1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi SoCs pinctrl driver
5 * Author: <alexandre.belloni@free-electrons.com>
6 * Author: <gregory.clement@bootlin.com>
7 * License: Dual MIT/GPL
8 * Copyright (c) 2017 Microsemi Corporation
12 #include <asm/system.h>
16 #include <dm/device-internal.h>
18 #include <dm/pinctrl.h>
23 #include "mscc-common.h"
25 static void mscc_writel(unsigned int offset, void *addr)
28 writel(BIT(offset), addr);
30 writel(BIT(offset % 32), addr + 4);
33 static unsigned int mscc_readl(unsigned int offset, void *addr)
38 return readl(addr + 4);
41 static void mscc_setbits(unsigned int offset, void *addr)
44 writel(readl(addr) | BIT(offset), addr);
46 writel(readl(addr + 4) | BIT(offset % 32), addr + 4);
49 static void mscc_clrbits(unsigned int offset, void *addr)
52 writel(readl(addr) & ~BIT(offset), addr);
54 writel(readl(addr + 4) & ~BIT(offset % 32), addr + 4);
57 static int mscc_get_functions_count(struct udevice *dev)
59 struct mscc_pinctrl *info = dev_get_priv(dev);
61 return info->num_func;
64 static const char *mscc_get_function_name(struct udevice *dev,
65 unsigned int function)
67 struct mscc_pinctrl *info = dev_get_priv(dev);
69 return info->function_names[function];
72 static int mscc_pin_function_idx(unsigned int pin, unsigned int function,
73 const struct mscc_pin_data *mscc_pins)
75 struct mscc_pin_caps *p = mscc_pins[pin].drv_data;
78 for (i = 0; i < MSCC_FUNC_PER_PIN; i++) {
79 if (function == p->functions[i])
86 static int mscc_pinmux_set_mux(struct udevice *dev,
87 unsigned int pin_selector, unsigned int selector)
89 struct mscc_pinctrl *info = dev_get_priv(dev);
90 struct mscc_pin_caps *pin = info->mscc_pins[pin_selector].drv_data;
91 int f, offset, regoff;
93 f = mscc_pin_function_idx(pin_selector, selector, info->mscc_pins);
97 * f is encoded on two bits.
98 * bit 0 of f goes in BIT(pin) of ALT0, bit 1 of f goes in BIT(pin) of
100 * This is racy because both registers can't be updated at the same time
101 * but it doesn't matter much for now.
104 regoff = info->mscc_gpios[MSCC_GPIO_ALT0];
106 offset = offset % 32;
107 regoff = info->mscc_gpios[MSCC_GPIO_ALT1];
111 mscc_setbits(offset, info->regs + regoff);
113 mscc_clrbits(offset, info->regs + regoff);
116 mscc_setbits(offset, info->regs + regoff + 4);
118 mscc_clrbits(offset, info->regs + regoff + 4);
123 static int mscc_pctl_get_groups_count(struct udevice *dev)
125 struct mscc_pinctrl *info = dev_get_priv(dev);
127 return info->num_pins;
130 static const char *mscc_pctl_get_group_name(struct udevice *dev,
133 struct mscc_pinctrl *info = dev_get_priv(dev);
135 return info->mscc_pins[group].name;
138 static int mscc_create_group_func_map(struct udevice *dev,
139 struct mscc_pinctrl *info)
141 u16 pins[info->num_pins];
144 for (f = 0; f < info->num_func; f++) {
145 for (npins = 0, i = 0; i < info->num_pins; i++) {
146 if (mscc_pin_function_idx(i, f, info->mscc_pins) >= 0)
150 info->func[f].ngroups = npins;
151 info->func[f].groups = devm_kzalloc(dev, npins * sizeof(char *),
153 if (!info->func[f].groups)
156 for (i = 0; i < npins; i++)
157 info->func[f].groups[i] = info->mscc_pins[pins[i]].name;
163 static int mscc_pinctrl_register(struct udevice *dev, struct mscc_pinctrl *info)
167 ret = mscc_create_group_func_map(dev, info);
169 dev_err(dev, "Unable to create group func map.\n");
176 static int mscc_gpio_get(struct udevice *dev, unsigned int offset)
178 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
181 if (mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]) &
183 val = mscc_readl(offset,
184 info->regs + info->mscc_gpios[MSCC_GPIO_OUT]);
186 val = mscc_readl(offset,
187 info->regs + info->mscc_gpios[MSCC_GPIO_IN]);
189 return !!(val & BIT(offset % 32));
192 static int mscc_gpio_set(struct udevice *dev, unsigned int offset, int value)
194 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
198 info->regs + info->mscc_gpios[MSCC_GPIO_OUT_SET]);
201 info->regs + info->mscc_gpios[MSCC_GPIO_OUT_CLR]);
206 static int mscc_gpio_get_direction(struct udevice *dev, unsigned int offset)
208 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
211 val = mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
213 return (val & BIT(offset % 32)) ? GPIOF_OUTPUT : GPIOF_INPUT;
216 static int mscc_gpio_direction_input(struct udevice *dev, unsigned int offset)
218 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
220 mscc_clrbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
225 static int mscc_gpio_direction_output(struct udevice *dev,
226 unsigned int offset, int value)
228 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
230 mscc_setbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
232 return mscc_gpio_set(dev, offset, value);
235 const struct dm_gpio_ops mscc_gpio_ops = {
236 .set_value = mscc_gpio_set,
237 .get_value = mscc_gpio_get,
238 .get_function = mscc_gpio_get_direction,
239 .direction_input = mscc_gpio_direction_input,
240 .direction_output = mscc_gpio_direction_output,
243 const struct pinctrl_ops mscc_pinctrl_ops = {
244 .get_pins_count = mscc_pctl_get_groups_count,
245 .get_pin_name = mscc_pctl_get_group_name,
246 .get_functions_count = mscc_get_functions_count,
247 .get_function_name = mscc_get_function_name,
248 .pinmux_set = mscc_pinmux_set_mux,
249 .set_state = pinctrl_generic_set_state,
252 int mscc_pinctrl_probe(struct udevice *dev, int num_func,
253 const struct mscc_pin_data *mscc_pins, int num_pins,
254 char * const *function_names,
255 const unsigned long *mscc_gpios)
257 struct mscc_pinctrl *priv = dev_get_priv(dev);
260 priv->regs = dev_remap_addr(dev);
264 priv->func = devm_kzalloc(dev, num_func * sizeof(struct mscc_pmx_func),
266 priv->num_func = num_func;
267 priv->mscc_pins = mscc_pins;
268 priv->num_pins = num_pins;
269 priv->function_names = function_names;
270 priv->mscc_gpios = mscc_gpios;
271 ret = mscc_pinctrl_register(dev, priv);