2 * Allwinner A1X SoCs pinctrl driver.
4 * Copyright (C) 2012 Maxime Ripard
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/pinctrl/machine.h>
20 #include <linux/pinctrl/pinctrl.h>
21 #include <linux/pinctrl/pinconf-generic.h>
22 #include <linux/pinctrl/pinmux.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
27 #include "pinctrl-sunxi.h"
29 static struct sunxi_pinctrl_group *
30 sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
34 for (i = 0; i < pctl->ngroups; i++) {
35 struct sunxi_pinctrl_group *grp = pctl->groups + i;
37 if (!strcmp(grp->name, group))
44 static struct sunxi_pinctrl_function *
45 sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
48 struct sunxi_pinctrl_function *func = pctl->functions;
51 for (i = 0; i < pctl->nfunctions; i++) {
55 if (!strcmp(func[i].name, name))
62 static struct sunxi_desc_function *
63 sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
65 const char *func_name)
69 for (i = 0; i < pctl->desc->npins; i++) {
70 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
72 if (!strcmp(pin->pin.name, pin_name)) {
73 struct sunxi_desc_function *func = pin->functions;
76 if (!strcmp(func->name, func_name))
87 static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
89 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
94 static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
97 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
99 return pctl->groups[group].name;
102 static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
104 const unsigned **pins,
107 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
109 *pins = (unsigned *)&pctl->groups[group].pin;
115 static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
116 struct device_node *node,
117 struct pinctrl_map **map,
120 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
121 unsigned long *pinconfig;
122 struct property *prop;
123 const char *function;
125 int ret, nmaps, i = 0;
131 ret = of_property_read_string(node, "allwinner,function", &function);
134 "missing allwinner,function property in node %s\n",
139 nmaps = of_property_count_strings(node, "allwinner,pins") * 2;
142 "missing allwinner,pins property in node %s\n",
147 *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
151 of_property_for_each_string(node, "allwinner,pins", prop, group) {
152 struct sunxi_pinctrl_group *grp =
153 sunxi_pinctrl_find_group_by_name(pctl, group);
154 int j = 0, configlen = 0;
157 dev_err(pctl->dev, "unknown pin %s", group);
161 if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
164 dev_err(pctl->dev, "unsupported function %s on pin %s",
169 (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
170 (*map)[i].data.mux.group = group;
171 (*map)[i].data.mux.function = function;
175 (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
176 (*map)[i].data.configs.group_or_pin = group;
178 if (of_find_property(node, "allwinner,drive", NULL))
180 if (of_find_property(node, "allwinner,pull", NULL))
183 pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
185 if (!of_property_read_u32(node, "allwinner,drive", &val)) {
186 u16 strength = (val + 1) * 10;
188 pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
192 if (!of_property_read_u32(node, "allwinner,pull", &val)) {
193 enum pin_config_param pull = PIN_CONFIG_END;
195 pull = PIN_CONFIG_BIAS_PULL_UP;
197 pull = PIN_CONFIG_BIAS_PULL_DOWN;
198 pinconfig[j++] = pinconf_to_config_packed(pull, 0);
201 (*map)[i].data.configs.configs = pinconfig;
202 (*map)[i].data.configs.num_configs = configlen;
212 static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
213 struct pinctrl_map *map,
218 for (i = 0; i < num_maps; i++) {
219 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
220 kfree(map[i].data.configs.configs);
226 static struct pinctrl_ops sunxi_pctrl_ops = {
227 .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
228 .dt_free_map = sunxi_pctrl_dt_free_map,
229 .get_groups_count = sunxi_pctrl_get_groups_count,
230 .get_group_name = sunxi_pctrl_get_group_name,
231 .get_group_pins = sunxi_pctrl_get_group_pins,
234 static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
236 unsigned long *config)
238 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
240 *config = pctl->groups[group].config;
245 static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
247 unsigned long config)
249 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
250 struct sunxi_pinctrl_group *g = &pctl->groups[group];
255 switch (pinconf_to_config_param(config)) {
256 case PIN_CONFIG_DRIVE_STRENGTH:
257 strength = pinconf_to_config_argument(config);
261 * We convert from mA to what the register expects:
267 dlevel = strength / 10 - 1;
268 val = readl(pctl->membase + sunxi_dlevel_reg(g->pin));
269 mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(g->pin);
270 writel((val & ~mask) | dlevel << sunxi_dlevel_offset(g->pin),
271 pctl->membase + sunxi_dlevel_reg(g->pin));
273 case PIN_CONFIG_BIAS_PULL_UP:
274 val = readl(pctl->membase + sunxi_pull_reg(g->pin));
275 mask = PULL_PINS_MASK << sunxi_pull_offset(g->pin);
276 writel((val & ~mask) | 1 << sunxi_pull_offset(g->pin),
277 pctl->membase + sunxi_pull_reg(g->pin));
279 case PIN_CONFIG_BIAS_PULL_DOWN:
280 val = readl(pctl->membase + sunxi_pull_reg(g->pin));
281 mask = PULL_PINS_MASK << sunxi_pull_offset(g->pin);
282 writel((val & ~mask) | 2 << sunxi_pull_offset(g->pin),
283 pctl->membase + sunxi_pull_reg(g->pin));
289 /* cache the config value */
295 static struct pinconf_ops sunxi_pconf_ops = {
296 .pin_config_group_get = sunxi_pconf_group_get,
297 .pin_config_group_set = sunxi_pconf_group_set,
300 static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
302 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
304 return pctl->nfunctions;
307 static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
310 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
312 return pctl->functions[function].name;
315 static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
317 const char * const **groups,
318 unsigned * const num_groups)
320 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
322 *groups = pctl->functions[function].groups;
323 *num_groups = pctl->functions[function].ngroups;
328 static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
332 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
334 u32 val = readl(pctl->membase + sunxi_mux_reg(pin));
335 u32 mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
336 writel((val & ~mask) | config << sunxi_mux_offset(pin),
337 pctl->membase + sunxi_mux_reg(pin));
340 static int sunxi_pmx_enable(struct pinctrl_dev *pctldev,
344 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
345 struct sunxi_pinctrl_group *g = pctl->groups + group;
346 struct sunxi_pinctrl_function *func = pctl->functions + function;
347 struct sunxi_desc_function *desc =
348 sunxi_pinctrl_desc_find_function_by_name(pctl,
355 sunxi_pmx_set(pctldev, g->pin, desc->muxval);
360 static struct pinmux_ops sunxi_pmx_ops = {
361 .get_functions_count = sunxi_pmx_get_funcs_cnt,
362 .get_function_name = sunxi_pmx_get_func_name,
363 .get_function_groups = sunxi_pmx_get_func_groups,
364 .enable = sunxi_pmx_enable,
367 static struct pinctrl_desc sunxi_pctrl_desc = {
368 .confops = &sunxi_pconf_ops,
369 .pctlops = &sunxi_pctrl_ops,
370 .pmxops = &sunxi_pmx_ops,
373 static struct of_device_id sunxi_pinctrl_match[] = {
376 MODULE_DEVICE_TABLE(of, sunxi_pinctrl_match);
378 static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
381 struct sunxi_pinctrl_function *func = pctl->functions;
384 /* function already there */
385 if (strcmp(func->name, name) == 0) {
400 static int sunxi_pinctrl_build_state(struct platform_device *pdev)
402 struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
405 pctl->ngroups = pctl->desc->npins;
407 /* Allocate groups */
408 pctl->groups = devm_kzalloc(&pdev->dev,
409 pctl->ngroups * sizeof(*pctl->groups),
414 for (i = 0; i < pctl->desc->npins; i++) {
415 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
416 struct sunxi_pinctrl_group *group = pctl->groups + i;
418 group->name = pin->pin.name;
419 group->pin = pin->pin.number;
423 * We suppose that we won't have any more functions than pins,
424 * we'll reallocate that later anyway
426 pctl->functions = devm_kzalloc(&pdev->dev,
427 pctl->desc->npins * sizeof(*pctl->functions),
429 if (!pctl->functions)
432 /* Count functions and their associated groups */
433 for (i = 0; i < pctl->desc->npins; i++) {
434 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
435 struct sunxi_desc_function *func = pin->functions;
438 sunxi_pinctrl_add_function(pctl, func->name);
443 pctl->functions = krealloc(pctl->functions,
444 pctl->nfunctions * sizeof(*pctl->functions),
447 for (i = 0; i < pctl->desc->npins; i++) {
448 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
449 struct sunxi_desc_function *func = pin->functions;
452 struct sunxi_pinctrl_function *func_item;
453 const char **func_grp;
455 func_item = sunxi_pinctrl_find_function_by_name(pctl,
460 if (!func_item->groups) {
462 devm_kzalloc(&pdev->dev,
463 func_item->ngroups * sizeof(*func_item->groups),
465 if (!func_item->groups)
469 func_grp = func_item->groups;
473 *func_grp = pin->pin.name;
481 static int sunxi_pinctrl_probe(struct platform_device *pdev)
483 struct device_node *node = pdev->dev.of_node;
484 const struct of_device_id *device;
485 struct pinctrl_pin_desc *pins;
486 struct sunxi_pinctrl *pctl;
489 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
492 platform_set_drvdata(pdev, pctl);
494 pctl->membase = of_iomap(node, 0);
498 device = of_match_device(sunxi_pinctrl_match, &pdev->dev);
502 pctl->desc = (struct sunxi_pinctrl_desc *)device->data;
504 ret = sunxi_pinctrl_build_state(pdev);
506 dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
510 pins = devm_kzalloc(&pdev->dev,
511 pctl->desc->npins * sizeof(*pins),
516 for (i = 0; i < pctl->desc->npins; i++)
517 pins[i] = pctl->desc->pins[i].pin;
519 sunxi_pctrl_desc.name = dev_name(&pdev->dev);
520 sunxi_pctrl_desc.owner = THIS_MODULE;
521 sunxi_pctrl_desc.pins = pins;
522 sunxi_pctrl_desc.npins = pctl->desc->npins;
523 pctl->dev = &pdev->dev;
524 pctl->pctl_dev = pinctrl_register(&sunxi_pctrl_desc,
526 if (!pctl->pctl_dev) {
527 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
531 dev_info(&pdev->dev, "initialized sunXi pin control driver\n");
536 static struct platform_driver sunxi_pinctrl_driver = {
537 .probe = sunxi_pinctrl_probe,
539 .name = "sunxi-pinctrl",
540 .owner = THIS_MODULE,
541 .of_match_table = sunxi_pinctrl_match,
544 module_platform_driver(sunxi_pinctrl_driver);
546 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
547 MODULE_DESCRIPTION("Allwinner A1X pinctrl driver");
548 MODULE_LICENSE("GPL");