2 * linux/arch/arm/plat-omap/gpio.c
4 * Support functions for OMAP GPIO
6 * Copyright (C) 2003-2005 Nokia Corporation
7 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/config.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/sched.h>
18 #include <linux/interrupt.h>
19 #include <linux/ptrace.h>
20 #include <linux/sysdev.h>
21 #include <linux/err.h>
22 #include <linux/clk.h>
24 #include <asm/hardware.h>
26 #include <asm/arch/irqs.h>
27 #include <asm/arch/gpio.h>
28 #include <asm/mach/irq.h>
33 * OMAP1510 GPIO registers
35 #define OMAP1510_GPIO_BASE (void __iomem *)0xfffce000
36 #define OMAP1510_GPIO_DATA_INPUT 0x00
37 #define OMAP1510_GPIO_DATA_OUTPUT 0x04
38 #define OMAP1510_GPIO_DIR_CONTROL 0x08
39 #define OMAP1510_GPIO_INT_CONTROL 0x0c
40 #define OMAP1510_GPIO_INT_MASK 0x10
41 #define OMAP1510_GPIO_INT_STATUS 0x14
42 #define OMAP1510_GPIO_PIN_CONTROL 0x18
44 #define OMAP1510_IH_GPIO_BASE 64
47 * OMAP1610 specific GPIO registers
49 #define OMAP1610_GPIO1_BASE (void __iomem *)0xfffbe400
50 #define OMAP1610_GPIO2_BASE (void __iomem *)0xfffbec00
51 #define OMAP1610_GPIO3_BASE (void __iomem *)0xfffbb400
52 #define OMAP1610_GPIO4_BASE (void __iomem *)0xfffbbc00
53 #define OMAP1610_GPIO_REVISION 0x0000
54 #define OMAP1610_GPIO_SYSCONFIG 0x0010
55 #define OMAP1610_GPIO_SYSSTATUS 0x0014
56 #define OMAP1610_GPIO_IRQSTATUS1 0x0018
57 #define OMAP1610_GPIO_IRQENABLE1 0x001c
58 #define OMAP1610_GPIO_WAKEUPENABLE 0x0028
59 #define OMAP1610_GPIO_DATAIN 0x002c
60 #define OMAP1610_GPIO_DATAOUT 0x0030
61 #define OMAP1610_GPIO_DIRECTION 0x0034
62 #define OMAP1610_GPIO_EDGE_CTRL1 0x0038
63 #define OMAP1610_GPIO_EDGE_CTRL2 0x003c
64 #define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
65 #define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
66 #define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
67 #define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
68 #define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
69 #define OMAP1610_GPIO_SET_DATAOUT 0x00f0
72 * OMAP730 specific GPIO registers
74 #define OMAP730_GPIO1_BASE (void __iomem *)0xfffbc000
75 #define OMAP730_GPIO2_BASE (void __iomem *)0xfffbc800
76 #define OMAP730_GPIO3_BASE (void __iomem *)0xfffbd000
77 #define OMAP730_GPIO4_BASE (void __iomem *)0xfffbd800
78 #define OMAP730_GPIO5_BASE (void __iomem *)0xfffbe000
79 #define OMAP730_GPIO6_BASE (void __iomem *)0xfffbe800
80 #define OMAP730_GPIO_DATA_INPUT 0x00
81 #define OMAP730_GPIO_DATA_OUTPUT 0x04
82 #define OMAP730_GPIO_DIR_CONTROL 0x08
83 #define OMAP730_GPIO_INT_CONTROL 0x0c
84 #define OMAP730_GPIO_INT_MASK 0x10
85 #define OMAP730_GPIO_INT_STATUS 0x14
88 * omap24xx specific GPIO registers
90 #define OMAP24XX_GPIO1_BASE (void __iomem *)0x48018000
91 #define OMAP24XX_GPIO2_BASE (void __iomem *)0x4801a000
92 #define OMAP24XX_GPIO3_BASE (void __iomem *)0x4801c000
93 #define OMAP24XX_GPIO4_BASE (void __iomem *)0x4801e000
94 #define OMAP24XX_GPIO_REVISION 0x0000
95 #define OMAP24XX_GPIO_SYSCONFIG 0x0010
96 #define OMAP24XX_GPIO_SYSSTATUS 0x0014
97 #define OMAP24XX_GPIO_IRQSTATUS1 0x0018
98 #define OMAP24XX_GPIO_IRQENABLE1 0x001c
99 #define OMAP24XX_GPIO_CTRL 0x0030
100 #define OMAP24XX_GPIO_OE 0x0034
101 #define OMAP24XX_GPIO_DATAIN 0x0038
102 #define OMAP24XX_GPIO_DATAOUT 0x003c
103 #define OMAP24XX_GPIO_LEVELDETECT0 0x0040
104 #define OMAP24XX_GPIO_LEVELDETECT1 0x0044
105 #define OMAP24XX_GPIO_RISINGDETECT 0x0048
106 #define OMAP24XX_GPIO_FALLINGDETECT 0x004c
107 #define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
108 #define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
109 #define OMAP24XX_GPIO_CLEARWKUENA 0x0080
110 #define OMAP24XX_GPIO_SETWKUENA 0x0084
111 #define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
112 #define OMAP24XX_GPIO_SETDATAOUT 0x0094
114 #define OMAP_MPUIO_MASK (~OMAP_MAX_GPIO_LINES & 0xff)
119 u16 virtual_irq_start;
127 #define METHOD_MPUIO 0
128 #define METHOD_GPIO_1510 1
129 #define METHOD_GPIO_1610 2
130 #define METHOD_GPIO_730 3
131 #define METHOD_GPIO_24XX 4
133 #ifdef CONFIG_ARCH_OMAP16XX
134 static struct gpio_bank gpio_bank_1610[5] = {
135 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
136 { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
137 { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
138 { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
139 { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
143 #ifdef CONFIG_ARCH_OMAP15XX
144 static struct gpio_bank gpio_bank_1510[2] = {
145 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
146 { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
150 #ifdef CONFIG_ARCH_OMAP730
151 static struct gpio_bank gpio_bank_730[7] = {
152 { OMAP_MPUIO_BASE, INT_730_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
153 { OMAP730_GPIO1_BASE, INT_730_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_730 },
154 { OMAP730_GPIO2_BASE, INT_730_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_730 },
155 { OMAP730_GPIO3_BASE, INT_730_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_730 },
156 { OMAP730_GPIO4_BASE, INT_730_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_730 },
157 { OMAP730_GPIO5_BASE, INT_730_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_730 },
158 { OMAP730_GPIO6_BASE, INT_730_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_730 },
162 #ifdef CONFIG_ARCH_OMAP24XX
163 static struct gpio_bank gpio_bank_24xx[4] = {
164 { OMAP24XX_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
165 { OMAP24XX_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
166 { OMAP24XX_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
167 { OMAP24XX_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
171 static struct gpio_bank *gpio_bank;
172 static int gpio_bank_count;
174 static inline struct gpio_bank *get_gpio_bank(int gpio)
176 #ifdef CONFIG_ARCH_OMAP15XX
177 if (cpu_is_omap1510()) {
178 if (OMAP_GPIO_IS_MPUIO(gpio))
179 return &gpio_bank[0];
180 return &gpio_bank[1];
183 #if defined(CONFIG_ARCH_OMAP16XX)
184 if (cpu_is_omap16xx()) {
185 if (OMAP_GPIO_IS_MPUIO(gpio))
186 return &gpio_bank[0];
187 return &gpio_bank[1 + (gpio >> 4)];
190 #ifdef CONFIG_ARCH_OMAP730
191 if (cpu_is_omap730()) {
192 if (OMAP_GPIO_IS_MPUIO(gpio))
193 return &gpio_bank[0];
194 return &gpio_bank[1 + (gpio >> 5)];
197 #ifdef CONFIG_ARCH_OMAP24XX
198 if (cpu_is_omap24xx())
199 return &gpio_bank[gpio >> 5];
203 static inline int get_gpio_index(int gpio)
205 #ifdef CONFIG_ARCH_OMAP730
206 if (cpu_is_omap730())
209 #ifdef CONFIG_ARCH_OMAP24XX
210 if (cpu_is_omap24xx())
216 static inline int gpio_valid(int gpio)
220 if (OMAP_GPIO_IS_MPUIO(gpio)) {
221 if ((gpio & OMAP_MPUIO_MASK) > 16)
225 #ifdef CONFIG_ARCH_OMAP15XX
226 if (cpu_is_omap1510() && gpio < 16)
229 #if defined(CONFIG_ARCH_OMAP16XX)
230 if ((cpu_is_omap16xx()) && gpio < 64)
233 #ifdef CONFIG_ARCH_OMAP730
234 if (cpu_is_omap730() && gpio < 192)
237 #ifdef CONFIG_ARCH_OMAP24XX
238 if (cpu_is_omap24xx() && gpio < 128)
244 static int check_gpio(int gpio)
246 if (unlikely(gpio_valid(gpio)) < 0) {
247 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
254 static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
256 void __iomem *reg = bank->base;
259 switch (bank->method) {
261 reg += OMAP_MPUIO_IO_CNTL;
263 case METHOD_GPIO_1510:
264 reg += OMAP1510_GPIO_DIR_CONTROL;
266 case METHOD_GPIO_1610:
267 reg += OMAP1610_GPIO_DIRECTION;
269 case METHOD_GPIO_730:
270 reg += OMAP730_GPIO_DIR_CONTROL;
272 case METHOD_GPIO_24XX:
273 reg += OMAP24XX_GPIO_OE;
276 l = __raw_readl(reg);
281 __raw_writel(l, reg);
284 void omap_set_gpio_direction(int gpio, int is_input)
286 struct gpio_bank *bank;
288 if (check_gpio(gpio) < 0)
290 bank = get_gpio_bank(gpio);
291 spin_lock(&bank->lock);
292 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
293 spin_unlock(&bank->lock);
296 static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
298 void __iomem *reg = bank->base;
301 switch (bank->method) {
303 reg += OMAP_MPUIO_OUTPUT;
304 l = __raw_readl(reg);
310 case METHOD_GPIO_1510:
311 reg += OMAP1510_GPIO_DATA_OUTPUT;
312 l = __raw_readl(reg);
318 case METHOD_GPIO_1610:
320 reg += OMAP1610_GPIO_SET_DATAOUT;
322 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
325 case METHOD_GPIO_730:
326 reg += OMAP730_GPIO_DATA_OUTPUT;
327 l = __raw_readl(reg);
333 case METHOD_GPIO_24XX:
335 reg += OMAP24XX_GPIO_SETDATAOUT;
337 reg += OMAP24XX_GPIO_CLEARDATAOUT;
344 __raw_writel(l, reg);
347 void omap_set_gpio_dataout(int gpio, int enable)
349 struct gpio_bank *bank;
351 if (check_gpio(gpio) < 0)
353 bank = get_gpio_bank(gpio);
354 spin_lock(&bank->lock);
355 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
356 spin_unlock(&bank->lock);
359 int omap_get_gpio_datain(int gpio)
361 struct gpio_bank *bank;
364 if (check_gpio(gpio) < 0)
366 bank = get_gpio_bank(gpio);
368 switch (bank->method) {
370 reg += OMAP_MPUIO_INPUT_LATCH;
372 case METHOD_GPIO_1510:
373 reg += OMAP1510_GPIO_DATA_INPUT;
375 case METHOD_GPIO_1610:
376 reg += OMAP1610_GPIO_DATAIN;
378 case METHOD_GPIO_730:
379 reg += OMAP730_GPIO_DATA_INPUT;
381 case METHOD_GPIO_24XX:
382 reg += OMAP24XX_GPIO_DATAIN;
388 return (__raw_readl(reg)
389 & (1 << get_gpio_index(gpio))) != 0;
392 #define MOD_REG_BIT(reg, bit_mask, set) \
394 int l = __raw_readl(base + reg); \
395 if (set) l |= bit_mask; \
396 else l &= ~bit_mask; \
397 __raw_writel(l, base + reg); \
400 static inline void set_24xx_gpio_triggering(void __iomem *base, int gpio, int trigger)
402 u32 gpio_bit = 1 << gpio;
404 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
406 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
407 trigger & IRQT_HIGH);
408 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
409 trigger & IRQT_RISING);
410 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
411 trigger & IRQT_FALLING);
412 /* FIXME: Possibly do 'set_irq_handler(j, do_level_IRQ)' if only level
413 * triggering requested. */
416 static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
418 void __iomem *reg = bank->base;
421 switch (bank->method) {
423 reg += OMAP_MPUIO_GPIO_INT_EDGE;
424 l = __raw_readl(reg);
425 if (trigger == IRQT_RISING)
427 else if (trigger == IRQT_FALLING)
432 case METHOD_GPIO_1510:
433 reg += OMAP1510_GPIO_INT_CONTROL;
434 l = __raw_readl(reg);
435 if (trigger == IRQT_RISING)
437 else if (trigger == IRQT_FALLING)
442 case METHOD_GPIO_1610:
444 reg += OMAP1610_GPIO_EDGE_CTRL2;
446 reg += OMAP1610_GPIO_EDGE_CTRL1;
448 /* We allow only edge triggering, i.e. two lowest bits */
449 if (trigger & ~IRQT_BOTHEDGE)
451 /* NOTE: knows __IRQT_{FAL,RIS}EDGE match OMAP hardware */
453 l = __raw_readl(reg);
454 l &= ~(3 << (gpio << 1));
455 l |= trigger << (gpio << 1);
457 case METHOD_GPIO_730:
458 reg += OMAP730_GPIO_INT_CONTROL;
459 l = __raw_readl(reg);
460 if (trigger == IRQT_RISING)
462 else if (trigger == IRQT_FALLING)
467 case METHOD_GPIO_24XX:
468 set_24xx_gpio_triggering(reg, gpio, trigger);
474 __raw_writel(l, reg);
480 static int gpio_irq_type(unsigned irq, unsigned type)
482 struct gpio_bank *bank;
486 if (irq > IH_MPUIO_BASE)
487 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
489 gpio = irq - IH_GPIO_BASE;
491 if (check_gpio(gpio) < 0)
494 if (type & (__IRQT_LOWLVL|__IRQT_HIGHLVL|IRQT_PROBE))
497 bank = get_gpio_bank(gpio);
498 spin_lock(&bank->lock);
499 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
500 spin_unlock(&bank->lock);
504 static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
506 void __iomem *reg = bank->base;
508 switch (bank->method) {
510 /* MPUIO irqstatus is reset by reading the status register,
511 * so do nothing here */
513 case METHOD_GPIO_1510:
514 reg += OMAP1510_GPIO_INT_STATUS;
516 case METHOD_GPIO_1610:
517 reg += OMAP1610_GPIO_IRQSTATUS1;
519 case METHOD_GPIO_730:
520 reg += OMAP730_GPIO_INT_STATUS;
522 case METHOD_GPIO_24XX:
523 reg += OMAP24XX_GPIO_IRQSTATUS1;
529 __raw_writel(gpio_mask, reg);
532 static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
534 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
537 static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
539 void __iomem *reg = bank->base;
542 switch (bank->method) {
544 reg += OMAP_MPUIO_GPIO_MASKIT;
545 l = __raw_readl(reg);
551 case METHOD_GPIO_1510:
552 reg += OMAP1510_GPIO_INT_MASK;
553 l = __raw_readl(reg);
559 case METHOD_GPIO_1610:
561 reg += OMAP1610_GPIO_SET_IRQENABLE1;
563 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
566 case METHOD_GPIO_730:
567 reg += OMAP730_GPIO_INT_MASK;
568 l = __raw_readl(reg);
574 case METHOD_GPIO_24XX:
576 reg += OMAP24XX_GPIO_SETIRQENABLE1;
578 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
585 __raw_writel(l, reg);
588 static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
590 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
594 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
595 * 1510 does not seem to have a wake-up register. If JTAG is connected
596 * to the target, system will wake up always on GPIO events. While
597 * system is running all registered GPIO interrupts need to have wake-up
598 * enabled. When system is suspended, only selected GPIO interrupts need
599 * to have wake-up enabled.
601 static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
603 switch (bank->method) {
604 case METHOD_GPIO_1610:
605 case METHOD_GPIO_24XX:
606 spin_lock(&bank->lock);
608 bank->suspend_wakeup |= (1 << gpio);
610 bank->suspend_wakeup &= ~(1 << gpio);
611 spin_unlock(&bank->lock);
614 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
620 /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
621 static int gpio_wake_enable(unsigned int irq, unsigned int enable)
623 unsigned int gpio = irq - IH_GPIO_BASE;
624 struct gpio_bank *bank;
627 if (check_gpio(gpio) < 0)
629 bank = get_gpio_bank(gpio);
630 spin_lock(&bank->lock);
631 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
632 spin_unlock(&bank->lock);
637 int omap_request_gpio(int gpio)
639 struct gpio_bank *bank;
641 if (check_gpio(gpio) < 0)
644 bank = get_gpio_bank(gpio);
645 spin_lock(&bank->lock);
646 if (unlikely(bank->reserved_map & (1 << get_gpio_index(gpio)))) {
647 printk(KERN_ERR "omap-gpio: GPIO %d is already reserved!\n", gpio);
649 spin_unlock(&bank->lock);
652 bank->reserved_map |= (1 << get_gpio_index(gpio));
654 /* Set trigger to none. You need to enable the trigger after request_irq */
655 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQT_NOEDGE);
657 #ifdef CONFIG_ARCH_OMAP15XX
658 if (bank->method == METHOD_GPIO_1510) {
661 /* Claim the pin for MPU */
662 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
663 __raw_writel(__raw_readl(reg) | (1 << get_gpio_index(gpio)), reg);
666 #ifdef CONFIG_ARCH_OMAP16XX
667 if (bank->method == METHOD_GPIO_1610) {
668 /* Enable wake-up during idle for dynamic tick */
669 void __iomem *reg = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
670 __raw_writel(1 << get_gpio_index(gpio), reg);
673 #ifdef CONFIG_ARCH_OMAP24XX
674 if (bank->method == METHOD_GPIO_24XX) {
675 /* Enable wake-up during idle for dynamic tick */
676 void __iomem *reg = bank->base + OMAP24XX_GPIO_SETWKUENA;
677 __raw_writel(1 << get_gpio_index(gpio), reg);
680 spin_unlock(&bank->lock);
685 void omap_free_gpio(int gpio)
687 struct gpio_bank *bank;
689 if (check_gpio(gpio) < 0)
691 bank = get_gpio_bank(gpio);
692 spin_lock(&bank->lock);
693 if (unlikely(!(bank->reserved_map & (1 << get_gpio_index(gpio))))) {
694 printk(KERN_ERR "omap-gpio: GPIO %d wasn't reserved!\n", gpio);
696 spin_unlock(&bank->lock);
699 #ifdef CONFIG_ARCH_OMAP16XX
700 if (bank->method == METHOD_GPIO_1610) {
701 /* Disable wake-up during idle for dynamic tick */
702 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
703 __raw_writel(1 << get_gpio_index(gpio), reg);
706 #ifdef CONFIG_ARCH_OMAP24XX
707 if (bank->method == METHOD_GPIO_24XX) {
708 /* Disable wake-up during idle for dynamic tick */
709 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
710 __raw_writel(1 << get_gpio_index(gpio), reg);
713 bank->reserved_map &= ~(1 << get_gpio_index(gpio));
714 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
715 _set_gpio_irqenable(bank, gpio, 0);
716 _clear_gpio_irqstatus(bank, gpio);
717 spin_unlock(&bank->lock);
721 * We need to unmask the GPIO bank interrupt as soon as possible to
722 * avoid missing GPIO interrupts for other lines in the bank.
723 * Then we need to mask-read-clear-unmask the triggered GPIO lines
724 * in the bank to avoid missing nested interrupts for a GPIO line.
725 * If we wait to unmask individual GPIO lines in the bank after the
726 * line's interrupt handler has been run, we may miss some nested
729 static void gpio_irq_handler(unsigned int irq, struct irqdesc *desc,
730 struct pt_regs *regs)
732 void __iomem *isr_reg = NULL;
734 unsigned int gpio_irq;
735 struct gpio_bank *bank;
737 desc->chip->ack(irq);
739 bank = (struct gpio_bank *) desc->data;
740 if (bank->method == METHOD_MPUIO)
741 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
742 #ifdef CONFIG_ARCH_OMAP15XX
743 if (bank->method == METHOD_GPIO_1510)
744 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
746 #if defined(CONFIG_ARCH_OMAP16XX)
747 if (bank->method == METHOD_GPIO_1610)
748 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
750 #ifdef CONFIG_ARCH_OMAP730
751 if (bank->method == METHOD_GPIO_730)
752 isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
754 #ifdef CONFIG_ARCH_OMAP24XX
755 if (bank->method == METHOD_GPIO_24XX)
756 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
760 isr = __raw_readl(isr_reg);
761 _enable_gpio_irqbank(bank, isr, 0);
762 _clear_gpio_irqbank(bank, isr);
763 _enable_gpio_irqbank(bank, isr, 1);
764 desc->chip->unmask(irq);
769 gpio_irq = bank->virtual_irq_start;
770 for (; isr != 0; isr >>= 1, gpio_irq++) {
774 d = irq_desc + gpio_irq;
775 desc_handle_irq(gpio_irq, d, regs);
780 static void gpio_ack_irq(unsigned int irq)
782 unsigned int gpio = irq - IH_GPIO_BASE;
783 struct gpio_bank *bank = get_gpio_bank(gpio);
785 _clear_gpio_irqstatus(bank, gpio);
788 static void gpio_mask_irq(unsigned int irq)
790 unsigned int gpio = irq - IH_GPIO_BASE;
791 struct gpio_bank *bank = get_gpio_bank(gpio);
793 _set_gpio_irqenable(bank, gpio, 0);
796 static void gpio_unmask_irq(unsigned int irq)
798 unsigned int gpio = irq - IH_GPIO_BASE;
799 unsigned int gpio_idx = get_gpio_index(gpio);
800 struct gpio_bank *bank = get_gpio_bank(gpio);
802 _set_gpio_irqenable(bank, gpio_idx, 1);
805 static void mpuio_ack_irq(unsigned int irq)
807 /* The ISR is reset automatically, so do nothing here. */
810 static void mpuio_mask_irq(unsigned int irq)
812 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
813 struct gpio_bank *bank = get_gpio_bank(gpio);
815 _set_gpio_irqenable(bank, gpio, 0);
818 static void mpuio_unmask_irq(unsigned int irq)
820 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
821 struct gpio_bank *bank = get_gpio_bank(gpio);
823 _set_gpio_irqenable(bank, gpio, 1);
826 static struct irqchip gpio_irq_chip = {
828 .mask = gpio_mask_irq,
829 .unmask = gpio_unmask_irq,
830 .set_type = gpio_irq_type,
831 .set_wake = gpio_wake_enable,
834 static struct irqchip mpuio_irq_chip = {
835 .ack = mpuio_ack_irq,
836 .mask = mpuio_mask_irq,
837 .unmask = mpuio_unmask_irq
840 static int initialized;
841 static struct clk * gpio_ick;
842 static struct clk * gpio_fck;
844 static int __init _omap_gpio_init(void)
847 struct gpio_bank *bank;
851 if (cpu_is_omap1510()) {
852 gpio_ick = clk_get(NULL, "arm_gpio_ck");
853 if (IS_ERR(gpio_ick))
854 printk("Could not get arm_gpio_ck\n");
858 if (cpu_is_omap24xx()) {
859 gpio_ick = clk_get(NULL, "gpios_ick");
860 if (IS_ERR(gpio_ick))
861 printk("Could not get gpios_ick\n");
864 gpio_fck = clk_get(NULL, "gpios_fck");
865 if (IS_ERR(gpio_ick))
866 printk("Could not get gpios_fck\n");
871 #ifdef CONFIG_ARCH_OMAP15XX
872 if (cpu_is_omap1510()) {
873 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
875 gpio_bank = gpio_bank_1510;
878 #if defined(CONFIG_ARCH_OMAP16XX)
879 if (cpu_is_omap16xx()) {
883 gpio_bank = gpio_bank_1610;
884 rev = omap_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
885 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
886 (rev >> 4) & 0x0f, rev & 0x0f);
889 #ifdef CONFIG_ARCH_OMAP730
890 if (cpu_is_omap730()) {
891 printk(KERN_INFO "OMAP730 GPIO hardware\n");
893 gpio_bank = gpio_bank_730;
896 #ifdef CONFIG_ARCH_OMAP24XX
897 if (cpu_is_omap24xx()) {
901 gpio_bank = gpio_bank_24xx;
902 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
903 printk(KERN_INFO "OMAP24xx GPIO hardware version %d.%d\n",
904 (rev >> 4) & 0x0f, rev & 0x0f);
907 for (i = 0; i < gpio_bank_count; i++) {
908 int j, gpio_count = 16;
910 bank = &gpio_bank[i];
911 bank->reserved_map = 0;
912 bank->base = IO_ADDRESS(bank->base);
913 spin_lock_init(&bank->lock);
914 if (bank->method == METHOD_MPUIO) {
915 omap_writew(0xFFFF, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_MASKIT);
917 #ifdef CONFIG_ARCH_OMAP15XX
918 if (bank->method == METHOD_GPIO_1510) {
919 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
920 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
923 #if defined(CONFIG_ARCH_OMAP16XX)
924 if (bank->method == METHOD_GPIO_1610) {
925 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
926 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
927 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
930 #ifdef CONFIG_ARCH_OMAP730
931 if (bank->method == METHOD_GPIO_730) {
932 __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
933 __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
935 gpio_count = 32; /* 730 has 32-bit GPIOs */
938 #ifdef CONFIG_ARCH_OMAP24XX
939 if (bank->method == METHOD_GPIO_24XX) {
940 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
941 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
946 for (j = bank->virtual_irq_start;
947 j < bank->virtual_irq_start + gpio_count; j++) {
948 if (bank->method == METHOD_MPUIO)
949 set_irq_chip(j, &mpuio_irq_chip);
951 set_irq_chip(j, &gpio_irq_chip);
952 set_irq_handler(j, do_simple_IRQ);
953 set_irq_flags(j, IRQF_VALID);
955 set_irq_chained_handler(bank->irq, gpio_irq_handler);
956 set_irq_data(bank->irq, bank);
959 /* Enable system clock for GPIO module.
960 * The CAM_CLK_CTRL *is* really the right place. */
961 if (cpu_is_omap16xx())
962 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
967 #if defined (CONFIG_ARCH_OMAP16XX) || defined (CONFIG_ARCH_OMAP24XX)
968 static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
972 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
975 for (i = 0; i < gpio_bank_count; i++) {
976 struct gpio_bank *bank = &gpio_bank[i];
977 void __iomem *wake_status;
978 void __iomem *wake_clear;
979 void __iomem *wake_set;
981 switch (bank->method) {
982 case METHOD_GPIO_1610:
983 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
984 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
985 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
987 case METHOD_GPIO_24XX:
988 wake_status = bank->base + OMAP24XX_GPIO_SETWKUENA;
989 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
990 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
996 spin_lock(&bank->lock);
997 bank->saved_wakeup = __raw_readl(wake_status);
998 __raw_writel(0xffffffff, wake_clear);
999 __raw_writel(bank->suspend_wakeup, wake_set);
1000 spin_unlock(&bank->lock);
1006 static int omap_gpio_resume(struct sys_device *dev)
1010 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
1013 for (i = 0; i < gpio_bank_count; i++) {
1014 struct gpio_bank *bank = &gpio_bank[i];
1015 void __iomem *wake_clear;
1016 void __iomem *wake_set;
1018 switch (bank->method) {
1019 case METHOD_GPIO_1610:
1020 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1021 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1023 case METHOD_GPIO_24XX:
1024 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1025 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1031 spin_lock(&bank->lock);
1032 __raw_writel(0xffffffff, wake_clear);
1033 __raw_writel(bank->saved_wakeup, wake_set);
1034 spin_unlock(&bank->lock);
1040 static struct sysdev_class omap_gpio_sysclass = {
1041 set_kset_name("gpio"),
1042 .suspend = omap_gpio_suspend,
1043 .resume = omap_gpio_resume,
1046 static struct sys_device omap_gpio_device = {
1048 .cls = &omap_gpio_sysclass,
1053 * This may get called early from board specific init
1054 * for boards that have interrupts routed via FPGA.
1056 int omap_gpio_init(void)
1059 return _omap_gpio_init();
1064 static int __init omap_gpio_sysinit(void)
1069 ret = _omap_gpio_init();
1071 #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX)
1072 if (cpu_is_omap16xx() || cpu_is_omap24xx()) {
1074 ret = sysdev_class_register(&omap_gpio_sysclass);
1076 ret = sysdev_register(&omap_gpio_device);
1084 EXPORT_SYMBOL(omap_request_gpio);
1085 EXPORT_SYMBOL(omap_free_gpio);
1086 EXPORT_SYMBOL(omap_set_gpio_direction);
1087 EXPORT_SYMBOL(omap_set_gpio_dataout);
1088 EXPORT_SYMBOL(omap_get_gpio_datain);
1090 arch_initcall(omap_gpio_sysinit);