blob: 5ad827a1a3e86b76bac9fed986afc0702522571e [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;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010057};
58
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -080059#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +053060struct omap3_gpio_regs {
Rajendra Nayak40c670f2008-09-26 17:47:48 +053061 u32 irqenable1;
62 u32 irqenable2;
63 u32 wake_en;
64 u32 ctrl;
65 u32 oe;
66 u32 leveldetect0;
67 u32 leveldetect1;
68 u32 risingdetect;
69 u32 fallingdetect;
70 u32 dataout;
Rajendra Nayak40c670f2008-09-26 17:47:48 +053071};
72
73static struct omap3_gpio_regs gpio_context[OMAP34XX_NR_GPIOS];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -080074#endif
75
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080076/*
77 * TODO: Cleanup gpio_bank usage as it is having information
78 * related to all instances of the device
79 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010080static struct gpio_bank *gpio_bank;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080081
82static int bank_width;
83
Varadarajan, Charulathac95d10b2010-12-07 16:26:56 -080084/* TODO: Analyze removing gpio_bank_count usage from driver code */
85int gpio_bank_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010086
87static inline struct gpio_bank *get_gpio_bank(int gpio)
88{
Tony Lindgren6e60e792006-04-02 17:46:23 +010089 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010090 if (OMAP_GPIO_IS_MPUIO(gpio))
91 return &gpio_bank[0];
92 return &gpio_bank[1];
93 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010094 if (cpu_is_omap16xx()) {
95 if (OMAP_GPIO_IS_MPUIO(gpio))
96 return &gpio_bank[0];
97 return &gpio_bank[1 + (gpio >> 4)];
98 }
Zebediah C. McClure56739a62009-03-23 18:07:40 -070099 if (cpu_is_omap7xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100100 if (OMAP_GPIO_IS_MPUIO(gpio))
101 return &gpio_bank[0];
102 return &gpio_bank[1 + (gpio >> 5)];
103 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100104 if (cpu_is_omap24xx())
105 return &gpio_bank[gpio >> 5];
Santosh Shilimkar44169072009-05-28 14:16:04 -0700106 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800107 return &gpio_bank[gpio >> 5];
David Brownelle031ab22008-12-10 17:35:27 -0800108 BUG();
109 return NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100110}
111
112static inline int get_gpio_index(int gpio)
113{
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700114 if (cpu_is_omap7xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100115 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100116 if (cpu_is_omap24xx())
117 return gpio & 0x1f;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700118 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800119 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100120 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121}
122
123static inline int gpio_valid(int gpio)
124{
125 if (gpio < 0)
126 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -0800127 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300128 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100129 return -1;
130 return 0;
131 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100132 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134 if ((cpu_is_omap16xx()) && gpio < 64)
135 return 0;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700136 if (cpu_is_omap7xx() && gpio < 192)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100137 return 0;
Tony Lindgren25d6f632010-08-02 14:21:39 +0300138 if (cpu_is_omap2420() && gpio < 128)
139 return 0;
140 if (cpu_is_omap2430() && gpio < 160)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100141 return 0;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700142 if ((cpu_is_omap34xx() || cpu_is_omap44xx()) && gpio < 192)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800143 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100144 return -1;
145}
146
147static int check_gpio(int gpio)
148{
Roel Kluind32b20f2009-11-17 14:39:03 -0800149 if (unlikely(gpio_valid(gpio) < 0)) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
151 dump_stack();
152 return -1;
153 }
154 return 0;
155}
156
157static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
158{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100159 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100160 u32 l;
161
162 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800163#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100164 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800165 reg += OMAP_MPUIO_IO_CNTL / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100166 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800167#endif
168#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100169 case METHOD_GPIO_1510:
170 reg += OMAP1510_GPIO_DIR_CONTROL;
171 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800172#endif
173#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100174 case METHOD_GPIO_1610:
175 reg += OMAP1610_GPIO_DIRECTION;
176 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800177#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100178#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100179 case METHOD_GPIO_7XX:
180 reg += OMAP7XX_GPIO_DIR_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700181 break;
182#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800183#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100184 case METHOD_GPIO_24XX:
185 reg += OMAP24XX_GPIO_OE;
186 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800187#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530188#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800189 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530190 reg += OMAP4_GPIO_OE;
191 break;
192#endif
David Brownelle5c56ed2006-12-06 17:13:59 -0800193 default:
194 WARN_ON(1);
195 return;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100196 }
197 l = __raw_readl(reg);
198 if (is_input)
199 l |= 1 << gpio;
200 else
201 l &= ~(1 << gpio);
202 __raw_writel(l, reg);
203}
204
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100205static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
206{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100207 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100208 u32 l = 0;
209
210 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800211#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100212 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800213 reg += OMAP_MPUIO_OUTPUT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100214 l = __raw_readl(reg);
215 if (enable)
216 l |= 1 << gpio;
217 else
218 l &= ~(1 << gpio);
219 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800220#endif
221#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100222 case METHOD_GPIO_1510:
223 reg += OMAP1510_GPIO_DATA_OUTPUT;
224 l = __raw_readl(reg);
225 if (enable)
226 l |= 1 << gpio;
227 else
228 l &= ~(1 << gpio);
229 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800230#endif
231#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100232 case METHOD_GPIO_1610:
233 if (enable)
234 reg += OMAP1610_GPIO_SET_DATAOUT;
235 else
236 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
237 l = 1 << gpio;
238 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800239#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100240#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100241 case METHOD_GPIO_7XX:
242 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700243 l = __raw_readl(reg);
244 if (enable)
245 l |= 1 << gpio;
246 else
247 l &= ~(1 << gpio);
248 break;
249#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800250#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100251 case METHOD_GPIO_24XX:
252 if (enable)
253 reg += OMAP24XX_GPIO_SETDATAOUT;
254 else
255 reg += OMAP24XX_GPIO_CLEARDATAOUT;
256 l = 1 << gpio;
257 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800258#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530259#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800260 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530261 if (enable)
262 reg += OMAP4_GPIO_SETDATAOUT;
263 else
264 reg += OMAP4_GPIO_CLEARDATAOUT;
265 l = 1 << gpio;
266 break;
267#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100268 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800269 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100270 return;
271 }
272 __raw_writel(l, reg);
273}
274
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300275static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100276{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100277 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100278
279 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800280 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100281 reg = bank->base;
282 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800283#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100284 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800285 reg += OMAP_MPUIO_INPUT_LATCH / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100286 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800287#endif
288#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100289 case METHOD_GPIO_1510:
290 reg += OMAP1510_GPIO_DATA_INPUT;
291 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800292#endif
293#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100294 case METHOD_GPIO_1610:
295 reg += OMAP1610_GPIO_DATAIN;
296 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800297#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100298#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100299 case METHOD_GPIO_7XX:
300 reg += OMAP7XX_GPIO_DATA_INPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700301 break;
302#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800303#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100304 case METHOD_GPIO_24XX:
305 reg += OMAP24XX_GPIO_DATAIN;
306 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800307#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530308#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800309 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530310 reg += OMAP4_GPIO_DATAIN;
311 break;
312#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100313 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800314 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100315 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100316 return (__raw_readl(reg)
317 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100318}
319
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300320static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
321{
322 void __iomem *reg;
323
324 if (check_gpio(gpio) < 0)
325 return -EINVAL;
326 reg = bank->base;
327
328 switch (bank->method) {
329#ifdef CONFIG_ARCH_OMAP1
330 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800331 reg += OMAP_MPUIO_OUTPUT / bank->stride;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300332 break;
333#endif
334#ifdef CONFIG_ARCH_OMAP15XX
335 case METHOD_GPIO_1510:
336 reg += OMAP1510_GPIO_DATA_OUTPUT;
337 break;
338#endif
339#ifdef CONFIG_ARCH_OMAP16XX
340 case METHOD_GPIO_1610:
341 reg += OMAP1610_GPIO_DATAOUT;
342 break;
343#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100344#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100345 case METHOD_GPIO_7XX:
346 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300347 break;
348#endif
Charulatha V9f096862010-05-14 12:05:27 -0700349#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300350 case METHOD_GPIO_24XX:
351 reg += OMAP24XX_GPIO_DATAOUT;
352 break;
353#endif
Charulatha V9f096862010-05-14 12:05:27 -0700354#ifdef CONFIG_ARCH_OMAP4
355 case METHOD_GPIO_44XX:
356 reg += OMAP4_GPIO_DATAOUT;
357 break;
358#endif
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300359 default:
360 return -EINVAL;
361 }
362
363 return (__raw_readl(reg) & (1 << get_gpio_index(gpio))) != 0;
364}
365
Tony Lindgren92105bb2005-09-07 17:20:26 +0100366#define MOD_REG_BIT(reg, bit_mask, set) \
367do { \
368 int l = __raw_readl(base + reg); \
369 if (set) l |= bit_mask; \
370 else l &= ~bit_mask; \
371 __raw_writel(l, base + reg); \
372} while(0)
373
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700374/**
375 * _set_gpio_debounce - low level gpio debounce time
376 * @bank: the gpio bank we're acting upon
377 * @gpio: the gpio number on this @gpio
378 * @debounce: debounce time to use
379 *
380 * OMAP's debounce time is in 31us steps so we need
381 * to convert and round up to the closest unit.
382 */
383static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
384 unsigned debounce)
385{
386 void __iomem *reg = bank->base;
387 u32 val;
388 u32 l;
389
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800390 if (!bank->dbck_flag)
391 return;
392
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700393 if (debounce < 32)
394 debounce = 0x01;
395 else if (debounce > 7936)
396 debounce = 0xff;
397 else
398 debounce = (debounce / 0x1f) - 1;
399
400 l = 1 << get_gpio_index(gpio);
401
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800402 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700403 reg += OMAP4_GPIO_DEBOUNCINGTIME;
404 else
405 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
406
407 __raw_writel(debounce, reg);
408
409 reg = bank->base;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800410 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700411 reg += OMAP4_GPIO_DEBOUNCENABLE;
412 else
413 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
414
415 val = __raw_readl(reg);
416
417 if (debounce) {
418 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800419 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700420 } else {
421 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800422 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700423 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300424 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700425
426 __raw_writel(val, reg);
427}
428
Tony Lindgren140455f2010-02-12 12:26:48 -0800429#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700430static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
431 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100432{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800433 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100434 u32 gpio_bit = 1 << gpio;
435
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530436 if (cpu_is_omap44xx()) {
437 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
438 trigger & IRQ_TYPE_LEVEL_LOW);
439 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
440 trigger & IRQ_TYPE_LEVEL_HIGH);
441 MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
442 trigger & IRQ_TYPE_EDGE_RISING);
443 MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
444 trigger & IRQ_TYPE_EDGE_FALLING);
445 } else {
446 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
447 trigger & IRQ_TYPE_LEVEL_LOW);
448 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
449 trigger & IRQ_TYPE_LEVEL_HIGH);
450 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
451 trigger & IRQ_TYPE_EDGE_RISING);
452 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
453 trigger & IRQ_TYPE_EDGE_FALLING);
454 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800455 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530456 if (cpu_is_omap44xx()) {
Colin Cross0622b252011-06-06 13:38:17 -0700457 MOD_REG_BIT(OMAP4_GPIO_IRQWAKEN0, gpio_bit,
458 trigger != 0);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530459 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000460 /*
461 * GPIO wakeup request can only be generated on edge
462 * transitions
463 */
464 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530465 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700466 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530467 else
468 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700469 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530470 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200471 }
472 /* This part needs to be executed always for OMAP34xx */
473 if (cpu_is_omap34xx() || (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000474 /*
475 * Log the edge gpio and manually trigger the IRQ
476 * after resume if the input level changes
477 * to avoid irq lost during PER RET/OFF mode
478 * Applies for omap2 non-wakeup gpio and all omap3 gpios
479 */
480 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800481 bank->enabled_non_wakeup_gpios |= gpio_bit;
482 else
483 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
484 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700485
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530486 if (cpu_is_omap44xx()) {
487 bank->level_mask =
488 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
489 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
490 } else {
491 bank->level_mask =
492 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
493 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
494 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100495}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800496#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100497
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800498#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800499/*
500 * This only applies to chips that can't do both rising and falling edge
501 * detection at once. For all other chips, this function is a noop.
502 */
503static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
504{
505 void __iomem *reg = bank->base;
506 u32 l = 0;
507
508 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800509 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800510 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800511 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800512#ifdef CONFIG_ARCH_OMAP15XX
513 case METHOD_GPIO_1510:
514 reg += OMAP1510_GPIO_INT_CONTROL;
515 break;
516#endif
517#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
518 case METHOD_GPIO_7XX:
519 reg += OMAP7XX_GPIO_INT_CONTROL;
520 break;
521#endif
522 default:
523 return;
524 }
525
526 l = __raw_readl(reg);
527 if ((l >> gpio) & 1)
528 l &= ~(1 << gpio);
529 else
530 l |= 1 << gpio;
531
532 __raw_writel(l, reg);
533}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800534#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800535
Tony Lindgren92105bb2005-09-07 17:20:26 +0100536static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
537{
538 void __iomem *reg = bank->base;
539 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100540
541 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800542#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100543 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800544 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100545 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000546 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800547 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100548 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100549 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100550 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100551 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100552 else
553 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100554 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800555#endif
556#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100557 case METHOD_GPIO_1510:
558 reg += OMAP1510_GPIO_INT_CONTROL;
559 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000560 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800561 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100562 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100563 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100564 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100565 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100566 else
567 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100568 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800569#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800570#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100571 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100572 if (gpio & 0x08)
573 reg += OMAP1610_GPIO_EDGE_CTRL2;
574 else
575 reg += OMAP1610_GPIO_EDGE_CTRL1;
576 gpio &= 0x07;
577 l = __raw_readl(reg);
578 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100579 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100580 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100581 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100582 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800583 if (trigger)
584 /* Enable wake-up during idle for dynamic tick */
585 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
586 else
587 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100588 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800589#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100590#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100591 case METHOD_GPIO_7XX:
592 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700593 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000594 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800595 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700596 if (trigger & IRQ_TYPE_EDGE_RISING)
597 l |= 1 << gpio;
598 else if (trigger & IRQ_TYPE_EDGE_FALLING)
599 l &= ~(1 << gpio);
600 else
601 goto bad;
602 break;
603#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800604#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100605 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800606 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800607 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200608 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800609#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100610 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100611 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100612 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100613 __raw_writel(l, reg);
614 return 0;
615bad:
616 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100617}
618
Lennert Buytenheke9191022010-11-29 11:17:17 +0100619static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100620{
621 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100622 unsigned gpio;
623 int retval;
David Brownella6472532008-03-03 04:33:30 -0800624 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100625
Lennert Buytenheke9191022010-11-29 11:17:17 +0100626 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
627 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100628 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100629 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100630
631 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100632 return -EINVAL;
633
David Brownelle5c56ed2006-12-06 17:13:59 -0800634 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100635 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800636
637 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800638 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800639 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100640 return -EINVAL;
641
Lennert Buytenheke9191022010-11-29 11:17:17 +0100642 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800643 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100644 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800645 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800646
647 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100648 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800649 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100650 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800651
Tony Lindgren92105bb2005-09-07 17:20:26 +0100652 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100653}
654
655static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
656{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100657 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100658
659 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800660#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100661 case METHOD_MPUIO:
662 /* MPUIO irqstatus is reset by reading the status register,
663 * so do nothing here */
664 return;
David Brownelle5c56ed2006-12-06 17:13:59 -0800665#endif
666#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100667 case METHOD_GPIO_1510:
668 reg += OMAP1510_GPIO_INT_STATUS;
669 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800670#endif
671#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100672 case METHOD_GPIO_1610:
673 reg += OMAP1610_GPIO_IRQSTATUS1;
674 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800675#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100676#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100677 case METHOD_GPIO_7XX:
678 reg += OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700679 break;
680#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800681#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100682 case METHOD_GPIO_24XX:
683 reg += OMAP24XX_GPIO_IRQSTATUS1;
684 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800685#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530686#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800687 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530688 reg += OMAP4_GPIO_IRQSTATUS0;
689 break;
690#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100691 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800692 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100693 return;
694 }
695 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300696
697 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800698 if (cpu_is_omap24xx() || cpu_is_omap34xx())
699 reg = bank->base + OMAP24XX_GPIO_IRQSTATUS2;
700 else if (cpu_is_omap44xx())
701 reg = bank->base + OMAP4_GPIO_IRQSTATUS1;
702
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530703 if (cpu_is_omap24xx() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Roger Quadrosbedfd152009-04-23 11:10:50 -0700704 __raw_writel(gpio_mask, reg);
705
706 /* Flush posted write for the irq status to avoid spurious interrupts */
707 __raw_readl(reg);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530708 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100709}
710
711static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
712{
713 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
714}
715
Imre Deakea6dedd2006-06-26 16:16:00 -0700716static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
717{
718 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700719 int inv = 0;
720 u32 l;
721 u32 mask;
Imre Deakea6dedd2006-06-26 16:16:00 -0700722
723 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800724#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700725 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800726 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Imre Deak99c47702006-06-26 16:16:07 -0700727 mask = 0xffff;
728 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700729 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800730#endif
731#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700732 case METHOD_GPIO_1510:
733 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700734 mask = 0xffff;
735 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700736 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800737#endif
738#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700739 case METHOD_GPIO_1610:
740 reg += OMAP1610_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700741 mask = 0xffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700742 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800743#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100744#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100745 case METHOD_GPIO_7XX:
746 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700747 mask = 0xffffffff;
748 inv = 1;
749 break;
750#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800751#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Imre Deakea6dedd2006-06-26 16:16:00 -0700752 case METHOD_GPIO_24XX:
753 reg += OMAP24XX_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700754 mask = 0xffffffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700755 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800756#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530757#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800758 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530759 reg += OMAP4_GPIO_IRQSTATUSSET0;
760 mask = 0xffffffff;
761 break;
762#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700763 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800764 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700765 return 0;
766 }
767
Imre Deak99c47702006-06-26 16:16:07 -0700768 l = __raw_readl(reg);
769 if (inv)
770 l = ~l;
771 l &= mask;
772 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700773}
774
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100775static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
776{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100777 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100778 u32 l;
779
780 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800781#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100782 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800783 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100784 l = __raw_readl(reg);
785 if (enable)
786 l &= ~(gpio_mask);
787 else
788 l |= gpio_mask;
789 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800790#endif
791#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100792 case METHOD_GPIO_1510:
793 reg += OMAP1510_GPIO_INT_MASK;
794 l = __raw_readl(reg);
795 if (enable)
796 l &= ~(gpio_mask);
797 else
798 l |= gpio_mask;
799 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800800#endif
801#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100802 case METHOD_GPIO_1610:
803 if (enable)
804 reg += OMAP1610_GPIO_SET_IRQENABLE1;
805 else
806 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
807 l = gpio_mask;
808 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800809#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100810#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100811 case METHOD_GPIO_7XX:
812 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700813 l = __raw_readl(reg);
814 if (enable)
815 l &= ~(gpio_mask);
816 else
817 l |= gpio_mask;
818 break;
819#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800820#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100821 case METHOD_GPIO_24XX:
822 if (enable)
823 reg += OMAP24XX_GPIO_SETIRQENABLE1;
824 else
825 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
826 l = gpio_mask;
827 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800828#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530829#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800830 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530831 if (enable)
832 reg += OMAP4_GPIO_IRQSTATUSSET0;
833 else
834 reg += OMAP4_GPIO_IRQSTATUSCLR0;
835 l = gpio_mask;
836 break;
837#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100838 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800839 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100840 return;
841 }
842 __raw_writel(l, reg);
843}
844
845static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
846{
847 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
848}
849
Tony Lindgren92105bb2005-09-07 17:20:26 +0100850/*
851 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
852 * 1510 does not seem to have a wake-up register. If JTAG is connected
853 * to the target, system will wake up always on GPIO events. While
854 * system is running all registered GPIO interrupts need to have wake-up
855 * enabled. When system is suspended, only selected GPIO interrupts need
856 * to have wake-up enabled.
857 */
858static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
859{
Tony Lindgren4cc64202010-01-08 10:29:05 -0800860 unsigned long uninitialized_var(flags);
David Brownella6472532008-03-03 04:33:30 -0800861
Tony Lindgren92105bb2005-09-07 17:20:26 +0100862 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800863#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800864 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100865 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800866 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700867 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100868 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700869 else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100870 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800871 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100872 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800873#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800874#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800875 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800876 case METHOD_GPIO_44XX:
David Brownell11a78b72006-12-06 17:14:11 -0800877 if (bank->non_wakeup_gpios & (1 << gpio)) {
878 printk(KERN_ERR "Unable to modify wakeup on "
879 "non-wakeup GPIO%d\n",
880 (bank - gpio_bank) * 32 + gpio);
881 return -EINVAL;
882 }
David Brownella6472532008-03-03 04:33:30 -0800883 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700884 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800885 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700886 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800887 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800888 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800889 return 0;
890#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100891 default:
892 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
893 bank->method);
894 return -EINVAL;
895 }
896}
897
Tony Lindgren4196dd62006-09-25 12:41:38 +0300898static void _reset_gpio(struct gpio_bank *bank, int gpio)
899{
900 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
901 _set_gpio_irqenable(bank, gpio, 0);
902 _clear_gpio_irqstatus(bank, gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100903 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300904}
905
Tony Lindgren92105bb2005-09-07 17:20:26 +0100906/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100907static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100908{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100909 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100910 struct gpio_bank *bank;
911 int retval;
912
913 if (check_gpio(gpio) < 0)
914 return -ENODEV;
Lennert Buytenheke9191022010-11-29 11:17:17 +0100915 bank = irq_data_get_irq_chip_data(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100916 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100917
918 return retval;
919}
920
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800921static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100922{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800923 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800924 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100925
David Brownella6472532008-03-03 04:33:30 -0800926 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100927
Tony Lindgren4196dd62006-09-25 12:41:38 +0300928 /* Set trigger to none. You need to enable the desired trigger with
929 * request_irq() or set_irq_type().
930 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800931 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100932
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000933#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100934 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100935 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100936
Tony Lindgren92105bb2005-09-07 17:20:26 +0100937 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100938 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800939 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100940 }
941#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800942 if (!cpu_class_is_omap1()) {
943 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700944 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800945 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700946
947 if (cpu_is_omap24xx() || cpu_is_omap34xx())
948 reg += OMAP24XX_GPIO_CTRL;
949 else if (cpu_is_omap44xx())
950 reg += OMAP4_GPIO_CTRL;
951 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800952 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700953 ctrl &= 0xFFFFFFFE;
954 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800955 }
956 bank->mod_usage |= 1 << offset;
957 }
David Brownella6472532008-03-03 04:33:30 -0800958 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100959
960 return 0;
961}
962
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800963static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100964{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800965 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800966 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100967
David Brownella6472532008-03-03 04:33:30 -0800968 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100969#ifdef CONFIG_ARCH_OMAP16XX
970 if (bank->method == METHOD_GPIO_1610) {
971 /* Disable wake-up during idle for dynamic tick */
972 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800973 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100974 }
975#endif
Charulatha V9f096862010-05-14 12:05:27 -0700976#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
977 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100978 /* Disable wake-up during idle for dynamic tick */
979 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800980 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100981 }
982#endif
Charulatha V9f096862010-05-14 12:05:27 -0700983#ifdef CONFIG_ARCH_OMAP4
984 if (bank->method == METHOD_GPIO_44XX) {
985 /* Disable wake-up during idle for dynamic tick */
986 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
987 __raw_writel(1 << offset, reg);
988 }
989#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800990 if (!cpu_class_is_omap1()) {
991 bank->mod_usage &= ~(1 << offset);
992 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700993 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800994 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700995
996 if (cpu_is_omap24xx() || cpu_is_omap34xx())
997 reg += OMAP24XX_GPIO_CTRL;
998 else if (cpu_is_omap44xx())
999 reg += OMAP4_GPIO_CTRL;
1000 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001001 /* Module is disabled, clocks are gated */
1002 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -07001003 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001004 }
1005 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001006 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -08001007 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001008}
1009
1010/*
1011 * We need to unmask the GPIO bank interrupt as soon as possible to
1012 * avoid missing GPIO interrupts for other lines in the bank.
1013 * Then we need to mask-read-clear-unmask the triggered GPIO lines
1014 * in the bank to avoid missing nested interrupts for a GPIO line.
1015 * If we wait to unmask individual GPIO lines in the bank after the
1016 * line's interrupt handler has been run, we may miss some nested
1017 * interrupts.
1018 */
Russell King10dd5ce2006-11-23 11:41:32 +00001019static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001020{
Tony Lindgren92105bb2005-09-07 17:20:26 +01001021 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001022 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -08001023 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001024 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -07001025 u32 retrigger = 0;
1026 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +00001027 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001028
Will Deaconee144182011-02-21 13:46:08 +00001029 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001030
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001031 bank = irq_get_handler_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -08001032#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001033 if (bank->method == METHOD_MPUIO)
Tony Lindgren5de62b82010-12-07 16:26:58 -08001034 isr_reg = bank->base +
1035 OMAP_MPUIO_GPIO_INT / bank->stride;
David Brownelle5c56ed2006-12-06 17:13:59 -08001036#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001037#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001038 if (bank->method == METHOD_GPIO_1510)
1039 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
1040#endif
1041#if defined(CONFIG_ARCH_OMAP16XX)
1042 if (bank->method == METHOD_GPIO_1610)
1043 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
1044#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +01001045#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +01001046 if (bank->method == METHOD_GPIO_7XX)
1047 isr_reg = bank->base + OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001048#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001049#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001050 if (bank->method == METHOD_GPIO_24XX)
1051 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1052#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301053#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001054 if (bank->method == METHOD_GPIO_44XX)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301055 isr_reg = bank->base + OMAP4_GPIO_IRQSTATUS0;
1056#endif
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -08001057
1058 if (WARN_ON(!isr_reg))
1059 goto exit;
1060
Tony Lindgren92105bb2005-09-07 17:20:26 +01001061 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001062 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -07001063 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001064
Imre Deakea6dedd2006-06-26 16:16:00 -07001065 enabled = _get_gpio_irqbank_mask(bank);
1066 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001067
1068 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1069 isr &= 0x0000ffff;
1070
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001071 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001072 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -07001073 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001074
1075 /* clear edge sensitive interrupts before handler(s) are
1076 called so that we don't miss any interrupt occurred while
1077 executing them */
1078 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1079 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1080 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1081
1082 /* if there is only edge sensitive GPIO pin interrupts
1083 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -07001084 if (!level_mask && !unmasked) {
1085 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +00001086 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -07001087 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001088
Imre Deakea6dedd2006-06-26 16:16:00 -07001089 isr |= retrigger;
1090 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001091 if (!isr)
1092 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001093
Tony Lindgren92105bb2005-09-07 17:20:26 +01001094 gpio_irq = bank->virtual_irq_start;
1095 for (; isr != 0; isr >>= 1, gpio_irq++) {
Cory Maccarrone4318f362010-01-08 10:29:04 -08001096 gpio_index = get_gpio_index(irq_to_gpio(gpio_irq));
1097
Tony Lindgren92105bb2005-09-07 17:20:26 +01001098 if (!(isr & 1))
1099 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001100
Cory Maccarrone4318f362010-01-08 10:29:04 -08001101#ifdef CONFIG_ARCH_OMAP1
1102 /*
1103 * Some chips can't respond to both rising and falling
1104 * at the same time. If this irq was requested with
1105 * both flags, we need to flip the ICR data for the IRQ
1106 * to respond to the IRQ for the opposite direction.
1107 * This will be indicated in the bank toggle_mask.
1108 */
1109 if (bank->toggle_mask & (1 << gpio_index))
1110 _toggle_gpio_edge_triggering(bank, gpio_index);
1111#endif
1112
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +01001113 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001114 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001115 }
Imre Deakea6dedd2006-06-26 16:16:00 -07001116 /* if bank has any level sensitive GPIO pin interrupt
1117 configured, we must unmask the bank interrupt only after
1118 handler(s) are executed in order to avoid spurious bank
1119 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -08001120exit:
Imre Deakea6dedd2006-06-26 16:16:00 -07001121 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +00001122 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001123}
1124
Lennert Buytenheke9191022010-11-29 11:17:17 +01001125static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +03001126{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001127 unsigned int gpio = d->irq - IH_GPIO_BASE;
1128 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001129
1130 _reset_gpio(bank, gpio);
1131}
1132
Lennert Buytenheke9191022010-11-29 11:17:17 +01001133static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001134{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001135 unsigned int gpio = d->irq - IH_GPIO_BASE;
1136 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001137
1138 _clear_gpio_irqstatus(bank, gpio);
1139}
1140
Lennert Buytenheke9191022010-11-29 11:17:17 +01001141static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001142{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001143 unsigned int gpio = d->irq - IH_GPIO_BASE;
1144 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001145
1146 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman55b60192009-06-04 15:57:10 -07001147 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001148}
1149
Lennert Buytenheke9191022010-11-29 11:17:17 +01001150static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001151{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001152 unsigned int gpio = d->irq - IH_GPIO_BASE;
1153 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001154 unsigned int irq_mask = 1 << get_gpio_index(gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +01001155 u32 trigger = irqd_get_trigger_type(d);
Kevin Hilman55b60192009-06-04 15:57:10 -07001156
1157 if (trigger)
1158 _set_gpio_triggering(bank, get_gpio_index(gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001159
1160 /* For level-triggered GPIOs, the clearing must be done after
1161 * the HW source is cleared, thus after the handler has run */
1162 if (bank->level_mask & irq_mask) {
1163 _set_gpio_irqenable(bank, gpio, 0);
1164 _clear_gpio_irqstatus(bank, gpio);
1165 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001166
Kevin Hilman4de8c752008-01-16 21:56:14 -08001167 _set_gpio_irqenable(bank, gpio, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001168}
1169
David Brownelle5c56ed2006-12-06 17:13:59 -08001170static struct irq_chip gpio_irq_chip = {
1171 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +01001172 .irq_shutdown = gpio_irq_shutdown,
1173 .irq_ack = gpio_ack_irq,
1174 .irq_mask = gpio_mask_irq,
1175 .irq_unmask = gpio_unmask_irq,
1176 .irq_set_type = gpio_irq_type,
1177 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -08001178};
1179
1180/*---------------------------------------------------------------------*/
1181
1182#ifdef CONFIG_ARCH_OMAP1
1183
1184/* MPUIO uses the always-on 32k clock */
1185
Lennert Buytenheke9191022010-11-29 11:17:17 +01001186static void mpuio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001187{
1188 /* The ISR is reset automatically, so do nothing here. */
1189}
1190
Lennert Buytenheke9191022010-11-29 11:17:17 +01001191static void mpuio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001192{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001193 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1194 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001195
1196 _set_gpio_irqenable(bank, gpio, 0);
1197}
1198
Lennert Buytenheke9191022010-11-29 11:17:17 +01001199static void mpuio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001200{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001201 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1202 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001203
1204 _set_gpio_irqenable(bank, gpio, 1);
1205}
1206
David Brownelle5c56ed2006-12-06 17:13:59 -08001207static struct irq_chip mpuio_irq_chip = {
1208 .name = "MPUIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +01001209 .irq_ack = mpuio_ack_irq,
1210 .irq_mask = mpuio_mask_irq,
1211 .irq_unmask = mpuio_unmask_irq,
1212 .irq_set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001213#ifdef CONFIG_ARCH_OMAP16XX
1214 /* REVISIT: assuming only 16xx supports MPUIO wake events */
Lennert Buytenheke9191022010-11-29 11:17:17 +01001215 .irq_set_wake = gpio_wake_enable,
David Brownell11a78b72006-12-06 17:14:11 -08001216#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001217};
1218
David Brownelle5c56ed2006-12-06 17:13:59 -08001219
1220#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1221
David Brownell11a78b72006-12-06 17:14:11 -08001222
1223#ifdef CONFIG_ARCH_OMAP16XX
1224
1225#include <linux/platform_device.h>
1226
Magnus Damm79ee0312009-07-08 13:22:04 +02001227static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001228{
Magnus Damm79ee0312009-07-08 13:22:04 +02001229 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001230 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001231 void __iomem *mask_reg = bank->base +
1232 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001233 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001234
David Brownella6472532008-03-03 04:33:30 -08001235 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001236 bank->saved_wakeup = __raw_readl(mask_reg);
1237 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001238 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001239
1240 return 0;
1241}
1242
Magnus Damm79ee0312009-07-08 13:22:04 +02001243static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001244{
Magnus Damm79ee0312009-07-08 13:22:04 +02001245 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001246 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001247 void __iomem *mask_reg = bank->base +
1248 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001249 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001250
David Brownella6472532008-03-03 04:33:30 -08001251 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001252 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001253 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001254
1255 return 0;
1256}
1257
Alexey Dobriyan47145212009-12-14 18:00:08 -08001258static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +02001259 .suspend_noirq = omap_mpuio_suspend_noirq,
1260 .resume_noirq = omap_mpuio_resume_noirq,
1261};
1262
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001263/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -08001264static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -08001265 .driver = {
1266 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +02001267 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -08001268 },
1269};
1270
1271static struct platform_device omap_mpuio_device = {
1272 .name = "mpuio",
1273 .id = -1,
1274 .dev = {
1275 .driver = &omap_mpuio_driver.driver,
1276 }
1277 /* could list the /proc/iomem resources */
1278};
1279
1280static inline void mpuio_init(void)
1281{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001282 struct gpio_bank *bank = get_gpio_bank(OMAP_MPUIO(0));
1283 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -07001284
David Brownell11a78b72006-12-06 17:14:11 -08001285 if (platform_driver_register(&omap_mpuio_driver) == 0)
1286 (void) platform_device_register(&omap_mpuio_device);
1287}
1288
1289#else
1290static inline void mpuio_init(void) {}
1291#endif /* 16xx */
1292
David Brownelle5c56ed2006-12-06 17:13:59 -08001293#else
1294
1295extern struct irq_chip mpuio_irq_chip;
1296
1297#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001298static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001299
1300#endif
1301
1302/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001303
David Brownell52e31342008-03-03 12:43:23 -08001304/* REVISIT these are stupid implementations! replace by ones that
1305 * don't switch on METHOD_* and which mostly avoid spinlocks
1306 */
1307
1308static int gpio_input(struct gpio_chip *chip, unsigned offset)
1309{
1310 struct gpio_bank *bank;
1311 unsigned long flags;
1312
1313 bank = container_of(chip, struct gpio_bank, chip);
1314 spin_lock_irqsave(&bank->lock, flags);
1315 _set_gpio_direction(bank, offset, 1);
1316 spin_unlock_irqrestore(&bank->lock, flags);
1317 return 0;
1318}
1319
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001320static int gpio_is_input(struct gpio_bank *bank, int mask)
1321{
1322 void __iomem *reg = bank->base;
1323
1324 switch (bank->method) {
1325 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -08001326 reg += OMAP_MPUIO_IO_CNTL / bank->stride;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001327 break;
1328 case METHOD_GPIO_1510:
1329 reg += OMAP1510_GPIO_DIR_CONTROL;
1330 break;
1331 case METHOD_GPIO_1610:
1332 reg += OMAP1610_GPIO_DIRECTION;
1333 break;
Alistair Buxton7c006922009-09-22 10:02:58 +01001334 case METHOD_GPIO_7XX:
1335 reg += OMAP7XX_GPIO_DIR_CONTROL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001336 break;
1337 case METHOD_GPIO_24XX:
1338 reg += OMAP24XX_GPIO_OE;
1339 break;
Charulatha V9f096862010-05-14 12:05:27 -07001340 case METHOD_GPIO_44XX:
1341 reg += OMAP4_GPIO_OE;
1342 break;
1343 default:
1344 WARN_ONCE(1, "gpio_is_input: incorrect OMAP GPIO method");
1345 return -EINVAL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001346 }
1347 return __raw_readl(reg) & mask;
1348}
1349
David Brownell52e31342008-03-03 12:43:23 -08001350static int gpio_get(struct gpio_chip *chip, unsigned offset)
1351{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001352 struct gpio_bank *bank;
1353 void __iomem *reg;
1354 int gpio;
1355 u32 mask;
1356
1357 gpio = chip->base + offset;
1358 bank = get_gpio_bank(gpio);
1359 reg = bank->base;
1360 mask = 1 << get_gpio_index(gpio);
1361
1362 if (gpio_is_input(bank, mask))
1363 return _get_gpio_datain(bank, gpio);
1364 else
1365 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -08001366}
1367
1368static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1369{
1370 struct gpio_bank *bank;
1371 unsigned long flags;
1372
1373 bank = container_of(chip, struct gpio_bank, chip);
1374 spin_lock_irqsave(&bank->lock, flags);
1375 _set_gpio_dataout(bank, offset, value);
1376 _set_gpio_direction(bank, offset, 0);
1377 spin_unlock_irqrestore(&bank->lock, flags);
1378 return 0;
1379}
1380
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001381static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
1382 unsigned debounce)
1383{
1384 struct gpio_bank *bank;
1385 unsigned long flags;
1386
1387 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001388
1389 if (!bank->dbck) {
1390 bank->dbck = clk_get(bank->dev, "dbclk");
1391 if (IS_ERR(bank->dbck))
1392 dev_err(bank->dev, "Could not get gpio dbck\n");
1393 }
1394
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001395 spin_lock_irqsave(&bank->lock, flags);
1396 _set_gpio_debounce(bank, offset, debounce);
1397 spin_unlock_irqrestore(&bank->lock, flags);
1398
1399 return 0;
1400}
1401
David Brownell52e31342008-03-03 12:43:23 -08001402static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1403{
1404 struct gpio_bank *bank;
1405 unsigned long flags;
1406
1407 bank = container_of(chip, struct gpio_bank, chip);
1408 spin_lock_irqsave(&bank->lock, flags);
1409 _set_gpio_dataout(bank, offset, value);
1410 spin_unlock_irqrestore(&bank->lock, flags);
1411}
1412
David Brownella007b702008-12-10 17:35:25 -08001413static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1414{
1415 struct gpio_bank *bank;
1416
1417 bank = container_of(chip, struct gpio_bank, chip);
1418 return bank->virtual_irq_start + offset;
1419}
1420
David Brownell52e31342008-03-03 12:43:23 -08001421/*---------------------------------------------------------------------*/
1422
Tony Lindgren9a748052010-12-07 16:26:56 -08001423static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001424{
1425 u32 rev;
1426
Tony Lindgren9a748052010-12-07 16:26:56 -08001427 if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
1428 rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001429 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001430 rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001431 else if (cpu_is_omap44xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001432 rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001433 else
1434 return;
1435
1436 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1437 (rev >> 4) & 0x0f, rev & 0x0f);
1438}
1439
David Brownell8ba55c52008-02-26 11:10:50 -08001440/* This lock class tells lockdep that GPIO irqs are in a different
1441 * category than their parents, so it won't report false recursion.
1442 */
1443static struct lock_class_key gpio_lock_class;
1444
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001445static inline int init_gpio_info(struct platform_device *pdev)
1446{
1447 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1448 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1449 GFP_KERNEL);
1450 if (!gpio_bank) {
1451 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1452 return -ENOMEM;
1453 }
1454 return 0;
1455}
1456
1457/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001458static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1459{
1460 if (cpu_class_is_omap2()) {
1461 if (cpu_is_omap44xx()) {
1462 __raw_writel(0xffffffff, bank->base +
1463 OMAP4_GPIO_IRQSTATUSCLR0);
1464 __raw_writel(0x00000000, bank->base +
1465 OMAP4_GPIO_DEBOUNCENABLE);
1466 /* Initialize interface clk ungated, module enabled */
1467 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1468 } else if (cpu_is_omap34xx()) {
1469 __raw_writel(0x00000000, bank->base +
1470 OMAP24XX_GPIO_IRQENABLE1);
1471 __raw_writel(0xffffffff, bank->base +
1472 OMAP24XX_GPIO_IRQSTATUS1);
1473 __raw_writel(0x00000000, bank->base +
1474 OMAP24XX_GPIO_DEBOUNCE_EN);
1475
1476 /* Initialize interface clk ungated, module enabled */
1477 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1478 } else if (cpu_is_omap24xx()) {
1479 static const u32 non_wakeup_gpios[] = {
1480 0xe203ffc0, 0x08700040
1481 };
1482 if (id < ARRAY_SIZE(non_wakeup_gpios))
1483 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1484 }
1485 } else if (cpu_class_is_omap1()) {
1486 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001487 __raw_writew(0xffff, bank->base +
1488 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001489 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1490 __raw_writew(0xffff, bank->base
1491 + OMAP1510_GPIO_INT_MASK);
1492 __raw_writew(0x0000, bank->base
1493 + OMAP1510_GPIO_INT_STATUS);
1494 }
1495 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1496 __raw_writew(0x0000, bank->base
1497 + OMAP1610_GPIO_IRQENABLE1);
1498 __raw_writew(0xffff, bank->base
1499 + OMAP1610_GPIO_IRQSTATUS1);
1500 __raw_writew(0x0014, bank->base
1501 + OMAP1610_GPIO_SYSCONFIG);
1502
1503 /*
1504 * Enable system clock for GPIO module.
1505 * The CAM_CLK_CTRL *is* really the right place.
1506 */
1507 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1508 ULPD_CAM_CLK_CTRL);
1509 }
1510 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1511 __raw_writel(0xffffffff, bank->base
1512 + OMAP7XX_GPIO_INT_MASK);
1513 __raw_writel(0x00000000, bank->base
1514 + OMAP7XX_GPIO_INT_STATUS);
1515 }
1516 }
1517}
1518
Russell Kingd52b31d2011-05-27 13:56:12 -07001519static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001520{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001521 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001522 static int gpio;
1523
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001524 bank->mod_usage = 0;
1525 /*
1526 * REVISIT eventually switch from OMAP-specific gpio structs
1527 * over to the generic ones
1528 */
1529 bank->chip.request = omap_gpio_request;
1530 bank->chip.free = omap_gpio_free;
1531 bank->chip.direction_input = gpio_input;
1532 bank->chip.get = gpio_get;
1533 bank->chip.direction_output = gpio_output;
1534 bank->chip.set_debounce = gpio_debounce;
1535 bank->chip.set = gpio_set;
1536 bank->chip.to_irq = gpio_2irq;
1537 if (bank_is_mpuio(bank)) {
1538 bank->chip.label = "mpuio";
1539#ifdef CONFIG_ARCH_OMAP16XX
1540 bank->chip.dev = &omap_mpuio_device.dev;
1541#endif
1542 bank->chip.base = OMAP_MPUIO(0);
1543 } else {
1544 bank->chip.label = "gpio";
1545 bank->chip.base = gpio;
1546 gpio += bank_width;
1547 }
1548 bank->chip.ngpio = bank_width;
1549
1550 gpiochip_add(&bank->chip);
1551
1552 for (j = bank->virtual_irq_start;
1553 j < bank->virtual_irq_start + bank_width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001554 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001555 irq_set_chip_data(j, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001556 if (bank_is_mpuio(bank))
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001557 irq_set_chip(j, &mpuio_irq_chip);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001558 else
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001559 irq_set_chip(j, &gpio_irq_chip);
1560 irq_set_handler(j, handle_simple_irq);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001561 set_irq_flags(j, IRQF_VALID);
1562 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001563 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1564 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001565}
1566
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001567static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001568{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001569 static int gpio_init_done;
1570 struct omap_gpio_platform_data *pdata;
1571 struct resource *res;
1572 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001573 struct gpio_bank *bank;
1574
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001575 if (!pdev->dev.platform_data)
1576 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001577
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001578 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001579
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001580 if (!gpio_init_done) {
1581 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001582
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001583 ret = init_gpio_info(pdev);
1584 if (ret)
1585 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001586 }
1587
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001588 id = pdev->id;
1589 bank = &gpio_bank[id];
1590
1591 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1592 if (unlikely(!res)) {
1593 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1594 return -ENODEV;
1595 }
1596
1597 bank->irq = res->start;
1598 bank->virtual_irq_start = pdata->virtual_irq_start;
1599 bank->method = pdata->bank_type;
1600 bank->dev = &pdev->dev;
1601 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001602 bank->stride = pdata->bank_stride;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001603 bank_width = pdata->bank_width;
1604
1605 spin_lock_init(&bank->lock);
1606
1607 /* Static mapping, never released */
1608 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1609 if (unlikely(!res)) {
1610 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1611 return -ENODEV;
1612 }
1613
1614 bank->base = ioremap(res->start, resource_size(res));
1615 if (!bank->base) {
1616 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1617 return -ENOMEM;
1618 }
1619
1620 pm_runtime_enable(bank->dev);
1621 pm_runtime_get_sync(bank->dev);
1622
1623 omap_gpio_mod_init(bank, id);
1624 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001625 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001626
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001627 if (!gpio_init_done)
1628 gpio_init_done = 1;
1629
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001630 return 0;
1631}
1632
Tony Lindgren140455f2010-02-12 12:26:48 -08001633#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001634static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001635{
1636 int i;
1637
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001638 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001639 return 0;
1640
1641 for (i = 0; i < gpio_bank_count; i++) {
1642 struct gpio_bank *bank = &gpio_bank[i];
1643 void __iomem *wake_status;
1644 void __iomem *wake_clear;
1645 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001646 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001647
1648 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001649#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001650 case METHOD_GPIO_1610:
1651 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1652 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1653 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1654 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001655#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001656#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001657 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001658 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001659 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1660 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1661 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001662#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301663#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001664 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301665 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1666 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1667 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1668 break;
1669#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001670 default:
1671 continue;
1672 }
1673
David Brownella6472532008-03-03 04:33:30 -08001674 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001675 bank->saved_wakeup = __raw_readl(wake_status);
1676 __raw_writel(0xffffffff, wake_clear);
1677 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001678 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001679 }
1680
1681 return 0;
1682}
1683
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001684static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001685{
1686 int i;
1687
Tero Kristo723fdb72008-11-26 14:35:16 -08001688 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001689 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001690
1691 for (i = 0; i < gpio_bank_count; i++) {
1692 struct gpio_bank *bank = &gpio_bank[i];
1693 void __iomem *wake_clear;
1694 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001695 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001696
1697 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001698#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001699 case METHOD_GPIO_1610:
1700 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1701 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1702 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001703#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001704#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001705 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001706 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1707 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001708 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001709#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301710#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001711 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301712 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1713 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1714 break;
1715#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001716 default:
1717 continue;
1718 }
1719
David Brownella6472532008-03-03 04:33:30 -08001720 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001721 __raw_writel(0xffffffff, wake_clear);
1722 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001723 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001724 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001725}
1726
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001727static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001728 .suspend = omap_gpio_suspend,
1729 .resume = omap_gpio_resume,
1730};
1731
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001732#endif
1733
Tony Lindgren140455f2010-02-12 12:26:48 -08001734#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001735
1736static int workaround_enabled;
1737
Paul Walmsley72e06d02010-12-21 21:05:16 -07001738void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001739{
1740 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001741 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001742
Tero Kristoa118b5f2008-12-22 14:27:12 +02001743 if (cpu_is_omap34xx())
1744 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001745
Tero Kristoa118b5f2008-12-22 14:27:12 +02001746 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001747 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001748 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001749 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001750
Kevin Hilman0aed04352010-09-22 16:06:27 -07001751 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001752 clk_disable(bank->dbck);
1753
Paul Walmsley72e06d02010-12-21 21:05:16 -07001754 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001755 continue;
1756
1757 /* If going to OFF, remove triggering for all
1758 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1759 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001760 if (!(bank->enabled_non_wakeup_gpios))
1761 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001762
1763 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1764 bank->saved_datain = __raw_readl(bank->base +
1765 OMAP24XX_GPIO_DATAIN);
1766 l1 = __raw_readl(bank->base +
1767 OMAP24XX_GPIO_FALLINGDETECT);
1768 l2 = __raw_readl(bank->base +
1769 OMAP24XX_GPIO_RISINGDETECT);
1770 }
1771
1772 if (cpu_is_omap44xx()) {
1773 bank->saved_datain = __raw_readl(bank->base +
1774 OMAP4_GPIO_DATAIN);
1775 l1 = __raw_readl(bank->base +
1776 OMAP4_GPIO_FALLINGDETECT);
1777 l2 = __raw_readl(bank->base +
1778 OMAP4_GPIO_RISINGDETECT);
1779 }
1780
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001781 bank->saved_fallingdetect = l1;
1782 bank->saved_risingdetect = l2;
1783 l1 &= ~bank->enabled_non_wakeup_gpios;
1784 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001785
1786 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1787 __raw_writel(l1, bank->base +
1788 OMAP24XX_GPIO_FALLINGDETECT);
1789 __raw_writel(l2, bank->base +
1790 OMAP24XX_GPIO_RISINGDETECT);
1791 }
1792
1793 if (cpu_is_omap44xx()) {
1794 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1795 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1796 }
1797
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001798 c++;
1799 }
1800 if (!c) {
1801 workaround_enabled = 0;
1802 return;
1803 }
1804 workaround_enabled = 1;
1805}
1806
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001807void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001808{
1809 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001810 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001811
Tero Kristoa118b5f2008-12-22 14:27:12 +02001812 if (cpu_is_omap34xx())
1813 min = 1;
1814 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001815 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001816 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001817 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001818
Kevin Hilman0aed04352010-09-22 16:06:27 -07001819 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001820 clk_enable(bank->dbck);
1821
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001822 if (!workaround_enabled)
1823 continue;
1824
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001825 if (!(bank->enabled_non_wakeup_gpios))
1826 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001827
1828 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1829 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001830 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001831 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001832 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001833 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1834 }
1835
1836 if (cpu_is_omap44xx()) {
1837 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301838 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001839 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301840 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001841 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1842 }
1843
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001844 /* Check if any of the non-wakeup interrupt GPIOs have changed
1845 * state. If so, generate an IRQ by software. This is
1846 * horribly racy, but it's the best we can do to work around
1847 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001848 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001849 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001850
1851 /*
1852 * No need to generate IRQs for the rising edge for gpio IRQs
1853 * configured with falling edge only; and vice versa.
1854 */
1855 gen0 = l & bank->saved_fallingdetect;
1856 gen0 &= bank->saved_datain;
1857
1858 gen1 = l & bank->saved_risingdetect;
1859 gen1 &= ~(bank->saved_datain);
1860
1861 /* FIXME: Consider GPIO IRQs with level detections properly! */
1862 gen = l & (~(bank->saved_fallingdetect) &
1863 ~(bank->saved_risingdetect));
1864 /* Consider all GPIO IRQs needed to be updated */
1865 gen |= gen0 | gen1;
1866
1867 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001868 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001869
Sergio Aguirref00d6492010-03-03 16:21:08 +00001870 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001871 old0 = __raw_readl(bank->base +
1872 OMAP24XX_GPIO_LEVELDETECT0);
1873 old1 = __raw_readl(bank->base +
1874 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001875 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001876 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001877 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001878 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001879 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001880 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001881 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001882 OMAP24XX_GPIO_LEVELDETECT1);
1883 }
1884
1885 if (cpu_is_omap44xx()) {
1886 old0 = __raw_readl(bank->base +
1887 OMAP4_GPIO_LEVELDETECT0);
1888 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301889 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001890 __raw_writel(old0 | l, bank->base +
1891 OMAP4_GPIO_LEVELDETECT0);
1892 __raw_writel(old1 | l, bank->base +
1893 OMAP4_GPIO_LEVELDETECT1);
1894 __raw_writel(old0, bank->base +
1895 OMAP4_GPIO_LEVELDETECT0);
1896 __raw_writel(old1, bank->base +
1897 OMAP4_GPIO_LEVELDETECT1);
1898 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001899 }
1900 }
1901
1902}
1903
Tony Lindgren92105bb2005-09-07 17:20:26 +01001904#endif
1905
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001906#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301907/* save the registers of bank 2-6 */
1908void omap_gpio_save_context(void)
1909{
1910 int i;
1911
1912 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1913 for (i = 1; i < gpio_bank_count; i++) {
1914 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301915 gpio_context[i].irqenable1 =
1916 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1917 gpio_context[i].irqenable2 =
1918 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1919 gpio_context[i].wake_en =
1920 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1921 gpio_context[i].ctrl =
1922 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1923 gpio_context[i].oe =
1924 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1925 gpio_context[i].leveldetect0 =
1926 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1927 gpio_context[i].leveldetect1 =
1928 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1929 gpio_context[i].risingdetect =
1930 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1931 gpio_context[i].fallingdetect =
1932 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1933 gpio_context[i].dataout =
1934 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301935 }
1936}
1937
1938/* restore the required registers of bank 2-6 */
1939void omap_gpio_restore_context(void)
1940{
1941 int i;
1942
1943 for (i = 1; i < gpio_bank_count; i++) {
1944 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301945 __raw_writel(gpio_context[i].irqenable1,
1946 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1947 __raw_writel(gpio_context[i].irqenable2,
1948 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1949 __raw_writel(gpio_context[i].wake_en,
1950 bank->base + OMAP24XX_GPIO_WAKE_EN);
1951 __raw_writel(gpio_context[i].ctrl,
1952 bank->base + OMAP24XX_GPIO_CTRL);
1953 __raw_writel(gpio_context[i].oe,
1954 bank->base + OMAP24XX_GPIO_OE);
1955 __raw_writel(gpio_context[i].leveldetect0,
1956 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1957 __raw_writel(gpio_context[i].leveldetect1,
1958 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1959 __raw_writel(gpio_context[i].risingdetect,
1960 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1961 __raw_writel(gpio_context[i].fallingdetect,
1962 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1963 __raw_writel(gpio_context[i].dataout,
1964 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301965 }
1966}
1967#endif
1968
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001969static struct platform_driver omap_gpio_driver = {
1970 .probe = omap_gpio_probe,
1971 .driver = {
1972 .name = "omap_gpio",
1973 },
1974};
1975
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001976/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001977 * gpio driver register needs to be done before
1978 * machine_init functions access gpio APIs.
1979 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001980 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001981static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001982{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001983 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001984}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001985postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001986
Tony Lindgren92105bb2005-09-07 17:20:26 +01001987static int __init omap_gpio_sysinit(void)
1988{
David Brownell11a78b72006-12-06 17:14:11 -08001989 mpuio_init();
1990
Tony Lindgren140455f2010-02-12 12:26:48 -08001991#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001992 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1993 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001994#endif
1995
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001996 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001997}
1998
Tony Lindgren92105bb2005-09-07 17:20:26 +01001999arch_initcall(omap_gpio_sysinit);