blob: 466fe70cf303c81b8d4146e0e801ee8fa6fb5e2b [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;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +020060 raw_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
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +020078 void (*set_dataout)(struct gpio_bank *bank, unsigned 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
Charulatha Vc8eef652011-05-02 15:21:42 +053084#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020086#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020087#define LINE_USED(line, offset) (line & (BIT(offset)))
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020088
Tony Lindgren3d009c82015-01-16 14:50:50 -080089static void omap_gpio_unmask_irq(struct irq_data *d);
90
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020091static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -060092{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +020093 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
94 return container_of(chip, struct gpio_bank, chip);
Benoit Cousson25db7112012-02-23 21:50:10 +010095}
96
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020097static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
98 int is_input)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100100 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100101 u32 l;
102
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700103 reg += bank->regs->direction;
Victor Kamensky661553b2013-11-16 02:01:04 +0200104 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100105 if (is_input)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200106 l |= BIT(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100107 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200108 l &= ~(BIT(gpio));
Victor Kamensky661553b2013-11-16 02:01:04 +0200109 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530110 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100111}
112
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700113
114/* set data out value using dedicate set/clear register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200115static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200116 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100117{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100118 void __iomem *reg = bank->base;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200119 u32 l = BIT(offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100120
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530121 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700122 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530123 bank->context.dataout |= l;
124 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700125 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530126 bank->context.dataout &= ~l;
127 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700128
Victor Kamensky661553b2013-11-16 02:01:04 +0200129 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700130}
131
132/* set data out value using mask register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200133static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200134 int enable)
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700135{
136 void __iomem *reg = bank->base + bank->regs->dataout;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200137 u32 gpio_bit = BIT(offset);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700138 u32 l;
139
Victor Kamensky661553b2013-11-16 02:01:04 +0200140 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700141 if (enable)
142 l |= gpio_bit;
143 else
144 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200145 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530146 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100147}
148
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200149static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700151 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100152
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200153 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100154}
155
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200156static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300157{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700158 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300159
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200160 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300161}
162
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200163static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700164{
Victor Kamensky661553b2013-11-16 02:01:04 +0200165 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700166
Benoit Cousson862ff642012-02-01 15:58:56 +0100167 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700168 l |= mask;
169 else
170 l &= ~mask;
171
Victor Kamensky661553b2013-11-16 02:01:04 +0200172 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700173}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100174
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200175static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530176{
177 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Rajendra Nayak345477f2014-04-23 11:41:03 +0530178 clk_prepare_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530179 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300180
Victor Kamensky661553b2013-11-16 02:01:04 +0200181 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300182 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530183 }
184}
185
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200186static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530187{
188 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300189 /*
190 * Disable debounce before cutting it's clock. If debounce is
191 * enabled but the clock is not, GPIO module seems to be unable
192 * to detect events and generate interrupts at least on OMAP3.
193 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200194 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300195
Rajendra Nayak345477f2014-04-23 11:41:03 +0530196 clk_disable_unprepare(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530197 bank->dbck_enabled = false;
198 }
199}
200
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700201/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200202 * omap2_set_gpio_debounce - low level gpio debounce time
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700203 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200204 * @offset: the gpio number on this @bank
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700205 * @debounce: debounce time to use
206 *
207 * OMAP's debounce time is in 31us steps so we need
208 * to convert and round up to the closest unit.
209 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200210static void omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200211 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700212{
Kevin Hilman9942da02011-04-22 12:02:05 -0700213 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700214 u32 val;
215 u32 l;
216
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800217 if (!bank->dbck_flag)
218 return;
219
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700220 if (debounce < 32)
221 debounce = 0x01;
222 else if (debounce > 7936)
223 debounce = 0xff;
224 else
225 debounce = (debounce / 0x1f) - 1;
226
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200227 l = BIT(offset);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700228
Rajendra Nayak345477f2014-04-23 11:41:03 +0530229 clk_prepare_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700230 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200231 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700232
Kevin Hilman9942da02011-04-22 12:02:05 -0700233 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200234 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700235
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530236 if (debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700237 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530238 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700239 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300240 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700241
Victor Kamensky661553b2013-11-16 02:01:04 +0200242 writel_relaxed(val, reg);
Rajendra Nayak345477f2014-04-23 11:41:03 +0530243 clk_disable_unprepare(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530244 /*
245 * Enable debounce clock per module.
246 * This call is mandatory because in omap_gpio_request() when
247 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
248 * runtime callbck fails to turn on dbck because dbck_enable_mask
249 * used within _gpio_dbck_enable() is still not initialized at
250 * that point. Therefore we have to enable dbck here.
251 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200252 omap_gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530253 if (bank->dbck_enable_mask) {
254 bank->context.debounce = debounce;
255 bank->context.debounce_en = val;
256 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700257}
258
Jon Hunterc9c55d92012-10-26 14:26:04 -0500259/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200260 * omap_clear_gpio_debounce - clear debounce settings for a gpio
Jon Hunterc9c55d92012-10-26 14:26:04 -0500261 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200262 * @offset: the gpio number on this @bank
Jon Hunterc9c55d92012-10-26 14:26:04 -0500263 *
264 * If a gpio is using debounce, then clear the debounce enable bit and if
265 * this is the only gpio in this bank using debounce, then clear the debounce
266 * time too. The debounce clock will also be disabled when calling this function
267 * if this is the only gpio in the bank using debounce.
268 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200269static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
Jon Hunterc9c55d92012-10-26 14:26:04 -0500270{
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200271 u32 gpio_bit = BIT(offset);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500272
273 if (!bank->dbck_flag)
274 return;
275
276 if (!(bank->dbck_enable_mask & gpio_bit))
277 return;
278
279 bank->dbck_enable_mask &= ~gpio_bit;
280 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200281 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500282 bank->base + bank->regs->debounce_en);
283
284 if (!bank->dbck_enable_mask) {
285 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200286 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500287 bank->regs->debounce);
Rajendra Nayak345477f2014-04-23 11:41:03 +0530288 clk_disable_unprepare(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500289 bank->dbck_enabled = false;
290 }
291}
292
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200293static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530294 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100295{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800296 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200297 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100298
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200299 omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
300 trigger & IRQ_TYPE_LEVEL_LOW);
301 omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
302 trigger & IRQ_TYPE_LEVEL_HIGH);
303 omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
304 trigger & IRQ_TYPE_EDGE_RISING);
305 omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
306 trigger & IRQ_TYPE_EDGE_FALLING);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530307
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530308 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200309 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530310 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200311 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530312 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200313 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530314 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200315 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530316
317 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200318 omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530319 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200320 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530321 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530322
Ambresh K55b220c2011-06-15 13:40:45 -0700323 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530324 if (!bank->regs->irqctrl) {
325 /* On omap24xx proceed only when valid GPIO bit is set */
326 if (bank->non_wakeup_gpios) {
327 if (!(bank->non_wakeup_gpios & gpio_bit))
328 goto exit;
329 }
330
Chunqiu Wang699117a62009-06-24 17:13:39 +0000331 /*
332 * Log the edge gpio and manually trigger the IRQ
333 * after resume if the input level changes
334 * to avoid irq lost during PER RET/OFF mode
335 * Applies for omap2 non-wakeup gpio and all omap3 gpios
336 */
337 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800338 bank->enabled_non_wakeup_gpios |= gpio_bit;
339 else
340 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
341 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700342
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530343exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530344 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200345 readl_relaxed(bank->base + bank->regs->leveldetect0) |
346 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100347}
348
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800349#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800350/*
351 * This only applies to chips that can't do both rising and falling edge
352 * detection at once. For all other chips, this function is a noop.
353 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200354static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800355{
356 void __iomem *reg = bank->base;
357 u32 l = 0;
358
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530359 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800360 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530361
362 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800363
Victor Kamensky661553b2013-11-16 02:01:04 +0200364 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800365 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200366 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800367 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200368 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800369
Victor Kamensky661553b2013-11-16 02:01:04 +0200370 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800371}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530372#else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200373static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800374#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800375
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200376static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
377 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100378{
379 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530380 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100381 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100382
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530383 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200384 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530385 } else if (bank->regs->irqctrl) {
386 reg += bank->regs->irqctrl;
387
Victor Kamensky661553b2013-11-16 02:01:04 +0200388 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000389 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200390 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100391 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200392 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100393 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200394 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100395 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530396 return -EINVAL;
397
Victor Kamensky661553b2013-11-16 02:01:04 +0200398 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530399 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100400 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530401 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100402 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530403 reg += bank->regs->edgectrl1;
404
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100405 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200406 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100407 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100408 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100409 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100410 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200411 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530412
413 /* Enable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200414 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530415 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200416 readl_relaxed(bank->base + bank->regs->wkup_en);
417 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100418 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100419 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100420}
421
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200422static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200423{
424 if (bank->regs->pinctrl) {
425 void __iomem *reg = bank->base + bank->regs->pinctrl;
426
427 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200428 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200429 }
430
431 if (bank->regs->ctrl && !BANK_USED(bank)) {
432 void __iomem *reg = bank->base + bank->regs->ctrl;
433 u32 ctrl;
434
Victor Kamensky661553b2013-11-16 02:01:04 +0200435 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200436 /* Module is enabled, clocks are not gated */
437 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200438 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200439 bank->context.ctrl = ctrl;
440 }
441}
442
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200443static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200444{
445 void __iomem *base = bank->base;
446
447 if (bank->regs->wkup_en &&
448 !LINE_USED(bank->mod_usage, offset) &&
449 !LINE_USED(bank->irq_usage, offset)) {
450 /* Disable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200451 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200452 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200453 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200454 }
455
456 if (bank->regs->ctrl && !BANK_USED(bank)) {
457 void __iomem *reg = bank->base + bank->regs->ctrl;
458 u32 ctrl;
459
Victor Kamensky661553b2013-11-16 02:01:04 +0200460 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200461 /* Module is disabled, clocks are gated */
462 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200463 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200464 bank->context.ctrl = ctrl;
465 }
466}
467
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200468static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200469{
470 void __iomem *reg = bank->base + bank->regs->direction;
471
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200472 return readl_relaxed(reg) & BIT(offset);
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200473}
474
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200475static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
Tony Lindgren3d009c82015-01-16 14:50:50 -0800476{
477 if (!LINE_USED(bank->mod_usage, offset)) {
478 omap_enable_gpio_module(bank, offset);
479 omap_set_gpio_direction(bank, offset, 1);
480 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200481 bank->irq_usage |= BIT(offset);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800482}
483
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200484static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100485{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200486 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100487 int retval;
David Brownella6472532008-03-03 04:33:30 -0800488 unsigned long flags;
Grygorii Strashkoea5fbe82015-03-23 14:18:29 +0200489 unsigned offset = d->hwirq;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100490
David Brownelle5c56ed2006-12-06 17:13:59 -0800491 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100492 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800493
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530494 if (!bank->regs->leveldetect0 &&
495 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100496 return -EINVAL;
497
Grygorii Strashko1562e462015-05-22 17:35:49 +0300498 if (!BANK_USED(bank))
499 pm_runtime_get_sync(bank->dev);
500
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200501 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200502 retval = omap_set_gpio_triggering(bank, offset, type);
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300503 if (retval) {
Axel Lin627c89b2015-08-05 22:37:41 +0800504 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300505 goto error;
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300506 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200507 omap_gpio_init_irq(bank, offset);
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200508 if (!omap_gpio_is_input(bank, offset)) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200509 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300510 retval = -EINVAL;
511 goto error;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200512 }
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200513 raw_spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800514
515 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner43ec2e42015-06-23 15:52:39 +0200516 irq_set_handler_locked(d, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800517 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner43ec2e42015-06-23 15:52:39 +0200518 irq_set_handler_locked(d, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800519
Grygorii Strashko1562e462015-05-22 17:35:49 +0300520 return 0;
521
522error:
523 if (!BANK_USED(bank))
524 pm_runtime_put(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100525 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100526}
527
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200528static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100530 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100531
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700532 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200533 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300534
535 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700536 if (bank->regs->irqstatus2) {
537 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200538 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700539 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700540
541 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200542 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100543}
544
Grygorii Strashko9943f262015-03-23 14:18:27 +0200545static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
546 unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100547{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200548 omap_clear_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100549}
550
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200551static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
Imre Deakea6dedd2006-06-26 16:16:00 -0700552{
553 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700554 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200555 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700556
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700557 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200558 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700559 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700560 l = ~l;
561 l &= mask;
562 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700563}
564
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200565static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100566{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100567 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100568 u32 l;
569
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700570 if (bank->regs->set_irqenable) {
571 reg += bank->regs->set_irqenable;
572 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530573 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700574 } else {
575 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200576 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700577 if (bank->regs->irqenable_inv)
578 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100579 else
580 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530581 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100582 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700583
Victor Kamensky661553b2013-11-16 02:01:04 +0200584 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700585}
586
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200587static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700588{
589 void __iomem *reg = bank->base;
590 u32 l;
591
592 if (bank->regs->clr_irqenable) {
593 reg += bank->regs->clr_irqenable;
594 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530595 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700596 } else {
597 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200598 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700599 if (bank->regs->irqenable_inv)
600 l |= gpio_mask;
601 else
602 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530603 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700604 }
605
Victor Kamensky661553b2013-11-16 02:01:04 +0200606 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100607}
608
Grygorii Strashko9943f262015-03-23 14:18:27 +0200609static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
610 unsigned offset, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100611{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530612 if (enable)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200613 omap_enable_gpio_irqbank(bank, BIT(offset));
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530614 else
Grygorii Strashko9943f262015-03-23 14:18:27 +0200615 omap_disable_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100616}
617
Tony Lindgren92105bb2005-09-07 17:20:26 +0100618/*
619 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
620 * 1510 does not seem to have a wake-up register. If JTAG is connected
621 * to the target, system will wake up always on GPIO events. While
622 * system is running all registered GPIO interrupts need to have wake-up
623 * enabled. When system is suspended, only selected GPIO interrupts need
624 * to have wake-up enabled.
625 */
Grygorii Strashko9943f262015-03-23 14:18:27 +0200626static int omap_set_gpio_wakeup(struct gpio_bank *bank, unsigned offset,
627 int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100628{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200629 u32 gpio_bit = BIT(offset);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700630 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800631
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700632 if (bank->non_wakeup_gpios & gpio_bit) {
Benoit Cousson862ff642012-02-01 15:58:56 +0100633 dev_err(bank->dev,
Grygorii Strashko9943f262015-03-23 14:18:27 +0200634 "Unable to modify wakeup on non-wakeup GPIO%d\n",
635 offset);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100636 return -EINVAL;
637 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700638
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200639 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700640 if (enable)
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530641 bank->context.wake_en |= gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700642 else
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530643 bank->context.wake_en &= ~gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700644
Victor Kamensky661553b2013-11-16 02:01:04 +0200645 writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200646 raw_spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700647
648 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100649}
650
651/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200652static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100653{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200654 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200655 unsigned offset = d->hwirq;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100656
Grygorii Strashko9943f262015-03-23 14:18:27 +0200657 return omap_set_gpio_wakeup(bank, offset, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100658}
659
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800660static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100661{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800662 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800663 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100664
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530665 /*
666 * If this is the first gpio_request for the bank,
667 * enable the bank module.
668 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200669 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530670 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100671
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200672 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashkoc3518172015-05-22 17:35:51 +0300673 omap_enable_gpio_module(bank, offset);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200674 bank->mod_usage |= BIT(offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200675 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100676
677 return 0;
678}
679
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800680static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100681{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800682 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800683 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100684
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200685 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200686 bank->mod_usage &= ~(BIT(offset));
Grygorii Strashko5f982c72015-05-22 17:35:48 +0300687 if (!LINE_USED(bank->irq_usage, offset)) {
688 omap_set_gpio_direction(bank, offset, 1);
689 omap_clear_gpio_debounce(bank, offset);
690 }
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200691 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200692 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530693
694 /*
695 * If this is the last gpio to be freed in the bank,
696 * disable the bank module.
697 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200698 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530699 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100700}
701
702/*
703 * We need to unmask the GPIO bank interrupt as soon as possible to
704 * avoid missing GPIO interrupts for other lines in the bank.
705 * Then we need to mask-read-clear-unmask the triggered GPIO lines
706 * in the bank to avoid missing nested interrupts for a GPIO line.
707 * If we wait to unmask individual GPIO lines in the bank after the
708 * line's interrupt handler has been run, we may miss some nested
709 * interrupts.
710 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200711static void omap_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100712{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100713 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100714 u32 isr;
Jon Hunter3513cde2013-04-04 15:16:14 -0500715 unsigned int bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100716 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700717 int unmasked = 0;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200718 struct irq_chip *irqchip = irq_desc_get_chip(desc);
Jiang Liu476f8b42015-06-04 12:13:15 +0800719 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100720
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200721 chained_irq_enter(irqchip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100722
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200723 bank = container_of(chip, struct gpio_bank, chip);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700724 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530725 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800726
727 if (WARN_ON(!isr_reg))
728 goto exit;
729
Laurent Navete83507b2013-03-20 13:15:57 +0100730 while (1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100731 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700732 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100733
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200734 enabled = omap_get_gpio_irqbank_mask(bank);
Victor Kamensky661553b2013-11-16 02:01:04 +0200735 isr_saved = isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100736
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530737 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800738 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100739
740 /* clear edge sensitive interrupts before handler(s) are
741 called so that we don't miss any interrupt occurred while
742 executing them */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200743 omap_disable_gpio_irqbank(bank, isr_saved & ~level_mask);
744 omap_clear_gpio_irqbank(bank, isr_saved & ~level_mask);
745 omap_enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100746
747 /* if there is only edge sensitive GPIO pin interrupts
748 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700749 if (!level_mask && !unmasked) {
750 unmasked = 1;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200751 chained_irq_exit(irqchip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700752 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100753
Tony Lindgren92105bb2005-09-07 17:20:26 +0100754 if (!isr)
755 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756
Jon Hunter3513cde2013-04-04 15:16:14 -0500757 while (isr) {
758 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200759 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100760
Cory Maccarrone4318f362010-01-08 10:29:04 -0800761 /*
762 * Some chips can't respond to both rising and falling
763 * at the same time. If this irq was requested with
764 * both flags, we need to flip the ICR data for the IRQ
765 * to respond to the IRQ for the opposite direction.
766 * This will be indicated in the bank toggle_mask.
767 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200768 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200769 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800770
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200771 generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
772 bit));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100773 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000774 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700775 /* if bank has any level sensitive GPIO pin interrupt
776 configured, we must unmask the bank interrupt only after
777 handler(s) are executed in order to avoid spurious bank
778 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800779exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700780 if (!unmasked)
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200781 chained_irq_exit(irqchip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530782 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100783}
784
Tony Lindgren3d009c82015-01-16 14:50:50 -0800785static unsigned int omap_gpio_irq_startup(struct irq_data *d)
786{
787 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800788 unsigned long flags;
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200789 unsigned offset = d->hwirq;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800790
791 if (!BANK_USED(bank))
792 pm_runtime_get_sync(bank->dev);
793
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200794 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300795
796 if (!LINE_USED(bank->mod_usage, offset))
797 omap_set_gpio_direction(bank, offset, 1);
798 else if (!omap_gpio_is_input(bank, offset))
799 goto err;
800 omap_enable_gpio_module(bank, offset);
801 bank->irq_usage |= BIT(offset);
802
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200803 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800804 omap_gpio_unmask_irq(d);
805
806 return 0;
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300807err:
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200808 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300809 if (!BANK_USED(bank))
810 pm_runtime_put(bank->dev);
811 return -EINVAL;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800812}
813
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200814static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300815{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200816 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700817 unsigned long flags;
Grygorii Strashko9943f262015-03-23 14:18:27 +0200818 unsigned offset = d->hwirq;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300819
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200820 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200821 bank->irq_usage &= ~(BIT(offset));
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300822 omap_set_gpio_irqenable(bank, offset, 0);
823 omap_clear_gpio_irqstatus(bank, offset);
824 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
825 if (!LINE_USED(bank->mod_usage, offset))
826 omap_clear_gpio_debounce(bank, offset);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200827 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200828 raw_spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200829
830 /*
831 * If this is the last IRQ to be freed in the bank,
832 * disable the bank module.
833 */
834 if (!BANK_USED(bank))
835 pm_runtime_put(bank->dev);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300836}
837
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200838static void omap_gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100839{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200840 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200841 unsigned offset = d->hwirq;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100842
Grygorii Strashko9943f262015-03-23 14:18:27 +0200843 omap_clear_gpio_irqstatus(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100844}
845
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200846static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100847{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200848 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200849 unsigned offset = d->hwirq;
Colin Cross85ec7b92011-06-06 13:38:18 -0700850 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100851
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200852 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200853 omap_set_gpio_irqenable(bank, offset, 0);
854 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200855 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100856}
857
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200858static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100859{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200860 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200861 unsigned offset = d->hwirq;
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100862 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700863 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700864
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200865 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700866 if (trigger)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200867 omap_set_gpio_triggering(bank, offset, trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800868
869 /* For level-triggered GPIOs, the clearing must be done after
870 * the HW source is cleared, thus after the handler has run */
Grygorii Strashko9943f262015-03-23 14:18:27 +0200871 if (bank->level_mask & BIT(offset)) {
872 omap_set_gpio_irqenable(bank, offset, 0);
873 omap_clear_gpio_irqstatus(bank, offset);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800874 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100875
Grygorii Strashko9943f262015-03-23 14:18:27 +0200876 omap_set_gpio_irqenable(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200877 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100878}
879
David Brownelle5c56ed2006-12-06 17:13:59 -0800880/*---------------------------------------------------------------------*/
881
Magnus Damm79ee0312009-07-08 13:22:04 +0200882static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800883{
Magnus Damm79ee0312009-07-08 13:22:04 +0200884 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800885 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800886 void __iomem *mask_reg = bank->base +
887 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800888 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800889
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200890 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200891 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200892 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800893
894 return 0;
895}
896
Magnus Damm79ee0312009-07-08 13:22:04 +0200897static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800898{
Magnus Damm79ee0312009-07-08 13:22:04 +0200899 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800900 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800901 void __iomem *mask_reg = bank->base +
902 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800903 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800904
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200905 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200906 writel_relaxed(bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200907 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800908
909 return 0;
910}
911
Alexey Dobriyan47145212009-12-14 18:00:08 -0800912static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200913 .suspend_noirq = omap_mpuio_suspend_noirq,
914 .resume_noirq = omap_mpuio_resume_noirq,
915};
916
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200917/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800918static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800919 .driver = {
920 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200921 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800922 },
923};
924
925static struct platform_device omap_mpuio_device = {
926 .name = "mpuio",
927 .id = -1,
928 .dev = {
929 .driver = &omap_mpuio_driver.driver,
930 }
931 /* could list the /proc/iomem resources */
932};
933
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200934static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800935{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800936 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700937
David Brownell11a78b72006-12-06 17:14:11 -0800938 if (platform_driver_register(&omap_mpuio_driver) == 0)
939 (void) platform_device_register(&omap_mpuio_device);
940}
941
David Brownelle5c56ed2006-12-06 17:13:59 -0800942/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100943
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200944static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200945{
946 struct gpio_bank *bank;
947 unsigned long flags;
948 void __iomem *reg;
949 int dir;
950
951 bank = container_of(chip, struct gpio_bank, chip);
952 reg = bank->base + bank->regs->direction;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200953 raw_spin_lock_irqsave(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +0200954 dir = !!(readl_relaxed(reg) & BIT(offset));
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200955 raw_spin_unlock_irqrestore(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +0200956 return dir;
957}
958
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200959static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800960{
961 struct gpio_bank *bank;
962 unsigned long flags;
963
964 bank = container_of(chip, struct gpio_bank, chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200965 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200966 omap_set_gpio_direction(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200967 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800968 return 0;
969}
970
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200971static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800972{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300973 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300974
Charulatha Va8be8da2011-04-22 16:38:16 +0530975 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300976
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200977 if (omap_gpio_is_input(bank, offset))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200978 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300979 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200980 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -0800981}
982
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200983static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800984{
985 struct gpio_bank *bank;
986 unsigned long flags;
987
988 bank = container_of(chip, struct gpio_bank, chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200989 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700990 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200991 omap_set_gpio_direction(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200992 raw_spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200993 return 0;
David Brownell52e31342008-03-03 12:43:23 -0800994}
995
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200996static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
997 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700998{
999 struct gpio_bank *bank;
1000 unsigned long flags;
1001
1002 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001003
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001004 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001005 omap2_set_gpio_debounce(bank, offset, debounce);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001006 raw_spin_unlock_irqrestore(&bank->lock, flags);
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001007
1008 return 0;
1009}
1010
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001011static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001012{
1013 struct gpio_bank *bank;
1014 unsigned long flags;
1015
1016 bank = container_of(chip, struct gpio_bank, chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001017 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001018 bank->set_dataout(bank, offset, value);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001019 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -08001020}
1021
1022/*---------------------------------------------------------------------*/
1023
Tony Lindgren9a748052010-12-07 16:26:56 -08001024static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001025{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001026 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001027 u32 rev;
1028
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001029 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001030 return;
1031
Victor Kamensky661553b2013-11-16 02:01:04 +02001032 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001033 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001034 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001035
1036 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001037}
1038
Charulatha V03e128c2011-05-05 19:58:01 +05301039static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001040{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301041 void __iomem *base = bank->base;
1042 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001043
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301044 if (bank->width == 16)
1045 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001046
Charulatha Vd0d665a2011-08-31 00:02:21 +05301047 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001048 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301049 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001050 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301051
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001052 omap_gpio_rmw(base, bank->regs->irqenable, l,
1053 bank->regs->irqenable_inv);
1054 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1055 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301056 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001057 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301058
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301059 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001060 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301061 /* Initialize interface clk ungated, module enabled */
1062 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001063 writel_relaxed(0, base + bank->regs->ctrl);
Tarun Kanti DebBarma34672012012-07-11 14:43:14 +05301064
1065 bank->dbck = clk_get(bank->dev, "dbclk");
1066 if (IS_ERR(bank->dbck))
1067 dev_err(bank->dev, "Could not get gpio dbck\n");
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001068}
1069
Nishanth Menon46824e22014-09-05 14:52:55 -05001070static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001071{
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001072 static int gpio;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001073 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001074 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001075
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001076 /*
1077 * REVISIT eventually switch from OMAP-specific gpio structs
1078 * over to the generic ones
1079 */
1080 bank->chip.request = omap_gpio_request;
1081 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001082 bank->chip.get_direction = omap_gpio_get_direction;
1083 bank->chip.direction_input = omap_gpio_input;
1084 bank->chip.get = omap_gpio_get;
1085 bank->chip.direction_output = omap_gpio_output;
1086 bank->chip.set_debounce = omap_gpio_debounce;
1087 bank->chip.set = omap_gpio_set;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301088 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001089 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301090 if (bank->regs->wkup_en)
1091 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001092 bank->chip.base = OMAP_MPUIO(0);
1093 } else {
1094 bank->chip.label = "gpio";
1095 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001096 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001097 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001098 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001099
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001100 ret = gpiochip_add(&bank->chip);
1101 if (ret) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001102 dev_err(bank->dev, "Could not register gpio chip %d\n", ret);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001103 return ret;
1104 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001105
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001106#ifdef CONFIG_ARCH_OMAP1
1107 /*
1108 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1109 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1110 */
1111 irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
1112 if (irq_base < 0) {
1113 dev_err(bank->dev, "Couldn't allocate IRQ numbers\n");
1114 return -ENODEV;
1115 }
1116#endif
1117
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001118 /* MPUIO is a bit different, reading IRQ status clears it */
1119 if (bank->is_mpuio) {
1120 irqc->irq_ack = dummy_irq_chip.irq_ack;
1121 irqc->irq_mask = irq_gc_mask_set_bit;
1122 irqc->irq_unmask = irq_gc_mask_clr_bit;
1123 if (!bank->regs->wkup_en)
1124 irqc->irq_set_wake = NULL;
1125 }
1126
Nishanth Menon46824e22014-09-05 14:52:55 -05001127 ret = gpiochip_irqchip_add(&bank->chip, irqc,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001128 irq_base, omap_gpio_irq_handler,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001129 IRQ_TYPE_NONE);
1130
1131 if (ret) {
1132 dev_err(bank->dev, "Couldn't add irqchip to gpiochip %d\n", ret);
Linus Walleijda26d5d2014-09-16 15:11:41 -07001133 gpiochip_remove(&bank->chip);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001134 return -ENODEV;
1135 }
1136
Nishanth Menon46824e22014-09-05 14:52:55 -05001137 gpiochip_set_chained_irqchip(&bank->chip, irqc,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001138 bank->irq, omap_gpio_irq_handler);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001139
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001140 return 0;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001141}
1142
Benoit Cousson384ebe12011-08-16 11:53:02 +02001143static const struct of_device_id omap_gpio_match[];
1144
Bill Pemberton38363092012-11-19 13:22:34 -05001145static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001146{
Benoit Cousson862ff642012-02-01 15:58:56 +01001147 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001148 struct device_node *node = dev->of_node;
1149 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001150 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001151 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001152 struct gpio_bank *bank;
Nishanth Menon46824e22014-09-05 14:52:55 -05001153 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001154 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001155
Benoit Cousson384ebe12011-08-16 11:53:02 +02001156 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1157
Jingoo Hane56aee12013-07-30 17:08:05 +09001158 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001159 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001160 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001161
Tobias Klauser086d5852012-10-05 11:37:38 +02001162 bank = devm_kzalloc(dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301163 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001164 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001165 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301166 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001167
Nishanth Menon46824e22014-09-05 14:52:55 -05001168 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1169 if (!irqc)
1170 return -ENOMEM;
1171
Tony Lindgren3d009c82015-01-16 14:50:50 -08001172 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e22014-09-05 14:52:55 -05001173 irqc->irq_shutdown = omap_gpio_irq_shutdown,
1174 irqc->irq_ack = omap_gpio_ack_irq,
1175 irqc->irq_mask = omap_gpio_mask_irq,
1176 irqc->irq_unmask = omap_gpio_unmask_irq,
1177 irqc->irq_set_type = omap_gpio_irq_type,
1178 irqc->irq_set_wake = omap_gpio_wake_enable,
1179 irqc->name = dev_name(&pdev->dev);
1180
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001181 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1182 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001183 dev_err(dev, "Invalid IRQ resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001184 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001185 }
1186
1187 bank->irq = res->start;
Benoit Cousson862ff642012-02-01 15:58:56 +01001188 bank->dev = dev;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001189 bank->chip.dev = dev;
Grygorii Strashkoc23837c2015-06-25 18:13:33 +03001190 bank->chip.owner = THIS_MODULE;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001191 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001192 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001193 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301194 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301195 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001196 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001197#ifdef CONFIG_OF_GPIO
1198 bank->chip.of_node = of_node_get(node);
1199#endif
Jon Huntera2797be2013-04-04 15:16:15 -05001200 if (node) {
1201 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1202 bank->loses_context = true;
1203 } else {
1204 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001205
1206 if (bank->loses_context)
1207 bank->get_context_loss_count =
1208 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001209 }
1210
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001211 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001212 bank->set_dataout = omap_set_gpio_dataout_reg;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001213 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001214 bank->set_dataout = omap_set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001215
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001216 raw_spin_lock_init(&bank->lock);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001217
1218 /* Static mapping, never released */
1219 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001220 bank->base = devm_ioremap_resource(dev, res);
1221 if (IS_ERR(bank->base)) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001222 irq_domain_remove(bank->chip.irqdomain);
Jingoo Han717f70e2014-02-12 11:51:38 +09001223 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001224 }
1225
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301226 platform_set_drvdata(pdev, bank);
1227
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001228 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301229 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001230 pm_runtime_get_sync(bank->dev);
1231
Charulatha Vd0d665a2011-08-31 00:02:21 +05301232 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001233 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301234
Charulatha V03e128c2011-05-05 19:58:01 +05301235 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001236
Nishanth Menon46824e22014-09-05 14:52:55 -05001237 ret = omap_gpio_chip_init(bank, irqc);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001238 if (ret)
1239 return ret;
1240
Tony Lindgren9a748052010-12-07 16:26:56 -08001241 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001242
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301243 pm_runtime_put(bank->dev);
1244
Charulatha V03e128c2011-05-05 19:58:01 +05301245 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001246
Jon Hunter879fe322013-04-04 15:16:12 -05001247 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001248}
1249
Tony Lindgrencac089f2015-04-23 16:56:22 -07001250static int omap_gpio_remove(struct platform_device *pdev)
1251{
1252 struct gpio_bank *bank = platform_get_drvdata(pdev);
1253
1254 list_del(&bank->node);
1255 gpiochip_remove(&bank->chip);
1256 pm_runtime_disable(bank->dev);
1257
1258 return 0;
1259}
1260
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301261#ifdef CONFIG_ARCH_OMAP2PLUS
1262
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001263#if defined(CONFIG_PM)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301264static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001265
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301266static int omap_gpio_runtime_suspend(struct device *dev)
1267{
1268 struct platform_device *pdev = to_platform_device(dev);
1269 struct gpio_bank *bank = platform_get_drvdata(pdev);
1270 u32 l1 = 0, l2 = 0;
1271 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001272 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301273
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001274 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001275
1276 /*
1277 * Only edges can generate a wakeup event to the PRCM.
1278 *
1279 * Therefore, ensure any wake-up capable GPIOs have
1280 * edge-detection enabled before going idle to ensure a wakeup
1281 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1282 * NDA TRM 25.5.3.1)
1283 *
1284 * The normal values will be restored upon ->runtime_resume()
1285 * by writing back the values saved in bank->context.
1286 */
1287 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1288 if (wake_low)
Victor Kamensky661553b2013-11-16 02:01:04 +02001289 writel_relaxed(wake_low | bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001290 bank->base + bank->regs->fallingdetect);
1291 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1292 if (wake_hi)
Victor Kamensky661553b2013-11-16 02:01:04 +02001293 writel_relaxed(wake_hi | bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001294 bank->base + bank->regs->risingdetect);
1295
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001296 if (!bank->enabled_non_wakeup_gpios)
1297 goto update_gpio_context_count;
1298
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301299 if (bank->power_mode != OFF_MODE) {
1300 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301301 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301302 }
1303 /*
1304 * If going to OFF, remove triggering for all
1305 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1306 * generated. See OMAP2420 Errata item 1.101.
1307 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001308 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301309 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301310 l1 = bank->context.fallingdetect;
1311 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301312
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301313 l1 &= ~bank->enabled_non_wakeup_gpios;
1314 l2 &= ~bank->enabled_non_wakeup_gpios;
1315
Victor Kamensky661553b2013-11-16 02:01:04 +02001316 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1317 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301318
1319 bank->workaround_enabled = true;
1320
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301321update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301322 if (bank->get_context_loss_count)
1323 bank->context_loss_count =
1324 bank->get_context_loss_count(bank->dev);
1325
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001326 omap_gpio_dbck_disable(bank);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001327 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301328
1329 return 0;
1330}
1331
Jon Hunter352a2d52013-04-15 13:06:54 -05001332static void omap_gpio_init_context(struct gpio_bank *p);
1333
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301334static int omap_gpio_runtime_resume(struct device *dev)
1335{
1336 struct platform_device *pdev = to_platform_device(dev);
1337 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301338 u32 l = 0, gen, gen0, gen1;
1339 unsigned long flags;
Jon Huntera2797be2013-04-04 15:16:15 -05001340 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301341
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001342 raw_spin_lock_irqsave(&bank->lock, flags);
Jon Hunter352a2d52013-04-15 13:06:54 -05001343
1344 /*
1345 * On the first resume during the probe, the context has not
1346 * been initialised and so initialise it now. Also initialise
1347 * the context loss count.
1348 */
1349 if (bank->loses_context && !bank->context_valid) {
1350 omap_gpio_init_context(bank);
1351
1352 if (bank->get_context_loss_count)
1353 bank->context_loss_count =
1354 bank->get_context_loss_count(bank->dev);
1355 }
1356
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001357 omap_gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001358
1359 /*
1360 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1361 * GPIOs were set to edge trigger also in order to be able to
1362 * generate a PRCM wakeup. Here we restore the
1363 * pre-runtime_suspend() values for edge triggering.
1364 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001365 writel_relaxed(bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001366 bank->base + bank->regs->fallingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001367 writel_relaxed(bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001368 bank->base + bank->regs->risingdetect);
1369
Jon Huntera2797be2013-04-04 15:16:15 -05001370 if (bank->loses_context) {
1371 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301372 omap_gpio_restore_context(bank);
1373 } else {
Jon Huntera2797be2013-04-04 15:16:15 -05001374 c = bank->get_context_loss_count(bank->dev);
1375 if (c != bank->context_loss_count) {
1376 omap_gpio_restore_context(bank);
1377 } else {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001378 raw_spin_unlock_irqrestore(&bank->lock, flags);
Jon Huntera2797be2013-04-04 15:16:15 -05001379 return 0;
1380 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301381 }
1382 }
1383
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301384 if (!bank->workaround_enabled) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001385 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301386 return 0;
1387 }
1388
Victor Kamensky661553b2013-11-16 02:01:04 +02001389 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301390
1391 /*
1392 * Check if any of the non-wakeup interrupt GPIOs have changed
1393 * state. If so, generate an IRQ by software. This is
1394 * horribly racy, but it's the best we can do to work around
1395 * this silicon bug.
1396 */
1397 l ^= bank->saved_datain;
1398 l &= bank->enabled_non_wakeup_gpios;
1399
1400 /*
1401 * No need to generate IRQs for the rising edge for gpio IRQs
1402 * configured with falling edge only; and vice versa.
1403 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301404 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301405 gen0 &= bank->saved_datain;
1406
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301407 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301408 gen1 &= ~(bank->saved_datain);
1409
1410 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301411 gen = l & (~(bank->context.fallingdetect) &
1412 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301413 /* Consider all GPIO IRQs needed to be updated */
1414 gen |= gen0 | gen1;
1415
1416 if (gen) {
1417 u32 old0, old1;
1418
Victor Kamensky661553b2013-11-16 02:01:04 +02001419 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1420 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301421
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301422 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001423 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301424 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001425 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301426 bank->regs->leveldetect1);
1427 }
1428
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301429 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001430 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301431 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001432 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301433 bank->regs->leveldetect1);
1434 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001435 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1436 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301437 }
1438
1439 bank->workaround_enabled = false;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001440 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301441
1442 return 0;
1443}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001444#endif /* CONFIG_PM */
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301445
Tony Lindgrencac089f2015-04-23 16:56:22 -07001446#if IS_BUILTIN(CONFIG_GPIO_OMAP)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301447void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001448{
Charulatha V03e128c2011-05-05 19:58:01 +05301449 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001450
Charulatha V03e128c2011-05-05 19:58:01 +05301451 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001452 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301453 continue;
1454
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301455 bank->power_mode = pwr_mode;
1456
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301457 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001458 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001459}
1460
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001461void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001462{
Charulatha V03e128c2011-05-05 19:58:01 +05301463 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001464
Charulatha V03e128c2011-05-05 19:58:01 +05301465 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001466 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301467 continue;
1468
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301469 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001470 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001471}
Tony Lindgrencac089f2015-04-23 16:56:22 -07001472#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001473
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001474#if defined(CONFIG_PM)
Jon Hunter352a2d52013-04-15 13:06:54 -05001475static void omap_gpio_init_context(struct gpio_bank *p)
1476{
1477 struct omap_gpio_reg_offs *regs = p->regs;
1478 void __iomem *base = p->base;
1479
Victor Kamensky661553b2013-11-16 02:01:04 +02001480 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1481 p->context.oe = readl_relaxed(base + regs->direction);
1482 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1483 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1484 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1485 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1486 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1487 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1488 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001489
1490 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001491 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001492 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001493 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001494
1495 p->context_valid = true;
1496}
1497
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301498static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301499{
Victor Kamensky661553b2013-11-16 02:01:04 +02001500 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301501 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001502 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1503 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301504 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001505 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301506 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001507 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301508 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001509 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301510 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301511 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001512 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301513 bank->base + bank->regs->set_dataout);
1514 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001515 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301516 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001517 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301518
Nishanth Menonae547352011-09-09 19:08:58 +05301519 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001520 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301521 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001522 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301523 bank->base + bank->regs->debounce_en);
1524 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301525
Victor Kamensky661553b2013-11-16 02:01:04 +02001526 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301527 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001528 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301529 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301530}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001531#endif /* CONFIG_PM */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301532#else
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301533#define omap_gpio_runtime_suspend NULL
1534#define omap_gpio_runtime_resume NULL
Arnd Bergmannea4a21a2013-05-31 17:59:46 +02001535static inline void omap_gpio_init_context(struct gpio_bank *p) {}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301536#endif
1537
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301538static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301539 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1540 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301541};
1542
Benoit Cousson384ebe12011-08-16 11:53:02 +02001543#if defined(CONFIG_OF)
1544static struct omap_gpio_reg_offs omap2_gpio_regs = {
1545 .revision = OMAP24XX_GPIO_REVISION,
1546 .direction = OMAP24XX_GPIO_OE,
1547 .datain = OMAP24XX_GPIO_DATAIN,
1548 .dataout = OMAP24XX_GPIO_DATAOUT,
1549 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1550 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1551 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1552 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1553 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1554 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1555 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1556 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1557 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1558 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1559 .ctrl = OMAP24XX_GPIO_CTRL,
1560 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1561 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1562 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1563 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1564 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1565};
1566
1567static struct omap_gpio_reg_offs omap4_gpio_regs = {
1568 .revision = OMAP4_GPIO_REVISION,
1569 .direction = OMAP4_GPIO_OE,
1570 .datain = OMAP4_GPIO_DATAIN,
1571 .dataout = OMAP4_GPIO_DATAOUT,
1572 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1573 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1574 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1575 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1576 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1577 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1578 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1579 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1580 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1581 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1582 .ctrl = OMAP4_GPIO_CTRL,
1583 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1584 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1585 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1586 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1587 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1588};
1589
Chen Gange9a65bb2013-02-06 18:44:32 +08001590static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001591 .regs = &omap2_gpio_regs,
1592 .bank_width = 32,
1593 .dbck_flag = false,
1594};
1595
Chen Gange9a65bb2013-02-06 18:44:32 +08001596static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001597 .regs = &omap2_gpio_regs,
1598 .bank_width = 32,
1599 .dbck_flag = true,
1600};
1601
Chen Gange9a65bb2013-02-06 18:44:32 +08001602static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001603 .regs = &omap4_gpio_regs,
1604 .bank_width = 32,
1605 .dbck_flag = true,
1606};
1607
1608static const struct of_device_id omap_gpio_match[] = {
1609 {
1610 .compatible = "ti,omap4-gpio",
1611 .data = &omap4_pdata,
1612 },
1613 {
1614 .compatible = "ti,omap3-gpio",
1615 .data = &omap3_pdata,
1616 },
1617 {
1618 .compatible = "ti,omap2-gpio",
1619 .data = &omap2_pdata,
1620 },
1621 { },
1622};
1623MODULE_DEVICE_TABLE(of, omap_gpio_match);
1624#endif
1625
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001626static struct platform_driver omap_gpio_driver = {
1627 .probe = omap_gpio_probe,
Tony Lindgrencac089f2015-04-23 16:56:22 -07001628 .remove = omap_gpio_remove,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001629 .driver = {
1630 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301631 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001632 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001633 },
1634};
1635
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001636/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001637 * gpio driver register needs to be done before
1638 * machine_init functions access gpio APIs.
1639 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001640 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001641static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001642{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001643 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001644}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001645postcore_initcall(omap_gpio_drv_reg);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001646
1647static void __exit omap_gpio_exit(void)
1648{
1649 platform_driver_unregister(&omap_gpio_driver);
1650}
1651module_exit(omap_gpio_exit);
1652
1653MODULE_DESCRIPTION("omap gpio driver");
1654MODULE_ALIAS("platform:gpio-omap");
1655MODULE_LICENSE("GPL v2");