blob: 6afca28a8c677a713353e0785890cb57871e43a7 [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 Wang699117a62009-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 Wang699117a62009-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 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700506 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700507
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700508 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700509 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700510 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700511 l = ~l;
512 l &= mask;
513 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700514}
515
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700516static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100517{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100518 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100519 u32 l;
520
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700521 if (bank->regs->set_irqenable) {
522 reg += bank->regs->set_irqenable;
523 l = gpio_mask;
524 } else {
525 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100526 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700527 if (bank->regs->irqenable_inv)
528 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529 else
530 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100531 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700532
533 __raw_writel(l, reg);
534}
535
536static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
537{
538 void __iomem *reg = bank->base;
539 u32 l;
540
541 if (bank->regs->clr_irqenable) {
542 reg += bank->regs->clr_irqenable;
543 l = gpio_mask;
544 } else {
545 reg += bank->regs->irqenable;
546 l = __raw_readl(reg);
547 if (bank->regs->irqenable_inv)
548 l |= gpio_mask;
549 else
550 l &= ~gpio_mask;
551 }
552
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100553 __raw_writel(l, reg);
554}
555
556static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
557{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700558 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100559}
560
Tony Lindgren92105bb2005-09-07 17:20:26 +0100561/*
562 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
563 * 1510 does not seem to have a wake-up register. If JTAG is connected
564 * to the target, system will wake up always on GPIO events. While
565 * system is running all registered GPIO interrupts need to have wake-up
566 * enabled. When system is suspended, only selected GPIO interrupts need
567 * to have wake-up enabled.
568 */
569static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
570{
Tony Lindgren4cc64202010-01-08 10:29:05 -0800571 unsigned long uninitialized_var(flags);
David Brownella6472532008-03-03 04:33:30 -0800572
Tony Lindgren92105bb2005-09-07 17:20:26 +0100573 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800574#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800575 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100576 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800577 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700578 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100579 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700580 else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100581 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800582 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100583 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800584#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800585#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800586 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a2010-02-15 09:27:25 -0800587 case METHOD_GPIO_44XX:
David Brownell11a78b72006-12-06 17:14:11 -0800588 if (bank->non_wakeup_gpios & (1 << gpio)) {
589 printk(KERN_ERR "Unable to modify wakeup on "
590 "non-wakeup GPIO%d\n",
Kevin Hilmand5f46242011-04-21 09:23:00 -0700591 (bank - gpio_bank) * bank->width + gpio);
David Brownell11a78b72006-12-06 17:14:11 -0800592 return -EINVAL;
593 }
David Brownella6472532008-03-03 04:33:30 -0800594 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700595 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800596 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700597 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800598 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800599 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800600 return 0;
601#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100602 default:
603 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
604 bank->method);
605 return -EINVAL;
606 }
607}
608
Tony Lindgren4196dd62006-09-25 12:41:38 +0300609static void _reset_gpio(struct gpio_bank *bank, int gpio)
610{
Kevin Hilman129fd222011-04-22 07:59:07 -0700611 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300612 _set_gpio_irqenable(bank, gpio, 0);
613 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700614 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300615}
616
Tony Lindgren92105bb2005-09-07 17:20:26 +0100617/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100618static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100619{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100620 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100621 struct gpio_bank *bank;
622 int retval;
623
624 if (check_gpio(gpio) < 0)
625 return -ENODEV;
Lennert Buytenheke9191022010-11-29 11:17:17 +0100626 bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700627 retval = _set_gpio_wakeup(bank, GPIO_INDEX(bank, gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100628
629 return retval;
630}
631
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800632static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100633{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800634 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800635 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100636
David Brownella6472532008-03-03 04:33:30 -0800637 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100638
Tony Lindgren4196dd62006-09-25 12:41:38 +0300639 /* Set trigger to none. You need to enable the desired trigger with
640 * request_irq() or set_irq_type().
641 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800642 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100643
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000644#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100645 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100646 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100647
Tony Lindgren92105bb2005-09-07 17:20:26 +0100648 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100649 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800650 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100651 }
652#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800653 if (!cpu_class_is_omap1()) {
654 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700655 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800656 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700657
658 if (cpu_is_omap24xx() || cpu_is_omap34xx())
659 reg += OMAP24XX_GPIO_CTRL;
660 else if (cpu_is_omap44xx())
661 reg += OMAP4_GPIO_CTRL;
662 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800663 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -0700664 ctrl &= 0xFFFFFFFE;
665 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800666 }
667 bank->mod_usage |= 1 << offset;
668 }
David Brownella6472532008-03-03 04:33:30 -0800669 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670
671 return 0;
672}
673
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800674static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800676 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800677 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100678
David Brownella6472532008-03-03 04:33:30 -0800679 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100680#ifdef CONFIG_ARCH_OMAP16XX
681 if (bank->method == METHOD_GPIO_1610) {
682 /* Disable wake-up during idle for dynamic tick */
683 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800684 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100685 }
686#endif
Charulatha V9f096862010-05-14 12:05:27 -0700687#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
688 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100689 /* Disable wake-up during idle for dynamic tick */
690 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800691 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100692 }
693#endif
Charulatha V9f096862010-05-14 12:05:27 -0700694#ifdef CONFIG_ARCH_OMAP4
695 if (bank->method == METHOD_GPIO_44XX) {
696 /* Disable wake-up during idle for dynamic tick */
697 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
698 __raw_writel(1 << offset, reg);
699 }
700#endif
Charulatha V058af1e2009-11-22 10:11:25 -0800701 if (!cpu_class_is_omap1()) {
702 bank->mod_usage &= ~(1 << offset);
703 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -0700704 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -0800705 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700706
707 if (cpu_is_omap24xx() || cpu_is_omap34xx())
708 reg += OMAP24XX_GPIO_CTRL;
709 else if (cpu_is_omap44xx())
710 reg += OMAP4_GPIO_CTRL;
711 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800712 /* Module is disabled, clocks are gated */
713 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -0700714 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800715 }
716 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800717 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800718 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100719}
720
721/*
722 * We need to unmask the GPIO bank interrupt as soon as possible to
723 * avoid missing GPIO interrupts for other lines in the bank.
724 * Then we need to mask-read-clear-unmask the triggered GPIO lines
725 * in the bank to avoid missing nested interrupts for a GPIO line.
726 * If we wait to unmask individual GPIO lines in the bank after the
727 * line's interrupt handler has been run, we may miss some nested
728 * interrupts.
729 */
Russell King10dd5ce2006-11-23 11:41:32 +0000730static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100731{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100732 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100733 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800734 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100735 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700736 u32 retrigger = 0;
737 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000738 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100739
Will Deaconee144182011-02-21 13:46:08 +0000740 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100741
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100742 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700743 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800744
745 if (WARN_ON(!isr_reg))
746 goto exit;
747
Tony Lindgren92105bb2005-09-07 17:20:26 +0100748 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100749 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700750 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100751
Imre Deakea6dedd2006-06-26 16:16:00 -0700752 enabled = _get_gpio_irqbank_mask(bank);
753 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100754
755 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
756 isr &= 0x0000ffff;
757
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800758 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800759 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -0700760 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100761
762 /* clear edge sensitive interrupts before handler(s) are
763 called so that we don't miss any interrupt occurred while
764 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700765 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100766 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700767 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100768
769 /* if there is only edge sensitive GPIO pin interrupts
770 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700771 if (!level_mask && !unmasked) {
772 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000773 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700774 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100775
Imre Deakea6dedd2006-06-26 16:16:00 -0700776 isr |= retrigger;
777 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100778 if (!isr)
779 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100780
Tony Lindgren92105bb2005-09-07 17:20:26 +0100781 gpio_irq = bank->virtual_irq_start;
782 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700783 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800784
Tony Lindgren92105bb2005-09-07 17:20:26 +0100785 if (!(isr & 1))
786 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200787
Cory Maccarrone4318f362010-01-08 10:29:04 -0800788#ifdef CONFIG_ARCH_OMAP1
789 /*
790 * Some chips can't respond to both rising and falling
791 * at the same time. If this irq was requested with
792 * both flags, we need to flip the ICR data for the IRQ
793 * to respond to the IRQ for the opposite direction.
794 * This will be indicated in the bank toggle_mask.
795 */
796 if (bank->toggle_mask & (1 << gpio_index))
797 _toggle_gpio_edge_triggering(bank, gpio_index);
798#endif
799
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100800 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100801 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000802 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700803 /* if bank has any level sensitive GPIO pin interrupt
804 configured, we must unmask the bank interrupt only after
805 handler(s) are executed in order to avoid spurious bank
806 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800807exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700808 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000809 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100810}
811
Lennert Buytenheke9191022010-11-29 11:17:17 +0100812static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300813{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100814 unsigned int gpio = d->irq - IH_GPIO_BASE;
815 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700816 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300817
Colin Cross85ec7b92011-06-06 13:38:18 -0700818 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300819 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700820 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300821}
822
Lennert Buytenheke9191022010-11-29 11:17:17 +0100823static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100824{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100825 unsigned int gpio = d->irq - IH_GPIO_BASE;
826 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100827
828 _clear_gpio_irqstatus(bank, gpio);
829}
830
Lennert Buytenheke9191022010-11-29 11:17:17 +0100831static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100832{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100833 unsigned int gpio = d->irq - IH_GPIO_BASE;
834 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700835 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100836
Colin Cross85ec7b92011-06-06 13:38:18 -0700837 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100838 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700839 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700840 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100841}
842
Lennert Buytenheke9191022010-11-29 11:17:17 +0100843static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100844{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100845 unsigned int gpio = d->irq - IH_GPIO_BASE;
846 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700847 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100848 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700849 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700850
Colin Cross85ec7b92011-06-06 13:38:18 -0700851 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700852 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700853 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800854
855 /* For level-triggered GPIOs, the clearing must be done after
856 * the HW source is cleared, thus after the handler has run */
857 if (bank->level_mask & irq_mask) {
858 _set_gpio_irqenable(bank, gpio, 0);
859 _clear_gpio_irqstatus(bank, gpio);
860 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100861
Kevin Hilman4de8c752008-01-16 21:56:14 -0800862 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700863 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100864}
865
David Brownelle5c56ed2006-12-06 17:13:59 -0800866static struct irq_chip gpio_irq_chip = {
867 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100868 .irq_shutdown = gpio_irq_shutdown,
869 .irq_ack = gpio_ack_irq,
870 .irq_mask = gpio_mask_irq,
871 .irq_unmask = gpio_unmask_irq,
872 .irq_set_type = gpio_irq_type,
873 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800874};
875
876/*---------------------------------------------------------------------*/
877
878#ifdef CONFIG_ARCH_OMAP1
879
880/* MPUIO uses the always-on 32k clock */
881
Lennert Buytenheke9191022010-11-29 11:17:17 +0100882static void mpuio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100883{
884 /* The ISR is reset automatically, so do nothing here. */
885}
886
Lennert Buytenheke9191022010-11-29 11:17:17 +0100887static void mpuio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100888{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100889 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
890 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100891
892 _set_gpio_irqenable(bank, gpio, 0);
893}
894
Lennert Buytenheke9191022010-11-29 11:17:17 +0100895static void mpuio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100896{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100897 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
898 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100899
900 _set_gpio_irqenable(bank, gpio, 1);
901}
902
David Brownelle5c56ed2006-12-06 17:13:59 -0800903static struct irq_chip mpuio_irq_chip = {
904 .name = "MPUIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100905 .irq_ack = mpuio_ack_irq,
906 .irq_mask = mpuio_mask_irq,
907 .irq_unmask = mpuio_unmask_irq,
908 .irq_set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -0800909#ifdef CONFIG_ARCH_OMAP16XX
910 /* REVISIT: assuming only 16xx supports MPUIO wake events */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100911 .irq_set_wake = gpio_wake_enable,
David Brownell11a78b72006-12-06 17:14:11 -0800912#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100913};
914
David Brownelle5c56ed2006-12-06 17:13:59 -0800915
916#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
917
David Brownell11a78b72006-12-06 17:14:11 -0800918
919#ifdef CONFIG_ARCH_OMAP16XX
920
921#include <linux/platform_device.h>
922
Magnus Damm79ee0312009-07-08 13:22:04 +0200923static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800924{
Magnus Damm79ee0312009-07-08 13:22:04 +0200925 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800926 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800927 void __iomem *mask_reg = bank->base +
928 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800929 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800930
David Brownella6472532008-03-03 04:33:30 -0800931 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800932 bank->saved_wakeup = __raw_readl(mask_reg);
933 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800934 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800935
936 return 0;
937}
938
Magnus Damm79ee0312009-07-08 13:22:04 +0200939static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800940{
Magnus Damm79ee0312009-07-08 13:22:04 +0200941 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800942 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800943 void __iomem *mask_reg = bank->base +
944 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800945 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800946
David Brownella6472532008-03-03 04:33:30 -0800947 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800948 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800949 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800950
951 return 0;
952}
953
Alexey Dobriyan47145212009-12-14 18:00:08 -0800954static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200955 .suspend_noirq = omap_mpuio_suspend_noirq,
956 .resume_noirq = omap_mpuio_resume_noirq,
957};
958
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200959/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800960static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800961 .driver = {
962 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200963 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800964 },
965};
966
967static struct platform_device omap_mpuio_device = {
968 .name = "mpuio",
969 .id = -1,
970 .dev = {
971 .driver = &omap_mpuio_driver.driver,
972 }
973 /* could list the /proc/iomem resources */
974};
975
976static inline void mpuio_init(void)
977{
Charulatha Va8be8da2011-04-22 16:38:16 +0530978 struct gpio_bank *bank = &gpio_bank[0];
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800979 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700980
David Brownell11a78b72006-12-06 17:14:11 -0800981 if (platform_driver_register(&omap_mpuio_driver) == 0)
982 (void) platform_device_register(&omap_mpuio_device);
983}
984
985#else
986static inline void mpuio_init(void) {}
987#endif /* 16xx */
988
David Brownelle5c56ed2006-12-06 17:13:59 -0800989#else
990
991extern struct irq_chip mpuio_irq_chip;
992
993#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -0800994static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -0800995
996#endif
997
998/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100999
David Brownell52e31342008-03-03 12:43:23 -08001000/* REVISIT these are stupid implementations! replace by ones that
1001 * don't switch on METHOD_* and which mostly avoid spinlocks
1002 */
1003
1004static int gpio_input(struct gpio_chip *chip, unsigned offset)
1005{
1006 struct gpio_bank *bank;
1007 unsigned long flags;
1008
1009 bank = container_of(chip, struct gpio_bank, chip);
1010 spin_lock_irqsave(&bank->lock, flags);
1011 _set_gpio_direction(bank, offset, 1);
1012 spin_unlock_irqrestore(&bank->lock, flags);
1013 return 0;
1014}
1015
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001016static int gpio_is_input(struct gpio_bank *bank, int mask)
1017{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001018 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001019
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001020 return __raw_readl(reg) & mask;
1021}
1022
David Brownell52e31342008-03-03 12:43:23 -08001023static int gpio_get(struct gpio_chip *chip, unsigned offset)
1024{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001025 struct gpio_bank *bank;
1026 void __iomem *reg;
1027 int gpio;
1028 u32 mask;
1029
1030 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +05301031 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001032 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -07001033 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001034
1035 if (gpio_is_input(bank, mask))
1036 return _get_gpio_datain(bank, gpio);
1037 else
1038 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -08001039}
1040
1041static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1042{
1043 struct gpio_bank *bank;
1044 unsigned long flags;
1045
1046 bank = container_of(chip, struct gpio_bank, chip);
1047 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001048 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001049 _set_gpio_direction(bank, offset, 0);
1050 spin_unlock_irqrestore(&bank->lock, flags);
1051 return 0;
1052}
1053
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001054static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
1055 unsigned debounce)
1056{
1057 struct gpio_bank *bank;
1058 unsigned long flags;
1059
1060 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001061
1062 if (!bank->dbck) {
1063 bank->dbck = clk_get(bank->dev, "dbclk");
1064 if (IS_ERR(bank->dbck))
1065 dev_err(bank->dev, "Could not get gpio dbck\n");
1066 }
1067
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001068 spin_lock_irqsave(&bank->lock, flags);
1069 _set_gpio_debounce(bank, offset, debounce);
1070 spin_unlock_irqrestore(&bank->lock, flags);
1071
1072 return 0;
1073}
1074
David Brownell52e31342008-03-03 12:43:23 -08001075static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1076{
1077 struct gpio_bank *bank;
1078 unsigned long flags;
1079
1080 bank = container_of(chip, struct gpio_bank, chip);
1081 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001082 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001083 spin_unlock_irqrestore(&bank->lock, flags);
1084}
1085
David Brownella007b702008-12-10 17:35:25 -08001086static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1087{
1088 struct gpio_bank *bank;
1089
1090 bank = container_of(chip, struct gpio_bank, chip);
1091 return bank->virtual_irq_start + offset;
1092}
1093
David Brownell52e31342008-03-03 12:43:23 -08001094/*---------------------------------------------------------------------*/
1095
Tony Lindgren9a748052010-12-07 16:26:56 -08001096static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001097{
1098 u32 rev;
1099
Tony Lindgren9a748052010-12-07 16:26:56 -08001100 if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
1101 rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001102 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001103 rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001104 else if (cpu_is_omap44xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001105 rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001106 else
1107 return;
1108
1109 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1110 (rev >> 4) & 0x0f, rev & 0x0f);
1111}
1112
David Brownell8ba55c52008-02-26 11:10:50 -08001113/* This lock class tells lockdep that GPIO irqs are in a different
1114 * category than their parents, so it won't report false recursion.
1115 */
1116static struct lock_class_key gpio_lock_class;
1117
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001118static inline int init_gpio_info(struct platform_device *pdev)
1119{
1120 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1121 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1122 GFP_KERNEL);
1123 if (!gpio_bank) {
1124 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1125 return -ENOMEM;
1126 }
1127 return 0;
1128}
1129
1130/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001131static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1132{
1133 if (cpu_class_is_omap2()) {
1134 if (cpu_is_omap44xx()) {
1135 __raw_writel(0xffffffff, bank->base +
1136 OMAP4_GPIO_IRQSTATUSCLR0);
1137 __raw_writel(0x00000000, bank->base +
1138 OMAP4_GPIO_DEBOUNCENABLE);
1139 /* Initialize interface clk ungated, module enabled */
1140 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1141 } else if (cpu_is_omap34xx()) {
1142 __raw_writel(0x00000000, bank->base +
1143 OMAP24XX_GPIO_IRQENABLE1);
1144 __raw_writel(0xffffffff, bank->base +
1145 OMAP24XX_GPIO_IRQSTATUS1);
1146 __raw_writel(0x00000000, bank->base +
1147 OMAP24XX_GPIO_DEBOUNCE_EN);
1148
1149 /* Initialize interface clk ungated, module enabled */
1150 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1151 } else if (cpu_is_omap24xx()) {
1152 static const u32 non_wakeup_gpios[] = {
1153 0xe203ffc0, 0x08700040
1154 };
1155 if (id < ARRAY_SIZE(non_wakeup_gpios))
1156 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1157 }
1158 } else if (cpu_class_is_omap1()) {
1159 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001160 __raw_writew(0xffff, bank->base +
1161 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001162 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1163 __raw_writew(0xffff, bank->base
1164 + OMAP1510_GPIO_INT_MASK);
1165 __raw_writew(0x0000, bank->base
1166 + OMAP1510_GPIO_INT_STATUS);
1167 }
1168 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1169 __raw_writew(0x0000, bank->base
1170 + OMAP1610_GPIO_IRQENABLE1);
1171 __raw_writew(0xffff, bank->base
1172 + OMAP1610_GPIO_IRQSTATUS1);
1173 __raw_writew(0x0014, bank->base
1174 + OMAP1610_GPIO_SYSCONFIG);
1175
1176 /*
1177 * Enable system clock for GPIO module.
1178 * The CAM_CLK_CTRL *is* really the right place.
1179 */
1180 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1181 ULPD_CAM_CLK_CTRL);
1182 }
1183 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1184 __raw_writel(0xffffffff, bank->base
1185 + OMAP7XX_GPIO_INT_MASK);
1186 __raw_writel(0x00000000, bank->base
1187 + OMAP7XX_GPIO_INT_STATUS);
1188 }
1189 }
1190}
1191
Russell Kingd52b31d2011-05-27 13:56:12 -07001192static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001193{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001194 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001195 static int gpio;
1196
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001197 bank->mod_usage = 0;
1198 /*
1199 * REVISIT eventually switch from OMAP-specific gpio structs
1200 * over to the generic ones
1201 */
1202 bank->chip.request = omap_gpio_request;
1203 bank->chip.free = omap_gpio_free;
1204 bank->chip.direction_input = gpio_input;
1205 bank->chip.get = gpio_get;
1206 bank->chip.direction_output = gpio_output;
1207 bank->chip.set_debounce = gpio_debounce;
1208 bank->chip.set = gpio_set;
1209 bank->chip.to_irq = gpio_2irq;
1210 if (bank_is_mpuio(bank)) {
1211 bank->chip.label = "mpuio";
1212#ifdef CONFIG_ARCH_OMAP16XX
1213 bank->chip.dev = &omap_mpuio_device.dev;
1214#endif
1215 bank->chip.base = OMAP_MPUIO(0);
1216 } else {
1217 bank->chip.label = "gpio";
1218 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001219 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001220 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001221 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001222
1223 gpiochip_add(&bank->chip);
1224
1225 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001226 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001227 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001228 irq_set_chip_data(j, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001229 if (bank_is_mpuio(bank))
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001230 irq_set_chip(j, &mpuio_irq_chip);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001231 else
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001232 irq_set_chip(j, &gpio_irq_chip);
1233 irq_set_handler(j, handle_simple_irq);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001234 set_irq_flags(j, IRQF_VALID);
1235 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001236 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1237 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001238}
1239
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001240static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001241{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001242 static int gpio_init_done;
1243 struct omap_gpio_platform_data *pdata;
1244 struct resource *res;
1245 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001246 struct gpio_bank *bank;
1247
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001248 if (!pdev->dev.platform_data)
1249 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001250
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001251 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001252
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001253 if (!gpio_init_done) {
1254 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001255
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001256 ret = init_gpio_info(pdev);
1257 if (ret)
1258 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001259 }
1260
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001261 id = pdev->id;
1262 bank = &gpio_bank[id];
1263
1264 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1265 if (unlikely(!res)) {
1266 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1267 return -ENODEV;
1268 }
1269
1270 bank->irq = res->start;
1271 bank->virtual_irq_start = pdata->virtual_irq_start;
1272 bank->method = pdata->bank_type;
1273 bank->dev = &pdev->dev;
1274 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001275 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001276 bank->width = pdata->bank_width;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001277
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001278 bank->regs = pdata->regs;
1279
1280 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1281 bank->set_dataout = _set_gpio_dataout_reg;
1282 else
1283 bank->set_dataout = _set_gpio_dataout_mask;
1284
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001285 spin_lock_init(&bank->lock);
1286
1287 /* Static mapping, never released */
1288 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1289 if (unlikely(!res)) {
1290 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1291 return -ENODEV;
1292 }
1293
1294 bank->base = ioremap(res->start, resource_size(res));
1295 if (!bank->base) {
1296 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1297 return -ENOMEM;
1298 }
1299
1300 pm_runtime_enable(bank->dev);
1301 pm_runtime_get_sync(bank->dev);
1302
1303 omap_gpio_mod_init(bank, id);
1304 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001305 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001306
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001307 if (!gpio_init_done)
1308 gpio_init_done = 1;
1309
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001310 return 0;
1311}
1312
Tony Lindgren140455f2010-02-12 12:26:48 -08001313#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001314static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001315{
1316 int i;
1317
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001318 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001319 return 0;
1320
1321 for (i = 0; i < gpio_bank_count; i++) {
1322 struct gpio_bank *bank = &gpio_bank[i];
1323 void __iomem *wake_status;
1324 void __iomem *wake_clear;
1325 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001326 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001327
1328 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001329#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001330 case METHOD_GPIO_1610:
1331 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1332 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1333 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1334 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001335#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001336#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001337 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001338 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001339 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1340 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1341 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001342#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301343#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001344 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301345 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1346 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1347 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1348 break;
1349#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001350 default:
1351 continue;
1352 }
1353
David Brownella6472532008-03-03 04:33:30 -08001354 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001355 bank->saved_wakeup = __raw_readl(wake_status);
1356 __raw_writel(0xffffffff, wake_clear);
1357 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001358 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001359 }
1360
1361 return 0;
1362}
1363
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001364static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001365{
1366 int i;
1367
Tero Kristo723fdb72008-11-26 14:35:16 -08001368 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001369 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001370
1371 for (i = 0; i < gpio_bank_count; i++) {
1372 struct gpio_bank *bank = &gpio_bank[i];
1373 void __iomem *wake_clear;
1374 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001375 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001376
1377 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001378#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001379 case METHOD_GPIO_1610:
1380 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1381 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1382 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001383#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001384#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001385 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001386 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1387 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001388 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001389#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301390#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001391 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301392 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1393 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1394 break;
1395#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001396 default:
1397 continue;
1398 }
1399
David Brownella6472532008-03-03 04:33:30 -08001400 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001401 __raw_writel(0xffffffff, wake_clear);
1402 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001403 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001404 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001405}
1406
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001407static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001408 .suspend = omap_gpio_suspend,
1409 .resume = omap_gpio_resume,
1410};
1411
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001412#endif
1413
Tony Lindgren140455f2010-02-12 12:26:48 -08001414#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001415
1416static int workaround_enabled;
1417
Paul Walmsley72e06d02010-12-21 21:05:16 -07001418void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001419{
1420 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001421 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001422
Tero Kristoa118b5f2008-12-22 14:27:12 +02001423 if (cpu_is_omap34xx())
1424 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001425
Tero Kristoa118b5f2008-12-22 14:27:12 +02001426 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001427 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001428 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001429 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001430
Kevin Hilman0aed04352010-09-22 16:06:27 -07001431 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001432 clk_disable(bank->dbck);
1433
Paul Walmsley72e06d02010-12-21 21:05:16 -07001434 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001435 continue;
1436
1437 /* If going to OFF, remove triggering for all
1438 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1439 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001440 if (!(bank->enabled_non_wakeup_gpios))
1441 continue;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001442
1443 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1444 bank->saved_datain = __raw_readl(bank->base +
1445 OMAP24XX_GPIO_DATAIN);
1446 l1 = __raw_readl(bank->base +
1447 OMAP24XX_GPIO_FALLINGDETECT);
1448 l2 = __raw_readl(bank->base +
1449 OMAP24XX_GPIO_RISINGDETECT);
1450 }
1451
1452 if (cpu_is_omap44xx()) {
1453 bank->saved_datain = __raw_readl(bank->base +
1454 OMAP4_GPIO_DATAIN);
1455 l1 = __raw_readl(bank->base +
1456 OMAP4_GPIO_FALLINGDETECT);
1457 l2 = __raw_readl(bank->base +
1458 OMAP4_GPIO_RISINGDETECT);
1459 }
1460
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001461 bank->saved_fallingdetect = l1;
1462 bank->saved_risingdetect = l2;
1463 l1 &= ~bank->enabled_non_wakeup_gpios;
1464 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001465
1466 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1467 __raw_writel(l1, bank->base +
1468 OMAP24XX_GPIO_FALLINGDETECT);
1469 __raw_writel(l2, bank->base +
1470 OMAP24XX_GPIO_RISINGDETECT);
1471 }
1472
1473 if (cpu_is_omap44xx()) {
1474 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1475 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1476 }
1477
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001478 c++;
1479 }
1480 if (!c) {
1481 workaround_enabled = 0;
1482 return;
1483 }
1484 workaround_enabled = 1;
1485}
1486
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001487void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001488{
1489 int i;
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;
1494 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001495 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001496 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001497 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001498
Kevin Hilman0aed04352010-09-22 16:06:27 -07001499 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001500 clk_enable(bank->dbck);
1501
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001502 if (!workaround_enabled)
1503 continue;
1504
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001505 if (!(bank->enabled_non_wakeup_gpios))
1506 continue;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001507
1508 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1509 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001510 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001511 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001512 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001513 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1514 }
1515
1516 if (cpu_is_omap44xx()) {
1517 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301518 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001519 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301520 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001521 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1522 }
1523
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001524 /* Check if any of the non-wakeup interrupt GPIOs have changed
1525 * state. If so, generate an IRQ by software. This is
1526 * horribly racy, but it's the best we can do to work around
1527 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001528 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001529 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001530
1531 /*
1532 * No need to generate IRQs for the rising edge for gpio IRQs
1533 * configured with falling edge only; and vice versa.
1534 */
1535 gen0 = l & bank->saved_fallingdetect;
1536 gen0 &= bank->saved_datain;
1537
1538 gen1 = l & bank->saved_risingdetect;
1539 gen1 &= ~(bank->saved_datain);
1540
1541 /* FIXME: Consider GPIO IRQs with level detections properly! */
1542 gen = l & (~(bank->saved_fallingdetect) &
1543 ~(bank->saved_risingdetect));
1544 /* Consider all GPIO IRQs needed to be updated */
1545 gen |= gen0 | gen1;
1546
1547 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001548 u32 old0, old1;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001549
Sergio Aguirref00d6492010-03-03 16:21:08 +00001550 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001551 old0 = __raw_readl(bank->base +
1552 OMAP24XX_GPIO_LEVELDETECT0);
1553 old1 = __raw_readl(bank->base +
1554 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001555 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001556 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001557 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001558 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001559 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001560 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001561 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001562 OMAP24XX_GPIO_LEVELDETECT1);
1563 }
1564
1565 if (cpu_is_omap44xx()) {
1566 old0 = __raw_readl(bank->base +
1567 OMAP4_GPIO_LEVELDETECT0);
1568 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301569 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001570 __raw_writel(old0 | l, bank->base +
1571 OMAP4_GPIO_LEVELDETECT0);
1572 __raw_writel(old1 | l, bank->base +
1573 OMAP4_GPIO_LEVELDETECT1);
1574 __raw_writel(old0, bank->base +
1575 OMAP4_GPIO_LEVELDETECT0);
1576 __raw_writel(old1, bank->base +
1577 OMAP4_GPIO_LEVELDETECT1);
1578 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001579 }
1580 }
1581
1582}
1583
Tony Lindgren92105bb2005-09-07 17:20:26 +01001584#endif
1585
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001586#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301587/* save the registers of bank 2-6 */
1588void omap_gpio_save_context(void)
1589{
1590 int i;
1591
1592 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1593 for (i = 1; i < gpio_bank_count; i++) {
1594 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301595 gpio_context[i].irqenable1 =
1596 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1597 gpio_context[i].irqenable2 =
1598 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1599 gpio_context[i].wake_en =
1600 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1601 gpio_context[i].ctrl =
1602 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1603 gpio_context[i].oe =
1604 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1605 gpio_context[i].leveldetect0 =
1606 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1607 gpio_context[i].leveldetect1 =
1608 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1609 gpio_context[i].risingdetect =
1610 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1611 gpio_context[i].fallingdetect =
1612 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1613 gpio_context[i].dataout =
1614 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301615 }
1616}
1617
1618/* restore the required registers of bank 2-6 */
1619void omap_gpio_restore_context(void)
1620{
1621 int i;
1622
1623 for (i = 1; i < gpio_bank_count; i++) {
1624 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301625 __raw_writel(gpio_context[i].irqenable1,
1626 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1627 __raw_writel(gpio_context[i].irqenable2,
1628 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1629 __raw_writel(gpio_context[i].wake_en,
1630 bank->base + OMAP24XX_GPIO_WAKE_EN);
1631 __raw_writel(gpio_context[i].ctrl,
1632 bank->base + OMAP24XX_GPIO_CTRL);
1633 __raw_writel(gpio_context[i].oe,
1634 bank->base + OMAP24XX_GPIO_OE);
1635 __raw_writel(gpio_context[i].leveldetect0,
1636 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1637 __raw_writel(gpio_context[i].leveldetect1,
1638 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1639 __raw_writel(gpio_context[i].risingdetect,
1640 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1641 __raw_writel(gpio_context[i].fallingdetect,
1642 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1643 __raw_writel(gpio_context[i].dataout,
1644 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301645 }
1646}
1647#endif
1648
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001649static struct platform_driver omap_gpio_driver = {
1650 .probe = omap_gpio_probe,
1651 .driver = {
1652 .name = "omap_gpio",
1653 },
1654};
1655
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001656/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001657 * gpio driver register needs to be done before
1658 * machine_init functions access gpio APIs.
1659 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001660 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001661static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001662{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001663 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001664}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001665postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001666
Tony Lindgren92105bb2005-09-07 17:20:26 +01001667static int __init omap_gpio_sysinit(void)
1668{
David Brownell11a78b72006-12-06 17:14:11 -08001669 mpuio_init();
1670
Tony Lindgren140455f2010-02-12 12:26:48 -08001671#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001672 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1673 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001674#endif
1675
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001676 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001677}
1678
Tony Lindgren92105bb2005-09-07 17:20:26 +01001679arch_initcall(omap_gpio_sysinit);