blob: cc320d09d0746ca7d2d0913e6936b245e5ff5037 [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
Charulatha V6d62e212011-04-18 15:06:51 +000034struct gpio_regs {
35 u32 irqenable1;
36 u32 irqenable2;
37 u32 wake_en;
38 u32 ctrl;
39 u32 oe;
40 u32 leveldetect0;
41 u32 leveldetect1;
42 u32 risingdetect;
43 u32 fallingdetect;
44 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053045 u32 debounce;
46 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000047};
48
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010049struct gpio_bank {
Tony Lindgren92105bb2005-09-07 17:20:26 +010050 void __iomem *base;
Grygorii Strashko30cefea2015-09-25 12:06:02 -070051 int irq;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080052 u32 non_wakeup_gpios;
53 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000054 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080055 u32 saved_datain;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080056 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080057 u32 toggle_mask;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +020058 raw_spinlock_t lock;
Grygorii Strashko450fa542015-09-25 12:28:03 -070059 raw_spinlock_t wa_lock;
David Brownell52e31342008-03-03 12:43:23 -080060 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080061 struct clk *dbck;
Tony Lindgrenb764a582018-09-20 12:35:31 -070062 struct notifier_block nb;
63 unsigned int is_suspended:1;
Charulatha V058af1e2009-11-22 10:11:25 -080064 u32 mod_usage;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020065 u32 irq_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080066 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +053067 bool dbck_enabled;
Charulatha Vd0d665a2011-08-31 00:02:21 +053068 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080069 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053070 bool loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -050071 bool context_valid;
Tony Lindgren5de62b82010-12-07 16:26:58 -080072 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070073 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053074 int context_loss_count;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070075
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +020076 void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable);
Janusz Krzysztofik442af142018-07-19 01:57:08 +020077 void (*set_dataout_multiple)(struct gpio_bank *bank,
78 unsigned long *mask, unsigned long *bits);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053079 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070080
81 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010082};
83
Charulatha Vc8eef652011-05-02 15:21:42 +053084#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020086#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020087#define LINE_USED(line, offset) (line & (BIT(offset)))
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020088
Tony Lindgren3d009c82015-01-16 14:50:50 -080089static void omap_gpio_unmask_irq(struct irq_data *d);
90
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020091static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -060092{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +020093 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
Linus Walleijd99f7ae2015-12-07 11:16:00 +010094 return gpiochip_get_data(chip);
Benoit Cousson25db7112012-02-23 21:50:10 +010095}
96
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020097static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
98 int is_input)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100100 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100101 u32 l;
102
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700103 reg += bank->regs->direction;
Victor Kamensky661553b2013-11-16 02:01:04 +0200104 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100105 if (is_input)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200106 l |= BIT(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100107 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200108 l &= ~(BIT(gpio));
Victor Kamensky661553b2013-11-16 02:01:04 +0200109 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530110 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100111}
112
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700113
114/* set data out value using dedicate set/clear register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200115static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200116 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100117{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100118 void __iomem *reg = bank->base;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200119 u32 l = BIT(offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100120
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530121 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700122 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530123 bank->context.dataout |= l;
124 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700125 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530126 bank->context.dataout &= ~l;
127 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700128
Victor Kamensky661553b2013-11-16 02:01:04 +0200129 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700130}
131
132/* set data out value using mask register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200133static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200134 int enable)
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700135{
136 void __iomem *reg = bank->base + bank->regs->dataout;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200137 u32 gpio_bit = BIT(offset);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700138 u32 l;
139
Victor Kamensky661553b2013-11-16 02:01:04 +0200140 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700141 if (enable)
142 l |= gpio_bit;
143 else
144 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200145 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530146 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100147}
148
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200149static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700151 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100152
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200153 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100154}
155
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200156static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300157{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700158 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300159
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200160 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300161}
162
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200163/* set multiple data out values using dedicate set/clear register */
164static void omap_set_gpio_dataout_reg_multiple(struct gpio_bank *bank,
165 unsigned long *mask,
166 unsigned long *bits)
167{
168 void __iomem *reg = bank->base;
169 u32 l;
170
171 l = *bits & *mask;
172 writel_relaxed(l, reg + bank->regs->set_dataout);
173 bank->context.dataout |= l;
174
175 l = ~*bits & *mask;
176 writel_relaxed(l, reg + bank->regs->clr_dataout);
177 bank->context.dataout &= ~l;
178}
179
180/* set multiple data out values using mask register */
181static void omap_set_gpio_dataout_mask_multiple(struct gpio_bank *bank,
182 unsigned long *mask,
183 unsigned long *bits)
184{
185 void __iomem *reg = bank->base + bank->regs->dataout;
186 u32 l = (readl_relaxed(reg) & ~*mask) | (*bits & *mask);
187
188 writel_relaxed(l, reg);
189 bank->context.dataout = l;
190}
191
192static unsigned long omap_get_gpio_datain_multiple(struct gpio_bank *bank,
193 unsigned long *mask)
194{
195 void __iomem *reg = bank->base + bank->regs->datain;
196
197 return readl_relaxed(reg) & *mask;
198}
199
200static unsigned long omap_get_gpio_dataout_multiple(struct gpio_bank *bank,
201 unsigned long *mask)
202{
203 void __iomem *reg = bank->base + bank->regs->dataout;
204
205 return readl_relaxed(reg) & *mask;
206}
207
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200208static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700209{
Victor Kamensky661553b2013-11-16 02:01:04 +0200210 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700211
Benoit Cousson862ff642012-02-01 15:58:56 +0100212 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700213 l |= mask;
214 else
215 l &= ~mask;
216
Victor Kamensky661553b2013-11-16 02:01:04 +0200217 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700218}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100219
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200220static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530221{
222 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300223 clk_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530224 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300225
Victor Kamensky661553b2013-11-16 02:01:04 +0200226 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300227 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530228 }
229}
230
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200231static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530232{
233 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300234 /*
235 * Disable debounce before cutting it's clock. If debounce is
236 * enabled but the clock is not, GPIO module seems to be unable
237 * to detect events and generate interrupts at least on OMAP3.
238 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200239 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300240
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300241 clk_disable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530242 bank->dbck_enabled = false;
243 }
244}
245
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700246/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200247 * omap2_set_gpio_debounce - low level gpio debounce time
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700248 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200249 * @offset: the gpio number on this @bank
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700250 * @debounce: debounce time to use
251 *
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300252 * OMAP's debounce time is in 31us steps
253 * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
254 * so we need to convert and round up to the closest unit.
David Rivshin83977442017-04-24 18:56:50 -0400255 *
256 * Return: 0 on success, negative error otherwise.
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700257 */
David Rivshin83977442017-04-24 18:56:50 -0400258static int omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
259 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700260{
Kevin Hilman9942da02011-04-22 12:02:05 -0700261 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700262 u32 val;
263 u32 l;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300264 bool enable = !!debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700265
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800266 if (!bank->dbck_flag)
David Rivshin83977442017-04-24 18:56:50 -0400267 return -ENOTSUPP;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800268
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300269 if (enable) {
270 debounce = DIV_ROUND_UP(debounce, 31) - 1;
David Rivshin83977442017-04-24 18:56:50 -0400271 if ((debounce & OMAP4_GPIO_DEBOUNCINGTIME_MASK) != debounce)
272 return -EINVAL;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300273 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700274
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200275 l = BIT(offset);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700276
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300277 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700278 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200279 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700280
Kevin Hilman9942da02011-04-22 12:02:05 -0700281 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200282 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700283
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300284 if (enable)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700285 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530286 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700287 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300288 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700289
Victor Kamensky661553b2013-11-16 02:01:04 +0200290 writel_relaxed(val, reg);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300291 clk_disable(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530292 /*
293 * Enable debounce clock per module.
294 * This call is mandatory because in omap_gpio_request() when
295 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
296 * runtime callbck fails to turn on dbck because dbck_enable_mask
297 * used within _gpio_dbck_enable() is still not initialized at
298 * that point. Therefore we have to enable dbck here.
299 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200300 omap_gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530301 if (bank->dbck_enable_mask) {
302 bank->context.debounce = debounce;
303 bank->context.debounce_en = val;
304 }
David Rivshin83977442017-04-24 18:56:50 -0400305
306 return 0;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700307}
308
Jon Hunterc9c55d92012-10-26 14:26:04 -0500309/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200310 * omap_clear_gpio_debounce - clear debounce settings for a gpio
Jon Hunterc9c55d92012-10-26 14:26:04 -0500311 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200312 * @offset: the gpio number on this @bank
Jon Hunterc9c55d92012-10-26 14:26:04 -0500313 *
314 * If a gpio is using debounce, then clear the debounce enable bit and if
315 * this is the only gpio in this bank using debounce, then clear the debounce
316 * time too. The debounce clock will also be disabled when calling this function
317 * if this is the only gpio in the bank using debounce.
318 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200319static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
Jon Hunterc9c55d92012-10-26 14:26:04 -0500320{
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200321 u32 gpio_bit = BIT(offset);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500322
323 if (!bank->dbck_flag)
324 return;
325
326 if (!(bank->dbck_enable_mask & gpio_bit))
327 return;
328
329 bank->dbck_enable_mask &= ~gpio_bit;
330 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200331 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500332 bank->base + bank->regs->debounce_en);
333
334 if (!bank->dbck_enable_mask) {
335 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200336 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500337 bank->regs->debounce);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300338 clk_disable(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500339 bank->dbck_enabled = false;
340 }
341}
342
Tony Lindgrenda38ef32019-03-25 15:43:18 -0700343/*
344 * Off mode wake-up capable GPIOs in bank(s) that are in the wakeup domain.
345 * See TRM section for GPIO for "Wake-Up Generation" for the list of GPIOs
346 * in wakeup domain. If bank->non_wakeup_gpios is not configured, assume none
347 * are capable waking up the system from off mode.
348 */
349static bool omap_gpio_is_off_wakeup_capable(struct gpio_bank *bank, u32 gpio_mask)
350{
351 u32 no_wake = bank->non_wakeup_gpios;
352
353 if (no_wake)
354 return !!(~no_wake & gpio_mask);
355
356 return false;
357}
358
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200359static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530360 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100361{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800362 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200363 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100364
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200365 omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
366 trigger & IRQ_TYPE_LEVEL_LOW);
367 omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
368 trigger & IRQ_TYPE_LEVEL_HIGH);
Russell Kinge6818d22019-04-08 12:46:53 -0700369
370 /*
371 * We need the edge detection enabled for to allow the GPIO block
372 * to be woken from idle state. Set the appropriate edge detection
373 * in addition to the level detection.
374 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200375 omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
Russell Kinge6818d22019-04-08 12:46:53 -0700376 trigger & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH));
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200377 omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
Russell Kinge6818d22019-04-08 12:46:53 -0700378 trigger & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW));
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530379
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530380 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200381 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530382 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200383 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530384 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200385 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530386 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200387 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530388
389 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Tony Lindgren00ded242018-12-07 11:08:29 -0800390 omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
391 bank->context.wake_en =
392 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530393 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530394
Ambresh K55b220c2011-06-15 13:40:45 -0700395 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tony Lindgrenda38ef32019-03-25 15:43:18 -0700396 if (!bank->regs->irqctrl && !omap_gpio_is_off_wakeup_capable(bank, gpio)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000397 /*
398 * Log the edge gpio and manually trigger the IRQ
399 * after resume if the input level changes
400 * to avoid irq lost during PER RET/OFF mode
401 * Applies for omap2 non-wakeup gpio and all omap3 gpios
402 */
403 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800404 bank->enabled_non_wakeup_gpios |= gpio_bit;
405 else
406 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
407 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700408
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530409 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200410 readl_relaxed(bank->base + bank->regs->leveldetect0) |
411 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100412}
413
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800414#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800415/*
416 * This only applies to chips that can't do both rising and falling edge
417 * detection at once. For all other chips, this function is a noop.
418 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200419static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800420{
421 void __iomem *reg = bank->base;
422 u32 l = 0;
423
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530424 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800425 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530426
427 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800428
Victor Kamensky661553b2013-11-16 02:01:04 +0200429 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800430 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200431 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800432 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200433 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800434
Victor Kamensky661553b2013-11-16 02:01:04 +0200435 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800436}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530437#else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200438static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800439#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800440
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200441static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
442 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100443{
444 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530445 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100446 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100447
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530448 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200449 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530450 } else if (bank->regs->irqctrl) {
451 reg += bank->regs->irqctrl;
452
Victor Kamensky661553b2013-11-16 02:01:04 +0200453 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000454 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200455 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100456 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200457 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100458 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200459 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100460 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530461 return -EINVAL;
462
Victor Kamensky661553b2013-11-16 02:01:04 +0200463 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530464 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100465 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530466 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100467 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530468 reg += bank->regs->edgectrl1;
469
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100470 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200471 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100472 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100473 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100474 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100475 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200476 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530477
478 /* Enable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200479 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530480 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200481 readl_relaxed(bank->base + bank->regs->wkup_en);
482 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100483 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100484 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100485}
486
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200487static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200488{
489 if (bank->regs->pinctrl) {
490 void __iomem *reg = bank->base + bank->regs->pinctrl;
491
492 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200493 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200494 }
495
496 if (bank->regs->ctrl && !BANK_USED(bank)) {
497 void __iomem *reg = bank->base + bank->regs->ctrl;
498 u32 ctrl;
499
Victor Kamensky661553b2013-11-16 02:01:04 +0200500 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200501 /* Module is enabled, clocks are not gated */
502 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200503 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200504 bank->context.ctrl = ctrl;
505 }
506}
507
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200508static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200509{
510 void __iomem *base = bank->base;
511
512 if (bank->regs->wkup_en &&
513 !LINE_USED(bank->mod_usage, offset) &&
514 !LINE_USED(bank->irq_usage, offset)) {
515 /* Disable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200516 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200517 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200518 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200519 }
520
521 if (bank->regs->ctrl && !BANK_USED(bank)) {
522 void __iomem *reg = bank->base + bank->regs->ctrl;
523 u32 ctrl;
524
Victor Kamensky661553b2013-11-16 02:01:04 +0200525 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200526 /* Module is disabled, clocks are gated */
527 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200528 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200529 bank->context.ctrl = ctrl;
530 }
531}
532
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200533static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200534{
535 void __iomem *reg = bank->base + bank->regs->direction;
536
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200537 return readl_relaxed(reg) & BIT(offset);
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200538}
539
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200540static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
Tony Lindgren3d009c82015-01-16 14:50:50 -0800541{
542 if (!LINE_USED(bank->mod_usage, offset)) {
543 omap_enable_gpio_module(bank, offset);
544 omap_set_gpio_direction(bank, offset, 1);
545 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200546 bank->irq_usage |= BIT(offset);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800547}
548
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200549static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100550{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200551 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100552 int retval;
David Brownella6472532008-03-03 04:33:30 -0800553 unsigned long flags;
Grygorii Strashkoea5fbe82015-03-23 14:18:29 +0200554 unsigned offset = d->hwirq;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100555
David Brownelle5c56ed2006-12-06 17:13:59 -0800556 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100557 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800558
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530559 if (!bank->regs->leveldetect0 &&
560 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100561 return -EINVAL;
562
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200563 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200564 retval = omap_set_gpio_triggering(bank, offset, type);
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300565 if (retval) {
Axel Lin627c89b2015-08-05 22:37:41 +0800566 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300567 goto error;
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300568 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200569 omap_gpio_init_irq(bank, offset);
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200570 if (!omap_gpio_is_input(bank, offset)) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200571 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300572 retval = -EINVAL;
573 goto error;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200574 }
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200575 raw_spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800576
577 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner43ec2e42015-06-23 15:52:39 +0200578 irq_set_handler_locked(d, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800579 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500580 /*
581 * Edge IRQs are already cleared/acked in irq_handler and
582 * not need to be masked, as result handle_edge_irq()
583 * logic is excessed here and may cause lose of interrupts.
584 * So just use handle_simple_irq.
585 */
586 irq_set_handler_locked(d, handle_simple_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800587
Grygorii Strashko1562e462015-05-22 17:35:49 +0300588 return 0;
589
590error:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100591 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100592}
593
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200594static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100595{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100596 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100597
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700598 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200599 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300600
601 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700602 if (bank->regs->irqstatus2) {
603 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200604 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700605 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700606
607 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200608 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100609}
610
Grygorii Strashko9943f262015-03-23 14:18:27 +0200611static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
612 unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100613{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200614 omap_clear_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100615}
616
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200617static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
Imre Deakea6dedd2006-06-26 16:16:00 -0700618{
619 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700620 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200621 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700622
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700623 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200624 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700625 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700626 l = ~l;
627 l &= mask;
628 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700629}
630
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200631static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100632{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100633 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100634 u32 l;
635
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700636 if (bank->regs->set_irqenable) {
637 reg += bank->regs->set_irqenable;
638 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530639 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700640 } else {
641 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200642 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700643 if (bank->regs->irqenable_inv)
644 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100645 else
646 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530647 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100648 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700649
Victor Kamensky661553b2013-11-16 02:01:04 +0200650 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700651}
652
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200653static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700654{
655 void __iomem *reg = bank->base;
656 u32 l;
657
658 if (bank->regs->clr_irqenable) {
659 reg += bank->regs->clr_irqenable;
660 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530661 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700662 } else {
663 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200664 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700665 if (bank->regs->irqenable_inv)
666 l |= gpio_mask;
667 else
668 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530669 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700670 }
671
Victor Kamensky661553b2013-11-16 02:01:04 +0200672 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100673}
674
Grygorii Strashko9943f262015-03-23 14:18:27 +0200675static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
676 unsigned offset, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100677{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530678 if (enable)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200679 omap_enable_gpio_irqbank(bank, BIT(offset));
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530680 else
Grygorii Strashko9943f262015-03-23 14:18:27 +0200681 omap_disable_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100682}
683
Tony Lindgren92105bb2005-09-07 17:20:26 +0100684/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200685static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100686{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200687 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100688
Grygorii Strashko0c0451e2016-04-12 13:52:31 +0300689 return irq_set_irq_wake(bank->irq, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100690}
691
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100692/*
693 * We need to unmask the GPIO bank interrupt as soon as possible to
694 * avoid missing GPIO interrupts for other lines in the bank.
695 * Then we need to mask-read-clear-unmask the triggered GPIO lines
696 * in the bank to avoid missing nested interrupts for a GPIO line.
697 * If we wait to unmask individual GPIO lines in the bank after the
698 * line's interrupt handler has been run, we may miss some nested
699 * interrupts.
700 */
Grygorii Strashko450fa542015-09-25 12:28:03 -0700701static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100703 void __iomem *isr_reg = NULL;
Russell King395373c2019-06-10 20:10:47 +0300704 u32 enabled, isr, edge;
Jon Hunter3513cde2013-04-04 15:16:14 -0500705 unsigned int bit;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700706 struct gpio_bank *bank = gpiobank;
707 unsigned long wa_lock_flags;
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300708 unsigned long lock_flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100709
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700710 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800711 if (WARN_ON(!isr_reg))
712 goto exit;
713
Tony Lindgren52845212018-09-20 12:35:32 -0700714 if (WARN_ONCE(!pm_runtime_active(bank->chip.parent),
715 "gpio irq%i while runtime suspended?\n", irq))
716 return IRQ_NONE;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700717
Laurent Navete83507b2013-03-20 13:15:57 +0100718 while (1) {
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300719 raw_spin_lock_irqsave(&bank->lock, lock_flags);
720
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200721 enabled = omap_get_gpio_irqbank_mask(bank);
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500722 isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100723
Russell King395373c2019-06-10 20:10:47 +0300724 /*
725 * Clear edge sensitive interrupts before calling handler(s)
726 * so subsequent edge transitions are not missed while the
727 * handlers are running.
728 */
729 edge = isr & ~bank->level_mask;
730 if (edge)
731 omap_clear_gpio_irqbank(bank, edge);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100732
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300733 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
734
Tony Lindgren92105bb2005-09-07 17:20:26 +0100735 if (!isr)
736 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100737
Jon Hunter3513cde2013-04-04 15:16:14 -0500738 while (isr) {
739 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200740 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100741
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300742 raw_spin_lock_irqsave(&bank->lock, lock_flags);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800743 /*
744 * Some chips can't respond to both rising and falling
745 * at the same time. If this irq was requested with
746 * both flags, we need to flip the ICR data for the IRQ
747 * to respond to the IRQ for the opposite direction.
748 * This will be indicated in the bank toggle_mask.
749 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200750 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200751 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800752
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300753 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
754
Grygorii Strashko450fa542015-09-25 12:28:03 -0700755 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
756
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100757 generic_handle_irq(irq_find_mapping(bank->chip.irq.domain,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200758 bit));
Grygorii Strashko450fa542015-09-25 12:28:03 -0700759
760 raw_spin_unlock_irqrestore(&bank->wa_lock,
761 wa_lock_flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100762 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000763 }
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800764exit:
Grygorii Strashko450fa542015-09-25 12:28:03 -0700765 return IRQ_HANDLED;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100766}
767
Tony Lindgren3d009c82015-01-16 14:50:50 -0800768static unsigned int omap_gpio_irq_startup(struct irq_data *d)
769{
770 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800771 unsigned long flags;
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200772 unsigned offset = d->hwirq;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800773
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200774 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300775
776 if (!LINE_USED(bank->mod_usage, offset))
777 omap_set_gpio_direction(bank, offset, 1);
778 else if (!omap_gpio_is_input(bank, offset))
779 goto err;
780 omap_enable_gpio_module(bank, offset);
781 bank->irq_usage |= BIT(offset);
782
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200783 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800784 omap_gpio_unmask_irq(d);
785
786 return 0;
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300787err:
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200788 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300789 return -EINVAL;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800790}
791
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200792static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300793{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200794 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700795 unsigned long flags;
Grygorii Strashko9943f262015-03-23 14:18:27 +0200796 unsigned offset = d->hwirq;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300797
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200798 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200799 bank->irq_usage &= ~(BIT(offset));
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300800 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Russell Kingc859e0d2019-06-10 20:10:44 +0300801 omap_clear_gpio_irqstatus(bank, offset);
802 omap_set_gpio_irqenable(bank, offset, 0);
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300803 if (!LINE_USED(bank->mod_usage, offset))
804 omap_clear_gpio_debounce(bank, offset);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200805 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200806 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700807}
808
809static void omap_gpio_irq_bus_lock(struct irq_data *data)
810{
811 struct gpio_bank *bank = omap_irq_data_get_bank(data);
812
Grygorii Strashko46748072018-09-28 16:39:50 -0500813 pm_runtime_get_sync(bank->chip.parent);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700814}
815
816static void gpio_irq_bus_sync_unlock(struct irq_data *data)
817{
818 struct gpio_bank *bank = omap_irq_data_get_bank(data);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200819
Grygorii Strashko46748072018-09-28 16:39:50 -0500820 pm_runtime_put(bank->chip.parent);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300821}
822
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200823static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100824{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200825 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200826 unsigned offset = d->hwirq;
Colin Cross85ec7b92011-06-06 13:38:18 -0700827 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100828
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200829 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200830 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Russell Kingc859e0d2019-06-10 20:10:44 +0300831 omap_set_gpio_irqenable(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200832 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100833}
834
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200835static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100836{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200837 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200838 unsigned offset = d->hwirq;
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100839 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700840 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700841
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200842 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200843 omap_set_gpio_irqenable(bank, offset, 1);
Russell Kingd01849f2019-03-01 11:02:52 -0800844
845 /*
846 * For level-triggered GPIOs, clearing must be done after the source
847 * is cleared, thus after the handler has run. OMAP4 needs this done
848 * after enabing the interrupt to clear the wakeup status.
849 */
Russell Kingc859e0d2019-06-10 20:10:44 +0300850 if (bank->regs->leveldetect0 && bank->regs->wkup_en &&
851 trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
Russell Kingd01849f2019-03-01 11:02:52 -0800852 omap_clear_gpio_irqstatus(bank, offset);
853
Russell Kingc859e0d2019-06-10 20:10:44 +0300854 if (trigger)
855 omap_set_gpio_triggering(bank, offset, trigger);
856
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200857 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100858}
859
David Brownelle5c56ed2006-12-06 17:13:59 -0800860/*---------------------------------------------------------------------*/
861
Magnus Damm79ee0312009-07-08 13:22:04 +0200862static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800863{
Wolfram Sanga3f4f722018-10-21 21:59:59 +0200864 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800865 void __iomem *mask_reg = bank->base +
866 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800867 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800868
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200869 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200870 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200871 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800872
873 return 0;
874}
875
Magnus Damm79ee0312009-07-08 13:22:04 +0200876static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800877{
Wolfram Sanga3f4f722018-10-21 21:59:59 +0200878 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800879 void __iomem *mask_reg = bank->base +
880 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800881 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800882
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200883 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200884 writel_relaxed(bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200885 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800886
887 return 0;
888}
889
Alexey Dobriyan47145212009-12-14 18:00:08 -0800890static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200891 .suspend_noirq = omap_mpuio_suspend_noirq,
892 .resume_noirq = omap_mpuio_resume_noirq,
893};
894
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200895/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800896static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800897 .driver = {
898 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200899 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800900 },
901};
902
903static struct platform_device omap_mpuio_device = {
904 .name = "mpuio",
905 .id = -1,
906 .dev = {
907 .driver = &omap_mpuio_driver.driver,
908 }
909 /* could list the /proc/iomem resources */
910};
911
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200912static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800913{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800914 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700915
David Brownell11a78b72006-12-06 17:14:11 -0800916 if (platform_driver_register(&omap_mpuio_driver) == 0)
917 (void) platform_device_register(&omap_mpuio_device);
918}
919
David Brownelle5c56ed2006-12-06 17:13:59 -0800920/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100921
Russell Kingdfbc6c72019-06-10 20:10:49 +0300922static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
923{
924 struct gpio_bank *bank = gpiochip_get_data(chip);
925 unsigned long flags;
926
927 pm_runtime_get_sync(chip->parent);
928
929 raw_spin_lock_irqsave(&bank->lock, flags);
930 omap_enable_gpio_module(bank, offset);
931 bank->mod_usage |= BIT(offset);
932 raw_spin_unlock_irqrestore(&bank->lock, flags);
933
934 return 0;
935}
936
937static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
938{
939 struct gpio_bank *bank = gpiochip_get_data(chip);
940 unsigned long flags;
941
942 raw_spin_lock_irqsave(&bank->lock, flags);
943 bank->mod_usage &= ~(BIT(offset));
944 if (!LINE_USED(bank->irq_usage, offset)) {
945 omap_set_gpio_direction(bank, offset, 1);
946 omap_clear_gpio_debounce(bank, offset);
947 }
948 omap_disable_gpio_module(bank, offset);
949 raw_spin_unlock_irqrestore(&bank->lock, flags);
950
951 pm_runtime_put(chip->parent);
952}
953
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200954static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200955{
956 struct gpio_bank *bank;
957 unsigned long flags;
958 void __iomem *reg;
959 int dir;
960
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100961 bank = gpiochip_get_data(chip);
Yegor Yefremov93700842014-04-24 08:57:39 +0200962 reg = bank->base + bank->regs->direction;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200963 raw_spin_lock_irqsave(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +0200964 dir = !!(readl_relaxed(reg) & BIT(offset));
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200965 raw_spin_unlock_irqrestore(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +0200966 return dir;
967}
968
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200969static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800970{
971 struct gpio_bank *bank;
972 unsigned long flags;
973
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100974 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200975 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200976 omap_set_gpio_direction(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200977 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800978 return 0;
979}
980
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200981static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800982{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300983 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300984
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100985 bank = gpiochip_get_data(chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300986
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200987 if (omap_gpio_is_input(bank, offset))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200988 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300989 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200990 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -0800991}
992
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200993static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800994{
995 struct gpio_bank *bank;
996 unsigned long flags;
997
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100998 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200999 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001000 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001001 omap_set_gpio_direction(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001002 raw_spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +02001003 return 0;
David Brownell52e31342008-03-03 12:43:23 -08001004}
1005
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001006static int omap_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
1007 unsigned long *bits)
1008{
1009 struct gpio_bank *bank = gpiochip_get_data(chip);
1010 void __iomem *reg = bank->base + bank->regs->direction;
1011 unsigned long in = readl_relaxed(reg), l;
1012
1013 *bits = 0;
1014
1015 l = in & *mask;
1016 if (l)
1017 *bits |= omap_get_gpio_datain_multiple(bank, &l);
1018
1019 l = ~in & *mask;
1020 if (l)
1021 *bits |= omap_get_gpio_dataout_multiple(bank, &l);
1022
1023 return 0;
1024}
1025
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001026static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
1027 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001028{
1029 struct gpio_bank *bank;
1030 unsigned long flags;
David Rivshin83977442017-04-24 18:56:50 -04001031 int ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001032
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001033 bank = gpiochip_get_data(chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001034
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001035 raw_spin_lock_irqsave(&bank->lock, flags);
David Rivshin83977442017-04-24 18:56:50 -04001036 ret = omap2_set_gpio_debounce(bank, offset, debounce);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001037 raw_spin_unlock_irqrestore(&bank->lock, flags);
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001038
David Rivshin83977442017-04-24 18:56:50 -04001039 if (ret)
1040 dev_info(chip->parent,
1041 "Could not set line %u debounce to %u microseconds (%d)",
1042 offset, debounce, ret);
1043
1044 return ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001045}
1046
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001047static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset,
1048 unsigned long config)
1049{
1050 u32 debounce;
1051
1052 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
1053 return -ENOTSUPP;
1054
1055 debounce = pinconf_to_config_argument(config);
1056 return omap_gpio_debounce(chip, offset, debounce);
1057}
1058
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001059static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001060{
1061 struct gpio_bank *bank;
1062 unsigned long flags;
1063
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001064 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001065 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001066 bank->set_dataout(bank, offset, value);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001067 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -08001068}
1069
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001070static void omap_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
1071 unsigned long *bits)
1072{
1073 struct gpio_bank *bank = gpiochip_get_data(chip);
1074 unsigned long flags;
1075
1076 raw_spin_lock_irqsave(&bank->lock, flags);
1077 bank->set_dataout_multiple(bank, mask, bits);
1078 raw_spin_unlock_irqrestore(&bank->lock, flags);
1079}
1080
David Brownell52e31342008-03-03 12:43:23 -08001081/*---------------------------------------------------------------------*/
1082
Arnd Bergmanne4b2ae72017-09-16 22:42:21 +02001083static void omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001084{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001085 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001086 u32 rev;
1087
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001088 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001089 return;
1090
Victor Kamensky661553b2013-11-16 02:01:04 +02001091 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001092 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001093 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001094
1095 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001096}
1097
Charulatha V03e128c2011-05-05 19:58:01 +05301098static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001099{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301100 void __iomem *base = bank->base;
1101 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001102
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301103 if (bank->width == 16)
1104 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001105
Charulatha Vd0d665a2011-08-31 00:02:21 +05301106 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001107 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301108 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001109 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301110
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001111 omap_gpio_rmw(base, bank->regs->irqenable, l,
1112 bank->regs->irqenable_inv);
1113 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1114 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301115 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001116 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301117
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301118 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001119 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301120 /* Initialize interface clk ungated, module enabled */
1121 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001122 writel_relaxed(0, base + bank->regs->ctrl);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001123}
1124
Nishanth Menon46824e222014-09-05 14:52:55 -05001125static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001126{
Grygorii Strashko81930322017-11-15 12:36:33 -06001127 struct gpio_irq_chip *irq;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001128 static int gpio;
Linus Walleij088413b2017-12-29 13:22:58 +01001129 const char *label;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001130 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001131 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001132
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001133 /*
1134 * REVISIT eventually switch from OMAP-specific gpio structs
1135 * over to the generic ones
1136 */
1137 bank->chip.request = omap_gpio_request;
1138 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001139 bank->chip.get_direction = omap_gpio_get_direction;
1140 bank->chip.direction_input = omap_gpio_input;
1141 bank->chip.get = omap_gpio_get;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001142 bank->chip.get_multiple = omap_gpio_get_multiple;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001143 bank->chip.direction_output = omap_gpio_output;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001144 bank->chip.set_config = omap_gpio_set_config;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001145 bank->chip.set = omap_gpio_set;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001146 bank->chip.set_multiple = omap_gpio_set_multiple;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301147 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001148 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301149 if (bank->regs->wkup_en)
Linus Walleij58383c782015-11-04 09:56:26 +01001150 bank->chip.parent = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001151 bank->chip.base = OMAP_MPUIO(0);
1152 } else {
Linus Walleij088413b2017-12-29 13:22:58 +01001153 label = devm_kasprintf(bank->chip.parent, GFP_KERNEL, "gpio-%d-%d",
1154 gpio, gpio + bank->width - 1);
1155 if (!label)
1156 return -ENOMEM;
1157 bank->chip.label = label;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001158 bank->chip.base = gpio;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001159 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001160 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001161
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001162#ifdef CONFIG_ARCH_OMAP1
1163 /*
1164 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1165 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1166 */
Bartosz Golaszewski2ed36f32017-03-04 17:23:31 +01001167 irq_base = devm_irq_alloc_descs(bank->chip.parent,
1168 -1, 0, bank->width, 0);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001169 if (irq_base < 0) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001170 dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n");
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001171 return -ENODEV;
1172 }
1173#endif
1174
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001175 /* MPUIO is a bit different, reading IRQ status clears it */
Russell King693de832019-06-10 20:10:48 +03001176 if (bank->is_mpuio && !bank->regs->wkup_en)
1177 irqc->irq_set_wake = NULL;
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001178
Grygorii Strashko81930322017-11-15 12:36:33 -06001179 irq = &bank->chip.irq;
1180 irq->chip = irqc;
1181 irq->handler = handle_bad_irq;
1182 irq->default_type = IRQ_TYPE_NONE;
1183 irq->num_parents = 1;
1184 irq->parents = &bank->irq;
1185 irq->first = irq_base;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001186
Grygorii Strashko81930322017-11-15 12:36:33 -06001187 ret = gpiochip_add_data(&bank->chip, bank);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001188 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001189 dev_err(bank->chip.parent,
Grygorii Strashko81930322017-11-15 12:36:33 -06001190 "Could not register gpio chip %d\n", ret);
1191 return ret;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001192 }
1193
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001194 ret = devm_request_irq(bank->chip.parent, bank->irq,
1195 omap_gpio_irq_handler,
1196 0, dev_name(bank->chip.parent), bank);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001197 if (ret)
1198 gpiochip_remove(&bank->chip);
1199
Grygorii Strashko81930322017-11-15 12:36:33 -06001200 if (!bank->is_mpuio)
1201 gpio += bank->width;
1202
Grygorii Strashko450fa542015-09-25 12:28:03 -07001203 return ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001204}
1205
Arnd Bergmann7c685712019-03-07 11:33:32 +01001206static void omap_gpio_init_context(struct gpio_bank *p)
1207{
1208 struct omap_gpio_reg_offs *regs = p->regs;
1209 void __iomem *base = p->base;
1210
1211 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1212 p->context.oe = readl_relaxed(base + regs->direction);
1213 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1214 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1215 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1216 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1217 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1218 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1219 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
1220
1221 if (regs->set_dataout && p->regs->clr_dataout)
1222 p->context.dataout = readl_relaxed(base + regs->set_dataout);
1223 else
1224 p->context.dataout = readl_relaxed(base + regs->dataout);
1225
1226 p->context_valid = true;
1227}
1228
1229static void omap_gpio_restore_context(struct gpio_bank *bank)
1230{
1231 writel_relaxed(bank->context.wake_en,
1232 bank->base + bank->regs->wkup_en);
1233 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1234 writel_relaxed(bank->context.leveldetect0,
1235 bank->base + bank->regs->leveldetect0);
1236 writel_relaxed(bank->context.leveldetect1,
1237 bank->base + bank->regs->leveldetect1);
1238 writel_relaxed(bank->context.risingdetect,
1239 bank->base + bank->regs->risingdetect);
1240 writel_relaxed(bank->context.fallingdetect,
1241 bank->base + bank->regs->fallingdetect);
1242 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1243 writel_relaxed(bank->context.dataout,
1244 bank->base + bank->regs->set_dataout);
1245 else
1246 writel_relaxed(bank->context.dataout,
1247 bank->base + bank->regs->dataout);
1248 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
1249
1250 if (bank->dbck_enable_mask) {
1251 writel_relaxed(bank->context.debounce, bank->base +
1252 bank->regs->debounce);
1253 writel_relaxed(bank->context.debounce_en,
1254 bank->base + bank->regs->debounce_en);
1255 }
1256
1257 writel_relaxed(bank->context.irqenable1,
1258 bank->base + bank->regs->irqenable);
1259 writel_relaxed(bank->context.irqenable2,
1260 bank->base + bank->regs->irqenable2);
1261}
1262
1263static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context)
1264{
1265 struct device *dev = bank->chip.parent;
Tony Lindgren21e21182019-03-25 15:43:16 -07001266 void __iomem *base = bank->base;
1267 u32 nowake;
1268
1269 bank->saved_datain = readl_relaxed(base + bank->regs->datain);
Arnd Bergmann7c685712019-03-07 11:33:32 +01001270
Arnd Bergmann7c685712019-03-07 11:33:32 +01001271 if (!bank->enabled_non_wakeup_gpios)
1272 goto update_gpio_context_count;
1273
1274 if (!may_lose_context)
1275 goto update_gpio_context_count;
1276
1277 /*
Tony Lindgren21e21182019-03-25 15:43:16 -07001278 * If going to OFF, remove triggering for all wkup domain
Arnd Bergmann7c685712019-03-07 11:33:32 +01001279 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1280 * generated. See OMAP2420 Errata item 1.101.
1281 */
Tony Lindgren21e21182019-03-25 15:43:16 -07001282 if (!bank->loses_context && bank->enabled_non_wakeup_gpios) {
1283 nowake = bank->enabled_non_wakeup_gpios;
1284 omap_gpio_rmw(base, bank->regs->fallingdetect, nowake, ~nowake);
1285 omap_gpio_rmw(base, bank->regs->risingdetect, nowake, ~nowake);
1286 }
Arnd Bergmann7c685712019-03-07 11:33:32 +01001287
1288update_gpio_context_count:
1289 if (bank->get_context_loss_count)
1290 bank->context_loss_count =
1291 bank->get_context_loss_count(dev);
1292
1293 omap_gpio_dbck_disable(bank);
1294}
1295
1296static void omap_gpio_unidle(struct gpio_bank *bank)
1297{
1298 struct device *dev = bank->chip.parent;
1299 u32 l = 0, gen, gen0, gen1;
1300 int c;
1301
1302 /*
1303 * On the first resume during the probe, the context has not
1304 * been initialised and so initialise it now. Also initialise
1305 * the context loss count.
1306 */
1307 if (bank->loses_context && !bank->context_valid) {
1308 omap_gpio_init_context(bank);
1309
1310 if (bank->get_context_loss_count)
1311 bank->context_loss_count =
1312 bank->get_context_loss_count(dev);
1313 }
1314
1315 omap_gpio_dbck_enable(bank);
1316
Arnd Bergmann7c685712019-03-07 11:33:32 +01001317 if (bank->loses_context) {
1318 if (!bank->get_context_loss_count) {
1319 omap_gpio_restore_context(bank);
1320 } else {
1321 c = bank->get_context_loss_count(dev);
1322 if (c != bank->context_loss_count) {
1323 omap_gpio_restore_context(bank);
1324 } else {
1325 return;
1326 }
1327 }
Tony Lindgren21e21182019-03-25 15:43:16 -07001328 } else {
1329 /* Restore changes done for OMAP2420 errata 1.101 */
1330 writel_relaxed(bank->context.fallingdetect,
1331 bank->base + bank->regs->fallingdetect);
1332 writel_relaxed(bank->context.risingdetect,
1333 bank->base + bank->regs->risingdetect);
Arnd Bergmann7c685712019-03-07 11:33:32 +01001334 }
1335
Arnd Bergmann7c685712019-03-07 11:33:32 +01001336 l = readl_relaxed(bank->base + bank->regs->datain);
1337
1338 /*
1339 * Check if any of the non-wakeup interrupt GPIOs have changed
1340 * state. If so, generate an IRQ by software. This is
1341 * horribly racy, but it's the best we can do to work around
1342 * this silicon bug.
1343 */
1344 l ^= bank->saved_datain;
1345 l &= bank->enabled_non_wakeup_gpios;
1346
1347 /*
1348 * No need to generate IRQs for the rising edge for gpio IRQs
1349 * configured with falling edge only; and vice versa.
1350 */
1351 gen0 = l & bank->context.fallingdetect;
1352 gen0 &= bank->saved_datain;
1353
1354 gen1 = l & bank->context.risingdetect;
1355 gen1 &= ~(bank->saved_datain);
1356
1357 /* FIXME: Consider GPIO IRQs with level detections properly! */
1358 gen = l & (~(bank->context.fallingdetect) &
1359 ~(bank->context.risingdetect));
1360 /* Consider all GPIO IRQs needed to be updated */
1361 gen |= gen0 | gen1;
1362
1363 if (gen) {
1364 u32 old0, old1;
1365
1366 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1367 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
1368
1369 if (!bank->regs->irqstatus_raw0) {
1370 writel_relaxed(old0 | gen, bank->base +
1371 bank->regs->leveldetect0);
1372 writel_relaxed(old1 | gen, bank->base +
1373 bank->regs->leveldetect1);
1374 }
1375
1376 if (bank->regs->irqstatus_raw0) {
1377 writel_relaxed(old0 | l, bank->base +
1378 bank->regs->leveldetect0);
1379 writel_relaxed(old1 | l, bank->base +
1380 bank->regs->leveldetect1);
1381 }
1382 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1383 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
1384 }
Arnd Bergmann7c685712019-03-07 11:33:32 +01001385}
Tony Lindgrenb764a582018-09-20 12:35:31 -07001386
1387static int gpio_omap_cpu_notifier(struct notifier_block *nb,
1388 unsigned long cmd, void *v)
1389{
1390 struct gpio_bank *bank;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001391 unsigned long flags;
1392
1393 bank = container_of(nb, struct gpio_bank, nb);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001394
1395 raw_spin_lock_irqsave(&bank->lock, flags);
1396 switch (cmd) {
1397 case CPU_CLUSTER_PM_ENTER:
1398 if (bank->is_suspended)
1399 break;
1400 omap_gpio_idle(bank, true);
1401 break;
1402 case CPU_CLUSTER_PM_ENTER_FAILED:
1403 case CPU_CLUSTER_PM_EXIT:
1404 if (bank->is_suspended)
1405 break;
1406 omap_gpio_unidle(bank);
1407 break;
1408 }
1409 raw_spin_unlock_irqrestore(&bank->lock, flags);
1410
1411 return NOTIFY_OK;
1412}
1413
Arnd Bergmann7c685712019-03-07 11:33:32 +01001414static struct omap_gpio_reg_offs omap2_gpio_regs = {
1415 .revision = OMAP24XX_GPIO_REVISION,
1416 .direction = OMAP24XX_GPIO_OE,
1417 .datain = OMAP24XX_GPIO_DATAIN,
1418 .dataout = OMAP24XX_GPIO_DATAOUT,
1419 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1420 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1421 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1422 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1423 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1424 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1425 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1426 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1427 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1428 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1429 .ctrl = OMAP24XX_GPIO_CTRL,
1430 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1431 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1432 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1433 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1434 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1435};
1436
1437static struct omap_gpio_reg_offs omap4_gpio_regs = {
1438 .revision = OMAP4_GPIO_REVISION,
1439 .direction = OMAP4_GPIO_OE,
1440 .datain = OMAP4_GPIO_DATAIN,
1441 .dataout = OMAP4_GPIO_DATAOUT,
1442 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1443 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1444 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1445 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
Russell King64ea3e92019-06-10 20:10:45 +03001446 .irqstatus_raw0 = OMAP4_GPIO_IRQSTATUSRAW0,
1447 .irqstatus_raw1 = OMAP4_GPIO_IRQSTATUSRAW1,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001448 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1449 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1450 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1451 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1452 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1453 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1454 .ctrl = OMAP4_GPIO_CTRL,
1455 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1456 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1457 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1458 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1459 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1460};
1461
Arnd Bergmann7c685712019-03-07 11:33:32 +01001462static const struct omap_gpio_platform_data omap2_pdata = {
1463 .regs = &omap2_gpio_regs,
1464 .bank_width = 32,
1465 .dbck_flag = false,
1466};
1467
1468static const struct omap_gpio_platform_data omap3_pdata = {
1469 .regs = &omap2_gpio_regs,
1470 .bank_width = 32,
1471 .dbck_flag = true,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001472};
1473
1474static const struct omap_gpio_platform_data omap4_pdata = {
1475 .regs = &omap4_gpio_regs,
1476 .bank_width = 32,
1477 .dbck_flag = true,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001478};
1479
1480static const struct of_device_id omap_gpio_match[] = {
1481 {
1482 .compatible = "ti,omap4-gpio",
1483 .data = &omap4_pdata,
1484 },
1485 {
1486 .compatible = "ti,omap3-gpio",
1487 .data = &omap3_pdata,
1488 },
1489 {
1490 .compatible = "ti,omap2-gpio",
1491 .data = &omap2_pdata,
1492 },
1493 { },
1494};
1495MODULE_DEVICE_TABLE(of, omap_gpio_match);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001496
Bill Pemberton38363092012-11-19 13:22:34 -05001497static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001498{
Benoit Cousson862ff642012-02-01 15:58:56 +01001499 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001500 struct device_node *node = dev->of_node;
1501 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001502 const struct omap_gpio_platform_data *pdata;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001503 struct gpio_bank *bank;
Nishanth Menon46824e222014-09-05 14:52:55 -05001504 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001505 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001506
Benoit Cousson384ebe12011-08-16 11:53:02 +02001507 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1508
Jingoo Hane56aee12013-07-30 17:08:05 +09001509 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001510 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001511 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001512
Markus Elfringf97364c2018-02-10 21:49:22 +01001513 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
Markus Elfring9117d402018-02-10 21:46:30 +01001514 if (!bank)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001515 return -ENOMEM;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001516
Nishanth Menon46824e222014-09-05 14:52:55 -05001517 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1518 if (!irqc)
1519 return -ENOMEM;
1520
Tony Lindgren3d009c82015-01-16 14:50:50 -08001521 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e222014-09-05 14:52:55 -05001522 irqc->irq_shutdown = omap_gpio_irq_shutdown,
Russell King693de832019-06-10 20:10:48 +03001523 irqc->irq_ack = dummy_irq_chip.irq_ack,
Nishanth Menon46824e222014-09-05 14:52:55 -05001524 irqc->irq_mask = omap_gpio_mask_irq,
1525 irqc->irq_unmask = omap_gpio_unmask_irq,
1526 irqc->irq_set_type = omap_gpio_irq_type,
1527 irqc->irq_set_wake = omap_gpio_wake_enable,
Grygorii Strashkoaca82d12015-09-25 12:28:02 -07001528 irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
1529 irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
Nishanth Menon46824e222014-09-05 14:52:55 -05001530 irqc->name = dev_name(&pdev->dev);
Grygorii Strashko0c0451e2016-04-12 13:52:31 +03001531 irqc->flags = IRQCHIP_MASK_ON_SUSPEND;
Grygorii Strashko46748072018-09-28 16:39:50 -05001532 irqc->parent_device = dev;
Nishanth Menon46824e222014-09-05 14:52:55 -05001533
Grygorii Strashko89d18e32015-08-18 14:10:53 +03001534 bank->irq = platform_get_irq(pdev, 0);
1535 if (bank->irq <= 0) {
1536 if (!bank->irq)
1537 bank->irq = -ENXIO;
1538 if (bank->irq != -EPROBE_DEFER)
1539 dev_err(dev,
1540 "can't get irq resource ret=%d\n", bank->irq);
1541 return bank->irq;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001542 }
1543
Linus Walleij58383c782015-11-04 09:56:26 +01001544 bank->chip.parent = dev;
Grygorii Strashkoc23837c2015-06-25 18:13:33 +03001545 bank->chip.owner = THIS_MODULE;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001546 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001547 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001548 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301549 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301550 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001551 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001552#ifdef CONFIG_OF_GPIO
1553 bank->chip.of_node = of_node_get(node);
1554#endif
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001555
Jon Huntera2797be2013-04-04 15:16:15 -05001556 if (node) {
1557 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1558 bank->loses_context = true;
1559 } else {
1560 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001561
1562 if (bank->loses_context)
1563 bank->get_context_loss_count =
1564 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001565 }
1566
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001567 if (bank->regs->set_dataout && bank->regs->clr_dataout) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001568 bank->set_dataout = omap_set_gpio_dataout_reg;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001569 bank->set_dataout_multiple = omap_set_gpio_dataout_reg_multiple;
1570 } else {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001571 bank->set_dataout = omap_set_gpio_dataout_mask;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001572 bank->set_dataout_multiple =
1573 omap_set_gpio_dataout_mask_multiple;
1574 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001575
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001576 raw_spin_lock_init(&bank->lock);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001577 raw_spin_lock_init(&bank->wa_lock);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001578
1579 /* Static mapping, never released */
Enrico Weigelt, metux IT consult58f57f82019-03-11 20:50:05 +01001580 bank->base = devm_platform_ioremap_resource(pdev, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001581 if (IS_ERR(bank->base)) {
Jingoo Han717f70e2014-02-12 11:51:38 +09001582 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001583 }
1584
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001585 if (bank->dbck_flag) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001586 bank->dbck = devm_clk_get(dev, "dbclk");
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001587 if (IS_ERR(bank->dbck)) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001588 dev_err(dev,
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001589 "Could not get gpio dbck. Disable debounce\n");
1590 bank->dbck_flag = false;
1591 } else {
1592 clk_prepare(bank->dbck);
1593 }
1594 }
1595
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301596 platform_set_drvdata(pdev, bank);
1597
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001598 pm_runtime_enable(dev);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001599 pm_runtime_get_sync(dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001600
Charulatha Vd0d665a2011-08-31 00:02:21 +05301601 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001602 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301603
Charulatha V03e128c2011-05-05 19:58:01 +05301604 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001605
Nishanth Menon46824e222014-09-05 14:52:55 -05001606 ret = omap_gpio_chip_init(bank, irqc);
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001607 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001608 pm_runtime_put_sync(dev);
1609 pm_runtime_disable(dev);
Arvind Yadave2c3c192017-08-01 12:14:31 +05301610 if (bank->dbck_flag)
1611 clk_unprepare(bank->dbck);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001612 return ret;
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001613 }
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001614
Tony Lindgren9a748052010-12-07 16:26:56 -08001615 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001616
Russell Kinge6818d22019-04-08 12:46:53 -07001617 bank->nb.notifier_call = gpio_omap_cpu_notifier;
1618 cpu_pm_register_notifier(&bank->nb);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001619
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001620 pm_runtime_put(dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301621
Jon Hunter879fe322013-04-04 15:16:12 -05001622 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001623}
1624
Tony Lindgrencac089f2015-04-23 16:56:22 -07001625static int omap_gpio_remove(struct platform_device *pdev)
1626{
1627 struct gpio_bank *bank = platform_get_drvdata(pdev);
1628
Russell Kinge6818d22019-04-08 12:46:53 -07001629 cpu_pm_unregister_notifier(&bank->nb);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001630 gpiochip_remove(&bank->chip);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001631 pm_runtime_disable(&pdev->dev);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001632 if (bank->dbck_flag)
1633 clk_unprepare(bank->dbck);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001634
1635 return 0;
1636}
1637
Tony Lindgrenb764a582018-09-20 12:35:31 -07001638static int __maybe_unused omap_gpio_runtime_suspend(struct device *dev)
1639{
Wolfram Sanga3f4f722018-10-21 21:59:59 +02001640 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001641 unsigned long flags;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001642
1643 raw_spin_lock_irqsave(&bank->lock, flags);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001644 omap_gpio_idle(bank, true);
1645 bank->is_suspended = true;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001646 raw_spin_unlock_irqrestore(&bank->lock, flags);
1647
Russell King044e4992019-04-10 12:51:13 -07001648 return 0;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001649}
1650
1651static int __maybe_unused omap_gpio_runtime_resume(struct device *dev)
1652{
Wolfram Sanga3f4f722018-10-21 21:59:59 +02001653 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001654 unsigned long flags;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001655
1656 raw_spin_lock_irqsave(&bank->lock, flags);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001657 omap_gpio_unidle(bank);
1658 bank->is_suspended = false;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001659 raw_spin_unlock_irqrestore(&bank->lock, flags);
1660
Russell King044e4992019-04-10 12:51:13 -07001661 return 0;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001662}
1663
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301664static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301665 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1666 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301667};
Benoit Cousson384ebe12011-08-16 11:53:02 +02001668
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001669static struct platform_driver omap_gpio_driver = {
1670 .probe = omap_gpio_probe,
Tony Lindgrencac089f2015-04-23 16:56:22 -07001671 .remove = omap_gpio_remove,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001672 .driver = {
1673 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301674 .pm = &gpio_pm_ops,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001675 .of_match_table = omap_gpio_match,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001676 },
1677};
1678
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001679/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001680 * gpio driver register needs to be done before
1681 * machine_init functions access gpio APIs.
1682 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001683 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001684static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001685{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001686 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001687}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001688postcore_initcall(omap_gpio_drv_reg);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001689
1690static void __exit omap_gpio_exit(void)
1691{
1692 platform_driver_unregister(&omap_gpio_driver);
1693}
1694module_exit(omap_gpio_exit);
1695
1696MODULE_DESCRIPTION("omap gpio driver");
1697MODULE_ALIAS("platform:gpio-omap");
1698MODULE_LICENSE("GPL v2");