Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[linux-2.6-microblaze.git] / drivers / staging / mt7621-pinctrl / pinctrl-rt2880.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  */
5
6 #include <linux/module.h>
7 #include <linux/device.h>
8 #include <linux/io.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/of.h>
12 #include <linux/pinctrl/pinctrl.h>
13 #include <linux/pinctrl/pinconf.h>
14 #include <linux/pinctrl/pinmux.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/pinctrl/machine.h>
17
18 #include <asm/mach-ralink/ralink_regs.h>
19 #include <asm/mach-ralink/pinmux.h>
20 #include <asm/mach-ralink/mt7620.h>
21
22 #include "core.h"
23 #include "pinctrl-utils.h"
24
25 #define SYSC_REG_GPIO_MODE      0x60
26 #define SYSC_REG_GPIO_MODE2     0x64
27
28 struct rt2880_priv {
29         struct device *dev;
30
31         struct pinctrl_pin_desc *pads;
32         struct pinctrl_desc *desc;
33
34         struct rt2880_pmx_func **func;
35         int func_count;
36
37         struct rt2880_pmx_group *groups;
38         const char **group_names;
39         int group_count;
40
41         u8 *gpio;
42         int max_pins;
43 };
44
45 static int rt2880_get_group_count(struct pinctrl_dev *pctrldev)
46 {
47         struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
48
49         return p->group_count;
50 }
51
52 static const char *rt2880_get_group_name(struct pinctrl_dev *pctrldev,
53                                          unsigned int group)
54 {
55         struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
56
57         return (group >= p->group_count) ? NULL : p->group_names[group];
58 }
59
60 static int rt2880_get_group_pins(struct pinctrl_dev *pctrldev,
61                                  unsigned int group,
62                                  const unsigned int **pins,
63                                  unsigned int *num_pins)
64 {
65         struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
66
67         if (group >= p->group_count)
68                 return -EINVAL;
69
70         *pins = p->groups[group].func[0].pins;
71         *num_pins = p->groups[group].func[0].pin_count;
72
73         return 0;
74 }
75
76 static int rt2880_pinctrl_dt_node_to_map(struct pinctrl_dev *pctrldev,
77                                          struct device_node *np_config,
78                                          struct pinctrl_map **map,
79                                          unsigned int *num_maps)
80 {
81         struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
82         struct property *prop;
83         const char *function_name, *group_name;
84         int ret;
85         int ngroups = 0;
86         unsigned int reserved_maps = 0;
87
88         for_each_node_with_property(np_config, "group")
89                 ngroups++;
90
91         *map = NULL;
92         ret = pinctrl_utils_reserve_map(pctrldev, map, &reserved_maps,
93                                         num_maps, ngroups);
94         if (ret) {
95                 dev_err(p->dev, "can't reserve map: %d\n", ret);
96                 return ret;
97         }
98
99         of_property_for_each_string(np_config, "group", prop, group_name) {
100                 ret = pinctrl_utils_add_map_mux(pctrldev, map, &reserved_maps,
101                                                 num_maps, group_name,
102                                                 function_name);
103                 if (ret) {
104                         dev_err(p->dev, "can't add map: %d\n", ret);
105                         return ret;
106                 }
107         }
108
109         return 0;
110 }
111
112 static const struct pinctrl_ops rt2880_pctrl_ops = {
113         .get_groups_count       = rt2880_get_group_count,
114         .get_group_name         = rt2880_get_group_name,
115         .get_group_pins         = rt2880_get_group_pins,
116         .dt_node_to_map         = rt2880_pinctrl_dt_node_to_map,
117         .dt_free_map            = pinctrl_utils_free_map,
118 };
119
120 static int rt2880_pmx_func_count(struct pinctrl_dev *pctrldev)
121 {
122         struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
123
124         return p->func_count;
125 }
126
127 static const char *rt2880_pmx_func_name(struct pinctrl_dev *pctrldev,
128                                         unsigned int func)
129 {
130         struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
131
132         return p->func[func]->name;
133 }
134
135 static int rt2880_pmx_group_get_groups(struct pinctrl_dev *pctrldev,
136                                        unsigned int func,
137                                        const char * const **groups,
138                                        unsigned int * const num_groups)
139 {
140         struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
141
142         if (p->func[func]->group_count == 1)
143                 *groups = &p->group_names[p->func[func]->groups[0]];
144         else
145                 *groups = p->group_names;
146
147         *num_groups = p->func[func]->group_count;
148
149         return 0;
150 }
151
152 static int rt2880_pmx_group_enable(struct pinctrl_dev *pctrldev,
153                                    unsigned int func, unsigned int group)
154 {
155         struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
156         u32 mode = 0;
157         u32 reg = SYSC_REG_GPIO_MODE;
158         int i;
159         int shift;
160
161         /* dont allow double use */
162         if (p->groups[group].enabled) {
163                 dev_err(p->dev, "%s is already enabled\n",
164                         p->groups[group].name);
165                 return -EBUSY;
166         }
167
168         p->groups[group].enabled = 1;
169         p->func[func]->enabled = 1;
170
171         shift = p->groups[group].shift;
172         if (shift >= 32) {
173                 shift -= 32;
174                 reg = SYSC_REG_GPIO_MODE2;
175         }
176         mode = rt_sysc_r32(reg);
177         mode &= ~(p->groups[group].mask << shift);
178
179         /* mark the pins as gpio */
180         for (i = 0; i < p->groups[group].func[0].pin_count; i++)
181                 p->gpio[p->groups[group].func[0].pins[i]] = 1;
182
183         /* function 0 is gpio and needs special handling */
184         if (func == 0) {
185                 mode |= p->groups[group].gpio << shift;
186         } else {
187                 for (i = 0; i < p->func[func]->pin_count; i++)
188                         p->gpio[p->func[func]->pins[i]] = 0;
189                 mode |= p->func[func]->value << shift;
190         }
191         rt_sysc_w32(mode, reg);
192
193         return 0;
194 }
195
196 static int rt2880_pmx_group_gpio_request_enable(struct pinctrl_dev *pctrldev,
197                                                 struct pinctrl_gpio_range *range,
198                                                 unsigned int pin)
199 {
200         struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
201
202         if (!p->gpio[pin]) {
203                 dev_err(p->dev, "pin %d is not set to gpio mux\n", pin);
204                 return -EINVAL;
205         }
206
207         return 0;
208 }
209
210 static const struct pinmux_ops rt2880_pmx_group_ops = {
211         .get_functions_count    = rt2880_pmx_func_count,
212         .get_function_name      = rt2880_pmx_func_name,
213         .get_function_groups    = rt2880_pmx_group_get_groups,
214         .set_mux                = rt2880_pmx_group_enable,
215         .gpio_request_enable    = rt2880_pmx_group_gpio_request_enable,
216 };
217
218 static struct pinctrl_desc rt2880_pctrl_desc = {
219         .owner          = THIS_MODULE,
220         .name           = "rt2880-pinmux",
221         .pctlops        = &rt2880_pctrl_ops,
222         .pmxops         = &rt2880_pmx_group_ops,
223 };
224
225 static struct rt2880_pmx_func gpio_func = {
226         .name = "gpio",
227 };
228
229 static int rt2880_pinmux_index(struct rt2880_priv *p)
230 {
231         struct rt2880_pmx_func **f;
232         struct rt2880_pmx_group *mux = p->groups;
233         int i, j, c = 0;
234
235         /* count the mux functions */
236         while (mux->name) {
237                 p->group_count++;
238                 mux++;
239         }
240
241         /* allocate the group names array needed by the gpio function */
242         p->group_names = devm_kcalloc(p->dev, p->group_count,
243                                       sizeof(char *), GFP_KERNEL);
244         if (!p->group_names)
245                 return -1;
246
247         for (i = 0; i < p->group_count; i++) {
248                 p->group_names[i] = p->groups[i].name;
249                 p->func_count += p->groups[i].func_count;
250         }
251
252         /* we have a dummy function[0] for gpio */
253         p->func_count++;
254
255         /* allocate our function and group mapping index buffers */
256         f = p->func = devm_kcalloc(p->dev,
257                                    p->func_count,
258                                    sizeof(struct rt2880_pmx_func),
259                                    GFP_KERNEL);
260         gpio_func.groups = devm_kcalloc(p->dev, p->group_count, sizeof(int),
261                                         GFP_KERNEL);
262         if (!f || !gpio_func.groups)
263                 return -1;
264
265         /* add a backpointer to the function so it knows its group */
266         gpio_func.group_count = p->group_count;
267         for (i = 0; i < gpio_func.group_count; i++)
268                 gpio_func.groups[i] = i;
269
270         f[c] = &gpio_func;
271         c++;
272
273         /* add remaining functions */
274         for (i = 0; i < p->group_count; i++) {
275                 for (j = 0; j < p->groups[i].func_count; j++) {
276                         f[c] = &p->groups[i].func[j];
277                         f[c]->groups = devm_kzalloc(p->dev, sizeof(int),
278                                                     GFP_KERNEL);
279                         f[c]->groups[0] = i;
280                         f[c]->group_count = 1;
281                         c++;
282                 }
283         }
284         return 0;
285 }
286
287 static int rt2880_pinmux_pins(struct rt2880_priv *p)
288 {
289         int i, j;
290
291         /*
292          * loop over the functions and initialize the pins array.
293          * also work out the highest pin used.
294          */
295         for (i = 0; i < p->func_count; i++) {
296                 int pin;
297
298                 if (!p->func[i]->pin_count)
299                         continue;
300
301                 p->func[i]->pins = devm_kcalloc(p->dev,
302                                                 p->func[i]->pin_count,
303                                                 sizeof(int),
304                                                 GFP_KERNEL);
305                 for (j = 0; j < p->func[i]->pin_count; j++)
306                         p->func[i]->pins[j] = p->func[i]->pin_first + j;
307
308                 pin = p->func[i]->pin_first + p->func[i]->pin_count;
309                 if (pin > p->max_pins)
310                         p->max_pins = pin;
311         }
312
313         /* the buffer that tells us which pins are gpio */
314         p->gpio = devm_kcalloc(p->dev, p->max_pins, sizeof(u8), GFP_KERNEL);
315         /* the pads needed to tell pinctrl about our pins */
316         p->pads = devm_kcalloc(p->dev, p->max_pins,
317                                sizeof(struct pinctrl_pin_desc), GFP_KERNEL);
318         if (!p->pads || !p->gpio) {
319                 dev_err(p->dev, "Failed to allocate gpio data\n");
320                 return -ENOMEM;
321         }
322
323         memset(p->gpio, 1, sizeof(u8) * p->max_pins);
324         for (i = 0; i < p->func_count; i++) {
325                 if (!p->func[i]->pin_count)
326                         continue;
327
328                 for (j = 0; j < p->func[i]->pin_count; j++)
329                         p->gpio[p->func[i]->pins[j]] = 0;
330         }
331
332         /* pin 0 is always a gpio */
333         p->gpio[0] = 1;
334
335         /* set the pads */
336         for (i = 0; i < p->max_pins; i++) {
337                 /* strlen("ioXY") + 1 = 5 */
338                 char *name = devm_kzalloc(p->dev, 5, GFP_KERNEL);
339
340                 if (!name)
341                         return -ENOMEM;
342                 snprintf(name, 5, "io%d", i);
343                 p->pads[i].number = i;
344                 p->pads[i].name = name;
345         }
346         p->desc->pins = p->pads;
347         p->desc->npins = p->max_pins;
348
349         return 0;
350 }
351
352 static int rt2880_pinmux_probe(struct platform_device *pdev)
353 {
354         struct rt2880_priv *p;
355         struct pinctrl_dev *dev;
356         struct device_node *np;
357
358         if (!rt2880_pinmux_data)
359                 return -ENOTSUPP;
360
361         /* setup the private data */
362         p = devm_kzalloc(&pdev->dev, sizeof(struct rt2880_priv), GFP_KERNEL);
363         if (!p)
364                 return -ENOMEM;
365
366         p->dev = &pdev->dev;
367         p->desc = &rt2880_pctrl_desc;
368         p->groups = rt2880_pinmux_data;
369         platform_set_drvdata(pdev, p);
370
371         /* init the device */
372         if (rt2880_pinmux_index(p)) {
373                 dev_err(&pdev->dev, "failed to load index\n");
374                 return -EINVAL;
375         }
376         if (rt2880_pinmux_pins(p)) {
377                 dev_err(&pdev->dev, "failed to load pins\n");
378                 return -EINVAL;
379         }
380         dev = pinctrl_register(p->desc, &pdev->dev, p);
381         if (IS_ERR(dev))
382                 return PTR_ERR(dev);
383
384         /* finalize by adding gpio ranges for enables gpio controllers */
385         for_each_compatible_node(np, NULL, "ralink,rt2880-gpio") {
386                 const __be32 *ngpio, *gpiobase;
387                 struct pinctrl_gpio_range *range;
388                 char *name;
389
390                 if (!of_device_is_available(np))
391                         continue;
392
393                 ngpio = of_get_property(np, "ralink,num-gpios", NULL);
394                 gpiobase = of_get_property(np, "ralink,gpio-base", NULL);
395                 if (!ngpio || !gpiobase) {
396                         dev_err(&pdev->dev, "failed to load chip info\n");
397                         return -EINVAL;
398                 }
399
400                 range = devm_kzalloc(p->dev, sizeof(*range) + 4, GFP_KERNEL);
401                 range->name = name = (char *) &range[1];
402                 sprintf(name, "pio");
403                 range->npins = __be32_to_cpu(*ngpio);
404                 range->base = __be32_to_cpu(*gpiobase);
405                 range->pin_base = range->base;
406                 pinctrl_add_gpio_range(dev, range);
407         }
408
409         return 0;
410 }
411
412 static const struct of_device_id rt2880_pinmux_match[] = {
413         { .compatible = "ralink,rt2880-pinmux" },
414         {},
415 };
416 MODULE_DEVICE_TABLE(of, rt2880_pinmux_match);
417
418 static struct platform_driver rt2880_pinmux_driver = {
419         .probe = rt2880_pinmux_probe,
420         .driver = {
421                 .name = "rt2880-pinmux",
422                 .of_match_table = rt2880_pinmux_match,
423         },
424 };
425
426 int __init rt2880_pinmux_init(void)
427 {
428         return platform_driver_register(&rt2880_pinmux_driver);
429 }
430
431 core_initcall_sync(rt2880_pinmux_init);