1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016-2022 NVIDIA Corporation
5 * Author: Thierry Reding <treding@nvidia.com>
6 * Dipen Patel <dpatel@nvidia.com>
9 #include <linux/gpio/driver.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/hte.h>
17 #include <dt-bindings/gpio/tegra186-gpio.h>
18 #include <dt-bindings/gpio/tegra194-gpio.h>
19 #include <dt-bindings/gpio/tegra234-gpio.h>
20 #include <dt-bindings/gpio/tegra241-gpio.h>
22 /* security registers */
23 #define TEGRA186_GPIO_CTL_SCR 0x0c
24 #define TEGRA186_GPIO_CTL_SCR_SEC_WEN BIT(28)
25 #define TEGRA186_GPIO_CTL_SCR_SEC_REN BIT(27)
27 #define TEGRA186_GPIO_INT_ROUTE_MAPPING(p, x) (0x14 + (p) * 0x20 + (x) * 4)
29 /* control registers */
30 #define TEGRA186_GPIO_ENABLE_CONFIG 0x00
31 #define TEGRA186_GPIO_ENABLE_CONFIG_ENABLE BIT(0)
32 #define TEGRA186_GPIO_ENABLE_CONFIG_OUT BIT(1)
33 #define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_NONE (0x0 << 2)
34 #define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_LEVEL (0x1 << 2)
35 #define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE (0x2 << 2)
36 #define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_DOUBLE_EDGE (0x3 << 2)
37 #define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_MASK (0x3 << 2)
38 #define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL BIT(4)
39 #define TEGRA186_GPIO_ENABLE_CONFIG_DEBOUNCE BIT(5)
40 #define TEGRA186_GPIO_ENABLE_CONFIG_INTERRUPT BIT(6)
41 #define TEGRA186_GPIO_ENABLE_CONFIG_TIMESTAMP_FUNC BIT(7)
43 #define TEGRA186_GPIO_DEBOUNCE_CONTROL 0x04
44 #define TEGRA186_GPIO_DEBOUNCE_CONTROL_THRESHOLD(x) ((x) & 0xff)
46 #define TEGRA186_GPIO_INPUT 0x08
47 #define TEGRA186_GPIO_INPUT_HIGH BIT(0)
49 #define TEGRA186_GPIO_OUTPUT_CONTROL 0x0c
50 #define TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED BIT(0)
52 #define TEGRA186_GPIO_OUTPUT_VALUE 0x10
53 #define TEGRA186_GPIO_OUTPUT_VALUE_HIGH BIT(0)
55 #define TEGRA186_GPIO_INTERRUPT_CLEAR 0x14
57 #define TEGRA186_GPIO_INTERRUPT_STATUS(x) (0x100 + (x) * 4)
59 struct tegra_gpio_port {
66 struct tegra186_pin_range {
71 struct tegra_gpio_soc {
72 const struct tegra_gpio_port *ports;
73 unsigned int num_ports;
75 unsigned int instance;
77 unsigned int num_irqs_per_bank;
79 const struct tegra186_pin_range *pin_ranges;
80 unsigned int num_pin_ranges;
86 struct gpio_chip gpio;
90 const struct tegra_gpio_soc *soc;
91 unsigned int num_irqs_per_bank;
92 unsigned int num_banks;
98 static const struct tegra_gpio_port *
99 tegra186_gpio_get_port(struct tegra_gpio *gpio, unsigned int *pin)
101 unsigned int start = 0, i;
103 for (i = 0; i < gpio->soc->num_ports; i++) {
104 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
106 if (*pin >= start && *pin < start + port->pins) {
117 static void __iomem *tegra186_gpio_get_base(struct tegra_gpio *gpio,
120 const struct tegra_gpio_port *port;
123 port = tegra186_gpio_get_port(gpio, &pin);
127 offset = port->bank * 0x1000 + port->port * 0x200;
129 return gpio->base + offset + pin * 0x20;
132 static int tegra186_gpio_get_direction(struct gpio_chip *chip,
135 struct tegra_gpio *gpio = gpiochip_get_data(chip);
139 base = tegra186_gpio_get_base(gpio, offset);
140 if (WARN_ON(base == NULL))
143 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
144 if (value & TEGRA186_GPIO_ENABLE_CONFIG_OUT)
145 return GPIO_LINE_DIRECTION_OUT;
147 return GPIO_LINE_DIRECTION_IN;
150 static int tegra186_gpio_direction_input(struct gpio_chip *chip,
153 struct tegra_gpio *gpio = gpiochip_get_data(chip);
157 base = tegra186_gpio_get_base(gpio, offset);
158 if (WARN_ON(base == NULL))
161 value = readl(base + TEGRA186_GPIO_OUTPUT_CONTROL);
162 value |= TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED;
163 writel(value, base + TEGRA186_GPIO_OUTPUT_CONTROL);
165 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
166 value |= TEGRA186_GPIO_ENABLE_CONFIG_ENABLE;
167 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_OUT;
168 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
173 static int tegra186_gpio_direction_output(struct gpio_chip *chip,
174 unsigned int offset, int level)
176 struct tegra_gpio *gpio = gpiochip_get_data(chip);
180 /* configure output level first */
181 chip->set(chip, offset, level);
183 base = tegra186_gpio_get_base(gpio, offset);
184 if (WARN_ON(base == NULL))
187 /* set the direction */
188 value = readl(base + TEGRA186_GPIO_OUTPUT_CONTROL);
189 value &= ~TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED;
190 writel(value, base + TEGRA186_GPIO_OUTPUT_CONTROL);
192 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
193 value |= TEGRA186_GPIO_ENABLE_CONFIG_ENABLE;
194 value |= TEGRA186_GPIO_ENABLE_CONFIG_OUT;
195 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
200 #define HTE_BOTH_EDGES (HTE_RISING_EDGE_TS | HTE_FALLING_EDGE_TS)
202 static int tegra186_gpio_en_hw_ts(struct gpio_chip *gc, u32 offset,
205 struct tegra_gpio *gpio;
212 gpio = gpiochip_get_data(gc);
216 base = tegra186_gpio_get_base(gpio, offset);
217 if (WARN_ON(base == NULL))
220 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
221 value |= TEGRA186_GPIO_ENABLE_CONFIG_TIMESTAMP_FUNC;
223 if (flags == HTE_BOTH_EDGES) {
224 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_DOUBLE_EDGE;
225 } else if (flags == HTE_RISING_EDGE_TS) {
226 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
227 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
228 } else if (flags == HTE_FALLING_EDGE_TS) {
229 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
232 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
237 static int tegra186_gpio_dis_hw_ts(struct gpio_chip *gc, u32 offset,
240 struct tegra_gpio *gpio;
247 gpio = gpiochip_get_data(gc);
251 base = tegra186_gpio_get_base(gpio, offset);
252 if (WARN_ON(base == NULL))
255 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
256 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TIMESTAMP_FUNC;
257 if (flags == HTE_BOTH_EDGES) {
258 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_DOUBLE_EDGE;
259 } else if (flags == HTE_RISING_EDGE_TS) {
260 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
261 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
262 } else if (flags == HTE_FALLING_EDGE_TS) {
263 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
265 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
270 static int tegra186_gpio_get(struct gpio_chip *chip, unsigned int offset)
272 struct tegra_gpio *gpio = gpiochip_get_data(chip);
276 base = tegra186_gpio_get_base(gpio, offset);
277 if (WARN_ON(base == NULL))
280 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
281 if (value & TEGRA186_GPIO_ENABLE_CONFIG_OUT)
282 value = readl(base + TEGRA186_GPIO_OUTPUT_VALUE);
284 value = readl(base + TEGRA186_GPIO_INPUT);
286 return value & BIT(0);
289 static void tegra186_gpio_set(struct gpio_chip *chip, unsigned int offset,
292 struct tegra_gpio *gpio = gpiochip_get_data(chip);
296 base = tegra186_gpio_get_base(gpio, offset);
297 if (WARN_ON(base == NULL))
300 value = readl(base + TEGRA186_GPIO_OUTPUT_VALUE);
302 value &= ~TEGRA186_GPIO_OUTPUT_VALUE_HIGH;
304 value |= TEGRA186_GPIO_OUTPUT_VALUE_HIGH;
306 writel(value, base + TEGRA186_GPIO_OUTPUT_VALUE);
309 static int tegra186_gpio_set_config(struct gpio_chip *chip,
311 unsigned long config)
313 struct tegra_gpio *gpio = gpiochip_get_data(chip);
317 base = tegra186_gpio_get_base(gpio, offset);
321 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
324 debounce = pinconf_to_config_argument(config);
327 * The Tegra186 GPIO controller supports a maximum of 255 ms debounce
330 if (debounce > 255000)
333 debounce = DIV_ROUND_UP(debounce, USEC_PER_MSEC);
335 value = TEGRA186_GPIO_DEBOUNCE_CONTROL_THRESHOLD(debounce);
336 writel(value, base + TEGRA186_GPIO_DEBOUNCE_CONTROL);
338 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
339 value |= TEGRA186_GPIO_ENABLE_CONFIG_DEBOUNCE;
340 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
345 static int tegra186_gpio_add_pin_ranges(struct gpio_chip *chip)
347 struct tegra_gpio *gpio = gpiochip_get_data(chip);
348 struct pinctrl_dev *pctldev;
349 struct device_node *np;
353 if (!gpio->soc->pinmux || gpio->soc->num_pin_ranges == 0)
356 np = of_find_compatible_node(NULL, NULL, gpio->soc->pinmux);
360 pctldev = of_pinctrl_get(np);
363 return -EPROBE_DEFER;
365 for (i = 0; i < gpio->soc->num_pin_ranges; i++) {
366 unsigned int pin = gpio->soc->pin_ranges[i].offset, port;
367 const char *group = gpio->soc->pin_ranges[i].group;
372 if (port >= gpio->soc->num_ports) {
373 dev_warn(chip->parent, "invalid port %u for %s\n",
378 for (j = 0; j < port; j++)
379 pin += gpio->soc->ports[j].pins;
381 err = gpiochip_add_pingroup_range(chip, pctldev, pin, group);
389 static int tegra186_gpio_of_xlate(struct gpio_chip *chip,
390 const struct of_phandle_args *spec,
393 struct tegra_gpio *gpio = gpiochip_get_data(chip);
394 unsigned int port, pin, i, offset = 0;
396 if (WARN_ON(chip->of_gpio_n_cells < 2))
399 if (WARN_ON(spec->args_count < chip->of_gpio_n_cells))
402 port = spec->args[0] / 8;
403 pin = spec->args[0] % 8;
405 if (port >= gpio->soc->num_ports) {
406 dev_err(chip->parent, "invalid port number: %u\n", port);
410 for (i = 0; i < port; i++)
411 offset += gpio->soc->ports[i].pins;
414 *flags = spec->args[1];
419 #define to_tegra_gpio(x) container_of((x), struct tegra_gpio, gpio)
421 static void tegra186_irq_ack(struct irq_data *data)
423 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
424 struct tegra_gpio *gpio = to_tegra_gpio(gc);
427 base = tegra186_gpio_get_base(gpio, data->hwirq);
428 if (WARN_ON(base == NULL))
431 writel(1, base + TEGRA186_GPIO_INTERRUPT_CLEAR);
434 static void tegra186_irq_mask(struct irq_data *data)
436 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
437 struct tegra_gpio *gpio = to_tegra_gpio(gc);
441 base = tegra186_gpio_get_base(gpio, data->hwirq);
442 if (WARN_ON(base == NULL))
445 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
446 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_INTERRUPT;
447 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
449 gpiochip_disable_irq(&gpio->gpio, data->hwirq);
452 static void tegra186_irq_unmask(struct irq_data *data)
454 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
455 struct tegra_gpio *gpio = to_tegra_gpio(gc);
459 base = tegra186_gpio_get_base(gpio, data->hwirq);
460 if (WARN_ON(base == NULL))
463 gpiochip_enable_irq(&gpio->gpio, data->hwirq);
465 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
466 value |= TEGRA186_GPIO_ENABLE_CONFIG_INTERRUPT;
467 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
470 static int tegra186_irq_set_type(struct irq_data *data, unsigned int type)
472 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
473 struct tegra_gpio *gpio = to_tegra_gpio(gc);
477 base = tegra186_gpio_get_base(gpio, data->hwirq);
478 if (WARN_ON(base == NULL))
481 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
482 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_MASK;
483 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
485 switch (type & IRQ_TYPE_SENSE_MASK) {
489 case IRQ_TYPE_EDGE_RISING:
490 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
491 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
494 case IRQ_TYPE_EDGE_FALLING:
495 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
498 case IRQ_TYPE_EDGE_BOTH:
499 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_DOUBLE_EDGE;
502 case IRQ_TYPE_LEVEL_HIGH:
503 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_LEVEL;
504 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
507 case IRQ_TYPE_LEVEL_LOW:
508 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_LEVEL;
515 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
517 if ((type & IRQ_TYPE_EDGE_BOTH) == 0)
518 irq_set_handler_locked(data, handle_level_irq);
520 irq_set_handler_locked(data, handle_edge_irq);
522 if (data->parent_data)
523 return irq_chip_set_type_parent(data, type);
528 static int tegra186_irq_set_wake(struct irq_data *data, unsigned int on)
530 if (data->parent_data)
531 return irq_chip_set_wake_parent(data, on);
536 static void tegra186_irq_print_chip(struct irq_data *data, struct seq_file *p)
538 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
540 seq_printf(p, dev_name(gc->parent));
543 static const struct irq_chip tegra186_gpio_irq_chip = {
544 .irq_ack = tegra186_irq_ack,
545 .irq_mask = tegra186_irq_mask,
546 .irq_unmask = tegra186_irq_unmask,
547 .irq_set_type = tegra186_irq_set_type,
548 .irq_set_wake = tegra186_irq_set_wake,
549 .irq_print_chip = tegra186_irq_print_chip,
550 .flags = IRQCHIP_IMMUTABLE,
551 GPIOCHIP_IRQ_RESOURCE_HELPERS,
554 static void tegra186_gpio_irq(struct irq_desc *desc)
556 struct tegra_gpio *gpio = irq_desc_get_handler_data(desc);
557 struct irq_domain *domain = gpio->gpio.irq.domain;
558 struct irq_chip *chip = irq_desc_get_chip(desc);
559 unsigned int parent = irq_desc_get_irq(desc);
560 unsigned int i, j, offset = 0;
562 chained_irq_enter(chip, desc);
564 for (i = 0; i < gpio->soc->num_ports; i++) {
565 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
570 base = gpio->base + port->bank * 0x1000 + port->port * 0x200;
572 /* skip ports that are not associated with this bank */
573 for (j = 0; j < gpio->num_irqs_per_bank; j++) {
574 if (parent == gpio->irq[port->bank * gpio->num_irqs_per_bank + j])
578 if (j == gpio->num_irqs_per_bank)
581 value = readl(base + TEGRA186_GPIO_INTERRUPT_STATUS(1));
583 for_each_set_bit(pin, &value, port->pins) {
584 int ret = generic_handle_domain_irq(domain, offset + pin);
585 WARN_RATELIMIT(ret, "hwirq = %d", offset + pin);
589 offset += port->pins;
592 chained_irq_exit(chip, desc);
595 static int tegra186_gpio_irq_domain_translate(struct irq_domain *domain,
596 struct irq_fwspec *fwspec,
597 unsigned long *hwirq,
600 struct tegra_gpio *gpio = gpiochip_get_data(domain->host_data);
601 unsigned int port, pin, i, offset = 0;
603 if (WARN_ON(gpio->gpio.of_gpio_n_cells < 2))
606 if (WARN_ON(fwspec->param_count < gpio->gpio.of_gpio_n_cells))
609 port = fwspec->param[0] / 8;
610 pin = fwspec->param[0] % 8;
612 if (port >= gpio->soc->num_ports)
615 for (i = 0; i < port; i++)
616 offset += gpio->soc->ports[i].pins;
618 *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
619 *hwirq = offset + pin;
624 static int tegra186_gpio_populate_parent_fwspec(struct gpio_chip *chip,
625 union gpio_irq_fwspec *gfwspec,
626 unsigned int parent_hwirq,
627 unsigned int parent_type)
629 struct tegra_gpio *gpio = gpiochip_get_data(chip);
630 struct irq_fwspec *fwspec = &gfwspec->fwspec;
632 fwspec->fwnode = chip->irq.parent_domain->fwnode;
633 fwspec->param_count = 3;
634 fwspec->param[0] = gpio->soc->instance;
635 fwspec->param[1] = parent_hwirq;
636 fwspec->param[2] = parent_type;
641 static int tegra186_gpio_child_to_parent_hwirq(struct gpio_chip *chip,
644 unsigned int *parent_hwirq,
645 unsigned int *parent_type)
647 *parent_hwirq = chip->irq.child_offset_to_irq(chip, hwirq);
653 static unsigned int tegra186_gpio_child_offset_to_irq(struct gpio_chip *chip,
656 struct tegra_gpio *gpio = gpiochip_get_data(chip);
659 for (i = 0; i < gpio->soc->num_ports; i++) {
660 if (offset < gpio->soc->ports[i].pins)
663 offset -= gpio->soc->ports[i].pins;
666 return offset + i * 8;
669 static const struct of_device_id tegra186_pmc_of_match[] = {
670 { .compatible = "nvidia,tegra186-pmc" },
671 { .compatible = "nvidia,tegra194-pmc" },
675 static void tegra186_gpio_init_route_mapping(struct tegra_gpio *gpio)
677 struct device *dev = gpio->gpio.parent;
681 for (i = 0; i < gpio->soc->num_ports; i++) {
682 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
683 unsigned int offset, p = port->port;
686 base = gpio->secure + port->bank * 0x1000 + 0x800;
688 value = readl(base + TEGRA186_GPIO_CTL_SCR);
691 * For controllers that haven't been locked down yet, make
692 * sure to program the default interrupt route mapping.
694 if ((value & TEGRA186_GPIO_CTL_SCR_SEC_REN) == 0 &&
695 (value & TEGRA186_GPIO_CTL_SCR_SEC_WEN) == 0) {
697 * On Tegra194 and later, each pin can be routed to one or more
700 for (j = 0; j < gpio->num_irqs_per_bank; j++) {
701 dev_dbg(dev, "programming default interrupt routing for port %s\n",
704 offset = TEGRA186_GPIO_INT_ROUTE_MAPPING(p, j);
707 * By default we only want to route GPIO pins to IRQ 0. This works
708 * only under the assumption that we're running as the host kernel
709 * and hence all GPIO pins are owned by Linux.
711 * For cases where Linux is the guest OS, the hypervisor will have
712 * to configure the interrupt routing and pass only the valid
713 * interrupts via device tree.
716 value = readl(base + offset);
717 value = BIT(port->pins) - 1;
718 writel(value, base + offset);
725 static unsigned int tegra186_gpio_irqs_per_bank(struct tegra_gpio *gpio)
727 struct device *dev = gpio->gpio.parent;
729 if (gpio->num_irq > gpio->num_banks) {
730 if (gpio->num_irq % gpio->num_banks != 0)
734 if (gpio->num_irq < gpio->num_banks)
737 gpio->num_irqs_per_bank = gpio->num_irq / gpio->num_banks;
739 if (gpio->num_irqs_per_bank > gpio->soc->num_irqs_per_bank)
745 dev_err(dev, "invalid number of interrupts (%u) for %u banks\n",
746 gpio->num_irq, gpio->num_banks);
750 static int tegra186_gpio_probe(struct platform_device *pdev)
752 unsigned int i, j, offset;
753 struct gpio_irq_chip *irq;
754 struct tegra_gpio *gpio;
755 struct device_node *np;
759 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
763 gpio->soc = device_get_match_data(&pdev->dev);
764 gpio->gpio.label = gpio->soc->name;
765 gpio->gpio.parent = &pdev->dev;
767 /* count the number of banks in the controller */
768 for (i = 0; i < gpio->soc->num_ports; i++)
769 if (gpio->soc->ports[i].bank > gpio->num_banks)
770 gpio->num_banks = gpio->soc->ports[i].bank;
774 /* get register apertures */
775 gpio->secure = devm_platform_ioremap_resource_byname(pdev, "security");
776 if (IS_ERR(gpio->secure)) {
777 gpio->secure = devm_platform_ioremap_resource(pdev, 0);
778 if (IS_ERR(gpio->secure))
779 return PTR_ERR(gpio->secure);
782 gpio->base = devm_platform_ioremap_resource_byname(pdev, "gpio");
783 if (IS_ERR(gpio->base)) {
784 gpio->base = devm_platform_ioremap_resource(pdev, 1);
785 if (IS_ERR(gpio->base))
786 return PTR_ERR(gpio->base);
789 err = platform_irq_count(pdev);
795 err = tegra186_gpio_irqs_per_bank(gpio);
799 gpio->irq = devm_kcalloc(&pdev->dev, gpio->num_irq, sizeof(*gpio->irq),
804 for (i = 0; i < gpio->num_irq; i++) {
805 err = platform_get_irq(pdev, i);
812 gpio->gpio.request = gpiochip_generic_request;
813 gpio->gpio.free = gpiochip_generic_free;
814 gpio->gpio.get_direction = tegra186_gpio_get_direction;
815 gpio->gpio.direction_input = tegra186_gpio_direction_input;
816 gpio->gpio.direction_output = tegra186_gpio_direction_output;
817 gpio->gpio.get = tegra186_gpio_get;
818 gpio->gpio.set = tegra186_gpio_set;
819 gpio->gpio.set_config = tegra186_gpio_set_config;
820 gpio->gpio.add_pin_ranges = tegra186_gpio_add_pin_ranges;
821 if (gpio->soc->has_gte) {
822 gpio->gpio.en_hw_timestamp = tegra186_gpio_en_hw_ts;
823 gpio->gpio.dis_hw_timestamp = tegra186_gpio_dis_hw_ts;
826 gpio->gpio.base = -1;
828 for (i = 0; i < gpio->soc->num_ports; i++)
829 gpio->gpio.ngpio += gpio->soc->ports[i].pins;
831 names = devm_kcalloc(gpio->gpio.parent, gpio->gpio.ngpio,
832 sizeof(*names), GFP_KERNEL);
836 for (i = 0, offset = 0; i < gpio->soc->num_ports; i++) {
837 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
840 for (j = 0; j < port->pins; j++) {
841 name = devm_kasprintf(gpio->gpio.parent, GFP_KERNEL,
842 "P%s.%02x", port->name, j);
846 names[offset + j] = name;
849 offset += port->pins;
852 gpio->gpio.names = (const char * const *)names;
854 #if defined(CONFIG_OF_GPIO)
855 gpio->gpio.of_gpio_n_cells = 2;
856 gpio->gpio.of_xlate = tegra186_gpio_of_xlate;
857 #endif /* CONFIG_OF_GPIO */
859 irq = &gpio->gpio.irq;
860 gpio_irq_chip_set_chip(irq, &tegra186_gpio_irq_chip);
861 irq->fwnode = of_node_to_fwnode(pdev->dev.of_node);
862 irq->child_to_parent_hwirq = tegra186_gpio_child_to_parent_hwirq;
863 irq->populate_parent_alloc_arg = tegra186_gpio_populate_parent_fwspec;
864 irq->child_offset_to_irq = tegra186_gpio_child_offset_to_irq;
865 irq->child_irq_domain_ops.translate = tegra186_gpio_irq_domain_translate;
866 irq->handler = handle_simple_irq;
867 irq->default_type = IRQ_TYPE_NONE;
868 irq->parent_handler = tegra186_gpio_irq;
869 irq->parent_handler_data = gpio;
870 irq->num_parents = gpio->num_irq;
873 * To simplify things, use a single interrupt per bank for now. Some
874 * chips support up to 8 interrupts per bank, which can be useful to
875 * distribute the load and decrease the processing latency for GPIOs
876 * but it also requires a more complicated interrupt routing than we
879 if (gpio->num_irqs_per_bank > 1) {
880 irq->parents = devm_kcalloc(&pdev->dev, gpio->num_banks,
881 sizeof(*irq->parents), GFP_KERNEL);
885 for (i = 0; i < gpio->num_banks; i++)
886 irq->parents[i] = gpio->irq[i * gpio->num_irqs_per_bank];
888 irq->num_parents = gpio->num_banks;
890 irq->num_parents = gpio->num_irq;
891 irq->parents = gpio->irq;
894 if (gpio->soc->num_irqs_per_bank > 1)
895 tegra186_gpio_init_route_mapping(gpio);
897 np = of_find_matching_node(NULL, tegra186_pmc_of_match);
899 irq->parent_domain = irq_find_host(np);
902 if (!irq->parent_domain)
903 return -EPROBE_DEFER;
906 irq->map = devm_kcalloc(&pdev->dev, gpio->gpio.ngpio,
907 sizeof(*irq->map), GFP_KERNEL);
911 for (i = 0, offset = 0; i < gpio->soc->num_ports; i++) {
912 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
914 for (j = 0; j < port->pins; j++)
915 irq->map[offset + j] = irq->parents[port->bank];
917 offset += port->pins;
920 return devm_gpiochip_add_data(&pdev->dev, &gpio->gpio, gpio);
923 #define TEGRA186_MAIN_GPIO_PORT(_name, _bank, _port, _pins) \
924 [TEGRA186_MAIN_GPIO_PORT_##_name] = { \
931 static const struct tegra_gpio_port tegra186_main_ports[] = {
932 TEGRA186_MAIN_GPIO_PORT( A, 2, 0, 7),
933 TEGRA186_MAIN_GPIO_PORT( B, 3, 0, 7),
934 TEGRA186_MAIN_GPIO_PORT( C, 3, 1, 7),
935 TEGRA186_MAIN_GPIO_PORT( D, 3, 2, 6),
936 TEGRA186_MAIN_GPIO_PORT( E, 2, 1, 8),
937 TEGRA186_MAIN_GPIO_PORT( F, 2, 2, 6),
938 TEGRA186_MAIN_GPIO_PORT( G, 4, 1, 6),
939 TEGRA186_MAIN_GPIO_PORT( H, 1, 0, 7),
940 TEGRA186_MAIN_GPIO_PORT( I, 0, 4, 8),
941 TEGRA186_MAIN_GPIO_PORT( J, 5, 0, 8),
942 TEGRA186_MAIN_GPIO_PORT( K, 5, 1, 1),
943 TEGRA186_MAIN_GPIO_PORT( L, 1, 1, 8),
944 TEGRA186_MAIN_GPIO_PORT( M, 5, 3, 6),
945 TEGRA186_MAIN_GPIO_PORT( N, 0, 0, 7),
946 TEGRA186_MAIN_GPIO_PORT( O, 0, 1, 4),
947 TEGRA186_MAIN_GPIO_PORT( P, 4, 0, 7),
948 TEGRA186_MAIN_GPIO_PORT( Q, 0, 2, 6),
949 TEGRA186_MAIN_GPIO_PORT( R, 0, 5, 6),
950 TEGRA186_MAIN_GPIO_PORT( T, 0, 3, 4),
951 TEGRA186_MAIN_GPIO_PORT( X, 1, 2, 8),
952 TEGRA186_MAIN_GPIO_PORT( Y, 1, 3, 7),
953 TEGRA186_MAIN_GPIO_PORT(BB, 2, 3, 2),
954 TEGRA186_MAIN_GPIO_PORT(CC, 5, 2, 4),
957 static const struct tegra_gpio_soc tegra186_main_soc = {
958 .num_ports = ARRAY_SIZE(tegra186_main_ports),
959 .ports = tegra186_main_ports,
960 .name = "tegra186-gpio",
962 .num_irqs_per_bank = 1,
965 #define TEGRA186_AON_GPIO_PORT(_name, _bank, _port, _pins) \
966 [TEGRA186_AON_GPIO_PORT_##_name] = { \
973 static const struct tegra_gpio_port tegra186_aon_ports[] = {
974 TEGRA186_AON_GPIO_PORT( S, 0, 1, 5),
975 TEGRA186_AON_GPIO_PORT( U, 0, 2, 6),
976 TEGRA186_AON_GPIO_PORT( V, 0, 4, 8),
977 TEGRA186_AON_GPIO_PORT( W, 0, 5, 8),
978 TEGRA186_AON_GPIO_PORT( Z, 0, 7, 4),
979 TEGRA186_AON_GPIO_PORT(AA, 0, 6, 8),
980 TEGRA186_AON_GPIO_PORT(EE, 0, 3, 3),
981 TEGRA186_AON_GPIO_PORT(FF, 0, 0, 5),
984 static const struct tegra_gpio_soc tegra186_aon_soc = {
985 .num_ports = ARRAY_SIZE(tegra186_aon_ports),
986 .ports = tegra186_aon_ports,
987 .name = "tegra186-gpio-aon",
989 .num_irqs_per_bank = 1,
992 #define TEGRA194_MAIN_GPIO_PORT(_name, _bank, _port, _pins) \
993 [TEGRA194_MAIN_GPIO_PORT_##_name] = { \
1000 static const struct tegra_gpio_port tegra194_main_ports[] = {
1001 TEGRA194_MAIN_GPIO_PORT( A, 1, 2, 8),
1002 TEGRA194_MAIN_GPIO_PORT( B, 4, 7, 2),
1003 TEGRA194_MAIN_GPIO_PORT( C, 4, 3, 8),
1004 TEGRA194_MAIN_GPIO_PORT( D, 4, 4, 4),
1005 TEGRA194_MAIN_GPIO_PORT( E, 4, 5, 8),
1006 TEGRA194_MAIN_GPIO_PORT( F, 4, 6, 6),
1007 TEGRA194_MAIN_GPIO_PORT( G, 4, 0, 8),
1008 TEGRA194_MAIN_GPIO_PORT( H, 4, 1, 8),
1009 TEGRA194_MAIN_GPIO_PORT( I, 4, 2, 5),
1010 TEGRA194_MAIN_GPIO_PORT( J, 5, 1, 6),
1011 TEGRA194_MAIN_GPIO_PORT( K, 3, 0, 8),
1012 TEGRA194_MAIN_GPIO_PORT( L, 3, 1, 4),
1013 TEGRA194_MAIN_GPIO_PORT( M, 2, 3, 8),
1014 TEGRA194_MAIN_GPIO_PORT( N, 2, 4, 3),
1015 TEGRA194_MAIN_GPIO_PORT( O, 5, 0, 6),
1016 TEGRA194_MAIN_GPIO_PORT( P, 2, 5, 8),
1017 TEGRA194_MAIN_GPIO_PORT( Q, 2, 6, 8),
1018 TEGRA194_MAIN_GPIO_PORT( R, 2, 7, 6),
1019 TEGRA194_MAIN_GPIO_PORT( S, 3, 3, 8),
1020 TEGRA194_MAIN_GPIO_PORT( T, 3, 4, 8),
1021 TEGRA194_MAIN_GPIO_PORT( U, 3, 5, 1),
1022 TEGRA194_MAIN_GPIO_PORT( V, 1, 0, 8),
1023 TEGRA194_MAIN_GPIO_PORT( W, 1, 1, 2),
1024 TEGRA194_MAIN_GPIO_PORT( X, 2, 0, 8),
1025 TEGRA194_MAIN_GPIO_PORT( Y, 2, 1, 8),
1026 TEGRA194_MAIN_GPIO_PORT( Z, 2, 2, 8),
1027 TEGRA194_MAIN_GPIO_PORT(FF, 3, 2, 2),
1028 TEGRA194_MAIN_GPIO_PORT(GG, 0, 0, 2)
1031 static const struct tegra186_pin_range tegra194_main_pin_ranges[] = {
1032 { TEGRA194_MAIN_GPIO(GG, 0), "pex_l5_clkreq_n_pgg0" },
1033 { TEGRA194_MAIN_GPIO(GG, 1), "pex_l5_rst_n_pgg1" },
1036 static const struct tegra_gpio_soc tegra194_main_soc = {
1037 .num_ports = ARRAY_SIZE(tegra194_main_ports),
1038 .ports = tegra194_main_ports,
1039 .name = "tegra194-gpio",
1041 .num_irqs_per_bank = 8,
1042 .num_pin_ranges = ARRAY_SIZE(tegra194_main_pin_ranges),
1043 .pin_ranges = tegra194_main_pin_ranges,
1044 .pinmux = "nvidia,tegra194-pinmux",
1047 #define TEGRA194_AON_GPIO_PORT(_name, _bank, _port, _pins) \
1048 [TEGRA194_AON_GPIO_PORT_##_name] = { \
1055 static const struct tegra_gpio_port tegra194_aon_ports[] = {
1056 TEGRA194_AON_GPIO_PORT(AA, 0, 3, 8),
1057 TEGRA194_AON_GPIO_PORT(BB, 0, 4, 4),
1058 TEGRA194_AON_GPIO_PORT(CC, 0, 1, 8),
1059 TEGRA194_AON_GPIO_PORT(DD, 0, 2, 3),
1060 TEGRA194_AON_GPIO_PORT(EE, 0, 0, 7)
1063 static const struct tegra_gpio_soc tegra194_aon_soc = {
1064 .num_ports = ARRAY_SIZE(tegra194_aon_ports),
1065 .ports = tegra194_aon_ports,
1066 .name = "tegra194-gpio-aon",
1068 .num_irqs_per_bank = 8,
1072 #define TEGRA234_MAIN_GPIO_PORT(_name, _bank, _port, _pins) \
1073 [TEGRA234_MAIN_GPIO_PORT_##_name] = { \
1080 static const struct tegra_gpio_port tegra234_main_ports[] = {
1081 TEGRA234_MAIN_GPIO_PORT( A, 0, 0, 8),
1082 TEGRA234_MAIN_GPIO_PORT( B, 0, 3, 1),
1083 TEGRA234_MAIN_GPIO_PORT( C, 5, 1, 8),
1084 TEGRA234_MAIN_GPIO_PORT( D, 5, 2, 4),
1085 TEGRA234_MAIN_GPIO_PORT( E, 5, 3, 8),
1086 TEGRA234_MAIN_GPIO_PORT( F, 5, 4, 6),
1087 TEGRA234_MAIN_GPIO_PORT( G, 4, 0, 8),
1088 TEGRA234_MAIN_GPIO_PORT( H, 4, 1, 8),
1089 TEGRA234_MAIN_GPIO_PORT( I, 4, 2, 7),
1090 TEGRA234_MAIN_GPIO_PORT( J, 5, 0, 6),
1091 TEGRA234_MAIN_GPIO_PORT( K, 3, 0, 8),
1092 TEGRA234_MAIN_GPIO_PORT( L, 3, 1, 4),
1093 TEGRA234_MAIN_GPIO_PORT( M, 2, 0, 8),
1094 TEGRA234_MAIN_GPIO_PORT( N, 2, 1, 8),
1095 TEGRA234_MAIN_GPIO_PORT( P, 2, 2, 8),
1096 TEGRA234_MAIN_GPIO_PORT( Q, 2, 3, 8),
1097 TEGRA234_MAIN_GPIO_PORT( R, 2, 4, 6),
1098 TEGRA234_MAIN_GPIO_PORT( X, 1, 0, 8),
1099 TEGRA234_MAIN_GPIO_PORT( Y, 1, 1, 8),
1100 TEGRA234_MAIN_GPIO_PORT( Z, 1, 2, 8),
1101 TEGRA234_MAIN_GPIO_PORT(AC, 0, 1, 8),
1102 TEGRA234_MAIN_GPIO_PORT(AD, 0, 2, 4),
1103 TEGRA234_MAIN_GPIO_PORT(AE, 3, 3, 2),
1104 TEGRA234_MAIN_GPIO_PORT(AF, 3, 4, 4),
1105 TEGRA234_MAIN_GPIO_PORT(AG, 3, 2, 8),
1108 static const struct tegra_gpio_soc tegra234_main_soc = {
1109 .num_ports = ARRAY_SIZE(tegra234_main_ports),
1110 .ports = tegra234_main_ports,
1111 .name = "tegra234-gpio",
1113 .num_irqs_per_bank = 8,
1116 #define TEGRA234_AON_GPIO_PORT(_name, _bank, _port, _pins) \
1117 [TEGRA234_AON_GPIO_PORT_##_name] = { \
1124 static const struct tegra_gpio_port tegra234_aon_ports[] = {
1125 TEGRA234_AON_GPIO_PORT(AA, 0, 4, 8),
1126 TEGRA234_AON_GPIO_PORT(BB, 0, 5, 4),
1127 TEGRA234_AON_GPIO_PORT(CC, 0, 2, 8),
1128 TEGRA234_AON_GPIO_PORT(DD, 0, 3, 3),
1129 TEGRA234_AON_GPIO_PORT(EE, 0, 0, 8),
1130 TEGRA234_AON_GPIO_PORT(GG, 0, 1, 1),
1133 static const struct tegra_gpio_soc tegra234_aon_soc = {
1134 .num_ports = ARRAY_SIZE(tegra234_aon_ports),
1135 .ports = tegra234_aon_ports,
1136 .name = "tegra234-gpio-aon",
1138 .num_irqs_per_bank = 8,
1141 #define TEGRA241_MAIN_GPIO_PORT(_name, _bank, _port, _pins) \
1142 [TEGRA241_MAIN_GPIO_PORT_##_name] = { \
1149 static const struct tegra_gpio_port tegra241_main_ports[] = {
1150 TEGRA241_MAIN_GPIO_PORT(A, 0, 0, 8),
1151 TEGRA241_MAIN_GPIO_PORT(B, 0, 1, 8),
1152 TEGRA241_MAIN_GPIO_PORT(C, 0, 2, 2),
1153 TEGRA241_MAIN_GPIO_PORT(D, 0, 3, 6),
1154 TEGRA241_MAIN_GPIO_PORT(E, 0, 4, 8),
1155 TEGRA241_MAIN_GPIO_PORT(F, 1, 0, 8),
1156 TEGRA241_MAIN_GPIO_PORT(G, 1, 1, 8),
1157 TEGRA241_MAIN_GPIO_PORT(H, 1, 2, 8),
1158 TEGRA241_MAIN_GPIO_PORT(J, 1, 3, 8),
1159 TEGRA241_MAIN_GPIO_PORT(K, 1, 4, 4),
1160 TEGRA241_MAIN_GPIO_PORT(L, 1, 5, 6),
1163 static const struct tegra_gpio_soc tegra241_main_soc = {
1164 .num_ports = ARRAY_SIZE(tegra241_main_ports),
1165 .ports = tegra241_main_ports,
1166 .name = "tegra241-gpio",
1168 .num_irqs_per_bank = 8,
1171 #define TEGRA241_AON_GPIO_PORT(_name, _bank, _port, _pins) \
1172 [TEGRA241_AON_GPIO_PORT_##_name] = { \
1179 static const struct tegra_gpio_port tegra241_aon_ports[] = {
1180 TEGRA241_AON_GPIO_PORT(AA, 0, 0, 8),
1181 TEGRA241_AON_GPIO_PORT(BB, 0, 0, 4),
1184 static const struct tegra_gpio_soc tegra241_aon_soc = {
1185 .num_ports = ARRAY_SIZE(tegra241_aon_ports),
1186 .ports = tegra241_aon_ports,
1187 .name = "tegra241-gpio-aon",
1189 .num_irqs_per_bank = 8,
1192 static const struct of_device_id tegra186_gpio_of_match[] = {
1194 .compatible = "nvidia,tegra186-gpio",
1195 .data = &tegra186_main_soc
1197 .compatible = "nvidia,tegra186-gpio-aon",
1198 .data = &tegra186_aon_soc
1200 .compatible = "nvidia,tegra194-gpio",
1201 .data = &tegra194_main_soc
1203 .compatible = "nvidia,tegra194-gpio-aon",
1204 .data = &tegra194_aon_soc
1206 .compatible = "nvidia,tegra234-gpio",
1207 .data = &tegra234_main_soc
1209 .compatible = "nvidia,tegra234-gpio-aon",
1210 .data = &tegra234_aon_soc
1215 MODULE_DEVICE_TABLE(of, tegra186_gpio_of_match);
1217 static const struct acpi_device_id tegra186_gpio_acpi_match[] = {
1218 { .id = "NVDA0108", .driver_data = (kernel_ulong_t)&tegra186_main_soc },
1219 { .id = "NVDA0208", .driver_data = (kernel_ulong_t)&tegra186_aon_soc },
1220 { .id = "NVDA0308", .driver_data = (kernel_ulong_t)&tegra194_main_soc },
1221 { .id = "NVDA0408", .driver_data = (kernel_ulong_t)&tegra194_aon_soc },
1222 { .id = "NVDA0508", .driver_data = (kernel_ulong_t)&tegra241_main_soc },
1223 { .id = "NVDA0608", .driver_data = (kernel_ulong_t)&tegra241_aon_soc },
1226 MODULE_DEVICE_TABLE(acpi, tegra186_gpio_acpi_match);
1228 static struct platform_driver tegra186_gpio_driver = {
1230 .name = "tegra186-gpio",
1231 .of_match_table = tegra186_gpio_of_match,
1232 .acpi_match_table = tegra186_gpio_acpi_match,
1234 .probe = tegra186_gpio_probe,
1236 module_platform_driver(tegra186_gpio_driver);
1238 MODULE_DESCRIPTION("NVIDIA Tegra186 GPIO controller driver");
1239 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1240 MODULE_LICENSE("GPL v2");