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
;
54 struct mvebu_pinctrl_group
*groups
;
56 struct mvebu_pinctrl_function
*functions
;
57 unsigned num_functions
;
61 static struct mvebu_pinctrl_group
*mvebu_pinctrl_find_group_by_pid(
62 struct mvebu_pinctrl
*pctl
, unsigned pid
)
65 for (n
= 0; n
< pctl
->num_groups
; n
++) {
66 if (pid
>= pctl
->groups
[n
].pins
[0] &&
67 pid
< pctl
->groups
[n
].pins
[0] +
68 pctl
->groups
[n
].npins
)
69 return &pctl
->groups
[n
];
74 static struct mvebu_pinctrl_group
*mvebu_pinctrl_find_group_by_name(
75 struct mvebu_pinctrl
*pctl
, const char *name
)
78 for (n
= 0; n
< pctl
->num_groups
; n
++) {
79 if (strcmp(name
, pctl
->groups
[n
].name
) == 0)
80 return &pctl
->groups
[n
];
85 static struct mvebu_mpp_ctrl_setting
*mvebu_pinctrl_find_setting_by_val(
86 struct mvebu_pinctrl
*pctl
, struct mvebu_pinctrl_group
*grp
,
90 for (n
= 0; n
< grp
->num_settings
; n
++) {
91 if (config
== grp
->settings
[n
].val
) {
92 if (!pctl
->variant
|| (pctl
->variant
&
93 grp
->settings
[n
].variant
))
94 return &grp
->settings
[n
];
100 static struct mvebu_mpp_ctrl_setting
*mvebu_pinctrl_find_setting_by_name(
101 struct mvebu_pinctrl
*pctl
, struct mvebu_pinctrl_group
*grp
,
105 for (n
= 0; n
< grp
->num_settings
; n
++) {
106 if (strcmp(name
, grp
->settings
[n
].name
) == 0) {
107 if (!pctl
->variant
|| (pctl
->variant
&
108 grp
->settings
[n
].variant
))
109 return &grp
->settings
[n
];
115 static struct mvebu_mpp_ctrl_setting
*mvebu_pinctrl_find_gpio_setting(
116 struct mvebu_pinctrl
*pctl
, struct mvebu_pinctrl_group
*grp
)
119 for (n
= 0; n
< grp
->num_settings
; n
++) {
120 if (grp
->settings
[n
].flags
&
121 (MVEBU_SETTING_GPO
| MVEBU_SETTING_GPI
)) {
122 if (!pctl
->variant
|| (pctl
->variant
&
123 grp
->settings
[n
].variant
))
124 return &grp
->settings
[n
];
130 static struct mvebu_pinctrl_function
*mvebu_pinctrl_find_function_by_name(
131 struct mvebu_pinctrl
*pctl
, const char *name
)
134 for (n
= 0; n
< pctl
->num_functions
; n
++) {
135 if (strcmp(name
, pctl
->functions
[n
].name
) == 0)
136 return &pctl
->functions
[n
];
142 * Common mpp pin configuration registers on MVEBU are
143 * registers of eight 4-bit values for each mpp setting.
144 * Register offset and bit mask are calculated accordingly below.
146 static int mvebu_common_mpp_get(struct mvebu_pinctrl
*pctl
,
147 struct mvebu_pinctrl_group
*grp
,
148 unsigned long *config
)
150 unsigned pin
= grp
->gid
;
151 unsigned off
= (pin
/ MPPS_PER_REG
) * MPP_BITS
;
152 unsigned shift
= (pin
% MPPS_PER_REG
) * MPP_BITS
;
154 *config
= readl(pctl
->base
+ off
);
161 static int mvebu_common_mpp_set(struct mvebu_pinctrl
*pctl
,
162 struct mvebu_pinctrl_group
*grp
,
163 unsigned long config
)
165 unsigned pin
= grp
->gid
;
166 unsigned off
= (pin
/ MPPS_PER_REG
) * MPP_BITS
;
167 unsigned shift
= (pin
% MPPS_PER_REG
) * MPP_BITS
;
170 reg
= readl(pctl
->base
+ off
);
171 reg
&= ~(MPP_MASK
<< shift
);
172 reg
|= (config
<< shift
);
173 writel(reg
, pctl
->base
+ off
);
178 static int mvebu_pinconf_group_get(struct pinctrl_dev
*pctldev
,
179 unsigned gid
, unsigned long *config
)
181 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
182 struct mvebu_pinctrl_group
*grp
= &pctl
->groups
[gid
];
187 if (grp
->ctrl
->mpp_get
)
188 return grp
->ctrl
->mpp_get(grp
->ctrl
, config
);
190 return mvebu_common_mpp_get(pctl
, grp
, config
);
193 static int mvebu_pinconf_group_set(struct pinctrl_dev
*pctldev
,
194 unsigned gid
, unsigned long config
)
196 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
197 struct mvebu_pinctrl_group
*grp
= &pctl
->groups
[gid
];
202 if (grp
->ctrl
->mpp_set
)
203 return grp
->ctrl
->mpp_set(grp
->ctrl
, config
);
205 return mvebu_common_mpp_set(pctl
, grp
, config
);
208 static void mvebu_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
209 struct seq_file
*s
, unsigned gid
)
211 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
212 struct mvebu_pinctrl_group
*grp
= &pctl
->groups
[gid
];
213 struct mvebu_mpp_ctrl_setting
*curr
;
214 unsigned long config
;
217 if (mvebu_pinconf_group_get(pctldev
, gid
, &config
))
220 curr
= mvebu_pinctrl_find_setting_by_val(pctl
, grp
, config
);
223 seq_printf(s
, "current: %s", curr
->name
);
225 seq_printf(s
, "(%s)", curr
->subname
);
226 if (curr
->flags
& (MVEBU_SETTING_GPO
| MVEBU_SETTING_GPI
)) {
228 if (curr
->flags
& MVEBU_SETTING_GPI
)
230 if (curr
->flags
& MVEBU_SETTING_GPO
)
235 seq_printf(s
, "current: UNKNOWN");
237 if (grp
->num_settings
> 1) {
238 seq_printf(s
, ", available = [");
239 for (n
= 0; n
< grp
->num_settings
; n
++) {
240 if (curr
== &grp
->settings
[n
])
243 /* skip unsupported settings for this variant */
245 !(pctl
->variant
& grp
->settings
[n
].variant
))
248 seq_printf(s
, " %s", grp
->settings
[n
].name
);
249 if (grp
->settings
[n
].subname
)
250 seq_printf(s
, "(%s)", grp
->settings
[n
].subname
);
251 if (grp
->settings
[n
].flags
&
252 (MVEBU_SETTING_GPO
| MVEBU_SETTING_GPI
)) {
254 if (grp
->settings
[n
].flags
& MVEBU_SETTING_GPI
)
256 if (grp
->settings
[n
].flags
& MVEBU_SETTING_GPO
)
266 static struct pinconf_ops mvebu_pinconf_ops
= {
267 .pin_config_group_get
= mvebu_pinconf_group_get
,
268 .pin_config_group_set
= mvebu_pinconf_group_set
,
269 .pin_config_group_dbg_show
= mvebu_pinconf_group_dbg_show
,
272 static int mvebu_pinmux_get_funcs_count(struct pinctrl_dev
*pctldev
)
274 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
276 return pctl
->num_functions
;
279 static const char *mvebu_pinmux_get_func_name(struct pinctrl_dev
*pctldev
,
282 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
284 return pctl
->functions
[fid
].name
;
287 static int mvebu_pinmux_get_groups(struct pinctrl_dev
*pctldev
, unsigned fid
,
288 const char * const **groups
,
289 unsigned * const num_groups
)
291 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
293 *groups
= pctl
->functions
[fid
].groups
;
294 *num_groups
= pctl
->functions
[fid
].num_groups
;
298 static int mvebu_pinmux_enable(struct pinctrl_dev
*pctldev
, unsigned fid
,
301 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
302 struct mvebu_pinctrl_function
*func
= &pctl
->functions
[fid
];
303 struct mvebu_pinctrl_group
*grp
= &pctl
->groups
[gid
];
304 struct mvebu_mpp_ctrl_setting
*setting
;
307 setting
= mvebu_pinctrl_find_setting_by_name(pctl
, grp
,
311 "unable to find setting %s in group %s\n",
312 func
->name
, func
->groups
[gid
]);
316 ret
= mvebu_pinconf_group_set(pctldev
, grp
->gid
, setting
->val
);
318 dev_err(pctl
->dev
, "cannot set group %s to %s\n",
319 func
->groups
[gid
], func
->name
);
326 static int mvebu_pinmux_gpio_request_enable(struct pinctrl_dev
*pctldev
,
327 struct pinctrl_gpio_range
*range
, unsigned offset
)
329 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
330 struct mvebu_pinctrl_group
*grp
;
331 struct mvebu_mpp_ctrl_setting
*setting
;
333 grp
= mvebu_pinctrl_find_group_by_pid(pctl
, offset
);
337 if (grp
->ctrl
->mpp_gpio_req
)
338 return grp
->ctrl
->mpp_gpio_req(grp
->ctrl
, offset
);
340 setting
= mvebu_pinctrl_find_gpio_setting(pctl
, grp
);
344 return mvebu_pinconf_group_set(pctldev
, grp
->gid
, setting
->val
);
347 static int mvebu_pinmux_gpio_set_direction(struct pinctrl_dev
*pctldev
,
348 struct pinctrl_gpio_range
*range
, unsigned offset
, bool input
)
350 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
351 struct mvebu_pinctrl_group
*grp
;
352 struct mvebu_mpp_ctrl_setting
*setting
;
354 grp
= mvebu_pinctrl_find_group_by_pid(pctl
, offset
);
358 if (grp
->ctrl
->mpp_gpio_dir
)
359 return grp
->ctrl
->mpp_gpio_dir(grp
->ctrl
, offset
, input
);
361 setting
= mvebu_pinctrl_find_gpio_setting(pctl
, grp
);
365 if ((input
&& (setting
->flags
& MVEBU_SETTING_GPI
)) ||
366 (!input
&& (setting
->flags
& MVEBU_SETTING_GPO
)))
372 static struct pinmux_ops mvebu_pinmux_ops
= {
373 .get_functions_count
= mvebu_pinmux_get_funcs_count
,
374 .get_function_name
= mvebu_pinmux_get_func_name
,
375 .get_function_groups
= mvebu_pinmux_get_groups
,
376 .gpio_request_enable
= mvebu_pinmux_gpio_request_enable
,
377 .gpio_set_direction
= mvebu_pinmux_gpio_set_direction
,
378 .enable
= mvebu_pinmux_enable
,
381 static int mvebu_pinctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
383 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
384 return pctl
->num_groups
;
387 static const char *mvebu_pinctrl_get_group_name(struct pinctrl_dev
*pctldev
,
390 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
391 return pctl
->groups
[gid
].name
;
394 static int mvebu_pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
395 unsigned gid
, const unsigned **pins
,
398 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
399 *pins
= pctl
->groups
[gid
].pins
;
400 *num_pins
= pctl
->groups
[gid
].npins
;
404 static int mvebu_pinctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
405 struct device_node
*np
,
406 struct pinctrl_map
**map
,
409 struct mvebu_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
410 struct property
*prop
;
411 const char *function
;
418 ret
= of_property_read_string(np
, "marvell,function", &function
);
421 "missing marvell,function in node %s\n", np
->name
);
425 nmaps
= of_property_count_strings(np
, "marvell,pins");
428 "missing marvell,pins in node %s\n", np
->name
);
432 *map
= kmalloc(nmaps
* sizeof(struct pinctrl_map
), GFP_KERNEL
);
435 "cannot allocate pinctrl_map memory for %s\n",
441 of_property_for_each_string(np
, "marvell,pins", prop
, group
) {
442 struct mvebu_pinctrl_group
*grp
=
443 mvebu_pinctrl_find_group_by_name(pctl
, group
);
446 dev_err(pctl
->dev
, "unknown pin %s", group
);
450 if (!mvebu_pinctrl_find_setting_by_name(pctl
, grp
, function
)) {
451 dev_err(pctl
->dev
, "unsupported function %s on pin %s",
456 (*map
)[n
].type
= PIN_MAP_TYPE_MUX_GROUP
;
457 (*map
)[n
].data
.mux
.group
= group
;
458 (*map
)[n
].data
.mux
.function
= function
;
467 static void mvebu_pinctrl_dt_free_map(struct pinctrl_dev
*pctldev
,
468 struct pinctrl_map
*map
, unsigned num_maps
)
473 static struct pinctrl_ops mvebu_pinctrl_ops
= {
474 .get_groups_count
= mvebu_pinctrl_get_groups_count
,
475 .get_group_name
= mvebu_pinctrl_get_group_name
,
476 .get_group_pins
= mvebu_pinctrl_get_group_pins
,
477 .dt_node_to_map
= mvebu_pinctrl_dt_node_to_map
,
478 .dt_free_map
= mvebu_pinctrl_dt_free_map
,
481 static int _add_function(struct mvebu_pinctrl_function
*funcs
, const char *name
)
483 while (funcs
->num_groups
) {
484 /* function already there */
485 if (strcmp(funcs
->name
, name
) == 0) {
492 funcs
->num_groups
= 1;
496 static int mvebu_pinctrl_build_functions(struct platform_device
*pdev
,
497 struct mvebu_pinctrl
*pctl
)
499 struct mvebu_pinctrl_function
*funcs
;
503 /* we allocate functions for number of pins and hope
504 * there are less unique functions than pins available */
505 funcs
= devm_kzalloc(&pdev
->dev
, pctl
->desc
.npins
*
506 sizeof(struct mvebu_pinctrl_function
), GFP_KERNEL
);
510 for (n
= 0; n
< pctl
->num_groups
; n
++) {
511 struct mvebu_pinctrl_group
*grp
= &pctl
->groups
[n
];
512 for (s
= 0; s
< grp
->num_settings
; s
++) {
513 /* skip unsupported settings on this variant */
515 !(pctl
->variant
& grp
->settings
[s
].variant
))
518 /* check for unique functions and count groups */
519 if (_add_function(funcs
, grp
->settings
[s
].name
))
526 /* with the number of unique functions and it's groups known,
527 reallocate functions and assign group names */
528 funcs
= krealloc(funcs
, num
* sizeof(struct mvebu_pinctrl_function
),
533 pctl
->num_functions
= num
;
534 pctl
->functions
= funcs
;
536 for (n
= 0; n
< pctl
->num_groups
; n
++) {
537 struct mvebu_pinctrl_group
*grp
= &pctl
->groups
[n
];
538 for (s
= 0; s
< grp
->num_settings
; s
++) {
539 struct mvebu_pinctrl_function
*f
;
542 /* skip unsupported settings on this variant */
544 !(pctl
->variant
& grp
->settings
[s
].variant
))
547 f
= mvebu_pinctrl_find_function_by_name(pctl
,
548 grp
->settings
[s
].name
);
550 /* allocate group name array if not done already */
552 f
->groups
= devm_kzalloc(&pdev
->dev
,
553 f
->num_groups
* sizeof(char *),
559 /* find next free group name and assign current name */
570 int mvebu_pinctrl_probe(struct platform_device
*pdev
)
572 struct mvebu_pinctrl_soc_info
*soc
= dev_get_platdata(&pdev
->dev
);
573 struct device_node
*np
= pdev
->dev
.of_node
;
574 struct mvebu_pinctrl
*pctl
;
576 struct pinctrl_pin_desc
*pdesc
;
580 if (!soc
|| !soc
->controls
|| !soc
->modes
) {
581 dev_err(&pdev
->dev
, "wrong pinctrl soc info\n");
585 base
= of_iomap(np
, 0);
587 dev_err(&pdev
->dev
, "unable to get base address\n");
591 pctl
= devm_kzalloc(&pdev
->dev
, sizeof(struct mvebu_pinctrl
),
594 dev_err(&pdev
->dev
, "unable to alloc driver\n");
598 pctl
->desc
.name
= dev_name(&pdev
->dev
);
599 pctl
->desc
.owner
= THIS_MODULE
;
600 pctl
->desc
.pctlops
= &mvebu_pinctrl_ops
;
601 pctl
->desc
.pmxops
= &mvebu_pinmux_ops
;
602 pctl
->desc
.confops
= &mvebu_pinconf_ops
;
603 pctl
->variant
= soc
->variant
;
605 pctl
->dev
= &pdev
->dev
;
606 platform_set_drvdata(pdev
, pctl
);
608 /* count controls and create names for mvebu generic
609 register controls; also does sanity checks */
610 pctl
->num_groups
= 0;
611 pctl
->desc
.npins
= 0;
612 for (n
= 0; n
< soc
->ncontrols
; n
++) {
613 struct mvebu_mpp_ctrl
*ctrl
= &soc
->controls
[n
];
616 pctl
->desc
.npins
+= ctrl
->npins
;
617 /* initial control pins */
618 for (k
= 0; k
< ctrl
->npins
; k
++)
619 ctrl
->pins
[k
] = ctrl
->pid
+ k
;
621 /* special soc specific control */
622 if (ctrl
->mpp_get
|| ctrl
->mpp_set
) {
623 if (!ctrl
->name
|| !ctrl
->mpp_set
|| !ctrl
->mpp_set
) {
624 dev_err(&pdev
->dev
, "wrong soc control info\n");
627 pctl
->num_groups
+= 1;
631 /* generic mvebu register control */
632 names
= devm_kzalloc(&pdev
->dev
, ctrl
->npins
* 8, GFP_KERNEL
);
634 dev_err(&pdev
->dev
, "failed to alloc mpp names\n");
637 for (k
= 0; k
< ctrl
->npins
; k
++)
638 sprintf(names
+ 8*k
, "mpp%d", ctrl
->pid
+k
);
640 pctl
->num_groups
+= ctrl
->npins
;
643 pdesc
= devm_kzalloc(&pdev
->dev
, pctl
->desc
.npins
*
644 sizeof(struct pinctrl_pin_desc
), GFP_KERNEL
);
646 dev_err(&pdev
->dev
, "failed to alloc pinctrl pins\n");
650 for (n
= 0; n
< pctl
->desc
.npins
; n
++)
652 pctl
->desc
.pins
= pdesc
;
654 pctl
->groups
= devm_kzalloc(&pdev
->dev
, pctl
->num_groups
*
655 sizeof(struct mvebu_pinctrl_group
), GFP_KERNEL
);
657 dev_err(&pdev
->dev
, "failed to alloc pinctrl groups\n");
661 /* assign mpp controls to groups */
663 for (n
= 0; n
< soc
->ncontrols
; n
++) {
664 struct mvebu_mpp_ctrl
*ctrl
= &soc
->controls
[n
];
665 pctl
->groups
[gid
].gid
= gid
;
666 pctl
->groups
[gid
].ctrl
= ctrl
;
667 pctl
->groups
[gid
].name
= ctrl
->name
;
668 pctl
->groups
[gid
].pins
= ctrl
->pins
;
669 pctl
->groups
[gid
].npins
= ctrl
->npins
;
671 /* generic mvebu register control maps to a number of groups */
672 if (!ctrl
->mpp_get
&& !ctrl
->mpp_set
) {
673 pctl
->groups
[gid
].npins
= 1;
675 for (k
= 1; k
< ctrl
->npins
; k
++) {
677 pctl
->groups
[gid
].gid
= gid
;
678 pctl
->groups
[gid
].ctrl
= ctrl
;
679 pctl
->groups
[gid
].name
= &ctrl
->name
[8*k
];
680 pctl
->groups
[gid
].pins
= &ctrl
->pins
[k
];
681 pctl
->groups
[gid
].npins
= 1;
687 /* assign mpp modes to groups */
688 for (n
= 0; n
< soc
->nmodes
; n
++) {
689 struct mvebu_mpp_mode
*mode
= &soc
->modes
[n
];
690 struct mvebu_pinctrl_group
*grp
=
691 mvebu_pinctrl_find_group_by_pid(pctl
, mode
->pid
);
692 unsigned num_settings
;
695 dev_warn(&pdev
->dev
, "unknown pinctrl group %d\n",
700 for (num_settings
= 0; ;) {
701 struct mvebu_mpp_ctrl_setting
*set
=
702 &mode
->settings
[num_settings
];
708 /* skip unsupported settings for this variant */
709 if (pctl
->variant
&& !(pctl
->variant
& set
->variant
))
712 /* find gpio/gpo/gpi settings */
713 if (strcmp(set
->name
, "gpio") == 0)
714 set
->flags
= MVEBU_SETTING_GPI
|
716 else if (strcmp(set
->name
, "gpo") == 0)
717 set
->flags
= MVEBU_SETTING_GPO
;
718 else if (strcmp(set
->name
, "gpi") == 0)
719 set
->flags
= MVEBU_SETTING_GPI
;
722 grp
->settings
= mode
->settings
;
723 grp
->num_settings
= num_settings
;
726 ret
= mvebu_pinctrl_build_functions(pdev
, pctl
);
728 dev_err(&pdev
->dev
, "unable to build functions\n");
732 pctl
->pctldev
= pinctrl_register(&pctl
->desc
, &pdev
->dev
, pctl
);
733 if (!pctl
->pctldev
) {
734 dev_err(&pdev
->dev
, "unable to register pinctrl driver\n");
738 dev_info(&pdev
->dev
, "registered pinctrl driver\n");
740 /* register gpio ranges */
741 for (n
= 0; n
< soc
->ngpioranges
; n
++)
742 pinctrl_add_gpio_range(pctl
->pctldev
, &soc
->gpioranges
[n
]);
747 int mvebu_pinctrl_remove(struct platform_device
*pdev
)
749 struct mvebu_pinctrl
*pctl
= platform_get_drvdata(pdev
);
750 pinctrl_unregister(pctl
->pctldev
);