blob: 628a1328e5032126638686df63a7ad75ddea3bef [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
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200482static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
Tony Lindgren3d009c82015-01-16 14:50:50 -0800483{
484 if (!LINE_USED(bank->mod_usage, offset)) {
485 omap_enable_gpio_module(bank, offset);
486 omap_set_gpio_direction(bank, offset, 1);
487 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200488 bank->irq_usage |= BIT(offset);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800489}
490
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200491static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100492{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200493 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren4b254082012-08-30 15:37:24 -0700494 unsigned gpio = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100495 int retval;
David Brownella6472532008-03-03 04:33:30 -0800496 unsigned long flags;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200497 unsigned offset;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100498
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200499 if (!BANK_USED(bank))
500 pm_runtime_get_sync(bank->dev);
Jon Hunter8d4c2772013-03-01 11:22:48 -0600501
Tony Lindgren4b254082012-08-30 15:37:24 -0700502#ifdef CONFIG_ARCH_OMAP1
503 if (d->irq > IH_MPUIO_BASE)
Lennert Buytenheke9191022010-11-29 11:17:17 +0100504 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren4b254082012-08-30 15:37:24 -0700505#endif
506
507 if (!gpio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200508 gpio = omap_irq_to_gpio(bank, d->hwirq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100509
David Brownelle5c56ed2006-12-06 17:13:59 -0800510 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100511 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800512
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530513 if (!bank->regs->leveldetect0 &&
514 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100515 return -EINVAL;
516
David Brownella6472532008-03-03 04:33:30 -0800517 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200518 offset = GPIO_INDEX(bank, gpio);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200519 retval = omap_set_gpio_triggering(bank, offset, type);
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200520 omap_gpio_init_irq(bank, offset);
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200521 if (!omap_gpio_is_input(bank, offset)) {
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200522 spin_unlock_irqrestore(&bank->lock, flags);
523 return -EINVAL;
524 }
David Brownella6472532008-03-03 04:33:30 -0800525 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800526
527 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100528 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800529 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100530 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800531
Tony Lindgren92105bb2005-09-07 17:20:26 +0100532 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100533}
534
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200535static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100536{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100537 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100538
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700539 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200540 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300541
542 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700543 if (bank->regs->irqstatus2) {
544 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200545 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700546 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700547
548 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200549 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100550}
551
Grygorii Strashko9943f262015-03-23 14:18:27 +0200552static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
553 unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100554{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200555 omap_clear_gpio_irqbank(bank, BIT(offset));
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
Grygorii Strashko9943f262015-03-23 14:18:27 +0200616static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
617 unsigned offset, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100618{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530619 if (enable)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200620 omap_enable_gpio_irqbank(bank, BIT(offset));
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530621 else
Grygorii Strashko9943f262015-03-23 14:18:27 +0200622 omap_disable_gpio_irqbank(bank, BIT(offset));
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 */
Grygorii Strashko9943f262015-03-23 14:18:27 +0200633static int omap_set_gpio_wakeup(struct gpio_bank *bank, unsigned offset,
634 int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100635{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200636 u32 gpio_bit = BIT(offset);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700637 unsigned long flags;
David Brownella6472532008-03-03 04:33:30 -0800638
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700639 if (bank->non_wakeup_gpios & gpio_bit) {
Benoit Cousson862ff642012-02-01 15:58:56 +0100640 dev_err(bank->dev,
Grygorii Strashko9943f262015-03-23 14:18:27 +0200641 "Unable to modify wakeup on non-wakeup GPIO%d\n",
642 offset);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100643 return -EINVAL;
644 }
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700645
646 spin_lock_irqsave(&bank->lock, flags);
647 if (enable)
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530648 bank->context.wake_en |= gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700649 else
Tarun Kanti DebBarma0aa27272012-04-27 19:43:33 +0530650 bank->context.wake_en &= ~gpio_bit;
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700651
Victor Kamensky661553b2013-11-16 02:01:04 +0200652 writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
Kevin Hilmanf64ad1a2011-04-22 09:45:27 -0700653 spin_unlock_irqrestore(&bank->lock, flags);
654
655 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100656}
657
Grygorii Strashko9943f262015-03-23 14:18:27 +0200658static void omap_reset_gpio(struct gpio_bank *bank, unsigned offset)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300659{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200660 omap_set_gpio_direction(bank, offset, 1);
661 omap_set_gpio_irqenable(bank, offset, 0);
662 omap_clear_gpio_irqstatus(bank, offset);
663 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
664 omap_clear_gpio_debounce(bank, offset);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300665}
666
Tony Lindgren92105bb2005-09-07 17:20:26 +0100667/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200668static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100669{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200670 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200671 unsigned offset = d->hwirq;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100672
Grygorii Strashko9943f262015-03-23 14:18:27 +0200673 return omap_set_gpio_wakeup(bank, offset, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100674}
675
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800676static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100677{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800678 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800679 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100680
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530681 /*
682 * If this is the first gpio_request for the bank,
683 * enable the bank module.
684 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200685 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530686 pm_runtime_get_sync(bank->dev);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100687
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530688 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300689 /* Set trigger to none. You need to enable the desired trigger with
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200690 * request_irq() or set_irq_type(). Only do this if the IRQ line has
691 * not already been requested.
Tony Lindgren4196dd62006-09-25 12:41:38 +0300692 */
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200693 if (!LINE_USED(bank->irq_usage, offset)) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200694 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
695 omap_enable_gpio_module(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100696 }
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200697 bank->mod_usage |= BIT(offset);
David Brownella6472532008-03-03 04:33:30 -0800698 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100699
700 return 0;
701}
702
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800703static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100704{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800705 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -0800706 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100707
David Brownella6472532008-03-03 04:33:30 -0800708 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200709 bank->mod_usage &= ~(BIT(offset));
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200710 omap_disable_gpio_module(bank, offset);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200711 omap_reset_gpio(bank, offset);
David Brownella6472532008-03-03 04:33:30 -0800712 spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530713
714 /*
715 * If this is the last gpio to be freed in the bank,
716 * disable the bank module.
717 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200718 if (!BANK_USED(bank))
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530719 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100720}
721
722/*
723 * We need to unmask the GPIO bank interrupt as soon as possible to
724 * avoid missing GPIO interrupts for other lines in the bank.
725 * Then we need to mask-read-clear-unmask the triggered GPIO lines
726 * in the bank to avoid missing nested interrupts for a GPIO line.
727 * If we wait to unmask individual GPIO lines in the bank after the
728 * line's interrupt handler has been run, we may miss some nested
729 * interrupts.
730 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200731static void omap_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100733 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100734 u32 isr;
Jon Hunter3513cde2013-04-04 15:16:14 -0500735 unsigned int bit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100736 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -0700737 int unmasked = 0;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200738 struct irq_chip *irqchip = irq_desc_get_chip(desc);
739 struct gpio_chip *chip = irq_get_handler_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200741 chained_irq_enter(irqchip, desc);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100742
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200743 bank = container_of(chip, struct gpio_bank, chip);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700744 isr_reg = bank->base + bank->regs->irqstatus;
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530745 pm_runtime_get_sync(bank->dev);
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800746
747 if (WARN_ON(!isr_reg))
748 goto exit;
749
Laurent Navete83507b2013-03-20 13:15:57 +0100750 while (1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100751 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700752 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100753
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200754 enabled = omap_get_gpio_irqbank_mask(bank);
Victor Kamensky661553b2013-11-16 02:01:04 +0200755 isr_saved = isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100756
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530757 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800758 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100759
760 /* clear edge sensitive interrupts before handler(s) are
761 called so that we don't miss any interrupt occurred while
762 executing them */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200763 omap_disable_gpio_irqbank(bank, isr_saved & ~level_mask);
764 omap_clear_gpio_irqbank(bank, isr_saved & ~level_mask);
765 omap_enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100766
767 /* if there is only edge sensitive GPIO pin interrupts
768 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -0700769 if (!level_mask && !unmasked) {
770 unmasked = 1;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200771 chained_irq_exit(irqchip, desc);
Imre Deakea6dedd2006-06-26 16:16:00 -0700772 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100773
Tony Lindgren92105bb2005-09-07 17:20:26 +0100774 if (!isr)
775 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100776
Jon Hunter3513cde2013-04-04 15:16:14 -0500777 while (isr) {
778 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200779 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100780
Cory Maccarrone4318f362010-01-08 10:29:04 -0800781 /*
782 * Some chips can't respond to both rising and falling
783 * at the same time. If this irq was requested with
784 * both flags, we need to flip the ICR data for the IRQ
785 * to respond to the IRQ for the opposite direction.
786 * This will be indicated in the bank toggle_mask.
787 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200788 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200789 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800790
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200791 generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
792 bit));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100793 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000794 }
Imre Deakea6dedd2006-06-26 16:16:00 -0700795 /* if bank has any level sensitive GPIO pin interrupt
796 configured, we must unmask the bank interrupt only after
797 handler(s) are executed in order to avoid spurious bank
798 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800799exit:
Imre Deakea6dedd2006-06-26 16:16:00 -0700800 if (!unmasked)
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200801 chained_irq_exit(irqchip, desc);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530802 pm_runtime_put(bank->dev);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100803}
804
Tony Lindgren3d009c82015-01-16 14:50:50 -0800805static unsigned int omap_gpio_irq_startup(struct irq_data *d)
806{
807 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800808 unsigned long flags;
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200809 unsigned offset = d->hwirq;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800810
811 if (!BANK_USED(bank))
812 pm_runtime_get_sync(bank->dev);
813
814 spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200815 omap_gpio_init_irq(bank, offset);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800816 spin_unlock_irqrestore(&bank->lock, flags);
817 omap_gpio_unmask_irq(d);
818
819 return 0;
820}
821
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200822static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300823{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200824 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700825 unsigned long flags;
Grygorii Strashko9943f262015-03-23 14:18:27 +0200826 unsigned offset = d->hwirq;
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);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200831 omap_reset_gpio(bank, offset);
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);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200845 unsigned offset = d->hwirq;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100846
Grygorii Strashko9943f262015-03-23 14:18:27 +0200847 omap_clear_gpio_irqstatus(bank, offset);
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);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200853 unsigned offset = 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);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200857 omap_set_gpio_irqenable(bank, offset, 0);
858 omap_set_gpio_triggering(bank, offset, 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);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200865 unsigned offset = d->hwirq;
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100866 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700867 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700868
Colin Cross85ec7b92011-06-06 13:38:18 -0700869 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700870 if (trigger)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200871 omap_set_gpio_triggering(bank, offset, trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800872
873 /* For level-triggered GPIOs, the clearing must be done after
874 * the HW source is cleared, thus after the handler has run */
Grygorii Strashko9943f262015-03-23 14:18:27 +0200875 if (bank->level_mask & BIT(offset)) {
876 omap_set_gpio_irqenable(bank, offset, 0);
877 omap_clear_gpio_irqstatus(bank, offset);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800878 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100879
Grygorii Strashko9943f262015-03-23 14:18:27 +0200880 omap_set_gpio_irqenable(bank, offset, 1);
Colin Cross85ec7b92011-06-06 13:38:18 -0700881 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100882}
883
David Brownelle5c56ed2006-12-06 17:13:59 -0800884/*---------------------------------------------------------------------*/
885
Magnus Damm79ee0312009-07-08 13:22:04 +0200886static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800887{
Magnus Damm79ee0312009-07-08 13:22:04 +0200888 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800889 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800890 void __iomem *mask_reg = bank->base +
891 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800892 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800893
David Brownella6472532008-03-03 04:33:30 -0800894 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200895 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800896 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800897
898 return 0;
899}
900
Magnus Damm79ee0312009-07-08 13:22:04 +0200901static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800902{
Magnus Damm79ee0312009-07-08 13:22:04 +0200903 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800904 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800905 void __iomem *mask_reg = bank->base +
906 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800907 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800908
David Brownella6472532008-03-03 04:33:30 -0800909 spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200910 writel_relaxed(bank->context.wake_en, mask_reg);
David Brownella6472532008-03-03 04:33:30 -0800911 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800912
913 return 0;
914}
915
Alexey Dobriyan47145212009-12-14 18:00:08 -0800916static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200917 .suspend_noirq = omap_mpuio_suspend_noirq,
918 .resume_noirq = omap_mpuio_resume_noirq,
919};
920
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200921/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800922static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800923 .driver = {
924 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200925 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800926 },
927};
928
929static struct platform_device omap_mpuio_device = {
930 .name = "mpuio",
931 .id = -1,
932 .dev = {
933 .driver = &omap_mpuio_driver.driver,
934 }
935 /* could list the /proc/iomem resources */
936};
937
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200938static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800939{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800940 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700941
David Brownell11a78b72006-12-06 17:14:11 -0800942 if (platform_driver_register(&omap_mpuio_driver) == 0)
943 (void) platform_device_register(&omap_mpuio_device);
944}
945
David Brownelle5c56ed2006-12-06 17:13:59 -0800946/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100947
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200948static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200949{
950 struct gpio_bank *bank;
951 unsigned long flags;
952 void __iomem *reg;
953 int dir;
954
955 bank = container_of(chip, struct gpio_bank, chip);
956 reg = bank->base + bank->regs->direction;
957 spin_lock_irqsave(&bank->lock, flags);
958 dir = !!(readl_relaxed(reg) & BIT(offset));
959 spin_unlock_irqrestore(&bank->lock, flags);
960 return dir;
961}
962
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200963static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800964{
965 struct gpio_bank *bank;
966 unsigned long flags;
967
968 bank = container_of(chip, struct gpio_bank, chip);
969 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200970 omap_set_gpio_direction(bank, offset, 1);
David Brownell52e31342008-03-03 12:43:23 -0800971 spin_unlock_irqrestore(&bank->lock, flags);
972 return 0;
973}
974
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200975static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800976{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300977 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300978
Charulatha Va8be8da2011-04-22 16:38:16 +0530979 bank = container_of(chip, struct gpio_bank, chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300980
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200981 if (omap_gpio_is_input(bank, offset))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200982 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300983 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200984 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -0800985}
986
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200987static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800988{
989 struct gpio_bank *bank;
990 unsigned long flags;
991
992 bank = container_of(chip, struct gpio_bank, chip);
993 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700994 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200995 omap_set_gpio_direction(bank, offset, 0);
David Brownell52e31342008-03-03 12:43:23 -0800996 spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200997 return 0;
David Brownell52e31342008-03-03 12:43:23 -0800998}
999
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001000static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
1001 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001002{
1003 struct gpio_bank *bank;
1004 unsigned long flags;
1005
1006 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001007
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001008 spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001009 omap2_set_gpio_debounce(bank, offset, debounce);
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001010 spin_unlock_irqrestore(&bank->lock, flags);
1011
1012 return 0;
1013}
1014
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001015static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001016{
1017 struct gpio_bank *bank;
1018 unsigned long flags;
1019
1020 bank = container_of(chip, struct gpio_bank, chip);
1021 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001022 bank->set_dataout(bank, offset, value);
David Brownell52e31342008-03-03 12:43:23 -08001023 spin_unlock_irqrestore(&bank->lock, flags);
1024}
1025
1026/*---------------------------------------------------------------------*/
1027
Tony Lindgren9a748052010-12-07 16:26:56 -08001028static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001029{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001030 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001031 u32 rev;
1032
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001033 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001034 return;
1035
Victor Kamensky661553b2013-11-16 02:01:04 +02001036 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001037 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001038 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001039
1040 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001041}
1042
Charulatha V03e128c2011-05-05 19:58:01 +05301043static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001044{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301045 void __iomem *base = bank->base;
1046 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001047
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301048 if (bank->width == 16)
1049 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001050
Charulatha Vd0d665a2011-08-31 00:02:21 +05301051 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001052 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301053 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001054 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301055
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001056 omap_gpio_rmw(base, bank->regs->irqenable, l,
1057 bank->regs->irqenable_inv);
1058 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1059 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301060 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001061 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301062
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301063 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001064 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301065 /* Initialize interface clk ungated, module enabled */
1066 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001067 writel_relaxed(0, base + bank->regs->ctrl);
Tarun Kanti DebBarma34672012012-07-11 14:43:14 +05301068
1069 bank->dbck = clk_get(bank->dev, "dbclk");
1070 if (IS_ERR(bank->dbck))
1071 dev_err(bank->dev, "Could not get gpio dbck\n");
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001072}
1073
Bill Pemberton38363092012-11-19 13:22:34 -05001074static void
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001075omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1076 unsigned int num)
1077{
1078 struct irq_chip_generic *gc;
1079 struct irq_chip_type *ct;
1080
1081 gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1082 handle_simple_irq);
Todd Poynor83233742011-07-18 07:43:14 -07001083 if (!gc) {
1084 dev_err(bank->dev, "Memory alloc failed for gc\n");
1085 return;
1086 }
1087
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001088 ct = gc->chip_types;
1089
1090 /* NOTE: No ack required, reading IRQ status clears it. */
1091 ct->chip.irq_mask = irq_gc_mask_set_bit;
1092 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001093 ct->chip.irq_set_type = omap_gpio_irq_type;
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301094
1095 if (bank->regs->wkup_en)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001096 ct->chip.irq_set_wake = omap_gpio_wake_enable;
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001097
1098 ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1099 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1100 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1101}
1102
Nishanth Menon46824e222014-09-05 14:52:55 -05001103static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001104{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001105 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001106 static int gpio;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001107 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001108 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001109
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001110 /*
1111 * REVISIT eventually switch from OMAP-specific gpio structs
1112 * over to the generic ones
1113 */
1114 bank->chip.request = omap_gpio_request;
1115 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001116 bank->chip.get_direction = omap_gpio_get_direction;
1117 bank->chip.direction_input = omap_gpio_input;
1118 bank->chip.get = omap_gpio_get;
1119 bank->chip.direction_output = omap_gpio_output;
1120 bank->chip.set_debounce = omap_gpio_debounce;
1121 bank->chip.set = omap_gpio_set;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301122 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001123 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301124 if (bank->regs->wkup_en)
1125 bank->chip.dev = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001126 bank->chip.base = OMAP_MPUIO(0);
1127 } else {
1128 bank->chip.label = "gpio";
1129 bank->chip.base = gpio;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001130 gpio += bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001131 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001132 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001133
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001134 ret = gpiochip_add(&bank->chip);
1135 if (ret) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001136 dev_err(bank->dev, "Could not register gpio chip %d\n", ret);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001137 return ret;
1138 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001139
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001140#ifdef CONFIG_ARCH_OMAP1
1141 /*
1142 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1143 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1144 */
1145 irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
1146 if (irq_base < 0) {
1147 dev_err(bank->dev, "Couldn't allocate IRQ numbers\n");
1148 return -ENODEV;
1149 }
1150#endif
1151
Nishanth Menon46824e222014-09-05 14:52:55 -05001152 ret = gpiochip_irqchip_add(&bank->chip, irqc,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001153 irq_base, omap_gpio_irq_handler,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001154 IRQ_TYPE_NONE);
1155
1156 if (ret) {
1157 dev_err(bank->dev, "Couldn't add irqchip to gpiochip %d\n", ret);
Linus Walleijda26d5d2014-09-16 15:11:41 -07001158 gpiochip_remove(&bank->chip);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001159 return -ENODEV;
1160 }
1161
Nishanth Menon46824e222014-09-05 14:52:55 -05001162 gpiochip_set_chained_irqchip(&bank->chip, irqc,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001163 bank->irq, omap_gpio_irq_handler);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001164
Jon Hunterede4d7a2013-03-01 11:22:47 -06001165 for (j = 0; j < bank->width; j++) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001166 int irq = irq_find_mapping(bank->chip.irqdomain, j);
Charulatha Vd0d665a2011-08-31 00:02:21 +05301167 if (bank->is_mpuio) {
Jon Hunterede4d7a2013-03-01 11:22:47 -06001168 omap_mpuio_alloc_gc(bank, irq, bank->width);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001169 irq_set_chip_and_handler(irq, NULL, NULL);
1170 set_irq_flags(irq, 0);
Kevin Hilmanf8b46b52011-04-21 13:23:34 -07001171 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001172 }
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001173
1174 return 0;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001175}
1176
Benoit Cousson384ebe12011-08-16 11:53:02 +02001177static const struct of_device_id omap_gpio_match[];
1178
Bill Pemberton38363092012-11-19 13:22:34 -05001179static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001180{
Benoit Cousson862ff642012-02-01 15:58:56 +01001181 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001182 struct device_node *node = dev->of_node;
1183 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001184 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001185 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001186 struct gpio_bank *bank;
Nishanth Menon46824e222014-09-05 14:52:55 -05001187 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001188 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001189
Benoit Cousson384ebe12011-08-16 11:53:02 +02001190 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1191
Jingoo Hane56aee12013-07-30 17:08:05 +09001192 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001193 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001194 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001195
Tobias Klauser086d5852012-10-05 11:37:38 +02001196 bank = devm_kzalloc(dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301197 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001198 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001199 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301200 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001201
Nishanth Menon46824e222014-09-05 14:52:55 -05001202 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1203 if (!irqc)
1204 return -ENOMEM;
1205
Tony Lindgren3d009c82015-01-16 14:50:50 -08001206 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e222014-09-05 14:52:55 -05001207 irqc->irq_shutdown = omap_gpio_irq_shutdown,
1208 irqc->irq_ack = omap_gpio_ack_irq,
1209 irqc->irq_mask = omap_gpio_mask_irq,
1210 irqc->irq_unmask = omap_gpio_unmask_irq,
1211 irqc->irq_set_type = omap_gpio_irq_type,
1212 irqc->irq_set_wake = omap_gpio_wake_enable,
1213 irqc->name = dev_name(&pdev->dev);
1214
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001215 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1216 if (unlikely(!res)) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001217 dev_err(dev, "Invalid IRQ resource\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001218 return -ENODEV;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001219 }
1220
1221 bank->irq = res->start;
Benoit Cousson862ff642012-02-01 15:58:56 +01001222 bank->dev = dev;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001223 bank->chip.dev = dev;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001224 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001225 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001226 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301227 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301228 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001229 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001230#ifdef CONFIG_OF_GPIO
1231 bank->chip.of_node = of_node_get(node);
1232#endif
Jon Huntera2797be2013-04-04 15:16:15 -05001233 if (node) {
1234 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1235 bank->loses_context = true;
1236 } else {
1237 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001238
1239 if (bank->loses_context)
1240 bank->get_context_loss_count =
1241 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001242 }
1243
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001244 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001245 bank->set_dataout = omap_set_gpio_dataout_reg;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001246 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001247 bank->set_dataout = omap_set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001248
1249 spin_lock_init(&bank->lock);
1250
1251 /* Static mapping, never released */
1252 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001253 bank->base = devm_ioremap_resource(dev, res);
1254 if (IS_ERR(bank->base)) {
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001255 irq_domain_remove(bank->chip.irqdomain);
Jingoo Han717f70e2014-02-12 11:51:38 +09001256 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001257 }
1258
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301259 platform_set_drvdata(pdev, bank);
1260
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001261 pm_runtime_enable(bank->dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301262 pm_runtime_irq_safe(bank->dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001263 pm_runtime_get_sync(bank->dev);
1264
Charulatha Vd0d665a2011-08-31 00:02:21 +05301265 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001266 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301267
Charulatha V03e128c2011-05-05 19:58:01 +05301268 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001269
Nishanth Menon46824e222014-09-05 14:52:55 -05001270 ret = omap_gpio_chip_init(bank, irqc);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001271 if (ret)
1272 return ret;
1273
Tony Lindgren9a748052010-12-07 16:26:56 -08001274 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001275
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301276 pm_runtime_put(bank->dev);
1277
Charulatha V03e128c2011-05-05 19:58:01 +05301278 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001279
Jon Hunter879fe322013-04-04 15:16:12 -05001280 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001281}
1282
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301283#ifdef CONFIG_ARCH_OMAP2PLUS
1284
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001285#if defined(CONFIG_PM)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301286static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001287
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301288static int omap_gpio_runtime_suspend(struct device *dev)
1289{
1290 struct platform_device *pdev = to_platform_device(dev);
1291 struct gpio_bank *bank = platform_get_drvdata(pdev);
1292 u32 l1 = 0, l2 = 0;
1293 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001294 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301295
1296 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001297
1298 /*
1299 * Only edges can generate a wakeup event to the PRCM.
1300 *
1301 * Therefore, ensure any wake-up capable GPIOs have
1302 * edge-detection enabled before going idle to ensure a wakeup
1303 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1304 * NDA TRM 25.5.3.1)
1305 *
1306 * The normal values will be restored upon ->runtime_resume()
1307 * by writing back the values saved in bank->context.
1308 */
1309 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1310 if (wake_low)
Victor Kamensky661553b2013-11-16 02:01:04 +02001311 writel_relaxed(wake_low | bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001312 bank->base + bank->regs->fallingdetect);
1313 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1314 if (wake_hi)
Victor Kamensky661553b2013-11-16 02:01:04 +02001315 writel_relaxed(wake_hi | bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001316 bank->base + bank->regs->risingdetect);
1317
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001318 if (!bank->enabled_non_wakeup_gpios)
1319 goto update_gpio_context_count;
1320
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301321 if (bank->power_mode != OFF_MODE) {
1322 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301323 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301324 }
1325 /*
1326 * If going to OFF, remove triggering for all
1327 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1328 * generated. See OMAP2420 Errata item 1.101.
1329 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001330 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301331 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301332 l1 = bank->context.fallingdetect;
1333 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301334
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301335 l1 &= ~bank->enabled_non_wakeup_gpios;
1336 l2 &= ~bank->enabled_non_wakeup_gpios;
1337
Victor Kamensky661553b2013-11-16 02:01:04 +02001338 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1339 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301340
1341 bank->workaround_enabled = true;
1342
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301343update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301344 if (bank->get_context_loss_count)
1345 bank->context_loss_count =
1346 bank->get_context_loss_count(bank->dev);
1347
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001348 omap_gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301349 spin_unlock_irqrestore(&bank->lock, flags);
1350
1351 return 0;
1352}
1353
Jon Hunter352a2d52013-04-15 13:06:54 -05001354static void omap_gpio_init_context(struct gpio_bank *p);
1355
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301356static int omap_gpio_runtime_resume(struct device *dev)
1357{
1358 struct platform_device *pdev = to_platform_device(dev);
1359 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301360 u32 l = 0, gen, gen0, gen1;
1361 unsigned long flags;
Jon Huntera2797be2013-04-04 15:16:15 -05001362 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301363
1364 spin_lock_irqsave(&bank->lock, flags);
Jon Hunter352a2d52013-04-15 13:06:54 -05001365
1366 /*
1367 * On the first resume during the probe, the context has not
1368 * been initialised and so initialise it now. Also initialise
1369 * the context loss count.
1370 */
1371 if (bank->loses_context && !bank->context_valid) {
1372 omap_gpio_init_context(bank);
1373
1374 if (bank->get_context_loss_count)
1375 bank->context_loss_count =
1376 bank->get_context_loss_count(bank->dev);
1377 }
1378
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001379 omap_gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001380
1381 /*
1382 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1383 * GPIOs were set to edge trigger also in order to be able to
1384 * generate a PRCM wakeup. Here we restore the
1385 * pre-runtime_suspend() values for edge triggering.
1386 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001387 writel_relaxed(bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001388 bank->base + bank->regs->fallingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001389 writel_relaxed(bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001390 bank->base + bank->regs->risingdetect);
1391
Jon Huntera2797be2013-04-04 15:16:15 -05001392 if (bank->loses_context) {
1393 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301394 omap_gpio_restore_context(bank);
1395 } else {
Jon Huntera2797be2013-04-04 15:16:15 -05001396 c = bank->get_context_loss_count(bank->dev);
1397 if (c != bank->context_loss_count) {
1398 omap_gpio_restore_context(bank);
1399 } else {
1400 spin_unlock_irqrestore(&bank->lock, flags);
1401 return 0;
1402 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301403 }
1404 }
1405
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301406 if (!bank->workaround_enabled) {
1407 spin_unlock_irqrestore(&bank->lock, flags);
1408 return 0;
1409 }
1410
Victor Kamensky661553b2013-11-16 02:01:04 +02001411 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301412
1413 /*
1414 * Check if any of the non-wakeup interrupt GPIOs have changed
1415 * state. If so, generate an IRQ by software. This is
1416 * horribly racy, but it's the best we can do to work around
1417 * this silicon bug.
1418 */
1419 l ^= bank->saved_datain;
1420 l &= bank->enabled_non_wakeup_gpios;
1421
1422 /*
1423 * No need to generate IRQs for the rising edge for gpio IRQs
1424 * configured with falling edge only; and vice versa.
1425 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301426 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301427 gen0 &= bank->saved_datain;
1428
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301429 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301430 gen1 &= ~(bank->saved_datain);
1431
1432 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301433 gen = l & (~(bank->context.fallingdetect) &
1434 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301435 /* Consider all GPIO IRQs needed to be updated */
1436 gen |= gen0 | gen1;
1437
1438 if (gen) {
1439 u32 old0, old1;
1440
Victor Kamensky661553b2013-11-16 02:01:04 +02001441 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1442 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301443
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301444 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001445 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301446 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001447 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301448 bank->regs->leveldetect1);
1449 }
1450
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301451 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001452 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301453 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001454 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301455 bank->regs->leveldetect1);
1456 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001457 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1458 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301459 }
1460
1461 bank->workaround_enabled = false;
1462 spin_unlock_irqrestore(&bank->lock, flags);
1463
1464 return 0;
1465}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001466#endif /* CONFIG_PM */
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301467
1468void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001469{
Charulatha V03e128c2011-05-05 19:58:01 +05301470 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001471
Charulatha V03e128c2011-05-05 19:58:01 +05301472 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001473 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301474 continue;
1475
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301476 bank->power_mode = pwr_mode;
1477
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301478 pm_runtime_put_sync_suspend(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001479 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001480}
1481
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001482void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001483{
Charulatha V03e128c2011-05-05 19:58:01 +05301484 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001485
Charulatha V03e128c2011-05-05 19:58:01 +05301486 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001487 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301488 continue;
1489
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301490 pm_runtime_get_sync(bank->dev);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001491 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001492}
1493
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001494#if defined(CONFIG_PM)
Jon Hunter352a2d52013-04-15 13:06:54 -05001495static void omap_gpio_init_context(struct gpio_bank *p)
1496{
1497 struct omap_gpio_reg_offs *regs = p->regs;
1498 void __iomem *base = p->base;
1499
Victor Kamensky661553b2013-11-16 02:01:04 +02001500 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1501 p->context.oe = readl_relaxed(base + regs->direction);
1502 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1503 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1504 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1505 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1506 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1507 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1508 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001509
1510 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001511 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001512 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001513 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001514
1515 p->context_valid = true;
1516}
1517
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301518static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301519{
Victor Kamensky661553b2013-11-16 02:01:04 +02001520 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301521 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001522 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1523 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301524 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001525 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301526 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001527 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301528 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001529 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301530 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301531 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001532 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301533 bank->base + bank->regs->set_dataout);
1534 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001535 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301536 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001537 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301538
Nishanth Menonae547352011-09-09 19:08:58 +05301539 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001540 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301541 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001542 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301543 bank->base + bank->regs->debounce_en);
1544 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301545
Victor Kamensky661553b2013-11-16 02:01:04 +02001546 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301547 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001548 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301549 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301550}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001551#endif /* CONFIG_PM */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301552#else
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301553#define omap_gpio_runtime_suspend NULL
1554#define omap_gpio_runtime_resume NULL
Arnd Bergmannea4a21a2013-05-31 17:59:46 +02001555static inline void omap_gpio_init_context(struct gpio_bank *p) {}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301556#endif
1557
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301558static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301559 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1560 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301561};
1562
Benoit Cousson384ebe12011-08-16 11:53:02 +02001563#if defined(CONFIG_OF)
1564static struct omap_gpio_reg_offs omap2_gpio_regs = {
1565 .revision = OMAP24XX_GPIO_REVISION,
1566 .direction = OMAP24XX_GPIO_OE,
1567 .datain = OMAP24XX_GPIO_DATAIN,
1568 .dataout = OMAP24XX_GPIO_DATAOUT,
1569 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1570 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1571 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1572 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1573 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1574 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1575 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1576 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1577 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1578 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1579 .ctrl = OMAP24XX_GPIO_CTRL,
1580 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1581 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1582 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1583 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1584 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1585};
1586
1587static struct omap_gpio_reg_offs omap4_gpio_regs = {
1588 .revision = OMAP4_GPIO_REVISION,
1589 .direction = OMAP4_GPIO_OE,
1590 .datain = OMAP4_GPIO_DATAIN,
1591 .dataout = OMAP4_GPIO_DATAOUT,
1592 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1593 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1594 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1595 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1596 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1597 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1598 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1599 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1600 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1601 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1602 .ctrl = OMAP4_GPIO_CTRL,
1603 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1604 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1605 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1606 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1607 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1608};
1609
Chen Gange9a65bb2013-02-06 18:44:32 +08001610static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001611 .regs = &omap2_gpio_regs,
1612 .bank_width = 32,
1613 .dbck_flag = false,
1614};
1615
Chen Gange9a65bb2013-02-06 18:44:32 +08001616static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001617 .regs = &omap2_gpio_regs,
1618 .bank_width = 32,
1619 .dbck_flag = true,
1620};
1621
Chen Gange9a65bb2013-02-06 18:44:32 +08001622static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001623 .regs = &omap4_gpio_regs,
1624 .bank_width = 32,
1625 .dbck_flag = true,
1626};
1627
1628static const struct of_device_id omap_gpio_match[] = {
1629 {
1630 .compatible = "ti,omap4-gpio",
1631 .data = &omap4_pdata,
1632 },
1633 {
1634 .compatible = "ti,omap3-gpio",
1635 .data = &omap3_pdata,
1636 },
1637 {
1638 .compatible = "ti,omap2-gpio",
1639 .data = &omap2_pdata,
1640 },
1641 { },
1642};
1643MODULE_DEVICE_TABLE(of, omap_gpio_match);
1644#endif
1645
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001646static struct platform_driver omap_gpio_driver = {
1647 .probe = omap_gpio_probe,
1648 .driver = {
1649 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301650 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001651 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001652 },
1653};
1654
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001655/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001656 * gpio driver register needs to be done before
1657 * machine_init functions access gpio APIs.
1658 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001659 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001660static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001661{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001662 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001663}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001664postcore_initcall(omap_gpio_drv_reg);