blob: bc2bd698ff2a8d861396db14be1c348d31c7b9cf [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>
Benoit Cousson96751fc2012-02-01 16:01:39 +010022#include <linux/device.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080023#include <linux/pm_runtime.h>
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +053024#include <linux/pm.h>
Benoit Cousson384ebe12011-08-16 11:53:02 +020025#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/irqdomain.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010028
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010030#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010031#include <mach/irqs.h>
Russell King1bc857f2011-07-26 10:54:55 +010032#include <asm/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010033#include <asm/mach/irq.h>
34
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053035#define OFF_MODE 1
36
Charulatha V03e128c2011-05-05 19:58:01 +053037static LIST_HEAD(omap_gpio_list);
38
Charulatha V6d62e212011-04-18 15:06:51 +000039struct gpio_regs {
40 u32 irqenable1;
41 u32 irqenable2;
42 u32 wake_en;
43 u32 ctrl;
44 u32 oe;
45 u32 leveldetect0;
46 u32 leveldetect1;
47 u32 risingdetect;
48 u32 fallingdetect;
49 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053050 u32 debounce;
51 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000052};
53
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010054struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053055 struct list_head node;
Tony Lindgren92105bb2005-09-07 17:20:26 +010056 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010057 u16 irq;
Benoit Cousson384ebe12011-08-16 11:53:02 +020058 int irq_base;
59 struct irq_domain *domain;
Tony Lindgren92105bb2005-09-07 17:20:26 +010060 u32 suspend_wakeup;
61 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080062 u32 non_wakeup_gpios;
63 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000064 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080065 u32 saved_datain;
66 u32 saved_fallingdetect;
67 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080068 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080069 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010070 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080071 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080072 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080073 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080074 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +053075 bool dbck_enabled;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080076 struct device *dev;
Charulatha Vd0d665a2011-08-31 00:02:21 +053077 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080078 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053079 bool loses_context;
Tony Lindgren5de62b82010-12-07 16:26:58 -080080 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070081 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053082 int context_loss_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053083 int power_mode;
84 bool workaround_enabled;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070085
86 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053087 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070088
89 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010090};
91
Kevin Hilman129fd222011-04-22 07:59:07 -070092#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
93#define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
Charulatha Vc8eef652011-05-02 15:21:42 +053094#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010095
96static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
97{
Tony Lindgren92105bb2005-09-07 17:20:26 +010098 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099 u32 l;
100
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700101 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100102 l = __raw_readl(reg);
103 if (is_input)
104 l |= 1 << gpio;
105 else
106 l &= ~(1 << gpio);
107 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530108 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100109}
110
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700111
112/* set data out value using dedicate set/clear register */
113static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100114{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100115 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700116 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100117
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700118 if (enable)
119 reg += bank->regs->set_dataout;
120 else
121 reg += bank->regs->clr_dataout;
122
123 __raw_writel(l, reg);
124}
125
126/* set data out value using mask register */
127static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
128{
129 void __iomem *reg = bank->base + bank->regs->dataout;
130 u32 gpio_bit = GPIO_BIT(bank, gpio);
131 u32 l;
132
133 l = __raw_readl(reg);
134 if (enable)
135 l |= gpio_bit;
136 else
137 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100138 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530139 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100140}
141
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300142static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100143{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700144 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100145
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700146 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100147}
148
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300149static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
150{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700151 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300152
Kevin Hilman129fd222011-04-22 07:59:07 -0700153 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300154}
155
Kevin Hilmanece95282011-07-12 08:18:15 -0700156static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
157{
158 int l = __raw_readl(base + reg);
159
Benoit Cousson862ff642012-02-01 15:58:56 +0100160 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700161 l |= mask;
162 else
163 l &= ~mask;
164
165 __raw_writel(l, base + reg);
166}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100167
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530168static inline void _gpio_dbck_enable(struct gpio_bank *bank)
169{
170 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
171 clk_enable(bank->dbck);
172 bank->dbck_enabled = true;
173 }
174}
175
176static inline void _gpio_dbck_disable(struct gpio_bank *bank)
177{
178 if (bank->dbck_enable_mask && bank->dbck_enabled) {
179 clk_disable(bank->dbck);
180 bank->dbck_enabled = false;
181 }
182}
183
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700184/**
185 * _set_gpio_debounce - low level gpio debounce time
186 * @bank: the gpio bank we're acting upon
187 * @gpio: the gpio number on this @gpio
188 * @debounce: debounce time to use
189 *
190 * OMAP's debounce time is in 31us steps so we need
191 * to convert and round up to the closest unit.
192 */
193static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
194 unsigned debounce)
195{
Kevin Hilman9942da02011-04-22 12:02:05 -0700196 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700197 u32 val;
198 u32 l;
199
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800200 if (!bank->dbck_flag)
201 return;
202
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700203 if (debounce < 32)
204 debounce = 0x01;
205 else if (debounce > 7936)
206 debounce = 0xff;
207 else
208 debounce = (debounce / 0x1f) - 1;
209
Kevin Hilman129fd222011-04-22 07:59:07 -0700210 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700211
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530212 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700213 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700214 __raw_writel(debounce, reg);
215
Kevin Hilman9942da02011-04-22 12:02:05 -0700216 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700217 val = __raw_readl(reg);
218
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530219 if (debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700220 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530221 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700222 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300223 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700224
225 __raw_writel(val, reg);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530226 clk_disable(bank->dbck);
227 /*
228 * Enable debounce clock per module.
229 * This call is mandatory because in omap_gpio_request() when
230 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
231 * runtime callbck fails to turn on dbck because dbck_enable_mask
232 * used within _gpio_dbck_enable() is still not initialized at
233 * that point. Therefore we have to enable dbck here.
234 */
235 _gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530236 if (bank->dbck_enable_mask) {
237 bank->context.debounce = debounce;
238 bank->context.debounce_en = val;
239 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700240}
241
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530242static inline void set_gpio_trigger(struct gpio_bank *bank, int gpio,
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700243 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100244{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800245 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100246 u32 gpio_bit = 1 << gpio;
247
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530248 _gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
249 trigger & IRQ_TYPE_LEVEL_LOW);
250 _gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
251 trigger & IRQ_TYPE_LEVEL_HIGH);
252 _gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
253 trigger & IRQ_TYPE_EDGE_RISING);
254 _gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
255 trigger & IRQ_TYPE_EDGE_FALLING);
256
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530257 bank->context.leveldetect0 =
258 __raw_readl(bank->base + bank->regs->leveldetect0);
259 bank->context.leveldetect1 =
260 __raw_readl(bank->base + bank->regs->leveldetect1);
261 bank->context.risingdetect =
262 __raw_readl(bank->base + bank->regs->risingdetect);
263 bank->context.fallingdetect =
264 __raw_readl(bank->base + bank->regs->fallingdetect);
265
266 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530267 _gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530268 bank->context.wake_en =
269 __raw_readl(bank->base + bank->regs->wkup_en);
270 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530271
Ambresh K55b220c2011-06-15 13:40:45 -0700272 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530273 if (!bank->regs->irqctrl) {
274 /* On omap24xx proceed only when valid GPIO bit is set */
275 if (bank->non_wakeup_gpios) {
276 if (!(bank->non_wakeup_gpios & gpio_bit))
277 goto exit;
278 }
279
Chunqiu Wang699117a62009-06-24 17:13:39 +0000280 /*
281 * Log the edge gpio and manually trigger the IRQ
282 * after resume if the input level changes
283 * to avoid irq lost during PER RET/OFF mode
284 * Applies for omap2 non-wakeup gpio and all omap3 gpios
285 */
286 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800287 bank->enabled_non_wakeup_gpios |= gpio_bit;
288 else
289 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
290 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700291
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530292exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530293 bank->level_mask =
294 __raw_readl(bank->base + bank->regs->leveldetect0) |
295 __raw_readl(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100296}
297
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800298#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800299/*
300 * This only applies to chips that can't do both rising and falling edge
301 * detection at once. For all other chips, this function is a noop.
302 */
303static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
304{
305 void __iomem *reg = bank->base;
306 u32 l = 0;
307
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530308 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800309 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530310
311 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800312
313 l = __raw_readl(reg);
314 if ((l >> gpio) & 1)
315 l &= ~(1 << gpio);
316 else
317 l |= 1 << gpio;
318
319 __raw_writel(l, reg);
320}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530321#else
322static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800323#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800324
Tony Lindgren92105bb2005-09-07 17:20:26 +0100325static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
326{
327 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530328 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100329 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100330
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530331 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
332 set_gpio_trigger(bank, gpio, trigger);
333 } else if (bank->regs->irqctrl) {
334 reg += bank->regs->irqctrl;
335
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100336 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000337 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800338 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100339 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100341 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100342 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100343 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530344 return -EINVAL;
345
346 __raw_writel(l, reg);
347 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530349 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100350 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530351 reg += bank->regs->edgectrl1;
352
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100353 gpio &= 0x07;
354 l = __raw_readl(reg);
355 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100356 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100357 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100358 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100359 l |= 1 << (gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530360
361 /* Enable wake-up during idle for dynamic tick */
362 _gpio_rmw(base, bank->regs->wkup_en, 1 << gpio, trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530363 bank->context.wake_en =
364 __raw_readl(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530365 __raw_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100366 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100367 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100368}
369
Lennert Buytenheke9191022010-11-29 11:17:17 +0100370static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100371{
372 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100373 unsigned gpio;
374 int retval;
David Brownella6472532008-03-03 04:33:30 -0800375 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100376
Lennert Buytenheke9191022010-11-29 11:17:17 +0100377 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
378 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100379 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100380 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100381
David Brownelle5c56ed2006-12-06 17:13:59 -0800382 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100383 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800384
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530385 bank = irq_data_get_irq_chip_data(d);
386
387 if (!bank->regs->leveldetect0 &&
388 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100389 return -EINVAL;
390
David Brownella6472532008-03-03 04:33:30 -0800391 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700392 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800393 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800394
395 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100396 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800397 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100398 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800399
Tony Lindgren92105bb2005-09-07 17:20:26 +0100400 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100401}
402
403static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
404{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100405 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100406
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700407 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100408 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300409
410 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700411 if (bank->regs->irqstatus2) {
412 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700413 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700414 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700415
416 /* Flush posted write for the irq status to avoid spurious interrupts */
417 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100418}
419
420static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
421{
Kevin Hilman129fd222011-04-22 07:59:07 -0700422 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100423}
424
Imre Deakea6dedd2006-06-26 16:16:00 -0700425static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
426{
427 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700428 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700429 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700430
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700431 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700432 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700433 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700434 l = ~l;
435 l &= mask;
436 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700437}
438
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700439static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100440{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100441 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100442 u32 l;
443
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700444 if (bank->regs->set_irqenable) {
445 reg += bank->regs->set_irqenable;
446 l = gpio_mask;
447 } else {
448 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100449 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700450 if (bank->regs->irqenable_inv)
451 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100452 else
453 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100454 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700455
456 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530457 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700458}
459
460static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
461{
462 void __iomem *reg = bank->base;
463 u32 l;
464
465 if (bank->regs->clr_irqenable) {
466 reg += bank->regs->clr_irqenable;
467 l = gpio_mask;
468 } else {
469 reg += bank->regs->irqenable;
470 l = __raw_readl(reg);
471 if (bank->regs->irqenable_inv)
472 l |= gpio_mask;
473 else
474 l &= ~gpio_mask;
475 }
476
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100477 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530478 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100479}
480
481static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
482{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700483 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100484}
485
Tony Lindgren92105bb2005-09-07 17:20:26 +0100486/*
487 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
488 * 1510 does not seem to have a wake-up register. If JTAG is connected
489 * to the target, system will wake up always on GPIO events. While
490 * system is running all registered GPIO interrupts need to have wake-up
491 * enabled. When system is suspended, only selected GPIO interrupts need
492 * to have wake-up enabled.
493 */
494static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
495{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700496 u32 gpio_bit = GPIO_BIT(bank, gpio);
497 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800498
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700499 if (bank->non_wakeup_gpios & gpio_bit) {
Benoit Cousson862ff642012-02-01 15:58:56 +0100500 dev_err(bank->dev,
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700501 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100502 return -EINVAL;
503 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700504
505 spin_lock_irqsave(&bank->lock, flags);
506 if (enable)
507 bank->suspend_wakeup |= gpio_bit;
508 else
509 bank->suspend_wakeup &= ~gpio_bit;
510
511 spin_unlock_irqrestore(&bank->lock, flags);
512
513 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100514}
515
Tony Lindgren4196dd62006-09-25 12:41:38 +0300516static void _reset_gpio(struct gpio_bank *bank, int gpio)
517{
Kevin Hilman129fd222011-04-22 07:59:07 -0700518 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300519 _set_gpio_irqenable(bank, gpio, 0);
520 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700521 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300522}
523
Tony Lindgren92105bb2005-09-07 17:20:26 +0100524/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100525static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100526{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100527 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100528 struct gpio_bank *bank;
529 int retval;
530
Lennert Buytenheke9191022010-11-29 11:17:17 +0100531 bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700532 retval = _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100533
534 return retval;
535}
536
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800537static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100538{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800539 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800540 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100541
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530542 /*
543 * If this is the first gpio_request for the bank,
544 * enable the bank module.
545 */
546 if (!bank->mod_usage)
547 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100548
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530549 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300550 /* Set trigger to none. You need to enable the desired trigger with
551 * request_irq() or set_irq_type().
552 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800553 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100554
Charulatha Vfad96ea2011-05-25 11:23:50 +0530555 if (bank->regs->pinctrl) {
556 void __iomem *reg = bank->base + bank->regs->pinctrl;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100557
Tony Lindgren92105bb2005-09-07 17:20:26 +0100558 /* Claim the pin for MPU */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800559 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100560 }
Charulatha Vfad96ea2011-05-25 11:23:50 +0530561
Charulatha Vc8eef652011-05-02 15:21:42 +0530562 if (bank->regs->ctrl && !bank->mod_usage) {
563 void __iomem *reg = bank->base + bank->regs->ctrl;
564 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700565
Charulatha Vc8eef652011-05-02 15:21:42 +0530566 ctrl = __raw_readl(reg);
567 /* Module is enabled, clocks are not gated */
568 ctrl &= ~GPIO_MOD_CTRL_BIT;
569 __raw_writel(ctrl, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530570 bank->context.ctrl = ctrl;
Charulatha V058af1e2009-11-22 10:11:25 -0800571 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530572
573 bank->mod_usage |= 1 << offset;
574
David Brownella6472532008-03-03 04:33:30 -0800575 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576
577 return 0;
578}
579
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800580static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100581{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800582 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530583 void __iomem *base = bank->base;
David Brownella6472532008-03-03 04:33:30 -0800584 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100585
David Brownella6472532008-03-03 04:33:30 -0800586 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530587
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530588 if (bank->regs->wkup_en) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100589 /* Disable wake-up during idle for dynamic tick */
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530590 _gpio_rmw(base, bank->regs->wkup_en, 1 << offset, 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530591 bank->context.wake_en =
592 __raw_readl(bank->base + bank->regs->wkup_en);
593 }
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530594
Charulatha Vc8eef652011-05-02 15:21:42 +0530595 bank->mod_usage &= ~(1 << offset);
Charulatha V9f096862010-05-14 12:05:27 -0700596
Charulatha Vc8eef652011-05-02 15:21:42 +0530597 if (bank->regs->ctrl && !bank->mod_usage) {
598 void __iomem *reg = bank->base + bank->regs->ctrl;
599 u32 ctrl;
600
601 ctrl = __raw_readl(reg);
602 /* Module is disabled, clocks are gated */
603 ctrl |= GPIO_MOD_CTRL_BIT;
604 __raw_writel(ctrl, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530605 bank->context.ctrl = ctrl;
Charulatha V058af1e2009-11-22 10:11:25 -0800606 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530607
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800608 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800609 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530610
611 /*
612 * If this is the last gpio to be freed in the bank,
613 * disable the bank module.
614 */
615 if (!bank->mod_usage)
616 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100617}
618
619/*
620 * We need to unmask the GPIO bank interrupt as soon as possible to
621 * avoid missing GPIO interrupts for other lines in the bank.
622 * Then we need to mask-read-clear-unmask the triggered GPIO lines
623 * in the bank to avoid missing nested interrupts for a GPIO line.
624 * If we wait to unmask individual GPIO lines in the bank after the
625 * line's interrupt handler has been run, we may miss some nested
626 * interrupts.
627 */
Russell King10dd5ce2006-11-23 11:41:32 +0000628static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100629{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100630 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100631 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800632 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100633 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700634 u32 retrigger = 0;
635 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000636 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100637
Will Deaconee144182011-02-21 13:46:08 +0000638 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100639
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100640 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700641 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530642 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800643
644 if (WARN_ON(!isr_reg))
645 goto exit;
646
Tony Lindgren92105bb2005-09-07 17:20:26 +0100647 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100648 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700649 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100650
Imre Deakea6dedd2006-06-26 16:16:00 -0700651 enabled = _get_gpio_irqbank_mask(bank);
652 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100653
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530654 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800655 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100656
657 /* clear edge sensitive interrupts before handler(s) are
658 called so that we don't miss any interrupt occurred while
659 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700660 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100661 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700662 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100663
664 /* if there is only edge sensitive GPIO pin interrupts
665 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700666 if (!level_mask && !unmasked) {
667 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000668 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700669 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670
Imre Deakea6dedd2006-06-26 16:16:00 -0700671 isr |= retrigger;
672 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100673 if (!isr)
674 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675
Benoit Cousson384ebe12011-08-16 11:53:02 +0200676 gpio_irq = bank->irq_base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100677 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700678 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800679
Tony Lindgren92105bb2005-09-07 17:20:26 +0100680 if (!(isr & 1))
681 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200682
Cory Maccarrone4318f362010-01-08 10:29:04 -0800683 /*
684 * Some chips can't respond to both rising and falling
685 * at the same time. If this irq was requested with
686 * both flags, we need to flip the ICR data for the IRQ
687 * to respond to the IRQ for the opposite direction.
688 * This will be indicated in the bank toggle_mask.
689 */
690 if (bank->toggle_mask & (1 << gpio_index))
691 _toggle_gpio_edge_triggering(bank, gpio_index);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800692
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100693 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100694 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000695 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700696 /* if bank has any level sensitive GPIO pin interrupt
697 configured, we must unmask the bank interrupt only after
698 handler(s) are executed in order to avoid spurious bank
699 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800700exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700701 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000702 chained_irq_exit(chip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530703 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100704}
705
Lennert Buytenheke9191022010-11-29 11:17:17 +0100706static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300707{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100708 unsigned int gpio = d->irq - IH_GPIO_BASE;
709 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700710 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300711
Colin Cross85ec7b92011-06-06 13:38:18 -0700712 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300713 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700714 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300715}
716
Lennert Buytenheke9191022010-11-29 11:17:17 +0100717static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100719 unsigned int gpio = d->irq - IH_GPIO_BASE;
720 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100721
722 _clear_gpio_irqstatus(bank, gpio);
723}
724
Lennert Buytenheke9191022010-11-29 11:17:17 +0100725static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100727 unsigned int gpio = d->irq - IH_GPIO_BASE;
728 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700729 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730
Colin Cross85ec7b92011-06-06 13:38:18 -0700731 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700733 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700734 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100735}
736
Lennert Buytenheke9191022010-11-29 11:17:17 +0100737static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100738{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100739 unsigned int gpio = d->irq - IH_GPIO_BASE;
740 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700741 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100742 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700743 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700744
Colin Cross85ec7b92011-06-06 13:38:18 -0700745 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700746 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700747 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800748
749 /* For level-triggered GPIOs, the clearing must be done after
750 * the HW source is cleared, thus after the handler has run */
751 if (bank->level_mask & irq_mask) {
752 _set_gpio_irqenable(bank, gpio, 0);
753 _clear_gpio_irqstatus(bank, gpio);
754 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100755
Kevin Hilman4de8c752008-01-16 21:56:14 -0800756 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700757 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100758}
759
David Brownelle5c56ed2006-12-06 17:13:59 -0800760static struct irq_chip gpio_irq_chip = {
761 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100762 .irq_shutdown = gpio_irq_shutdown,
763 .irq_ack = gpio_ack_irq,
764 .irq_mask = gpio_mask_irq,
765 .irq_unmask = gpio_unmask_irq,
766 .irq_set_type = gpio_irq_type,
767 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800768};
769
770/*---------------------------------------------------------------------*/
771
Magnus Damm79ee0312009-07-08 13:22:04 +0200772static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800773{
Magnus Damm79ee0312009-07-08 13:22:04 +0200774 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800775 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800776 void __iomem *mask_reg = bank->base +
777 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800778 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800779
David Brownella6472532008-03-03 04:33:30 -0800780 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800781 bank->saved_wakeup = __raw_readl(mask_reg);
782 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800783 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800784
785 return 0;
786}
787
Magnus Damm79ee0312009-07-08 13:22:04 +0200788static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800789{
Magnus Damm79ee0312009-07-08 13:22:04 +0200790 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800791 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800792 void __iomem *mask_reg = bank->base +
793 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800794 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800795
David Brownella6472532008-03-03 04:33:30 -0800796 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800797 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800798 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800799
800 return 0;
801}
802
Alexey Dobriyan47145212009-12-14 18:00:08 -0800803static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200804 .suspend_noirq = omap_mpuio_suspend_noirq,
805 .resume_noirq = omap_mpuio_resume_noirq,
806};
807
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200808/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800809static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800810 .driver = {
811 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200812 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800813 },
814};
815
816static struct platform_device omap_mpuio_device = {
817 .name = "mpuio",
818 .id = -1,
819 .dev = {
820 .driver = &omap_mpuio_driver.driver,
821 }
822 /* could list the /proc/iomem resources */
823};
824
Charulatha V03e128c2011-05-05 19:58:01 +0530825static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800826{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800827 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700828
David Brownell11a78b72006-12-06 17:14:11 -0800829 if (platform_driver_register(&omap_mpuio_driver) == 0)
830 (void) platform_device_register(&omap_mpuio_device);
831}
832
David Brownelle5c56ed2006-12-06 17:13:59 -0800833/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100834
David Brownell52e31342008-03-03 12:43:23 -0800835static int gpio_input(struct gpio_chip *chip, unsigned offset)
836{
837 struct gpio_bank *bank;
838 unsigned long flags;
839
840 bank = container_of(chip, struct gpio_bank, chip);
841 spin_lock_irqsave(&bank->lock, flags);
842 _set_gpio_direction(bank, offset, 1);
843 spin_unlock_irqrestore(&bank->lock, flags);
844 return 0;
845}
846
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300847static int gpio_is_input(struct gpio_bank *bank, int mask)
848{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700849 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300850
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300851 return __raw_readl(reg) & mask;
852}
853
David Brownell52e31342008-03-03 12:43:23 -0800854static int gpio_get(struct gpio_chip *chip, unsigned offset)
855{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300856 struct gpio_bank *bank;
857 void __iomem *reg;
858 int gpio;
859 u32 mask;
860
861 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530862 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300863 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700864 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300865
866 if (gpio_is_input(bank, mask))
867 return _get_gpio_datain(bank, gpio);
868 else
869 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800870}
871
872static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
873{
874 struct gpio_bank *bank;
875 unsigned long flags;
876
877 bank = container_of(chip, struct gpio_bank, chip);
878 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700879 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800880 _set_gpio_direction(bank, offset, 0);
881 spin_unlock_irqrestore(&bank->lock, flags);
882 return 0;
883}
884
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700885static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
886 unsigned debounce)
887{
888 struct gpio_bank *bank;
889 unsigned long flags;
890
891 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800892
893 if (!bank->dbck) {
894 bank->dbck = clk_get(bank->dev, "dbclk");
895 if (IS_ERR(bank->dbck))
896 dev_err(bank->dev, "Could not get gpio dbck\n");
897 }
898
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700899 spin_lock_irqsave(&bank->lock, flags);
900 _set_gpio_debounce(bank, offset, debounce);
901 spin_unlock_irqrestore(&bank->lock, flags);
902
903 return 0;
904}
905
David Brownell52e31342008-03-03 12:43:23 -0800906static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
907{
908 struct gpio_bank *bank;
909 unsigned long flags;
910
911 bank = container_of(chip, struct gpio_bank, chip);
912 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700913 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800914 spin_unlock_irqrestore(&bank->lock, flags);
915}
916
David Brownella007b702008-12-10 17:35:25 -0800917static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
918{
919 struct gpio_bank *bank;
920
921 bank = container_of(chip, struct gpio_bank, chip);
Benoit Cousson384ebe12011-08-16 11:53:02 +0200922 return bank->irq_base + offset;
David Brownella007b702008-12-10 17:35:25 -0800923}
924
David Brownell52e31342008-03-03 12:43:23 -0800925/*---------------------------------------------------------------------*/
926
Tony Lindgren9a748052010-12-07 16:26:56 -0800927static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700928{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700929 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700930 u32 rev;
931
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700932 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700933 return;
934
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700935 rev = __raw_readw(bank->base + bank->regs->revision);
936 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700937 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700938
939 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700940}
941
David Brownell8ba55c52008-02-26 11:10:50 -0800942/* This lock class tells lockdep that GPIO irqs are in a different
943 * category than their parents, so it won't report false recursion.
944 */
945static struct lock_class_key gpio_lock_class;
946
Charulatha V03e128c2011-05-05 19:58:01 +0530947static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800948{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530949 void __iomem *base = bank->base;
950 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800951
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530952 if (bank->width == 16)
953 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800954
Charulatha Vd0d665a2011-08-31 00:02:21 +0530955 if (bank->is_mpuio) {
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530956 __raw_writel(l, bank->base + bank->regs->irqenable);
957 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800958 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530959
960 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->irqenable_inv);
961 _gpio_rmw(base, bank->regs->irqstatus, l,
962 bank->regs->irqenable_inv == false);
963 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->debounce_en != 0);
964 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->ctrl != 0);
965 if (bank->regs->debounce_en)
966 _gpio_rmw(base, bank->regs->debounce_en, 0, 1);
967
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +0530968 /* Save OE default value (0xffffffff) in the context */
969 bank->context.oe = __raw_readl(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530970 /* Initialize interface clk ungated, module enabled */
971 if (bank->regs->ctrl)
972 _gpio_rmw(base, bank->regs->ctrl, 0, 1);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800973}
974
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700975static __init void
976omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
977 unsigned int num)
978{
979 struct irq_chip_generic *gc;
980 struct irq_chip_type *ct;
981
982 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
983 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -0700984 if (!gc) {
985 dev_err(bank->dev, "Memory alloc failed for gc\n");
986 return;
987 }
988
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700989 ct = gc->chip_types;
990
991 /* NOTE: No ack required, reading IRQ status clears it. */
992 ct->chip.irq_mask = irq_gc_mask_set_bit;
993 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
994 ct->chip.irq_set_type = gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530995
996 if (bank->regs->wkup_en)
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700997 ct->chip.irq_set_wake = gpio_wake_enable,
998
999 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1000 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1001 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1002}
1003
Russell Kingd52b31d2011-05-27 13:56:12 -07001004static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001005{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001006 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001007 static int gpio;
1008
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001009 /*
1010 * REVISIT eventually switch from OMAP-specific gpio structs
1011 * over to the generic ones
1012 */
1013 bank->chip.request = omap_gpio_request;
1014 bank->chip.free = omap_gpio_free;
1015 bank->chip.direction_input = gpio_input;
1016 bank->chip.get = gpio_get;
1017 bank->chip.direction_output = gpio_output;
1018 bank->chip.set_debounce = gpio_debounce;
1019 bank->chip.set = gpio_set;
1020 bank->chip.to_irq = gpio_2irq;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301021 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001022 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301023 if (bank->regs->wkup_en)
1024 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001025 bank->chip.base = OMAP_MPUIO(0);
1026 } else {
1027 bank->chip.label = "gpio";
1028 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001029 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001030 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001031 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001032
1033 gpiochip_add(&bank->chip);
1034
Benoit Cousson384ebe12011-08-16 11:53:02 +02001035 for (j = bank->irq_base; j < bank->irq_base + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001036 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001037 irq_set_chip_data(j, bank);
Charulatha Vd0d665a2011-08-31 00:02:21 +05301038 if (bank->is_mpuio) {
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001039 omap_mpuio_alloc_gc(bank, j, bank->width);
1040 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001041 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001042 irq_set_handler(j, handle_simple_irq);
1043 set_irq_flags(j, IRQF_VALID);
1044 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001045 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001046 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1047 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001048}
1049
Benoit Cousson384ebe12011-08-16 11:53:02 +02001050static const struct of_device_id omap_gpio_match[];
1051
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001052static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001053{
Benoit Cousson862ff642012-02-01 15:58:56 +01001054 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001055 struct device_node *node = dev->of_node;
1056 const struct of_device_id *match;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001057 struct omap_gpio_platform_data *pdata;
1058 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001059 struct gpio_bank *bank;
Charulatha V03e128c2011-05-05 19:58:01 +05301060 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001061
Benoit Cousson384ebe12011-08-16 11:53:02 +02001062 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1063
1064 pdata = match ? match->data : dev->platform_data;
1065 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001066 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001067
Benoit Cousson96751fc2012-02-01 16:01:39 +01001068 bank = devm_kzalloc(&pdev->dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301069 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001070 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001071 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301072 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001073
1074 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1075 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001076 dev_err(dev, "Invalid IRQ resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001077 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001078 }
1079
1080 bank->irq = res->start;
Benoit Cousson862ff642012-02-01 15:58:56 +01001081 bank->dev = dev;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001082 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001083 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001084 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301085 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301086 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Charulatha V0cde8d02011-05-05 20:15:16 +05301087 bank->loses_context = pdata->loses_context;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301088 bank->get_context_loss_count = pdata->get_context_loss_count;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001089 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001090#ifdef CONFIG_OF_GPIO
1091 bank->chip.of_node = of_node_get(node);
1092#endif
1093
1094 bank->irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
1095 if (bank->irq_base < 0) {
1096 dev_err(dev, "Couldn't allocate IRQ numbers\n");
1097 return -ENODEV;
1098 }
1099
1100 bank->domain = irq_domain_add_legacy(node, bank->width, bank->irq_base,
1101 0, &irq_domain_simple_ops, NULL);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001102
1103 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1104 bank->set_dataout = _set_gpio_dataout_reg;
1105 else
1106 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001107
1108 spin_lock_init(&bank->lock);
1109
1110 /* Static mapping, never released */
1111 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1112 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001113 dev_err(dev, "Invalid mem resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001114 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001115 }
1116
Benoit Cousson96751fc2012-02-01 16:01:39 +01001117 if (!devm_request_mem_region(dev, res->start, resource_size(res),
1118 pdev->name)) {
1119 dev_err(dev, "Region already claimed\n");
1120 return -EBUSY;
1121 }
1122
1123 bank->base = devm_ioremap(dev, res->start, resource_size(res));
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001124 if (!bank->base) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001125 dev_err(dev, "Could not ioremap\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001126 return -ENOMEM;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001127 }
1128
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301129 platform_set_drvdata(pdev, bank);
1130
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001131 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301132 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001133 pm_runtime_get_sync(bank->dev);
1134
Charulatha Vd0d665a2011-08-31 00:02:21 +05301135 if (bank->is_mpuio)
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301136 mpuio_init(bank);
1137
Charulatha V03e128c2011-05-05 19:58:01 +05301138 omap_gpio_mod_init(bank);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001139 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001140 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001141
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301142 pm_runtime_put(bank->dev);
1143
Charulatha V03e128c2011-05-05 19:58:01 +05301144 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001145
Charulatha V03e128c2011-05-05 19:58:01 +05301146 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001147}
1148
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301149#ifdef CONFIG_ARCH_OMAP2PLUS
1150
1151#if defined(CONFIG_PM_SLEEP)
1152static int omap_gpio_suspend(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001153{
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301154 struct platform_device *pdev = to_platform_device(dev);
1155 struct gpio_bank *bank = platform_get_drvdata(pdev);
1156 void __iomem *base = bank->base;
1157 void __iomem *wakeup_enable;
1158 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001159
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301160 if (!bank->mod_usage || !bank->loses_context)
1161 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001162
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301163 if (!bank->regs->wkup_en || !bank->suspend_wakeup)
1164 return 0;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301165
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301166 wakeup_enable = bank->base + bank->regs->wkup_en;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001167
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301168 spin_lock_irqsave(&bank->lock, flags);
1169 bank->saved_wakeup = __raw_readl(wakeup_enable);
1170 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1171 _gpio_rmw(base, bank->regs->wkup_en, bank->suspend_wakeup, 1);
1172 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001173
1174 return 0;
1175}
1176
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301177static int omap_gpio_resume(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001178{
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301179 struct platform_device *pdev = to_platform_device(dev);
1180 struct gpio_bank *bank = platform_get_drvdata(pdev);
1181 void __iomem *base = bank->base;
1182 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001183
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301184 if (!bank->mod_usage || !bank->loses_context)
1185 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001186
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301187 if (!bank->regs->wkup_en || !bank->saved_wakeup)
1188 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001189
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301190 spin_lock_irqsave(&bank->lock, flags);
1191 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1192 _gpio_rmw(base, bank->regs->wkup_en, bank->saved_wakeup, 1);
1193 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301194
1195 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001196}
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301197#endif /* CONFIG_PM_SLEEP */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001198
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301199#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301200static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001201
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301202static int omap_gpio_runtime_suspend(struct device *dev)
1203{
1204 struct platform_device *pdev = to_platform_device(dev);
1205 struct gpio_bank *bank = platform_get_drvdata(pdev);
1206 u32 l1 = 0, l2 = 0;
1207 unsigned long flags;
1208
1209 spin_lock_irqsave(&bank->lock, flags);
1210 if (bank->power_mode != OFF_MODE) {
1211 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301212 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301213 }
1214 /*
1215 * If going to OFF, remove triggering for all
1216 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1217 * generated. See OMAP2420 Errata item 1.101.
1218 */
1219 if (!(bank->enabled_non_wakeup_gpios))
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301220 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301221
1222 bank->saved_datain = __raw_readl(bank->base +
1223 bank->regs->datain);
1224 l1 = __raw_readl(bank->base + bank->regs->fallingdetect);
1225 l2 = __raw_readl(bank->base + bank->regs->risingdetect);
1226
1227 bank->saved_fallingdetect = l1;
1228 bank->saved_risingdetect = l2;
1229 l1 &= ~bank->enabled_non_wakeup_gpios;
1230 l2 &= ~bank->enabled_non_wakeup_gpios;
1231
1232 __raw_writel(l1, bank->base + bank->regs->fallingdetect);
1233 __raw_writel(l2, bank->base + bank->regs->risingdetect);
1234
1235 bank->workaround_enabled = true;
1236
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301237update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301238 if (bank->get_context_loss_count)
1239 bank->context_loss_count =
1240 bank->get_context_loss_count(bank->dev);
1241
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +05301242 _gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301243 spin_unlock_irqrestore(&bank->lock, flags);
1244
1245 return 0;
1246}
1247
1248static int omap_gpio_runtime_resume(struct device *dev)
1249{
1250 struct platform_device *pdev = to_platform_device(dev);
1251 struct gpio_bank *bank = platform_get_drvdata(pdev);
1252 int context_lost_cnt_after;
1253 u32 l = 0, gen, gen0, gen1;
1254 unsigned long flags;
1255
1256 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +05301257 _gpio_dbck_enable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301258 if (!bank->enabled_non_wakeup_gpios || !bank->workaround_enabled) {
1259 spin_unlock_irqrestore(&bank->lock, flags);
1260 return 0;
1261 }
1262
1263 if (bank->get_context_loss_count) {
1264 context_lost_cnt_after =
1265 bank->get_context_loss_count(bank->dev);
1266 if (context_lost_cnt_after != bank->context_loss_count ||
1267 !context_lost_cnt_after) {
1268 omap_gpio_restore_context(bank);
1269 } else {
1270 spin_unlock_irqrestore(&bank->lock, flags);
1271 return 0;
1272 }
1273 }
1274
1275 __raw_writel(bank->saved_fallingdetect,
1276 bank->base + bank->regs->fallingdetect);
1277 __raw_writel(bank->saved_risingdetect,
1278 bank->base + bank->regs->risingdetect);
1279 l = __raw_readl(bank->base + bank->regs->datain);
1280
1281 /*
1282 * Check if any of the non-wakeup interrupt GPIOs have changed
1283 * state. If so, generate an IRQ by software. This is
1284 * horribly racy, but it's the best we can do to work around
1285 * this silicon bug.
1286 */
1287 l ^= bank->saved_datain;
1288 l &= bank->enabled_non_wakeup_gpios;
1289
1290 /*
1291 * No need to generate IRQs for the rising edge for gpio IRQs
1292 * configured with falling edge only; and vice versa.
1293 */
1294 gen0 = l & bank->saved_fallingdetect;
1295 gen0 &= bank->saved_datain;
1296
1297 gen1 = l & bank->saved_risingdetect;
1298 gen1 &= ~(bank->saved_datain);
1299
1300 /* FIXME: Consider GPIO IRQs with level detections properly! */
1301 gen = l & (~(bank->saved_fallingdetect) & ~(bank->saved_risingdetect));
1302 /* Consider all GPIO IRQs needed to be updated */
1303 gen |= gen0 | gen1;
1304
1305 if (gen) {
1306 u32 old0, old1;
1307
1308 old0 = __raw_readl(bank->base + bank->regs->leveldetect0);
1309 old1 = __raw_readl(bank->base + bank->regs->leveldetect1);
1310
1311 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1312 __raw_writel(old0 | gen, bank->base +
1313 bank->regs->leveldetect0);
1314 __raw_writel(old1 | gen, bank->base +
1315 bank->regs->leveldetect1);
1316 }
1317
1318 if (cpu_is_omap44xx()) {
1319 __raw_writel(old0 | l, bank->base +
1320 bank->regs->leveldetect0);
1321 __raw_writel(old1 | l, bank->base +
1322 bank->regs->leveldetect1);
1323 }
1324 __raw_writel(old0, bank->base + bank->regs->leveldetect0);
1325 __raw_writel(old1, bank->base + bank->regs->leveldetect1);
1326 }
1327
1328 bank->workaround_enabled = false;
1329 spin_unlock_irqrestore(&bank->lock, flags);
1330
1331 return 0;
1332}
1333#endif /* CONFIG_PM_RUNTIME */
1334
1335void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001336{
Charulatha V03e128c2011-05-05 19:58:01 +05301337 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001338
Charulatha V03e128c2011-05-05 19:58:01 +05301339 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301340 if (!bank->mod_usage || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301341 continue;
1342
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301343 bank->power_mode = pwr_mode;
1344
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301345 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001346 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001347}
1348
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001349void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001350{
Charulatha V03e128c2011-05-05 19:58:01 +05301351 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001352
Charulatha V03e128c2011-05-05 19:58:01 +05301353 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301354 if (!bank->mod_usage || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301355 continue;
1356
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301357 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001358 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001359}
1360
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301361#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301362static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301363{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301364 __raw_writel(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301365 bank->base + bank->regs->wkup_en);
1366 __raw_writel(bank->context.ctrl, bank->base + bank->regs->ctrl);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301367 __raw_writel(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301368 bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301369 __raw_writel(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301370 bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301371 __raw_writel(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301372 bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301373 __raw_writel(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301374 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301375 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1376 __raw_writel(bank->context.dataout,
1377 bank->base + bank->regs->set_dataout);
1378 else
1379 __raw_writel(bank->context.dataout,
1380 bank->base + bank->regs->dataout);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301381 __raw_writel(bank->context.oe, bank->base + bank->regs->direction);
1382
Nishanth Menonae547352011-09-09 19:08:58 +05301383 if (bank->dbck_enable_mask) {
1384 __raw_writel(bank->context.debounce, bank->base +
1385 bank->regs->debounce);
1386 __raw_writel(bank->context.debounce_en,
1387 bank->base + bank->regs->debounce_en);
1388 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301389
1390 __raw_writel(bank->context.irqenable1,
1391 bank->base + bank->regs->irqenable);
1392 __raw_writel(bank->context.irqenable2,
1393 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301394}
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301395#endif /* CONFIG_PM_RUNTIME */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301396#else
1397#define omap_gpio_suspend NULL
1398#define omap_gpio_resume NULL
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301399#define omap_gpio_runtime_suspend NULL
1400#define omap_gpio_runtime_resume NULL
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301401#endif
1402
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301403static const struct dev_pm_ops gpio_pm_ops = {
1404 SET_SYSTEM_SLEEP_PM_OPS(omap_gpio_suspend, omap_gpio_resume)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301405 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1406 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301407};
1408
Benoit Cousson384ebe12011-08-16 11:53:02 +02001409#if defined(CONFIG_OF)
1410static struct omap_gpio_reg_offs omap2_gpio_regs = {
1411 .revision = OMAP24XX_GPIO_REVISION,
1412 .direction = OMAP24XX_GPIO_OE,
1413 .datain = OMAP24XX_GPIO_DATAIN,
1414 .dataout = OMAP24XX_GPIO_DATAOUT,
1415 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1416 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1417 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1418 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1419 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1420 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1421 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1422 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1423 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1424 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1425 .ctrl = OMAP24XX_GPIO_CTRL,
1426 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1427 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1428 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1429 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1430 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1431};
1432
1433static struct omap_gpio_reg_offs omap4_gpio_regs = {
1434 .revision = OMAP4_GPIO_REVISION,
1435 .direction = OMAP4_GPIO_OE,
1436 .datain = OMAP4_GPIO_DATAIN,
1437 .dataout = OMAP4_GPIO_DATAOUT,
1438 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1439 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1440 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1441 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1442 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1443 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1444 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1445 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1446 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1447 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1448 .ctrl = OMAP4_GPIO_CTRL,
1449 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1450 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1451 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1452 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1453 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1454};
1455
1456static struct omap_gpio_platform_data omap2_pdata = {
1457 .regs = &omap2_gpio_regs,
1458 .bank_width = 32,
1459 .dbck_flag = false,
1460};
1461
1462static struct omap_gpio_platform_data omap3_pdata = {
1463 .regs = &omap2_gpio_regs,
1464 .bank_width = 32,
1465 .dbck_flag = true,
1466};
1467
1468static struct omap_gpio_platform_data omap4_pdata = {
1469 .regs = &omap4_gpio_regs,
1470 .bank_width = 32,
1471 .dbck_flag = true,
1472};
1473
1474static const struct of_device_id omap_gpio_match[] = {
1475 {
1476 .compatible = "ti,omap4-gpio",
1477 .data = &omap4_pdata,
1478 },
1479 {
1480 .compatible = "ti,omap3-gpio",
1481 .data = &omap3_pdata,
1482 },
1483 {
1484 .compatible = "ti,omap2-gpio",
1485 .data = &omap2_pdata,
1486 },
1487 { },
1488};
1489MODULE_DEVICE_TABLE(of, omap_gpio_match);
1490#endif
1491
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001492static struct platform_driver omap_gpio_driver = {
1493 .probe = omap_gpio_probe,
1494 .driver = {
1495 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301496 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001497 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001498 },
1499};
1500
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001501/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001502 * gpio driver register needs to be done before
1503 * machine_init functions access gpio APIs.
1504 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001505 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001506static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001507{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001508 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001509}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001510postcore_initcall(omap_gpio_drv_reg);