blob: bdf0132b70ec268a3986c36897a462a6f368d116 [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
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700484 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100485 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300486
487 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700488 if (bank->regs->irqstatus2) {
489 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700490 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700491 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700492
493 /* Flush posted write for the irq status to avoid spurious interrupts */
494 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100495}
496
497static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
498{
Kevin Hilman129fd222011-04-22 07:59:07 -0700499 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100500}
501
Imre Deakea6dedd2006-06-26 16:16:00 -0700502static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
503{
504 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700505 int inv = 0;
506 u32 l;
Kevin Hilmanc390aad2011-04-21 09:33:36 -0700507 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700508
509 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800510#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700511 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800512 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Imre Deak99c47702006-06-26 16:16:07 -0700513 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700514 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800515#endif
516#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700517 case METHOD_GPIO_1510:
518 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700519 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700520 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800521#endif
522#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700523 case METHOD_GPIO_1610:
524 reg += OMAP1610_GPIO_IRQENABLE1;
525 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800526#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100527#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100528 case METHOD_GPIO_7XX:
529 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700530 inv = 1;
531 break;
532#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800533#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Imre Deakea6dedd2006-06-26 16:16:00 -0700534 case METHOD_GPIO_24XX:
535 reg += OMAP24XX_GPIO_IRQENABLE1;
536 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800537#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530538#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800539 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530540 reg += OMAP4_GPIO_IRQSTATUSSET0;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530541 break;
542#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700543 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800544 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700545 return 0;
546 }
547
Imre Deak99c47702006-06-26 16:16:07 -0700548 l = __raw_readl(reg);
549 if (inv)
550 l = ~l;
551 l &= mask;
552 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700553}
554
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100555static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
556{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100557 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100558 u32 l;
559
560 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800561#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100562 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800563 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564 l = __raw_readl(reg);
565 if (enable)
566 l &= ~(gpio_mask);
567 else
568 l |= gpio_mask;
569 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800570#endif
571#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100572 case METHOD_GPIO_1510:
573 reg += OMAP1510_GPIO_INT_MASK;
574 l = __raw_readl(reg);
575 if (enable)
576 l &= ~(gpio_mask);
577 else
578 l |= gpio_mask;
579 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800580#endif
581#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100582 case METHOD_GPIO_1610:
583 if (enable)
584 reg += OMAP1610_GPIO_SET_IRQENABLE1;
585 else
586 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
587 l = gpio_mask;
588 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800589#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100590#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100591 case METHOD_GPIO_7XX:
592 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700593 l = __raw_readl(reg);
594 if (enable)
595 l &= ~(gpio_mask);
596 else
597 l |= gpio_mask;
598 break;
599#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800600#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100601 case METHOD_GPIO_24XX:
602 if (enable)
603 reg += OMAP24XX_GPIO_SETIRQENABLE1;
604 else
605 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
606 l = gpio_mask;
607 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800608#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530609#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800610 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530611 if (enable)
612 reg += OMAP4_GPIO_IRQSTATUSSET0;
613 else
614 reg += OMAP4_GPIO_IRQSTATUSCLR0;
615 l = gpio_mask;
616 break;
617#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100618 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800619 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100620 return;
621 }
622 __raw_writel(l, reg);
623}
624
625static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
626{
Kevin Hilman129fd222011-04-22 07:59:07 -0700627 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio), enable);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100628}
629
Tony Lindgren92105bb2005-09-07 17:20:26 +0100630/*
631 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
632 * 1510 does not seem to have a wake-up register. If JTAG is connected
633 * to the target, system will wake up always on GPIO events. While
634 * system is running all registered GPIO interrupts need to have wake-up
635 * enabled. When system is suspended, only selected GPIO interrupts need
636 * to have wake-up enabled.
637 */
638static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
639{
Tony Lindgren4cc64202010-01-08 10:29:05 -0800640 unsigned long uninitialized_var(flags);
David Brownella6472532008-03-03 04:33:30 -0800641
Tony Lindgren92105bb2005-09-07 17:20:26 +0100642 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800643#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800644 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100645 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800646 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700647 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100648 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700649 else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100650 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800651 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100652 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800653#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800654#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800655 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800656 case METHOD_GPIO_44XX:
David Brownell11a78b72006-12-06 17:14:11 -0800657 if (bank->non_wakeup_gpios & (1 << gpio)) {
658 printk(KERN_ERR "Unable to modify wakeup on "
659 "non-wakeup GPIO%d\n",
Kevin Hilmand5f46242011-04-21 09:23:00 -0700660 (bank - gpio_bank) * bank->width + gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800661 return -EINVAL;
662 }
David Brownella6472532008-03-03 04:33:30 -0800663 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700664 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800665 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700666 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800667 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800668 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800669 return 0;
670#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100671 default:
672 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
673 bank->method);
674 return -EINVAL;
675 }
676}
677
Tony Lindgren4196dd62006-09-25 12:41:38 +0300678static void _reset_gpio(struct gpio_bank *bank, int gpio)
679{
Kevin Hilman129fd222011-04-22 07:59:07 -0700680 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300681 _set_gpio_irqenable(bank, gpio, 0);
682 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700683 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300684}
685
Tony Lindgren92105bb2005-09-07 17:20:26 +0100686/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100687static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100688{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100689 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100690 struct gpio_bank *bank;
691 int retval;
692
693 if (check_gpio(gpio) < 0)
694 return -ENODEV;
Lennert Buytenheke9191022010-11-29 11:17:17 +0100695 bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700696 retval = _set_gpio_wakeup(bank, GPIO_INDEX(bank, gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100697
698 return retval;
699}
700
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800701static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800703 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800704 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100705
David Brownella6472532008-03-03 04:33:30 -0800706 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100707
Tony Lindgren4196dd62006-09-25 12:41:38 +0300708 /* Set trigger to none. You need to enable the desired trigger with
709 * request_irq() or set_irq_type().
710 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800711 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100712
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000713#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100714 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100715 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100716
Tony Lindgren92105bb2005-09-07 17:20:26 +0100717 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800719 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100720 }
721#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800722 if (!cpu_class_is_omap1()) {
723 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700724 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800725 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700726
727 if (cpu_is_omap24xx() || cpu_is_omap34xx())
728 reg += OMAP24XX_GPIO_CTRL;
729 else if (cpu_is_omap44xx())
730 reg += OMAP4_GPIO_CTRL;
731 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800732 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700733 ctrl &= 0xFFFFFFFE;
734 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800735 }
736 bank->mod_usage |= 1 << offset;
737 }
David Brownella6472532008-03-03 04:33:30 -0800738 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100739
740 return 0;
741}
742
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800743static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100744{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800745 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800746 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100747
David Brownella6472532008-03-03 04:33:30 -0800748 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100749#ifdef CONFIG_ARCH_OMAP16XX
750 if (bank->method == METHOD_GPIO_1610) {
751 /* Disable wake-up during idle for dynamic tick */
752 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800753 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100754 }
755#endif
Charulatha V9f096862010-05-14 12:05:27 -0700756#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
757 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100758 /* Disable wake-up during idle for dynamic tick */
759 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800760 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100761 }
762#endif
Charulatha V9f096862010-05-14 12:05:27 -0700763#ifdef CONFIG_ARCH_OMAP4
764 if (bank->method == METHOD_GPIO_44XX) {
765 /* Disable wake-up during idle for dynamic tick */
766 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
767 __raw_writel(1 << offset, reg);
768 }
769#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800770 if (!cpu_class_is_omap1()) {
771 bank->mod_usage &= ~(1 << offset);
772 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700773 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800774 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700775
776 if (cpu_is_omap24xx() || cpu_is_omap34xx())
777 reg += OMAP24XX_GPIO_CTRL;
778 else if (cpu_is_omap44xx())
779 reg += OMAP4_GPIO_CTRL;
780 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800781 /* Module is disabled, clocks are gated */
782 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700783 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800784 }
785 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800786 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800787 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100788}
789
790/*
791 * We need to unmask the GPIO bank interrupt as soon as possible to
792 * avoid missing GPIO interrupts for other lines in the bank.
793 * Then we need to mask-read-clear-unmask the triggered GPIO lines
794 * in the bank to avoid missing nested interrupts for a GPIO line.
795 * If we wait to unmask individual GPIO lines in the bank after the
796 * line's interrupt handler has been run, we may miss some nested
797 * interrupts.
798 */
Russell King10dd5ce2006-11-23 11:41:32 +0000799static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100800{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100801 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100802 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800803 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100804 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700805 u32 retrigger = 0;
806 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000807 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100808
Will Deaconee144182011-02-21 13:46:08 +0000809 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100810
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100811 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700812 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800813
814 if (WARN_ON(!isr_reg))
815 goto exit;
816
Tony Lindgren92105bb2005-09-07 17:20:26 +0100817 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100818 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700819 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100820
Imre Deakea6dedd2006-06-26 16:16:00 -0700821 enabled = _get_gpio_irqbank_mask(bank);
822 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100823
824 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
825 isr &= 0x0000ffff;
826
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800827 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800828 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -0700829 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100830
831 /* clear edge sensitive interrupts before handler(s) are
832 called so that we don't miss any interrupt occurred while
833 executing them */
834 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
835 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
836 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
837
838 /* if there is only edge sensitive GPIO pin interrupts
839 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700840 if (!level_mask && !unmasked) {
841 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000842 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700843 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100844
Imre Deakea6dedd2006-06-26 16:16:00 -0700845 isr |= retrigger;
846 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100847 if (!isr)
848 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100849
Tony Lindgren92105bb2005-09-07 17:20:26 +0100850 gpio_irq = bank->virtual_irq_start;
851 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700852 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800853
Tony Lindgren92105bb2005-09-07 17:20:26 +0100854 if (!(isr & 1))
855 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200856
Cory Maccarrone4318f362010-01-08 10:29:04 -0800857#ifdef CONFIG_ARCH_OMAP1
858 /*
859 * Some chips can't respond to both rising and falling
860 * at the same time. If this irq was requested with
861 * both flags, we need to flip the ICR data for the IRQ
862 * to respond to the IRQ for the opposite direction.
863 * This will be indicated in the bank toggle_mask.
864 */
865 if (bank->toggle_mask & (1 << gpio_index))
866 _toggle_gpio_edge_triggering(bank, gpio_index);
867#endif
868
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100869 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100870 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000871 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700872 /* if bank has any level sensitive GPIO pin interrupt
873 configured, we must unmask the bank interrupt only after
874 handler(s) are executed in order to avoid spurious bank
875 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800876exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700877 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000878 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100879}
880
Lennert Buytenheke9191022010-11-29 11:17:17 +0100881static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300882{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100883 unsigned int gpio = d->irq - IH_GPIO_BASE;
884 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700885 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300886
Colin Cross85ec7b92011-06-06 13:38:18 -0700887 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300888 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700889 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300890}
891
Lennert Buytenheke9191022010-11-29 11:17:17 +0100892static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100893{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100894 unsigned int gpio = d->irq - IH_GPIO_BASE;
895 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100896
897 _clear_gpio_irqstatus(bank, gpio);
898}
899
Lennert Buytenheke9191022010-11-29 11:17:17 +0100900static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100901{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100902 unsigned int gpio = d->irq - IH_GPIO_BASE;
903 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700904 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100905
Colin Cross85ec7b92011-06-06 13:38:18 -0700906 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100907 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700908 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700909 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100910}
911
Lennert Buytenheke9191022010-11-29 11:17:17 +0100912static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100913{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100914 unsigned int gpio = d->irq - IH_GPIO_BASE;
915 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700916 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100917 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700918 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700919
Colin Cross85ec7b92011-06-06 13:38:18 -0700920 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700921 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700922 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800923
924 /* For level-triggered GPIOs, the clearing must be done after
925 * the HW source is cleared, thus after the handler has run */
926 if (bank->level_mask & irq_mask) {
927 _set_gpio_irqenable(bank, gpio, 0);
928 _clear_gpio_irqstatus(bank, gpio);
929 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100930
Kevin Hilman4de8c752008-01-16 21:56:14 -0800931 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700932 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100933}
934
David Brownelle5c56ed2006-12-06 17:13:59 -0800935static struct irq_chip gpio_irq_chip = {
936 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100937 .irq_shutdown = gpio_irq_shutdown,
938 .irq_ack = gpio_ack_irq,
939 .irq_mask = gpio_mask_irq,
940 .irq_unmask = gpio_unmask_irq,
941 .irq_set_type = gpio_irq_type,
942 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800943};
944
945/*---------------------------------------------------------------------*/
946
947#ifdef CONFIG_ARCH_OMAP1
948
949/* MPUIO uses the always-on 32k clock */
950
Lennert Buytenheke9191022010-11-29 11:17:17 +0100951static void mpuio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100952{
953 /* The ISR is reset automatically, so do nothing here. */
954}
955
Lennert Buytenheke9191022010-11-29 11:17:17 +0100956static void mpuio_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 = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
959 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100960
961 _set_gpio_irqenable(bank, gpio, 0);
962}
963
Lennert Buytenheke9191022010-11-29 11:17:17 +0100964static void mpuio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100965{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100966 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
967 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100968
969 _set_gpio_irqenable(bank, gpio, 1);
970}
971
David Brownelle5c56ed2006-12-06 17:13:59 -0800972static struct irq_chip mpuio_irq_chip = {
973 .name = "MPUIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100974 .irq_ack = mpuio_ack_irq,
975 .irq_mask = mpuio_mask_irq,
976 .irq_unmask = mpuio_unmask_irq,
977 .irq_set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -0800978#ifdef CONFIG_ARCH_OMAP16XX
979 /* REVISIT: assuming only 16xx supports MPUIO wake events */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100980 .irq_set_wake = gpio_wake_enable,
David Brownell11a78b72006-12-06 17:14:11 -0800981#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100982};
983
David Brownelle5c56ed2006-12-06 17:13:59 -0800984
985#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
986
David Brownell11a78b72006-12-06 17:14:11 -0800987
988#ifdef CONFIG_ARCH_OMAP16XX
989
990#include <linux/platform_device.h>
991
Magnus Damm79ee0312009-07-08 13:22:04 +0200992static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800993{
Magnus Damm79ee0312009-07-08 13:22:04 +0200994 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800995 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800996 void __iomem *mask_reg = bank->base +
997 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800998 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800999
David Brownella6472532008-03-03 04:33:30 -08001000 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001001 bank->saved_wakeup = __raw_readl(mask_reg);
1002 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001003 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001004
1005 return 0;
1006}
1007
Magnus Damm79ee0312009-07-08 13:22:04 +02001008static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001009{
Magnus Damm79ee0312009-07-08 13:22:04 +02001010 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001011 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001012 void __iomem *mask_reg = bank->base +
1013 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001014 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001015
David Brownella6472532008-03-03 04:33:30 -08001016 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001017 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001018 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001019
1020 return 0;
1021}
1022
Alexey Dobriyan47145212009-12-14 18:00:08 -08001023static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +02001024 .suspend_noirq = omap_mpuio_suspend_noirq,
1025 .resume_noirq = omap_mpuio_resume_noirq,
1026};
1027
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001028/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -08001029static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -08001030 .driver = {
1031 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +02001032 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -08001033 },
1034};
1035
1036static struct platform_device omap_mpuio_device = {
1037 .name = "mpuio",
1038 .id = -1,
1039 .dev = {
1040 .driver = &omap_mpuio_driver.driver,
1041 }
1042 /* could list the /proc/iomem resources */
1043};
1044
1045static inline void mpuio_init(void)
1046{
Charulatha Va8be8da2011-04-22 16:38:16 +05301047 struct gpio_bank *bank = &gpio_bank[0];
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001048 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -07001049
David Brownell11a78b72006-12-06 17:14:11 -08001050 if (platform_driver_register(&omap_mpuio_driver) == 0)
1051 (void) platform_device_register(&omap_mpuio_device);
1052}
1053
1054#else
1055static inline void mpuio_init(void) {}
1056#endif /* 16xx */
1057
David Brownelle5c56ed2006-12-06 17:13:59 -08001058#else
1059
1060extern struct irq_chip mpuio_irq_chip;
1061
1062#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001063static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001064
1065#endif
1066
1067/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001068
David Brownell52e31342008-03-03 12:43:23 -08001069/* REVISIT these are stupid implementations! replace by ones that
1070 * don't switch on METHOD_* and which mostly avoid spinlocks
1071 */
1072
1073static int gpio_input(struct gpio_chip *chip, unsigned offset)
1074{
1075 struct gpio_bank *bank;
1076 unsigned long flags;
1077
1078 bank = container_of(chip, struct gpio_bank, chip);
1079 spin_lock_irqsave(&bank->lock, flags);
1080 _set_gpio_direction(bank, offset, 1);
1081 spin_unlock_irqrestore(&bank->lock, flags);
1082 return 0;
1083}
1084
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001085static int gpio_is_input(struct gpio_bank *bank, int mask)
1086{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001087 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001088
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001089 return __raw_readl(reg) & mask;
1090}
1091
David Brownell52e31342008-03-03 12:43:23 -08001092static int gpio_get(struct gpio_chip *chip, unsigned offset)
1093{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001094 struct gpio_bank *bank;
1095 void __iomem *reg;
1096 int gpio;
1097 u32 mask;
1098
1099 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +05301100 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001101 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -07001102 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001103
1104 if (gpio_is_input(bank, mask))
1105 return _get_gpio_datain(bank, gpio);
1106 else
1107 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -08001108}
1109
1110static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1111{
1112 struct gpio_bank *bank;
1113 unsigned long flags;
1114
1115 bank = container_of(chip, struct gpio_bank, chip);
1116 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001117 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001118 _set_gpio_direction(bank, offset, 0);
1119 spin_unlock_irqrestore(&bank->lock, flags);
1120 return 0;
1121}
1122
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001123static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
1124 unsigned debounce)
1125{
1126 struct gpio_bank *bank;
1127 unsigned long flags;
1128
1129 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001130
1131 if (!bank->dbck) {
1132 bank->dbck = clk_get(bank->dev, "dbclk");
1133 if (IS_ERR(bank->dbck))
1134 dev_err(bank->dev, "Could not get gpio dbck\n");
1135 }
1136
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001137 spin_lock_irqsave(&bank->lock, flags);
1138 _set_gpio_debounce(bank, offset, debounce);
1139 spin_unlock_irqrestore(&bank->lock, flags);
1140
1141 return 0;
1142}
1143
David Brownell52e31342008-03-03 12:43:23 -08001144static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1145{
1146 struct gpio_bank *bank;
1147 unsigned long flags;
1148
1149 bank = container_of(chip, struct gpio_bank, chip);
1150 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001151 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001152 spin_unlock_irqrestore(&bank->lock, flags);
1153}
1154
David Brownella007b702008-12-10 17:35:25 -08001155static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1156{
1157 struct gpio_bank *bank;
1158
1159 bank = container_of(chip, struct gpio_bank, chip);
1160 return bank->virtual_irq_start + offset;
1161}
1162
David Brownell52e31342008-03-03 12:43:23 -08001163/*---------------------------------------------------------------------*/
1164
Tony Lindgren9a748052010-12-07 16:26:56 -08001165static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001166{
1167 u32 rev;
1168
Tony Lindgren9a748052010-12-07 16:26:56 -08001169 if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
1170 rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001171 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001172 rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001173 else if (cpu_is_omap44xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001174 rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001175 else
1176 return;
1177
1178 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1179 (rev >> 4) & 0x0f, rev & 0x0f);
1180}
1181
David Brownell8ba55c52008-02-26 11:10:50 -08001182/* This lock class tells lockdep that GPIO irqs are in a different
1183 * category than their parents, so it won't report false recursion.
1184 */
1185static struct lock_class_key gpio_lock_class;
1186
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001187static inline int init_gpio_info(struct platform_device *pdev)
1188{
1189 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1190 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1191 GFP_KERNEL);
1192 if (!gpio_bank) {
1193 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1194 return -ENOMEM;
1195 }
1196 return 0;
1197}
1198
1199/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001200static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1201{
1202 if (cpu_class_is_omap2()) {
1203 if (cpu_is_omap44xx()) {
1204 __raw_writel(0xffffffff, bank->base +
1205 OMAP4_GPIO_IRQSTATUSCLR0);
1206 __raw_writel(0x00000000, bank->base +
1207 OMAP4_GPIO_DEBOUNCENABLE);
1208 /* Initialize interface clk ungated, module enabled */
1209 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1210 } else if (cpu_is_omap34xx()) {
1211 __raw_writel(0x00000000, bank->base +
1212 OMAP24XX_GPIO_IRQENABLE1);
1213 __raw_writel(0xffffffff, bank->base +
1214 OMAP24XX_GPIO_IRQSTATUS1);
1215 __raw_writel(0x00000000, bank->base +
1216 OMAP24XX_GPIO_DEBOUNCE_EN);
1217
1218 /* Initialize interface clk ungated, module enabled */
1219 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1220 } else if (cpu_is_omap24xx()) {
1221 static const u32 non_wakeup_gpios[] = {
1222 0xe203ffc0, 0x08700040
1223 };
1224 if (id < ARRAY_SIZE(non_wakeup_gpios))
1225 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1226 }
1227 } else if (cpu_class_is_omap1()) {
1228 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001229 __raw_writew(0xffff, bank->base +
1230 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001231 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1232 __raw_writew(0xffff, bank->base
1233 + OMAP1510_GPIO_INT_MASK);
1234 __raw_writew(0x0000, bank->base
1235 + OMAP1510_GPIO_INT_STATUS);
1236 }
1237 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1238 __raw_writew(0x0000, bank->base
1239 + OMAP1610_GPIO_IRQENABLE1);
1240 __raw_writew(0xffff, bank->base
1241 + OMAP1610_GPIO_IRQSTATUS1);
1242 __raw_writew(0x0014, bank->base
1243 + OMAP1610_GPIO_SYSCONFIG);
1244
1245 /*
1246 * Enable system clock for GPIO module.
1247 * The CAM_CLK_CTRL *is* really the right place.
1248 */
1249 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1250 ULPD_CAM_CLK_CTRL);
1251 }
1252 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1253 __raw_writel(0xffffffff, bank->base
1254 + OMAP7XX_GPIO_INT_MASK);
1255 __raw_writel(0x00000000, bank->base
1256 + OMAP7XX_GPIO_INT_STATUS);
1257 }
1258 }
1259}
1260
Russell Kingd52b31d2011-05-27 13:56:12 -07001261static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001262{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001263 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001264 static int gpio;
1265
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001266 bank->mod_usage = 0;
1267 /*
1268 * REVISIT eventually switch from OMAP-specific gpio structs
1269 * over to the generic ones
1270 */
1271 bank->chip.request = omap_gpio_request;
1272 bank->chip.free = omap_gpio_free;
1273 bank->chip.direction_input = gpio_input;
1274 bank->chip.get = gpio_get;
1275 bank->chip.direction_output = gpio_output;
1276 bank->chip.set_debounce = gpio_debounce;
1277 bank->chip.set = gpio_set;
1278 bank->chip.to_irq = gpio_2irq;
1279 if (bank_is_mpuio(bank)) {
1280 bank->chip.label = "mpuio";
1281#ifdef CONFIG_ARCH_OMAP16XX
1282 bank->chip.dev = &omap_mpuio_device.dev;
1283#endif
1284 bank->chip.base = OMAP_MPUIO(0);
1285 } else {
1286 bank->chip.label = "gpio";
1287 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001288 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001289 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001290 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001291
1292 gpiochip_add(&bank->chip);
1293
1294 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001295 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001296 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001297 irq_set_chip_data(j, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001298 if (bank_is_mpuio(bank))
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001299 irq_set_chip(j, &mpuio_irq_chip);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001300 else
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001301 irq_set_chip(j, &gpio_irq_chip);
1302 irq_set_handler(j, handle_simple_irq);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001303 set_irq_flags(j, IRQF_VALID);
1304 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001305 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1306 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001307}
1308
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001309static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001310{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001311 static int gpio_init_done;
1312 struct omap_gpio_platform_data *pdata;
1313 struct resource *res;
1314 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001315 struct gpio_bank *bank;
1316
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001317 if (!pdev->dev.platform_data)
1318 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001319
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001320 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001321
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001322 if (!gpio_init_done) {
1323 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001324
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001325 ret = init_gpio_info(pdev);
1326 if (ret)
1327 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001328 }
1329
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001330 id = pdev->id;
1331 bank = &gpio_bank[id];
1332
1333 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1334 if (unlikely(!res)) {
1335 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1336 return -ENODEV;
1337 }
1338
1339 bank->irq = res->start;
1340 bank->virtual_irq_start = pdata->virtual_irq_start;
1341 bank->method = pdata->bank_type;
1342 bank->dev = &pdev->dev;
1343 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001344 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001345 bank->width = pdata->bank_width;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001346
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001347 bank->regs = pdata->regs;
1348
1349 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1350 bank->set_dataout = _set_gpio_dataout_reg;
1351 else
1352 bank->set_dataout = _set_gpio_dataout_mask;
1353
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001354 spin_lock_init(&bank->lock);
1355
1356 /* Static mapping, never released */
1357 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1358 if (unlikely(!res)) {
1359 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1360 return -ENODEV;
1361 }
1362
1363 bank->base = ioremap(res->start, resource_size(res));
1364 if (!bank->base) {
1365 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1366 return -ENOMEM;
1367 }
1368
1369 pm_runtime_enable(bank->dev);
1370 pm_runtime_get_sync(bank->dev);
1371
1372 omap_gpio_mod_init(bank, id);
1373 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001374 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001375
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001376 if (!gpio_init_done)
1377 gpio_init_done = 1;
1378
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001379 return 0;
1380}
1381
Tony Lindgren140455f2010-02-12 12:26:48 -08001382#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001383static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001384{
1385 int i;
1386
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001387 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001388 return 0;
1389
1390 for (i = 0; i < gpio_bank_count; i++) {
1391 struct gpio_bank *bank = &gpio_bank[i];
1392 void __iomem *wake_status;
1393 void __iomem *wake_clear;
1394 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001395 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001396
1397 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001398#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001399 case METHOD_GPIO_1610:
1400 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1401 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1402 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1403 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001404#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001405#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001406 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001407 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001408 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1409 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1410 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001411#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301412#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001413 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301414 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1415 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1416 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1417 break;
1418#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001419 default:
1420 continue;
1421 }
1422
David Brownella6472532008-03-03 04:33:30 -08001423 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001424 bank->saved_wakeup = __raw_readl(wake_status);
1425 __raw_writel(0xffffffff, wake_clear);
1426 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001427 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001428 }
1429
1430 return 0;
1431}
1432
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001433static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001434{
1435 int i;
1436
Tero Kristo723fdb72008-11-26 14:35:16 -08001437 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001438 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001439
1440 for (i = 0; i < gpio_bank_count; i++) {
1441 struct gpio_bank *bank = &gpio_bank[i];
1442 void __iomem *wake_clear;
1443 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001444 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001445
1446 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001447#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001448 case METHOD_GPIO_1610:
1449 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1450 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1451 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001452#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001453#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001454 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001455 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1456 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001457 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001458#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301459#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001460 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301461 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1462 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1463 break;
1464#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001465 default:
1466 continue;
1467 }
1468
David Brownella6472532008-03-03 04:33:30 -08001469 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001470 __raw_writel(0xffffffff, wake_clear);
1471 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001472 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001473 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001474}
1475
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001476static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001477 .suspend = omap_gpio_suspend,
1478 .resume = omap_gpio_resume,
1479};
1480
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001481#endif
1482
Tony Lindgren140455f2010-02-12 12:26:48 -08001483#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001484
1485static int workaround_enabled;
1486
Paul Walmsley72e06d02010-12-21 21:05:16 -07001487void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001488{
1489 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001490 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001491
Tero Kristoa118b5f2008-12-22 14:27:12 +02001492 if (cpu_is_omap34xx())
1493 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001494
Tero Kristoa118b5f2008-12-22 14:27:12 +02001495 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001496 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001497 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001498 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001499
Kevin Hilman0aed04352010-09-22 16:06:27 -07001500 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001501 clk_disable(bank->dbck);
1502
Paul Walmsley72e06d02010-12-21 21:05:16 -07001503 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001504 continue;
1505
1506 /* If going to OFF, remove triggering for all
1507 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1508 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001509 if (!(bank->enabled_non_wakeup_gpios))
1510 continue;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001511
1512 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1513 bank->saved_datain = __raw_readl(bank->base +
1514 OMAP24XX_GPIO_DATAIN);
1515 l1 = __raw_readl(bank->base +
1516 OMAP24XX_GPIO_FALLINGDETECT);
1517 l2 = __raw_readl(bank->base +
1518 OMAP24XX_GPIO_RISINGDETECT);
1519 }
1520
1521 if (cpu_is_omap44xx()) {
1522 bank->saved_datain = __raw_readl(bank->base +
1523 OMAP4_GPIO_DATAIN);
1524 l1 = __raw_readl(bank->base +
1525 OMAP4_GPIO_FALLINGDETECT);
1526 l2 = __raw_readl(bank->base +
1527 OMAP4_GPIO_RISINGDETECT);
1528 }
1529
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001530 bank->saved_fallingdetect = l1;
1531 bank->saved_risingdetect = l2;
1532 l1 &= ~bank->enabled_non_wakeup_gpios;
1533 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001534
1535 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1536 __raw_writel(l1, bank->base +
1537 OMAP24XX_GPIO_FALLINGDETECT);
1538 __raw_writel(l2, bank->base +
1539 OMAP24XX_GPIO_RISINGDETECT);
1540 }
1541
1542 if (cpu_is_omap44xx()) {
1543 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1544 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1545 }
1546
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001547 c++;
1548 }
1549 if (!c) {
1550 workaround_enabled = 0;
1551 return;
1552 }
1553 workaround_enabled = 1;
1554}
1555
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001556void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001557{
1558 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001559 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001560
Tero Kristoa118b5f2008-12-22 14:27:12 +02001561 if (cpu_is_omap34xx())
1562 min = 1;
1563 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001564 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001565 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001566 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001567
Kevin Hilman0aed04352010-09-22 16:06:27 -07001568 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001569 clk_enable(bank->dbck);
1570
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001571 if (!workaround_enabled)
1572 continue;
1573
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001574 if (!(bank->enabled_non_wakeup_gpios))
1575 continue;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001576
1577 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1578 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001579 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001580 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001581 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001582 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1583 }
1584
1585 if (cpu_is_omap44xx()) {
1586 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301587 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001588 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301589 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001590 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1591 }
1592
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001593 /* Check if any of the non-wakeup interrupt GPIOs have changed
1594 * state. If so, generate an IRQ by software. This is
1595 * horribly racy, but it's the best we can do to work around
1596 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001597 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001598 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001599
1600 /*
1601 * No need to generate IRQs for the rising edge for gpio IRQs
1602 * configured with falling edge only; and vice versa.
1603 */
1604 gen0 = l & bank->saved_fallingdetect;
1605 gen0 &= bank->saved_datain;
1606
1607 gen1 = l & bank->saved_risingdetect;
1608 gen1 &= ~(bank->saved_datain);
1609
1610 /* FIXME: Consider GPIO IRQs with level detections properly! */
1611 gen = l & (~(bank->saved_fallingdetect) &
1612 ~(bank->saved_risingdetect));
1613 /* Consider all GPIO IRQs needed to be updated */
1614 gen |= gen0 | gen1;
1615
1616 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001617 u32 old0, old1;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001618
Sergio Aguirref00d6492010-03-03 16:21:08 +00001619 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001620 old0 = __raw_readl(bank->base +
1621 OMAP24XX_GPIO_LEVELDETECT0);
1622 old1 = __raw_readl(bank->base +
1623 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001624 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001625 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001626 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001627 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001628 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001629 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001630 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001631 OMAP24XX_GPIO_LEVELDETECT1);
1632 }
1633
1634 if (cpu_is_omap44xx()) {
1635 old0 = __raw_readl(bank->base +
1636 OMAP4_GPIO_LEVELDETECT0);
1637 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301638 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001639 __raw_writel(old0 | l, bank->base +
1640 OMAP4_GPIO_LEVELDETECT0);
1641 __raw_writel(old1 | l, bank->base +
1642 OMAP4_GPIO_LEVELDETECT1);
1643 __raw_writel(old0, bank->base +
1644 OMAP4_GPIO_LEVELDETECT0);
1645 __raw_writel(old1, bank->base +
1646 OMAP4_GPIO_LEVELDETECT1);
1647 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001648 }
1649 }
1650
1651}
1652
Tony Lindgren92105bb2005-09-07 17:20:26 +01001653#endif
1654
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001655#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301656/* save the registers of bank 2-6 */
1657void omap_gpio_save_context(void)
1658{
1659 int i;
1660
1661 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1662 for (i = 1; i < gpio_bank_count; i++) {
1663 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301664 gpio_context[i].irqenable1 =
1665 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1666 gpio_context[i].irqenable2 =
1667 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1668 gpio_context[i].wake_en =
1669 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1670 gpio_context[i].ctrl =
1671 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1672 gpio_context[i].oe =
1673 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1674 gpio_context[i].leveldetect0 =
1675 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1676 gpio_context[i].leveldetect1 =
1677 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1678 gpio_context[i].risingdetect =
1679 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1680 gpio_context[i].fallingdetect =
1681 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1682 gpio_context[i].dataout =
1683 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301684 }
1685}
1686
1687/* restore the required registers of bank 2-6 */
1688void omap_gpio_restore_context(void)
1689{
1690 int i;
1691
1692 for (i = 1; i < gpio_bank_count; i++) {
1693 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301694 __raw_writel(gpio_context[i].irqenable1,
1695 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1696 __raw_writel(gpio_context[i].irqenable2,
1697 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1698 __raw_writel(gpio_context[i].wake_en,
1699 bank->base + OMAP24XX_GPIO_WAKE_EN);
1700 __raw_writel(gpio_context[i].ctrl,
1701 bank->base + OMAP24XX_GPIO_CTRL);
1702 __raw_writel(gpio_context[i].oe,
1703 bank->base + OMAP24XX_GPIO_OE);
1704 __raw_writel(gpio_context[i].leveldetect0,
1705 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1706 __raw_writel(gpio_context[i].leveldetect1,
1707 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1708 __raw_writel(gpio_context[i].risingdetect,
1709 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1710 __raw_writel(gpio_context[i].fallingdetect,
1711 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1712 __raw_writel(gpio_context[i].dataout,
1713 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301714 }
1715}
1716#endif
1717
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001718static struct platform_driver omap_gpio_driver = {
1719 .probe = omap_gpio_probe,
1720 .driver = {
1721 .name = "omap_gpio",
1722 },
1723};
1724
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001725/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001726 * gpio driver register needs to be done before
1727 * machine_init functions access gpio APIs.
1728 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001729 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001730static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001731{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001732 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001733}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001734postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001735
Tony Lindgren92105bb2005-09-07 17:20:26 +01001736static int __init omap_gpio_sysinit(void)
1737{
David Brownell11a78b72006-12-06 17:14:11 -08001738 mpuio_init();
1739
Tony Lindgren140455f2010-02-12 12:26:48 -08001740#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001741 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1742 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001743#endif
1744
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001745 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001746}
1747
Tony Lindgren92105bb2005-09-07 17:20:26 +01001748arch_initcall(omap_gpio_sysinit);