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/mfd/syscon.h>
20 #include <linux/of_device.h>
21 #include <linux/of_address.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>
26 #include <linux/slab.h>
27 #include <linux/regmap.h>
30 #include "../pinconf.h"
31 #include "../pinmux.h"
32 #include "pinctrl-imx.h"
34 /* The bits in CONFIG cell defined in binding doc*/
35 #define IMX_NO_PAD_CTL 0x80000000 /* no pin config need */
36 #define IMX_PAD_SION 0x40000000 /* set SION */
38 static inline const struct group_desc *imx_pinctrl_find_group_by_name(
39 struct pinctrl_dev *pctldev,
42 const struct group_desc *grp = NULL;
45 for (i = 0; i < pctldev->num_groups; i++) {
46 grp = pinctrl_generic_get_group(pctldev, i);
47 if (grp && !strcmp(grp->name, name))
54 static void imx_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
57 seq_printf(s, "%s", dev_name(pctldev->dev));
60 static int imx_dt_node_to_map(struct pinctrl_dev *pctldev,
61 struct device_node *np,
62 struct pinctrl_map **map, unsigned *num_maps)
64 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
65 const struct group_desc *grp;
66 struct pinctrl_map *new_map;
67 struct device_node *parent;
72 * first find the group of this node and check if we need create
73 * config maps for pins
75 grp = imx_pinctrl_find_group_by_name(pctldev, np->name);
77 dev_err(ipctl->dev, "unable to find group for node %s\n",
82 for (i = 0; i < grp->num_pins; i++) {
83 struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i];
85 if (!(pin->config & IMX_NO_PAD_CTL))
89 new_map = kmalloc(sizeof(struct pinctrl_map) * map_num, GFP_KERNEL);
97 parent = of_get_parent(np);
102 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
103 new_map[0].data.mux.function = parent->name;
104 new_map[0].data.mux.group = np->name;
107 /* create config map */
109 for (i = j = 0; i < grp->num_pins; i++) {
110 struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i];
112 if (!(pin->config & IMX_NO_PAD_CTL)) {
113 new_map[j].type = PIN_MAP_TYPE_CONFIGS_PIN;
114 new_map[j].data.configs.group_or_pin =
115 pin_get_name(pctldev, pin->pin);
116 new_map[j].data.configs.configs = &pin->config;
117 new_map[j].data.configs.num_configs = 1;
122 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
123 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
128 static void imx_dt_free_map(struct pinctrl_dev *pctldev,
129 struct pinctrl_map *map, unsigned num_maps)
134 static const struct pinctrl_ops imx_pctrl_ops = {
135 .get_groups_count = pinctrl_generic_get_group_count,
136 .get_group_name = pinctrl_generic_get_group_name,
137 .get_group_pins = pinctrl_generic_get_group_pins,
138 .pin_dbg_show = imx_pin_dbg_show,
139 .dt_node_to_map = imx_dt_node_to_map,
140 .dt_free_map = imx_dt_free_map,
144 static int imx_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
147 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
148 const struct imx_pinctrl_soc_info *info = ipctl->info;
149 const struct imx_pin_reg *pin_reg;
150 unsigned int npins, pin_id;
152 struct group_desc *grp = NULL;
153 struct function_desc *func = NULL;
156 * Configure the mux mode for each pin in the group for a specific
159 grp = pinctrl_generic_get_group(pctldev, group);
163 func = pinmux_generic_get_function(pctldev, selector);
167 npins = grp->num_pins;
169 dev_dbg(ipctl->dev, "enable function %s group %s\n",
170 func->name, grp->name);
172 for (i = 0; i < npins; i++) {
173 struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i];
176 pin_reg = &ipctl->pin_regs[pin_id];
178 if (pin_reg->mux_reg == -1) {
179 dev_dbg(ipctl->dev, "Pin(%s) does not support mux function\n",
180 info->pins[pin_id].name);
184 if (info->flags & SHARE_MUX_CONF_REG) {
186 reg = readl(ipctl->base + pin_reg->mux_reg);
187 reg &= ~info->mux_mask;
188 reg |= (pin->mux_mode << info->mux_shift);
189 writel(reg, ipctl->base + pin_reg->mux_reg);
190 dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%x\n",
191 pin_reg->mux_reg, reg);
193 writel(pin->mux_mode, ipctl->base + pin_reg->mux_reg);
194 dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%x\n",
195 pin_reg->mux_reg, pin->mux_mode);
199 * If the select input value begins with 0xff, it's a quirky
200 * select input and the value should be interpreted as below.
202 * | 0xff | shift | width | select |
203 * It's used to work around the problem that the select
204 * input for some pin is not implemented in the select
205 * input register but in some general purpose register.
206 * We encode the select input value, width and shift of
207 * the bit field into input_val cell of pin function ID
208 * in device tree, and then decode them here for setting
209 * up the select input bits in general purpose register.
211 if (pin->input_val >> 24 == 0xff) {
212 u32 val = pin->input_val;
213 u8 select = val & 0xff;
214 u8 width = (val >> 8) & 0xff;
215 u8 shift = (val >> 16) & 0xff;
216 u32 mask = ((1 << width) - 1) << shift;
218 * The input_reg[i] here is actually some IOMUXC general
219 * purpose register, not regular select input register.
221 val = readl(ipctl->base + pin->input_reg);
223 val |= select << shift;
224 writel(val, ipctl->base + pin->input_reg);
225 } else if (pin->input_reg) {
227 * Regular select input register can never be at offset
228 * 0, and we only print register value for regular case.
230 if (ipctl->input_sel_base)
231 writel(pin->input_val, ipctl->input_sel_base +
234 writel(pin->input_val, ipctl->base +
237 "==>select_input: offset 0x%x val 0x%x\n",
238 pin->input_reg, pin->input_val);
245 struct pinmux_ops imx_pmx_ops = {
246 .get_functions_count = pinmux_generic_get_function_count,
247 .get_function_name = pinmux_generic_get_function_name,
248 .get_function_groups = pinmux_generic_get_function_groups,
249 .set_mux = imx_pmx_set,
252 /* decode generic config into raw register values */
253 static u32 imx_pinconf_decode_generic_config(struct imx_pinctrl *ipctl,
254 unsigned long *configs,
255 unsigned int num_configs)
257 const struct imx_pinctrl_soc_info *info = ipctl->info;
258 const struct imx_cfg_params_decode *decode;
259 enum pin_config_param param;
264 WARN_ON(num_configs > info->num_decodes);
266 for (i = 0; i < num_configs; i++) {
267 param = pinconf_to_config_param(configs[i]);
268 param_val = pinconf_to_config_argument(configs[i]);
269 decode = info->decodes;
270 for (j = 0; j < info->num_decodes; j++) {
271 if (param == decode->param) {
273 param_val = !param_val;
274 raw_config |= (param_val << decode->shift)
283 info->fixup(configs, num_configs, &raw_config);
288 static u32 imx_pinconf_parse_generic_config(struct device_node *np,
289 struct imx_pinctrl *ipctl)
291 const struct imx_pinctrl_soc_info *info = ipctl->info;
292 struct pinctrl_dev *pctl = ipctl->pctl;
293 unsigned int num_configs;
294 unsigned long *configs;
297 if (!info->generic_pinconf)
300 ret = pinconf_generic_parse_dt_config(np, pctl, &configs,
305 return imx_pinconf_decode_generic_config(ipctl, configs, num_configs);
308 static int imx_pinconf_get(struct pinctrl_dev *pctldev,
309 unsigned pin_id, unsigned long *config)
311 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
312 const struct imx_pinctrl_soc_info *info = ipctl->info;
313 const struct imx_pin_reg *pin_reg = &ipctl->pin_regs[pin_id];
315 if (pin_reg->conf_reg == -1) {
316 dev_err(ipctl->dev, "Pin(%s) does not support config function\n",
317 info->pins[pin_id].name);
321 *config = readl(ipctl->base + pin_reg->conf_reg);
323 if (info->flags & SHARE_MUX_CONF_REG)
324 *config &= ~info->mux_mask;
329 static int imx_pinconf_set(struct pinctrl_dev *pctldev,
330 unsigned pin_id, unsigned long *configs,
331 unsigned num_configs)
333 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
334 const struct imx_pinctrl_soc_info *info = ipctl->info;
335 const struct imx_pin_reg *pin_reg = &ipctl->pin_regs[pin_id];
338 if (pin_reg->conf_reg == -1) {
339 dev_err(ipctl->dev, "Pin(%s) does not support config function\n",
340 info->pins[pin_id].name);
344 dev_dbg(ipctl->dev, "pinconf set pin %s\n",
345 info->pins[pin_id].name);
347 for (i = 0; i < num_configs; i++) {
348 if (info->flags & SHARE_MUX_CONF_REG) {
350 reg = readl(ipctl->base + pin_reg->conf_reg);
351 reg &= info->mux_mask;
353 writel(reg, ipctl->base + pin_reg->conf_reg);
354 dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%x\n",
355 pin_reg->conf_reg, reg);
357 writel(configs[i], ipctl->base + pin_reg->conf_reg);
358 dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%lx\n",
359 pin_reg->conf_reg, configs[i]);
361 } /* for each config */
366 static void imx_pinconf_dbg_show(struct pinctrl_dev *pctldev,
367 struct seq_file *s, unsigned pin_id)
369 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
370 const struct imx_pin_reg *pin_reg = &ipctl->pin_regs[pin_id];
371 unsigned long config;
373 if (!pin_reg || pin_reg->conf_reg == -1) {
378 config = readl(ipctl->base + pin_reg->conf_reg);
379 seq_printf(s, "0x%lx", config);
382 static void imx_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
383 struct seq_file *s, unsigned group)
385 struct group_desc *grp;
386 unsigned long config;
390 if (group > pctldev->num_groups)
394 grp = pinctrl_generic_get_group(pctldev, group);
398 for (i = 0; i < grp->num_pins; i++) {
399 struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i];
401 name = pin_get_name(pctldev, pin->pin);
402 ret = imx_pinconf_get(pctldev, pin->pin, &config);
405 seq_printf(s, " %s: 0x%lx\n", name, config);
409 static const struct pinconf_ops imx_pinconf_ops = {
410 .pin_config_get = imx_pinconf_get,
411 .pin_config_set = imx_pinconf_set,
412 .pin_config_dbg_show = imx_pinconf_dbg_show,
413 .pin_config_group_dbg_show = imx_pinconf_group_dbg_show,
417 * Each pin represented in fsl,pins consists of a number of u32 PIN_FUNC_ID
418 * and 1 u32 CONFIG, the total size is PIN_FUNC_ID + CONFIG for each pin.
419 * For generic_pinconf case, there's no extra u32 CONFIG.
421 * PIN_FUNC_ID format:
423 * <mux_reg conf_reg input_reg mux_mode input_val>
424 * SHARE_MUX_CONF_REG:
425 * <mux_conf_reg input_reg mux_mode input_val>
427 #define FSL_PIN_SIZE 24
428 #define FSL_PIN_SHARE_SIZE 20
430 static int imx_pinctrl_parse_groups(struct device_node *np,
431 struct group_desc *grp,
432 struct imx_pinctrl *ipctl,
435 const struct imx_pinctrl_soc_info *info = ipctl->info;
441 dev_dbg(ipctl->dev, "group(%d): %s\n", index, np->name);
443 if (info->flags & SHARE_MUX_CONF_REG)
444 pin_size = FSL_PIN_SHARE_SIZE;
446 pin_size = FSL_PIN_SIZE;
448 if (info->generic_pinconf)
451 /* Initialise group */
452 grp->name = np->name;
455 * the binding format is fsl,pins = <PIN_FUNC_ID CONFIG ...>,
456 * do sanity check and calculate pins number
458 * First try legacy 'fsl,pins' property, then fall back to the
461 * Note: for generic 'pinmux' case, there's no CONFIG part in
462 * the binding format.
464 list = of_get_property(np, "fsl,pins", &size);
466 list = of_get_property(np, "pinmux", &size);
469 "no fsl,pins and pins property in node %pOF\n", np);
474 /* we do not check return since it's safe node passed down */
475 if (!size || size % pin_size) {
476 dev_err(ipctl->dev, "Invalid fsl,pins or pins property in node %pOF\n", np);
480 /* first try to parse the generic pin config */
481 config = imx_pinconf_parse_generic_config(np, ipctl);
483 grp->num_pins = size / pin_size;
484 grp->data = devm_kzalloc(ipctl->dev, grp->num_pins *
485 sizeof(struct imx_pin), GFP_KERNEL);
486 grp->pins = devm_kzalloc(ipctl->dev, grp->num_pins *
487 sizeof(unsigned int), GFP_KERNEL);
488 if (!grp->pins || !grp->data)
491 for (i = 0; i < grp->num_pins; i++) {
492 u32 mux_reg = be32_to_cpu(*list++);
495 struct imx_pin_reg *pin_reg;
496 struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i];
498 if (!(info->flags & ZERO_OFFSET_VALID) && !mux_reg)
501 if (info->flags & SHARE_MUX_CONF_REG) {
504 conf_reg = be32_to_cpu(*list++);
509 pin_id = (mux_reg != -1) ? mux_reg / 4 : conf_reg / 4;
510 pin_reg = &ipctl->pin_regs[pin_id];
512 grp->pins[i] = pin_id;
513 pin_reg->mux_reg = mux_reg;
514 pin_reg->conf_reg = conf_reg;
515 pin->input_reg = be32_to_cpu(*list++);
516 pin->mux_mode = be32_to_cpu(*list++);
517 pin->input_val = be32_to_cpu(*list++);
519 if (info->generic_pinconf) {
520 /* generic pin config decoded */
521 pin->config = config;
523 /* legacy pin config read from devicetree */
524 config = be32_to_cpu(*list++);
526 /* SION bit is in mux register */
527 if (config & IMX_PAD_SION)
528 pin->mux_mode |= IOMUXC_CONFIG_SION;
529 pin->config = config & ~IMX_PAD_SION;
532 dev_dbg(ipctl->dev, "%s: 0x%x 0x%08lx", info->pins[pin_id].name,
533 pin->mux_mode, pin->config);
539 static int imx_pinctrl_parse_functions(struct device_node *np,
540 struct imx_pinctrl *ipctl,
543 struct pinctrl_dev *pctl = ipctl->pctl;
544 struct device_node *child;
545 struct function_desc *func;
546 struct group_desc *grp;
549 dev_dbg(pctl->dev, "parse function(%d): %s\n", index, np->name);
551 func = pinmux_generic_get_function(pctl, index);
555 /* Initialise function */
556 func->name = np->name;
557 func->num_group_names = of_get_child_count(np);
558 if (func->num_group_names == 0) {
559 dev_err(ipctl->dev, "no groups defined in %pOF\n", np);
562 func->group_names = devm_kcalloc(ipctl->dev, func->num_group_names,
563 sizeof(char *), GFP_KERNEL);
564 if (!func->group_names)
567 for_each_child_of_node(np, child) {
568 func->group_names[i] = child->name;
570 grp = devm_kzalloc(ipctl->dev, sizeof(struct group_desc),
575 mutex_lock(&ipctl->mutex);
576 radix_tree_insert(&pctl->pin_group_tree,
577 ipctl->group_index++, grp);
578 mutex_unlock(&ipctl->mutex);
580 imx_pinctrl_parse_groups(child, grp, ipctl, i++);
587 * Check if the DT contains pins in the direct child nodes. This indicates the
588 * newer DT format to store pins. This function returns true if the first found
589 * fsl,pins property is in a child of np. Otherwise false is returned.
591 static bool imx_pinctrl_dt_is_flat_functions(struct device_node *np)
593 struct device_node *function_np;
594 struct device_node *pinctrl_np;
596 for_each_child_of_node(np, function_np) {
597 if (of_property_read_bool(function_np, "fsl,pins"))
600 for_each_child_of_node(function_np, pinctrl_np) {
601 if (of_property_read_bool(pinctrl_np, "fsl,pins"))
609 static int imx_pinctrl_probe_dt(struct platform_device *pdev,
610 struct imx_pinctrl *ipctl)
612 struct device_node *np = pdev->dev.of_node;
613 struct device_node *child;
614 struct pinctrl_dev *pctl = ipctl->pctl;
622 flat_funcs = imx_pinctrl_dt_is_flat_functions(np);
626 nfuncs = of_get_child_count(np);
628 dev_err(&pdev->dev, "no functions defined\n");
633 for (i = 0; i < nfuncs; i++) {
634 struct function_desc *function;
636 function = devm_kzalloc(&pdev->dev, sizeof(*function),
641 mutex_lock(&ipctl->mutex);
642 radix_tree_insert(&pctl->pin_function_tree, i, function);
643 mutex_unlock(&ipctl->mutex);
645 pctl->num_functions = nfuncs;
647 ipctl->group_index = 0;
649 pctl->num_groups = of_get_child_count(np);
651 pctl->num_groups = 0;
652 for_each_child_of_node(np, child)
653 pctl->num_groups += of_get_child_count(child);
657 imx_pinctrl_parse_functions(np, ipctl, 0);
660 for_each_child_of_node(np, child)
661 imx_pinctrl_parse_functions(child, ipctl, i++);
668 * imx_free_resources() - free memory used by this driver
669 * @info: info driver instance
671 static void imx_free_resources(struct imx_pinctrl *ipctl)
674 pinctrl_unregister(ipctl->pctl);
677 int imx_pinctrl_probe(struct platform_device *pdev,
678 const struct imx_pinctrl_soc_info *info)
680 struct regmap_config config = { .name = "gpr" };
681 struct device_node *dev_np = pdev->dev.of_node;
682 struct pinctrl_desc *imx_pinctrl_desc;
683 struct device_node *np;
684 struct imx_pinctrl *ipctl;
685 struct resource *res;
689 if (!info || !info->pins || !info->npins) {
690 dev_err(&pdev->dev, "wrong pinctrl info\n");
694 if (info->gpr_compatible) {
695 gpr = syscon_regmap_lookup_by_compatible(info->gpr_compatible);
697 regmap_attach_dev(&pdev->dev, gpr, &config);
700 /* Create state holders etc for this driver */
701 ipctl = devm_kzalloc(&pdev->dev, sizeof(*ipctl), GFP_KERNEL);
705 ipctl->pin_regs = devm_kmalloc(&pdev->dev, sizeof(*ipctl->pin_regs) *
706 info->npins, GFP_KERNEL);
707 if (!ipctl->pin_regs)
710 for (i = 0; i < info->npins; i++) {
711 ipctl->pin_regs[i].mux_reg = -1;
712 ipctl->pin_regs[i].conf_reg = -1;
715 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
716 ipctl->base = devm_ioremap_resource(&pdev->dev, res);
717 if (IS_ERR(ipctl->base))
718 return PTR_ERR(ipctl->base);
720 if (of_property_read_bool(dev_np, "fsl,input-sel")) {
721 np = of_parse_phandle(dev_np, "fsl,input-sel", 0);
723 dev_err(&pdev->dev, "iomuxc fsl,input-sel property not found\n");
727 ipctl->input_sel_base = of_iomap(np, 0);
729 if (!ipctl->input_sel_base) {
731 "iomuxc input select base address not found\n");
736 imx_pinctrl_desc = devm_kzalloc(&pdev->dev, sizeof(*imx_pinctrl_desc),
738 if (!imx_pinctrl_desc)
741 imx_pinctrl_desc->name = dev_name(&pdev->dev);
742 imx_pinctrl_desc->pins = info->pins;
743 imx_pinctrl_desc->npins = info->npins;
744 imx_pinctrl_desc->pctlops = &imx_pctrl_ops;
745 imx_pinctrl_desc->pmxops = &imx_pmx_ops;
746 imx_pinctrl_desc->confops = &imx_pinconf_ops;
747 imx_pinctrl_desc->owner = THIS_MODULE;
749 /* for generic pinconf */
750 imx_pinctrl_desc->custom_params = info->custom_params;
751 imx_pinctrl_desc->num_custom_params = info->num_custom_params;
753 /* platform specific callback */
754 imx_pmx_ops.gpio_set_direction = info->gpio_set_direction;
756 mutex_init(&ipctl->mutex);
759 ipctl->dev = &pdev->dev;
760 platform_set_drvdata(pdev, ipctl);
761 ret = devm_pinctrl_register_and_init(&pdev->dev,
762 imx_pinctrl_desc, ipctl,
765 dev_err(&pdev->dev, "could not register IMX pinctrl driver\n");
769 ret = imx_pinctrl_probe_dt(pdev, ipctl);
771 dev_err(&pdev->dev, "fail to probe dt properties\n");
775 dev_info(&pdev->dev, "initialized IMX pinctrl driver\n");
777 return pinctrl_enable(ipctl->pctl);
780 imx_free_resources(ipctl);