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 #define IMX_PMX_DUMP(info, p, m, c, n) \
33 printk(KERN_DEBUG "Format: Pin Mux Config\n"); \
34 for (i = 0; i < n; i++) { \
36 printk(KERN_DEBUG "%s %d 0x%lx\n", \
42 /* The bits in CONFIG cell defined in binding doc*/
43 #define IMX_NO_PAD_CTL 0x80000000 /* no pin config need */
44 #define IMX_PAD_SION 0x40000000 /* set SION */
47 * @dev: a pointer back to containing device
48 * @base: the offset to the controller in virtual memory
52 struct pinctrl_dev
*pctl
;
54 const struct imx_pinctrl_soc_info
*info
;
57 static const struct imx_pin_reg
*imx_find_pin_reg(
58 const struct imx_pinctrl_soc_info
*info
,
59 unsigned pin
, bool is_mux
, unsigned mux
)
61 const struct imx_pin_reg
*pin_reg
= NULL
;
64 for (i
= 0; i
< info
->npin_regs
; i
++) {
65 pin_reg
= &info
->pin_regs
[i
];
66 if (pin_reg
->pid
!= pin
)
70 else if (pin_reg
->mux_mode
== (mux
& IMX_MUX_MASK
))
74 if (i
== info
->npin_regs
) {
75 dev_err(info
->dev
, "Pin(%s): unable to find pin reg map\n",
76 info
->pins
[pin
].name
);
83 static const inline struct imx_pin_group
*imx_pinctrl_find_group_by_name(
84 const struct imx_pinctrl_soc_info
*info
,
87 const struct imx_pin_group
*grp
= NULL
;
90 for (i
= 0; i
< info
->ngroups
; i
++) {
91 if (!strcmp(info
->groups
[i
].name
, name
)) {
92 grp
= &info
->groups
[i
];
100 static int imx_get_groups_count(struct pinctrl_dev
*pctldev
)
102 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
103 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
105 return info
->ngroups
;
108 static const char *imx_get_group_name(struct pinctrl_dev
*pctldev
,
111 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
112 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
114 return info
->groups
[selector
].name
;
117 static int imx_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned selector
,
118 const unsigned **pins
,
121 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
122 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
124 if (selector
>= info
->ngroups
)
127 *pins
= info
->groups
[selector
].pins
;
128 *npins
= info
->groups
[selector
].npins
;
133 static void imx_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
136 seq_printf(s
, "%s", dev_name(pctldev
->dev
));
139 static int imx_dt_node_to_map(struct pinctrl_dev
*pctldev
,
140 struct device_node
*np
,
141 struct pinctrl_map
**map
, unsigned *num_maps
)
143 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
144 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
145 const struct imx_pin_group
*grp
;
146 struct pinctrl_map
*new_map
;
147 struct device_node
*parent
;
152 * first find the group of this node and check if we need create
153 * config maps for pins
155 grp
= imx_pinctrl_find_group_by_name(info
, np
->name
);
157 dev_err(info
->dev
, "unable to find group for node %s\n",
162 for (i
= 0; i
< grp
->npins
; i
++) {
163 if (!(grp
->configs
[i
] & IMX_NO_PAD_CTL
))
167 new_map
= kmalloc(sizeof(struct pinctrl_map
) * map_num
, GFP_KERNEL
);
175 parent
= of_get_parent(np
);
180 new_map
[0].type
= PIN_MAP_TYPE_MUX_GROUP
;
181 new_map
[0].data
.mux
.function
= parent
->name
;
182 new_map
[0].data
.mux
.group
= np
->name
;
185 /* create config map */
187 for (i
= j
= 0; i
< grp
->npins
; i
++) {
188 if (!(grp
->configs
[i
] & IMX_NO_PAD_CTL
)) {
189 new_map
[j
].type
= PIN_MAP_TYPE_CONFIGS_PIN
;
190 new_map
[j
].data
.configs
.group_or_pin
=
191 pin_get_name(pctldev
, grp
->pins
[i
]);
192 new_map
[j
].data
.configs
.configs
= &grp
->configs
[i
];
193 new_map
[j
].data
.configs
.num_configs
= 1;
198 dev_dbg(pctldev
->dev
, "maps: function %s group %s num %d\n",
199 (*map
)->data
.mux
.function
, (*map
)->data
.mux
.group
, map_num
);
204 static void imx_dt_free_map(struct pinctrl_dev
*pctldev
,
205 struct pinctrl_map
*map
, unsigned num_maps
)
210 static struct pinctrl_ops imx_pctrl_ops
= {
211 .get_groups_count
= imx_get_groups_count
,
212 .get_group_name
= imx_get_group_name
,
213 .get_group_pins
= imx_get_group_pins
,
214 .pin_dbg_show
= imx_pin_dbg_show
,
215 .dt_node_to_map
= imx_dt_node_to_map
,
216 .dt_free_map
= imx_dt_free_map
,
220 static int imx_pmx_enable(struct pinctrl_dev
*pctldev
, unsigned selector
,
223 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
224 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
225 const struct imx_pin_reg
*pin_reg
;
226 const unsigned *pins
, *mux
;
227 unsigned int npins
, pin_id
;
231 * Configure the mux mode for each pin in the group for a specific
234 pins
= info
->groups
[group
].pins
;
235 npins
= info
->groups
[group
].npins
;
236 mux
= info
->groups
[group
].mux_mode
;
238 WARN_ON(!pins
|| !npins
|| !mux
);
240 dev_dbg(ipctl
->dev
, "enable function %s group %s\n",
241 info
->functions
[selector
].name
, info
->groups
[group
].name
);
243 for (i
= 0; i
< npins
; i
++) {
246 pin_reg
= imx_find_pin_reg(info
, pin_id
, 1, mux
[i
]);
250 if (!pin_reg
->mux_reg
) {
251 dev_err(ipctl
->dev
, "Pin(%s) does not support mux function\n",
252 info
->pins
[pin_id
].name
);
256 writel(mux
[i
], ipctl
->base
+ pin_reg
->mux_reg
);
257 dev_dbg(ipctl
->dev
, "write: offset 0x%x val 0x%x\n",
258 pin_reg
->mux_reg
, mux
[i
]);
260 /* some pins also need select input setting, set it if found */
261 if (pin_reg
->input_reg
) {
262 writel(pin_reg
->input_val
, ipctl
->base
+ pin_reg
->input_reg
);
264 "==>select_input: offset 0x%x val 0x%x\n",
265 pin_reg
->input_reg
, pin_reg
->input_val
);
272 static int imx_pmx_get_funcs_count(struct pinctrl_dev
*pctldev
)
274 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
275 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
277 return info
->nfunctions
;
280 static const char *imx_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
283 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
284 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
286 return info
->functions
[selector
].name
;
289 static int imx_pmx_get_groups(struct pinctrl_dev
*pctldev
, unsigned selector
,
290 const char * const **groups
,
291 unsigned * const num_groups
)
293 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
294 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
296 *groups
= info
->functions
[selector
].groups
;
297 *num_groups
= info
->functions
[selector
].num_groups
;
302 static struct pinmux_ops imx_pmx_ops
= {
303 .get_functions_count
= imx_pmx_get_funcs_count
,
304 .get_function_name
= imx_pmx_get_func_name
,
305 .get_function_groups
= imx_pmx_get_groups
,
306 .enable
= imx_pmx_enable
,
309 static int imx_pinconf_get(struct pinctrl_dev
*pctldev
,
310 unsigned pin_id
, unsigned long *config
)
312 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
313 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
314 const struct imx_pin_reg
*pin_reg
;
316 pin_reg
= imx_find_pin_reg(info
, pin_id
, 0, 0);
320 if (!pin_reg
->conf_reg
) {
321 dev_err(info
->dev
, "Pin(%s) does not support config function\n",
322 info
->pins
[pin_id
].name
);
326 *config
= readl(ipctl
->base
+ pin_reg
->conf_reg
);
331 static int imx_pinconf_set(struct pinctrl_dev
*pctldev
,
332 unsigned pin_id
, unsigned long config
)
334 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
335 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
336 const struct imx_pin_reg
*pin_reg
;
338 pin_reg
= imx_find_pin_reg(info
, pin_id
, 0, 0);
342 if (!pin_reg
->conf_reg
) {
343 dev_err(info
->dev
, "Pin(%s) does not support config function\n",
344 info
->pins
[pin_id
].name
);
348 dev_dbg(ipctl
->dev
, "pinconf set pin %s\n",
349 info
->pins
[pin_id
].name
);
351 writel(config
, ipctl
->base
+ pin_reg
->conf_reg
);
352 dev_dbg(ipctl
->dev
, "write: offset 0x%x val 0x%lx\n",
353 pin_reg
->conf_reg
, config
);
358 static void imx_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
359 struct seq_file
*s
, unsigned pin_id
)
361 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
362 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
363 const struct imx_pin_reg
*pin_reg
;
364 unsigned long config
;
366 pin_reg
= imx_find_pin_reg(info
, pin_id
, 0, 0);
367 if (!pin_reg
|| !pin_reg
->conf_reg
) {
368 seq_printf(s
, "N/A");
372 config
= readl(ipctl
->base
+ pin_reg
->conf_reg
);
373 seq_printf(s
, "0x%lx", config
);
376 static void imx_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
377 struct seq_file
*s
, unsigned group
)
379 struct imx_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
380 const struct imx_pinctrl_soc_info
*info
= ipctl
->info
;
381 struct imx_pin_group
*grp
;
382 unsigned long config
;
386 if (group
> info
->ngroups
)
390 grp
= &info
->groups
[group
];
391 for (i
= 0; i
< grp
->npins
; i
++) {
392 name
= pin_get_name(pctldev
, grp
->pins
[i
]);
393 ret
= imx_pinconf_get(pctldev
, grp
->pins
[i
], &config
);
396 seq_printf(s
, "%s: 0x%lx", name
, config
);
400 static struct pinconf_ops imx_pinconf_ops
= {
401 .pin_config_get
= imx_pinconf_get
,
402 .pin_config_set
= imx_pinconf_set
,
403 .pin_config_dbg_show
= imx_pinconf_dbg_show
,
404 .pin_config_group_dbg_show
= imx_pinconf_group_dbg_show
,
407 static struct pinctrl_desc imx_pinctrl_desc
= {
408 .pctlops
= &imx_pctrl_ops
,
409 .pmxops
= &imx_pmx_ops
,
410 .confops
= &imx_pinconf_ops
,
411 .owner
= THIS_MODULE
,
414 /* decode pin id and mux from pin function id got from device tree*/
415 static int imx_pinctrl_get_pin_id_and_mux(const struct imx_pinctrl_soc_info
*info
,
416 unsigned int pin_func_id
, unsigned int *pin_id
,
419 if (pin_func_id
> info
->npin_regs
)
422 *pin_id
= info
->pin_regs
[pin_func_id
].pid
;
423 *mux
= info
->pin_regs
[pin_func_id
].mux_mode
;
428 static int imx_pinctrl_parse_groups(struct device_node
*np
,
429 struct imx_pin_group
*grp
,
430 struct imx_pinctrl_soc_info
*info
,
433 unsigned int pin_func_id
;
439 dev_dbg(info
->dev
, "group(%d): %s\n", index
, np
->name
);
441 /* Initialise group */
442 grp
->name
= np
->name
;
445 * the binding format is fsl,pins = <PIN_FUNC_ID CONFIG ...>,
446 * do sanity check and calculate pins number
448 list
= of_get_property(np
, "fsl,pins", &size
);
449 /* we do not check return since it's safe node passed down */
450 size
/= sizeof(*list
);
451 if (!size
|| size
% 2) {
452 dev_err(info
->dev
, "wrong pins number or pins and configs should be pairs\n");
456 grp
->npins
= size
/ 2;
457 grp
->pins
= devm_kzalloc(info
->dev
, grp
->npins
* sizeof(unsigned int),
459 grp
->mux_mode
= devm_kzalloc(info
->dev
, grp
->npins
* sizeof(unsigned int),
461 grp
->configs
= devm_kzalloc(info
->dev
, grp
->npins
* sizeof(unsigned long),
463 for (i
= 0, j
= 0; i
< size
; i
+= 2, j
++) {
464 pin_func_id
= be32_to_cpu(*list
++);
465 ret
= imx_pinctrl_get_pin_id_and_mux(info
, pin_func_id
,
466 &grp
->pins
[j
], &grp
->mux_mode
[j
]);
468 dev_err(info
->dev
, "get invalid pin function id\n");
471 /* SION bit is in mux register */
472 config
= be32_to_cpu(*list
++);
473 if (config
& IMX_PAD_SION
)
474 grp
->mux_mode
[j
] |= IOMUXC_CONFIG_SION
;
475 grp
->configs
[j
] = config
& ~IMX_PAD_SION
;
479 IMX_PMX_DUMP(info
, grp
->pins
, grp
->mux_mode
, grp
->configs
, grp
->npins
);
485 static int imx_pinctrl_parse_functions(struct device_node
*np
,
486 struct imx_pinctrl_soc_info
*info
,
489 struct device_node
*child
;
490 struct imx_pmx_func
*func
;
491 struct imx_pin_group
*grp
;
493 static u32 grp_index
;
496 dev_dbg(info
->dev
, "parse function(%d): %s\n", index
, np
->name
);
498 func
= &info
->functions
[index
];
500 /* Initialise function */
501 func
->name
= np
->name
;
502 func
->num_groups
= of_get_child_count(np
);
503 if (func
->num_groups
<= 0) {
504 dev_err(info
->dev
, "no groups defined\n");
507 func
->groups
= devm_kzalloc(info
->dev
,
508 func
->num_groups
* sizeof(char *), GFP_KERNEL
);
510 for_each_child_of_node(np
, child
) {
511 func
->groups
[i
] = child
->name
;
512 grp
= &info
->groups
[grp_index
++];
513 ret
= imx_pinctrl_parse_groups(child
, grp
, info
, i
++);
521 static int imx_pinctrl_probe_dt(struct platform_device
*pdev
,
522 struct imx_pinctrl_soc_info
*info
)
524 struct device_node
*np
= pdev
->dev
.of_node
;
525 struct device_node
*child
;
533 nfuncs
= of_get_child_count(np
);
535 dev_err(&pdev
->dev
, "no functions defined\n");
539 info
->nfunctions
= nfuncs
;
540 info
->functions
= devm_kzalloc(&pdev
->dev
, nfuncs
* sizeof(struct imx_pmx_func
),
542 if (!info
->functions
)
546 for_each_child_of_node(np
, child
)
547 info
->ngroups
+= of_get_child_count(child
);
548 info
->groups
= devm_kzalloc(&pdev
->dev
, info
->ngroups
* sizeof(struct imx_pin_group
),
553 for_each_child_of_node(np
, child
) {
554 ret
= imx_pinctrl_parse_functions(child
, info
, i
++);
556 dev_err(&pdev
->dev
, "failed to parse function\n");
564 int imx_pinctrl_probe(struct platform_device
*pdev
,
565 struct imx_pinctrl_soc_info
*info
)
567 struct imx_pinctrl
*ipctl
;
568 struct resource
*res
;
571 if (!info
|| !info
->pins
|| !info
->npins
572 || !info
->pin_regs
|| !info
->npin_regs
) {
573 dev_err(&pdev
->dev
, "wrong pinctrl info\n");
576 info
->dev
= &pdev
->dev
;
578 /* Create state holders etc for this driver */
579 ipctl
= devm_kzalloc(&pdev
->dev
, sizeof(*ipctl
), GFP_KERNEL
);
583 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
587 ipctl
->base
= devm_request_and_ioremap(&pdev
->dev
, res
);
591 imx_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
592 imx_pinctrl_desc
.pins
= info
->pins
;
593 imx_pinctrl_desc
.npins
= info
->npins
;
595 ret
= imx_pinctrl_probe_dt(pdev
, info
);
597 dev_err(&pdev
->dev
, "fail to probe dt properties\n");
602 ipctl
->dev
= info
->dev
;
603 platform_set_drvdata(pdev
, ipctl
);
604 ipctl
->pctl
= pinctrl_register(&imx_pinctrl_desc
, &pdev
->dev
, ipctl
);
606 dev_err(&pdev
->dev
, "could not register IMX pinctrl driver\n");
610 dev_info(&pdev
->dev
, "initialized IMX pinctrl driver\n");
615 int imx_pinctrl_remove(struct platform_device
*pdev
)
617 struct imx_pinctrl
*ipctl
= platform_get_drvdata(pdev
);
619 pinctrl_unregister(ipctl
->pctl
);