blob: 6ed621a0056182f8b9c7e2af450c478e90c01b19 [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#define OMAP_GPIO_QUIRK_DEFERRED_WKUP_EN BIT(1)
36
Charulatha V6d62e212011-04-18 15:06:51 +000037struct gpio_regs {
38 u32 irqenable1;
39 u32 irqenable2;
40 u32 wake_en;
41 u32 ctrl;
42 u32 oe;
43 u32 leveldetect0;
44 u32 leveldetect1;
45 u32 risingdetect;
46 u32 fallingdetect;
47 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053048 u32 debounce;
49 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000050};
51
Tony Lindgrenec0daae2018-09-20 12:35:30 -070052struct gpio_bank;
53
54struct gpio_omap_funcs {
55 void (*idle_enable_level_quirk)(struct gpio_bank *bank);
56 void (*idle_disable_level_quirk)(struct gpio_bank *bank);
57};
58
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010059struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053060 struct list_head node;
Tony Lindgren92105bb2005-09-07 17:20:26 +010061 void __iomem *base;
Grygorii Strashko30cefea2015-09-25 12:06:02 -070062 int irq;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080063 u32 non_wakeup_gpios;
64 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000065 struct gpio_regs context;
Tony Lindgrenec0daae2018-09-20 12:35:30 -070066 struct gpio_omap_funcs funcs;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080067 u32 saved_datain;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080068 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080069 u32 toggle_mask;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +020070 raw_spinlock_t lock;
Grygorii Strashko450fa542015-09-25 12:28:03 -070071 raw_spinlock_t wa_lock;
David Brownell52e31342008-03-03 12:43:23 -080072 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080073 struct clk *dbck;
Tony Lindgrenb764a582018-09-20 12:35:31 -070074 struct notifier_block nb;
75 unsigned int is_suspended:1;
Charulatha V058af1e2009-11-22 10:11:25 -080076 u32 mod_usage;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020077 u32 irq_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080078 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +053079 bool dbck_enabled;
Charulatha Vd0d665a2011-08-31 00:02:21 +053080 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080081 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053082 bool loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -050083 bool context_valid;
Tony Lindgren5de62b82010-12-07 16:26:58 -080084 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070085 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053086 int context_loss_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053087 bool workaround_enabled;
Tony Lindgrenec0daae2018-09-20 12:35:30 -070088 u32 quirks;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070089
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +020090 void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable);
Janusz Krzysztofik442af142018-07-19 01:57:08 +020091 void (*set_dataout_multiple)(struct gpio_bank *bank,
92 unsigned long *mask, unsigned long *bits);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053093 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070094
95 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010096};
97
Charulatha Vc8eef652011-05-02 15:21:42 +053098#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200100#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200101#define LINE_USED(line, offset) (line & (BIT(offset)))
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200102
Tony Lindgren3d009c82015-01-16 14:50:50 -0800103static void omap_gpio_unmask_irq(struct irq_data *d);
104
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200105static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -0600106{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200107 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100108 return gpiochip_get_data(chip);
Benoit Cousson25db7112012-02-23 21:50:10 +0100109}
110
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200111static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
112 int is_input)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100113{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100114 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100115 u32 l;
116
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700117 reg += bank->regs->direction;
Victor Kamensky661553b2013-11-16 02:01:04 +0200118 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100119 if (is_input)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200120 l |= BIT(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200122 l &= ~(BIT(gpio));
Victor Kamensky661553b2013-11-16 02:01:04 +0200123 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530124 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100125}
126
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700127
128/* set data out value using dedicate set/clear register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200129static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200130 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100131{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100132 void __iomem *reg = bank->base;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200133 u32 l = BIT(offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530135 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700136 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530137 bank->context.dataout |= l;
138 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700139 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530140 bank->context.dataout &= ~l;
141 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700142
Victor Kamensky661553b2013-11-16 02:01:04 +0200143 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700144}
145
146/* set data out value using mask register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200147static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200148 int enable)
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700149{
150 void __iomem *reg = bank->base + bank->regs->dataout;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200151 u32 gpio_bit = BIT(offset);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700152 u32 l;
153
Victor Kamensky661553b2013-11-16 02:01:04 +0200154 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700155 if (enable)
156 l |= gpio_bit;
157 else
158 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200159 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530160 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100161}
162
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200163static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100164{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700165 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100166
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200167 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100168}
169
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200170static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300171{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700172 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300173
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200174 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300175}
176
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200177/* set multiple data out values using dedicate set/clear register */
178static void omap_set_gpio_dataout_reg_multiple(struct gpio_bank *bank,
179 unsigned long *mask,
180 unsigned long *bits)
181{
182 void __iomem *reg = bank->base;
183 u32 l;
184
185 l = *bits & *mask;
186 writel_relaxed(l, reg + bank->regs->set_dataout);
187 bank->context.dataout |= l;
188
189 l = ~*bits & *mask;
190 writel_relaxed(l, reg + bank->regs->clr_dataout);
191 bank->context.dataout &= ~l;
192}
193
194/* set multiple data out values using mask register */
195static void omap_set_gpio_dataout_mask_multiple(struct gpio_bank *bank,
196 unsigned long *mask,
197 unsigned long *bits)
198{
199 void __iomem *reg = bank->base + bank->regs->dataout;
200 u32 l = (readl_relaxed(reg) & ~*mask) | (*bits & *mask);
201
202 writel_relaxed(l, reg);
203 bank->context.dataout = l;
204}
205
206static unsigned long omap_get_gpio_datain_multiple(struct gpio_bank *bank,
207 unsigned long *mask)
208{
209 void __iomem *reg = bank->base + bank->regs->datain;
210
211 return readl_relaxed(reg) & *mask;
212}
213
214static unsigned long omap_get_gpio_dataout_multiple(struct gpio_bank *bank,
215 unsigned long *mask)
216{
217 void __iomem *reg = bank->base + bank->regs->dataout;
218
219 return readl_relaxed(reg) & *mask;
220}
221
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200222static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700223{
Victor Kamensky661553b2013-11-16 02:01:04 +0200224 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700225
Benoit Cousson862ff642012-02-01 15:58:56 +0100226 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700227 l |= mask;
228 else
229 l &= ~mask;
230
Victor Kamensky661553b2013-11-16 02:01:04 +0200231 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700232}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100233
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200234static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530235{
236 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300237 clk_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530238 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300239
Victor Kamensky661553b2013-11-16 02:01:04 +0200240 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300241 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530242 }
243}
244
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200245static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530246{
247 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300248 /*
249 * Disable debounce before cutting it's clock. If debounce is
250 * enabled but the clock is not, GPIO module seems to be unable
251 * to detect events and generate interrupts at least on OMAP3.
252 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200253 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300254
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300255 clk_disable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530256 bank->dbck_enabled = false;
257 }
258}
259
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700260/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200261 * omap2_set_gpio_debounce - low level gpio debounce time
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700262 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200263 * @offset: the gpio number on this @bank
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700264 * @debounce: debounce time to use
265 *
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300266 * OMAP's debounce time is in 31us steps
267 * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
268 * so we need to convert and round up to the closest unit.
David Rivshin83977442017-04-24 18:56:50 -0400269 *
270 * Return: 0 on success, negative error otherwise.
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700271 */
David Rivshin83977442017-04-24 18:56:50 -0400272static int omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
273 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700274{
Kevin Hilman9942da02011-04-22 12:02:05 -0700275 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700276 u32 val;
277 u32 l;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300278 bool enable = !!debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700279
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800280 if (!bank->dbck_flag)
David Rivshin83977442017-04-24 18:56:50 -0400281 return -ENOTSUPP;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800282
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300283 if (enable) {
284 debounce = DIV_ROUND_UP(debounce, 31) - 1;
David Rivshin83977442017-04-24 18:56:50 -0400285 if ((debounce & OMAP4_GPIO_DEBOUNCINGTIME_MASK) != debounce)
286 return -EINVAL;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300287 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700288
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200289 l = BIT(offset);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700290
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300291 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700292 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200293 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700294
Kevin Hilman9942da02011-04-22 12:02:05 -0700295 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200296 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700297
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300298 if (enable)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700299 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530300 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700301 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300302 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700303
Victor Kamensky661553b2013-11-16 02:01:04 +0200304 writel_relaxed(val, reg);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300305 clk_disable(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530306 /*
307 * Enable debounce clock per module.
308 * This call is mandatory because in omap_gpio_request() when
309 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
310 * runtime callbck fails to turn on dbck because dbck_enable_mask
311 * used within _gpio_dbck_enable() is still not initialized at
312 * that point. Therefore we have to enable dbck here.
313 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200314 omap_gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530315 if (bank->dbck_enable_mask) {
316 bank->context.debounce = debounce;
317 bank->context.debounce_en = val;
318 }
David Rivshin83977442017-04-24 18:56:50 -0400319
320 return 0;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700321}
322
Jon Hunterc9c55d92012-10-26 14:26:04 -0500323/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200324 * omap_clear_gpio_debounce - clear debounce settings for a gpio
Jon Hunterc9c55d92012-10-26 14:26:04 -0500325 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200326 * @offset: the gpio number on this @bank
Jon Hunterc9c55d92012-10-26 14:26:04 -0500327 *
328 * If a gpio is using debounce, then clear the debounce enable bit and if
329 * this is the only gpio in this bank using debounce, then clear the debounce
330 * time too. The debounce clock will also be disabled when calling this function
331 * if this is the only gpio in the bank using debounce.
332 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200333static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
Jon Hunterc9c55d92012-10-26 14:26:04 -0500334{
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200335 u32 gpio_bit = BIT(offset);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500336
337 if (!bank->dbck_flag)
338 return;
339
340 if (!(bank->dbck_enable_mask & gpio_bit))
341 return;
342
343 bank->dbck_enable_mask &= ~gpio_bit;
344 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200345 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500346 bank->base + bank->regs->debounce_en);
347
348 if (!bank->dbck_enable_mask) {
349 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200350 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500351 bank->regs->debounce);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300352 clk_disable(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500353 bank->dbck_enabled = false;
354 }
355}
356
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200357static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530358 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100359{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800360 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200361 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100362
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200363 omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
364 trigger & IRQ_TYPE_LEVEL_LOW);
365 omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
366 trigger & IRQ_TYPE_LEVEL_HIGH);
367 omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
368 trigger & IRQ_TYPE_EDGE_RISING);
369 omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
370 trigger & IRQ_TYPE_EDGE_FALLING);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530371
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530372 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200373 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530374 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200375 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530376 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200377 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530378 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200379 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530380
381 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Tony Lindgrenec0daae2018-09-20 12:35:30 -0700382 /* Defer wkup_en register update until we idle? */
383 if (bank->quirks & OMAP_GPIO_QUIRK_DEFERRED_WKUP_EN) {
384 if (trigger)
385 bank->context.wake_en |= gpio_bit;
386 else
387 bank->context.wake_en &= ~gpio_bit;
388 } else {
389 omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit,
390 trigger != 0);
391 bank->context.wake_en =
392 readl_relaxed(bank->base + bank->regs->wkup_en);
393 }
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530394 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530395
Ambresh K55b220c2011-06-15 13:40:45 -0700396 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530397 if (!bank->regs->irqctrl) {
398 /* On omap24xx proceed only when valid GPIO bit is set */
399 if (bank->non_wakeup_gpios) {
400 if (!(bank->non_wakeup_gpios & gpio_bit))
401 goto exit;
402 }
403
Chunqiu Wang699117a62009-06-24 17:13:39 +0000404 /*
405 * Log the edge gpio and manually trigger the IRQ
406 * after resume if the input level changes
407 * to avoid irq lost during PER RET/OFF mode
408 * Applies for omap2 non-wakeup gpio and all omap3 gpios
409 */
410 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800411 bank->enabled_non_wakeup_gpios |= gpio_bit;
412 else
413 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
414 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700415
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530416exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530417 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200418 readl_relaxed(bank->base + bank->regs->leveldetect0) |
419 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100420}
421
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800422#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800423/*
424 * This only applies to chips that can't do both rising and falling edge
425 * detection at once. For all other chips, this function is a noop.
426 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200427static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800428{
429 void __iomem *reg = bank->base;
430 u32 l = 0;
431
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530432 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800433 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530434
435 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800436
Victor Kamensky661553b2013-11-16 02:01:04 +0200437 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800438 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200439 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800440 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200441 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800442
Victor Kamensky661553b2013-11-16 02:01:04 +0200443 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800444}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530445#else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200446static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800447#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800448
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200449static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
450 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100451{
452 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530453 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100454 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100455
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530456 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200457 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530458 } else if (bank->regs->irqctrl) {
459 reg += bank->regs->irqctrl;
460
Victor Kamensky661553b2013-11-16 02:01:04 +0200461 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000462 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200463 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100464 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200465 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100466 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200467 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100468 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530469 return -EINVAL;
470
Victor Kamensky661553b2013-11-16 02:01:04 +0200471 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530472 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100473 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530474 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530476 reg += bank->regs->edgectrl1;
477
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100478 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200479 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100480 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100481 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100482 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100483 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200484 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530485
486 /* Enable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200487 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530488 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200489 readl_relaxed(bank->base + bank->regs->wkup_en);
490 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100491 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100492 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100493}
494
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200495static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200496{
497 if (bank->regs->pinctrl) {
498 void __iomem *reg = bank->base + bank->regs->pinctrl;
499
500 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200501 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200502 }
503
504 if (bank->regs->ctrl && !BANK_USED(bank)) {
505 void __iomem *reg = bank->base + bank->regs->ctrl;
506 u32 ctrl;
507
Victor Kamensky661553b2013-11-16 02:01:04 +0200508 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200509 /* Module is enabled, clocks are not gated */
510 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200511 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200512 bank->context.ctrl = ctrl;
513 }
514}
515
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200516static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200517{
518 void __iomem *base = bank->base;
519
520 if (bank->regs->wkup_en &&
521 !LINE_USED(bank->mod_usage, offset) &&
522 !LINE_USED(bank->irq_usage, offset)) {
523 /* Disable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200524 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200525 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200526 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200527 }
528
529 if (bank->regs->ctrl && !BANK_USED(bank)) {
530 void __iomem *reg = bank->base + bank->regs->ctrl;
531 u32 ctrl;
532
Victor Kamensky661553b2013-11-16 02:01:04 +0200533 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200534 /* Module is disabled, clocks are gated */
535 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200536 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200537 bank->context.ctrl = ctrl;
538 }
539}
540
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200541static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200542{
543 void __iomem *reg = bank->base + bank->regs->direction;
544
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200545 return readl_relaxed(reg) & BIT(offset);
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200546}
547
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200548static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
Tony Lindgren3d009c82015-01-16 14:50:50 -0800549{
550 if (!LINE_USED(bank->mod_usage, offset)) {
551 omap_enable_gpio_module(bank, offset);
552 omap_set_gpio_direction(bank, offset, 1);
553 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200554 bank->irq_usage |= BIT(offset);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800555}
556
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200557static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100558{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200559 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100560 int retval;
David Brownella6472532008-03-03 04:33:30 -0800561 unsigned long flags;
Grygorii Strashkoea5fbe82015-03-23 14:18:29 +0200562 unsigned offset = d->hwirq;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100563
David Brownelle5c56ed2006-12-06 17:13:59 -0800564 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100565 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800566
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530567 if (!bank->regs->leveldetect0 &&
568 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100569 return -EINVAL;
570
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200571 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200572 retval = omap_set_gpio_triggering(bank, offset, type);
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300573 if (retval) {
Axel Lin627c89b2015-08-05 22:37:41 +0800574 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300575 goto error;
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300576 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200577 omap_gpio_init_irq(bank, offset);
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200578 if (!omap_gpio_is_input(bank, offset)) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200579 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300580 retval = -EINVAL;
581 goto error;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200582 }
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200583 raw_spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800584
585 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner43ec2e42015-06-23 15:52:39 +0200586 irq_set_handler_locked(d, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800587 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500588 /*
589 * Edge IRQs are already cleared/acked in irq_handler and
590 * not need to be masked, as result handle_edge_irq()
591 * logic is excessed here and may cause lose of interrupts.
592 * So just use handle_simple_irq.
593 */
594 irq_set_handler_locked(d, handle_simple_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800595
Grygorii Strashko1562e462015-05-22 17:35:49 +0300596 return 0;
597
598error:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100599 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100600}
601
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200602static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100603{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100604 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100605
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700606 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200607 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300608
609 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700610 if (bank->regs->irqstatus2) {
611 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200612 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700613 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700614
615 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200616 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100617}
618
Grygorii Strashko9943f262015-03-23 14:18:27 +0200619static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
620 unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100621{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200622 omap_clear_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100623}
624
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200625static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
Imre Deakea6dedd2006-06-26 16:16:00 -0700626{
627 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700628 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200629 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700630
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700631 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200632 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700633 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700634 l = ~l;
635 l &= mask;
636 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700637}
638
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200639static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100640{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100641 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100642 u32 l;
643
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700644 if (bank->regs->set_irqenable) {
645 reg += bank->regs->set_irqenable;
646 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530647 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700648 } else {
649 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200650 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700651 if (bank->regs->irqenable_inv)
652 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100653 else
654 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530655 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100656 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700657
Victor Kamensky661553b2013-11-16 02:01:04 +0200658 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700659}
660
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200661static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700662{
663 void __iomem *reg = bank->base;
664 u32 l;
665
666 if (bank->regs->clr_irqenable) {
667 reg += bank->regs->clr_irqenable;
668 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530669 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700670 } else {
671 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200672 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700673 if (bank->regs->irqenable_inv)
674 l |= gpio_mask;
675 else
676 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530677 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700678 }
679
Victor Kamensky661553b2013-11-16 02:01:04 +0200680 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100681}
682
Grygorii Strashko9943f262015-03-23 14:18:27 +0200683static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
684 unsigned offset, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100685{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530686 if (enable)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200687 omap_enable_gpio_irqbank(bank, BIT(offset));
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530688 else
Grygorii Strashko9943f262015-03-23 14:18:27 +0200689 omap_disable_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100690}
691
Tony Lindgren92105bb2005-09-07 17:20:26 +0100692/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200693static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100694{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200695 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100696
Grygorii Strashko0c0451e2016-04-12 13:52:31 +0300697 return irq_set_irq_wake(bank->irq, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100698}
699
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800700static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100701{
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100702 struct gpio_bank *bank = gpiochip_get_data(chip);
David Brownella6472532008-03-03 04:33:30 -0800703 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100704
Grygorii Strashko46748072018-09-28 16:39:50 -0500705 pm_runtime_get_sync(chip->parent);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100706
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200707 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashkoc3518172015-05-22 17:35:51 +0300708 omap_enable_gpio_module(bank, offset);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200709 bank->mod_usage |= BIT(offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200710 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100711
712 return 0;
713}
714
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800715static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100716{
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100717 struct gpio_bank *bank = gpiochip_get_data(chip);
David Brownella6472532008-03-03 04:33:30 -0800718 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100719
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200720 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200721 bank->mod_usage &= ~(BIT(offset));
Grygorii Strashko5f982c72015-05-22 17:35:48 +0300722 if (!LINE_USED(bank->irq_usage, offset)) {
723 omap_set_gpio_direction(bank, offset, 1);
724 omap_clear_gpio_debounce(bank, offset);
725 }
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200726 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200727 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530728
Grygorii Strashko46748072018-09-28 16:39:50 -0500729 pm_runtime_put(chip->parent);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730}
731
732/*
733 * We need to unmask the GPIO bank interrupt as soon as possible to
734 * avoid missing GPIO interrupts for other lines in the bank.
735 * Then we need to mask-read-clear-unmask the triggered GPIO lines
736 * in the bank to avoid missing nested interrupts for a GPIO line.
737 * If we wait to unmask individual GPIO lines in the bank after the
738 * line's interrupt handler has been run, we may miss some nested
739 * interrupts.
740 */
Grygorii Strashko450fa542015-09-25 12:28:03 -0700741static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100742{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100743 void __iomem *isr_reg = NULL;
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500744 u32 enabled, isr, level_mask;
Jon Hunter3513cde2013-04-04 15:16:14 -0500745 unsigned int bit;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700746 struct gpio_bank *bank = gpiobank;
747 unsigned long wa_lock_flags;
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300748 unsigned long lock_flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100749
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700750 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800751 if (WARN_ON(!isr_reg))
752 goto exit;
753
Tony Lindgren52845212018-09-20 12:35:32 -0700754 if (WARN_ONCE(!pm_runtime_active(bank->chip.parent),
755 "gpio irq%i while runtime suspended?\n", irq))
756 return IRQ_NONE;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700757
Laurent Navete83507b2013-03-20 13:15:57 +0100758 while (1) {
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300759 raw_spin_lock_irqsave(&bank->lock, lock_flags);
760
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200761 enabled = omap_get_gpio_irqbank_mask(bank);
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500762 isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100763
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530764 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800765 level_mask = bank->level_mask & enabled;
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500766 else
767 level_mask = 0;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100768
769 /* clear edge sensitive interrupts before handler(s) are
770 called so that we don't miss any interrupt occurred while
771 executing them */
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500772 if (isr & ~level_mask)
773 omap_clear_gpio_irqbank(bank, isr & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100774
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300775 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
776
Tony Lindgren92105bb2005-09-07 17:20:26 +0100777 if (!isr)
778 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100779
Jon Hunter3513cde2013-04-04 15:16:14 -0500780 while (isr) {
781 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200782 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100783
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300784 raw_spin_lock_irqsave(&bank->lock, lock_flags);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800785 /*
786 * Some chips can't respond to both rising and falling
787 * at the same time. If this irq was requested with
788 * both flags, we need to flip the ICR data for the IRQ
789 * to respond to the IRQ for the opposite direction.
790 * This will be indicated in the bank toggle_mask.
791 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200792 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200793 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800794
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300795 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
796
Grygorii Strashko450fa542015-09-25 12:28:03 -0700797 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
798
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100799 generic_handle_irq(irq_find_mapping(bank->chip.irq.domain,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200800 bit));
Grygorii Strashko450fa542015-09-25 12:28:03 -0700801
802 raw_spin_unlock_irqrestore(&bank->wa_lock,
803 wa_lock_flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100804 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000805 }
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800806exit:
Grygorii Strashko450fa542015-09-25 12:28:03 -0700807 return IRQ_HANDLED;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100808}
809
Tony Lindgren3d009c82015-01-16 14:50:50 -0800810static unsigned int omap_gpio_irq_startup(struct irq_data *d)
811{
812 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800813 unsigned long flags;
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200814 unsigned offset = d->hwirq;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800815
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200816 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300817
818 if (!LINE_USED(bank->mod_usage, offset))
819 omap_set_gpio_direction(bank, offset, 1);
820 else if (!omap_gpio_is_input(bank, offset))
821 goto err;
822 omap_enable_gpio_module(bank, offset);
823 bank->irq_usage |= BIT(offset);
824
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200825 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800826 omap_gpio_unmask_irq(d);
827
828 return 0;
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300829err:
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200830 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300831 return -EINVAL;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800832}
833
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200834static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300835{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200836 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700837 unsigned long flags;
Grygorii Strashko9943f262015-03-23 14:18:27 +0200838 unsigned offset = d->hwirq;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300839
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200840 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200841 bank->irq_usage &= ~(BIT(offset));
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300842 omap_set_gpio_irqenable(bank, offset, 0);
843 omap_clear_gpio_irqstatus(bank, offset);
844 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
845 if (!LINE_USED(bank->mod_usage, offset))
846 omap_clear_gpio_debounce(bank, offset);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200847 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200848 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700849}
850
851static void omap_gpio_irq_bus_lock(struct irq_data *data)
852{
853 struct gpio_bank *bank = omap_irq_data_get_bank(data);
854
Grygorii Strashko46748072018-09-28 16:39:50 -0500855 pm_runtime_get_sync(bank->chip.parent);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700856}
857
858static void gpio_irq_bus_sync_unlock(struct irq_data *data)
859{
860 struct gpio_bank *bank = omap_irq_data_get_bank(data);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200861
Grygorii Strashko46748072018-09-28 16:39:50 -0500862 pm_runtime_put(bank->chip.parent);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300863}
864
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200865static void omap_gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100866{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200867 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200868 unsigned offset = d->hwirq;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100869
Grygorii Strashko9943f262015-03-23 14:18:27 +0200870 omap_clear_gpio_irqstatus(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100871}
872
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200873static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100874{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200875 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200876 unsigned offset = d->hwirq;
Colin Cross85ec7b92011-06-06 13:38:18 -0700877 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100878
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200879 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200880 omap_set_gpio_irqenable(bank, offset, 0);
881 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200882 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100883}
884
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200885static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100886{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200887 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200888 unsigned offset = d->hwirq;
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100889 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700890 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700891
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200892 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700893 if (trigger)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200894 omap_set_gpio_triggering(bank, offset, trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800895
896 /* For level-triggered GPIOs, the clearing must be done after
897 * the HW source is cleared, thus after the handler has run */
Grygorii Strashko9943f262015-03-23 14:18:27 +0200898 if (bank->level_mask & BIT(offset)) {
899 omap_set_gpio_irqenable(bank, offset, 0);
900 omap_clear_gpio_irqstatus(bank, offset);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800901 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100902
Grygorii Strashko9943f262015-03-23 14:18:27 +0200903 omap_set_gpio_irqenable(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200904 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100905}
906
Tony Lindgrenec0daae2018-09-20 12:35:30 -0700907/*
908 * Only edges can generate a wakeup event to the PRCM.
909 *
910 * Therefore, ensure any wake-up capable GPIOs have
911 * edge-detection enabled before going idle to ensure a wakeup
912 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
913 * NDA TRM 25.5.3.1)
914 *
915 * The normal values will be restored upon ->runtime_resume()
916 * by writing back the values saved in bank->context.
917 */
918static void __maybe_unused
919omap2_gpio_enable_level_quirk(struct gpio_bank *bank)
920{
921 u32 wake_low, wake_hi;
922
923 /* Enable additional edge detection for level gpios for idle */
924 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
925 if (wake_low)
926 writel_relaxed(wake_low | bank->context.fallingdetect,
927 bank->base + bank->regs->fallingdetect);
928
929 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
930 if (wake_hi)
931 writel_relaxed(wake_hi | bank->context.risingdetect,
932 bank->base + bank->regs->risingdetect);
933}
934
935static void __maybe_unused
936omap2_gpio_disable_level_quirk(struct gpio_bank *bank)
937{
938 /* Disable edge detection for level gpios after idle */
939 writel_relaxed(bank->context.fallingdetect,
940 bank->base + bank->regs->fallingdetect);
941 writel_relaxed(bank->context.risingdetect,
942 bank->base + bank->regs->risingdetect);
943}
944
945/*
946 * On omap4 and later SoC variants a level interrupt with wkup_en
947 * enabled blocks the GPIO functional clock from idling until the GPIO
948 * instance has been reset. To avoid that, we must set wkup_en only for
949 * idle for level interrupts, and clear level registers for the duration
950 * of idle. The level interrupts will be still there on wakeup by their
951 * nature.
952 */
953static void __maybe_unused
954omap4_gpio_enable_level_quirk(struct gpio_bank *bank)
955{
956 /* Update wake register for idle, edge bits might be already set */
957 writel_relaxed(bank->context.wake_en,
958 bank->base + bank->regs->wkup_en);
959
960 /* Clear level registers for idle */
961 writel_relaxed(0, bank->base + bank->regs->leveldetect0);
962 writel_relaxed(0, bank->base + bank->regs->leveldetect1);
963}
964
965static void __maybe_unused
966omap4_gpio_disable_level_quirk(struct gpio_bank *bank)
967{
968 /* Restore level registers after idle */
969 writel_relaxed(bank->context.leveldetect0,
970 bank->base + bank->regs->leveldetect0);
971 writel_relaxed(bank->context.leveldetect1,
972 bank->base + bank->regs->leveldetect1);
973
974 /* Clear saved wkup_en for level, it will be set for next idle again */
975 bank->context.wake_en &= ~(bank->context.leveldetect0 |
976 bank->context.leveldetect1);
977
978 /* Update wake with only edge configuration */
979 writel_relaxed(bank->context.wake_en,
980 bank->base + bank->regs->wkup_en);
981}
982
David Brownelle5c56ed2006-12-06 17:13:59 -0800983/*---------------------------------------------------------------------*/
984
Magnus Damm79ee0312009-07-08 13:22:04 +0200985static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800986{
Wolfram Sanga3f4f722018-10-21 21:59:59 +0200987 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800988 void __iomem *mask_reg = bank->base +
989 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800990 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800991
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200992 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200993 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200994 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800995
996 return 0;
997}
998
Magnus Damm79ee0312009-07-08 13:22:04 +0200999static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001000{
Wolfram Sanga3f4f722018-10-21 21:59:59 +02001001 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001002 void __iomem *mask_reg = bank->base +
1003 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001004 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001005
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001006 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +02001007 writel_relaxed(bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001008 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001009
1010 return 0;
1011}
1012
Alexey Dobriyan47145212009-12-14 18:00:08 -08001013static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +02001014 .suspend_noirq = omap_mpuio_suspend_noirq,
1015 .resume_noirq = omap_mpuio_resume_noirq,
1016};
1017
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +02001018/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -08001019static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -08001020 .driver = {
1021 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +02001022 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -08001023 },
1024};
1025
1026static struct platform_device omap_mpuio_device = {
1027 .name = "mpuio",
1028 .id = -1,
1029 .dev = {
1030 .driver = &omap_mpuio_driver.driver,
1031 }
1032 /* could list the /proc/iomem resources */
1033};
1034
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001035static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -08001036{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001037 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -07001038
David Brownell11a78b72006-12-06 17:14:11 -08001039 if (platform_driver_register(&omap_mpuio_driver) == 0)
1040 (void) platform_device_register(&omap_mpuio_device);
1041}
1042
David Brownelle5c56ed2006-12-06 17:13:59 -08001043/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001044
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001045static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +02001046{
1047 struct gpio_bank *bank;
1048 unsigned long flags;
1049 void __iomem *reg;
1050 int dir;
1051
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001052 bank = gpiochip_get_data(chip);
Yegor Yefremov93700842014-04-24 08:57:39 +02001053 reg = bank->base + bank->regs->direction;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001054 raw_spin_lock_irqsave(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +02001055 dir = !!(readl_relaxed(reg) & BIT(offset));
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001056 raw_spin_unlock_irqrestore(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +02001057 return dir;
1058}
1059
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001060static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -08001061{
1062 struct gpio_bank *bank;
1063 unsigned long flags;
1064
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001065 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001066 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001067 omap_set_gpio_direction(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001068 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -08001069 return 0;
1070}
1071
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001072static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -08001073{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001074 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001075
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001076 bank = gpiochip_get_data(chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001077
Grygorii Strashkob2b20042015-03-23 14:18:23 +02001078 if (omap_gpio_is_input(bank, offset))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001079 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001080 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001081 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -08001082}
1083
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001084static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001085{
1086 struct gpio_bank *bank;
1087 unsigned long flags;
1088
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001089 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001090 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001091 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001092 omap_set_gpio_direction(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001093 raw_spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +02001094 return 0;
David Brownell52e31342008-03-03 12:43:23 -08001095}
1096
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001097static int omap_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
1098 unsigned long *bits)
1099{
1100 struct gpio_bank *bank = gpiochip_get_data(chip);
1101 void __iomem *reg = bank->base + bank->regs->direction;
1102 unsigned long in = readl_relaxed(reg), l;
1103
1104 *bits = 0;
1105
1106 l = in & *mask;
1107 if (l)
1108 *bits |= omap_get_gpio_datain_multiple(bank, &l);
1109
1110 l = ~in & *mask;
1111 if (l)
1112 *bits |= omap_get_gpio_dataout_multiple(bank, &l);
1113
1114 return 0;
1115}
1116
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001117static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
1118 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001119{
1120 struct gpio_bank *bank;
1121 unsigned long flags;
David Rivshin83977442017-04-24 18:56:50 -04001122 int ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001123
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001124 bank = gpiochip_get_data(chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001125
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001126 raw_spin_lock_irqsave(&bank->lock, flags);
David Rivshin83977442017-04-24 18:56:50 -04001127 ret = omap2_set_gpio_debounce(bank, offset, debounce);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001128 raw_spin_unlock_irqrestore(&bank->lock, flags);
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001129
David Rivshin83977442017-04-24 18:56:50 -04001130 if (ret)
1131 dev_info(chip->parent,
1132 "Could not set line %u debounce to %u microseconds (%d)",
1133 offset, debounce, ret);
1134
1135 return ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001136}
1137
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001138static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset,
1139 unsigned long config)
1140{
1141 u32 debounce;
1142
1143 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
1144 return -ENOTSUPP;
1145
1146 debounce = pinconf_to_config_argument(config);
1147 return omap_gpio_debounce(chip, offset, debounce);
1148}
1149
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001150static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001151{
1152 struct gpio_bank *bank;
1153 unsigned long flags;
1154
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001155 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001156 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001157 bank->set_dataout(bank, offset, value);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001158 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -08001159}
1160
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001161static void omap_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
1162 unsigned long *bits)
1163{
1164 struct gpio_bank *bank = gpiochip_get_data(chip);
1165 unsigned long flags;
1166
1167 raw_spin_lock_irqsave(&bank->lock, flags);
1168 bank->set_dataout_multiple(bank, mask, bits);
1169 raw_spin_unlock_irqrestore(&bank->lock, flags);
1170}
1171
David Brownell52e31342008-03-03 12:43:23 -08001172/*---------------------------------------------------------------------*/
1173
Arnd Bergmanne4b2ae72017-09-16 22:42:21 +02001174static void omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001175{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001176 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001177 u32 rev;
1178
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001179 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001180 return;
1181
Victor Kamensky661553b2013-11-16 02:01:04 +02001182 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001183 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001184 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001185
1186 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001187}
1188
Charulatha V03e128c2011-05-05 19:58:01 +05301189static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001190{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301191 void __iomem *base = bank->base;
1192 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001193
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301194 if (bank->width == 16)
1195 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001196
Charulatha Vd0d665a2011-08-31 00:02:21 +05301197 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001198 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301199 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001200 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301201
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001202 omap_gpio_rmw(base, bank->regs->irqenable, l,
1203 bank->regs->irqenable_inv);
1204 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1205 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301206 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001207 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301208
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301209 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001210 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301211 /* Initialize interface clk ungated, module enabled */
1212 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001213 writel_relaxed(0, base + bank->regs->ctrl);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001214}
1215
Nishanth Menon46824e222014-09-05 14:52:55 -05001216static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001217{
Grygorii Strashko81930322017-11-15 12:36:33 -06001218 struct gpio_irq_chip *irq;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001219 static int gpio;
Linus Walleij088413b2017-12-29 13:22:58 +01001220 const char *label;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001221 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001222 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001223
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001224 /*
1225 * REVISIT eventually switch from OMAP-specific gpio structs
1226 * over to the generic ones
1227 */
1228 bank->chip.request = omap_gpio_request;
1229 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001230 bank->chip.get_direction = omap_gpio_get_direction;
1231 bank->chip.direction_input = omap_gpio_input;
1232 bank->chip.get = omap_gpio_get;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001233 bank->chip.get_multiple = omap_gpio_get_multiple;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001234 bank->chip.direction_output = omap_gpio_output;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001235 bank->chip.set_config = omap_gpio_set_config;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001236 bank->chip.set = omap_gpio_set;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001237 bank->chip.set_multiple = omap_gpio_set_multiple;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301238 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001239 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301240 if (bank->regs->wkup_en)
Linus Walleij58383c782015-11-04 09:56:26 +01001241 bank->chip.parent = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001242 bank->chip.base = OMAP_MPUIO(0);
1243 } else {
Linus Walleij088413b2017-12-29 13:22:58 +01001244 label = devm_kasprintf(bank->chip.parent, GFP_KERNEL, "gpio-%d-%d",
1245 gpio, gpio + bank->width - 1);
1246 if (!label)
1247 return -ENOMEM;
1248 bank->chip.label = label;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001249 bank->chip.base = gpio;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001250 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001251 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001252
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001253#ifdef CONFIG_ARCH_OMAP1
1254 /*
1255 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1256 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1257 */
Bartosz Golaszewski2ed36f32017-03-04 17:23:31 +01001258 irq_base = devm_irq_alloc_descs(bank->chip.parent,
1259 -1, 0, bank->width, 0);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001260 if (irq_base < 0) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001261 dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n");
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001262 return -ENODEV;
1263 }
1264#endif
1265
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001266 /* MPUIO is a bit different, reading IRQ status clears it */
1267 if (bank->is_mpuio) {
1268 irqc->irq_ack = dummy_irq_chip.irq_ack;
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001269 if (!bank->regs->wkup_en)
1270 irqc->irq_set_wake = NULL;
1271 }
1272
Grygorii Strashko81930322017-11-15 12:36:33 -06001273 irq = &bank->chip.irq;
1274 irq->chip = irqc;
1275 irq->handler = handle_bad_irq;
1276 irq->default_type = IRQ_TYPE_NONE;
1277 irq->num_parents = 1;
1278 irq->parents = &bank->irq;
1279 irq->first = irq_base;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001280
Grygorii Strashko81930322017-11-15 12:36:33 -06001281 ret = gpiochip_add_data(&bank->chip, bank);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001282 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001283 dev_err(bank->chip.parent,
Grygorii Strashko81930322017-11-15 12:36:33 -06001284 "Could not register gpio chip %d\n", ret);
1285 return ret;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001286 }
1287
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001288 ret = devm_request_irq(bank->chip.parent, bank->irq,
1289 omap_gpio_irq_handler,
1290 0, dev_name(bank->chip.parent), bank);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001291 if (ret)
1292 gpiochip_remove(&bank->chip);
1293
Grygorii Strashko81930322017-11-15 12:36:33 -06001294 if (!bank->is_mpuio)
1295 gpio += bank->width;
1296
Grygorii Strashko450fa542015-09-25 12:28:03 -07001297 return ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001298}
1299
Tony Lindgrenb764a582018-09-20 12:35:31 -07001300static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context);
1301static void omap_gpio_unidle(struct gpio_bank *bank);
1302
1303static int gpio_omap_cpu_notifier(struct notifier_block *nb,
1304 unsigned long cmd, void *v)
1305{
1306 struct gpio_bank *bank;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001307 unsigned long flags;
1308
1309 bank = container_of(nb, struct gpio_bank, nb);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001310
1311 raw_spin_lock_irqsave(&bank->lock, flags);
1312 switch (cmd) {
1313 case CPU_CLUSTER_PM_ENTER:
1314 if (bank->is_suspended)
1315 break;
1316 omap_gpio_idle(bank, true);
1317 break;
1318 case CPU_CLUSTER_PM_ENTER_FAILED:
1319 case CPU_CLUSTER_PM_EXIT:
1320 if (bank->is_suspended)
1321 break;
1322 omap_gpio_unidle(bank);
1323 break;
1324 }
1325 raw_spin_unlock_irqrestore(&bank->lock, flags);
1326
1327 return NOTIFY_OK;
1328}
1329
Benoit Cousson384ebe12011-08-16 11:53:02 +02001330static const struct of_device_id omap_gpio_match[];
1331
Bill Pemberton38363092012-11-19 13:22:34 -05001332static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001333{
Benoit Cousson862ff642012-02-01 15:58:56 +01001334 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001335 struct device_node *node = dev->of_node;
1336 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001337 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001338 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001339 struct gpio_bank *bank;
Nishanth Menon46824e222014-09-05 14:52:55 -05001340 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001341 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001342
Benoit Cousson384ebe12011-08-16 11:53:02 +02001343 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1344
Jingoo Hane56aee12013-07-30 17:08:05 +09001345 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001346 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001347 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001348
Markus Elfringf97364c2018-02-10 21:49:22 +01001349 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
Markus Elfring9117d402018-02-10 21:46:30 +01001350 if (!bank)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001351 return -ENOMEM;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001352
Nishanth Menon46824e222014-09-05 14:52:55 -05001353 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1354 if (!irqc)
1355 return -ENOMEM;
1356
Tony Lindgren3d009c82015-01-16 14:50:50 -08001357 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e222014-09-05 14:52:55 -05001358 irqc->irq_shutdown = omap_gpio_irq_shutdown,
1359 irqc->irq_ack = omap_gpio_ack_irq,
1360 irqc->irq_mask = omap_gpio_mask_irq,
1361 irqc->irq_unmask = omap_gpio_unmask_irq,
1362 irqc->irq_set_type = omap_gpio_irq_type,
1363 irqc->irq_set_wake = omap_gpio_wake_enable,
Grygorii Strashkoaca82d12015-09-25 12:28:02 -07001364 irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
1365 irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
Nishanth Menon46824e222014-09-05 14:52:55 -05001366 irqc->name = dev_name(&pdev->dev);
Grygorii Strashko0c0451e2016-04-12 13:52:31 +03001367 irqc->flags = IRQCHIP_MASK_ON_SUSPEND;
Grygorii Strashko46748072018-09-28 16:39:50 -05001368 irqc->parent_device = dev;
Nishanth Menon46824e222014-09-05 14:52:55 -05001369
Grygorii Strashko89d18e32015-08-18 14:10:53 +03001370 bank->irq = platform_get_irq(pdev, 0);
1371 if (bank->irq <= 0) {
1372 if (!bank->irq)
1373 bank->irq = -ENXIO;
1374 if (bank->irq != -EPROBE_DEFER)
1375 dev_err(dev,
1376 "can't get irq resource ret=%d\n", bank->irq);
1377 return bank->irq;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001378 }
1379
Linus Walleij58383c782015-11-04 09:56:26 +01001380 bank->chip.parent = dev;
Grygorii Strashkoc23837c2015-06-25 18:13:33 +03001381 bank->chip.owner = THIS_MODULE;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001382 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001383 bank->quirks = pdata->quirks;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001384 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001385 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301386 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301387 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001388 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001389#ifdef CONFIG_OF_GPIO
1390 bank->chip.of_node = of_node_get(node);
1391#endif
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001392
Jon Huntera2797be2013-04-04 15:16:15 -05001393 if (node) {
1394 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1395 bank->loses_context = true;
1396 } else {
1397 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001398
1399 if (bank->loses_context)
1400 bank->get_context_loss_count =
1401 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001402 }
1403
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001404 if (bank->regs->set_dataout && bank->regs->clr_dataout) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001405 bank->set_dataout = omap_set_gpio_dataout_reg;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001406 bank->set_dataout_multiple = omap_set_gpio_dataout_reg_multiple;
1407 } else {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001408 bank->set_dataout = omap_set_gpio_dataout_mask;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001409 bank->set_dataout_multiple =
1410 omap_set_gpio_dataout_mask_multiple;
1411 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001412
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001413 if (bank->quirks & OMAP_GPIO_QUIRK_DEFERRED_WKUP_EN) {
1414 bank->funcs.idle_enable_level_quirk =
1415 omap4_gpio_enable_level_quirk;
1416 bank->funcs.idle_disable_level_quirk =
1417 omap4_gpio_disable_level_quirk;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001418 } else if (bank->quirks & OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER) {
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001419 bank->funcs.idle_enable_level_quirk =
1420 omap2_gpio_enable_level_quirk;
1421 bank->funcs.idle_disable_level_quirk =
1422 omap2_gpio_disable_level_quirk;
1423 }
1424
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001425 raw_spin_lock_init(&bank->lock);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001426 raw_spin_lock_init(&bank->wa_lock);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001427
1428 /* Static mapping, never released */
1429 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001430 bank->base = devm_ioremap_resource(dev, res);
1431 if (IS_ERR(bank->base)) {
Jingoo Han717f70e2014-02-12 11:51:38 +09001432 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001433 }
1434
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001435 if (bank->dbck_flag) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001436 bank->dbck = devm_clk_get(dev, "dbclk");
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001437 if (IS_ERR(bank->dbck)) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001438 dev_err(dev,
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001439 "Could not get gpio dbck. Disable debounce\n");
1440 bank->dbck_flag = false;
1441 } else {
1442 clk_prepare(bank->dbck);
1443 }
1444 }
1445
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301446 platform_set_drvdata(pdev, bank);
1447
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001448 pm_runtime_enable(dev);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001449 pm_runtime_get_sync(dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001450
Charulatha Vd0d665a2011-08-31 00:02:21 +05301451 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001452 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301453
Charulatha V03e128c2011-05-05 19:58:01 +05301454 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001455
Nishanth Menon46824e222014-09-05 14:52:55 -05001456 ret = omap_gpio_chip_init(bank, irqc);
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001457 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001458 pm_runtime_put_sync(dev);
1459 pm_runtime_disable(dev);
Arvind Yadave2c3c192017-08-01 12:14:31 +05301460 if (bank->dbck_flag)
1461 clk_unprepare(bank->dbck);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001462 return ret;
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001463 }
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001464
Tony Lindgren9a748052010-12-07 16:26:56 -08001465 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001466
Tony Lindgrenb764a582018-09-20 12:35:31 -07001467 if (bank->funcs.idle_enable_level_quirk &&
1468 bank->funcs.idle_disable_level_quirk) {
1469 bank->nb.notifier_call = gpio_omap_cpu_notifier;
1470 cpu_pm_register_notifier(&bank->nb);
1471 }
1472
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001473 pm_runtime_put(dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301474
Jon Hunter879fe322013-04-04 15:16:12 -05001475 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001476}
1477
Tony Lindgrencac089f2015-04-23 16:56:22 -07001478static int omap_gpio_remove(struct platform_device *pdev)
1479{
1480 struct gpio_bank *bank = platform_get_drvdata(pdev);
1481
Tony Lindgrenb764a582018-09-20 12:35:31 -07001482 if (bank->nb.notifier_call)
1483 cpu_pm_unregister_notifier(&bank->nb);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001484 list_del(&bank->node);
1485 gpiochip_remove(&bank->chip);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001486 pm_runtime_disable(&pdev->dev);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001487 if (bank->dbck_flag)
1488 clk_unprepare(bank->dbck);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001489
1490 return 0;
1491}
1492
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301493static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001494
Tony Lindgrenb764a582018-09-20 12:35:31 -07001495static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301496{
Tony Lindgrenb764a582018-09-20 12:35:31 -07001497 struct device *dev = bank->chip.parent;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301498 u32 l1 = 0, l2 = 0;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001499
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001500 if (bank->funcs.idle_enable_level_quirk)
1501 bank->funcs.idle_enable_level_quirk(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001502
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001503 if (!bank->enabled_non_wakeup_gpios)
1504 goto update_gpio_context_count;
1505
Tony Lindgrenb764a582018-09-20 12:35:31 -07001506 if (!may_lose_context)
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301507 goto update_gpio_context_count;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001508
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301509 /*
1510 * If going to OFF, remove triggering for all
1511 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1512 * generated. See OMAP2420 Errata item 1.101.
1513 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001514 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301515 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301516 l1 = bank->context.fallingdetect;
1517 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301518
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301519 l1 &= ~bank->enabled_non_wakeup_gpios;
1520 l2 &= ~bank->enabled_non_wakeup_gpios;
1521
Victor Kamensky661553b2013-11-16 02:01:04 +02001522 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1523 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301524
1525 bank->workaround_enabled = true;
1526
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301527update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301528 if (bank->get_context_loss_count)
1529 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001530 bank->get_context_loss_count(dev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301531
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001532 omap_gpio_dbck_disable(bank);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301533}
1534
Jon Hunter352a2d52013-04-15 13:06:54 -05001535static void omap_gpio_init_context(struct gpio_bank *p);
1536
Tony Lindgrenb764a582018-09-20 12:35:31 -07001537static void omap_gpio_unidle(struct gpio_bank *bank)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301538{
Tony Lindgrenb764a582018-09-20 12:35:31 -07001539 struct device *dev = bank->chip.parent;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301540 u32 l = 0, gen, gen0, gen1;
Jon Huntera2797be2013-04-04 15:16:15 -05001541 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301542
Jon Hunter352a2d52013-04-15 13:06:54 -05001543 /*
1544 * On the first resume during the probe, the context has not
1545 * been initialised and so initialise it now. Also initialise
1546 * the context loss count.
1547 */
1548 if (bank->loses_context && !bank->context_valid) {
1549 omap_gpio_init_context(bank);
1550
1551 if (bank->get_context_loss_count)
1552 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001553 bank->get_context_loss_count(dev);
Jon Hunter352a2d52013-04-15 13:06:54 -05001554 }
1555
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001556 omap_gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001557
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001558 if (bank->funcs.idle_disable_level_quirk)
1559 bank->funcs.idle_disable_level_quirk(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001560
Jon Huntera2797be2013-04-04 15:16:15 -05001561 if (bank->loses_context) {
1562 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301563 omap_gpio_restore_context(bank);
1564 } else {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001565 c = bank->get_context_loss_count(dev);
Jon Huntera2797be2013-04-04 15:16:15 -05001566 if (c != bank->context_loss_count) {
1567 omap_gpio_restore_context(bank);
1568 } else {
Tony Lindgrenb764a582018-09-20 12:35:31 -07001569 return;
Jon Huntera2797be2013-04-04 15:16:15 -05001570 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301571 }
1572 }
1573
Tony Lindgrenb764a582018-09-20 12:35:31 -07001574 if (!bank->workaround_enabled)
1575 return;
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301576
Victor Kamensky661553b2013-11-16 02:01:04 +02001577 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301578
1579 /*
1580 * Check if any of the non-wakeup interrupt GPIOs have changed
1581 * state. If so, generate an IRQ by software. This is
1582 * horribly racy, but it's the best we can do to work around
1583 * this silicon bug.
1584 */
1585 l ^= bank->saved_datain;
1586 l &= bank->enabled_non_wakeup_gpios;
1587
1588 /*
1589 * No need to generate IRQs for the rising edge for gpio IRQs
1590 * configured with falling edge only; and vice versa.
1591 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301592 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301593 gen0 &= bank->saved_datain;
1594
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301595 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301596 gen1 &= ~(bank->saved_datain);
1597
1598 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301599 gen = l & (~(bank->context.fallingdetect) &
1600 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301601 /* Consider all GPIO IRQs needed to be updated */
1602 gen |= gen0 | gen1;
1603
1604 if (gen) {
1605 u32 old0, old1;
1606
Victor Kamensky661553b2013-11-16 02:01:04 +02001607 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1608 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301609
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301610 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001611 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301612 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001613 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301614 bank->regs->leveldetect1);
1615 }
1616
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301617 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001618 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301619 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001620 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301621 bank->regs->leveldetect1);
1622 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001623 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1624 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301625 }
1626
1627 bank->workaround_enabled = false;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001628}
1629
Jon Hunter352a2d52013-04-15 13:06:54 -05001630static void omap_gpio_init_context(struct gpio_bank *p)
1631{
1632 struct omap_gpio_reg_offs *regs = p->regs;
1633 void __iomem *base = p->base;
1634
Victor Kamensky661553b2013-11-16 02:01:04 +02001635 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1636 p->context.oe = readl_relaxed(base + regs->direction);
1637 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1638 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1639 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1640 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1641 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1642 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1643 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001644
1645 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001646 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001647 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001648 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001649
1650 p->context_valid = true;
1651}
1652
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301653static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301654{
Victor Kamensky661553b2013-11-16 02:01:04 +02001655 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301656 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001657 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1658 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301659 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001660 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301661 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001662 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301663 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001664 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301665 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301666 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001667 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301668 bank->base + bank->regs->set_dataout);
1669 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001670 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301671 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001672 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301673
Nishanth Menonae547352011-09-09 19:08:58 +05301674 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001675 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301676 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001677 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301678 bank->base + bank->regs->debounce_en);
1679 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301680
Victor Kamensky661553b2013-11-16 02:01:04 +02001681 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301682 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001683 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301684 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301685}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301686
Tony Lindgrenb764a582018-09-20 12:35:31 -07001687static int __maybe_unused omap_gpio_runtime_suspend(struct device *dev)
1688{
Wolfram Sanga3f4f722018-10-21 21:59:59 +02001689 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001690 unsigned long flags;
1691 int error = 0;
1692
1693 raw_spin_lock_irqsave(&bank->lock, flags);
1694 /* Must be idled only by CPU_CLUSTER_PM_ENTER? */
1695 if (bank->irq_usage) {
1696 error = -EBUSY;
1697 goto unlock;
1698 }
1699 omap_gpio_idle(bank, true);
1700 bank->is_suspended = true;
1701unlock:
1702 raw_spin_unlock_irqrestore(&bank->lock, flags);
1703
1704 return error;
1705}
1706
1707static int __maybe_unused omap_gpio_runtime_resume(struct device *dev)
1708{
Wolfram Sanga3f4f722018-10-21 21:59:59 +02001709 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001710 unsigned long flags;
1711 int error = 0;
1712
1713 raw_spin_lock_irqsave(&bank->lock, flags);
1714 /* Must be unidled only by CPU_CLUSTER_PM_ENTER? */
1715 if (bank->irq_usage) {
1716 error = -EBUSY;
1717 goto unlock;
1718 }
1719 omap_gpio_unidle(bank);
1720 bank->is_suspended = false;
1721unlock:
1722 raw_spin_unlock_irqrestore(&bank->lock, flags);
1723
1724 return error;
1725}
1726
1727#ifdef CONFIG_ARCH_OMAP2PLUS
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301728static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301729 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1730 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301731};
Tony Lindgrenb764a582018-09-20 12:35:31 -07001732#else
1733static const struct dev_pm_ops gpio_pm_ops;
1734#endif /* CONFIG_ARCH_OMAP2PLUS */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301735
Benoit Cousson384ebe12011-08-16 11:53:02 +02001736#if defined(CONFIG_OF)
1737static struct omap_gpio_reg_offs omap2_gpio_regs = {
1738 .revision = OMAP24XX_GPIO_REVISION,
1739 .direction = OMAP24XX_GPIO_OE,
1740 .datain = OMAP24XX_GPIO_DATAIN,
1741 .dataout = OMAP24XX_GPIO_DATAOUT,
1742 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1743 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1744 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1745 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1746 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1747 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1748 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1749 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1750 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1751 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1752 .ctrl = OMAP24XX_GPIO_CTRL,
1753 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1754 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1755 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1756 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1757 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1758};
1759
1760static struct omap_gpio_reg_offs omap4_gpio_regs = {
1761 .revision = OMAP4_GPIO_REVISION,
1762 .direction = OMAP4_GPIO_OE,
1763 .datain = OMAP4_GPIO_DATAIN,
1764 .dataout = OMAP4_GPIO_DATAOUT,
1765 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1766 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1767 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1768 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1769 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1770 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1771 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1772 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1773 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1774 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1775 .ctrl = OMAP4_GPIO_CTRL,
1776 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1777 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1778 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1779 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1780 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1781};
1782
Tony Lindgrenb764a582018-09-20 12:35:31 -07001783/*
1784 * Note that omap2 does not currently support idle modes with context loss so
1785 * no need to add OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER quirk flag to save
1786 * and restore context.
1787 */
Chen Gange9a65bb2013-02-06 18:44:32 +08001788static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001789 .regs = &omap2_gpio_regs,
1790 .bank_width = 32,
1791 .dbck_flag = false,
1792};
1793
Chen Gange9a65bb2013-02-06 18:44:32 +08001794static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001795 .regs = &omap2_gpio_regs,
1796 .bank_width = 32,
1797 .dbck_flag = true,
Tony Lindgrenb764a582018-09-20 12:35:31 -07001798 .quirks = OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001799};
1800
Chen Gange9a65bb2013-02-06 18:44:32 +08001801static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001802 .regs = &omap4_gpio_regs,
1803 .bank_width = 32,
1804 .dbck_flag = true,
Tony Lindgrenb764a582018-09-20 12:35:31 -07001805 .quirks = OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER |
1806 OMAP_GPIO_QUIRK_DEFERRED_WKUP_EN,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001807};
1808
1809static const struct of_device_id omap_gpio_match[] = {
1810 {
1811 .compatible = "ti,omap4-gpio",
1812 .data = &omap4_pdata,
1813 },
1814 {
1815 .compatible = "ti,omap3-gpio",
1816 .data = &omap3_pdata,
1817 },
1818 {
1819 .compatible = "ti,omap2-gpio",
1820 .data = &omap2_pdata,
1821 },
1822 { },
1823};
1824MODULE_DEVICE_TABLE(of, omap_gpio_match);
1825#endif
1826
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001827static struct platform_driver omap_gpio_driver = {
1828 .probe = omap_gpio_probe,
Tony Lindgrencac089f2015-04-23 16:56:22 -07001829 .remove = omap_gpio_remove,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001830 .driver = {
1831 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301832 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001833 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001834 },
1835};
1836
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001837/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001838 * gpio driver register needs to be done before
1839 * machine_init functions access gpio APIs.
1840 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001841 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001842static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001843{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001844 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001845}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001846postcore_initcall(omap_gpio_drv_reg);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001847
1848static void __exit omap_gpio_exit(void)
1849{
1850 platform_driver_unregister(&omap_gpio_driver);
1851}
1852module_exit(omap_gpio_exit);
1853
1854MODULE_DESCRIPTION("omap gpio driver");
1855MODULE_ALIAS("platform:gpio-omap");
1856MODULE_LICENSE("GPL v2");