2 * Driver for the NVIDIA Tegra pinmux
4 * Copyright (c) 2011-2014, 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>
32 #include <linux/syscore_ops.h>
33 #include <linux/uaccess.h>
34 #include <linux/pinctrl/pinctrl-tegra.h>
36 #include <mach/pinconf-tegra.h>
37 #include <mach/pinmux-defines.h>
40 #include "pinctrl-tegra.h"
42 static DEFINE_SPINLOCK(mux_lock);
46 struct pinctrl_dev *pctl;
48 const struct tegra_pinctrl_soc_data *soc;
53 unsigned int *reg_base;
56 unsigned drive_group_start_index;
59 static struct tegra_pmx *pmx;
61 static int tegra_pinconf_group_set(struct pinctrl_dev *pctldev,
62 unsigned group, unsigned long config);
64 static inline u32 pmx_readl(struct tegra_pmx *pmx, u32 bank, u32 reg)
66 return readl(pmx->regs[bank] + reg);
69 static inline void pmx_writel(struct tegra_pmx *pmx, u32 val, u32 bank, u32 reg)
71 writel(val, pmx->regs[bank] + reg);
74 static int tegra_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
76 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
78 return pmx->soc->ngroups;
81 static const char *tegra_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
84 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
86 return pmx->soc->groups[group].name;
89 static int tegra_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
91 const unsigned **pins,
94 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
96 *pins = pmx->soc->groups[group].pins;
97 *num_pins = pmx->soc->groups[group].npins;
102 #ifdef CONFIG_DEBUG_FS
103 static void tegra_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
107 seq_printf(s, " %s", dev_name(pctldev->dev));
111 static int reserve_map(struct device *dev, struct pinctrl_map **map,
112 unsigned *reserved_maps, unsigned *num_maps,
115 unsigned old_num = *reserved_maps;
116 unsigned new_num = *num_maps + reserve;
117 struct pinctrl_map *new_map;
119 if (old_num >= new_num)
122 new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
124 dev_err(dev, "krealloc(map) failed\n");
128 memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
131 *reserved_maps = new_num;
136 static int add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps,
137 unsigned *num_maps, const char *group,
138 const char *function)
140 if (WARN_ON(*num_maps == *reserved_maps))
143 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
144 (*map)[*num_maps].data.mux.group = group;
145 (*map)[*num_maps].data.mux.function = function;
151 static int add_map_configs(struct device *dev, struct pinctrl_map **map,
152 unsigned *reserved_maps, unsigned *num_maps,
153 const char *group, unsigned long *configs,
154 unsigned num_configs)
156 unsigned long *dup_configs;
158 if (WARN_ON(*num_maps == *reserved_maps))
161 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
164 dev_err(dev, "kmemdup(configs) failed\n");
168 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
169 (*map)[*num_maps].data.configs.group_or_pin = group;
170 (*map)[*num_maps].data.configs.configs = dup_configs;
171 (*map)[*num_maps].data.configs.num_configs = num_configs;
177 static int add_config(struct device *dev, unsigned long **configs,
178 unsigned *num_configs, unsigned long config)
180 unsigned old_num = *num_configs;
181 unsigned new_num = old_num + 1;
182 unsigned long *new_configs;
184 new_configs = krealloc(*configs, sizeof(*new_configs) * new_num,
187 dev_err(dev, "krealloc(configs) failed\n");
191 new_configs[old_num] = config;
193 *configs = new_configs;
194 *num_configs = new_num;
199 static void tegra_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
200 struct pinctrl_map *map,
205 for (i = 0; i < num_maps; i++)
206 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
207 kfree(map[i].data.configs.configs);
212 static const struct cfg_param {
213 const char *property;
214 enum tegra_pinconf_param param;
216 {"nvidia,pull", TEGRA_PINCONF_PARAM_PULL},
217 {"nvidia,tristate", TEGRA_PINCONF_PARAM_TRISTATE},
218 {"nvidia,enable-input", TEGRA_PINCONF_PARAM_ENABLE_INPUT},
219 {"nvidia,open-drain", TEGRA_PINCONF_PARAM_OPEN_DRAIN},
220 {"nvidia,lock", TEGRA_PINCONF_PARAM_LOCK},
221 {"nvidia,io-reset", TEGRA_PINCONF_PARAM_IORESET},
222 {"nvidia,rcv-sel", TEGRA_PINCONF_PARAM_RCV_SEL},
223 {"nvidia,high-speed-mode", TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE},
224 {"nvidia,schmitt", TEGRA_PINCONF_PARAM_SCHMITT},
225 {"nvidia,low-power-mode", TEGRA_PINCONF_PARAM_LOW_POWER_MODE},
226 {"nvidia,pull-down-strength", TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH},
227 {"nvidia,pull-up-strength", TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH},
228 {"nvidia,slew-rate-falling", TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING},
229 {"nvidia,slew-rate-rising", TEGRA_PINCONF_PARAM_SLEW_RATE_RISING},
230 {"nvidia,drive-type", TEGRA_PINCONF_PARAM_DRIVE_TYPE},
233 static int tegra_pinctrl_dt_subnode_to_map(struct device *dev,
234 struct device_node *np,
235 struct pinctrl_map **map,
236 unsigned *reserved_maps,
240 const char *function;
242 unsigned long config;
243 unsigned long *configs = NULL;
244 unsigned num_configs = 0;
246 struct property *prop;
249 ret = of_property_read_string(np, "nvidia,function", &function);
251 /* EINVAL=missing, which is fine since it's optional */
254 "could not parse property nvidia,function\n");
258 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
259 ret = of_property_read_u32(np, cfg_params[i].property, &val);
261 config = TEGRA_PINCONF_PACK(cfg_params[i].param, val);
262 ret = add_config(dev, &configs, &num_configs, config);
265 /* EINVAL=missing, which is fine since it's optional */
266 } else if (ret != -EINVAL) {
267 dev_err(dev, "could not parse property %s\n",
268 cfg_params[i].property);
273 if (function != NULL)
277 ret = of_property_count_strings(np, "nvidia,pins");
279 dev_err(dev, "could not parse property nvidia,pins\n");
284 ret = reserve_map(dev, map, reserved_maps, num_maps, reserve);
288 of_property_for_each_string(np, "nvidia,pins", prop, group) {
290 ret = add_map_mux(map, reserved_maps, num_maps,
297 ret = add_map_configs(dev, map, reserved_maps,
298 num_maps, group, configs,
312 static int tegra_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
313 struct device_node *np_config,
314 struct pinctrl_map **map,
317 unsigned reserved_maps;
318 struct device_node *np;
325 for_each_child_of_node(np_config, np) {
326 ret = tegra_pinctrl_dt_subnode_to_map(pctldev->dev, np, map,
327 &reserved_maps, num_maps);
329 tegra_pinctrl_dt_free_map(pctldev, *map, *num_maps);
337 static const struct pinctrl_ops tegra_pinctrl_ops = {
338 .get_groups_count = tegra_pinctrl_get_groups_count,
339 .get_group_name = tegra_pinctrl_get_group_name,
340 .get_group_pins = tegra_pinctrl_get_group_pins,
341 #ifdef CONFIG_DEBUG_FS
342 .pin_dbg_show = tegra_pinctrl_pin_dbg_show,
344 .dt_node_to_map = tegra_pinctrl_dt_node_to_map,
345 .dt_free_map = tegra_pinctrl_dt_free_map,
348 static int tegra_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
350 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
352 return pmx->soc->nfunctions;
355 static const char *tegra_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
358 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
360 return pmx->soc->functions[function].name;
363 static int tegra_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
365 const char * const **groups,
366 unsigned * const num_groups)
368 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
370 *groups = pmx->soc->functions[function].groups;
371 *num_groups = pmx->soc->functions[function].ngroups;
376 static int tegra_pinconfig_group_set(struct pinctrl_dev *pctldev,
377 unsigned group, unsigned long param, unsigned long arg)
379 unsigned long config;
382 config = TEGRA_PINCONF_PACK(param, arg);
383 ret = tegra_pinconf_group_set(pctldev, group, config);
385 dev_err(pctldev->dev,
386 "Pinctrl group %u tristate config failed: %d\n",
391 static int tegra_pinctrl_enable(struct pinctrl_dev *pctldev, unsigned req_function,
394 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
395 const struct tegra_pingroup *g;
396 unsigned function = req_function;
402 g = &pmx->soc->groups[group];
404 if (WARN_ON(g->mux_reg < 0))
407 /* Last function option is safe option */
409 function = g->func_safe;
411 for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
412 if (g->funcs[i] == function)
415 if (WARN_ON(i == ARRAY_SIZE(g->funcs)))
418 spin_lock_irqsave(&mux_lock, flags);
420 val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
421 val &= ~(0x3 << g->mux_bit);
422 val |= i << g->mux_bit;
423 pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
425 spin_unlock_irqrestore(&mux_lock, flags);
430 static void tegra_pinctrl_disable(struct pinctrl_dev *pctldev,
431 unsigned function, unsigned group)
433 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
434 const struct tegra_pingroup *g;
439 g = &pmx->soc->groups[group];
441 if (WARN_ON(g->mux_reg < 0))
444 spin_lock_irqsave(&mux_lock, flags);
446 val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
447 val &= ~(0x3 << g->mux_bit);
448 for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
449 if (g->funcs[i] == g->func_safe)
452 val |= i << g->mux_bit;
453 pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
455 spin_unlock_irqrestore(&mux_lock, flags);
458 static int tegra_pinctrl_gpio_request_enable(struct pinctrl_dev *pctldev,
459 struct pinctrl_gpio_range *range,
462 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
464 if (pmx->soc->gpio_request_enable)
465 return pmx->soc->gpio_request_enable(pin);
469 static int tegra_pinctrl_gpio_set_direction(struct pinctrl_dev *pctldev,
470 struct pinctrl_gpio_range *range, unsigned offset, bool input)
472 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
474 const unsigned *pins;
478 for (group = 0; group < pmx->soc->ngroups; ++group) {
479 ret = tegra_pinctrl_get_group_pins(pctldev, group,
481 if (ret < 0 || num_pins != 1)
483 if (offset == pins[0])
487 if (group == pmx->soc->ngroups) {
488 dev_err(pctldev->dev,
489 "Pingroup not found for pin %u\n", offset);
494 * Set input = 1 for the input direction and
495 * tristate = 0 for output direction.
498 ret = tegra_pinconfig_group_set(pctldev, group,
499 TEGRA_PINCONF_PARAM_ENABLE_INPUT, 1);
501 ret = tegra_pinconfig_group_set(pctldev, group,
502 TEGRA_PINCONF_PARAM_TRISTATE, 0);
506 static const struct pinmux_ops tegra_pinmux_ops = {
507 .get_functions_count = tegra_pinctrl_get_funcs_count,
508 .get_function_name = tegra_pinctrl_get_func_name,
509 .get_function_groups = tegra_pinctrl_get_func_groups,
510 .enable = tegra_pinctrl_enable,
511 .disable = tegra_pinctrl_disable,
512 .gpio_request_enable = tegra_pinctrl_gpio_request_enable,
513 .gpio_set_direction = tegra_pinctrl_gpio_set_direction,
516 static int tegra_pinconf_reg(struct tegra_pmx *pmx,
517 const struct tegra_pingroup *g,
518 enum tegra_pinconf_param param,
520 s8 *bank, s16 *reg, s8 *bit, s8 *width)
523 case TEGRA_PINCONF_PARAM_PULL:
524 *bank = g->pupd_bank;
529 case TEGRA_PINCONF_PARAM_TRISTATE:
535 case TEGRA_PINCONF_PARAM_ENABLE_INPUT:
536 *bank = g->einput_bank;
537 *reg = g->einput_reg;
538 *bit = g->einput_bit;
541 case TEGRA_PINCONF_PARAM_OPEN_DRAIN:
542 *bank = g->odrain_bank;
543 *reg = g->odrain_reg;
544 *bit = g->odrain_bit;
547 case TEGRA_PINCONF_PARAM_LOCK:
548 *bank = g->lock_bank;
553 case TEGRA_PINCONF_PARAM_IORESET:
554 *bank = g->ioreset_bank;
555 *reg = g->ioreset_reg;
556 *bit = g->ioreset_bit;
559 case TEGRA_PINCONF_PARAM_RCV_SEL:
560 *bank = g->rcv_sel_bank;
561 *reg = g->rcv_sel_reg;
562 *bit = g->rcv_sel_bit;
565 case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE:
571 case TEGRA_PINCONF_PARAM_SCHMITT:
574 *bit = g->schmitt_bit;
577 case TEGRA_PINCONF_PARAM_LOW_POWER_MODE:
583 case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH:
587 *width = g->drvdn_width;
589 case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH:
593 *width = g->drvup_width;
595 case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING:
599 *width = g->slwf_width;
601 case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING:
605 *width = g->slwr_width;
607 case TEGRA_PINCONF_PARAM_DRIVE_TYPE:
608 *bank = g->drvtype_bank;
609 *reg = g->drvtype_reg;
610 *bit = g->drvtype_bit;
611 *width = g->drvtype_width;
614 dev_err(pmx->dev, "Invalid config param %04x\n", param);
618 if (*reg < 0 || *bit < 0) {
621 "Config param %04x not supported on group %s\n",
629 static int tegra_pinconf_get(struct pinctrl_dev *pctldev,
630 unsigned pin, unsigned long *config)
632 dev_err(pctldev->dev, "pin_config_get op not supported\n");
636 static int tegra_pinconf_set(struct pinctrl_dev *pctldev,
637 unsigned pin, unsigned long config)
639 dev_err(pctldev->dev, "pin_config_set op not supported\n");
643 static int tegra_pinconf_group_get(struct pinctrl_dev *pctldev,
644 unsigned group, unsigned long *config)
646 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
647 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(*config);
649 const struct tegra_pingroup *g;
656 g = &pmx->soc->groups[group];
658 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, ®, &bit,
663 spin_lock_irqsave(&mux_lock, flags);
665 val = pmx_readl(pmx, bank, reg);
667 spin_unlock_irqrestore(&mux_lock, flags);
669 mask = (1 << width) - 1;
670 arg = (val >> bit) & mask;
672 *config = TEGRA_PINCONF_PACK(param, arg);
677 static int tegra_pinconf_group_set(struct pinctrl_dev *pctldev,
678 unsigned group, unsigned long config)
680 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
681 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(config);
682 u16 arg = TEGRA_PINCONF_UNPACK_ARG(config);
683 const struct tegra_pingroup *g;
690 g = &pmx->soc->groups[group];
692 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, ®, &bit,
697 spin_lock_irqsave(&mux_lock, flags);
698 val = pmx_readl(pmx, bank, reg);
700 /* LOCK can't be cleared */
701 if (param == TEGRA_PINCONF_PARAM_LOCK) {
702 if ((val & BIT(bit)) && !arg) {
703 dev_err(pctldev->dev, "LOCK bit cannot be cleared\n");
709 /* Special-case Boolean values; allow any non-zero as true */
713 /* Range-check user-supplied value */
714 mask = (1 << width) - 1;
716 dev_err(pctldev->dev,
717 "group %s config %lx: %x too big for %d bit register\n",
718 g->name, config, arg, width);
723 /* Update register */
724 val &= ~(mask << bit);
726 pmx_writel(pmx, val, bank, reg);
729 spin_unlock_irqrestore(&mux_lock, flags);
733 #ifdef CONFIG_DEBUG_FS
734 static void tegra_pinconf_dbg_show(struct pinctrl_dev *pctldev,
735 struct seq_file *s, unsigned offset)
739 static const char *strip_prefix(const char *s)
741 const char *comma = strchr(s, ',');
748 static void tegra_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
749 struct seq_file *s, unsigned group)
751 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
752 const struct tegra_pingroup *g;
758 g = &pmx->soc->groups[group];
760 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
761 ret = tegra_pinconf_reg(pmx, g, cfg_params[i].param, false,
762 &bank, ®, &bit, &width);
766 val = pmx_readl(pmx, bank, reg);
768 val &= (1 << width) - 1;
770 seq_printf(s, "\n\t%s=%u",
771 strip_prefix(cfg_params[i].property), val);
775 static void tegra_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
777 unsigned long config)
779 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(config);
780 u16 arg = TEGRA_PINCONF_UNPACK_ARG(config);
781 const char *pname = "unknown";
784 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
785 if (cfg_params[i].param == param) {
786 pname = cfg_params[i].property;
791 seq_printf(s, "%s=%d", strip_prefix(pname), arg);
795 static const struct pinconf_ops tegra_pinconf_ops = {
796 .pin_config_get = tegra_pinconf_get,
797 .pin_config_set = tegra_pinconf_set,
798 .pin_config_group_get = tegra_pinconf_group_get,
799 .pin_config_group_set = tegra_pinconf_group_set,
800 #ifdef CONFIG_DEBUG_FS
801 .pin_config_dbg_show = tegra_pinconf_dbg_show,
802 .pin_config_group_dbg_show = tegra_pinconf_group_dbg_show,
803 .pin_config_config_dbg_show = tegra_pinconf_config_dbg_show,
807 static struct pinctrl_gpio_range tegra_pinctrl_gpio_range = {
808 .name = "Tegra GPIOs",
813 static struct pinctrl_desc tegra_pinctrl_desc = {
814 .pctlops = &tegra_pinctrl_ops,
815 .pmxops = &tegra_pinmux_ops,
816 .confops = &tegra_pinconf_ops,
817 .owner = THIS_MODULE,
820 #ifdef CONFIG_PM_SLEEP
822 static int pinctrl_suspend(void)
825 u32 *pg_data = pmx->pg_data;
828 if (pmx->soc->suspend) {
831 ret = pmx->soc->suspend(pg_data);
833 pinctrl_configure_user_state(pmx->pctl, "suspend");
837 for (i = 0; i < pmx->nbanks; i++) {
839 for (j = 0; j < pmx->regs_size[i] / 4; j++)
840 *pg_data++ = readl(regs++);
845 static void pinctrl_resume(void)
848 u32 *pg_data = pmx->pg_data;
851 if (pmx->soc->resume) {
852 pmx->soc->resume(pg_data);
856 for (i = 0; i < pmx->nbanks; i++) {
858 for (j = 0; j < pmx->regs_size[i] / 4; j++)
859 writel(*pg_data++, regs++);
863 static struct syscore_ops pinctrl_syscore_ops = {
864 .suspend = pinctrl_suspend,
865 .resume = pinctrl_resume,
866 .save = pinctrl_suspend,
867 .restore = pinctrl_resume,
872 static int tegra_pinctrl_get_group(struct tegra_pmx *pmx, const char *name)
876 for (i = 0; i< pmx->soc->ngroups; ++i) {
877 if (!strcmp(pmx->soc->groups[i].name, name))
883 static int tegra_pinctrl_set_config(struct pinctrl_dev *pctldev,
884 int pg, int param, int val)
886 unsigned long config;
888 config = TEGRA_PINCONF_PACK(param, val);
889 return tegra_pinconf_group_set(pmx->pctl, pg, config);
892 static void tegra_pinctrl_default_soc_init(struct tegra_pmx *pmx)
894 struct tegra_pinctrl_group_config_data *cdata;
898 for (i = 0; i < pmx->soc->nconfig_data; ++i) {
899 cdata = &pmx->soc->config_data[i];
900 group = tegra_pinctrl_get_group(pmx, cdata->name);
902 dev_warn(pmx->dev, "Group name %s not found\n",
907 if (pmx->soc->groups[i].hsm_bit >= 0)
908 tegra_pinctrl_set_config(pmx->pctl, group,
909 TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE,
910 cdata->high_speed_mode);
912 if (pmx->soc->groups[i].schmitt_bit >= 0)
913 tegra_pinctrl_set_config(pmx->pctl, group,
914 TEGRA_PINCONF_PARAM_SCHMITT,
917 if (pmx->soc->groups[i].lpmd_bit >= 0)
918 tegra_pinctrl_set_config(pmx->pctl, group,
919 TEGRA_PINCONF_PARAM_LOW_POWER_MODE,
920 cdata->low_power_mode);
922 if (pmx->soc->groups[i].drvdn_bit >= 0)
923 tegra_pinctrl_set_config(pmx->pctl, group,
924 TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH,
925 cdata->pull_down_strength);
927 if (pmx->soc->groups[i].drvup_bit >= 0)
928 tegra_pinctrl_set_config(pmx->pctl, group,
929 TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH,
930 cdata->pull_up_strength);
932 if (pmx->soc->groups[i].slwf_bit >= 0)
933 tegra_pinctrl_set_config(pmx->pctl, group,
934 TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING,
935 cdata->slew_rate_falling);
937 if (pmx->soc->groups[i].slwr_bit >= 0)
938 tegra_pinctrl_set_config(pmx->pctl, group,
939 TEGRA_PINCONF_PARAM_SLEW_RATE_RISING,
940 cdata->slew_rate_rising);
942 if (pmx->soc->groups[i].drvtype_reg >= 0)
943 tegra_pinctrl_set_config(pmx->pctl, group,
944 TEGRA_PINCONF_PARAM_DRIVE_TYPE,
949 int tegra_pinctrl_probe(struct platform_device *pdev,
950 const struct tegra_pinctrl_soc_data *soc_data)
952 struct resource *res;
953 int i, pg_data_size = 0;
955 pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
957 dev_err(&pdev->dev, "Can't alloc tegra_pmx\n");
960 pmx->dev = &pdev->dev;
963 pmx->drive_group_start_index = -1;
965 for (i = 0; i < pmx->soc->ngroups; ++i) {
966 if (pmx->soc->groups[i].drv_reg < 0)
968 pmx->drive_group_start_index = i;
972 tegra_pinctrl_gpio_range.npins = pmx->soc->ngpios;
973 tegra_pinctrl_desc.name = dev_name(&pdev->dev);
974 tegra_pinctrl_desc.pins = pmx->soc->pins;
975 tegra_pinctrl_desc.npins = pmx->soc->npins;
978 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
981 pg_data_size += resource_size(res);
985 pmx->regs = devm_kzalloc(&pdev->dev, pmx->nbanks * sizeof(*pmx->regs),
988 dev_err(&pdev->dev, "Can't alloc regs pointer\n");
992 pmx->reg_base = devm_kzalloc(&pdev->dev, pmx->nbanks *
993 sizeof(*pmx->reg_base), GFP_KERNEL);
994 if (!pmx->reg_base) {
995 dev_err(&pdev->dev, "Can't alloc reg_base pointer\n");
1000 #ifdef CONFIG_PM_SLEEP
1001 pmx->regs_size = devm_kzalloc(&pdev->dev,
1002 pmx->nbanks * sizeof(*(pmx->regs_size)),
1004 if (!pmx->regs_size) {
1005 dev_err(&pdev->dev, "Can't alloc regs pointer\n");
1009 pmx->pg_data = devm_kzalloc(&pdev->dev, pg_data_size, GFP_KERNEL);
1010 if (!pmx->pg_data) {
1011 dev_err(&pdev->dev, "Can't alloc pingroup data pointer\n");
1016 for (i = 0; i < pmx->nbanks; i++) {
1017 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1019 dev_err(&pdev->dev, "Missing MEM resource\n");
1023 if (!devm_request_mem_region(&pdev->dev, res->start,
1025 dev_name(&pdev->dev))) {
1027 "Couldn't request MEM resource %d\n", i);
1031 pmx->reg_base[i] = res->start;
1032 pmx->regs[i] = devm_ioremap(&pdev->dev, res->start,
1033 resource_size(res));
1034 if (!pmx->regs[i]) {
1035 dev_err(&pdev->dev, "Couldn't ioremap regs %d\n", i);
1039 #ifdef CONFIG_PM_SLEEP
1040 pmx->regs_size[i] = resource_size(res);
1044 pmx->pctl = pinctrl_register(&tegra_pinctrl_desc, &pdev->dev, pmx);
1046 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1050 pinctrl_add_gpio_range(pmx->pctl, &tegra_pinctrl_gpio_range);
1052 platform_set_drvdata(pdev, pmx);
1054 tegra_pinctrl_default_soc_init(pmx);
1056 pinctrl_configure_user_state(pmx->pctl, "drive");
1057 pinctrl_configure_user_state(pmx->pctl, "unused");
1059 #ifdef CONFIG_PM_SLEEP
1060 register_syscore_ops(&pinctrl_syscore_ops);
1062 dev_dbg(&pdev->dev, "Probed Tegra pinctrl driver\n");
1066 EXPORT_SYMBOL_GPL(tegra_pinctrl_probe);
1068 int tegra_pinctrl_remove(struct platform_device *pdev)
1070 struct tegra_pmx *pmx = platform_get_drvdata(pdev);
1072 pinctrl_unregister(pmx->pctl);
1076 EXPORT_SYMBOL_GPL(tegra_pinctrl_remove);
1078 /*** To support non-dt pin control */
1080 static const char *tegra_pinctrl_drive_names[TEGRA_MAX_DRIVE] = {
1081 [TEGRA_DRIVE_DIV_8] = "DIV_8",
1082 [TEGRA_DRIVE_DIV_4] = "DIV_4",
1083 [TEGRA_DRIVE_DIV_2] = "DIV_2",
1084 [TEGRA_DRIVE_DIV_1] = "DIV_1",
1087 static const char *tegra_pinctrl_slew_names[TEGRA_MAX_SLEW] = {
1088 [TEGRA_SLEW_FASTEST] = "FASTEST",
1089 [TEGRA_SLEW_FAST] = "FAST",
1090 [TEGRA_SLEW_SLOW] = "SLOW",
1091 [TEGRA_SLEW_SLOWEST] = "SLOWEST",
1094 #define HSM_EN(reg) (((reg) >> 2) & 0x1)
1095 #define SCHMT_EN(reg) (((reg) >> 3) & 0x1)
1096 #define LPMD(reg) (((reg) >> 4) & 0x3)
1097 #define DRVDN(reg, offset, w) (((reg) >> offset) & (BIT(w) -1))
1098 #define DRVUP(reg, offset, w) (((reg) >> offset) & (BIT(w) -1))
1099 #define SLWR(reg, offset, w) (((reg) >> offset) & (BIT(w) -1))
1100 #define SLWF(reg, offset, w) (((reg) >> offset) & (BIT(w) -1))
1102 static const char *tegra_pinctrl_function_name(enum tegra_mux_func func)
1104 if (func == TEGRA_MUX_RSVD1)
1107 if (func == TEGRA_MUX_RSVD2)
1110 if (func == TEGRA_MUX_RSVD3)
1113 if (func == TEGRA_MUX_RSVD4)
1116 if (func == TEGRA_MUX_INVALID)
1119 if (func < 0 || func >= TEGRA_MAX_MUX)
1122 return tegra_pinctrl_get_func_name(pmx->pctl, func);
1126 static const char *tegra_pinctrl_tri_name(unsigned long val)
1128 return val ? "TRISTATE" : "NORMAL";
1130 static const char *tegra_pinctrl_pupd_name(unsigned long val)
1146 #if !defined(CONFIG_ARCH_TEGRA_2x_SOC)
1147 static const char *tegra_pinctrl_lock_name(unsigned long val)
1150 case TEGRA_PIN_LOCK_DEFAULT:
1151 return "LOCK_DEFUALT";
1153 case TEGRA_PIN_LOCK_DISABLE:
1154 return "LOCK_DISABLE";
1156 case TEGRA_PIN_LOCK_ENABLE:
1157 return "LOCK_ENABLE";
1159 return "LOCK_DEFAULT";
1163 static const char *tegra_pinctrl_od_name(unsigned long val)
1166 case TEGRA_PIN_OD_DEFAULT:
1167 return "OD_DEFAULT";
1169 case TEGRA_PIN_OD_DISABLE:
1170 return "OD_DISABLE";
1172 case TEGRA_PIN_OD_ENABLE:
1175 return "OD_DEFAULT";
1179 static const char *tegra_pinctrl_ioreset_name(unsigned long val)
1182 case TEGRA_PIN_IO_RESET_DEFAULT:
1183 return "IO_RESET_DEFAULT";
1185 case TEGRA_PIN_IO_RESET_DISABLE:
1186 return "IO_RESET_DISABLE";
1188 case TEGRA_PIN_IO_RESET_ENABLE:
1189 return "IO_RESET_ENABLE";
1191 return "IO_RESET_DEFAULT";
1195 static const char *tegra_pinctrl_io_name(unsigned long val)
1209 static const char *tegra_pinctrl_rcv_sel_name(unsigned long val)
1213 return "RCV-DISABLE";
1216 return "RCV-DISABLE";
1223 static const char *drive_pinmux_name(int pg)
1225 if (pg < 0 || !pmx || pg >= pmx->soc->ngroups)
1227 if (pmx->soc->groups[pg].drv_reg < 0)
1230 return pmx->soc->groups[pg].name;
1233 static const char *enable_name(unsigned long val)
1235 return val ? "ENABLE" : "DISABLE";
1238 static const char *drive_name(unsigned long val)
1240 if (val >= TEGRA_MAX_DRIVE)
1243 return tegra_pinctrl_drive_names[val];
1246 static const char *slew_name(unsigned long val)
1248 if (val >= TEGRA_MAX_SLEW)
1251 return tegra_pinctrl_slew_names[val];
1254 u32 tegra_pinctrl_readl(u32 bank, u32 reg)
1256 return readl(pmx->regs[bank] + reg);
1258 EXPORT_SYMBOL_GPL(tegra_pinctrl_readl);
1260 void tegra_pinctrl_writel(u32 val, u32 bank, u32 reg)
1262 writel(val, pmx->regs[bank] + reg);
1264 EXPORT_SYMBOL_GPL(tegra_pinctrl_writel);
1266 int tegra_pinctrl_gpio_to_pingroup(int gpio_nr)
1270 if (!pmx || gpio_nr < 0)
1273 for (i = 0; i < pmx->soc->ngroups; ++i) {
1274 if (pmx->soc->groups[i].drv_reg >= 0)
1277 if (pmx->soc->groups[i].pins[0] == gpio_nr) {
1278 if (pmx->soc->groups[i].pins[0] >= pmx->soc->ngpios)
1285 EXPORT_SYMBOL(tegra_pinctrl_gpio_to_pingroup);
1287 int tegra_pinctrl_pg_set_func(const struct tegra_pingroup_config *config)
1293 int pg = config->pingroup;
1294 enum tegra_mux_func func = config->func;
1295 const struct tegra_pingroup *g;
1299 pr_err("Pingroup not registered yet\n");
1300 return -EPROBE_DEFER;
1303 if (pg < 0 || pg >= pmx->soc->ngroups)
1306 g = &pmx->soc->groups[pg];
1311 if (func == TEGRA_MUX_INVALID) {
1312 pr_err("The pingroup %s is not recommended for option %s\n",
1313 g->name, tegra_pinctrl_function_name(func));
1321 if (func == TEGRA_MUX_SAFE) {
1322 func = g->func_safe_non_dt;
1323 func_dt = g->func_safe;
1325 if (func & TEGRA_MUX_RSVD) {
1326 for (i = 0; i < 4; i++) {
1327 if (g->funcs_non_dt[i] & TEGRA_MUX_RSVD)
1330 if (g->funcs_non_dt[i] == func) {
1337 for (i = 0; i < 4; i++) {
1338 if (g->funcs_non_dt[i] == func) {
1347 pr_warn("The pingroup %s was configured to %s instead of %s\n",
1349 tegra_pinctrl_function_name(g->funcs_non_dt[mux]),
1350 tegra_pinctrl_function_name(func));
1352 func = g->funcs_non_dt[mux];
1353 func_dt = g->funcs[mux];
1357 pr_warn("The pingroup %s does not have option %s\n",
1358 g->name, tegra_pinctrl_function_name(func));
1362 ret = tegra_pinctrl_enable(pmx->pctl, func_dt, pg);
1364 pr_err("Not able to set function %s for pin group %s\n",
1365 tegra_pinctrl_function_name(func), g->name);
1369 ret = tegra_pinctrl_pg_set_io(pg, config->io);
1371 pr_err("Not able to set io %s for pin group %s\n",
1372 tegra_pinctrl_io_name(config->io), g->name);
1377 EXPORT_SYMBOL(tegra_pinctrl_pg_set_func);
1379 int tegra_pinctrl_pg_get_func(int pg)
1382 const struct tegra_pingroup *g;
1386 pr_err("Pingroup not registered yet\n");
1387 return -EPROBE_DEFER;
1390 if (pg < 0 || pg >= pmx->soc->ngroups)
1393 g = &pmx->soc->groups[pg];
1395 if (g->mux_reg < 0 || g->mux_bit)
1398 val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
1399 mux = (val >> g->mux_bit) & 0x3;
1400 return g->funcs_non_dt[mux];
1402 EXPORT_SYMBOL(tegra_pinctrl_pg_get_func);
1404 int tegra_pinctrl_pg_set_tristate(int pg, int tristate)
1407 pr_err("Pingroup not registered yet\n");
1408 return -EPROBE_DEFER;
1411 if (pg < 0 || pg >= pmx->soc->ngroups)
1414 return tegra_pinctrl_set_config(pmx->pctl, pg,
1415 TEGRA_PINCONF_PARAM_TRISTATE, tristate);
1417 EXPORT_SYMBOL(tegra_pinctrl_pg_set_tristate);
1419 int tegra_pinctrl_pg_set_io(int pg, int input)
1422 pr_err("Pingroup not registered yet\n");
1423 return -EPROBE_DEFER;
1426 if (pg < 0 || pg >= pmx->soc->ngroups)
1429 if (pmx->soc->groups[pg].einput_reg < 0)
1432 return tegra_pinctrl_set_config(pmx->pctl, pg,
1433 TEGRA_PINCONF_PARAM_ENABLE_INPUT, input);
1435 EXPORT_SYMBOL(tegra_pinctrl_pg_set_io);
1437 int tegra_pinctrl_pg_set_lock(int pg, int lock)
1439 int lv = (lock == TEGRA_PIN_LOCK_ENABLE) ? 1 : 0;
1442 pr_err("Pingroup not registered yet\n");
1443 return -EPROBE_DEFER;
1446 if (pg < 0 || pg >= pmx->soc->ngroups)
1449 if (lock == TEGRA_PIN_LOCK_DEFAULT)
1452 tegra_pinctrl_set_config(pmx->pctl, pg,
1453 TEGRA_PINCONF_PARAM_LOCK, lv);
1456 EXPORT_SYMBOL(tegra_pinctrl_pg_set_lock);
1458 int tegra_pinctrl_pg_set_od(int pg, int od)
1460 int ov = (od == TEGRA_PIN_OD_ENABLE) ? 1 : 0;
1463 pr_err("Pingroup not registered yet\n");
1464 return -EPROBE_DEFER;
1467 if (pg < 0 || pg >= pmx->soc->ngroups)
1470 if (od == TEGRA_PIN_OD_DEFAULT)
1473 tegra_pinctrl_set_config(pmx->pctl, pg,
1474 TEGRA_PINCONF_PARAM_OPEN_DRAIN, ov);
1477 EXPORT_SYMBOL(tegra_pinctrl_pg_set_od);
1479 int tegra_pinctrl_pg_set_ioreset(int pg, int ioreset)
1481 int iov = (ioreset == TEGRA_PIN_IO_RESET_ENABLE) ? 1 : 0;
1484 pr_err("Pingroup not registered yet\n");
1485 return -EPROBE_DEFER;
1488 if (pg < 0 || pg >= pmx->soc->ngroups)
1491 if (ioreset == TEGRA_PIN_IO_RESET_DEFAULT)
1494 tegra_pinctrl_set_config(pmx->pctl, pg,
1495 TEGRA_PINCONF_PARAM_IORESET, iov);
1498 EXPORT_SYMBOL(tegra_pinctrl_pg_set_ioreset);
1500 int tegra_pinctrl_pg_set_rcv_sel(int pg, int rcv_sel)
1502 int rcv = (rcv_sel == TEGRA_PIN_RCV_SEL_HIGH) ? 1 : 0;
1505 pr_err("Pingroup not registered yet\n");
1506 return -EPROBE_DEFER;
1509 if (pg < 0 || pg >= pmx->soc->ngroups)
1512 if (rcv_sel == TEGRA_PIN_RCV_SEL_DEFAULT)
1515 tegra_pinctrl_set_config(pmx->pctl, pg,
1516 TEGRA_PINCONF_PARAM_RCV_SEL, rcv);
1519 EXPORT_SYMBOL(tegra_pinctrl_pg_set_rcv_sel);
1521 int tegra_pinctrl_pg_set_pullupdown(int pg, int pupd)
1524 pr_err("Pingroup not registered yet\n");
1525 return -EPROBE_DEFER;
1528 if (pg < 0 || pg >= pmx->soc->ngroups)
1531 if (pupd != TEGRA_PUPD_NORMAL &&
1532 pupd != TEGRA_PUPD_PULL_DOWN &&
1533 pupd != TEGRA_PUPD_PULL_UP)
1536 tegra_pinctrl_set_config(pmx->pctl, pg,
1537 TEGRA_PINCONF_PARAM_PULL, pupd);
1540 EXPORT_SYMBOL(tegra_pinctrl_pg_set_pullupdown);
1542 void tegra_pinctrl_pg_config_pingroup(
1543 const struct tegra_pingroup_config *config)
1545 int pg = config->pingroup;
1546 enum tegra_mux_func func = config->func;
1547 enum tegra_pullupdown pupd = config->pupd;
1548 enum tegra_tristate tristate = config->tristate;
1549 #if !defined(CONFIG_ARCH_TEGRA_2x_SOC)
1550 enum tegra_pin_lock lock = config->lock;
1551 enum tegra_pin_od od = config->od;
1552 enum tegra_pin_ioreset ioreset = config->ioreset;
1553 enum tegra_pin_rcv_sel rcv_sel = config->rcv_sel;
1555 const struct tegra_pingroup *g;
1559 pr_err("Pingroup not registered yet\n");
1563 if (pg < 0 || pg >= pmx->soc->ngroups)
1566 g = &pmx->soc->groups[pg];
1568 if (g->mux_reg >= 0) {
1569 err = tegra_pinctrl_pg_set_func(config);
1571 pr_err("pinmux: can't set pingroup %s func to %s: %d\n",
1572 g->name, tegra_pinctrl_function_name(func), err);
1575 if (g->pupd_reg >= 0) {
1576 err = tegra_pinctrl_pg_set_pullupdown(pg, pupd);
1578 pr_err("pinmux: can't set pingroup %s pullupdown to %s: %d\n",
1579 g->name, tegra_pinctrl_pupd_name(pupd), err);
1582 if (g->tri_reg >= 0) {
1583 err = tegra_pinctrl_pg_set_tristate(pg, tristate);
1585 pr_err("pinmux: can't set pingroup %s tristate to %s: %d\n",
1586 g->name, tegra_pinctrl_tri_name(tristate), err);
1589 #if !defined(CONFIG_ARCH_TEGRA_2x_SOC)
1590 if (g->mux_reg >= 0) {
1591 err = tegra_pinctrl_pg_set_lock(pg, lock);
1593 pr_err("pinmux: can't set pingroup %s lock to %s: %d\n",
1594 g->name, tegra_pinctrl_lock_name(lock), err);
1597 if (g->mux_reg >= 0) {
1598 err = tegra_pinctrl_pg_set_od(pg, od);
1600 pr_err("pinmux: can't set pingroup %s od to %s: %d\n",
1601 g->name, tegra_pinctrl_od_name(od), err);
1604 if (g->mux_reg >= 0) {
1605 err = tegra_pinctrl_pg_set_ioreset(pg, ioreset);
1607 pr_err("pinmux: can't set pingroup %s ioreset to %s: %d\n",
1608 g->name, tegra_pinctrl_ioreset_name(ioreset),
1611 if (g->mux_reg >= 0) {
1612 err = tegra_pinctrl_pg_set_rcv_sel(pg, rcv_sel);
1614 pr_err("pinmux: can't set pingroup %s rcv_sel to %s: %d\n",
1615 g->name, tegra_pinctrl_rcv_sel_name(rcv_sel),
1620 EXPORT_SYMBOL(tegra_pinctrl_pg_config_pingroup);
1622 void tegra_pinctrl_pg_config_table(const struct tegra_pingroup_config *config,
1627 for (i = 0; i < len; i++)
1628 tegra_pinctrl_pg_config_pingroup(&config[i]);
1630 EXPORT_SYMBOL(tegra_pinctrl_pg_config_table);
1632 int tegra_pinctrl_pg_drive_set_hsm(int pdg, int hsm)
1634 int val = (hsm == TEGRA_HSM_ENABLE) ? 1 : 0;
1635 int pg = pmx->drive_group_start_index + pdg;
1637 if (pg < 0 || pg >= pmx->soc->ngroups)
1640 if (hsm != TEGRA_HSM_ENABLE && hsm != TEGRA_HSM_DISABLE)
1643 tegra_pinctrl_set_config(pmx->pctl, pg,
1644 TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE, val);
1647 EXPORT_SYMBOL(tegra_pinctrl_pg_drive_set_hsm);
1649 int tegra_pinctrl_pg_drive_set_schmitt(int pdg, int schmitt)
1651 int pg = pmx->drive_group_start_index + pdg;
1652 int val = (schmitt == TEGRA_SCHMITT_ENABLE) ? 1 : 0;
1654 if (pg < 0 || pg >= pmx->soc->ngroups)
1657 if (schmitt != TEGRA_SCHMITT_ENABLE && schmitt != TEGRA_SCHMITT_DISABLE)
1660 tegra_pinctrl_set_config(pmx->pctl, pg,
1661 TEGRA_PINCONF_PARAM_SCHMITT, val);
1664 EXPORT_SYMBOL(tegra_pinctrl_pg_drive_set_schmitt);
1666 int tegra_pinctrl_pg_drive_set_drive(int pdg, int drive)
1668 int pg = pmx->drive_group_start_index + pdg;
1670 if (pg < 0 || pg >= pmx->soc->ngroups)
1673 if (drive < 0 || drive >= TEGRA_MAX_DRIVE)
1676 tegra_pinctrl_set_config(pmx->pctl, pg,
1677 TEGRA_PINCONF_PARAM_LOW_POWER_MODE, drive);
1680 EXPORT_SYMBOL(tegra_pinctrl_pg_drive_set_drive);
1682 int tegra_pinctrl_pg_drive_set_pull_down(int pdg, int pull_down)
1684 int pg = pmx->drive_group_start_index + pdg;
1686 if (pg < 0 || pg >= pmx->soc->ngroups)
1689 if (pull_down < 0 || pull_down >= TEGRA_MAX_PULL)
1692 tegra_pinctrl_set_config(pmx->pctl, pg,
1693 TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH, pull_down);
1696 EXPORT_SYMBOL(tegra_pinctrl_pg_drive_set_pull_down);
1698 int tegra_pinctrl_pg_drive_set_pull_up(int pdg, int pull_up)
1700 int pg = pmx->drive_group_start_index + pdg;
1702 if (pg < 0 || pg >= pmx->soc->ngroups)
1705 if (pull_up < 0 || pull_up >= TEGRA_MAX_PULL)
1708 tegra_pinctrl_set_config(pmx->pctl, pg,
1709 TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH, pull_up);
1712 EXPORT_SYMBOL(tegra_pinctrl_pg_drive_set_pull_up);
1714 int tegra_pinctrl_pg_drive_set_slew_rising(int pdg, int slew_rising)
1716 int pg = pmx->drive_group_start_index + pdg;
1718 if (pg < 0 || pg >= pmx->soc->ngroups)
1721 if (slew_rising < 0 || slew_rising >= TEGRA_MAX_SLEW)
1724 tegra_pinctrl_set_config(pmx->pctl, pg,
1725 TEGRA_PINCONF_PARAM_SLEW_RATE_RISING, slew_rising);
1728 EXPORT_SYMBOL(tegra_pinctrl_pg_drive_set_slew_rising);
1730 int tegra_pinctrl_pg_drive_set_slew_falling(int pdg, int slew_falling)
1732 int pg = pmx->drive_group_start_index + pdg;
1734 if (pg < 0 || pg >= pmx->soc->ngroups)
1737 if (slew_falling < 0 || slew_falling >= TEGRA_MAX_SLEW)
1740 tegra_pinctrl_set_config(pmx->pctl, pg,
1741 TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING, slew_falling);
1744 EXPORT_SYMBOL(tegra_pinctrl_pg_drive_set_slew_falling);
1746 int tegra_pinctrl_pg_drive_set_drive_type(int pdg, int drive_type)
1748 int pg = pmx->drive_group_start_index + pdg;
1750 if (pg < 0 || pg >= pmx->soc->ngroups)
1753 if (pmx->soc->groups[pg].drvtype_reg < 0)
1756 if (drive_type < 0 || drive_type >= TEGRA_MAX_DRIVE_TYPE)
1759 tegra_pinctrl_set_config(pmx->pctl, pg,
1760 TEGRA_PINCONF_PARAM_DRIVE_TYPE, drive_type);
1763 EXPORT_SYMBOL(tegra_pinctrl_pg_drive_set_drive_type);
1765 void tegra_pinctrl_pg_drive_config_pingroup(int pingroup,
1766 int hsm, int schmitt, int drive, int pull_down,
1767 int pull_up, int slew_rising, int slew_falling,
1771 int pg = pmx->drive_group_start_index + pingroup;
1774 err = tegra_pinctrl_pg_drive_set_hsm(pingroup, hsm);
1776 pr_err("pinmux: can't set pingroup %s hsm to %s: %d\n",
1777 drive_pinmux_name(pg),
1778 enable_name(hsm), err);
1780 err = tegra_pinctrl_pg_drive_set_schmitt(pingroup, schmitt);
1782 pr_err("pinmux: can't set pingroup %s schmitt to %s: %d\n",
1783 drive_pinmux_name(pg),
1784 enable_name(schmitt), err);
1786 err = tegra_pinctrl_pg_drive_set_drive(pingroup, drive);
1788 pr_err("pinmux: can't set pingroup %s drive to %s: %d\n",
1789 drive_pinmux_name(pg),
1790 drive_name(drive), err);
1792 err = tegra_pinctrl_pg_drive_set_pull_down(pingroup, pull_down);
1794 pr_err("pinmux: can't set pingroup %s pull down to %d: %d\n",
1795 drive_pinmux_name(pg),
1798 err = tegra_pinctrl_pg_drive_set_pull_up(pingroup, pull_up);
1800 pr_err("pinmux: can't set pingroup %s pull up to %d: %d\n",
1801 drive_pinmux_name(pg),
1804 err = tegra_pinctrl_pg_drive_set_slew_rising(pingroup, slew_rising);
1806 pr_err("pinmux: can't set pingroup %s rising slew to %s: %d\n",
1807 drive_pinmux_name(pg),
1808 slew_name(slew_rising), err);
1810 err = tegra_pinctrl_pg_drive_set_slew_falling(pingroup, slew_falling);
1812 pr_err("pinmux: can't set pingroup %s falling slew to %s: %d\n",
1813 drive_pinmux_name(pg),
1814 slew_name(slew_falling), err);
1816 err = tegra_pinctrl_pg_drive_set_drive_type(pingroup, drive_type);
1818 pr_err("pinmux: can't set pingroup %s driver type to %d: %d\n",
1819 drive_pinmux_name(pg),
1822 EXPORT_SYMBOL(tegra_pinctrl_pg_drive_config_pingroup);
1824 void tegra_pinctrl_pg_drive_config_table(
1825 struct tegra_drive_pingroup_config *config, int len)
1829 for (i = 0; i < len; i++)
1830 tegra_pinctrl_pg_drive_config_pingroup(config[i].pingroup,
1834 config[i].pull_down,
1836 config[i].slew_rising,
1837 config[i].slew_falling,
1838 config[i].drive_type);
1840 EXPORT_SYMBOL(tegra_pinctrl_pg_drive_config_table);
1842 int tegra_pinctrl_pg_drive_get_pingroup(struct device *dev)
1850 dev_id = dev_name(dev);
1851 for (pg = 0; pg < pmx->soc->ngroups; pg++) {
1852 if (pmx->soc->groups[pg].dev_id &&
1853 !(strcmp(pmx->soc->groups[pg].dev_id, dev_id))) {
1854 if (pg >= pmx->drive_group_start_index)
1855 pg -= pmx->drive_group_start_index;
1860 return (pg == pmx->soc->ngroups) ? -EINVAL : pg;
1862 EXPORT_SYMBOL(tegra_pinctrl_pg_drive_get_pingroup);
1864 void tegra_pinctrl_pg_set_safe_pinmux_table(
1865 const struct tegra_pingroup_config *config, int len)
1868 struct tegra_pingroup_config c;
1870 for (i = 0; i < len; i++) {
1873 if (c.pingroup < 0 || c.pingroup >= pmx->soc->ngroups) {
1877 c.func = pmx->soc->groups[c.pingroup].func_safe;
1878 err = tegra_pinctrl_pg_set_func(&c);
1880 pr_err("%s: tegra_pinctrl_pg_set_func returned %d "
1881 "setting %s to %s\n", __func__, err,
1882 pmx->soc->groups[c.pingroup].name,
1883 tegra_pinctrl_function_name(c.func));
1886 EXPORT_SYMBOL(tegra_pinctrl_pg_set_safe_pinmux_table);
1888 void tegra_pinctrl_pg_config_pinmux_table(
1889 const struct tegra_pingroup_config *config, int len)
1893 for (i = 0; i < len; i++) {
1895 if (config[i].pingroup < 0 ||
1896 config[i].pingroup >= pmx->soc->ngroups) {
1900 err = tegra_pinctrl_pg_set_func(&config[i]);
1902 pr_err("%s: tegra_pinctrl_pg_set_func returned %d "
1903 "setting %s to %s\n", __func__, err,
1904 pmx->soc->groups[config[i].pingroup].name,
1905 tegra_pinctrl_function_name(config[i].func));
1908 EXPORT_SYMBOL(tegra_pinctrl_pg_config_pinmux_table);
1910 void tegra_pinctrl_pg_config_tristate_table(
1911 const struct tegra_pingroup_config *config,
1912 int len, int tristate)
1918 for (i = 0; i < len; i++) {
1919 pingroup = config[i].pingroup;
1920 if (pmx->soc->groups[pingroup].tri_reg > 0) {
1921 err = tegra_pinctrl_pg_set_tristate(pingroup, tristate);
1923 pr_err("pinmux: can't set pingroup %s tristate"
1925 pmx->soc->groups[pingroup].name,
1926 tegra_pinctrl_tri_name(tristate), err);
1930 EXPORT_SYMBOL(tegra_pinctrl_pg_config_tristate_table);
1932 void tegra_pinctrl_pg_config_pullupdown_table(
1933 const struct tegra_pingroup_config *config,
1940 for (i = 0; i < len; i++) {
1941 pingroup = config[i].pingroup;
1942 if (pmx->soc->groups[pingroup].pupd_reg > 0) {
1943 err = tegra_pinctrl_pg_set_pullupdown(pingroup, pupd);
1945 pr_err("pinmux: can't set pingroup %s pullupdown"
1947 pmx->soc->groups[pingroup].name,
1948 tegra_pinctrl_pupd_name(pupd), err);
1952 EXPORT_SYMBOL(tegra_pinctrl_pg_config_pullupdown_table);
1954 #ifdef CONFIG_DEBUG_FS
1956 #include <linux/debugfs.h>
1957 #include <linux/seq_file.h>
1959 static void dbg_pad_field(struct seq_file *s, int len)
1967 static int dbg_pinmux_show(struct seq_file *s, void *unused)
1972 for (i = 0; i < pmx->soc->ngroups; i++) {
1978 if (!pmx->soc->groups[i].name)
1981 if (pmx->soc->groups[i].mux_reg < 0)
1984 seq_printf(s, "\t{%s", pmx->soc->groups[i].name);
1985 len = strlen(pmx->soc->groups[i].name);
1986 dbg_pad_field(s, 15 - len);
1988 if (pmx->soc->groups[i].mux_reg < 0) {
1989 seq_puts(s, "TEGRA_MUX_NONE");
1990 len = strlen("NONE");
1992 reg = pmx_readl(pmx, pmx->soc->groups[i].mux_bank,
1993 pmx->soc->groups[i].mux_reg);
1994 mux = (reg >> pmx->soc->groups[i].mux_bit) & 0x3;
1995 BUG_ON(pmx->soc->groups[i].funcs[mux] == 0);
1996 if (pmx->soc->groups[i].funcs[mux] ==
1997 TEGRA_MUX_INVALID) {
1998 seq_puts(s, "TEGRA_MUX_INVALID");
2000 } else if (pmx->soc->groups[i].funcs[mux] &
2002 seq_printf(s, "TEGRA_MUX_RSVD%1lu", mux);
2005 seq_printf(s, "TEGRA_MUX_%s",
2006 tegra_pinctrl_function_name(
2007 pmx->soc->groups[i].funcs[mux]));
2008 len = strlen(tegra_pinctrl_function_name(
2009 pmx->soc->groups[i].funcs[mux]));
2012 dbg_pad_field(s, 13-len);
2014 if (pmx->soc->groups[i].einput_reg >= 0) {
2016 io = (pmx_readl(pmx, pmx->soc->groups[i].mux_bank,
2017 pmx->soc->groups[i].mux_reg) >> 5) & 0x1;
2018 seq_printf(s, "TEGRA_PIN_%s",
2019 tegra_pinctrl_io_name(io));
2020 len = strlen(tegra_pinctrl_io_name(io));
2021 dbg_pad_field(s, 6 - len);
2023 if (pmx->soc->groups[i].pupd_reg < 0) {
2024 seq_puts(s, "TEGRA_PUPD_NORMAL");
2025 len = strlen("NORMAL");
2027 reg = pmx_readl(pmx, pmx->soc->groups[i].pupd_bank,
2028 pmx->soc->groups[i].pupd_reg);
2029 pupd = (reg >> pmx->soc->groups[i].pupd_bit) & 0x3;
2030 seq_printf(s, "TEGRA_PUPD_%s",
2031 tegra_pinctrl_pupd_name(pupd));
2032 len = strlen(tegra_pinctrl_pupd_name(pupd));
2034 dbg_pad_field(s, 9 - len);
2036 if (pmx->soc->groups[i].tri_reg < 0) {
2037 seq_puts(s, "TEGRA_TRI_NORMAL");
2039 reg = pmx_readl(pmx, pmx->soc->groups[i].tri_bank,
2040 pmx->soc->groups[i].tri_reg);
2041 tri = (reg >> pmx->soc->groups[i].tri_bit) & 0x1;
2043 seq_printf(s, "TEGRA_TRI_%s",
2044 tegra_pinctrl_tri_name(tri));
2046 seq_puts(s, "},\n");
2051 static int dbg_pinmux_open(struct inode *inode, struct file *file)
2053 return single_open(file, dbg_pinmux_show, &inode->i_private);
2057 * Changing pinmux configuration at runtime
2059 * Usage: Feed "<PINGROUP> <FUNCTION> <E_INPUT> <PUPD> <TRISTATE>"
2061 * ex) # echo "HDMI_CEC CEC OUTPUT NORMAL TRISTATE" > /d/tegra_pinmux
2063 #define FIELD_DELIMITER " "
2064 #define LINE_DELIMITER "\n"
2065 static ssize_t dbg_pinmux_write(struct file *file,
2066 const char __user *userbuf, size_t count, loff_t *ppos)
2069 char *pbuf = &buf[0];
2071 struct tegra_pingroup_config pg_config;
2074 if (sizeof(buf) <= count)
2076 if (copy_from_user(buf, userbuf, count))
2079 pr_debug("%s buf: %s\n", __func__, buf);
2081 /* ping group index by name */
2082 token = strsep(&pbuf, FIELD_DELIMITER);
2083 for (i = 0; i < pmx->soc->ngroups; i++)
2084 if (!strcasecmp(token, pmx->soc->groups[i].name))
2086 if (i == pmx->soc->ngroups) { /* no pingroup matched with name */
2087 pr_err("no pingroup matched with name\n");
2090 pg_config.pingroup = i;
2092 /* func index by name */
2093 token = strsep(&pbuf, FIELD_DELIMITER);
2094 for (i = 0; i < TEGRA_MAX_MUX; i++)
2095 if (!strcasecmp(token, tegra_pinctrl_function_name(i)))
2097 if (i == TEGRA_MAX_MUX) { /* no func matched with name */
2098 pr_err("no func matched with name\n");
2104 token = strsep(&pbuf, FIELD_DELIMITER);
2105 i = !strcasecmp(token, "OUTPUT") ? 0 :
2106 !strcasecmp(token, "INPUT") ? 1 : -1;
2107 if (i == -1) { /* no IO matched with name */
2108 pr_err("no IO matched with name\n");
2113 /* pull up/down by name */
2114 token = strsep(&pbuf, FIELD_DELIMITER);
2115 i = !strcasecmp(token, "NORMAL") ? 0 :
2116 !strcasecmp(token, "PULL_DOWN") ? 1 :
2117 !strcasecmp(token, "PULL_UP") ? 2 : -1;
2118 if (i == -1) { /* no PUPD matched with name */
2119 pr_err("no PUPD matched with name\n");
2124 /* tristate by name */
2125 token = strsep(&pbuf, LINE_DELIMITER);
2126 i = !strcasecmp(token, "NORMAL") ? 0 :
2127 !strcasecmp(token, "TRISTATE") ? 1 : -1;
2128 if (i == -1) { /* no tristate matched with name */
2129 pr_err("no tristate matched with name\n");
2132 pg_config.tristate = i;
2134 pr_debug("pingroup=%d, func=%d, io=%d, pupd=%d, tristate=%d\n",
2135 pg_config.pingroup, pg_config.func, pg_config.io,
2136 pg_config.pupd, pg_config.tristate);
2137 tegra_pinctrl_pg_set_func(&pg_config);
2138 tegra_pinctrl_pg_set_pullupdown(pg_config.pingroup, pg_config.pupd);
2139 tegra_pinctrl_pg_set_tristate(pg_config.pingroup, pg_config.tristate);
2144 static const struct file_operations debug_fops = {
2145 .open = dbg_pinmux_open,
2146 .write = dbg_pinmux_write,
2148 .llseek = seq_lseek,
2149 .release = single_release,
2152 static int dbg_drive_pinmux_show(struct seq_file *s, void *unused)
2159 for (i = 0; i < pmx->soc->ngroups; i++) {
2162 if (pmx->soc->groups[i].drv_reg < 0)
2165 seq_printf(s, "\t{%s", pmx->soc->groups[i].name);
2166 len = strlen(pmx->soc->groups[i].name);
2167 dbg_pad_field(s, 15 - len);
2170 reg = pmx_readl(pmx, pmx->soc->groups[i].drv_bank,
2171 pmx->soc->groups[i].drv_reg);
2172 if (pmx->soc->groups[i].hsm_bit >= 0) {
2174 seq_puts(s, "TEGRA_HSM_ENABLE");
2177 seq_puts(s, "TEGRA_HSM_DISABLE");
2181 seq_puts(s, "TEGRA_HSM_XXXXXX");
2185 dbg_pad_field(s, 17 - len);
2187 if (pmx->soc->groups[i].schmitt_bit >= 0) {
2188 if (SCHMT_EN(reg)) {
2189 seq_puts(s, "TEGRA_SCHMITT_ENABLE");
2192 seq_puts(s, "TEGRA_SCHMITT_DISABLE");
2196 seq_puts(s, "TEGRA_SCHMITT_XXXXXXX");
2199 dbg_pad_field(s, 22 - len);
2201 if (pmx->soc->groups[i].lpmd_bit < 0) {
2202 seq_printf(s, "TEGRA_DRIVE_XXXXX");
2205 seq_printf(s, "TEGRA_DRIVE_%s", drive_name(LPMD(reg)));
2206 len = strlen(drive_name(LPMD(reg)));
2208 dbg_pad_field(s, 5 - len);
2210 if (pmx->soc->groups[i].drvdn_bit >= 0) {
2211 offset = pmx->soc->groups[i].drvdn_bit;
2212 width = pmx->soc->groups[i].drvdn_width;
2213 seq_printf(s, "TEGRA_PULL_%lu",
2214 DRVDN(reg, offset, width));
2215 len = DRVDN(reg, offset, width) < 10 ? 1 : 2;
2217 seq_printf(s, "TEGRA_PULL_XXX");
2220 dbg_pad_field(s, 2 - len);
2222 if (pmx->soc->groups[i].drvup_bit >= 0) {
2223 offset = pmx->soc->groups[i].drvup_bit;
2224 width = pmx->soc->groups[i].drvup_width;
2225 seq_printf(s, "TEGRA_PULL_%lu",
2226 DRVUP(reg, offset, width));
2227 len = DRVUP(reg, offset, width) < 10 ? 1 : 2;
2229 seq_printf(s, "TEGRA_PULL_XXX");
2232 dbg_pad_field(s, 2 - len);
2234 if (pmx->soc->groups[i].slwr_bit >= 0) {
2235 offset = pmx->soc->groups[i].slwr_bit;
2236 width = pmx->soc->groups[i].slwr_width;
2237 seq_printf(s, "TEGRA_SLEW_%s",
2238 slew_name(SLWR(reg, offset, width)));
2239 len = strlen(slew_name(SLWR(reg, offset, width)));
2241 seq_printf(s, "TEGRA_SLEW_XXXXXXX");
2244 dbg_pad_field(s, 7 - len);
2246 if (pmx->soc->groups[i].slwf_bit >= 0) {
2247 offset = pmx->soc->groups[i].slwf_bit;
2248 width = pmx->soc->groups[i].slwf_width;
2249 seq_printf(s, "TEGRA_SLEW_%s",
2250 slew_name(SLWF(reg, offset, width)));
2252 seq_printf(s, "TEGRA_SLEW_XXXXXXX");
2255 seq_puts(s, "},\n");
2260 static int dbg_drive_pinmux_open(struct inode *inode, struct file *file)
2262 return single_open(file, dbg_drive_pinmux_show, &inode->i_private);
2265 static const struct file_operations debug_drive_fops = {
2266 .open = dbg_drive_pinmux_open,
2268 .llseek = seq_lseek,
2269 .release = single_release,
2272 static int dbg_reg_pinmux_show(struct seq_file *s, void *unused)
2279 for (i = 0; i < pmx->soc->ngroups; i++) {
2280 if (pmx->soc->groups[i].drv_reg < 0) {
2281 bank = pmx->soc->groups[i].mux_bank;
2282 offset = pmx->soc->groups[i].mux_reg;
2284 bank = pmx->soc->groups[i].drv_bank;
2285 offset = pmx->soc->groups[i].drv_reg;
2287 reg = pmx_readl(pmx, bank, offset);
2288 seq_printf(s, "Bank: %d Reg: 0x%08x Val: 0x%08x -> %s\n",
2289 bank, pmx->reg_base[bank] + offset, reg,
2290 pmx->soc->groups[i].name);
2295 static int dbg_reg_pinmux_open(struct inode *inode, struct file *file)
2297 return single_open(file, dbg_reg_pinmux_show, &inode->i_private);
2300 static const struct file_operations debug_reg_fops = {
2301 .open = dbg_reg_pinmux_open,
2303 .llseek = seq_lseek,
2304 .release = single_release,
2307 static int __init tegra_pinctrl_debuginit(void)
2312 (void) debugfs_create_file("tegra_pinctrl", S_IRUGO | S_IWUSR | S_IWGRP,
2313 NULL, NULL, &debug_fops);
2314 (void) debugfs_create_file("tegra_pinctrl_drive", S_IRUGO,
2315 NULL, NULL, &debug_drive_fops);
2316 (void) debugfs_create_file("tegra_pinctrl_reg", S_IRUGO,
2317 NULL, NULL, &debug_reg_fops);
2320 late_initcall(tegra_pinctrl_debuginit);