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_kcalloc(p
->dev
, p
->group_count
, sizeof(char *),
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
;
300 /* we have a dummy function[0] for gpio */
303 /* allocate our function and group mapping index buffers */
304 f
= p
->func
= devm_kcalloc(p
->dev
,
306 sizeof(struct rt2880_pmx_func
),
308 gpio_func
.groups
= devm_kcalloc(p
->dev
, p
->group_count
, sizeof(int),
310 if (!f
|| !gpio_func
.groups
)
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
;
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
);
327 f
[c
]->group_count
= 1;
334 static int rt2880_pinmux_pins(struct rt2880_priv
*p
)
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
++) {
342 if (!p
->func
[i
]->pin_count
)
345 p
->func
[i
]->pins
= devm_kcalloc(p
->dev
,
346 p
->func
[i
]->pin_count
,
349 for (j
= 0; j
< p
->func
[i
]->pin_count
; j
++)
350 p
->func
[i
]->pins
[j
] = p
->func
[i
]->pin_first
+ j
;
352 pin
= p
->func
[i
]->pin_first
+ p
->func
[i
]->pin_count
;
353 if (pin
> p
->max_pins
)
357 /* the buffer that tells us which pins are gpio */
358 p
->gpio
= devm_kcalloc(p
->dev
,p
->max_pins
, sizeof(uint8_t),
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
),
364 if (!p
->pads
|| !p
->gpio
) {
365 dev_err(p
->dev
, "Failed to allocate gpio data\n");
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
)
374 for (j
= 0; j
< p
->func
[i
]->pin_count
; j
++)
375 p
->gpio
[p
->func
[i
]->pins
[j
]] = 0;
378 /* pin 0 is always a gpio */
382 for (i
= 0; i
< p
->max_pins
; i
++) {
383 /* strlen("ioXY") + 1 = 5 */
384 char *name
= devm_kzalloc(p
->dev
, 5, GFP_KERNEL
);
387 dev_err(p
->dev
, "Failed to allocate pad name\n");
390 snprintf(name
, 5, "io%d", i
);
391 p
->pads
[i
].number
= i
;
392 p
->pads
[i
].name
= name
;
394 p
->desc
->pins
= p
->pads
;
395 p
->desc
->npins
= p
->max_pins
;
400 static int rt2880_pinmux_probe(struct platform_device
*pdev
)
402 struct rt2880_priv
*p
;
403 struct pinctrl_dev
*dev
;
404 struct device_node
*np
;
406 if (!rt2880_pinmux_data
)
409 /* setup the private data */
410 p
= devm_kzalloc(&pdev
->dev
, sizeof(struct rt2880_priv
), GFP_KERNEL
);
415 p
->desc
= &rt2880_pctrl_desc
;
416 p
->groups
= rt2880_pinmux_data
;
417 platform_set_drvdata(pdev
, p
);
419 /* init the device */
420 if (rt2880_pinmux_index(p
)) {
421 dev_err(&pdev
->dev
, "failed to load index\n");
424 if (rt2880_pinmux_pins(p
)) {
425 dev_err(&pdev
->dev
, "failed to load pins\n");
428 dev
= pinctrl_register(p
->desc
, &pdev
->dev
, p
);
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
;
438 if (!of_device_is_available(np
))
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");
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
);
460 static const struct of_device_id rt2880_pinmux_match
[] = {
461 { .compatible
= "ralink,rt2880-pinmux" },
464 MODULE_DEVICE_TABLE(of
, rt2880_pinmux_match
);
466 static struct platform_driver rt2880_pinmux_driver
= {
467 .probe
= rt2880_pinmux_probe
,
469 .name
= "rt2880-pinmux",
470 .of_match_table
= rt2880_pinmux_match
,
474 int __init
rt2880_pinmux_init(void)
476 return platform_driver_register(&rt2880_pinmux_driver
);
479 core_initcall_sync(rt2880_pinmux_init
);