blob: 55059a24ad419da31fbc026f5345a9db386ee9fc [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
14#include <linux/config.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/sched.h>
18#include <linux/interrupt.h>
19#include <linux/ptrace.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010020#include <linux/sysdev.h>
21#include <linux/err.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010022
23#include <asm/hardware.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010024#include <asm/hardware/clock.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010025#include <asm/irq.h>
26#include <asm/arch/irqs.h>
27#include <asm/arch/gpio.h>
28#include <asm/mach/irq.h>
29
30#include <asm/io.h>
31
32/*
33 * OMAP1510 GPIO registers
34 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010035#define OMAP1510_GPIO_BASE (void __iomem *)0xfffce000
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010036#define OMAP1510_GPIO_DATA_INPUT 0x00
37#define OMAP1510_GPIO_DATA_OUTPUT 0x04
38#define OMAP1510_GPIO_DIR_CONTROL 0x08
39#define OMAP1510_GPIO_INT_CONTROL 0x0c
40#define OMAP1510_GPIO_INT_MASK 0x10
41#define OMAP1510_GPIO_INT_STATUS 0x14
42#define OMAP1510_GPIO_PIN_CONTROL 0x18
43
44#define OMAP1510_IH_GPIO_BASE 64
45
46/*
47 * OMAP1610 specific GPIO registers
48 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010049#define OMAP1610_GPIO1_BASE (void __iomem *)0xfffbe400
50#define OMAP1610_GPIO2_BASE (void __iomem *)0xfffbec00
51#define OMAP1610_GPIO3_BASE (void __iomem *)0xfffbb400
52#define OMAP1610_GPIO4_BASE (void __iomem *)0xfffbbc00
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010053#define OMAP1610_GPIO_REVISION 0x0000
54#define OMAP1610_GPIO_SYSCONFIG 0x0010
55#define OMAP1610_GPIO_SYSSTATUS 0x0014
56#define OMAP1610_GPIO_IRQSTATUS1 0x0018
57#define OMAP1610_GPIO_IRQENABLE1 0x001c
Tony Lindgren92105bb2005-09-07 17:20:26 +010058#define OMAP1610_GPIO_WAKEUPENABLE 0x0028
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010059#define OMAP1610_GPIO_DATAIN 0x002c
60#define OMAP1610_GPIO_DATAOUT 0x0030
61#define OMAP1610_GPIO_DIRECTION 0x0034
62#define OMAP1610_GPIO_EDGE_CTRL1 0x0038
63#define OMAP1610_GPIO_EDGE_CTRL2 0x003c
64#define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
Tony Lindgren92105bb2005-09-07 17:20:26 +010065#define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010066#define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
67#define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
Tony Lindgren92105bb2005-09-07 17:20:26 +010068#define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010069#define OMAP1610_GPIO_SET_DATAOUT 0x00f0
70
71/*
72 * OMAP730 specific GPIO registers
73 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010074#define OMAP730_GPIO1_BASE (void __iomem *)0xfffbc000
75#define OMAP730_GPIO2_BASE (void __iomem *)0xfffbc800
76#define OMAP730_GPIO3_BASE (void __iomem *)0xfffbd000
77#define OMAP730_GPIO4_BASE (void __iomem *)0xfffbd800
78#define OMAP730_GPIO5_BASE (void __iomem *)0xfffbe000
79#define OMAP730_GPIO6_BASE (void __iomem *)0xfffbe800
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010080#define OMAP730_GPIO_DATA_INPUT 0x00
81#define OMAP730_GPIO_DATA_OUTPUT 0x04
82#define OMAP730_GPIO_DIR_CONTROL 0x08
83#define OMAP730_GPIO_INT_CONTROL 0x0c
84#define OMAP730_GPIO_INT_MASK 0x10
85#define OMAP730_GPIO_INT_STATUS 0x14
86
Tony Lindgren92105bb2005-09-07 17:20:26 +010087/*
88 * omap24xx specific GPIO registers
89 */
90#define OMAP24XX_GPIO1_BASE (void __iomem *)0x48018000
91#define OMAP24XX_GPIO2_BASE (void __iomem *)0x4801a000
92#define OMAP24XX_GPIO3_BASE (void __iomem *)0x4801c000
93#define OMAP24XX_GPIO4_BASE (void __iomem *)0x4801e000
94#define OMAP24XX_GPIO_REVISION 0x0000
95#define OMAP24XX_GPIO_SYSCONFIG 0x0010
96#define OMAP24XX_GPIO_SYSSTATUS 0x0014
97#define OMAP24XX_GPIO_IRQSTATUS1 0x0018
98#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 +0100114#define OMAP_MPUIO_MASK (~OMAP_MAX_GPIO_LINES & 0xff)
115
116struct gpio_bank {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100117 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100118 u16 irq;
119 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100120 int method;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121 u32 reserved_map;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100122 u32 suspend_wakeup;
123 u32 saved_wakeup;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100124 spinlock_t lock;
125};
126
127#define METHOD_MPUIO 0
128#define METHOD_GPIO_1510 1
129#define METHOD_GPIO_1610 2
130#define METHOD_GPIO_730 3
Tony Lindgren92105bb2005-09-07 17:20:26 +0100131#define METHOD_GPIO_24XX 4
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132
Tony Lindgren92105bb2005-09-07 17:20:26 +0100133#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134static struct gpio_bank gpio_bank_1610[5] = {
135 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
136 { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
137 { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
138 { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
139 { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
140};
141#endif
142
143#ifdef CONFIG_ARCH_OMAP1510
144static struct gpio_bank gpio_bank_1510[2] = {
145 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
146 { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
147};
148#endif
149
150#ifdef CONFIG_ARCH_OMAP730
151static struct gpio_bank gpio_bank_730[7] = {
152 { OMAP_MPUIO_BASE, INT_730_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
153 { OMAP730_GPIO1_BASE, INT_730_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_730 },
154 { OMAP730_GPIO2_BASE, INT_730_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_730 },
155 { OMAP730_GPIO3_BASE, INT_730_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_730 },
156 { OMAP730_GPIO4_BASE, INT_730_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_730 },
157 { OMAP730_GPIO5_BASE, INT_730_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_730 },
158 { OMAP730_GPIO6_BASE, INT_730_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_730 },
159};
160#endif
161
Tony Lindgren92105bb2005-09-07 17:20:26 +0100162#ifdef CONFIG_ARCH_OMAP24XX
163static struct gpio_bank gpio_bank_24xx[4] = {
164 { OMAP24XX_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
165 { OMAP24XX_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
166 { OMAP24XX_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
167 { OMAP24XX_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
168};
169#endif
170
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100171static struct gpio_bank *gpio_bank;
172static int gpio_bank_count;
173
174static inline struct gpio_bank *get_gpio_bank(int gpio)
175{
176#ifdef CONFIG_ARCH_OMAP1510
177 if (cpu_is_omap1510()) {
178 if (OMAP_GPIO_IS_MPUIO(gpio))
179 return &gpio_bank[0];
180 return &gpio_bank[1];
181 }
182#endif
183#if defined(CONFIG_ARCH_OMAP16XX)
184 if (cpu_is_omap16xx()) {
185 if (OMAP_GPIO_IS_MPUIO(gpio))
186 return &gpio_bank[0];
187 return &gpio_bank[1 + (gpio >> 4)];
188 }
189#endif
190#ifdef CONFIG_ARCH_OMAP730
191 if (cpu_is_omap730()) {
192 if (OMAP_GPIO_IS_MPUIO(gpio))
193 return &gpio_bank[0];
194 return &gpio_bank[1 + (gpio >> 5)];
195 }
196#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100197#ifdef CONFIG_ARCH_OMAP24XX
198 if (cpu_is_omap24xx())
199 return &gpio_bank[gpio >> 5];
200#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100201}
202
203static inline int get_gpio_index(int gpio)
204{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100205#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100206 if (cpu_is_omap730())
207 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100208#endif
209#ifdef CONFIG_ARCH_OMAP24XX
210 if (cpu_is_omap24xx())
211 return gpio & 0x1f;
212#endif
213 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100214}
215
216static inline int gpio_valid(int gpio)
217{
218 if (gpio < 0)
219 return -1;
220 if (OMAP_GPIO_IS_MPUIO(gpio)) {
221 if ((gpio & OMAP_MPUIO_MASK) > 16)
222 return -1;
223 return 0;
224 }
225#ifdef CONFIG_ARCH_OMAP1510
226 if (cpu_is_omap1510() && gpio < 16)
227 return 0;
228#endif
229#if defined(CONFIG_ARCH_OMAP16XX)
230 if ((cpu_is_omap16xx()) && gpio < 64)
231 return 0;
232#endif
233#ifdef CONFIG_ARCH_OMAP730
234 if (cpu_is_omap730() && gpio < 192)
235 return 0;
236#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100237#ifdef CONFIG_ARCH_OMAP24XX
238 if (cpu_is_omap24xx() && gpio < 128)
239 return 0;
240#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100241 return -1;
242}
243
244static int check_gpio(int gpio)
245{
246 if (unlikely(gpio_valid(gpio)) < 0) {
247 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
248 dump_stack();
249 return -1;
250 }
251 return 0;
252}
253
254static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
255{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100256 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100257 u32 l;
258
259 switch (bank->method) {
260 case METHOD_MPUIO:
261 reg += OMAP_MPUIO_IO_CNTL;
262 break;
263 case METHOD_GPIO_1510:
264 reg += OMAP1510_GPIO_DIR_CONTROL;
265 break;
266 case METHOD_GPIO_1610:
267 reg += OMAP1610_GPIO_DIRECTION;
268 break;
269 case METHOD_GPIO_730:
270 reg += OMAP730_GPIO_DIR_CONTROL;
271 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100272 case METHOD_GPIO_24XX:
273 reg += OMAP24XX_GPIO_OE;
274 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100275 }
276 l = __raw_readl(reg);
277 if (is_input)
278 l |= 1 << gpio;
279 else
280 l &= ~(1 << gpio);
281 __raw_writel(l, reg);
282}
283
284void omap_set_gpio_direction(int gpio, int is_input)
285{
286 struct gpio_bank *bank;
287
288 if (check_gpio(gpio) < 0)
289 return;
290 bank = get_gpio_bank(gpio);
291 spin_lock(&bank->lock);
292 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
293 spin_unlock(&bank->lock);
294}
295
296static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
297{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100298 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100299 u32 l = 0;
300
301 switch (bank->method) {
302 case METHOD_MPUIO:
303 reg += OMAP_MPUIO_OUTPUT;
304 l = __raw_readl(reg);
305 if (enable)
306 l |= 1 << gpio;
307 else
308 l &= ~(1 << gpio);
309 break;
310 case METHOD_GPIO_1510:
311 reg += OMAP1510_GPIO_DATA_OUTPUT;
312 l = __raw_readl(reg);
313 if (enable)
314 l |= 1 << gpio;
315 else
316 l &= ~(1 << gpio);
317 break;
318 case METHOD_GPIO_1610:
319 if (enable)
320 reg += OMAP1610_GPIO_SET_DATAOUT;
321 else
322 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
323 l = 1 << gpio;
324 break;
325 case METHOD_GPIO_730:
326 reg += OMAP730_GPIO_DATA_OUTPUT;
327 l = __raw_readl(reg);
328 if (enable)
329 l |= 1 << gpio;
330 else
331 l &= ~(1 << gpio);
332 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100333 case METHOD_GPIO_24XX:
334 if (enable)
335 reg += OMAP24XX_GPIO_SETDATAOUT;
336 else
337 reg += OMAP24XX_GPIO_CLEARDATAOUT;
338 l = 1 << gpio;
339 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340 default:
341 BUG();
342 return;
343 }
344 __raw_writel(l, reg);
345}
346
347void omap_set_gpio_dataout(int gpio, int enable)
348{
349 struct gpio_bank *bank;
350
351 if (check_gpio(gpio) < 0)
352 return;
353 bank = get_gpio_bank(gpio);
354 spin_lock(&bank->lock);
355 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
356 spin_unlock(&bank->lock);
357}
358
359int omap_get_gpio_datain(int gpio)
360{
361 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100362 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100363
364 if (check_gpio(gpio) < 0)
365 return -1;
366 bank = get_gpio_bank(gpio);
367 reg = bank->base;
368 switch (bank->method) {
369 case METHOD_MPUIO:
370 reg += OMAP_MPUIO_INPUT_LATCH;
371 break;
372 case METHOD_GPIO_1510:
373 reg += OMAP1510_GPIO_DATA_INPUT;
374 break;
375 case METHOD_GPIO_1610:
376 reg += OMAP1610_GPIO_DATAIN;
377 break;
378 case METHOD_GPIO_730:
379 reg += OMAP730_GPIO_DATA_INPUT;
380 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100381 case METHOD_GPIO_24XX:
382 reg += OMAP24XX_GPIO_DATAIN;
383 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100384 default:
385 BUG();
386 return -1;
387 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100388 return (__raw_readl(reg)
389 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100390}
391
Tony Lindgren92105bb2005-09-07 17:20:26 +0100392#define MOD_REG_BIT(reg, bit_mask, set) \
393do { \
394 int l = __raw_readl(base + reg); \
395 if (set) l |= bit_mask; \
396 else l &= ~bit_mask; \
397 __raw_writel(l, base + reg); \
398} while(0)
399
400static inline void set_24xx_gpio_triggering(void __iomem *base, int gpio, int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100401{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100402 u32 gpio_bit = 1 << gpio;
403
404 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
405 trigger & IRQT_LOW);
406 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
407 trigger & IRQT_HIGH);
408 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
409 trigger & IRQT_RISING);
410 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
411 trigger & IRQT_FALLING);
412 /* FIXME: Possibly do 'set_irq_handler(j, do_level_IRQ)' if only level
413 * triggering requested. */
414}
415
416static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
417{
418 void __iomem *reg = bank->base;
419 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100420
421 switch (bank->method) {
422 case METHOD_MPUIO:
423 reg += OMAP_MPUIO_GPIO_INT_EDGE;
424 l = __raw_readl(reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100425 if (trigger == IRQT_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100426 l |= 1 << gpio;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100427 else if (trigger == IRQT_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100428 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100429 else
430 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100431 break;
432 case METHOD_GPIO_1510:
433 reg += OMAP1510_GPIO_INT_CONTROL;
434 l = __raw_readl(reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100435 if (trigger == IRQT_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100436 l |= 1 << gpio;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100437 else if (trigger == IRQT_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100438 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100439 else
440 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100441 break;
442 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100443 if (gpio & 0x08)
444 reg += OMAP1610_GPIO_EDGE_CTRL2;
445 else
446 reg += OMAP1610_GPIO_EDGE_CTRL1;
447 gpio &= 0x07;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100448 /* We allow only edge triggering, i.e. two lowest bits */
449 if (trigger & ~IRQT_BOTHEDGE)
450 BUG();
451 /* NOTE: knows __IRQT_{FAL,RIS}EDGE match OMAP hardware */
452 trigger &= 0x03;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100453 l = __raw_readl(reg);
454 l &= ~(3 << (gpio << 1));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100455 l |= trigger << (gpio << 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100456 break;
457 case METHOD_GPIO_730:
458 reg += OMAP730_GPIO_INT_CONTROL;
459 l = __raw_readl(reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100460 if (trigger == IRQT_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100461 l |= 1 << gpio;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100462 else if (trigger == IRQT_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100463 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100464 else
465 goto bad;
466 break;
467 case METHOD_GPIO_24XX:
468 set_24xx_gpio_triggering(reg, gpio, trigger);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100469 break;
470 default:
471 BUG();
Tony Lindgren92105bb2005-09-07 17:20:26 +0100472 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100473 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100474 __raw_writel(l, reg);
475 return 0;
476bad:
477 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100478}
479
Tony Lindgren92105bb2005-09-07 17:20:26 +0100480static int gpio_irq_type(unsigned irq, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100481{
482 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100483 unsigned gpio;
484 int retval;
485
486 if (irq > IH_MPUIO_BASE)
487 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
488 else
489 gpio = irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100490
491 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100492 return -EINVAL;
493
494 if (type & (__IRQT_LOWLVL|__IRQT_HIGHLVL|IRQT_PROBE))
495 return -EINVAL;
496
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100497 bank = get_gpio_bank(gpio);
498 spin_lock(&bank->lock);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100499 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100500 spin_unlock(&bank->lock);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100501 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100502}
503
504static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
505{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100506 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100507
508 switch (bank->method) {
509 case METHOD_MPUIO:
510 /* MPUIO irqstatus is reset by reading the status register,
511 * so do nothing here */
512 return;
513 case METHOD_GPIO_1510:
514 reg += OMAP1510_GPIO_INT_STATUS;
515 break;
516 case METHOD_GPIO_1610:
517 reg += OMAP1610_GPIO_IRQSTATUS1;
518 break;
519 case METHOD_GPIO_730:
520 reg += OMAP730_GPIO_INT_STATUS;
521 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100522 case METHOD_GPIO_24XX:
523 reg += OMAP24XX_GPIO_IRQSTATUS1;
524 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100525 default:
526 BUG();
527 return;
528 }
529 __raw_writel(gpio_mask, reg);
530}
531
532static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
533{
534 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
535}
536
537static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
538{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100539 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100540 u32 l;
541
542 switch (bank->method) {
543 case METHOD_MPUIO:
544 reg += OMAP_MPUIO_GPIO_MASKIT;
545 l = __raw_readl(reg);
546 if (enable)
547 l &= ~(gpio_mask);
548 else
549 l |= gpio_mask;
550 break;
551 case METHOD_GPIO_1510:
552 reg += OMAP1510_GPIO_INT_MASK;
553 l = __raw_readl(reg);
554 if (enable)
555 l &= ~(gpio_mask);
556 else
557 l |= gpio_mask;
558 break;
559 case METHOD_GPIO_1610:
560 if (enable)
561 reg += OMAP1610_GPIO_SET_IRQENABLE1;
562 else
563 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
564 l = gpio_mask;
565 break;
566 case METHOD_GPIO_730:
567 reg += OMAP730_GPIO_INT_MASK;
568 l = __raw_readl(reg);
569 if (enable)
570 l &= ~(gpio_mask);
571 else
572 l |= gpio_mask;
573 break;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100574 case METHOD_GPIO_24XX:
575 if (enable)
576 reg += OMAP24XX_GPIO_SETIRQENABLE1;
577 else
578 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
579 l = gpio_mask;
580 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100581 default:
582 BUG();
583 return;
584 }
585 __raw_writel(l, reg);
586}
587
588static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
589{
590 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
591}
592
Tony Lindgren92105bb2005-09-07 17:20:26 +0100593/*
594 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
595 * 1510 does not seem to have a wake-up register. If JTAG is connected
596 * to the target, system will wake up always on GPIO events. While
597 * system is running all registered GPIO interrupts need to have wake-up
598 * enabled. When system is suspended, only selected GPIO interrupts need
599 * to have wake-up enabled.
600 */
601static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
602{
603 switch (bank->method) {
604 case METHOD_GPIO_1610:
605 case METHOD_GPIO_24XX:
606 spin_lock(&bank->lock);
607 if (enable)
608 bank->suspend_wakeup |= (1 << gpio);
609 else
610 bank->suspend_wakeup &= ~(1 << gpio);
611 spin_unlock(&bank->lock);
612 return 0;
613 default:
614 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
615 bank->method);
616 return -EINVAL;
617 }
618}
619
620/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
621static int gpio_wake_enable(unsigned int irq, unsigned int enable)
622{
623 unsigned int gpio = irq - IH_GPIO_BASE;
624 struct gpio_bank *bank;
625 int retval;
626
627 if (check_gpio(gpio) < 0)
628 return -ENODEV;
629 bank = get_gpio_bank(gpio);
630 spin_lock(&bank->lock);
631 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
632 spin_unlock(&bank->lock);
633
634 return retval;
635}
636
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100637int omap_request_gpio(int gpio)
638{
639 struct gpio_bank *bank;
640
641 if (check_gpio(gpio) < 0)
642 return -EINVAL;
643
644 bank = get_gpio_bank(gpio);
645 spin_lock(&bank->lock);
646 if (unlikely(bank->reserved_map & (1 << get_gpio_index(gpio)))) {
647 printk(KERN_ERR "omap-gpio: GPIO %d is already reserved!\n", gpio);
648 dump_stack();
649 spin_unlock(&bank->lock);
650 return -1;
651 }
652 bank->reserved_map |= (1 << get_gpio_index(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100653
654 /* Set trigger to none. You need to enable the trigger after request_irq */
655 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQT_NOEDGE);
656
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100657#ifdef CONFIG_ARCH_OMAP1510
658 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100659 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100660
Tony Lindgren92105bb2005-09-07 17:20:26 +0100661 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100662 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
663 __raw_writel(__raw_readl(reg) | (1 << get_gpio_index(gpio)), reg);
664 }
665#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100666#ifdef CONFIG_ARCH_OMAP16XX
667 if (bank->method == METHOD_GPIO_1610) {
668 /* Enable wake-up during idle for dynamic tick */
669 void __iomem *reg = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
670 __raw_writel(1 << get_gpio_index(gpio), reg);
671 }
672#endif
673#ifdef CONFIG_ARCH_OMAP24XX
674 if (bank->method == METHOD_GPIO_24XX) {
675 /* Enable wake-up during idle for dynamic tick */
676 void __iomem *reg = bank->base + OMAP24XX_GPIO_SETWKUENA;
677 __raw_writel(1 << get_gpio_index(gpio), reg);
678 }
679#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100680 spin_unlock(&bank->lock);
681
682 return 0;
683}
684
685void omap_free_gpio(int gpio)
686{
687 struct gpio_bank *bank;
688
689 if (check_gpio(gpio) < 0)
690 return;
691 bank = get_gpio_bank(gpio);
692 spin_lock(&bank->lock);
693 if (unlikely(!(bank->reserved_map & (1 << get_gpio_index(gpio))))) {
694 printk(KERN_ERR "omap-gpio: GPIO %d wasn't reserved!\n", gpio);
695 dump_stack();
696 spin_unlock(&bank->lock);
697 return;
698 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100699#ifdef CONFIG_ARCH_OMAP16XX
700 if (bank->method == METHOD_GPIO_1610) {
701 /* Disable wake-up during idle for dynamic tick */
702 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
703 __raw_writel(1 << get_gpio_index(gpio), reg);
704 }
705#endif
706#ifdef CONFIG_ARCH_OMAP24XX
707 if (bank->method == METHOD_GPIO_24XX) {
708 /* Disable wake-up during idle for dynamic tick */
709 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
710 __raw_writel(1 << get_gpio_index(gpio), reg);
711 }
712#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100713 bank->reserved_map &= ~(1 << get_gpio_index(gpio));
714 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
715 _set_gpio_irqenable(bank, gpio, 0);
716 _clear_gpio_irqstatus(bank, gpio);
717 spin_unlock(&bank->lock);
718}
719
720/*
721 * We need to unmask the GPIO bank interrupt as soon as possible to
722 * avoid missing GPIO interrupts for other lines in the bank.
723 * Then we need to mask-read-clear-unmask the triggered GPIO lines
724 * in the bank to avoid missing nested interrupts for a GPIO line.
725 * If we wait to unmask individual GPIO lines in the bank after the
726 * line's interrupt handler has been run, we may miss some nested
727 * interrupts.
728 */
729static void gpio_irq_handler(unsigned int irq, struct irqdesc *desc,
730 struct pt_regs *regs)
731{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100732 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100733 u32 isr;
734 unsigned int gpio_irq;
735 struct gpio_bank *bank;
736
737 desc->chip->ack(irq);
738
739 bank = (struct gpio_bank *) desc->data;
740 if (bank->method == METHOD_MPUIO)
741 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
742#ifdef CONFIG_ARCH_OMAP1510
743 if (bank->method == METHOD_GPIO_1510)
744 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
745#endif
746#if defined(CONFIG_ARCH_OMAP16XX)
747 if (bank->method == METHOD_GPIO_1610)
748 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
749#endif
750#ifdef CONFIG_ARCH_OMAP730
751 if (bank->method == METHOD_GPIO_730)
752 isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
753#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100754#ifdef CONFIG_ARCH_OMAP24XX
755 if (bank->method == METHOD_GPIO_24XX)
756 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
757#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100758
Tony Lindgren92105bb2005-09-07 17:20:26 +0100759 while(1) {
760 isr = __raw_readl(isr_reg);
761 _enable_gpio_irqbank(bank, isr, 0);
762 _clear_gpio_irqbank(bank, isr);
763 _enable_gpio_irqbank(bank, isr, 1);
764 desc->chip->unmask(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100765
Tony Lindgren92105bb2005-09-07 17:20:26 +0100766 if (!isr)
767 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100768
Tony Lindgren92105bb2005-09-07 17:20:26 +0100769 gpio_irq = bank->virtual_irq_start;
770 for (; isr != 0; isr >>= 1, gpio_irq++) {
771 struct irqdesc *d;
772 if (!(isr & 1))
773 continue;
774 d = irq_desc + gpio_irq;
775 desc_handle_irq(gpio_irq, d, regs);
776 }
777 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100778}
779
780static void gpio_ack_irq(unsigned int irq)
781{
782 unsigned int gpio = irq - IH_GPIO_BASE;
783 struct gpio_bank *bank = get_gpio_bank(gpio);
784
785 _clear_gpio_irqstatus(bank, gpio);
786}
787
788static void gpio_mask_irq(unsigned int irq)
789{
790 unsigned int gpio = irq - IH_GPIO_BASE;
791 struct gpio_bank *bank = get_gpio_bank(gpio);
792
793 _set_gpio_irqenable(bank, gpio, 0);
794}
795
796static void gpio_unmask_irq(unsigned int irq)
797{
798 unsigned int gpio = irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100799 unsigned int gpio_idx = get_gpio_index(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100800 struct gpio_bank *bank = get_gpio_bank(gpio);
801
Tony Lindgren92105bb2005-09-07 17:20:26 +0100802 _set_gpio_irqenable(bank, gpio_idx, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100803}
804
805static void mpuio_ack_irq(unsigned int irq)
806{
807 /* The ISR is reset automatically, so do nothing here. */
808}
809
810static void mpuio_mask_irq(unsigned int irq)
811{
812 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
813 struct gpio_bank *bank = get_gpio_bank(gpio);
814
815 _set_gpio_irqenable(bank, gpio, 0);
816}
817
818static void mpuio_unmask_irq(unsigned int irq)
819{
820 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
821 struct gpio_bank *bank = get_gpio_bank(gpio);
822
823 _set_gpio_irqenable(bank, gpio, 1);
824}
825
826static struct irqchip gpio_irq_chip = {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100827 .ack = gpio_ack_irq,
828 .mask = gpio_mask_irq,
829 .unmask = gpio_unmask_irq,
830 .set_type = gpio_irq_type,
831 .set_wake = gpio_wake_enable,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100832};
833
834static struct irqchip mpuio_irq_chip = {
835 .ack = mpuio_ack_irq,
836 .mask = mpuio_mask_irq,
837 .unmask = mpuio_unmask_irq
838};
839
840static int initialized = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100841static struct clk * gpio_ck = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100842
843static int __init _omap_gpio_init(void)
844{
845 int i;
846 struct gpio_bank *bank;
847
848 initialized = 1;
849
Tony Lindgren92105bb2005-09-07 17:20:26 +0100850 if (cpu_is_omap1510()) {
851 gpio_ck = clk_get(NULL, "arm_gpio_ck");
852 if (IS_ERR(gpio_ck))
853 printk("Could not get arm_gpio_ck\n");
854 else
855 clk_use(gpio_ck);
856 }
857
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100858#ifdef CONFIG_ARCH_OMAP1510
859 if (cpu_is_omap1510()) {
860 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
861 gpio_bank_count = 2;
862 gpio_bank = gpio_bank_1510;
863 }
864#endif
865#if defined(CONFIG_ARCH_OMAP16XX)
866 if (cpu_is_omap16xx()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100867 u32 rev;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100868
869 gpio_bank_count = 5;
870 gpio_bank = gpio_bank_1610;
871 rev = omap_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
872 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
873 (rev >> 4) & 0x0f, rev & 0x0f);
874 }
875#endif
876#ifdef CONFIG_ARCH_OMAP730
877 if (cpu_is_omap730()) {
878 printk(KERN_INFO "OMAP730 GPIO hardware\n");
879 gpio_bank_count = 7;
880 gpio_bank = gpio_bank_730;
881 }
882#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100883#ifdef CONFIG_ARCH_OMAP24XX
884 if (cpu_is_omap24xx()) {
885 int rev;
886
887 gpio_bank_count = 4;
888 gpio_bank = gpio_bank_24xx;
889 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
890 printk(KERN_INFO "OMAP24xx GPIO hardware version %d.%d\n",
891 (rev >> 4) & 0x0f, rev & 0x0f);
892 }
893#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100894 for (i = 0; i < gpio_bank_count; i++) {
895 int j, gpio_count = 16;
896
897 bank = &gpio_bank[i];
898 bank->reserved_map = 0;
899 bank->base = IO_ADDRESS(bank->base);
900 spin_lock_init(&bank->lock);
901 if (bank->method == METHOD_MPUIO) {
902 omap_writew(0xFFFF, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_MASKIT);
903 }
904#ifdef CONFIG_ARCH_OMAP1510
905 if (bank->method == METHOD_GPIO_1510) {
906 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
907 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
908 }
909#endif
910#if defined(CONFIG_ARCH_OMAP16XX)
911 if (bank->method == METHOD_GPIO_1610) {
912 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
913 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100914 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100915 }
916#endif
917#ifdef CONFIG_ARCH_OMAP730
918 if (bank->method == METHOD_GPIO_730) {
919 __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
920 __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
921
922 gpio_count = 32; /* 730 has 32-bit GPIOs */
923 }
924#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100925#ifdef CONFIG_ARCH_OMAP24XX
926 if (bank->method == METHOD_GPIO_24XX) {
927 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
928 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
929
930 gpio_count = 32;
931 }
932#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100933 for (j = bank->virtual_irq_start;
934 j < bank->virtual_irq_start + gpio_count; j++) {
935 if (bank->method == METHOD_MPUIO)
936 set_irq_chip(j, &mpuio_irq_chip);
937 else
938 set_irq_chip(j, &gpio_irq_chip);
939 set_irq_handler(j, do_simple_IRQ);
940 set_irq_flags(j, IRQF_VALID);
941 }
942 set_irq_chained_handler(bank->irq, gpio_irq_handler);
943 set_irq_data(bank->irq, bank);
944 }
945
946 /* Enable system clock for GPIO module.
947 * The CAM_CLK_CTRL *is* really the right place. */
Tony Lindgren92105bb2005-09-07 17:20:26 +0100948 if (cpu_is_omap16xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100949 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
950
951 return 0;
952}
953
Tony Lindgren92105bb2005-09-07 17:20:26 +0100954#if defined (CONFIG_ARCH_OMAP16XX) || defined (CONFIG_ARCH_OMAP24XX)
955static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
956{
957 int i;
958
959 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
960 return 0;
961
962 for (i = 0; i < gpio_bank_count; i++) {
963 struct gpio_bank *bank = &gpio_bank[i];
964 void __iomem *wake_status;
965 void __iomem *wake_clear;
966 void __iomem *wake_set;
967
968 switch (bank->method) {
969 case METHOD_GPIO_1610:
970 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
971 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
972 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
973 break;
974 case METHOD_GPIO_24XX:
975 wake_status = bank->base + OMAP24XX_GPIO_SETWKUENA;
976 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
977 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
978 break;
979 default:
980 continue;
981 }
982
983 spin_lock(&bank->lock);
984 bank->saved_wakeup = __raw_readl(wake_status);
985 __raw_writel(0xffffffff, wake_clear);
986 __raw_writel(bank->suspend_wakeup, wake_set);
987 spin_unlock(&bank->lock);
988 }
989
990 return 0;
991}
992
993static int omap_gpio_resume(struct sys_device *dev)
994{
995 int i;
996
997 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
998 return 0;
999
1000 for (i = 0; i < gpio_bank_count; i++) {
1001 struct gpio_bank *bank = &gpio_bank[i];
1002 void __iomem *wake_clear;
1003 void __iomem *wake_set;
1004
1005 switch (bank->method) {
1006 case METHOD_GPIO_1610:
1007 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1008 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1009 break;
1010 case METHOD_GPIO_24XX:
1011 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1012 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1013 break;
1014 default:
1015 continue;
1016 }
1017
1018 spin_lock(&bank->lock);
1019 __raw_writel(0xffffffff, wake_clear);
1020 __raw_writel(bank->saved_wakeup, wake_set);
1021 spin_unlock(&bank->lock);
1022 }
1023
1024 return 0;
1025}
1026
1027static struct sysdev_class omap_gpio_sysclass = {
1028 set_kset_name("gpio"),
1029 .suspend = omap_gpio_suspend,
1030 .resume = omap_gpio_resume,
1031};
1032
1033static struct sys_device omap_gpio_device = {
1034 .id = 0,
1035 .cls = &omap_gpio_sysclass,
1036};
1037#endif
1038
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001039/*
1040 * This may get called early from board specific init
1041 */
1042int omap_gpio_init(void)
1043{
1044 if (!initialized)
1045 return _omap_gpio_init();
1046 else
1047 return 0;
1048}
1049
Tony Lindgren92105bb2005-09-07 17:20:26 +01001050static int __init omap_gpio_sysinit(void)
1051{
1052 int ret = 0;
1053
1054 if (!initialized)
1055 ret = _omap_gpio_init();
1056
1057#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX)
1058 if (cpu_is_omap16xx() || cpu_is_omap24xx()) {
1059 if (ret == 0) {
1060 ret = sysdev_class_register(&omap_gpio_sysclass);
1061 if (ret == 0)
1062 ret = sysdev_register(&omap_gpio_device);
1063 }
1064 }
1065#endif
1066
1067 return ret;
1068}
1069
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001070EXPORT_SYMBOL(omap_request_gpio);
1071EXPORT_SYMBOL(omap_free_gpio);
1072EXPORT_SYMBOL(omap_set_gpio_direction);
1073EXPORT_SYMBOL(omap_set_gpio_dataout);
1074EXPORT_SYMBOL(omap_get_gpio_datain);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001075
Tony Lindgren92105bb2005-09-07 17:20:26 +01001076arch_initcall(omap_gpio_sysinit);