blob: 0766aa1c3f3152037a399a46ac89c446fc113dba [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080022#include <linux/slab.h>
23#include <linux/pm_runtime.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/irqs.h>
28#include <mach/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029#include <asm/mach/irq.h>
30
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031struct gpio_bank {
Tony Lindgren9f7065d2009-10-19 15:25:20 -070032 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010033 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010034 u16 irq;
35 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010036 int method;
Tony Lindgren140455f2010-02-12 12:26:48 -080037#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +010038 u32 suspend_wakeup;
39 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080040#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080041 u32 non_wakeup_gpios;
42 u32 enabled_non_wakeup_gpios;
43
44 u32 saved_datain;
45 u32 saved_fallingdetect;
46 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080047 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080048 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010049 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080050 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080051 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080052 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080053 u32 dbck_enable_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080054 struct device *dev;
55 bool dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -080056 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070057 u32 width;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010058};
59
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -080060#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +053061struct omap3_gpio_regs {
Rajendra Nayak40c670f2008-09-26 17:47:48 +053062 u32 irqenable1;
63 u32 irqenable2;
64 u32 wake_en;
65 u32 ctrl;
66 u32 oe;
67 u32 leveldetect0;
68 u32 leveldetect1;
69 u32 risingdetect;
70 u32 fallingdetect;
71 u32 dataout;
Rajendra Nayak40c670f2008-09-26 17:47:48 +053072};
73
74static struct omap3_gpio_regs gpio_context[OMAP34XX_NR_GPIOS];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -080075#endif
76
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080077/*
78 * TODO: Cleanup gpio_bank usage as it is having information
79 * related to all instances of the device
80 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010081static struct gpio_bank *gpio_bank;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080082
Varadarajan, Charulathac95d10b2010-12-07 16:26:56 -080083/* TODO: Analyze removing gpio_bank_count usage from driver code */
84int gpio_bank_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085
86static inline struct gpio_bank *get_gpio_bank(int gpio)
87{
Tony Lindgren6e60e792006-04-02 17:46:23 +010088 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010089 if (OMAP_GPIO_IS_MPUIO(gpio))
90 return &gpio_bank[0];
91 return &gpio_bank[1];
92 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010093 if (cpu_is_omap16xx()) {
94 if (OMAP_GPIO_IS_MPUIO(gpio))
95 return &gpio_bank[0];
96 return &gpio_bank[1 + (gpio >> 4)];
97 }
Zebediah C. McClure56739a62009-03-23 18:07:40 -070098 if (cpu_is_omap7xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099 if (OMAP_GPIO_IS_MPUIO(gpio))
100 return &gpio_bank[0];
101 return &gpio_bank[1 + (gpio >> 5)];
102 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100103 if (cpu_is_omap24xx())
104 return &gpio_bank[gpio >> 5];
Santosh Shilimkar44169072009-05-28 14:16:04 -0700105 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800106 return &gpio_bank[gpio >> 5];
David Brownelle031ab22008-12-10 17:35:27 -0800107 BUG();
108 return NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100109}
110
111static inline int get_gpio_index(int gpio)
112{
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700113 if (cpu_is_omap7xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100114 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100115 if (cpu_is_omap24xx())
116 return gpio & 0x1f;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700117 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800118 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100119 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100120}
121
122static inline int gpio_valid(int gpio)
123{
124 if (gpio < 0)
125 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -0800126 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300127 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100128 return -1;
129 return 0;
130 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100131 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133 if ((cpu_is_omap16xx()) && gpio < 64)
134 return 0;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700135 if (cpu_is_omap7xx() && gpio < 192)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136 return 0;
Tony Lindgren25d6f632010-08-02 14:21:39 +0300137 if (cpu_is_omap2420() && gpio < 128)
138 return 0;
139 if (cpu_is_omap2430() && gpio < 160)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100140 return 0;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700141 if ((cpu_is_omap34xx() || cpu_is_omap44xx()) && gpio < 192)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800142 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100143 return -1;
144}
145
146static int check_gpio(int gpio)
147{
Roel Kluind32b20f2009-11-17 14:39:03 -0800148 if (unlikely(gpio_valid(gpio) < 0)) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100149 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
150 dump_stack();
151 return -1;
152 }
153 return 0;
154}
155
156static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
157{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100158 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100159 u32 l;
160
161 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800162#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100163 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800164 reg += OMAP_MPUIO_IO_CNTL / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100165 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800166#endif
167#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100168 case METHOD_GPIO_1510:
169 reg += OMAP1510_GPIO_DIR_CONTROL;
170 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800171#endif
172#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100173 case METHOD_GPIO_1610:
174 reg += OMAP1610_GPIO_DIRECTION;
175 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800176#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100177#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100178 case METHOD_GPIO_7XX:
179 reg += OMAP7XX_GPIO_DIR_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700180 break;
181#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800182#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100183 case METHOD_GPIO_24XX:
184 reg += OMAP24XX_GPIO_OE;
185 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800186#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530187#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800188 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530189 reg += OMAP4_GPIO_OE;
190 break;
191#endif
David Brownelle5c56ed2006-12-06 17:13:59 -0800192 default:
193 WARN_ON(1);
194 return;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100195 }
196 l = __raw_readl(reg);
197 if (is_input)
198 l |= 1 << gpio;
199 else
200 l &= ~(1 << gpio);
201 __raw_writel(l, reg);
202}
203
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100204static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
205{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100206 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100207 u32 l = 0;
208
209 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800210#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100211 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800212 reg += OMAP_MPUIO_OUTPUT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100213 l = __raw_readl(reg);
214 if (enable)
215 l |= 1 << gpio;
216 else
217 l &= ~(1 << gpio);
218 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800219#endif
220#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100221 case METHOD_GPIO_1510:
222 reg += OMAP1510_GPIO_DATA_OUTPUT;
223 l = __raw_readl(reg);
224 if (enable)
225 l |= 1 << gpio;
226 else
227 l &= ~(1 << gpio);
228 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800229#endif
230#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100231 case METHOD_GPIO_1610:
232 if (enable)
233 reg += OMAP1610_GPIO_SET_DATAOUT;
234 else
235 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
236 l = 1 << gpio;
237 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800238#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100239#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100240 case METHOD_GPIO_7XX:
241 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700242 l = __raw_readl(reg);
243 if (enable)
244 l |= 1 << gpio;
245 else
246 l &= ~(1 << gpio);
247 break;
248#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800249#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100250 case METHOD_GPIO_24XX:
251 if (enable)
252 reg += OMAP24XX_GPIO_SETDATAOUT;
253 else
254 reg += OMAP24XX_GPIO_CLEARDATAOUT;
255 l = 1 << gpio;
256 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800257#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530258#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800259 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530260 if (enable)
261 reg += OMAP4_GPIO_SETDATAOUT;
262 else
263 reg += OMAP4_GPIO_CLEARDATAOUT;
264 l = 1 << gpio;
265 break;
266#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100267 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800268 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100269 return;
270 }
271 __raw_writel(l, reg);
272}
273
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300274static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100275{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100276 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100277
278 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800279 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100280 reg = bank->base;
281 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800282#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100283 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800284 reg += OMAP_MPUIO_INPUT_LATCH / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100285 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800286#endif
287#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100288 case METHOD_GPIO_1510:
289 reg += OMAP1510_GPIO_DATA_INPUT;
290 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800291#endif
292#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100293 case METHOD_GPIO_1610:
294 reg += OMAP1610_GPIO_DATAIN;
295 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800296#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100297#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100298 case METHOD_GPIO_7XX:
299 reg += OMAP7XX_GPIO_DATA_INPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700300 break;
301#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800302#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100303 case METHOD_GPIO_24XX:
304 reg += OMAP24XX_GPIO_DATAIN;
305 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800306#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530307#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800308 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530309 reg += OMAP4_GPIO_DATAIN;
310 break;
311#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100312 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800313 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100314 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100315 return (__raw_readl(reg)
316 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100317}
318
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300319static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
320{
321 void __iomem *reg;
322
323 if (check_gpio(gpio) < 0)
324 return -EINVAL;
325 reg = bank->base;
326
327 switch (bank->method) {
328#ifdef CONFIG_ARCH_OMAP1
329 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800330 reg += OMAP_MPUIO_OUTPUT / bank->stride;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300331 break;
332#endif
333#ifdef CONFIG_ARCH_OMAP15XX
334 case METHOD_GPIO_1510:
335 reg += OMAP1510_GPIO_DATA_OUTPUT;
336 break;
337#endif
338#ifdef CONFIG_ARCH_OMAP16XX
339 case METHOD_GPIO_1610:
340 reg += OMAP1610_GPIO_DATAOUT;
341 break;
342#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100343#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100344 case METHOD_GPIO_7XX:
345 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300346 break;
347#endif
Charulatha V9f096862010-05-14 12:05:27 -0700348#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300349 case METHOD_GPIO_24XX:
350 reg += OMAP24XX_GPIO_DATAOUT;
351 break;
352#endif
Charulatha V9f096862010-05-14 12:05:27 -0700353#ifdef CONFIG_ARCH_OMAP4
354 case METHOD_GPIO_44XX:
355 reg += OMAP4_GPIO_DATAOUT;
356 break;
357#endif
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300358 default:
359 return -EINVAL;
360 }
361
362 return (__raw_readl(reg) & (1 << get_gpio_index(gpio))) != 0;
363}
364
Tony Lindgren92105bb2005-09-07 17:20:26 +0100365#define MOD_REG_BIT(reg, bit_mask, set) \
366do { \
367 int l = __raw_readl(base + reg); \
368 if (set) l |= bit_mask; \
369 else l &= ~bit_mask; \
370 __raw_writel(l, base + reg); \
371} while(0)
372
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700373/**
374 * _set_gpio_debounce - low level gpio debounce time
375 * @bank: the gpio bank we're acting upon
376 * @gpio: the gpio number on this @gpio
377 * @debounce: debounce time to use
378 *
379 * OMAP's debounce time is in 31us steps so we need
380 * to convert and round up to the closest unit.
381 */
382static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
383 unsigned debounce)
384{
385 void __iomem *reg = bank->base;
386 u32 val;
387 u32 l;
388
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800389 if (!bank->dbck_flag)
390 return;
391
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700392 if (debounce < 32)
393 debounce = 0x01;
394 else if (debounce > 7936)
395 debounce = 0xff;
396 else
397 debounce = (debounce / 0x1f) - 1;
398
399 l = 1 << get_gpio_index(gpio);
400
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800401 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700402 reg += OMAP4_GPIO_DEBOUNCINGTIME;
403 else
404 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
405
406 __raw_writel(debounce, reg);
407
408 reg = bank->base;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800409 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700410 reg += OMAP4_GPIO_DEBOUNCENABLE;
411 else
412 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
413
414 val = __raw_readl(reg);
415
416 if (debounce) {
417 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800418 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700419 } else {
420 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800421 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700422 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300423 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700424
425 __raw_writel(val, reg);
426}
427
Tony Lindgren140455f2010-02-12 12:26:48 -0800428#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700429static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
430 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100431{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800432 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100433 u32 gpio_bit = 1 << gpio;
434
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530435 if (cpu_is_omap44xx()) {
436 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
437 trigger & IRQ_TYPE_LEVEL_LOW);
438 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
439 trigger & IRQ_TYPE_LEVEL_HIGH);
440 MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
441 trigger & IRQ_TYPE_EDGE_RISING);
442 MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
443 trigger & IRQ_TYPE_EDGE_FALLING);
444 } else {
445 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
446 trigger & IRQ_TYPE_LEVEL_LOW);
447 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
448 trigger & IRQ_TYPE_LEVEL_HIGH);
449 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
450 trigger & IRQ_TYPE_EDGE_RISING);
451 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
452 trigger & IRQ_TYPE_EDGE_FALLING);
453 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800454 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530455 if (cpu_is_omap44xx()) {
Colin Cross0622b252011-06-06 13:38:17 -0700456 MOD_REG_BIT(OMAP4_GPIO_IRQWAKEN0, gpio_bit,
457 trigger != 0);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530458 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000459 /*
460 * GPIO wakeup request can only be generated on edge
461 * transitions
462 */
463 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530464 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700465 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530466 else
467 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700468 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530469 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200470 }
471 /* This part needs to be executed always for OMAP34xx */
472 if (cpu_is_omap34xx() || (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000473 /*
474 * Log the edge gpio and manually trigger the IRQ
475 * after resume if the input level changes
476 * to avoid irq lost during PER RET/OFF mode
477 * Applies for omap2 non-wakeup gpio and all omap3 gpios
478 */
479 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800480 bank->enabled_non_wakeup_gpios |= gpio_bit;
481 else
482 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
483 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700484
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530485 if (cpu_is_omap44xx()) {
486 bank->level_mask =
487 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
488 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
489 } else {
490 bank->level_mask =
491 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
492 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
493 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100494}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800495#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100496
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800497#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800498/*
499 * This only applies to chips that can't do both rising and falling edge
500 * detection at once. For all other chips, this function is a noop.
501 */
502static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
503{
504 void __iomem *reg = bank->base;
505 u32 l = 0;
506
507 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800508 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800509 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800510 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800511#ifdef CONFIG_ARCH_OMAP15XX
512 case METHOD_GPIO_1510:
513 reg += OMAP1510_GPIO_INT_CONTROL;
514 break;
515#endif
516#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
517 case METHOD_GPIO_7XX:
518 reg += OMAP7XX_GPIO_INT_CONTROL;
519 break;
520#endif
521 default:
522 return;
523 }
524
525 l = __raw_readl(reg);
526 if ((l >> gpio) & 1)
527 l &= ~(1 << gpio);
528 else
529 l |= 1 << gpio;
530
531 __raw_writel(l, reg);
532}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800533#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800534
Tony Lindgren92105bb2005-09-07 17:20:26 +0100535static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
536{
537 void __iomem *reg = bank->base;
538 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100539
540 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800541#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100542 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800543 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100544 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000545 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800546 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100547 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100548 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100549 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100550 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100551 else
552 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100553 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800554#endif
555#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100556 case METHOD_GPIO_1510:
557 reg += OMAP1510_GPIO_INT_CONTROL;
558 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000559 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800560 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100561 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100562 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100563 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100565 else
566 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100567 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800568#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800569#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100570 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100571 if (gpio & 0x08)
572 reg += OMAP1610_GPIO_EDGE_CTRL2;
573 else
574 reg += OMAP1610_GPIO_EDGE_CTRL1;
575 gpio &= 0x07;
576 l = __raw_readl(reg);
577 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100578 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100579 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100580 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100581 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800582 if (trigger)
583 /* Enable wake-up during idle for dynamic tick */
584 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
585 else
586 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100587 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800588#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100589#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100590 case METHOD_GPIO_7XX:
591 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700592 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000593 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800594 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700595 if (trigger & IRQ_TYPE_EDGE_RISING)
596 l |= 1 << gpio;
597 else if (trigger & IRQ_TYPE_EDGE_FALLING)
598 l &= ~(1 << gpio);
599 else
600 goto bad;
601 break;
602#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800603#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100604 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800605 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800606 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200607 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800608#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100609 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100610 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100611 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100612 __raw_writel(l, reg);
613 return 0;
614bad:
615 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100616}
617
Lennert Buytenheke9191022010-11-29 11:17:17 +0100618static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100619{
620 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100621 unsigned gpio;
622 int retval;
David Brownella6472532008-03-03 04:33:30 -0800623 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100624
Lennert Buytenheke9191022010-11-29 11:17:17 +0100625 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
626 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100627 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100628 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100629
630 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100631 return -EINVAL;
632
David Brownelle5c56ed2006-12-06 17:13:59 -0800633 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100634 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800635
636 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800637 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800638 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100639 return -EINVAL;
640
Lennert Buytenheke9191022010-11-29 11:17:17 +0100641 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800642 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100643 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800644 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800645
646 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100647 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800648 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100649 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800650
Tony Lindgren92105bb2005-09-07 17:20:26 +0100651 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100652}
653
654static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
655{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100656 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100657
658 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800659#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100660 case METHOD_GPIO_1510:
661 reg += OMAP1510_GPIO_INT_STATUS;
662 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800663#endif
664#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100665 case METHOD_GPIO_1610:
666 reg += OMAP1610_GPIO_IRQSTATUS1;
667 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800668#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100669#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100670 case METHOD_GPIO_7XX:
671 reg += OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700672 break;
673#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800674#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100675 case METHOD_GPIO_24XX:
676 reg += OMAP24XX_GPIO_IRQSTATUS1;
677 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800678#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530679#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800680 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530681 reg += OMAP4_GPIO_IRQSTATUS0;
682 break;
683#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100684 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800685 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100686 return;
687 }
688 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300689
690 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800691 if (cpu_is_omap24xx() || cpu_is_omap34xx())
692 reg = bank->base + OMAP24XX_GPIO_IRQSTATUS2;
693 else if (cpu_is_omap44xx())
694 reg = bank->base + OMAP4_GPIO_IRQSTATUS1;
695
Kevin Hilmandf3c8512011-04-21 09:08:15 -0700696 if (cpu_is_omap24xx() || cpu_is_omap34xx() || cpu_is_omap44xx())
Roger Quadrosbedfd152009-04-23 11:10:50 -0700697 __raw_writel(gpio_mask, reg);
698
699 /* Flush posted write for the irq status to avoid spurious interrupts */
700 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100701}
702
703static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
704{
705 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
706}
707
Imre Deakea6dedd2006-06-26 16:16:00 -0700708static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
709{
710 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700711 int inv = 0;
712 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700713 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700714
715 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800716#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700717 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800718 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Imre Deak99c47702006-06-26 16:16:07 -0700719 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700720 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800721#endif
722#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700723 case METHOD_GPIO_1510:
724 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700725 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700726 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800727#endif
728#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700729 case METHOD_GPIO_1610:
730 reg += OMAP1610_GPIO_IRQENABLE1;
731 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800732#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100733#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100734 case METHOD_GPIO_7XX:
735 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700736 inv = 1;
737 break;
738#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800739#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Imre Deakea6dedd2006-06-26 16:16:00 -0700740 case METHOD_GPIO_24XX:
741 reg += OMAP24XX_GPIO_IRQENABLE1;
742 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800743#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530744#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800745 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530746 reg += OMAP4_GPIO_IRQSTATUSSET0;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530747 break;
748#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700749 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800750 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700751 return 0;
752 }
753
Imre Deak99c47702006-06-26 16:16:07 -0700754 l = __raw_readl(reg);
755 if (inv)
756 l = ~l;
757 l &= mask;
758 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700759}
760
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100761static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
762{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100763 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100764 u32 l;
765
766 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800767#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100768 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800769 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100770 l = __raw_readl(reg);
771 if (enable)
772 l &= ~(gpio_mask);
773 else
774 l |= gpio_mask;
775 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800776#endif
777#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100778 case METHOD_GPIO_1510:
779 reg += OMAP1510_GPIO_INT_MASK;
780 l = __raw_readl(reg);
781 if (enable)
782 l &= ~(gpio_mask);
783 else
784 l |= gpio_mask;
785 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800786#endif
787#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100788 case METHOD_GPIO_1610:
789 if (enable)
790 reg += OMAP1610_GPIO_SET_IRQENABLE1;
791 else
792 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
793 l = gpio_mask;
794 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800795#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100796#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100797 case METHOD_GPIO_7XX:
798 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700799 l = __raw_readl(reg);
800 if (enable)
801 l &= ~(gpio_mask);
802 else
803 l |= gpio_mask;
804 break;
805#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800806#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100807 case METHOD_GPIO_24XX:
808 if (enable)
809 reg += OMAP24XX_GPIO_SETIRQENABLE1;
810 else
811 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
812 l = gpio_mask;
813 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800814#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530815#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800816 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530817 if (enable)
818 reg += OMAP4_GPIO_IRQSTATUSSET0;
819 else
820 reg += OMAP4_GPIO_IRQSTATUSCLR0;
821 l = gpio_mask;
822 break;
823#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100824 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800825 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100826 return;
827 }
828 __raw_writel(l, reg);
829}
830
831static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
832{
833 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
834}
835
Tony Lindgren92105bb2005-09-07 17:20:26 +0100836/*
837 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
838 * 1510 does not seem to have a wake-up register. If JTAG is connected
839 * to the target, system will wake up always on GPIO events. While
840 * system is running all registered GPIO interrupts need to have wake-up
841 * enabled. When system is suspended, only selected GPIO interrupts need
842 * to have wake-up enabled.
843 */
844static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
845{
Tony Lindgren4cc64202010-01-08 10:29:05 -0800846 unsigned long uninitialized_var(flags);
David Brownella6472532008-03-03 04:33:30 -0800847
Tony Lindgren92105bb2005-09-07 17:20:26 +0100848 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800849#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800850 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100851 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800852 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700853 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100854 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700855 else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100856 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800857 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100858 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800859#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800860#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800861 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800862 case METHOD_GPIO_44XX:
David Brownell11a78b72006-12-06 17:14:11 -0800863 if (bank->non_wakeup_gpios & (1 << gpio)) {
864 printk(KERN_ERR "Unable to modify wakeup on "
865 "non-wakeup GPIO%d\n",
Kevin Hilmand5f46242011-04-21 09:23:00 -0700866 (bank - gpio_bank) * bank->width + gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800867 return -EINVAL;
868 }
David Brownella6472532008-03-03 04:33:30 -0800869 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700870 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800871 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700872 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800873 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800874 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800875 return 0;
876#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100877 default:
878 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
879 bank->method);
880 return -EINVAL;
881 }
882}
883
Tony Lindgren4196dd62006-09-25 12:41:38 +0300884static void _reset_gpio(struct gpio_bank *bank, int gpio)
885{
886 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
887 _set_gpio_irqenable(bank, gpio, 0);
888 _clear_gpio_irqstatus(bank, gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100889 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300890}
891
Tony Lindgren92105bb2005-09-07 17:20:26 +0100892/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100893static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100894{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100895 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100896 struct gpio_bank *bank;
897 int retval;
898
899 if (check_gpio(gpio) < 0)
900 return -ENODEV;
Lennert Buytenheke9191022010-11-29 11:17:17 +0100901 bank = irq_data_get_irq_chip_data(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100902 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100903
904 return retval;
905}
906
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800907static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100908{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800909 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800910 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100911
David Brownella6472532008-03-03 04:33:30 -0800912 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100913
Tony Lindgren4196dd62006-09-25 12:41:38 +0300914 /* Set trigger to none. You need to enable the desired trigger with
915 * request_irq() or set_irq_type().
916 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800917 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100918
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000919#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100920 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100921 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100922
Tony Lindgren92105bb2005-09-07 17:20:26 +0100923 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100924 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800925 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100926 }
927#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800928 if (!cpu_class_is_omap1()) {
929 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700930 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800931 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700932
933 if (cpu_is_omap24xx() || cpu_is_omap34xx())
934 reg += OMAP24XX_GPIO_CTRL;
935 else if (cpu_is_omap44xx())
936 reg += OMAP4_GPIO_CTRL;
937 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800938 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700939 ctrl &= 0xFFFFFFFE;
940 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800941 }
942 bank->mod_usage |= 1 << offset;
943 }
David Brownella6472532008-03-03 04:33:30 -0800944 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100945
946 return 0;
947}
948
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800949static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100950{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800951 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800952 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100953
David Brownella6472532008-03-03 04:33:30 -0800954 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100955#ifdef CONFIG_ARCH_OMAP16XX
956 if (bank->method == METHOD_GPIO_1610) {
957 /* Disable wake-up during idle for dynamic tick */
958 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800959 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100960 }
961#endif
Charulatha V9f096862010-05-14 12:05:27 -0700962#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
963 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100964 /* Disable wake-up during idle for dynamic tick */
965 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800966 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100967 }
968#endif
Charulatha V9f096862010-05-14 12:05:27 -0700969#ifdef CONFIG_ARCH_OMAP4
970 if (bank->method == METHOD_GPIO_44XX) {
971 /* Disable wake-up during idle for dynamic tick */
972 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
973 __raw_writel(1 << offset, reg);
974 }
975#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800976 if (!cpu_class_is_omap1()) {
977 bank->mod_usage &= ~(1 << offset);
978 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700979 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800980 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700981
982 if (cpu_is_omap24xx() || cpu_is_omap34xx())
983 reg += OMAP24XX_GPIO_CTRL;
984 else if (cpu_is_omap44xx())
985 reg += OMAP4_GPIO_CTRL;
986 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800987 /* Module is disabled, clocks are gated */
988 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700989 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800990 }
991 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800992 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800993 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100994}
995
996/*
997 * We need to unmask the GPIO bank interrupt as soon as possible to
998 * avoid missing GPIO interrupts for other lines in the bank.
999 * Then we need to mask-read-clear-unmask the triggered GPIO lines
1000 * in the bank to avoid missing nested interrupts for a GPIO line.
1001 * If we wait to unmask individual GPIO lines in the bank after the
1002 * line's interrupt handler has been run, we may miss some nested
1003 * interrupts.
1004 */
Russell King10dd5ce2006-11-23 11:41:32 +00001005static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001006{
Tony Lindgren92105bb2005-09-07 17:20:26 +01001007 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001008 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -08001009 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001010 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -07001011 u32 retrigger = 0;
1012 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +00001013 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001014
Will Deaconee144182011-02-21 13:46:08 +00001015 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001016
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001017 bank = irq_get_handler_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -08001018#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001019 if (bank->method == METHOD_MPUIO)
Tony Lindgren5de62b82010-12-07 16:26:58 -08001020 isr_reg = bank->base +
1021 OMAP_MPUIO_GPIO_INT / bank->stride;
David Brownelle5c56ed2006-12-06 17:13:59 -08001022#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001023#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001024 if (bank->method == METHOD_GPIO_1510)
1025 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
1026#endif
1027#if defined(CONFIG_ARCH_OMAP16XX)
1028 if (bank->method == METHOD_GPIO_1610)
1029 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
1030#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +01001031#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +01001032 if (bank->method == METHOD_GPIO_7XX)
1033 isr_reg = bank->base + OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001034#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001035#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001036 if (bank->method == METHOD_GPIO_24XX)
1037 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1038#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301039#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001040 if (bank->method == METHOD_GPIO_44XX)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301041 isr_reg = bank->base + OMAP4_GPIO_IRQSTATUS0;
1042#endif
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -08001043
1044 if (WARN_ON(!isr_reg))
1045 goto exit;
1046
Tony Lindgren92105bb2005-09-07 17:20:26 +01001047 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001048 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -07001049 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001050
Imre Deakea6dedd2006-06-26 16:16:00 -07001051 enabled = _get_gpio_irqbank_mask(bank);
1052 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001053
1054 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1055 isr &= 0x0000ffff;
1056
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001057 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001058 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -07001059 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001060
1061 /* clear edge sensitive interrupts before handler(s) are
1062 called so that we don't miss any interrupt occurred while
1063 executing them */
1064 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1065 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1066 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1067
1068 /* if there is only edge sensitive GPIO pin interrupts
1069 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -07001070 if (!level_mask && !unmasked) {
1071 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +00001072 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -07001073 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001074
Imre Deakea6dedd2006-06-26 16:16:00 -07001075 isr |= retrigger;
1076 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001077 if (!isr)
1078 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001079
Tony Lindgren92105bb2005-09-07 17:20:26 +01001080 gpio_irq = bank->virtual_irq_start;
1081 for (; isr != 0; isr >>= 1, gpio_irq++) {
Cory Maccarrone4318f362010-01-08 10:29:04 -08001082 gpio_index = get_gpio_index(irq_to_gpio(gpio_irq));
1083
Tony Lindgren92105bb2005-09-07 17:20:26 +01001084 if (!(isr & 1))
1085 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001086
Cory Maccarrone4318f362010-01-08 10:29:04 -08001087#ifdef CONFIG_ARCH_OMAP1
1088 /*
1089 * Some chips can't respond to both rising and falling
1090 * at the same time. If this irq was requested with
1091 * both flags, we need to flip the ICR data for the IRQ
1092 * to respond to the IRQ for the opposite direction.
1093 * This will be indicated in the bank toggle_mask.
1094 */
1095 if (bank->toggle_mask & (1 << gpio_index))
1096 _toggle_gpio_edge_triggering(bank, gpio_index);
1097#endif
1098
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +01001099 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001100 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001101 }
Imre Deakea6dedd2006-06-26 16:16:00 -07001102 /* if bank has any level sensitive GPIO pin interrupt
1103 configured, we must unmask the bank interrupt only after
1104 handler(s) are executed in order to avoid spurious bank
1105 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -08001106exit:
Imre Deakea6dedd2006-06-26 16:16:00 -07001107 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +00001108 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001109}
1110
Lennert Buytenheke9191022010-11-29 11:17:17 +01001111static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +03001112{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001113 unsigned int gpio = d->irq - IH_GPIO_BASE;
1114 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -07001115 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +03001116
Colin Cross85ec7b92011-06-06 13:38:18 -07001117 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001118 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -07001119 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001120}
1121
Lennert Buytenheke9191022010-11-29 11:17:17 +01001122static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001123{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001124 unsigned int gpio = d->irq - IH_GPIO_BASE;
1125 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001126
1127 _clear_gpio_irqstatus(bank, gpio);
1128}
1129
Lennert Buytenheke9191022010-11-29 11:17:17 +01001130static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001131{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001132 unsigned int gpio = d->irq - IH_GPIO_BASE;
1133 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -07001134 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001135
Colin Cross85ec7b92011-06-06 13:38:18 -07001136 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001137 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman55b60192009-06-04 15:57:10 -07001138 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -07001139 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001140}
1141
Lennert Buytenheke9191022010-11-29 11:17:17 +01001142static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001143{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001144 unsigned int gpio = d->irq - IH_GPIO_BASE;
1145 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001146 unsigned int irq_mask = 1 << get_gpio_index(gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +01001147 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -07001148 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -07001149
Colin Cross85ec7b92011-06-06 13:38:18 -07001150 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -07001151 if (trigger)
1152 _set_gpio_triggering(bank, get_gpio_index(gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001153
1154 /* For level-triggered GPIOs, the clearing must be done after
1155 * the HW source is cleared, thus after the handler has run */
1156 if (bank->level_mask & irq_mask) {
1157 _set_gpio_irqenable(bank, gpio, 0);
1158 _clear_gpio_irqstatus(bank, gpio);
1159 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001160
Kevin Hilman4de8c752008-01-16 21:56:14 -08001161 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -07001162 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001163}
1164
David Brownelle5c56ed2006-12-06 17:13:59 -08001165static struct irq_chip gpio_irq_chip = {
1166 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +01001167 .irq_shutdown = gpio_irq_shutdown,
1168 .irq_ack = gpio_ack_irq,
1169 .irq_mask = gpio_mask_irq,
1170 .irq_unmask = gpio_unmask_irq,
1171 .irq_set_type = gpio_irq_type,
1172 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -08001173};
1174
1175/*---------------------------------------------------------------------*/
1176
1177#ifdef CONFIG_ARCH_OMAP1
1178
1179/* MPUIO uses the always-on 32k clock */
1180
Lennert Buytenheke9191022010-11-29 11:17:17 +01001181static void mpuio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001182{
1183 /* The ISR is reset automatically, so do nothing here. */
1184}
1185
Lennert Buytenheke9191022010-11-29 11:17:17 +01001186static void mpuio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001187{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001188 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1189 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001190
1191 _set_gpio_irqenable(bank, gpio, 0);
1192}
1193
Lennert Buytenheke9191022010-11-29 11:17:17 +01001194static void mpuio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001195{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001196 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1197 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001198
1199 _set_gpio_irqenable(bank, gpio, 1);
1200}
1201
David Brownelle5c56ed2006-12-06 17:13:59 -08001202static struct irq_chip mpuio_irq_chip = {
1203 .name = "MPUIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +01001204 .irq_ack = mpuio_ack_irq,
1205 .irq_mask = mpuio_mask_irq,
1206 .irq_unmask = mpuio_unmask_irq,
1207 .irq_set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001208#ifdef CONFIG_ARCH_OMAP16XX
1209 /* REVISIT: assuming only 16xx supports MPUIO wake events */
Lennert Buytenheke9191022010-11-29 11:17:17 +01001210 .irq_set_wake = gpio_wake_enable,
David Brownell11a78b72006-12-06 17:14:11 -08001211#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001212};
1213
David Brownelle5c56ed2006-12-06 17:13:59 -08001214
1215#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1216
David Brownell11a78b72006-12-06 17:14:11 -08001217
1218#ifdef CONFIG_ARCH_OMAP16XX
1219
1220#include <linux/platform_device.h>
1221
Magnus Damm79ee0312009-07-08 13:22:04 +02001222static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001223{
Magnus Damm79ee0312009-07-08 13:22:04 +02001224 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001225 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001226 void __iomem *mask_reg = bank->base +
1227 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001228 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001229
David Brownella6472532008-03-03 04:33:30 -08001230 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001231 bank->saved_wakeup = __raw_readl(mask_reg);
1232 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001233 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001234
1235 return 0;
1236}
1237
Magnus Damm79ee0312009-07-08 13:22:04 +02001238static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001239{
Magnus Damm79ee0312009-07-08 13:22:04 +02001240 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001241 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001242 void __iomem *mask_reg = bank->base +
1243 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001244 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001245
David Brownella6472532008-03-03 04:33:30 -08001246 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001247 __raw_writel(bank->saved_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
Alexey Dobriyan47145212009-12-14 18:00:08 -08001253static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +02001254 .suspend_noirq = omap_mpuio_suspend_noirq,
1255 .resume_noirq = omap_mpuio_resume_noirq,
1256};
1257
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001258/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -08001259static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -08001260 .driver = {
1261 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +02001262 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -08001263 },
1264};
1265
1266static struct platform_device omap_mpuio_device = {
1267 .name = "mpuio",
1268 .id = -1,
1269 .dev = {
1270 .driver = &omap_mpuio_driver.driver,
1271 }
1272 /* could list the /proc/iomem resources */
1273};
1274
1275static inline void mpuio_init(void)
1276{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001277 struct gpio_bank *bank = get_gpio_bank(OMAP_MPUIO(0));
1278 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -07001279
David Brownell11a78b72006-12-06 17:14:11 -08001280 if (platform_driver_register(&omap_mpuio_driver) == 0)
1281 (void) platform_device_register(&omap_mpuio_device);
1282}
1283
1284#else
1285static inline void mpuio_init(void) {}
1286#endif /* 16xx */
1287
David Brownelle5c56ed2006-12-06 17:13:59 -08001288#else
1289
1290extern struct irq_chip mpuio_irq_chip;
1291
1292#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001293static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001294
1295#endif
1296
1297/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001298
David Brownell52e31342008-03-03 12:43:23 -08001299/* REVISIT these are stupid implementations! replace by ones that
1300 * don't switch on METHOD_* and which mostly avoid spinlocks
1301 */
1302
1303static int gpio_input(struct gpio_chip *chip, unsigned offset)
1304{
1305 struct gpio_bank *bank;
1306 unsigned long flags;
1307
1308 bank = container_of(chip, struct gpio_bank, chip);
1309 spin_lock_irqsave(&bank->lock, flags);
1310 _set_gpio_direction(bank, offset, 1);
1311 spin_unlock_irqrestore(&bank->lock, flags);
1312 return 0;
1313}
1314
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001315static int gpio_is_input(struct gpio_bank *bank, int mask)
1316{
1317 void __iomem *reg = bank->base;
1318
1319 switch (bank->method) {
1320 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -08001321 reg += OMAP_MPUIO_IO_CNTL / bank->stride;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001322 break;
1323 case METHOD_GPIO_1510:
1324 reg += OMAP1510_GPIO_DIR_CONTROL;
1325 break;
1326 case METHOD_GPIO_1610:
1327 reg += OMAP1610_GPIO_DIRECTION;
1328 break;
Alistair Buxton7c006922009-09-22 10:02:58 +01001329 case METHOD_GPIO_7XX:
1330 reg += OMAP7XX_GPIO_DIR_CONTROL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001331 break;
1332 case METHOD_GPIO_24XX:
1333 reg += OMAP24XX_GPIO_OE;
1334 break;
Charulatha V9f096862010-05-14 12:05:27 -07001335 case METHOD_GPIO_44XX:
1336 reg += OMAP4_GPIO_OE;
1337 break;
1338 default:
1339 WARN_ONCE(1, "gpio_is_input: incorrect OMAP GPIO method");
1340 return -EINVAL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001341 }
1342 return __raw_readl(reg) & mask;
1343}
1344
David Brownell52e31342008-03-03 12:43:23 -08001345static int gpio_get(struct gpio_chip *chip, unsigned offset)
1346{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001347 struct gpio_bank *bank;
1348 void __iomem *reg;
1349 int gpio;
1350 u32 mask;
1351
1352 gpio = chip->base + offset;
1353 bank = get_gpio_bank(gpio);
1354 reg = bank->base;
1355 mask = 1 << get_gpio_index(gpio);
1356
1357 if (gpio_is_input(bank, mask))
1358 return _get_gpio_datain(bank, gpio);
1359 else
1360 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -08001361}
1362
1363static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1364{
1365 struct gpio_bank *bank;
1366 unsigned long flags;
1367
1368 bank = container_of(chip, struct gpio_bank, chip);
1369 spin_lock_irqsave(&bank->lock, flags);
1370 _set_gpio_dataout(bank, offset, value);
1371 _set_gpio_direction(bank, offset, 0);
1372 spin_unlock_irqrestore(&bank->lock, flags);
1373 return 0;
1374}
1375
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001376static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
1377 unsigned debounce)
1378{
1379 struct gpio_bank *bank;
1380 unsigned long flags;
1381
1382 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001383
1384 if (!bank->dbck) {
1385 bank->dbck = clk_get(bank->dev, "dbclk");
1386 if (IS_ERR(bank->dbck))
1387 dev_err(bank->dev, "Could not get gpio dbck\n");
1388 }
1389
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001390 spin_lock_irqsave(&bank->lock, flags);
1391 _set_gpio_debounce(bank, offset, debounce);
1392 spin_unlock_irqrestore(&bank->lock, flags);
1393
1394 return 0;
1395}
1396
David Brownell52e31342008-03-03 12:43:23 -08001397static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1398{
1399 struct gpio_bank *bank;
1400 unsigned long flags;
1401
1402 bank = container_of(chip, struct gpio_bank, chip);
1403 spin_lock_irqsave(&bank->lock, flags);
1404 _set_gpio_dataout(bank, offset, value);
1405 spin_unlock_irqrestore(&bank->lock, flags);
1406}
1407
David Brownella007b702008-12-10 17:35:25 -08001408static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1409{
1410 struct gpio_bank *bank;
1411
1412 bank = container_of(chip, struct gpio_bank, chip);
1413 return bank->virtual_irq_start + offset;
1414}
1415
David Brownell52e31342008-03-03 12:43:23 -08001416/*---------------------------------------------------------------------*/
1417
Tony Lindgren9a748052010-12-07 16:26:56 -08001418static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001419{
1420 u32 rev;
1421
Tony Lindgren9a748052010-12-07 16:26:56 -08001422 if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
1423 rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001424 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001425 rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001426 else if (cpu_is_omap44xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001427 rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001428 else
1429 return;
1430
1431 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1432 (rev >> 4) & 0x0f, rev & 0x0f);
1433}
1434
David Brownell8ba55c52008-02-26 11:10:50 -08001435/* This lock class tells lockdep that GPIO irqs are in a different
1436 * category than their parents, so it won't report false recursion.
1437 */
1438static struct lock_class_key gpio_lock_class;
1439
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001440static inline int init_gpio_info(struct platform_device *pdev)
1441{
1442 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1443 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1444 GFP_KERNEL);
1445 if (!gpio_bank) {
1446 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1447 return -ENOMEM;
1448 }
1449 return 0;
1450}
1451
1452/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001453static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1454{
1455 if (cpu_class_is_omap2()) {
1456 if (cpu_is_omap44xx()) {
1457 __raw_writel(0xffffffff, bank->base +
1458 OMAP4_GPIO_IRQSTATUSCLR0);
1459 __raw_writel(0x00000000, bank->base +
1460 OMAP4_GPIO_DEBOUNCENABLE);
1461 /* Initialize interface clk ungated, module enabled */
1462 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1463 } else if (cpu_is_omap34xx()) {
1464 __raw_writel(0x00000000, bank->base +
1465 OMAP24XX_GPIO_IRQENABLE1);
1466 __raw_writel(0xffffffff, bank->base +
1467 OMAP24XX_GPIO_IRQSTATUS1);
1468 __raw_writel(0x00000000, bank->base +
1469 OMAP24XX_GPIO_DEBOUNCE_EN);
1470
1471 /* Initialize interface clk ungated, module enabled */
1472 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1473 } else if (cpu_is_omap24xx()) {
1474 static const u32 non_wakeup_gpios[] = {
1475 0xe203ffc0, 0x08700040
1476 };
1477 if (id < ARRAY_SIZE(non_wakeup_gpios))
1478 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1479 }
1480 } else if (cpu_class_is_omap1()) {
1481 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001482 __raw_writew(0xffff, bank->base +
1483 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001484 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1485 __raw_writew(0xffff, bank->base
1486 + OMAP1510_GPIO_INT_MASK);
1487 __raw_writew(0x0000, bank->base
1488 + OMAP1510_GPIO_INT_STATUS);
1489 }
1490 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1491 __raw_writew(0x0000, bank->base
1492 + OMAP1610_GPIO_IRQENABLE1);
1493 __raw_writew(0xffff, bank->base
1494 + OMAP1610_GPIO_IRQSTATUS1);
1495 __raw_writew(0x0014, bank->base
1496 + OMAP1610_GPIO_SYSCONFIG);
1497
1498 /*
1499 * Enable system clock for GPIO module.
1500 * The CAM_CLK_CTRL *is* really the right place.
1501 */
1502 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1503 ULPD_CAM_CLK_CTRL);
1504 }
1505 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1506 __raw_writel(0xffffffff, bank->base
1507 + OMAP7XX_GPIO_INT_MASK);
1508 __raw_writel(0x00000000, bank->base
1509 + OMAP7XX_GPIO_INT_STATUS);
1510 }
1511 }
1512}
1513
Russell Kingd52b31d2011-05-27 13:56:12 -07001514static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001515{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001516 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001517 static int gpio;
1518
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001519 bank->mod_usage = 0;
1520 /*
1521 * REVISIT eventually switch from OMAP-specific gpio structs
1522 * over to the generic ones
1523 */
1524 bank->chip.request = omap_gpio_request;
1525 bank->chip.free = omap_gpio_free;
1526 bank->chip.direction_input = gpio_input;
1527 bank->chip.get = gpio_get;
1528 bank->chip.direction_output = gpio_output;
1529 bank->chip.set_debounce = gpio_debounce;
1530 bank->chip.set = gpio_set;
1531 bank->chip.to_irq = gpio_2irq;
1532 if (bank_is_mpuio(bank)) {
1533 bank->chip.label = "mpuio";
1534#ifdef CONFIG_ARCH_OMAP16XX
1535 bank->chip.dev = &omap_mpuio_device.dev;
1536#endif
1537 bank->chip.base = OMAP_MPUIO(0);
1538 } else {
1539 bank->chip.label = "gpio";
1540 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001541 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001542 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001543 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001544
1545 gpiochip_add(&bank->chip);
1546
1547 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001548 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001549 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001550 irq_set_chip_data(j, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001551 if (bank_is_mpuio(bank))
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001552 irq_set_chip(j, &mpuio_irq_chip);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001553 else
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001554 irq_set_chip(j, &gpio_irq_chip);
1555 irq_set_handler(j, handle_simple_irq);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001556 set_irq_flags(j, IRQF_VALID);
1557 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001558 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1559 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001560}
1561
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001562static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001563{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001564 static int gpio_init_done;
1565 struct omap_gpio_platform_data *pdata;
1566 struct resource *res;
1567 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001568 struct gpio_bank *bank;
1569
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001570 if (!pdev->dev.platform_data)
1571 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001572
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001573 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001574
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001575 if (!gpio_init_done) {
1576 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001577
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001578 ret = init_gpio_info(pdev);
1579 if (ret)
1580 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001581 }
1582
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001583 id = pdev->id;
1584 bank = &gpio_bank[id];
1585
1586 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1587 if (unlikely(!res)) {
1588 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1589 return -ENODEV;
1590 }
1591
1592 bank->irq = res->start;
1593 bank->virtual_irq_start = pdata->virtual_irq_start;
1594 bank->method = pdata->bank_type;
1595 bank->dev = &pdev->dev;
1596 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001597 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001598 bank->width = pdata->bank_width;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001599
1600 spin_lock_init(&bank->lock);
1601
1602 /* Static mapping, never released */
1603 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1604 if (unlikely(!res)) {
1605 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1606 return -ENODEV;
1607 }
1608
1609 bank->base = ioremap(res->start, resource_size(res));
1610 if (!bank->base) {
1611 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1612 return -ENOMEM;
1613 }
1614
1615 pm_runtime_enable(bank->dev);
1616 pm_runtime_get_sync(bank->dev);
1617
1618 omap_gpio_mod_init(bank, id);
1619 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001620 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001621
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001622 if (!gpio_init_done)
1623 gpio_init_done = 1;
1624
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001625 return 0;
1626}
1627
Tony Lindgren140455f2010-02-12 12:26:48 -08001628#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001629static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001630{
1631 int i;
1632
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001633 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001634 return 0;
1635
1636 for (i = 0; i < gpio_bank_count; i++) {
1637 struct gpio_bank *bank = &gpio_bank[i];
1638 void __iomem *wake_status;
1639 void __iomem *wake_clear;
1640 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001641 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001642
1643 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001644#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001645 case METHOD_GPIO_1610:
1646 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1647 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1648 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1649 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001650#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001651#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001652 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001653 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001654 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1655 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1656 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001657#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301658#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001659 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301660 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1661 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1662 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1663 break;
1664#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001665 default:
1666 continue;
1667 }
1668
David Brownella6472532008-03-03 04:33:30 -08001669 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001670 bank->saved_wakeup = __raw_readl(wake_status);
1671 __raw_writel(0xffffffff, wake_clear);
1672 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001673 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001674 }
1675
1676 return 0;
1677}
1678
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001679static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001680{
1681 int i;
1682
Tero Kristo723fdb72008-11-26 14:35:16 -08001683 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001684 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001685
1686 for (i = 0; i < gpio_bank_count; i++) {
1687 struct gpio_bank *bank = &gpio_bank[i];
1688 void __iomem *wake_clear;
1689 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001690 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001691
1692 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001693#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001694 case METHOD_GPIO_1610:
1695 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1696 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1697 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001698#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001699#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001700 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001701 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1702 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001703 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001704#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301705#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001706 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301707 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1708 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1709 break;
1710#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001711 default:
1712 continue;
1713 }
1714
David Brownella6472532008-03-03 04:33:30 -08001715 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001716 __raw_writel(0xffffffff, wake_clear);
1717 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001718 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001719 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001720}
1721
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001722static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001723 .suspend = omap_gpio_suspend,
1724 .resume = omap_gpio_resume,
1725};
1726
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001727#endif
1728
Tony Lindgren140455f2010-02-12 12:26:48 -08001729#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001730
1731static int workaround_enabled;
1732
Paul Walmsley72e06d02010-12-21 21:05:16 -07001733void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001734{
1735 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001736 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001737
Tero Kristoa118b5f2008-12-22 14:27:12 +02001738 if (cpu_is_omap34xx())
1739 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001740
Tero Kristoa118b5f2008-12-22 14:27:12 +02001741 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001742 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001743 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001744 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001745
Kevin Hilman0aed04352010-09-22 16:06:27 -07001746 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001747 clk_disable(bank->dbck);
1748
Paul Walmsley72e06d02010-12-21 21:05:16 -07001749 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001750 continue;
1751
1752 /* If going to OFF, remove triggering for all
1753 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1754 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001755 if (!(bank->enabled_non_wakeup_gpios))
1756 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001757
1758 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1759 bank->saved_datain = __raw_readl(bank->base +
1760 OMAP24XX_GPIO_DATAIN);
1761 l1 = __raw_readl(bank->base +
1762 OMAP24XX_GPIO_FALLINGDETECT);
1763 l2 = __raw_readl(bank->base +
1764 OMAP24XX_GPIO_RISINGDETECT);
1765 }
1766
1767 if (cpu_is_omap44xx()) {
1768 bank->saved_datain = __raw_readl(bank->base +
1769 OMAP4_GPIO_DATAIN);
1770 l1 = __raw_readl(bank->base +
1771 OMAP4_GPIO_FALLINGDETECT);
1772 l2 = __raw_readl(bank->base +
1773 OMAP4_GPIO_RISINGDETECT);
1774 }
1775
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001776 bank->saved_fallingdetect = l1;
1777 bank->saved_risingdetect = l2;
1778 l1 &= ~bank->enabled_non_wakeup_gpios;
1779 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001780
1781 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1782 __raw_writel(l1, bank->base +
1783 OMAP24XX_GPIO_FALLINGDETECT);
1784 __raw_writel(l2, bank->base +
1785 OMAP24XX_GPIO_RISINGDETECT);
1786 }
1787
1788 if (cpu_is_omap44xx()) {
1789 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1790 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1791 }
1792
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001793 c++;
1794 }
1795 if (!c) {
1796 workaround_enabled = 0;
1797 return;
1798 }
1799 workaround_enabled = 1;
1800}
1801
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001802void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001803{
1804 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001805 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001806
Tero Kristoa118b5f2008-12-22 14:27:12 +02001807 if (cpu_is_omap34xx())
1808 min = 1;
1809 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001810 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001811 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001812 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001813
Kevin Hilman0aed04352010-09-22 16:06:27 -07001814 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001815 clk_enable(bank->dbck);
1816
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001817 if (!workaround_enabled)
1818 continue;
1819
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001820 if (!(bank->enabled_non_wakeup_gpios))
1821 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001822
1823 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1824 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001825 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001826 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001827 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001828 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1829 }
1830
1831 if (cpu_is_omap44xx()) {
1832 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301833 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001834 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301835 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001836 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1837 }
1838
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001839 /* Check if any of the non-wakeup interrupt GPIOs have changed
1840 * state. If so, generate an IRQ by software. This is
1841 * horribly racy, but it's the best we can do to work around
1842 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001843 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001844 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001845
1846 /*
1847 * No need to generate IRQs for the rising edge for gpio IRQs
1848 * configured with falling edge only; and vice versa.
1849 */
1850 gen0 = l & bank->saved_fallingdetect;
1851 gen0 &= bank->saved_datain;
1852
1853 gen1 = l & bank->saved_risingdetect;
1854 gen1 &= ~(bank->saved_datain);
1855
1856 /* FIXME: Consider GPIO IRQs with level detections properly! */
1857 gen = l & (~(bank->saved_fallingdetect) &
1858 ~(bank->saved_risingdetect));
1859 /* Consider all GPIO IRQs needed to be updated */
1860 gen |= gen0 | gen1;
1861
1862 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001863 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001864
Sergio Aguirref00d6492010-03-03 16:21:08 +00001865 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001866 old0 = __raw_readl(bank->base +
1867 OMAP24XX_GPIO_LEVELDETECT0);
1868 old1 = __raw_readl(bank->base +
1869 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001870 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001871 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001872 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001873 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001874 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001875 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001876 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001877 OMAP24XX_GPIO_LEVELDETECT1);
1878 }
1879
1880 if (cpu_is_omap44xx()) {
1881 old0 = __raw_readl(bank->base +
1882 OMAP4_GPIO_LEVELDETECT0);
1883 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301884 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001885 __raw_writel(old0 | l, bank->base +
1886 OMAP4_GPIO_LEVELDETECT0);
1887 __raw_writel(old1 | l, bank->base +
1888 OMAP4_GPIO_LEVELDETECT1);
1889 __raw_writel(old0, bank->base +
1890 OMAP4_GPIO_LEVELDETECT0);
1891 __raw_writel(old1, bank->base +
1892 OMAP4_GPIO_LEVELDETECT1);
1893 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001894 }
1895 }
1896
1897}
1898
Tony Lindgren92105bb2005-09-07 17:20:26 +01001899#endif
1900
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001901#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301902/* save the registers of bank 2-6 */
1903void omap_gpio_save_context(void)
1904{
1905 int i;
1906
1907 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1908 for (i = 1; i < gpio_bank_count; i++) {
1909 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301910 gpio_context[i].irqenable1 =
1911 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1912 gpio_context[i].irqenable2 =
1913 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1914 gpio_context[i].wake_en =
1915 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1916 gpio_context[i].ctrl =
1917 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1918 gpio_context[i].oe =
1919 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1920 gpio_context[i].leveldetect0 =
1921 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1922 gpio_context[i].leveldetect1 =
1923 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1924 gpio_context[i].risingdetect =
1925 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1926 gpio_context[i].fallingdetect =
1927 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1928 gpio_context[i].dataout =
1929 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301930 }
1931}
1932
1933/* restore the required registers of bank 2-6 */
1934void omap_gpio_restore_context(void)
1935{
1936 int i;
1937
1938 for (i = 1; i < gpio_bank_count; i++) {
1939 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301940 __raw_writel(gpio_context[i].irqenable1,
1941 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1942 __raw_writel(gpio_context[i].irqenable2,
1943 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1944 __raw_writel(gpio_context[i].wake_en,
1945 bank->base + OMAP24XX_GPIO_WAKE_EN);
1946 __raw_writel(gpio_context[i].ctrl,
1947 bank->base + OMAP24XX_GPIO_CTRL);
1948 __raw_writel(gpio_context[i].oe,
1949 bank->base + OMAP24XX_GPIO_OE);
1950 __raw_writel(gpio_context[i].leveldetect0,
1951 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1952 __raw_writel(gpio_context[i].leveldetect1,
1953 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1954 __raw_writel(gpio_context[i].risingdetect,
1955 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1956 __raw_writel(gpio_context[i].fallingdetect,
1957 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1958 __raw_writel(gpio_context[i].dataout,
1959 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301960 }
1961}
1962#endif
1963
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001964static struct platform_driver omap_gpio_driver = {
1965 .probe = omap_gpio_probe,
1966 .driver = {
1967 .name = "omap_gpio",
1968 },
1969};
1970
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001971/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001972 * gpio driver register needs to be done before
1973 * machine_init functions access gpio APIs.
1974 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001975 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001976static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001977{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001978 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001979}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001980postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001981
Tony Lindgren92105bb2005-09-07 17:20:26 +01001982static int __init omap_gpio_sysinit(void)
1983{
David Brownell11a78b72006-12-06 17:14:11 -08001984 mpuio_init();
1985
Tony Lindgren140455f2010-02-12 12:26:48 -08001986#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001987 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1988 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001989#endif
1990
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001991 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001992}
1993
Tony Lindgren92105bb2005-09-07 17:20:26 +01001994arch_initcall(omap_gpio_sysinit);