blob: 07efa15c35492237a3a9c99dd28bbb3d49d01a7a [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>
Russell King1bc857f2011-07-26 10:54:55 +010028#include <asm/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029#include <asm/mach/irq.h>
30
Charulatha V03e128c2011-05-05 19:58:01 +053031static LIST_HEAD(omap_gpio_list);
32
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010033struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053034 struct list_head node;
Tony Lindgren9f7065d2009-10-19 15:25:20 -070035 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010036 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010037 u16 irq;
38 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010039 int method;
Tony Lindgren92105bb2005-09-07 17:20:26 +010040 u32 suspend_wakeup;
Janusz Krzysztofik78a43152011-08-23 13:42:24 +020041#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +010042 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080043#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080044 u32 non_wakeup_gpios;
45 u32 enabled_non_wakeup_gpios;
46
47 u32 saved_datain;
48 u32 saved_fallingdetect;
49 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080050 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080051 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010052 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080053 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080054 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080055 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080056 u32 dbck_enable_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080057 struct device *dev;
58 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053059 bool loses_context;
Tony Lindgren5de62b82010-12-07 16:26:58 -080060 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070061 u32 width;
Charulatha V03e128c2011-05-05 19:58:01 +053062 u16 id;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070063
64 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
65
66 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010067};
68
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -080069#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +053070struct omap3_gpio_regs {
Rajendra Nayak40c670f2008-09-26 17:47:48 +053071 u32 irqenable1;
72 u32 irqenable2;
73 u32 wake_en;
74 u32 ctrl;
75 u32 oe;
76 u32 leveldetect0;
77 u32 leveldetect1;
78 u32 risingdetect;
79 u32 fallingdetect;
80 u32 dataout;
Rajendra Nayak40c670f2008-09-26 17:47:48 +053081};
82
83static struct omap3_gpio_regs gpio_context[OMAP34XX_NR_GPIOS];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -080084#endif
85
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))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010088
89static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
90{
Tony Lindgren92105bb2005-09-07 17:20:26 +010091 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010092 u32 l;
93
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070094 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010095 l = __raw_readl(reg);
96 if (is_input)
97 l |= 1 << gpio;
98 else
99 l &= ~(1 << gpio);
100 __raw_writel(l, reg);
101}
102
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700103
104/* set data out value using dedicate set/clear register */
105static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100106{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100107 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700108 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100109
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700110 if (enable)
111 reg += bank->regs->set_dataout;
112 else
113 reg += bank->regs->clr_dataout;
114
115 __raw_writel(l, reg);
116}
117
118/* set data out value using mask register */
119static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
120{
121 void __iomem *reg = bank->base + bank->regs->dataout;
122 u32 gpio_bit = GPIO_BIT(bank, gpio);
123 u32 l;
124
125 l = __raw_readl(reg);
126 if (enable)
127 l |= gpio_bit;
128 else
129 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130 __raw_writel(l, reg);
131}
132
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300133static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700135 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700137 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100138}
139
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300140static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
141{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700142 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300143
Kevin Hilman129fd222011-04-22 07:59:07 -0700144 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300145}
146
Kevin Hilmanece95282011-07-12 08:18:15 -0700147static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
148{
149 int l = __raw_readl(base + reg);
150
151 if (set)
152 l |= mask;
153 else
154 l &= ~mask;
155
156 __raw_writel(l, base + reg);
157}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100158
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700159/**
160 * _set_gpio_debounce - low level gpio debounce time
161 * @bank: the gpio bank we're acting upon
162 * @gpio: the gpio number on this @gpio
163 * @debounce: debounce time to use
164 *
165 * OMAP's debounce time is in 31us steps so we need
166 * to convert and round up to the closest unit.
167 */
168static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
169 unsigned debounce)
170{
Kevin Hilman9942da02011-04-22 12:02:05 -0700171 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700172 u32 val;
173 u32 l;
174
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800175 if (!bank->dbck_flag)
176 return;
177
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700178 if (debounce < 32)
179 debounce = 0x01;
180 else if (debounce > 7936)
181 debounce = 0xff;
182 else
183 debounce = (debounce / 0x1f) - 1;
184
Kevin Hilman129fd222011-04-22 07:59:07 -0700185 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700186
Kevin Hilman9942da02011-04-22 12:02:05 -0700187 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700188 __raw_writel(debounce, reg);
189
Kevin Hilman9942da02011-04-22 12:02:05 -0700190 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700191 val = __raw_readl(reg);
192
193 if (debounce) {
194 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800195 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700196 } else {
197 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800198 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700199 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300200 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700201
202 __raw_writel(val, reg);
203}
204
Tony Lindgren140455f2010-02-12 12:26:48 -0800205#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700206static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
207 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100208{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800209 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100210 u32 gpio_bit = 1 << gpio;
211
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530212 if (cpu_is_omap44xx()) {
Kevin Hilmanece95282011-07-12 08:18:15 -0700213 _gpio_rmw(base, OMAP4_GPIO_LEVELDETECT0, gpio_bit,
214 trigger & IRQ_TYPE_LEVEL_LOW);
215 _gpio_rmw(base, OMAP4_GPIO_LEVELDETECT1, gpio_bit,
216 trigger & IRQ_TYPE_LEVEL_HIGH);
217 _gpio_rmw(base, OMAP4_GPIO_RISINGDETECT, gpio_bit,
218 trigger & IRQ_TYPE_EDGE_RISING);
219 _gpio_rmw(base, OMAP4_GPIO_FALLINGDETECT, gpio_bit,
220 trigger & IRQ_TYPE_EDGE_FALLING);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530221 } else {
Kevin Hilmanece95282011-07-12 08:18:15 -0700222 _gpio_rmw(base, OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
223 trigger & IRQ_TYPE_LEVEL_LOW);
224 _gpio_rmw(base, OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
225 trigger & IRQ_TYPE_LEVEL_HIGH);
226 _gpio_rmw(base, OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
227 trigger & IRQ_TYPE_EDGE_RISING);
228 _gpio_rmw(base, OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
229 trigger & IRQ_TYPE_EDGE_FALLING);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530230 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800231 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530232 if (cpu_is_omap44xx()) {
Kevin Hilmanece95282011-07-12 08:18:15 -0700233 _gpio_rmw(base, OMAP4_GPIO_IRQWAKEN0, gpio_bit,
234 trigger != 0);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530235 } else {
Chunqiu Wang699117a2009-06-24 17:13:39 +0000236 /*
237 * GPIO wakeup request can only be generated on edge
238 * transitions
239 */
240 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530241 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700242 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530243 else
244 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700245 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530246 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200247 }
Ambresh K55b220c2011-06-15 13:40:45 -0700248 /* This part needs to be executed always for OMAP{34xx, 44xx} */
249 if (cpu_is_omap34xx() || cpu_is_omap44xx() ||
250 (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a2009-06-24 17:13:39 +0000251 /*
252 * Log the edge gpio and manually trigger the IRQ
253 * after resume if the input level changes
254 * to avoid irq lost during PER RET/OFF mode
255 * Applies for omap2 non-wakeup gpio and all omap3 gpios
256 */
257 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800258 bank->enabled_non_wakeup_gpios |= gpio_bit;
259 else
260 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
261 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700262
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530263 if (cpu_is_omap44xx()) {
264 bank->level_mask =
265 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
266 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
267 } else {
268 bank->level_mask =
269 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
270 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
271 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100272}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800273#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100274
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800275#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800276/*
277 * This only applies to chips that can't do both rising and falling edge
278 * detection at once. For all other chips, this function is a noop.
279 */
280static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
281{
282 void __iomem *reg = bank->base;
283 u32 l = 0;
284
285 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800286 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800287 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800288 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800289#ifdef CONFIG_ARCH_OMAP15XX
290 case METHOD_GPIO_1510:
291 reg += OMAP1510_GPIO_INT_CONTROL;
292 break;
293#endif
294#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
295 case METHOD_GPIO_7XX:
296 reg += OMAP7XX_GPIO_INT_CONTROL;
297 break;
298#endif
299 default:
300 return;
301 }
302
303 l = __raw_readl(reg);
304 if ((l >> gpio) & 1)
305 l &= ~(1 << gpio);
306 else
307 l |= 1 << gpio;
308
309 __raw_writel(l, reg);
310}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800311#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800312
Tony Lindgren92105bb2005-09-07 17:20:26 +0100313static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
314{
315 void __iomem *reg = bank->base;
316 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100317
318 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800319#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100320 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800321 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100322 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000323 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800324 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100325 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100326 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100327 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100328 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100329 else
330 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100331 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800332#endif
333#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100334 case METHOD_GPIO_1510:
335 reg += OMAP1510_GPIO_INT_CONTROL;
336 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000337 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800338 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100339 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100341 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100342 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100343 else
344 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100345 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800346#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800347#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100349 if (gpio & 0x08)
350 reg += OMAP1610_GPIO_EDGE_CTRL2;
351 else
352 reg += OMAP1610_GPIO_EDGE_CTRL1;
353 gpio &= 0x07;
354 l = __raw_readl(reg);
355 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100356 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100357 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100358 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100359 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800360 if (trigger)
361 /* Enable wake-up during idle for dynamic tick */
362 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
363 else
364 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100365 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800366#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100367#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100368 case METHOD_GPIO_7XX:
369 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700370 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000371 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800372 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700373 if (trigger & IRQ_TYPE_EDGE_RISING)
374 l |= 1 << gpio;
375 else if (trigger & IRQ_TYPE_EDGE_FALLING)
376 l &= ~(1 << gpio);
377 else
378 goto bad;
379 break;
380#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800381#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100382 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800383 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800384 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200385 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800386#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100387 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100388 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100389 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100390 __raw_writel(l, reg);
391 return 0;
392bad:
393 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100394}
395
Lennert Buytenheke9191022010-11-29 11:17:17 +0100396static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100397{
398 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100399 unsigned gpio;
400 int retval;
David Brownella6472532008-03-03 04:33:30 -0800401 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100402
Lennert Buytenheke9191022010-11-29 11:17:17 +0100403 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
404 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100405 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100406 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100407
David Brownelle5c56ed2006-12-06 17:13:59 -0800408 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100409 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800410
411 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800412 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800413 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100414 return -EINVAL;
415
Lennert Buytenheke9191022010-11-29 11:17:17 +0100416 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800417 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700418 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800419 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800420
421 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100422 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800423 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100424 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800425
Tony Lindgren92105bb2005-09-07 17:20:26 +0100426 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100427}
428
429static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
430{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100431 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100432
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700433 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100434 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300435
436 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700437 if (bank->regs->irqstatus2) {
438 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700439 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700440 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700441
442 /* Flush posted write for the irq status to avoid spurious interrupts */
443 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100444}
445
446static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
447{
Kevin Hilman129fd222011-04-22 07:59:07 -0700448 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100449}
450
Imre Deakea6dedd2006-06-26 16:16:00 -0700451static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
452{
453 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700454 u32 l;
Kevin Hilmanc390aad2011-04-21 09:33:36 -0700455 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700456
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700457 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700458 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700459 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700460 l = ~l;
461 l &= mask;
462 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700463}
464
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700465static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100466{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100467 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100468 u32 l;
469
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700470 if (bank->regs->set_irqenable) {
471 reg += bank->regs->set_irqenable;
472 l = gpio_mask;
473 } else {
474 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700476 if (bank->regs->irqenable_inv)
477 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100478 else
479 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100480 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700481
482 __raw_writel(l, reg);
483}
484
485static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
486{
487 void __iomem *reg = bank->base;
488 u32 l;
489
490 if (bank->regs->clr_irqenable) {
491 reg += bank->regs->clr_irqenable;
492 l = gpio_mask;
493 } else {
494 reg += bank->regs->irqenable;
495 l = __raw_readl(reg);
496 if (bank->regs->irqenable_inv)
497 l |= gpio_mask;
498 else
499 l &= ~gpio_mask;
500 }
501
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100502 __raw_writel(l, reg);
503}
504
505static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
506{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700507 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100508}
509
Tony Lindgren92105bb2005-09-07 17:20:26 +0100510/*
511 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
512 * 1510 does not seem to have a wake-up register. If JTAG is connected
513 * to the target, system will wake up always on GPIO events. While
514 * system is running all registered GPIO interrupts need to have wake-up
515 * enabled. When system is suspended, only selected GPIO interrupts need
516 * to have wake-up enabled.
517 */
518static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
519{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700520 u32 gpio_bit = GPIO_BIT(bank, gpio);
521 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800522
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700523 if (bank->non_wakeup_gpios & gpio_bit) {
524 dev_err(bank->dev,
525 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100526 return -EINVAL;
527 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700528
529 spin_lock_irqsave(&bank->lock, flags);
530 if (enable)
531 bank->suspend_wakeup |= gpio_bit;
532 else
533 bank->suspend_wakeup &= ~gpio_bit;
534
535 spin_unlock_irqrestore(&bank->lock, flags);
536
537 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100538}
539
Tony Lindgren4196dd62006-09-25 12:41:38 +0300540static void _reset_gpio(struct gpio_bank *bank, int gpio)
541{
Kevin Hilman129fd222011-04-22 07:59:07 -0700542 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300543 _set_gpio_irqenable(bank, gpio, 0);
544 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700545 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300546}
547
Tony Lindgren92105bb2005-09-07 17:20:26 +0100548/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100549static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100550{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100551 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100552 struct gpio_bank *bank;
553 int retval;
554
Lennert Buytenheke9191022010-11-29 11:17:17 +0100555 bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700556 retval = _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100557
558 return retval;
559}
560
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800561static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100562{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800563 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800564 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100565
David Brownella6472532008-03-03 04:33:30 -0800566 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100567
Tony Lindgren4196dd62006-09-25 12:41:38 +0300568 /* Set trigger to none. You need to enable the desired trigger with
569 * request_irq() or set_irq_type().
570 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800571 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100572
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000573#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100574 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100575 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576
Tony Lindgren92105bb2005-09-07 17:20:26 +0100577 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800579 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100580 }
581#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800582 if (!cpu_class_is_omap1()) {
583 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700584 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800585 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700586
587 if (cpu_is_omap24xx() || cpu_is_omap34xx())
588 reg += OMAP24XX_GPIO_CTRL;
589 else if (cpu_is_omap44xx())
590 reg += OMAP4_GPIO_CTRL;
591 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800592 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700593 ctrl &= 0xFFFFFFFE;
594 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800595 }
596 bank->mod_usage |= 1 << offset;
597 }
David Brownella6472532008-03-03 04:33:30 -0800598 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100599
600 return 0;
601}
602
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800603static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100604{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800605 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800606 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100607
David Brownella6472532008-03-03 04:33:30 -0800608 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100609#ifdef CONFIG_ARCH_OMAP16XX
610 if (bank->method == METHOD_GPIO_1610) {
611 /* Disable wake-up during idle for dynamic tick */
612 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800613 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100614 }
615#endif
Charulatha V9f096862010-05-14 12:05:27 -0700616#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
617 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100618 /* Disable wake-up during idle for dynamic tick */
619 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800620 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100621 }
622#endif
Charulatha V9f096862010-05-14 12:05:27 -0700623#ifdef CONFIG_ARCH_OMAP4
624 if (bank->method == METHOD_GPIO_44XX) {
625 /* Disable wake-up during idle for dynamic tick */
626 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
627 __raw_writel(1 << offset, reg);
628 }
629#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800630 if (!cpu_class_is_omap1()) {
631 bank->mod_usage &= ~(1 << offset);
632 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700633 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800634 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700635
636 if (cpu_is_omap24xx() || cpu_is_omap34xx())
637 reg += OMAP24XX_GPIO_CTRL;
638 else if (cpu_is_omap44xx())
639 reg += OMAP4_GPIO_CTRL;
640 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800641 /* Module is disabled, clocks are gated */
642 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700643 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800644 }
645 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800646 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800647 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100648}
649
650/*
651 * We need to unmask the GPIO bank interrupt as soon as possible to
652 * avoid missing GPIO interrupts for other lines in the bank.
653 * Then we need to mask-read-clear-unmask the triggered GPIO lines
654 * in the bank to avoid missing nested interrupts for a GPIO line.
655 * If we wait to unmask individual GPIO lines in the bank after the
656 * line's interrupt handler has been run, we may miss some nested
657 * interrupts.
658 */
Russell King10dd5ce2006-11-23 11:41:32 +0000659static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100660{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100661 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100662 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800663 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100664 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700665 u32 retrigger = 0;
666 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000667 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100668
Will Deaconee144182011-02-21 13:46:08 +0000669 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100671 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700672 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800673
674 if (WARN_ON(!isr_reg))
675 goto exit;
676
Tony Lindgren92105bb2005-09-07 17:20:26 +0100677 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100678 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700679 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100680
Imre Deakea6dedd2006-06-26 16:16:00 -0700681 enabled = _get_gpio_irqbank_mask(bank);
682 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100683
684 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
685 isr &= 0x0000ffff;
686
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800687 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800688 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -0700689 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100690
691 /* clear edge sensitive interrupts before handler(s) are
692 called so that we don't miss any interrupt occurred while
693 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700694 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100695 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700696 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100697
698 /* if there is only edge sensitive GPIO pin interrupts
699 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700700 if (!level_mask && !unmasked) {
701 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000702 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700703 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100704
Imre Deakea6dedd2006-06-26 16:16:00 -0700705 isr |= retrigger;
706 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100707 if (!isr)
708 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100709
Tony Lindgren92105bb2005-09-07 17:20:26 +0100710 gpio_irq = bank->virtual_irq_start;
711 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700712 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800713
Tony Lindgren92105bb2005-09-07 17:20:26 +0100714 if (!(isr & 1))
715 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200716
Cory Maccarrone4318f362010-01-08 10:29:04 -0800717#ifdef CONFIG_ARCH_OMAP1
718 /*
719 * Some chips can't respond to both rising and falling
720 * at the same time. If this irq was requested with
721 * both flags, we need to flip the ICR data for the IRQ
722 * to respond to the IRQ for the opposite direction.
723 * This will be indicated in the bank toggle_mask.
724 */
725 if (bank->toggle_mask & (1 << gpio_index))
726 _toggle_gpio_edge_triggering(bank, gpio_index);
727#endif
728
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100729 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100730 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000731 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700732 /* if bank has any level sensitive GPIO pin interrupt
733 configured, we must unmask the bank interrupt only after
734 handler(s) are executed in order to avoid spurious bank
735 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800736exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700737 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000738 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100739}
740
Lennert Buytenheke9191022010-11-29 11:17:17 +0100741static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300742{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100743 unsigned int gpio = d->irq - IH_GPIO_BASE;
744 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700745 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300746
Colin Cross85ec7b92011-06-06 13:38:18 -0700747 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300748 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700749 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300750}
751
Lennert Buytenheke9191022010-11-29 11:17:17 +0100752static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100753{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100754 unsigned int gpio = d->irq - IH_GPIO_BASE;
755 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756
757 _clear_gpio_irqstatus(bank, gpio);
758}
759
Lennert Buytenheke9191022010-11-29 11:17:17 +0100760static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100761{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100762 unsigned int gpio = d->irq - IH_GPIO_BASE;
763 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700764 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100765
Colin Cross85ec7b92011-06-06 13:38:18 -0700766 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100767 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700768 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700769 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100770}
771
Lennert Buytenheke9191022010-11-29 11:17:17 +0100772static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100773{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100774 unsigned int gpio = d->irq - IH_GPIO_BASE;
775 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700776 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100777 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700778 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700779
Colin Cross85ec7b92011-06-06 13:38:18 -0700780 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700781 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700782 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800783
784 /* For level-triggered GPIOs, the clearing must be done after
785 * the HW source is cleared, thus after the handler has run */
786 if (bank->level_mask & irq_mask) {
787 _set_gpio_irqenable(bank, gpio, 0);
788 _clear_gpio_irqstatus(bank, gpio);
789 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100790
Kevin Hilman4de8c752008-01-16 21:56:14 -0800791 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700792 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100793}
794
David Brownelle5c56ed2006-12-06 17:13:59 -0800795static struct irq_chip gpio_irq_chip = {
796 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100797 .irq_shutdown = gpio_irq_shutdown,
798 .irq_ack = gpio_ack_irq,
799 .irq_mask = gpio_mask_irq,
800 .irq_unmask = gpio_unmask_irq,
801 .irq_set_type = gpio_irq_type,
802 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800803};
804
805/*---------------------------------------------------------------------*/
806
807#ifdef CONFIG_ARCH_OMAP1
808
David Brownelle5c56ed2006-12-06 17:13:59 -0800809#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
810
David Brownell11a78b72006-12-06 17:14:11 -0800811#ifdef CONFIG_ARCH_OMAP16XX
812
813#include <linux/platform_device.h>
814
Magnus Damm79ee0312009-07-08 13:22:04 +0200815static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800816{
Magnus Damm79ee0312009-07-08 13:22:04 +0200817 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800818 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800819 void __iomem *mask_reg = bank->base +
820 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800821 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800822
David Brownella6472532008-03-03 04:33:30 -0800823 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800824 bank->saved_wakeup = __raw_readl(mask_reg);
825 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800826 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800827
828 return 0;
829}
830
Magnus Damm79ee0312009-07-08 13:22:04 +0200831static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800832{
Magnus Damm79ee0312009-07-08 13:22:04 +0200833 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800834 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800835 void __iomem *mask_reg = bank->base +
836 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800837 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800838
David Brownella6472532008-03-03 04:33:30 -0800839 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800840 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800841 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800842
843 return 0;
844}
845
Alexey Dobriyan47145212009-12-14 18:00:08 -0800846static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200847 .suspend_noirq = omap_mpuio_suspend_noirq,
848 .resume_noirq = omap_mpuio_resume_noirq,
849};
850
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200851/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800852static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800853 .driver = {
854 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200855 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800856 },
857};
858
859static struct platform_device omap_mpuio_device = {
860 .name = "mpuio",
861 .id = -1,
862 .dev = {
863 .driver = &omap_mpuio_driver.driver,
864 }
865 /* could list the /proc/iomem resources */
866};
867
Charulatha V03e128c2011-05-05 19:58:01 +0530868static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800869{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800870 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700871
David Brownell11a78b72006-12-06 17:14:11 -0800872 if (platform_driver_register(&omap_mpuio_driver) == 0)
873 (void) platform_device_register(&omap_mpuio_device);
874}
875
876#else
Charulatha V03e128c2011-05-05 19:58:01 +0530877static inline void mpuio_init(struct gpio_bank *bank) {}
David Brownell11a78b72006-12-06 17:14:11 -0800878#endif /* 16xx */
879
David Brownelle5c56ed2006-12-06 17:13:59 -0800880#else
881
David Brownelle5c56ed2006-12-06 17:13:59 -0800882#define bank_is_mpuio(bank) 0
Charulatha V03e128c2011-05-05 19:58:01 +0530883static inline void mpuio_init(struct gpio_bank *bank) {}
David Brownelle5c56ed2006-12-06 17:13:59 -0800884
885#endif
886
887/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100888
David Brownell52e31342008-03-03 12:43:23 -0800889/* REVISIT these are stupid implementations! replace by ones that
890 * don't switch on METHOD_* and which mostly avoid spinlocks
891 */
892
893static int gpio_input(struct gpio_chip *chip, unsigned offset)
894{
895 struct gpio_bank *bank;
896 unsigned long flags;
897
898 bank = container_of(chip, struct gpio_bank, chip);
899 spin_lock_irqsave(&bank->lock, flags);
900 _set_gpio_direction(bank, offset, 1);
901 spin_unlock_irqrestore(&bank->lock, flags);
902 return 0;
903}
904
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300905static int gpio_is_input(struct gpio_bank *bank, int mask)
906{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700907 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300908
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300909 return __raw_readl(reg) & mask;
910}
911
David Brownell52e31342008-03-03 12:43:23 -0800912static int gpio_get(struct gpio_chip *chip, unsigned offset)
913{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300914 struct gpio_bank *bank;
915 void __iomem *reg;
916 int gpio;
917 u32 mask;
918
919 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530920 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300921 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700922 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300923
924 if (gpio_is_input(bank, mask))
925 return _get_gpio_datain(bank, gpio);
926 else
927 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800928}
929
930static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
931{
932 struct gpio_bank *bank;
933 unsigned long flags;
934
935 bank = container_of(chip, struct gpio_bank, chip);
936 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700937 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800938 _set_gpio_direction(bank, offset, 0);
939 spin_unlock_irqrestore(&bank->lock, flags);
940 return 0;
941}
942
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700943static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
944 unsigned debounce)
945{
946 struct gpio_bank *bank;
947 unsigned long flags;
948
949 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800950
951 if (!bank->dbck) {
952 bank->dbck = clk_get(bank->dev, "dbclk");
953 if (IS_ERR(bank->dbck))
954 dev_err(bank->dev, "Could not get gpio dbck\n");
955 }
956
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700957 spin_lock_irqsave(&bank->lock, flags);
958 _set_gpio_debounce(bank, offset, debounce);
959 spin_unlock_irqrestore(&bank->lock, flags);
960
961 return 0;
962}
963
David Brownell52e31342008-03-03 12:43:23 -0800964static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
965{
966 struct gpio_bank *bank;
967 unsigned long flags;
968
969 bank = container_of(chip, struct gpio_bank, chip);
970 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700971 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800972 spin_unlock_irqrestore(&bank->lock, flags);
973}
974
David Brownella007b702008-12-10 17:35:25 -0800975static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
976{
977 struct gpio_bank *bank;
978
979 bank = container_of(chip, struct gpio_bank, chip);
980 return bank->virtual_irq_start + offset;
981}
982
David Brownell52e31342008-03-03 12:43:23 -0800983/*---------------------------------------------------------------------*/
984
Tony Lindgren9a748052010-12-07 16:26:56 -0800985static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700986{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700987 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700988 u32 rev;
989
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700990 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700991 return;
992
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700993 rev = __raw_readw(bank->base + bank->regs->revision);
994 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700995 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700996
997 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700998}
999
David Brownell8ba55c52008-02-26 11:10:50 -08001000/* This lock class tells lockdep that GPIO irqs are in a different
1001 * category than their parents, so it won't report false recursion.
1002 */
1003static struct lock_class_key gpio_lock_class;
1004
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001005/* TODO: Cleanup cpu_is_* checks */
Charulatha V03e128c2011-05-05 19:58:01 +05301006static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001007{
1008 if (cpu_class_is_omap2()) {
1009 if (cpu_is_omap44xx()) {
1010 __raw_writel(0xffffffff, bank->base +
1011 OMAP4_GPIO_IRQSTATUSCLR0);
1012 __raw_writel(0x00000000, bank->base +
1013 OMAP4_GPIO_DEBOUNCENABLE);
1014 /* Initialize interface clk ungated, module enabled */
1015 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1016 } else if (cpu_is_omap34xx()) {
1017 __raw_writel(0x00000000, bank->base +
1018 OMAP24XX_GPIO_IRQENABLE1);
1019 __raw_writel(0xffffffff, bank->base +
1020 OMAP24XX_GPIO_IRQSTATUS1);
1021 __raw_writel(0x00000000, bank->base +
1022 OMAP24XX_GPIO_DEBOUNCE_EN);
1023
1024 /* Initialize interface clk ungated, module enabled */
1025 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1026 } else if (cpu_is_omap24xx()) {
1027 static const u32 non_wakeup_gpios[] = {
1028 0xe203ffc0, 0x08700040
1029 };
Charulatha V03e128c2011-05-05 19:58:01 +05301030 if (bank->id < ARRAY_SIZE(non_wakeup_gpios))
1031 bank->non_wakeup_gpios =
1032 non_wakeup_gpios[bank->id];
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001033 }
1034 } else if (cpu_class_is_omap1()) {
Charulatha V03e128c2011-05-05 19:58:01 +05301035 if (bank_is_mpuio(bank)) {
Tony Lindgren5de62b82010-12-07 16:26:58 -08001036 __raw_writew(0xffff, bank->base +
1037 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Charulatha V03e128c2011-05-05 19:58:01 +05301038 mpuio_init(bank);
1039 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001040 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1041 __raw_writew(0xffff, bank->base
1042 + OMAP1510_GPIO_INT_MASK);
1043 __raw_writew(0x0000, bank->base
1044 + OMAP1510_GPIO_INT_STATUS);
1045 }
1046 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1047 __raw_writew(0x0000, bank->base
1048 + OMAP1610_GPIO_IRQENABLE1);
1049 __raw_writew(0xffff, bank->base
1050 + OMAP1610_GPIO_IRQSTATUS1);
1051 __raw_writew(0x0014, bank->base
1052 + OMAP1610_GPIO_SYSCONFIG);
1053
1054 /*
1055 * Enable system clock for GPIO module.
1056 * The CAM_CLK_CTRL *is* really the right place.
1057 */
1058 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1059 ULPD_CAM_CLK_CTRL);
1060 }
1061 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1062 __raw_writel(0xffffffff, bank->base
1063 + OMAP7XX_GPIO_INT_MASK);
1064 __raw_writel(0x00000000, bank->base
1065 + OMAP7XX_GPIO_INT_STATUS);
1066 }
1067 }
1068}
1069
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001070static __init void
1071omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1072 unsigned int num)
1073{
1074 struct irq_chip_generic *gc;
1075 struct irq_chip_type *ct;
1076
1077 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1078 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -07001079 if (!gc) {
1080 dev_err(bank->dev, "Memory alloc failed for gc\n");
1081 return;
1082 }
1083
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001084 ct = gc->chip_types;
1085
1086 /* NOTE: No ack required, reading IRQ status clears it. */
1087 ct->chip.irq_mask = irq_gc_mask_set_bit;
1088 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1089 ct->chip.irq_set_type = gpio_irq_type;
1090 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1091 if (cpu_is_omap16xx())
1092 ct->chip.irq_set_wake = gpio_wake_enable,
1093
1094 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1095 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1096 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1097}
1098
Russell Kingd52b31d2011-05-27 13:56:12 -07001099static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001100{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001101 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001102 static int gpio;
1103
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001104 bank->mod_usage = 0;
1105 /*
1106 * REVISIT eventually switch from OMAP-specific gpio structs
1107 * over to the generic ones
1108 */
1109 bank->chip.request = omap_gpio_request;
1110 bank->chip.free = omap_gpio_free;
1111 bank->chip.direction_input = gpio_input;
1112 bank->chip.get = gpio_get;
1113 bank->chip.direction_output = gpio_output;
1114 bank->chip.set_debounce = gpio_debounce;
1115 bank->chip.set = gpio_set;
1116 bank->chip.to_irq = gpio_2irq;
1117 if (bank_is_mpuio(bank)) {
1118 bank->chip.label = "mpuio";
1119#ifdef CONFIG_ARCH_OMAP16XX
1120 bank->chip.dev = &omap_mpuio_device.dev;
1121#endif
1122 bank->chip.base = OMAP_MPUIO(0);
1123 } else {
1124 bank->chip.label = "gpio";
1125 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001126 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001127 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001128 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001129
1130 gpiochip_add(&bank->chip);
1131
1132 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001133 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001134 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001135 irq_set_chip_data(j, bank);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001136 if (bank_is_mpuio(bank)) {
1137 omap_mpuio_alloc_gc(bank, j, bank->width);
1138 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001139 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001140 irq_set_handler(j, handle_simple_irq);
1141 set_irq_flags(j, IRQF_VALID);
1142 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001143 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001144 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1145 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001146}
1147
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001148static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001149{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001150 struct omap_gpio_platform_data *pdata;
1151 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001152 struct gpio_bank *bank;
Charulatha V03e128c2011-05-05 19:58:01 +05301153 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001154
Charulatha V03e128c2011-05-05 19:58:01 +05301155 if (!pdev->dev.platform_data) {
1156 ret = -EINVAL;
1157 goto err_exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001158 }
1159
Charulatha V03e128c2011-05-05 19:58:01 +05301160 bank = kzalloc(sizeof(struct gpio_bank), GFP_KERNEL);
1161 if (!bank) {
1162 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1163 ret = -ENOMEM;
1164 goto err_exit;
1165 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001166
1167 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1168 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301169 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n",
1170 pdev->id);
1171 ret = -ENODEV;
1172 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001173 }
1174
1175 bank->irq = res->start;
Charulatha V03e128c2011-05-05 19:58:01 +05301176 bank->id = pdev->id;
1177
1178 pdata = pdev->dev.platform_data;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001179 bank->virtual_irq_start = pdata->virtual_irq_start;
1180 bank->method = pdata->bank_type;
1181 bank->dev = &pdev->dev;
1182 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001183 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001184 bank->width = pdata->bank_width;
Charulatha V0cde8d02011-05-05 20:15:16 +05301185 bank->loses_context = pdata->loses_context;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001186 bank->regs = pdata->regs;
1187
1188 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1189 bank->set_dataout = _set_gpio_dataout_reg;
1190 else
1191 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001192
1193 spin_lock_init(&bank->lock);
1194
1195 /* Static mapping, never released */
1196 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1197 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301198 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n",
1199 pdev->id);
1200 ret = -ENODEV;
1201 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001202 }
1203
1204 bank->base = ioremap(res->start, resource_size(res));
1205 if (!bank->base) {
Charulatha V03e128c2011-05-05 19:58:01 +05301206 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n",
1207 pdev->id);
1208 ret = -ENOMEM;
1209 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001210 }
1211
1212 pm_runtime_enable(bank->dev);
1213 pm_runtime_get_sync(bank->dev);
1214
Charulatha V03e128c2011-05-05 19:58:01 +05301215 omap_gpio_mod_init(bank);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001216 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001217 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001218
Charulatha V03e128c2011-05-05 19:58:01 +05301219 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001220
Charulatha V03e128c2011-05-05 19:58:01 +05301221 return ret;
1222
1223err_free:
1224 kfree(bank);
1225err_exit:
1226 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001227}
1228
Tony Lindgren140455f2010-02-12 12:26:48 -08001229#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001230static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001231{
Charulatha V03e128c2011-05-05 19:58:01 +05301232 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001233
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001234 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001235 return 0;
1236
Charulatha V03e128c2011-05-05 19:58:01 +05301237 list_for_each_entry(bank, &omap_gpio_list, node) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001238 void __iomem *wake_status;
1239 void __iomem *wake_clear;
1240 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001241 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001242
1243 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001244#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001245 case METHOD_GPIO_1610:
1246 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1247 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1248 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1249 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001250#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001251#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001252 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001253 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001254 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1255 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1256 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001257#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301258#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001259 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301260 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1261 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1262 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1263 break;
1264#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001265 default:
1266 continue;
1267 }
1268
David Brownella6472532008-03-03 04:33:30 -08001269 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001270 bank->saved_wakeup = __raw_readl(wake_status);
1271 __raw_writel(0xffffffff, wake_clear);
1272 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001273 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001274 }
1275
1276 return 0;
1277}
1278
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001279static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001280{
Charulatha V03e128c2011-05-05 19:58:01 +05301281 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001282
Tero Kristo723fdb72008-11-26 14:35:16 -08001283 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001284 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001285
Charulatha V03e128c2011-05-05 19:58:01 +05301286 list_for_each_entry(bank, &omap_gpio_list, node) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001287 void __iomem *wake_clear;
1288 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001289 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001290
1291 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001292#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001293 case METHOD_GPIO_1610:
1294 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1295 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1296 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001297#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001298#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001299 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001300 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1301 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001302 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001303#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301304#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001305 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301306 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1307 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1308 break;
1309#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001310 default:
1311 continue;
1312 }
1313
David Brownella6472532008-03-03 04:33:30 -08001314 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001315 __raw_writel(0xffffffff, wake_clear);
1316 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001317 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001318 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001319}
1320
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001321static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001322 .suspend = omap_gpio_suspend,
1323 .resume = omap_gpio_resume,
1324};
1325
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001326#endif
1327
Tony Lindgren140455f2010-02-12 12:26:48 -08001328#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001329
1330static int workaround_enabled;
1331
Paul Walmsley72e06d02010-12-21 21:05:16 -07001332void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001333{
Charulatha V03e128c2011-05-05 19:58:01 +05301334 int c = 0;
1335 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001336
Charulatha V03e128c2011-05-05 19:58:01 +05301337 list_for_each_entry(bank, &omap_gpio_list, node) {
Sanjeev Premica828762010-09-23 18:27:18 -07001338 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001339 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001340
Charulatha V0cde8d02011-05-05 20:15:16 +05301341 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301342 continue;
1343
Kevin Hilman0aed04352010-09-22 16:06:27 -07001344 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001345 clk_disable(bank->dbck);
1346
Paul Walmsley72e06d02010-12-21 21:05:16 -07001347 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001348 continue;
1349
1350 /* If going to OFF, remove triggering for all
1351 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1352 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001353 if (!(bank->enabled_non_wakeup_gpios))
1354 continue;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001355
1356 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1357 bank->saved_datain = __raw_readl(bank->base +
1358 OMAP24XX_GPIO_DATAIN);
1359 l1 = __raw_readl(bank->base +
1360 OMAP24XX_GPIO_FALLINGDETECT);
1361 l2 = __raw_readl(bank->base +
1362 OMAP24XX_GPIO_RISINGDETECT);
1363 }
1364
1365 if (cpu_is_omap44xx()) {
1366 bank->saved_datain = __raw_readl(bank->base +
1367 OMAP4_GPIO_DATAIN);
1368 l1 = __raw_readl(bank->base +
1369 OMAP4_GPIO_FALLINGDETECT);
1370 l2 = __raw_readl(bank->base +
1371 OMAP4_GPIO_RISINGDETECT);
1372 }
1373
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001374 bank->saved_fallingdetect = l1;
1375 bank->saved_risingdetect = l2;
1376 l1 &= ~bank->enabled_non_wakeup_gpios;
1377 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001378
1379 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1380 __raw_writel(l1, bank->base +
1381 OMAP24XX_GPIO_FALLINGDETECT);
1382 __raw_writel(l2, bank->base +
1383 OMAP24XX_GPIO_RISINGDETECT);
1384 }
1385
1386 if (cpu_is_omap44xx()) {
1387 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1388 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1389 }
1390
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001391 c++;
1392 }
1393 if (!c) {
1394 workaround_enabled = 0;
1395 return;
1396 }
1397 workaround_enabled = 1;
1398}
1399
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001400void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001401{
Charulatha V03e128c2011-05-05 19:58:01 +05301402 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001403
Charulatha V03e128c2011-05-05 19:58:01 +05301404 list_for_each_entry(bank, &omap_gpio_list, node) {
Sanjeev Premica828762010-09-23 18:27:18 -07001405 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001406 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001407
Charulatha V0cde8d02011-05-05 20:15:16 +05301408 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301409 continue;
1410
Kevin Hilman0aed04352010-09-22 16:06:27 -07001411 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001412 clk_enable(bank->dbck);
1413
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001414 if (!workaround_enabled)
1415 continue;
1416
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001417 if (!(bank->enabled_non_wakeup_gpios))
1418 continue;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001419
1420 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1421 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001422 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001423 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001424 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001425 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1426 }
1427
1428 if (cpu_is_omap44xx()) {
1429 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301430 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001431 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301432 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001433 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1434 }
1435
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001436 /* Check if any of the non-wakeup interrupt GPIOs have changed
1437 * state. If so, generate an IRQ by software. This is
1438 * horribly racy, but it's the best we can do to work around
1439 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001440 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001441 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001442
1443 /*
1444 * No need to generate IRQs for the rising edge for gpio IRQs
1445 * configured with falling edge only; and vice versa.
1446 */
1447 gen0 = l & bank->saved_fallingdetect;
1448 gen0 &= bank->saved_datain;
1449
1450 gen1 = l & bank->saved_risingdetect;
1451 gen1 &= ~(bank->saved_datain);
1452
1453 /* FIXME: Consider GPIO IRQs with level detections properly! */
1454 gen = l & (~(bank->saved_fallingdetect) &
1455 ~(bank->saved_risingdetect));
1456 /* Consider all GPIO IRQs needed to be updated */
1457 gen |= gen0 | gen1;
1458
1459 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001460 u32 old0, old1;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001461
Sergio Aguirref00d6492010-03-03 16:21:08 +00001462 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001463 old0 = __raw_readl(bank->base +
1464 OMAP24XX_GPIO_LEVELDETECT0);
1465 old1 = __raw_readl(bank->base +
1466 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001467 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001468 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001469 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001470 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001471 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001472 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001473 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001474 OMAP24XX_GPIO_LEVELDETECT1);
1475 }
1476
1477 if (cpu_is_omap44xx()) {
1478 old0 = __raw_readl(bank->base +
1479 OMAP4_GPIO_LEVELDETECT0);
1480 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301481 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001482 __raw_writel(old0 | l, bank->base +
1483 OMAP4_GPIO_LEVELDETECT0);
1484 __raw_writel(old1 | l, bank->base +
1485 OMAP4_GPIO_LEVELDETECT1);
1486 __raw_writel(old0, bank->base +
1487 OMAP4_GPIO_LEVELDETECT0);
1488 __raw_writel(old1, bank->base +
1489 OMAP4_GPIO_LEVELDETECT1);
1490 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001491 }
1492 }
1493
1494}
1495
Tony Lindgren92105bb2005-09-07 17:20:26 +01001496#endif
1497
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001498#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301499void omap_gpio_save_context(void)
1500{
Charulatha V03e128c2011-05-05 19:58:01 +05301501 struct gpio_bank *bank;
1502 int i = 0;
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301503
Charulatha V03e128c2011-05-05 19:58:01 +05301504 list_for_each_entry(bank, &omap_gpio_list, node) {
1505 i++;
1506
Charulatha V0cde8d02011-05-05 20:15:16 +05301507 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301508 continue;
1509
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301510 gpio_context[i].irqenable1 =
1511 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1512 gpio_context[i].irqenable2 =
1513 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1514 gpio_context[i].wake_en =
1515 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1516 gpio_context[i].ctrl =
1517 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1518 gpio_context[i].oe =
1519 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1520 gpio_context[i].leveldetect0 =
1521 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1522 gpio_context[i].leveldetect1 =
1523 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1524 gpio_context[i].risingdetect =
1525 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1526 gpio_context[i].fallingdetect =
1527 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1528 gpio_context[i].dataout =
1529 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301530 }
1531}
1532
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301533void omap_gpio_restore_context(void)
1534{
Charulatha V03e128c2011-05-05 19:58:01 +05301535 struct gpio_bank *bank;
1536 int i = 0;
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301537
Charulatha V03e128c2011-05-05 19:58:01 +05301538 list_for_each_entry(bank, &omap_gpio_list, node) {
1539 i++;
1540
Charulatha V0cde8d02011-05-05 20:15:16 +05301541 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301542 continue;
1543
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301544 __raw_writel(gpio_context[i].irqenable1,
1545 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1546 __raw_writel(gpio_context[i].irqenable2,
1547 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1548 __raw_writel(gpio_context[i].wake_en,
1549 bank->base + OMAP24XX_GPIO_WAKE_EN);
1550 __raw_writel(gpio_context[i].ctrl,
1551 bank->base + OMAP24XX_GPIO_CTRL);
1552 __raw_writel(gpio_context[i].oe,
1553 bank->base + OMAP24XX_GPIO_OE);
1554 __raw_writel(gpio_context[i].leveldetect0,
1555 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1556 __raw_writel(gpio_context[i].leveldetect1,
1557 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1558 __raw_writel(gpio_context[i].risingdetect,
1559 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1560 __raw_writel(gpio_context[i].fallingdetect,
1561 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1562 __raw_writel(gpio_context[i].dataout,
1563 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301564 }
1565}
1566#endif
1567
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001568static struct platform_driver omap_gpio_driver = {
1569 .probe = omap_gpio_probe,
1570 .driver = {
1571 .name = "omap_gpio",
1572 },
1573};
1574
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001575/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001576 * gpio driver register needs to be done before
1577 * machine_init functions access gpio APIs.
1578 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001579 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001580static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001581{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001582 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001583}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001584postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001585
Tony Lindgren92105bb2005-09-07 17:20:26 +01001586static int __init omap_gpio_sysinit(void)
1587{
David Brownell11a78b72006-12-06 17:14:11 -08001588
Tony Lindgren140455f2010-02-12 12:26:48 -08001589#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001590 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1591 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001592#endif
1593
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001594 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001595}
1596
Tony Lindgren92105bb2005-09-07 17:20:26 +01001597arch_initcall(omap_gpio_sysinit);