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 const 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 const 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 const 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
, int *funcsize
,
487 while (funcs
->num_groups
) {
488 /* function already there */
489 if (strcmp(funcs
->name
, name
) == 0) {
496 /* append new unique function */
498 funcs
->num_groups
= 1;
504 static int mvebu_pinctrl_build_functions(struct platform_device
*pdev
,
505 struct mvebu_pinctrl
*pctl
)
507 struct mvebu_pinctrl_function
*funcs
;
508 int num
= 0, funcsize
= pctl
->desc
.npins
;
511 /* we allocate functions for number of pins and hope
512 * there are fewer unique functions than pins available */
513 funcs
= devm_kzalloc(&pdev
->dev
, funcsize
*
514 sizeof(struct mvebu_pinctrl_function
), GFP_KERNEL
);
518 for (n
= 0; n
< pctl
->num_groups
; n
++) {
519 struct mvebu_pinctrl_group
*grp
= &pctl
->groups
[n
];
520 for (s
= 0; s
< grp
->num_settings
; s
++) {
523 /* skip unsupported settings on this variant */
525 !(pctl
->variant
& grp
->settings
[s
].variant
))
528 /* check for unique functions and count groups */
529 ret
= _add_function(funcs
, &funcsize
,
530 grp
->settings
[s
].name
);
531 if (ret
== -EOVERFLOW
)
533 "More functions than pins(%d)\n",
542 pctl
->num_functions
= num
;
543 pctl
->functions
= funcs
;
545 for (n
= 0; n
< pctl
->num_groups
; n
++) {
546 struct mvebu_pinctrl_group
*grp
= &pctl
->groups
[n
];
547 for (s
= 0; s
< grp
->num_settings
; s
++) {
548 struct mvebu_pinctrl_function
*f
;
551 /* skip unsupported settings on this variant */
553 !(pctl
->variant
& grp
->settings
[s
].variant
))
556 f
= mvebu_pinctrl_find_function_by_name(pctl
,
557 grp
->settings
[s
].name
);
559 /* allocate group name array if not done already */
561 f
->groups
= devm_kzalloc(&pdev
->dev
,
562 f
->num_groups
* sizeof(char *),
568 /* find next free group name and assign current name */
579 int mvebu_pinctrl_probe(struct platform_device
*pdev
)
581 struct mvebu_pinctrl_soc_info
*soc
= dev_get_platdata(&pdev
->dev
);
582 struct device_node
*np
= pdev
->dev
.of_node
;
583 struct mvebu_pinctrl
*pctl
;
585 struct pinctrl_pin_desc
*pdesc
;
589 if (!soc
|| !soc
->controls
|| !soc
->modes
) {
590 dev_err(&pdev
->dev
, "wrong pinctrl soc info\n");
594 base
= of_iomap(np
, 0);
596 dev_err(&pdev
->dev
, "unable to get base address\n");
600 pctl
= devm_kzalloc(&pdev
->dev
, sizeof(struct mvebu_pinctrl
),
603 dev_err(&pdev
->dev
, "unable to alloc driver\n");
607 pctl
->desc
.name
= dev_name(&pdev
->dev
);
608 pctl
->desc
.owner
= THIS_MODULE
;
609 pctl
->desc
.pctlops
= &mvebu_pinctrl_ops
;
610 pctl
->desc
.pmxops
= &mvebu_pinmux_ops
;
611 pctl
->desc
.confops
= &mvebu_pinconf_ops
;
612 pctl
->variant
= soc
->variant
;
614 pctl
->dev
= &pdev
->dev
;
615 platform_set_drvdata(pdev
, pctl
);
617 /* count controls and create names for mvebu generic
618 register controls; also does sanity checks */
619 pctl
->num_groups
= 0;
620 pctl
->desc
.npins
= 0;
621 for (n
= 0; n
< soc
->ncontrols
; n
++) {
622 struct mvebu_mpp_ctrl
*ctrl
= &soc
->controls
[n
];
625 pctl
->desc
.npins
+= ctrl
->npins
;
626 /* initial control pins */
627 for (k
= 0; k
< ctrl
->npins
; k
++)
628 ctrl
->pins
[k
] = ctrl
->pid
+ k
;
630 /* special soc specific control */
631 if (ctrl
->mpp_get
|| ctrl
->mpp_set
) {
632 if (!ctrl
->name
|| !ctrl
->mpp_get
|| !ctrl
->mpp_set
) {
633 dev_err(&pdev
->dev
, "wrong soc control info\n");
636 pctl
->num_groups
+= 1;
640 /* generic mvebu register control */
641 names
= devm_kzalloc(&pdev
->dev
, ctrl
->npins
* 8, GFP_KERNEL
);
643 dev_err(&pdev
->dev
, "failed to alloc mpp names\n");
646 for (k
= 0; k
< ctrl
->npins
; k
++)
647 sprintf(names
+ 8*k
, "mpp%d", ctrl
->pid
+k
);
649 pctl
->num_groups
+= ctrl
->npins
;
652 pdesc
= devm_kzalloc(&pdev
->dev
, pctl
->desc
.npins
*
653 sizeof(struct pinctrl_pin_desc
), GFP_KERNEL
);
655 dev_err(&pdev
->dev
, "failed to alloc pinctrl pins\n");
659 for (n
= 0; n
< pctl
->desc
.npins
; n
++)
661 pctl
->desc
.pins
= pdesc
;
663 pctl
->groups
= devm_kzalloc(&pdev
->dev
, pctl
->num_groups
*
664 sizeof(struct mvebu_pinctrl_group
), GFP_KERNEL
);
666 dev_err(&pdev
->dev
, "failed to alloc pinctrl groups\n");
670 /* assign mpp controls to groups */
672 for (n
= 0; n
< soc
->ncontrols
; n
++) {
673 struct mvebu_mpp_ctrl
*ctrl
= &soc
->controls
[n
];
674 pctl
->groups
[gid
].gid
= gid
;
675 pctl
->groups
[gid
].ctrl
= ctrl
;
676 pctl
->groups
[gid
].name
= ctrl
->name
;
677 pctl
->groups
[gid
].pins
= ctrl
->pins
;
678 pctl
->groups
[gid
].npins
= ctrl
->npins
;
680 /* generic mvebu register control maps to a number of groups */
681 if (!ctrl
->mpp_get
&& !ctrl
->mpp_set
) {
682 pctl
->groups
[gid
].npins
= 1;
684 for (k
= 1; k
< ctrl
->npins
; k
++) {
686 pctl
->groups
[gid
].gid
= gid
;
687 pctl
->groups
[gid
].ctrl
= ctrl
;
688 pctl
->groups
[gid
].name
= &ctrl
->name
[8*k
];
689 pctl
->groups
[gid
].pins
= &ctrl
->pins
[k
];
690 pctl
->groups
[gid
].npins
= 1;
696 /* assign mpp modes to groups */
697 for (n
= 0; n
< soc
->nmodes
; n
++) {
698 struct mvebu_mpp_mode
*mode
= &soc
->modes
[n
];
699 struct mvebu_pinctrl_group
*grp
=
700 mvebu_pinctrl_find_group_by_pid(pctl
, mode
->pid
);
701 unsigned num_settings
;
704 dev_warn(&pdev
->dev
, "unknown pinctrl group %d\n",
709 for (num_settings
= 0; ;) {
710 struct mvebu_mpp_ctrl_setting
*set
=
711 &mode
->settings
[num_settings
];
717 /* skip unsupported settings for this variant */
718 if (pctl
->variant
&& !(pctl
->variant
& set
->variant
))
721 /* find gpio/gpo/gpi settings */
722 if (strcmp(set
->name
, "gpio") == 0)
723 set
->flags
= MVEBU_SETTING_GPI
|
725 else if (strcmp(set
->name
, "gpo") == 0)
726 set
->flags
= MVEBU_SETTING_GPO
;
727 else if (strcmp(set
->name
, "gpi") == 0)
728 set
->flags
= MVEBU_SETTING_GPI
;
731 grp
->settings
= mode
->settings
;
732 grp
->num_settings
= num_settings
;
735 ret
= mvebu_pinctrl_build_functions(pdev
, pctl
);
737 dev_err(&pdev
->dev
, "unable to build functions\n");
741 pctl
->pctldev
= pinctrl_register(&pctl
->desc
, &pdev
->dev
, pctl
);
742 if (!pctl
->pctldev
) {
743 dev_err(&pdev
->dev
, "unable to register pinctrl driver\n");
747 dev_info(&pdev
->dev
, "registered pinctrl driver\n");
749 /* register gpio ranges */
750 for (n
= 0; n
< soc
->ngpioranges
; n
++)
751 pinctrl_add_gpio_range(pctl
->pctldev
, &soc
->gpioranges
[n
]);
756 int mvebu_pinctrl_remove(struct platform_device
*pdev
)
758 struct mvebu_pinctrl
*pctl
= platform_get_drvdata(pdev
);
759 pinctrl_unregister(pctl
->pctldev
);