blob: 6c17e581231220817673139fa4ebf1cf8a90df59 [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 DebBarma72f83af2011-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
Benoit Cousson25db7112012-02-23 21:50:10 +010096static int irq_to_gpio(struct gpio_bank *bank, unsigned int gpio_irq)
97{
98 return gpio_irq - bank->irq_base + bank->chip.base;
99}
100
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100101static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
102{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100103 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100104 u32 l;
105
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700106 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100107 l = __raw_readl(reg);
108 if (is_input)
109 l |= 1 << gpio;
110 else
111 l &= ~(1 << gpio);
112 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530113 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100114}
115
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700116
117/* set data out value using dedicate set/clear register */
118static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100119{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100120 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700121 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100122
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530123 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700124 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530125 bank->context.dataout |= l;
126 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700127 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530128 bank->context.dataout &= ~l;
129 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700130
131 __raw_writel(l, reg);
132}
133
134/* set data out value using mask register */
135static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
136{
137 void __iomem *reg = bank->base + bank->regs->dataout;
138 u32 gpio_bit = GPIO_BIT(bank, gpio);
139 u32 l;
140
141 l = __raw_readl(reg);
142 if (enable)
143 l |= gpio_bit;
144 else
145 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100146 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530147 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100148}
149
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300150static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100151{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700152 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700154 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100155}
156
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300157static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
158{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700159 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300160
Kevin Hilman129fd222011-04-22 07:59:07 -0700161 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300162}
163
Kevin Hilmanece95282011-07-12 08:18:15 -0700164static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
165{
166 int l = __raw_readl(base + reg);
167
Benoit Cousson862ff642012-02-01 15:58:56 +0100168 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700169 l |= mask;
170 else
171 l &= ~mask;
172
173 __raw_writel(l, base + reg);
174}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100175
Tarun Kanti DebBarma72f83af2011-11-24 03:03:28 +0530176static inline void _gpio_dbck_enable(struct gpio_bank *bank)
177{
178 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
179 clk_enable(bank->dbck);
180 bank->dbck_enabled = true;
181 }
182}
183
184static inline void _gpio_dbck_disable(struct gpio_bank *bank)
185{
186 if (bank->dbck_enable_mask && bank->dbck_enabled) {
187 clk_disable(bank->dbck);
188 bank->dbck_enabled = false;
189 }
190}
191
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700192/**
193 * _set_gpio_debounce - low level gpio debounce time
194 * @bank: the gpio bank we're acting upon
195 * @gpio: the gpio number on this @gpio
196 * @debounce: debounce time to use
197 *
198 * OMAP's debounce time is in 31us steps so we need
199 * to convert and round up to the closest unit.
200 */
201static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
202 unsigned debounce)
203{
Kevin Hilman9942da02011-04-22 12:02:05 -0700204 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700205 u32 val;
206 u32 l;
207
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800208 if (!bank->dbck_flag)
209 return;
210
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700211 if (debounce < 32)
212 debounce = 0x01;
213 else if (debounce > 7936)
214 debounce = 0xff;
215 else
216 debounce = (debounce / 0x1f) - 1;
217
Kevin Hilman129fd222011-04-22 07:59:07 -0700218 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700219
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530220 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700221 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700222 __raw_writel(debounce, reg);
223
Kevin Hilman9942da02011-04-22 12:02:05 -0700224 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700225 val = __raw_readl(reg);
226
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530227 if (debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700228 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530229 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700230 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300231 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700232
233 __raw_writel(val, reg);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530234 clk_disable(bank->dbck);
235 /*
236 * Enable debounce clock per module.
237 * This call is mandatory because in omap_gpio_request() when
238 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
239 * runtime callbck fails to turn on dbck because dbck_enable_mask
240 * used within _gpio_dbck_enable() is still not initialized at
241 * that point. Therefore we have to enable dbck here.
242 */
243 _gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530244 if (bank->dbck_enable_mask) {
245 bank->context.debounce = debounce;
246 bank->context.debounce_en = val;
247 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700248}
249
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530250static inline void set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530251 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100252{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800253 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100254 u32 gpio_bit = 1 << gpio;
255
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530256 _gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
257 trigger & IRQ_TYPE_LEVEL_LOW);
258 _gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
259 trigger & IRQ_TYPE_LEVEL_HIGH);
260 _gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
261 trigger & IRQ_TYPE_EDGE_RISING);
262 _gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
263 trigger & IRQ_TYPE_EDGE_FALLING);
264
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530265 bank->context.leveldetect0 =
266 __raw_readl(bank->base + bank->regs->leveldetect0);
267 bank->context.leveldetect1 =
268 __raw_readl(bank->base + bank->regs->leveldetect1);
269 bank->context.risingdetect =
270 __raw_readl(bank->base + bank->regs->risingdetect);
271 bank->context.fallingdetect =
272 __raw_readl(bank->base + bank->regs->fallingdetect);
273
274 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530275 _gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530276 bank->context.wake_en =
277 __raw_readl(bank->base + bank->regs->wkup_en);
278 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530279
Ambresh K55b220c2011-06-15 13:40:45 -0700280 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530281 if (!bank->regs->irqctrl) {
282 /* On omap24xx proceed only when valid GPIO bit is set */
283 if (bank->non_wakeup_gpios) {
284 if (!(bank->non_wakeup_gpios & gpio_bit))
285 goto exit;
286 }
287
Chunqiu Wang699117a2009-06-24 17:13:39 +0000288 /*
289 * Log the edge gpio and manually trigger the IRQ
290 * after resume if the input level changes
291 * to avoid irq lost during PER RET/OFF mode
292 * Applies for omap2 non-wakeup gpio and all omap3 gpios
293 */
294 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800295 bank->enabled_non_wakeup_gpios |= gpio_bit;
296 else
297 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
298 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700299
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530300exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530301 bank->level_mask =
302 __raw_readl(bank->base + bank->regs->leveldetect0) |
303 __raw_readl(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100304}
305
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800306#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800307/*
308 * This only applies to chips that can't do both rising and falling edge
309 * detection at once. For all other chips, this function is a noop.
310 */
311static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
312{
313 void __iomem *reg = bank->base;
314 u32 l = 0;
315
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530316 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800317 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530318
319 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800320
321 l = __raw_readl(reg);
322 if ((l >> gpio) & 1)
323 l &= ~(1 << gpio);
324 else
325 l |= 1 << gpio;
326
327 __raw_writel(l, reg);
328}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530329#else
330static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800331#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800332
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530333static int _set_gpio_triggering(struct gpio_bank *bank, int gpio,
334 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100335{
336 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530337 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100338 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100339
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530340 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
341 set_gpio_trigger(bank, gpio, trigger);
342 } else if (bank->regs->irqctrl) {
343 reg += bank->regs->irqctrl;
344
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100345 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000346 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800347 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100348 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100349 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100350 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100351 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100352 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530353 return -EINVAL;
354
355 __raw_writel(l, reg);
356 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100357 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530358 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100359 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530360 reg += bank->regs->edgectrl1;
361
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100362 gpio &= 0x07;
363 l = __raw_readl(reg);
364 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100365 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100366 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100367 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100368 l |= 1 << (gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530369
370 /* Enable wake-up during idle for dynamic tick */
371 _gpio_rmw(base, bank->regs->wkup_en, 1 << gpio, trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530372 bank->context.wake_en =
373 __raw_readl(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530374 __raw_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100375 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100376 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100377}
378
Lennert Buytenheke9191022010-11-29 11:17:17 +0100379static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100380{
Benoit Cousson25db7112012-02-23 21:50:10 +0100381 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100382 unsigned gpio;
383 int retval;
David Brownella6472532008-03-03 04:33:30 -0800384 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100385
Lennert Buytenheke9191022010-11-29 11:17:17 +0100386 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
387 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100388 else
Benoit Cousson25db7112012-02-23 21:50:10 +0100389 gpio = irq_to_gpio(bank, d->irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100390
David Brownelle5c56ed2006-12-06 17:13:59 -0800391 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100392 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800393
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530394 if (!bank->regs->leveldetect0 &&
395 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100396 return -EINVAL;
397
David Brownella6472532008-03-03 04:33:30 -0800398 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700399 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800400 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800401
402 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100403 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800404 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100405 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800406
Tony Lindgren92105bb2005-09-07 17:20:26 +0100407 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100408}
409
410static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
411{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100412 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100413
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700414 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100415 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300416
417 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700418 if (bank->regs->irqstatus2) {
419 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700420 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700421 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700422
423 /* Flush posted write for the irq status to avoid spurious interrupts */
424 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100425}
426
427static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
428{
Kevin Hilman129fd222011-04-22 07:59:07 -0700429 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100430}
431
Imre Deakea6dedd2006-06-26 16:16:00 -0700432static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
433{
434 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700435 u32 l;
Kevin Hilmanc390aad2011-04-21 09:33:36 -0700436 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700437
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700438 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700439 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700440 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700441 l = ~l;
442 l &= mask;
443 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700444}
445
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700446static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100447{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100448 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100449 u32 l;
450
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700451 if (bank->regs->set_irqenable) {
452 reg += bank->regs->set_irqenable;
453 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530454 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700455 } else {
456 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100457 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700458 if (bank->regs->irqenable_inv)
459 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100460 else
461 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530462 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100463 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700464
465 __raw_writel(l, reg);
466}
467
468static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
469{
470 void __iomem *reg = bank->base;
471 u32 l;
472
473 if (bank->regs->clr_irqenable) {
474 reg += bank->regs->clr_irqenable;
475 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530476 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700477 } else {
478 reg += bank->regs->irqenable;
479 l = __raw_readl(reg);
480 if (bank->regs->irqenable_inv)
481 l |= gpio_mask;
482 else
483 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530484 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700485 }
486
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100487 __raw_writel(l, reg);
488}
489
490static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
491{
Tarun Kanti DebBarma82765362011-11-25 15:27:37 +0530492 if (enable)
493 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
494 else
495 _disable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100496}
497
Tony Lindgren92105bb2005-09-07 17:20:26 +0100498/*
499 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
500 * 1510 does not seem to have a wake-up register. If JTAG is connected
501 * to the target, system will wake up always on GPIO events. While
502 * system is running all registered GPIO interrupts need to have wake-up
503 * enabled. When system is suspended, only selected GPIO interrupts need
504 * to have wake-up enabled.
505 */
506static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
507{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700508 u32 gpio_bit = GPIO_BIT(bank, gpio);
509 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800510
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700511 if (bank->non_wakeup_gpios & gpio_bit) {
Benoit Cousson862ff642012-02-01 15:58:56 +0100512 dev_err(bank->dev,
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700513 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100514 return -EINVAL;
515 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700516
517 spin_lock_irqsave(&bank->lock, flags);
518 if (enable)
519 bank->suspend_wakeup |= gpio_bit;
520 else
521 bank->suspend_wakeup &= ~gpio_bit;
522
Tarun Kanti DebBarma381a7522012-02-29 21:49:21 +0530523 __raw_writel(bank->suspend_wakeup, bank->base + bank->regs->wkup_en);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700524 spin_unlock_irqrestore(&bank->lock, flags);
525
526 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100527}
528
Tony Lindgren4196dd62006-09-25 12:41:38 +0300529static void _reset_gpio(struct gpio_bank *bank, int gpio)
530{
Kevin Hilman129fd222011-04-22 07:59:07 -0700531 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300532 _set_gpio_irqenable(bank, gpio, 0);
533 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700534 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300535}
536
Tony Lindgren92105bb2005-09-07 17:20:26 +0100537/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100538static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100539{
Benoit Cousson25db7112012-02-23 21:50:10 +0100540 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
541 unsigned int gpio = irq_to_gpio(bank, d->irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100542
Benoit Cousson25db7112012-02-23 21:50:10 +0100543 return _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100544}
545
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800546static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100547{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800548 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800549 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100550
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530551 /*
552 * If this is the first gpio_request for the bank,
553 * enable the bank module.
554 */
555 if (!bank->mod_usage)
556 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100557
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530558 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300559 /* Set trigger to none. You need to enable the desired trigger with
560 * request_irq() or set_irq_type().
561 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800562 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100563
Charulatha Vfad96ea2011-05-25 11:23:50 +0530564 if (bank->regs->pinctrl) {
565 void __iomem *reg = bank->base + bank->regs->pinctrl;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100566
Tony Lindgren92105bb2005-09-07 17:20:26 +0100567 /* Claim the pin for MPU */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800568 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100569 }
Charulatha Vfad96ea2011-05-25 11:23:50 +0530570
Charulatha Vc8eef652011-05-02 15:21:42 +0530571 if (bank->regs->ctrl && !bank->mod_usage) {
572 void __iomem *reg = bank->base + bank->regs->ctrl;
573 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700574
Charulatha Vc8eef652011-05-02 15:21:42 +0530575 ctrl = __raw_readl(reg);
576 /* Module is enabled, clocks are not gated */
577 ctrl &= ~GPIO_MOD_CTRL_BIT;
578 __raw_writel(ctrl, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530579 bank->context.ctrl = ctrl;
Charulatha V058af1e2009-11-22 10:11:25 -0800580 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530581
582 bank->mod_usage |= 1 << offset;
583
David Brownella6472532008-03-03 04:33:30 -0800584 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100585
586 return 0;
587}
588
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800589static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100590{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800591 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530592 void __iomem *base = bank->base;
David Brownella6472532008-03-03 04:33:30 -0800593 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100594
David Brownella6472532008-03-03 04:33:30 -0800595 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530596
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530597 if (bank->regs->wkup_en) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100598 /* Disable wake-up during idle for dynamic tick */
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530599 _gpio_rmw(base, bank->regs->wkup_en, 1 << offset, 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530600 bank->context.wake_en =
601 __raw_readl(bank->base + bank->regs->wkup_en);
602 }
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530603
Charulatha Vc8eef652011-05-02 15:21:42 +0530604 bank->mod_usage &= ~(1 << offset);
Charulatha V9f096862010-05-14 12:05:27 -0700605
Charulatha Vc8eef652011-05-02 15:21:42 +0530606 if (bank->regs->ctrl && !bank->mod_usage) {
607 void __iomem *reg = bank->base + bank->regs->ctrl;
608 u32 ctrl;
609
610 ctrl = __raw_readl(reg);
611 /* Module is disabled, clocks are gated */
612 ctrl |= GPIO_MOD_CTRL_BIT;
613 __raw_writel(ctrl, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530614 bank->context.ctrl = ctrl;
Charulatha V058af1e2009-11-22 10:11:25 -0800615 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530616
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800617 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800618 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530619
620 /*
621 * If this is the last gpio to be freed in the bank,
622 * disable the bank module.
623 */
624 if (!bank->mod_usage)
625 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100626}
627
628/*
629 * We need to unmask the GPIO bank interrupt as soon as possible to
630 * avoid missing GPIO interrupts for other lines in the bank.
631 * Then we need to mask-read-clear-unmask the triggered GPIO lines
632 * in the bank to avoid missing nested interrupts for a GPIO line.
633 * If we wait to unmask individual GPIO lines in the bank after the
634 * line's interrupt handler has been run, we may miss some nested
635 * interrupts.
636 */
Russell King10dd5ce2006-11-23 11:41:32 +0000637static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100638{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100639 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100640 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800641 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100642 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700643 u32 retrigger = 0;
644 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000645 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100646
Will Deaconee144182011-02-21 13:46:08 +0000647 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100648
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100649 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700650 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530651 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800652
653 if (WARN_ON(!isr_reg))
654 goto exit;
655
Tony Lindgren92105bb2005-09-07 17:20:26 +0100656 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100657 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700658 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100659
Imre Deakea6dedd2006-06-26 16:16:00 -0700660 enabled = _get_gpio_irqbank_mask(bank);
661 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100662
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530663 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800664 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100665
666 /* clear edge sensitive interrupts before handler(s) are
667 called so that we don't miss any interrupt occurred while
668 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700669 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100670 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700671 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100672
673 /* if there is only edge sensitive GPIO pin interrupts
674 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700675 if (!level_mask && !unmasked) {
676 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000677 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700678 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100679
Imre Deakea6dedd2006-06-26 16:16:00 -0700680 isr |= retrigger;
681 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100682 if (!isr)
683 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100684
Benoit Cousson384ebe12011-08-16 11:53:02 +0200685 gpio_irq = bank->irq_base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100686 for (; isr != 0; isr >>= 1, gpio_irq++) {
Benoit Cousson25db7112012-02-23 21:50:10 +0100687 int gpio = irq_to_gpio(bank, gpio_irq);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800688
Tony Lindgren92105bb2005-09-07 17:20:26 +0100689 if (!(isr & 1))
690 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200691
Benoit Cousson25db7112012-02-23 21:50:10 +0100692 gpio_index = GPIO_INDEX(bank, gpio);
693
Cory Maccarrone4318f362010-01-08 10:29:04 -0800694 /*
695 * Some chips can't respond to both rising and falling
696 * at the same time. If this irq was requested with
697 * both flags, we need to flip the ICR data for the IRQ
698 * to respond to the IRQ for the opposite direction.
699 * This will be indicated in the bank toggle_mask.
700 */
701 if (bank->toggle_mask & (1 << gpio_index))
702 _toggle_gpio_edge_triggering(bank, gpio_index);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800703
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100704 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100705 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000706 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700707 /* if bank has any level sensitive GPIO pin interrupt
708 configured, we must unmask the bank interrupt only after
709 handler(s) are executed in order to avoid spurious bank
710 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800711exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700712 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000713 chained_irq_exit(chip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530714 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100715}
716
Lennert Buytenheke9191022010-11-29 11:17:17 +0100717static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300718{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100719 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Benoit Cousson25db7112012-02-23 21:50:10 +0100720 unsigned int gpio = irq_to_gpio(bank, d->irq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700721 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300722
Colin Cross85ec7b92011-06-06 13:38:18 -0700723 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300724 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700725 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300726}
727
Lennert Buytenheke9191022010-11-29 11:17:17 +0100728static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100729{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100730 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Benoit Cousson25db7112012-02-23 21:50:10 +0100731 unsigned int gpio = irq_to_gpio(bank, d->irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732
733 _clear_gpio_irqstatus(bank, gpio);
734}
735
Lennert Buytenheke9191022010-11-29 11:17:17 +0100736static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100737{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100738 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Benoit Cousson25db7112012-02-23 21:50:10 +0100739 unsigned int gpio = irq_to_gpio(bank, d->irq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700740 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100741
Colin Cross85ec7b92011-06-06 13:38:18 -0700742 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100743 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700744 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700745 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100746}
747
Lennert Buytenheke9191022010-11-29 11:17:17 +0100748static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100749{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100750 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Benoit Cousson25db7112012-02-23 21:50:10 +0100751 unsigned int gpio = irq_to_gpio(bank, d->irq);
Kevin Hilman129fd222011-04-22 07:59:07 -0700752 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100753 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700754 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700755
Colin Cross85ec7b92011-06-06 13:38:18 -0700756 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700757 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700758 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800759
760 /* For level-triggered GPIOs, the clearing must be done after
761 * the HW source is cleared, thus after the handler has run */
762 if (bank->level_mask & irq_mask) {
763 _set_gpio_irqenable(bank, gpio, 0);
764 _clear_gpio_irqstatus(bank, gpio);
765 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100766
Kevin Hilman4de8c752008-01-16 21:56:14 -0800767 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700768 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100769}
770
David Brownelle5c56ed2006-12-06 17:13:59 -0800771static struct irq_chip gpio_irq_chip = {
772 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100773 .irq_shutdown = gpio_irq_shutdown,
774 .irq_ack = gpio_ack_irq,
775 .irq_mask = gpio_mask_irq,
776 .irq_unmask = gpio_unmask_irq,
777 .irq_set_type = gpio_irq_type,
778 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800779};
780
781/*---------------------------------------------------------------------*/
782
Magnus Damm79ee0312009-07-08 13:22:04 +0200783static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800784{
Magnus Damm79ee0312009-07-08 13:22:04 +0200785 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800786 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800787 void __iomem *mask_reg = bank->base +
788 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800789 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800790
David Brownella6472532008-03-03 04:33:30 -0800791 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800792 bank->saved_wakeup = __raw_readl(mask_reg);
793 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800794 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800795
796 return 0;
797}
798
Magnus Damm79ee0312009-07-08 13:22:04 +0200799static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800800{
Magnus Damm79ee0312009-07-08 13:22:04 +0200801 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800802 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800803 void __iomem *mask_reg = bank->base +
804 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800805 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800806
David Brownella6472532008-03-03 04:33:30 -0800807 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800808 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800809 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800810
811 return 0;
812}
813
Alexey Dobriyan47145212009-12-14 18:00:08 -0800814static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200815 .suspend_noirq = omap_mpuio_suspend_noirq,
816 .resume_noirq = omap_mpuio_resume_noirq,
817};
818
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200819/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800820static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800821 .driver = {
822 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200823 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800824 },
825};
826
827static struct platform_device omap_mpuio_device = {
828 .name = "mpuio",
829 .id = -1,
830 .dev = {
831 .driver = &omap_mpuio_driver.driver,
832 }
833 /* could list the /proc/iomem resources */
834};
835
Charulatha V03e128c2011-05-05 19:58:01 +0530836static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800837{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800838 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700839
David Brownell11a78b72006-12-06 17:14:11 -0800840 if (platform_driver_register(&omap_mpuio_driver) == 0)
841 (void) platform_device_register(&omap_mpuio_device);
842}
843
David Brownelle5c56ed2006-12-06 17:13:59 -0800844/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100845
David Brownell52e31342008-03-03 12:43:23 -0800846static int gpio_input(struct gpio_chip *chip, unsigned offset)
847{
848 struct gpio_bank *bank;
849 unsigned long flags;
850
851 bank = container_of(chip, struct gpio_bank, chip);
852 spin_lock_irqsave(&bank->lock, flags);
853 _set_gpio_direction(bank, offset, 1);
854 spin_unlock_irqrestore(&bank->lock, flags);
855 return 0;
856}
857
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300858static int gpio_is_input(struct gpio_bank *bank, int mask)
859{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700860 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300861
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300862 return __raw_readl(reg) & mask;
863}
864
David Brownell52e31342008-03-03 12:43:23 -0800865static int gpio_get(struct gpio_chip *chip, unsigned offset)
866{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300867 struct gpio_bank *bank;
868 void __iomem *reg;
869 int gpio;
870 u32 mask;
871
872 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530873 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300874 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700875 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300876
877 if (gpio_is_input(bank, mask))
878 return _get_gpio_datain(bank, gpio);
879 else
880 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800881}
882
883static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
884{
885 struct gpio_bank *bank;
886 unsigned long flags;
887
888 bank = container_of(chip, struct gpio_bank, chip);
889 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700890 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800891 _set_gpio_direction(bank, offset, 0);
892 spin_unlock_irqrestore(&bank->lock, flags);
893 return 0;
894}
895
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700896static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
897 unsigned debounce)
898{
899 struct gpio_bank *bank;
900 unsigned long flags;
901
902 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800903
904 if (!bank->dbck) {
905 bank->dbck = clk_get(bank->dev, "dbclk");
906 if (IS_ERR(bank->dbck))
907 dev_err(bank->dev, "Could not get gpio dbck\n");
908 }
909
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700910 spin_lock_irqsave(&bank->lock, flags);
911 _set_gpio_debounce(bank, offset, debounce);
912 spin_unlock_irqrestore(&bank->lock, flags);
913
914 return 0;
915}
916
David Brownell52e31342008-03-03 12:43:23 -0800917static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
918{
919 struct gpio_bank *bank;
920 unsigned long flags;
921
922 bank = container_of(chip, struct gpio_bank, chip);
923 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700924 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800925 spin_unlock_irqrestore(&bank->lock, flags);
926}
927
David Brownella007b702008-12-10 17:35:25 -0800928static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
929{
930 struct gpio_bank *bank;
931
932 bank = container_of(chip, struct gpio_bank, chip);
Benoit Cousson384ebe12011-08-16 11:53:02 +0200933 return bank->irq_base + offset;
David Brownella007b702008-12-10 17:35:25 -0800934}
935
David Brownell52e31342008-03-03 12:43:23 -0800936/*---------------------------------------------------------------------*/
937
Tony Lindgren9a748052010-12-07 16:26:56 -0800938static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700939{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700940 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700941 u32 rev;
942
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700943 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700944 return;
945
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700946 rev = __raw_readw(bank->base + bank->regs->revision);
947 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700948 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700949
950 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700951}
952
David Brownell8ba55c52008-02-26 11:10:50 -0800953/* This lock class tells lockdep that GPIO irqs are in a different
954 * category than their parents, so it won't report false recursion.
955 */
956static struct lock_class_key gpio_lock_class;
957
Charulatha V03e128c2011-05-05 19:58:01 +0530958static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800959{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530960 void __iomem *base = bank->base;
961 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800962
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530963 if (bank->width == 16)
964 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800965
Charulatha Vd0d665a2011-08-31 00:02:21 +0530966 if (bank->is_mpuio) {
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530967 __raw_writel(l, bank->base + bank->regs->irqenable);
968 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800969 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530970
971 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->irqenable_inv);
972 _gpio_rmw(base, bank->regs->irqstatus, l,
973 bank->regs->irqenable_inv == false);
974 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->debounce_en != 0);
975 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->ctrl != 0);
976 if (bank->regs->debounce_en)
977 _gpio_rmw(base, bank->regs->debounce_en, 0, 1);
978
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +0530979 /* Save OE default value (0xffffffff) in the context */
980 bank->context.oe = __raw_readl(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530981 /* Initialize interface clk ungated, module enabled */
982 if (bank->regs->ctrl)
983 _gpio_rmw(base, bank->regs->ctrl, 0, 1);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800984}
985
Tony Lindgren8805f412012-03-05 15:32:38 -0800986static __devinit void
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700987omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
988 unsigned int num)
989{
990 struct irq_chip_generic *gc;
991 struct irq_chip_type *ct;
992
993 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
994 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -0700995 if (!gc) {
996 dev_err(bank->dev, "Memory alloc failed for gc\n");
997 return;
998 }
999
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001000 ct = gc->chip_types;
1001
1002 /* NOTE: No ack required, reading IRQ status clears it. */
1003 ct->chip.irq_mask = irq_gc_mask_set_bit;
1004 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1005 ct->chip.irq_set_type = gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301006
1007 if (bank->regs->wkup_en)
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001008 ct->chip.irq_set_wake = gpio_wake_enable,
1009
1010 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1011 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1012 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1013}
1014
Russell Kingd52b31d2011-05-27 13:56:12 -07001015static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001016{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001017 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001018 static int gpio;
1019
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001020 /*
1021 * REVISIT eventually switch from OMAP-specific gpio structs
1022 * over to the generic ones
1023 */
1024 bank->chip.request = omap_gpio_request;
1025 bank->chip.free = omap_gpio_free;
1026 bank->chip.direction_input = gpio_input;
1027 bank->chip.get = gpio_get;
1028 bank->chip.direction_output = gpio_output;
1029 bank->chip.set_debounce = gpio_debounce;
1030 bank->chip.set = gpio_set;
1031 bank->chip.to_irq = gpio_2irq;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301032 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001033 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301034 if (bank->regs->wkup_en)
1035 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001036 bank->chip.base = OMAP_MPUIO(0);
1037 } else {
1038 bank->chip.label = "gpio";
1039 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001040 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001041 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001042 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001043
1044 gpiochip_add(&bank->chip);
1045
Benoit Cousson384ebe12011-08-16 11:53:02 +02001046 for (j = bank->irq_base; j < bank->irq_base + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001047 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001048 irq_set_chip_data(j, bank);
Charulatha Vd0d665a2011-08-31 00:02:21 +05301049 if (bank->is_mpuio) {
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001050 omap_mpuio_alloc_gc(bank, j, bank->width);
1051 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001052 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001053 irq_set_handler(j, handle_simple_irq);
1054 set_irq_flags(j, IRQF_VALID);
1055 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001056 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001057 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1058 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001059}
1060
Benoit Cousson384ebe12011-08-16 11:53:02 +02001061static const struct of_device_id omap_gpio_match[];
1062
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001063static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001064{
Benoit Cousson862ff642012-02-01 15:58:56 +01001065 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001066 struct device_node *node = dev->of_node;
1067 const struct of_device_id *match;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001068 struct omap_gpio_platform_data *pdata;
1069 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001070 struct gpio_bank *bank;
Charulatha V03e128c2011-05-05 19:58:01 +05301071 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001072
Benoit Cousson384ebe12011-08-16 11:53:02 +02001073 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1074
1075 pdata = match ? match->data : dev->platform_data;
1076 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001077 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001078
Benoit Cousson96751fc2012-02-01 16:01:39 +01001079 bank = devm_kzalloc(&pdev->dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301080 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001081 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001082 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301083 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001084
1085 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1086 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001087 dev_err(dev, "Invalid IRQ resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001088 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001089 }
1090
1091 bank->irq = res->start;
Benoit Cousson862ff642012-02-01 15:58:56 +01001092 bank->dev = dev;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001093 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001094 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001095 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301096 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301097 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Charulatha V0cde8d02011-05-05 20:15:16 +05301098 bank->loses_context = pdata->loses_context;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301099 bank->get_context_loss_count = pdata->get_context_loss_count;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001100 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001101#ifdef CONFIG_OF_GPIO
1102 bank->chip.of_node = of_node_get(node);
1103#endif
1104
1105 bank->irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
1106 if (bank->irq_base < 0) {
1107 dev_err(dev, "Couldn't allocate IRQ numbers\n");
1108 return -ENODEV;
1109 }
1110
1111 bank->domain = irq_domain_add_legacy(node, bank->width, bank->irq_base,
1112 0, &irq_domain_simple_ops, NULL);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001113
1114 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1115 bank->set_dataout = _set_gpio_dataout_reg;
1116 else
1117 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001118
1119 spin_lock_init(&bank->lock);
1120
1121 /* Static mapping, never released */
1122 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1123 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001124 dev_err(dev, "Invalid mem resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001125 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001126 }
1127
Benoit Cousson96751fc2012-02-01 16:01:39 +01001128 if (!devm_request_mem_region(dev, res->start, resource_size(res),
1129 pdev->name)) {
1130 dev_err(dev, "Region already claimed\n");
1131 return -EBUSY;
1132 }
1133
1134 bank->base = devm_ioremap(dev, res->start, resource_size(res));
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001135 if (!bank->base) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001136 dev_err(dev, "Could not ioremap\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001137 return -ENOMEM;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001138 }
1139
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301140 platform_set_drvdata(pdev, bank);
1141
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001142 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301143 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001144 pm_runtime_get_sync(bank->dev);
1145
Charulatha Vd0d665a2011-08-31 00:02:21 +05301146 if (bank->is_mpuio)
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301147 mpuio_init(bank);
1148
Charulatha V03e128c2011-05-05 19:58:01 +05301149 omap_gpio_mod_init(bank);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001150 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001151 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001152
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301153 pm_runtime_put(bank->dev);
1154
Charulatha V03e128c2011-05-05 19:58:01 +05301155 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001156
Charulatha V03e128c2011-05-05 19:58:01 +05301157 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001158}
1159
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301160#ifdef CONFIG_ARCH_OMAP2PLUS
1161
1162#if defined(CONFIG_PM_SLEEP)
1163static int omap_gpio_suspend(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001164{
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301165 struct platform_device *pdev = to_platform_device(dev);
1166 struct gpio_bank *bank = platform_get_drvdata(pdev);
1167 void __iomem *base = bank->base;
1168 void __iomem *wakeup_enable;
1169 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001170
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301171 if (!bank->mod_usage || !bank->loses_context)
1172 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001173
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301174 if (!bank->regs->wkup_en || !bank->suspend_wakeup)
1175 return 0;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301176
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301177 wakeup_enable = bank->base + bank->regs->wkup_en;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001178
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301179 spin_lock_irqsave(&bank->lock, flags);
1180 bank->saved_wakeup = __raw_readl(wakeup_enable);
1181 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1182 _gpio_rmw(base, bank->regs->wkup_en, bank->suspend_wakeup, 1);
1183 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001184
1185 return 0;
1186}
1187
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301188static int omap_gpio_resume(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001189{
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301190 struct platform_device *pdev = to_platform_device(dev);
1191 struct gpio_bank *bank = platform_get_drvdata(pdev);
1192 void __iomem *base = bank->base;
1193 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001194
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301195 if (!bank->mod_usage || !bank->loses_context)
1196 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001197
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301198 if (!bank->regs->wkup_en || !bank->saved_wakeup)
1199 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001200
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301201 spin_lock_irqsave(&bank->lock, flags);
1202 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1203 _gpio_rmw(base, bank->regs->wkup_en, bank->saved_wakeup, 1);
1204 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301205
1206 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001207}
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301208#endif /* CONFIG_PM_SLEEP */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001209
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301210#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301211static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001212
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301213static int omap_gpio_runtime_suspend(struct device *dev)
1214{
1215 struct platform_device *pdev = to_platform_device(dev);
1216 struct gpio_bank *bank = platform_get_drvdata(pdev);
1217 u32 l1 = 0, l2 = 0;
1218 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001219 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301220
1221 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001222
1223 /*
1224 * Only edges can generate a wakeup event to the PRCM.
1225 *
1226 * Therefore, ensure any wake-up capable GPIOs have
1227 * edge-detection enabled before going idle to ensure a wakeup
1228 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1229 * NDA TRM 25.5.3.1)
1230 *
1231 * The normal values will be restored upon ->runtime_resume()
1232 * by writing back the values saved in bank->context.
1233 */
1234 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1235 if (wake_low)
1236 __raw_writel(wake_low | bank->context.fallingdetect,
1237 bank->base + bank->regs->fallingdetect);
1238 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1239 if (wake_hi)
1240 __raw_writel(wake_hi | bank->context.risingdetect,
1241 bank->base + bank->regs->risingdetect);
1242
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301243 if (bank->power_mode != OFF_MODE) {
1244 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301245 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301246 }
1247 /*
1248 * If going to OFF, remove triggering for all
1249 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1250 * generated. See OMAP2420 Errata item 1.101.
1251 */
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301252 bank->saved_datain = __raw_readl(bank->base +
1253 bank->regs->datain);
1254 l1 = __raw_readl(bank->base + bank->regs->fallingdetect);
1255 l2 = __raw_readl(bank->base + bank->regs->risingdetect);
1256
1257 bank->saved_fallingdetect = l1;
1258 bank->saved_risingdetect = l2;
1259 l1 &= ~bank->enabled_non_wakeup_gpios;
1260 l2 &= ~bank->enabled_non_wakeup_gpios;
1261
1262 __raw_writel(l1, bank->base + bank->regs->fallingdetect);
1263 __raw_writel(l2, bank->base + bank->regs->risingdetect);
1264
1265 bank->workaround_enabled = true;
1266
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301267update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301268 if (bank->get_context_loss_count)
1269 bank->context_loss_count =
1270 bank->get_context_loss_count(bank->dev);
1271
Tarun Kanti DebBarma72f83af2011-11-24 03:03:28 +05301272 _gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301273 spin_unlock_irqrestore(&bank->lock, flags);
1274
1275 return 0;
1276}
1277
1278static int omap_gpio_runtime_resume(struct device *dev)
1279{
1280 struct platform_device *pdev = to_platform_device(dev);
1281 struct gpio_bank *bank = platform_get_drvdata(pdev);
1282 int context_lost_cnt_after;
1283 u32 l = 0, gen, gen0, gen1;
1284 unsigned long flags;
1285
1286 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma72f83af2011-11-24 03:03:28 +05301287 _gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001288
1289 /*
1290 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1291 * GPIOs were set to edge trigger also in order to be able to
1292 * generate a PRCM wakeup. Here we restore the
1293 * pre-runtime_suspend() values for edge triggering.
1294 */
1295 __raw_writel(bank->context.fallingdetect,
1296 bank->base + bank->regs->fallingdetect);
1297 __raw_writel(bank->context.risingdetect,
1298 bank->base + bank->regs->risingdetect);
1299
Tarun Kanti DebBarma960edff2012-03-05 16:00:54 +05301300 if (!bank->workaround_enabled) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301301 spin_unlock_irqrestore(&bank->lock, flags);
1302 return 0;
1303 }
1304
1305 if (bank->get_context_loss_count) {
1306 context_lost_cnt_after =
1307 bank->get_context_loss_count(bank->dev);
1308 if (context_lost_cnt_after != bank->context_loss_count ||
1309 !context_lost_cnt_after) {
1310 omap_gpio_restore_context(bank);
1311 } else {
1312 spin_unlock_irqrestore(&bank->lock, flags);
1313 return 0;
1314 }
1315 }
1316
1317 __raw_writel(bank->saved_fallingdetect,
1318 bank->base + bank->regs->fallingdetect);
1319 __raw_writel(bank->saved_risingdetect,
1320 bank->base + bank->regs->risingdetect);
1321 l = __raw_readl(bank->base + bank->regs->datain);
1322
1323 /*
1324 * Check if any of the non-wakeup interrupt GPIOs have changed
1325 * state. If so, generate an IRQ by software. This is
1326 * horribly racy, but it's the best we can do to work around
1327 * this silicon bug.
1328 */
1329 l ^= bank->saved_datain;
1330 l &= bank->enabled_non_wakeup_gpios;
1331
1332 /*
1333 * No need to generate IRQs for the rising edge for gpio IRQs
1334 * configured with falling edge only; and vice versa.
1335 */
1336 gen0 = l & bank->saved_fallingdetect;
1337 gen0 &= bank->saved_datain;
1338
1339 gen1 = l & bank->saved_risingdetect;
1340 gen1 &= ~(bank->saved_datain);
1341
1342 /* FIXME: Consider GPIO IRQs with level detections properly! */
1343 gen = l & (~(bank->saved_fallingdetect) & ~(bank->saved_risingdetect));
1344 /* Consider all GPIO IRQs needed to be updated */
1345 gen |= gen0 | gen1;
1346
1347 if (gen) {
1348 u32 old0, old1;
1349
1350 old0 = __raw_readl(bank->base + bank->regs->leveldetect0);
1351 old1 = __raw_readl(bank->base + bank->regs->leveldetect1);
1352
1353 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1354 __raw_writel(old0 | gen, bank->base +
1355 bank->regs->leveldetect0);
1356 __raw_writel(old1 | gen, bank->base +
1357 bank->regs->leveldetect1);
1358 }
1359
1360 if (cpu_is_omap44xx()) {
1361 __raw_writel(old0 | l, bank->base +
1362 bank->regs->leveldetect0);
1363 __raw_writel(old1 | l, bank->base +
1364 bank->regs->leveldetect1);
1365 }
1366 __raw_writel(old0, bank->base + bank->regs->leveldetect0);
1367 __raw_writel(old1, bank->base + bank->regs->leveldetect1);
1368 }
1369
1370 bank->workaround_enabled = false;
1371 spin_unlock_irqrestore(&bank->lock, flags);
1372
1373 return 0;
1374}
1375#endif /* CONFIG_PM_RUNTIME */
1376
1377void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001378{
Charulatha V03e128c2011-05-05 19:58:01 +05301379 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001380
Charulatha V03e128c2011-05-05 19:58:01 +05301381 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301382 if (!bank->mod_usage || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301383 continue;
1384
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301385 bank->power_mode = pwr_mode;
1386
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301387 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001388 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001389}
1390
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001391void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001392{
Charulatha V03e128c2011-05-05 19:58:01 +05301393 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001394
Charulatha V03e128c2011-05-05 19:58:01 +05301395 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301396 if (!bank->mod_usage || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301397 continue;
1398
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301399 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001400 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001401}
1402
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301403#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301404static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301405{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301406 __raw_writel(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301407 bank->base + bank->regs->wkup_en);
1408 __raw_writel(bank->context.ctrl, bank->base + bank->regs->ctrl);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301409 __raw_writel(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301410 bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301411 __raw_writel(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301412 bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301413 __raw_writel(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301414 bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301415 __raw_writel(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301416 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301417 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1418 __raw_writel(bank->context.dataout,
1419 bank->base + bank->regs->set_dataout);
1420 else
1421 __raw_writel(bank->context.dataout,
1422 bank->base + bank->regs->dataout);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301423 __raw_writel(bank->context.oe, bank->base + bank->regs->direction);
1424
Nishanth Menonae547352011-09-09 19:08:58 +05301425 if (bank->dbck_enable_mask) {
1426 __raw_writel(bank->context.debounce, bank->base +
1427 bank->regs->debounce);
1428 __raw_writel(bank->context.debounce_en,
1429 bank->base + bank->regs->debounce_en);
1430 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301431
1432 __raw_writel(bank->context.irqenable1,
1433 bank->base + bank->regs->irqenable);
1434 __raw_writel(bank->context.irqenable2,
1435 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301436}
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301437#endif /* CONFIG_PM_RUNTIME */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301438#else
1439#define omap_gpio_suspend NULL
1440#define omap_gpio_resume NULL
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301441#define omap_gpio_runtime_suspend NULL
1442#define omap_gpio_runtime_resume NULL
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301443#endif
1444
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301445static const struct dev_pm_ops gpio_pm_ops = {
1446 SET_SYSTEM_SLEEP_PM_OPS(omap_gpio_suspend, omap_gpio_resume)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301447 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1448 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301449};
1450
Benoit Cousson384ebe12011-08-16 11:53:02 +02001451#if defined(CONFIG_OF)
1452static struct omap_gpio_reg_offs omap2_gpio_regs = {
1453 .revision = OMAP24XX_GPIO_REVISION,
1454 .direction = OMAP24XX_GPIO_OE,
1455 .datain = OMAP24XX_GPIO_DATAIN,
1456 .dataout = OMAP24XX_GPIO_DATAOUT,
1457 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1458 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1459 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1460 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1461 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1462 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1463 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1464 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1465 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1466 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1467 .ctrl = OMAP24XX_GPIO_CTRL,
1468 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1469 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1470 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1471 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1472 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1473};
1474
1475static struct omap_gpio_reg_offs omap4_gpio_regs = {
1476 .revision = OMAP4_GPIO_REVISION,
1477 .direction = OMAP4_GPIO_OE,
1478 .datain = OMAP4_GPIO_DATAIN,
1479 .dataout = OMAP4_GPIO_DATAOUT,
1480 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1481 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1482 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1483 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1484 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1485 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1486 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1487 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1488 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1489 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1490 .ctrl = OMAP4_GPIO_CTRL,
1491 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1492 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1493 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1494 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1495 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1496};
1497
1498static struct omap_gpio_platform_data omap2_pdata = {
1499 .regs = &omap2_gpio_regs,
1500 .bank_width = 32,
1501 .dbck_flag = false,
1502};
1503
1504static struct omap_gpio_platform_data omap3_pdata = {
1505 .regs = &omap2_gpio_regs,
1506 .bank_width = 32,
1507 .dbck_flag = true,
1508};
1509
1510static struct omap_gpio_platform_data omap4_pdata = {
1511 .regs = &omap4_gpio_regs,
1512 .bank_width = 32,
1513 .dbck_flag = true,
1514};
1515
1516static const struct of_device_id omap_gpio_match[] = {
1517 {
1518 .compatible = "ti,omap4-gpio",
1519 .data = &omap4_pdata,
1520 },
1521 {
1522 .compatible = "ti,omap3-gpio",
1523 .data = &omap3_pdata,
1524 },
1525 {
1526 .compatible = "ti,omap2-gpio",
1527 .data = &omap2_pdata,
1528 },
1529 { },
1530};
1531MODULE_DEVICE_TABLE(of, omap_gpio_match);
1532#endif
1533
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001534static struct platform_driver omap_gpio_driver = {
1535 .probe = omap_gpio_probe,
1536 .driver = {
1537 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301538 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001539 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001540 },
1541};
1542
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001543/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001544 * gpio driver register needs to be done before
1545 * machine_init functions access gpio APIs.
1546 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001547 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001548static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001549{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001550 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001551}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001552postcore_initcall(omap_gpio_drv_reg);