blob: 30646cfe0efa91e2378fa0e5871dec14cd7ece7d [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>
Tony Lindgren4b254082012-08-30 15:37:24 -070027#include <linux/gpio.h>
Yegor Yefremov93700842014-04-24 08:57:39 +020028#include <linux/bitops.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070029#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)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020085#define GPIO_BIT(bank, gpio) (BIT(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)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020089#define LINE_USED(line, offset) (line & (BIT(offset)))
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020090
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020091static int omap_irq_to_gpio(struct gpio_bank *bank, unsigned int gpio_irq)
Benoit Cousson25db7112012-02-23 21:50:10 +010092{
Jon Hunterede4d7a2013-03-01 11:22:47 -060093 return bank->chip.base + gpio_irq;
94}
95
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020096static inline struct gpio_bank *omap_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
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200102static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
103 int is_input)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100104{
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 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200120static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, int gpio,
121 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100122{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100123 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700124 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100125
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530126 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700127 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530128 bank->context.dataout |= l;
129 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700130 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530131 bank->context.dataout &= ~l;
132 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700133
Victor Kamensky661553b2013-11-16 02:01:04 +0200134 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700135}
136
137/* set data out value using mask register */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200138static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, int gpio,
139 int enable)
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700140{
141 void __iomem *reg = bank->base + bank->regs->dataout;
142 u32 gpio_bit = GPIO_BIT(bank, gpio);
143 u32 l;
144
Victor Kamensky661553b2013-11-16 02:01:04 +0200145 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700146 if (enable)
147 l |= gpio_bit;
148 else
149 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200150 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530151 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100152}
153
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200154static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100155{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700156 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100157
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200158 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100159}
160
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200161static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300162{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700163 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300164
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200165 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300166}
167
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200168static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700169{
Victor Kamensky661553b2013-11-16 02:01:04 +0200170 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700171
Benoit Cousson862ff642012-02-01 15:58:56 +0100172 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700173 l |= mask;
174 else
175 l &= ~mask;
176
Victor Kamensky661553b2013-11-16 02:01:04 +0200177 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700178}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100179
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200180static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530181{
182 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Rajendra Nayak345477f2014-04-23 11:41:03 +0530183 clk_prepare_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530184 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300185
Victor Kamensky661553b2013-11-16 02:01:04 +0200186 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300187 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530188 }
189}
190
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200191static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530192{
193 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300194 /*
195 * Disable debounce before cutting it's clock. If debounce is
196 * enabled but the clock is not, GPIO module seems to be unable
197 * to detect events and generate interrupts at least on OMAP3.
198 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200199 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300200
Rajendra Nayak345477f2014-04-23 11:41:03 +0530201 clk_disable_unprepare(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530202 bank->dbck_enabled = false;
203 }
204}
205
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700206/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200207 * omap2_set_gpio_debounce - low level gpio debounce time
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700208 * @bank: the gpio bank we're acting upon
209 * @gpio: the gpio number on this @gpio
210 * @debounce: debounce time to use
211 *
212 * OMAP's debounce time is in 31us steps so we need
213 * to convert and round up to the closest unit.
214 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200215static void omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
216 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700217{
Kevin Hilman9942da02011-04-22 12:02:05 -0700218 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700219 u32 val;
220 u32 l;
221
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800222 if (!bank->dbck_flag)
223 return;
224
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700225 if (debounce < 32)
226 debounce = 0x01;
227 else if (debounce > 7936)
228 debounce = 0xff;
229 else
230 debounce = (debounce / 0x1f) - 1;
231
Kevin Hilman129fd222011-04-22 07:59:07 -0700232 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700233
Rajendra Nayak345477f2014-04-23 11:41:03 +0530234 clk_prepare_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700235 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200236 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700237
Kevin Hilman9942da02011-04-22 12:02:05 -0700238 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200239 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700240
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530241 if (debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700242 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530243 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700244 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300245 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700246
Victor Kamensky661553b2013-11-16 02:01:04 +0200247 writel_relaxed(val, reg);
Rajendra Nayak345477f2014-04-23 11:41:03 +0530248 clk_disable_unprepare(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530249 /*
250 * Enable debounce clock per module.
251 * This call is mandatory because in omap_gpio_request() when
252 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
253 * runtime callbck fails to turn on dbck because dbck_enable_mask
254 * used within _gpio_dbck_enable() is still not initialized at
255 * that point. Therefore we have to enable dbck here.
256 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200257 omap_gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530258 if (bank->dbck_enable_mask) {
259 bank->context.debounce = debounce;
260 bank->context.debounce_en = val;
261 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700262}
263
Jon Hunterc9c55d92012-10-26 14:26:04 -0500264/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200265 * omap_clear_gpio_debounce - clear debounce settings for a gpio
Jon Hunterc9c55d92012-10-26 14:26:04 -0500266 * @bank: the gpio bank we're acting upon
267 * @gpio: the gpio number on this @gpio
268 *
269 * If a gpio is using debounce, then clear the debounce enable bit and if
270 * this is the only gpio in this bank using debounce, then clear the debounce
271 * time too. The debounce clock will also be disabled when calling this function
272 * if this is the only gpio in the bank using debounce.
273 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200274static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned gpio)
Jon Hunterc9c55d92012-10-26 14:26:04 -0500275{
276 u32 gpio_bit = GPIO_BIT(bank, gpio);
277
278 if (!bank->dbck_flag)
279 return;
280
281 if (!(bank->dbck_enable_mask & gpio_bit))
282 return;
283
284 bank->dbck_enable_mask &= ~gpio_bit;
285 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200286 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500287 bank->base + bank->regs->debounce_en);
288
289 if (!bank->dbck_enable_mask) {
290 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200291 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500292 bank->regs->debounce);
Rajendra Nayak345477f2014-04-23 11:41:03 +0530293 clk_disable_unprepare(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500294 bank->dbck_enabled = false;
295 }
296}
297
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200298static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530299 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100300{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800301 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200302 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100303
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200304 omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
305 trigger & IRQ_TYPE_LEVEL_LOW);
306 omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
307 trigger & IRQ_TYPE_LEVEL_HIGH);
308 omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
309 trigger & IRQ_TYPE_EDGE_RISING);
310 omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
311 trigger & IRQ_TYPE_EDGE_FALLING);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530312
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530313 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200314 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530315 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200316 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530317 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200318 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530319 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200320 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530321
322 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200323 omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530324 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200325 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530326 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530327
Ambresh K55b220c2011-06-15 13:40:45 -0700328 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530329 if (!bank->regs->irqctrl) {
330 /* On omap24xx proceed only when valid GPIO bit is set */
331 if (bank->non_wakeup_gpios) {
332 if (!(bank->non_wakeup_gpios & gpio_bit))
333 goto exit;
334 }
335
Chunqiu Wang699117a62009-06-24 17:13:39 +0000336 /*
337 * Log the edge gpio and manually trigger the IRQ
338 * after resume if the input level changes
339 * to avoid irq lost during PER RET/OFF mode
340 * Applies for omap2 non-wakeup gpio and all omap3 gpios
341 */
342 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800343 bank->enabled_non_wakeup_gpios |= gpio_bit;
344 else
345 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
346 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700347
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530348exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530349 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200350 readl_relaxed(bank->base + bank->regs->leveldetect0) |
351 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100352}
353
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800354#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800355/*
356 * This only applies to chips that can't do both rising and falling edge
357 * detection at once. For all other chips, this function is a noop.
358 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200359static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800360{
361 void __iomem *reg = bank->base;
362 u32 l = 0;
363
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530364 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800365 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530366
367 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800368
Victor Kamensky661553b2013-11-16 02:01:04 +0200369 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800370 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200371 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800372 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200373 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800374
Victor Kamensky661553b2013-11-16 02:01:04 +0200375 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800376}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530377#else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200378static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800379#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800380
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200381static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
382 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100383{
384 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530385 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100386 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100387
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530388 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200389 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530390 } else if (bank->regs->irqctrl) {
391 reg += bank->regs->irqctrl;
392
Victor Kamensky661553b2013-11-16 02:01:04 +0200393 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000394 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200395 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100396 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200397 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100398 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200399 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100400 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530401 return -EINVAL;
402
Victor Kamensky661553b2013-11-16 02:01:04 +0200403 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530404 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100405 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530406 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100407 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530408 reg += bank->regs->edgectrl1;
409
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100410 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200411 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100412 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100413 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100414 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100415 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200416 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530417
418 /* Enable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200419 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530420 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200421 readl_relaxed(bank->base + bank->regs->wkup_en);
422 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100423 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100424 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100425}
426
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200427static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200428{
429 if (bank->regs->pinctrl) {
430 void __iomem *reg = bank->base + bank->regs->pinctrl;
431
432 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200433 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200434 }
435
436 if (bank->regs->ctrl && !BANK_USED(bank)) {
437 void __iomem *reg = bank->base + bank->regs->ctrl;
438 u32 ctrl;
439
Victor Kamensky661553b2013-11-16 02:01:04 +0200440 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200441 /* Module is enabled, clocks are not gated */
442 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200443 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200444 bank->context.ctrl = ctrl;
445 }
446}
447
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200448static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200449{
450 void __iomem *base = bank->base;
451
452 if (bank->regs->wkup_en &&
453 !LINE_USED(bank->mod_usage, offset) &&
454 !LINE_USED(bank->irq_usage, offset)) {
455 /* Disable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200456 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200457 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200458 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200459 }
460
461 if (bank->regs->ctrl && !BANK_USED(bank)) {
462 void __iomem *reg = bank->base + bank->regs->ctrl;
463 u32 ctrl;
464
Victor Kamensky661553b2013-11-16 02:01:04 +0200465 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200466 /* Module is disabled, clocks are gated */
467 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200468 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200469 bank->context.ctrl = ctrl;
470 }
471}
472
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200473static int omap_gpio_is_input(struct gpio_bank *bank, int mask)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200474{
475 void __iomem *reg = bank->base + bank->regs->direction;
476
Victor Kamensky661553b2013-11-16 02:01:04 +0200477 return readl_relaxed(reg) & mask;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200478}
479
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200480static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100481{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200482 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren4b254082012-08-30 15:37:24 -0700483 unsigned gpio = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100484 int retval;
David Brownella6472532008-03-03 04:33:30 -0800485 unsigned long flags;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200486 unsigned offset;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100487
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200488 if (!BANK_USED(bank))
489 pm_runtime_get_sync(bank->dev);
Jon Hunter8d4c2772013-03-01 11:22:48 -0600490
Tony Lindgren4b254082012-08-30 15:37:24 -0700491#ifdef CONFIG_ARCH_OMAP1
492 if (d->irq > IH_MPUIO_BASE)
Lennert Buytenheke9191022010-11-29 11:17:17 +0100493 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren4b254082012-08-30 15:37:24 -0700494#endif
495
496 if (!gpio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200497 gpio = omap_irq_to_gpio(bank, d->hwirq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100498
David Brownelle5c56ed2006-12-06 17:13:59 -0800499 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100500 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800501
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530502 if (!bank->regs->leveldetect0 &&
503 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100504 return -EINVAL;
505
David Brownella6472532008-03-03 04:33:30 -0800506 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200507 offset = GPIO_INDEX(bank, gpio);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200508 retval = omap_set_gpio_triggering(bank, offset, type);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200509 if (!LINE_USED(bank->mod_usage, offset)) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200510 omap_enable_gpio_module(bank, offset);
511 omap_set_gpio_direction(bank, offset, 1);
512 } else if (!omap_gpio_is_input(bank, BIT(offset))) {
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200513 spin_unlock_irqrestore(&bank->lock, flags);
514 return -EINVAL;
515 }
516
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200517 bank->irq_usage |= BIT(GPIO_INDEX(bank, gpio));
David Brownella6472532008-03-03 04:33:30 -0800518 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800519
520 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100521 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800522 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100523 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800524
Tony Lindgren92105bb2005-09-07 17:20:26 +0100525 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100526}
527
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200528static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100530 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100531
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700532 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200533 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300534
535 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700536 if (bank->regs->irqstatus2) {
537 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200538 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700539 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700540
541 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200542 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100543}
544
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200545static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100546{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200547 omap_clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100548}
549
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200550static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
Imre Deakea6dedd2006-06-26 16:16:00 -0700551{
552 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700553 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200554 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700555
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700556 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200557 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700558 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700559 l = ~l;
560 l &= mask;
561 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700562}
563
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200564static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100565{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100566 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100567 u32 l;
568
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700569 if (bank->regs->set_irqenable) {
570 reg += bank->regs->set_irqenable;
571 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530572 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700573 } else {
574 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200575 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700576 if (bank->regs->irqenable_inv)
577 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578 else
579 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530580 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100581 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700582
Victor Kamensky661553b2013-11-16 02:01:04 +0200583 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700584}
585
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200586static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700587{
588 void __iomem *reg = bank->base;
589 u32 l;
590
591 if (bank->regs->clr_irqenable) {
592 reg += bank->regs->clr_irqenable;
593 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530594 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700595 } else {
596 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200597 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700598 if (bank->regs->irqenable_inv)
599 l |= gpio_mask;
600 else
601 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530602 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700603 }
604
Victor Kamensky661553b2013-11-16 02:01:04 +0200605 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100606}
607
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200608static inline void omap_set_gpio_irqenable(struct gpio_bank *bank, int gpio,
609 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100610{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530611 if (enable)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200612 omap_enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530613 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200614 omap_disable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100615}
616
Tony Lindgren92105bb2005-09-07 17:20:26 +0100617/*
618 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
619 * 1510 does not seem to have a wake-up register. If JTAG is connected
620 * to the target, system will wake up always on GPIO events. While
621 * system is running all registered GPIO interrupts need to have wake-up
622 * enabled. When system is suspended, only selected GPIO interrupts need
623 * to have wake-up enabled.
624 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200625static int omap_set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100626{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700627 u32 gpio_bit = GPIO_BIT(bank, gpio);
628 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800629
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700630 if (bank->non_wakeup_gpios & gpio_bit) {
Benoit Cousson862ff642012-02-01 15:58:56 +0100631 dev_err(bank->dev,
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700632 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100633 return -EINVAL;
634 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700635
636 spin_lock_irqsave(&bank->lock, flags);
637 if (enable)
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530638 bank->context.wake_en |= gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700639 else
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530640 bank->context.wake_en &= ~gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700641
Victor Kamensky661553b2013-11-16 02:01:04 +0200642 writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700643 spin_unlock_irqrestore(&bank->lock, flags);
644
645 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100646}
647
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200648static void omap_reset_gpio(struct gpio_bank *bank, int gpio)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300649{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200650 omap_set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
651 omap_set_gpio_irqenable(bank, gpio, 0);
652 omap_clear_gpio_irqstatus(bank, gpio);
653 omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
654 omap_clear_gpio_debounce(bank, gpio);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300655}
656
Tony Lindgren92105bb2005-09-07 17:20:26 +0100657/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200658static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100659{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200660 struct gpio_bank *bank = omap_irq_data_get_bank(d);
661 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100662
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200663 return omap_set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100664}
665
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800666static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100667{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800668 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800669 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530671 /*
672 * If this is the first gpio_request for the bank,
673 * enable the bank module.
674 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200675 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530676 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100677
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530678 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300679 /* Set trigger to none. You need to enable the desired trigger with
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200680 * request_irq() or set_irq_type(). Only do this if the IRQ line has
681 * not already been requested.
Tony Lindgren4196dd62006-09-25 12:41:38 +0300682 */
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200683 if (!LINE_USED(bank->irq_usage, offset)) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200684 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
685 omap_enable_gpio_module(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100686 }
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200687 bank->mod_usage |= BIT(offset);
David Brownella6472532008-03-03 04:33:30 -0800688 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100689
690 return 0;
691}
692
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800693static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100694{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800695 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800696 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100697
David Brownella6472532008-03-03 04:33:30 -0800698 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200699 bank->mod_usage &= ~(BIT(offset));
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200700 omap_disable_gpio_module(bank, offset);
701 omap_reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800702 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530703
704 /*
705 * If this is the last gpio to be freed in the bank,
706 * disable the bank module.
707 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200708 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530709 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100710}
711
712/*
713 * We need to unmask the GPIO bank interrupt as soon as possible to
714 * avoid missing GPIO interrupts for other lines in the bank.
715 * Then we need to mask-read-clear-unmask the triggered GPIO lines
716 * in the bank to avoid missing nested interrupts for a GPIO line.
717 * If we wait to unmask individual GPIO lines in the bank after the
718 * line's interrupt handler has been run, we may miss some nested
719 * interrupts.
720 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200721static void omap_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100722{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100723 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100724 u32 isr;
Jon Hunter3513cde2013-04-04 15:16:14 -0500725 unsigned int bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700727 int unmasked = 0;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200728 struct irq_chip *irqchip = irq_desc_get_chip(desc);
729 struct gpio_chip *chip = irq_get_handler_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200731 chained_irq_enter(irqchip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200733 bank = container_of(chip, struct gpio_bank, chip);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700734 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530735 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800736
737 if (WARN_ON(!isr_reg))
738 goto exit;
739
Laurent Navete83507b2013-03-20 13:15:57 +0100740 while (1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100741 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700742 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100743
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200744 enabled = omap_get_gpio_irqbank_mask(bank);
Victor Kamensky661553b2013-11-16 02:01:04 +0200745 isr_saved = isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100746
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530747 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800748 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100749
750 /* clear edge sensitive interrupts before handler(s) are
751 called so that we don't miss any interrupt occurred while
752 executing them */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200753 omap_disable_gpio_irqbank(bank, isr_saved & ~level_mask);
754 omap_clear_gpio_irqbank(bank, isr_saved & ~level_mask);
755 omap_enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100756
757 /* if there is only edge sensitive GPIO pin interrupts
758 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700759 if (!level_mask && !unmasked) {
760 unmasked = 1;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200761 chained_irq_exit(irqchip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700762 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100763
Tony Lindgren92105bb2005-09-07 17:20:26 +0100764 if (!isr)
765 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100766
Jon Hunter3513cde2013-04-04 15:16:14 -0500767 while (isr) {
768 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200769 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100770
Cory Maccarrone4318f362010-01-08 10:29:04 -0800771 /*
772 * Some chips can't respond to both rising and falling
773 * at the same time. If this irq was requested with
774 * both flags, we need to flip the ICR data for the IRQ
775 * to respond to the IRQ for the opposite direction.
776 * This will be indicated in the bank toggle_mask.
777 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200778 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200779 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800780
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200781 generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
782 bit));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100783 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000784 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700785 /* if bank has any level sensitive GPIO pin interrupt
786 configured, we must unmask the bank interrupt only after
787 handler(s) are executed in order to avoid spurious bank
788 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800789exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700790 if (!unmasked)
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200791 chained_irq_exit(irqchip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530792 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100793}
794
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200795static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300796{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200797 struct gpio_bank *bank = omap_irq_data_get_bank(d);
798 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700799 unsigned long flags;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200800 unsigned offset = GPIO_INDEX(bank, gpio);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300801
Colin Cross85ec7b92011-06-06 13:38:18 -0700802 spin_lock_irqsave(&bank->lock, flags);
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900803 gpiochip_unlock_as_irq(&bank->chip, offset);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200804 bank->irq_usage &= ~(BIT(offset));
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200805 omap_disable_gpio_module(bank, offset);
806 omap_reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700807 spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200808
809 /*
810 * If this is the last IRQ to be freed in the bank,
811 * disable the bank module.
812 */
813 if (!BANK_USED(bank))
814 pm_runtime_put(bank->dev);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300815}
816
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200817static void omap_gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100818{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200819 struct gpio_bank *bank = omap_irq_data_get_bank(d);
820 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100821
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200822 omap_clear_gpio_irqstatus(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100823}
824
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200825static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100826{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200827 struct gpio_bank *bank = omap_irq_data_get_bank(d);
828 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700829 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100830
Colin Cross85ec7b92011-06-06 13:38:18 -0700831 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200832 omap_set_gpio_irqenable(bank, gpio, 0);
833 omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700834 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100835}
836
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200837static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100838{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200839 struct gpio_bank *bank = omap_irq_data_get_bank(d);
840 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Kevin Hilman129fd222011-04-22 07:59:07 -0700841 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100842 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700843 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700844
Colin Cross85ec7b92011-06-06 13:38:18 -0700845 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700846 if (trigger)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200847 omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800848
849 /* For level-triggered GPIOs, the clearing must be done after
850 * the HW source is cleared, thus after the handler has run */
851 if (bank->level_mask & irq_mask) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200852 omap_set_gpio_irqenable(bank, gpio, 0);
853 omap_clear_gpio_irqstatus(bank, gpio);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800854 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100855
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200856 omap_set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700857 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100858}
859
David Brownelle5c56ed2006-12-06 17:13:59 -0800860/*---------------------------------------------------------------------*/
861
Magnus Damm79ee0312009-07-08 13:22:04 +0200862static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800863{
Magnus Damm79ee0312009-07-08 13:22:04 +0200864 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800865 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800866 void __iomem *mask_reg = bank->base +
867 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800868 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800869
David Brownella6472532008-03-03 04:33:30 -0800870 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200871 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800872 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800873
874 return 0;
875}
876
Magnus Damm79ee0312009-07-08 13:22:04 +0200877static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800878{
Magnus Damm79ee0312009-07-08 13:22:04 +0200879 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800880 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800881 void __iomem *mask_reg = bank->base +
882 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800883 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800884
David Brownella6472532008-03-03 04:33:30 -0800885 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200886 writel_relaxed(bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800887 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800888
889 return 0;
890}
891
Alexey Dobriyan47145212009-12-14 18:00:08 -0800892static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200893 .suspend_noirq = omap_mpuio_suspend_noirq,
894 .resume_noirq = omap_mpuio_resume_noirq,
895};
896
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200897/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800898static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800899 .driver = {
900 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200901 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800902 },
903};
904
905static struct platform_device omap_mpuio_device = {
906 .name = "mpuio",
907 .id = -1,
908 .dev = {
909 .driver = &omap_mpuio_driver.driver,
910 }
911 /* could list the /proc/iomem resources */
912};
913
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200914static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800915{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800916 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700917
David Brownell11a78b72006-12-06 17:14:11 -0800918 if (platform_driver_register(&omap_mpuio_driver) == 0)
919 (void) platform_device_register(&omap_mpuio_device);
920}
921
David Brownelle5c56ed2006-12-06 17:13:59 -0800922/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100923
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200924static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200925{
926 struct gpio_bank *bank;
927 unsigned long flags;
928 void __iomem *reg;
929 int dir;
930
931 bank = container_of(chip, struct gpio_bank, chip);
932 reg = bank->base + bank->regs->direction;
933 spin_lock_irqsave(&bank->lock, flags);
934 dir = !!(readl_relaxed(reg) & BIT(offset));
935 spin_unlock_irqrestore(&bank->lock, flags);
936 return dir;
937}
938
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200939static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800940{
941 struct gpio_bank *bank;
942 unsigned long flags;
943
944 bank = container_of(chip, struct gpio_bank, chip);
945 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200946 omap_set_gpio_direction(bank, offset, 1);
David Brownell52e31342008-03-03 12:43:23 -0800947 spin_unlock_irqrestore(&bank->lock, flags);
948 return 0;
949}
950
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200951static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800952{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300953 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300954 u32 mask;
955
Charulatha Va8be8da2011-04-22 16:38:16 +0530956 bank = container_of(chip, struct gpio_bank, chip);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200957 mask = (BIT(offset));
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300958
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200959 if (omap_gpio_is_input(bank, mask))
960 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300961 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200962 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -0800963}
964
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200965static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800966{
967 struct gpio_bank *bank;
968 unsigned long flags;
969
970 bank = container_of(chip, struct gpio_bank, chip);
971 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700972 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200973 omap_set_gpio_direction(bank, offset, 0);
David Brownell52e31342008-03-03 12:43:23 -0800974 spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200975 return 0;
David Brownell52e31342008-03-03 12:43:23 -0800976}
977
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200978static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
979 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700980{
981 struct gpio_bank *bank;
982 unsigned long flags;
983
984 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800985
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700986 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200987 omap2_set_gpio_debounce(bank, offset, debounce);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700988 spin_unlock_irqrestore(&bank->lock, flags);
989
990 return 0;
991}
992
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200993static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800994{
995 struct gpio_bank *bank;
996 unsigned long flags;
997
998 bank = container_of(chip, struct gpio_bank, chip);
999 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001000 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001001 spin_unlock_irqrestore(&bank->lock, flags);
1002}
1003
1004/*---------------------------------------------------------------------*/
1005
Tony Lindgren9a748052010-12-07 16:26:56 -08001006static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001007{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001008 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001009 u32 rev;
1010
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001011 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001012 return;
1013
Victor Kamensky661553b2013-11-16 02:01:04 +02001014 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001015 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001016 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001017
1018 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001019}
1020
Charulatha V03e128c2011-05-05 19:58:01 +05301021static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001022{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301023 void __iomem *base = bank->base;
1024 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001025
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301026 if (bank->width == 16)
1027 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001028
Charulatha Vd0d665a2011-08-31 00:02:21 +05301029 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001030 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301031 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001032 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301033
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001034 omap_gpio_rmw(base, bank->regs->irqenable, l,
1035 bank->regs->irqenable_inv);
1036 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1037 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301038 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001039 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301040
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301041 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001042 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301043 /* Initialize interface clk ungated, module enabled */
1044 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001045 writel_relaxed(0, base + bank->regs->ctrl);
Tarun Kanti DebBarma34672012012-07-11 14:43:14 +05301046
1047 bank->dbck = clk_get(bank->dev, "dbclk");
1048 if (IS_ERR(bank->dbck))
1049 dev_err(bank->dev, "Could not get gpio dbck\n");
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001050}
1051
Bill Pemberton38363092012-11-19 13:22:34 -05001052static void
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001053omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1054 unsigned int num)
1055{
1056 struct irq_chip_generic *gc;
1057 struct irq_chip_type *ct;
1058
1059 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1060 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -07001061 if (!gc) {
1062 dev_err(bank->dev, "Memory alloc failed for gc\n");
1063 return;
1064 }
1065
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001066 ct = gc->chip_types;
1067
1068 /* NOTE: No ack required, reading IRQ status clears it. */
1069 ct->chip.irq_mask = irq_gc_mask_set_bit;
1070 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001071 ct->chip.irq_set_type = omap_gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301072
1073 if (bank->regs->wkup_en)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001074 ct->chip.irq_set_wake = omap_gpio_wake_enable;
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001075
1076 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1077 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1078 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1079}
1080
Nishanth Menon46824e222014-09-05 14:52:55 -05001081static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001082{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001083 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001084 static int gpio;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001085 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001086 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001087
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001088 /*
1089 * REVISIT eventually switch from OMAP-specific gpio structs
1090 * over to the generic ones
1091 */
1092 bank->chip.request = omap_gpio_request;
1093 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001094 bank->chip.get_direction = omap_gpio_get_direction;
1095 bank->chip.direction_input = omap_gpio_input;
1096 bank->chip.get = omap_gpio_get;
1097 bank->chip.direction_output = omap_gpio_output;
1098 bank->chip.set_debounce = omap_gpio_debounce;
1099 bank->chip.set = omap_gpio_set;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301100 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001101 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301102 if (bank->regs->wkup_en)
1103 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001104 bank->chip.base = OMAP_MPUIO(0);
1105 } else {
1106 bank->chip.label = "gpio";
1107 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001108 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001109 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001110 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001111
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001112 ret = gpiochip_add(&bank->chip);
1113 if (ret) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001114 dev_err(bank->dev, "Could not register gpio chip %d\n", ret);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001115 return ret;
1116 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001117
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001118#ifdef CONFIG_ARCH_OMAP1
1119 /*
1120 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1121 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1122 */
1123 irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
1124 if (irq_base < 0) {
1125 dev_err(bank->dev, "Couldn't allocate IRQ numbers\n");
1126 return -ENODEV;
1127 }
1128#endif
1129
Nishanth Menon46824e222014-09-05 14:52:55 -05001130 ret = gpiochip_irqchip_add(&bank->chip, irqc,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001131 irq_base, omap_gpio_irq_handler,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001132 IRQ_TYPE_NONE);
1133
1134 if (ret) {
1135 dev_err(bank->dev, "Couldn't add irqchip to gpiochip %d\n", ret);
Linus Walleijda26d5d2014-09-16 15:11:41 -07001136 gpiochip_remove(&bank->chip);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001137 return -ENODEV;
1138 }
1139
Nishanth Menon46824e222014-09-05 14:52:55 -05001140 gpiochip_set_chained_irqchip(&bank->chip, irqc,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001141 bank->irq, omap_gpio_irq_handler);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001142
Jon Hunterede4d7a2013-03-01 11:22:47 -06001143 for (j = 0; j < bank->width; j++) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001144 int irq = irq_find_mapping(bank->chip.irqdomain, j);
Charulatha Vd0d665a2011-08-31 00:02:21 +05301145 if (bank->is_mpuio) {
Jon Hunterede4d7a2013-03-01 11:22:47 -06001146 omap_mpuio_alloc_gc(bank, irq, bank->width);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001147 irq_set_chip_and_handler(irq, NULL, NULL);
1148 set_irq_flags(irq, 0);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001149 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001150 }
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001151
1152 return 0;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001153}
1154
Benoit Cousson384ebe12011-08-16 11:53:02 +02001155static const struct of_device_id omap_gpio_match[];
1156
Bill Pemberton38363092012-11-19 13:22:34 -05001157static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001158{
Benoit Cousson862ff642012-02-01 15:58:56 +01001159 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001160 struct device_node *node = dev->of_node;
1161 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001162 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001163 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001164 struct gpio_bank *bank;
Nishanth Menon46824e222014-09-05 14:52:55 -05001165 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001166 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001167
Benoit Cousson384ebe12011-08-16 11:53:02 +02001168 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1169
Jingoo Hane56aee12013-07-30 17:08:05 +09001170 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001171 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001172 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001173
Tobias Klauser086d5852012-10-05 11:37:38 +02001174 bank = devm_kzalloc(dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301175 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001176 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001177 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301178 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001179
Nishanth Menon46824e222014-09-05 14:52:55 -05001180 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1181 if (!irqc)
1182 return -ENOMEM;
1183
1184 irqc->irq_shutdown = omap_gpio_irq_shutdown,
1185 irqc->irq_ack = omap_gpio_ack_irq,
1186 irqc->irq_mask = omap_gpio_mask_irq,
1187 irqc->irq_unmask = omap_gpio_unmask_irq,
1188 irqc->irq_set_type = omap_gpio_irq_type,
1189 irqc->irq_set_wake = omap_gpio_wake_enable,
1190 irqc->name = dev_name(&pdev->dev);
1191
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001192 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1193 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001194 dev_err(dev, "Invalid IRQ resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001195 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001196 }
1197
1198 bank->irq = res->start;
Benoit Cousson862ff642012-02-01 15:58:56 +01001199 bank->dev = dev;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001200 bank->chip.dev = dev;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001201 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001202 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001203 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301204 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301205 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001206 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001207#ifdef CONFIG_OF_GPIO
1208 bank->chip.of_node = of_node_get(node);
1209#endif
Jon Huntera2797be2013-04-04 15:16:15 -05001210 if (node) {
1211 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1212 bank->loses_context = true;
1213 } else {
1214 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001215
1216 if (bank->loses_context)
1217 bank->get_context_loss_count =
1218 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001219 }
1220
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001221 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001222 bank->set_dataout = omap_set_gpio_dataout_reg;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001223 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001224 bank->set_dataout = omap_set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001225
1226 spin_lock_init(&bank->lock);
1227
1228 /* Static mapping, never released */
1229 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001230 bank->base = devm_ioremap_resource(dev, res);
1231 if (IS_ERR(bank->base)) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001232 irq_domain_remove(bank->chip.irqdomain);
Jingoo Han717f70e2014-02-12 11:51:38 +09001233 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001234 }
1235
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301236 platform_set_drvdata(pdev, bank);
1237
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001238 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301239 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001240 pm_runtime_get_sync(bank->dev);
1241
Charulatha Vd0d665a2011-08-31 00:02:21 +05301242 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001243 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301244
Charulatha V03e128c2011-05-05 19:58:01 +05301245 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001246
Nishanth Menon46824e222014-09-05 14:52:55 -05001247 ret = omap_gpio_chip_init(bank, irqc);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001248 if (ret)
1249 return ret;
1250
Tony Lindgren9a748052010-12-07 16:26:56 -08001251 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001252
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301253 pm_runtime_put(bank->dev);
1254
Charulatha V03e128c2011-05-05 19:58:01 +05301255 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001256
Jon Hunter879fe322013-04-04 15:16:12 -05001257 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001258}
1259
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301260#ifdef CONFIG_ARCH_OMAP2PLUS
1261
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001262#if defined(CONFIG_PM)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301263static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001264
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301265static int omap_gpio_runtime_suspend(struct device *dev)
1266{
1267 struct platform_device *pdev = to_platform_device(dev);
1268 struct gpio_bank *bank = platform_get_drvdata(pdev);
1269 u32 l1 = 0, l2 = 0;
1270 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001271 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301272
1273 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001274
1275 /*
1276 * Only edges can generate a wakeup event to the PRCM.
1277 *
1278 * Therefore, ensure any wake-up capable GPIOs have
1279 * edge-detection enabled before going idle to ensure a wakeup
1280 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1281 * NDA TRM 25.5.3.1)
1282 *
1283 * The normal values will be restored upon ->runtime_resume()
1284 * by writing back the values saved in bank->context.
1285 */
1286 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1287 if (wake_low)
Victor Kamensky661553b2013-11-16 02:01:04 +02001288 writel_relaxed(wake_low | bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001289 bank->base + bank->regs->fallingdetect);
1290 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1291 if (wake_hi)
Victor Kamensky661553b2013-11-16 02:01:04 +02001292 writel_relaxed(wake_hi | bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001293 bank->base + bank->regs->risingdetect);
1294
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001295 if (!bank->enabled_non_wakeup_gpios)
1296 goto update_gpio_context_count;
1297
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301298 if (bank->power_mode != OFF_MODE) {
1299 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301300 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301301 }
1302 /*
1303 * If going to OFF, remove triggering for all
1304 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1305 * generated. See OMAP2420 Errata item 1.101.
1306 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001307 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301308 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301309 l1 = bank->context.fallingdetect;
1310 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301311
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301312 l1 &= ~bank->enabled_non_wakeup_gpios;
1313 l2 &= ~bank->enabled_non_wakeup_gpios;
1314
Victor Kamensky661553b2013-11-16 02:01:04 +02001315 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1316 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301317
1318 bank->workaround_enabled = true;
1319
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301320update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301321 if (bank->get_context_loss_count)
1322 bank->context_loss_count =
1323 bank->get_context_loss_count(bank->dev);
1324
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001325 omap_gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301326 spin_unlock_irqrestore(&bank->lock, flags);
1327
1328 return 0;
1329}
1330
Jon Hunter352a2d52013-04-15 13:06:54 -05001331static void omap_gpio_init_context(struct gpio_bank *p);
1332
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301333static int omap_gpio_runtime_resume(struct device *dev)
1334{
1335 struct platform_device *pdev = to_platform_device(dev);
1336 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301337 u32 l = 0, gen, gen0, gen1;
1338 unsigned long flags;
Jon Huntera2797be2013-04-04 15:16:15 -05001339 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301340
1341 spin_lock_irqsave(&bank->lock, flags);
Jon Hunter352a2d52013-04-15 13:06:54 -05001342
1343 /*
1344 * On the first resume during the probe, the context has not
1345 * been initialised and so initialise it now. Also initialise
1346 * the context loss count.
1347 */
1348 if (bank->loses_context && !bank->context_valid) {
1349 omap_gpio_init_context(bank);
1350
1351 if (bank->get_context_loss_count)
1352 bank->context_loss_count =
1353 bank->get_context_loss_count(bank->dev);
1354 }
1355
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001356 omap_gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001357
1358 /*
1359 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1360 * GPIOs were set to edge trigger also in order to be able to
1361 * generate a PRCM wakeup. Here we restore the
1362 * pre-runtime_suspend() values for edge triggering.
1363 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001364 writel_relaxed(bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001365 bank->base + bank->regs->fallingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001366 writel_relaxed(bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001367 bank->base + bank->regs->risingdetect);
1368
Jon Huntera2797be2013-04-04 15:16:15 -05001369 if (bank->loses_context) {
1370 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301371 omap_gpio_restore_context(bank);
1372 } else {
Jon Huntera2797be2013-04-04 15:16:15 -05001373 c = bank->get_context_loss_count(bank->dev);
1374 if (c != bank->context_loss_count) {
1375 omap_gpio_restore_context(bank);
1376 } else {
1377 spin_unlock_irqrestore(&bank->lock, flags);
1378 return 0;
1379 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301380 }
1381 }
1382
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301383 if (!bank->workaround_enabled) {
1384 spin_unlock_irqrestore(&bank->lock, flags);
1385 return 0;
1386 }
1387
Victor Kamensky661553b2013-11-16 02:01:04 +02001388 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301389
1390 /*
1391 * Check if any of the non-wakeup interrupt GPIOs have changed
1392 * state. If so, generate an IRQ by software. This is
1393 * horribly racy, but it's the best we can do to work around
1394 * this silicon bug.
1395 */
1396 l ^= bank->saved_datain;
1397 l &= bank->enabled_non_wakeup_gpios;
1398
1399 /*
1400 * No need to generate IRQs for the rising edge for gpio IRQs
1401 * configured with falling edge only; and vice versa.
1402 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301403 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301404 gen0 &= bank->saved_datain;
1405
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301406 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301407 gen1 &= ~(bank->saved_datain);
1408
1409 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301410 gen = l & (~(bank->context.fallingdetect) &
1411 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301412 /* Consider all GPIO IRQs needed to be updated */
1413 gen |= gen0 | gen1;
1414
1415 if (gen) {
1416 u32 old0, old1;
1417
Victor Kamensky661553b2013-11-16 02:01:04 +02001418 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1419 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301420
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301421 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001422 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301423 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001424 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301425 bank->regs->leveldetect1);
1426 }
1427
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301428 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001429 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301430 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001431 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301432 bank->regs->leveldetect1);
1433 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001434 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1435 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301436 }
1437
1438 bank->workaround_enabled = false;
1439 spin_unlock_irqrestore(&bank->lock, flags);
1440
1441 return 0;
1442}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001443#endif /* CONFIG_PM */
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301444
1445void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001446{
Charulatha V03e128c2011-05-05 19:58:01 +05301447 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001448
Charulatha V03e128c2011-05-05 19:58:01 +05301449 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001450 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301451 continue;
1452
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301453 bank->power_mode = pwr_mode;
1454
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301455 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001456 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001457}
1458
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001459void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001460{
Charulatha V03e128c2011-05-05 19:58:01 +05301461 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001462
Charulatha V03e128c2011-05-05 19:58:01 +05301463 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001464 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301465 continue;
1466
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301467 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001468 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001469}
1470
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001471#if defined(CONFIG_PM)
Jon Hunter352a2d52013-04-15 13:06:54 -05001472static void omap_gpio_init_context(struct gpio_bank *p)
1473{
1474 struct omap_gpio_reg_offs *regs = p->regs;
1475 void __iomem *base = p->base;
1476
Victor Kamensky661553b2013-11-16 02:01:04 +02001477 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1478 p->context.oe = readl_relaxed(base + regs->direction);
1479 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1480 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1481 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1482 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1483 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1484 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1485 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001486
1487 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001488 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001489 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001490 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001491
1492 p->context_valid = true;
1493}
1494
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301495static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301496{
Victor Kamensky661553b2013-11-16 02:01:04 +02001497 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301498 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001499 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1500 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301501 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001502 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301503 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001504 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301505 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001506 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301507 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301508 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001509 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301510 bank->base + bank->regs->set_dataout);
1511 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001512 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301513 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001514 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301515
Nishanth Menonae547352011-09-09 19:08:58 +05301516 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001517 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301518 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001519 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301520 bank->base + bank->regs->debounce_en);
1521 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301522
Victor Kamensky661553b2013-11-16 02:01:04 +02001523 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301524 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001525 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301526 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301527}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001528#endif /* CONFIG_PM */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301529#else
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301530#define omap_gpio_runtime_suspend NULL
1531#define omap_gpio_runtime_resume NULL
Arnd Bergmannea4a21a2013-05-31 17:59:46 +02001532static inline void omap_gpio_init_context(struct gpio_bank *p) {}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301533#endif
1534
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301535static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301536 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1537 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301538};
1539
Benoit Cousson384ebe12011-08-16 11:53:02 +02001540#if defined(CONFIG_OF)
1541static struct omap_gpio_reg_offs omap2_gpio_regs = {
1542 .revision = OMAP24XX_GPIO_REVISION,
1543 .direction = OMAP24XX_GPIO_OE,
1544 .datain = OMAP24XX_GPIO_DATAIN,
1545 .dataout = OMAP24XX_GPIO_DATAOUT,
1546 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1547 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1548 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1549 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1550 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1551 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1552 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1553 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1554 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1555 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1556 .ctrl = OMAP24XX_GPIO_CTRL,
1557 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1558 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1559 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1560 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1561 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1562};
1563
1564static struct omap_gpio_reg_offs omap4_gpio_regs = {
1565 .revision = OMAP4_GPIO_REVISION,
1566 .direction = OMAP4_GPIO_OE,
1567 .datain = OMAP4_GPIO_DATAIN,
1568 .dataout = OMAP4_GPIO_DATAOUT,
1569 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1570 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1571 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1572 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1573 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1574 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1575 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1576 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1577 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1578 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1579 .ctrl = OMAP4_GPIO_CTRL,
1580 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1581 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1582 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1583 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1584 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1585};
1586
Chen Gange9a65bb2013-02-06 18:44:32 +08001587static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001588 .regs = &omap2_gpio_regs,
1589 .bank_width = 32,
1590 .dbck_flag = false,
1591};
1592
Chen Gange9a65bb2013-02-06 18:44:32 +08001593static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001594 .regs = &omap2_gpio_regs,
1595 .bank_width = 32,
1596 .dbck_flag = true,
1597};
1598
Chen Gange9a65bb2013-02-06 18:44:32 +08001599static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001600 .regs = &omap4_gpio_regs,
1601 .bank_width = 32,
1602 .dbck_flag = true,
1603};
1604
1605static const struct of_device_id omap_gpio_match[] = {
1606 {
1607 .compatible = "ti,omap4-gpio",
1608 .data = &omap4_pdata,
1609 },
1610 {
1611 .compatible = "ti,omap3-gpio",
1612 .data = &omap3_pdata,
1613 },
1614 {
1615 .compatible = "ti,omap2-gpio",
1616 .data = &omap2_pdata,
1617 },
1618 { },
1619};
1620MODULE_DEVICE_TABLE(of, omap_gpio_match);
1621#endif
1622
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001623static struct platform_driver omap_gpio_driver = {
1624 .probe = omap_gpio_probe,
1625 .driver = {
1626 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301627 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001628 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001629 },
1630};
1631
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001632/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001633 * gpio driver register needs to be done before
1634 * machine_init functions access gpio APIs.
1635 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001636 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001637static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001638{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001639 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001640}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001641postcore_initcall(omap_gpio_drv_reg);