1 From 7adbe9a88c33c6e362a10b109d963b5500a21f00 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 27 Jul 2014 09:34:05 +0100
4 Subject: [PATCH 25/53] pinctrl: ralink: add pinctrl driver
6 Signed-off-by: John Crispin <blogic@openwrt.org>
8 arch/mips/Kconfig | 2 +
9 drivers/pinctrl/Kconfig | 5 +
10 drivers/pinctrl/Makefile | 1 +
11 drivers/pinctrl/pinctrl-rt2880.c | 474 ++++++++++++++++++++++++++++++++++++++
12 4 files changed, 482 insertions(+)
13 create mode 100644 drivers/pinctrl/pinctrl-rt2880.c
15 diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
16 index e3aa5b0..0098bff 100644
17 --- a/arch/mips/Kconfig
18 +++ b/arch/mips/Kconfig
19 @@ -557,6 +557,8 @@ config RALINK
21 select ARCH_HAS_RESET_CONTROLLER
22 select RESET_CONTROLLER
24 + select PINCTRL_RT2880
27 bool "SGI IP22 (Indy/Indigo2)"
28 diff --git a/drivers/pinctrl/Kconfig b/drivers/pinctrl/Kconfig
29 index 84dd2ed..d016935 100644
30 --- a/drivers/pinctrl/Kconfig
31 +++ b/drivers/pinctrl/Kconfig
32 @@ -103,6 +103,11 @@ config PINCTRL_LPC18XX
34 Pinctrl driver for NXP LPC18xx/43xx System Control Unit (SCU).
36 +config PINCTRL_RT2880
44 diff --git a/drivers/pinctrl/Makefile b/drivers/pinctrl/Makefile
45 index cad077c..0c86632 100644
46 --- a/drivers/pinctrl/Makefile
47 +++ b/drivers/pinctrl/Makefile
48 @@ -19,6 +19,7 @@ obj-$(CONFIG_PINCTRL_MESON) += meson/
49 obj-$(CONFIG_PINCTRL_PALMAS) += pinctrl-palmas.o
50 obj-$(CONFIG_PINCTRL_PISTACHIO) += pinctrl-pistachio.o
51 obj-$(CONFIG_PINCTRL_ROCKCHIP) += pinctrl-rockchip.o
52 +obj-$(CONFIG_PINCTRL_RT2880) += pinctrl-rt2880.o
53 obj-$(CONFIG_PINCTRL_SINGLE) += pinctrl-single.o
54 obj-$(CONFIG_PINCTRL_SIRF) += sirf/
55 obj-$(CONFIG_PINCTRL_TEGRA) += pinctrl-tegra.o
56 diff --git a/drivers/pinctrl/pinctrl-rt2880.c b/drivers/pinctrl/pinctrl-rt2880.c
58 index 0000000..fe0af77
60 +++ b/drivers/pinctrl/pinctrl-rt2880.c
63 + * linux/drivers/pinctrl/pinctrl-rt2880.c
65 + * This program is free software; you can redistribute it and/or modify
66 + * it under the terms of the GNU General Public License version 2 as
67 + * publishhed by the Free Software Foundation.
69 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
72 +#include <linux/module.h>
73 +#include <linux/device.h>
74 +#include <linux/io.h>
75 +#include <linux/platform_device.h>
76 +#include <linux/slab.h>
77 +#include <linux/of.h>
78 +#include <linux/pinctrl/pinctrl.h>
79 +#include <linux/pinctrl/pinconf.h>
80 +#include <linux/pinctrl/pinmux.h>
81 +#include <linux/pinctrl/consumer.h>
82 +#include <linux/pinctrl/machine.h>
84 +#include <asm/mach-ralink/ralink_regs.h>
85 +#include <asm/mach-ralink/pinmux.h>
86 +#include <asm/mach-ralink/mt7620.h>
90 +#define SYSC_REG_GPIO_MODE 0x60
91 +#define SYSC_REG_GPIO_MODE2 0x64
96 + struct pinctrl_pin_desc *pads;
97 + struct pinctrl_desc *desc;
99 + struct rt2880_pmx_func **func;
102 + struct rt2880_pmx_group *groups;
103 + const char **group_names;
110 +struct rt2880_pmx_group *rt2880_pinmux_data = NULL;
112 +static int rt2880_get_group_count(struct pinctrl_dev *pctrldev)
114 + struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
116 + return p->group_count;
119 +static const char *rt2880_get_group_name(struct pinctrl_dev *pctrldev,
122 + struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
124 + if (group >= p->group_count)
127 + return p->group_names[group];
130 +static int rt2880_get_group_pins(struct pinctrl_dev *pctrldev,
132 + const unsigned **pins,
133 + unsigned *num_pins)
135 + struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
137 + if (group >= p->group_count)
140 + *pins = p->groups[group].func[0].pins;
141 + *num_pins = p->groups[group].func[0].pin_count;
146 +static void rt2880_pinctrl_dt_free_map(struct pinctrl_dev *pctrldev,
147 + struct pinctrl_map *map, unsigned num_maps)
151 + for (i = 0; i < num_maps; i++)
152 + if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN ||
153 + map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
154 + kfree(map[i].data.configs.configs);
158 +static void rt2880_pinctrl_pin_dbg_show(struct pinctrl_dev *pctrldev,
159 + struct seq_file *s,
162 + seq_printf(s, "ralink pio");
165 +static void rt2880_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctrldev,
166 + struct device_node *np,
167 + struct pinctrl_map **map)
169 + const char *function;
170 + int func = of_property_read_string(np, "ralink,function", &function);
171 + int grps = of_property_count_strings(np, "ralink,group");
177 + for (i = 0; i < grps; i++) {
180 + of_property_read_string_index(np, "ralink,group", i, &group);
182 + (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
183 + (*map)->name = function;
184 + (*map)->data.mux.group = group;
185 + (*map)->data.mux.function = function;
190 +static int rt2880_pinctrl_dt_node_to_map(struct pinctrl_dev *pctrldev,
191 + struct device_node *np_config,
192 + struct pinctrl_map **map,
193 + unsigned *num_maps)
196 + struct pinctrl_map *tmp;
197 + struct device_node *np;
199 + for_each_child_of_node(np_config, np) {
200 + int ret = of_property_count_strings(np, "ralink,group");
209 + *map = kzalloc(max_maps * sizeof(struct pinctrl_map), GFP_KERNEL);
215 + for_each_child_of_node(np_config, np)
216 + rt2880_pinctrl_dt_subnode_to_map(pctrldev, np, &tmp);
217 + *num_maps = max_maps;
222 +static const struct pinctrl_ops rt2880_pctrl_ops = {
223 + .get_groups_count = rt2880_get_group_count,
224 + .get_group_name = rt2880_get_group_name,
225 + .get_group_pins = rt2880_get_group_pins,
226 + .pin_dbg_show = rt2880_pinctrl_pin_dbg_show,
227 + .dt_node_to_map = rt2880_pinctrl_dt_node_to_map,
228 + .dt_free_map = rt2880_pinctrl_dt_free_map,
231 +static int rt2880_pmx_func_count(struct pinctrl_dev *pctrldev)
233 + struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
235 + return p->func_count;
238 +static const char *rt2880_pmx_func_name(struct pinctrl_dev *pctrldev,
241 + struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
243 + return p->func[func]->name;
246 +static int rt2880_pmx_group_get_groups(struct pinctrl_dev *pctrldev,
248 + const char * const **groups,
249 + unsigned * const num_groups)
251 + struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
253 + if (p->func[func]->group_count == 1)
254 + *groups = &p->group_names[p->func[func]->groups[0]];
256 + *groups = p->group_names;
258 + *num_groups = p->func[func]->group_count;
263 +static int rt2880_pmx_group_enable(struct pinctrl_dev *pctrldev,
267 + struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
269 + u32 reg = SYSC_REG_GPIO_MODE;
273 + /* dont allow double use */
274 + if (p->groups[group].enabled) {
275 + dev_err(p->dev, "%s is already enabled\n", p->groups[group].name);
279 + p->groups[group].enabled = 1;
280 + p->func[func]->enabled = 1;
282 + shift = p->groups[group].shift;
285 + reg = SYSC_REG_GPIO_MODE2;
287 + mode = rt_sysc_r32(reg);
288 + mode &= ~(p->groups[group].mask << shift);
290 + /* mark the pins as gpio */
291 + for (i = 0; i < p->groups[group].func[0].pin_count; i++)
292 + p->gpio[p->groups[group].func[0].pins[i]] = 1;
294 + /* function 0 is gpio and needs special handling */
296 + mode |= p->groups[group].gpio << shift;
298 + for (i = 0; i < p->func[func]->pin_count; i++)
299 + p->gpio[p->func[func]->pins[i]] = 0;
300 + mode |= p->func[func]->value << shift;
302 + rt_sysc_w32(mode, reg);
307 +static int rt2880_pmx_group_gpio_request_enable(struct pinctrl_dev *pctrldev,
308 + struct pinctrl_gpio_range *range,
311 + struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
313 + if (!p->gpio[pin]) {
314 + dev_err(p->dev, "pin %d is not set to gpio mux\n", pin);
321 +static const struct pinmux_ops rt2880_pmx_group_ops = {
322 + .get_functions_count = rt2880_pmx_func_count,
323 + .get_function_name = rt2880_pmx_func_name,
324 + .get_function_groups = rt2880_pmx_group_get_groups,
325 + .set_mux = rt2880_pmx_group_enable,
326 + .gpio_request_enable = rt2880_pmx_group_gpio_request_enable,
329 +static struct pinctrl_desc rt2880_pctrl_desc = {
330 + .owner = THIS_MODULE,
331 + .name = "rt2880-pinmux",
332 + .pctlops = &rt2880_pctrl_ops,
333 + .pmxops = &rt2880_pmx_group_ops,
336 +static struct rt2880_pmx_func gpio_func = {
340 +static int rt2880_pinmux_index(struct rt2880_priv *p)
342 + struct rt2880_pmx_func **f;
343 + struct rt2880_pmx_group *mux = p->groups;
346 + /* count the mux functions */
347 + while (mux->name) {
352 + /* allocate the group names array needed by the gpio function */
353 + p->group_names = devm_kzalloc(p->dev, sizeof(char *) * p->group_count, GFP_KERNEL);
354 + if (!p->group_names)
357 + for (i = 0; i < p->group_count; i++) {
358 + p->group_names[i] = p->groups[i].name;
359 + p->func_count += p->groups[i].func_count;
362 + /* we have a dummy function[0] for gpio */
365 + /* allocate our function and group mapping index buffers */
366 + f = p->func = devm_kzalloc(p->dev, sizeof(struct rt2880_pmx_func) * p->func_count, GFP_KERNEL);
367 + gpio_func.groups = devm_kzalloc(p->dev, sizeof(int) * p->group_count, GFP_KERNEL);
368 + if (!f || !gpio_func.groups)
371 + /* add a backpointer to the function so it knows its group */
372 + gpio_func.group_count = p->group_count;
373 + for (i = 0; i < gpio_func.group_count; i++)
374 + gpio_func.groups[i] = i;
379 + /* add remaining functions */
380 + for (i = 0; i < p->group_count; i++) {
381 + for (j = 0; j < p->groups[i].func_count; j++) {
382 + f[c] = &p->groups[i].func[j];
383 + f[c]->groups = devm_kzalloc(p->dev, sizeof(int), GFP_KERNEL);
384 + f[c]->groups[0] = i;
385 + f[c]->group_count = 1;
392 +static int rt2880_pinmux_pins(struct rt2880_priv *p)
396 + /* loop over the functions and initialize the pins array. also work out the highest pin used */
397 + for (i = 0; i < p->func_count; i++) {
400 + if (!p->func[i]->pin_count)
403 + p->func[i]->pins = devm_kzalloc(p->dev, sizeof(int) * p->func[i]->pin_count, GFP_KERNEL);
404 + for (j = 0; j < p->func[i]->pin_count; j++)
405 + p->func[i]->pins[j] = p->func[i]->pin_first + j;
407 + pin = p->func[i]->pin_first + p->func[i]->pin_count;
408 + if (pin > p->max_pins)
412 + /* the buffer that tells us which pins are gpio */
413 + p->gpio = devm_kzalloc(p->dev,sizeof(uint8_t) * p->max_pins,
415 + /* the pads needed to tell pinctrl about our pins */
416 + p->pads = devm_kzalloc(p->dev,
417 + sizeof(struct pinctrl_pin_desc) * p->max_pins,
419 + if (!p->pads || !p->gpio ) {
420 + dev_err(p->dev, "Failed to allocate gpio data\n");
424 + memset(p->gpio, 1, sizeof(uint8_t) * p->max_pins);
425 + for (i = 0; i < p->func_count; i++) {
426 + if (!p->func[i]->pin_count)
429 + for (j = 0; j < p->func[i]->pin_count; j++)
430 + p->gpio[p->func[i]->pins[j]] = 0;
433 + /* pin 0 is always a gpio */
437 + for (i = 0; i < p->max_pins; i++) {
438 + /* strlen("ioXY") + 1 = 5 */
439 + char *name = devm_kzalloc(p->dev, 5, GFP_KERNEL);
442 + dev_err(p->dev, "Failed to allocate pad name\n");
445 + snprintf(name, 5, "io%d", i);
446 + p->pads[i].number = i;
447 + p->pads[i].name = name;
449 + p->desc->pins = p->pads;
450 + p->desc->npins = p->max_pins;
455 +static int rt2880_pinmux_probe(struct platform_device *pdev)
457 + struct rt2880_priv *p;
458 + struct pinctrl_dev *dev;
459 + struct device_node *np;
461 + if (!rt2880_pinmux_data)
464 + /* setup the private data */
465 + p = devm_kzalloc(&pdev->dev, sizeof(struct rt2880_priv), GFP_KERNEL);
469 + p->dev = &pdev->dev;
470 + p->desc = &rt2880_pctrl_desc;
471 + p->groups = rt2880_pinmux_data;
472 + platform_set_drvdata(pdev, p);
474 + /* init the device */
475 + if (rt2880_pinmux_index(p)) {
476 + dev_err(&pdev->dev, "failed to load index\n");
479 + if (rt2880_pinmux_pins(p)) {
480 + dev_err(&pdev->dev, "failed to load pins\n");
483 + dev = pinctrl_register(p->desc, &pdev->dev, p);
485 + return PTR_ERR(dev);
487 + /* finalize by adding gpio ranges for enables gpio controllers */
488 + for_each_compatible_node(np, NULL, "ralink,rt2880-gpio") {
489 + const __be32 *ngpio, *gpiobase;
490 + struct pinctrl_gpio_range *range;
493 + if (!of_device_is_available(np))
496 + ngpio = of_get_property(np, "ralink,num-gpios", NULL);
497 + gpiobase = of_get_property(np, "ralink,gpio-base", NULL);
498 + if (!ngpio || !gpiobase) {
499 + dev_err(&pdev->dev, "failed to load chip info\n");
503 + range = devm_kzalloc(p->dev, sizeof(struct pinctrl_gpio_range) + 4, GFP_KERNEL);
504 + range->name = name = (char *) &range[1];
505 + sprintf(name, "pio");
506 + range->npins = __be32_to_cpu(*ngpio);
507 + range->base = __be32_to_cpu(*gpiobase);
508 + range->pin_base = range->base;
509 + pinctrl_add_gpio_range(dev, range);
515 +static const struct of_device_id rt2880_pinmux_match[] = {
516 + { .compatible = "ralink,rt2880-pinmux" },
519 +MODULE_DEVICE_TABLE(of, rt2880_pinmux_match);
521 +static struct platform_driver rt2880_pinmux_driver = {
522 + .probe = rt2880_pinmux_probe,
524 + .name = "rt2880-pinmux",
525 + .owner = THIS_MODULE,
526 + .of_match_table = rt2880_pinmux_match,
530 +int __init rt2880_pinmux_init(void)
532 + return platform_driver_register(&rt2880_pinmux_driver);
535 +core_initcall_sync(rt2880_pinmux_init);