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>
22 #include <mach/hardware.h>
24 #include <mach/irqs.h>
25 #include <mach/gpio.h>
26 #include <asm/mach/irq.h>
29 * OMAP1510 GPIO registers
31 #define OMAP1510_GPIO_BASE IO_ADDRESS(0xfffce000)
32 #define OMAP1510_GPIO_DATA_INPUT 0x00
33 #define OMAP1510_GPIO_DATA_OUTPUT 0x04
34 #define OMAP1510_GPIO_DIR_CONTROL 0x08
35 #define OMAP1510_GPIO_INT_CONTROL 0x0c
36 #define OMAP1510_GPIO_INT_MASK 0x10
37 #define OMAP1510_GPIO_INT_STATUS 0x14
38 #define OMAP1510_GPIO_PIN_CONTROL 0x18
40 #define OMAP1510_IH_GPIO_BASE 64
43 * OMAP1610 specific GPIO registers
45 #define OMAP1610_GPIO1_BASE IO_ADDRESS(0xfffbe400)
46 #define OMAP1610_GPIO2_BASE IO_ADDRESS(0xfffbec00)
47 #define OMAP1610_GPIO3_BASE IO_ADDRESS(0xfffbb400)
48 #define OMAP1610_GPIO4_BASE IO_ADDRESS(0xfffbbc00)
49 #define OMAP1610_GPIO_REVISION 0x0000
50 #define OMAP1610_GPIO_SYSCONFIG 0x0010
51 #define OMAP1610_GPIO_SYSSTATUS 0x0014
52 #define OMAP1610_GPIO_IRQSTATUS1 0x0018
53 #define OMAP1610_GPIO_IRQENABLE1 0x001c
54 #define OMAP1610_GPIO_WAKEUPENABLE 0x0028
55 #define OMAP1610_GPIO_DATAIN 0x002c
56 #define OMAP1610_GPIO_DATAOUT 0x0030
57 #define OMAP1610_GPIO_DIRECTION 0x0034
58 #define OMAP1610_GPIO_EDGE_CTRL1 0x0038
59 #define OMAP1610_GPIO_EDGE_CTRL2 0x003c
60 #define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
61 #define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
62 #define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
63 #define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
64 #define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
65 #define OMAP1610_GPIO_SET_DATAOUT 0x00f0
68 * OMAP730 specific GPIO registers
70 #define OMAP730_GPIO1_BASE IO_ADDRESS(0xfffbc000)
71 #define OMAP730_GPIO2_BASE IO_ADDRESS(0xfffbc800)
72 #define OMAP730_GPIO3_BASE IO_ADDRESS(0xfffbd000)
73 #define OMAP730_GPIO4_BASE IO_ADDRESS(0xfffbd800)
74 #define OMAP730_GPIO5_BASE IO_ADDRESS(0xfffbe000)
75 #define OMAP730_GPIO6_BASE IO_ADDRESS(0xfffbe800)
76 #define OMAP730_GPIO_DATA_INPUT 0x00
77 #define OMAP730_GPIO_DATA_OUTPUT 0x04
78 #define OMAP730_GPIO_DIR_CONTROL 0x08
79 #define OMAP730_GPIO_INT_CONTROL 0x0c
80 #define OMAP730_GPIO_INT_MASK 0x10
81 #define OMAP730_GPIO_INT_STATUS 0x14
84 * omap24xx specific GPIO registers
86 #define OMAP242X_GPIO1_BASE IO_ADDRESS(0x48018000)
87 #define OMAP242X_GPIO2_BASE IO_ADDRESS(0x4801a000)
88 #define OMAP242X_GPIO3_BASE IO_ADDRESS(0x4801c000)
89 #define OMAP242X_GPIO4_BASE IO_ADDRESS(0x4801e000)
91 #define OMAP243X_GPIO1_BASE IO_ADDRESS(0x4900C000)
92 #define OMAP243X_GPIO2_BASE IO_ADDRESS(0x4900E000)
93 #define OMAP243X_GPIO3_BASE IO_ADDRESS(0x49010000)
94 #define OMAP243X_GPIO4_BASE IO_ADDRESS(0x49012000)
95 #define OMAP243X_GPIO5_BASE IO_ADDRESS(0x480B6000)
97 #define OMAP24XX_GPIO_REVISION 0x0000
98 #define OMAP24XX_GPIO_SYSCONFIG 0x0010
99 #define OMAP24XX_GPIO_SYSSTATUS 0x0014
100 #define OMAP24XX_GPIO_IRQSTATUS1 0x0018
101 #define OMAP24XX_GPIO_IRQSTATUS2 0x0028
102 #define OMAP24XX_GPIO_IRQENABLE2 0x002c
103 #define OMAP24XX_GPIO_IRQENABLE1 0x001c
104 #define OMAP24XX_GPIO_WAKE_EN 0x0020
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 IO_ADDRESS(0x48310000)
127 #define OMAP34XX_GPIO2_BASE IO_ADDRESS(0x49050000)
128 #define OMAP34XX_GPIO3_BASE IO_ADDRESS(0x49052000)
129 #define OMAP34XX_GPIO4_BASE IO_ADDRESS(0x49054000)
130 #define OMAP34XX_GPIO5_BASE IO_ADDRESS(0x49056000)
131 #define OMAP34XX_GPIO6_BASE IO_ADDRESS(0x49058000)
133 #define OMAP_MPUIO_VBASE IO_ADDRESS(OMAP_MPUIO_BASE)
138 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;
154 struct gpio_chip chip;
158 #define METHOD_MPUIO 0
159 #define METHOD_GPIO_1510 1
160 #define METHOD_GPIO_1610 2
161 #define METHOD_GPIO_730 3
162 #define METHOD_GPIO_24XX 4
164 #ifdef CONFIG_ARCH_OMAP16XX
165 static struct gpio_bank gpio_bank_1610[5] = {
166 { OMAP_MPUIO_VBASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
167 { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
168 { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
169 { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
170 { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
174 #ifdef CONFIG_ARCH_OMAP15XX
175 static struct gpio_bank gpio_bank_1510[2] = {
176 { OMAP_MPUIO_VBASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
177 { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
181 #ifdef CONFIG_ARCH_OMAP730
182 static struct gpio_bank gpio_bank_730[7] = {
183 { OMAP_MPUIO_VBASE, INT_730_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
184 { OMAP730_GPIO1_BASE, INT_730_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_730 },
185 { OMAP730_GPIO2_BASE, INT_730_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_730 },
186 { OMAP730_GPIO3_BASE, INT_730_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_730 },
187 { OMAP730_GPIO4_BASE, INT_730_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_730 },
188 { OMAP730_GPIO5_BASE, INT_730_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_730 },
189 { OMAP730_GPIO6_BASE, INT_730_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_730 },
193 #ifdef CONFIG_ARCH_OMAP24XX
195 static struct gpio_bank gpio_bank_242x[4] = {
196 { OMAP242X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
197 { OMAP242X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
198 { OMAP242X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
199 { OMAP242X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
202 static struct gpio_bank gpio_bank_243x[5] = {
203 { OMAP243X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
204 { OMAP243X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
205 { OMAP243X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
206 { OMAP243X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
207 { OMAP243X_GPIO5_BASE, INT_24XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
212 #ifdef CONFIG_ARCH_OMAP34XX
213 static struct gpio_bank gpio_bank_34xx[6] = {
214 { OMAP34XX_GPIO1_BASE, INT_34XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
215 { OMAP34XX_GPIO2_BASE, INT_34XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
216 { OMAP34XX_GPIO3_BASE, INT_34XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
217 { OMAP34XX_GPIO4_BASE, INT_34XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
218 { OMAP34XX_GPIO5_BASE, INT_34XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
219 { OMAP34XX_GPIO6_BASE, INT_34XX_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_24XX },
224 static struct gpio_bank *gpio_bank;
225 static int gpio_bank_count;
227 static inline struct gpio_bank *get_gpio_bank(int gpio)
229 if (cpu_is_omap15xx()) {
230 if (OMAP_GPIO_IS_MPUIO(gpio))
231 return &gpio_bank[0];
232 return &gpio_bank[1];
234 if (cpu_is_omap16xx()) {
235 if (OMAP_GPIO_IS_MPUIO(gpio))
236 return &gpio_bank[0];
237 return &gpio_bank[1 + (gpio >> 4)];
239 if (cpu_is_omap730()) {
240 if (OMAP_GPIO_IS_MPUIO(gpio))
241 return &gpio_bank[0];
242 return &gpio_bank[1 + (gpio >> 5)];
244 if (cpu_is_omap24xx())
245 return &gpio_bank[gpio >> 5];
246 if (cpu_is_omap34xx())
247 return &gpio_bank[gpio >> 5];
250 static inline int get_gpio_index(int gpio)
252 if (cpu_is_omap730())
254 if (cpu_is_omap24xx())
256 if (cpu_is_omap34xx())
261 static inline int gpio_valid(int gpio)
265 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
266 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
270 if (cpu_is_omap15xx() && gpio < 16)
272 if ((cpu_is_omap16xx()) && gpio < 64)
274 if (cpu_is_omap730() && gpio < 192)
276 if (cpu_is_omap24xx() && gpio < 128)
278 if (cpu_is_omap34xx() && gpio < 160)
283 static int check_gpio(int gpio)
285 if (unlikely(gpio_valid(gpio)) < 0) {
286 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
293 static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
295 void __iomem *reg = bank->base;
298 switch (bank->method) {
299 #ifdef CONFIG_ARCH_OMAP1
301 reg += OMAP_MPUIO_IO_CNTL;
304 #ifdef CONFIG_ARCH_OMAP15XX
305 case METHOD_GPIO_1510:
306 reg += OMAP1510_GPIO_DIR_CONTROL;
309 #ifdef CONFIG_ARCH_OMAP16XX
310 case METHOD_GPIO_1610:
311 reg += OMAP1610_GPIO_DIRECTION;
314 #ifdef CONFIG_ARCH_OMAP730
315 case METHOD_GPIO_730:
316 reg += OMAP730_GPIO_DIR_CONTROL;
319 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
320 case METHOD_GPIO_24XX:
321 reg += OMAP24XX_GPIO_OE;
328 l = __raw_readl(reg);
333 __raw_writel(l, reg);
336 void omap_set_gpio_direction(int gpio, int is_input)
338 struct gpio_bank *bank;
341 if (check_gpio(gpio) < 0)
343 bank = get_gpio_bank(gpio);
344 spin_lock_irqsave(&bank->lock, flags);
345 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
346 spin_unlock_irqrestore(&bank->lock, flags);
349 static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
351 void __iomem *reg = bank->base;
354 switch (bank->method) {
355 #ifdef CONFIG_ARCH_OMAP1
357 reg += OMAP_MPUIO_OUTPUT;
358 l = __raw_readl(reg);
365 #ifdef CONFIG_ARCH_OMAP15XX
366 case METHOD_GPIO_1510:
367 reg += OMAP1510_GPIO_DATA_OUTPUT;
368 l = __raw_readl(reg);
375 #ifdef CONFIG_ARCH_OMAP16XX
376 case METHOD_GPIO_1610:
378 reg += OMAP1610_GPIO_SET_DATAOUT;
380 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
384 #ifdef CONFIG_ARCH_OMAP730
385 case METHOD_GPIO_730:
386 reg += OMAP730_GPIO_DATA_OUTPUT;
387 l = __raw_readl(reg);
394 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
395 case METHOD_GPIO_24XX:
397 reg += OMAP24XX_GPIO_SETDATAOUT;
399 reg += OMAP24XX_GPIO_CLEARDATAOUT;
407 __raw_writel(l, reg);
410 void omap_set_gpio_dataout(int gpio, int enable)
412 struct gpio_bank *bank;
415 if (check_gpio(gpio) < 0)
417 bank = get_gpio_bank(gpio);
418 spin_lock_irqsave(&bank->lock, flags);
419 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
420 spin_unlock_irqrestore(&bank->lock, flags);
423 int omap_get_gpio_datain(int gpio)
425 struct gpio_bank *bank;
428 if (check_gpio(gpio) < 0)
430 bank = get_gpio_bank(gpio);
432 switch (bank->method) {
433 #ifdef CONFIG_ARCH_OMAP1
435 reg += OMAP_MPUIO_INPUT_LATCH;
438 #ifdef CONFIG_ARCH_OMAP15XX
439 case METHOD_GPIO_1510:
440 reg += OMAP1510_GPIO_DATA_INPUT;
443 #ifdef CONFIG_ARCH_OMAP16XX
444 case METHOD_GPIO_1610:
445 reg += OMAP1610_GPIO_DATAIN;
448 #ifdef CONFIG_ARCH_OMAP730
449 case METHOD_GPIO_730:
450 reg += OMAP730_GPIO_DATA_INPUT;
453 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
454 case METHOD_GPIO_24XX:
455 reg += OMAP24XX_GPIO_DATAIN;
461 return (__raw_readl(reg)
462 & (1 << get_gpio_index(gpio))) != 0;
465 #define MOD_REG_BIT(reg, bit_mask, set) \
467 int l = __raw_readl(base + reg); \
468 if (set) l |= bit_mask; \
469 else l &= ~bit_mask; \
470 __raw_writel(l, base + reg); \
473 void omap_set_gpio_debounce(int gpio, int enable)
475 struct gpio_bank *bank;
477 u32 val, l = 1 << get_gpio_index(gpio);
479 if (cpu_class_is_omap1())
482 bank = get_gpio_bank(gpio);
485 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
486 val = __raw_readl(reg);
488 if (enable && !(val & l))
490 else if (!enable && val & l)
495 if (cpu_is_omap34xx())
496 enable ? clk_enable(bank->dbck) : clk_disable(bank->dbck);
498 __raw_writel(val, reg);
500 EXPORT_SYMBOL(omap_set_gpio_debounce);
502 void omap_set_gpio_debounce_time(int gpio, int enc_time)
504 struct gpio_bank *bank;
507 if (cpu_class_is_omap1())
510 bank = get_gpio_bank(gpio);
514 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
515 __raw_writel(enc_time, reg);
517 EXPORT_SYMBOL(omap_set_gpio_debounce_time);
519 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
520 static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
523 void __iomem *base = bank->base;
524 u32 gpio_bit = 1 << gpio;
526 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
527 trigger & IRQ_TYPE_LEVEL_LOW);
528 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
529 trigger & IRQ_TYPE_LEVEL_HIGH);
530 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
531 trigger & IRQ_TYPE_EDGE_RISING);
532 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
533 trigger & IRQ_TYPE_EDGE_FALLING);
535 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
537 __raw_writel(1 << gpio, bank->base
538 + OMAP24XX_GPIO_SETWKUENA);
540 __raw_writel(1 << gpio, bank->base
541 + OMAP24XX_GPIO_CLEARWKUENA);
544 bank->enabled_non_wakeup_gpios |= gpio_bit;
546 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
550 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
551 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
555 static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
557 void __iomem *reg = bank->base;
560 switch (bank->method) {
561 #ifdef CONFIG_ARCH_OMAP1
563 reg += OMAP_MPUIO_GPIO_INT_EDGE;
564 l = __raw_readl(reg);
565 if (trigger & IRQ_TYPE_EDGE_RISING)
567 else if (trigger & IRQ_TYPE_EDGE_FALLING)
573 #ifdef CONFIG_ARCH_OMAP15XX
574 case METHOD_GPIO_1510:
575 reg += OMAP1510_GPIO_INT_CONTROL;
576 l = __raw_readl(reg);
577 if (trigger & IRQ_TYPE_EDGE_RISING)
579 else if (trigger & IRQ_TYPE_EDGE_FALLING)
585 #ifdef CONFIG_ARCH_OMAP16XX
586 case METHOD_GPIO_1610:
588 reg += OMAP1610_GPIO_EDGE_CTRL2;
590 reg += OMAP1610_GPIO_EDGE_CTRL1;
592 l = __raw_readl(reg);
593 l &= ~(3 << (gpio << 1));
594 if (trigger & IRQ_TYPE_EDGE_RISING)
595 l |= 2 << (gpio << 1);
596 if (trigger & IRQ_TYPE_EDGE_FALLING)
597 l |= 1 << (gpio << 1);
599 /* Enable wake-up during idle for dynamic tick */
600 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
602 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
605 #ifdef CONFIG_ARCH_OMAP730
606 case METHOD_GPIO_730:
607 reg += OMAP730_GPIO_INT_CONTROL;
608 l = __raw_readl(reg);
609 if (trigger & IRQ_TYPE_EDGE_RISING)
611 else if (trigger & IRQ_TYPE_EDGE_FALLING)
617 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
618 case METHOD_GPIO_24XX:
619 set_24xx_gpio_triggering(bank, gpio, trigger);
625 __raw_writel(l, reg);
631 static int gpio_irq_type(unsigned irq, unsigned type)
633 struct gpio_bank *bank;
638 if (!cpu_class_is_omap2() && irq > IH_MPUIO_BASE)
639 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
641 gpio = irq - IH_GPIO_BASE;
643 if (check_gpio(gpio) < 0)
646 if (type & ~IRQ_TYPE_SENSE_MASK)
649 /* OMAP1 allows only only edge triggering */
650 if (!cpu_class_is_omap2()
651 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
654 bank = get_irq_chip_data(irq);
655 spin_lock_irqsave(&bank->lock, flags);
656 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
658 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
659 irq_desc[irq].status |= type;
661 spin_unlock_irqrestore(&bank->lock, flags);
663 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
664 __set_irq_handler_unlocked(irq, handle_level_irq);
665 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
666 __set_irq_handler_unlocked(irq, handle_edge_irq);
671 static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
673 void __iomem *reg = bank->base;
675 switch (bank->method) {
676 #ifdef CONFIG_ARCH_OMAP1
678 /* MPUIO irqstatus is reset by reading the status register,
679 * so do nothing here */
682 #ifdef CONFIG_ARCH_OMAP15XX
683 case METHOD_GPIO_1510:
684 reg += OMAP1510_GPIO_INT_STATUS;
687 #ifdef CONFIG_ARCH_OMAP16XX
688 case METHOD_GPIO_1610:
689 reg += OMAP1610_GPIO_IRQSTATUS1;
692 #ifdef CONFIG_ARCH_OMAP730
693 case METHOD_GPIO_730:
694 reg += OMAP730_GPIO_INT_STATUS;
697 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
698 case METHOD_GPIO_24XX:
699 reg += OMAP24XX_GPIO_IRQSTATUS1;
706 __raw_writel(gpio_mask, reg);
708 /* Workaround for clearing DSP GPIO interrupts to allow retention */
709 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
710 if (cpu_is_omap24xx() || cpu_is_omap34xx())
711 __raw_writel(gpio_mask, bank->base + OMAP24XX_GPIO_IRQSTATUS2);
715 static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
717 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
720 static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
722 void __iomem *reg = bank->base;
727 switch (bank->method) {
728 #ifdef CONFIG_ARCH_OMAP1
730 reg += OMAP_MPUIO_GPIO_MASKIT;
735 #ifdef CONFIG_ARCH_OMAP15XX
736 case METHOD_GPIO_1510:
737 reg += OMAP1510_GPIO_INT_MASK;
742 #ifdef CONFIG_ARCH_OMAP16XX
743 case METHOD_GPIO_1610:
744 reg += OMAP1610_GPIO_IRQENABLE1;
748 #ifdef CONFIG_ARCH_OMAP730
749 case METHOD_GPIO_730:
750 reg += OMAP730_GPIO_INT_MASK;
755 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
756 case METHOD_GPIO_24XX:
757 reg += OMAP24XX_GPIO_IRQENABLE1;
766 l = __raw_readl(reg);
773 static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
775 void __iomem *reg = bank->base;
778 switch (bank->method) {
779 #ifdef CONFIG_ARCH_OMAP1
781 reg += OMAP_MPUIO_GPIO_MASKIT;
782 l = __raw_readl(reg);
789 #ifdef CONFIG_ARCH_OMAP15XX
790 case METHOD_GPIO_1510:
791 reg += OMAP1510_GPIO_INT_MASK;
792 l = __raw_readl(reg);
799 #ifdef CONFIG_ARCH_OMAP16XX
800 case METHOD_GPIO_1610:
802 reg += OMAP1610_GPIO_SET_IRQENABLE1;
804 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
808 #ifdef CONFIG_ARCH_OMAP730
809 case METHOD_GPIO_730:
810 reg += OMAP730_GPIO_INT_MASK;
811 l = __raw_readl(reg);
818 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
819 case METHOD_GPIO_24XX:
821 reg += OMAP24XX_GPIO_SETIRQENABLE1;
823 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
831 __raw_writel(l, reg);
834 static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
836 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
840 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
841 * 1510 does not seem to have a wake-up register. If JTAG is connected
842 * to the target, system will wake up always on GPIO events. While
843 * system is running all registered GPIO interrupts need to have wake-up
844 * enabled. When system is suspended, only selected GPIO interrupts need
845 * to have wake-up enabled.
847 static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
851 switch (bank->method) {
852 #ifdef CONFIG_ARCH_OMAP16XX
854 case METHOD_GPIO_1610:
855 spin_lock_irqsave(&bank->lock, flags);
857 bank->suspend_wakeup |= (1 << gpio);
858 enable_irq_wake(bank->irq);
860 disable_irq_wake(bank->irq);
861 bank->suspend_wakeup &= ~(1 << gpio);
863 spin_unlock_irqrestore(&bank->lock, flags);
866 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
867 case METHOD_GPIO_24XX:
868 if (bank->non_wakeup_gpios & (1 << gpio)) {
869 printk(KERN_ERR "Unable to modify wakeup on "
870 "non-wakeup GPIO%d\n",
871 (bank - gpio_bank) * 32 + gpio);
874 spin_lock_irqsave(&bank->lock, flags);
876 bank->suspend_wakeup |= (1 << gpio);
877 enable_irq_wake(bank->irq);
879 disable_irq_wake(bank->irq);
880 bank->suspend_wakeup &= ~(1 << gpio);
882 spin_unlock_irqrestore(&bank->lock, flags);
886 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
892 static void _reset_gpio(struct gpio_bank *bank, int gpio)
894 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
895 _set_gpio_irqenable(bank, gpio, 0);
896 _clear_gpio_irqstatus(bank, gpio);
897 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
900 /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
901 static int gpio_wake_enable(unsigned int irq, unsigned int enable)
903 unsigned int gpio = irq - IH_GPIO_BASE;
904 struct gpio_bank *bank;
907 if (check_gpio(gpio) < 0)
909 bank = get_irq_chip_data(irq);
910 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
915 int omap_request_gpio(int gpio)
917 struct gpio_bank *bank;
921 if (check_gpio(gpio) < 0)
924 status = gpio_request(gpio, NULL);
928 bank = get_gpio_bank(gpio);
929 spin_lock_irqsave(&bank->lock, flags);
931 /* Set trigger to none. You need to enable the desired trigger with
932 * request_irq() or set_irq_type().
934 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
936 #ifdef CONFIG_ARCH_OMAP15XX
937 if (bank->method == METHOD_GPIO_1510) {
940 /* Claim the pin for MPU */
941 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
942 __raw_writel(__raw_readl(reg) | (1 << get_gpio_index(gpio)), reg);
945 spin_unlock_irqrestore(&bank->lock, flags);
950 void omap_free_gpio(int gpio)
952 struct gpio_bank *bank;
955 if (check_gpio(gpio) < 0)
957 bank = get_gpio_bank(gpio);
958 spin_lock_irqsave(&bank->lock, flags);
959 if (unlikely(!gpiochip_is_requested(&bank->chip,
960 get_gpio_index(gpio)))) {
961 spin_unlock_irqrestore(&bank->lock, flags);
962 printk(KERN_ERR "omap-gpio: GPIO %d wasn't reserved!\n", gpio);
966 #ifdef CONFIG_ARCH_OMAP16XX
967 if (bank->method == METHOD_GPIO_1610) {
968 /* Disable wake-up during idle for dynamic tick */
969 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
970 __raw_writel(1 << get_gpio_index(gpio), reg);
973 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
974 if (bank->method == METHOD_GPIO_24XX) {
975 /* Disable wake-up during idle for dynamic tick */
976 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
977 __raw_writel(1 << get_gpio_index(gpio), reg);
980 _reset_gpio(bank, gpio);
981 spin_unlock_irqrestore(&bank->lock, flags);
986 * We need to unmask the GPIO bank interrupt as soon as possible to
987 * avoid missing GPIO interrupts for other lines in the bank.
988 * Then we need to mask-read-clear-unmask the triggered GPIO lines
989 * in the bank to avoid missing nested interrupts for a GPIO line.
990 * If we wait to unmask individual GPIO lines in the bank after the
991 * line's interrupt handler has been run, we may miss some nested
994 static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
996 void __iomem *isr_reg = NULL;
998 unsigned int gpio_irq;
999 struct gpio_bank *bank;
1003 desc->chip->ack(irq);
1005 bank = get_irq_data(irq);
1006 #ifdef CONFIG_ARCH_OMAP1
1007 if (bank->method == METHOD_MPUIO)
1008 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
1010 #ifdef CONFIG_ARCH_OMAP15XX
1011 if (bank->method == METHOD_GPIO_1510)
1012 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
1014 #if defined(CONFIG_ARCH_OMAP16XX)
1015 if (bank->method == METHOD_GPIO_1610)
1016 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
1018 #ifdef CONFIG_ARCH_OMAP730
1019 if (bank->method == METHOD_GPIO_730)
1020 isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
1022 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1023 if (bank->method == METHOD_GPIO_24XX)
1024 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1027 u32 isr_saved, level_mask = 0;
1030 enabled = _get_gpio_irqbank_mask(bank);
1031 isr_saved = isr = __raw_readl(isr_reg) & enabled;
1033 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1036 if (cpu_class_is_omap2()) {
1037 level_mask = bank->level_mask & enabled;
1040 /* clear edge sensitive interrupts before handler(s) are
1041 called so that we don't miss any interrupt occurred while
1043 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1044 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1045 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1047 /* if there is only edge sensitive GPIO pin interrupts
1048 configured, we could unmask GPIO bank interrupt immediately */
1049 if (!level_mask && !unmasked) {
1051 desc->chip->unmask(irq);
1059 gpio_irq = bank->virtual_irq_start;
1060 for (; isr != 0; isr >>= 1, gpio_irq++) {
1064 generic_handle_irq(gpio_irq);
1067 /* if bank has any level sensitive GPIO pin interrupt
1068 configured, we must unmask the bank interrupt only after
1069 handler(s) are executed in order to avoid spurious bank
1072 desc->chip->unmask(irq);
1076 static void gpio_irq_shutdown(unsigned int irq)
1078 unsigned int gpio = irq - IH_GPIO_BASE;
1079 struct gpio_bank *bank = get_irq_chip_data(irq);
1081 _reset_gpio(bank, gpio);
1084 static void gpio_ack_irq(unsigned int irq)
1086 unsigned int gpio = irq - IH_GPIO_BASE;
1087 struct gpio_bank *bank = get_irq_chip_data(irq);
1089 _clear_gpio_irqstatus(bank, gpio);
1092 static void gpio_mask_irq(unsigned int irq)
1094 unsigned int gpio = irq - IH_GPIO_BASE;
1095 struct gpio_bank *bank = get_irq_chip_data(irq);
1097 _set_gpio_irqenable(bank, gpio, 0);
1100 static void gpio_unmask_irq(unsigned int irq)
1102 unsigned int gpio = irq - IH_GPIO_BASE;
1103 struct gpio_bank *bank = get_irq_chip_data(irq);
1104 unsigned int irq_mask = 1 << get_gpio_index(gpio);
1106 /* For level-triggered GPIOs, the clearing must be done after
1107 * the HW source is cleared, thus after the handler has run */
1108 if (bank->level_mask & irq_mask) {
1109 _set_gpio_irqenable(bank, gpio, 0);
1110 _clear_gpio_irqstatus(bank, gpio);
1113 _set_gpio_irqenable(bank, gpio, 1);
1116 static struct irq_chip gpio_irq_chip = {
1118 .shutdown = gpio_irq_shutdown,
1119 .ack = gpio_ack_irq,
1120 .mask = gpio_mask_irq,
1121 .unmask = gpio_unmask_irq,
1122 .set_type = gpio_irq_type,
1123 .set_wake = gpio_wake_enable,
1126 /*---------------------------------------------------------------------*/
1128 #ifdef CONFIG_ARCH_OMAP1
1130 /* MPUIO uses the always-on 32k clock */
1132 static void mpuio_ack_irq(unsigned int irq)
1134 /* The ISR is reset automatically, so do nothing here. */
1137 static void mpuio_mask_irq(unsigned int irq)
1139 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
1140 struct gpio_bank *bank = get_irq_chip_data(irq);
1142 _set_gpio_irqenable(bank, gpio, 0);
1145 static void mpuio_unmask_irq(unsigned int irq)
1147 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
1148 struct gpio_bank *bank = get_irq_chip_data(irq);
1150 _set_gpio_irqenable(bank, gpio, 1);
1153 static struct irq_chip mpuio_irq_chip = {
1155 .ack = mpuio_ack_irq,
1156 .mask = mpuio_mask_irq,
1157 .unmask = mpuio_unmask_irq,
1158 .set_type = gpio_irq_type,
1159 #ifdef CONFIG_ARCH_OMAP16XX
1160 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1161 .set_wake = gpio_wake_enable,
1166 #define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1169 #ifdef CONFIG_ARCH_OMAP16XX
1171 #include <linux/platform_device.h>
1173 static int omap_mpuio_suspend_late(struct platform_device *pdev, pm_message_t mesg)
1175 struct gpio_bank *bank = platform_get_drvdata(pdev);
1176 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
1177 unsigned long flags;
1179 spin_lock_irqsave(&bank->lock, flags);
1180 bank->saved_wakeup = __raw_readl(mask_reg);
1181 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
1182 spin_unlock_irqrestore(&bank->lock, flags);
1187 static int omap_mpuio_resume_early(struct platform_device *pdev)
1189 struct gpio_bank *bank = platform_get_drvdata(pdev);
1190 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
1191 unsigned long flags;
1193 spin_lock_irqsave(&bank->lock, flags);
1194 __raw_writel(bank->saved_wakeup, mask_reg);
1195 spin_unlock_irqrestore(&bank->lock, flags);
1200 /* use platform_driver for this, now that there's no longer any
1201 * point to sys_device (other than not disturbing old code).
1203 static struct platform_driver omap_mpuio_driver = {
1204 .suspend_late = omap_mpuio_suspend_late,
1205 .resume_early = omap_mpuio_resume_early,
1211 static struct platform_device omap_mpuio_device = {
1215 .driver = &omap_mpuio_driver.driver,
1217 /* could list the /proc/iomem resources */
1220 static inline void mpuio_init(void)
1222 platform_set_drvdata(&omap_mpuio_device, &gpio_bank_1610[0]);
1224 if (platform_driver_register(&omap_mpuio_driver) == 0)
1225 (void) platform_device_register(&omap_mpuio_device);
1229 static inline void mpuio_init(void) {}
1234 extern struct irq_chip mpuio_irq_chip;
1236 #define bank_is_mpuio(bank) 0
1237 static inline void mpuio_init(void) {}
1241 /*---------------------------------------------------------------------*/
1243 /* REVISIT these are stupid implementations! replace by ones that
1244 * don't switch on METHOD_* and which mostly avoid spinlocks
1247 static int gpio_input(struct gpio_chip *chip, unsigned offset)
1249 struct gpio_bank *bank;
1250 unsigned long flags;
1252 bank = container_of(chip, struct gpio_bank, chip);
1253 spin_lock_irqsave(&bank->lock, flags);
1254 _set_gpio_direction(bank, offset, 1);
1255 spin_unlock_irqrestore(&bank->lock, flags);
1259 static int gpio_get(struct gpio_chip *chip, unsigned offset)
1261 return omap_get_gpio_datain(chip->base + offset);
1264 static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1266 struct gpio_bank *bank;
1267 unsigned long flags;
1269 bank = container_of(chip, struct gpio_bank, chip);
1270 spin_lock_irqsave(&bank->lock, flags);
1271 _set_gpio_dataout(bank, offset, value);
1272 _set_gpio_direction(bank, offset, 0);
1273 spin_unlock_irqrestore(&bank->lock, flags);
1277 static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1279 struct gpio_bank *bank;
1280 unsigned long flags;
1282 bank = container_of(chip, struct gpio_bank, chip);
1283 spin_lock_irqsave(&bank->lock, flags);
1284 _set_gpio_dataout(bank, offset, value);
1285 spin_unlock_irqrestore(&bank->lock, flags);
1288 /*---------------------------------------------------------------------*/
1290 static int initialized;
1291 #if !defined(CONFIG_ARCH_OMAP3)
1292 static struct clk * gpio_ick;
1295 #if defined(CONFIG_ARCH_OMAP2)
1296 static struct clk * gpio_fck;
1299 #if defined(CONFIG_ARCH_OMAP2430)
1300 static struct clk * gpio5_ick;
1301 static struct clk * gpio5_fck;
1304 #if defined(CONFIG_ARCH_OMAP3)
1305 static struct clk *gpio_iclks[OMAP34XX_NR_GPIOS];
1308 /* This lock class tells lockdep that GPIO irqs are in a different
1309 * category than their parents, so it won't report false recursion.
1311 static struct lock_class_key gpio_lock_class;
1313 static int __init _omap_gpio_init(void)
1317 struct gpio_bank *bank;
1322 #if defined(CONFIG_ARCH_OMAP1)
1323 if (cpu_is_omap15xx()) {
1324 gpio_ick = clk_get(NULL, "arm_gpio_ck");
1325 if (IS_ERR(gpio_ick))
1326 printk("Could not get arm_gpio_ck\n");
1328 clk_enable(gpio_ick);
1331 #if defined(CONFIG_ARCH_OMAP2)
1332 if (cpu_class_is_omap2()) {
1333 gpio_ick = clk_get(NULL, "gpios_ick");
1334 if (IS_ERR(gpio_ick))
1335 printk("Could not get gpios_ick\n");
1337 clk_enable(gpio_ick);
1338 gpio_fck = clk_get(NULL, "gpios_fck");
1339 if (IS_ERR(gpio_fck))
1340 printk("Could not get gpios_fck\n");
1342 clk_enable(gpio_fck);
1345 * On 2430 & 3430 GPIO 5 uses CORE L4 ICLK
1347 #if defined(CONFIG_ARCH_OMAP2430)
1348 if (cpu_is_omap2430()) {
1349 gpio5_ick = clk_get(NULL, "gpio5_ick");
1350 if (IS_ERR(gpio5_ick))
1351 printk("Could not get gpio5_ick\n");
1353 clk_enable(gpio5_ick);
1354 gpio5_fck = clk_get(NULL, "gpio5_fck");
1355 if (IS_ERR(gpio5_fck))
1356 printk("Could not get gpio5_fck\n");
1358 clk_enable(gpio5_fck);
1364 #if defined(CONFIG_ARCH_OMAP3)
1365 if (cpu_is_omap34xx()) {
1366 for (i = 0; i < OMAP34XX_NR_GPIOS; i++) {
1367 sprintf(clk_name, "gpio%d_ick", i + 1);
1368 gpio_iclks[i] = clk_get(NULL, clk_name);
1369 if (IS_ERR(gpio_iclks[i]))
1370 printk(KERN_ERR "Could not get %s\n", clk_name);
1372 clk_enable(gpio_iclks[i]);
1378 #ifdef CONFIG_ARCH_OMAP15XX
1379 if (cpu_is_omap15xx()) {
1380 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
1381 gpio_bank_count = 2;
1382 gpio_bank = gpio_bank_1510;
1385 #if defined(CONFIG_ARCH_OMAP16XX)
1386 if (cpu_is_omap16xx()) {
1389 gpio_bank_count = 5;
1390 gpio_bank = gpio_bank_1610;
1391 rev = __raw_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
1392 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1393 (rev >> 4) & 0x0f, rev & 0x0f);
1396 #ifdef CONFIG_ARCH_OMAP730
1397 if (cpu_is_omap730()) {
1398 printk(KERN_INFO "OMAP730 GPIO hardware\n");
1399 gpio_bank_count = 7;
1400 gpio_bank = gpio_bank_730;
1404 #ifdef CONFIG_ARCH_OMAP24XX
1405 if (cpu_is_omap242x()) {
1408 gpio_bank_count = 4;
1409 gpio_bank = gpio_bank_242x;
1410 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1411 printk(KERN_INFO "OMAP242x GPIO hardware version %d.%d\n",
1412 (rev >> 4) & 0x0f, rev & 0x0f);
1414 if (cpu_is_omap243x()) {
1417 gpio_bank_count = 5;
1418 gpio_bank = gpio_bank_243x;
1419 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1420 printk(KERN_INFO "OMAP243x GPIO hardware version %d.%d\n",
1421 (rev >> 4) & 0x0f, rev & 0x0f);
1424 #ifdef CONFIG_ARCH_OMAP34XX
1425 if (cpu_is_omap34xx()) {
1428 gpio_bank_count = OMAP34XX_NR_GPIOS;
1429 gpio_bank = gpio_bank_34xx;
1430 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1431 printk(KERN_INFO "OMAP34xx GPIO hardware version %d.%d\n",
1432 (rev >> 4) & 0x0f, rev & 0x0f);
1435 for (i = 0; i < gpio_bank_count; i++) {
1436 int j, gpio_count = 16;
1438 bank = &gpio_bank[i];
1439 spin_lock_init(&bank->lock);
1440 if (bank_is_mpuio(bank))
1441 __raw_writew(0xffff, bank->base + OMAP_MPUIO_GPIO_MASKIT);
1442 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1443 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
1444 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
1446 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1447 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
1448 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
1449 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
1451 if (cpu_is_omap730() && bank->method == METHOD_GPIO_730) {
1452 __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
1453 __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
1455 gpio_count = 32; /* 730 has 32-bit GPIOs */
1458 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1459 if (bank->method == METHOD_GPIO_24XX) {
1460 static const u32 non_wakeup_gpios[] = {
1461 0xe203ffc0, 0x08700040
1464 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
1465 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
1466 __raw_writew(0x0015, bank->base + OMAP24XX_GPIO_SYSCONFIG);
1468 /* Initialize interface clock ungated, module enabled */
1469 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1470 if (i < ARRAY_SIZE(non_wakeup_gpios))
1471 bank->non_wakeup_gpios = non_wakeup_gpios[i];
1476 /* REVISIT eventually switch from OMAP-specific gpio structs
1477 * over to the generic ones
1479 bank->chip.direction_input = gpio_input;
1480 bank->chip.get = gpio_get;
1481 bank->chip.direction_output = gpio_output;
1482 bank->chip.set = gpio_set;
1483 if (bank_is_mpuio(bank)) {
1484 bank->chip.label = "mpuio";
1485 #ifdef CONFIG_ARCH_OMAP16XX
1486 bank->chip.dev = &omap_mpuio_device.dev;
1488 bank->chip.base = OMAP_MPUIO(0);
1490 bank->chip.label = "gpio";
1491 bank->chip.base = gpio;
1494 bank->chip.ngpio = gpio_count;
1496 gpiochip_add(&bank->chip);
1498 for (j = bank->virtual_irq_start;
1499 j < bank->virtual_irq_start + gpio_count; j++) {
1500 lockdep_set_class(&irq_desc[j].lock, &gpio_lock_class);
1501 set_irq_chip_data(j, bank);
1502 if (bank_is_mpuio(bank))
1503 set_irq_chip(j, &mpuio_irq_chip);
1505 set_irq_chip(j, &gpio_irq_chip);
1506 set_irq_handler(j, handle_simple_irq);
1507 set_irq_flags(j, IRQF_VALID);
1509 set_irq_chained_handler(bank->irq, gpio_irq_handler);
1510 set_irq_data(bank->irq, bank);
1512 if (cpu_is_omap34xx()) {
1513 sprintf(clk_name, "gpio%d_dbck", i + 1);
1514 bank->dbck = clk_get(NULL, clk_name);
1515 if (IS_ERR(bank->dbck))
1516 printk(KERN_ERR "Could not get %s\n", clk_name);
1520 /* Enable system clock for GPIO module.
1521 * The CAM_CLK_CTRL *is* really the right place. */
1522 if (cpu_is_omap16xx())
1523 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
1525 /* Enable autoidle for the OCP interface */
1526 if (cpu_is_omap24xx())
1527 omap_writel(1 << 0, 0x48019010);
1528 if (cpu_is_omap34xx())
1529 omap_writel(1 << 0, 0x48306814);
1534 #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1535 static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
1539 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
1542 for (i = 0; i < gpio_bank_count; i++) {
1543 struct gpio_bank *bank = &gpio_bank[i];
1544 void __iomem *wake_status;
1545 void __iomem *wake_clear;
1546 void __iomem *wake_set;
1547 unsigned long flags;
1549 switch (bank->method) {
1550 #ifdef CONFIG_ARCH_OMAP16XX
1551 case METHOD_GPIO_1610:
1552 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1553 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1554 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1557 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1558 case METHOD_GPIO_24XX:
1559 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
1560 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1561 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1568 spin_lock_irqsave(&bank->lock, flags);
1569 bank->saved_wakeup = __raw_readl(wake_status);
1570 __raw_writel(0xffffffff, wake_clear);
1571 __raw_writel(bank->suspend_wakeup, wake_set);
1572 spin_unlock_irqrestore(&bank->lock, flags);
1578 static int omap_gpio_resume(struct sys_device *dev)
1582 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
1585 for (i = 0; i < gpio_bank_count; i++) {
1586 struct gpio_bank *bank = &gpio_bank[i];
1587 void __iomem *wake_clear;
1588 void __iomem *wake_set;
1589 unsigned long flags;
1591 switch (bank->method) {
1592 #ifdef CONFIG_ARCH_OMAP16XX
1593 case METHOD_GPIO_1610:
1594 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1595 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1598 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1599 case METHOD_GPIO_24XX:
1600 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1601 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1608 spin_lock_irqsave(&bank->lock, flags);
1609 __raw_writel(0xffffffff, wake_clear);
1610 __raw_writel(bank->saved_wakeup, wake_set);
1611 spin_unlock_irqrestore(&bank->lock, flags);
1617 static struct sysdev_class omap_gpio_sysclass = {
1619 .suspend = omap_gpio_suspend,
1620 .resume = omap_gpio_resume,
1623 static struct sys_device omap_gpio_device = {
1625 .cls = &omap_gpio_sysclass,
1630 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1632 static int workaround_enabled;
1634 void omap2_gpio_prepare_for_retention(void)
1638 /* Remove triggering for all non-wakeup GPIOs. Otherwise spurious
1639 * IRQs will be generated. See OMAP2420 Errata item 1.101. */
1640 for (i = 0; i < gpio_bank_count; i++) {
1641 struct gpio_bank *bank = &gpio_bank[i];
1644 if (!(bank->enabled_non_wakeup_gpios))
1646 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1647 bank->saved_datain = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1648 l1 = __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1649 l2 = __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1651 bank->saved_fallingdetect = l1;
1652 bank->saved_risingdetect = l2;
1653 l1 &= ~bank->enabled_non_wakeup_gpios;
1654 l2 &= ~bank->enabled_non_wakeup_gpios;
1655 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1656 __raw_writel(l1, bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1657 __raw_writel(l2, bank->base + OMAP24XX_GPIO_RISINGDETECT);
1662 workaround_enabled = 0;
1665 workaround_enabled = 1;
1668 void omap2_gpio_resume_after_retention(void)
1672 if (!workaround_enabled)
1674 for (i = 0; i < gpio_bank_count; i++) {
1675 struct gpio_bank *bank = &gpio_bank[i];
1678 if (!(bank->enabled_non_wakeup_gpios))
1680 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1681 __raw_writel(bank->saved_fallingdetect,
1682 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1683 __raw_writel(bank->saved_risingdetect,
1684 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1686 /* Check if any of the non-wakeup interrupt GPIOs have changed
1687 * state. If so, generate an IRQ by software. This is
1688 * horribly racy, but it's the best we can do to work around
1689 * this silicon bug. */
1690 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1691 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1693 l ^= bank->saved_datain;
1694 l &= bank->non_wakeup_gpios;
1697 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1698 old0 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1699 old1 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1700 __raw_writel(old0 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1701 __raw_writel(old1 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1702 __raw_writel(old0, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1703 __raw_writel(old1, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1713 * This may get called early from board specific init
1714 * for boards that have interrupts routed via FPGA.
1716 int __init omap_gpio_init(void)
1719 return _omap_gpio_init();
1724 static int __init omap_gpio_sysinit(void)
1729 ret = _omap_gpio_init();
1733 #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1734 if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
1736 ret = sysdev_class_register(&omap_gpio_sysclass);
1738 ret = sysdev_register(&omap_gpio_device);
1746 EXPORT_SYMBOL(omap_request_gpio);
1747 EXPORT_SYMBOL(omap_free_gpio);
1748 EXPORT_SYMBOL(omap_set_gpio_direction);
1749 EXPORT_SYMBOL(omap_set_gpio_dataout);
1750 EXPORT_SYMBOL(omap_get_gpio_datain);
1752 arch_initcall(omap_gpio_sysinit);
1755 #ifdef CONFIG_DEBUG_FS
1757 #include <linux/debugfs.h>
1758 #include <linux/seq_file.h>
1760 static int gpio_is_input(struct gpio_bank *bank, int mask)
1762 void __iomem *reg = bank->base;
1764 switch (bank->method) {
1766 reg += OMAP_MPUIO_IO_CNTL;
1768 case METHOD_GPIO_1510:
1769 reg += OMAP1510_GPIO_DIR_CONTROL;
1771 case METHOD_GPIO_1610:
1772 reg += OMAP1610_GPIO_DIRECTION;
1774 case METHOD_GPIO_730:
1775 reg += OMAP730_GPIO_DIR_CONTROL;
1777 case METHOD_GPIO_24XX:
1778 reg += OMAP24XX_GPIO_OE;
1781 return __raw_readl(reg) & mask;
1785 static int dbg_gpio_show(struct seq_file *s, void *unused)
1787 unsigned i, j, gpio;
1789 for (i = 0, gpio = 0; i < gpio_bank_count; i++) {
1790 struct gpio_bank *bank = gpio_bank + i;
1791 unsigned bankwidth = 16;
1794 if (bank_is_mpuio(bank))
1795 gpio = OMAP_MPUIO(0);
1796 else if (cpu_class_is_omap2() || cpu_is_omap730())
1799 for (j = 0; j < bankwidth; j++, gpio++, mask <<= 1) {
1800 unsigned irq, value, is_in, irqstat;
1803 label = gpiochip_is_requested(&bank->chip, j);
1807 irq = bank->virtual_irq_start + j;
1808 value = omap_get_gpio_datain(gpio);
1809 is_in = gpio_is_input(bank, mask);
1811 if (bank_is_mpuio(bank))
1812 seq_printf(s, "MPUIO %2d ", j);
1814 seq_printf(s, "GPIO %3d ", gpio);
1815 seq_printf(s, "(%-20.20s): %s %s",
1817 is_in ? "in " : "out",
1818 value ? "hi" : "lo");
1820 /* FIXME for at least omap2, show pullup/pulldown state */
1822 irqstat = irq_desc[irq].status;
1823 if (is_in && ((bank->suspend_wakeup & mask)
1824 || irqstat & IRQ_TYPE_SENSE_MASK)) {
1825 char *trigger = NULL;
1827 switch (irqstat & IRQ_TYPE_SENSE_MASK) {
1828 case IRQ_TYPE_EDGE_FALLING:
1829 trigger = "falling";
1831 case IRQ_TYPE_EDGE_RISING:
1834 case IRQ_TYPE_EDGE_BOTH:
1835 trigger = "bothedge";
1837 case IRQ_TYPE_LEVEL_LOW:
1840 case IRQ_TYPE_LEVEL_HIGH:
1847 seq_printf(s, ", irq-%d %-8s%s",
1849 (bank->suspend_wakeup & mask)
1852 seq_printf(s, "\n");
1855 if (bank_is_mpuio(bank)) {
1856 seq_printf(s, "\n");
1863 static int dbg_gpio_open(struct inode *inode, struct file *file)
1865 return single_open(file, dbg_gpio_show, &inode->i_private);
1868 static const struct file_operations debug_fops = {
1869 .open = dbg_gpio_open,
1871 .llseek = seq_lseek,
1872 .release = single_release,
1875 static int __init omap_gpio_debuginit(void)
1877 (void) debugfs_create_file("omap_gpio", S_IRUGO,
1878 NULL, NULL, &debug_fops);
1881 late_initcall(omap_gpio_debuginit);