blob: 5cc2c04cd0a656e4a34cfdac22104164b75bf269 [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))
Charulatha Vc8eef652011-05-02 15:21:42 +053086#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010087
88static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
89{
Tony Lindgren92105bb2005-09-07 17:20:26 +010090 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010091 u32 l;
92
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070093 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010094 l = __raw_readl(reg);
95 if (is_input)
96 l |= 1 << gpio;
97 else
98 l &= ~(1 << gpio);
99 __raw_writel(l, reg);
100}
101
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700102
103/* set data out value using dedicate set/clear register */
104static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100105{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100106 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700107 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100108
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700109 if (enable)
110 reg += bank->regs->set_dataout;
111 else
112 reg += bank->regs->clr_dataout;
113
114 __raw_writel(l, reg);
115}
116
117/* set data out value using mask register */
118static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
119{
120 void __iomem *reg = bank->base + bank->regs->dataout;
121 u32 gpio_bit = GPIO_BIT(bank, gpio);
122 u32 l;
123
124 l = __raw_readl(reg);
125 if (enable)
126 l |= gpio_bit;
127 else
128 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100129 __raw_writel(l, reg);
130}
131
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300132static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700134 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100135
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700136 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100137}
138
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300139static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
140{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700141 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300142
Kevin Hilman129fd222011-04-22 07:59:07 -0700143 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300144}
145
Kevin Hilmanece95282011-07-12 08:18:15 -0700146static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
147{
148 int l = __raw_readl(base + reg);
149
150 if (set)
151 l |= mask;
152 else
153 l &= ~mask;
154
155 __raw_writel(l, base + reg);
156}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100157
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700158/**
159 * _set_gpio_debounce - low level gpio debounce time
160 * @bank: the gpio bank we're acting upon
161 * @gpio: the gpio number on this @gpio
162 * @debounce: debounce time to use
163 *
164 * OMAP's debounce time is in 31us steps so we need
165 * to convert and round up to the closest unit.
166 */
167static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
168 unsigned debounce)
169{
Kevin Hilman9942da02011-04-22 12:02:05 -0700170 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700171 u32 val;
172 u32 l;
173
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800174 if (!bank->dbck_flag)
175 return;
176
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700177 if (debounce < 32)
178 debounce = 0x01;
179 else if (debounce > 7936)
180 debounce = 0xff;
181 else
182 debounce = (debounce / 0x1f) - 1;
183
Kevin Hilman129fd222011-04-22 07:59:07 -0700184 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700185
Kevin Hilman9942da02011-04-22 12:02:05 -0700186 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700187 __raw_writel(debounce, reg);
188
Kevin Hilman9942da02011-04-22 12:02:05 -0700189 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700190 val = __raw_readl(reg);
191
192 if (debounce) {
193 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800194 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700195 } else {
196 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800197 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700198 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300199 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700200
201 __raw_writel(val, reg);
202}
203
Tony Lindgren140455f2010-02-12 12:26:48 -0800204#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700205static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
206 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100207{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800208 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100209 u32 gpio_bit = 1 << gpio;
210
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530211 if (cpu_is_omap44xx()) {
Kevin Hilmanece95282011-07-12 08:18:15 -0700212 _gpio_rmw(base, OMAP4_GPIO_LEVELDETECT0, gpio_bit,
213 trigger & IRQ_TYPE_LEVEL_LOW);
214 _gpio_rmw(base, OMAP4_GPIO_LEVELDETECT1, gpio_bit,
215 trigger & IRQ_TYPE_LEVEL_HIGH);
216 _gpio_rmw(base, OMAP4_GPIO_RISINGDETECT, gpio_bit,
217 trigger & IRQ_TYPE_EDGE_RISING);
218 _gpio_rmw(base, OMAP4_GPIO_FALLINGDETECT, gpio_bit,
219 trigger & IRQ_TYPE_EDGE_FALLING);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530220 } else {
Kevin Hilmanece95282011-07-12 08:18:15 -0700221 _gpio_rmw(base, OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
222 trigger & IRQ_TYPE_LEVEL_LOW);
223 _gpio_rmw(base, OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
224 trigger & IRQ_TYPE_LEVEL_HIGH);
225 _gpio_rmw(base, OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
226 trigger & IRQ_TYPE_EDGE_RISING);
227 _gpio_rmw(base, OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
228 trigger & IRQ_TYPE_EDGE_FALLING);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530229 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800230 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530231 if (cpu_is_omap44xx()) {
Kevin Hilmanece95282011-07-12 08:18:15 -0700232 _gpio_rmw(base, OMAP4_GPIO_IRQWAKEN0, gpio_bit,
233 trigger != 0);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530234 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000235 /*
236 * GPIO wakeup request can only be generated on edge
237 * transitions
238 */
239 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530240 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700241 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530242 else
243 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700244 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530245 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200246 }
Ambresh K55b220c2011-06-15 13:40:45 -0700247 /* This part needs to be executed always for OMAP{34xx, 44xx} */
248 if (cpu_is_omap34xx() || cpu_is_omap44xx() ||
249 (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000250 /*
251 * Log the edge gpio and manually trigger the IRQ
252 * after resume if the input level changes
253 * to avoid irq lost during PER RET/OFF mode
254 * Applies for omap2 non-wakeup gpio and all omap3 gpios
255 */
256 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800257 bank->enabled_non_wakeup_gpios |= gpio_bit;
258 else
259 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
260 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700261
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530262 if (cpu_is_omap44xx()) {
263 bank->level_mask =
264 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
265 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
266 } else {
267 bank->level_mask =
268 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
269 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
270 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100271}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800272#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100273
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800274#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800275/*
276 * This only applies to chips that can't do both rising and falling edge
277 * detection at once. For all other chips, this function is a noop.
278 */
279static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
280{
281 void __iomem *reg = bank->base;
282 u32 l = 0;
283
284 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800285 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800286 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800287 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800288#ifdef CONFIG_ARCH_OMAP15XX
289 case METHOD_GPIO_1510:
290 reg += OMAP1510_GPIO_INT_CONTROL;
291 break;
292#endif
293#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
294 case METHOD_GPIO_7XX:
295 reg += OMAP7XX_GPIO_INT_CONTROL;
296 break;
297#endif
298 default:
299 return;
300 }
301
302 l = __raw_readl(reg);
303 if ((l >> gpio) & 1)
304 l &= ~(1 << gpio);
305 else
306 l |= 1 << gpio;
307
308 __raw_writel(l, reg);
309}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800310#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800311
Tony Lindgren92105bb2005-09-07 17:20:26 +0100312static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
313{
314 void __iomem *reg = bank->base;
315 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100316
317 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800318#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100319 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800320 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100321 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000322 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800323 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100324 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100325 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100326 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100327 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100328 else
329 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100330 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800331#endif
332#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100333 case METHOD_GPIO_1510:
334 reg += OMAP1510_GPIO_INT_CONTROL;
335 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000336 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800337 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100338 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100339 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100340 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100341 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100342 else
343 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100344 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800345#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800346#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100347 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348 if (gpio & 0x08)
349 reg += OMAP1610_GPIO_EDGE_CTRL2;
350 else
351 reg += OMAP1610_GPIO_EDGE_CTRL1;
352 gpio &= 0x07;
353 l = __raw_readl(reg);
354 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100355 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100356 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100357 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100358 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800359 if (trigger)
360 /* Enable wake-up during idle for dynamic tick */
361 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
362 else
363 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100364 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800365#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100366#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100367 case METHOD_GPIO_7XX:
368 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700369 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000370 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800371 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700372 if (trigger & IRQ_TYPE_EDGE_RISING)
373 l |= 1 << gpio;
374 else if (trigger & IRQ_TYPE_EDGE_FALLING)
375 l &= ~(1 << gpio);
376 else
377 goto bad;
378 break;
379#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800380#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100381 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800382 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800383 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200384 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800385#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100386 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100387 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100388 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100389 __raw_writel(l, reg);
390 return 0;
391bad:
392 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100393}
394
Lennert Buytenheke9191022010-11-29 11:17:17 +0100395static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100396{
397 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100398 unsigned gpio;
399 int retval;
David Brownella6472532008-03-03 04:33:30 -0800400 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100401
Lennert Buytenheke9191022010-11-29 11:17:17 +0100402 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
403 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100404 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100405 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100406
David Brownelle5c56ed2006-12-06 17:13:59 -0800407 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100408 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800409
410 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800411 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800412 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100413 return -EINVAL;
414
Lennert Buytenheke9191022010-11-29 11:17:17 +0100415 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800416 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700417 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800418 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800419
420 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100421 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800422 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100423 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800424
Tony Lindgren92105bb2005-09-07 17:20:26 +0100425 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100426}
427
428static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
429{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100430 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100431
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700432 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100433 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300434
435 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700436 if (bank->regs->irqstatus2) {
437 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700438 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700439 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700440
441 /* Flush posted write for the irq status to avoid spurious interrupts */
442 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100443}
444
445static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
446{
Kevin Hilman129fd222011-04-22 07:59:07 -0700447 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100448}
449
Imre Deakea6dedd2006-06-26 16:16:00 -0700450static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
451{
452 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700453 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700454 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700455
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700456 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700457 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700458 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700459 l = ~l;
460 l &= mask;
461 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700462}
463
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700464static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100465{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100466 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100467 u32 l;
468
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700469 if (bank->regs->set_irqenable) {
470 reg += bank->regs->set_irqenable;
471 l = gpio_mask;
472 } else {
473 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100474 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700475 if (bank->regs->irqenable_inv)
476 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100477 else
478 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100479 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700480
481 __raw_writel(l, reg);
482}
483
484static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
485{
486 void __iomem *reg = bank->base;
487 u32 l;
488
489 if (bank->regs->clr_irqenable) {
490 reg += bank->regs->clr_irqenable;
491 l = gpio_mask;
492 } else {
493 reg += bank->regs->irqenable;
494 l = __raw_readl(reg);
495 if (bank->regs->irqenable_inv)
496 l |= gpio_mask;
497 else
498 l &= ~gpio_mask;
499 }
500
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100501 __raw_writel(l, reg);
502}
503
504static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
505{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700506 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100507}
508
Tony Lindgren92105bb2005-09-07 17:20:26 +0100509/*
510 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
511 * 1510 does not seem to have a wake-up register. If JTAG is connected
512 * to the target, system will wake up always on GPIO events. While
513 * system is running all registered GPIO interrupts need to have wake-up
514 * enabled. When system is suspended, only selected GPIO interrupts need
515 * to have wake-up enabled.
516 */
517static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
518{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700519 u32 gpio_bit = GPIO_BIT(bank, gpio);
520 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800521
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700522 if (bank->non_wakeup_gpios & gpio_bit) {
523 dev_err(bank->dev,
524 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100525 return -EINVAL;
526 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700527
528 spin_lock_irqsave(&bank->lock, flags);
529 if (enable)
530 bank->suspend_wakeup |= gpio_bit;
531 else
532 bank->suspend_wakeup &= ~gpio_bit;
533
534 spin_unlock_irqrestore(&bank->lock, flags);
535
536 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100537}
538
Tony Lindgren4196dd62006-09-25 12:41:38 +0300539static void _reset_gpio(struct gpio_bank *bank, int gpio)
540{
Kevin Hilman129fd222011-04-22 07:59:07 -0700541 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300542 _set_gpio_irqenable(bank, gpio, 0);
543 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700544 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300545}
546
Tony Lindgren92105bb2005-09-07 17:20:26 +0100547/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100548static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100549{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100550 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100551 struct gpio_bank *bank;
552 int retval;
553
Lennert Buytenheke9191022010-11-29 11:17:17 +0100554 bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700555 retval = _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100556
557 return retval;
558}
559
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800560static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100561{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800562 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800563 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564
David Brownella6472532008-03-03 04:33:30 -0800565 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100566
Tony Lindgren4196dd62006-09-25 12:41:38 +0300567 /* Set trigger to none. You need to enable the desired trigger with
568 * request_irq() or set_irq_type().
569 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800570 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100571
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000572#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100574 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100575
Tony Lindgren92105bb2005-09-07 17:20:26 +0100576 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100577 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800578 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100579 }
580#endif
Charulatha Vc8eef652011-05-02 15:21:42 +0530581 if (bank->regs->ctrl && !bank->mod_usage) {
582 void __iomem *reg = bank->base + bank->regs->ctrl;
583 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700584
Charulatha Vc8eef652011-05-02 15:21:42 +0530585 ctrl = __raw_readl(reg);
586 /* Module is enabled, clocks are not gated */
587 ctrl &= ~GPIO_MOD_CTRL_BIT;
588 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800589 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530590
591 bank->mod_usage |= 1 << offset;
592
David Brownella6472532008-03-03 04:33:30 -0800593 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100594
595 return 0;
596}
597
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800598static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100599{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800600 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800601 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100602
David Brownella6472532008-03-03 04:33:30 -0800603 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100604#ifdef CONFIG_ARCH_OMAP16XX
605 if (bank->method == METHOD_GPIO_1610) {
606 /* Disable wake-up during idle for dynamic tick */
607 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800608 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100609 }
610#endif
Charulatha V9f096862010-05-14 12:05:27 -0700611#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
612 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100613 /* Disable wake-up during idle for dynamic tick */
614 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800615 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100616 }
617#endif
Charulatha V9f096862010-05-14 12:05:27 -0700618#ifdef CONFIG_ARCH_OMAP4
619 if (bank->method == METHOD_GPIO_44XX) {
620 /* Disable wake-up during idle for dynamic tick */
621 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
622 __raw_writel(1 << offset, reg);
623 }
624#endif
Charulatha Vc8eef652011-05-02 15:21:42 +0530625 bank->mod_usage &= ~(1 << offset);
Charulatha V9f096862010-05-14 12:05:27 -0700626
Charulatha Vc8eef652011-05-02 15:21:42 +0530627 if (bank->regs->ctrl && !bank->mod_usage) {
628 void __iomem *reg = bank->base + bank->regs->ctrl;
629 u32 ctrl;
630
631 ctrl = __raw_readl(reg);
632 /* Module is disabled, clocks are gated */
633 ctrl |= GPIO_MOD_CTRL_BIT;
634 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800635 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530636
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800637 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800638 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100639}
640
641/*
642 * We need to unmask the GPIO bank interrupt as soon as possible to
643 * avoid missing GPIO interrupts for other lines in the bank.
644 * Then we need to mask-read-clear-unmask the triggered GPIO lines
645 * in the bank to avoid missing nested interrupts for a GPIO line.
646 * If we wait to unmask individual GPIO lines in the bank after the
647 * line's interrupt handler has been run, we may miss some nested
648 * interrupts.
649 */
Russell King10dd5ce2006-11-23 11:41:32 +0000650static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100651{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100652 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100653 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800654 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100655 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700656 u32 retrigger = 0;
657 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000658 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100659
Will Deaconee144182011-02-21 13:46:08 +0000660 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100661
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100662 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700663 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800664
665 if (WARN_ON(!isr_reg))
666 goto exit;
667
Tony Lindgren92105bb2005-09-07 17:20:26 +0100668 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100669 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700670 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100671
Imre Deakea6dedd2006-06-26 16:16:00 -0700672 enabled = _get_gpio_irqbank_mask(bank);
673 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100674
675 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
676 isr &= 0x0000ffff;
677
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800678 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800679 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -0700680 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100681
682 /* clear edge sensitive interrupts before handler(s) are
683 called so that we don't miss any interrupt occurred while
684 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700685 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100686 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700687 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100688
689 /* if there is only edge sensitive GPIO pin interrupts
690 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700691 if (!level_mask && !unmasked) {
692 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000693 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700694 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100695
Imre Deakea6dedd2006-06-26 16:16:00 -0700696 isr |= retrigger;
697 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100698 if (!isr)
699 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100700
Tony Lindgren92105bb2005-09-07 17:20:26 +0100701 gpio_irq = bank->virtual_irq_start;
702 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700703 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800704
Tony Lindgren92105bb2005-09-07 17:20:26 +0100705 if (!(isr & 1))
706 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200707
Cory Maccarrone4318f362010-01-08 10:29:04 -0800708#ifdef CONFIG_ARCH_OMAP1
709 /*
710 * Some chips can't respond to both rising and falling
711 * at the same time. If this irq was requested with
712 * both flags, we need to flip the ICR data for the IRQ
713 * to respond to the IRQ for the opposite direction.
714 * This will be indicated in the bank toggle_mask.
715 */
716 if (bank->toggle_mask & (1 << gpio_index))
717 _toggle_gpio_edge_triggering(bank, gpio_index);
718#endif
719
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100720 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100721 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000722 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700723 /* if bank has any level sensitive GPIO pin interrupt
724 configured, we must unmask the bank interrupt only after
725 handler(s) are executed in order to avoid spurious bank
726 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800727exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700728 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000729 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730}
731
Lennert Buytenheke9191022010-11-29 11:17:17 +0100732static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300733{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100734 unsigned int gpio = d->irq - IH_GPIO_BASE;
735 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700736 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300737
Colin Cross85ec7b92011-06-06 13:38:18 -0700738 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300739 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700740 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300741}
742
Lennert Buytenheke9191022010-11-29 11:17:17 +0100743static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100744{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100745 unsigned int gpio = d->irq - IH_GPIO_BASE;
746 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100747
748 _clear_gpio_irqstatus(bank, gpio);
749}
750
Lennert Buytenheke9191022010-11-29 11:17:17 +0100751static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100752{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100753 unsigned int gpio = d->irq - IH_GPIO_BASE;
754 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700755 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756
Colin Cross85ec7b92011-06-06 13:38:18 -0700757 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100758 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700759 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700760 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100761}
762
Lennert Buytenheke9191022010-11-29 11:17:17 +0100763static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100764{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100765 unsigned int gpio = d->irq - IH_GPIO_BASE;
766 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700767 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100768 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700769 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700770
Colin Cross85ec7b92011-06-06 13:38:18 -0700771 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700772 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700773 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800774
775 /* For level-triggered GPIOs, the clearing must be done after
776 * the HW source is cleared, thus after the handler has run */
777 if (bank->level_mask & irq_mask) {
778 _set_gpio_irqenable(bank, gpio, 0);
779 _clear_gpio_irqstatus(bank, gpio);
780 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100781
Kevin Hilman4de8c752008-01-16 21:56:14 -0800782 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700783 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100784}
785
David Brownelle5c56ed2006-12-06 17:13:59 -0800786static struct irq_chip gpio_irq_chip = {
787 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100788 .irq_shutdown = gpio_irq_shutdown,
789 .irq_ack = gpio_ack_irq,
790 .irq_mask = gpio_mask_irq,
791 .irq_unmask = gpio_unmask_irq,
792 .irq_set_type = gpio_irq_type,
793 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800794};
795
796/*---------------------------------------------------------------------*/
797
798#ifdef CONFIG_ARCH_OMAP1
799
David Brownelle5c56ed2006-12-06 17:13:59 -0800800#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
801
David Brownell11a78b72006-12-06 17:14:11 -0800802#ifdef CONFIG_ARCH_OMAP16XX
803
804#include <linux/platform_device.h>
805
Magnus Damm79ee0312009-07-08 13:22:04 +0200806static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800807{
Magnus Damm79ee0312009-07-08 13:22:04 +0200808 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800809 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800810 void __iomem *mask_reg = bank->base +
811 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800812 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800813
David Brownella6472532008-03-03 04:33:30 -0800814 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800815 bank->saved_wakeup = __raw_readl(mask_reg);
816 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800817 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800818
819 return 0;
820}
821
Magnus Damm79ee0312009-07-08 13:22:04 +0200822static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800823{
Magnus Damm79ee0312009-07-08 13:22:04 +0200824 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800825 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800826 void __iomem *mask_reg = bank->base +
827 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800828 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800829
David Brownella6472532008-03-03 04:33:30 -0800830 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800831 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800832 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800833
834 return 0;
835}
836
Alexey Dobriyan47145212009-12-14 18:00:08 -0800837static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200838 .suspend_noirq = omap_mpuio_suspend_noirq,
839 .resume_noirq = omap_mpuio_resume_noirq,
840};
841
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200842/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800843static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800844 .driver = {
845 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200846 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800847 },
848};
849
850static struct platform_device omap_mpuio_device = {
851 .name = "mpuio",
852 .id = -1,
853 .dev = {
854 .driver = &omap_mpuio_driver.driver,
855 }
856 /* could list the /proc/iomem resources */
857};
858
Charulatha V03e128c2011-05-05 19:58:01 +0530859static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800860{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800861 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700862
David Brownell11a78b72006-12-06 17:14:11 -0800863 if (platform_driver_register(&omap_mpuio_driver) == 0)
864 (void) platform_device_register(&omap_mpuio_device);
865}
866
867#else
Charulatha V03e128c2011-05-05 19:58:01 +0530868static inline void mpuio_init(struct gpio_bank *bank) {}
David Brownell11a78b72006-12-06 17:14:11 -0800869#endif /* 16xx */
870
David Brownelle5c56ed2006-12-06 17:13:59 -0800871#else
872
David Brownelle5c56ed2006-12-06 17:13:59 -0800873#define bank_is_mpuio(bank) 0
Charulatha V03e128c2011-05-05 19:58:01 +0530874static inline void mpuio_init(struct gpio_bank *bank) {}
David Brownelle5c56ed2006-12-06 17:13:59 -0800875
876#endif
877
878/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100879
David Brownell52e31342008-03-03 12:43:23 -0800880/* REVISIT these are stupid implementations! replace by ones that
881 * don't switch on METHOD_* and which mostly avoid spinlocks
882 */
883
884static int gpio_input(struct gpio_chip *chip, unsigned offset)
885{
886 struct gpio_bank *bank;
887 unsigned long flags;
888
889 bank = container_of(chip, struct gpio_bank, chip);
890 spin_lock_irqsave(&bank->lock, flags);
891 _set_gpio_direction(bank, offset, 1);
892 spin_unlock_irqrestore(&bank->lock, flags);
893 return 0;
894}
895
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300896static int gpio_is_input(struct gpio_bank *bank, int mask)
897{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700898 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300899
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300900 return __raw_readl(reg) & mask;
901}
902
David Brownell52e31342008-03-03 12:43:23 -0800903static int gpio_get(struct gpio_chip *chip, unsigned offset)
904{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300905 struct gpio_bank *bank;
906 void __iomem *reg;
907 int gpio;
908 u32 mask;
909
910 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530911 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300912 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700913 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300914
915 if (gpio_is_input(bank, mask))
916 return _get_gpio_datain(bank, gpio);
917 else
918 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800919}
920
921static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
922{
923 struct gpio_bank *bank;
924 unsigned long flags;
925
926 bank = container_of(chip, struct gpio_bank, chip);
927 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700928 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800929 _set_gpio_direction(bank, offset, 0);
930 spin_unlock_irqrestore(&bank->lock, flags);
931 return 0;
932}
933
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700934static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
935 unsigned debounce)
936{
937 struct gpio_bank *bank;
938 unsigned long flags;
939
940 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800941
942 if (!bank->dbck) {
943 bank->dbck = clk_get(bank->dev, "dbclk");
944 if (IS_ERR(bank->dbck))
945 dev_err(bank->dev, "Could not get gpio dbck\n");
946 }
947
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700948 spin_lock_irqsave(&bank->lock, flags);
949 _set_gpio_debounce(bank, offset, debounce);
950 spin_unlock_irqrestore(&bank->lock, flags);
951
952 return 0;
953}
954
David Brownell52e31342008-03-03 12:43:23 -0800955static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
956{
957 struct gpio_bank *bank;
958 unsigned long flags;
959
960 bank = container_of(chip, struct gpio_bank, chip);
961 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700962 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800963 spin_unlock_irqrestore(&bank->lock, flags);
964}
965
David Brownella007b702008-12-10 17:35:25 -0800966static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
967{
968 struct gpio_bank *bank;
969
970 bank = container_of(chip, struct gpio_bank, chip);
971 return bank->virtual_irq_start + offset;
972}
973
David Brownell52e31342008-03-03 12:43:23 -0800974/*---------------------------------------------------------------------*/
975
Tony Lindgren9a748052010-12-07 16:26:56 -0800976static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700977{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700978 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700979 u32 rev;
980
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700981 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700982 return;
983
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700984 rev = __raw_readw(bank->base + bank->regs->revision);
985 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700986 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700987
988 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700989}
990
David Brownell8ba55c52008-02-26 11:10:50 -0800991/* This lock class tells lockdep that GPIO irqs are in a different
992 * category than their parents, so it won't report false recursion.
993 */
994static struct lock_class_key gpio_lock_class;
995
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800996/* TODO: Cleanup cpu_is_* checks */
Charulatha V03e128c2011-05-05 19:58:01 +0530997static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800998{
999 if (cpu_class_is_omap2()) {
1000 if (cpu_is_omap44xx()) {
1001 __raw_writel(0xffffffff, bank->base +
1002 OMAP4_GPIO_IRQSTATUSCLR0);
1003 __raw_writel(0x00000000, bank->base +
1004 OMAP4_GPIO_DEBOUNCENABLE);
1005 /* Initialize interface clk ungated, module enabled */
1006 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1007 } else if (cpu_is_omap34xx()) {
1008 __raw_writel(0x00000000, bank->base +
1009 OMAP24XX_GPIO_IRQENABLE1);
1010 __raw_writel(0xffffffff, bank->base +
1011 OMAP24XX_GPIO_IRQSTATUS1);
1012 __raw_writel(0x00000000, bank->base +
1013 OMAP24XX_GPIO_DEBOUNCE_EN);
1014
1015 /* Initialize interface clk ungated, module enabled */
1016 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001017 }
1018 } else if (cpu_class_is_omap1()) {
Charulatha V03e128c2011-05-05 19:58:01 +05301019 if (bank_is_mpuio(bank)) {
Tony Lindgren5de62b82010-12-07 16:26:58 -08001020 __raw_writew(0xffff, bank->base +
1021 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Charulatha V03e128c2011-05-05 19:58:01 +05301022 mpuio_init(bank);
1023 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001024 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1025 __raw_writew(0xffff, bank->base
1026 + OMAP1510_GPIO_INT_MASK);
1027 __raw_writew(0x0000, bank->base
1028 + OMAP1510_GPIO_INT_STATUS);
1029 }
1030 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1031 __raw_writew(0x0000, bank->base
1032 + OMAP1610_GPIO_IRQENABLE1);
1033 __raw_writew(0xffff, bank->base
1034 + OMAP1610_GPIO_IRQSTATUS1);
1035 __raw_writew(0x0014, bank->base
1036 + OMAP1610_GPIO_SYSCONFIG);
1037
1038 /*
1039 * Enable system clock for GPIO module.
1040 * The CAM_CLK_CTRL *is* really the right place.
1041 */
1042 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1043 ULPD_CAM_CLK_CTRL);
1044 }
1045 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1046 __raw_writel(0xffffffff, bank->base
1047 + OMAP7XX_GPIO_INT_MASK);
1048 __raw_writel(0x00000000, bank->base
1049 + OMAP7XX_GPIO_INT_STATUS);
1050 }
1051 }
1052}
1053
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001054static __init void
1055omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1056 unsigned int num)
1057{
1058 struct irq_chip_generic *gc;
1059 struct irq_chip_type *ct;
1060
1061 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1062 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -07001063 if (!gc) {
1064 dev_err(bank->dev, "Memory alloc failed for gc\n");
1065 return;
1066 }
1067
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001068 ct = gc->chip_types;
1069
1070 /* NOTE: No ack required, reading IRQ status clears it. */
1071 ct->chip.irq_mask = irq_gc_mask_set_bit;
1072 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1073 ct->chip.irq_set_type = gpio_irq_type;
1074 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1075 if (cpu_is_omap16xx())
1076 ct->chip.irq_set_wake = gpio_wake_enable,
1077
1078 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1079 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1080 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1081}
1082
Russell Kingd52b31d2011-05-27 13:56:12 -07001083static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001084{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001085 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001086 static int gpio;
1087
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001088 bank->mod_usage = 0;
1089 /*
1090 * REVISIT eventually switch from OMAP-specific gpio structs
1091 * over to the generic ones
1092 */
1093 bank->chip.request = omap_gpio_request;
1094 bank->chip.free = omap_gpio_free;
1095 bank->chip.direction_input = gpio_input;
1096 bank->chip.get = gpio_get;
1097 bank->chip.direction_output = gpio_output;
1098 bank->chip.set_debounce = gpio_debounce;
1099 bank->chip.set = gpio_set;
1100 bank->chip.to_irq = gpio_2irq;
1101 if (bank_is_mpuio(bank)) {
1102 bank->chip.label = "mpuio";
1103#ifdef CONFIG_ARCH_OMAP16XX
1104 bank->chip.dev = &omap_mpuio_device.dev;
1105#endif
1106 bank->chip.base = OMAP_MPUIO(0);
1107 } else {
1108 bank->chip.label = "gpio";
1109 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001110 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001111 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001112 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001113
1114 gpiochip_add(&bank->chip);
1115
1116 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001117 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001118 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001119 irq_set_chip_data(j, bank);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001120 if (bank_is_mpuio(bank)) {
1121 omap_mpuio_alloc_gc(bank, j, bank->width);
1122 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001123 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001124 irq_set_handler(j, handle_simple_irq);
1125 set_irq_flags(j, IRQF_VALID);
1126 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001127 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001128 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1129 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001130}
1131
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001132static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001133{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001134 struct omap_gpio_platform_data *pdata;
1135 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001136 struct gpio_bank *bank;
Charulatha V03e128c2011-05-05 19:58:01 +05301137 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001138
Charulatha V03e128c2011-05-05 19:58:01 +05301139 if (!pdev->dev.platform_data) {
1140 ret = -EINVAL;
1141 goto err_exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001142 }
1143
Charulatha V03e128c2011-05-05 19:58:01 +05301144 bank = kzalloc(sizeof(struct gpio_bank), GFP_KERNEL);
1145 if (!bank) {
1146 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1147 ret = -ENOMEM;
1148 goto err_exit;
1149 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001150
1151 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1152 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301153 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n",
1154 pdev->id);
1155 ret = -ENODEV;
1156 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001157 }
1158
1159 bank->irq = res->start;
Charulatha V03e128c2011-05-05 19:58:01 +05301160 bank->id = pdev->id;
1161
1162 pdata = pdev->dev.platform_data;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001163 bank->virtual_irq_start = pdata->virtual_irq_start;
1164 bank->method = pdata->bank_type;
1165 bank->dev = &pdev->dev;
1166 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001167 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001168 bank->width = pdata->bank_width;
Charulatha V803a2432011-05-05 17:04:12 +05301169 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Charulatha V0cde8d02011-05-05 20:15:16 +05301170 bank->loses_context = pdata->loses_context;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301171 bank->get_context_loss_count = pdata->get_context_loss_count;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001172 bank->regs = pdata->regs;
1173
1174 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1175 bank->set_dataout = _set_gpio_dataout_reg;
1176 else
1177 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001178
1179 spin_lock_init(&bank->lock);
1180
1181 /* Static mapping, never released */
1182 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1183 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301184 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n",
1185 pdev->id);
1186 ret = -ENODEV;
1187 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001188 }
1189
1190 bank->base = ioremap(res->start, resource_size(res));
1191 if (!bank->base) {
Charulatha V03e128c2011-05-05 19:58:01 +05301192 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n",
1193 pdev->id);
1194 ret = -ENOMEM;
1195 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001196 }
1197
1198 pm_runtime_enable(bank->dev);
1199 pm_runtime_get_sync(bank->dev);
1200
Charulatha V03e128c2011-05-05 19:58:01 +05301201 omap_gpio_mod_init(bank);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001202 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001203 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001204
Charulatha V03e128c2011-05-05 19:58:01 +05301205 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001206
Charulatha V03e128c2011-05-05 19:58:01 +05301207 return ret;
1208
1209err_free:
1210 kfree(bank);
1211err_exit:
1212 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001213}
1214
Tony Lindgren140455f2010-02-12 12:26:48 -08001215#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001216static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001217{
Charulatha V03e128c2011-05-05 19:58:01 +05301218 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001219
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001220 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001221 return 0;
1222
Charulatha V03e128c2011-05-05 19:58:01 +05301223 list_for_each_entry(bank, &omap_gpio_list, node) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001224 void __iomem *wake_status;
1225 void __iomem *wake_clear;
1226 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001227 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001228
1229 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001230#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001231 case METHOD_GPIO_1610:
1232 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1233 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1234 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1235 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001236#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001237#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001238 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001239 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001240 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1241 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1242 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001243#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301244#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001245 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301246 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1247 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1248 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1249 break;
1250#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001251 default:
1252 continue;
1253 }
1254
David Brownella6472532008-03-03 04:33:30 -08001255 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001256 bank->saved_wakeup = __raw_readl(wake_status);
1257 __raw_writel(0xffffffff, wake_clear);
1258 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001259 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001260 }
1261
1262 return 0;
1263}
1264
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001265static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001266{
Charulatha V03e128c2011-05-05 19:58:01 +05301267 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001268
Tero Kristo723fdb72008-11-26 14:35:16 -08001269 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001270 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001271
Charulatha V03e128c2011-05-05 19:58:01 +05301272 list_for_each_entry(bank, &omap_gpio_list, node) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001273 void __iomem *wake_clear;
1274 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001275 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001276
1277 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001278#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001279 case METHOD_GPIO_1610:
1280 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1281 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1282 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001283#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001284#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001285 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001286 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1287 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001288 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001289#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301290#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001291 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301292 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1293 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1294 break;
1295#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001296 default:
1297 continue;
1298 }
1299
David Brownella6472532008-03-03 04:33:30 -08001300 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001301 __raw_writel(0xffffffff, wake_clear);
1302 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001303 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001304 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001305}
1306
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001307static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001308 .suspend = omap_gpio_suspend,
1309 .resume = omap_gpio_resume,
1310};
1311
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001312#endif
1313
Tony Lindgren140455f2010-02-12 12:26:48 -08001314#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001315
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301316static void omap_gpio_save_context(struct gpio_bank *bank);
1317static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001318
Paul Walmsley72e06d02010-12-21 21:05:16 -07001319void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001320{
Charulatha V03e128c2011-05-05 19:58:01 +05301321 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001322
Charulatha V03e128c2011-05-05 19:58:01 +05301323 list_for_each_entry(bank, &omap_gpio_list, node) {
Sanjeev Premica828762010-09-23 18:27:18 -07001324 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001325 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001326
Charulatha V0cde8d02011-05-05 20:15:16 +05301327 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301328 continue;
1329
Kevin Hilman0aed04352010-09-22 16:06:27 -07001330 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001331 clk_disable(bank->dbck);
1332
Paul Walmsley72e06d02010-12-21 21:05:16 -07001333 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001334 continue;
1335
1336 /* If going to OFF, remove triggering for all
1337 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1338 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001339 if (!(bank->enabled_non_wakeup_gpios))
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301340 goto save_gpio_context;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001341
1342 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1343 bank->saved_datain = __raw_readl(bank->base +
1344 OMAP24XX_GPIO_DATAIN);
1345 l1 = __raw_readl(bank->base +
1346 OMAP24XX_GPIO_FALLINGDETECT);
1347 l2 = __raw_readl(bank->base +
1348 OMAP24XX_GPIO_RISINGDETECT);
1349 }
1350
1351 if (cpu_is_omap44xx()) {
1352 bank->saved_datain = __raw_readl(bank->base +
1353 OMAP4_GPIO_DATAIN);
1354 l1 = __raw_readl(bank->base +
1355 OMAP4_GPIO_FALLINGDETECT);
1356 l2 = __raw_readl(bank->base +
1357 OMAP4_GPIO_RISINGDETECT);
1358 }
1359
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001360 bank->saved_fallingdetect = l1;
1361 bank->saved_risingdetect = l2;
1362 l1 &= ~bank->enabled_non_wakeup_gpios;
1363 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001364
1365 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1366 __raw_writel(l1, bank->base +
1367 OMAP24XX_GPIO_FALLINGDETECT);
1368 __raw_writel(l2, bank->base +
1369 OMAP24XX_GPIO_RISINGDETECT);
1370 }
1371
1372 if (cpu_is_omap44xx()) {
1373 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1374 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1375 }
1376
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301377save_gpio_context:
1378 if (bank->get_context_loss_count)
1379 bank->context_loss_count =
1380 bank->get_context_loss_count(bank->dev);
1381
1382 omap_gpio_save_context(bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001383 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001384}
1385
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001386void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001387{
Charulatha V03e128c2011-05-05 19:58:01 +05301388 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001389
Charulatha V03e128c2011-05-05 19:58:01 +05301390 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301391 int context_lost_cnt_after;
Sanjeev Premica828762010-09-23 18:27:18 -07001392 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001393 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001394
Charulatha V0cde8d02011-05-05 20:15:16 +05301395 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301396 continue;
1397
Kevin Hilman0aed04352010-09-22 16:06:27 -07001398 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001399 clk_enable(bank->dbck);
1400
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301401 if (bank->get_context_loss_count) {
1402 context_lost_cnt_after =
1403 bank->get_context_loss_count(bank->dev);
1404 if (context_lost_cnt_after != bank->context_loss_count
1405 || !context_lost_cnt_after)
1406 omap_gpio_restore_context(bank);
1407 }
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001408
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001409 if (!(bank->enabled_non_wakeup_gpios))
1410 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001411
1412 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1413 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001414 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001415 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001416 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001417 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1418 }
1419
1420 if (cpu_is_omap44xx()) {
1421 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301422 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001423 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301424 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001425 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1426 }
1427
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001428 /* Check if any of the non-wakeup interrupt GPIOs have changed
1429 * state. If so, generate an IRQ by software. This is
1430 * horribly racy, but it's the best we can do to work around
1431 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001432 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001433 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001434
1435 /*
1436 * No need to generate IRQs for the rising edge for gpio IRQs
1437 * configured with falling edge only; and vice versa.
1438 */
1439 gen0 = l & bank->saved_fallingdetect;
1440 gen0 &= bank->saved_datain;
1441
1442 gen1 = l & bank->saved_risingdetect;
1443 gen1 &= ~(bank->saved_datain);
1444
1445 /* FIXME: Consider GPIO IRQs with level detections properly! */
1446 gen = l & (~(bank->saved_fallingdetect) &
1447 ~(bank->saved_risingdetect));
1448 /* Consider all GPIO IRQs needed to be updated */
1449 gen |= gen0 | gen1;
1450
1451 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001452 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001453
Sergio Aguirref00d6492010-03-03 16:21:08 +00001454 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001455 old0 = __raw_readl(bank->base +
1456 OMAP24XX_GPIO_LEVELDETECT0);
1457 old1 = __raw_readl(bank->base +
1458 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001459 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001460 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001461 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001462 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001463 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001464 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001465 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001466 OMAP24XX_GPIO_LEVELDETECT1);
1467 }
1468
1469 if (cpu_is_omap44xx()) {
1470 old0 = __raw_readl(bank->base +
1471 OMAP4_GPIO_LEVELDETECT0);
1472 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301473 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001474 __raw_writel(old0 | l, bank->base +
1475 OMAP4_GPIO_LEVELDETECT0);
1476 __raw_writel(old1 | l, bank->base +
1477 OMAP4_GPIO_LEVELDETECT1);
1478 __raw_writel(old0, bank->base +
1479 OMAP4_GPIO_LEVELDETECT0);
1480 __raw_writel(old1, bank->base +
1481 OMAP4_GPIO_LEVELDETECT1);
1482 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001483 }
1484 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001485}
1486
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301487static void omap_gpio_save_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301488{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301489 bank->context.irqenable1 =
1490 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1491 bank->context.irqenable2 =
1492 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1493 bank->context.wake_en =
1494 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1495 bank->context.ctrl = __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1496 bank->context.oe = __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1497 bank->context.leveldetect0 =
1498 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1499 bank->context.leveldetect1 =
1500 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1501 bank->context.risingdetect =
1502 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1503 bank->context.fallingdetect =
1504 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1505 bank->context.dataout =
1506 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301507}
1508
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301509static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301510{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301511 __raw_writel(bank->context.irqenable1,
1512 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1513 __raw_writel(bank->context.irqenable2,
1514 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1515 __raw_writel(bank->context.wake_en,
1516 bank->base + OMAP24XX_GPIO_WAKE_EN);
1517 __raw_writel(bank->context.ctrl, bank->base + OMAP24XX_GPIO_CTRL);
1518 __raw_writel(bank->context.oe, bank->base + OMAP24XX_GPIO_OE);
1519 __raw_writel(bank->context.leveldetect0,
1520 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1521 __raw_writel(bank->context.leveldetect1,
1522 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1523 __raw_writel(bank->context.risingdetect,
1524 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1525 __raw_writel(bank->context.fallingdetect,
1526 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1527 __raw_writel(bank->context.dataout,
1528 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301529}
1530#endif
1531
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001532static struct platform_driver omap_gpio_driver = {
1533 .probe = omap_gpio_probe,
1534 .driver = {
1535 .name = "omap_gpio",
1536 },
1537};
1538
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001539/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001540 * gpio driver register needs to be done before
1541 * machine_init functions access gpio APIs.
1542 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001543 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001544static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001545{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001546 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001547}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001548postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001549
Tony Lindgren92105bb2005-09-07 17:20:26 +01001550static int __init omap_gpio_sysinit(void)
1551{
David Brownell11a78b72006-12-06 17:14:11 -08001552
Tony Lindgren140455f2010-02-12 12:26:48 -08001553#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001554 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1555 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001556#endif
1557
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001558 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001559}
1560
Tony Lindgren92105bb2005-09-07 17:20:26 +01001561arch_initcall(omap_gpio_sysinit);