dm: core: Create a new header file for 'compat' features
[oweals/u-boot.git] / drivers / pinctrl / mscc / mscc-common.c
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /*
3  * Microsemi SoCs pinctrl driver
4  *
5  * Author: <alexandre.belloni@free-electrons.com>
6  * Author: <gregory.clement@bootlin.com>
7  * License: Dual MIT/GPL
8  * Copyright (c) 2017 Microsemi Corporation
9  */
10
11 #include <asm/gpio.h>
12 #include <asm/system.h>
13 #include <common.h>
14 #include <config.h>
15 #include <dm.h>
16 #include <dm/device-internal.h>
17 #include <dm/device_compat.h>
18 #include <dm/devres.h>
19 #include <dm/lists.h>
20 #include <dm/pinctrl.h>
21 #include <dm/root.h>
22 #include <errno.h>
23 #include <fdtdec.h>
24 #include <linux/io.h>
25 #include "mscc-common.h"
26
27 static void mscc_writel(unsigned int offset, void *addr)
28 {
29         if (offset < 32)
30                 writel(BIT(offset), addr);
31         else
32                 writel(BIT(offset % 32), addr + 4);
33 }
34
35 static unsigned int mscc_readl(unsigned int offset, void *addr)
36 {
37         if (offset < 32)
38                 return readl(addr);
39         else
40                 return readl(addr + 4);
41 }
42
43 static void mscc_setbits(unsigned int offset, void *addr)
44 {
45         if (offset < 32)
46                 writel(readl(addr) | BIT(offset), addr);
47         else
48                 writel(readl(addr + 4) | BIT(offset % 32), addr + 4);
49 }
50
51 static void mscc_clrbits(unsigned int offset, void *addr)
52 {
53         if (offset < 32)
54                 writel(readl(addr) & ~BIT(offset), addr);
55         else
56                 writel(readl(addr + 4) & ~BIT(offset % 32), addr + 4);
57 }
58
59 static int mscc_get_functions_count(struct udevice *dev)
60 {
61         struct mscc_pinctrl *info = dev_get_priv(dev);
62
63         return info->num_func;
64 }
65
66 static const char *mscc_get_function_name(struct udevice *dev,
67                                           unsigned int function)
68 {
69         struct mscc_pinctrl *info = dev_get_priv(dev);
70
71         return info->function_names[function];
72 }
73
74 static int mscc_pin_function_idx(unsigned int pin, unsigned int function,
75                                  const struct mscc_pin_data *mscc_pins)
76 {
77         struct mscc_pin_caps *p = mscc_pins[pin].drv_data;
78         int i;
79
80         for (i = 0; i < MSCC_FUNC_PER_PIN; i++) {
81                 if (function == p->functions[i])
82                         return i;
83         }
84
85         return -1;
86 }
87
88 static int mscc_pinmux_set_mux(struct udevice *dev,
89                                unsigned int pin_selector, unsigned int selector)
90 {
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;
94
95         f = mscc_pin_function_idx(pin_selector, selector, info->mscc_pins);
96         if (f < 0)
97                 return -EINVAL;
98         /*
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
101          * ALT1
102          * This is racy because both registers can't be updated at the same time
103          * but it doesn't matter much for now.
104          */
105         offset = pin->pin;
106         regoff = info->mscc_gpios[MSCC_GPIO_ALT0];
107         if (offset >= 32) {
108                 offset = offset % 32;
109                 regoff = info->mscc_gpios[MSCC_GPIO_ALT1];
110         }
111
112         if (f & BIT(0))
113                 mscc_setbits(offset, info->regs + regoff);
114         else
115                 mscc_clrbits(offset, info->regs + regoff);
116
117         if (f & BIT(1))
118                 mscc_setbits(offset, info->regs + regoff + 4);
119         else
120                 mscc_clrbits(offset, info->regs + regoff + 4);
121
122         return 0;
123 }
124
125 static int mscc_pctl_get_groups_count(struct udevice *dev)
126 {
127         struct mscc_pinctrl *info = dev_get_priv(dev);
128
129         return info->num_pins;
130 }
131
132 static const char *mscc_pctl_get_group_name(struct udevice *dev,
133                                             unsigned int group)
134 {
135         struct mscc_pinctrl *info = dev_get_priv(dev);
136
137         return info->mscc_pins[group].name;
138 }
139
140 static int mscc_create_group_func_map(struct udevice *dev,
141                                       struct mscc_pinctrl *info)
142 {
143         u16 pins[info->num_pins];
144         int f, npins, i;
145
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)
149                                 pins[npins++] = i;
150                 }
151
152                 info->func[f].ngroups = npins;
153                 info->func[f].groups = devm_kzalloc(dev, npins * sizeof(char *),
154                                                     GFP_KERNEL);
155                 if (!info->func[f].groups)
156                         return -ENOMEM;
157
158                 for (i = 0; i < npins; i++)
159                         info->func[f].groups[i] = info->mscc_pins[pins[i]].name;
160         }
161
162         return 0;
163 }
164
165 static int mscc_pinctrl_register(struct udevice *dev, struct mscc_pinctrl *info)
166 {
167         int ret;
168
169         ret = mscc_create_group_func_map(dev, info);
170         if (ret) {
171                 dev_err(dev, "Unable to create group func map.\n");
172                 return ret;
173         }
174
175         return 0;
176 }
177
178 static int mscc_gpio_get(struct udevice *dev, unsigned int offset)
179 {
180         struct mscc_pinctrl *info = dev_get_priv(dev->parent);
181         unsigned int val;
182
183         if (mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]) &
184             BIT(offset % 32))
185                 val = mscc_readl(offset,
186                                  info->regs + info->mscc_gpios[MSCC_GPIO_OUT]);
187         else
188                 val = mscc_readl(offset,
189                                  info->regs + info->mscc_gpios[MSCC_GPIO_IN]);
190
191         return !!(val & BIT(offset % 32));
192 }
193
194 static int mscc_gpio_set(struct udevice *dev, unsigned int offset, int value)
195 {
196         struct mscc_pinctrl *info = dev_get_priv(dev->parent);
197
198         if (value)
199                 mscc_writel(offset,
200                             info->regs + info->mscc_gpios[MSCC_GPIO_OUT_SET]);
201         else
202                 mscc_writel(offset,
203                             info->regs + info->mscc_gpios[MSCC_GPIO_OUT_CLR]);
204
205         return 0;
206 }
207
208 static int mscc_gpio_get_direction(struct udevice *dev, unsigned int offset)
209 {
210         struct mscc_pinctrl *info = dev_get_priv(dev->parent);
211         unsigned int val;
212
213         val = mscc_readl(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
214
215         return (val & BIT(offset % 32)) ? GPIOF_OUTPUT : GPIOF_INPUT;
216 }
217
218 static int mscc_gpio_direction_input(struct udevice *dev, unsigned int offset)
219 {
220         struct mscc_pinctrl *info = dev_get_priv(dev->parent);
221
222         mscc_clrbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
223
224         return 0;
225 }
226
227 static int mscc_gpio_direction_output(struct udevice *dev,
228                                       unsigned int offset, int value)
229 {
230         struct mscc_pinctrl *info = dev_get_priv(dev->parent);
231
232         mscc_setbits(offset, info->regs + info->mscc_gpios[MSCC_GPIO_OE]);
233
234         return mscc_gpio_set(dev, offset, value);
235 }
236
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,
243 };
244
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,
252 };
253
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)
258 {
259         struct mscc_pinctrl *priv = dev_get_priv(dev);
260         int ret;
261
262         priv->regs = dev_remap_addr(dev);
263         if (!priv->regs)
264                 return -EINVAL;
265
266         priv->func = devm_kzalloc(dev, num_func * sizeof(struct mscc_pmx_func),
267                                   GFP_KERNEL);
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);
274
275         return ret;
276 }