2 * Driver for the NVIDIA Tegra pinmux
4 * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved.
7 * Copyright (C) 2010 Google, Inc.
8 * Copyright (C) 2010 NVIDIA Corporation
9 * Copyright (C) 2009-2011 ST-Ericsson AB
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms and conditions of the GNU General Public License,
13 * version 2, as published by the Free Software Foundation.
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 #include <linux/err.h>
22 #include <linux/init.h>
25 #include <linux/platform_device.h>
26 #include <linux/pinctrl/machine.h>
27 #include <linux/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/pinmux.h>
29 #include <linux/pinctrl/pinconf.h>
30 #include <linux/slab.h>
33 #include "../pinctrl-utils.h"
34 #include "pinctrl-tegra.h"
36 static inline u32
pmx_readl(struct tegra_pmx
*pmx
, u32 bank
, u32 reg
)
38 return readl(pmx
->regs
[bank
] + reg
);
41 static inline void pmx_writel(struct tegra_pmx
*pmx
, u32 val
, u32 bank
, u32 reg
)
43 writel_relaxed(val
, pmx
->regs
[bank
] + reg
);
44 /* make sure pinmux register write completed */
45 pmx_readl(pmx
, bank
, reg
);
48 static int tegra_pinctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
50 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
52 return pmx
->soc
->ngroups
;
55 static const char *tegra_pinctrl_get_group_name(struct pinctrl_dev
*pctldev
,
58 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
60 return pmx
->soc
->groups
[group
].name
;
63 static int tegra_pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
65 const unsigned **pins
,
68 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
70 *pins
= pmx
->soc
->groups
[group
].pins
;
71 *num_pins
= pmx
->soc
->groups
[group
].npins
;
76 #ifdef CONFIG_DEBUG_FS
77 static void tegra_pinctrl_pin_dbg_show(struct pinctrl_dev
*pctldev
,
81 seq_printf(s
, " %s", dev_name(pctldev
->dev
));
85 static const struct cfg_param
{
87 enum tegra_pinconf_param param
;
89 {"nvidia,pull", TEGRA_PINCONF_PARAM_PULL
},
90 {"nvidia,tristate", TEGRA_PINCONF_PARAM_TRISTATE
},
91 {"nvidia,enable-input", TEGRA_PINCONF_PARAM_ENABLE_INPUT
},
92 {"nvidia,open-drain", TEGRA_PINCONF_PARAM_OPEN_DRAIN
},
93 {"nvidia,lock", TEGRA_PINCONF_PARAM_LOCK
},
94 {"nvidia,io-reset", TEGRA_PINCONF_PARAM_IORESET
},
95 {"nvidia,rcv-sel", TEGRA_PINCONF_PARAM_RCV_SEL
},
96 {"nvidia,io-hv", TEGRA_PINCONF_PARAM_RCV_SEL
},
97 {"nvidia,high-speed-mode", TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE
},
98 {"nvidia,schmitt", TEGRA_PINCONF_PARAM_SCHMITT
},
99 {"nvidia,low-power-mode", TEGRA_PINCONF_PARAM_LOW_POWER_MODE
},
100 {"nvidia,pull-down-strength", TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH
},
101 {"nvidia,pull-up-strength", TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH
},
102 {"nvidia,slew-rate-falling", TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING
},
103 {"nvidia,slew-rate-rising", TEGRA_PINCONF_PARAM_SLEW_RATE_RISING
},
104 {"nvidia,drive-type", TEGRA_PINCONF_PARAM_DRIVE_TYPE
},
107 static int tegra_pinctrl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
108 struct device_node
*np
,
109 struct pinctrl_map
**map
,
110 unsigned *reserved_maps
,
113 struct device
*dev
= pctldev
->dev
;
115 const char *function
;
117 unsigned long config
;
118 unsigned long *configs
= NULL
;
119 unsigned num_configs
= 0;
121 struct property
*prop
;
124 ret
= of_property_read_string(np
, "nvidia,function", &function
);
126 /* EINVAL=missing, which is fine since it's optional */
129 "could not parse property nvidia,function\n");
133 for (i
= 0; i
< ARRAY_SIZE(cfg_params
); i
++) {
134 ret
= of_property_read_u32(np
, cfg_params
[i
].property
, &val
);
136 config
= TEGRA_PINCONF_PACK(cfg_params
[i
].param
, val
);
137 ret
= pinctrl_utils_add_config(pctldev
, &configs
,
138 &num_configs
, config
);
141 /* EINVAL=missing, which is fine since it's optional */
142 } else if (ret
!= -EINVAL
) {
143 dev_err(dev
, "could not parse property %s\n",
144 cfg_params
[i
].property
);
149 if (function
!= NULL
)
153 ret
= of_property_count_strings(np
, "nvidia,pins");
155 dev_err(dev
, "could not parse property nvidia,pins\n");
160 ret
= pinctrl_utils_reserve_map(pctldev
, map
, reserved_maps
,
165 of_property_for_each_string(np
, "nvidia,pins", prop
, group
) {
167 ret
= pinctrl_utils_add_map_mux(pctldev
, map
,
168 reserved_maps
, num_maps
, group
,
175 ret
= pinctrl_utils_add_map_configs(pctldev
, map
,
176 reserved_maps
, num_maps
, group
,
177 configs
, num_configs
,
178 PIN_MAP_TYPE_CONFIGS_GROUP
);
191 static int tegra_pinctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
192 struct device_node
*np_config
,
193 struct pinctrl_map
**map
,
196 unsigned reserved_maps
;
197 struct device_node
*np
;
204 for_each_child_of_node(np_config
, np
) {
205 ret
= tegra_pinctrl_dt_subnode_to_map(pctldev
, np
, map
,
206 &reserved_maps
, num_maps
);
208 pinctrl_utils_free_map(pctldev
, *map
,
218 static const struct pinctrl_ops tegra_pinctrl_ops
= {
219 .get_groups_count
= tegra_pinctrl_get_groups_count
,
220 .get_group_name
= tegra_pinctrl_get_group_name
,
221 .get_group_pins
= tegra_pinctrl_get_group_pins
,
222 #ifdef CONFIG_DEBUG_FS
223 .pin_dbg_show
= tegra_pinctrl_pin_dbg_show
,
225 .dt_node_to_map
= tegra_pinctrl_dt_node_to_map
,
226 .dt_free_map
= pinctrl_utils_free_map
,
229 static int tegra_pinctrl_get_funcs_count(struct pinctrl_dev
*pctldev
)
231 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
233 return pmx
->soc
->nfunctions
;
236 static const char *tegra_pinctrl_get_func_name(struct pinctrl_dev
*pctldev
,
239 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
241 return pmx
->soc
->functions
[function
].name
;
244 static int tegra_pinctrl_get_func_groups(struct pinctrl_dev
*pctldev
,
246 const char * const **groups
,
247 unsigned * const num_groups
)
249 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
251 *groups
= pmx
->soc
->functions
[function
].groups
;
252 *num_groups
= pmx
->soc
->functions
[function
].ngroups
;
257 static int tegra_pinctrl_set_mux(struct pinctrl_dev
*pctldev
,
261 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
262 const struct tegra_pingroup
*g
;
266 g
= &pmx
->soc
->groups
[group
];
268 if (WARN_ON(g
->mux_reg
< 0))
271 for (i
= 0; i
< ARRAY_SIZE(g
->funcs
); i
++) {
272 if (g
->funcs
[i
] == function
)
275 if (WARN_ON(i
== ARRAY_SIZE(g
->funcs
)))
278 val
= pmx_readl(pmx
, g
->mux_bank
, g
->mux_reg
);
279 val
&= ~(0x3 << g
->mux_bit
);
280 val
|= i
<< g
->mux_bit
;
281 pmx_writel(pmx
, val
, g
->mux_bank
, g
->mux_reg
);
286 static const struct pinmux_ops tegra_pinmux_ops
= {
287 .get_functions_count
= tegra_pinctrl_get_funcs_count
,
288 .get_function_name
= tegra_pinctrl_get_func_name
,
289 .get_function_groups
= tegra_pinctrl_get_func_groups
,
290 .set_mux
= tegra_pinctrl_set_mux
,
293 static int tegra_pinconf_reg(struct tegra_pmx
*pmx
,
294 const struct tegra_pingroup
*g
,
295 enum tegra_pinconf_param param
,
297 s8
*bank
, s16
*reg
, s8
*bit
, s8
*width
)
300 case TEGRA_PINCONF_PARAM_PULL
:
301 *bank
= g
->pupd_bank
;
306 case TEGRA_PINCONF_PARAM_TRISTATE
:
312 case TEGRA_PINCONF_PARAM_ENABLE_INPUT
:
315 *bit
= g
->einput_bit
;
318 case TEGRA_PINCONF_PARAM_OPEN_DRAIN
:
321 *bit
= g
->odrain_bit
;
324 case TEGRA_PINCONF_PARAM_LOCK
:
330 case TEGRA_PINCONF_PARAM_IORESET
:
333 *bit
= g
->ioreset_bit
;
336 case TEGRA_PINCONF_PARAM_RCV_SEL
:
339 *bit
= g
->rcv_sel_bit
;
342 case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE
:
343 if (pmx
->soc
->hsm_in_mux
) {
353 case TEGRA_PINCONF_PARAM_SCHMITT
:
354 if (pmx
->soc
->schmitt_in_mux
) {
361 *bit
= g
->schmitt_bit
;
364 case TEGRA_PINCONF_PARAM_LOW_POWER_MODE
:
370 case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH
:
374 *width
= g
->drvdn_width
;
376 case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH
:
380 *width
= g
->drvup_width
;
382 case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING
:
386 *width
= g
->slwf_width
;
388 case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING
:
392 *width
= g
->slwr_width
;
394 case TEGRA_PINCONF_PARAM_DRIVE_TYPE
:
395 if (pmx
->soc
->drvtype_in_mux
) {
402 *bit
= g
->drvtype_bit
;
406 dev_err(pmx
->dev
, "Invalid config param %04x\n", param
);
410 if (*reg
< 0 || *bit
< 0) {
412 const char *prop
= "unknown";
415 for (i
= 0; i
< ARRAY_SIZE(cfg_params
); i
++) {
416 if (cfg_params
[i
].param
== param
) {
417 prop
= cfg_params
[i
].property
;
423 "Config param %04x (%s) not supported on group %s\n",
424 param
, prop
, g
->name
);
432 static int tegra_pinconf_get(struct pinctrl_dev
*pctldev
,
433 unsigned pin
, unsigned long *config
)
435 dev_err(pctldev
->dev
, "pin_config_get op not supported\n");
439 static int tegra_pinconf_set(struct pinctrl_dev
*pctldev
,
440 unsigned pin
, unsigned long *configs
,
441 unsigned num_configs
)
443 dev_err(pctldev
->dev
, "pin_config_set op not supported\n");
447 static int tegra_pinconf_group_get(struct pinctrl_dev
*pctldev
,
448 unsigned group
, unsigned long *config
)
450 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
451 enum tegra_pinconf_param param
= TEGRA_PINCONF_UNPACK_PARAM(*config
);
453 const struct tegra_pingroup
*g
;
459 g
= &pmx
->soc
->groups
[group
];
461 ret
= tegra_pinconf_reg(pmx
, g
, param
, true, &bank
, ®
, &bit
,
466 val
= pmx_readl(pmx
, bank
, reg
);
467 mask
= (1 << width
) - 1;
468 arg
= (val
>> bit
) & mask
;
470 *config
= TEGRA_PINCONF_PACK(param
, arg
);
475 static int tegra_pinconf_group_set(struct pinctrl_dev
*pctldev
,
476 unsigned group
, unsigned long *configs
,
477 unsigned num_configs
)
479 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
480 enum tegra_pinconf_param param
;
482 const struct tegra_pingroup
*g
;
488 g
= &pmx
->soc
->groups
[group
];
490 for (i
= 0; i
< num_configs
; i
++) {
491 param
= TEGRA_PINCONF_UNPACK_PARAM(configs
[i
]);
492 arg
= TEGRA_PINCONF_UNPACK_ARG(configs
[i
]);
494 ret
= tegra_pinconf_reg(pmx
, g
, param
, true, &bank
, ®
, &bit
,
499 val
= pmx_readl(pmx
, bank
, reg
);
501 /* LOCK can't be cleared */
502 if (param
== TEGRA_PINCONF_PARAM_LOCK
) {
503 if ((val
& BIT(bit
)) && !arg
) {
504 dev_err(pctldev
->dev
, "LOCK bit cannot be cleared\n");
509 /* Special-case Boolean values; allow any non-zero as true */
513 /* Range-check user-supplied value */
514 mask
= (1 << width
) - 1;
516 dev_err(pctldev
->dev
,
517 "config %lx: %x too big for %d bit register\n",
518 configs
[i
], arg
, width
);
522 /* Update register */
523 val
&= ~(mask
<< bit
);
525 pmx_writel(pmx
, val
, bank
, reg
);
526 } /* for each config */
531 #ifdef CONFIG_DEBUG_FS
532 static void tegra_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
533 struct seq_file
*s
, unsigned offset
)
537 static const char *strip_prefix(const char *s
)
539 const char *comma
= strchr(s
, ',');
546 static void tegra_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
547 struct seq_file
*s
, unsigned group
)
549 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
550 const struct tegra_pingroup
*g
;
556 g
= &pmx
->soc
->groups
[group
];
558 for (i
= 0; i
< ARRAY_SIZE(cfg_params
); i
++) {
559 ret
= tegra_pinconf_reg(pmx
, g
, cfg_params
[i
].param
, false,
560 &bank
, ®
, &bit
, &width
);
564 val
= pmx_readl(pmx
, bank
, reg
);
566 val
&= (1 << width
) - 1;
568 seq_printf(s
, "\n\t%s=%u",
569 strip_prefix(cfg_params
[i
].property
), val
);
573 static void tegra_pinconf_config_dbg_show(struct pinctrl_dev
*pctldev
,
575 unsigned long config
)
577 enum tegra_pinconf_param param
= TEGRA_PINCONF_UNPACK_PARAM(config
);
578 u16 arg
= TEGRA_PINCONF_UNPACK_ARG(config
);
579 const char *pname
= "unknown";
582 for (i
= 0; i
< ARRAY_SIZE(cfg_params
); i
++) {
583 if (cfg_params
[i
].param
== param
) {
584 pname
= cfg_params
[i
].property
;
589 seq_printf(s
, "%s=%d", strip_prefix(pname
), arg
);
593 static const struct pinconf_ops tegra_pinconf_ops
= {
594 .pin_config_get
= tegra_pinconf_get
,
595 .pin_config_set
= tegra_pinconf_set
,
596 .pin_config_group_get
= tegra_pinconf_group_get
,
597 .pin_config_group_set
= tegra_pinconf_group_set
,
598 #ifdef CONFIG_DEBUG_FS
599 .pin_config_dbg_show
= tegra_pinconf_dbg_show
,
600 .pin_config_group_dbg_show
= tegra_pinconf_group_dbg_show
,
601 .pin_config_config_dbg_show
= tegra_pinconf_config_dbg_show
,
605 static struct pinctrl_gpio_range tegra_pinctrl_gpio_range
= {
606 .name
= "Tegra GPIOs",
611 static struct pinctrl_desc tegra_pinctrl_desc
= {
612 .pctlops
= &tegra_pinctrl_ops
,
613 .pmxops
= &tegra_pinmux_ops
,
614 .confops
= &tegra_pinconf_ops
,
615 .owner
= THIS_MODULE
,
618 static void tegra_pinctrl_clear_parked_bits(struct tegra_pmx
*pmx
)
621 const struct tegra_pingroup
*g
;
624 for (i
= 0; i
< pmx
->soc
->ngroups
; ++i
) {
625 g
= &pmx
->soc
->groups
[i
];
626 if (g
->parked_bit
>= 0) {
627 val
= pmx_readl(pmx
, g
->mux_bank
, g
->mux_reg
);
628 val
&= ~(1 << g
->parked_bit
);
629 pmx_writel(pmx
, val
, g
->mux_bank
, g
->mux_reg
);
634 static bool gpio_node_has_range(const char *compatible
)
636 struct device_node
*np
;
637 bool has_prop
= false;
639 np
= of_find_compatible_node(NULL
, NULL
, compatible
);
643 has_prop
= of_find_property(np
, "gpio-ranges", NULL
);
650 int tegra_pinctrl_probe(struct platform_device
*pdev
,
651 const struct tegra_pinctrl_soc_data
*soc_data
)
653 struct tegra_pmx
*pmx
;
654 struct resource
*res
;
656 const char **group_pins
;
659 pmx
= devm_kzalloc(&pdev
->dev
, sizeof(*pmx
), GFP_KERNEL
);
663 pmx
->dev
= &pdev
->dev
;
667 * Each mux group will appear in 4 functions' list of groups.
668 * This over-allocates slightly, since not all groups are mux groups.
670 pmx
->group_pins
= devm_kcalloc(&pdev
->dev
,
671 soc_data
->ngroups
* 4, sizeof(*pmx
->group_pins
),
673 if (!pmx
->group_pins
)
676 group_pins
= pmx
->group_pins
;
677 for (fn
= 0; fn
< soc_data
->nfunctions
; fn
++) {
678 struct tegra_function
*func
= &soc_data
->functions
[fn
];
680 func
->groups
= group_pins
;
682 for (gn
= 0; gn
< soc_data
->ngroups
; gn
++) {
683 const struct tegra_pingroup
*g
= &soc_data
->groups
[gn
];
685 if (g
->mux_reg
== -1)
688 for (gfn
= 0; gfn
< 4; gfn
++)
689 if (g
->funcs
[gfn
] == fn
)
694 BUG_ON(group_pins
- pmx
->group_pins
>=
695 soc_data
->ngroups
* 4);
696 *group_pins
++ = g
->name
;
701 tegra_pinctrl_gpio_range
.npins
= pmx
->soc
->ngpios
;
702 tegra_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
703 tegra_pinctrl_desc
.pins
= pmx
->soc
->pins
;
704 tegra_pinctrl_desc
.npins
= pmx
->soc
->npins
;
707 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
713 pmx
->regs
= devm_kcalloc(&pdev
->dev
, pmx
->nbanks
, sizeof(*pmx
->regs
),
718 for (i
= 0; i
< pmx
->nbanks
; i
++) {
719 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
720 pmx
->regs
[i
] = devm_ioremap_resource(&pdev
->dev
, res
);
721 if (IS_ERR(pmx
->regs
[i
]))
722 return PTR_ERR(pmx
->regs
[i
]);
725 pmx
->pctl
= devm_pinctrl_register(&pdev
->dev
, &tegra_pinctrl_desc
, pmx
);
726 if (IS_ERR(pmx
->pctl
)) {
727 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
728 return PTR_ERR(pmx
->pctl
);
731 tegra_pinctrl_clear_parked_bits(pmx
);
733 if (!gpio_node_has_range(pmx
->soc
->gpio_compatible
))
734 pinctrl_add_gpio_range(pmx
->pctl
, &tegra_pinctrl_gpio_range
);
736 platform_set_drvdata(pdev
, pmx
);
738 dev_dbg(&pdev
->dev
, "Probed Tegra pinctrl driver\n");
742 EXPORT_SYMBOL_GPL(tegra_pinctrl_probe
);