2 * arch/arm/mach-tegra/gpio.c
4 * Copyright (c) 2010 Google, Inc
7 * Erik Gilling <konkers@google.com>
9 * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
11 * This software is licensed under the terms of the GNU General Public
12 * License version 2, as published by the Free Software Foundation, and
13 * may be copied, distributed, and modified under those terms.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
22 #include <linux/err.h>
23 #include <linux/init.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
27 #include <linux/gpio.h>
28 #include <linux/of_device.h>
29 #include <linux/platform_device.h>
30 #include <linux/module.h>
31 #include <linux/delay.h>
32 #include <linux/irqdomain.h>
33 #include <linux/irqchip/chained_irq.h>
34 #include <linux/pinctrl/consumer.h>
36 #include <linux/syscore_ops.h>
37 #include <linux/tegra-soc.h>
38 #include <linux/irqchip/tegra.h>
39 #include <linux/tegra-pm.h>
41 #define GPIO_BANK(x) ((x) >> 5)
42 #define GPIO_PORT(x) (((x) >> 3) & 0x3)
43 #define GPIO_BIT(x) ((x) & 0x7)
45 #define GPIO_REG(x) (GPIO_BANK(x) * tegra_gpio_bank_stride + \
48 #define GPIO_CNF(x) (GPIO_REG(x) + 0x00)
49 #define GPIO_OE(x) (GPIO_REG(x) + 0x10)
50 #define GPIO_OUT(x) (GPIO_REG(x) + 0X20)
51 #define GPIO_IN(x) (GPIO_REG(x) + 0x30)
52 #define GPIO_INT_STA(x) (GPIO_REG(x) + 0x40)
53 #define GPIO_INT_ENB(x) (GPIO_REG(x) + 0x50)
54 #define GPIO_INT_LVL(x) (GPIO_REG(x) + 0x60)
55 #define GPIO_INT_CLR(x) (GPIO_REG(x) + 0x70)
57 #define GPIO_MSK_CNF(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x00)
58 #define GPIO_MSK_OE(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x10)
59 #define GPIO_MSK_OUT(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0X20)
60 #define GPIO_MSK_INT_STA(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x40)
61 #define GPIO_MSK_INT_ENB(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x50)
62 #define GPIO_MSK_INT_LVL(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x60)
64 #define GPIO_INT_LVL_MASK 0x010101
65 #define GPIO_INT_LVL_EDGE_RISING 0x000101
66 #define GPIO_INT_LVL_EDGE_FALLING 0x000100
67 #define GPIO_INT_LVL_EDGE_BOTH 0x010100
68 #define GPIO_INT_LVL_LEVEL_HIGH 0x000001
69 #define GPIO_INT_LVL_LEVEL_LOW 0x000000
71 struct tegra_gpio_bank {
74 spinlock_t lvl_lock[4];
75 #ifdef CONFIG_PM_SLEEP
87 static struct irq_domain *irq_domain;
88 static void __iomem *regs;
90 static u32 tegra_gpio_bank_count;
91 static u32 tegra_gpio_bank_stride;
92 static u32 tegra_gpio_upper_offset;
93 static struct tegra_gpio_bank *tegra_gpio_banks;
95 static inline void tegra_gpio_writel(u32 val, u32 reg)
97 __raw_writel(val, regs + reg);
100 static inline u32 tegra_gpio_readl(u32 reg)
102 return __raw_readl(regs + reg);
105 static int tegra_gpio_compose(int bank, int port, int bit)
107 return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
110 static void tegra_gpio_mask_write(u32 reg, int gpio, int value)
114 val = 0x100 << GPIO_BIT(gpio);
116 val |= 1 << GPIO_BIT(gpio);
117 tegra_gpio_writel(val, reg);
120 int tegra_gpio_get_bank_int_nr(int gpio)
125 irq = tegra_gpio_banks[bank].irq;
129 static void tegra_gpio_enable(int gpio)
131 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 1);
134 int tegra_is_gpio(int gpio)
136 return (tegra_gpio_readl(GPIO_CNF(gpio)) >> GPIO_BIT(gpio)) & 0x1;
138 EXPORT_SYMBOL(tegra_is_gpio);
141 static void tegra_gpio_disable(int gpio)
143 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 0);
146 void tegra_gpio_init_configure(unsigned gpio, bool is_input, int value)
149 tegra_gpio_mask_write(GPIO_MSK_OE(gpio), gpio, 0);
151 tegra_gpio_mask_write(GPIO_MSK_OUT(gpio), gpio, value);
152 tegra_gpio_mask_write(GPIO_MSK_OE(gpio), gpio, 1);
154 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 1);
157 static int tegra_gpio_request(struct gpio_chip *chip, unsigned offset)
160 return pinctrl_request_gpio(offset);
166 static void tegra_gpio_free(struct gpio_chip *chip, unsigned offset)
169 pinctrl_free_gpio(offset);
171 tegra_gpio_disable(offset);
174 static void tegra_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
176 tegra_gpio_mask_write(GPIO_MSK_OUT(offset), offset, value);
179 static int tegra_gpio_get(struct gpio_chip *chip, unsigned offset)
181 /* If gpio is in output mode then read from the out value */
182 if ((tegra_gpio_readl(GPIO_OE(offset)) >> GPIO_BIT(offset)) & 1)
183 return (tegra_gpio_readl(GPIO_OUT(offset)) >>
184 GPIO_BIT(offset)) & 0x1;
186 return (tegra_gpio_readl(GPIO_IN(offset)) >> GPIO_BIT(offset)) & 0x1;
189 static int tegra_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
191 tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 0);
192 tegra_gpio_enable(offset);
196 static int tegra_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
199 tegra_gpio_set(chip, offset, value);
200 tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 1);
201 tegra_gpio_enable(offset);
205 static int tegra_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
211 static int tegra_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
213 return irq_find_mapping(irq_domain, offset);
216 static struct gpio_chip tegra_gpio_chip = {
217 .label = "tegra-gpio",
218 .request = tegra_gpio_request,
219 .free = tegra_gpio_free,
220 .direction_input = tegra_gpio_direction_input,
221 .get = tegra_gpio_get,
222 .direction_output = tegra_gpio_direction_output,
223 .set = tegra_gpio_set,
224 .set_debounce = tegra_gpio_set_debounce,
225 .to_irq = tegra_gpio_to_irq,
229 static void tegra_gpio_irq_ack(struct irq_data *d)
233 tegra_gpio_writel(1 << GPIO_BIT(gpio), GPIO_INT_CLR(gpio));
235 /* FPGA platforms have a serializer between the GPIO
236 block and interrupt controller. Allow time for
237 clearing of the GPIO interrupt to propagate to the
238 interrupt controller before re-enabling the IRQ
239 to prevent double interrupts. */
240 if (tegra_platform_is_fpga())
244 static void tegra_gpio_irq_mask(struct irq_data *d)
248 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 0);
251 static void tegra_gpio_irq_unmask(struct irq_data *d)
255 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 1);
258 static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
261 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
262 int port = GPIO_PORT(gpio);
266 int wake = tegra_gpio_to_wake(d->hwirq);
268 switch (type & IRQ_TYPE_SENSE_MASK) {
269 case IRQ_TYPE_EDGE_RISING:
270 lvl_type = GPIO_INT_LVL_EDGE_RISING;
273 case IRQ_TYPE_EDGE_FALLING:
274 lvl_type = GPIO_INT_LVL_EDGE_FALLING;
277 case IRQ_TYPE_EDGE_BOTH:
278 lvl_type = GPIO_INT_LVL_EDGE_BOTH;
281 case IRQ_TYPE_LEVEL_HIGH:
282 lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
285 case IRQ_TYPE_LEVEL_LOW:
286 lvl_type = GPIO_INT_LVL_LEVEL_LOW;
293 spin_lock_irqsave(&bank->lvl_lock[port], flags);
295 val = tegra_gpio_readl(GPIO_INT_LVL(gpio));
296 val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
297 val |= lvl_type << GPIO_BIT(gpio);
298 tegra_gpio_writel(val, GPIO_INT_LVL(gpio));
300 spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
302 tegra_gpio_mask_write(GPIO_MSK_OE(gpio), gpio, 0);
303 tegra_gpio_enable(gpio);
305 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
306 __irq_set_handler_locked(d->irq, handle_level_irq);
307 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
308 __irq_set_handler_locked(d->irq, handle_edge_irq);
310 tegra_pm_irq_set_wake_type(wake, type);
315 static void tegra_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
317 struct tegra_gpio_bank *bank;
320 struct irq_chip *chip = irq_desc_get_chip(desc);
322 chained_irq_enter(chip, desc);
324 bank = irq_get_handler_data(irq);
326 for (port = 0; port < 4; port++) {
327 int gpio = tegra_gpio_compose(bank->bank, port, 0);
328 unsigned long sta = tegra_gpio_readl(GPIO_INT_STA(gpio)) &
329 tegra_gpio_readl(GPIO_INT_ENB(gpio));
331 for_each_set_bit(pin, &sta, 8)
332 generic_handle_irq(gpio_to_irq(gpio + pin));
335 chained_irq_exit(chip, desc);
339 #ifdef CONFIG_PM_SLEEP
340 static void tegra_gpio_resume(void)
346 local_irq_save(flags);
348 for (b = 0; b < tegra_gpio_bank_count; b++) {
349 struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
351 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
352 unsigned int gpio = (b<<5) | (p<<3);
353 tegra_gpio_writel(bank->cnf[p], GPIO_CNF(gpio));
354 tegra_gpio_writel(bank->out[p], GPIO_OUT(gpio));
355 tegra_gpio_writel(bank->oe[p], GPIO_OE(gpio));
356 tegra_gpio_writel(bank->int_lvl[p], GPIO_INT_LVL(gpio));
357 tegra_gpio_writel(bank->int_enb[p], GPIO_INT_ENB(gpio));
361 local_irq_restore(flags);
364 static int tegra_gpio_suspend(void)
370 local_irq_save(flags);
371 for (b = 0; b < tegra_gpio_bank_count; b++) {
372 struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
374 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
375 unsigned int gpio = (b<<5) | (p<<3);
376 unsigned int wake_enb;
377 bank->cnf[p] = tegra_gpio_readl(GPIO_CNF(gpio));
378 bank->out[p] = tegra_gpio_readl(GPIO_OUT(gpio));
379 bank->oe[p] = tegra_gpio_readl(GPIO_OE(gpio));
380 bank->int_enb[p] = tegra_gpio_readl(GPIO_INT_ENB(gpio));
381 bank->int_lvl[p] = tegra_gpio_readl(GPIO_INT_LVL(gpio));
383 /* disable gpio interrupts that are not wake sources */
384 wake_enb = (current_suspend_mode == TEGRA_SUSPEND_LP0) ?
385 (bank->wake_enb[p] & bank->wake_lp0_cap[p]) :
387 tegra_gpio_writel(wake_enb, GPIO_INT_ENB(gpio));
390 local_irq_restore(flags);
395 static int tegra_update_lp1_gpio_wake(struct irq_data *d, bool enable, int wake)
397 #ifdef CONFIG_PM_SLEEP
398 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
401 u8 pin_index_in_bank;
407 pin_index_in_bank = (gpio & 0x1F);
408 port_index = pin_index_in_bank >> 3;
409 pin_in_port = (pin_index_in_bank & 0x7);
410 mask = BIT(pin_in_port);
412 bank->wake_enb[port_index] |= mask;
414 bank->wake_enb[port_index] &= ~mask;
416 /* Enable GPIO interrupt in Lp0 when GPIO is a Lp0 wake up source */
418 bank->wake_lp0_cap[port_index] |= mask;
424 static int tegra_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
426 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
428 int wake = tegra_gpio_to_wake(d->hwirq);
431 * update LP1 mask for gpio port/pin interrupt
432 * LP1 enable independent of LP0 wake support
434 ret = tegra_update_lp1_gpio_wake(d, enable, wake);
436 pr_err("Failed gpio lp1 %s for irq=%d, error=%d\n",
437 (enable ? "enable" : "disable"), d->irq, ret);
441 /* LP1 enable for bank interrupt */
443 if (bank->wake_depth++ == 0) {
444 ret = tegra_update_lp1_irq_wake(bank->irq, enable);
446 bank->wake_depth = 0;
449 if (bank->wake_depth == 0) {
450 WARN(1, "Unbalanced IRQ %d wake disable\n", bank->irq);
451 } else if (--bank->wake_depth == 0) {
452 ret = tegra_update_lp1_irq_wake(bank->irq, enable);
454 bank->wake_depth = 1;
458 pr_err("Failed gpio lp1 %s for irq=%d, error=%d\n",
459 (enable ? "enable" : "disable"), bank->irq, ret);
462 pr_err("Warning: enabling a non-LP0 wake source %lu\n",
465 ret = tegra_pm_irq_set_wake(wake, enable);
467 pr_err("Failed gpio lp0 %s for irq=%d, error=%d\n",
468 (enable ? "enable" : "disable"), d->irq, ret);
475 #define tegra_gpio_irq_set_wake NULL
476 #define tegra_update_lp1_gpio_wake NULL
479 static struct syscore_ops tegra_gpio_syscore_ops = {
480 .suspend = tegra_gpio_suspend,
481 .resume = tegra_gpio_resume,
482 .save = tegra_gpio_suspend,
483 .restore = tegra_gpio_resume,
486 static struct irq_chip tegra_gpio_irq_chip = {
488 .irq_ack = tegra_gpio_irq_ack,
489 .irq_mask = tegra_gpio_irq_mask,
490 .irq_unmask = tegra_gpio_irq_unmask,
491 .irq_set_type = tegra_gpio_irq_set_type,
492 .irq_set_wake = tegra_gpio_irq_set_wake,
493 .flags = IRQCHIP_MASK_ON_SUSPEND,
496 struct tegra_gpio_soc_config {
501 static struct tegra_gpio_soc_config tegra20_gpio_config = {
503 .upper_offset = 0x800,
506 static struct tegra_gpio_soc_config tegra30_gpio_config = {
507 .bank_stride = 0x100,
508 .upper_offset = 0x80,
511 static struct of_device_id tegra_gpio_of_match[] = {
512 { .compatible = "nvidia,tegra124-gpio", .data = &tegra30_gpio_config },
513 { .compatible = "nvidia,tegra148-gpio", .data = &tegra30_gpio_config },
514 { .compatible = "nvidia,tegra114-gpio", .data = &tegra30_gpio_config },
515 { .compatible = "nvidia,tegra30-gpio", .data = &tegra30_gpio_config },
516 { .compatible = "nvidia,tegra20-gpio", .data = &tegra20_gpio_config },
520 /* This lock class tells lockdep that GPIO irqs are in a different
521 * category than their parents, so it won't report false recursion.
523 static struct lock_class_key gpio_lock_class;
525 static int tegra_gpio_probe(struct platform_device *pdev)
527 const struct of_device_id *match;
528 struct tegra_gpio_soc_config *config;
529 struct resource *res;
530 struct tegra_gpio_bank *bank;
535 match = of_match_device(tegra_gpio_of_match, &pdev->dev);
537 dev_err(&pdev->dev, "Error: No device match found\n");
540 config = (struct tegra_gpio_soc_config *)match->data;
542 tegra_gpio_bank_stride = config->bank_stride;
543 tegra_gpio_upper_offset = config->upper_offset;
546 res = platform_get_resource(pdev, IORESOURCE_IRQ, tegra_gpio_bank_count);
549 tegra_gpio_bank_count++;
551 if (!tegra_gpio_bank_count) {
552 dev_err(&pdev->dev, "Missing IRQ resource\n");
556 tegra_gpio_chip.dev = &pdev->dev;
557 tegra_gpio_chip.ngpio = tegra_gpio_bank_count * 32;
559 tegra_gpio_banks = devm_kzalloc(&pdev->dev,
560 tegra_gpio_bank_count * sizeof(*tegra_gpio_banks),
562 if (!tegra_gpio_banks) {
563 dev_err(&pdev->dev, "Couldn't allocate bank structure\n");
567 irq_domain = irq_domain_add_linear(pdev->dev.of_node,
568 tegra_gpio_chip.ngpio,
569 &irq_domain_simple_ops, NULL);
573 for (i = 0; i < tegra_gpio_bank_count; i++) {
574 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
576 dev_err(&pdev->dev, "Missing IRQ resource\n");
580 bank = &tegra_gpio_banks[i];
582 bank->irq = res->start;
585 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
587 dev_err(&pdev->dev, "Missing MEM resource\n");
591 regs = devm_ioremap_resource(&pdev->dev, res);
593 return PTR_ERR(regs);
595 for (i = 0; i < tegra_gpio_bank_count; i++) {
596 for (j = 0; j < 4; j++) {
597 int gpio = tegra_gpio_compose(i, j, 0);
598 tegra_gpio_writel(0x00, GPIO_INT_ENB(gpio));
599 tegra_gpio_writel(0x00, GPIO_INT_STA(gpio));
603 tegra_gpio_chip.of_node = pdev->dev.of_node;
605 gpiochip_add(&tegra_gpio_chip);
607 for (gpio = 0; gpio < tegra_gpio_chip.ngpio; gpio++) {
608 int irq = irq_create_mapping(irq_domain, gpio);
609 /* No validity check; all Tegra GPIOs are valid IRQs */
611 bank = &tegra_gpio_banks[GPIO_BANK(gpio)];
613 irq_set_lockdep_class(irq, &gpio_lock_class);
614 irq_set_chip_data(irq, bank);
615 irq_set_chip_and_handler(irq, &tegra_gpio_irq_chip,
617 set_irq_flags(irq, IRQF_VALID);
620 for (i = 0; i < tegra_gpio_bank_count; i++) {
621 bank = &tegra_gpio_banks[i];
623 for (j = 0; j < 4; j++)
624 spin_lock_init(&bank->lvl_lock[j]);
626 irq_set_handler_data(bank->irq, bank);
627 irq_set_chained_handler(bank->irq, tegra_gpio_irq_handler);
634 static struct platform_driver tegra_gpio_driver = {
636 .name = "tegra-gpio",
637 .owner = THIS_MODULE,
638 .of_match_table = tegra_gpio_of_match,
640 .probe = tegra_gpio_probe,
643 static int __init tegra_gpio_init(void)
645 register_syscore_ops(&tegra_gpio_syscore_ops);
646 return platform_driver_register(&tegra_gpio_driver);
648 postcore_initcall(tegra_gpio_init);
650 #ifdef CONFIG_DEBUG_FS
652 #include <linux/debugfs.h>
653 #include <linux/seq_file.h>
655 static int dbg_gpio_show(struct seq_file *s, void *unused)
664 seq_printf(s, "Name:Bank:Port CNF OE OUT IN INT_STA INT_ENB INT_LVL\n");
665 for (i = 0; i < tegra_gpio_bank_count; i++) {
666 for (j = 0; j < 4; j++) {
667 int gpio = tegra_gpio_compose(i, j, 0);
669 "%c%c: %d:%d %02x %02x %02x %02x %02x %02x %06x\n",
671 tegra_gpio_readl(GPIO_CNF(gpio)),
672 tegra_gpio_readl(GPIO_OE(gpio)),
673 tegra_gpio_readl(GPIO_OUT(gpio)),
674 tegra_gpio_readl(GPIO_IN(gpio)),
675 tegra_gpio_readl(GPIO_INT_STA(gpio)),
676 tegra_gpio_readl(GPIO_INT_ENB(gpio)),
677 tegra_gpio_readl(GPIO_INT_LVL(gpio)));
692 static int dbg_gpio_open(struct inode *inode, struct file *file)
694 return single_open(file, dbg_gpio_show, &inode->i_private);
697 static const struct file_operations debug_fops = {
698 .open = dbg_gpio_open,
701 .release = single_release,
704 static int __init tegra_gpio_debuginit(void)
706 (void) debugfs_create_file("tegra_gpio", S_IRUGO,
707 NULL, NULL, &debug_fops);
710 late_initcall(tegra_gpio_debuginit);