• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Driver for the NVIDIA Tegra pinmux
3  *
4  * Copyright (c) 2011-2012, NVIDIA CORPORATION.  All rights reserved.
5  *
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>
25 #include <linux/of.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 
33 #include "core.h"
34 #include "pinctrl-tegra.h"
35 #include "pinctrl-utils.h"
36 
37 struct tegra_pmx {
38 	struct device *dev;
39 	struct pinctrl_dev *pctl;
40 
41 	const struct tegra_pinctrl_soc_data *soc;
42 	const char **group_pins;
43 
44 	int nbanks;
45 	void __iomem **regs;
46 };
47 
pmx_readl(struct tegra_pmx * pmx,u32 bank,u32 reg)48 static inline u32 pmx_readl(struct tegra_pmx *pmx, u32 bank, u32 reg)
49 {
50 	return readl(pmx->regs[bank] + reg);
51 }
52 
pmx_writel(struct tegra_pmx * pmx,u32 val,u32 bank,u32 reg)53 static inline void pmx_writel(struct tegra_pmx *pmx, u32 val, u32 bank, u32 reg)
54 {
55 	writel_relaxed(val, pmx->regs[bank] + reg);
56 	/* make sure pinmux register write completed */
57 	pmx_readl(pmx, bank, reg);
58 }
59 
tegra_pinctrl_get_groups_count(struct pinctrl_dev * pctldev)60 static int tegra_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
61 {
62 	struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
63 
64 	return pmx->soc->ngroups;
65 }
66 
tegra_pinctrl_get_group_name(struct pinctrl_dev * pctldev,unsigned group)67 static const char *tegra_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
68 						unsigned group)
69 {
70 	struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
71 
72 	return pmx->soc->groups[group].name;
73 }
74 
tegra_pinctrl_get_group_pins(struct pinctrl_dev * pctldev,unsigned group,const unsigned ** pins,unsigned * num_pins)75 static int tegra_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
76 					unsigned group,
77 					const unsigned **pins,
78 					unsigned *num_pins)
79 {
80 	struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
81 
82 	*pins = pmx->soc->groups[group].pins;
83 	*num_pins = pmx->soc->groups[group].npins;
84 
85 	return 0;
86 }
87 
88 #ifdef CONFIG_DEBUG_FS
tegra_pinctrl_pin_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned offset)89 static void tegra_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
90 				       struct seq_file *s,
91 				       unsigned offset)
92 {
93 	seq_printf(s, " %s", dev_name(pctldev->dev));
94 }
95 #endif
96 
97 static const struct cfg_param {
98 	const char *property;
99 	enum tegra_pinconf_param param;
100 } cfg_params[] = {
101 	{"nvidia,pull",			TEGRA_PINCONF_PARAM_PULL},
102 	{"nvidia,tristate",		TEGRA_PINCONF_PARAM_TRISTATE},
103 	{"nvidia,enable-input",		TEGRA_PINCONF_PARAM_ENABLE_INPUT},
104 	{"nvidia,open-drain",		TEGRA_PINCONF_PARAM_OPEN_DRAIN},
105 	{"nvidia,lock",			TEGRA_PINCONF_PARAM_LOCK},
106 	{"nvidia,io-reset",		TEGRA_PINCONF_PARAM_IORESET},
107 	{"nvidia,rcv-sel",		TEGRA_PINCONF_PARAM_RCV_SEL},
108 	{"nvidia,io-hv",		TEGRA_PINCONF_PARAM_RCV_SEL},
109 	{"nvidia,high-speed-mode",	TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE},
110 	{"nvidia,schmitt",		TEGRA_PINCONF_PARAM_SCHMITT},
111 	{"nvidia,low-power-mode",	TEGRA_PINCONF_PARAM_LOW_POWER_MODE},
112 	{"nvidia,pull-down-strength",	TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH},
113 	{"nvidia,pull-up-strength",	TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH},
114 	{"nvidia,slew-rate-falling",	TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING},
115 	{"nvidia,slew-rate-rising",	TEGRA_PINCONF_PARAM_SLEW_RATE_RISING},
116 	{"nvidia,drive-type",		TEGRA_PINCONF_PARAM_DRIVE_TYPE},
117 };
118 
tegra_pinctrl_dt_subnode_to_map(struct pinctrl_dev * pctldev,struct device_node * np,struct pinctrl_map ** map,unsigned * reserved_maps,unsigned * num_maps)119 static int tegra_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
120 					   struct device_node *np,
121 					   struct pinctrl_map **map,
122 					   unsigned *reserved_maps,
123 					   unsigned *num_maps)
124 {
125 	struct device *dev = pctldev->dev;
126 	int ret, i;
127 	const char *function;
128 	u32 val;
129 	unsigned long config;
130 	unsigned long *configs = NULL;
131 	unsigned num_configs = 0;
132 	unsigned reserve;
133 	struct property *prop;
134 	const char *group;
135 
136 	ret = of_property_read_string(np, "nvidia,function", &function);
137 	if (ret < 0) {
138 		/* EINVAL=missing, which is fine since it's optional */
139 		if (ret != -EINVAL)
140 			dev_err(dev,
141 				"could not parse property nvidia,function\n");
142 		function = NULL;
143 	}
144 
145 	for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
146 		ret = of_property_read_u32(np, cfg_params[i].property, &val);
147 		if (!ret) {
148 			config = TEGRA_PINCONF_PACK(cfg_params[i].param, val);
149 			ret = pinctrl_utils_add_config(pctldev, &configs,
150 					&num_configs, config);
151 			if (ret < 0)
152 				goto exit;
153 		/* EINVAL=missing, which is fine since it's optional */
154 		} else if (ret != -EINVAL) {
155 			dev_err(dev, "could not parse property %s\n",
156 				cfg_params[i].property);
157 		}
158 	}
159 
160 	reserve = 0;
161 	if (function != NULL)
162 		reserve++;
163 	if (num_configs)
164 		reserve++;
165 	ret = of_property_count_strings(np, "nvidia,pins");
166 	if (ret < 0) {
167 		dev_err(dev, "could not parse property nvidia,pins\n");
168 		goto exit;
169 	}
170 	reserve *= ret;
171 
172 	ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
173 					num_maps, reserve);
174 	if (ret < 0)
175 		goto exit;
176 
177 	of_property_for_each_string(np, "nvidia,pins", prop, group) {
178 		if (function) {
179 			ret = pinctrl_utils_add_map_mux(pctldev, map,
180 					reserved_maps, num_maps, group,
181 					function);
182 			if (ret < 0)
183 				goto exit;
184 		}
185 
186 		if (num_configs) {
187 			ret = pinctrl_utils_add_map_configs(pctldev, map,
188 					reserved_maps, num_maps, group,
189 					configs, num_configs,
190 					PIN_MAP_TYPE_CONFIGS_GROUP);
191 			if (ret < 0)
192 				goto exit;
193 		}
194 	}
195 
196 	ret = 0;
197 
198 exit:
199 	kfree(configs);
200 	return ret;
201 }
202 
tegra_pinctrl_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np_config,struct pinctrl_map ** map,unsigned * num_maps)203 static int tegra_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
204 					struct device_node *np_config,
205 					struct pinctrl_map **map,
206 					unsigned *num_maps)
207 {
208 	unsigned reserved_maps;
209 	struct device_node *np;
210 	int ret;
211 
212 	reserved_maps = 0;
213 	*map = NULL;
214 	*num_maps = 0;
215 
216 	for_each_child_of_node(np_config, np) {
217 		ret = tegra_pinctrl_dt_subnode_to_map(pctldev, np, map,
218 						      &reserved_maps, num_maps);
219 		if (ret < 0) {
220 			pinctrl_utils_dt_free_map(pctldev, *map,
221 				*num_maps);
222 			return ret;
223 		}
224 	}
225 
226 	return 0;
227 }
228 
229 static const struct pinctrl_ops tegra_pinctrl_ops = {
230 	.get_groups_count = tegra_pinctrl_get_groups_count,
231 	.get_group_name = tegra_pinctrl_get_group_name,
232 	.get_group_pins = tegra_pinctrl_get_group_pins,
233 #ifdef CONFIG_DEBUG_FS
234 	.pin_dbg_show = tegra_pinctrl_pin_dbg_show,
235 #endif
236 	.dt_node_to_map = tegra_pinctrl_dt_node_to_map,
237 	.dt_free_map = pinctrl_utils_dt_free_map,
238 };
239 
tegra_pinctrl_get_funcs_count(struct pinctrl_dev * pctldev)240 static int tegra_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
241 {
242 	struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
243 
244 	return pmx->soc->nfunctions;
245 }
246 
tegra_pinctrl_get_func_name(struct pinctrl_dev * pctldev,unsigned function)247 static const char *tegra_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
248 					       unsigned function)
249 {
250 	struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
251 
252 	return pmx->soc->functions[function].name;
253 }
254 
tegra_pinctrl_get_func_groups(struct pinctrl_dev * pctldev,unsigned function,const char * const ** groups,unsigned * const num_groups)255 static int tegra_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
256 					 unsigned function,
257 					 const char * const **groups,
258 					 unsigned * const num_groups)
259 {
260 	struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
261 
262 	*groups = pmx->soc->functions[function].groups;
263 	*num_groups = pmx->soc->functions[function].ngroups;
264 
265 	return 0;
266 }
267 
tegra_pinctrl_set_mux(struct pinctrl_dev * pctldev,unsigned function,unsigned group)268 static int tegra_pinctrl_set_mux(struct pinctrl_dev *pctldev,
269 				 unsigned function,
270 				 unsigned group)
271 {
272 	struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
273 	const struct tegra_pingroup *g;
274 	int i;
275 	u32 val;
276 
277 	g = &pmx->soc->groups[group];
278 
279 	if (WARN_ON(g->mux_reg < 0))
280 		return -EINVAL;
281 
282 	for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
283 		if (g->funcs[i] == function)
284 			break;
285 	}
286 	if (WARN_ON(i == ARRAY_SIZE(g->funcs)))
287 		return -EINVAL;
288 
289 	val = pmx_readl(pmx, g->mux_bank, g->mux_reg);
290 	val &= ~(0x3 << g->mux_bit);
291 	val |= i << g->mux_bit;
292 	pmx_writel(pmx, val, g->mux_bank, g->mux_reg);
293 
294 	return 0;
295 }
296 
297 static const struct pinmux_ops tegra_pinmux_ops = {
298 	.get_functions_count = tegra_pinctrl_get_funcs_count,
299 	.get_function_name = tegra_pinctrl_get_func_name,
300 	.get_function_groups = tegra_pinctrl_get_func_groups,
301 	.set_mux = tegra_pinctrl_set_mux,
302 };
303 
tegra_pinconf_reg(struct tegra_pmx * pmx,const struct tegra_pingroup * g,enum tegra_pinconf_param param,bool report_err,s8 * bank,s16 * reg,s8 * bit,s8 * width)304 static int tegra_pinconf_reg(struct tegra_pmx *pmx,
305 			     const struct tegra_pingroup *g,
306 			     enum tegra_pinconf_param param,
307 			     bool report_err,
308 			     s8 *bank, s16 *reg, s8 *bit, s8 *width)
309 {
310 	switch (param) {
311 	case TEGRA_PINCONF_PARAM_PULL:
312 		*bank = g->pupd_bank;
313 		*reg = g->pupd_reg;
314 		*bit = g->pupd_bit;
315 		*width = 2;
316 		break;
317 	case TEGRA_PINCONF_PARAM_TRISTATE:
318 		*bank = g->tri_bank;
319 		*reg = g->tri_reg;
320 		*bit = g->tri_bit;
321 		*width = 1;
322 		break;
323 	case TEGRA_PINCONF_PARAM_ENABLE_INPUT:
324 		*bank = g->mux_bank;
325 		*reg = g->mux_reg;
326 		*bit = g->einput_bit;
327 		*width = 1;
328 		break;
329 	case TEGRA_PINCONF_PARAM_OPEN_DRAIN:
330 		*bank = g->mux_bank;
331 		*reg = g->mux_reg;
332 		*bit = g->odrain_bit;
333 		*width = 1;
334 		break;
335 	case TEGRA_PINCONF_PARAM_LOCK:
336 		*bank = g->mux_bank;
337 		*reg = g->mux_reg;
338 		*bit = g->lock_bit;
339 		*width = 1;
340 		break;
341 	case TEGRA_PINCONF_PARAM_IORESET:
342 		*bank = g->mux_bank;
343 		*reg = g->mux_reg;
344 		*bit = g->ioreset_bit;
345 		*width = 1;
346 		break;
347 	case TEGRA_PINCONF_PARAM_RCV_SEL:
348 		*bank = g->mux_bank;
349 		*reg = g->mux_reg;
350 		*bit = g->rcv_sel_bit;
351 		*width = 1;
352 		break;
353 	case TEGRA_PINCONF_PARAM_HIGH_SPEED_MODE:
354 		if (pmx->soc->hsm_in_mux) {
355 			*bank = g->mux_bank;
356 			*reg = g->mux_reg;
357 		} else {
358 			*bank = g->drv_bank;
359 			*reg = g->drv_reg;
360 		}
361 		*bit = g->hsm_bit;
362 		*width = 1;
363 		break;
364 	case TEGRA_PINCONF_PARAM_SCHMITT:
365 		if (pmx->soc->schmitt_in_mux) {
366 			*bank = g->mux_bank;
367 			*reg = g->mux_reg;
368 		} else {
369 			*bank = g->drv_bank;
370 			*reg = g->drv_reg;
371 		}
372 		*bit = g->schmitt_bit;
373 		*width = 1;
374 		break;
375 	case TEGRA_PINCONF_PARAM_LOW_POWER_MODE:
376 		*bank = g->drv_bank;
377 		*reg = g->drv_reg;
378 		*bit = g->lpmd_bit;
379 		*width = 2;
380 		break;
381 	case TEGRA_PINCONF_PARAM_DRIVE_DOWN_STRENGTH:
382 		*bank = g->drv_bank;
383 		*reg = g->drv_reg;
384 		*bit = g->drvdn_bit;
385 		*width = g->drvdn_width;
386 		break;
387 	case TEGRA_PINCONF_PARAM_DRIVE_UP_STRENGTH:
388 		*bank = g->drv_bank;
389 		*reg = g->drv_reg;
390 		*bit = g->drvup_bit;
391 		*width = g->drvup_width;
392 		break;
393 	case TEGRA_PINCONF_PARAM_SLEW_RATE_FALLING:
394 		*bank = g->drv_bank;
395 		*reg = g->drv_reg;
396 		*bit = g->slwf_bit;
397 		*width = g->slwf_width;
398 		break;
399 	case TEGRA_PINCONF_PARAM_SLEW_RATE_RISING:
400 		*bank = g->drv_bank;
401 		*reg = g->drv_reg;
402 		*bit = g->slwr_bit;
403 		*width = g->slwr_width;
404 		break;
405 	case TEGRA_PINCONF_PARAM_DRIVE_TYPE:
406 		if (pmx->soc->drvtype_in_mux) {
407 			*bank = g->mux_bank;
408 			*reg = g->mux_reg;
409 		} else {
410 			*bank = g->drv_bank;
411 			*reg = g->drv_reg;
412 		}
413 		*bit = g->drvtype_bit;
414 		*width = 2;
415 		break;
416 	default:
417 		dev_err(pmx->dev, "Invalid config param %04x\n", param);
418 		return -ENOTSUPP;
419 	}
420 
421 	if (*reg < 0 || *bit < 0)  {
422 		if (report_err) {
423 			const char *prop = "unknown";
424 			int i;
425 
426 			for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
427 				if (cfg_params[i].param == param) {
428 					prop = cfg_params[i].property;
429 					break;
430 				}
431 			}
432 
433 			dev_err(pmx->dev,
434 				"Config param %04x (%s) not supported on group %s\n",
435 				param, prop, g->name);
436 		}
437 		return -ENOTSUPP;
438 	}
439 
440 	return 0;
441 }
442 
tegra_pinconf_get(struct pinctrl_dev * pctldev,unsigned pin,unsigned long * config)443 static int tegra_pinconf_get(struct pinctrl_dev *pctldev,
444 			     unsigned pin, unsigned long *config)
445 {
446 	dev_err(pctldev->dev, "pin_config_get op not supported\n");
447 	return -ENOTSUPP;
448 }
449 
tegra_pinconf_set(struct pinctrl_dev * pctldev,unsigned pin,unsigned long * configs,unsigned num_configs)450 static int tegra_pinconf_set(struct pinctrl_dev *pctldev,
451 			     unsigned pin, unsigned long *configs,
452 			     unsigned num_configs)
453 {
454 	dev_err(pctldev->dev, "pin_config_set op not supported\n");
455 	return -ENOTSUPP;
456 }
457 
tegra_pinconf_group_get(struct pinctrl_dev * pctldev,unsigned group,unsigned long * config)458 static int tegra_pinconf_group_get(struct pinctrl_dev *pctldev,
459 				   unsigned group, unsigned long *config)
460 {
461 	struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
462 	enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(*config);
463 	u16 arg;
464 	const struct tegra_pingroup *g;
465 	int ret;
466 	s8 bank, bit, width;
467 	s16 reg;
468 	u32 val, mask;
469 
470 	g = &pmx->soc->groups[group];
471 
472 	ret = tegra_pinconf_reg(pmx, g, param, true, &bank, &reg, &bit,
473 				&width);
474 	if (ret < 0)
475 		return ret;
476 
477 	val = pmx_readl(pmx, bank, reg);
478 	mask = (1 << width) - 1;
479 	arg = (val >> bit) & mask;
480 
481 	*config = TEGRA_PINCONF_PACK(param, arg);
482 
483 	return 0;
484 }
485 
tegra_pinconf_group_set(struct pinctrl_dev * pctldev,unsigned group,unsigned long * configs,unsigned num_configs)486 static int tegra_pinconf_group_set(struct pinctrl_dev *pctldev,
487 				   unsigned group, unsigned long *configs,
488 				   unsigned num_configs)
489 {
490 	struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
491 	enum tegra_pinconf_param param;
492 	u16 arg;
493 	const struct tegra_pingroup *g;
494 	int ret, i;
495 	s8 bank, bit, width;
496 	s16 reg;
497 	u32 val, mask;
498 
499 	g = &pmx->soc->groups[group];
500 
501 	for (i = 0; i < num_configs; i++) {
502 		param = TEGRA_PINCONF_UNPACK_PARAM(configs[i]);
503 		arg = TEGRA_PINCONF_UNPACK_ARG(configs[i]);
504 
505 		ret = tegra_pinconf_reg(pmx, g, param, true, &bank, &reg, &bit,
506 					&width);
507 		if (ret < 0)
508 			return ret;
509 
510 		val = pmx_readl(pmx, bank, reg);
511 
512 		/* LOCK can't be cleared */
513 		if (param == TEGRA_PINCONF_PARAM_LOCK) {
514 			if ((val & BIT(bit)) && !arg) {
515 				dev_err(pctldev->dev, "LOCK bit cannot be cleared\n");
516 				return -EINVAL;
517 			}
518 		}
519 
520 		/* Special-case Boolean values; allow any non-zero as true */
521 		if (width == 1)
522 			arg = !!arg;
523 
524 		/* Range-check user-supplied value */
525 		mask = (1 << width) - 1;
526 		if (arg & ~mask) {
527 			dev_err(pctldev->dev,
528 				"config %lx: %x too big for %d bit register\n",
529 				configs[i], arg, width);
530 			return -EINVAL;
531 		}
532 
533 		/* Update register */
534 		val &= ~(mask << bit);
535 		val |= arg << bit;
536 		pmx_writel(pmx, val, bank, reg);
537 	} /* for each config */
538 
539 	return 0;
540 }
541 
542 #ifdef CONFIG_DEBUG_FS
tegra_pinconf_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned offset)543 static void tegra_pinconf_dbg_show(struct pinctrl_dev *pctldev,
544 				   struct seq_file *s, unsigned offset)
545 {
546 }
547 
strip_prefix(const char * s)548 static const char *strip_prefix(const char *s)
549 {
550 	const char *comma = strchr(s, ',');
551 	if (!comma)
552 		return s;
553 
554 	return comma + 1;
555 }
556 
tegra_pinconf_group_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned group)557 static void tegra_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
558 					 struct seq_file *s, unsigned group)
559 {
560 	struct tegra_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
561 	const struct tegra_pingroup *g;
562 	int i, ret;
563 	s8 bank, bit, width;
564 	s16 reg;
565 	u32 val;
566 
567 	g = &pmx->soc->groups[group];
568 
569 	for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
570 		ret = tegra_pinconf_reg(pmx, g, cfg_params[i].param, false,
571 					&bank, &reg, &bit, &width);
572 		if (ret < 0)
573 			continue;
574 
575 		val = pmx_readl(pmx, bank, reg);
576 		val >>= bit;
577 		val &= (1 << width) - 1;
578 
579 		seq_printf(s, "\n\t%s=%u",
580 			   strip_prefix(cfg_params[i].property), val);
581 	}
582 }
583 
tegra_pinconf_config_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned long config)584 static void tegra_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
585 					  struct seq_file *s,
586 					  unsigned long config)
587 {
588 	enum tegra_pinconf_param param = TEGRA_PINCONF_UNPACK_PARAM(config);
589 	u16 arg = TEGRA_PINCONF_UNPACK_ARG(config);
590 	const char *pname = "unknown";
591 	int i;
592 
593 	for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
594 		if (cfg_params[i].param == param) {
595 			pname = cfg_params[i].property;
596 			break;
597 		}
598 	}
599 
600 	seq_printf(s, "%s=%d", strip_prefix(pname), arg);
601 }
602 #endif
603 
604 static const struct pinconf_ops tegra_pinconf_ops = {
605 	.pin_config_get = tegra_pinconf_get,
606 	.pin_config_set = tegra_pinconf_set,
607 	.pin_config_group_get = tegra_pinconf_group_get,
608 	.pin_config_group_set = tegra_pinconf_group_set,
609 #ifdef CONFIG_DEBUG_FS
610 	.pin_config_dbg_show = tegra_pinconf_dbg_show,
611 	.pin_config_group_dbg_show = tegra_pinconf_group_dbg_show,
612 	.pin_config_config_dbg_show = tegra_pinconf_config_dbg_show,
613 #endif
614 };
615 
616 static struct pinctrl_gpio_range tegra_pinctrl_gpio_range = {
617 	.name = "Tegra GPIOs",
618 	.id = 0,
619 	.base = 0,
620 };
621 
622 static struct pinctrl_desc tegra_pinctrl_desc = {
623 	.pctlops = &tegra_pinctrl_ops,
624 	.pmxops = &tegra_pinmux_ops,
625 	.confops = &tegra_pinconf_ops,
626 	.owner = THIS_MODULE,
627 };
628 
gpio_node_has_range(void)629 static bool gpio_node_has_range(void)
630 {
631 	struct device_node *np;
632 	bool has_prop = false;
633 
634 	np = of_find_compatible_node(NULL, NULL, "nvidia,tegra30-gpio");
635 	if (!np)
636 		return has_prop;
637 
638 	has_prop = of_find_property(np, "gpio-ranges", NULL);
639 
640 	of_node_put(np);
641 
642 	return has_prop;
643 }
644 
tegra_pinctrl_probe(struct platform_device * pdev,const struct tegra_pinctrl_soc_data * soc_data)645 int tegra_pinctrl_probe(struct platform_device *pdev,
646 			const struct tegra_pinctrl_soc_data *soc_data)
647 {
648 	struct tegra_pmx *pmx;
649 	struct resource *res;
650 	int i;
651 	const char **group_pins;
652 	int fn, gn, gfn;
653 
654 	pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
655 	if (!pmx) {
656 		dev_err(&pdev->dev, "Can't alloc tegra_pmx\n");
657 		return -ENOMEM;
658 	}
659 	pmx->dev = &pdev->dev;
660 	pmx->soc = soc_data;
661 
662 	/*
663 	 * Each mux group will appear in 4 functions' list of groups.
664 	 * This over-allocates slightly, since not all groups are mux groups.
665 	 */
666 	pmx->group_pins = devm_kzalloc(&pdev->dev,
667 		soc_data->ngroups * 4 * sizeof(*pmx->group_pins),
668 		GFP_KERNEL);
669 	if (!pmx->group_pins)
670 		return -ENOMEM;
671 
672 	group_pins = pmx->group_pins;
673 	for (fn = 0; fn < soc_data->nfunctions; fn++) {
674 		struct tegra_function *func = &soc_data->functions[fn];
675 
676 		func->groups = group_pins;
677 
678 		for (gn = 0; gn < soc_data->ngroups; gn++) {
679 			const struct tegra_pingroup *g = &soc_data->groups[gn];
680 
681 			if (g->mux_reg == -1)
682 				continue;
683 
684 			for (gfn = 0; gfn < 4; gfn++)
685 				if (g->funcs[gfn] == fn)
686 					break;
687 			if (gfn == 4)
688 				continue;
689 
690 			BUG_ON(group_pins - pmx->group_pins >=
691 				soc_data->ngroups * 4);
692 			*group_pins++ = g->name;
693 			func->ngroups++;
694 		}
695 	}
696 
697 	tegra_pinctrl_gpio_range.npins = pmx->soc->ngpios;
698 	tegra_pinctrl_desc.name = dev_name(&pdev->dev);
699 	tegra_pinctrl_desc.pins = pmx->soc->pins;
700 	tegra_pinctrl_desc.npins = pmx->soc->npins;
701 
702 	for (i = 0; ; i++) {
703 		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
704 		if (!res)
705 			break;
706 	}
707 	pmx->nbanks = i;
708 
709 	pmx->regs = devm_kzalloc(&pdev->dev, pmx->nbanks * sizeof(*pmx->regs),
710 				 GFP_KERNEL);
711 	if (!pmx->regs) {
712 		dev_err(&pdev->dev, "Can't alloc regs pointer\n");
713 		return -ENOMEM;
714 	}
715 
716 	for (i = 0; i < pmx->nbanks; i++) {
717 		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
718 		pmx->regs[i] = devm_ioremap_resource(&pdev->dev, res);
719 		if (IS_ERR(pmx->regs[i]))
720 			return PTR_ERR(pmx->regs[i]);
721 	}
722 
723 	pmx->pctl = pinctrl_register(&tegra_pinctrl_desc, &pdev->dev, pmx);
724 	if (IS_ERR(pmx->pctl)) {
725 		dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
726 		return PTR_ERR(pmx->pctl);
727 	}
728 
729 	if (!gpio_node_has_range())
730 		pinctrl_add_gpio_range(pmx->pctl, &tegra_pinctrl_gpio_range);
731 
732 	platform_set_drvdata(pdev, pmx);
733 
734 	dev_dbg(&pdev->dev, "Probed Tegra pinctrl driver\n");
735 
736 	return 0;
737 }
738 EXPORT_SYMBOL_GPL(tegra_pinctrl_probe);
739 
tegra_pinctrl_remove(struct platform_device * pdev)740 int tegra_pinctrl_remove(struct platform_device *pdev)
741 {
742 	struct tegra_pmx *pmx = platform_get_drvdata(pdev);
743 
744 	pinctrl_unregister(pmx->pctl);
745 
746 	return 0;
747 }
748 EXPORT_SYMBOL_GPL(tegra_pinctrl_remove);
749