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/init.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/interrupt.h>
18 #include <linux/ptrace.h>
19 #include <linux/sysdev.h>
20 #include <linux/err.h>
21 #include <linux/clk.h>
23 #include <asm/hardware.h>
25 #include <asm/arch/irqs.h>
26 #include <asm/arch/gpio.h>
27 #include <asm/mach/irq.h>
32 * OMAP1510 GPIO registers
34 #define OMAP1510_GPIO_BASE (void __iomem *)0xfffce000
35 #define OMAP1510_GPIO_DATA_INPUT 0x00
36 #define OMAP1510_GPIO_DATA_OUTPUT 0x04
37 #define OMAP1510_GPIO_DIR_CONTROL 0x08
38 #define OMAP1510_GPIO_INT_CONTROL 0x0c
39 #define OMAP1510_GPIO_INT_MASK 0x10
40 #define OMAP1510_GPIO_INT_STATUS 0x14
41 #define OMAP1510_GPIO_PIN_CONTROL 0x18
43 #define OMAP1510_IH_GPIO_BASE 64
46 * OMAP1610 specific GPIO registers
48 #define OMAP1610_GPIO1_BASE (void __iomem *)0xfffbe400
49 #define OMAP1610_GPIO2_BASE (void __iomem *)0xfffbec00
50 #define OMAP1610_GPIO3_BASE (void __iomem *)0xfffbb400
51 #define OMAP1610_GPIO4_BASE (void __iomem *)0xfffbbc00
52 #define OMAP1610_GPIO_REVISION 0x0000
53 #define OMAP1610_GPIO_SYSCONFIG 0x0010
54 #define OMAP1610_GPIO_SYSSTATUS 0x0014
55 #define OMAP1610_GPIO_IRQSTATUS1 0x0018
56 #define OMAP1610_GPIO_IRQENABLE1 0x001c
57 #define OMAP1610_GPIO_WAKEUPENABLE 0x0028
58 #define OMAP1610_GPIO_DATAIN 0x002c
59 #define OMAP1610_GPIO_DATAOUT 0x0030
60 #define OMAP1610_GPIO_DIRECTION 0x0034
61 #define OMAP1610_GPIO_EDGE_CTRL1 0x0038
62 #define OMAP1610_GPIO_EDGE_CTRL2 0x003c
63 #define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
64 #define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
65 #define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
66 #define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
67 #define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
68 #define OMAP1610_GPIO_SET_DATAOUT 0x00f0
71 * OMAP730 specific GPIO registers
73 #define OMAP730_GPIO1_BASE (void __iomem *)0xfffbc000
74 #define OMAP730_GPIO2_BASE (void __iomem *)0xfffbc800
75 #define OMAP730_GPIO3_BASE (void __iomem *)0xfffbd000
76 #define OMAP730_GPIO4_BASE (void __iomem *)0xfffbd800
77 #define OMAP730_GPIO5_BASE (void __iomem *)0xfffbe000
78 #define OMAP730_GPIO6_BASE (void __iomem *)0xfffbe800
79 #define OMAP730_GPIO_DATA_INPUT 0x00
80 #define OMAP730_GPIO_DATA_OUTPUT 0x04
81 #define OMAP730_GPIO_DIR_CONTROL 0x08
82 #define OMAP730_GPIO_INT_CONTROL 0x0c
83 #define OMAP730_GPIO_INT_MASK 0x10
84 #define OMAP730_GPIO_INT_STATUS 0x14
87 * omap24xx specific GPIO registers
89 #define OMAP24XX_GPIO1_BASE (void __iomem *)0x48018000
90 #define OMAP24XX_GPIO2_BASE (void __iomem *)0x4801a000
91 #define OMAP24XX_GPIO3_BASE (void __iomem *)0x4801c000
92 #define OMAP24XX_GPIO4_BASE (void __iomem *)0x4801e000
93 #define OMAP24XX_GPIO_REVISION 0x0000
94 #define OMAP24XX_GPIO_SYSCONFIG 0x0010
95 #define OMAP24XX_GPIO_SYSSTATUS 0x0014
96 #define OMAP24XX_GPIO_IRQSTATUS1 0x0018
97 #define OMAP24XX_GPIO_IRQENABLE1 0x001c
98 #define OMAP24XX_GPIO_CTRL 0x0030
99 #define OMAP24XX_GPIO_OE 0x0034
100 #define OMAP24XX_GPIO_DATAIN 0x0038
101 #define OMAP24XX_GPIO_DATAOUT 0x003c
102 #define OMAP24XX_GPIO_LEVELDETECT0 0x0040
103 #define OMAP24XX_GPIO_LEVELDETECT1 0x0044
104 #define OMAP24XX_GPIO_RISINGDETECT 0x0048
105 #define OMAP24XX_GPIO_FALLINGDETECT 0x004c
106 #define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
107 #define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
108 #define OMAP24XX_GPIO_CLEARWKUENA 0x0080
109 #define OMAP24XX_GPIO_SETWKUENA 0x0084
110 #define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
111 #define OMAP24XX_GPIO_SETDATAOUT 0x0094
113 #define OMAP_MPUIO_MASK (~OMAP_MAX_GPIO_LINES & 0xff)
118 u16 virtual_irq_start;
126 #define METHOD_MPUIO 0
127 #define METHOD_GPIO_1510 1
128 #define METHOD_GPIO_1610 2
129 #define METHOD_GPIO_730 3
130 #define METHOD_GPIO_24XX 4
132 #ifdef CONFIG_ARCH_OMAP16XX
133 static struct gpio_bank gpio_bank_1610[5] = {
134 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
135 { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
136 { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
137 { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
138 { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
142 #ifdef CONFIG_ARCH_OMAP15XX
143 static struct gpio_bank gpio_bank_1510[2] = {
144 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
145 { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
149 #ifdef CONFIG_ARCH_OMAP730
150 static struct gpio_bank gpio_bank_730[7] = {
151 { OMAP_MPUIO_BASE, INT_730_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
152 { OMAP730_GPIO1_BASE, INT_730_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_730 },
153 { OMAP730_GPIO2_BASE, INT_730_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_730 },
154 { OMAP730_GPIO3_BASE, INT_730_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_730 },
155 { OMAP730_GPIO4_BASE, INT_730_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_730 },
156 { OMAP730_GPIO5_BASE, INT_730_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_730 },
157 { OMAP730_GPIO6_BASE, INT_730_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_730 },
161 #ifdef CONFIG_ARCH_OMAP24XX
162 static struct gpio_bank gpio_bank_24xx[4] = {
163 { OMAP24XX_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
164 { OMAP24XX_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
165 { OMAP24XX_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
166 { OMAP24XX_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
170 static struct gpio_bank *gpio_bank;
171 static int gpio_bank_count;
173 static inline struct gpio_bank *get_gpio_bank(int gpio)
175 #ifdef CONFIG_ARCH_OMAP15XX
176 if (cpu_is_omap15xx()) {
177 if (OMAP_GPIO_IS_MPUIO(gpio))
178 return &gpio_bank[0];
179 return &gpio_bank[1];
182 #if defined(CONFIG_ARCH_OMAP16XX)
183 if (cpu_is_omap16xx()) {
184 if (OMAP_GPIO_IS_MPUIO(gpio))
185 return &gpio_bank[0];
186 return &gpio_bank[1 + (gpio >> 4)];
189 #ifdef CONFIG_ARCH_OMAP730
190 if (cpu_is_omap730()) {
191 if (OMAP_GPIO_IS_MPUIO(gpio))
192 return &gpio_bank[0];
193 return &gpio_bank[1 + (gpio >> 5)];
196 #ifdef CONFIG_ARCH_OMAP24XX
197 if (cpu_is_omap24xx())
198 return &gpio_bank[gpio >> 5];
202 static inline int get_gpio_index(int gpio)
204 #ifdef CONFIG_ARCH_OMAP730
205 if (cpu_is_omap730())
208 #ifdef CONFIG_ARCH_OMAP24XX
209 if (cpu_is_omap24xx())
215 static inline int gpio_valid(int gpio)
219 if (OMAP_GPIO_IS_MPUIO(gpio)) {
220 if ((gpio & OMAP_MPUIO_MASK) > 16)
224 #ifdef CONFIG_ARCH_OMAP15XX
225 if (cpu_is_omap15xx() && gpio < 16)
228 #if defined(CONFIG_ARCH_OMAP16XX)
229 if ((cpu_is_omap16xx()) && gpio < 64)
232 #ifdef CONFIG_ARCH_OMAP730
233 if (cpu_is_omap730() && gpio < 192)
236 #ifdef CONFIG_ARCH_OMAP24XX
237 if (cpu_is_omap24xx() && gpio < 128)
243 static int check_gpio(int gpio)
245 if (unlikely(gpio_valid(gpio)) < 0) {
246 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
253 static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
255 void __iomem *reg = bank->base;
258 switch (bank->method) {
260 reg += OMAP_MPUIO_IO_CNTL;
262 case METHOD_GPIO_1510:
263 reg += OMAP1510_GPIO_DIR_CONTROL;
265 case METHOD_GPIO_1610:
266 reg += OMAP1610_GPIO_DIRECTION;
268 case METHOD_GPIO_730:
269 reg += OMAP730_GPIO_DIR_CONTROL;
271 case METHOD_GPIO_24XX:
272 reg += OMAP24XX_GPIO_OE;
275 l = __raw_readl(reg);
280 __raw_writel(l, reg);
283 void omap_set_gpio_direction(int gpio, int is_input)
285 struct gpio_bank *bank;
287 if (check_gpio(gpio) < 0)
289 bank = get_gpio_bank(gpio);
290 spin_lock(&bank->lock);
291 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
292 spin_unlock(&bank->lock);
295 static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
297 void __iomem *reg = bank->base;
300 switch (bank->method) {
302 reg += OMAP_MPUIO_OUTPUT;
303 l = __raw_readl(reg);
309 case METHOD_GPIO_1510:
310 reg += OMAP1510_GPIO_DATA_OUTPUT;
311 l = __raw_readl(reg);
317 case METHOD_GPIO_1610:
319 reg += OMAP1610_GPIO_SET_DATAOUT;
321 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
324 case METHOD_GPIO_730:
325 reg += OMAP730_GPIO_DATA_OUTPUT;
326 l = __raw_readl(reg);
332 case METHOD_GPIO_24XX:
334 reg += OMAP24XX_GPIO_SETDATAOUT;
336 reg += OMAP24XX_GPIO_CLEARDATAOUT;
343 __raw_writel(l, reg);
346 void omap_set_gpio_dataout(int gpio, int enable)
348 struct gpio_bank *bank;
350 if (check_gpio(gpio) < 0)
352 bank = get_gpio_bank(gpio);
353 spin_lock(&bank->lock);
354 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
355 spin_unlock(&bank->lock);
358 int omap_get_gpio_datain(int gpio)
360 struct gpio_bank *bank;
363 if (check_gpio(gpio) < 0)
365 bank = get_gpio_bank(gpio);
367 switch (bank->method) {
369 reg += OMAP_MPUIO_INPUT_LATCH;
371 case METHOD_GPIO_1510:
372 reg += OMAP1510_GPIO_DATA_INPUT;
374 case METHOD_GPIO_1610:
375 reg += OMAP1610_GPIO_DATAIN;
377 case METHOD_GPIO_730:
378 reg += OMAP730_GPIO_DATA_INPUT;
380 case METHOD_GPIO_24XX:
381 reg += OMAP24XX_GPIO_DATAIN;
387 return (__raw_readl(reg)
388 & (1 << get_gpio_index(gpio))) != 0;
391 #define MOD_REG_BIT(reg, bit_mask, set) \
393 int l = __raw_readl(base + reg); \
394 if (set) l |= bit_mask; \
395 else l &= ~bit_mask; \
396 __raw_writel(l, base + reg); \
399 static inline void set_24xx_gpio_triggering(void __iomem *base, int gpio, int trigger)
401 u32 gpio_bit = 1 << gpio;
403 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
404 trigger & __IRQT_LOWLVL);
405 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
406 trigger & __IRQT_HIGHLVL);
407 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
408 trigger & __IRQT_RISEDGE);
409 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
410 trigger & __IRQT_FALEDGE);
411 /* FIXME: Possibly do 'set_irq_handler(j, do_level_IRQ)' if only level
412 * triggering requested. */
415 static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
417 void __iomem *reg = bank->base;
420 switch (bank->method) {
422 reg += OMAP_MPUIO_GPIO_INT_EDGE;
423 l = __raw_readl(reg);
424 if (trigger & __IRQT_RISEDGE)
426 else if (trigger & __IRQT_FALEDGE)
431 case METHOD_GPIO_1510:
432 reg += OMAP1510_GPIO_INT_CONTROL;
433 l = __raw_readl(reg);
434 if (trigger & __IRQT_RISEDGE)
436 else if (trigger & __IRQT_FALEDGE)
441 case METHOD_GPIO_1610:
443 reg += OMAP1610_GPIO_EDGE_CTRL2;
445 reg += OMAP1610_GPIO_EDGE_CTRL1;
447 /* We allow only edge triggering, i.e. two lowest bits */
448 if (trigger & (__IRQT_LOWLVL | __IRQT_HIGHLVL))
450 l = __raw_readl(reg);
451 l &= ~(3 << (gpio << 1));
452 if (trigger & __IRQT_RISEDGE)
453 l |= 2 << (gpio << 1);
454 if (trigger & __IRQT_FALEDGE)
455 l |= 1 << (gpio << 1);
457 case METHOD_GPIO_730:
458 reg += OMAP730_GPIO_INT_CONTROL;
459 l = __raw_readl(reg);
460 if (trigger & __IRQT_RISEDGE)
462 else if (trigger & __IRQT_FALEDGE)
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_PROBE)
496 if (!cpu_is_omap24xx() && (type & (__IRQT_LOWLVL|__IRQT_HIGHLVL)))
499 bank = get_gpio_bank(gpio);
500 spin_lock(&bank->lock);
501 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
502 spin_unlock(&bank->lock);
506 static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
508 void __iomem *reg = bank->base;
510 switch (bank->method) {
512 /* MPUIO irqstatus is reset by reading the status register,
513 * so do nothing here */
515 case METHOD_GPIO_1510:
516 reg += OMAP1510_GPIO_INT_STATUS;
518 case METHOD_GPIO_1610:
519 reg += OMAP1610_GPIO_IRQSTATUS1;
521 case METHOD_GPIO_730:
522 reg += OMAP730_GPIO_INT_STATUS;
524 case METHOD_GPIO_24XX:
525 reg += OMAP24XX_GPIO_IRQSTATUS1;
531 __raw_writel(gpio_mask, reg);
534 static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
536 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
539 static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
541 void __iomem *reg = bank->base;
544 switch (bank->method) {
546 reg += OMAP_MPUIO_GPIO_MASKIT;
547 l = __raw_readl(reg);
553 case METHOD_GPIO_1510:
554 reg += OMAP1510_GPIO_INT_MASK;
555 l = __raw_readl(reg);
561 case METHOD_GPIO_1610:
563 reg += OMAP1610_GPIO_SET_IRQENABLE1;
565 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
568 case METHOD_GPIO_730:
569 reg += OMAP730_GPIO_INT_MASK;
570 l = __raw_readl(reg);
576 case METHOD_GPIO_24XX:
578 reg += OMAP24XX_GPIO_SETIRQENABLE1;
580 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
587 __raw_writel(l, reg);
590 static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
592 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
596 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
597 * 1510 does not seem to have a wake-up register. If JTAG is connected
598 * to the target, system will wake up always on GPIO events. While
599 * system is running all registered GPIO interrupts need to have wake-up
600 * enabled. When system is suspended, only selected GPIO interrupts need
601 * to have wake-up enabled.
603 static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
605 switch (bank->method) {
606 case METHOD_GPIO_1610:
607 case METHOD_GPIO_24XX:
608 spin_lock(&bank->lock);
610 bank->suspend_wakeup |= (1 << gpio);
612 bank->suspend_wakeup &= ~(1 << gpio);
613 spin_unlock(&bank->lock);
616 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
622 /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
623 static int gpio_wake_enable(unsigned int irq, unsigned int enable)
625 unsigned int gpio = irq - IH_GPIO_BASE;
626 struct gpio_bank *bank;
629 if (check_gpio(gpio) < 0)
631 bank = get_gpio_bank(gpio);
632 spin_lock(&bank->lock);
633 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
634 spin_unlock(&bank->lock);
639 int omap_request_gpio(int gpio)
641 struct gpio_bank *bank;
643 if (check_gpio(gpio) < 0)
646 bank = get_gpio_bank(gpio);
647 spin_lock(&bank->lock);
648 if (unlikely(bank->reserved_map & (1 << get_gpio_index(gpio)))) {
649 printk(KERN_ERR "omap-gpio: GPIO %d is already reserved!\n", gpio);
651 spin_unlock(&bank->lock);
654 bank->reserved_map |= (1 << get_gpio_index(gpio));
656 /* Set trigger to none. You need to enable the trigger after request_irq */
657 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQT_NOEDGE);
659 #ifdef CONFIG_ARCH_OMAP15XX
660 if (bank->method == METHOD_GPIO_1510) {
663 /* Claim the pin for MPU */
664 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
665 __raw_writel(__raw_readl(reg) | (1 << get_gpio_index(gpio)), reg);
668 #ifdef CONFIG_ARCH_OMAP16XX
669 if (bank->method == METHOD_GPIO_1610) {
670 /* Enable wake-up during idle for dynamic tick */
671 void __iomem *reg = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
672 __raw_writel(1 << get_gpio_index(gpio), reg);
675 #ifdef CONFIG_ARCH_OMAP24XX
676 if (bank->method == METHOD_GPIO_24XX) {
677 /* Enable wake-up during idle for dynamic tick */
678 void __iomem *reg = bank->base + OMAP24XX_GPIO_SETWKUENA;
679 __raw_writel(1 << get_gpio_index(gpio), reg);
682 spin_unlock(&bank->lock);
687 void omap_free_gpio(int gpio)
689 struct gpio_bank *bank;
691 if (check_gpio(gpio) < 0)
693 bank = get_gpio_bank(gpio);
694 spin_lock(&bank->lock);
695 if (unlikely(!(bank->reserved_map & (1 << get_gpio_index(gpio))))) {
696 printk(KERN_ERR "omap-gpio: GPIO %d wasn't reserved!\n", gpio);
698 spin_unlock(&bank->lock);
701 #ifdef CONFIG_ARCH_OMAP16XX
702 if (bank->method == METHOD_GPIO_1610) {
703 /* Disable wake-up during idle for dynamic tick */
704 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
705 __raw_writel(1 << get_gpio_index(gpio), reg);
708 #ifdef CONFIG_ARCH_OMAP24XX
709 if (bank->method == METHOD_GPIO_24XX) {
710 /* Disable wake-up during idle for dynamic tick */
711 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
712 __raw_writel(1 << get_gpio_index(gpio), reg);
715 bank->reserved_map &= ~(1 << get_gpio_index(gpio));
716 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
717 _set_gpio_irqenable(bank, gpio, 0);
718 _clear_gpio_irqstatus(bank, gpio);
719 spin_unlock(&bank->lock);
723 * We need to unmask the GPIO bank interrupt as soon as possible to
724 * avoid missing GPIO interrupts for other lines in the bank.
725 * Then we need to mask-read-clear-unmask the triggered GPIO lines
726 * in the bank to avoid missing nested interrupts for a GPIO line.
727 * If we wait to unmask individual GPIO lines in the bank after the
728 * line's interrupt handler has been run, we may miss some nested
731 static void gpio_irq_handler(unsigned int irq, struct irqdesc *desc,
732 struct pt_regs *regs)
734 void __iomem *isr_reg = NULL;
736 unsigned int gpio_irq;
737 struct gpio_bank *bank;
739 desc->chip->ack(irq);
741 bank = (struct gpio_bank *) desc->data;
742 if (bank->method == METHOD_MPUIO)
743 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
744 #ifdef CONFIG_ARCH_OMAP15XX
745 if (bank->method == METHOD_GPIO_1510)
746 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
748 #if defined(CONFIG_ARCH_OMAP16XX)
749 if (bank->method == METHOD_GPIO_1610)
750 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
752 #ifdef CONFIG_ARCH_OMAP730
753 if (bank->method == METHOD_GPIO_730)
754 isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
756 #ifdef CONFIG_ARCH_OMAP24XX
757 if (bank->method == METHOD_GPIO_24XX)
758 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
761 u32 isr_saved, level_mask = 0;
763 isr_saved = isr = __raw_readl(isr_reg);
765 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
768 if (cpu_is_omap24xx())
770 __raw_readl(bank->base +
771 OMAP24XX_GPIO_LEVELDETECT0) |
772 __raw_readl(bank->base +
773 OMAP24XX_GPIO_LEVELDETECT1);
775 /* clear edge sensitive interrupts before handler(s) are
776 called so that we don't miss any interrupt occurred while
778 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
779 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
780 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
782 /* if there is only edge sensitive GPIO pin interrupts
783 configured, we could unmask GPIO bank interrupt immediately */
785 desc->chip->unmask(irq);
790 gpio_irq = bank->virtual_irq_start;
791 for (; isr != 0; isr >>= 1, gpio_irq++) {
795 d = irq_desc + gpio_irq;
796 desc_handle_irq(gpio_irq, d, regs);
799 if (cpu_is_omap24xx()) {
800 /* clear level sensitive interrupts after handler(s) */
801 _enable_gpio_irqbank(bank, isr_saved & level_mask, 0);
802 _clear_gpio_irqbank(bank, isr_saved & level_mask);
803 _enable_gpio_irqbank(bank, isr_saved & level_mask, 1);
806 /* if bank has any level sensitive GPIO pin interrupt
807 configured, we must unmask the bank interrupt only after
808 handler(s) are executed in order to avoid spurious bank
811 desc->chip->unmask(irq);
815 static void gpio_ack_irq(unsigned int irq)
817 unsigned int gpio = irq - IH_GPIO_BASE;
818 struct gpio_bank *bank = get_gpio_bank(gpio);
820 _clear_gpio_irqstatus(bank, gpio);
823 static void gpio_mask_irq(unsigned int irq)
825 unsigned int gpio = irq - IH_GPIO_BASE;
826 struct gpio_bank *bank = get_gpio_bank(gpio);
828 _set_gpio_irqenable(bank, gpio, 0);
831 static void gpio_unmask_irq(unsigned int irq)
833 unsigned int gpio = irq - IH_GPIO_BASE;
834 unsigned int gpio_idx = get_gpio_index(gpio);
835 struct gpio_bank *bank = get_gpio_bank(gpio);
837 _set_gpio_irqenable(bank, gpio_idx, 1);
840 static void mpuio_ack_irq(unsigned int irq)
842 /* The ISR is reset automatically, so do nothing here. */
845 static void mpuio_mask_irq(unsigned int irq)
847 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
848 struct gpio_bank *bank = get_gpio_bank(gpio);
850 _set_gpio_irqenable(bank, gpio, 0);
853 static void mpuio_unmask_irq(unsigned int irq)
855 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
856 struct gpio_bank *bank = get_gpio_bank(gpio);
858 _set_gpio_irqenable(bank, gpio, 1);
861 static struct irqchip gpio_irq_chip = {
863 .mask = gpio_mask_irq,
864 .unmask = gpio_unmask_irq,
865 .set_type = gpio_irq_type,
866 .set_wake = gpio_wake_enable,
869 static struct irqchip mpuio_irq_chip = {
870 .ack = mpuio_ack_irq,
871 .mask = mpuio_mask_irq,
872 .unmask = mpuio_unmask_irq
875 static int initialized;
876 static struct clk * gpio_ick;
877 static struct clk * gpio_fck;
879 static int __init _omap_gpio_init(void)
882 struct gpio_bank *bank;
886 if (cpu_is_omap15xx()) {
887 gpio_ick = clk_get(NULL, "arm_gpio_ck");
888 if (IS_ERR(gpio_ick))
889 printk("Could not get arm_gpio_ck\n");
891 clk_enable(gpio_ick);
893 if (cpu_is_omap24xx()) {
894 gpio_ick = clk_get(NULL, "gpios_ick");
895 if (IS_ERR(gpio_ick))
896 printk("Could not get gpios_ick\n");
898 clk_enable(gpio_ick);
899 gpio_fck = clk_get(NULL, "gpios_fck");
900 if (IS_ERR(gpio_ick))
901 printk("Could not get gpios_fck\n");
903 clk_enable(gpio_fck);
906 #ifdef CONFIG_ARCH_OMAP15XX
907 if (cpu_is_omap15xx()) {
908 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
910 gpio_bank = gpio_bank_1510;
913 #if defined(CONFIG_ARCH_OMAP16XX)
914 if (cpu_is_omap16xx()) {
918 gpio_bank = gpio_bank_1610;
919 rev = omap_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
920 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
921 (rev >> 4) & 0x0f, rev & 0x0f);
924 #ifdef CONFIG_ARCH_OMAP730
925 if (cpu_is_omap730()) {
926 printk(KERN_INFO "OMAP730 GPIO hardware\n");
928 gpio_bank = gpio_bank_730;
931 #ifdef CONFIG_ARCH_OMAP24XX
932 if (cpu_is_omap24xx()) {
936 gpio_bank = gpio_bank_24xx;
937 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
938 printk(KERN_INFO "OMAP24xx GPIO hardware version %d.%d\n",
939 (rev >> 4) & 0x0f, rev & 0x0f);
942 for (i = 0; i < gpio_bank_count; i++) {
943 int j, gpio_count = 16;
945 bank = &gpio_bank[i];
946 bank->reserved_map = 0;
947 bank->base = IO_ADDRESS(bank->base);
948 spin_lock_init(&bank->lock);
949 if (bank->method == METHOD_MPUIO) {
950 omap_writew(0xFFFF, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_MASKIT);
952 #ifdef CONFIG_ARCH_OMAP15XX
953 if (bank->method == METHOD_GPIO_1510) {
954 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
955 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
958 #if defined(CONFIG_ARCH_OMAP16XX)
959 if (bank->method == METHOD_GPIO_1610) {
960 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
961 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
962 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
965 #ifdef CONFIG_ARCH_OMAP730
966 if (bank->method == METHOD_GPIO_730) {
967 __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
968 __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
970 gpio_count = 32; /* 730 has 32-bit GPIOs */
973 #ifdef CONFIG_ARCH_OMAP24XX
974 if (bank->method == METHOD_GPIO_24XX) {
975 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
976 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
981 for (j = bank->virtual_irq_start;
982 j < bank->virtual_irq_start + gpio_count; j++) {
983 if (bank->method == METHOD_MPUIO)
984 set_irq_chip(j, &mpuio_irq_chip);
986 set_irq_chip(j, &gpio_irq_chip);
987 set_irq_handler(j, do_simple_IRQ);
988 set_irq_flags(j, IRQF_VALID);
990 set_irq_chained_handler(bank->irq, gpio_irq_handler);
991 set_irq_data(bank->irq, bank);
994 /* Enable system clock for GPIO module.
995 * The CAM_CLK_CTRL *is* really the right place. */
996 if (cpu_is_omap16xx())
997 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
1002 #if defined (CONFIG_ARCH_OMAP16XX) || defined (CONFIG_ARCH_OMAP24XX)
1003 static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
1007 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
1010 for (i = 0; i < gpio_bank_count; i++) {
1011 struct gpio_bank *bank = &gpio_bank[i];
1012 void __iomem *wake_status;
1013 void __iomem *wake_clear;
1014 void __iomem *wake_set;
1016 switch (bank->method) {
1017 case METHOD_GPIO_1610:
1018 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1019 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1020 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1022 case METHOD_GPIO_24XX:
1023 wake_status = bank->base + OMAP24XX_GPIO_SETWKUENA;
1024 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1025 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1031 spin_lock(&bank->lock);
1032 bank->saved_wakeup = __raw_readl(wake_status);
1033 __raw_writel(0xffffffff, wake_clear);
1034 __raw_writel(bank->suspend_wakeup, wake_set);
1035 spin_unlock(&bank->lock);
1041 static int omap_gpio_resume(struct sys_device *dev)
1045 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
1048 for (i = 0; i < gpio_bank_count; i++) {
1049 struct gpio_bank *bank = &gpio_bank[i];
1050 void __iomem *wake_clear;
1051 void __iomem *wake_set;
1053 switch (bank->method) {
1054 case METHOD_GPIO_1610:
1055 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1056 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1058 case METHOD_GPIO_24XX:
1059 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1060 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1066 spin_lock(&bank->lock);
1067 __raw_writel(0xffffffff, wake_clear);
1068 __raw_writel(bank->saved_wakeup, wake_set);
1069 spin_unlock(&bank->lock);
1075 static struct sysdev_class omap_gpio_sysclass = {
1076 set_kset_name("gpio"),
1077 .suspend = omap_gpio_suspend,
1078 .resume = omap_gpio_resume,
1081 static struct sys_device omap_gpio_device = {
1083 .cls = &omap_gpio_sysclass,
1088 * This may get called early from board specific init
1089 * for boards that have interrupts routed via FPGA.
1091 int omap_gpio_init(void)
1094 return _omap_gpio_init();
1099 static int __init omap_gpio_sysinit(void)
1104 ret = _omap_gpio_init();
1106 #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX)
1107 if (cpu_is_omap16xx() || cpu_is_omap24xx()) {
1109 ret = sysdev_class_register(&omap_gpio_sysclass);
1111 ret = sysdev_register(&omap_gpio_device);
1119 EXPORT_SYMBOL(omap_request_gpio);
1120 EXPORT_SYMBOL(omap_free_gpio);
1121 EXPORT_SYMBOL(omap_set_gpio_direction);
1122 EXPORT_SYMBOL(omap_set_gpio_dataout);
1123 EXPORT_SYMBOL(omap_get_gpio_datain);
1125 arch_initcall(omap_gpio_sysinit);