blob: 5b3e83cd71378b0ee81f83a51e4a69380d752fef [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>
Tony Lindgrenb764a582018-09-20 12:35:31 -070022#include <linux/cpu_pm.h>
Benoit Cousson96751fc2012-02-01 16:01:39 +010023#include <linux/device.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080024#include <linux/pm_runtime.h>
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +053025#include <linux/pm.h>
Benoit Cousson384ebe12011-08-16 11:53:02 +020026#include <linux/of.h>
27#include <linux/of_device.h>
Linus Walleijb7351b02018-05-24 14:24:00 +020028#include <linux/gpio/driver.h>
Yegor Yefremov93700842014-04-24 08:57:39 +020029#include <linux/bitops.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070030#include <linux/platform_data/gpio-omap.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +030032#define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053033
Tony Lindgrenb764a582018-09-20 12:35:31 -070034#define OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER BIT(2)
Tony Lindgrenec0daae2018-09-20 12:35:30 -070035
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 Lindgrenec0daae2018-09-20 12:35:30 -070051struct gpio_bank;
52
53struct gpio_omap_funcs {
54 void (*idle_enable_level_quirk)(struct gpio_bank *bank);
55 void (*idle_disable_level_quirk)(struct gpio_bank *bank);
56};
57
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010058struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053059 struct list_head node;
Tony Lindgren92105bb2005-09-07 17:20:26 +010060 void __iomem *base;
Grygorii Strashko30cefea2015-09-25 12:06:02 -070061 int irq;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080062 u32 non_wakeup_gpios;
63 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000064 struct gpio_regs context;
Tony Lindgrenec0daae2018-09-20 12:35:30 -070065 struct gpio_omap_funcs funcs;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080066 u32 saved_datain;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080067 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080068 u32 toggle_mask;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +020069 raw_spinlock_t lock;
Grygorii Strashko450fa542015-09-25 12:28:03 -070070 raw_spinlock_t wa_lock;
David Brownell52e31342008-03-03 12:43:23 -080071 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080072 struct clk *dbck;
Tony Lindgrenb764a582018-09-20 12:35:31 -070073 struct notifier_block nb;
74 unsigned int is_suspended:1;
Charulatha V058af1e2009-11-22 10:11:25 -080075 u32 mod_usage;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020076 u32 irq_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080077 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +053078 bool dbck_enabled;
Charulatha Vd0d665a2011-08-31 00:02:21 +053079 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080080 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053081 bool loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -050082 bool context_valid;
Tony Lindgren5de62b82010-12-07 16:26:58 -080083 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070084 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053085 int context_loss_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053086 bool workaround_enabled;
Tony Lindgrenec0daae2018-09-20 12:35:30 -070087 u32 quirks;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070088
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +020089 void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable);
Janusz Krzysztofik442af142018-07-19 01:57:08 +020090 void (*set_dataout_multiple)(struct gpio_bank *bank,
91 unsigned long *mask, unsigned long *bits);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053092 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070093
94 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010095};
96
Charulatha Vc8eef652011-05-02 15:21:42 +053097#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010098
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020099#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200100#define LINE_USED(line, offset) (line & (BIT(offset)))
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200101
Tony Lindgren3d009c82015-01-16 14:50:50 -0800102static void omap_gpio_unmask_irq(struct irq_data *d);
103
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200104static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -0600105{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200106 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100107 return gpiochip_get_data(chip);
Benoit Cousson25db7112012-02-23 21:50:10 +0100108}
109
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200110static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
111 int is_input)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100112{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100113 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100114 u32 l;
115
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700116 reg += bank->regs->direction;
Victor Kamensky661553b2013-11-16 02:01:04 +0200117 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100118 if (is_input)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200119 l |= BIT(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100120 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200121 l &= ~(BIT(gpio));
Victor Kamensky661553b2013-11-16 02:01:04 +0200122 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530123 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100124}
125
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700126
127/* set data out value using dedicate set/clear register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200128static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200129 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100131 void __iomem *reg = bank->base;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200132 u32 l = BIT(offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530134 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700135 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530136 bank->context.dataout |= l;
137 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700138 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530139 bank->context.dataout &= ~l;
140 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700141
Victor Kamensky661553b2013-11-16 02:01:04 +0200142 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700143}
144
145/* set data out value using mask register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200146static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200147 int enable)
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700148{
149 void __iomem *reg = bank->base + bank->regs->dataout;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200150 u32 gpio_bit = BIT(offset);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700151 u32 l;
152
Victor Kamensky661553b2013-11-16 02:01:04 +0200153 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700154 if (enable)
155 l |= gpio_bit;
156 else
157 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200158 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530159 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100160}
161
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200162static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100163{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700164 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100165
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200166 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100167}
168
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200169static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300170{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700171 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300172
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200173 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300174}
175
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200176/* set multiple data out values using dedicate set/clear register */
177static void omap_set_gpio_dataout_reg_multiple(struct gpio_bank *bank,
178 unsigned long *mask,
179 unsigned long *bits)
180{
181 void __iomem *reg = bank->base;
182 u32 l;
183
184 l = *bits & *mask;
185 writel_relaxed(l, reg + bank->regs->set_dataout);
186 bank->context.dataout |= l;
187
188 l = ~*bits & *mask;
189 writel_relaxed(l, reg + bank->regs->clr_dataout);
190 bank->context.dataout &= ~l;
191}
192
193/* set multiple data out values using mask register */
194static void omap_set_gpio_dataout_mask_multiple(struct gpio_bank *bank,
195 unsigned long *mask,
196 unsigned long *bits)
197{
198 void __iomem *reg = bank->base + bank->regs->dataout;
199 u32 l = (readl_relaxed(reg) & ~*mask) | (*bits & *mask);
200
201 writel_relaxed(l, reg);
202 bank->context.dataout = l;
203}
204
205static unsigned long omap_get_gpio_datain_multiple(struct gpio_bank *bank,
206 unsigned long *mask)
207{
208 void __iomem *reg = bank->base + bank->regs->datain;
209
210 return readl_relaxed(reg) & *mask;
211}
212
213static unsigned long omap_get_gpio_dataout_multiple(struct gpio_bank *bank,
214 unsigned long *mask)
215{
216 void __iomem *reg = bank->base + bank->regs->dataout;
217
218 return readl_relaxed(reg) & *mask;
219}
220
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200221static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700222{
Victor Kamensky661553b2013-11-16 02:01:04 +0200223 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700224
Benoit Cousson862ff642012-02-01 15:58:56 +0100225 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700226 l |= mask;
227 else
228 l &= ~mask;
229
Victor Kamensky661553b2013-11-16 02:01:04 +0200230 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700231}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100232
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200233static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530234{
235 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300236 clk_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530237 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300238
Victor Kamensky661553b2013-11-16 02:01:04 +0200239 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300240 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530241 }
242}
243
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200244static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530245{
246 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300247 /*
248 * Disable debounce before cutting it's clock. If debounce is
249 * enabled but the clock is not, GPIO module seems to be unable
250 * to detect events and generate interrupts at least on OMAP3.
251 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200252 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300253
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300254 clk_disable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530255 bank->dbck_enabled = false;
256 }
257}
258
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700259/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200260 * omap2_set_gpio_debounce - low level gpio debounce time
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700261 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200262 * @offset: the gpio number on this @bank
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700263 * @debounce: debounce time to use
264 *
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300265 * OMAP's debounce time is in 31us steps
266 * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
267 * so we need to convert and round up to the closest unit.
David Rivshin83977442017-04-24 18:56:50 -0400268 *
269 * Return: 0 on success, negative error otherwise.
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700270 */
David Rivshin83977442017-04-24 18:56:50 -0400271static int omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
272 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700273{
Kevin Hilman9942da02011-04-22 12:02:05 -0700274 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700275 u32 val;
276 u32 l;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300277 bool enable = !!debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700278
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800279 if (!bank->dbck_flag)
David Rivshin83977442017-04-24 18:56:50 -0400280 return -ENOTSUPP;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800281
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300282 if (enable) {
283 debounce = DIV_ROUND_UP(debounce, 31) - 1;
David Rivshin83977442017-04-24 18:56:50 -0400284 if ((debounce & OMAP4_GPIO_DEBOUNCINGTIME_MASK) != debounce)
285 return -EINVAL;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300286 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700287
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200288 l = BIT(offset);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700289
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300290 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700291 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200292 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700293
Kevin Hilman9942da02011-04-22 12:02:05 -0700294 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200295 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700296
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300297 if (enable)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700298 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530299 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700300 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300301 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700302
Victor Kamensky661553b2013-11-16 02:01:04 +0200303 writel_relaxed(val, reg);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300304 clk_disable(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530305 /*
306 * Enable debounce clock per module.
307 * This call is mandatory because in omap_gpio_request() when
308 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
309 * runtime callbck fails to turn on dbck because dbck_enable_mask
310 * used within _gpio_dbck_enable() is still not initialized at
311 * that point. Therefore we have to enable dbck here.
312 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200313 omap_gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530314 if (bank->dbck_enable_mask) {
315 bank->context.debounce = debounce;
316 bank->context.debounce_en = val;
317 }
David Rivshin83977442017-04-24 18:56:50 -0400318
319 return 0;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700320}
321
Jon Hunterc9c55d92012-10-26 14:26:04 -0500322/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200323 * omap_clear_gpio_debounce - clear debounce settings for a gpio
Jon Hunterc9c55d92012-10-26 14:26:04 -0500324 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200325 * @offset: the gpio number on this @bank
Jon Hunterc9c55d92012-10-26 14:26:04 -0500326 *
327 * If a gpio is using debounce, then clear the debounce enable bit and if
328 * this is the only gpio in this bank using debounce, then clear the debounce
329 * time too. The debounce clock will also be disabled when calling this function
330 * if this is the only gpio in the bank using debounce.
331 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200332static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
Jon Hunterc9c55d92012-10-26 14:26:04 -0500333{
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200334 u32 gpio_bit = BIT(offset);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500335
336 if (!bank->dbck_flag)
337 return;
338
339 if (!(bank->dbck_enable_mask & gpio_bit))
340 return;
341
342 bank->dbck_enable_mask &= ~gpio_bit;
343 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200344 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500345 bank->base + bank->regs->debounce_en);
346
347 if (!bank->dbck_enable_mask) {
348 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200349 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500350 bank->regs->debounce);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300351 clk_disable(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500352 bank->dbck_enabled = false;
353 }
354}
355
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200356static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530357 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100358{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800359 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200360 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100361
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200362 omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
363 trigger & IRQ_TYPE_LEVEL_LOW);
364 omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
365 trigger & IRQ_TYPE_LEVEL_HIGH);
366 omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
367 trigger & IRQ_TYPE_EDGE_RISING);
368 omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
369 trigger & IRQ_TYPE_EDGE_FALLING);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530370
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530371 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200372 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530373 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200374 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530375 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200376 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530377 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200378 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530379
380 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Tony Lindgren00ded242018-12-07 11:08:29 -0800381 omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
382 bank->context.wake_en =
383 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530384 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530385
Ambresh K55b220c2011-06-15 13:40:45 -0700386 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530387 if (!bank->regs->irqctrl) {
388 /* On omap24xx proceed only when valid GPIO bit is set */
389 if (bank->non_wakeup_gpios) {
390 if (!(bank->non_wakeup_gpios & gpio_bit))
391 goto exit;
392 }
393
Chunqiu Wang699117a62009-06-24 17:13:39 +0000394 /*
395 * Log the edge gpio and manually trigger the IRQ
396 * after resume if the input level changes
397 * to avoid irq lost during PER RET/OFF mode
398 * Applies for omap2 non-wakeup gpio and all omap3 gpios
399 */
400 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800401 bank->enabled_non_wakeup_gpios |= gpio_bit;
402 else
403 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
404 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700405
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530406exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530407 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200408 readl_relaxed(bank->base + bank->regs->leveldetect0) |
409 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100410}
411
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800412#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800413/*
414 * This only applies to chips that can't do both rising and falling edge
415 * detection at once. For all other chips, this function is a noop.
416 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200417static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800418{
419 void __iomem *reg = bank->base;
420 u32 l = 0;
421
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530422 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800423 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530424
425 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800426
Victor Kamensky661553b2013-11-16 02:01:04 +0200427 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800428 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200429 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800430 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200431 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800432
Victor Kamensky661553b2013-11-16 02:01:04 +0200433 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800434}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530435#else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200436static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800437#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800438
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200439static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
440 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100441{
442 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530443 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100444 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100445
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530446 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200447 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530448 } else if (bank->regs->irqctrl) {
449 reg += bank->regs->irqctrl;
450
Victor Kamensky661553b2013-11-16 02:01:04 +0200451 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000452 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200453 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100454 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200455 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100456 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200457 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100458 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530459 return -EINVAL;
460
Victor Kamensky661553b2013-11-16 02:01:04 +0200461 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530462 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100463 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530464 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100465 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530466 reg += bank->regs->edgectrl1;
467
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100468 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200469 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100470 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100471 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100472 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100473 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200474 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530475
476 /* Enable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200477 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530478 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200479 readl_relaxed(bank->base + bank->regs->wkup_en);
480 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100481 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100482 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100483}
484
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200485static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200486{
487 if (bank->regs->pinctrl) {
488 void __iomem *reg = bank->base + bank->regs->pinctrl;
489
490 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200491 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200492 }
493
494 if (bank->regs->ctrl && !BANK_USED(bank)) {
495 void __iomem *reg = bank->base + bank->regs->ctrl;
496 u32 ctrl;
497
Victor Kamensky661553b2013-11-16 02:01:04 +0200498 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200499 /* Module is enabled, clocks are not gated */
500 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200501 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200502 bank->context.ctrl = ctrl;
503 }
504}
505
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200506static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200507{
508 void __iomem *base = bank->base;
509
510 if (bank->regs->wkup_en &&
511 !LINE_USED(bank->mod_usage, offset) &&
512 !LINE_USED(bank->irq_usage, offset)) {
513 /* Disable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200514 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200515 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200516 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200517 }
518
519 if (bank->regs->ctrl && !BANK_USED(bank)) {
520 void __iomem *reg = bank->base + bank->regs->ctrl;
521 u32 ctrl;
522
Victor Kamensky661553b2013-11-16 02:01:04 +0200523 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200524 /* Module is disabled, clocks are gated */
525 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200526 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200527 bank->context.ctrl = ctrl;
528 }
529}
530
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200531static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200532{
533 void __iomem *reg = bank->base + bank->regs->direction;
534
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200535 return readl_relaxed(reg) & BIT(offset);
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200536}
537
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200538static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
Tony Lindgren3d009c82015-01-16 14:50:50 -0800539{
540 if (!LINE_USED(bank->mod_usage, offset)) {
541 omap_enable_gpio_module(bank, offset);
542 omap_set_gpio_direction(bank, offset, 1);
543 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200544 bank->irq_usage |= BIT(offset);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800545}
546
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200547static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100548{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200549 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100550 int retval;
David Brownella6472532008-03-03 04:33:30 -0800551 unsigned long flags;
Grygorii Strashkoea5fbe82015-03-23 14:18:29 +0200552 unsigned offset = d->hwirq;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100553
David Brownelle5c56ed2006-12-06 17:13:59 -0800554 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100555 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800556
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530557 if (!bank->regs->leveldetect0 &&
558 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100559 return -EINVAL;
560
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200561 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200562 retval = omap_set_gpio_triggering(bank, offset, type);
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300563 if (retval) {
Axel Lin627c89b2015-08-05 22:37:41 +0800564 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300565 goto error;
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300566 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200567 omap_gpio_init_irq(bank, offset);
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200568 if (!omap_gpio_is_input(bank, offset)) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200569 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300570 retval = -EINVAL;
571 goto error;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200572 }
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200573 raw_spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800574
575 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner43ec2e42015-06-23 15:52:39 +0200576 irq_set_handler_locked(d, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800577 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500578 /*
579 * Edge IRQs are already cleared/acked in irq_handler and
580 * not need to be masked, as result handle_edge_irq()
581 * logic is excessed here and may cause lose of interrupts.
582 * So just use handle_simple_irq.
583 */
584 irq_set_handler_locked(d, handle_simple_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800585
Grygorii Strashko1562e462015-05-22 17:35:49 +0300586 return 0;
587
588error:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100589 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100590}
591
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200592static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100593{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100594 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100595
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700596 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200597 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300598
599 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700600 if (bank->regs->irqstatus2) {
601 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200602 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700603 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700604
605 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200606 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100607}
608
Grygorii Strashko9943f262015-03-23 14:18:27 +0200609static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
610 unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100611{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200612 omap_clear_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100613}
614
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200615static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
Imre Deakea6dedd2006-06-26 16:16:00 -0700616{
617 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700618 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200619 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700620
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700621 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200622 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700623 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700624 l = ~l;
625 l &= mask;
626 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700627}
628
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200629static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100630{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100631 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100632 u32 l;
633
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700634 if (bank->regs->set_irqenable) {
635 reg += bank->regs->set_irqenable;
636 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530637 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700638 } else {
639 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200640 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700641 if (bank->regs->irqenable_inv)
642 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100643 else
644 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530645 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100646 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700647
Victor Kamensky661553b2013-11-16 02:01:04 +0200648 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700649}
650
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200651static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700652{
653 void __iomem *reg = bank->base;
654 u32 l;
655
656 if (bank->regs->clr_irqenable) {
657 reg += bank->regs->clr_irqenable;
658 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530659 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700660 } else {
661 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200662 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700663 if (bank->regs->irqenable_inv)
664 l |= gpio_mask;
665 else
666 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530667 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700668 }
669
Victor Kamensky661553b2013-11-16 02:01:04 +0200670 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100671}
672
Grygorii Strashko9943f262015-03-23 14:18:27 +0200673static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
674 unsigned offset, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530676 if (enable)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200677 omap_enable_gpio_irqbank(bank, BIT(offset));
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530678 else
Grygorii Strashko9943f262015-03-23 14:18:27 +0200679 omap_disable_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100680}
681
Tony Lindgren92105bb2005-09-07 17:20:26 +0100682/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200683static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100684{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200685 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100686
Grygorii Strashko0c0451e2016-04-12 13:52:31 +0300687 return irq_set_irq_wake(bank->irq, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100688}
689
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800690static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100691{
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100692 struct gpio_bank *bank = gpiochip_get_data(chip);
David Brownella6472532008-03-03 04:33:30 -0800693 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100694
Grygorii Strashko46748072018-09-28 16:39:50 -0500695 pm_runtime_get_sync(chip->parent);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100696
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200697 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashkoc3518172015-05-22 17:35:51 +0300698 omap_enable_gpio_module(bank, offset);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200699 bank->mod_usage |= BIT(offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200700 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100701
702 return 0;
703}
704
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800705static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100706{
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100707 struct gpio_bank *bank = gpiochip_get_data(chip);
David Brownella6472532008-03-03 04:33:30 -0800708 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100709
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200710 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200711 bank->mod_usage &= ~(BIT(offset));
Grygorii Strashko5f982c72015-05-22 17:35:48 +0300712 if (!LINE_USED(bank->irq_usage, offset)) {
713 omap_set_gpio_direction(bank, offset, 1);
714 omap_clear_gpio_debounce(bank, offset);
715 }
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200716 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200717 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530718
Grygorii Strashko46748072018-09-28 16:39:50 -0500719 pm_runtime_put(chip->parent);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100720}
721
722/*
723 * We need to unmask the GPIO bank interrupt as soon as possible to
724 * avoid missing GPIO interrupts for other lines in the bank.
725 * Then we need to mask-read-clear-unmask the triggered GPIO lines
726 * in the bank to avoid missing nested interrupts for a GPIO line.
727 * If we wait to unmask individual GPIO lines in the bank after the
728 * line's interrupt handler has been run, we may miss some nested
729 * interrupts.
730 */
Grygorii Strashko450fa542015-09-25 12:28:03 -0700731static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100733 void __iomem *isr_reg = NULL;
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500734 u32 enabled, isr, level_mask;
Jon Hunter3513cde2013-04-04 15:16:14 -0500735 unsigned int bit;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700736 struct gpio_bank *bank = gpiobank;
737 unsigned long wa_lock_flags;
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300738 unsigned long lock_flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100739
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700740 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800741 if (WARN_ON(!isr_reg))
742 goto exit;
743
Tony Lindgren52845212018-09-20 12:35:32 -0700744 if (WARN_ONCE(!pm_runtime_active(bank->chip.parent),
745 "gpio irq%i while runtime suspended?\n", irq))
746 return IRQ_NONE;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700747
Laurent Navete83507b2013-03-20 13:15:57 +0100748 while (1) {
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300749 raw_spin_lock_irqsave(&bank->lock, lock_flags);
750
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200751 enabled = omap_get_gpio_irqbank_mask(bank);
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500752 isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100753
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530754 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800755 level_mask = bank->level_mask & enabled;
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500756 else
757 level_mask = 0;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100758
759 /* clear edge sensitive interrupts before handler(s) are
760 called so that we don't miss any interrupt occurred while
761 executing them */
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500762 if (isr & ~level_mask)
763 omap_clear_gpio_irqbank(bank, isr & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100764
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300765 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
766
Tony Lindgren92105bb2005-09-07 17:20:26 +0100767 if (!isr)
768 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100769
Jon Hunter3513cde2013-04-04 15:16:14 -0500770 while (isr) {
771 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200772 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100773
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300774 raw_spin_lock_irqsave(&bank->lock, lock_flags);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800775 /*
776 * Some chips can't respond to both rising and falling
777 * at the same time. If this irq was requested with
778 * both flags, we need to flip the ICR data for the IRQ
779 * to respond to the IRQ for the opposite direction.
780 * This will be indicated in the bank toggle_mask.
781 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200782 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200783 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800784
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300785 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
786
Grygorii Strashko450fa542015-09-25 12:28:03 -0700787 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
788
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100789 generic_handle_irq(irq_find_mapping(bank->chip.irq.domain,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200790 bit));
Grygorii Strashko450fa542015-09-25 12:28:03 -0700791
792 raw_spin_unlock_irqrestore(&bank->wa_lock,
793 wa_lock_flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100794 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000795 }
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800796exit:
Grygorii Strashko450fa542015-09-25 12:28:03 -0700797 return IRQ_HANDLED;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100798}
799
Tony Lindgren3d009c82015-01-16 14:50:50 -0800800static unsigned int omap_gpio_irq_startup(struct irq_data *d)
801{
802 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800803 unsigned long flags;
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200804 unsigned offset = d->hwirq;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800805
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200806 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300807
808 if (!LINE_USED(bank->mod_usage, offset))
809 omap_set_gpio_direction(bank, offset, 1);
810 else if (!omap_gpio_is_input(bank, offset))
811 goto err;
812 omap_enable_gpio_module(bank, offset);
813 bank->irq_usage |= BIT(offset);
814
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200815 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800816 omap_gpio_unmask_irq(d);
817
818 return 0;
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300819err:
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200820 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300821 return -EINVAL;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800822}
823
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200824static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300825{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200826 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700827 unsigned long flags;
Grygorii Strashko9943f262015-03-23 14:18:27 +0200828 unsigned offset = d->hwirq;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300829
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200830 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200831 bank->irq_usage &= ~(BIT(offset));
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300832 omap_set_gpio_irqenable(bank, offset, 0);
833 omap_clear_gpio_irqstatus(bank, offset);
834 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
835 if (!LINE_USED(bank->mod_usage, offset))
836 omap_clear_gpio_debounce(bank, offset);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200837 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200838 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700839}
840
841static void omap_gpio_irq_bus_lock(struct irq_data *data)
842{
843 struct gpio_bank *bank = omap_irq_data_get_bank(data);
844
Grygorii Strashko46748072018-09-28 16:39:50 -0500845 pm_runtime_get_sync(bank->chip.parent);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700846}
847
848static void gpio_irq_bus_sync_unlock(struct irq_data *data)
849{
850 struct gpio_bank *bank = omap_irq_data_get_bank(data);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200851
Grygorii Strashko46748072018-09-28 16:39:50 -0500852 pm_runtime_put(bank->chip.parent);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300853}
854
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200855static void omap_gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100856{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200857 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200858 unsigned offset = d->hwirq;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100859
Grygorii Strashko9943f262015-03-23 14:18:27 +0200860 omap_clear_gpio_irqstatus(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100861}
862
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200863static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100864{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200865 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200866 unsigned offset = d->hwirq;
Colin Cross85ec7b92011-06-06 13:38:18 -0700867 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100868
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200869 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200870 omap_set_gpio_irqenable(bank, offset, 0);
871 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200872 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100873}
874
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200875static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100876{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200877 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200878 unsigned offset = d->hwirq;
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100879 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700880 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700881
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200882 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700883 if (trigger)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200884 omap_set_gpio_triggering(bank, offset, trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800885
886 /* For level-triggered GPIOs, the clearing must be done after
887 * the HW source is cleared, thus after the handler has run */
Grygorii Strashko9943f262015-03-23 14:18:27 +0200888 if (bank->level_mask & BIT(offset)) {
889 omap_set_gpio_irqenable(bank, offset, 0);
890 omap_clear_gpio_irqstatus(bank, offset);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800891 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100892
Grygorii Strashko9943f262015-03-23 14:18:27 +0200893 omap_set_gpio_irqenable(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200894 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100895}
896
Tony Lindgrenec0daae2018-09-20 12:35:30 -0700897/*
898 * Only edges can generate a wakeup event to the PRCM.
899 *
900 * Therefore, ensure any wake-up capable GPIOs have
901 * edge-detection enabled before going idle to ensure a wakeup
902 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
903 * NDA TRM 25.5.3.1)
904 *
905 * The normal values will be restored upon ->runtime_resume()
906 * by writing back the values saved in bank->context.
907 */
908static void __maybe_unused
909omap2_gpio_enable_level_quirk(struct gpio_bank *bank)
910{
911 u32 wake_low, wake_hi;
912
913 /* Enable additional edge detection for level gpios for idle */
914 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
915 if (wake_low)
916 writel_relaxed(wake_low | bank->context.fallingdetect,
917 bank->base + bank->regs->fallingdetect);
918
919 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
920 if (wake_hi)
921 writel_relaxed(wake_hi | bank->context.risingdetect,
922 bank->base + bank->regs->risingdetect);
923}
924
925static void __maybe_unused
926omap2_gpio_disable_level_quirk(struct gpio_bank *bank)
927{
928 /* Disable edge detection for level gpios after idle */
929 writel_relaxed(bank->context.fallingdetect,
930 bank->base + bank->regs->fallingdetect);
931 writel_relaxed(bank->context.risingdetect,
932 bank->base + bank->regs->risingdetect);
933}
934
David Brownelle5c56ed2006-12-06 17:13:59 -0800935/*---------------------------------------------------------------------*/
936
Magnus Damm79ee0312009-07-08 13:22:04 +0200937static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800938{
Magnus Damm79ee0312009-07-08 13:22:04 +0200939 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800940 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800941 void __iomem *mask_reg = bank->base +
942 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800943 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800944
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200945 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200946 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200947 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800948
949 return 0;
950}
951
Magnus Damm79ee0312009-07-08 13:22:04 +0200952static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800953{
Magnus Damm79ee0312009-07-08 13:22:04 +0200954 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800955 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800956 void __iomem *mask_reg = bank->base +
957 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800958 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800959
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200960 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200961 writel_relaxed(bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200962 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800963
964 return 0;
965}
966
Alexey Dobriyan47145212009-12-14 18:00:08 -0800967static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200968 .suspend_noirq = omap_mpuio_suspend_noirq,
969 .resume_noirq = omap_mpuio_resume_noirq,
970};
971
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200972/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800973static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800974 .driver = {
975 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200976 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800977 },
978};
979
980static struct platform_device omap_mpuio_device = {
981 .name = "mpuio",
982 .id = -1,
983 .dev = {
984 .driver = &omap_mpuio_driver.driver,
985 }
986 /* could list the /proc/iomem resources */
987};
988
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200989static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800990{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800991 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700992
David Brownell11a78b72006-12-06 17:14:11 -0800993 if (platform_driver_register(&omap_mpuio_driver) == 0)
994 (void) platform_device_register(&omap_mpuio_device);
995}
996
David Brownelle5c56ed2006-12-06 17:13:59 -0800997/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100998
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200999static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +02001000{
1001 struct gpio_bank *bank;
1002 unsigned long flags;
1003 void __iomem *reg;
1004 int dir;
1005
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001006 bank = gpiochip_get_data(chip);
Yegor Yefremov93700842014-04-24 08:57:39 +02001007 reg = bank->base + bank->regs->direction;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001008 raw_spin_lock_irqsave(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +02001009 dir = !!(readl_relaxed(reg) & BIT(offset));
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001010 raw_spin_unlock_irqrestore(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +02001011 return dir;
1012}
1013
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001014static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -08001015{
1016 struct gpio_bank *bank;
1017 unsigned long flags;
1018
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001019 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001020 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001021 omap_set_gpio_direction(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001022 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -08001023 return 0;
1024}
1025
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001026static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -08001027{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001028 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001029
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001030 bank = gpiochip_get_data(chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001031
Grygorii Strashkob2b20042015-03-23 14:18:23 +02001032 if (omap_gpio_is_input(bank, offset))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001033 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001034 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001035 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -08001036}
1037
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001038static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001039{
1040 struct gpio_bank *bank;
1041 unsigned long flags;
1042
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001043 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001044 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001045 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001046 omap_set_gpio_direction(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001047 raw_spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +02001048 return 0;
David Brownell52e31342008-03-03 12:43:23 -08001049}
1050
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001051static int omap_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
1052 unsigned long *bits)
1053{
1054 struct gpio_bank *bank = gpiochip_get_data(chip);
1055 void __iomem *reg = bank->base + bank->regs->direction;
1056 unsigned long in = readl_relaxed(reg), l;
1057
1058 *bits = 0;
1059
1060 l = in & *mask;
1061 if (l)
1062 *bits |= omap_get_gpio_datain_multiple(bank, &l);
1063
1064 l = ~in & *mask;
1065 if (l)
1066 *bits |= omap_get_gpio_dataout_multiple(bank, &l);
1067
1068 return 0;
1069}
1070
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001071static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
1072 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001073{
1074 struct gpio_bank *bank;
1075 unsigned long flags;
David Rivshin83977442017-04-24 18:56:50 -04001076 int ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001077
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001078 bank = gpiochip_get_data(chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001079
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001080 raw_spin_lock_irqsave(&bank->lock, flags);
David Rivshin83977442017-04-24 18:56:50 -04001081 ret = omap2_set_gpio_debounce(bank, offset, debounce);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001082 raw_spin_unlock_irqrestore(&bank->lock, flags);
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001083
David Rivshin83977442017-04-24 18:56:50 -04001084 if (ret)
1085 dev_info(chip->parent,
1086 "Could not set line %u debounce to %u microseconds (%d)",
1087 offset, debounce, ret);
1088
1089 return ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001090}
1091
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001092static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset,
1093 unsigned long config)
1094{
1095 u32 debounce;
1096
1097 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
1098 return -ENOTSUPP;
1099
1100 debounce = pinconf_to_config_argument(config);
1101 return omap_gpio_debounce(chip, offset, debounce);
1102}
1103
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001104static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001105{
1106 struct gpio_bank *bank;
1107 unsigned long flags;
1108
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001109 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001110 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001111 bank->set_dataout(bank, offset, value);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001112 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -08001113}
1114
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001115static void omap_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
1116 unsigned long *bits)
1117{
1118 struct gpio_bank *bank = gpiochip_get_data(chip);
1119 unsigned long flags;
1120
1121 raw_spin_lock_irqsave(&bank->lock, flags);
1122 bank->set_dataout_multiple(bank, mask, bits);
1123 raw_spin_unlock_irqrestore(&bank->lock, flags);
1124}
1125
David Brownell52e31342008-03-03 12:43:23 -08001126/*---------------------------------------------------------------------*/
1127
Arnd Bergmanne4b2ae72017-09-16 22:42:21 +02001128static void omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001129{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001130 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001131 u32 rev;
1132
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001133 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001134 return;
1135
Victor Kamensky661553b2013-11-16 02:01:04 +02001136 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001137 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001138 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001139
1140 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001141}
1142
Charulatha V03e128c2011-05-05 19:58:01 +05301143static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001144{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301145 void __iomem *base = bank->base;
1146 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001147
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301148 if (bank->width == 16)
1149 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001150
Charulatha Vd0d665a2011-08-31 00:02:21 +05301151 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001152 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301153 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001154 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301155
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001156 omap_gpio_rmw(base, bank->regs->irqenable, l,
1157 bank->regs->irqenable_inv);
1158 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1159 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301160 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001161 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301162
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301163 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001164 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301165 /* Initialize interface clk ungated, module enabled */
1166 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001167 writel_relaxed(0, base + bank->regs->ctrl);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001168}
1169
Nishanth Menon46824e222014-09-05 14:52:55 -05001170static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001171{
Grygorii Strashko81930322017-11-15 12:36:33 -06001172 struct gpio_irq_chip *irq;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001173 static int gpio;
Linus Walleij088413b2017-12-29 13:22:58 +01001174 const char *label;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001175 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001176 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001177
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001178 /*
1179 * REVISIT eventually switch from OMAP-specific gpio structs
1180 * over to the generic ones
1181 */
1182 bank->chip.request = omap_gpio_request;
1183 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001184 bank->chip.get_direction = omap_gpio_get_direction;
1185 bank->chip.direction_input = omap_gpio_input;
1186 bank->chip.get = omap_gpio_get;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001187 bank->chip.get_multiple = omap_gpio_get_multiple;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001188 bank->chip.direction_output = omap_gpio_output;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001189 bank->chip.set_config = omap_gpio_set_config;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001190 bank->chip.set = omap_gpio_set;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001191 bank->chip.set_multiple = omap_gpio_set_multiple;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301192 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001193 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301194 if (bank->regs->wkup_en)
Linus Walleij58383c782015-11-04 09:56:26 +01001195 bank->chip.parent = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001196 bank->chip.base = OMAP_MPUIO(0);
1197 } else {
Linus Walleij088413b2017-12-29 13:22:58 +01001198 label = devm_kasprintf(bank->chip.parent, GFP_KERNEL, "gpio-%d-%d",
1199 gpio, gpio + bank->width - 1);
1200 if (!label)
1201 return -ENOMEM;
1202 bank->chip.label = label;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001203 bank->chip.base = gpio;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001204 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001205 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001206
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001207#ifdef CONFIG_ARCH_OMAP1
1208 /*
1209 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1210 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1211 */
Bartosz Golaszewski2ed36f32017-03-04 17:23:31 +01001212 irq_base = devm_irq_alloc_descs(bank->chip.parent,
1213 -1, 0, bank->width, 0);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001214 if (irq_base < 0) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001215 dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n");
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001216 return -ENODEV;
1217 }
1218#endif
1219
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001220 /* MPUIO is a bit different, reading IRQ status clears it */
1221 if (bank->is_mpuio) {
1222 irqc->irq_ack = dummy_irq_chip.irq_ack;
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001223 if (!bank->regs->wkup_en)
1224 irqc->irq_set_wake = NULL;
1225 }
1226
Grygorii Strashko81930322017-11-15 12:36:33 -06001227 irq = &bank->chip.irq;
1228 irq->chip = irqc;
1229 irq->handler = handle_bad_irq;
1230 irq->default_type = IRQ_TYPE_NONE;
1231 irq->num_parents = 1;
1232 irq->parents = &bank->irq;
1233 irq->first = irq_base;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001234
Grygorii Strashko81930322017-11-15 12:36:33 -06001235 ret = gpiochip_add_data(&bank->chip, bank);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001236 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001237 dev_err(bank->chip.parent,
Grygorii Strashko81930322017-11-15 12:36:33 -06001238 "Could not register gpio chip %d\n", ret);
1239 return ret;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001240 }
1241
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001242 ret = devm_request_irq(bank->chip.parent, bank->irq,
1243 omap_gpio_irq_handler,
1244 0, dev_name(bank->chip.parent), bank);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001245 if (ret)
1246 gpiochip_remove(&bank->chip);
1247
Grygorii Strashko81930322017-11-15 12:36:33 -06001248 if (!bank->is_mpuio)
1249 gpio += bank->width;
1250
Grygorii Strashko450fa542015-09-25 12:28:03 -07001251 return ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001252}
1253
Tony Lindgrenb764a582018-09-20 12:35:31 -07001254static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context);
1255static void omap_gpio_unidle(struct gpio_bank *bank);
1256
1257static int gpio_omap_cpu_notifier(struct notifier_block *nb,
1258 unsigned long cmd, void *v)
1259{
1260 struct gpio_bank *bank;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001261 unsigned long flags;
1262
1263 bank = container_of(nb, struct gpio_bank, nb);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001264
1265 raw_spin_lock_irqsave(&bank->lock, flags);
1266 switch (cmd) {
1267 case CPU_CLUSTER_PM_ENTER:
1268 if (bank->is_suspended)
1269 break;
1270 omap_gpio_idle(bank, true);
1271 break;
1272 case CPU_CLUSTER_PM_ENTER_FAILED:
1273 case CPU_CLUSTER_PM_EXIT:
1274 if (bank->is_suspended)
1275 break;
1276 omap_gpio_unidle(bank);
1277 break;
1278 }
1279 raw_spin_unlock_irqrestore(&bank->lock, flags);
1280
1281 return NOTIFY_OK;
1282}
1283
Benoit Cousson384ebe12011-08-16 11:53:02 +02001284static const struct of_device_id omap_gpio_match[];
1285
Bill Pemberton38363092012-11-19 13:22:34 -05001286static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001287{
Benoit Cousson862ff642012-02-01 15:58:56 +01001288 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001289 struct device_node *node = dev->of_node;
1290 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001291 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001292 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001293 struct gpio_bank *bank;
Nishanth Menon46824e222014-09-05 14:52:55 -05001294 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001295 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001296
Benoit Cousson384ebe12011-08-16 11:53:02 +02001297 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1298
Jingoo Hane56aee12013-07-30 17:08:05 +09001299 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001300 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001301 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001302
Markus Elfringf97364c2018-02-10 21:49:22 +01001303 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
Markus Elfring9117d402018-02-10 21:46:30 +01001304 if (!bank)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001305 return -ENOMEM;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001306
Nishanth Menon46824e222014-09-05 14:52:55 -05001307 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1308 if (!irqc)
1309 return -ENOMEM;
1310
Tony Lindgren3d009c82015-01-16 14:50:50 -08001311 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e222014-09-05 14:52:55 -05001312 irqc->irq_shutdown = omap_gpio_irq_shutdown,
1313 irqc->irq_ack = omap_gpio_ack_irq,
1314 irqc->irq_mask = omap_gpio_mask_irq,
1315 irqc->irq_unmask = omap_gpio_unmask_irq,
1316 irqc->irq_set_type = omap_gpio_irq_type,
1317 irqc->irq_set_wake = omap_gpio_wake_enable,
Grygorii Strashkoaca82d12015-09-25 12:28:02 -07001318 irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
1319 irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
Nishanth Menon46824e222014-09-05 14:52:55 -05001320 irqc->name = dev_name(&pdev->dev);
Grygorii Strashko0c0451e2016-04-12 13:52:31 +03001321 irqc->flags = IRQCHIP_MASK_ON_SUSPEND;
Grygorii Strashko46748072018-09-28 16:39:50 -05001322 irqc->parent_device = dev;
Nishanth Menon46824e222014-09-05 14:52:55 -05001323
Grygorii Strashko89d18e32015-08-18 14:10:53 +03001324 bank->irq = platform_get_irq(pdev, 0);
1325 if (bank->irq <= 0) {
1326 if (!bank->irq)
1327 bank->irq = -ENXIO;
1328 if (bank->irq != -EPROBE_DEFER)
1329 dev_err(dev,
1330 "can't get irq resource ret=%d\n", bank->irq);
1331 return bank->irq;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001332 }
1333
Linus Walleij58383c782015-11-04 09:56:26 +01001334 bank->chip.parent = dev;
Grygorii Strashkoc23837c2015-06-25 18:13:33 +03001335 bank->chip.owner = THIS_MODULE;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001336 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001337 bank->quirks = pdata->quirks;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001338 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001339 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301340 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301341 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001342 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001343#ifdef CONFIG_OF_GPIO
1344 bank->chip.of_node = of_node_get(node);
1345#endif
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001346
Jon Huntera2797be2013-04-04 15:16:15 -05001347 if (node) {
1348 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1349 bank->loses_context = true;
1350 } else {
1351 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001352
1353 if (bank->loses_context)
1354 bank->get_context_loss_count =
1355 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001356 }
1357
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001358 if (bank->regs->set_dataout && bank->regs->clr_dataout) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001359 bank->set_dataout = omap_set_gpio_dataout_reg;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001360 bank->set_dataout_multiple = omap_set_gpio_dataout_reg_multiple;
1361 } else {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001362 bank->set_dataout = omap_set_gpio_dataout_mask;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001363 bank->set_dataout_multiple =
1364 omap_set_gpio_dataout_mask_multiple;
1365 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001366
Tony Lindgren00ded242018-12-07 11:08:29 -08001367 if (bank->quirks & OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER) {
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001368 bank->funcs.idle_enable_level_quirk =
1369 omap2_gpio_enable_level_quirk;
1370 bank->funcs.idle_disable_level_quirk =
1371 omap2_gpio_disable_level_quirk;
1372 }
1373
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001374 raw_spin_lock_init(&bank->lock);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001375 raw_spin_lock_init(&bank->wa_lock);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001376
1377 /* Static mapping, never released */
1378 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001379 bank->base = devm_ioremap_resource(dev, res);
1380 if (IS_ERR(bank->base)) {
Jingoo Han717f70e2014-02-12 11:51:38 +09001381 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001382 }
1383
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001384 if (bank->dbck_flag) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001385 bank->dbck = devm_clk_get(dev, "dbclk");
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001386 if (IS_ERR(bank->dbck)) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001387 dev_err(dev,
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001388 "Could not get gpio dbck. Disable debounce\n");
1389 bank->dbck_flag = false;
1390 } else {
1391 clk_prepare(bank->dbck);
1392 }
1393 }
1394
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301395 platform_set_drvdata(pdev, bank);
1396
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001397 pm_runtime_enable(dev);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001398 pm_runtime_get_sync(dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001399
Charulatha Vd0d665a2011-08-31 00:02:21 +05301400 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001401 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301402
Charulatha V03e128c2011-05-05 19:58:01 +05301403 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001404
Nishanth Menon46824e222014-09-05 14:52:55 -05001405 ret = omap_gpio_chip_init(bank, irqc);
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001406 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001407 pm_runtime_put_sync(dev);
1408 pm_runtime_disable(dev);
Arvind Yadave2c3c192017-08-01 12:14:31 +05301409 if (bank->dbck_flag)
1410 clk_unprepare(bank->dbck);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001411 return ret;
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001412 }
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001413
Tony Lindgren9a748052010-12-07 16:26:56 -08001414 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001415
Tony Lindgrenb764a582018-09-20 12:35:31 -07001416 if (bank->funcs.idle_enable_level_quirk &&
1417 bank->funcs.idle_disable_level_quirk) {
1418 bank->nb.notifier_call = gpio_omap_cpu_notifier;
1419 cpu_pm_register_notifier(&bank->nb);
1420 }
1421
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001422 pm_runtime_put(dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301423
Jon Hunter879fe322013-04-04 15:16:12 -05001424 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001425}
1426
Tony Lindgrencac089f2015-04-23 16:56:22 -07001427static int omap_gpio_remove(struct platform_device *pdev)
1428{
1429 struct gpio_bank *bank = platform_get_drvdata(pdev);
1430
Tony Lindgrenb764a582018-09-20 12:35:31 -07001431 if (bank->nb.notifier_call)
1432 cpu_pm_unregister_notifier(&bank->nb);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001433 list_del(&bank->node);
1434 gpiochip_remove(&bank->chip);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001435 pm_runtime_disable(&pdev->dev);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001436 if (bank->dbck_flag)
1437 clk_unprepare(bank->dbck);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001438
1439 return 0;
1440}
1441
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301442static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001443
Tony Lindgrenb764a582018-09-20 12:35:31 -07001444static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301445{
Tony Lindgrenb764a582018-09-20 12:35:31 -07001446 struct device *dev = bank->chip.parent;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301447 u32 l1 = 0, l2 = 0;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001448
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001449 if (bank->funcs.idle_enable_level_quirk)
1450 bank->funcs.idle_enable_level_quirk(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001451
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001452 if (!bank->enabled_non_wakeup_gpios)
1453 goto update_gpio_context_count;
1454
Tony Lindgrenb764a582018-09-20 12:35:31 -07001455 if (!may_lose_context)
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301456 goto update_gpio_context_count;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001457
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301458 /*
1459 * If going to OFF, remove triggering for all
1460 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1461 * generated. See OMAP2420 Errata item 1.101.
1462 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001463 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301464 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301465 l1 = bank->context.fallingdetect;
1466 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301467
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301468 l1 &= ~bank->enabled_non_wakeup_gpios;
1469 l2 &= ~bank->enabled_non_wakeup_gpios;
1470
Victor Kamensky661553b2013-11-16 02:01:04 +02001471 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1472 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301473
1474 bank->workaround_enabled = true;
1475
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301476update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301477 if (bank->get_context_loss_count)
1478 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001479 bank->get_context_loss_count(dev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301480
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001481 omap_gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301482}
1483
Jon Hunter352a2d52013-04-15 13:06:54 -05001484static void omap_gpio_init_context(struct gpio_bank *p);
1485
Tony Lindgrenb764a582018-09-20 12:35:31 -07001486static void omap_gpio_unidle(struct gpio_bank *bank)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301487{
Tony Lindgrenb764a582018-09-20 12:35:31 -07001488 struct device *dev = bank->chip.parent;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301489 u32 l = 0, gen, gen0, gen1;
Jon Huntera2797be2013-04-04 15:16:15 -05001490 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301491
Jon Hunter352a2d52013-04-15 13:06:54 -05001492 /*
1493 * On the first resume during the probe, the context has not
1494 * been initialised and so initialise it now. Also initialise
1495 * the context loss count.
1496 */
1497 if (bank->loses_context && !bank->context_valid) {
1498 omap_gpio_init_context(bank);
1499
1500 if (bank->get_context_loss_count)
1501 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001502 bank->get_context_loss_count(dev);
Jon Hunter352a2d52013-04-15 13:06:54 -05001503 }
1504
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001505 omap_gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001506
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001507 if (bank->funcs.idle_disable_level_quirk)
1508 bank->funcs.idle_disable_level_quirk(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001509
Jon Huntera2797be2013-04-04 15:16:15 -05001510 if (bank->loses_context) {
1511 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301512 omap_gpio_restore_context(bank);
1513 } else {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001514 c = bank->get_context_loss_count(dev);
Jon Huntera2797be2013-04-04 15:16:15 -05001515 if (c != bank->context_loss_count) {
1516 omap_gpio_restore_context(bank);
1517 } else {
Tony Lindgrenb764a582018-09-20 12:35:31 -07001518 return;
Jon Huntera2797be2013-04-04 15:16:15 -05001519 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301520 }
1521 }
1522
Tony Lindgrenb764a582018-09-20 12:35:31 -07001523 if (!bank->workaround_enabled)
1524 return;
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301525
Victor Kamensky661553b2013-11-16 02:01:04 +02001526 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301527
1528 /*
1529 * Check if any of the non-wakeup interrupt GPIOs have changed
1530 * state. If so, generate an IRQ by software. This is
1531 * horribly racy, but it's the best we can do to work around
1532 * this silicon bug.
1533 */
1534 l ^= bank->saved_datain;
1535 l &= bank->enabled_non_wakeup_gpios;
1536
1537 /*
1538 * No need to generate IRQs for the rising edge for gpio IRQs
1539 * configured with falling edge only; and vice versa.
1540 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301541 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301542 gen0 &= bank->saved_datain;
1543
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301544 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301545 gen1 &= ~(bank->saved_datain);
1546
1547 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301548 gen = l & (~(bank->context.fallingdetect) &
1549 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301550 /* Consider all GPIO IRQs needed to be updated */
1551 gen |= gen0 | gen1;
1552
1553 if (gen) {
1554 u32 old0, old1;
1555
Victor Kamensky661553b2013-11-16 02:01:04 +02001556 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1557 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301558
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301559 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001560 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301561 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001562 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301563 bank->regs->leveldetect1);
1564 }
1565
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301566 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001567 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301568 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001569 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301570 bank->regs->leveldetect1);
1571 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001572 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1573 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301574 }
1575
1576 bank->workaround_enabled = false;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001577}
1578
Jon Hunter352a2d52013-04-15 13:06:54 -05001579static void omap_gpio_init_context(struct gpio_bank *p)
1580{
1581 struct omap_gpio_reg_offs *regs = p->regs;
1582 void __iomem *base = p->base;
1583
Victor Kamensky661553b2013-11-16 02:01:04 +02001584 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1585 p->context.oe = readl_relaxed(base + regs->direction);
1586 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1587 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1588 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1589 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1590 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1591 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1592 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001593
1594 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001595 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001596 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001597 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001598
1599 p->context_valid = true;
1600}
1601
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301602static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301603{
Victor Kamensky661553b2013-11-16 02:01:04 +02001604 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301605 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001606 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1607 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301608 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001609 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301610 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001611 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301612 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001613 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301614 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301615 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001616 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301617 bank->base + bank->regs->set_dataout);
1618 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001619 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301620 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001621 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301622
Nishanth Menonae547352011-09-09 19:08:58 +05301623 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001624 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301625 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001626 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301627 bank->base + bank->regs->debounce_en);
1628 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301629
Victor Kamensky661553b2013-11-16 02:01:04 +02001630 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301631 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001632 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301633 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301634}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301635
Tony Lindgrenb764a582018-09-20 12:35:31 -07001636static int __maybe_unused omap_gpio_runtime_suspend(struct device *dev)
1637{
1638 struct platform_device *pdev = to_platform_device(dev);
1639 struct gpio_bank *bank = platform_get_drvdata(pdev);
1640 unsigned long flags;
1641 int error = 0;
1642
1643 raw_spin_lock_irqsave(&bank->lock, flags);
1644 /* Must be idled only by CPU_CLUSTER_PM_ENTER? */
1645 if (bank->irq_usage) {
1646 error = -EBUSY;
1647 goto unlock;
1648 }
1649 omap_gpio_idle(bank, true);
1650 bank->is_suspended = true;
1651unlock:
1652 raw_spin_unlock_irqrestore(&bank->lock, flags);
1653
1654 return error;
1655}
1656
1657static int __maybe_unused omap_gpio_runtime_resume(struct device *dev)
1658{
1659 struct platform_device *pdev = to_platform_device(dev);
1660 struct gpio_bank *bank = platform_get_drvdata(pdev);
1661 unsigned long flags;
1662 int error = 0;
1663
1664 raw_spin_lock_irqsave(&bank->lock, flags);
1665 /* Must be unidled only by CPU_CLUSTER_PM_ENTER? */
1666 if (bank->irq_usage) {
1667 error = -EBUSY;
1668 goto unlock;
1669 }
1670 omap_gpio_unidle(bank);
1671 bank->is_suspended = false;
1672unlock:
1673 raw_spin_unlock_irqrestore(&bank->lock, flags);
1674
1675 return error;
1676}
1677
1678#ifdef CONFIG_ARCH_OMAP2PLUS
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301679static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301680 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1681 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301682};
Tony Lindgrenb764a582018-09-20 12:35:31 -07001683#else
1684static const struct dev_pm_ops gpio_pm_ops;
1685#endif /* CONFIG_ARCH_OMAP2PLUS */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301686
Benoit Cousson384ebe12011-08-16 11:53:02 +02001687#if defined(CONFIG_OF)
1688static struct omap_gpio_reg_offs omap2_gpio_regs = {
1689 .revision = OMAP24XX_GPIO_REVISION,
1690 .direction = OMAP24XX_GPIO_OE,
1691 .datain = OMAP24XX_GPIO_DATAIN,
1692 .dataout = OMAP24XX_GPIO_DATAOUT,
1693 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1694 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1695 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1696 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1697 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1698 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1699 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1700 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1701 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1702 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1703 .ctrl = OMAP24XX_GPIO_CTRL,
1704 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1705 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1706 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1707 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1708 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1709};
1710
1711static struct omap_gpio_reg_offs omap4_gpio_regs = {
1712 .revision = OMAP4_GPIO_REVISION,
1713 .direction = OMAP4_GPIO_OE,
1714 .datain = OMAP4_GPIO_DATAIN,
1715 .dataout = OMAP4_GPIO_DATAOUT,
1716 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1717 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1718 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1719 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1720 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1721 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1722 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1723 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1724 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1725 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1726 .ctrl = OMAP4_GPIO_CTRL,
1727 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1728 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1729 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1730 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1731 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1732};
1733
Tony Lindgrenb764a582018-09-20 12:35:31 -07001734/*
1735 * Note that omap2 does not currently support idle modes with context loss so
1736 * no need to add OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER quirk flag to save
1737 * and restore context.
1738 */
Chen Gange9a65bb2013-02-06 18:44:32 +08001739static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001740 .regs = &omap2_gpio_regs,
1741 .bank_width = 32,
1742 .dbck_flag = false,
1743};
1744
Chen Gange9a65bb2013-02-06 18:44:32 +08001745static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001746 .regs = &omap2_gpio_regs,
1747 .bank_width = 32,
1748 .dbck_flag = true,
Tony Lindgrenb764a582018-09-20 12:35:31 -07001749 .quirks = OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001750};
1751
Chen Gange9a65bb2013-02-06 18:44:32 +08001752static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001753 .regs = &omap4_gpio_regs,
1754 .bank_width = 32,
1755 .dbck_flag = true,
Tony Lindgren00ded242018-12-07 11:08:29 -08001756 .quirks = OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001757};
1758
1759static const struct of_device_id omap_gpio_match[] = {
1760 {
1761 .compatible = "ti,omap4-gpio",
1762 .data = &omap4_pdata,
1763 },
1764 {
1765 .compatible = "ti,omap3-gpio",
1766 .data = &omap3_pdata,
1767 },
1768 {
1769 .compatible = "ti,omap2-gpio",
1770 .data = &omap2_pdata,
1771 },
1772 { },
1773};
1774MODULE_DEVICE_TABLE(of, omap_gpio_match);
1775#endif
1776
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001777static struct platform_driver omap_gpio_driver = {
1778 .probe = omap_gpio_probe,
Tony Lindgrencac089f2015-04-23 16:56:22 -07001779 .remove = omap_gpio_remove,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001780 .driver = {
1781 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301782 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001783 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001784 },
1785};
1786
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001787/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001788 * gpio driver register needs to be done before
1789 * machine_init functions access gpio APIs.
1790 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001791 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001792static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001793{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001794 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001795}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001796postcore_initcall(omap_gpio_drv_reg);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001797
1798static void __exit omap_gpio_exit(void)
1799{
1800 platform_driver_unregister(&omap_gpio_driver);
1801}
1802module_exit(omap_gpio_exit);
1803
1804MODULE_DESCRIPTION("omap gpio driver");
1805MODULE_ALIAS("platform:gpio-omap");
1806MODULE_LICENSE("GPL v2");