blob: d483cc9f0c638e485476d54ec8c6c91481062014 [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;
47};
48
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010049struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053050 struct list_head node;
Tony Lindgren9f7065d2009-10-19 15:25:20 -070051 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010052 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010053 u16 irq;
54 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010055 u32 suspend_wakeup;
56 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080057 u32 non_wakeup_gpios;
58 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000059 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080060 u32 saved_datain;
61 u32 saved_fallingdetect;
62 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080063 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080064 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010065 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080066 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080067 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080068 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080069 u32 dbck_enable_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080070 struct device *dev;
Charulatha Vd0d665a2011-08-31 00:02:21 +053071 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080072 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053073 bool loses_context;
Tony Lindgren5de62b82010-12-07 16:26:58 -080074 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070075 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053076 int context_loss_count;
Charulatha V03e128c2011-05-05 19:58:01 +053077 u16 id;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053078 int power_mode;
79 bool workaround_enabled;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070080
81 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053082 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070083
84 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085};
86
Kevin Hilman129fd222011-04-22 07:59:07 -070087#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
88#define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
Charulatha Vc8eef652011-05-02 15:21:42 +053089#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010090
91static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
92{
Tony Lindgren92105bb2005-09-07 17:20:26 +010093 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010094 u32 l;
95
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070096 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010097 l = __raw_readl(reg);
98 if (is_input)
99 l |= 1 << gpio;
100 else
101 l &= ~(1 << gpio);
102 __raw_writel(l, reg);
103}
104
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700105
106/* set data out value using dedicate set/clear register */
107static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100108{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100109 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700110 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100111
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700112 if (enable)
113 reg += bank->regs->set_dataout;
114 else
115 reg += bank->regs->clr_dataout;
116
117 __raw_writel(l, reg);
118}
119
120/* set data out value using mask register */
121static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
122{
123 void __iomem *reg = bank->base + bank->regs->dataout;
124 u32 gpio_bit = GPIO_BIT(bank, gpio);
125 u32 l;
126
127 l = __raw_readl(reg);
128 if (enable)
129 l |= gpio_bit;
130 else
131 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132 __raw_writel(l, reg);
133}
134
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300135static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700137 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100138
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700139 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100140}
141
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300142static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
143{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700144 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300145
Kevin Hilman129fd222011-04-22 07:59:07 -0700146 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300147}
148
Kevin Hilmanece95282011-07-12 08:18:15 -0700149static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
150{
151 int l = __raw_readl(base + reg);
152
153 if (set)
154 l |= mask;
155 else
156 l &= ~mask;
157
158 __raw_writel(l, base + reg);
159}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100160
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700161/**
162 * _set_gpio_debounce - low level gpio debounce time
163 * @bank: the gpio bank we're acting upon
164 * @gpio: the gpio number on this @gpio
165 * @debounce: debounce time to use
166 *
167 * OMAP's debounce time is in 31us steps so we need
168 * to convert and round up to the closest unit.
169 */
170static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
171 unsigned debounce)
172{
Kevin Hilman9942da02011-04-22 12:02:05 -0700173 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700174 u32 val;
175 u32 l;
176
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800177 if (!bank->dbck_flag)
178 return;
179
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700180 if (debounce < 32)
181 debounce = 0x01;
182 else if (debounce > 7936)
183 debounce = 0xff;
184 else
185 debounce = (debounce / 0x1f) - 1;
186
Kevin Hilman129fd222011-04-22 07:59:07 -0700187 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700188
Kevin Hilman9942da02011-04-22 12:02:05 -0700189 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700190 __raw_writel(debounce, reg);
191
Kevin Hilman9942da02011-04-22 12:02:05 -0700192 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700193 val = __raw_readl(reg);
194
195 if (debounce) {
196 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800197 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700198 } else {
199 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800200 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700201 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300202 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700203
204 __raw_writel(val, reg);
205}
206
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530207static inline void set_gpio_trigger(struct gpio_bank *bank, int gpio,
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700208 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100209{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800210 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100211 u32 gpio_bit = 1 << gpio;
212
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530213 _gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
214 trigger & IRQ_TYPE_LEVEL_LOW);
215 _gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
216 trigger & IRQ_TYPE_LEVEL_HIGH);
217 _gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
218 trigger & IRQ_TYPE_EDGE_RISING);
219 _gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
220 trigger & IRQ_TYPE_EDGE_FALLING);
221
222 if (likely(!(bank->non_wakeup_gpios & gpio_bit)))
223 _gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
224
Ambresh K55b220c2011-06-15 13:40:45 -0700225 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530226 if (!bank->regs->irqctrl) {
227 /* On omap24xx proceed only when valid GPIO bit is set */
228 if (bank->non_wakeup_gpios) {
229 if (!(bank->non_wakeup_gpios & gpio_bit))
230 goto exit;
231 }
232
Chunqiu Wang699117a62009-06-24 17:13:39 +0000233 /*
234 * Log the edge gpio and manually trigger the IRQ
235 * after resume if the input level changes
236 * to avoid irq lost during PER RET/OFF mode
237 * Applies for omap2 non-wakeup gpio and all omap3 gpios
238 */
239 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800240 bank->enabled_non_wakeup_gpios |= gpio_bit;
241 else
242 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
243 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700244
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530245exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530246 bank->level_mask =
247 __raw_readl(bank->base + bank->regs->leveldetect0) |
248 __raw_readl(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100249}
250
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800251#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800252/*
253 * This only applies to chips that can't do both rising and falling edge
254 * detection at once. For all other chips, this function is a noop.
255 */
256static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
257{
258 void __iomem *reg = bank->base;
259 u32 l = 0;
260
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530261 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800262 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530263
264 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800265
266 l = __raw_readl(reg);
267 if ((l >> gpio) & 1)
268 l &= ~(1 << gpio);
269 else
270 l |= 1 << gpio;
271
272 __raw_writel(l, reg);
273}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530274#else
275static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800276#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800277
Tony Lindgren92105bb2005-09-07 17:20:26 +0100278static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
279{
280 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530281 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100282 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100283
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530284 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
285 set_gpio_trigger(bank, gpio, trigger);
286 } else if (bank->regs->irqctrl) {
287 reg += bank->regs->irqctrl;
288
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100289 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000290 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800291 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100292 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100293 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100294 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100295 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100296 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530297 return -EINVAL;
298
299 __raw_writel(l, reg);
300 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100301 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530302 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100303 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530304 reg += bank->regs->edgectrl1;
305
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100306 gpio &= 0x07;
307 l = __raw_readl(reg);
308 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100309 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100310 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100311 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100312 l |= 1 << (gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530313
314 /* Enable wake-up during idle for dynamic tick */
315 _gpio_rmw(base, bank->regs->wkup_en, 1 << gpio, trigger);
316 __raw_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100317 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100318 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100319}
320
Lennert Buytenheke9191022010-11-29 11:17:17 +0100321static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100322{
323 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100324 unsigned gpio;
325 int retval;
David Brownella6472532008-03-03 04:33:30 -0800326 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100327
Lennert Buytenheke9191022010-11-29 11:17:17 +0100328 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
329 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100330 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100331 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100332
David Brownelle5c56ed2006-12-06 17:13:59 -0800333 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100334 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800335
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530336 bank = irq_data_get_irq_chip_data(d);
337
338 if (!bank->regs->leveldetect0 &&
339 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100340 return -EINVAL;
341
David Brownella6472532008-03-03 04:33:30 -0800342 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700343 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800344 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800345
346 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100347 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800348 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100349 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800350
Tony Lindgren92105bb2005-09-07 17:20:26 +0100351 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100352}
353
354static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
355{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100356 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100357
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700358 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100359 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300360
361 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700362 if (bank->regs->irqstatus2) {
363 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700364 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700365 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700366
367 /* Flush posted write for the irq status to avoid spurious interrupts */
368 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100369}
370
371static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
372{
Kevin Hilman129fd222011-04-22 07:59:07 -0700373 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100374}
375
Imre Deakea6dedd2006-06-26 16:16:00 -0700376static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
377{
378 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700379 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700380 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700381
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700382 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700383 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700384 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700385 l = ~l;
386 l &= mask;
387 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700388}
389
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700390static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100391{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100392 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100393 u32 l;
394
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700395 if (bank->regs->set_irqenable) {
396 reg += bank->regs->set_irqenable;
397 l = gpio_mask;
398 } else {
399 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100400 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700401 if (bank->regs->irqenable_inv)
402 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100403 else
404 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100405 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700406
407 __raw_writel(l, reg);
408}
409
410static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
411{
412 void __iomem *reg = bank->base;
413 u32 l;
414
415 if (bank->regs->clr_irqenable) {
416 reg += bank->regs->clr_irqenable;
417 l = gpio_mask;
418 } else {
419 reg += bank->regs->irqenable;
420 l = __raw_readl(reg);
421 if (bank->regs->irqenable_inv)
422 l |= gpio_mask;
423 else
424 l &= ~gpio_mask;
425 }
426
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100427 __raw_writel(l, reg);
428}
429
430static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
431{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700432 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100433}
434
Tony Lindgren92105bb2005-09-07 17:20:26 +0100435/*
436 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
437 * 1510 does not seem to have a wake-up register. If JTAG is connected
438 * to the target, system will wake up always on GPIO events. While
439 * system is running all registered GPIO interrupts need to have wake-up
440 * enabled. When system is suspended, only selected GPIO interrupts need
441 * to have wake-up enabled.
442 */
443static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
444{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700445 u32 gpio_bit = GPIO_BIT(bank, gpio);
446 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800447
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700448 if (bank->non_wakeup_gpios & gpio_bit) {
449 dev_err(bank->dev,
450 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100451 return -EINVAL;
452 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700453
454 spin_lock_irqsave(&bank->lock, flags);
455 if (enable)
456 bank->suspend_wakeup |= gpio_bit;
457 else
458 bank->suspend_wakeup &= ~gpio_bit;
459
460 spin_unlock_irqrestore(&bank->lock, flags);
461
462 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100463}
464
Tony Lindgren4196dd62006-09-25 12:41:38 +0300465static void _reset_gpio(struct gpio_bank *bank, int gpio)
466{
Kevin Hilman129fd222011-04-22 07:59:07 -0700467 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300468 _set_gpio_irqenable(bank, gpio, 0);
469 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700470 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300471}
472
Tony Lindgren92105bb2005-09-07 17:20:26 +0100473/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100474static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100475{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100476 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100477 struct gpio_bank *bank;
478 int retval;
479
Lennert Buytenheke9191022010-11-29 11:17:17 +0100480 bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700481 retval = _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100482
483 return retval;
484}
485
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800486static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100487{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800488 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800489 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100490
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530491 /*
492 * If this is the first gpio_request for the bank,
493 * enable the bank module.
494 */
495 if (!bank->mod_usage)
496 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100497
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530498 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300499 /* Set trigger to none. You need to enable the desired trigger with
500 * request_irq() or set_irq_type().
501 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800502 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100503
Charulatha Vfad96ea2011-05-25 11:23:50 +0530504 if (bank->regs->pinctrl) {
505 void __iomem *reg = bank->base + bank->regs->pinctrl;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100506
Tony Lindgren92105bb2005-09-07 17:20:26 +0100507 /* Claim the pin for MPU */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800508 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100509 }
Charulatha Vfad96ea2011-05-25 11:23:50 +0530510
Charulatha Vc8eef652011-05-02 15:21:42 +0530511 if (bank->regs->ctrl && !bank->mod_usage) {
512 void __iomem *reg = bank->base + bank->regs->ctrl;
513 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700514
Charulatha Vc8eef652011-05-02 15:21:42 +0530515 ctrl = __raw_readl(reg);
516 /* Module is enabled, clocks are not gated */
517 ctrl &= ~GPIO_MOD_CTRL_BIT;
518 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800519 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530520
521 bank->mod_usage |= 1 << offset;
522
David Brownella6472532008-03-03 04:33:30 -0800523 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100524
525 return 0;
526}
527
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800528static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800530 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530531 void __iomem *base = bank->base;
David Brownella6472532008-03-03 04:33:30 -0800532 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100533
David Brownella6472532008-03-03 04:33:30 -0800534 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530535
536 if (bank->regs->wkup_en)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100537 /* Disable wake-up during idle for dynamic tick */
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530538 _gpio_rmw(base, bank->regs->wkup_en, 1 << offset, 0);
539
Charulatha Vc8eef652011-05-02 15:21:42 +0530540 bank->mod_usage &= ~(1 << offset);
Charulatha V9f096862010-05-14 12:05:27 -0700541
Charulatha Vc8eef652011-05-02 15:21:42 +0530542 if (bank->regs->ctrl && !bank->mod_usage) {
543 void __iomem *reg = bank->base + bank->regs->ctrl;
544 u32 ctrl;
545
546 ctrl = __raw_readl(reg);
547 /* Module is disabled, clocks are gated */
548 ctrl |= GPIO_MOD_CTRL_BIT;
549 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800550 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530551
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800552 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800553 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530554
555 /*
556 * If this is the last gpio to be freed in the bank,
557 * disable the bank module.
558 */
559 if (!bank->mod_usage)
560 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100561}
562
563/*
564 * We need to unmask the GPIO bank interrupt as soon as possible to
565 * avoid missing GPIO interrupts for other lines in the bank.
566 * Then we need to mask-read-clear-unmask the triggered GPIO lines
567 * in the bank to avoid missing nested interrupts for a GPIO line.
568 * If we wait to unmask individual GPIO lines in the bank after the
569 * line's interrupt handler has been run, we may miss some nested
570 * interrupts.
571 */
Russell King10dd5ce2006-11-23 11:41:32 +0000572static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100574 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100575 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800576 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100577 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700578 u32 retrigger = 0;
579 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000580 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100581
Will Deaconee144182011-02-21 13:46:08 +0000582 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100583
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100584 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700585 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530586 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800587
588 if (WARN_ON(!isr_reg))
589 goto exit;
590
Tony Lindgren92105bb2005-09-07 17:20:26 +0100591 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100592 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700593 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100594
Imre Deakea6dedd2006-06-26 16:16:00 -0700595 enabled = _get_gpio_irqbank_mask(bank);
596 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100597
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530598 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800599 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100600
601 /* clear edge sensitive interrupts before handler(s) are
602 called so that we don't miss any interrupt occurred while
603 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700604 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100605 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700606 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100607
608 /* if there is only edge sensitive GPIO pin interrupts
609 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700610 if (!level_mask && !unmasked) {
611 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000612 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700613 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100614
Imre Deakea6dedd2006-06-26 16:16:00 -0700615 isr |= retrigger;
616 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100617 if (!isr)
618 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100619
Tony Lindgren92105bb2005-09-07 17:20:26 +0100620 gpio_irq = bank->virtual_irq_start;
621 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700622 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800623
Tony Lindgren92105bb2005-09-07 17:20:26 +0100624 if (!(isr & 1))
625 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200626
Cory Maccarrone4318f362010-01-08 10:29:04 -0800627 /*
628 * Some chips can't respond to both rising and falling
629 * at the same time. If this irq was requested with
630 * both flags, we need to flip the ICR data for the IRQ
631 * to respond to the IRQ for the opposite direction.
632 * This will be indicated in the bank toggle_mask.
633 */
634 if (bank->toggle_mask & (1 << gpio_index))
635 _toggle_gpio_edge_triggering(bank, gpio_index);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800636
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100637 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100638 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000639 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700640 /* if bank has any level sensitive GPIO pin interrupt
641 configured, we must unmask the bank interrupt only after
642 handler(s) are executed in order to avoid spurious bank
643 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800644exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700645 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000646 chained_irq_exit(chip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530647 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100648}
649
Lennert Buytenheke9191022010-11-29 11:17:17 +0100650static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300651{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100652 unsigned int gpio = d->irq - IH_GPIO_BASE;
653 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700654 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300655
Colin Cross85ec7b92011-06-06 13:38:18 -0700656 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300657 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700658 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300659}
660
Lennert Buytenheke9191022010-11-29 11:17:17 +0100661static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100662{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100663 unsigned int gpio = d->irq - IH_GPIO_BASE;
664 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100665
666 _clear_gpio_irqstatus(bank, gpio);
667}
668
Lennert Buytenheke9191022010-11-29 11:17:17 +0100669static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100671 unsigned int gpio = d->irq - IH_GPIO_BASE;
672 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700673 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100674
Colin Cross85ec7b92011-06-06 13:38:18 -0700675 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100676 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700677 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700678 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100679}
680
Lennert Buytenheke9191022010-11-29 11:17:17 +0100681static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100682{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100683 unsigned int gpio = d->irq - IH_GPIO_BASE;
684 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700685 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100686 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700687 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700688
Colin Cross85ec7b92011-06-06 13:38:18 -0700689 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700690 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700691 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800692
693 /* For level-triggered GPIOs, the clearing must be done after
694 * the HW source is cleared, thus after the handler has run */
695 if (bank->level_mask & irq_mask) {
696 _set_gpio_irqenable(bank, gpio, 0);
697 _clear_gpio_irqstatus(bank, gpio);
698 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100699
Kevin Hilman4de8c752008-01-16 21:56:14 -0800700 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700701 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702}
703
David Brownelle5c56ed2006-12-06 17:13:59 -0800704static struct irq_chip gpio_irq_chip = {
705 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100706 .irq_shutdown = gpio_irq_shutdown,
707 .irq_ack = gpio_ack_irq,
708 .irq_mask = gpio_mask_irq,
709 .irq_unmask = gpio_unmask_irq,
710 .irq_set_type = gpio_irq_type,
711 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800712};
713
714/*---------------------------------------------------------------------*/
715
Magnus Damm79ee0312009-07-08 13:22:04 +0200716static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800717{
Magnus Damm79ee0312009-07-08 13:22:04 +0200718 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800719 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800720 void __iomem *mask_reg = bank->base +
721 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800722 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800723
David Brownella6472532008-03-03 04:33:30 -0800724 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800725 bank->saved_wakeup = __raw_readl(mask_reg);
726 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800727 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800728
729 return 0;
730}
731
Magnus Damm79ee0312009-07-08 13:22:04 +0200732static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800733{
Magnus Damm79ee0312009-07-08 13:22:04 +0200734 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800735 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800736 void __iomem *mask_reg = bank->base +
737 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800738 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800739
David Brownella6472532008-03-03 04:33:30 -0800740 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800741 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800742 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800743
744 return 0;
745}
746
Alexey Dobriyan47145212009-12-14 18:00:08 -0800747static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200748 .suspend_noirq = omap_mpuio_suspend_noirq,
749 .resume_noirq = omap_mpuio_resume_noirq,
750};
751
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200752/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800753static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800754 .driver = {
755 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200756 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800757 },
758};
759
760static struct platform_device omap_mpuio_device = {
761 .name = "mpuio",
762 .id = -1,
763 .dev = {
764 .driver = &omap_mpuio_driver.driver,
765 }
766 /* could list the /proc/iomem resources */
767};
768
Charulatha V03e128c2011-05-05 19:58:01 +0530769static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800770{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800771 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700772
David Brownell11a78b72006-12-06 17:14:11 -0800773 if (platform_driver_register(&omap_mpuio_driver) == 0)
774 (void) platform_device_register(&omap_mpuio_device);
775}
776
David Brownelle5c56ed2006-12-06 17:13:59 -0800777/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100778
David Brownell52e31342008-03-03 12:43:23 -0800779static int gpio_input(struct gpio_chip *chip, unsigned offset)
780{
781 struct gpio_bank *bank;
782 unsigned long flags;
783
784 bank = container_of(chip, struct gpio_bank, chip);
785 spin_lock_irqsave(&bank->lock, flags);
786 _set_gpio_direction(bank, offset, 1);
787 spin_unlock_irqrestore(&bank->lock, flags);
788 return 0;
789}
790
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300791static int gpio_is_input(struct gpio_bank *bank, int mask)
792{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700793 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300794
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300795 return __raw_readl(reg) & mask;
796}
797
David Brownell52e31342008-03-03 12:43:23 -0800798static int gpio_get(struct gpio_chip *chip, unsigned offset)
799{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300800 struct gpio_bank *bank;
801 void __iomem *reg;
802 int gpio;
803 u32 mask;
804
805 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530806 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300807 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700808 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300809
810 if (gpio_is_input(bank, mask))
811 return _get_gpio_datain(bank, gpio);
812 else
813 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800814}
815
816static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
817{
818 struct gpio_bank *bank;
819 unsigned long flags;
820
821 bank = container_of(chip, struct gpio_bank, chip);
822 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700823 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800824 _set_gpio_direction(bank, offset, 0);
825 spin_unlock_irqrestore(&bank->lock, flags);
826 return 0;
827}
828
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700829static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
830 unsigned debounce)
831{
832 struct gpio_bank *bank;
833 unsigned long flags;
834
835 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800836
837 if (!bank->dbck) {
838 bank->dbck = clk_get(bank->dev, "dbclk");
839 if (IS_ERR(bank->dbck))
840 dev_err(bank->dev, "Could not get gpio dbck\n");
841 }
842
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700843 spin_lock_irqsave(&bank->lock, flags);
844 _set_gpio_debounce(bank, offset, debounce);
845 spin_unlock_irqrestore(&bank->lock, flags);
846
847 return 0;
848}
849
David Brownell52e31342008-03-03 12:43:23 -0800850static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
851{
852 struct gpio_bank *bank;
853 unsigned long flags;
854
855 bank = container_of(chip, struct gpio_bank, chip);
856 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700857 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800858 spin_unlock_irqrestore(&bank->lock, flags);
859}
860
David Brownella007b702008-12-10 17:35:25 -0800861static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
862{
863 struct gpio_bank *bank;
864
865 bank = container_of(chip, struct gpio_bank, chip);
866 return bank->virtual_irq_start + offset;
867}
868
David Brownell52e31342008-03-03 12:43:23 -0800869/*---------------------------------------------------------------------*/
870
Tony Lindgren9a748052010-12-07 16:26:56 -0800871static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700872{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700873 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700874 u32 rev;
875
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700876 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700877 return;
878
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700879 rev = __raw_readw(bank->base + bank->regs->revision);
880 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700881 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700882
883 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700884}
885
David Brownell8ba55c52008-02-26 11:10:50 -0800886/* This lock class tells lockdep that GPIO irqs are in a different
887 * category than their parents, so it won't report false recursion.
888 */
889static struct lock_class_key gpio_lock_class;
890
Charulatha V03e128c2011-05-05 19:58:01 +0530891static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800892{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530893 void __iomem *base = bank->base;
894 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800895
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530896 if (bank->width == 16)
897 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800898
Charulatha Vd0d665a2011-08-31 00:02:21 +0530899 if (bank->is_mpuio) {
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530900 __raw_writel(l, bank->base + bank->regs->irqenable);
901 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800902 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530903
904 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->irqenable_inv);
905 _gpio_rmw(base, bank->regs->irqstatus, l,
906 bank->regs->irqenable_inv == false);
907 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->debounce_en != 0);
908 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->ctrl != 0);
909 if (bank->regs->debounce_en)
910 _gpio_rmw(base, bank->regs->debounce_en, 0, 1);
911
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +0530912 /* Save OE default value (0xffffffff) in the context */
913 bank->context.oe = __raw_readl(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530914 /* Initialize interface clk ungated, module enabled */
915 if (bank->regs->ctrl)
916 _gpio_rmw(base, bank->regs->ctrl, 0, 1);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800917}
918
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700919static __init void
920omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
921 unsigned int num)
922{
923 struct irq_chip_generic *gc;
924 struct irq_chip_type *ct;
925
926 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
927 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -0700928 if (!gc) {
929 dev_err(bank->dev, "Memory alloc failed for gc\n");
930 return;
931 }
932
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700933 ct = gc->chip_types;
934
935 /* NOTE: No ack required, reading IRQ status clears it. */
936 ct->chip.irq_mask = irq_gc_mask_set_bit;
937 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
938 ct->chip.irq_set_type = gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530939
940 if (bank->regs->wkup_en)
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700941 ct->chip.irq_set_wake = gpio_wake_enable,
942
943 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
944 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
945 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
946}
947
Russell Kingd52b31d2011-05-27 13:56:12 -0700948static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800949{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800950 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800951 static int gpio;
952
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800953 /*
954 * REVISIT eventually switch from OMAP-specific gpio structs
955 * over to the generic ones
956 */
957 bank->chip.request = omap_gpio_request;
958 bank->chip.free = omap_gpio_free;
959 bank->chip.direction_input = gpio_input;
960 bank->chip.get = gpio_get;
961 bank->chip.direction_output = gpio_output;
962 bank->chip.set_debounce = gpio_debounce;
963 bank->chip.set = gpio_set;
964 bank->chip.to_irq = gpio_2irq;
Charulatha Vd0d665a2011-08-31 00:02:21 +0530965 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800966 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530967 if (bank->regs->wkup_en)
968 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800969 bank->chip.base = OMAP_MPUIO(0);
970 } else {
971 bank->chip.label = "gpio";
972 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -0700973 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800974 }
Kevin Hilmand5f46242011-04-21 09:23:00 -0700975 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800976
977 gpiochip_add(&bank->chip);
978
979 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -0700980 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +0100981 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100982 irq_set_chip_data(j, bank);
Charulatha Vd0d665a2011-08-31 00:02:21 +0530983 if (bank->is_mpuio) {
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700984 omap_mpuio_alloc_gc(bank, j, bank->width);
985 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100986 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700987 irq_set_handler(j, handle_simple_irq);
988 set_irq_flags(j, IRQF_VALID);
989 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800990 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100991 irq_set_chained_handler(bank->irq, gpio_irq_handler);
992 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800993}
994
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800995static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100996{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800997 struct omap_gpio_platform_data *pdata;
998 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100999 struct gpio_bank *bank;
Charulatha V03e128c2011-05-05 19:58:01 +05301000 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001001
Charulatha V03e128c2011-05-05 19:58:01 +05301002 if (!pdev->dev.platform_data) {
1003 ret = -EINVAL;
1004 goto err_exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001005 }
1006
Charulatha V03e128c2011-05-05 19:58:01 +05301007 bank = kzalloc(sizeof(struct gpio_bank), GFP_KERNEL);
1008 if (!bank) {
1009 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1010 ret = -ENOMEM;
1011 goto err_exit;
1012 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001013
1014 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1015 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301016 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n",
1017 pdev->id);
1018 ret = -ENODEV;
1019 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001020 }
1021
1022 bank->irq = res->start;
Charulatha V03e128c2011-05-05 19:58:01 +05301023 bank->id = pdev->id;
1024
1025 pdata = pdev->dev.platform_data;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001026 bank->virtual_irq_start = pdata->virtual_irq_start;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001027 bank->dev = &pdev->dev;
1028 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001029 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001030 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301031 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301032 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Charulatha V0cde8d02011-05-05 20:15:16 +05301033 bank->loses_context = pdata->loses_context;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301034 bank->get_context_loss_count = pdata->get_context_loss_count;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001035 bank->regs = pdata->regs;
1036
1037 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1038 bank->set_dataout = _set_gpio_dataout_reg;
1039 else
1040 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001041
1042 spin_lock_init(&bank->lock);
1043
1044 /* Static mapping, never released */
1045 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1046 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301047 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n",
1048 pdev->id);
1049 ret = -ENODEV;
1050 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001051 }
1052
1053 bank->base = ioremap(res->start, resource_size(res));
1054 if (!bank->base) {
Charulatha V03e128c2011-05-05 19:58:01 +05301055 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n",
1056 pdev->id);
1057 ret = -ENOMEM;
1058 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001059 }
1060
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301061 platform_set_drvdata(pdev, bank);
1062
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001063 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301064 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001065 pm_runtime_get_sync(bank->dev);
1066
Charulatha Vd0d665a2011-08-31 00:02:21 +05301067 if (bank->is_mpuio)
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301068 mpuio_init(bank);
1069
Charulatha V03e128c2011-05-05 19:58:01 +05301070 omap_gpio_mod_init(bank);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001071 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001072 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001073
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301074 pm_runtime_put(bank->dev);
1075
Charulatha V03e128c2011-05-05 19:58:01 +05301076 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001077
Charulatha V03e128c2011-05-05 19:58:01 +05301078 return ret;
1079
1080err_free:
1081 kfree(bank);
1082err_exit:
1083 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001084}
1085
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301086#ifdef CONFIG_ARCH_OMAP2PLUS
1087
1088#if defined(CONFIG_PM_SLEEP)
1089static int omap_gpio_suspend(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001090{
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301091 struct platform_device *pdev = to_platform_device(dev);
1092 struct gpio_bank *bank = platform_get_drvdata(pdev);
1093 void __iomem *base = bank->base;
1094 void __iomem *wakeup_enable;
1095 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001096
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301097 if (!bank->mod_usage || !bank->loses_context)
1098 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001099
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301100 if (!bank->regs->wkup_en || !bank->suspend_wakeup)
1101 return 0;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301102
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301103 wakeup_enable = bank->base + bank->regs->wkup_en;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001104
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301105 spin_lock_irqsave(&bank->lock, flags);
1106 bank->saved_wakeup = __raw_readl(wakeup_enable);
1107 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1108 _gpio_rmw(base, bank->regs->wkup_en, bank->suspend_wakeup, 1);
1109 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001110
1111 return 0;
1112}
1113
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301114static int omap_gpio_resume(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001115{
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301116 struct platform_device *pdev = to_platform_device(dev);
1117 struct gpio_bank *bank = platform_get_drvdata(pdev);
1118 void __iomem *base = bank->base;
1119 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001120
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301121 if (!bank->mod_usage || !bank->loses_context)
1122 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001123
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301124 if (!bank->regs->wkup_en || !bank->saved_wakeup)
1125 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001126
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301127 spin_lock_irqsave(&bank->lock, flags);
1128 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1129 _gpio_rmw(base, bank->regs->wkup_en, bank->saved_wakeup, 1);
1130 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301131
1132 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001133}
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301134#endif /* CONFIG_PM_SLEEP */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001135
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301136#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301137static void omap_gpio_save_context(struct gpio_bank *bank);
1138static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001139
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301140static int omap_gpio_runtime_suspend(struct device *dev)
1141{
1142 struct platform_device *pdev = to_platform_device(dev);
1143 struct gpio_bank *bank = platform_get_drvdata(pdev);
1144 u32 l1 = 0, l2 = 0;
1145 unsigned long flags;
1146
1147 spin_lock_irqsave(&bank->lock, flags);
1148 if (bank->power_mode != OFF_MODE) {
1149 bank->power_mode = 0;
1150 goto save_gpio_context;
1151 }
1152 /*
1153 * If going to OFF, remove triggering for all
1154 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1155 * generated. See OMAP2420 Errata item 1.101.
1156 */
1157 if (!(bank->enabled_non_wakeup_gpios))
1158 goto save_gpio_context;
1159
1160 bank->saved_datain = __raw_readl(bank->base +
1161 bank->regs->datain);
1162 l1 = __raw_readl(bank->base + bank->regs->fallingdetect);
1163 l2 = __raw_readl(bank->base + bank->regs->risingdetect);
1164
1165 bank->saved_fallingdetect = l1;
1166 bank->saved_risingdetect = l2;
1167 l1 &= ~bank->enabled_non_wakeup_gpios;
1168 l2 &= ~bank->enabled_non_wakeup_gpios;
1169
1170 __raw_writel(l1, bank->base + bank->regs->fallingdetect);
1171 __raw_writel(l2, bank->base + bank->regs->risingdetect);
1172
1173 bank->workaround_enabled = true;
1174
1175save_gpio_context:
1176 if (bank->get_context_loss_count)
1177 bank->context_loss_count =
1178 bank->get_context_loss_count(bank->dev);
1179
1180 omap_gpio_save_context(bank);
1181 spin_unlock_irqrestore(&bank->lock, flags);
1182
1183 return 0;
1184}
1185
1186static int omap_gpio_runtime_resume(struct device *dev)
1187{
1188 struct platform_device *pdev = to_platform_device(dev);
1189 struct gpio_bank *bank = platform_get_drvdata(pdev);
1190 int context_lost_cnt_after;
1191 u32 l = 0, gen, gen0, gen1;
1192 unsigned long flags;
1193
1194 spin_lock_irqsave(&bank->lock, flags);
1195 if (!bank->enabled_non_wakeup_gpios || !bank->workaround_enabled) {
1196 spin_unlock_irqrestore(&bank->lock, flags);
1197 return 0;
1198 }
1199
1200 if (bank->get_context_loss_count) {
1201 context_lost_cnt_after =
1202 bank->get_context_loss_count(bank->dev);
1203 if (context_lost_cnt_after != bank->context_loss_count ||
1204 !context_lost_cnt_after) {
1205 omap_gpio_restore_context(bank);
1206 } else {
1207 spin_unlock_irqrestore(&bank->lock, flags);
1208 return 0;
1209 }
1210 }
1211
1212 __raw_writel(bank->saved_fallingdetect,
1213 bank->base + bank->regs->fallingdetect);
1214 __raw_writel(bank->saved_risingdetect,
1215 bank->base + bank->regs->risingdetect);
1216 l = __raw_readl(bank->base + bank->regs->datain);
1217
1218 /*
1219 * Check if any of the non-wakeup interrupt GPIOs have changed
1220 * state. If so, generate an IRQ by software. This is
1221 * horribly racy, but it's the best we can do to work around
1222 * this silicon bug.
1223 */
1224 l ^= bank->saved_datain;
1225 l &= bank->enabled_non_wakeup_gpios;
1226
1227 /*
1228 * No need to generate IRQs for the rising edge for gpio IRQs
1229 * configured with falling edge only; and vice versa.
1230 */
1231 gen0 = l & bank->saved_fallingdetect;
1232 gen0 &= bank->saved_datain;
1233
1234 gen1 = l & bank->saved_risingdetect;
1235 gen1 &= ~(bank->saved_datain);
1236
1237 /* FIXME: Consider GPIO IRQs with level detections properly! */
1238 gen = l & (~(bank->saved_fallingdetect) & ~(bank->saved_risingdetect));
1239 /* Consider all GPIO IRQs needed to be updated */
1240 gen |= gen0 | gen1;
1241
1242 if (gen) {
1243 u32 old0, old1;
1244
1245 old0 = __raw_readl(bank->base + bank->regs->leveldetect0);
1246 old1 = __raw_readl(bank->base + bank->regs->leveldetect1);
1247
1248 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1249 __raw_writel(old0 | gen, bank->base +
1250 bank->regs->leveldetect0);
1251 __raw_writel(old1 | gen, bank->base +
1252 bank->regs->leveldetect1);
1253 }
1254
1255 if (cpu_is_omap44xx()) {
1256 __raw_writel(old0 | l, bank->base +
1257 bank->regs->leveldetect0);
1258 __raw_writel(old1 | l, bank->base +
1259 bank->regs->leveldetect1);
1260 }
1261 __raw_writel(old0, bank->base + bank->regs->leveldetect0);
1262 __raw_writel(old1, bank->base + bank->regs->leveldetect1);
1263 }
1264
1265 bank->workaround_enabled = false;
1266 spin_unlock_irqrestore(&bank->lock, flags);
1267
1268 return 0;
1269}
1270#endif /* CONFIG_PM_RUNTIME */
1271
1272void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001273{
Charulatha V03e128c2011-05-05 19:58:01 +05301274 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001275
Charulatha V03e128c2011-05-05 19:58:01 +05301276 list_for_each_entry(bank, &omap_gpio_list, node) {
Kevin Hilman0aed04352010-09-22 16:06:27 -07001277 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001278
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301279 if (!bank->mod_usage || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301280 continue;
1281
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301282 bank->power_mode = pwr_mode;
1283
Kevin Hilman0aed04352010-09-22 16:06:27 -07001284 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001285 clk_disable(bank->dbck);
1286
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301287 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001288 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001289}
1290
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001291void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001292{
Charulatha V03e128c2011-05-05 19:58:01 +05301293 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001294
Charulatha V03e128c2011-05-05 19:58:01 +05301295 list_for_each_entry(bank, &omap_gpio_list, node) {
Kevin Hilman0aed04352010-09-22 16:06:27 -07001296 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001297
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301298 if (!bank->mod_usage || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301299 continue;
1300
Kevin Hilman0aed04352010-09-22 16:06:27 -07001301 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001302 clk_enable(bank->dbck);
1303
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301304 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001305 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001306}
1307
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301308#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301309static void omap_gpio_save_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301310{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301311 bank->context.irqenable1 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301312 __raw_readl(bank->base + bank->regs->irqenable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301313 bank->context.irqenable2 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301314 __raw_readl(bank->base + bank->regs->irqenable2);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301315 bank->context.wake_en =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301316 __raw_readl(bank->base + bank->regs->wkup_en);
1317 bank->context.ctrl = __raw_readl(bank->base + bank->regs->ctrl);
1318 bank->context.oe = __raw_readl(bank->base + bank->regs->direction);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301319 bank->context.leveldetect0 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301320 __raw_readl(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301321 bank->context.leveldetect1 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301322 __raw_readl(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301323 bank->context.risingdetect =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301324 __raw_readl(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301325 bank->context.fallingdetect =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301326 __raw_readl(bank->base + bank->regs->fallingdetect);
1327 bank->context.dataout = __raw_readl(bank->base + bank->regs->dataout);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301328}
1329
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301330static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301331{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301332 __raw_writel(bank->context.irqenable1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301333 bank->base + bank->regs->irqenable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301334 __raw_writel(bank->context.irqenable2,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301335 bank->base + bank->regs->irqenable2);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301336 __raw_writel(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301337 bank->base + bank->regs->wkup_en);
1338 __raw_writel(bank->context.ctrl, bank->base + bank->regs->ctrl);
1339 __raw_writel(bank->context.oe, bank->base + bank->regs->direction);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301340 __raw_writel(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301341 bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301342 __raw_writel(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301343 bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301344 __raw_writel(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301345 bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301346 __raw_writel(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301347 bank->base + bank->regs->fallingdetect);
1348 __raw_writel(bank->context.dataout, bank->base + bank->regs->dataout);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301349}
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301350#endif /* CONFIG_PM_RUNTIME */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301351#else
1352#define omap_gpio_suspend NULL
1353#define omap_gpio_resume NULL
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301354#define omap_gpio_runtime_suspend NULL
1355#define omap_gpio_runtime_resume NULL
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301356#endif
1357
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301358static const struct dev_pm_ops gpio_pm_ops = {
1359 SET_SYSTEM_SLEEP_PM_OPS(omap_gpio_suspend, omap_gpio_resume)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301360 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1361 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301362};
1363
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001364static struct platform_driver omap_gpio_driver = {
1365 .probe = omap_gpio_probe,
1366 .driver = {
1367 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301368 .pm = &gpio_pm_ops,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001369 },
1370};
1371
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001372/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001373 * gpio driver register needs to be done before
1374 * machine_init functions access gpio APIs.
1375 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001376 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001377static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001378{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001379 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001380}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001381postcore_initcall(omap_gpio_drv_reg);