blob: 00f29aa1fb9dda1a981b3d8245af3b1b9f1232e2 [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>
Yegor Yefremov93700842014-04-24 08:57:39 +020029#include <linux/bitops.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070030#include <linux/platform_data/gpio-omap.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053032#define OFF_MODE 1
33
Charulatha V03e128c2011-05-05 19:58:01 +053034static LIST_HEAD(omap_gpio_list);
35
Charulatha V6d62e212011-04-18 15:06:51 +000036struct gpio_regs {
37 u32 irqenable1;
38 u32 irqenable2;
39 u32 wake_en;
40 u32 ctrl;
41 u32 oe;
42 u32 leveldetect0;
43 u32 leveldetect1;
44 u32 risingdetect;
45 u32 fallingdetect;
46 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053047 u32 debounce;
48 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000049};
50
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010051struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053052 struct list_head node;
Tony Lindgren92105bb2005-09-07 17:20:26 +010053 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010054 u16 irq;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080055 u32 non_wakeup_gpios;
56 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000057 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080058 u32 saved_datain;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080059 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080060 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010061 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080062 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080063 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080064 u32 mod_usage;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020065 u32 irq_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080066 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +053067 bool dbck_enabled;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080068 struct device *dev;
Charulatha Vd0d665a2011-08-31 00:02:21 +053069 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080070 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053071 bool loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -050072 bool context_valid;
Tony Lindgren5de62b82010-12-07 16:26:58 -080073 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070074 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053075 int context_loss_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053076 int power_mode;
77 bool workaround_enabled;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070078
79 void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053080 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070081
82 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010083};
84
Kevin Hilman129fd222011-04-22 07:59:07 -070085#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020086#define GPIO_BIT(bank, gpio) (BIT(GPIO_INDEX(bank, gpio)))
Charulatha Vc8eef652011-05-02 15:21:42 +053087#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010088
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020089#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020090#define LINE_USED(line, offset) (line & (BIT(offset)))
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020091
Benoit Cousson25db7112012-02-23 21:50:10 +010092static int irq_to_gpio(struct gpio_bank *bank, unsigned int gpio_irq)
93{
Jon Hunterede4d7a2013-03-01 11:22:47 -060094 return bank->chip.base + gpio_irq;
95}
96
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +020097static inline struct gpio_bank *_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -060098{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +020099 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
100 return container_of(chip, struct gpio_bank, chip);
Benoit Cousson25db7112012-02-23 21:50:10 +0100101}
102
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100103static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
104{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100105 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100106 u32 l;
107
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700108 reg += bank->regs->direction;
Victor Kamensky661553b2013-11-16 02:01:04 +0200109 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100110 if (is_input)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200111 l |= BIT(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100112 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200113 l &= ~(BIT(gpio));
Victor Kamensky661553b2013-11-16 02:01:04 +0200114 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530115 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100116}
117
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700118
119/* set data out value using dedicate set/clear register */
120static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100122 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700123 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100124
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530125 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700126 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530127 bank->context.dataout |= l;
128 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700129 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530130 bank->context.dataout &= ~l;
131 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700132
Victor Kamensky661553b2013-11-16 02:01:04 +0200133 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700134}
135
136/* set data out value using mask register */
137static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
138{
139 void __iomem *reg = bank->base + bank->regs->dataout;
140 u32 gpio_bit = GPIO_BIT(bank, gpio);
141 u32 l;
142
Victor Kamensky661553b2013-11-16 02:01:04 +0200143 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700144 if (enable)
145 l |= gpio_bit;
146 else
147 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200148 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530149 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150}
151
Tarun Kanti DebBarma7fcca712012-02-27 11:46:09 +0530152static int _get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700154 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100155
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200156 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100157}
158
Tarun Kanti DebBarma7fcca712012-02-27 11:46:09 +0530159static int _get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300160{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700161 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300162
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200163 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300164}
165
Kevin Hilmanece95282011-07-12 08:18:15 -0700166static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
167{
Victor Kamensky661553b2013-11-16 02:01:04 +0200168 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700169
Benoit Cousson862ff642012-02-01 15:58:56 +0100170 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700171 l |= mask;
172 else
173 l &= ~mask;
174
Victor Kamensky661553b2013-11-16 02:01:04 +0200175 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700176}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100177
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530178static inline void _gpio_dbck_enable(struct gpio_bank *bank)
179{
180 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Rajendra Nayak345477f2014-04-23 11:41:03 +0530181 clk_prepare_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530182 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300183
Victor Kamensky661553b2013-11-16 02:01:04 +0200184 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300185 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530186 }
187}
188
189static inline void _gpio_dbck_disable(struct gpio_bank *bank)
190{
191 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300192 /*
193 * Disable debounce before cutting it's clock. If debounce is
194 * enabled but the clock is not, GPIO module seems to be unable
195 * to detect events and generate interrupts at least on OMAP3.
196 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200197 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300198
Rajendra Nayak345477f2014-04-23 11:41:03 +0530199 clk_disable_unprepare(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530200 bank->dbck_enabled = false;
201 }
202}
203
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700204/**
205 * _set_gpio_debounce - low level gpio debounce time
206 * @bank: the gpio bank we're acting upon
207 * @gpio: the gpio number on this @gpio
208 * @debounce: debounce time to use
209 *
210 * OMAP's debounce time is in 31us steps so we need
211 * to convert and round up to the closest unit.
212 */
213static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
214 unsigned debounce)
215{
Kevin Hilman9942da02011-04-22 12:02:05 -0700216 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700217 u32 val;
218 u32 l;
219
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800220 if (!bank->dbck_flag)
221 return;
222
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700223 if (debounce < 32)
224 debounce = 0x01;
225 else if (debounce > 7936)
226 debounce = 0xff;
227 else
228 debounce = (debounce / 0x1f) - 1;
229
Kevin Hilman129fd222011-04-22 07:59:07 -0700230 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700231
Rajendra Nayak345477f2014-04-23 11:41:03 +0530232 clk_prepare_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700233 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200234 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700235
Kevin Hilman9942da02011-04-22 12:02:05 -0700236 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200237 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700238
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530239 if (debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700240 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530241 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700242 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300243 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700244
Victor Kamensky661553b2013-11-16 02:01:04 +0200245 writel_relaxed(val, reg);
Rajendra Nayak345477f2014-04-23 11:41:03 +0530246 clk_disable_unprepare(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530247 /*
248 * Enable debounce clock per module.
249 * This call is mandatory because in omap_gpio_request() when
250 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
251 * runtime callbck fails to turn on dbck because dbck_enable_mask
252 * used within _gpio_dbck_enable() is still not initialized at
253 * that point. Therefore we have to enable dbck here.
254 */
255 _gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530256 if (bank->dbck_enable_mask) {
257 bank->context.debounce = debounce;
258 bank->context.debounce_en = val;
259 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700260}
261
Jon Hunterc9c55d92012-10-26 14:26:04 -0500262/**
263 * _clear_gpio_debounce - clear debounce settings for a gpio
264 * @bank: the gpio bank we're acting upon
265 * @gpio: the gpio number on this @gpio
266 *
267 * If a gpio is using debounce, then clear the debounce enable bit and if
268 * this is the only gpio in this bank using debounce, then clear the debounce
269 * time too. The debounce clock will also be disabled when calling this function
270 * if this is the only gpio in the bank using debounce.
271 */
272static void _clear_gpio_debounce(struct gpio_bank *bank, unsigned gpio)
273{
274 u32 gpio_bit = GPIO_BIT(bank, gpio);
275
276 if (!bank->dbck_flag)
277 return;
278
279 if (!(bank->dbck_enable_mask & gpio_bit))
280 return;
281
282 bank->dbck_enable_mask &= ~gpio_bit;
283 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200284 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500285 bank->base + bank->regs->debounce_en);
286
287 if (!bank->dbck_enable_mask) {
288 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200289 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500290 bank->regs->debounce);
Rajendra Nayak345477f2014-04-23 11:41:03 +0530291 clk_disable_unprepare(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500292 bank->dbck_enabled = false;
293 }
294}
295
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530296static inline void set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530297 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100298{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800299 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200300 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100301
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530302 _gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
303 trigger & IRQ_TYPE_LEVEL_LOW);
304 _gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
305 trigger & IRQ_TYPE_LEVEL_HIGH);
306 _gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
307 trigger & IRQ_TYPE_EDGE_RISING);
308 _gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
309 trigger & IRQ_TYPE_EDGE_FALLING);
310
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530311 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200312 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530313 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200314 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530315 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200316 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530317 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200318 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530319
320 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530321 _gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530322 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200323 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530324 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530325
Ambresh K55b220c2011-06-15 13:40:45 -0700326 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530327 if (!bank->regs->irqctrl) {
328 /* On omap24xx proceed only when valid GPIO bit is set */
329 if (bank->non_wakeup_gpios) {
330 if (!(bank->non_wakeup_gpios & gpio_bit))
331 goto exit;
332 }
333
Chunqiu Wang699117a62009-06-24 17:13:39 +0000334 /*
335 * Log the edge gpio and manually trigger the IRQ
336 * after resume if the input level changes
337 * to avoid irq lost during PER RET/OFF mode
338 * Applies for omap2 non-wakeup gpio and all omap3 gpios
339 */
340 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800341 bank->enabled_non_wakeup_gpios |= gpio_bit;
342 else
343 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
344 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700345
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530346exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530347 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200348 readl_relaxed(bank->base + bank->regs->leveldetect0) |
349 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100350}
351
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800352#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800353/*
354 * This only applies to chips that can't do both rising and falling edge
355 * detection at once. For all other chips, this function is a noop.
356 */
357static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
358{
359 void __iomem *reg = bank->base;
360 u32 l = 0;
361
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530362 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800363 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530364
365 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800366
Victor Kamensky661553b2013-11-16 02:01:04 +0200367 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800368 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200369 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800370 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200371 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800372
Victor Kamensky661553b2013-11-16 02:01:04 +0200373 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800374}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530375#else
376static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800377#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800378
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530379static int _set_gpio_triggering(struct gpio_bank *bank, int gpio,
380 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100381{
382 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530383 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100384 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100385
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530386 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
387 set_gpio_trigger(bank, gpio, trigger);
388 } else if (bank->regs->irqctrl) {
389 reg += bank->regs->irqctrl;
390
Victor Kamensky661553b2013-11-16 02:01:04 +0200391 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000392 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200393 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100394 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200395 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100396 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200397 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100398 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530399 return -EINVAL;
400
Victor Kamensky661553b2013-11-16 02:01:04 +0200401 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530402 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100403 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530404 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100405 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530406 reg += bank->regs->edgectrl1;
407
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100408 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200409 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100410 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100411 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100412 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100413 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200414 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530415
416 /* Enable wake-up during idle for dynamic tick */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200417 _gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530418 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200419 readl_relaxed(bank->base + bank->regs->wkup_en);
420 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100421 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100422 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100423}
424
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200425static void _enable_gpio_module(struct gpio_bank *bank, unsigned offset)
426{
427 if (bank->regs->pinctrl) {
428 void __iomem *reg = bank->base + bank->regs->pinctrl;
429
430 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200431 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200432 }
433
434 if (bank->regs->ctrl && !BANK_USED(bank)) {
435 void __iomem *reg = bank->base + bank->regs->ctrl;
436 u32 ctrl;
437
Victor Kamensky661553b2013-11-16 02:01:04 +0200438 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200439 /* Module is enabled, clocks are not gated */
440 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200441 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200442 bank->context.ctrl = ctrl;
443 }
444}
445
446static void _disable_gpio_module(struct gpio_bank *bank, unsigned offset)
447{
448 void __iomem *base = bank->base;
449
450 if (bank->regs->wkup_en &&
451 !LINE_USED(bank->mod_usage, offset) &&
452 !LINE_USED(bank->irq_usage, offset)) {
453 /* Disable wake-up during idle for dynamic tick */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200454 _gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200455 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200456 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200457 }
458
459 if (bank->regs->ctrl && !BANK_USED(bank)) {
460 void __iomem *reg = bank->base + bank->regs->ctrl;
461 u32 ctrl;
462
Victor Kamensky661553b2013-11-16 02:01:04 +0200463 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200464 /* Module is disabled, clocks are gated */
465 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200466 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200467 bank->context.ctrl = ctrl;
468 }
469}
470
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200471static int gpio_is_input(struct gpio_bank *bank, int mask)
472{
473 void __iomem *reg = bank->base + bank->regs->direction;
474
Victor Kamensky661553b2013-11-16 02:01:04 +0200475 return readl_relaxed(reg) & mask;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200476}
477
Lennert Buytenheke9191022010-11-29 11:17:17 +0100478static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100479{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200480 struct gpio_bank *bank = _irq_data_get_bank(d);
Tony Lindgren4b254082012-08-30 15:37:24 -0700481 unsigned gpio = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100482 int retval;
David Brownella6472532008-03-03 04:33:30 -0800483 unsigned long flags;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200484 unsigned offset;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100485
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200486 if (!BANK_USED(bank))
487 pm_runtime_get_sync(bank->dev);
Jon Hunter8d4c2772013-03-01 11:22:48 -0600488
Tony Lindgren4b254082012-08-30 15:37:24 -0700489#ifdef CONFIG_ARCH_OMAP1
490 if (d->irq > IH_MPUIO_BASE)
Lennert Buytenheke9191022010-11-29 11:17:17 +0100491 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren4b254082012-08-30 15:37:24 -0700492#endif
493
494 if (!gpio)
Jon Hunterede4d7a2013-03-01 11:22:47 -0600495 gpio = irq_to_gpio(bank, d->hwirq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100496
David Brownelle5c56ed2006-12-06 17:13:59 -0800497 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100498 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800499
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530500 if (!bank->regs->leveldetect0 &&
501 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100502 return -EINVAL;
503
David Brownella6472532008-03-03 04:33:30 -0800504 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200505 offset = GPIO_INDEX(bank, gpio);
506 retval = _set_gpio_triggering(bank, offset, type);
507 if (!LINE_USED(bank->mod_usage, offset)) {
508 _enable_gpio_module(bank, offset);
509 _set_gpio_direction(bank, offset, 1);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200510 } else if (!gpio_is_input(bank, BIT(offset))) {
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200511 spin_unlock_irqrestore(&bank->lock, flags);
512 return -EINVAL;
513 }
514
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200515 bank->irq_usage |= BIT(GPIO_INDEX(bank, gpio));
David Brownella6472532008-03-03 04:33:30 -0800516 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800517
518 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100519 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800520 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100521 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800522
Tony Lindgren92105bb2005-09-07 17:20:26 +0100523 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100524}
525
526static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
527{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100528 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700530 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200531 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300532
533 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700534 if (bank->regs->irqstatus2) {
535 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200536 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700537 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700538
539 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200540 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100541}
542
543static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
544{
Kevin Hilman129fd222011-04-22 07:59:07 -0700545 _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100546}
547
Imre Deakea6dedd2006-06-26 16:16:00 -0700548static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
549{
550 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700551 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200552 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700553
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700554 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200555 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700556 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700557 l = ~l;
558 l &= mask;
559 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700560}
561
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700562static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100563{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100564 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100565 u32 l;
566
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700567 if (bank->regs->set_irqenable) {
568 reg += bank->regs->set_irqenable;
569 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530570 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700571 } else {
572 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200573 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700574 if (bank->regs->irqenable_inv)
575 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576 else
577 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530578 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100579 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700580
Victor Kamensky661553b2013-11-16 02:01:04 +0200581 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700582}
583
584static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
585{
586 void __iomem *reg = bank->base;
587 u32 l;
588
589 if (bank->regs->clr_irqenable) {
590 reg += bank->regs->clr_irqenable;
591 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530592 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700593 } else {
594 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200595 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700596 if (bank->regs->irqenable_inv)
597 l |= gpio_mask;
598 else
599 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530600 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700601 }
602
Victor Kamensky661553b2013-11-16 02:01:04 +0200603 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100604}
605
606static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
607{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530608 if (enable)
609 _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
610 else
611 _disable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100612}
613
Tony Lindgren92105bb2005-09-07 17:20:26 +0100614/*
615 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
616 * 1510 does not seem to have a wake-up register. If JTAG is connected
617 * to the target, system will wake up always on GPIO events. While
618 * system is running all registered GPIO interrupts need to have wake-up
619 * enabled. When system is suspended, only selected GPIO interrupts need
620 * to have wake-up enabled.
621 */
622static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
623{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700624 u32 gpio_bit = GPIO_BIT(bank, gpio);
625 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800626
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700627 if (bank->non_wakeup_gpios & gpio_bit) {
Benoit Cousson862ff642012-02-01 15:58:56 +0100628 dev_err(bank->dev,
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700629 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100630 return -EINVAL;
631 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700632
633 spin_lock_irqsave(&bank->lock, flags);
634 if (enable)
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530635 bank->context.wake_en |= gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700636 else
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530637 bank->context.wake_en &= ~gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700638
Victor Kamensky661553b2013-11-16 02:01:04 +0200639 writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700640 spin_unlock_irqrestore(&bank->lock, flags);
641
642 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100643}
644
Tony Lindgren4196dd62006-09-25 12:41:38 +0300645static void _reset_gpio(struct gpio_bank *bank, int gpio)
646{
Kevin Hilman129fd222011-04-22 07:59:07 -0700647 _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300648 _set_gpio_irqenable(bank, gpio, 0);
649 _clear_gpio_irqstatus(bank, gpio);
Kevin Hilman129fd222011-04-22 07:59:07 -0700650 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500651 _clear_gpio_debounce(bank, gpio);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300652}
653
Tony Lindgren92105bb2005-09-07 17:20:26 +0100654/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +0100655static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100656{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200657 struct gpio_bank *bank = _irq_data_get_bank(d);
Jon Hunterede4d7a2013-03-01 11:22:47 -0600658 unsigned int gpio = irq_to_gpio(bank, d->hwirq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100659
Benoit Cousson25db7112012-02-23 21:50:10 +0100660 return _set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100661}
662
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800663static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100664{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800665 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800666 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100667
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530668 /*
669 * If this is the first gpio_request for the bank,
670 * enable the bank module.
671 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200672 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530673 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100674
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530675 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300676 /* Set trigger to none. You need to enable the desired trigger with
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200677 * request_irq() or set_irq_type(). Only do this if the IRQ line has
678 * not already been requested.
Tony Lindgren4196dd62006-09-25 12:41:38 +0300679 */
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200680 if (!LINE_USED(bank->irq_usage, offset)) {
681 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
682 _enable_gpio_module(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100683 }
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200684 bank->mod_usage |= BIT(offset);
David Brownella6472532008-03-03 04:33:30 -0800685 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100686
687 return 0;
688}
689
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800690static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100691{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800692 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800693 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100694
David Brownella6472532008-03-03 04:33:30 -0800695 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200696 bank->mod_usage &= ~(BIT(offset));
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200697 _disable_gpio_module(bank, offset);
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800698 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800699 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530700
701 /*
702 * If this is the last gpio to be freed in the bank,
703 * disable the bank module.
704 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200705 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530706 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100707}
708
709/*
710 * We need to unmask the GPIO bank interrupt as soon as possible to
711 * avoid missing GPIO interrupts for other lines in the bank.
712 * Then we need to mask-read-clear-unmask the triggered GPIO lines
713 * in the bank to avoid missing nested interrupts for a GPIO line.
714 * If we wait to unmask individual GPIO lines in the bank after the
715 * line's interrupt handler has been run, we may miss some nested
716 * interrupts.
717 */
Russell King10dd5ce2006-11-23 11:41:32 +0000718static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100719{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100720 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100721 u32 isr;
Jon Hunter3513cde2013-04-04 15:16:14 -0500722 unsigned int bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700724 int unmasked = 0;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200725 struct irq_chip *irqchip = irq_desc_get_chip(desc);
726 struct gpio_chip *chip = irq_get_handler_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100727
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200728 chained_irq_enter(irqchip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100729
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200730 bank = container_of(chip, struct gpio_bank, chip);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700731 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530732 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800733
734 if (WARN_ON(!isr_reg))
735 goto exit;
736
Laurent Navete83507b2013-03-20 13:15:57 +0100737 while (1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100738 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700739 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100740
Imre Deakea6dedd2006-06-26 16:16:00 -0700741 enabled = _get_gpio_irqbank_mask(bank);
Victor Kamensky661553b2013-11-16 02:01:04 +0200742 isr_saved = isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100743
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530744 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800745 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100746
747 /* clear edge sensitive interrupts before handler(s) are
748 called so that we don't miss any interrupt occurred while
749 executing them */
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700750 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100751 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700752 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100753
754 /* if there is only edge sensitive GPIO pin interrupts
755 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700756 if (!level_mask && !unmasked) {
757 unmasked = 1;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200758 chained_irq_exit(irqchip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700759 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100760
Tony Lindgren92105bb2005-09-07 17:20:26 +0100761 if (!isr)
762 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100763
Jon Hunter3513cde2013-04-04 15:16:14 -0500764 while (isr) {
765 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200766 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100767
Cory Maccarrone4318f362010-01-08 10:29:04 -0800768 /*
769 * Some chips can't respond to both rising and falling
770 * at the same time. If this irq was requested with
771 * both flags, we need to flip the ICR data for the IRQ
772 * to respond to the IRQ for the opposite direction.
773 * This will be indicated in the bank toggle_mask.
774 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200775 if (bank->toggle_mask & (BIT(bit)))
Jon Hunter3513cde2013-04-04 15:16:14 -0500776 _toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800777
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200778 generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
779 bit));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100780 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000781 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700782 /* if bank has any level sensitive GPIO pin interrupt
783 configured, we must unmask the bank interrupt only after
784 handler(s) are executed in order to avoid spurious bank
785 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800786exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700787 if (!unmasked)
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200788 chained_irq_exit(irqchip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530789 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100790}
791
Lennert Buytenheke9191022010-11-29 11:17:17 +0100792static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300793{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200794 struct gpio_bank *bank = _irq_data_get_bank(d);
Jon Hunterede4d7a2013-03-01 11:22:47 -0600795 unsigned int gpio = irq_to_gpio(bank, d->hwirq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700796 unsigned long flags;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200797 unsigned offset = GPIO_INDEX(bank, gpio);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300798
Colin Cross85ec7b92011-06-06 13:38:18 -0700799 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200800 gpio_unlock_as_irq(&bank->chip, offset);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200801 bank->irq_usage &= ~(BIT(offset));
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200802 _disable_gpio_module(bank, offset);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300803 _reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700804 spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200805
806 /*
807 * If this is the last IRQ to be freed in the bank,
808 * disable the bank module.
809 */
810 if (!BANK_USED(bank))
811 pm_runtime_put(bank->dev);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300812}
813
Lennert Buytenheke9191022010-11-29 11:17:17 +0100814static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100815{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200816 struct gpio_bank *bank = _irq_data_get_bank(d);
Jon Hunterede4d7a2013-03-01 11:22:47 -0600817 unsigned int gpio = irq_to_gpio(bank, d->hwirq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100818
819 _clear_gpio_irqstatus(bank, gpio);
820}
821
Lennert Buytenheke9191022010-11-29 11:17:17 +0100822static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100823{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200824 struct gpio_bank *bank = _irq_data_get_bank(d);
Jon Hunterede4d7a2013-03-01 11:22:47 -0600825 unsigned int gpio = irq_to_gpio(bank, d->hwirq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700826 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100827
Colin Cross85ec7b92011-06-06 13:38:18 -0700828 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100829 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman129fd222011-04-22 07:59:07 -0700830 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700831 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100832}
833
Lennert Buytenheke9191022010-11-29 11:17:17 +0100834static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100835{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200836 struct gpio_bank *bank = _irq_data_get_bank(d);
Jon Hunterede4d7a2013-03-01 11:22:47 -0600837 unsigned int gpio = irq_to_gpio(bank, d->hwirq);
Kevin Hilman129fd222011-04-22 07:59:07 -0700838 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100839 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700840 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700841
Colin Cross85ec7b92011-06-06 13:38:18 -0700842 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700843 if (trigger)
Kevin Hilman129fd222011-04-22 07:59:07 -0700844 _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800845
846 /* For level-triggered GPIOs, the clearing must be done after
847 * the HW source is cleared, thus after the handler has run */
848 if (bank->level_mask & irq_mask) {
849 _set_gpio_irqenable(bank, gpio, 0);
850 _clear_gpio_irqstatus(bank, gpio);
851 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100852
Kevin Hilman4de8c752008-01-16 21:56:14 -0800853 _set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700854 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100855}
856
David Brownelle5c56ed2006-12-06 17:13:59 -0800857static struct irq_chip gpio_irq_chip = {
858 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +0100859 .irq_shutdown = gpio_irq_shutdown,
860 .irq_ack = gpio_ack_irq,
861 .irq_mask = gpio_mask_irq,
862 .irq_unmask = gpio_unmask_irq,
863 .irq_set_type = gpio_irq_type,
864 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -0800865};
866
867/*---------------------------------------------------------------------*/
868
Magnus Damm79ee0312009-07-08 13:22:04 +0200869static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800870{
Magnus Damm79ee0312009-07-08 13:22:04 +0200871 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800872 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800873 void __iomem *mask_reg = bank->base +
874 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800875 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800876
David Brownella6472532008-03-03 04:33:30 -0800877 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200878 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800879 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800880
881 return 0;
882}
883
Magnus Damm79ee0312009-07-08 13:22:04 +0200884static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800885{
Magnus Damm79ee0312009-07-08 13:22:04 +0200886 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800887 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800888 void __iomem *mask_reg = bank->base +
889 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800890 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800891
David Brownella6472532008-03-03 04:33:30 -0800892 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200893 writel_relaxed(bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800894 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800895
896 return 0;
897}
898
Alexey Dobriyan47145212009-12-14 18:00:08 -0800899static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200900 .suspend_noirq = omap_mpuio_suspend_noirq,
901 .resume_noirq = omap_mpuio_resume_noirq,
902};
903
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200904/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800905static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800906 .driver = {
907 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200908 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800909 },
910};
911
912static struct platform_device omap_mpuio_device = {
913 .name = "mpuio",
914 .id = -1,
915 .dev = {
916 .driver = &omap_mpuio_driver.driver,
917 }
918 /* could list the /proc/iomem resources */
919};
920
Charulatha V03e128c2011-05-05 19:58:01 +0530921static inline void mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800922{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800923 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700924
David Brownell11a78b72006-12-06 17:14:11 -0800925 if (platform_driver_register(&omap_mpuio_driver) == 0)
926 (void) platform_device_register(&omap_mpuio_device);
927}
928
David Brownelle5c56ed2006-12-06 17:13:59 -0800929/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100930
Yegor Yefremov93700842014-04-24 08:57:39 +0200931static int gpio_get_direction(struct gpio_chip *chip, unsigned offset)
932{
933 struct gpio_bank *bank;
934 unsigned long flags;
935 void __iomem *reg;
936 int dir;
937
938 bank = container_of(chip, struct gpio_bank, chip);
939 reg = bank->base + bank->regs->direction;
940 spin_lock_irqsave(&bank->lock, flags);
941 dir = !!(readl_relaxed(reg) & BIT(offset));
942 spin_unlock_irqrestore(&bank->lock, flags);
943 return dir;
944}
945
David Brownell52e31342008-03-03 12:43:23 -0800946static int gpio_input(struct gpio_chip *chip, unsigned offset)
947{
948 struct gpio_bank *bank;
949 unsigned long flags;
950
951 bank = container_of(chip, struct gpio_bank, chip);
952 spin_lock_irqsave(&bank->lock, flags);
953 _set_gpio_direction(bank, offset, 1);
954 spin_unlock_irqrestore(&bank->lock, flags);
955 return 0;
956}
957
958static int gpio_get(struct gpio_chip *chip, unsigned offset)
959{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300960 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300961 u32 mask;
962
Charulatha Va8be8da2011-04-22 16:38:16 +0530963 bank = container_of(chip, struct gpio_bank, chip);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200964 mask = (BIT(offset));
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300965
966 if (gpio_is_input(bank, mask))
Tarun Kanti DebBarma7fcca712012-02-27 11:46:09 +0530967 return _get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300968 else
Tarun Kanti DebBarma7fcca712012-02-27 11:46:09 +0530969 return _get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -0800970}
971
972static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
973{
974 struct gpio_bank *bank;
975 unsigned long flags;
976
977 bank = container_of(chip, struct gpio_bank, chip);
978 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700979 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -0800980 _set_gpio_direction(bank, offset, 0);
981 spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200982 return 0;
David Brownell52e31342008-03-03 12:43:23 -0800983}
984
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700985static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
986 unsigned debounce)
987{
988 struct gpio_bank *bank;
989 unsigned long flags;
990
991 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800992
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700993 spin_lock_irqsave(&bank->lock, flags);
994 _set_gpio_debounce(bank, offset, debounce);
995 spin_unlock_irqrestore(&bank->lock, flags);
996
997 return 0;
998}
999
David Brownell52e31342008-03-03 12:43:23 -08001000static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1001{
1002 struct gpio_bank *bank;
1003 unsigned long flags;
1004
1005 bank = container_of(chip, struct gpio_bank, chip);
1006 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001007 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001008 spin_unlock_irqrestore(&bank->lock, flags);
1009}
1010
1011/*---------------------------------------------------------------------*/
1012
Tony Lindgren9a748052010-12-07 16:26:56 -08001013static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001014{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001015 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001016 u32 rev;
1017
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001018 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001019 return;
1020
Victor Kamensky661553b2013-11-16 02:01:04 +02001021 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001022 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001023 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001024
1025 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001026}
1027
David Brownell8ba55c52008-02-26 11:10:50 -08001028/* This lock class tells lockdep that GPIO irqs are in a different
1029 * category than their parents, so it won't report false recursion.
1030 */
1031static struct lock_class_key gpio_lock_class;
1032
Charulatha V03e128c2011-05-05 19:58:01 +05301033static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001034{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301035 void __iomem *base = bank->base;
1036 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001037
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301038 if (bank->width == 16)
1039 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001040
Charulatha Vd0d665a2011-08-31 00:02:21 +05301041 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001042 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301043 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001044 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301045
1046 _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->irqenable_inv);
Tarun Kanti DebBarma6edd94d2012-04-30 12:50:12 +05301047 _gpio_rmw(base, bank->regs->irqstatus, l, !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301048 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001049 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301050
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301051 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001052 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301053 /* Initialize interface clk ungated, module enabled */
1054 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001055 writel_relaxed(0, base + bank->regs->ctrl);
Tarun Kanti DebBarma34672012012-07-11 14:43:14 +05301056
1057 bank->dbck = clk_get(bank->dev, "dbclk");
1058 if (IS_ERR(bank->dbck))
1059 dev_err(bank->dev, "Could not get gpio dbck\n");
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001060}
1061
Bill Pemberton38363092012-11-19 13:22:34 -05001062static void
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001063omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1064 unsigned int num)
1065{
1066 struct irq_chip_generic *gc;
1067 struct irq_chip_type *ct;
1068
1069 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1070 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -07001071 if (!gc) {
1072 dev_err(bank->dev, "Memory alloc failed for gc\n");
1073 return;
1074 }
1075
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001076 ct = gc->chip_types;
1077
1078 /* NOTE: No ack required, reading IRQ status clears it. */
1079 ct->chip.irq_mask = irq_gc_mask_set_bit;
1080 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1081 ct->chip.irq_set_type = gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301082
1083 if (bank->regs->wkup_en)
Julia Lawall388f4302013-08-13 09:16:56 +02001084 ct->chip.irq_set_wake = gpio_wake_enable;
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001085
1086 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1087 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1088 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1089}
1090
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001091static int omap_gpio_chip_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001092{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001093 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001094 static int gpio;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001095 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001096 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001097
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001098 /*
1099 * REVISIT eventually switch from OMAP-specific gpio structs
1100 * over to the generic ones
1101 */
1102 bank->chip.request = omap_gpio_request;
1103 bank->chip.free = omap_gpio_free;
Yegor Yefremov93700842014-04-24 08:57:39 +02001104 bank->chip.get_direction = gpio_get_direction;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001105 bank->chip.direction_input = gpio_input;
1106 bank->chip.get = gpio_get;
1107 bank->chip.direction_output = gpio_output;
1108 bank->chip.set_debounce = gpio_debounce;
1109 bank->chip.set = gpio_set;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301110 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001111 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301112 if (bank->regs->wkup_en)
1113 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001114 bank->chip.base = OMAP_MPUIO(0);
1115 } else {
1116 bank->chip.label = "gpio";
1117 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001118 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001119 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001120 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001121
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001122 ret = gpiochip_add(&bank->chip);
1123 if (ret) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001124 dev_err(bank->dev, "Could not register gpio chip %d\n", ret);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001125 return ret;
1126 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001127
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001128#ifdef CONFIG_ARCH_OMAP1
1129 /*
1130 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1131 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1132 */
1133 irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
1134 if (irq_base < 0) {
1135 dev_err(bank->dev, "Couldn't allocate IRQ numbers\n");
1136 return -ENODEV;
1137 }
1138#endif
1139
1140 ret = gpiochip_irqchip_add(&bank->chip, &gpio_irq_chip,
1141 irq_base, gpio_irq_handler,
1142 IRQ_TYPE_NONE);
1143
1144 if (ret) {
1145 dev_err(bank->dev, "Couldn't add irqchip to gpiochip %d\n", ret);
1146 ret = gpiochip_remove(&bank->chip);
1147 return -ENODEV;
1148 }
1149
1150 gpiochip_set_chained_irqchip(&bank->chip, &gpio_irq_chip,
1151 bank->irq, gpio_irq_handler);
1152
Jon Hunterede4d7a2013-03-01 11:22:47 -06001153 for (j = 0; j < bank->width; j++) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001154 int irq = irq_find_mapping(bank->chip.irqdomain, j);
Jon Hunterede4d7a2013-03-01 11:22:47 -06001155 irq_set_lockdep_class(irq, &gpio_lock_class);
Charulatha Vd0d665a2011-08-31 00:02:21 +05301156 if (bank->is_mpuio) {
Jon Hunterede4d7a2013-03-01 11:22:47 -06001157 omap_mpuio_alloc_gc(bank, irq, bank->width);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001158 irq_set_chip_and_handler(irq, NULL, NULL);
1159 set_irq_flags(irq, 0);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001160 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001161 }
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001162
1163 return 0;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001164}
1165
Benoit Cousson384ebe12011-08-16 11:53:02 +02001166static const struct of_device_id omap_gpio_match[];
1167
Bill Pemberton38363092012-11-19 13:22:34 -05001168static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001169{
Benoit Cousson862ff642012-02-01 15:58:56 +01001170 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001171 struct device_node *node = dev->of_node;
1172 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001173 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001174 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001175 struct gpio_bank *bank;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001176 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001177
Benoit Cousson384ebe12011-08-16 11:53:02 +02001178 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1179
Jingoo Hane56aee12013-07-30 17:08:05 +09001180 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001181 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001182 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001183
Tobias Klauser086d5852012-10-05 11:37:38 +02001184 bank = devm_kzalloc(dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301185 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001186 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001187 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301188 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001189
1190 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1191 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001192 dev_err(dev, "Invalid IRQ resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001193 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001194 }
1195
1196 bank->irq = res->start;
Benoit Cousson862ff642012-02-01 15:58:56 +01001197 bank->dev = dev;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001198 bank->chip.dev = dev;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001199 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001200 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001201 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301202 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301203 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001204 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001205#ifdef CONFIG_OF_GPIO
1206 bank->chip.of_node = of_node_get(node);
1207#endif
Jon Huntera2797be2013-04-04 15:16:15 -05001208 if (node) {
1209 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1210 bank->loses_context = true;
1211 } else {
1212 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001213
1214 if (bank->loses_context)
1215 bank->get_context_loss_count =
1216 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001217 }
1218
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001219 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1220 bank->set_dataout = _set_gpio_dataout_reg;
1221 else
1222 bank->set_dataout = _set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001223
1224 spin_lock_init(&bank->lock);
1225
1226 /* Static mapping, never released */
1227 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001228 bank->base = devm_ioremap_resource(dev, res);
1229 if (IS_ERR(bank->base)) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001230 irq_domain_remove(bank->chip.irqdomain);
Jingoo Han717f70e2014-02-12 11:51:38 +09001231 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001232 }
1233
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301234 platform_set_drvdata(pdev, bank);
1235
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001236 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301237 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001238 pm_runtime_get_sync(bank->dev);
1239
Charulatha Vd0d665a2011-08-31 00:02:21 +05301240 if (bank->is_mpuio)
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301241 mpuio_init(bank);
1242
Charulatha V03e128c2011-05-05 19:58:01 +05301243 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001244
1245 ret = omap_gpio_chip_init(bank);
1246 if (ret)
1247 return ret;
1248
Tony Lindgren9a748052010-12-07 16:26:56 -08001249 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001250
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301251 pm_runtime_put(bank->dev);
1252
Charulatha V03e128c2011-05-05 19:58:01 +05301253 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001254
Jon Hunter879fe322013-04-04 15:16:12 -05001255 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001256}
1257
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301258#ifdef CONFIG_ARCH_OMAP2PLUS
1259
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301260#if defined(CONFIG_PM_RUNTIME)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301261static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001262
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301263static int omap_gpio_runtime_suspend(struct device *dev)
1264{
1265 struct platform_device *pdev = to_platform_device(dev);
1266 struct gpio_bank *bank = platform_get_drvdata(pdev);
1267 u32 l1 = 0, l2 = 0;
1268 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001269 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301270
1271 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001272
1273 /*
1274 * Only edges can generate a wakeup event to the PRCM.
1275 *
1276 * Therefore, ensure any wake-up capable GPIOs have
1277 * edge-detection enabled before going idle to ensure a wakeup
1278 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1279 * NDA TRM 25.5.3.1)
1280 *
1281 * The normal values will be restored upon ->runtime_resume()
1282 * by writing back the values saved in bank->context.
1283 */
1284 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1285 if (wake_low)
Victor Kamensky661553b2013-11-16 02:01:04 +02001286 writel_relaxed(wake_low | bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001287 bank->base + bank->regs->fallingdetect);
1288 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1289 if (wake_hi)
Victor Kamensky661553b2013-11-16 02:01:04 +02001290 writel_relaxed(wake_hi | bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001291 bank->base + bank->regs->risingdetect);
1292
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001293 if (!bank->enabled_non_wakeup_gpios)
1294 goto update_gpio_context_count;
1295
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301296 if (bank->power_mode != OFF_MODE) {
1297 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301298 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301299 }
1300 /*
1301 * If going to OFF, remove triggering for all
1302 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1303 * generated. See OMAP2420 Errata item 1.101.
1304 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001305 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301306 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301307 l1 = bank->context.fallingdetect;
1308 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301309
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301310 l1 &= ~bank->enabled_non_wakeup_gpios;
1311 l2 &= ~bank->enabled_non_wakeup_gpios;
1312
Victor Kamensky661553b2013-11-16 02:01:04 +02001313 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1314 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301315
1316 bank->workaround_enabled = true;
1317
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301318update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301319 if (bank->get_context_loss_count)
1320 bank->context_loss_count =
1321 bank->get_context_loss_count(bank->dev);
1322
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +05301323 _gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301324 spin_unlock_irqrestore(&bank->lock, flags);
1325
1326 return 0;
1327}
1328
Jon Hunter352a2d52013-04-15 13:06:54 -05001329static void omap_gpio_init_context(struct gpio_bank *p);
1330
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301331static int omap_gpio_runtime_resume(struct device *dev)
1332{
1333 struct platform_device *pdev = to_platform_device(dev);
1334 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301335 u32 l = 0, gen, gen0, gen1;
1336 unsigned long flags;
Jon Huntera2797be2013-04-04 15:16:15 -05001337 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301338
1339 spin_lock_irqsave(&bank->lock, flags);
Jon Hunter352a2d52013-04-15 13:06:54 -05001340
1341 /*
1342 * On the first resume during the probe, the context has not
1343 * been initialised and so initialise it now. Also initialise
1344 * the context loss count.
1345 */
1346 if (bank->loses_context && !bank->context_valid) {
1347 omap_gpio_init_context(bank);
1348
1349 if (bank->get_context_loss_count)
1350 bank->context_loss_count =
1351 bank->get_context_loss_count(bank->dev);
1352 }
1353
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +05301354 _gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001355
1356 /*
1357 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1358 * GPIOs were set to edge trigger also in order to be able to
1359 * generate a PRCM wakeup. Here we restore the
1360 * pre-runtime_suspend() values for edge triggering.
1361 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001362 writel_relaxed(bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001363 bank->base + bank->regs->fallingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001364 writel_relaxed(bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001365 bank->base + bank->regs->risingdetect);
1366
Jon Huntera2797be2013-04-04 15:16:15 -05001367 if (bank->loses_context) {
1368 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301369 omap_gpio_restore_context(bank);
1370 } else {
Jon Huntera2797be2013-04-04 15:16:15 -05001371 c = bank->get_context_loss_count(bank->dev);
1372 if (c != bank->context_loss_count) {
1373 omap_gpio_restore_context(bank);
1374 } else {
1375 spin_unlock_irqrestore(&bank->lock, flags);
1376 return 0;
1377 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301378 }
1379 }
1380
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301381 if (!bank->workaround_enabled) {
1382 spin_unlock_irqrestore(&bank->lock, flags);
1383 return 0;
1384 }
1385
Victor Kamensky661553b2013-11-16 02:01:04 +02001386 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301387
1388 /*
1389 * Check if any of the non-wakeup interrupt GPIOs have changed
1390 * state. If so, generate an IRQ by software. This is
1391 * horribly racy, but it's the best we can do to work around
1392 * this silicon bug.
1393 */
1394 l ^= bank->saved_datain;
1395 l &= bank->enabled_non_wakeup_gpios;
1396
1397 /*
1398 * No need to generate IRQs for the rising edge for gpio IRQs
1399 * configured with falling edge only; and vice versa.
1400 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301401 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301402 gen0 &= bank->saved_datain;
1403
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301404 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301405 gen1 &= ~(bank->saved_datain);
1406
1407 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301408 gen = l & (~(bank->context.fallingdetect) &
1409 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301410 /* Consider all GPIO IRQs needed to be updated */
1411 gen |= gen0 | gen1;
1412
1413 if (gen) {
1414 u32 old0, old1;
1415
Victor Kamensky661553b2013-11-16 02:01:04 +02001416 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1417 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301418
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301419 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001420 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301421 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001422 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301423 bank->regs->leveldetect1);
1424 }
1425
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301426 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001427 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301428 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001429 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301430 bank->regs->leveldetect1);
1431 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001432 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1433 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301434 }
1435
1436 bank->workaround_enabled = false;
1437 spin_unlock_irqrestore(&bank->lock, flags);
1438
1439 return 0;
1440}
1441#endif /* CONFIG_PM_RUNTIME */
1442
1443void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001444{
Charulatha V03e128c2011-05-05 19:58:01 +05301445 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001446
Charulatha V03e128c2011-05-05 19:58:01 +05301447 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001448 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301449 continue;
1450
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301451 bank->power_mode = pwr_mode;
1452
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301453 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001454 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001455}
1456
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001457void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001458{
Charulatha V03e128c2011-05-05 19:58:01 +05301459 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001460
Charulatha V03e128c2011-05-05 19:58:01 +05301461 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001462 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301463 continue;
1464
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301465 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001466 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001467}
1468
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301469#if defined(CONFIG_PM_RUNTIME)
Jon Hunter352a2d52013-04-15 13:06:54 -05001470static void omap_gpio_init_context(struct gpio_bank *p)
1471{
1472 struct omap_gpio_reg_offs *regs = p->regs;
1473 void __iomem *base = p->base;
1474
Victor Kamensky661553b2013-11-16 02:01:04 +02001475 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1476 p->context.oe = readl_relaxed(base + regs->direction);
1477 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1478 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1479 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1480 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1481 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1482 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1483 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001484
1485 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001486 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001487 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001488 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001489
1490 p->context_valid = true;
1491}
1492
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301493static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301494{
Victor Kamensky661553b2013-11-16 02:01:04 +02001495 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301496 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001497 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1498 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301499 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001500 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301501 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001502 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301503 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001504 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301505 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301506 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001507 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301508 bank->base + bank->regs->set_dataout);
1509 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001510 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301511 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001512 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301513
Nishanth Menonae547352011-09-09 19:08:58 +05301514 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001515 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301516 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001517 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301518 bank->base + bank->regs->debounce_en);
1519 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301520
Victor Kamensky661553b2013-11-16 02:01:04 +02001521 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301522 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001523 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301524 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301525}
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301526#endif /* CONFIG_PM_RUNTIME */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301527#else
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301528#define omap_gpio_runtime_suspend NULL
1529#define omap_gpio_runtime_resume NULL
Arnd Bergmannea4a21a2013-05-31 17:59:46 +02001530static inline void omap_gpio_init_context(struct gpio_bank *p) {}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301531#endif
1532
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301533static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301534 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1535 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301536};
1537
Benoit Cousson384ebe12011-08-16 11:53:02 +02001538#if defined(CONFIG_OF)
1539static struct omap_gpio_reg_offs omap2_gpio_regs = {
1540 .revision = OMAP24XX_GPIO_REVISION,
1541 .direction = OMAP24XX_GPIO_OE,
1542 .datain = OMAP24XX_GPIO_DATAIN,
1543 .dataout = OMAP24XX_GPIO_DATAOUT,
1544 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1545 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1546 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1547 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1548 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1549 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1550 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1551 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1552 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1553 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1554 .ctrl = OMAP24XX_GPIO_CTRL,
1555 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1556 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1557 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1558 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1559 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1560};
1561
1562static struct omap_gpio_reg_offs omap4_gpio_regs = {
1563 .revision = OMAP4_GPIO_REVISION,
1564 .direction = OMAP4_GPIO_OE,
1565 .datain = OMAP4_GPIO_DATAIN,
1566 .dataout = OMAP4_GPIO_DATAOUT,
1567 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1568 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1569 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1570 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1571 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1572 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1573 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1574 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1575 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1576 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1577 .ctrl = OMAP4_GPIO_CTRL,
1578 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1579 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1580 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1581 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1582 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1583};
1584
Chen Gange9a65bb2013-02-06 18:44:32 +08001585static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001586 .regs = &omap2_gpio_regs,
1587 .bank_width = 32,
1588 .dbck_flag = false,
1589};
1590
Chen Gange9a65bb2013-02-06 18:44:32 +08001591static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001592 .regs = &omap2_gpio_regs,
1593 .bank_width = 32,
1594 .dbck_flag = true,
1595};
1596
Chen Gange9a65bb2013-02-06 18:44:32 +08001597static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001598 .regs = &omap4_gpio_regs,
1599 .bank_width = 32,
1600 .dbck_flag = true,
1601};
1602
1603static const struct of_device_id omap_gpio_match[] = {
1604 {
1605 .compatible = "ti,omap4-gpio",
1606 .data = &omap4_pdata,
1607 },
1608 {
1609 .compatible = "ti,omap3-gpio",
1610 .data = &omap3_pdata,
1611 },
1612 {
1613 .compatible = "ti,omap2-gpio",
1614 .data = &omap2_pdata,
1615 },
1616 { },
1617};
1618MODULE_DEVICE_TABLE(of, omap_gpio_match);
1619#endif
1620
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001621static struct platform_driver omap_gpio_driver = {
1622 .probe = omap_gpio_probe,
1623 .driver = {
1624 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301625 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001626 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001627 },
1628};
1629
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001630/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001631 * gpio driver register needs to be done before
1632 * machine_init functions access gpio APIs.
1633 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001634 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001635static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001636{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001637 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001638}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001639postcore_initcall(omap_gpio_drv_reg);