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 void *tegra186_gpio_populate_parent_fwspec(struct gpio_chip *chip,
625 unsigned int parent_hwirq,
626 unsigned int parent_type)
628 struct tegra_gpio *gpio = gpiochip_get_data(chip);
629 struct irq_fwspec *fwspec;
631 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
635 fwspec->fwnode = chip->irq.parent_domain->fwnode;
636 fwspec->param_count = 3;
637 fwspec->param[0] = gpio->soc->instance;
638 fwspec->param[1] = parent_hwirq;
639 fwspec->param[2] = parent_type;
644 static int tegra186_gpio_child_to_parent_hwirq(struct gpio_chip *chip,
647 unsigned int *parent_hwirq,
648 unsigned int *parent_type)
650 *parent_hwirq = chip->irq.child_offset_to_irq(chip, hwirq);
656 static unsigned int tegra186_gpio_child_offset_to_irq(struct gpio_chip *chip,
659 struct tegra_gpio *gpio = gpiochip_get_data(chip);
662 for (i = 0; i < gpio->soc->num_ports; i++) {
663 if (offset < gpio->soc->ports[i].pins)
666 offset -= gpio->soc->ports[i].pins;
669 return offset + i * 8;
672 static const struct of_device_id tegra186_pmc_of_match[] = {
673 { .compatible = "nvidia,tegra186-pmc" },
674 { .compatible = "nvidia,tegra194-pmc" },
678 static void tegra186_gpio_init_route_mapping(struct tegra_gpio *gpio)
680 struct device *dev = gpio->gpio.parent;
684 for (i = 0; i < gpio->soc->num_ports; i++) {
685 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
686 unsigned int offset, p = port->port;
689 base = gpio->secure + port->bank * 0x1000 + 0x800;
691 value = readl(base + TEGRA186_GPIO_CTL_SCR);
694 * For controllers that haven't been locked down yet, make
695 * sure to program the default interrupt route mapping.
697 if ((value & TEGRA186_GPIO_CTL_SCR_SEC_REN) == 0 &&
698 (value & TEGRA186_GPIO_CTL_SCR_SEC_WEN) == 0) {
700 * On Tegra194 and later, each pin can be routed to one or more
703 for (j = 0; j < gpio->num_irqs_per_bank; j++) {
704 dev_dbg(dev, "programming default interrupt routing for port %s\n",
707 offset = TEGRA186_GPIO_INT_ROUTE_MAPPING(p, j);
710 * By default we only want to route GPIO pins to IRQ 0. This works
711 * only under the assumption that we're running as the host kernel
712 * and hence all GPIO pins are owned by Linux.
714 * For cases where Linux is the guest OS, the hypervisor will have
715 * to configure the interrupt routing and pass only the valid
716 * interrupts via device tree.
719 value = readl(base + offset);
720 value = BIT(port->pins) - 1;
721 writel(value, base + offset);
728 static unsigned int tegra186_gpio_irqs_per_bank(struct tegra_gpio *gpio)
730 struct device *dev = gpio->gpio.parent;
732 if (gpio->num_irq > gpio->num_banks) {
733 if (gpio->num_irq % gpio->num_banks != 0)
737 if (gpio->num_irq < gpio->num_banks)
740 gpio->num_irqs_per_bank = gpio->num_irq / gpio->num_banks;
742 if (gpio->num_irqs_per_bank > gpio->soc->num_irqs_per_bank)
748 dev_err(dev, "invalid number of interrupts (%u) for %u banks\n",
749 gpio->num_irq, gpio->num_banks);
753 static int tegra186_gpio_probe(struct platform_device *pdev)
755 unsigned int i, j, offset;
756 struct gpio_irq_chip *irq;
757 struct tegra_gpio *gpio;
758 struct device_node *np;
762 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
766 gpio->soc = device_get_match_data(&pdev->dev);
767 gpio->gpio.label = gpio->soc->name;
768 gpio->gpio.parent = &pdev->dev;
770 /* count the number of banks in the controller */
771 for (i = 0; i < gpio->soc->num_ports; i++)
772 if (gpio->soc->ports[i].bank > gpio->num_banks)
773 gpio->num_banks = gpio->soc->ports[i].bank;
777 /* get register apertures */
778 gpio->secure = devm_platform_ioremap_resource_byname(pdev, "security");
779 if (IS_ERR(gpio->secure)) {
780 gpio->secure = devm_platform_ioremap_resource(pdev, 0);
781 if (IS_ERR(gpio->secure))
782 return PTR_ERR(gpio->secure);
785 gpio->base = devm_platform_ioremap_resource_byname(pdev, "gpio");
786 if (IS_ERR(gpio->base)) {
787 gpio->base = devm_platform_ioremap_resource(pdev, 1);
788 if (IS_ERR(gpio->base))
789 return PTR_ERR(gpio->base);
792 err = platform_irq_count(pdev);
798 err = tegra186_gpio_irqs_per_bank(gpio);
802 gpio->irq = devm_kcalloc(&pdev->dev, gpio->num_irq, sizeof(*gpio->irq),
807 for (i = 0; i < gpio->num_irq; i++) {
808 err = platform_get_irq(pdev, i);
815 gpio->gpio.request = gpiochip_generic_request;
816 gpio->gpio.free = gpiochip_generic_free;
817 gpio->gpio.get_direction = tegra186_gpio_get_direction;
818 gpio->gpio.direction_input = tegra186_gpio_direction_input;
819 gpio->gpio.direction_output = tegra186_gpio_direction_output;
820 gpio->gpio.get = tegra186_gpio_get;
821 gpio->gpio.set = tegra186_gpio_set;
822 gpio->gpio.set_config = tegra186_gpio_set_config;
823 gpio->gpio.add_pin_ranges = tegra186_gpio_add_pin_ranges;
824 if (gpio->soc->has_gte) {
825 gpio->gpio.en_hw_timestamp = tegra186_gpio_en_hw_ts;
826 gpio->gpio.dis_hw_timestamp = tegra186_gpio_dis_hw_ts;
829 gpio->gpio.base = -1;
831 for (i = 0; i < gpio->soc->num_ports; i++)
832 gpio->gpio.ngpio += gpio->soc->ports[i].pins;
834 names = devm_kcalloc(gpio->gpio.parent, gpio->gpio.ngpio,
835 sizeof(*names), GFP_KERNEL);
839 for (i = 0, offset = 0; i < gpio->soc->num_ports; i++) {
840 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
843 for (j = 0; j < port->pins; j++) {
844 name = devm_kasprintf(gpio->gpio.parent, GFP_KERNEL,
845 "P%s.%02x", port->name, j);
849 names[offset + j] = name;
852 offset += port->pins;
855 gpio->gpio.names = (const char * const *)names;
857 #if defined(CONFIG_OF_GPIO)
858 gpio->gpio.of_gpio_n_cells = 2;
859 gpio->gpio.of_xlate = tegra186_gpio_of_xlate;
860 #endif /* CONFIG_OF_GPIO */
862 irq = &gpio->gpio.irq;
863 gpio_irq_chip_set_chip(irq, &tegra186_gpio_irq_chip);
864 irq->fwnode = of_node_to_fwnode(pdev->dev.of_node);
865 irq->child_to_parent_hwirq = tegra186_gpio_child_to_parent_hwirq;
866 irq->populate_parent_alloc_arg = tegra186_gpio_populate_parent_fwspec;
867 irq->child_offset_to_irq = tegra186_gpio_child_offset_to_irq;
868 irq->child_irq_domain_ops.translate = tegra186_gpio_irq_domain_translate;
869 irq->handler = handle_simple_irq;
870 irq->default_type = IRQ_TYPE_NONE;
871 irq->parent_handler = tegra186_gpio_irq;
872 irq->parent_handler_data = gpio;
873 irq->num_parents = gpio->num_irq;
876 * To simplify things, use a single interrupt per bank for now. Some
877 * chips support up to 8 interrupts per bank, which can be useful to
878 * distribute the load and decrease the processing latency for GPIOs
879 * but it also requires a more complicated interrupt routing than we
882 if (gpio->num_irqs_per_bank > 1) {
883 irq->parents = devm_kcalloc(&pdev->dev, gpio->num_banks,
884 sizeof(*irq->parents), GFP_KERNEL);
888 for (i = 0; i < gpio->num_banks; i++)
889 irq->parents[i] = gpio->irq[i * gpio->num_irqs_per_bank];
891 irq->num_parents = gpio->num_banks;
893 irq->num_parents = gpio->num_irq;
894 irq->parents = gpio->irq;
897 if (gpio->soc->num_irqs_per_bank > 1)
898 tegra186_gpio_init_route_mapping(gpio);
900 np = of_find_matching_node(NULL, tegra186_pmc_of_match);
902 irq->parent_domain = irq_find_host(np);
905 if (!irq->parent_domain)
906 return -EPROBE_DEFER;
909 irq->map = devm_kcalloc(&pdev->dev, gpio->gpio.ngpio,
910 sizeof(*irq->map), GFP_KERNEL);
914 for (i = 0, offset = 0; i < gpio->soc->num_ports; i++) {
915 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
917 for (j = 0; j < port->pins; j++)
918 irq->map[offset + j] = irq->parents[port->bank];
920 offset += port->pins;
923 return devm_gpiochip_add_data(&pdev->dev, &gpio->gpio, gpio);
926 #define TEGRA186_MAIN_GPIO_PORT(_name, _bank, _port, _pins) \
927 [TEGRA186_MAIN_GPIO_PORT_##_name] = { \
934 static const struct tegra_gpio_port tegra186_main_ports[] = {
935 TEGRA186_MAIN_GPIO_PORT( A, 2, 0, 7),
936 TEGRA186_MAIN_GPIO_PORT( B, 3, 0, 7),
937 TEGRA186_MAIN_GPIO_PORT( C, 3, 1, 7),
938 TEGRA186_MAIN_GPIO_PORT( D, 3, 2, 6),
939 TEGRA186_MAIN_GPIO_PORT( E, 2, 1, 8),
940 TEGRA186_MAIN_GPIO_PORT( F, 2, 2, 6),
941 TEGRA186_MAIN_GPIO_PORT( G, 4, 1, 6),
942 TEGRA186_MAIN_GPIO_PORT( H, 1, 0, 7),
943 TEGRA186_MAIN_GPIO_PORT( I, 0, 4, 8),
944 TEGRA186_MAIN_GPIO_PORT( J, 5, 0, 8),
945 TEGRA186_MAIN_GPIO_PORT( K, 5, 1, 1),
946 TEGRA186_MAIN_GPIO_PORT( L, 1, 1, 8),
947 TEGRA186_MAIN_GPIO_PORT( M, 5, 3, 6),
948 TEGRA186_MAIN_GPIO_PORT( N, 0, 0, 7),
949 TEGRA186_MAIN_GPIO_PORT( O, 0, 1, 4),
950 TEGRA186_MAIN_GPIO_PORT( P, 4, 0, 7),
951 TEGRA186_MAIN_GPIO_PORT( Q, 0, 2, 6),
952 TEGRA186_MAIN_GPIO_PORT( R, 0, 5, 6),
953 TEGRA186_MAIN_GPIO_PORT( T, 0, 3, 4),
954 TEGRA186_MAIN_GPIO_PORT( X, 1, 2, 8),
955 TEGRA186_MAIN_GPIO_PORT( Y, 1, 3, 7),
956 TEGRA186_MAIN_GPIO_PORT(BB, 2, 3, 2),
957 TEGRA186_MAIN_GPIO_PORT(CC, 5, 2, 4),
960 static const struct tegra_gpio_soc tegra186_main_soc = {
961 .num_ports = ARRAY_SIZE(tegra186_main_ports),
962 .ports = tegra186_main_ports,
963 .name = "tegra186-gpio",
965 .num_irqs_per_bank = 1,
968 #define TEGRA186_AON_GPIO_PORT(_name, _bank, _port, _pins) \
969 [TEGRA186_AON_GPIO_PORT_##_name] = { \
976 static const struct tegra_gpio_port tegra186_aon_ports[] = {
977 TEGRA186_AON_GPIO_PORT( S, 0, 1, 5),
978 TEGRA186_AON_GPIO_PORT( U, 0, 2, 6),
979 TEGRA186_AON_GPIO_PORT( V, 0, 4, 8),
980 TEGRA186_AON_GPIO_PORT( W, 0, 5, 8),
981 TEGRA186_AON_GPIO_PORT( Z, 0, 7, 4),
982 TEGRA186_AON_GPIO_PORT(AA, 0, 6, 8),
983 TEGRA186_AON_GPIO_PORT(EE, 0, 3, 3),
984 TEGRA186_AON_GPIO_PORT(FF, 0, 0, 5),
987 static const struct tegra_gpio_soc tegra186_aon_soc = {
988 .num_ports = ARRAY_SIZE(tegra186_aon_ports),
989 .ports = tegra186_aon_ports,
990 .name = "tegra186-gpio-aon",
992 .num_irqs_per_bank = 1,
995 #define TEGRA194_MAIN_GPIO_PORT(_name, _bank, _port, _pins) \
996 [TEGRA194_MAIN_GPIO_PORT_##_name] = { \
1003 static const struct tegra_gpio_port tegra194_main_ports[] = {
1004 TEGRA194_MAIN_GPIO_PORT( A, 1, 2, 8),
1005 TEGRA194_MAIN_GPIO_PORT( B, 4, 7, 2),
1006 TEGRA194_MAIN_GPIO_PORT( C, 4, 3, 8),
1007 TEGRA194_MAIN_GPIO_PORT( D, 4, 4, 4),
1008 TEGRA194_MAIN_GPIO_PORT( E, 4, 5, 8),
1009 TEGRA194_MAIN_GPIO_PORT( F, 4, 6, 6),
1010 TEGRA194_MAIN_GPIO_PORT( G, 4, 0, 8),
1011 TEGRA194_MAIN_GPIO_PORT( H, 4, 1, 8),
1012 TEGRA194_MAIN_GPIO_PORT( I, 4, 2, 5),
1013 TEGRA194_MAIN_GPIO_PORT( J, 5, 1, 6),
1014 TEGRA194_MAIN_GPIO_PORT( K, 3, 0, 8),
1015 TEGRA194_MAIN_GPIO_PORT( L, 3, 1, 4),
1016 TEGRA194_MAIN_GPIO_PORT( M, 2, 3, 8),
1017 TEGRA194_MAIN_GPIO_PORT( N, 2, 4, 3),
1018 TEGRA194_MAIN_GPIO_PORT( O, 5, 0, 6),
1019 TEGRA194_MAIN_GPIO_PORT( P, 2, 5, 8),
1020 TEGRA194_MAIN_GPIO_PORT( Q, 2, 6, 8),
1021 TEGRA194_MAIN_GPIO_PORT( R, 2, 7, 6),
1022 TEGRA194_MAIN_GPIO_PORT( S, 3, 3, 8),
1023 TEGRA194_MAIN_GPIO_PORT( T, 3, 4, 8),
1024 TEGRA194_MAIN_GPIO_PORT( U, 3, 5, 1),
1025 TEGRA194_MAIN_GPIO_PORT( V, 1, 0, 8),
1026 TEGRA194_MAIN_GPIO_PORT( W, 1, 1, 2),
1027 TEGRA194_MAIN_GPIO_PORT( X, 2, 0, 8),
1028 TEGRA194_MAIN_GPIO_PORT( Y, 2, 1, 8),
1029 TEGRA194_MAIN_GPIO_PORT( Z, 2, 2, 8),
1030 TEGRA194_MAIN_GPIO_PORT(FF, 3, 2, 2),
1031 TEGRA194_MAIN_GPIO_PORT(GG, 0, 0, 2)
1034 static const struct tegra186_pin_range tegra194_main_pin_ranges[] = {
1035 { TEGRA194_MAIN_GPIO(GG, 0), "pex_l5_clkreq_n_pgg0" },
1036 { TEGRA194_MAIN_GPIO(GG, 1), "pex_l5_rst_n_pgg1" },
1039 static const struct tegra_gpio_soc tegra194_main_soc = {
1040 .num_ports = ARRAY_SIZE(tegra194_main_ports),
1041 .ports = tegra194_main_ports,
1042 .name = "tegra194-gpio",
1044 .num_irqs_per_bank = 8,
1045 .num_pin_ranges = ARRAY_SIZE(tegra194_main_pin_ranges),
1046 .pin_ranges = tegra194_main_pin_ranges,
1047 .pinmux = "nvidia,tegra194-pinmux",
1050 #define TEGRA194_AON_GPIO_PORT(_name, _bank, _port, _pins) \
1051 [TEGRA194_AON_GPIO_PORT_##_name] = { \
1058 static const struct tegra_gpio_port tegra194_aon_ports[] = {
1059 TEGRA194_AON_GPIO_PORT(AA, 0, 3, 8),
1060 TEGRA194_AON_GPIO_PORT(BB, 0, 4, 4),
1061 TEGRA194_AON_GPIO_PORT(CC, 0, 1, 8),
1062 TEGRA194_AON_GPIO_PORT(DD, 0, 2, 3),
1063 TEGRA194_AON_GPIO_PORT(EE, 0, 0, 7)
1066 static const struct tegra_gpio_soc tegra194_aon_soc = {
1067 .num_ports = ARRAY_SIZE(tegra194_aon_ports),
1068 .ports = tegra194_aon_ports,
1069 .name = "tegra194-gpio-aon",
1071 .num_irqs_per_bank = 8,
1075 #define TEGRA234_MAIN_GPIO_PORT(_name, _bank, _port, _pins) \
1076 [TEGRA234_MAIN_GPIO_PORT_##_name] = { \
1083 static const struct tegra_gpio_port tegra234_main_ports[] = {
1084 TEGRA234_MAIN_GPIO_PORT( A, 0, 0, 8),
1085 TEGRA234_MAIN_GPIO_PORT( B, 0, 3, 1),
1086 TEGRA234_MAIN_GPIO_PORT( C, 5, 1, 8),
1087 TEGRA234_MAIN_GPIO_PORT( D, 5, 2, 4),
1088 TEGRA234_MAIN_GPIO_PORT( E, 5, 3, 8),
1089 TEGRA234_MAIN_GPIO_PORT( F, 5, 4, 6),
1090 TEGRA234_MAIN_GPIO_PORT( G, 4, 0, 8),
1091 TEGRA234_MAIN_GPIO_PORT( H, 4, 1, 8),
1092 TEGRA234_MAIN_GPIO_PORT( I, 4, 2, 7),
1093 TEGRA234_MAIN_GPIO_PORT( J, 5, 0, 6),
1094 TEGRA234_MAIN_GPIO_PORT( K, 3, 0, 8),
1095 TEGRA234_MAIN_GPIO_PORT( L, 3, 1, 4),
1096 TEGRA234_MAIN_GPIO_PORT( M, 2, 0, 8),
1097 TEGRA234_MAIN_GPIO_PORT( N, 2, 1, 8),
1098 TEGRA234_MAIN_GPIO_PORT( P, 2, 2, 8),
1099 TEGRA234_MAIN_GPIO_PORT( Q, 2, 3, 8),
1100 TEGRA234_MAIN_GPIO_PORT( R, 2, 4, 6),
1101 TEGRA234_MAIN_GPIO_PORT( X, 1, 0, 8),
1102 TEGRA234_MAIN_GPIO_PORT( Y, 1, 1, 8),
1103 TEGRA234_MAIN_GPIO_PORT( Z, 1, 2, 8),
1104 TEGRA234_MAIN_GPIO_PORT(AC, 0, 1, 8),
1105 TEGRA234_MAIN_GPIO_PORT(AD, 0, 2, 4),
1106 TEGRA234_MAIN_GPIO_PORT(AE, 3, 3, 2),
1107 TEGRA234_MAIN_GPIO_PORT(AF, 3, 4, 4),
1108 TEGRA234_MAIN_GPIO_PORT(AG, 3, 2, 8),
1111 static const struct tegra_gpio_soc tegra234_main_soc = {
1112 .num_ports = ARRAY_SIZE(tegra234_main_ports),
1113 .ports = tegra234_main_ports,
1114 .name = "tegra234-gpio",
1116 .num_irqs_per_bank = 8,
1119 #define TEGRA234_AON_GPIO_PORT(_name, _bank, _port, _pins) \
1120 [TEGRA234_AON_GPIO_PORT_##_name] = { \
1127 static const struct tegra_gpio_port tegra234_aon_ports[] = {
1128 TEGRA234_AON_GPIO_PORT(AA, 0, 4, 8),
1129 TEGRA234_AON_GPIO_PORT(BB, 0, 5, 4),
1130 TEGRA234_AON_GPIO_PORT(CC, 0, 2, 8),
1131 TEGRA234_AON_GPIO_PORT(DD, 0, 3, 3),
1132 TEGRA234_AON_GPIO_PORT(EE, 0, 0, 8),
1133 TEGRA234_AON_GPIO_PORT(GG, 0, 1, 1),
1136 static const struct tegra_gpio_soc tegra234_aon_soc = {
1137 .num_ports = ARRAY_SIZE(tegra234_aon_ports),
1138 .ports = tegra234_aon_ports,
1139 .name = "tegra234-gpio-aon",
1141 .num_irqs_per_bank = 8,
1144 #define TEGRA241_MAIN_GPIO_PORT(_name, _bank, _port, _pins) \
1145 [TEGRA241_MAIN_GPIO_PORT_##_name] = { \
1152 static const struct tegra_gpio_port tegra241_main_ports[] = {
1153 TEGRA241_MAIN_GPIO_PORT(A, 0, 0, 8),
1154 TEGRA241_MAIN_GPIO_PORT(B, 0, 1, 8),
1155 TEGRA241_MAIN_GPIO_PORT(C, 0, 2, 2),
1156 TEGRA241_MAIN_GPIO_PORT(D, 0, 3, 6),
1157 TEGRA241_MAIN_GPIO_PORT(E, 0, 4, 8),
1158 TEGRA241_MAIN_GPIO_PORT(F, 1, 0, 8),
1159 TEGRA241_MAIN_GPIO_PORT(G, 1, 1, 8),
1160 TEGRA241_MAIN_GPIO_PORT(H, 1, 2, 8),
1161 TEGRA241_MAIN_GPIO_PORT(J, 1, 3, 8),
1162 TEGRA241_MAIN_GPIO_PORT(K, 1, 4, 4),
1163 TEGRA241_MAIN_GPIO_PORT(L, 1, 5, 6),
1166 static const struct tegra_gpio_soc tegra241_main_soc = {
1167 .num_ports = ARRAY_SIZE(tegra241_main_ports),
1168 .ports = tegra241_main_ports,
1169 .name = "tegra241-gpio",
1171 .num_irqs_per_bank = 8,
1174 #define TEGRA241_AON_GPIO_PORT(_name, _bank, _port, _pins) \
1175 [TEGRA241_AON_GPIO_PORT_##_name] = { \
1182 static const struct tegra_gpio_port tegra241_aon_ports[] = {
1183 TEGRA241_AON_GPIO_PORT(AA, 0, 0, 8),
1184 TEGRA241_AON_GPIO_PORT(BB, 0, 0, 4),
1187 static const struct tegra_gpio_soc tegra241_aon_soc = {
1188 .num_ports = ARRAY_SIZE(tegra241_aon_ports),
1189 .ports = tegra241_aon_ports,
1190 .name = "tegra241-gpio-aon",
1192 .num_irqs_per_bank = 8,
1195 static const struct of_device_id tegra186_gpio_of_match[] = {
1197 .compatible = "nvidia,tegra186-gpio",
1198 .data = &tegra186_main_soc
1200 .compatible = "nvidia,tegra186-gpio-aon",
1201 .data = &tegra186_aon_soc
1203 .compatible = "nvidia,tegra194-gpio",
1204 .data = &tegra194_main_soc
1206 .compatible = "nvidia,tegra194-gpio-aon",
1207 .data = &tegra194_aon_soc
1209 .compatible = "nvidia,tegra234-gpio",
1210 .data = &tegra234_main_soc
1212 .compatible = "nvidia,tegra234-gpio-aon",
1213 .data = &tegra234_aon_soc
1218 MODULE_DEVICE_TABLE(of, tegra186_gpio_of_match);
1220 static const struct acpi_device_id tegra186_gpio_acpi_match[] = {
1221 { .id = "NVDA0108", .driver_data = (kernel_ulong_t)&tegra186_main_soc },
1222 { .id = "NVDA0208", .driver_data = (kernel_ulong_t)&tegra186_aon_soc },
1223 { .id = "NVDA0308", .driver_data = (kernel_ulong_t)&tegra194_main_soc },
1224 { .id = "NVDA0408", .driver_data = (kernel_ulong_t)&tegra194_aon_soc },
1225 { .id = "NVDA0508", .driver_data = (kernel_ulong_t)&tegra241_main_soc },
1226 { .id = "NVDA0608", .driver_data = (kernel_ulong_t)&tegra241_aon_soc },
1229 MODULE_DEVICE_TABLE(acpi, tegra186_gpio_acpi_match);
1231 static struct platform_driver tegra186_gpio_driver = {
1233 .name = "tegra186-gpio",
1234 .of_match_table = tegra186_gpio_of_match,
1235 .acpi_match_table = tegra186_gpio_acpi_match,
1237 .probe = tegra186_gpio_probe,
1239 module_platform_driver(tegra186_gpio_driver);
1241 MODULE_DESCRIPTION("NVIDIA Tegra186 GPIO controller driver");
1242 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1243 MODULE_LICENSE("GPL v2");