blob: f55f99ae58aea6ab07402c2562ab00b84ff8c38e [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
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01007 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
8 *
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>
16#include <linux/sched.h>
17#include <linux/interrupt.h>
18#include <linux/ptrace.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/sysdev.h>
20#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000021#include <linux/clk.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010022
23#include <asm/hardware.h>
24#include <asm/irq.h>
25#include <asm/arch/irqs.h>
26#include <asm/arch/gpio.h>
27#include <asm/mach/irq.h>
28
29#include <asm/io.h>
30
31/*
32 * OMAP1510 GPIO registers
33 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010034#define OMAP1510_GPIO_BASE (void __iomem *)0xfffce000
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010035#define OMAP1510_GPIO_DATA_INPUT 0x00
36#define OMAP1510_GPIO_DATA_OUTPUT 0x04
37#define OMAP1510_GPIO_DIR_CONTROL 0x08
38#define OMAP1510_GPIO_INT_CONTROL 0x0c
39#define OMAP1510_GPIO_INT_MASK 0x10
40#define OMAP1510_GPIO_INT_STATUS 0x14
41#define OMAP1510_GPIO_PIN_CONTROL 0x18
42
43#define OMAP1510_IH_GPIO_BASE 64
44
45/*
46 * OMAP1610 specific GPIO registers
47 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010048#define OMAP1610_GPIO1_BASE (void __iomem *)0xfffbe400
49#define OMAP1610_GPIO2_BASE (void __iomem *)0xfffbec00
50#define OMAP1610_GPIO3_BASE (void __iomem *)0xfffbb400
51#define OMAP1610_GPIO4_BASE (void __iomem *)0xfffbbc00
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010052#define OMAP1610_GPIO_REVISION 0x0000
53#define OMAP1610_GPIO_SYSCONFIG 0x0010
54#define OMAP1610_GPIO_SYSSTATUS 0x0014
55#define OMAP1610_GPIO_IRQSTATUS1 0x0018
56#define OMAP1610_GPIO_IRQENABLE1 0x001c
Tony Lindgren92105bb2005-09-07 17:20:26 +010057#define OMAP1610_GPIO_WAKEUPENABLE 0x0028
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010058#define OMAP1610_GPIO_DATAIN 0x002c
59#define OMAP1610_GPIO_DATAOUT 0x0030
60#define OMAP1610_GPIO_DIRECTION 0x0034
61#define OMAP1610_GPIO_EDGE_CTRL1 0x0038
62#define OMAP1610_GPIO_EDGE_CTRL2 0x003c
63#define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
Tony Lindgren92105bb2005-09-07 17:20:26 +010064#define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010065#define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
66#define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
Tony Lindgren92105bb2005-09-07 17:20:26 +010067#define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010068#define OMAP1610_GPIO_SET_DATAOUT 0x00f0
69
70/*
71 * OMAP730 specific GPIO registers
72 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010073#define OMAP730_GPIO1_BASE (void __iomem *)0xfffbc000
74#define OMAP730_GPIO2_BASE (void __iomem *)0xfffbc800
75#define OMAP730_GPIO3_BASE (void __iomem *)0xfffbd000
76#define OMAP730_GPIO4_BASE (void __iomem *)0xfffbd800
77#define OMAP730_GPIO5_BASE (void __iomem *)0xfffbe000
78#define OMAP730_GPIO6_BASE (void __iomem *)0xfffbe800
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010079#define OMAP730_GPIO_DATA_INPUT 0x00
80#define OMAP730_GPIO_DATA_OUTPUT 0x04
81#define OMAP730_GPIO_DIR_CONTROL 0x08
82#define OMAP730_GPIO_INT_CONTROL 0x0c
83#define OMAP730_GPIO_INT_MASK 0x10
84#define OMAP730_GPIO_INT_STATUS 0x14
85
Tony Lindgren92105bb2005-09-07 17:20:26 +010086/*
87 * omap24xx specific GPIO registers
88 */
89#define OMAP24XX_GPIO1_BASE (void __iomem *)0x48018000
90#define OMAP24XX_GPIO2_BASE (void __iomem *)0x4801a000
91#define OMAP24XX_GPIO3_BASE (void __iomem *)0x4801c000
92#define OMAP24XX_GPIO4_BASE (void __iomem *)0x4801e000
93#define OMAP24XX_GPIO_REVISION 0x0000
94#define OMAP24XX_GPIO_SYSCONFIG 0x0010
95#define OMAP24XX_GPIO_SYSSTATUS 0x0014
96#define OMAP24XX_GPIO_IRQSTATUS1 0x0018
Hiroshi DOYUbee79302006-09-25 12:41:46 +030097#define OMAP24XX_GPIO_IRQSTATUS2 0x0028
98#define OMAP24XX_GPIO_IRQENABLE2 0x002c
Tony Lindgren92105bb2005-09-07 17:20:26 +010099#define OMAP24XX_GPIO_IRQENABLE1 0x001c
100#define OMAP24XX_GPIO_CTRL 0x0030
101#define OMAP24XX_GPIO_OE 0x0034
102#define OMAP24XX_GPIO_DATAIN 0x0038
103#define OMAP24XX_GPIO_DATAOUT 0x003c
104#define OMAP24XX_GPIO_LEVELDETECT0 0x0040
105#define OMAP24XX_GPIO_LEVELDETECT1 0x0044
106#define OMAP24XX_GPIO_RISINGDETECT 0x0048
107#define OMAP24XX_GPIO_FALLINGDETECT 0x004c
108#define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
109#define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
110#define OMAP24XX_GPIO_CLEARWKUENA 0x0080
111#define OMAP24XX_GPIO_SETWKUENA 0x0084
112#define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
113#define OMAP24XX_GPIO_SETDATAOUT 0x0094
114
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100115struct gpio_bank {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100116 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100117 u16 irq;
118 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100119 int method;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100120 u32 reserved_map;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100121 u32 suspend_wakeup;
122 u32 saved_wakeup;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100123 spinlock_t lock;
124};
125
126#define METHOD_MPUIO 0
127#define METHOD_GPIO_1510 1
128#define METHOD_GPIO_1610 2
129#define METHOD_GPIO_730 3
Tony Lindgren92105bb2005-09-07 17:20:26 +0100130#define METHOD_GPIO_24XX 4
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100131
Tony Lindgren92105bb2005-09-07 17:20:26 +0100132#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133static struct gpio_bank gpio_bank_1610[5] = {
134 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
135 { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
136 { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
137 { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
138 { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
139};
140#endif
141
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000142#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100143static struct gpio_bank gpio_bank_1510[2] = {
144 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
145 { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
146};
147#endif
148
149#ifdef CONFIG_ARCH_OMAP730
150static struct gpio_bank gpio_bank_730[7] = {
151 { OMAP_MPUIO_BASE, INT_730_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
152 { OMAP730_GPIO1_BASE, INT_730_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_730 },
153 { OMAP730_GPIO2_BASE, INT_730_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_730 },
154 { OMAP730_GPIO3_BASE, INT_730_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_730 },
155 { OMAP730_GPIO4_BASE, INT_730_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_730 },
156 { OMAP730_GPIO5_BASE, INT_730_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_730 },
157 { OMAP730_GPIO6_BASE, INT_730_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_730 },
158};
159#endif
160
Tony Lindgren92105bb2005-09-07 17:20:26 +0100161#ifdef CONFIG_ARCH_OMAP24XX
162static struct gpio_bank gpio_bank_24xx[4] = {
163 { OMAP24XX_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
164 { OMAP24XX_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
165 { OMAP24XX_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
166 { OMAP24XX_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
167};
168#endif
169
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100170static struct gpio_bank *gpio_bank;
171static int gpio_bank_count;
172
173static inline struct gpio_bank *get_gpio_bank(int gpio)
174{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000175#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +0100176 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100177 if (OMAP_GPIO_IS_MPUIO(gpio))
178 return &gpio_bank[0];
179 return &gpio_bank[1];
180 }
181#endif
182#if defined(CONFIG_ARCH_OMAP16XX)
183 if (cpu_is_omap16xx()) {
184 if (OMAP_GPIO_IS_MPUIO(gpio))
185 return &gpio_bank[0];
186 return &gpio_bank[1 + (gpio >> 4)];
187 }
188#endif
189#ifdef CONFIG_ARCH_OMAP730
190 if (cpu_is_omap730()) {
191 if (OMAP_GPIO_IS_MPUIO(gpio))
192 return &gpio_bank[0];
193 return &gpio_bank[1 + (gpio >> 5)];
194 }
195#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100196#ifdef CONFIG_ARCH_OMAP24XX
197 if (cpu_is_omap24xx())
198 return &gpio_bank[gpio >> 5];
199#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100200}
201
202static inline int get_gpio_index(int gpio)
203{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100204#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100205 if (cpu_is_omap730())
206 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100207#endif
208#ifdef CONFIG_ARCH_OMAP24XX
209 if (cpu_is_omap24xx())
210 return gpio & 0x1f;
211#endif
212 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100213}
214
215static inline int gpio_valid(int gpio)
216{
217 if (gpio < 0)
218 return -1;
Imre Deak5a4e86d2006-09-25 12:41:27 +0300219#ifndef CONFIG_ARCH_OMAP24XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100220 if (OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300221 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100222 return -1;
223 return 0;
224 }
Imre Deak5a4e86d2006-09-25 12:41:27 +0300225#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000226#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +0100227 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100228 return 0;
229#endif
230#if defined(CONFIG_ARCH_OMAP16XX)
231 if ((cpu_is_omap16xx()) && gpio < 64)
232 return 0;
233#endif
234#ifdef CONFIG_ARCH_OMAP730
235 if (cpu_is_omap730() && gpio < 192)
236 return 0;
237#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100238#ifdef CONFIG_ARCH_OMAP24XX
239 if (cpu_is_omap24xx() && gpio < 128)
240 return 0;
241#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100242 return -1;
243}
244
245static int check_gpio(int gpio)
246{
247 if (unlikely(gpio_valid(gpio)) < 0) {
248 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
249 dump_stack();
250 return -1;
251 }
252 return 0;
253}
254
255static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
256{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100257 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100258 u32 l;
259
260 switch (bank->method) {
261 case METHOD_MPUIO:
262 reg += OMAP_MPUIO_IO_CNTL;
263 break;
264 case METHOD_GPIO_1510:
265 reg += OMAP1510_GPIO_DIR_CONTROL;
266 break;
267 case METHOD_GPIO_1610:
268 reg += OMAP1610_GPIO_DIRECTION;
269 break;
270 case METHOD_GPIO_730:
271 reg += OMAP730_GPIO_DIR_CONTROL;
272 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100273 case METHOD_GPIO_24XX:
274 reg += OMAP24XX_GPIO_OE;
275 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100276 }
277 l = __raw_readl(reg);
278 if (is_input)
279 l |= 1 << gpio;
280 else
281 l &= ~(1 << gpio);
282 __raw_writel(l, reg);
283}
284
285void omap_set_gpio_direction(int gpio, int is_input)
286{
287 struct gpio_bank *bank;
288
289 if (check_gpio(gpio) < 0)
290 return;
291 bank = get_gpio_bank(gpio);
292 spin_lock(&bank->lock);
293 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
294 spin_unlock(&bank->lock);
295}
296
297static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
298{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100299 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100300 u32 l = 0;
301
302 switch (bank->method) {
303 case METHOD_MPUIO:
304 reg += OMAP_MPUIO_OUTPUT;
305 l = __raw_readl(reg);
306 if (enable)
307 l |= 1 << gpio;
308 else
309 l &= ~(1 << gpio);
310 break;
311 case METHOD_GPIO_1510:
312 reg += OMAP1510_GPIO_DATA_OUTPUT;
313 l = __raw_readl(reg);
314 if (enable)
315 l |= 1 << gpio;
316 else
317 l &= ~(1 << gpio);
318 break;
319 case METHOD_GPIO_1610:
320 if (enable)
321 reg += OMAP1610_GPIO_SET_DATAOUT;
322 else
323 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
324 l = 1 << gpio;
325 break;
326 case METHOD_GPIO_730:
327 reg += OMAP730_GPIO_DATA_OUTPUT;
328 l = __raw_readl(reg);
329 if (enable)
330 l |= 1 << gpio;
331 else
332 l &= ~(1 << gpio);
333 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100334 case METHOD_GPIO_24XX:
335 if (enable)
336 reg += OMAP24XX_GPIO_SETDATAOUT;
337 else
338 reg += OMAP24XX_GPIO_CLEARDATAOUT;
339 l = 1 << gpio;
340 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100341 default:
342 BUG();
343 return;
344 }
345 __raw_writel(l, reg);
346}
347
348void omap_set_gpio_dataout(int gpio, int enable)
349{
350 struct gpio_bank *bank;
351
352 if (check_gpio(gpio) < 0)
353 return;
354 bank = get_gpio_bank(gpio);
355 spin_lock(&bank->lock);
356 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
357 spin_unlock(&bank->lock);
358}
359
360int omap_get_gpio_datain(int gpio)
361{
362 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100363 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100364
365 if (check_gpio(gpio) < 0)
366 return -1;
367 bank = get_gpio_bank(gpio);
368 reg = bank->base;
369 switch (bank->method) {
370 case METHOD_MPUIO:
371 reg += OMAP_MPUIO_INPUT_LATCH;
372 break;
373 case METHOD_GPIO_1510:
374 reg += OMAP1510_GPIO_DATA_INPUT;
375 break;
376 case METHOD_GPIO_1610:
377 reg += OMAP1610_GPIO_DATAIN;
378 break;
379 case METHOD_GPIO_730:
380 reg += OMAP730_GPIO_DATA_INPUT;
381 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100382 case METHOD_GPIO_24XX:
383 reg += OMAP24XX_GPIO_DATAIN;
384 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100385 default:
386 BUG();
387 return -1;
388 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100389 return (__raw_readl(reg)
390 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100391}
392
Tony Lindgren92105bb2005-09-07 17:20:26 +0100393#define MOD_REG_BIT(reg, bit_mask, set) \
394do { \
395 int l = __raw_readl(base + reg); \
396 if (set) l |= bit_mask; \
397 else l &= ~bit_mask; \
398 __raw_writel(l, base + reg); \
399} while(0)
400
401static inline void set_24xx_gpio_triggering(void __iomem *base, int gpio, int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100402{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100403 u32 gpio_bit = 1 << gpio;
404
405 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100406 trigger & __IRQT_LOWLVL);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100407 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100408 trigger & __IRQT_HIGHLVL);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100409 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100410 trigger & __IRQT_RISEDGE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100411 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100412 trigger & __IRQT_FALEDGE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100413 /* FIXME: Possibly do 'set_irq_handler(j, do_level_IRQ)' if only level
414 * triggering requested. */
415}
416
417static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
418{
419 void __iomem *reg = bank->base;
420 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100421
422 switch (bank->method) {
423 case METHOD_MPUIO:
424 reg += OMAP_MPUIO_GPIO_INT_EDGE;
425 l = __raw_readl(reg);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100426 if (trigger & __IRQT_RISEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100427 l |= 1 << gpio;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100428 else if (trigger & __IRQT_FALEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100429 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100430 else
431 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100432 break;
433 case METHOD_GPIO_1510:
434 reg += OMAP1510_GPIO_INT_CONTROL;
435 l = __raw_readl(reg);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100436 if (trigger & __IRQT_RISEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100437 l |= 1 << gpio;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100438 else if (trigger & __IRQT_FALEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100439 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100440 else
441 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100442 break;
443 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100444 if (gpio & 0x08)
445 reg += OMAP1610_GPIO_EDGE_CTRL2;
446 else
447 reg += OMAP1610_GPIO_EDGE_CTRL1;
448 gpio &= 0x07;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100449 /* We allow only edge triggering, i.e. two lowest bits */
Tony Lindgren6e60e792006-04-02 17:46:23 +0100450 if (trigger & (__IRQT_LOWLVL | __IRQT_HIGHLVL))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100451 BUG();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100452 l = __raw_readl(reg);
453 l &= ~(3 << (gpio << 1));
Tony Lindgren6e60e792006-04-02 17:46:23 +0100454 if (trigger & __IRQT_RISEDGE)
455 l |= 2 << (gpio << 1);
456 if (trigger & __IRQT_FALEDGE)
457 l |= 1 << (gpio << 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100458 break;
459 case METHOD_GPIO_730:
460 reg += OMAP730_GPIO_INT_CONTROL;
461 l = __raw_readl(reg);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100462 if (trigger & __IRQT_RISEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100463 l |= 1 << gpio;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100464 else if (trigger & __IRQT_FALEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100465 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100466 else
467 goto bad;
468 break;
469 case METHOD_GPIO_24XX:
470 set_24xx_gpio_triggering(reg, gpio, trigger);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100471 break;
472 default:
473 BUG();
Tony Lindgren92105bb2005-09-07 17:20:26 +0100474 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100476 __raw_writel(l, reg);
477 return 0;
478bad:
479 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100480}
481
Tony Lindgren92105bb2005-09-07 17:20:26 +0100482static int gpio_irq_type(unsigned irq, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100483{
484 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100485 unsigned gpio;
486 int retval;
487
488 if (irq > IH_MPUIO_BASE)
489 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
490 else
491 gpio = irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100492
493 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100494 return -EINVAL;
495
Tony Lindgren6e60e792006-04-02 17:46:23 +0100496 if (type & IRQT_PROBE)
497 return -EINVAL;
498 if (!cpu_is_omap24xx() && (type & (__IRQT_LOWLVL|__IRQT_HIGHLVL)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100499 return -EINVAL;
500
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100501 bank = get_gpio_bank(gpio);
502 spin_lock(&bank->lock);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100503 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100504 spin_unlock(&bank->lock);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100505 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100506}
507
508static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
509{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100510 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100511
512 switch (bank->method) {
513 case METHOD_MPUIO:
514 /* MPUIO irqstatus is reset by reading the status register,
515 * so do nothing here */
516 return;
517 case METHOD_GPIO_1510:
518 reg += OMAP1510_GPIO_INT_STATUS;
519 break;
520 case METHOD_GPIO_1610:
521 reg += OMAP1610_GPIO_IRQSTATUS1;
522 break;
523 case METHOD_GPIO_730:
524 reg += OMAP730_GPIO_INT_STATUS;
525 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100526 case METHOD_GPIO_24XX:
527 reg += OMAP24XX_GPIO_IRQSTATUS1;
528 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529 default:
530 BUG();
531 return;
532 }
533 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300534
535 /* Workaround for clearing DSP GPIO interrupts to allow retention */
536 if (cpu_is_omap2420())
537 __raw_writel(gpio_mask, bank->base + OMAP24XX_GPIO_IRQSTATUS2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100538}
539
540static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
541{
542 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
543}
544
Imre Deakea6dedd2006-06-26 16:16:00 -0700545static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
546{
547 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700548 int inv = 0;
549 u32 l;
550 u32 mask;
Imre Deakea6dedd2006-06-26 16:16:00 -0700551
552 switch (bank->method) {
553 case METHOD_MPUIO:
554 reg += OMAP_MPUIO_GPIO_MASKIT;
Imre Deak99c47702006-06-26 16:16:07 -0700555 mask = 0xffff;
556 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700557 break;
558 case METHOD_GPIO_1510:
559 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700560 mask = 0xffff;
561 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700562 break;
563 case METHOD_GPIO_1610:
564 reg += OMAP1610_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700565 mask = 0xffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700566 break;
567 case METHOD_GPIO_730:
568 reg += OMAP730_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700569 mask = 0xffffffff;
570 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700571 break;
572 case METHOD_GPIO_24XX:
573 reg += OMAP24XX_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700574 mask = 0xffffffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700575 break;
576 default:
577 BUG();
578 return 0;
579 }
580
Imre Deak99c47702006-06-26 16:16:07 -0700581 l = __raw_readl(reg);
582 if (inv)
583 l = ~l;
584 l &= mask;
585 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700586}
587
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100588static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
589{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100590 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100591 u32 l;
592
593 switch (bank->method) {
594 case METHOD_MPUIO:
595 reg += OMAP_MPUIO_GPIO_MASKIT;
596 l = __raw_readl(reg);
597 if (enable)
598 l &= ~(gpio_mask);
599 else
600 l |= gpio_mask;
601 break;
602 case METHOD_GPIO_1510:
603 reg += OMAP1510_GPIO_INT_MASK;
604 l = __raw_readl(reg);
605 if (enable)
606 l &= ~(gpio_mask);
607 else
608 l |= gpio_mask;
609 break;
610 case METHOD_GPIO_1610:
611 if (enable)
612 reg += OMAP1610_GPIO_SET_IRQENABLE1;
613 else
614 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
615 l = gpio_mask;
616 break;
617 case METHOD_GPIO_730:
618 reg += OMAP730_GPIO_INT_MASK;
619 l = __raw_readl(reg);
620 if (enable)
621 l &= ~(gpio_mask);
622 else
623 l |= gpio_mask;
624 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100625 case METHOD_GPIO_24XX:
626 if (enable)
627 reg += OMAP24XX_GPIO_SETIRQENABLE1;
628 else
629 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
630 l = gpio_mask;
631 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100632 default:
633 BUG();
634 return;
635 }
636 __raw_writel(l, reg);
637}
638
639static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
640{
641 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
642}
643
Tony Lindgren92105bb2005-09-07 17:20:26 +0100644/*
645 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
646 * 1510 does not seem to have a wake-up register. If JTAG is connected
647 * to the target, system will wake up always on GPIO events. While
648 * system is running all registered GPIO interrupts need to have wake-up
649 * enabled. When system is suspended, only selected GPIO interrupts need
650 * to have wake-up enabled.
651 */
652static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
653{
654 switch (bank->method) {
655 case METHOD_GPIO_1610:
656 case METHOD_GPIO_24XX:
657 spin_lock(&bank->lock);
658 if (enable)
659 bank->suspend_wakeup |= (1 << gpio);
660 else
661 bank->suspend_wakeup &= ~(1 << gpio);
662 spin_unlock(&bank->lock);
663 return 0;
664 default:
665 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
666 bank->method);
667 return -EINVAL;
668 }
669}
670
Tony Lindgren4196dd62006-09-25 12:41:38 +0300671static void _reset_gpio(struct gpio_bank *bank, int gpio)
672{
673 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
674 _set_gpio_irqenable(bank, gpio, 0);
675 _clear_gpio_irqstatus(bank, gpio);
676 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQT_NOEDGE);
677}
678
Tony Lindgren92105bb2005-09-07 17:20:26 +0100679/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
680static int gpio_wake_enable(unsigned int irq, unsigned int enable)
681{
682 unsigned int gpio = irq - IH_GPIO_BASE;
683 struct gpio_bank *bank;
684 int retval;
685
686 if (check_gpio(gpio) < 0)
687 return -ENODEV;
688 bank = get_gpio_bank(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100689 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100690
691 return retval;
692}
693
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100694int omap_request_gpio(int gpio)
695{
696 struct gpio_bank *bank;
697
698 if (check_gpio(gpio) < 0)
699 return -EINVAL;
700
701 bank = get_gpio_bank(gpio);
702 spin_lock(&bank->lock);
703 if (unlikely(bank->reserved_map & (1 << get_gpio_index(gpio)))) {
704 printk(KERN_ERR "omap-gpio: GPIO %d is already reserved!\n", gpio);
705 dump_stack();
706 spin_unlock(&bank->lock);
707 return -1;
708 }
709 bank->reserved_map |= (1 << get_gpio_index(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100710
Tony Lindgren4196dd62006-09-25 12:41:38 +0300711 /* Set trigger to none. You need to enable the desired trigger with
712 * request_irq() or set_irq_type().
713 */
Tony Lindgren92105bb2005-09-07 17:20:26 +0100714 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQT_NOEDGE);
715
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000716#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100717 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100718 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100719
Tony Lindgren92105bb2005-09-07 17:20:26 +0100720 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100721 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
722 __raw_writel(__raw_readl(reg) | (1 << get_gpio_index(gpio)), reg);
723 }
724#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100725#ifdef CONFIG_ARCH_OMAP16XX
726 if (bank->method == METHOD_GPIO_1610) {
727 /* Enable wake-up during idle for dynamic tick */
728 void __iomem *reg = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
729 __raw_writel(1 << get_gpio_index(gpio), reg);
730 }
731#endif
732#ifdef CONFIG_ARCH_OMAP24XX
733 if (bank->method == METHOD_GPIO_24XX) {
734 /* Enable wake-up during idle for dynamic tick */
735 void __iomem *reg = bank->base + OMAP24XX_GPIO_SETWKUENA;
736 __raw_writel(1 << get_gpio_index(gpio), reg);
737 }
738#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100739 spin_unlock(&bank->lock);
740
741 return 0;
742}
743
744void omap_free_gpio(int gpio)
745{
746 struct gpio_bank *bank;
747
748 if (check_gpio(gpio) < 0)
749 return;
750 bank = get_gpio_bank(gpio);
751 spin_lock(&bank->lock);
752 if (unlikely(!(bank->reserved_map & (1 << get_gpio_index(gpio))))) {
753 printk(KERN_ERR "omap-gpio: GPIO %d wasn't reserved!\n", gpio);
754 dump_stack();
755 spin_unlock(&bank->lock);
756 return;
757 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100758#ifdef CONFIG_ARCH_OMAP16XX
759 if (bank->method == METHOD_GPIO_1610) {
760 /* Disable wake-up during idle for dynamic tick */
761 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
762 __raw_writel(1 << get_gpio_index(gpio), reg);
763 }
764#endif
765#ifdef CONFIG_ARCH_OMAP24XX
766 if (bank->method == METHOD_GPIO_24XX) {
767 /* Disable wake-up during idle for dynamic tick */
768 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
769 __raw_writel(1 << get_gpio_index(gpio), reg);
770 }
771#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100772 bank->reserved_map &= ~(1 << get_gpio_index(gpio));
Tony Lindgren4196dd62006-09-25 12:41:38 +0300773 _reset_gpio(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100774 spin_unlock(&bank->lock);
775}
776
777/*
778 * We need to unmask the GPIO bank interrupt as soon as possible to
779 * avoid missing GPIO interrupts for other lines in the bank.
780 * Then we need to mask-read-clear-unmask the triggered GPIO lines
781 * in the bank to avoid missing nested interrupts for a GPIO line.
782 * If we wait to unmask individual GPIO lines in the bank after the
783 * line's interrupt handler has been run, we may miss some nested
784 * interrupts.
785 */
786static void gpio_irq_handler(unsigned int irq, struct irqdesc *desc,
787 struct pt_regs *regs)
788{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100789 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100790 u32 isr;
791 unsigned int gpio_irq;
792 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700793 u32 retrigger = 0;
794 int unmasked = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100795
796 desc->chip->ack(irq);
797
Thomas Gleixner418ca1f02006-07-01 22:32:41 +0100798 bank = get_irq_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100799 if (bank->method == METHOD_MPUIO)
800 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000801#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100802 if (bank->method == METHOD_GPIO_1510)
803 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
804#endif
805#if defined(CONFIG_ARCH_OMAP16XX)
806 if (bank->method == METHOD_GPIO_1610)
807 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
808#endif
809#ifdef CONFIG_ARCH_OMAP730
810 if (bank->method == METHOD_GPIO_730)
811 isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
812#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100813#ifdef CONFIG_ARCH_OMAP24XX
814 if (bank->method == METHOD_GPIO_24XX)
815 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
816#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100817 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100818 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700819 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100820
Imre Deakea6dedd2006-06-26 16:16:00 -0700821 enabled = _get_gpio_irqbank_mask(bank);
822 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100823
824 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
825 isr &= 0x0000ffff;
826
Imre Deakea6dedd2006-06-26 16:16:00 -0700827 if (cpu_is_omap24xx()) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100828 level_mask =
829 __raw_readl(bank->base +
830 OMAP24XX_GPIO_LEVELDETECT0) |
831 __raw_readl(bank->base +
832 OMAP24XX_GPIO_LEVELDETECT1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700833 level_mask &= enabled;
834 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100835
836 /* clear edge sensitive interrupts before handler(s) are
837 called so that we don't miss any interrupt occurred while
838 executing them */
839 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
840 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
841 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
842
843 /* if there is only edge sensitive GPIO pin interrupts
844 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700845 if (!level_mask && !unmasked) {
846 unmasked = 1;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100847 desc->chip->unmask(irq);
Imre Deakea6dedd2006-06-26 16:16:00 -0700848 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100849
Imre Deakea6dedd2006-06-26 16:16:00 -0700850 isr |= retrigger;
851 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100852 if (!isr)
853 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100854
Tony Lindgren92105bb2005-09-07 17:20:26 +0100855 gpio_irq = bank->virtual_irq_start;
856 for (; isr != 0; isr >>= 1, gpio_irq++) {
857 struct irqdesc *d;
Imre Deakea6dedd2006-06-26 16:16:00 -0700858 int irq_mask;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100859 if (!(isr & 1))
860 continue;
861 d = irq_desc + gpio_irq;
Imre Deakea6dedd2006-06-26 16:16:00 -0700862 /* Don't run the handler if it's already running
863 * or was disabled lazely.
864 */
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200865 if (unlikely((d->depth ||
866 (d->status & IRQ_INPROGRESS)))) {
Imre Deakea6dedd2006-06-26 16:16:00 -0700867 irq_mask = 1 <<
868 (gpio_irq - bank->virtual_irq_start);
869 /* The unmasking will be done by
870 * enable_irq in case it is disabled or
871 * after returning from the handler if
872 * it's already running.
873 */
874 _enable_gpio_irqbank(bank, irq_mask, 0);
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200875 if (!d->depth) {
Imre Deakea6dedd2006-06-26 16:16:00 -0700876 /* Level triggered interrupts
877 * won't ever be reentered
878 */
879 BUG_ON(level_mask & irq_mask);
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200880 d->status |= IRQ_PENDING;
Imre Deakea6dedd2006-06-26 16:16:00 -0700881 }
882 continue;
883 }
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200884
Tony Lindgren92105bb2005-09-07 17:20:26 +0100885 desc_handle_irq(gpio_irq, d, regs);
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200886
887 if (unlikely((d->status & IRQ_PENDING) && !d->depth)) {
Imre Deakea6dedd2006-06-26 16:16:00 -0700888 irq_mask = 1 <<
889 (gpio_irq - bank->virtual_irq_start);
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200890 d->status &= ~IRQ_PENDING;
Imre Deakea6dedd2006-06-26 16:16:00 -0700891 _enable_gpio_irqbank(bank, irq_mask, 1);
892 retrigger |= irq_mask;
893 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100894 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100895
896 if (cpu_is_omap24xx()) {
897 /* clear level sensitive interrupts after handler(s) */
898 _enable_gpio_irqbank(bank, isr_saved & level_mask, 0);
899 _clear_gpio_irqbank(bank, isr_saved & level_mask);
900 _enable_gpio_irqbank(bank, isr_saved & level_mask, 1);
901 }
902
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000903 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700904 /* if bank has any level sensitive GPIO pin interrupt
905 configured, we must unmask the bank interrupt only after
906 handler(s) are executed in order to avoid spurious bank
907 interrupt */
908 if (!unmasked)
909 desc->chip->unmask(irq);
910
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100911}
912
Tony Lindgren4196dd62006-09-25 12:41:38 +0300913static void gpio_irq_shutdown(unsigned int irq)
914{
915 unsigned int gpio = irq - IH_GPIO_BASE;
916 struct gpio_bank *bank = get_gpio_bank(gpio);
917
918 _reset_gpio(bank, gpio);
919}
920
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100921static void gpio_ack_irq(unsigned int irq)
922{
923 unsigned int gpio = irq - IH_GPIO_BASE;
924 struct gpio_bank *bank = get_gpio_bank(gpio);
925
926 _clear_gpio_irqstatus(bank, gpio);
927}
928
929static void gpio_mask_irq(unsigned int irq)
930{
931 unsigned int gpio = irq - IH_GPIO_BASE;
932 struct gpio_bank *bank = get_gpio_bank(gpio);
933
934 _set_gpio_irqenable(bank, gpio, 0);
935}
936
937static void gpio_unmask_irq(unsigned int irq)
938{
939 unsigned int gpio = irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100940 unsigned int gpio_idx = get_gpio_index(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100941 struct gpio_bank *bank = get_gpio_bank(gpio);
942
Tony Lindgren92105bb2005-09-07 17:20:26 +0100943 _set_gpio_irqenable(bank, gpio_idx, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100944}
945
946static void mpuio_ack_irq(unsigned int irq)
947{
948 /* The ISR is reset automatically, so do nothing here. */
949}
950
951static void mpuio_mask_irq(unsigned int irq)
952{
953 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
954 struct gpio_bank *bank = get_gpio_bank(gpio);
955
956 _set_gpio_irqenable(bank, gpio, 0);
957}
958
959static void mpuio_unmask_irq(unsigned int irq)
960{
961 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
962 struct gpio_bank *bank = get_gpio_bank(gpio);
963
964 _set_gpio_irqenable(bank, gpio, 1);
965}
966
David Brownell38c677c2006-08-01 22:26:25 +0100967static struct irq_chip gpio_irq_chip = {
968 .name = "GPIO",
Tony Lindgren4196dd62006-09-25 12:41:38 +0300969 .shutdown = gpio_irq_shutdown,
Tony Lindgren92105bb2005-09-07 17:20:26 +0100970 .ack = gpio_ack_irq,
971 .mask = gpio_mask_irq,
972 .unmask = gpio_unmask_irq,
973 .set_type = gpio_irq_type,
974 .set_wake = gpio_wake_enable,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100975};
976
David Brownell38c677c2006-08-01 22:26:25 +0100977static struct irq_chip mpuio_irq_chip = {
978 .name = "MPUIO",
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100979 .ack = mpuio_ack_irq,
980 .mask = mpuio_mask_irq,
David Brownell38c677c2006-08-01 22:26:25 +0100981 .unmask = mpuio_unmask_irq
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100982};
983
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000984static int initialized;
985static struct clk * gpio_ick;
986static struct clk * gpio_fck;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100987
988static int __init _omap_gpio_init(void)
989{
990 int i;
991 struct gpio_bank *bank;
992
993 initialized = 1;
994
Tony Lindgren6e60e792006-04-02 17:46:23 +0100995 if (cpu_is_omap15xx()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000996 gpio_ick = clk_get(NULL, "arm_gpio_ck");
997 if (IS_ERR(gpio_ick))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100998 printk("Could not get arm_gpio_ck\n");
999 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001000 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001001 }
1002 if (cpu_is_omap24xx()) {
1003 gpio_ick = clk_get(NULL, "gpios_ick");
1004 if (IS_ERR(gpio_ick))
1005 printk("Could not get gpios_ick\n");
1006 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001007 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001008 gpio_fck = clk_get(NULL, "gpios_fck");
Komal Shah1630b522006-09-25 12:51:08 +03001009 if (IS_ERR(gpio_fck))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001010 printk("Could not get gpios_fck\n");
1011 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001012 clk_enable(gpio_fck);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001013 }
1014
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001015#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +01001016 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001017 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
1018 gpio_bank_count = 2;
1019 gpio_bank = gpio_bank_1510;
1020 }
1021#endif
1022#if defined(CONFIG_ARCH_OMAP16XX)
1023 if (cpu_is_omap16xx()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001024 u32 rev;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001025
1026 gpio_bank_count = 5;
1027 gpio_bank = gpio_bank_1610;
1028 rev = omap_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
1029 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1030 (rev >> 4) & 0x0f, rev & 0x0f);
1031 }
1032#endif
1033#ifdef CONFIG_ARCH_OMAP730
1034 if (cpu_is_omap730()) {
1035 printk(KERN_INFO "OMAP730 GPIO hardware\n");
1036 gpio_bank_count = 7;
1037 gpio_bank = gpio_bank_730;
1038 }
1039#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001040#ifdef CONFIG_ARCH_OMAP24XX
1041 if (cpu_is_omap24xx()) {
1042 int rev;
1043
1044 gpio_bank_count = 4;
1045 gpio_bank = gpio_bank_24xx;
1046 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1047 printk(KERN_INFO "OMAP24xx GPIO hardware version %d.%d\n",
1048 (rev >> 4) & 0x0f, rev & 0x0f);
1049 }
1050#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001051 for (i = 0; i < gpio_bank_count; i++) {
1052 int j, gpio_count = 16;
1053
1054 bank = &gpio_bank[i];
1055 bank->reserved_map = 0;
1056 bank->base = IO_ADDRESS(bank->base);
1057 spin_lock_init(&bank->lock);
1058 if (bank->method == METHOD_MPUIO) {
1059 omap_writew(0xFFFF, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_MASKIT);
1060 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001061#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001062 if (bank->method == METHOD_GPIO_1510) {
1063 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
1064 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
1065 }
1066#endif
1067#if defined(CONFIG_ARCH_OMAP16XX)
1068 if (bank->method == METHOD_GPIO_1610) {
1069 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
1070 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001071 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001072 }
1073#endif
1074#ifdef CONFIG_ARCH_OMAP730
1075 if (bank->method == METHOD_GPIO_730) {
1076 __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
1077 __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
1078
1079 gpio_count = 32; /* 730 has 32-bit GPIOs */
1080 }
1081#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001082#ifdef CONFIG_ARCH_OMAP24XX
1083 if (bank->method == METHOD_GPIO_24XX) {
1084 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
1085 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
1086
1087 gpio_count = 32;
1088 }
1089#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001090 for (j = bank->virtual_irq_start;
1091 j < bank->virtual_irq_start + gpio_count; j++) {
1092 if (bank->method == METHOD_MPUIO)
1093 set_irq_chip(j, &mpuio_irq_chip);
1094 else
1095 set_irq_chip(j, &gpio_irq_chip);
1096 set_irq_handler(j, do_simple_IRQ);
1097 set_irq_flags(j, IRQF_VALID);
1098 }
1099 set_irq_chained_handler(bank->irq, gpio_irq_handler);
1100 set_irq_data(bank->irq, bank);
1101 }
1102
1103 /* Enable system clock for GPIO module.
1104 * The CAM_CLK_CTRL *is* really the right place. */
Tony Lindgren92105bb2005-09-07 17:20:26 +01001105 if (cpu_is_omap16xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001106 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
1107
1108 return 0;
1109}
1110
Tony Lindgren92105bb2005-09-07 17:20:26 +01001111#if defined (CONFIG_ARCH_OMAP16XX) || defined (CONFIG_ARCH_OMAP24XX)
1112static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
1113{
1114 int i;
1115
1116 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
1117 return 0;
1118
1119 for (i = 0; i < gpio_bank_count; i++) {
1120 struct gpio_bank *bank = &gpio_bank[i];
1121 void __iomem *wake_status;
1122 void __iomem *wake_clear;
1123 void __iomem *wake_set;
1124
1125 switch (bank->method) {
1126 case METHOD_GPIO_1610:
1127 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1128 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1129 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1130 break;
1131 case METHOD_GPIO_24XX:
1132 wake_status = bank->base + OMAP24XX_GPIO_SETWKUENA;
1133 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1134 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1135 break;
1136 default:
1137 continue;
1138 }
1139
1140 spin_lock(&bank->lock);
1141 bank->saved_wakeup = __raw_readl(wake_status);
1142 __raw_writel(0xffffffff, wake_clear);
1143 __raw_writel(bank->suspend_wakeup, wake_set);
1144 spin_unlock(&bank->lock);
1145 }
1146
1147 return 0;
1148}
1149
1150static int omap_gpio_resume(struct sys_device *dev)
1151{
1152 int i;
1153
1154 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
1155 return 0;
1156
1157 for (i = 0; i < gpio_bank_count; i++) {
1158 struct gpio_bank *bank = &gpio_bank[i];
1159 void __iomem *wake_clear;
1160 void __iomem *wake_set;
1161
1162 switch (bank->method) {
1163 case METHOD_GPIO_1610:
1164 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1165 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1166 break;
1167 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001168 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1169 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001170 break;
1171 default:
1172 continue;
1173 }
1174
1175 spin_lock(&bank->lock);
1176 __raw_writel(0xffffffff, wake_clear);
1177 __raw_writel(bank->saved_wakeup, wake_set);
1178 spin_unlock(&bank->lock);
1179 }
1180
1181 return 0;
1182}
1183
1184static struct sysdev_class omap_gpio_sysclass = {
1185 set_kset_name("gpio"),
1186 .suspend = omap_gpio_suspend,
1187 .resume = omap_gpio_resume,
1188};
1189
1190static struct sys_device omap_gpio_device = {
1191 .id = 0,
1192 .cls = &omap_gpio_sysclass,
1193};
1194#endif
1195
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001196/*
1197 * This may get called early from board specific init
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001198 * for boards that have interrupts routed via FPGA.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001199 */
1200int omap_gpio_init(void)
1201{
1202 if (!initialized)
1203 return _omap_gpio_init();
1204 else
1205 return 0;
1206}
1207
Tony Lindgren92105bb2005-09-07 17:20:26 +01001208static int __init omap_gpio_sysinit(void)
1209{
1210 int ret = 0;
1211
1212 if (!initialized)
1213 ret = _omap_gpio_init();
1214
1215#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX)
1216 if (cpu_is_omap16xx() || cpu_is_omap24xx()) {
1217 if (ret == 0) {
1218 ret = sysdev_class_register(&omap_gpio_sysclass);
1219 if (ret == 0)
1220 ret = sysdev_register(&omap_gpio_device);
1221 }
1222 }
1223#endif
1224
1225 return ret;
1226}
1227
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001228EXPORT_SYMBOL(omap_request_gpio);
1229EXPORT_SYMBOL(omap_free_gpio);
1230EXPORT_SYMBOL(omap_set_gpio_direction);
1231EXPORT_SYMBOL(omap_set_gpio_dataout);
1232EXPORT_SYMBOL(omap_get_gpio_datain);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001233
Tony Lindgren92105bb2005-09-07 17:20:26 +01001234arch_initcall(omap_gpio_sysinit);