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/pinconf-generic.h>
15 #include <linux/pinctrl/pinmux.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/pinctrl/machine.h>
19 #include <asm/mach-ralink/ralink_regs.h>
20 #include <asm/mach-ralink/pinmux.h>
21 #include <asm/mach-ralink/mt7620.h>
24 #include "pinctrl-utils.h"
26 #define SYSC_REG_GPIO_MODE 0x60
27 #define SYSC_REG_GPIO_MODE2 0x64
32 struct pinctrl_pin_desc
*pads
;
33 struct pinctrl_desc
*desc
;
35 struct rt2880_pmx_func
**func
;
38 struct rt2880_pmx_group
*groups
;
39 const char **group_names
;
46 static int rt2880_get_group_count(struct pinctrl_dev
*pctrldev
)
48 struct rt2880_priv
*p
= pinctrl_dev_get_drvdata(pctrldev
);
50 return p
->group_count
;
53 static const char *rt2880_get_group_name(struct pinctrl_dev
*pctrldev
,
56 struct rt2880_priv
*p
= pinctrl_dev_get_drvdata(pctrldev
);
58 return (group
>= p
->group_count
) ? NULL
: p
->group_names
[group
];
61 static int rt2880_get_group_pins(struct pinctrl_dev
*pctrldev
,
63 const unsigned int **pins
,
64 unsigned int *num_pins
)
66 struct rt2880_priv
*p
= pinctrl_dev_get_drvdata(pctrldev
);
68 if (group
>= p
->group_count
)
71 *pins
= p
->groups
[group
].func
[0].pins
;
72 *num_pins
= p
->groups
[group
].func
[0].pin_count
;
77 static const struct pinctrl_ops rt2880_pctrl_ops
= {
78 .get_groups_count
= rt2880_get_group_count
,
79 .get_group_name
= rt2880_get_group_name
,
80 .get_group_pins
= rt2880_get_group_pins
,
81 .dt_node_to_map
= pinconf_generic_dt_node_to_map_all
,
82 .dt_free_map
= pinconf_generic_dt_free_map
,
85 static int rt2880_pmx_func_count(struct pinctrl_dev
*pctrldev
)
87 struct rt2880_priv
*p
= pinctrl_dev_get_drvdata(pctrldev
);
92 static const char *rt2880_pmx_func_name(struct pinctrl_dev
*pctrldev
,
95 struct rt2880_priv
*p
= pinctrl_dev_get_drvdata(pctrldev
);
97 return p
->func
[func
]->name
;
100 static int rt2880_pmx_group_get_groups(struct pinctrl_dev
*pctrldev
,
102 const char * const **groups
,
103 unsigned int * const num_groups
)
105 struct rt2880_priv
*p
= pinctrl_dev_get_drvdata(pctrldev
);
107 if (p
->func
[func
]->group_count
== 1)
108 *groups
= &p
->group_names
[p
->func
[func
]->groups
[0]];
110 *groups
= p
->group_names
;
112 *num_groups
= p
->func
[func
]->group_count
;
117 static int rt2880_pmx_group_enable(struct pinctrl_dev
*pctrldev
,
118 unsigned int func
, unsigned int group
)
120 struct rt2880_priv
*p
= pinctrl_dev_get_drvdata(pctrldev
);
122 u32 reg
= SYSC_REG_GPIO_MODE
;
126 /* dont allow double use */
127 if (p
->groups
[group
].enabled
) {
128 dev_err(p
->dev
, "%s is already enabled\n",
129 p
->groups
[group
].name
);
133 p
->groups
[group
].enabled
= 1;
134 p
->func
[func
]->enabled
= 1;
136 shift
= p
->groups
[group
].shift
;
139 reg
= SYSC_REG_GPIO_MODE2
;
141 mode
= rt_sysc_r32(reg
);
142 mode
&= ~(p
->groups
[group
].mask
<< shift
);
144 /* mark the pins as gpio */
145 for (i
= 0; i
< p
->groups
[group
].func
[0].pin_count
; i
++)
146 p
->gpio
[p
->groups
[group
].func
[0].pins
[i
]] = 1;
148 /* function 0 is gpio and needs special handling */
150 mode
|= p
->groups
[group
].gpio
<< shift
;
152 for (i
= 0; i
< p
->func
[func
]->pin_count
; i
++)
153 p
->gpio
[p
->func
[func
]->pins
[i
]] = 0;
154 mode
|= p
->func
[func
]->value
<< shift
;
156 rt_sysc_w32(mode
, reg
);
161 static int rt2880_pmx_group_gpio_request_enable(struct pinctrl_dev
*pctrldev
,
162 struct pinctrl_gpio_range
*range
,
165 struct rt2880_priv
*p
= pinctrl_dev_get_drvdata(pctrldev
);
168 dev_err(p
->dev
, "pin %d is not set to gpio mux\n", pin
);
175 static const struct pinmux_ops rt2880_pmx_group_ops
= {
176 .get_functions_count
= rt2880_pmx_func_count
,
177 .get_function_name
= rt2880_pmx_func_name
,
178 .get_function_groups
= rt2880_pmx_group_get_groups
,
179 .set_mux
= rt2880_pmx_group_enable
,
180 .gpio_request_enable
= rt2880_pmx_group_gpio_request_enable
,
183 static struct pinctrl_desc rt2880_pctrl_desc
= {
184 .owner
= THIS_MODULE
,
185 .name
= "rt2880-pinmux",
186 .pctlops
= &rt2880_pctrl_ops
,
187 .pmxops
= &rt2880_pmx_group_ops
,
190 static struct rt2880_pmx_func gpio_func
= {
194 static int rt2880_pinmux_index(struct rt2880_priv
*p
)
196 struct rt2880_pmx_func
**f
;
197 struct rt2880_pmx_group
*mux
= p
->groups
;
200 /* count the mux functions */
206 /* allocate the group names array needed by the gpio function */
207 p
->group_names
= devm_kcalloc(p
->dev
, p
->group_count
,
208 sizeof(char *), GFP_KERNEL
);
212 for (i
= 0; i
< p
->group_count
; i
++) {
213 p
->group_names
[i
] = p
->groups
[i
].name
;
214 p
->func_count
+= p
->groups
[i
].func_count
;
217 /* we have a dummy function[0] for gpio */
220 /* allocate our function and group mapping index buffers */
221 f
= p
->func
= devm_kcalloc(p
->dev
,
223 sizeof(struct rt2880_pmx_func
),
225 gpio_func
.groups
= devm_kcalloc(p
->dev
, p
->group_count
, sizeof(int),
227 if (!f
|| !gpio_func
.groups
)
230 /* add a backpointer to the function so it knows its group */
231 gpio_func
.group_count
= p
->group_count
;
232 for (i
= 0; i
< gpio_func
.group_count
; i
++)
233 gpio_func
.groups
[i
] = i
;
238 /* add remaining functions */
239 for (i
= 0; i
< p
->group_count
; i
++) {
240 for (j
= 0; j
< p
->groups
[i
].func_count
; j
++) {
241 f
[c
] = &p
->groups
[i
].func
[j
];
242 f
[c
]->groups
= devm_kzalloc(p
->dev
, sizeof(int),
245 f
[c
]->group_count
= 1;
252 static int rt2880_pinmux_pins(struct rt2880_priv
*p
)
257 * loop over the functions and initialize the pins array.
258 * also work out the highest pin used.
260 for (i
= 0; i
< p
->func_count
; i
++) {
263 if (!p
->func
[i
]->pin_count
)
266 p
->func
[i
]->pins
= devm_kcalloc(p
->dev
,
267 p
->func
[i
]->pin_count
,
270 for (j
= 0; j
< p
->func
[i
]->pin_count
; j
++)
271 p
->func
[i
]->pins
[j
] = p
->func
[i
]->pin_first
+ j
;
273 pin
= p
->func
[i
]->pin_first
+ p
->func
[i
]->pin_count
;
274 if (pin
> p
->max_pins
)
278 /* the buffer that tells us which pins are gpio */
279 p
->gpio
= devm_kcalloc(p
->dev
, p
->max_pins
, sizeof(u8
), GFP_KERNEL
);
280 /* the pads needed to tell pinctrl about our pins */
281 p
->pads
= devm_kcalloc(p
->dev
, p
->max_pins
,
282 sizeof(struct pinctrl_pin_desc
), GFP_KERNEL
);
283 if (!p
->pads
|| !p
->gpio
) {
284 dev_err(p
->dev
, "Failed to allocate gpio data\n");
288 memset(p
->gpio
, 1, sizeof(u8
) * p
->max_pins
);
289 for (i
= 0; i
< p
->func_count
; i
++) {
290 if (!p
->func
[i
]->pin_count
)
293 for (j
= 0; j
< p
->func
[i
]->pin_count
; j
++)
294 p
->gpio
[p
->func
[i
]->pins
[j
]] = 0;
297 /* pin 0 is always a gpio */
301 for (i
= 0; i
< p
->max_pins
; i
++) {
302 /* strlen("ioXY") + 1 = 5 */
303 char *name
= devm_kzalloc(p
->dev
, 5, GFP_KERNEL
);
307 snprintf(name
, 5, "io%d", i
);
308 p
->pads
[i
].number
= i
;
309 p
->pads
[i
].name
= name
;
311 p
->desc
->pins
= p
->pads
;
312 p
->desc
->npins
= p
->max_pins
;
317 static int rt2880_pinmux_probe(struct platform_device
*pdev
)
319 struct rt2880_priv
*p
;
320 struct pinctrl_dev
*dev
;
321 struct device_node
*np
;
323 if (!rt2880_pinmux_data
)
326 /* setup the private data */
327 p
= devm_kzalloc(&pdev
->dev
, sizeof(struct rt2880_priv
), GFP_KERNEL
);
332 p
->desc
= &rt2880_pctrl_desc
;
333 p
->groups
= rt2880_pinmux_data
;
334 platform_set_drvdata(pdev
, p
);
336 /* init the device */
337 if (rt2880_pinmux_index(p
)) {
338 dev_err(&pdev
->dev
, "failed to load index\n");
341 if (rt2880_pinmux_pins(p
)) {
342 dev_err(&pdev
->dev
, "failed to load pins\n");
345 dev
= pinctrl_register(p
->desc
, &pdev
->dev
, p
);
349 /* finalize by adding gpio ranges for enables gpio controllers */
350 for_each_compatible_node(np
, NULL
, "ralink,rt2880-gpio") {
351 const __be32
*ngpio
, *gpiobase
;
352 struct pinctrl_gpio_range
*range
;
354 if (!of_device_is_available(np
))
357 ngpio
= of_get_property(np
, "ralink,num-gpios", NULL
);
358 gpiobase
= of_get_property(np
, "ralink,gpio-base", NULL
);
359 if (!ngpio
|| !gpiobase
) {
360 dev_err(&pdev
->dev
, "failed to load chip info\n");
365 range
= devm_kzalloc(p
->dev
, sizeof(*range
), GFP_KERNEL
);
371 range
->npins
= __be32_to_cpu(*ngpio
);
372 range
->base
= __be32_to_cpu(*gpiobase
);
373 range
->pin_base
= range
->base
;
374 pinctrl_add_gpio_range(dev
, range
);
380 static const struct of_device_id rt2880_pinmux_match
[] = {
381 { .compatible
= "ralink,rt2880-pinmux" },
384 MODULE_DEVICE_TABLE(of
, rt2880_pinmux_match
);
386 static struct platform_driver rt2880_pinmux_driver
= {
387 .probe
= rt2880_pinmux_probe
,
389 .name
= "rt2880-pinmux",
390 .of_match_table
= rt2880_pinmux_match
,
394 int __init
rt2880_pinmux_init(void)
396 return platform_driver_register(&rt2880_pinmux_driver
);
399 core_initcall_sync(rt2880_pinmux_init
);