blob: e5667baf5004e8a943abde0f08928bb0dfbc99e1 [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 Lindgren3f1686a92010-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 Lindgren3f1686a92010-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
David Brownelle5c56ed2006-12-06 17:13:59 -0800880#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
881
David Brownell11a78b72006-12-06 17:14:11 -0800882#ifdef CONFIG_ARCH_OMAP16XX
883
884#include <linux/platform_device.h>
885
Magnus Damm79ee0312009-07-08 13:22:04 +0200886static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800887{
Magnus Damm79ee0312009-07-08 13:22:04 +0200888 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800889 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800890 void __iomem *mask_reg = bank->base +
891 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800892 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800893
David Brownella6472532008-03-03 04:33:30 -0800894 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800895 bank->saved_wakeup = __raw_readl(mask_reg);
896 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800897 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800898
899 return 0;
900}
901
Magnus Damm79ee0312009-07-08 13:22:04 +0200902static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800903{
Magnus Damm79ee0312009-07-08 13:22:04 +0200904 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800905 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800906 void __iomem *mask_reg = bank->base +
907 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800908 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800909
David Brownella6472532008-03-03 04:33:30 -0800910 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800911 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800912 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800913
914 return 0;
915}
916
Alexey Dobriyan47145212009-12-14 18:00:08 -0800917static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200918 .suspend_noirq = omap_mpuio_suspend_noirq,
919 .resume_noirq = omap_mpuio_resume_noirq,
920};
921
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200922/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800923static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800924 .driver = {
925 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200926 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800927 },
928};
929
930static struct platform_device omap_mpuio_device = {
931 .name = "mpuio",
932 .id = -1,
933 .dev = {
934 .driver = &omap_mpuio_driver.driver,
935 }
936 /* could list the /proc/iomem resources */
937};
938
939static inline void mpuio_init(void)
940{
Charulatha Va8be8da2011-04-22 16:38:16 +0530941 struct gpio_bank *bank = &gpio_bank[0];
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800942 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700943
David Brownell11a78b72006-12-06 17:14:11 -0800944 if (platform_driver_register(&omap_mpuio_driver) == 0)
945 (void) platform_device_register(&omap_mpuio_device);
946}
947
948#else
949static inline void mpuio_init(void) {}
950#endif /* 16xx */
951
David Brownelle5c56ed2006-12-06 17:13:59 -0800952#else
953
David Brownelle5c56ed2006-12-06 17:13:59 -0800954#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -0800955static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -0800956
957#endif
958
959/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100960
David Brownell52e31342008-03-03 12:43:23 -0800961/* REVISIT these are stupid implementations! replace by ones that
962 * don't switch on METHOD_* and which mostly avoid spinlocks
963 */
964
965static int gpio_input(struct gpio_chip *chip, unsigned offset)
966{
967 struct gpio_bank *bank;
968 unsigned long flags;
969
970 bank = container_of(chip, struct gpio_bank, chip);
971 spin_lock_irqsave(&bank->lock, flags);
972 _set_gpio_direction(bank, offset, 1);
973 spin_unlock_irqrestore(&bank->lock, flags);
974 return 0;
975}
976
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300977static int gpio_is_input(struct gpio_bank *bank, int mask)
978{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700979 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300980
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300981 return __raw_readl(reg) & mask;
982}
983
David Brownell52e31342008-03-03 12:43:23 -0800984static int gpio_get(struct gpio_chip *chip, unsigned offset)
985{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300986 struct gpio_bank *bank;
987 void __iomem *reg;
988 int gpio;
989 u32 mask;
990
991 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530992 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300993 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700994 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300995
996 if (gpio_is_input(bank, mask))
997 return _get_gpio_datain(bank, gpio);
998 else
999 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -08001000}
1001
1002static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1003{
1004 struct gpio_bank *bank;
1005 unsigned long flags;
1006
1007 bank = container_of(chip, struct gpio_bank, chip);
1008 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001009 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001010 _set_gpio_direction(bank, offset, 0);
1011 spin_unlock_irqrestore(&bank->lock, flags);
1012 return 0;
1013}
1014
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001015static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
1016 unsigned debounce)
1017{
1018 struct gpio_bank *bank;
1019 unsigned long flags;
1020
1021 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001022
1023 if (!bank->dbck) {
1024 bank->dbck = clk_get(bank->dev, "dbclk");
1025 if (IS_ERR(bank->dbck))
1026 dev_err(bank->dev, "Could not get gpio dbck\n");
1027 }
1028
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001029 spin_lock_irqsave(&bank->lock, flags);
1030 _set_gpio_debounce(bank, offset, debounce);
1031 spin_unlock_irqrestore(&bank->lock, flags);
1032
1033 return 0;
1034}
1035
David Brownell52e31342008-03-03 12:43:23 -08001036static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1037{
1038 struct gpio_bank *bank;
1039 unsigned long flags;
1040
1041 bank = container_of(chip, struct gpio_bank, chip);
1042 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001043 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001044 spin_unlock_irqrestore(&bank->lock, flags);
1045}
1046
David Brownella007b702008-12-10 17:35:25 -08001047static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1048{
1049 struct gpio_bank *bank;
1050
1051 bank = container_of(chip, struct gpio_bank, chip);
1052 return bank->virtual_irq_start + offset;
1053}
1054
David Brownell52e31342008-03-03 12:43:23 -08001055/*---------------------------------------------------------------------*/
1056
Tony Lindgren9a748052010-12-07 16:26:56 -08001057static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001058{
1059 u32 rev;
1060
Tony Lindgren9a748052010-12-07 16:26:56 -08001061 if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
1062 rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001063 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001064 rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001065 else if (cpu_is_omap44xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001066 rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001067 else
1068 return;
1069
1070 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1071 (rev >> 4) & 0x0f, rev & 0x0f);
1072}
1073
David Brownell8ba55c52008-02-26 11:10:50 -08001074/* This lock class tells lockdep that GPIO irqs are in a different
1075 * category than their parents, so it won't report false recursion.
1076 */
1077static struct lock_class_key gpio_lock_class;
1078
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001079static inline int init_gpio_info(struct platform_device *pdev)
1080{
1081 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1082 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1083 GFP_KERNEL);
1084 if (!gpio_bank) {
1085 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1086 return -ENOMEM;
1087 }
1088 return 0;
1089}
1090
1091/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001092static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1093{
1094 if (cpu_class_is_omap2()) {
1095 if (cpu_is_omap44xx()) {
1096 __raw_writel(0xffffffff, bank->base +
1097 OMAP4_GPIO_IRQSTATUSCLR0);
1098 __raw_writel(0x00000000, bank->base +
1099 OMAP4_GPIO_DEBOUNCENABLE);
1100 /* Initialize interface clk ungated, module enabled */
1101 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1102 } else if (cpu_is_omap34xx()) {
1103 __raw_writel(0x00000000, bank->base +
1104 OMAP24XX_GPIO_IRQENABLE1);
1105 __raw_writel(0xffffffff, bank->base +
1106 OMAP24XX_GPIO_IRQSTATUS1);
1107 __raw_writel(0x00000000, bank->base +
1108 OMAP24XX_GPIO_DEBOUNCE_EN);
1109
1110 /* Initialize interface clk ungated, module enabled */
1111 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1112 } else if (cpu_is_omap24xx()) {
1113 static const u32 non_wakeup_gpios[] = {
1114 0xe203ffc0, 0x08700040
1115 };
1116 if (id < ARRAY_SIZE(non_wakeup_gpios))
1117 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1118 }
1119 } else if (cpu_class_is_omap1()) {
1120 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001121 __raw_writew(0xffff, bank->base +
1122 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001123 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1124 __raw_writew(0xffff, bank->base
1125 + OMAP1510_GPIO_INT_MASK);
1126 __raw_writew(0x0000, bank->base
1127 + OMAP1510_GPIO_INT_STATUS);
1128 }
1129 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1130 __raw_writew(0x0000, bank->base
1131 + OMAP1610_GPIO_IRQENABLE1);
1132 __raw_writew(0xffff, bank->base
1133 + OMAP1610_GPIO_IRQSTATUS1);
1134 __raw_writew(0x0014, bank->base
1135 + OMAP1610_GPIO_SYSCONFIG);
1136
1137 /*
1138 * Enable system clock for GPIO module.
1139 * The CAM_CLK_CTRL *is* really the right place.
1140 */
1141 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1142 ULPD_CAM_CLK_CTRL);
1143 }
1144 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1145 __raw_writel(0xffffffff, bank->base
1146 + OMAP7XX_GPIO_INT_MASK);
1147 __raw_writel(0x00000000, bank->base
1148 + OMAP7XX_GPIO_INT_STATUS);
1149 }
1150 }
1151}
1152
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001153static __init void
1154omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1155 unsigned int num)
1156{
1157 struct irq_chip_generic *gc;
1158 struct irq_chip_type *ct;
1159
1160 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1161 handle_simple_irq);
1162 ct = gc->chip_types;
1163
1164 /* NOTE: No ack required, reading IRQ status clears it. */
1165 ct->chip.irq_mask = irq_gc_mask_set_bit;
1166 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1167 ct->chip.irq_set_type = gpio_irq_type;
1168 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1169 if (cpu_is_omap16xx())
1170 ct->chip.irq_set_wake = gpio_wake_enable,
1171
1172 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1173 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1174 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1175}
1176
Russell Kingd52b31d2011-05-27 13:56:12 -07001177static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001178{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001179 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001180 static int gpio;
1181
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001182 bank->mod_usage = 0;
1183 /*
1184 * REVISIT eventually switch from OMAP-specific gpio structs
1185 * over to the generic ones
1186 */
1187 bank->chip.request = omap_gpio_request;
1188 bank->chip.free = omap_gpio_free;
1189 bank->chip.direction_input = gpio_input;
1190 bank->chip.get = gpio_get;
1191 bank->chip.direction_output = gpio_output;
1192 bank->chip.set_debounce = gpio_debounce;
1193 bank->chip.set = gpio_set;
1194 bank->chip.to_irq = gpio_2irq;
1195 if (bank_is_mpuio(bank)) {
1196 bank->chip.label = "mpuio";
1197#ifdef CONFIG_ARCH_OMAP16XX
1198 bank->chip.dev = &omap_mpuio_device.dev;
1199#endif
1200 bank->chip.base = OMAP_MPUIO(0);
1201 } else {
1202 bank->chip.label = "gpio";
1203 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001204 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001205 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001206 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001207
1208 gpiochip_add(&bank->chip);
1209
1210 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001211 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001212 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001213 irq_set_chip_data(j, bank);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001214 if (bank_is_mpuio(bank)) {
1215 omap_mpuio_alloc_gc(bank, j, bank->width);
1216 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001217 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001218 irq_set_handler(j, handle_simple_irq);
1219 set_irq_flags(j, IRQF_VALID);
1220 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001221 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001222 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1223 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001224}
1225
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001226static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001227{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001228 static int gpio_init_done;
1229 struct omap_gpio_platform_data *pdata;
1230 struct resource *res;
1231 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001232 struct gpio_bank *bank;
1233
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001234 if (!pdev->dev.platform_data)
1235 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001236
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001237 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001238
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001239 if (!gpio_init_done) {
1240 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001241
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001242 ret = init_gpio_info(pdev);
1243 if (ret)
1244 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001245 }
1246
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001247 id = pdev->id;
1248 bank = &gpio_bank[id];
1249
1250 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1251 if (unlikely(!res)) {
1252 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1253 return -ENODEV;
1254 }
1255
1256 bank->irq = res->start;
1257 bank->virtual_irq_start = pdata->virtual_irq_start;
1258 bank->method = pdata->bank_type;
1259 bank->dev = &pdev->dev;
1260 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001261 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001262 bank->width = pdata->bank_width;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001263
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001264 bank->regs = pdata->regs;
1265
1266 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1267 bank->set_dataout = _set_gpio_dataout_reg;
1268 else
1269 bank->set_dataout = _set_gpio_dataout_mask;
1270
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001271 spin_lock_init(&bank->lock);
1272
1273 /* Static mapping, never released */
1274 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1275 if (unlikely(!res)) {
1276 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1277 return -ENODEV;
1278 }
1279
1280 bank->base = ioremap(res->start, resource_size(res));
1281 if (!bank->base) {
1282 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1283 return -ENOMEM;
1284 }
1285
1286 pm_runtime_enable(bank->dev);
1287 pm_runtime_get_sync(bank->dev);
1288
1289 omap_gpio_mod_init(bank, id);
1290 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001291 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001292
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001293 if (!gpio_init_done)
1294 gpio_init_done = 1;
1295
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001296 return 0;
1297}
1298
Tony Lindgren140455f2010-02-12 12:26:48 -08001299#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001300static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001301{
1302 int i;
1303
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001304 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001305 return 0;
1306
1307 for (i = 0; i < gpio_bank_count; i++) {
1308 struct gpio_bank *bank = &gpio_bank[i];
1309 void __iomem *wake_status;
1310 void __iomem *wake_clear;
1311 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001312 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001313
1314 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001315#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001316 case METHOD_GPIO_1610:
1317 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1318 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1319 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1320 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001321#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001322#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001323 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001324 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001325 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1326 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1327 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001328#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301329#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001330 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301331 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1332 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1333 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1334 break;
1335#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001336 default:
1337 continue;
1338 }
1339
David Brownella6472532008-03-03 04:33:30 -08001340 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001341 bank->saved_wakeup = __raw_readl(wake_status);
1342 __raw_writel(0xffffffff, wake_clear);
1343 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001344 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001345 }
1346
1347 return 0;
1348}
1349
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001350static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001351{
1352 int i;
1353
Tero Kristo723fdb72008-11-26 14:35:16 -08001354 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001355 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001356
1357 for (i = 0; i < gpio_bank_count; i++) {
1358 struct gpio_bank *bank = &gpio_bank[i];
1359 void __iomem *wake_clear;
1360 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001361 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001362
1363 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001364#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001365 case METHOD_GPIO_1610:
1366 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1367 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1368 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001369#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001370#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001371 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001372 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1373 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001374 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001375#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301376#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001377 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301378 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1379 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1380 break;
1381#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001382 default:
1383 continue;
1384 }
1385
David Brownella6472532008-03-03 04:33:30 -08001386 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001387 __raw_writel(0xffffffff, wake_clear);
1388 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001389 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001390 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001391}
1392
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001393static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001394 .suspend = omap_gpio_suspend,
1395 .resume = omap_gpio_resume,
1396};
1397
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001398#endif
1399
Tony Lindgren140455f2010-02-12 12:26:48 -08001400#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001401
1402static int workaround_enabled;
1403
Paul Walmsley72e06d02010-12-21 21:05:16 -07001404void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001405{
1406 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001407 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001408
Tero Kristoa118b5f2008-12-22 14:27:12 +02001409 if (cpu_is_omap34xx())
1410 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001411
Tero Kristoa118b5f2008-12-22 14:27:12 +02001412 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001413 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001414 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001415 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001416
Kevin Hilman0aed04352010-09-22 16:06:27 -07001417 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001418 clk_disable(bank->dbck);
1419
Paul Walmsley72e06d02010-12-21 21:05:16 -07001420 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001421 continue;
1422
1423 /* If going to OFF, remove triggering for all
1424 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1425 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001426 if (!(bank->enabled_non_wakeup_gpios))
1427 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001428
1429 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1430 bank->saved_datain = __raw_readl(bank->base +
1431 OMAP24XX_GPIO_DATAIN);
1432 l1 = __raw_readl(bank->base +
1433 OMAP24XX_GPIO_FALLINGDETECT);
1434 l2 = __raw_readl(bank->base +
1435 OMAP24XX_GPIO_RISINGDETECT);
1436 }
1437
1438 if (cpu_is_omap44xx()) {
1439 bank->saved_datain = __raw_readl(bank->base +
1440 OMAP4_GPIO_DATAIN);
1441 l1 = __raw_readl(bank->base +
1442 OMAP4_GPIO_FALLINGDETECT);
1443 l2 = __raw_readl(bank->base +
1444 OMAP4_GPIO_RISINGDETECT);
1445 }
1446
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001447 bank->saved_fallingdetect = l1;
1448 bank->saved_risingdetect = l2;
1449 l1 &= ~bank->enabled_non_wakeup_gpios;
1450 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001451
1452 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1453 __raw_writel(l1, bank->base +
1454 OMAP24XX_GPIO_FALLINGDETECT);
1455 __raw_writel(l2, bank->base +
1456 OMAP24XX_GPIO_RISINGDETECT);
1457 }
1458
1459 if (cpu_is_omap44xx()) {
1460 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1461 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1462 }
1463
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001464 c++;
1465 }
1466 if (!c) {
1467 workaround_enabled = 0;
1468 return;
1469 }
1470 workaround_enabled = 1;
1471}
1472
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001473void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001474{
1475 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001476 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001477
Tero Kristoa118b5f2008-12-22 14:27:12 +02001478 if (cpu_is_omap34xx())
1479 min = 1;
1480 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001481 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001482 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001483 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001484
Kevin Hilman0aed04352010-09-22 16:06:27 -07001485 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001486 clk_enable(bank->dbck);
1487
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001488 if (!workaround_enabled)
1489 continue;
1490
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001491 if (!(bank->enabled_non_wakeup_gpios))
1492 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001493
1494 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1495 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001496 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001497 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001498 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001499 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1500 }
1501
1502 if (cpu_is_omap44xx()) {
1503 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301504 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001505 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301506 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001507 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1508 }
1509
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001510 /* Check if any of the non-wakeup interrupt GPIOs have changed
1511 * state. If so, generate an IRQ by software. This is
1512 * horribly racy, but it's the best we can do to work around
1513 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001514 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001515 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001516
1517 /*
1518 * No need to generate IRQs for the rising edge for gpio IRQs
1519 * configured with falling edge only; and vice versa.
1520 */
1521 gen0 = l & bank->saved_fallingdetect;
1522 gen0 &= bank->saved_datain;
1523
1524 gen1 = l & bank->saved_risingdetect;
1525 gen1 &= ~(bank->saved_datain);
1526
1527 /* FIXME: Consider GPIO IRQs with level detections properly! */
1528 gen = l & (~(bank->saved_fallingdetect) &
1529 ~(bank->saved_risingdetect));
1530 /* Consider all GPIO IRQs needed to be updated */
1531 gen |= gen0 | gen1;
1532
1533 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001534 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001535
Sergio Aguirref00d6492010-03-03 16:21:08 +00001536 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001537 old0 = __raw_readl(bank->base +
1538 OMAP24XX_GPIO_LEVELDETECT0);
1539 old1 = __raw_readl(bank->base +
1540 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001541 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001542 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001543 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001544 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001545 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001546 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001547 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001548 OMAP24XX_GPIO_LEVELDETECT1);
1549 }
1550
1551 if (cpu_is_omap44xx()) {
1552 old0 = __raw_readl(bank->base +
1553 OMAP4_GPIO_LEVELDETECT0);
1554 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301555 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001556 __raw_writel(old0 | l, bank->base +
1557 OMAP4_GPIO_LEVELDETECT0);
1558 __raw_writel(old1 | l, bank->base +
1559 OMAP4_GPIO_LEVELDETECT1);
1560 __raw_writel(old0, bank->base +
1561 OMAP4_GPIO_LEVELDETECT0);
1562 __raw_writel(old1, bank->base +
1563 OMAP4_GPIO_LEVELDETECT1);
1564 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001565 }
1566 }
1567
1568}
1569
Tony Lindgren92105bb2005-09-07 17:20:26 +01001570#endif
1571
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001572#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301573/* save the registers of bank 2-6 */
1574void omap_gpio_save_context(void)
1575{
1576 int i;
1577
1578 /* saving banks from 2-6 only since GPIO1 is in WKUP */
1579 for (i = 1; i < gpio_bank_count; i++) {
1580 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301581 gpio_context[i].irqenable1 =
1582 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
1583 gpio_context[i].irqenable2 =
1584 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
1585 gpio_context[i].wake_en =
1586 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
1587 gpio_context[i].ctrl =
1588 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
1589 gpio_context[i].oe =
1590 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
1591 gpio_context[i].leveldetect0 =
1592 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1593 gpio_context[i].leveldetect1 =
1594 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1595 gpio_context[i].risingdetect =
1596 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
1597 gpio_context[i].fallingdetect =
1598 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1599 gpio_context[i].dataout =
1600 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301601 }
1602}
1603
1604/* restore the required registers of bank 2-6 */
1605void omap_gpio_restore_context(void)
1606{
1607 int i;
1608
1609 for (i = 1; i < gpio_bank_count; i++) {
1610 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301611 __raw_writel(gpio_context[i].irqenable1,
1612 bank->base + OMAP24XX_GPIO_IRQENABLE1);
1613 __raw_writel(gpio_context[i].irqenable2,
1614 bank->base + OMAP24XX_GPIO_IRQENABLE2);
1615 __raw_writel(gpio_context[i].wake_en,
1616 bank->base + OMAP24XX_GPIO_WAKE_EN);
1617 __raw_writel(gpio_context[i].ctrl,
1618 bank->base + OMAP24XX_GPIO_CTRL);
1619 __raw_writel(gpio_context[i].oe,
1620 bank->base + OMAP24XX_GPIO_OE);
1621 __raw_writel(gpio_context[i].leveldetect0,
1622 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1623 __raw_writel(gpio_context[i].leveldetect1,
1624 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
1625 __raw_writel(gpio_context[i].risingdetect,
1626 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1627 __raw_writel(gpio_context[i].fallingdetect,
1628 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1629 __raw_writel(gpio_context[i].dataout,
1630 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301631 }
1632}
1633#endif
1634
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001635static struct platform_driver omap_gpio_driver = {
1636 .probe = omap_gpio_probe,
1637 .driver = {
1638 .name = "omap_gpio",
1639 },
1640};
1641
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001642/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001643 * gpio driver register needs to be done before
1644 * machine_init functions access gpio APIs.
1645 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001646 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001647static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001648{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001649 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001650}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001651postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001652
Tony Lindgren92105bb2005-09-07 17:20:26 +01001653static int __init omap_gpio_sysinit(void)
1654{
David Brownell11a78b72006-12-06 17:14:11 -08001655 mpuio_init();
1656
Tony Lindgren140455f2010-02-12 12:26:48 -08001657#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001658 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1659 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001660#endif
1661
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001662 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001663}
1664
Tony Lindgren92105bb2005-09-07 17:20:26 +01001665arch_initcall(omap_gpio_sysinit);