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/interrupt.h>
17 #include <linux/sysdev.h>
18 #include <linux/err.h>
19 #include <linux/clk.h>
21 #include <asm/hardware.h>
23 #include <asm/arch/irqs.h>
24 #include <asm/arch/gpio.h>
25 #include <asm/mach/irq.h>
30 * OMAP1510 GPIO registers
32 #define OMAP1510_GPIO_BASE (void __iomem *)0xfffce000
33 #define OMAP1510_GPIO_DATA_INPUT 0x00
34 #define OMAP1510_GPIO_DATA_OUTPUT 0x04
35 #define OMAP1510_GPIO_DIR_CONTROL 0x08
36 #define OMAP1510_GPIO_INT_CONTROL 0x0c
37 #define OMAP1510_GPIO_INT_MASK 0x10
38 #define OMAP1510_GPIO_INT_STATUS 0x14
39 #define OMAP1510_GPIO_PIN_CONTROL 0x18
41 #define OMAP1510_IH_GPIO_BASE 64
44 * OMAP1610 specific GPIO registers
46 #define OMAP1610_GPIO1_BASE (void __iomem *)0xfffbe400
47 #define OMAP1610_GPIO2_BASE (void __iomem *)0xfffbec00
48 #define OMAP1610_GPIO3_BASE (void __iomem *)0xfffbb400
49 #define OMAP1610_GPIO4_BASE (void __iomem *)0xfffbbc00
50 #define OMAP1610_GPIO_REVISION 0x0000
51 #define OMAP1610_GPIO_SYSCONFIG 0x0010
52 #define OMAP1610_GPIO_SYSSTATUS 0x0014
53 #define OMAP1610_GPIO_IRQSTATUS1 0x0018
54 #define OMAP1610_GPIO_IRQENABLE1 0x001c
55 #define OMAP1610_GPIO_WAKEUPENABLE 0x0028
56 #define OMAP1610_GPIO_DATAIN 0x002c
57 #define OMAP1610_GPIO_DATAOUT 0x0030
58 #define OMAP1610_GPIO_DIRECTION 0x0034
59 #define OMAP1610_GPIO_EDGE_CTRL1 0x0038
60 #define OMAP1610_GPIO_EDGE_CTRL2 0x003c
61 #define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
62 #define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
63 #define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
64 #define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
65 #define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
66 #define OMAP1610_GPIO_SET_DATAOUT 0x00f0
69 * OMAP730 specific GPIO registers
71 #define OMAP730_GPIO1_BASE (void __iomem *)0xfffbc000
72 #define OMAP730_GPIO2_BASE (void __iomem *)0xfffbc800
73 #define OMAP730_GPIO3_BASE (void __iomem *)0xfffbd000
74 #define OMAP730_GPIO4_BASE (void __iomem *)0xfffbd800
75 #define OMAP730_GPIO5_BASE (void __iomem *)0xfffbe000
76 #define OMAP730_GPIO6_BASE (void __iomem *)0xfffbe800
77 #define OMAP730_GPIO_DATA_INPUT 0x00
78 #define OMAP730_GPIO_DATA_OUTPUT 0x04
79 #define OMAP730_GPIO_DIR_CONTROL 0x08
80 #define OMAP730_GPIO_INT_CONTROL 0x0c
81 #define OMAP730_GPIO_INT_MASK 0x10
82 #define OMAP730_GPIO_INT_STATUS 0x14
85 * omap24xx specific GPIO registers
87 #define OMAP242X_GPIO1_BASE (void __iomem *)0x48018000
88 #define OMAP242X_GPIO2_BASE (void __iomem *)0x4801a000
89 #define OMAP242X_GPIO3_BASE (void __iomem *)0x4801c000
90 #define OMAP242X_GPIO4_BASE (void __iomem *)0x4801e000
92 #define OMAP243X_GPIO1_BASE (void __iomem *)0x4900C000
93 #define OMAP243X_GPIO2_BASE (void __iomem *)0x4900E000
94 #define OMAP243X_GPIO3_BASE (void __iomem *)0x49010000
95 #define OMAP243X_GPIO4_BASE (void __iomem *)0x49012000
96 #define OMAP243X_GPIO5_BASE (void __iomem *)0x480B6000
98 #define OMAP24XX_GPIO_REVISION 0x0000
99 #define OMAP24XX_GPIO_SYSCONFIG 0x0010
100 #define OMAP24XX_GPIO_SYSSTATUS 0x0014
101 #define OMAP24XX_GPIO_IRQSTATUS1 0x0018
102 #define OMAP24XX_GPIO_IRQSTATUS2 0x0028
103 #define OMAP24XX_GPIO_IRQENABLE2 0x002c
104 #define OMAP24XX_GPIO_IRQENABLE1 0x001c
105 #define OMAP24XX_GPIO_CTRL 0x0030
106 #define OMAP24XX_GPIO_OE 0x0034
107 #define OMAP24XX_GPIO_DATAIN 0x0038
108 #define OMAP24XX_GPIO_DATAOUT 0x003c
109 #define OMAP24XX_GPIO_LEVELDETECT0 0x0040
110 #define OMAP24XX_GPIO_LEVELDETECT1 0x0044
111 #define OMAP24XX_GPIO_RISINGDETECT 0x0048
112 #define OMAP24XX_GPIO_FALLINGDETECT 0x004c
113 #define OMAP24XX_GPIO_DEBOUNCE_EN 0x0050
114 #define OMAP24XX_GPIO_DEBOUNCE_VAL 0x0054
115 #define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
116 #define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
117 #define OMAP24XX_GPIO_CLEARWKUENA 0x0080
118 #define OMAP24XX_GPIO_SETWKUENA 0x0084
119 #define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
120 #define OMAP24XX_GPIO_SETDATAOUT 0x0094
123 * omap34xx specific GPIO registers
126 #define OMAP34XX_GPIO1_BASE (void __iomem *)0x48310000
127 #define OMAP34XX_GPIO2_BASE (void __iomem *)0x49050000
128 #define OMAP34XX_GPIO3_BASE (void __iomem *)0x49052000
129 #define OMAP34XX_GPIO4_BASE (void __iomem *)0x49054000
130 #define OMAP34XX_GPIO5_BASE (void __iomem *)0x49056000
131 #define OMAP34XX_GPIO6_BASE (void __iomem *)0x49058000
137 u16 virtual_irq_start;
140 #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
144 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
145 u32 non_wakeup_gpios;
146 u32 enabled_non_wakeup_gpios;
149 u32 saved_fallingdetect;
150 u32 saved_risingdetect;
155 #define METHOD_MPUIO 0
156 #define METHOD_GPIO_1510 1
157 #define METHOD_GPIO_1610 2
158 #define METHOD_GPIO_730 3
159 #define METHOD_GPIO_24XX 4
161 #ifdef CONFIG_ARCH_OMAP16XX
162 static struct gpio_bank gpio_bank_1610[5] = {
163 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
164 { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
165 { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
166 { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
167 { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
171 #ifdef CONFIG_ARCH_OMAP15XX
172 static struct gpio_bank gpio_bank_1510[2] = {
173 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
174 { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
178 #ifdef CONFIG_ARCH_OMAP730
179 static struct gpio_bank gpio_bank_730[7] = {
180 { OMAP_MPUIO_BASE, INT_730_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
181 { OMAP730_GPIO1_BASE, INT_730_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_730 },
182 { OMAP730_GPIO2_BASE, INT_730_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_730 },
183 { OMAP730_GPIO3_BASE, INT_730_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_730 },
184 { OMAP730_GPIO4_BASE, INT_730_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_730 },
185 { OMAP730_GPIO5_BASE, INT_730_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_730 },
186 { OMAP730_GPIO6_BASE, INT_730_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_730 },
190 #ifdef CONFIG_ARCH_OMAP24XX
192 static struct gpio_bank gpio_bank_242x[4] = {
193 { OMAP242X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
194 { OMAP242X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
195 { OMAP242X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
196 { OMAP242X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
199 static struct gpio_bank gpio_bank_243x[5] = {
200 { OMAP243X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
201 { OMAP243X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
202 { OMAP243X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
203 { OMAP243X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
204 { OMAP243X_GPIO5_BASE, INT_24XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
209 #ifdef CONFIG_ARCH_OMAP34XX
210 static struct gpio_bank gpio_bank_34xx[6] = {
211 { OMAP34XX_GPIO1_BASE, INT_34XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
212 { OMAP34XX_GPIO2_BASE, INT_34XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
213 { OMAP34XX_GPIO3_BASE, INT_34XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
214 { OMAP34XX_GPIO4_BASE, INT_34XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
215 { OMAP34XX_GPIO5_BASE, INT_34XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
216 { OMAP34XX_GPIO6_BASE, INT_34XX_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_24XX },
221 static struct gpio_bank *gpio_bank;
222 static int gpio_bank_count;
224 static inline struct gpio_bank *get_gpio_bank(int gpio)
226 if (cpu_is_omap15xx()) {
227 if (OMAP_GPIO_IS_MPUIO(gpio))
228 return &gpio_bank[0];
229 return &gpio_bank[1];
231 if (cpu_is_omap16xx()) {
232 if (OMAP_GPIO_IS_MPUIO(gpio))
233 return &gpio_bank[0];
234 return &gpio_bank[1 + (gpio >> 4)];
236 if (cpu_is_omap730()) {
237 if (OMAP_GPIO_IS_MPUIO(gpio))
238 return &gpio_bank[0];
239 return &gpio_bank[1 + (gpio >> 5)];
241 if (cpu_is_omap24xx())
242 return &gpio_bank[gpio >> 5];
243 if (cpu_is_omap34xx())
244 return &gpio_bank[gpio >> 5];
247 static inline int get_gpio_index(int gpio)
249 if (cpu_is_omap730())
251 if (cpu_is_omap24xx())
253 if (cpu_is_omap34xx())
258 static inline int gpio_valid(int gpio)
262 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
263 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
267 if (cpu_is_omap15xx() && gpio < 16)
269 if ((cpu_is_omap16xx()) && gpio < 64)
271 if (cpu_is_omap730() && gpio < 192)
273 if (cpu_is_omap24xx() && gpio < 128)
275 if (cpu_is_omap34xx() && gpio < 160)
280 static int check_gpio(int gpio)
282 if (unlikely(gpio_valid(gpio)) < 0) {
283 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
290 static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
292 void __iomem *reg = bank->base;
295 switch (bank->method) {
296 #ifdef CONFIG_ARCH_OMAP1
298 reg += OMAP_MPUIO_IO_CNTL;
301 #ifdef CONFIG_ARCH_OMAP15XX
302 case METHOD_GPIO_1510:
303 reg += OMAP1510_GPIO_DIR_CONTROL;
306 #ifdef CONFIG_ARCH_OMAP16XX
307 case METHOD_GPIO_1610:
308 reg += OMAP1610_GPIO_DIRECTION;
311 #ifdef CONFIG_ARCH_OMAP730
312 case METHOD_GPIO_730:
313 reg += OMAP730_GPIO_DIR_CONTROL;
316 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
317 case METHOD_GPIO_24XX:
318 reg += OMAP24XX_GPIO_OE;
325 l = __raw_readl(reg);
330 __raw_writel(l, reg);
333 void omap_set_gpio_direction(int gpio, int is_input)
335 struct gpio_bank *bank;
337 if (check_gpio(gpio) < 0)
339 bank = get_gpio_bank(gpio);
340 spin_lock(&bank->lock);
341 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
342 spin_unlock(&bank->lock);
345 static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
347 void __iomem *reg = bank->base;
350 switch (bank->method) {
351 #ifdef CONFIG_ARCH_OMAP1
353 reg += OMAP_MPUIO_OUTPUT;
354 l = __raw_readl(reg);
361 #ifdef CONFIG_ARCH_OMAP15XX
362 case METHOD_GPIO_1510:
363 reg += OMAP1510_GPIO_DATA_OUTPUT;
364 l = __raw_readl(reg);
371 #ifdef CONFIG_ARCH_OMAP16XX
372 case METHOD_GPIO_1610:
374 reg += OMAP1610_GPIO_SET_DATAOUT;
376 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
380 #ifdef CONFIG_ARCH_OMAP730
381 case METHOD_GPIO_730:
382 reg += OMAP730_GPIO_DATA_OUTPUT;
383 l = __raw_readl(reg);
390 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
391 case METHOD_GPIO_24XX:
393 reg += OMAP24XX_GPIO_SETDATAOUT;
395 reg += OMAP24XX_GPIO_CLEARDATAOUT;
403 __raw_writel(l, reg);
406 void omap_set_gpio_dataout(int gpio, int enable)
408 struct gpio_bank *bank;
410 if (check_gpio(gpio) < 0)
412 bank = get_gpio_bank(gpio);
413 spin_lock(&bank->lock);
414 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
415 spin_unlock(&bank->lock);
418 int omap_get_gpio_datain(int gpio)
420 struct gpio_bank *bank;
423 if (check_gpio(gpio) < 0)
425 bank = get_gpio_bank(gpio);
427 switch (bank->method) {
428 #ifdef CONFIG_ARCH_OMAP1
430 reg += OMAP_MPUIO_INPUT_LATCH;
433 #ifdef CONFIG_ARCH_OMAP15XX
434 case METHOD_GPIO_1510:
435 reg += OMAP1510_GPIO_DATA_INPUT;
438 #ifdef CONFIG_ARCH_OMAP16XX
439 case METHOD_GPIO_1610:
440 reg += OMAP1610_GPIO_DATAIN;
443 #ifdef CONFIG_ARCH_OMAP730
444 case METHOD_GPIO_730:
445 reg += OMAP730_GPIO_DATA_INPUT;
448 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
449 case METHOD_GPIO_24XX:
450 reg += OMAP24XX_GPIO_DATAIN;
456 return (__raw_readl(reg)
457 & (1 << get_gpio_index(gpio))) != 0;
460 #define MOD_REG_BIT(reg, bit_mask, set) \
462 int l = __raw_readl(base + reg); \
463 if (set) l |= bit_mask; \
464 else l &= ~bit_mask; \
465 __raw_writel(l, base + reg); \
468 void omap_set_gpio_debounce(int gpio, int enable)
470 struct gpio_bank *bank;
472 u32 val, l = 1 << get_gpio_index(gpio);
474 if (cpu_class_is_omap1())
477 bank = get_gpio_bank(gpio);
480 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
481 val = __raw_readl(reg);
488 __raw_writel(val, reg);
490 EXPORT_SYMBOL(omap_set_gpio_debounce);
492 void omap_set_gpio_debounce_time(int gpio, int enc_time)
494 struct gpio_bank *bank;
497 if (cpu_class_is_omap1())
500 bank = get_gpio_bank(gpio);
504 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
505 __raw_writel(enc_time, reg);
507 EXPORT_SYMBOL(omap_set_gpio_debounce_time);
509 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
510 static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
513 void __iomem *base = bank->base;
514 u32 gpio_bit = 1 << gpio;
516 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
517 trigger & __IRQT_LOWLVL);
518 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
519 trigger & __IRQT_HIGHLVL);
520 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
521 trigger & __IRQT_RISEDGE);
522 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
523 trigger & __IRQT_FALEDGE);
525 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
527 __raw_writel(1 << gpio, bank->base
528 + OMAP24XX_GPIO_SETWKUENA);
530 __raw_writel(1 << gpio, bank->base
531 + OMAP24XX_GPIO_CLEARWKUENA);
534 bank->enabled_non_wakeup_gpios |= gpio_bit;
536 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
540 * FIXME: Possibly do 'set_irq_handler(j, handle_level_irq)' if only
541 * level triggering requested.
546 static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
548 void __iomem *reg = bank->base;
551 switch (bank->method) {
552 #ifdef CONFIG_ARCH_OMAP1
554 reg += OMAP_MPUIO_GPIO_INT_EDGE;
555 l = __raw_readl(reg);
556 if (trigger & __IRQT_RISEDGE)
558 else if (trigger & __IRQT_FALEDGE)
564 #ifdef CONFIG_ARCH_OMAP15XX
565 case METHOD_GPIO_1510:
566 reg += OMAP1510_GPIO_INT_CONTROL;
567 l = __raw_readl(reg);
568 if (trigger & __IRQT_RISEDGE)
570 else if (trigger & __IRQT_FALEDGE)
576 #ifdef CONFIG_ARCH_OMAP16XX
577 case METHOD_GPIO_1610:
579 reg += OMAP1610_GPIO_EDGE_CTRL2;
581 reg += OMAP1610_GPIO_EDGE_CTRL1;
583 l = __raw_readl(reg);
584 l &= ~(3 << (gpio << 1));
585 if (trigger & __IRQT_RISEDGE)
586 l |= 2 << (gpio << 1);
587 if (trigger & __IRQT_FALEDGE)
588 l |= 1 << (gpio << 1);
590 /* Enable wake-up during idle for dynamic tick */
591 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
593 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
596 #ifdef CONFIG_ARCH_OMAP730
597 case METHOD_GPIO_730:
598 reg += OMAP730_GPIO_INT_CONTROL;
599 l = __raw_readl(reg);
600 if (trigger & __IRQT_RISEDGE)
602 else if (trigger & __IRQT_FALEDGE)
608 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
609 case METHOD_GPIO_24XX:
610 set_24xx_gpio_triggering(bank, gpio, trigger);
616 __raw_writel(l, reg);
622 static int gpio_irq_type(unsigned irq, unsigned type)
624 struct gpio_bank *bank;
628 if (!cpu_class_is_omap2() && irq > IH_MPUIO_BASE)
629 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
631 gpio = irq - IH_GPIO_BASE;
633 if (check_gpio(gpio) < 0)
636 if (type & ~IRQ_TYPE_SENSE_MASK)
639 /* OMAP1 allows only only edge triggering */
640 if (!cpu_class_is_omap2()
641 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
644 bank = get_irq_chip_data(irq);
645 spin_lock(&bank->lock);
646 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
648 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
649 irq_desc[irq].status |= type;
651 spin_unlock(&bank->lock);
655 static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
657 void __iomem *reg = bank->base;
659 switch (bank->method) {
660 #ifdef CONFIG_ARCH_OMAP1
662 /* MPUIO irqstatus is reset by reading the status register,
663 * so do nothing here */
666 #ifdef CONFIG_ARCH_OMAP15XX
667 case METHOD_GPIO_1510:
668 reg += OMAP1510_GPIO_INT_STATUS;
671 #ifdef CONFIG_ARCH_OMAP16XX
672 case METHOD_GPIO_1610:
673 reg += OMAP1610_GPIO_IRQSTATUS1;
676 #ifdef CONFIG_ARCH_OMAP730
677 case METHOD_GPIO_730:
678 reg += OMAP730_GPIO_INT_STATUS;
681 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
682 case METHOD_GPIO_24XX:
683 reg += OMAP24XX_GPIO_IRQSTATUS1;
690 __raw_writel(gpio_mask, reg);
692 /* Workaround for clearing DSP GPIO interrupts to allow retention */
693 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
694 if (cpu_is_omap24xx() || cpu_is_omap34xx())
695 __raw_writel(gpio_mask, bank->base + OMAP24XX_GPIO_IRQSTATUS2);
699 static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
701 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
704 static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
706 void __iomem *reg = bank->base;
711 switch (bank->method) {
712 #ifdef CONFIG_ARCH_OMAP1
714 reg += OMAP_MPUIO_GPIO_MASKIT;
719 #ifdef CONFIG_ARCH_OMAP15XX
720 case METHOD_GPIO_1510:
721 reg += OMAP1510_GPIO_INT_MASK;
726 #ifdef CONFIG_ARCH_OMAP16XX
727 case METHOD_GPIO_1610:
728 reg += OMAP1610_GPIO_IRQENABLE1;
732 #ifdef CONFIG_ARCH_OMAP730
733 case METHOD_GPIO_730:
734 reg += OMAP730_GPIO_INT_MASK;
739 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
740 case METHOD_GPIO_24XX:
741 reg += OMAP24XX_GPIO_IRQENABLE1;
750 l = __raw_readl(reg);
757 static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
759 void __iomem *reg = bank->base;
762 switch (bank->method) {
763 #ifdef CONFIG_ARCH_OMAP1
765 reg += OMAP_MPUIO_GPIO_MASKIT;
766 l = __raw_readl(reg);
773 #ifdef CONFIG_ARCH_OMAP15XX
774 case METHOD_GPIO_1510:
775 reg += OMAP1510_GPIO_INT_MASK;
776 l = __raw_readl(reg);
783 #ifdef CONFIG_ARCH_OMAP16XX
784 case METHOD_GPIO_1610:
786 reg += OMAP1610_GPIO_SET_IRQENABLE1;
788 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
792 #ifdef CONFIG_ARCH_OMAP730
793 case METHOD_GPIO_730:
794 reg += OMAP730_GPIO_INT_MASK;
795 l = __raw_readl(reg);
802 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
803 case METHOD_GPIO_24XX:
805 reg += OMAP24XX_GPIO_SETIRQENABLE1;
807 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
815 __raw_writel(l, reg);
818 static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
820 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
824 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
825 * 1510 does not seem to have a wake-up register. If JTAG is connected
826 * to the target, system will wake up always on GPIO events. While
827 * system is running all registered GPIO interrupts need to have wake-up
828 * enabled. When system is suspended, only selected GPIO interrupts need
829 * to have wake-up enabled.
831 static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
833 switch (bank->method) {
834 #ifdef CONFIG_ARCH_OMAP16XX
836 case METHOD_GPIO_1610:
837 spin_lock(&bank->lock);
839 bank->suspend_wakeup |= (1 << gpio);
840 enable_irq_wake(bank->irq);
842 disable_irq_wake(bank->irq);
843 bank->suspend_wakeup &= ~(1 << gpio);
845 spin_unlock(&bank->lock);
848 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
849 case METHOD_GPIO_24XX:
850 if (bank->non_wakeup_gpios & (1 << gpio)) {
851 printk(KERN_ERR "Unable to modify wakeup on "
852 "non-wakeup GPIO%d\n",
853 (bank - gpio_bank) * 32 + gpio);
856 spin_lock(&bank->lock);
858 bank->suspend_wakeup |= (1 << gpio);
859 enable_irq_wake(bank->irq);
861 disable_irq_wake(bank->irq);
862 bank->suspend_wakeup &= ~(1 << gpio);
864 spin_unlock(&bank->lock);
868 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
874 static void _reset_gpio(struct gpio_bank *bank, int gpio)
876 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
877 _set_gpio_irqenable(bank, gpio, 0);
878 _clear_gpio_irqstatus(bank, gpio);
879 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQT_NOEDGE);
882 /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
883 static int gpio_wake_enable(unsigned int irq, unsigned int enable)
885 unsigned int gpio = irq - IH_GPIO_BASE;
886 struct gpio_bank *bank;
889 if (check_gpio(gpio) < 0)
891 bank = get_irq_chip_data(irq);
892 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
897 int omap_request_gpio(int gpio)
899 struct gpio_bank *bank;
901 if (check_gpio(gpio) < 0)
904 bank = get_gpio_bank(gpio);
905 spin_lock(&bank->lock);
906 if (unlikely(bank->reserved_map & (1 << get_gpio_index(gpio)))) {
907 printk(KERN_ERR "omap-gpio: GPIO %d is already reserved!\n", gpio);
909 spin_unlock(&bank->lock);
912 bank->reserved_map |= (1 << get_gpio_index(gpio));
914 /* Set trigger to none. You need to enable the desired trigger with
915 * request_irq() or set_irq_type().
917 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQT_NOEDGE);
919 #ifdef CONFIG_ARCH_OMAP15XX
920 if (bank->method == METHOD_GPIO_1510) {
923 /* Claim the pin for MPU */
924 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
925 __raw_writel(__raw_readl(reg) | (1 << get_gpio_index(gpio)), reg);
928 spin_unlock(&bank->lock);
933 void omap_free_gpio(int gpio)
935 struct gpio_bank *bank;
937 if (check_gpio(gpio) < 0)
939 bank = get_gpio_bank(gpio);
940 spin_lock(&bank->lock);
941 if (unlikely(!(bank->reserved_map & (1 << get_gpio_index(gpio))))) {
942 printk(KERN_ERR "omap-gpio: GPIO %d wasn't reserved!\n", gpio);
944 spin_unlock(&bank->lock);
947 #ifdef CONFIG_ARCH_OMAP16XX
948 if (bank->method == METHOD_GPIO_1610) {
949 /* Disable wake-up during idle for dynamic tick */
950 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
951 __raw_writel(1 << get_gpio_index(gpio), reg);
954 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
955 if (bank->method == METHOD_GPIO_24XX) {
956 /* Disable wake-up during idle for dynamic tick */
957 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
958 __raw_writel(1 << get_gpio_index(gpio), reg);
961 bank->reserved_map &= ~(1 << get_gpio_index(gpio));
962 _reset_gpio(bank, gpio);
963 spin_unlock(&bank->lock);
967 * We need to unmask the GPIO bank interrupt as soon as possible to
968 * avoid missing GPIO interrupts for other lines in the bank.
969 * Then we need to mask-read-clear-unmask the triggered GPIO lines
970 * in the bank to avoid missing nested interrupts for a GPIO line.
971 * If we wait to unmask individual GPIO lines in the bank after the
972 * line's interrupt handler has been run, we may miss some nested
975 static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
977 void __iomem *isr_reg = NULL;
979 unsigned int gpio_irq;
980 struct gpio_bank *bank;
984 desc->chip->ack(irq);
986 bank = get_irq_data(irq);
987 #ifdef CONFIG_ARCH_OMAP1
988 if (bank->method == METHOD_MPUIO)
989 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
991 #ifdef CONFIG_ARCH_OMAP15XX
992 if (bank->method == METHOD_GPIO_1510)
993 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
995 #if defined(CONFIG_ARCH_OMAP16XX)
996 if (bank->method == METHOD_GPIO_1610)
997 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
999 #ifdef CONFIG_ARCH_OMAP730
1000 if (bank->method == METHOD_GPIO_730)
1001 isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
1003 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1004 if (bank->method == METHOD_GPIO_24XX)
1005 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1008 u32 isr_saved, level_mask = 0;
1011 enabled = _get_gpio_irqbank_mask(bank);
1012 isr_saved = isr = __raw_readl(isr_reg) & enabled;
1014 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1017 if (cpu_class_is_omap2()) {
1019 __raw_readl(bank->base +
1020 OMAP24XX_GPIO_LEVELDETECT0) |
1021 __raw_readl(bank->base +
1022 OMAP24XX_GPIO_LEVELDETECT1);
1023 level_mask &= enabled;
1026 /* clear edge sensitive interrupts before handler(s) are
1027 called so that we don't miss any interrupt occurred while
1029 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1030 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1031 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1033 /* if there is only edge sensitive GPIO pin interrupts
1034 configured, we could unmask GPIO bank interrupt immediately */
1035 if (!level_mask && !unmasked) {
1037 desc->chip->unmask(irq);
1045 gpio_irq = bank->virtual_irq_start;
1046 for (; isr != 0; isr >>= 1, gpio_irq++) {
1051 d = irq_desc + gpio_irq;
1052 /* Don't run the handler if it's already running
1053 * or was disabled lazely.
1055 if (unlikely((d->depth ||
1056 (d->status & IRQ_INPROGRESS)))) {
1058 (gpio_irq - bank->virtual_irq_start);
1059 /* The unmasking will be done by
1060 * enable_irq in case it is disabled or
1061 * after returning from the handler if
1062 * it's already running.
1064 _enable_gpio_irqbank(bank, irq_mask, 0);
1066 /* Level triggered interrupts
1067 * won't ever be reentered
1069 BUG_ON(level_mask & irq_mask);
1070 d->status |= IRQ_PENDING;
1075 desc_handle_irq(gpio_irq, d);
1077 if (unlikely((d->status & IRQ_PENDING) && !d->depth)) {
1079 (gpio_irq - bank->virtual_irq_start);
1080 d->status &= ~IRQ_PENDING;
1081 _enable_gpio_irqbank(bank, irq_mask, 1);
1082 retrigger |= irq_mask;
1086 if (cpu_class_is_omap2()) {
1087 /* clear level sensitive interrupts after handler(s) */
1088 _enable_gpio_irqbank(bank, isr_saved & level_mask, 0);
1089 _clear_gpio_irqbank(bank, isr_saved & level_mask);
1090 _enable_gpio_irqbank(bank, isr_saved & level_mask, 1);
1094 /* if bank has any level sensitive GPIO pin interrupt
1095 configured, we must unmask the bank interrupt only after
1096 handler(s) are executed in order to avoid spurious bank
1099 desc->chip->unmask(irq);
1103 static void gpio_irq_shutdown(unsigned int irq)
1105 unsigned int gpio = irq - IH_GPIO_BASE;
1106 struct gpio_bank *bank = get_irq_chip_data(irq);
1108 _reset_gpio(bank, gpio);
1111 static void gpio_ack_irq(unsigned int irq)
1113 unsigned int gpio = irq - IH_GPIO_BASE;
1114 struct gpio_bank *bank = get_irq_chip_data(irq);
1116 _clear_gpio_irqstatus(bank, gpio);
1119 static void gpio_mask_irq(unsigned int irq)
1121 unsigned int gpio = irq - IH_GPIO_BASE;
1122 struct gpio_bank *bank = get_irq_chip_data(irq);
1124 _set_gpio_irqenable(bank, gpio, 0);
1127 static void gpio_unmask_irq(unsigned int irq)
1129 unsigned int gpio = irq - IH_GPIO_BASE;
1130 unsigned int gpio_idx = get_gpio_index(gpio);
1131 struct gpio_bank *bank = get_irq_chip_data(irq);
1133 _set_gpio_irqenable(bank, gpio_idx, 1);
1136 static struct irq_chip gpio_irq_chip = {
1138 .shutdown = gpio_irq_shutdown,
1139 .ack = gpio_ack_irq,
1140 .mask = gpio_mask_irq,
1141 .unmask = gpio_unmask_irq,
1142 .set_type = gpio_irq_type,
1143 .set_wake = gpio_wake_enable,
1146 /*---------------------------------------------------------------------*/
1148 #ifdef CONFIG_ARCH_OMAP1
1150 /* MPUIO uses the always-on 32k clock */
1152 static void mpuio_ack_irq(unsigned int irq)
1154 /* The ISR is reset automatically, so do nothing here. */
1157 static void mpuio_mask_irq(unsigned int irq)
1159 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
1160 struct gpio_bank *bank = get_irq_chip_data(irq);
1162 _set_gpio_irqenable(bank, gpio, 0);
1165 static void mpuio_unmask_irq(unsigned int irq)
1167 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
1168 struct gpio_bank *bank = get_irq_chip_data(irq);
1170 _set_gpio_irqenable(bank, gpio, 1);
1173 static struct irq_chip mpuio_irq_chip = {
1175 .ack = mpuio_ack_irq,
1176 .mask = mpuio_mask_irq,
1177 .unmask = mpuio_unmask_irq,
1178 .set_type = gpio_irq_type,
1179 #ifdef CONFIG_ARCH_OMAP16XX
1180 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1181 .set_wake = gpio_wake_enable,
1186 #define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1189 #ifdef CONFIG_ARCH_OMAP16XX
1191 #include <linux/platform_device.h>
1193 static int omap_mpuio_suspend_late(struct platform_device *pdev, pm_message_t mesg)
1195 struct gpio_bank *bank = platform_get_drvdata(pdev);
1196 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
1198 spin_lock(&bank->lock);
1199 bank->saved_wakeup = __raw_readl(mask_reg);
1200 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
1201 spin_unlock(&bank->lock);
1206 static int omap_mpuio_resume_early(struct platform_device *pdev)
1208 struct gpio_bank *bank = platform_get_drvdata(pdev);
1209 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
1211 spin_lock(&bank->lock);
1212 __raw_writel(bank->saved_wakeup, mask_reg);
1213 spin_unlock(&bank->lock);
1218 /* use platform_driver for this, now that there's no longer any
1219 * point to sys_device (other than not disturbing old code).
1221 static struct platform_driver omap_mpuio_driver = {
1222 .suspend_late = omap_mpuio_suspend_late,
1223 .resume_early = omap_mpuio_resume_early,
1229 static struct platform_device omap_mpuio_device = {
1233 .driver = &omap_mpuio_driver.driver,
1235 /* could list the /proc/iomem resources */
1238 static inline void mpuio_init(void)
1240 platform_set_drvdata(&omap_mpuio_device, &gpio_bank_1610[0]);
1242 if (platform_driver_register(&omap_mpuio_driver) == 0)
1243 (void) platform_device_register(&omap_mpuio_device);
1247 static inline void mpuio_init(void) {}
1252 extern struct irq_chip mpuio_irq_chip;
1254 #define bank_is_mpuio(bank) 0
1255 static inline void mpuio_init(void) {}
1259 /*---------------------------------------------------------------------*/
1261 static int initialized;
1262 #if !defined(CONFIG_ARCH_OMAP3)
1263 static struct clk * gpio_ick;
1266 #if defined(CONFIG_ARCH_OMAP2)
1267 static struct clk * gpio_fck;
1270 #if defined(CONFIG_ARCH_OMAP2430)
1271 static struct clk * gpio5_ick;
1272 static struct clk * gpio5_fck;
1275 #if defined(CONFIG_ARCH_OMAP3)
1276 static struct clk *gpio_fclks[OMAP34XX_NR_GPIOS];
1277 static struct clk *gpio_iclks[OMAP34XX_NR_GPIOS];
1280 static int __init _omap_gpio_init(void)
1283 struct gpio_bank *bank;
1284 #if defined(CONFIG_ARCH_OMAP3)
1290 #if defined(CONFIG_ARCH_OMAP1)
1291 if (cpu_is_omap15xx()) {
1292 gpio_ick = clk_get(NULL, "arm_gpio_ck");
1293 if (IS_ERR(gpio_ick))
1294 printk("Could not get arm_gpio_ck\n");
1296 clk_enable(gpio_ick);
1299 #if defined(CONFIG_ARCH_OMAP2)
1300 if (cpu_class_is_omap2()) {
1301 gpio_ick = clk_get(NULL, "gpios_ick");
1302 if (IS_ERR(gpio_ick))
1303 printk("Could not get gpios_ick\n");
1305 clk_enable(gpio_ick);
1306 gpio_fck = clk_get(NULL, "gpios_fck");
1307 if (IS_ERR(gpio_fck))
1308 printk("Could not get gpios_fck\n");
1310 clk_enable(gpio_fck);
1313 * On 2430 & 3430 GPIO 5 uses CORE L4 ICLK
1315 #if defined(CONFIG_ARCH_OMAP2430)
1316 if (cpu_is_omap2430()) {
1317 gpio5_ick = clk_get(NULL, "gpio5_ick");
1318 if (IS_ERR(gpio5_ick))
1319 printk("Could not get gpio5_ick\n");
1321 clk_enable(gpio5_ick);
1322 gpio5_fck = clk_get(NULL, "gpio5_fck");
1323 if (IS_ERR(gpio5_fck))
1324 printk("Could not get gpio5_fck\n");
1326 clk_enable(gpio5_fck);
1332 #if defined(CONFIG_ARCH_OMAP3)
1333 if (cpu_is_omap34xx()) {
1334 for (i = 0; i < OMAP34XX_NR_GPIOS; i++) {
1335 sprintf(clk_name, "gpio%d_ick", i + 1);
1336 gpio_iclks[i] = clk_get(NULL, clk_name);
1337 if (IS_ERR(gpio_iclks[i]))
1338 printk(KERN_ERR "Could not get %s\n", clk_name);
1340 clk_enable(gpio_iclks[i]);
1341 sprintf(clk_name, "gpio%d_fck", i + 1);
1342 gpio_fclks[i] = clk_get(NULL, clk_name);
1343 if (IS_ERR(gpio_fclks[i]))
1344 printk(KERN_ERR "Could not get %s\n", clk_name);
1346 clk_enable(gpio_fclks[i]);
1352 #ifdef CONFIG_ARCH_OMAP15XX
1353 if (cpu_is_omap15xx()) {
1354 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
1355 gpio_bank_count = 2;
1356 gpio_bank = gpio_bank_1510;
1359 #if defined(CONFIG_ARCH_OMAP16XX)
1360 if (cpu_is_omap16xx()) {
1363 gpio_bank_count = 5;
1364 gpio_bank = gpio_bank_1610;
1365 rev = omap_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
1366 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1367 (rev >> 4) & 0x0f, rev & 0x0f);
1370 #ifdef CONFIG_ARCH_OMAP730
1371 if (cpu_is_omap730()) {
1372 printk(KERN_INFO "OMAP730 GPIO hardware\n");
1373 gpio_bank_count = 7;
1374 gpio_bank = gpio_bank_730;
1378 #ifdef CONFIG_ARCH_OMAP24XX
1379 if (cpu_is_omap242x()) {
1382 gpio_bank_count = 4;
1383 gpio_bank = gpio_bank_242x;
1384 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1385 printk(KERN_INFO "OMAP242x GPIO hardware version %d.%d\n",
1386 (rev >> 4) & 0x0f, rev & 0x0f);
1388 if (cpu_is_omap243x()) {
1391 gpio_bank_count = 5;
1392 gpio_bank = gpio_bank_243x;
1393 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1394 printk(KERN_INFO "OMAP243x GPIO hardware version %d.%d\n",
1395 (rev >> 4) & 0x0f, rev & 0x0f);
1398 #ifdef CONFIG_ARCH_OMAP34XX
1399 if (cpu_is_omap34xx()) {
1402 gpio_bank_count = OMAP34XX_NR_GPIOS;
1403 gpio_bank = gpio_bank_34xx;
1404 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1405 printk(KERN_INFO "OMAP34xx GPIO hardware version %d.%d\n",
1406 (rev >> 4) & 0x0f, rev & 0x0f);
1409 for (i = 0; i < gpio_bank_count; i++) {
1410 int j, gpio_count = 16;
1412 bank = &gpio_bank[i];
1413 bank->reserved_map = 0;
1414 bank->base = IO_ADDRESS(bank->base);
1415 spin_lock_init(&bank->lock);
1416 if (bank_is_mpuio(bank))
1417 omap_writew(0xFFFF, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_MASKIT);
1418 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1419 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
1420 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
1422 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1423 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
1424 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
1425 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
1427 if (cpu_is_omap730() && bank->method == METHOD_GPIO_730) {
1428 __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
1429 __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
1431 gpio_count = 32; /* 730 has 32-bit GPIOs */
1434 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1435 if (bank->method == METHOD_GPIO_24XX) {
1436 static const u32 non_wakeup_gpios[] = {
1437 0xe203ffc0, 0x08700040
1440 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
1441 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
1442 __raw_writew(0x0015, bank->base + OMAP24XX_GPIO_SYSCONFIG);
1444 /* Initialize interface clock ungated, module enabled */
1445 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1446 if (i < ARRAY_SIZE(non_wakeup_gpios))
1447 bank->non_wakeup_gpios = non_wakeup_gpios[i];
1451 for (j = bank->virtual_irq_start;
1452 j < bank->virtual_irq_start + gpio_count; j++) {
1453 set_irq_chip_data(j, bank);
1454 if (bank_is_mpuio(bank))
1455 set_irq_chip(j, &mpuio_irq_chip);
1457 set_irq_chip(j, &gpio_irq_chip);
1458 set_irq_handler(j, handle_simple_irq);
1459 set_irq_flags(j, IRQF_VALID);
1461 set_irq_chained_handler(bank->irq, gpio_irq_handler);
1462 set_irq_data(bank->irq, bank);
1465 /* Enable system clock for GPIO module.
1466 * The CAM_CLK_CTRL *is* really the right place. */
1467 if (cpu_is_omap16xx())
1468 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
1470 /* Enable autoidle for the OCP interface */
1471 if (cpu_is_omap24xx())
1472 omap_writel(1 << 0, 0x48019010);
1473 if (cpu_is_omap34xx())
1474 omap_writel(1 << 0, 0x48306814);
1479 #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1480 static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
1484 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
1487 for (i = 0; i < gpio_bank_count; i++) {
1488 struct gpio_bank *bank = &gpio_bank[i];
1489 void __iomem *wake_status;
1490 void __iomem *wake_clear;
1491 void __iomem *wake_set;
1493 switch (bank->method) {
1494 #ifdef CONFIG_ARCH_OMAP16XX
1495 case METHOD_GPIO_1610:
1496 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1497 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1498 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1501 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1502 case METHOD_GPIO_24XX:
1503 wake_status = bank->base + OMAP24XX_GPIO_SETWKUENA;
1504 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1505 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1512 spin_lock(&bank->lock);
1513 bank->saved_wakeup = __raw_readl(wake_status);
1514 __raw_writel(0xffffffff, wake_clear);
1515 __raw_writel(bank->suspend_wakeup, wake_set);
1516 spin_unlock(&bank->lock);
1522 static int omap_gpio_resume(struct sys_device *dev)
1526 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
1529 for (i = 0; i < gpio_bank_count; i++) {
1530 struct gpio_bank *bank = &gpio_bank[i];
1531 void __iomem *wake_clear;
1532 void __iomem *wake_set;
1534 switch (bank->method) {
1535 #ifdef CONFIG_ARCH_OMAP16XX
1536 case METHOD_GPIO_1610:
1537 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1538 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1541 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1542 case METHOD_GPIO_24XX:
1543 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1544 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1551 spin_lock(&bank->lock);
1552 __raw_writel(0xffffffff, wake_clear);
1553 __raw_writel(bank->saved_wakeup, wake_set);
1554 spin_unlock(&bank->lock);
1560 static struct sysdev_class omap_gpio_sysclass = {
1562 .suspend = omap_gpio_suspend,
1563 .resume = omap_gpio_resume,
1566 static struct sys_device omap_gpio_device = {
1568 .cls = &omap_gpio_sysclass,
1573 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1575 static int workaround_enabled;
1577 void omap2_gpio_prepare_for_retention(void)
1581 /* Remove triggering for all non-wakeup GPIOs. Otherwise spurious
1582 * IRQs will be generated. See OMAP2420 Errata item 1.101. */
1583 for (i = 0; i < gpio_bank_count; i++) {
1584 struct gpio_bank *bank = &gpio_bank[i];
1587 if (!(bank->enabled_non_wakeup_gpios))
1589 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1590 bank->saved_datain = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1591 l1 = __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1592 l2 = __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1594 bank->saved_fallingdetect = l1;
1595 bank->saved_risingdetect = l2;
1596 l1 &= ~bank->enabled_non_wakeup_gpios;
1597 l2 &= ~bank->enabled_non_wakeup_gpios;
1598 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1599 __raw_writel(l1, bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1600 __raw_writel(l2, bank->base + OMAP24XX_GPIO_RISINGDETECT);
1605 workaround_enabled = 0;
1608 workaround_enabled = 1;
1611 void omap2_gpio_resume_after_retention(void)
1615 if (!workaround_enabled)
1617 for (i = 0; i < gpio_bank_count; i++) {
1618 struct gpio_bank *bank = &gpio_bank[i];
1621 if (!(bank->enabled_non_wakeup_gpios))
1623 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1624 __raw_writel(bank->saved_fallingdetect,
1625 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1626 __raw_writel(bank->saved_risingdetect,
1627 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1629 /* Check if any of the non-wakeup interrupt GPIOs have changed
1630 * state. If so, generate an IRQ by software. This is
1631 * horribly racy, but it's the best we can do to work around
1632 * this silicon bug. */
1633 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1634 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1636 l ^= bank->saved_datain;
1637 l &= bank->non_wakeup_gpios;
1640 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1641 old0 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1642 old1 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1643 __raw_writel(old0 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1644 __raw_writel(old1 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1645 __raw_writel(old0, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1646 __raw_writel(old1, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1656 * This may get called early from board specific init
1657 * for boards that have interrupts routed via FPGA.
1659 int __init omap_gpio_init(void)
1662 return _omap_gpio_init();
1667 static int __init omap_gpio_sysinit(void)
1672 ret = _omap_gpio_init();
1676 #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1677 if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
1679 ret = sysdev_class_register(&omap_gpio_sysclass);
1681 ret = sysdev_register(&omap_gpio_device);
1689 EXPORT_SYMBOL(omap_request_gpio);
1690 EXPORT_SYMBOL(omap_free_gpio);
1691 EXPORT_SYMBOL(omap_set_gpio_direction);
1692 EXPORT_SYMBOL(omap_set_gpio_dataout);
1693 EXPORT_SYMBOL(omap_get_gpio_datain);
1695 arch_initcall(omap_gpio_sysinit);
1698 #ifdef CONFIG_DEBUG_FS
1700 #include <linux/debugfs.h>
1701 #include <linux/seq_file.h>
1703 static int gpio_is_input(struct gpio_bank *bank, int mask)
1705 void __iomem *reg = bank->base;
1707 switch (bank->method) {
1709 reg += OMAP_MPUIO_IO_CNTL;
1711 case METHOD_GPIO_1510:
1712 reg += OMAP1510_GPIO_DIR_CONTROL;
1714 case METHOD_GPIO_1610:
1715 reg += OMAP1610_GPIO_DIRECTION;
1717 case METHOD_GPIO_730:
1718 reg += OMAP730_GPIO_DIR_CONTROL;
1720 case METHOD_GPIO_24XX:
1721 reg += OMAP24XX_GPIO_OE;
1724 return __raw_readl(reg) & mask;
1728 static int dbg_gpio_show(struct seq_file *s, void *unused)
1730 unsigned i, j, gpio;
1732 for (i = 0, gpio = 0; i < gpio_bank_count; i++) {
1733 struct gpio_bank *bank = gpio_bank + i;
1734 unsigned bankwidth = 16;
1737 if (bank_is_mpuio(bank))
1738 gpio = OMAP_MPUIO(0);
1739 else if (cpu_class_is_omap2() || cpu_is_omap730())
1742 for (j = 0; j < bankwidth; j++, gpio++, mask <<= 1) {
1743 unsigned irq, value, is_in, irqstat;
1745 if (!(bank->reserved_map & mask))
1748 irq = bank->virtual_irq_start + j;
1749 value = omap_get_gpio_datain(gpio);
1750 is_in = gpio_is_input(bank, mask);
1752 if (bank_is_mpuio(bank))
1753 seq_printf(s, "MPUIO %2d: ", j);
1755 seq_printf(s, "GPIO %3d: ", gpio);
1756 seq_printf(s, "%s %s",
1757 is_in ? "in " : "out",
1758 value ? "hi" : "lo");
1760 irqstat = irq_desc[irq].status;
1761 if (is_in && ((bank->suspend_wakeup & mask)
1762 || irqstat & IRQ_TYPE_SENSE_MASK)) {
1763 char *trigger = NULL;
1765 switch (irqstat & IRQ_TYPE_SENSE_MASK) {
1766 case IRQ_TYPE_EDGE_FALLING:
1767 trigger = "falling";
1769 case IRQ_TYPE_EDGE_RISING:
1772 case IRQ_TYPE_EDGE_BOTH:
1773 trigger = "bothedge";
1775 case IRQ_TYPE_LEVEL_LOW:
1778 case IRQ_TYPE_LEVEL_HIGH:
1782 trigger = "(unspecified)";
1785 seq_printf(s, ", irq-%d %s%s",
1787 (bank->suspend_wakeup & mask)
1790 seq_printf(s, "\n");
1793 if (bank_is_mpuio(bank)) {
1794 seq_printf(s, "\n");
1801 static int dbg_gpio_open(struct inode *inode, struct file *file)
1803 return single_open(file, dbg_gpio_show, &inode->i_private);
1806 static const struct file_operations debug_fops = {
1807 .open = dbg_gpio_open,
1809 .llseek = seq_lseek,
1810 .release = single_release,
1813 static int __init omap_gpio_debuginit(void)
1815 (void) debugfs_create_file("omap_gpio", S_IRUGO,
1816 NULL, NULL, &debug_fops);
1819 late_initcall(omap_gpio_debuginit);