blob: f4e9921fa9663643bf966e378c1095e6a260661f [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{
Wolfram Sanga3f4f722018-10-21 21:59:59 +0200939 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800940 void __iomem *mask_reg = bank->base +
941 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800942 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800943
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200944 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200945 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200946 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800947
948 return 0;
949}
950
Magnus Damm79ee0312009-07-08 13:22:04 +0200951static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800952{
Wolfram Sanga3f4f722018-10-21 21:59:59 +0200953 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800954 void __iomem *mask_reg = bank->base +
955 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800956 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800957
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200958 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200959 writel_relaxed(bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200960 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800961
962 return 0;
963}
964
Alexey Dobriyan47145212009-12-14 18:00:08 -0800965static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200966 .suspend_noirq = omap_mpuio_suspend_noirq,
967 .resume_noirq = omap_mpuio_resume_noirq,
968};
969
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200970/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800971static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800972 .driver = {
973 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200974 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800975 },
976};
977
978static struct platform_device omap_mpuio_device = {
979 .name = "mpuio",
980 .id = -1,
981 .dev = {
982 .driver = &omap_mpuio_driver.driver,
983 }
984 /* could list the /proc/iomem resources */
985};
986
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200987static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800988{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800989 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700990
David Brownell11a78b72006-12-06 17:14:11 -0800991 if (platform_driver_register(&omap_mpuio_driver) == 0)
992 (void) platform_device_register(&omap_mpuio_device);
993}
994
David Brownelle5c56ed2006-12-06 17:13:59 -0800995/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100996
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200997static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200998{
999 struct gpio_bank *bank;
1000 unsigned long flags;
1001 void __iomem *reg;
1002 int dir;
1003
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001004 bank = gpiochip_get_data(chip);
Yegor Yefremov93700842014-04-24 08:57:39 +02001005 reg = bank->base + bank->regs->direction;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001006 raw_spin_lock_irqsave(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +02001007 dir = !!(readl_relaxed(reg) & BIT(offset));
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001008 raw_spin_unlock_irqrestore(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +02001009 return dir;
1010}
1011
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001012static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -08001013{
1014 struct gpio_bank *bank;
1015 unsigned long flags;
1016
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001017 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001018 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001019 omap_set_gpio_direction(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001020 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -08001021 return 0;
1022}
1023
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001024static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -08001025{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001026 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001027
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001028 bank = gpiochip_get_data(chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001029
Grygorii Strashkob2b20042015-03-23 14:18:23 +02001030 if (omap_gpio_is_input(bank, offset))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001031 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001032 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001033 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -08001034}
1035
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001036static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001037{
1038 struct gpio_bank *bank;
1039 unsigned long flags;
1040
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001041 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001042 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001043 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001044 omap_set_gpio_direction(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001045 raw_spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +02001046 return 0;
David Brownell52e31342008-03-03 12:43:23 -08001047}
1048
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001049static int omap_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
1050 unsigned long *bits)
1051{
1052 struct gpio_bank *bank = gpiochip_get_data(chip);
1053 void __iomem *reg = bank->base + bank->regs->direction;
1054 unsigned long in = readl_relaxed(reg), l;
1055
1056 *bits = 0;
1057
1058 l = in & *mask;
1059 if (l)
1060 *bits |= omap_get_gpio_datain_multiple(bank, &l);
1061
1062 l = ~in & *mask;
1063 if (l)
1064 *bits |= omap_get_gpio_dataout_multiple(bank, &l);
1065
1066 return 0;
1067}
1068
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001069static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
1070 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001071{
1072 struct gpio_bank *bank;
1073 unsigned long flags;
David Rivshin83977442017-04-24 18:56:50 -04001074 int ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001075
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001076 bank = gpiochip_get_data(chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001077
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001078 raw_spin_lock_irqsave(&bank->lock, flags);
David Rivshin83977442017-04-24 18:56:50 -04001079 ret = omap2_set_gpio_debounce(bank, offset, debounce);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001080 raw_spin_unlock_irqrestore(&bank->lock, flags);
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001081
David Rivshin83977442017-04-24 18:56:50 -04001082 if (ret)
1083 dev_info(chip->parent,
1084 "Could not set line %u debounce to %u microseconds (%d)",
1085 offset, debounce, ret);
1086
1087 return ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001088}
1089
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001090static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset,
1091 unsigned long config)
1092{
1093 u32 debounce;
1094
1095 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
1096 return -ENOTSUPP;
1097
1098 debounce = pinconf_to_config_argument(config);
1099 return omap_gpio_debounce(chip, offset, debounce);
1100}
1101
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001102static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001103{
1104 struct gpio_bank *bank;
1105 unsigned long flags;
1106
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001107 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001108 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001109 bank->set_dataout(bank, offset, value);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001110 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -08001111}
1112
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001113static void omap_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
1114 unsigned long *bits)
1115{
1116 struct gpio_bank *bank = gpiochip_get_data(chip);
1117 unsigned long flags;
1118
1119 raw_spin_lock_irqsave(&bank->lock, flags);
1120 bank->set_dataout_multiple(bank, mask, bits);
1121 raw_spin_unlock_irqrestore(&bank->lock, flags);
1122}
1123
David Brownell52e31342008-03-03 12:43:23 -08001124/*---------------------------------------------------------------------*/
1125
Arnd Bergmanne4b2ae72017-09-16 22:42:21 +02001126static void omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001127{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001128 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001129 u32 rev;
1130
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001131 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001132 return;
1133
Victor Kamensky661553b2013-11-16 02:01:04 +02001134 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001135 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001136 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001137
1138 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001139}
1140
Charulatha V03e128c2011-05-05 19:58:01 +05301141static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001142{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301143 void __iomem *base = bank->base;
1144 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001145
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301146 if (bank->width == 16)
1147 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001148
Charulatha Vd0d665a2011-08-31 00:02:21 +05301149 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001150 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301151 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001152 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301153
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001154 omap_gpio_rmw(base, bank->regs->irqenable, l,
1155 bank->regs->irqenable_inv);
1156 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1157 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301158 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001159 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301160
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301161 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001162 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301163 /* Initialize interface clk ungated, module enabled */
1164 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001165 writel_relaxed(0, base + bank->regs->ctrl);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001166}
1167
Nishanth Menon46824e222014-09-05 14:52:55 -05001168static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001169{
Grygorii Strashko81930322017-11-15 12:36:33 -06001170 struct gpio_irq_chip *irq;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001171 static int gpio;
Linus Walleij088413b2017-12-29 13:22:58 +01001172 const char *label;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001173 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001174 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001175
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001176 /*
1177 * REVISIT eventually switch from OMAP-specific gpio structs
1178 * over to the generic ones
1179 */
1180 bank->chip.request = omap_gpio_request;
1181 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001182 bank->chip.get_direction = omap_gpio_get_direction;
1183 bank->chip.direction_input = omap_gpio_input;
1184 bank->chip.get = omap_gpio_get;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001185 bank->chip.get_multiple = omap_gpio_get_multiple;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001186 bank->chip.direction_output = omap_gpio_output;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001187 bank->chip.set_config = omap_gpio_set_config;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001188 bank->chip.set = omap_gpio_set;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001189 bank->chip.set_multiple = omap_gpio_set_multiple;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301190 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001191 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301192 if (bank->regs->wkup_en)
Linus Walleij58383c782015-11-04 09:56:26 +01001193 bank->chip.parent = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001194 bank->chip.base = OMAP_MPUIO(0);
1195 } else {
Linus Walleij088413b2017-12-29 13:22:58 +01001196 label = devm_kasprintf(bank->chip.parent, GFP_KERNEL, "gpio-%d-%d",
1197 gpio, gpio + bank->width - 1);
1198 if (!label)
1199 return -ENOMEM;
1200 bank->chip.label = label;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001201 bank->chip.base = gpio;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001202 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001203 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001204
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001205#ifdef CONFIG_ARCH_OMAP1
1206 /*
1207 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1208 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1209 */
Bartosz Golaszewski2ed36f32017-03-04 17:23:31 +01001210 irq_base = devm_irq_alloc_descs(bank->chip.parent,
1211 -1, 0, bank->width, 0);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001212 if (irq_base < 0) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001213 dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n");
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001214 return -ENODEV;
1215 }
1216#endif
1217
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001218 /* MPUIO is a bit different, reading IRQ status clears it */
1219 if (bank->is_mpuio) {
1220 irqc->irq_ack = dummy_irq_chip.irq_ack;
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001221 if (!bank->regs->wkup_en)
1222 irqc->irq_set_wake = NULL;
1223 }
1224
Grygorii Strashko81930322017-11-15 12:36:33 -06001225 irq = &bank->chip.irq;
1226 irq->chip = irqc;
1227 irq->handler = handle_bad_irq;
1228 irq->default_type = IRQ_TYPE_NONE;
1229 irq->num_parents = 1;
1230 irq->parents = &bank->irq;
1231 irq->first = irq_base;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001232
Grygorii Strashko81930322017-11-15 12:36:33 -06001233 ret = gpiochip_add_data(&bank->chip, bank);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001234 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001235 dev_err(bank->chip.parent,
Grygorii Strashko81930322017-11-15 12:36:33 -06001236 "Could not register gpio chip %d\n", ret);
1237 return ret;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001238 }
1239
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001240 ret = devm_request_irq(bank->chip.parent, bank->irq,
1241 omap_gpio_irq_handler,
1242 0, dev_name(bank->chip.parent), bank);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001243 if (ret)
1244 gpiochip_remove(&bank->chip);
1245
Grygorii Strashko81930322017-11-15 12:36:33 -06001246 if (!bank->is_mpuio)
1247 gpio += bank->width;
1248
Grygorii Strashko450fa542015-09-25 12:28:03 -07001249 return ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001250}
1251
Tony Lindgrenb764a582018-09-20 12:35:31 -07001252static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context);
1253static void omap_gpio_unidle(struct gpio_bank *bank);
1254
1255static int gpio_omap_cpu_notifier(struct notifier_block *nb,
1256 unsigned long cmd, void *v)
1257{
1258 struct gpio_bank *bank;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001259 unsigned long flags;
1260
1261 bank = container_of(nb, struct gpio_bank, nb);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001262
1263 raw_spin_lock_irqsave(&bank->lock, flags);
1264 switch (cmd) {
1265 case CPU_CLUSTER_PM_ENTER:
1266 if (bank->is_suspended)
1267 break;
1268 omap_gpio_idle(bank, true);
1269 break;
1270 case CPU_CLUSTER_PM_ENTER_FAILED:
1271 case CPU_CLUSTER_PM_EXIT:
1272 if (bank->is_suspended)
1273 break;
1274 omap_gpio_unidle(bank);
1275 break;
1276 }
1277 raw_spin_unlock_irqrestore(&bank->lock, flags);
1278
1279 return NOTIFY_OK;
1280}
1281
Benoit Cousson384ebe12011-08-16 11:53:02 +02001282static const struct of_device_id omap_gpio_match[];
1283
Bill Pemberton38363092012-11-19 13:22:34 -05001284static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001285{
Benoit Cousson862ff642012-02-01 15:58:56 +01001286 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001287 struct device_node *node = dev->of_node;
1288 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001289 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001290 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001291 struct gpio_bank *bank;
Nishanth Menon46824e222014-09-05 14:52:55 -05001292 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001293 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001294
Benoit Cousson384ebe12011-08-16 11:53:02 +02001295 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1296
Jingoo Hane56aee12013-07-30 17:08:05 +09001297 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001298 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001299 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001300
Markus Elfringf97364c2018-02-10 21:49:22 +01001301 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
Markus Elfring9117d402018-02-10 21:46:30 +01001302 if (!bank)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001303 return -ENOMEM;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001304
Nishanth Menon46824e222014-09-05 14:52:55 -05001305 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1306 if (!irqc)
1307 return -ENOMEM;
1308
Tony Lindgren3d009c82015-01-16 14:50:50 -08001309 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e222014-09-05 14:52:55 -05001310 irqc->irq_shutdown = omap_gpio_irq_shutdown,
1311 irqc->irq_ack = omap_gpio_ack_irq,
1312 irqc->irq_mask = omap_gpio_mask_irq,
1313 irqc->irq_unmask = omap_gpio_unmask_irq,
1314 irqc->irq_set_type = omap_gpio_irq_type,
1315 irqc->irq_set_wake = omap_gpio_wake_enable,
Grygorii Strashkoaca82d12015-09-25 12:28:02 -07001316 irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
1317 irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
Nishanth Menon46824e222014-09-05 14:52:55 -05001318 irqc->name = dev_name(&pdev->dev);
Grygorii Strashko0c0451e2016-04-12 13:52:31 +03001319 irqc->flags = IRQCHIP_MASK_ON_SUSPEND;
Grygorii Strashko46748072018-09-28 16:39:50 -05001320 irqc->parent_device = dev;
Nishanth Menon46824e222014-09-05 14:52:55 -05001321
Grygorii Strashko89d18e32015-08-18 14:10:53 +03001322 bank->irq = platform_get_irq(pdev, 0);
1323 if (bank->irq <= 0) {
1324 if (!bank->irq)
1325 bank->irq = -ENXIO;
1326 if (bank->irq != -EPROBE_DEFER)
1327 dev_err(dev,
1328 "can't get irq resource ret=%d\n", bank->irq);
1329 return bank->irq;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001330 }
1331
Linus Walleij58383c782015-11-04 09:56:26 +01001332 bank->chip.parent = dev;
Grygorii Strashkoc23837c2015-06-25 18:13:33 +03001333 bank->chip.owner = THIS_MODULE;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001334 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001335 bank->quirks = pdata->quirks;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001336 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001337 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301338 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301339 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001340 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001341#ifdef CONFIG_OF_GPIO
1342 bank->chip.of_node = of_node_get(node);
1343#endif
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001344
Jon Huntera2797be2013-04-04 15:16:15 -05001345 if (node) {
1346 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1347 bank->loses_context = true;
1348 } else {
1349 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001350
1351 if (bank->loses_context)
1352 bank->get_context_loss_count =
1353 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001354 }
1355
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001356 if (bank->regs->set_dataout && bank->regs->clr_dataout) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001357 bank->set_dataout = omap_set_gpio_dataout_reg;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001358 bank->set_dataout_multiple = omap_set_gpio_dataout_reg_multiple;
1359 } else {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001360 bank->set_dataout = omap_set_gpio_dataout_mask;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001361 bank->set_dataout_multiple =
1362 omap_set_gpio_dataout_mask_multiple;
1363 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001364
Tony Lindgren00ded242018-12-07 11:08:29 -08001365 if (bank->quirks & OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER) {
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001366 bank->funcs.idle_enable_level_quirk =
1367 omap2_gpio_enable_level_quirk;
1368 bank->funcs.idle_disable_level_quirk =
1369 omap2_gpio_disable_level_quirk;
1370 }
1371
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001372 raw_spin_lock_init(&bank->lock);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001373 raw_spin_lock_init(&bank->wa_lock);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001374
1375 /* Static mapping, never released */
1376 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001377 bank->base = devm_ioremap_resource(dev, res);
1378 if (IS_ERR(bank->base)) {
Jingoo Han717f70e2014-02-12 11:51:38 +09001379 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001380 }
1381
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001382 if (bank->dbck_flag) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001383 bank->dbck = devm_clk_get(dev, "dbclk");
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001384 if (IS_ERR(bank->dbck)) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001385 dev_err(dev,
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001386 "Could not get gpio dbck. Disable debounce\n");
1387 bank->dbck_flag = false;
1388 } else {
1389 clk_prepare(bank->dbck);
1390 }
1391 }
1392
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301393 platform_set_drvdata(pdev, bank);
1394
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001395 pm_runtime_enable(dev);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001396 pm_runtime_get_sync(dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001397
Charulatha Vd0d665a2011-08-31 00:02:21 +05301398 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001399 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301400
Charulatha V03e128c2011-05-05 19:58:01 +05301401 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001402
Nishanth Menon46824e222014-09-05 14:52:55 -05001403 ret = omap_gpio_chip_init(bank, irqc);
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001404 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001405 pm_runtime_put_sync(dev);
1406 pm_runtime_disable(dev);
Arvind Yadave2c3c192017-08-01 12:14:31 +05301407 if (bank->dbck_flag)
1408 clk_unprepare(bank->dbck);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001409 return ret;
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001410 }
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001411
Tony Lindgren9a748052010-12-07 16:26:56 -08001412 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001413
Tony Lindgrenb764a582018-09-20 12:35:31 -07001414 if (bank->funcs.idle_enable_level_quirk &&
1415 bank->funcs.idle_disable_level_quirk) {
1416 bank->nb.notifier_call = gpio_omap_cpu_notifier;
1417 cpu_pm_register_notifier(&bank->nb);
1418 }
1419
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001420 pm_runtime_put(dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301421
Jon Hunter879fe322013-04-04 15:16:12 -05001422 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001423}
1424
Tony Lindgrencac089f2015-04-23 16:56:22 -07001425static int omap_gpio_remove(struct platform_device *pdev)
1426{
1427 struct gpio_bank *bank = platform_get_drvdata(pdev);
1428
Tony Lindgrenb764a582018-09-20 12:35:31 -07001429 if (bank->nb.notifier_call)
1430 cpu_pm_unregister_notifier(&bank->nb);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001431 list_del(&bank->node);
1432 gpiochip_remove(&bank->chip);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001433 pm_runtime_disable(&pdev->dev);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001434 if (bank->dbck_flag)
1435 clk_unprepare(bank->dbck);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001436
1437 return 0;
1438}
1439
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301440static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001441
Tony Lindgrenb764a582018-09-20 12:35:31 -07001442static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301443{
Tony Lindgrenb764a582018-09-20 12:35:31 -07001444 struct device *dev = bank->chip.parent;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301445 u32 l1 = 0, l2 = 0;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001446
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001447 if (bank->funcs.idle_enable_level_quirk)
1448 bank->funcs.idle_enable_level_quirk(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001449
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001450 if (!bank->enabled_non_wakeup_gpios)
1451 goto update_gpio_context_count;
1452
Tony Lindgrenb764a582018-09-20 12:35:31 -07001453 if (!may_lose_context)
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301454 goto update_gpio_context_count;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001455
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301456 /*
1457 * If going to OFF, remove triggering for all
1458 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1459 * generated. See OMAP2420 Errata item 1.101.
1460 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001461 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301462 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301463 l1 = bank->context.fallingdetect;
1464 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301465
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301466 l1 &= ~bank->enabled_non_wakeup_gpios;
1467 l2 &= ~bank->enabled_non_wakeup_gpios;
1468
Victor Kamensky661553b2013-11-16 02:01:04 +02001469 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1470 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301471
1472 bank->workaround_enabled = true;
1473
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301474update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301475 if (bank->get_context_loss_count)
1476 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001477 bank->get_context_loss_count(dev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301478
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001479 omap_gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301480}
1481
Jon Hunter352a2d52013-04-15 13:06:54 -05001482static void omap_gpio_init_context(struct gpio_bank *p);
1483
Tony Lindgrenb764a582018-09-20 12:35:31 -07001484static void omap_gpio_unidle(struct gpio_bank *bank)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301485{
Tony Lindgrenb764a582018-09-20 12:35:31 -07001486 struct device *dev = bank->chip.parent;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301487 u32 l = 0, gen, gen0, gen1;
Jon Huntera2797be2013-04-04 15:16:15 -05001488 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301489
Jon Hunter352a2d52013-04-15 13:06:54 -05001490 /*
1491 * On the first resume during the probe, the context has not
1492 * been initialised and so initialise it now. Also initialise
1493 * the context loss count.
1494 */
1495 if (bank->loses_context && !bank->context_valid) {
1496 omap_gpio_init_context(bank);
1497
1498 if (bank->get_context_loss_count)
1499 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001500 bank->get_context_loss_count(dev);
Jon Hunter352a2d52013-04-15 13:06:54 -05001501 }
1502
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001503 omap_gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001504
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001505 if (bank->funcs.idle_disable_level_quirk)
1506 bank->funcs.idle_disable_level_quirk(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001507
Jon Huntera2797be2013-04-04 15:16:15 -05001508 if (bank->loses_context) {
1509 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301510 omap_gpio_restore_context(bank);
1511 } else {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001512 c = bank->get_context_loss_count(dev);
Jon Huntera2797be2013-04-04 15:16:15 -05001513 if (c != bank->context_loss_count) {
1514 omap_gpio_restore_context(bank);
1515 } else {
Tony Lindgrenb764a582018-09-20 12:35:31 -07001516 return;
Jon Huntera2797be2013-04-04 15:16:15 -05001517 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301518 }
1519 }
1520
Tony Lindgrenb764a582018-09-20 12:35:31 -07001521 if (!bank->workaround_enabled)
1522 return;
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301523
Victor Kamensky661553b2013-11-16 02:01:04 +02001524 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301525
1526 /*
1527 * Check if any of the non-wakeup interrupt GPIOs have changed
1528 * state. If so, generate an IRQ by software. This is
1529 * horribly racy, but it's the best we can do to work around
1530 * this silicon bug.
1531 */
1532 l ^= bank->saved_datain;
1533 l &= bank->enabled_non_wakeup_gpios;
1534
1535 /*
1536 * No need to generate IRQs for the rising edge for gpio IRQs
1537 * configured with falling edge only; and vice versa.
1538 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301539 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301540 gen0 &= bank->saved_datain;
1541
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301542 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301543 gen1 &= ~(bank->saved_datain);
1544
1545 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301546 gen = l & (~(bank->context.fallingdetect) &
1547 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301548 /* Consider all GPIO IRQs needed to be updated */
1549 gen |= gen0 | gen1;
1550
1551 if (gen) {
1552 u32 old0, old1;
1553
Victor Kamensky661553b2013-11-16 02:01:04 +02001554 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1555 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301556
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301557 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001558 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301559 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001560 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301561 bank->regs->leveldetect1);
1562 }
1563
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301564 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001565 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301566 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001567 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301568 bank->regs->leveldetect1);
1569 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001570 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1571 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301572 }
1573
1574 bank->workaround_enabled = false;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001575}
1576
Jon Hunter352a2d52013-04-15 13:06:54 -05001577static void omap_gpio_init_context(struct gpio_bank *p)
1578{
1579 struct omap_gpio_reg_offs *regs = p->regs;
1580 void __iomem *base = p->base;
1581
Victor Kamensky661553b2013-11-16 02:01:04 +02001582 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1583 p->context.oe = readl_relaxed(base + regs->direction);
1584 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1585 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1586 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1587 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1588 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1589 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1590 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001591
1592 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001593 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001594 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001595 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001596
1597 p->context_valid = true;
1598}
1599
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301600static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301601{
Victor Kamensky661553b2013-11-16 02:01:04 +02001602 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301603 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001604 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1605 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301606 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001607 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301608 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001609 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301610 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001611 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301612 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301613 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001614 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301615 bank->base + bank->regs->set_dataout);
1616 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001617 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301618 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001619 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301620
Nishanth Menonae547352011-09-09 19:08:58 +05301621 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001622 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301623 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001624 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301625 bank->base + bank->regs->debounce_en);
1626 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301627
Victor Kamensky661553b2013-11-16 02:01:04 +02001628 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301629 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001630 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301631 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301632}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301633
Tony Lindgrenb764a582018-09-20 12:35:31 -07001634static int __maybe_unused omap_gpio_runtime_suspend(struct device *dev)
1635{
Wolfram Sanga3f4f722018-10-21 21:59:59 +02001636 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001637 unsigned long flags;
1638 int error = 0;
1639
1640 raw_spin_lock_irqsave(&bank->lock, flags);
1641 /* Must be idled only by CPU_CLUSTER_PM_ENTER? */
1642 if (bank->irq_usage) {
1643 error = -EBUSY;
1644 goto unlock;
1645 }
1646 omap_gpio_idle(bank, true);
1647 bank->is_suspended = true;
1648unlock:
1649 raw_spin_unlock_irqrestore(&bank->lock, flags);
1650
1651 return error;
1652}
1653
1654static int __maybe_unused omap_gpio_runtime_resume(struct device *dev)
1655{
Wolfram Sanga3f4f722018-10-21 21:59:59 +02001656 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001657 unsigned long flags;
1658 int error = 0;
1659
1660 raw_spin_lock_irqsave(&bank->lock, flags);
1661 /* Must be unidled only by CPU_CLUSTER_PM_ENTER? */
1662 if (bank->irq_usage) {
1663 error = -EBUSY;
1664 goto unlock;
1665 }
1666 omap_gpio_unidle(bank);
1667 bank->is_suspended = false;
1668unlock:
1669 raw_spin_unlock_irqrestore(&bank->lock, flags);
1670
1671 return error;
1672}
1673
1674#ifdef CONFIG_ARCH_OMAP2PLUS
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301675static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301676 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1677 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301678};
Tony Lindgrenb764a582018-09-20 12:35:31 -07001679#else
1680static const struct dev_pm_ops gpio_pm_ops;
1681#endif /* CONFIG_ARCH_OMAP2PLUS */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301682
Benoit Cousson384ebe12011-08-16 11:53:02 +02001683#if defined(CONFIG_OF)
1684static struct omap_gpio_reg_offs omap2_gpio_regs = {
1685 .revision = OMAP24XX_GPIO_REVISION,
1686 .direction = OMAP24XX_GPIO_OE,
1687 .datain = OMAP24XX_GPIO_DATAIN,
1688 .dataout = OMAP24XX_GPIO_DATAOUT,
1689 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1690 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1691 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1692 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1693 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1694 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1695 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1696 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1697 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1698 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1699 .ctrl = OMAP24XX_GPIO_CTRL,
1700 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1701 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1702 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1703 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1704 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1705};
1706
1707static struct omap_gpio_reg_offs omap4_gpio_regs = {
1708 .revision = OMAP4_GPIO_REVISION,
1709 .direction = OMAP4_GPIO_OE,
1710 .datain = OMAP4_GPIO_DATAIN,
1711 .dataout = OMAP4_GPIO_DATAOUT,
1712 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1713 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1714 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1715 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1716 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1717 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1718 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1719 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1720 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1721 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1722 .ctrl = OMAP4_GPIO_CTRL,
1723 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1724 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1725 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1726 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1727 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1728};
1729
Tony Lindgrenb764a582018-09-20 12:35:31 -07001730/*
1731 * Note that omap2 does not currently support idle modes with context loss so
1732 * no need to add OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER quirk flag to save
1733 * and restore context.
1734 */
Chen Gange9a65bb2013-02-06 18:44:32 +08001735static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001736 .regs = &omap2_gpio_regs,
1737 .bank_width = 32,
1738 .dbck_flag = false,
1739};
1740
Chen Gange9a65bb2013-02-06 18:44:32 +08001741static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001742 .regs = &omap2_gpio_regs,
1743 .bank_width = 32,
1744 .dbck_flag = true,
Tony Lindgrenb764a582018-09-20 12:35:31 -07001745 .quirks = OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001746};
1747
Chen Gange9a65bb2013-02-06 18:44:32 +08001748static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001749 .regs = &omap4_gpio_regs,
1750 .bank_width = 32,
1751 .dbck_flag = true,
Tony Lindgren00ded242018-12-07 11:08:29 -08001752 .quirks = OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001753};
1754
1755static const struct of_device_id omap_gpio_match[] = {
1756 {
1757 .compatible = "ti,omap4-gpio",
1758 .data = &omap4_pdata,
1759 },
1760 {
1761 .compatible = "ti,omap3-gpio",
1762 .data = &omap3_pdata,
1763 },
1764 {
1765 .compatible = "ti,omap2-gpio",
1766 .data = &omap2_pdata,
1767 },
1768 { },
1769};
1770MODULE_DEVICE_TABLE(of, omap_gpio_match);
1771#endif
1772
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001773static struct platform_driver omap_gpio_driver = {
1774 .probe = omap_gpio_probe,
Tony Lindgrencac089f2015-04-23 16:56:22 -07001775 .remove = omap_gpio_remove,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001776 .driver = {
1777 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301778 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001779 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001780 },
1781};
1782
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001783/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001784 * gpio driver register needs to be done before
1785 * machine_init functions access gpio APIs.
1786 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001787 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001788static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001789{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001790 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001791}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001792postcore_initcall(omap_gpio_drv_reg);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001793
1794static void __exit omap_gpio_exit(void)
1795{
1796 platform_driver_unregister(&omap_gpio_driver);
1797}
1798module_exit(omap_gpio_exit);
1799
1800MODULE_DESCRIPTION("omap gpio driver");
1801MODULE_ALIAS("platform:gpio-omap");
1802MODULE_LICENSE("GPL v2");