blob: 0b0562979171065bb3c454112c985cb74a633728 [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
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 Lindgren92105bb2005-09-07 17:20:26 +010037 u32 suspend_wakeup;
Janusz Krzysztofik78a43152011-08-23 13:42:24 +020038#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +010039 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;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070058
59 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
60
61 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010062};
63
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -080064#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +053065struct omap3_gpio_regs {
Rajendra Nayak40c670f2008-09-26 17:47:48 +053066 u32 irqenable1;
67 u32 irqenable2;
68 u32 wake_en;
69 u32 ctrl;
70 u32 oe;
71 u32 leveldetect0;
72 u32 leveldetect1;
73 u32 risingdetect;
74 u32 fallingdetect;
75 u32 dataout;
Rajendra Nayak40c670f2008-09-26 17:47:48 +053076};
77
78static struct omap3_gpio_regs gpio_context[OMAP34XX_NR_GPIOS];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -080079#endif
80
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080081/*
82 * TODO: Cleanup gpio_bank usage as it is having information
83 * related to all instances of the device
84 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085static struct gpio_bank *gpio_bank;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080086
Varadarajan, Charulathac95d10b2010-12-07 16:26:56 -080087/* TODO: Analyze removing gpio_bank_count usage from driver code */
88int gpio_bank_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010089
Kevin Hilman129fd222011-04-22 07:59:07 -070090#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
91#define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010092
93static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
94{
Tony Lindgren92105bb2005-09-07 17:20:26 +010095 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010096 u32 l;
97
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070098 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099 l = __raw_readl(reg);
100 if (is_input)
101 l |= 1 << gpio;
102 else
103 l &= ~(1 << gpio);
104 __raw_writel(l, reg);
105}
106
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700107
108/* set data out value using dedicate set/clear register */
109static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100110{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100111 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700112 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100113
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700114 if (enable)
115 reg += bank->regs->set_dataout;
116 else
117 reg += bank->regs->clr_dataout;
118
119 __raw_writel(l, reg);
120}
121
122/* set data out value using mask register */
123static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
124{
125 void __iomem *reg = bank->base + bank->regs->dataout;
126 u32 gpio_bit = GPIO_BIT(bank, gpio);
127 u32 l;
128
129 l = __raw_readl(reg);
130 if (enable)
131 l |= gpio_bit;
132 else
133 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134 __raw_writel(l, reg);
135}
136
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300137static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100138{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700139 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100140
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700141 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100142}
143
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300144static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
145{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700146 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300147
Kevin Hilman129fd222011-04-22 07:59:07 -0700148 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300149}
150
Kevin Hilmanece95282011-07-12 08:18:15 -0700151static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
152{
153 int l = __raw_readl(base + reg);
154
155 if (set)
156 l |= mask;
157 else
158 l &= ~mask;
159
160 __raw_writel(l, base + reg);
161}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100162
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700163/**
164 * _set_gpio_debounce - low level gpio debounce time
165 * @bank: the gpio bank we're acting upon
166 * @gpio: the gpio number on this @gpio
167 * @debounce: debounce time to use
168 *
169 * OMAP's debounce time is in 31us steps so we need
170 * to convert and round up to the closest unit.
171 */
172static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
173 unsigned debounce)
174{
Kevin Hilman9942da02011-04-22 12:02:05 -0700175 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700176 u32 val;
177 u32 l;
178
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800179 if (!bank->dbck_flag)
180 return;
181
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700182 if (debounce < 32)
183 debounce = 0x01;
184 else if (debounce > 7936)
185 debounce = 0xff;
186 else
187 debounce = (debounce / 0x1f) - 1;
188
Kevin Hilman129fd222011-04-22 07:59:07 -0700189 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700190
Kevin Hilman9942da02011-04-22 12:02:05 -0700191 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700192 __raw_writel(debounce, reg);
193
Kevin Hilman9942da02011-04-22 12:02:05 -0700194 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700195 val = __raw_readl(reg);
196
197 if (debounce) {
198 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800199 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700200 } else {
201 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800202 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700203 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300204 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700205
206 __raw_writel(val, reg);
207}
208
Tony Lindgren140455f2010-02-12 12:26:48 -0800209#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700210static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
211 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100212{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800213 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100214 u32 gpio_bit = 1 << gpio;
215
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530216 if (cpu_is_omap44xx()) {
Kevin Hilmanece95282011-07-12 08:18:15 -0700217 _gpio_rmw(base, OMAP4_GPIO_LEVELDETECT0, gpio_bit,
218 trigger & IRQ_TYPE_LEVEL_LOW);
219 _gpio_rmw(base, OMAP4_GPIO_LEVELDETECT1, gpio_bit,
220 trigger & IRQ_TYPE_LEVEL_HIGH);
221 _gpio_rmw(base, OMAP4_GPIO_RISINGDETECT, gpio_bit,
222 trigger & IRQ_TYPE_EDGE_RISING);
223 _gpio_rmw(base, OMAP4_GPIO_FALLINGDETECT, gpio_bit,
224 trigger & IRQ_TYPE_EDGE_FALLING);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530225 } else {
Kevin Hilmanece95282011-07-12 08:18:15 -0700226 _gpio_rmw(base, OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
227 trigger & IRQ_TYPE_LEVEL_LOW);
228 _gpio_rmw(base, OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
229 trigger & IRQ_TYPE_LEVEL_HIGH);
230 _gpio_rmw(base, OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
231 trigger & IRQ_TYPE_EDGE_RISING);
232 _gpio_rmw(base, OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
233 trigger & IRQ_TYPE_EDGE_FALLING);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530234 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800235 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530236 if (cpu_is_omap44xx()) {
Kevin Hilmanece95282011-07-12 08:18:15 -0700237 _gpio_rmw(base, OMAP4_GPIO_IRQWAKEN0, gpio_bit,
238 trigger != 0);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530239 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000240 /*
241 * GPIO wakeup request can only be generated on edge
242 * transitions
243 */
244 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530245 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700246 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530247 else
248 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700249 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530250 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200251 }
Ambresh K55b220c2011-06-15 13:40:45 -0700252 /* This part needs to be executed always for OMAP{34xx, 44xx} */
253 if (cpu_is_omap34xx() || cpu_is_omap44xx() ||
254 (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000255 /*
256 * Log the edge gpio and manually trigger the IRQ
257 * after resume if the input level changes
258 * to avoid irq lost during PER RET/OFF mode
259 * Applies for omap2 non-wakeup gpio and all omap3 gpios
260 */
261 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800262 bank->enabled_non_wakeup_gpios |= gpio_bit;
263 else
264 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
265 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700266
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530267 if (cpu_is_omap44xx()) {
268 bank->level_mask =
269 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
270 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
271 } else {
272 bank->level_mask =
273 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
274 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
275 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100276}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800277#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100278
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800279#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800280/*
281 * This only applies to chips that can't do both rising and falling edge
282 * detection at once. For all other chips, this function is a noop.
283 */
284static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
285{
286 void __iomem *reg = bank->base;
287 u32 l = 0;
288
289 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800290 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800291 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800292 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800293#ifdef CONFIG_ARCH_OMAP15XX
294 case METHOD_GPIO_1510:
295 reg += OMAP1510_GPIO_INT_CONTROL;
296 break;
297#endif
298#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
299 case METHOD_GPIO_7XX:
300 reg += OMAP7XX_GPIO_INT_CONTROL;
301 break;
302#endif
303 default:
304 return;
305 }
306
307 l = __raw_readl(reg);
308 if ((l >> gpio) & 1)
309 l &= ~(1 << gpio);
310 else
311 l |= 1 << gpio;
312
313 __raw_writel(l, reg);
314}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800315#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800316
Tony Lindgren92105bb2005-09-07 17:20:26 +0100317static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
318{
319 void __iomem *reg = bank->base;
320 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100321
322 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800323#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100324 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800325 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100326 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000327 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800328 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100329 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100330 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100331 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100332 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100333 else
334 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100335 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800336#endif
337#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100338 case METHOD_GPIO_1510:
339 reg += OMAP1510_GPIO_INT_CONTROL;
340 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000341 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800342 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100343 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100344 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100345 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100346 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100347 else
348 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100349 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800350#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800351#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100352 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100353 if (gpio & 0x08)
354 reg += OMAP1610_GPIO_EDGE_CTRL2;
355 else
356 reg += OMAP1610_GPIO_EDGE_CTRL1;
357 gpio &= 0x07;
358 l = __raw_readl(reg);
359 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100360 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100361 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100362 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100363 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800364 if (trigger)
365 /* Enable wake-up during idle for dynamic tick */
366 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
367 else
368 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100369 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800370#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100371#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100372 case METHOD_GPIO_7XX:
373 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700374 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000375 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800376 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700377 if (trigger & IRQ_TYPE_EDGE_RISING)
378 l |= 1 << gpio;
379 else if (trigger & IRQ_TYPE_EDGE_FALLING)
380 l &= ~(1 << gpio);
381 else
382 goto bad;
383 break;
384#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800385#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100386 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800387 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800388 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200389 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800390#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100391 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100392 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100393 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100394 __raw_writel(l, reg);
395 return 0;
396bad:
397 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100398}
399
Lennert Buytenheke9191022010-11-29 11:17:17 +0100400static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100401{
402 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100403 unsigned gpio;
404 int retval;
David Brownella6472532008-03-03 04:33:30 -0800405 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100406
Lennert Buytenheke9191022010-11-29 11:17:17 +0100407 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
408 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100409 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100410 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100411
David Brownelle5c56ed2006-12-06 17:13:59 -0800412 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100413 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800414
415 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800416 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800417 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100418 return -EINVAL;
419
Lennert Buytenheke9191022010-11-29 11:17:17 +0100420 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800421 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700422 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800423 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800424
425 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100426 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800427 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100428 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800429
Tony Lindgren92105bb2005-09-07 17:20:26 +0100430 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100431}
432
433static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
434{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100435 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100436
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700437 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100438 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300439
440 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700441 if (bank->regs->irqstatus2) {
442 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700443 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700444 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700445
446 /* Flush posted write for the irq status to avoid spurious interrupts */
447 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100448}
449
450static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
451{
Kevin Hilman129fd222011-04-22 07:59:07 -0700452 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100453}
454
Imre Deakea6dedd2006-06-26 16:16:00 -0700455static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
456{
457 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700458 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700459 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700460
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700461 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700462 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700463 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700464 l = ~l;
465 l &= mask;
466 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700467}
468
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700469static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100470{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100471 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100472 u32 l;
473
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700474 if (bank->regs->set_irqenable) {
475 reg += bank->regs->set_irqenable;
476 l = gpio_mask;
477 } else {
478 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100479 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700480 if (bank->regs->irqenable_inv)
481 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100482 else
483 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100484 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700485
486 __raw_writel(l, reg);
487}
488
489static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
490{
491 void __iomem *reg = bank->base;
492 u32 l;
493
494 if (bank->regs->clr_irqenable) {
495 reg += bank->regs->clr_irqenable;
496 l = gpio_mask;
497 } else {
498 reg += bank->regs->irqenable;
499 l = __raw_readl(reg);
500 if (bank->regs->irqenable_inv)
501 l |= gpio_mask;
502 else
503 l &= ~gpio_mask;
504 }
505
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100506 __raw_writel(l, reg);
507}
508
509static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
510{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700511 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100512}
513
Tony Lindgren92105bb2005-09-07 17:20:26 +0100514/*
515 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
516 * 1510 does not seem to have a wake-up register. If JTAG is connected
517 * to the target, system will wake up always on GPIO events. While
518 * system is running all registered GPIO interrupts need to have wake-up
519 * enabled. When system is suspended, only selected GPIO interrupts need
520 * to have wake-up enabled.
521 */
522static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
523{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700524 u32 gpio_bit = GPIO_BIT(bank, gpio);
525 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800526
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700527 if (bank->non_wakeup_gpios & gpio_bit) {
528 dev_err(bank->dev,
529 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100530 return -EINVAL;
531 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700532
533 spin_lock_irqsave(&bank->lock, flags);
534 if (enable)
535 bank->suspend_wakeup |= gpio_bit;
536 else
537 bank->suspend_wakeup &= ~gpio_bit;
538
539 spin_unlock_irqrestore(&bank->lock, flags);
540
541 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100542}
543
Tony Lindgren4196dd62006-09-25 12:41:38 +0300544static void _reset_gpio(struct gpio_bank *bank, int gpio)
545{
Kevin Hilman129fd222011-04-22 07:59:07 -0700546 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300547 _set_gpio_irqenable(bank, gpio, 0);
548 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700549 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300550}
551
Tony Lindgren92105bb2005-09-07 17:20:26 +0100552/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100553static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100554{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100555 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100556 struct gpio_bank *bank;
557 int retval;
558
Lennert Buytenheke9191022010-11-29 11:17:17 +0100559 bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700560 retval = _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100561
562 return retval;
563}
564
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800565static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100566{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800567 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800568 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100569
David Brownella6472532008-03-03 04:33:30 -0800570 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100571
Tony Lindgren4196dd62006-09-25 12:41:38 +0300572 /* Set trigger to none. You need to enable the desired trigger with
573 * request_irq() or set_irq_type().
574 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800575 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100576
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000577#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100579 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100580
Tony Lindgren92105bb2005-09-07 17:20:26 +0100581 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100582 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800583 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100584 }
585#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800586 if (!cpu_class_is_omap1()) {
587 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700588 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800589 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700590
591 if (cpu_is_omap24xx() || cpu_is_omap34xx())
592 reg += OMAP24XX_GPIO_CTRL;
593 else if (cpu_is_omap44xx())
594 reg += OMAP4_GPIO_CTRL;
595 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800596 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700597 ctrl &= 0xFFFFFFFE;
598 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800599 }
600 bank->mod_usage |= 1 << offset;
601 }
David Brownella6472532008-03-03 04:33:30 -0800602 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100603
604 return 0;
605}
606
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800607static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100608{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800609 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800610 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100611
David Brownella6472532008-03-03 04:33:30 -0800612 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100613#ifdef CONFIG_ARCH_OMAP16XX
614 if (bank->method == METHOD_GPIO_1610) {
615 /* Disable wake-up during idle for dynamic tick */
616 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800617 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100618 }
619#endif
Charulatha V9f096862010-05-14 12:05:27 -0700620#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
621 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100622 /* Disable wake-up during idle for dynamic tick */
623 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800624 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100625 }
626#endif
Charulatha V9f096862010-05-14 12:05:27 -0700627#ifdef CONFIG_ARCH_OMAP4
628 if (bank->method == METHOD_GPIO_44XX) {
629 /* Disable wake-up during idle for dynamic tick */
630 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
631 __raw_writel(1 << offset, reg);
632 }
633#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800634 if (!cpu_class_is_omap1()) {
635 bank->mod_usage &= ~(1 << offset);
636 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700637 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800638 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700639
640 if (cpu_is_omap24xx() || cpu_is_omap34xx())
641 reg += OMAP24XX_GPIO_CTRL;
642 else if (cpu_is_omap44xx())
643 reg += OMAP4_GPIO_CTRL;
644 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800645 /* Module is disabled, clocks are gated */
646 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700647 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800648 }
649 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800650 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800651 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100652}
653
654/*
655 * We need to unmask the GPIO bank interrupt as soon as possible to
656 * avoid missing GPIO interrupts for other lines in the bank.
657 * Then we need to mask-read-clear-unmask the triggered GPIO lines
658 * in the bank to avoid missing nested interrupts for a GPIO line.
659 * If we wait to unmask individual GPIO lines in the bank after the
660 * line's interrupt handler has been run, we may miss some nested
661 * interrupts.
662 */
Russell King10dd5ce2006-11-23 11:41:32 +0000663static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100664{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100665 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100666 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800667 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100668 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700669 u32 retrigger = 0;
670 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000671 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100672
Will Deaconee144182011-02-21 13:46:08 +0000673 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100674
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100675 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700676 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800677
678 if (WARN_ON(!isr_reg))
679 goto exit;
680
Tony Lindgren92105bb2005-09-07 17:20:26 +0100681 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100682 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700683 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100684
Imre Deakea6dedd2006-06-26 16:16:00 -0700685 enabled = _get_gpio_irqbank_mask(bank);
686 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100687
688 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
689 isr &= 0x0000ffff;
690
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800691 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800692 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -0700693 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100694
695 /* clear edge sensitive interrupts before handler(s) are
696 called so that we don't miss any interrupt occurred while
697 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700698 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100699 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700700 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100701
702 /* if there is only edge sensitive GPIO pin interrupts
703 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700704 if (!level_mask && !unmasked) {
705 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000706 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700707 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100708
Imre Deakea6dedd2006-06-26 16:16:00 -0700709 isr |= retrigger;
710 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100711 if (!isr)
712 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100713
Tony Lindgren92105bb2005-09-07 17:20:26 +0100714 gpio_irq = bank->virtual_irq_start;
715 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700716 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800717
Tony Lindgren92105bb2005-09-07 17:20:26 +0100718 if (!(isr & 1))
719 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200720
Cory Maccarrone4318f362010-01-08 10:29:04 -0800721#ifdef CONFIG_ARCH_OMAP1
722 /*
723 * Some chips can't respond to both rising and falling
724 * at the same time. If this irq was requested with
725 * both flags, we need to flip the ICR data for the IRQ
726 * to respond to the IRQ for the opposite direction.
727 * This will be indicated in the bank toggle_mask.
728 */
729 if (bank->toggle_mask & (1 << gpio_index))
730 _toggle_gpio_edge_triggering(bank, gpio_index);
731#endif
732
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100733 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100734 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000735 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700736 /* if bank has any level sensitive GPIO pin interrupt
737 configured, we must unmask the bank interrupt only after
738 handler(s) are executed in order to avoid spurious bank
739 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800740exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700741 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000742 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100743}
744
Lennert Buytenheke9191022010-11-29 11:17:17 +0100745static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300746{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100747 unsigned int gpio = d->irq - IH_GPIO_BASE;
748 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700749 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300750
Colin Cross85ec7b92011-06-06 13:38:18 -0700751 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300752 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700753 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300754}
755
Lennert Buytenheke9191022010-11-29 11:17:17 +0100756static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100757{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100758 unsigned int gpio = d->irq - IH_GPIO_BASE;
759 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100760
761 _clear_gpio_irqstatus(bank, gpio);
762}
763
Lennert Buytenheke9191022010-11-29 11:17:17 +0100764static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100765{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100766 unsigned int gpio = d->irq - IH_GPIO_BASE;
767 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700768 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100769
Colin Cross85ec7b92011-06-06 13:38:18 -0700770 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100771 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700772 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700773 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100774}
775
Lennert Buytenheke9191022010-11-29 11:17:17 +0100776static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100777{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100778 unsigned int gpio = d->irq - IH_GPIO_BASE;
779 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700780 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100781 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700782 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700783
Colin Cross85ec7b92011-06-06 13:38:18 -0700784 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700785 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700786 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800787
788 /* For level-triggered GPIOs, the clearing must be done after
789 * the HW source is cleared, thus after the handler has run */
790 if (bank->level_mask & irq_mask) {
791 _set_gpio_irqenable(bank, gpio, 0);
792 _clear_gpio_irqstatus(bank, gpio);
793 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100794
Kevin Hilman4de8c752008-01-16 21:56:14 -0800795 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700796 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100797}
798
David Brownelle5c56ed2006-12-06 17:13:59 -0800799static struct irq_chip gpio_irq_chip = {
800 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100801 .irq_shutdown = gpio_irq_shutdown,
802 .irq_ack = gpio_ack_irq,
803 .irq_mask = gpio_mask_irq,
804 .irq_unmask = gpio_unmask_irq,
805 .irq_set_type = gpio_irq_type,
806 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800807};
808
809/*---------------------------------------------------------------------*/
810
811#ifdef CONFIG_ARCH_OMAP1
812
David Brownelle5c56ed2006-12-06 17:13:59 -0800813#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
814
David Brownell11a78b72006-12-06 17:14:11 -0800815#ifdef CONFIG_ARCH_OMAP16XX
816
817#include <linux/platform_device.h>
818
Magnus Damm79ee0312009-07-08 13:22:04 +0200819static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800820{
Magnus Damm79ee0312009-07-08 13:22:04 +0200821 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800822 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800823 void __iomem *mask_reg = bank->base +
824 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800825 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800826
David Brownella6472532008-03-03 04:33:30 -0800827 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800828 bank->saved_wakeup = __raw_readl(mask_reg);
829 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800830 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800831
832 return 0;
833}
834
Magnus Damm79ee0312009-07-08 13:22:04 +0200835static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800836{
Magnus Damm79ee0312009-07-08 13:22:04 +0200837 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800838 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800839 void __iomem *mask_reg = bank->base +
840 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800841 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800842
David Brownella6472532008-03-03 04:33:30 -0800843 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800844 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800845 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800846
847 return 0;
848}
849
Alexey Dobriyan47145212009-12-14 18:00:08 -0800850static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200851 .suspend_noirq = omap_mpuio_suspend_noirq,
852 .resume_noirq = omap_mpuio_resume_noirq,
853};
854
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200855/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800856static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800857 .driver = {
858 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200859 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800860 },
861};
862
863static struct platform_device omap_mpuio_device = {
864 .name = "mpuio",
865 .id = -1,
866 .dev = {
867 .driver = &omap_mpuio_driver.driver,
868 }
869 /* could list the /proc/iomem resources */
870};
871
872static inline void mpuio_init(void)
873{
Charulatha Va8be8da2011-04-22 16:38:16 +0530874 struct gpio_bank *bank = &gpio_bank[0];
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800875 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700876
David Brownell11a78b72006-12-06 17:14:11 -0800877 if (platform_driver_register(&omap_mpuio_driver) == 0)
878 (void) platform_device_register(&omap_mpuio_device);
879}
880
881#else
882static inline void mpuio_init(void) {}
883#endif /* 16xx */
884
David Brownelle5c56ed2006-12-06 17:13:59 -0800885#else
886
David Brownelle5c56ed2006-12-06 17:13:59 -0800887#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -0800888static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -0800889
890#endif
891
892/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100893
David Brownell52e31342008-03-03 12:43:23 -0800894/* REVISIT these are stupid implementations! replace by ones that
895 * don't switch on METHOD_* and which mostly avoid spinlocks
896 */
897
898static int gpio_input(struct gpio_chip *chip, unsigned offset)
899{
900 struct gpio_bank *bank;
901 unsigned long flags;
902
903 bank = container_of(chip, struct gpio_bank, chip);
904 spin_lock_irqsave(&bank->lock, flags);
905 _set_gpio_direction(bank, offset, 1);
906 spin_unlock_irqrestore(&bank->lock, flags);
907 return 0;
908}
909
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300910static int gpio_is_input(struct gpio_bank *bank, int mask)
911{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700912 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300913
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300914 return __raw_readl(reg) & mask;
915}
916
David Brownell52e31342008-03-03 12:43:23 -0800917static int gpio_get(struct gpio_chip *chip, unsigned offset)
918{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300919 struct gpio_bank *bank;
920 void __iomem *reg;
921 int gpio;
922 u32 mask;
923
924 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530925 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300926 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700927 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300928
929 if (gpio_is_input(bank, mask))
930 return _get_gpio_datain(bank, gpio);
931 else
932 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800933}
934
935static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
936{
937 struct gpio_bank *bank;
938 unsigned long flags;
939
940 bank = container_of(chip, struct gpio_bank, chip);
941 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700942 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800943 _set_gpio_direction(bank, offset, 0);
944 spin_unlock_irqrestore(&bank->lock, flags);
945 return 0;
946}
947
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700948static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
949 unsigned debounce)
950{
951 struct gpio_bank *bank;
952 unsigned long flags;
953
954 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800955
956 if (!bank->dbck) {
957 bank->dbck = clk_get(bank->dev, "dbclk");
958 if (IS_ERR(bank->dbck))
959 dev_err(bank->dev, "Could not get gpio dbck\n");
960 }
961
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700962 spin_lock_irqsave(&bank->lock, flags);
963 _set_gpio_debounce(bank, offset, debounce);
964 spin_unlock_irqrestore(&bank->lock, flags);
965
966 return 0;
967}
968
David Brownell52e31342008-03-03 12:43:23 -0800969static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
970{
971 struct gpio_bank *bank;
972 unsigned long flags;
973
974 bank = container_of(chip, struct gpio_bank, chip);
975 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700976 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800977 spin_unlock_irqrestore(&bank->lock, flags);
978}
979
David Brownella007b702008-12-10 17:35:25 -0800980static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
981{
982 struct gpio_bank *bank;
983
984 bank = container_of(chip, struct gpio_bank, chip);
985 return bank->virtual_irq_start + offset;
986}
987
David Brownell52e31342008-03-03 12:43:23 -0800988/*---------------------------------------------------------------------*/
989
Tony Lindgren9a748052010-12-07 16:26:56 -0800990static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700991{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700992 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700993 u32 rev;
994
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700995 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700996 return;
997
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700998 rev = __raw_readw(bank->base + bank->regs->revision);
999 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001000 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001001
1002 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001003}
1004
David Brownell8ba55c52008-02-26 11:10:50 -08001005/* This lock class tells lockdep that GPIO irqs are in a different
1006 * category than their parents, so it won't report false recursion.
1007 */
1008static struct lock_class_key gpio_lock_class;
1009
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001010static inline int init_gpio_info(struct platform_device *pdev)
1011{
1012 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1013 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1014 GFP_KERNEL);
1015 if (!gpio_bank) {
1016 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1017 return -ENOMEM;
1018 }
1019 return 0;
1020}
1021
1022/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001023static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1024{
1025 if (cpu_class_is_omap2()) {
1026 if (cpu_is_omap44xx()) {
1027 __raw_writel(0xffffffff, bank->base +
1028 OMAP4_GPIO_IRQSTATUSCLR0);
1029 __raw_writel(0x00000000, bank->base +
1030 OMAP4_GPIO_DEBOUNCENABLE);
1031 /* Initialize interface clk ungated, module enabled */
1032 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1033 } else if (cpu_is_omap34xx()) {
1034 __raw_writel(0x00000000, bank->base +
1035 OMAP24XX_GPIO_IRQENABLE1);
1036 __raw_writel(0xffffffff, bank->base +
1037 OMAP24XX_GPIO_IRQSTATUS1);
1038 __raw_writel(0x00000000, bank->base +
1039 OMAP24XX_GPIO_DEBOUNCE_EN);
1040
1041 /* Initialize interface clk ungated, module enabled */
1042 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1043 } else if (cpu_is_omap24xx()) {
1044 static const u32 non_wakeup_gpios[] = {
1045 0xe203ffc0, 0x08700040
1046 };
1047 if (id < ARRAY_SIZE(non_wakeup_gpios))
1048 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1049 }
1050 } else if (cpu_class_is_omap1()) {
1051 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001052 __raw_writew(0xffff, bank->base +
1053 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001054 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1055 __raw_writew(0xffff, bank->base
1056 + OMAP1510_GPIO_INT_MASK);
1057 __raw_writew(0x0000, bank->base
1058 + OMAP1510_GPIO_INT_STATUS);
1059 }
1060 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1061 __raw_writew(0x0000, bank->base
1062 + OMAP1610_GPIO_IRQENABLE1);
1063 __raw_writew(0xffff, bank->base
1064 + OMAP1610_GPIO_IRQSTATUS1);
1065 __raw_writew(0x0014, bank->base
1066 + OMAP1610_GPIO_SYSCONFIG);
1067
1068 /*
1069 * Enable system clock for GPIO module.
1070 * The CAM_CLK_CTRL *is* really the right place.
1071 */
1072 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1073 ULPD_CAM_CLK_CTRL);
1074 }
1075 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1076 __raw_writel(0xffffffff, bank->base
1077 + OMAP7XX_GPIO_INT_MASK);
1078 __raw_writel(0x00000000, bank->base
1079 + OMAP7XX_GPIO_INT_STATUS);
1080 }
1081 }
1082}
1083
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001084static __init void
1085omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1086 unsigned int num)
1087{
1088 struct irq_chip_generic *gc;
1089 struct irq_chip_type *ct;
1090
1091 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1092 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -07001093 if (!gc) {
1094 dev_err(bank->dev, "Memory alloc failed for gc\n");
1095 return;
1096 }
1097
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001098 ct = gc->chip_types;
1099
1100 /* NOTE: No ack required, reading IRQ status clears it. */
1101 ct->chip.irq_mask = irq_gc_mask_set_bit;
1102 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1103 ct->chip.irq_set_type = gpio_irq_type;
1104 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1105 if (cpu_is_omap16xx())
1106 ct->chip.irq_set_wake = gpio_wake_enable,
1107
1108 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1109 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1110 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1111}
1112
Russell Kingd52b31d2011-05-27 13:56:12 -07001113static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001114{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001115 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001116 static int gpio;
1117
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001118 bank->mod_usage = 0;
1119 /*
1120 * REVISIT eventually switch from OMAP-specific gpio structs
1121 * over to the generic ones
1122 */
1123 bank->chip.request = omap_gpio_request;
1124 bank->chip.free = omap_gpio_free;
1125 bank->chip.direction_input = gpio_input;
1126 bank->chip.get = gpio_get;
1127 bank->chip.direction_output = gpio_output;
1128 bank->chip.set_debounce = gpio_debounce;
1129 bank->chip.set = gpio_set;
1130 bank->chip.to_irq = gpio_2irq;
1131 if (bank_is_mpuio(bank)) {
1132 bank->chip.label = "mpuio";
1133#ifdef CONFIG_ARCH_OMAP16XX
1134 bank->chip.dev = &omap_mpuio_device.dev;
1135#endif
1136 bank->chip.base = OMAP_MPUIO(0);
1137 } else {
1138 bank->chip.label = "gpio";
1139 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001140 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001141 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001142 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001143
1144 gpiochip_add(&bank->chip);
1145
1146 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001147 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001148 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001149 irq_set_chip_data(j, bank);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001150 if (bank_is_mpuio(bank)) {
1151 omap_mpuio_alloc_gc(bank, j, bank->width);
1152 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001153 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001154 irq_set_handler(j, handle_simple_irq);
1155 set_irq_flags(j, IRQF_VALID);
1156 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001157 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001158 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1159 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001160}
1161
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001162static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001163{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001164 static int gpio_init_done;
1165 struct omap_gpio_platform_data *pdata;
1166 struct resource *res;
1167 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001168 struct gpio_bank *bank;
1169
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001170 if (!pdev->dev.platform_data)
1171 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001172
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001173 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001174
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001175 if (!gpio_init_done) {
1176 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001177
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001178 ret = init_gpio_info(pdev);
1179 if (ret)
1180 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001181 }
1182
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001183 id = pdev->id;
1184 bank = &gpio_bank[id];
1185
1186 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1187 if (unlikely(!res)) {
1188 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1189 return -ENODEV;
1190 }
1191
1192 bank->irq = res->start;
1193 bank->virtual_irq_start = pdata->virtual_irq_start;
1194 bank->method = pdata->bank_type;
1195 bank->dev = &pdev->dev;
1196 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001197 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001198 bank->width = pdata->bank_width;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001199
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001200 bank->regs = pdata->regs;
1201
1202 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1203 bank->set_dataout = _set_gpio_dataout_reg;
1204 else
1205 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001206
1207 spin_lock_init(&bank->lock);
1208
1209 /* Static mapping, never released */
1210 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1211 if (unlikely(!res)) {
1212 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1213 return -ENODEV;
1214 }
1215
1216 bank->base = ioremap(res->start, resource_size(res));
1217 if (!bank->base) {
1218 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1219 return -ENOMEM;
1220 }
1221
1222 pm_runtime_enable(bank->dev);
1223 pm_runtime_get_sync(bank->dev);
1224
1225 omap_gpio_mod_init(bank, id);
1226 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001227 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001228
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001229 if (!gpio_init_done)
1230 gpio_init_done = 1;
1231
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001232 return 0;
1233}
1234
Tony Lindgren140455f2010-02-12 12:26:48 -08001235#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001236static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001237{
1238 int i;
1239
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001240 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001241 return 0;
1242
1243 for (i = 0; i < gpio_bank_count; i++) {
1244 struct gpio_bank *bank = &gpio_bank[i];
1245 void __iomem *wake_status;
1246 void __iomem *wake_clear;
1247 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001248 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001249
1250 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001251#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001252 case METHOD_GPIO_1610:
1253 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1254 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1255 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1256 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001257#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001258#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001259 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001260 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001261 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1262 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1263 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001264#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301265#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001266 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301267 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1268 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1269 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1270 break;
1271#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001272 default:
1273 continue;
1274 }
1275
David Brownella6472532008-03-03 04:33:30 -08001276 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001277 bank->saved_wakeup = __raw_readl(wake_status);
1278 __raw_writel(0xffffffff, wake_clear);
1279 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001280 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001281 }
1282
1283 return 0;
1284}
1285
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001286static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001287{
1288 int i;
1289
Tero Kristo723fdb72008-11-26 14:35:16 -08001290 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001291 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001292
1293 for (i = 0; i < gpio_bank_count; i++) {
1294 struct gpio_bank *bank = &gpio_bank[i];
1295 void __iomem *wake_clear;
1296 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001297 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001298
1299 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001300#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001301 case METHOD_GPIO_1610:
1302 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1303 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1304 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001305#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001306#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001307 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001308 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1309 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001310 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001311#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301312#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001313 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301314 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1315 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1316 break;
1317#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001318 default:
1319 continue;
1320 }
1321
David Brownella6472532008-03-03 04:33:30 -08001322 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001323 __raw_writel(0xffffffff, wake_clear);
1324 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001325 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001326 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001327}
1328
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001329static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001330 .suspend = omap_gpio_suspend,
1331 .resume = omap_gpio_resume,
1332};
1333
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001334#endif
1335
Tony Lindgren140455f2010-02-12 12:26:48 -08001336#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001337
1338static int workaround_enabled;
1339
Paul Walmsley72e06d02010-12-21 21:05:16 -07001340void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001341{
1342 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001343 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001344
Tero Kristoa118b5f2008-12-22 14:27:12 +02001345 if (cpu_is_omap34xx())
1346 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001347
Tero Kristoa118b5f2008-12-22 14:27:12 +02001348 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001349 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001350 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001351 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001352
Kevin Hilman0aed04352010-09-22 16:06:27 -07001353 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001354 clk_disable(bank->dbck);
1355
Paul Walmsley72e06d02010-12-21 21:05:16 -07001356 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001357 continue;
1358
1359 /* If going to OFF, remove triggering for all
1360 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1361 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001362 if (!(bank->enabled_non_wakeup_gpios))
1363 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001364
1365 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1366 bank->saved_datain = __raw_readl(bank->base +
1367 OMAP24XX_GPIO_DATAIN);
1368 l1 = __raw_readl(bank->base +
1369 OMAP24XX_GPIO_FALLINGDETECT);
1370 l2 = __raw_readl(bank->base +
1371 OMAP24XX_GPIO_RISINGDETECT);
1372 }
1373
1374 if (cpu_is_omap44xx()) {
1375 bank->saved_datain = __raw_readl(bank->base +
1376 OMAP4_GPIO_DATAIN);
1377 l1 = __raw_readl(bank->base +
1378 OMAP4_GPIO_FALLINGDETECT);
1379 l2 = __raw_readl(bank->base +
1380 OMAP4_GPIO_RISINGDETECT);
1381 }
1382
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001383 bank->saved_fallingdetect = l1;
1384 bank->saved_risingdetect = l2;
1385 l1 &= ~bank->enabled_non_wakeup_gpios;
1386 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001387
1388 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1389 __raw_writel(l1, bank->base +
1390 OMAP24XX_GPIO_FALLINGDETECT);
1391 __raw_writel(l2, bank->base +
1392 OMAP24XX_GPIO_RISINGDETECT);
1393 }
1394
1395 if (cpu_is_omap44xx()) {
1396 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1397 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1398 }
1399
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001400 c++;
1401 }
1402 if (!c) {
1403 workaround_enabled = 0;
1404 return;
1405 }
1406 workaround_enabled = 1;
1407}
1408
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001409void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001410{
1411 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001412 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001413
Tero Kristoa118b5f2008-12-22 14:27:12 +02001414 if (cpu_is_omap34xx())
1415 min = 1;
1416 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001417 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001418 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001419 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001420
Kevin Hilman0aed04352010-09-22 16:06:27 -07001421 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001422 clk_enable(bank->dbck);
1423
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001424 if (!workaround_enabled)
1425 continue;
1426
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001427 if (!(bank->enabled_non_wakeup_gpios))
1428 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001429
1430 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1431 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001432 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001433 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001434 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001435 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1436 }
1437
1438 if (cpu_is_omap44xx()) {
1439 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301440 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001441 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301442 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001443 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1444 }
1445
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001446 /* Check if any of the non-wakeup interrupt GPIOs have changed
1447 * state. If so, generate an IRQ by software. This is
1448 * horribly racy, but it's the best we can do to work around
1449 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001450 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001451 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001452
1453 /*
1454 * No need to generate IRQs for the rising edge for gpio IRQs
1455 * configured with falling edge only; and vice versa.
1456 */
1457 gen0 = l & bank->saved_fallingdetect;
1458 gen0 &= bank->saved_datain;
1459
1460 gen1 = l & bank->saved_risingdetect;
1461 gen1 &= ~(bank->saved_datain);
1462
1463 /* FIXME: Consider GPIO IRQs with level detections properly! */
1464 gen = l & (~(bank->saved_fallingdetect) &
1465 ~(bank->saved_risingdetect));
1466 /* Consider all GPIO IRQs needed to be updated */
1467 gen |= gen0 | gen1;
1468
1469 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001470 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001471
Sergio Aguirref00d6492010-03-03 16:21:08 +00001472 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001473 old0 = __raw_readl(bank->base +
1474 OMAP24XX_GPIO_LEVELDETECT0);
1475 old1 = __raw_readl(bank->base +
1476 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001477 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001478 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001479 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001480 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001481 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001482 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001483 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001484 OMAP24XX_GPIO_LEVELDETECT1);
1485 }
1486
1487 if (cpu_is_omap44xx()) {
1488 old0 = __raw_readl(bank->base +
1489 OMAP4_GPIO_LEVELDETECT0);
1490 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301491 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001492 __raw_writel(old0 | l, bank->base +
1493 OMAP4_GPIO_LEVELDETECT0);
1494 __raw_writel(old1 | l, bank->base +
1495 OMAP4_GPIO_LEVELDETECT1);
1496 __raw_writel(old0, bank->base +
1497 OMAP4_GPIO_LEVELDETECT0);
1498 __raw_writel(old1, bank->base +
1499 OMAP4_GPIO_LEVELDETECT1);
1500 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001501 }
1502 }
1503
1504}
1505
Tony Lindgren92105bb2005-09-07 17:20:26 +01001506#endif
1507
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001508#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301509/* save the registers of bank 2-6 */
1510void omap_gpio_save_context(void)
1511{
1512 int i;
1513
1514 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1515 for (i = 1; i < gpio_bank_count; i++) {
1516 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301517 gpio_context[i].irqenable1 =
1518 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1519 gpio_context[i].irqenable2 =
1520 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1521 gpio_context[i].wake_en =
1522 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1523 gpio_context[i].ctrl =
1524 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1525 gpio_context[i].oe =
1526 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1527 gpio_context[i].leveldetect0 =
1528 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1529 gpio_context[i].leveldetect1 =
1530 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1531 gpio_context[i].risingdetect =
1532 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1533 gpio_context[i].fallingdetect =
1534 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1535 gpio_context[i].dataout =
1536 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301537 }
1538}
1539
1540/* restore the required registers of bank 2-6 */
1541void omap_gpio_restore_context(void)
1542{
1543 int i;
1544
1545 for (i = 1; i < gpio_bank_count; i++) {
1546 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301547 __raw_writel(gpio_context[i].irqenable1,
1548 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1549 __raw_writel(gpio_context[i].irqenable2,
1550 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1551 __raw_writel(gpio_context[i].wake_en,
1552 bank->base + OMAP24XX_GPIO_WAKE_EN);
1553 __raw_writel(gpio_context[i].ctrl,
1554 bank->base + OMAP24XX_GPIO_CTRL);
1555 __raw_writel(gpio_context[i].oe,
1556 bank->base + OMAP24XX_GPIO_OE);
1557 __raw_writel(gpio_context[i].leveldetect0,
1558 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1559 __raw_writel(gpio_context[i].leveldetect1,
1560 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1561 __raw_writel(gpio_context[i].risingdetect,
1562 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1563 __raw_writel(gpio_context[i].fallingdetect,
1564 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1565 __raw_writel(gpio_context[i].dataout,
1566 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301567 }
1568}
1569#endif
1570
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001571static struct platform_driver omap_gpio_driver = {
1572 .probe = omap_gpio_probe,
1573 .driver = {
1574 .name = "omap_gpio",
1575 },
1576};
1577
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001578/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001579 * gpio driver register needs to be done before
1580 * machine_init functions access gpio APIs.
1581 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001582 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001583static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001584{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001585 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001586}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001587postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001588
Tony Lindgren92105bb2005-09-07 17:20:26 +01001589static int __init omap_gpio_sysinit(void)
1590{
David Brownell11a78b72006-12-06 17:14:11 -08001591 mpuio_init();
1592
Tony Lindgren140455f2010-02-12 12:26:48 -08001593#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001594 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1595 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001596#endif
1597
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001598 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001599}
1600
Tony Lindgren92105bb2005-09-07 17:20:26 +01001601arch_initcall(omap_gpio_sysinit);