blob: 1a144ac3b34ec4e7025c4d652deffd4076bc0d0b [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Benoit Cousson96751fc2012-02-01 16:01:39 +010022#include <linux/device.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080023#include <linux/pm_runtime.h>
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +053024#include <linux/pm.h>
Benoit Cousson384ebe12011-08-16 11:53:02 +020025#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/irqdomain.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010028
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010030#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010031#include <mach/irqs.h>
Russell King1bc857f2011-07-26 10:54:55 +010032#include <asm/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010033#include <asm/mach/irq.h>
34
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053035#define OFF_MODE 1
36
Charulatha V03e128c2011-05-05 19:58:01 +053037static LIST_HEAD(omap_gpio_list);
38
Charulatha V6d62e212011-04-18 15:06:51 +000039struct gpio_regs {
40 u32 irqenable1;
41 u32 irqenable2;
42 u32 wake_en;
43 u32 ctrl;
44 u32 oe;
45 u32 leveldetect0;
46 u32 leveldetect1;
47 u32 risingdetect;
48 u32 fallingdetect;
49 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053050 u32 debounce;
51 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000052};
53
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010054struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053055 struct list_head node;
Tony Lindgren92105bb2005-09-07 17:20:26 +010056 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010057 u16 irq;
Benoit Cousson384ebe12011-08-16 11:53:02 +020058 int irq_base;
59 struct irq_domain *domain;
Tony Lindgren92105bb2005-09-07 17:20:26 +010060 u32 suspend_wakeup;
61 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080062 u32 non_wakeup_gpios;
63 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000064 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080065 u32 saved_datain;
66 u32 saved_fallingdetect;
67 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080068 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080069 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010070 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080071 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080072 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080073 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080074 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af2011-11-24 03:03:28 +053075 bool dbck_enabled;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080076 struct device *dev;
Charulatha Vd0d665a2011-08-31 00:02:21 +053077 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080078 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053079 bool loses_context;
Tony Lindgren5de62b82010-12-07 16:26:58 -080080 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070081 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053082 int context_loss_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053083 int power_mode;
84 bool workaround_enabled;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070085
86 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053087 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070088
89 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010090};
91
Kevin Hilman129fd222011-04-22 07:59:07 -070092#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
93#define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
Charulatha Vc8eef652011-05-02 15:21:42 +053094#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010095
Benoit Cousson25db7112012-02-23 21:50:10 +010096static int irq_to_gpio(struct gpio_bank *bank, unsigned int gpio_irq)
97{
98 return gpio_irq - bank->irq_base + bank->chip.base;
99}
100
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100101static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
102{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100103 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100104 u32 l;
105
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700106 reg += bank->regs->direction;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100107 l = __raw_readl(reg);
108 if (is_input)
109 l |= 1 << gpio;
110 else
111 l &= ~(1 << gpio);
112 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530113 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100114}
115
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700116
117/* set data out value using dedicate set/clear register */
118static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100119{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100120 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700121 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100122
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700123 if (enable)
124 reg += bank->regs->set_dataout;
125 else
126 reg += bank->regs->clr_dataout;
127
128 __raw_writel(l, reg);
129}
130
131/* set data out value using mask register */
132static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
133{
134 void __iomem *reg = bank->base + bank->regs->dataout;
135 u32 gpio_bit = GPIO_BIT(bank, gpio);
136 u32 l;
137
138 l = __raw_readl(reg);
139 if (enable)
140 l |= gpio_bit;
141 else
142 l &= ~gpio_bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100143 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530144 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100145}
146
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300147static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100148{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700149 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700151 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100152}
153
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300154static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
155{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700156 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300157
Kevin Hilman129fd222011-04-22 07:59:07 -0700158 return (__raw_readl(reg) & GPIO_BIT(bank, gpio)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300159}
160
Kevin Hilmanece95282011-07-12 08:18:15 -0700161static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
162{
163 int l = __raw_readl(base + reg);
164
Benoit Cousson862ff642012-02-01 15:58:56 +0100165 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700166 l |= mask;
167 else
168 l &= ~mask;
169
170 __raw_writel(l, base + reg);
171}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100172
Tarun Kanti DebBarma72f83af2011-11-24 03:03:28 +0530173static inline void _gpio_dbck_enable(struct gpio_bank *bank)
174{
175 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
176 clk_enable(bank->dbck);
177 bank->dbck_enabled = true;
178 }
179}
180
181static inline void _gpio_dbck_disable(struct gpio_bank *bank)
182{
183 if (bank->dbck_enable_mask && bank->dbck_enabled) {
184 clk_disable(bank->dbck);
185 bank->dbck_enabled = false;
186 }
187}
188
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700189/**
190 * _set_gpio_debounce - low level gpio debounce time
191 * @bank: the gpio bank we're acting upon
192 * @gpio: the gpio number on this @gpio
193 * @debounce: debounce time to use
194 *
195 * OMAP's debounce time is in 31us steps so we need
196 * to convert and round up to the closest unit.
197 */
198static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
199 unsigned debounce)
200{
Kevin Hilman9942da02011-04-22 12:02:05 -0700201 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700202 u32 val;
203 u32 l;
204
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800205 if (!bank->dbck_flag)
206 return;
207
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700208 if (debounce < 32)
209 debounce = 0x01;
210 else if (debounce > 7936)
211 debounce = 0xff;
212 else
213 debounce = (debounce / 0x1f) - 1;
214
Kevin Hilman129fd222011-04-22 07:59:07 -0700215 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700216
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530217 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700218 reg = bank->base + bank->regs->debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700219 __raw_writel(debounce, reg);
220
Kevin Hilman9942da02011-04-22 12:02:05 -0700221 reg = bank->base + bank->regs->debounce_en;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700222 val = __raw_readl(reg);
223
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530224 if (debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700225 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530226 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700227 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300228 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700229
230 __raw_writel(val, reg);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530231 clk_disable(bank->dbck);
232 /*
233 * Enable debounce clock per module.
234 * This call is mandatory because in omap_gpio_request() when
235 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
236 * runtime callbck fails to turn on dbck because dbck_enable_mask
237 * used within _gpio_dbck_enable() is still not initialized at
238 * that point. Therefore we have to enable dbck here.
239 */
240 _gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530241 if (bank->dbck_enable_mask) {
242 bank->context.debounce = debounce;
243 bank->context.debounce_en = val;
244 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700245}
246
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530247static inline void set_gpio_trigger(struct gpio_bank *bank, int gpio,
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700248 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100249{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800250 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100251 u32 gpio_bit = 1 << gpio;
252
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530253 _gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
254 trigger & IRQ_TYPE_LEVEL_LOW);
255 _gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
256 trigger & IRQ_TYPE_LEVEL_HIGH);
257 _gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
258 trigger & IRQ_TYPE_EDGE_RISING);
259 _gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
260 trigger & IRQ_TYPE_EDGE_FALLING);
261
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530262 bank->context.leveldetect0 =
263 __raw_readl(bank->base + bank->regs->leveldetect0);
264 bank->context.leveldetect1 =
265 __raw_readl(bank->base + bank->regs->leveldetect1);
266 bank->context.risingdetect =
267 __raw_readl(bank->base + bank->regs->risingdetect);
268 bank->context.fallingdetect =
269 __raw_readl(bank->base + bank->regs->fallingdetect);
270
271 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530272 _gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530273 bank->context.wake_en =
274 __raw_readl(bank->base + bank->regs->wkup_en);
275 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530276
Ambresh K55b220c2011-06-15 13:40:45 -0700277 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530278 if (!bank->regs->irqctrl) {
279 /* On omap24xx proceed only when valid GPIO bit is set */
280 if (bank->non_wakeup_gpios) {
281 if (!(bank->non_wakeup_gpios & gpio_bit))
282 goto exit;
283 }
284
Chunqiu Wang699117a2009-06-24 17:13:39 +0000285 /*
286 * Log the edge gpio and manually trigger the IRQ
287 * after resume if the input level changes
288 * to avoid irq lost during PER RET/OFF mode
289 * Applies for omap2 non-wakeup gpio and all omap3 gpios
290 */
291 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800292 bank->enabled_non_wakeup_gpios |= gpio_bit;
293 else
294 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
295 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700296
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530297exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530298 bank->level_mask =
299 __raw_readl(bank->base + bank->regs->leveldetect0) |
300 __raw_readl(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100301}
302
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800303#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800304/*
305 * This only applies to chips that can't do both rising and falling edge
306 * detection at once. For all other chips, this function is a noop.
307 */
308static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
309{
310 void __iomem *reg = bank->base;
311 u32 l = 0;
312
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530313 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800314 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530315
316 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800317
318 l = __raw_readl(reg);
319 if ((l >> gpio) & 1)
320 l &= ~(1 << gpio);
321 else
322 l |= 1 << gpio;
323
324 __raw_writel(l, reg);
325}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530326#else
327static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800328#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800329
Tony Lindgren92105bb2005-09-07 17:20:26 +0100330static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
331{
332 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530333 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100334 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100335
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530336 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
337 set_gpio_trigger(bank, gpio, trigger);
338 } else if (bank->regs->irqctrl) {
339 reg += bank->regs->irqctrl;
340
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100341 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000342 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800343 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100344 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100345 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100346 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100347 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100348 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530349 return -EINVAL;
350
351 __raw_writel(l, reg);
352 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100353 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530354 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100355 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530356 reg += bank->regs->edgectrl1;
357
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100358 gpio &= 0x07;
359 l = __raw_readl(reg);
360 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100361 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100362 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100363 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100364 l |= 1 << (gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530365
366 /* Enable wake-up during idle for dynamic tick */
367 _gpio_rmw(base, bank->regs->wkup_en, 1 << gpio, trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530368 bank->context.wake_en =
369 __raw_readl(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530370 __raw_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100371 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100372 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100373}
374
Lennert Buytenheke9191022010-11-29 11:17:17 +0100375static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100376{
Benoit Cousson25db7112012-02-23 21:50:10 +0100377 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100378 unsigned gpio;
379 int retval;
David Brownella6472532008-03-03 04:33:30 -0800380 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100381
Lennert Buytenheke9191022010-11-29 11:17:17 +0100382 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
383 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100384 else
Benoit Cousson25db7112012-02-23 21:50:10 +0100385 gpio = irq_to_gpio(bank, d->irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100386
David Brownelle5c56ed2006-12-06 17:13:59 -0800387 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100388 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800389
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530390 if (!bank->regs->leveldetect0 &&
391 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100392 return -EINVAL;
393
David Brownella6472532008-03-03 04:33:30 -0800394 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman129fd222011-04-22 07:59:07 -0700395 retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800396 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800397
398 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100399 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800400 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100401 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800402
Tony Lindgren92105bb2005-09-07 17:20:26 +0100403 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100404}
405
406static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
407{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100408 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100409
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700410 reg += bank->regs->irqstatus;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100411 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300412
413 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700414 if (bank->regs->irqstatus2) {
415 reg = bank->base + bank->regs->irqstatus2;
Roger Quadrosbedfd152009-04-23 11:10:50 -0700416 __raw_writel(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700417 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700418
419 /* Flush posted write for the irq status to avoid spurious interrupts */
420 __raw_readl(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100421}
422
423static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
424{
Kevin Hilman129fd222011-04-22 07:59:07 -0700425 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100426}
427
Imre Deakea6dedd2006-06-26 16:16:00 -0700428static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
429{
430 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700431 u32 l;
Kevin Hilmanc390aad2011-04-21 09:33:36 -0700432 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700433
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700434 reg += bank->regs->irqenable;
Imre Deak99c47702006-06-26 16:16:07 -0700435 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700436 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700437 l = ~l;
438 l &= mask;
439 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700440}
441
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700442static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100443{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100444 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100445 u32 l;
446
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700447 if (bank->regs->set_irqenable) {
448 reg += bank->regs->set_irqenable;
449 l = gpio_mask;
450 } else {
451 reg += bank->regs->irqenable;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100452 l = __raw_readl(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700453 if (bank->regs->irqenable_inv)
454 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100455 else
456 l |= gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100457 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700458
459 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530460 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700461}
462
463static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
464{
465 void __iomem *reg = bank->base;
466 u32 l;
467
468 if (bank->regs->clr_irqenable) {
469 reg += bank->regs->clr_irqenable;
470 l = gpio_mask;
471 } else {
472 reg += bank->regs->irqenable;
473 l = __raw_readl(reg);
474 if (bank->regs->irqenable_inv)
475 l |= gpio_mask;
476 else
477 l &= ~gpio_mask;
478 }
479
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100480 __raw_writel(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530481 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100482}
483
484static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
485{
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700486 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100487}
488
Tony Lindgren92105bb2005-09-07 17:20:26 +0100489/*
490 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
491 * 1510 does not seem to have a wake-up register. If JTAG is connected
492 * to the target, system will wake up always on GPIO events. While
493 * system is running all registered GPIO interrupts need to have wake-up
494 * enabled. When system is suspended, only selected GPIO interrupts need
495 * to have wake-up enabled.
496 */
497static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
498{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700499 u32 gpio_bit = GPIO_BIT(bank, gpio);
500 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800501
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700502 if (bank->non_wakeup_gpios & gpio_bit) {
Benoit Cousson862ff642012-02-01 15:58:56 +0100503 dev_err(bank->dev,
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700504 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100505 return -EINVAL;
506 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700507
508 spin_lock_irqsave(&bank->lock, flags);
509 if (enable)
510 bank->suspend_wakeup |= gpio_bit;
511 else
512 bank->suspend_wakeup &= ~gpio_bit;
513
Tarun Kanti DebBarma381a7522012-02-29 21:49:21 +0530514 __raw_writel(bank->suspend_wakeup, bank->base + bank->regs->wkup_en);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700515 spin_unlock_irqrestore(&bank->lock, flags);
516
517 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100518}
519
Tony Lindgren4196dd62006-09-25 12:41:38 +0300520static void _reset_gpio(struct gpio_bank *bank, int gpio)
521{
Kevin Hilman129fd222011-04-22 07:59:07 -0700522 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300523 _set_gpio_irqenable(bank, gpio, 0);
524 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700525 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300526}
527
Tony Lindgren92105bb2005-09-07 17:20:26 +0100528/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100529static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100530{
Benoit Cousson25db7112012-02-23 21:50:10 +0100531 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
532 unsigned int gpio = irq_to_gpio(bank, d->irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100533
Benoit Cousson25db7112012-02-23 21:50:10 +0100534 return _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100535}
536
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800537static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100538{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800539 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800540 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100541
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530542 /*
543 * If this is the first gpio_request for the bank,
544 * enable the bank module.
545 */
546 if (!bank->mod_usage)
547 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100548
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530549 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300550 /* Set trigger to none. You need to enable the desired trigger with
551 * request_irq() or set_irq_type().
552 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800553 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100554
Charulatha Vfad96ea2011-05-25 11:23:50 +0530555 if (bank->regs->pinctrl) {
556 void __iomem *reg = bank->base + bank->regs->pinctrl;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100557
Tony Lindgren92105bb2005-09-07 17:20:26 +0100558 /* Claim the pin for MPU */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800559 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100560 }
Charulatha Vfad96ea2011-05-25 11:23:50 +0530561
Charulatha Vc8eef652011-05-02 15:21:42 +0530562 if (bank->regs->ctrl && !bank->mod_usage) {
563 void __iomem *reg = bank->base + bank->regs->ctrl;
564 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -0700565
Charulatha Vc8eef652011-05-02 15:21:42 +0530566 ctrl = __raw_readl(reg);
567 /* Module is enabled, clocks are not gated */
568 ctrl &= ~GPIO_MOD_CTRL_BIT;
569 __raw_writel(ctrl, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530570 bank->context.ctrl = ctrl;
Charulatha V058af1e2009-11-22 10:11:25 -0800571 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530572
573 bank->mod_usage |= 1 << offset;
574
David Brownella6472532008-03-03 04:33:30 -0800575 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576
577 return 0;
578}
579
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800580static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100581{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800582 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530583 void __iomem *base = bank->base;
David Brownella6472532008-03-03 04:33:30 -0800584 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100585
David Brownella6472532008-03-03 04:33:30 -0800586 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530587
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530588 if (bank->regs->wkup_en) {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100589 /* Disable wake-up during idle for dynamic tick */
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530590 _gpio_rmw(base, bank->regs->wkup_en, 1 << offset, 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530591 bank->context.wake_en =
592 __raw_readl(bank->base + bank->regs->wkup_en);
593 }
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530594
Charulatha Vc8eef652011-05-02 15:21:42 +0530595 bank->mod_usage &= ~(1 << offset);
Charulatha V9f096862010-05-14 12:05:27 -0700596
Charulatha Vc8eef652011-05-02 15:21:42 +0530597 if (bank->regs->ctrl && !bank->mod_usage) {
598 void __iomem *reg = bank->base + bank->regs->ctrl;
599 u32 ctrl;
600
601 ctrl = __raw_readl(reg);
602 /* Module is disabled, clocks are gated */
603 ctrl |= GPIO_MOD_CTRL_BIT;
604 __raw_writel(ctrl, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530605 bank->context.ctrl = ctrl;
Charulatha V058af1e2009-11-22 10:11:25 -0800606 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530607
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800608 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800609 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530610
611 /*
612 * If this is the last gpio to be freed in the bank,
613 * disable the bank module.
614 */
615 if (!bank->mod_usage)
616 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100617}
618
619/*
620 * We need to unmask the GPIO bank interrupt as soon as possible to
621 * avoid missing GPIO interrupts for other lines in the bank.
622 * Then we need to mask-read-clear-unmask the triggered GPIO lines
623 * in the bank to avoid missing nested interrupts for a GPIO line.
624 * If we wait to unmask individual GPIO lines in the bank after the
625 * line's interrupt handler has been run, we may miss some nested
626 * interrupts.
627 */
Russell King10dd5ce2006-11-23 11:41:32 +0000628static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100629{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100630 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100631 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800632 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100633 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700634 u32 retrigger = 0;
635 int unmasked = 0;
Will Deaconee144182011-02-21 13:46:08 +0000636 struct irq_chip *chip = irq_desc_get_chip(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100637
Will Deaconee144182011-02-21 13:46:08 +0000638 chained_irq_enter(chip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100639
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100640 bank = irq_get_handler_data(irq);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700641 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530642 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800643
644 if (WARN_ON(!isr_reg))
645 goto exit;
646
Tony Lindgren92105bb2005-09-07 17:20:26 +0100647 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100648 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700649 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100650
Imre Deakea6dedd2006-06-26 16:16:00 -0700651 enabled = _get_gpio_irqbank_mask(bank);
652 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100653
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530654 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800655 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100656
657 /* clear edge sensitive interrupts before handler(s) are
658 called so that we don't miss any interrupt occurred while
659 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700660 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100661 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700662 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100663
664 /* if there is only edge sensitive GPIO pin interrupts
665 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700666 if (!level_mask && !unmasked) {
667 unmasked = 1;
Will Deaconee144182011-02-21 13:46:08 +0000668 chained_irq_exit(chip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700669 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670
Imre Deakea6dedd2006-06-26 16:16:00 -0700671 isr |= retrigger;
672 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100673 if (!isr)
674 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675
Benoit Cousson384ebe12011-08-16 11:53:02 +0200676 gpio_irq = bank->irq_base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100677 for (; isr != 0; isr >>= 1, gpio_irq++) {
Benoit Cousson25db7112012-02-23 21:50:10 +0100678 int gpio = irq_to_gpio(bank, gpio_irq);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800679
Tony Lindgren92105bb2005-09-07 17:20:26 +0100680 if (!(isr & 1))
681 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +0200682
Benoit Cousson25db7112012-02-23 21:50:10 +0100683 gpio_index = GPIO_INDEX(bank, gpio);
684
Cory Maccarrone4318f362010-01-08 10:29:04 -0800685 /*
686 * Some chips can't respond to both rising and falling
687 * at the same time. If this irq was requested with
688 * both flags, we need to flip the ICR data for the IRQ
689 * to respond to the IRQ for the opposite direction.
690 * This will be indicated in the bank toggle_mask.
691 */
692 if (bank->toggle_mask & (1 << gpio_index))
693 _toggle_gpio_edge_triggering(bank, gpio_index);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800694
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100695 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100696 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000697 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700698 /* if bank has any level sensitive GPIO pin interrupt
699 configured, we must unmask the bank interrupt only after
700 handler(s) are executed in order to avoid spurious bank
701 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800702exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700703 if (!unmasked)
Will Deaconee144182011-02-21 13:46:08 +0000704 chained_irq_exit(chip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530705 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100706}
707
Lennert Buytenheke9191022010-11-29 11:17:17 +0100708static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300709{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100710 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Benoit Cousson25db7112012-02-23 21:50:10 +0100711 unsigned int gpio = irq_to_gpio(bank, d->irq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700712 unsigned long flags;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300713
Colin Cross85ec7b92011-06-06 13:38:18 -0700714 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300715 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700716 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300717}
718
Lennert Buytenheke9191022010-11-29 11:17:17 +0100719static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100720{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100721 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Benoit Cousson25db7112012-02-23 21:50:10 +0100722 unsigned int gpio = irq_to_gpio(bank, d->irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723
724 _clear_gpio_irqstatus(bank, gpio);
725}
726
Lennert Buytenheke9191022010-11-29 11:17:17 +0100727static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100728{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100729 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Benoit Cousson25db7112012-02-23 21:50:10 +0100730 unsigned int gpio = irq_to_gpio(bank, d->irq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700731 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732
Colin Cross85ec7b92011-06-06 13:38:18 -0700733 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100734 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700735 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700736 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100737}
738
Lennert Buytenheke9191022010-11-29 11:17:17 +0100739static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740{
Lennert Buytenheke9191022010-11-29 11:17:17 +0100741 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Benoit Cousson25db7112012-02-23 21:50:10 +0100742 unsigned int gpio = irq_to_gpio(bank, d->irq);
Kevin Hilman129fd222011-04-22 07:59:07 -0700743 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100744 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700745 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700746
Colin Cross85ec7b92011-06-06 13:38:18 -0700747 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700748 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700749 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800750
751 /* For level-triggered GPIOs, the clearing must be done after
752 * the HW source is cleared, thus after the handler has run */
753 if (bank->level_mask & irq_mask) {
754 _set_gpio_irqenable(bank, gpio, 0);
755 _clear_gpio_irqstatus(bank, gpio);
756 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100757
Kevin Hilman4de8c752008-01-16 21:56:14 -0800758 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700759 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100760}
761
David Brownelle5c56ed2006-12-06 17:13:59 -0800762static struct irq_chip gpio_irq_chip = {
763 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100764 .irq_shutdown = gpio_irq_shutdown,
765 .irq_ack = gpio_ack_irq,
766 .irq_mask = gpio_mask_irq,
767 .irq_unmask = gpio_unmask_irq,
768 .irq_set_type = gpio_irq_type,
769 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800770};
771
772/*---------------------------------------------------------------------*/
773
Magnus Damm79ee0312009-07-08 13:22:04 +0200774static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800775{
Magnus Damm79ee0312009-07-08 13:22:04 +0200776 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800777 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800778 void __iomem *mask_reg = bank->base +
779 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800780 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800781
David Brownella6472532008-03-03 04:33:30 -0800782 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800783 bank->saved_wakeup = __raw_readl(mask_reg);
784 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800785 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800786
787 return 0;
788}
789
Magnus Damm79ee0312009-07-08 13:22:04 +0200790static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800791{
Magnus Damm79ee0312009-07-08 13:22:04 +0200792 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800793 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800794 void __iomem *mask_reg = bank->base +
795 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800796 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800797
David Brownella6472532008-03-03 04:33:30 -0800798 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800799 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800800 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800801
802 return 0;
803}
804
Alexey Dobriyan47145212009-12-14 18:00:08 -0800805static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200806 .suspend_noirq = omap_mpuio_suspend_noirq,
807 .resume_noirq = omap_mpuio_resume_noirq,
808};
809
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200810/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800811static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800812 .driver = {
813 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200814 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800815 },
816};
817
818static struct platform_device omap_mpuio_device = {
819 .name = "mpuio",
820 .id = -1,
821 .dev = {
822 .driver = &omap_mpuio_driver.driver,
823 }
824 /* could list the /proc/iomem resources */
825};
826
Charulatha V03e128c2011-05-05 19:58:01 +0530827static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800828{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800829 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700830
David Brownell11a78b72006-12-06 17:14:11 -0800831 if (platform_driver_register(&omap_mpuio_driver) == 0)
832 (void) platform_device_register(&omap_mpuio_device);
833}
834
David Brownelle5c56ed2006-12-06 17:13:59 -0800835/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100836
David Brownell52e31342008-03-03 12:43:23 -0800837static int gpio_input(struct gpio_chip *chip, unsigned offset)
838{
839 struct gpio_bank *bank;
840 unsigned long flags;
841
842 bank = container_of(chip, struct gpio_bank, chip);
843 spin_lock_irqsave(&bank->lock, flags);
844 _set_gpio_direction(bank, offset, 1);
845 spin_unlock_irqrestore(&bank->lock, flags);
846 return 0;
847}
848
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300849static int gpio_is_input(struct gpio_bank *bank, int mask)
850{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700851 void __iomem *reg = bank->base + bank->regs->direction;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300852
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300853 return __raw_readl(reg) & mask;
854}
855
David Brownell52e31342008-03-03 12:43:23 -0800856static int gpio_get(struct gpio_chip *chip, unsigned offset)
857{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300858 struct gpio_bank *bank;
859 void __iomem *reg;
860 int gpio;
861 u32 mask;
862
863 gpio = chip->base + offset;
Charulatha Va8be8da2011-04-22 16:38:16 +0530864 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300865 reg = bank->base;
Kevin Hilman129fd222011-04-22 07:59:07 -0700866 mask = GPIO_BIT(bank, gpio);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300867
868 if (gpio_is_input(bank, mask))
869 return _get_gpio_datain(bank, gpio);
870 else
871 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -0800872}
873
874static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
875{
876 struct gpio_bank *bank;
877 unsigned long flags;
878
879 bank = container_of(chip, struct gpio_bank, chip);
880 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700881 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800882 _set_gpio_direction(bank, offset, 0);
883 spin_unlock_irqrestore(&bank->lock, flags);
884 return 0;
885}
886
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700887static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
888 unsigned debounce)
889{
890 struct gpio_bank *bank;
891 unsigned long flags;
892
893 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800894
895 if (!bank->dbck) {
896 bank->dbck = clk_get(bank->dev, "dbclk");
897 if (IS_ERR(bank->dbck))
898 dev_err(bank->dev, "Could not get gpio dbck\n");
899 }
900
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700901 spin_lock_irqsave(&bank->lock, flags);
902 _set_gpio_debounce(bank, offset, debounce);
903 spin_unlock_irqrestore(&bank->lock, flags);
904
905 return 0;
906}
907
David Brownell52e31342008-03-03 12:43:23 -0800908static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
909{
910 struct gpio_bank *bank;
911 unsigned long flags;
912
913 bank = container_of(chip, struct gpio_bank, chip);
914 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700915 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800916 spin_unlock_irqrestore(&bank->lock, flags);
917}
918
David Brownella007b702008-12-10 17:35:25 -0800919static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
920{
921 struct gpio_bank *bank;
922
923 bank = container_of(chip, struct gpio_bank, chip);
Benoit Cousson384ebe12011-08-16 11:53:02 +0200924 return bank->irq_base + offset;
David Brownella007b702008-12-10 17:35:25 -0800925}
926
David Brownell52e31342008-03-03 12:43:23 -0800927/*---------------------------------------------------------------------*/
928
Tony Lindgren9a748052010-12-07 16:26:56 -0800929static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700930{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700931 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700932 u32 rev;
933
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700934 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700935 return;
936
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700937 rev = __raw_readw(bank->base + bank->regs->revision);
938 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700939 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700940
941 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700942}
943
David Brownell8ba55c52008-02-26 11:10:50 -0800944/* This lock class tells lockdep that GPIO irqs are in a different
945 * category than their parents, so it won't report false recursion.
946 */
947static struct lock_class_key gpio_lock_class;
948
Charulatha V03e128c2011-05-05 19:58:01 +0530949static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800950{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530951 void __iomem *base = bank->base;
952 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800953
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530954 if (bank->width == 16)
955 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800956
Charulatha Vd0d665a2011-08-31 00:02:21 +0530957 if (bank->is_mpuio) {
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530958 __raw_writel(l, bank->base + bank->regs->irqenable);
959 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800960 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530961
962 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->irqenable_inv);
963 _gpio_rmw(base, bank->regs->irqstatus, l,
964 bank->regs->irqenable_inv == false);
965 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->debounce_en != 0);
966 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->ctrl != 0);
967 if (bank->regs->debounce_en)
968 _gpio_rmw(base, bank->regs->debounce_en, 0, 1);
969
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +0530970 /* Save OE default value (0xffffffff) in the context */
971 bank->context.oe = __raw_readl(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530972 /* Initialize interface clk ungated, module enabled */
973 if (bank->regs->ctrl)
974 _gpio_rmw(base, bank->regs->ctrl, 0, 1);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800975}
976
Tony Lindgren8805f412012-03-05 15:32:38 -0800977static __devinit void
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700978omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
979 unsigned int num)
980{
981 struct irq_chip_generic *gc;
982 struct irq_chip_type *ct;
983
984 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
985 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -0700986 if (!gc) {
987 dev_err(bank->dev, "Memory alloc failed for gc\n");
988 return;
989 }
990
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700991 ct = gc->chip_types;
992
993 /* NOTE: No ack required, reading IRQ status clears it. */
994 ct->chip.irq_mask = irq_gc_mask_set_bit;
995 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
996 ct->chip.irq_set_type = gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +0530997
998 if (bank->regs->wkup_en)
Kevin Hilmanf8b46b52011-04-21 13:23:34 -0700999 ct->chip.irq_set_wake = gpio_wake_enable,
1000
1001 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1002 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1003 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1004}
1005
Russell Kingd52b31d2011-05-27 13:56:12 -07001006static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001007{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001008 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001009 static int gpio;
1010
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001011 /*
1012 * REVISIT eventually switch from OMAP-specific gpio structs
1013 * over to the generic ones
1014 */
1015 bank->chip.request = omap_gpio_request;
1016 bank->chip.free = omap_gpio_free;
1017 bank->chip.direction_input = gpio_input;
1018 bank->chip.get = gpio_get;
1019 bank->chip.direction_output = gpio_output;
1020 bank->chip.set_debounce = gpio_debounce;
1021 bank->chip.set = gpio_set;
1022 bank->chip.to_irq = gpio_2irq;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301023 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001024 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301025 if (bank->regs->wkup_en)
1026 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001027 bank->chip.base = OMAP_MPUIO(0);
1028 } else {
1029 bank->chip.label = "gpio";
1030 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001031 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001032 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001033 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001034
1035 gpiochip_add(&bank->chip);
1036
Benoit Cousson384ebe12011-08-16 11:53:02 +02001037 for (j = bank->irq_base; j < bank->irq_base + bank->width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001038 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001039 irq_set_chip_data(j, bank);
Charulatha Vd0d665a2011-08-31 00:02:21 +05301040 if (bank->is_mpuio) {
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001041 omap_mpuio_alloc_gc(bank, j, bank->width);
1042 } else {
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001043 irq_set_chip(j, &gpio_irq_chip);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001044 irq_set_handler(j, handle_simple_irq);
1045 set_irq_flags(j, IRQF_VALID);
1046 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001047 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001048 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1049 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001050}
1051
Benoit Cousson384ebe12011-08-16 11:53:02 +02001052static const struct of_device_id omap_gpio_match[];
1053
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001054static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001055{
Benoit Cousson862ff642012-02-01 15:58:56 +01001056 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001057 struct device_node *node = dev->of_node;
1058 const struct of_device_id *match;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001059 struct omap_gpio_platform_data *pdata;
1060 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001061 struct gpio_bank *bank;
Charulatha V03e128c2011-05-05 19:58:01 +05301062 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001063
Benoit Cousson384ebe12011-08-16 11:53:02 +02001064 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1065
1066 pdata = match ? match->data : dev->platform_data;
1067 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001068 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001069
Benoit Cousson96751fc2012-02-01 16:01:39 +01001070 bank = devm_kzalloc(&pdev->dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301071 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001072 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001073 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301074 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001075
1076 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1077 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001078 dev_err(dev, "Invalid IRQ resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001079 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001080 }
1081
1082 bank->irq = res->start;
Benoit Cousson862ff642012-02-01 15:58:56 +01001083 bank->dev = dev;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001084 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001085 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001086 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301087 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301088 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Charulatha V0cde8d02011-05-05 20:15:16 +05301089 bank->loses_context = pdata->loses_context;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301090 bank->get_context_loss_count = pdata->get_context_loss_count;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001091 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001092#ifdef CONFIG_OF_GPIO
1093 bank->chip.of_node = of_node_get(node);
1094#endif
1095
1096 bank->irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
1097 if (bank->irq_base < 0) {
1098 dev_err(dev, "Couldn't allocate IRQ numbers\n");
1099 return -ENODEV;
1100 }
1101
1102 bank->domain = irq_domain_add_legacy(node, bank->width, bank->irq_base,
1103 0, &irq_domain_simple_ops, NULL);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001104
1105 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1106 bank->set_dataout = _set_gpio_dataout_reg;
1107 else
1108 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001109
1110 spin_lock_init(&bank->lock);
1111
1112 /* Static mapping, never released */
1113 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1114 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001115 dev_err(dev, "Invalid mem resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001116 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001117 }
1118
Benoit Cousson96751fc2012-02-01 16:01:39 +01001119 if (!devm_request_mem_region(dev, res->start, resource_size(res),
1120 pdev->name)) {
1121 dev_err(dev, "Region already claimed\n");
1122 return -EBUSY;
1123 }
1124
1125 bank->base = devm_ioremap(dev, res->start, resource_size(res));
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001126 if (!bank->base) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001127 dev_err(dev, "Could not ioremap\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001128 return -ENOMEM;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001129 }
1130
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301131 platform_set_drvdata(pdev, bank);
1132
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001133 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301134 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001135 pm_runtime_get_sync(bank->dev);
1136
Charulatha Vd0d665a2011-08-31 00:02:21 +05301137 if (bank->is_mpuio)
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301138 mpuio_init(bank);
1139
Charulatha V03e128c2011-05-05 19:58:01 +05301140 omap_gpio_mod_init(bank);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001141 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001142 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001143
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301144 pm_runtime_put(bank->dev);
1145
Charulatha V03e128c2011-05-05 19:58:01 +05301146 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001147
Charulatha V03e128c2011-05-05 19:58:01 +05301148 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001149}
1150
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301151#ifdef CONFIG_ARCH_OMAP2PLUS
1152
1153#if defined(CONFIG_PM_SLEEP)
1154static int omap_gpio_suspend(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001155{
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301156 struct platform_device *pdev = to_platform_device(dev);
1157 struct gpio_bank *bank = platform_get_drvdata(pdev);
1158 void __iomem *base = bank->base;
1159 void __iomem *wakeup_enable;
1160 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001161
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301162 if (!bank->mod_usage || !bank->loses_context)
1163 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001164
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301165 if (!bank->regs->wkup_en || !bank->suspend_wakeup)
1166 return 0;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301167
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301168 wakeup_enable = bank->base + bank->regs->wkup_en;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001169
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301170 spin_lock_irqsave(&bank->lock, flags);
1171 bank->saved_wakeup = __raw_readl(wakeup_enable);
1172 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1173 _gpio_rmw(base, bank->regs->wkup_en, bank->suspend_wakeup, 1);
1174 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001175
1176 return 0;
1177}
1178
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301179static int omap_gpio_resume(struct device *dev)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001180{
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301181 struct platform_device *pdev = to_platform_device(dev);
1182 struct gpio_bank *bank = platform_get_drvdata(pdev);
1183 void __iomem *base = bank->base;
1184 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001185
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301186 if (!bank->mod_usage || !bank->loses_context)
1187 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001188
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301189 if (!bank->regs->wkup_en || !bank->saved_wakeup)
1190 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001191
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301192 spin_lock_irqsave(&bank->lock, flags);
1193 _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
1194 _gpio_rmw(base, bank->regs->wkup_en, bank->saved_wakeup, 1);
1195 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301196
1197 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001198}
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301199#endif /* CONFIG_PM_SLEEP */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001200
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301201#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301202static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001203
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301204static int omap_gpio_runtime_suspend(struct device *dev)
1205{
1206 struct platform_device *pdev = to_platform_device(dev);
1207 struct gpio_bank *bank = platform_get_drvdata(pdev);
1208 u32 l1 = 0, l2 = 0;
1209 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001210 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301211
1212 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001213
1214 /*
1215 * Only edges can generate a wakeup event to the PRCM.
1216 *
1217 * Therefore, ensure any wake-up capable GPIOs have
1218 * edge-detection enabled before going idle to ensure a wakeup
1219 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1220 * NDA TRM 25.5.3.1)
1221 *
1222 * The normal values will be restored upon ->runtime_resume()
1223 * by writing back the values saved in bank->context.
1224 */
1225 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1226 if (wake_low)
1227 __raw_writel(wake_low | bank->context.fallingdetect,
1228 bank->base + bank->regs->fallingdetect);
1229 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1230 if (wake_hi)
1231 __raw_writel(wake_hi | bank->context.risingdetect,
1232 bank->base + bank->regs->risingdetect);
1233
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301234 if (bank->power_mode != OFF_MODE) {
1235 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301236 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301237 }
1238 /*
1239 * If going to OFF, remove triggering for all
1240 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1241 * generated. See OMAP2420 Errata item 1.101.
1242 */
1243 if (!(bank->enabled_non_wakeup_gpios))
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301244 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301245
1246 bank->saved_datain = __raw_readl(bank->base +
1247 bank->regs->datain);
1248 l1 = __raw_readl(bank->base + bank->regs->fallingdetect);
1249 l2 = __raw_readl(bank->base + bank->regs->risingdetect);
1250
1251 bank->saved_fallingdetect = l1;
1252 bank->saved_risingdetect = l2;
1253 l1 &= ~bank->enabled_non_wakeup_gpios;
1254 l2 &= ~bank->enabled_non_wakeup_gpios;
1255
1256 __raw_writel(l1, bank->base + bank->regs->fallingdetect);
1257 __raw_writel(l2, bank->base + bank->regs->risingdetect);
1258
1259 bank->workaround_enabled = true;
1260
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301261update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301262 if (bank->get_context_loss_count)
1263 bank->context_loss_count =
1264 bank->get_context_loss_count(bank->dev);
1265
Tarun Kanti DebBarma72f83af2011-11-24 03:03:28 +05301266 _gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301267 spin_unlock_irqrestore(&bank->lock, flags);
1268
1269 return 0;
1270}
1271
1272static int omap_gpio_runtime_resume(struct device *dev)
1273{
1274 struct platform_device *pdev = to_platform_device(dev);
1275 struct gpio_bank *bank = platform_get_drvdata(pdev);
1276 int context_lost_cnt_after;
1277 u32 l = 0, gen, gen0, gen1;
1278 unsigned long flags;
1279
1280 spin_lock_irqsave(&bank->lock, flags);
Tarun Kanti DebBarma72f83af2011-11-24 03:03:28 +05301281 _gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001282
1283 /*
1284 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1285 * GPIOs were set to edge trigger also in order to be able to
1286 * generate a PRCM wakeup. Here we restore the
1287 * pre-runtime_suspend() values for edge triggering.
1288 */
1289 __raw_writel(bank->context.fallingdetect,
1290 bank->base + bank->regs->fallingdetect);
1291 __raw_writel(bank->context.risingdetect,
1292 bank->base + bank->regs->risingdetect);
1293
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301294 if (!bank->enabled_non_wakeup_gpios || !bank->workaround_enabled) {
1295 spin_unlock_irqrestore(&bank->lock, flags);
1296 return 0;
1297 }
1298
1299 if (bank->get_context_loss_count) {
1300 context_lost_cnt_after =
1301 bank->get_context_loss_count(bank->dev);
1302 if (context_lost_cnt_after != bank->context_loss_count ||
1303 !context_lost_cnt_after) {
1304 omap_gpio_restore_context(bank);
1305 } else {
1306 spin_unlock_irqrestore(&bank->lock, flags);
1307 return 0;
1308 }
1309 }
1310
1311 __raw_writel(bank->saved_fallingdetect,
1312 bank->base + bank->regs->fallingdetect);
1313 __raw_writel(bank->saved_risingdetect,
1314 bank->base + bank->regs->risingdetect);
1315 l = __raw_readl(bank->base + bank->regs->datain);
1316
1317 /*
1318 * Check if any of the non-wakeup interrupt GPIOs have changed
1319 * state. If so, generate an IRQ by software. This is
1320 * horribly racy, but it's the best we can do to work around
1321 * this silicon bug.
1322 */
1323 l ^= bank->saved_datain;
1324 l &= bank->enabled_non_wakeup_gpios;
1325
1326 /*
1327 * No need to generate IRQs for the rising edge for gpio IRQs
1328 * configured with falling edge only; and vice versa.
1329 */
1330 gen0 = l & bank->saved_fallingdetect;
1331 gen0 &= bank->saved_datain;
1332
1333 gen1 = l & bank->saved_risingdetect;
1334 gen1 &= ~(bank->saved_datain);
1335
1336 /* FIXME: Consider GPIO IRQs with level detections properly! */
1337 gen = l & (~(bank->saved_fallingdetect) & ~(bank->saved_risingdetect));
1338 /* Consider all GPIO IRQs needed to be updated */
1339 gen |= gen0 | gen1;
1340
1341 if (gen) {
1342 u32 old0, old1;
1343
1344 old0 = __raw_readl(bank->base + bank->regs->leveldetect0);
1345 old1 = __raw_readl(bank->base + bank->regs->leveldetect1);
1346
1347 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1348 __raw_writel(old0 | gen, bank->base +
1349 bank->regs->leveldetect0);
1350 __raw_writel(old1 | gen, bank->base +
1351 bank->regs->leveldetect1);
1352 }
1353
1354 if (cpu_is_omap44xx()) {
1355 __raw_writel(old0 | l, bank->base +
1356 bank->regs->leveldetect0);
1357 __raw_writel(old1 | l, bank->base +
1358 bank->regs->leveldetect1);
1359 }
1360 __raw_writel(old0, bank->base + bank->regs->leveldetect0);
1361 __raw_writel(old1, bank->base + bank->regs->leveldetect1);
1362 }
1363
1364 bank->workaround_enabled = false;
1365 spin_unlock_irqrestore(&bank->lock, flags);
1366
1367 return 0;
1368}
1369#endif /* CONFIG_PM_RUNTIME */
1370
1371void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001372{
Charulatha V03e128c2011-05-05 19:58:01 +05301373 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001374
Charulatha V03e128c2011-05-05 19:58:01 +05301375 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301376 if (!bank->mod_usage || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301377 continue;
1378
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301379 bank->power_mode = pwr_mode;
1380
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301381 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001382 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001383}
1384
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001385void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001386{
Charulatha V03e128c2011-05-05 19:58:01 +05301387 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001388
Charulatha V03e128c2011-05-05 19:58:01 +05301389 list_for_each_entry(bank, &omap_gpio_list, node) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301390 if (!bank->mod_usage || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301391 continue;
1392
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301393 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001394 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001395}
1396
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301397#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301398static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301399{
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301400 __raw_writel(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301401 bank->base + bank->regs->wkup_en);
1402 __raw_writel(bank->context.ctrl, bank->base + bank->regs->ctrl);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301403 __raw_writel(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301404 bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301405 __raw_writel(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301406 bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301407 __raw_writel(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301408 bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301409 __raw_writel(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301410 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301411 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1412 __raw_writel(bank->context.dataout,
1413 bank->base + bank->regs->set_dataout);
1414 else
1415 __raw_writel(bank->context.dataout,
1416 bank->base + bank->regs->dataout);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301417 __raw_writel(bank->context.oe, bank->base + bank->regs->direction);
1418
Nishanth Menonae547352011-09-09 19:08:58 +05301419 if (bank->dbck_enable_mask) {
1420 __raw_writel(bank->context.debounce, bank->base +
1421 bank->regs->debounce);
1422 __raw_writel(bank->context.debounce_en,
1423 bank->base + bank->regs->debounce_en);
1424 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301425
1426 __raw_writel(bank->context.irqenable1,
1427 bank->base + bank->regs->irqenable);
1428 __raw_writel(bank->context.irqenable2,
1429 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301430}
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301431#endif /* CONFIG_PM_RUNTIME */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301432#else
1433#define omap_gpio_suspend NULL
1434#define omap_gpio_resume NULL
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301435#define omap_gpio_runtime_suspend NULL
1436#define omap_gpio_runtime_resume NULL
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301437#endif
1438
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301439static const struct dev_pm_ops gpio_pm_ops = {
1440 SET_SYSTEM_SLEEP_PM_OPS(omap_gpio_suspend, omap_gpio_resume)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301441 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1442 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301443};
1444
Benoit Cousson384ebe12011-08-16 11:53:02 +02001445#if defined(CONFIG_OF)
1446static struct omap_gpio_reg_offs omap2_gpio_regs = {
1447 .revision = OMAP24XX_GPIO_REVISION,
1448 .direction = OMAP24XX_GPIO_OE,
1449 .datain = OMAP24XX_GPIO_DATAIN,
1450 .dataout = OMAP24XX_GPIO_DATAOUT,
1451 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1452 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1453 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1454 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1455 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1456 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1457 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1458 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1459 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1460 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1461 .ctrl = OMAP24XX_GPIO_CTRL,
1462 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1463 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1464 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1465 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1466 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1467};
1468
1469static struct omap_gpio_reg_offs omap4_gpio_regs = {
1470 .revision = OMAP4_GPIO_REVISION,
1471 .direction = OMAP4_GPIO_OE,
1472 .datain = OMAP4_GPIO_DATAIN,
1473 .dataout = OMAP4_GPIO_DATAOUT,
1474 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1475 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1476 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1477 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1478 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1479 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1480 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1481 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1482 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1483 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1484 .ctrl = OMAP4_GPIO_CTRL,
1485 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1486 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1487 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1488 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1489 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1490};
1491
1492static struct omap_gpio_platform_data omap2_pdata = {
1493 .regs = &omap2_gpio_regs,
1494 .bank_width = 32,
1495 .dbck_flag = false,
1496};
1497
1498static struct omap_gpio_platform_data omap3_pdata = {
1499 .regs = &omap2_gpio_regs,
1500 .bank_width = 32,
1501 .dbck_flag = true,
1502};
1503
1504static struct omap_gpio_platform_data omap4_pdata = {
1505 .regs = &omap4_gpio_regs,
1506 .bank_width = 32,
1507 .dbck_flag = true,
1508};
1509
1510static const struct of_device_id omap_gpio_match[] = {
1511 {
1512 .compatible = "ti,omap4-gpio",
1513 .data = &omap4_pdata,
1514 },
1515 {
1516 .compatible = "ti,omap3-gpio",
1517 .data = &omap3_pdata,
1518 },
1519 {
1520 .compatible = "ti,omap2-gpio",
1521 .data = &omap2_pdata,
1522 },
1523 { },
1524};
1525MODULE_DEVICE_TABLE(of, omap_gpio_match);
1526#endif
1527
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001528static struct platform_driver omap_gpio_driver = {
1529 .probe = omap_gpio_probe,
1530 .driver = {
1531 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301532 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001533 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001534 },
1535};
1536
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001537/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001538 * gpio driver register needs to be done before
1539 * machine_init functions access gpio APIs.
1540 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001541 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001542static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001543{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001544 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001545}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001546postcore_initcall(omap_gpio_drv_reg);