blob: 77bad14633e1335c77d4433cf41203ff4db8700b [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 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07009 * Copyright (C) 2009 Texas Instruments
10 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
11 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/init.h>
18#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010019#include <linux/interrupt.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010020#include <linux/sysdev.h>
21#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000022#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010023#include <linux/io.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/irqs.h>
28#include <mach/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029#include <asm/mach/irq.h>
30
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031/*
32 * OMAP1510 GPIO registers
33 */
Russell King7c7095a2008-09-05 15:49:14 +010034#define OMAP1510_GPIO_BASE IO_ADDRESS(0xfffce000)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010035#define OMAP1510_GPIO_DATA_INPUT 0x00
36#define OMAP1510_GPIO_DATA_OUTPUT 0x04
37#define OMAP1510_GPIO_DIR_CONTROL 0x08
38#define OMAP1510_GPIO_INT_CONTROL 0x0c
39#define OMAP1510_GPIO_INT_MASK 0x10
40#define OMAP1510_GPIO_INT_STATUS 0x14
41#define OMAP1510_GPIO_PIN_CONTROL 0x18
42
43#define OMAP1510_IH_GPIO_BASE 64
44
45/*
46 * OMAP1610 specific GPIO registers
47 */
Russell King7c7095a2008-09-05 15:49:14 +010048#define OMAP1610_GPIO1_BASE IO_ADDRESS(0xfffbe400)
49#define OMAP1610_GPIO2_BASE IO_ADDRESS(0xfffbec00)
50#define OMAP1610_GPIO3_BASE IO_ADDRESS(0xfffbb400)
51#define OMAP1610_GPIO4_BASE IO_ADDRESS(0xfffbbc00)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010052#define OMAP1610_GPIO_REVISION 0x0000
53#define OMAP1610_GPIO_SYSCONFIG 0x0010
54#define OMAP1610_GPIO_SYSSTATUS 0x0014
55#define OMAP1610_GPIO_IRQSTATUS1 0x0018
56#define OMAP1610_GPIO_IRQENABLE1 0x001c
Tony Lindgren92105bb2005-09-07 17:20:26 +010057#define OMAP1610_GPIO_WAKEUPENABLE 0x0028
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010058#define OMAP1610_GPIO_DATAIN 0x002c
59#define OMAP1610_GPIO_DATAOUT 0x0030
60#define OMAP1610_GPIO_DIRECTION 0x0034
61#define OMAP1610_GPIO_EDGE_CTRL1 0x0038
62#define OMAP1610_GPIO_EDGE_CTRL2 0x003c
63#define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
Tony Lindgren92105bb2005-09-07 17:20:26 +010064#define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010065#define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
66#define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
Tony Lindgren92105bb2005-09-07 17:20:26 +010067#define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010068#define OMAP1610_GPIO_SET_DATAOUT 0x00f0
69
70/*
71 * OMAP730 specific GPIO registers
72 */
Russell King7c7095a2008-09-05 15:49:14 +010073#define OMAP730_GPIO1_BASE IO_ADDRESS(0xfffbc000)
74#define OMAP730_GPIO2_BASE IO_ADDRESS(0xfffbc800)
75#define OMAP730_GPIO3_BASE IO_ADDRESS(0xfffbd000)
76#define OMAP730_GPIO4_BASE IO_ADDRESS(0xfffbd800)
77#define OMAP730_GPIO5_BASE IO_ADDRESS(0xfffbe000)
78#define OMAP730_GPIO6_BASE IO_ADDRESS(0xfffbe800)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010079#define OMAP730_GPIO_DATA_INPUT 0x00
80#define OMAP730_GPIO_DATA_OUTPUT 0x04
81#define OMAP730_GPIO_DIR_CONTROL 0x08
82#define OMAP730_GPIO_INT_CONTROL 0x0c
83#define OMAP730_GPIO_INT_MASK 0x10
84#define OMAP730_GPIO_INT_STATUS 0x14
85
Tony Lindgren92105bb2005-09-07 17:20:26 +010086/*
Zebediah C. McClure56739a62009-03-23 18:07:40 -070087 * OMAP850 specific GPIO registers
88 */
89#define OMAP850_GPIO1_BASE IO_ADDRESS(0xfffbc000)
90#define OMAP850_GPIO2_BASE IO_ADDRESS(0xfffbc800)
91#define OMAP850_GPIO3_BASE IO_ADDRESS(0xfffbd000)
92#define OMAP850_GPIO4_BASE IO_ADDRESS(0xfffbd800)
93#define OMAP850_GPIO5_BASE IO_ADDRESS(0xfffbe000)
94#define OMAP850_GPIO6_BASE IO_ADDRESS(0xfffbe800)
95#define OMAP850_GPIO_DATA_INPUT 0x00
96#define OMAP850_GPIO_DATA_OUTPUT 0x04
97#define OMAP850_GPIO_DIR_CONTROL 0x08
98#define OMAP850_GPIO_INT_CONTROL 0x0c
99#define OMAP850_GPIO_INT_MASK 0x10
100#define OMAP850_GPIO_INT_STATUS 0x14
101
102/*
Tony Lindgren92105bb2005-09-07 17:20:26 +0100103 * omap24xx specific GPIO registers
104 */
Russell King7c7095a2008-09-05 15:49:14 +0100105#define OMAP242X_GPIO1_BASE IO_ADDRESS(0x48018000)
106#define OMAP242X_GPIO2_BASE IO_ADDRESS(0x4801a000)
107#define OMAP242X_GPIO3_BASE IO_ADDRESS(0x4801c000)
108#define OMAP242X_GPIO4_BASE IO_ADDRESS(0x4801e000)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800109
Russell King7c7095a2008-09-05 15:49:14 +0100110#define OMAP243X_GPIO1_BASE IO_ADDRESS(0x4900C000)
111#define OMAP243X_GPIO2_BASE IO_ADDRESS(0x4900E000)
112#define OMAP243X_GPIO3_BASE IO_ADDRESS(0x49010000)
113#define OMAP243X_GPIO4_BASE IO_ADDRESS(0x49012000)
114#define OMAP243X_GPIO5_BASE IO_ADDRESS(0x480B6000)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800115
Tony Lindgren92105bb2005-09-07 17:20:26 +0100116#define OMAP24XX_GPIO_REVISION 0x0000
117#define OMAP24XX_GPIO_SYSCONFIG 0x0010
118#define OMAP24XX_GPIO_SYSSTATUS 0x0014
119#define OMAP24XX_GPIO_IRQSTATUS1 0x0018
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300120#define OMAP24XX_GPIO_IRQSTATUS2 0x0028
121#define OMAP24XX_GPIO_IRQENABLE2 0x002c
Tony Lindgren92105bb2005-09-07 17:20:26 +0100122#define OMAP24XX_GPIO_IRQENABLE1 0x001c
Tero Kristo723fdb72008-11-26 14:35:16 -0800123#define OMAP24XX_GPIO_WAKE_EN 0x0020
Tony Lindgren92105bb2005-09-07 17:20:26 +0100124#define OMAP24XX_GPIO_CTRL 0x0030
125#define OMAP24XX_GPIO_OE 0x0034
126#define OMAP24XX_GPIO_DATAIN 0x0038
127#define OMAP24XX_GPIO_DATAOUT 0x003c
128#define OMAP24XX_GPIO_LEVELDETECT0 0x0040
129#define OMAP24XX_GPIO_LEVELDETECT1 0x0044
130#define OMAP24XX_GPIO_RISINGDETECT 0x0048
131#define OMAP24XX_GPIO_FALLINGDETECT 0x004c
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700132#define OMAP24XX_GPIO_DEBOUNCE_EN 0x0050
133#define OMAP24XX_GPIO_DEBOUNCE_VAL 0x0054
Tony Lindgren92105bb2005-09-07 17:20:26 +0100134#define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
135#define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
136#define OMAP24XX_GPIO_CLEARWKUENA 0x0080
137#define OMAP24XX_GPIO_SETWKUENA 0x0084
138#define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
139#define OMAP24XX_GPIO_SETDATAOUT 0x0094
140
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800141/*
142 * omap34xx specific GPIO registers
143 */
144
Russell King7c7095a2008-09-05 15:49:14 +0100145#define OMAP34XX_GPIO1_BASE IO_ADDRESS(0x48310000)
146#define OMAP34XX_GPIO2_BASE IO_ADDRESS(0x49050000)
147#define OMAP34XX_GPIO3_BASE IO_ADDRESS(0x49052000)
148#define OMAP34XX_GPIO4_BASE IO_ADDRESS(0x49054000)
149#define OMAP34XX_GPIO5_BASE IO_ADDRESS(0x49056000)
150#define OMAP34XX_GPIO6_BASE IO_ADDRESS(0x49058000)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800151
Santosh Shilimkar44169072009-05-28 14:16:04 -0700152/*
153 * OMAP44XX specific GPIO registers
154 */
155#define OMAP44XX_GPIO1_BASE IO_ADDRESS(0x4a310000)
156#define OMAP44XX_GPIO2_BASE IO_ADDRESS(0x48055000)
157#define OMAP44XX_GPIO3_BASE IO_ADDRESS(0x48057000)
158#define OMAP44XX_GPIO4_BASE IO_ADDRESS(0x48059000)
159#define OMAP44XX_GPIO5_BASE IO_ADDRESS(0x4805B000)
160#define OMAP44XX_GPIO6_BASE IO_ADDRESS(0x4805D000)
161
Russell King7c7095a2008-09-05 15:49:14 +0100162#define OMAP_MPUIO_VBASE IO_ADDRESS(OMAP_MPUIO_BASE)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800163
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100164struct gpio_bank {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100165 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100166 u16 irq;
167 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100168 int method;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700169#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || \
170 defined(CONFIG_ARCH_OMAP34XX) || defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100171 u32 suspend_wakeup;
172 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800173#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -0700174#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
175 defined(CONFIG_ARCH_OMAP4)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800176 u32 non_wakeup_gpios;
177 u32 enabled_non_wakeup_gpios;
178
179 u32 saved_datain;
180 u32 saved_fallingdetect;
181 u32 saved_risingdetect;
182#endif
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800183 u32 level_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100184 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -0800185 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -0800186 struct clk *dbck;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100187};
188
189#define METHOD_MPUIO 0
190#define METHOD_GPIO_1510 1
191#define METHOD_GPIO_1610 2
192#define METHOD_GPIO_730 3
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700193#define METHOD_GPIO_850 4
194#define METHOD_GPIO_24XX 5
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100195
Tony Lindgren92105bb2005-09-07 17:20:26 +0100196#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100197static struct gpio_bank gpio_bank_1610[5] = {
Russell King7c7095a2008-09-05 15:49:14 +0100198 { OMAP_MPUIO_VBASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100199 { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
200 { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
201 { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
202 { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
203};
204#endif
205
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000206#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100207static struct gpio_bank gpio_bank_1510[2] = {
Russell King7c7095a2008-09-05 15:49:14 +0100208 { OMAP_MPUIO_VBASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100209 { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
210};
211#endif
212
213#ifdef CONFIG_ARCH_OMAP730
214static struct gpio_bank gpio_bank_730[7] = {
Russell King7c7095a2008-09-05 15:49:14 +0100215 { OMAP_MPUIO_VBASE, INT_730_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100216 { OMAP730_GPIO1_BASE, INT_730_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_730 },
217 { OMAP730_GPIO2_BASE, INT_730_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_730 },
218 { OMAP730_GPIO3_BASE, INT_730_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_730 },
219 { OMAP730_GPIO4_BASE, INT_730_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_730 },
220 { OMAP730_GPIO5_BASE, INT_730_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_730 },
221 { OMAP730_GPIO6_BASE, INT_730_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_730 },
222};
223#endif
224
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700225#ifdef CONFIG_ARCH_OMAP850
226static struct gpio_bank gpio_bank_850[7] = {
227 { OMAP_MPUIO_BASE, INT_850_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
228 { OMAP850_GPIO1_BASE, INT_850_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_850 },
229 { OMAP850_GPIO2_BASE, INT_850_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_850 },
230 { OMAP850_GPIO3_BASE, INT_850_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_850 },
231 { OMAP850_GPIO4_BASE, INT_850_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_850 },
232 { OMAP850_GPIO5_BASE, INT_850_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_850 },
233 { OMAP850_GPIO6_BASE, INT_850_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_850 },
234};
235#endif
236
237
Tony Lindgren92105bb2005-09-07 17:20:26 +0100238#ifdef CONFIG_ARCH_OMAP24XX
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800239
240static struct gpio_bank gpio_bank_242x[4] = {
241 { OMAP242X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
242 { OMAP242X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
243 { OMAP242X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
244 { OMAP242X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
Tony Lindgren92105bb2005-09-07 17:20:26 +0100245};
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800246
247static struct gpio_bank gpio_bank_243x[5] = {
248 { OMAP243X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
249 { OMAP243X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
250 { OMAP243X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
251 { OMAP243X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
252 { OMAP243X_GPIO5_BASE, INT_24XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
253};
254
Tony Lindgren92105bb2005-09-07 17:20:26 +0100255#endif
256
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800257#ifdef CONFIG_ARCH_OMAP34XX
258static struct gpio_bank gpio_bank_34xx[6] = {
259 { OMAP34XX_GPIO1_BASE, INT_34XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
260 { OMAP34XX_GPIO2_BASE, INT_34XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
261 { OMAP34XX_GPIO3_BASE, INT_34XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
262 { OMAP34XX_GPIO4_BASE, INT_34XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
263 { OMAP34XX_GPIO5_BASE, INT_34XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
264 { OMAP34XX_GPIO6_BASE, INT_34XX_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_24XX },
265};
266
267#endif
268
Santosh Shilimkar44169072009-05-28 14:16:04 -0700269#ifdef CONFIG_ARCH_OMAP4
270static struct gpio_bank gpio_bank_44xx[6] = {
271 { OMAP44XX_GPIO1_BASE, INT_44XX_GPIO_BANK1, IH_GPIO_BASE, \
272 METHOD_GPIO_24XX },
273 { OMAP44XX_GPIO2_BASE, INT_44XX_GPIO_BANK2, IH_GPIO_BASE + 32, \
274 METHOD_GPIO_24XX },
275 { OMAP44XX_GPIO3_BASE, INT_44XX_GPIO_BANK3, IH_GPIO_BASE + 64, \
276 METHOD_GPIO_24XX },
277 { OMAP44XX_GPIO4_BASE, INT_44XX_GPIO_BANK4, IH_GPIO_BASE + 96, \
278 METHOD_GPIO_24XX },
279 { OMAP44XX_GPIO5_BASE, INT_44XX_GPIO_BANK5, IH_GPIO_BASE + 128, \
280 METHOD_GPIO_24XX },
281 { OMAP44XX_GPIO6_BASE, INT_44XX_GPIO_BANK6, IH_GPIO_BASE + 160, \
282 METHOD_GPIO_24XX },
283};
284
285#endif
286
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100287static struct gpio_bank *gpio_bank;
288static int gpio_bank_count;
289
290static inline struct gpio_bank *get_gpio_bank(int gpio)
291{
Tony Lindgren6e60e792006-04-02 17:46:23 +0100292 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100293 if (OMAP_GPIO_IS_MPUIO(gpio))
294 return &gpio_bank[0];
295 return &gpio_bank[1];
296 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100297 if (cpu_is_omap16xx()) {
298 if (OMAP_GPIO_IS_MPUIO(gpio))
299 return &gpio_bank[0];
300 return &gpio_bank[1 + (gpio >> 4)];
301 }
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700302 if (cpu_is_omap7xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100303 if (OMAP_GPIO_IS_MPUIO(gpio))
304 return &gpio_bank[0];
305 return &gpio_bank[1 + (gpio >> 5)];
306 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100307 if (cpu_is_omap24xx())
308 return &gpio_bank[gpio >> 5];
Santosh Shilimkar44169072009-05-28 14:16:04 -0700309 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800310 return &gpio_bank[gpio >> 5];
David Brownelle031ab22008-12-10 17:35:27 -0800311 BUG();
312 return NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100313}
314
315static inline int get_gpio_index(int gpio)
316{
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700317 if (cpu_is_omap7xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100318 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100319 if (cpu_is_omap24xx())
320 return gpio & 0x1f;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700321 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800322 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100323 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100324}
325
326static inline int gpio_valid(int gpio)
327{
328 if (gpio < 0)
329 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -0800330 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300331 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100332 return -1;
333 return 0;
334 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100335 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100336 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100337 if ((cpu_is_omap16xx()) && gpio < 64)
338 return 0;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700339 if (cpu_is_omap7xx() && gpio < 192)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100341 if (cpu_is_omap24xx() && gpio < 128)
342 return 0;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700343 if ((cpu_is_omap34xx() || cpu_is_omap44xx()) && gpio < 192)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800344 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100345 return -1;
346}
347
348static int check_gpio(int gpio)
349{
350 if (unlikely(gpio_valid(gpio)) < 0) {
351 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
352 dump_stack();
353 return -1;
354 }
355 return 0;
356}
357
358static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
359{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100360 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100361 u32 l;
362
363 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800364#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100365 case METHOD_MPUIO:
366 reg += OMAP_MPUIO_IO_CNTL;
367 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800368#endif
369#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100370 case METHOD_GPIO_1510:
371 reg += OMAP1510_GPIO_DIR_CONTROL;
372 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800373#endif
374#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100375 case METHOD_GPIO_1610:
376 reg += OMAP1610_GPIO_DIRECTION;
377 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800378#endif
379#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100380 case METHOD_GPIO_730:
381 reg += OMAP730_GPIO_DIR_CONTROL;
382 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800383#endif
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700384#ifdef CONFIG_ARCH_OMAP850
385 case METHOD_GPIO_850:
386 reg += OMAP850_GPIO_DIR_CONTROL;
387 break;
388#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -0700389#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
390 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100391 case METHOD_GPIO_24XX:
392 reg += OMAP24XX_GPIO_OE;
393 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800394#endif
395 default:
396 WARN_ON(1);
397 return;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100398 }
399 l = __raw_readl(reg);
400 if (is_input)
401 l |= 1 << gpio;
402 else
403 l &= ~(1 << gpio);
404 __raw_writel(l, reg);
405}
406
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100407static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
408{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100409 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100410 u32 l = 0;
411
412 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800413#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100414 case METHOD_MPUIO:
415 reg += OMAP_MPUIO_OUTPUT;
416 l = __raw_readl(reg);
417 if (enable)
418 l |= 1 << gpio;
419 else
420 l &= ~(1 << gpio);
421 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800422#endif
423#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100424 case METHOD_GPIO_1510:
425 reg += OMAP1510_GPIO_DATA_OUTPUT;
426 l = __raw_readl(reg);
427 if (enable)
428 l |= 1 << gpio;
429 else
430 l &= ~(1 << gpio);
431 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800432#endif
433#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100434 case METHOD_GPIO_1610:
435 if (enable)
436 reg += OMAP1610_GPIO_SET_DATAOUT;
437 else
438 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
439 l = 1 << gpio;
440 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800441#endif
442#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100443 case METHOD_GPIO_730:
444 reg += OMAP730_GPIO_DATA_OUTPUT;
445 l = __raw_readl(reg);
446 if (enable)
447 l |= 1 << gpio;
448 else
449 l &= ~(1 << gpio);
450 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800451#endif
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700452#ifdef CONFIG_ARCH_OMAP850
453 case METHOD_GPIO_850:
454 reg += OMAP850_GPIO_DATA_OUTPUT;
455 l = __raw_readl(reg);
456 if (enable)
457 l |= 1 << gpio;
458 else
459 l &= ~(1 << gpio);
460 break;
461#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -0700462#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
463 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100464 case METHOD_GPIO_24XX:
465 if (enable)
466 reg += OMAP24XX_GPIO_SETDATAOUT;
467 else
468 reg += OMAP24XX_GPIO_CLEARDATAOUT;
469 l = 1 << gpio;
470 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800471#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100472 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800473 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100474 return;
475 }
476 __raw_writel(l, reg);
477}
478
David Brownell0b84b5c2008-12-10 17:35:25 -0800479static int __omap_get_gpio_datain(int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100480{
481 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100482 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100483
484 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800485 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100486 bank = get_gpio_bank(gpio);
487 reg = bank->base;
488 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800489#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100490 case METHOD_MPUIO:
491 reg += OMAP_MPUIO_INPUT_LATCH;
492 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800493#endif
494#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100495 case METHOD_GPIO_1510:
496 reg += OMAP1510_GPIO_DATA_INPUT;
497 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800498#endif
499#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100500 case METHOD_GPIO_1610:
501 reg += OMAP1610_GPIO_DATAIN;
502 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800503#endif
504#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100505 case METHOD_GPIO_730:
506 reg += OMAP730_GPIO_DATA_INPUT;
507 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800508#endif
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700509#ifdef CONFIG_ARCH_OMAP850
510 case METHOD_GPIO_850:
511 reg += OMAP850_GPIO_DATA_INPUT;
512 break;
513#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -0700514#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
515 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100516 case METHOD_GPIO_24XX:
517 reg += OMAP24XX_GPIO_DATAIN;
518 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800519#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100520 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800521 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100522 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100523 return (__raw_readl(reg)
524 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100525}
526
Tony Lindgren92105bb2005-09-07 17:20:26 +0100527#define MOD_REG_BIT(reg, bit_mask, set) \
528do { \
529 int l = __raw_readl(base + reg); \
530 if (set) l |= bit_mask; \
531 else l &= ~bit_mask; \
532 __raw_writel(l, base + reg); \
533} while(0)
534
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700535void omap_set_gpio_debounce(int gpio, int enable)
536{
537 struct gpio_bank *bank;
538 void __iomem *reg;
David Brownelle031ab22008-12-10 17:35:27 -0800539 unsigned long flags;
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700540 u32 val, l = 1 << get_gpio_index(gpio);
541
542 if (cpu_class_is_omap1())
543 return;
544
545 bank = get_gpio_bank(gpio);
546 reg = bank->base;
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700547 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
David Brownelle031ab22008-12-10 17:35:27 -0800548
549 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700550 val = __raw_readl(reg);
551
Jouni Hogander89db9482008-12-10 17:35:24 -0800552 if (enable && !(val & l))
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700553 val |= l;
David Brownelle031ab22008-12-10 17:35:27 -0800554 else if (!enable && (val & l))
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700555 val &= ~l;
Jouni Hogander89db9482008-12-10 17:35:24 -0800556 else
David Brownelle031ab22008-12-10 17:35:27 -0800557 goto done;
Jouni Hogander89db9482008-12-10 17:35:24 -0800558
Santosh Shilimkar44169072009-05-28 14:16:04 -0700559 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
David Brownelle031ab22008-12-10 17:35:27 -0800560 if (enable)
561 clk_enable(bank->dbck);
562 else
563 clk_disable(bank->dbck);
564 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700565
566 __raw_writel(val, reg);
David Brownelle031ab22008-12-10 17:35:27 -0800567done:
568 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700569}
570EXPORT_SYMBOL(omap_set_gpio_debounce);
571
572void omap_set_gpio_debounce_time(int gpio, int enc_time)
573{
574 struct gpio_bank *bank;
575 void __iomem *reg;
576
577 if (cpu_class_is_omap1())
578 return;
579
580 bank = get_gpio_bank(gpio);
581 reg = bank->base;
582
583 enc_time &= 0xff;
584 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
585 __raw_writel(enc_time, reg);
586}
587EXPORT_SYMBOL(omap_set_gpio_debounce_time);
588
Santosh Shilimkar44169072009-05-28 14:16:04 -0700589#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
590 defined(CONFIG_ARCH_OMAP4)
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700591static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
592 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100593{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800594 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100595 u32 gpio_bit = 1 << gpio;
596
597 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100598 trigger & IRQ_TYPE_LEVEL_LOW);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100599 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100600 trigger & IRQ_TYPE_LEVEL_HIGH);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100601 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100602 trigger & IRQ_TYPE_EDGE_RISING);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100603 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100604 trigger & IRQ_TYPE_EDGE_FALLING);
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700605
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800606 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
607 if (trigger != 0)
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700608 __raw_writel(1 << gpio, bank->base
609 + OMAP24XX_GPIO_SETWKUENA);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800610 else
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700611 __raw_writel(1 << gpio, bank->base
612 + OMAP24XX_GPIO_CLEARWKUENA);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800613 } else {
614 if (trigger != 0)
615 bank->enabled_non_wakeup_gpios |= gpio_bit;
616 else
617 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
618 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700619
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800620 bank->level_mask =
621 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
622 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100623}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800624#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100625
626static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
627{
628 void __iomem *reg = bank->base;
629 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100630
631 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800632#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100633 case METHOD_MPUIO:
634 reg += OMAP_MPUIO_GPIO_INT_EDGE;
635 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100636 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100637 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100638 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100639 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100640 else
641 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100642 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800643#endif
644#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100645 case METHOD_GPIO_1510:
646 reg += OMAP1510_GPIO_INT_CONTROL;
647 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100648 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100649 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100650 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100651 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100652 else
653 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100654 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800655#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800656#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100657 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100658 if (gpio & 0x08)
659 reg += OMAP1610_GPIO_EDGE_CTRL2;
660 else
661 reg += OMAP1610_GPIO_EDGE_CTRL1;
662 gpio &= 0x07;
663 l = __raw_readl(reg);
664 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100665 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100666 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100667 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100668 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800669 if (trigger)
670 /* Enable wake-up during idle for dynamic tick */
671 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
672 else
673 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100674 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800675#endif
676#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100677 case METHOD_GPIO_730:
678 reg += OMAP730_GPIO_INT_CONTROL;
679 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100680 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100681 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100682 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100683 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100684 else
685 goto bad;
686 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800687#endif
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700688#ifdef CONFIG_ARCH_OMAP850
689 case METHOD_GPIO_850:
690 reg += OMAP850_GPIO_INT_CONTROL;
691 l = __raw_readl(reg);
692 if (trigger & IRQ_TYPE_EDGE_RISING)
693 l |= 1 << gpio;
694 else if (trigger & IRQ_TYPE_EDGE_FALLING)
695 l &= ~(1 << gpio);
696 else
697 goto bad;
698 break;
699#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -0700700#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
701 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100702 case METHOD_GPIO_24XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800703 set_24xx_gpio_triggering(bank, gpio, trigger);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100704 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800705#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100706 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100707 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100708 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100709 __raw_writel(l, reg);
710 return 0;
711bad:
712 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100713}
714
Tony Lindgren92105bb2005-09-07 17:20:26 +0100715static int gpio_irq_type(unsigned irq, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100716{
717 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100718 unsigned gpio;
719 int retval;
David Brownella6472532008-03-03 04:33:30 -0800720 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100721
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800722 if (!cpu_class_is_omap2() && irq > IH_MPUIO_BASE)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100723 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
724 else
725 gpio = irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726
727 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100728 return -EINVAL;
729
David Brownelle5c56ed2006-12-06 17:13:59 -0800730 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100731 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800732
733 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800734 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800735 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100736 return -EINVAL;
737
David Brownell58781012006-12-06 17:14:10 -0800738 bank = get_irq_chip_data(irq);
David Brownella6472532008-03-03 04:33:30 -0800739 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100740 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
David Brownellb9772a22006-12-06 17:13:53 -0800741 if (retval == 0) {
742 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
743 irq_desc[irq].status |= type;
744 }
David Brownella6472532008-03-03 04:33:30 -0800745 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800746
747 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
748 __set_irq_handler_unlocked(irq, handle_level_irq);
749 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
750 __set_irq_handler_unlocked(irq, handle_edge_irq);
751
Tony Lindgren92105bb2005-09-07 17:20:26 +0100752 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100753}
754
755static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
756{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100757 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100758
759 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800760#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100761 case METHOD_MPUIO:
762 /* MPUIO irqstatus is reset by reading the status register,
763 * so do nothing here */
764 return;
David Brownelle5c56ed2006-12-06 17:13:59 -0800765#endif
766#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100767 case METHOD_GPIO_1510:
768 reg += OMAP1510_GPIO_INT_STATUS;
769 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800770#endif
771#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100772 case METHOD_GPIO_1610:
773 reg += OMAP1610_GPIO_IRQSTATUS1;
774 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800775#endif
776#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100777 case METHOD_GPIO_730:
778 reg += OMAP730_GPIO_INT_STATUS;
779 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800780#endif
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700781#ifdef CONFIG_ARCH_OMAP850
782 case METHOD_GPIO_850:
783 reg += OMAP850_GPIO_INT_STATUS;
784 break;
785#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -0700786#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
787 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100788 case METHOD_GPIO_24XX:
789 reg += OMAP24XX_GPIO_IRQSTATUS1;
790 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800791#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100792 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800793 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100794 return;
795 }
796 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300797
798 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800799#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Roger Quadrosbedfd152009-04-23 11:10:50 -0700800 reg = bank->base + OMAP24XX_GPIO_IRQSTATUS2;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800801 if (cpu_is_omap24xx() || cpu_is_omap34xx())
Roger Quadrosbedfd152009-04-23 11:10:50 -0700802 __raw_writel(gpio_mask, reg);
803
804 /* Flush posted write for the irq status to avoid spurious interrupts */
805 __raw_readl(reg);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800806#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100807}
808
809static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
810{
811 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
812}
813
Imre Deakea6dedd2006-06-26 16:16:00 -0700814static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
815{
816 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700817 int inv = 0;
818 u32 l;
819 u32 mask;
Imre Deakea6dedd2006-06-26 16:16:00 -0700820
821 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800822#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700823 case METHOD_MPUIO:
824 reg += OMAP_MPUIO_GPIO_MASKIT;
Imre Deak99c47702006-06-26 16:16:07 -0700825 mask = 0xffff;
826 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700827 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800828#endif
829#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700830 case METHOD_GPIO_1510:
831 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700832 mask = 0xffff;
833 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700834 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800835#endif
836#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700837 case METHOD_GPIO_1610:
838 reg += OMAP1610_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700839 mask = 0xffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700840 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800841#endif
842#ifdef CONFIG_ARCH_OMAP730
Imre Deakea6dedd2006-06-26 16:16:00 -0700843 case METHOD_GPIO_730:
844 reg += OMAP730_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700845 mask = 0xffffffff;
846 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700847 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800848#endif
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700849#ifdef CONFIG_ARCH_OMAP850
850 case METHOD_GPIO_850:
851 reg += OMAP850_GPIO_INT_MASK;
852 mask = 0xffffffff;
853 inv = 1;
854 break;
855#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -0700856#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
857 defined(CONFIG_ARCH_OMAP4)
Imre Deakea6dedd2006-06-26 16:16:00 -0700858 case METHOD_GPIO_24XX:
859 reg += OMAP24XX_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700860 mask = 0xffffffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700861 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800862#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700863 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800864 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700865 return 0;
866 }
867
Imre Deak99c47702006-06-26 16:16:07 -0700868 l = __raw_readl(reg);
869 if (inv)
870 l = ~l;
871 l &= mask;
872 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700873}
874
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100875static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
876{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100877 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100878 u32 l;
879
880 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800881#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100882 case METHOD_MPUIO:
883 reg += OMAP_MPUIO_GPIO_MASKIT;
884 l = __raw_readl(reg);
885 if (enable)
886 l &= ~(gpio_mask);
887 else
888 l |= gpio_mask;
889 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800890#endif
891#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100892 case METHOD_GPIO_1510:
893 reg += OMAP1510_GPIO_INT_MASK;
894 l = __raw_readl(reg);
895 if (enable)
896 l &= ~(gpio_mask);
897 else
898 l |= gpio_mask;
899 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800900#endif
901#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100902 case METHOD_GPIO_1610:
903 if (enable)
904 reg += OMAP1610_GPIO_SET_IRQENABLE1;
905 else
906 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
907 l = gpio_mask;
908 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800909#endif
910#ifdef CONFIG_ARCH_OMAP730
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100911 case METHOD_GPIO_730:
912 reg += OMAP730_GPIO_INT_MASK;
913 l = __raw_readl(reg);
914 if (enable)
915 l &= ~(gpio_mask);
916 else
917 l |= gpio_mask;
918 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800919#endif
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700920#ifdef CONFIG_ARCH_OMAP850
921 case METHOD_GPIO_850:
922 reg += OMAP850_GPIO_INT_MASK;
923 l = __raw_readl(reg);
924 if (enable)
925 l &= ~(gpio_mask);
926 else
927 l |= gpio_mask;
928 break;
929#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -0700930#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
931 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100932 case METHOD_GPIO_24XX:
933 if (enable)
934 reg += OMAP24XX_GPIO_SETIRQENABLE1;
935 else
936 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
937 l = gpio_mask;
938 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800939#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100940 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800941 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100942 return;
943 }
944 __raw_writel(l, reg);
945}
946
947static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
948{
949 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
950}
951
Tony Lindgren92105bb2005-09-07 17:20:26 +0100952/*
953 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
954 * 1510 does not seem to have a wake-up register. If JTAG is connected
955 * to the target, system will wake up always on GPIO events. While
956 * system is running all registered GPIO interrupts need to have wake-up
957 * enabled. When system is suspended, only selected GPIO interrupts need
958 * to have wake-up enabled.
959 */
960static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
961{
David Brownella6472532008-03-03 04:33:30 -0800962 unsigned long flags;
963
Tony Lindgren92105bb2005-09-07 17:20:26 +0100964 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800965#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800966 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100967 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800968 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700969 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100970 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700971 else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100972 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800973 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100974 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800975#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -0700976#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
977 defined(CONFIG_ARCH_OMAP4)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800978 case METHOD_GPIO_24XX:
David Brownell11a78b72006-12-06 17:14:11 -0800979 if (bank->non_wakeup_gpios & (1 << gpio)) {
980 printk(KERN_ERR "Unable to modify wakeup on "
981 "non-wakeup GPIO%d\n",
982 (bank - gpio_bank) * 32 + gpio);
983 return -EINVAL;
984 }
David Brownella6472532008-03-03 04:33:30 -0800985 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700986 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800987 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700988 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800989 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800990 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800991 return 0;
992#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100993 default:
994 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
995 bank->method);
996 return -EINVAL;
997 }
998}
999
Tony Lindgren4196dd62006-09-25 12:41:38 +03001000static void _reset_gpio(struct gpio_bank *bank, int gpio)
1001{
1002 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
1003 _set_gpio_irqenable(bank, gpio, 0);
1004 _clear_gpio_irqstatus(bank, gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +01001005 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001006}
1007
Tony Lindgren92105bb2005-09-07 17:20:26 +01001008/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
1009static int gpio_wake_enable(unsigned int irq, unsigned int enable)
1010{
1011 unsigned int gpio = irq - IH_GPIO_BASE;
1012 struct gpio_bank *bank;
1013 int retval;
1014
1015 if (check_gpio(gpio) < 0)
1016 return -ENODEV;
David Brownell58781012006-12-06 17:14:10 -08001017 bank = get_irq_chip_data(irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001018 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001019
1020 return retval;
1021}
1022
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001023static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001024{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001025 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -08001026 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001027
David Brownella6472532008-03-03 04:33:30 -08001028 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001029
Tony Lindgren4196dd62006-09-25 12:41:38 +03001030 /* Set trigger to none. You need to enable the desired trigger with
1031 * request_irq() or set_irq_type().
1032 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001033 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001034
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001035#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001036 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001037 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001038
Tony Lindgren92105bb2005-09-07 17:20:26 +01001039 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001040 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001041 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001042 }
1043#endif
David Brownella6472532008-03-03 04:33:30 -08001044 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001045
1046 return 0;
1047}
1048
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001049static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001050{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001051 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -08001052 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001053
David Brownella6472532008-03-03 04:33:30 -08001054 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001055#ifdef CONFIG_ARCH_OMAP16XX
1056 if (bank->method == METHOD_GPIO_1610) {
1057 /* Disable wake-up during idle for dynamic tick */
1058 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001059 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001060 }
1061#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -07001062#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1063 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001064 if (bank->method == METHOD_GPIO_24XX) {
1065 /* Disable wake-up during idle for dynamic tick */
1066 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001067 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001068 }
1069#endif
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001070 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -08001071 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001072}
1073
1074/*
1075 * We need to unmask the GPIO bank interrupt as soon as possible to
1076 * avoid missing GPIO interrupts for other lines in the bank.
1077 * Then we need to mask-read-clear-unmask the triggered GPIO lines
1078 * in the bank to avoid missing nested interrupts for a GPIO line.
1079 * If we wait to unmask individual GPIO lines in the bank after the
1080 * line's interrupt handler has been run, we may miss some nested
1081 * interrupts.
1082 */
Russell King10dd5ce2006-11-23 11:41:32 +00001083static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001084{
Tony Lindgren92105bb2005-09-07 17:20:26 +01001085 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001086 u32 isr;
1087 unsigned int gpio_irq;
1088 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -07001089 u32 retrigger = 0;
1090 int unmasked = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001091
1092 desc->chip->ack(irq);
1093
Thomas Gleixner418ca1f02006-07-01 22:32:41 +01001094 bank = get_irq_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -08001095#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001096 if (bank->method == METHOD_MPUIO)
1097 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
David Brownelle5c56ed2006-12-06 17:13:59 -08001098#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001099#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001100 if (bank->method == METHOD_GPIO_1510)
1101 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
1102#endif
1103#if defined(CONFIG_ARCH_OMAP16XX)
1104 if (bank->method == METHOD_GPIO_1610)
1105 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
1106#endif
1107#ifdef CONFIG_ARCH_OMAP730
1108 if (bank->method == METHOD_GPIO_730)
1109 isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
1110#endif
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001111#ifdef CONFIG_ARCH_OMAP850
1112 if (bank->method == METHOD_GPIO_850)
1113 isr_reg = bank->base + OMAP850_GPIO_INT_STATUS;
1114#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -07001115#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1116 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001117 if (bank->method == METHOD_GPIO_24XX)
1118 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1119#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001120 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001121 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -07001122 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001123
Imre Deakea6dedd2006-06-26 16:16:00 -07001124 enabled = _get_gpio_irqbank_mask(bank);
1125 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001126
1127 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1128 isr &= 0x0000ffff;
1129
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001130 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001131 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -07001132 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001133
1134 /* clear edge sensitive interrupts before handler(s) are
1135 called so that we don't miss any interrupt occurred while
1136 executing them */
1137 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1138 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1139 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1140
1141 /* if there is only edge sensitive GPIO pin interrupts
1142 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -07001143 if (!level_mask && !unmasked) {
1144 unmasked = 1;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001145 desc->chip->unmask(irq);
Imre Deakea6dedd2006-06-26 16:16:00 -07001146 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001147
Imre Deakea6dedd2006-06-26 16:16:00 -07001148 isr |= retrigger;
1149 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001150 if (!isr)
1151 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001152
Tony Lindgren92105bb2005-09-07 17:20:26 +01001153 gpio_irq = bank->virtual_irq_start;
1154 for (; isr != 0; isr >>= 1, gpio_irq++) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001155 if (!(isr & 1))
1156 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001157
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +01001158 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001159 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001160 }
Imre Deakea6dedd2006-06-26 16:16:00 -07001161 /* if bank has any level sensitive GPIO pin interrupt
1162 configured, we must unmask the bank interrupt only after
1163 handler(s) are executed in order to avoid spurious bank
1164 interrupt */
1165 if (!unmasked)
1166 desc->chip->unmask(irq);
1167
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001168}
1169
Tony Lindgren4196dd62006-09-25 12:41:38 +03001170static void gpio_irq_shutdown(unsigned int irq)
1171{
1172 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001173 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001174
1175 _reset_gpio(bank, gpio);
1176}
1177
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001178static void gpio_ack_irq(unsigned int irq)
1179{
1180 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001181 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001182
1183 _clear_gpio_irqstatus(bank, gpio);
1184}
1185
1186static void gpio_mask_irq(unsigned int irq)
1187{
1188 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001189 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001190
1191 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman55b60192009-06-04 15:57:10 -07001192 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001193}
1194
1195static void gpio_unmask_irq(unsigned int irq)
1196{
1197 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001198 struct gpio_bank *bank = get_irq_chip_data(irq);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001199 unsigned int irq_mask = 1 << get_gpio_index(gpio);
Kevin Hilman55b60192009-06-04 15:57:10 -07001200 struct irq_desc *desc = irq_to_desc(irq);
1201 u32 trigger = desc->status & IRQ_TYPE_SENSE_MASK;
1202
1203 if (trigger)
1204 _set_gpio_triggering(bank, get_gpio_index(gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001205
1206 /* For level-triggered GPIOs, the clearing must be done after
1207 * the HW source is cleared, thus after the handler has run */
1208 if (bank->level_mask & irq_mask) {
1209 _set_gpio_irqenable(bank, gpio, 0);
1210 _clear_gpio_irqstatus(bank, gpio);
1211 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001212
Kevin Hilman4de8c752008-01-16 21:56:14 -08001213 _set_gpio_irqenable(bank, gpio, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001214}
1215
David Brownelle5c56ed2006-12-06 17:13:59 -08001216static struct irq_chip gpio_irq_chip = {
1217 .name = "GPIO",
1218 .shutdown = gpio_irq_shutdown,
1219 .ack = gpio_ack_irq,
1220 .mask = gpio_mask_irq,
1221 .unmask = gpio_unmask_irq,
1222 .set_type = gpio_irq_type,
1223 .set_wake = gpio_wake_enable,
1224};
1225
1226/*---------------------------------------------------------------------*/
1227
1228#ifdef CONFIG_ARCH_OMAP1
1229
1230/* MPUIO uses the always-on 32k clock */
1231
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001232static void mpuio_ack_irq(unsigned int irq)
1233{
1234 /* The ISR is reset automatically, so do nothing here. */
1235}
1236
1237static void mpuio_mask_irq(unsigned int irq)
1238{
1239 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001240 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001241
1242 _set_gpio_irqenable(bank, gpio, 0);
1243}
1244
1245static void mpuio_unmask_irq(unsigned int irq)
1246{
1247 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001248 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001249
1250 _set_gpio_irqenable(bank, gpio, 1);
1251}
1252
David Brownelle5c56ed2006-12-06 17:13:59 -08001253static struct irq_chip mpuio_irq_chip = {
1254 .name = "MPUIO",
1255 .ack = mpuio_ack_irq,
1256 .mask = mpuio_mask_irq,
1257 .unmask = mpuio_unmask_irq,
Tony Lindgren92105bb2005-09-07 17:20:26 +01001258 .set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001259#ifdef CONFIG_ARCH_OMAP16XX
1260 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1261 .set_wake = gpio_wake_enable,
1262#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001263};
1264
David Brownelle5c56ed2006-12-06 17:13:59 -08001265
1266#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1267
David Brownell11a78b72006-12-06 17:14:11 -08001268
1269#ifdef CONFIG_ARCH_OMAP16XX
1270
1271#include <linux/platform_device.h>
1272
1273static int omap_mpuio_suspend_late(struct platform_device *pdev, pm_message_t mesg)
1274{
1275 struct gpio_bank *bank = platform_get_drvdata(pdev);
1276 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001277 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001278
David Brownella6472532008-03-03 04:33:30 -08001279 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001280 bank->saved_wakeup = __raw_readl(mask_reg);
1281 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001282 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001283
1284 return 0;
1285}
1286
1287static int omap_mpuio_resume_early(struct platform_device *pdev)
1288{
1289 struct gpio_bank *bank = platform_get_drvdata(pdev);
1290 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001291 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001292
David Brownella6472532008-03-03 04:33:30 -08001293 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001294 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001295 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001296
1297 return 0;
1298}
1299
1300/* use platform_driver for this, now that there's no longer any
1301 * point to sys_device (other than not disturbing old code).
1302 */
1303static struct platform_driver omap_mpuio_driver = {
1304 .suspend_late = omap_mpuio_suspend_late,
1305 .resume_early = omap_mpuio_resume_early,
1306 .driver = {
1307 .name = "mpuio",
1308 },
1309};
1310
1311static struct platform_device omap_mpuio_device = {
1312 .name = "mpuio",
1313 .id = -1,
1314 .dev = {
1315 .driver = &omap_mpuio_driver.driver,
1316 }
1317 /* could list the /proc/iomem resources */
1318};
1319
1320static inline void mpuio_init(void)
1321{
David Brownellfcf126d2007-04-02 12:46:47 -07001322 platform_set_drvdata(&omap_mpuio_device, &gpio_bank_1610[0]);
1323
David Brownell11a78b72006-12-06 17:14:11 -08001324 if (platform_driver_register(&omap_mpuio_driver) == 0)
1325 (void) platform_device_register(&omap_mpuio_device);
1326}
1327
1328#else
1329static inline void mpuio_init(void) {}
1330#endif /* 16xx */
1331
David Brownelle5c56ed2006-12-06 17:13:59 -08001332#else
1333
1334extern struct irq_chip mpuio_irq_chip;
1335
1336#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001337static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001338
1339#endif
1340
1341/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001342
David Brownell52e31342008-03-03 12:43:23 -08001343/* REVISIT these are stupid implementations! replace by ones that
1344 * don't switch on METHOD_* and which mostly avoid spinlocks
1345 */
1346
1347static int gpio_input(struct gpio_chip *chip, unsigned offset)
1348{
1349 struct gpio_bank *bank;
1350 unsigned long flags;
1351
1352 bank = container_of(chip, struct gpio_bank, chip);
1353 spin_lock_irqsave(&bank->lock, flags);
1354 _set_gpio_direction(bank, offset, 1);
1355 spin_unlock_irqrestore(&bank->lock, flags);
1356 return 0;
1357}
1358
1359static int gpio_get(struct gpio_chip *chip, unsigned offset)
1360{
David Brownell0b84b5c2008-12-10 17:35:25 -08001361 return __omap_get_gpio_datain(chip->base + offset);
David Brownell52e31342008-03-03 12:43:23 -08001362}
1363
1364static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1365{
1366 struct gpio_bank *bank;
1367 unsigned long flags;
1368
1369 bank = container_of(chip, struct gpio_bank, chip);
1370 spin_lock_irqsave(&bank->lock, flags);
1371 _set_gpio_dataout(bank, offset, value);
1372 _set_gpio_direction(bank, offset, 0);
1373 spin_unlock_irqrestore(&bank->lock, flags);
1374 return 0;
1375}
1376
1377static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1378{
1379 struct gpio_bank *bank;
1380 unsigned long flags;
1381
1382 bank = container_of(chip, struct gpio_bank, chip);
1383 spin_lock_irqsave(&bank->lock, flags);
1384 _set_gpio_dataout(bank, offset, value);
1385 spin_unlock_irqrestore(&bank->lock, flags);
1386}
1387
David Brownella007b702008-12-10 17:35:25 -08001388static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1389{
1390 struct gpio_bank *bank;
1391
1392 bank = container_of(chip, struct gpio_bank, chip);
1393 return bank->virtual_irq_start + offset;
1394}
1395
David Brownell52e31342008-03-03 12:43:23 -08001396/*---------------------------------------------------------------------*/
1397
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001398static int initialized;
Santosh Shilimkar44169072009-05-28 14:16:04 -07001399#if !(defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001400static struct clk * gpio_ick;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001401#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001402
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001403#if defined(CONFIG_ARCH_OMAP2)
1404static struct clk * gpio_fck;
1405#endif
1406
1407#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001408static struct clk * gpio5_ick;
1409static struct clk * gpio5_fck;
1410#endif
1411
Santosh Shilimkar44169072009-05-28 14:16:04 -07001412#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001413static struct clk *gpio_iclks[OMAP34XX_NR_GPIOS];
1414#endif
1415
David Brownell8ba55c52008-02-26 11:10:50 -08001416/* This lock class tells lockdep that GPIO irqs are in a different
1417 * category than their parents, so it won't report false recursion.
1418 */
1419static struct lock_class_key gpio_lock_class;
1420
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001421static int __init _omap_gpio_init(void)
1422{
1423 int i;
David Brownell52e31342008-03-03 12:43:23 -08001424 int gpio = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001425 struct gpio_bank *bank;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001426 char clk_name[11];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001427
1428 initialized = 1;
1429
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001430#if defined(CONFIG_ARCH_OMAP1)
Tony Lindgren6e60e792006-04-02 17:46:23 +01001431 if (cpu_is_omap15xx()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001432 gpio_ick = clk_get(NULL, "arm_gpio_ck");
1433 if (IS_ERR(gpio_ick))
Tony Lindgren92105bb2005-09-07 17:20:26 +01001434 printk("Could not get arm_gpio_ck\n");
1435 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001436 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001437 }
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001438#endif
1439#if defined(CONFIG_ARCH_OMAP2)
1440 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001441 gpio_ick = clk_get(NULL, "gpios_ick");
1442 if (IS_ERR(gpio_ick))
1443 printk("Could not get gpios_ick\n");
1444 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001445 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001446 gpio_fck = clk_get(NULL, "gpios_fck");
Komal Shah1630b522006-09-25 12:51:08 +03001447 if (IS_ERR(gpio_fck))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001448 printk("Could not get gpios_fck\n");
1449 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001450 clk_enable(gpio_fck);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001451
1452 /*
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001453 * On 2430 & 3430 GPIO 5 uses CORE L4 ICLK
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001454 */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001455#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001456 if (cpu_is_omap2430()) {
1457 gpio5_ick = clk_get(NULL, "gpio5_ick");
1458 if (IS_ERR(gpio5_ick))
1459 printk("Could not get gpio5_ick\n");
1460 else
1461 clk_enable(gpio5_ick);
1462 gpio5_fck = clk_get(NULL, "gpio5_fck");
1463 if (IS_ERR(gpio5_fck))
1464 printk("Could not get gpio5_fck\n");
1465 else
1466 clk_enable(gpio5_fck);
1467 }
1468#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001469 }
1470#endif
1471
Santosh Shilimkar44169072009-05-28 14:16:04 -07001472#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
1473 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001474 for (i = 0; i < OMAP34XX_NR_GPIOS; i++) {
1475 sprintf(clk_name, "gpio%d_ick", i + 1);
1476 gpio_iclks[i] = clk_get(NULL, clk_name);
1477 if (IS_ERR(gpio_iclks[i]))
1478 printk(KERN_ERR "Could not get %s\n", clk_name);
1479 else
1480 clk_enable(gpio_iclks[i]);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001481 }
1482 }
1483#endif
1484
Tony Lindgren92105bb2005-09-07 17:20:26 +01001485
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001486#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +01001487 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001488 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
1489 gpio_bank_count = 2;
1490 gpio_bank = gpio_bank_1510;
1491 }
1492#endif
1493#if defined(CONFIG_ARCH_OMAP16XX)
1494 if (cpu_is_omap16xx()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001495 u32 rev;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001496
1497 gpio_bank_count = 5;
1498 gpio_bank = gpio_bank_1610;
Russell King7c7095a2008-09-05 15:49:14 +01001499 rev = __raw_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001500 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1501 (rev >> 4) & 0x0f, rev & 0x0f);
1502 }
1503#endif
1504#ifdef CONFIG_ARCH_OMAP730
1505 if (cpu_is_omap730()) {
1506 printk(KERN_INFO "OMAP730 GPIO hardware\n");
1507 gpio_bank_count = 7;
1508 gpio_bank = gpio_bank_730;
1509 }
1510#endif
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001511#ifdef CONFIG_ARCH_OMAP850
1512 if (cpu_is_omap850()) {
1513 printk(KERN_INFO "OMAP850 GPIO hardware\n");
1514 gpio_bank_count = 7;
1515 gpio_bank = gpio_bank_850;
1516 }
1517#endif
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001518
Tony Lindgren92105bb2005-09-07 17:20:26 +01001519#ifdef CONFIG_ARCH_OMAP24XX
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001520 if (cpu_is_omap242x()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001521 int rev;
1522
1523 gpio_bank_count = 4;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001524 gpio_bank = gpio_bank_242x;
Russell King7c7095a2008-09-05 15:49:14 +01001525 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001526 printk(KERN_INFO "OMAP242x GPIO hardware version %d.%d\n",
1527 (rev >> 4) & 0x0f, rev & 0x0f);
1528 }
1529 if (cpu_is_omap243x()) {
1530 int rev;
1531
1532 gpio_bank_count = 5;
1533 gpio_bank = gpio_bank_243x;
Russell King7c7095a2008-09-05 15:49:14 +01001534 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001535 printk(KERN_INFO "OMAP243x GPIO hardware version %d.%d\n",
Tony Lindgren92105bb2005-09-07 17:20:26 +01001536 (rev >> 4) & 0x0f, rev & 0x0f);
1537 }
1538#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001539#ifdef CONFIG_ARCH_OMAP34XX
1540 if (cpu_is_omap34xx()) {
1541 int rev;
1542
1543 gpio_bank_count = OMAP34XX_NR_GPIOS;
1544 gpio_bank = gpio_bank_34xx;
Russell King7c7095a2008-09-05 15:49:14 +01001545 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001546 printk(KERN_INFO "OMAP34xx GPIO hardware version %d.%d\n",
1547 (rev >> 4) & 0x0f, rev & 0x0f);
1548 }
1549#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -07001550#ifdef CONFIG_ARCH_OMAP4
1551 if (cpu_is_omap44xx()) {
1552 int rev;
1553
1554 gpio_bank_count = OMAP34XX_NR_GPIOS;
1555 gpio_bank = gpio_bank_44xx;
1556 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1557 printk(KERN_INFO "OMAP44xx GPIO hardware version %d.%d\n",
1558 (rev >> 4) & 0x0f, rev & 0x0f);
1559 }
1560#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001561 for (i = 0; i < gpio_bank_count; i++) {
1562 int j, gpio_count = 16;
1563
1564 bank = &gpio_bank[i];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001565 spin_lock_init(&bank->lock);
David Brownelle5c56ed2006-12-06 17:13:59 -08001566 if (bank_is_mpuio(bank))
Russell King7c7095a2008-09-05 15:49:14 +01001567 __raw_writew(0xffff, bank->base + OMAP_MPUIO_GPIO_MASKIT);
Tony Lindgrend11ac972008-01-12 15:35:04 -08001568 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001569 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
1570 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
1571 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001572 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001573 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
1574 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001575 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001576 }
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001577 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_730) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001578 __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
1579 __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
1580
1581 gpio_count = 32; /* 730 has 32-bit GPIOs */
1582 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001583
Santosh Shilimkar44169072009-05-28 14:16:04 -07001584#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1585 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001586 if (bank->method == METHOD_GPIO_24XX) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001587 static const u32 non_wakeup_gpios[] = {
1588 0xe203ffc0, 0x08700040
1589 };
1590
Tony Lindgren92105bb2005-09-07 17:20:26 +01001591 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
1592 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001593 __raw_writew(0x0015, bank->base + OMAP24XX_GPIO_SYSCONFIG);
janboecb5793d2009-06-23 13:30:25 +03001594 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_DEBOUNCE_EN);
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001595
1596 /* Initialize interface clock ungated, module enabled */
1597 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001598 if (i < ARRAY_SIZE(non_wakeup_gpios))
1599 bank->non_wakeup_gpios = non_wakeup_gpios[i];
Tony Lindgren92105bb2005-09-07 17:20:26 +01001600 gpio_count = 32;
1601 }
1602#endif
David Brownell52e31342008-03-03 12:43:23 -08001603
1604 /* REVISIT eventually switch from OMAP-specific gpio structs
1605 * over to the generic ones
1606 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001607 bank->chip.request = omap_gpio_request;
1608 bank->chip.free = omap_gpio_free;
David Brownell52e31342008-03-03 12:43:23 -08001609 bank->chip.direction_input = gpio_input;
1610 bank->chip.get = gpio_get;
1611 bank->chip.direction_output = gpio_output;
1612 bank->chip.set = gpio_set;
David Brownella007b702008-12-10 17:35:25 -08001613 bank->chip.to_irq = gpio_2irq;
David Brownell52e31342008-03-03 12:43:23 -08001614 if (bank_is_mpuio(bank)) {
1615 bank->chip.label = "mpuio";
Russell King69114a42008-09-03 10:15:26 +01001616#ifdef CONFIG_ARCH_OMAP16XX
David Brownelld8f388d82008-07-25 01:46:07 -07001617 bank->chip.dev = &omap_mpuio_device.dev;
1618#endif
David Brownell52e31342008-03-03 12:43:23 -08001619 bank->chip.base = OMAP_MPUIO(0);
1620 } else {
1621 bank->chip.label = "gpio";
1622 bank->chip.base = gpio;
1623 gpio += gpio_count;
1624 }
1625 bank->chip.ngpio = gpio_count;
1626
1627 gpiochip_add(&bank->chip);
1628
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001629 for (j = bank->virtual_irq_start;
1630 j < bank->virtual_irq_start + gpio_count; j++) {
David Brownell8ba55c52008-02-26 11:10:50 -08001631 lockdep_set_class(&irq_desc[j].lock, &gpio_lock_class);
David Brownell58781012006-12-06 17:14:10 -08001632 set_irq_chip_data(j, bank);
David Brownelle5c56ed2006-12-06 17:13:59 -08001633 if (bank_is_mpuio(bank))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001634 set_irq_chip(j, &mpuio_irq_chip);
1635 else
1636 set_irq_chip(j, &gpio_irq_chip);
Russell King10dd5ce2006-11-23 11:41:32 +00001637 set_irq_handler(j, handle_simple_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001638 set_irq_flags(j, IRQF_VALID);
1639 }
1640 set_irq_chained_handler(bank->irq, gpio_irq_handler);
1641 set_irq_data(bank->irq, bank);
Jouni Hogander89db9482008-12-10 17:35:24 -08001642
Santosh Shilimkar44169072009-05-28 14:16:04 -07001643 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Jouni Hogander89db9482008-12-10 17:35:24 -08001644 sprintf(clk_name, "gpio%d_dbck", i + 1);
1645 bank->dbck = clk_get(NULL, clk_name);
1646 if (IS_ERR(bank->dbck))
1647 printk(KERN_ERR "Could not get %s\n", clk_name);
1648 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001649 }
1650
1651 /* Enable system clock for GPIO module.
1652 * The CAM_CLK_CTRL *is* really the right place. */
Tony Lindgren92105bb2005-09-07 17:20:26 +01001653 if (cpu_is_omap16xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001654 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
1655
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001656 /* Enable autoidle for the OCP interface */
1657 if (cpu_is_omap24xx())
1658 omap_writel(1 << 0, 0x48019010);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001659 if (cpu_is_omap34xx())
1660 omap_writel(1 << 0, 0x48306814);
Tony Lindgrend11ac972008-01-12 15:35:04 -08001661
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001662 return 0;
1663}
1664
Santosh Shilimkar44169072009-05-28 14:16:04 -07001665#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || \
1666 defined(CONFIG_ARCH_OMAP34XX) || defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001667static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
1668{
1669 int i;
1670
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001671 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001672 return 0;
1673
1674 for (i = 0; i < gpio_bank_count; i++) {
1675 struct gpio_bank *bank = &gpio_bank[i];
1676 void __iomem *wake_status;
1677 void __iomem *wake_clear;
1678 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001679 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001680
1681 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001682#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001683 case METHOD_GPIO_1610:
1684 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1685 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1686 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1687 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001688#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -07001689#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1690 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001691 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001692 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001693 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1694 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1695 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001696#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001697 default:
1698 continue;
1699 }
1700
David Brownella6472532008-03-03 04:33:30 -08001701 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001702 bank->saved_wakeup = __raw_readl(wake_status);
1703 __raw_writel(0xffffffff, wake_clear);
1704 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001705 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001706 }
1707
1708 return 0;
1709}
1710
1711static int omap_gpio_resume(struct sys_device *dev)
1712{
1713 int i;
1714
Tero Kristo723fdb72008-11-26 14:35:16 -08001715 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001716 return 0;
1717
1718 for (i = 0; i < gpio_bank_count; i++) {
1719 struct gpio_bank *bank = &gpio_bank[i];
1720 void __iomem *wake_clear;
1721 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001722 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001723
1724 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001725#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001726 case METHOD_GPIO_1610:
1727 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1728 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1729 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001730#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -07001731#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1732 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001733 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001734 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1735 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001736 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001737#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001738 default:
1739 continue;
1740 }
1741
David Brownella6472532008-03-03 04:33:30 -08001742 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001743 __raw_writel(0xffffffff, wake_clear);
1744 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001745 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001746 }
1747
1748 return 0;
1749}
1750
1751static struct sysdev_class omap_gpio_sysclass = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01001752 .name = "gpio",
Tony Lindgren92105bb2005-09-07 17:20:26 +01001753 .suspend = omap_gpio_suspend,
1754 .resume = omap_gpio_resume,
1755};
1756
1757static struct sys_device omap_gpio_device = {
1758 .id = 0,
1759 .cls = &omap_gpio_sysclass,
1760};
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001761
1762#endif
1763
Santosh Shilimkar44169072009-05-28 14:16:04 -07001764#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1765 defined(CONFIG_ARCH_OMAP4)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001766
1767static int workaround_enabled;
1768
1769void omap2_gpio_prepare_for_retention(void)
1770{
1771 int i, c = 0;
1772
1773 /* Remove triggering for all non-wakeup GPIOs. Otherwise spurious
1774 * IRQs will be generated. See OMAP2420 Errata item 1.101. */
1775 for (i = 0; i < gpio_bank_count; i++) {
1776 struct gpio_bank *bank = &gpio_bank[i];
1777 u32 l1, l2;
1778
1779 if (!(bank->enabled_non_wakeup_gpios))
1780 continue;
Santosh Shilimkar44169072009-05-28 14:16:04 -07001781#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1782 defined(CONFIG_ARCH_OMAP4)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001783 bank->saved_datain = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1784 l1 = __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1785 l2 = __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001786#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001787 bank->saved_fallingdetect = l1;
1788 bank->saved_risingdetect = l2;
1789 l1 &= ~bank->enabled_non_wakeup_gpios;
1790 l2 &= ~bank->enabled_non_wakeup_gpios;
Santosh Shilimkar44169072009-05-28 14:16:04 -07001791#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1792 defined(CONFIG_ARCH_OMAP4)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001793 __raw_writel(l1, bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1794 __raw_writel(l2, bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001795#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001796 c++;
1797 }
1798 if (!c) {
1799 workaround_enabled = 0;
1800 return;
1801 }
1802 workaround_enabled = 1;
1803}
1804
1805void omap2_gpio_resume_after_retention(void)
1806{
1807 int i;
1808
1809 if (!workaround_enabled)
1810 return;
1811 for (i = 0; i < gpio_bank_count; i++) {
1812 struct gpio_bank *bank = &gpio_bank[i];
1813 u32 l;
1814
1815 if (!(bank->enabled_non_wakeup_gpios))
1816 continue;
Santosh Shilimkar44169072009-05-28 14:16:04 -07001817#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1818 defined(CONFIG_ARCH_OMAP4)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001819 __raw_writel(bank->saved_fallingdetect,
1820 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1821 __raw_writel(bank->saved_risingdetect,
1822 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001823#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001824 /* Check if any of the non-wakeup interrupt GPIOs have changed
1825 * state. If so, generate an IRQ by software. This is
1826 * horribly racy, but it's the best we can do to work around
1827 * this silicon bug. */
Santosh Shilimkar44169072009-05-28 14:16:04 -07001828#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1829 defined(CONFIG_ARCH_OMAP4)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001830 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001831#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001832 l ^= bank->saved_datain;
1833 l &= bank->non_wakeup_gpios;
1834 if (l) {
1835 u32 old0, old1;
Santosh Shilimkar44169072009-05-28 14:16:04 -07001836#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1837 defined(CONFIG_ARCH_OMAP4)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001838 old0 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1839 old1 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1840 __raw_writel(old0 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1841 __raw_writel(old1 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1842 __raw_writel(old0, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1843 __raw_writel(old1, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001844#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001845 }
1846 }
1847
1848}
1849
Tony Lindgren92105bb2005-09-07 17:20:26 +01001850#endif
1851
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001852/*
1853 * This may get called early from board specific init
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001854 * for boards that have interrupts routed via FPGA.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001855 */
David Brownell277d58e2006-12-06 17:13:59 -08001856int __init omap_gpio_init(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001857{
1858 if (!initialized)
1859 return _omap_gpio_init();
1860 else
1861 return 0;
1862}
1863
Tony Lindgren92105bb2005-09-07 17:20:26 +01001864static int __init omap_gpio_sysinit(void)
1865{
1866 int ret = 0;
1867
1868 if (!initialized)
1869 ret = _omap_gpio_init();
1870
David Brownell11a78b72006-12-06 17:14:11 -08001871 mpuio_init();
1872
Santosh Shilimkar44169072009-05-28 14:16:04 -07001873#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || \
1874 defined(CONFIG_ARCH_OMAP34XX) || defined(CONFIG_ARCH_OMAP4)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001875 if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001876 if (ret == 0) {
1877 ret = sysdev_class_register(&omap_gpio_sysclass);
1878 if (ret == 0)
1879 ret = sysdev_register(&omap_gpio_device);
1880 }
1881 }
1882#endif
1883
1884 return ret;
1885}
1886
Tony Lindgren92105bb2005-09-07 17:20:26 +01001887arch_initcall(omap_gpio_sysinit);
David Brownellb9772a22006-12-06 17:13:53 -08001888
1889
1890#ifdef CONFIG_DEBUG_FS
1891
1892#include <linux/debugfs.h>
1893#include <linux/seq_file.h>
1894
1895static int gpio_is_input(struct gpio_bank *bank, int mask)
1896{
1897 void __iomem *reg = bank->base;
1898
1899 switch (bank->method) {
1900 case METHOD_MPUIO:
1901 reg += OMAP_MPUIO_IO_CNTL;
1902 break;
1903 case METHOD_GPIO_1510:
1904 reg += OMAP1510_GPIO_DIR_CONTROL;
1905 break;
1906 case METHOD_GPIO_1610:
1907 reg += OMAP1610_GPIO_DIRECTION;
1908 break;
1909 case METHOD_GPIO_730:
1910 reg += OMAP730_GPIO_DIR_CONTROL;
1911 break;
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001912 case METHOD_GPIO_850:
1913 reg += OMAP850_GPIO_DIR_CONTROL;
1914 break;
David Brownellb9772a22006-12-06 17:13:53 -08001915 case METHOD_GPIO_24XX:
1916 reg += OMAP24XX_GPIO_OE;
1917 break;
1918 }
1919 return __raw_readl(reg) & mask;
1920}
1921
1922
1923static int dbg_gpio_show(struct seq_file *s, void *unused)
1924{
1925 unsigned i, j, gpio;
1926
1927 for (i = 0, gpio = 0; i < gpio_bank_count; i++) {
1928 struct gpio_bank *bank = gpio_bank + i;
1929 unsigned bankwidth = 16;
1930 u32 mask = 1;
1931
David Brownelle5c56ed2006-12-06 17:13:59 -08001932 if (bank_is_mpuio(bank))
David Brownellb9772a22006-12-06 17:13:53 -08001933 gpio = OMAP_MPUIO(0);
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001934 else if (cpu_class_is_omap2() || cpu_is_omap730() ||
1935 cpu_is_omap850())
David Brownellb9772a22006-12-06 17:13:53 -08001936 bankwidth = 32;
1937
1938 for (j = 0; j < bankwidth; j++, gpio++, mask <<= 1) {
1939 unsigned irq, value, is_in, irqstat;
David Brownell52e31342008-03-03 12:43:23 -08001940 const char *label;
David Brownellb9772a22006-12-06 17:13:53 -08001941
David Brownell52e31342008-03-03 12:43:23 -08001942 label = gpiochip_is_requested(&bank->chip, j);
1943 if (!label)
David Brownellb9772a22006-12-06 17:13:53 -08001944 continue;
1945
1946 irq = bank->virtual_irq_start + j;
David Brownell0b84b5c2008-12-10 17:35:25 -08001947 value = gpio_get_value(gpio);
David Brownellb9772a22006-12-06 17:13:53 -08001948 is_in = gpio_is_input(bank, mask);
1949
David Brownelle5c56ed2006-12-06 17:13:59 -08001950 if (bank_is_mpuio(bank))
David Brownell52e31342008-03-03 12:43:23 -08001951 seq_printf(s, "MPUIO %2d ", j);
David Brownellb9772a22006-12-06 17:13:53 -08001952 else
David Brownell52e31342008-03-03 12:43:23 -08001953 seq_printf(s, "GPIO %3d ", gpio);
Jarkko Nikula21c867f2008-12-10 17:35:24 -08001954 seq_printf(s, "(%-20.20s): %s %s",
David Brownell52e31342008-03-03 12:43:23 -08001955 label,
David Brownellb9772a22006-12-06 17:13:53 -08001956 is_in ? "in " : "out",
1957 value ? "hi" : "lo");
1958
David Brownell52e31342008-03-03 12:43:23 -08001959/* FIXME for at least omap2, show pullup/pulldown state */
1960
David Brownellb9772a22006-12-06 17:13:53 -08001961 irqstat = irq_desc[irq].status;
Tony Lindgren3a26e332009-01-15 13:09:53 +02001962#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || \
Santosh Shilimkar44169072009-05-28 14:16:04 -07001963 defined(CONFIG_ARCH_OMAP34XX) || defined(CONFIG_ARCH_OMAP4)
David Brownellb9772a22006-12-06 17:13:53 -08001964 if (is_in && ((bank->suspend_wakeup & mask)
1965 || irqstat & IRQ_TYPE_SENSE_MASK)) {
1966 char *trigger = NULL;
1967
1968 switch (irqstat & IRQ_TYPE_SENSE_MASK) {
1969 case IRQ_TYPE_EDGE_FALLING:
1970 trigger = "falling";
1971 break;
1972 case IRQ_TYPE_EDGE_RISING:
1973 trigger = "rising";
1974 break;
1975 case IRQ_TYPE_EDGE_BOTH:
1976 trigger = "bothedge";
1977 break;
1978 case IRQ_TYPE_LEVEL_LOW:
1979 trigger = "low";
1980 break;
1981 case IRQ_TYPE_LEVEL_HIGH:
1982 trigger = "high";
1983 break;
1984 case IRQ_TYPE_NONE:
David Brownell52e31342008-03-03 12:43:23 -08001985 trigger = "(?)";
David Brownellb9772a22006-12-06 17:13:53 -08001986 break;
1987 }
David Brownell52e31342008-03-03 12:43:23 -08001988 seq_printf(s, ", irq-%d %-8s%s",
David Brownellb9772a22006-12-06 17:13:53 -08001989 irq, trigger,
1990 (bank->suspend_wakeup & mask)
1991 ? " wakeup" : "");
1992 }
Tony Lindgren3a26e332009-01-15 13:09:53 +02001993#endif
David Brownellb9772a22006-12-06 17:13:53 -08001994 seq_printf(s, "\n");
1995 }
1996
David Brownelle5c56ed2006-12-06 17:13:59 -08001997 if (bank_is_mpuio(bank)) {
David Brownellb9772a22006-12-06 17:13:53 -08001998 seq_printf(s, "\n");
1999 gpio = 0;
2000 }
2001 }
2002 return 0;
2003}
2004
2005static int dbg_gpio_open(struct inode *inode, struct file *file)
2006{
David Brownelle5c56ed2006-12-06 17:13:59 -08002007 return single_open(file, dbg_gpio_show, &inode->i_private);
David Brownellb9772a22006-12-06 17:13:53 -08002008}
2009
2010static const struct file_operations debug_fops = {
2011 .open = dbg_gpio_open,
2012 .read = seq_read,
2013 .llseek = seq_lseek,
2014 .release = single_release,
2015};
2016
2017static int __init omap_gpio_debuginit(void)
2018{
David Brownelle5c56ed2006-12-06 17:13:59 -08002019 (void) debugfs_create_file("omap_gpio", S_IRUGO,
2020 NULL, NULL, &debug_fops);
David Brownellb9772a22006-12-06 17:13:53 -08002021 return 0;
2022}
2023late_initcall(omap_gpio_debuginit);
2024#endif