blob: 44acabd5f530d964b7085290f6806fc737cc27c3 [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>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020020#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010021#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>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080024#include <linux/slab.h>
25#include <linux/pm_runtime.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010028#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/irqs.h>
30#include <mach/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031#include <asm/mach/irq.h>
32
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010033struct gpio_bank {
Tony Lindgren9f7065d2009-10-19 15:25:20 -070034 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010035 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010036 u16 irq;
37 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010038 int method;
Tony Lindgren140455f2010-02-12 12:26:48 -080039#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +010040 u32 suspend_wakeup;
41 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080042#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080043 u32 non_wakeup_gpios;
44 u32 enabled_non_wakeup_gpios;
45
46 u32 saved_datain;
47 u32 saved_fallingdetect;
48 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080049 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080050 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010051 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080052 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080053 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080054 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080055 u32 dbck_enable_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080056 struct device *dev;
57 bool dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -080058 int stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010059};
60
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -080061#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +053062struct omap3_gpio_regs {
Rajendra Nayak40c670f2008-09-26 17:47:48 +053063 u32 irqenable1;
64 u32 irqenable2;
65 u32 wake_en;
66 u32 ctrl;
67 u32 oe;
68 u32 leveldetect0;
69 u32 leveldetect1;
70 u32 risingdetect;
71 u32 fallingdetect;
72 u32 dataout;
Rajendra Nayak40c670f2008-09-26 17:47:48 +053073};
74
75static struct omap3_gpio_regs gpio_context[OMAP34XX_NR_GPIOS];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -080076#endif
77
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080078/*
79 * TODO: Cleanup gpio_bank usage as it is having information
80 * related to all instances of the device
81 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010082static struct gpio_bank *gpio_bank;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080083
84static int bank_width;
85
Varadarajan, Charulathac95d10b2010-12-07 16:26:56 -080086/* TODO: Analyze removing gpio_bank_count usage from driver code */
87int gpio_bank_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010088
89static inline struct gpio_bank *get_gpio_bank(int gpio)
90{
Tony Lindgren6e60e792006-04-02 17:46:23 +010091 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010092 if (OMAP_GPIO_IS_MPUIO(gpio))
93 return &gpio_bank[0];
94 return &gpio_bank[1];
95 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010096 if (cpu_is_omap16xx()) {
97 if (OMAP_GPIO_IS_MPUIO(gpio))
98 return &gpio_bank[0];
99 return &gpio_bank[1 + (gpio >> 4)];
100 }
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700101 if (cpu_is_omap7xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100102 if (OMAP_GPIO_IS_MPUIO(gpio))
103 return &gpio_bank[0];
104 return &gpio_bank[1 + (gpio >> 5)];
105 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100106 if (cpu_is_omap24xx())
107 return &gpio_bank[gpio >> 5];
Santosh Shilimkar44169072009-05-28 14:16:04 -0700108 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800109 return &gpio_bank[gpio >> 5];
David Brownelle031ab22008-12-10 17:35:27 -0800110 BUG();
111 return NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100112}
113
114static inline int get_gpio_index(int gpio)
115{
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700116 if (cpu_is_omap7xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100117 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100118 if (cpu_is_omap24xx())
119 return gpio & 0x1f;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700120 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800121 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100122 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100123}
124
125static inline int gpio_valid(int gpio)
126{
127 if (gpio < 0)
128 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -0800129 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300130 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100131 return -1;
132 return 0;
133 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100134 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100135 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136 if ((cpu_is_omap16xx()) && gpio < 64)
137 return 0;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700138 if (cpu_is_omap7xx() && gpio < 192)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100139 return 0;
Tony Lindgren25d6f632010-08-02 14:21:39 +0300140 if (cpu_is_omap2420() && gpio < 128)
141 return 0;
142 if (cpu_is_omap2430() && gpio < 160)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100143 return 0;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700144 if ((cpu_is_omap34xx() || cpu_is_omap44xx()) && gpio < 192)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800145 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100146 return -1;
147}
148
149static int check_gpio(int gpio)
150{
Roel Kluind32b20f2009-11-17 14:39:03 -0800151 if (unlikely(gpio_valid(gpio) < 0)) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100152 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
153 dump_stack();
154 return -1;
155 }
156 return 0;
157}
158
159static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
160{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100161 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100162 u32 l;
163
164 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800165#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100166 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800167 reg += OMAP_MPUIO_IO_CNTL / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100168 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800169#endif
170#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100171 case METHOD_GPIO_1510:
172 reg += OMAP1510_GPIO_DIR_CONTROL;
173 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800174#endif
175#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100176 case METHOD_GPIO_1610:
177 reg += OMAP1610_GPIO_DIRECTION;
178 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800179#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100180#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100181 case METHOD_GPIO_7XX:
182 reg += OMAP7XX_GPIO_DIR_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700183 break;
184#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800185#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100186 case METHOD_GPIO_24XX:
187 reg += OMAP24XX_GPIO_OE;
188 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800189#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530190#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800191 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530192 reg += OMAP4_GPIO_OE;
193 break;
194#endif
David Brownelle5c56ed2006-12-06 17:13:59 -0800195 default:
196 WARN_ON(1);
197 return;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100198 }
199 l = __raw_readl(reg);
200 if (is_input)
201 l |= 1 << gpio;
202 else
203 l &= ~(1 << gpio);
204 __raw_writel(l, reg);
205}
206
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100207static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
208{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100209 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100210 u32 l = 0;
211
212 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800213#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100214 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800215 reg += OMAP_MPUIO_OUTPUT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100216 l = __raw_readl(reg);
217 if (enable)
218 l |= 1 << gpio;
219 else
220 l &= ~(1 << gpio);
221 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800222#endif
223#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100224 case METHOD_GPIO_1510:
225 reg += OMAP1510_GPIO_DATA_OUTPUT;
226 l = __raw_readl(reg);
227 if (enable)
228 l |= 1 << gpio;
229 else
230 l &= ~(1 << gpio);
231 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800232#endif
233#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100234 case METHOD_GPIO_1610:
235 if (enable)
236 reg += OMAP1610_GPIO_SET_DATAOUT;
237 else
238 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
239 l = 1 << gpio;
240 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800241#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100242#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100243 case METHOD_GPIO_7XX:
244 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700245 l = __raw_readl(reg);
246 if (enable)
247 l |= 1 << gpio;
248 else
249 l &= ~(1 << gpio);
250 break;
251#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800252#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100253 case METHOD_GPIO_24XX:
254 if (enable)
255 reg += OMAP24XX_GPIO_SETDATAOUT;
256 else
257 reg += OMAP24XX_GPIO_CLEARDATAOUT;
258 l = 1 << gpio;
259 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800260#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530261#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800262 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530263 if (enable)
264 reg += OMAP4_GPIO_SETDATAOUT;
265 else
266 reg += OMAP4_GPIO_CLEARDATAOUT;
267 l = 1 << gpio;
268 break;
269#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100270 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800271 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100272 return;
273 }
274 __raw_writel(l, reg);
275}
276
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300277static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100278{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100279 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100280
281 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800282 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100283 reg = bank->base;
284 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800285#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100286 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800287 reg += OMAP_MPUIO_INPUT_LATCH / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100288 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800289#endif
290#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100291 case METHOD_GPIO_1510:
292 reg += OMAP1510_GPIO_DATA_INPUT;
293 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800294#endif
295#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100296 case METHOD_GPIO_1610:
297 reg += OMAP1610_GPIO_DATAIN;
298 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800299#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100300#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100301 case METHOD_GPIO_7XX:
302 reg += OMAP7XX_GPIO_DATA_INPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700303 break;
304#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800305#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100306 case METHOD_GPIO_24XX:
307 reg += OMAP24XX_GPIO_DATAIN;
308 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800309#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530310#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800311 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530312 reg += OMAP4_GPIO_DATAIN;
313 break;
314#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100315 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800316 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100317 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100318 return (__raw_readl(reg)
319 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100320}
321
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300322static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
323{
324 void __iomem *reg;
325
326 if (check_gpio(gpio) < 0)
327 return -EINVAL;
328 reg = bank->base;
329
330 switch (bank->method) {
331#ifdef CONFIG_ARCH_OMAP1
332 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800333 reg += OMAP_MPUIO_OUTPUT / bank->stride;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300334 break;
335#endif
336#ifdef CONFIG_ARCH_OMAP15XX
337 case METHOD_GPIO_1510:
338 reg += OMAP1510_GPIO_DATA_OUTPUT;
339 break;
340#endif
341#ifdef CONFIG_ARCH_OMAP16XX
342 case METHOD_GPIO_1610:
343 reg += OMAP1610_GPIO_DATAOUT;
344 break;
345#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100346#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100347 case METHOD_GPIO_7XX:
348 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300349 break;
350#endif
Charulatha V9f096862010-05-14 12:05:27 -0700351#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300352 case METHOD_GPIO_24XX:
353 reg += OMAP24XX_GPIO_DATAOUT;
354 break;
355#endif
Charulatha V9f096862010-05-14 12:05:27 -0700356#ifdef CONFIG_ARCH_OMAP4
357 case METHOD_GPIO_44XX:
358 reg += OMAP4_GPIO_DATAOUT;
359 break;
360#endif
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300361 default:
362 return -EINVAL;
363 }
364
365 return (__raw_readl(reg) & (1 << get_gpio_index(gpio))) != 0;
366}
367
Tony Lindgren92105bb2005-09-07 17:20:26 +0100368#define MOD_REG_BIT(reg, bit_mask, set) \
369do { \
370 int l = __raw_readl(base + reg); \
371 if (set) l |= bit_mask; \
372 else l &= ~bit_mask; \
373 __raw_writel(l, base + reg); \
374} while(0)
375
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700376/**
377 * _set_gpio_debounce - low level gpio debounce time
378 * @bank: the gpio bank we're acting upon
379 * @gpio: the gpio number on this @gpio
380 * @debounce: debounce time to use
381 *
382 * OMAP's debounce time is in 31us steps so we need
383 * to convert and round up to the closest unit.
384 */
385static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
386 unsigned debounce)
387{
388 void __iomem *reg = bank->base;
389 u32 val;
390 u32 l;
391
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800392 if (!bank->dbck_flag)
393 return;
394
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700395 if (debounce < 32)
396 debounce = 0x01;
397 else if (debounce > 7936)
398 debounce = 0xff;
399 else
400 debounce = (debounce / 0x1f) - 1;
401
402 l = 1 << get_gpio_index(gpio);
403
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800404 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700405 reg += OMAP4_GPIO_DEBOUNCINGTIME;
406 else
407 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
408
409 __raw_writel(debounce, reg);
410
411 reg = bank->base;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800412 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700413 reg += OMAP4_GPIO_DEBOUNCENABLE;
414 else
415 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
416
417 val = __raw_readl(reg);
418
419 if (debounce) {
420 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800421 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700422 } else {
423 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800424 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700425 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300426 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700427
428 __raw_writel(val, reg);
429}
430
Tony Lindgren140455f2010-02-12 12:26:48 -0800431#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700432static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
433 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100434{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800435 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100436 u32 gpio_bit = 1 << gpio;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530437 u32 val;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100438
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530439 if (cpu_is_omap44xx()) {
440 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
441 trigger & IRQ_TYPE_LEVEL_LOW);
442 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
443 trigger & IRQ_TYPE_LEVEL_HIGH);
444 MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
445 trigger & IRQ_TYPE_EDGE_RISING);
446 MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
447 trigger & IRQ_TYPE_EDGE_FALLING);
448 } else {
449 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
450 trigger & IRQ_TYPE_LEVEL_LOW);
451 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
452 trigger & IRQ_TYPE_LEVEL_HIGH);
453 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
454 trigger & IRQ_TYPE_EDGE_RISING);
455 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
456 trigger & IRQ_TYPE_EDGE_FALLING);
457 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800458 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530459 if (cpu_is_omap44xx()) {
460 if (trigger != 0)
461 __raw_writel(1 << gpio, bank->base+
462 OMAP4_GPIO_IRQWAKEN0);
463 else {
464 val = __raw_readl(bank->base +
465 OMAP4_GPIO_IRQWAKEN0);
466 __raw_writel(val & (~(1 << gpio)), bank->base +
467 OMAP4_GPIO_IRQWAKEN0);
468 }
469 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000470 /*
471 * GPIO wakeup request can only be generated on edge
472 * transitions
473 */
474 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530475 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700476 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530477 else
478 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700479 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530480 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200481 }
482 /* This part needs to be executed always for OMAP34xx */
483 if (cpu_is_omap34xx() || (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000484 /*
485 * Log the edge gpio and manually trigger the IRQ
486 * after resume if the input level changes
487 * to avoid irq lost during PER RET/OFF mode
488 * Applies for omap2 non-wakeup gpio and all omap3 gpios
489 */
490 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800491 bank->enabled_non_wakeup_gpios |= gpio_bit;
492 else
493 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
494 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700495
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530496 if (cpu_is_omap44xx()) {
497 bank->level_mask =
498 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
499 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
500 } else {
501 bank->level_mask =
502 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
503 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
504 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100505}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800506#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100507
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800508#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800509/*
510 * This only applies to chips that can't do both rising and falling edge
511 * detection at once. For all other chips, this function is a noop.
512 */
513static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
514{
515 void __iomem *reg = bank->base;
516 u32 l = 0;
517
518 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800519 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800520 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800521 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800522#ifdef CONFIG_ARCH_OMAP15XX
523 case METHOD_GPIO_1510:
524 reg += OMAP1510_GPIO_INT_CONTROL;
525 break;
526#endif
527#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
528 case METHOD_GPIO_7XX:
529 reg += OMAP7XX_GPIO_INT_CONTROL;
530 break;
531#endif
532 default:
533 return;
534 }
535
536 l = __raw_readl(reg);
537 if ((l >> gpio) & 1)
538 l &= ~(1 << gpio);
539 else
540 l |= 1 << gpio;
541
542 __raw_writel(l, reg);
543}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800544#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800545
Tony Lindgren92105bb2005-09-07 17:20:26 +0100546static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
547{
548 void __iomem *reg = bank->base;
549 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100550
551 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800552#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100553 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800554 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100555 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000556 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800557 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100558 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100559 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100560 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100561 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100562 else
563 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800565#endif
566#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100567 case METHOD_GPIO_1510:
568 reg += OMAP1510_GPIO_INT_CONTROL;
569 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000570 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800571 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100572 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100574 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100575 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100576 else
577 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800579#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800580#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100581 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100582 if (gpio & 0x08)
583 reg += OMAP1610_GPIO_EDGE_CTRL2;
584 else
585 reg += OMAP1610_GPIO_EDGE_CTRL1;
586 gpio &= 0x07;
587 l = __raw_readl(reg);
588 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100589 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100590 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100591 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100592 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800593 if (trigger)
594 /* Enable wake-up during idle for dynamic tick */
595 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
596 else
597 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100598 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800599#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100600#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100601 case METHOD_GPIO_7XX:
602 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700603 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000604 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800605 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700606 if (trigger & IRQ_TYPE_EDGE_RISING)
607 l |= 1 << gpio;
608 else if (trigger & IRQ_TYPE_EDGE_FALLING)
609 l &= ~(1 << gpio);
610 else
611 goto bad;
612 break;
613#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800614#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100615 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800616 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800617 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200618 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800619#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100620 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100621 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100622 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100623 __raw_writel(l, reg);
624 return 0;
625bad:
626 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100627}
628
Lennert Buytenheke9191022010-11-29 11:17:17 +0100629static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100630{
631 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100632 unsigned gpio;
633 int retval;
David Brownella6472532008-03-03 04:33:30 -0800634 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100635
Lennert Buytenheke9191022010-11-29 11:17:17 +0100636 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
637 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100638 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100639 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100640
641 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100642 return -EINVAL;
643
David Brownelle5c56ed2006-12-06 17:13:59 -0800644 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100645 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800646
647 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800648 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800649 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100650 return -EINVAL;
651
Lennert Buytenheke9191022010-11-29 11:17:17 +0100652 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800653 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100654 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800655 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800656
657 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100658 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800659 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100660 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800661
Tony Lindgren92105bb2005-09-07 17:20:26 +0100662 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100663}
664
665static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
666{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100667 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100668
669 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800670#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100671 case METHOD_MPUIO:
672 /* MPUIO irqstatus is reset by reading the status register,
673 * so do nothing here */
674 return;
David Brownelle5c56ed2006-12-06 17:13:59 -0800675#endif
676#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100677 case METHOD_GPIO_1510:
678 reg += OMAP1510_GPIO_INT_STATUS;
679 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800680#endif
681#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100682 case METHOD_GPIO_1610:
683 reg += OMAP1610_GPIO_IRQSTATUS1;
684 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800685#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100686#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100687 case METHOD_GPIO_7XX:
688 reg += OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700689 break;
690#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800691#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100692 case METHOD_GPIO_24XX:
693 reg += OMAP24XX_GPIO_IRQSTATUS1;
694 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800695#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530696#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800697 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530698 reg += OMAP4_GPIO_IRQSTATUS0;
699 break;
700#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100701 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800702 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100703 return;
704 }
705 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300706
707 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800708 if (cpu_is_omap24xx() || cpu_is_omap34xx())
709 reg = bank->base + OMAP24XX_GPIO_IRQSTATUS2;
710 else if (cpu_is_omap44xx())
711 reg = bank->base + OMAP4_GPIO_IRQSTATUS1;
712
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530713 if (cpu_is_omap24xx() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Roger Quadrosbedfd152009-04-23 11:10:50 -0700714 __raw_writel(gpio_mask, reg);
715
716 /* Flush posted write for the irq status to avoid spurious interrupts */
717 __raw_readl(reg);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530718 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100719}
720
721static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
722{
723 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
724}
725
Imre Deakea6dedd2006-06-26 16:16:00 -0700726static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
727{
728 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700729 int inv = 0;
730 u32 l;
731 u32 mask;
Imre Deakea6dedd2006-06-26 16:16:00 -0700732
733 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800734#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700735 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800736 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Imre Deak99c47702006-06-26 16:16:07 -0700737 mask = 0xffff;
738 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700739 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800740#endif
741#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700742 case METHOD_GPIO_1510:
743 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700744 mask = 0xffff;
745 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700746 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800747#endif
748#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700749 case METHOD_GPIO_1610:
750 reg += OMAP1610_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700751 mask = 0xffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700752 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800753#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100754#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100755 case METHOD_GPIO_7XX:
756 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700757 mask = 0xffffffff;
758 inv = 1;
759 break;
760#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800761#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Imre Deakea6dedd2006-06-26 16:16:00 -0700762 case METHOD_GPIO_24XX:
763 reg += OMAP24XX_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700764 mask = 0xffffffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700765 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800766#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530767#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800768 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530769 reg += OMAP4_GPIO_IRQSTATUSSET0;
770 mask = 0xffffffff;
771 break;
772#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700773 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800774 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700775 return 0;
776 }
777
Imre Deak99c47702006-06-26 16:16:07 -0700778 l = __raw_readl(reg);
779 if (inv)
780 l = ~l;
781 l &= mask;
782 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700783}
784
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100785static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
786{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100787 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100788 u32 l;
789
790 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800791#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100792 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800793 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100794 l = __raw_readl(reg);
795 if (enable)
796 l &= ~(gpio_mask);
797 else
798 l |= gpio_mask;
799 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800800#endif
801#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100802 case METHOD_GPIO_1510:
803 reg += OMAP1510_GPIO_INT_MASK;
804 l = __raw_readl(reg);
805 if (enable)
806 l &= ~(gpio_mask);
807 else
808 l |= gpio_mask;
809 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800810#endif
811#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100812 case METHOD_GPIO_1610:
813 if (enable)
814 reg += OMAP1610_GPIO_SET_IRQENABLE1;
815 else
816 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
817 l = gpio_mask;
818 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800819#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100820#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100821 case METHOD_GPIO_7XX:
822 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700823 l = __raw_readl(reg);
824 if (enable)
825 l &= ~(gpio_mask);
826 else
827 l |= gpio_mask;
828 break;
829#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800830#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100831 case METHOD_GPIO_24XX:
832 if (enable)
833 reg += OMAP24XX_GPIO_SETIRQENABLE1;
834 else
835 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
836 l = gpio_mask;
837 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800838#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530839#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800840 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530841 if (enable)
842 reg += OMAP4_GPIO_IRQSTATUSSET0;
843 else
844 reg += OMAP4_GPIO_IRQSTATUSCLR0;
845 l = gpio_mask;
846 break;
847#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100848 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800849 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100850 return;
851 }
852 __raw_writel(l, reg);
853}
854
855static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
856{
857 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
858}
859
Tony Lindgren92105bb2005-09-07 17:20:26 +0100860/*
861 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
862 * 1510 does not seem to have a wake-up register. If JTAG is connected
863 * to the target, system will wake up always on GPIO events. While
864 * system is running all registered GPIO interrupts need to have wake-up
865 * enabled. When system is suspended, only selected GPIO interrupts need
866 * to have wake-up enabled.
867 */
868static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
869{
Tony Lindgren4cc64202010-01-08 10:29:05 -0800870 unsigned long uninitialized_var(flags);
David Brownella6472532008-03-03 04:33:30 -0800871
Tony Lindgren92105bb2005-09-07 17:20:26 +0100872 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800873#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800874 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100875 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800876 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700877 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100878 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700879 else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100880 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800881 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100882 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800883#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800884#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800885 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800886 case METHOD_GPIO_44XX:
David Brownell11a78b72006-12-06 17:14:11 -0800887 if (bank->non_wakeup_gpios & (1 << gpio)) {
888 printk(KERN_ERR "Unable to modify wakeup on "
889 "non-wakeup GPIO%d\n",
890 (bank - gpio_bank) * 32 + gpio);
891 return -EINVAL;
892 }
David Brownella6472532008-03-03 04:33:30 -0800893 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700894 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800895 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700896 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800897 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800898 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800899 return 0;
900#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100901 default:
902 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
903 bank->method);
904 return -EINVAL;
905 }
906}
907
Tony Lindgren4196dd62006-09-25 12:41:38 +0300908static void _reset_gpio(struct gpio_bank *bank, int gpio)
909{
910 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
911 _set_gpio_irqenable(bank, gpio, 0);
912 _clear_gpio_irqstatus(bank, gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100913 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300914}
915
Tony Lindgren92105bb2005-09-07 17:20:26 +0100916/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100917static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100918{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100919 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100920 struct gpio_bank *bank;
921 int retval;
922
923 if (check_gpio(gpio) < 0)
924 return -ENODEV;
Lennert Buytenheke9191022010-11-29 11:17:17 +0100925 bank = irq_data_get_irq_chip_data(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100926 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100927
928 return retval;
929}
930
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800931static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100932{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800933 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800934 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100935
David Brownella6472532008-03-03 04:33:30 -0800936 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100937
Tony Lindgren4196dd62006-09-25 12:41:38 +0300938 /* Set trigger to none. You need to enable the desired trigger with
939 * request_irq() or set_irq_type().
940 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800941 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100942
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000943#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100944 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100945 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100946
Tony Lindgren92105bb2005-09-07 17:20:26 +0100947 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100948 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800949 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100950 }
951#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800952 if (!cpu_class_is_omap1()) {
953 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700954 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800955 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700956
957 if (cpu_is_omap24xx() || cpu_is_omap34xx())
958 reg += OMAP24XX_GPIO_CTRL;
959 else if (cpu_is_omap44xx())
960 reg += OMAP4_GPIO_CTRL;
961 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800962 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700963 ctrl &= 0xFFFFFFFE;
964 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800965 }
966 bank->mod_usage |= 1 << offset;
967 }
David Brownella6472532008-03-03 04:33:30 -0800968 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100969
970 return 0;
971}
972
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800973static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100974{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800975 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800976 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100977
David Brownella6472532008-03-03 04:33:30 -0800978 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100979#ifdef CONFIG_ARCH_OMAP16XX
980 if (bank->method == METHOD_GPIO_1610) {
981 /* Disable wake-up during idle for dynamic tick */
982 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800983 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100984 }
985#endif
Charulatha V9f096862010-05-14 12:05:27 -0700986#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
987 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100988 /* Disable wake-up during idle for dynamic tick */
989 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800990 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100991 }
992#endif
Charulatha V9f096862010-05-14 12:05:27 -0700993#ifdef CONFIG_ARCH_OMAP4
994 if (bank->method == METHOD_GPIO_44XX) {
995 /* Disable wake-up during idle for dynamic tick */
996 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
997 __raw_writel(1 << offset, reg);
998 }
999#endif
Charulatha V058af1e2009-11-22 10:11:25 -08001000 if (!cpu_class_is_omap1()) {
1001 bank->mod_usage &= ~(1 << offset);
1002 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -07001003 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -08001004 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -07001005
1006 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1007 reg += OMAP24XX_GPIO_CTRL;
1008 else if (cpu_is_omap44xx())
1009 reg += OMAP4_GPIO_CTRL;
1010 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001011 /* Module is disabled, clocks are gated */
1012 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -07001013 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001014 }
1015 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001016 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -08001017 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001018}
1019
1020/*
1021 * We need to unmask the GPIO bank interrupt as soon as possible to
1022 * avoid missing GPIO interrupts for other lines in the bank.
1023 * Then we need to mask-read-clear-unmask the triggered GPIO lines
1024 * in the bank to avoid missing nested interrupts for a GPIO line.
1025 * If we wait to unmask individual GPIO lines in the bank after the
1026 * line's interrupt handler has been run, we may miss some nested
1027 * interrupts.
1028 */
Russell King10dd5ce2006-11-23 11:41:32 +00001029static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001030{
Tony Lindgren92105bb2005-09-07 17:20:26 +01001031 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001032 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -08001033 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001034 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -07001035 u32 retrigger = 0;
1036 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +00001037 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001038
Will Deaconee144182011-02-21 13:46:08 +00001039 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001040
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001041 bank = irq_get_handler_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -08001042#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001043 if (bank->method == METHOD_MPUIO)
Tony Lindgren5de62b82010-12-07 16:26:58 -08001044 isr_reg = bank->base +
1045 OMAP_MPUIO_GPIO_INT / bank->stride;
David Brownelle5c56ed2006-12-06 17:13:59 -08001046#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001047#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001048 if (bank->method == METHOD_GPIO_1510)
1049 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
1050#endif
1051#if defined(CONFIG_ARCH_OMAP16XX)
1052 if (bank->method == METHOD_GPIO_1610)
1053 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
1054#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +01001055#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +01001056 if (bank->method == METHOD_GPIO_7XX)
1057 isr_reg = bank->base + OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001058#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001059#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001060 if (bank->method == METHOD_GPIO_24XX)
1061 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1062#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301063#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001064 if (bank->method == METHOD_GPIO_44XX)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301065 isr_reg = bank->base + OMAP4_GPIO_IRQSTATUS0;
1066#endif
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -08001067
1068 if (WARN_ON(!isr_reg))
1069 goto exit;
1070
Tony Lindgren92105bb2005-09-07 17:20:26 +01001071 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001072 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -07001073 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001074
Imre Deakea6dedd2006-06-26 16:16:00 -07001075 enabled = _get_gpio_irqbank_mask(bank);
1076 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001077
1078 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1079 isr &= 0x0000ffff;
1080
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001081 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001082 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -07001083 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001084
1085 /* clear edge sensitive interrupts before handler(s) are
1086 called so that we don't miss any interrupt occurred while
1087 executing them */
1088 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1089 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1090 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1091
1092 /* if there is only edge sensitive GPIO pin interrupts
1093 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -07001094 if (!level_mask && !unmasked) {
1095 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +00001096 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -07001097 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001098
Imre Deakea6dedd2006-06-26 16:16:00 -07001099 isr |= retrigger;
1100 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001101 if (!isr)
1102 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001103
Tony Lindgren92105bb2005-09-07 17:20:26 +01001104 gpio_irq = bank->virtual_irq_start;
1105 for (; isr != 0; isr >>= 1, gpio_irq++) {
Cory Maccarrone4318f362010-01-08 10:29:04 -08001106 gpio_index = get_gpio_index(irq_to_gpio(gpio_irq));
1107
Tony Lindgren92105bb2005-09-07 17:20:26 +01001108 if (!(isr & 1))
1109 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001110
Cory Maccarrone4318f362010-01-08 10:29:04 -08001111#ifdef CONFIG_ARCH_OMAP1
1112 /*
1113 * Some chips can't respond to both rising and falling
1114 * at the same time. If this irq was requested with
1115 * both flags, we need to flip the ICR data for the IRQ
1116 * to respond to the IRQ for the opposite direction.
1117 * This will be indicated in the bank toggle_mask.
1118 */
1119 if (bank->toggle_mask & (1 << gpio_index))
1120 _toggle_gpio_edge_triggering(bank, gpio_index);
1121#endif
1122
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +01001123 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001124 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001125 }
Imre Deakea6dedd2006-06-26 16:16:00 -07001126 /* if bank has any level sensitive GPIO pin interrupt
1127 configured, we must unmask the bank interrupt only after
1128 handler(s) are executed in order to avoid spurious bank
1129 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -08001130exit:
Imre Deakea6dedd2006-06-26 16:16:00 -07001131 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +00001132 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001133}
1134
Lennert Buytenheke9191022010-11-29 11:17:17 +01001135static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +03001136{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001137 unsigned int gpio = d->irq - IH_GPIO_BASE;
1138 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001139
1140 _reset_gpio(bank, gpio);
1141}
1142
Lennert Buytenheke9191022010-11-29 11:17:17 +01001143static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001144{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001145 unsigned int gpio = d->irq - IH_GPIO_BASE;
1146 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001147
1148 _clear_gpio_irqstatus(bank, gpio);
1149}
1150
Lennert Buytenheke9191022010-11-29 11:17:17 +01001151static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001152{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001153 unsigned int gpio = d->irq - IH_GPIO_BASE;
1154 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001155
1156 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman55b60192009-06-04 15:57:10 -07001157 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001158}
1159
Lennert Buytenheke9191022010-11-29 11:17:17 +01001160static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001161{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001162 unsigned int gpio = d->irq - IH_GPIO_BASE;
1163 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001164 unsigned int irq_mask = 1 << get_gpio_index(gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +01001165 u32 trigger = irqd_get_trigger_type(d);
Kevin Hilman55b60192009-06-04 15:57:10 -07001166
1167 if (trigger)
1168 _set_gpio_triggering(bank, get_gpio_index(gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001169
1170 /* For level-triggered GPIOs, the clearing must be done after
1171 * the HW source is cleared, thus after the handler has run */
1172 if (bank->level_mask & irq_mask) {
1173 _set_gpio_irqenable(bank, gpio, 0);
1174 _clear_gpio_irqstatus(bank, gpio);
1175 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001176
Kevin Hilman4de8c752008-01-16 21:56:14 -08001177 _set_gpio_irqenable(bank, gpio, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001178}
1179
David Brownelle5c56ed2006-12-06 17:13:59 -08001180static struct irq_chip gpio_irq_chip = {
1181 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +01001182 .irq_shutdown = gpio_irq_shutdown,
1183 .irq_ack = gpio_ack_irq,
1184 .irq_mask = gpio_mask_irq,
1185 .irq_unmask = gpio_unmask_irq,
1186 .irq_set_type = gpio_irq_type,
1187 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -08001188};
1189
1190/*---------------------------------------------------------------------*/
1191
1192#ifdef CONFIG_ARCH_OMAP1
1193
1194/* MPUIO uses the always-on 32k clock */
1195
Lennert Buytenheke9191022010-11-29 11:17:17 +01001196static void mpuio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001197{
1198 /* The ISR is reset automatically, so do nothing here. */
1199}
1200
Lennert Buytenheke9191022010-11-29 11:17:17 +01001201static void mpuio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001202{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001203 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1204 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001205
1206 _set_gpio_irqenable(bank, gpio, 0);
1207}
1208
Lennert Buytenheke9191022010-11-29 11:17:17 +01001209static void mpuio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001210{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001211 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1212 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001213
1214 _set_gpio_irqenable(bank, gpio, 1);
1215}
1216
David Brownelle5c56ed2006-12-06 17:13:59 -08001217static struct irq_chip mpuio_irq_chip = {
1218 .name = "MPUIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +01001219 .irq_ack = mpuio_ack_irq,
1220 .irq_mask = mpuio_mask_irq,
1221 .irq_unmask = mpuio_unmask_irq,
1222 .irq_set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001223#ifdef CONFIG_ARCH_OMAP16XX
1224 /* REVISIT: assuming only 16xx supports MPUIO wake events */
Lennert Buytenheke9191022010-11-29 11:17:17 +01001225 .irq_set_wake = gpio_wake_enable,
David Brownell11a78b72006-12-06 17:14:11 -08001226#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001227};
1228
David Brownelle5c56ed2006-12-06 17:13:59 -08001229
1230#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1231
David Brownell11a78b72006-12-06 17:14:11 -08001232
1233#ifdef CONFIG_ARCH_OMAP16XX
1234
1235#include <linux/platform_device.h>
1236
Magnus Damm79ee0312009-07-08 13:22:04 +02001237static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001238{
Magnus Damm79ee0312009-07-08 13:22:04 +02001239 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001240 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001241 void __iomem *mask_reg = bank->base +
1242 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001243 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001244
David Brownella6472532008-03-03 04:33:30 -08001245 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001246 bank->saved_wakeup = __raw_readl(mask_reg);
1247 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001248 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001249
1250 return 0;
1251}
1252
Magnus Damm79ee0312009-07-08 13:22:04 +02001253static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001254{
Magnus Damm79ee0312009-07-08 13:22:04 +02001255 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001256 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001257 void __iomem *mask_reg = bank->base +
1258 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001259 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001260
David Brownella6472532008-03-03 04:33:30 -08001261 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001262 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001263 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001264
1265 return 0;
1266}
1267
Alexey Dobriyan47145212009-12-14 18:00:08 -08001268static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +02001269 .suspend_noirq = omap_mpuio_suspend_noirq,
1270 .resume_noirq = omap_mpuio_resume_noirq,
1271};
1272
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001273/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -08001274static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -08001275 .driver = {
1276 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +02001277 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -08001278 },
1279};
1280
1281static struct platform_device omap_mpuio_device = {
1282 .name = "mpuio",
1283 .id = -1,
1284 .dev = {
1285 .driver = &omap_mpuio_driver.driver,
1286 }
1287 /* could list the /proc/iomem resources */
1288};
1289
1290static inline void mpuio_init(void)
1291{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001292 struct gpio_bank *bank = get_gpio_bank(OMAP_MPUIO(0));
1293 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -07001294
David Brownell11a78b72006-12-06 17:14:11 -08001295 if (platform_driver_register(&omap_mpuio_driver) == 0)
1296 (void) platform_device_register(&omap_mpuio_device);
1297}
1298
1299#else
1300static inline void mpuio_init(void) {}
1301#endif /* 16xx */
1302
David Brownelle5c56ed2006-12-06 17:13:59 -08001303#else
1304
1305extern struct irq_chip mpuio_irq_chip;
1306
1307#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001308static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001309
1310#endif
1311
1312/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001313
David Brownell52e31342008-03-03 12:43:23 -08001314/* REVISIT these are stupid implementations! replace by ones that
1315 * don't switch on METHOD_* and which mostly avoid spinlocks
1316 */
1317
1318static int gpio_input(struct gpio_chip *chip, unsigned offset)
1319{
1320 struct gpio_bank *bank;
1321 unsigned long flags;
1322
1323 bank = container_of(chip, struct gpio_bank, chip);
1324 spin_lock_irqsave(&bank->lock, flags);
1325 _set_gpio_direction(bank, offset, 1);
1326 spin_unlock_irqrestore(&bank->lock, flags);
1327 return 0;
1328}
1329
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001330static int gpio_is_input(struct gpio_bank *bank, int mask)
1331{
1332 void __iomem *reg = bank->base;
1333
1334 switch (bank->method) {
1335 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -08001336 reg += OMAP_MPUIO_IO_CNTL / bank->stride;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001337 break;
1338 case METHOD_GPIO_1510:
1339 reg += OMAP1510_GPIO_DIR_CONTROL;
1340 break;
1341 case METHOD_GPIO_1610:
1342 reg += OMAP1610_GPIO_DIRECTION;
1343 break;
Alistair Buxton7c006922009-09-22 10:02:58 +01001344 case METHOD_GPIO_7XX:
1345 reg += OMAP7XX_GPIO_DIR_CONTROL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001346 break;
1347 case METHOD_GPIO_24XX:
1348 reg += OMAP24XX_GPIO_OE;
1349 break;
Charulatha V9f096862010-05-14 12:05:27 -07001350 case METHOD_GPIO_44XX:
1351 reg += OMAP4_GPIO_OE;
1352 break;
1353 default:
1354 WARN_ONCE(1, "gpio_is_input: incorrect OMAP GPIO method");
1355 return -EINVAL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001356 }
1357 return __raw_readl(reg) & mask;
1358}
1359
David Brownell52e31342008-03-03 12:43:23 -08001360static int gpio_get(struct gpio_chip *chip, unsigned offset)
1361{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001362 struct gpio_bank *bank;
1363 void __iomem *reg;
1364 int gpio;
1365 u32 mask;
1366
1367 gpio = chip->base + offset;
1368 bank = get_gpio_bank(gpio);
1369 reg = bank->base;
1370 mask = 1 << get_gpio_index(gpio);
1371
1372 if (gpio_is_input(bank, mask))
1373 return _get_gpio_datain(bank, gpio);
1374 else
1375 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -08001376}
1377
1378static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1379{
1380 struct gpio_bank *bank;
1381 unsigned long flags;
1382
1383 bank = container_of(chip, struct gpio_bank, chip);
1384 spin_lock_irqsave(&bank->lock, flags);
1385 _set_gpio_dataout(bank, offset, value);
1386 _set_gpio_direction(bank, offset, 0);
1387 spin_unlock_irqrestore(&bank->lock, flags);
1388 return 0;
1389}
1390
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001391static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
1392 unsigned debounce)
1393{
1394 struct gpio_bank *bank;
1395 unsigned long flags;
1396
1397 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001398
1399 if (!bank->dbck) {
1400 bank->dbck = clk_get(bank->dev, "dbclk");
1401 if (IS_ERR(bank->dbck))
1402 dev_err(bank->dev, "Could not get gpio dbck\n");
1403 }
1404
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001405 spin_lock_irqsave(&bank->lock, flags);
1406 _set_gpio_debounce(bank, offset, debounce);
1407 spin_unlock_irqrestore(&bank->lock, flags);
1408
1409 return 0;
1410}
1411
David Brownell52e31342008-03-03 12:43:23 -08001412static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1413{
1414 struct gpio_bank *bank;
1415 unsigned long flags;
1416
1417 bank = container_of(chip, struct gpio_bank, chip);
1418 spin_lock_irqsave(&bank->lock, flags);
1419 _set_gpio_dataout(bank, offset, value);
1420 spin_unlock_irqrestore(&bank->lock, flags);
1421}
1422
David Brownella007b702008-12-10 17:35:25 -08001423static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1424{
1425 struct gpio_bank *bank;
1426
1427 bank = container_of(chip, struct gpio_bank, chip);
1428 return bank->virtual_irq_start + offset;
1429}
1430
David Brownell52e31342008-03-03 12:43:23 -08001431/*---------------------------------------------------------------------*/
1432
Tony Lindgren9a748052010-12-07 16:26:56 -08001433static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001434{
1435 u32 rev;
1436
Tony Lindgren9a748052010-12-07 16:26:56 -08001437 if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
1438 rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001439 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001440 rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001441 else if (cpu_is_omap44xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001442 rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001443 else
1444 return;
1445
1446 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1447 (rev >> 4) & 0x0f, rev & 0x0f);
1448}
1449
David Brownell8ba55c52008-02-26 11:10:50 -08001450/* This lock class tells lockdep that GPIO irqs are in a different
1451 * category than their parents, so it won't report false recursion.
1452 */
1453static struct lock_class_key gpio_lock_class;
1454
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001455static inline int init_gpio_info(struct platform_device *pdev)
1456{
1457 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1458 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1459 GFP_KERNEL);
1460 if (!gpio_bank) {
1461 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1462 return -ENOMEM;
1463 }
1464 return 0;
1465}
1466
1467/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001468static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1469{
1470 if (cpu_class_is_omap2()) {
1471 if (cpu_is_omap44xx()) {
1472 __raw_writel(0xffffffff, bank->base +
1473 OMAP4_GPIO_IRQSTATUSCLR0);
1474 __raw_writel(0x00000000, bank->base +
1475 OMAP4_GPIO_DEBOUNCENABLE);
1476 /* Initialize interface clk ungated, module enabled */
1477 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1478 } else if (cpu_is_omap34xx()) {
1479 __raw_writel(0x00000000, bank->base +
1480 OMAP24XX_GPIO_IRQENABLE1);
1481 __raw_writel(0xffffffff, bank->base +
1482 OMAP24XX_GPIO_IRQSTATUS1);
1483 __raw_writel(0x00000000, bank->base +
1484 OMAP24XX_GPIO_DEBOUNCE_EN);
1485
1486 /* Initialize interface clk ungated, module enabled */
1487 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1488 } else if (cpu_is_omap24xx()) {
1489 static const u32 non_wakeup_gpios[] = {
1490 0xe203ffc0, 0x08700040
1491 };
1492 if (id < ARRAY_SIZE(non_wakeup_gpios))
1493 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1494 }
1495 } else if (cpu_class_is_omap1()) {
1496 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001497 __raw_writew(0xffff, bank->base +
1498 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001499 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1500 __raw_writew(0xffff, bank->base
1501 + OMAP1510_GPIO_INT_MASK);
1502 __raw_writew(0x0000, bank->base
1503 + OMAP1510_GPIO_INT_STATUS);
1504 }
1505 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1506 __raw_writew(0x0000, bank->base
1507 + OMAP1610_GPIO_IRQENABLE1);
1508 __raw_writew(0xffff, bank->base
1509 + OMAP1610_GPIO_IRQSTATUS1);
1510 __raw_writew(0x0014, bank->base
1511 + OMAP1610_GPIO_SYSCONFIG);
1512
1513 /*
1514 * Enable system clock for GPIO module.
1515 * The CAM_CLK_CTRL *is* really the right place.
1516 */
1517 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1518 ULPD_CAM_CLK_CTRL);
1519 }
1520 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1521 __raw_writel(0xffffffff, bank->base
1522 + OMAP7XX_GPIO_INT_MASK);
1523 __raw_writel(0x00000000, bank->base
1524 + OMAP7XX_GPIO_INT_STATUS);
1525 }
1526 }
1527}
1528
1529static void __init omap_gpio_chip_init(struct gpio_bank *bank)
1530{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001531 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001532 static int gpio;
1533
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001534 bank->mod_usage = 0;
1535 /*
1536 * REVISIT eventually switch from OMAP-specific gpio structs
1537 * over to the generic ones
1538 */
1539 bank->chip.request = omap_gpio_request;
1540 bank->chip.free = omap_gpio_free;
1541 bank->chip.direction_input = gpio_input;
1542 bank->chip.get = gpio_get;
1543 bank->chip.direction_output = gpio_output;
1544 bank->chip.set_debounce = gpio_debounce;
1545 bank->chip.set = gpio_set;
1546 bank->chip.to_irq = gpio_2irq;
1547 if (bank_is_mpuio(bank)) {
1548 bank->chip.label = "mpuio";
1549#ifdef CONFIG_ARCH_OMAP16XX
1550 bank->chip.dev = &omap_mpuio_device.dev;
1551#endif
1552 bank->chip.base = OMAP_MPUIO(0);
1553 } else {
1554 bank->chip.label = "gpio";
1555 bank->chip.base = gpio;
1556 gpio += bank_width;
1557 }
1558 bank->chip.ngpio = bank_width;
1559
1560 gpiochip_add(&bank->chip);
1561
1562 for (j = bank->virtual_irq_start;
1563 j < bank->virtual_irq_start + bank_width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001564 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001565 irq_set_chip_data(j, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001566 if (bank_is_mpuio(bank))
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001567 irq_set_chip(j, &mpuio_irq_chip);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001568 else
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001569 irq_set_chip(j, &gpio_irq_chip);
1570 irq_set_handler(j, handle_simple_irq);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001571 set_irq_flags(j, IRQF_VALID);
1572 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001573 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1574 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001575}
1576
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001577static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001578{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001579 static int gpio_init_done;
1580 struct omap_gpio_platform_data *pdata;
1581 struct resource *res;
1582 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001583 struct gpio_bank *bank;
1584
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001585 if (!pdev->dev.platform_data)
1586 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001587
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001588 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001589
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001590 if (!gpio_init_done) {
1591 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001592
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001593 ret = init_gpio_info(pdev);
1594 if (ret)
1595 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001596 }
1597
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001598 id = pdev->id;
1599 bank = &gpio_bank[id];
1600
1601 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1602 if (unlikely(!res)) {
1603 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1604 return -ENODEV;
1605 }
1606
1607 bank->irq = res->start;
1608 bank->virtual_irq_start = pdata->virtual_irq_start;
1609 bank->method = pdata->bank_type;
1610 bank->dev = &pdev->dev;
1611 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001612 bank->stride = pdata->bank_stride;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001613 bank_width = pdata->bank_width;
1614
1615 spin_lock_init(&bank->lock);
1616
1617 /* Static mapping, never released */
1618 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1619 if (unlikely(!res)) {
1620 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1621 return -ENODEV;
1622 }
1623
1624 bank->base = ioremap(res->start, resource_size(res));
1625 if (!bank->base) {
1626 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1627 return -ENOMEM;
1628 }
1629
1630 pm_runtime_enable(bank->dev);
1631 pm_runtime_get_sync(bank->dev);
1632
1633 omap_gpio_mod_init(bank, id);
1634 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001635 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001636
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001637 if (!gpio_init_done)
1638 gpio_init_done = 1;
1639
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001640 return 0;
1641}
1642
Tony Lindgren140455f2010-02-12 12:26:48 -08001643#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001644static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001645{
1646 int i;
1647
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001648 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001649 return 0;
1650
1651 for (i = 0; i < gpio_bank_count; i++) {
1652 struct gpio_bank *bank = &gpio_bank[i];
1653 void __iomem *wake_status;
1654 void __iomem *wake_clear;
1655 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001656 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001657
1658 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001659#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001660 case METHOD_GPIO_1610:
1661 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1662 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1663 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1664 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001665#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001666#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001667 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001668 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001669 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1670 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1671 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001672#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301673#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001674 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301675 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1676 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1677 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1678 break;
1679#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001680 default:
1681 continue;
1682 }
1683
David Brownella6472532008-03-03 04:33:30 -08001684 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001685 bank->saved_wakeup = __raw_readl(wake_status);
1686 __raw_writel(0xffffffff, wake_clear);
1687 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001688 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001689 }
1690
1691 return 0;
1692}
1693
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001694static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001695{
1696 int i;
1697
Tero Kristo723fdb72008-11-26 14:35:16 -08001698 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001699 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001700
1701 for (i = 0; i < gpio_bank_count; i++) {
1702 struct gpio_bank *bank = &gpio_bank[i];
1703 void __iomem *wake_clear;
1704 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001705 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001706
1707 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001708#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001709 case METHOD_GPIO_1610:
1710 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1711 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1712 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001713#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001714#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001715 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001716 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1717 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001718 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001719#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301720#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001721 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301722 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1723 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1724 break;
1725#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001726 default:
1727 continue;
1728 }
1729
David Brownella6472532008-03-03 04:33:30 -08001730 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001731 __raw_writel(0xffffffff, wake_clear);
1732 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001733 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001734 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001735}
1736
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001737static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001738 .suspend = omap_gpio_suspend,
1739 .resume = omap_gpio_resume,
1740};
1741
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001742#endif
1743
Tony Lindgren140455f2010-02-12 12:26:48 -08001744#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001745
1746static int workaround_enabled;
1747
Paul Walmsley72e06d02010-12-21 21:05:16 -07001748void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001749{
1750 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001751 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001752
Tero Kristoa118b5f2008-12-22 14:27:12 +02001753 if (cpu_is_omap34xx())
1754 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001755
Tero Kristoa118b5f2008-12-22 14:27:12 +02001756 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001757 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001758 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001759 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001760
Kevin Hilman0aed04352010-09-22 16:06:27 -07001761 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001762 clk_disable(bank->dbck);
1763
Paul Walmsley72e06d02010-12-21 21:05:16 -07001764 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001765 continue;
1766
1767 /* If going to OFF, remove triggering for all
1768 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1769 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001770 if (!(bank->enabled_non_wakeup_gpios))
1771 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001772
1773 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1774 bank->saved_datain = __raw_readl(bank->base +
1775 OMAP24XX_GPIO_DATAIN);
1776 l1 = __raw_readl(bank->base +
1777 OMAP24XX_GPIO_FALLINGDETECT);
1778 l2 = __raw_readl(bank->base +
1779 OMAP24XX_GPIO_RISINGDETECT);
1780 }
1781
1782 if (cpu_is_omap44xx()) {
1783 bank->saved_datain = __raw_readl(bank->base +
1784 OMAP4_GPIO_DATAIN);
1785 l1 = __raw_readl(bank->base +
1786 OMAP4_GPIO_FALLINGDETECT);
1787 l2 = __raw_readl(bank->base +
1788 OMAP4_GPIO_RISINGDETECT);
1789 }
1790
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001791 bank->saved_fallingdetect = l1;
1792 bank->saved_risingdetect = l2;
1793 l1 &= ~bank->enabled_non_wakeup_gpios;
1794 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001795
1796 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1797 __raw_writel(l1, bank->base +
1798 OMAP24XX_GPIO_FALLINGDETECT);
1799 __raw_writel(l2, bank->base +
1800 OMAP24XX_GPIO_RISINGDETECT);
1801 }
1802
1803 if (cpu_is_omap44xx()) {
1804 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1805 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1806 }
1807
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001808 c++;
1809 }
1810 if (!c) {
1811 workaround_enabled = 0;
1812 return;
1813 }
1814 workaround_enabled = 1;
1815}
1816
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001817void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001818{
1819 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001820 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001821
Tero Kristoa118b5f2008-12-22 14:27:12 +02001822 if (cpu_is_omap34xx())
1823 min = 1;
1824 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001825 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001826 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001827 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001828
Kevin Hilman0aed04352010-09-22 16:06:27 -07001829 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001830 clk_enable(bank->dbck);
1831
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001832 if (!workaround_enabled)
1833 continue;
1834
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001835 if (!(bank->enabled_non_wakeup_gpios))
1836 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001837
1838 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1839 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001840 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001841 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001842 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001843 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1844 }
1845
1846 if (cpu_is_omap44xx()) {
1847 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301848 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001849 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301850 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001851 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1852 }
1853
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001854 /* Check if any of the non-wakeup interrupt GPIOs have changed
1855 * state. If so, generate an IRQ by software. This is
1856 * horribly racy, but it's the best we can do to work around
1857 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001858 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001859 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001860
1861 /*
1862 * No need to generate IRQs for the rising edge for gpio IRQs
1863 * configured with falling edge only; and vice versa.
1864 */
1865 gen0 = l & bank->saved_fallingdetect;
1866 gen0 &= bank->saved_datain;
1867
1868 gen1 = l & bank->saved_risingdetect;
1869 gen1 &= ~(bank->saved_datain);
1870
1871 /* FIXME: Consider GPIO IRQs with level detections properly! */
1872 gen = l & (~(bank->saved_fallingdetect) &
1873 ~(bank->saved_risingdetect));
1874 /* Consider all GPIO IRQs needed to be updated */
1875 gen |= gen0 | gen1;
1876
1877 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001878 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001879
Sergio Aguirref00d6492010-03-03 16:21:08 +00001880 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001881 old0 = __raw_readl(bank->base +
1882 OMAP24XX_GPIO_LEVELDETECT0);
1883 old1 = __raw_readl(bank->base +
1884 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001885 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001886 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001887 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001888 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001889 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001890 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001891 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001892 OMAP24XX_GPIO_LEVELDETECT1);
1893 }
1894
1895 if (cpu_is_omap44xx()) {
1896 old0 = __raw_readl(bank->base +
1897 OMAP4_GPIO_LEVELDETECT0);
1898 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301899 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001900 __raw_writel(old0 | l, bank->base +
1901 OMAP4_GPIO_LEVELDETECT0);
1902 __raw_writel(old1 | l, bank->base +
1903 OMAP4_GPIO_LEVELDETECT1);
1904 __raw_writel(old0, bank->base +
1905 OMAP4_GPIO_LEVELDETECT0);
1906 __raw_writel(old1, bank->base +
1907 OMAP4_GPIO_LEVELDETECT1);
1908 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001909 }
1910 }
1911
1912}
1913
Tony Lindgren92105bb2005-09-07 17:20:26 +01001914#endif
1915
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001916#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301917/* save the registers of bank 2-6 */
1918void omap_gpio_save_context(void)
1919{
1920 int i;
1921
1922 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1923 for (i = 1; i < gpio_bank_count; i++) {
1924 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301925 gpio_context[i].irqenable1 =
1926 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1927 gpio_context[i].irqenable2 =
1928 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1929 gpio_context[i].wake_en =
1930 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1931 gpio_context[i].ctrl =
1932 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1933 gpio_context[i].oe =
1934 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1935 gpio_context[i].leveldetect0 =
1936 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1937 gpio_context[i].leveldetect1 =
1938 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1939 gpio_context[i].risingdetect =
1940 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1941 gpio_context[i].fallingdetect =
1942 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1943 gpio_context[i].dataout =
1944 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301945 }
1946}
1947
1948/* restore the required registers of bank 2-6 */
1949void omap_gpio_restore_context(void)
1950{
1951 int i;
1952
1953 for (i = 1; i < gpio_bank_count; i++) {
1954 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301955 __raw_writel(gpio_context[i].irqenable1,
1956 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1957 __raw_writel(gpio_context[i].irqenable2,
1958 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1959 __raw_writel(gpio_context[i].wake_en,
1960 bank->base + OMAP24XX_GPIO_WAKE_EN);
1961 __raw_writel(gpio_context[i].ctrl,
1962 bank->base + OMAP24XX_GPIO_CTRL);
1963 __raw_writel(gpio_context[i].oe,
1964 bank->base + OMAP24XX_GPIO_OE);
1965 __raw_writel(gpio_context[i].leveldetect0,
1966 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1967 __raw_writel(gpio_context[i].leveldetect1,
1968 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1969 __raw_writel(gpio_context[i].risingdetect,
1970 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1971 __raw_writel(gpio_context[i].fallingdetect,
1972 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1973 __raw_writel(gpio_context[i].dataout,
1974 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301975 }
1976}
1977#endif
1978
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001979static struct platform_driver omap_gpio_driver = {
1980 .probe = omap_gpio_probe,
1981 .driver = {
1982 .name = "omap_gpio",
1983 },
1984};
1985
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001986/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001987 * gpio driver register needs to be done before
1988 * machine_init functions access gpio APIs.
1989 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001990 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001991static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001992{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001993 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001994}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001995postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001996
Tony Lindgren92105bb2005-09-07 17:20:26 +01001997static int __init omap_gpio_sysinit(void)
1998{
David Brownell11a78b72006-12-06 17:14:11 -08001999 mpuio_init();
2000
Tony Lindgren140455f2010-02-12 12:26:48 -08002001#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02002002 if (cpu_is_omap16xx() || cpu_class_is_omap2())
2003 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01002004#endif
2005
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02002006 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01002007}
2008
Tony Lindgren92105bb2005-09-07 17:20:26 +01002009arch_initcall(omap_gpio_sysinit);