blob: 2eed159d964e67b2bfd2285f66bf20e3e28bbee8 [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);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001024 }
1025 } else if (cpu_class_is_omap1()) {
Charulatha V03e128c2011-05-05 19:58:01 +05301026 if (bank_is_mpuio(bank)) {
Tony Lindgren5de62b82010-12-07 16:26:58 -08001027 __raw_writew(0xffff, bank->base +
1028 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Charulatha V03e128c2011-05-05 19:58:01 +05301029 mpuio_init(bank);
1030 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001031 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1032 __raw_writew(0xffff, bank->base
1033 + OMAP1510_GPIO_INT_MASK);
1034 __raw_writew(0x0000, bank->base
1035 + OMAP1510_GPIO_INT_STATUS);
1036 }
1037 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1038 __raw_writew(0x0000, bank->base
1039 + OMAP1610_GPIO_IRQENABLE1);
1040 __raw_writew(0xffff, bank->base
1041 + OMAP1610_GPIO_IRQSTATUS1);
1042 __raw_writew(0x0014, bank->base
1043 + OMAP1610_GPIO_SYSCONFIG);
1044
1045 /*
1046 * Enable system clock for GPIO module.
1047 * The CAM_CLK_CTRL *is* really the right place.
1048 */
1049 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1050 ULPD_CAM_CLK_CTRL);
1051 }
1052 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1053 __raw_writel(0xffffffff, bank->base
1054 + OMAP7XX_GPIO_INT_MASK);
1055 __raw_writel(0x00000000, bank->base
1056 + OMAP7XX_GPIO_INT_STATUS);
1057 }
1058 }
1059}
1060
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001061static __init void
1062omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1063 unsigned int num)
1064{
1065 struct irq_chip_generic *gc;
1066 struct irq_chip_type *ct;
1067
1068 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1069 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -07001070 if (!gc) {
1071 dev_err(bank->dev, "Memory alloc failed for gc\n");
1072 return;
1073 }
1074
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001075 ct = gc->chip_types;
1076
1077 /* NOTE: No ack required, reading IRQ status clears it. */
1078 ct->chip.irq_mask = irq_gc_mask_set_bit;
1079 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1080 ct->chip.irq_set_type = gpio_irq_type;
1081 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1082 if (cpu_is_omap16xx())
1083 ct->chip.irq_set_wake = gpio_wake_enable,
1084
1085 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1086 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1087 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1088}
1089
Russell Kingd52b31d2011-05-27 13:56:12 -07001090static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001091{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001092 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001093 static int gpio;
1094
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001095 bank->mod_usage = 0;
1096 /*
1097 * REVISIT eventually switch from OMAP-specific gpio structs
1098 * over to the generic ones
1099 */
1100 bank->chip.request = omap_gpio_request;
1101 bank->chip.free = omap_gpio_free;
1102 bank->chip.direction_input = gpio_input;
1103 bank->chip.get = gpio_get;
1104 bank->chip.direction_output = gpio_output;
1105 bank->chip.set_debounce = gpio_debounce;
1106 bank->chip.set = gpio_set;
1107 bank->chip.to_irq = gpio_2irq;
1108 if (bank_is_mpuio(bank)) {
1109 bank->chip.label = "mpuio";
1110#ifdef CONFIG_ARCH_OMAP16XX
1111 bank->chip.dev = &omap_mpuio_device.dev;
1112#endif
1113 bank->chip.base = OMAP_MPUIO(0);
1114 } else {
1115 bank->chip.label = "gpio";
1116 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001117 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001118 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001119 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001120
1121 gpiochip_add(&bank->chip);
1122
1123 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001124 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001125 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001126 irq_set_chip_data(j, bank);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001127 if (bank_is_mpuio(bank)) {
1128 omap_mpuio_alloc_gc(bank, j, bank->width);
1129 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001130 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001131 irq_set_handler(j, handle_simple_irq);
1132 set_irq_flags(j, IRQF_VALID);
1133 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001134 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001135 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1136 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001137}
1138
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001139static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001140{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001141 struct omap_gpio_platform_data *pdata;
1142 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001143 struct gpio_bank *bank;
Charulatha V03e128c2011-05-05 19:58:01 +05301144 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001145
Charulatha V03e128c2011-05-05 19:58:01 +05301146 if (!pdev->dev.platform_data) {
1147 ret = -EINVAL;
1148 goto err_exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001149 }
1150
Charulatha V03e128c2011-05-05 19:58:01 +05301151 bank = kzalloc(sizeof(struct gpio_bank), GFP_KERNEL);
1152 if (!bank) {
1153 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1154 ret = -ENOMEM;
1155 goto err_exit;
1156 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001157
1158 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1159 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301160 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n",
1161 pdev->id);
1162 ret = -ENODEV;
1163 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001164 }
1165
1166 bank->irq = res->start;
Charulatha V03e128c2011-05-05 19:58:01 +05301167 bank->id = pdev->id;
1168
1169 pdata = pdev->dev.platform_data;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001170 bank->virtual_irq_start = pdata->virtual_irq_start;
1171 bank->method = pdata->bank_type;
1172 bank->dev = &pdev->dev;
1173 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001174 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001175 bank->width = pdata->bank_width;
Charulatha V803a2432011-05-05 17:04:12 +05301176 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Charulatha V0cde8d02011-05-05 20:15:16 +05301177 bank->loses_context = pdata->loses_context;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301178 bank->get_context_loss_count = pdata->get_context_loss_count;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001179 bank->regs = pdata->regs;
1180
1181 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1182 bank->set_dataout = _set_gpio_dataout_reg;
1183 else
1184 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001185
1186 spin_lock_init(&bank->lock);
1187
1188 /* Static mapping, never released */
1189 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1190 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301191 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n",
1192 pdev->id);
1193 ret = -ENODEV;
1194 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001195 }
1196
1197 bank->base = ioremap(res->start, resource_size(res));
1198 if (!bank->base) {
Charulatha V03e128c2011-05-05 19:58:01 +05301199 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n",
1200 pdev->id);
1201 ret = -ENOMEM;
1202 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001203 }
1204
1205 pm_runtime_enable(bank->dev);
1206 pm_runtime_get_sync(bank->dev);
1207
Charulatha V03e128c2011-05-05 19:58:01 +05301208 omap_gpio_mod_init(bank);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001209 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001210 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001211
Charulatha V03e128c2011-05-05 19:58:01 +05301212 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001213
Charulatha V03e128c2011-05-05 19:58:01 +05301214 return ret;
1215
1216err_free:
1217 kfree(bank);
1218err_exit:
1219 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001220}
1221
Tony Lindgren140455f2010-02-12 12:26:48 -08001222#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001223static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001224{
Charulatha V03e128c2011-05-05 19:58:01 +05301225 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001226
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001227 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001228 return 0;
1229
Charulatha V03e128c2011-05-05 19:58:01 +05301230 list_for_each_entry(bank, &omap_gpio_list, node) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001231 void __iomem *wake_status;
1232 void __iomem *wake_clear;
1233 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001234 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001235
1236 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001237#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001238 case METHOD_GPIO_1610:
1239 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1240 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1241 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1242 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001243#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001244#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001245 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001246 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001247 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1248 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1249 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001250#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301251#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001252 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301253 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1254 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1255 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1256 break;
1257#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001258 default:
1259 continue;
1260 }
1261
David Brownella6472532008-03-03 04:33:30 -08001262 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001263 bank->saved_wakeup = __raw_readl(wake_status);
1264 __raw_writel(0xffffffff, wake_clear);
1265 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001266 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001267 }
1268
1269 return 0;
1270}
1271
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001272static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001273{
Charulatha V03e128c2011-05-05 19:58:01 +05301274 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001275
Tero Kristo723fdb72008-11-26 14:35:16 -08001276 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001277 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001278
Charulatha V03e128c2011-05-05 19:58:01 +05301279 list_for_each_entry(bank, &omap_gpio_list, node) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001280 void __iomem *wake_clear;
1281 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001282 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001283
1284 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001285#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001286 case METHOD_GPIO_1610:
1287 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1288 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1289 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001290#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001291#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001292 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001293 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1294 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001295 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001296#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301297#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001298 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301299 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1300 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1301 break;
1302#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001303 default:
1304 continue;
1305 }
1306
David Brownella6472532008-03-03 04:33:30 -08001307 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001308 __raw_writel(0xffffffff, wake_clear);
1309 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001310 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001311 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001312}
1313
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001314static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001315 .suspend = omap_gpio_suspend,
1316 .resume = omap_gpio_resume,
1317};
1318
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001319#endif
1320
Tony Lindgren140455f2010-02-12 12:26:48 -08001321#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001322
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301323static void omap_gpio_save_context(struct gpio_bank *bank);
1324static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001325
Paul Walmsley72e06d02010-12-21 21:05:16 -07001326void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001327{
Charulatha V03e128c2011-05-05 19:58:01 +05301328 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001329
Charulatha V03e128c2011-05-05 19:58:01 +05301330 list_for_each_entry(bank, &omap_gpio_list, node) {
Sanjeev Premica828762010-09-23 18:27:18 -07001331 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001332 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001333
Charulatha V0cde8d02011-05-05 20:15:16 +05301334 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301335 continue;
1336
Kevin Hilman0aed04352010-09-22 16:06:27 -07001337 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001338 clk_disable(bank->dbck);
1339
Paul Walmsley72e06d02010-12-21 21:05:16 -07001340 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001341 continue;
1342
1343 /* If going to OFF, remove triggering for all
1344 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1345 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001346 if (!(bank->enabled_non_wakeup_gpios))
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301347 goto save_gpio_context;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001348
1349 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1350 bank->saved_datain = __raw_readl(bank->base +
1351 OMAP24XX_GPIO_DATAIN);
1352 l1 = __raw_readl(bank->base +
1353 OMAP24XX_GPIO_FALLINGDETECT);
1354 l2 = __raw_readl(bank->base +
1355 OMAP24XX_GPIO_RISINGDETECT);
1356 }
1357
1358 if (cpu_is_omap44xx()) {
1359 bank->saved_datain = __raw_readl(bank->base +
1360 OMAP4_GPIO_DATAIN);
1361 l1 = __raw_readl(bank->base +
1362 OMAP4_GPIO_FALLINGDETECT);
1363 l2 = __raw_readl(bank->base +
1364 OMAP4_GPIO_RISINGDETECT);
1365 }
1366
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001367 bank->saved_fallingdetect = l1;
1368 bank->saved_risingdetect = l2;
1369 l1 &= ~bank->enabled_non_wakeup_gpios;
1370 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001371
1372 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1373 __raw_writel(l1, bank->base +
1374 OMAP24XX_GPIO_FALLINGDETECT);
1375 __raw_writel(l2, bank->base +
1376 OMAP24XX_GPIO_RISINGDETECT);
1377 }
1378
1379 if (cpu_is_omap44xx()) {
1380 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1381 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1382 }
1383
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301384save_gpio_context:
1385 if (bank->get_context_loss_count)
1386 bank->context_loss_count =
1387 bank->get_context_loss_count(bank->dev);
1388
1389 omap_gpio_save_context(bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001390 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001391}
1392
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001393void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001394{
Charulatha V03e128c2011-05-05 19:58:01 +05301395 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001396
Charulatha V03e128c2011-05-05 19:58:01 +05301397 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301398 int context_lost_cnt_after;
Sanjeev Premica828762010-09-23 18:27:18 -07001399 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001400 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001401
Charulatha V0cde8d02011-05-05 20:15:16 +05301402 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301403 continue;
1404
Kevin Hilman0aed04352010-09-22 16:06:27 -07001405 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001406 clk_enable(bank->dbck);
1407
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301408 if (bank->get_context_loss_count) {
1409 context_lost_cnt_after =
1410 bank->get_context_loss_count(bank->dev);
1411 if (context_lost_cnt_after != bank->context_loss_count
1412 || !context_lost_cnt_after)
1413 omap_gpio_restore_context(bank);
1414 }
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001415
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001416 if (!(bank->enabled_non_wakeup_gpios))
1417 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001418
1419 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1420 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001421 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001422 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001423 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001424 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1425 }
1426
1427 if (cpu_is_omap44xx()) {
1428 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301429 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001430 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301431 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001432 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1433 }
1434
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001435 /* Check if any of the non-wakeup interrupt GPIOs have changed
1436 * state. If so, generate an IRQ by software. This is
1437 * horribly racy, but it's the best we can do to work around
1438 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001439 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001440 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001441
1442 /*
1443 * No need to generate IRQs for the rising edge for gpio IRQs
1444 * configured with falling edge only; and vice versa.
1445 */
1446 gen0 = l & bank->saved_fallingdetect;
1447 gen0 &= bank->saved_datain;
1448
1449 gen1 = l & bank->saved_risingdetect;
1450 gen1 &= ~(bank->saved_datain);
1451
1452 /* FIXME: Consider GPIO IRQs with level detections properly! */
1453 gen = l & (~(bank->saved_fallingdetect) &
1454 ~(bank->saved_risingdetect));
1455 /* Consider all GPIO IRQs needed to be updated */
1456 gen |= gen0 | gen1;
1457
1458 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001459 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001460
Sergio Aguirref00d6492010-03-03 16:21:08 +00001461 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001462 old0 = __raw_readl(bank->base +
1463 OMAP24XX_GPIO_LEVELDETECT0);
1464 old1 = __raw_readl(bank->base +
1465 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001466 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001467 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001468 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001469 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001470 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001471 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001472 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001473 OMAP24XX_GPIO_LEVELDETECT1);
1474 }
1475
1476 if (cpu_is_omap44xx()) {
1477 old0 = __raw_readl(bank->base +
1478 OMAP4_GPIO_LEVELDETECT0);
1479 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301480 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001481 __raw_writel(old0 | l, bank->base +
1482 OMAP4_GPIO_LEVELDETECT0);
1483 __raw_writel(old1 | l, bank->base +
1484 OMAP4_GPIO_LEVELDETECT1);
1485 __raw_writel(old0, bank->base +
1486 OMAP4_GPIO_LEVELDETECT0);
1487 __raw_writel(old1, bank->base +
1488 OMAP4_GPIO_LEVELDETECT1);
1489 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001490 }
1491 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001492}
1493
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301494static void omap_gpio_save_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301495{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301496 bank->context.irqenable1 =
1497 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1498 bank->context.irqenable2 =
1499 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1500 bank->context.wake_en =
1501 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1502 bank->context.ctrl = __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1503 bank->context.oe = __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1504 bank->context.leveldetect0 =
1505 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1506 bank->context.leveldetect1 =
1507 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1508 bank->context.risingdetect =
1509 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1510 bank->context.fallingdetect =
1511 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1512 bank->context.dataout =
1513 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301514}
1515
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301516static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301517{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301518 __raw_writel(bank->context.irqenable1,
1519 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1520 __raw_writel(bank->context.irqenable2,
1521 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1522 __raw_writel(bank->context.wake_en,
1523 bank->base + OMAP24XX_GPIO_WAKE_EN);
1524 __raw_writel(bank->context.ctrl, bank->base + OMAP24XX_GPIO_CTRL);
1525 __raw_writel(bank->context.oe, bank->base + OMAP24XX_GPIO_OE);
1526 __raw_writel(bank->context.leveldetect0,
1527 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1528 __raw_writel(bank->context.leveldetect1,
1529 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1530 __raw_writel(bank->context.risingdetect,
1531 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1532 __raw_writel(bank->context.fallingdetect,
1533 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1534 __raw_writel(bank->context.dataout,
1535 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301536}
1537#endif
1538
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001539static struct platform_driver omap_gpio_driver = {
1540 .probe = omap_gpio_probe,
1541 .driver = {
1542 .name = "omap_gpio",
1543 },
1544};
1545
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001546/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001547 * gpio driver register needs to be done before
1548 * machine_init functions access gpio APIs.
1549 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001550 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001551static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001552{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001553 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001554}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001555postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001556
Tony Lindgren92105bb2005-09-07 17:20:26 +01001557static int __init omap_gpio_sysinit(void)
1558{
David Brownell11a78b72006-12-06 17:14:11 -08001559
Tony Lindgren140455f2010-02-12 12:26:48 -08001560#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001561 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1562 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001563#endif
1564
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001565 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001566}
1567
Tony Lindgren92105bb2005-09-07 17:20:26 +01001568arch_initcall(omap_gpio_sysinit);