2 * Marvell MVEBU pinctrl core driver
4 * Authors: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
5 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
18 #include <linux/of_address.h>
19 #include <linux/of_platform.h>
20 #include <linux/err.h>
21 #include <linux/gpio.h>
22 #include <linux/pinctrl/machine.h>
23 #include <linux/pinctrl/pinconf.h>
24 #include <linux/pinctrl/pinctrl.h>
25 #include <linux/pinctrl/pinmux.h>
27 #include "pinctrl-mvebu.h"
29 #define MPPS_PER_REG 8
33 struct mvebu_pinctrl_function {
39 struct mvebu_pinctrl_group {
41 struct mvebu_mpp_ctrl *ctrl;
42 struct mvebu_mpp_ctrl_setting *settings;
43 unsigned num_settings;
49 struct mvebu_pinctrl {
51 struct pinctrl_dev *pctldev;
52 struct pinctrl_desc desc;
53 struct mvebu_pinctrl_group *groups;
55 struct mvebu_pinctrl_function *functions;
56 unsigned num_functions;
60 static struct mvebu_pinctrl_group *mvebu_pinctrl_find_group_by_pid(
61 struct mvebu_pinctrl *pctl, unsigned pid)
64 for (n = 0; n < pctl->num_groups; n++) {
65 if (pid >= pctl->groups[n].pins[0] &&
66 pid < pctl->groups[n].pins[0] +
67 pctl->groups[n].npins)
68 return &pctl->groups[n];
73 static struct mvebu_pinctrl_group *mvebu_pinctrl_find_group_by_name(
74 struct mvebu_pinctrl *pctl, const char *name)
77 for (n = 0; n < pctl->num_groups; n++) {
78 if (strcmp(name, pctl->groups[n].name) == 0)
79 return &pctl->groups[n];
84 static struct mvebu_mpp_ctrl_setting *mvebu_pinctrl_find_setting_by_val(
85 struct mvebu_pinctrl *pctl, struct mvebu_pinctrl_group *grp,
89 for (n = 0; n < grp->num_settings; n++) {
90 if (config == grp->settings[n].val) {
91 if (!pctl->variant || (pctl->variant &
92 grp->settings[n].variant))
93 return &grp->settings[n];
99 static struct mvebu_mpp_ctrl_setting *mvebu_pinctrl_find_setting_by_name(
100 struct mvebu_pinctrl *pctl, struct mvebu_pinctrl_group *grp,
104 for (n = 0; n < grp->num_settings; n++) {
105 if (strcmp(name, grp->settings[n].name) == 0) {
106 if (!pctl->variant || (pctl->variant &
107 grp->settings[n].variant))
108 return &grp->settings[n];
114 static struct mvebu_mpp_ctrl_setting *mvebu_pinctrl_find_gpio_setting(
115 struct mvebu_pinctrl *pctl, struct mvebu_pinctrl_group *grp)
118 for (n = 0; n < grp->num_settings; n++) {
119 if (grp->settings[n].flags &
120 (MVEBU_SETTING_GPO | MVEBU_SETTING_GPI)) {
121 if (!pctl->variant || (pctl->variant &
122 grp->settings[n].variant))
123 return &grp->settings[n];
129 static struct mvebu_pinctrl_function *mvebu_pinctrl_find_function_by_name(
130 struct mvebu_pinctrl *pctl, const char *name)
133 for (n = 0; n < pctl->num_functions; n++) {
134 if (strcmp(name, pctl->functions[n].name) == 0)
135 return &pctl->functions[n];
140 static int mvebu_pinconf_group_get(struct pinctrl_dev *pctldev,
141 unsigned gid, unsigned long *config)
143 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
144 struct mvebu_pinctrl_group *grp = &pctl->groups[gid];
149 return grp->ctrl->mpp_get(grp->pins[0], config);
152 static int mvebu_pinconf_group_set(struct pinctrl_dev *pctldev,
153 unsigned gid, unsigned long *configs,
154 unsigned num_configs)
156 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
157 struct mvebu_pinctrl_group *grp = &pctl->groups[gid];
163 for (i = 0; i < num_configs; i++) {
164 ret = grp->ctrl->mpp_set(grp->pins[0], configs[i]);
167 } /* for each config */
172 static void mvebu_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
173 struct seq_file *s, unsigned gid)
175 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
176 struct mvebu_pinctrl_group *grp = &pctl->groups[gid];
177 struct mvebu_mpp_ctrl_setting *curr;
178 unsigned long config;
181 if (mvebu_pinconf_group_get(pctldev, gid, &config))
184 curr = mvebu_pinctrl_find_setting_by_val(pctl, grp, config);
187 seq_printf(s, "current: %s", curr->name);
189 seq_printf(s, "(%s)", curr->subname);
190 if (curr->flags & (MVEBU_SETTING_GPO | MVEBU_SETTING_GPI)) {
192 if (curr->flags & MVEBU_SETTING_GPI)
194 if (curr->flags & MVEBU_SETTING_GPO)
199 seq_printf(s, "current: UNKNOWN");
201 if (grp->num_settings > 1) {
202 seq_printf(s, ", available = [");
203 for (n = 0; n < grp->num_settings; n++) {
204 if (curr == &grp->settings[n])
207 /* skip unsupported settings for this variant */
209 !(pctl->variant & grp->settings[n].variant))
212 seq_printf(s, " %s", grp->settings[n].name);
213 if (grp->settings[n].subname)
214 seq_printf(s, "(%s)", grp->settings[n].subname);
215 if (grp->settings[n].flags &
216 (MVEBU_SETTING_GPO | MVEBU_SETTING_GPI)) {
218 if (grp->settings[n].flags & MVEBU_SETTING_GPI)
220 if (grp->settings[n].flags & MVEBU_SETTING_GPO)
230 static const struct pinconf_ops mvebu_pinconf_ops = {
231 .pin_config_group_get = mvebu_pinconf_group_get,
232 .pin_config_group_set = mvebu_pinconf_group_set,
233 .pin_config_group_dbg_show = mvebu_pinconf_group_dbg_show,
236 static int mvebu_pinmux_get_funcs_count(struct pinctrl_dev *pctldev)
238 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
240 return pctl->num_functions;
243 static const char *mvebu_pinmux_get_func_name(struct pinctrl_dev *pctldev,
246 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
248 return pctl->functions[fid].name;
251 static int mvebu_pinmux_get_groups(struct pinctrl_dev *pctldev, unsigned fid,
252 const char * const **groups,
253 unsigned * const num_groups)
255 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
257 *groups = pctl->functions[fid].groups;
258 *num_groups = pctl->functions[fid].num_groups;
262 static int mvebu_pinmux_set(struct pinctrl_dev *pctldev, unsigned fid,
265 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
266 struct mvebu_pinctrl_function *func = &pctl->functions[fid];
267 struct mvebu_pinctrl_group *grp = &pctl->groups[gid];
268 struct mvebu_mpp_ctrl_setting *setting;
270 unsigned long config;
272 setting = mvebu_pinctrl_find_setting_by_name(pctl, grp,
276 "unable to find setting %s in group %s\n",
277 func->name, func->groups[gid]);
281 config = setting->val;
282 ret = mvebu_pinconf_group_set(pctldev, grp->gid, &config, 1);
284 dev_err(pctl->dev, "cannot set group %s to %s\n",
285 func->groups[gid], func->name);
292 static int mvebu_pinmux_gpio_request_enable(struct pinctrl_dev *pctldev,
293 struct pinctrl_gpio_range *range, unsigned offset)
295 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
296 struct mvebu_pinctrl_group *grp;
297 struct mvebu_mpp_ctrl_setting *setting;
298 unsigned long config;
300 grp = mvebu_pinctrl_find_group_by_pid(pctl, offset);
304 if (grp->ctrl->mpp_gpio_req)
305 return grp->ctrl->mpp_gpio_req(offset);
307 setting = mvebu_pinctrl_find_gpio_setting(pctl, grp);
311 config = setting->val;
313 return mvebu_pinconf_group_set(pctldev, grp->gid, &config, 1);
316 static int mvebu_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
317 struct pinctrl_gpio_range *range, unsigned offset, bool input)
319 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
320 struct mvebu_pinctrl_group *grp;
321 struct mvebu_mpp_ctrl_setting *setting;
323 grp = mvebu_pinctrl_find_group_by_pid(pctl, offset);
327 if (grp->ctrl->mpp_gpio_dir)
328 return grp->ctrl->mpp_gpio_dir(offset, input);
330 setting = mvebu_pinctrl_find_gpio_setting(pctl, grp);
334 if ((input && (setting->flags & MVEBU_SETTING_GPI)) ||
335 (!input && (setting->flags & MVEBU_SETTING_GPO)))
341 static const struct pinmux_ops mvebu_pinmux_ops = {
342 .get_functions_count = mvebu_pinmux_get_funcs_count,
343 .get_function_name = mvebu_pinmux_get_func_name,
344 .get_function_groups = mvebu_pinmux_get_groups,
345 .gpio_request_enable = mvebu_pinmux_gpio_request_enable,
346 .gpio_set_direction = mvebu_pinmux_gpio_set_direction,
347 .set_mux = mvebu_pinmux_set,
350 static int mvebu_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
352 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
353 return pctl->num_groups;
356 static const char *mvebu_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
359 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
360 return pctl->groups[gid].name;
363 static int mvebu_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
364 unsigned gid, const unsigned **pins,
367 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
368 *pins = pctl->groups[gid].pins;
369 *num_pins = pctl->groups[gid].npins;
373 static int mvebu_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
374 struct device_node *np,
375 struct pinctrl_map **map,
378 struct mvebu_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
379 struct property *prop;
380 const char *function;
387 ret = of_property_read_string(np, "marvell,function", &function);
390 "missing marvell,function in node %s\n", np->name);
394 nmaps = of_property_count_strings(np, "marvell,pins");
397 "missing marvell,pins in node %s\n", np->name);
401 *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
404 "cannot allocate pinctrl_map memory for %s\n",
410 of_property_for_each_string(np, "marvell,pins", prop, group) {
411 struct mvebu_pinctrl_group *grp =
412 mvebu_pinctrl_find_group_by_name(pctl, group);
415 dev_err(pctl->dev, "unknown pin %s", group);
419 if (!mvebu_pinctrl_find_setting_by_name(pctl, grp, function)) {
420 dev_err(pctl->dev, "unsupported function %s on pin %s",
425 (*map)[n].type = PIN_MAP_TYPE_MUX_GROUP;
426 (*map)[n].data.mux.group = group;
427 (*map)[n].data.mux.function = function;
436 static void mvebu_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
437 struct pinctrl_map *map, unsigned num_maps)
442 static const struct pinctrl_ops mvebu_pinctrl_ops = {
443 .get_groups_count = mvebu_pinctrl_get_groups_count,
444 .get_group_name = mvebu_pinctrl_get_group_name,
445 .get_group_pins = mvebu_pinctrl_get_group_pins,
446 .dt_node_to_map = mvebu_pinctrl_dt_node_to_map,
447 .dt_free_map = mvebu_pinctrl_dt_free_map,
450 static int _add_function(struct mvebu_pinctrl_function *funcs, int *funcsize,
456 while (funcs->num_groups) {
457 /* function already there */
458 if (strcmp(funcs->name, name) == 0) {
465 /* append new unique function */
467 funcs->num_groups = 1;
473 static int mvebu_pinctrl_build_functions(struct platform_device *pdev,
474 struct mvebu_pinctrl *pctl)
476 struct mvebu_pinctrl_function *funcs;
477 int num = 0, funcsize = pctl->desc.npins;
480 /* we allocate functions for number of pins and hope
481 * there are fewer unique functions than pins available */
482 funcs = devm_kzalloc(&pdev->dev, funcsize *
483 sizeof(struct mvebu_pinctrl_function), GFP_KERNEL);
487 for (n = 0; n < pctl->num_groups; n++) {
488 struct mvebu_pinctrl_group *grp = &pctl->groups[n];
489 for (s = 0; s < grp->num_settings; s++) {
492 /* skip unsupported settings on this variant */
494 !(pctl->variant & grp->settings[s].variant))
497 /* check for unique functions and count groups */
498 ret = _add_function(funcs, &funcsize,
499 grp->settings[s].name);
500 if (ret == -EOVERFLOW)
502 "More functions than pins(%d)\n",
511 pctl->num_functions = num;
512 pctl->functions = funcs;
514 for (n = 0; n < pctl->num_groups; n++) {
515 struct mvebu_pinctrl_group *grp = &pctl->groups[n];
516 for (s = 0; s < grp->num_settings; s++) {
517 struct mvebu_pinctrl_function *f;
520 /* skip unsupported settings on this variant */
522 !(pctl->variant & grp->settings[s].variant))
525 f = mvebu_pinctrl_find_function_by_name(pctl,
526 grp->settings[s].name);
528 /* allocate group name array if not done already */
530 f->groups = devm_kzalloc(&pdev->dev,
531 f->num_groups * sizeof(char *),
537 /* find next free group name and assign current name */
548 int mvebu_pinctrl_probe(struct platform_device *pdev)
550 struct mvebu_pinctrl_soc_info *soc = dev_get_platdata(&pdev->dev);
551 struct mvebu_pinctrl *pctl;
552 struct pinctrl_pin_desc *pdesc;
554 unsigned size, noname = 0;
559 if (!soc || !soc->controls || !soc->modes) {
560 dev_err(&pdev->dev, "wrong pinctrl soc info\n");
564 pctl = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_pinctrl),
567 dev_err(&pdev->dev, "unable to alloc driver\n");
571 pctl->desc.name = dev_name(&pdev->dev);
572 pctl->desc.owner = THIS_MODULE;
573 pctl->desc.pctlops = &mvebu_pinctrl_ops;
574 pctl->desc.pmxops = &mvebu_pinmux_ops;
575 pctl->desc.confops = &mvebu_pinconf_ops;
576 pctl->variant = soc->variant;
577 pctl->dev = &pdev->dev;
578 platform_set_drvdata(pdev, pctl);
580 /* count controls and create names for mvebu generic
581 register controls; also does sanity checks */
582 pctl->num_groups = 0;
583 pctl->desc.npins = 0;
584 for (n = 0; n < soc->ncontrols; n++) {
585 struct mvebu_mpp_ctrl *ctrl = &soc->controls[n];
587 pctl->desc.npins += ctrl->npins;
588 /* initialize control's pins[] array */
589 for (k = 0; k < ctrl->npins; k++)
590 ctrl->pins[k] = ctrl->pid + k;
593 * We allow to pass controls with NULL name that we treat
594 * as a range of one-pin groups with generic mvebu register
598 pctl->num_groups += ctrl->npins;
599 noname += ctrl->npins;
601 pctl->num_groups += 1;
605 pdesc = devm_kzalloc(&pdev->dev, pctl->desc.npins *
606 sizeof(struct pinctrl_pin_desc), GFP_KERNEL);
608 dev_err(&pdev->dev, "failed to alloc pinctrl pins\n");
612 for (n = 0; n < pctl->desc.npins; n++)
614 pctl->desc.pins = pdesc;
617 * allocate groups and name buffers for unnamed groups.
619 size = pctl->num_groups * sizeof(*pctl->groups) + noname * 8;
620 p = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
622 dev_err(&pdev->dev, "failed to alloc group data\n");
626 noname_buf = p + pctl->num_groups * sizeof(*pctl->groups);
628 /* assign mpp controls to groups */
630 for (n = 0; n < soc->ncontrols; n++) {
631 struct mvebu_mpp_ctrl *ctrl = &soc->controls[n];
632 pctl->groups[gid].gid = gid;
633 pctl->groups[gid].ctrl = ctrl;
634 pctl->groups[gid].name = ctrl->name;
635 pctl->groups[gid].pins = ctrl->pins;
636 pctl->groups[gid].npins = ctrl->npins;
639 * We treat unnamed controls as a range of one-pin groups
640 * with generic mvebu register controls. Use one group for
641 * each in this range and assign a default group name.
644 pctl->groups[gid].name = noname_buf;
645 pctl->groups[gid].npins = 1;
646 sprintf(noname_buf, "mpp%d", ctrl->pid+0);
649 for (k = 1; k < ctrl->npins; k++) {
651 pctl->groups[gid].gid = gid;
652 pctl->groups[gid].ctrl = ctrl;
653 pctl->groups[gid].name = noname_buf;
654 pctl->groups[gid].pins = &ctrl->pins[k];
655 pctl->groups[gid].npins = 1;
656 sprintf(noname_buf, "mpp%d", ctrl->pid+k);
663 /* assign mpp modes to groups */
664 for (n = 0; n < soc->nmodes; n++) {
665 struct mvebu_mpp_mode *mode = &soc->modes[n];
666 struct mvebu_pinctrl_group *grp =
667 mvebu_pinctrl_find_group_by_pid(pctl, mode->pid);
668 unsigned num_settings;
671 dev_warn(&pdev->dev, "unknown pinctrl group %d\n",
676 for (num_settings = 0; ;) {
677 struct mvebu_mpp_ctrl_setting *set =
678 &mode->settings[num_settings];
684 /* skip unsupported settings for this variant */
685 if (pctl->variant && !(pctl->variant & set->variant))
688 /* find gpio/gpo/gpi settings */
689 if (strcmp(set->name, "gpio") == 0)
690 set->flags = MVEBU_SETTING_GPI |
692 else if (strcmp(set->name, "gpo") == 0)
693 set->flags = MVEBU_SETTING_GPO;
694 else if (strcmp(set->name, "gpi") == 0)
695 set->flags = MVEBU_SETTING_GPI;
698 grp->settings = mode->settings;
699 grp->num_settings = num_settings;
702 ret = mvebu_pinctrl_build_functions(pdev, pctl);
704 dev_err(&pdev->dev, "unable to build functions\n");
708 pctl->pctldev = pinctrl_register(&pctl->desc, &pdev->dev, pctl);
709 if (IS_ERR(pctl->pctldev)) {
710 dev_err(&pdev->dev, "unable to register pinctrl driver\n");
711 return PTR_ERR(pctl->pctldev);
714 dev_info(&pdev->dev, "registered pinctrl driver\n");
716 /* register gpio ranges */
717 for (n = 0; n < soc->ngpioranges; n++)
718 pinctrl_add_gpio_range(pctl->pctldev, &soc->gpioranges[n]);
723 int mvebu_pinctrl_remove(struct platform_device *pdev)
725 struct mvebu_pinctrl *pctl = platform_get_drvdata(pdev);
726 pinctrl_unregister(pctl->pctldev);