blob: 7e1e50c23f935af55cef938f127d378d4f2eebd2 [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
Charulatha V03e128c2011-05-05 19:58:01 +053032static LIST_HEAD(omap_gpio_list);
33
Charulatha V6d62e212011-04-18 15:06:51 +000034struct gpio_regs {
35 u32 irqenable1;
36 u32 irqenable2;
37 u32 wake_en;
38 u32 ctrl;
39 u32 oe;
40 u32 leveldetect0;
41 u32 leveldetect1;
42 u32 risingdetect;
43 u32 fallingdetect;
44 u32 dataout;
45};
46
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010047struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053048 struct list_head node;
Tony Lindgren9f7065d2009-10-19 15:25:20 -070049 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010050 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010051 u16 irq;
52 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010053 u32 suspend_wakeup;
54 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080055 u32 non_wakeup_gpios;
56 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000057 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080058 u32 saved_datain;
59 u32 saved_fallingdetect;
60 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080061 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080062 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010063 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080064 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080065 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080066 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080067 u32 dbck_enable_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080068 struct device *dev;
Charulatha Vd0d665a2011-08-31 00:02:21 +053069 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080070 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053071 bool loses_context;
Tony Lindgren5de62b82010-12-07 16:26:58 -080072 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070073 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053074 int context_loss_count;
Charulatha V03e128c2011-05-05 19:58:01 +053075 u16 id;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070076
77 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053078 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070079
80 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010081};
82
Kevin Hilman129fd222011-04-22 07:59:07 -070083#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
84#define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
Charulatha Vc8eef652011-05-02 15:21:42 +053085#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010086
87static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
88{
Tony Lindgren92105bb2005-09-07 17:20:26 +010089 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010090 u32 l;
91
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070092 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010093 l = __raw_readl(reg);
94 if (is_input)
95 l |= 1 << gpio;
96 else
97 l &= ~(1 << gpio);
98 __raw_writel(l, reg);
99}
100
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700101
102/* set data out value using dedicate set/clear register */
103static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100104{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100105 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700106 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100107
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700108 if (enable)
109 reg += bank->regs->set_dataout;
110 else
111 reg += bank->regs->clr_dataout;
112
113 __raw_writel(l, reg);
114}
115
116/* set data out value using mask register */
117static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
118{
119 void __iomem *reg = bank->base + bank->regs->dataout;
120 u32 gpio_bit = GPIO_BIT(bank, gpio);
121 u32 l;
122
123 l = __raw_readl(reg);
124 if (enable)
125 l |= gpio_bit;
126 else
127 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100128 __raw_writel(l, reg);
129}
130
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300131static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700133 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700135 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136}
137
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300138static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
139{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700140 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300141
Kevin Hilman129fd222011-04-22 07:59:07 -0700142 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300143}
144
Kevin Hilmanece95282011-07-12 08:18:15 -0700145static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
146{
147 int l = __raw_readl(base + reg);
148
149 if (set)
150 l |= mask;
151 else
152 l &= ~mask;
153
154 __raw_writel(l, base + reg);
155}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100156
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700157/**
158 * _set_gpio_debounce - low level gpio debounce time
159 * @bank: the gpio bank we're acting upon
160 * @gpio: the gpio number on this @gpio
161 * @debounce: debounce time to use
162 *
163 * OMAP's debounce time is in 31us steps so we need
164 * to convert and round up to the closest unit.
165 */
166static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
167 unsigned debounce)
168{
Kevin Hilman9942da02011-04-22 12:02:05 -0700169 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700170 u32 val;
171 u32 l;
172
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800173 if (!bank->dbck_flag)
174 return;
175
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700176 if (debounce < 32)
177 debounce = 0x01;
178 else if (debounce > 7936)
179 debounce = 0xff;
180 else
181 debounce = (debounce / 0x1f) - 1;
182
Kevin Hilman129fd222011-04-22 07:59:07 -0700183 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700184
Kevin Hilman9942da02011-04-22 12:02:05 -0700185 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700186 __raw_writel(debounce, reg);
187
Kevin Hilman9942da02011-04-22 12:02:05 -0700188 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700189 val = __raw_readl(reg);
190
191 if (debounce) {
192 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800193 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700194 } else {
195 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800196 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700197 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300198 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700199
200 __raw_writel(val, reg);
201}
202
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530203static inline void set_gpio_trigger(struct gpio_bank *bank, int gpio,
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700204 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100205{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800206 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100207 u32 gpio_bit = 1 << gpio;
208
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530209 _gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
210 trigger & IRQ_TYPE_LEVEL_LOW);
211 _gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
212 trigger & IRQ_TYPE_LEVEL_HIGH);
213 _gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
214 trigger & IRQ_TYPE_EDGE_RISING);
215 _gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
216 trigger & IRQ_TYPE_EDGE_FALLING);
217
218 if (likely(!(bank->non_wakeup_gpios & gpio_bit)))
219 _gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
220
Ambresh K55b220c2011-06-15 13:40:45 -0700221 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530222 if (!bank->regs->irqctrl) {
223 /* On omap24xx proceed only when valid GPIO bit is set */
224 if (bank->non_wakeup_gpios) {
225 if (!(bank->non_wakeup_gpios & gpio_bit))
226 goto exit;
227 }
228
Chunqiu Wang699117a2009-06-24 17:13:39 +0000229 /*
230 * Log the edge gpio and manually trigger the IRQ
231 * after resume if the input level changes
232 * to avoid irq lost during PER RET/OFF mode
233 * Applies for omap2 non-wakeup gpio and all omap3 gpios
234 */
235 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800236 bank->enabled_non_wakeup_gpios |= gpio_bit;
237 else
238 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
239 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700240
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530241exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530242 bank->level_mask =
243 __raw_readl(bank->base + bank->regs->leveldetect0) |
244 __raw_readl(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100245}
246
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800247#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800248/*
249 * This only applies to chips that can't do both rising and falling edge
250 * detection at once. For all other chips, this function is a noop.
251 */
252static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
253{
254 void __iomem *reg = bank->base;
255 u32 l = 0;
256
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530257 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800258 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530259
260 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800261
262 l = __raw_readl(reg);
263 if ((l >> gpio) & 1)
264 l &= ~(1 << gpio);
265 else
266 l |= 1 << gpio;
267
268 __raw_writel(l, reg);
269}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530270#else
271static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800272#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800273
Tony Lindgren92105bb2005-09-07 17:20:26 +0100274static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
275{
276 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530277 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100278 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100279
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530280 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
281 set_gpio_trigger(bank, gpio, trigger);
282 } else if (bank->regs->irqctrl) {
283 reg += bank->regs->irqctrl;
284
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100285 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000286 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800287 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100288 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100289 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100290 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100291 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100292 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530293 return -EINVAL;
294
295 __raw_writel(l, reg);
296 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100297 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530298 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100299 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530300 reg += bank->regs->edgectrl1;
301
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100302 gpio &= 0x07;
303 l = __raw_readl(reg);
304 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100305 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100306 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100307 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100308 l |= 1 << (gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530309
310 /* Enable wake-up during idle for dynamic tick */
311 _gpio_rmw(base, bank->regs->wkup_en, 1 << gpio, trigger);
312 __raw_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100313 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100314 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100315}
316
Lennert Buytenheke9191022010-11-29 11:17:17 +0100317static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100318{
319 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100320 unsigned gpio;
321 int retval;
David Brownella6472532008-03-03 04:33:30 -0800322 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100323
Lennert Buytenheke9191022010-11-29 11:17:17 +0100324 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
325 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100326 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100327 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100328
David Brownelle5c56ed2006-12-06 17:13:59 -0800329 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100330 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800331
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530332 bank = irq_data_get_irq_chip_data(d);
333
334 if (!bank->regs->leveldetect0 &&
335 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100336 return -EINVAL;
337
David Brownella6472532008-03-03 04:33:30 -0800338 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700339 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800340 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800341
342 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100343 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800344 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100345 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800346
Tony Lindgren92105bb2005-09-07 17:20:26 +0100347 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348}
349
350static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
351{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100352 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100353
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700354 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100355 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300356
357 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700358 if (bank->regs->irqstatus2) {
359 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700360 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700361 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700362
363 /* Flush posted write for the irq status to avoid spurious interrupts */
364 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100365}
366
367static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
368{
Kevin Hilman129fd222011-04-22 07:59:07 -0700369 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100370}
371
Imre Deakea6dedd2006-06-26 16:16:00 -0700372static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
373{
374 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700375 u32 l;
Kevin Hilmanc390aad2011-04-21 09:33:36 -0700376 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700377
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700378 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700379 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700380 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700381 l = ~l;
382 l &= mask;
383 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700384}
385
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700386static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100387{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100388 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100389 u32 l;
390
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700391 if (bank->regs->set_irqenable) {
392 reg += bank->regs->set_irqenable;
393 l = gpio_mask;
394 } else {
395 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100396 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700397 if (bank->regs->irqenable_inv)
398 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100399 else
400 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100401 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700402
403 __raw_writel(l, reg);
404}
405
406static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
407{
408 void __iomem *reg = bank->base;
409 u32 l;
410
411 if (bank->regs->clr_irqenable) {
412 reg += bank->regs->clr_irqenable;
413 l = gpio_mask;
414 } else {
415 reg += bank->regs->irqenable;
416 l = __raw_readl(reg);
417 if (bank->regs->irqenable_inv)
418 l |= gpio_mask;
419 else
420 l &= ~gpio_mask;
421 }
422
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100423 __raw_writel(l, reg);
424}
425
426static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
427{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700428 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100429}
430
Tony Lindgren92105bb2005-09-07 17:20:26 +0100431/*
432 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
433 * 1510 does not seem to have a wake-up register. If JTAG is connected
434 * to the target, system will wake up always on GPIO events. While
435 * system is running all registered GPIO interrupts need to have wake-up
436 * enabled. When system is suspended, only selected GPIO interrupts need
437 * to have wake-up enabled.
438 */
439static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
440{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700441 u32 gpio_bit = GPIO_BIT(bank, gpio);
442 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800443
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700444 if (bank->non_wakeup_gpios & gpio_bit) {
445 dev_err(bank->dev,
446 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100447 return -EINVAL;
448 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700449
450 spin_lock_irqsave(&bank->lock, flags);
451 if (enable)
452 bank->suspend_wakeup |= gpio_bit;
453 else
454 bank->suspend_wakeup &= ~gpio_bit;
455
456 spin_unlock_irqrestore(&bank->lock, flags);
457
458 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100459}
460
Tony Lindgren4196dd62006-09-25 12:41:38 +0300461static void _reset_gpio(struct gpio_bank *bank, int gpio)
462{
Kevin Hilman129fd222011-04-22 07:59:07 -0700463 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300464 _set_gpio_irqenable(bank, gpio, 0);
465 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700466 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300467}
468
Tony Lindgren92105bb2005-09-07 17:20:26 +0100469/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100470static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100471{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100472 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100473 struct gpio_bank *bank;
474 int retval;
475
Lennert Buytenheke9191022010-11-29 11:17:17 +0100476 bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700477 retval = _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100478
479 return retval;
480}
481
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800482static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100483{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800484 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800485 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100486
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530487 /*
488 * If this is the first gpio_request for the bank,
489 * enable the bank module.
490 */
491 if (!bank->mod_usage)
492 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100493
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530494 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300495 /* Set trigger to none. You need to enable the desired trigger with
496 * request_irq() or set_irq_type().
497 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800498 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100499
Charulatha Vfad96ea2011-05-25 11:23:50 +0530500 if (bank->regs->pinctrl) {
501 void __iomem *reg = bank->base + bank->regs->pinctrl;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100502
Tony Lindgren92105bb2005-09-07 17:20:26 +0100503 /* Claim the pin for MPU */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800504 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100505 }
Charulatha Vfad96ea2011-05-25 11:23:50 +0530506
Charulatha Vc8eef652011-05-02 15:21:42 +0530507 if (bank->regs->ctrl && !bank->mod_usage) {
508 void __iomem *reg = bank->base + bank->regs->ctrl;
509 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700510
Charulatha Vc8eef652011-05-02 15:21:42 +0530511 ctrl = __raw_readl(reg);
512 /* Module is enabled, clocks are not gated */
513 ctrl &= ~GPIO_MOD_CTRL_BIT;
514 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800515 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530516
517 bank->mod_usage |= 1 << offset;
518
David Brownella6472532008-03-03 04:33:30 -0800519 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100520
521 return 0;
522}
523
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800524static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100525{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800526 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530527 void __iomem *base = bank->base;
David Brownella6472532008-03-03 04:33:30 -0800528 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529
David Brownella6472532008-03-03 04:33:30 -0800530 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530531
532 if (bank->regs->wkup_en)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100533 /* Disable wake-up during idle for dynamic tick */
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530534 _gpio_rmw(base, bank->regs->wkup_en, 1 << offset, 0);
535
Charulatha Vc8eef652011-05-02 15:21:42 +0530536 bank->mod_usage &= ~(1 << offset);
Charulatha V9f096862010-05-14 12:05:27 -0700537
Charulatha Vc8eef652011-05-02 15:21:42 +0530538 if (bank->regs->ctrl && !bank->mod_usage) {
539 void __iomem *reg = bank->base + bank->regs->ctrl;
540 u32 ctrl;
541
542 ctrl = __raw_readl(reg);
543 /* Module is disabled, clocks are gated */
544 ctrl |= GPIO_MOD_CTRL_BIT;
545 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800546 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530547
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800548 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800549 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530550
551 /*
552 * If this is the last gpio to be freed in the bank,
553 * disable the bank module.
554 */
555 if (!bank->mod_usage)
556 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100557}
558
559/*
560 * We need to unmask the GPIO bank interrupt as soon as possible to
561 * avoid missing GPIO interrupts for other lines in the bank.
562 * Then we need to mask-read-clear-unmask the triggered GPIO lines
563 * in the bank to avoid missing nested interrupts for a GPIO line.
564 * If we wait to unmask individual GPIO lines in the bank after the
565 * line's interrupt handler has been run, we may miss some nested
566 * interrupts.
567 */
Russell King10dd5ce2006-11-23 11:41:32 +0000568static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100569{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100570 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100571 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800572 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700574 u32 retrigger = 0;
575 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000576 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100577
Will Deaconee144182011-02-21 13:46:08 +0000578 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100579
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100580 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700581 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530582 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800583
584 if (WARN_ON(!isr_reg))
585 goto exit;
586
Tony Lindgren92105bb2005-09-07 17:20:26 +0100587 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100588 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700589 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100590
Imre Deakea6dedd2006-06-26 16:16:00 -0700591 enabled = _get_gpio_irqbank_mask(bank);
592 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100593
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530594 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800595 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100596
597 /* clear edge sensitive interrupts before handler(s) are
598 called so that we don't miss any interrupt occurred while
599 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700600 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100601 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700602 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100603
604 /* if there is only edge sensitive GPIO pin interrupts
605 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700606 if (!level_mask && !unmasked) {
607 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000608 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700609 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100610
Imre Deakea6dedd2006-06-26 16:16:00 -0700611 isr |= retrigger;
612 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100613 if (!isr)
614 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100615
Tony Lindgren92105bb2005-09-07 17:20:26 +0100616 gpio_irq = bank->virtual_irq_start;
617 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700618 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800619
Tony Lindgren92105bb2005-09-07 17:20:26 +0100620 if (!(isr & 1))
621 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200622
Cory Maccarrone4318f362010-01-08 10:29:04 -0800623 /*
624 * Some chips can't respond to both rising and falling
625 * at the same time. If this irq was requested with
626 * both flags, we need to flip the ICR data for the IRQ
627 * to respond to the IRQ for the opposite direction.
628 * This will be indicated in the bank toggle_mask.
629 */
630 if (bank->toggle_mask & (1 << gpio_index))
631 _toggle_gpio_edge_triggering(bank, gpio_index);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800632
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100633 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100634 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000635 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700636 /* if bank has any level sensitive GPIO pin interrupt
637 configured, we must unmask the bank interrupt only after
638 handler(s) are executed in order to avoid spurious bank
639 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800640exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700641 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000642 chained_irq_exit(chip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530643 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100644}
645
Lennert Buytenheke9191022010-11-29 11:17:17 +0100646static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300647{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100648 unsigned int gpio = d->irq - IH_GPIO_BASE;
649 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700650 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300651
Colin Cross85ec7b92011-06-06 13:38:18 -0700652 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300653 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700654 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300655}
656
Lennert Buytenheke9191022010-11-29 11:17:17 +0100657static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100658{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100659 unsigned int gpio = d->irq - IH_GPIO_BASE;
660 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100661
662 _clear_gpio_irqstatus(bank, gpio);
663}
664
Lennert Buytenheke9191022010-11-29 11:17:17 +0100665static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100666{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100667 unsigned int gpio = d->irq - IH_GPIO_BASE;
668 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700669 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670
Colin Cross85ec7b92011-06-06 13:38:18 -0700671 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100672 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700673 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700674 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675}
676
Lennert Buytenheke9191022010-11-29 11:17:17 +0100677static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100678{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100679 unsigned int gpio = d->irq - IH_GPIO_BASE;
680 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700681 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100682 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700683 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700684
Colin Cross85ec7b92011-06-06 13:38:18 -0700685 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700686 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700687 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800688
689 /* For level-triggered GPIOs, the clearing must be done after
690 * the HW source is cleared, thus after the handler has run */
691 if (bank->level_mask & irq_mask) {
692 _set_gpio_irqenable(bank, gpio, 0);
693 _clear_gpio_irqstatus(bank, gpio);
694 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100695
Kevin Hilman4de8c752008-01-16 21:56:14 -0800696 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700697 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100698}
699
David Brownelle5c56ed2006-12-06 17:13:59 -0800700static struct irq_chip gpio_irq_chip = {
701 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100702 .irq_shutdown = gpio_irq_shutdown,
703 .irq_ack = gpio_ack_irq,
704 .irq_mask = gpio_mask_irq,
705 .irq_unmask = gpio_unmask_irq,
706 .irq_set_type = gpio_irq_type,
707 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800708};
709
710/*---------------------------------------------------------------------*/
711
Magnus Damm79ee0312009-07-08 13:22:04 +0200712static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800713{
Magnus Damm79ee0312009-07-08 13:22:04 +0200714 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800715 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800716 void __iomem *mask_reg = bank->base +
717 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800718 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800719
David Brownella6472532008-03-03 04:33:30 -0800720 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800721 bank->saved_wakeup = __raw_readl(mask_reg);
722 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800723 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800724
725 return 0;
726}
727
Magnus Damm79ee0312009-07-08 13:22:04 +0200728static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800729{
Magnus Damm79ee0312009-07-08 13:22:04 +0200730 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800731 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800732 void __iomem *mask_reg = bank->base +
733 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800734 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800735
David Brownella6472532008-03-03 04:33:30 -0800736 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800737 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800738 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800739
740 return 0;
741}
742
Alexey Dobriyan47145212009-12-14 18:00:08 -0800743static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200744 .suspend_noirq = omap_mpuio_suspend_noirq,
745 .resume_noirq = omap_mpuio_resume_noirq,
746};
747
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200748/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800749static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800750 .driver = {
751 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200752 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800753 },
754};
755
756static struct platform_device omap_mpuio_device = {
757 .name = "mpuio",
758 .id = -1,
759 .dev = {
760 .driver = &omap_mpuio_driver.driver,
761 }
762 /* could list the /proc/iomem resources */
763};
764
Charulatha V03e128c2011-05-05 19:58:01 +0530765static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800766{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800767 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700768
David Brownell11a78b72006-12-06 17:14:11 -0800769 if (platform_driver_register(&omap_mpuio_driver) == 0)
770 (void) platform_device_register(&omap_mpuio_device);
771}
772
David Brownelle5c56ed2006-12-06 17:13:59 -0800773/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100774
David Brownell52e31342008-03-03 12:43:23 -0800775static int gpio_input(struct gpio_chip *chip, unsigned offset)
776{
777 struct gpio_bank *bank;
778 unsigned long flags;
779
780 bank = container_of(chip, struct gpio_bank, chip);
781 spin_lock_irqsave(&bank->lock, flags);
782 _set_gpio_direction(bank, offset, 1);
783 spin_unlock_irqrestore(&bank->lock, flags);
784 return 0;
785}
786
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300787static int gpio_is_input(struct gpio_bank *bank, int mask)
788{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700789 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300790
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300791 return __raw_readl(reg) & mask;
792}
793
David Brownell52e31342008-03-03 12:43:23 -0800794static int gpio_get(struct gpio_chip *chip, unsigned offset)
795{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300796 struct gpio_bank *bank;
797 void __iomem *reg;
798 int gpio;
799 u32 mask;
800
801 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530802 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300803 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700804 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300805
806 if (gpio_is_input(bank, mask))
807 return _get_gpio_datain(bank, gpio);
808 else
809 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800810}
811
812static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
813{
814 struct gpio_bank *bank;
815 unsigned long flags;
816
817 bank = container_of(chip, struct gpio_bank, chip);
818 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700819 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800820 _set_gpio_direction(bank, offset, 0);
821 spin_unlock_irqrestore(&bank->lock, flags);
822 return 0;
823}
824
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700825static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
826 unsigned debounce)
827{
828 struct gpio_bank *bank;
829 unsigned long flags;
830
831 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800832
833 if (!bank->dbck) {
834 bank->dbck = clk_get(bank->dev, "dbclk");
835 if (IS_ERR(bank->dbck))
836 dev_err(bank->dev, "Could not get gpio dbck\n");
837 }
838
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700839 spin_lock_irqsave(&bank->lock, flags);
840 _set_gpio_debounce(bank, offset, debounce);
841 spin_unlock_irqrestore(&bank->lock, flags);
842
843 return 0;
844}
845
David Brownell52e31342008-03-03 12:43:23 -0800846static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
847{
848 struct gpio_bank *bank;
849 unsigned long flags;
850
851 bank = container_of(chip, struct gpio_bank, chip);
852 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700853 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800854 spin_unlock_irqrestore(&bank->lock, flags);
855}
856
David Brownella007b702008-12-10 17:35:25 -0800857static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
858{
859 struct gpio_bank *bank;
860
861 bank = container_of(chip, struct gpio_bank, chip);
862 return bank->virtual_irq_start + offset;
863}
864
David Brownell52e31342008-03-03 12:43:23 -0800865/*---------------------------------------------------------------------*/
866
Tony Lindgren9a748052010-12-07 16:26:56 -0800867static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700868{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700869 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700870 u32 rev;
871
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700872 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700873 return;
874
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700875 rev = __raw_readw(bank->base + bank->regs->revision);
876 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700877 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700878
879 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700880}
881
David Brownell8ba55c52008-02-26 11:10:50 -0800882/* This lock class tells lockdep that GPIO irqs are in a different
883 * category than their parents, so it won't report false recursion.
884 */
885static struct lock_class_key gpio_lock_class;
886
Charulatha V03e128c2011-05-05 19:58:01 +0530887static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800888{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530889 void __iomem *base = bank->base;
890 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800891
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530892 if (bank->width == 16)
893 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800894
Charulatha Vd0d665a2011-08-31 00:02:21 +0530895 if (bank->is_mpuio) {
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530896 __raw_writel(l, bank->base + bank->regs->irqenable);
897 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800898 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530899
900 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->irqenable_inv);
901 _gpio_rmw(base, bank->regs->irqstatus, l,
902 bank->regs->irqenable_inv == false);
903 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->debounce_en != 0);
904 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->ctrl != 0);
905 if (bank->regs->debounce_en)
906 _gpio_rmw(base, bank->regs->debounce_en, 0, 1);
907
908 /* Initialize interface clk ungated, module enabled */
909 if (bank->regs->ctrl)
910 _gpio_rmw(base, bank->regs->ctrl, 0, 1);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800911}
912
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700913static __init void
914omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
915 unsigned int num)
916{
917 struct irq_chip_generic *gc;
918 struct irq_chip_type *ct;
919
920 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
921 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -0700922 if (!gc) {
923 dev_err(bank->dev, "Memory alloc failed for gc\n");
924 return;
925 }
926
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700927 ct = gc->chip_types;
928
929 /* NOTE: No ack required, reading IRQ status clears it. */
930 ct->chip.irq_mask = irq_gc_mask_set_bit;
931 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
932 ct->chip.irq_set_type = gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530933
934 if (bank->regs->wkup_en)
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700935 ct->chip.irq_set_wake = gpio_wake_enable,
936
937 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
938 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
939 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
940}
941
Russell Kingd52b31d2011-05-27 13:56:12 -0700942static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800943{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800944 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800945 static int gpio;
946
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800947 /*
948 * REVISIT eventually switch from OMAP-specific gpio structs
949 * over to the generic ones
950 */
951 bank->chip.request = omap_gpio_request;
952 bank->chip.free = omap_gpio_free;
953 bank->chip.direction_input = gpio_input;
954 bank->chip.get = gpio_get;
955 bank->chip.direction_output = gpio_output;
956 bank->chip.set_debounce = gpio_debounce;
957 bank->chip.set = gpio_set;
958 bank->chip.to_irq = gpio_2irq;
Charulatha Vd0d665a2011-08-31 00:02:21 +0530959 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800960 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530961 if (bank->regs->wkup_en)
962 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800963 bank->chip.base = OMAP_MPUIO(0);
964 } else {
965 bank->chip.label = "gpio";
966 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -0700967 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800968 }
Kevin Hilmand5f46242011-04-21 09:23:00 -0700969 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800970
971 gpiochip_add(&bank->chip);
972
973 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -0700974 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +0100975 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100976 irq_set_chip_data(j, bank);
Charulatha Vd0d665a2011-08-31 00:02:21 +0530977 if (bank->is_mpuio) {
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700978 omap_mpuio_alloc_gc(bank, j, bank->width);
979 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100980 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700981 irq_set_handler(j, handle_simple_irq);
982 set_irq_flags(j, IRQF_VALID);
983 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800984 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100985 irq_set_chained_handler(bank->irq, gpio_irq_handler);
986 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800987}
988
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800989static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100990{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800991 struct omap_gpio_platform_data *pdata;
992 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100993 struct gpio_bank *bank;
Charulatha V03e128c2011-05-05 19:58:01 +0530994 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100995
Charulatha V03e128c2011-05-05 19:58:01 +0530996 if (!pdev->dev.platform_data) {
997 ret = -EINVAL;
998 goto err_exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100999 }
1000
Charulatha V03e128c2011-05-05 19:58:01 +05301001 bank = kzalloc(sizeof(struct gpio_bank), GFP_KERNEL);
1002 if (!bank) {
1003 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1004 ret = -ENOMEM;
1005 goto err_exit;
1006 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001007
1008 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1009 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301010 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n",
1011 pdev->id);
1012 ret = -ENODEV;
1013 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001014 }
1015
1016 bank->irq = res->start;
Charulatha V03e128c2011-05-05 19:58:01 +05301017 bank->id = pdev->id;
1018
1019 pdata = pdev->dev.platform_data;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001020 bank->virtual_irq_start = pdata->virtual_irq_start;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001021 bank->dev = &pdev->dev;
1022 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001023 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001024 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301025 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301026 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Charulatha V0cde8d02011-05-05 20:15:16 +05301027 bank->loses_context = pdata->loses_context;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301028 bank->get_context_loss_count = pdata->get_context_loss_count;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001029 bank->regs = pdata->regs;
1030
1031 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1032 bank->set_dataout = _set_gpio_dataout_reg;
1033 else
1034 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001035
1036 spin_lock_init(&bank->lock);
1037
1038 /* Static mapping, never released */
1039 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1040 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301041 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n",
1042 pdev->id);
1043 ret = -ENODEV;
1044 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001045 }
1046
1047 bank->base = ioremap(res->start, resource_size(res));
1048 if (!bank->base) {
Charulatha V03e128c2011-05-05 19:58:01 +05301049 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n",
1050 pdev->id);
1051 ret = -ENOMEM;
1052 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001053 }
1054
1055 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301056 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001057 pm_runtime_get_sync(bank->dev);
1058
Charulatha Vd0d665a2011-08-31 00:02:21 +05301059 if (bank->is_mpuio)
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301060 mpuio_init(bank);
1061
Charulatha V03e128c2011-05-05 19:58:01 +05301062 omap_gpio_mod_init(bank);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001063 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001064 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001065
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301066 pm_runtime_put(bank->dev);
1067
Charulatha V03e128c2011-05-05 19:58:01 +05301068 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001069
Charulatha V03e128c2011-05-05 19:58:01 +05301070 return ret;
1071
1072err_free:
1073 kfree(bank);
1074err_exit:
1075 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001076}
1077
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301078#ifdef CONFIG_ARCH_OMAP2PLUS
1079
1080#if defined(CONFIG_PM_SLEEP)
1081static int omap_gpio_suspend(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001082{
Charulatha V03e128c2011-05-05 19:58:01 +05301083 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001084
Charulatha V03e128c2011-05-05 19:58:01 +05301085 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301086 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001087 void __iomem *wake_status;
David Brownella6472532008-03-03 04:33:30 -08001088 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001089
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301090 if (!bank->regs->wkup_en)
1091 return 0;
1092
1093 wake_status = bank->base + bank->regs->wkup_en;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001094
David Brownella6472532008-03-03 04:33:30 -08001095 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001096 bank->saved_wakeup = __raw_readl(wake_status);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301097 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1098 _gpio_rmw(base, bank->regs->wkup_en, bank->suspend_wakeup, 1);
David Brownella6472532008-03-03 04:33:30 -08001099 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001100 }
1101
1102 return 0;
1103}
1104
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301105static int omap_gpio_resume(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001106{
Charulatha V03e128c2011-05-05 19:58:01 +05301107 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001108
Charulatha V03e128c2011-05-05 19:58:01 +05301109 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301110 void __iomem *base = bank->base;
David Brownella6472532008-03-03 04:33:30 -08001111 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001112
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301113 if (!bank->regs->wkup_en)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301114 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001115
David Brownella6472532008-03-03 04:33:30 -08001116 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301117 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1118 _gpio_rmw(base, bank->regs->wkup_en, bank->saved_wakeup, 1);
David Brownella6472532008-03-03 04:33:30 -08001119 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001120 }
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301121
1122 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001123}
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301124#endif /* CONFIG_PM_SLEEP */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001125
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301126static void omap_gpio_save_context(struct gpio_bank *bank);
1127static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001128
Paul Walmsley72e06d02010-12-21 21:05:16 -07001129void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001130{
Charulatha V03e128c2011-05-05 19:58:01 +05301131 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001132
Charulatha V03e128c2011-05-05 19:58:01 +05301133 list_for_each_entry(bank, &omap_gpio_list, node) {
Sanjeev Premica828762010-09-23 18:27:18 -07001134 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001135 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001136
Charulatha V0cde8d02011-05-05 20:15:16 +05301137 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301138 continue;
1139
Kevin Hilman0aed04352010-09-22 16:06:27 -07001140 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001141 clk_disable(bank->dbck);
1142
Paul Walmsley72e06d02010-12-21 21:05:16 -07001143 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001144 continue;
1145
1146 /* If going to OFF, remove triggering for all
1147 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1148 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001149 if (!(bank->enabled_non_wakeup_gpios))
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301150 goto save_gpio_context;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001151
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301152 bank->saved_datain = __raw_readl(bank->base +
1153 bank->regs->datain);
1154 l1 = __raw_readl(bank->base + bank->regs->fallingdetect);
1155 l2 = __raw_readl(bank->base + bank->regs->risingdetect);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001156
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001157 bank->saved_fallingdetect = l1;
1158 bank->saved_risingdetect = l2;
1159 l1 &= ~bank->enabled_non_wakeup_gpios;
1160 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001161
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301162 __raw_writel(l1, bank->base + bank->regs->fallingdetect);
1163 __raw_writel(l2, bank->base + bank->regs->risingdetect);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001164
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301165save_gpio_context:
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301166
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301167 if (bank->get_context_loss_count)
1168 bank->context_loss_count =
1169 bank->get_context_loss_count(bank->dev);
1170
1171 omap_gpio_save_context(bank);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301172
1173 if (!pm_runtime_suspended(bank->dev))
1174 pm_runtime_put(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001175 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001176}
1177
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001178void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001179{
Charulatha V03e128c2011-05-05 19:58:01 +05301180 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001181
Charulatha V03e128c2011-05-05 19:58:01 +05301182 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301183 int context_lost_cnt_after;
Sanjeev Premica828762010-09-23 18:27:18 -07001184 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001185 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001186
Charulatha V0cde8d02011-05-05 20:15:16 +05301187 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301188 continue;
1189
Kevin Hilman0aed04352010-09-22 16:06:27 -07001190 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001191 clk_enable(bank->dbck);
1192
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301193 if (pm_runtime_suspended(bank->dev))
1194 pm_runtime_get_sync(bank->dev);
1195
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301196 if (bank->get_context_loss_count) {
1197 context_lost_cnt_after =
1198 bank->get_context_loss_count(bank->dev);
1199 if (context_lost_cnt_after != bank->context_loss_count
1200 || !context_lost_cnt_after)
1201 omap_gpio_restore_context(bank);
1202 }
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001203
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001204 if (!(bank->enabled_non_wakeup_gpios))
1205 continue;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001206
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301207 __raw_writel(bank->saved_fallingdetect,
1208 bank->base + bank->regs->fallingdetect);
1209 __raw_writel(bank->saved_risingdetect,
1210 bank->base + bank->regs->risingdetect);
1211 l = __raw_readl(bank->base + bank->regs->datain);
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001212
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001213 /* Check if any of the non-wakeup interrupt GPIOs have changed
1214 * state. If so, generate an IRQ by software. This is
1215 * horribly racy, but it's the best we can do to work around
1216 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001217 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001218 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001219
1220 /*
1221 * No need to generate IRQs for the rising edge for gpio IRQs
1222 * configured with falling edge only; and vice versa.
1223 */
1224 gen0 = l & bank->saved_fallingdetect;
1225 gen0 &= bank->saved_datain;
1226
1227 gen1 = l & bank->saved_risingdetect;
1228 gen1 &= ~(bank->saved_datain);
1229
1230 /* FIXME: Consider GPIO IRQs with level detections properly! */
1231 gen = l & (~(bank->saved_fallingdetect) &
1232 ~(bank->saved_risingdetect));
1233 /* Consider all GPIO IRQs needed to be updated */
1234 gen |= gen0 | gen1;
1235
1236 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001237 u32 old0, old1;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001238
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301239 old0 = __raw_readl(bank->base +
1240 bank->regs->leveldetect0);
1241 old1 = __raw_readl(bank->base +
1242 bank->regs->leveldetect1);
1243
Sergio Aguirref00d6492010-03-03 16:21:08 +00001244 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301245 old0 |= gen;
1246 old1 |= gen;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001247 }
1248
1249 if (cpu_is_omap44xx()) {
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301250 old0 |= l;
1251 old1 |= l;
Tony Lindgren3f1686a2010-02-15 09:27:25 -08001252 }
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301253 __raw_writel(old0, bank->base +
1254 bank->regs->leveldetect0);
1255 __raw_writel(old1, bank->base +
1256 bank->regs->leveldetect1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001257 }
1258 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001259}
1260
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301261static void omap_gpio_save_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301262{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301263 bank->context.irqenable1 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301264 __raw_readl(bank->base + bank->regs->irqenable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301265 bank->context.irqenable2 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301266 __raw_readl(bank->base + bank->regs->irqenable2);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301267 bank->context.wake_en =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301268 __raw_readl(bank->base + bank->regs->wkup_en);
1269 bank->context.ctrl = __raw_readl(bank->base + bank->regs->ctrl);
1270 bank->context.oe = __raw_readl(bank->base + bank->regs->direction);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301271 bank->context.leveldetect0 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301272 __raw_readl(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301273 bank->context.leveldetect1 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301274 __raw_readl(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301275 bank->context.risingdetect =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301276 __raw_readl(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301277 bank->context.fallingdetect =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301278 __raw_readl(bank->base + bank->regs->fallingdetect);
1279 bank->context.dataout = __raw_readl(bank->base + bank->regs->dataout);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301280}
1281
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301282static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301283{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301284 __raw_writel(bank->context.irqenable1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301285 bank->base + bank->regs->irqenable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301286 __raw_writel(bank->context.irqenable2,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301287 bank->base + bank->regs->irqenable2);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301288 __raw_writel(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301289 bank->base + bank->regs->wkup_en);
1290 __raw_writel(bank->context.ctrl, bank->base + bank->regs->ctrl);
1291 __raw_writel(bank->context.oe, bank->base + bank->regs->direction);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301292 __raw_writel(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301293 bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301294 __raw_writel(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301295 bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301296 __raw_writel(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301297 bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301298 __raw_writel(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301299 bank->base + bank->regs->fallingdetect);
1300 __raw_writel(bank->context.dataout, bank->base + bank->regs->dataout);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301301}
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301302#else
1303#define omap_gpio_suspend NULL
1304#define omap_gpio_resume NULL
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301305#endif
1306
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301307static const struct dev_pm_ops gpio_pm_ops = {
1308 SET_SYSTEM_SLEEP_PM_OPS(omap_gpio_suspend, omap_gpio_resume)
1309};
1310
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001311static struct platform_driver omap_gpio_driver = {
1312 .probe = omap_gpio_probe,
1313 .driver = {
1314 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301315 .pm = &gpio_pm_ops,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001316 },
1317};
1318
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001319/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001320 * gpio driver register needs to be done before
1321 * machine_init functions access gpio APIs.
1322 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001323 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001324static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001325{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001326 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001327}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001328postcore_initcall(omap_gpio_drv_reg);