blob: 945642143e1b3d3090620a63768869730287582b [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 inline int gpio_valid(int gpio)
94{
95 if (gpio < 0)
96 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -080097 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +030098 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099 return -1;
100 return 0;
101 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100102 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100103 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100104 if ((cpu_is_omap16xx()) && gpio < 64)
105 return 0;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700106 if (cpu_is_omap7xx() && gpio < 192)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100107 return 0;
Tony Lindgren25d6f632010-08-02 14:21:39 +0300108 if (cpu_is_omap2420() && gpio < 128)
109 return 0;
110 if (cpu_is_omap2430() && gpio < 160)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100111 return 0;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700112 if ((cpu_is_omap34xx() || cpu_is_omap44xx()) && gpio < 192)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800113 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100114 return -1;
115}
116
117static int check_gpio(int gpio)
118{
Roel Kluind32b20f2009-11-17 14:39:03 -0800119 if (unlikely(gpio_valid(gpio) < 0)) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100120 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
121 dump_stack();
122 return -1;
123 }
124 return 0;
125}
126
127static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
128{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100129 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130 u32 l;
131
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700132 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133 l = __raw_readl(reg);
134 if (is_input)
135 l |= 1 << gpio;
136 else
137 l &= ~(1 << gpio);
138 __raw_writel(l, reg);
139}
140
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700141
142/* set data out value using dedicate set/clear register */
143static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100144{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100145 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700146 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100147
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700148 if (enable)
149 reg += bank->regs->set_dataout;
150 else
151 reg += bank->regs->clr_dataout;
152
153 __raw_writel(l, reg);
154}
155
156/* set data out value using mask register */
157static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
158{
159 void __iomem *reg = bank->base + bank->regs->dataout;
160 u32 gpio_bit = GPIO_BIT(bank, gpio);
161 u32 l;
162
163 l = __raw_readl(reg);
164 if (enable)
165 l |= gpio_bit;
166 else
167 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100168 __raw_writel(l, reg);
169}
170
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300171static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100172{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700173 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100174
175 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800176 return -EINVAL;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700177
178 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100179}
180
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300181static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
182{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700183 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300184
185 if (check_gpio(gpio) < 0)
186 return -EINVAL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300187
Kevin Hilman129fd222011-04-22 07:59:07 -0700188 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300189}
190
Tony Lindgren92105bb2005-09-07 17:20:26 +0100191#define MOD_REG_BIT(reg, bit_mask, set) \
192do { \
193 int l = __raw_readl(base + reg); \
194 if (set) l |= bit_mask; \
195 else l &= ~bit_mask; \
196 __raw_writel(l, base + reg); \
197} while(0)
198
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700199/**
200 * _set_gpio_debounce - low level gpio debounce time
201 * @bank: the gpio bank we're acting upon
202 * @gpio: the gpio number on this @gpio
203 * @debounce: debounce time to use
204 *
205 * OMAP's debounce time is in 31us steps so we need
206 * to convert and round up to the closest unit.
207 */
208static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
209 unsigned debounce)
210{
211 void __iomem *reg = bank->base;
212 u32 val;
213 u32 l;
214
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800215 if (!bank->dbck_flag)
216 return;
217
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700218 if (debounce < 32)
219 debounce = 0x01;
220 else if (debounce > 7936)
221 debounce = 0xff;
222 else
223 debounce = (debounce / 0x1f) - 1;
224
Kevin Hilman129fd222011-04-22 07:59:07 -0700225 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700226
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800227 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700228 reg += OMAP4_GPIO_DEBOUNCINGTIME;
229 else
230 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
231
232 __raw_writel(debounce, reg);
233
234 reg = bank->base;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800235 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700236 reg += OMAP4_GPIO_DEBOUNCENABLE;
237 else
238 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
239
240 val = __raw_readl(reg);
241
242 if (debounce) {
243 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800244 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700245 } else {
246 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800247 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700248 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300249 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700250
251 __raw_writel(val, reg);
252}
253
Tony Lindgren140455f2010-02-12 12:26:48 -0800254#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700255static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
256 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100257{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800258 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100259 u32 gpio_bit = 1 << gpio;
260
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530261 if (cpu_is_omap44xx()) {
262 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
263 trigger & IRQ_TYPE_LEVEL_LOW);
264 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
265 trigger & IRQ_TYPE_LEVEL_HIGH);
266 MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
267 trigger & IRQ_TYPE_EDGE_RISING);
268 MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
269 trigger & IRQ_TYPE_EDGE_FALLING);
270 } else {
271 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
272 trigger & IRQ_TYPE_LEVEL_LOW);
273 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
274 trigger & IRQ_TYPE_LEVEL_HIGH);
275 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
276 trigger & IRQ_TYPE_EDGE_RISING);
277 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
278 trigger & IRQ_TYPE_EDGE_FALLING);
279 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800280 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530281 if (cpu_is_omap44xx()) {
Colin Cross0622b252011-06-06 13:38:17 -0700282 MOD_REG_BIT(OMAP4_GPIO_IRQWAKEN0, gpio_bit,
283 trigger != 0);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530284 } else {
Chunqiu Wang699117a2009-06-24 17:13:39 +0000285 /*
286 * GPIO wakeup request can only be generated on edge
287 * transitions
288 */
289 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530290 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700291 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530292 else
293 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700294 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530295 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200296 }
297 /* This part needs to be executed always for OMAP34xx */
298 if (cpu_is_omap34xx() || (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a2009-06-24 17:13:39 +0000299 /*
300 * Log the edge gpio and manually trigger the IRQ
301 * after resume if the input level changes
302 * to avoid irq lost during PER RET/OFF mode
303 * Applies for omap2 non-wakeup gpio and all omap3 gpios
304 */
305 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800306 bank->enabled_non_wakeup_gpios |= gpio_bit;
307 else
308 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
309 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700310
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530311 if (cpu_is_omap44xx()) {
312 bank->level_mask =
313 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
314 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
315 } else {
316 bank->level_mask =
317 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
318 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
319 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100320}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800321#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100322
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800323#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800324/*
325 * This only applies to chips that can't do both rising and falling edge
326 * detection at once. For all other chips, this function is a noop.
327 */
328static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
329{
330 void __iomem *reg = bank->base;
331 u32 l = 0;
332
333 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800334 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800335 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800336 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800337#ifdef CONFIG_ARCH_OMAP15XX
338 case METHOD_GPIO_1510:
339 reg += OMAP1510_GPIO_INT_CONTROL;
340 break;
341#endif
342#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
343 case METHOD_GPIO_7XX:
344 reg += OMAP7XX_GPIO_INT_CONTROL;
345 break;
346#endif
347 default:
348 return;
349 }
350
351 l = __raw_readl(reg);
352 if ((l >> gpio) & 1)
353 l &= ~(1 << gpio);
354 else
355 l |= 1 << gpio;
356
357 __raw_writel(l, reg);
358}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800359#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800360
Tony Lindgren92105bb2005-09-07 17:20:26 +0100361static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
362{
363 void __iomem *reg = bank->base;
364 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100365
366 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800367#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100368 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800369 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100370 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000371 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800372 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100373 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100374 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100375 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100376 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100377 else
378 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100379 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800380#endif
381#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100382 case METHOD_GPIO_1510:
383 reg += OMAP1510_GPIO_INT_CONTROL;
384 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000385 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800386 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100387 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100388 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100389 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100390 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100391 else
392 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100393 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800394#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800395#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100396 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100397 if (gpio & 0x08)
398 reg += OMAP1610_GPIO_EDGE_CTRL2;
399 else
400 reg += OMAP1610_GPIO_EDGE_CTRL1;
401 gpio &= 0x07;
402 l = __raw_readl(reg);
403 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100404 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100405 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100406 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100407 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800408 if (trigger)
409 /* Enable wake-up during idle for dynamic tick */
410 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
411 else
412 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100413 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800414#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100415#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100416 case METHOD_GPIO_7XX:
417 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700418 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000419 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800420 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700421 if (trigger & IRQ_TYPE_EDGE_RISING)
422 l |= 1 << gpio;
423 else if (trigger & IRQ_TYPE_EDGE_FALLING)
424 l &= ~(1 << gpio);
425 else
426 goto bad;
427 break;
428#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800429#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100430 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800431 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800432 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200433 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800434#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100435 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100436 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100437 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100438 __raw_writel(l, reg);
439 return 0;
440bad:
441 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100442}
443
Lennert Buytenheke9191022010-11-29 11:17:17 +0100444static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100445{
446 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100447 unsigned gpio;
448 int retval;
David Brownella6472532008-03-03 04:33:30 -0800449 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100450
Lennert Buytenheke9191022010-11-29 11:17:17 +0100451 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
452 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100453 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100454 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100455
456 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100457 return -EINVAL;
458
David Brownelle5c56ed2006-12-06 17:13:59 -0800459 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100460 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800461
462 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800463 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800464 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100465 return -EINVAL;
466
Lennert Buytenheke9191022010-11-29 11:17:17 +0100467 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800468 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700469 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800470 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800471
472 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100473 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800474 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100475 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800476
Tony Lindgren92105bb2005-09-07 17:20:26 +0100477 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100478}
479
480static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
481{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100482 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100483
484 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800485#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100486 case METHOD_GPIO_1510:
487 reg += OMAP1510_GPIO_INT_STATUS;
488 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800489#endif
490#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100491 case METHOD_GPIO_1610:
492 reg += OMAP1610_GPIO_IRQSTATUS1;
493 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800494#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100495#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100496 case METHOD_GPIO_7XX:
497 reg += OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700498 break;
499#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800500#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100501 case METHOD_GPIO_24XX:
502 reg += OMAP24XX_GPIO_IRQSTATUS1;
503 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800504#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530505#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800506 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530507 reg += OMAP4_GPIO_IRQSTATUS0;
508 break;
509#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800511 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100512 return;
513 }
514 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300515
516 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800517 if (cpu_is_omap24xx() || cpu_is_omap34xx())
518 reg = bank->base + OMAP24XX_GPIO_IRQSTATUS2;
519 else if (cpu_is_omap44xx())
520 reg = bank->base + OMAP4_GPIO_IRQSTATUS1;
521
Kevin Hilmandf3c8512011-04-21 09:08:15 -0700522 if (cpu_is_omap24xx() || cpu_is_omap34xx() || cpu_is_omap44xx())
Roger Quadrosbedfd152009-04-23 11:10:50 -0700523 __raw_writel(gpio_mask, reg);
524
525 /* Flush posted write for the irq status to avoid spurious interrupts */
526 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100527}
528
529static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
530{
Kevin Hilman129fd222011-04-22 07:59:07 -0700531 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100532}
533
Imre Deakea6dedd2006-06-26 16:16:00 -0700534static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
535{
536 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700537 int inv = 0;
538 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700539 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700540
541 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800542#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700543 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800544 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Imre Deak99c47702006-06-26 16:16:07 -0700545 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700546 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800547#endif
548#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700549 case METHOD_GPIO_1510:
550 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700551 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700552 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800553#endif
554#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700555 case METHOD_GPIO_1610:
556 reg += OMAP1610_GPIO_IRQENABLE1;
557 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800558#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100559#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100560 case METHOD_GPIO_7XX:
561 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700562 inv = 1;
563 break;
564#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800565#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Imre Deakea6dedd2006-06-26 16:16:00 -0700566 case METHOD_GPIO_24XX:
567 reg += OMAP24XX_GPIO_IRQENABLE1;
568 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800569#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530570#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800571 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530572 reg += OMAP4_GPIO_IRQSTATUSSET0;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530573 break;
574#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700575 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800576 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700577 return 0;
578 }
579
Imre Deak99c47702006-06-26 16:16:07 -0700580 l = __raw_readl(reg);
581 if (inv)
582 l = ~l;
583 l &= mask;
584 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700585}
586
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100587static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
588{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100589 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100590 u32 l;
591
592 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800593#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100594 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800595 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100596 l = __raw_readl(reg);
597 if (enable)
598 l &= ~(gpio_mask);
599 else
600 l |= gpio_mask;
601 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800602#endif
603#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100604 case METHOD_GPIO_1510:
605 reg += OMAP1510_GPIO_INT_MASK;
606 l = __raw_readl(reg);
607 if (enable)
608 l &= ~(gpio_mask);
609 else
610 l |= gpio_mask;
611 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800612#endif
613#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100614 case METHOD_GPIO_1610:
615 if (enable)
616 reg += OMAP1610_GPIO_SET_IRQENABLE1;
617 else
618 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
619 l = gpio_mask;
620 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800621#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100622#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100623 case METHOD_GPIO_7XX:
624 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700625 l = __raw_readl(reg);
626 if (enable)
627 l &= ~(gpio_mask);
628 else
629 l |= gpio_mask;
630 break;
631#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800632#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100633 case METHOD_GPIO_24XX:
634 if (enable)
635 reg += OMAP24XX_GPIO_SETIRQENABLE1;
636 else
637 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
638 l = gpio_mask;
639 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800640#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530641#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800642 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530643 if (enable)
644 reg += OMAP4_GPIO_IRQSTATUSSET0;
645 else
646 reg += OMAP4_GPIO_IRQSTATUSCLR0;
647 l = gpio_mask;
648 break;
649#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100650 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800651 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100652 return;
653 }
654 __raw_writel(l, reg);
655}
656
657static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
658{
Kevin Hilman129fd222011-04-22 07:59:07 -0700659 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio), enable);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100660}
661
Tony Lindgren92105bb2005-09-07 17:20:26 +0100662/*
663 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
664 * 1510 does not seem to have a wake-up register. If JTAG is connected
665 * to the target, system will wake up always on GPIO events. While
666 * system is running all registered GPIO interrupts need to have wake-up
667 * enabled. When system is suspended, only selected GPIO interrupts need
668 * to have wake-up enabled.
669 */
670static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
671{
Tony Lindgren4cc64202010-01-08 10:29:05 -0800672 unsigned long uninitialized_var(flags);
David Brownella6472532008-03-03 04:33:30 -0800673
Tony Lindgren92105bb2005-09-07 17:20:26 +0100674 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800675#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800676 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100677 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800678 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700679 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100680 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700681 else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100682 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800683 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100684 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800685#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800686#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800687 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800688 case METHOD_GPIO_44XX:
David Brownell11a78b72006-12-06 17:14:11 -0800689 if (bank->non_wakeup_gpios & (1 << gpio)) {
690 printk(KERN_ERR "Unable to modify wakeup on "
691 "non-wakeup GPIO%d\n",
Kevin Hilmand5f46242011-04-21 09:23:00 -0700692 (bank - gpio_bank) * bank->width + gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800693 return -EINVAL;
694 }
David Brownella6472532008-03-03 04:33:30 -0800695 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700696 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800697 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700698 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800699 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800700 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800701 return 0;
702#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100703 default:
704 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
705 bank->method);
706 return -EINVAL;
707 }
708}
709
Tony Lindgren4196dd62006-09-25 12:41:38 +0300710static void _reset_gpio(struct gpio_bank *bank, int gpio)
711{
Kevin Hilman129fd222011-04-22 07:59:07 -0700712 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300713 _set_gpio_irqenable(bank, gpio, 0);
714 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700715 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300716}
717
Tony Lindgren92105bb2005-09-07 17:20:26 +0100718/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100719static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100720{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100721 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100722 struct gpio_bank *bank;
723 int retval;
724
725 if (check_gpio(gpio) < 0)
726 return -ENODEV;
Lennert Buytenheke9191022010-11-29 11:17:17 +0100727 bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700728 retval = _set_gpio_wakeup(bank, GPIO_INDEX(bank, gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100729
730 return retval;
731}
732
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800733static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100734{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800735 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800736 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100737
David Brownella6472532008-03-03 04:33:30 -0800738 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100739
Tony Lindgren4196dd62006-09-25 12:41:38 +0300740 /* Set trigger to none. You need to enable the desired trigger with
741 * request_irq() or set_irq_type().
742 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800743 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100744
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000745#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100746 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100747 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100748
Tony Lindgren92105bb2005-09-07 17:20:26 +0100749 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100750 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800751 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100752 }
753#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800754 if (!cpu_class_is_omap1()) {
755 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700756 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800757 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700758
759 if (cpu_is_omap24xx() || cpu_is_omap34xx())
760 reg += OMAP24XX_GPIO_CTRL;
761 else if (cpu_is_omap44xx())
762 reg += OMAP4_GPIO_CTRL;
763 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800764 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700765 ctrl &= 0xFFFFFFFE;
766 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800767 }
768 bank->mod_usage |= 1 << offset;
769 }
David Brownella6472532008-03-03 04:33:30 -0800770 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100771
772 return 0;
773}
774
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800775static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100776{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800777 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800778 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100779
David Brownella6472532008-03-03 04:33:30 -0800780 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100781#ifdef CONFIG_ARCH_OMAP16XX
782 if (bank->method == METHOD_GPIO_1610) {
783 /* Disable wake-up during idle for dynamic tick */
784 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800785 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100786 }
787#endif
Charulatha V9f096862010-05-14 12:05:27 -0700788#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
789 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100790 /* Disable wake-up during idle for dynamic tick */
791 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800792 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100793 }
794#endif
Charulatha V9f096862010-05-14 12:05:27 -0700795#ifdef CONFIG_ARCH_OMAP4
796 if (bank->method == METHOD_GPIO_44XX) {
797 /* Disable wake-up during idle for dynamic tick */
798 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
799 __raw_writel(1 << offset, reg);
800 }
801#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800802 if (!cpu_class_is_omap1()) {
803 bank->mod_usage &= ~(1 << offset);
804 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700805 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800806 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700807
808 if (cpu_is_omap24xx() || cpu_is_omap34xx())
809 reg += OMAP24XX_GPIO_CTRL;
810 else if (cpu_is_omap44xx())
811 reg += OMAP4_GPIO_CTRL;
812 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800813 /* Module is disabled, clocks are gated */
814 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700815 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800816 }
817 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800818 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800819 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100820}
821
822/*
823 * We need to unmask the GPIO bank interrupt as soon as possible to
824 * avoid missing GPIO interrupts for other lines in the bank.
825 * Then we need to mask-read-clear-unmask the triggered GPIO lines
826 * in the bank to avoid missing nested interrupts for a GPIO line.
827 * If we wait to unmask individual GPIO lines in the bank after the
828 * line's interrupt handler has been run, we may miss some nested
829 * interrupts.
830 */
Russell King10dd5ce2006-11-23 11:41:32 +0000831static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100832{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100833 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100834 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800835 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100836 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700837 u32 retrigger = 0;
838 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000839 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100840
Will Deaconee144182011-02-21 13:46:08 +0000841 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100842
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100843 bank = irq_get_handler_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -0800844#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100845 if (bank->method == METHOD_MPUIO)
Tony Lindgren5de62b82010-12-07 16:26:58 -0800846 isr_reg = bank->base +
847 OMAP_MPUIO_GPIO_INT / bank->stride;
David Brownelle5c56ed2006-12-06 17:13:59 -0800848#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000849#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100850 if (bank->method == METHOD_GPIO_1510)
851 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
852#endif
853#if defined(CONFIG_ARCH_OMAP16XX)
854 if (bank->method == METHOD_GPIO_1610)
855 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
856#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100857#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100858 if (bank->method == METHOD_GPIO_7XX)
859 isr_reg = bank->base + OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700860#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800861#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100862 if (bank->method == METHOD_GPIO_24XX)
863 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
864#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530865#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800866 if (bank->method == METHOD_GPIO_44XX)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530867 isr_reg = bank->base + OMAP4_GPIO_IRQSTATUS0;
868#endif
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800869
870 if (WARN_ON(!isr_reg))
871 goto exit;
872
Tony Lindgren92105bb2005-09-07 17:20:26 +0100873 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100874 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700875 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100876
Imre Deakea6dedd2006-06-26 16:16:00 -0700877 enabled = _get_gpio_irqbank_mask(bank);
878 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100879
880 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
881 isr &= 0x0000ffff;
882
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800883 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800884 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -0700885 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100886
887 /* clear edge sensitive interrupts before handler(s) are
888 called so that we don't miss any interrupt occurred while
889 executing them */
890 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
891 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
892 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
893
894 /* if there is only edge sensitive GPIO pin interrupts
895 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700896 if (!level_mask && !unmasked) {
897 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000898 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700899 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100900
Imre Deakea6dedd2006-06-26 16:16:00 -0700901 isr |= retrigger;
902 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100903 if (!isr)
904 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100905
Tony Lindgren92105bb2005-09-07 17:20:26 +0100906 gpio_irq = bank->virtual_irq_start;
907 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700908 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800909
Tony Lindgren92105bb2005-09-07 17:20:26 +0100910 if (!(isr & 1))
911 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200912
Cory Maccarrone4318f362010-01-08 10:29:04 -0800913#ifdef CONFIG_ARCH_OMAP1
914 /*
915 * Some chips can't respond to both rising and falling
916 * at the same time. If this irq was requested with
917 * both flags, we need to flip the ICR data for the IRQ
918 * to respond to the IRQ for the opposite direction.
919 * This will be indicated in the bank toggle_mask.
920 */
921 if (bank->toggle_mask & (1 << gpio_index))
922 _toggle_gpio_edge_triggering(bank, gpio_index);
923#endif
924
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100925 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100926 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000927 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700928 /* if bank has any level sensitive GPIO pin interrupt
929 configured, we must unmask the bank interrupt only after
930 handler(s) are executed in order to avoid spurious bank
931 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800932exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700933 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000934 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100935}
936
Lennert Buytenheke9191022010-11-29 11:17:17 +0100937static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300938{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100939 unsigned int gpio = d->irq - IH_GPIO_BASE;
940 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700941 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300942
Colin Cross85ec7b92011-06-06 13:38:18 -0700943 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300944 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700945 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300946}
947
Lennert Buytenheke9191022010-11-29 11:17:17 +0100948static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100949{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100950 unsigned int gpio = d->irq - IH_GPIO_BASE;
951 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100952
953 _clear_gpio_irqstatus(bank, gpio);
954}
955
Lennert Buytenheke9191022010-11-29 11:17:17 +0100956static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100957{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100958 unsigned int gpio = d->irq - IH_GPIO_BASE;
959 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700960 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100961
Colin Cross85ec7b92011-06-06 13:38:18 -0700962 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100963 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700964 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700965 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100966}
967
Lennert Buytenheke9191022010-11-29 11:17:17 +0100968static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100969{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100970 unsigned int gpio = d->irq - IH_GPIO_BASE;
971 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700972 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100973 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700974 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700975
Colin Cross85ec7b92011-06-06 13:38:18 -0700976 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700977 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700978 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800979
980 /* For level-triggered GPIOs, the clearing must be done after
981 * the HW source is cleared, thus after the handler has run */
982 if (bank->level_mask & irq_mask) {
983 _set_gpio_irqenable(bank, gpio, 0);
984 _clear_gpio_irqstatus(bank, gpio);
985 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100986
Kevin Hilman4de8c752008-01-16 21:56:14 -0800987 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700988 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100989}
990
David Brownelle5c56ed2006-12-06 17:13:59 -0800991static struct irq_chip gpio_irq_chip = {
992 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100993 .irq_shutdown = gpio_irq_shutdown,
994 .irq_ack = gpio_ack_irq,
995 .irq_mask = gpio_mask_irq,
996 .irq_unmask = gpio_unmask_irq,
997 .irq_set_type = gpio_irq_type,
998 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800999};
1000
1001/*---------------------------------------------------------------------*/
1002
1003#ifdef CONFIG_ARCH_OMAP1
1004
1005/* MPUIO uses the always-on 32k clock */
1006
Lennert Buytenheke9191022010-11-29 11:17:17 +01001007static void mpuio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001008{
1009 /* The ISR is reset automatically, so do nothing here. */
1010}
1011
Lennert Buytenheke9191022010-11-29 11:17:17 +01001012static void mpuio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001013{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001014 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1015 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001016
1017 _set_gpio_irqenable(bank, gpio, 0);
1018}
1019
Lennert Buytenheke9191022010-11-29 11:17:17 +01001020static void mpuio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001021{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001022 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1023 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001024
1025 _set_gpio_irqenable(bank, gpio, 1);
1026}
1027
David Brownelle5c56ed2006-12-06 17:13:59 -08001028static struct irq_chip mpuio_irq_chip = {
1029 .name = "MPUIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +01001030 .irq_ack = mpuio_ack_irq,
1031 .irq_mask = mpuio_mask_irq,
1032 .irq_unmask = mpuio_unmask_irq,
1033 .irq_set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001034#ifdef CONFIG_ARCH_OMAP16XX
1035 /* REVISIT: assuming only 16xx supports MPUIO wake events */
Lennert Buytenheke9191022010-11-29 11:17:17 +01001036 .irq_set_wake = gpio_wake_enable,
David Brownell11a78b72006-12-06 17:14:11 -08001037#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001038};
1039
David Brownelle5c56ed2006-12-06 17:13:59 -08001040
1041#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1042
David Brownell11a78b72006-12-06 17:14:11 -08001043
1044#ifdef CONFIG_ARCH_OMAP16XX
1045
1046#include <linux/platform_device.h>
1047
Magnus Damm79ee0312009-07-08 13:22:04 +02001048static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001049{
Magnus Damm79ee0312009-07-08 13:22:04 +02001050 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001051 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001052 void __iomem *mask_reg = bank->base +
1053 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001054 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001055
David Brownella6472532008-03-03 04:33:30 -08001056 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001057 bank->saved_wakeup = __raw_readl(mask_reg);
1058 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001059 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001060
1061 return 0;
1062}
1063
Magnus Damm79ee0312009-07-08 13:22:04 +02001064static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001065{
Magnus Damm79ee0312009-07-08 13:22:04 +02001066 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001067 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001068 void __iomem *mask_reg = bank->base +
1069 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001070 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001071
David Brownella6472532008-03-03 04:33:30 -08001072 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001073 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001074 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001075
1076 return 0;
1077}
1078
Alexey Dobriyan47145212009-12-14 18:00:08 -08001079static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +02001080 .suspend_noirq = omap_mpuio_suspend_noirq,
1081 .resume_noirq = omap_mpuio_resume_noirq,
1082};
1083
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001084/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -08001085static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -08001086 .driver = {
1087 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +02001088 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -08001089 },
1090};
1091
1092static struct platform_device omap_mpuio_device = {
1093 .name = "mpuio",
1094 .id = -1,
1095 .dev = {
1096 .driver = &omap_mpuio_driver.driver,
1097 }
1098 /* could list the /proc/iomem resources */
1099};
1100
1101static inline void mpuio_init(void)
1102{
Charulatha Va8be8da2011-04-22 16:38:16 +05301103 struct gpio_bank *bank = &gpio_bank[0];
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001104 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -07001105
David Brownell11a78b72006-12-06 17:14:11 -08001106 if (platform_driver_register(&omap_mpuio_driver) == 0)
1107 (void) platform_device_register(&omap_mpuio_device);
1108}
1109
1110#else
1111static inline void mpuio_init(void) {}
1112#endif /* 16xx */
1113
David Brownelle5c56ed2006-12-06 17:13:59 -08001114#else
1115
1116extern struct irq_chip mpuio_irq_chip;
1117
1118#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001119static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001120
1121#endif
1122
1123/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001124
David Brownell52e31342008-03-03 12:43:23 -08001125/* REVISIT these are stupid implementations! replace by ones that
1126 * don't switch on METHOD_* and which mostly avoid spinlocks
1127 */
1128
1129static int gpio_input(struct gpio_chip *chip, unsigned offset)
1130{
1131 struct gpio_bank *bank;
1132 unsigned long flags;
1133
1134 bank = container_of(chip, struct gpio_bank, chip);
1135 spin_lock_irqsave(&bank->lock, flags);
1136 _set_gpio_direction(bank, offset, 1);
1137 spin_unlock_irqrestore(&bank->lock, flags);
1138 return 0;
1139}
1140
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001141static int gpio_is_input(struct gpio_bank *bank, int mask)
1142{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001143 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001144
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001145 return __raw_readl(reg) & mask;
1146}
1147
David Brownell52e31342008-03-03 12:43:23 -08001148static int gpio_get(struct gpio_chip *chip, unsigned offset)
1149{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001150 struct gpio_bank *bank;
1151 void __iomem *reg;
1152 int gpio;
1153 u32 mask;
1154
1155 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +05301156 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001157 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -07001158 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001159
1160 if (gpio_is_input(bank, mask))
1161 return _get_gpio_datain(bank, gpio);
1162 else
1163 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -08001164}
1165
1166static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1167{
1168 struct gpio_bank *bank;
1169 unsigned long flags;
1170
1171 bank = container_of(chip, struct gpio_bank, chip);
1172 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001173 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001174 _set_gpio_direction(bank, offset, 0);
1175 spin_unlock_irqrestore(&bank->lock, flags);
1176 return 0;
1177}
1178
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001179static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
1180 unsigned debounce)
1181{
1182 struct gpio_bank *bank;
1183 unsigned long flags;
1184
1185 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001186
1187 if (!bank->dbck) {
1188 bank->dbck = clk_get(bank->dev, "dbclk");
1189 if (IS_ERR(bank->dbck))
1190 dev_err(bank->dev, "Could not get gpio dbck\n");
1191 }
1192
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001193 spin_lock_irqsave(&bank->lock, flags);
1194 _set_gpio_debounce(bank, offset, debounce);
1195 spin_unlock_irqrestore(&bank->lock, flags);
1196
1197 return 0;
1198}
1199
David Brownell52e31342008-03-03 12:43:23 -08001200static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1201{
1202 struct gpio_bank *bank;
1203 unsigned long flags;
1204
1205 bank = container_of(chip, struct gpio_bank, chip);
1206 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001207 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001208 spin_unlock_irqrestore(&bank->lock, flags);
1209}
1210
David Brownella007b702008-12-10 17:35:25 -08001211static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1212{
1213 struct gpio_bank *bank;
1214
1215 bank = container_of(chip, struct gpio_bank, chip);
1216 return bank->virtual_irq_start + offset;
1217}
1218
David Brownell52e31342008-03-03 12:43:23 -08001219/*---------------------------------------------------------------------*/
1220
Tony Lindgren9a748052010-12-07 16:26:56 -08001221static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001222{
1223 u32 rev;
1224
Tony Lindgren9a748052010-12-07 16:26:56 -08001225 if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
1226 rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001227 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001228 rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001229 else if (cpu_is_omap44xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001230 rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001231 else
1232 return;
1233
1234 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1235 (rev >> 4) & 0x0f, rev & 0x0f);
1236}
1237
David Brownell8ba55c52008-02-26 11:10:50 -08001238/* This lock class tells lockdep that GPIO irqs are in a different
1239 * category than their parents, so it won't report false recursion.
1240 */
1241static struct lock_class_key gpio_lock_class;
1242
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001243static inline int init_gpio_info(struct platform_device *pdev)
1244{
1245 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1246 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1247 GFP_KERNEL);
1248 if (!gpio_bank) {
1249 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1250 return -ENOMEM;
1251 }
1252 return 0;
1253}
1254
1255/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001256static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1257{
1258 if (cpu_class_is_omap2()) {
1259 if (cpu_is_omap44xx()) {
1260 __raw_writel(0xffffffff, bank->base +
1261 OMAP4_GPIO_IRQSTATUSCLR0);
1262 __raw_writel(0x00000000, bank->base +
1263 OMAP4_GPIO_DEBOUNCENABLE);
1264 /* Initialize interface clk ungated, module enabled */
1265 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1266 } else if (cpu_is_omap34xx()) {
1267 __raw_writel(0x00000000, bank->base +
1268 OMAP24XX_GPIO_IRQENABLE1);
1269 __raw_writel(0xffffffff, bank->base +
1270 OMAP24XX_GPIO_IRQSTATUS1);
1271 __raw_writel(0x00000000, bank->base +
1272 OMAP24XX_GPIO_DEBOUNCE_EN);
1273
1274 /* Initialize interface clk ungated, module enabled */
1275 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1276 } else if (cpu_is_omap24xx()) {
1277 static const u32 non_wakeup_gpios[] = {
1278 0xe203ffc0, 0x08700040
1279 };
1280 if (id < ARRAY_SIZE(non_wakeup_gpios))
1281 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1282 }
1283 } else if (cpu_class_is_omap1()) {
1284 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001285 __raw_writew(0xffff, bank->base +
1286 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001287 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1288 __raw_writew(0xffff, bank->base
1289 + OMAP1510_GPIO_INT_MASK);
1290 __raw_writew(0x0000, bank->base
1291 + OMAP1510_GPIO_INT_STATUS);
1292 }
1293 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1294 __raw_writew(0x0000, bank->base
1295 + OMAP1610_GPIO_IRQENABLE1);
1296 __raw_writew(0xffff, bank->base
1297 + OMAP1610_GPIO_IRQSTATUS1);
1298 __raw_writew(0x0014, bank->base
1299 + OMAP1610_GPIO_SYSCONFIG);
1300
1301 /*
1302 * Enable system clock for GPIO module.
1303 * The CAM_CLK_CTRL *is* really the right place.
1304 */
1305 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1306 ULPD_CAM_CLK_CTRL);
1307 }
1308 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1309 __raw_writel(0xffffffff, bank->base
1310 + OMAP7XX_GPIO_INT_MASK);
1311 __raw_writel(0x00000000, bank->base
1312 + OMAP7XX_GPIO_INT_STATUS);
1313 }
1314 }
1315}
1316
Russell Kingd52b31d2011-05-27 13:56:12 -07001317static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001318{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001319 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001320 static int gpio;
1321
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001322 bank->mod_usage = 0;
1323 /*
1324 * REVISIT eventually switch from OMAP-specific gpio structs
1325 * over to the generic ones
1326 */
1327 bank->chip.request = omap_gpio_request;
1328 bank->chip.free = omap_gpio_free;
1329 bank->chip.direction_input = gpio_input;
1330 bank->chip.get = gpio_get;
1331 bank->chip.direction_output = gpio_output;
1332 bank->chip.set_debounce = gpio_debounce;
1333 bank->chip.set = gpio_set;
1334 bank->chip.to_irq = gpio_2irq;
1335 if (bank_is_mpuio(bank)) {
1336 bank->chip.label = "mpuio";
1337#ifdef CONFIG_ARCH_OMAP16XX
1338 bank->chip.dev = &omap_mpuio_device.dev;
1339#endif
1340 bank->chip.base = OMAP_MPUIO(0);
1341 } else {
1342 bank->chip.label = "gpio";
1343 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001344 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001345 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001346 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001347
1348 gpiochip_add(&bank->chip);
1349
1350 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001351 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001352 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001353 irq_set_chip_data(j, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001354 if (bank_is_mpuio(bank))
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001355 irq_set_chip(j, &mpuio_irq_chip);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001356 else
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001357 irq_set_chip(j, &gpio_irq_chip);
1358 irq_set_handler(j, handle_simple_irq);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001359 set_irq_flags(j, IRQF_VALID);
1360 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001361 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1362 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001363}
1364
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001365static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001366{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001367 static int gpio_init_done;
1368 struct omap_gpio_platform_data *pdata;
1369 struct resource *res;
1370 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001371 struct gpio_bank *bank;
1372
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001373 if (!pdev->dev.platform_data)
1374 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001375
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001376 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001377
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001378 if (!gpio_init_done) {
1379 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001380
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001381 ret = init_gpio_info(pdev);
1382 if (ret)
1383 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001384 }
1385
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001386 id = pdev->id;
1387 bank = &gpio_bank[id];
1388
1389 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1390 if (unlikely(!res)) {
1391 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1392 return -ENODEV;
1393 }
1394
1395 bank->irq = res->start;
1396 bank->virtual_irq_start = pdata->virtual_irq_start;
1397 bank->method = pdata->bank_type;
1398 bank->dev = &pdev->dev;
1399 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001400 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001401 bank->width = pdata->bank_width;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001402
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001403 bank->regs = pdata->regs;
1404
1405 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1406 bank->set_dataout = _set_gpio_dataout_reg;
1407 else
1408 bank->set_dataout = _set_gpio_dataout_mask;
1409
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001410 spin_lock_init(&bank->lock);
1411
1412 /* Static mapping, never released */
1413 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1414 if (unlikely(!res)) {
1415 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1416 return -ENODEV;
1417 }
1418
1419 bank->base = ioremap(res->start, resource_size(res));
1420 if (!bank->base) {
1421 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1422 return -ENOMEM;
1423 }
1424
1425 pm_runtime_enable(bank->dev);
1426 pm_runtime_get_sync(bank->dev);
1427
1428 omap_gpio_mod_init(bank, id);
1429 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001430 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001431
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001432 if (!gpio_init_done)
1433 gpio_init_done = 1;
1434
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001435 return 0;
1436}
1437
Tony Lindgren140455f2010-02-12 12:26:48 -08001438#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001439static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001440{
1441 int i;
1442
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001443 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001444 return 0;
1445
1446 for (i = 0; i < gpio_bank_count; i++) {
1447 struct gpio_bank *bank = &gpio_bank[i];
1448 void __iomem *wake_status;
1449 void __iomem *wake_clear;
1450 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001451 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001452
1453 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001454#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001455 case METHOD_GPIO_1610:
1456 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1457 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1458 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1459 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001460#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001461#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001462 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001463 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001464 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1465 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1466 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001467#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301468#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001469 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301470 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1471 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1472 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1473 break;
1474#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001475 default:
1476 continue;
1477 }
1478
David Brownella6472532008-03-03 04:33:30 -08001479 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001480 bank->saved_wakeup = __raw_readl(wake_status);
1481 __raw_writel(0xffffffff, wake_clear);
1482 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001483 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001484 }
1485
1486 return 0;
1487}
1488
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001489static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001490{
1491 int i;
1492
Tero Kristo723fdb72008-11-26 14:35:16 -08001493 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001494 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001495
1496 for (i = 0; i < gpio_bank_count; i++) {
1497 struct gpio_bank *bank = &gpio_bank[i];
1498 void __iomem *wake_clear;
1499 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001500 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001501
1502 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001503#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001504 case METHOD_GPIO_1610:
1505 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1506 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1507 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001508#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001509#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001510 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001511 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1512 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001513 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001514#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301515#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001516 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301517 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1518 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1519 break;
1520#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001521 default:
1522 continue;
1523 }
1524
David Brownella6472532008-03-03 04:33:30 -08001525 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001526 __raw_writel(0xffffffff, wake_clear);
1527 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001528 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001529 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001530}
1531
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001532static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001533 .suspend = omap_gpio_suspend,
1534 .resume = omap_gpio_resume,
1535};
1536
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001537#endif
1538
Tony Lindgren140455f2010-02-12 12:26:48 -08001539#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001540
1541static int workaround_enabled;
1542
Paul Walmsley72e06d02010-12-21 21:05:16 -07001543void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001544{
1545 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001546 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001547
Tero Kristoa118b5f2008-12-22 14:27:12 +02001548 if (cpu_is_omap34xx())
1549 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001550
Tero Kristoa118b5f2008-12-22 14:27:12 +02001551 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001552 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001553 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001554 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001555
Kevin Hilman0aed04352010-09-22 16:06:27 -07001556 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001557 clk_disable(bank->dbck);
1558
Paul Walmsley72e06d02010-12-21 21:05:16 -07001559 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001560 continue;
1561
1562 /* If going to OFF, remove triggering for all
1563 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1564 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001565 if (!(bank->enabled_non_wakeup_gpios))
1566 continue;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001567
1568 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1569 bank->saved_datain = __raw_readl(bank->base +
1570 OMAP24XX_GPIO_DATAIN);
1571 l1 = __raw_readl(bank->base +
1572 OMAP24XX_GPIO_FALLINGDETECT);
1573 l2 = __raw_readl(bank->base +
1574 OMAP24XX_GPIO_RISINGDETECT);
1575 }
1576
1577 if (cpu_is_omap44xx()) {
1578 bank->saved_datain = __raw_readl(bank->base +
1579 OMAP4_GPIO_DATAIN);
1580 l1 = __raw_readl(bank->base +
1581 OMAP4_GPIO_FALLINGDETECT);
1582 l2 = __raw_readl(bank->base +
1583 OMAP4_GPIO_RISINGDETECT);
1584 }
1585
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001586 bank->saved_fallingdetect = l1;
1587 bank->saved_risingdetect = l2;
1588 l1 &= ~bank->enabled_non_wakeup_gpios;
1589 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001590
1591 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1592 __raw_writel(l1, bank->base +
1593 OMAP24XX_GPIO_FALLINGDETECT);
1594 __raw_writel(l2, bank->base +
1595 OMAP24XX_GPIO_RISINGDETECT);
1596 }
1597
1598 if (cpu_is_omap44xx()) {
1599 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1600 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1601 }
1602
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001603 c++;
1604 }
1605 if (!c) {
1606 workaround_enabled = 0;
1607 return;
1608 }
1609 workaround_enabled = 1;
1610}
1611
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001612void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001613{
1614 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001615 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001616
Tero Kristoa118b5f2008-12-22 14:27:12 +02001617 if (cpu_is_omap34xx())
1618 min = 1;
1619 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001620 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001621 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001622 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001623
Kevin Hilman0aed04352010-09-22 16:06:27 -07001624 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001625 clk_enable(bank->dbck);
1626
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001627 if (!workaround_enabled)
1628 continue;
1629
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001630 if (!(bank->enabled_non_wakeup_gpios))
1631 continue;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001632
1633 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1634 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001635 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001636 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001637 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001638 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1639 }
1640
1641 if (cpu_is_omap44xx()) {
1642 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301643 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001644 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301645 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001646 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1647 }
1648
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001649 /* Check if any of the non-wakeup interrupt GPIOs have changed
1650 * state. If so, generate an IRQ by software. This is
1651 * horribly racy, but it's the best we can do to work around
1652 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001653 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001654 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001655
1656 /*
1657 * No need to generate IRQs for the rising edge for gpio IRQs
1658 * configured with falling edge only; and vice versa.
1659 */
1660 gen0 = l & bank->saved_fallingdetect;
1661 gen0 &= bank->saved_datain;
1662
1663 gen1 = l & bank->saved_risingdetect;
1664 gen1 &= ~(bank->saved_datain);
1665
1666 /* FIXME: Consider GPIO IRQs with level detections properly! */
1667 gen = l & (~(bank->saved_fallingdetect) &
1668 ~(bank->saved_risingdetect));
1669 /* Consider all GPIO IRQs needed to be updated */
1670 gen |= gen0 | gen1;
1671
1672 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001673 u32 old0, old1;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001674
Sergio Aguirref00d6492010-03-03 16:21:08 +00001675 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001676 old0 = __raw_readl(bank->base +
1677 OMAP24XX_GPIO_LEVELDETECT0);
1678 old1 = __raw_readl(bank->base +
1679 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001680 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001681 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001682 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001683 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001684 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001685 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001686 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001687 OMAP24XX_GPIO_LEVELDETECT1);
1688 }
1689
1690 if (cpu_is_omap44xx()) {
1691 old0 = __raw_readl(bank->base +
1692 OMAP4_GPIO_LEVELDETECT0);
1693 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301694 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001695 __raw_writel(old0 | l, bank->base +
1696 OMAP4_GPIO_LEVELDETECT0);
1697 __raw_writel(old1 | l, bank->base +
1698 OMAP4_GPIO_LEVELDETECT1);
1699 __raw_writel(old0, bank->base +
1700 OMAP4_GPIO_LEVELDETECT0);
1701 __raw_writel(old1, bank->base +
1702 OMAP4_GPIO_LEVELDETECT1);
1703 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001704 }
1705 }
1706
1707}
1708
Tony Lindgren92105bb2005-09-07 17:20:26 +01001709#endif
1710
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001711#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301712/* save the registers of bank 2-6 */
1713void omap_gpio_save_context(void)
1714{
1715 int i;
1716
1717 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1718 for (i = 1; i < gpio_bank_count; i++) {
1719 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301720 gpio_context[i].irqenable1 =
1721 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1722 gpio_context[i].irqenable2 =
1723 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1724 gpio_context[i].wake_en =
1725 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1726 gpio_context[i].ctrl =
1727 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1728 gpio_context[i].oe =
1729 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1730 gpio_context[i].leveldetect0 =
1731 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1732 gpio_context[i].leveldetect1 =
1733 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1734 gpio_context[i].risingdetect =
1735 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1736 gpio_context[i].fallingdetect =
1737 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1738 gpio_context[i].dataout =
1739 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301740 }
1741}
1742
1743/* restore the required registers of bank 2-6 */
1744void omap_gpio_restore_context(void)
1745{
1746 int i;
1747
1748 for (i = 1; i < gpio_bank_count; i++) {
1749 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301750 __raw_writel(gpio_context[i].irqenable1,
1751 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1752 __raw_writel(gpio_context[i].irqenable2,
1753 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1754 __raw_writel(gpio_context[i].wake_en,
1755 bank->base + OMAP24XX_GPIO_WAKE_EN);
1756 __raw_writel(gpio_context[i].ctrl,
1757 bank->base + OMAP24XX_GPIO_CTRL);
1758 __raw_writel(gpio_context[i].oe,
1759 bank->base + OMAP24XX_GPIO_OE);
1760 __raw_writel(gpio_context[i].leveldetect0,
1761 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1762 __raw_writel(gpio_context[i].leveldetect1,
1763 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1764 __raw_writel(gpio_context[i].risingdetect,
1765 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1766 __raw_writel(gpio_context[i].fallingdetect,
1767 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1768 __raw_writel(gpio_context[i].dataout,
1769 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301770 }
1771}
1772#endif
1773
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001774static struct platform_driver omap_gpio_driver = {
1775 .probe = omap_gpio_probe,
1776 .driver = {
1777 .name = "omap_gpio",
1778 },
1779};
1780
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001781/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001782 * gpio driver register needs to be done before
1783 * machine_init functions access gpio APIs.
1784 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001785 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001786static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001787{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001788 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001789}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001790postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001791
Tony Lindgren92105bb2005-09-07 17:20:26 +01001792static int __init omap_gpio_sysinit(void)
1793{
David Brownell11a78b72006-12-06 17:14:11 -08001794 mpuio_init();
1795
Tony Lindgren140455f2010-02-12 12:26:48 -08001796#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001797 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1798 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001799#endif
1800
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001801 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001802}
1803
Tony Lindgren92105bb2005-09-07 17:20:26 +01001804arch_initcall(omap_gpio_sysinit);