1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
6 #include <linux/module.h>
7 #include <linux/device.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
12 #include <linux/pinctrl/pinctrl.h>
13 #include <linux/pinctrl/pinconf.h>
14 #include <linux/pinctrl/pinmux.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/pinctrl/machine.h>
18 #include <asm/mach-ralink/ralink_regs.h>
19 #include <asm/mach-ralink/pinmux.h>
20 #include <asm/mach-ralink/mt7620.h>
23 #include "pinctrl-utils.h"
25 #define SYSC_REG_GPIO_MODE 0x60
26 #define SYSC_REG_GPIO_MODE2 0x64
31 struct pinctrl_pin_desc *pads;
32 struct pinctrl_desc *desc;
34 struct rt2880_pmx_func **func;
37 struct rt2880_pmx_group *groups;
38 const char **group_names;
45 static int rt2880_get_group_count(struct pinctrl_dev *pctrldev)
47 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
49 return p->group_count;
52 static const char *rt2880_get_group_name(struct pinctrl_dev *pctrldev,
55 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
57 return (group >= p->group_count) ? NULL : p->group_names[group];
60 static int rt2880_get_group_pins(struct pinctrl_dev *pctrldev,
62 const unsigned int **pins,
63 unsigned int *num_pins)
65 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
67 if (group >= p->group_count)
70 *pins = p->groups[group].func[0].pins;
71 *num_pins = p->groups[group].func[0].pin_count;
76 static int rt2880_pinctrl_dt_node_to_map(struct pinctrl_dev *pctrldev,
77 struct device_node *np_config,
78 struct pinctrl_map **map,
79 unsigned int *num_maps)
81 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
82 struct property *prop;
83 const char *function_name, *group_name;
86 unsigned int reserved_maps = 0;
88 for_each_node_with_property(np_config, "group")
92 ret = pinctrl_utils_reserve_map(pctrldev, map, &reserved_maps,
95 dev_err(p->dev, "can't reserve map: %d\n", ret);
99 of_property_for_each_string(np_config, "group", prop, group_name) {
100 ret = pinctrl_utils_add_map_mux(pctrldev, map, &reserved_maps,
101 num_maps, group_name,
104 dev_err(p->dev, "can't add map: %d\n", ret);
112 static const struct pinctrl_ops rt2880_pctrl_ops = {
113 .get_groups_count = rt2880_get_group_count,
114 .get_group_name = rt2880_get_group_name,
115 .get_group_pins = rt2880_get_group_pins,
116 .dt_node_to_map = rt2880_pinctrl_dt_node_to_map,
117 .dt_free_map = pinctrl_utils_free_map,
120 static int rt2880_pmx_func_count(struct pinctrl_dev *pctrldev)
122 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
124 return p->func_count;
127 static const char *rt2880_pmx_func_name(struct pinctrl_dev *pctrldev,
130 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
132 return p->func[func]->name;
135 static int rt2880_pmx_group_get_groups(struct pinctrl_dev *pctrldev,
137 const char * const **groups,
138 unsigned int * const num_groups)
140 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
142 if (p->func[func]->group_count == 1)
143 *groups = &p->group_names[p->func[func]->groups[0]];
145 *groups = p->group_names;
147 *num_groups = p->func[func]->group_count;
152 static int rt2880_pmx_group_enable(struct pinctrl_dev *pctrldev,
153 unsigned int func, unsigned int group)
155 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
157 u32 reg = SYSC_REG_GPIO_MODE;
161 /* dont allow double use */
162 if (p->groups[group].enabled) {
163 dev_err(p->dev, "%s is already enabled\n",
164 p->groups[group].name);
168 p->groups[group].enabled = 1;
169 p->func[func]->enabled = 1;
171 shift = p->groups[group].shift;
174 reg = SYSC_REG_GPIO_MODE2;
176 mode = rt_sysc_r32(reg);
177 mode &= ~(p->groups[group].mask << shift);
179 /* mark the pins as gpio */
180 for (i = 0; i < p->groups[group].func[0].pin_count; i++)
181 p->gpio[p->groups[group].func[0].pins[i]] = 1;
183 /* function 0 is gpio and needs special handling */
185 mode |= p->groups[group].gpio << shift;
187 for (i = 0; i < p->func[func]->pin_count; i++)
188 p->gpio[p->func[func]->pins[i]] = 0;
189 mode |= p->func[func]->value << shift;
191 rt_sysc_w32(mode, reg);
196 static int rt2880_pmx_group_gpio_request_enable(struct pinctrl_dev *pctrldev,
197 struct pinctrl_gpio_range *range,
200 struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
203 dev_err(p->dev, "pin %d is not set to gpio mux\n", pin);
210 static const struct pinmux_ops rt2880_pmx_group_ops = {
211 .get_functions_count = rt2880_pmx_func_count,
212 .get_function_name = rt2880_pmx_func_name,
213 .get_function_groups = rt2880_pmx_group_get_groups,
214 .set_mux = rt2880_pmx_group_enable,
215 .gpio_request_enable = rt2880_pmx_group_gpio_request_enable,
218 static struct pinctrl_desc rt2880_pctrl_desc = {
219 .owner = THIS_MODULE,
220 .name = "rt2880-pinmux",
221 .pctlops = &rt2880_pctrl_ops,
222 .pmxops = &rt2880_pmx_group_ops,
225 static struct rt2880_pmx_func gpio_func = {
229 static int rt2880_pinmux_index(struct rt2880_priv *p)
231 struct rt2880_pmx_func **f;
232 struct rt2880_pmx_group *mux = p->groups;
235 /* count the mux functions */
241 /* allocate the group names array needed by the gpio function */
242 p->group_names = devm_kcalloc(p->dev, p->group_count,
243 sizeof(char *), GFP_KERNEL);
247 for (i = 0; i < p->group_count; i++) {
248 p->group_names[i] = p->groups[i].name;
249 p->func_count += p->groups[i].func_count;
252 /* we have a dummy function[0] for gpio */
255 /* allocate our function and group mapping index buffers */
256 f = p->func = devm_kcalloc(p->dev,
258 sizeof(struct rt2880_pmx_func),
260 gpio_func.groups = devm_kcalloc(p->dev, p->group_count, sizeof(int),
262 if (!f || !gpio_func.groups)
265 /* add a backpointer to the function so it knows its group */
266 gpio_func.group_count = p->group_count;
267 for (i = 0; i < gpio_func.group_count; i++)
268 gpio_func.groups[i] = i;
273 /* add remaining functions */
274 for (i = 0; i < p->group_count; i++) {
275 for (j = 0; j < p->groups[i].func_count; j++) {
276 f[c] = &p->groups[i].func[j];
277 f[c]->groups = devm_kzalloc(p->dev, sizeof(int),
280 f[c]->group_count = 1;
287 static int rt2880_pinmux_pins(struct rt2880_priv *p)
292 * loop over the functions and initialize the pins array.
293 * also work out the highest pin used.
295 for (i = 0; i < p->func_count; i++) {
298 if (!p->func[i]->pin_count)
301 p->func[i]->pins = devm_kcalloc(p->dev,
302 p->func[i]->pin_count,
305 for (j = 0; j < p->func[i]->pin_count; j++)
306 p->func[i]->pins[j] = p->func[i]->pin_first + j;
308 pin = p->func[i]->pin_first + p->func[i]->pin_count;
309 if (pin > p->max_pins)
313 /* the buffer that tells us which pins are gpio */
314 p->gpio = devm_kcalloc(p->dev, p->max_pins, sizeof(u8), GFP_KERNEL);
315 /* the pads needed to tell pinctrl about our pins */
316 p->pads = devm_kcalloc(p->dev, p->max_pins,
317 sizeof(struct pinctrl_pin_desc), GFP_KERNEL);
318 if (!p->pads || !p->gpio) {
319 dev_err(p->dev, "Failed to allocate gpio data\n");
323 memset(p->gpio, 1, sizeof(u8) * p->max_pins);
324 for (i = 0; i < p->func_count; i++) {
325 if (!p->func[i]->pin_count)
328 for (j = 0; j < p->func[i]->pin_count; j++)
329 p->gpio[p->func[i]->pins[j]] = 0;
332 /* pin 0 is always a gpio */
336 for (i = 0; i < p->max_pins; i++) {
337 /* strlen("ioXY") + 1 = 5 */
338 char *name = devm_kzalloc(p->dev, 5, GFP_KERNEL);
342 snprintf(name, 5, "io%d", i);
343 p->pads[i].number = i;
344 p->pads[i].name = name;
346 p->desc->pins = p->pads;
347 p->desc->npins = p->max_pins;
352 static int rt2880_pinmux_probe(struct platform_device *pdev)
354 struct rt2880_priv *p;
355 struct pinctrl_dev *dev;
356 struct device_node *np;
358 if (!rt2880_pinmux_data)
361 /* setup the private data */
362 p = devm_kzalloc(&pdev->dev, sizeof(struct rt2880_priv), GFP_KERNEL);
367 p->desc = &rt2880_pctrl_desc;
368 p->groups = rt2880_pinmux_data;
369 platform_set_drvdata(pdev, p);
371 /* init the device */
372 if (rt2880_pinmux_index(p)) {
373 dev_err(&pdev->dev, "failed to load index\n");
376 if (rt2880_pinmux_pins(p)) {
377 dev_err(&pdev->dev, "failed to load pins\n");
380 dev = pinctrl_register(p->desc, &pdev->dev, p);
384 /* finalize by adding gpio ranges for enables gpio controllers */
385 for_each_compatible_node(np, NULL, "ralink,rt2880-gpio") {
386 const __be32 *ngpio, *gpiobase;
387 struct pinctrl_gpio_range *range;
390 if (!of_device_is_available(np))
393 ngpio = of_get_property(np, "ralink,num-gpios", NULL);
394 gpiobase = of_get_property(np, "ralink,gpio-base", NULL);
395 if (!ngpio || !gpiobase) {
396 dev_err(&pdev->dev, "failed to load chip info\n");
400 range = devm_kzalloc(p->dev, sizeof(*range) + 4, GFP_KERNEL);
401 range->name = name = (char *) &range[1];
402 sprintf(name, "pio");
403 range->npins = __be32_to_cpu(*ngpio);
404 range->base = __be32_to_cpu(*gpiobase);
405 range->pin_base = range->base;
406 pinctrl_add_gpio_range(dev, range);
412 static const struct of_device_id rt2880_pinmux_match[] = {
413 { .compatible = "ralink,rt2880-pinmux" },
416 MODULE_DEVICE_TABLE(of, rt2880_pinmux_match);
418 static struct platform_driver rt2880_pinmux_driver = {
419 .probe = rt2880_pinmux_probe,
421 .name = "rt2880-pinmux",
422 .of_match_table = rt2880_pinmux_match,
426 int __init rt2880_pinmux_init(void)
428 return platform_driver_register(&rt2880_pinmux_driver);
431 core_initcall_sync(rt2880_pinmux_init);