]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - drivers/staging/mt7621-pinctrl/pinctrl-rt2880.c
treewide: devm_kzalloc() -> devm_kcalloc()
[mirror_ubuntu-focal-kernel.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_kcalloc(p->dev, p->group_count, sizeof(char *),
291 GFP_KERNEL);
292 if (!p->group_names)
293 return -1;
294
295 for (i = 0; i < p->group_count; i++) {
296 p->group_names[i] = p->groups[i].name;
297 p->func_count += p->groups[i].func_count;
298 }
299
300 /* we have a dummy function[0] for gpio */
301 p->func_count++;
302
303 /* allocate our function and group mapping index buffers */
304 f = p->func = devm_kcalloc(p->dev,
305 p->func_count,
306 sizeof(struct rt2880_pmx_func),
307 GFP_KERNEL);
308 gpio_func.groups = devm_kcalloc(p->dev, p->group_count, sizeof(int),
309 GFP_KERNEL);
310 if (!f || !gpio_func.groups)
311 return -1;
312
313 /* add a backpointer to the function so it knows its group */
314 gpio_func.group_count = p->group_count;
315 for (i = 0; i < gpio_func.group_count; i++)
316 gpio_func.groups[i] = i;
317
318 f[c] = &gpio_func;
319 c++;
320
321 /* add remaining functions */
322 for (i = 0; i < p->group_count; i++) {
323 for (j = 0; j < p->groups[i].func_count; j++) {
324 f[c] = &p->groups[i].func[j];
325 f[c]->groups = devm_kzalloc(p->dev, sizeof(int), GFP_KERNEL);
326 f[c]->groups[0] = i;
327 f[c]->group_count = 1;
328 c++;
329 }
330 }
331 return 0;
332 }
333
334 static int rt2880_pinmux_pins(struct rt2880_priv *p)
335 {
336 int i, j;
337
338 /* loop over the functions and initialize the pins array. also work out the highest pin used */
339 for (i = 0; i < p->func_count; i++) {
340 int pin;
341
342 if (!p->func[i]->pin_count)
343 continue;
344
345 p->func[i]->pins = devm_kcalloc(p->dev,
346 p->func[i]->pin_count,
347 sizeof(int),
348 GFP_KERNEL);
349 for (j = 0; j < p->func[i]->pin_count; j++)
350 p->func[i]->pins[j] = p->func[i]->pin_first + j;
351
352 pin = p->func[i]->pin_first + p->func[i]->pin_count;
353 if (pin > p->max_pins)
354 p->max_pins = pin;
355 }
356
357 /* the buffer that tells us which pins are gpio */
358 p->gpio = devm_kcalloc(p->dev,p->max_pins, sizeof(uint8_t),
359 GFP_KERNEL);
360 /* the pads needed to tell pinctrl about our pins */
361 p->pads = devm_kcalloc(p->dev,
362 p->max_pins, sizeof(struct pinctrl_pin_desc),
363 GFP_KERNEL);
364 if (!p->pads || !p->gpio ) {
365 dev_err(p->dev, "Failed to allocate gpio data\n");
366 return -ENOMEM;
367 }
368
369 memset(p->gpio, 1, sizeof(uint8_t) * p->max_pins);
370 for (i = 0; i < p->func_count; i++) {
371 if (!p->func[i]->pin_count)
372 continue;
373
374 for (j = 0; j < p->func[i]->pin_count; j++)
375 p->gpio[p->func[i]->pins[j]] = 0;
376 }
377
378 /* pin 0 is always a gpio */
379 p->gpio[0] = 1;
380
381 /* set the pads */
382 for (i = 0; i < p->max_pins; i++) {
383 /* strlen("ioXY") + 1 = 5 */
384 char *name = devm_kzalloc(p->dev, 5, GFP_KERNEL);
385
386 if (!name) {
387 dev_err(p->dev, "Failed to allocate pad name\n");
388 return -ENOMEM;
389 }
390 snprintf(name, 5, "io%d", i);
391 p->pads[i].number = i;
392 p->pads[i].name = name;
393 }
394 p->desc->pins = p->pads;
395 p->desc->npins = p->max_pins;
396
397 return 0;
398 }
399
400 static int rt2880_pinmux_probe(struct platform_device *pdev)
401 {
402 struct rt2880_priv *p;
403 struct pinctrl_dev *dev;
404 struct device_node *np;
405
406 if (!rt2880_pinmux_data)
407 return -ENOSYS;
408
409 /* setup the private data */
410 p = devm_kzalloc(&pdev->dev, sizeof(struct rt2880_priv), GFP_KERNEL);
411 if (!p)
412 return -ENOMEM;
413
414 p->dev = &pdev->dev;
415 p->desc = &rt2880_pctrl_desc;
416 p->groups = rt2880_pinmux_data;
417 platform_set_drvdata(pdev, p);
418
419 /* init the device */
420 if (rt2880_pinmux_index(p)) {
421 dev_err(&pdev->dev, "failed to load index\n");
422 return -EINVAL;
423 }
424 if (rt2880_pinmux_pins(p)) {
425 dev_err(&pdev->dev, "failed to load pins\n");
426 return -EINVAL;
427 }
428 dev = pinctrl_register(p->desc, &pdev->dev, p);
429 if (IS_ERR(dev))
430 return PTR_ERR(dev);
431
432 /* finalize by adding gpio ranges for enables gpio controllers */
433 for_each_compatible_node(np, NULL, "ralink,rt2880-gpio") {
434 const __be32 *ngpio, *gpiobase;
435 struct pinctrl_gpio_range *range;
436 char *name;
437
438 if (!of_device_is_available(np))
439 continue;
440
441 ngpio = of_get_property(np, "ralink,num-gpios", NULL);
442 gpiobase = of_get_property(np, "ralink,gpio-base", NULL);
443 if (!ngpio || !gpiobase) {
444 dev_err(&pdev->dev, "failed to load chip info\n");
445 return -EINVAL;
446 }
447
448 range = devm_kzalloc(p->dev, sizeof(struct pinctrl_gpio_range) + 4, GFP_KERNEL);
449 range->name = name = (char *) &range[1];
450 sprintf(name, "pio");
451 range->npins = __be32_to_cpu(*ngpio);
452 range->base = __be32_to_cpu(*gpiobase);
453 range->pin_base = range->base;
454 pinctrl_add_gpio_range(dev, range);
455 }
456
457 return 0;
458 }
459
460 static const struct of_device_id rt2880_pinmux_match[] = {
461 { .compatible = "ralink,rt2880-pinmux" },
462 {},
463 };
464 MODULE_DEVICE_TABLE(of, rt2880_pinmux_match);
465
466 static struct platform_driver rt2880_pinmux_driver = {
467 .probe = rt2880_pinmux_probe,
468 .driver = {
469 .name = "rt2880-pinmux",
470 .of_match_table = rt2880_pinmux_match,
471 },
472 };
473
474 int __init rt2880_pinmux_init(void)
475 {
476 return platform_driver_register(&rt2880_pinmux_driver);
477 }
478
479 core_initcall_sync(rt2880_pinmux_init);