2 * arch/arm/mach-tegra/gpio.c
4 * Copyright (c) 2010 Google, Inc
7 * Erik Gilling <konkers@google.com>
9 * Copyright (c) 2011-2013, 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>
40 #define GPIO_BANK(x) ((x) >> 5)
41 #define GPIO_PORT(x) (((x) >> 3) & 0x3)
42 #define GPIO_BIT(x) ((x) & 0x7)
44 #define GPIO_REG(x) (GPIO_BANK(x) * tegra_gpio_bank_stride + \
47 #define GPIO_CNF(x) (GPIO_REG(x) + 0x00)
48 #define GPIO_OE(x) (GPIO_REG(x) + 0x10)
49 #define GPIO_OUT(x) (GPIO_REG(x) + 0X20)
50 #define GPIO_IN(x) (GPIO_REG(x) + 0x30)
51 #define GPIO_INT_STA(x) (GPIO_REG(x) + 0x40)
52 #define GPIO_INT_ENB(x) (GPIO_REG(x) + 0x50)
53 #define GPIO_INT_LVL(x) (GPIO_REG(x) + 0x60)
54 #define GPIO_INT_CLR(x) (GPIO_REG(x) + 0x70)
56 #define GPIO_MSK_CNF(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x00)
57 #define GPIO_MSK_OE(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x10)
58 #define GPIO_MSK_OUT(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0X20)
59 #define GPIO_MSK_INT_STA(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x40)
60 #define GPIO_MSK_INT_ENB(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x50)
61 #define GPIO_MSK_INT_LVL(x) (GPIO_REG(x) + tegra_gpio_upper_offset + 0x60)
63 #define GPIO_INT_LVL_MASK 0x010101
64 #define GPIO_INT_LVL_EDGE_RISING 0x000101
65 #define GPIO_INT_LVL_EDGE_FALLING 0x000100
66 #define GPIO_INT_LVL_EDGE_BOTH 0x010100
67 #define GPIO_INT_LVL_LEVEL_HIGH 0x000001
68 #define GPIO_INT_LVL_LEVEL_LOW 0x000000
70 struct tegra_gpio_bank {
73 spinlock_t lvl_lock[4];
74 #ifdef CONFIG_PM_SLEEP
85 static struct irq_domain *irq_domain;
86 static void __iomem *regs;
88 static u32 tegra_gpio_bank_count;
89 static u32 tegra_gpio_bank_stride;
90 static u32 tegra_gpio_upper_offset;
91 static struct tegra_gpio_bank *tegra_gpio_banks;
93 static inline void tegra_gpio_writel(u32 val, u32 reg)
95 __raw_writel(val, regs + reg);
98 static inline u32 tegra_gpio_readl(u32 reg)
100 return __raw_readl(regs + reg);
103 static int tegra_gpio_compose(int bank, int port, int bit)
105 return (bank << 5) | ((port & 0x3) << 3) | (bit & 0x7);
108 static void tegra_gpio_mask_write(u32 reg, int gpio, int value)
112 val = 0x100 << GPIO_BIT(gpio);
114 val |= 1 << GPIO_BIT(gpio);
115 tegra_gpio_writel(val, reg);
118 int tegra_gpio_get_bank_int_nr(int gpio)
123 irq = tegra_gpio_banks[bank].irq;
127 static void tegra_gpio_enable(int gpio)
129 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 1);
132 int tegra_is_gpio(int gpio)
134 return (tegra_gpio_readl(GPIO_CNF(gpio)) >> GPIO_BIT(gpio)) & 0x1;
136 EXPORT_SYMBOL(tegra_is_gpio);
139 static void tegra_gpio_disable(int gpio)
141 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 0);
144 void tegra_gpio_init_configure(unsigned gpio, bool is_input, int value)
147 tegra_gpio_mask_write(GPIO_MSK_OE(gpio), gpio, 0);
149 tegra_gpio_mask_write(GPIO_MSK_OUT(gpio), gpio, value);
150 tegra_gpio_mask_write(GPIO_MSK_OE(gpio), gpio, 1);
152 tegra_gpio_mask_write(GPIO_MSK_CNF(gpio), gpio, 1);
155 static int tegra_gpio_request(struct gpio_chip *chip, unsigned offset)
157 return pinctrl_request_gpio(offset);
160 static void tegra_gpio_free(struct gpio_chip *chip, unsigned offset)
162 pinctrl_free_gpio(offset);
163 tegra_gpio_disable(offset);
166 static void tegra_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
168 tegra_gpio_mask_write(GPIO_MSK_OUT(offset), offset, value);
171 static int tegra_gpio_get(struct gpio_chip *chip, unsigned offset)
173 /* If gpio is in output mode then read from the out value */
174 if ((tegra_gpio_readl(GPIO_OE(offset)) >> GPIO_BIT(offset)) & 1)
175 return (tegra_gpio_readl(GPIO_OUT(offset)) >>
176 GPIO_BIT(offset)) & 0x1;
178 return (tegra_gpio_readl(GPIO_IN(offset)) >> GPIO_BIT(offset)) & 0x1;
181 static int tegra_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
183 tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 0);
184 tegra_gpio_enable(offset);
188 static int tegra_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
191 tegra_gpio_set(chip, offset, value);
192 tegra_gpio_mask_write(GPIO_MSK_OE(offset), offset, 1);
193 tegra_gpio_enable(offset);
197 static int tegra_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
203 static int tegra_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
205 return irq_find_mapping(irq_domain, offset);
208 static struct gpio_chip tegra_gpio_chip = {
209 .label = "tegra-gpio",
210 .request = tegra_gpio_request,
211 .free = tegra_gpio_free,
212 .direction_input = tegra_gpio_direction_input,
213 .get = tegra_gpio_get,
214 .direction_output = tegra_gpio_direction_output,
215 .set = tegra_gpio_set,
216 .set_debounce = tegra_gpio_set_debounce,
217 .to_irq = tegra_gpio_to_irq,
221 static void tegra_gpio_irq_ack(struct irq_data *d)
225 tegra_gpio_writel(1 << GPIO_BIT(gpio), GPIO_INT_CLR(gpio));
227 /* FPGA platforms have a serializer between the GPIO
228 block and interrupt controller. Allow time for
229 clearing of the GPIO interrupt to propagate to the
230 interrupt controller before re-enabling the IRQ
231 to prevent double interrupts. */
232 if (tegra_platform_is_fpga())
236 static void tegra_gpio_irq_mask(struct irq_data *d)
240 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 0);
243 static void tegra_gpio_irq_unmask(struct irq_data *d)
247 tegra_gpio_mask_write(GPIO_MSK_INT_ENB(gpio), gpio, 1);
250 static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
253 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
254 int port = GPIO_PORT(gpio);
258 int wake = tegra_gpio_to_wake(d->hwirq);
260 switch (type & IRQ_TYPE_SENSE_MASK) {
261 case IRQ_TYPE_EDGE_RISING:
262 lvl_type = GPIO_INT_LVL_EDGE_RISING;
265 case IRQ_TYPE_EDGE_FALLING:
266 lvl_type = GPIO_INT_LVL_EDGE_FALLING;
269 case IRQ_TYPE_EDGE_BOTH:
270 lvl_type = GPIO_INT_LVL_EDGE_BOTH;
273 case IRQ_TYPE_LEVEL_HIGH:
274 lvl_type = GPIO_INT_LVL_LEVEL_HIGH;
277 case IRQ_TYPE_LEVEL_LOW:
278 lvl_type = GPIO_INT_LVL_LEVEL_LOW;
285 spin_lock_irqsave(&bank->lvl_lock[port], flags);
287 val = tegra_gpio_readl(GPIO_INT_LVL(gpio));
288 val &= ~(GPIO_INT_LVL_MASK << GPIO_BIT(gpio));
289 val |= lvl_type << GPIO_BIT(gpio);
290 tegra_gpio_writel(val, GPIO_INT_LVL(gpio));
292 spin_unlock_irqrestore(&bank->lvl_lock[port], flags);
294 tegra_gpio_mask_write(GPIO_MSK_OE(gpio), gpio, 0);
295 tegra_gpio_enable(gpio);
297 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
298 __irq_set_handler_locked(d->irq, handle_level_irq);
299 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
300 __irq_set_handler_locked(d->irq, handle_edge_irq);
302 tegra_pm_irq_set_wake_type(wake, type);
307 static void tegra_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
309 struct tegra_gpio_bank *bank;
312 struct irq_chip *chip = irq_desc_get_chip(desc);
314 chained_irq_enter(chip, desc);
316 bank = irq_get_handler_data(irq);
318 for (port = 0; port < 4; port++) {
319 int gpio = tegra_gpio_compose(bank->bank, port, 0);
320 unsigned long sta = tegra_gpio_readl(GPIO_INT_STA(gpio)) &
321 tegra_gpio_readl(GPIO_INT_ENB(gpio));
323 for_each_set_bit(pin, &sta, 8)
324 generic_handle_irq(gpio_to_irq(gpio + pin));
327 chained_irq_exit(chip, desc);
331 #ifdef CONFIG_PM_SLEEP
332 static void tegra_gpio_resume(void)
338 local_irq_save(flags);
340 for (b = 0; b < tegra_gpio_bank_count; b++) {
341 struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
343 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
344 unsigned int gpio = (b<<5) | (p<<3);
345 tegra_gpio_writel(bank->cnf[p], GPIO_CNF(gpio));
346 tegra_gpio_writel(bank->out[p], GPIO_OUT(gpio));
347 tegra_gpio_writel(bank->oe[p], GPIO_OE(gpio));
348 tegra_gpio_writel(bank->int_lvl[p], GPIO_INT_LVL(gpio));
349 tegra_gpio_writel(bank->int_enb[p], GPIO_INT_ENB(gpio));
353 local_irq_restore(flags);
356 static int tegra_gpio_suspend(void)
362 local_irq_save(flags);
363 for (b = 0; b < tegra_gpio_bank_count; b++) {
364 struct tegra_gpio_bank *bank = &tegra_gpio_banks[b];
366 for (p = 0; p < ARRAY_SIZE(bank->oe); p++) {
367 unsigned int gpio = (b<<5) | (p<<3);
368 bank->cnf[p] = tegra_gpio_readl(GPIO_CNF(gpio));
369 bank->out[p] = tegra_gpio_readl(GPIO_OUT(gpio));
370 bank->oe[p] = tegra_gpio_readl(GPIO_OE(gpio));
371 bank->int_enb[p] = tegra_gpio_readl(GPIO_INT_ENB(gpio));
372 bank->int_lvl[p] = tegra_gpio_readl(GPIO_INT_LVL(gpio));
374 /* disable gpio interrupts that are not wake sources */
375 tegra_gpio_writel(bank->wake_enb[p], GPIO_INT_ENB(gpio));
378 local_irq_restore(flags);
383 static int tegra_update_lp1_gpio_wake(struct irq_data *d, bool enable)
385 #ifdef CONFIG_PM_SLEEP
386 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
389 u8 pin_index_in_bank;
395 pin_index_in_bank = (gpio & 0x1F);
396 port_index = pin_index_in_bank >> 3;
397 pin_in_port = (pin_index_in_bank & 0x7);
398 mask = BIT(pin_in_port);
400 bank->wake_enb[port_index] |= mask;
402 bank->wake_enb[port_index] &= ~mask;
408 static int tegra_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
410 struct tegra_gpio_bank *bank = irq_data_get_irq_chip_data(d);
412 int wake = tegra_gpio_to_wake(d->hwirq);
415 * update LP1 mask for gpio port/pin interrupt
416 * LP1 enable independent of LP0 wake support
418 ret = tegra_update_lp1_gpio_wake(d, enable);
420 pr_err("Failed gpio lp1 %s for irq=%d, error=%d\n",
421 (enable ? "enable" : "disable"), d->irq, ret);
425 /* LP1 enable for bank interrupt */
427 if (bank->wake_depth++ == 0) {
428 ret = tegra_update_lp1_irq_wake(bank->irq, enable);
430 bank->wake_depth = 0;
433 if (bank->wake_depth == 0) {
434 WARN(1, "Unbalanced IRQ %d wake disable\n", bank->irq);
435 } else if (--bank->wake_depth == 0) {
436 ret = tegra_update_lp1_irq_wake(bank->irq, enable);
438 bank->wake_depth = 1;
442 pr_err("Failed gpio lp1 %s for irq=%d, error=%d\n",
443 (enable ? "enable" : "disable"), bank->irq, ret);
446 pr_err("Warning: enabling a non-LP0 wake source %lu\n",
449 ret = tegra_pm_irq_set_wake(wake, enable);
451 pr_err("Failed gpio lp0 %s for irq=%d, error=%d\n",
452 (enable ? "enable" : "disable"), d->irq, ret);
459 #define tegra_gpio_irq_set_wake NULL
460 #define tegra_update_lp1_gpio_wake NULL
463 static struct syscore_ops tegra_gpio_syscore_ops = {
464 .suspend = tegra_gpio_suspend,
465 .resume = tegra_gpio_resume,
466 .save = tegra_gpio_suspend,
467 .restore = tegra_gpio_resume,
470 static struct irq_chip tegra_gpio_irq_chip = {
472 .irq_ack = tegra_gpio_irq_ack,
473 .irq_mask = tegra_gpio_irq_mask,
474 .irq_unmask = tegra_gpio_irq_unmask,
475 .irq_set_type = tegra_gpio_irq_set_type,
476 .irq_set_wake = tegra_gpio_irq_set_wake,
477 .flags = IRQCHIP_MASK_ON_SUSPEND,
480 struct tegra_gpio_soc_config {
485 static struct tegra_gpio_soc_config tegra20_gpio_config = {
487 .upper_offset = 0x800,
490 static struct tegra_gpio_soc_config tegra30_gpio_config = {
491 .bank_stride = 0x100,
492 .upper_offset = 0x80,
495 static struct of_device_id tegra_gpio_of_match[] = {
496 { .compatible = "nvidia,tegra124-gpio", .data = &tegra30_gpio_config },
497 { .compatible = "nvidia,tegra148-gpio", .data = &tegra30_gpio_config },
498 { .compatible = "nvidia,tegra114-gpio", .data = &tegra30_gpio_config },
499 { .compatible = "nvidia,tegra30-gpio", .data = &tegra30_gpio_config },
500 { .compatible = "nvidia,tegra20-gpio", .data = &tegra20_gpio_config },
504 /* This lock class tells lockdep that GPIO irqs are in a different
505 * category than their parents, so it won't report false recursion.
507 static struct lock_class_key gpio_lock_class;
509 static int tegra_gpio_probe(struct platform_device *pdev)
511 const struct of_device_id *match;
512 struct tegra_gpio_soc_config *config;
513 struct resource *res;
514 struct tegra_gpio_bank *bank;
519 match = of_match_device(tegra_gpio_of_match, &pdev->dev);
521 dev_err(&pdev->dev, "Error: No device match found\n");
524 config = (struct tegra_gpio_soc_config *)match->data;
526 tegra_gpio_bank_stride = config->bank_stride;
527 tegra_gpio_upper_offset = config->upper_offset;
530 res = platform_get_resource(pdev, IORESOURCE_IRQ, tegra_gpio_bank_count);
533 tegra_gpio_bank_count++;
535 if (!tegra_gpio_bank_count) {
536 dev_err(&pdev->dev, "Missing IRQ resource\n");
540 tegra_gpio_chip.dev = &pdev->dev;
541 tegra_gpio_chip.ngpio = tegra_gpio_bank_count * 32;
543 tegra_gpio_banks = devm_kzalloc(&pdev->dev,
544 tegra_gpio_bank_count * sizeof(*tegra_gpio_banks),
546 if (!tegra_gpio_banks) {
547 dev_err(&pdev->dev, "Couldn't allocate bank structure\n");
551 irq_domain = irq_domain_add_linear(pdev->dev.of_node,
552 tegra_gpio_chip.ngpio,
553 &irq_domain_simple_ops, NULL);
557 for (i = 0; i < tegra_gpio_bank_count; i++) {
558 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
560 dev_err(&pdev->dev, "Missing IRQ resource\n");
564 bank = &tegra_gpio_banks[i];
566 bank->irq = res->start;
569 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
571 dev_err(&pdev->dev, "Missing MEM resource\n");
575 regs = devm_ioremap_resource(&pdev->dev, res);
577 return PTR_ERR(regs);
579 for (i = 0; i < tegra_gpio_bank_count; i++) {
580 for (j = 0; j < 4; j++) {
581 int gpio = tegra_gpio_compose(i, j, 0);
582 tegra_gpio_writel(0x00, GPIO_INT_ENB(gpio));
583 tegra_gpio_writel(0x00, GPIO_INT_STA(gpio));
587 tegra_gpio_chip.of_node = pdev->dev.of_node;
589 gpiochip_add(&tegra_gpio_chip);
591 for (gpio = 0; gpio < tegra_gpio_chip.ngpio; gpio++) {
592 int irq = irq_create_mapping(irq_domain, gpio);
593 /* No validity check; all Tegra GPIOs are valid IRQs */
595 bank = &tegra_gpio_banks[GPIO_BANK(gpio)];
597 irq_set_lockdep_class(irq, &gpio_lock_class);
598 irq_set_chip_data(irq, bank);
599 irq_set_chip_and_handler(irq, &tegra_gpio_irq_chip,
601 set_irq_flags(irq, IRQF_VALID);
604 for (i = 0; i < tegra_gpio_bank_count; i++) {
605 bank = &tegra_gpio_banks[i];
607 for (j = 0; j < 4; j++)
608 spin_lock_init(&bank->lvl_lock[j]);
610 irq_set_handler_data(bank->irq, bank);
611 irq_set_chained_handler(bank->irq, tegra_gpio_irq_handler);
618 static struct platform_driver tegra_gpio_driver = {
620 .name = "tegra-gpio",
621 .owner = THIS_MODULE,
622 .of_match_table = tegra_gpio_of_match,
624 .probe = tegra_gpio_probe,
627 static int __init tegra_gpio_init(void)
629 register_syscore_ops(&tegra_gpio_syscore_ops);
630 return platform_driver_register(&tegra_gpio_driver);
632 postcore_initcall(tegra_gpio_init);
634 #ifdef CONFIG_DEBUG_FS
636 #include <linux/debugfs.h>
637 #include <linux/seq_file.h>
639 static int dbg_gpio_show(struct seq_file *s, void *unused)
648 seq_printf(s, "Name:Bank:Port CNF OE OUT IN INT_STA INT_ENB INT_LVL\n");
649 for (i = 0; i < tegra_gpio_bank_count; i++) {
650 for (j = 0; j < 4; j++) {
651 int gpio = tegra_gpio_compose(i, j, 0);
653 "%c%c: %d:%d %02x %02x %02x %02x %02x %02x %06x\n",
655 tegra_gpio_readl(GPIO_CNF(gpio)),
656 tegra_gpio_readl(GPIO_OE(gpio)),
657 tegra_gpio_readl(GPIO_OUT(gpio)),
658 tegra_gpio_readl(GPIO_IN(gpio)),
659 tegra_gpio_readl(GPIO_INT_STA(gpio)),
660 tegra_gpio_readl(GPIO_INT_ENB(gpio)),
661 tegra_gpio_readl(GPIO_INT_LVL(gpio)));
676 static int dbg_gpio_open(struct inode *inode, struct file *file)
678 return single_open(file, dbg_gpio_show, &inode->i_private);
681 static const struct file_operations debug_fops = {
682 .open = dbg_gpio_open,
685 .release = single_release,
688 static int __init tegra_gpio_debuginit(void)
690 (void) debugfs_create_file("tegra_gpio", S_IRUGO,
691 NULL, NULL, &debug_fops);
694 late_initcall(tegra_gpio_debuginit);