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>
17 #include <dm/devres.h>
19 #include <dm/pinctrl.h>
24 #include "mscc-common.h"
26 static void mscc_writel(unsigned int offset, void *addr)
29 writel(BIT(offset), addr);
31 writel(BIT(offset % 32), addr + 4);
34 static unsigned int mscc_readl(unsigned int offset, void *addr)
39 return readl(addr + 4);
42 static void mscc_setbits(unsigned int offset, void *addr)
45 writel(readl(addr) | BIT(offset), addr);
47 writel(readl(addr + 4) | BIT(offset % 32), addr + 4);
50 static void mscc_clrbits(unsigned int offset, void *addr)
53 writel(readl(addr) & ~BIT(offset), addr);
55 writel(readl(addr + 4) & ~BIT(offset % 32), addr + 4);
58 static int mscc_get_functions_count(struct udevice *dev)
60 struct mscc_pinctrl *info = dev_get_priv(dev);
62 return info->num_func;
65 static const char *mscc_get_function_name(struct udevice *dev,
66 unsigned int function)
68 struct mscc_pinctrl *info = dev_get_priv(dev);
70 return info->function_names[function];
73 static int mscc_pin_function_idx(unsigned int pin, unsigned int function,
74 const struct mscc_pin_data *mscc_pins)
76 struct mscc_pin_caps *p = mscc_pins[pin].drv_data;
79 for (i = 0; i < MSCC_FUNC_PER_PIN; i++) {
80 if (function == p->functions[i])
87 static int mscc_pinmux_set_mux(struct udevice *dev,
88 unsigned int pin_selector, unsigned int selector)
90 struct mscc_pinctrl *info = dev_get_priv(dev);
91 struct mscc_pin_caps *pin = info->mscc_pins[pin_selector].drv_data;
92 int f, offset, regoff;
94 f = mscc_pin_function_idx(pin_selector, selector, info->mscc_pins);
98 * f is encoded on two bits.
99 * bit 0 of f goes in BIT(pin) of ALT0, bit 1 of f goes in BIT(pin) of
101 * This is racy because both registers can't be updated at the same time
102 * but it doesn't matter much for now.
105 regoff = info->mscc_gpios[MSCC_GPIO_ALT0];
107 offset = offset % 32;
108 regoff = info->mscc_gpios[MSCC_GPIO_ALT1];
112 mscc_setbits(offset, info->regs + regoff);
114 mscc_clrbits(offset, info->regs + regoff);
117 mscc_setbits(offset, info->regs + regoff + 4);
119 mscc_clrbits(offset, info->regs + regoff + 4);
124 static int mscc_pctl_get_groups_count(struct udevice *dev)
126 struct mscc_pinctrl *info = dev_get_priv(dev);
128 return info->num_pins;
131 static const char *mscc_pctl_get_group_name(struct udevice *dev,
134 struct mscc_pinctrl *info = dev_get_priv(dev);
136 return info->mscc_pins[group].name;
139 static int mscc_create_group_func_map(struct udevice *dev,
140 struct mscc_pinctrl *info)
142 u16 pins[info->num_pins];
145 for (f = 0; f < info->num_func; f++) {
146 for (npins = 0, i = 0; i < info->num_pins; i++) {
147 if (mscc_pin_function_idx(i, f, info->mscc_pins) >= 0)
151 info->func[f].ngroups = npins;
152 info->func[f].groups = devm_kzalloc(dev, npins * sizeof(char *),
154 if (!info->func[f].groups)
157 for (i = 0; i < npins; i++)
158 info->func[f].groups[i] = info->mscc_pins[pins[i]].name;
164 static int mscc_pinctrl_register(struct udevice *dev, struct mscc_pinctrl *info)
168 ret = mscc_create_group_func_map(dev, info);
170 dev_err(dev, "Unable to create group func map.\n");
177 static int mscc_gpio_get(struct udevice *dev, unsigned int offset)
179 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
182 if (mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]) &
184 val = mscc_readl(offset,
185 info->regs + info->mscc_gpios[MSCC_GPIO_OUT]);
187 val = mscc_readl(offset,
188 info->regs + info->mscc_gpios[MSCC_GPIO_IN]);
190 return !!(val & BIT(offset % 32));
193 static int mscc_gpio_set(struct udevice *dev, unsigned int offset, int value)
195 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
199 info->regs + info->mscc_gpios[MSCC_GPIO_OUT_SET]);
202 info->regs + info->mscc_gpios[MSCC_GPIO_OUT_CLR]);
207 static int mscc_gpio_get_direction(struct udevice *dev, unsigned int offset)
209 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
212 val = mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
214 return (val & BIT(offset % 32)) ? GPIOF_OUTPUT : GPIOF_INPUT;
217 static int mscc_gpio_direction_input(struct udevice *dev, unsigned int offset)
219 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
221 mscc_clrbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
226 static int mscc_gpio_direction_output(struct udevice *dev,
227 unsigned int offset, int value)
229 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
231 mscc_setbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
233 return mscc_gpio_set(dev, offset, value);
236 const struct dm_gpio_ops mscc_gpio_ops = {
237 .set_value = mscc_gpio_set,
238 .get_value = mscc_gpio_get,
239 .get_function = mscc_gpio_get_direction,
240 .direction_input = mscc_gpio_direction_input,
241 .direction_output = mscc_gpio_direction_output,
244 const struct pinctrl_ops mscc_pinctrl_ops = {
245 .get_pins_count = mscc_pctl_get_groups_count,
246 .get_pin_name = mscc_pctl_get_group_name,
247 .get_functions_count = mscc_get_functions_count,
248 .get_function_name = mscc_get_function_name,
249 .pinmux_set = mscc_pinmux_set_mux,
250 .set_state = pinctrl_generic_set_state,
253 int mscc_pinctrl_probe(struct udevice *dev, int num_func,
254 const struct mscc_pin_data *mscc_pins, int num_pins,
255 char * const *function_names,
256 const unsigned long *mscc_gpios)
258 struct mscc_pinctrl *priv = dev_get_priv(dev);
261 priv->regs = dev_remap_addr(dev);
265 priv->func = devm_kzalloc(dev, num_func * sizeof(struct mscc_pmx_func),
267 priv->num_func = num_func;
268 priv->mscc_pins = mscc_pins;
269 priv->num_pins = num_pins;
270 priv->function_names = function_names;
271 priv->mscc_gpios = mscc_gpios;
272 ret = mscc_pinctrl_register(dev, priv);