1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm/mach-tegra/gpio.c
5 * Copyright (c) 2010 Google, Inc
6 * Copyright (c) 2011-2016, NVIDIA CORPORATION. All rights reserved.
9 * Erik Gilling <konkers@google.com>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/irq.h>
15 #include <linux/interrupt.h>
17 #include <linux/gpio/driver.h>
19 #include <linux/platform_device.h>
20 #include <linux/module.h>
21 #include <linux/seq_file.h>
22 #include <linux/irqdomain.h>
23 #include <linux/irqchip/chained_irq.h>
24 #include <linux/pinctrl/consumer.h>
27 #define GPIO_BANK(x) ((x) >> 5)
28 #define GPIO_PORT(x) (((x) >> 3) & 0x3)
29 #define GPIO_BIT(x) ((x) & 0x7)
31 #define GPIO_REG(tgi, x) (GPIO_BANK(x) * tgi->soc->bank_stride + \
34 #define GPIO_CNF(t, x) (GPIO_REG(t, x) + 0x00)
35 #define GPIO_OE(t, x) (GPIO_REG(t, x) + 0x10)
36 #define GPIO_OUT(t, x) (GPIO_REG(t, x) + 0X20)
37 #define GPIO_IN(t, x) (GPIO_REG(t, x) + 0x30)
38 #define GPIO_INT_STA(t, x) (GPIO_REG(t, x) + 0x40)
39 #define GPIO_INT_ENB(t, x) (GPIO_REG(t, x) + 0x50)
40 #define GPIO_INT_LVL(t, x) (GPIO_REG(t, x) + 0x60)
41 #define GPIO_INT_CLR(t, x) (GPIO_REG(t, x) + 0x70)
42 #define GPIO_DBC_CNT(t, x) (GPIO_REG(t, x) + 0xF0)
45 #define GPIO_MSK_CNF(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x00)
46 #define GPIO_MSK_OE(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x10)
47 #define GPIO_MSK_OUT(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0X20)
48 #define GPIO_MSK_DBC_EN(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x30)
49 #define GPIO_MSK_INT_STA(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x40)
50 #define GPIO_MSK_INT_ENB(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x50)
51 #define GPIO_MSK_INT_LVL(t, x) (GPIO_REG(t, x) + t->soc->upper_offset + 0x60)
53 #define GPIO_INT_LVL_MASK 0x010101
54 #define GPIO_INT_LVL_EDGE_RISING 0x000101
55 #define GPIO_INT_LVL_EDGE_FALLING 0x000100
56 #define GPIO_INT_LVL_EDGE_BOTH 0x010100
57 #define GPIO_INT_LVL_LEVEL_HIGH 0x000001
58 #define GPIO_INT_LVL_LEVEL_LOW 0x000000
60 struct tegra_gpio_info;
62 struct tegra_gpio_bank {
66 * IRQ-core code uses raw locking, and thus, nested locking also
67 * should be raw in order not to trip spinlock debug warnings.
69 raw_spinlock_t lvl_lock[4];
71 /* Lock for updating debounce count register */
72 spinlock_t dbc_lock[4];
74 #ifdef CONFIG_PM_SLEEP
86 struct tegra_gpio_soc_config {
87 bool debounce_supported;
92 struct tegra_gpio_info {
95 struct tegra_gpio_bank *bank_info;
96 const struct tegra_gpio_soc_config *soc;
102 static inline void tegra_gpio_writel(struct tegra_gpio_info *tgi,
105 writel_relaxed(val, tgi->regs + reg);
108 static inline u32 tegra_gpio_readl(struct tegra_gpio_info *tgi, u32 reg)
110 return readl_relaxed(tgi->regs + reg);
113 static unsigned int tegra_gpio_compose(unsigned int bank, unsigned int port,
116 return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
119 static void tegra_gpio_mask_write(struct tegra_gpio_info *tgi, u32 reg,
120 unsigned int gpio, u32 value)
124 val = 0x100 << GPIO_BIT(gpio);
126 val |= 1 << GPIO_BIT(gpio);
127 tegra_gpio_writel(tgi, val, reg);
130 static void tegra_gpio_enable(struct tegra_gpio_info *tgi, unsigned int gpio)
132 tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 1);
135 static void tegra_gpio_disable(struct tegra_gpio_info *tgi, unsigned int gpio)
137 tegra_gpio_mask_write(tgi, GPIO_MSK_CNF(tgi, gpio), gpio, 0);
140 static int tegra_gpio_request(struct gpio_chip *chip, unsigned int offset)
142 return pinctrl_gpio_request(chip->base + offset);
145 static void tegra_gpio_free(struct gpio_chip *chip, unsigned int offset)
147 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
149 pinctrl_gpio_free(chip->base + offset);
150 tegra_gpio_disable(tgi, offset);
153 static void tegra_gpio_set(struct gpio_chip *chip, unsigned int offset,
156 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
158 tegra_gpio_mask_write(tgi, GPIO_MSK_OUT(tgi, offset), offset, value);
161 static int tegra_gpio_get(struct gpio_chip *chip, unsigned int offset)
163 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
164 unsigned int bval = BIT(GPIO_BIT(offset));
166 /* If gpio is in output mode then read from the out value */
167 if (tegra_gpio_readl(tgi, GPIO_OE(tgi, offset)) & bval)
168 return !!(tegra_gpio_readl(tgi, GPIO_OUT(tgi, offset)) & bval);
170 return !!(tegra_gpio_readl(tgi, GPIO_IN(tgi, offset)) & bval);
173 static int tegra_gpio_direction_input(struct gpio_chip *chip,
176 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
179 tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 0);
180 tegra_gpio_enable(tgi, offset);
182 ret = pinctrl_gpio_direction_input(chip->base + offset);
185 "Failed to set pinctrl input direction of GPIO %d: %d",
186 chip->base + offset, ret);
191 static int tegra_gpio_direction_output(struct gpio_chip *chip,
195 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
198 tegra_gpio_set(chip, offset, value);
199 tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 1);
200 tegra_gpio_enable(tgi, offset);
202 ret = pinctrl_gpio_direction_output(chip->base + offset);
205 "Failed to set pinctrl output direction of GPIO %d: %d",
206 chip->base + offset, ret);
211 static int tegra_gpio_get_direction(struct gpio_chip *chip,
214 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
215 u32 pin_mask = BIT(GPIO_BIT(offset));
218 cnf = tegra_gpio_readl(tgi, GPIO_CNF(tgi, offset));
219 if (!(cnf & pin_mask))
222 oe = tegra_gpio_readl(tgi, GPIO_OE(tgi, offset));
225 return GPIO_LINE_DIRECTION_OUT;
227 return GPIO_LINE_DIRECTION_IN;
230 static int tegra_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset,
231 unsigned int debounce)
233 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
234 struct tegra_gpio_bank *bank = &tgi->bank_info[GPIO_BANK(offset)];
235 unsigned int debounce_ms = DIV_ROUND_UP(debounce, 1000);
240 tegra_gpio_mask_write(tgi, GPIO_MSK_DBC_EN(tgi, offset),
245 debounce_ms = min(debounce_ms, 255U);
246 port = GPIO_PORT(offset);
248 /* There is only one debounce count register per port and hence
249 * set the maximum of current and requested debounce time.
251 spin_lock_irqsave(&bank->dbc_lock[port], flags);
252 if (bank->dbc_cnt[port] < debounce_ms) {
253 tegra_gpio_writel(tgi, debounce_ms, GPIO_DBC_CNT(tgi, offset));
254 bank->dbc_cnt[port] = debounce_ms;
256 spin_unlock_irqrestore(&bank->dbc_lock[port], flags);
258 tegra_gpio_mask_write(tgi, GPIO_MSK_DBC_EN(tgi, offset), offset, 1);
263 static int tegra_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
264 unsigned long config)
268 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
271 debounce = pinconf_to_config_argument(config);
272 return tegra_gpio_set_debounce(chip, offset, debounce);
275 static void tegra_gpio_irq_ack(struct irq_data *d)
277 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
278 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
279 unsigned int gpio = d->hwirq;
281 tegra_gpio_writel(tgi, 1 << GPIO_BIT(gpio), GPIO_INT_CLR(tgi, gpio));
284 static void tegra_gpio_irq_mask(struct irq_data *d)
286 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
287 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
288 unsigned int gpio = d->hwirq;
290 tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 0);
291 gpiochip_disable_irq(chip, gpio);
294 static void tegra_gpio_irq_unmask(struct irq_data *d)
296 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
297 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
298 unsigned int gpio = d->hwirq;
300 gpiochip_enable_irq(chip, gpio);
301 tegra_gpio_mask_write(tgi, GPIO_MSK_INT_ENB(tgi, gpio), gpio, 1);
304 static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
306 unsigned int gpio = d->hwirq, port = GPIO_PORT(gpio), lvl_type;
307 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
308 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
309 struct tegra_gpio_bank *bank;
314 bank = &tgi->bank_info[GPIO_BANK(d->hwirq)];
316 switch (type & IRQ_TYPE_SENSE_MASK) {
317 case IRQ_TYPE_EDGE_RISING:
318 lvl_type = GPIO_INT_LVL_EDGE_RISING;
321 case IRQ_TYPE_EDGE_FALLING:
322 lvl_type = GPIO_INT_LVL_EDGE_FALLING;
325 case IRQ_TYPE_EDGE_BOTH:
326 lvl_type = GPIO_INT_LVL_EDGE_BOTH;
329 case IRQ_TYPE_LEVEL_HIGH:
330 lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
333 case IRQ_TYPE_LEVEL_LOW:
334 lvl_type = GPIO_INT_LVL_LEVEL_LOW;
341 raw_spin_lock_irqsave(&bank->lvl_lock[port], flags);
343 val = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
344 val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
345 val |= lvl_type << GPIO_BIT(gpio);
346 tegra_gpio_writel(tgi, val, GPIO_INT_LVL(tgi, gpio));
348 raw_spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
350 tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, gpio), gpio, 0);
351 tegra_gpio_enable(tgi, gpio);
353 ret = gpiochip_lock_as_irq(&tgi->gc, gpio);
356 "unable to lock Tegra GPIO %u as IRQ\n", gpio);
357 tegra_gpio_disable(tgi, gpio);
361 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
362 irq_set_handler_locked(d, handle_level_irq);
363 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
364 irq_set_handler_locked(d, handle_edge_irq);
367 ret = irq_chip_set_type_parent(d, type);
372 static void tegra_gpio_irq_shutdown(struct irq_data *d)
374 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
375 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
376 unsigned int gpio = d->hwirq;
378 tegra_gpio_irq_mask(d);
379 gpiochip_unlock_as_irq(&tgi->gc, gpio);
382 static void tegra_gpio_irq_handler(struct irq_desc *desc)
384 struct tegra_gpio_info *tgi = irq_desc_get_handler_data(desc);
385 struct irq_chip *chip = irq_desc_get_chip(desc);
386 struct irq_domain *domain = tgi->gc.irq.domain;
387 unsigned int irq = irq_desc_get_irq(desc);
388 struct tegra_gpio_bank *bank = NULL;
389 unsigned int port, pin, gpio, i;
390 bool unmasked = false;
394 for (i = 0; i < tgi->bank_count; i++) {
395 if (tgi->irqs[i] == irq) {
396 bank = &tgi->bank_info[i];
401 if (WARN_ON(bank == NULL))
404 chained_irq_enter(chip, desc);
406 for (port = 0; port < 4; port++) {
407 gpio = tegra_gpio_compose(bank->bank, port, 0);
408 sta = tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)) &
409 tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio));
410 lvl = tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio));
412 for_each_set_bit(pin, &sta, 8) {
415 tegra_gpio_writel(tgi, 1 << pin,
416 GPIO_INT_CLR(tgi, gpio));
418 /* if gpio is edge triggered, clear condition
419 * before executing the handler so that we don't
422 if (!unmasked && lvl & (0x100 << pin)) {
424 chained_irq_exit(chip, desc);
427 ret = generic_handle_domain_irq(domain, gpio + pin);
428 WARN_RATELIMIT(ret, "hwirq = %d", gpio + pin);
433 chained_irq_exit(chip, desc);
436 static int tegra_gpio_child_to_parent_hwirq(struct gpio_chip *chip,
439 unsigned int *parent_hwirq,
440 unsigned int *parent_type)
442 *parent_hwirq = chip->irq.child_offset_to_irq(chip, hwirq);
448 static int tegra_gpio_populate_parent_fwspec(struct gpio_chip *chip,
449 union gpio_irq_fwspec *gfwspec,
450 unsigned int parent_hwirq,
451 unsigned int parent_type)
453 struct irq_fwspec *fwspec = &gfwspec->fwspec;
455 fwspec->fwnode = chip->irq.parent_domain->fwnode;
456 fwspec->param_count = 3;
457 fwspec->param[0] = 0;
458 fwspec->param[1] = parent_hwirq;
459 fwspec->param[2] = parent_type;
464 #ifdef CONFIG_PM_SLEEP
465 static int tegra_gpio_resume(struct device *dev)
467 struct tegra_gpio_info *tgi = dev_get_drvdata(dev);
470 for (b = 0; b < tgi->bank_count; b++) {
471 struct tegra_gpio_bank *bank = &tgi->bank_info[b];
473 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
474 unsigned int gpio = (b << 5) | (p << 3);
476 tegra_gpio_writel(tgi, bank->cnf[p],
477 GPIO_CNF(tgi, gpio));
479 if (tgi->soc->debounce_supported) {
480 tegra_gpio_writel(tgi, bank->dbc_cnt[p],
481 GPIO_DBC_CNT(tgi, gpio));
482 tegra_gpio_writel(tgi, bank->dbc_enb[p],
483 GPIO_MSK_DBC_EN(tgi, gpio));
486 tegra_gpio_writel(tgi, bank->out[p],
487 GPIO_OUT(tgi, gpio));
488 tegra_gpio_writel(tgi, bank->oe[p],
490 tegra_gpio_writel(tgi, bank->int_lvl[p],
491 GPIO_INT_LVL(tgi, gpio));
492 tegra_gpio_writel(tgi, bank->int_enb[p],
493 GPIO_INT_ENB(tgi, gpio));
500 static int tegra_gpio_suspend(struct device *dev)
502 struct tegra_gpio_info *tgi = dev_get_drvdata(dev);
505 for (b = 0; b < tgi->bank_count; b++) {
506 struct tegra_gpio_bank *bank = &tgi->bank_info[b];
508 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
509 unsigned int gpio = (b << 5) | (p << 3);
511 bank->cnf[p] = tegra_gpio_readl(tgi,
512 GPIO_CNF(tgi, gpio));
513 bank->out[p] = tegra_gpio_readl(tgi,
514 GPIO_OUT(tgi, gpio));
515 bank->oe[p] = tegra_gpio_readl(tgi,
517 if (tgi->soc->debounce_supported) {
518 bank->dbc_enb[p] = tegra_gpio_readl(tgi,
519 GPIO_MSK_DBC_EN(tgi, gpio));
520 bank->dbc_enb[p] = (bank->dbc_enb[p] << 8) |
524 bank->int_enb[p] = tegra_gpio_readl(tgi,
525 GPIO_INT_ENB(tgi, gpio));
526 bank->int_lvl[p] = tegra_gpio_readl(tgi,
527 GPIO_INT_LVL(tgi, gpio));
529 /* Enable gpio irq for wake up source */
530 tegra_gpio_writel(tgi, bank->wake_enb[p],
531 GPIO_INT_ENB(tgi, gpio));
538 static int tegra_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
540 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
541 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
542 struct tegra_gpio_bank *bank;
543 unsigned int gpio = d->hwirq;
547 bank = &tgi->bank_info[GPIO_BANK(d->hwirq)];
549 port = GPIO_PORT(gpio);
550 bit = GPIO_BIT(gpio);
553 err = irq_set_irq_wake(tgi->irqs[bank->bank], enable);
557 if (d->parent_data) {
558 err = irq_chip_set_wake_parent(d, enable);
560 irq_set_irq_wake(tgi->irqs[bank->bank], !enable);
566 bank->wake_enb[port] |= mask;
568 bank->wake_enb[port] &= ~mask;
574 static int tegra_gpio_irq_set_affinity(struct irq_data *data,
575 const struct cpumask *dest,
578 if (data->parent_data)
579 return irq_chip_set_affinity_parent(data, dest, force);
584 static int tegra_gpio_irq_request_resources(struct irq_data *d)
586 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
587 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
589 tegra_gpio_enable(tgi, d->hwirq);
591 return gpiochip_reqres_irq(chip, d->hwirq);
594 static void tegra_gpio_irq_release_resources(struct irq_data *d)
596 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
597 struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
599 gpiochip_relres_irq(chip, d->hwirq);
600 tegra_gpio_enable(tgi, d->hwirq);
603 static void tegra_gpio_irq_print_chip(struct irq_data *d, struct seq_file *s)
605 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
607 seq_printf(s, dev_name(chip->parent));
610 static const struct irq_chip tegra_gpio_irq_chip = {
611 .irq_shutdown = tegra_gpio_irq_shutdown,
612 .irq_ack = tegra_gpio_irq_ack,
613 .irq_mask = tegra_gpio_irq_mask,
614 .irq_unmask = tegra_gpio_irq_unmask,
615 .irq_set_type = tegra_gpio_irq_set_type,
616 #ifdef CONFIG_PM_SLEEP
617 .irq_set_wake = tegra_gpio_irq_set_wake,
619 .irq_print_chip = tegra_gpio_irq_print_chip,
620 .irq_request_resources = tegra_gpio_irq_request_resources,
621 .irq_release_resources = tegra_gpio_irq_release_resources,
622 .flags = IRQCHIP_IMMUTABLE,
625 static const struct irq_chip tegra210_gpio_irq_chip = {
626 .irq_shutdown = tegra_gpio_irq_shutdown,
627 .irq_ack = tegra_gpio_irq_ack,
628 .irq_mask = tegra_gpio_irq_mask,
629 .irq_unmask = tegra_gpio_irq_unmask,
630 .irq_set_affinity = tegra_gpio_irq_set_affinity,
631 .irq_set_type = tegra_gpio_irq_set_type,
632 #ifdef CONFIG_PM_SLEEP
633 .irq_set_wake = tegra_gpio_irq_set_wake,
635 .irq_print_chip = tegra_gpio_irq_print_chip,
636 .irq_request_resources = tegra_gpio_irq_request_resources,
637 .irq_release_resources = tegra_gpio_irq_release_resources,
638 .flags = IRQCHIP_IMMUTABLE,
641 #ifdef CONFIG_DEBUG_FS
643 #include <linux/debugfs.h>
645 static int tegra_dbg_gpio_show(struct seq_file *s, void *unused)
647 struct tegra_gpio_info *tgi = dev_get_drvdata(s->private);
650 for (i = 0; i < tgi->bank_count; i++) {
651 for (j = 0; j < 4; j++) {
652 unsigned int gpio = tegra_gpio_compose(i, j, 0);
655 "%u:%u %02x %02x %02x %02x %02x %02x %06x\n",
657 tegra_gpio_readl(tgi, GPIO_CNF(tgi, gpio)),
658 tegra_gpio_readl(tgi, GPIO_OE(tgi, gpio)),
659 tegra_gpio_readl(tgi, GPIO_OUT(tgi, gpio)),
660 tegra_gpio_readl(tgi, GPIO_IN(tgi, gpio)),
661 tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)),
662 tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio)),
663 tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio)));
669 static void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
671 debugfs_create_devm_seqfile(tgi->dev, "tegra_gpio", NULL,
672 tegra_dbg_gpio_show);
677 static inline void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
683 static const struct dev_pm_ops tegra_gpio_pm_ops = {
684 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_gpio_suspend, tegra_gpio_resume)
687 static const struct of_device_id tegra_pmc_of_match[] = {
688 { .compatible = "nvidia,tegra210-pmc", },
692 static int tegra_gpio_probe(struct platform_device *pdev)
694 struct tegra_gpio_bank *bank;
695 struct tegra_gpio_info *tgi;
696 struct gpio_irq_chip *irq;
697 struct device_node *np;
701 tgi = devm_kzalloc(&pdev->dev, sizeof(*tgi), GFP_KERNEL);
705 tgi->soc = of_device_get_match_data(&pdev->dev);
706 tgi->dev = &pdev->dev;
708 ret = platform_irq_count(pdev);
712 tgi->bank_count = ret;
714 if (!tgi->bank_count) {
715 dev_err(&pdev->dev, "Missing IRQ resource\n");
719 tgi->gc.label = "tegra-gpio";
720 tgi->gc.request = tegra_gpio_request;
721 tgi->gc.free = tegra_gpio_free;
722 tgi->gc.direction_input = tegra_gpio_direction_input;
723 tgi->gc.get = tegra_gpio_get;
724 tgi->gc.direction_output = tegra_gpio_direction_output;
725 tgi->gc.set = tegra_gpio_set;
726 tgi->gc.get_direction = tegra_gpio_get_direction;
728 tgi->gc.ngpio = tgi->bank_count * 32;
729 tgi->gc.parent = &pdev->dev;
731 platform_set_drvdata(pdev, tgi);
733 if (tgi->soc->debounce_supported)
734 tgi->gc.set_config = tegra_gpio_set_config;
736 tgi->bank_info = devm_kcalloc(&pdev->dev, tgi->bank_count,
737 sizeof(*tgi->bank_info), GFP_KERNEL);
741 tgi->irqs = devm_kcalloc(&pdev->dev, tgi->bank_count,
742 sizeof(*tgi->irqs), GFP_KERNEL);
746 for (i = 0; i < tgi->bank_count; i++) {
747 ret = platform_get_irq(pdev, i);
751 bank = &tgi->bank_info[i];
756 for (j = 0; j < 4; j++) {
757 raw_spin_lock_init(&bank->lvl_lock[j]);
758 spin_lock_init(&bank->dbc_lock[j]);
763 irq->fwnode = of_node_to_fwnode(pdev->dev.of_node);
764 irq->child_to_parent_hwirq = tegra_gpio_child_to_parent_hwirq;
765 irq->populate_parent_alloc_arg = tegra_gpio_populate_parent_fwspec;
766 irq->handler = handle_simple_irq;
767 irq->default_type = IRQ_TYPE_NONE;
768 irq->parent_handler = tegra_gpio_irq_handler;
769 irq->parent_handler_data = tgi;
770 irq->num_parents = tgi->bank_count;
771 irq->parents = tgi->irqs;
773 np = of_find_matching_node(NULL, tegra_pmc_of_match);
775 irq->parent_domain = irq_find_host(np);
778 if (!irq->parent_domain)
779 return -EPROBE_DEFER;
781 gpio_irq_chip_set_chip(irq, &tegra210_gpio_irq_chip);
783 gpio_irq_chip_set_chip(irq, &tegra_gpio_irq_chip);
786 tgi->regs = devm_platform_ioremap_resource(pdev, 0);
787 if (IS_ERR(tgi->regs))
788 return PTR_ERR(tgi->regs);
790 for (i = 0; i < tgi->bank_count; i++) {
791 for (j = 0; j < 4; j++) {
792 int gpio = tegra_gpio_compose(i, j, 0);
794 tegra_gpio_writel(tgi, 0x00, GPIO_INT_ENB(tgi, gpio));
798 ret = devm_gpiochip_add_data(&pdev->dev, &tgi->gc, tgi);
802 tegra_gpio_debuginit(tgi);
807 static const struct tegra_gpio_soc_config tegra20_gpio_config = {
809 .upper_offset = 0x800,
812 static const struct tegra_gpio_soc_config tegra30_gpio_config = {
813 .bank_stride = 0x100,
814 .upper_offset = 0x80,
817 static const struct tegra_gpio_soc_config tegra210_gpio_config = {
818 .debounce_supported = true,
819 .bank_stride = 0x100,
820 .upper_offset = 0x80,
823 static const struct of_device_id tegra_gpio_of_match[] = {
824 { .compatible = "nvidia,tegra210-gpio", .data = &tegra210_gpio_config },
825 { .compatible = "nvidia,tegra30-gpio", .data = &tegra30_gpio_config },
826 { .compatible = "nvidia,tegra20-gpio", .data = &tegra20_gpio_config },
829 MODULE_DEVICE_TABLE(of, tegra_gpio_of_match);
831 static struct platform_driver tegra_gpio_driver = {
833 .name = "tegra-gpio",
834 .pm = &tegra_gpio_pm_ops,
835 .of_match_table = tegra_gpio_of_match,
837 .probe = tegra_gpio_probe,
839 module_platform_driver(tegra_gpio_driver);
841 MODULE_DESCRIPTION("NVIDIA Tegra GPIO controller driver");
842 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
843 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
844 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
845 MODULE_AUTHOR("Erik Gilling <konkers@google.com>");
846 MODULE_LICENSE("GPL v2");