blob: 857ec3939fdd0f7865c660b0e47ce162a9819f56 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080022#include <linux/slab.h>
23#include <linux/pm_runtime.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/irqs.h>
28#include <mach/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029#include <asm/mach/irq.h>
30
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031struct gpio_bank {
Tony Lindgren9f7065d2009-10-19 15:25:20 -070032 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010033 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010034 u16 irq;
35 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010036 int method;
Tony Lindgren140455f2010-02-12 12:26:48 -080037#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +010038 u32 suspend_wakeup;
39 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080040#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080041 u32 non_wakeup_gpios;
42 u32 enabled_non_wakeup_gpios;
43
44 u32 saved_datain;
45 u32 saved_fallingdetect;
46 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080047 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080048 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010049 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080050 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080051 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080052 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080053 u32 dbck_enable_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080054 struct device *dev;
55 bool dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -080056 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070057 u32 width;
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))
92
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010093static 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
Tony Lindgren92105bb2005-09-07 17:20:26 +0100151#define MOD_REG_BIT(reg, bit_mask, set) \
152do { \
153 int l = __raw_readl(base + reg); \
154 if (set) l |= bit_mask; \
155 else l &= ~bit_mask; \
156 __raw_writel(l, base + reg); \
157} while(0)
158
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{
171 void __iomem *reg = bank->base;
172 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
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800187 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700188 reg += OMAP4_GPIO_DEBOUNCINGTIME;
189 else
190 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
191
192 __raw_writel(debounce, reg);
193
194 reg = bank->base;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800195 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700196 reg += OMAP4_GPIO_DEBOUNCENABLE;
197 else
198 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
199
200 val = __raw_readl(reg);
201
202 if (debounce) {
203 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800204 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700205 } else {
206 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800207 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700208 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300209 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700210
211 __raw_writel(val, reg);
212}
213
Tony Lindgren140455f2010-02-12 12:26:48 -0800214#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700215static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
216 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100217{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800218 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100219 u32 gpio_bit = 1 << gpio;
220
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530221 if (cpu_is_omap44xx()) {
222 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
223 trigger & IRQ_TYPE_LEVEL_LOW);
224 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
225 trigger & IRQ_TYPE_LEVEL_HIGH);
226 MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
227 trigger & IRQ_TYPE_EDGE_RISING);
228 MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
229 trigger & IRQ_TYPE_EDGE_FALLING);
230 } else {
231 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
232 trigger & IRQ_TYPE_LEVEL_LOW);
233 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
234 trigger & IRQ_TYPE_LEVEL_HIGH);
235 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
236 trigger & IRQ_TYPE_EDGE_RISING);
237 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
238 trigger & IRQ_TYPE_EDGE_FALLING);
239 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800240 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530241 if (cpu_is_omap44xx()) {
Colin Cross0622b252011-06-06 13:38:17 -0700242 MOD_REG_BIT(OMAP4_GPIO_IRQWAKEN0, gpio_bit,
243 trigger != 0);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530244 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000245 /*
246 * GPIO wakeup request can only be generated on edge
247 * transitions
248 */
249 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530250 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700251 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530252 else
253 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700254 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530255 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200256 }
257 /* This part needs to be executed always for OMAP34xx */
258 if (cpu_is_omap34xx() || (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000259 /*
260 * Log the edge gpio and manually trigger the IRQ
261 * after resume if the input level changes
262 * to avoid irq lost during PER RET/OFF mode
263 * Applies for omap2 non-wakeup gpio and all omap3 gpios
264 */
265 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800266 bank->enabled_non_wakeup_gpios |= gpio_bit;
267 else
268 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
269 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700270
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530271 if (cpu_is_omap44xx()) {
272 bank->level_mask =
273 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
274 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
275 } else {
276 bank->level_mask =
277 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
278 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
279 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100280}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800281#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100282
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800283#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800284/*
285 * This only applies to chips that can't do both rising and falling edge
286 * detection at once. For all other chips, this function is a noop.
287 */
288static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
289{
290 void __iomem *reg = bank->base;
291 u32 l = 0;
292
293 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800294 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800295 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800296 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800297#ifdef CONFIG_ARCH_OMAP15XX
298 case METHOD_GPIO_1510:
299 reg += OMAP1510_GPIO_INT_CONTROL;
300 break;
301#endif
302#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
303 case METHOD_GPIO_7XX:
304 reg += OMAP7XX_GPIO_INT_CONTROL;
305 break;
306#endif
307 default:
308 return;
309 }
310
311 l = __raw_readl(reg);
312 if ((l >> gpio) & 1)
313 l &= ~(1 << gpio);
314 else
315 l |= 1 << gpio;
316
317 __raw_writel(l, reg);
318}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800319#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800320
Tony Lindgren92105bb2005-09-07 17:20:26 +0100321static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
322{
323 void __iomem *reg = bank->base;
324 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100325
326 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800327#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100328 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800329 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100330 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000331 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800332 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100333 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100334 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100335 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100336 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100337 else
338 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100339 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800340#endif
341#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100342 case METHOD_GPIO_1510:
343 reg += OMAP1510_GPIO_INT_CONTROL;
344 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000345 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800346 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100347 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100349 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100350 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100351 else
352 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100353 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800354#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800355#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100356 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100357 if (gpio & 0x08)
358 reg += OMAP1610_GPIO_EDGE_CTRL2;
359 else
360 reg += OMAP1610_GPIO_EDGE_CTRL1;
361 gpio &= 0x07;
362 l = __raw_readl(reg);
363 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100364 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100365 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100366 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100367 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800368 if (trigger)
369 /* Enable wake-up during idle for dynamic tick */
370 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
371 else
372 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100373 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800374#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100375#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100376 case METHOD_GPIO_7XX:
377 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700378 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000379 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800380 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700381 if (trigger & IRQ_TYPE_EDGE_RISING)
382 l |= 1 << gpio;
383 else if (trigger & IRQ_TYPE_EDGE_FALLING)
384 l &= ~(1 << gpio);
385 else
386 goto bad;
387 break;
388#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800389#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100390 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800391 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800392 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200393 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800394#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100395 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100396 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100397 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100398 __raw_writel(l, reg);
399 return 0;
400bad:
401 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100402}
403
Lennert Buytenheke9191022010-11-29 11:17:17 +0100404static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100405{
406 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100407 unsigned gpio;
408 int retval;
David Brownella6472532008-03-03 04:33:30 -0800409 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100410
Lennert Buytenheke9191022010-11-29 11:17:17 +0100411 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
412 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100413 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100414 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100415
David Brownelle5c56ed2006-12-06 17:13:59 -0800416 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100417 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800418
419 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800420 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800421 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100422 return -EINVAL;
423
Lennert Buytenheke9191022010-11-29 11:17:17 +0100424 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800425 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700426 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800427 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800428
429 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100430 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800431 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100432 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800433
Tony Lindgren92105bb2005-09-07 17:20:26 +0100434 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100435}
436
437static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
438{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100439 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100440
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700441 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100442 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300443
444 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700445 if (bank->regs->irqstatus2) {
446 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700447 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700448 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700449
450 /* Flush posted write for the irq status to avoid spurious interrupts */
451 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100452}
453
454static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
455{
Kevin Hilman129fd222011-04-22 07:59:07 -0700456 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100457}
458
Imre Deakea6dedd2006-06-26 16:16:00 -0700459static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
460{
461 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700462 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700463 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700464
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700465 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700466 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700467 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700468 l = ~l;
469 l &= mask;
470 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700471}
472
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700473static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100474{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100475 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100476 u32 l;
477
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700478 if (bank->regs->set_irqenable) {
479 reg += bank->regs->set_irqenable;
480 l = gpio_mask;
481 } else {
482 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100483 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700484 if (bank->regs->irqenable_inv)
485 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100486 else
487 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100488 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700489
490 __raw_writel(l, reg);
491}
492
493static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
494{
495 void __iomem *reg = bank->base;
496 u32 l;
497
498 if (bank->regs->clr_irqenable) {
499 reg += bank->regs->clr_irqenable;
500 l = gpio_mask;
501 } else {
502 reg += bank->regs->irqenable;
503 l = __raw_readl(reg);
504 if (bank->regs->irqenable_inv)
505 l |= gpio_mask;
506 else
507 l &= ~gpio_mask;
508 }
509
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510 __raw_writel(l, reg);
511}
512
513static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
514{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700515 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100516}
517
Tony Lindgren92105bb2005-09-07 17:20:26 +0100518/*
519 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
520 * 1510 does not seem to have a wake-up register. If JTAG is connected
521 * to the target, system will wake up always on GPIO events. While
522 * system is running all registered GPIO interrupts need to have wake-up
523 * enabled. When system is suspended, only selected GPIO interrupts need
524 * to have wake-up enabled.
525 */
526static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
527{
Tony Lindgren4cc64202010-01-08 10:29:05 -0800528 unsigned long uninitialized_var(flags);
David Brownella6472532008-03-03 04:33:30 -0800529
Tony Lindgren92105bb2005-09-07 17:20:26 +0100530 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800531#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800532 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100533 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800534 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700535 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100536 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700537 else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100538 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800539 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100540 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800541#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800542#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800543 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800544 case METHOD_GPIO_44XX:
David Brownell11a78b72006-12-06 17:14:11 -0800545 if (bank->non_wakeup_gpios & (1 << gpio)) {
546 printk(KERN_ERR "Unable to modify wakeup on "
547 "non-wakeup GPIO%d\n",
Kevin Hilmand5f46242011-04-21 09:23:00 -0700548 (bank - gpio_bank) * bank->width + gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800549 return -EINVAL;
550 }
David Brownella6472532008-03-03 04:33:30 -0800551 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700552 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800553 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700554 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800555 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800556 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800557 return 0;
558#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100559 default:
560 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
561 bank->method);
562 return -EINVAL;
563 }
564}
565
Tony Lindgren4196dd62006-09-25 12:41:38 +0300566static void _reset_gpio(struct gpio_bank *bank, int gpio)
567{
Kevin Hilman129fd222011-04-22 07:59:07 -0700568 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300569 _set_gpio_irqenable(bank, gpio, 0);
570 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700571 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300572}
573
Tony Lindgren92105bb2005-09-07 17:20:26 +0100574/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100575static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100576{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100577 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100578 struct gpio_bank *bank;
579 int retval;
580
Lennert Buytenheke9191022010-11-29 11:17:17 +0100581 bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700582 retval = _set_gpio_wakeup(bank, GPIO_INDEX(bank, gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100583
584 return retval;
585}
586
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800587static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100588{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800589 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800590 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100591
David Brownella6472532008-03-03 04:33:30 -0800592 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100593
Tony Lindgren4196dd62006-09-25 12:41:38 +0300594 /* Set trigger to none. You need to enable the desired trigger with
595 * request_irq() or set_irq_type().
596 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800597 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100598
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000599#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100600 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100601 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100602
Tony Lindgren92105bb2005-09-07 17:20:26 +0100603 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100604 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800605 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100606 }
607#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800608 if (!cpu_class_is_omap1()) {
609 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700610 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800611 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700612
613 if (cpu_is_omap24xx() || cpu_is_omap34xx())
614 reg += OMAP24XX_GPIO_CTRL;
615 else if (cpu_is_omap44xx())
616 reg += OMAP4_GPIO_CTRL;
617 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800618 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700619 ctrl &= 0xFFFFFFFE;
620 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800621 }
622 bank->mod_usage |= 1 << offset;
623 }
David Brownella6472532008-03-03 04:33:30 -0800624 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100625
626 return 0;
627}
628
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800629static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100630{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800631 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800632 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100633
David Brownella6472532008-03-03 04:33:30 -0800634 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100635#ifdef CONFIG_ARCH_OMAP16XX
636 if (bank->method == METHOD_GPIO_1610) {
637 /* Disable wake-up during idle for dynamic tick */
638 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800639 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100640 }
641#endif
Charulatha V9f096862010-05-14 12:05:27 -0700642#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
643 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100644 /* Disable wake-up during idle for dynamic tick */
645 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800646 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100647 }
648#endif
Charulatha V9f096862010-05-14 12:05:27 -0700649#ifdef CONFIG_ARCH_OMAP4
650 if (bank->method == METHOD_GPIO_44XX) {
651 /* Disable wake-up during idle for dynamic tick */
652 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
653 __raw_writel(1 << offset, reg);
654 }
655#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800656 if (!cpu_class_is_omap1()) {
657 bank->mod_usage &= ~(1 << offset);
658 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700659 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800660 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700661
662 if (cpu_is_omap24xx() || cpu_is_omap34xx())
663 reg += OMAP24XX_GPIO_CTRL;
664 else if (cpu_is_omap44xx())
665 reg += OMAP4_GPIO_CTRL;
666 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800667 /* Module is disabled, clocks are gated */
668 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700669 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800670 }
671 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800672 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800673 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100674}
675
676/*
677 * We need to unmask the GPIO bank interrupt as soon as possible to
678 * avoid missing GPIO interrupts for other lines in the bank.
679 * Then we need to mask-read-clear-unmask the triggered GPIO lines
680 * in the bank to avoid missing nested interrupts for a GPIO line.
681 * If we wait to unmask individual GPIO lines in the bank after the
682 * line's interrupt handler has been run, we may miss some nested
683 * interrupts.
684 */
Russell King10dd5ce2006-11-23 11:41:32 +0000685static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100686{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100687 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100688 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800689 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100690 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700691 u32 retrigger = 0;
692 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000693 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100694
Will Deaconee144182011-02-21 13:46:08 +0000695 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100696
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100697 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700698 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800699
700 if (WARN_ON(!isr_reg))
701 goto exit;
702
Tony Lindgren92105bb2005-09-07 17:20:26 +0100703 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100704 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700705 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100706
Imre Deakea6dedd2006-06-26 16:16:00 -0700707 enabled = _get_gpio_irqbank_mask(bank);
708 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100709
710 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
711 isr &= 0x0000ffff;
712
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800713 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800714 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -0700715 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100716
717 /* clear edge sensitive interrupts before handler(s) are
718 called so that we don't miss any interrupt occurred while
719 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700720 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100721 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700722 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100723
724 /* if there is only edge sensitive GPIO pin interrupts
725 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700726 if (!level_mask && !unmasked) {
727 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000728 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700729 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730
Imre Deakea6dedd2006-06-26 16:16:00 -0700731 isr |= retrigger;
732 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100733 if (!isr)
734 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100735
Tony Lindgren92105bb2005-09-07 17:20:26 +0100736 gpio_irq = bank->virtual_irq_start;
737 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700738 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800739
Tony Lindgren92105bb2005-09-07 17:20:26 +0100740 if (!(isr & 1))
741 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200742
Cory Maccarrone4318f362010-01-08 10:29:04 -0800743#ifdef CONFIG_ARCH_OMAP1
744 /*
745 * Some chips can't respond to both rising and falling
746 * at the same time. If this irq was requested with
747 * both flags, we need to flip the ICR data for the IRQ
748 * to respond to the IRQ for the opposite direction.
749 * This will be indicated in the bank toggle_mask.
750 */
751 if (bank->toggle_mask & (1 << gpio_index))
752 _toggle_gpio_edge_triggering(bank, gpio_index);
753#endif
754
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100755 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100756 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000757 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700758 /* if bank has any level sensitive GPIO pin interrupt
759 configured, we must unmask the bank interrupt only after
760 handler(s) are executed in order to avoid spurious bank
761 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800762exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700763 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000764 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100765}
766
Lennert Buytenheke9191022010-11-29 11:17:17 +0100767static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300768{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100769 unsigned int gpio = d->irq - IH_GPIO_BASE;
770 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700771 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300772
Colin Cross85ec7b92011-06-06 13:38:18 -0700773 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300774 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700775 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300776}
777
Lennert Buytenheke9191022010-11-29 11:17:17 +0100778static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100779{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100780 unsigned int gpio = d->irq - IH_GPIO_BASE;
781 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100782
783 _clear_gpio_irqstatus(bank, gpio);
784}
785
Lennert Buytenheke9191022010-11-29 11:17:17 +0100786static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100787{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100788 unsigned int gpio = d->irq - IH_GPIO_BASE;
789 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700790 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100791
Colin Cross85ec7b92011-06-06 13:38:18 -0700792 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100793 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700794 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700795 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100796}
797
Lennert Buytenheke9191022010-11-29 11:17:17 +0100798static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100799{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100800 unsigned int gpio = d->irq - IH_GPIO_BASE;
801 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700802 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100803 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700804 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700805
Colin Cross85ec7b92011-06-06 13:38:18 -0700806 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700807 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700808 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800809
810 /* For level-triggered GPIOs, the clearing must be done after
811 * the HW source is cleared, thus after the handler has run */
812 if (bank->level_mask & irq_mask) {
813 _set_gpio_irqenable(bank, gpio, 0);
814 _clear_gpio_irqstatus(bank, gpio);
815 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100816
Kevin Hilman4de8c752008-01-16 21:56:14 -0800817 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700818 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100819}
820
David Brownelle5c56ed2006-12-06 17:13:59 -0800821static struct irq_chip gpio_irq_chip = {
822 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100823 .irq_shutdown = gpio_irq_shutdown,
824 .irq_ack = gpio_ack_irq,
825 .irq_mask = gpio_mask_irq,
826 .irq_unmask = gpio_unmask_irq,
827 .irq_set_type = gpio_irq_type,
828 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800829};
830
831/*---------------------------------------------------------------------*/
832
833#ifdef CONFIG_ARCH_OMAP1
834
David Brownelle5c56ed2006-12-06 17:13:59 -0800835#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
836
David Brownell11a78b72006-12-06 17:14:11 -0800837#ifdef CONFIG_ARCH_OMAP16XX
838
839#include <linux/platform_device.h>
840
Magnus Damm79ee0312009-07-08 13:22:04 +0200841static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800842{
Magnus Damm79ee0312009-07-08 13:22:04 +0200843 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800844 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800845 void __iomem *mask_reg = bank->base +
846 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800847 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800848
David Brownella6472532008-03-03 04:33:30 -0800849 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800850 bank->saved_wakeup = __raw_readl(mask_reg);
851 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800852 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800853
854 return 0;
855}
856
Magnus Damm79ee0312009-07-08 13:22:04 +0200857static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800858{
Magnus Damm79ee0312009-07-08 13:22:04 +0200859 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800860 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800861 void __iomem *mask_reg = bank->base +
862 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800863 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800864
David Brownella6472532008-03-03 04:33:30 -0800865 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800866 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800867 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800868
869 return 0;
870}
871
Alexey Dobriyan47145212009-12-14 18:00:08 -0800872static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200873 .suspend_noirq = omap_mpuio_suspend_noirq,
874 .resume_noirq = omap_mpuio_resume_noirq,
875};
876
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200877/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800878static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800879 .driver = {
880 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200881 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800882 },
883};
884
885static struct platform_device omap_mpuio_device = {
886 .name = "mpuio",
887 .id = -1,
888 .dev = {
889 .driver = &omap_mpuio_driver.driver,
890 }
891 /* could list the /proc/iomem resources */
892};
893
894static inline void mpuio_init(void)
895{
Charulatha Va8be8da2011-04-22 16:38:16 +0530896 struct gpio_bank *bank = &gpio_bank[0];
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800897 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700898
David Brownell11a78b72006-12-06 17:14:11 -0800899 if (platform_driver_register(&omap_mpuio_driver) == 0)
900 (void) platform_device_register(&omap_mpuio_device);
901}
902
903#else
904static inline void mpuio_init(void) {}
905#endif /* 16xx */
906
David Brownelle5c56ed2006-12-06 17:13:59 -0800907#else
908
David Brownelle5c56ed2006-12-06 17:13:59 -0800909#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -0800910static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -0800911
912#endif
913
914/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100915
David Brownell52e31342008-03-03 12:43:23 -0800916/* REVISIT these are stupid implementations! replace by ones that
917 * don't switch on METHOD_* and which mostly avoid spinlocks
918 */
919
920static int gpio_input(struct gpio_chip *chip, unsigned offset)
921{
922 struct gpio_bank *bank;
923 unsigned long flags;
924
925 bank = container_of(chip, struct gpio_bank, chip);
926 spin_lock_irqsave(&bank->lock, flags);
927 _set_gpio_direction(bank, offset, 1);
928 spin_unlock_irqrestore(&bank->lock, flags);
929 return 0;
930}
931
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300932static int gpio_is_input(struct gpio_bank *bank, int mask)
933{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700934 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300935
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300936 return __raw_readl(reg) & mask;
937}
938
David Brownell52e31342008-03-03 12:43:23 -0800939static int gpio_get(struct gpio_chip *chip, unsigned offset)
940{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300941 struct gpio_bank *bank;
942 void __iomem *reg;
943 int gpio;
944 u32 mask;
945
946 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530947 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300948 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700949 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300950
951 if (gpio_is_input(bank, mask))
952 return _get_gpio_datain(bank, gpio);
953 else
954 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800955}
956
957static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
958{
959 struct gpio_bank *bank;
960 unsigned long flags;
961
962 bank = container_of(chip, struct gpio_bank, chip);
963 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700964 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800965 _set_gpio_direction(bank, offset, 0);
966 spin_unlock_irqrestore(&bank->lock, flags);
967 return 0;
968}
969
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700970static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
971 unsigned debounce)
972{
973 struct gpio_bank *bank;
974 unsigned long flags;
975
976 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800977
978 if (!bank->dbck) {
979 bank->dbck = clk_get(bank->dev, "dbclk");
980 if (IS_ERR(bank->dbck))
981 dev_err(bank->dev, "Could not get gpio dbck\n");
982 }
983
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700984 spin_lock_irqsave(&bank->lock, flags);
985 _set_gpio_debounce(bank, offset, debounce);
986 spin_unlock_irqrestore(&bank->lock, flags);
987
988 return 0;
989}
990
David Brownell52e31342008-03-03 12:43:23 -0800991static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
992{
993 struct gpio_bank *bank;
994 unsigned long flags;
995
996 bank = container_of(chip, struct gpio_bank, chip);
997 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700998 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800999 spin_unlock_irqrestore(&bank->lock, flags);
1000}
1001
David Brownella007b702008-12-10 17:35:25 -08001002static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1003{
1004 struct gpio_bank *bank;
1005
1006 bank = container_of(chip, struct gpio_bank, chip);
1007 return bank->virtual_irq_start + offset;
1008}
1009
David Brownell52e31342008-03-03 12:43:23 -08001010/*---------------------------------------------------------------------*/
1011
Tony Lindgren9a748052010-12-07 16:26:56 -08001012static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001013{
1014 u32 rev;
1015
Tony Lindgren9a748052010-12-07 16:26:56 -08001016 if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
1017 rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001018 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001019 rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001020 else if (cpu_is_omap44xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001021 rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001022 else
1023 return;
1024
1025 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1026 (rev >> 4) & 0x0f, rev & 0x0f);
1027}
1028
David Brownell8ba55c52008-02-26 11:10:50 -08001029/* This lock class tells lockdep that GPIO irqs are in a different
1030 * category than their parents, so it won't report false recursion.
1031 */
1032static struct lock_class_key gpio_lock_class;
1033
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001034static inline int init_gpio_info(struct platform_device *pdev)
1035{
1036 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1037 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1038 GFP_KERNEL);
1039 if (!gpio_bank) {
1040 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1041 return -ENOMEM;
1042 }
1043 return 0;
1044}
1045
1046/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001047static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1048{
1049 if (cpu_class_is_omap2()) {
1050 if (cpu_is_omap44xx()) {
1051 __raw_writel(0xffffffff, bank->base +
1052 OMAP4_GPIO_IRQSTATUSCLR0);
1053 __raw_writel(0x00000000, bank->base +
1054 OMAP4_GPIO_DEBOUNCENABLE);
1055 /* Initialize interface clk ungated, module enabled */
1056 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1057 } else if (cpu_is_omap34xx()) {
1058 __raw_writel(0x00000000, bank->base +
1059 OMAP24XX_GPIO_IRQENABLE1);
1060 __raw_writel(0xffffffff, bank->base +
1061 OMAP24XX_GPIO_IRQSTATUS1);
1062 __raw_writel(0x00000000, bank->base +
1063 OMAP24XX_GPIO_DEBOUNCE_EN);
1064
1065 /* Initialize interface clk ungated, module enabled */
1066 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1067 } else if (cpu_is_omap24xx()) {
1068 static const u32 non_wakeup_gpios[] = {
1069 0xe203ffc0, 0x08700040
1070 };
1071 if (id < ARRAY_SIZE(non_wakeup_gpios))
1072 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1073 }
1074 } else if (cpu_class_is_omap1()) {
1075 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001076 __raw_writew(0xffff, bank->base +
1077 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001078 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1079 __raw_writew(0xffff, bank->base
1080 + OMAP1510_GPIO_INT_MASK);
1081 __raw_writew(0x0000, bank->base
1082 + OMAP1510_GPIO_INT_STATUS);
1083 }
1084 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1085 __raw_writew(0x0000, bank->base
1086 + OMAP1610_GPIO_IRQENABLE1);
1087 __raw_writew(0xffff, bank->base
1088 + OMAP1610_GPIO_IRQSTATUS1);
1089 __raw_writew(0x0014, bank->base
1090 + OMAP1610_GPIO_SYSCONFIG);
1091
1092 /*
1093 * Enable system clock for GPIO module.
1094 * The CAM_CLK_CTRL *is* really the right place.
1095 */
1096 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1097 ULPD_CAM_CLK_CTRL);
1098 }
1099 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1100 __raw_writel(0xffffffff, bank->base
1101 + OMAP7XX_GPIO_INT_MASK);
1102 __raw_writel(0x00000000, bank->base
1103 + OMAP7XX_GPIO_INT_STATUS);
1104 }
1105 }
1106}
1107
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001108static __init void
1109omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1110 unsigned int num)
1111{
1112 struct irq_chip_generic *gc;
1113 struct irq_chip_type *ct;
1114
1115 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1116 handle_simple_irq);
1117 ct = gc->chip_types;
1118
1119 /* NOTE: No ack required, reading IRQ status clears it. */
1120 ct->chip.irq_mask = irq_gc_mask_set_bit;
1121 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1122 ct->chip.irq_set_type = gpio_irq_type;
1123 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1124 if (cpu_is_omap16xx())
1125 ct->chip.irq_set_wake = gpio_wake_enable,
1126
1127 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1128 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1129 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1130}
1131
Russell Kingd52b31d2011-05-27 13:56:12 -07001132static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001133{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001134 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001135 static int gpio;
1136
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001137 bank->mod_usage = 0;
1138 /*
1139 * REVISIT eventually switch from OMAP-specific gpio structs
1140 * over to the generic ones
1141 */
1142 bank->chip.request = omap_gpio_request;
1143 bank->chip.free = omap_gpio_free;
1144 bank->chip.direction_input = gpio_input;
1145 bank->chip.get = gpio_get;
1146 bank->chip.direction_output = gpio_output;
1147 bank->chip.set_debounce = gpio_debounce;
1148 bank->chip.set = gpio_set;
1149 bank->chip.to_irq = gpio_2irq;
1150 if (bank_is_mpuio(bank)) {
1151 bank->chip.label = "mpuio";
1152#ifdef CONFIG_ARCH_OMAP16XX
1153 bank->chip.dev = &omap_mpuio_device.dev;
1154#endif
1155 bank->chip.base = OMAP_MPUIO(0);
1156 } else {
1157 bank->chip.label = "gpio";
1158 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001159 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001160 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001161 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001162
1163 gpiochip_add(&bank->chip);
1164
1165 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001166 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001167 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001168 irq_set_chip_data(j, bank);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001169 if (bank_is_mpuio(bank)) {
1170 omap_mpuio_alloc_gc(bank, j, bank->width);
1171 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001172 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001173 irq_set_handler(j, handle_simple_irq);
1174 set_irq_flags(j, IRQF_VALID);
1175 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001176 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001177 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1178 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001179}
1180
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001181static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001182{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001183 static int gpio_init_done;
1184 struct omap_gpio_platform_data *pdata;
1185 struct resource *res;
1186 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001187 struct gpio_bank *bank;
1188
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001189 if (!pdev->dev.platform_data)
1190 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001191
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001192 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001193
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001194 if (!gpio_init_done) {
1195 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001196
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001197 ret = init_gpio_info(pdev);
1198 if (ret)
1199 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001200 }
1201
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001202 id = pdev->id;
1203 bank = &gpio_bank[id];
1204
1205 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1206 if (unlikely(!res)) {
1207 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1208 return -ENODEV;
1209 }
1210
1211 bank->irq = res->start;
1212 bank->virtual_irq_start = pdata->virtual_irq_start;
1213 bank->method = pdata->bank_type;
1214 bank->dev = &pdev->dev;
1215 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001216 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001217 bank->width = pdata->bank_width;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001218
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001219 bank->regs = pdata->regs;
1220
1221 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1222 bank->set_dataout = _set_gpio_dataout_reg;
1223 else
1224 bank->set_dataout = _set_gpio_dataout_mask;
1225
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001226 spin_lock_init(&bank->lock);
1227
1228 /* Static mapping, never released */
1229 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1230 if (unlikely(!res)) {
1231 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1232 return -ENODEV;
1233 }
1234
1235 bank->base = ioremap(res->start, resource_size(res));
1236 if (!bank->base) {
1237 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1238 return -ENOMEM;
1239 }
1240
1241 pm_runtime_enable(bank->dev);
1242 pm_runtime_get_sync(bank->dev);
1243
1244 omap_gpio_mod_init(bank, id);
1245 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001246 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001247
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001248 if (!gpio_init_done)
1249 gpio_init_done = 1;
1250
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001251 return 0;
1252}
1253
Tony Lindgren140455f2010-02-12 12:26:48 -08001254#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001255static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001256{
1257 int i;
1258
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001259 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001260 return 0;
1261
1262 for (i = 0; i < gpio_bank_count; i++) {
1263 struct gpio_bank *bank = &gpio_bank[i];
1264 void __iomem *wake_status;
1265 void __iomem *wake_clear;
1266 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001267 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001268
1269 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001270#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001271 case METHOD_GPIO_1610:
1272 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1273 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1274 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1275 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001276#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001277#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001278 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001279 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001280 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1281 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1282 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001283#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301284#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001285 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301286 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1287 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1288 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1289 break;
1290#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001291 default:
1292 continue;
1293 }
1294
David Brownella6472532008-03-03 04:33:30 -08001295 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001296 bank->saved_wakeup = __raw_readl(wake_status);
1297 __raw_writel(0xffffffff, wake_clear);
1298 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001299 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001300 }
1301
1302 return 0;
1303}
1304
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001305static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001306{
1307 int i;
1308
Tero Kristo723fdb72008-11-26 14:35:16 -08001309 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001310 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001311
1312 for (i = 0; i < gpio_bank_count; i++) {
1313 struct gpio_bank *bank = &gpio_bank[i];
1314 void __iomem *wake_clear;
1315 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001316 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001317
1318 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001319#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001320 case METHOD_GPIO_1610:
1321 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1322 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1323 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001324#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001325#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001326 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001327 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1328 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001329 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001330#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301331#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001332 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301333 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1334 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1335 break;
1336#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001337 default:
1338 continue;
1339 }
1340
David Brownella6472532008-03-03 04:33:30 -08001341 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001342 __raw_writel(0xffffffff, wake_clear);
1343 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001344 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001345 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001346}
1347
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001348static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001349 .suspend = omap_gpio_suspend,
1350 .resume = omap_gpio_resume,
1351};
1352
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001353#endif
1354
Tony Lindgren140455f2010-02-12 12:26:48 -08001355#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001356
1357static int workaround_enabled;
1358
Paul Walmsley72e06d02010-12-21 21:05:16 -07001359void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001360{
1361 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001362 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001363
Tero Kristoa118b5f2008-12-22 14:27:12 +02001364 if (cpu_is_omap34xx())
1365 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001366
Tero Kristoa118b5f2008-12-22 14:27:12 +02001367 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001368 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001369 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001370 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001371
Kevin Hilman0aed04352010-09-22 16:06:27 -07001372 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001373 clk_disable(bank->dbck);
1374
Paul Walmsley72e06d02010-12-21 21:05:16 -07001375 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001376 continue;
1377
1378 /* If going to OFF, remove triggering for all
1379 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1380 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001381 if (!(bank->enabled_non_wakeup_gpios))
1382 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001383
1384 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1385 bank->saved_datain = __raw_readl(bank->base +
1386 OMAP24XX_GPIO_DATAIN);
1387 l1 = __raw_readl(bank->base +
1388 OMAP24XX_GPIO_FALLINGDETECT);
1389 l2 = __raw_readl(bank->base +
1390 OMAP24XX_GPIO_RISINGDETECT);
1391 }
1392
1393 if (cpu_is_omap44xx()) {
1394 bank->saved_datain = __raw_readl(bank->base +
1395 OMAP4_GPIO_DATAIN);
1396 l1 = __raw_readl(bank->base +
1397 OMAP4_GPIO_FALLINGDETECT);
1398 l2 = __raw_readl(bank->base +
1399 OMAP4_GPIO_RISINGDETECT);
1400 }
1401
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001402 bank->saved_fallingdetect = l1;
1403 bank->saved_risingdetect = l2;
1404 l1 &= ~bank->enabled_non_wakeup_gpios;
1405 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001406
1407 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1408 __raw_writel(l1, bank->base +
1409 OMAP24XX_GPIO_FALLINGDETECT);
1410 __raw_writel(l2, bank->base +
1411 OMAP24XX_GPIO_RISINGDETECT);
1412 }
1413
1414 if (cpu_is_omap44xx()) {
1415 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1416 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1417 }
1418
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001419 c++;
1420 }
1421 if (!c) {
1422 workaround_enabled = 0;
1423 return;
1424 }
1425 workaround_enabled = 1;
1426}
1427
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001428void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001429{
1430 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001431 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001432
Tero Kristoa118b5f2008-12-22 14:27:12 +02001433 if (cpu_is_omap34xx())
1434 min = 1;
1435 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001436 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001437 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001438 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001439
Kevin Hilman0aed04352010-09-22 16:06:27 -07001440 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001441 clk_enable(bank->dbck);
1442
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001443 if (!workaround_enabled)
1444 continue;
1445
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001446 if (!(bank->enabled_non_wakeup_gpios))
1447 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001448
1449 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1450 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001451 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001452 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001453 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001454 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1455 }
1456
1457 if (cpu_is_omap44xx()) {
1458 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301459 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001460 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301461 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001462 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1463 }
1464
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001465 /* Check if any of the non-wakeup interrupt GPIOs have changed
1466 * state. If so, generate an IRQ by software. This is
1467 * horribly racy, but it's the best we can do to work around
1468 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001469 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001470 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001471
1472 /*
1473 * No need to generate IRQs for the rising edge for gpio IRQs
1474 * configured with falling edge only; and vice versa.
1475 */
1476 gen0 = l & bank->saved_fallingdetect;
1477 gen0 &= bank->saved_datain;
1478
1479 gen1 = l & bank->saved_risingdetect;
1480 gen1 &= ~(bank->saved_datain);
1481
1482 /* FIXME: Consider GPIO IRQs with level detections properly! */
1483 gen = l & (~(bank->saved_fallingdetect) &
1484 ~(bank->saved_risingdetect));
1485 /* Consider all GPIO IRQs needed to be updated */
1486 gen |= gen0 | gen1;
1487
1488 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001489 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001490
Sergio Aguirref00d6492010-03-03 16:21:08 +00001491 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001492 old0 = __raw_readl(bank->base +
1493 OMAP24XX_GPIO_LEVELDETECT0);
1494 old1 = __raw_readl(bank->base +
1495 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001496 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001497 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001498 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001499 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001500 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001501 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001502 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001503 OMAP24XX_GPIO_LEVELDETECT1);
1504 }
1505
1506 if (cpu_is_omap44xx()) {
1507 old0 = __raw_readl(bank->base +
1508 OMAP4_GPIO_LEVELDETECT0);
1509 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301510 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001511 __raw_writel(old0 | l, bank->base +
1512 OMAP4_GPIO_LEVELDETECT0);
1513 __raw_writel(old1 | l, bank->base +
1514 OMAP4_GPIO_LEVELDETECT1);
1515 __raw_writel(old0, bank->base +
1516 OMAP4_GPIO_LEVELDETECT0);
1517 __raw_writel(old1, bank->base +
1518 OMAP4_GPIO_LEVELDETECT1);
1519 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001520 }
1521 }
1522
1523}
1524
Tony Lindgren92105bb2005-09-07 17:20:26 +01001525#endif
1526
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001527#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301528/* save the registers of bank 2-6 */
1529void omap_gpio_save_context(void)
1530{
1531 int i;
1532
1533 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1534 for (i = 1; i < gpio_bank_count; i++) {
1535 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301536 gpio_context[i].irqenable1 =
1537 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1538 gpio_context[i].irqenable2 =
1539 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1540 gpio_context[i].wake_en =
1541 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1542 gpio_context[i].ctrl =
1543 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1544 gpio_context[i].oe =
1545 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1546 gpio_context[i].leveldetect0 =
1547 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1548 gpio_context[i].leveldetect1 =
1549 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1550 gpio_context[i].risingdetect =
1551 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1552 gpio_context[i].fallingdetect =
1553 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1554 gpio_context[i].dataout =
1555 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301556 }
1557}
1558
1559/* restore the required registers of bank 2-6 */
1560void omap_gpio_restore_context(void)
1561{
1562 int i;
1563
1564 for (i = 1; i < gpio_bank_count; i++) {
1565 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301566 __raw_writel(gpio_context[i].irqenable1,
1567 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1568 __raw_writel(gpio_context[i].irqenable2,
1569 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1570 __raw_writel(gpio_context[i].wake_en,
1571 bank->base + OMAP24XX_GPIO_WAKE_EN);
1572 __raw_writel(gpio_context[i].ctrl,
1573 bank->base + OMAP24XX_GPIO_CTRL);
1574 __raw_writel(gpio_context[i].oe,
1575 bank->base + OMAP24XX_GPIO_OE);
1576 __raw_writel(gpio_context[i].leveldetect0,
1577 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1578 __raw_writel(gpio_context[i].leveldetect1,
1579 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1580 __raw_writel(gpio_context[i].risingdetect,
1581 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1582 __raw_writel(gpio_context[i].fallingdetect,
1583 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1584 __raw_writel(gpio_context[i].dataout,
1585 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301586 }
1587}
1588#endif
1589
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001590static struct platform_driver omap_gpio_driver = {
1591 .probe = omap_gpio_probe,
1592 .driver = {
1593 .name = "omap_gpio",
1594 },
1595};
1596
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001597/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001598 * gpio driver register needs to be done before
1599 * machine_init functions access gpio APIs.
1600 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001601 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001602static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001603{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001604 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001605}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001606postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001607
Tony Lindgren92105bb2005-09-07 17:20:26 +01001608static int __init omap_gpio_sysinit(void)
1609{
David Brownell11a78b72006-12-06 17:14:11 -08001610 mpuio_init();
1611
Tony Lindgren140455f2010-02-12 12:26:48 -08001612#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001613 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1614 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001615#endif
1616
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001617 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001618}
1619
Tony Lindgren92105bb2005-09-07 17:20:26 +01001620arch_initcall(omap_gpio_sysinit);