blob: 63e094342ef6b7e38ddba9df5c2585b21aaad22d [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
2 * linux/arch/arm/plat-omap/gpio.c
3 *
4 * Support functions for OMAP GPIO
5 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01006 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02007 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010014#include <linux/init.h>
15#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010016#include <linux/interrupt.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010017#include <linux/sysdev.h>
18#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000019#include <linux/clk.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010020
21#include <asm/hardware.h>
22#include <asm/irq.h>
23#include <asm/arch/irqs.h>
24#include <asm/arch/gpio.h>
25#include <asm/mach/irq.h>
26
27#include <asm/io.h>
28
29/*
30 * OMAP1510 GPIO registers
31 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010032#define OMAP1510_GPIO_BASE (void __iomem *)0xfffce000
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010033#define OMAP1510_GPIO_DATA_INPUT 0x00
34#define OMAP1510_GPIO_DATA_OUTPUT 0x04
35#define OMAP1510_GPIO_DIR_CONTROL 0x08
36#define OMAP1510_GPIO_INT_CONTROL 0x0c
37#define OMAP1510_GPIO_INT_MASK 0x10
38#define OMAP1510_GPIO_INT_STATUS 0x14
39#define OMAP1510_GPIO_PIN_CONTROL 0x18
40
41#define OMAP1510_IH_GPIO_BASE 64
42
43/*
44 * OMAP1610 specific GPIO registers
45 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010046#define OMAP1610_GPIO1_BASE (void __iomem *)0xfffbe400
47#define OMAP1610_GPIO2_BASE (void __iomem *)0xfffbec00
48#define OMAP1610_GPIO3_BASE (void __iomem *)0xfffbb400
49#define OMAP1610_GPIO4_BASE (void __iomem *)0xfffbbc00
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010050#define OMAP1610_GPIO_REVISION 0x0000
51#define OMAP1610_GPIO_SYSCONFIG 0x0010
52#define OMAP1610_GPIO_SYSSTATUS 0x0014
53#define OMAP1610_GPIO_IRQSTATUS1 0x0018
54#define OMAP1610_GPIO_IRQENABLE1 0x001c
Tony Lindgren92105bb2005-09-07 17:20:26 +010055#define OMAP1610_GPIO_WAKEUPENABLE 0x0028
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010056#define OMAP1610_GPIO_DATAIN 0x002c
57#define OMAP1610_GPIO_DATAOUT 0x0030
58#define OMAP1610_GPIO_DIRECTION 0x0034
59#define OMAP1610_GPIO_EDGE_CTRL1 0x0038
60#define OMAP1610_GPIO_EDGE_CTRL2 0x003c
61#define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
Tony Lindgren92105bb2005-09-07 17:20:26 +010062#define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010063#define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
64#define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
Tony Lindgren92105bb2005-09-07 17:20:26 +010065#define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010066#define OMAP1610_GPIO_SET_DATAOUT 0x00f0
67
68/*
69 * OMAP730 specific GPIO registers
70 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010071#define OMAP730_GPIO1_BASE (void __iomem *)0xfffbc000
72#define OMAP730_GPIO2_BASE (void __iomem *)0xfffbc800
73#define OMAP730_GPIO3_BASE (void __iomem *)0xfffbd000
74#define OMAP730_GPIO4_BASE (void __iomem *)0xfffbd800
75#define OMAP730_GPIO5_BASE (void __iomem *)0xfffbe000
76#define OMAP730_GPIO6_BASE (void __iomem *)0xfffbe800
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010077#define OMAP730_GPIO_DATA_INPUT 0x00
78#define OMAP730_GPIO_DATA_OUTPUT 0x04
79#define OMAP730_GPIO_DIR_CONTROL 0x08
80#define OMAP730_GPIO_INT_CONTROL 0x0c
81#define OMAP730_GPIO_INT_MASK 0x10
82#define OMAP730_GPIO_INT_STATUS 0x14
83
Tony Lindgren92105bb2005-09-07 17:20:26 +010084/*
85 * omap24xx specific GPIO registers
86 */
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -080087#define OMAP242X_GPIO1_BASE (void __iomem *)0x48018000
88#define OMAP242X_GPIO2_BASE (void __iomem *)0x4801a000
89#define OMAP242X_GPIO3_BASE (void __iomem *)0x4801c000
90#define OMAP242X_GPIO4_BASE (void __iomem *)0x4801e000
91
92#define OMAP243X_GPIO1_BASE (void __iomem *)0x4900C000
93#define OMAP243X_GPIO2_BASE (void __iomem *)0x4900E000
94#define OMAP243X_GPIO3_BASE (void __iomem *)0x49010000
95#define OMAP243X_GPIO4_BASE (void __iomem *)0x49012000
96#define OMAP243X_GPIO5_BASE (void __iomem *)0x480B6000
97
Tony Lindgren92105bb2005-09-07 17:20:26 +010098#define OMAP24XX_GPIO_REVISION 0x0000
99#define OMAP24XX_GPIO_SYSCONFIG 0x0010
100#define OMAP24XX_GPIO_SYSSTATUS 0x0014
101#define OMAP24XX_GPIO_IRQSTATUS1 0x0018
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300102#define OMAP24XX_GPIO_IRQSTATUS2 0x0028
103#define OMAP24XX_GPIO_IRQENABLE2 0x002c
Tony Lindgren92105bb2005-09-07 17:20:26 +0100104#define OMAP24XX_GPIO_IRQENABLE1 0x001c
105#define OMAP24XX_GPIO_CTRL 0x0030
106#define OMAP24XX_GPIO_OE 0x0034
107#define OMAP24XX_GPIO_DATAIN 0x0038
108#define OMAP24XX_GPIO_DATAOUT 0x003c
109#define OMAP24XX_GPIO_LEVELDETECT0 0x0040
110#define OMAP24XX_GPIO_LEVELDETECT1 0x0044
111#define OMAP24XX_GPIO_RISINGDETECT 0x0048
112#define OMAP24XX_GPIO_FALLINGDETECT 0x004c
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700113#define OMAP24XX_GPIO_DEBOUNCE_EN 0x0050
114#define OMAP24XX_GPIO_DEBOUNCE_VAL 0x0054
Tony Lindgren92105bb2005-09-07 17:20:26 +0100115#define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
116#define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
117#define OMAP24XX_GPIO_CLEARWKUENA 0x0080
118#define OMAP24XX_GPIO_SETWKUENA 0x0084
119#define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
120#define OMAP24XX_GPIO_SETDATAOUT 0x0094
121
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800122/*
123 * omap34xx specific GPIO registers
124 */
125
126#define OMAP34XX_GPIO1_BASE (void __iomem *)0x48310000
127#define OMAP34XX_GPIO2_BASE (void __iomem *)0x49050000
128#define OMAP34XX_GPIO3_BASE (void __iomem *)0x49052000
129#define OMAP34XX_GPIO4_BASE (void __iomem *)0x49054000
130#define OMAP34XX_GPIO5_BASE (void __iomem *)0x49056000
131#define OMAP34XX_GPIO6_BASE (void __iomem *)0x49058000
132
133
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134struct gpio_bank {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100135 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136 u16 irq;
137 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100138 int method;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800139#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100140 u32 suspend_wakeup;
141 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800142#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800143#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800144 u32 non_wakeup_gpios;
145 u32 enabled_non_wakeup_gpios;
146
147 u32 saved_datain;
148 u32 saved_fallingdetect;
149 u32 saved_risingdetect;
150#endif
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800151 u32 level_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100152 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -0800153 struct gpio_chip chip;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100154};
155
156#define METHOD_MPUIO 0
157#define METHOD_GPIO_1510 1
158#define METHOD_GPIO_1610 2
159#define METHOD_GPIO_730 3
Tony Lindgren92105bb2005-09-07 17:20:26 +0100160#define METHOD_GPIO_24XX 4
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100161
Tony Lindgren92105bb2005-09-07 17:20:26 +0100162#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100163static struct gpio_bank gpio_bank_1610[5] = {
164 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
165 { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
166 { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
167 { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
168 { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
169};
170#endif
171
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000172#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100173static struct gpio_bank gpio_bank_1510[2] = {
174 { OMAP_MPUIO_BASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
175 { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
176};
177#endif
178
179#ifdef CONFIG_ARCH_OMAP730
180static struct gpio_bank gpio_bank_730[7] = {
181 { OMAP_MPUIO_BASE, INT_730_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
182 { OMAP730_GPIO1_BASE, INT_730_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_730 },
183 { OMAP730_GPIO2_BASE, INT_730_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_730 },
184 { OMAP730_GPIO3_BASE, INT_730_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_730 },
185 { OMAP730_GPIO4_BASE, INT_730_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_730 },
186 { OMAP730_GPIO5_BASE, INT_730_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_730 },
187 { OMAP730_GPIO6_BASE, INT_730_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_730 },
188};
189#endif
190
Tony Lindgren92105bb2005-09-07 17:20:26 +0100191#ifdef CONFIG_ARCH_OMAP24XX
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800192
193static struct gpio_bank gpio_bank_242x[4] = {
194 { OMAP242X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
195 { OMAP242X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
196 { OMAP242X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
197 { OMAP242X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
Tony Lindgren92105bb2005-09-07 17:20:26 +0100198};
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800199
200static struct gpio_bank gpio_bank_243x[5] = {
201 { OMAP243X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
202 { OMAP243X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
203 { OMAP243X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
204 { OMAP243X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
205 { OMAP243X_GPIO5_BASE, INT_24XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
206};
207
Tony Lindgren92105bb2005-09-07 17:20:26 +0100208#endif
209
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800210#ifdef CONFIG_ARCH_OMAP34XX
211static struct gpio_bank gpio_bank_34xx[6] = {
212 { OMAP34XX_GPIO1_BASE, INT_34XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
213 { OMAP34XX_GPIO2_BASE, INT_34XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
214 { OMAP34XX_GPIO3_BASE, INT_34XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
215 { OMAP34XX_GPIO4_BASE, INT_34XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
216 { OMAP34XX_GPIO5_BASE, INT_34XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
217 { OMAP34XX_GPIO6_BASE, INT_34XX_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_24XX },
218};
219
220#endif
221
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100222static struct gpio_bank *gpio_bank;
223static int gpio_bank_count;
224
225static inline struct gpio_bank *get_gpio_bank(int gpio)
226{
Tony Lindgren6e60e792006-04-02 17:46:23 +0100227 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100228 if (OMAP_GPIO_IS_MPUIO(gpio))
229 return &gpio_bank[0];
230 return &gpio_bank[1];
231 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100232 if (cpu_is_omap16xx()) {
233 if (OMAP_GPIO_IS_MPUIO(gpio))
234 return &gpio_bank[0];
235 return &gpio_bank[1 + (gpio >> 4)];
236 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100237 if (cpu_is_omap730()) {
238 if (OMAP_GPIO_IS_MPUIO(gpio))
239 return &gpio_bank[0];
240 return &gpio_bank[1 + (gpio >> 5)];
241 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100242 if (cpu_is_omap24xx())
243 return &gpio_bank[gpio >> 5];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800244 if (cpu_is_omap34xx())
245 return &gpio_bank[gpio >> 5];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100246}
247
248static inline int get_gpio_index(int gpio)
249{
250 if (cpu_is_omap730())
251 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100252 if (cpu_is_omap24xx())
253 return gpio & 0x1f;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800254 if (cpu_is_omap34xx())
255 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100256 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100257}
258
259static inline int gpio_valid(int gpio)
260{
261 if (gpio < 0)
262 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -0800263 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300264 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100265 return -1;
266 return 0;
267 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100268 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100269 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100270 if ((cpu_is_omap16xx()) && gpio < 64)
271 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100272 if (cpu_is_omap730() && gpio < 192)
273 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100274 if (cpu_is_omap24xx() && gpio < 128)
275 return 0;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800276 if (cpu_is_omap34xx() && gpio < 160)
277 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100278 return -1;
279}
280
281static int check_gpio(int gpio)
282{
283 if (unlikely(gpio_valid(gpio)) < 0) {
284 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
285 dump_stack();
286 return -1;
287 }
288 return 0;
289}
290
291static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
292{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100293 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100294 u32 l;
295
296 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800297#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100298 case METHOD_MPUIO:
299 reg += OMAP_MPUIO_IO_CNTL;
300 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800301#endif
302#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100303 case METHOD_GPIO_1510:
304 reg += OMAP1510_GPIO_DIR_CONTROL;
305 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800306#endif
307#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100308 case METHOD_GPIO_1610:
309 reg += OMAP1610_GPIO_DIRECTION;
310 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800311#endif
312#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100313 case METHOD_GPIO_730:
314 reg += OMAP730_GPIO_DIR_CONTROL;
315 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800316#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800317#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100318 case METHOD_GPIO_24XX:
319 reg += OMAP24XX_GPIO_OE;
320 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800321#endif
322 default:
323 WARN_ON(1);
324 return;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100325 }
326 l = __raw_readl(reg);
327 if (is_input)
328 l |= 1 << gpio;
329 else
330 l &= ~(1 << gpio);
331 __raw_writel(l, reg);
332}
333
334void omap_set_gpio_direction(int gpio, int is_input)
335{
336 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800337 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100338
339 if (check_gpio(gpio) < 0)
340 return;
341 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800342 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100343 _set_gpio_direction(bank, get_gpio_index(gpio), is_input);
David Brownella6472532008-03-03 04:33:30 -0800344 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100345}
346
347static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
348{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100349 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100350 u32 l = 0;
351
352 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800353#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100354 case METHOD_MPUIO:
355 reg += OMAP_MPUIO_OUTPUT;
356 l = __raw_readl(reg);
357 if (enable)
358 l |= 1 << gpio;
359 else
360 l &= ~(1 << gpio);
361 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800362#endif
363#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100364 case METHOD_GPIO_1510:
365 reg += OMAP1510_GPIO_DATA_OUTPUT;
366 l = __raw_readl(reg);
367 if (enable)
368 l |= 1 << gpio;
369 else
370 l &= ~(1 << gpio);
371 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800372#endif
373#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100374 case METHOD_GPIO_1610:
375 if (enable)
376 reg += OMAP1610_GPIO_SET_DATAOUT;
377 else
378 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
379 l = 1 << gpio;
380 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800381#endif
382#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100383 case METHOD_GPIO_730:
384 reg += OMAP730_GPIO_DATA_OUTPUT;
385 l = __raw_readl(reg);
386 if (enable)
387 l |= 1 << gpio;
388 else
389 l &= ~(1 << gpio);
390 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800391#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800392#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100393 case METHOD_GPIO_24XX:
394 if (enable)
395 reg += OMAP24XX_GPIO_SETDATAOUT;
396 else
397 reg += OMAP24XX_GPIO_CLEARDATAOUT;
398 l = 1 << gpio;
399 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800400#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100401 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800402 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100403 return;
404 }
405 __raw_writel(l, reg);
406}
407
408void omap_set_gpio_dataout(int gpio, int enable)
409{
410 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800411 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100412
413 if (check_gpio(gpio) < 0)
414 return;
415 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800416 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100417 _set_gpio_dataout(bank, get_gpio_index(gpio), enable);
David Brownella6472532008-03-03 04:33:30 -0800418 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100419}
420
421int omap_get_gpio_datain(int gpio)
422{
423 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100424 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100425
426 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800427 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100428 bank = get_gpio_bank(gpio);
429 reg = bank->base;
430 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800431#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100432 case METHOD_MPUIO:
433 reg += OMAP_MPUIO_INPUT_LATCH;
434 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800435#endif
436#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100437 case METHOD_GPIO_1510:
438 reg += OMAP1510_GPIO_DATA_INPUT;
439 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800440#endif
441#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100442 case METHOD_GPIO_1610:
443 reg += OMAP1610_GPIO_DATAIN;
444 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800445#endif
446#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100447 case METHOD_GPIO_730:
448 reg += OMAP730_GPIO_DATA_INPUT;
449 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800450#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800451#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100452 case METHOD_GPIO_24XX:
453 reg += OMAP24XX_GPIO_DATAIN;
454 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800455#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100456 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800457 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100458 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100459 return (__raw_readl(reg)
460 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100461}
462
Tony Lindgren92105bb2005-09-07 17:20:26 +0100463#define MOD_REG_BIT(reg, bit_mask, set) \
464do { \
465 int l = __raw_readl(base + reg); \
466 if (set) l |= bit_mask; \
467 else l &= ~bit_mask; \
468 __raw_writel(l, base + reg); \
469} while(0)
470
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700471void omap_set_gpio_debounce(int gpio, int enable)
472{
473 struct gpio_bank *bank;
474 void __iomem *reg;
475 u32 val, l = 1 << get_gpio_index(gpio);
476
477 if (cpu_class_is_omap1())
478 return;
479
480 bank = get_gpio_bank(gpio);
481 reg = bank->base;
482
483 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
484 val = __raw_readl(reg);
485
486 if (enable)
487 val |= l;
488 else
489 val &= ~l;
490
491 __raw_writel(val, reg);
492}
493EXPORT_SYMBOL(omap_set_gpio_debounce);
494
495void omap_set_gpio_debounce_time(int gpio, int enc_time)
496{
497 struct gpio_bank *bank;
498 void __iomem *reg;
499
500 if (cpu_class_is_omap1())
501 return;
502
503 bank = get_gpio_bank(gpio);
504 reg = bank->base;
505
506 enc_time &= 0xff;
507 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
508 __raw_writel(enc_time, reg);
509}
510EXPORT_SYMBOL(omap_set_gpio_debounce_time);
511
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800512#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700513static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
514 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100515{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800516 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100517 u32 gpio_bit = 1 << gpio;
518
519 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100520 trigger & IRQ_TYPE_LEVEL_LOW);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100521 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100522 trigger & IRQ_TYPE_LEVEL_HIGH);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100523 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100524 trigger & IRQ_TYPE_EDGE_RISING);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100525 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100526 trigger & IRQ_TYPE_EDGE_FALLING);
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700527
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800528 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
529 if (trigger != 0)
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700530 __raw_writel(1 << gpio, bank->base
531 + OMAP24XX_GPIO_SETWKUENA);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800532 else
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700533 __raw_writel(1 << gpio, bank->base
534 + OMAP24XX_GPIO_CLEARWKUENA);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800535 } else {
536 if (trigger != 0)
537 bank->enabled_non_wakeup_gpios |= gpio_bit;
538 else
539 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
540 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700541
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800542 bank->level_mask =
543 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
544 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100545}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800546#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100547
548static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
549{
550 void __iomem *reg = bank->base;
551 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100552
553 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800554#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100555 case METHOD_MPUIO:
556 reg += OMAP_MPUIO_GPIO_INT_EDGE;
557 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100558 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100559 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100560 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100561 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100562 else
563 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800565#endif
566#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100567 case METHOD_GPIO_1510:
568 reg += OMAP1510_GPIO_INT_CONTROL;
569 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100570 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100571 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100572 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100574 else
575 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800577#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800578#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100579 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100580 if (gpio & 0x08)
581 reg += OMAP1610_GPIO_EDGE_CTRL2;
582 else
583 reg += OMAP1610_GPIO_EDGE_CTRL1;
584 gpio &= 0x07;
585 l = __raw_readl(reg);
586 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100587 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100588 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100589 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100590 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800591 if (trigger)
592 /* Enable wake-up during idle for dynamic tick */
593 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
594 else
595 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100596 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800597#endif
598#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100599 case METHOD_GPIO_730:
600 reg += OMAP730_GPIO_INT_CONTROL;
601 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100602 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100603 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100604 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100605 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100606 else
607 goto bad;
608 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800609#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800610#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100611 case METHOD_GPIO_24XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800612 set_24xx_gpio_triggering(bank, gpio, trigger);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100613 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800614#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100615 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100616 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100617 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100618 __raw_writel(l, reg);
619 return 0;
620bad:
621 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100622}
623
Tony Lindgren92105bb2005-09-07 17:20:26 +0100624static int gpio_irq_type(unsigned irq, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100625{
626 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100627 unsigned gpio;
628 int retval;
David Brownella6472532008-03-03 04:33:30 -0800629 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100630
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800631 if (!cpu_class_is_omap2() && irq > IH_MPUIO_BASE)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100632 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
633 else
634 gpio = irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100635
636 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100637 return -EINVAL;
638
David Brownelle5c56ed2006-12-06 17:13:59 -0800639 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100640 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800641
642 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800643 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800644 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100645 return -EINVAL;
646
David Brownell58781012006-12-06 17:14:10 -0800647 bank = get_irq_chip_data(irq);
David Brownella6472532008-03-03 04:33:30 -0800648 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100649 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
David Brownellb9772a22006-12-06 17:13:53 -0800650 if (retval == 0) {
651 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
652 irq_desc[irq].status |= type;
653 }
David Brownella6472532008-03-03 04:33:30 -0800654 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800655
656 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
657 __set_irq_handler_unlocked(irq, handle_level_irq);
658 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
659 __set_irq_handler_unlocked(irq, handle_edge_irq);
660
Tony Lindgren92105bb2005-09-07 17:20:26 +0100661 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100662}
663
664static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
665{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100666 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100667
668 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800669#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670 case METHOD_MPUIO:
671 /* MPUIO irqstatus is reset by reading the status register,
672 * so do nothing here */
673 return;
David Brownelle5c56ed2006-12-06 17:13:59 -0800674#endif
675#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100676 case METHOD_GPIO_1510:
677 reg += OMAP1510_GPIO_INT_STATUS;
678 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800679#endif
680#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100681 case METHOD_GPIO_1610:
682 reg += OMAP1610_GPIO_IRQSTATUS1;
683 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800684#endif
685#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100686 case METHOD_GPIO_730:
687 reg += OMAP730_GPIO_INT_STATUS;
688 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800689#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800690#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100691 case METHOD_GPIO_24XX:
692 reg += OMAP24XX_GPIO_IRQSTATUS1;
693 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800694#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100695 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800696 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100697 return;
698 }
699 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300700
701 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800702#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
703 if (cpu_is_omap24xx() || cpu_is_omap34xx())
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300704 __raw_writel(gpio_mask, bank->base + OMAP24XX_GPIO_IRQSTATUS2);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800705#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100706}
707
708static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
709{
710 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
711}
712
Imre Deakea6dedd2006-06-26 16:16:00 -0700713static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
714{
715 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700716 int inv = 0;
717 u32 l;
718 u32 mask;
Imre Deakea6dedd2006-06-26 16:16:00 -0700719
720 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800721#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700722 case METHOD_MPUIO:
723 reg += OMAP_MPUIO_GPIO_MASKIT;
Imre Deak99c47702006-06-26 16:16:07 -0700724 mask = 0xffff;
725 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700726 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800727#endif
728#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700729 case METHOD_GPIO_1510:
730 reg += OMAP1510_GPIO_INT_MASK;
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_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700736 case METHOD_GPIO_1610:
737 reg += OMAP1610_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700738 mask = 0xffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700739 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800740#endif
741#ifdef CONFIG_ARCH_OMAP730
Imre Deakea6dedd2006-06-26 16:16:00 -0700742 case METHOD_GPIO_730:
743 reg += OMAP730_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700744 mask = 0xffffffff;
745 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700746 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800747#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800748#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Imre Deakea6dedd2006-06-26 16:16:00 -0700749 case METHOD_GPIO_24XX:
750 reg += OMAP24XX_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700751 mask = 0xffffffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700752 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800753#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700754 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800755 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700756 return 0;
757 }
758
Imre Deak99c47702006-06-26 16:16:07 -0700759 l = __raw_readl(reg);
760 if (inv)
761 l = ~l;
762 l &= mask;
763 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700764}
765
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100766static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
767{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100768 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100769 u32 l;
770
771 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800772#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100773 case METHOD_MPUIO:
774 reg += OMAP_MPUIO_GPIO_MASKIT;
775 l = __raw_readl(reg);
776 if (enable)
777 l &= ~(gpio_mask);
778 else
779 l |= gpio_mask;
780 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800781#endif
782#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100783 case METHOD_GPIO_1510:
784 reg += OMAP1510_GPIO_INT_MASK;
785 l = __raw_readl(reg);
786 if (enable)
787 l &= ~(gpio_mask);
788 else
789 l |= gpio_mask;
790 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800791#endif
792#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100793 case METHOD_GPIO_1610:
794 if (enable)
795 reg += OMAP1610_GPIO_SET_IRQENABLE1;
796 else
797 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
798 l = gpio_mask;
799 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800800#endif
801#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100802 case METHOD_GPIO_730:
803 reg += OMAP730_GPIO_INT_MASK;
804 l = __raw_readl(reg);
805 if (enable)
806 l &= ~(gpio_mask);
807 else
808 l |= gpio_mask;
809 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800810#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800811#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100812 case METHOD_GPIO_24XX:
813 if (enable)
814 reg += OMAP24XX_GPIO_SETIRQENABLE1;
815 else
816 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
817 l = gpio_mask;
818 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800819#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100820 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800821 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100822 return;
823 }
824 __raw_writel(l, reg);
825}
826
827static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
828{
829 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
830}
831
Tony Lindgren92105bb2005-09-07 17:20:26 +0100832/*
833 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
834 * 1510 does not seem to have a wake-up register. If JTAG is connected
835 * to the target, system will wake up always on GPIO events. While
836 * system is running all registered GPIO interrupts need to have wake-up
837 * enabled. When system is suspended, only selected GPIO interrupts need
838 * to have wake-up enabled.
839 */
840static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
841{
David Brownella6472532008-03-03 04:33:30 -0800842 unsigned long flags;
843
Tony Lindgren92105bb2005-09-07 17:20:26 +0100844 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800845#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800846 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100847 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800848 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800849 if (enable) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100850 bank->suspend_wakeup |= (1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800851 enable_irq_wake(bank->irq);
852 } else {
853 disable_irq_wake(bank->irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100854 bank->suspend_wakeup &= ~(1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800855 }
David Brownella6472532008-03-03 04:33:30 -0800856 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100857 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800858#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800859#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800860 case METHOD_GPIO_24XX:
David Brownell11a78b72006-12-06 17:14:11 -0800861 if (bank->non_wakeup_gpios & (1 << gpio)) {
862 printk(KERN_ERR "Unable to modify wakeup on "
863 "non-wakeup GPIO%d\n",
864 (bank - gpio_bank) * 32 + gpio);
865 return -EINVAL;
866 }
David Brownella6472532008-03-03 04:33:30 -0800867 spin_lock_irqsave(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800868 if (enable) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800869 bank->suspend_wakeup |= (1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800870 enable_irq_wake(bank->irq);
871 } else {
872 disable_irq_wake(bank->irq);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800873 bank->suspend_wakeup &= ~(1 << gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800874 }
David Brownella6472532008-03-03 04:33:30 -0800875 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800876 return 0;
877#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100878 default:
879 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
880 bank->method);
881 return -EINVAL;
882 }
883}
884
Tony Lindgren4196dd62006-09-25 12:41:38 +0300885static void _reset_gpio(struct gpio_bank *bank, int gpio)
886{
887 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
888 _set_gpio_irqenable(bank, gpio, 0);
889 _clear_gpio_irqstatus(bank, gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100890 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300891}
892
Tony Lindgren92105bb2005-09-07 17:20:26 +0100893/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
894static int gpio_wake_enable(unsigned int irq, unsigned int enable)
895{
896 unsigned int gpio = irq - IH_GPIO_BASE;
897 struct gpio_bank *bank;
898 int retval;
899
900 if (check_gpio(gpio) < 0)
901 return -ENODEV;
David Brownell58781012006-12-06 17:14:10 -0800902 bank = get_irq_chip_data(irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100903 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100904
905 return retval;
906}
907
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100908int omap_request_gpio(int gpio)
909{
910 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800911 unsigned long flags;
David Brownell52e31342008-03-03 12:43:23 -0800912 int status;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100913
914 if (check_gpio(gpio) < 0)
915 return -EINVAL;
916
David Brownell52e31342008-03-03 12:43:23 -0800917 status = gpio_request(gpio, NULL);
918 if (status < 0)
919 return status;
920
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100921 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800922 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100923
Tony Lindgren4196dd62006-09-25 12:41:38 +0300924 /* Set trigger to none. You need to enable the desired trigger with
925 * request_irq() or set_irq_type().
926 */
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100927 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100928
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000929#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100930 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100931 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100932
Tony Lindgren92105bb2005-09-07 17:20:26 +0100933 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100934 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
935 __raw_writel(__raw_readl(reg) | (1 << get_gpio_index(gpio)), reg);
936 }
937#endif
David Brownella6472532008-03-03 04:33:30 -0800938 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100939
940 return 0;
941}
942
943void omap_free_gpio(int gpio)
944{
945 struct gpio_bank *bank;
David Brownella6472532008-03-03 04:33:30 -0800946 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100947
948 if (check_gpio(gpio) < 0)
949 return;
950 bank = get_gpio_bank(gpio);
David Brownella6472532008-03-03 04:33:30 -0800951 spin_lock_irqsave(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800952 if (unlikely(!gpiochip_is_requested(&bank->chip,
953 get_gpio_index(gpio)))) {
954 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100955 printk(KERN_ERR "omap-gpio: GPIO %d wasn't reserved!\n", gpio);
956 dump_stack();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100957 return;
958 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100959#ifdef CONFIG_ARCH_OMAP16XX
960 if (bank->method == METHOD_GPIO_1610) {
961 /* Disable wake-up during idle for dynamic tick */
962 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
963 __raw_writel(1 << get_gpio_index(gpio), reg);
964 }
965#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800966#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100967 if (bank->method == METHOD_GPIO_24XX) {
968 /* Disable wake-up during idle for dynamic tick */
969 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
970 __raw_writel(1 << get_gpio_index(gpio), reg);
971 }
972#endif
Tony Lindgren4196dd62006-09-25 12:41:38 +0300973 _reset_gpio(bank, gpio);
David Brownella6472532008-03-03 04:33:30 -0800974 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800975 gpio_free(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100976}
977
978/*
979 * We need to unmask the GPIO bank interrupt as soon as possible to
980 * avoid missing GPIO interrupts for other lines in the bank.
981 * Then we need to mask-read-clear-unmask the triggered GPIO lines
982 * in the bank to avoid missing nested interrupts for a GPIO line.
983 * If we wait to unmask individual GPIO lines in the bank after the
984 * line's interrupt handler has been run, we may miss some nested
985 * interrupts.
986 */
Russell King10dd5ce2006-11-23 11:41:32 +0000987static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100988{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100989 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100990 u32 isr;
991 unsigned int gpio_irq;
992 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700993 u32 retrigger = 0;
994 int unmasked = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100995
996 desc->chip->ack(irq);
997
Thomas Gleixner418ca1f02006-07-01 22:32:41 +0100998 bank = get_irq_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -0800999#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001000 if (bank->method == METHOD_MPUIO)
1001 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
David Brownelle5c56ed2006-12-06 17:13:59 -08001002#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001003#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001004 if (bank->method == METHOD_GPIO_1510)
1005 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
1006#endif
1007#if defined(CONFIG_ARCH_OMAP16XX)
1008 if (bank->method == METHOD_GPIO_1610)
1009 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
1010#endif
1011#ifdef CONFIG_ARCH_OMAP730
1012 if (bank->method == METHOD_GPIO_730)
1013 isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
1014#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001015#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001016 if (bank->method == METHOD_GPIO_24XX)
1017 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1018#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001019 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001020 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -07001021 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001022
Imre Deakea6dedd2006-06-26 16:16:00 -07001023 enabled = _get_gpio_irqbank_mask(bank);
1024 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001025
1026 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1027 isr &= 0x0000ffff;
1028
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001029 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001030 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -07001031 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001032
1033 /* clear edge sensitive interrupts before handler(s) are
1034 called so that we don't miss any interrupt occurred while
1035 executing them */
1036 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1037 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1038 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1039
1040 /* if there is only edge sensitive GPIO pin interrupts
1041 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -07001042 if (!level_mask && !unmasked) {
1043 unmasked = 1;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001044 desc->chip->unmask(irq);
Imre Deakea6dedd2006-06-26 16:16:00 -07001045 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001046
Imre Deakea6dedd2006-06-26 16:16:00 -07001047 isr |= retrigger;
1048 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001049 if (!isr)
1050 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001051
Tony Lindgren92105bb2005-09-07 17:20:26 +01001052 gpio_irq = bank->virtual_irq_start;
1053 for (; isr != 0; isr >>= 1, gpio_irq++) {
Russell King10dd5ce2006-11-23 11:41:32 +00001054 struct irq_desc *d;
Kevin Hilman672e3022008-01-16 21:56:16 -08001055
Tony Lindgren92105bb2005-09-07 17:20:26 +01001056 if (!(isr & 1))
1057 continue;
1058 d = irq_desc + gpio_irq;
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001059
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001060 desc_handle_irq(gpio_irq, d);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001061 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001062 }
Imre Deakea6dedd2006-06-26 16:16:00 -07001063 /* if bank has any level sensitive GPIO pin interrupt
1064 configured, we must unmask the bank interrupt only after
1065 handler(s) are executed in order to avoid spurious bank
1066 interrupt */
1067 if (!unmasked)
1068 desc->chip->unmask(irq);
1069
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001070}
1071
Tony Lindgren4196dd62006-09-25 12:41:38 +03001072static void gpio_irq_shutdown(unsigned int irq)
1073{
1074 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001075 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001076
1077 _reset_gpio(bank, gpio);
1078}
1079
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001080static void gpio_ack_irq(unsigned int irq)
1081{
1082 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001083 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001084
1085 _clear_gpio_irqstatus(bank, gpio);
1086}
1087
1088static void gpio_mask_irq(unsigned int irq)
1089{
1090 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001091 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001092
1093 _set_gpio_irqenable(bank, gpio, 0);
1094}
1095
1096static void gpio_unmask_irq(unsigned int irq)
1097{
1098 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001099 struct gpio_bank *bank = get_irq_chip_data(irq);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001100 unsigned int irq_mask = 1 << get_gpio_index(gpio);
1101
1102 /* For level-triggered GPIOs, the clearing must be done after
1103 * the HW source is cleared, thus after the handler has run */
1104 if (bank->level_mask & irq_mask) {
1105 _set_gpio_irqenable(bank, gpio, 0);
1106 _clear_gpio_irqstatus(bank, gpio);
1107 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001108
Kevin Hilman4de8c752008-01-16 21:56:14 -08001109 _set_gpio_irqenable(bank, gpio, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001110}
1111
David Brownelle5c56ed2006-12-06 17:13:59 -08001112static struct irq_chip gpio_irq_chip = {
1113 .name = "GPIO",
1114 .shutdown = gpio_irq_shutdown,
1115 .ack = gpio_ack_irq,
1116 .mask = gpio_mask_irq,
1117 .unmask = gpio_unmask_irq,
1118 .set_type = gpio_irq_type,
1119 .set_wake = gpio_wake_enable,
1120};
1121
1122/*---------------------------------------------------------------------*/
1123
1124#ifdef CONFIG_ARCH_OMAP1
1125
1126/* MPUIO uses the always-on 32k clock */
1127
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001128static void mpuio_ack_irq(unsigned int irq)
1129{
1130 /* The ISR is reset automatically, so do nothing here. */
1131}
1132
1133static void mpuio_mask_irq(unsigned int irq)
1134{
1135 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001136 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001137
1138 _set_gpio_irqenable(bank, gpio, 0);
1139}
1140
1141static void mpuio_unmask_irq(unsigned int irq)
1142{
1143 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001144 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001145
1146 _set_gpio_irqenable(bank, gpio, 1);
1147}
1148
David Brownelle5c56ed2006-12-06 17:13:59 -08001149static struct irq_chip mpuio_irq_chip = {
1150 .name = "MPUIO",
1151 .ack = mpuio_ack_irq,
1152 .mask = mpuio_mask_irq,
1153 .unmask = mpuio_unmask_irq,
Tony Lindgren92105bb2005-09-07 17:20:26 +01001154 .set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001155#ifdef CONFIG_ARCH_OMAP16XX
1156 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1157 .set_wake = gpio_wake_enable,
1158#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001159};
1160
David Brownelle5c56ed2006-12-06 17:13:59 -08001161
1162#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1163
David Brownell11a78b72006-12-06 17:14:11 -08001164
1165#ifdef CONFIG_ARCH_OMAP16XX
1166
1167#include <linux/platform_device.h>
1168
1169static int omap_mpuio_suspend_late(struct platform_device *pdev, pm_message_t mesg)
1170{
1171 struct gpio_bank *bank = platform_get_drvdata(pdev);
1172 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001173 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001174
David Brownella6472532008-03-03 04:33:30 -08001175 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001176 bank->saved_wakeup = __raw_readl(mask_reg);
1177 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001178 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001179
1180 return 0;
1181}
1182
1183static int omap_mpuio_resume_early(struct platform_device *pdev)
1184{
1185 struct gpio_bank *bank = platform_get_drvdata(pdev);
1186 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001187 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001188
David Brownella6472532008-03-03 04:33:30 -08001189 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001190 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001191 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001192
1193 return 0;
1194}
1195
1196/* use platform_driver for this, now that there's no longer any
1197 * point to sys_device (other than not disturbing old code).
1198 */
1199static struct platform_driver omap_mpuio_driver = {
1200 .suspend_late = omap_mpuio_suspend_late,
1201 .resume_early = omap_mpuio_resume_early,
1202 .driver = {
1203 .name = "mpuio",
1204 },
1205};
1206
1207static struct platform_device omap_mpuio_device = {
1208 .name = "mpuio",
1209 .id = -1,
1210 .dev = {
1211 .driver = &omap_mpuio_driver.driver,
1212 }
1213 /* could list the /proc/iomem resources */
1214};
1215
1216static inline void mpuio_init(void)
1217{
David Brownellfcf126d2007-04-02 12:46:47 -07001218 platform_set_drvdata(&omap_mpuio_device, &gpio_bank_1610[0]);
1219
David Brownell11a78b72006-12-06 17:14:11 -08001220 if (platform_driver_register(&omap_mpuio_driver) == 0)
1221 (void) platform_device_register(&omap_mpuio_device);
1222}
1223
1224#else
1225static inline void mpuio_init(void) {}
1226#endif /* 16xx */
1227
David Brownelle5c56ed2006-12-06 17:13:59 -08001228#else
1229
1230extern struct irq_chip mpuio_irq_chip;
1231
1232#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001233static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001234
1235#endif
1236
1237/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001238
David Brownell52e31342008-03-03 12:43:23 -08001239/* REVISIT these are stupid implementations! replace by ones that
1240 * don't switch on METHOD_* and which mostly avoid spinlocks
1241 */
1242
1243static int gpio_input(struct gpio_chip *chip, unsigned offset)
1244{
1245 struct gpio_bank *bank;
1246 unsigned long flags;
1247
1248 bank = container_of(chip, struct gpio_bank, chip);
1249 spin_lock_irqsave(&bank->lock, flags);
1250 _set_gpio_direction(bank, offset, 1);
1251 spin_unlock_irqrestore(&bank->lock, flags);
1252 return 0;
1253}
1254
1255static int gpio_get(struct gpio_chip *chip, unsigned offset)
1256{
1257 return omap_get_gpio_datain(chip->base + offset);
1258}
1259
1260static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1261{
1262 struct gpio_bank *bank;
1263 unsigned long flags;
1264
1265 bank = container_of(chip, struct gpio_bank, chip);
1266 spin_lock_irqsave(&bank->lock, flags);
1267 _set_gpio_dataout(bank, offset, value);
1268 _set_gpio_direction(bank, offset, 0);
1269 spin_unlock_irqrestore(&bank->lock, flags);
1270 return 0;
1271}
1272
1273static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1274{
1275 struct gpio_bank *bank;
1276 unsigned long flags;
1277
1278 bank = container_of(chip, struct gpio_bank, chip);
1279 spin_lock_irqsave(&bank->lock, flags);
1280 _set_gpio_dataout(bank, offset, value);
1281 spin_unlock_irqrestore(&bank->lock, flags);
1282}
1283
1284/*---------------------------------------------------------------------*/
1285
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001286static int initialized;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001287#if !defined(CONFIG_ARCH_OMAP3)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001288static struct clk * gpio_ick;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001289#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001290
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001291#if defined(CONFIG_ARCH_OMAP2)
1292static struct clk * gpio_fck;
1293#endif
1294
1295#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001296static struct clk * gpio5_ick;
1297static struct clk * gpio5_fck;
1298#endif
1299
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001300#if defined(CONFIG_ARCH_OMAP3)
1301static struct clk *gpio_fclks[OMAP34XX_NR_GPIOS];
1302static struct clk *gpio_iclks[OMAP34XX_NR_GPIOS];
1303#endif
1304
David Brownell8ba55c52008-02-26 11:10:50 -08001305/* This lock class tells lockdep that GPIO irqs are in a different
1306 * category than their parents, so it won't report false recursion.
1307 */
1308static struct lock_class_key gpio_lock_class;
1309
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001310static int __init _omap_gpio_init(void)
1311{
1312 int i;
David Brownell52e31342008-03-03 12:43:23 -08001313 int gpio = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001314 struct gpio_bank *bank;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001315#if defined(CONFIG_ARCH_OMAP3)
1316 char clk_name[11];
1317#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001318
1319 initialized = 1;
1320
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001321#if defined(CONFIG_ARCH_OMAP1)
Tony Lindgren6e60e792006-04-02 17:46:23 +01001322 if (cpu_is_omap15xx()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001323 gpio_ick = clk_get(NULL, "arm_gpio_ck");
1324 if (IS_ERR(gpio_ick))
Tony Lindgren92105bb2005-09-07 17:20:26 +01001325 printk("Could not get arm_gpio_ck\n");
1326 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001327 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001328 }
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001329#endif
1330#if defined(CONFIG_ARCH_OMAP2)
1331 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001332 gpio_ick = clk_get(NULL, "gpios_ick");
1333 if (IS_ERR(gpio_ick))
1334 printk("Could not get gpios_ick\n");
1335 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001336 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001337 gpio_fck = clk_get(NULL, "gpios_fck");
Komal Shah1630b522006-09-25 12:51:08 +03001338 if (IS_ERR(gpio_fck))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001339 printk("Could not get gpios_fck\n");
1340 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001341 clk_enable(gpio_fck);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001342
1343 /*
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001344 * On 2430 & 3430 GPIO 5 uses CORE L4 ICLK
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001345 */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001346#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001347 if (cpu_is_omap2430()) {
1348 gpio5_ick = clk_get(NULL, "gpio5_ick");
1349 if (IS_ERR(gpio5_ick))
1350 printk("Could not get gpio5_ick\n");
1351 else
1352 clk_enable(gpio5_ick);
1353 gpio5_fck = clk_get(NULL, "gpio5_fck");
1354 if (IS_ERR(gpio5_fck))
1355 printk("Could not get gpio5_fck\n");
1356 else
1357 clk_enable(gpio5_fck);
1358 }
1359#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001360 }
1361#endif
1362
1363#if defined(CONFIG_ARCH_OMAP3)
1364 if (cpu_is_omap34xx()) {
1365 for (i = 0; i < OMAP34XX_NR_GPIOS; i++) {
1366 sprintf(clk_name, "gpio%d_ick", i + 1);
1367 gpio_iclks[i] = clk_get(NULL, clk_name);
1368 if (IS_ERR(gpio_iclks[i]))
1369 printk(KERN_ERR "Could not get %s\n", clk_name);
1370 else
1371 clk_enable(gpio_iclks[i]);
1372 sprintf(clk_name, "gpio%d_fck", i + 1);
1373 gpio_fclks[i] = clk_get(NULL, clk_name);
1374 if (IS_ERR(gpio_fclks[i]))
1375 printk(KERN_ERR "Could not get %s\n", clk_name);
1376 else
1377 clk_enable(gpio_fclks[i]);
1378 }
1379 }
1380#endif
1381
Tony Lindgren92105bb2005-09-07 17:20:26 +01001382
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001383#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +01001384 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001385 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
1386 gpio_bank_count = 2;
1387 gpio_bank = gpio_bank_1510;
1388 }
1389#endif
1390#if defined(CONFIG_ARCH_OMAP16XX)
1391 if (cpu_is_omap16xx()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001392 u32 rev;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001393
1394 gpio_bank_count = 5;
1395 gpio_bank = gpio_bank_1610;
1396 rev = omap_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
1397 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1398 (rev >> 4) & 0x0f, rev & 0x0f);
1399 }
1400#endif
1401#ifdef CONFIG_ARCH_OMAP730
1402 if (cpu_is_omap730()) {
1403 printk(KERN_INFO "OMAP730 GPIO hardware\n");
1404 gpio_bank_count = 7;
1405 gpio_bank = gpio_bank_730;
1406 }
1407#endif
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001408
Tony Lindgren92105bb2005-09-07 17:20:26 +01001409#ifdef CONFIG_ARCH_OMAP24XX
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001410 if (cpu_is_omap242x()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001411 int rev;
1412
1413 gpio_bank_count = 4;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001414 gpio_bank = gpio_bank_242x;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001415 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001416 printk(KERN_INFO "OMAP242x GPIO hardware version %d.%d\n",
1417 (rev >> 4) & 0x0f, rev & 0x0f);
1418 }
1419 if (cpu_is_omap243x()) {
1420 int rev;
1421
1422 gpio_bank_count = 5;
1423 gpio_bank = gpio_bank_243x;
1424 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1425 printk(KERN_INFO "OMAP243x GPIO hardware version %d.%d\n",
Tony Lindgren92105bb2005-09-07 17:20:26 +01001426 (rev >> 4) & 0x0f, rev & 0x0f);
1427 }
1428#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001429#ifdef CONFIG_ARCH_OMAP34XX
1430 if (cpu_is_omap34xx()) {
1431 int rev;
1432
1433 gpio_bank_count = OMAP34XX_NR_GPIOS;
1434 gpio_bank = gpio_bank_34xx;
1435 rev = omap_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1436 printk(KERN_INFO "OMAP34xx GPIO hardware version %d.%d\n",
1437 (rev >> 4) & 0x0f, rev & 0x0f);
1438 }
1439#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001440 for (i = 0; i < gpio_bank_count; i++) {
1441 int j, gpio_count = 16;
1442
1443 bank = &gpio_bank[i];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001444 bank->base = IO_ADDRESS(bank->base);
1445 spin_lock_init(&bank->lock);
David Brownelle5c56ed2006-12-06 17:13:59 -08001446 if (bank_is_mpuio(bank))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001447 omap_writew(0xFFFF, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_MASKIT);
Tony Lindgrend11ac972008-01-12 15:35:04 -08001448 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001449 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
1450 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
1451 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001452 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001453 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
1454 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001455 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001456 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001457 if (cpu_is_omap730() && bank->method == METHOD_GPIO_730) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001458 __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
1459 __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
1460
1461 gpio_count = 32; /* 730 has 32-bit GPIOs */
1462 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001463
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001464#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001465 if (bank->method == METHOD_GPIO_24XX) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001466 static const u32 non_wakeup_gpios[] = {
1467 0xe203ffc0, 0x08700040
1468 };
1469
Tony Lindgren92105bb2005-09-07 17:20:26 +01001470 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
1471 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001472 __raw_writew(0x0015, bank->base + OMAP24XX_GPIO_SYSCONFIG);
1473
1474 /* Initialize interface clock ungated, module enabled */
1475 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001476 if (i < ARRAY_SIZE(non_wakeup_gpios))
1477 bank->non_wakeup_gpios = non_wakeup_gpios[i];
Tony Lindgren92105bb2005-09-07 17:20:26 +01001478 gpio_count = 32;
1479 }
1480#endif
David Brownell52e31342008-03-03 12:43:23 -08001481
1482 /* REVISIT eventually switch from OMAP-specific gpio structs
1483 * over to the generic ones
1484 */
1485 bank->chip.direction_input = gpio_input;
1486 bank->chip.get = gpio_get;
1487 bank->chip.direction_output = gpio_output;
1488 bank->chip.set = gpio_set;
1489 if (bank_is_mpuio(bank)) {
1490 bank->chip.label = "mpuio";
David Brownelld8f388d82008-07-25 01:46:07 -07001491#ifdef CONFIG_ARCH_OMAP1
1492 bank->chip.dev = &omap_mpuio_device.dev;
1493#endif
David Brownell52e31342008-03-03 12:43:23 -08001494 bank->chip.base = OMAP_MPUIO(0);
1495 } else {
1496 bank->chip.label = "gpio";
1497 bank->chip.base = gpio;
1498 gpio += gpio_count;
1499 }
1500 bank->chip.ngpio = gpio_count;
1501
1502 gpiochip_add(&bank->chip);
1503
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001504 for (j = bank->virtual_irq_start;
1505 j < bank->virtual_irq_start + gpio_count; j++) {
David Brownell8ba55c52008-02-26 11:10:50 -08001506 lockdep_set_class(&irq_desc[j].lock, &gpio_lock_class);
David Brownell58781012006-12-06 17:14:10 -08001507 set_irq_chip_data(j, bank);
David Brownelle5c56ed2006-12-06 17:13:59 -08001508 if (bank_is_mpuio(bank))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001509 set_irq_chip(j, &mpuio_irq_chip);
1510 else
1511 set_irq_chip(j, &gpio_irq_chip);
Russell King10dd5ce2006-11-23 11:41:32 +00001512 set_irq_handler(j, handle_simple_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001513 set_irq_flags(j, IRQF_VALID);
1514 }
1515 set_irq_chained_handler(bank->irq, gpio_irq_handler);
1516 set_irq_data(bank->irq, bank);
1517 }
1518
1519 /* Enable system clock for GPIO module.
1520 * The CAM_CLK_CTRL *is* really the right place. */
Tony Lindgren92105bb2005-09-07 17:20:26 +01001521 if (cpu_is_omap16xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001522 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
1523
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001524 /* Enable autoidle for the OCP interface */
1525 if (cpu_is_omap24xx())
1526 omap_writel(1 << 0, 0x48019010);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001527 if (cpu_is_omap34xx())
1528 omap_writel(1 << 0, 0x48306814);
Tony Lindgrend11ac972008-01-12 15:35:04 -08001529
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001530 return 0;
1531}
1532
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001533#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001534static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
1535{
1536 int i;
1537
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001538 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001539 return 0;
1540
1541 for (i = 0; i < gpio_bank_count; i++) {
1542 struct gpio_bank *bank = &gpio_bank[i];
1543 void __iomem *wake_status;
1544 void __iomem *wake_clear;
1545 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001546 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001547
1548 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001549#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001550 case METHOD_GPIO_1610:
1551 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1552 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1553 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1554 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001555#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001556#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001557 case METHOD_GPIO_24XX:
1558 wake_status = bank->base + OMAP24XX_GPIO_SETWKUENA;
1559 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1560 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1561 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001562#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001563 default:
1564 continue;
1565 }
1566
David Brownella6472532008-03-03 04:33:30 -08001567 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001568 bank->saved_wakeup = __raw_readl(wake_status);
1569 __raw_writel(0xffffffff, wake_clear);
1570 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001571 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001572 }
1573
1574 return 0;
1575}
1576
1577static int omap_gpio_resume(struct sys_device *dev)
1578{
1579 int i;
1580
1581 if (!cpu_is_omap24xx() && !cpu_is_omap16xx())
1582 return 0;
1583
1584 for (i = 0; i < gpio_bank_count; i++) {
1585 struct gpio_bank *bank = &gpio_bank[i];
1586 void __iomem *wake_clear;
1587 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001588 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001589
1590 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001591#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001592 case METHOD_GPIO_1610:
1593 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1594 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1595 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001596#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001597#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001598 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001599 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1600 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001601 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001602#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001603 default:
1604 continue;
1605 }
1606
David Brownella6472532008-03-03 04:33:30 -08001607 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001608 __raw_writel(0xffffffff, wake_clear);
1609 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001610 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001611 }
1612
1613 return 0;
1614}
1615
1616static struct sysdev_class omap_gpio_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001617 .name = "gpio",
Tony Lindgren92105bb2005-09-07 17:20:26 +01001618 .suspend = omap_gpio_suspend,
1619 .resume = omap_gpio_resume,
1620};
1621
1622static struct sys_device omap_gpio_device = {
1623 .id = 0,
1624 .cls = &omap_gpio_sysclass,
1625};
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001626
1627#endif
1628
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001629#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001630
1631static int workaround_enabled;
1632
1633void omap2_gpio_prepare_for_retention(void)
1634{
1635 int i, c = 0;
1636
1637 /* Remove triggering for all non-wakeup GPIOs. Otherwise spurious
1638 * IRQs will be generated. See OMAP2420 Errata item 1.101. */
1639 for (i = 0; i < gpio_bank_count; i++) {
1640 struct gpio_bank *bank = &gpio_bank[i];
1641 u32 l1, l2;
1642
1643 if (!(bank->enabled_non_wakeup_gpios))
1644 continue;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001645#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001646 bank->saved_datain = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1647 l1 = __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1648 l2 = __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001649#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001650 bank->saved_fallingdetect = l1;
1651 bank->saved_risingdetect = l2;
1652 l1 &= ~bank->enabled_non_wakeup_gpios;
1653 l2 &= ~bank->enabled_non_wakeup_gpios;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001654#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001655 __raw_writel(l1, bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1656 __raw_writel(l2, bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001657#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001658 c++;
1659 }
1660 if (!c) {
1661 workaround_enabled = 0;
1662 return;
1663 }
1664 workaround_enabled = 1;
1665}
1666
1667void omap2_gpio_resume_after_retention(void)
1668{
1669 int i;
1670
1671 if (!workaround_enabled)
1672 return;
1673 for (i = 0; i < gpio_bank_count; i++) {
1674 struct gpio_bank *bank = &gpio_bank[i];
1675 u32 l;
1676
1677 if (!(bank->enabled_non_wakeup_gpios))
1678 continue;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001679#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001680 __raw_writel(bank->saved_fallingdetect,
1681 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1682 __raw_writel(bank->saved_risingdetect,
1683 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001684#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001685 /* Check if any of the non-wakeup interrupt GPIOs have changed
1686 * state. If so, generate an IRQ by software. This is
1687 * horribly racy, but it's the best we can do to work around
1688 * this silicon bug. */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001689#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001690 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001691#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001692 l ^= bank->saved_datain;
1693 l &= bank->non_wakeup_gpios;
1694 if (l) {
1695 u32 old0, old1;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001696#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001697 old0 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1698 old1 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1699 __raw_writel(old0 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1700 __raw_writel(old1 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1701 __raw_writel(old0, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1702 __raw_writel(old1, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001703#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001704 }
1705 }
1706
1707}
1708
Tony Lindgren92105bb2005-09-07 17:20:26 +01001709#endif
1710
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001711/*
1712 * This may get called early from board specific init
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001713 * for boards that have interrupts routed via FPGA.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001714 */
David Brownell277d58e2006-12-06 17:13:59 -08001715int __init omap_gpio_init(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001716{
1717 if (!initialized)
1718 return _omap_gpio_init();
1719 else
1720 return 0;
1721}
1722
Tony Lindgren92105bb2005-09-07 17:20:26 +01001723static int __init omap_gpio_sysinit(void)
1724{
1725 int ret = 0;
1726
1727 if (!initialized)
1728 ret = _omap_gpio_init();
1729
David Brownell11a78b72006-12-06 17:14:11 -08001730 mpuio_init();
1731
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001732#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
1733 if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001734 if (ret == 0) {
1735 ret = sysdev_class_register(&omap_gpio_sysclass);
1736 if (ret == 0)
1737 ret = sysdev_register(&omap_gpio_device);
1738 }
1739 }
1740#endif
1741
1742 return ret;
1743}
1744
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001745EXPORT_SYMBOL(omap_request_gpio);
1746EXPORT_SYMBOL(omap_free_gpio);
1747EXPORT_SYMBOL(omap_set_gpio_direction);
1748EXPORT_SYMBOL(omap_set_gpio_dataout);
1749EXPORT_SYMBOL(omap_get_gpio_datain);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001750
Tony Lindgren92105bb2005-09-07 17:20:26 +01001751arch_initcall(omap_gpio_sysinit);
David Brownellb9772a22006-12-06 17:13:53 -08001752
1753
1754#ifdef CONFIG_DEBUG_FS
1755
1756#include <linux/debugfs.h>
1757#include <linux/seq_file.h>
1758
1759static int gpio_is_input(struct gpio_bank *bank, int mask)
1760{
1761 void __iomem *reg = bank->base;
1762
1763 switch (bank->method) {
1764 case METHOD_MPUIO:
1765 reg += OMAP_MPUIO_IO_CNTL;
1766 break;
1767 case METHOD_GPIO_1510:
1768 reg += OMAP1510_GPIO_DIR_CONTROL;
1769 break;
1770 case METHOD_GPIO_1610:
1771 reg += OMAP1610_GPIO_DIRECTION;
1772 break;
1773 case METHOD_GPIO_730:
1774 reg += OMAP730_GPIO_DIR_CONTROL;
1775 break;
1776 case METHOD_GPIO_24XX:
1777 reg += OMAP24XX_GPIO_OE;
1778 break;
1779 }
1780 return __raw_readl(reg) & mask;
1781}
1782
1783
1784static int dbg_gpio_show(struct seq_file *s, void *unused)
1785{
1786 unsigned i, j, gpio;
1787
1788 for (i = 0, gpio = 0; i < gpio_bank_count; i++) {
1789 struct gpio_bank *bank = gpio_bank + i;
1790 unsigned bankwidth = 16;
1791 u32 mask = 1;
1792
David Brownelle5c56ed2006-12-06 17:13:59 -08001793 if (bank_is_mpuio(bank))
David Brownellb9772a22006-12-06 17:13:53 -08001794 gpio = OMAP_MPUIO(0);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001795 else if (cpu_class_is_omap2() || cpu_is_omap730())
David Brownellb9772a22006-12-06 17:13:53 -08001796 bankwidth = 32;
1797
1798 for (j = 0; j < bankwidth; j++, gpio++, mask <<= 1) {
1799 unsigned irq, value, is_in, irqstat;
David Brownell52e31342008-03-03 12:43:23 -08001800 const char *label;
David Brownellb9772a22006-12-06 17:13:53 -08001801
David Brownell52e31342008-03-03 12:43:23 -08001802 label = gpiochip_is_requested(&bank->chip, j);
1803 if (!label)
David Brownellb9772a22006-12-06 17:13:53 -08001804 continue;
1805
1806 irq = bank->virtual_irq_start + j;
1807 value = omap_get_gpio_datain(gpio);
1808 is_in = gpio_is_input(bank, mask);
1809
David Brownelle5c56ed2006-12-06 17:13:59 -08001810 if (bank_is_mpuio(bank))
David Brownell52e31342008-03-03 12:43:23 -08001811 seq_printf(s, "MPUIO %2d ", j);
David Brownellb9772a22006-12-06 17:13:53 -08001812 else
David Brownell52e31342008-03-03 12:43:23 -08001813 seq_printf(s, "GPIO %3d ", gpio);
1814 seq_printf(s, "(%10s): %s %s",
1815 label,
David Brownellb9772a22006-12-06 17:13:53 -08001816 is_in ? "in " : "out",
1817 value ? "hi" : "lo");
1818
David Brownell52e31342008-03-03 12:43:23 -08001819/* FIXME for at least omap2, show pullup/pulldown state */
1820
David Brownellb9772a22006-12-06 17:13:53 -08001821 irqstat = irq_desc[irq].status;
1822 if (is_in && ((bank->suspend_wakeup & mask)
1823 || irqstat & IRQ_TYPE_SENSE_MASK)) {
1824 char *trigger = NULL;
1825
1826 switch (irqstat & IRQ_TYPE_SENSE_MASK) {
1827 case IRQ_TYPE_EDGE_FALLING:
1828 trigger = "falling";
1829 break;
1830 case IRQ_TYPE_EDGE_RISING:
1831 trigger = "rising";
1832 break;
1833 case IRQ_TYPE_EDGE_BOTH:
1834 trigger = "bothedge";
1835 break;
1836 case IRQ_TYPE_LEVEL_LOW:
1837 trigger = "low";
1838 break;
1839 case IRQ_TYPE_LEVEL_HIGH:
1840 trigger = "high";
1841 break;
1842 case IRQ_TYPE_NONE:
David Brownell52e31342008-03-03 12:43:23 -08001843 trigger = "(?)";
David Brownellb9772a22006-12-06 17:13:53 -08001844 break;
1845 }
David Brownell52e31342008-03-03 12:43:23 -08001846 seq_printf(s, ", irq-%d %-8s%s",
David Brownellb9772a22006-12-06 17:13:53 -08001847 irq, trigger,
1848 (bank->suspend_wakeup & mask)
1849 ? " wakeup" : "");
1850 }
1851 seq_printf(s, "\n");
1852 }
1853
David Brownelle5c56ed2006-12-06 17:13:59 -08001854 if (bank_is_mpuio(bank)) {
David Brownellb9772a22006-12-06 17:13:53 -08001855 seq_printf(s, "\n");
1856 gpio = 0;
1857 }
1858 }
1859 return 0;
1860}
1861
1862static int dbg_gpio_open(struct inode *inode, struct file *file)
1863{
David Brownelle5c56ed2006-12-06 17:13:59 -08001864 return single_open(file, dbg_gpio_show, &inode->i_private);
David Brownellb9772a22006-12-06 17:13:53 -08001865}
1866
1867static const struct file_operations debug_fops = {
1868 .open = dbg_gpio_open,
1869 .read = seq_read,
1870 .llseek = seq_lseek,
1871 .release = single_release,
1872};
1873
1874static int __init omap_gpio_debuginit(void)
1875{
David Brownelle5c56ed2006-12-06 17:13:59 -08001876 (void) debugfs_create_file("omap_gpio", S_IRUGO,
1877 NULL, NULL, &debug_fops);
David Brownellb9772a22006-12-06 17:13:53 -08001878 return 0;
1879}
1880late_initcall(omap_gpio_debuginit);
1881#endif