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>
25 #include "mscc-common.h"
27 static void mscc_writel(unsigned int offset, void *addr)
30 writel(BIT(offset), addr);
32 writel(BIT(offset % 32), addr + 4);
35 static unsigned int mscc_readl(unsigned int offset, void *addr)
40 return readl(addr + 4);
43 static void mscc_setbits(unsigned int offset, void *addr)
46 writel(readl(addr) | BIT(offset), addr);
48 writel(readl(addr + 4) | BIT(offset % 32), addr + 4);
51 static void mscc_clrbits(unsigned int offset, void *addr)
54 writel(readl(addr) & ~BIT(offset), addr);
56 writel(readl(addr + 4) & ~BIT(offset % 32), addr + 4);
59 static int mscc_get_functions_count(struct udevice *dev)
61 struct mscc_pinctrl *info = dev_get_priv(dev);
63 return info->num_func;
66 static const char *mscc_get_function_name(struct udevice *dev,
67 unsigned int function)
69 struct mscc_pinctrl *info = dev_get_priv(dev);
71 return info->function_names[function];
74 static int mscc_pin_function_idx(unsigned int pin, unsigned int function,
75 const struct mscc_pin_data *mscc_pins)
77 struct mscc_pin_caps *p = mscc_pins[pin].drv_data;
80 for (i = 0; i < MSCC_FUNC_PER_PIN; i++) {
81 if (function == p->functions[i])
88 static int mscc_pinmux_set_mux(struct udevice *dev,
89 unsigned int pin_selector, unsigned int selector)
91 struct mscc_pinctrl *info = dev_get_priv(dev);
92 struct mscc_pin_caps *pin = info->mscc_pins[pin_selector].drv_data;
93 int f, offset, regoff;
95 f = mscc_pin_function_idx(pin_selector, selector, info->mscc_pins);
99 * f is encoded on two bits.
100 * bit 0 of f goes in BIT(pin) of ALT0, bit 1 of f goes in BIT(pin) of
102 * This is racy because both registers can't be updated at the same time
103 * but it doesn't matter much for now.
106 regoff = info->mscc_gpios[MSCC_GPIO_ALT0];
108 offset = offset % 32;
109 regoff = info->mscc_gpios[MSCC_GPIO_ALT1];
113 mscc_setbits(offset, info->regs + regoff);
115 mscc_clrbits(offset, info->regs + regoff);
118 mscc_setbits(offset, info->regs + regoff + 4);
120 mscc_clrbits(offset, info->regs + regoff + 4);
125 static int mscc_pctl_get_groups_count(struct udevice *dev)
127 struct mscc_pinctrl *info = dev_get_priv(dev);
129 return info->num_pins;
132 static const char *mscc_pctl_get_group_name(struct udevice *dev,
135 struct mscc_pinctrl *info = dev_get_priv(dev);
137 return info->mscc_pins[group].name;
140 static int mscc_create_group_func_map(struct udevice *dev,
141 struct mscc_pinctrl *info)
143 u16 pins[info->num_pins];
146 for (f = 0; f < info->num_func; f++) {
147 for (npins = 0, i = 0; i < info->num_pins; i++) {
148 if (mscc_pin_function_idx(i, f, info->mscc_pins) >= 0)
152 info->func[f].ngroups = npins;
153 info->func[f].groups = devm_kzalloc(dev, npins * sizeof(char *),
155 if (!info->func[f].groups)
158 for (i = 0; i < npins; i++)
159 info->func[f].groups[i] = info->mscc_pins[pins[i]].name;
165 static int mscc_pinctrl_register(struct udevice *dev, struct mscc_pinctrl *info)
169 ret = mscc_create_group_func_map(dev, info);
171 dev_err(dev, "Unable to create group func map.\n");
178 static int mscc_gpio_get(struct udevice *dev, unsigned int offset)
180 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
183 if (mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]) &
185 val = mscc_readl(offset,
186 info->regs + info->mscc_gpios[MSCC_GPIO_OUT]);
188 val = mscc_readl(offset,
189 info->regs + info->mscc_gpios[MSCC_GPIO_IN]);
191 return !!(val & BIT(offset % 32));
194 static int mscc_gpio_set(struct udevice *dev, unsigned int offset, int value)
196 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
200 info->regs + info->mscc_gpios[MSCC_GPIO_OUT_SET]);
203 info->regs + info->mscc_gpios[MSCC_GPIO_OUT_CLR]);
208 static int mscc_gpio_get_direction(struct udevice *dev, unsigned int offset)
210 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
213 val = mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
215 return (val & BIT(offset % 32)) ? GPIOF_OUTPUT : GPIOF_INPUT;
218 static int mscc_gpio_direction_input(struct udevice *dev, unsigned int offset)
220 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
222 mscc_clrbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
227 static int mscc_gpio_direction_output(struct udevice *dev,
228 unsigned int offset, int value)
230 struct mscc_pinctrl *info = dev_get_priv(dev->parent);
232 mscc_setbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
234 return mscc_gpio_set(dev, offset, value);
237 const struct dm_gpio_ops mscc_gpio_ops = {
238 .set_value = mscc_gpio_set,
239 .get_value = mscc_gpio_get,
240 .get_function = mscc_gpio_get_direction,
241 .direction_input = mscc_gpio_direction_input,
242 .direction_output = mscc_gpio_direction_output,
245 const struct pinctrl_ops mscc_pinctrl_ops = {
246 .get_pins_count = mscc_pctl_get_groups_count,
247 .get_pin_name = mscc_pctl_get_group_name,
248 .get_functions_count = mscc_get_functions_count,
249 .get_function_name = mscc_get_function_name,
250 .pinmux_set = mscc_pinmux_set_mux,
251 .set_state = pinctrl_generic_set_state,
254 int mscc_pinctrl_probe(struct udevice *dev, int num_func,
255 const struct mscc_pin_data *mscc_pins, int num_pins,
256 char * const *function_names,
257 const unsigned long *mscc_gpios)
259 struct mscc_pinctrl *priv = dev_get_priv(dev);
262 priv->regs = dev_remap_addr(dev);
266 priv->func = devm_kzalloc(dev, num_func * sizeof(struct mscc_pmx_func),
268 priv->num_func = num_func;
269 priv->mscc_pins = mscc_pins;
270 priv->num_pins = num_pins;
271 priv->function_names = function_names;
272 priv->mscc_gpios = mscc_gpios;
273 ret = mscc_pinctrl_register(dev, priv);