24 #include <linux/module.h>
31 #include <linux/slab.h>
56 static int tegra_pinctrl_get_groups_count(
struct pinctrl_dev *pctldev)
60 return pmx->
soc->ngroups;
63 static const char *tegra_pinctrl_get_group_name(
struct pinctrl_dev *pctldev,
71 static int tegra_pinctrl_get_group_pins(
struct pinctrl_dev *pctldev,
73 const unsigned **
pins,
78 *pins = pmx->
soc->groups[
group].pins;
79 *num_pins = pmx->
soc->groups[
group].npins;
84 #ifdef CONFIG_DEBUG_FS
85 static void tegra_pinctrl_pin_dbg_show(
struct pinctrl_dev *pctldev,
94 unsigned *reserved_maps,
unsigned *num_maps,
97 unsigned old_num = *reserved_maps;
98 unsigned new_num = *num_maps +
reserve;
101 if (old_num >= new_num)
106 dev_err(dev,
"krealloc(map) failed\n");
110 memset(new_map + old_num, 0, (new_num - old_num) *
sizeof(*new_map));
113 *reserved_maps = new_num;
118 static int add_map_mux(
struct pinctrl_map **map,
unsigned *reserved_maps,
119 unsigned *num_maps,
const char *
group,
120 const char *
function)
122 if (
WARN_ON(*num_maps == *reserved_maps))
126 (*map)[*num_maps].data.mux.group =
group;
127 (*map)[*num_maps].data.mux.function =
function;
134 unsigned *reserved_maps,
unsigned *num_maps,
135 const char *group,
unsigned long *
configs,
136 unsigned num_configs)
138 unsigned long *dup_configs;
140 if (
WARN_ON(*num_maps == *reserved_maps))
143 dup_configs =
kmemdup(configs, num_configs *
sizeof(*dup_configs),
146 dev_err(dev,
"kmemdup(configs) failed\n");
151 (*map)[*num_maps].data.configs.group_or_pin =
group;
152 (*map)[*num_maps].data.configs.configs = dup_configs;
153 (*map)[*num_maps].data.configs.num_configs = num_configs;
159 static int add_config(
struct device *dev,
unsigned long **configs,
160 unsigned *num_configs,
unsigned long config)
162 unsigned old_num = *num_configs;
163 unsigned new_num = old_num + 1;
164 unsigned long *new_configs;
166 new_configs =
krealloc(*configs,
sizeof(*new_configs) * new_num,
169 dev_err(dev,
"krealloc(configs) failed\n");
173 new_configs[old_num] =
config;
175 *configs = new_configs;
176 *num_configs = new_num;
186 for (i = 0; i < num_maps; i++)
193 static const struct cfg_param {
215 unsigned *reserved_maps,
219 const char *
function;
222 unsigned long *configs =
NULL;
223 unsigned num_configs = 0;
233 "could not parse property nvidia,function\n");
237 for (i = 0; i <
ARRAY_SIZE(cfg_params); i++) {
238 ret = of_property_read_u32(np, cfg_params[i].
property, &val);
241 ret = add_config(dev, &configs, &num_configs, config);
245 }
else if (ret != -
EINVAL) {
246 dev_err(dev,
"could not parse property %s\n",
247 cfg_params[i].property);
252 if (
function !=
NULL)
258 dev_err(dev,
"could not parse property nvidia,pins\n");
263 ret = reserve_map(dev, map, reserved_maps, num_maps, reserve);
269 ret = add_map_mux(map, reserved_maps, num_maps,
276 ret = add_map_configs(dev, map, reserved_maps,
277 num_maps, group, configs,
295 unsigned reserved_maps;
305 &reserved_maps, num_maps);
315 static struct pinctrl_ops tegra_pinctrl_ops = {
316 .get_groups_count = tegra_pinctrl_get_groups_count,
317 .get_group_name = tegra_pinctrl_get_group_name,
318 .get_group_pins = tegra_pinctrl_get_group_pins,
319 #ifdef CONFIG_DEBUG_FS
320 .pin_dbg_show = tegra_pinctrl_pin_dbg_show,
326 static int tegra_pinctrl_get_funcs_count(
struct pinctrl_dev *pctldev)
330 return pmx->
soc->nfunctions;
333 static const char *tegra_pinctrl_get_func_name(
struct pinctrl_dev *pctldev,
338 return pmx->
soc->functions[
function].name;
341 static int tegra_pinctrl_get_func_groups(
struct pinctrl_dev *pctldev,
343 const char *
const **groups,
344 unsigned *
const num_groups)
348 *groups = pmx->
soc->functions[
function].groups;
349 *num_groups = pmx->
soc->functions[
function].ngroups;
354 static int tegra_pinctrl_enable(
struct pinctrl_dev *pctldev,
unsigned function,
368 if (g->
funcs[i] ==
function)
382 static void tegra_pinctrl_disable(
struct pinctrl_dev *pctldev,
383 unsigned function,
unsigned group)
400 static struct pinmux_ops tegra_pinmux_ops = {
401 .get_functions_count = tegra_pinctrl_get_funcs_count,
402 .get_function_name = tegra_pinctrl_get_func_name,
403 .get_function_groups = tegra_pinctrl_get_func_groups,
404 .enable = tegra_pinctrl_enable,
405 .disable = tegra_pinctrl_disable,
408 static int tegra_pinconf_reg(
struct tegra_pmx *pmx,
494 dev_err(pmx->
dev,
"Invalid config param %04x\n", param);
501 "Config param %04x not supported on group %s\n",
509 static int tegra_pinconf_get(
struct pinctrl_dev *pctldev,
510 unsigned pin,
unsigned long *config)
512 dev_err(pctldev->
dev,
"pin_config_get op not supported\n");
516 static int tegra_pinconf_set(
struct pinctrl_dev *pctldev,
517 unsigned pin,
unsigned long config)
519 dev_err(pctldev->
dev,
"pin_config_set op not supported\n");
523 static int tegra_pinconf_group_get(
struct pinctrl_dev *pctldev,
524 unsigned group,
unsigned long *config)
537 ret = tegra_pinconf_reg(pmx, g, param,
true, &bank, ®, &bit,
542 val = pmx_readl(pmx, bank, reg);
543 mask = (1 <<
width) - 1;
544 arg = (val >>
bit) & mask;
551 static int tegra_pinconf_group_set(
struct pinctrl_dev *pctldev,
552 unsigned group,
unsigned long config)
565 ret = tegra_pinconf_reg(pmx, g, param,
true, &bank, ®, &bit,
570 val = pmx_readl(pmx, bank, reg);
574 if ((val &
BIT(bit)) && !arg) {
575 dev_err(pctldev->
dev,
"LOCK bit cannot be cleared\n");
585 mask = (1 <<
width) - 1;
588 "config %lx: %x too big for %d bit register\n",
594 val &= ~(mask <<
bit);
596 pmx_writel(pmx, val, bank, reg);
601 #ifdef CONFIG_DEBUG_FS
602 static void tegra_pinconf_dbg_show(
struct pinctrl_dev *pctldev,
603 struct seq_file *s,
unsigned offset)
607 static const char *strip_prefix(
const char *s)
609 const char *comma =
strchr(s,
',');
616 static void tegra_pinconf_group_dbg_show(
struct pinctrl_dev *pctldev,
628 for (i = 0; i <
ARRAY_SIZE(cfg_params); i++) {
629 ret = tegra_pinconf_reg(pmx, g, cfg_params[i].param,
false,
630 &bank, ®, &bit, &width);
634 val = pmx_readl(pmx, bank, reg);
636 val &= (1 <<
width) - 1;
639 strip_prefix(cfg_params[i].
property), val);
643 static void tegra_pinconf_config_dbg_show(
struct pinctrl_dev *pctldev,
645 unsigned long config)
649 const char *pname =
"unknown";
652 for (i = 0; i <
ARRAY_SIZE(cfg_params); i++) {
653 if (cfg_params[i].param == param) {
654 pname = cfg_params[
i].property;
659 seq_printf(s,
"%s=%d", strip_prefix(pname), arg);
664 .pin_config_get = tegra_pinconf_get,
665 .pin_config_set = tegra_pinconf_set,
666 .pin_config_group_get = tegra_pinconf_group_get,
667 .pin_config_group_set = tegra_pinconf_group_set,
668 #ifdef CONFIG_DEBUG_FS
669 .pin_config_dbg_show = tegra_pinconf_dbg_show,
670 .pin_config_group_dbg_show = tegra_pinconf_group_dbg_show,
671 .pin_config_config_dbg_show = tegra_pinconf_config_dbg_show,
675 static struct pinctrl_gpio_range tegra_pinctrl_gpio_range = {
676 .name =
"Tegra GPIOs",
681 static struct pinctrl_desc tegra_pinctrl_desc = {
682 .pctlops = &tegra_pinctrl_ops,
683 .pmxops = &tegra_pinmux_ops,
697 dev_err(&pdev->
dev,
"Can't alloc tegra_pmx\n");
703 tegra_pinctrl_gpio_range.
npins = pmx->
soc->ngpios;
704 tegra_pinctrl_desc.name = dev_name(&pdev->
dev);
705 tegra_pinctrl_desc.pins = pmx->
soc->pins;
706 tegra_pinctrl_desc.npins = pmx->
soc->npins;
718 dev_err(&pdev->
dev,
"Can't alloc regs pointer\n");
722 for (i = 0; i < pmx->
nbanks; i++) {
725 dev_err(&pdev->
dev,
"Missing MEM resource\n");
731 dev_name(&pdev->
dev))) {
733 "Couldn't request MEM resource %d\n", i);
740 dev_err(&pdev->
dev,
"Couldn't ioremap regs %d\n", i);
747 dev_err(&pdev->
dev,
"Couldn't register pinctrl driver\n");
753 platform_set_drvdata(pdev, pmx);
755 dev_dbg(&pdev->
dev,
"Probed Tegra pinctrl driver\n");
763 struct tegra_pmx *pmx = platform_get_drvdata(pdev);