blob: f49bd6f47a504b0ec732173e202f6f3dca716afd [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080022#include <linux/slab.h>
23#include <linux/pm_runtime.h>
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +053024#include <linux/pm.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010025
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010027#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/irqs.h>
Russell King1bc857f2011-07-26 10:54:55 +010029#include <asm/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010030#include <asm/mach/irq.h>
31
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053032#define OFF_MODE 1
33
Charulatha V03e128c2011-05-05 19:58:01 +053034static LIST_HEAD(omap_gpio_list);
35
Charulatha V6d62e212011-04-18 15:06:51 +000036struct gpio_regs {
37 u32 irqenable1;
38 u32 irqenable2;
39 u32 wake_en;
40 u32 ctrl;
41 u32 oe;
42 u32 leveldetect0;
43 u32 leveldetect1;
44 u32 risingdetect;
45 u32 fallingdetect;
46 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053047 u32 debounce;
48 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000049};
50
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010051struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053052 struct list_head node;
Tony Lindgren9f7065d2009-10-19 15:25:20 -070053 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010054 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010055 u16 irq;
56 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010057 u32 suspend_wakeup;
58 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080059 u32 non_wakeup_gpios;
60 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000061 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080062 u32 saved_datain;
63 u32 saved_fallingdetect;
64 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080065 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080066 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010067 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080068 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080069 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080070 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080071 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af2011-11-24 03:03:28 +053072 bool dbck_enabled;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080073 struct device *dev;
Charulatha Vd0d665a2011-08-31 00:02:21 +053074 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080075 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053076 bool loses_context;
Tony Lindgren5de62b82010-12-07 16:26:58 -080077 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070078 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053079 int context_loss_count;
Charulatha V03e128c2011-05-05 19:58:01 +053080 u16 id;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053081 int power_mode;
82 bool workaround_enabled;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070083
84 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053085 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070086
87 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010088};
89
Kevin Hilman129fd222011-04-22 07:59:07 -070090#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
91#define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
Charulatha Vc8eef652011-05-02 15:21:42 +053092#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010093
94static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
95{
Tony Lindgren92105bb2005-09-07 17:20:26 +010096 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010097 u32 l;
98
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070099 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100100 l = __raw_readl(reg);
101 if (is_input)
102 l |= 1 << gpio;
103 else
104 l &= ~(1 << gpio);
105 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530106 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100107}
108
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700109
110/* set data out value using dedicate set/clear register */
111static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100112{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100113 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700114 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100115
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700116 if (enable)
117 reg += bank->regs->set_dataout;
118 else
119 reg += bank->regs->clr_dataout;
120
121 __raw_writel(l, reg);
122}
123
124/* set data out value using mask register */
125static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
126{
127 void __iomem *reg = bank->base + bank->regs->dataout;
128 u32 gpio_bit = GPIO_BIT(bank, gpio);
129 u32 l;
130
131 l = __raw_readl(reg);
132 if (enable)
133 l |= gpio_bit;
134 else
135 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530137 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100138}
139
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300140static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100141{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700142 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100143
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700144 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100145}
146
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300147static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
148{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700149 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300150
Kevin Hilman129fd222011-04-22 07:59:07 -0700151 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300152}
153
Kevin Hilmanece95282011-07-12 08:18:15 -0700154static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
155{
156 int l = __raw_readl(base + reg);
157
158 if (set)
159 l |= mask;
160 else
161 l &= ~mask;
162
163 __raw_writel(l, base + reg);
164}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100165
Tarun Kanti DebBarma72f83af2011-11-24 03:03:28 +0530166static inline void _gpio_dbck_enable(struct gpio_bank *bank)
167{
168 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
169 clk_enable(bank->dbck);
170 bank->dbck_enabled = true;
171 }
172}
173
174static inline void _gpio_dbck_disable(struct gpio_bank *bank)
175{
176 if (bank->dbck_enable_mask && bank->dbck_enabled) {
177 clk_disable(bank->dbck);
178 bank->dbck_enabled = false;
179 }
180}
181
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700182/**
183 * _set_gpio_debounce - low level gpio debounce time
184 * @bank: the gpio bank we're acting upon
185 * @gpio: the gpio number on this @gpio
186 * @debounce: debounce time to use
187 *
188 * OMAP's debounce time is in 31us steps so we need
189 * to convert and round up to the closest unit.
190 */
191static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
192 unsigned debounce)
193{
Kevin Hilman9942da02011-04-22 12:02:05 -0700194 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700195 u32 val;
196 u32 l;
197
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800198 if (!bank->dbck_flag)
199 return;
200
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700201 if (debounce < 32)
202 debounce = 0x01;
203 else if (debounce > 7936)
204 debounce = 0xff;
205 else
206 debounce = (debounce / 0x1f) - 1;
207
Kevin Hilman129fd222011-04-22 07:59:07 -0700208 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700209
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530210 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700211 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700212 __raw_writel(debounce, reg);
213
Kevin Hilman9942da02011-04-22 12:02:05 -0700214 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700215 val = __raw_readl(reg);
216
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530217 if (debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700218 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530219 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700220 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300221 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700222
223 __raw_writel(val, reg);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530224 clk_disable(bank->dbck);
225 /*
226 * Enable debounce clock per module.
227 * This call is mandatory because in omap_gpio_request() when
228 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
229 * runtime callbck fails to turn on dbck because dbck_enable_mask
230 * used within _gpio_dbck_enable() is still not initialized at
231 * that point. Therefore we have to enable dbck here.
232 */
233 _gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530234 if (bank->dbck_enable_mask) {
235 bank->context.debounce = debounce;
236 bank->context.debounce_en = val;
237 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700238}
239
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530240static inline void set_gpio_trigger(struct gpio_bank *bank, int gpio,
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700241 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100242{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800243 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100244 u32 gpio_bit = 1 << gpio;
245
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530246 _gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
247 trigger & IRQ_TYPE_LEVEL_LOW);
248 _gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
249 trigger & IRQ_TYPE_LEVEL_HIGH);
250 _gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
251 trigger & IRQ_TYPE_EDGE_RISING);
252 _gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
253 trigger & IRQ_TYPE_EDGE_FALLING);
254
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530255 bank->context.leveldetect0 =
256 __raw_readl(bank->base + bank->regs->leveldetect0);
257 bank->context.leveldetect1 =
258 __raw_readl(bank->base + bank->regs->leveldetect1);
259 bank->context.risingdetect =
260 __raw_readl(bank->base + bank->regs->risingdetect);
261 bank->context.fallingdetect =
262 __raw_readl(bank->base + bank->regs->fallingdetect);
263
264 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530265 _gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530266 bank->context.wake_en =
267 __raw_readl(bank->base + bank->regs->wkup_en);
268 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530269
Ambresh K55b220c2011-06-15 13:40:45 -0700270 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530271 if (!bank->regs->irqctrl) {
272 /* On omap24xx proceed only when valid GPIO bit is set */
273 if (bank->non_wakeup_gpios) {
274 if (!(bank->non_wakeup_gpios & gpio_bit))
275 goto exit;
276 }
277
Chunqiu Wang699117a2009-06-24 17:13:39 +0000278 /*
279 * Log the edge gpio and manually trigger the IRQ
280 * after resume if the input level changes
281 * to avoid irq lost during PER RET/OFF mode
282 * Applies for omap2 non-wakeup gpio and all omap3 gpios
283 */
284 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800285 bank->enabled_non_wakeup_gpios |= gpio_bit;
286 else
287 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
288 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700289
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530290exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530291 bank->level_mask =
292 __raw_readl(bank->base + bank->regs->leveldetect0) |
293 __raw_readl(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100294}
295
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800296#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800297/*
298 * This only applies to chips that can't do both rising and falling edge
299 * detection at once. For all other chips, this function is a noop.
300 */
301static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
302{
303 void __iomem *reg = bank->base;
304 u32 l = 0;
305
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530306 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800307 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530308
309 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800310
311 l = __raw_readl(reg);
312 if ((l >> gpio) & 1)
313 l &= ~(1 << gpio);
314 else
315 l |= 1 << gpio;
316
317 __raw_writel(l, reg);
318}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530319#else
320static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800321#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800322
Tony Lindgren92105bb2005-09-07 17:20:26 +0100323static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
324{
325 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530326 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100327 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100328
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530329 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
330 set_gpio_trigger(bank, gpio, trigger);
331 } else if (bank->regs->irqctrl) {
332 reg += bank->regs->irqctrl;
333
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100334 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000335 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800336 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100337 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100338 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100339 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100341 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530342 return -EINVAL;
343
344 __raw_writel(l, reg);
345 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100346 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530347 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530349 reg += bank->regs->edgectrl1;
350
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100351 gpio &= 0x07;
352 l = __raw_readl(reg);
353 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100354 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100355 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100356 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100357 l |= 1 << (gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530358
359 /* Enable wake-up during idle for dynamic tick */
360 _gpio_rmw(base, bank->regs->wkup_en, 1 << gpio, trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530361 bank->context.wake_en =
362 __raw_readl(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530363 __raw_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100364 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100365 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100366}
367
Lennert Buytenheke9191022010-11-29 11:17:17 +0100368static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100369{
370 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100371 unsigned gpio;
372 int retval;
David Brownella6472532008-03-03 04:33:30 -0800373 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100374
Lennert Buytenheke9191022010-11-29 11:17:17 +0100375 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
376 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100377 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100378 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100379
David Brownelle5c56ed2006-12-06 17:13:59 -0800380 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100381 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800382
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530383 bank = irq_data_get_irq_chip_data(d);
384
385 if (!bank->regs->leveldetect0 &&
386 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100387 return -EINVAL;
388
David Brownella6472532008-03-03 04:33:30 -0800389 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700390 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800391 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800392
393 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100394 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800395 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100396 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800397
Tony Lindgren92105bb2005-09-07 17:20:26 +0100398 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100399}
400
401static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
402{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100403 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100404
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700405 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100406 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300407
408 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700409 if (bank->regs->irqstatus2) {
410 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700411 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700412 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700413
414 /* Flush posted write for the irq status to avoid spurious interrupts */
415 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100416}
417
418static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
419{
Kevin Hilman129fd222011-04-22 07:59:07 -0700420 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100421}
422
Imre Deakea6dedd2006-06-26 16:16:00 -0700423static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
424{
425 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700426 u32 l;
Kevin Hilmanc390aad2011-04-21 09:33:36 -0700427 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700428
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700429 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700430 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700431 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700432 l = ~l;
433 l &= mask;
434 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700435}
436
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700437static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100438{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100439 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100440 u32 l;
441
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700442 if (bank->regs->set_irqenable) {
443 reg += bank->regs->set_irqenable;
444 l = gpio_mask;
445 } else {
446 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100447 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700448 if (bank->regs->irqenable_inv)
449 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100450 else
451 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100452 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700453
454 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530455 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700456}
457
458static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
459{
460 void __iomem *reg = bank->base;
461 u32 l;
462
463 if (bank->regs->clr_irqenable) {
464 reg += bank->regs->clr_irqenable;
465 l = gpio_mask;
466 } else {
467 reg += bank->regs->irqenable;
468 l = __raw_readl(reg);
469 if (bank->regs->irqenable_inv)
470 l |= gpio_mask;
471 else
472 l &= ~gpio_mask;
473 }
474
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530476 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100477}
478
479static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
480{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700481 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100482}
483
Tony Lindgren92105bb2005-09-07 17:20:26 +0100484/*
485 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
486 * 1510 does not seem to have a wake-up register. If JTAG is connected
487 * to the target, system will wake up always on GPIO events. While
488 * system is running all registered GPIO interrupts need to have wake-up
489 * enabled. When system is suspended, only selected GPIO interrupts need
490 * to have wake-up enabled.
491 */
492static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
493{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700494 u32 gpio_bit = GPIO_BIT(bank, gpio);
495 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800496
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700497 if (bank->non_wakeup_gpios & gpio_bit) {
498 dev_err(bank->dev,
499 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100500 return -EINVAL;
501 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700502
503 spin_lock_irqsave(&bank->lock, flags);
504 if (enable)
505 bank->suspend_wakeup |= gpio_bit;
506 else
507 bank->suspend_wakeup &= ~gpio_bit;
508
509 spin_unlock_irqrestore(&bank->lock, flags);
510
511 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100512}
513
Tony Lindgren4196dd62006-09-25 12:41:38 +0300514static void _reset_gpio(struct gpio_bank *bank, int gpio)
515{
Kevin Hilman129fd222011-04-22 07:59:07 -0700516 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300517 _set_gpio_irqenable(bank, gpio, 0);
518 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700519 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300520}
521
Tony Lindgren92105bb2005-09-07 17:20:26 +0100522/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100523static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100524{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100525 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100526 struct gpio_bank *bank;
527 int retval;
528
Lennert Buytenheke9191022010-11-29 11:17:17 +0100529 bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700530 retval = _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100531
532 return retval;
533}
534
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800535static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100536{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800537 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800538 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100539
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530540 /*
541 * If this is the first gpio_request for the bank,
542 * enable the bank module.
543 */
544 if (!bank->mod_usage)
545 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100546
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530547 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300548 /* Set trigger to none. You need to enable the desired trigger with
549 * request_irq() or set_irq_type().
550 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800551 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100552
Charulatha Vfad96ea2011-05-25 11:23:50 +0530553 if (bank->regs->pinctrl) {
554 void __iomem *reg = bank->base + bank->regs->pinctrl;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100555
Tony Lindgren92105bb2005-09-07 17:20:26 +0100556 /* Claim the pin for MPU */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800557 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100558 }
Charulatha Vfad96ea2011-05-25 11:23:50 +0530559
Charulatha Vc8eef652011-05-02 15:21:42 +0530560 if (bank->regs->ctrl && !bank->mod_usage) {
561 void __iomem *reg = bank->base + bank->regs->ctrl;
562 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700563
Charulatha Vc8eef652011-05-02 15:21:42 +0530564 ctrl = __raw_readl(reg);
565 /* Module is enabled, clocks are not gated */
566 ctrl &= ~GPIO_MOD_CTRL_BIT;
567 __raw_writel(ctrl, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530568 bank->context.ctrl = ctrl;
Charulatha V058af1e2009-11-22 10:11:25 -0800569 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530570
571 bank->mod_usage |= 1 << offset;
572
David Brownella6472532008-03-03 04:33:30 -0800573 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100574
575 return 0;
576}
577
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800578static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100579{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800580 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530581 void __iomem *base = bank->base;
David Brownella6472532008-03-03 04:33:30 -0800582 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100583
David Brownella6472532008-03-03 04:33:30 -0800584 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530585
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530586 if (bank->regs->wkup_en) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100587 /* Disable wake-up during idle for dynamic tick */
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530588 _gpio_rmw(base, bank->regs->wkup_en, 1 << offset, 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530589 bank->context.wake_en =
590 __raw_readl(bank->base + bank->regs->wkup_en);
591 }
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530592
Charulatha Vc8eef652011-05-02 15:21:42 +0530593 bank->mod_usage &= ~(1 << offset);
Charulatha V9f096862010-05-14 12:05:27 -0700594
Charulatha Vc8eef652011-05-02 15:21:42 +0530595 if (bank->regs->ctrl && !bank->mod_usage) {
596 void __iomem *reg = bank->base + bank->regs->ctrl;
597 u32 ctrl;
598
599 ctrl = __raw_readl(reg);
600 /* Module is disabled, clocks are gated */
601 ctrl |= GPIO_MOD_CTRL_BIT;
602 __raw_writel(ctrl, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530603 bank->context.ctrl = ctrl;
Charulatha V058af1e2009-11-22 10:11:25 -0800604 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530605
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800606 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800607 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530608
609 /*
610 * If this is the last gpio to be freed in the bank,
611 * disable the bank module.
612 */
613 if (!bank->mod_usage)
614 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100615}
616
617/*
618 * We need to unmask the GPIO bank interrupt as soon as possible to
619 * avoid missing GPIO interrupts for other lines in the bank.
620 * Then we need to mask-read-clear-unmask the triggered GPIO lines
621 * in the bank to avoid missing nested interrupts for a GPIO line.
622 * If we wait to unmask individual GPIO lines in the bank after the
623 * line's interrupt handler has been run, we may miss some nested
624 * interrupts.
625 */
Russell King10dd5ce2006-11-23 11:41:32 +0000626static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100627{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100628 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100629 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800630 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100631 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700632 u32 retrigger = 0;
633 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000634 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100635
Will Deaconee144182011-02-21 13:46:08 +0000636 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100637
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100638 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700639 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530640 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800641
642 if (WARN_ON(!isr_reg))
643 goto exit;
644
Tony Lindgren92105bb2005-09-07 17:20:26 +0100645 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100646 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700647 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100648
Imre Deakea6dedd2006-06-26 16:16:00 -0700649 enabled = _get_gpio_irqbank_mask(bank);
650 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100651
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530652 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800653 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100654
655 /* clear edge sensitive interrupts before handler(s) are
656 called so that we don't miss any interrupt occurred while
657 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700658 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100659 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700660 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100661
662 /* if there is only edge sensitive GPIO pin interrupts
663 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700664 if (!level_mask && !unmasked) {
665 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000666 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700667 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100668
Imre Deakea6dedd2006-06-26 16:16:00 -0700669 isr |= retrigger;
670 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100671 if (!isr)
672 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100673
Tony Lindgren92105bb2005-09-07 17:20:26 +0100674 gpio_irq = bank->virtual_irq_start;
675 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700676 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800677
Tony Lindgren92105bb2005-09-07 17:20:26 +0100678 if (!(isr & 1))
679 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200680
Cory Maccarrone4318f362010-01-08 10:29:04 -0800681 /*
682 * Some chips can't respond to both rising and falling
683 * at the same time. If this irq was requested with
684 * both flags, we need to flip the ICR data for the IRQ
685 * to respond to the IRQ for the opposite direction.
686 * This will be indicated in the bank toggle_mask.
687 */
688 if (bank->toggle_mask & (1 << gpio_index))
689 _toggle_gpio_edge_triggering(bank, gpio_index);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800690
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100691 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100692 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000693 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700694 /* if bank has any level sensitive GPIO pin interrupt
695 configured, we must unmask the bank interrupt only after
696 handler(s) are executed in order to avoid spurious bank
697 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800698exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700699 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000700 chained_irq_exit(chip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530701 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702}
703
Lennert Buytenheke9191022010-11-29 11:17:17 +0100704static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300705{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100706 unsigned int gpio = d->irq - IH_GPIO_BASE;
707 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700708 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300709
Colin Cross85ec7b92011-06-06 13:38:18 -0700710 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300711 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700712 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300713}
714
Lennert Buytenheke9191022010-11-29 11:17:17 +0100715static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100716{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100717 unsigned int gpio = d->irq - IH_GPIO_BASE;
718 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100719
720 _clear_gpio_irqstatus(bank, gpio);
721}
722
Lennert Buytenheke9191022010-11-29 11:17:17 +0100723static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100724{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100725 unsigned int gpio = d->irq - IH_GPIO_BASE;
726 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700727 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100728
Colin Cross85ec7b92011-06-06 13:38:18 -0700729 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700731 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700732 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100733}
734
Lennert Buytenheke9191022010-11-29 11:17:17 +0100735static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100736{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100737 unsigned int gpio = d->irq - IH_GPIO_BASE;
738 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700739 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100740 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700741 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700742
Colin Cross85ec7b92011-06-06 13:38:18 -0700743 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700744 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700745 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800746
747 /* For level-triggered GPIOs, the clearing must be done after
748 * the HW source is cleared, thus after the handler has run */
749 if (bank->level_mask & irq_mask) {
750 _set_gpio_irqenable(bank, gpio, 0);
751 _clear_gpio_irqstatus(bank, gpio);
752 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100753
Kevin Hilman4de8c752008-01-16 21:56:14 -0800754 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700755 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756}
757
David Brownelle5c56ed2006-12-06 17:13:59 -0800758static struct irq_chip gpio_irq_chip = {
759 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100760 .irq_shutdown = gpio_irq_shutdown,
761 .irq_ack = gpio_ack_irq,
762 .irq_mask = gpio_mask_irq,
763 .irq_unmask = gpio_unmask_irq,
764 .irq_set_type = gpio_irq_type,
765 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800766};
767
768/*---------------------------------------------------------------------*/
769
Magnus Damm79ee0312009-07-08 13:22:04 +0200770static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800771{
Magnus Damm79ee0312009-07-08 13:22:04 +0200772 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800773 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800774 void __iomem *mask_reg = bank->base +
775 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800776 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800777
David Brownella6472532008-03-03 04:33:30 -0800778 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800779 bank->saved_wakeup = __raw_readl(mask_reg);
780 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800781 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800782
783 return 0;
784}
785
Magnus Damm79ee0312009-07-08 13:22:04 +0200786static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800787{
Magnus Damm79ee0312009-07-08 13:22:04 +0200788 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800789 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800790 void __iomem *mask_reg = bank->base +
791 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800792 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800793
David Brownella6472532008-03-03 04:33:30 -0800794 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800795 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800796 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800797
798 return 0;
799}
800
Alexey Dobriyan47145212009-12-14 18:00:08 -0800801static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200802 .suspend_noirq = omap_mpuio_suspend_noirq,
803 .resume_noirq = omap_mpuio_resume_noirq,
804};
805
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200806/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800807static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800808 .driver = {
809 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200810 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800811 },
812};
813
814static struct platform_device omap_mpuio_device = {
815 .name = "mpuio",
816 .id = -1,
817 .dev = {
818 .driver = &omap_mpuio_driver.driver,
819 }
820 /* could list the /proc/iomem resources */
821};
822
Charulatha V03e128c2011-05-05 19:58:01 +0530823static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800824{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800825 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700826
David Brownell11a78b72006-12-06 17:14:11 -0800827 if (platform_driver_register(&omap_mpuio_driver) == 0)
828 (void) platform_device_register(&omap_mpuio_device);
829}
830
David Brownelle5c56ed2006-12-06 17:13:59 -0800831/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100832
David Brownell52e31342008-03-03 12:43:23 -0800833static int gpio_input(struct gpio_chip *chip, unsigned offset)
834{
835 struct gpio_bank *bank;
836 unsigned long flags;
837
838 bank = container_of(chip, struct gpio_bank, chip);
839 spin_lock_irqsave(&bank->lock, flags);
840 _set_gpio_direction(bank, offset, 1);
841 spin_unlock_irqrestore(&bank->lock, flags);
842 return 0;
843}
844
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300845static int gpio_is_input(struct gpio_bank *bank, int mask)
846{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700847 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300848
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300849 return __raw_readl(reg) & mask;
850}
851
David Brownell52e31342008-03-03 12:43:23 -0800852static int gpio_get(struct gpio_chip *chip, unsigned offset)
853{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300854 struct gpio_bank *bank;
855 void __iomem *reg;
856 int gpio;
857 u32 mask;
858
859 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530860 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300861 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700862 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300863
864 if (gpio_is_input(bank, mask))
865 return _get_gpio_datain(bank, gpio);
866 else
867 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800868}
869
870static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
871{
872 struct gpio_bank *bank;
873 unsigned long flags;
874
875 bank = container_of(chip, struct gpio_bank, chip);
876 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700877 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800878 _set_gpio_direction(bank, offset, 0);
879 spin_unlock_irqrestore(&bank->lock, flags);
880 return 0;
881}
882
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700883static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
884 unsigned debounce)
885{
886 struct gpio_bank *bank;
887 unsigned long flags;
888
889 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800890
891 if (!bank->dbck) {
892 bank->dbck = clk_get(bank->dev, "dbclk");
893 if (IS_ERR(bank->dbck))
894 dev_err(bank->dev, "Could not get gpio dbck\n");
895 }
896
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700897 spin_lock_irqsave(&bank->lock, flags);
898 _set_gpio_debounce(bank, offset, debounce);
899 spin_unlock_irqrestore(&bank->lock, flags);
900
901 return 0;
902}
903
David Brownell52e31342008-03-03 12:43:23 -0800904static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
905{
906 struct gpio_bank *bank;
907 unsigned long flags;
908
909 bank = container_of(chip, struct gpio_bank, chip);
910 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700911 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800912 spin_unlock_irqrestore(&bank->lock, flags);
913}
914
David Brownella007b702008-12-10 17:35:25 -0800915static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
916{
917 struct gpio_bank *bank;
918
919 bank = container_of(chip, struct gpio_bank, chip);
920 return bank->virtual_irq_start + offset;
921}
922
David Brownell52e31342008-03-03 12:43:23 -0800923/*---------------------------------------------------------------------*/
924
Tony Lindgren9a748052010-12-07 16:26:56 -0800925static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700926{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700927 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700928 u32 rev;
929
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700930 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700931 return;
932
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700933 rev = __raw_readw(bank->base + bank->regs->revision);
934 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700935 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700936
937 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700938}
939
David Brownell8ba55c52008-02-26 11:10:50 -0800940/* This lock class tells lockdep that GPIO irqs are in a different
941 * category than their parents, so it won't report false recursion.
942 */
943static struct lock_class_key gpio_lock_class;
944
Charulatha V03e128c2011-05-05 19:58:01 +0530945static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800946{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530947 void __iomem *base = bank->base;
948 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800949
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530950 if (bank->width == 16)
951 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800952
Charulatha Vd0d665a2011-08-31 00:02:21 +0530953 if (bank->is_mpuio) {
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530954 __raw_writel(l, bank->base + bank->regs->irqenable);
955 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800956 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530957
958 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->irqenable_inv);
959 _gpio_rmw(base, bank->regs->irqstatus, l,
960 bank->regs->irqenable_inv == false);
961 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->debounce_en != 0);
962 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->ctrl != 0);
963 if (bank->regs->debounce_en)
964 _gpio_rmw(base, bank->regs->debounce_en, 0, 1);
965
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +0530966 /* Save OE default value (0xffffffff) in the context */
967 bank->context.oe = __raw_readl(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530968 /* Initialize interface clk ungated, module enabled */
969 if (bank->regs->ctrl)
970 _gpio_rmw(base, bank->regs->ctrl, 0, 1);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800971}
972
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700973static __init void
974omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
975 unsigned int num)
976{
977 struct irq_chip_generic *gc;
978 struct irq_chip_type *ct;
979
980 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
981 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -0700982 if (!gc) {
983 dev_err(bank->dev, "Memory alloc failed for gc\n");
984 return;
985 }
986
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700987 ct = gc->chip_types;
988
989 /* NOTE: No ack required, reading IRQ status clears it. */
990 ct->chip.irq_mask = irq_gc_mask_set_bit;
991 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
992 ct->chip.irq_set_type = gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530993
994 if (bank->regs->wkup_en)
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700995 ct->chip.irq_set_wake = gpio_wake_enable,
996
997 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
998 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
999 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1000}
1001
Russell Kingd52b31d2011-05-27 13:56:12 -07001002static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001003{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001004 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001005 static int gpio;
1006
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001007 /*
1008 * REVISIT eventually switch from OMAP-specific gpio structs
1009 * over to the generic ones
1010 */
1011 bank->chip.request = omap_gpio_request;
1012 bank->chip.free = omap_gpio_free;
1013 bank->chip.direction_input = gpio_input;
1014 bank->chip.get = gpio_get;
1015 bank->chip.direction_output = gpio_output;
1016 bank->chip.set_debounce = gpio_debounce;
1017 bank->chip.set = gpio_set;
1018 bank->chip.to_irq = gpio_2irq;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301019 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001020 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301021 if (bank->regs->wkup_en)
1022 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001023 bank->chip.base = OMAP_MPUIO(0);
1024 } else {
1025 bank->chip.label = "gpio";
1026 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001027 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001028 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001029 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001030
1031 gpiochip_add(&bank->chip);
1032
1033 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001034 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001035 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001036 irq_set_chip_data(j, bank);
Charulatha Vd0d665a2011-08-31 00:02:21 +05301037 if (bank->is_mpuio) {
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001038 omap_mpuio_alloc_gc(bank, j, bank->width);
1039 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001040 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001041 irq_set_handler(j, handle_simple_irq);
1042 set_irq_flags(j, IRQF_VALID);
1043 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001044 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001045 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1046 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001047}
1048
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001049static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001050{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001051 struct omap_gpio_platform_data *pdata;
1052 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001053 struct gpio_bank *bank;
Charulatha V03e128c2011-05-05 19:58:01 +05301054 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001055
Charulatha V03e128c2011-05-05 19:58:01 +05301056 if (!pdev->dev.platform_data) {
1057 ret = -EINVAL;
1058 goto err_exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001059 }
1060
Charulatha V03e128c2011-05-05 19:58:01 +05301061 bank = kzalloc(sizeof(struct gpio_bank), GFP_KERNEL);
1062 if (!bank) {
1063 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1064 ret = -ENOMEM;
1065 goto err_exit;
1066 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001067
1068 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1069 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301070 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n",
1071 pdev->id);
1072 ret = -ENODEV;
1073 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001074 }
1075
1076 bank->irq = res->start;
Charulatha V03e128c2011-05-05 19:58:01 +05301077 bank->id = pdev->id;
1078
1079 pdata = pdev->dev.platform_data;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001080 bank->virtual_irq_start = pdata->virtual_irq_start;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001081 bank->dev = &pdev->dev;
1082 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;
1090
1091 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1092 bank->set_dataout = _set_gpio_dataout_reg;
1093 else
1094 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001095
1096 spin_lock_init(&bank->lock);
1097
1098 /* Static mapping, never released */
1099 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1100 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301101 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n",
1102 pdev->id);
1103 ret = -ENODEV;
1104 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001105 }
1106
1107 bank->base = ioremap(res->start, resource_size(res));
1108 if (!bank->base) {
Charulatha V03e128c2011-05-05 19:58:01 +05301109 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n",
1110 pdev->id);
1111 ret = -ENOMEM;
1112 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001113 }
1114
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301115 platform_set_drvdata(pdev, bank);
1116
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001117 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301118 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001119 pm_runtime_get_sync(bank->dev);
1120
Charulatha Vd0d665a2011-08-31 00:02:21 +05301121 if (bank->is_mpuio)
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301122 mpuio_init(bank);
1123
Charulatha V03e128c2011-05-05 19:58:01 +05301124 omap_gpio_mod_init(bank);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001125 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001126 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001127
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301128 pm_runtime_put(bank->dev);
1129
Charulatha V03e128c2011-05-05 19:58:01 +05301130 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001131
Charulatha V03e128c2011-05-05 19:58:01 +05301132 return ret;
1133
1134err_free:
1135 kfree(bank);
1136err_exit:
1137 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001138}
1139
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301140#ifdef CONFIG_ARCH_OMAP2PLUS
1141
1142#if defined(CONFIG_PM_SLEEP)
1143static int omap_gpio_suspend(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001144{
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301145 struct platform_device *pdev = to_platform_device(dev);
1146 struct gpio_bank *bank = platform_get_drvdata(pdev);
1147 void __iomem *base = bank->base;
1148 void __iomem *wakeup_enable;
1149 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001150
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301151 if (!bank->mod_usage || !bank->loses_context)
1152 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001153
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301154 if (!bank->regs->wkup_en || !bank->suspend_wakeup)
1155 return 0;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301156
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301157 wakeup_enable = bank->base + bank->regs->wkup_en;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001158
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301159 spin_lock_irqsave(&bank->lock, flags);
1160 bank->saved_wakeup = __raw_readl(wakeup_enable);
1161 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1162 _gpio_rmw(base, bank->regs->wkup_en, bank->suspend_wakeup, 1);
1163 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001164
1165 return 0;
1166}
1167
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301168static int omap_gpio_resume(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001169{
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301170 struct platform_device *pdev = to_platform_device(dev);
1171 struct gpio_bank *bank = platform_get_drvdata(pdev);
1172 void __iomem *base = bank->base;
1173 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001174
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301175 if (!bank->mod_usage || !bank->loses_context)
1176 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001177
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301178 if (!bank->regs->wkup_en || !bank->saved_wakeup)
1179 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001180
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301181 spin_lock_irqsave(&bank->lock, flags);
1182 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1183 _gpio_rmw(base, bank->regs->wkup_en, bank->saved_wakeup, 1);
1184 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301185
1186 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001187}
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301188#endif /* CONFIG_PM_SLEEP */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001189
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301190#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301191static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001192
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301193static int omap_gpio_runtime_suspend(struct device *dev)
1194{
1195 struct platform_device *pdev = to_platform_device(dev);
1196 struct gpio_bank *bank = platform_get_drvdata(pdev);
1197 u32 l1 = 0, l2 = 0;
1198 unsigned long flags;
1199
1200 spin_lock_irqsave(&bank->lock, flags);
1201 if (bank->power_mode != OFF_MODE) {
1202 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301203 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301204 }
1205 /*
1206 * If going to OFF, remove triggering for all
1207 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1208 * generated. See OMAP2420 Errata item 1.101.
1209 */
1210 if (!(bank->enabled_non_wakeup_gpios))
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301211 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301212
1213 bank->saved_datain = __raw_readl(bank->base +
1214 bank->regs->datain);
1215 l1 = __raw_readl(bank->base + bank->regs->fallingdetect);
1216 l2 = __raw_readl(bank->base + bank->regs->risingdetect);
1217
1218 bank->saved_fallingdetect = l1;
1219 bank->saved_risingdetect = l2;
1220 l1 &= ~bank->enabled_non_wakeup_gpios;
1221 l2 &= ~bank->enabled_non_wakeup_gpios;
1222
1223 __raw_writel(l1, bank->base + bank->regs->fallingdetect);
1224 __raw_writel(l2, bank->base + bank->regs->risingdetect);
1225
1226 bank->workaround_enabled = true;
1227
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301228update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301229 if (bank->get_context_loss_count)
1230 bank->context_loss_count =
1231 bank->get_context_loss_count(bank->dev);
1232
Tarun Kanti DebBarma72f83af2011-11-24 03:03:28 +05301233 _gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301234 spin_unlock_irqrestore(&bank->lock, flags);
1235
1236 return 0;
1237}
1238
1239static int omap_gpio_runtime_resume(struct device *dev)
1240{
1241 struct platform_device *pdev = to_platform_device(dev);
1242 struct gpio_bank *bank = platform_get_drvdata(pdev);
1243 int context_lost_cnt_after;
1244 u32 l = 0, gen, gen0, gen1;
1245 unsigned long flags;
1246
1247 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma72f83af2011-11-24 03:03:28 +05301248 _gpio_dbck_enable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301249 if (!bank->enabled_non_wakeup_gpios || !bank->workaround_enabled) {
1250 spin_unlock_irqrestore(&bank->lock, flags);
1251 return 0;
1252 }
1253
1254 if (bank->get_context_loss_count) {
1255 context_lost_cnt_after =
1256 bank->get_context_loss_count(bank->dev);
1257 if (context_lost_cnt_after != bank->context_loss_count ||
1258 !context_lost_cnt_after) {
1259 omap_gpio_restore_context(bank);
1260 } else {
1261 spin_unlock_irqrestore(&bank->lock, flags);
1262 return 0;
1263 }
1264 }
1265
1266 __raw_writel(bank->saved_fallingdetect,
1267 bank->base + bank->regs->fallingdetect);
1268 __raw_writel(bank->saved_risingdetect,
1269 bank->base + bank->regs->risingdetect);
1270 l = __raw_readl(bank->base + bank->regs->datain);
1271
1272 /*
1273 * Check if any of the non-wakeup interrupt GPIOs have changed
1274 * state. If so, generate an IRQ by software. This is
1275 * horribly racy, but it's the best we can do to work around
1276 * this silicon bug.
1277 */
1278 l ^= bank->saved_datain;
1279 l &= bank->enabled_non_wakeup_gpios;
1280
1281 /*
1282 * No need to generate IRQs for the rising edge for gpio IRQs
1283 * configured with falling edge only; and vice versa.
1284 */
1285 gen0 = l & bank->saved_fallingdetect;
1286 gen0 &= bank->saved_datain;
1287
1288 gen1 = l & bank->saved_risingdetect;
1289 gen1 &= ~(bank->saved_datain);
1290
1291 /* FIXME: Consider GPIO IRQs with level detections properly! */
1292 gen = l & (~(bank->saved_fallingdetect) & ~(bank->saved_risingdetect));
1293 /* Consider all GPIO IRQs needed to be updated */
1294 gen |= gen0 | gen1;
1295
1296 if (gen) {
1297 u32 old0, old1;
1298
1299 old0 = __raw_readl(bank->base + bank->regs->leveldetect0);
1300 old1 = __raw_readl(bank->base + bank->regs->leveldetect1);
1301
1302 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1303 __raw_writel(old0 | gen, bank->base +
1304 bank->regs->leveldetect0);
1305 __raw_writel(old1 | gen, bank->base +
1306 bank->regs->leveldetect1);
1307 }
1308
1309 if (cpu_is_omap44xx()) {
1310 __raw_writel(old0 | l, bank->base +
1311 bank->regs->leveldetect0);
1312 __raw_writel(old1 | l, bank->base +
1313 bank->regs->leveldetect1);
1314 }
1315 __raw_writel(old0, bank->base + bank->regs->leveldetect0);
1316 __raw_writel(old1, bank->base + bank->regs->leveldetect1);
1317 }
1318
1319 bank->workaround_enabled = false;
1320 spin_unlock_irqrestore(&bank->lock, flags);
1321
1322 return 0;
1323}
1324#endif /* CONFIG_PM_RUNTIME */
1325
1326void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001327{
Charulatha V03e128c2011-05-05 19:58:01 +05301328 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001329
Charulatha V03e128c2011-05-05 19:58:01 +05301330 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301331 if (!bank->mod_usage || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301332 continue;
1333
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301334 bank->power_mode = pwr_mode;
1335
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301336 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001337 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001338}
1339
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001340void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001341{
Charulatha V03e128c2011-05-05 19:58:01 +05301342 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001343
Charulatha V03e128c2011-05-05 19:58:01 +05301344 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301345 if (!bank->mod_usage || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301346 continue;
1347
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301348 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001349 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001350}
1351
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301352#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301353static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301354{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301355 __raw_writel(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301356 bank->base + bank->regs->wkup_en);
1357 __raw_writel(bank->context.ctrl, bank->base + bank->regs->ctrl);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301358 __raw_writel(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301359 bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301360 __raw_writel(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301361 bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301362 __raw_writel(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301363 bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301364 __raw_writel(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301365 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301366 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1367 __raw_writel(bank->context.dataout,
1368 bank->base + bank->regs->set_dataout);
1369 else
1370 __raw_writel(bank->context.dataout,
1371 bank->base + bank->regs->dataout);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301372 __raw_writel(bank->context.oe, bank->base + bank->regs->direction);
1373
Nishanth Menonae547352011-09-09 19:08:58 +05301374 if (bank->dbck_enable_mask) {
1375 __raw_writel(bank->context.debounce, bank->base +
1376 bank->regs->debounce);
1377 __raw_writel(bank->context.debounce_en,
1378 bank->base + bank->regs->debounce_en);
1379 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301380
1381 __raw_writel(bank->context.irqenable1,
1382 bank->base + bank->regs->irqenable);
1383 __raw_writel(bank->context.irqenable2,
1384 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301385}
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301386#endif /* CONFIG_PM_RUNTIME */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301387#else
1388#define omap_gpio_suspend NULL
1389#define omap_gpio_resume NULL
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301390#define omap_gpio_runtime_suspend NULL
1391#define omap_gpio_runtime_resume NULL
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301392#endif
1393
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301394static const struct dev_pm_ops gpio_pm_ops = {
1395 SET_SYSTEM_SLEEP_PM_OPS(omap_gpio_suspend, omap_gpio_resume)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301396 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1397 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301398};
1399
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001400static struct platform_driver omap_gpio_driver = {
1401 .probe = omap_gpio_probe,
1402 .driver = {
1403 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301404 .pm = &gpio_pm_ops,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001405 },
1406};
1407
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001408/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001409 * gpio driver register needs to be done before
1410 * machine_init functions access gpio APIs.
1411 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001412 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001413static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001414{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001415 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001416}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001417postcore_initcall(omap_gpio_drv_reg);