blob: 9da4da219a0778af3f3ab6ec8b4336554fa15b66 [file] [log] [blame]
Stephen Warren971dac72012-02-01 14:04:47 -07001/*
2 * Driver for the NVIDIA Tegra pinmux
3 *
Stephen Warren52f48fe2012-04-11 12:53:09 -06004 * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved.
Stephen Warren971dac72012-02-01 14:04:47 -07005 *
6 * Derived from code:
7 * Copyright (C) 2010 Google, Inc.
8 * Copyright (C) 2010 NVIDIA Corporation
9 * Copyright (C) 2009-2011 ST-Ericsson AB
10 *
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.
14 *
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
18 * more details.
19 */
20
21#include <linux/err.h>
22#include <linux/init.h>
23#include <linux/io.h>
24#include <linux/module.h>
Stephen Warren52f48fe2012-04-11 12:53:09 -060025#include <linux/of.h>
26#include <linux/platform_device.h>
Stephen Warren60f7f502012-04-04 09:27:50 -060027#include <linux/pinctrl/machine.h>
Stephen Warren971dac72012-02-01 14:04:47 -070028#include <linux/pinctrl/pinctrl.h>
29#include <linux/pinctrl/pinmux.h>
30#include <linux/pinctrl/pinconf.h>
Stephen Warren60f7f502012-04-04 09:27:50 -060031#include <linux/slab.h>
Stephen Warren971dac72012-02-01 14:04:47 -070032
Stephen Warren52f48fe2012-04-11 12:53:09 -060033#include "core.h"
Stephen Warren971dac72012-02-01 14:04:47 -070034#include "pinctrl-tegra.h"
Laxman Dewangan1ede12d2013-08-21 16:53:38 +053035#include "pinctrl-utils.h"
Stephen Warren971dac72012-02-01 14:04:47 -070036
Stephen Warren971dac72012-02-01 14:04:47 -070037struct tegra_pmx {
38 struct device *dev;
39 struct pinctrl_dev *pctl;
40
41 const struct tegra_pinctrl_soc_data *soc;
Stephen Warrence436252014-03-07 12:22:16 -070042 const char **group_pins;
Stephen Warren971dac72012-02-01 14:04:47 -070043
44 int nbanks;
45 void __iomem **regs;
46};
47
48static inline u32 pmx_readl(struct tegra_pmx *pmx, u32 bank, u32 reg)
49{
50 return readl(pmx->regs[bank] + reg);
51}
52
53static inline void pmx_writel(struct tegra_pmx *pmx, u32 val, u32 bank, u32 reg)
54{
55 writel(val, pmx->regs[bank] + reg);
56}
57
Viresh Kumard1e90e92012-03-30 11:25:40 +053058static int tegra_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
Stephen Warren971dac72012-02-01 14:04:47 -070059{
60 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
61
Viresh Kumard1e90e92012-03-30 11:25:40 +053062 return pmx->soc->ngroups;
Stephen Warren971dac72012-02-01 14:04:47 -070063}
64
65static const char *tegra_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
66 unsigned group)
67{
68 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
69
Stephen Warren971dac72012-02-01 14:04:47 -070070 return pmx->soc->groups[group].name;
71}
72
73static int tegra_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
74 unsigned group,
75 const unsigned **pins,
76 unsigned *num_pins)
77{
78 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
79
Stephen Warren971dac72012-02-01 14:04:47 -070080 *pins = pmx->soc->groups[group].pins;
81 *num_pins = pmx->soc->groups[group].npins;
82
83 return 0;
84}
85
Stephen Warrenb5badba2012-04-11 15:42:56 -060086#ifdef CONFIG_DEBUG_FS
Stephen Warren971dac72012-02-01 14:04:47 -070087static void tegra_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
88 struct seq_file *s,
89 unsigned offset)
90{
Stephen Warren52f48fe2012-04-11 12:53:09 -060091 seq_printf(s, " %s", dev_name(pctldev->dev));
Stephen Warren971dac72012-02-01 14:04:47 -070092}
Stephen Warrenb5badba2012-04-11 15:42:56 -060093#endif
Stephen Warren971dac72012-02-01 14:04:47 -070094
Stephen Warren60f7f502012-04-04 09:27:50 -060095static const struct cfg_param {
96 const char *property;
97 enum tegra_pinconf_param param;
98} cfg_params[] = {
99 {"nvidia,pull", TEGRA_PINCONF_PARAM_PULL},
100 {"nvidia,tristate", TEGRA_PINCONF_PARAM_TRISTATE},
101 {"nvidia,enable-input", TEGRA_PINCONF_PARAM_ENABLE_INPUT},
102 {"nvidia,open-drain", TEGRA_PINCONF_PARAM_OPEN_DRAIN},
103 {"nvidia,lock", TEGRA_PINCONF_PARAM_LOCK},
104 {"nvidia,io-reset", TEGRA_PINCONF_PARAM_IORESET},
Pritesh Raithatha348d1bf2013-01-08 13:02:36 +0530105 {"nvidia,rcv-sel", TEGRA_PINCONF_PARAM_RCV_SEL},
Stephen Warrenec654e52015-02-24 14:00:50 -0700106 {"nvidia,io-hv", TEGRA_PINCONF_PARAM_RCV_SEL},
Stephen Warren60f7f502012-04-04 09:27:50 -0600107 {"nvidia,high-speed-mode", TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE},
108 {"nvidia,schmitt", TEGRA_PINCONF_PARAM_SCHMITT},
109 {"nvidia,low-power-mode", TEGRA_PINCONF_PARAM_LOW_POWER_MODE},
110 {"nvidia,pull-down-strength", TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH},
111 {"nvidia,pull-up-strength", TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH},
112 {"nvidia,slew-rate-falling", TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING},
113 {"nvidia,slew-rate-rising", TEGRA_PINCONF_PARAM_SLEW_RATE_RISING},
Pritesh Raithatha348d1bf2013-01-08 13:02:36 +0530114 {"nvidia,drive-type", TEGRA_PINCONF_PARAM_DRIVE_TYPE},
Stephen Warren60f7f502012-04-04 09:27:50 -0600115};
116
Laxman Dewangan1ede12d2013-08-21 16:53:38 +0530117static int tegra_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
Axel Lin102caad2012-11-11 10:31:31 +0800118 struct device_node *np,
119 struct pinctrl_map **map,
120 unsigned *reserved_maps,
121 unsigned *num_maps)
Stephen Warren60f7f502012-04-04 09:27:50 -0600122{
Laxman Dewangan1ede12d2013-08-21 16:53:38 +0530123 struct device *dev = pctldev->dev;
Stephen Warren60f7f502012-04-04 09:27:50 -0600124 int ret, i;
125 const char *function;
126 u32 val;
127 unsigned long config;
128 unsigned long *configs = NULL;
129 unsigned num_configs = 0;
130 unsigned reserve;
131 struct property *prop;
132 const char *group;
133
134 ret = of_property_read_string(np, "nvidia,function", &function);
Stephen Warrenaef77042012-04-23 10:05:22 -0600135 if (ret < 0) {
136 /* EINVAL=missing, which is fine since it's optional */
137 if (ret != -EINVAL)
138 dev_err(dev,
139 "could not parse property nvidia,function\n");
Stephen Warren60f7f502012-04-04 09:27:50 -0600140 function = NULL;
Stephen Warrenaef77042012-04-23 10:05:22 -0600141 }
Stephen Warren60f7f502012-04-04 09:27:50 -0600142
143 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
144 ret = of_property_read_u32(np, cfg_params[i].property, &val);
145 if (!ret) {
146 config = TEGRA_PINCONF_PACK(cfg_params[i].param, val);
Laxman Dewangan1ede12d2013-08-21 16:53:38 +0530147 ret = pinctrl_utils_add_config(pctldev, &configs,
148 &num_configs, config);
Stephen Warren60f7f502012-04-04 09:27:50 -0600149 if (ret < 0)
150 goto exit;
Stephen Warrenaef77042012-04-23 10:05:22 -0600151 /* EINVAL=missing, which is fine since it's optional */
152 } else if (ret != -EINVAL) {
153 dev_err(dev, "could not parse property %s\n",
154 cfg_params[i].property);
Stephen Warren60f7f502012-04-04 09:27:50 -0600155 }
156 }
157
158 reserve = 0;
159 if (function != NULL)
160 reserve++;
161 if (num_configs)
162 reserve++;
163 ret = of_property_count_strings(np, "nvidia,pins");
Stephen Warrenaef77042012-04-23 10:05:22 -0600164 if (ret < 0) {
165 dev_err(dev, "could not parse property nvidia,pins\n");
Stephen Warren60f7f502012-04-04 09:27:50 -0600166 goto exit;
Stephen Warrenaef77042012-04-23 10:05:22 -0600167 }
Stephen Warren60f7f502012-04-04 09:27:50 -0600168 reserve *= ret;
169
Laxman Dewangan1ede12d2013-08-21 16:53:38 +0530170 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
171 num_maps, reserve);
Stephen Warren60f7f502012-04-04 09:27:50 -0600172 if (ret < 0)
173 goto exit;
174
175 of_property_for_each_string(np, "nvidia,pins", prop, group) {
176 if (function) {
Laxman Dewangan1ede12d2013-08-21 16:53:38 +0530177 ret = pinctrl_utils_add_map_mux(pctldev, map,
178 reserved_maps, num_maps, group,
179 function);
Stephen Warren60f7f502012-04-04 09:27:50 -0600180 if (ret < 0)
181 goto exit;
182 }
183
184 if (num_configs) {
Laxman Dewangan1ede12d2013-08-21 16:53:38 +0530185 ret = pinctrl_utils_add_map_configs(pctldev, map,
186 reserved_maps, num_maps, group,
187 configs, num_configs,
188 PIN_MAP_TYPE_CONFIGS_GROUP);
Stephen Warren60f7f502012-04-04 09:27:50 -0600189 if (ret < 0)
190 goto exit;
191 }
192 }
193
194 ret = 0;
195
196exit:
197 kfree(configs);
198 return ret;
199}
200
Axel Lin102caad2012-11-11 10:31:31 +0800201static int tegra_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
202 struct device_node *np_config,
203 struct pinctrl_map **map,
204 unsigned *num_maps)
Stephen Warren60f7f502012-04-04 09:27:50 -0600205{
206 unsigned reserved_maps;
207 struct device_node *np;
208 int ret;
209
210 reserved_maps = 0;
211 *map = NULL;
212 *num_maps = 0;
213
214 for_each_child_of_node(np_config, np) {
Laxman Dewangan1ede12d2013-08-21 16:53:38 +0530215 ret = tegra_pinctrl_dt_subnode_to_map(pctldev, np, map,
Stephen Warrenaef77042012-04-23 10:05:22 -0600216 &reserved_maps, num_maps);
Stephen Warren60f7f502012-04-04 09:27:50 -0600217 if (ret < 0) {
Laxman Dewangan1ede12d2013-08-21 16:53:38 +0530218 pinctrl_utils_dt_free_map(pctldev, *map,
219 *num_maps);
Julia Lawall21a8fe82015-12-21 17:39:44 +0100220 of_node_put(np);
Stephen Warren60f7f502012-04-04 09:27:50 -0600221 return ret;
222 }
223 }
224
225 return 0;
226}
227
Laurent Pinchart022ab142013-02-16 10:25:07 +0100228static const struct pinctrl_ops tegra_pinctrl_ops = {
Viresh Kumard1e90e92012-03-30 11:25:40 +0530229 .get_groups_count = tegra_pinctrl_get_groups_count,
Stephen Warren971dac72012-02-01 14:04:47 -0700230 .get_group_name = tegra_pinctrl_get_group_name,
231 .get_group_pins = tegra_pinctrl_get_group_pins,
Stephen Warrenb5badba2012-04-11 15:42:56 -0600232#ifdef CONFIG_DEBUG_FS
Stephen Warren971dac72012-02-01 14:04:47 -0700233 .pin_dbg_show = tegra_pinctrl_pin_dbg_show,
Stephen Warrenb5badba2012-04-11 15:42:56 -0600234#endif
Stephen Warren60f7f502012-04-04 09:27:50 -0600235 .dt_node_to_map = tegra_pinctrl_dt_node_to_map,
Laxman Dewangan1ede12d2013-08-21 16:53:38 +0530236 .dt_free_map = pinctrl_utils_dt_free_map,
Stephen Warren971dac72012-02-01 14:04:47 -0700237};
238
Viresh Kumard1e90e92012-03-30 11:25:40 +0530239static int tegra_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
Stephen Warren971dac72012-02-01 14:04:47 -0700240{
241 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
242
Viresh Kumard1e90e92012-03-30 11:25:40 +0530243 return pmx->soc->nfunctions;
Stephen Warren971dac72012-02-01 14:04:47 -0700244}
245
246static const char *tegra_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
247 unsigned function)
248{
249 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
250
Stephen Warren971dac72012-02-01 14:04:47 -0700251 return pmx->soc->functions[function].name;
252}
253
254static int tegra_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
255 unsigned function,
256 const char * const **groups,
257 unsigned * const num_groups)
258{
259 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
260
Stephen Warren971dac72012-02-01 14:04:47 -0700261 *groups = pmx->soc->functions[function].groups;
262 *num_groups = pmx->soc->functions[function].ngroups;
263
264 return 0;
265}
266
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200267static int tegra_pinctrl_set_mux(struct pinctrl_dev *pctldev,
268 unsigned function,
269 unsigned group)
Stephen Warren971dac72012-02-01 14:04:47 -0700270{
271 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
272 const struct tegra_pingroup *g;
273 int i;
274 u32 val;
275
Stephen Warren971dac72012-02-01 14:04:47 -0700276 g = &pmx->soc->groups[group];
277
Stephen Warrenaef77042012-04-23 10:05:22 -0600278 if (WARN_ON(g->mux_reg < 0))
Stephen Warren971dac72012-02-01 14:04:47 -0700279 return -EINVAL;
280
281 for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
282 if (g->funcs[i] == function)
283 break;
284 }
Stephen Warrenaef77042012-04-23 10:05:22 -0600285 if (WARN_ON(i == ARRAY_SIZE(g->funcs)))
Stephen Warren971dac72012-02-01 14:04:47 -0700286 return -EINVAL;
287
288 val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
289 val &= ~(0x3 << g->mux_bit);
290 val |= i << g->mux_bit;
291 pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
292
293 return 0;
294}
295
Laurent Pinchart022ab142013-02-16 10:25:07 +0100296static const struct pinmux_ops tegra_pinmux_ops = {
Viresh Kumard1e90e92012-03-30 11:25:40 +0530297 .get_functions_count = tegra_pinctrl_get_funcs_count,
Stephen Warren971dac72012-02-01 14:04:47 -0700298 .get_function_name = tegra_pinctrl_get_func_name,
299 .get_function_groups = tegra_pinctrl_get_func_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200300 .set_mux = tegra_pinctrl_set_mux,
Stephen Warren971dac72012-02-01 14:04:47 -0700301};
302
303static int tegra_pinconf_reg(struct tegra_pmx *pmx,
304 const struct tegra_pingroup *g,
305 enum tegra_pinconf_param param,
Stephen Warrenb5badba2012-04-11 15:42:56 -0600306 bool report_err,
Stephen Warren971dac72012-02-01 14:04:47 -0700307 s8 *bank, s16 *reg, s8 *bit, s8 *width)
308{
309 switch (param) {
310 case TEGRA_PINCONF_PARAM_PULL:
311 *bank = g->pupd_bank;
312 *reg = g->pupd_reg;
313 *bit = g->pupd_bit;
314 *width = 2;
315 break;
316 case TEGRA_PINCONF_PARAM_TRISTATE:
317 *bank = g->tri_bank;
318 *reg = g->tri_reg;
319 *bit = g->tri_bit;
320 *width = 1;
321 break;
322 case TEGRA_PINCONF_PARAM_ENABLE_INPUT:
Stephen Warrene53b7972014-04-15 11:00:50 -0600323 *bank = g->mux_bank;
324 *reg = g->mux_reg;
Stephen Warren971dac72012-02-01 14:04:47 -0700325 *bit = g->einput_bit;
326 *width = 1;
327 break;
328 case TEGRA_PINCONF_PARAM_OPEN_DRAIN:
Stephen Warrene53b7972014-04-15 11:00:50 -0600329 *bank = g->mux_bank;
330 *reg = g->mux_reg;
Stephen Warren971dac72012-02-01 14:04:47 -0700331 *bit = g->odrain_bit;
332 *width = 1;
333 break;
334 case TEGRA_PINCONF_PARAM_LOCK:
Stephen Warrene53b7972014-04-15 11:00:50 -0600335 *bank = g->mux_bank;
336 *reg = g->mux_reg;
Stephen Warren971dac72012-02-01 14:04:47 -0700337 *bit = g->lock_bit;
338 *width = 1;
339 break;
340 case TEGRA_PINCONF_PARAM_IORESET:
Stephen Warrene53b7972014-04-15 11:00:50 -0600341 *bank = g->mux_bank;
342 *reg = g->mux_reg;
Stephen Warren971dac72012-02-01 14:04:47 -0700343 *bit = g->ioreset_bit;
344 *width = 1;
345 break;
Pritesh Raithatha348d1bf2013-01-08 13:02:36 +0530346 case TEGRA_PINCONF_PARAM_RCV_SEL:
Stephen Warrene53b7972014-04-15 11:00:50 -0600347 *bank = g->mux_bank;
348 *reg = g->mux_reg;
Pritesh Raithatha348d1bf2013-01-08 13:02:36 +0530349 *bit = g->rcv_sel_bit;
350 *width = 1;
351 break;
Stephen Warren971dac72012-02-01 14:04:47 -0700352 case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE:
Stephen Warrenea623062015-02-24 14:00:49 -0700353 if (pmx->soc->hsm_in_mux) {
354 *bank = g->mux_bank;
355 *reg = g->mux_reg;
356 } else {
357 *bank = g->drv_bank;
358 *reg = g->drv_reg;
359 }
Stephen Warren971dac72012-02-01 14:04:47 -0700360 *bit = g->hsm_bit;
361 *width = 1;
362 break;
363 case TEGRA_PINCONF_PARAM_SCHMITT:
Stephen Warrenea623062015-02-24 14:00:49 -0700364 if (pmx->soc->schmitt_in_mux) {
365 *bank = g->mux_bank;
366 *reg = g->mux_reg;
367 } else {
368 *bank = g->drv_bank;
369 *reg = g->drv_reg;
370 }
Stephen Warren971dac72012-02-01 14:04:47 -0700371 *bit = g->schmitt_bit;
372 *width = 1;
373 break;
374 case TEGRA_PINCONF_PARAM_LOW_POWER_MODE:
375 *bank = g->drv_bank;
376 *reg = g->drv_reg;
377 *bit = g->lpmd_bit;
Pritesh Raithatha154f3eb2012-10-17 17:09:36 +0530378 *width = 2;
Stephen Warren971dac72012-02-01 14:04:47 -0700379 break;
380 case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH:
381 *bank = g->drv_bank;
382 *reg = g->drv_reg;
383 *bit = g->drvdn_bit;
384 *width = g->drvdn_width;
385 break;
386 case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH:
387 *bank = g->drv_bank;
388 *reg = g->drv_reg;
389 *bit = g->drvup_bit;
390 *width = g->drvup_width;
391 break;
392 case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING:
393 *bank = g->drv_bank;
394 *reg = g->drv_reg;
395 *bit = g->slwf_bit;
396 *width = g->slwf_width;
397 break;
398 case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING:
399 *bank = g->drv_bank;
400 *reg = g->drv_reg;
401 *bit = g->slwr_bit;
402 *width = g->slwr_width;
403 break;
Pritesh Raithatha348d1bf2013-01-08 13:02:36 +0530404 case TEGRA_PINCONF_PARAM_DRIVE_TYPE:
Stephen Warrenea623062015-02-24 14:00:49 -0700405 if (pmx->soc->drvtype_in_mux) {
406 *bank = g->mux_bank;
407 *reg = g->mux_reg;
408 } else {
409 *bank = g->drv_bank;
410 *reg = g->drv_reg;
411 }
Pritesh Raithatha348d1bf2013-01-08 13:02:36 +0530412 *bit = g->drvtype_bit;
413 *width = 2;
414 break;
Stephen Warren971dac72012-02-01 14:04:47 -0700415 default:
416 dev_err(pmx->dev, "Invalid config param %04x\n", param);
417 return -ENOTSUPP;
418 }
419
Stephen Warrene53b7972014-04-15 11:00:50 -0600420 if (*reg < 0 || *bit > 31) {
Stephen Warren36e80dc2014-04-14 15:33:42 -0600421 if (report_err) {
422 const char *prop = "unknown";
423 int i;
424
425 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
426 if (cfg_params[i].param == param) {
427 prop = cfg_params[i].property;
428 break;
429 }
430 }
431
Stephen Warrenb5badba2012-04-11 15:42:56 -0600432 dev_err(pmx->dev,
Stephen Warren36e80dc2014-04-14 15:33:42 -0600433 "Config param %04x (%s) not supported on group %s\n",
434 param, prop, g->name);
435 }
Stephen Warren971dac72012-02-01 14:04:47 -0700436 return -ENOTSUPP;
437 }
438
439 return 0;
440}
441
442static int tegra_pinconf_get(struct pinctrl_dev *pctldev,
443 unsigned pin, unsigned long *config)
444{
Stephen Warrenaef77042012-04-23 10:05:22 -0600445 dev_err(pctldev->dev, "pin_config_get op not supported\n");
Stephen Warren971dac72012-02-01 14:04:47 -0700446 return -ENOTSUPP;
447}
448
449static int tegra_pinconf_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -0700450 unsigned pin, unsigned long *configs,
451 unsigned num_configs)
Stephen Warren971dac72012-02-01 14:04:47 -0700452{
Stephen Warrenaef77042012-04-23 10:05:22 -0600453 dev_err(pctldev->dev, "pin_config_set op not supported\n");
Stephen Warren971dac72012-02-01 14:04:47 -0700454 return -ENOTSUPP;
455}
456
457static int tegra_pinconf_group_get(struct pinctrl_dev *pctldev,
458 unsigned group, unsigned long *config)
459{
460 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
461 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(*config);
462 u16 arg;
463 const struct tegra_pingroup *g;
464 int ret;
465 s8 bank, bit, width;
466 s16 reg;
467 u32 val, mask;
468
Stephen Warren971dac72012-02-01 14:04:47 -0700469 g = &pmx->soc->groups[group];
470
Stephen Warrenb5badba2012-04-11 15:42:56 -0600471 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, &reg, &bit,
472 &width);
Stephen Warren971dac72012-02-01 14:04:47 -0700473 if (ret < 0)
474 return ret;
475
476 val = pmx_readl(pmx, bank, reg);
477 mask = (1 << width) - 1;
478 arg = (val >> bit) & mask;
479
480 *config = TEGRA_PINCONF_PACK(param, arg);
481
482 return 0;
483}
484
485static int tegra_pinconf_group_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -0700486 unsigned group, unsigned long *configs,
487 unsigned num_configs)
Stephen Warren971dac72012-02-01 14:04:47 -0700488{
489 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
Sherman Yin03b054e2013-08-27 11:32:12 -0700490 enum tegra_pinconf_param param;
491 u16 arg;
Stephen Warren971dac72012-02-01 14:04:47 -0700492 const struct tegra_pingroup *g;
Sherman Yin03b054e2013-08-27 11:32:12 -0700493 int ret, i;
Stephen Warren971dac72012-02-01 14:04:47 -0700494 s8 bank, bit, width;
495 s16 reg;
496 u32 val, mask;
497
Stephen Warren971dac72012-02-01 14:04:47 -0700498 g = &pmx->soc->groups[group];
499
Sherman Yin03b054e2013-08-27 11:32:12 -0700500 for (i = 0; i < num_configs; i++) {
501 param = TEGRA_PINCONF_UNPACK_PARAM(configs[i]);
502 arg = TEGRA_PINCONF_UNPACK_ARG(configs[i]);
Stephen Warren971dac72012-02-01 14:04:47 -0700503
Sherman Yin03b054e2013-08-27 11:32:12 -0700504 ret = tegra_pinconf_reg(pmx, g, param, true, &bank, &reg, &bit,
505 &width);
506 if (ret < 0)
507 return ret;
Stephen Warren971dac72012-02-01 14:04:47 -0700508
Sherman Yin03b054e2013-08-27 11:32:12 -0700509 val = pmx_readl(pmx, bank, reg);
510
511 /* LOCK can't be cleared */
512 if (param == TEGRA_PINCONF_PARAM_LOCK) {
513 if ((val & BIT(bit)) && !arg) {
514 dev_err(pctldev->dev, "LOCK bit cannot be cleared\n");
515 return -EINVAL;
516 }
517 }
518
519 /* Special-case Boolean values; allow any non-zero as true */
520 if (width == 1)
521 arg = !!arg;
522
523 /* Range-check user-supplied value */
524 mask = (1 << width) - 1;
525 if (arg & ~mask) {
526 dev_err(pctldev->dev,
527 "config %lx: %x too big for %d bit register\n",
528 configs[i], arg, width);
Stephen Warren971dac72012-02-01 14:04:47 -0700529 return -EINVAL;
Stephen Warrenaef77042012-04-23 10:05:22 -0600530 }
Stephen Warren971dac72012-02-01 14:04:47 -0700531
Sherman Yin03b054e2013-08-27 11:32:12 -0700532 /* Update register */
533 val &= ~(mask << bit);
534 val |= arg << bit;
535 pmx_writel(pmx, val, bank, reg);
536 } /* for each config */
Stephen Warren971dac72012-02-01 14:04:47 -0700537
538 return 0;
539}
540
Stephen Warrenb5badba2012-04-11 15:42:56 -0600541#ifdef CONFIG_DEBUG_FS
Stephen Warren971dac72012-02-01 14:04:47 -0700542static void tegra_pinconf_dbg_show(struct pinctrl_dev *pctldev,
543 struct seq_file *s, unsigned offset)
544{
545}
546
Stephen Warrenb5badba2012-04-11 15:42:56 -0600547static const char *strip_prefix(const char *s)
Stephen Warren971dac72012-02-01 14:04:47 -0700548{
Stephen Warrenb5badba2012-04-11 15:42:56 -0600549 const char *comma = strchr(s, ',');
550 if (!comma)
551 return s;
552
553 return comma + 1;
Stephen Warren971dac72012-02-01 14:04:47 -0700554}
555
Stephen Warrenb5badba2012-04-11 15:42:56 -0600556static void tegra_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
557 struct seq_file *s, unsigned group)
558{
559 struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
560 const struct tegra_pingroup *g;
561 int i, ret;
562 s8 bank, bit, width;
563 s16 reg;
564 u32 val;
565
566 g = &pmx->soc->groups[group];
567
568 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
569 ret = tegra_pinconf_reg(pmx, g, cfg_params[i].param, false,
570 &bank, &reg, &bit, &width);
571 if (ret < 0)
572 continue;
573
574 val = pmx_readl(pmx, bank, reg);
575 val >>= bit;
576 val &= (1 << width) - 1;
577
578 seq_printf(s, "\n\t%s=%u",
579 strip_prefix(cfg_params[i].property), val);
580 }
581}
582
583static void tegra_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
584 struct seq_file *s,
585 unsigned long config)
586{
587 enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(config);
588 u16 arg = TEGRA_PINCONF_UNPACK_ARG(config);
589 const char *pname = "unknown";
590 int i;
591
592 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
593 if (cfg_params[i].param == param) {
594 pname = cfg_params[i].property;
595 break;
596 }
597 }
598
599 seq_printf(s, "%s=%d", strip_prefix(pname), arg);
600}
601#endif
602
Laurent Pinchart022ab142013-02-16 10:25:07 +0100603static const struct pinconf_ops tegra_pinconf_ops = {
Stephen Warren971dac72012-02-01 14:04:47 -0700604 .pin_config_get = tegra_pinconf_get,
605 .pin_config_set = tegra_pinconf_set,
606 .pin_config_group_get = tegra_pinconf_group_get,
607 .pin_config_group_set = tegra_pinconf_group_set,
Stephen Warrenb5badba2012-04-11 15:42:56 -0600608#ifdef CONFIG_DEBUG_FS
Stephen Warren971dac72012-02-01 14:04:47 -0700609 .pin_config_dbg_show = tegra_pinconf_dbg_show,
610 .pin_config_group_dbg_show = tegra_pinconf_group_dbg_show,
Stephen Warrenb5badba2012-04-11 15:42:56 -0600611 .pin_config_config_dbg_show = tegra_pinconf_config_dbg_show,
612#endif
Stephen Warren971dac72012-02-01 14:04:47 -0700613};
614
615static struct pinctrl_gpio_range tegra_pinctrl_gpio_range = {
616 .name = "Tegra GPIOs",
617 .id = 0,
618 .base = 0,
619};
620
621static struct pinctrl_desc tegra_pinctrl_desc = {
Stephen Warren971dac72012-02-01 14:04:47 -0700622 .pctlops = &tegra_pinctrl_ops,
623 .pmxops = &tegra_pinmux_ops,
624 .confops = &tegra_pinconf_ops,
625 .owner = THIS_MODULE,
626};
627
Tomeu Vizoso94625102015-07-14 10:29:55 +0200628static bool gpio_node_has_range(void)
629{
630 struct device_node *np;
631 bool has_prop = false;
632
633 np = of_find_compatible_node(NULL, NULL, "nvidia,tegra30-gpio");
634 if (!np)
635 return has_prop;
636
637 has_prop = of_find_property(np, "gpio-ranges", NULL);
638
639 of_node_put(np);
640
641 return has_prop;
642}
643
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800644int tegra_pinctrl_probe(struct platform_device *pdev,
Stephen Warren52f48fe2012-04-11 12:53:09 -0600645 const struct tegra_pinctrl_soc_data *soc_data)
Stephen Warren971dac72012-02-01 14:04:47 -0700646{
Stephen Warren971dac72012-02-01 14:04:47 -0700647 struct tegra_pmx *pmx;
648 struct resource *res;
649 int i;
Stephen Warrence436252014-03-07 12:22:16 -0700650 const char **group_pins;
651 int fn, gn, gfn;
Stephen Warren971dac72012-02-01 14:04:47 -0700652
Stephen Warren971dac72012-02-01 14:04:47 -0700653 pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
654 if (!pmx) {
655 dev_err(&pdev->dev, "Can't alloc tegra_pmx\n");
656 return -ENOMEM;
657 }
658 pmx->dev = &pdev->dev;
Stephen Warren52f48fe2012-04-11 12:53:09 -0600659 pmx->soc = soc_data;
Stephen Warren971dac72012-02-01 14:04:47 -0700660
Stephen Warrence436252014-03-07 12:22:16 -0700661 /*
662 * Each mux group will appear in 4 functions' list of groups.
663 * This over-allocates slightly, since not all groups are mux groups.
664 */
665 pmx->group_pins = devm_kzalloc(&pdev->dev,
666 soc_data->ngroups * 4 * sizeof(*pmx->group_pins),
667 GFP_KERNEL);
668 if (!pmx->group_pins)
669 return -ENOMEM;
670
671 group_pins = pmx->group_pins;
672 for (fn = 0; fn < soc_data->nfunctions; fn++) {
673 struct tegra_function *func = &soc_data->functions[fn];
674
675 func->groups = group_pins;
676
677 for (gn = 0; gn < soc_data->ngroups; gn++) {
678 const struct tegra_pingroup *g = &soc_data->groups[gn];
679
680 if (g->mux_reg == -1)
681 continue;
682
683 for (gfn = 0; gfn < 4; gfn++)
684 if (g->funcs[gfn] == fn)
685 break;
686 if (gfn == 4)
687 continue;
688
689 BUG_ON(group_pins - pmx->group_pins >=
690 soc_data->ngroups * 4);
691 *group_pins++ = g->name;
692 func->ngroups++;
693 }
694 }
695
Stephen Warren971dac72012-02-01 14:04:47 -0700696 tegra_pinctrl_gpio_range.npins = pmx->soc->ngpios;
Stephen Warren52f48fe2012-04-11 12:53:09 -0600697 tegra_pinctrl_desc.name = dev_name(&pdev->dev);
Stephen Warren971dac72012-02-01 14:04:47 -0700698 tegra_pinctrl_desc.pins = pmx->soc->pins;
699 tegra_pinctrl_desc.npins = pmx->soc->npins;
700
701 for (i = 0; ; i++) {
702 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
703 if (!res)
704 break;
705 }
706 pmx->nbanks = i;
707
708 pmx->regs = devm_kzalloc(&pdev->dev, pmx->nbanks * sizeof(*pmx->regs),
709 GFP_KERNEL);
710 if (!pmx->regs) {
711 dev_err(&pdev->dev, "Can't alloc regs pointer\n");
Laxman Dewangan5b232c52014-02-05 19:11:34 +0530712 return -ENOMEM;
Stephen Warren971dac72012-02-01 14:04:47 -0700713 }
714
715 for (i = 0; i < pmx->nbanks; i++) {
716 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
Axel Lin394a8ff2013-08-27 09:07:23 +0800717 pmx->regs[i] = devm_ioremap_resource(&pdev->dev, res);
718 if (IS_ERR(pmx->regs[i]))
719 return PTR_ERR(pmx->regs[i]);
Stephen Warren971dac72012-02-01 14:04:47 -0700720 }
721
722 pmx->pctl = pinctrl_register(&tegra_pinctrl_desc, &pdev->dev, pmx);
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900723 if (IS_ERR(pmx->pctl)) {
Stephen Warren971dac72012-02-01 14:04:47 -0700724 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900725 return PTR_ERR(pmx->pctl);
Stephen Warren971dac72012-02-01 14:04:47 -0700726 }
727
Tomeu Vizoso94625102015-07-14 10:29:55 +0200728 if (!gpio_node_has_range())
729 pinctrl_add_gpio_range(pmx->pctl, &tegra_pinctrl_gpio_range);
Stephen Warren971dac72012-02-01 14:04:47 -0700730
731 platform_set_drvdata(pdev, pmx);
732
733 dev_dbg(&pdev->dev, "Probed Tegra pinctrl driver\n");
734
735 return 0;
736}
Stephen Warren52f48fe2012-04-11 12:53:09 -0600737EXPORT_SYMBOL_GPL(tegra_pinctrl_probe);
Stephen Warren971dac72012-02-01 14:04:47 -0700738
Bill Pembertonf90f54b2012-11-19 13:26:06 -0500739int tegra_pinctrl_remove(struct platform_device *pdev)
Stephen Warren971dac72012-02-01 14:04:47 -0700740{
741 struct tegra_pmx *pmx = platform_get_drvdata(pdev);
742
Stephen Warren971dac72012-02-01 14:04:47 -0700743 pinctrl_unregister(pmx->pctl);
744
745 return 0;
746}
Stephen Warren52f48fe2012-04-11 12:53:09 -0600747EXPORT_SYMBOL_GPL(tegra_pinctrl_remove);