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