1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright 2012 Freescale Semiconductor, Inc.
6 #include <linux/init.h>
9 #include <linux/of_address.h>
10 #include <linux/pinctrl/machine.h>
11 #include <linux/pinctrl/pinconf.h>
12 #include <linux/pinctrl/pinctrl.h>
13 #include <linux/pinctrl/pinmux.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
17 #include "pinctrl-mxs.h"
21 struct mxs_pinctrl_data
{
23 struct pinctrl_dev
*pctl
;
25 struct mxs_pinctrl_soc_data
*soc
;
28 static int mxs_get_groups_count(struct pinctrl_dev
*pctldev
)
30 struct mxs_pinctrl_data
*d
= pinctrl_dev_get_drvdata(pctldev
);
32 return d
->soc
->ngroups
;
35 static const char *mxs_get_group_name(struct pinctrl_dev
*pctldev
,
38 struct mxs_pinctrl_data
*d
= pinctrl_dev_get_drvdata(pctldev
);
40 return d
->soc
->groups
[group
].name
;
43 static int mxs_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned group
,
44 const unsigned **pins
, unsigned *num_pins
)
46 struct mxs_pinctrl_data
*d
= pinctrl_dev_get_drvdata(pctldev
);
48 *pins
= d
->soc
->groups
[group
].pins
;
49 *num_pins
= d
->soc
->groups
[group
].npins
;
54 static void mxs_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
57 seq_printf(s
, " %s", dev_name(pctldev
->dev
));
60 static int mxs_dt_node_to_map(struct pinctrl_dev
*pctldev
,
61 struct device_node
*np
,
62 struct pinctrl_map
**map
, unsigned *num_maps
)
64 struct pinctrl_map
*new_map
;
67 unsigned long config
= 0;
68 unsigned long *pconfig
;
69 int length
= strlen(np
->name
) + SUFFIX_LEN
;
74 /* Check for pin config node which has no 'reg' property */
75 if (of_property_read_u32(np
, "reg", ®
))
78 ret
= of_property_read_u32(np
, "fsl,drive-strength", &val
);
80 config
= val
| MA_PRESENT
;
81 ret
= of_property_read_u32(np
, "fsl,voltage", &val
);
83 config
|= val
<< VOL_SHIFT
| VOL_PRESENT
;
84 ret
= of_property_read_u32(np
, "fsl,pull-up", &val
);
86 config
|= val
<< PULL_SHIFT
| PULL_PRESENT
;
88 /* Check for group node which has both mux and config settings */
89 if (!purecfg
&& config
)
92 new_map
= kcalloc(new_num
, sizeof(*new_map
), GFP_KERNEL
);
97 new_map
[i
].type
= PIN_MAP_TYPE_MUX_GROUP
;
98 new_map
[i
].data
.mux
.function
= np
->name
;
100 /* Compose group name */
101 group
= kzalloc(length
, GFP_KERNEL
);
106 snprintf(group
, length
, "%s.%d", np
->name
, reg
);
107 new_map
[i
].data
.mux
.group
= group
;
112 pconfig
= kmemdup(&config
, sizeof(config
), GFP_KERNEL
);
118 new_map
[i
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
119 new_map
[i
].data
.configs
.group_or_pin
= purecfg
? np
->name
:
121 new_map
[i
].data
.configs
.configs
= pconfig
;
122 new_map
[i
].data
.configs
.num_configs
= 1;
138 static void mxs_dt_free_map(struct pinctrl_dev
*pctldev
,
139 struct pinctrl_map
*map
, unsigned num_maps
)
143 for (i
= 0; i
< num_maps
; i
++) {
144 if (map
[i
].type
== PIN_MAP_TYPE_MUX_GROUP
)
145 kfree(map
[i
].data
.mux
.group
);
146 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
)
147 kfree(map
[i
].data
.configs
.configs
);
153 static const struct pinctrl_ops mxs_pinctrl_ops
= {
154 .get_groups_count
= mxs_get_groups_count
,
155 .get_group_name
= mxs_get_group_name
,
156 .get_group_pins
= mxs_get_group_pins
,
157 .pin_dbg_show
= mxs_pin_dbg_show
,
158 .dt_node_to_map
= mxs_dt_node_to_map
,
159 .dt_free_map
= mxs_dt_free_map
,
162 static int mxs_pinctrl_get_funcs_count(struct pinctrl_dev
*pctldev
)
164 struct mxs_pinctrl_data
*d
= pinctrl_dev_get_drvdata(pctldev
);
166 return d
->soc
->nfunctions
;
169 static const char *mxs_pinctrl_get_func_name(struct pinctrl_dev
*pctldev
,
172 struct mxs_pinctrl_data
*d
= pinctrl_dev_get_drvdata(pctldev
);
174 return d
->soc
->functions
[function
].name
;
177 static int mxs_pinctrl_get_func_groups(struct pinctrl_dev
*pctldev
,
179 const char * const **groups
,
180 unsigned * const num_groups
)
182 struct mxs_pinctrl_data
*d
= pinctrl_dev_get_drvdata(pctldev
);
184 *groups
= d
->soc
->functions
[group
].groups
;
185 *num_groups
= d
->soc
->functions
[group
].ngroups
;
190 static void mxs_pinctrl_rmwl(u32 value
, u32 mask
, u8 shift
, void __iomem
*reg
)
195 tmp
&= ~(mask
<< shift
);
196 tmp
|= value
<< shift
;
200 static int mxs_pinctrl_set_mux(struct pinctrl_dev
*pctldev
, unsigned selector
,
203 struct mxs_pinctrl_data
*d
= pinctrl_dev_get_drvdata(pctldev
);
204 struct mxs_group
*g
= &d
->soc
->groups
[group
];
210 for (i
= 0; i
< g
->npins
; i
++) {
211 bank
= PINID_TO_BANK(g
->pins
[i
]);
212 pin
= PINID_TO_PIN(g
->pins
[i
]);
213 reg
= d
->base
+ d
->soc
->regs
->muxsel
;
214 reg
+= bank
* 0x20 + pin
/ 16 * 0x10;
215 shift
= pin
% 16 * 2;
217 mxs_pinctrl_rmwl(g
->muxsel
[i
], 0x3, shift
, reg
);
223 static const struct pinmux_ops mxs_pinmux_ops
= {
224 .get_functions_count
= mxs_pinctrl_get_funcs_count
,
225 .get_function_name
= mxs_pinctrl_get_func_name
,
226 .get_function_groups
= mxs_pinctrl_get_func_groups
,
227 .set_mux
= mxs_pinctrl_set_mux
,
230 static int mxs_pinconf_get(struct pinctrl_dev
*pctldev
,
231 unsigned pin
, unsigned long *config
)
236 static int mxs_pinconf_set(struct pinctrl_dev
*pctldev
,
237 unsigned pin
, unsigned long *configs
,
238 unsigned num_configs
)
243 static int mxs_pinconf_group_get(struct pinctrl_dev
*pctldev
,
244 unsigned group
, unsigned long *config
)
246 struct mxs_pinctrl_data
*d
= pinctrl_dev_get_drvdata(pctldev
);
248 *config
= d
->soc
->groups
[group
].config
;
253 static int mxs_pinconf_group_set(struct pinctrl_dev
*pctldev
,
254 unsigned group
, unsigned long *configs
,
255 unsigned num_configs
)
257 struct mxs_pinctrl_data
*d
= pinctrl_dev_get_drvdata(pctldev
);
258 struct mxs_group
*g
= &d
->soc
->groups
[group
];
260 u8 ma
, vol
, pull
, bank
, shift
;
264 unsigned long config
;
266 for (n
= 0; n
< num_configs
; n
++) {
269 ma
= CONFIG_TO_MA(config
);
270 vol
= CONFIG_TO_VOL(config
);
271 pull
= CONFIG_TO_PULL(config
);
273 for (i
= 0; i
< g
->npins
; i
++) {
274 bank
= PINID_TO_BANK(g
->pins
[i
]);
275 pin
= PINID_TO_PIN(g
->pins
[i
]);
278 reg
= d
->base
+ d
->soc
->regs
->drive
;
279 reg
+= bank
* 0x40 + pin
/ 8 * 0x10;
282 if (config
& MA_PRESENT
) {
284 mxs_pinctrl_rmwl(ma
, 0x3, shift
, reg
);
288 if (config
& VOL_PRESENT
) {
289 shift
= pin
% 8 * 4 + 2;
291 writel(1 << shift
, reg
+ SET
);
293 writel(1 << shift
, reg
+ CLR
);
297 if (config
& PULL_PRESENT
) {
298 reg
= d
->base
+ d
->soc
->regs
->pull
;
302 writel(1 << shift
, reg
+ SET
);
304 writel(1 << shift
, reg
+ CLR
);
308 /* cache the config value for mxs_pinconf_group_get() */
311 } /* for each config */
316 static void mxs_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
317 struct seq_file
*s
, unsigned pin
)
322 static void mxs_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
323 struct seq_file
*s
, unsigned group
)
325 unsigned long config
;
327 if (!mxs_pinconf_group_get(pctldev
, group
, &config
))
328 seq_printf(s
, "0x%lx", config
);
331 static const struct pinconf_ops mxs_pinconf_ops
= {
332 .pin_config_get
= mxs_pinconf_get
,
333 .pin_config_set
= mxs_pinconf_set
,
334 .pin_config_group_get
= mxs_pinconf_group_get
,
335 .pin_config_group_set
= mxs_pinconf_group_set
,
336 .pin_config_dbg_show
= mxs_pinconf_dbg_show
,
337 .pin_config_group_dbg_show
= mxs_pinconf_group_dbg_show
,
340 static struct pinctrl_desc mxs_pinctrl_desc
= {
341 .pctlops
= &mxs_pinctrl_ops
,
342 .pmxops
= &mxs_pinmux_ops
,
343 .confops
= &mxs_pinconf_ops
,
344 .owner
= THIS_MODULE
,
347 static int mxs_pinctrl_parse_group(struct platform_device
*pdev
,
348 struct device_node
*np
, int idx
,
349 const char **out_name
)
351 struct mxs_pinctrl_data
*d
= platform_get_drvdata(pdev
);
352 struct mxs_group
*g
= &d
->soc
->groups
[idx
];
353 struct property
*prop
;
354 const char *propname
= "fsl,pinmux-ids";
356 int length
= strlen(np
->name
) + SUFFIX_LEN
;
359 group
= devm_kzalloc(&pdev
->dev
, length
, GFP_KERNEL
);
362 if (of_property_read_u32(np
, "reg", &val
))
363 snprintf(group
, length
, "%s", np
->name
);
365 snprintf(group
, length
, "%s.%d", np
->name
, val
);
368 prop
= of_find_property(np
, propname
, &length
);
371 g
->npins
= length
/ sizeof(u32
);
373 g
->pins
= devm_kcalloc(&pdev
->dev
, g
->npins
, sizeof(*g
->pins
),
378 g
->muxsel
= devm_kcalloc(&pdev
->dev
, g
->npins
, sizeof(*g
->muxsel
),
383 of_property_read_u32_array(np
, propname
, g
->pins
, g
->npins
);
384 for (i
= 0; i
< g
->npins
; i
++) {
385 g
->muxsel
[i
] = MUXID_TO_MUXSEL(g
->pins
[i
]);
386 g
->pins
[i
] = MUXID_TO_PINID(g
->pins
[i
]);
395 static int mxs_pinctrl_probe_dt(struct platform_device
*pdev
,
396 struct mxs_pinctrl_data
*d
)
398 struct mxs_pinctrl_soc_data
*soc
= d
->soc
;
399 struct device_node
*np
= pdev
->dev
.of_node
;
400 struct device_node
*child
;
401 struct mxs_function
*f
;
402 const char *gpio_compat
= "fsl,mxs-gpio";
403 const char *fn
, *fnull
= "";
404 int i
= 0, idxf
= 0, idxg
= 0;
408 child
= of_get_next_child(np
, NULL
);
410 dev_err(&pdev
->dev
, "no group is defined\n");
414 /* Count total functions and groups */
416 for_each_child_of_node(np
, child
) {
417 if (of_device_is_compatible(child
, gpio_compat
))
420 /* Skip pure pinconf node */
421 if (of_property_read_u32(child
, "reg", &val
))
423 if (strcmp(fn
, child
->name
)) {
429 soc
->functions
= devm_kcalloc(&pdev
->dev
,
431 sizeof(*soc
->functions
),
436 soc
->groups
= devm_kcalloc(&pdev
->dev
,
437 soc
->ngroups
, sizeof(*soc
->groups
),
442 /* Count groups for each function */
444 f
= &soc
->functions
[idxf
];
445 for_each_child_of_node(np
, child
) {
446 if (of_device_is_compatible(child
, gpio_compat
))
448 if (of_property_read_u32(child
, "reg", &val
))
450 if (strcmp(fn
, child
->name
)) {
451 struct device_node
*child2
;
454 * This reference is dropped by
455 * of_get_next_child(np, * child)
460 * The logic parsing the functions from dt currently
461 * doesn't handle if functions with the same name are
462 * not grouped together. Only the first contiguous
463 * cluster is usable for each function name. This is a
464 * bug that is not trivial to fix, but at least warn
467 for (child2
= of_get_next_child(np
, child
);
469 child2
= of_get_next_child(np
, child2
)) {
470 if (!strcmp(child2
->name
, fn
))
472 "function nodes must be grouped by name (failed for: %s)",
476 f
= &soc
->functions
[idxf
++];
477 f
->name
= fn
= child
->name
;
482 /* Get groups for each function */
485 for_each_child_of_node(np
, child
) {
486 if (of_device_is_compatible(child
, gpio_compat
))
488 if (of_property_read_u32(child
, "reg", &val
)) {
489 ret
= mxs_pinctrl_parse_group(pdev
, child
,
498 if (strcmp(fn
, child
->name
)) {
499 f
= &soc
->functions
[idxf
++];
500 f
->groups
= devm_kcalloc(&pdev
->dev
,
511 ret
= mxs_pinctrl_parse_group(pdev
, child
, idxg
++,
522 int mxs_pinctrl_probe(struct platform_device
*pdev
,
523 struct mxs_pinctrl_soc_data
*soc
)
525 struct device_node
*np
= pdev
->dev
.of_node
;
526 struct mxs_pinctrl_data
*d
;
529 d
= devm_kzalloc(&pdev
->dev
, sizeof(*d
), GFP_KERNEL
);
536 d
->base
= of_iomap(np
, 0);
538 return -EADDRNOTAVAIL
;
540 mxs_pinctrl_desc
.pins
= d
->soc
->pins
;
541 mxs_pinctrl_desc
.npins
= d
->soc
->npins
;
542 mxs_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
544 platform_set_drvdata(pdev
, d
);
546 ret
= mxs_pinctrl_probe_dt(pdev
, d
);
548 dev_err(&pdev
->dev
, "dt probe failed: %d\n", ret
);
552 d
->pctl
= pinctrl_register(&mxs_pinctrl_desc
, &pdev
->dev
, d
);
553 if (IS_ERR(d
->pctl
)) {
554 dev_err(&pdev
->dev
, "Couldn't register MXS pinctrl driver\n");
555 ret
= PTR_ERR(d
->pctl
);