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-utils.h"
35 #include "pinctrl-tegra.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_relaxed(val, pmx->regs[bank] + reg);
56 /* make sure pinmux register write completed */
57 pmx_readl(pmx, bank, reg);
60 static int tegra_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
62 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
64 return pmx->soc->ngroups;
67 static const char *tegra_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
70 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
72 return pmx->soc->groups[group].name;
75 static int tegra_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
77 const unsigned **pins,
80 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
82 *pins = pmx->soc->groups[group].pins;
83 *num_pins = pmx->soc->groups[group].npins;
88 #ifdef CONFIG_DEBUG_FS
89 static void tegra_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
93 seq_printf(s, " %s", dev_name(pctldev->dev));
97 static const struct cfg_param {
99 enum tegra_pinconf_param param;
101 {"nvidia,pull", TEGRA_PINCONF_PARAM_PULL},
102 {"nvidia,tristate", TEGRA_PINCONF_PARAM_TRISTATE},
103 {"nvidia,enable-input", TEGRA_PINCONF_PARAM_ENABLE_INPUT},
104 {"nvidia,open-drain", TEGRA_PINCONF_PARAM_OPEN_DRAIN},
105 {"nvidia,lock", TEGRA_PINCONF_PARAM_LOCK},
106 {"nvidia,io-reset", TEGRA_PINCONF_PARAM_IORESET},
107 {"nvidia,rcv-sel", TEGRA_PINCONF_PARAM_RCV_SEL},
108 {"nvidia,io-hv", TEGRA_PINCONF_PARAM_RCV_SEL},
109 {"nvidia,high-speed-mode", TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE},
110 {"nvidia,schmitt", TEGRA_PINCONF_PARAM_SCHMITT},
111 {"nvidia,low-power-mode", TEGRA_PINCONF_PARAM_LOW_POWER_MODE},
112 {"nvidia,pull-down-strength", TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH},
113 {"nvidia,pull-up-strength", TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH},
114 {"nvidia,slew-rate-falling", TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING},
115 {"nvidia,slew-rate-rising", TEGRA_PINCONF_PARAM_SLEW_RATE_RISING},
116 {"nvidia,drive-type", TEGRA_PINCONF_PARAM_DRIVE_TYPE},
119 static int tegra_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
120 struct device_node *np,
121 struct pinctrl_map **map,
122 unsigned *reserved_maps,
125 struct device *dev = pctldev->dev;
127 const char *function;
129 unsigned long config;
130 unsigned long *configs = NULL;
131 unsigned num_configs = 0;
133 struct property *prop;
136 ret = of_property_read_string(np, "nvidia,function", &function);
138 /* EINVAL=missing, which is fine since it's optional */
141 "could not parse property nvidia,function\n");
145 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
146 ret = of_property_read_u32(np, cfg_params[i].property, &val);
148 config = TEGRA_PINCONF_PACK(cfg_params[i].param, val);
149 ret = pinctrl_utils_add_config(pctldev, &configs,
150 &num_configs, config);
153 /* EINVAL=missing, which is fine since it's optional */
154 } else if (ret != -EINVAL) {
155 dev_err(dev, "could not parse property %s\n",
156 cfg_params[i].property);
161 if (function != NULL)
165 ret = of_property_count_strings(np, "nvidia,pins");
167 dev_err(dev, "could not parse property nvidia,pins\n");
172 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
177 of_property_for_each_string(np, "nvidia,pins", prop, group) {
179 ret = pinctrl_utils_add_map_mux(pctldev, map,
180 reserved_maps, num_maps, group,
187 ret = pinctrl_utils_add_map_configs(pctldev, map,
188 reserved_maps, num_maps, group,
189 configs, num_configs,
190 PIN_MAP_TYPE_CONFIGS_GROUP);
203 static int tegra_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
204 struct device_node *np_config,
205 struct pinctrl_map **map,
208 unsigned reserved_maps;
209 struct device_node *np;
216 for_each_child_of_node(np_config, np) {
217 ret = tegra_pinctrl_dt_subnode_to_map(pctldev, np, map,
218 &reserved_maps, num_maps);
220 pinctrl_utils_free_map(pctldev, *map,
230 static const struct pinctrl_ops tegra_pinctrl_ops = {
231 .get_groups_count = tegra_pinctrl_get_groups_count,
232 .get_group_name = tegra_pinctrl_get_group_name,
233 .get_group_pins = tegra_pinctrl_get_group_pins,
234 #ifdef CONFIG_DEBUG_FS
235 .pin_dbg_show = tegra_pinctrl_pin_dbg_show,
237 .dt_node_to_map = tegra_pinctrl_dt_node_to_map,
238 .dt_free_map = pinctrl_utils_free_map,
241 static int tegra_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
243 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
245 return pmx->soc->nfunctions;
248 static const char *tegra_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
251 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
253 return pmx->soc->functions[function].name;
256 static int tegra_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
258 const char * const **groups,
259 unsigned * const num_groups)
261 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
263 *groups = pmx->soc->functions[function].groups;
264 *num_groups = pmx->soc->functions[function].ngroups;
269 static int tegra_pinctrl_set_mux(struct pinctrl_dev *pctldev,
273 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
274 const struct tegra_pingroup *g;
278 g = &pmx->soc->groups[group];
280 if (WARN_ON(g->mux_reg < 0))
283 for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
284 if (g->funcs[i] == function)
287 if (WARN_ON(i == ARRAY_SIZE(g->funcs)))
290 val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
291 val &= ~(0x3 << g->mux_bit);
292 val |= i << g->mux_bit;
293 pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
298 static const struct pinmux_ops tegra_pinmux_ops = {
299 .get_functions_count = tegra_pinctrl_get_funcs_count,
300 .get_function_name = tegra_pinctrl_get_func_name,
301 .get_function_groups = tegra_pinctrl_get_func_groups,
302 .set_mux = tegra_pinctrl_set_mux,
305 static int tegra_pinconf_reg(struct tegra_pmx *pmx,
306 const struct tegra_pingroup *g,
307 enum tegra_pinconf_param param,
309 s8 *bank, s16 *reg, s8 *bit, s8 *width)
312 case TEGRA_PINCONF_PARAM_PULL:
313 *bank = g->pupd_bank;
318 case TEGRA_PINCONF_PARAM_TRISTATE:
324 case TEGRA_PINCONF_PARAM_ENABLE_INPUT:
327 *bit = g->einput_bit;
330 case TEGRA_PINCONF_PARAM_OPEN_DRAIN:
333 *bit = g->odrain_bit;
336 case TEGRA_PINCONF_PARAM_LOCK:
342 case TEGRA_PINCONF_PARAM_IORESET:
345 *bit = g->ioreset_bit;
348 case TEGRA_PINCONF_PARAM_RCV_SEL:
351 *bit = g->rcv_sel_bit;
354 case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE:
355 if (pmx->soc->hsm_in_mux) {
365 case TEGRA_PINCONF_PARAM_SCHMITT:
366 if (pmx->soc->schmitt_in_mux) {
373 *bit = g->schmitt_bit;
376 case TEGRA_PINCONF_PARAM_LOW_POWER_MODE:
382 case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH:
386 *width = g->drvdn_width;
388 case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH:
392 *width = g->drvup_width;
394 case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING:
398 *width = g->slwf_width;
400 case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING:
404 *width = g->slwr_width;
406 case TEGRA_PINCONF_PARAM_DRIVE_TYPE:
407 if (pmx->soc->drvtype_in_mux) {
414 *bit = g->drvtype_bit;
418 dev_err(pmx->dev, "Invalid config param %04x\n", param);
422 if (*reg < 0 || *bit < 0) {
424 const char *prop = "unknown";
427 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
428 if (cfg_params[i].param == param) {
429 prop = cfg_params[i].property;
435 "Config param %04x (%s) not supported on group %s\n",
436 param, prop, g->name);
444 static int tegra_pinconf_get(struct pinctrl_dev *pctldev,
445 unsigned pin, unsigned long *config)
447 dev_err(pctldev->dev, "pin_config_get op not supported\n");
451 static int tegra_pinconf_set(struct pinctrl_dev *pctldev,
452 unsigned pin, unsigned long *configs,
453 unsigned num_configs)
455 dev_err(pctldev->dev, "pin_config_set op not supported\n");
459 static int tegra_pinconf_group_get(struct pinctrl_dev *pctldev,
460 unsigned group, unsigned long *config)
462 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
463 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(*config);
465 const struct tegra_pingroup *g;
471 g = &pmx->soc->groups[group];
473 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, ®, &bit,
478 val = pmx_readl(pmx, bank, reg);
479 mask = (1 << width) - 1;
480 arg = (val >> bit) & mask;
482 *config = TEGRA_PINCONF_PACK(param, arg);
487 static int tegra_pinconf_group_set(struct pinctrl_dev *pctldev,
488 unsigned group, unsigned long *configs,
489 unsigned num_configs)
491 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
492 enum tegra_pinconf_param param;
494 const struct tegra_pingroup *g;
500 g = &pmx->soc->groups[group];
502 for (i = 0; i < num_configs; i++) {
503 param = TEGRA_PINCONF_UNPACK_PARAM(configs[i]);
504 arg = TEGRA_PINCONF_UNPACK_ARG(configs[i]);
506 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, ®, &bit,
511 val = pmx_readl(pmx, bank, reg);
513 /* LOCK can't be cleared */
514 if (param == TEGRA_PINCONF_PARAM_LOCK) {
515 if ((val & BIT(bit)) && !arg) {
516 dev_err(pctldev->dev, "LOCK bit cannot be cleared\n");
521 /* Special-case Boolean values; allow any non-zero as true */
525 /* Range-check user-supplied value */
526 mask = (1 << width) - 1;
528 dev_err(pctldev->dev,
529 "config %lx: %x too big for %d bit register\n",
530 configs[i], arg, width);
534 /* Update register */
535 val &= ~(mask << bit);
537 pmx_writel(pmx, val, bank, reg);
538 } /* for each config */
543 #ifdef CONFIG_DEBUG_FS
544 static void tegra_pinconf_dbg_show(struct pinctrl_dev *pctldev,
545 struct seq_file *s, unsigned offset)
549 static const char *strip_prefix(const char *s)
551 const char *comma = strchr(s, ',');
558 static void tegra_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
559 struct seq_file *s, unsigned group)
561 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
562 const struct tegra_pingroup *g;
568 g = &pmx->soc->groups[group];
570 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
571 ret = tegra_pinconf_reg(pmx, g, cfg_params[i].param, false,
572 &bank, ®, &bit, &width);
576 val = pmx_readl(pmx, bank, reg);
578 val &= (1 << width) - 1;
580 seq_printf(s, "\n\t%s=%u",
581 strip_prefix(cfg_params[i].property), val);
585 static void tegra_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
587 unsigned long config)
589 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(config);
590 u16 arg = TEGRA_PINCONF_UNPACK_ARG(config);
591 const char *pname = "unknown";
594 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
595 if (cfg_params[i].param == param) {
596 pname = cfg_params[i].property;
601 seq_printf(s, "%s=%d", strip_prefix(pname), arg);
605 static const struct pinconf_ops tegra_pinconf_ops = {
606 .pin_config_get = tegra_pinconf_get,
607 .pin_config_set = tegra_pinconf_set,
608 .pin_config_group_get = tegra_pinconf_group_get,
609 .pin_config_group_set = tegra_pinconf_group_set,
610 #ifdef CONFIG_DEBUG_FS
611 .pin_config_dbg_show = tegra_pinconf_dbg_show,
612 .pin_config_group_dbg_show = tegra_pinconf_group_dbg_show,
613 .pin_config_config_dbg_show = tegra_pinconf_config_dbg_show,
617 static struct pinctrl_gpio_range tegra_pinctrl_gpio_range = {
618 .name = "Tegra GPIOs",
623 static struct pinctrl_desc tegra_pinctrl_desc = {
624 .pctlops = &tegra_pinctrl_ops,
625 .pmxops = &tegra_pinmux_ops,
626 .confops = &tegra_pinconf_ops,
627 .owner = THIS_MODULE,
630 static void tegra_pinctrl_clear_parked_bits(struct tegra_pmx *pmx)
633 const struct tegra_pingroup *g;
636 for (i = 0; i < pmx->soc->ngroups; ++i) {
637 g = &pmx->soc->groups[i];
638 if (g->parked_bit >= 0) {
639 val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
640 val &= ~(1 << g->parked_bit);
641 pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
646 static bool gpio_node_has_range(void)
648 struct device_node *np;
649 bool has_prop = false;
651 np = of_find_compatible_node(NULL, NULL, "nvidia,tegra30-gpio");
655 has_prop = of_find_property(np, "gpio-ranges", NULL);
662 int tegra_pinctrl_probe(struct platform_device *pdev,
663 const struct tegra_pinctrl_soc_data *soc_data)
665 struct tegra_pmx *pmx;
666 struct resource *res;
668 const char **group_pins;
671 pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
673 dev_err(&pdev->dev, "Can't alloc tegra_pmx\n");
676 pmx->dev = &pdev->dev;
680 * Each mux group will appear in 4 functions' list of groups.
681 * This over-allocates slightly, since not all groups are mux groups.
683 pmx->group_pins = devm_kzalloc(&pdev->dev,
684 soc_data->ngroups * 4 * sizeof(*pmx->group_pins),
686 if (!pmx->group_pins)
689 group_pins = pmx->group_pins;
690 for (fn = 0; fn < soc_data->nfunctions; fn++) {
691 struct tegra_function *func = &soc_data->functions[fn];
693 func->groups = group_pins;
695 for (gn = 0; gn < soc_data->ngroups; gn++) {
696 const struct tegra_pingroup *g = &soc_data->groups[gn];
698 if (g->mux_reg == -1)
701 for (gfn = 0; gfn < 4; gfn++)
702 if (g->funcs[gfn] == fn)
707 BUG_ON(group_pins - pmx->group_pins >=
708 soc_data->ngroups * 4);
709 *group_pins++ = g->name;
714 tegra_pinctrl_gpio_range.npins = pmx->soc->ngpios;
715 tegra_pinctrl_desc.name = dev_name(&pdev->dev);
716 tegra_pinctrl_desc.pins = pmx->soc->pins;
717 tegra_pinctrl_desc.npins = pmx->soc->npins;
720 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
726 pmx->regs = devm_kzalloc(&pdev->dev, pmx->nbanks * sizeof(*pmx->regs),
729 dev_err(&pdev->dev, "Can't alloc regs pointer\n");
733 for (i = 0; i < pmx->nbanks; i++) {
734 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
735 pmx->regs[i] = devm_ioremap_resource(&pdev->dev, res);
736 if (IS_ERR(pmx->regs[i]))
737 return PTR_ERR(pmx->regs[i]);
740 pmx->pctl = devm_pinctrl_register(&pdev->dev, &tegra_pinctrl_desc, pmx);
741 if (IS_ERR(pmx->pctl)) {
742 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
743 return PTR_ERR(pmx->pctl);
746 tegra_pinctrl_clear_parked_bits(pmx);
748 if (!gpio_node_has_range())
749 pinctrl_add_gpio_range(pmx->pctl, &tegra_pinctrl_gpio_range);
751 platform_set_drvdata(pdev, pmx);
753 dev_dbg(&pdev->dev, "Probed Tegra pinctrl driver\n");
757 EXPORT_SYMBOL_GPL(tegra_pinctrl_probe);