blob: 56889fdb07407d0437da2a078c96a102ff8531c1 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
2 * linux/arch/arm/plat-omap/gpio.c
3 *
4 * Support functions for OMAP GPIO
5 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01006 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02007 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01008 *
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.
12 */
13
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010014#include <linux/init.h>
15#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010016#include <linux/interrupt.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010017#include <linux/sysdev.h>
18#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000019#include <linux/clk.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010020
21#include <asm/hardware.h>
22#include <asm/irq.h>
23#include <asm/arch/irqs.h>
24#include <asm/arch/gpio.h>
25#include <asm/mach/irq.h>
26
27#include <asm/io.h>
28
29/*
30 * OMAP1510 GPIO registers
31 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010032#define OMAP1510_GPIO_BASE (void __iomem *)0xfffce000
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010033#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
40
41#define OMAP1510_IH_GPIO_BASE 64
42
43/*
44 * OMAP1610 specific GPIO registers
45 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010046#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
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010050#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
Tony Lindgren92105bb2005-09-07 17:20:26 +010055#define OMAP1610_GPIO_WAKEUPENABLE 0x0028
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010056#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
Tony Lindgren92105bb2005-09-07 17:20:26 +010062#define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010063#define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
64#define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
Tony Lindgren92105bb2005-09-07 17:20:26 +010065#define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010066#define OMAP1610_GPIO_SET_DATAOUT 0x00f0
67
68/*
69 * OMAP730 specific GPIO registers
70 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010071#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
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010077#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
83
Tony Lindgren92105bb2005-09-07 17:20:26 +010084/*
85 * omap24xx specific GPIO registers
86 */
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -080087#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
91
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
97
Tony Lindgren92105bb2005-09-07 17:20:26 +010098#define OMAP24XX_GPIO_REVISION 0x0000
99#define OMAP24XX_GPIO_SYSCONFIG 0x0010
100#define OMAP24XX_GPIO_SYSSTATUS 0x0014
101#define OMAP24XX_GPIO_IRQSTATUS1 0x0018
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300102#define OMAP24XX_GPIO_IRQSTATUS2 0x0028
103#define OMAP24XX_GPIO_IRQENABLE2 0x002c
Tony Lindgren92105bb2005-09-07 17:20:26 +0100104#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
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700113#define OMAP24XX_GPIO_DEBOUNCE_EN 0x0050
114#define OMAP24XX_GPIO_DEBOUNCE_VAL 0x0054
Tony Lindgren92105bb2005-09-07 17:20:26 +0100115#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
121
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800122/*
123 * omap34xx specific GPIO registers
124 */
125
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
132
133
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134struct gpio_bank {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100135 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136 u16 irq;
137 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100138 int method;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800139#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100140 u32 suspend_wakeup;
141 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800142#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800143#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800144 u32 non_wakeup_gpios;
145 u32 enabled_non_wakeup_gpios;
146
147 u32 saved_datain;
148 u32 saved_fallingdetect;
149 u32 saved_risingdetect;
150#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100151 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -0800152 struct gpio_chip chip;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153};
154
155#define METHOD_MPUIO 0
156#define METHOD_GPIO_1510 1
157#define METHOD_GPIO_1610 2
158#define METHOD_GPIO_730 3
Tony Lindgren92105bb2005-09-07 17:20:26 +0100159#define METHOD_GPIO_24XX 4
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100160
Tony Lindgren92105bb2005-09-07 17:20:26 +0100161#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100162static 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 },
168};
169#endif
170
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000171#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100172static 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 }
175};
176#endif
177
178#ifdef CONFIG_ARCH_OMAP730
179static 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 },
187};
188#endif
189
Tony Lindgren92105bb2005-09-07 17:20:26 +0100190#ifdef CONFIG_ARCH_OMAP24XX
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800191
192static 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 },
Tony Lindgren92105bb2005-09-07 17:20:26 +0100197};
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800198
199static 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 },
205};
206
Tony Lindgren92105bb2005-09-07 17:20:26 +0100207#endif
208
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800209#ifdef CONFIG_ARCH_OMAP34XX
210static 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 },
217};
218
219#endif
220
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100221static struct gpio_bank *gpio_bank;
222static int gpio_bank_count;
223
224static inline struct gpio_bank *get_gpio_bank(int gpio)
225{
Tony Lindgren6e60e792006-04-02 17:46:23 +0100226 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100227 if (OMAP_GPIO_IS_MPUIO(gpio))
228 return &gpio_bank[0];
229 return &gpio_bank[1];
230 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100231 if (cpu_is_omap16xx()) {
232 if (OMAP_GPIO_IS_MPUIO(gpio))
233 return &gpio_bank[0];
234 return &gpio_bank[1 + (gpio >> 4)];
235 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100236 if (cpu_is_omap730()) {
237 if (OMAP_GPIO_IS_MPUIO(gpio))
238 return &gpio_bank[0];
239 return &gpio_bank[1 + (gpio >> 5)];
240 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100241 if (cpu_is_omap24xx())
242 return &gpio_bank[gpio >> 5];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800243 if (cpu_is_omap34xx())
244 return &gpio_bank[gpio >> 5];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100245}
246
247static inline int get_gpio_index(int gpio)
248{
249 if (cpu_is_omap730())
250 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100251 if (cpu_is_omap24xx())
252 return gpio & 0x1f;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800253 if (cpu_is_omap34xx())
254 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100255 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100256}
257
258static inline int gpio_valid(int gpio)
259{
260 if (gpio < 0)
261 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -0800262 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300263 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100264 return -1;
265 return 0;
266 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100267 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100268 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100269 if ((cpu_is_omap16xx()) && gpio < 64)
270 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100271 if (cpu_is_omap730() && gpio < 192)
272 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100273 if (cpu_is_omap24xx() && gpio < 128)
274 return 0;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800275 if (cpu_is_omap34xx() && gpio < 160)
276 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100277 return -1;
278}
279
280static int check_gpio(int gpio)
281{
282 if (unlikely(gpio_valid(gpio)) < 0) {
283 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
284 dump_stack();
285 return -1;
286 }
287 return 0;
288}
289
290static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
291{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100292 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100293 u32 l;
294
295 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800296#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100297 case METHOD_MPUIO:
298 reg += OMAP_MPUIO_IO_CNTL;
299 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800300#endif
301#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100302 case METHOD_GPIO_1510:
303 reg += OMAP1510_GPIO_DIR_CONTROL;
304 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800305#endif
306#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100307 case METHOD_GPIO_1610:
308 reg += OMAP1610_GPIO_DIRECTION;
309 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800310#endif
311#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100312 case METHOD_GPIO_730:
313 reg += OMAP730_GPIO_DIR_CONTROL;
314 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800315#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800316#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100317 case METHOD_GPIO_24XX:
318 reg += OMAP24XX_GPIO_OE;
319 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800320#endif
321 default:
322 WARN_ON(1);
323 return;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100324 }
325 l = __raw_readl(reg);
326 if (is_input)
327 l |= 1 << gpio;
328 else
329 l &= ~(1 << gpio);
330 __raw_writel(l, reg);
331}
332
333void omap_set_gpio_direction(int gpio, int is_input)
334{
335 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800336 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100337
338 if (check_gpio(gpio) < 0)
339 return;
340 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800341 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100342 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
David Brownella6472532008-03-03 04:33:30 -0800343 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100344}
345
346static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
347{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100348 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100349 u32 l = 0;
350
351 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800352#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100353 case METHOD_MPUIO:
354 reg += OMAP_MPUIO_OUTPUT;
355 l = __raw_readl(reg);
356 if (enable)
357 l |= 1 << gpio;
358 else
359 l &= ~(1 << gpio);
360 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800361#endif
362#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100363 case METHOD_GPIO_1510:
364 reg += OMAP1510_GPIO_DATA_OUTPUT;
365 l = __raw_readl(reg);
366 if (enable)
367 l |= 1 << gpio;
368 else
369 l &= ~(1 << gpio);
370 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800371#endif
372#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100373 case METHOD_GPIO_1610:
374 if (enable)
375 reg += OMAP1610_GPIO_SET_DATAOUT;
376 else
377 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
378 l = 1 << gpio;
379 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800380#endif
381#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100382 case METHOD_GPIO_730:
383 reg += OMAP730_GPIO_DATA_OUTPUT;
384 l = __raw_readl(reg);
385 if (enable)
386 l |= 1 << gpio;
387 else
388 l &= ~(1 << gpio);
389 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800390#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800391#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100392 case METHOD_GPIO_24XX:
393 if (enable)
394 reg += OMAP24XX_GPIO_SETDATAOUT;
395 else
396 reg += OMAP24XX_GPIO_CLEARDATAOUT;
397 l = 1 << gpio;
398 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800399#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100400 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800401 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100402 return;
403 }
404 __raw_writel(l, reg);
405}
406
407void omap_set_gpio_dataout(int gpio, int enable)
408{
409 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800410 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100411
412 if (check_gpio(gpio) < 0)
413 return;
414 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800415 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100416 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
David Brownella6472532008-03-03 04:33:30 -0800417 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100418}
419
420int omap_get_gpio_datain(int gpio)
421{
422 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100423 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100424
425 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800426 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100427 bank = get_gpio_bank(gpio);
428 reg = bank->base;
429 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800430#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100431 case METHOD_MPUIO:
432 reg += OMAP_MPUIO_INPUT_LATCH;
433 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800434#endif
435#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100436 case METHOD_GPIO_1510:
437 reg += OMAP1510_GPIO_DATA_INPUT;
438 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800439#endif
440#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100441 case METHOD_GPIO_1610:
442 reg += OMAP1610_GPIO_DATAIN;
443 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800444#endif
445#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100446 case METHOD_GPIO_730:
447 reg += OMAP730_GPIO_DATA_INPUT;
448 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800449#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800450#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100451 case METHOD_GPIO_24XX:
452 reg += OMAP24XX_GPIO_DATAIN;
453 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800454#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100455 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800456 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100457 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100458 return (__raw_readl(reg)
459 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100460}
461
Tony Lindgren92105bb2005-09-07 17:20:26 +0100462#define MOD_REG_BIT(reg, bit_mask, set) \
463do { \
464 int l = __raw_readl(base + reg); \
465 if (set) l |= bit_mask; \
466 else l &= ~bit_mask; \
467 __raw_writel(l, base + reg); \
468} while(0)
469
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700470void omap_set_gpio_debounce(int gpio, int enable)
471{
472 struct gpio_bank *bank;
473 void __iomem *reg;
474 u32 val, l = 1 << get_gpio_index(gpio);
475
476 if (cpu_class_is_omap1())
477 return;
478
479 bank = get_gpio_bank(gpio);
480 reg = bank->base;
481
482 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
483 val = __raw_readl(reg);
484
485 if (enable)
486 val |= l;
487 else
488 val &= ~l;
489
490 __raw_writel(val, reg);
491}
492EXPORT_SYMBOL(omap_set_gpio_debounce);
493
494void omap_set_gpio_debounce_time(int gpio, int enc_time)
495{
496 struct gpio_bank *bank;
497 void __iomem *reg;
498
499 if (cpu_class_is_omap1())
500 return;
501
502 bank = get_gpio_bank(gpio);
503 reg = bank->base;
504
505 enc_time &= 0xff;
506 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
507 __raw_writel(enc_time, reg);
508}
509EXPORT_SYMBOL(omap_set_gpio_debounce_time);
510
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800511#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700512static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
513 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100514{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800515 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100516 u32 gpio_bit = 1 << gpio;
517
518 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100519 trigger & __IRQT_LOWLVL);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100520 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100521 trigger & __IRQT_HIGHLVL);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100522 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100523 trigger & __IRQT_RISEDGE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100524 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100525 trigger & __IRQT_FALEDGE);
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700526
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800527 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
528 if (trigger != 0)
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700529 __raw_writel(1 << gpio, bank->base
530 + OMAP24XX_GPIO_SETWKUENA);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800531 else
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700532 __raw_writel(1 << gpio, bank->base
533 + OMAP24XX_GPIO_CLEARWKUENA);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800534 } else {
535 if (trigger != 0)
536 bank->enabled_non_wakeup_gpios |= gpio_bit;
537 else
538 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
539 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700540
541 /*
542 * FIXME: Possibly do 'set_irq_handler(j, handle_level_irq)' if only
543 * level triggering requested.
544 */
Tony Lindgren92105bb2005-09-07 17:20:26 +0100545}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800546#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100547
548static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
549{
550 void __iomem *reg = bank->base;
551 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100552
553 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800554#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100555 case METHOD_MPUIO:
556 reg += OMAP_MPUIO_GPIO_INT_EDGE;
557 l = __raw_readl(reg);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100558 if (trigger & __IRQT_RISEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100559 l |= 1 << gpio;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100560 else if (trigger & __IRQT_FALEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100561 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100562 else
563 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800565#endif
566#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100567 case METHOD_GPIO_1510:
568 reg += OMAP1510_GPIO_INT_CONTROL;
569 l = __raw_readl(reg);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100570 if (trigger & __IRQT_RISEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100571 l |= 1 << gpio;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100572 else if (trigger & __IRQT_FALEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100574 else
575 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800577#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800578#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100579 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100580 if (gpio & 0x08)
581 reg += OMAP1610_GPIO_EDGE_CTRL2;
582 else
583 reg += OMAP1610_GPIO_EDGE_CTRL1;
584 gpio &= 0x07;
585 l = __raw_readl(reg);
586 l &= ~(3 << (gpio << 1));
Tony Lindgren6e60e792006-04-02 17:46:23 +0100587 if (trigger & __IRQT_RISEDGE)
588 l |= 2 << (gpio << 1);
589 if (trigger & __IRQT_FALEDGE)
590 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800591 if (trigger)
592 /* Enable wake-up during idle for dynamic tick */
593 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
594 else
595 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100596 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800597#endif
598#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100599 case METHOD_GPIO_730:
600 reg += OMAP730_GPIO_INT_CONTROL;
601 l = __raw_readl(reg);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100602 if (trigger & __IRQT_RISEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100603 l |= 1 << gpio;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100604 else if (trigger & __IRQT_FALEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100605 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100606 else
607 goto bad;
608 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800609#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800610#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100611 case METHOD_GPIO_24XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800612 set_24xx_gpio_triggering(bank, gpio, trigger);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100613 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800614#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100615 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100616 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100617 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100618 __raw_writel(l, reg);
619 return 0;
620bad:
621 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100622}
623
Tony Lindgren92105bb2005-09-07 17:20:26 +0100624static int gpio_irq_type(unsigned irq, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100625{
626 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100627 unsigned gpio;
628 int retval;
David Brownella6472532008-03-03 04:33:30 -0800629 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100630
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800631 if (!cpu_class_is_omap2() && irq > IH_MPUIO_BASE)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100632 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
633 else
634 gpio = irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100635
636 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100637 return -EINVAL;
638
David Brownelle5c56ed2006-12-06 17:13:59 -0800639 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100640 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800641
642 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800643 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800644 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100645 return -EINVAL;
646
David Brownell58781012006-12-06 17:14:10 -0800647 bank = get_irq_chip_data(irq);
David Brownella6472532008-03-03 04:33:30 -0800648 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100649 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
David Brownellb9772a22006-12-06 17:13:53 -0800650 if (retval == 0) {
651 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
652 irq_desc[irq].status |= type;
653 }
David Brownella6472532008-03-03 04:33:30 -0800654 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100655 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100656}
657
658static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
659{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100660 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100661
662 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800663#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100664 case METHOD_MPUIO:
665 /* MPUIO irqstatus is reset by reading the status register,
666 * so do nothing here */
667 return;
David Brownelle5c56ed2006-12-06 17:13:59 -0800668#endif
669#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670 case METHOD_GPIO_1510:
671 reg += OMAP1510_GPIO_INT_STATUS;
672 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800673#endif
674#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675 case METHOD_GPIO_1610:
676 reg += OMAP1610_GPIO_IRQSTATUS1;
677 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800678#endif
679#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100680 case METHOD_GPIO_730:
681 reg += OMAP730_GPIO_INT_STATUS;
682 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800683#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800684#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100685 case METHOD_GPIO_24XX:
686 reg += OMAP24XX_GPIO_IRQSTATUS1;
687 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800688#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100689 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800690 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100691 return;
692 }
693 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300694
695 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800696#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
697 if (cpu_is_omap24xx() || cpu_is_omap34xx())
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300698 __raw_writel(gpio_mask, bank->base + OMAP24XX_GPIO_IRQSTATUS2);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800699#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100700}
701
702static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
703{
704 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
705}
706
Imre Deakea6dedd2006-06-26 16:16:00 -0700707static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
708{
709 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700710 int inv = 0;
711 u32 l;
712 u32 mask;
Imre Deakea6dedd2006-06-26 16:16:00 -0700713
714 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800715#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700716 case METHOD_MPUIO:
717 reg += OMAP_MPUIO_GPIO_MASKIT;
Imre Deak99c47702006-06-26 16:16:07 -0700718 mask = 0xffff;
719 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700720 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800721#endif
722#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700723 case METHOD_GPIO_1510:
724 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700725 mask = 0xffff;
726 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700727 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800728#endif
729#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700730 case METHOD_GPIO_1610:
731 reg += OMAP1610_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700732 mask = 0xffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700733 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800734#endif
735#ifdef CONFIG_ARCH_OMAP730
Imre Deakea6dedd2006-06-26 16:16:00 -0700736 case METHOD_GPIO_730:
737 reg += OMAP730_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700738 mask = 0xffffffff;
739 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700740 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800741#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800742#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Imre Deakea6dedd2006-06-26 16:16:00 -0700743 case METHOD_GPIO_24XX:
744 reg += OMAP24XX_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700745 mask = 0xffffffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700746 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800747#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700748 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800749 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700750 return 0;
751 }
752
Imre Deak99c47702006-06-26 16:16:07 -0700753 l = __raw_readl(reg);
754 if (inv)
755 l = ~l;
756 l &= mask;
757 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700758}
759
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100760static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
761{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100762 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100763 u32 l;
764
765 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800766#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100767 case METHOD_MPUIO:
768 reg += OMAP_MPUIO_GPIO_MASKIT;
769 l = __raw_readl(reg);
770 if (enable)
771 l &= ~(gpio_mask);
772 else
773 l |= gpio_mask;
774 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800775#endif
776#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100777 case METHOD_GPIO_1510:
778 reg += OMAP1510_GPIO_INT_MASK;
779 l = __raw_readl(reg);
780 if (enable)
781 l &= ~(gpio_mask);
782 else
783 l |= gpio_mask;
784 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800785#endif
786#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100787 case METHOD_GPIO_1610:
788 if (enable)
789 reg += OMAP1610_GPIO_SET_IRQENABLE1;
790 else
791 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
792 l = gpio_mask;
793 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800794#endif
795#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100796 case METHOD_GPIO_730:
797 reg += OMAP730_GPIO_INT_MASK;
798 l = __raw_readl(reg);
799 if (enable)
800 l &= ~(gpio_mask);
801 else
802 l |= gpio_mask;
803 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800804#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800805#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100806 case METHOD_GPIO_24XX:
807 if (enable)
808 reg += OMAP24XX_GPIO_SETIRQENABLE1;
809 else
810 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
811 l = gpio_mask;
812 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800813#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100814 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800815 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100816 return;
817 }
818 __raw_writel(l, reg);
819}
820
821static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
822{
823 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
824}
825
Tony Lindgren92105bb2005-09-07 17:20:26 +0100826/*
827 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
828 * 1510 does not seem to have a wake-up register. If JTAG is connected
829 * to the target, system will wake up always on GPIO events. While
830 * system is running all registered GPIO interrupts need to have wake-up
831 * enabled. When system is suspended, only selected GPIO interrupts need
832 * to have wake-up enabled.
833 */
834static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
835{
David Brownella6472532008-03-03 04:33:30 -0800836 unsigned long flags;
837
Tony Lindgren92105bb2005-09-07 17:20:26 +0100838 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800839#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800840 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100841 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800842 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800843 if (enable) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100844 bank->suspend_wakeup |= (1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800845 enable_irq_wake(bank->irq);
846 } else {
847 disable_irq_wake(bank->irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100848 bank->suspend_wakeup &= ~(1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800849 }
David Brownella6472532008-03-03 04:33:30 -0800850 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100851 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800852#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800853#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800854 case METHOD_GPIO_24XX:
David Brownell11a78b72006-12-06 17:14:11 -0800855 if (bank->non_wakeup_gpios & (1 << gpio)) {
856 printk(KERN_ERR "Unable to modify wakeup on "
857 "non-wakeup GPIO%d\n",
858 (bank - gpio_bank) * 32 + gpio);
859 return -EINVAL;
860 }
David Brownella6472532008-03-03 04:33:30 -0800861 spin_lock_irqsave(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800862 if (enable) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800863 bank->suspend_wakeup |= (1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800864 enable_irq_wake(bank->irq);
865 } else {
866 disable_irq_wake(bank->irq);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800867 bank->suspend_wakeup &= ~(1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800868 }
David Brownella6472532008-03-03 04:33:30 -0800869 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800870 return 0;
871#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100872 default:
873 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
874 bank->method);
875 return -EINVAL;
876 }
877}
878
Tony Lindgren4196dd62006-09-25 12:41:38 +0300879static void _reset_gpio(struct gpio_bank *bank, int gpio)
880{
881 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
882 _set_gpio_irqenable(bank, gpio, 0);
883 _clear_gpio_irqstatus(bank, gpio);
884 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQT_NOEDGE);
885}
886
Tony Lindgren92105bb2005-09-07 17:20:26 +0100887/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
888static int gpio_wake_enable(unsigned int irq, unsigned int enable)
889{
890 unsigned int gpio = irq - IH_GPIO_BASE;
891 struct gpio_bank *bank;
892 int retval;
893
894 if (check_gpio(gpio) < 0)
895 return -ENODEV;
David Brownell58781012006-12-06 17:14:10 -0800896 bank = get_irq_chip_data(irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100897 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100898
899 return retval;
900}
901
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100902int omap_request_gpio(int gpio)
903{
904 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800905 unsigned long flags;
David Brownell52e31342008-03-03 12:43:23 -0800906 int status;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100907
908 if (check_gpio(gpio) < 0)
909 return -EINVAL;
910
David Brownell52e31342008-03-03 12:43:23 -0800911 status = gpio_request(gpio, NULL);
912 if (status < 0)
913 return status;
914
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100915 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800916 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100917
Tony Lindgren4196dd62006-09-25 12:41:38 +0300918 /* Set trigger to none. You need to enable the desired trigger with
919 * request_irq() or set_irq_type().
920 */
Tony Lindgren92105bb2005-09-07 17:20:26 +0100921 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQT_NOEDGE);
922
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000923#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100924 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100925 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100926
Tony Lindgren92105bb2005-09-07 17:20:26 +0100927 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100928 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
929 __raw_writel(__raw_readl(reg) | (1 << get_gpio_index(gpio)), reg);
930 }
931#endif
David Brownella6472532008-03-03 04:33:30 -0800932 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100933
934 return 0;
935}
936
937void omap_free_gpio(int gpio)
938{
939 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800940 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100941
942 if (check_gpio(gpio) < 0)
943 return;
944 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800945 spin_lock_irqsave(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800946 if (unlikely(!gpiochip_is_requested(&bank->chip,
947 get_gpio_index(gpio)))) {
948 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100949 printk(KERN_ERR "omap-gpio: GPIO %d wasn't reserved!\n", gpio);
950 dump_stack();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100951 return;
952 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100953#ifdef CONFIG_ARCH_OMAP16XX
954 if (bank->method == METHOD_GPIO_1610) {
955 /* Disable wake-up during idle for dynamic tick */
956 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
957 __raw_writel(1 << get_gpio_index(gpio), reg);
958 }
959#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800960#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100961 if (bank->method == METHOD_GPIO_24XX) {
962 /* Disable wake-up during idle for dynamic tick */
963 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
964 __raw_writel(1 << get_gpio_index(gpio), reg);
965 }
966#endif
Tony Lindgren4196dd62006-09-25 12:41:38 +0300967 _reset_gpio(bank, gpio);
David Brownella6472532008-03-03 04:33:30 -0800968 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800969 gpio_free(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100970}
971
972/*
973 * We need to unmask the GPIO bank interrupt as soon as possible to
974 * avoid missing GPIO interrupts for other lines in the bank.
975 * Then we need to mask-read-clear-unmask the triggered GPIO lines
976 * in the bank to avoid missing nested interrupts for a GPIO line.
977 * If we wait to unmask individual GPIO lines in the bank after the
978 * line's interrupt handler has been run, we may miss some nested
979 * interrupts.
980 */
Russell King10dd5ce2006-11-23 11:41:32 +0000981static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100982{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100983 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100984 u32 isr;
985 unsigned int gpio_irq;
986 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700987 u32 retrigger = 0;
988 int unmasked = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100989
990 desc->chip->ack(irq);
991
Thomas Gleixner418ca1f2006-07-01 22:32:41 +0100992 bank = get_irq_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -0800993#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100994 if (bank->method == METHOD_MPUIO)
995 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
David Brownelle5c56ed2006-12-06 17:13:59 -0800996#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000997#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100998 if (bank->method == METHOD_GPIO_1510)
999 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
1000#endif
1001#if defined(CONFIG_ARCH_OMAP16XX)
1002 if (bank->method == METHOD_GPIO_1610)
1003 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
1004#endif
1005#ifdef CONFIG_ARCH_OMAP730
1006 if (bank->method == METHOD_GPIO_730)
1007 isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
1008#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001009#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001010 if (bank->method == METHOD_GPIO_24XX)
1011 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1012#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001013 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001014 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -07001015 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001016
Imre Deakea6dedd2006-06-26 16:16:00 -07001017 enabled = _get_gpio_irqbank_mask(bank);
1018 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001019
1020 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1021 isr &= 0x0000ffff;
1022
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001023 if (cpu_class_is_omap2()) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001024 level_mask =
1025 __raw_readl(bank->base +
1026 OMAP24XX_GPIO_LEVELDETECT0) |
1027 __raw_readl(bank->base +
1028 OMAP24XX_GPIO_LEVELDETECT1);
Imre Deakea6dedd2006-06-26 16:16:00 -07001029 level_mask &= enabled;
1030 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001031
1032 /* clear edge sensitive interrupts before handler(s) are
1033 called so that we don't miss any interrupt occurred while
1034 executing them */
1035 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1036 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1037 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1038
1039 /* if there is only edge sensitive GPIO pin interrupts
1040 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -07001041 if (!level_mask && !unmasked) {
1042 unmasked = 1;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001043 desc->chip->unmask(irq);
Imre Deakea6dedd2006-06-26 16:16:00 -07001044 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001045
Imre Deakea6dedd2006-06-26 16:16:00 -07001046 isr |= retrigger;
1047 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001048 if (!isr)
1049 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001050
Tony Lindgren92105bb2005-09-07 17:20:26 +01001051 gpio_irq = bank->virtual_irq_start;
1052 for (; isr != 0; isr >>= 1, gpio_irq++) {
Russell King10dd5ce2006-11-23 11:41:32 +00001053 struct irq_desc *d;
Imre Deakea6dedd2006-06-26 16:16:00 -07001054 int irq_mask;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001055 if (!(isr & 1))
1056 continue;
1057 d = irq_desc + gpio_irq;
Imre Deakea6dedd2006-06-26 16:16:00 -07001058 /* Don't run the handler if it's already running
1059 * or was disabled lazely.
1060 */
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001061 if (unlikely((d->depth ||
1062 (d->status & IRQ_INPROGRESS)))) {
Imre Deakea6dedd2006-06-26 16:16:00 -07001063 irq_mask = 1 <<
1064 (gpio_irq - bank->virtual_irq_start);
1065 /* The unmasking will be done by
1066 * enable_irq in case it is disabled or
1067 * after returning from the handler if
1068 * it's already running.
1069 */
1070 _enable_gpio_irqbank(bank, irq_mask, 0);
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001071 if (!d->depth) {
Imre Deakea6dedd2006-06-26 16:16:00 -07001072 /* Level triggered interrupts
1073 * won't ever be reentered
1074 */
1075 BUG_ON(level_mask & irq_mask);
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001076 d->status |= IRQ_PENDING;
Imre Deakea6dedd2006-06-26 16:16:00 -07001077 }
1078 continue;
1079 }
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001080
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001081 desc_handle_irq(gpio_irq, d);
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001082
1083 if (unlikely((d->status & IRQ_PENDING) && !d->depth)) {
Imre Deakea6dedd2006-06-26 16:16:00 -07001084 irq_mask = 1 <<
1085 (gpio_irq - bank->virtual_irq_start);
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001086 d->status &= ~IRQ_PENDING;
Imre Deakea6dedd2006-06-26 16:16:00 -07001087 _enable_gpio_irqbank(bank, irq_mask, 1);
1088 retrigger |= irq_mask;
1089 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001090 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001091
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001092 if (cpu_class_is_omap2()) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001093 /* clear level sensitive interrupts after handler(s) */
1094 _enable_gpio_irqbank(bank, isr_saved & level_mask, 0);
1095 _clear_gpio_irqbank(bank, isr_saved & level_mask);
1096 _enable_gpio_irqbank(bank, isr_saved & level_mask, 1);
1097 }
1098
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001099 }
Imre Deakea6dedd2006-06-26 16:16:00 -07001100 /* if bank has any level sensitive GPIO pin interrupt
1101 configured, we must unmask the bank interrupt only after
1102 handler(s) are executed in order to avoid spurious bank
1103 interrupt */
1104 if (!unmasked)
1105 desc->chip->unmask(irq);
1106
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001107}
1108
Tony Lindgren4196dd62006-09-25 12:41:38 +03001109static void gpio_irq_shutdown(unsigned int irq)
1110{
1111 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001112 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001113
1114 _reset_gpio(bank, gpio);
1115}
1116
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001117static void gpio_ack_irq(unsigned int irq)
1118{
1119 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001120 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001121
1122 _clear_gpio_irqstatus(bank, gpio);
1123}
1124
1125static void gpio_mask_irq(unsigned int irq)
1126{
1127 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001128 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001129
1130 _set_gpio_irqenable(bank, gpio, 0);
1131}
1132
1133static void gpio_unmask_irq(unsigned int irq)
1134{
1135 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001136 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001137
Kevin Hilman4de8c752008-01-16 21:56:14 -08001138 _set_gpio_irqenable(bank, gpio, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001139}
1140
David Brownelle5c56ed2006-12-06 17:13:59 -08001141static struct irq_chip gpio_irq_chip = {
1142 .name = "GPIO",
1143 .shutdown = gpio_irq_shutdown,
1144 .ack = gpio_ack_irq,
1145 .mask = gpio_mask_irq,
1146 .unmask = gpio_unmask_irq,
1147 .set_type = gpio_irq_type,
1148 .set_wake = gpio_wake_enable,
1149};
1150
1151/*---------------------------------------------------------------------*/
1152
1153#ifdef CONFIG_ARCH_OMAP1
1154
1155/* MPUIO uses the always-on 32k clock */
1156
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001157static void mpuio_ack_irq(unsigned int irq)
1158{
1159 /* The ISR is reset automatically, so do nothing here. */
1160}
1161
1162static void mpuio_mask_irq(unsigned int irq)
1163{
1164 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001165 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001166
1167 _set_gpio_irqenable(bank, gpio, 0);
1168}
1169
1170static void mpuio_unmask_irq(unsigned int irq)
1171{
1172 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001173 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001174
1175 _set_gpio_irqenable(bank, gpio, 1);
1176}
1177
David Brownelle5c56ed2006-12-06 17:13:59 -08001178static struct irq_chip mpuio_irq_chip = {
1179 .name = "MPUIO",
1180 .ack = mpuio_ack_irq,
1181 .mask = mpuio_mask_irq,
1182 .unmask = mpuio_unmask_irq,
Tony Lindgren92105bb2005-09-07 17:20:26 +01001183 .set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001184#ifdef CONFIG_ARCH_OMAP16XX
1185 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1186 .set_wake = gpio_wake_enable,
1187#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001188};
1189
David Brownelle5c56ed2006-12-06 17:13:59 -08001190
1191#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1192
David Brownell11a78b72006-12-06 17:14:11 -08001193
1194#ifdef CONFIG_ARCH_OMAP16XX
1195
1196#include <linux/platform_device.h>
1197
1198static int omap_mpuio_suspend_late(struct platform_device *pdev, pm_message_t mesg)
1199{
1200 struct gpio_bank *bank = platform_get_drvdata(pdev);
1201 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001202 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001203
David Brownella6472532008-03-03 04:33:30 -08001204 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001205 bank->saved_wakeup = __raw_readl(mask_reg);
1206 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001207 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001208
1209 return 0;
1210}
1211
1212static int omap_mpuio_resume_early(struct platform_device *pdev)
1213{
1214 struct gpio_bank *bank = platform_get_drvdata(pdev);
1215 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001216 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001217
David Brownella6472532008-03-03 04:33:30 -08001218 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001219 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001220 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001221
1222 return 0;
1223}
1224
1225/* use platform_driver for this, now that there's no longer any
1226 * point to sys_device (other than not disturbing old code).
1227 */
1228static struct platform_driver omap_mpuio_driver = {
1229 .suspend_late = omap_mpuio_suspend_late,
1230 .resume_early = omap_mpuio_resume_early,
1231 .driver = {
1232 .name = "mpuio",
1233 },
1234};
1235
1236static struct platform_device omap_mpuio_device = {
1237 .name = "mpuio",
1238 .id = -1,
1239 .dev = {
1240 .driver = &omap_mpuio_driver.driver,
1241 }
1242 /* could list the /proc/iomem resources */
1243};
1244
1245static inline void mpuio_init(void)
1246{
David Brownellfcf126d2007-04-02 12:46:47 -07001247 platform_set_drvdata(&omap_mpuio_device, &gpio_bank_1610[0]);
1248
David Brownell11a78b72006-12-06 17:14:11 -08001249 if (platform_driver_register(&omap_mpuio_driver) == 0)
1250 (void) platform_device_register(&omap_mpuio_device);
1251}
1252
1253#else
1254static inline void mpuio_init(void) {}
1255#endif /* 16xx */
1256
David Brownelle5c56ed2006-12-06 17:13:59 -08001257#else
1258
1259extern struct irq_chip mpuio_irq_chip;
1260
1261#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001262static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001263
1264#endif
1265
1266/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001267
David Brownell52e31342008-03-03 12:43:23 -08001268/* REVISIT these are stupid implementations! replace by ones that
1269 * don't switch on METHOD_* and which mostly avoid spinlocks
1270 */
1271
1272static int gpio_input(struct gpio_chip *chip, unsigned offset)
1273{
1274 struct gpio_bank *bank;
1275 unsigned long flags;
1276
1277 bank = container_of(chip, struct gpio_bank, chip);
1278 spin_lock_irqsave(&bank->lock, flags);
1279 _set_gpio_direction(bank, offset, 1);
1280 spin_unlock_irqrestore(&bank->lock, flags);
1281 return 0;
1282}
1283
1284static int gpio_get(struct gpio_chip *chip, unsigned offset)
1285{
1286 return omap_get_gpio_datain(chip->base + offset);
1287}
1288
1289static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1290{
1291 struct gpio_bank *bank;
1292 unsigned long flags;
1293
1294 bank = container_of(chip, struct gpio_bank, chip);
1295 spin_lock_irqsave(&bank->lock, flags);
1296 _set_gpio_dataout(bank, offset, value);
1297 _set_gpio_direction(bank, offset, 0);
1298 spin_unlock_irqrestore(&bank->lock, flags);
1299 return 0;
1300}
1301
1302static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1303{
1304 struct gpio_bank *bank;
1305 unsigned long flags;
1306
1307 bank = container_of(chip, struct gpio_bank, chip);
1308 spin_lock_irqsave(&bank->lock, flags);
1309 _set_gpio_dataout(bank, offset, value);
1310 spin_unlock_irqrestore(&bank->lock, flags);
1311}
1312
1313/*---------------------------------------------------------------------*/
1314
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001315static int initialized;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001316#if !defined(CONFIG_ARCH_OMAP3)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001317static struct clk * gpio_ick;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001318#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001319
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001320#if defined(CONFIG_ARCH_OMAP2)
1321static struct clk * gpio_fck;
1322#endif
1323
1324#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001325static struct clk * gpio5_ick;
1326static struct clk * gpio5_fck;
1327#endif
1328
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001329#if defined(CONFIG_ARCH_OMAP3)
1330static struct clk *gpio_fclks[OMAP34XX_NR_GPIOS];
1331static struct clk *gpio_iclks[OMAP34XX_NR_GPIOS];
1332#endif
1333
David Brownell8ba55c52008-02-26 11:10:50 -08001334/* This lock class tells lockdep that GPIO irqs are in a different
1335 * category than their parents, so it won't report false recursion.
1336 */
1337static struct lock_class_key gpio_lock_class;
1338
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001339static int __init _omap_gpio_init(void)
1340{
1341 int i;
David Brownell52e31342008-03-03 12:43:23 -08001342 int gpio = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001343 struct gpio_bank *bank;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001344#if defined(CONFIG_ARCH_OMAP3)
1345 char clk_name[11];
1346#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001347
1348 initialized = 1;
1349
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001350#if defined(CONFIG_ARCH_OMAP1)
Tony Lindgren6e60e792006-04-02 17:46:23 +01001351 if (cpu_is_omap15xx()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001352 gpio_ick = clk_get(NULL, "arm_gpio_ck");
1353 if (IS_ERR(gpio_ick))
Tony Lindgren92105bb2005-09-07 17:20:26 +01001354 printk("Could not get arm_gpio_ck\n");
1355 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001356 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001357 }
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001358#endif
1359#if defined(CONFIG_ARCH_OMAP2)
1360 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001361 gpio_ick = clk_get(NULL, "gpios_ick");
1362 if (IS_ERR(gpio_ick))
1363 printk("Could not get gpios_ick\n");
1364 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001365 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001366 gpio_fck = clk_get(NULL, "gpios_fck");
Komal Shah1630b522006-09-25 12:51:08 +03001367 if (IS_ERR(gpio_fck))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001368 printk("Could not get gpios_fck\n");
1369 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001370 clk_enable(gpio_fck);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001371
1372 /*
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001373 * On 2430 & 3430 GPIO 5 uses CORE L4 ICLK
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001374 */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001375#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001376 if (cpu_is_omap2430()) {
1377 gpio5_ick = clk_get(NULL, "gpio5_ick");
1378 if (IS_ERR(gpio5_ick))
1379 printk("Could not get gpio5_ick\n");
1380 else
1381 clk_enable(gpio5_ick);
1382 gpio5_fck = clk_get(NULL, "gpio5_fck");
1383 if (IS_ERR(gpio5_fck))
1384 printk("Could not get gpio5_fck\n");
1385 else
1386 clk_enable(gpio5_fck);
1387 }
1388#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001389 }
1390#endif
1391
1392#if defined(CONFIG_ARCH_OMAP3)
1393 if (cpu_is_omap34xx()) {
1394 for (i = 0; i < OMAP34XX_NR_GPIOS; i++) {
1395 sprintf(clk_name, "gpio%d_ick", i + 1);
1396 gpio_iclks[i] = clk_get(NULL, clk_name);
1397 if (IS_ERR(gpio_iclks[i]))
1398 printk(KERN_ERR "Could not get %s\n", clk_name);
1399 else
1400 clk_enable(gpio_iclks[i]);
1401 sprintf(clk_name, "gpio%d_fck", i + 1);
1402 gpio_fclks[i] = clk_get(NULL, clk_name);
1403 if (IS_ERR(gpio_fclks[i]))
1404 printk(KERN_ERR "Could not get %s\n", clk_name);
1405 else
1406 clk_enable(gpio_fclks[i]);
1407 }
1408 }
1409#endif
1410
Tony Lindgren92105bb2005-09-07 17:20:26 +01001411
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001412#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +01001413 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001414 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
1415 gpio_bank_count = 2;
1416 gpio_bank = gpio_bank_1510;
1417 }
1418#endif
1419#if defined(CONFIG_ARCH_OMAP16XX)
1420 if (cpu_is_omap16xx()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001421 u32 rev;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001422
1423 gpio_bank_count = 5;
1424 gpio_bank = gpio_bank_1610;
1425 rev = omap_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
1426 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1427 (rev >> 4) & 0x0f, rev & 0x0f);
1428 }
1429#endif
1430#ifdef CONFIG_ARCH_OMAP730
1431 if (cpu_is_omap730()) {
1432 printk(KERN_INFO "OMAP730 GPIO hardware\n");
1433 gpio_bank_count = 7;
1434 gpio_bank = gpio_bank_730;
1435 }
1436#endif
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001437
Tony Lindgren92105bb2005-09-07 17:20:26 +01001438#ifdef CONFIG_ARCH_OMAP24XX
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001439 if (cpu_is_omap242x()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001440 int rev;
1441
1442 gpio_bank_count = 4;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001443 gpio_bank = gpio_bank_242x;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001444 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001445 printk(KERN_INFO "OMAP242x GPIO hardware version %d.%d\n",
1446 (rev >> 4) & 0x0f, rev & 0x0f);
1447 }
1448 if (cpu_is_omap243x()) {
1449 int rev;
1450
1451 gpio_bank_count = 5;
1452 gpio_bank = gpio_bank_243x;
1453 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1454 printk(KERN_INFO "OMAP243x GPIO hardware version %d.%d\n",
Tony Lindgren92105bb2005-09-07 17:20:26 +01001455 (rev >> 4) & 0x0f, rev & 0x0f);
1456 }
1457#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001458#ifdef CONFIG_ARCH_OMAP34XX
1459 if (cpu_is_omap34xx()) {
1460 int rev;
1461
1462 gpio_bank_count = OMAP34XX_NR_GPIOS;
1463 gpio_bank = gpio_bank_34xx;
1464 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1465 printk(KERN_INFO "OMAP34xx GPIO hardware version %d.%d\n",
1466 (rev >> 4) & 0x0f, rev & 0x0f);
1467 }
1468#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001469 for (i = 0; i < gpio_bank_count; i++) {
1470 int j, gpio_count = 16;
1471
1472 bank = &gpio_bank[i];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001473 bank->base = IO_ADDRESS(bank->base);
1474 spin_lock_init(&bank->lock);
David Brownelle5c56ed2006-12-06 17:13:59 -08001475 if (bank_is_mpuio(bank))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001476 omap_writew(0xFFFF, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_MASKIT);
Tony Lindgrend11ac972008-01-12 15:35:04 -08001477 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001478 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
1479 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
1480 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001481 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001482 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
1483 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001484 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001485 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001486 if (cpu_is_omap730() && bank->method == METHOD_GPIO_730) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001487 __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
1488 __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
1489
1490 gpio_count = 32; /* 730 has 32-bit GPIOs */
1491 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001492
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001493#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001494 if (bank->method == METHOD_GPIO_24XX) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001495 static const u32 non_wakeup_gpios[] = {
1496 0xe203ffc0, 0x08700040
1497 };
1498
Tony Lindgren92105bb2005-09-07 17:20:26 +01001499 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
1500 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001501 __raw_writew(0x0015, bank->base + OMAP24XX_GPIO_SYSCONFIG);
1502
1503 /* Initialize interface clock ungated, module enabled */
1504 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001505 if (i < ARRAY_SIZE(non_wakeup_gpios))
1506 bank->non_wakeup_gpios = non_wakeup_gpios[i];
Tony Lindgren92105bb2005-09-07 17:20:26 +01001507 gpio_count = 32;
1508 }
1509#endif
David Brownell52e31342008-03-03 12:43:23 -08001510
1511 /* REVISIT eventually switch from OMAP-specific gpio structs
1512 * over to the generic ones
1513 */
1514 bank->chip.direction_input = gpio_input;
1515 bank->chip.get = gpio_get;
1516 bank->chip.direction_output = gpio_output;
1517 bank->chip.set = gpio_set;
1518 if (bank_is_mpuio(bank)) {
1519 bank->chip.label = "mpuio";
1520 bank->chip.base = OMAP_MPUIO(0);
1521 } else {
1522 bank->chip.label = "gpio";
1523 bank->chip.base = gpio;
1524 gpio += gpio_count;
1525 }
1526 bank->chip.ngpio = gpio_count;
1527
1528 gpiochip_add(&bank->chip);
1529
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001530 for (j = bank->virtual_irq_start;
1531 j < bank->virtual_irq_start + gpio_count; j++) {
David Brownell8ba55c52008-02-26 11:10:50 -08001532 lockdep_set_class(&irq_desc[j].lock, &gpio_lock_class);
David Brownell58781012006-12-06 17:14:10 -08001533 set_irq_chip_data(j, bank);
David Brownelle5c56ed2006-12-06 17:13:59 -08001534 if (bank_is_mpuio(bank))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001535 set_irq_chip(j, &mpuio_irq_chip);
1536 else
1537 set_irq_chip(j, &gpio_irq_chip);
Russell King10dd5ce2006-11-23 11:41:32 +00001538 set_irq_handler(j, handle_simple_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001539 set_irq_flags(j, IRQF_VALID);
1540 }
1541 set_irq_chained_handler(bank->irq, gpio_irq_handler);
1542 set_irq_data(bank->irq, bank);
1543 }
1544
1545 /* Enable system clock for GPIO module.
1546 * The CAM_CLK_CTRL *is* really the right place. */
Tony Lindgren92105bb2005-09-07 17:20:26 +01001547 if (cpu_is_omap16xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001548 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
1549
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001550 /* Enable autoidle for the OCP interface */
1551 if (cpu_is_omap24xx())
1552 omap_writel(1 << 0, 0x48019010);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001553 if (cpu_is_omap34xx())
1554 omap_writel(1 << 0, 0x48306814);
Tony Lindgrend11ac972008-01-12 15:35:04 -08001555
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001556 return 0;
1557}
1558
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001559#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001560static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
1561{
1562 int i;
1563
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001564 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001565 return 0;
1566
1567 for (i = 0; i < gpio_bank_count; i++) {
1568 struct gpio_bank *bank = &gpio_bank[i];
1569 void __iomem *wake_status;
1570 void __iomem *wake_clear;
1571 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001572 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001573
1574 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001575#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001576 case METHOD_GPIO_1610:
1577 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1578 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1579 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1580 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001581#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001582#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001583 case METHOD_GPIO_24XX:
1584 wake_status = bank->base + OMAP24XX_GPIO_SETWKUENA;
1585 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1586 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1587 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001588#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001589 default:
1590 continue;
1591 }
1592
David Brownella6472532008-03-03 04:33:30 -08001593 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001594 bank->saved_wakeup = __raw_readl(wake_status);
1595 __raw_writel(0xffffffff, wake_clear);
1596 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001597 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001598 }
1599
1600 return 0;
1601}
1602
1603static int omap_gpio_resume(struct sys_device *dev)
1604{
1605 int i;
1606
1607 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
1608 return 0;
1609
1610 for (i = 0; i < gpio_bank_count; i++) {
1611 struct gpio_bank *bank = &gpio_bank[i];
1612 void __iomem *wake_clear;
1613 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001614 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001615
1616 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001617#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001618 case METHOD_GPIO_1610:
1619 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1620 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1621 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001622#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001623#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001624 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001625 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1626 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001627 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001628#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001629 default:
1630 continue;
1631 }
1632
David Brownella6472532008-03-03 04:33:30 -08001633 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001634 __raw_writel(0xffffffff, wake_clear);
1635 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001636 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001637 }
1638
1639 return 0;
1640}
1641
1642static struct sysdev_class omap_gpio_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001643 .name = "gpio",
Tony Lindgren92105bb2005-09-07 17:20:26 +01001644 .suspend = omap_gpio_suspend,
1645 .resume = omap_gpio_resume,
1646};
1647
1648static struct sys_device omap_gpio_device = {
1649 .id = 0,
1650 .cls = &omap_gpio_sysclass,
1651};
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001652
1653#endif
1654
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001655#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001656
1657static int workaround_enabled;
1658
1659void omap2_gpio_prepare_for_retention(void)
1660{
1661 int i, c = 0;
1662
1663 /* Remove triggering for all non-wakeup GPIOs. Otherwise spurious
1664 * IRQs will be generated. See OMAP2420 Errata item 1.101. */
1665 for (i = 0; i < gpio_bank_count; i++) {
1666 struct gpio_bank *bank = &gpio_bank[i];
1667 u32 l1, l2;
1668
1669 if (!(bank->enabled_non_wakeup_gpios))
1670 continue;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001671#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001672 bank->saved_datain = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1673 l1 = __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1674 l2 = __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001675#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001676 bank->saved_fallingdetect = l1;
1677 bank->saved_risingdetect = l2;
1678 l1 &= ~bank->enabled_non_wakeup_gpios;
1679 l2 &= ~bank->enabled_non_wakeup_gpios;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001680#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001681 __raw_writel(l1, bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1682 __raw_writel(l2, bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001683#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001684 c++;
1685 }
1686 if (!c) {
1687 workaround_enabled = 0;
1688 return;
1689 }
1690 workaround_enabled = 1;
1691}
1692
1693void omap2_gpio_resume_after_retention(void)
1694{
1695 int i;
1696
1697 if (!workaround_enabled)
1698 return;
1699 for (i = 0; i < gpio_bank_count; i++) {
1700 struct gpio_bank *bank = &gpio_bank[i];
1701 u32 l;
1702
1703 if (!(bank->enabled_non_wakeup_gpios))
1704 continue;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001705#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001706 __raw_writel(bank->saved_fallingdetect,
1707 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1708 __raw_writel(bank->saved_risingdetect,
1709 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001710#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001711 /* Check if any of the non-wakeup interrupt GPIOs have changed
1712 * state. If so, generate an IRQ by software. This is
1713 * horribly racy, but it's the best we can do to work around
1714 * this silicon bug. */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001715#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001716 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001717#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001718 l ^= bank->saved_datain;
1719 l &= bank->non_wakeup_gpios;
1720 if (l) {
1721 u32 old0, old1;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001722#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001723 old0 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1724 old1 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1725 __raw_writel(old0 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1726 __raw_writel(old1 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1727 __raw_writel(old0, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1728 __raw_writel(old1, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001729#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001730 }
1731 }
1732
1733}
1734
Tony Lindgren92105bb2005-09-07 17:20:26 +01001735#endif
1736
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001737/*
1738 * This may get called early from board specific init
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001739 * for boards that have interrupts routed via FPGA.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001740 */
David Brownell277d58e2006-12-06 17:13:59 -08001741int __init omap_gpio_init(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001742{
1743 if (!initialized)
1744 return _omap_gpio_init();
1745 else
1746 return 0;
1747}
1748
Tony Lindgren92105bb2005-09-07 17:20:26 +01001749static int __init omap_gpio_sysinit(void)
1750{
1751 int ret = 0;
1752
1753 if (!initialized)
1754 ret = _omap_gpio_init();
1755
David Brownell11a78b72006-12-06 17:14:11 -08001756 mpuio_init();
1757
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001758#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1759 if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001760 if (ret == 0) {
1761 ret = sysdev_class_register(&omap_gpio_sysclass);
1762 if (ret == 0)
1763 ret = sysdev_register(&omap_gpio_device);
1764 }
1765 }
1766#endif
1767
1768 return ret;
1769}
1770
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001771EXPORT_SYMBOL(omap_request_gpio);
1772EXPORT_SYMBOL(omap_free_gpio);
1773EXPORT_SYMBOL(omap_set_gpio_direction);
1774EXPORT_SYMBOL(omap_set_gpio_dataout);
1775EXPORT_SYMBOL(omap_get_gpio_datain);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001776
Tony Lindgren92105bb2005-09-07 17:20:26 +01001777arch_initcall(omap_gpio_sysinit);
David Brownellb9772a22006-12-06 17:13:53 -08001778
1779
1780#ifdef CONFIG_DEBUG_FS
1781
1782#include <linux/debugfs.h>
1783#include <linux/seq_file.h>
1784
1785static int gpio_is_input(struct gpio_bank *bank, int mask)
1786{
1787 void __iomem *reg = bank->base;
1788
1789 switch (bank->method) {
1790 case METHOD_MPUIO:
1791 reg += OMAP_MPUIO_IO_CNTL;
1792 break;
1793 case METHOD_GPIO_1510:
1794 reg += OMAP1510_GPIO_DIR_CONTROL;
1795 break;
1796 case METHOD_GPIO_1610:
1797 reg += OMAP1610_GPIO_DIRECTION;
1798 break;
1799 case METHOD_GPIO_730:
1800 reg += OMAP730_GPIO_DIR_CONTROL;
1801 break;
1802 case METHOD_GPIO_24XX:
1803 reg += OMAP24XX_GPIO_OE;
1804 break;
1805 }
1806 return __raw_readl(reg) & mask;
1807}
1808
1809
1810static int dbg_gpio_show(struct seq_file *s, void *unused)
1811{
1812 unsigned i, j, gpio;
1813
1814 for (i = 0, gpio = 0; i < gpio_bank_count; i++) {
1815 struct gpio_bank *bank = gpio_bank + i;
1816 unsigned bankwidth = 16;
1817 u32 mask = 1;
1818
David Brownelle5c56ed2006-12-06 17:13:59 -08001819 if (bank_is_mpuio(bank))
David Brownellb9772a22006-12-06 17:13:53 -08001820 gpio = OMAP_MPUIO(0);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001821 else if (cpu_class_is_omap2() || cpu_is_omap730())
David Brownellb9772a22006-12-06 17:13:53 -08001822 bankwidth = 32;
1823
1824 for (j = 0; j < bankwidth; j++, gpio++, mask <<= 1) {
1825 unsigned irq, value, is_in, irqstat;
David Brownell52e31342008-03-03 12:43:23 -08001826 const char *label;
David Brownellb9772a22006-12-06 17:13:53 -08001827
David Brownell52e31342008-03-03 12:43:23 -08001828 label = gpiochip_is_requested(&bank->chip, j);
1829 if (!label)
David Brownellb9772a22006-12-06 17:13:53 -08001830 continue;
1831
1832 irq = bank->virtual_irq_start + j;
1833 value = omap_get_gpio_datain(gpio);
1834 is_in = gpio_is_input(bank, mask);
1835
David Brownelle5c56ed2006-12-06 17:13:59 -08001836 if (bank_is_mpuio(bank))
David Brownell52e31342008-03-03 12:43:23 -08001837 seq_printf(s, "MPUIO %2d ", j);
David Brownellb9772a22006-12-06 17:13:53 -08001838 else
David Brownell52e31342008-03-03 12:43:23 -08001839 seq_printf(s, "GPIO %3d ", gpio);
1840 seq_printf(s, "(%10s): %s %s",
1841 label,
David Brownellb9772a22006-12-06 17:13:53 -08001842 is_in ? "in " : "out",
1843 value ? "hi" : "lo");
1844
David Brownell52e31342008-03-03 12:43:23 -08001845/* FIXME for at least omap2, show pullup/pulldown state */
1846
David Brownellb9772a22006-12-06 17:13:53 -08001847 irqstat = irq_desc[irq].status;
1848 if (is_in && ((bank->suspend_wakeup & mask)
1849 || irqstat & IRQ_TYPE_SENSE_MASK)) {
1850 char *trigger = NULL;
1851
1852 switch (irqstat & IRQ_TYPE_SENSE_MASK) {
1853 case IRQ_TYPE_EDGE_FALLING:
1854 trigger = "falling";
1855 break;
1856 case IRQ_TYPE_EDGE_RISING:
1857 trigger = "rising";
1858 break;
1859 case IRQ_TYPE_EDGE_BOTH:
1860 trigger = "bothedge";
1861 break;
1862 case IRQ_TYPE_LEVEL_LOW:
1863 trigger = "low";
1864 break;
1865 case IRQ_TYPE_LEVEL_HIGH:
1866 trigger = "high";
1867 break;
1868 case IRQ_TYPE_NONE:
David Brownell52e31342008-03-03 12:43:23 -08001869 trigger = "(?)";
David Brownellb9772a22006-12-06 17:13:53 -08001870 break;
1871 }
David Brownell52e31342008-03-03 12:43:23 -08001872 seq_printf(s, ", irq-%d %-8s%s",
David Brownellb9772a22006-12-06 17:13:53 -08001873 irq, trigger,
1874 (bank->suspend_wakeup & mask)
1875 ? " wakeup" : "");
1876 }
1877 seq_printf(s, "\n");
1878 }
1879
David Brownelle5c56ed2006-12-06 17:13:59 -08001880 if (bank_is_mpuio(bank)) {
David Brownellb9772a22006-12-06 17:13:53 -08001881 seq_printf(s, "\n");
1882 gpio = 0;
1883 }
1884 }
1885 return 0;
1886}
1887
1888static int dbg_gpio_open(struct inode *inode, struct file *file)
1889{
David Brownelle5c56ed2006-12-06 17:13:59 -08001890 return single_open(file, dbg_gpio_show, &inode->i_private);
David Brownellb9772a22006-12-06 17:13:53 -08001891}
1892
1893static const struct file_operations debug_fops = {
1894 .open = dbg_gpio_open,
1895 .read = seq_read,
1896 .llseek = seq_lseek,
1897 .release = single_release,
1898};
1899
1900static int __init omap_gpio_debuginit(void)
1901{
David Brownelle5c56ed2006-12-06 17:13:59 -08001902 (void) debugfs_create_file("omap_gpio", S_IRUGO,
1903 NULL, NULL, &debug_fops);
David Brownellb9772a22006-12-06 17:13:53 -08001904 return 0;
1905}
1906late_initcall(omap_gpio_debuginit);
1907#endif