blob: 66e364738fbbdeaa7143f2643ada9d02bac992a7 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
2 * linux/arch/arm/plat-omap/gpio.c
3 *
4 * Support functions for OMAP GPIO
5 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01006 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02007 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010014#include <linux/init.h>
15#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010016#include <linux/interrupt.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010017#include <linux/sysdev.h>
18#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000019#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010020#include <linux/io.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010021
Russell Kinga09e64f2008-08-05 16:14:15 +010022#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010023#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010024#include <mach/irqs.h>
25#include <mach/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026#include <asm/mach/irq.h>
27
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010028/*
29 * OMAP1510 GPIO registers
30 */
Russell King7c7095a2008-09-05 15:49:14 +010031#define OMAP1510_GPIO_BASE IO_ADDRESS(0xfffce000)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010032#define OMAP1510_GPIO_DATA_INPUT 0x00
33#define OMAP1510_GPIO_DATA_OUTPUT 0x04
34#define OMAP1510_GPIO_DIR_CONTROL 0x08
35#define OMAP1510_GPIO_INT_CONTROL 0x0c
36#define OMAP1510_GPIO_INT_MASK 0x10
37#define OMAP1510_GPIO_INT_STATUS 0x14
38#define OMAP1510_GPIO_PIN_CONTROL 0x18
39
40#define OMAP1510_IH_GPIO_BASE 64
41
42/*
43 * OMAP1610 specific GPIO registers
44 */
Russell King7c7095a2008-09-05 15:49:14 +010045#define OMAP1610_GPIO1_BASE IO_ADDRESS(0xfffbe400)
46#define OMAP1610_GPIO2_BASE IO_ADDRESS(0xfffbec00)
47#define OMAP1610_GPIO3_BASE IO_ADDRESS(0xfffbb400)
48#define OMAP1610_GPIO4_BASE IO_ADDRESS(0xfffbbc00)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010049#define OMAP1610_GPIO_REVISION 0x0000
50#define OMAP1610_GPIO_SYSCONFIG 0x0010
51#define OMAP1610_GPIO_SYSSTATUS 0x0014
52#define OMAP1610_GPIO_IRQSTATUS1 0x0018
53#define OMAP1610_GPIO_IRQENABLE1 0x001c
Tony Lindgren92105bb2005-09-07 17:20:26 +010054#define OMAP1610_GPIO_WAKEUPENABLE 0x0028
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010055#define OMAP1610_GPIO_DATAIN 0x002c
56#define OMAP1610_GPIO_DATAOUT 0x0030
57#define OMAP1610_GPIO_DIRECTION 0x0034
58#define OMAP1610_GPIO_EDGE_CTRL1 0x0038
59#define OMAP1610_GPIO_EDGE_CTRL2 0x003c
60#define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
Tony Lindgren92105bb2005-09-07 17:20:26 +010061#define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010062#define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
63#define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
Tony Lindgren92105bb2005-09-07 17:20:26 +010064#define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010065#define OMAP1610_GPIO_SET_DATAOUT 0x00f0
66
67/*
68 * OMAP730 specific GPIO registers
69 */
Russell King7c7095a2008-09-05 15:49:14 +010070#define OMAP730_GPIO1_BASE IO_ADDRESS(0xfffbc000)
71#define OMAP730_GPIO2_BASE IO_ADDRESS(0xfffbc800)
72#define OMAP730_GPIO3_BASE IO_ADDRESS(0xfffbd000)
73#define OMAP730_GPIO4_BASE IO_ADDRESS(0xfffbd800)
74#define OMAP730_GPIO5_BASE IO_ADDRESS(0xfffbe000)
75#define OMAP730_GPIO6_BASE IO_ADDRESS(0xfffbe800)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010076#define OMAP730_GPIO_DATA_INPUT 0x00
77#define OMAP730_GPIO_DATA_OUTPUT 0x04
78#define OMAP730_GPIO_DIR_CONTROL 0x08
79#define OMAP730_GPIO_INT_CONTROL 0x0c
80#define OMAP730_GPIO_INT_MASK 0x10
81#define OMAP730_GPIO_INT_STATUS 0x14
82
Tony Lindgren92105bb2005-09-07 17:20:26 +010083/*
84 * omap24xx specific GPIO registers
85 */
Russell King7c7095a2008-09-05 15:49:14 +010086#define OMAP242X_GPIO1_BASE IO_ADDRESS(0x48018000)
87#define OMAP242X_GPIO2_BASE IO_ADDRESS(0x4801a000)
88#define OMAP242X_GPIO3_BASE IO_ADDRESS(0x4801c000)
89#define OMAP242X_GPIO4_BASE IO_ADDRESS(0x4801e000)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -080090
Russell King7c7095a2008-09-05 15:49:14 +010091#define OMAP243X_GPIO1_BASE IO_ADDRESS(0x4900C000)
92#define OMAP243X_GPIO2_BASE IO_ADDRESS(0x4900E000)
93#define OMAP243X_GPIO3_BASE IO_ADDRESS(0x49010000)
94#define OMAP243X_GPIO4_BASE IO_ADDRESS(0x49012000)
95#define OMAP243X_GPIO5_BASE IO_ADDRESS(0x480B6000)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -080096
Tony Lindgren92105bb2005-09-07 17:20:26 +010097#define OMAP24XX_GPIO_REVISION 0x0000
98#define OMAP24XX_GPIO_SYSCONFIG 0x0010
99#define OMAP24XX_GPIO_SYSSTATUS 0x0014
100#define OMAP24XX_GPIO_IRQSTATUS1 0x0018
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300101#define OMAP24XX_GPIO_IRQSTATUS2 0x0028
102#define OMAP24XX_GPIO_IRQENABLE2 0x002c
Tony Lindgren92105bb2005-09-07 17:20:26 +0100103#define OMAP24XX_GPIO_IRQENABLE1 0x001c
Tero Kristo723fdb72008-11-26 14:35:16 -0800104#define OMAP24XX_GPIO_WAKE_EN 0x0020
Tony Lindgren92105bb2005-09-07 17:20:26 +0100105#define OMAP24XX_GPIO_CTRL 0x0030
106#define OMAP24XX_GPIO_OE 0x0034
107#define OMAP24XX_GPIO_DATAIN 0x0038
108#define OMAP24XX_GPIO_DATAOUT 0x003c
109#define OMAP24XX_GPIO_LEVELDETECT0 0x0040
110#define OMAP24XX_GPIO_LEVELDETECT1 0x0044
111#define OMAP24XX_GPIO_RISINGDETECT 0x0048
112#define OMAP24XX_GPIO_FALLINGDETECT 0x004c
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700113#define OMAP24XX_GPIO_DEBOUNCE_EN 0x0050
114#define OMAP24XX_GPIO_DEBOUNCE_VAL 0x0054
Tony Lindgren92105bb2005-09-07 17:20:26 +0100115#define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
116#define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
117#define OMAP24XX_GPIO_CLEARWKUENA 0x0080
118#define OMAP24XX_GPIO_SETWKUENA 0x0084
119#define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
120#define OMAP24XX_GPIO_SETDATAOUT 0x0094
121
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800122/*
123 * omap34xx specific GPIO registers
124 */
125
Russell King7c7095a2008-09-05 15:49:14 +0100126#define OMAP34XX_GPIO1_BASE IO_ADDRESS(0x48310000)
127#define OMAP34XX_GPIO2_BASE IO_ADDRESS(0x49050000)
128#define OMAP34XX_GPIO3_BASE IO_ADDRESS(0x49052000)
129#define OMAP34XX_GPIO4_BASE IO_ADDRESS(0x49054000)
130#define OMAP34XX_GPIO5_BASE IO_ADDRESS(0x49056000)
131#define OMAP34XX_GPIO6_BASE IO_ADDRESS(0x49058000)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800132
Russell King7c7095a2008-09-05 15:49:14 +0100133#define OMAP_MPUIO_VBASE IO_ADDRESS(OMAP_MPUIO_BASE)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800134
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100135struct gpio_bank {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100136 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100137 u16 irq;
138 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100139 int method;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800140#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100141 u32 suspend_wakeup;
142 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800143#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800144#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800145 u32 non_wakeup_gpios;
146 u32 enabled_non_wakeup_gpios;
147
148 u32 saved_datain;
149 u32 saved_fallingdetect;
150 u32 saved_risingdetect;
151#endif
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800152 u32 level_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -0800154 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -0800155 struct clk *dbck;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100156};
157
158#define METHOD_MPUIO 0
159#define METHOD_GPIO_1510 1
160#define METHOD_GPIO_1610 2
161#define METHOD_GPIO_730 3
Tony Lindgren92105bb2005-09-07 17:20:26 +0100162#define METHOD_GPIO_24XX 4
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100163
Tony Lindgren92105bb2005-09-07 17:20:26 +0100164#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100165static struct gpio_bank gpio_bank_1610[5] = {
Russell King7c7095a2008-09-05 15:49:14 +0100166 { OMAP_MPUIO_VBASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100167 { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
168 { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
169 { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
170 { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
171};
172#endif
173
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000174#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100175static struct gpio_bank gpio_bank_1510[2] = {
Russell King7c7095a2008-09-05 15:49:14 +0100176 { OMAP_MPUIO_VBASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100177 { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
178};
179#endif
180
181#ifdef CONFIG_ARCH_OMAP730
182static struct gpio_bank gpio_bank_730[7] = {
Russell King7c7095a2008-09-05 15:49:14 +0100183 { OMAP_MPUIO_VBASE, INT_730_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100184 { OMAP730_GPIO1_BASE, INT_730_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_730 },
185 { OMAP730_GPIO2_BASE, INT_730_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_730 },
186 { OMAP730_GPIO3_BASE, INT_730_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_730 },
187 { OMAP730_GPIO4_BASE, INT_730_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_730 },
188 { OMAP730_GPIO5_BASE, INT_730_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_730 },
189 { OMAP730_GPIO6_BASE, INT_730_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_730 },
190};
191#endif
192
Tony Lindgren92105bb2005-09-07 17:20:26 +0100193#ifdef CONFIG_ARCH_OMAP24XX
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800194
195static struct gpio_bank gpio_bank_242x[4] = {
196 { OMAP242X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
197 { OMAP242X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
198 { OMAP242X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
199 { OMAP242X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
Tony Lindgren92105bb2005-09-07 17:20:26 +0100200};
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800201
202static struct gpio_bank gpio_bank_243x[5] = {
203 { OMAP243X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
204 { OMAP243X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
205 { OMAP243X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
206 { OMAP243X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
207 { OMAP243X_GPIO5_BASE, INT_24XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
208};
209
Tony Lindgren92105bb2005-09-07 17:20:26 +0100210#endif
211
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800212#ifdef CONFIG_ARCH_OMAP34XX
213static struct gpio_bank gpio_bank_34xx[6] = {
214 { OMAP34XX_GPIO1_BASE, INT_34XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
215 { OMAP34XX_GPIO2_BASE, INT_34XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
216 { OMAP34XX_GPIO3_BASE, INT_34XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
217 { OMAP34XX_GPIO4_BASE, INT_34XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
218 { OMAP34XX_GPIO5_BASE, INT_34XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
219 { OMAP34XX_GPIO6_BASE, INT_34XX_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_24XX },
220};
221
222#endif
223
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100224static struct gpio_bank *gpio_bank;
225static int gpio_bank_count;
226
227static inline struct gpio_bank *get_gpio_bank(int gpio)
228{
Tony Lindgren6e60e792006-04-02 17:46:23 +0100229 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100230 if (OMAP_GPIO_IS_MPUIO(gpio))
231 return &gpio_bank[0];
232 return &gpio_bank[1];
233 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100234 if (cpu_is_omap16xx()) {
235 if (OMAP_GPIO_IS_MPUIO(gpio))
236 return &gpio_bank[0];
237 return &gpio_bank[1 + (gpio >> 4)];
238 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100239 if (cpu_is_omap730()) {
240 if (OMAP_GPIO_IS_MPUIO(gpio))
241 return &gpio_bank[0];
242 return &gpio_bank[1 + (gpio >> 5)];
243 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100244 if (cpu_is_omap24xx())
245 return &gpio_bank[gpio >> 5];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800246 if (cpu_is_omap34xx())
247 return &gpio_bank[gpio >> 5];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100248}
249
250static inline int get_gpio_index(int gpio)
251{
252 if (cpu_is_omap730())
253 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100254 if (cpu_is_omap24xx())
255 return gpio & 0x1f;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800256 if (cpu_is_omap34xx())
257 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100258 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100259}
260
261static inline int gpio_valid(int gpio)
262{
263 if (gpio < 0)
264 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -0800265 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300266 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100267 return -1;
268 return 0;
269 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100270 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100271 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100272 if ((cpu_is_omap16xx()) && gpio < 64)
273 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100274 if (cpu_is_omap730() && gpio < 192)
275 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100276 if (cpu_is_omap24xx() && gpio < 128)
277 return 0;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800278 if (cpu_is_omap34xx() && gpio < 160)
279 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100280 return -1;
281}
282
283static int check_gpio(int gpio)
284{
285 if (unlikely(gpio_valid(gpio)) < 0) {
286 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
287 dump_stack();
288 return -1;
289 }
290 return 0;
291}
292
293static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
294{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100295 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100296 u32 l;
297
298 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800299#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100300 case METHOD_MPUIO:
301 reg += OMAP_MPUIO_IO_CNTL;
302 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800303#endif
304#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100305 case METHOD_GPIO_1510:
306 reg += OMAP1510_GPIO_DIR_CONTROL;
307 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800308#endif
309#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100310 case METHOD_GPIO_1610:
311 reg += OMAP1610_GPIO_DIRECTION;
312 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800313#endif
314#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100315 case METHOD_GPIO_730:
316 reg += OMAP730_GPIO_DIR_CONTROL;
317 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800318#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800319#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100320 case METHOD_GPIO_24XX:
321 reg += OMAP24XX_GPIO_OE;
322 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800323#endif
324 default:
325 WARN_ON(1);
326 return;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100327 }
328 l = __raw_readl(reg);
329 if (is_input)
330 l |= 1 << gpio;
331 else
332 l &= ~(1 << gpio);
333 __raw_writel(l, reg);
334}
335
336void omap_set_gpio_direction(int gpio, int is_input)
337{
338 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800339 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340
341 if (check_gpio(gpio) < 0)
342 return;
343 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800344 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100345 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
David Brownella6472532008-03-03 04:33:30 -0800346 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100347}
348
349static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
350{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100351 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100352 u32 l = 0;
353
354 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800355#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100356 case METHOD_MPUIO:
357 reg += OMAP_MPUIO_OUTPUT;
358 l = __raw_readl(reg);
359 if (enable)
360 l |= 1 << gpio;
361 else
362 l &= ~(1 << gpio);
363 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800364#endif
365#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100366 case METHOD_GPIO_1510:
367 reg += OMAP1510_GPIO_DATA_OUTPUT;
368 l = __raw_readl(reg);
369 if (enable)
370 l |= 1 << gpio;
371 else
372 l &= ~(1 << gpio);
373 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800374#endif
375#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100376 case METHOD_GPIO_1610:
377 if (enable)
378 reg += OMAP1610_GPIO_SET_DATAOUT;
379 else
380 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
381 l = 1 << gpio;
382 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800383#endif
384#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100385 case METHOD_GPIO_730:
386 reg += OMAP730_GPIO_DATA_OUTPUT;
387 l = __raw_readl(reg);
388 if (enable)
389 l |= 1 << gpio;
390 else
391 l &= ~(1 << gpio);
392 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800393#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800394#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100395 case METHOD_GPIO_24XX:
396 if (enable)
397 reg += OMAP24XX_GPIO_SETDATAOUT;
398 else
399 reg += OMAP24XX_GPIO_CLEARDATAOUT;
400 l = 1 << gpio;
401 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800402#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100403 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800404 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100405 return;
406 }
407 __raw_writel(l, reg);
408}
409
410void omap_set_gpio_dataout(int gpio, int enable)
411{
412 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800413 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100414
415 if (check_gpio(gpio) < 0)
416 return;
417 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800418 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100419 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
David Brownella6472532008-03-03 04:33:30 -0800420 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100421}
422
423int omap_get_gpio_datain(int gpio)
424{
425 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100426 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100427
428 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800429 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100430 bank = get_gpio_bank(gpio);
431 reg = bank->base;
432 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800433#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100434 case METHOD_MPUIO:
435 reg += OMAP_MPUIO_INPUT_LATCH;
436 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800437#endif
438#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100439 case METHOD_GPIO_1510:
440 reg += OMAP1510_GPIO_DATA_INPUT;
441 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800442#endif
443#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100444 case METHOD_GPIO_1610:
445 reg += OMAP1610_GPIO_DATAIN;
446 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800447#endif
448#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100449 case METHOD_GPIO_730:
450 reg += OMAP730_GPIO_DATA_INPUT;
451 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800452#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800453#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100454 case METHOD_GPIO_24XX:
455 reg += OMAP24XX_GPIO_DATAIN;
456 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800457#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100458 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800459 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100460 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100461 return (__raw_readl(reg)
462 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100463}
464
Tony Lindgren92105bb2005-09-07 17:20:26 +0100465#define MOD_REG_BIT(reg, bit_mask, set) \
466do { \
467 int l = __raw_readl(base + reg); \
468 if (set) l |= bit_mask; \
469 else l &= ~bit_mask; \
470 __raw_writel(l, base + reg); \
471} while(0)
472
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700473void omap_set_gpio_debounce(int gpio, int enable)
474{
475 struct gpio_bank *bank;
476 void __iomem *reg;
477 u32 val, l = 1 << get_gpio_index(gpio);
478
479 if (cpu_class_is_omap1())
480 return;
481
482 bank = get_gpio_bank(gpio);
483 reg = bank->base;
484
485 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
486 val = __raw_readl(reg);
487
Jouni Hogander89db9482008-12-10 17:35:24 -0800488 if (enable && !(val & l))
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700489 val |= l;
Jouni Hogander89db9482008-12-10 17:35:24 -0800490 else if (!enable && val & l)
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700491 val &= ~l;
Jouni Hogander89db9482008-12-10 17:35:24 -0800492 else
493 return;
494
495 if (cpu_is_omap34xx())
496 enable ? clk_enable(bank->dbck) : clk_disable(bank->dbck);
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700497
498 __raw_writel(val, reg);
499}
500EXPORT_SYMBOL(omap_set_gpio_debounce);
501
502void omap_set_gpio_debounce_time(int gpio, int enc_time)
503{
504 struct gpio_bank *bank;
505 void __iomem *reg;
506
507 if (cpu_class_is_omap1())
508 return;
509
510 bank = get_gpio_bank(gpio);
511 reg = bank->base;
512
513 enc_time &= 0xff;
514 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
515 __raw_writel(enc_time, reg);
516}
517EXPORT_SYMBOL(omap_set_gpio_debounce_time);
518
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800519#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700520static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
521 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100522{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800523 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100524 u32 gpio_bit = 1 << gpio;
525
526 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100527 trigger & IRQ_TYPE_LEVEL_LOW);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100528 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100529 trigger & IRQ_TYPE_LEVEL_HIGH);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100530 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100531 trigger & IRQ_TYPE_EDGE_RISING);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100532 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100533 trigger & IRQ_TYPE_EDGE_FALLING);
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700534
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800535 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
536 if (trigger != 0)
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700537 __raw_writel(1 << gpio, bank->base
538 + OMAP24XX_GPIO_SETWKUENA);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800539 else
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700540 __raw_writel(1 << gpio, bank->base
541 + OMAP24XX_GPIO_CLEARWKUENA);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800542 } else {
543 if (trigger != 0)
544 bank->enabled_non_wakeup_gpios |= gpio_bit;
545 else
546 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
547 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700548
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800549 bank->level_mask =
550 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
551 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100552}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800553#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100554
555static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
556{
557 void __iomem *reg = bank->base;
558 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100559
560 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800561#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100562 case METHOD_MPUIO:
563 reg += OMAP_MPUIO_GPIO_INT_EDGE;
564 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100565 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100566 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100567 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100568 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100569 else
570 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100571 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800572#endif
573#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100574 case METHOD_GPIO_1510:
575 reg += OMAP1510_GPIO_INT_CONTROL;
576 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100577 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100579 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100580 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100581 else
582 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100583 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800584#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800585#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100586 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100587 if (gpio & 0x08)
588 reg += OMAP1610_GPIO_EDGE_CTRL2;
589 else
590 reg += OMAP1610_GPIO_EDGE_CTRL1;
591 gpio &= 0x07;
592 l = __raw_readl(reg);
593 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100594 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100595 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100596 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100597 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800598 if (trigger)
599 /* Enable wake-up during idle for dynamic tick */
600 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
601 else
602 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100603 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800604#endif
605#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100606 case METHOD_GPIO_730:
607 reg += OMAP730_GPIO_INT_CONTROL;
608 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100609 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100610 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100611 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100612 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100613 else
614 goto bad;
615 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800616#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800617#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100618 case METHOD_GPIO_24XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800619 set_24xx_gpio_triggering(bank, gpio, trigger);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100620 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800621#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100622 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100623 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100624 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100625 __raw_writel(l, reg);
626 return 0;
627bad:
628 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100629}
630
Tony Lindgren92105bb2005-09-07 17:20:26 +0100631static int gpio_irq_type(unsigned irq, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100632{
633 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100634 unsigned gpio;
635 int retval;
David Brownella6472532008-03-03 04:33:30 -0800636 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100637
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800638 if (!cpu_class_is_omap2() && irq > IH_MPUIO_BASE)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100639 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
640 else
641 gpio = irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100642
643 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100644 return -EINVAL;
645
David Brownelle5c56ed2006-12-06 17:13:59 -0800646 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100647 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800648
649 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800650 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800651 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100652 return -EINVAL;
653
David Brownell58781012006-12-06 17:14:10 -0800654 bank = get_irq_chip_data(irq);
David Brownella6472532008-03-03 04:33:30 -0800655 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100656 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
David Brownellb9772a22006-12-06 17:13:53 -0800657 if (retval == 0) {
658 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
659 irq_desc[irq].status |= type;
660 }
David Brownella6472532008-03-03 04:33:30 -0800661 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800662
663 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
664 __set_irq_handler_unlocked(irq, handle_level_irq);
665 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
666 __set_irq_handler_unlocked(irq, handle_edge_irq);
667
Tony Lindgren92105bb2005-09-07 17:20:26 +0100668 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100669}
670
671static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
672{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100673 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100674
675 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800676#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100677 case METHOD_MPUIO:
678 /* MPUIO irqstatus is reset by reading the status register,
679 * so do nothing here */
680 return;
David Brownelle5c56ed2006-12-06 17:13:59 -0800681#endif
682#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100683 case METHOD_GPIO_1510:
684 reg += OMAP1510_GPIO_INT_STATUS;
685 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800686#endif
687#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100688 case METHOD_GPIO_1610:
689 reg += OMAP1610_GPIO_IRQSTATUS1;
690 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800691#endif
692#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100693 case METHOD_GPIO_730:
694 reg += OMAP730_GPIO_INT_STATUS;
695 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800696#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800697#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100698 case METHOD_GPIO_24XX:
699 reg += OMAP24XX_GPIO_IRQSTATUS1;
700 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800701#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800703 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100704 return;
705 }
706 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300707
708 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800709#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
710 if (cpu_is_omap24xx() || cpu_is_omap34xx())
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300711 __raw_writel(gpio_mask, bank->base + OMAP24XX_GPIO_IRQSTATUS2);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800712#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100713}
714
715static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
716{
717 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
718}
719
Imre Deakea6dedd2006-06-26 16:16:00 -0700720static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
721{
722 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700723 int inv = 0;
724 u32 l;
725 u32 mask;
Imre Deakea6dedd2006-06-26 16:16:00 -0700726
727 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800728#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700729 case METHOD_MPUIO:
730 reg += OMAP_MPUIO_GPIO_MASKIT;
Imre Deak99c47702006-06-26 16:16:07 -0700731 mask = 0xffff;
732 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700733 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800734#endif
735#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700736 case METHOD_GPIO_1510:
737 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700738 mask = 0xffff;
739 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700740 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800741#endif
742#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700743 case METHOD_GPIO_1610:
744 reg += OMAP1610_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700745 mask = 0xffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700746 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800747#endif
748#ifdef CONFIG_ARCH_OMAP730
Imre Deakea6dedd2006-06-26 16:16:00 -0700749 case METHOD_GPIO_730:
750 reg += OMAP730_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700751 mask = 0xffffffff;
752 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700753 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800754#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800755#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Imre Deakea6dedd2006-06-26 16:16:00 -0700756 case METHOD_GPIO_24XX:
757 reg += OMAP24XX_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700758 mask = 0xffffffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700759 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800760#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700761 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800762 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700763 return 0;
764 }
765
Imre Deak99c47702006-06-26 16:16:07 -0700766 l = __raw_readl(reg);
767 if (inv)
768 l = ~l;
769 l &= mask;
770 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700771}
772
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100773static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
774{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100775 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100776 u32 l;
777
778 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800779#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100780 case METHOD_MPUIO:
781 reg += OMAP_MPUIO_GPIO_MASKIT;
782 l = __raw_readl(reg);
783 if (enable)
784 l &= ~(gpio_mask);
785 else
786 l |= gpio_mask;
787 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800788#endif
789#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100790 case METHOD_GPIO_1510:
791 reg += OMAP1510_GPIO_INT_MASK;
792 l = __raw_readl(reg);
793 if (enable)
794 l &= ~(gpio_mask);
795 else
796 l |= gpio_mask;
797 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800798#endif
799#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100800 case METHOD_GPIO_1610:
801 if (enable)
802 reg += OMAP1610_GPIO_SET_IRQENABLE1;
803 else
804 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
805 l = gpio_mask;
806 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800807#endif
808#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100809 case METHOD_GPIO_730:
810 reg += OMAP730_GPIO_INT_MASK;
811 l = __raw_readl(reg);
812 if (enable)
813 l &= ~(gpio_mask);
814 else
815 l |= gpio_mask;
816 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800817#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800818#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100819 case METHOD_GPIO_24XX:
820 if (enable)
821 reg += OMAP24XX_GPIO_SETIRQENABLE1;
822 else
823 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
824 l = gpio_mask;
825 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800826#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100827 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800828 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100829 return;
830 }
831 __raw_writel(l, reg);
832}
833
834static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
835{
836 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
837}
838
Tony Lindgren92105bb2005-09-07 17:20:26 +0100839/*
840 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
841 * 1510 does not seem to have a wake-up register. If JTAG is connected
842 * to the target, system will wake up always on GPIO events. While
843 * system is running all registered GPIO interrupts need to have wake-up
844 * enabled. When system is suspended, only selected GPIO interrupts need
845 * to have wake-up enabled.
846 */
847static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
848{
David Brownella6472532008-03-03 04:33:30 -0800849 unsigned long flags;
850
Tony Lindgren92105bb2005-09-07 17:20:26 +0100851 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800852#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800853 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100854 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800855 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800856 if (enable) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100857 bank->suspend_wakeup |= (1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800858 enable_irq_wake(bank->irq);
859 } else {
860 disable_irq_wake(bank->irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100861 bank->suspend_wakeup &= ~(1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800862 }
David Brownella6472532008-03-03 04:33:30 -0800863 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100864 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800865#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800866#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800867 case METHOD_GPIO_24XX:
David Brownell11a78b72006-12-06 17:14:11 -0800868 if (bank->non_wakeup_gpios & (1 << gpio)) {
869 printk(KERN_ERR "Unable to modify wakeup on "
870 "non-wakeup GPIO%d\n",
871 (bank - gpio_bank) * 32 + gpio);
872 return -EINVAL;
873 }
David Brownella6472532008-03-03 04:33:30 -0800874 spin_lock_irqsave(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800875 if (enable) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800876 bank->suspend_wakeup |= (1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800877 enable_irq_wake(bank->irq);
878 } else {
879 disable_irq_wake(bank->irq);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800880 bank->suspend_wakeup &= ~(1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800881 }
David Brownella6472532008-03-03 04:33:30 -0800882 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800883 return 0;
884#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100885 default:
886 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
887 bank->method);
888 return -EINVAL;
889 }
890}
891
Tony Lindgren4196dd62006-09-25 12:41:38 +0300892static void _reset_gpio(struct gpio_bank *bank, int gpio)
893{
894 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
895 _set_gpio_irqenable(bank, gpio, 0);
896 _clear_gpio_irqstatus(bank, gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100897 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300898}
899
Tony Lindgren92105bb2005-09-07 17:20:26 +0100900/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
901static int gpio_wake_enable(unsigned int irq, unsigned int enable)
902{
903 unsigned int gpio = irq - IH_GPIO_BASE;
904 struct gpio_bank *bank;
905 int retval;
906
907 if (check_gpio(gpio) < 0)
908 return -ENODEV;
David Brownell58781012006-12-06 17:14:10 -0800909 bank = get_irq_chip_data(irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100910 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100911
912 return retval;
913}
914
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100915int omap_request_gpio(int gpio)
916{
917 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800918 unsigned long flags;
David Brownell52e31342008-03-03 12:43:23 -0800919 int status;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100920
921 if (check_gpio(gpio) < 0)
922 return -EINVAL;
923
David Brownell52e31342008-03-03 12:43:23 -0800924 status = gpio_request(gpio, NULL);
925 if (status < 0)
926 return status;
927
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100928 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800929 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100930
Tony Lindgren4196dd62006-09-25 12:41:38 +0300931 /* Set trigger to none. You need to enable the desired trigger with
932 * request_irq() or set_irq_type().
933 */
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100934 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100935
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000936#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100937 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100938 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100939
Tony Lindgren92105bb2005-09-07 17:20:26 +0100940 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100941 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
942 __raw_writel(__raw_readl(reg) | (1 << get_gpio_index(gpio)), reg);
943 }
944#endif
David Brownella6472532008-03-03 04:33:30 -0800945 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100946
947 return 0;
948}
949
950void omap_free_gpio(int gpio)
951{
952 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800953 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100954
955 if (check_gpio(gpio) < 0)
956 return;
957 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800958 spin_lock_irqsave(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800959 if (unlikely(!gpiochip_is_requested(&bank->chip,
960 get_gpio_index(gpio)))) {
961 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100962 printk(KERN_ERR "omap-gpio: GPIO %d wasn't reserved!\n", gpio);
963 dump_stack();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100964 return;
965 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100966#ifdef CONFIG_ARCH_OMAP16XX
967 if (bank->method == METHOD_GPIO_1610) {
968 /* Disable wake-up during idle for dynamic tick */
969 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
970 __raw_writel(1 << get_gpio_index(gpio), reg);
971 }
972#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800973#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100974 if (bank->method == METHOD_GPIO_24XX) {
975 /* Disable wake-up during idle for dynamic tick */
976 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
977 __raw_writel(1 << get_gpio_index(gpio), reg);
978 }
979#endif
Tony Lindgren4196dd62006-09-25 12:41:38 +0300980 _reset_gpio(bank, gpio);
David Brownella6472532008-03-03 04:33:30 -0800981 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800982 gpio_free(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100983}
984
985/*
986 * We need to unmask the GPIO bank interrupt as soon as possible to
987 * avoid missing GPIO interrupts for other lines in the bank.
988 * Then we need to mask-read-clear-unmask the triggered GPIO lines
989 * in the bank to avoid missing nested interrupts for a GPIO line.
990 * If we wait to unmask individual GPIO lines in the bank after the
991 * line's interrupt handler has been run, we may miss some nested
992 * interrupts.
993 */
Russell King10dd5ce2006-11-23 11:41:32 +0000994static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100995{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100996 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100997 u32 isr;
998 unsigned int gpio_irq;
999 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -07001000 u32 retrigger = 0;
1001 int unmasked = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001002
1003 desc->chip->ack(irq);
1004
Thomas Gleixner418ca1f2006-07-01 22:32:41 +01001005 bank = get_irq_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -08001006#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001007 if (bank->method == METHOD_MPUIO)
1008 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
David Brownelle5c56ed2006-12-06 17:13:59 -08001009#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001010#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001011 if (bank->method == METHOD_GPIO_1510)
1012 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
1013#endif
1014#if defined(CONFIG_ARCH_OMAP16XX)
1015 if (bank->method == METHOD_GPIO_1610)
1016 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
1017#endif
1018#ifdef CONFIG_ARCH_OMAP730
1019 if (bank->method == METHOD_GPIO_730)
1020 isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
1021#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001022#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001023 if (bank->method == METHOD_GPIO_24XX)
1024 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1025#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001026 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001027 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -07001028 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001029
Imre Deakea6dedd2006-06-26 16:16:00 -07001030 enabled = _get_gpio_irqbank_mask(bank);
1031 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001032
1033 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1034 isr &= 0x0000ffff;
1035
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001036 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001037 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -07001038 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001039
1040 /* clear edge sensitive interrupts before handler(s) are
1041 called so that we don't miss any interrupt occurred while
1042 executing them */
1043 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1044 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1045 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1046
1047 /* if there is only edge sensitive GPIO pin interrupts
1048 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -07001049 if (!level_mask && !unmasked) {
1050 unmasked = 1;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001051 desc->chip->unmask(irq);
Imre Deakea6dedd2006-06-26 16:16:00 -07001052 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001053
Imre Deakea6dedd2006-06-26 16:16:00 -07001054 isr |= retrigger;
1055 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001056 if (!isr)
1057 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001058
Tony Lindgren92105bb2005-09-07 17:20:26 +01001059 gpio_irq = bank->virtual_irq_start;
1060 for (; isr != 0; isr >>= 1, gpio_irq++) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001061 if (!(isr & 1))
1062 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001063
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +01001064 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001065 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001066 }
Imre Deakea6dedd2006-06-26 16:16:00 -07001067 /* if bank has any level sensitive GPIO pin interrupt
1068 configured, we must unmask the bank interrupt only after
1069 handler(s) are executed in order to avoid spurious bank
1070 interrupt */
1071 if (!unmasked)
1072 desc->chip->unmask(irq);
1073
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001074}
1075
Tony Lindgren4196dd62006-09-25 12:41:38 +03001076static void gpio_irq_shutdown(unsigned int irq)
1077{
1078 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001079 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001080
1081 _reset_gpio(bank, gpio);
1082}
1083
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001084static void gpio_ack_irq(unsigned int irq)
1085{
1086 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001087 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001088
1089 _clear_gpio_irqstatus(bank, gpio);
1090}
1091
1092static void gpio_mask_irq(unsigned int irq)
1093{
1094 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001095 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001096
1097 _set_gpio_irqenable(bank, gpio, 0);
1098}
1099
1100static void gpio_unmask_irq(unsigned int irq)
1101{
1102 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001103 struct gpio_bank *bank = get_irq_chip_data(irq);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001104 unsigned int irq_mask = 1 << get_gpio_index(gpio);
1105
1106 /* For level-triggered GPIOs, the clearing must be done after
1107 * the HW source is cleared, thus after the handler has run */
1108 if (bank->level_mask & irq_mask) {
1109 _set_gpio_irqenable(bank, gpio, 0);
1110 _clear_gpio_irqstatus(bank, gpio);
1111 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001112
Kevin Hilman4de8c752008-01-16 21:56:14 -08001113 _set_gpio_irqenable(bank, gpio, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001114}
1115
David Brownelle5c56ed2006-12-06 17:13:59 -08001116static struct irq_chip gpio_irq_chip = {
1117 .name = "GPIO",
1118 .shutdown = gpio_irq_shutdown,
1119 .ack = gpio_ack_irq,
1120 .mask = gpio_mask_irq,
1121 .unmask = gpio_unmask_irq,
1122 .set_type = gpio_irq_type,
1123 .set_wake = gpio_wake_enable,
1124};
1125
1126/*---------------------------------------------------------------------*/
1127
1128#ifdef CONFIG_ARCH_OMAP1
1129
1130/* MPUIO uses the always-on 32k clock */
1131
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001132static void mpuio_ack_irq(unsigned int irq)
1133{
1134 /* The ISR is reset automatically, so do nothing here. */
1135}
1136
1137static void mpuio_mask_irq(unsigned int irq)
1138{
1139 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001140 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001141
1142 _set_gpio_irqenable(bank, gpio, 0);
1143}
1144
1145static void mpuio_unmask_irq(unsigned int irq)
1146{
1147 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001148 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001149
1150 _set_gpio_irqenable(bank, gpio, 1);
1151}
1152
David Brownelle5c56ed2006-12-06 17:13:59 -08001153static struct irq_chip mpuio_irq_chip = {
1154 .name = "MPUIO",
1155 .ack = mpuio_ack_irq,
1156 .mask = mpuio_mask_irq,
1157 .unmask = mpuio_unmask_irq,
Tony Lindgren92105bb2005-09-07 17:20:26 +01001158 .set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001159#ifdef CONFIG_ARCH_OMAP16XX
1160 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1161 .set_wake = gpio_wake_enable,
1162#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001163};
1164
David Brownelle5c56ed2006-12-06 17:13:59 -08001165
1166#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1167
David Brownell11a78b72006-12-06 17:14:11 -08001168
1169#ifdef CONFIG_ARCH_OMAP16XX
1170
1171#include <linux/platform_device.h>
1172
1173static int omap_mpuio_suspend_late(struct platform_device *pdev, pm_message_t mesg)
1174{
1175 struct gpio_bank *bank = platform_get_drvdata(pdev);
1176 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001177 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001178
David Brownella6472532008-03-03 04:33:30 -08001179 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001180 bank->saved_wakeup = __raw_readl(mask_reg);
1181 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001182 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001183
1184 return 0;
1185}
1186
1187static int omap_mpuio_resume_early(struct platform_device *pdev)
1188{
1189 struct gpio_bank *bank = platform_get_drvdata(pdev);
1190 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001191 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001192
David Brownella6472532008-03-03 04:33:30 -08001193 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001194 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001195 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001196
1197 return 0;
1198}
1199
1200/* use platform_driver for this, now that there's no longer any
1201 * point to sys_device (other than not disturbing old code).
1202 */
1203static struct platform_driver omap_mpuio_driver = {
1204 .suspend_late = omap_mpuio_suspend_late,
1205 .resume_early = omap_mpuio_resume_early,
1206 .driver = {
1207 .name = "mpuio",
1208 },
1209};
1210
1211static struct platform_device omap_mpuio_device = {
1212 .name = "mpuio",
1213 .id = -1,
1214 .dev = {
1215 .driver = &omap_mpuio_driver.driver,
1216 }
1217 /* could list the /proc/iomem resources */
1218};
1219
1220static inline void mpuio_init(void)
1221{
David Brownellfcf126d2007-04-02 12:46:47 -07001222 platform_set_drvdata(&omap_mpuio_device, &gpio_bank_1610[0]);
1223
David Brownell11a78b72006-12-06 17:14:11 -08001224 if (platform_driver_register(&omap_mpuio_driver) == 0)
1225 (void) platform_device_register(&omap_mpuio_device);
1226}
1227
1228#else
1229static inline void mpuio_init(void) {}
1230#endif /* 16xx */
1231
David Brownelle5c56ed2006-12-06 17:13:59 -08001232#else
1233
1234extern struct irq_chip mpuio_irq_chip;
1235
1236#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001237static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001238
1239#endif
1240
1241/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001242
David Brownell52e31342008-03-03 12:43:23 -08001243/* REVISIT these are stupid implementations! replace by ones that
1244 * don't switch on METHOD_* and which mostly avoid spinlocks
1245 */
1246
1247static int gpio_input(struct gpio_chip *chip, unsigned offset)
1248{
1249 struct gpio_bank *bank;
1250 unsigned long flags;
1251
1252 bank = container_of(chip, struct gpio_bank, chip);
1253 spin_lock_irqsave(&bank->lock, flags);
1254 _set_gpio_direction(bank, offset, 1);
1255 spin_unlock_irqrestore(&bank->lock, flags);
1256 return 0;
1257}
1258
1259static int gpio_get(struct gpio_chip *chip, unsigned offset)
1260{
1261 return omap_get_gpio_datain(chip->base + offset);
1262}
1263
1264static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1265{
1266 struct gpio_bank *bank;
1267 unsigned long flags;
1268
1269 bank = container_of(chip, struct gpio_bank, chip);
1270 spin_lock_irqsave(&bank->lock, flags);
1271 _set_gpio_dataout(bank, offset, value);
1272 _set_gpio_direction(bank, offset, 0);
1273 spin_unlock_irqrestore(&bank->lock, flags);
1274 return 0;
1275}
1276
1277static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1278{
1279 struct gpio_bank *bank;
1280 unsigned long flags;
1281
1282 bank = container_of(chip, struct gpio_bank, chip);
1283 spin_lock_irqsave(&bank->lock, flags);
1284 _set_gpio_dataout(bank, offset, value);
1285 spin_unlock_irqrestore(&bank->lock, flags);
1286}
1287
1288/*---------------------------------------------------------------------*/
1289
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001290static int initialized;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001291#if !defined(CONFIG_ARCH_OMAP3)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001292static struct clk * gpio_ick;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001293#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001294
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001295#if defined(CONFIG_ARCH_OMAP2)
1296static struct clk * gpio_fck;
1297#endif
1298
1299#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001300static struct clk * gpio5_ick;
1301static struct clk * gpio5_fck;
1302#endif
1303
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001304#if defined(CONFIG_ARCH_OMAP3)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001305static struct clk *gpio_iclks[OMAP34XX_NR_GPIOS];
1306#endif
1307
David Brownell8ba55c52008-02-26 11:10:50 -08001308/* This lock class tells lockdep that GPIO irqs are in a different
1309 * category than their parents, so it won't report false recursion.
1310 */
1311static struct lock_class_key gpio_lock_class;
1312
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001313static int __init _omap_gpio_init(void)
1314{
1315 int i;
David Brownell52e31342008-03-03 12:43:23 -08001316 int gpio = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001317 struct gpio_bank *bank;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001318 char clk_name[11];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001319
1320 initialized = 1;
1321
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001322#if defined(CONFIG_ARCH_OMAP1)
Tony Lindgren6e60e792006-04-02 17:46:23 +01001323 if (cpu_is_omap15xx()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001324 gpio_ick = clk_get(NULL, "arm_gpio_ck");
1325 if (IS_ERR(gpio_ick))
Tony Lindgren92105bb2005-09-07 17:20:26 +01001326 printk("Could not get arm_gpio_ck\n");
1327 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001328 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001329 }
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001330#endif
1331#if defined(CONFIG_ARCH_OMAP2)
1332 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001333 gpio_ick = clk_get(NULL, "gpios_ick");
1334 if (IS_ERR(gpio_ick))
1335 printk("Could not get gpios_ick\n");
1336 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001337 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001338 gpio_fck = clk_get(NULL, "gpios_fck");
Komal Shah1630b522006-09-25 12:51:08 +03001339 if (IS_ERR(gpio_fck))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001340 printk("Could not get gpios_fck\n");
1341 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001342 clk_enable(gpio_fck);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001343
1344 /*
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001345 * On 2430 & 3430 GPIO 5 uses CORE L4 ICLK
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001346 */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001347#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001348 if (cpu_is_omap2430()) {
1349 gpio5_ick = clk_get(NULL, "gpio5_ick");
1350 if (IS_ERR(gpio5_ick))
1351 printk("Could not get gpio5_ick\n");
1352 else
1353 clk_enable(gpio5_ick);
1354 gpio5_fck = clk_get(NULL, "gpio5_fck");
1355 if (IS_ERR(gpio5_fck))
1356 printk("Could not get gpio5_fck\n");
1357 else
1358 clk_enable(gpio5_fck);
1359 }
1360#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001361 }
1362#endif
1363
1364#if defined(CONFIG_ARCH_OMAP3)
1365 if (cpu_is_omap34xx()) {
1366 for (i = 0; i < OMAP34XX_NR_GPIOS; i++) {
1367 sprintf(clk_name, "gpio%d_ick", i + 1);
1368 gpio_iclks[i] = clk_get(NULL, clk_name);
1369 if (IS_ERR(gpio_iclks[i]))
1370 printk(KERN_ERR "Could not get %s\n", clk_name);
1371 else
1372 clk_enable(gpio_iclks[i]);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001373 }
1374 }
1375#endif
1376
Tony Lindgren92105bb2005-09-07 17:20:26 +01001377
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001378#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +01001379 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001380 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
1381 gpio_bank_count = 2;
1382 gpio_bank = gpio_bank_1510;
1383 }
1384#endif
1385#if defined(CONFIG_ARCH_OMAP16XX)
1386 if (cpu_is_omap16xx()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001387 u32 rev;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001388
1389 gpio_bank_count = 5;
1390 gpio_bank = gpio_bank_1610;
Russell King7c7095a2008-09-05 15:49:14 +01001391 rev = __raw_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001392 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1393 (rev >> 4) & 0x0f, rev & 0x0f);
1394 }
1395#endif
1396#ifdef CONFIG_ARCH_OMAP730
1397 if (cpu_is_omap730()) {
1398 printk(KERN_INFO "OMAP730 GPIO hardware\n");
1399 gpio_bank_count = 7;
1400 gpio_bank = gpio_bank_730;
1401 }
1402#endif
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001403
Tony Lindgren92105bb2005-09-07 17:20:26 +01001404#ifdef CONFIG_ARCH_OMAP24XX
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001405 if (cpu_is_omap242x()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001406 int rev;
1407
1408 gpio_bank_count = 4;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001409 gpio_bank = gpio_bank_242x;
Russell King7c7095a2008-09-05 15:49:14 +01001410 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001411 printk(KERN_INFO "OMAP242x GPIO hardware version %d.%d\n",
1412 (rev >> 4) & 0x0f, rev & 0x0f);
1413 }
1414 if (cpu_is_omap243x()) {
1415 int rev;
1416
1417 gpio_bank_count = 5;
1418 gpio_bank = gpio_bank_243x;
Russell King7c7095a2008-09-05 15:49:14 +01001419 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001420 printk(KERN_INFO "OMAP243x GPIO hardware version %d.%d\n",
Tony Lindgren92105bb2005-09-07 17:20:26 +01001421 (rev >> 4) & 0x0f, rev & 0x0f);
1422 }
1423#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001424#ifdef CONFIG_ARCH_OMAP34XX
1425 if (cpu_is_omap34xx()) {
1426 int rev;
1427
1428 gpio_bank_count = OMAP34XX_NR_GPIOS;
1429 gpio_bank = gpio_bank_34xx;
Russell King7c7095a2008-09-05 15:49:14 +01001430 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001431 printk(KERN_INFO "OMAP34xx GPIO hardware version %d.%d\n",
1432 (rev >> 4) & 0x0f, rev & 0x0f);
1433 }
1434#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001435 for (i = 0; i < gpio_bank_count; i++) {
1436 int j, gpio_count = 16;
1437
1438 bank = &gpio_bank[i];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001439 spin_lock_init(&bank->lock);
David Brownelle5c56ed2006-12-06 17:13:59 -08001440 if (bank_is_mpuio(bank))
Russell King7c7095a2008-09-05 15:49:14 +01001441 __raw_writew(0xffff, bank->base + OMAP_MPUIO_GPIO_MASKIT);
Tony Lindgrend11ac972008-01-12 15:35:04 -08001442 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001443 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
1444 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
1445 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001446 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001447 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
1448 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001449 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001450 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001451 if (cpu_is_omap730() && bank->method == METHOD_GPIO_730) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001452 __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
1453 __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
1454
1455 gpio_count = 32; /* 730 has 32-bit GPIOs */
1456 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001457
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001458#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001459 if (bank->method == METHOD_GPIO_24XX) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001460 static const u32 non_wakeup_gpios[] = {
1461 0xe203ffc0, 0x08700040
1462 };
1463
Tony Lindgren92105bb2005-09-07 17:20:26 +01001464 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
1465 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001466 __raw_writew(0x0015, bank->base + OMAP24XX_GPIO_SYSCONFIG);
1467
1468 /* Initialize interface clock ungated, module enabled */
1469 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001470 if (i < ARRAY_SIZE(non_wakeup_gpios))
1471 bank->non_wakeup_gpios = non_wakeup_gpios[i];
Tony Lindgren92105bb2005-09-07 17:20:26 +01001472 gpio_count = 32;
1473 }
1474#endif
David Brownell52e31342008-03-03 12:43:23 -08001475
1476 /* REVISIT eventually switch from OMAP-specific gpio structs
1477 * over to the generic ones
1478 */
1479 bank->chip.direction_input = gpio_input;
1480 bank->chip.get = gpio_get;
1481 bank->chip.direction_output = gpio_output;
1482 bank->chip.set = gpio_set;
1483 if (bank_is_mpuio(bank)) {
1484 bank->chip.label = "mpuio";
Russell King69114a42008-09-03 10:15:26 +01001485#ifdef CONFIG_ARCH_OMAP16XX
David Brownelld8f388d2008-07-25 01:46:07 -07001486 bank->chip.dev = &omap_mpuio_device.dev;
1487#endif
David Brownell52e31342008-03-03 12:43:23 -08001488 bank->chip.base = OMAP_MPUIO(0);
1489 } else {
1490 bank->chip.label = "gpio";
1491 bank->chip.base = gpio;
1492 gpio += gpio_count;
1493 }
1494 bank->chip.ngpio = gpio_count;
1495
1496 gpiochip_add(&bank->chip);
1497
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001498 for (j = bank->virtual_irq_start;
1499 j < bank->virtual_irq_start + gpio_count; j++) {
David Brownell8ba55c52008-02-26 11:10:50 -08001500 lockdep_set_class(&irq_desc[j].lock, &gpio_lock_class);
David Brownell58781012006-12-06 17:14:10 -08001501 set_irq_chip_data(j, bank);
David Brownelle5c56ed2006-12-06 17:13:59 -08001502 if (bank_is_mpuio(bank))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001503 set_irq_chip(j, &mpuio_irq_chip);
1504 else
1505 set_irq_chip(j, &gpio_irq_chip);
Russell King10dd5ce2006-11-23 11:41:32 +00001506 set_irq_handler(j, handle_simple_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001507 set_irq_flags(j, IRQF_VALID);
1508 }
1509 set_irq_chained_handler(bank->irq, gpio_irq_handler);
1510 set_irq_data(bank->irq, bank);
Jouni Hogander89db9482008-12-10 17:35:24 -08001511
1512 if (cpu_is_omap34xx()) {
1513 sprintf(clk_name, "gpio%d_dbck", i + 1);
1514 bank->dbck = clk_get(NULL, clk_name);
1515 if (IS_ERR(bank->dbck))
1516 printk(KERN_ERR "Could not get %s\n", clk_name);
1517 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001518 }
1519
1520 /* Enable system clock for GPIO module.
1521 * The CAM_CLK_CTRL *is* really the right place. */
Tony Lindgren92105bb2005-09-07 17:20:26 +01001522 if (cpu_is_omap16xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001523 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
1524
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001525 /* Enable autoidle for the OCP interface */
1526 if (cpu_is_omap24xx())
1527 omap_writel(1 << 0, 0x48019010);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001528 if (cpu_is_omap34xx())
1529 omap_writel(1 << 0, 0x48306814);
Tony Lindgrend11ac972008-01-12 15:35:04 -08001530
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001531 return 0;
1532}
1533
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001534#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001535static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
1536{
1537 int i;
1538
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001539 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001540 return 0;
1541
1542 for (i = 0; i < gpio_bank_count; i++) {
1543 struct gpio_bank *bank = &gpio_bank[i];
1544 void __iomem *wake_status;
1545 void __iomem *wake_clear;
1546 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001547 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001548
1549 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001550#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001551 case METHOD_GPIO_1610:
1552 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1553 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1554 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1555 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001556#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001557#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001558 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001559 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001560 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1561 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1562 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001563#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001564 default:
1565 continue;
1566 }
1567
David Brownella6472532008-03-03 04:33:30 -08001568 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001569 bank->saved_wakeup = __raw_readl(wake_status);
1570 __raw_writel(0xffffffff, wake_clear);
1571 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001572 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001573 }
1574
1575 return 0;
1576}
1577
1578static int omap_gpio_resume(struct sys_device *dev)
1579{
1580 int i;
1581
Tero Kristo723fdb72008-11-26 14:35:16 -08001582 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001583 return 0;
1584
1585 for (i = 0; i < gpio_bank_count; i++) {
1586 struct gpio_bank *bank = &gpio_bank[i];
1587 void __iomem *wake_clear;
1588 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001589 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001590
1591 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001592#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001593 case METHOD_GPIO_1610:
1594 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1595 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1596 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001597#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001598#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001599 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001600 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1601 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001602 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001603#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001604 default:
1605 continue;
1606 }
1607
David Brownella6472532008-03-03 04:33:30 -08001608 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001609 __raw_writel(0xffffffff, wake_clear);
1610 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001611 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001612 }
1613
1614 return 0;
1615}
1616
1617static struct sysdev_class omap_gpio_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001618 .name = "gpio",
Tony Lindgren92105bb2005-09-07 17:20:26 +01001619 .suspend = omap_gpio_suspend,
1620 .resume = omap_gpio_resume,
1621};
1622
1623static struct sys_device omap_gpio_device = {
1624 .id = 0,
1625 .cls = &omap_gpio_sysclass,
1626};
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001627
1628#endif
1629
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001630#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001631
1632static int workaround_enabled;
1633
1634void omap2_gpio_prepare_for_retention(void)
1635{
1636 int i, c = 0;
1637
1638 /* Remove triggering for all non-wakeup GPIOs. Otherwise spurious
1639 * IRQs will be generated. See OMAP2420 Errata item 1.101. */
1640 for (i = 0; i < gpio_bank_count; i++) {
1641 struct gpio_bank *bank = &gpio_bank[i];
1642 u32 l1, l2;
1643
1644 if (!(bank->enabled_non_wakeup_gpios))
1645 continue;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001646#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001647 bank->saved_datain = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1648 l1 = __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1649 l2 = __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001650#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001651 bank->saved_fallingdetect = l1;
1652 bank->saved_risingdetect = l2;
1653 l1 &= ~bank->enabled_non_wakeup_gpios;
1654 l2 &= ~bank->enabled_non_wakeup_gpios;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001655#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001656 __raw_writel(l1, bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1657 __raw_writel(l2, bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001658#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001659 c++;
1660 }
1661 if (!c) {
1662 workaround_enabled = 0;
1663 return;
1664 }
1665 workaround_enabled = 1;
1666}
1667
1668void omap2_gpio_resume_after_retention(void)
1669{
1670 int i;
1671
1672 if (!workaround_enabled)
1673 return;
1674 for (i = 0; i < gpio_bank_count; i++) {
1675 struct gpio_bank *bank = &gpio_bank[i];
1676 u32 l;
1677
1678 if (!(bank->enabled_non_wakeup_gpios))
1679 continue;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001680#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001681 __raw_writel(bank->saved_fallingdetect,
1682 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1683 __raw_writel(bank->saved_risingdetect,
1684 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001685#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001686 /* Check if any of the non-wakeup interrupt GPIOs have changed
1687 * state. If so, generate an IRQ by software. This is
1688 * horribly racy, but it's the best we can do to work around
1689 * this silicon bug. */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001690#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001691 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001692#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001693 l ^= bank->saved_datain;
1694 l &= bank->non_wakeup_gpios;
1695 if (l) {
1696 u32 old0, old1;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001697#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001698 old0 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1699 old1 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1700 __raw_writel(old0 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1701 __raw_writel(old1 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1702 __raw_writel(old0, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1703 __raw_writel(old1, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001704#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001705 }
1706 }
1707
1708}
1709
Tony Lindgren92105bb2005-09-07 17:20:26 +01001710#endif
1711
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001712/*
1713 * This may get called early from board specific init
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001714 * for boards that have interrupts routed via FPGA.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001715 */
David Brownell277d58e2006-12-06 17:13:59 -08001716int __init omap_gpio_init(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001717{
1718 if (!initialized)
1719 return _omap_gpio_init();
1720 else
1721 return 0;
1722}
1723
Tony Lindgren92105bb2005-09-07 17:20:26 +01001724static int __init omap_gpio_sysinit(void)
1725{
1726 int ret = 0;
1727
1728 if (!initialized)
1729 ret = _omap_gpio_init();
1730
David Brownell11a78b72006-12-06 17:14:11 -08001731 mpuio_init();
1732
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001733#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1734 if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001735 if (ret == 0) {
1736 ret = sysdev_class_register(&omap_gpio_sysclass);
1737 if (ret == 0)
1738 ret = sysdev_register(&omap_gpio_device);
1739 }
1740 }
1741#endif
1742
1743 return ret;
1744}
1745
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001746EXPORT_SYMBOL(omap_request_gpio);
1747EXPORT_SYMBOL(omap_free_gpio);
1748EXPORT_SYMBOL(omap_set_gpio_direction);
1749EXPORT_SYMBOL(omap_set_gpio_dataout);
1750EXPORT_SYMBOL(omap_get_gpio_datain);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001751
Tony Lindgren92105bb2005-09-07 17:20:26 +01001752arch_initcall(omap_gpio_sysinit);
David Brownellb9772a22006-12-06 17:13:53 -08001753
1754
1755#ifdef CONFIG_DEBUG_FS
1756
1757#include <linux/debugfs.h>
1758#include <linux/seq_file.h>
1759
1760static int gpio_is_input(struct gpio_bank *bank, int mask)
1761{
1762 void __iomem *reg = bank->base;
1763
1764 switch (bank->method) {
1765 case METHOD_MPUIO:
1766 reg += OMAP_MPUIO_IO_CNTL;
1767 break;
1768 case METHOD_GPIO_1510:
1769 reg += OMAP1510_GPIO_DIR_CONTROL;
1770 break;
1771 case METHOD_GPIO_1610:
1772 reg += OMAP1610_GPIO_DIRECTION;
1773 break;
1774 case METHOD_GPIO_730:
1775 reg += OMAP730_GPIO_DIR_CONTROL;
1776 break;
1777 case METHOD_GPIO_24XX:
1778 reg += OMAP24XX_GPIO_OE;
1779 break;
1780 }
1781 return __raw_readl(reg) & mask;
1782}
1783
1784
1785static int dbg_gpio_show(struct seq_file *s, void *unused)
1786{
1787 unsigned i, j, gpio;
1788
1789 for (i = 0, gpio = 0; i < gpio_bank_count; i++) {
1790 struct gpio_bank *bank = gpio_bank + i;
1791 unsigned bankwidth = 16;
1792 u32 mask = 1;
1793
David Brownelle5c56ed2006-12-06 17:13:59 -08001794 if (bank_is_mpuio(bank))
David Brownellb9772a22006-12-06 17:13:53 -08001795 gpio = OMAP_MPUIO(0);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001796 else if (cpu_class_is_omap2() || cpu_is_omap730())
David Brownellb9772a22006-12-06 17:13:53 -08001797 bankwidth = 32;
1798
1799 for (j = 0; j < bankwidth; j++, gpio++, mask <<= 1) {
1800 unsigned irq, value, is_in, irqstat;
David Brownell52e31342008-03-03 12:43:23 -08001801 const char *label;
David Brownellb9772a22006-12-06 17:13:53 -08001802
David Brownell52e31342008-03-03 12:43:23 -08001803 label = gpiochip_is_requested(&bank->chip, j);
1804 if (!label)
David Brownellb9772a22006-12-06 17:13:53 -08001805 continue;
1806
1807 irq = bank->virtual_irq_start + j;
1808 value = omap_get_gpio_datain(gpio);
1809 is_in = gpio_is_input(bank, mask);
1810
David Brownelle5c56ed2006-12-06 17:13:59 -08001811 if (bank_is_mpuio(bank))
David Brownell52e31342008-03-03 12:43:23 -08001812 seq_printf(s, "MPUIO %2d ", j);
David Brownellb9772a22006-12-06 17:13:53 -08001813 else
David Brownell52e31342008-03-03 12:43:23 -08001814 seq_printf(s, "GPIO %3d ", gpio);
1815 seq_printf(s, "(%10s): %s %s",
1816 label,
David Brownellb9772a22006-12-06 17:13:53 -08001817 is_in ? "in " : "out",
1818 value ? "hi" : "lo");
1819
David Brownell52e31342008-03-03 12:43:23 -08001820/* FIXME for at least omap2, show pullup/pulldown state */
1821
David Brownellb9772a22006-12-06 17:13:53 -08001822 irqstat = irq_desc[irq].status;
1823 if (is_in && ((bank->suspend_wakeup & mask)
1824 || irqstat & IRQ_TYPE_SENSE_MASK)) {
1825 char *trigger = NULL;
1826
1827 switch (irqstat & IRQ_TYPE_SENSE_MASK) {
1828 case IRQ_TYPE_EDGE_FALLING:
1829 trigger = "falling";
1830 break;
1831 case IRQ_TYPE_EDGE_RISING:
1832 trigger = "rising";
1833 break;
1834 case IRQ_TYPE_EDGE_BOTH:
1835 trigger = "bothedge";
1836 break;
1837 case IRQ_TYPE_LEVEL_LOW:
1838 trigger = "low";
1839 break;
1840 case IRQ_TYPE_LEVEL_HIGH:
1841 trigger = "high";
1842 break;
1843 case IRQ_TYPE_NONE:
David Brownell52e31342008-03-03 12:43:23 -08001844 trigger = "(?)";
David Brownellb9772a22006-12-06 17:13:53 -08001845 break;
1846 }
David Brownell52e31342008-03-03 12:43:23 -08001847 seq_printf(s, ", irq-%d %-8s%s",
David Brownellb9772a22006-12-06 17:13:53 -08001848 irq, trigger,
1849 (bank->suspend_wakeup & mask)
1850 ? " wakeup" : "");
1851 }
1852 seq_printf(s, "\n");
1853 }
1854
David Brownelle5c56ed2006-12-06 17:13:59 -08001855 if (bank_is_mpuio(bank)) {
David Brownellb9772a22006-12-06 17:13:53 -08001856 seq_printf(s, "\n");
1857 gpio = 0;
1858 }
1859 }
1860 return 0;
1861}
1862
1863static int dbg_gpio_open(struct inode *inode, struct file *file)
1864{
David Brownelle5c56ed2006-12-06 17:13:59 -08001865 return single_open(file, dbg_gpio_show, &inode->i_private);
David Brownellb9772a22006-12-06 17:13:53 -08001866}
1867
1868static const struct file_operations debug_fops = {
1869 .open = dbg_gpio_open,
1870 .read = seq_read,
1871 .llseek = seq_lseek,
1872 .release = single_release,
1873};
1874
1875static int __init omap_gpio_debuginit(void)
1876{
David Brownelle5c56ed2006-12-06 17:13:59 -08001877 (void) debugfs_create_file("omap_gpio", S_IRUGO,
1878 NULL, NULL, &debug_fops);
David Brownellb9772a22006-12-06 17:13:53 -08001879 return 0;
1880}
1881late_initcall(omap_gpio_debuginit);
1882#endif