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>
24 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/pinctrl/machine.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include <linux/pinctrl/pinconf.h>
31 #include <linux/slab.h>
34 #include "pinctrl-tegra.h"
35 #include "pinctrl-utils.h"
39 struct pinctrl_dev *pctl;
41 const struct tegra_pinctrl_soc_data *soc;
42 const char **group_pins;
48 static inline u32 pmx_readl(struct tegra_pmx *pmx, u32 bank, u32 reg)
50 return readl(pmx->regs[bank] + reg);
53 static inline void pmx_writel(struct tegra_pmx *pmx, u32 val, u32 bank, u32 reg)
55 writel(val, pmx->regs[bank] + reg);
58 static int tegra_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
60 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
62 return pmx->soc->ngroups;
65 static const char *tegra_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
68 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
70 return pmx->soc->groups[group].name;
73 static int tegra_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
75 const unsigned **pins,
78 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
80 *pins = pmx->soc->groups[group].pins;
81 *num_pins = pmx->soc->groups[group].npins;
86 #ifdef CONFIG_DEBUG_FS
87 static void tegra_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
91 seq_printf(s, " %s", dev_name(pctldev->dev));
95 static const struct cfg_param {
97 enum tegra_pinconf_param param;
99 {"nvidia,pull", TEGRA_PINCONF_PARAM_PULL},
100 {"nvidia,tristate", TEGRA_PINCONF_PARAM_TRISTATE},
101 {"nvidia,enable-input", TEGRA_PINCONF_PARAM_ENABLE_INPUT},
102 {"nvidia,open-drain", TEGRA_PINCONF_PARAM_OPEN_DRAIN},
103 {"nvidia,lock", TEGRA_PINCONF_PARAM_LOCK},
104 {"nvidia,io-reset", TEGRA_PINCONF_PARAM_IORESET},
105 {"nvidia,rcv-sel", TEGRA_PINCONF_PARAM_RCV_SEL},
106 {"nvidia,io-hv", TEGRA_PINCONF_PARAM_RCV_SEL},
107 {"nvidia,high-speed-mode", TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE},
108 {"nvidia,schmitt", TEGRA_PINCONF_PARAM_SCHMITT},
109 {"nvidia,low-power-mode", TEGRA_PINCONF_PARAM_LOW_POWER_MODE},
110 {"nvidia,pull-down-strength", TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH},
111 {"nvidia,pull-up-strength", TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH},
112 {"nvidia,slew-rate-falling", TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING},
113 {"nvidia,slew-rate-rising", TEGRA_PINCONF_PARAM_SLEW_RATE_RISING},
114 {"nvidia,drive-type", TEGRA_PINCONF_PARAM_DRIVE_TYPE},
117 static int tegra_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
118 struct device_node *np,
119 struct pinctrl_map **map,
120 unsigned *reserved_maps,
123 struct device *dev = pctldev->dev;
125 const char *function;
127 unsigned long config;
128 unsigned long *configs = NULL;
129 unsigned num_configs = 0;
131 struct property *prop;
134 ret = of_property_read_string(np, "nvidia,function", &function);
136 /* EINVAL=missing, which is fine since it's optional */
139 "could not parse property nvidia,function\n");
143 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
144 ret = of_property_read_u32(np, cfg_params[i].property, &val);
146 config = TEGRA_PINCONF_PACK(cfg_params[i].param, val);
147 ret = pinctrl_utils_add_config(pctldev, &configs,
148 &num_configs, config);
151 /* EINVAL=missing, which is fine since it's optional */
152 } else if (ret != -EINVAL) {
153 dev_err(dev, "could not parse property %s\n",
154 cfg_params[i].property);
159 if (function != NULL)
163 ret = of_property_count_strings(np, "nvidia,pins");
165 dev_err(dev, "could not parse property nvidia,pins\n");
170 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
175 of_property_for_each_string(np, "nvidia,pins", prop, group) {
177 ret = pinctrl_utils_add_map_mux(pctldev, map,
178 reserved_maps, num_maps, group,
185 ret = pinctrl_utils_add_map_configs(pctldev, map,
186 reserved_maps, num_maps, group,
187 configs, num_configs,
188 PIN_MAP_TYPE_CONFIGS_GROUP);
201 static int tegra_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
202 struct device_node *np_config,
203 struct pinctrl_map **map,
206 unsigned reserved_maps;
207 struct device_node *np;
214 for_each_child_of_node(np_config, np) {
215 ret = tegra_pinctrl_dt_subnode_to_map(pctldev, np, map,
216 &reserved_maps, num_maps);
218 pinctrl_utils_dt_free_map(pctldev, *map,
227 static const struct pinctrl_ops tegra_pinctrl_ops = {
228 .get_groups_count = tegra_pinctrl_get_groups_count,
229 .get_group_name = tegra_pinctrl_get_group_name,
230 .get_group_pins = tegra_pinctrl_get_group_pins,
231 #ifdef CONFIG_DEBUG_FS
232 .pin_dbg_show = tegra_pinctrl_pin_dbg_show,
234 .dt_node_to_map = tegra_pinctrl_dt_node_to_map,
235 .dt_free_map = pinctrl_utils_dt_free_map,
238 static int tegra_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
240 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
242 return pmx->soc->nfunctions;
245 static const char *tegra_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
248 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
250 return pmx->soc->functions[function].name;
253 static int tegra_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
255 const char * const **groups,
256 unsigned * const num_groups)
258 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
260 *groups = pmx->soc->functions[function].groups;
261 *num_groups = pmx->soc->functions[function].ngroups;
266 static int tegra_pinctrl_set_mux(struct pinctrl_dev *pctldev,
270 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
271 const struct tegra_pingroup *g;
275 g = &pmx->soc->groups[group];
277 if (WARN_ON(g->mux_reg < 0))
280 for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
281 if (g->funcs[i] == function)
284 if (WARN_ON(i == ARRAY_SIZE(g->funcs)))
287 val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
288 val &= ~(0x3 << g->mux_bit);
289 val |= i << g->mux_bit;
290 pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
295 static const struct pinmux_ops tegra_pinmux_ops = {
296 .get_functions_count = tegra_pinctrl_get_funcs_count,
297 .get_function_name = tegra_pinctrl_get_func_name,
298 .get_function_groups = tegra_pinctrl_get_func_groups,
299 .set_mux = tegra_pinctrl_set_mux,
302 static int tegra_pinconf_reg(struct tegra_pmx *pmx,
303 const struct tegra_pingroup *g,
304 enum tegra_pinconf_param param,
306 s8 *bank, s16 *reg, s8 *bit, s8 *width)
309 case TEGRA_PINCONF_PARAM_PULL:
310 *bank = g->pupd_bank;
315 case TEGRA_PINCONF_PARAM_TRISTATE:
321 case TEGRA_PINCONF_PARAM_ENABLE_INPUT:
324 *bit = g->einput_bit;
327 case TEGRA_PINCONF_PARAM_OPEN_DRAIN:
330 *bit = g->odrain_bit;
333 case TEGRA_PINCONF_PARAM_LOCK:
339 case TEGRA_PINCONF_PARAM_IORESET:
342 *bit = g->ioreset_bit;
345 case TEGRA_PINCONF_PARAM_RCV_SEL:
348 *bit = g->rcv_sel_bit;
351 case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE:
352 if (pmx->soc->hsm_in_mux) {
362 case TEGRA_PINCONF_PARAM_SCHMITT:
363 if (pmx->soc->schmitt_in_mux) {
370 *bit = g->schmitt_bit;
373 case TEGRA_PINCONF_PARAM_LOW_POWER_MODE:
379 case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH:
383 *width = g->drvdn_width;
385 case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH:
389 *width = g->drvup_width;
391 case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING:
395 *width = g->slwf_width;
397 case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING:
401 *width = g->slwr_width;
403 case TEGRA_PINCONF_PARAM_DRIVE_TYPE:
404 if (pmx->soc->drvtype_in_mux) {
411 *bit = g->drvtype_bit;
415 dev_err(pmx->dev, "Invalid config param %04x\n", param);
419 if (*reg < 0 || *bit > 31) {
421 const char *prop = "unknown";
424 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
425 if (cfg_params[i].param == param) {
426 prop = cfg_params[i].property;
432 "Config param %04x (%s) not supported on group %s\n",
433 param, prop, g->name);
441 static int tegra_pinconf_get(struct pinctrl_dev *pctldev,
442 unsigned pin, unsigned long *config)
444 dev_err(pctldev->dev, "pin_config_get op not supported\n");
448 static int tegra_pinconf_set(struct pinctrl_dev *pctldev,
449 unsigned pin, unsigned long *configs,
450 unsigned num_configs)
452 dev_err(pctldev->dev, "pin_config_set op not supported\n");
456 static int tegra_pinconf_group_get(struct pinctrl_dev *pctldev,
457 unsigned group, unsigned long *config)
459 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
460 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(*config);
462 const struct tegra_pingroup *g;
468 g = &pmx->soc->groups[group];
470 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, ®, &bit,
475 val = pmx_readl(pmx, bank, reg);
476 mask = (1 << width) - 1;
477 arg = (val >> bit) & mask;
479 *config = TEGRA_PINCONF_PACK(param, arg);
484 static int tegra_pinconf_group_set(struct pinctrl_dev *pctldev,
485 unsigned group, unsigned long *configs,
486 unsigned num_configs)
488 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
489 enum tegra_pinconf_param param;
491 const struct tegra_pingroup *g;
497 g = &pmx->soc->groups[group];
499 for (i = 0; i < num_configs; i++) {
500 param = TEGRA_PINCONF_UNPACK_PARAM(configs[i]);
501 arg = TEGRA_PINCONF_UNPACK_ARG(configs[i]);
503 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, ®, &bit,
508 val = pmx_readl(pmx, bank, reg);
510 /* LOCK can't be cleared */
511 if (param == TEGRA_PINCONF_PARAM_LOCK) {
512 if ((val & BIT(bit)) && !arg) {
513 dev_err(pctldev->dev, "LOCK bit cannot be cleared\n");
518 /* Special-case Boolean values; allow any non-zero as true */
522 /* Range-check user-supplied value */
523 mask = (1 << width) - 1;
525 dev_err(pctldev->dev,
526 "config %lx: %x too big for %d bit register\n",
527 configs[i], arg, width);
531 /* Update register */
532 val &= ~(mask << bit);
534 pmx_writel(pmx, val, bank, reg);
535 } /* for each config */
540 #ifdef CONFIG_DEBUG_FS
541 static void tegra_pinconf_dbg_show(struct pinctrl_dev *pctldev,
542 struct seq_file *s, unsigned offset)
546 static const char *strip_prefix(const char *s)
548 const char *comma = strchr(s, ',');
555 static void tegra_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
556 struct seq_file *s, unsigned group)
558 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
559 const struct tegra_pingroup *g;
565 g = &pmx->soc->groups[group];
567 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
568 ret = tegra_pinconf_reg(pmx, g, cfg_params[i].param, false,
569 &bank, ®, &bit, &width);
573 val = pmx_readl(pmx, bank, reg);
575 val &= (1 << width) - 1;
577 seq_printf(s, "\n\t%s=%u",
578 strip_prefix(cfg_params[i].property), val);
582 static void tegra_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
584 unsigned long config)
586 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(config);
587 u16 arg = TEGRA_PINCONF_UNPACK_ARG(config);
588 const char *pname = "unknown";
591 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
592 if (cfg_params[i].param == param) {
593 pname = cfg_params[i].property;
598 seq_printf(s, "%s=%d", strip_prefix(pname), arg);
602 static const struct pinconf_ops tegra_pinconf_ops = {
603 .pin_config_get = tegra_pinconf_get,
604 .pin_config_set = tegra_pinconf_set,
605 .pin_config_group_get = tegra_pinconf_group_get,
606 .pin_config_group_set = tegra_pinconf_group_set,
607 #ifdef CONFIG_DEBUG_FS
608 .pin_config_dbg_show = tegra_pinconf_dbg_show,
609 .pin_config_group_dbg_show = tegra_pinconf_group_dbg_show,
610 .pin_config_config_dbg_show = tegra_pinconf_config_dbg_show,
614 static struct pinctrl_gpio_range tegra_pinctrl_gpio_range = {
615 .name = "Tegra GPIOs",
620 static struct pinctrl_desc tegra_pinctrl_desc = {
621 .pctlops = &tegra_pinctrl_ops,
622 .pmxops = &tegra_pinmux_ops,
623 .confops = &tegra_pinconf_ops,
624 .owner = THIS_MODULE,
627 int tegra_pinctrl_probe(struct platform_device *pdev,
628 const struct tegra_pinctrl_soc_data *soc_data)
630 struct tegra_pmx *pmx;
631 struct resource *res;
633 const char **group_pins;
636 pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
638 dev_err(&pdev->dev, "Can't alloc tegra_pmx\n");
641 pmx->dev = &pdev->dev;
645 * Each mux group will appear in 4 functions' list of groups.
646 * This over-allocates slightly, since not all groups are mux groups.
648 pmx->group_pins = devm_kzalloc(&pdev->dev,
649 soc_data->ngroups * 4 * sizeof(*pmx->group_pins),
651 if (!pmx->group_pins)
654 group_pins = pmx->group_pins;
655 for (fn = 0; fn < soc_data->nfunctions; fn++) {
656 struct tegra_function *func = &soc_data->functions[fn];
658 func->groups = group_pins;
660 for (gn = 0; gn < soc_data->ngroups; gn++) {
661 const struct tegra_pingroup *g = &soc_data->groups[gn];
663 if (g->mux_reg == -1)
666 for (gfn = 0; gfn < 4; gfn++)
667 if (g->funcs[gfn] == fn)
672 BUG_ON(group_pins - pmx->group_pins >=
673 soc_data->ngroups * 4);
674 *group_pins++ = g->name;
679 tegra_pinctrl_gpio_range.npins = pmx->soc->ngpios;
680 tegra_pinctrl_desc.name = dev_name(&pdev->dev);
681 tegra_pinctrl_desc.pins = pmx->soc->pins;
682 tegra_pinctrl_desc.npins = pmx->soc->npins;
685 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
691 pmx->regs = devm_kzalloc(&pdev->dev, pmx->nbanks * sizeof(*pmx->regs),
694 dev_err(&pdev->dev, "Can't alloc regs pointer\n");
698 for (i = 0; i < pmx->nbanks; i++) {
699 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
700 pmx->regs[i] = devm_ioremap_resource(&pdev->dev, res);
701 if (IS_ERR(pmx->regs[i]))
702 return PTR_ERR(pmx->regs[i]);
705 pmx->pctl = pinctrl_register(&tegra_pinctrl_desc, &pdev->dev, pmx);
707 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
711 pinctrl_add_gpio_range(pmx->pctl, &tegra_pinctrl_gpio_range);
713 platform_set_drvdata(pdev, pmx);
715 dev_dbg(&pdev->dev, "Probed Tegra pinctrl driver\n");
719 EXPORT_SYMBOL_GPL(tegra_pinctrl_probe);
721 int tegra_pinctrl_remove(struct platform_device *pdev)
723 struct tegra_pmx *pmx = platform_get_drvdata(pdev);
725 pinctrl_unregister(pmx->pctl);
729 EXPORT_SYMBOL_GPL(tegra_pinctrl_remove);