blob: 25a7ee6bddb0663a4b7960d43a89b9a2818dfa93 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080022#include <linux/slab.h>
23#include <linux/pm_runtime.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/irqs.h>
28#include <mach/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029#include <asm/mach/irq.h>
30
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031struct gpio_bank {
Tony Lindgren9f7065d2009-10-19 15:25:20 -070032 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010033 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010034 u16 irq;
35 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010036 int method;
Tony Lindgren140455f2010-02-12 12:26:48 -080037#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +010038 u32 suspend_wakeup;
39 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080040#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080041 u32 non_wakeup_gpios;
42 u32 enabled_non_wakeup_gpios;
43
44 u32 saved_datain;
45 u32 saved_fallingdetect;
46 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080047 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080048 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010049 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080050 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080051 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080052 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080053 u32 dbck_enable_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080054 struct device *dev;
55 bool dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -080056 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070057 u32 width;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010058};
59
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -080060#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +053061struct omap3_gpio_regs {
Rajendra Nayak40c670f2008-09-26 17:47:48 +053062 u32 irqenable1;
63 u32 irqenable2;
64 u32 wake_en;
65 u32 ctrl;
66 u32 oe;
67 u32 leveldetect0;
68 u32 leveldetect1;
69 u32 risingdetect;
70 u32 fallingdetect;
71 u32 dataout;
Rajendra Nayak40c670f2008-09-26 17:47:48 +053072};
73
74static struct omap3_gpio_regs gpio_context[OMAP34XX_NR_GPIOS];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -080075#endif
76
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080077/*
78 * TODO: Cleanup gpio_bank usage as it is having information
79 * related to all instances of the device
80 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010081static struct gpio_bank *gpio_bank;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080082
Varadarajan, Charulathac95d10b2010-12-07 16:26:56 -080083/* TODO: Analyze removing gpio_bank_count usage from driver code */
84int gpio_bank_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085
Kevin Hilman129fd222011-04-22 07:59:07 -070086#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
87#define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
88
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010089static inline int gpio_valid(int gpio)
90{
91 if (gpio < 0)
92 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -080093 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +030094 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010095 return -1;
96 return 0;
97 }
Tony Lindgren6e60e792006-04-02 17:46:23 +010098 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100100 if ((cpu_is_omap16xx()) && gpio < 64)
101 return 0;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700102 if (cpu_is_omap7xx() && gpio < 192)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100103 return 0;
Tony Lindgren25d6f632010-08-02 14:21:39 +0300104 if (cpu_is_omap2420() && gpio < 128)
105 return 0;
106 if (cpu_is_omap2430() && gpio < 160)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100107 return 0;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700108 if ((cpu_is_omap34xx() || cpu_is_omap44xx()) && gpio < 192)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800109 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100110 return -1;
111}
112
113static int check_gpio(int gpio)
114{
Roel Kluind32b20f2009-11-17 14:39:03 -0800115 if (unlikely(gpio_valid(gpio) < 0)) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100116 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
117 dump_stack();
118 return -1;
119 }
120 return 0;
121}
122
123static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
124{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100125 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100126 u32 l;
127
128 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800129#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800131 reg += OMAP_MPUIO_IO_CNTL / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800133#endif
134#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100135 case METHOD_GPIO_1510:
136 reg += OMAP1510_GPIO_DIR_CONTROL;
137 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800138#endif
139#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100140 case METHOD_GPIO_1610:
141 reg += OMAP1610_GPIO_DIRECTION;
142 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800143#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100144#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100145 case METHOD_GPIO_7XX:
146 reg += OMAP7XX_GPIO_DIR_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700147 break;
148#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800149#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100150 case METHOD_GPIO_24XX:
151 reg += OMAP24XX_GPIO_OE;
152 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800153#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530154#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800155 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530156 reg += OMAP4_GPIO_OE;
157 break;
158#endif
David Brownelle5c56ed2006-12-06 17:13:59 -0800159 default:
160 WARN_ON(1);
161 return;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100162 }
163 l = __raw_readl(reg);
164 if (is_input)
165 l |= 1 << gpio;
166 else
167 l &= ~(1 << gpio);
168 __raw_writel(l, reg);
169}
170
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100171static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
172{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100173 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100174 u32 l = 0;
175
176 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800177#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100178 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800179 reg += OMAP_MPUIO_OUTPUT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100180 l = __raw_readl(reg);
181 if (enable)
182 l |= 1 << gpio;
183 else
184 l &= ~(1 << gpio);
185 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800186#endif
187#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100188 case METHOD_GPIO_1510:
189 reg += OMAP1510_GPIO_DATA_OUTPUT;
190 l = __raw_readl(reg);
191 if (enable)
192 l |= 1 << gpio;
193 else
194 l &= ~(1 << gpio);
195 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800196#endif
197#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100198 case METHOD_GPIO_1610:
199 if (enable)
200 reg += OMAP1610_GPIO_SET_DATAOUT;
201 else
202 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
203 l = 1 << gpio;
204 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800205#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100206#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100207 case METHOD_GPIO_7XX:
208 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700209 l = __raw_readl(reg);
210 if (enable)
211 l |= 1 << gpio;
212 else
213 l &= ~(1 << gpio);
214 break;
215#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800216#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100217 case METHOD_GPIO_24XX:
218 if (enable)
219 reg += OMAP24XX_GPIO_SETDATAOUT;
220 else
221 reg += OMAP24XX_GPIO_CLEARDATAOUT;
222 l = 1 << gpio;
223 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800224#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530225#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800226 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530227 if (enable)
228 reg += OMAP4_GPIO_SETDATAOUT;
229 else
230 reg += OMAP4_GPIO_CLEARDATAOUT;
231 l = 1 << gpio;
232 break;
233#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100234 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800235 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100236 return;
237 }
238 __raw_writel(l, reg);
239}
240
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300241static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100242{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100243 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100244
245 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800246 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100247 reg = bank->base;
248 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800249#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100250 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800251 reg += OMAP_MPUIO_INPUT_LATCH / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100252 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800253#endif
254#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100255 case METHOD_GPIO_1510:
256 reg += OMAP1510_GPIO_DATA_INPUT;
257 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800258#endif
259#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100260 case METHOD_GPIO_1610:
261 reg += OMAP1610_GPIO_DATAIN;
262 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800263#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100264#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100265 case METHOD_GPIO_7XX:
266 reg += OMAP7XX_GPIO_DATA_INPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700267 break;
268#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800269#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100270 case METHOD_GPIO_24XX:
271 reg += OMAP24XX_GPIO_DATAIN;
272 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800273#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530274#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800275 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530276 reg += OMAP4_GPIO_DATAIN;
277 break;
278#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100279 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800280 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100281 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100282 return (__raw_readl(reg)
Kevin Hilman129fd222011-04-22 07:59:07 -0700283 & (GPIO_BIT(bank, gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100284}
285
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300286static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
287{
288 void __iomem *reg;
289
290 if (check_gpio(gpio) < 0)
291 return -EINVAL;
292 reg = bank->base;
293
294 switch (bank->method) {
295#ifdef CONFIG_ARCH_OMAP1
296 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800297 reg += OMAP_MPUIO_OUTPUT / bank->stride;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300298 break;
299#endif
300#ifdef CONFIG_ARCH_OMAP15XX
301 case METHOD_GPIO_1510:
302 reg += OMAP1510_GPIO_DATA_OUTPUT;
303 break;
304#endif
305#ifdef CONFIG_ARCH_OMAP16XX
306 case METHOD_GPIO_1610:
307 reg += OMAP1610_GPIO_DATAOUT;
308 break;
309#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100310#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100311 case METHOD_GPIO_7XX:
312 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300313 break;
314#endif
Charulatha V9f096862010-05-14 12:05:27 -0700315#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300316 case METHOD_GPIO_24XX:
317 reg += OMAP24XX_GPIO_DATAOUT;
318 break;
319#endif
Charulatha V9f096862010-05-14 12:05:27 -0700320#ifdef CONFIG_ARCH_OMAP4
321 case METHOD_GPIO_44XX:
322 reg += OMAP4_GPIO_DATAOUT;
323 break;
324#endif
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300325 default:
326 return -EINVAL;
327 }
328
Kevin Hilman129fd222011-04-22 07:59:07 -0700329 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300330}
331
Tony Lindgren92105bb2005-09-07 17:20:26 +0100332#define MOD_REG_BIT(reg, bit_mask, set) \
333do { \
334 int l = __raw_readl(base + reg); \
335 if (set) l |= bit_mask; \
336 else l &= ~bit_mask; \
337 __raw_writel(l, base + reg); \
338} while(0)
339
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700340/**
341 * _set_gpio_debounce - low level gpio debounce time
342 * @bank: the gpio bank we're acting upon
343 * @gpio: the gpio number on this @gpio
344 * @debounce: debounce time to use
345 *
346 * OMAP's debounce time is in 31us steps so we need
347 * to convert and round up to the closest unit.
348 */
349static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
350 unsigned debounce)
351{
352 void __iomem *reg = bank->base;
353 u32 val;
354 u32 l;
355
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800356 if (!bank->dbck_flag)
357 return;
358
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700359 if (debounce < 32)
360 debounce = 0x01;
361 else if (debounce > 7936)
362 debounce = 0xff;
363 else
364 debounce = (debounce / 0x1f) - 1;
365
Kevin Hilman129fd222011-04-22 07:59:07 -0700366 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700367
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800368 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700369 reg += OMAP4_GPIO_DEBOUNCINGTIME;
370 else
371 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
372
373 __raw_writel(debounce, reg);
374
375 reg = bank->base;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800376 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700377 reg += OMAP4_GPIO_DEBOUNCENABLE;
378 else
379 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
380
381 val = __raw_readl(reg);
382
383 if (debounce) {
384 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800385 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700386 } else {
387 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800388 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700389 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300390 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700391
392 __raw_writel(val, reg);
393}
394
Tony Lindgren140455f2010-02-12 12:26:48 -0800395#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700396static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
397 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100398{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800399 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100400 u32 gpio_bit = 1 << gpio;
401
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530402 if (cpu_is_omap44xx()) {
403 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
404 trigger & IRQ_TYPE_LEVEL_LOW);
405 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
406 trigger & IRQ_TYPE_LEVEL_HIGH);
407 MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
408 trigger & IRQ_TYPE_EDGE_RISING);
409 MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
410 trigger & IRQ_TYPE_EDGE_FALLING);
411 } else {
412 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
413 trigger & IRQ_TYPE_LEVEL_LOW);
414 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
415 trigger & IRQ_TYPE_LEVEL_HIGH);
416 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
417 trigger & IRQ_TYPE_EDGE_RISING);
418 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
419 trigger & IRQ_TYPE_EDGE_FALLING);
420 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800421 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530422 if (cpu_is_omap44xx()) {
Colin Cross0622b252011-06-06 13:38:17 -0700423 MOD_REG_BIT(OMAP4_GPIO_IRQWAKEN0, gpio_bit,
424 trigger != 0);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530425 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000426 /*
427 * GPIO wakeup request can only be generated on edge
428 * transitions
429 */
430 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530431 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700432 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530433 else
434 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700435 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530436 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200437 }
438 /* This part needs to be executed always for OMAP34xx */
439 if (cpu_is_omap34xx() || (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000440 /*
441 * Log the edge gpio and manually trigger the IRQ
442 * after resume if the input level changes
443 * to avoid irq lost during PER RET/OFF mode
444 * Applies for omap2 non-wakeup gpio and all omap3 gpios
445 */
446 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800447 bank->enabled_non_wakeup_gpios |= gpio_bit;
448 else
449 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
450 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700451
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530452 if (cpu_is_omap44xx()) {
453 bank->level_mask =
454 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
455 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
456 } else {
457 bank->level_mask =
458 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
459 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
460 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100461}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800462#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100463
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800464#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800465/*
466 * This only applies to chips that can't do both rising and falling edge
467 * detection at once. For all other chips, this function is a noop.
468 */
469static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
470{
471 void __iomem *reg = bank->base;
472 u32 l = 0;
473
474 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800475 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800476 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800477 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800478#ifdef CONFIG_ARCH_OMAP15XX
479 case METHOD_GPIO_1510:
480 reg += OMAP1510_GPIO_INT_CONTROL;
481 break;
482#endif
483#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
484 case METHOD_GPIO_7XX:
485 reg += OMAP7XX_GPIO_INT_CONTROL;
486 break;
487#endif
488 default:
489 return;
490 }
491
492 l = __raw_readl(reg);
493 if ((l >> gpio) & 1)
494 l &= ~(1 << gpio);
495 else
496 l |= 1 << gpio;
497
498 __raw_writel(l, reg);
499}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800500#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800501
Tony Lindgren92105bb2005-09-07 17:20:26 +0100502static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
503{
504 void __iomem *reg = bank->base;
505 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100506
507 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800508#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100509 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800510 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100511 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000512 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800513 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100514 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100515 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100516 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100517 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100518 else
519 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100520 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800521#endif
522#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100523 case METHOD_GPIO_1510:
524 reg += OMAP1510_GPIO_INT_CONTROL;
525 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000526 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800527 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100528 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100530 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100531 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100532 else
533 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100534 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800535#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800536#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100537 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100538 if (gpio & 0x08)
539 reg += OMAP1610_GPIO_EDGE_CTRL2;
540 else
541 reg += OMAP1610_GPIO_EDGE_CTRL1;
542 gpio &= 0x07;
543 l = __raw_readl(reg);
544 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100545 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100546 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100547 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100548 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800549 if (trigger)
550 /* Enable wake-up during idle for dynamic tick */
551 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
552 else
553 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100554 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800555#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100556#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100557 case METHOD_GPIO_7XX:
558 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700559 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;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700562 if (trigger & IRQ_TYPE_EDGE_RISING)
563 l |= 1 << gpio;
564 else if (trigger & IRQ_TYPE_EDGE_FALLING)
565 l &= ~(1 << gpio);
566 else
567 goto bad;
568 break;
569#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800570#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100571 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800572 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800573 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200574 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800575#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100577 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100579 __raw_writel(l, reg);
580 return 0;
581bad:
582 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100583}
584
Lennert Buytenheke9191022010-11-29 11:17:17 +0100585static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100586{
587 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100588 unsigned gpio;
589 int retval;
David Brownella6472532008-03-03 04:33:30 -0800590 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100591
Lennert Buytenheke9191022010-11-29 11:17:17 +0100592 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
593 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100594 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100595 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100596
597 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100598 return -EINVAL;
599
David Brownelle5c56ed2006-12-06 17:13:59 -0800600 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100601 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800602
603 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800604 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800605 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100606 return -EINVAL;
607
Lennert Buytenheke9191022010-11-29 11:17:17 +0100608 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800609 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700610 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800611 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800612
613 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100614 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800615 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100616 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800617
Tony Lindgren92105bb2005-09-07 17:20:26 +0100618 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100619}
620
621static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
622{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100623 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100624
625 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800626#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100627 case METHOD_GPIO_1510:
628 reg += OMAP1510_GPIO_INT_STATUS;
629 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800630#endif
631#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100632 case METHOD_GPIO_1610:
633 reg += OMAP1610_GPIO_IRQSTATUS1;
634 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800635#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100636#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100637 case METHOD_GPIO_7XX:
638 reg += OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700639 break;
640#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800641#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100642 case METHOD_GPIO_24XX:
643 reg += OMAP24XX_GPIO_IRQSTATUS1;
644 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800645#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530646#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800647 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530648 reg += OMAP4_GPIO_IRQSTATUS0;
649 break;
650#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100651 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800652 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100653 return;
654 }
655 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300656
657 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800658 if (cpu_is_omap24xx() || cpu_is_omap34xx())
659 reg = bank->base + OMAP24XX_GPIO_IRQSTATUS2;
660 else if (cpu_is_omap44xx())
661 reg = bank->base + OMAP4_GPIO_IRQSTATUS1;
662
Kevin Hilmandf3c8512011-04-21 09:08:15 -0700663 if (cpu_is_omap24xx() || cpu_is_omap34xx() || cpu_is_omap44xx())
Roger Quadrosbedfd152009-04-23 11:10:50 -0700664 __raw_writel(gpio_mask, reg);
665
666 /* Flush posted write for the irq status to avoid spurious interrupts */
667 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100668}
669
670static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
671{
Kevin Hilman129fd222011-04-22 07:59:07 -0700672 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100673}
674
Imre Deakea6dedd2006-06-26 16:16:00 -0700675static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
676{
677 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700678 int inv = 0;
679 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700680 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700681
682 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800683#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700684 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800685 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Imre Deak99c47702006-06-26 16:16:07 -0700686 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700687 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800688#endif
689#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700690 case METHOD_GPIO_1510:
691 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700692 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700693 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800694#endif
695#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700696 case METHOD_GPIO_1610:
697 reg += OMAP1610_GPIO_IRQENABLE1;
698 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800699#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100700#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100701 case METHOD_GPIO_7XX:
702 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700703 inv = 1;
704 break;
705#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800706#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Imre Deakea6dedd2006-06-26 16:16:00 -0700707 case METHOD_GPIO_24XX:
708 reg += OMAP24XX_GPIO_IRQENABLE1;
709 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800710#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530711#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800712 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530713 reg += OMAP4_GPIO_IRQSTATUSSET0;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530714 break;
715#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700716 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800717 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700718 return 0;
719 }
720
Imre Deak99c47702006-06-26 16:16:07 -0700721 l = __raw_readl(reg);
722 if (inv)
723 l = ~l;
724 l &= mask;
725 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700726}
727
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100728static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
729{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100730 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100731 u32 l;
732
733 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800734#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100735 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800736 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100737 l = __raw_readl(reg);
738 if (enable)
739 l &= ~(gpio_mask);
740 else
741 l |= gpio_mask;
742 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800743#endif
744#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100745 case METHOD_GPIO_1510:
746 reg += OMAP1510_GPIO_INT_MASK;
747 l = __raw_readl(reg);
748 if (enable)
749 l &= ~(gpio_mask);
750 else
751 l |= gpio_mask;
752 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800753#endif
754#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100755 case METHOD_GPIO_1610:
756 if (enable)
757 reg += OMAP1610_GPIO_SET_IRQENABLE1;
758 else
759 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
760 l = gpio_mask;
761 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800762#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100763#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100764 case METHOD_GPIO_7XX:
765 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700766 l = __raw_readl(reg);
767 if (enable)
768 l &= ~(gpio_mask);
769 else
770 l |= gpio_mask;
771 break;
772#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800773#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100774 case METHOD_GPIO_24XX:
775 if (enable)
776 reg += OMAP24XX_GPIO_SETIRQENABLE1;
777 else
778 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
779 l = gpio_mask;
780 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800781#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530782#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800783 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530784 if (enable)
785 reg += OMAP4_GPIO_IRQSTATUSSET0;
786 else
787 reg += OMAP4_GPIO_IRQSTATUSCLR0;
788 l = gpio_mask;
789 break;
790#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100791 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800792 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100793 return;
794 }
795 __raw_writel(l, reg);
796}
797
798static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
799{
Kevin Hilman129fd222011-04-22 07:59:07 -0700800 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio), enable);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100801}
802
Tony Lindgren92105bb2005-09-07 17:20:26 +0100803/*
804 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
805 * 1510 does not seem to have a wake-up register. If JTAG is connected
806 * to the target, system will wake up always on GPIO events. While
807 * system is running all registered GPIO interrupts need to have wake-up
808 * enabled. When system is suspended, only selected GPIO interrupts need
809 * to have wake-up enabled.
810 */
811static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
812{
Tony Lindgren4cc64202010-01-08 10:29:05 -0800813 unsigned long uninitialized_var(flags);
David Brownella6472532008-03-03 04:33:30 -0800814
Tony Lindgren92105bb2005-09-07 17:20:26 +0100815 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800816#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800817 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100818 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800819 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700820 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100821 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700822 else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100823 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800824 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100825 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800826#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800827#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800828 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800829 case METHOD_GPIO_44XX:
David Brownell11a78b72006-12-06 17:14:11 -0800830 if (bank->non_wakeup_gpios & (1 << gpio)) {
831 printk(KERN_ERR "Unable to modify wakeup on "
832 "non-wakeup GPIO%d\n",
Kevin Hilmand5f46242011-04-21 09:23:00 -0700833 (bank - gpio_bank) * bank->width + gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800834 return -EINVAL;
835 }
David Brownella6472532008-03-03 04:33:30 -0800836 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700837 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800838 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700839 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800840 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800841 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800842 return 0;
843#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100844 default:
845 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
846 bank->method);
847 return -EINVAL;
848 }
849}
850
Tony Lindgren4196dd62006-09-25 12:41:38 +0300851static void _reset_gpio(struct gpio_bank *bank, int gpio)
852{
Kevin Hilman129fd222011-04-22 07:59:07 -0700853 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300854 _set_gpio_irqenable(bank, gpio, 0);
855 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700856 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300857}
858
Tony Lindgren92105bb2005-09-07 17:20:26 +0100859/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100860static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100861{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100862 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100863 struct gpio_bank *bank;
864 int retval;
865
866 if (check_gpio(gpio) < 0)
867 return -ENODEV;
Lennert Buytenheke9191022010-11-29 11:17:17 +0100868 bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700869 retval = _set_gpio_wakeup(bank, GPIO_INDEX(bank, gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100870
871 return retval;
872}
873
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800874static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100875{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800876 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800877 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100878
David Brownella6472532008-03-03 04:33:30 -0800879 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100880
Tony Lindgren4196dd62006-09-25 12:41:38 +0300881 /* Set trigger to none. You need to enable the desired trigger with
882 * request_irq() or set_irq_type().
883 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800884 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100885
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000886#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100887 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100888 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100889
Tony Lindgren92105bb2005-09-07 17:20:26 +0100890 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100891 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800892 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100893 }
894#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800895 if (!cpu_class_is_omap1()) {
896 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700897 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800898 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700899
900 if (cpu_is_omap24xx() || cpu_is_omap34xx())
901 reg += OMAP24XX_GPIO_CTRL;
902 else if (cpu_is_omap44xx())
903 reg += OMAP4_GPIO_CTRL;
904 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800905 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700906 ctrl &= 0xFFFFFFFE;
907 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800908 }
909 bank->mod_usage |= 1 << offset;
910 }
David Brownella6472532008-03-03 04:33:30 -0800911 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100912
913 return 0;
914}
915
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800916static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100917{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800918 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800919 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100920
David Brownella6472532008-03-03 04:33:30 -0800921 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100922#ifdef CONFIG_ARCH_OMAP16XX
923 if (bank->method == METHOD_GPIO_1610) {
924 /* Disable wake-up during idle for dynamic tick */
925 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800926 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100927 }
928#endif
Charulatha V9f096862010-05-14 12:05:27 -0700929#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
930 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100931 /* Disable wake-up during idle for dynamic tick */
932 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800933 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100934 }
935#endif
Charulatha V9f096862010-05-14 12:05:27 -0700936#ifdef CONFIG_ARCH_OMAP4
937 if (bank->method == METHOD_GPIO_44XX) {
938 /* Disable wake-up during idle for dynamic tick */
939 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
940 __raw_writel(1 << offset, reg);
941 }
942#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800943 if (!cpu_class_is_omap1()) {
944 bank->mod_usage &= ~(1 << offset);
945 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700946 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800947 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700948
949 if (cpu_is_omap24xx() || cpu_is_omap34xx())
950 reg += OMAP24XX_GPIO_CTRL;
951 else if (cpu_is_omap44xx())
952 reg += OMAP4_GPIO_CTRL;
953 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800954 /* Module is disabled, clocks are gated */
955 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700956 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800957 }
958 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800959 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800960 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100961}
962
963/*
964 * We need to unmask the GPIO bank interrupt as soon as possible to
965 * avoid missing GPIO interrupts for other lines in the bank.
966 * Then we need to mask-read-clear-unmask the triggered GPIO lines
967 * in the bank to avoid missing nested interrupts for a GPIO line.
968 * If we wait to unmask individual GPIO lines in the bank after the
969 * line's interrupt handler has been run, we may miss some nested
970 * interrupts.
971 */
Russell King10dd5ce2006-11-23 11:41:32 +0000972static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100973{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100974 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100975 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800976 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100977 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700978 u32 retrigger = 0;
979 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000980 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100981
Will Deaconee144182011-02-21 13:46:08 +0000982 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100983
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100984 bank = irq_get_handler_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -0800985#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100986 if (bank->method == METHOD_MPUIO)
Tony Lindgren5de62b82010-12-07 16:26:58 -0800987 isr_reg = bank->base +
988 OMAP_MPUIO_GPIO_INT / bank->stride;
David Brownelle5c56ed2006-12-06 17:13:59 -0800989#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000990#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100991 if (bank->method == METHOD_GPIO_1510)
992 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
993#endif
994#if defined(CONFIG_ARCH_OMAP16XX)
995 if (bank->method == METHOD_GPIO_1610)
996 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
997#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100998#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100999 if (bank->method == METHOD_GPIO_7XX)
1000 isr_reg = bank->base + OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001001#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001002#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001003 if (bank->method == METHOD_GPIO_24XX)
1004 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1005#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301006#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001007 if (bank->method == METHOD_GPIO_44XX)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301008 isr_reg = bank->base + OMAP4_GPIO_IRQSTATUS0;
1009#endif
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -08001010
1011 if (WARN_ON(!isr_reg))
1012 goto exit;
1013
Tony Lindgren92105bb2005-09-07 17:20:26 +01001014 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001015 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -07001016 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001017
Imre Deakea6dedd2006-06-26 16:16:00 -07001018 enabled = _get_gpio_irqbank_mask(bank);
1019 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001020
1021 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1022 isr &= 0x0000ffff;
1023
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001024 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001025 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -07001026 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001027
1028 /* clear edge sensitive interrupts before handler(s) are
1029 called so that we don't miss any interrupt occurred while
1030 executing them */
1031 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1032 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1033 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1034
1035 /* if there is only edge sensitive GPIO pin interrupts
1036 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -07001037 if (!level_mask && !unmasked) {
1038 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +00001039 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -07001040 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001041
Imre Deakea6dedd2006-06-26 16:16:00 -07001042 isr |= retrigger;
1043 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001044 if (!isr)
1045 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001046
Tony Lindgren92105bb2005-09-07 17:20:26 +01001047 gpio_irq = bank->virtual_irq_start;
1048 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -07001049 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -08001050
Tony Lindgren92105bb2005-09-07 17:20:26 +01001051 if (!(isr & 1))
1052 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001053
Cory Maccarrone4318f362010-01-08 10:29:04 -08001054#ifdef CONFIG_ARCH_OMAP1
1055 /*
1056 * Some chips can't respond to both rising and falling
1057 * at the same time. If this irq was requested with
1058 * both flags, we need to flip the ICR data for the IRQ
1059 * to respond to the IRQ for the opposite direction.
1060 * This will be indicated in the bank toggle_mask.
1061 */
1062 if (bank->toggle_mask & (1 << gpio_index))
1063 _toggle_gpio_edge_triggering(bank, gpio_index);
1064#endif
1065
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +01001066 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001067 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001068 }
Imre Deakea6dedd2006-06-26 16:16:00 -07001069 /* if bank has any level sensitive GPIO pin interrupt
1070 configured, we must unmask the bank interrupt only after
1071 handler(s) are executed in order to avoid spurious bank
1072 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -08001073exit:
Imre Deakea6dedd2006-06-26 16:16:00 -07001074 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +00001075 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001076}
1077
Lennert Buytenheke9191022010-11-29 11:17:17 +01001078static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +03001079{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001080 unsigned int gpio = d->irq - IH_GPIO_BASE;
1081 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -07001082 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +03001083
Colin Cross85ec7b92011-06-06 13:38:18 -07001084 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001085 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -07001086 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001087}
1088
Lennert Buytenheke9191022010-11-29 11:17:17 +01001089static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001090{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001091 unsigned int gpio = d->irq - IH_GPIO_BASE;
1092 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001093
1094 _clear_gpio_irqstatus(bank, gpio);
1095}
1096
Lennert Buytenheke9191022010-11-29 11:17:17 +01001097static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001098{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001099 unsigned int gpio = d->irq - IH_GPIO_BASE;
1100 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -07001101 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001102
Colin Cross85ec7b92011-06-06 13:38:18 -07001103 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001104 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -07001105 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -07001106 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001107}
1108
Lennert Buytenheke9191022010-11-29 11:17:17 +01001109static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001110{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001111 unsigned int gpio = d->irq - IH_GPIO_BASE;
1112 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -07001113 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +01001114 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -07001115 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -07001116
Colin Cross85ec7b92011-06-06 13:38:18 -07001117 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -07001118 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -07001119 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001120
1121 /* For level-triggered GPIOs, the clearing must be done after
1122 * the HW source is cleared, thus after the handler has run */
1123 if (bank->level_mask & irq_mask) {
1124 _set_gpio_irqenable(bank, gpio, 0);
1125 _clear_gpio_irqstatus(bank, gpio);
1126 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001127
Kevin Hilman4de8c752008-01-16 21:56:14 -08001128 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -07001129 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001130}
1131
David Brownelle5c56ed2006-12-06 17:13:59 -08001132static struct irq_chip gpio_irq_chip = {
1133 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +01001134 .irq_shutdown = gpio_irq_shutdown,
1135 .irq_ack = gpio_ack_irq,
1136 .irq_mask = gpio_mask_irq,
1137 .irq_unmask = gpio_unmask_irq,
1138 .irq_set_type = gpio_irq_type,
1139 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -08001140};
1141
1142/*---------------------------------------------------------------------*/
1143
1144#ifdef CONFIG_ARCH_OMAP1
1145
1146/* MPUIO uses the always-on 32k clock */
1147
Lennert Buytenheke9191022010-11-29 11:17:17 +01001148static void mpuio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001149{
1150 /* The ISR is reset automatically, so do nothing here. */
1151}
1152
Lennert Buytenheke9191022010-11-29 11:17:17 +01001153static void mpuio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001154{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001155 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1156 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001157
1158 _set_gpio_irqenable(bank, gpio, 0);
1159}
1160
Lennert Buytenheke9191022010-11-29 11:17:17 +01001161static void mpuio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001162{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001163 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1164 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001165
1166 _set_gpio_irqenable(bank, gpio, 1);
1167}
1168
David Brownelle5c56ed2006-12-06 17:13:59 -08001169static struct irq_chip mpuio_irq_chip = {
1170 .name = "MPUIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +01001171 .irq_ack = mpuio_ack_irq,
1172 .irq_mask = mpuio_mask_irq,
1173 .irq_unmask = mpuio_unmask_irq,
1174 .irq_set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001175#ifdef CONFIG_ARCH_OMAP16XX
1176 /* REVISIT: assuming only 16xx supports MPUIO wake events */
Lennert Buytenheke9191022010-11-29 11:17:17 +01001177 .irq_set_wake = gpio_wake_enable,
David Brownell11a78b72006-12-06 17:14:11 -08001178#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001179};
1180
David Brownelle5c56ed2006-12-06 17:13:59 -08001181
1182#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1183
David Brownell11a78b72006-12-06 17:14:11 -08001184
1185#ifdef CONFIG_ARCH_OMAP16XX
1186
1187#include <linux/platform_device.h>
1188
Magnus Damm79ee0312009-07-08 13:22:04 +02001189static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001190{
Magnus Damm79ee0312009-07-08 13:22:04 +02001191 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001192 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001193 void __iomem *mask_reg = bank->base +
1194 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001195 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001196
David Brownella6472532008-03-03 04:33:30 -08001197 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001198 bank->saved_wakeup = __raw_readl(mask_reg);
1199 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001200 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001201
1202 return 0;
1203}
1204
Magnus Damm79ee0312009-07-08 13:22:04 +02001205static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001206{
Magnus Damm79ee0312009-07-08 13:22:04 +02001207 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001208 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001209 void __iomem *mask_reg = bank->base +
1210 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001211 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001212
David Brownella6472532008-03-03 04:33:30 -08001213 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001214 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001215 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001216
1217 return 0;
1218}
1219
Alexey Dobriyan47145212009-12-14 18:00:08 -08001220static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +02001221 .suspend_noirq = omap_mpuio_suspend_noirq,
1222 .resume_noirq = omap_mpuio_resume_noirq,
1223};
1224
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001225/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -08001226static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -08001227 .driver = {
1228 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +02001229 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -08001230 },
1231};
1232
1233static struct platform_device omap_mpuio_device = {
1234 .name = "mpuio",
1235 .id = -1,
1236 .dev = {
1237 .driver = &omap_mpuio_driver.driver,
1238 }
1239 /* could list the /proc/iomem resources */
1240};
1241
1242static inline void mpuio_init(void)
1243{
Charulatha Va8be8da2011-04-22 16:38:16 +05301244 struct gpio_bank *bank = &gpio_bank[0];
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001245 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -07001246
David Brownell11a78b72006-12-06 17:14:11 -08001247 if (platform_driver_register(&omap_mpuio_driver) == 0)
1248 (void) platform_device_register(&omap_mpuio_device);
1249}
1250
1251#else
1252static inline void mpuio_init(void) {}
1253#endif /* 16xx */
1254
David Brownelle5c56ed2006-12-06 17:13:59 -08001255#else
1256
1257extern struct irq_chip mpuio_irq_chip;
1258
1259#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001260static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001261
1262#endif
1263
1264/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001265
David Brownell52e31342008-03-03 12:43:23 -08001266/* REVISIT these are stupid implementations! replace by ones that
1267 * don't switch on METHOD_* and which mostly avoid spinlocks
1268 */
1269
1270static int gpio_input(struct gpio_chip *chip, unsigned offset)
1271{
1272 struct gpio_bank *bank;
1273 unsigned long flags;
1274
1275 bank = container_of(chip, struct gpio_bank, chip);
1276 spin_lock_irqsave(&bank->lock, flags);
1277 _set_gpio_direction(bank, offset, 1);
1278 spin_unlock_irqrestore(&bank->lock, flags);
1279 return 0;
1280}
1281
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001282static int gpio_is_input(struct gpio_bank *bank, int mask)
1283{
1284 void __iomem *reg = bank->base;
1285
1286 switch (bank->method) {
1287 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -08001288 reg += OMAP_MPUIO_IO_CNTL / bank->stride;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001289 break;
1290 case METHOD_GPIO_1510:
1291 reg += OMAP1510_GPIO_DIR_CONTROL;
1292 break;
1293 case METHOD_GPIO_1610:
1294 reg += OMAP1610_GPIO_DIRECTION;
1295 break;
Alistair Buxton7c006922009-09-22 10:02:58 +01001296 case METHOD_GPIO_7XX:
1297 reg += OMAP7XX_GPIO_DIR_CONTROL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001298 break;
1299 case METHOD_GPIO_24XX:
1300 reg += OMAP24XX_GPIO_OE;
1301 break;
Charulatha V9f096862010-05-14 12:05:27 -07001302 case METHOD_GPIO_44XX:
1303 reg += OMAP4_GPIO_OE;
1304 break;
1305 default:
1306 WARN_ONCE(1, "gpio_is_input: incorrect OMAP GPIO method");
1307 return -EINVAL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001308 }
1309 return __raw_readl(reg) & mask;
1310}
1311
David Brownell52e31342008-03-03 12:43:23 -08001312static int gpio_get(struct gpio_chip *chip, unsigned offset)
1313{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001314 struct gpio_bank *bank;
1315 void __iomem *reg;
1316 int gpio;
1317 u32 mask;
1318
1319 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +05301320 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001321 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -07001322 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001323
1324 if (gpio_is_input(bank, mask))
1325 return _get_gpio_datain(bank, gpio);
1326 else
1327 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -08001328}
1329
1330static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1331{
1332 struct gpio_bank *bank;
1333 unsigned long flags;
1334
1335 bank = container_of(chip, struct gpio_bank, chip);
1336 spin_lock_irqsave(&bank->lock, flags);
1337 _set_gpio_dataout(bank, offset, value);
1338 _set_gpio_direction(bank, offset, 0);
1339 spin_unlock_irqrestore(&bank->lock, flags);
1340 return 0;
1341}
1342
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001343static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
1344 unsigned debounce)
1345{
1346 struct gpio_bank *bank;
1347 unsigned long flags;
1348
1349 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001350
1351 if (!bank->dbck) {
1352 bank->dbck = clk_get(bank->dev, "dbclk");
1353 if (IS_ERR(bank->dbck))
1354 dev_err(bank->dev, "Could not get gpio dbck\n");
1355 }
1356
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001357 spin_lock_irqsave(&bank->lock, flags);
1358 _set_gpio_debounce(bank, offset, debounce);
1359 spin_unlock_irqrestore(&bank->lock, flags);
1360
1361 return 0;
1362}
1363
David Brownell52e31342008-03-03 12:43:23 -08001364static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1365{
1366 struct gpio_bank *bank;
1367 unsigned long flags;
1368
1369 bank = container_of(chip, struct gpio_bank, chip);
1370 spin_lock_irqsave(&bank->lock, flags);
1371 _set_gpio_dataout(bank, offset, value);
1372 spin_unlock_irqrestore(&bank->lock, flags);
1373}
1374
David Brownella007b702008-12-10 17:35:25 -08001375static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1376{
1377 struct gpio_bank *bank;
1378
1379 bank = container_of(chip, struct gpio_bank, chip);
1380 return bank->virtual_irq_start + offset;
1381}
1382
David Brownell52e31342008-03-03 12:43:23 -08001383/*---------------------------------------------------------------------*/
1384
Tony Lindgren9a748052010-12-07 16:26:56 -08001385static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001386{
1387 u32 rev;
1388
Tony Lindgren9a748052010-12-07 16:26:56 -08001389 if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
1390 rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001391 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001392 rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001393 else if (cpu_is_omap44xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001394 rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001395 else
1396 return;
1397
1398 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1399 (rev >> 4) & 0x0f, rev & 0x0f);
1400}
1401
David Brownell8ba55c52008-02-26 11:10:50 -08001402/* This lock class tells lockdep that GPIO irqs are in a different
1403 * category than their parents, so it won't report false recursion.
1404 */
1405static struct lock_class_key gpio_lock_class;
1406
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001407static inline int init_gpio_info(struct platform_device *pdev)
1408{
1409 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1410 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1411 GFP_KERNEL);
1412 if (!gpio_bank) {
1413 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1414 return -ENOMEM;
1415 }
1416 return 0;
1417}
1418
1419/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001420static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1421{
1422 if (cpu_class_is_omap2()) {
1423 if (cpu_is_omap44xx()) {
1424 __raw_writel(0xffffffff, bank->base +
1425 OMAP4_GPIO_IRQSTATUSCLR0);
1426 __raw_writel(0x00000000, bank->base +
1427 OMAP4_GPIO_DEBOUNCENABLE);
1428 /* Initialize interface clk ungated, module enabled */
1429 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1430 } else if (cpu_is_omap34xx()) {
1431 __raw_writel(0x00000000, bank->base +
1432 OMAP24XX_GPIO_IRQENABLE1);
1433 __raw_writel(0xffffffff, bank->base +
1434 OMAP24XX_GPIO_IRQSTATUS1);
1435 __raw_writel(0x00000000, bank->base +
1436 OMAP24XX_GPIO_DEBOUNCE_EN);
1437
1438 /* Initialize interface clk ungated, module enabled */
1439 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1440 } else if (cpu_is_omap24xx()) {
1441 static const u32 non_wakeup_gpios[] = {
1442 0xe203ffc0, 0x08700040
1443 };
1444 if (id < ARRAY_SIZE(non_wakeup_gpios))
1445 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1446 }
1447 } else if (cpu_class_is_omap1()) {
1448 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001449 __raw_writew(0xffff, bank->base +
1450 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001451 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1452 __raw_writew(0xffff, bank->base
1453 + OMAP1510_GPIO_INT_MASK);
1454 __raw_writew(0x0000, bank->base
1455 + OMAP1510_GPIO_INT_STATUS);
1456 }
1457 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1458 __raw_writew(0x0000, bank->base
1459 + OMAP1610_GPIO_IRQENABLE1);
1460 __raw_writew(0xffff, bank->base
1461 + OMAP1610_GPIO_IRQSTATUS1);
1462 __raw_writew(0x0014, bank->base
1463 + OMAP1610_GPIO_SYSCONFIG);
1464
1465 /*
1466 * Enable system clock for GPIO module.
1467 * The CAM_CLK_CTRL *is* really the right place.
1468 */
1469 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1470 ULPD_CAM_CLK_CTRL);
1471 }
1472 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1473 __raw_writel(0xffffffff, bank->base
1474 + OMAP7XX_GPIO_INT_MASK);
1475 __raw_writel(0x00000000, bank->base
1476 + OMAP7XX_GPIO_INT_STATUS);
1477 }
1478 }
1479}
1480
Russell Kingd52b31d2011-05-27 13:56:12 -07001481static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001482{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001483 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001484 static int gpio;
1485
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001486 bank->mod_usage = 0;
1487 /*
1488 * REVISIT eventually switch from OMAP-specific gpio structs
1489 * over to the generic ones
1490 */
1491 bank->chip.request = omap_gpio_request;
1492 bank->chip.free = omap_gpio_free;
1493 bank->chip.direction_input = gpio_input;
1494 bank->chip.get = gpio_get;
1495 bank->chip.direction_output = gpio_output;
1496 bank->chip.set_debounce = gpio_debounce;
1497 bank->chip.set = gpio_set;
1498 bank->chip.to_irq = gpio_2irq;
1499 if (bank_is_mpuio(bank)) {
1500 bank->chip.label = "mpuio";
1501#ifdef CONFIG_ARCH_OMAP16XX
1502 bank->chip.dev = &omap_mpuio_device.dev;
1503#endif
1504 bank->chip.base = OMAP_MPUIO(0);
1505 } else {
1506 bank->chip.label = "gpio";
1507 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001508 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001509 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001510 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001511
1512 gpiochip_add(&bank->chip);
1513
1514 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001515 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001516 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001517 irq_set_chip_data(j, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001518 if (bank_is_mpuio(bank))
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001519 irq_set_chip(j, &mpuio_irq_chip);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001520 else
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001521 irq_set_chip(j, &gpio_irq_chip);
1522 irq_set_handler(j, handle_simple_irq);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001523 set_irq_flags(j, IRQF_VALID);
1524 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001525 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1526 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001527}
1528
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001529static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001530{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001531 static int gpio_init_done;
1532 struct omap_gpio_platform_data *pdata;
1533 struct resource *res;
1534 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001535 struct gpio_bank *bank;
1536
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001537 if (!pdev->dev.platform_data)
1538 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001539
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001540 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001541
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001542 if (!gpio_init_done) {
1543 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001544
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001545 ret = init_gpio_info(pdev);
1546 if (ret)
1547 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001548 }
1549
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001550 id = pdev->id;
1551 bank = &gpio_bank[id];
1552
1553 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1554 if (unlikely(!res)) {
1555 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1556 return -ENODEV;
1557 }
1558
1559 bank->irq = res->start;
1560 bank->virtual_irq_start = pdata->virtual_irq_start;
1561 bank->method = pdata->bank_type;
1562 bank->dev = &pdev->dev;
1563 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001564 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001565 bank->width = pdata->bank_width;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001566
1567 spin_lock_init(&bank->lock);
1568
1569 /* Static mapping, never released */
1570 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1571 if (unlikely(!res)) {
1572 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1573 return -ENODEV;
1574 }
1575
1576 bank->base = ioremap(res->start, resource_size(res));
1577 if (!bank->base) {
1578 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1579 return -ENOMEM;
1580 }
1581
1582 pm_runtime_enable(bank->dev);
1583 pm_runtime_get_sync(bank->dev);
1584
1585 omap_gpio_mod_init(bank, id);
1586 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001587 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001588
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001589 if (!gpio_init_done)
1590 gpio_init_done = 1;
1591
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001592 return 0;
1593}
1594
Tony Lindgren140455f2010-02-12 12:26:48 -08001595#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001596static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001597{
1598 int i;
1599
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001600 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001601 return 0;
1602
1603 for (i = 0; i < gpio_bank_count; i++) {
1604 struct gpio_bank *bank = &gpio_bank[i];
1605 void __iomem *wake_status;
1606 void __iomem *wake_clear;
1607 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001608 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001609
1610 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001611#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001612 case METHOD_GPIO_1610:
1613 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1614 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1615 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1616 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001617#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001618#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001619 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001620 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001621 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1622 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1623 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001624#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301625#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001626 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301627 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1628 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1629 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1630 break;
1631#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001632 default:
1633 continue;
1634 }
1635
David Brownella6472532008-03-03 04:33:30 -08001636 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001637 bank->saved_wakeup = __raw_readl(wake_status);
1638 __raw_writel(0xffffffff, wake_clear);
1639 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001640 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001641 }
1642
1643 return 0;
1644}
1645
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001646static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001647{
1648 int i;
1649
Tero Kristo723fdb72008-11-26 14:35:16 -08001650 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001651 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001652
1653 for (i = 0; i < gpio_bank_count; i++) {
1654 struct gpio_bank *bank = &gpio_bank[i];
1655 void __iomem *wake_clear;
1656 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001657 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001658
1659 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001660#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001661 case METHOD_GPIO_1610:
1662 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1663 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1664 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001665#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001666#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001667 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001668 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1669 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001670 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001671#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301672#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001673 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301674 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1675 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1676 break;
1677#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001678 default:
1679 continue;
1680 }
1681
David Brownella6472532008-03-03 04:33:30 -08001682 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001683 __raw_writel(0xffffffff, wake_clear);
1684 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001685 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001686 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001687}
1688
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001689static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001690 .suspend = omap_gpio_suspend,
1691 .resume = omap_gpio_resume,
1692};
1693
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001694#endif
1695
Tony Lindgren140455f2010-02-12 12:26:48 -08001696#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001697
1698static int workaround_enabled;
1699
Paul Walmsley72e06d02010-12-21 21:05:16 -07001700void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001701{
1702 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001703 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001704
Tero Kristoa118b5f2008-12-22 14:27:12 +02001705 if (cpu_is_omap34xx())
1706 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001707
Tero Kristoa118b5f2008-12-22 14:27:12 +02001708 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001709 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001710 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001711 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001712
Kevin Hilman0aed04352010-09-22 16:06:27 -07001713 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001714 clk_disable(bank->dbck);
1715
Paul Walmsley72e06d02010-12-21 21:05:16 -07001716 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001717 continue;
1718
1719 /* If going to OFF, remove triggering for all
1720 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1721 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001722 if (!(bank->enabled_non_wakeup_gpios))
1723 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001724
1725 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1726 bank->saved_datain = __raw_readl(bank->base +
1727 OMAP24XX_GPIO_DATAIN);
1728 l1 = __raw_readl(bank->base +
1729 OMAP24XX_GPIO_FALLINGDETECT);
1730 l2 = __raw_readl(bank->base +
1731 OMAP24XX_GPIO_RISINGDETECT);
1732 }
1733
1734 if (cpu_is_omap44xx()) {
1735 bank->saved_datain = __raw_readl(bank->base +
1736 OMAP4_GPIO_DATAIN);
1737 l1 = __raw_readl(bank->base +
1738 OMAP4_GPIO_FALLINGDETECT);
1739 l2 = __raw_readl(bank->base +
1740 OMAP4_GPIO_RISINGDETECT);
1741 }
1742
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001743 bank->saved_fallingdetect = l1;
1744 bank->saved_risingdetect = l2;
1745 l1 &= ~bank->enabled_non_wakeup_gpios;
1746 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001747
1748 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1749 __raw_writel(l1, bank->base +
1750 OMAP24XX_GPIO_FALLINGDETECT);
1751 __raw_writel(l2, bank->base +
1752 OMAP24XX_GPIO_RISINGDETECT);
1753 }
1754
1755 if (cpu_is_omap44xx()) {
1756 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1757 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1758 }
1759
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001760 c++;
1761 }
1762 if (!c) {
1763 workaround_enabled = 0;
1764 return;
1765 }
1766 workaround_enabled = 1;
1767}
1768
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001769void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001770{
1771 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001772 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001773
Tero Kristoa118b5f2008-12-22 14:27:12 +02001774 if (cpu_is_omap34xx())
1775 min = 1;
1776 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001777 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001778 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001779 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001780
Kevin Hilman0aed04352010-09-22 16:06:27 -07001781 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001782 clk_enable(bank->dbck);
1783
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001784 if (!workaround_enabled)
1785 continue;
1786
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001787 if (!(bank->enabled_non_wakeup_gpios))
1788 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001789
1790 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1791 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001792 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001793 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001794 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001795 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1796 }
1797
1798 if (cpu_is_omap44xx()) {
1799 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301800 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001801 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301802 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001803 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1804 }
1805
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001806 /* Check if any of the non-wakeup interrupt GPIOs have changed
1807 * state. If so, generate an IRQ by software. This is
1808 * horribly racy, but it's the best we can do to work around
1809 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001810 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001811 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001812
1813 /*
1814 * No need to generate IRQs for the rising edge for gpio IRQs
1815 * configured with falling edge only; and vice versa.
1816 */
1817 gen0 = l & bank->saved_fallingdetect;
1818 gen0 &= bank->saved_datain;
1819
1820 gen1 = l & bank->saved_risingdetect;
1821 gen1 &= ~(bank->saved_datain);
1822
1823 /* FIXME: Consider GPIO IRQs with level detections properly! */
1824 gen = l & (~(bank->saved_fallingdetect) &
1825 ~(bank->saved_risingdetect));
1826 /* Consider all GPIO IRQs needed to be updated */
1827 gen |= gen0 | gen1;
1828
1829 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001830 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001831
Sergio Aguirref00d6492010-03-03 16:21:08 +00001832 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001833 old0 = __raw_readl(bank->base +
1834 OMAP24XX_GPIO_LEVELDETECT0);
1835 old1 = __raw_readl(bank->base +
1836 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001837 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001838 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001839 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001840 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001841 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001842 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001843 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001844 OMAP24XX_GPIO_LEVELDETECT1);
1845 }
1846
1847 if (cpu_is_omap44xx()) {
1848 old0 = __raw_readl(bank->base +
1849 OMAP4_GPIO_LEVELDETECT0);
1850 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301851 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001852 __raw_writel(old0 | l, bank->base +
1853 OMAP4_GPIO_LEVELDETECT0);
1854 __raw_writel(old1 | l, bank->base +
1855 OMAP4_GPIO_LEVELDETECT1);
1856 __raw_writel(old0, bank->base +
1857 OMAP4_GPIO_LEVELDETECT0);
1858 __raw_writel(old1, bank->base +
1859 OMAP4_GPIO_LEVELDETECT1);
1860 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001861 }
1862 }
1863
1864}
1865
Tony Lindgren92105bb2005-09-07 17:20:26 +01001866#endif
1867
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001868#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301869/* save the registers of bank 2-6 */
1870void omap_gpio_save_context(void)
1871{
1872 int i;
1873
1874 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1875 for (i = 1; i < gpio_bank_count; i++) {
1876 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301877 gpio_context[i].irqenable1 =
1878 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1879 gpio_context[i].irqenable2 =
1880 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1881 gpio_context[i].wake_en =
1882 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1883 gpio_context[i].ctrl =
1884 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1885 gpio_context[i].oe =
1886 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1887 gpio_context[i].leveldetect0 =
1888 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1889 gpio_context[i].leveldetect1 =
1890 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1891 gpio_context[i].risingdetect =
1892 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1893 gpio_context[i].fallingdetect =
1894 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1895 gpio_context[i].dataout =
1896 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301897 }
1898}
1899
1900/* restore the required registers of bank 2-6 */
1901void omap_gpio_restore_context(void)
1902{
1903 int i;
1904
1905 for (i = 1; i < gpio_bank_count; i++) {
1906 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301907 __raw_writel(gpio_context[i].irqenable1,
1908 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1909 __raw_writel(gpio_context[i].irqenable2,
1910 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1911 __raw_writel(gpio_context[i].wake_en,
1912 bank->base + OMAP24XX_GPIO_WAKE_EN);
1913 __raw_writel(gpio_context[i].ctrl,
1914 bank->base + OMAP24XX_GPIO_CTRL);
1915 __raw_writel(gpio_context[i].oe,
1916 bank->base + OMAP24XX_GPIO_OE);
1917 __raw_writel(gpio_context[i].leveldetect0,
1918 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1919 __raw_writel(gpio_context[i].leveldetect1,
1920 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1921 __raw_writel(gpio_context[i].risingdetect,
1922 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1923 __raw_writel(gpio_context[i].fallingdetect,
1924 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1925 __raw_writel(gpio_context[i].dataout,
1926 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301927 }
1928}
1929#endif
1930
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001931static struct platform_driver omap_gpio_driver = {
1932 .probe = omap_gpio_probe,
1933 .driver = {
1934 .name = "omap_gpio",
1935 },
1936};
1937
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001938/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001939 * gpio driver register needs to be done before
1940 * machine_init functions access gpio APIs.
1941 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001942 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001943static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001944{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001945 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001946}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001947postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001948
Tony Lindgren92105bb2005-09-07 17:20:26 +01001949static int __init omap_gpio_sysinit(void)
1950{
David Brownell11a78b72006-12-06 17:14:11 -08001951 mpuio_init();
1952
Tony Lindgren140455f2010-02-12 12:26:48 -08001953#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001954 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1955 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001956#endif
1957
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001958 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001959}
1960
Tony Lindgren92105bb2005-09-07 17:20:26 +01001961arch_initcall(omap_gpio_sysinit);