blob: ab5035b968866ef8d6892d67816292582557bfe5 [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
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +030032#define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053033
Charulatha V03e128c2011-05-05 19:58:01 +053034static LIST_HEAD(omap_gpio_list);
35
Charulatha V6d62e212011-04-18 15:06:51 +000036struct gpio_regs {
37 u32 irqenable1;
38 u32 irqenable2;
39 u32 wake_en;
40 u32 ctrl;
41 u32 oe;
42 u32 leveldetect0;
43 u32 leveldetect1;
44 u32 risingdetect;
45 u32 fallingdetect;
46 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053047 u32 debounce;
48 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000049};
50
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010051struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053052 struct list_head node;
Tony Lindgren92105bb2005-09-07 17:20:26 +010053 void __iomem *base;
Grygorii Strashko30cefea2015-09-25 12:06:02 -070054 int irq;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080055 u32 non_wakeup_gpios;
56 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000057 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080058 u32 saved_datain;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080059 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080060 u32 toggle_mask;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +020061 raw_spinlock_t lock;
Grygorii Strashko450fa542015-09-25 12:28:03 -070062 raw_spinlock_t wa_lock;
David Brownell52e31342008-03-03 12:43:23 -080063 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080064 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080065 u32 mod_usage;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020066 u32 irq_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080067 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +053068 bool dbck_enabled;
Charulatha Vd0d665a2011-08-31 00:02:21 +053069 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080070 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053071 bool loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -050072 bool context_valid;
Tony Lindgren5de62b82010-12-07 16:26:58 -080073 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070074 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053075 int context_loss_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053076 int power_mode;
77 bool workaround_enabled;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070078
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +020079 void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053080 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070081
82 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010083};
84
Charulatha Vc8eef652011-05-02 15:21:42 +053085#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010086
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020087#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020088#define LINE_USED(line, offset) (line & (BIT(offset)))
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020089
Tony Lindgren3d009c82015-01-16 14:50:50 -080090static void omap_gpio_unmask_irq(struct irq_data *d);
91
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020092static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -060093{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +020094 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
Linus Walleijd99f7ae2015-12-07 11:16:00 +010095 return gpiochip_get_data(chip);
Benoit Cousson25db7112012-02-23 21:50:10 +010096}
97
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020098static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
99 int is_input)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100100{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100101 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100102 u32 l;
103
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700104 reg += bank->regs->direction;
Victor Kamensky661553b2013-11-16 02:01:04 +0200105 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100106 if (is_input)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200107 l |= BIT(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100108 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200109 l &= ~(BIT(gpio));
Victor Kamensky661553b2013-11-16 02:01:04 +0200110 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530111 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100112}
113
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700114
115/* set data out value using dedicate set/clear register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200116static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200117 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100118{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100119 void __iomem *reg = bank->base;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200120 u32 l = BIT(offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530122 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700123 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530124 bank->context.dataout |= l;
125 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700126 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530127 bank->context.dataout &= ~l;
128 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700129
Victor Kamensky661553b2013-11-16 02:01:04 +0200130 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700131}
132
133/* set data out value using mask register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200134static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200135 int enable)
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700136{
137 void __iomem *reg = bank->base + bank->regs->dataout;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200138 u32 gpio_bit = BIT(offset);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700139 u32 l;
140
Victor Kamensky661553b2013-11-16 02:01:04 +0200141 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700142 if (enable)
143 l |= gpio_bit;
144 else
145 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200146 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530147 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100148}
149
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200150static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100151{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700152 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200154 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100155}
156
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200157static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300158{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700159 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300160
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200161 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300162}
163
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200164static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700165{
Victor Kamensky661553b2013-11-16 02:01:04 +0200166 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700167
Benoit Cousson862ff642012-02-01 15:58:56 +0100168 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700169 l |= mask;
170 else
171 l &= ~mask;
172
Victor Kamensky661553b2013-11-16 02:01:04 +0200173 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700174}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100175
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200176static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530177{
178 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300179 clk_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530180 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300181
Victor Kamensky661553b2013-11-16 02:01:04 +0200182 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300183 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530184 }
185}
186
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200187static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530188{
189 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300190 /*
191 * Disable debounce before cutting it's clock. If debounce is
192 * enabled but the clock is not, GPIO module seems to be unable
193 * to detect events and generate interrupts at least on OMAP3.
194 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200195 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300196
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300197 clk_disable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530198 bank->dbck_enabled = false;
199 }
200}
201
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700202/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200203 * omap2_set_gpio_debounce - low level gpio debounce time
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700204 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200205 * @offset: the gpio number on this @bank
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700206 * @debounce: debounce time to use
207 *
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300208 * OMAP's debounce time is in 31us steps
209 * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
210 * so we need to convert and round up to the closest unit.
David Rivshin83977442017-04-24 18:56:50 -0400211 *
212 * Return: 0 on success, negative error otherwise.
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700213 */
David Rivshin83977442017-04-24 18:56:50 -0400214static int omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
215 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700216{
Kevin Hilman9942da02011-04-22 12:02:05 -0700217 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700218 u32 val;
219 u32 l;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300220 bool enable = !!debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700221
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800222 if (!bank->dbck_flag)
David Rivshin83977442017-04-24 18:56:50 -0400223 return -ENOTSUPP;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800224
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300225 if (enable) {
226 debounce = DIV_ROUND_UP(debounce, 31) - 1;
David Rivshin83977442017-04-24 18:56:50 -0400227 if ((debounce & OMAP4_GPIO_DEBOUNCINGTIME_MASK) != debounce)
228 return -EINVAL;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300229 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700230
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200231 l = BIT(offset);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700232
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300233 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700234 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200235 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700236
Kevin Hilman9942da02011-04-22 12:02:05 -0700237 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200238 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700239
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300240 if (enable)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700241 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530242 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700243 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300244 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700245
Victor Kamensky661553b2013-11-16 02:01:04 +0200246 writel_relaxed(val, reg);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300247 clk_disable(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530248 /*
249 * Enable debounce clock per module.
250 * This call is mandatory because in omap_gpio_request() when
251 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
252 * runtime callbck fails to turn on dbck because dbck_enable_mask
253 * used within _gpio_dbck_enable() is still not initialized at
254 * that point. Therefore we have to enable dbck here.
255 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200256 omap_gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530257 if (bank->dbck_enable_mask) {
258 bank->context.debounce = debounce;
259 bank->context.debounce_en = val;
260 }
David Rivshin83977442017-04-24 18:56:50 -0400261
262 return 0;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700263}
264
Jon Hunterc9c55d92012-10-26 14:26:04 -0500265/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200266 * omap_clear_gpio_debounce - clear debounce settings for a gpio
Jon Hunterc9c55d92012-10-26 14:26:04 -0500267 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200268 * @offset: the gpio number on this @bank
Jon Hunterc9c55d92012-10-26 14:26:04 -0500269 *
270 * If a gpio is using debounce, then clear the debounce enable bit and if
271 * this is the only gpio in this bank using debounce, then clear the debounce
272 * time too. The debounce clock will also be disabled when calling this function
273 * if this is the only gpio in the bank using debounce.
274 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200275static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
Jon Hunterc9c55d92012-10-26 14:26:04 -0500276{
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200277 u32 gpio_bit = BIT(offset);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500278
279 if (!bank->dbck_flag)
280 return;
281
282 if (!(bank->dbck_enable_mask & gpio_bit))
283 return;
284
285 bank->dbck_enable_mask &= ~gpio_bit;
286 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200287 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500288 bank->base + bank->regs->debounce_en);
289
290 if (!bank->dbck_enable_mask) {
291 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200292 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500293 bank->regs->debounce);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300294 clk_disable(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500295 bank->dbck_enabled = false;
296 }
297}
298
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200299static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530300 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100301{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800302 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200303 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100304
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200305 omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
306 trigger & IRQ_TYPE_LEVEL_LOW);
307 omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
308 trigger & IRQ_TYPE_LEVEL_HIGH);
309 omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
310 trigger & IRQ_TYPE_EDGE_RISING);
311 omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
312 trigger & IRQ_TYPE_EDGE_FALLING);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530313
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530314 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200315 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530316 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200317 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530318 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200319 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530320 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200321 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530322
323 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200324 omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530325 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200326 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530327 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530328
Ambresh K55b220c2011-06-15 13:40:45 -0700329 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530330 if (!bank->regs->irqctrl) {
331 /* On omap24xx proceed only when valid GPIO bit is set */
332 if (bank->non_wakeup_gpios) {
333 if (!(bank->non_wakeup_gpios & gpio_bit))
334 goto exit;
335 }
336
Chunqiu Wang699117a62009-06-24 17:13:39 +0000337 /*
338 * Log the edge gpio and manually trigger the IRQ
339 * after resume if the input level changes
340 * to avoid irq lost during PER RET/OFF mode
341 * Applies for omap2 non-wakeup gpio and all omap3 gpios
342 */
343 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800344 bank->enabled_non_wakeup_gpios |= gpio_bit;
345 else
346 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
347 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700348
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530349exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530350 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200351 readl_relaxed(bank->base + bank->regs->leveldetect0) |
352 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100353}
354
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800355#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800356/*
357 * This only applies to chips that can't do both rising and falling edge
358 * detection at once. For all other chips, this function is a noop.
359 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200360static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800361{
362 void __iomem *reg = bank->base;
363 u32 l = 0;
364
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530365 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800366 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530367
368 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800369
Victor Kamensky661553b2013-11-16 02:01:04 +0200370 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800371 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200372 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800373 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200374 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800375
Victor Kamensky661553b2013-11-16 02:01:04 +0200376 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800377}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530378#else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200379static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800380#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800381
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200382static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
383 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100384{
385 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530386 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100387 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100388
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530389 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200390 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530391 } else if (bank->regs->irqctrl) {
392 reg += bank->regs->irqctrl;
393
Victor Kamensky661553b2013-11-16 02:01:04 +0200394 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000395 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200396 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100397 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200398 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100399 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200400 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100401 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530402 return -EINVAL;
403
Victor Kamensky661553b2013-11-16 02:01:04 +0200404 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530405 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100406 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530407 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100408 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530409 reg += bank->regs->edgectrl1;
410
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100411 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200412 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100413 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100414 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100415 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100416 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200417 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530418
419 /* Enable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200420 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530421 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200422 readl_relaxed(bank->base + bank->regs->wkup_en);
423 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100424 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100425 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100426}
427
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200428static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200429{
430 if (bank->regs->pinctrl) {
431 void __iomem *reg = bank->base + bank->regs->pinctrl;
432
433 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200434 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200435 }
436
437 if (bank->regs->ctrl && !BANK_USED(bank)) {
438 void __iomem *reg = bank->base + bank->regs->ctrl;
439 u32 ctrl;
440
Victor Kamensky661553b2013-11-16 02:01:04 +0200441 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200442 /* Module is enabled, clocks are not gated */
443 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200444 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200445 bank->context.ctrl = ctrl;
446 }
447}
448
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200449static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200450{
451 void __iomem *base = bank->base;
452
453 if (bank->regs->wkup_en &&
454 !LINE_USED(bank->mod_usage, offset) &&
455 !LINE_USED(bank->irq_usage, offset)) {
456 /* Disable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200457 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200458 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200459 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200460 }
461
462 if (bank->regs->ctrl && !BANK_USED(bank)) {
463 void __iomem *reg = bank->base + bank->regs->ctrl;
464 u32 ctrl;
465
Victor Kamensky661553b2013-11-16 02:01:04 +0200466 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200467 /* Module is disabled, clocks are gated */
468 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200469 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200470 bank->context.ctrl = ctrl;
471 }
472}
473
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200474static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200475{
476 void __iomem *reg = bank->base + bank->regs->direction;
477
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200478 return readl_relaxed(reg) & BIT(offset);
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200479}
480
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200481static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
Tony Lindgren3d009c82015-01-16 14:50:50 -0800482{
483 if (!LINE_USED(bank->mod_usage, offset)) {
484 omap_enable_gpio_module(bank, offset);
485 omap_set_gpio_direction(bank, offset, 1);
486 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200487 bank->irq_usage |= BIT(offset);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800488}
489
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200490static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100491{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200492 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100493 int retval;
David Brownella6472532008-03-03 04:33:30 -0800494 unsigned long flags;
Grygorii Strashkoea5fbe82015-03-23 14:18:29 +0200495 unsigned offset = d->hwirq;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100496
David Brownelle5c56ed2006-12-06 17:13:59 -0800497 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100498 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800499
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530500 if (!bank->regs->leveldetect0 &&
501 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100502 return -EINVAL;
503
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200504 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200505 retval = omap_set_gpio_triggering(bank, offset, type);
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300506 if (retval) {
Axel Lin627c89b2015-08-05 22:37:41 +0800507 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300508 goto error;
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300509 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200510 omap_gpio_init_irq(bank, offset);
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200511 if (!omap_gpio_is_input(bank, offset)) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200512 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300513 retval = -EINVAL;
514 goto error;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200515 }
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200516 raw_spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800517
518 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner43ec2e42015-06-23 15:52:39 +0200519 irq_set_handler_locked(d, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800520 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500521 /*
522 * Edge IRQs are already cleared/acked in irq_handler and
523 * not need to be masked, as result handle_edge_irq()
524 * logic is excessed here and may cause lose of interrupts.
525 * So just use handle_simple_irq.
526 */
527 irq_set_handler_locked(d, handle_simple_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800528
Grygorii Strashko1562e462015-05-22 17:35:49 +0300529 return 0;
530
531error:
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/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200626static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100627{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200628 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100629
Grygorii Strashko0c0451e2016-04-12 13:52:31 +0300630 return irq_set_irq_wake(bank->irq, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100631}
632
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800633static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100634{
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100635 struct gpio_bank *bank = gpiochip_get_data(chip);
David Brownella6472532008-03-03 04:33:30 -0800636 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100637
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530638 /*
639 * If this is the first gpio_request for the bank,
640 * enable the bank module.
641 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200642 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200643 pm_runtime_get_sync(chip->parent);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100644
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200645 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashkoc3518172015-05-22 17:35:51 +0300646 omap_enable_gpio_module(bank, offset);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200647 bank->mod_usage |= BIT(offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200648 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100649
650 return 0;
651}
652
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800653static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100654{
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100655 struct gpio_bank *bank = gpiochip_get_data(chip);
David Brownella6472532008-03-03 04:33:30 -0800656 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100657
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200658 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200659 bank->mod_usage &= ~(BIT(offset));
Grygorii Strashko5f982c72015-05-22 17:35:48 +0300660 if (!LINE_USED(bank->irq_usage, offset)) {
661 omap_set_gpio_direction(bank, offset, 1);
662 omap_clear_gpio_debounce(bank, offset);
663 }
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200664 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200665 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530666
667 /*
668 * If this is the last gpio to be freed in the bank,
669 * disable the bank module.
670 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200671 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200672 pm_runtime_put(chip->parent);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100673}
674
675/*
676 * We need to unmask the GPIO bank interrupt as soon as possible to
677 * avoid missing GPIO interrupts for other lines in the bank.
678 * Then we need to mask-read-clear-unmask the triggered GPIO lines
679 * in the bank to avoid missing nested interrupts for a GPIO line.
680 * If we wait to unmask individual GPIO lines in the bank after the
681 * line's interrupt handler has been run, we may miss some nested
682 * interrupts.
683 */
Grygorii Strashko450fa542015-09-25 12:28:03 -0700684static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100685{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100686 void __iomem *isr_reg = NULL;
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500687 u32 enabled, isr, level_mask;
Jon Hunter3513cde2013-04-04 15:16:14 -0500688 unsigned int bit;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700689 struct gpio_bank *bank = gpiobank;
690 unsigned long wa_lock_flags;
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300691 unsigned long lock_flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100692
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700693 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800694 if (WARN_ON(!isr_reg))
695 goto exit;
696
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200697 pm_runtime_get_sync(bank->chip.parent);
Grygorii Strashko450fa542015-09-25 12:28:03 -0700698
Laurent Navete83507b2013-03-20 13:15:57 +0100699 while (1) {
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300700 raw_spin_lock_irqsave(&bank->lock, lock_flags);
701
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200702 enabled = omap_get_gpio_irqbank_mask(bank);
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500703 isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100704
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530705 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800706 level_mask = bank->level_mask & enabled;
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500707 else
708 level_mask = 0;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100709
710 /* clear edge sensitive interrupts before handler(s) are
711 called so that we don't miss any interrupt occurred while
712 executing them */
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500713 if (isr & ~level_mask)
714 omap_clear_gpio_irqbank(bank, isr & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100715
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300716 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
717
Tony Lindgren92105bb2005-09-07 17:20:26 +0100718 if (!isr)
719 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100720
Jon Hunter3513cde2013-04-04 15:16:14 -0500721 while (isr) {
722 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200723 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100724
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300725 raw_spin_lock_irqsave(&bank->lock, lock_flags);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800726 /*
727 * Some chips can't respond to both rising and falling
728 * at the same time. If this irq was requested with
729 * both flags, we need to flip the ICR data for the IRQ
730 * to respond to the IRQ for the opposite direction.
731 * This will be indicated in the bank toggle_mask.
732 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200733 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200734 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800735
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300736 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
737
Grygorii Strashko450fa542015-09-25 12:28:03 -0700738 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
739
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100740 generic_handle_irq(irq_find_mapping(bank->chip.irq.domain,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200741 bit));
Grygorii Strashko450fa542015-09-25 12:28:03 -0700742
743 raw_spin_unlock_irqrestore(&bank->wa_lock,
744 wa_lock_flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100745 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000746 }
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800747exit:
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200748 pm_runtime_put(bank->chip.parent);
Grygorii Strashko450fa542015-09-25 12:28:03 -0700749 return IRQ_HANDLED;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100750}
751
Tony Lindgren3d009c82015-01-16 14:50:50 -0800752static unsigned int omap_gpio_irq_startup(struct irq_data *d)
753{
754 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800755 unsigned long flags;
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200756 unsigned offset = d->hwirq;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800757
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200758 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300759
760 if (!LINE_USED(bank->mod_usage, offset))
761 omap_set_gpio_direction(bank, offset, 1);
762 else if (!omap_gpio_is_input(bank, offset))
763 goto err;
764 omap_enable_gpio_module(bank, offset);
765 bank->irq_usage |= BIT(offset);
766
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200767 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800768 omap_gpio_unmask_irq(d);
769
770 return 0;
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300771err:
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200772 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300773 return -EINVAL;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800774}
775
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200776static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300777{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200778 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700779 unsigned long flags;
Grygorii Strashko9943f262015-03-23 14:18:27 +0200780 unsigned offset = d->hwirq;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300781
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200782 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200783 bank->irq_usage &= ~(BIT(offset));
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300784 omap_set_gpio_irqenable(bank, offset, 0);
785 omap_clear_gpio_irqstatus(bank, offset);
786 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
787 if (!LINE_USED(bank->mod_usage, offset))
788 omap_clear_gpio_debounce(bank, offset);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200789 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200790 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700791}
792
793static void omap_gpio_irq_bus_lock(struct irq_data *data)
794{
795 struct gpio_bank *bank = omap_irq_data_get_bank(data);
796
797 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200798 pm_runtime_get_sync(bank->chip.parent);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700799}
800
801static void gpio_irq_bus_sync_unlock(struct irq_data *data)
802{
803 struct gpio_bank *bank = omap_irq_data_get_bank(data);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200804
805 /*
806 * If this is the last IRQ to be freed in the bank,
807 * disable the bank module.
808 */
809 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200810 pm_runtime_put(bank->chip.parent);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300811}
812
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200813static void omap_gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100814{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200815 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200816 unsigned offset = d->hwirq;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100817
Grygorii Strashko9943f262015-03-23 14:18:27 +0200818 omap_clear_gpio_irqstatus(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100819}
820
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200821static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100822{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200823 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200824 unsigned offset = d->hwirq;
Colin Cross85ec7b92011-06-06 13:38:18 -0700825 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100826
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200827 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200828 omap_set_gpio_irqenable(bank, offset, 0);
829 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200830 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100831}
832
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200833static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100834{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200835 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200836 unsigned offset = d->hwirq;
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100837 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700838 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700839
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200840 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700841 if (trigger)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200842 omap_set_gpio_triggering(bank, offset, trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800843
844 /* For level-triggered GPIOs, the clearing must be done after
845 * the HW source is cleared, thus after the handler has run */
Grygorii Strashko9943f262015-03-23 14:18:27 +0200846 if (bank->level_mask & BIT(offset)) {
847 omap_set_gpio_irqenable(bank, offset, 0);
848 omap_clear_gpio_irqstatus(bank, offset);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800849 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100850
Grygorii Strashko9943f262015-03-23 14:18:27 +0200851 omap_set_gpio_irqenable(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200852 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100853}
854
David Brownelle5c56ed2006-12-06 17:13:59 -0800855/*---------------------------------------------------------------------*/
856
Magnus Damm79ee0312009-07-08 13:22:04 +0200857static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800858{
Magnus Damm79ee0312009-07-08 13:22:04 +0200859 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800860 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800861 void __iomem *mask_reg = bank->base +
862 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800863 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800864
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200865 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200866 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200867 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800868
869 return 0;
870}
871
Magnus Damm79ee0312009-07-08 13:22:04 +0200872static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800873{
Magnus Damm79ee0312009-07-08 13:22:04 +0200874 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800875 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800876 void __iomem *mask_reg = bank->base +
877 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800878 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800879
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200880 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200881 writel_relaxed(bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200882 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800883
884 return 0;
885}
886
Alexey Dobriyan47145212009-12-14 18:00:08 -0800887static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200888 .suspend_noirq = omap_mpuio_suspend_noirq,
889 .resume_noirq = omap_mpuio_resume_noirq,
890};
891
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200892/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800893static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800894 .driver = {
895 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200896 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800897 },
898};
899
900static struct platform_device omap_mpuio_device = {
901 .name = "mpuio",
902 .id = -1,
903 .dev = {
904 .driver = &omap_mpuio_driver.driver,
905 }
906 /* could list the /proc/iomem resources */
907};
908
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200909static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800910{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800911 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700912
David Brownell11a78b72006-12-06 17:14:11 -0800913 if (platform_driver_register(&omap_mpuio_driver) == 0)
914 (void) platform_device_register(&omap_mpuio_device);
915}
916
David Brownelle5c56ed2006-12-06 17:13:59 -0800917/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100918
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200919static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200920{
921 struct gpio_bank *bank;
922 unsigned long flags;
923 void __iomem *reg;
924 int dir;
925
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100926 bank = gpiochip_get_data(chip);
Yegor Yefremov93700842014-04-24 08:57:39 +0200927 reg = bank->base + bank->regs->direction;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200928 raw_spin_lock_irqsave(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +0200929 dir = !!(readl_relaxed(reg) & BIT(offset));
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200930 raw_spin_unlock_irqrestore(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +0200931 return dir;
932}
933
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200934static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800935{
936 struct gpio_bank *bank;
937 unsigned long flags;
938
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100939 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200940 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200941 omap_set_gpio_direction(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200942 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800943 return 0;
944}
945
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200946static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800947{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300948 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300949
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100950 bank = gpiochip_get_data(chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300951
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200952 if (omap_gpio_is_input(bank, offset))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200953 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300954 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200955 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -0800956}
957
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200958static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800959{
960 struct gpio_bank *bank;
961 unsigned long flags;
962
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100963 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200964 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700965 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200966 omap_set_gpio_direction(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200967 raw_spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200968 return 0;
David Brownell52e31342008-03-03 12:43:23 -0800969}
970
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200971static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
972 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700973{
974 struct gpio_bank *bank;
975 unsigned long flags;
David Rivshin83977442017-04-24 18:56:50 -0400976 int ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700977
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100978 bank = gpiochip_get_data(chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800979
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200980 raw_spin_lock_irqsave(&bank->lock, flags);
David Rivshin83977442017-04-24 18:56:50 -0400981 ret = omap2_set_gpio_debounce(bank, offset, debounce);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200982 raw_spin_unlock_irqrestore(&bank->lock, flags);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700983
David Rivshin83977442017-04-24 18:56:50 -0400984 if (ret)
985 dev_info(chip->parent,
986 "Could not set line %u debounce to %u microseconds (%d)",
987 offset, debounce, ret);
988
989 return ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700990}
991
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300992static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset,
993 unsigned long config)
994{
995 u32 debounce;
996
997 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
998 return -ENOTSUPP;
999
1000 debounce = pinconf_to_config_argument(config);
1001 return omap_gpio_debounce(chip, offset, debounce);
1002}
1003
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001004static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001005{
1006 struct gpio_bank *bank;
1007 unsigned long flags;
1008
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001009 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001010 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001011 bank->set_dataout(bank, offset, value);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001012 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -08001013}
1014
1015/*---------------------------------------------------------------------*/
1016
Arnd Bergmanne4b2ae72017-09-16 22:42:21 +02001017static void omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001018{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001019 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001020 u32 rev;
1021
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001022 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001023 return;
1024
Victor Kamensky661553b2013-11-16 02:01:04 +02001025 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001026 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001027 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001028
1029 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001030}
1031
Charulatha V03e128c2011-05-05 19:58:01 +05301032static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001033{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301034 void __iomem *base = bank->base;
1035 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001036
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301037 if (bank->width == 16)
1038 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001039
Charulatha Vd0d665a2011-08-31 00:02:21 +05301040 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001041 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301042 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001043 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301044
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001045 omap_gpio_rmw(base, bank->regs->irqenable, l,
1046 bank->regs->irqenable_inv);
1047 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1048 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301049 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001050 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301051
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301052 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001053 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301054 /* Initialize interface clk ungated, module enabled */
1055 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001056 writel_relaxed(0, base + bank->regs->ctrl);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001057}
1058
Nishanth Menon46824e222014-09-05 14:52:55 -05001059static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001060{
Grygorii Strashko81930322017-11-15 12:36:33 -06001061 struct gpio_irq_chip *irq;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001062 static int gpio;
Linus Walleij088413b2017-12-29 13:22:58 +01001063 const char *label;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001064 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001065 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001066
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001067 /*
1068 * REVISIT eventually switch from OMAP-specific gpio structs
1069 * over to the generic ones
1070 */
1071 bank->chip.request = omap_gpio_request;
1072 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001073 bank->chip.get_direction = omap_gpio_get_direction;
1074 bank->chip.direction_input = omap_gpio_input;
1075 bank->chip.get = omap_gpio_get;
1076 bank->chip.direction_output = omap_gpio_output;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001077 bank->chip.set_config = omap_gpio_set_config;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001078 bank->chip.set = omap_gpio_set;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301079 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001080 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301081 if (bank->regs->wkup_en)
Linus Walleij58383c782015-11-04 09:56:26 +01001082 bank->chip.parent = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001083 bank->chip.base = OMAP_MPUIO(0);
1084 } else {
Linus Walleij088413b2017-12-29 13:22:58 +01001085 label = devm_kasprintf(bank->chip.parent, GFP_KERNEL, "gpio-%d-%d",
1086 gpio, gpio + bank->width - 1);
1087 if (!label)
1088 return -ENOMEM;
1089 bank->chip.label = label;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001090 bank->chip.base = gpio;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001091 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001092 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001093
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001094#ifdef CONFIG_ARCH_OMAP1
1095 /*
1096 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1097 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1098 */
Bartosz Golaszewski2ed36f32017-03-04 17:23:31 +01001099 irq_base = devm_irq_alloc_descs(bank->chip.parent,
1100 -1, 0, bank->width, 0);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001101 if (irq_base < 0) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001102 dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n");
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001103 return -ENODEV;
1104 }
1105#endif
1106
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001107 /* MPUIO is a bit different, reading IRQ status clears it */
1108 if (bank->is_mpuio) {
1109 irqc->irq_ack = dummy_irq_chip.irq_ack;
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001110 if (!bank->regs->wkup_en)
1111 irqc->irq_set_wake = NULL;
1112 }
1113
Grygorii Strashko81930322017-11-15 12:36:33 -06001114 irq = &bank->chip.irq;
1115 irq->chip = irqc;
1116 irq->handler = handle_bad_irq;
1117 irq->default_type = IRQ_TYPE_NONE;
1118 irq->num_parents = 1;
1119 irq->parents = &bank->irq;
1120 irq->first = irq_base;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001121
Grygorii Strashko81930322017-11-15 12:36:33 -06001122 ret = gpiochip_add_data(&bank->chip, bank);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001123 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001124 dev_err(bank->chip.parent,
Grygorii Strashko81930322017-11-15 12:36:33 -06001125 "Could not register gpio chip %d\n", ret);
1126 return ret;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001127 }
1128
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001129 ret = devm_request_irq(bank->chip.parent, bank->irq,
1130 omap_gpio_irq_handler,
1131 0, dev_name(bank->chip.parent), bank);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001132 if (ret)
1133 gpiochip_remove(&bank->chip);
1134
Grygorii Strashko81930322017-11-15 12:36:33 -06001135 if (!bank->is_mpuio)
1136 gpio += bank->width;
1137
Grygorii Strashko450fa542015-09-25 12:28:03 -07001138 return ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001139}
1140
Benoit Cousson384ebe12011-08-16 11:53:02 +02001141static const struct of_device_id omap_gpio_match[];
1142
Bill Pemberton38363092012-11-19 13:22:34 -05001143static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001144{
Benoit Cousson862ff642012-02-01 15:58:56 +01001145 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001146 struct device_node *node = dev->of_node;
1147 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001148 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001149 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001150 struct gpio_bank *bank;
Nishanth Menon46824e222014-09-05 14:52:55 -05001151 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001152 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001153
Benoit Cousson384ebe12011-08-16 11:53:02 +02001154 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1155
Jingoo Hane56aee12013-07-30 17:08:05 +09001156 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001157 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001158 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001159
Tobias Klauser086d5852012-10-05 11:37:38 +02001160 bank = devm_kzalloc(dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301161 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001162 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001163 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301164 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001165
Nishanth Menon46824e222014-09-05 14:52:55 -05001166 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1167 if (!irqc)
1168 return -ENOMEM;
1169
Tony Lindgren3d009c82015-01-16 14:50:50 -08001170 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e222014-09-05 14:52:55 -05001171 irqc->irq_shutdown = omap_gpio_irq_shutdown,
1172 irqc->irq_ack = omap_gpio_ack_irq,
1173 irqc->irq_mask = omap_gpio_mask_irq,
1174 irqc->irq_unmask = omap_gpio_unmask_irq,
1175 irqc->irq_set_type = omap_gpio_irq_type,
1176 irqc->irq_set_wake = omap_gpio_wake_enable,
Grygorii Strashkoaca82d12015-09-25 12:28:02 -07001177 irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
1178 irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
Nishanth Menon46824e222014-09-05 14:52:55 -05001179 irqc->name = dev_name(&pdev->dev);
Grygorii Strashko0c0451e2016-04-12 13:52:31 +03001180 irqc->flags = IRQCHIP_MASK_ON_SUSPEND;
Nishanth Menon46824e222014-09-05 14:52:55 -05001181
Grygorii Strashko89d18e32015-08-18 14:10:53 +03001182 bank->irq = platform_get_irq(pdev, 0);
1183 if (bank->irq <= 0) {
1184 if (!bank->irq)
1185 bank->irq = -ENXIO;
1186 if (bank->irq != -EPROBE_DEFER)
1187 dev_err(dev,
1188 "can't get irq resource ret=%d\n", bank->irq);
1189 return bank->irq;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001190 }
1191
Linus Walleij58383c782015-11-04 09:56:26 +01001192 bank->chip.parent = dev;
Grygorii Strashkoc23837c2015-06-25 18:13:33 +03001193 bank->chip.owner = THIS_MODULE;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001194 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001195 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001196 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301197 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301198 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001199 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001200#ifdef CONFIG_OF_GPIO
1201 bank->chip.of_node = of_node_get(node);
1202#endif
Jon Huntera2797be2013-04-04 15:16:15 -05001203 if (node) {
1204 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1205 bank->loses_context = true;
1206 } else {
1207 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001208
1209 if (bank->loses_context)
1210 bank->get_context_loss_count =
1211 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001212 }
1213
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001214 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001215 bank->set_dataout = omap_set_gpio_dataout_reg;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001216 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001217 bank->set_dataout = omap_set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001218
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001219 raw_spin_lock_init(&bank->lock);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001220 raw_spin_lock_init(&bank->wa_lock);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001221
1222 /* Static mapping, never released */
1223 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001224 bank->base = devm_ioremap_resource(dev, res);
1225 if (IS_ERR(bank->base)) {
Jingoo Han717f70e2014-02-12 11:51:38 +09001226 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001227 }
1228
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001229 if (bank->dbck_flag) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001230 bank->dbck = devm_clk_get(dev, "dbclk");
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001231 if (IS_ERR(bank->dbck)) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001232 dev_err(dev,
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001233 "Could not get gpio dbck. Disable debounce\n");
1234 bank->dbck_flag = false;
1235 } else {
1236 clk_prepare(bank->dbck);
1237 }
1238 }
1239
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301240 platform_set_drvdata(pdev, bank);
1241
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001242 pm_runtime_enable(dev);
1243 pm_runtime_irq_safe(dev);
1244 pm_runtime_get_sync(dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001245
Charulatha Vd0d665a2011-08-31 00:02:21 +05301246 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001247 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301248
Charulatha V03e128c2011-05-05 19:58:01 +05301249 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001250
Nishanth Menon46824e222014-09-05 14:52:55 -05001251 ret = omap_gpio_chip_init(bank, irqc);
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001252 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001253 pm_runtime_put_sync(dev);
1254 pm_runtime_disable(dev);
Arvind Yadave2c3c192017-08-01 12:14:31 +05301255 if (bank->dbck_flag)
1256 clk_unprepare(bank->dbck);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001257 return ret;
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001258 }
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001259
Tony Lindgren9a748052010-12-07 16:26:56 -08001260 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001261
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001262 pm_runtime_put(dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301263
Charulatha V03e128c2011-05-05 19:58:01 +05301264 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001265
Jon Hunter879fe322013-04-04 15:16:12 -05001266 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001267}
1268
Tony Lindgrencac089f2015-04-23 16:56:22 -07001269static int omap_gpio_remove(struct platform_device *pdev)
1270{
1271 struct gpio_bank *bank = platform_get_drvdata(pdev);
1272
1273 list_del(&bank->node);
1274 gpiochip_remove(&bank->chip);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001275 pm_runtime_disable(&pdev->dev);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001276 if (bank->dbck_flag)
1277 clk_unprepare(bank->dbck);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001278
1279 return 0;
1280}
1281
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301282#ifdef CONFIG_ARCH_OMAP2PLUS
1283
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001284#if defined(CONFIG_PM)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301285static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001286
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301287static int omap_gpio_runtime_suspend(struct device *dev)
1288{
1289 struct platform_device *pdev = to_platform_device(dev);
1290 struct gpio_bank *bank = platform_get_drvdata(pdev);
1291 u32 l1 = 0, l2 = 0;
1292 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001293 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301294
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001295 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001296
1297 /*
1298 * Only edges can generate a wakeup event to the PRCM.
1299 *
1300 * Therefore, ensure any wake-up capable GPIOs have
1301 * edge-detection enabled before going idle to ensure a wakeup
1302 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1303 * NDA TRM 25.5.3.1)
1304 *
1305 * The normal values will be restored upon ->runtime_resume()
1306 * by writing back the values saved in bank->context.
1307 */
1308 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1309 if (wake_low)
Victor Kamensky661553b2013-11-16 02:01:04 +02001310 writel_relaxed(wake_low | bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001311 bank->base + bank->regs->fallingdetect);
1312 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1313 if (wake_hi)
Victor Kamensky661553b2013-11-16 02:01:04 +02001314 writel_relaxed(wake_hi | bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001315 bank->base + bank->regs->risingdetect);
1316
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001317 if (!bank->enabled_non_wakeup_gpios)
1318 goto update_gpio_context_count;
1319
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301320 if (bank->power_mode != OFF_MODE) {
1321 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301322 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301323 }
1324 /*
1325 * If going to OFF, remove triggering for all
1326 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1327 * generated. See OMAP2420 Errata item 1.101.
1328 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001329 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301330 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301331 l1 = bank->context.fallingdetect;
1332 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301333
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301334 l1 &= ~bank->enabled_non_wakeup_gpios;
1335 l2 &= ~bank->enabled_non_wakeup_gpios;
1336
Victor Kamensky661553b2013-11-16 02:01:04 +02001337 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1338 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301339
1340 bank->workaround_enabled = true;
1341
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301342update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301343 if (bank->get_context_loss_count)
1344 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001345 bank->get_context_loss_count(dev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301346
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001347 omap_gpio_dbck_disable(bank);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001348 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301349
1350 return 0;
1351}
1352
Jon Hunter352a2d52013-04-15 13:06:54 -05001353static void omap_gpio_init_context(struct gpio_bank *p);
1354
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301355static int omap_gpio_runtime_resume(struct device *dev)
1356{
1357 struct platform_device *pdev = to_platform_device(dev);
1358 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301359 u32 l = 0, gen, gen0, gen1;
1360 unsigned long flags;
Jon Huntera2797be2013-04-04 15:16:15 -05001361 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301362
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001363 raw_spin_lock_irqsave(&bank->lock, flags);
Jon Hunter352a2d52013-04-15 13:06:54 -05001364
1365 /*
1366 * On the first resume during the probe, the context has not
1367 * been initialised and so initialise it now. Also initialise
1368 * the context loss count.
1369 */
1370 if (bank->loses_context && !bank->context_valid) {
1371 omap_gpio_init_context(bank);
1372
1373 if (bank->get_context_loss_count)
1374 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001375 bank->get_context_loss_count(dev);
Jon Hunter352a2d52013-04-15 13:06:54 -05001376 }
1377
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001378 omap_gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001379
1380 /*
1381 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1382 * GPIOs were set to edge trigger also in order to be able to
1383 * generate a PRCM wakeup. Here we restore the
1384 * pre-runtime_suspend() values for edge triggering.
1385 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001386 writel_relaxed(bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001387 bank->base + bank->regs->fallingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001388 writel_relaxed(bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001389 bank->base + bank->regs->risingdetect);
1390
Jon Huntera2797be2013-04-04 15:16:15 -05001391 if (bank->loses_context) {
1392 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301393 omap_gpio_restore_context(bank);
1394 } else {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001395 c = bank->get_context_loss_count(dev);
Jon Huntera2797be2013-04-04 15:16:15 -05001396 if (c != bank->context_loss_count) {
1397 omap_gpio_restore_context(bank);
1398 } else {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001399 raw_spin_unlock_irqrestore(&bank->lock, flags);
Jon Huntera2797be2013-04-04 15:16:15 -05001400 return 0;
1401 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301402 }
1403 }
1404
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301405 if (!bank->workaround_enabled) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001406 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301407 return 0;
1408 }
1409
Victor Kamensky661553b2013-11-16 02:01:04 +02001410 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301411
1412 /*
1413 * Check if any of the non-wakeup interrupt GPIOs have changed
1414 * state. If so, generate an IRQ by software. This is
1415 * horribly racy, but it's the best we can do to work around
1416 * this silicon bug.
1417 */
1418 l ^= bank->saved_datain;
1419 l &= bank->enabled_non_wakeup_gpios;
1420
1421 /*
1422 * No need to generate IRQs for the rising edge for gpio IRQs
1423 * configured with falling edge only; and vice versa.
1424 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301425 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301426 gen0 &= bank->saved_datain;
1427
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301428 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301429 gen1 &= ~(bank->saved_datain);
1430
1431 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301432 gen = l & (~(bank->context.fallingdetect) &
1433 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301434 /* Consider all GPIO IRQs needed to be updated */
1435 gen |= gen0 | gen1;
1436
1437 if (gen) {
1438 u32 old0, old1;
1439
Victor Kamensky661553b2013-11-16 02:01:04 +02001440 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1441 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301442
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301443 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001444 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301445 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001446 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301447 bank->regs->leveldetect1);
1448 }
1449
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301450 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001451 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301452 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001453 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301454 bank->regs->leveldetect1);
1455 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001456 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1457 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301458 }
1459
1460 bank->workaround_enabled = false;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001461 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301462
1463 return 0;
1464}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001465#endif /* CONFIG_PM */
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301466
Tony Lindgrencac089f2015-04-23 16:56:22 -07001467#if IS_BUILTIN(CONFIG_GPIO_OMAP)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301468void 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
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001478 pm_runtime_put_sync_suspend(bank->chip.parent);
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
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001490 pm_runtime_get_sync(bank->chip.parent);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001491 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001492}
Tony Lindgrencac089f2015-04-23 16:56:22 -07001493#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001494
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001495#if defined(CONFIG_PM)
Jon Hunter352a2d52013-04-15 13:06:54 -05001496static void omap_gpio_init_context(struct gpio_bank *p)
1497{
1498 struct omap_gpio_reg_offs *regs = p->regs;
1499 void __iomem *base = p->base;
1500
Victor Kamensky661553b2013-11-16 02:01:04 +02001501 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1502 p->context.oe = readl_relaxed(base + regs->direction);
1503 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1504 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1505 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1506 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1507 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1508 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1509 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001510
1511 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001512 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001513 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001514 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001515
1516 p->context_valid = true;
1517}
1518
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301519static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301520{
Victor Kamensky661553b2013-11-16 02:01:04 +02001521 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301522 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001523 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1524 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301525 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001526 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301527 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001528 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301529 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001530 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301531 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301532 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001533 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301534 bank->base + bank->regs->set_dataout);
1535 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001536 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301537 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001538 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301539
Nishanth Menonae547352011-09-09 19:08:58 +05301540 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001541 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301542 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001543 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301544 bank->base + bank->regs->debounce_en);
1545 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301546
Victor Kamensky661553b2013-11-16 02:01:04 +02001547 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301548 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001549 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301550 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301551}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001552#endif /* CONFIG_PM */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301553#else
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301554#define omap_gpio_runtime_suspend NULL
1555#define omap_gpio_runtime_resume NULL
Arnd Bergmannea4a21a2013-05-31 17:59:46 +02001556static inline void omap_gpio_init_context(struct gpio_bank *p) {}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301557#endif
1558
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301559static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301560 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1561 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301562};
1563
Benoit Cousson384ebe12011-08-16 11:53:02 +02001564#if defined(CONFIG_OF)
1565static struct omap_gpio_reg_offs omap2_gpio_regs = {
1566 .revision = OMAP24XX_GPIO_REVISION,
1567 .direction = OMAP24XX_GPIO_OE,
1568 .datain = OMAP24XX_GPIO_DATAIN,
1569 .dataout = OMAP24XX_GPIO_DATAOUT,
1570 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1571 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1572 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1573 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1574 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1575 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1576 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1577 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1578 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1579 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1580 .ctrl = OMAP24XX_GPIO_CTRL,
1581 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1582 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1583 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1584 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1585 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1586};
1587
1588static struct omap_gpio_reg_offs omap4_gpio_regs = {
1589 .revision = OMAP4_GPIO_REVISION,
1590 .direction = OMAP4_GPIO_OE,
1591 .datain = OMAP4_GPIO_DATAIN,
1592 .dataout = OMAP4_GPIO_DATAOUT,
1593 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1594 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1595 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1596 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1597 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1598 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1599 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1600 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1601 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1602 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1603 .ctrl = OMAP4_GPIO_CTRL,
1604 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1605 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1606 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1607 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1608 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1609};
1610
Chen Gange9a65bb2013-02-06 18:44:32 +08001611static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001612 .regs = &omap2_gpio_regs,
1613 .bank_width = 32,
1614 .dbck_flag = false,
1615};
1616
Chen Gange9a65bb2013-02-06 18:44:32 +08001617static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001618 .regs = &omap2_gpio_regs,
1619 .bank_width = 32,
1620 .dbck_flag = true,
1621};
1622
Chen Gange9a65bb2013-02-06 18:44:32 +08001623static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001624 .regs = &omap4_gpio_regs,
1625 .bank_width = 32,
1626 .dbck_flag = true,
1627};
1628
1629static const struct of_device_id omap_gpio_match[] = {
1630 {
1631 .compatible = "ti,omap4-gpio",
1632 .data = &omap4_pdata,
1633 },
1634 {
1635 .compatible = "ti,omap3-gpio",
1636 .data = &omap3_pdata,
1637 },
1638 {
1639 .compatible = "ti,omap2-gpio",
1640 .data = &omap2_pdata,
1641 },
1642 { },
1643};
1644MODULE_DEVICE_TABLE(of, omap_gpio_match);
1645#endif
1646
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001647static struct platform_driver omap_gpio_driver = {
1648 .probe = omap_gpio_probe,
Tony Lindgrencac089f2015-04-23 16:56:22 -07001649 .remove = omap_gpio_remove,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001650 .driver = {
1651 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301652 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001653 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001654 },
1655};
1656
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001657/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001658 * gpio driver register needs to be done before
1659 * machine_init functions access gpio APIs.
1660 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001661 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001662static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001663{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001664 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001665}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001666postcore_initcall(omap_gpio_drv_reg);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001667
1668static void __exit omap_gpio_exit(void)
1669{
1670 platform_driver_unregister(&omap_gpio_driver);
1671}
1672module_exit(omap_gpio_exit);
1673
1674MODULE_DESCRIPTION("omap gpio driver");
1675MODULE_ALIAS("platform:gpio-omap");
1676MODULE_LICENSE("GPL v2");