2 * Driver for the NVIDIA Tegra pinmux
4 * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved.
7 * Copyright (C) 2010 Google, Inc.
8 * Copyright (C) 2010 NVIDIA Corporation
9 * Copyright (C) 2009-2011 ST-Ericsson AB
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms and conditions of the GNU General Public License,
13 * version 2, as published by the Free Software Foundation.
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 #include <linux/err.h>
22 #include <linux/init.h>
24 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/pinctrl/machine.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include <linux/pinctrl/pinconf.h>
31 #include <linux/slab.h>
34 #include "pinctrl-tegra.h"
38 struct pinctrl_dev
*pctl
;
40 const struct tegra_pinctrl_soc_data
*soc
;
46 static inline u32
pmx_readl(struct tegra_pmx
*pmx
, u32 bank
, u32 reg
)
48 return readl(pmx
->regs
[bank
] + reg
);
51 static inline void pmx_writel(struct tegra_pmx
*pmx
, u32 val
, u32 bank
, u32 reg
)
53 writel(val
, pmx
->regs
[bank
] + reg
);
56 static int tegra_pinctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
58 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
60 return pmx
->soc
->ngroups
;
63 static const char *tegra_pinctrl_get_group_name(struct pinctrl_dev
*pctldev
,
66 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
68 return pmx
->soc
->groups
[group
].name
;
71 static int tegra_pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
73 const unsigned **pins
,
76 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
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
,
89 seq_printf(s
, " %s", dev_name(pctldev
->dev
));
93 static int reserve_map(struct device
*dev
, struct pinctrl_map
**map
,
94 unsigned *reserved_maps
, unsigned *num_maps
,
97 unsigned old_num
= *reserved_maps
;
98 unsigned new_num
= *num_maps
+ reserve
;
99 struct pinctrl_map
*new_map
;
101 if (old_num
>= new_num
)
104 new_map
= krealloc(*map
, sizeof(*new_map
) * new_num
, GFP_KERNEL
);
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
))
125 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
126 (*map
)[*num_maps
].data
.mux
.group
= group
;
127 (*map
)[*num_maps
].data
.mux
.function
= function
;
133 static int add_map_configs(struct device
*dev
, struct pinctrl_map
**map
,
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");
150 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
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
;
181 static void tegra_pinctrl_dt_free_map(struct pinctrl_dev
*pctldev
,
182 struct pinctrl_map
*map
,
187 for (i
= 0; i
< num_maps
; i
++)
188 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
)
189 kfree(map
[i
].data
.configs
.configs
);
194 static const struct cfg_param
{
195 const char *property
;
196 enum tegra_pinconf_param param
;
198 {"nvidia,pull", TEGRA_PINCONF_PARAM_PULL
},
199 {"nvidia,tristate", TEGRA_PINCONF_PARAM_TRISTATE
},
200 {"nvidia,enable-input", TEGRA_PINCONF_PARAM_ENABLE_INPUT
},
201 {"nvidia,open-drain", TEGRA_PINCONF_PARAM_OPEN_DRAIN
},
202 {"nvidia,lock", TEGRA_PINCONF_PARAM_LOCK
},
203 {"nvidia,io-reset", TEGRA_PINCONF_PARAM_IORESET
},
204 {"nvidia,rcv-sel", TEGRA_PINCONF_PARAM_RCV_SEL
},
205 {"nvidia,high-speed-mode", TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE
},
206 {"nvidia,schmitt", TEGRA_PINCONF_PARAM_SCHMITT
},
207 {"nvidia,low-power-mode", TEGRA_PINCONF_PARAM_LOW_POWER_MODE
},
208 {"nvidia,pull-down-strength", TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH
},
209 {"nvidia,pull-up-strength", TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH
},
210 {"nvidia,slew-rate-falling", TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING
},
211 {"nvidia,slew-rate-rising", TEGRA_PINCONF_PARAM_SLEW_RATE_RISING
},
212 {"nvidia,drive-type", TEGRA_PINCONF_PARAM_DRIVE_TYPE
},
215 static int tegra_pinctrl_dt_subnode_to_map(struct device
*dev
,
216 struct device_node
*np
,
217 struct pinctrl_map
**map
,
218 unsigned *reserved_maps
,
222 const char *function
;
224 unsigned long config
;
225 unsigned long *configs
= NULL
;
226 unsigned num_configs
= 0;
228 struct property
*prop
;
231 ret
= of_property_read_string(np
, "nvidia,function", &function
);
233 /* EINVAL=missing, which is fine since it's optional */
236 "could not parse property nvidia,function\n");
240 for (i
= 0; i
< ARRAY_SIZE(cfg_params
); i
++) {
241 ret
= of_property_read_u32(np
, cfg_params
[i
].property
, &val
);
243 config
= TEGRA_PINCONF_PACK(cfg_params
[i
].param
, val
);
244 ret
= add_config(dev
, &configs
, &num_configs
, config
);
247 /* EINVAL=missing, which is fine since it's optional */
248 } else if (ret
!= -EINVAL
) {
249 dev_err(dev
, "could not parse property %s\n",
250 cfg_params
[i
].property
);
255 if (function
!= NULL
)
259 ret
= of_property_count_strings(np
, "nvidia,pins");
261 dev_err(dev
, "could not parse property nvidia,pins\n");
266 ret
= reserve_map(dev
, map
, reserved_maps
, num_maps
, reserve
);
270 of_property_for_each_string(np
, "nvidia,pins", prop
, group
) {
272 ret
= add_map_mux(map
, reserved_maps
, num_maps
,
279 ret
= add_map_configs(dev
, map
, reserved_maps
,
280 num_maps
, group
, configs
,
294 static int tegra_pinctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
295 struct device_node
*np_config
,
296 struct pinctrl_map
**map
,
299 unsigned reserved_maps
;
300 struct device_node
*np
;
307 for_each_child_of_node(np_config
, np
) {
308 ret
= tegra_pinctrl_dt_subnode_to_map(pctldev
->dev
, np
, map
,
309 &reserved_maps
, num_maps
);
311 tegra_pinctrl_dt_free_map(pctldev
, *map
, *num_maps
);
319 static const struct pinctrl_ops tegra_pinctrl_ops
= {
320 .get_groups_count
= tegra_pinctrl_get_groups_count
,
321 .get_group_name
= tegra_pinctrl_get_group_name
,
322 .get_group_pins
= tegra_pinctrl_get_group_pins
,
323 #ifdef CONFIG_DEBUG_FS
324 .pin_dbg_show
= tegra_pinctrl_pin_dbg_show
,
326 .dt_node_to_map
= tegra_pinctrl_dt_node_to_map
,
327 .dt_free_map
= tegra_pinctrl_dt_free_map
,
330 static int tegra_pinctrl_get_funcs_count(struct pinctrl_dev
*pctldev
)
332 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
334 return pmx
->soc
->nfunctions
;
337 static const char *tegra_pinctrl_get_func_name(struct pinctrl_dev
*pctldev
,
340 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
342 return pmx
->soc
->functions
[function
].name
;
345 static int tegra_pinctrl_get_func_groups(struct pinctrl_dev
*pctldev
,
347 const char * const **groups
,
348 unsigned * const num_groups
)
350 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
352 *groups
= pmx
->soc
->functions
[function
].groups
;
353 *num_groups
= pmx
->soc
->functions
[function
].ngroups
;
358 static int tegra_pinctrl_enable(struct pinctrl_dev
*pctldev
, unsigned function
,
361 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
362 const struct tegra_pingroup
*g
;
366 g
= &pmx
->soc
->groups
[group
];
368 if (WARN_ON(g
->mux_reg
< 0))
371 for (i
= 0; i
< ARRAY_SIZE(g
->funcs
); i
++) {
372 if (g
->funcs
[i
] == function
)
375 if (WARN_ON(i
== ARRAY_SIZE(g
->funcs
)))
378 val
= pmx_readl(pmx
, g
->mux_bank
, g
->mux_reg
);
379 val
&= ~(0x3 << g
->mux_bit
);
380 val
|= i
<< g
->mux_bit
;
381 pmx_writel(pmx
, val
, g
->mux_bank
, g
->mux_reg
);
386 static void tegra_pinctrl_disable(struct pinctrl_dev
*pctldev
,
387 unsigned function
, unsigned group
)
389 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
390 const struct tegra_pingroup
*g
;
393 g
= &pmx
->soc
->groups
[group
];
395 if (WARN_ON(g
->mux_reg
< 0))
398 val
= pmx_readl(pmx
, g
->mux_bank
, g
->mux_reg
);
399 val
&= ~(0x3 << g
->mux_bit
);
400 val
|= g
->func_safe
<< g
->mux_bit
;
401 pmx_writel(pmx
, val
, g
->mux_bank
, g
->mux_reg
);
404 static const struct pinmux_ops tegra_pinmux_ops
= {
405 .get_functions_count
= tegra_pinctrl_get_funcs_count
,
406 .get_function_name
= tegra_pinctrl_get_func_name
,
407 .get_function_groups
= tegra_pinctrl_get_func_groups
,
408 .enable
= tegra_pinctrl_enable
,
409 .disable
= tegra_pinctrl_disable
,
412 static int tegra_pinconf_reg(struct tegra_pmx
*pmx
,
413 const struct tegra_pingroup
*g
,
414 enum tegra_pinconf_param param
,
416 s8
*bank
, s16
*reg
, s8
*bit
, s8
*width
)
419 case TEGRA_PINCONF_PARAM_PULL
:
420 *bank
= g
->pupd_bank
;
425 case TEGRA_PINCONF_PARAM_TRISTATE
:
431 case TEGRA_PINCONF_PARAM_ENABLE_INPUT
:
432 *bank
= g
->einput_bank
;
433 *reg
= g
->einput_reg
;
434 *bit
= g
->einput_bit
;
437 case TEGRA_PINCONF_PARAM_OPEN_DRAIN
:
438 *bank
= g
->odrain_bank
;
439 *reg
= g
->odrain_reg
;
440 *bit
= g
->odrain_bit
;
443 case TEGRA_PINCONF_PARAM_LOCK
:
444 *bank
= g
->lock_bank
;
449 case TEGRA_PINCONF_PARAM_IORESET
:
450 *bank
= g
->ioreset_bank
;
451 *reg
= g
->ioreset_reg
;
452 *bit
= g
->ioreset_bit
;
455 case TEGRA_PINCONF_PARAM_RCV_SEL
:
456 *bank
= g
->rcv_sel_bank
;
457 *reg
= g
->rcv_sel_reg
;
458 *bit
= g
->rcv_sel_bit
;
461 case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE
:
467 case TEGRA_PINCONF_PARAM_SCHMITT
:
470 *bit
= g
->schmitt_bit
;
473 case TEGRA_PINCONF_PARAM_LOW_POWER_MODE
:
479 case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH
:
483 *width
= g
->drvdn_width
;
485 case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH
:
489 *width
= g
->drvup_width
;
491 case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING
:
495 *width
= g
->slwf_width
;
497 case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING
:
501 *width
= g
->slwr_width
;
503 case TEGRA_PINCONF_PARAM_DRIVE_TYPE
:
504 *bank
= g
->drvtype_bank
;
505 *reg
= g
->drvtype_reg
;
506 *bit
= g
->drvtype_bit
;
510 dev_err(pmx
->dev
, "Invalid config param %04x\n", param
);
517 "Config param %04x not supported on group %s\n",
525 static int tegra_pinconf_get(struct pinctrl_dev
*pctldev
,
526 unsigned pin
, unsigned long *config
)
528 dev_err(pctldev
->dev
, "pin_config_get op not supported\n");
532 static int tegra_pinconf_set(struct pinctrl_dev
*pctldev
,
533 unsigned pin
, unsigned long config
)
535 dev_err(pctldev
->dev
, "pin_config_set op not supported\n");
539 static int tegra_pinconf_group_get(struct pinctrl_dev
*pctldev
,
540 unsigned group
, unsigned long *config
)
542 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
543 enum tegra_pinconf_param param
= TEGRA_PINCONF_UNPACK_PARAM(*config
);
545 const struct tegra_pingroup
*g
;
551 g
= &pmx
->soc
->groups
[group
];
553 ret
= tegra_pinconf_reg(pmx
, g
, param
, true, &bank
, ®
, &bit
,
558 val
= pmx_readl(pmx
, bank
, reg
);
559 mask
= (1 << width
) - 1;
560 arg
= (val
>> bit
) & mask
;
562 *config
= TEGRA_PINCONF_PACK(param
, arg
);
567 static int tegra_pinconf_group_set(struct pinctrl_dev
*pctldev
,
568 unsigned group
, unsigned long config
)
570 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
571 enum tegra_pinconf_param param
= TEGRA_PINCONF_UNPACK_PARAM(config
);
572 u16 arg
= TEGRA_PINCONF_UNPACK_ARG(config
);
573 const struct tegra_pingroup
*g
;
579 g
= &pmx
->soc
->groups
[group
];
581 ret
= tegra_pinconf_reg(pmx
, g
, param
, true, &bank
, ®
, &bit
,
586 val
= pmx_readl(pmx
, bank
, reg
);
588 /* LOCK can't be cleared */
589 if (param
== TEGRA_PINCONF_PARAM_LOCK
) {
590 if ((val
& BIT(bit
)) && !arg
) {
591 dev_err(pctldev
->dev
, "LOCK bit cannot be cleared\n");
596 /* Special-case Boolean values; allow any non-zero as true */
600 /* Range-check user-supplied value */
601 mask
= (1 << width
) - 1;
603 dev_err(pctldev
->dev
,
604 "config %lx: %x too big for %d bit register\n",
609 /* Update register */
610 val
&= ~(mask
<< bit
);
612 pmx_writel(pmx
, val
, bank
, reg
);
617 #ifdef CONFIG_DEBUG_FS
618 static void tegra_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
619 struct seq_file
*s
, unsigned offset
)
623 static const char *strip_prefix(const char *s
)
625 const char *comma
= strchr(s
, ',');
632 static void tegra_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
633 struct seq_file
*s
, unsigned group
)
635 struct tegra_pmx
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
636 const struct tegra_pingroup
*g
;
642 g
= &pmx
->soc
->groups
[group
];
644 for (i
= 0; i
< ARRAY_SIZE(cfg_params
); i
++) {
645 ret
= tegra_pinconf_reg(pmx
, g
, cfg_params
[i
].param
, false,
646 &bank
, ®
, &bit
, &width
);
650 val
= pmx_readl(pmx
, bank
, reg
);
652 val
&= (1 << width
) - 1;
654 seq_printf(s
, "\n\t%s=%u",
655 strip_prefix(cfg_params
[i
].property
), val
);
659 static void tegra_pinconf_config_dbg_show(struct pinctrl_dev
*pctldev
,
661 unsigned long config
)
663 enum tegra_pinconf_param param
= TEGRA_PINCONF_UNPACK_PARAM(config
);
664 u16 arg
= TEGRA_PINCONF_UNPACK_ARG(config
);
665 const char *pname
= "unknown";
668 for (i
= 0; i
< ARRAY_SIZE(cfg_params
); i
++) {
669 if (cfg_params
[i
].param
== param
) {
670 pname
= cfg_params
[i
].property
;
675 seq_printf(s
, "%s=%d", strip_prefix(pname
), arg
);
679 static const struct pinconf_ops tegra_pinconf_ops
= {
680 .pin_config_get
= tegra_pinconf_get
,
681 .pin_config_set
= tegra_pinconf_set
,
682 .pin_config_group_get
= tegra_pinconf_group_get
,
683 .pin_config_group_set
= tegra_pinconf_group_set
,
684 #ifdef CONFIG_DEBUG_FS
685 .pin_config_dbg_show
= tegra_pinconf_dbg_show
,
686 .pin_config_group_dbg_show
= tegra_pinconf_group_dbg_show
,
687 .pin_config_config_dbg_show
= tegra_pinconf_config_dbg_show
,
691 static struct pinctrl_gpio_range tegra_pinctrl_gpio_range
= {
692 .name
= "Tegra GPIOs",
697 static struct pinctrl_desc tegra_pinctrl_desc
= {
698 .pctlops
= &tegra_pinctrl_ops
,
699 .pmxops
= &tegra_pinmux_ops
,
700 .confops
= &tegra_pinconf_ops
,
701 .owner
= THIS_MODULE
,
704 int tegra_pinctrl_probe(struct platform_device
*pdev
,
705 const struct tegra_pinctrl_soc_data
*soc_data
)
707 struct tegra_pmx
*pmx
;
708 struct resource
*res
;
711 pmx
= devm_kzalloc(&pdev
->dev
, sizeof(*pmx
), GFP_KERNEL
);
713 dev_err(&pdev
->dev
, "Can't alloc tegra_pmx\n");
716 pmx
->dev
= &pdev
->dev
;
719 tegra_pinctrl_gpio_range
.npins
= pmx
->soc
->ngpios
;
720 tegra_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
721 tegra_pinctrl_desc
.pins
= pmx
->soc
->pins
;
722 tegra_pinctrl_desc
.npins
= pmx
->soc
->npins
;
725 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
731 pmx
->regs
= devm_kzalloc(&pdev
->dev
, pmx
->nbanks
* sizeof(*pmx
->regs
),
734 dev_err(&pdev
->dev
, "Can't alloc regs pointer\n");
738 for (i
= 0; i
< pmx
->nbanks
; i
++) {
739 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
741 dev_err(&pdev
->dev
, "Missing MEM resource\n");
745 if (!devm_request_mem_region(&pdev
->dev
, res
->start
,
747 dev_name(&pdev
->dev
))) {
749 "Couldn't request MEM resource %d\n", i
);
753 pmx
->regs
[i
] = devm_ioremap(&pdev
->dev
, res
->start
,
756 dev_err(&pdev
->dev
, "Couldn't ioremap regs %d\n", i
);
761 pmx
->pctl
= pinctrl_register(&tegra_pinctrl_desc
, &pdev
->dev
, pmx
);
763 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
767 pinctrl_add_gpio_range(pmx
->pctl
, &tegra_pinctrl_gpio_range
);
769 platform_set_drvdata(pdev
, pmx
);
771 dev_dbg(&pdev
->dev
, "Probed Tegra pinctrl driver\n");
775 EXPORT_SYMBOL_GPL(tegra_pinctrl_probe
);
777 int tegra_pinctrl_remove(struct platform_device
*pdev
)
779 struct tegra_pmx
*pmx
= platform_get_drvdata(pdev
);
781 pinctrl_unregister(pmx
->pctl
);
785 EXPORT_SYMBOL_GPL(tegra_pinctrl_remove
);