2 * Core driver for the imx pin controller
4 * Copyright (C) 2012 Freescale Semiconductor, Inc.
5 * Copyright (C) 2012 Linaro Ltd.
7 * Author: Dong Aisheng <dong.aisheng@linaro.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/err.h>
16 #include <linux/init.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/pinctrl/machine.h>
22 #include <linux/pinctrl/pinconf.h>
23 #include <linux/pinctrl/pinctrl.h>
24 #include <linux/pinctrl/pinmux.h>
25 #include <linux/slab.h>
28 #include "pinctrl-imx.h"
30 /* The bits in CONFIG cell defined in binding doc*/
31 #define IMX_NO_PAD_CTL 0x80000000 /* no pin config need */
32 #define IMX_PAD_SION 0x40000000 /* set SION */
35 * @dev: a pointer back to containing device
36 * @base: the offset to the controller in virtual memory
40 struct pinctrl_dev
*pctl
;
42 const struct imx_pinctrl_soc_info
*info
;
45 static const inline struct imx_pin_group
*imx_pinctrl_find_group_by_name(
46 const struct imx_pinctrl_soc_info
*info
,
49 const struct imx_pin_group
*grp
= NULL
;
52 for (i
= 0; i
< info
->ngroups
; i
++) {
53 if (!strcmp(info
->groups
[i
].name
, name
)) {
54 grp
= &info
->groups
[i
];
62 static int imx_get_groups_count(struct pinctrl_dev
*pctldev
)
64 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
65 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
70 static const char *imx_get_group_name(struct pinctrl_dev
*pctldev
,
73 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
74 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
76 return info
->groups
[selector
].name
;
79 static int imx_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned selector
,
80 const unsigned **pins
,
83 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
84 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
86 if (selector
>= info
->ngroups
)
89 *pins
= info
->groups
[selector
].pin_ids
;
90 *npins
= info
->groups
[selector
].npins
;
95 static void imx_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
98 seq_printf(s
, "%s", dev_name(pctldev
->dev
));
101 static int imx_dt_node_to_map(struct pinctrl_dev
*pctldev
,
102 struct device_node
*np
,
103 struct pinctrl_map
**map
, unsigned *num_maps
)
105 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
106 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
107 const struct imx_pin_group
*grp
;
108 struct pinctrl_map
*new_map
;
109 struct device_node
*parent
;
114 * first find the group of this node and check if we need create
115 * config maps for pins
117 grp
= imx_pinctrl_find_group_by_name(info
, np
->name
);
119 dev_err(info
->dev
, "unable to find group for node %s\n",
124 for (i
= 0; i
< grp
->npins
; i
++) {
125 if (!(grp
->pins
[i
].config
& IMX_NO_PAD_CTL
))
129 new_map
= kmalloc(sizeof(struct pinctrl_map
) * map_num
, GFP_KERNEL
);
137 parent
= of_get_parent(np
);
142 new_map
[0].type
= PIN_MAP_TYPE_MUX_GROUP
;
143 new_map
[0].data
.mux
.function
= parent
->name
;
144 new_map
[0].data
.mux
.group
= np
->name
;
147 /* create config map */
149 for (i
= j
= 0; i
< grp
->npins
; i
++) {
150 if (!(grp
->pins
[i
].config
& IMX_NO_PAD_CTL
)) {
151 new_map
[j
].type
= PIN_MAP_TYPE_CONFIGS_PIN
;
152 new_map
[j
].data
.configs
.group_or_pin
=
153 pin_get_name(pctldev
, grp
->pins
[i
].pin
);
154 new_map
[j
].data
.configs
.configs
= &grp
->pins
[i
].config
;
155 new_map
[j
].data
.configs
.num_configs
= 1;
160 dev_dbg(pctldev
->dev
, "maps: function %s group %s num %d\n",
161 (*map
)->data
.mux
.function
, (*map
)->data
.mux
.group
, map_num
);
166 static void imx_dt_free_map(struct pinctrl_dev
*pctldev
,
167 struct pinctrl_map
*map
, unsigned num_maps
)
172 static const struct pinctrl_ops imx_pctrl_ops
= {
173 .get_groups_count
= imx_get_groups_count
,
174 .get_group_name
= imx_get_group_name
,
175 .get_group_pins
= imx_get_group_pins
,
176 .pin_dbg_show
= imx_pin_dbg_show
,
177 .dt_node_to_map
= imx_dt_node_to_map
,
178 .dt_free_map
= imx_dt_free_map
,
182 static int imx_pmx_set(struct pinctrl_dev
*pctldev
, unsigned selector
,
185 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
186 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
187 const struct imx_pin_reg
*pin_reg
;
188 unsigned int npins
, pin_id
;
190 struct imx_pin_group
*grp
;
193 * Configure the mux mode for each pin in the group for a specific
196 grp
= &info
->groups
[group
];
199 dev_dbg(ipctl
->dev
, "enable function %s group %s\n",
200 info
->functions
[selector
].name
, grp
->name
);
202 for (i
= 0; i
< npins
; i
++) {
203 struct imx_pin
*pin
= &grp
->pins
[i
];
205 pin_reg
= &info
->pin_regs
[pin_id
];
207 if (pin_reg
->mux_reg
== -1) {
208 dev_err(ipctl
->dev
, "Pin(%s) does not support mux function\n",
209 info
->pins
[pin_id
].name
);
213 if (info
->flags
& SHARE_MUX_CONF_REG
) {
215 reg
= readl(ipctl
->base
+ pin_reg
->mux_reg
);
217 reg
|= (pin
->mux_mode
<< 20);
218 writel(reg
, ipctl
->base
+ pin_reg
->mux_reg
);
220 writel(pin
->mux_mode
, ipctl
->base
+ pin_reg
->mux_reg
);
222 dev_dbg(ipctl
->dev
, "write: offset 0x%x val 0x%x\n",
223 pin_reg
->mux_reg
, pin
->mux_mode
);
226 * If the select input value begins with 0xff, it's a quirky
227 * select input and the value should be interpreted as below.
229 * | 0xff | shift | width | select |
230 * It's used to work around the problem that the select
231 * input for some pin is not implemented in the select
232 * input register but in some general purpose register.
233 * We encode the select input value, width and shift of
234 * the bit field into input_val cell of pin function ID
235 * in device tree, and then decode them here for setting
236 * up the select input bits in general purpose register.
238 if (pin
->input_val
>> 24 == 0xff) {
239 u32 val
= pin
->input_val
;
240 u8 select
= val
& 0xff;
241 u8 width
= (val
>> 8) & 0xff;
242 u8 shift
= (val
>> 16) & 0xff;
243 u32 mask
= ((1 << width
) - 1) << shift
;
245 * The input_reg[i] here is actually some IOMUXC general
246 * purpose register, not regular select input register.
248 val
= readl(ipctl
->base
+ pin
->input_reg
);
250 val
|= select
<< shift
;
251 writel(val
, ipctl
->base
+ pin
->input_reg
);
252 } else if (pin
->input_reg
) {
254 * Regular select input register can never be at offset
255 * 0, and we only print register value for regular case.
257 writel(pin
->input_val
, ipctl
->base
+ pin
->input_reg
);
259 "==>select_input: offset 0x%x val 0x%x\n",
260 pin
->input_reg
, pin
->input_val
);
267 static int imx_pmx_get_funcs_count(struct pinctrl_dev
*pctldev
)
269 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
270 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
272 return info
->nfunctions
;
275 static const char *imx_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
278 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
279 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
281 return info
->functions
[selector
].name
;
284 static int imx_pmx_get_groups(struct pinctrl_dev
*pctldev
, unsigned selector
,
285 const char * const **groups
,
286 unsigned * const num_groups
)
288 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
289 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
291 *groups
= info
->functions
[selector
].groups
;
292 *num_groups
= info
->functions
[selector
].num_groups
;
297 static int imx_pmx_gpio_request_enable(struct pinctrl_dev
*pctldev
,
298 struct pinctrl_gpio_range
*range
, unsigned offset
)
300 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
301 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
302 const struct imx_pin_reg
*pin_reg
;
303 struct imx_pin_group
*grp
;
304 struct imx_pin
*imx_pin
;
305 unsigned int pin
, group
;
308 /* Currently implementation only for shared mux/conf register */
309 if (!(info
->flags
& SHARE_MUX_CONF_REG
))
312 pin_reg
= &info
->pin_regs
[offset
];
313 if (pin_reg
->mux_reg
== -1)
316 /* Find the pinctrl config with GPIO mux mode for the requested pin */
317 for (group
= 0; group
< info
->ngroups
; group
++) {
318 grp
= &info
->groups
[group
];
319 for (pin
= 0; pin
< grp
->npins
; pin
++) {
320 imx_pin
= &grp
->pins
[pin
];
321 if (imx_pin
->pin
== offset
&& !imx_pin
->mux_mode
)
329 reg
= readl(ipctl
->base
+ pin_reg
->mux_reg
);
331 reg
|= imx_pin
->config
;
332 writel(reg
, ipctl
->base
+ pin_reg
->mux_reg
);
337 static int imx_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
338 struct pinctrl_gpio_range
*range
, unsigned offset
, bool input
)
340 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
341 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
342 const struct imx_pin_reg
*pin_reg
;
346 * Only Vybrid has the input/output buffer enable flags (IBE/OBE)
347 * They are part of the shared mux/conf register.
349 if (!(info
->flags
& SHARE_MUX_CONF_REG
))
352 pin_reg
= &info
->pin_regs
[offset
];
353 if (pin_reg
->mux_reg
== -1)
356 /* IBE always enabled allows us to read the value "on the wire" */
357 reg
= readl(ipctl
->base
+ pin_reg
->mux_reg
);
362 writel(reg
, ipctl
->base
+ pin_reg
->mux_reg
);
367 static const struct pinmux_ops imx_pmx_ops
= {
368 .get_functions_count
= imx_pmx_get_funcs_count
,
369 .get_function_name
= imx_pmx_get_func_name
,
370 .get_function_groups
= imx_pmx_get_groups
,
371 .set_mux
= imx_pmx_set
,
372 .gpio_request_enable
= imx_pmx_gpio_request_enable
,
373 .gpio_set_direction
= imx_pmx_gpio_set_direction
,
376 static int imx_pinconf_get(struct pinctrl_dev
*pctldev
,
377 unsigned pin_id
, unsigned long *config
)
379 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
380 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
381 const struct imx_pin_reg
*pin_reg
= &info
->pin_regs
[pin_id
];
383 if (pin_reg
->conf_reg
== -1) {
384 dev_err(info
->dev
, "Pin(%s) does not support config function\n",
385 info
->pins
[pin_id
].name
);
389 *config
= readl(ipctl
->base
+ pin_reg
->conf_reg
);
391 if (info
->flags
& SHARE_MUX_CONF_REG
)
397 static int imx_pinconf_set(struct pinctrl_dev
*pctldev
,
398 unsigned pin_id
, unsigned long *configs
,
399 unsigned num_configs
)
401 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
402 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
403 const struct imx_pin_reg
*pin_reg
= &info
->pin_regs
[pin_id
];
406 if (pin_reg
->conf_reg
== -1) {
407 dev_err(info
->dev
, "Pin(%s) does not support config function\n",
408 info
->pins
[pin_id
].name
);
412 dev_dbg(ipctl
->dev
, "pinconf set pin %s\n",
413 info
->pins
[pin_id
].name
);
415 for (i
= 0; i
< num_configs
; i
++) {
416 if (info
->flags
& SHARE_MUX_CONF_REG
) {
418 reg
= readl(ipctl
->base
+ pin_reg
->conf_reg
);
421 writel(reg
, ipctl
->base
+ pin_reg
->conf_reg
);
423 writel(configs
[i
], ipctl
->base
+ pin_reg
->conf_reg
);
425 dev_dbg(ipctl
->dev
, "write: offset 0x%x val 0x%lx\n",
426 pin_reg
->conf_reg
, configs
[i
]);
427 } /* for each config */
432 static void imx_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
433 struct seq_file
*s
, unsigned pin_id
)
435 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
436 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
437 const struct imx_pin_reg
*pin_reg
= &info
->pin_regs
[pin_id
];
438 unsigned long config
;
440 if (!pin_reg
|| pin_reg
->conf_reg
== -1) {
441 seq_printf(s
, "N/A");
445 config
= readl(ipctl
->base
+ pin_reg
->conf_reg
);
446 seq_printf(s
, "0x%lx", config
);
449 static void imx_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
450 struct seq_file
*s
, unsigned group
)
452 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
453 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
454 struct imx_pin_group
*grp
;
455 unsigned long config
;
459 if (group
> info
->ngroups
)
463 grp
= &info
->groups
[group
];
464 for (i
= 0; i
< grp
->npins
; i
++) {
465 struct imx_pin
*pin
= &grp
->pins
[i
];
466 name
= pin_get_name(pctldev
, pin
->pin
);
467 ret
= imx_pinconf_get(pctldev
, pin
->pin
, &config
);
470 seq_printf(s
, "%s: 0x%lx", name
, config
);
474 static const struct pinconf_ops imx_pinconf_ops
= {
475 .pin_config_get
= imx_pinconf_get
,
476 .pin_config_set
= imx_pinconf_set
,
477 .pin_config_dbg_show
= imx_pinconf_dbg_show
,
478 .pin_config_group_dbg_show
= imx_pinconf_group_dbg_show
,
481 static struct pinctrl_desc imx_pinctrl_desc
= {
482 .pctlops
= &imx_pctrl_ops
,
483 .pmxops
= &imx_pmx_ops
,
484 .confops
= &imx_pinconf_ops
,
485 .owner
= THIS_MODULE
,
489 * Each pin represented in fsl,pins consists of 5 u32 PIN_FUNC_ID and
490 * 1 u32 CONFIG, so 24 types in total for each pin.
492 #define FSL_PIN_SIZE 24
493 #define SHARE_FSL_PIN_SIZE 20
495 static int imx_pinctrl_parse_groups(struct device_node
*np
,
496 struct imx_pin_group
*grp
,
497 struct imx_pinctrl_soc_info
*info
,
505 dev_dbg(info
->dev
, "group(%d): %s\n", index
, np
->name
);
507 if (info
->flags
& SHARE_MUX_CONF_REG
)
508 pin_size
= SHARE_FSL_PIN_SIZE
;
510 pin_size
= FSL_PIN_SIZE
;
511 /* Initialise group */
512 grp
->name
= np
->name
;
515 * the binding format is fsl,pins = <PIN_FUNC_ID CONFIG ...>,
516 * do sanity check and calculate pins number
518 list
= of_get_property(np
, "fsl,pins", &size
);
520 dev_err(info
->dev
, "no fsl,pins property in node %s\n", np
->full_name
);
524 /* we do not check return since it's safe node passed down */
525 if (!size
|| size
% pin_size
) {
526 dev_err(info
->dev
, "Invalid fsl,pins property in node %s\n", np
->full_name
);
530 grp
->npins
= size
/ pin_size
;
531 grp
->pins
= devm_kzalloc(info
->dev
, grp
->npins
* sizeof(struct imx_pin
),
533 grp
->pin_ids
= devm_kzalloc(info
->dev
, grp
->npins
* sizeof(unsigned int),
535 if (!grp
->pins
|| ! grp
->pin_ids
)
538 for (i
= 0; i
< grp
->npins
; i
++) {
539 u32 mux_reg
= be32_to_cpu(*list
++);
542 struct imx_pin_reg
*pin_reg
;
543 struct imx_pin
*pin
= &grp
->pins
[i
];
545 if (info
->flags
& SHARE_MUX_CONF_REG
) {
548 conf_reg
= be32_to_cpu(*list
++);
553 pin_id
= mux_reg
? mux_reg
/ 4 : conf_reg
/ 4;
554 pin_reg
= &info
->pin_regs
[pin_id
];
556 grp
->pin_ids
[i
] = pin_id
;
557 pin_reg
->mux_reg
= mux_reg
;
558 pin_reg
->conf_reg
= conf_reg
;
559 pin
->input_reg
= be32_to_cpu(*list
++);
560 pin
->mux_mode
= be32_to_cpu(*list
++);
561 pin
->input_val
= be32_to_cpu(*list
++);
563 /* SION bit is in mux register */
564 config
= be32_to_cpu(*list
++);
565 if (config
& IMX_PAD_SION
)
566 pin
->mux_mode
|= IOMUXC_CONFIG_SION
;
567 pin
->config
= config
& ~IMX_PAD_SION
;
569 dev_dbg(info
->dev
, "%s: 0x%x 0x%08lx", info
->pins
[pin_id
].name
,
570 pin
->mux_mode
, pin
->config
);
576 static int imx_pinctrl_parse_functions(struct device_node
*np
,
577 struct imx_pinctrl_soc_info
*info
,
580 struct device_node
*child
;
581 struct imx_pmx_func
*func
;
582 struct imx_pin_group
*grp
;
583 static u32 grp_index
;
586 dev_dbg(info
->dev
, "parse function(%d): %s\n", index
, np
->name
);
588 func
= &info
->functions
[index
];
590 /* Initialise function */
591 func
->name
= np
->name
;
592 func
->num_groups
= of_get_child_count(np
);
593 if (func
->num_groups
== 0) {
594 dev_err(info
->dev
, "no groups defined in %s\n", np
->full_name
);
597 func
->groups
= devm_kzalloc(info
->dev
,
598 func
->num_groups
* sizeof(char *), GFP_KERNEL
);
600 for_each_child_of_node(np
, child
) {
601 func
->groups
[i
] = child
->name
;
602 grp
= &info
->groups
[grp_index
++];
603 imx_pinctrl_parse_groups(child
, grp
, info
, i
++);
609 static int imx_pinctrl_probe_dt(struct platform_device
*pdev
,
610 struct imx_pinctrl_soc_info
*info
)
612 struct device_node
*np
= pdev
->dev
.of_node
;
613 struct device_node
*child
;
620 nfuncs
= of_get_child_count(np
);
622 dev_err(&pdev
->dev
, "no functions defined\n");
626 info
->nfunctions
= nfuncs
;
627 info
->functions
= devm_kzalloc(&pdev
->dev
, nfuncs
* sizeof(struct imx_pmx_func
),
629 if (!info
->functions
)
633 for_each_child_of_node(np
, child
)
634 info
->ngroups
+= of_get_child_count(child
);
635 info
->groups
= devm_kzalloc(&pdev
->dev
, info
->ngroups
* sizeof(struct imx_pin_group
),
640 for_each_child_of_node(np
, child
)
641 imx_pinctrl_parse_functions(child
, info
, i
++);
646 int imx_pinctrl_probe(struct platform_device
*pdev
,
647 struct imx_pinctrl_soc_info
*info
)
649 struct imx_pinctrl
*ipctl
;
650 struct resource
*res
;
653 if (!info
|| !info
->pins
|| !info
->npins
) {
654 dev_err(&pdev
->dev
, "wrong pinctrl info\n");
657 info
->dev
= &pdev
->dev
;
659 /* Create state holders etc for this driver */
660 ipctl
= devm_kzalloc(&pdev
->dev
, sizeof(*ipctl
), GFP_KERNEL
);
664 info
->pin_regs
= devm_kmalloc(&pdev
->dev
, sizeof(*info
->pin_regs
) *
665 info
->npins
, GFP_KERNEL
);
669 for (i
= 0; i
< info
->npins
; i
++) {
670 info
->pin_regs
[i
].mux_reg
= -1;
671 info
->pin_regs
[i
].conf_reg
= -1;
674 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
675 ipctl
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
676 if (IS_ERR(ipctl
->base
))
677 return PTR_ERR(ipctl
->base
);
679 imx_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
680 imx_pinctrl_desc
.pins
= info
->pins
;
681 imx_pinctrl_desc
.npins
= info
->npins
;
683 ret
= imx_pinctrl_probe_dt(pdev
, info
);
685 dev_err(&pdev
->dev
, "fail to probe dt properties\n");
690 ipctl
->dev
= info
->dev
;
691 platform_set_drvdata(pdev
, ipctl
);
692 ipctl
->pctl
= pinctrl_register(&imx_pinctrl_desc
, &pdev
->dev
, ipctl
);
694 dev_err(&pdev
->dev
, "could not register IMX pinctrl driver\n");
698 dev_info(&pdev
->dev
, "initialized IMX pinctrl driver\n");
703 int imx_pinctrl_remove(struct platform_device
*pdev
)
705 struct imx_pinctrl
*ipctl
= platform_get_drvdata(pdev
);
707 pinctrl_unregister(ipctl
->pctl
);