1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the NVIDIA Tegra pinmux
5 * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved.
8 * Copyright (C) 2010 Google, Inc.
9 * Copyright (C) 2010 NVIDIA Corporation
10 * Copyright (C) 2009-2011 ST-Ericsson AB
13 #include <linux/err.h>
14 #include <linux/init.h>
17 #include <linux/platform_device.h>
18 #include <linux/seq_file.h>
19 #include <linux/slab.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>
27 #include "../pinctrl-utils.h"
28 #include "pinctrl-tegra.h"
30 static inline u32 pmx_readl(struct tegra_pmx *pmx, u32 bank, u32 reg)
32 return readl(pmx->regs[bank] + reg);
35 static inline void pmx_writel(struct tegra_pmx *pmx, u32 val, u32 bank, u32 reg)
37 writel_relaxed(val, pmx->regs[bank] + reg);
38 /* make sure pinmux register write completed */
39 pmx_readl(pmx, bank, reg);
42 static int tegra_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
44 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
46 return pmx->soc->ngroups;
49 static const char *tegra_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
52 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
54 return pmx->soc->groups[group].name;
57 static int tegra_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
59 const unsigned **pins,
62 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
64 *pins = pmx->soc->groups[group].pins;
65 *num_pins = pmx->soc->groups[group].npins;
70 #ifdef CONFIG_DEBUG_FS
71 static void tegra_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
75 seq_printf(s, " %s", dev_name(pctldev->dev));
79 static const struct cfg_param {
81 enum tegra_pinconf_param param;
83 {"nvidia,pull", TEGRA_PINCONF_PARAM_PULL},
84 {"nvidia,tristate", TEGRA_PINCONF_PARAM_TRISTATE},
85 {"nvidia,enable-input", TEGRA_PINCONF_PARAM_ENABLE_INPUT},
86 {"nvidia,open-drain", TEGRA_PINCONF_PARAM_OPEN_DRAIN},
87 {"nvidia,lock", TEGRA_PINCONF_PARAM_LOCK},
88 {"nvidia,io-reset", TEGRA_PINCONF_PARAM_IORESET},
89 {"nvidia,rcv-sel", TEGRA_PINCONF_PARAM_RCV_SEL},
90 {"nvidia,io-hv", TEGRA_PINCONF_PARAM_RCV_SEL},
91 {"nvidia,high-speed-mode", TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE},
92 {"nvidia,schmitt", TEGRA_PINCONF_PARAM_SCHMITT},
93 {"nvidia,low-power-mode", TEGRA_PINCONF_PARAM_LOW_POWER_MODE},
94 {"nvidia,pull-down-strength", TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH},
95 {"nvidia,pull-up-strength", TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH},
96 {"nvidia,slew-rate-falling", TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING},
97 {"nvidia,slew-rate-rising", TEGRA_PINCONF_PARAM_SLEW_RATE_RISING},
98 {"nvidia,drive-type", TEGRA_PINCONF_PARAM_DRIVE_TYPE},
99 {"nvidia,function", TEGRA_PINCONF_PARAM_FUNCTION},
102 static int tegra_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
103 struct device_node *np,
104 struct pinctrl_map **map,
105 unsigned *reserved_maps,
108 struct device *dev = pctldev->dev;
110 const char *function;
112 unsigned long config;
113 unsigned long *configs = NULL;
114 unsigned num_configs = 0;
116 struct property *prop;
119 ret = of_property_read_string(np, "nvidia,function", &function);
121 /* EINVAL=missing, which is fine since it's optional */
124 "could not parse property nvidia,function\n");
128 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
129 ret = of_property_read_u32(np, cfg_params[i].property, &val);
131 config = TEGRA_PINCONF_PACK(cfg_params[i].param, val);
132 ret = pinctrl_utils_add_config(pctldev, &configs,
133 &num_configs, config);
136 /* EINVAL=missing, which is fine since it's optional */
137 } else if (ret != -EINVAL) {
138 dev_err(dev, "could not parse property %s\n",
139 cfg_params[i].property);
144 if (function != NULL)
148 ret = of_property_count_strings(np, "nvidia,pins");
150 dev_err(dev, "could not parse property nvidia,pins\n");
155 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
160 of_property_for_each_string(np, "nvidia,pins", prop, group) {
162 ret = pinctrl_utils_add_map_mux(pctldev, map,
163 reserved_maps, num_maps, group,
170 ret = pinctrl_utils_add_map_configs(pctldev, map,
171 reserved_maps, num_maps, group,
172 configs, num_configs,
173 PIN_MAP_TYPE_CONFIGS_GROUP);
186 static int tegra_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
187 struct device_node *np_config,
188 struct pinctrl_map **map,
191 unsigned reserved_maps;
192 struct device_node *np;
199 for_each_child_of_node(np_config, np) {
200 ret = tegra_pinctrl_dt_subnode_to_map(pctldev, np, map,
201 &reserved_maps, num_maps);
203 pinctrl_utils_free_map(pctldev, *map,
213 static const struct pinctrl_ops tegra_pinctrl_ops = {
214 .get_groups_count = tegra_pinctrl_get_groups_count,
215 .get_group_name = tegra_pinctrl_get_group_name,
216 .get_group_pins = tegra_pinctrl_get_group_pins,
217 #ifdef CONFIG_DEBUG_FS
218 .pin_dbg_show = tegra_pinctrl_pin_dbg_show,
220 .dt_node_to_map = tegra_pinctrl_dt_node_to_map,
221 .dt_free_map = pinctrl_utils_free_map,
224 static int tegra_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
226 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
228 return pmx->soc->nfunctions;
231 static const char *tegra_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
234 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
236 return pmx->functions[function].name;
239 static int tegra_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
241 const char * const **groups,
242 unsigned * const num_groups)
244 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
246 *groups = pmx->functions[function].groups;
247 *num_groups = pmx->functions[function].ngroups;
252 static int tegra_pinctrl_set_mux(struct pinctrl_dev *pctldev,
256 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
257 const struct tegra_pingroup *g;
261 g = &pmx->soc->groups[group];
263 if (WARN_ON(g->mux_reg < 0))
266 for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
267 if (g->funcs[i] == function)
270 if (WARN_ON(i == ARRAY_SIZE(g->funcs)))
273 val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
274 val &= ~(0x3 << g->mux_bit);
275 val |= i << g->mux_bit;
276 pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
281 static const struct tegra_pingroup *tegra_pinctrl_get_group(struct pinctrl_dev *pctldev,
284 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
285 unsigned int group, num_pins, j;
286 const unsigned int *pins;
289 for (group = 0; group < pmx->soc->ngroups; ++group) {
290 ret = tegra_pinctrl_get_group_pins(pctldev, group, &pins, &num_pins);
293 for (j = 0; j < num_pins; j++) {
294 if (offset == pins[j])
295 return &pmx->soc->groups[group];
299 dev_err(pctldev->dev, "Pingroup not found for pin %u\n", offset);
303 static int tegra_pinctrl_gpio_request_enable(struct pinctrl_dev *pctldev,
304 struct pinctrl_gpio_range *range,
307 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
308 const struct tegra_pingroup *group;
311 if (!pmx->soc->sfsel_in_mux)
314 group = tegra_pinctrl_get_group(pctldev, offset);
319 if (group->mux_reg < 0 || group->sfsel_bit < 0)
322 value = pmx_readl(pmx, group->mux_bank, group->mux_reg);
323 value &= ~BIT(group->sfsel_bit);
324 pmx_writel(pmx, value, group->mux_bank, group->mux_reg);
329 static void tegra_pinctrl_gpio_disable_free(struct pinctrl_dev *pctldev,
330 struct pinctrl_gpio_range *range,
333 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
334 const struct tegra_pingroup *group;
337 if (!pmx->soc->sfsel_in_mux)
340 group = tegra_pinctrl_get_group(pctldev, offset);
345 if (group->mux_reg < 0 || group->sfsel_bit < 0)
348 value = pmx_readl(pmx, group->mux_bank, group->mux_reg);
349 value |= BIT(group->sfsel_bit);
350 pmx_writel(pmx, value, group->mux_bank, group->mux_reg);
353 static const struct pinmux_ops tegra_pinmux_ops = {
354 .get_functions_count = tegra_pinctrl_get_funcs_count,
355 .get_function_name = tegra_pinctrl_get_func_name,
356 .get_function_groups = tegra_pinctrl_get_func_groups,
357 .set_mux = tegra_pinctrl_set_mux,
358 .gpio_request_enable = tegra_pinctrl_gpio_request_enable,
359 .gpio_disable_free = tegra_pinctrl_gpio_disable_free,
362 static int tegra_pinconf_reg(struct tegra_pmx *pmx,
363 const struct tegra_pingroup *g,
364 enum tegra_pinconf_param param,
366 s8 *bank, s32 *reg, s8 *bit, s8 *width)
369 case TEGRA_PINCONF_PARAM_PULL:
370 *bank = g->pupd_bank;
375 case TEGRA_PINCONF_PARAM_TRISTATE:
381 case TEGRA_PINCONF_PARAM_ENABLE_INPUT:
384 *bit = g->einput_bit;
387 case TEGRA_PINCONF_PARAM_OPEN_DRAIN:
390 *bit = g->odrain_bit;
393 case TEGRA_PINCONF_PARAM_LOCK:
399 case TEGRA_PINCONF_PARAM_IORESET:
402 *bit = g->ioreset_bit;
405 case TEGRA_PINCONF_PARAM_RCV_SEL:
408 *bit = g->rcv_sel_bit;
411 case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE:
412 if (pmx->soc->hsm_in_mux) {
422 case TEGRA_PINCONF_PARAM_SCHMITT:
423 if (pmx->soc->schmitt_in_mux) {
430 *bit = g->schmitt_bit;
433 case TEGRA_PINCONF_PARAM_LOW_POWER_MODE:
439 case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH:
443 *width = g->drvdn_width;
445 case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH:
449 *width = g->drvup_width;
451 case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING:
455 *width = g->slwf_width;
457 case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING:
461 *width = g->slwr_width;
463 case TEGRA_PINCONF_PARAM_DRIVE_TYPE:
464 if (pmx->soc->drvtype_in_mux) {
471 *bit = g->drvtype_bit;
474 case TEGRA_PINCONF_PARAM_FUNCTION:
481 dev_err(pmx->dev, "Invalid config param %04x\n", param);
485 if (*reg < 0 || *bit < 0) {
487 const char *prop = "unknown";
490 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
491 if (cfg_params[i].param == param) {
492 prop = cfg_params[i].property;
498 "Config param %04x (%s) not supported on group %s\n",
499 param, prop, g->name);
507 static int tegra_pinconf_get(struct pinctrl_dev *pctldev,
508 unsigned pin, unsigned long *config)
510 dev_err(pctldev->dev, "pin_config_get op not supported\n");
514 static int tegra_pinconf_set(struct pinctrl_dev *pctldev,
515 unsigned pin, unsigned long *configs,
516 unsigned num_configs)
518 dev_err(pctldev->dev, "pin_config_set op not supported\n");
522 static int tegra_pinconf_group_get(struct pinctrl_dev *pctldev,
523 unsigned group, unsigned long *config)
525 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
526 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(*config);
528 const struct tegra_pingroup *g;
534 g = &pmx->soc->groups[group];
536 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, ®, &bit,
541 val = pmx_readl(pmx, bank, reg);
542 mask = (1 << width) - 1;
543 arg = (val >> bit) & mask;
545 *config = TEGRA_PINCONF_PACK(param, arg);
550 static int tegra_pinconf_group_set(struct pinctrl_dev *pctldev,
551 unsigned group, unsigned long *configs,
552 unsigned num_configs)
554 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
555 enum tegra_pinconf_param param;
557 const struct tegra_pingroup *g;
563 g = &pmx->soc->groups[group];
565 for (i = 0; i < num_configs; i++) {
566 param = TEGRA_PINCONF_UNPACK_PARAM(configs[i]);
567 arg = TEGRA_PINCONF_UNPACK_ARG(configs[i]);
569 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, ®, &bit,
574 val = pmx_readl(pmx, bank, reg);
576 /* LOCK can't be cleared */
577 if (param == TEGRA_PINCONF_PARAM_LOCK) {
578 if ((val & BIT(bit)) && !arg) {
579 dev_err(pctldev->dev, "LOCK bit cannot be cleared\n");
584 /* Special-case Boolean values; allow any non-zero as true */
588 /* Range-check user-supplied value */
589 mask = (1 << width) - 1;
591 dev_err(pctldev->dev,
592 "config %lx: %x too big for %d bit register\n",
593 configs[i], arg, width);
597 /* Update register */
598 val &= ~(mask << bit);
600 pmx_writel(pmx, val, bank, reg);
601 } /* for each config */
606 #ifdef CONFIG_DEBUG_FS
607 static void tegra_pinconf_dbg_show(struct pinctrl_dev *pctldev,
608 struct seq_file *s, unsigned offset)
612 static const char *strip_prefix(const char *s)
614 const char *comma = strchr(s, ',');
621 static void tegra_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
622 struct seq_file *s, unsigned group)
624 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
625 const struct tegra_pingroup *g;
631 g = &pmx->soc->groups[group];
633 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
634 ret = tegra_pinconf_reg(pmx, g, cfg_params[i].param, false,
635 &bank, ®, &bit, &width);
639 val = pmx_readl(pmx, bank, reg);
641 val &= (1 << width) - 1;
643 if (cfg_params[i].param == TEGRA_PINCONF_PARAM_FUNCTION) {
644 u8 idx = pmx->soc->groups[group].funcs[val];
646 seq_printf(s, "\n\t%s=%s",
647 strip_prefix(cfg_params[i].property),
648 pmx->functions[idx].name);
650 seq_printf(s, "\n\t%s=%u",
651 strip_prefix(cfg_params[i].property), val);
656 static void tegra_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
658 unsigned long config)
660 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(config);
661 u16 arg = TEGRA_PINCONF_UNPACK_ARG(config);
662 const char *pname = "unknown";
665 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
666 if (cfg_params[i].param == param) {
667 pname = cfg_params[i].property;
672 seq_printf(s, "%s=%d", strip_prefix(pname), arg);
676 static const struct pinconf_ops tegra_pinconf_ops = {
677 .pin_config_get = tegra_pinconf_get,
678 .pin_config_set = tegra_pinconf_set,
679 .pin_config_group_get = tegra_pinconf_group_get,
680 .pin_config_group_set = tegra_pinconf_group_set,
681 #ifdef CONFIG_DEBUG_FS
682 .pin_config_dbg_show = tegra_pinconf_dbg_show,
683 .pin_config_group_dbg_show = tegra_pinconf_group_dbg_show,
684 .pin_config_config_dbg_show = tegra_pinconf_config_dbg_show,
688 static void tegra_pinctrl_clear_parked_bits(struct tegra_pmx *pmx)
691 const struct tegra_pingroup *g;
694 for (i = 0; i < pmx->soc->ngroups; ++i) {
695 g = &pmx->soc->groups[i];
696 if (g->parked_bitmask > 0) {
697 unsigned int bank, reg;
699 if (g->mux_reg != -1) {
707 val = pmx_readl(pmx, bank, reg);
708 val &= ~g->parked_bitmask;
709 pmx_writel(pmx, val, bank, reg);
714 static size_t tegra_pinctrl_get_bank_size(struct device *dev,
715 unsigned int bank_id)
717 struct platform_device *pdev = to_platform_device(dev);
718 struct resource *res;
720 res = platform_get_resource(pdev, IORESOURCE_MEM, bank_id);
722 return resource_size(res) / 4;
725 static int tegra_pinctrl_suspend(struct device *dev)
727 struct tegra_pmx *pmx = dev_get_drvdata(dev);
728 u32 *backup_regs = pmx->backup_regs;
733 for (i = 0; i < pmx->nbanks; i++) {
734 bank_size = tegra_pinctrl_get_bank_size(dev, i);
736 for (k = 0; k < bank_size; k++)
737 *backup_regs++ = readl_relaxed(regs++);
740 return pinctrl_force_sleep(pmx->pctl);
743 static int tegra_pinctrl_resume(struct device *dev)
745 struct tegra_pmx *pmx = dev_get_drvdata(dev);
746 u32 *backup_regs = pmx->backup_regs;
751 for (i = 0; i < pmx->nbanks; i++) {
752 bank_size = tegra_pinctrl_get_bank_size(dev, i);
754 for (k = 0; k < bank_size; k++)
755 writel_relaxed(*backup_regs++, regs++);
758 /* flush all the prior writes */
759 readl_relaxed(pmx->regs[0]);
760 /* wait for pinctrl register read to complete */
765 DEFINE_NOIRQ_DEV_PM_OPS(tegra_pinctrl_pm, tegra_pinctrl_suspend, tegra_pinctrl_resume);
767 static bool tegra_pinctrl_gpio_node_has_range(struct tegra_pmx *pmx)
769 struct device_node *np;
770 bool has_prop = false;
772 np = of_find_compatible_node(NULL, NULL, pmx->soc->gpio_compatible);
776 has_prop = of_find_property(np, "gpio-ranges", NULL);
783 int tegra_pinctrl_probe(struct platform_device *pdev,
784 const struct tegra_pinctrl_soc_data *soc_data)
786 struct tegra_pmx *pmx;
787 struct resource *res;
789 const char **group_pins;
791 unsigned long backup_regs_size = 0;
793 pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
797 pmx->dev = &pdev->dev;
801 * Each mux group will appear in 4 functions' list of groups.
802 * This over-allocates slightly, since not all groups are mux groups.
804 pmx->group_pins = devm_kcalloc(&pdev->dev, pmx->soc->ngroups * 4,
805 sizeof(*pmx->group_pins), GFP_KERNEL);
806 if (!pmx->group_pins)
809 pmx->functions = devm_kcalloc(&pdev->dev, pmx->soc->nfunctions,
810 sizeof(*pmx->functions), GFP_KERNEL);
814 group_pins = pmx->group_pins;
816 for (fn = 0; fn < pmx->soc->nfunctions; fn++) {
817 struct tegra_function *func = &pmx->functions[fn];
819 func->name = pmx->soc->functions[fn];
820 func->groups = group_pins;
822 for (gn = 0; gn < pmx->soc->ngroups; gn++) {
823 const struct tegra_pingroup *g = &pmx->soc->groups[gn];
825 if (g->mux_reg == -1)
828 for (gfn = 0; gfn < 4; gfn++)
829 if (g->funcs[gfn] == fn)
834 BUG_ON(group_pins - pmx->group_pins >=
835 pmx->soc->ngroups * 4);
836 *group_pins++ = g->name;
841 pmx->gpio_range.name = "Tegra GPIOs";
842 pmx->gpio_range.id = 0;
843 pmx->gpio_range.base = 0;
844 pmx->gpio_range.npins = pmx->soc->ngpios;
846 pmx->desc.pctlops = &tegra_pinctrl_ops;
847 pmx->desc.pmxops = &tegra_pinmux_ops;
848 pmx->desc.confops = &tegra_pinconf_ops;
849 pmx->desc.owner = THIS_MODULE;
850 pmx->desc.name = dev_name(&pdev->dev);
851 pmx->desc.pins = pmx->soc->pins;
852 pmx->desc.npins = pmx->soc->npins;
855 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
858 backup_regs_size += resource_size(res);
862 pmx->regs = devm_kcalloc(&pdev->dev, pmx->nbanks, sizeof(*pmx->regs),
867 pmx->backup_regs = devm_kzalloc(&pdev->dev, backup_regs_size,
869 if (!pmx->backup_regs)
872 for (i = 0; i < pmx->nbanks; i++) {
873 pmx->regs[i] = devm_platform_ioremap_resource(pdev, i);
874 if (IS_ERR(pmx->regs[i]))
875 return PTR_ERR(pmx->regs[i]);
878 pmx->pctl = devm_pinctrl_register(&pdev->dev, &pmx->desc, pmx);
879 if (IS_ERR(pmx->pctl)) {
880 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
881 return PTR_ERR(pmx->pctl);
884 tegra_pinctrl_clear_parked_bits(pmx);
886 if (pmx->soc->ngpios > 0 && !tegra_pinctrl_gpio_node_has_range(pmx))
887 pinctrl_add_gpio_range(pmx->pctl, &pmx->gpio_range);
889 platform_set_drvdata(pdev, pmx);
891 dev_dbg(&pdev->dev, "Probed Tegra pinctrl driver\n");