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/device_compat.h>
18 #include <dm/devres.h>
20 #include <dm/pinctrl.h>
24 #include <linux/bitops.h>
26 #include "mscc-common.h"
28 static void mscc_writel(unsigned int offset, void *addr)
31 writel(BIT(offset), addr);
33 writel(BIT(offset % 32), addr + 4);
36 static unsigned int mscc_readl(unsigned int offset, void *addr)
41 return readl(addr + 4);
44 static void mscc_setbits(unsigned int offset, void *addr)
47 writel(readl(addr) | BIT(offset), addr);
49 writel(readl(addr + 4) | BIT(offset % 32), addr + 4);
52 static void mscc_clrbits(unsigned int offset, void *addr)
55 writel(readl(addr) & ~BIT(offset), addr);
57 writel(readl(addr + 4) & ~BIT(offset % 32), addr + 4);
60 static int mscc_get_functions_count(struct udevice *dev)
62 struct mscc_pinctrl *info = dev_get_priv(dev);
64 return info->num_func;
67 static const char *mscc_get_function_name(struct udevice *dev,
68 unsigned int function)
70 struct mscc_pinctrl *info = dev_get_priv(dev);
72 return info->function_names[function];
75 static int mscc_pin_function_idx(unsigned int pin, unsigned int function,
76 const struct mscc_pin_data *mscc_pins)
78 struct mscc_pin_caps *p = mscc_pins[pin].drv_data;
81 for (i = 0; i < MSCC_FUNC_PER_PIN; i++) {
82 if (function == p->functions[i])
89 static int mscc_pinmux_set_mux(struct udevice *dev,
90 unsigned int pin_selector, unsigned int selector)
92 struct mscc_pinctrl *info = dev_get_priv(dev);
93 struct mscc_pin_caps *pin = info->mscc_pins[pin_selector].drv_data;
94 int f, offset, regoff;
96 f = mscc_pin_function_idx(pin_selector, selector, info->mscc_pins);
100 * f is encoded on two bits.
101 * bit 0 of f goes in BIT(pin) of ALT0, bit 1 of f goes in BIT(pin) of
103 * This is racy because both registers can't be updated at the same time
104 * but it doesn't matter much for now.
107 regoff = info->mscc_gpios[MSCC_GPIO_ALT0];
109 offset = offset % 32;
110 regoff = info->mscc_gpios[MSCC_GPIO_ALT1];
114 mscc_setbits(offset, info->regs + regoff);
116 mscc_clrbits(offset, info->regs + regoff);
119 mscc_setbits(offset, info->regs + regoff + 4);
121 mscc_clrbits(offset, info->regs + regoff + 4);
126 static int mscc_pctl_get_groups_count(struct udevice *dev)
128 struct mscc_pinctrl *info = dev_get_priv(dev);
130 return info->num_pins;
133 static const char *mscc_pctl_get_group_name(struct udevice *dev,
136 struct mscc_pinctrl *info = dev_get_priv(dev);
138 return info->mscc_pins[group].name;
141 static int mscc_create_group_func_map(struct udevice *dev,
142 struct mscc_pinctrl *info)
144 u16 pins[info->num_pins];
147 for (f = 0; f < info->num_func; f++) {
148 for (npins = 0, i = 0; i < info->num_pins; i++) {
149 if (mscc_pin_function_idx(i, f, info->mscc_pins) >= 0)
153 info->func[f].ngroups = npins;
154 info->func[f].groups = devm_kzalloc(dev, npins * sizeof(char *),
156 if (!info->func[f].groups)
159 for (i = 0; i < npins; i++)
160 info->func[f].groups[i] = info->mscc_pins[pins[i]].name;
166 static int mscc_pinctrl_register(struct udevice *dev, struct mscc_pinctrl *info)
170 ret = mscc_create_group_func_map(dev, info);
172 dev_err(dev, "Unable to create group func map.\n");
179 static int mscc_gpio_get(struct udevice *dev, unsigned int offset)
181 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
184 if (mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]) &
186 val = mscc_readl(offset,
187 info->regs + info->mscc_gpios[MSCC_GPIO_OUT]);
189 val = mscc_readl(offset,
190 info->regs + info->mscc_gpios[MSCC_GPIO_IN]);
192 return !!(val & BIT(offset % 32));
195 static int mscc_gpio_set(struct udevice *dev, unsigned int offset, int value)
197 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
201 info->regs + info->mscc_gpios[MSCC_GPIO_OUT_SET]);
204 info->regs + info->mscc_gpios[MSCC_GPIO_OUT_CLR]);
209 static int mscc_gpio_get_direction(struct udevice *dev, unsigned int offset)
211 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
214 val = mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
216 return (val & BIT(offset % 32)) ? GPIOF_OUTPUT : GPIOF_INPUT;
219 static int mscc_gpio_direction_input(struct udevice *dev, unsigned int offset)
221 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
223 mscc_clrbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
228 static int mscc_gpio_direction_output(struct udevice *dev,
229 unsigned int offset, int value)
231 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
233 mscc_setbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
235 return mscc_gpio_set(dev, offset, value);
238 const struct dm_gpio_ops mscc_gpio_ops = {
239 .set_value = mscc_gpio_set,
240 .get_value = mscc_gpio_get,
241 .get_function = mscc_gpio_get_direction,
242 .direction_input = mscc_gpio_direction_input,
243 .direction_output = mscc_gpio_direction_output,
246 const struct pinctrl_ops mscc_pinctrl_ops = {
247 .get_pins_count = mscc_pctl_get_groups_count,
248 .get_pin_name = mscc_pctl_get_group_name,
249 .get_functions_count = mscc_get_functions_count,
250 .get_function_name = mscc_get_function_name,
251 .pinmux_set = mscc_pinmux_set_mux,
252 .set_state = pinctrl_generic_set_state,
255 int mscc_pinctrl_probe(struct udevice *dev, int num_func,
256 const struct mscc_pin_data *mscc_pins, int num_pins,
257 char * const *function_names,
258 const unsigned long *mscc_gpios)
260 struct mscc_pinctrl *priv = dev_get_priv(dev);
263 priv->regs = dev_remap_addr(dev);
267 priv->func = devm_kzalloc(dev, num_func * sizeof(struct mscc_pmx_func),
269 priv->num_func = num_func;
270 priv->mscc_pins = mscc_pins;
271 priv->num_pins = num_pins;
272 priv->function_names = function_names;
273 priv->mscc_gpios = mscc_gpios;
274 ret = mscc_pinctrl_register(dev, priv);