blob: 9c27244fd680ecf06e9e3309e983a8810c83baae [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080022#include <linux/slab.h>
23#include <linux/pm_runtime.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/irqs.h>
Russell King1bc857f2011-07-26 10:54:55 +010028#include <asm/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029#include <asm/mach/irq.h>
30
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031struct gpio_bank {
Tony Lindgren9f7065d2009-10-19 15:25:20 -070032 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010033 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010034 u16 irq;
35 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010036 int method;
Tony 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))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010092
93static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
94{
Tony Lindgren92105bb2005-09-07 17:20:26 +010095 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010096 u32 l;
97
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070098 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099 l = __raw_readl(reg);
100 if (is_input)
101 l |= 1 << gpio;
102 else
103 l &= ~(1 << gpio);
104 __raw_writel(l, reg);
105}
106
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700107
108/* set data out value using dedicate set/clear register */
109static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100110{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100111 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700112 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100113
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700114 if (enable)
115 reg += bank->regs->set_dataout;
116 else
117 reg += bank->regs->clr_dataout;
118
119 __raw_writel(l, reg);
120}
121
122/* set data out value using mask register */
123static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
124{
125 void __iomem *reg = bank->base + bank->regs->dataout;
126 u32 gpio_bit = GPIO_BIT(bank, gpio);
127 u32 l;
128
129 l = __raw_readl(reg);
130 if (enable)
131 l |= gpio_bit;
132 else
133 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134 __raw_writel(l, reg);
135}
136
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300137static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100138{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700139 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100140
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700141 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100142}
143
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300144static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
145{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700146 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300147
Kevin Hilman129fd222011-04-22 07:59:07 -0700148 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300149}
150
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{
Kevin Hilman9942da02011-04-22 12:02:05 -0700171 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700172 u32 val;
173 u32 l;
174
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800175 if (!bank->dbck_flag)
176 return;
177
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700178 if (debounce < 32)
179 debounce = 0x01;
180 else if (debounce > 7936)
181 debounce = 0xff;
182 else
183 debounce = (debounce / 0x1f) - 1;
184
Kevin Hilman129fd222011-04-22 07:59:07 -0700185 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700186
Kevin Hilman9942da02011-04-22 12:02:05 -0700187 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700188 __raw_writel(debounce, reg);
189
Kevin Hilman9942da02011-04-22 12:02:05 -0700190 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700191 val = __raw_readl(reg);
192
193 if (debounce) {
194 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800195 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700196 } else {
197 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800198 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700199 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300200 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700201
202 __raw_writel(val, reg);
203}
204
Tony Lindgren140455f2010-02-12 12:26:48 -0800205#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700206static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
207 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100208{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800209 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100210 u32 gpio_bit = 1 << gpio;
211
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530212 if (cpu_is_omap44xx()) {
213 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
214 trigger & IRQ_TYPE_LEVEL_LOW);
215 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
216 trigger & IRQ_TYPE_LEVEL_HIGH);
217 MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
218 trigger & IRQ_TYPE_EDGE_RISING);
219 MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
220 trigger & IRQ_TYPE_EDGE_FALLING);
221 } else {
222 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
223 trigger & IRQ_TYPE_LEVEL_LOW);
224 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
225 trigger & IRQ_TYPE_LEVEL_HIGH);
226 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
227 trigger & IRQ_TYPE_EDGE_RISING);
228 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
229 trigger & IRQ_TYPE_EDGE_FALLING);
230 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800231 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530232 if (cpu_is_omap44xx()) {
Colin Cross0622b252011-06-06 13:38:17 -0700233 MOD_REG_BIT(OMAP4_GPIO_IRQWAKEN0, gpio_bit,
234 trigger != 0);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530235 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000236 /*
237 * GPIO wakeup request can only be generated on edge
238 * transitions
239 */
240 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530241 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700242 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530243 else
244 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700245 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530246 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200247 }
Ambresh K55b220c2011-06-15 13:40:45 -0700248 /* This part needs to be executed always for OMAP{34xx, 44xx} */
249 if (cpu_is_omap34xx() || cpu_is_omap44xx() ||
250 (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000251 /*
252 * Log the edge gpio and manually trigger the IRQ
253 * after resume if the input level changes
254 * to avoid irq lost during PER RET/OFF mode
255 * Applies for omap2 non-wakeup gpio and all omap3 gpios
256 */
257 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800258 bank->enabled_non_wakeup_gpios |= gpio_bit;
259 else
260 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
261 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700262
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530263 if (cpu_is_omap44xx()) {
264 bank->level_mask =
265 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
266 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
267 } else {
268 bank->level_mask =
269 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
270 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
271 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100272}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800273#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100274
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800275#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800276/*
277 * This only applies to chips that can't do both rising and falling edge
278 * detection at once. For all other chips, this function is a noop.
279 */
280static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
281{
282 void __iomem *reg = bank->base;
283 u32 l = 0;
284
285 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800286 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800287 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800288 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800289#ifdef CONFIG_ARCH_OMAP15XX
290 case METHOD_GPIO_1510:
291 reg += OMAP1510_GPIO_INT_CONTROL;
292 break;
293#endif
294#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
295 case METHOD_GPIO_7XX:
296 reg += OMAP7XX_GPIO_INT_CONTROL;
297 break;
298#endif
299 default:
300 return;
301 }
302
303 l = __raw_readl(reg);
304 if ((l >> gpio) & 1)
305 l &= ~(1 << gpio);
306 else
307 l |= 1 << gpio;
308
309 __raw_writel(l, reg);
310}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800311#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800312
Tony Lindgren92105bb2005-09-07 17:20:26 +0100313static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
314{
315 void __iomem *reg = bank->base;
316 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100317
318 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800319#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100320 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800321 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100322 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000323 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800324 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100325 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100326 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100327 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100328 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100329 else
330 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100331 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800332#endif
333#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100334 case METHOD_GPIO_1510:
335 reg += OMAP1510_GPIO_INT_CONTROL;
336 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000337 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800338 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100339 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100341 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100342 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100343 else
344 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100345 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800346#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800347#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100349 if (gpio & 0x08)
350 reg += OMAP1610_GPIO_EDGE_CTRL2;
351 else
352 reg += OMAP1610_GPIO_EDGE_CTRL1;
353 gpio &= 0x07;
354 l = __raw_readl(reg);
355 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100356 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100357 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100358 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100359 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800360 if (trigger)
361 /* Enable wake-up during idle for dynamic tick */
362 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
363 else
364 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100365 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800366#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100367#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100368 case METHOD_GPIO_7XX:
369 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700370 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000371 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800372 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700373 if (trigger & IRQ_TYPE_EDGE_RISING)
374 l |= 1 << gpio;
375 else if (trigger & IRQ_TYPE_EDGE_FALLING)
376 l &= ~(1 << gpio);
377 else
378 goto bad;
379 break;
380#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800381#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100382 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800383 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800384 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200385 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800386#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100387 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100388 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100389 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100390 __raw_writel(l, reg);
391 return 0;
392bad:
393 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100394}
395
Lennert Buytenheke9191022010-11-29 11:17:17 +0100396static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100397{
398 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100399 unsigned gpio;
400 int retval;
David Brownella6472532008-03-03 04:33:30 -0800401 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100402
Lennert Buytenheke9191022010-11-29 11:17:17 +0100403 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
404 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100405 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100406 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100407
David Brownelle5c56ed2006-12-06 17:13:59 -0800408 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100409 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800410
411 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800412 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800413 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100414 return -EINVAL;
415
Lennert Buytenheke9191022010-11-29 11:17:17 +0100416 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800417 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700418 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800419 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800420
421 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100422 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800423 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100424 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800425
Tony Lindgren92105bb2005-09-07 17:20:26 +0100426 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100427}
428
429static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
430{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100431 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100432
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700433 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100434 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300435
436 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700437 if (bank->regs->irqstatus2) {
438 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700439 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700440 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700441
442 /* Flush posted write for the irq status to avoid spurious interrupts */
443 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100444}
445
446static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
447{
Kevin Hilman129fd222011-04-22 07:59:07 -0700448 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100449}
450
Imre Deakea6dedd2006-06-26 16:16:00 -0700451static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
452{
453 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700454 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700455 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700456
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700457 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700458 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700459 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700460 l = ~l;
461 l &= mask;
462 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700463}
464
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700465static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100466{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100467 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100468 u32 l;
469
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700470 if (bank->regs->set_irqenable) {
471 reg += bank->regs->set_irqenable;
472 l = gpio_mask;
473 } else {
474 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700476 if (bank->regs->irqenable_inv)
477 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100478 else
479 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100480 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700481
482 __raw_writel(l, reg);
483}
484
485static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
486{
487 void __iomem *reg = bank->base;
488 u32 l;
489
490 if (bank->regs->clr_irqenable) {
491 reg += bank->regs->clr_irqenable;
492 l = gpio_mask;
493 } else {
494 reg += bank->regs->irqenable;
495 l = __raw_readl(reg);
496 if (bank->regs->irqenable_inv)
497 l |= gpio_mask;
498 else
499 l &= ~gpio_mask;
500 }
501
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100502 __raw_writel(l, reg);
503}
504
505static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
506{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700507 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100508}
509
Tony Lindgren92105bb2005-09-07 17:20:26 +0100510/*
511 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
512 * 1510 does not seem to have a wake-up register. If JTAG is connected
513 * to the target, system will wake up always on GPIO events. While
514 * system is running all registered GPIO interrupts need to have wake-up
515 * enabled. When system is suspended, only selected GPIO interrupts need
516 * to have wake-up enabled.
517 */
518static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
519{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700520 u32 gpio_bit = GPIO_BIT(bank, gpio);
521 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800522
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700523 if (bank->non_wakeup_gpios & gpio_bit) {
524 dev_err(bank->dev,
525 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100526 return -EINVAL;
527 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700528
529 spin_lock_irqsave(&bank->lock, flags);
530 if (enable)
531 bank->suspend_wakeup |= gpio_bit;
532 else
533 bank->suspend_wakeup &= ~gpio_bit;
534
535 spin_unlock_irqrestore(&bank->lock, flags);
536
537 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100538}
539
Tony Lindgren4196dd62006-09-25 12:41:38 +0300540static void _reset_gpio(struct gpio_bank *bank, int gpio)
541{
Kevin Hilman129fd222011-04-22 07:59:07 -0700542 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300543 _set_gpio_irqenable(bank, gpio, 0);
544 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700545 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300546}
547
Tony Lindgren92105bb2005-09-07 17:20:26 +0100548/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100549static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100550{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100551 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100552 struct gpio_bank *bank;
553 int retval;
554
Lennert Buytenheke9191022010-11-29 11:17:17 +0100555 bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700556 retval = _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100557
558 return retval;
559}
560
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800561static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100562{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800563 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800564 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100565
David Brownella6472532008-03-03 04:33:30 -0800566 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100567
Tony Lindgren4196dd62006-09-25 12:41:38 +0300568 /* Set trigger to none. You need to enable the desired trigger with
569 * request_irq() or set_irq_type().
570 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800571 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100572
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000573#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100574 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100575 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576
Tony Lindgren92105bb2005-09-07 17:20:26 +0100577 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800579 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100580 }
581#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800582 if (!cpu_class_is_omap1()) {
583 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700584 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800585 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700586
587 if (cpu_is_omap24xx() || cpu_is_omap34xx())
588 reg += OMAP24XX_GPIO_CTRL;
589 else if (cpu_is_omap44xx())
590 reg += OMAP4_GPIO_CTRL;
591 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800592 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700593 ctrl &= 0xFFFFFFFE;
594 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800595 }
596 bank->mod_usage |= 1 << offset;
597 }
David Brownella6472532008-03-03 04:33:30 -0800598 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100599
600 return 0;
601}
602
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800603static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100604{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800605 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800606 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100607
David Brownella6472532008-03-03 04:33:30 -0800608 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100609#ifdef CONFIG_ARCH_OMAP16XX
610 if (bank->method == METHOD_GPIO_1610) {
611 /* Disable wake-up during idle for dynamic tick */
612 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800613 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100614 }
615#endif
Charulatha V9f096862010-05-14 12:05:27 -0700616#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
617 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100618 /* Disable wake-up during idle for dynamic tick */
619 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800620 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100621 }
622#endif
Charulatha V9f096862010-05-14 12:05:27 -0700623#ifdef CONFIG_ARCH_OMAP4
624 if (bank->method == METHOD_GPIO_44XX) {
625 /* Disable wake-up during idle for dynamic tick */
626 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
627 __raw_writel(1 << offset, reg);
628 }
629#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800630 if (!cpu_class_is_omap1()) {
631 bank->mod_usage &= ~(1 << offset);
632 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700633 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800634 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700635
636 if (cpu_is_omap24xx() || cpu_is_omap34xx())
637 reg += OMAP24XX_GPIO_CTRL;
638 else if (cpu_is_omap44xx())
639 reg += OMAP4_GPIO_CTRL;
640 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800641 /* Module is disabled, clocks are gated */
642 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700643 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800644 }
645 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800646 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800647 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100648}
649
650/*
651 * We need to unmask the GPIO bank interrupt as soon as possible to
652 * avoid missing GPIO interrupts for other lines in the bank.
653 * Then we need to mask-read-clear-unmask the triggered GPIO lines
654 * in the bank to avoid missing nested interrupts for a GPIO line.
655 * If we wait to unmask individual GPIO lines in the bank after the
656 * line's interrupt handler has been run, we may miss some nested
657 * interrupts.
658 */
Russell King10dd5ce2006-11-23 11:41:32 +0000659static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100660{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100661 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100662 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800663 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100664 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700665 u32 retrigger = 0;
666 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000667 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100668
Will Deaconee144182011-02-21 13:46:08 +0000669 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100671 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700672 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800673
674 if (WARN_ON(!isr_reg))
675 goto exit;
676
Tony Lindgren92105bb2005-09-07 17:20:26 +0100677 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100678 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700679 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100680
Imre Deakea6dedd2006-06-26 16:16:00 -0700681 enabled = _get_gpio_irqbank_mask(bank);
682 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100683
684 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
685 isr &= 0x0000ffff;
686
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800687 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800688 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -0700689 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100690
691 /* clear edge sensitive interrupts before handler(s) are
692 called so that we don't miss any interrupt occurred while
693 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700694 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100695 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700696 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100697
698 /* if there is only edge sensitive GPIO pin interrupts
699 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700700 if (!level_mask && !unmasked) {
701 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000702 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700703 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100704
Imre Deakea6dedd2006-06-26 16:16:00 -0700705 isr |= retrigger;
706 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100707 if (!isr)
708 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100709
Tony Lindgren92105bb2005-09-07 17:20:26 +0100710 gpio_irq = bank->virtual_irq_start;
711 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700712 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800713
Tony Lindgren92105bb2005-09-07 17:20:26 +0100714 if (!(isr & 1))
715 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200716
Cory Maccarrone4318f362010-01-08 10:29:04 -0800717#ifdef CONFIG_ARCH_OMAP1
718 /*
719 * Some chips can't respond to both rising and falling
720 * at the same time. If this irq was requested with
721 * both flags, we need to flip the ICR data for the IRQ
722 * to respond to the IRQ for the opposite direction.
723 * This will be indicated in the bank toggle_mask.
724 */
725 if (bank->toggle_mask & (1 << gpio_index))
726 _toggle_gpio_edge_triggering(bank, gpio_index);
727#endif
728
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100729 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100730 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000731 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700732 /* if bank has any level sensitive GPIO pin interrupt
733 configured, we must unmask the bank interrupt only after
734 handler(s) are executed in order to avoid spurious bank
735 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800736exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700737 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000738 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100739}
740
Lennert Buytenheke9191022010-11-29 11:17:17 +0100741static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300742{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100743 unsigned int gpio = d->irq - IH_GPIO_BASE;
744 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700745 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300746
Colin Cross85ec7b92011-06-06 13:38:18 -0700747 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300748 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700749 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300750}
751
Lennert Buytenheke9191022010-11-29 11:17:17 +0100752static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100753{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100754 unsigned int gpio = d->irq - IH_GPIO_BASE;
755 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756
757 _clear_gpio_irqstatus(bank, gpio);
758}
759
Lennert Buytenheke9191022010-11-29 11:17:17 +0100760static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100761{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100762 unsigned int gpio = d->irq - IH_GPIO_BASE;
763 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700764 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100765
Colin Cross85ec7b92011-06-06 13:38:18 -0700766 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100767 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700768 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700769 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100770}
771
Lennert Buytenheke9191022010-11-29 11:17:17 +0100772static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100773{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100774 unsigned int gpio = d->irq - IH_GPIO_BASE;
775 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700776 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100777 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700778 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700779
Colin Cross85ec7b92011-06-06 13:38:18 -0700780 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700781 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700782 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800783
784 /* For level-triggered GPIOs, the clearing must be done after
785 * the HW source is cleared, thus after the handler has run */
786 if (bank->level_mask & irq_mask) {
787 _set_gpio_irqenable(bank, gpio, 0);
788 _clear_gpio_irqstatus(bank, gpio);
789 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100790
Kevin Hilman4de8c752008-01-16 21:56:14 -0800791 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700792 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100793}
794
David Brownelle5c56ed2006-12-06 17:13:59 -0800795static struct irq_chip gpio_irq_chip = {
796 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100797 .irq_shutdown = gpio_irq_shutdown,
798 .irq_ack = gpio_ack_irq,
799 .irq_mask = gpio_mask_irq,
800 .irq_unmask = gpio_unmask_irq,
801 .irq_set_type = gpio_irq_type,
802 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800803};
804
805/*---------------------------------------------------------------------*/
806
807#ifdef CONFIG_ARCH_OMAP1
808
David Brownelle5c56ed2006-12-06 17:13:59 -0800809#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
810
David Brownell11a78b72006-12-06 17:14:11 -0800811#ifdef CONFIG_ARCH_OMAP16XX
812
813#include <linux/platform_device.h>
814
Magnus Damm79ee0312009-07-08 13:22:04 +0200815static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800816{
Magnus Damm79ee0312009-07-08 13:22:04 +0200817 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800818 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800819 void __iomem *mask_reg = bank->base +
820 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800821 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800822
David Brownella6472532008-03-03 04:33:30 -0800823 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800824 bank->saved_wakeup = __raw_readl(mask_reg);
825 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800826 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800827
828 return 0;
829}
830
Magnus Damm79ee0312009-07-08 13:22:04 +0200831static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800832{
Magnus Damm79ee0312009-07-08 13:22:04 +0200833 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800834 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800835 void __iomem *mask_reg = bank->base +
836 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800837 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800838
David Brownella6472532008-03-03 04:33:30 -0800839 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800840 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800841 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800842
843 return 0;
844}
845
Alexey Dobriyan47145212009-12-14 18:00:08 -0800846static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200847 .suspend_noirq = omap_mpuio_suspend_noirq,
848 .resume_noirq = omap_mpuio_resume_noirq,
849};
850
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200851/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800852static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800853 .driver = {
854 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200855 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800856 },
857};
858
859static struct platform_device omap_mpuio_device = {
860 .name = "mpuio",
861 .id = -1,
862 .dev = {
863 .driver = &omap_mpuio_driver.driver,
864 }
865 /* could list the /proc/iomem resources */
866};
867
868static inline void mpuio_init(void)
869{
Charulatha Va8be8da2011-04-22 16:38:16 +0530870 struct gpio_bank *bank = &gpio_bank[0];
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800871 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700872
David Brownell11a78b72006-12-06 17:14:11 -0800873 if (platform_driver_register(&omap_mpuio_driver) == 0)
874 (void) platform_device_register(&omap_mpuio_device);
875}
876
877#else
878static inline void mpuio_init(void) {}
879#endif /* 16xx */
880
David Brownelle5c56ed2006-12-06 17:13:59 -0800881#else
882
David Brownelle5c56ed2006-12-06 17:13:59 -0800883#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -0800884static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -0800885
886#endif
887
888/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100889
David Brownell52e31342008-03-03 12:43:23 -0800890/* REVISIT these are stupid implementations! replace by ones that
891 * don't switch on METHOD_* and which mostly avoid spinlocks
892 */
893
894static int gpio_input(struct gpio_chip *chip, unsigned offset)
895{
896 struct gpio_bank *bank;
897 unsigned long flags;
898
899 bank = container_of(chip, struct gpio_bank, chip);
900 spin_lock_irqsave(&bank->lock, flags);
901 _set_gpio_direction(bank, offset, 1);
902 spin_unlock_irqrestore(&bank->lock, flags);
903 return 0;
904}
905
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300906static int gpio_is_input(struct gpio_bank *bank, int mask)
907{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700908 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300909
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300910 return __raw_readl(reg) & mask;
911}
912
David Brownell52e31342008-03-03 12:43:23 -0800913static int gpio_get(struct gpio_chip *chip, unsigned offset)
914{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300915 struct gpio_bank *bank;
916 void __iomem *reg;
917 int gpio;
918 u32 mask;
919
920 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530921 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300922 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700923 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300924
925 if (gpio_is_input(bank, mask))
926 return _get_gpio_datain(bank, gpio);
927 else
928 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800929}
930
931static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
932{
933 struct gpio_bank *bank;
934 unsigned long flags;
935
936 bank = container_of(chip, struct gpio_bank, chip);
937 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700938 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800939 _set_gpio_direction(bank, offset, 0);
940 spin_unlock_irqrestore(&bank->lock, flags);
941 return 0;
942}
943
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700944static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
945 unsigned debounce)
946{
947 struct gpio_bank *bank;
948 unsigned long flags;
949
950 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800951
952 if (!bank->dbck) {
953 bank->dbck = clk_get(bank->dev, "dbclk");
954 if (IS_ERR(bank->dbck))
955 dev_err(bank->dev, "Could not get gpio dbck\n");
956 }
957
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700958 spin_lock_irqsave(&bank->lock, flags);
959 _set_gpio_debounce(bank, offset, debounce);
960 spin_unlock_irqrestore(&bank->lock, flags);
961
962 return 0;
963}
964
David Brownell52e31342008-03-03 12:43:23 -0800965static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
966{
967 struct gpio_bank *bank;
968 unsigned long flags;
969
970 bank = container_of(chip, struct gpio_bank, chip);
971 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700972 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800973 spin_unlock_irqrestore(&bank->lock, flags);
974}
975
David Brownella007b702008-12-10 17:35:25 -0800976static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
977{
978 struct gpio_bank *bank;
979
980 bank = container_of(chip, struct gpio_bank, chip);
981 return bank->virtual_irq_start + offset;
982}
983
David Brownell52e31342008-03-03 12:43:23 -0800984/*---------------------------------------------------------------------*/
985
Tony Lindgren9a748052010-12-07 16:26:56 -0800986static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700987{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700988 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700989 u32 rev;
990
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700991 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700992 return;
993
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700994 rev = __raw_readw(bank->base + bank->regs->revision);
995 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700996 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700997
998 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700999}
1000
David Brownell8ba55c52008-02-26 11:10:50 -08001001/* This lock class tells lockdep that GPIO irqs are in a different
1002 * category than their parents, so it won't report false recursion.
1003 */
1004static struct lock_class_key gpio_lock_class;
1005
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001006static inline int init_gpio_info(struct platform_device *pdev)
1007{
1008 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1009 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1010 GFP_KERNEL);
1011 if (!gpio_bank) {
1012 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1013 return -ENOMEM;
1014 }
1015 return 0;
1016}
1017
1018/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001019static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1020{
1021 if (cpu_class_is_omap2()) {
1022 if (cpu_is_omap44xx()) {
1023 __raw_writel(0xffffffff, bank->base +
1024 OMAP4_GPIO_IRQSTATUSCLR0);
1025 __raw_writel(0x00000000, bank->base +
1026 OMAP4_GPIO_DEBOUNCENABLE);
1027 /* Initialize interface clk ungated, module enabled */
1028 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1029 } else if (cpu_is_omap34xx()) {
1030 __raw_writel(0x00000000, bank->base +
1031 OMAP24XX_GPIO_IRQENABLE1);
1032 __raw_writel(0xffffffff, bank->base +
1033 OMAP24XX_GPIO_IRQSTATUS1);
1034 __raw_writel(0x00000000, bank->base +
1035 OMAP24XX_GPIO_DEBOUNCE_EN);
1036
1037 /* Initialize interface clk ungated, module enabled */
1038 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1039 } else if (cpu_is_omap24xx()) {
1040 static const u32 non_wakeup_gpios[] = {
1041 0xe203ffc0, 0x08700040
1042 };
1043 if (id < ARRAY_SIZE(non_wakeup_gpios))
1044 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1045 }
1046 } else if (cpu_class_is_omap1()) {
1047 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001048 __raw_writew(0xffff, bank->base +
1049 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001050 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1051 __raw_writew(0xffff, bank->base
1052 + OMAP1510_GPIO_INT_MASK);
1053 __raw_writew(0x0000, bank->base
1054 + OMAP1510_GPIO_INT_STATUS);
1055 }
1056 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1057 __raw_writew(0x0000, bank->base
1058 + OMAP1610_GPIO_IRQENABLE1);
1059 __raw_writew(0xffff, bank->base
1060 + OMAP1610_GPIO_IRQSTATUS1);
1061 __raw_writew(0x0014, bank->base
1062 + OMAP1610_GPIO_SYSCONFIG);
1063
1064 /*
1065 * Enable system clock for GPIO module.
1066 * The CAM_CLK_CTRL *is* really the right place.
1067 */
1068 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1069 ULPD_CAM_CLK_CTRL);
1070 }
1071 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1072 __raw_writel(0xffffffff, bank->base
1073 + OMAP7XX_GPIO_INT_MASK);
1074 __raw_writel(0x00000000, bank->base
1075 + OMAP7XX_GPIO_INT_STATUS);
1076 }
1077 }
1078}
1079
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001080static __init void
1081omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1082 unsigned int num)
1083{
1084 struct irq_chip_generic *gc;
1085 struct irq_chip_type *ct;
1086
1087 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1088 handle_simple_irq);
1089 ct = gc->chip_types;
1090
1091 /* NOTE: No ack required, reading IRQ status clears it. */
1092 ct->chip.irq_mask = irq_gc_mask_set_bit;
1093 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1094 ct->chip.irq_set_type = gpio_irq_type;
1095 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1096 if (cpu_is_omap16xx())
1097 ct->chip.irq_set_wake = gpio_wake_enable,
1098
1099 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1100 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1101 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1102}
1103
Russell Kingd52b31d2011-05-27 13:56:12 -07001104static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001105{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001106 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001107 static int gpio;
1108
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001109 bank->mod_usage = 0;
1110 /*
1111 * REVISIT eventually switch from OMAP-specific gpio structs
1112 * over to the generic ones
1113 */
1114 bank->chip.request = omap_gpio_request;
1115 bank->chip.free = omap_gpio_free;
1116 bank->chip.direction_input = gpio_input;
1117 bank->chip.get = gpio_get;
1118 bank->chip.direction_output = gpio_output;
1119 bank->chip.set_debounce = gpio_debounce;
1120 bank->chip.set = gpio_set;
1121 bank->chip.to_irq = gpio_2irq;
1122 if (bank_is_mpuio(bank)) {
1123 bank->chip.label = "mpuio";
1124#ifdef CONFIG_ARCH_OMAP16XX
1125 bank->chip.dev = &omap_mpuio_device.dev;
1126#endif
1127 bank->chip.base = OMAP_MPUIO(0);
1128 } else {
1129 bank->chip.label = "gpio";
1130 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001131 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001132 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001133 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001134
1135 gpiochip_add(&bank->chip);
1136
1137 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001138 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001139 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001140 irq_set_chip_data(j, bank);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001141 if (bank_is_mpuio(bank)) {
1142 omap_mpuio_alloc_gc(bank, j, bank->width);
1143 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001144 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001145 irq_set_handler(j, handle_simple_irq);
1146 set_irq_flags(j, IRQF_VALID);
1147 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001148 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001149 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1150 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001151}
1152
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001153static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001154{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001155 static int gpio_init_done;
1156 struct omap_gpio_platform_data *pdata;
1157 struct resource *res;
1158 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001159 struct gpio_bank *bank;
1160
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001161 if (!pdev->dev.platform_data)
1162 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001163
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001164 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001165
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001166 if (!gpio_init_done) {
1167 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001168
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001169 ret = init_gpio_info(pdev);
1170 if (ret)
1171 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001172 }
1173
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001174 id = pdev->id;
1175 bank = &gpio_bank[id];
1176
1177 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1178 if (unlikely(!res)) {
1179 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1180 return -ENODEV;
1181 }
1182
1183 bank->irq = res->start;
1184 bank->virtual_irq_start = pdata->virtual_irq_start;
1185 bank->method = pdata->bank_type;
1186 bank->dev = &pdev->dev;
1187 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001188 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001189 bank->width = pdata->bank_width;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001190
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001191 bank->regs = pdata->regs;
1192
1193 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1194 bank->set_dataout = _set_gpio_dataout_reg;
1195 else
1196 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001197
1198 spin_lock_init(&bank->lock);
1199
1200 /* Static mapping, never released */
1201 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1202 if (unlikely(!res)) {
1203 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1204 return -ENODEV;
1205 }
1206
1207 bank->base = ioremap(res->start, resource_size(res));
1208 if (!bank->base) {
1209 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1210 return -ENOMEM;
1211 }
1212
1213 pm_runtime_enable(bank->dev);
1214 pm_runtime_get_sync(bank->dev);
1215
1216 omap_gpio_mod_init(bank, id);
1217 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001218 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001219
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001220 if (!gpio_init_done)
1221 gpio_init_done = 1;
1222
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001223 return 0;
1224}
1225
Tony Lindgren140455f2010-02-12 12:26:48 -08001226#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001227static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001228{
1229 int i;
1230
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001231 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001232 return 0;
1233
1234 for (i = 0; i < gpio_bank_count; i++) {
1235 struct gpio_bank *bank = &gpio_bank[i];
1236 void __iomem *wake_status;
1237 void __iomem *wake_clear;
1238 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001239 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001240
1241 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001242#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001243 case METHOD_GPIO_1610:
1244 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1245 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1246 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1247 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001248#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001249#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001250 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001251 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001252 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1253 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1254 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001255#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301256#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001257 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301258 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1259 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1260 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1261 break;
1262#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001263 default:
1264 continue;
1265 }
1266
David Brownella6472532008-03-03 04:33:30 -08001267 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001268 bank->saved_wakeup = __raw_readl(wake_status);
1269 __raw_writel(0xffffffff, wake_clear);
1270 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001271 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001272 }
1273
1274 return 0;
1275}
1276
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001277static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001278{
1279 int i;
1280
Tero Kristo723fdb72008-11-26 14:35:16 -08001281 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001282 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001283
1284 for (i = 0; i < gpio_bank_count; i++) {
1285 struct gpio_bank *bank = &gpio_bank[i];
1286 void __iomem *wake_clear;
1287 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001288 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001289
1290 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001291#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001292 case METHOD_GPIO_1610:
1293 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1294 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1295 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001296#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001297#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001298 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001299 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1300 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001301 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001302#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301303#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001304 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301305 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1306 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1307 break;
1308#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001309 default:
1310 continue;
1311 }
1312
David Brownella6472532008-03-03 04:33:30 -08001313 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001314 __raw_writel(0xffffffff, wake_clear);
1315 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001316 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001317 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001318}
1319
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001320static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001321 .suspend = omap_gpio_suspend,
1322 .resume = omap_gpio_resume,
1323};
1324
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001325#endif
1326
Tony Lindgren140455f2010-02-12 12:26:48 -08001327#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001328
1329static int workaround_enabled;
1330
Paul Walmsley72e06d02010-12-21 21:05:16 -07001331void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001332{
1333 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001334 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001335
Tero Kristoa118b5f2008-12-22 14:27:12 +02001336 if (cpu_is_omap34xx())
1337 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001338
Tero Kristoa118b5f2008-12-22 14:27:12 +02001339 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001340 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001341 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001342 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001343
Kevin Hilman0aed04352010-09-22 16:06:27 -07001344 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001345 clk_disable(bank->dbck);
1346
Paul Walmsley72e06d02010-12-21 21:05:16 -07001347 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001348 continue;
1349
1350 /* If going to OFF, remove triggering for all
1351 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1352 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001353 if (!(bank->enabled_non_wakeup_gpios))
1354 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001355
1356 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1357 bank->saved_datain = __raw_readl(bank->base +
1358 OMAP24XX_GPIO_DATAIN);
1359 l1 = __raw_readl(bank->base +
1360 OMAP24XX_GPIO_FALLINGDETECT);
1361 l2 = __raw_readl(bank->base +
1362 OMAP24XX_GPIO_RISINGDETECT);
1363 }
1364
1365 if (cpu_is_omap44xx()) {
1366 bank->saved_datain = __raw_readl(bank->base +
1367 OMAP4_GPIO_DATAIN);
1368 l1 = __raw_readl(bank->base +
1369 OMAP4_GPIO_FALLINGDETECT);
1370 l2 = __raw_readl(bank->base +
1371 OMAP4_GPIO_RISINGDETECT);
1372 }
1373
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001374 bank->saved_fallingdetect = l1;
1375 bank->saved_risingdetect = l2;
1376 l1 &= ~bank->enabled_non_wakeup_gpios;
1377 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001378
1379 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1380 __raw_writel(l1, bank->base +
1381 OMAP24XX_GPIO_FALLINGDETECT);
1382 __raw_writel(l2, bank->base +
1383 OMAP24XX_GPIO_RISINGDETECT);
1384 }
1385
1386 if (cpu_is_omap44xx()) {
1387 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1388 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1389 }
1390
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001391 c++;
1392 }
1393 if (!c) {
1394 workaround_enabled = 0;
1395 return;
1396 }
1397 workaround_enabled = 1;
1398}
1399
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001400void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001401{
1402 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001403 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001404
Tero Kristoa118b5f2008-12-22 14:27:12 +02001405 if (cpu_is_omap34xx())
1406 min = 1;
1407 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001408 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001409 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001410 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001411
Kevin Hilman0aed04352010-09-22 16:06:27 -07001412 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001413 clk_enable(bank->dbck);
1414
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001415 if (!workaround_enabled)
1416 continue;
1417
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001418 if (!(bank->enabled_non_wakeup_gpios))
1419 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001420
1421 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1422 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001423 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001424 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001425 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001426 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1427 }
1428
1429 if (cpu_is_omap44xx()) {
1430 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301431 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001432 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301433 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001434 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1435 }
1436
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001437 /* Check if any of the non-wakeup interrupt GPIOs have changed
1438 * state. If so, generate an IRQ by software. This is
1439 * horribly racy, but it's the best we can do to work around
1440 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001441 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001442 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001443
1444 /*
1445 * No need to generate IRQs for the rising edge for gpio IRQs
1446 * configured with falling edge only; and vice versa.
1447 */
1448 gen0 = l & bank->saved_fallingdetect;
1449 gen0 &= bank->saved_datain;
1450
1451 gen1 = l & bank->saved_risingdetect;
1452 gen1 &= ~(bank->saved_datain);
1453
1454 /* FIXME: Consider GPIO IRQs with level detections properly! */
1455 gen = l & (~(bank->saved_fallingdetect) &
1456 ~(bank->saved_risingdetect));
1457 /* Consider all GPIO IRQs needed to be updated */
1458 gen |= gen0 | gen1;
1459
1460 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001461 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001462
Sergio Aguirref00d6492010-03-03 16:21:08 +00001463 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001464 old0 = __raw_readl(bank->base +
1465 OMAP24XX_GPIO_LEVELDETECT0);
1466 old1 = __raw_readl(bank->base +
1467 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001468 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001469 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001470 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001471 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001472 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001473 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001474 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001475 OMAP24XX_GPIO_LEVELDETECT1);
1476 }
1477
1478 if (cpu_is_omap44xx()) {
1479 old0 = __raw_readl(bank->base +
1480 OMAP4_GPIO_LEVELDETECT0);
1481 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301482 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001483 __raw_writel(old0 | l, bank->base +
1484 OMAP4_GPIO_LEVELDETECT0);
1485 __raw_writel(old1 | l, bank->base +
1486 OMAP4_GPIO_LEVELDETECT1);
1487 __raw_writel(old0, bank->base +
1488 OMAP4_GPIO_LEVELDETECT0);
1489 __raw_writel(old1, bank->base +
1490 OMAP4_GPIO_LEVELDETECT1);
1491 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001492 }
1493 }
1494
1495}
1496
Tony Lindgren92105bb2005-09-07 17:20:26 +01001497#endif
1498
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001499#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301500/* save the registers of bank 2-6 */
1501void omap_gpio_save_context(void)
1502{
1503 int i;
1504
1505 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1506 for (i = 1; i < gpio_bank_count; i++) {
1507 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301508 gpio_context[i].irqenable1 =
1509 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1510 gpio_context[i].irqenable2 =
1511 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1512 gpio_context[i].wake_en =
1513 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1514 gpio_context[i].ctrl =
1515 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1516 gpio_context[i].oe =
1517 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1518 gpio_context[i].leveldetect0 =
1519 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1520 gpio_context[i].leveldetect1 =
1521 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1522 gpio_context[i].risingdetect =
1523 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1524 gpio_context[i].fallingdetect =
1525 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1526 gpio_context[i].dataout =
1527 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301528 }
1529}
1530
1531/* restore the required registers of bank 2-6 */
1532void omap_gpio_restore_context(void)
1533{
1534 int i;
1535
1536 for (i = 1; i < gpio_bank_count; i++) {
1537 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301538 __raw_writel(gpio_context[i].irqenable1,
1539 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1540 __raw_writel(gpio_context[i].irqenable2,
1541 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1542 __raw_writel(gpio_context[i].wake_en,
1543 bank->base + OMAP24XX_GPIO_WAKE_EN);
1544 __raw_writel(gpio_context[i].ctrl,
1545 bank->base + OMAP24XX_GPIO_CTRL);
1546 __raw_writel(gpio_context[i].oe,
1547 bank->base + OMAP24XX_GPIO_OE);
1548 __raw_writel(gpio_context[i].leveldetect0,
1549 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1550 __raw_writel(gpio_context[i].leveldetect1,
1551 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1552 __raw_writel(gpio_context[i].risingdetect,
1553 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1554 __raw_writel(gpio_context[i].fallingdetect,
1555 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1556 __raw_writel(gpio_context[i].dataout,
1557 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301558 }
1559}
1560#endif
1561
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001562static struct platform_driver omap_gpio_driver = {
1563 .probe = omap_gpio_probe,
1564 .driver = {
1565 .name = "omap_gpio",
1566 },
1567};
1568
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001569/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001570 * gpio driver register needs to be done before
1571 * machine_init functions access gpio APIs.
1572 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001573 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001574static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001575{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001576 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001577}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001578postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001579
Tony Lindgren92105bb2005-09-07 17:20:26 +01001580static int __init omap_gpio_sysinit(void)
1581{
David Brownell11a78b72006-12-06 17:14:11 -08001582 mpuio_init();
1583
Tony Lindgren140455f2010-02-12 12:26:48 -08001584#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001585 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1586 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001587#endif
1588
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001589 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001590}
1591
Tony Lindgren92105bb2005-09-07 17:20:26 +01001592arch_initcall(omap_gpio_sysinit);