blob: 0059f19185a74cf4da2e73d434e4ae758b4215d2 [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>
Tony Lindgren92105bb2005-09-07 17:20:26 +010018#include <linux/sysdev.h>
19#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010021
22#include <asm/hardware.h>
23#include <asm/irq.h>
24#include <asm/arch/irqs.h>
25#include <asm/arch/gpio.h>
26#include <asm/mach/irq.h>
27
28#include <asm/io.h>
29
30/*
31 * OMAP1510 GPIO registers
32 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010033#define OMAP1510_GPIO_BASE (void __iomem *)0xfffce000
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010034#define OMAP1510_GPIO_DATA_INPUT 0x00
35#define OMAP1510_GPIO_DATA_OUTPUT 0x04
36#define OMAP1510_GPIO_DIR_CONTROL 0x08
37#define OMAP1510_GPIO_INT_CONTROL 0x0c
38#define OMAP1510_GPIO_INT_MASK 0x10
39#define OMAP1510_GPIO_INT_STATUS 0x14
40#define OMAP1510_GPIO_PIN_CONTROL 0x18
41
42#define OMAP1510_IH_GPIO_BASE 64
43
44/*
45 * OMAP1610 specific GPIO registers
46 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010047#define OMAP1610_GPIO1_BASE (void __iomem *)0xfffbe400
48#define OMAP1610_GPIO2_BASE (void __iomem *)0xfffbec00
49#define OMAP1610_GPIO3_BASE (void __iomem *)0xfffbb400
50#define OMAP1610_GPIO4_BASE (void __iomem *)0xfffbbc00
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010051#define OMAP1610_GPIO_REVISION 0x0000
52#define OMAP1610_GPIO_SYSCONFIG 0x0010
53#define OMAP1610_GPIO_SYSSTATUS 0x0014
54#define OMAP1610_GPIO_IRQSTATUS1 0x0018
55#define OMAP1610_GPIO_IRQENABLE1 0x001c
Tony Lindgren92105bb2005-09-07 17:20:26 +010056#define OMAP1610_GPIO_WAKEUPENABLE 0x0028
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010057#define OMAP1610_GPIO_DATAIN 0x002c
58#define OMAP1610_GPIO_DATAOUT 0x0030
59#define OMAP1610_GPIO_DIRECTION 0x0034
60#define OMAP1610_GPIO_EDGE_CTRL1 0x0038
61#define OMAP1610_GPIO_EDGE_CTRL2 0x003c
62#define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
Tony Lindgren92105bb2005-09-07 17:20:26 +010063#define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010064#define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
65#define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
Tony Lindgren92105bb2005-09-07 17:20:26 +010066#define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010067#define OMAP1610_GPIO_SET_DATAOUT 0x00f0
68
69/*
70 * OMAP730 specific GPIO registers
71 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010072#define OMAP730_GPIO1_BASE (void __iomem *)0xfffbc000
73#define OMAP730_GPIO2_BASE (void __iomem *)0xfffbc800
74#define OMAP730_GPIO3_BASE (void __iomem *)0xfffbd000
75#define OMAP730_GPIO4_BASE (void __iomem *)0xfffbd800
76#define OMAP730_GPIO5_BASE (void __iomem *)0xfffbe000
77#define OMAP730_GPIO6_BASE (void __iomem *)0xfffbe800
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010078#define OMAP730_GPIO_DATA_INPUT 0x00
79#define OMAP730_GPIO_DATA_OUTPUT 0x04
80#define OMAP730_GPIO_DIR_CONTROL 0x08
81#define OMAP730_GPIO_INT_CONTROL 0x0c
82#define OMAP730_GPIO_INT_MASK 0x10
83#define OMAP730_GPIO_INT_STATUS 0x14
84
Tony Lindgren92105bb2005-09-07 17:20:26 +010085/*
86 * omap24xx specific GPIO registers
87 */
88#define OMAP24XX_GPIO1_BASE (void __iomem *)0x48018000
89#define OMAP24XX_GPIO2_BASE (void __iomem *)0x4801a000
90#define OMAP24XX_GPIO3_BASE (void __iomem *)0x4801c000
91#define OMAP24XX_GPIO4_BASE (void __iomem *)0x4801e000
92#define OMAP24XX_GPIO_REVISION 0x0000
93#define OMAP24XX_GPIO_SYSCONFIG 0x0010
94#define OMAP24XX_GPIO_SYSSTATUS 0x0014
95#define OMAP24XX_GPIO_IRQSTATUS1 0x0018
Hiroshi DOYUbee79302006-09-25 12:41:46 +030096#define OMAP24XX_GPIO_IRQSTATUS2 0x0028
97#define OMAP24XX_GPIO_IRQENABLE2 0x002c
Tony Lindgren92105bb2005-09-07 17:20:26 +010098#define OMAP24XX_GPIO_IRQENABLE1 0x001c
99#define OMAP24XX_GPIO_CTRL 0x0030
100#define OMAP24XX_GPIO_OE 0x0034
101#define OMAP24XX_GPIO_DATAIN 0x0038
102#define OMAP24XX_GPIO_DATAOUT 0x003c
103#define OMAP24XX_GPIO_LEVELDETECT0 0x0040
104#define OMAP24XX_GPIO_LEVELDETECT1 0x0044
105#define OMAP24XX_GPIO_RISINGDETECT 0x0048
106#define OMAP24XX_GPIO_FALLINGDETECT 0x004c
107#define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
108#define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
109#define OMAP24XX_GPIO_CLEARWKUENA 0x0080
110#define OMAP24XX_GPIO_SETWKUENA 0x0084
111#define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
112#define OMAP24XX_GPIO_SETDATAOUT 0x0094
113
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100114struct gpio_bank {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100115 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100116 u16 irq;
117 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100118 int method;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100119 u32 reserved_map;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800120#if defined (CONFIG_ARCH_OMAP16XX) || defined (CONFIG_ARCH_OMAP24XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100121 u32 suspend_wakeup;
122 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800123#endif
124#ifdef CONFIG_ARCH_OMAP24XX
125 u32 non_wakeup_gpios;
126 u32 enabled_non_wakeup_gpios;
127
128 u32 saved_datain;
129 u32 saved_fallingdetect;
130 u32 saved_risingdetect;
131#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132 spinlock_t lock;
133};
134
135#define METHOD_MPUIO 0
136#define METHOD_GPIO_1510 1
137#define METHOD_GPIO_1610 2
138#define METHOD_GPIO_730 3
Tony Lindgren92105bb2005-09-07 17:20:26 +0100139#define METHOD_GPIO_24XX 4
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100140
Tony Lindgren92105bb2005-09-07 17:20:26 +0100141#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100142static struct gpio_bank gpio_bank_1610[5] = {
143 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
144 { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
145 { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
146 { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
147 { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
148};
149#endif
150
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000151#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100152static struct gpio_bank gpio_bank_1510[2] = {
153 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
154 { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
155};
156#endif
157
158#ifdef CONFIG_ARCH_OMAP730
159static struct gpio_bank gpio_bank_730[7] = {
160 { OMAP_MPUIO_BASE, INT_730_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
161 { OMAP730_GPIO1_BASE, INT_730_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_730 },
162 { OMAP730_GPIO2_BASE, INT_730_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_730 },
163 { OMAP730_GPIO3_BASE, INT_730_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_730 },
164 { OMAP730_GPIO4_BASE, INT_730_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_730 },
165 { OMAP730_GPIO5_BASE, INT_730_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_730 },
166 { OMAP730_GPIO6_BASE, INT_730_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_730 },
167};
168#endif
169
Tony Lindgren92105bb2005-09-07 17:20:26 +0100170#ifdef CONFIG_ARCH_OMAP24XX
171static struct gpio_bank gpio_bank_24xx[4] = {
172 { OMAP24XX_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
173 { OMAP24XX_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
174 { OMAP24XX_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
175 { OMAP24XX_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
176};
177#endif
178
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100179static struct gpio_bank *gpio_bank;
180static int gpio_bank_count;
181
182static inline struct gpio_bank *get_gpio_bank(int gpio)
183{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000184#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +0100185 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100186 if (OMAP_GPIO_IS_MPUIO(gpio))
187 return &gpio_bank[0];
188 return &gpio_bank[1];
189 }
190#endif
191#if defined(CONFIG_ARCH_OMAP16XX)
192 if (cpu_is_omap16xx()) {
193 if (OMAP_GPIO_IS_MPUIO(gpio))
194 return &gpio_bank[0];
195 return &gpio_bank[1 + (gpio >> 4)];
196 }
197#endif
198#ifdef CONFIG_ARCH_OMAP730
199 if (cpu_is_omap730()) {
200 if (OMAP_GPIO_IS_MPUIO(gpio))
201 return &gpio_bank[0];
202 return &gpio_bank[1 + (gpio >> 5)];
203 }
204#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100205#ifdef CONFIG_ARCH_OMAP24XX
206 if (cpu_is_omap24xx())
207 return &gpio_bank[gpio >> 5];
208#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100209}
210
211static inline int get_gpio_index(int gpio)
212{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100213#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100214 if (cpu_is_omap730())
215 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100216#endif
217#ifdef CONFIG_ARCH_OMAP24XX
218 if (cpu_is_omap24xx())
219 return gpio & 0x1f;
220#endif
221 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100222}
223
224static inline int gpio_valid(int gpio)
225{
226 if (gpio < 0)
227 return -1;
Imre Deak5a4e86d2006-09-25 12:41:27 +0300228#ifndef CONFIG_ARCH_OMAP24XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100229 if (OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300230 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100231 return -1;
232 return 0;
233 }
Imre Deak5a4e86d2006-09-25 12:41:27 +0300234#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000235#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +0100236 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100237 return 0;
238#endif
239#if defined(CONFIG_ARCH_OMAP16XX)
240 if ((cpu_is_omap16xx()) && gpio < 64)
241 return 0;
242#endif
243#ifdef CONFIG_ARCH_OMAP730
244 if (cpu_is_omap730() && gpio < 192)
245 return 0;
246#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100247#ifdef CONFIG_ARCH_OMAP24XX
248 if (cpu_is_omap24xx() && gpio < 128)
249 return 0;
250#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100251 return -1;
252}
253
254static int check_gpio(int gpio)
255{
256 if (unlikely(gpio_valid(gpio)) < 0) {
257 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
258 dump_stack();
259 return -1;
260 }
261 return 0;
262}
263
264static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
265{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100266 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100267 u32 l;
268
269 switch (bank->method) {
270 case METHOD_MPUIO:
271 reg += OMAP_MPUIO_IO_CNTL;
272 break;
273 case METHOD_GPIO_1510:
274 reg += OMAP1510_GPIO_DIR_CONTROL;
275 break;
276 case METHOD_GPIO_1610:
277 reg += OMAP1610_GPIO_DIRECTION;
278 break;
279 case METHOD_GPIO_730:
280 reg += OMAP730_GPIO_DIR_CONTROL;
281 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100282 case METHOD_GPIO_24XX:
283 reg += OMAP24XX_GPIO_OE;
284 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100285 }
286 l = __raw_readl(reg);
287 if (is_input)
288 l |= 1 << gpio;
289 else
290 l &= ~(1 << gpio);
291 __raw_writel(l, reg);
292}
293
294void omap_set_gpio_direction(int gpio, int is_input)
295{
296 struct gpio_bank *bank;
297
298 if (check_gpio(gpio) < 0)
299 return;
300 bank = get_gpio_bank(gpio);
301 spin_lock(&bank->lock);
302 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
303 spin_unlock(&bank->lock);
304}
305
306static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
307{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100308 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100309 u32 l = 0;
310
311 switch (bank->method) {
312 case METHOD_MPUIO:
313 reg += OMAP_MPUIO_OUTPUT;
314 l = __raw_readl(reg);
315 if (enable)
316 l |= 1 << gpio;
317 else
318 l &= ~(1 << gpio);
319 break;
320 case METHOD_GPIO_1510:
321 reg += OMAP1510_GPIO_DATA_OUTPUT;
322 l = __raw_readl(reg);
323 if (enable)
324 l |= 1 << gpio;
325 else
326 l &= ~(1 << gpio);
327 break;
328 case METHOD_GPIO_1610:
329 if (enable)
330 reg += OMAP1610_GPIO_SET_DATAOUT;
331 else
332 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
333 l = 1 << gpio;
334 break;
335 case METHOD_GPIO_730:
336 reg += OMAP730_GPIO_DATA_OUTPUT;
337 l = __raw_readl(reg);
338 if (enable)
339 l |= 1 << gpio;
340 else
341 l &= ~(1 << gpio);
342 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100343 case METHOD_GPIO_24XX:
344 if (enable)
345 reg += OMAP24XX_GPIO_SETDATAOUT;
346 else
347 reg += OMAP24XX_GPIO_CLEARDATAOUT;
348 l = 1 << gpio;
349 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100350 default:
351 BUG();
352 return;
353 }
354 __raw_writel(l, reg);
355}
356
357void omap_set_gpio_dataout(int gpio, int enable)
358{
359 struct gpio_bank *bank;
360
361 if (check_gpio(gpio) < 0)
362 return;
363 bank = get_gpio_bank(gpio);
364 spin_lock(&bank->lock);
365 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
366 spin_unlock(&bank->lock);
367}
368
369int omap_get_gpio_datain(int gpio)
370{
371 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100372 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100373
374 if (check_gpio(gpio) < 0)
375 return -1;
376 bank = get_gpio_bank(gpio);
377 reg = bank->base;
378 switch (bank->method) {
379 case METHOD_MPUIO:
380 reg += OMAP_MPUIO_INPUT_LATCH;
381 break;
382 case METHOD_GPIO_1510:
383 reg += OMAP1510_GPIO_DATA_INPUT;
384 break;
385 case METHOD_GPIO_1610:
386 reg += OMAP1610_GPIO_DATAIN;
387 break;
388 case METHOD_GPIO_730:
389 reg += OMAP730_GPIO_DATA_INPUT;
390 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100391 case METHOD_GPIO_24XX:
392 reg += OMAP24XX_GPIO_DATAIN;
393 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100394 default:
395 BUG();
396 return -1;
397 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100398 return (__raw_readl(reg)
399 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100400}
401
Tony Lindgren92105bb2005-09-07 17:20:26 +0100402#define MOD_REG_BIT(reg, bit_mask, set) \
403do { \
404 int l = __raw_readl(base + reg); \
405 if (set) l |= bit_mask; \
406 else l &= ~bit_mask; \
407 __raw_writel(l, base + reg); \
408} while(0)
409
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800410#ifdef CONFIG_ARCH_OMAP24XX
411static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100412{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800413 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100414 u32 gpio_bit = 1 << gpio;
415
416 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100417 trigger & __IRQT_LOWLVL);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100418 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100419 trigger & __IRQT_HIGHLVL);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100420 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100421 trigger & __IRQT_RISEDGE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100422 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
Tony Lindgren6e60e792006-04-02 17:46:23 +0100423 trigger & __IRQT_FALEDGE);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800424 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
425 if (trigger != 0)
426 __raw_writel(1 << gpio, bank->base + OMAP24XX_GPIO_SETWKUENA);
427 else
428 __raw_writel(1 << gpio, bank->base + OMAP24XX_GPIO_CLEARWKUENA);
429 } else {
430 if (trigger != 0)
431 bank->enabled_non_wakeup_gpios |= gpio_bit;
432 else
433 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
434 }
Russell King10dd5ce2006-11-23 11:41:32 +0000435 /* FIXME: Possibly do 'set_irq_handler(j, handle_level_irq)' if only level
Tony Lindgren92105bb2005-09-07 17:20:26 +0100436 * triggering requested. */
437}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800438#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100439
440static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
441{
442 void __iomem *reg = bank->base;
443 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100444
445 switch (bank->method) {
446 case METHOD_MPUIO:
447 reg += OMAP_MPUIO_GPIO_INT_EDGE;
448 l = __raw_readl(reg);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100449 if (trigger & __IRQT_RISEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100450 l |= 1 << gpio;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100451 else if (trigger & __IRQT_FALEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100452 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100453 else
454 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100455 break;
456 case METHOD_GPIO_1510:
457 reg += OMAP1510_GPIO_INT_CONTROL;
458 l = __raw_readl(reg);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100459 if (trigger & __IRQT_RISEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100460 l |= 1 << gpio;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100461 else if (trigger & __IRQT_FALEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100462 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100463 else
464 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100465 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800466#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100467 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100468 if (gpio & 0x08)
469 reg += OMAP1610_GPIO_EDGE_CTRL2;
470 else
471 reg += OMAP1610_GPIO_EDGE_CTRL1;
472 gpio &= 0x07;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100473 /* We allow only edge triggering, i.e. two lowest bits */
Tony Lindgren6e60e792006-04-02 17:46:23 +0100474 if (trigger & (__IRQT_LOWLVL | __IRQT_HIGHLVL))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100475 BUG();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100476 l = __raw_readl(reg);
477 l &= ~(3 << (gpio << 1));
Tony Lindgren6e60e792006-04-02 17:46:23 +0100478 if (trigger & __IRQT_RISEDGE)
479 l |= 2 << (gpio << 1);
480 if (trigger & __IRQT_FALEDGE)
481 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800482 if (trigger)
483 /* Enable wake-up during idle for dynamic tick */
484 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
485 else
486 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100487 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800488#endif
489#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100490 case METHOD_GPIO_730:
491 reg += OMAP730_GPIO_INT_CONTROL;
492 l = __raw_readl(reg);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100493 if (trigger & __IRQT_RISEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100494 l |= 1 << gpio;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100495 else if (trigger & __IRQT_FALEDGE)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100496 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100497 else
498 goto bad;
499 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800500#endif
501#ifdef CONFIG_ARCH_OMAP24XX
Tony Lindgren92105bb2005-09-07 17:20:26 +0100502 case METHOD_GPIO_24XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800503 set_24xx_gpio_triggering(bank, gpio, trigger);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100504 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800505#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100506 default:
507 BUG();
Tony Lindgren92105bb2005-09-07 17:20:26 +0100508 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100509 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100510 __raw_writel(l, reg);
511 return 0;
512bad:
513 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100514}
515
Tony Lindgren92105bb2005-09-07 17:20:26 +0100516static int gpio_irq_type(unsigned irq, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100517{
518 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100519 unsigned gpio;
520 int retval;
521
522 if (irq > IH_MPUIO_BASE)
523 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
524 else
525 gpio = irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100526
527 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100528 return -EINVAL;
529
Tony Lindgren6e60e792006-04-02 17:46:23 +0100530 if (type & IRQT_PROBE)
531 return -EINVAL;
532 if (!cpu_is_omap24xx() && (type & (__IRQT_LOWLVL|__IRQT_HIGHLVL)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100533 return -EINVAL;
534
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100535 bank = get_gpio_bank(gpio);
536 spin_lock(&bank->lock);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100537 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
David Brownellb9772a22006-12-06 17:13:53 -0800538 if (retval == 0) {
539 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
540 irq_desc[irq].status |= type;
541 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100542 spin_unlock(&bank->lock);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100543 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100544}
545
546static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
547{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100548 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100549
550 switch (bank->method) {
551 case METHOD_MPUIO:
552 /* MPUIO irqstatus is reset by reading the status register,
553 * so do nothing here */
554 return;
555 case METHOD_GPIO_1510:
556 reg += OMAP1510_GPIO_INT_STATUS;
557 break;
558 case METHOD_GPIO_1610:
559 reg += OMAP1610_GPIO_IRQSTATUS1;
560 break;
561 case METHOD_GPIO_730:
562 reg += OMAP730_GPIO_INT_STATUS;
563 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100564 case METHOD_GPIO_24XX:
565 reg += OMAP24XX_GPIO_IRQSTATUS1;
566 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100567 default:
568 BUG();
569 return;
570 }
571 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300572
573 /* Workaround for clearing DSP GPIO interrupts to allow retention */
574 if (cpu_is_omap2420())
575 __raw_writel(gpio_mask, bank->base + OMAP24XX_GPIO_IRQSTATUS2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576}
577
578static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
579{
580 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
581}
582
Imre Deakea6dedd2006-06-26 16:16:00 -0700583static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
584{
585 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700586 int inv = 0;
587 u32 l;
588 u32 mask;
Imre Deakea6dedd2006-06-26 16:16:00 -0700589
590 switch (bank->method) {
591 case METHOD_MPUIO:
592 reg += OMAP_MPUIO_GPIO_MASKIT;
Imre Deak99c47702006-06-26 16:16:07 -0700593 mask = 0xffff;
594 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700595 break;
596 case METHOD_GPIO_1510:
597 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700598 mask = 0xffff;
599 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700600 break;
601 case METHOD_GPIO_1610:
602 reg += OMAP1610_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700603 mask = 0xffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700604 break;
605 case METHOD_GPIO_730:
606 reg += OMAP730_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700607 mask = 0xffffffff;
608 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700609 break;
610 case METHOD_GPIO_24XX:
611 reg += OMAP24XX_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700612 mask = 0xffffffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700613 break;
614 default:
615 BUG();
616 return 0;
617 }
618
Imre Deak99c47702006-06-26 16:16:07 -0700619 l = __raw_readl(reg);
620 if (inv)
621 l = ~l;
622 l &= mask;
623 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700624}
625
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100626static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
627{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100628 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100629 u32 l;
630
631 switch (bank->method) {
632 case METHOD_MPUIO:
633 reg += OMAP_MPUIO_GPIO_MASKIT;
634 l = __raw_readl(reg);
635 if (enable)
636 l &= ~(gpio_mask);
637 else
638 l |= gpio_mask;
639 break;
640 case METHOD_GPIO_1510:
641 reg += OMAP1510_GPIO_INT_MASK;
642 l = __raw_readl(reg);
643 if (enable)
644 l &= ~(gpio_mask);
645 else
646 l |= gpio_mask;
647 break;
648 case METHOD_GPIO_1610:
649 if (enable)
650 reg += OMAP1610_GPIO_SET_IRQENABLE1;
651 else
652 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
653 l = gpio_mask;
654 break;
655 case METHOD_GPIO_730:
656 reg += OMAP730_GPIO_INT_MASK;
657 l = __raw_readl(reg);
658 if (enable)
659 l &= ~(gpio_mask);
660 else
661 l |= gpio_mask;
662 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100663 case METHOD_GPIO_24XX:
664 if (enable)
665 reg += OMAP24XX_GPIO_SETIRQENABLE1;
666 else
667 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
668 l = gpio_mask;
669 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670 default:
671 BUG();
672 return;
673 }
674 __raw_writel(l, reg);
675}
676
677static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
678{
679 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
680}
681
Tony Lindgren92105bb2005-09-07 17:20:26 +0100682/*
683 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
684 * 1510 does not seem to have a wake-up register. If JTAG is connected
685 * to the target, system will wake up always on GPIO events. While
686 * system is running all registered GPIO interrupts need to have wake-up
687 * enabled. When system is suspended, only selected GPIO interrupts need
688 * to have wake-up enabled.
689 */
690static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
691{
692 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800693#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +0100694 case METHOD_GPIO_1610:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100695 spin_lock(&bank->lock);
696 if (enable)
697 bank->suspend_wakeup |= (1 << gpio);
698 else
699 bank->suspend_wakeup &= ~(1 << gpio);
700 spin_unlock(&bank->lock);
701 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800702#endif
703#ifdef CONFIG_ARCH_OMAP24XX
704 case METHOD_GPIO_24XX:
705 spin_lock(&bank->lock);
706 if (enable) {
707 if (bank->non_wakeup_gpios & (1 << gpio)) {
David Brownellb9772a22006-12-06 17:13:53 -0800708 printk(KERN_ERR "Unable to enable wakeup on "
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800709 "non-wakeup GPIO%d\n",
710 (bank - gpio_bank) * 32 + gpio);
711 spin_unlock(&bank->lock);
712 return -EINVAL;
713 }
714 bank->suspend_wakeup |= (1 << gpio);
715 } else
716 bank->suspend_wakeup &= ~(1 << gpio);
717 spin_unlock(&bank->lock);
718 return 0;
719#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100720 default:
721 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
722 bank->method);
723 return -EINVAL;
724 }
725}
726
Tony Lindgren4196dd62006-09-25 12:41:38 +0300727static void _reset_gpio(struct gpio_bank *bank, int gpio)
728{
729 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
730 _set_gpio_irqenable(bank, gpio, 0);
731 _clear_gpio_irqstatus(bank, gpio);
732 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQT_NOEDGE);
733}
734
Tony Lindgren92105bb2005-09-07 17:20:26 +0100735/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
736static int gpio_wake_enable(unsigned int irq, unsigned int enable)
737{
738 unsigned int gpio = irq - IH_GPIO_BASE;
739 struct gpio_bank *bank;
740 int retval;
741
742 if (check_gpio(gpio) < 0)
743 return -ENODEV;
744 bank = get_gpio_bank(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100745 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100746
747 return retval;
748}
749
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100750int omap_request_gpio(int gpio)
751{
752 struct gpio_bank *bank;
753
754 if (check_gpio(gpio) < 0)
755 return -EINVAL;
756
757 bank = get_gpio_bank(gpio);
758 spin_lock(&bank->lock);
759 if (unlikely(bank->reserved_map & (1 << get_gpio_index(gpio)))) {
760 printk(KERN_ERR "omap-gpio: GPIO %d is already reserved!\n", gpio);
761 dump_stack();
762 spin_unlock(&bank->lock);
763 return -1;
764 }
765 bank->reserved_map |= (1 << get_gpio_index(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100766
Tony Lindgren4196dd62006-09-25 12:41:38 +0300767 /* Set trigger to none. You need to enable the desired trigger with
768 * request_irq() or set_irq_type().
769 */
Tony Lindgren92105bb2005-09-07 17:20:26 +0100770 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQT_NOEDGE);
771
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000772#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100773 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100774 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100775
Tony Lindgren92105bb2005-09-07 17:20:26 +0100776 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100777 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
778 __raw_writel(__raw_readl(reg) | (1 << get_gpio_index(gpio)), reg);
779 }
780#endif
781 spin_unlock(&bank->lock);
782
783 return 0;
784}
785
786void omap_free_gpio(int gpio)
787{
788 struct gpio_bank *bank;
789
790 if (check_gpio(gpio) < 0)
791 return;
792 bank = get_gpio_bank(gpio);
793 spin_lock(&bank->lock);
794 if (unlikely(!(bank->reserved_map & (1 << get_gpio_index(gpio))))) {
795 printk(KERN_ERR "omap-gpio: GPIO %d wasn't reserved!\n", gpio);
796 dump_stack();
797 spin_unlock(&bank->lock);
798 return;
799 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100800#ifdef CONFIG_ARCH_OMAP16XX
801 if (bank->method == METHOD_GPIO_1610) {
802 /* Disable wake-up during idle for dynamic tick */
803 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
804 __raw_writel(1 << get_gpio_index(gpio), reg);
805 }
806#endif
807#ifdef CONFIG_ARCH_OMAP24XX
808 if (bank->method == METHOD_GPIO_24XX) {
809 /* Disable wake-up during idle for dynamic tick */
810 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
811 __raw_writel(1 << get_gpio_index(gpio), reg);
812 }
813#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100814 bank->reserved_map &= ~(1 << get_gpio_index(gpio));
Tony Lindgren4196dd62006-09-25 12:41:38 +0300815 _reset_gpio(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100816 spin_unlock(&bank->lock);
817}
818
819/*
820 * We need to unmask the GPIO bank interrupt as soon as possible to
821 * avoid missing GPIO interrupts for other lines in the bank.
822 * Then we need to mask-read-clear-unmask the triggered GPIO lines
823 * in the bank to avoid missing nested interrupts for a GPIO line.
824 * If we wait to unmask individual GPIO lines in the bank after the
825 * line's interrupt handler has been run, we may miss some nested
826 * interrupts.
827 */
Russell King10dd5ce2006-11-23 11:41:32 +0000828static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100829{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100830 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100831 u32 isr;
832 unsigned int gpio_irq;
833 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700834 u32 retrigger = 0;
835 int unmasked = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100836
837 desc->chip->ack(irq);
838
Thomas Gleixner418ca1f02006-07-01 22:32:41 +0100839 bank = get_irq_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100840 if (bank->method == METHOD_MPUIO)
841 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000842#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100843 if (bank->method == METHOD_GPIO_1510)
844 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
845#endif
846#if defined(CONFIG_ARCH_OMAP16XX)
847 if (bank->method == METHOD_GPIO_1610)
848 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
849#endif
850#ifdef CONFIG_ARCH_OMAP730
851 if (bank->method == METHOD_GPIO_730)
852 isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
853#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100854#ifdef CONFIG_ARCH_OMAP24XX
855 if (bank->method == METHOD_GPIO_24XX)
856 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
857#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100858 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100859 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700860 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100861
Imre Deakea6dedd2006-06-26 16:16:00 -0700862 enabled = _get_gpio_irqbank_mask(bank);
863 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100864
865 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
866 isr &= 0x0000ffff;
867
Imre Deakea6dedd2006-06-26 16:16:00 -0700868 if (cpu_is_omap24xx()) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100869 level_mask =
870 __raw_readl(bank->base +
871 OMAP24XX_GPIO_LEVELDETECT0) |
872 __raw_readl(bank->base +
873 OMAP24XX_GPIO_LEVELDETECT1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700874 level_mask &= enabled;
875 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100876
877 /* clear edge sensitive interrupts before handler(s) are
878 called so that we don't miss any interrupt occurred while
879 executing them */
880 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
881 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
882 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
883
884 /* if there is only edge sensitive GPIO pin interrupts
885 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700886 if (!level_mask && !unmasked) {
887 unmasked = 1;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100888 desc->chip->unmask(irq);
Imre Deakea6dedd2006-06-26 16:16:00 -0700889 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100890
Imre Deakea6dedd2006-06-26 16:16:00 -0700891 isr |= retrigger;
892 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100893 if (!isr)
894 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100895
Tony Lindgren92105bb2005-09-07 17:20:26 +0100896 gpio_irq = bank->virtual_irq_start;
897 for (; isr != 0; isr >>= 1, gpio_irq++) {
Russell King10dd5ce2006-11-23 11:41:32 +0000898 struct irq_desc *d;
Imre Deakea6dedd2006-06-26 16:16:00 -0700899 int irq_mask;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100900 if (!(isr & 1))
901 continue;
902 d = irq_desc + gpio_irq;
Imre Deakea6dedd2006-06-26 16:16:00 -0700903 /* Don't run the handler if it's already running
904 * or was disabled lazely.
905 */
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200906 if (unlikely((d->depth ||
907 (d->status & IRQ_INPROGRESS)))) {
Imre Deakea6dedd2006-06-26 16:16:00 -0700908 irq_mask = 1 <<
909 (gpio_irq - bank->virtual_irq_start);
910 /* The unmasking will be done by
911 * enable_irq in case it is disabled or
912 * after returning from the handler if
913 * it's already running.
914 */
915 _enable_gpio_irqbank(bank, irq_mask, 0);
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200916 if (!d->depth) {
Imre Deakea6dedd2006-06-26 16:16:00 -0700917 /* Level triggered interrupts
918 * won't ever be reentered
919 */
920 BUG_ON(level_mask & irq_mask);
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200921 d->status |= IRQ_PENDING;
Imre Deakea6dedd2006-06-26 16:16:00 -0700922 }
923 continue;
924 }
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200925
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700926 desc_handle_irq(gpio_irq, d);
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200927
928 if (unlikely((d->status & IRQ_PENDING) && !d->depth)) {
Imre Deakea6dedd2006-06-26 16:16:00 -0700929 irq_mask = 1 <<
930 (gpio_irq - bank->virtual_irq_start);
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200931 d->status &= ~IRQ_PENDING;
Imre Deakea6dedd2006-06-26 16:16:00 -0700932 _enable_gpio_irqbank(bank, irq_mask, 1);
933 retrigger |= irq_mask;
934 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100935 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100936
937 if (cpu_is_omap24xx()) {
938 /* clear level sensitive interrupts after handler(s) */
939 _enable_gpio_irqbank(bank, isr_saved & level_mask, 0);
940 _clear_gpio_irqbank(bank, isr_saved & level_mask);
941 _enable_gpio_irqbank(bank, isr_saved & level_mask, 1);
942 }
943
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000944 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700945 /* if bank has any level sensitive GPIO pin interrupt
946 configured, we must unmask the bank interrupt only after
947 handler(s) are executed in order to avoid spurious bank
948 interrupt */
949 if (!unmasked)
950 desc->chip->unmask(irq);
951
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100952}
953
Tony Lindgren4196dd62006-09-25 12:41:38 +0300954static void gpio_irq_shutdown(unsigned int irq)
955{
956 unsigned int gpio = irq - IH_GPIO_BASE;
957 struct gpio_bank *bank = get_gpio_bank(gpio);
958
959 _reset_gpio(bank, gpio);
960}
961
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100962static void gpio_ack_irq(unsigned int irq)
963{
964 unsigned int gpio = irq - IH_GPIO_BASE;
965 struct gpio_bank *bank = get_gpio_bank(gpio);
966
967 _clear_gpio_irqstatus(bank, gpio);
968}
969
970static void gpio_mask_irq(unsigned int irq)
971{
972 unsigned int gpio = irq - IH_GPIO_BASE;
973 struct gpio_bank *bank = get_gpio_bank(gpio);
974
975 _set_gpio_irqenable(bank, gpio, 0);
976}
977
978static void gpio_unmask_irq(unsigned int irq)
979{
980 unsigned int gpio = irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100981 unsigned int gpio_idx = get_gpio_index(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100982 struct gpio_bank *bank = get_gpio_bank(gpio);
983
Tony Lindgren92105bb2005-09-07 17:20:26 +0100984 _set_gpio_irqenable(bank, gpio_idx, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100985}
986
987static void mpuio_ack_irq(unsigned int irq)
988{
989 /* The ISR is reset automatically, so do nothing here. */
990}
991
992static void mpuio_mask_irq(unsigned int irq)
993{
994 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
995 struct gpio_bank *bank = get_gpio_bank(gpio);
996
997 _set_gpio_irqenable(bank, gpio, 0);
998}
999
1000static void mpuio_unmask_irq(unsigned int irq)
1001{
1002 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
1003 struct gpio_bank *bank = get_gpio_bank(gpio);
1004
1005 _set_gpio_irqenable(bank, gpio, 1);
1006}
1007
David Brownell38c677c2006-08-01 22:26:25 +01001008static struct irq_chip gpio_irq_chip = {
1009 .name = "GPIO",
Tony Lindgren4196dd62006-09-25 12:41:38 +03001010 .shutdown = gpio_irq_shutdown,
Tony Lindgren92105bb2005-09-07 17:20:26 +01001011 .ack = gpio_ack_irq,
1012 .mask = gpio_mask_irq,
1013 .unmask = gpio_unmask_irq,
1014 .set_type = gpio_irq_type,
1015 .set_wake = gpio_wake_enable,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001016};
1017
David Brownell38c677c2006-08-01 22:26:25 +01001018static struct irq_chip mpuio_irq_chip = {
Dirk Behmeb286f7b2006-12-06 17:13:57 -08001019 .name = "MPUIO",
1020 .ack = mpuio_ack_irq,
1021 .mask = mpuio_mask_irq,
1022 .unmask = mpuio_unmask_irq,
1023 .set_type = gpio_irq_type,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001024};
1025
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001026static int initialized;
1027static struct clk * gpio_ick;
1028static struct clk * gpio_fck;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001029
1030static int __init _omap_gpio_init(void)
1031{
1032 int i;
1033 struct gpio_bank *bank;
1034
1035 initialized = 1;
1036
Tony Lindgren6e60e792006-04-02 17:46:23 +01001037 if (cpu_is_omap15xx()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001038 gpio_ick = clk_get(NULL, "arm_gpio_ck");
1039 if (IS_ERR(gpio_ick))
Tony Lindgren92105bb2005-09-07 17:20:26 +01001040 printk("Could not get arm_gpio_ck\n");
1041 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001042 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001043 }
1044 if (cpu_is_omap24xx()) {
1045 gpio_ick = clk_get(NULL, "gpios_ick");
1046 if (IS_ERR(gpio_ick))
1047 printk("Could not get gpios_ick\n");
1048 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001049 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001050 gpio_fck = clk_get(NULL, "gpios_fck");
Komal Shah1630b522006-09-25 12:51:08 +03001051 if (IS_ERR(gpio_fck))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001052 printk("Could not get gpios_fck\n");
1053 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001054 clk_enable(gpio_fck);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001055 }
1056
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001057#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +01001058 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001059 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
1060 gpio_bank_count = 2;
1061 gpio_bank = gpio_bank_1510;
1062 }
1063#endif
1064#if defined(CONFIG_ARCH_OMAP16XX)
1065 if (cpu_is_omap16xx()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001066 u32 rev;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001067
1068 gpio_bank_count = 5;
1069 gpio_bank = gpio_bank_1610;
1070 rev = omap_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
1071 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1072 (rev >> 4) & 0x0f, rev & 0x0f);
1073 }
1074#endif
1075#ifdef CONFIG_ARCH_OMAP730
1076 if (cpu_is_omap730()) {
1077 printk(KERN_INFO "OMAP730 GPIO hardware\n");
1078 gpio_bank_count = 7;
1079 gpio_bank = gpio_bank_730;
1080 }
1081#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001082#ifdef CONFIG_ARCH_OMAP24XX
1083 if (cpu_is_omap24xx()) {
1084 int rev;
1085
1086 gpio_bank_count = 4;
1087 gpio_bank = gpio_bank_24xx;
1088 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1089 printk(KERN_INFO "OMAP24xx GPIO hardware version %d.%d\n",
1090 (rev >> 4) & 0x0f, rev & 0x0f);
1091 }
1092#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001093 for (i = 0; i < gpio_bank_count; i++) {
1094 int j, gpio_count = 16;
1095
1096 bank = &gpio_bank[i];
1097 bank->reserved_map = 0;
1098 bank->base = IO_ADDRESS(bank->base);
1099 spin_lock_init(&bank->lock);
1100 if (bank->method == METHOD_MPUIO) {
1101 omap_writew(0xFFFF, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_MASKIT);
1102 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001103#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001104 if (bank->method == METHOD_GPIO_1510) {
1105 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
1106 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
1107 }
1108#endif
1109#if defined(CONFIG_ARCH_OMAP16XX)
1110 if (bank->method == METHOD_GPIO_1610) {
1111 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
1112 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001113 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001114 }
1115#endif
1116#ifdef CONFIG_ARCH_OMAP730
1117 if (bank->method == METHOD_GPIO_730) {
1118 __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
1119 __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
1120
1121 gpio_count = 32; /* 730 has 32-bit GPIOs */
1122 }
1123#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001124#ifdef CONFIG_ARCH_OMAP24XX
1125 if (bank->method == METHOD_GPIO_24XX) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001126 static const u32 non_wakeup_gpios[] = {
1127 0xe203ffc0, 0x08700040
1128 };
1129
Tony Lindgren92105bb2005-09-07 17:20:26 +01001130 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
1131 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001132 __raw_writew(0x0015, bank->base + OMAP24XX_GPIO_SYSCONFIG);
1133
1134 /* Initialize interface clock ungated, module enabled */
1135 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001136 if (i < ARRAY_SIZE(non_wakeup_gpios))
1137 bank->non_wakeup_gpios = non_wakeup_gpios[i];
Tony Lindgren92105bb2005-09-07 17:20:26 +01001138 gpio_count = 32;
1139 }
1140#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001141 for (j = bank->virtual_irq_start;
1142 j < bank->virtual_irq_start + gpio_count; j++) {
1143 if (bank->method == METHOD_MPUIO)
1144 set_irq_chip(j, &mpuio_irq_chip);
1145 else
1146 set_irq_chip(j, &gpio_irq_chip);
Russell King10dd5ce2006-11-23 11:41:32 +00001147 set_irq_handler(j, handle_simple_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001148 set_irq_flags(j, IRQF_VALID);
1149 }
1150 set_irq_chained_handler(bank->irq, gpio_irq_handler);
1151 set_irq_data(bank->irq, bank);
1152 }
1153
1154 /* Enable system clock for GPIO module.
1155 * The CAM_CLK_CTRL *is* really the right place. */
Tony Lindgren92105bb2005-09-07 17:20:26 +01001156 if (cpu_is_omap16xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001157 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
1158
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001159#ifdef CONFIG_ARCH_OMAP24XX
1160 /* Enable autoidle for the OCP interface */
1161 if (cpu_is_omap24xx())
1162 omap_writel(1 << 0, 0x48019010);
1163#endif
1164
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001165 return 0;
1166}
1167
Tony Lindgren92105bb2005-09-07 17:20:26 +01001168#if defined (CONFIG_ARCH_OMAP16XX) || defined (CONFIG_ARCH_OMAP24XX)
1169static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
1170{
1171 int i;
1172
1173 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
1174 return 0;
1175
1176 for (i = 0; i < gpio_bank_count; i++) {
1177 struct gpio_bank *bank = &gpio_bank[i];
1178 void __iomem *wake_status;
1179 void __iomem *wake_clear;
1180 void __iomem *wake_set;
1181
1182 switch (bank->method) {
1183 case METHOD_GPIO_1610:
1184 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1185 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1186 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1187 break;
1188 case METHOD_GPIO_24XX:
1189 wake_status = bank->base + OMAP24XX_GPIO_SETWKUENA;
1190 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1191 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1192 break;
1193 default:
1194 continue;
1195 }
1196
1197 spin_lock(&bank->lock);
1198 bank->saved_wakeup = __raw_readl(wake_status);
1199 __raw_writel(0xffffffff, wake_clear);
1200 __raw_writel(bank->suspend_wakeup, wake_set);
1201 spin_unlock(&bank->lock);
1202 }
1203
1204 return 0;
1205}
1206
1207static int omap_gpio_resume(struct sys_device *dev)
1208{
1209 int i;
1210
1211 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
1212 return 0;
1213
1214 for (i = 0; i < gpio_bank_count; i++) {
1215 struct gpio_bank *bank = &gpio_bank[i];
1216 void __iomem *wake_clear;
1217 void __iomem *wake_set;
1218
1219 switch (bank->method) {
1220 case METHOD_GPIO_1610:
1221 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1222 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1223 break;
1224 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001225 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1226 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001227 break;
1228 default:
1229 continue;
1230 }
1231
1232 spin_lock(&bank->lock);
1233 __raw_writel(0xffffffff, wake_clear);
1234 __raw_writel(bank->saved_wakeup, wake_set);
1235 spin_unlock(&bank->lock);
1236 }
1237
1238 return 0;
1239}
1240
1241static struct sysdev_class omap_gpio_sysclass = {
1242 set_kset_name("gpio"),
1243 .suspend = omap_gpio_suspend,
1244 .resume = omap_gpio_resume,
1245};
1246
1247static struct sys_device omap_gpio_device = {
1248 .id = 0,
1249 .cls = &omap_gpio_sysclass,
1250};
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001251
1252#endif
1253
1254#ifdef CONFIG_ARCH_OMAP24XX
1255
1256static int workaround_enabled;
1257
1258void omap2_gpio_prepare_for_retention(void)
1259{
1260 int i, c = 0;
1261
1262 /* Remove triggering for all non-wakeup GPIOs. Otherwise spurious
1263 * IRQs will be generated. See OMAP2420 Errata item 1.101. */
1264 for (i = 0; i < gpio_bank_count; i++) {
1265 struct gpio_bank *bank = &gpio_bank[i];
1266 u32 l1, l2;
1267
1268 if (!(bank->enabled_non_wakeup_gpios))
1269 continue;
1270 bank->saved_datain = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1271 l1 = __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1272 l2 = __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1273 bank->saved_fallingdetect = l1;
1274 bank->saved_risingdetect = l2;
1275 l1 &= ~bank->enabled_non_wakeup_gpios;
1276 l2 &= ~bank->enabled_non_wakeup_gpios;
1277 __raw_writel(l1, bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1278 __raw_writel(l2, bank->base + OMAP24XX_GPIO_RISINGDETECT);
1279 c++;
1280 }
1281 if (!c) {
1282 workaround_enabled = 0;
1283 return;
1284 }
1285 workaround_enabled = 1;
1286}
1287
1288void omap2_gpio_resume_after_retention(void)
1289{
1290 int i;
1291
1292 if (!workaround_enabled)
1293 return;
1294 for (i = 0; i < gpio_bank_count; i++) {
1295 struct gpio_bank *bank = &gpio_bank[i];
1296 u32 l;
1297
1298 if (!(bank->enabled_non_wakeup_gpios))
1299 continue;
1300 __raw_writel(bank->saved_fallingdetect,
1301 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1302 __raw_writel(bank->saved_risingdetect,
1303 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1304 /* Check if any of the non-wakeup interrupt GPIOs have changed
1305 * state. If so, generate an IRQ by software. This is
1306 * horribly racy, but it's the best we can do to work around
1307 * this silicon bug. */
1308 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1309 l ^= bank->saved_datain;
1310 l &= bank->non_wakeup_gpios;
1311 if (l) {
1312 u32 old0, old1;
1313
1314 old0 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1315 old1 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1316 __raw_writel(old0 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1317 __raw_writel(old1 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1318 __raw_writel(old0, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1319 __raw_writel(old1, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1320 }
1321 }
1322
1323}
1324
Tony Lindgren92105bb2005-09-07 17:20:26 +01001325#endif
1326
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001327/*
1328 * This may get called early from board specific init
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001329 * for boards that have interrupts routed via FPGA.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001330 */
1331int omap_gpio_init(void)
1332{
1333 if (!initialized)
1334 return _omap_gpio_init();
1335 else
1336 return 0;
1337}
1338
Tony Lindgren92105bb2005-09-07 17:20:26 +01001339static int __init omap_gpio_sysinit(void)
1340{
1341 int ret = 0;
1342
1343 if (!initialized)
1344 ret = _omap_gpio_init();
1345
1346#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX)
1347 if (cpu_is_omap16xx() || cpu_is_omap24xx()) {
1348 if (ret == 0) {
1349 ret = sysdev_class_register(&omap_gpio_sysclass);
1350 if (ret == 0)
1351 ret = sysdev_register(&omap_gpio_device);
1352 }
1353 }
1354#endif
1355
1356 return ret;
1357}
1358
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001359EXPORT_SYMBOL(omap_request_gpio);
1360EXPORT_SYMBOL(omap_free_gpio);
1361EXPORT_SYMBOL(omap_set_gpio_direction);
1362EXPORT_SYMBOL(omap_set_gpio_dataout);
1363EXPORT_SYMBOL(omap_get_gpio_datain);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001364
Tony Lindgren92105bb2005-09-07 17:20:26 +01001365arch_initcall(omap_gpio_sysinit);
David Brownellb9772a22006-12-06 17:13:53 -08001366
1367
1368#ifdef CONFIG_DEBUG_FS
1369
1370#include <linux/debugfs.h>
1371#include <linux/seq_file.h>
1372
1373static int gpio_is_input(struct gpio_bank *bank, int mask)
1374{
1375 void __iomem *reg = bank->base;
1376
1377 switch (bank->method) {
1378 case METHOD_MPUIO:
1379 reg += OMAP_MPUIO_IO_CNTL;
1380 break;
1381 case METHOD_GPIO_1510:
1382 reg += OMAP1510_GPIO_DIR_CONTROL;
1383 break;
1384 case METHOD_GPIO_1610:
1385 reg += OMAP1610_GPIO_DIRECTION;
1386 break;
1387 case METHOD_GPIO_730:
1388 reg += OMAP730_GPIO_DIR_CONTROL;
1389 break;
1390 case METHOD_GPIO_24XX:
1391 reg += OMAP24XX_GPIO_OE;
1392 break;
1393 }
1394 return __raw_readl(reg) & mask;
1395}
1396
1397
1398static int dbg_gpio_show(struct seq_file *s, void *unused)
1399{
1400 unsigned i, j, gpio;
1401
1402 for (i = 0, gpio = 0; i < gpio_bank_count; i++) {
1403 struct gpio_bank *bank = gpio_bank + i;
1404 unsigned bankwidth = 16;
1405 u32 mask = 1;
1406
1407 if (!cpu_is_omap24xx() && bank->method == METHOD_MPUIO)
1408 gpio = OMAP_MPUIO(0);
1409 else if (cpu_is_omap24xx() || cpu_is_omap730())
1410 bankwidth = 32;
1411
1412 for (j = 0; j < bankwidth; j++, gpio++, mask <<= 1) {
1413 unsigned irq, value, is_in, irqstat;
1414
1415 if (!(bank->reserved_map & mask))
1416 continue;
1417
1418 irq = bank->virtual_irq_start + j;
1419 value = omap_get_gpio_datain(gpio);
1420 is_in = gpio_is_input(bank, mask);
1421
1422 if (!cpu_is_omap24xx() && bank->method == METHOD_MPUIO)
1423 seq_printf(s, "MPUIO %2d: ", j);
1424 else
1425 seq_printf(s, "GPIO %3d: ", gpio);
1426 seq_printf(s, "%s %s",
1427 is_in ? "in " : "out",
1428 value ? "hi" : "lo");
1429
1430 irqstat = irq_desc[irq].status;
1431 if (is_in && ((bank->suspend_wakeup & mask)
1432 || irqstat & IRQ_TYPE_SENSE_MASK)) {
1433 char *trigger = NULL;
1434
1435 switch (irqstat & IRQ_TYPE_SENSE_MASK) {
1436 case IRQ_TYPE_EDGE_FALLING:
1437 trigger = "falling";
1438 break;
1439 case IRQ_TYPE_EDGE_RISING:
1440 trigger = "rising";
1441 break;
1442 case IRQ_TYPE_EDGE_BOTH:
1443 trigger = "bothedge";
1444 break;
1445 case IRQ_TYPE_LEVEL_LOW:
1446 trigger = "low";
1447 break;
1448 case IRQ_TYPE_LEVEL_HIGH:
1449 trigger = "high";
1450 break;
1451 case IRQ_TYPE_NONE:
1452 trigger = "(unspecified)";
1453 break;
1454 }
1455 seq_printf(s, ", irq-%d %s%s",
1456 irq, trigger,
1457 (bank->suspend_wakeup & mask)
1458 ? " wakeup" : "");
1459 }
1460 seq_printf(s, "\n");
1461 }
1462
1463 if (!cpu_is_omap24xx() && bank->method == METHOD_MPUIO) {
1464 seq_printf(s, "\n");
1465 gpio = 0;
1466 }
1467 }
1468 return 0;
1469}
1470
1471static int dbg_gpio_open(struct inode *inode, struct file *file)
1472{
1473 return single_open(file, dbg_gpio_show, inode->u.generic_ip/*i_private*/);
1474}
1475
1476static const struct file_operations debug_fops = {
1477 .open = dbg_gpio_open,
1478 .read = seq_read,
1479 .llseek = seq_lseek,
1480 .release = single_release,
1481};
1482
1483static int __init omap_gpio_debuginit(void)
1484{
1485 (void) debugfs_create_file("omap_gpio", S_IRUGO, NULL, NULL, &debug_fops);
1486 return 0;
1487}
1488late_initcall(omap_gpio_debuginit);
1489#endif