2 * linux/drivers/pinctrl/pinctrl-rt2880.c
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.
8 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
11 #include <linux/module.h>
12 #include <linux/device.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.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>
23 #include <asm/mach-ralink/ralink_regs.h>
24 #include <asm/mach-ralink/pinmux.h>
25 #include <asm/mach-ralink/mt7620.h>
29 #define SYSC_REG_GPIO_MODE 0x60
30 #define SYSC_REG_GPIO_MODE2 0x64
35 struct pinctrl_pin_desc *pads;
36 struct pinctrl_desc *desc;
38 struct rt2880_pmx_func **func;
41 struct rt2880_pmx_group *groups;
42 const char **group_names;
49 static int rt2880_get_group_count(struct pinctrl_dev *pctrldev)
51 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
53 return p->group_count;
56 static const char *rt2880_get_group_name(struct pinctrl_dev *pctrldev,
59 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
61 if (group >= p->group_count)
64 return p->group_names[group];
67 static int rt2880_get_group_pins(struct pinctrl_dev *pctrldev,
69 const unsigned **pins,
72 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
74 if (group >= p->group_count)
77 *pins = p->groups[group].func[0].pins;
78 *num_pins = p->groups[group].func[0].pin_count;
83 static void rt2880_pinctrl_dt_free_map(struct pinctrl_dev *pctrldev,
84 struct pinctrl_map *map, unsigned num_maps)
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);
95 static void rt2880_pinctrl_pin_dbg_show(struct pinctrl_dev *pctrldev,
99 seq_printf(s, "ralink pio");
102 static void rt2880_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctrldev,
103 struct device_node *np,
104 struct pinctrl_map **map)
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");
114 for (i = 0; i < grps; i++) {
117 of_property_read_string_index(np, "ralink,group", i, &group);
119 (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
120 (*map)->name = function;
121 (*map)->data.mux.group = group;
122 (*map)->data.mux.function = function;
127 static int rt2880_pinctrl_dt_node_to_map(struct pinctrl_dev *pctrldev,
128 struct device_node *np_config,
129 struct pinctrl_map **map,
133 struct pinctrl_map *tmp;
134 struct device_node *np;
136 for_each_child_of_node(np_config, np) {
137 int ret = of_property_count_strings(np, "ralink,group");
146 *map = kcalloc(max_maps, sizeof(struct pinctrl_map), GFP_KERNEL);
152 for_each_child_of_node(np_config, np)
153 rt2880_pinctrl_dt_subnode_to_map(pctrldev, np, &tmp);
154 *num_maps = max_maps;
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,
168 static int rt2880_pmx_func_count(struct pinctrl_dev *pctrldev)
170 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
172 return p->func_count;
175 static const char *rt2880_pmx_func_name(struct pinctrl_dev *pctrldev,
178 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
180 return p->func[func]->name;
183 static int rt2880_pmx_group_get_groups(struct pinctrl_dev *pctrldev,
185 const char * const **groups,
186 unsigned * const num_groups)
188 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
190 if (p->func[func]->group_count == 1)
191 *groups = &p->group_names[p->func[func]->groups[0]];
193 *groups = p->group_names;
195 *num_groups = p->func[func]->group_count;
200 static int rt2880_pmx_group_enable(struct pinctrl_dev *pctrldev,
204 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
206 u32 reg = SYSC_REG_GPIO_MODE;
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);
216 p->groups[group].enabled = 1;
217 p->func[func]->enabled = 1;
219 shift = p->groups[group].shift;
222 reg = SYSC_REG_GPIO_MODE2;
224 mode = rt_sysc_r32(reg);
225 mode &= ~(p->groups[group].mask << shift);
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;
231 /* function 0 is gpio and needs special handling */
233 mode |= p->groups[group].gpio << shift;
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;
239 rt_sysc_w32(mode, reg);
244 static int rt2880_pmx_group_gpio_request_enable(struct pinctrl_dev *pctrldev,
245 struct pinctrl_gpio_range *range,
248 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
251 dev_err(p->dev, "pin %d is not set to gpio mux\n", pin);
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,
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,
273 static struct rt2880_pmx_func gpio_func = {
277 static int rt2880_pinmux_index(struct rt2880_priv *p)
279 struct rt2880_pmx_func **f;
280 struct rt2880_pmx_group *mux = p->groups;
283 /* count the mux functions */
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);
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;
299 /* we have a dummy function[0] for gpio */
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)
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;
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);
322 f[c]->group_count = 1;
329 static int rt2880_pinmux_pins(struct rt2880_priv *p)
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++) {
337 if (!p->func[i]->pin_count)
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;
344 pin = p->func[i]->pin_first + p->func[i]->pin_count;
345 if (pin > p->max_pins)
349 /* the buffer that tells us which pins are gpio */
350 p->gpio = devm_kzalloc(p->dev,sizeof(uint8_t) * p->max_pins,
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,
356 if (!p->pads || !p->gpio ) {
357 dev_err(p->dev, "Failed to allocate gpio data\n");
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)
366 for (j = 0; j < p->func[i]->pin_count; j++)
367 p->gpio[p->func[i]->pins[j]] = 0;
370 /* pin 0 is always a gpio */
374 for (i = 0; i < p->max_pins; i++) {
375 /* strlen("ioXY") + 1 = 5 */
376 char *name = devm_kzalloc(p->dev, 5, GFP_KERNEL);
379 dev_err(p->dev, "Failed to allocate pad name\n");
382 snprintf(name, 5, "io%d", i);
383 p->pads[i].number = i;
384 p->pads[i].name = name;
386 p->desc->pins = p->pads;
387 p->desc->npins = p->max_pins;
392 static int rt2880_pinmux_probe(struct platform_device *pdev)
394 struct rt2880_priv *p;
395 struct pinctrl_dev *dev;
396 struct device_node *np;
398 if (!rt2880_pinmux_data)
401 /* setup the private data */
402 p = devm_kzalloc(&pdev->dev, sizeof(struct rt2880_priv), GFP_KERNEL);
407 p->desc = &rt2880_pctrl_desc;
408 p->groups = rt2880_pinmux_data;
409 platform_set_drvdata(pdev, p);
411 /* init the device */
412 if (rt2880_pinmux_index(p)) {
413 dev_err(&pdev->dev, "failed to load index\n");
416 if (rt2880_pinmux_pins(p)) {
417 dev_err(&pdev->dev, "failed to load pins\n");
420 dev = pinctrl_register(p->desc, &pdev->dev, p);
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;
430 if (!of_device_is_available(np))
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");
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);
452 static const struct of_device_id rt2880_pinmux_match[] = {
453 { .compatible = "ralink,rt2880-pinmux" },
456 MODULE_DEVICE_TABLE(of, rt2880_pinmux_match);
458 static struct platform_driver rt2880_pinmux_driver = {
459 .probe = rt2880_pinmux_probe,
461 .name = "rt2880-pinmux",
462 .of_match_table = rt2880_pinmux_match,
466 int __init rt2880_pinmux_init(void)
468 return platform_driver_register(&rt2880_pinmux_driver);
471 core_initcall_sync(rt2880_pinmux_init);