blob: 8cc9e91e7e4e8ffe480ca4ec1d9a1bd2182185dd [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>
Catalin Marinasde88cbb2013-01-18 15:31:37 +000027#include <linux/irqchip/chained_irq.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070028#include <linux/gpio.h>
29#include <linux/platform_data/gpio-omap.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010030
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053031#define OFF_MODE 1
32
Charulatha V03e128c2011-05-05 19:58:01 +053033static LIST_HEAD(omap_gpio_list);
34
Charulatha V6d62e212011-04-18 15:06:51 +000035struct gpio_regs {
36 u32 irqenable1;
37 u32 irqenable2;
38 u32 wake_en;
39 u32 ctrl;
40 u32 oe;
41 u32 leveldetect0;
42 u32 leveldetect1;
43 u32 risingdetect;
44 u32 fallingdetect;
45 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053046 u32 debounce;
47 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000048};
49
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010050struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053051 struct list_head node;
Tony Lindgren92105bb2005-09-07 17:20:26 +010052 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010053 u16 irq;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080054 u32 non_wakeup_gpios;
55 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000056 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080057 u32 saved_datain;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080058 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080059 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010060 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080061 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080062 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080063 u32 mod_usage;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020064 u32 irq_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080065 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +053066 bool dbck_enabled;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080067 struct device *dev;
Charulatha Vd0d665a2011-08-31 00:02:21 +053068 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080069 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053070 bool loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -050071 bool context_valid;
Tony Lindgren5de62b82010-12-07 16:26:58 -080072 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070073 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053074 int context_loss_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053075 int power_mode;
76 bool workaround_enabled;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070077
78 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053079 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070080
81 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010082};
83
Kevin Hilman129fd222011-04-22 07:59:07 -070084#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
85#define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
Charulatha Vc8eef652011-05-02 15:21:42 +053086#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010087
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020088#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
89#define LINE_USED(line, offset) (line & (1 << offset))
90
Benoit Cousson25db7112012-02-23 21:50:10 +010091static int irq_to_gpio(struct gpio_bank *bank, unsigned int gpio_irq)
92{
Jon Hunterede4d7a2013-03-01 11:22:47 -060093 return bank->chip.base + gpio_irq;
94}
95
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +020096static inline struct gpio_bank *_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -060097{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +020098 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
99 return container_of(chip, struct gpio_bank, chip);
Benoit Cousson25db7112012-02-23 21:50:10 +0100100}
101
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100102static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
103{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100104 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100105 u32 l;
106
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700107 reg += bank->regs->direction;
Victor Kamensky661553b2013-11-16 02:01:04 +0200108 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100109 if (is_input)
110 l |= 1 << gpio;
111 else
112 l &= ~(1 << gpio);
Victor Kamensky661553b2013-11-16 02:01:04 +0200113 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530114 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100115}
116
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700117
118/* set data out value using dedicate set/clear register */
119static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100120{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100121 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700122 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100123
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530124 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700125 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530126 bank->context.dataout |= l;
127 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700128 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530129 bank->context.dataout &= ~l;
130 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700131
Victor Kamensky661553b2013-11-16 02:01:04 +0200132 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700133}
134
135/* set data out value using mask register */
136static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
137{
138 void __iomem *reg = bank->base + bank->regs->dataout;
139 u32 gpio_bit = GPIO_BIT(bank, gpio);
140 u32 l;
141
Victor Kamensky661553b2013-11-16 02:01:04 +0200142 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700143 if (enable)
144 l |= gpio_bit;
145 else
146 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200147 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530148 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100149}
150
Tarun Kanti DebBarma7fcca712012-02-27 11:46:09 +0530151static int _get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100152{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700153 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100154
Victor Kamensky661553b2013-11-16 02:01:04 +0200155 return (readl_relaxed(reg) & (1 << offset)) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100156}
157
Tarun Kanti DebBarma7fcca712012-02-27 11:46:09 +0530158static int _get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300159{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700160 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300161
Victor Kamensky661553b2013-11-16 02:01:04 +0200162 return (readl_relaxed(reg) & (1 << offset)) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300163}
164
Kevin Hilmanece95282011-07-12 08:18:15 -0700165static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
166{
Victor Kamensky661553b2013-11-16 02:01:04 +0200167 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700168
Benoit Cousson862ff642012-02-01 15:58:56 +0100169 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700170 l |= mask;
171 else
172 l &= ~mask;
173
Victor Kamensky661553b2013-11-16 02:01:04 +0200174 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700175}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100176
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530177static inline void _gpio_dbck_enable(struct gpio_bank *bank)
178{
179 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
180 clk_enable(bank->dbck);
181 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300182
Victor Kamensky661553b2013-11-16 02:01:04 +0200183 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300184 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530185 }
186}
187
188static inline void _gpio_dbck_disable(struct gpio_bank *bank)
189{
190 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300191 /*
192 * Disable debounce before cutting it's clock. If debounce is
193 * enabled but the clock is not, GPIO module seems to be unable
194 * to detect events and generate interrupts at least on OMAP3.
195 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200196 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300197
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530198 clk_disable(bank->dbck);
199 bank->dbck_enabled = false;
200 }
201}
202
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700203/**
204 * _set_gpio_debounce - low level gpio debounce time
205 * @bank: the gpio bank we're acting upon
206 * @gpio: the gpio number on this @gpio
207 * @debounce: debounce time to use
208 *
209 * OMAP's debounce time is in 31us steps so we need
210 * to convert and round up to the closest unit.
211 */
212static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
213 unsigned debounce)
214{
Kevin Hilman9942da02011-04-22 12:02:05 -0700215 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700216 u32 val;
217 u32 l;
218
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800219 if (!bank->dbck_flag)
220 return;
221
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700222 if (debounce < 32)
223 debounce = 0x01;
224 else if (debounce > 7936)
225 debounce = 0xff;
226 else
227 debounce = (debounce / 0x1f) - 1;
228
Kevin Hilman129fd222011-04-22 07:59:07 -0700229 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700230
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530231 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700232 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200233 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700234
Kevin Hilman9942da02011-04-22 12:02:05 -0700235 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200236 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700237
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530238 if (debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700239 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530240 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700241 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300242 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700243
Victor Kamensky661553b2013-11-16 02:01:04 +0200244 writel_relaxed(val, reg);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530245 clk_disable(bank->dbck);
246 /*
247 * Enable debounce clock per module.
248 * This call is mandatory because in omap_gpio_request() when
249 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
250 * runtime callbck fails to turn on dbck because dbck_enable_mask
251 * used within _gpio_dbck_enable() is still not initialized at
252 * that point. Therefore we have to enable dbck here.
253 */
254 _gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530255 if (bank->dbck_enable_mask) {
256 bank->context.debounce = debounce;
257 bank->context.debounce_en = val;
258 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700259}
260
Jon Hunterc9c55d92012-10-26 14:26:04 -0500261/**
262 * _clear_gpio_debounce - clear debounce settings for a gpio
263 * @bank: the gpio bank we're acting upon
264 * @gpio: the gpio number on this @gpio
265 *
266 * If a gpio is using debounce, then clear the debounce enable bit and if
267 * this is the only gpio in this bank using debounce, then clear the debounce
268 * time too. The debounce clock will also be disabled when calling this function
269 * if this is the only gpio in the bank using debounce.
270 */
271static void _clear_gpio_debounce(struct gpio_bank *bank, unsigned gpio)
272{
273 u32 gpio_bit = GPIO_BIT(bank, gpio);
274
275 if (!bank->dbck_flag)
276 return;
277
278 if (!(bank->dbck_enable_mask & gpio_bit))
279 return;
280
281 bank->dbck_enable_mask &= ~gpio_bit;
282 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200283 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500284 bank->base + bank->regs->debounce_en);
285
286 if (!bank->dbck_enable_mask) {
287 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200288 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500289 bank->regs->debounce);
290 clk_disable(bank->dbck);
291 bank->dbck_enabled = false;
292 }
293}
294
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530295static inline void set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530296 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100297{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800298 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100299 u32 gpio_bit = 1 << gpio;
300
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530301 _gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
302 trigger & IRQ_TYPE_LEVEL_LOW);
303 _gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
304 trigger & IRQ_TYPE_LEVEL_HIGH);
305 _gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
306 trigger & IRQ_TYPE_EDGE_RISING);
307 _gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
308 trigger & IRQ_TYPE_EDGE_FALLING);
309
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530310 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200311 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530312 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200313 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530314 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200315 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530316 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200317 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530318
319 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530320 _gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530321 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200322 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530323 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530324
Ambresh K55b220c2011-06-15 13:40:45 -0700325 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530326 if (!bank->regs->irqctrl) {
327 /* On omap24xx proceed only when valid GPIO bit is set */
328 if (bank->non_wakeup_gpios) {
329 if (!(bank->non_wakeup_gpios & gpio_bit))
330 goto exit;
331 }
332
Chunqiu Wang699117a62009-06-24 17:13:39 +0000333 /*
334 * Log the edge gpio and manually trigger the IRQ
335 * after resume if the input level changes
336 * to avoid irq lost during PER RET/OFF mode
337 * Applies for omap2 non-wakeup gpio and all omap3 gpios
338 */
339 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800340 bank->enabled_non_wakeup_gpios |= gpio_bit;
341 else
342 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
343 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700344
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530345exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530346 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200347 readl_relaxed(bank->base + bank->regs->leveldetect0) |
348 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100349}
350
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800351#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800352/*
353 * This only applies to chips that can't do both rising and falling edge
354 * detection at once. For all other chips, this function is a noop.
355 */
356static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
357{
358 void __iomem *reg = bank->base;
359 u32 l = 0;
360
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530361 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800362 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530363
364 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800365
Victor Kamensky661553b2013-11-16 02:01:04 +0200366 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800367 if ((l >> gpio) & 1)
368 l &= ~(1 << gpio);
369 else
370 l |= 1 << gpio;
371
Victor Kamensky661553b2013-11-16 02:01:04 +0200372 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800373}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530374#else
375static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800376#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800377
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530378static int _set_gpio_triggering(struct gpio_bank *bank, int gpio,
379 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100380{
381 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530382 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100383 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100384
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530385 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
386 set_gpio_trigger(bank, gpio, trigger);
387 } else if (bank->regs->irqctrl) {
388 reg += bank->regs->irqctrl;
389
Victor Kamensky661553b2013-11-16 02:01:04 +0200390 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000391 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800392 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100393 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100394 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100395 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100396 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100397 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530398 return -EINVAL;
399
Victor Kamensky661553b2013-11-16 02:01:04 +0200400 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530401 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100402 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530403 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100404 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530405 reg += bank->regs->edgectrl1;
406
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100407 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200408 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100409 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100410 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100411 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100412 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100413 l |= 1 << (gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530414
415 /* Enable wake-up during idle for dynamic tick */
416 _gpio_rmw(base, bank->regs->wkup_en, 1 << gpio, trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530417 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200418 readl_relaxed(bank->base + bank->regs->wkup_en);
419 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100420 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100421 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100422}
423
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200424static void _enable_gpio_module(struct gpio_bank *bank, unsigned offset)
425{
426 if (bank->regs->pinctrl) {
427 void __iomem *reg = bank->base + bank->regs->pinctrl;
428
429 /* Claim the pin for MPU */
Victor Kamensky661553b2013-11-16 02:01:04 +0200430 writel_relaxed(readl_relaxed(reg) | (1 << offset), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200431 }
432
433 if (bank->regs->ctrl && !BANK_USED(bank)) {
434 void __iomem *reg = bank->base + bank->regs->ctrl;
435 u32 ctrl;
436
Victor Kamensky661553b2013-11-16 02:01:04 +0200437 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200438 /* Module is enabled, clocks are not gated */
439 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200440 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200441 bank->context.ctrl = ctrl;
442 }
443}
444
445static void _disable_gpio_module(struct gpio_bank *bank, unsigned offset)
446{
447 void __iomem *base = bank->base;
448
449 if (bank->regs->wkup_en &&
450 !LINE_USED(bank->mod_usage, offset) &&
451 !LINE_USED(bank->irq_usage, offset)) {
452 /* Disable wake-up during idle for dynamic tick */
453 _gpio_rmw(base, bank->regs->wkup_en, 1 << offset, 0);
454 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200455 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200456 }
457
458 if (bank->regs->ctrl && !BANK_USED(bank)) {
459 void __iomem *reg = bank->base + bank->regs->ctrl;
460 u32 ctrl;
461
Victor Kamensky661553b2013-11-16 02:01:04 +0200462 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200463 /* Module is disabled, clocks are gated */
464 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200465 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200466 bank->context.ctrl = ctrl;
467 }
468}
469
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200470static int gpio_is_input(struct gpio_bank *bank, int mask)
471{
472 void __iomem *reg = bank->base + bank->regs->direction;
473
Victor Kamensky661553b2013-11-16 02:01:04 +0200474 return readl_relaxed(reg) & mask;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200475}
476
Lennert Buytenheke9191022010-11-29 11:17:17 +0100477static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100478{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200479 struct gpio_bank *bank = _irq_data_get_bank(d);
Tony Lindgren4b254082012-08-30 15:37:24 -0700480 unsigned gpio = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100481 int retval;
David Brownella6472532008-03-03 04:33:30 -0800482 unsigned long flags;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200483 unsigned offset;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100484
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200485 if (!BANK_USED(bank))
486 pm_runtime_get_sync(bank->dev);
Jon Hunter8d4c2772013-03-01 11:22:48 -0600487
Tony Lindgren4b254082012-08-30 15:37:24 -0700488#ifdef CONFIG_ARCH_OMAP1
489 if (d->irq > IH_MPUIO_BASE)
Lennert Buytenheke9191022010-11-29 11:17:17 +0100490 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren4b254082012-08-30 15:37:24 -0700491#endif
492
493 if (!gpio)
Jon Hunterede4d7a2013-03-01 11:22:47 -0600494 gpio = irq_to_gpio(bank, d->hwirq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100495
David Brownelle5c56ed2006-12-06 17:13:59 -0800496 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100497 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800498
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530499 if (!bank->regs->leveldetect0 &&
500 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100501 return -EINVAL;
502
David Brownella6472532008-03-03 04:33:30 -0800503 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200504 offset = GPIO_INDEX(bank, gpio);
505 retval = _set_gpio_triggering(bank, offset, type);
506 if (!LINE_USED(bank->mod_usage, offset)) {
507 _enable_gpio_module(bank, offset);
508 _set_gpio_direction(bank, offset, 1);
509 } else if (!gpio_is_input(bank, 1 << offset)) {
510 spin_unlock_irqrestore(&bank->lock, flags);
511 return -EINVAL;
512 }
513
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200514 bank->irq_usage |= 1 << GPIO_INDEX(bank, gpio);
David Brownella6472532008-03-03 04:33:30 -0800515 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800516
517 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100518 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800519 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100520 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800521
Tony Lindgren92105bb2005-09-07 17:20:26 +0100522 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100523}
524
525static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
526{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100527 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100528
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700529 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200530 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300531
532 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700533 if (bank->regs->irqstatus2) {
534 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200535 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700536 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700537
538 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200539 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100540}
541
542static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
543{
Kevin Hilman129fd222011-04-22 07:59:07 -0700544 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100545}
546
Imre Deakea6dedd2006-06-26 16:16:00 -0700547static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
548{
549 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700550 u32 l;
Kevin Hilmanc390aad02011-04-21 09:33:36 -0700551 u32 mask = (1 << bank->width) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700552
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700553 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200554 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700555 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700556 l = ~l;
557 l &= mask;
558 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700559}
560
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700561static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100562{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100563 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564 u32 l;
565
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700566 if (bank->regs->set_irqenable) {
567 reg += bank->regs->set_irqenable;
568 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530569 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700570 } else {
571 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200572 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700573 if (bank->regs->irqenable_inv)
574 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100575 else
576 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530577 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700579
Victor Kamensky661553b2013-11-16 02:01:04 +0200580 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700581}
582
583static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
584{
585 void __iomem *reg = bank->base;
586 u32 l;
587
588 if (bank->regs->clr_irqenable) {
589 reg += bank->regs->clr_irqenable;
590 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530591 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700592 } else {
593 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200594 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700595 if (bank->regs->irqenable_inv)
596 l |= gpio_mask;
597 else
598 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530599 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700600 }
601
Victor Kamensky661553b2013-11-16 02:01:04 +0200602 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100603}
604
605static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
606{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530607 if (enable)
608 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
609 else
610 _disable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100611}
612
Tony Lindgren92105bb2005-09-07 17:20:26 +0100613/*
614 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
615 * 1510 does not seem to have a wake-up register. If JTAG is connected
616 * to the target, system will wake up always on GPIO events. While
617 * system is running all registered GPIO interrupts need to have wake-up
618 * enabled. When system is suspended, only selected GPIO interrupts need
619 * to have wake-up enabled.
620 */
621static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
622{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700623 u32 gpio_bit = GPIO_BIT(bank, gpio);
624 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800625
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700626 if (bank->non_wakeup_gpios & gpio_bit) {
Benoit Cousson862ff642012-02-01 15:58:56 +0100627 dev_err(bank->dev,
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700628 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100629 return -EINVAL;
630 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700631
632 spin_lock_irqsave(&bank->lock, flags);
633 if (enable)
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530634 bank->context.wake_en |= gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700635 else
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530636 bank->context.wake_en &= ~gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700637
Victor Kamensky661553b2013-11-16 02:01:04 +0200638 writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700639 spin_unlock_irqrestore(&bank->lock, flags);
640
641 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100642}
643
Tony Lindgren4196dd62006-09-25 12:41:38 +0300644static void _reset_gpio(struct gpio_bank *bank, int gpio)
645{
Kevin Hilman129fd222011-04-22 07:59:07 -0700646 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300647 _set_gpio_irqenable(bank, gpio, 0);
648 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700649 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500650 _clear_gpio_debounce(bank, gpio);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300651}
652
Tony Lindgren92105bb2005-09-07 17:20:26 +0100653/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100654static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100655{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200656 struct gpio_bank *bank = _irq_data_get_bank(d);
Jon Hunterede4d7a2013-03-01 11:22:47 -0600657 unsigned int gpio = irq_to_gpio(bank, d->hwirq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100658
Benoit Cousson25db7112012-02-23 21:50:10 +0100659 return _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100660}
661
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800662static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100663{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800664 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800665 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100666
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530667 /*
668 * If this is the first gpio_request for the bank,
669 * enable the bank module.
670 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200671 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530672 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100673
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530674 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300675 /* Set trigger to none. You need to enable the desired trigger with
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200676 * request_irq() or set_irq_type(). Only do this if the IRQ line has
677 * not already been requested.
Tony Lindgren4196dd62006-09-25 12:41:38 +0300678 */
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200679 if (!LINE_USED(bank->irq_usage, offset)) {
680 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
681 _enable_gpio_module(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100682 }
Charulatha Vc8eef652011-05-02 15:21:42 +0530683 bank->mod_usage |= 1 << offset;
David Brownella6472532008-03-03 04:33:30 -0800684 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100685
686 return 0;
687}
688
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800689static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100690{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800691 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800692 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100693
David Brownella6472532008-03-03 04:33:30 -0800694 spin_lock_irqsave(&bank->lock, flags);
Charulatha Vc8eef652011-05-02 15:21:42 +0530695 bank->mod_usage &= ~(1 << offset);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200696 _disable_gpio_module(bank, offset);
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800697 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800698 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530699
700 /*
701 * If this is the last gpio to be freed in the bank,
702 * disable the bank module.
703 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200704 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530705 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100706}
707
708/*
709 * We need to unmask the GPIO bank interrupt as soon as possible to
710 * avoid missing GPIO interrupts for other lines in the bank.
711 * Then we need to mask-read-clear-unmask the triggered GPIO lines
712 * in the bank to avoid missing nested interrupts for a GPIO line.
713 * If we wait to unmask individual GPIO lines in the bank after the
714 * line's interrupt handler has been run, we may miss some nested
715 * interrupts.
716 */
Russell King10dd5ce2006-11-23 11:41:32 +0000717static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100719 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100720 u32 isr;
Jon Hunter3513cde2013-04-04 15:16:14 -0500721 unsigned int bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100722 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700723 int unmasked = 0;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200724 struct irq_chip *irqchip = irq_desc_get_chip(desc);
725 struct gpio_chip *chip = irq_get_handler_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200727 chained_irq_enter(irqchip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100728
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200729 bank = container_of(chip, struct gpio_bank, chip);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700730 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530731 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800732
733 if (WARN_ON(!isr_reg))
734 goto exit;
735
Laurent Navete83507b2013-03-20 13:15:57 +0100736 while (1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100737 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700738 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100739
Imre Deakea6dedd2006-06-26 16:16:00 -0700740 enabled = _get_gpio_irqbank_mask(bank);
Victor Kamensky661553b2013-11-16 02:01:04 +0200741 isr_saved = isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100742
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530743 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800744 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100745
746 /* clear edge sensitive interrupts before handler(s) are
747 called so that we don't miss any interrupt occurred while
748 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700749 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100750 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700751 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100752
753 /* if there is only edge sensitive GPIO pin interrupts
754 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700755 if (!level_mask && !unmasked) {
756 unmasked = 1;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200757 chained_irq_exit(irqchip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700758 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100759
Tony Lindgren92105bb2005-09-07 17:20:26 +0100760 if (!isr)
761 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100762
Jon Hunter3513cde2013-04-04 15:16:14 -0500763 while (isr) {
764 bit = __ffs(isr);
765 isr &= ~(1 << bit);
Benoit Cousson25db7112012-02-23 21:50:10 +0100766
Cory Maccarrone4318f362010-01-08 10:29:04 -0800767 /*
768 * Some chips can't respond to both rising and falling
769 * at the same time. If this irq was requested with
770 * both flags, we need to flip the ICR data for the IRQ
771 * to respond to the IRQ for the opposite direction.
772 * This will be indicated in the bank toggle_mask.
773 */
Jon Hunter3513cde2013-04-04 15:16:14 -0500774 if (bank->toggle_mask & (1 << bit))
775 _toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800776
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200777 generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
778 bit));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100779 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000780 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700781 /* if bank has any level sensitive GPIO pin interrupt
782 configured, we must unmask the bank interrupt only after
783 handler(s) are executed in order to avoid spurious bank
784 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800785exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700786 if (!unmasked)
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200787 chained_irq_exit(irqchip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530788 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100789}
790
Lennert Buytenheke9191022010-11-29 11:17:17 +0100791static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300792{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200793 struct gpio_bank *bank = _irq_data_get_bank(d);
Jon Hunterede4d7a2013-03-01 11:22:47 -0600794 unsigned int gpio = irq_to_gpio(bank, d->hwirq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700795 unsigned long flags;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200796 unsigned offset = GPIO_INDEX(bank, gpio);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300797
Colin Cross85ec7b92011-06-06 13:38:18 -0700798 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200799 gpio_unlock_as_irq(&bank->chip, offset);
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200800 bank->irq_usage &= ~(1 << offset);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200801 _disable_gpio_module(bank, offset);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300802 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700803 spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200804
805 /*
806 * If this is the last IRQ to be freed in the bank,
807 * disable the bank module.
808 */
809 if (!BANK_USED(bank))
810 pm_runtime_put(bank->dev);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300811}
812
Lennert Buytenheke9191022010-11-29 11:17:17 +0100813static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100814{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200815 struct gpio_bank *bank = _irq_data_get_bank(d);
Jon Hunterede4d7a2013-03-01 11:22:47 -0600816 unsigned int gpio = irq_to_gpio(bank, d->hwirq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100817
818 _clear_gpio_irqstatus(bank, gpio);
819}
820
Lennert Buytenheke9191022010-11-29 11:17:17 +0100821static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100822{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200823 struct gpio_bank *bank = _irq_data_get_bank(d);
Jon Hunterede4d7a2013-03-01 11:22:47 -0600824 unsigned int gpio = irq_to_gpio(bank, d->hwirq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700825 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100826
Colin Cross85ec7b92011-06-06 13:38:18 -0700827 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100828 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700829 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700830 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100831}
832
Lennert Buytenheke9191022010-11-29 11:17:17 +0100833static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100834{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200835 struct gpio_bank *bank = _irq_data_get_bank(d);
Jon Hunterede4d7a2013-03-01 11:22:47 -0600836 unsigned int gpio = irq_to_gpio(bank, d->hwirq);
Kevin Hilman129fd222011-04-22 07:59:07 -0700837 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100838 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700839 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700840
Colin Cross85ec7b92011-06-06 13:38:18 -0700841 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700842 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700843 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800844
845 /* For level-triggered GPIOs, the clearing must be done after
846 * the HW source is cleared, thus after the handler has run */
847 if (bank->level_mask & irq_mask) {
848 _set_gpio_irqenable(bank, gpio, 0);
849 _clear_gpio_irqstatus(bank, gpio);
850 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100851
Kevin Hilman4de8c752008-01-16 21:56:14 -0800852 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700853 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100854}
855
David Brownelle5c56ed2006-12-06 17:13:59 -0800856static struct irq_chip gpio_irq_chip = {
857 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100858 .irq_shutdown = gpio_irq_shutdown,
859 .irq_ack = gpio_ack_irq,
860 .irq_mask = gpio_mask_irq,
861 .irq_unmask = gpio_unmask_irq,
862 .irq_set_type = gpio_irq_type,
863 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800864};
865
866/*---------------------------------------------------------------------*/
867
Magnus Damm79ee0312009-07-08 13:22:04 +0200868static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800869{
Magnus Damm79ee0312009-07-08 13:22:04 +0200870 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800871 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800872 void __iomem *mask_reg = bank->base +
873 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800874 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800875
David Brownella6472532008-03-03 04:33:30 -0800876 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200877 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800878 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800879
880 return 0;
881}
882
Magnus Damm79ee0312009-07-08 13:22:04 +0200883static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800884{
Magnus Damm79ee0312009-07-08 13:22:04 +0200885 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800886 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800887 void __iomem *mask_reg = bank->base +
888 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800889 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800890
David Brownella6472532008-03-03 04:33:30 -0800891 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200892 writel_relaxed(bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800893 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800894
895 return 0;
896}
897
Alexey Dobriyan47145212009-12-14 18:00:08 -0800898static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200899 .suspend_noirq = omap_mpuio_suspend_noirq,
900 .resume_noirq = omap_mpuio_resume_noirq,
901};
902
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200903/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800904static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800905 .driver = {
906 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200907 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800908 },
909};
910
911static struct platform_device omap_mpuio_device = {
912 .name = "mpuio",
913 .id = -1,
914 .dev = {
915 .driver = &omap_mpuio_driver.driver,
916 }
917 /* could list the /proc/iomem resources */
918};
919
Charulatha V03e128c2011-05-05 19:58:01 +0530920static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800921{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800922 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700923
David Brownell11a78b72006-12-06 17:14:11 -0800924 if (platform_driver_register(&omap_mpuio_driver) == 0)
925 (void) platform_device_register(&omap_mpuio_device);
926}
927
David Brownelle5c56ed2006-12-06 17:13:59 -0800928/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100929
David Brownell52e31342008-03-03 12:43:23 -0800930static int gpio_input(struct gpio_chip *chip, unsigned offset)
931{
932 struct gpio_bank *bank;
933 unsigned long flags;
934
935 bank = container_of(chip, struct gpio_bank, chip);
936 spin_lock_irqsave(&bank->lock, flags);
937 _set_gpio_direction(bank, offset, 1);
938 spin_unlock_irqrestore(&bank->lock, flags);
939 return 0;
940}
941
942static int gpio_get(struct gpio_chip *chip, unsigned offset)
943{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300944 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300945 u32 mask;
946
Charulatha Va8be8da2011-04-22 16:38:16 +0530947 bank = container_of(chip, struct gpio_bank, chip);
Tarun Kanti DebBarma7fcca712012-02-27 11:46:09 +0530948 mask = (1 << offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300949
950 if (gpio_is_input(bank, mask))
Tarun Kanti DebBarma7fcca712012-02-27 11:46:09 +0530951 return _get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300952 else
Tarun Kanti DebBarma7fcca712012-02-27 11:46:09 +0530953 return _get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -0800954}
955
956static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
957{
958 struct gpio_bank *bank;
959 unsigned long flags;
960
961 bank = container_of(chip, struct gpio_bank, chip);
962 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700963 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800964 _set_gpio_direction(bank, offset, 0);
965 spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200966 return 0;
David Brownell52e31342008-03-03 12:43:23 -0800967}
968
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700969static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
970 unsigned debounce)
971{
972 struct gpio_bank *bank;
973 unsigned long flags;
974
975 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800976
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700977 spin_lock_irqsave(&bank->lock, flags);
978 _set_gpio_debounce(bank, offset, debounce);
979 spin_unlock_irqrestore(&bank->lock, flags);
980
981 return 0;
982}
983
David Brownell52e31342008-03-03 12:43:23 -0800984static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
985{
986 struct gpio_bank *bank;
987 unsigned long flags;
988
989 bank = container_of(chip, struct gpio_bank, chip);
990 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700991 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800992 spin_unlock_irqrestore(&bank->lock, flags);
993}
994
995/*---------------------------------------------------------------------*/
996
Tony Lindgren9a748052010-12-07 16:26:56 -0800997static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700998{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700999 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001000 u32 rev;
1001
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001002 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001003 return;
1004
Victor Kamensky661553b2013-11-16 02:01:04 +02001005 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001006 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001007 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001008
1009 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001010}
1011
David Brownell8ba55c52008-02-26 11:10:50 -08001012/* This lock class tells lockdep that GPIO irqs are in a different
1013 * category than their parents, so it won't report false recursion.
1014 */
1015static struct lock_class_key gpio_lock_class;
1016
Charulatha V03e128c2011-05-05 19:58:01 +05301017static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001018{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301019 void __iomem *base = bank->base;
1020 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001021
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301022 if (bank->width == 16)
1023 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001024
Charulatha Vd0d665a2011-08-31 00:02:21 +05301025 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001026 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301027 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001028 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301029
1030 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->irqenable_inv);
Tarun Kanti DebBarma6edd94d2012-04-30 12:50:12 +05301031 _gpio_rmw(base, bank->regs->irqstatus, l, !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301032 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001033 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301034
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301035 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001036 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301037 /* Initialize interface clk ungated, module enabled */
1038 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001039 writel_relaxed(0, base + bank->regs->ctrl);
Tarun Kanti DebBarma34672012012-07-11 14:43:14 +05301040
1041 bank->dbck = clk_get(bank->dev, "dbclk");
1042 if (IS_ERR(bank->dbck))
1043 dev_err(bank->dev, "Could not get gpio dbck\n");
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001044}
1045
Bill Pemberton38363092012-11-19 13:22:34 -05001046static void
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001047omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1048 unsigned int num)
1049{
1050 struct irq_chip_generic *gc;
1051 struct irq_chip_type *ct;
1052
1053 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1054 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -07001055 if (!gc) {
1056 dev_err(bank->dev, "Memory alloc failed for gc\n");
1057 return;
1058 }
1059
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001060 ct = gc->chip_types;
1061
1062 /* NOTE: No ack required, reading IRQ status clears it. */
1063 ct->chip.irq_mask = irq_gc_mask_set_bit;
1064 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1065 ct->chip.irq_set_type = gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301066
1067 if (bank->regs->wkup_en)
Julia Lawall388f4302013-08-13 09:16:56 +02001068 ct->chip.irq_set_wake = gpio_wake_enable;
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001069
1070 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1071 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1072 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1073}
1074
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001075static int omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001076{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001077 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001078 static int gpio;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001079 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001080 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001081
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001082 /*
1083 * REVISIT eventually switch from OMAP-specific gpio structs
1084 * over to the generic ones
1085 */
1086 bank->chip.request = omap_gpio_request;
1087 bank->chip.free = omap_gpio_free;
1088 bank->chip.direction_input = gpio_input;
1089 bank->chip.get = gpio_get;
1090 bank->chip.direction_output = gpio_output;
1091 bank->chip.set_debounce = gpio_debounce;
1092 bank->chip.set = gpio_set;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301093 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001094 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301095 if (bank->regs->wkup_en)
1096 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001097 bank->chip.base = OMAP_MPUIO(0);
1098 } else {
1099 bank->chip.label = "gpio";
1100 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001101 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001102 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001103 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001104
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001105 ret = gpiochip_add(&bank->chip);
1106 if (ret) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001107 dev_err(bank->dev, "Could not register gpio chip %d\n", ret);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001108 return ret;
1109 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001110
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001111#ifdef CONFIG_ARCH_OMAP1
1112 /*
1113 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1114 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1115 */
1116 irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
1117 if (irq_base < 0) {
1118 dev_err(bank->dev, "Couldn't allocate IRQ numbers\n");
1119 return -ENODEV;
1120 }
1121#endif
1122
1123 ret = gpiochip_irqchip_add(&bank->chip, &gpio_irq_chip,
1124 irq_base, gpio_irq_handler,
1125 IRQ_TYPE_NONE);
1126
1127 if (ret) {
1128 dev_err(bank->dev, "Couldn't add irqchip to gpiochip %d\n", ret);
1129 ret = gpiochip_remove(&bank->chip);
1130 return -ENODEV;
1131 }
1132
1133 gpiochip_set_chained_irqchip(&bank->chip, &gpio_irq_chip,
1134 bank->irq, gpio_irq_handler);
1135
Jon Hunterede4d7a2013-03-01 11:22:47 -06001136 for (j = 0; j < bank->width; j++) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001137 int irq = irq_find_mapping(bank->chip.irqdomain, j);
Jon Hunterede4d7a2013-03-01 11:22:47 -06001138 irq_set_lockdep_class(irq, &gpio_lock_class);
Charulatha Vd0d665a2011-08-31 00:02:21 +05301139 if (bank->is_mpuio) {
Jon Hunterede4d7a2013-03-01 11:22:47 -06001140 omap_mpuio_alloc_gc(bank, irq, bank->width);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001141 irq_set_chip_and_handler(irq, NULL, NULL);
1142 set_irq_flags(irq, 0);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001143 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001144 }
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001145
1146 return 0;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001147}
1148
Benoit Cousson384ebe12011-08-16 11:53:02 +02001149static const struct of_device_id omap_gpio_match[];
1150
Bill Pemberton38363092012-11-19 13:22:34 -05001151static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001152{
Benoit Cousson862ff642012-02-01 15:58:56 +01001153 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001154 struct device_node *node = dev->of_node;
1155 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001156 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001157 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001158 struct gpio_bank *bank;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001159 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001160
Benoit Cousson384ebe12011-08-16 11:53:02 +02001161 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1162
Jingoo Hane56aee12013-07-30 17:08:05 +09001163 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001164 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001165 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001166
Tobias Klauser086d5852012-10-05 11:37:38 +02001167 bank = devm_kzalloc(dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301168 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001169 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001170 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301171 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001172
1173 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1174 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001175 dev_err(dev, "Invalid IRQ resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001176 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001177 }
1178
1179 bank->irq = res->start;
Benoit Cousson862ff642012-02-01 15:58:56 +01001180 bank->dev = dev;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001181 bank->chip.dev = dev;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001182 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001183 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001184 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301185 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301186 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001187 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001188#ifdef CONFIG_OF_GPIO
1189 bank->chip.of_node = of_node_get(node);
1190#endif
Jon Huntera2797be2013-04-04 15:16:15 -05001191 if (node) {
1192 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1193 bank->loses_context = true;
1194 } else {
1195 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001196
1197 if (bank->loses_context)
1198 bank->get_context_loss_count =
1199 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001200 }
1201
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001202 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1203 bank->set_dataout = _set_gpio_dataout_reg;
1204 else
1205 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001206
1207 spin_lock_init(&bank->lock);
1208
1209 /* Static mapping, never released */
1210 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001211 bank->base = devm_ioremap_resource(dev, res);
1212 if (IS_ERR(bank->base)) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001213 irq_domain_remove(bank->chip.irqdomain);
Jingoo Han717f70e2014-02-12 11:51:38 +09001214 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001215 }
1216
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301217 platform_set_drvdata(pdev, bank);
1218
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001219 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301220 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001221 pm_runtime_get_sync(bank->dev);
1222
Charulatha Vd0d665a2011-08-31 00:02:21 +05301223 if (bank->is_mpuio)
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301224 mpuio_init(bank);
1225
Charulatha V03e128c2011-05-05 19:58:01 +05301226 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001227
1228 ret = omap_gpio_chip_init(bank);
1229 if (ret)
1230 return ret;
1231
Tony Lindgren9a748052010-12-07 16:26:56 -08001232 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001233
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301234 pm_runtime_put(bank->dev);
1235
Charulatha V03e128c2011-05-05 19:58:01 +05301236 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001237
Jon Hunter879fe322013-04-04 15:16:12 -05001238 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001239}
1240
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301241#ifdef CONFIG_ARCH_OMAP2PLUS
1242
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301243#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301244static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001245
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301246static int omap_gpio_runtime_suspend(struct device *dev)
1247{
1248 struct platform_device *pdev = to_platform_device(dev);
1249 struct gpio_bank *bank = platform_get_drvdata(pdev);
1250 u32 l1 = 0, l2 = 0;
1251 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001252 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301253
1254 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001255
1256 /*
1257 * Only edges can generate a wakeup event to the PRCM.
1258 *
1259 * Therefore, ensure any wake-up capable GPIOs have
1260 * edge-detection enabled before going idle to ensure a wakeup
1261 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1262 * NDA TRM 25.5.3.1)
1263 *
1264 * The normal values will be restored upon ->runtime_resume()
1265 * by writing back the values saved in bank->context.
1266 */
1267 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1268 if (wake_low)
Victor Kamensky661553b2013-11-16 02:01:04 +02001269 writel_relaxed(wake_low | bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001270 bank->base + bank->regs->fallingdetect);
1271 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1272 if (wake_hi)
Victor Kamensky661553b2013-11-16 02:01:04 +02001273 writel_relaxed(wake_hi | bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001274 bank->base + bank->regs->risingdetect);
1275
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001276 if (!bank->enabled_non_wakeup_gpios)
1277 goto update_gpio_context_count;
1278
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301279 if (bank->power_mode != OFF_MODE) {
1280 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301281 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301282 }
1283 /*
1284 * If going to OFF, remove triggering for all
1285 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1286 * generated. See OMAP2420 Errata item 1.101.
1287 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001288 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301289 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301290 l1 = bank->context.fallingdetect;
1291 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301292
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301293 l1 &= ~bank->enabled_non_wakeup_gpios;
1294 l2 &= ~bank->enabled_non_wakeup_gpios;
1295
Victor Kamensky661553b2013-11-16 02:01:04 +02001296 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1297 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301298
1299 bank->workaround_enabled = true;
1300
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301301update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301302 if (bank->get_context_loss_count)
1303 bank->context_loss_count =
1304 bank->get_context_loss_count(bank->dev);
1305
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +05301306 _gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301307 spin_unlock_irqrestore(&bank->lock, flags);
1308
1309 return 0;
1310}
1311
Jon Hunter352a2d52013-04-15 13:06:54 -05001312static void omap_gpio_init_context(struct gpio_bank *p);
1313
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301314static int omap_gpio_runtime_resume(struct device *dev)
1315{
1316 struct platform_device *pdev = to_platform_device(dev);
1317 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301318 u32 l = 0, gen, gen0, gen1;
1319 unsigned long flags;
Jon Huntera2797be2013-04-04 15:16:15 -05001320 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301321
1322 spin_lock_irqsave(&bank->lock, flags);
Jon Hunter352a2d52013-04-15 13:06:54 -05001323
1324 /*
1325 * On the first resume during the probe, the context has not
1326 * been initialised and so initialise it now. Also initialise
1327 * the context loss count.
1328 */
1329 if (bank->loses_context && !bank->context_valid) {
1330 omap_gpio_init_context(bank);
1331
1332 if (bank->get_context_loss_count)
1333 bank->context_loss_count =
1334 bank->get_context_loss_count(bank->dev);
1335 }
1336
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +05301337 _gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001338
1339 /*
1340 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1341 * GPIOs were set to edge trigger also in order to be able to
1342 * generate a PRCM wakeup. Here we restore the
1343 * pre-runtime_suspend() values for edge triggering.
1344 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001345 writel_relaxed(bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001346 bank->base + bank->regs->fallingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001347 writel_relaxed(bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001348 bank->base + bank->regs->risingdetect);
1349
Jon Huntera2797be2013-04-04 15:16:15 -05001350 if (bank->loses_context) {
1351 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301352 omap_gpio_restore_context(bank);
1353 } else {
Jon Huntera2797be2013-04-04 15:16:15 -05001354 c = bank->get_context_loss_count(bank->dev);
1355 if (c != bank->context_loss_count) {
1356 omap_gpio_restore_context(bank);
1357 } else {
1358 spin_unlock_irqrestore(&bank->lock, flags);
1359 return 0;
1360 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301361 }
1362 }
1363
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301364 if (!bank->workaround_enabled) {
1365 spin_unlock_irqrestore(&bank->lock, flags);
1366 return 0;
1367 }
1368
Victor Kamensky661553b2013-11-16 02:01:04 +02001369 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301370
1371 /*
1372 * Check if any of the non-wakeup interrupt GPIOs have changed
1373 * state. If so, generate an IRQ by software. This is
1374 * horribly racy, but it's the best we can do to work around
1375 * this silicon bug.
1376 */
1377 l ^= bank->saved_datain;
1378 l &= bank->enabled_non_wakeup_gpios;
1379
1380 /*
1381 * No need to generate IRQs for the rising edge for gpio IRQs
1382 * configured with falling edge only; and vice versa.
1383 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301384 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301385 gen0 &= bank->saved_datain;
1386
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301387 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301388 gen1 &= ~(bank->saved_datain);
1389
1390 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301391 gen = l & (~(bank->context.fallingdetect) &
1392 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301393 /* Consider all GPIO IRQs needed to be updated */
1394 gen |= gen0 | gen1;
1395
1396 if (gen) {
1397 u32 old0, old1;
1398
Victor Kamensky661553b2013-11-16 02:01:04 +02001399 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1400 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301401
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301402 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001403 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301404 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001405 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301406 bank->regs->leveldetect1);
1407 }
1408
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301409 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001410 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301411 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001412 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301413 bank->regs->leveldetect1);
1414 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001415 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1416 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301417 }
1418
1419 bank->workaround_enabled = false;
1420 spin_unlock_irqrestore(&bank->lock, flags);
1421
1422 return 0;
1423}
1424#endif /* CONFIG_PM_RUNTIME */
1425
1426void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001427{
Charulatha V03e128c2011-05-05 19:58:01 +05301428 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001429
Charulatha V03e128c2011-05-05 19:58:01 +05301430 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001431 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301432 continue;
1433
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301434 bank->power_mode = pwr_mode;
1435
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301436 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001437 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001438}
1439
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001440void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001441{
Charulatha V03e128c2011-05-05 19:58:01 +05301442 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001443
Charulatha V03e128c2011-05-05 19:58:01 +05301444 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001445 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301446 continue;
1447
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301448 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001449 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001450}
1451
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301452#if defined(CONFIG_PM_RUNTIME)
Jon Hunter352a2d52013-04-15 13:06:54 -05001453static void omap_gpio_init_context(struct gpio_bank *p)
1454{
1455 struct omap_gpio_reg_offs *regs = p->regs;
1456 void __iomem *base = p->base;
1457
Victor Kamensky661553b2013-11-16 02:01:04 +02001458 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1459 p->context.oe = readl_relaxed(base + regs->direction);
1460 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1461 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1462 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1463 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1464 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1465 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1466 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001467
1468 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001469 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001470 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001471 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001472
1473 p->context_valid = true;
1474}
1475
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301476static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301477{
Victor Kamensky661553b2013-11-16 02:01:04 +02001478 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301479 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001480 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1481 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301482 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001483 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301484 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001485 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301486 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001487 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301488 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301489 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001490 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301491 bank->base + bank->regs->set_dataout);
1492 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001493 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301494 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001495 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301496
Nishanth Menonae547352011-09-09 19:08:58 +05301497 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001498 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301499 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001500 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301501 bank->base + bank->regs->debounce_en);
1502 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301503
Victor Kamensky661553b2013-11-16 02:01:04 +02001504 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301505 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001506 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301507 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301508}
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301509#endif /* CONFIG_PM_RUNTIME */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301510#else
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301511#define omap_gpio_runtime_suspend NULL
1512#define omap_gpio_runtime_resume NULL
Arnd Bergmannea4a21a2013-05-31 17:59:46 +02001513static inline void omap_gpio_init_context(struct gpio_bank *p) {}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301514#endif
1515
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301516static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301517 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1518 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301519};
1520
Benoit Cousson384ebe12011-08-16 11:53:02 +02001521#if defined(CONFIG_OF)
1522static struct omap_gpio_reg_offs omap2_gpio_regs = {
1523 .revision = OMAP24XX_GPIO_REVISION,
1524 .direction = OMAP24XX_GPIO_OE,
1525 .datain = OMAP24XX_GPIO_DATAIN,
1526 .dataout = OMAP24XX_GPIO_DATAOUT,
1527 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1528 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1529 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1530 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1531 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1532 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1533 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1534 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1535 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1536 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1537 .ctrl = OMAP24XX_GPIO_CTRL,
1538 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1539 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1540 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1541 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1542 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1543};
1544
1545static struct omap_gpio_reg_offs omap4_gpio_regs = {
1546 .revision = OMAP4_GPIO_REVISION,
1547 .direction = OMAP4_GPIO_OE,
1548 .datain = OMAP4_GPIO_DATAIN,
1549 .dataout = OMAP4_GPIO_DATAOUT,
1550 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1551 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1552 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1553 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1554 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1555 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1556 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1557 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1558 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1559 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1560 .ctrl = OMAP4_GPIO_CTRL,
1561 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1562 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1563 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1564 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1565 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1566};
1567
Chen Gange9a65bb2013-02-06 18:44:32 +08001568static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001569 .regs = &omap2_gpio_regs,
1570 .bank_width = 32,
1571 .dbck_flag = false,
1572};
1573
Chen Gange9a65bb2013-02-06 18:44:32 +08001574static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001575 .regs = &omap2_gpio_regs,
1576 .bank_width = 32,
1577 .dbck_flag = true,
1578};
1579
Chen Gange9a65bb2013-02-06 18:44:32 +08001580static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001581 .regs = &omap4_gpio_regs,
1582 .bank_width = 32,
1583 .dbck_flag = true,
1584};
1585
1586static const struct of_device_id omap_gpio_match[] = {
1587 {
1588 .compatible = "ti,omap4-gpio",
1589 .data = &omap4_pdata,
1590 },
1591 {
1592 .compatible = "ti,omap3-gpio",
1593 .data = &omap3_pdata,
1594 },
1595 {
1596 .compatible = "ti,omap2-gpio",
1597 .data = &omap2_pdata,
1598 },
1599 { },
1600};
1601MODULE_DEVICE_TABLE(of, omap_gpio_match);
1602#endif
1603
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001604static struct platform_driver omap_gpio_driver = {
1605 .probe = omap_gpio_probe,
1606 .driver = {
1607 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301608 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001609 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001610 },
1611};
1612
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001613/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001614 * gpio driver register needs to be done before
1615 * machine_init functions access gpio APIs.
1616 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001617 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001618static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001619{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001620 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001621}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001622postcore_initcall(omap_gpio_drv_reg);