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},
101 static int tegra_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
102 struct device_node *np,
103 struct pinctrl_map **map,
104 unsigned *reserved_maps,
107 struct device *dev = pctldev->dev;
109 const char *function;
111 unsigned long config;
112 unsigned long *configs = NULL;
113 unsigned num_configs = 0;
115 struct property *prop;
118 ret = of_property_read_string(np, "nvidia,function", &function);
120 /* EINVAL=missing, which is fine since it's optional */
123 "could not parse property nvidia,function\n");
127 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
128 ret = of_property_read_u32(np, cfg_params[i].property, &val);
130 config = TEGRA_PINCONF_PACK(cfg_params[i].param, val);
131 ret = pinctrl_utils_add_config(pctldev, &configs,
132 &num_configs, config);
135 /* EINVAL=missing, which is fine since it's optional */
136 } else if (ret != -EINVAL) {
137 dev_err(dev, "could not parse property %s\n",
138 cfg_params[i].property);
143 if (function != NULL)
147 ret = of_property_count_strings(np, "nvidia,pins");
149 dev_err(dev, "could not parse property nvidia,pins\n");
154 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
159 of_property_for_each_string(np, "nvidia,pins", prop, group) {
161 ret = pinctrl_utils_add_map_mux(pctldev, map,
162 reserved_maps, num_maps, group,
169 ret = pinctrl_utils_add_map_configs(pctldev, map,
170 reserved_maps, num_maps, group,
171 configs, num_configs,
172 PIN_MAP_TYPE_CONFIGS_GROUP);
185 static int tegra_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
186 struct device_node *np_config,
187 struct pinctrl_map **map,
190 unsigned reserved_maps;
191 struct device_node *np;
198 for_each_child_of_node(np_config, np) {
199 ret = tegra_pinctrl_dt_subnode_to_map(pctldev, np, map,
200 &reserved_maps, num_maps);
202 pinctrl_utils_free_map(pctldev, *map,
212 static const struct pinctrl_ops tegra_pinctrl_ops = {
213 .get_groups_count = tegra_pinctrl_get_groups_count,
214 .get_group_name = tegra_pinctrl_get_group_name,
215 .get_group_pins = tegra_pinctrl_get_group_pins,
216 #ifdef CONFIG_DEBUG_FS
217 .pin_dbg_show = tegra_pinctrl_pin_dbg_show,
219 .dt_node_to_map = tegra_pinctrl_dt_node_to_map,
220 .dt_free_map = pinctrl_utils_free_map,
223 static int tegra_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
225 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
227 return pmx->soc->nfunctions;
230 static const char *tegra_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
233 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
235 return pmx->functions[function].name;
238 static int tegra_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
240 const char * const **groups,
241 unsigned * const num_groups)
243 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
245 *groups = pmx->functions[function].groups;
246 *num_groups = pmx->functions[function].ngroups;
251 static int tegra_pinctrl_set_mux(struct pinctrl_dev *pctldev,
255 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
256 const struct tegra_pingroup *g;
260 g = &pmx->soc->groups[group];
262 if (WARN_ON(g->mux_reg < 0))
265 for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
266 if (g->funcs[i] == function)
269 if (WARN_ON(i == ARRAY_SIZE(g->funcs)))
272 val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
273 val &= ~(0x3 << g->mux_bit);
274 val |= i << g->mux_bit;
275 pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
280 static const struct tegra_pingroup *tegra_pinctrl_get_group(struct pinctrl_dev *pctldev,
283 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
284 unsigned int group, num_pins, j;
285 const unsigned int *pins;
288 for (group = 0; group < pmx->soc->ngroups; ++group) {
289 ret = tegra_pinctrl_get_group_pins(pctldev, group, &pins, &num_pins);
292 for (j = 0; j < num_pins; j++) {
293 if (offset == pins[j])
294 return &pmx->soc->groups[group];
298 dev_err(pctldev->dev, "Pingroup not found for pin %u\n", offset);
302 static int tegra_pinctrl_gpio_request_enable(struct pinctrl_dev *pctldev,
303 struct pinctrl_gpio_range *range,
306 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
307 const struct tegra_pingroup *group;
310 if (!pmx->soc->sfsel_in_mux)
313 group = tegra_pinctrl_get_group(pctldev, offset);
318 if (group->mux_reg < 0 || group->sfsel_bit < 0)
321 value = pmx_readl(pmx, group->mux_bank, group->mux_reg);
322 value &= ~BIT(group->sfsel_bit);
323 pmx_writel(pmx, value, group->mux_bank, group->mux_reg);
328 static void tegra_pinctrl_gpio_disable_free(struct pinctrl_dev *pctldev,
329 struct pinctrl_gpio_range *range,
332 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
333 const struct tegra_pingroup *group;
336 if (!pmx->soc->sfsel_in_mux)
339 group = tegra_pinctrl_get_group(pctldev, offset);
344 if (group->mux_reg < 0 || group->sfsel_bit < 0)
347 value = pmx_readl(pmx, group->mux_bank, group->mux_reg);
348 value |= BIT(group->sfsel_bit);
349 pmx_writel(pmx, value, group->mux_bank, group->mux_reg);
352 static const struct pinmux_ops tegra_pinmux_ops = {
353 .get_functions_count = tegra_pinctrl_get_funcs_count,
354 .get_function_name = tegra_pinctrl_get_func_name,
355 .get_function_groups = tegra_pinctrl_get_func_groups,
356 .set_mux = tegra_pinctrl_set_mux,
357 .gpio_request_enable = tegra_pinctrl_gpio_request_enable,
358 .gpio_disable_free = tegra_pinctrl_gpio_disable_free,
361 static int tegra_pinconf_reg(struct tegra_pmx *pmx,
362 const struct tegra_pingroup *g,
363 enum tegra_pinconf_param param,
365 s8 *bank, s32 *reg, s8 *bit, s8 *width)
368 case TEGRA_PINCONF_PARAM_PULL:
369 *bank = g->pupd_bank;
374 case TEGRA_PINCONF_PARAM_TRISTATE:
380 case TEGRA_PINCONF_PARAM_ENABLE_INPUT:
383 *bit = g->einput_bit;
386 case TEGRA_PINCONF_PARAM_OPEN_DRAIN:
389 *bit = g->odrain_bit;
392 case TEGRA_PINCONF_PARAM_LOCK:
398 case TEGRA_PINCONF_PARAM_IORESET:
401 *bit = g->ioreset_bit;
404 case TEGRA_PINCONF_PARAM_RCV_SEL:
407 *bit = g->rcv_sel_bit;
410 case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE:
411 if (pmx->soc->hsm_in_mux) {
421 case TEGRA_PINCONF_PARAM_SCHMITT:
422 if (pmx->soc->schmitt_in_mux) {
429 *bit = g->schmitt_bit;
432 case TEGRA_PINCONF_PARAM_LOW_POWER_MODE:
438 case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH:
442 *width = g->drvdn_width;
444 case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH:
448 *width = g->drvup_width;
450 case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING:
454 *width = g->slwf_width;
456 case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING:
460 *width = g->slwr_width;
462 case TEGRA_PINCONF_PARAM_DRIVE_TYPE:
463 if (pmx->soc->drvtype_in_mux) {
470 *bit = g->drvtype_bit;
474 dev_err(pmx->dev, "Invalid config param %04x\n", param);
478 if (*reg < 0 || *bit < 0) {
480 const char *prop = "unknown";
483 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
484 if (cfg_params[i].param == param) {
485 prop = cfg_params[i].property;
491 "Config param %04x (%s) not supported on group %s\n",
492 param, prop, g->name);
500 static int tegra_pinconf_get(struct pinctrl_dev *pctldev,
501 unsigned pin, unsigned long *config)
503 dev_err(pctldev->dev, "pin_config_get op not supported\n");
507 static int tegra_pinconf_set(struct pinctrl_dev *pctldev,
508 unsigned pin, unsigned long *configs,
509 unsigned num_configs)
511 dev_err(pctldev->dev, "pin_config_set op not supported\n");
515 static int tegra_pinconf_group_get(struct pinctrl_dev *pctldev,
516 unsigned group, unsigned long *config)
518 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
519 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(*config);
521 const struct tegra_pingroup *g;
527 g = &pmx->soc->groups[group];
529 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, ®, &bit,
534 val = pmx_readl(pmx, bank, reg);
535 mask = (1 << width) - 1;
536 arg = (val >> bit) & mask;
538 *config = TEGRA_PINCONF_PACK(param, arg);
543 static int tegra_pinconf_group_set(struct pinctrl_dev *pctldev,
544 unsigned group, unsigned long *configs,
545 unsigned num_configs)
547 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
548 enum tegra_pinconf_param param;
550 const struct tegra_pingroup *g;
556 g = &pmx->soc->groups[group];
558 for (i = 0; i < num_configs; i++) {
559 param = TEGRA_PINCONF_UNPACK_PARAM(configs[i]);
560 arg = TEGRA_PINCONF_UNPACK_ARG(configs[i]);
562 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, ®, &bit,
567 val = pmx_readl(pmx, bank, reg);
569 /* LOCK can't be cleared */
570 if (param == TEGRA_PINCONF_PARAM_LOCK) {
571 if ((val & BIT(bit)) && !arg) {
572 dev_err(pctldev->dev, "LOCK bit cannot be cleared\n");
577 /* Special-case Boolean values; allow any non-zero as true */
581 /* Range-check user-supplied value */
582 mask = (1 << width) - 1;
584 dev_err(pctldev->dev,
585 "config %lx: %x too big for %d bit register\n",
586 configs[i], arg, width);
590 /* Update register */
591 val &= ~(mask << bit);
593 pmx_writel(pmx, val, bank, reg);
594 } /* for each config */
599 #ifdef CONFIG_DEBUG_FS
600 static void tegra_pinconf_dbg_show(struct pinctrl_dev *pctldev,
601 struct seq_file *s, unsigned offset)
605 static const char *strip_prefix(const char *s)
607 const char *comma = strchr(s, ',');
614 static void tegra_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
615 struct seq_file *s, unsigned group)
617 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
618 const struct tegra_pingroup *g;
624 g = &pmx->soc->groups[group];
626 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
627 ret = tegra_pinconf_reg(pmx, g, cfg_params[i].param, false,
628 &bank, ®, &bit, &width);
632 val = pmx_readl(pmx, bank, reg);
634 val &= (1 << width) - 1;
636 seq_printf(s, "\n\t%s=%u",
637 strip_prefix(cfg_params[i].property), val);
641 static void tegra_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
643 unsigned long config)
645 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(config);
646 u16 arg = TEGRA_PINCONF_UNPACK_ARG(config);
647 const char *pname = "unknown";
650 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
651 if (cfg_params[i].param == param) {
652 pname = cfg_params[i].property;
657 seq_printf(s, "%s=%d", strip_prefix(pname), arg);
661 static const struct pinconf_ops tegra_pinconf_ops = {
662 .pin_config_get = tegra_pinconf_get,
663 .pin_config_set = tegra_pinconf_set,
664 .pin_config_group_get = tegra_pinconf_group_get,
665 .pin_config_group_set = tegra_pinconf_group_set,
666 #ifdef CONFIG_DEBUG_FS
667 .pin_config_dbg_show = tegra_pinconf_dbg_show,
668 .pin_config_group_dbg_show = tegra_pinconf_group_dbg_show,
669 .pin_config_config_dbg_show = tegra_pinconf_config_dbg_show,
673 static void tegra_pinctrl_clear_parked_bits(struct tegra_pmx *pmx)
676 const struct tegra_pingroup *g;
679 for (i = 0; i < pmx->soc->ngroups; ++i) {
680 g = &pmx->soc->groups[i];
681 if (g->parked_bitmask > 0) {
682 unsigned int bank, reg;
684 if (g->mux_reg != -1) {
692 val = pmx_readl(pmx, bank, reg);
693 val &= ~g->parked_bitmask;
694 pmx_writel(pmx, val, bank, reg);
699 static size_t tegra_pinctrl_get_bank_size(struct device *dev,
700 unsigned int bank_id)
702 struct platform_device *pdev = to_platform_device(dev);
703 struct resource *res;
705 res = platform_get_resource(pdev, IORESOURCE_MEM, bank_id);
707 return resource_size(res) / 4;
710 static int tegra_pinctrl_suspend(struct device *dev)
712 struct tegra_pmx *pmx = dev_get_drvdata(dev);
713 u32 *backup_regs = pmx->backup_regs;
718 for (i = 0; i < pmx->nbanks; i++) {
719 bank_size = tegra_pinctrl_get_bank_size(dev, i);
721 for (k = 0; k < bank_size; k++)
722 *backup_regs++ = readl_relaxed(regs++);
725 return pinctrl_force_sleep(pmx->pctl);
728 static int tegra_pinctrl_resume(struct device *dev)
730 struct tegra_pmx *pmx = dev_get_drvdata(dev);
731 u32 *backup_regs = pmx->backup_regs;
736 for (i = 0; i < pmx->nbanks; i++) {
737 bank_size = tegra_pinctrl_get_bank_size(dev, i);
739 for (k = 0; k < bank_size; k++)
740 writel_relaxed(*backup_regs++, regs++);
743 /* flush all the prior writes */
744 readl_relaxed(pmx->regs[0]);
745 /* wait for pinctrl register read to complete */
750 const struct dev_pm_ops tegra_pinctrl_pm = {
751 .suspend_noirq = &tegra_pinctrl_suspend,
752 .resume_noirq = &tegra_pinctrl_resume
755 static bool tegra_pinctrl_gpio_node_has_range(struct tegra_pmx *pmx)
757 struct device_node *np;
758 bool has_prop = false;
760 np = of_find_compatible_node(NULL, NULL, pmx->soc->gpio_compatible);
764 has_prop = of_find_property(np, "gpio-ranges", NULL);
771 int tegra_pinctrl_probe(struct platform_device *pdev,
772 const struct tegra_pinctrl_soc_data *soc_data)
774 struct tegra_pmx *pmx;
775 struct resource *res;
777 const char **group_pins;
779 unsigned long backup_regs_size = 0;
781 pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
785 pmx->dev = &pdev->dev;
789 * Each mux group will appear in 4 functions' list of groups.
790 * This over-allocates slightly, since not all groups are mux groups.
792 pmx->group_pins = devm_kcalloc(&pdev->dev,
793 soc_data->ngroups * 4, sizeof(*pmx->group_pins),
795 if (!pmx->group_pins)
798 pmx->functions = devm_kcalloc(&pdev->dev, pmx->soc->nfunctions,
799 sizeof(*pmx->functions), GFP_KERNEL);
803 group_pins = pmx->group_pins;
805 for (fn = 0; fn < soc_data->nfunctions; fn++) {
806 struct tegra_function *func = &pmx->functions[fn];
808 func->name = pmx->soc->functions[fn];
809 func->groups = group_pins;
811 for (gn = 0; gn < soc_data->ngroups; gn++) {
812 const struct tegra_pingroup *g = &soc_data->groups[gn];
814 if (g->mux_reg == -1)
817 for (gfn = 0; gfn < 4; gfn++)
818 if (g->funcs[gfn] == fn)
823 BUG_ON(group_pins - pmx->group_pins >=
824 soc_data->ngroups * 4);
825 *group_pins++ = g->name;
830 pmx->gpio_range.name = "Tegra GPIOs";
831 pmx->gpio_range.id = 0;
832 pmx->gpio_range.base = 0;
833 pmx->gpio_range.npins = pmx->soc->ngpios;
835 pmx->desc.pctlops = &tegra_pinctrl_ops;
836 pmx->desc.pmxops = &tegra_pinmux_ops;
837 pmx->desc.confops = &tegra_pinconf_ops;
838 pmx->desc.owner = THIS_MODULE;
839 pmx->desc.name = dev_name(&pdev->dev);
840 pmx->desc.pins = pmx->soc->pins;
841 pmx->desc.npins = pmx->soc->npins;
844 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
847 backup_regs_size += resource_size(res);
851 pmx->regs = devm_kcalloc(&pdev->dev, pmx->nbanks, sizeof(*pmx->regs),
856 pmx->backup_regs = devm_kzalloc(&pdev->dev, backup_regs_size,
858 if (!pmx->backup_regs)
861 for (i = 0; i < pmx->nbanks; i++) {
862 pmx->regs[i] = devm_platform_ioremap_resource(pdev, i);
863 if (IS_ERR(pmx->regs[i]))
864 return PTR_ERR(pmx->regs[i]);
867 pmx->pctl = devm_pinctrl_register(&pdev->dev, &pmx->desc, pmx);
868 if (IS_ERR(pmx->pctl)) {
869 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
870 return PTR_ERR(pmx->pctl);
873 tegra_pinctrl_clear_parked_bits(pmx);
875 if (pmx->soc->ngpios > 0 && !tegra_pinctrl_gpio_node_has_range(pmx))
876 pinctrl_add_gpio_range(pmx->pctl, &pmx->gpio_range);
878 platform_set_drvdata(pdev, pmx);
880 dev_dbg(&pdev->dev, "Probed Tegra pinctrl driver\n");