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