xref: /linux/drivers/gpio/gpio-tegra186.c (revision 8f43640c9109bd82bcf37def6adfe1b8b09e7b61)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2016-2022 NVIDIA Corporation
4  *
5  * Author: Thierry Reding <treding@nvidia.com>
6  *	   Dipen Patel <dpatel@nvidia.com>
7  */
8 
9 #include <linux/gpio/driver.h>
10 #include <linux/hte.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
17 #include <linux/seq_file.h>
18 
19 #include <dt-bindings/gpio/tegra186-gpio.h>
20 #include <dt-bindings/gpio/tegra194-gpio.h>
21 #include <dt-bindings/gpio/tegra234-gpio.h>
22 #include <dt-bindings/gpio/tegra241-gpio.h>
23 
24 /* security registers */
25 #define TEGRA186_GPIO_CTL_SCR 0x0c
26 #define  TEGRA186_GPIO_CTL_SCR_SEC_WEN BIT(28)
27 #define  TEGRA186_GPIO_CTL_SCR_SEC_REN BIT(27)
28 
29 #define TEGRA186_GPIO_INT_ROUTE_MAPPING(p, x) (0x14 + (p) * 0x20 + (x) * 4)
30 
31 #define  TEGRA186_GPIO_VM			0x00
32 #define  TEGRA186_GPIO_VM_RW_MASK		0x03
33 #define  TEGRA186_GPIO_SCR			0x04
34 #define  TEGRA186_GPIO_SCR_PIN_SIZE		0x08
35 #define  TEGRA186_GPIO_SCR_PORT_SIZE		0x40
36 #define  TEGRA186_GPIO_SCR_SEC_WEN		BIT(28)
37 #define  TEGRA186_GPIO_SCR_SEC_REN		BIT(27)
38 #define  TEGRA186_GPIO_SCR_SEC_G1W		BIT(9)
39 #define  TEGRA186_GPIO_SCR_SEC_G1R		BIT(1)
40 
41 /* control registers */
42 #define TEGRA186_GPIO_ENABLE_CONFIG 0x00
43 #define  TEGRA186_GPIO_ENABLE_CONFIG_ENABLE BIT(0)
44 #define  TEGRA186_GPIO_ENABLE_CONFIG_OUT BIT(1)
45 #define  TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_NONE (0x0 << 2)
46 #define  TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_LEVEL (0x1 << 2)
47 #define  TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE (0x2 << 2)
48 #define  TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_DOUBLE_EDGE (0x3 << 2)
49 #define  TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_MASK (0x3 << 2)
50 #define  TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL BIT(4)
51 #define  TEGRA186_GPIO_ENABLE_CONFIG_DEBOUNCE BIT(5)
52 #define  TEGRA186_GPIO_ENABLE_CONFIG_INTERRUPT BIT(6)
53 #define  TEGRA186_GPIO_ENABLE_CONFIG_TIMESTAMP_FUNC BIT(7)
54 
55 #define TEGRA186_GPIO_DEBOUNCE_CONTROL 0x04
56 #define  TEGRA186_GPIO_DEBOUNCE_CONTROL_THRESHOLD(x) ((x) & 0xff)
57 
58 #define TEGRA186_GPIO_INPUT 0x08
59 #define  TEGRA186_GPIO_INPUT_HIGH BIT(0)
60 
61 #define TEGRA186_GPIO_OUTPUT_CONTROL 0x0c
62 #define  TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED BIT(0)
63 
64 #define TEGRA186_GPIO_OUTPUT_VALUE 0x10
65 #define  TEGRA186_GPIO_OUTPUT_VALUE_HIGH BIT(0)
66 
67 #define TEGRA186_GPIO_INTERRUPT_CLEAR 0x14
68 
69 #define TEGRA186_GPIO_INTERRUPT_STATUS(x) (0x100 + (x) * 4)
70 
71 struct tegra_gpio_port {
72 	const char *name;
73 	unsigned int bank;
74 	unsigned int port;
75 	unsigned int pins;
76 };
77 
78 struct tegra186_pin_range {
79 	unsigned int offset;
80 	const char *group;
81 };
82 
83 struct tegra_gpio_soc {
84 	const struct tegra_gpio_port *ports;
85 	unsigned int num_ports;
86 	const char *name;
87 	unsigned int instance;
88 
89 	unsigned int num_irqs_per_bank;
90 
91 	const struct tegra186_pin_range *pin_ranges;
92 	unsigned int num_pin_ranges;
93 	const char *pinmux;
94 	bool has_gte;
95 	bool has_vm_support;
96 };
97 
98 struct tegra_gpio {
99 	struct gpio_chip gpio;
100 	unsigned int num_irq;
101 	unsigned int *irq;
102 
103 	const struct tegra_gpio_soc *soc;
104 	unsigned int num_irqs_per_bank;
105 	unsigned int num_banks;
106 
107 	void __iomem *secure;
108 	void __iomem *base;
109 };
110 
111 static const struct tegra_gpio_port *
tegra186_gpio_get_port(struct tegra_gpio * gpio,unsigned int * pin)112 tegra186_gpio_get_port(struct tegra_gpio *gpio, unsigned int *pin)
113 {
114 	unsigned int start = 0, i;
115 
116 	for (i = 0; i < gpio->soc->num_ports; i++) {
117 		const struct tegra_gpio_port *port = &gpio->soc->ports[i];
118 
119 		if (*pin >= start && *pin < start + port->pins) {
120 			*pin -= start;
121 			return port;
122 		}
123 
124 		start += port->pins;
125 	}
126 
127 	return NULL;
128 }
129 
tegra186_gpio_get_base(struct tegra_gpio * gpio,unsigned int pin)130 static void __iomem *tegra186_gpio_get_base(struct tegra_gpio *gpio,
131 					    unsigned int pin)
132 {
133 	const struct tegra_gpio_port *port;
134 	unsigned int offset;
135 
136 	port = tegra186_gpio_get_port(gpio, &pin);
137 	if (!port)
138 		return NULL;
139 
140 	offset = port->bank * 0x1000 + port->port * 0x200;
141 
142 	return gpio->base + offset + pin * 0x20;
143 }
144 
tegra186_gpio_get_secure_base(struct tegra_gpio * gpio,unsigned int pin)145 static void __iomem *tegra186_gpio_get_secure_base(struct tegra_gpio *gpio,
146 						   unsigned int pin)
147 {
148 	const struct tegra_gpio_port *port;
149 	unsigned int offset;
150 
151 	port = tegra186_gpio_get_port(gpio, &pin);
152 	if (!port)
153 		return NULL;
154 
155 	offset = port->bank * 0x1000 + port->port * TEGRA186_GPIO_SCR_PORT_SIZE;
156 
157 	return gpio->secure + offset + pin * TEGRA186_GPIO_SCR_PIN_SIZE;
158 }
159 
tegra186_gpio_is_accessible(struct tegra_gpio * gpio,unsigned int pin)160 static inline bool tegra186_gpio_is_accessible(struct tegra_gpio *gpio, unsigned int pin)
161 {
162 	void __iomem *secure;
163 	u32 value;
164 
165 	secure = tegra186_gpio_get_secure_base(gpio, pin);
166 
167 	if (gpio->soc->has_vm_support) {
168 		value = readl(secure + TEGRA186_GPIO_VM);
169 		if ((value & TEGRA186_GPIO_VM_RW_MASK) != TEGRA186_GPIO_VM_RW_MASK)
170 			return false;
171 	}
172 
173 	value = __raw_readl(secure + TEGRA186_GPIO_SCR);
174 
175 	/*
176 	 * When SCR_SEC_[R|W]EN is unset, then we have full read/write access to all the
177 	 * registers for given GPIO pin.
178 	 * When SCR_SEC[R|W]EN is set, then there is need to further check the accompanying
179 	 * SCR_SEC_G1[R|W] bit to determine read/write access to all the registers for given
180 	 * GPIO pin.
181 	 */
182 
183 	if (((value & TEGRA186_GPIO_SCR_SEC_REN) == 0 ||
184 	     ((value & TEGRA186_GPIO_SCR_SEC_REN) && (value & TEGRA186_GPIO_SCR_SEC_G1R))) &&
185 	     ((value & TEGRA186_GPIO_SCR_SEC_WEN) == 0 ||
186 	     ((value & TEGRA186_GPIO_SCR_SEC_WEN) && (value & TEGRA186_GPIO_SCR_SEC_G1W))))
187 		return true;
188 
189 	return false;
190 }
191 
tegra186_init_valid_mask(struct gpio_chip * chip,unsigned long * valid_mask,unsigned int ngpios)192 static int tegra186_init_valid_mask(struct gpio_chip *chip,
193 				    unsigned long *valid_mask, unsigned int ngpios)
194 {
195 	struct tegra_gpio *gpio = gpiochip_get_data(chip);
196 	unsigned int j;
197 
198 	for (j = 0; j < ngpios; j++) {
199 		if (!tegra186_gpio_is_accessible(gpio, j))
200 			clear_bit(j, valid_mask);
201 	}
202 	return 0;
203 }
204 
tegra186_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)205 static int tegra186_gpio_get_direction(struct gpio_chip *chip,
206 				       unsigned int offset)
207 {
208 	struct tegra_gpio *gpio = gpiochip_get_data(chip);
209 	void __iomem *base;
210 	u32 value;
211 
212 	base = tegra186_gpio_get_base(gpio, offset);
213 	if (WARN_ON(base == NULL))
214 		return -ENODEV;
215 
216 	value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
217 	if (value & TEGRA186_GPIO_ENABLE_CONFIG_OUT)
218 		return GPIO_LINE_DIRECTION_OUT;
219 
220 	return GPIO_LINE_DIRECTION_IN;
221 }
222 
tegra186_gpio_direction_input(struct gpio_chip * chip,unsigned int offset)223 static int tegra186_gpio_direction_input(struct gpio_chip *chip,
224 					 unsigned int offset)
225 {
226 	struct tegra_gpio *gpio = gpiochip_get_data(chip);
227 	void __iomem *base;
228 	u32 value;
229 
230 	base = tegra186_gpio_get_base(gpio, offset);
231 	if (WARN_ON(base == NULL))
232 		return -ENODEV;
233 
234 	value = readl(base + TEGRA186_GPIO_OUTPUT_CONTROL);
235 	value |= TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED;
236 	writel(value, base + TEGRA186_GPIO_OUTPUT_CONTROL);
237 
238 	value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
239 	value |= TEGRA186_GPIO_ENABLE_CONFIG_ENABLE;
240 	value &= ~TEGRA186_GPIO_ENABLE_CONFIG_OUT;
241 	writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
242 
243 	return 0;
244 }
245 
tegra186_gpio_direction_output(struct gpio_chip * chip,unsigned int offset,int level)246 static int tegra186_gpio_direction_output(struct gpio_chip *chip,
247 					  unsigned int offset, int level)
248 {
249 	struct tegra_gpio *gpio = gpiochip_get_data(chip);
250 	void __iomem *base;
251 	u32 value;
252 
253 	/* configure output level first */
254 	chip->set(chip, offset, level);
255 
256 	base = tegra186_gpio_get_base(gpio, offset);
257 	if (WARN_ON(base == NULL))
258 		return -EINVAL;
259 
260 	/* set the direction */
261 	value = readl(base + TEGRA186_GPIO_OUTPUT_CONTROL);
262 	value &= ~TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED;
263 	writel(value, base + TEGRA186_GPIO_OUTPUT_CONTROL);
264 
265 	value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
266 	value |= TEGRA186_GPIO_ENABLE_CONFIG_ENABLE;
267 	value |= TEGRA186_GPIO_ENABLE_CONFIG_OUT;
268 	writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
269 
270 	return 0;
271 }
272 
273 #define HTE_BOTH_EDGES	(HTE_RISING_EDGE_TS | HTE_FALLING_EDGE_TS)
274 
tegra186_gpio_en_hw_ts(struct gpio_chip * gc,u32 offset,unsigned long flags)275 static int tegra186_gpio_en_hw_ts(struct gpio_chip *gc, u32 offset,
276 				  unsigned long flags)
277 {
278 	struct tegra_gpio *gpio;
279 	void __iomem *base;
280 	int value;
281 
282 	if (!gc)
283 		return -EINVAL;
284 
285 	gpio = gpiochip_get_data(gc);
286 	if (!gpio)
287 		return -ENODEV;
288 
289 	base = tegra186_gpio_get_base(gpio, offset);
290 	if (WARN_ON(base == NULL))
291 		return -EINVAL;
292 
293 	value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
294 	value |= TEGRA186_GPIO_ENABLE_CONFIG_TIMESTAMP_FUNC;
295 
296 	if (flags == HTE_BOTH_EDGES) {
297 		value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_DOUBLE_EDGE;
298 	} else if (flags == HTE_RISING_EDGE_TS) {
299 		value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
300 		value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
301 	} else if (flags == HTE_FALLING_EDGE_TS) {
302 		value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
303 	}
304 
305 	writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
306 
307 	return 0;
308 }
309 
tegra186_gpio_dis_hw_ts(struct gpio_chip * gc,u32 offset,unsigned long flags)310 static int tegra186_gpio_dis_hw_ts(struct gpio_chip *gc, u32 offset,
311 				   unsigned long flags)
312 {
313 	struct tegra_gpio *gpio;
314 	void __iomem *base;
315 	int value;
316 
317 	if (!gc)
318 		return -EINVAL;
319 
320 	gpio = gpiochip_get_data(gc);
321 	if (!gpio)
322 		return -ENODEV;
323 
324 	base = tegra186_gpio_get_base(gpio, offset);
325 	if (WARN_ON(base == NULL))
326 		return -EINVAL;
327 
328 	value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
329 	value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TIMESTAMP_FUNC;
330 	if (flags == HTE_BOTH_EDGES) {
331 		value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_DOUBLE_EDGE;
332 	} else if (flags == HTE_RISING_EDGE_TS) {
333 		value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
334 		value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
335 	} else if (flags == HTE_FALLING_EDGE_TS) {
336 		value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
337 	}
338 	writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
339 
340 	return 0;
341 }
342 
tegra186_gpio_get(struct gpio_chip * chip,unsigned int offset)343 static int tegra186_gpio_get(struct gpio_chip *chip, unsigned int offset)
344 {
345 	struct tegra_gpio *gpio = gpiochip_get_data(chip);
346 	void __iomem *base;
347 	u32 value;
348 
349 	base = tegra186_gpio_get_base(gpio, offset);
350 	if (WARN_ON(base == NULL))
351 		return -ENODEV;
352 
353 	value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
354 	if (value & TEGRA186_GPIO_ENABLE_CONFIG_OUT)
355 		value = readl(base + TEGRA186_GPIO_OUTPUT_VALUE);
356 	else
357 		value = readl(base + TEGRA186_GPIO_INPUT);
358 
359 	return value & BIT(0);
360 }
361 
tegra186_gpio_set(struct gpio_chip * chip,unsigned int offset,int level)362 static void tegra186_gpio_set(struct gpio_chip *chip, unsigned int offset,
363 			      int level)
364 {
365 	struct tegra_gpio *gpio = gpiochip_get_data(chip);
366 	void __iomem *base;
367 	u32 value;
368 
369 	base = tegra186_gpio_get_base(gpio, offset);
370 	if (WARN_ON(base == NULL))
371 		return;
372 
373 	value = readl(base + TEGRA186_GPIO_OUTPUT_VALUE);
374 	if (level == 0)
375 		value &= ~TEGRA186_GPIO_OUTPUT_VALUE_HIGH;
376 	else
377 		value |= TEGRA186_GPIO_OUTPUT_VALUE_HIGH;
378 
379 	writel(value, base + TEGRA186_GPIO_OUTPUT_VALUE);
380 }
381 
tegra186_gpio_set_config(struct gpio_chip * chip,unsigned int offset,unsigned long config)382 static int tegra186_gpio_set_config(struct gpio_chip *chip,
383 				    unsigned int offset,
384 				    unsigned long config)
385 {
386 	struct tegra_gpio *gpio = gpiochip_get_data(chip);
387 	u32 debounce, value;
388 	void __iomem *base;
389 
390 	base = tegra186_gpio_get_base(gpio, offset);
391 	if (base == NULL)
392 		return -ENXIO;
393 
394 	if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
395 		return -ENOTSUPP;
396 
397 	debounce = pinconf_to_config_argument(config);
398 
399 	/*
400 	 * The Tegra186 GPIO controller supports a maximum of 255 ms debounce
401 	 * time.
402 	 */
403 	if (debounce > 255000)
404 		return -EINVAL;
405 
406 	debounce = DIV_ROUND_UP(debounce, USEC_PER_MSEC);
407 
408 	value = TEGRA186_GPIO_DEBOUNCE_CONTROL_THRESHOLD(debounce);
409 	writel(value, base + TEGRA186_GPIO_DEBOUNCE_CONTROL);
410 
411 	value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
412 	value |= TEGRA186_GPIO_ENABLE_CONFIG_DEBOUNCE;
413 	writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
414 
415 	return 0;
416 }
417 
tegra186_gpio_add_pin_ranges(struct gpio_chip * chip)418 static int tegra186_gpio_add_pin_ranges(struct gpio_chip *chip)
419 {
420 	struct tegra_gpio *gpio = gpiochip_get_data(chip);
421 	struct pinctrl_dev *pctldev;
422 	struct device_node *np;
423 	unsigned int i, j;
424 	int err;
425 
426 	if (!gpio->soc->pinmux || gpio->soc->num_pin_ranges == 0)
427 		return 0;
428 
429 	np = of_find_compatible_node(NULL, NULL, gpio->soc->pinmux);
430 	if (!np)
431 		return -ENODEV;
432 
433 	pctldev = of_pinctrl_get(np);
434 	of_node_put(np);
435 	if (!pctldev)
436 		return -EPROBE_DEFER;
437 
438 	for (i = 0; i < gpio->soc->num_pin_ranges; i++) {
439 		unsigned int pin = gpio->soc->pin_ranges[i].offset, port;
440 		const char *group = gpio->soc->pin_ranges[i].group;
441 
442 		port = pin / 8;
443 		pin = pin % 8;
444 
445 		if (port >= gpio->soc->num_ports) {
446 			dev_warn(chip->parent, "invalid port %u for %s\n",
447 				 port, group);
448 			continue;
449 		}
450 
451 		for (j = 0; j < port; j++)
452 			pin += gpio->soc->ports[j].pins;
453 
454 		err = gpiochip_add_pingroup_range(chip, pctldev, pin, group);
455 		if (err < 0)
456 			return err;
457 	}
458 
459 	return 0;
460 }
461 
tegra186_gpio_of_xlate(struct gpio_chip * chip,const struct of_phandle_args * spec,u32 * flags)462 static int tegra186_gpio_of_xlate(struct gpio_chip *chip,
463 				  const struct of_phandle_args *spec,
464 				  u32 *flags)
465 {
466 	struct tegra_gpio *gpio = gpiochip_get_data(chip);
467 	unsigned int port, pin, i, offset = 0;
468 
469 	if (WARN_ON(chip->of_gpio_n_cells < 2))
470 		return -EINVAL;
471 
472 	if (WARN_ON(spec->args_count < chip->of_gpio_n_cells))
473 		return -EINVAL;
474 
475 	port = spec->args[0] / 8;
476 	pin = spec->args[0] % 8;
477 
478 	if (port >= gpio->soc->num_ports) {
479 		dev_err(chip->parent, "invalid port number: %u\n", port);
480 		return -EINVAL;
481 	}
482 
483 	for (i = 0; i < port; i++)
484 		offset += gpio->soc->ports[i].pins;
485 
486 	if (flags)
487 		*flags = spec->args[1];
488 
489 	return offset + pin;
490 }
491 
492 #define to_tegra_gpio(x) container_of((x), struct tegra_gpio, gpio)
493 
tegra186_irq_ack(struct irq_data * data)494 static void tegra186_irq_ack(struct irq_data *data)
495 {
496 	struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
497 	struct tegra_gpio *gpio = to_tegra_gpio(gc);
498 	void __iomem *base;
499 
500 	base = tegra186_gpio_get_base(gpio, data->hwirq);
501 	if (WARN_ON(base == NULL))
502 		return;
503 
504 	writel(1, base + TEGRA186_GPIO_INTERRUPT_CLEAR);
505 }
506 
tegra186_irq_mask(struct irq_data * data)507 static void tegra186_irq_mask(struct irq_data *data)
508 {
509 	struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
510 	struct tegra_gpio *gpio = to_tegra_gpio(gc);
511 	void __iomem *base;
512 	u32 value;
513 
514 	base = tegra186_gpio_get_base(gpio, data->hwirq);
515 	if (WARN_ON(base == NULL))
516 		return;
517 
518 	value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
519 	value &= ~TEGRA186_GPIO_ENABLE_CONFIG_INTERRUPT;
520 	writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
521 
522 	gpiochip_disable_irq(&gpio->gpio, data->hwirq);
523 }
524 
tegra186_irq_unmask(struct irq_data * data)525 static void tegra186_irq_unmask(struct irq_data *data)
526 {
527 	struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
528 	struct tegra_gpio *gpio = to_tegra_gpio(gc);
529 	void __iomem *base;
530 	u32 value;
531 
532 	base = tegra186_gpio_get_base(gpio, data->hwirq);
533 	if (WARN_ON(base == NULL))
534 		return;
535 
536 	gpiochip_enable_irq(&gpio->gpio, data->hwirq);
537 
538 	value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
539 	value |= TEGRA186_GPIO_ENABLE_CONFIG_INTERRUPT;
540 	writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
541 }
542 
tegra186_irq_set_type(struct irq_data * data,unsigned int type)543 static int tegra186_irq_set_type(struct irq_data *data, unsigned int type)
544 {
545 	struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
546 	struct tegra_gpio *gpio = to_tegra_gpio(gc);
547 	void __iomem *base;
548 	u32 value;
549 
550 	base = tegra186_gpio_get_base(gpio, data->hwirq);
551 	if (WARN_ON(base == NULL))
552 		return -ENODEV;
553 
554 	value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
555 	value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_MASK;
556 	value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
557 
558 	switch (type & IRQ_TYPE_SENSE_MASK) {
559 	case IRQ_TYPE_NONE:
560 		break;
561 
562 	case IRQ_TYPE_EDGE_RISING:
563 		value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
564 		value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
565 		break;
566 
567 	case IRQ_TYPE_EDGE_FALLING:
568 		value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
569 		break;
570 
571 	case IRQ_TYPE_EDGE_BOTH:
572 		value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_DOUBLE_EDGE;
573 		break;
574 
575 	case IRQ_TYPE_LEVEL_HIGH:
576 		value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_LEVEL;
577 		value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
578 		break;
579 
580 	case IRQ_TYPE_LEVEL_LOW:
581 		value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_LEVEL;
582 		break;
583 
584 	default:
585 		return -EINVAL;
586 	}
587 
588 	writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
589 
590 	if ((type & IRQ_TYPE_EDGE_BOTH) == 0)
591 		irq_set_handler_locked(data, handle_level_irq);
592 	else
593 		irq_set_handler_locked(data, handle_edge_irq);
594 
595 	if (data->parent_data)
596 		return irq_chip_set_type_parent(data, type);
597 
598 	return 0;
599 }
600 
tegra186_irq_set_wake(struct irq_data * data,unsigned int on)601 static int tegra186_irq_set_wake(struct irq_data *data, unsigned int on)
602 {
603 	if (data->parent_data)
604 		return irq_chip_set_wake_parent(data, on);
605 
606 	return 0;
607 }
608 
tegra186_irq_print_chip(struct irq_data * data,struct seq_file * p)609 static void tegra186_irq_print_chip(struct irq_data *data, struct seq_file *p)
610 {
611 	struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
612 
613 	seq_puts(p, dev_name(gc->parent));
614 }
615 
616 static const struct irq_chip tegra186_gpio_irq_chip = {
617 	.irq_ack		= tegra186_irq_ack,
618 	.irq_mask		= tegra186_irq_mask,
619 	.irq_unmask		= tegra186_irq_unmask,
620 	.irq_set_type		= tegra186_irq_set_type,
621 	.irq_set_wake		= tegra186_irq_set_wake,
622 	.irq_print_chip		= tegra186_irq_print_chip,
623 	.flags			= IRQCHIP_IMMUTABLE,
624 	GPIOCHIP_IRQ_RESOURCE_HELPERS,
625 };
626 
tegra186_gpio_irq(struct irq_desc * desc)627 static void tegra186_gpio_irq(struct irq_desc *desc)
628 {
629 	struct tegra_gpio *gpio = irq_desc_get_handler_data(desc);
630 	struct irq_domain *domain = gpio->gpio.irq.domain;
631 	struct irq_chip *chip = irq_desc_get_chip(desc);
632 	unsigned int parent = irq_desc_get_irq(desc);
633 	unsigned int i, j, offset = 0;
634 
635 	chained_irq_enter(chip, desc);
636 
637 	for (i = 0; i < gpio->soc->num_ports; i++) {
638 		const struct tegra_gpio_port *port = &gpio->soc->ports[i];
639 		unsigned int pin;
640 		unsigned long value;
641 		void __iomem *base;
642 
643 		base = gpio->base + port->bank * 0x1000 + port->port * 0x200;
644 
645 		/* skip ports that are not associated with this bank */
646 		for (j = 0; j < gpio->num_irqs_per_bank; j++) {
647 			if (parent == gpio->irq[port->bank * gpio->num_irqs_per_bank + j])
648 				break;
649 		}
650 
651 		if (j == gpio->num_irqs_per_bank)
652 			goto skip;
653 
654 		value = readl(base + TEGRA186_GPIO_INTERRUPT_STATUS(1));
655 
656 		for_each_set_bit(pin, &value, port->pins) {
657 			int ret = generic_handle_domain_irq(domain, offset + pin);
658 			WARN_RATELIMIT(ret, "hwirq = %d", offset + pin);
659 		}
660 
661 skip:
662 		offset += port->pins;
663 	}
664 
665 	chained_irq_exit(chip, desc);
666 }
667 
tegra186_gpio_irq_domain_translate(struct irq_domain * domain,struct irq_fwspec * fwspec,unsigned long * hwirq,unsigned int * type)668 static int tegra186_gpio_irq_domain_translate(struct irq_domain *domain,
669 					      struct irq_fwspec *fwspec,
670 					      unsigned long *hwirq,
671 					      unsigned int *type)
672 {
673 	struct tegra_gpio *gpio = gpiochip_get_data(domain->host_data);
674 	unsigned int port, pin, i, offset = 0;
675 
676 	if (WARN_ON(gpio->gpio.of_gpio_n_cells < 2))
677 		return -EINVAL;
678 
679 	if (WARN_ON(fwspec->param_count < gpio->gpio.of_gpio_n_cells))
680 		return -EINVAL;
681 
682 	port = fwspec->param[0] / 8;
683 	pin = fwspec->param[0] % 8;
684 
685 	if (port >= gpio->soc->num_ports)
686 		return -EINVAL;
687 
688 	for (i = 0; i < port; i++)
689 		offset += gpio->soc->ports[i].pins;
690 
691 	*type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
692 	*hwirq = offset + pin;
693 
694 	return 0;
695 }
696 
tegra186_gpio_populate_parent_fwspec(struct gpio_chip * chip,union gpio_irq_fwspec * gfwspec,unsigned int parent_hwirq,unsigned int parent_type)697 static int tegra186_gpio_populate_parent_fwspec(struct gpio_chip *chip,
698 						union gpio_irq_fwspec *gfwspec,
699 						unsigned int parent_hwirq,
700 						unsigned int parent_type)
701 {
702 	struct tegra_gpio *gpio = gpiochip_get_data(chip);
703 	struct irq_fwspec *fwspec = &gfwspec->fwspec;
704 
705 	fwspec->fwnode = chip->irq.parent_domain->fwnode;
706 	fwspec->param_count = 3;
707 	fwspec->param[0] = gpio->soc->instance;
708 	fwspec->param[1] = parent_hwirq;
709 	fwspec->param[2] = parent_type;
710 
711 	return 0;
712 }
713 
tegra186_gpio_child_to_parent_hwirq(struct gpio_chip * chip,unsigned int hwirq,unsigned int type,unsigned int * parent_hwirq,unsigned int * parent_type)714 static int tegra186_gpio_child_to_parent_hwirq(struct gpio_chip *chip,
715 					       unsigned int hwirq,
716 					       unsigned int type,
717 					       unsigned int *parent_hwirq,
718 					       unsigned int *parent_type)
719 {
720 	*parent_hwirq = chip->irq.child_offset_to_irq(chip, hwirq);
721 	*parent_type = type;
722 
723 	return 0;
724 }
725 
tegra186_gpio_child_offset_to_irq(struct gpio_chip * chip,unsigned int offset)726 static unsigned int tegra186_gpio_child_offset_to_irq(struct gpio_chip *chip,
727 						      unsigned int offset)
728 {
729 	struct tegra_gpio *gpio = gpiochip_get_data(chip);
730 	unsigned int i;
731 
732 	for (i = 0; i < gpio->soc->num_ports; i++) {
733 		if (offset < gpio->soc->ports[i].pins)
734 			break;
735 
736 		offset -= gpio->soc->ports[i].pins;
737 	}
738 
739 	return offset + i * 8;
740 }
741 
742 static const struct of_device_id tegra186_pmc_of_match[] = {
743 	{ .compatible = "nvidia,tegra186-pmc" },
744 	{ .compatible = "nvidia,tegra194-pmc" },
745 	{ .compatible = "nvidia,tegra234-pmc" },
746 	{ /* sentinel */ }
747 };
748 
tegra186_gpio_init_route_mapping(struct tegra_gpio * gpio)749 static void tegra186_gpio_init_route_mapping(struct tegra_gpio *gpio)
750 {
751 	struct device *dev = gpio->gpio.parent;
752 	unsigned int i;
753 	u32 value;
754 
755 	for (i = 0; i < gpio->soc->num_ports; i++) {
756 		const struct tegra_gpio_port *port = &gpio->soc->ports[i];
757 		unsigned int offset, p = port->port;
758 		void __iomem *base;
759 
760 		base = gpio->secure + port->bank * 0x1000 + 0x800;
761 
762 		value = readl(base + TEGRA186_GPIO_CTL_SCR);
763 
764 		/*
765 		 * For controllers that haven't been locked down yet, make
766 		 * sure to program the default interrupt route mapping.
767 		 */
768 		if ((value & TEGRA186_GPIO_CTL_SCR_SEC_REN) == 0 &&
769 		    (value & TEGRA186_GPIO_CTL_SCR_SEC_WEN) == 0) {
770 			/*
771 			 * On Tegra194 and later, each pin can be routed to one or more
772 			 * interrupts.
773 			 */
774 			dev_dbg(dev, "programming default interrupt routing for port %s\n",
775 				port->name);
776 
777 			offset = TEGRA186_GPIO_INT_ROUTE_MAPPING(p, 0);
778 
779 			/*
780 			 * By default we only want to route GPIO pins to IRQ 0. This works
781 			 * only under the assumption that we're running as the host kernel
782 			 * and hence all GPIO pins are owned by Linux.
783 			 *
784 			 * For cases where Linux is the guest OS, the hypervisor will have
785 			 * to configure the interrupt routing and pass only the valid
786 			 * interrupts via device tree.
787 			 */
788 			value = readl(base + offset);
789 			value = BIT(port->pins) - 1;
790 			writel(value, base + offset);
791 		}
792 	}
793 }
794 
tegra186_gpio_irqs_per_bank(struct tegra_gpio * gpio)795 static unsigned int tegra186_gpio_irqs_per_bank(struct tegra_gpio *gpio)
796 {
797 	struct device *dev = gpio->gpio.parent;
798 
799 	if (gpio->num_irq > gpio->num_banks) {
800 		if (gpio->num_irq % gpio->num_banks != 0)
801 			goto error;
802 	}
803 
804 	if (gpio->num_irq < gpio->num_banks)
805 		goto error;
806 
807 	gpio->num_irqs_per_bank = gpio->num_irq / gpio->num_banks;
808 
809 	if (gpio->num_irqs_per_bank > gpio->soc->num_irqs_per_bank)
810 		goto error;
811 
812 	return 0;
813 
814 error:
815 	dev_err(dev, "invalid number of interrupts (%u) for %u banks\n",
816 		gpio->num_irq, gpio->num_banks);
817 	return -EINVAL;
818 }
819 
tegra186_gpio_probe(struct platform_device * pdev)820 static int tegra186_gpio_probe(struct platform_device *pdev)
821 {
822 	unsigned int i, j, offset;
823 	struct gpio_irq_chip *irq;
824 	struct tegra_gpio *gpio;
825 	struct device_node *np;
826 	struct resource *res;
827 	char **names;
828 	int err;
829 
830 	gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
831 	if (!gpio)
832 		return -ENOMEM;
833 
834 	gpio->soc = device_get_match_data(&pdev->dev);
835 	gpio->gpio.label = gpio->soc->name;
836 	gpio->gpio.parent = &pdev->dev;
837 
838 	/* count the number of banks in the controller */
839 	for (i = 0; i < gpio->soc->num_ports; i++)
840 		if (gpio->soc->ports[i].bank > gpio->num_banks)
841 			gpio->num_banks = gpio->soc->ports[i].bank;
842 
843 	gpio->num_banks++;
844 
845 	/* get register apertures */
846 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "security");
847 	if (!res)
848 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
849 	gpio->secure = devm_ioremap_resource(&pdev->dev, res);
850 	if (IS_ERR(gpio->secure))
851 		return PTR_ERR(gpio->secure);
852 
853 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "gpio");
854 	if (!res)
855 		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
856 	gpio->base = devm_ioremap_resource(&pdev->dev, res);
857 	if (IS_ERR(gpio->base))
858 		return PTR_ERR(gpio->base);
859 
860 	err = platform_irq_count(pdev);
861 	if (err < 0)
862 		return err;
863 
864 	gpio->num_irq = err;
865 
866 	err = tegra186_gpio_irqs_per_bank(gpio);
867 	if (err < 0)
868 		return err;
869 
870 	gpio->irq = devm_kcalloc(&pdev->dev, gpio->num_irq, sizeof(*gpio->irq),
871 				 GFP_KERNEL);
872 	if (!gpio->irq)
873 		return -ENOMEM;
874 
875 	for (i = 0; i < gpio->num_irq; i++) {
876 		err = platform_get_irq(pdev, i);
877 		if (err < 0)
878 			return err;
879 
880 		gpio->irq[i] = err;
881 	}
882 
883 	gpio->gpio.request = gpiochip_generic_request;
884 	gpio->gpio.free = gpiochip_generic_free;
885 	gpio->gpio.get_direction = tegra186_gpio_get_direction;
886 	gpio->gpio.direction_input = tegra186_gpio_direction_input;
887 	gpio->gpio.direction_output = tegra186_gpio_direction_output;
888 	gpio->gpio.get = tegra186_gpio_get;
889 	gpio->gpio.set = tegra186_gpio_set;
890 	gpio->gpio.set_config = tegra186_gpio_set_config;
891 	gpio->gpio.add_pin_ranges = tegra186_gpio_add_pin_ranges;
892 	gpio->gpio.init_valid_mask = tegra186_init_valid_mask;
893 	if (gpio->soc->has_gte) {
894 		gpio->gpio.en_hw_timestamp = tegra186_gpio_en_hw_ts;
895 		gpio->gpio.dis_hw_timestamp = tegra186_gpio_dis_hw_ts;
896 	}
897 
898 	gpio->gpio.base = -1;
899 
900 	for (i = 0; i < gpio->soc->num_ports; i++)
901 		gpio->gpio.ngpio += gpio->soc->ports[i].pins;
902 
903 	names = devm_kcalloc(gpio->gpio.parent, gpio->gpio.ngpio,
904 			     sizeof(*names), GFP_KERNEL);
905 	if (!names)
906 		return -ENOMEM;
907 
908 	for (i = 0, offset = 0; i < gpio->soc->num_ports; i++) {
909 		const struct tegra_gpio_port *port = &gpio->soc->ports[i];
910 		char *name;
911 
912 		for (j = 0; j < port->pins; j++) {
913 			name = devm_kasprintf(gpio->gpio.parent, GFP_KERNEL,
914 					      "P%s.%02x", port->name, j);
915 			if (!name)
916 				return -ENOMEM;
917 
918 			names[offset + j] = name;
919 		}
920 
921 		offset += port->pins;
922 	}
923 
924 	gpio->gpio.names = (const char * const *)names;
925 
926 #if defined(CONFIG_OF_GPIO)
927 	gpio->gpio.of_gpio_n_cells = 2;
928 	gpio->gpio.of_xlate = tegra186_gpio_of_xlate;
929 #endif /* CONFIG_OF_GPIO */
930 
931 	irq = &gpio->gpio.irq;
932 	gpio_irq_chip_set_chip(irq, &tegra186_gpio_irq_chip);
933 	irq->fwnode = dev_fwnode(&pdev->dev);
934 	irq->child_to_parent_hwirq = tegra186_gpio_child_to_parent_hwirq;
935 	irq->populate_parent_alloc_arg = tegra186_gpio_populate_parent_fwspec;
936 	irq->child_offset_to_irq = tegra186_gpio_child_offset_to_irq;
937 	irq->child_irq_domain_ops.translate = tegra186_gpio_irq_domain_translate;
938 	irq->handler = handle_simple_irq;
939 	irq->default_type = IRQ_TYPE_NONE;
940 	irq->parent_handler = tegra186_gpio_irq;
941 	irq->parent_handler_data = gpio;
942 	irq->num_parents = gpio->num_irq;
943 
944 	/*
945 	 * To simplify things, use a single interrupt per bank for now. Some
946 	 * chips support up to 8 interrupts per bank, which can be useful to
947 	 * distribute the load and decrease the processing latency for GPIOs
948 	 * but it also requires a more complicated interrupt routing than we
949 	 * currently program.
950 	 */
951 	if (gpio->num_irqs_per_bank > 1) {
952 		irq->parents = devm_kcalloc(&pdev->dev, gpio->num_banks,
953 					    sizeof(*irq->parents), GFP_KERNEL);
954 		if (!irq->parents)
955 			return -ENOMEM;
956 
957 		for (i = 0; i < gpio->num_banks; i++)
958 			irq->parents[i] = gpio->irq[i * gpio->num_irqs_per_bank];
959 
960 		irq->num_parents = gpio->num_banks;
961 	} else {
962 		irq->num_parents = gpio->num_irq;
963 		irq->parents = gpio->irq;
964 	}
965 
966 	if (gpio->soc->num_irqs_per_bank > 1)
967 		tegra186_gpio_init_route_mapping(gpio);
968 
969 	np = of_find_matching_node(NULL, tegra186_pmc_of_match);
970 	if (np) {
971 		if (of_device_is_available(np)) {
972 			irq->parent_domain = irq_find_host(np);
973 			of_node_put(np);
974 
975 			if (!irq->parent_domain)
976 				return -EPROBE_DEFER;
977 		} else {
978 			of_node_put(np);
979 		}
980 	}
981 
982 	irq->map = devm_kcalloc(&pdev->dev, gpio->gpio.ngpio,
983 				sizeof(*irq->map), GFP_KERNEL);
984 	if (!irq->map)
985 		return -ENOMEM;
986 
987 	for (i = 0, offset = 0; i < gpio->soc->num_ports; i++) {
988 		const struct tegra_gpio_port *port = &gpio->soc->ports[i];
989 
990 		for (j = 0; j < port->pins; j++)
991 			irq->map[offset + j] = irq->parents[port->bank];
992 
993 		offset += port->pins;
994 	}
995 
996 	return devm_gpiochip_add_data(&pdev->dev, &gpio->gpio, gpio);
997 }
998 
999 #define TEGRA186_MAIN_GPIO_PORT(_name, _bank, _port, _pins)	\
1000 	[TEGRA186_MAIN_GPIO_PORT_##_name] = {			\
1001 		.name = #_name,					\
1002 		.bank = _bank,					\
1003 		.port = _port,					\
1004 		.pins = _pins,					\
1005 	}
1006 
1007 static const struct tegra_gpio_port tegra186_main_ports[] = {
1008 	TEGRA186_MAIN_GPIO_PORT( A, 2, 0, 7),
1009 	TEGRA186_MAIN_GPIO_PORT( B, 3, 0, 7),
1010 	TEGRA186_MAIN_GPIO_PORT( C, 3, 1, 7),
1011 	TEGRA186_MAIN_GPIO_PORT( D, 3, 2, 6),
1012 	TEGRA186_MAIN_GPIO_PORT( E, 2, 1, 8),
1013 	TEGRA186_MAIN_GPIO_PORT( F, 2, 2, 6),
1014 	TEGRA186_MAIN_GPIO_PORT( G, 4, 1, 6),
1015 	TEGRA186_MAIN_GPIO_PORT( H, 1, 0, 7),
1016 	TEGRA186_MAIN_GPIO_PORT( I, 0, 4, 8),
1017 	TEGRA186_MAIN_GPIO_PORT( J, 5, 0, 8),
1018 	TEGRA186_MAIN_GPIO_PORT( K, 5, 1, 1),
1019 	TEGRA186_MAIN_GPIO_PORT( L, 1, 1, 8),
1020 	TEGRA186_MAIN_GPIO_PORT( M, 5, 3, 6),
1021 	TEGRA186_MAIN_GPIO_PORT( N, 0, 0, 7),
1022 	TEGRA186_MAIN_GPIO_PORT( O, 0, 1, 4),
1023 	TEGRA186_MAIN_GPIO_PORT( P, 4, 0, 7),
1024 	TEGRA186_MAIN_GPIO_PORT( Q, 0, 2, 6),
1025 	TEGRA186_MAIN_GPIO_PORT( R, 0, 5, 6),
1026 	TEGRA186_MAIN_GPIO_PORT( T, 0, 3, 4),
1027 	TEGRA186_MAIN_GPIO_PORT( X, 1, 2, 8),
1028 	TEGRA186_MAIN_GPIO_PORT( Y, 1, 3, 7),
1029 	TEGRA186_MAIN_GPIO_PORT(BB, 2, 3, 2),
1030 	TEGRA186_MAIN_GPIO_PORT(CC, 5, 2, 4),
1031 };
1032 
1033 static const struct tegra_gpio_soc tegra186_main_soc = {
1034 	.num_ports = ARRAY_SIZE(tegra186_main_ports),
1035 	.ports = tegra186_main_ports,
1036 	.name = "tegra186-gpio",
1037 	.instance = 0,
1038 	.num_irqs_per_bank = 1,
1039 	.has_vm_support = false,
1040 };
1041 
1042 #define TEGRA186_AON_GPIO_PORT(_name, _bank, _port, _pins)	\
1043 	[TEGRA186_AON_GPIO_PORT_##_name] = {			\
1044 		.name = #_name,					\
1045 		.bank = _bank,					\
1046 		.port = _port,					\
1047 		.pins = _pins,					\
1048 	}
1049 
1050 static const struct tegra_gpio_port tegra186_aon_ports[] = {
1051 	TEGRA186_AON_GPIO_PORT( S, 0, 1, 5),
1052 	TEGRA186_AON_GPIO_PORT( U, 0, 2, 6),
1053 	TEGRA186_AON_GPIO_PORT( V, 0, 4, 8),
1054 	TEGRA186_AON_GPIO_PORT( W, 0, 5, 8),
1055 	TEGRA186_AON_GPIO_PORT( Z, 0, 7, 4),
1056 	TEGRA186_AON_GPIO_PORT(AA, 0, 6, 8),
1057 	TEGRA186_AON_GPIO_PORT(EE, 0, 3, 3),
1058 	TEGRA186_AON_GPIO_PORT(FF, 0, 0, 5),
1059 };
1060 
1061 static const struct tegra_gpio_soc tegra186_aon_soc = {
1062 	.num_ports = ARRAY_SIZE(tegra186_aon_ports),
1063 	.ports = tegra186_aon_ports,
1064 	.name = "tegra186-gpio-aon",
1065 	.instance = 1,
1066 	.num_irqs_per_bank = 1,
1067 	.has_vm_support = false,
1068 };
1069 
1070 #define TEGRA194_MAIN_GPIO_PORT(_name, _bank, _port, _pins)	\
1071 	[TEGRA194_MAIN_GPIO_PORT_##_name] = {			\
1072 		.name = #_name,					\
1073 		.bank = _bank,					\
1074 		.port = _port,					\
1075 		.pins = _pins,					\
1076 	}
1077 
1078 static const struct tegra_gpio_port tegra194_main_ports[] = {
1079 	TEGRA194_MAIN_GPIO_PORT( A, 1, 2, 8),
1080 	TEGRA194_MAIN_GPIO_PORT( B, 4, 7, 2),
1081 	TEGRA194_MAIN_GPIO_PORT( C, 4, 3, 8),
1082 	TEGRA194_MAIN_GPIO_PORT( D, 4, 4, 4),
1083 	TEGRA194_MAIN_GPIO_PORT( E, 4, 5, 8),
1084 	TEGRA194_MAIN_GPIO_PORT( F, 4, 6, 6),
1085 	TEGRA194_MAIN_GPIO_PORT( G, 4, 0, 8),
1086 	TEGRA194_MAIN_GPIO_PORT( H, 4, 1, 8),
1087 	TEGRA194_MAIN_GPIO_PORT( I, 4, 2, 5),
1088 	TEGRA194_MAIN_GPIO_PORT( J, 5, 1, 6),
1089 	TEGRA194_MAIN_GPIO_PORT( K, 3, 0, 8),
1090 	TEGRA194_MAIN_GPIO_PORT( L, 3, 1, 4),
1091 	TEGRA194_MAIN_GPIO_PORT( M, 2, 3, 8),
1092 	TEGRA194_MAIN_GPIO_PORT( N, 2, 4, 3),
1093 	TEGRA194_MAIN_GPIO_PORT( O, 5, 0, 6),
1094 	TEGRA194_MAIN_GPIO_PORT( P, 2, 5, 8),
1095 	TEGRA194_MAIN_GPIO_PORT( Q, 2, 6, 8),
1096 	TEGRA194_MAIN_GPIO_PORT( R, 2, 7, 6),
1097 	TEGRA194_MAIN_GPIO_PORT( S, 3, 3, 8),
1098 	TEGRA194_MAIN_GPIO_PORT( T, 3, 4, 8),
1099 	TEGRA194_MAIN_GPIO_PORT( U, 3, 5, 1),
1100 	TEGRA194_MAIN_GPIO_PORT( V, 1, 0, 8),
1101 	TEGRA194_MAIN_GPIO_PORT( W, 1, 1, 2),
1102 	TEGRA194_MAIN_GPIO_PORT( X, 2, 0, 8),
1103 	TEGRA194_MAIN_GPIO_PORT( Y, 2, 1, 8),
1104 	TEGRA194_MAIN_GPIO_PORT( Z, 2, 2, 8),
1105 	TEGRA194_MAIN_GPIO_PORT(FF, 3, 2, 2),
1106 	TEGRA194_MAIN_GPIO_PORT(GG, 0, 0, 2)
1107 };
1108 
1109 static const struct tegra186_pin_range tegra194_main_pin_ranges[] = {
1110 	{ TEGRA194_MAIN_GPIO(GG, 0), "pex_l5_clkreq_n_pgg0" },
1111 	{ TEGRA194_MAIN_GPIO(GG, 1), "pex_l5_rst_n_pgg1" },
1112 };
1113 
1114 static const struct tegra_gpio_soc tegra194_main_soc = {
1115 	.num_ports = ARRAY_SIZE(tegra194_main_ports),
1116 	.ports = tegra194_main_ports,
1117 	.name = "tegra194-gpio",
1118 	.instance = 0,
1119 	.num_irqs_per_bank = 8,
1120 	.num_pin_ranges = ARRAY_SIZE(tegra194_main_pin_ranges),
1121 	.pin_ranges = tegra194_main_pin_ranges,
1122 	.pinmux = "nvidia,tegra194-pinmux",
1123 	.has_vm_support = true,
1124 };
1125 
1126 #define TEGRA194_AON_GPIO_PORT(_name, _bank, _port, _pins)	\
1127 	[TEGRA194_AON_GPIO_PORT_##_name] = {			\
1128 		.name = #_name,					\
1129 		.bank = _bank,					\
1130 		.port = _port,					\
1131 		.pins = _pins,					\
1132 	}
1133 
1134 static const struct tegra_gpio_port tegra194_aon_ports[] = {
1135 	TEGRA194_AON_GPIO_PORT(AA, 0, 3, 8),
1136 	TEGRA194_AON_GPIO_PORT(BB, 0, 4, 4),
1137 	TEGRA194_AON_GPIO_PORT(CC, 0, 1, 8),
1138 	TEGRA194_AON_GPIO_PORT(DD, 0, 2, 3),
1139 	TEGRA194_AON_GPIO_PORT(EE, 0, 0, 7)
1140 };
1141 
1142 static const struct tegra_gpio_soc tegra194_aon_soc = {
1143 	.num_ports = ARRAY_SIZE(tegra194_aon_ports),
1144 	.ports = tegra194_aon_ports,
1145 	.name = "tegra194-gpio-aon",
1146 	.instance = 1,
1147 	.num_irqs_per_bank = 8,
1148 	.has_gte = true,
1149 	.has_vm_support = false,
1150 };
1151 
1152 #define TEGRA234_MAIN_GPIO_PORT(_name, _bank, _port, _pins)	\
1153 	[TEGRA234_MAIN_GPIO_PORT_##_name] = {			\
1154 		.name = #_name,					\
1155 		.bank = _bank,					\
1156 		.port = _port,					\
1157 		.pins = _pins,					\
1158 	}
1159 
1160 static const struct tegra_gpio_port tegra234_main_ports[] = {
1161 	TEGRA234_MAIN_GPIO_PORT( A, 0, 0, 8),
1162 	TEGRA234_MAIN_GPIO_PORT( B, 0, 3, 1),
1163 	TEGRA234_MAIN_GPIO_PORT( C, 5, 1, 8),
1164 	TEGRA234_MAIN_GPIO_PORT( D, 5, 2, 4),
1165 	TEGRA234_MAIN_GPIO_PORT( E, 5, 3, 8),
1166 	TEGRA234_MAIN_GPIO_PORT( F, 5, 4, 6),
1167 	TEGRA234_MAIN_GPIO_PORT( G, 4, 0, 8),
1168 	TEGRA234_MAIN_GPIO_PORT( H, 4, 1, 8),
1169 	TEGRA234_MAIN_GPIO_PORT( I, 4, 2, 7),
1170 	TEGRA234_MAIN_GPIO_PORT( J, 5, 0, 6),
1171 	TEGRA234_MAIN_GPIO_PORT( K, 3, 0, 8),
1172 	TEGRA234_MAIN_GPIO_PORT( L, 3, 1, 4),
1173 	TEGRA234_MAIN_GPIO_PORT( M, 2, 0, 8),
1174 	TEGRA234_MAIN_GPIO_PORT( N, 2, 1, 8),
1175 	TEGRA234_MAIN_GPIO_PORT( P, 2, 2, 8),
1176 	TEGRA234_MAIN_GPIO_PORT( Q, 2, 3, 8),
1177 	TEGRA234_MAIN_GPIO_PORT( R, 2, 4, 6),
1178 	TEGRA234_MAIN_GPIO_PORT( X, 1, 0, 8),
1179 	TEGRA234_MAIN_GPIO_PORT( Y, 1, 1, 8),
1180 	TEGRA234_MAIN_GPIO_PORT( Z, 1, 2, 8),
1181 	TEGRA234_MAIN_GPIO_PORT(AC, 0, 1, 8),
1182 	TEGRA234_MAIN_GPIO_PORT(AD, 0, 2, 4),
1183 	TEGRA234_MAIN_GPIO_PORT(AE, 3, 3, 2),
1184 	TEGRA234_MAIN_GPIO_PORT(AF, 3, 4, 4),
1185 	TEGRA234_MAIN_GPIO_PORT(AG, 3, 2, 8),
1186 };
1187 
1188 static const struct tegra_gpio_soc tegra234_main_soc = {
1189 	.num_ports = ARRAY_SIZE(tegra234_main_ports),
1190 	.ports = tegra234_main_ports,
1191 	.name = "tegra234-gpio",
1192 	.instance = 0,
1193 	.num_irqs_per_bank = 8,
1194 	.has_vm_support = true,
1195 };
1196 
1197 #define TEGRA234_AON_GPIO_PORT(_name, _bank, _port, _pins)	\
1198 	[TEGRA234_AON_GPIO_PORT_##_name] = {			\
1199 		.name = #_name,					\
1200 		.bank = _bank,					\
1201 		.port = _port,					\
1202 		.pins = _pins,					\
1203 	}
1204 
1205 static const struct tegra_gpio_port tegra234_aon_ports[] = {
1206 	TEGRA234_AON_GPIO_PORT(AA, 0, 4, 8),
1207 	TEGRA234_AON_GPIO_PORT(BB, 0, 5, 4),
1208 	TEGRA234_AON_GPIO_PORT(CC, 0, 2, 8),
1209 	TEGRA234_AON_GPIO_PORT(DD, 0, 3, 3),
1210 	TEGRA234_AON_GPIO_PORT(EE, 0, 0, 8),
1211 	TEGRA234_AON_GPIO_PORT(GG, 0, 1, 1),
1212 };
1213 
1214 static const struct tegra_gpio_soc tegra234_aon_soc = {
1215 	.num_ports = ARRAY_SIZE(tegra234_aon_ports),
1216 	.ports = tegra234_aon_ports,
1217 	.name = "tegra234-gpio-aon",
1218 	.instance = 1,
1219 	.num_irqs_per_bank = 8,
1220 	.has_gte = true,
1221 	.has_vm_support = false,
1222 };
1223 
1224 #define TEGRA241_MAIN_GPIO_PORT(_name, _bank, _port, _pins)	\
1225 	[TEGRA241_MAIN_GPIO_PORT_##_name] = {			\
1226 		.name = #_name,					\
1227 		.bank = _bank,					\
1228 		.port = _port,					\
1229 		.pins = _pins,					\
1230 	}
1231 
1232 static const struct tegra_gpio_port tegra241_main_ports[] = {
1233 	TEGRA241_MAIN_GPIO_PORT(A, 0, 0, 8),
1234 	TEGRA241_MAIN_GPIO_PORT(B, 0, 1, 8),
1235 	TEGRA241_MAIN_GPIO_PORT(C, 0, 2, 2),
1236 	TEGRA241_MAIN_GPIO_PORT(D, 0, 3, 6),
1237 	TEGRA241_MAIN_GPIO_PORT(E, 0, 4, 8),
1238 	TEGRA241_MAIN_GPIO_PORT(F, 1, 0, 8),
1239 	TEGRA241_MAIN_GPIO_PORT(G, 1, 1, 8),
1240 	TEGRA241_MAIN_GPIO_PORT(H, 1, 2, 8),
1241 	TEGRA241_MAIN_GPIO_PORT(J, 1, 3, 8),
1242 	TEGRA241_MAIN_GPIO_PORT(K, 1, 4, 4),
1243 	TEGRA241_MAIN_GPIO_PORT(L, 1, 5, 6),
1244 };
1245 
1246 static const struct tegra_gpio_soc tegra241_main_soc = {
1247 	.num_ports = ARRAY_SIZE(tegra241_main_ports),
1248 	.ports = tegra241_main_ports,
1249 	.name = "tegra241-gpio",
1250 	.instance = 0,
1251 	.num_irqs_per_bank = 8,
1252 	.has_vm_support = false,
1253 };
1254 
1255 #define TEGRA241_AON_GPIO_PORT(_name, _bank, _port, _pins)	\
1256 	[TEGRA241_AON_GPIO_PORT_##_name] = {			\
1257 		.name = #_name,					\
1258 		.bank = _bank,					\
1259 		.port = _port,					\
1260 		.pins = _pins,					\
1261 	}
1262 
1263 static const struct tegra_gpio_port tegra241_aon_ports[] = {
1264 	TEGRA241_AON_GPIO_PORT(AA, 0, 0, 8),
1265 	TEGRA241_AON_GPIO_PORT(BB, 0, 0, 4),
1266 };
1267 
1268 static const struct tegra_gpio_soc tegra241_aon_soc = {
1269 	.num_ports = ARRAY_SIZE(tegra241_aon_ports),
1270 	.ports = tegra241_aon_ports,
1271 	.name = "tegra241-gpio-aon",
1272 	.instance = 1,
1273 	.num_irqs_per_bank = 8,
1274 	.has_vm_support = false,
1275 };
1276 
1277 static const struct of_device_id tegra186_gpio_of_match[] = {
1278 	{
1279 		.compatible = "nvidia,tegra186-gpio",
1280 		.data = &tegra186_main_soc
1281 	}, {
1282 		.compatible = "nvidia,tegra186-gpio-aon",
1283 		.data = &tegra186_aon_soc
1284 	}, {
1285 		.compatible = "nvidia,tegra194-gpio",
1286 		.data = &tegra194_main_soc
1287 	}, {
1288 		.compatible = "nvidia,tegra194-gpio-aon",
1289 		.data = &tegra194_aon_soc
1290 	}, {
1291 		.compatible = "nvidia,tegra234-gpio",
1292 		.data = &tegra234_main_soc
1293 	}, {
1294 		.compatible = "nvidia,tegra234-gpio-aon",
1295 		.data = &tegra234_aon_soc
1296 	}, {
1297 		/* sentinel */
1298 	}
1299 };
1300 MODULE_DEVICE_TABLE(of, tegra186_gpio_of_match);
1301 
1302 static const struct acpi_device_id  tegra186_gpio_acpi_match[] = {
1303 	{ .id = "NVDA0108", .driver_data = (kernel_ulong_t)&tegra186_main_soc },
1304 	{ .id = "NVDA0208", .driver_data = (kernel_ulong_t)&tegra186_aon_soc },
1305 	{ .id = "NVDA0308", .driver_data = (kernel_ulong_t)&tegra194_main_soc },
1306 	{ .id = "NVDA0408", .driver_data = (kernel_ulong_t)&tegra194_aon_soc },
1307 	{ .id = "NVDA0508", .driver_data = (kernel_ulong_t)&tegra241_main_soc },
1308 	{ .id = "NVDA0608", .driver_data = (kernel_ulong_t)&tegra241_aon_soc },
1309 	{}
1310 };
1311 MODULE_DEVICE_TABLE(acpi, tegra186_gpio_acpi_match);
1312 
1313 static struct platform_driver tegra186_gpio_driver = {
1314 	.driver = {
1315 		.name = "tegra186-gpio",
1316 		.of_match_table = tegra186_gpio_of_match,
1317 		.acpi_match_table = tegra186_gpio_acpi_match,
1318 	},
1319 	.probe = tegra186_gpio_probe,
1320 };
1321 module_platform_driver(tegra186_gpio_driver);
1322 
1323 MODULE_DESCRIPTION("NVIDIA Tegra186 GPIO controller driver");
1324 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1325 MODULE_LICENSE("GPL v2");
1326