blob: dbe0cf36e909ca5f8124f6402b9639d7217c6462 [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 Wang699117a2009-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 Wang699117a2009-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 Lindgren3f1686a2010-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{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700528 u32 gpio_bit = GPIO_BIT(bank, gpio);
529 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800530
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700531 if (bank->non_wakeup_gpios & gpio_bit) {
532 dev_err(bank->dev,
533 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100534 return -EINVAL;
535 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700536
537 spin_lock_irqsave(&bank->lock, flags);
538 if (enable)
539 bank->suspend_wakeup |= gpio_bit;
540 else
541 bank->suspend_wakeup &= ~gpio_bit;
542
543 spin_unlock_irqrestore(&bank->lock, flags);
544
545 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100546}
547
Tony Lindgren4196dd62006-09-25 12:41:38 +0300548static void _reset_gpio(struct gpio_bank *bank, int gpio)
549{
Kevin Hilman129fd222011-04-22 07:59:07 -0700550 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300551 _set_gpio_irqenable(bank, gpio, 0);
552 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700553 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300554}
555
Tony Lindgren92105bb2005-09-07 17:20:26 +0100556/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100557static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100558{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100559 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100560 struct gpio_bank *bank;
561 int retval;
562
Lennert Buytenheke9191022010-11-29 11:17:17 +0100563 bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700564 retval = _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100565
566 return retval;
567}
568
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800569static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100570{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800571 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800572 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573
David Brownella6472532008-03-03 04:33:30 -0800574 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100575
Tony Lindgren4196dd62006-09-25 12:41:38 +0300576 /* Set trigger to none. You need to enable the desired trigger with
577 * request_irq() or set_irq_type().
578 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800579 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100580
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000581#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100582 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100583 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100584
Tony Lindgren92105bb2005-09-07 17:20:26 +0100585 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100586 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800587 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100588 }
589#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800590 if (!cpu_class_is_omap1()) {
591 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700592 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800593 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700594
595 if (cpu_is_omap24xx() || cpu_is_omap34xx())
596 reg += OMAP24XX_GPIO_CTRL;
597 else if (cpu_is_omap44xx())
598 reg += OMAP4_GPIO_CTRL;
599 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800600 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700601 ctrl &= 0xFFFFFFFE;
602 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800603 }
604 bank->mod_usage |= 1 << offset;
605 }
David Brownella6472532008-03-03 04:33:30 -0800606 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100607
608 return 0;
609}
610
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800611static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100612{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800613 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800614 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100615
David Brownella6472532008-03-03 04:33:30 -0800616 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100617#ifdef CONFIG_ARCH_OMAP16XX
618 if (bank->method == METHOD_GPIO_1610) {
619 /* Disable wake-up during idle for dynamic tick */
620 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800621 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100622 }
623#endif
Charulatha V9f096862010-05-14 12:05:27 -0700624#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
625 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100626 /* Disable wake-up during idle for dynamic tick */
627 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800628 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100629 }
630#endif
Charulatha V9f096862010-05-14 12:05:27 -0700631#ifdef CONFIG_ARCH_OMAP4
632 if (bank->method == METHOD_GPIO_44XX) {
633 /* Disable wake-up during idle for dynamic tick */
634 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
635 __raw_writel(1 << offset, reg);
636 }
637#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800638 if (!cpu_class_is_omap1()) {
639 bank->mod_usage &= ~(1 << offset);
640 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700641 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800642 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700643
644 if (cpu_is_omap24xx() || cpu_is_omap34xx())
645 reg += OMAP24XX_GPIO_CTRL;
646 else if (cpu_is_omap44xx())
647 reg += OMAP4_GPIO_CTRL;
648 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800649 /* Module is disabled, clocks are gated */
650 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700651 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800652 }
653 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800654 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800655 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100656}
657
658/*
659 * We need to unmask the GPIO bank interrupt as soon as possible to
660 * avoid missing GPIO interrupts for other lines in the bank.
661 * Then we need to mask-read-clear-unmask the triggered GPIO lines
662 * in the bank to avoid missing nested interrupts for a GPIO line.
663 * If we wait to unmask individual GPIO lines in the bank after the
664 * line's interrupt handler has been run, we may miss some nested
665 * interrupts.
666 */
Russell King10dd5ce2006-11-23 11:41:32 +0000667static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100668{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100669 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800671 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100672 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700673 u32 retrigger = 0;
674 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000675 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100676
Will Deaconee144182011-02-21 13:46:08 +0000677 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100678
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100679 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700680 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800681
682 if (WARN_ON(!isr_reg))
683 goto exit;
684
Tony Lindgren92105bb2005-09-07 17:20:26 +0100685 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100686 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700687 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100688
Imre Deakea6dedd2006-06-26 16:16:00 -0700689 enabled = _get_gpio_irqbank_mask(bank);
690 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100691
692 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
693 isr &= 0x0000ffff;
694
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800695 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800696 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -0700697 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100698
699 /* clear edge sensitive interrupts before handler(s) are
700 called so that we don't miss any interrupt occurred while
701 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700702 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100703 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700704 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100705
706 /* if there is only edge sensitive GPIO pin interrupts
707 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700708 if (!level_mask && !unmasked) {
709 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000710 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700711 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100712
Imre Deakea6dedd2006-06-26 16:16:00 -0700713 isr |= retrigger;
714 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100715 if (!isr)
716 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100717
Tony Lindgren92105bb2005-09-07 17:20:26 +0100718 gpio_irq = bank->virtual_irq_start;
719 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700720 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800721
Tony Lindgren92105bb2005-09-07 17:20:26 +0100722 if (!(isr & 1))
723 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200724
Cory Maccarrone4318f362010-01-08 10:29:04 -0800725#ifdef CONFIG_ARCH_OMAP1
726 /*
727 * Some chips can't respond to both rising and falling
728 * at the same time. If this irq was requested with
729 * both flags, we need to flip the ICR data for the IRQ
730 * to respond to the IRQ for the opposite direction.
731 * This will be indicated in the bank toggle_mask.
732 */
733 if (bank->toggle_mask & (1 << gpio_index))
734 _toggle_gpio_edge_triggering(bank, gpio_index);
735#endif
736
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100737 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100738 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000739 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700740 /* if bank has any level sensitive GPIO pin interrupt
741 configured, we must unmask the bank interrupt only after
742 handler(s) are executed in order to avoid spurious bank
743 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800744exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700745 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000746 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100747}
748
Lennert Buytenheke9191022010-11-29 11:17:17 +0100749static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300750{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100751 unsigned int gpio = d->irq - IH_GPIO_BASE;
752 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700753 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300754
Colin Cross85ec7b92011-06-06 13:38:18 -0700755 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300756 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700757 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300758}
759
Lennert Buytenheke9191022010-11-29 11:17:17 +0100760static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100761{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100762 unsigned int gpio = d->irq - IH_GPIO_BASE;
763 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100764
765 _clear_gpio_irqstatus(bank, gpio);
766}
767
Lennert Buytenheke9191022010-11-29 11:17:17 +0100768static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100769{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100770 unsigned int gpio = d->irq - IH_GPIO_BASE;
771 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700772 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100773
Colin Cross85ec7b92011-06-06 13:38:18 -0700774 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100775 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700776 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700777 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100778}
779
Lennert Buytenheke9191022010-11-29 11:17:17 +0100780static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100781{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100782 unsigned int gpio = d->irq - IH_GPIO_BASE;
783 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700784 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100785 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700786 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700787
Colin Cross85ec7b92011-06-06 13:38:18 -0700788 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700789 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700790 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800791
792 /* For level-triggered GPIOs, the clearing must be done after
793 * the HW source is cleared, thus after the handler has run */
794 if (bank->level_mask & irq_mask) {
795 _set_gpio_irqenable(bank, gpio, 0);
796 _clear_gpio_irqstatus(bank, gpio);
797 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100798
Kevin Hilman4de8c752008-01-16 21:56:14 -0800799 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700800 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100801}
802
David Brownelle5c56ed2006-12-06 17:13:59 -0800803static struct irq_chip gpio_irq_chip = {
804 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100805 .irq_shutdown = gpio_irq_shutdown,
806 .irq_ack = gpio_ack_irq,
807 .irq_mask = gpio_mask_irq,
808 .irq_unmask = gpio_unmask_irq,
809 .irq_set_type = gpio_irq_type,
810 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800811};
812
813/*---------------------------------------------------------------------*/
814
815#ifdef CONFIG_ARCH_OMAP1
816
David Brownelle5c56ed2006-12-06 17:13:59 -0800817#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
818
David Brownell11a78b72006-12-06 17:14:11 -0800819#ifdef CONFIG_ARCH_OMAP16XX
820
821#include <linux/platform_device.h>
822
Magnus Damm79ee0312009-07-08 13:22:04 +0200823static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800824{
Magnus Damm79ee0312009-07-08 13:22:04 +0200825 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800826 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800827 void __iomem *mask_reg = bank->base +
828 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800829 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800830
David Brownella6472532008-03-03 04:33:30 -0800831 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800832 bank->saved_wakeup = __raw_readl(mask_reg);
833 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800834 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800835
836 return 0;
837}
838
Magnus Damm79ee0312009-07-08 13:22:04 +0200839static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800840{
Magnus Damm79ee0312009-07-08 13:22:04 +0200841 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800842 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800843 void __iomem *mask_reg = bank->base +
844 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800845 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800846
David Brownella6472532008-03-03 04:33:30 -0800847 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800848 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800849 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800850
851 return 0;
852}
853
Alexey Dobriyan47145212009-12-14 18:00:08 -0800854static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200855 .suspend_noirq = omap_mpuio_suspend_noirq,
856 .resume_noirq = omap_mpuio_resume_noirq,
857};
858
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200859/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800860static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800861 .driver = {
862 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200863 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800864 },
865};
866
867static struct platform_device omap_mpuio_device = {
868 .name = "mpuio",
869 .id = -1,
870 .dev = {
871 .driver = &omap_mpuio_driver.driver,
872 }
873 /* could list the /proc/iomem resources */
874};
875
876static inline void mpuio_init(void)
877{
Charulatha Va8be8da2011-04-22 16:38:16 +0530878 struct gpio_bank *bank = &gpio_bank[0];
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800879 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700880
David Brownell11a78b72006-12-06 17:14:11 -0800881 if (platform_driver_register(&omap_mpuio_driver) == 0)
882 (void) platform_device_register(&omap_mpuio_device);
883}
884
885#else
886static inline void mpuio_init(void) {}
887#endif /* 16xx */
888
David Brownelle5c56ed2006-12-06 17:13:59 -0800889#else
890
David Brownelle5c56ed2006-12-06 17:13:59 -0800891#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -0800892static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -0800893
894#endif
895
896/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100897
David Brownell52e31342008-03-03 12:43:23 -0800898/* REVISIT these are stupid implementations! replace by ones that
899 * don't switch on METHOD_* and which mostly avoid spinlocks
900 */
901
902static int gpio_input(struct gpio_chip *chip, unsigned offset)
903{
904 struct gpio_bank *bank;
905 unsigned long flags;
906
907 bank = container_of(chip, struct gpio_bank, chip);
908 spin_lock_irqsave(&bank->lock, flags);
909 _set_gpio_direction(bank, offset, 1);
910 spin_unlock_irqrestore(&bank->lock, flags);
911 return 0;
912}
913
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300914static int gpio_is_input(struct gpio_bank *bank, int mask)
915{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700916 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300917
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300918 return __raw_readl(reg) & mask;
919}
920
David Brownell52e31342008-03-03 12:43:23 -0800921static int gpio_get(struct gpio_chip *chip, unsigned offset)
922{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300923 struct gpio_bank *bank;
924 void __iomem *reg;
925 int gpio;
926 u32 mask;
927
928 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530929 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300930 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700931 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300932
933 if (gpio_is_input(bank, mask))
934 return _get_gpio_datain(bank, gpio);
935 else
936 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800937}
938
939static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
940{
941 struct gpio_bank *bank;
942 unsigned long flags;
943
944 bank = container_of(chip, struct gpio_bank, chip);
945 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700946 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800947 _set_gpio_direction(bank, offset, 0);
948 spin_unlock_irqrestore(&bank->lock, flags);
949 return 0;
950}
951
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700952static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
953 unsigned debounce)
954{
955 struct gpio_bank *bank;
956 unsigned long flags;
957
958 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800959
960 if (!bank->dbck) {
961 bank->dbck = clk_get(bank->dev, "dbclk");
962 if (IS_ERR(bank->dbck))
963 dev_err(bank->dev, "Could not get gpio dbck\n");
964 }
965
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700966 spin_lock_irqsave(&bank->lock, flags);
967 _set_gpio_debounce(bank, offset, debounce);
968 spin_unlock_irqrestore(&bank->lock, flags);
969
970 return 0;
971}
972
David Brownell52e31342008-03-03 12:43:23 -0800973static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
974{
975 struct gpio_bank *bank;
976 unsigned long flags;
977
978 bank = container_of(chip, struct gpio_bank, chip);
979 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700980 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800981 spin_unlock_irqrestore(&bank->lock, flags);
982}
983
David Brownella007b702008-12-10 17:35:25 -0800984static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
985{
986 struct gpio_bank *bank;
987
988 bank = container_of(chip, struct gpio_bank, chip);
989 return bank->virtual_irq_start + offset;
990}
991
David Brownell52e31342008-03-03 12:43:23 -0800992/*---------------------------------------------------------------------*/
993
Tony Lindgren9a748052010-12-07 16:26:56 -0800994static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700995{
996 u32 rev;
997
Tony Lindgren9a748052010-12-07 16:26:56 -0800998 if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
999 rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001000 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001001 rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001002 else if (cpu_is_omap44xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001003 rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001004 else
1005 return;
1006
1007 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1008 (rev >> 4) & 0x0f, rev & 0x0f);
1009}
1010
David Brownell8ba55c52008-02-26 11:10:50 -08001011/* This lock class tells lockdep that GPIO irqs are in a different
1012 * category than their parents, so it won't report false recursion.
1013 */
1014static struct lock_class_key gpio_lock_class;
1015
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001016static inline int init_gpio_info(struct platform_device *pdev)
1017{
1018 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1019 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1020 GFP_KERNEL);
1021 if (!gpio_bank) {
1022 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1023 return -ENOMEM;
1024 }
1025 return 0;
1026}
1027
1028/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001029static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1030{
1031 if (cpu_class_is_omap2()) {
1032 if (cpu_is_omap44xx()) {
1033 __raw_writel(0xffffffff, bank->base +
1034 OMAP4_GPIO_IRQSTATUSCLR0);
1035 __raw_writel(0x00000000, bank->base +
1036 OMAP4_GPIO_DEBOUNCENABLE);
1037 /* Initialize interface clk ungated, module enabled */
1038 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1039 } else if (cpu_is_omap34xx()) {
1040 __raw_writel(0x00000000, bank->base +
1041 OMAP24XX_GPIO_IRQENABLE1);
1042 __raw_writel(0xffffffff, bank->base +
1043 OMAP24XX_GPIO_IRQSTATUS1);
1044 __raw_writel(0x00000000, bank->base +
1045 OMAP24XX_GPIO_DEBOUNCE_EN);
1046
1047 /* Initialize interface clk ungated, module enabled */
1048 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1049 } else if (cpu_is_omap24xx()) {
1050 static const u32 non_wakeup_gpios[] = {
1051 0xe203ffc0, 0x08700040
1052 };
1053 if (id < ARRAY_SIZE(non_wakeup_gpios))
1054 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1055 }
1056 } else if (cpu_class_is_omap1()) {
1057 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001058 __raw_writew(0xffff, bank->base +
1059 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001060 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1061 __raw_writew(0xffff, bank->base
1062 + OMAP1510_GPIO_INT_MASK);
1063 __raw_writew(0x0000, bank->base
1064 + OMAP1510_GPIO_INT_STATUS);
1065 }
1066 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1067 __raw_writew(0x0000, bank->base
1068 + OMAP1610_GPIO_IRQENABLE1);
1069 __raw_writew(0xffff, bank->base
1070 + OMAP1610_GPIO_IRQSTATUS1);
1071 __raw_writew(0x0014, bank->base
1072 + OMAP1610_GPIO_SYSCONFIG);
1073
1074 /*
1075 * Enable system clock for GPIO module.
1076 * The CAM_CLK_CTRL *is* really the right place.
1077 */
1078 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1079 ULPD_CAM_CLK_CTRL);
1080 }
1081 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1082 __raw_writel(0xffffffff, bank->base
1083 + OMAP7XX_GPIO_INT_MASK);
1084 __raw_writel(0x00000000, bank->base
1085 + OMAP7XX_GPIO_INT_STATUS);
1086 }
1087 }
1088}
1089
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001090static __init void
1091omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1092 unsigned int num)
1093{
1094 struct irq_chip_generic *gc;
1095 struct irq_chip_type *ct;
1096
1097 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1098 handle_simple_irq);
1099 ct = gc->chip_types;
1100
1101 /* NOTE: No ack required, reading IRQ status clears it. */
1102 ct->chip.irq_mask = irq_gc_mask_set_bit;
1103 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1104 ct->chip.irq_set_type = gpio_irq_type;
1105 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1106 if (cpu_is_omap16xx())
1107 ct->chip.irq_set_wake = gpio_wake_enable,
1108
1109 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1110 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1111 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1112}
1113
Russell Kingd52b31d2011-05-27 13:56:12 -07001114static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001115{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001116 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001117 static int gpio;
1118
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001119 bank->mod_usage = 0;
1120 /*
1121 * REVISIT eventually switch from OMAP-specific gpio structs
1122 * over to the generic ones
1123 */
1124 bank->chip.request = omap_gpio_request;
1125 bank->chip.free = omap_gpio_free;
1126 bank->chip.direction_input = gpio_input;
1127 bank->chip.get = gpio_get;
1128 bank->chip.direction_output = gpio_output;
1129 bank->chip.set_debounce = gpio_debounce;
1130 bank->chip.set = gpio_set;
1131 bank->chip.to_irq = gpio_2irq;
1132 if (bank_is_mpuio(bank)) {
1133 bank->chip.label = "mpuio";
1134#ifdef CONFIG_ARCH_OMAP16XX
1135 bank->chip.dev = &omap_mpuio_device.dev;
1136#endif
1137 bank->chip.base = OMAP_MPUIO(0);
1138 } else {
1139 bank->chip.label = "gpio";
1140 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001141 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001142 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001143 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001144
1145 gpiochip_add(&bank->chip);
1146
1147 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001148 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001149 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001150 irq_set_chip_data(j, bank);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001151 if (bank_is_mpuio(bank)) {
1152 omap_mpuio_alloc_gc(bank, j, bank->width);
1153 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001154 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001155 irq_set_handler(j, handle_simple_irq);
1156 set_irq_flags(j, IRQF_VALID);
1157 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001158 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001159 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1160 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001161}
1162
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001163static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001164{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001165 static int gpio_init_done;
1166 struct omap_gpio_platform_data *pdata;
1167 struct resource *res;
1168 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001169 struct gpio_bank *bank;
1170
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001171 if (!pdev->dev.platform_data)
1172 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001173
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001174 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001175
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001176 if (!gpio_init_done) {
1177 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001178
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001179 ret = init_gpio_info(pdev);
1180 if (ret)
1181 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001182 }
1183
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001184 id = pdev->id;
1185 bank = &gpio_bank[id];
1186
1187 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1188 if (unlikely(!res)) {
1189 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1190 return -ENODEV;
1191 }
1192
1193 bank->irq = res->start;
1194 bank->virtual_irq_start = pdata->virtual_irq_start;
1195 bank->method = pdata->bank_type;
1196 bank->dev = &pdev->dev;
1197 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001198 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001199 bank->width = pdata->bank_width;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001200
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001201 bank->regs = pdata->regs;
1202
1203 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1204 bank->set_dataout = _set_gpio_dataout_reg;
1205 else
1206 bank->set_dataout = _set_gpio_dataout_mask;
1207
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001208 spin_lock_init(&bank->lock);
1209
1210 /* Static mapping, never released */
1211 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1212 if (unlikely(!res)) {
1213 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1214 return -ENODEV;
1215 }
1216
1217 bank->base = ioremap(res->start, resource_size(res));
1218 if (!bank->base) {
1219 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1220 return -ENOMEM;
1221 }
1222
1223 pm_runtime_enable(bank->dev);
1224 pm_runtime_get_sync(bank->dev);
1225
1226 omap_gpio_mod_init(bank, id);
1227 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001228 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001229
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001230 if (!gpio_init_done)
1231 gpio_init_done = 1;
1232
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001233 return 0;
1234}
1235
Tony Lindgren140455f2010-02-12 12:26:48 -08001236#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001237static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001238{
1239 int i;
1240
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001241 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001242 return 0;
1243
1244 for (i = 0; i < gpio_bank_count; i++) {
1245 struct gpio_bank *bank = &gpio_bank[i];
1246 void __iomem *wake_status;
1247 void __iomem *wake_clear;
1248 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001249 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001250
1251 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001252#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001253 case METHOD_GPIO_1610:
1254 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1255 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1256 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1257 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001258#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001259#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001260 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001261 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001262 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1263 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1264 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001265#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301266#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001267 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301268 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1269 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1270 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1271 break;
1272#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001273 default:
1274 continue;
1275 }
1276
David Brownella6472532008-03-03 04:33:30 -08001277 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001278 bank->saved_wakeup = __raw_readl(wake_status);
1279 __raw_writel(0xffffffff, wake_clear);
1280 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001281 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001282 }
1283
1284 return 0;
1285}
1286
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001287static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001288{
1289 int i;
1290
Tero Kristo723fdb72008-11-26 14:35:16 -08001291 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001292 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001293
1294 for (i = 0; i < gpio_bank_count; i++) {
1295 struct gpio_bank *bank = &gpio_bank[i];
1296 void __iomem *wake_clear;
1297 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001298 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001299
1300 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001301#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001302 case METHOD_GPIO_1610:
1303 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1304 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1305 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001306#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001307#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001308 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001309 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1310 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001311 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001312#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301313#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001314 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301315 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1316 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1317 break;
1318#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001319 default:
1320 continue;
1321 }
1322
David Brownella6472532008-03-03 04:33:30 -08001323 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001324 __raw_writel(0xffffffff, wake_clear);
1325 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001326 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001327 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001328}
1329
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001330static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001331 .suspend = omap_gpio_suspend,
1332 .resume = omap_gpio_resume,
1333};
1334
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001335#endif
1336
Tony Lindgren140455f2010-02-12 12:26:48 -08001337#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001338
1339static int workaround_enabled;
1340
Paul Walmsley72e06d02010-12-21 21:05:16 -07001341void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001342{
1343 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001344 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001345
Tero Kristoa118b5f2008-12-22 14:27:12 +02001346 if (cpu_is_omap34xx())
1347 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001348
Tero Kristoa118b5f2008-12-22 14:27:12 +02001349 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001350 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001351 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001352 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001353
Kevin Hilman0aed04352010-09-22 16:06:27 -07001354 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001355 clk_disable(bank->dbck);
1356
Paul Walmsley72e06d02010-12-21 21:05:16 -07001357 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001358 continue;
1359
1360 /* If going to OFF, remove triggering for all
1361 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1362 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001363 if (!(bank->enabled_non_wakeup_gpios))
1364 continue;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001365
1366 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1367 bank->saved_datain = __raw_readl(bank->base +
1368 OMAP24XX_GPIO_DATAIN);
1369 l1 = __raw_readl(bank->base +
1370 OMAP24XX_GPIO_FALLINGDETECT);
1371 l2 = __raw_readl(bank->base +
1372 OMAP24XX_GPIO_RISINGDETECT);
1373 }
1374
1375 if (cpu_is_omap44xx()) {
1376 bank->saved_datain = __raw_readl(bank->base +
1377 OMAP4_GPIO_DATAIN);
1378 l1 = __raw_readl(bank->base +
1379 OMAP4_GPIO_FALLINGDETECT);
1380 l2 = __raw_readl(bank->base +
1381 OMAP4_GPIO_RISINGDETECT);
1382 }
1383
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001384 bank->saved_fallingdetect = l1;
1385 bank->saved_risingdetect = l2;
1386 l1 &= ~bank->enabled_non_wakeup_gpios;
1387 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001388
1389 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1390 __raw_writel(l1, bank->base +
1391 OMAP24XX_GPIO_FALLINGDETECT);
1392 __raw_writel(l2, bank->base +
1393 OMAP24XX_GPIO_RISINGDETECT);
1394 }
1395
1396 if (cpu_is_omap44xx()) {
1397 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1398 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1399 }
1400
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001401 c++;
1402 }
1403 if (!c) {
1404 workaround_enabled = 0;
1405 return;
1406 }
1407 workaround_enabled = 1;
1408}
1409
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001410void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001411{
1412 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001413 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001414
Tero Kristoa118b5f2008-12-22 14:27:12 +02001415 if (cpu_is_omap34xx())
1416 min = 1;
1417 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001418 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001419 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001420 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001421
Kevin Hilman0aed04352010-09-22 16:06:27 -07001422 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001423 clk_enable(bank->dbck);
1424
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001425 if (!workaround_enabled)
1426 continue;
1427
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001428 if (!(bank->enabled_non_wakeup_gpios))
1429 continue;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001430
1431 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1432 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001433 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001434 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001435 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001436 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1437 }
1438
1439 if (cpu_is_omap44xx()) {
1440 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301441 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001442 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301443 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001444 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1445 }
1446
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001447 /* Check if any of the non-wakeup interrupt GPIOs have changed
1448 * state. If so, generate an IRQ by software. This is
1449 * horribly racy, but it's the best we can do to work around
1450 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001451 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001452 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001453
1454 /*
1455 * No need to generate IRQs for the rising edge for gpio IRQs
1456 * configured with falling edge only; and vice versa.
1457 */
1458 gen0 = l & bank->saved_fallingdetect;
1459 gen0 &= bank->saved_datain;
1460
1461 gen1 = l & bank->saved_risingdetect;
1462 gen1 &= ~(bank->saved_datain);
1463
1464 /* FIXME: Consider GPIO IRQs with level detections properly! */
1465 gen = l & (~(bank->saved_fallingdetect) &
1466 ~(bank->saved_risingdetect));
1467 /* Consider all GPIO IRQs needed to be updated */
1468 gen |= gen0 | gen1;
1469
1470 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001471 u32 old0, old1;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001472
Sergio Aguirref00d6492010-03-03 16:21:08 +00001473 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001474 old0 = __raw_readl(bank->base +
1475 OMAP24XX_GPIO_LEVELDETECT0);
1476 old1 = __raw_readl(bank->base +
1477 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001478 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001479 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001480 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001481 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001482 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001483 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001484 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001485 OMAP24XX_GPIO_LEVELDETECT1);
1486 }
1487
1488 if (cpu_is_omap44xx()) {
1489 old0 = __raw_readl(bank->base +
1490 OMAP4_GPIO_LEVELDETECT0);
1491 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301492 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001493 __raw_writel(old0 | l, bank->base +
1494 OMAP4_GPIO_LEVELDETECT0);
1495 __raw_writel(old1 | l, bank->base +
1496 OMAP4_GPIO_LEVELDETECT1);
1497 __raw_writel(old0, bank->base +
1498 OMAP4_GPIO_LEVELDETECT0);
1499 __raw_writel(old1, bank->base +
1500 OMAP4_GPIO_LEVELDETECT1);
1501 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001502 }
1503 }
1504
1505}
1506
Tony Lindgren92105bb2005-09-07 17:20:26 +01001507#endif
1508
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001509#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301510/* save the registers of bank 2-6 */
1511void omap_gpio_save_context(void)
1512{
1513 int i;
1514
1515 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1516 for (i = 1; i < gpio_bank_count; i++) {
1517 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301518 gpio_context[i].irqenable1 =
1519 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1520 gpio_context[i].irqenable2 =
1521 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1522 gpio_context[i].wake_en =
1523 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1524 gpio_context[i].ctrl =
1525 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1526 gpio_context[i].oe =
1527 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1528 gpio_context[i].leveldetect0 =
1529 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1530 gpio_context[i].leveldetect1 =
1531 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1532 gpio_context[i].risingdetect =
1533 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1534 gpio_context[i].fallingdetect =
1535 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1536 gpio_context[i].dataout =
1537 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301538 }
1539}
1540
1541/* restore the required registers of bank 2-6 */
1542void omap_gpio_restore_context(void)
1543{
1544 int i;
1545
1546 for (i = 1; i < gpio_bank_count; i++) {
1547 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301548 __raw_writel(gpio_context[i].irqenable1,
1549 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1550 __raw_writel(gpio_context[i].irqenable2,
1551 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1552 __raw_writel(gpio_context[i].wake_en,
1553 bank->base + OMAP24XX_GPIO_WAKE_EN);
1554 __raw_writel(gpio_context[i].ctrl,
1555 bank->base + OMAP24XX_GPIO_CTRL);
1556 __raw_writel(gpio_context[i].oe,
1557 bank->base + OMAP24XX_GPIO_OE);
1558 __raw_writel(gpio_context[i].leveldetect0,
1559 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1560 __raw_writel(gpio_context[i].leveldetect1,
1561 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1562 __raw_writel(gpio_context[i].risingdetect,
1563 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1564 __raw_writel(gpio_context[i].fallingdetect,
1565 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1566 __raw_writel(gpio_context[i].dataout,
1567 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301568 }
1569}
1570#endif
1571
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001572static struct platform_driver omap_gpio_driver = {
1573 .probe = omap_gpio_probe,
1574 .driver = {
1575 .name = "omap_gpio",
1576 },
1577};
1578
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001579/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001580 * gpio driver register needs to be done before
1581 * machine_init functions access gpio APIs.
1582 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001583 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001584static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001585{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001586 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001587}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001588postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001589
Tony Lindgren92105bb2005-09-07 17:20:26 +01001590static int __init omap_gpio_sysinit(void)
1591{
David Brownell11a78b72006-12-06 17:14:11 -08001592 mpuio_init();
1593
Tony Lindgren140455f2010-02-12 12:26:48 -08001594#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001595 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1596 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001597#endif
1598
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001599 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001600}
1601
Tony Lindgren92105bb2005-09-07 17:20:26 +01001602arch_initcall(omap_gpio_sysinit);