blob: 2df693bb2693272f6d5bcd34294b17a8d1baced4 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Benoit Cousson96751fc2012-02-01 16:01:39 +010022#include <linux/device.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080023#include <linux/pm_runtime.h>
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +053024#include <linux/pm.h>
Benoit Cousson384ebe12011-08-16 11:53:02 +020025#include <linux/of.h>
26#include <linux/of_device.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070027#include <linux/gpio.h>
Yegor Yefremov93700842014-04-24 08:57:39 +020028#include <linux/bitops.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070029#include <linux/platform_data/gpio-omap.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010030
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053031#define OFF_MODE 1
32
Charulatha V03e128c2011-05-05 19:58:01 +053033static LIST_HEAD(omap_gpio_list);
34
Charulatha V6d62e212011-04-18 15:06:51 +000035struct gpio_regs {
36 u32 irqenable1;
37 u32 irqenable2;
38 u32 wake_en;
39 u32 ctrl;
40 u32 oe;
41 u32 leveldetect0;
42 u32 leveldetect1;
43 u32 risingdetect;
44 u32 fallingdetect;
45 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053046 u32 debounce;
47 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000048};
49
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010050struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053051 struct list_head node;
Tony Lindgren92105bb2005-09-07 17:20:26 +010052 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010053 u16 irq;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080054 u32 non_wakeup_gpios;
55 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000056 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080057 u32 saved_datain;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080058 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080059 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010060 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -080061 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080062 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080063 u32 mod_usage;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020064 u32 irq_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080065 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +053066 bool dbck_enabled;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080067 struct device *dev;
Charulatha Vd0d665a2011-08-31 00:02:21 +053068 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080069 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053070 bool loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -050071 bool context_valid;
Tony Lindgren5de62b82010-12-07 16:26:58 -080072 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070073 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053074 int context_loss_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053075 int power_mode;
76 bool workaround_enabled;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070077
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
Kevin Hilman129fd222011-04-22 07:59:07 -070084#define GPIO_INDEX(bank, gpio) (gpio % bank->width)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020085#define GPIO_BIT(bank, gpio) (BIT(GPIO_INDEX(bank, gpio)))
Charulatha Vc8eef652011-05-02 15:21:42 +053086#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010087
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020088#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020089#define LINE_USED(line, offset) (line & (BIT(offset)))
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020090
Tony Lindgren3d009c82015-01-16 14:50:50 -080091static void omap_gpio_unmask_irq(struct irq_data *d);
92
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020093static int omap_irq_to_gpio(struct gpio_bank *bank, unsigned int gpio_irq)
Benoit Cousson25db7112012-02-23 21:50:10 +010094{
Jon Hunterede4d7a2013-03-01 11:22:47 -060095 return bank->chip.base + gpio_irq;
96}
97
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020098static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -060099{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200100 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
101 return container_of(chip, struct gpio_bank, chip);
Benoit Cousson25db7112012-02-23 21:50:10 +0100102}
103
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200104static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
105 int is_input)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100106{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100107 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100108 u32 l;
109
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700110 reg += bank->regs->direction;
Victor Kamensky661553b2013-11-16 02:01:04 +0200111 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100112 if (is_input)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200113 l |= BIT(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100114 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200115 l &= ~(BIT(gpio));
Victor Kamensky661553b2013-11-16 02:01:04 +0200116 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530117 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100118}
119
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700120
121/* set data out value using dedicate set/clear register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200122static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200123 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100124{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100125 void __iomem *reg = bank->base;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200126 u32 l = BIT(offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100127
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530128 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700129 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530130 bank->context.dataout |= l;
131 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700132 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530133 bank->context.dataout &= ~l;
134 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700135
Victor Kamensky661553b2013-11-16 02:01:04 +0200136 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700137}
138
139/* set data out value using mask register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200140static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200141 int enable)
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700142{
143 void __iomem *reg = bank->base + bank->regs->dataout;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200144 u32 gpio_bit = BIT(offset);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700145 u32 l;
146
Victor Kamensky661553b2013-11-16 02:01:04 +0200147 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700148 if (enable)
149 l |= gpio_bit;
150 else
151 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200152 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530153 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100154}
155
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200156static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100157{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700158 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100159
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200160 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100161}
162
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200163static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300164{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700165 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300166
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200167 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300168}
169
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200170static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700171{
Victor Kamensky661553b2013-11-16 02:01:04 +0200172 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700173
Benoit Cousson862ff642012-02-01 15:58:56 +0100174 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700175 l |= mask;
176 else
177 l &= ~mask;
178
Victor Kamensky661553b2013-11-16 02:01:04 +0200179 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700180}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100181
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200182static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530183{
184 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Rajendra Nayak345477f2014-04-23 11:41:03 +0530185 clk_prepare_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530186 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300187
Victor Kamensky661553b2013-11-16 02:01:04 +0200188 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300189 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530190 }
191}
192
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200193static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530194{
195 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300196 /*
197 * Disable debounce before cutting it's clock. If debounce is
198 * enabled but the clock is not, GPIO module seems to be unable
199 * to detect events and generate interrupts at least on OMAP3.
200 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200201 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300202
Rajendra Nayak345477f2014-04-23 11:41:03 +0530203 clk_disable_unprepare(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530204 bank->dbck_enabled = false;
205 }
206}
207
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700208/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200209 * omap2_set_gpio_debounce - low level gpio debounce time
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700210 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200211 * @offset: the gpio number on this @bank
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700212 * @debounce: debounce time to use
213 *
214 * OMAP's debounce time is in 31us steps so we need
215 * to convert and round up to the closest unit.
216 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200217static void omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200218 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700219{
Kevin Hilman9942da02011-04-22 12:02:05 -0700220 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700221 u32 val;
222 u32 l;
223
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800224 if (!bank->dbck_flag)
225 return;
226
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700227 if (debounce < 32)
228 debounce = 0x01;
229 else if (debounce > 7936)
230 debounce = 0xff;
231 else
232 debounce = (debounce / 0x1f) - 1;
233
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200234 l = BIT(offset);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700235
Rajendra Nayak345477f2014-04-23 11:41:03 +0530236 clk_prepare_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700237 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200238 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700239
Kevin Hilman9942da02011-04-22 12:02:05 -0700240 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200241 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700242
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530243 if (debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700244 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530245 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700246 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300247 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700248
Victor Kamensky661553b2013-11-16 02:01:04 +0200249 writel_relaxed(val, reg);
Rajendra Nayak345477f2014-04-23 11:41:03 +0530250 clk_disable_unprepare(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530251 /*
252 * Enable debounce clock per module.
253 * This call is mandatory because in omap_gpio_request() when
254 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
255 * runtime callbck fails to turn on dbck because dbck_enable_mask
256 * used within _gpio_dbck_enable() is still not initialized at
257 * that point. Therefore we have to enable dbck here.
258 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200259 omap_gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530260 if (bank->dbck_enable_mask) {
261 bank->context.debounce = debounce;
262 bank->context.debounce_en = val;
263 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700264}
265
Jon Hunterc9c55d92012-10-26 14:26:04 -0500266/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200267 * omap_clear_gpio_debounce - clear debounce settings for a gpio
Jon Hunterc9c55d92012-10-26 14:26:04 -0500268 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200269 * @offset: the gpio number on this @bank
Jon Hunterc9c55d92012-10-26 14:26:04 -0500270 *
271 * If a gpio is using debounce, then clear the debounce enable bit and if
272 * this is the only gpio in this bank using debounce, then clear the debounce
273 * time too. The debounce clock will also be disabled when calling this function
274 * if this is the only gpio in the bank using debounce.
275 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200276static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
Jon Hunterc9c55d92012-10-26 14:26:04 -0500277{
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200278 u32 gpio_bit = BIT(offset);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500279
280 if (!bank->dbck_flag)
281 return;
282
283 if (!(bank->dbck_enable_mask & gpio_bit))
284 return;
285
286 bank->dbck_enable_mask &= ~gpio_bit;
287 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200288 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500289 bank->base + bank->regs->debounce_en);
290
291 if (!bank->dbck_enable_mask) {
292 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200293 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500294 bank->regs->debounce);
Rajendra Nayak345477f2014-04-23 11:41:03 +0530295 clk_disable_unprepare(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500296 bank->dbck_enabled = false;
297 }
298}
299
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200300static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530301 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100302{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800303 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200304 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100305
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200306 omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
307 trigger & IRQ_TYPE_LEVEL_LOW);
308 omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
309 trigger & IRQ_TYPE_LEVEL_HIGH);
310 omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
311 trigger & IRQ_TYPE_EDGE_RISING);
312 omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
313 trigger & IRQ_TYPE_EDGE_FALLING);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530314
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530315 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200316 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530317 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200318 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530319 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200320 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530321 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200322 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530323
324 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200325 omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530326 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200327 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530328 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530329
Ambresh K55b220c2011-06-15 13:40:45 -0700330 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530331 if (!bank->regs->irqctrl) {
332 /* On omap24xx proceed only when valid GPIO bit is set */
333 if (bank->non_wakeup_gpios) {
334 if (!(bank->non_wakeup_gpios & gpio_bit))
335 goto exit;
336 }
337
Chunqiu Wang699117a62009-06-24 17:13:39 +0000338 /*
339 * Log the edge gpio and manually trigger the IRQ
340 * after resume if the input level changes
341 * to avoid irq lost during PER RET/OFF mode
342 * Applies for omap2 non-wakeup gpio and all omap3 gpios
343 */
344 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800345 bank->enabled_non_wakeup_gpios |= gpio_bit;
346 else
347 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
348 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700349
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530350exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530351 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200352 readl_relaxed(bank->base + bank->regs->leveldetect0) |
353 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100354}
355
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800356#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800357/*
358 * This only applies to chips that can't do both rising and falling edge
359 * detection at once. For all other chips, this function is a noop.
360 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200361static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800362{
363 void __iomem *reg = bank->base;
364 u32 l = 0;
365
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530366 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800367 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530368
369 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800370
Victor Kamensky661553b2013-11-16 02:01:04 +0200371 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800372 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200373 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800374 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200375 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800376
Victor Kamensky661553b2013-11-16 02:01:04 +0200377 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800378}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530379#else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200380static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800381#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800382
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200383static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
384 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100385{
386 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530387 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100388 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100389
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530390 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200391 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530392 } else if (bank->regs->irqctrl) {
393 reg += bank->regs->irqctrl;
394
Victor Kamensky661553b2013-11-16 02:01:04 +0200395 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000396 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200397 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100398 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200399 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100400 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200401 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100402 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530403 return -EINVAL;
404
Victor Kamensky661553b2013-11-16 02:01:04 +0200405 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530406 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100407 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530408 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100409 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530410 reg += bank->regs->edgectrl1;
411
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100412 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200413 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100414 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100415 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100416 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100417 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200418 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530419
420 /* Enable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200421 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530422 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200423 readl_relaxed(bank->base + bank->regs->wkup_en);
424 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100425 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100426 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100427}
428
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200429static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200430{
431 if (bank->regs->pinctrl) {
432 void __iomem *reg = bank->base + bank->regs->pinctrl;
433
434 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200435 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200436 }
437
438 if (bank->regs->ctrl && !BANK_USED(bank)) {
439 void __iomem *reg = bank->base + bank->regs->ctrl;
440 u32 ctrl;
441
Victor Kamensky661553b2013-11-16 02:01:04 +0200442 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200443 /* Module is enabled, clocks are not gated */
444 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200445 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200446 bank->context.ctrl = ctrl;
447 }
448}
449
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200450static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200451{
452 void __iomem *base = bank->base;
453
454 if (bank->regs->wkup_en &&
455 !LINE_USED(bank->mod_usage, offset) &&
456 !LINE_USED(bank->irq_usage, offset)) {
457 /* Disable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200458 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200459 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200460 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200461 }
462
463 if (bank->regs->ctrl && !BANK_USED(bank)) {
464 void __iomem *reg = bank->base + bank->regs->ctrl;
465 u32 ctrl;
466
Victor Kamensky661553b2013-11-16 02:01:04 +0200467 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200468 /* Module is disabled, clocks are gated */
469 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200470 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200471 bank->context.ctrl = ctrl;
472 }
473}
474
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200475static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200476{
477 void __iomem *reg = bank->base + bank->regs->direction;
478
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200479 return readl_relaxed(reg) & BIT(offset);
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200480}
481
Tony Lindgren3d009c82015-01-16 14:50:50 -0800482static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned gpio,
483 unsigned offset)
484{
485 if (!LINE_USED(bank->mod_usage, offset)) {
486 omap_enable_gpio_module(bank, offset);
487 omap_set_gpio_direction(bank, offset, 1);
488 }
489 bank->irq_usage |= BIT(GPIO_INDEX(bank, gpio));
490}
491
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200492static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100493{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200494 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren4b254082012-08-30 15:37:24 -0700495 unsigned gpio = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100496 int retval;
David Brownella6472532008-03-03 04:33:30 -0800497 unsigned long flags;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200498 unsigned offset;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100499
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200500 if (!BANK_USED(bank))
501 pm_runtime_get_sync(bank->dev);
Jon Hunter8d4c2772013-03-01 11:22:48 -0600502
Tony Lindgren4b254082012-08-30 15:37:24 -0700503#ifdef CONFIG_ARCH_OMAP1
504 if (d->irq > IH_MPUIO_BASE)
Lennert Buytenheke9191022010-11-29 11:17:17 +0100505 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren4b254082012-08-30 15:37:24 -0700506#endif
507
508 if (!gpio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200509 gpio = omap_irq_to_gpio(bank, d->hwirq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510
David Brownelle5c56ed2006-12-06 17:13:59 -0800511 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100512 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800513
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530514 if (!bank->regs->leveldetect0 &&
515 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100516 return -EINVAL;
517
David Brownella6472532008-03-03 04:33:30 -0800518 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200519 offset = GPIO_INDEX(bank, gpio);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200520 retval = omap_set_gpio_triggering(bank, offset, type);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800521 omap_gpio_init_irq(bank, gpio, offset);
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200522 if (!omap_gpio_is_input(bank, offset)) {
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200523 spin_unlock_irqrestore(&bank->lock, flags);
524 return -EINVAL;
525 }
David Brownella6472532008-03-03 04:33:30 -0800526 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800527
528 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100529 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800530 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100531 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800532
Tony Lindgren92105bb2005-09-07 17:20:26 +0100533 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100534}
535
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200536static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100537{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100538 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100539
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700540 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200541 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300542
543 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700544 if (bank->regs->irqstatus2) {
545 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200546 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700547 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700548
549 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200550 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100551}
552
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200553static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100554{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200555 omap_clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100556}
557
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200558static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
Imre Deakea6dedd2006-06-26 16:16:00 -0700559{
560 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700561 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200562 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700563
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700564 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200565 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700566 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700567 l = ~l;
568 l &= mask;
569 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700570}
571
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200572static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100574 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100575 u32 l;
576
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700577 if (bank->regs->set_irqenable) {
578 reg += bank->regs->set_irqenable;
579 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530580 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700581 } else {
582 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200583 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700584 if (bank->regs->irqenable_inv)
585 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100586 else
587 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530588 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100589 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700590
Victor Kamensky661553b2013-11-16 02:01:04 +0200591 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700592}
593
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200594static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700595{
596 void __iomem *reg = bank->base;
597 u32 l;
598
599 if (bank->regs->clr_irqenable) {
600 reg += bank->regs->clr_irqenable;
601 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530602 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700603 } else {
604 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200605 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700606 if (bank->regs->irqenable_inv)
607 l |= gpio_mask;
608 else
609 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530610 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700611 }
612
Victor Kamensky661553b2013-11-16 02:01:04 +0200613 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100614}
615
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200616static inline void omap_set_gpio_irqenable(struct gpio_bank *bank, int gpio,
617 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100618{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530619 if (enable)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200620 omap_enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530621 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200622 omap_disable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100623}
624
Tony Lindgren92105bb2005-09-07 17:20:26 +0100625/*
626 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
627 * 1510 does not seem to have a wake-up register. If JTAG is connected
628 * to the target, system will wake up always on GPIO events. While
629 * system is running all registered GPIO interrupts need to have wake-up
630 * enabled. When system is suspended, only selected GPIO interrupts need
631 * to have wake-up enabled.
632 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200633static int omap_set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100634{
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700635 u32 gpio_bit = GPIO_BIT(bank, gpio);
636 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800637
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700638 if (bank->non_wakeup_gpios & gpio_bit) {
Benoit Cousson862ff642012-02-01 15:58:56 +0100639 dev_err(bank->dev,
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700640 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100641 return -EINVAL;
642 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700643
644 spin_lock_irqsave(&bank->lock, flags);
645 if (enable)
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530646 bank->context.wake_en |= gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700647 else
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530648 bank->context.wake_en &= ~gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700649
Victor Kamensky661553b2013-11-16 02:01:04 +0200650 writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700651 spin_unlock_irqrestore(&bank->lock, flags);
652
653 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100654}
655
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200656static void omap_reset_gpio(struct gpio_bank *bank, int gpio)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300657{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200658 omap_set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
659 omap_set_gpio_irqenable(bank, gpio, 0);
660 omap_clear_gpio_irqstatus(bank, gpio);
661 omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200662 omap_clear_gpio_debounce(bank, GPIO_INDEX(bank, gpio));
Tony Lindgren4196dd62006-09-25 12:41:38 +0300663}
664
Tony Lindgren92105bb2005-09-07 17:20:26 +0100665/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200666static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100667{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200668 struct gpio_bank *bank = omap_irq_data_get_bank(d);
669 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100670
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200671 return omap_set_gpio_wakeup(bank, gpio, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100672}
673
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800674static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800676 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800677 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100678
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530679 /*
680 * If this is the first gpio_request for the bank,
681 * enable the bank module.
682 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200683 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530684 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100685
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530686 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300687 /* Set trigger to none. You need to enable the desired trigger with
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200688 * request_irq() or set_irq_type(). Only do this if the IRQ line has
689 * not already been requested.
Tony Lindgren4196dd62006-09-25 12:41:38 +0300690 */
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200691 if (!LINE_USED(bank->irq_usage, offset)) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200692 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
693 omap_enable_gpio_module(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100694 }
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200695 bank->mod_usage |= BIT(offset);
David Brownella6472532008-03-03 04:33:30 -0800696 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100697
698 return 0;
699}
700
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800701static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800703 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800704 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100705
David Brownella6472532008-03-03 04:33:30 -0800706 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200707 bank->mod_usage &= ~(BIT(offset));
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200708 omap_disable_gpio_module(bank, offset);
709 omap_reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -0800710 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530711
712 /*
713 * If this is the last gpio to be freed in the bank,
714 * disable the bank module.
715 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200716 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530717 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718}
719
720/*
721 * We need to unmask the GPIO bank interrupt as soon as possible to
722 * avoid missing GPIO interrupts for other lines in the bank.
723 * Then we need to mask-read-clear-unmask the triggered GPIO lines
724 * in the bank to avoid missing nested interrupts for a GPIO line.
725 * If we wait to unmask individual GPIO lines in the bank after the
726 * line's interrupt handler has been run, we may miss some nested
727 * interrupts.
728 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200729static void omap_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100731 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732 u32 isr;
Jon Hunter3513cde2013-04-04 15:16:14 -0500733 unsigned int bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100734 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700735 int unmasked = 0;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200736 struct irq_chip *irqchip = irq_desc_get_chip(desc);
737 struct gpio_chip *chip = irq_get_handler_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100738
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200739 chained_irq_enter(irqchip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200741 bank = container_of(chip, struct gpio_bank, chip);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700742 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530743 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800744
745 if (WARN_ON(!isr_reg))
746 goto exit;
747
Laurent Navete83507b2013-03-20 13:15:57 +0100748 while (1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100749 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700750 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100751
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200752 enabled = omap_get_gpio_irqbank_mask(bank);
Victor Kamensky661553b2013-11-16 02:01:04 +0200753 isr_saved = isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100754
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530755 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800756 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100757
758 /* clear edge sensitive interrupts before handler(s) are
759 called so that we don't miss any interrupt occurred while
760 executing them */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200761 omap_disable_gpio_irqbank(bank, isr_saved & ~level_mask);
762 omap_clear_gpio_irqbank(bank, isr_saved & ~level_mask);
763 omap_enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100764
765 /* if there is only edge sensitive GPIO pin interrupts
766 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700767 if (!level_mask && !unmasked) {
768 unmasked = 1;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200769 chained_irq_exit(irqchip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700770 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100771
Tony Lindgren92105bb2005-09-07 17:20:26 +0100772 if (!isr)
773 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100774
Jon Hunter3513cde2013-04-04 15:16:14 -0500775 while (isr) {
776 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200777 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100778
Cory Maccarrone4318f362010-01-08 10:29:04 -0800779 /*
780 * Some chips can't respond to both rising and falling
781 * at the same time. If this irq was requested with
782 * both flags, we need to flip the ICR data for the IRQ
783 * to respond to the IRQ for the opposite direction.
784 * This will be indicated in the bank toggle_mask.
785 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200786 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200787 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800788
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200789 generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
790 bit));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100791 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000792 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700793 /* if bank has any level sensitive GPIO pin interrupt
794 configured, we must unmask the bank interrupt only after
795 handler(s) are executed in order to avoid spurious bank
796 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800797exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700798 if (!unmasked)
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200799 chained_irq_exit(irqchip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530800 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100801}
802
Tony Lindgren3d009c82015-01-16 14:50:50 -0800803static unsigned int omap_gpio_irq_startup(struct irq_data *d)
804{
805 struct gpio_bank *bank = omap_irq_data_get_bank(d);
806 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
807 unsigned long flags;
808 unsigned offset = GPIO_INDEX(bank, gpio);
809
810 if (!BANK_USED(bank))
811 pm_runtime_get_sync(bank->dev);
812
813 spin_lock_irqsave(&bank->lock, flags);
814 omap_gpio_init_irq(bank, gpio, offset);
815 spin_unlock_irqrestore(&bank->lock, flags);
816 omap_gpio_unmask_irq(d);
817
818 return 0;
819}
820
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200821static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300822{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200823 struct gpio_bank *bank = omap_irq_data_get_bank(d);
824 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700825 unsigned long flags;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200826 unsigned offset = GPIO_INDEX(bank, gpio);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300827
Colin Cross85ec7b92011-06-06 13:38:18 -0700828 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200829 bank->irq_usage &= ~(BIT(offset));
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200830 omap_disable_gpio_module(bank, offset);
831 omap_reset_gpio(bank, gpio);
Colin Cross85ec7b92011-06-06 13:38:18 -0700832 spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200833
834 /*
835 * If this is the last IRQ to be freed in the bank,
836 * disable the bank module.
837 */
838 if (!BANK_USED(bank))
839 pm_runtime_put(bank->dev);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300840}
841
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200842static void omap_gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100843{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200844 struct gpio_bank *bank = omap_irq_data_get_bank(d);
845 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100846
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200847 omap_clear_gpio_irqstatus(bank, gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100848}
849
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200850static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100851{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200852 struct gpio_bank *bank = omap_irq_data_get_bank(d);
853 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Colin Cross85ec7b92011-06-06 13:38:18 -0700854 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100855
Colin Cross85ec7b92011-06-06 13:38:18 -0700856 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200857 omap_set_gpio_irqenable(bank, gpio, 0);
858 omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
Colin Cross85ec7b92011-06-06 13:38:18 -0700859 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100860}
861
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200862static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100863{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200864 struct gpio_bank *bank = omap_irq_data_get_bank(d);
865 unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
Kevin Hilman129fd222011-04-22 07:59:07 -0700866 unsigned int irq_mask = GPIO_BIT(bank, gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100867 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700868 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700869
Colin Cross85ec7b92011-06-06 13:38:18 -0700870 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700871 if (trigger)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200872 omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800873
874 /* For level-triggered GPIOs, the clearing must be done after
875 * the HW source is cleared, thus after the handler has run */
876 if (bank->level_mask & irq_mask) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200877 omap_set_gpio_irqenable(bank, gpio, 0);
878 omap_clear_gpio_irqstatus(bank, gpio);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800879 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100880
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200881 omap_set_gpio_irqenable(bank, gpio, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700882 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100883}
884
David Brownelle5c56ed2006-12-06 17:13:59 -0800885/*---------------------------------------------------------------------*/
886
Magnus Damm79ee0312009-07-08 13:22:04 +0200887static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800888{
Magnus Damm79ee0312009-07-08 13:22:04 +0200889 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800890 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800891 void __iomem *mask_reg = bank->base +
892 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800893 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800894
David Brownella6472532008-03-03 04:33:30 -0800895 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200896 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800897 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800898
899 return 0;
900}
901
Magnus Damm79ee0312009-07-08 13:22:04 +0200902static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800903{
Magnus Damm79ee0312009-07-08 13:22:04 +0200904 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800905 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800906 void __iomem *mask_reg = bank->base +
907 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800908 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800909
David Brownella6472532008-03-03 04:33:30 -0800910 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200911 writel_relaxed(bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800912 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800913
914 return 0;
915}
916
Alexey Dobriyan47145212009-12-14 18:00:08 -0800917static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200918 .suspend_noirq = omap_mpuio_suspend_noirq,
919 .resume_noirq = omap_mpuio_resume_noirq,
920};
921
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200922/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800923static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800924 .driver = {
925 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200926 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800927 },
928};
929
930static struct platform_device omap_mpuio_device = {
931 .name = "mpuio",
932 .id = -1,
933 .dev = {
934 .driver = &omap_mpuio_driver.driver,
935 }
936 /* could list the /proc/iomem resources */
937};
938
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200939static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800940{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800941 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700942
David Brownell11a78b72006-12-06 17:14:11 -0800943 if (platform_driver_register(&omap_mpuio_driver) == 0)
944 (void) platform_device_register(&omap_mpuio_device);
945}
946
David Brownelle5c56ed2006-12-06 17:13:59 -0800947/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100948
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200949static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200950{
951 struct gpio_bank *bank;
952 unsigned long flags;
953 void __iomem *reg;
954 int dir;
955
956 bank = container_of(chip, struct gpio_bank, chip);
957 reg = bank->base + bank->regs->direction;
958 spin_lock_irqsave(&bank->lock, flags);
959 dir = !!(readl_relaxed(reg) & BIT(offset));
960 spin_unlock_irqrestore(&bank->lock, flags);
961 return dir;
962}
963
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200964static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800965{
966 struct gpio_bank *bank;
967 unsigned long flags;
968
969 bank = container_of(chip, struct gpio_bank, chip);
970 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200971 omap_set_gpio_direction(bank, offset, 1);
David Brownell52e31342008-03-03 12:43:23 -0800972 spin_unlock_irqrestore(&bank->lock, flags);
973 return 0;
974}
975
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200976static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800977{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300978 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300979
Charulatha Va8be8da2011-04-22 16:38:16 +0530980 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300981
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200982 if (omap_gpio_is_input(bank, offset))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200983 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300984 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200985 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -0800986}
987
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200988static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800989{
990 struct gpio_bank *bank;
991 unsigned long flags;
992
993 bank = container_of(chip, struct gpio_bank, chip);
994 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700995 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200996 omap_set_gpio_direction(bank, offset, 0);
David Brownell52e31342008-03-03 12:43:23 -0800997 spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200998 return 0;
David Brownell52e31342008-03-03 12:43:23 -0800999}
1000
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001001static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
1002 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001003{
1004 struct gpio_bank *bank;
1005 unsigned long flags;
1006
1007 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001008
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001009 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001010 omap2_set_gpio_debounce(bank, offset, debounce);
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001011 spin_unlock_irqrestore(&bank->lock, flags);
1012
1013 return 0;
1014}
1015
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001016static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001017{
1018 struct gpio_bank *bank;
1019 unsigned long flags;
1020
1021 bank = container_of(chip, struct gpio_bank, chip);
1022 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001023 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001024 spin_unlock_irqrestore(&bank->lock, flags);
1025}
1026
1027/*---------------------------------------------------------------------*/
1028
Tony Lindgren9a748052010-12-07 16:26:56 -08001029static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001030{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001031 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001032 u32 rev;
1033
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001034 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001035 return;
1036
Victor Kamensky661553b2013-11-16 02:01:04 +02001037 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001038 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001039 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001040
1041 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001042}
1043
Charulatha V03e128c2011-05-05 19:58:01 +05301044static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001045{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301046 void __iomem *base = bank->base;
1047 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001048
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301049 if (bank->width == 16)
1050 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001051
Charulatha Vd0d665a2011-08-31 00:02:21 +05301052 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001053 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301054 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001055 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301056
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001057 omap_gpio_rmw(base, bank->regs->irqenable, l,
1058 bank->regs->irqenable_inv);
1059 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1060 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301061 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001062 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301063
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301064 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001065 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301066 /* Initialize interface clk ungated, module enabled */
1067 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001068 writel_relaxed(0, base + bank->regs->ctrl);
Tarun Kanti DebBarma34672012012-07-11 14:43:14 +05301069
1070 bank->dbck = clk_get(bank->dev, "dbclk");
1071 if (IS_ERR(bank->dbck))
1072 dev_err(bank->dev, "Could not get gpio dbck\n");
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001073}
1074
Bill Pemberton38363092012-11-19 13:22:34 -05001075static void
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001076omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1077 unsigned int num)
1078{
1079 struct irq_chip_generic *gc;
1080 struct irq_chip_type *ct;
1081
1082 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1083 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -07001084 if (!gc) {
1085 dev_err(bank->dev, "Memory alloc failed for gc\n");
1086 return;
1087 }
1088
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001089 ct = gc->chip_types;
1090
1091 /* NOTE: No ack required, reading IRQ status clears it. */
1092 ct->chip.irq_mask = irq_gc_mask_set_bit;
1093 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001094 ct->chip.irq_set_type = omap_gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301095
1096 if (bank->regs->wkup_en)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001097 ct->chip.irq_set_wake = omap_gpio_wake_enable;
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001098
1099 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1100 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1101 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1102}
1103
Nishanth Menon46824e22014-09-05 14:52:55 -05001104static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001105{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001106 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001107 static int gpio;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001108 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001109 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001110
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001111 /*
1112 * REVISIT eventually switch from OMAP-specific gpio structs
1113 * over to the generic ones
1114 */
1115 bank->chip.request = omap_gpio_request;
1116 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001117 bank->chip.get_direction = omap_gpio_get_direction;
1118 bank->chip.direction_input = omap_gpio_input;
1119 bank->chip.get = omap_gpio_get;
1120 bank->chip.direction_output = omap_gpio_output;
1121 bank->chip.set_debounce = omap_gpio_debounce;
1122 bank->chip.set = omap_gpio_set;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301123 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001124 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301125 if (bank->regs->wkup_en)
1126 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001127 bank->chip.base = OMAP_MPUIO(0);
1128 } else {
1129 bank->chip.label = "gpio";
1130 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001131 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001132 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001133 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001134
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001135 ret = gpiochip_add(&bank->chip);
1136 if (ret) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001137 dev_err(bank->dev, "Could not register gpio chip %d\n", ret);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001138 return ret;
1139 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001140
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001141#ifdef CONFIG_ARCH_OMAP1
1142 /*
1143 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1144 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1145 */
1146 irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
1147 if (irq_base < 0) {
1148 dev_err(bank->dev, "Couldn't allocate IRQ numbers\n");
1149 return -ENODEV;
1150 }
1151#endif
1152
Nishanth Menon46824e22014-09-05 14:52:55 -05001153 ret = gpiochip_irqchip_add(&bank->chip, irqc,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001154 irq_base, omap_gpio_irq_handler,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001155 IRQ_TYPE_NONE);
1156
1157 if (ret) {
1158 dev_err(bank->dev, "Couldn't add irqchip to gpiochip %d\n", ret);
Linus Walleijda26d5d2014-09-16 15:11:41 -07001159 gpiochip_remove(&bank->chip);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001160 return -ENODEV;
1161 }
1162
Nishanth Menon46824e22014-09-05 14:52:55 -05001163 gpiochip_set_chained_irqchip(&bank->chip, irqc,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001164 bank->irq, omap_gpio_irq_handler);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001165
Jon Hunterede4d7a2013-03-01 11:22:47 -06001166 for (j = 0; j < bank->width; j++) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001167 int irq = irq_find_mapping(bank->chip.irqdomain, j);
Charulatha Vd0d665a2011-08-31 00:02:21 +05301168 if (bank->is_mpuio) {
Jon Hunterede4d7a2013-03-01 11:22:47 -06001169 omap_mpuio_alloc_gc(bank, irq, bank->width);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001170 irq_set_chip_and_handler(irq, NULL, NULL);
1171 set_irq_flags(irq, 0);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001172 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001173 }
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001174
1175 return 0;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001176}
1177
Benoit Cousson384ebe12011-08-16 11:53:02 +02001178static const struct of_device_id omap_gpio_match[];
1179
Bill Pemberton38363092012-11-19 13:22:34 -05001180static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001181{
Benoit Cousson862ff642012-02-01 15:58:56 +01001182 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001183 struct device_node *node = dev->of_node;
1184 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001185 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001186 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001187 struct gpio_bank *bank;
Nishanth Menon46824e22014-09-05 14:52:55 -05001188 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001189 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001190
Benoit Cousson384ebe12011-08-16 11:53:02 +02001191 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1192
Jingoo Hane56aee12013-07-30 17:08:05 +09001193 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001194 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001195 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001196
Tobias Klauser086d5852012-10-05 11:37:38 +02001197 bank = devm_kzalloc(dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301198 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001199 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001200 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301201 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001202
Nishanth Menon46824e22014-09-05 14:52:55 -05001203 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1204 if (!irqc)
1205 return -ENOMEM;
1206
Tony Lindgren3d009c82015-01-16 14:50:50 -08001207 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e22014-09-05 14:52:55 -05001208 irqc->irq_shutdown = omap_gpio_irq_shutdown,
1209 irqc->irq_ack = omap_gpio_ack_irq,
1210 irqc->irq_mask = omap_gpio_mask_irq,
1211 irqc->irq_unmask = omap_gpio_unmask_irq,
1212 irqc->irq_set_type = omap_gpio_irq_type,
1213 irqc->irq_set_wake = omap_gpio_wake_enable,
1214 irqc->name = dev_name(&pdev->dev);
1215
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001216 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1217 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001218 dev_err(dev, "Invalid IRQ resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001219 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001220 }
1221
1222 bank->irq = res->start;
Benoit Cousson862ff642012-02-01 15:58:56 +01001223 bank->dev = dev;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001224 bank->chip.dev = dev;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001225 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001226 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001227 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301228 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301229 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001230 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001231#ifdef CONFIG_OF_GPIO
1232 bank->chip.of_node = of_node_get(node);
1233#endif
Jon Huntera2797be2013-04-04 15:16:15 -05001234 if (node) {
1235 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1236 bank->loses_context = true;
1237 } else {
1238 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001239
1240 if (bank->loses_context)
1241 bank->get_context_loss_count =
1242 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001243 }
1244
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001245 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001246 bank->set_dataout = omap_set_gpio_dataout_reg;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001247 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001248 bank->set_dataout = omap_set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001249
1250 spin_lock_init(&bank->lock);
1251
1252 /* Static mapping, never released */
1253 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001254 bank->base = devm_ioremap_resource(dev, res);
1255 if (IS_ERR(bank->base)) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001256 irq_domain_remove(bank->chip.irqdomain);
Jingoo Han717f70e2014-02-12 11:51:38 +09001257 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001258 }
1259
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301260 platform_set_drvdata(pdev, bank);
1261
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001262 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301263 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001264 pm_runtime_get_sync(bank->dev);
1265
Charulatha Vd0d665a2011-08-31 00:02:21 +05301266 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001267 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301268
Charulatha V03e128c2011-05-05 19:58:01 +05301269 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001270
Nishanth Menon46824e22014-09-05 14:52:55 -05001271 ret = omap_gpio_chip_init(bank, irqc);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001272 if (ret)
1273 return ret;
1274
Tony Lindgren9a748052010-12-07 16:26:56 -08001275 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001276
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301277 pm_runtime_put(bank->dev);
1278
Charulatha V03e128c2011-05-05 19:58:01 +05301279 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001280
Jon Hunter879fe322013-04-04 15:16:12 -05001281 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001282}
1283
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301284#ifdef CONFIG_ARCH_OMAP2PLUS
1285
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001286#if defined(CONFIG_PM)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301287static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001288
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301289static int omap_gpio_runtime_suspend(struct device *dev)
1290{
1291 struct platform_device *pdev = to_platform_device(dev);
1292 struct gpio_bank *bank = platform_get_drvdata(pdev);
1293 u32 l1 = 0, l2 = 0;
1294 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001295 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301296
1297 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001298
1299 /*
1300 * Only edges can generate a wakeup event to the PRCM.
1301 *
1302 * Therefore, ensure any wake-up capable GPIOs have
1303 * edge-detection enabled before going idle to ensure a wakeup
1304 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1305 * NDA TRM 25.5.3.1)
1306 *
1307 * The normal values will be restored upon ->runtime_resume()
1308 * by writing back the values saved in bank->context.
1309 */
1310 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1311 if (wake_low)
Victor Kamensky661553b2013-11-16 02:01:04 +02001312 writel_relaxed(wake_low | bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001313 bank->base + bank->regs->fallingdetect);
1314 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1315 if (wake_hi)
Victor Kamensky661553b2013-11-16 02:01:04 +02001316 writel_relaxed(wake_hi | bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001317 bank->base + bank->regs->risingdetect);
1318
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001319 if (!bank->enabled_non_wakeup_gpios)
1320 goto update_gpio_context_count;
1321
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301322 if (bank->power_mode != OFF_MODE) {
1323 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301324 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301325 }
1326 /*
1327 * If going to OFF, remove triggering for all
1328 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1329 * generated. See OMAP2420 Errata item 1.101.
1330 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001331 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301332 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301333 l1 = bank->context.fallingdetect;
1334 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301335
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301336 l1 &= ~bank->enabled_non_wakeup_gpios;
1337 l2 &= ~bank->enabled_non_wakeup_gpios;
1338
Victor Kamensky661553b2013-11-16 02:01:04 +02001339 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1340 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301341
1342 bank->workaround_enabled = true;
1343
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301344update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301345 if (bank->get_context_loss_count)
1346 bank->context_loss_count =
1347 bank->get_context_loss_count(bank->dev);
1348
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001349 omap_gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301350 spin_unlock_irqrestore(&bank->lock, flags);
1351
1352 return 0;
1353}
1354
Jon Hunter352a2d52013-04-15 13:06:54 -05001355static void omap_gpio_init_context(struct gpio_bank *p);
1356
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301357static int omap_gpio_runtime_resume(struct device *dev)
1358{
1359 struct platform_device *pdev = to_platform_device(dev);
1360 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301361 u32 l = 0, gen, gen0, gen1;
1362 unsigned long flags;
Jon Huntera2797be2013-04-04 15:16:15 -05001363 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301364
1365 spin_lock_irqsave(&bank->lock, flags);
Jon Hunter352a2d52013-04-15 13:06:54 -05001366
1367 /*
1368 * On the first resume during the probe, the context has not
1369 * been initialised and so initialise it now. Also initialise
1370 * the context loss count.
1371 */
1372 if (bank->loses_context && !bank->context_valid) {
1373 omap_gpio_init_context(bank);
1374
1375 if (bank->get_context_loss_count)
1376 bank->context_loss_count =
1377 bank->get_context_loss_count(bank->dev);
1378 }
1379
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001380 omap_gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001381
1382 /*
1383 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1384 * GPIOs were set to edge trigger also in order to be able to
1385 * generate a PRCM wakeup. Here we restore the
1386 * pre-runtime_suspend() values for edge triggering.
1387 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001388 writel_relaxed(bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001389 bank->base + bank->regs->fallingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001390 writel_relaxed(bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001391 bank->base + bank->regs->risingdetect);
1392
Jon Huntera2797be2013-04-04 15:16:15 -05001393 if (bank->loses_context) {
1394 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301395 omap_gpio_restore_context(bank);
1396 } else {
Jon Huntera2797be2013-04-04 15:16:15 -05001397 c = bank->get_context_loss_count(bank->dev);
1398 if (c != bank->context_loss_count) {
1399 omap_gpio_restore_context(bank);
1400 } else {
1401 spin_unlock_irqrestore(&bank->lock, flags);
1402 return 0;
1403 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301404 }
1405 }
1406
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301407 if (!bank->workaround_enabled) {
1408 spin_unlock_irqrestore(&bank->lock, flags);
1409 return 0;
1410 }
1411
Victor Kamensky661553b2013-11-16 02:01:04 +02001412 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301413
1414 /*
1415 * Check if any of the non-wakeup interrupt GPIOs have changed
1416 * state. If so, generate an IRQ by software. This is
1417 * horribly racy, but it's the best we can do to work around
1418 * this silicon bug.
1419 */
1420 l ^= bank->saved_datain;
1421 l &= bank->enabled_non_wakeup_gpios;
1422
1423 /*
1424 * No need to generate IRQs for the rising edge for gpio IRQs
1425 * configured with falling edge only; and vice versa.
1426 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301427 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301428 gen0 &= bank->saved_datain;
1429
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301430 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301431 gen1 &= ~(bank->saved_datain);
1432
1433 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301434 gen = l & (~(bank->context.fallingdetect) &
1435 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301436 /* Consider all GPIO IRQs needed to be updated */
1437 gen |= gen0 | gen1;
1438
1439 if (gen) {
1440 u32 old0, old1;
1441
Victor Kamensky661553b2013-11-16 02:01:04 +02001442 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1443 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301444
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301445 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001446 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301447 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001448 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301449 bank->regs->leveldetect1);
1450 }
1451
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301452 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001453 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301454 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001455 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301456 bank->regs->leveldetect1);
1457 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001458 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1459 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301460 }
1461
1462 bank->workaround_enabled = false;
1463 spin_unlock_irqrestore(&bank->lock, flags);
1464
1465 return 0;
1466}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001467#endif /* CONFIG_PM */
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301468
1469void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001470{
Charulatha V03e128c2011-05-05 19:58:01 +05301471 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001472
Charulatha V03e128c2011-05-05 19:58:01 +05301473 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001474 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301475 continue;
1476
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301477 bank->power_mode = pwr_mode;
1478
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301479 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001480 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001481}
1482
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001483void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001484{
Charulatha V03e128c2011-05-05 19:58:01 +05301485 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001486
Charulatha V03e128c2011-05-05 19:58:01 +05301487 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001488 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301489 continue;
1490
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301491 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001492 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001493}
1494
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001495#if defined(CONFIG_PM)
Jon Hunter352a2d52013-04-15 13:06:54 -05001496static void omap_gpio_init_context(struct gpio_bank *p)
1497{
1498 struct omap_gpio_reg_offs *regs = p->regs;
1499 void __iomem *base = p->base;
1500
Victor Kamensky661553b2013-11-16 02:01:04 +02001501 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1502 p->context.oe = readl_relaxed(base + regs->direction);
1503 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1504 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1505 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1506 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1507 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1508 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1509 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001510
1511 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001512 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001513 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001514 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001515
1516 p->context_valid = true;
1517}
1518
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301519static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301520{
Victor Kamensky661553b2013-11-16 02:01:04 +02001521 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301522 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001523 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1524 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301525 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001526 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301527 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001528 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301529 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001530 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301531 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301532 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001533 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301534 bank->base + bank->regs->set_dataout);
1535 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001536 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301537 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001538 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301539
Nishanth Menonae547352011-09-09 19:08:58 +05301540 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001541 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301542 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001543 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301544 bank->base + bank->regs->debounce_en);
1545 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301546
Victor Kamensky661553b2013-11-16 02:01:04 +02001547 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301548 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001549 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301550 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301551}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001552#endif /* CONFIG_PM */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301553#else
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301554#define omap_gpio_runtime_suspend NULL
1555#define omap_gpio_runtime_resume NULL
Arnd Bergmannea4a21a2013-05-31 17:59:46 +02001556static inline void omap_gpio_init_context(struct gpio_bank *p) {}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301557#endif
1558
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301559static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301560 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1561 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301562};
1563
Benoit Cousson384ebe12011-08-16 11:53:02 +02001564#if defined(CONFIG_OF)
1565static struct omap_gpio_reg_offs omap2_gpio_regs = {
1566 .revision = OMAP24XX_GPIO_REVISION,
1567 .direction = OMAP24XX_GPIO_OE,
1568 .datain = OMAP24XX_GPIO_DATAIN,
1569 .dataout = OMAP24XX_GPIO_DATAOUT,
1570 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1571 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1572 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1573 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1574 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1575 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1576 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1577 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1578 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1579 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1580 .ctrl = OMAP24XX_GPIO_CTRL,
1581 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1582 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1583 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1584 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1585 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1586};
1587
1588static struct omap_gpio_reg_offs omap4_gpio_regs = {
1589 .revision = OMAP4_GPIO_REVISION,
1590 .direction = OMAP4_GPIO_OE,
1591 .datain = OMAP4_GPIO_DATAIN,
1592 .dataout = OMAP4_GPIO_DATAOUT,
1593 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1594 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1595 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1596 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1597 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1598 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1599 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1600 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1601 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1602 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1603 .ctrl = OMAP4_GPIO_CTRL,
1604 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1605 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1606 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1607 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1608 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1609};
1610
Chen Gange9a65bb2013-02-06 18:44:32 +08001611static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001612 .regs = &omap2_gpio_regs,
1613 .bank_width = 32,
1614 .dbck_flag = false,
1615};
1616
Chen Gange9a65bb2013-02-06 18:44:32 +08001617static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001618 .regs = &omap2_gpio_regs,
1619 .bank_width = 32,
1620 .dbck_flag = true,
1621};
1622
Chen Gange9a65bb2013-02-06 18:44:32 +08001623static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001624 .regs = &omap4_gpio_regs,
1625 .bank_width = 32,
1626 .dbck_flag = true,
1627};
1628
1629static const struct of_device_id omap_gpio_match[] = {
1630 {
1631 .compatible = "ti,omap4-gpio",
1632 .data = &omap4_pdata,
1633 },
1634 {
1635 .compatible = "ti,omap3-gpio",
1636 .data = &omap3_pdata,
1637 },
1638 {
1639 .compatible = "ti,omap2-gpio",
1640 .data = &omap2_pdata,
1641 },
1642 { },
1643};
1644MODULE_DEVICE_TABLE(of, omap_gpio_match);
1645#endif
1646
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001647static struct platform_driver omap_gpio_driver = {
1648 .probe = omap_gpio_probe,
1649 .driver = {
1650 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301651 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001652 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001653 },
1654};
1655
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001656/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001657 * gpio driver register needs to be done before
1658 * machine_init functions access gpio APIs.
1659 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001660 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001661static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001662{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001663 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001664}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001665postcore_initcall(omap_gpio_drv_reg);