blob: 485b6b207ef9fdffde22a74b0d343d571c28fc2f [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>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/irqs.h>
Russell King1bc857f2011-07-26 10:54:55 +010028#include <asm/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029#include <asm/mach/irq.h>
30
Charulatha V03e128c2011-05-05 19:58:01 +053031static LIST_HEAD(omap_gpio_list);
32
Charulatha V6d62e212011-04-18 15:06:51 +000033struct gpio_regs {
34 u32 irqenable1;
35 u32 irqenable2;
36 u32 wake_en;
37 u32 ctrl;
38 u32 oe;
39 u32 leveldetect0;
40 u32 leveldetect1;
41 u32 risingdetect;
42 u32 fallingdetect;
43 u32 dataout;
44};
45
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010046struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053047 struct list_head node;
Tony Lindgren9f7065d2009-10-19 15:25:20 -070048 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +010049 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010050 u16 irq;
51 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +010052 int method;
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;
69 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053070 bool loses_context;
Tony Lindgren5de62b82010-12-07 16:26:58 -080071 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070072 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053073 int context_loss_count;
Charulatha V03e128c2011-05-05 19:58:01 +053074 u16 id;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070075
76 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053077 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070078
79 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010080};
81
Kevin Hilman129fd222011-04-22 07:59:07 -070082#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
83#define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
Charulatha Vc8eef652011-05-02 15:21:42 +053084#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085
86static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
87{
Tony Lindgren92105bb2005-09-07 17:20:26 +010088 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010089 u32 l;
90
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070091 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010092 l = __raw_readl(reg);
93 if (is_input)
94 l |= 1 << gpio;
95 else
96 l &= ~(1 << gpio);
97 __raw_writel(l, reg);
98}
99
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700100
101/* set data out value using dedicate set/clear register */
102static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100103{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100104 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700105 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100106
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700107 if (enable)
108 reg += bank->regs->set_dataout;
109 else
110 reg += bank->regs->clr_dataout;
111
112 __raw_writel(l, reg);
113}
114
115/* set data out value using mask register */
116static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
117{
118 void __iomem *reg = bank->base + bank->regs->dataout;
119 u32 gpio_bit = GPIO_BIT(bank, gpio);
120 u32 l;
121
122 l = __raw_readl(reg);
123 if (enable)
124 l |= gpio_bit;
125 else
126 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100127 __raw_writel(l, reg);
128}
129
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300130static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100131{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700132 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700134 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100135}
136
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300137static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
138{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700139 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300140
Kevin Hilman129fd222011-04-22 07:59:07 -0700141 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300142}
143
Kevin Hilmanece95282011-07-12 08:18:15 -0700144static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
145{
146 int l = __raw_readl(base + reg);
147
148 if (set)
149 l |= mask;
150 else
151 l &= ~mask;
152
153 __raw_writel(l, base + reg);
154}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100155
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700156/**
157 * _set_gpio_debounce - low level gpio debounce time
158 * @bank: the gpio bank we're acting upon
159 * @gpio: the gpio number on this @gpio
160 * @debounce: debounce time to use
161 *
162 * OMAP's debounce time is in 31us steps so we need
163 * to convert and round up to the closest unit.
164 */
165static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
166 unsigned debounce)
167{
Kevin Hilman9942da02011-04-22 12:02:05 -0700168 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700169 u32 val;
170 u32 l;
171
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800172 if (!bank->dbck_flag)
173 return;
174
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700175 if (debounce < 32)
176 debounce = 0x01;
177 else if (debounce > 7936)
178 debounce = 0xff;
179 else
180 debounce = (debounce / 0x1f) - 1;
181
Kevin Hilman129fd222011-04-22 07:59:07 -0700182 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700183
Kevin Hilman9942da02011-04-22 12:02:05 -0700184 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700185 __raw_writel(debounce, reg);
186
Kevin Hilman9942da02011-04-22 12:02:05 -0700187 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700188 val = __raw_readl(reg);
189
190 if (debounce) {
191 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800192 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700193 } else {
194 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800195 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700196 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300197 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700198
199 __raw_writel(val, reg);
200}
201
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530202static inline void set_gpio_trigger(struct gpio_bank *bank, int gpio,
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700203 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100204{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800205 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100206 u32 gpio_bit = 1 << gpio;
207
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530208 _gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
209 trigger & IRQ_TYPE_LEVEL_LOW);
210 _gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
211 trigger & IRQ_TYPE_LEVEL_HIGH);
212 _gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
213 trigger & IRQ_TYPE_EDGE_RISING);
214 _gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
215 trigger & IRQ_TYPE_EDGE_FALLING);
216
217 if (likely(!(bank->non_wakeup_gpios & gpio_bit)))
218 _gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
219
Ambresh K55b220c2011-06-15 13:40:45 -0700220 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530221 if (!bank->regs->irqctrl) {
222 /* On omap24xx proceed only when valid GPIO bit is set */
223 if (bank->non_wakeup_gpios) {
224 if (!(bank->non_wakeup_gpios & gpio_bit))
225 goto exit;
226 }
227
Chunqiu Wang699117a62009-06-24 17:13:39 +0000228 /*
229 * Log the edge gpio and manually trigger the IRQ
230 * after resume if the input level changes
231 * to avoid irq lost during PER RET/OFF mode
232 * Applies for omap2 non-wakeup gpio and all omap3 gpios
233 */
234 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800235 bank->enabled_non_wakeup_gpios |= gpio_bit;
236 else
237 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
238 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700239
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530240exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530241 bank->level_mask =
242 __raw_readl(bank->base + bank->regs->leveldetect0) |
243 __raw_readl(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100244}
245
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800246#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800247/*
248 * This only applies to chips that can't do both rising and falling edge
249 * detection at once. For all other chips, this function is a noop.
250 */
251static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
252{
253 void __iomem *reg = bank->base;
254 u32 l = 0;
255
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530256 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800257 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530258
259 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800260
261 l = __raw_readl(reg);
262 if ((l >> gpio) & 1)
263 l &= ~(1 << gpio);
264 else
265 l |= 1 << gpio;
266
267 __raw_writel(l, reg);
268}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530269#else
270static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800271#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800272
Tony Lindgren92105bb2005-09-07 17:20:26 +0100273static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
274{
275 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530276 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100277 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100278
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530279 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
280 set_gpio_trigger(bank, gpio, trigger);
281 } else if (bank->regs->irqctrl) {
282 reg += bank->regs->irqctrl;
283
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100284 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000285 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800286 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100287 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100288 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100289 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100290 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100291 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530292 return -EINVAL;
293
294 __raw_writel(l, reg);
295 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100296 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530297 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100298 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530299 reg += bank->regs->edgectrl1;
300
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100301 gpio &= 0x07;
302 l = __raw_readl(reg);
303 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100304 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100305 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100306 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100307 l |= 1 << (gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530308
309 /* Enable wake-up during idle for dynamic tick */
310 _gpio_rmw(base, bank->regs->wkup_en, 1 << gpio, trigger);
311 __raw_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100312 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100313 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100314}
315
Lennert Buytenheke9191022010-11-29 11:17:17 +0100316static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100317{
318 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100319 unsigned gpio;
320 int retval;
David Brownella6472532008-03-03 04:33:30 -0800321 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100322
Lennert Buytenheke9191022010-11-29 11:17:17 +0100323 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
324 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100325 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100326 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100327
David Brownelle5c56ed2006-12-06 17:13:59 -0800328 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100329 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800330
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530331 bank = irq_data_get_irq_chip_data(d);
332
333 if (!bank->regs->leveldetect0 &&
334 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100335 return -EINVAL;
336
David Brownella6472532008-03-03 04:33:30 -0800337 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700338 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800339 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800340
341 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100342 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800343 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100344 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800345
Tony Lindgren92105bb2005-09-07 17:20:26 +0100346 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100347}
348
349static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
350{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100351 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100352
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700353 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100354 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300355
356 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700357 if (bank->regs->irqstatus2) {
358 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700359 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700360 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700361
362 /* Flush posted write for the irq status to avoid spurious interrupts */
363 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100364}
365
366static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
367{
Kevin Hilman129fd222011-04-22 07:59:07 -0700368 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100369}
370
Imre Deakea6dedd2006-06-26 16:16:00 -0700371static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
372{
373 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700374 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700375 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700376
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700377 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700378 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700379 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700380 l = ~l;
381 l &= mask;
382 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700383}
384
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700385static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100386{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100387 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100388 u32 l;
389
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700390 if (bank->regs->set_irqenable) {
391 reg += bank->regs->set_irqenable;
392 l = gpio_mask;
393 } else {
394 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100395 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700396 if (bank->regs->irqenable_inv)
397 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100398 else
399 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100400 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700401
402 __raw_writel(l, reg);
403}
404
405static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
406{
407 void __iomem *reg = bank->base;
408 u32 l;
409
410 if (bank->regs->clr_irqenable) {
411 reg += bank->regs->clr_irqenable;
412 l = gpio_mask;
413 } else {
414 reg += bank->regs->irqenable;
415 l = __raw_readl(reg);
416 if (bank->regs->irqenable_inv)
417 l |= gpio_mask;
418 else
419 l &= ~gpio_mask;
420 }
421
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100422 __raw_writel(l, reg);
423}
424
425static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
426{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700427 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100428}
429
Tony Lindgren92105bb2005-09-07 17:20:26 +0100430/*
431 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
432 * 1510 does not seem to have a wake-up register. If JTAG is connected
433 * to the target, system will wake up always on GPIO events. While
434 * system is running all registered GPIO interrupts need to have wake-up
435 * enabled. When system is suspended, only selected GPIO interrupts need
436 * to have wake-up enabled.
437 */
438static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
439{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700440 u32 gpio_bit = GPIO_BIT(bank, gpio);
441 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800442
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700443 if (bank->non_wakeup_gpios & gpio_bit) {
444 dev_err(bank->dev,
445 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100446 return -EINVAL;
447 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700448
449 spin_lock_irqsave(&bank->lock, flags);
450 if (enable)
451 bank->suspend_wakeup |= gpio_bit;
452 else
453 bank->suspend_wakeup &= ~gpio_bit;
454
455 spin_unlock_irqrestore(&bank->lock, flags);
456
457 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100458}
459
Tony Lindgren4196dd62006-09-25 12:41:38 +0300460static void _reset_gpio(struct gpio_bank *bank, int gpio)
461{
Kevin Hilman129fd222011-04-22 07:59:07 -0700462 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300463 _set_gpio_irqenable(bank, gpio, 0);
464 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700465 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300466}
467
Tony Lindgren92105bb2005-09-07 17:20:26 +0100468/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100469static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100470{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100471 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100472 struct gpio_bank *bank;
473 int retval;
474
Lennert Buytenheke9191022010-11-29 11:17:17 +0100475 bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700476 retval = _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100477
478 return retval;
479}
480
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800481static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100482{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800483 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800484 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100485
David Brownella6472532008-03-03 04:33:30 -0800486 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100487
Tony Lindgren4196dd62006-09-25 12:41:38 +0300488 /* Set trigger to none. You need to enable the desired trigger with
489 * request_irq() or set_irq_type().
490 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800491 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100492
Charulatha Vfad96ea2011-05-25 11:23:50 +0530493 if (bank->regs->pinctrl) {
494 void __iomem *reg = bank->base + bank->regs->pinctrl;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100495
Tony Lindgren92105bb2005-09-07 17:20:26 +0100496 /* Claim the pin for MPU */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800497 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100498 }
Charulatha Vfad96ea2011-05-25 11:23:50 +0530499
Charulatha Vc8eef652011-05-02 15:21:42 +0530500 if (bank->regs->ctrl && !bank->mod_usage) {
501 void __iomem *reg = bank->base + bank->regs->ctrl;
502 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700503
Charulatha Vc8eef652011-05-02 15:21:42 +0530504 ctrl = __raw_readl(reg);
505 /* Module is enabled, clocks are not gated */
506 ctrl &= ~GPIO_MOD_CTRL_BIT;
507 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800508 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530509
510 bank->mod_usage |= 1 << offset;
511
David Brownella6472532008-03-03 04:33:30 -0800512 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100513
514 return 0;
515}
516
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800517static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100518{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800519 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530520 void __iomem *base = bank->base;
David Brownella6472532008-03-03 04:33:30 -0800521 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100522
David Brownella6472532008-03-03 04:33:30 -0800523 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530524
525 if (bank->regs->wkup_en)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100526 /* Disable wake-up during idle for dynamic tick */
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530527 _gpio_rmw(base, bank->regs->wkup_en, 1 << offset, 0);
528
Charulatha Vc8eef652011-05-02 15:21:42 +0530529 bank->mod_usage &= ~(1 << offset);
Charulatha V9f096862010-05-14 12:05:27 -0700530
Charulatha Vc8eef652011-05-02 15:21:42 +0530531 if (bank->regs->ctrl && !bank->mod_usage) {
532 void __iomem *reg = bank->base + bank->regs->ctrl;
533 u32 ctrl;
534
535 ctrl = __raw_readl(reg);
536 /* Module is disabled, clocks are gated */
537 ctrl |= GPIO_MOD_CTRL_BIT;
538 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -0800539 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530540
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800541 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800542 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100543}
544
545/*
546 * We need to unmask the GPIO bank interrupt as soon as possible to
547 * avoid missing GPIO interrupts for other lines in the bank.
548 * Then we need to mask-read-clear-unmask the triggered GPIO lines
549 * in the bank to avoid missing nested interrupts for a GPIO line.
550 * If we wait to unmask individual GPIO lines in the bank after the
551 * line's interrupt handler has been run, we may miss some nested
552 * interrupts.
553 */
Russell King10dd5ce2006-11-23 11:41:32 +0000554static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100555{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100556 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100557 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800558 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100559 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700560 u32 retrigger = 0;
561 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000562 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100563
Will Deaconee144182011-02-21 13:46:08 +0000564 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100565
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100566 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700567 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800568
569 if (WARN_ON(!isr_reg))
570 goto exit;
571
Tony Lindgren92105bb2005-09-07 17:20:26 +0100572 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100573 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700574 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100575
Imre Deakea6dedd2006-06-26 16:16:00 -0700576 enabled = _get_gpio_irqbank_mask(bank);
577 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100578
579 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
580 isr &= 0x0000ffff;
581
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530582 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800583 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100584
585 /* clear edge sensitive interrupts before handler(s) are
586 called so that we don't miss any interrupt occurred while
587 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700588 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100589 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700590 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100591
592 /* if there is only edge sensitive GPIO pin interrupts
593 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700594 if (!level_mask && !unmasked) {
595 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000596 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700597 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100598
Imre Deakea6dedd2006-06-26 16:16:00 -0700599 isr |= retrigger;
600 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100601 if (!isr)
602 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100603
Tony Lindgren92105bb2005-09-07 17:20:26 +0100604 gpio_irq = bank->virtual_irq_start;
605 for (; isr != 0; isr >>= 1, gpio_irq++) {
Kevin Hilman129fd222011-04-22 07:59:07 -0700606 gpio_index = GPIO_INDEX(bank, irq_to_gpio(gpio_irq));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800607
Tony Lindgren92105bb2005-09-07 17:20:26 +0100608 if (!(isr & 1))
609 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200610
Cory Maccarrone4318f362010-01-08 10:29:04 -0800611 /*
612 * Some chips can't respond to both rising and falling
613 * at the same time. If this irq was requested with
614 * both flags, we need to flip the ICR data for the IRQ
615 * to respond to the IRQ for the opposite direction.
616 * This will be indicated in the bank toggle_mask.
617 */
618 if (bank->toggle_mask & (1 << gpio_index))
619 _toggle_gpio_edge_triggering(bank, gpio_index);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800620
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100621 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100622 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000623 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700624 /* if bank has any level sensitive GPIO pin interrupt
625 configured, we must unmask the bank interrupt only after
626 handler(s) are executed in order to avoid spurious bank
627 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800628exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700629 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000630 chained_irq_exit(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100631}
632
Lennert Buytenheke9191022010-11-29 11:17:17 +0100633static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300634{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100635 unsigned int gpio = d->irq - IH_GPIO_BASE;
636 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700637 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300638
Colin Cross85ec7b92011-06-06 13:38:18 -0700639 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300640 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700641 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300642}
643
Lennert Buytenheke9191022010-11-29 11:17:17 +0100644static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100645{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100646 unsigned int gpio = d->irq - IH_GPIO_BASE;
647 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100648
649 _clear_gpio_irqstatus(bank, gpio);
650}
651
Lennert Buytenheke9191022010-11-29 11:17:17 +0100652static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100653{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100654 unsigned int gpio = d->irq - IH_GPIO_BASE;
655 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700656 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100657
Colin Cross85ec7b92011-06-06 13:38:18 -0700658 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100659 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700660 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700661 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100662}
663
Lennert Buytenheke9191022010-11-29 11:17:17 +0100664static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100665{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100666 unsigned int gpio = d->irq - IH_GPIO_BASE;
667 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilman129fd222011-04-22 07:59:07 -0700668 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100669 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700670 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700671
Colin Cross85ec7b92011-06-06 13:38:18 -0700672 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700673 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700674 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800675
676 /* For level-triggered GPIOs, the clearing must be done after
677 * the HW source is cleared, thus after the handler has run */
678 if (bank->level_mask & irq_mask) {
679 _set_gpio_irqenable(bank, gpio, 0);
680 _clear_gpio_irqstatus(bank, gpio);
681 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100682
Kevin Hilman4de8c752008-01-16 21:56:14 -0800683 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700684 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100685}
686
David Brownelle5c56ed2006-12-06 17:13:59 -0800687static struct irq_chip gpio_irq_chip = {
688 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100689 .irq_shutdown = gpio_irq_shutdown,
690 .irq_ack = gpio_ack_irq,
691 .irq_mask = gpio_mask_irq,
692 .irq_unmask = gpio_unmask_irq,
693 .irq_set_type = gpio_irq_type,
694 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800695};
696
697/*---------------------------------------------------------------------*/
698
699#ifdef CONFIG_ARCH_OMAP1
700
David Brownelle5c56ed2006-12-06 17:13:59 -0800701#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
702
David Brownell11a78b72006-12-06 17:14:11 -0800703#ifdef CONFIG_ARCH_OMAP16XX
704
705#include <linux/platform_device.h>
706
Magnus Damm79ee0312009-07-08 13:22:04 +0200707static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800708{
Magnus Damm79ee0312009-07-08 13:22:04 +0200709 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800710 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800711 void __iomem *mask_reg = bank->base +
712 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800713 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800714
David Brownella6472532008-03-03 04:33:30 -0800715 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800716 bank->saved_wakeup = __raw_readl(mask_reg);
717 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800718 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800719
720 return 0;
721}
722
Magnus Damm79ee0312009-07-08 13:22:04 +0200723static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800724{
Magnus Damm79ee0312009-07-08 13:22:04 +0200725 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800726 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800727 void __iomem *mask_reg = bank->base +
728 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800729 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800730
David Brownella6472532008-03-03 04:33:30 -0800731 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800732 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800733 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800734
735 return 0;
736}
737
Alexey Dobriyan47145212009-12-14 18:00:08 -0800738static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200739 .suspend_noirq = omap_mpuio_suspend_noirq,
740 .resume_noirq = omap_mpuio_resume_noirq,
741};
742
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200743/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800744static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800745 .driver = {
746 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200747 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800748 },
749};
750
751static struct platform_device omap_mpuio_device = {
752 .name = "mpuio",
753 .id = -1,
754 .dev = {
755 .driver = &omap_mpuio_driver.driver,
756 }
757 /* could list the /proc/iomem resources */
758};
759
Charulatha V03e128c2011-05-05 19:58:01 +0530760static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800761{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800762 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700763
David Brownell11a78b72006-12-06 17:14:11 -0800764 if (platform_driver_register(&omap_mpuio_driver) == 0)
765 (void) platform_device_register(&omap_mpuio_device);
766}
767
768#else
Charulatha V03e128c2011-05-05 19:58:01 +0530769static inline void mpuio_init(struct gpio_bank *bank) {}
David Brownell11a78b72006-12-06 17:14:11 -0800770#endif /* 16xx */
771
David Brownelle5c56ed2006-12-06 17:13:59 -0800772#else
773
David Brownelle5c56ed2006-12-06 17:13:59 -0800774#define bank_is_mpuio(bank) 0
Charulatha V03e128c2011-05-05 19:58:01 +0530775static inline void mpuio_init(struct gpio_bank *bank) {}
David Brownelle5c56ed2006-12-06 17:13:59 -0800776
777#endif
778
779/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100780
David Brownell52e31342008-03-03 12:43:23 -0800781/* REVISIT these are stupid implementations! replace by ones that
782 * don't switch on METHOD_* and which mostly avoid spinlocks
783 */
784
785static int gpio_input(struct gpio_chip *chip, unsigned offset)
786{
787 struct gpio_bank *bank;
788 unsigned long flags;
789
790 bank = container_of(chip, struct gpio_bank, chip);
791 spin_lock_irqsave(&bank->lock, flags);
792 _set_gpio_direction(bank, offset, 1);
793 spin_unlock_irqrestore(&bank->lock, flags);
794 return 0;
795}
796
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300797static int gpio_is_input(struct gpio_bank *bank, int mask)
798{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700799 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300800
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300801 return __raw_readl(reg) & mask;
802}
803
David Brownell52e31342008-03-03 12:43:23 -0800804static int gpio_get(struct gpio_chip *chip, unsigned offset)
805{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300806 struct gpio_bank *bank;
807 void __iomem *reg;
808 int gpio;
809 u32 mask;
810
811 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530812 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300813 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700814 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300815
816 if (gpio_is_input(bank, mask))
817 return _get_gpio_datain(bank, gpio);
818 else
819 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800820}
821
822static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
823{
824 struct gpio_bank *bank;
825 unsigned long flags;
826
827 bank = container_of(chip, struct gpio_bank, chip);
828 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700829 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800830 _set_gpio_direction(bank, offset, 0);
831 spin_unlock_irqrestore(&bank->lock, flags);
832 return 0;
833}
834
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700835static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
836 unsigned debounce)
837{
838 struct gpio_bank *bank;
839 unsigned long flags;
840
841 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800842
843 if (!bank->dbck) {
844 bank->dbck = clk_get(bank->dev, "dbclk");
845 if (IS_ERR(bank->dbck))
846 dev_err(bank->dev, "Could not get gpio dbck\n");
847 }
848
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700849 spin_lock_irqsave(&bank->lock, flags);
850 _set_gpio_debounce(bank, offset, debounce);
851 spin_unlock_irqrestore(&bank->lock, flags);
852
853 return 0;
854}
855
David Brownell52e31342008-03-03 12:43:23 -0800856static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
857{
858 struct gpio_bank *bank;
859 unsigned long flags;
860
861 bank = container_of(chip, struct gpio_bank, chip);
862 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700863 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800864 spin_unlock_irqrestore(&bank->lock, flags);
865}
866
David Brownella007b702008-12-10 17:35:25 -0800867static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
868{
869 struct gpio_bank *bank;
870
871 bank = container_of(chip, struct gpio_bank, chip);
872 return bank->virtual_irq_start + offset;
873}
874
David Brownell52e31342008-03-03 12:43:23 -0800875/*---------------------------------------------------------------------*/
876
Tony Lindgren9a748052010-12-07 16:26:56 -0800877static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700878{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700879 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700880 u32 rev;
881
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700882 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700883 return;
884
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700885 rev = __raw_readw(bank->base + bank->regs->revision);
886 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700887 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700888
889 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700890}
891
David Brownell8ba55c52008-02-26 11:10:50 -0800892/* This lock class tells lockdep that GPIO irqs are in a different
893 * category than their parents, so it won't report false recursion.
894 */
895static struct lock_class_key gpio_lock_class;
896
Charulatha V03e128c2011-05-05 19:58:01 +0530897static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800898{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530899 void __iomem *base = bank->base;
900 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800901
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530902 if (bank->width == 16)
903 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800904
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530905 if (bank_is_mpuio(bank)) {
906 __raw_writel(l, bank->base + bank->regs->irqenable);
907 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800908 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530909
910 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->irqenable_inv);
911 _gpio_rmw(base, bank->regs->irqstatus, l,
912 bank->regs->irqenable_inv == false);
913 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->debounce_en != 0);
914 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->ctrl != 0);
915 if (bank->regs->debounce_en)
916 _gpio_rmw(base, bank->regs->debounce_en, 0, 1);
917
918 /* Initialize interface clk ungated, module enabled */
919 if (bank->regs->ctrl)
920 _gpio_rmw(base, bank->regs->ctrl, 0, 1);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800921}
922
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700923static __init void
924omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
925 unsigned int num)
926{
927 struct irq_chip_generic *gc;
928 struct irq_chip_type *ct;
929
930 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
931 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -0700932 if (!gc) {
933 dev_err(bank->dev, "Memory alloc failed for gc\n");
934 return;
935 }
936
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700937 ct = gc->chip_types;
938
939 /* NOTE: No ack required, reading IRQ status clears it. */
940 ct->chip.irq_mask = irq_gc_mask_set_bit;
941 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
942 ct->chip.irq_set_type = gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530943
944 if (bank->regs->wkup_en)
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700945 ct->chip.irq_set_wake = gpio_wake_enable,
946
947 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
948 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
949 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
950}
951
Russell Kingd52b31d2011-05-27 13:56:12 -0700952static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800953{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800954 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800955 static int gpio;
956
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800957 bank->mod_usage = 0;
958 /*
959 * REVISIT eventually switch from OMAP-specific gpio structs
960 * over to the generic ones
961 */
962 bank->chip.request = omap_gpio_request;
963 bank->chip.free = omap_gpio_free;
964 bank->chip.direction_input = gpio_input;
965 bank->chip.get = gpio_get;
966 bank->chip.direction_output = gpio_output;
967 bank->chip.set_debounce = gpio_debounce;
968 bank->chip.set = gpio_set;
969 bank->chip.to_irq = gpio_2irq;
970 if (bank_is_mpuio(bank)) {
971 bank->chip.label = "mpuio";
972#ifdef CONFIG_ARCH_OMAP16XX
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530973 if (bank->regs->wkup_en)
974 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800975#endif
976 bank->chip.base = OMAP_MPUIO(0);
977 } else {
978 bank->chip.label = "gpio";
979 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -0700980 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800981 }
Kevin Hilmand5f46242011-04-21 09:23:00 -0700982 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800983
984 gpiochip_add(&bank->chip);
985
986 for (j = bank->virtual_irq_start;
Kevin Hilmand5f46242011-04-21 09:23:00 -0700987 j < bank->virtual_irq_start + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +0100988 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100989 irq_set_chip_data(j, bank);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700990 if (bank_is_mpuio(bank)) {
991 omap_mpuio_alloc_gc(bank, j, bank->width);
992 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100993 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700994 irq_set_handler(j, handle_simple_irq);
995 set_irq_flags(j, IRQF_VALID);
996 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800997 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100998 irq_set_chained_handler(bank->irq, gpio_irq_handler);
999 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001000}
1001
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001002static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001003{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001004 struct omap_gpio_platform_data *pdata;
1005 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001006 struct gpio_bank *bank;
Charulatha V03e128c2011-05-05 19:58:01 +05301007 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001008
Charulatha V03e128c2011-05-05 19:58:01 +05301009 if (!pdev->dev.platform_data) {
1010 ret = -EINVAL;
1011 goto err_exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001012 }
1013
Charulatha V03e128c2011-05-05 19:58:01 +05301014 bank = kzalloc(sizeof(struct gpio_bank), GFP_KERNEL);
1015 if (!bank) {
1016 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1017 ret = -ENOMEM;
1018 goto err_exit;
1019 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001020
1021 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1022 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301023 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n",
1024 pdev->id);
1025 ret = -ENODEV;
1026 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001027 }
1028
1029 bank->irq = res->start;
Charulatha V03e128c2011-05-05 19:58:01 +05301030 bank->id = pdev->id;
1031
1032 pdata = pdev->dev.platform_data;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001033 bank->virtual_irq_start = pdata->virtual_irq_start;
1034 bank->method = pdata->bank_type;
1035 bank->dev = &pdev->dev;
1036 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001037 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001038 bank->width = pdata->bank_width;
Charulatha V803a2432011-05-05 17:04:12 +05301039 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Charulatha V0cde8d02011-05-05 20:15:16 +05301040 bank->loses_context = pdata->loses_context;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301041 bank->get_context_loss_count = pdata->get_context_loss_count;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001042 bank->regs = pdata->regs;
1043
1044 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1045 bank->set_dataout = _set_gpio_dataout_reg;
1046 else
1047 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001048
1049 spin_lock_init(&bank->lock);
1050
1051 /* Static mapping, never released */
1052 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1053 if (unlikely(!res)) {
Charulatha V03e128c2011-05-05 19:58:01 +05301054 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n",
1055 pdev->id);
1056 ret = -ENODEV;
1057 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001058 }
1059
1060 bank->base = ioremap(res->start, resource_size(res));
1061 if (!bank->base) {
Charulatha V03e128c2011-05-05 19:58:01 +05301062 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n",
1063 pdev->id);
1064 ret = -ENOMEM;
1065 goto err_free;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001066 }
1067
1068 pm_runtime_enable(bank->dev);
1069 pm_runtime_get_sync(bank->dev);
1070
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301071 if (bank_is_mpuio(bank))
1072 mpuio_init(bank);
1073
Charulatha V03e128c2011-05-05 19:58:01 +05301074 omap_gpio_mod_init(bank);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001075 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001076 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001077
Charulatha V03e128c2011-05-05 19:58:01 +05301078 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001079
Charulatha V03e128c2011-05-05 19:58:01 +05301080 return ret;
1081
1082err_free:
1083 kfree(bank);
1084err_exit:
1085 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001086}
1087
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001088static int omap_gpio_suspend(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001089{
Charulatha V03e128c2011-05-05 19:58:01 +05301090 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001091
Charulatha V03e128c2011-05-05 19:58:01 +05301092 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301093 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001094 void __iomem *wake_status;
David Brownella6472532008-03-03 04:33:30 -08001095 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001096
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301097 if (!bank->regs->wkup_en)
1098 return 0;
1099
1100 wake_status = bank->base + bank->regs->wkup_en;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001101
David Brownella6472532008-03-03 04:33:30 -08001102 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001103 bank->saved_wakeup = __raw_readl(wake_status);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301104 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1105 _gpio_rmw(base, bank->regs->wkup_en, bank->suspend_wakeup, 1);
David Brownella6472532008-03-03 04:33:30 -08001106 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001107 }
1108
1109 return 0;
1110}
1111
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001112static void omap_gpio_resume(void)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001113{
Charulatha V03e128c2011-05-05 19:58:01 +05301114 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001115
Charulatha V03e128c2011-05-05 19:58:01 +05301116 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301117 void __iomem *base = bank->base;
David Brownella6472532008-03-03 04:33:30 -08001118 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001119
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301120 if (!bank->regs->wkup_en)
1121 return;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001122
David Brownella6472532008-03-03 04:33:30 -08001123 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301124 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1125 _gpio_rmw(base, bank->regs->wkup_en, bank->saved_wakeup, 1);
David Brownella6472532008-03-03 04:33:30 -08001126 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001127 }
Tony Lindgren92105bb2005-09-07 17:20:26 +01001128}
1129
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001130static struct syscore_ops omap_gpio_syscore_ops = {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001131 .suspend = omap_gpio_suspend,
1132 .resume = omap_gpio_resume,
1133};
1134
Tony Lindgren140455f2010-02-12 12:26:48 -08001135#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001136
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
Paul Walmsley72e06d02010-12-21 21:05:16 -07001140void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001141{
Charulatha V03e128c2011-05-05 19:58:01 +05301142 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001143
Charulatha V03e128c2011-05-05 19:58:01 +05301144 list_for_each_entry(bank, &omap_gpio_list, node) {
Sanjeev Premica828762010-09-23 18:27:18 -07001145 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001146 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001147
Charulatha V0cde8d02011-05-05 20:15:16 +05301148 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301149 continue;
1150
Kevin Hilman0aed04352010-09-22 16:06:27 -07001151 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001152 clk_disable(bank->dbck);
1153
Paul Walmsley72e06d02010-12-21 21:05:16 -07001154 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001155 continue;
1156
1157 /* If going to OFF, remove triggering for all
1158 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1159 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001160 if (!(bank->enabled_non_wakeup_gpios))
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301161 goto save_gpio_context;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001162
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301163 bank->saved_datain = __raw_readl(bank->base +
1164 bank->regs->datain);
1165 l1 = __raw_readl(bank->base + bank->regs->fallingdetect);
1166 l2 = __raw_readl(bank->base + bank->regs->risingdetect);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001167
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001168 bank->saved_fallingdetect = l1;
1169 bank->saved_risingdetect = l2;
1170 l1 &= ~bank->enabled_non_wakeup_gpios;
1171 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001172
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301173 __raw_writel(l1, bank->base + bank->regs->fallingdetect);
1174 __raw_writel(l2, bank->base + bank->regs->risingdetect);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001175
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301176save_gpio_context:
1177 if (bank->get_context_loss_count)
1178 bank->context_loss_count =
1179 bank->get_context_loss_count(bank->dev);
1180
1181 omap_gpio_save_context(bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001182 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001183}
1184
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001185void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001186{
Charulatha V03e128c2011-05-05 19:58:01 +05301187 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001188
Charulatha V03e128c2011-05-05 19:58:01 +05301189 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301190 int context_lost_cnt_after;
Sanjeev Premica828762010-09-23 18:27:18 -07001191 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001192 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001193
Charulatha V0cde8d02011-05-05 20:15:16 +05301194 if (!bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301195 continue;
1196
Kevin Hilman0aed04352010-09-22 16:06:27 -07001197 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001198 clk_enable(bank->dbck);
1199
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301200 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 }
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001207
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001208 if (!(bank->enabled_non_wakeup_gpios))
1209 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001210
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301211 __raw_writel(bank->saved_fallingdetect,
1212 bank->base + bank->regs->fallingdetect);
1213 __raw_writel(bank->saved_risingdetect,
1214 bank->base + bank->regs->risingdetect);
1215 l = __raw_readl(bank->base + bank->regs->datain);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001216
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001217 /* Check if any of the non-wakeup interrupt GPIOs have changed
1218 * state. If so, generate an IRQ by software. This is
1219 * horribly racy, but it's the best we can do to work around
1220 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001221 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001222 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001223
1224 /*
1225 * No need to generate IRQs for the rising edge for gpio IRQs
1226 * configured with falling edge only; and vice versa.
1227 */
1228 gen0 = l & bank->saved_fallingdetect;
1229 gen0 &= bank->saved_datain;
1230
1231 gen1 = l & bank->saved_risingdetect;
1232 gen1 &= ~(bank->saved_datain);
1233
1234 /* FIXME: Consider GPIO IRQs with level detections properly! */
1235 gen = l & (~(bank->saved_fallingdetect) &
1236 ~(bank->saved_risingdetect));
1237 /* Consider all GPIO IRQs needed to be updated */
1238 gen |= gen0 | gen1;
1239
1240 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001241 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001242
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301243 old0 = __raw_readl(bank->base +
1244 bank->regs->leveldetect0);
1245 old1 = __raw_readl(bank->base +
1246 bank->regs->leveldetect1);
1247
Sergio Aguirref00d6492010-03-03 16:21:08 +00001248 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301249 old0 |= gen;
1250 old1 |= gen;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001251 }
1252
1253 if (cpu_is_omap44xx()) {
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301254 old0 |= l;
1255 old1 |= l;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001256 }
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +05301257 __raw_writel(old0, bank->base +
1258 bank->regs->leveldetect0);
1259 __raw_writel(old1, bank->base +
1260 bank->regs->leveldetect1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001261 }
1262 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001263}
1264
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301265static void omap_gpio_save_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301266{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301267 bank->context.irqenable1 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301268 __raw_readl(bank->base + bank->regs->irqenable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301269 bank->context.irqenable2 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301270 __raw_readl(bank->base + bank->regs->irqenable2);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301271 bank->context.wake_en =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301272 __raw_readl(bank->base + bank->regs->wkup_en);
1273 bank->context.ctrl = __raw_readl(bank->base + bank->regs->ctrl);
1274 bank->context.oe = __raw_readl(bank->base + bank->regs->direction);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301275 bank->context.leveldetect0 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301276 __raw_readl(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301277 bank->context.leveldetect1 =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301278 __raw_readl(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301279 bank->context.risingdetect =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301280 __raw_readl(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301281 bank->context.fallingdetect =
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301282 __raw_readl(bank->base + bank->regs->fallingdetect);
1283 bank->context.dataout = __raw_readl(bank->base + bank->regs->dataout);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301284}
1285
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301286static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301287{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301288 __raw_writel(bank->context.irqenable1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301289 bank->base + bank->regs->irqenable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301290 __raw_writel(bank->context.irqenable2,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301291 bank->base + bank->regs->irqenable2);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301292 __raw_writel(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301293 bank->base + bank->regs->wkup_en);
1294 __raw_writel(bank->context.ctrl, bank->base + bank->regs->ctrl);
1295 __raw_writel(bank->context.oe, bank->base + bank->regs->direction);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301296 __raw_writel(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301297 bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301298 __raw_writel(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301299 bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301300 __raw_writel(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301301 bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301302 __raw_writel(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301303 bank->base + bank->regs->fallingdetect);
1304 __raw_writel(bank->context.dataout, bank->base + bank->regs->dataout);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301305}
1306#endif
1307
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001308static struct platform_driver omap_gpio_driver = {
1309 .probe = omap_gpio_probe,
1310 .driver = {
1311 .name = "omap_gpio",
1312 },
1313};
1314
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001315/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001316 * gpio driver register needs to be done before
1317 * machine_init functions access gpio APIs.
1318 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001319 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001320static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001321{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001322 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001323}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001324postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001325
Tony Lindgren92105bb2005-09-07 17:20:26 +01001326static int __init omap_gpio_sysinit(void)
1327{
David Brownell11a78b72006-12-06 17:14:11 -08001328
Tony Lindgren140455f2010-02-12 12:26:48 -08001329#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001330 if (cpu_is_omap16xx() || cpu_class_is_omap2())
1331 register_syscore_ops(&omap_gpio_syscore_ops);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001332#endif
1333
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001334 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001335}
1336
Tony Lindgren92105bb2005-09-07 17:20:26 +01001337arch_initcall(omap_gpio_sysinit);