blob: a1a3b9dca178ecea49081259d91fca4bf1092b8f [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080022#include <linux/slab.h>
23#include <linux/pm_runtime.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/irqs.h>
Russell King1bc857f2011-07-26 10:54:55 +010028#include <asm/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029#include <asm/mach/irq.h>
30
Charulatha V03e128c2011-05-05 19:58:01 +053031static LIST_HEAD(omap_gpio_list);
32
Charulatha V6d62e212011-04-18 15:06:51 +000033struct gpio_regs {
34 u32 irqenable1;
35 u32 irqenable2;
36 u32 wake_en;
37 u32 ctrl;
38 u32 oe;
39 u32 leveldetect0;
40 u32 leveldetect1;
41 u32 risingdetect;
42 u32 fallingdetect;
43 u32 dataout;
44};
45
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010046struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053047 struct list_head node;
Tony Lindgren9f7065d2009-10-19 15:25:20 -070048 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010049 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010050 u16 irq;
51 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010052 int method;
Tony Lindgren92105bb2005-09-07 17:20:26 +010053 u32 suspend_wakeup;
Janusz Krzysztofik78a43152011-08-23 13:42:24 +020054#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +010055 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080056#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080057 u32 non_wakeup_gpios;
58 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000059 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080060 u32 saved_datain;
61 u32 saved_fallingdetect;
62 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080063 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080064 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010065 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080066 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080067 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080068 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080069 u32 dbck_enable_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080070 struct device *dev;
71 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053072 bool loses_context;
Tony Lindgren5de62b82010-12-07 16:26:58 -080073 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070074 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053075 int context_loss_count;
Charulatha V03e128c2011-05-05 19:58:01 +053076 u16 id;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070077
78 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053079 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070080
81 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010082};
83
Kevin Hilman129fd222011-04-22 07:59:07 -070084#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
85#define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010086
87static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
88{
Tony Lindgren92105bb2005-09-07 17:20:26 +010089 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010090 u32 l;
91
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070092 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010093 l = __raw_readl(reg);
94 if (is_input)
95 l |= 1 << gpio;
96 else
97 l &= ~(1 << gpio);
98 __raw_writel(l, reg);
99}
100
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700101
102/* set data out value using dedicate set/clear register */
103static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100104{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100105 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700106 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100107
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700108 if (enable)
109 reg += bank->regs->set_dataout;
110 else
111 reg += bank->regs->clr_dataout;
112
113 __raw_writel(l, reg);
114}
115
116/* set data out value using mask register */
117static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
118{
119 void __iomem *reg = bank->base + bank->regs->dataout;
120 u32 gpio_bit = GPIO_BIT(bank, gpio);
121 u32 l;
122
123 l = __raw_readl(reg);
124 if (enable)
125 l |= gpio_bit;
126 else
127 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100128 __raw_writel(l, reg);
129}
130
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300131static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700133 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700135 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136}
137
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300138static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
139{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700140 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300141
Kevin Hilman129fd222011-04-22 07:59:07 -0700142 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300143}
144
Kevin Hilmanece95282011-07-12 08:18:15 -0700145static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
146{
147 int l = __raw_readl(base + reg);
148
149 if (set)
150 l |= mask;
151 else
152 l &= ~mask;
153
154 __raw_writel(l, base + reg);
155}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100156
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700157/**
158 * _set_gpio_debounce - low level gpio debounce time
159 * @bank: the gpio bank we're acting upon
160 * @gpio: the gpio number on this @gpio
161 * @debounce: debounce time to use
162 *
163 * OMAP's debounce time is in 31us steps so we need
164 * to convert and round up to the closest unit.
165 */
166static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
167 unsigned debounce)
168{
Kevin Hilman9942da02011-04-22 12:02:05 -0700169 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700170 u32 val;
171 u32 l;
172
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800173 if (!bank->dbck_flag)
174 return;
175
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700176 if (debounce < 32)
177 debounce = 0x01;
178 else if (debounce > 7936)
179 debounce = 0xff;
180 else
181 debounce = (debounce / 0x1f) - 1;
182
Kevin Hilman129fd222011-04-22 07:59:07 -0700183 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700184
Kevin Hilman9942da02011-04-22 12:02:05 -0700185 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700186 __raw_writel(debounce, reg);
187
Kevin Hilman9942da02011-04-22 12:02:05 -0700188 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700189 val = __raw_readl(reg);
190
191 if (debounce) {
192 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800193 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700194 } else {
195 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800196 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700197 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300198 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700199
200 __raw_writel(val, reg);
201}
202
Tony Lindgren140455f2010-02-12 12:26:48 -0800203#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700204static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
205 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100206{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800207 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100208 u32 gpio_bit = 1 << gpio;
209
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530210 if (cpu_is_omap44xx()) {
Kevin Hilmanece95282011-07-12 08:18:15 -0700211 _gpio_rmw(base, OMAP4_GPIO_LEVELDETECT0, gpio_bit,
212 trigger & IRQ_TYPE_LEVEL_LOW);
213 _gpio_rmw(base, OMAP4_GPIO_LEVELDETECT1, gpio_bit,
214 trigger & IRQ_TYPE_LEVEL_HIGH);
215 _gpio_rmw(base, OMAP4_GPIO_RISINGDETECT, gpio_bit,
216 trigger & IRQ_TYPE_EDGE_RISING);
217 _gpio_rmw(base, OMAP4_GPIO_FALLINGDETECT, gpio_bit,
218 trigger & IRQ_TYPE_EDGE_FALLING);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530219 } else {
Kevin Hilmanece95282011-07-12 08:18:15 -0700220 _gpio_rmw(base, OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
221 trigger & IRQ_TYPE_LEVEL_LOW);
222 _gpio_rmw(base, OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
223 trigger & IRQ_TYPE_LEVEL_HIGH);
224 _gpio_rmw(base, OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
225 trigger & IRQ_TYPE_EDGE_RISING);
226 _gpio_rmw(base, OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
227 trigger & IRQ_TYPE_EDGE_FALLING);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530228 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800229 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530230 if (cpu_is_omap44xx()) {
Kevin Hilmanece95282011-07-12 08:18:15 -0700231 _gpio_rmw(base, OMAP4_GPIO_IRQWAKEN0, gpio_bit,
232 trigger != 0);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530233 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000234 /*
235 * GPIO wakeup request can only be generated on edge
236 * transitions
237 */
238 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530239 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700240 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530241 else
242 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700243 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530244 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200245 }
Ambresh K55b220c2011-06-15 13:40:45 -0700246 /* This part needs to be executed always for OMAP{34xx, 44xx} */
247 if (cpu_is_omap34xx() || cpu_is_omap44xx() ||
248 (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000249 /*
250 * Log the edge gpio and manually trigger the IRQ
251 * after resume if the input level changes
252 * to avoid irq lost during PER RET/OFF mode
253 * Applies for omap2 non-wakeup gpio and all omap3 gpios
254 */
255 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800256 bank->enabled_non_wakeup_gpios |= gpio_bit;
257 else
258 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
259 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700260
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530261 if (cpu_is_omap44xx()) {
262 bank->level_mask =
263 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
264 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
265 } else {
266 bank->level_mask =
267 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
268 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
269 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100270}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800271#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100272
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800273#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800274/*
275 * This only applies to chips that can't do both rising and falling edge
276 * detection at once. For all other chips, this function is a noop.
277 */
278static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
279{
280 void __iomem *reg = bank->base;
281 u32 l = 0;
282
283 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800284 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800285 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800286 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800287#ifdef CONFIG_ARCH_OMAP15XX
288 case METHOD_GPIO_1510:
289 reg += OMAP1510_GPIO_INT_CONTROL;
290 break;
291#endif
292#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
293 case METHOD_GPIO_7XX:
294 reg += OMAP7XX_GPIO_INT_CONTROL;
295 break;
296#endif
297 default:
298 return;
299 }
300
301 l = __raw_readl(reg);
302 if ((l >> gpio) & 1)
303 l &= ~(1 << gpio);
304 else
305 l |= 1 << gpio;
306
307 __raw_writel(l, reg);
308}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800309#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800310
Tony Lindgren92105bb2005-09-07 17:20:26 +0100311static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
312{
313 void __iomem *reg = bank->base;
314 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100315
316 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800317#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100318 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800319 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100320 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000321 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800322 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100323 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100324 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100325 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100326 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100327 else
328 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100329 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800330#endif
331#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100332 case METHOD_GPIO_1510:
333 reg += OMAP1510_GPIO_INT_CONTROL;
334 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000335 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800336 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100337 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100338 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100339 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100341 else
342 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100343 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800344#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800345#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100346 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100347 if (gpio & 0x08)
348 reg += OMAP1610_GPIO_EDGE_CTRL2;
349 else
350 reg += OMAP1610_GPIO_EDGE_CTRL1;
351 gpio &= 0x07;
352 l = __raw_readl(reg);
353 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100354 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100355 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100356 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100357 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800358 if (trigger)
359 /* Enable wake-up during idle for dynamic tick */
360 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
361 else
362 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100363 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800364#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100365#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100366 case METHOD_GPIO_7XX:
367 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700368 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000369 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800370 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700371 if (trigger & IRQ_TYPE_EDGE_RISING)
372 l |= 1 << gpio;
373 else if (trigger & IRQ_TYPE_EDGE_FALLING)
374 l &= ~(1 << gpio);
375 else
376 goto bad;
377 break;
378#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800379#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100380 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800381 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800382 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200383 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800384#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100385 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100386 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100387 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100388 __raw_writel(l, reg);
389 return 0;
390bad:
391 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100392}
393
Lennert Buytenheke9191022010-11-29 11:17:17 +0100394static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100395{
396 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100397 unsigned gpio;
398 int retval;
David Brownella6472532008-03-03 04:33:30 -0800399 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100400
Lennert Buytenheke9191022010-11-29 11:17:17 +0100401 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
402 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100403 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100404 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100405
David Brownelle5c56ed2006-12-06 17:13:59 -0800406 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100407 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800408
409 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800410 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800411 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100412 return -EINVAL;
413
Lennert Buytenheke9191022010-11-29 11:17:17 +0100414 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800415 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700416 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800417 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800418
419 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100420 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800421 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100422 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800423
Tony Lindgren92105bb2005-09-07 17:20:26 +0100424 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100425}
426
427static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
428{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100429 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100430
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700431 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100432 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300433
434 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700435 if (bank->regs->irqstatus2) {
436 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700437 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700438 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700439
440 /* Flush posted write for the irq status to avoid spurious interrupts */
441 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100442}
443
444static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
445{
Kevin Hilman129fd222011-04-22 07:59:07 -0700446 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100447}
448
Imre Deakea6dedd2006-06-26 16:16:00 -0700449static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
450{
451 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700452 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700453 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700454
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700455 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700456 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700457 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700458 l = ~l;
459 l &= mask;
460 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700461}
462
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700463static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100464{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100465 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100466 u32 l;
467
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700468 if (bank->regs->set_irqenable) {
469 reg += bank->regs->set_irqenable;
470 l = gpio_mask;
471 } else {
472 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100473 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700474 if (bank->regs->irqenable_inv)
475 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100476 else
477 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100478 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700479
480 __raw_writel(l, reg);
481}
482
483static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
484{
485 void __iomem *reg = bank->base;
486 u32 l;
487
488 if (bank->regs->clr_irqenable) {
489 reg += bank->regs->clr_irqenable;
490 l = gpio_mask;
491 } else {
492 reg += bank->regs->irqenable;
493 l = __raw_readl(reg);
494 if (bank->regs->irqenable_inv)
495 l |= gpio_mask;
496 else
497 l &= ~gpio_mask;
498 }
499
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100500 __raw_writel(l, reg);
501}
502
503static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
504{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700505 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100506}
507
Tony Lindgren92105bb2005-09-07 17:20:26 +0100508/*
509 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
510 * 1510 does not seem to have a wake-up register. If JTAG is connected
511 * to the target, system will wake up always on GPIO events. While
512 * system is running all registered GPIO interrupts need to have wake-up
513 * enabled. When system is suspended, only selected GPIO interrupts need
514 * to have wake-up enabled.
515 */
516static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
517{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700518 u32 gpio_bit = GPIO_BIT(bank, gpio);
519 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800520
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700521 if (bank->non_wakeup_gpios & gpio_bit) {
522 dev_err(bank->dev,
523 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100524 return -EINVAL;
525 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700526
527 spin_lock_irqsave(&bank->lock, flags);
528 if (enable)
529 bank->suspend_wakeup |= gpio_bit;
530 else
531 bank->suspend_wakeup &= ~gpio_bit;
532
533 spin_unlock_irqrestore(&bank->lock, flags);
534
535 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100536}
537
Tony Lindgren4196dd62006-09-25 12:41:38 +0300538static void _reset_gpio(struct gpio_bank *bank, int gpio)
539{
Kevin Hilman129fd222011-04-22 07:59:07 -0700540 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300541 _set_gpio_irqenable(bank, gpio, 0);
542 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700543 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300544}
545
Tony Lindgren92105bb2005-09-07 17:20:26 +0100546/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100547static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100548{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100549 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100550 struct gpio_bank *bank;
551 int retval;
552
Lennert Buytenheke9191022010-11-29 11:17:17 +0100553 bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700554 retval = _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100555
556 return retval;
557}
558
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800559static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100560{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800561 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800562 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100563
David Brownella6472532008-03-03 04:33:30 -0800564 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100565
Tony Lindgren4196dd62006-09-25 12:41:38 +0300566 /* Set trigger to none. You need to enable the desired trigger with
567 * request_irq() or set_irq_type().
568 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800569 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100570
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000571#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100572 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100573 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100574
Tony Lindgren92105bb2005-09-07 17:20:26 +0100575 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800577 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578 }
579#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800580 if (!cpu_class_is_omap1()) {
581 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700582 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800583 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700584
585 if (cpu_is_omap24xx() || cpu_is_omap34xx())
586 reg += OMAP24XX_GPIO_CTRL;
587 else if (cpu_is_omap44xx())
588 reg += OMAP4_GPIO_CTRL;
589 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800590 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700591 ctrl &= 0xFFFFFFFE;
592 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800593 }
594 bank->mod_usage |= 1 << offset;
595 }
David Brownella6472532008-03-03 04:33:30 -0800596 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100597
598 return 0;
599}
600
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800601static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100602{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800603 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800604 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100605
David Brownella6472532008-03-03 04:33:30 -0800606 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100607#ifdef CONFIG_ARCH_OMAP16XX
608 if (bank->method == METHOD_GPIO_1610) {
609 /* Disable wake-up during idle for dynamic tick */
610 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800611 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100612 }
613#endif
Charulatha V9f096862010-05-14 12:05:27 -0700614#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
615 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100616 /* Disable wake-up during idle for dynamic tick */
617 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800618 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100619 }
620#endif
Charulatha V9f096862010-05-14 12:05:27 -0700621#ifdef CONFIG_ARCH_OMAP4
622 if (bank->method == METHOD_GPIO_44XX) {
623 /* Disable wake-up during idle for dynamic tick */
624 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
625 __raw_writel(1 << offset, reg);
626 }
627#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800628 if (!cpu_class_is_omap1()) {
629 bank->mod_usage &= ~(1 << offset);
630 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700631 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800632 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700633
634 if (cpu_is_omap24xx() || cpu_is_omap34xx())
635 reg += OMAP24XX_GPIO_CTRL;
636 else if (cpu_is_omap44xx())
637 reg += OMAP4_GPIO_CTRL;
638 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800639 /* Module is disabled, clocks are gated */
640 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700641 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800642 }
643 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800644 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800645 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100646}
647
648/*
649 * We need to unmask the GPIO bank interrupt as soon as possible to
650 * avoid missing GPIO interrupts for other lines in the bank.
651 * Then we need to mask-read-clear-unmask the triggered GPIO lines
652 * in the bank to avoid missing nested interrupts for a GPIO line.
653 * If we wait to unmask individual GPIO lines in the bank after the
654 * line's interrupt handler has been run, we may miss some nested
655 * interrupts.
656 */
Russell King10dd5ce2006-11-23 11:41:32 +0000657static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100658{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100659 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100660 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800661 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100662 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700663 u32 retrigger = 0;
664 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000665 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100666
Will Deaconee144182011-02-21 13:46:08 +0000667 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100668
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100669 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700670 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800671
672 if (WARN_ON(!isr_reg))
673 goto exit;
674
Tony Lindgren92105bb2005-09-07 17:20:26 +0100675 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100676 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700677 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100678
Imre Deakea6dedd2006-06-26 16:16:00 -0700679 enabled = _get_gpio_irqbank_mask(bank);
680 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100681
682 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
683 isr &= 0x0000ffff;
684
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800685 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800686 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -0700687 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100688
689 /* clear edge sensitive interrupts before handler(s) are
690 called so that we don't miss any interrupt occurred while
691 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700692 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100693 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700694 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100695
696 /* if there is only edge sensitive GPIO pin interrupts
697 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700698 if (!level_mask && !unmasked) {
699 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000700 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700701 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702
Imre Deakea6dedd2006-06-26 16:16:00 -0700703 isr |= retrigger;
704 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100705 if (!isr)
706 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100707
Tony Lindgren92105bb2005-09-07 17:20:26 +0100708 gpio_irq = bank->virtual_irq_start;
709 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700710 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800711
Tony Lindgren92105bb2005-09-07 17:20:26 +0100712 if (!(isr & 1))
713 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200714
Cory Maccarrone4318f362010-01-08 10:29:04 -0800715#ifdef CONFIG_ARCH_OMAP1
716 /*
717 * Some chips can't respond to both rising and falling
718 * at the same time. If this irq was requested with
719 * both flags, we need to flip the ICR data for the IRQ
720 * to respond to the IRQ for the opposite direction.
721 * This will be indicated in the bank toggle_mask.
722 */
723 if (bank->toggle_mask & (1 << gpio_index))
724 _toggle_gpio_edge_triggering(bank, gpio_index);
725#endif
726
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100727 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100728 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000729 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700730 /* if bank has any level sensitive GPIO pin interrupt
731 configured, we must unmask the bank interrupt only after
732 handler(s) are executed in order to avoid spurious bank
733 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800734exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700735 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000736 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100737}
738
Lennert Buytenheke9191022010-11-29 11:17:17 +0100739static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300740{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100741 unsigned int gpio = d->irq - IH_GPIO_BASE;
742 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700743 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300744
Colin Cross85ec7b92011-06-06 13:38:18 -0700745 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300746 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700747 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300748}
749
Lennert Buytenheke9191022010-11-29 11:17:17 +0100750static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100751{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100752 unsigned int gpio = d->irq - IH_GPIO_BASE;
753 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100754
755 _clear_gpio_irqstatus(bank, gpio);
756}
757
Lennert Buytenheke9191022010-11-29 11:17:17 +0100758static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100759{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100760 unsigned int gpio = d->irq - IH_GPIO_BASE;
761 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700762 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100763
Colin Cross85ec7b92011-06-06 13:38:18 -0700764 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100765 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700766 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700767 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100768}
769
Lennert Buytenheke9191022010-11-29 11:17:17 +0100770static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100771{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100772 unsigned int gpio = d->irq - IH_GPIO_BASE;
773 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700774 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100775 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700776 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700777
Colin Cross85ec7b92011-06-06 13:38:18 -0700778 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700779 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700780 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800781
782 /* For level-triggered GPIOs, the clearing must be done after
783 * the HW source is cleared, thus after the handler has run */
784 if (bank->level_mask & irq_mask) {
785 _set_gpio_irqenable(bank, gpio, 0);
786 _clear_gpio_irqstatus(bank, gpio);
787 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100788
Kevin Hilman4de8c752008-01-16 21:56:14 -0800789 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700790 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100791}
792
David Brownelle5c56ed2006-12-06 17:13:59 -0800793static struct irq_chip gpio_irq_chip = {
794 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100795 .irq_shutdown = gpio_irq_shutdown,
796 .irq_ack = gpio_ack_irq,
797 .irq_mask = gpio_mask_irq,
798 .irq_unmask = gpio_unmask_irq,
799 .irq_set_type = gpio_irq_type,
800 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800801};
802
803/*---------------------------------------------------------------------*/
804
805#ifdef CONFIG_ARCH_OMAP1
806
David Brownelle5c56ed2006-12-06 17:13:59 -0800807#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
808
David Brownell11a78b72006-12-06 17:14:11 -0800809#ifdef CONFIG_ARCH_OMAP16XX
810
811#include <linux/platform_device.h>
812
Magnus Damm79ee0312009-07-08 13:22:04 +0200813static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800814{
Magnus Damm79ee0312009-07-08 13:22:04 +0200815 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800816 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800817 void __iomem *mask_reg = bank->base +
818 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800819 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800820
David Brownella6472532008-03-03 04:33:30 -0800821 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800822 bank->saved_wakeup = __raw_readl(mask_reg);
823 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800824 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800825
826 return 0;
827}
828
Magnus Damm79ee0312009-07-08 13:22:04 +0200829static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800830{
Magnus Damm79ee0312009-07-08 13:22:04 +0200831 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800832 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800833 void __iomem *mask_reg = bank->base +
834 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800835 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800836
David Brownella6472532008-03-03 04:33:30 -0800837 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800838 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800839 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800840
841 return 0;
842}
843
Alexey Dobriyan47145212009-12-14 18:00:08 -0800844static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200845 .suspend_noirq = omap_mpuio_suspend_noirq,
846 .resume_noirq = omap_mpuio_resume_noirq,
847};
848
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200849/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800850static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800851 .driver = {
852 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200853 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800854 },
855};
856
857static struct platform_device omap_mpuio_device = {
858 .name = "mpuio",
859 .id = -1,
860 .dev = {
861 .driver = &omap_mpuio_driver.driver,
862 }
863 /* could list the /proc/iomem resources */
864};
865
Charulatha V03e128c2011-05-05 19:58:01 +0530866static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800867{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800868 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700869
David Brownell11a78b72006-12-06 17:14:11 -0800870 if (platform_driver_register(&omap_mpuio_driver) == 0)
871 (void) platform_device_register(&omap_mpuio_device);
872}
873
874#else
Charulatha V03e128c2011-05-05 19:58:01 +0530875static inline void mpuio_init(struct gpio_bank *bank) {}
David Brownell11a78b72006-12-06 17:14:11 -0800876#endif /* 16xx */
877
David Brownelle5c56ed2006-12-06 17:13:59 -0800878#else
879
David Brownelle5c56ed2006-12-06 17:13:59 -0800880#define bank_is_mpuio(bank) 0
Charulatha V03e128c2011-05-05 19:58:01 +0530881static inline void mpuio_init(struct gpio_bank *bank) {}
David Brownelle5c56ed2006-12-06 17:13:59 -0800882
883#endif
884
885/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100886
David Brownell52e31342008-03-03 12:43:23 -0800887/* REVISIT these are stupid implementations! replace by ones that
888 * don't switch on METHOD_* and which mostly avoid spinlocks
889 */
890
891static int gpio_input(struct gpio_chip *chip, unsigned offset)
892{
893 struct gpio_bank *bank;
894 unsigned long flags;
895
896 bank = container_of(chip, struct gpio_bank, chip);
897 spin_lock_irqsave(&bank->lock, flags);
898 _set_gpio_direction(bank, offset, 1);
899 spin_unlock_irqrestore(&bank->lock, flags);
900 return 0;
901}
902
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300903static int gpio_is_input(struct gpio_bank *bank, int mask)
904{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700905 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300906
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300907 return __raw_readl(reg) & mask;
908}
909
David Brownell52e31342008-03-03 12:43:23 -0800910static int gpio_get(struct gpio_chip *chip, unsigned offset)
911{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300912 struct gpio_bank *bank;
913 void __iomem *reg;
914 int gpio;
915 u32 mask;
916
917 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530918 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300919 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700920 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300921
922 if (gpio_is_input(bank, mask))
923 return _get_gpio_datain(bank, gpio);
924 else
925 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800926}
927
928static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
929{
930 struct gpio_bank *bank;
931 unsigned long flags;
932
933 bank = container_of(chip, struct gpio_bank, chip);
934 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700935 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800936 _set_gpio_direction(bank, offset, 0);
937 spin_unlock_irqrestore(&bank->lock, flags);
938 return 0;
939}
940
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700941static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
942 unsigned debounce)
943{
944 struct gpio_bank *bank;
945 unsigned long flags;
946
947 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800948
949 if (!bank->dbck) {
950 bank->dbck = clk_get(bank->dev, "dbclk");
951 if (IS_ERR(bank->dbck))
952 dev_err(bank->dev, "Could not get gpio dbck\n");
953 }
954
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700955 spin_lock_irqsave(&bank->lock, flags);
956 _set_gpio_debounce(bank, offset, debounce);
957 spin_unlock_irqrestore(&bank->lock, flags);
958
959 return 0;
960}
961
David Brownell52e31342008-03-03 12:43:23 -0800962static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
963{
964 struct gpio_bank *bank;
965 unsigned long flags;
966
967 bank = container_of(chip, struct gpio_bank, chip);
968 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700969 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800970 spin_unlock_irqrestore(&bank->lock, flags);
971}
972
David Brownella007b702008-12-10 17:35:25 -0800973static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
974{
975 struct gpio_bank *bank;
976
977 bank = container_of(chip, struct gpio_bank, chip);
978 return bank->virtual_irq_start + offset;
979}
980
David Brownell52e31342008-03-03 12:43:23 -0800981/*---------------------------------------------------------------------*/
982
Tony Lindgren9a748052010-12-07 16:26:56 -0800983static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700984{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700985 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700986 u32 rev;
987
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700988 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700989 return;
990
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700991 rev = __raw_readw(bank->base + bank->regs->revision);
992 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700993 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700994
995 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700996}
997
David Brownell8ba55c52008-02-26 11:10:50 -0800998/* This lock class tells lockdep that GPIO irqs are in a different
999 * category than their parents, so it won't report false recursion.
1000 */
1001static struct lock_class_key gpio_lock_class;
1002
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001003/* TODO: Cleanup cpu_is_* checks */
Charulatha V03e128c2011-05-05 19:58:01 +05301004static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001005{
1006 if (cpu_class_is_omap2()) {
1007 if (cpu_is_omap44xx()) {
1008 __raw_writel(0xffffffff, bank->base +
1009 OMAP4_GPIO_IRQSTATUSCLR0);
1010 __raw_writel(0x00000000, bank->base +
1011 OMAP4_GPIO_DEBOUNCENABLE);
1012 /* Initialize interface clk ungated, module enabled */
1013 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1014 } else if (cpu_is_omap34xx()) {
1015 __raw_writel(0x00000000, bank->base +
1016 OMAP24XX_GPIO_IRQENABLE1);
1017 __raw_writel(0xffffffff, bank->base +
1018 OMAP24XX_GPIO_IRQSTATUS1);
1019 __raw_writel(0x00000000, bank->base +
1020 OMAP24XX_GPIO_DEBOUNCE_EN);
1021
1022 /* Initialize interface clk ungated, module enabled */
1023 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1024 } else if (cpu_is_omap24xx()) {
1025 static const u32 non_wakeup_gpios[] = {
1026 0xe203ffc0, 0x08700040
1027 };
Charulatha V03e128c2011-05-05 19:58:01 +05301028 if (bank->id < ARRAY_SIZE(non_wakeup_gpios))
1029 bank->non_wakeup_gpios =
1030 non_wakeup_gpios[bank->id];
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001031 }
1032 } else if (cpu_class_is_omap1()) {
Charulatha V03e128c2011-05-05 19:58:01 +05301033 if (bank_is_mpuio(bank)) {
Tony Lindgren5de62b82010-12-07 16:26:58 -08001034 __raw_writew(0xffff, bank->base +
1035 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Charulatha V03e128c2011-05-05 19:58:01 +05301036 mpuio_init(bank);
1037 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001038 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1039 __raw_writew(0xffff, bank->base
1040 + OMAP1510_GPIO_INT_MASK);
1041 __raw_writew(0x0000, bank->base
1042 + OMAP1510_GPIO_INT_STATUS);
1043 }
1044 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1045 __raw_writew(0x0000, bank->base
1046 + OMAP1610_GPIO_IRQENABLE1);
1047 __raw_writew(0xffff, bank->base
1048 + OMAP1610_GPIO_IRQSTATUS1);
1049 __raw_writew(0x0014, bank->base
1050 + OMAP1610_GPIO_SYSCONFIG);
1051
1052 /*
1053 * Enable system clock for GPIO module.
1054 * The CAM_CLK_CTRL *is* really the right place.
1055 */
1056 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1057 ULPD_CAM_CLK_CTRL);
1058 }
1059 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1060 __raw_writel(0xffffffff, bank->base
1061 + OMAP7XX_GPIO_INT_MASK);
1062 __raw_writel(0x00000000, bank->base
1063 + OMAP7XX_GPIO_INT_STATUS);
1064 }
1065 }
1066}
1067
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001068static __init void
1069omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1070 unsigned int num)
1071{
1072 struct irq_chip_generic *gc;
1073 struct irq_chip_type *ct;
1074
1075 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1076 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -07001077 if (!gc) {
1078 dev_err(bank->dev, "Memory alloc failed for gc\n");
1079 return;
1080 }
1081
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001082 ct = gc->chip_types;
1083
1084 /* NOTE: No ack required, reading IRQ status clears it. */
1085 ct->chip.irq_mask = irq_gc_mask_set_bit;
1086 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1087 ct->chip.irq_set_type = gpio_irq_type;
1088 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1089 if (cpu_is_omap16xx())
1090 ct->chip.irq_set_wake = gpio_wake_enable,
1091
1092 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1093 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1094 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1095}
1096
Russell Kingd52b31d2011-05-27 13:56:12 -07001097static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001098{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001099 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001100 static int gpio;
1101
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001102 bank->mod_usage = 0;
1103 /*
1104 * REVISIT eventually switch from OMAP-specific gpio structs
1105 * over to the generic ones
1106 */
1107 bank->chip.request = omap_gpio_request;
1108 bank->chip.free = omap_gpio_free;
1109 bank->chip.direction_input = gpio_input;
1110 bank->chip.get = gpio_get;
1111 bank->chip.direction_output = gpio_output;
1112 bank->chip.set_debounce = gpio_debounce;
1113 bank->chip.set = gpio_set;
1114 bank->chip.to_irq = gpio_2irq;
1115 if (bank_is_mpuio(bank)) {
1116 bank->chip.label = "mpuio";
1117#ifdef CONFIG_ARCH_OMAP16XX
1118 bank->chip.dev = &omap_mpuio_device.dev;
1119#endif
1120 bank->chip.base = OMAP_MPUIO(0);
1121 } else {
1122 bank->chip.label = "gpio";
1123 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001124 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001125 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001126 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001127
1128 gpiochip_add(&bank->chip);
1129
1130 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001131 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001132 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001133 irq_set_chip_data(j, bank);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001134 if (bank_is_mpuio(bank)) {
1135 omap_mpuio_alloc_gc(bank, j, bank->width);
1136 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001137 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001138 irq_set_handler(j, handle_simple_irq);
1139 set_irq_flags(j, IRQF_VALID);
1140 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001141 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001142 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1143 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001144}
1145
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001146static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001147{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001148 struct omap_gpio_platform_data *pdata;
1149 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001150 struct gpio_bank *bank;
Charulatha V03e128c2011-05-05 19:58:01 +05301151 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001152
Charulatha V03e128c2011-05-05 19:58:01 +05301153 if (!pdev->dev.platform_data) {
1154 ret = -EINVAL;
1155 goto err_exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001156 }
1157
Charulatha V03e128c2011-05-05 19:58:01 +05301158 bank = kzalloc(sizeof(struct gpio_bank), GFP_KERNEL);
1159 if (!bank) {
1160 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1161 ret = -ENOMEM;
1162 goto err_exit;
1163 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001164
1165 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1166 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301167 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n",
1168 pdev->id);
1169 ret = -ENODEV;
1170 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001171 }
1172
1173 bank->irq = res->start;
Charulatha V03e128c2011-05-05 19:58:01 +05301174 bank->id = pdev->id;
1175
1176 pdata = pdev->dev.platform_data;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001177 bank->virtual_irq_start = pdata->virtual_irq_start;
1178 bank->method = pdata->bank_type;
1179 bank->dev = &pdev->dev;
1180 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001181 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001182 bank->width = pdata->bank_width;
Charulatha V0cde8d02011-05-05 20:15:16 +05301183 bank->loses_context = pdata->loses_context;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301184 bank->get_context_loss_count = pdata->get_context_loss_count;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001185 bank->regs = pdata->regs;
1186
1187 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1188 bank->set_dataout = _set_gpio_dataout_reg;
1189 else
1190 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001191
1192 spin_lock_init(&bank->lock);
1193
1194 /* Static mapping, never released */
1195 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1196 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301197 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n",
1198 pdev->id);
1199 ret = -ENODEV;
1200 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001201 }
1202
1203 bank->base = ioremap(res->start, resource_size(res));
1204 if (!bank->base) {
Charulatha V03e128c2011-05-05 19:58:01 +05301205 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n",
1206 pdev->id);
1207 ret = -ENOMEM;
1208 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001209 }
1210
1211 pm_runtime_enable(bank->dev);
1212 pm_runtime_get_sync(bank->dev);
1213
Charulatha V03e128c2011-05-05 19:58:01 +05301214 omap_gpio_mod_init(bank);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001215 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001216 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001217
Charulatha V03e128c2011-05-05 19:58:01 +05301218 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001219
Charulatha V03e128c2011-05-05 19:58:01 +05301220 return ret;
1221
1222err_free:
1223 kfree(bank);
1224err_exit:
1225 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001226}
1227
Tony Lindgren140455f2010-02-12 12:26:48 -08001228#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001229static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001230{
Charulatha V03e128c2011-05-05 19:58:01 +05301231 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001232
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001233 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001234 return 0;
1235
Charulatha V03e128c2011-05-05 19:58:01 +05301236 list_for_each_entry(bank, &omap_gpio_list, node) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001237 void __iomem *wake_status;
1238 void __iomem *wake_clear;
1239 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001240 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001241
1242 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001243#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001244 case METHOD_GPIO_1610:
1245 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1246 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1247 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1248 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001249#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001250#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001251 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001252 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001253 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1254 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1255 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001256#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301257#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001258 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301259 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1260 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1261 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1262 break;
1263#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001264 default:
1265 continue;
1266 }
1267
David Brownella6472532008-03-03 04:33:30 -08001268 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001269 bank->saved_wakeup = __raw_readl(wake_status);
1270 __raw_writel(0xffffffff, wake_clear);
1271 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001272 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001273 }
1274
1275 return 0;
1276}
1277
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001278static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001279{
Charulatha V03e128c2011-05-05 19:58:01 +05301280 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001281
Tero Kristo723fdb72008-11-26 14:35:16 -08001282 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001283 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001284
Charulatha V03e128c2011-05-05 19:58:01 +05301285 list_for_each_entry(bank, &omap_gpio_list, node) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001286 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
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301329static void omap_gpio_save_context(struct gpio_bank *bank);
1330static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001331
Paul Walmsley72e06d02010-12-21 21:05:16 -07001332void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001333{
Charulatha V03e128c2011-05-05 19:58:01 +05301334 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001335
Charulatha V03e128c2011-05-05 19:58:01 +05301336 list_for_each_entry(bank, &omap_gpio_list, node) {
Sanjeev Premica828762010-09-23 18:27:18 -07001337 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001338 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001339
Charulatha V0cde8d02011-05-05 20:15:16 +05301340 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301341 continue;
1342
Kevin Hilman0aed04352010-09-22 16:06:27 -07001343 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001344 clk_disable(bank->dbck);
1345
Paul Walmsley72e06d02010-12-21 21:05:16 -07001346 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001347 continue;
1348
1349 /* If going to OFF, remove triggering for all
1350 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1351 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001352 if (!(bank->enabled_non_wakeup_gpios))
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301353 goto save_gpio_context;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001354
1355 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1356 bank->saved_datain = __raw_readl(bank->base +
1357 OMAP24XX_GPIO_DATAIN);
1358 l1 = __raw_readl(bank->base +
1359 OMAP24XX_GPIO_FALLINGDETECT);
1360 l2 = __raw_readl(bank->base +
1361 OMAP24XX_GPIO_RISINGDETECT);
1362 }
1363
1364 if (cpu_is_omap44xx()) {
1365 bank->saved_datain = __raw_readl(bank->base +
1366 OMAP4_GPIO_DATAIN);
1367 l1 = __raw_readl(bank->base +
1368 OMAP4_GPIO_FALLINGDETECT);
1369 l2 = __raw_readl(bank->base +
1370 OMAP4_GPIO_RISINGDETECT);
1371 }
1372
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001373 bank->saved_fallingdetect = l1;
1374 bank->saved_risingdetect = l2;
1375 l1 &= ~bank->enabled_non_wakeup_gpios;
1376 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001377
1378 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1379 __raw_writel(l1, bank->base +
1380 OMAP24XX_GPIO_FALLINGDETECT);
1381 __raw_writel(l2, bank->base +
1382 OMAP24XX_GPIO_RISINGDETECT);
1383 }
1384
1385 if (cpu_is_omap44xx()) {
1386 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1387 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1388 }
1389
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301390save_gpio_context:
1391 if (bank->get_context_loss_count)
1392 bank->context_loss_count =
1393 bank->get_context_loss_count(bank->dev);
1394
1395 omap_gpio_save_context(bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001396 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001397}
1398
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001399void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001400{
Charulatha V03e128c2011-05-05 19:58:01 +05301401 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001402
Charulatha V03e128c2011-05-05 19:58:01 +05301403 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301404 int context_lost_cnt_after;
Sanjeev Premica828762010-09-23 18:27:18 -07001405 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001406 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001407
Charulatha V0cde8d02011-05-05 20:15:16 +05301408 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301409 continue;
1410
Kevin Hilman0aed04352010-09-22 16:06:27 -07001411 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001412 clk_enable(bank->dbck);
1413
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301414 if (bank->get_context_loss_count) {
1415 context_lost_cnt_after =
1416 bank->get_context_loss_count(bank->dev);
1417 if (context_lost_cnt_after != bank->context_loss_count
1418 || !context_lost_cnt_after)
1419 omap_gpio_restore_context(bank);
1420 }
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001421
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001422 if (!(bank->enabled_non_wakeup_gpios))
1423 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001424
1425 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1426 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001427 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001428 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001429 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001430 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1431 }
1432
1433 if (cpu_is_omap44xx()) {
1434 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301435 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001436 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301437 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001438 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1439 }
1440
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001441 /* Check if any of the non-wakeup interrupt GPIOs have changed
1442 * state. If so, generate an IRQ by software. This is
1443 * horribly racy, but it's the best we can do to work around
1444 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001445 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001446 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001447
1448 /*
1449 * No need to generate IRQs for the rising edge for gpio IRQs
1450 * configured with falling edge only; and vice versa.
1451 */
1452 gen0 = l & bank->saved_fallingdetect;
1453 gen0 &= bank->saved_datain;
1454
1455 gen1 = l & bank->saved_risingdetect;
1456 gen1 &= ~(bank->saved_datain);
1457
1458 /* FIXME: Consider GPIO IRQs with level detections properly! */
1459 gen = l & (~(bank->saved_fallingdetect) &
1460 ~(bank->saved_risingdetect));
1461 /* Consider all GPIO IRQs needed to be updated */
1462 gen |= gen0 | gen1;
1463
1464 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001465 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001466
Sergio Aguirref00d6492010-03-03 16:21:08 +00001467 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001468 old0 = __raw_readl(bank->base +
1469 OMAP24XX_GPIO_LEVELDETECT0);
1470 old1 = __raw_readl(bank->base +
1471 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001472 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001473 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001474 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001475 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001476 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001477 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001478 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001479 OMAP24XX_GPIO_LEVELDETECT1);
1480 }
1481
1482 if (cpu_is_omap44xx()) {
1483 old0 = __raw_readl(bank->base +
1484 OMAP4_GPIO_LEVELDETECT0);
1485 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301486 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001487 __raw_writel(old0 | l, bank->base +
1488 OMAP4_GPIO_LEVELDETECT0);
1489 __raw_writel(old1 | l, bank->base +
1490 OMAP4_GPIO_LEVELDETECT1);
1491 __raw_writel(old0, bank->base +
1492 OMAP4_GPIO_LEVELDETECT0);
1493 __raw_writel(old1, bank->base +
1494 OMAP4_GPIO_LEVELDETECT1);
1495 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001496 }
1497 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001498}
1499
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301500static void omap_gpio_save_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301501{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301502 bank->context.irqenable1 =
1503 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1504 bank->context.irqenable2 =
1505 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1506 bank->context.wake_en =
1507 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1508 bank->context.ctrl = __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1509 bank->context.oe = __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1510 bank->context.leveldetect0 =
1511 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1512 bank->context.leveldetect1 =
1513 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1514 bank->context.risingdetect =
1515 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1516 bank->context.fallingdetect =
1517 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1518 bank->context.dataout =
1519 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301520}
1521
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301522static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301523{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301524 __raw_writel(bank->context.irqenable1,
1525 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1526 __raw_writel(bank->context.irqenable2,
1527 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1528 __raw_writel(bank->context.wake_en,
1529 bank->base + OMAP24XX_GPIO_WAKE_EN);
1530 __raw_writel(bank->context.ctrl, bank->base + OMAP24XX_GPIO_CTRL);
1531 __raw_writel(bank->context.oe, bank->base + OMAP24XX_GPIO_OE);
1532 __raw_writel(bank->context.leveldetect0,
1533 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1534 __raw_writel(bank->context.leveldetect1,
1535 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1536 __raw_writel(bank->context.risingdetect,
1537 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1538 __raw_writel(bank->context.fallingdetect,
1539 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1540 __raw_writel(bank->context.dataout,
1541 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301542}
1543#endif
1544
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001545static struct platform_driver omap_gpio_driver = {
1546 .probe = omap_gpio_probe,
1547 .driver = {
1548 .name = "omap_gpio",
1549 },
1550};
1551
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001552/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001553 * gpio driver register needs to be done before
1554 * machine_init functions access gpio APIs.
1555 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001556 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001557static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001558{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001559 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001560}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001561postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001562
Tony Lindgren92105bb2005-09-07 17:20:26 +01001563static int __init omap_gpio_sysinit(void)
1564{
David Brownell11a78b72006-12-06 17:14:11 -08001565
Tony Lindgren140455f2010-02-12 12:26:48 -08001566#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001567 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1568 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001569#endif
1570
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001571 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001572}
1573
Tony Lindgren92105bb2005-09-07 17:20:26 +01001574arch_initcall(omap_gpio_sysinit);