blob: f476ae2eb0b3c8610e54377cf7e3010079e916bf [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
Tony Lindgren3d009c82015-01-16 14:50:50 -080091static void omap_gpio_unmask_irq(struct irq_data *d);
92
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020093static int omap_irq_to_gpio(struct gpio_bank *bank, unsigned int gpio_irq)
Benoit Cousson25db7112012-02-23 21:50:10 +010094{
Jon Hunterede4d7a2013-03-01 11:22:47 -060095 return bank->chip.base + gpio_irq;
96}
97
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020098static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -060099{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200100 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
101 return container_of(chip, struct gpio_bank, chip);
Benoit Cousson25db7112012-02-23 21:50:10 +0100102}
103
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200104static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
105 int is_input)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100106{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100107 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100108 u32 l;
109
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700110 reg += bank->regs->direction;
Victor Kamensky661553b2013-11-16 02:01:04 +0200111 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100112 if (is_input)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200113 l |= BIT(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100114 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200115 l &= ~(BIT(gpio));
Victor Kamensky661553b2013-11-16 02:01:04 +0200116 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530117 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100118}
119
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700120
121/* set data out value using dedicate set/clear register */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200122static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, int gpio,
123 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100124{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100125 void __iomem *reg = bank->base;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700126 u32 l = GPIO_BIT(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100127
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530128 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700129 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530130 bank->context.dataout |= l;
131 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700132 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530133 bank->context.dataout &= ~l;
134 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700135
Victor Kamensky661553b2013-11-16 02:01:04 +0200136 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700137}
138
139/* set data out value using mask register */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200140static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, int gpio,
141 int enable)
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700142{
143 void __iomem *reg = bank->base + bank->regs->dataout;
144 u32 gpio_bit = GPIO_BIT(bank, gpio);
145 u32 l;
146
Victor Kamensky661553b2013-11-16 02:01:04 +0200147 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700148 if (enable)
149 l |= gpio_bit;
150 else
151 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200152 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530153 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100154}
155
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200156static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100157{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700158 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100159
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200160 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100161}
162
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200163static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300164{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700165 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300166
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200167 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300168}
169
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200170static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700171{
Victor Kamensky661553b2013-11-16 02:01:04 +0200172 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700173
Benoit Cousson862ff642012-02-01 15:58:56 +0100174 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700175 l |= mask;
176 else
177 l &= ~mask;
178
Victor Kamensky661553b2013-11-16 02:01:04 +0200179 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700180}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100181
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200182static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530183{
184 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Rajendra Nayak345477f2014-04-23 11:41:03 +0530185 clk_prepare_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530186 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300187
Victor Kamensky661553b2013-11-16 02:01:04 +0200188 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300189 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530190 }
191}
192
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200193static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530194{
195 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300196 /*
197 * Disable debounce before cutting it's clock. If debounce is
198 * enabled but the clock is not, GPIO module seems to be unable
199 * to detect events and generate interrupts at least on OMAP3.
200 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200201 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300202
Rajendra Nayak345477f2014-04-23 11:41:03 +0530203 clk_disable_unprepare(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530204 bank->dbck_enabled = false;
205 }
206}
207
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700208/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200209 * omap2_set_gpio_debounce - low level gpio debounce time
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700210 * @bank: the gpio bank we're acting upon
211 * @gpio: the gpio number on this @gpio
212 * @debounce: debounce time to use
213 *
214 * OMAP's debounce time is in 31us steps so we need
215 * to convert and round up to the closest unit.
216 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200217static void omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
218 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700219{
Kevin Hilman9942da02011-04-22 12:02:05 -0700220 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700221 u32 val;
222 u32 l;
223
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800224 if (!bank->dbck_flag)
225 return;
226
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700227 if (debounce < 32)
228 debounce = 0x01;
229 else if (debounce > 7936)
230 debounce = 0xff;
231 else
232 debounce = (debounce / 0x1f) - 1;
233
Kevin Hilman129fd222011-04-22 07:59:07 -0700234 l = GPIO_BIT(bank, gpio);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700235
Rajendra Nayak345477f2014-04-23 11:41:03 +0530236 clk_prepare_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700237 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200238 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700239
Kevin Hilman9942da02011-04-22 12:02:05 -0700240 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200241 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700242
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530243 if (debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700244 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530245 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700246 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300247 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700248
Victor Kamensky661553b2013-11-16 02:01:04 +0200249 writel_relaxed(val, reg);
Rajendra Nayak345477f2014-04-23 11:41:03 +0530250 clk_disable_unprepare(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530251 /*
252 * Enable debounce clock per module.
253 * This call is mandatory because in omap_gpio_request() when
254 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
255 * runtime callbck fails to turn on dbck because dbck_enable_mask
256 * used within _gpio_dbck_enable() is still not initialized at
257 * that point. Therefore we have to enable dbck here.
258 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200259 omap_gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530260 if (bank->dbck_enable_mask) {
261 bank->context.debounce = debounce;
262 bank->context.debounce_en = val;
263 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700264}
265
Jon Hunterc9c55d92012-10-26 14:26:04 -0500266/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200267 * omap_clear_gpio_debounce - clear debounce settings for a gpio
Jon Hunterc9c55d92012-10-26 14:26:04 -0500268 * @bank: the gpio bank we're acting upon
269 * @gpio: the gpio number on this @gpio
270 *
271 * If a gpio is using debounce, then clear the debounce enable bit and if
272 * this is the only gpio in this bank using debounce, then clear the debounce
273 * time too. The debounce clock will also be disabled when calling this function
274 * if this is the only gpio in the bank using debounce.
275 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200276static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned gpio)
Jon Hunterc9c55d92012-10-26 14:26:04 -0500277{
278 u32 gpio_bit = GPIO_BIT(bank, gpio);
279
280 if (!bank->dbck_flag)
281 return;
282
283 if (!(bank->dbck_enable_mask & gpio_bit))
284 return;
285
286 bank->dbck_enable_mask &= ~gpio_bit;
287 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200288 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500289 bank->base + bank->regs->debounce_en);
290
291 if (!bank->dbck_enable_mask) {
292 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200293 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500294 bank->regs->debounce);
Rajendra Nayak345477f2014-04-23 11:41:03 +0530295 clk_disable_unprepare(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500296 bank->dbck_enabled = false;
297 }
298}
299
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200300static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530301 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100302{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800303 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200304 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100305
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200306 omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
307 trigger & IRQ_TYPE_LEVEL_LOW);
308 omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
309 trigger & IRQ_TYPE_LEVEL_HIGH);
310 omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
311 trigger & IRQ_TYPE_EDGE_RISING);
312 omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
313 trigger & IRQ_TYPE_EDGE_FALLING);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530314
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530315 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200316 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530317 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200318 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530319 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200320 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530321 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200322 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530323
324 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200325 omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530326 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200327 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530328 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530329
Ambresh K55b220c2011-06-15 13:40:45 -0700330 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530331 if (!bank->regs->irqctrl) {
332 /* On omap24xx proceed only when valid GPIO bit is set */
333 if (bank->non_wakeup_gpios) {
334 if (!(bank->non_wakeup_gpios & gpio_bit))
335 goto exit;
336 }
337
Chunqiu Wang699117a62009-06-24 17:13:39 +0000338 /*
339 * Log the edge gpio and manually trigger the IRQ
340 * after resume if the input level changes
341 * to avoid irq lost during PER RET/OFF mode
342 * Applies for omap2 non-wakeup gpio and all omap3 gpios
343 */
344 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800345 bank->enabled_non_wakeup_gpios |= gpio_bit;
346 else
347 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
348 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700349
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530350exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530351 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200352 readl_relaxed(bank->base + bank->regs->leveldetect0) |
353 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100354}
355
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800356#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800357/*
358 * This only applies to chips that can't do both rising and falling edge
359 * detection at once. For all other chips, this function is a noop.
360 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200361static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800362{
363 void __iomem *reg = bank->base;
364 u32 l = 0;
365
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530366 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800367 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530368
369 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800370
Victor Kamensky661553b2013-11-16 02:01:04 +0200371 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800372 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200373 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800374 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200375 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800376
Victor Kamensky661553b2013-11-16 02:01:04 +0200377 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800378}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530379#else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200380static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800381#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800382
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200383static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
384 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100385{
386 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530387 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100388 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100389
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530390 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200391 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530392 } else if (bank->regs->irqctrl) {
393 reg += bank->regs->irqctrl;
394
Victor Kamensky661553b2013-11-16 02:01:04 +0200395 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000396 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200397 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100398 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200399 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100400 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200401 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100402 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530403 return -EINVAL;
404
Victor Kamensky661553b2013-11-16 02:01:04 +0200405 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530406 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100407 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530408 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100409 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530410 reg += bank->regs->edgectrl1;
411
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100412 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200413 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100414 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100415 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100416 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100417 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200418 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530419
420 /* Enable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200421 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530422 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200423 readl_relaxed(bank->base + bank->regs->wkup_en);
424 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100425 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100426 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100427}
428
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200429static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200430{
431 if (bank->regs->pinctrl) {
432 void __iomem *reg = bank->base + bank->regs->pinctrl;
433
434 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200435 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200436 }
437
438 if (bank->regs->ctrl && !BANK_USED(bank)) {
439 void __iomem *reg = bank->base + bank->regs->ctrl;
440 u32 ctrl;
441
Victor Kamensky661553b2013-11-16 02:01:04 +0200442 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200443 /* Module is enabled, clocks are not gated */
444 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200445 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200446 bank->context.ctrl = ctrl;
447 }
448}
449
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200450static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200451{
452 void __iomem *base = bank->base;
453
454 if (bank->regs->wkup_en &&
455 !LINE_USED(bank->mod_usage, offset) &&
456 !LINE_USED(bank->irq_usage, offset)) {
457 /* Disable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200458 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200459 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200460 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200461 }
462
463 if (bank->regs->ctrl && !BANK_USED(bank)) {
464 void __iomem *reg = bank->base + bank->regs->ctrl;
465 u32 ctrl;
466
Victor Kamensky661553b2013-11-16 02:01:04 +0200467 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200468 /* Module is disabled, clocks are gated */
469 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200470 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200471 bank->context.ctrl = ctrl;
472 }
473}
474
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200475static int omap_gpio_is_input(struct gpio_bank *bank, int mask)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200476{
477 void __iomem *reg = bank->base + bank->regs->direction;
478
Victor Kamensky661553b2013-11-16 02:01:04 +0200479 return readl_relaxed(reg) & mask;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200480}
481
Tony Lindgren3d009c82015-01-16 14:50:50 -0800482static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned gpio,
483 unsigned offset)
484{
485 if (!LINE_USED(bank->mod_usage, offset)) {
486 omap_enable_gpio_module(bank, offset);
487 omap_set_gpio_direction(bank, offset, 1);
488 }
489 bank->irq_usage |= BIT(GPIO_INDEX(bank, gpio));
490}
491
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200492static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100493{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200494 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren4b254082012-08-30 15:37:24 -0700495 unsigned gpio = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100496 int retval;
David Brownella6472532008-03-03 04:33:30 -0800497 unsigned long flags;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200498 unsigned offset;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100499
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200500 if (!BANK_USED(bank))
501 pm_runtime_get_sync(bank->dev);
Jon Hunter8d4c2772013-03-01 11:22:48 -0600502
Tony Lindgren4b254082012-08-30 15:37:24 -0700503#ifdef CONFIG_ARCH_OMAP1
504 if (d->irq > IH_MPUIO_BASE)
Lennert Buytenheke9191022010-11-29 11:17:17 +0100505 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren4b254082012-08-30 15:37:24 -0700506#endif
507
508 if (!gpio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200509 gpio = omap_irq_to_gpio(bank, d->hwirq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510
David Brownelle5c56ed2006-12-06 17:13:59 -0800511 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100512 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800513
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530514 if (!bank->regs->leveldetect0 &&
515 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100516 return -EINVAL;
517
David Brownella6472532008-03-03 04:33:30 -0800518 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200519 offset = GPIO_INDEX(bank, gpio);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200520 retval = omap_set_gpio_triggering(bank, offset, type);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800521 omap_gpio_init_irq(bank, gpio, offset);
522 if (!omap_gpio_is_input(bank, BIT(offset))) {
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200523 spin_unlock_irqrestore(&bank->lock, flags);
524 return -EINVAL;
525 }
David Brownella6472532008-03-03 04:33:30 -0800526 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800527
528 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100529 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800530 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100531 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800532
Tony Lindgren92105bb2005-09-07 17:20:26 +0100533 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100534}
535
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200536static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100537{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100538 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100539
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700540 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200541 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300542
543 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700544 if (bank->regs->irqstatus2) {
545 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200546 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700547 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700548
549 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200550 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100551}
552
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200553static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100554{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200555 omap_clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100556}
557
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200558static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
Imre Deakea6dedd2006-06-26 16:16:00 -0700559{
560 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700561 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200562 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700563
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700564 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200565 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700566 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700567 l = ~l;
568 l &= mask;
569 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700570}
571
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200572static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100574 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100575 u32 l;
576
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700577 if (bank->regs->set_irqenable) {
578 reg += bank->regs->set_irqenable;
579 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530580 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700581 } else {
582 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200583 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700584 if (bank->regs->irqenable_inv)
585 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100586 else
587 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530588 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100589 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700590
Victor Kamensky661553b2013-11-16 02:01:04 +0200591 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700592}
593
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200594static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700595{
596 void __iomem *reg = bank->base;
597 u32 l;
598
599 if (bank->regs->clr_irqenable) {
600 reg += bank->regs->clr_irqenable;
601 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530602 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700603 } else {
604 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200605 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700606 if (bank->regs->irqenable_inv)
607 l |= gpio_mask;
608 else
609 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530610 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700611 }
612
Victor Kamensky661553b2013-11-16 02:01:04 +0200613 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100614}
615
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200616static inline void omap_set_gpio_irqenable(struct gpio_bank *bank, int gpio,
617 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100618{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530619 if (enable)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200620 omap_enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530621 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200622 omap_disable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100623}
624
Tony Lindgren92105bb2005-09-07 17:20:26 +0100625/*
626 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
627 * 1510 does not seem to have a wake-up register. If JTAG is connected
628 * to the target, system will wake up always on GPIO events. While
629 * system is running all registered GPIO interrupts need to have wake-up
630 * enabled. When system is suspended, only selected GPIO interrupts need
631 * to have wake-up enabled.
632 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200633static int omap_set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100634{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700635 u32 gpio_bit = GPIO_BIT(bank, gpio);
636 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800637
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700638 if (bank->non_wakeup_gpios & gpio_bit) {
Benoit Cousson862ff642012-02-01 15:58:56 +0100639 dev_err(bank->dev,
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700640 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100641 return -EINVAL;
642 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700643
644 spin_lock_irqsave(&bank->lock, flags);
645 if (enable)
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530646 bank->context.wake_en |= gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700647 else
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530648 bank->context.wake_en &= ~gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700649
Victor Kamensky661553b2013-11-16 02:01:04 +0200650 writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700651 spin_unlock_irqrestore(&bank->lock, flags);
652
653 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100654}
655
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200656static void omap_reset_gpio(struct gpio_bank *bank, int gpio)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300657{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200658 omap_set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
659 omap_set_gpio_irqenable(bank, gpio, 0);
660 omap_clear_gpio_irqstatus(bank, gpio);
661 omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
662 omap_clear_gpio_debounce(bank, gpio);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300663}
664
Tony Lindgren92105bb2005-09-07 17:20:26 +0100665/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200666static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100667{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200668 struct gpio_bank *bank = omap_irq_data_get_bank(d);
669 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100670
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200671 return omap_set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100672}
673
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800674static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800676 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800677 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100678
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530679 /*
680 * If this is the first gpio_request for the bank,
681 * enable the bank module.
682 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200683 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530684 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100685
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530686 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300687 /* Set trigger to none. You need to enable the desired trigger with
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200688 * request_irq() or set_irq_type(). Only do this if the IRQ line has
689 * not already been requested.
Tony Lindgren4196dd62006-09-25 12:41:38 +0300690 */
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200691 if (!LINE_USED(bank->irq_usage, offset)) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200692 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
693 omap_enable_gpio_module(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100694 }
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200695 bank->mod_usage |= BIT(offset);
David Brownella6472532008-03-03 04:33:30 -0800696 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100697
698 return 0;
699}
700
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800701static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800703 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800704 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100705
David Brownella6472532008-03-03 04:33:30 -0800706 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200707 bank->mod_usage &= ~(BIT(offset));
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200708 omap_disable_gpio_module(bank, offset);
709 omap_reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800710 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530711
712 /*
713 * If this is the last gpio to be freed in the bank,
714 * disable the bank module.
715 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200716 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530717 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718}
719
720/*
721 * We need to unmask the GPIO bank interrupt as soon as possible to
722 * avoid missing GPIO interrupts for other lines in the bank.
723 * Then we need to mask-read-clear-unmask the triggered GPIO lines
724 * in the bank to avoid missing nested interrupts for a GPIO line.
725 * If we wait to unmask individual GPIO lines in the bank after the
726 * line's interrupt handler has been run, we may miss some nested
727 * interrupts.
728 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200729static void omap_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100731 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732 u32 isr;
Jon Hunter3513cde2013-04-04 15:16:14 -0500733 unsigned int bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100734 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700735 int unmasked = 0;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200736 struct irq_chip *irqchip = irq_desc_get_chip(desc);
737 struct gpio_chip *chip = irq_get_handler_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100738
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200739 chained_irq_enter(irqchip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200741 bank = container_of(chip, struct gpio_bank, chip);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700742 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530743 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800744
745 if (WARN_ON(!isr_reg))
746 goto exit;
747
Laurent Navete83507b2013-03-20 13:15:57 +0100748 while (1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100749 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700750 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100751
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200752 enabled = omap_get_gpio_irqbank_mask(bank);
Victor Kamensky661553b2013-11-16 02:01:04 +0200753 isr_saved = isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100754
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530755 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800756 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100757
758 /* clear edge sensitive interrupts before handler(s) are
759 called so that we don't miss any interrupt occurred while
760 executing them */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200761 omap_disable_gpio_irqbank(bank, isr_saved & ~level_mask);
762 omap_clear_gpio_irqbank(bank, isr_saved & ~level_mask);
763 omap_enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100764
765 /* if there is only edge sensitive GPIO pin interrupts
766 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700767 if (!level_mask && !unmasked) {
768 unmasked = 1;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200769 chained_irq_exit(irqchip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700770 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100771
Tony Lindgren92105bb2005-09-07 17:20:26 +0100772 if (!isr)
773 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100774
Jon Hunter3513cde2013-04-04 15:16:14 -0500775 while (isr) {
776 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200777 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100778
Cory Maccarrone4318f362010-01-08 10:29:04 -0800779 /*
780 * Some chips can't respond to both rising and falling
781 * at the same time. If this irq was requested with
782 * both flags, we need to flip the ICR data for the IRQ
783 * to respond to the IRQ for the opposite direction.
784 * This will be indicated in the bank toggle_mask.
785 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200786 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200787 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800788
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200789 generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
790 bit));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100791 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000792 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700793 /* if bank has any level sensitive GPIO pin interrupt
794 configured, we must unmask the bank interrupt only after
795 handler(s) are executed in order to avoid spurious bank
796 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800797exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700798 if (!unmasked)
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200799 chained_irq_exit(irqchip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530800 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100801}
802
Tony Lindgren3d009c82015-01-16 14:50:50 -0800803static unsigned int omap_gpio_irq_startup(struct irq_data *d)
804{
805 struct gpio_bank *bank = omap_irq_data_get_bank(d);
806 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
807 unsigned long flags;
808 unsigned offset = GPIO_INDEX(bank, gpio);
809
810 if (!BANK_USED(bank))
811 pm_runtime_get_sync(bank->dev);
812
813 spin_lock_irqsave(&bank->lock, flags);
814 omap_gpio_init_irq(bank, gpio, offset);
815 spin_unlock_irqrestore(&bank->lock, flags);
816 omap_gpio_unmask_irq(d);
817
818 return 0;
819}
820
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200821static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300822{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200823 struct gpio_bank *bank = omap_irq_data_get_bank(d);
824 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700825 unsigned long flags;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200826 unsigned offset = GPIO_INDEX(bank, gpio);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300827
Colin Cross85ec7b92011-06-06 13:38:18 -0700828 spin_lock_irqsave(&bank->lock, flags);
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900829 gpiochip_unlock_as_irq(&bank->chip, offset);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200830 bank->irq_usage &= ~(BIT(offset));
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200831 omap_disable_gpio_module(bank, offset);
832 omap_reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700833 spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200834
835 /*
836 * If this is the last IRQ to be freed in the bank,
837 * disable the bank module.
838 */
839 if (!BANK_USED(bank))
840 pm_runtime_put(bank->dev);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300841}
842
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200843static void omap_gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100844{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200845 struct gpio_bank *bank = omap_irq_data_get_bank(d);
846 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100847
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200848 omap_clear_gpio_irqstatus(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100849}
850
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200851static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100852{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200853 struct gpio_bank *bank = omap_irq_data_get_bank(d);
854 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700855 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100856
Colin Cross85ec7b92011-06-06 13:38:18 -0700857 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200858 omap_set_gpio_irqenable(bank, gpio, 0);
859 omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700860 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100861}
862
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200863static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100864{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200865 struct gpio_bank *bank = omap_irq_data_get_bank(d);
866 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Kevin Hilman129fd222011-04-22 07:59:07 -0700867 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100868 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700869 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700870
Colin Cross85ec7b92011-06-06 13:38:18 -0700871 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700872 if (trigger)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200873 omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800874
875 /* For level-triggered GPIOs, the clearing must be done after
876 * the HW source is cleared, thus after the handler has run */
877 if (bank->level_mask & irq_mask) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200878 omap_set_gpio_irqenable(bank, gpio, 0);
879 omap_clear_gpio_irqstatus(bank, gpio);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800880 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100881
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200882 omap_set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700883 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100884}
885
David Brownelle5c56ed2006-12-06 17:13:59 -0800886/*---------------------------------------------------------------------*/
887
Magnus Damm79ee0312009-07-08 13:22:04 +0200888static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800889{
Magnus Damm79ee0312009-07-08 13:22:04 +0200890 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800891 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800892 void __iomem *mask_reg = bank->base +
893 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800894 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800895
David Brownella6472532008-03-03 04:33:30 -0800896 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200897 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800898 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800899
900 return 0;
901}
902
Magnus Damm79ee0312009-07-08 13:22:04 +0200903static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800904{
Magnus Damm79ee0312009-07-08 13:22:04 +0200905 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800906 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800907 void __iomem *mask_reg = bank->base +
908 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800909 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800910
David Brownella6472532008-03-03 04:33:30 -0800911 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200912 writel_relaxed(bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800913 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800914
915 return 0;
916}
917
Alexey Dobriyan47145212009-12-14 18:00:08 -0800918static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200919 .suspend_noirq = omap_mpuio_suspend_noirq,
920 .resume_noirq = omap_mpuio_resume_noirq,
921};
922
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200923/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800924static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800925 .driver = {
926 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200927 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800928 },
929};
930
931static struct platform_device omap_mpuio_device = {
932 .name = "mpuio",
933 .id = -1,
934 .dev = {
935 .driver = &omap_mpuio_driver.driver,
936 }
937 /* could list the /proc/iomem resources */
938};
939
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200940static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800941{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800942 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700943
David Brownell11a78b72006-12-06 17:14:11 -0800944 if (platform_driver_register(&omap_mpuio_driver) == 0)
945 (void) platform_device_register(&omap_mpuio_device);
946}
947
David Brownelle5c56ed2006-12-06 17:13:59 -0800948/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100949
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200950static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200951{
952 struct gpio_bank *bank;
953 unsigned long flags;
954 void __iomem *reg;
955 int dir;
956
957 bank = container_of(chip, struct gpio_bank, chip);
958 reg = bank->base + bank->regs->direction;
959 spin_lock_irqsave(&bank->lock, flags);
960 dir = !!(readl_relaxed(reg) & BIT(offset));
961 spin_unlock_irqrestore(&bank->lock, flags);
962 return dir;
963}
964
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200965static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
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);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200972 omap_set_gpio_direction(bank, offset, 1);
David Brownell52e31342008-03-03 12:43:23 -0800973 spin_unlock_irqrestore(&bank->lock, flags);
974 return 0;
975}
976
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200977static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800978{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300979 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300980 u32 mask;
981
Charulatha Va8be8da2011-04-22 16:38:16 +0530982 bank = container_of(chip, struct gpio_bank, chip);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200983 mask = (BIT(offset));
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300984
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200985 if (omap_gpio_is_input(bank, mask))
986 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300987 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200988 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -0800989}
990
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200991static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800992{
993 struct gpio_bank *bank;
994 unsigned long flags;
995
996 bank = container_of(chip, struct gpio_bank, chip);
997 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700998 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200999 omap_set_gpio_direction(bank, offset, 0);
David Brownell52e31342008-03-03 12:43:23 -08001000 spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +02001001 return 0;
David Brownell52e31342008-03-03 12:43:23 -08001002}
1003
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001004static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
1005 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001006{
1007 struct gpio_bank *bank;
1008 unsigned long flags;
1009
1010 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001011
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001012 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001013 omap2_set_gpio_debounce(bank, offset, debounce);
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001014 spin_unlock_irqrestore(&bank->lock, flags);
1015
1016 return 0;
1017}
1018
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001019static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001020{
1021 struct gpio_bank *bank;
1022 unsigned long flags;
1023
1024 bank = container_of(chip, struct gpio_bank, chip);
1025 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001026 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001027 spin_unlock_irqrestore(&bank->lock, flags);
1028}
1029
1030/*---------------------------------------------------------------------*/
1031
Tony Lindgren9a748052010-12-07 16:26:56 -08001032static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001033{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001034 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001035 u32 rev;
1036
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001037 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001038 return;
1039
Victor Kamensky661553b2013-11-16 02:01:04 +02001040 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001041 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001042 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001043
1044 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001045}
1046
Charulatha V03e128c2011-05-05 19:58:01 +05301047static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001048{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301049 void __iomem *base = bank->base;
1050 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001051
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301052 if (bank->width == 16)
1053 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001054
Charulatha Vd0d665a2011-08-31 00:02:21 +05301055 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001056 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301057 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001058 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301059
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001060 omap_gpio_rmw(base, bank->regs->irqenable, l,
1061 bank->regs->irqenable_inv);
1062 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1063 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301064 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001065 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301066
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301067 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001068 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301069 /* Initialize interface clk ungated, module enabled */
1070 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001071 writel_relaxed(0, base + bank->regs->ctrl);
Tarun Kanti DebBarma34672012012-07-11 14:43:14 +05301072
1073 bank->dbck = clk_get(bank->dev, "dbclk");
1074 if (IS_ERR(bank->dbck))
1075 dev_err(bank->dev, "Could not get gpio dbck\n");
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001076}
1077
Bill Pemberton38363092012-11-19 13:22:34 -05001078static void
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001079omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1080 unsigned int num)
1081{
1082 struct irq_chip_generic *gc;
1083 struct irq_chip_type *ct;
1084
1085 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1086 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -07001087 if (!gc) {
1088 dev_err(bank->dev, "Memory alloc failed for gc\n");
1089 return;
1090 }
1091
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001092 ct = gc->chip_types;
1093
1094 /* NOTE: No ack required, reading IRQ status clears it. */
1095 ct->chip.irq_mask = irq_gc_mask_set_bit;
1096 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001097 ct->chip.irq_set_type = omap_gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301098
1099 if (bank->regs->wkup_en)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001100 ct->chip.irq_set_wake = omap_gpio_wake_enable;
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001101
1102 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1103 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1104 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1105}
1106
Nishanth Menon46824e22014-09-05 14:52:55 -05001107static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001108{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001109 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001110 static int gpio;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001111 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001112 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001113
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001114 /*
1115 * REVISIT eventually switch from OMAP-specific gpio structs
1116 * over to the generic ones
1117 */
1118 bank->chip.request = omap_gpio_request;
1119 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001120 bank->chip.get_direction = omap_gpio_get_direction;
1121 bank->chip.direction_input = omap_gpio_input;
1122 bank->chip.get = omap_gpio_get;
1123 bank->chip.direction_output = omap_gpio_output;
1124 bank->chip.set_debounce = omap_gpio_debounce;
1125 bank->chip.set = omap_gpio_set;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301126 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001127 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301128 if (bank->regs->wkup_en)
1129 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001130 bank->chip.base = OMAP_MPUIO(0);
1131 } else {
1132 bank->chip.label = "gpio";
1133 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001134 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001135 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001136 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001137
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001138 ret = gpiochip_add(&bank->chip);
1139 if (ret) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001140 dev_err(bank->dev, "Could not register gpio chip %d\n", ret);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001141 return ret;
1142 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001143
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001144#ifdef CONFIG_ARCH_OMAP1
1145 /*
1146 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1147 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1148 */
1149 irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
1150 if (irq_base < 0) {
1151 dev_err(bank->dev, "Couldn't allocate IRQ numbers\n");
1152 return -ENODEV;
1153 }
1154#endif
1155
Nishanth Menon46824e22014-09-05 14:52:55 -05001156 ret = gpiochip_irqchip_add(&bank->chip, irqc,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001157 irq_base, omap_gpio_irq_handler,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001158 IRQ_TYPE_NONE);
1159
1160 if (ret) {
1161 dev_err(bank->dev, "Couldn't add irqchip to gpiochip %d\n", ret);
Linus Walleijda26d5d2014-09-16 15:11:41 -07001162 gpiochip_remove(&bank->chip);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001163 return -ENODEV;
1164 }
1165
Nishanth Menon46824e22014-09-05 14:52:55 -05001166 gpiochip_set_chained_irqchip(&bank->chip, irqc,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001167 bank->irq, omap_gpio_irq_handler);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001168
Jon Hunterede4d7a2013-03-01 11:22:47 -06001169 for (j = 0; j < bank->width; j++) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001170 int irq = irq_find_mapping(bank->chip.irqdomain, j);
Charulatha Vd0d665a2011-08-31 00:02:21 +05301171 if (bank->is_mpuio) {
Jon Hunterede4d7a2013-03-01 11:22:47 -06001172 omap_mpuio_alloc_gc(bank, irq, bank->width);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001173 irq_set_chip_and_handler(irq, NULL, NULL);
1174 set_irq_flags(irq, 0);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001175 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001176 }
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001177
1178 return 0;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001179}
1180
Benoit Cousson384ebe12011-08-16 11:53:02 +02001181static const struct of_device_id omap_gpio_match[];
1182
Bill Pemberton38363092012-11-19 13:22:34 -05001183static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001184{
Benoit Cousson862ff642012-02-01 15:58:56 +01001185 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001186 struct device_node *node = dev->of_node;
1187 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001188 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001189 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001190 struct gpio_bank *bank;
Nishanth Menon46824e22014-09-05 14:52:55 -05001191 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001192 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001193
Benoit Cousson384ebe12011-08-16 11:53:02 +02001194 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1195
Jingoo Hane56aee12013-07-30 17:08:05 +09001196 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001197 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001198 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001199
Tobias Klauser086d5852012-10-05 11:37:38 +02001200 bank = devm_kzalloc(dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301201 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001202 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001203 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301204 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001205
Nishanth Menon46824e22014-09-05 14:52:55 -05001206 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1207 if (!irqc)
1208 return -ENOMEM;
1209
Tony Lindgren3d009c82015-01-16 14:50:50 -08001210 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e22014-09-05 14:52:55 -05001211 irqc->irq_shutdown = omap_gpio_irq_shutdown,
1212 irqc->irq_ack = omap_gpio_ack_irq,
1213 irqc->irq_mask = omap_gpio_mask_irq,
1214 irqc->irq_unmask = omap_gpio_unmask_irq,
1215 irqc->irq_set_type = omap_gpio_irq_type,
1216 irqc->irq_set_wake = omap_gpio_wake_enable,
1217 irqc->name = dev_name(&pdev->dev);
1218
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001219 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1220 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001221 dev_err(dev, "Invalid IRQ resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001222 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001223 }
1224
1225 bank->irq = res->start;
Benoit Cousson862ff642012-02-01 15:58:56 +01001226 bank->dev = dev;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001227 bank->chip.dev = dev;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001228 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001229 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001230 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301231 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301232 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001233 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001234#ifdef CONFIG_OF_GPIO
1235 bank->chip.of_node = of_node_get(node);
1236#endif
Jon Huntera2797be2013-04-04 15:16:15 -05001237 if (node) {
1238 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1239 bank->loses_context = true;
1240 } else {
1241 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001242
1243 if (bank->loses_context)
1244 bank->get_context_loss_count =
1245 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001246 }
1247
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001248 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001249 bank->set_dataout = omap_set_gpio_dataout_reg;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001250 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001251 bank->set_dataout = omap_set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001252
1253 spin_lock_init(&bank->lock);
1254
1255 /* Static mapping, never released */
1256 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001257 bank->base = devm_ioremap_resource(dev, res);
1258 if (IS_ERR(bank->base)) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001259 irq_domain_remove(bank->chip.irqdomain);
Jingoo Han717f70e2014-02-12 11:51:38 +09001260 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001261 }
1262
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301263 platform_set_drvdata(pdev, bank);
1264
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001265 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301266 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001267 pm_runtime_get_sync(bank->dev);
1268
Charulatha Vd0d665a2011-08-31 00:02:21 +05301269 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001270 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301271
Charulatha V03e128c2011-05-05 19:58:01 +05301272 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001273
Nishanth Menon46824e22014-09-05 14:52:55 -05001274 ret = omap_gpio_chip_init(bank, irqc);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001275 if (ret)
1276 return ret;
1277
Tony Lindgren9a748052010-12-07 16:26:56 -08001278 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001279
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301280 pm_runtime_put(bank->dev);
1281
Charulatha V03e128c2011-05-05 19:58:01 +05301282 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001283
Jon Hunter879fe322013-04-04 15:16:12 -05001284 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001285}
1286
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301287#ifdef CONFIG_ARCH_OMAP2PLUS
1288
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001289#if defined(CONFIG_PM)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301290static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001291
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301292static int omap_gpio_runtime_suspend(struct device *dev)
1293{
1294 struct platform_device *pdev = to_platform_device(dev);
1295 struct gpio_bank *bank = platform_get_drvdata(pdev);
1296 u32 l1 = 0, l2 = 0;
1297 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001298 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301299
1300 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001301
1302 /*
1303 * Only edges can generate a wakeup event to the PRCM.
1304 *
1305 * Therefore, ensure any wake-up capable GPIOs have
1306 * edge-detection enabled before going idle to ensure a wakeup
1307 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1308 * NDA TRM 25.5.3.1)
1309 *
1310 * The normal values will be restored upon ->runtime_resume()
1311 * by writing back the values saved in bank->context.
1312 */
1313 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1314 if (wake_low)
Victor Kamensky661553b2013-11-16 02:01:04 +02001315 writel_relaxed(wake_low | bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001316 bank->base + bank->regs->fallingdetect);
1317 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1318 if (wake_hi)
Victor Kamensky661553b2013-11-16 02:01:04 +02001319 writel_relaxed(wake_hi | bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001320 bank->base + bank->regs->risingdetect);
1321
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001322 if (!bank->enabled_non_wakeup_gpios)
1323 goto update_gpio_context_count;
1324
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301325 if (bank->power_mode != OFF_MODE) {
1326 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301327 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301328 }
1329 /*
1330 * If going to OFF, remove triggering for all
1331 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1332 * generated. See OMAP2420 Errata item 1.101.
1333 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001334 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301335 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301336 l1 = bank->context.fallingdetect;
1337 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301338
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301339 l1 &= ~bank->enabled_non_wakeup_gpios;
1340 l2 &= ~bank->enabled_non_wakeup_gpios;
1341
Victor Kamensky661553b2013-11-16 02:01:04 +02001342 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1343 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301344
1345 bank->workaround_enabled = true;
1346
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301347update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301348 if (bank->get_context_loss_count)
1349 bank->context_loss_count =
1350 bank->get_context_loss_count(bank->dev);
1351
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001352 omap_gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301353 spin_unlock_irqrestore(&bank->lock, flags);
1354
1355 return 0;
1356}
1357
Jon Hunter352a2d52013-04-15 13:06:54 -05001358static void omap_gpio_init_context(struct gpio_bank *p);
1359
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301360static int omap_gpio_runtime_resume(struct device *dev)
1361{
1362 struct platform_device *pdev = to_platform_device(dev);
1363 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301364 u32 l = 0, gen, gen0, gen1;
1365 unsigned long flags;
Jon Huntera2797be2013-04-04 15:16:15 -05001366 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301367
1368 spin_lock_irqsave(&bank->lock, flags);
Jon Hunter352a2d52013-04-15 13:06:54 -05001369
1370 /*
1371 * On the first resume during the probe, the context has not
1372 * been initialised and so initialise it now. Also initialise
1373 * the context loss count.
1374 */
1375 if (bank->loses_context && !bank->context_valid) {
1376 omap_gpio_init_context(bank);
1377
1378 if (bank->get_context_loss_count)
1379 bank->context_loss_count =
1380 bank->get_context_loss_count(bank->dev);
1381 }
1382
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001383 omap_gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001384
1385 /*
1386 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1387 * GPIOs were set to edge trigger also in order to be able to
1388 * generate a PRCM wakeup. Here we restore the
1389 * pre-runtime_suspend() values for edge triggering.
1390 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001391 writel_relaxed(bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001392 bank->base + bank->regs->fallingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001393 writel_relaxed(bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001394 bank->base + bank->regs->risingdetect);
1395
Jon Huntera2797be2013-04-04 15:16:15 -05001396 if (bank->loses_context) {
1397 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301398 omap_gpio_restore_context(bank);
1399 } else {
Jon Huntera2797be2013-04-04 15:16:15 -05001400 c = bank->get_context_loss_count(bank->dev);
1401 if (c != bank->context_loss_count) {
1402 omap_gpio_restore_context(bank);
1403 } else {
1404 spin_unlock_irqrestore(&bank->lock, flags);
1405 return 0;
1406 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301407 }
1408 }
1409
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301410 if (!bank->workaround_enabled) {
1411 spin_unlock_irqrestore(&bank->lock, flags);
1412 return 0;
1413 }
1414
Victor Kamensky661553b2013-11-16 02:01:04 +02001415 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301416
1417 /*
1418 * Check if any of the non-wakeup interrupt GPIOs have changed
1419 * state. If so, generate an IRQ by software. This is
1420 * horribly racy, but it's the best we can do to work around
1421 * this silicon bug.
1422 */
1423 l ^= bank->saved_datain;
1424 l &= bank->enabled_non_wakeup_gpios;
1425
1426 /*
1427 * No need to generate IRQs for the rising edge for gpio IRQs
1428 * configured with falling edge only; and vice versa.
1429 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301430 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301431 gen0 &= bank->saved_datain;
1432
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301433 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301434 gen1 &= ~(bank->saved_datain);
1435
1436 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301437 gen = l & (~(bank->context.fallingdetect) &
1438 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301439 /* Consider all GPIO IRQs needed to be updated */
1440 gen |= gen0 | gen1;
1441
1442 if (gen) {
1443 u32 old0, old1;
1444
Victor Kamensky661553b2013-11-16 02:01:04 +02001445 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1446 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301447
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301448 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001449 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301450 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001451 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301452 bank->regs->leveldetect1);
1453 }
1454
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301455 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001456 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301457 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001458 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301459 bank->regs->leveldetect1);
1460 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001461 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1462 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301463 }
1464
1465 bank->workaround_enabled = false;
1466 spin_unlock_irqrestore(&bank->lock, flags);
1467
1468 return 0;
1469}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001470#endif /* CONFIG_PM */
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301471
1472void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001473{
Charulatha V03e128c2011-05-05 19:58:01 +05301474 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001475
Charulatha V03e128c2011-05-05 19:58:01 +05301476 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001477 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301478 continue;
1479
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301480 bank->power_mode = pwr_mode;
1481
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301482 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001483 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001484}
1485
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001486void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001487{
Charulatha V03e128c2011-05-05 19:58:01 +05301488 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001489
Charulatha V03e128c2011-05-05 19:58:01 +05301490 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001491 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301492 continue;
1493
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301494 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001495 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001496}
1497
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001498#if defined(CONFIG_PM)
Jon Hunter352a2d52013-04-15 13:06:54 -05001499static void omap_gpio_init_context(struct gpio_bank *p)
1500{
1501 struct omap_gpio_reg_offs *regs = p->regs;
1502 void __iomem *base = p->base;
1503
Victor Kamensky661553b2013-11-16 02:01:04 +02001504 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1505 p->context.oe = readl_relaxed(base + regs->direction);
1506 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1507 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1508 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1509 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1510 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1511 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1512 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001513
1514 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001515 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001516 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001517 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001518
1519 p->context_valid = true;
1520}
1521
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301522static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301523{
Victor Kamensky661553b2013-11-16 02:01:04 +02001524 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301525 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001526 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1527 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301528 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001529 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301530 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001531 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301532 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001533 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301534 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301535 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001536 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301537 bank->base + bank->regs->set_dataout);
1538 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001539 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301540 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001541 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301542
Nishanth Menonae547352011-09-09 19:08:58 +05301543 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001544 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301545 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001546 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301547 bank->base + bank->regs->debounce_en);
1548 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301549
Victor Kamensky661553b2013-11-16 02:01:04 +02001550 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301551 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001552 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301553 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301554}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001555#endif /* CONFIG_PM */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301556#else
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301557#define omap_gpio_runtime_suspend NULL
1558#define omap_gpio_runtime_resume NULL
Arnd Bergmannea4a21a2013-05-31 17:59:46 +02001559static inline void omap_gpio_init_context(struct gpio_bank *p) {}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301560#endif
1561
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301562static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301563 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1564 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301565};
1566
Benoit Cousson384ebe12011-08-16 11:53:02 +02001567#if defined(CONFIG_OF)
1568static struct omap_gpio_reg_offs omap2_gpio_regs = {
1569 .revision = OMAP24XX_GPIO_REVISION,
1570 .direction = OMAP24XX_GPIO_OE,
1571 .datain = OMAP24XX_GPIO_DATAIN,
1572 .dataout = OMAP24XX_GPIO_DATAOUT,
1573 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1574 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1575 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1576 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1577 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1578 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1579 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1580 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1581 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1582 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1583 .ctrl = OMAP24XX_GPIO_CTRL,
1584 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1585 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1586 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1587 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1588 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1589};
1590
1591static struct omap_gpio_reg_offs omap4_gpio_regs = {
1592 .revision = OMAP4_GPIO_REVISION,
1593 .direction = OMAP4_GPIO_OE,
1594 .datain = OMAP4_GPIO_DATAIN,
1595 .dataout = OMAP4_GPIO_DATAOUT,
1596 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1597 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1598 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1599 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1600 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1601 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1602 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1603 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1604 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1605 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1606 .ctrl = OMAP4_GPIO_CTRL,
1607 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1608 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1609 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1610 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1611 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1612};
1613
Chen Gange9a65bb2013-02-06 18:44:32 +08001614static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001615 .regs = &omap2_gpio_regs,
1616 .bank_width = 32,
1617 .dbck_flag = false,
1618};
1619
Chen Gange9a65bb2013-02-06 18:44:32 +08001620static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001621 .regs = &omap2_gpio_regs,
1622 .bank_width = 32,
1623 .dbck_flag = true,
1624};
1625
Chen Gange9a65bb2013-02-06 18:44:32 +08001626static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001627 .regs = &omap4_gpio_regs,
1628 .bank_width = 32,
1629 .dbck_flag = true,
1630};
1631
1632static const struct of_device_id omap_gpio_match[] = {
1633 {
1634 .compatible = "ti,omap4-gpio",
1635 .data = &omap4_pdata,
1636 },
1637 {
1638 .compatible = "ti,omap3-gpio",
1639 .data = &omap3_pdata,
1640 },
1641 {
1642 .compatible = "ti,omap2-gpio",
1643 .data = &omap2_pdata,
1644 },
1645 { },
1646};
1647MODULE_DEVICE_TABLE(of, omap_gpio_match);
1648#endif
1649
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001650static struct platform_driver omap_gpio_driver = {
1651 .probe = omap_gpio_probe,
1652 .driver = {
1653 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301654 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001655 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001656 },
1657};
1658
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001659/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001660 * gpio driver register needs to be done before
1661 * machine_init functions access gpio APIs.
1662 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001663 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001664static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001665{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001666 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001667}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001668postcore_initcall(omap_gpio_drv_reg);