blob: 501ca3d6a49eb5a0993121b08022e77466fa4f08 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080022#include <linux/slab.h>
23#include <linux/pm_runtime.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/irqs.h>
28#include <mach/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029#include <asm/mach/irq.h>
30
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031struct gpio_bank {
Tony Lindgren9f7065d2009-10-19 15:25:20 -070032 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010033 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010034 u16 irq;
35 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010036 int method;
Tony Lindgren140455f2010-02-12 12:26:48 -080037#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +010038 u32 suspend_wakeup;
39 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080040#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080041 u32 non_wakeup_gpios;
42 u32 enabled_non_wakeup_gpios;
43
44 u32 saved_datain;
45 u32 saved_fallingdetect;
46 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080047 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080048 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010049 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080050 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080051 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080052 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080053 u32 dbck_enable_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080054 struct device *dev;
55 bool dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -080056 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070057 u32 width;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070058
59 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
60
61 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010062};
63
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -080064#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +053065struct omap3_gpio_regs {
Rajendra Nayak40c670f2008-09-26 17:47:48 +053066 u32 irqenable1;
67 u32 irqenable2;
68 u32 wake_en;
69 u32 ctrl;
70 u32 oe;
71 u32 leveldetect0;
72 u32 leveldetect1;
73 u32 risingdetect;
74 u32 fallingdetect;
75 u32 dataout;
Rajendra Nayak40c670f2008-09-26 17:47:48 +053076};
77
78static struct omap3_gpio_regs gpio_context[OMAP34XX_NR_GPIOS];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -080079#endif
80
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080081/*
82 * TODO: Cleanup gpio_bank usage as it is having information
83 * related to all instances of the device
84 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085static struct gpio_bank *gpio_bank;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080086
Varadarajan, Charulathac95d10b2010-12-07 16:26:56 -080087/* TODO: Analyze removing gpio_bank_count usage from driver code */
88int gpio_bank_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010089
Kevin Hilman129fd222011-04-22 07:59:07 -070090#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
91#define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
92
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010093static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
94{
Tony Lindgren92105bb2005-09-07 17:20:26 +010095 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010096 u32 l;
97
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070098 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099 l = __raw_readl(reg);
100 if (is_input)
101 l |= 1 << gpio;
102 else
103 l &= ~(1 << gpio);
104 __raw_writel(l, reg);
105}
106
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700107
108/* set data out value using dedicate set/clear register */
109static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100110{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100111 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700112 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100113
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700114 if (enable)
115 reg += bank->regs->set_dataout;
116 else
117 reg += bank->regs->clr_dataout;
118
119 __raw_writel(l, reg);
120}
121
122/* set data out value using mask register */
123static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
124{
125 void __iomem *reg = bank->base + bank->regs->dataout;
126 u32 gpio_bit = GPIO_BIT(bank, gpio);
127 u32 l;
128
129 l = __raw_readl(reg);
130 if (enable)
131 l |= gpio_bit;
132 else
133 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134 __raw_writel(l, reg);
135}
136
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300137static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100138{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700139 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100140
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700141 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100142}
143
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300144static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
145{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700146 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300147
Kevin Hilman129fd222011-04-22 07:59:07 -0700148 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300149}
150
Tony Lindgren92105bb2005-09-07 17:20:26 +0100151#define MOD_REG_BIT(reg, bit_mask, set) \
152do { \
153 int l = __raw_readl(base + reg); \
154 if (set) l |= bit_mask; \
155 else l &= ~bit_mask; \
156 __raw_writel(l, base + reg); \
157} while(0)
158
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700159/**
160 * _set_gpio_debounce - low level gpio debounce time
161 * @bank: the gpio bank we're acting upon
162 * @gpio: the gpio number on this @gpio
163 * @debounce: debounce time to use
164 *
165 * OMAP's debounce time is in 31us steps so we need
166 * to convert and round up to the closest unit.
167 */
168static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
169 unsigned debounce)
170{
Kevin Hilman9942da02011-04-22 12:02:05 -0700171 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700172 u32 val;
173 u32 l;
174
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800175 if (!bank->dbck_flag)
176 return;
177
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700178 if (debounce < 32)
179 debounce = 0x01;
180 else if (debounce > 7936)
181 debounce = 0xff;
182 else
183 debounce = (debounce / 0x1f) - 1;
184
Kevin Hilman129fd222011-04-22 07:59:07 -0700185 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700186
Kevin Hilman9942da02011-04-22 12:02:05 -0700187 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700188 __raw_writel(debounce, reg);
189
Kevin Hilman9942da02011-04-22 12:02:05 -0700190 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700191 val = __raw_readl(reg);
192
193 if (debounce) {
194 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800195 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700196 } else {
197 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800198 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700199 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300200 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700201
202 __raw_writel(val, reg);
203}
204
Tony Lindgren140455f2010-02-12 12:26:48 -0800205#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700206static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
207 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100208{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800209 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100210 u32 gpio_bit = 1 << gpio;
211
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530212 if (cpu_is_omap44xx()) {
213 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
214 trigger & IRQ_TYPE_LEVEL_LOW);
215 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
216 trigger & IRQ_TYPE_LEVEL_HIGH);
217 MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
218 trigger & IRQ_TYPE_EDGE_RISING);
219 MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
220 trigger & IRQ_TYPE_EDGE_FALLING);
221 } else {
222 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
223 trigger & IRQ_TYPE_LEVEL_LOW);
224 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
225 trigger & IRQ_TYPE_LEVEL_HIGH);
226 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
227 trigger & IRQ_TYPE_EDGE_RISING);
228 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
229 trigger & IRQ_TYPE_EDGE_FALLING);
230 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800231 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530232 if (cpu_is_omap44xx()) {
Colin Cross0622b252011-06-06 13:38:17 -0700233 MOD_REG_BIT(OMAP4_GPIO_IRQWAKEN0, gpio_bit,
234 trigger != 0);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530235 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000236 /*
237 * GPIO wakeup request can only be generated on edge
238 * transitions
239 */
240 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530241 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700242 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530243 else
244 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700245 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530246 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200247 }
248 /* This part needs to be executed always for OMAP34xx */
249 if (cpu_is_omap34xx() || (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 V058af1e2009-11-22 10:11:25 -0800581 if (!cpu_class_is_omap1()) {
582 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700583 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800584 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700585
586 if (cpu_is_omap24xx() || cpu_is_omap34xx())
587 reg += OMAP24XX_GPIO_CTRL;
588 else if (cpu_is_omap44xx())
589 reg += OMAP4_GPIO_CTRL;
590 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800591 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700592 ctrl &= 0xFFFFFFFE;
593 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800594 }
595 bank->mod_usage |= 1 << offset;
596 }
David Brownella6472532008-03-03 04:33:30 -0800597 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100598
599 return 0;
600}
601
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800602static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100603{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800604 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800605 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100606
David Brownella6472532008-03-03 04:33:30 -0800607 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100608#ifdef CONFIG_ARCH_OMAP16XX
609 if (bank->method == METHOD_GPIO_1610) {
610 /* Disable wake-up during idle for dynamic tick */
611 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800612 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100613 }
614#endif
Charulatha V9f096862010-05-14 12:05:27 -0700615#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
616 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100617 /* Disable wake-up during idle for dynamic tick */
618 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800619 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100620 }
621#endif
Charulatha V9f096862010-05-14 12:05:27 -0700622#ifdef CONFIG_ARCH_OMAP4
623 if (bank->method == METHOD_GPIO_44XX) {
624 /* Disable wake-up during idle for dynamic tick */
625 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
626 __raw_writel(1 << offset, reg);
627 }
628#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800629 if (!cpu_class_is_omap1()) {
630 bank->mod_usage &= ~(1 << offset);
631 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700632 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800633 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700634
635 if (cpu_is_omap24xx() || cpu_is_omap34xx())
636 reg += OMAP24XX_GPIO_CTRL;
637 else if (cpu_is_omap44xx())
638 reg += OMAP4_GPIO_CTRL;
639 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800640 /* Module is disabled, clocks are gated */
641 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700642 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800643 }
644 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800645 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800646 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100647}
648
649/*
650 * We need to unmask the GPIO bank interrupt as soon as possible to
651 * avoid missing GPIO interrupts for other lines in the bank.
652 * Then we need to mask-read-clear-unmask the triggered GPIO lines
653 * in the bank to avoid missing nested interrupts for a GPIO line.
654 * If we wait to unmask individual GPIO lines in the bank after the
655 * line's interrupt handler has been run, we may miss some nested
656 * interrupts.
657 */
Russell King10dd5ce2006-11-23 11:41:32 +0000658static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100659{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100660 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100661 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800662 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100663 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700664 u32 retrigger = 0;
665 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000666 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100667
Will Deaconee144182011-02-21 13:46:08 +0000668 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100669
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100670 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700671 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800672
673 if (WARN_ON(!isr_reg))
674 goto exit;
675
Tony Lindgren92105bb2005-09-07 17:20:26 +0100676 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100677 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700678 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100679
Imre Deakea6dedd2006-06-26 16:16:00 -0700680 enabled = _get_gpio_irqbank_mask(bank);
681 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100682
683 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
684 isr &= 0x0000ffff;
685
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800686 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800687 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -0700688 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100689
690 /* clear edge sensitive interrupts before handler(s) are
691 called so that we don't miss any interrupt occurred while
692 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700693 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100694 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700695 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100696
697 /* if there is only edge sensitive GPIO pin interrupts
698 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700699 if (!level_mask && !unmasked) {
700 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000701 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700702 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100703
Imre Deakea6dedd2006-06-26 16:16:00 -0700704 isr |= retrigger;
705 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100706 if (!isr)
707 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100708
Tony Lindgren92105bb2005-09-07 17:20:26 +0100709 gpio_irq = bank->virtual_irq_start;
710 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700711 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800712
Tony Lindgren92105bb2005-09-07 17:20:26 +0100713 if (!(isr & 1))
714 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200715
Cory Maccarrone4318f362010-01-08 10:29:04 -0800716#ifdef CONFIG_ARCH_OMAP1
717 /*
718 * Some chips can't respond to both rising and falling
719 * at the same time. If this irq was requested with
720 * both flags, we need to flip the ICR data for the IRQ
721 * to respond to the IRQ for the opposite direction.
722 * This will be indicated in the bank toggle_mask.
723 */
724 if (bank->toggle_mask & (1 << gpio_index))
725 _toggle_gpio_edge_triggering(bank, gpio_index);
726#endif
727
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100728 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100729 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000730 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700731 /* if bank has any level sensitive GPIO pin interrupt
732 configured, we must unmask the bank interrupt only after
733 handler(s) are executed in order to avoid spurious bank
734 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800735exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700736 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000737 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100738}
739
Lennert Buytenheke9191022010-11-29 11:17:17 +0100740static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300741{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100742 unsigned int gpio = d->irq - IH_GPIO_BASE;
743 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700744 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300745
Colin Cross85ec7b92011-06-06 13:38:18 -0700746 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300747 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700748 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300749}
750
Lennert Buytenheke9191022010-11-29 11:17:17 +0100751static void gpio_ack_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);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100755
756 _clear_gpio_irqstatus(bank, gpio);
757}
758
Lennert Buytenheke9191022010-11-29 11:17:17 +0100759static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100760{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100761 unsigned int gpio = d->irq - IH_GPIO_BASE;
762 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700763 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100764
Colin Cross85ec7b92011-06-06 13:38:18 -0700765 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100766 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700767 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700768 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100769}
770
Lennert Buytenheke9191022010-11-29 11:17:17 +0100771static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100772{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100773 unsigned int gpio = d->irq - IH_GPIO_BASE;
774 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700775 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100776 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700777 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700778
Colin Cross85ec7b92011-06-06 13:38:18 -0700779 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700780 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700781 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800782
783 /* For level-triggered GPIOs, the clearing must be done after
784 * the HW source is cleared, thus after the handler has run */
785 if (bank->level_mask & irq_mask) {
786 _set_gpio_irqenable(bank, gpio, 0);
787 _clear_gpio_irqstatus(bank, gpio);
788 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100789
Kevin Hilman4de8c752008-01-16 21:56:14 -0800790 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700791 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100792}
793
David Brownelle5c56ed2006-12-06 17:13:59 -0800794static struct irq_chip gpio_irq_chip = {
795 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100796 .irq_shutdown = gpio_irq_shutdown,
797 .irq_ack = gpio_ack_irq,
798 .irq_mask = gpio_mask_irq,
799 .irq_unmask = gpio_unmask_irq,
800 .irq_set_type = gpio_irq_type,
801 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800802};
803
804/*---------------------------------------------------------------------*/
805
806#ifdef CONFIG_ARCH_OMAP1
807
David Brownelle5c56ed2006-12-06 17:13:59 -0800808#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
809
David Brownell11a78b72006-12-06 17:14:11 -0800810#ifdef CONFIG_ARCH_OMAP16XX
811
812#include <linux/platform_device.h>
813
Magnus Damm79ee0312009-07-08 13:22:04 +0200814static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800815{
Magnus Damm79ee0312009-07-08 13:22:04 +0200816 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800817 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800818 void __iomem *mask_reg = bank->base +
819 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800820 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800821
David Brownella6472532008-03-03 04:33:30 -0800822 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800823 bank->saved_wakeup = __raw_readl(mask_reg);
824 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800825 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800826
827 return 0;
828}
829
Magnus Damm79ee0312009-07-08 13:22:04 +0200830static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800831{
Magnus Damm79ee0312009-07-08 13:22:04 +0200832 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800833 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800834 void __iomem *mask_reg = bank->base +
835 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800836 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800837
David Brownella6472532008-03-03 04:33:30 -0800838 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800839 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800840 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800841
842 return 0;
843}
844
Alexey Dobriyan47145212009-12-14 18:00:08 -0800845static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200846 .suspend_noirq = omap_mpuio_suspend_noirq,
847 .resume_noirq = omap_mpuio_resume_noirq,
848};
849
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200850/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800851static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800852 .driver = {
853 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200854 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800855 },
856};
857
858static struct platform_device omap_mpuio_device = {
859 .name = "mpuio",
860 .id = -1,
861 .dev = {
862 .driver = &omap_mpuio_driver.driver,
863 }
864 /* could list the /proc/iomem resources */
865};
866
867static inline void mpuio_init(void)
868{
Charulatha Va8be8da2011-04-22 16:38:16 +0530869 struct gpio_bank *bank = &gpio_bank[0];
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800870 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700871
David Brownell11a78b72006-12-06 17:14:11 -0800872 if (platform_driver_register(&omap_mpuio_driver) == 0)
873 (void) platform_device_register(&omap_mpuio_device);
874}
875
876#else
877static inline void mpuio_init(void) {}
878#endif /* 16xx */
879
David Brownelle5c56ed2006-12-06 17:13:59 -0800880#else
881
David Brownelle5c56ed2006-12-06 17:13:59 -0800882#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -0800883static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -0800884
885#endif
886
887/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100888
David Brownell52e31342008-03-03 12:43:23 -0800889/* REVISIT these are stupid implementations! replace by ones that
890 * don't switch on METHOD_* and which mostly avoid spinlocks
891 */
892
893static int gpio_input(struct gpio_chip *chip, unsigned offset)
894{
895 struct gpio_bank *bank;
896 unsigned long flags;
897
898 bank = container_of(chip, struct gpio_bank, chip);
899 spin_lock_irqsave(&bank->lock, flags);
900 _set_gpio_direction(bank, offset, 1);
901 spin_unlock_irqrestore(&bank->lock, flags);
902 return 0;
903}
904
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300905static int gpio_is_input(struct gpio_bank *bank, int mask)
906{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700907 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300908
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300909 return __raw_readl(reg) & mask;
910}
911
David Brownell52e31342008-03-03 12:43:23 -0800912static int gpio_get(struct gpio_chip *chip, unsigned offset)
913{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300914 struct gpio_bank *bank;
915 void __iomem *reg;
916 int gpio;
917 u32 mask;
918
919 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530920 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300921 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700922 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300923
924 if (gpio_is_input(bank, mask))
925 return _get_gpio_datain(bank, gpio);
926 else
927 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800928}
929
930static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
931{
932 struct gpio_bank *bank;
933 unsigned long flags;
934
935 bank = container_of(chip, struct gpio_bank, chip);
936 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700937 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800938 _set_gpio_direction(bank, offset, 0);
939 spin_unlock_irqrestore(&bank->lock, flags);
940 return 0;
941}
942
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700943static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
944 unsigned debounce)
945{
946 struct gpio_bank *bank;
947 unsigned long flags;
948
949 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800950
951 if (!bank->dbck) {
952 bank->dbck = clk_get(bank->dev, "dbclk");
953 if (IS_ERR(bank->dbck))
954 dev_err(bank->dev, "Could not get gpio dbck\n");
955 }
956
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700957 spin_lock_irqsave(&bank->lock, flags);
958 _set_gpio_debounce(bank, offset, debounce);
959 spin_unlock_irqrestore(&bank->lock, flags);
960
961 return 0;
962}
963
David Brownell52e31342008-03-03 12:43:23 -0800964static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
965{
966 struct gpio_bank *bank;
967 unsigned long flags;
968
969 bank = container_of(chip, struct gpio_bank, chip);
970 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700971 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800972 spin_unlock_irqrestore(&bank->lock, flags);
973}
974
David Brownella007b702008-12-10 17:35:25 -0800975static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
976{
977 struct gpio_bank *bank;
978
979 bank = container_of(chip, struct gpio_bank, chip);
980 return bank->virtual_irq_start + offset;
981}
982
David Brownell52e31342008-03-03 12:43:23 -0800983/*---------------------------------------------------------------------*/
984
Tony Lindgren9a748052010-12-07 16:26:56 -0800985static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700986{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700987 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700988 u32 rev;
989
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700990 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700991 return;
992
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700993 rev = __raw_readw(bank->base + bank->regs->revision);
994 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700995 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700996
997 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700998}
999
David Brownell8ba55c52008-02-26 11:10:50 -08001000/* This lock class tells lockdep that GPIO irqs are in a different
1001 * category than their parents, so it won't report false recursion.
1002 */
1003static struct lock_class_key gpio_lock_class;
1004
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001005static inline int init_gpio_info(struct platform_device *pdev)
1006{
1007 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1008 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1009 GFP_KERNEL);
1010 if (!gpio_bank) {
1011 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1012 return -ENOMEM;
1013 }
1014 return 0;
1015}
1016
1017/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001018static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1019{
1020 if (cpu_class_is_omap2()) {
1021 if (cpu_is_omap44xx()) {
1022 __raw_writel(0xffffffff, bank->base +
1023 OMAP4_GPIO_IRQSTATUSCLR0);
1024 __raw_writel(0x00000000, bank->base +
1025 OMAP4_GPIO_DEBOUNCENABLE);
1026 /* Initialize interface clk ungated, module enabled */
1027 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1028 } else if (cpu_is_omap34xx()) {
1029 __raw_writel(0x00000000, bank->base +
1030 OMAP24XX_GPIO_IRQENABLE1);
1031 __raw_writel(0xffffffff, bank->base +
1032 OMAP24XX_GPIO_IRQSTATUS1);
1033 __raw_writel(0x00000000, bank->base +
1034 OMAP24XX_GPIO_DEBOUNCE_EN);
1035
1036 /* Initialize interface clk ungated, module enabled */
1037 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1038 } else if (cpu_is_omap24xx()) {
1039 static const u32 non_wakeup_gpios[] = {
1040 0xe203ffc0, 0x08700040
1041 };
1042 if (id < ARRAY_SIZE(non_wakeup_gpios))
1043 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1044 }
1045 } else if (cpu_class_is_omap1()) {
1046 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001047 __raw_writew(0xffff, bank->base +
1048 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001049 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1050 __raw_writew(0xffff, bank->base
1051 + OMAP1510_GPIO_INT_MASK);
1052 __raw_writew(0x0000, bank->base
1053 + OMAP1510_GPIO_INT_STATUS);
1054 }
1055 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1056 __raw_writew(0x0000, bank->base
1057 + OMAP1610_GPIO_IRQENABLE1);
1058 __raw_writew(0xffff, bank->base
1059 + OMAP1610_GPIO_IRQSTATUS1);
1060 __raw_writew(0x0014, bank->base
1061 + OMAP1610_GPIO_SYSCONFIG);
1062
1063 /*
1064 * Enable system clock for GPIO module.
1065 * The CAM_CLK_CTRL *is* really the right place.
1066 */
1067 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1068 ULPD_CAM_CLK_CTRL);
1069 }
1070 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1071 __raw_writel(0xffffffff, bank->base
1072 + OMAP7XX_GPIO_INT_MASK);
1073 __raw_writel(0x00000000, bank->base
1074 + OMAP7XX_GPIO_INT_STATUS);
1075 }
1076 }
1077}
1078
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001079static __init void
1080omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1081 unsigned int num)
1082{
1083 struct irq_chip_generic *gc;
1084 struct irq_chip_type *ct;
1085
1086 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1087 handle_simple_irq);
1088 ct = gc->chip_types;
1089
1090 /* NOTE: No ack required, reading IRQ status clears it. */
1091 ct->chip.irq_mask = irq_gc_mask_set_bit;
1092 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1093 ct->chip.irq_set_type = gpio_irq_type;
1094 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1095 if (cpu_is_omap16xx())
1096 ct->chip.irq_set_wake = gpio_wake_enable,
1097
1098 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1099 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1100 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1101}
1102
Russell Kingd52b31d2011-05-27 13:56:12 -07001103static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001104{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001105 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001106 static int gpio;
1107
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001108 bank->mod_usage = 0;
1109 /*
1110 * REVISIT eventually switch from OMAP-specific gpio structs
1111 * over to the generic ones
1112 */
1113 bank->chip.request = omap_gpio_request;
1114 bank->chip.free = omap_gpio_free;
1115 bank->chip.direction_input = gpio_input;
1116 bank->chip.get = gpio_get;
1117 bank->chip.direction_output = gpio_output;
1118 bank->chip.set_debounce = gpio_debounce;
1119 bank->chip.set = gpio_set;
1120 bank->chip.to_irq = gpio_2irq;
1121 if (bank_is_mpuio(bank)) {
1122 bank->chip.label = "mpuio";
1123#ifdef CONFIG_ARCH_OMAP16XX
1124 bank->chip.dev = &omap_mpuio_device.dev;
1125#endif
1126 bank->chip.base = OMAP_MPUIO(0);
1127 } else {
1128 bank->chip.label = "gpio";
1129 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001130 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001131 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001132 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001133
1134 gpiochip_add(&bank->chip);
1135
1136 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001137 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001138 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001139 irq_set_chip_data(j, bank);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001140 if (bank_is_mpuio(bank)) {
1141 omap_mpuio_alloc_gc(bank, j, bank->width);
1142 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001143 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001144 irq_set_handler(j, handle_simple_irq);
1145 set_irq_flags(j, IRQF_VALID);
1146 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001147 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001148 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1149 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001150}
1151
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001152static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001153{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001154 static int gpio_init_done;
1155 struct omap_gpio_platform_data *pdata;
1156 struct resource *res;
1157 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001158 struct gpio_bank *bank;
1159
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001160 if (!pdev->dev.platform_data)
1161 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001162
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001163 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001164
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001165 if (!gpio_init_done) {
1166 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001167
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001168 ret = init_gpio_info(pdev);
1169 if (ret)
1170 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001171 }
1172
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001173 id = pdev->id;
1174 bank = &gpio_bank[id];
1175
1176 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1177 if (unlikely(!res)) {
1178 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1179 return -ENODEV;
1180 }
1181
1182 bank->irq = res->start;
1183 bank->virtual_irq_start = pdata->virtual_irq_start;
1184 bank->method = pdata->bank_type;
1185 bank->dev = &pdev->dev;
1186 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001187 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001188 bank->width = pdata->bank_width;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001189
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001190 bank->regs = pdata->regs;
1191
1192 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1193 bank->set_dataout = _set_gpio_dataout_reg;
1194 else
1195 bank->set_dataout = _set_gpio_dataout_mask;
1196
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001197 spin_lock_init(&bank->lock);
1198
1199 /* Static mapping, never released */
1200 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1201 if (unlikely(!res)) {
1202 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1203 return -ENODEV;
1204 }
1205
1206 bank->base = ioremap(res->start, resource_size(res));
1207 if (!bank->base) {
1208 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1209 return -ENOMEM;
1210 }
1211
1212 pm_runtime_enable(bank->dev);
1213 pm_runtime_get_sync(bank->dev);
1214
1215 omap_gpio_mod_init(bank, id);
1216 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001217 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001218
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001219 if (!gpio_init_done)
1220 gpio_init_done = 1;
1221
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001222 return 0;
1223}
1224
Tony Lindgren140455f2010-02-12 12:26:48 -08001225#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001226static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001227{
1228 int i;
1229
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001230 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001231 return 0;
1232
1233 for (i = 0; i < gpio_bank_count; i++) {
1234 struct gpio_bank *bank = &gpio_bank[i];
1235 void __iomem *wake_status;
1236 void __iomem *wake_clear;
1237 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001238 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001239
1240 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001241#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001242 case METHOD_GPIO_1610:
1243 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1244 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1245 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1246 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001247#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001248#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001249 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001250 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001251 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1252 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1253 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001254#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301255#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001256 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301257 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1258 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1259 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1260 break;
1261#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001262 default:
1263 continue;
1264 }
1265
David Brownella6472532008-03-03 04:33:30 -08001266 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001267 bank->saved_wakeup = __raw_readl(wake_status);
1268 __raw_writel(0xffffffff, wake_clear);
1269 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001270 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001271 }
1272
1273 return 0;
1274}
1275
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001276static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001277{
1278 int i;
1279
Tero Kristo723fdb72008-11-26 14:35:16 -08001280 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001281 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001282
1283 for (i = 0; i < gpio_bank_count; i++) {
1284 struct gpio_bank *bank = &gpio_bank[i];
1285 void __iomem *wake_clear;
1286 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001287 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001288
1289 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001290#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001291 case METHOD_GPIO_1610:
1292 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1293 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1294 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001295#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001296#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001297 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001298 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1299 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001300 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001301#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301302#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001303 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301304 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1305 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1306 break;
1307#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001308 default:
1309 continue;
1310 }
1311
David Brownella6472532008-03-03 04:33:30 -08001312 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001313 __raw_writel(0xffffffff, wake_clear);
1314 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001315 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001316 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001317}
1318
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001319static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001320 .suspend = omap_gpio_suspend,
1321 .resume = omap_gpio_resume,
1322};
1323
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001324#endif
1325
Tony Lindgren140455f2010-02-12 12:26:48 -08001326#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001327
1328static int workaround_enabled;
1329
Paul Walmsley72e06d02010-12-21 21:05:16 -07001330void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001331{
1332 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001333 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001334
Tero Kristoa118b5f2008-12-22 14:27:12 +02001335 if (cpu_is_omap34xx())
1336 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001337
Tero Kristoa118b5f2008-12-22 14:27:12 +02001338 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001339 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001340 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001341 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001342
Kevin Hilman0aed04352010-09-22 16:06:27 -07001343 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001344 clk_disable(bank->dbck);
1345
Paul Walmsley72e06d02010-12-21 21:05:16 -07001346 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001347 continue;
1348
1349 /* If going to OFF, remove triggering for all
1350 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1351 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001352 if (!(bank->enabled_non_wakeup_gpios))
1353 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001354
1355 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1356 bank->saved_datain = __raw_readl(bank->base +
1357 OMAP24XX_GPIO_DATAIN);
1358 l1 = __raw_readl(bank->base +
1359 OMAP24XX_GPIO_FALLINGDETECT);
1360 l2 = __raw_readl(bank->base +
1361 OMAP24XX_GPIO_RISINGDETECT);
1362 }
1363
1364 if (cpu_is_omap44xx()) {
1365 bank->saved_datain = __raw_readl(bank->base +
1366 OMAP4_GPIO_DATAIN);
1367 l1 = __raw_readl(bank->base +
1368 OMAP4_GPIO_FALLINGDETECT);
1369 l2 = __raw_readl(bank->base +
1370 OMAP4_GPIO_RISINGDETECT);
1371 }
1372
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001373 bank->saved_fallingdetect = l1;
1374 bank->saved_risingdetect = l2;
1375 l1 &= ~bank->enabled_non_wakeup_gpios;
1376 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001377
1378 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1379 __raw_writel(l1, bank->base +
1380 OMAP24XX_GPIO_FALLINGDETECT);
1381 __raw_writel(l2, bank->base +
1382 OMAP24XX_GPIO_RISINGDETECT);
1383 }
1384
1385 if (cpu_is_omap44xx()) {
1386 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1387 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1388 }
1389
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001390 c++;
1391 }
1392 if (!c) {
1393 workaround_enabled = 0;
1394 return;
1395 }
1396 workaround_enabled = 1;
1397}
1398
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001399void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001400{
1401 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001402 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001403
Tero Kristoa118b5f2008-12-22 14:27:12 +02001404 if (cpu_is_omap34xx())
1405 min = 1;
1406 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001407 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001408 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001409 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001410
Kevin Hilman0aed04352010-09-22 16:06:27 -07001411 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001412 clk_enable(bank->dbck);
1413
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001414 if (!workaround_enabled)
1415 continue;
1416
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001417 if (!(bank->enabled_non_wakeup_gpios))
1418 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001419
1420 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1421 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001422 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001423 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001424 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001425 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1426 }
1427
1428 if (cpu_is_omap44xx()) {
1429 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301430 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001431 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301432 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001433 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1434 }
1435
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001436 /* Check if any of the non-wakeup interrupt GPIOs have changed
1437 * state. If so, generate an IRQ by software. This is
1438 * horribly racy, but it's the best we can do to work around
1439 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001440 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001441 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001442
1443 /*
1444 * No need to generate IRQs for the rising edge for gpio IRQs
1445 * configured with falling edge only; and vice versa.
1446 */
1447 gen0 = l & bank->saved_fallingdetect;
1448 gen0 &= bank->saved_datain;
1449
1450 gen1 = l & bank->saved_risingdetect;
1451 gen1 &= ~(bank->saved_datain);
1452
1453 /* FIXME: Consider GPIO IRQs with level detections properly! */
1454 gen = l & (~(bank->saved_fallingdetect) &
1455 ~(bank->saved_risingdetect));
1456 /* Consider all GPIO IRQs needed to be updated */
1457 gen |= gen0 | gen1;
1458
1459 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001460 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001461
Sergio Aguirref00d6492010-03-03 16:21:08 +00001462 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001463 old0 = __raw_readl(bank->base +
1464 OMAP24XX_GPIO_LEVELDETECT0);
1465 old1 = __raw_readl(bank->base +
1466 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001467 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001468 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001469 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001470 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001471 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001472 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001473 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001474 OMAP24XX_GPIO_LEVELDETECT1);
1475 }
1476
1477 if (cpu_is_omap44xx()) {
1478 old0 = __raw_readl(bank->base +
1479 OMAP4_GPIO_LEVELDETECT0);
1480 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301481 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001482 __raw_writel(old0 | l, bank->base +
1483 OMAP4_GPIO_LEVELDETECT0);
1484 __raw_writel(old1 | l, bank->base +
1485 OMAP4_GPIO_LEVELDETECT1);
1486 __raw_writel(old0, bank->base +
1487 OMAP4_GPIO_LEVELDETECT0);
1488 __raw_writel(old1, bank->base +
1489 OMAP4_GPIO_LEVELDETECT1);
1490 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001491 }
1492 }
1493
1494}
1495
Tony Lindgren92105bb2005-09-07 17:20:26 +01001496#endif
1497
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001498#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301499/* save the registers of bank 2-6 */
1500void omap_gpio_save_context(void)
1501{
1502 int i;
1503
1504 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1505 for (i = 1; i < gpio_bank_count; i++) {
1506 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301507 gpio_context[i].irqenable1 =
1508 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1509 gpio_context[i].irqenable2 =
1510 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1511 gpio_context[i].wake_en =
1512 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1513 gpio_context[i].ctrl =
1514 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1515 gpio_context[i].oe =
1516 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1517 gpio_context[i].leveldetect0 =
1518 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1519 gpio_context[i].leveldetect1 =
1520 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1521 gpio_context[i].risingdetect =
1522 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1523 gpio_context[i].fallingdetect =
1524 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1525 gpio_context[i].dataout =
1526 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301527 }
1528}
1529
1530/* restore the required registers of bank 2-6 */
1531void omap_gpio_restore_context(void)
1532{
1533 int i;
1534
1535 for (i = 1; i < gpio_bank_count; i++) {
1536 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301537 __raw_writel(gpio_context[i].irqenable1,
1538 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1539 __raw_writel(gpio_context[i].irqenable2,
1540 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1541 __raw_writel(gpio_context[i].wake_en,
1542 bank->base + OMAP24XX_GPIO_WAKE_EN);
1543 __raw_writel(gpio_context[i].ctrl,
1544 bank->base + OMAP24XX_GPIO_CTRL);
1545 __raw_writel(gpio_context[i].oe,
1546 bank->base + OMAP24XX_GPIO_OE);
1547 __raw_writel(gpio_context[i].leveldetect0,
1548 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1549 __raw_writel(gpio_context[i].leveldetect1,
1550 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1551 __raw_writel(gpio_context[i].risingdetect,
1552 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1553 __raw_writel(gpio_context[i].fallingdetect,
1554 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1555 __raw_writel(gpio_context[i].dataout,
1556 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301557 }
1558}
1559#endif
1560
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001561static struct platform_driver omap_gpio_driver = {
1562 .probe = omap_gpio_probe,
1563 .driver = {
1564 .name = "omap_gpio",
1565 },
1566};
1567
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001568/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001569 * gpio driver register needs to be done before
1570 * machine_init functions access gpio APIs.
1571 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001572 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001573static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001574{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001575 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001576}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001577postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001578
Tony Lindgren92105bb2005-09-07 17:20:26 +01001579static int __init omap_gpio_sysinit(void)
1580{
David Brownell11a78b72006-12-06 17:14:11 -08001581 mpuio_init();
1582
Tony Lindgren140455f2010-02-12 12:26:48 -08001583#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001584 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1585 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001586#endif
1587
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001588 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001589}
1590
Tony Lindgren92105bb2005-09-07 17:20:26 +01001591arch_initcall(omap_gpio_sysinit);