blob: e81008678a38f5c36987b37a83bac80ccaccae8a [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Benoit Cousson96751fc2012-02-01 16:01:39 +010022#include <linux/device.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080023#include <linux/pm_runtime.h>
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +053024#include <linux/pm.h>
Benoit Cousson384ebe12011-08-16 11:53:02 +020025#include <linux/of.h>
26#include <linux/of_device.h>
Linus Walleijb7351b02018-05-24 14:24:00 +020027#include <linux/gpio/driver.h>
Yegor Yefremov93700842014-04-24 08:57:39 +020028#include <linux/bitops.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070029#include <linux/platform_data/gpio-omap.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010030
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053031#define OFF_MODE 1
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +030032#define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053033
Charulatha V03e128c2011-05-05 19:58:01 +053034static LIST_HEAD(omap_gpio_list);
35
Charulatha V6d62e212011-04-18 15:06:51 +000036struct gpio_regs {
37 u32 irqenable1;
38 u32 irqenable2;
39 u32 wake_en;
40 u32 ctrl;
41 u32 oe;
42 u32 leveldetect0;
43 u32 leveldetect1;
44 u32 risingdetect;
45 u32 fallingdetect;
46 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053047 u32 debounce;
48 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000049};
50
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010051struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053052 struct list_head node;
Tony Lindgren92105bb2005-09-07 17:20:26 +010053 void __iomem *base;
Grygorii Strashko30cefea2015-09-25 12:06:02 -070054 int irq;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080055 u32 non_wakeup_gpios;
56 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000057 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080058 u32 saved_datain;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080059 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080060 u32 toggle_mask;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +020061 raw_spinlock_t lock;
Grygorii Strashko450fa542015-09-25 12:28:03 -070062 raw_spinlock_t wa_lock;
David Brownell52e31342008-03-03 12:43:23 -080063 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080064 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080065 u32 mod_usage;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020066 u32 irq_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080067 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +053068 bool dbck_enabled;
Charulatha Vd0d665a2011-08-31 00:02:21 +053069 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080070 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053071 bool loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -050072 bool context_valid;
Tony Lindgren5de62b82010-12-07 16:26:58 -080073 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070074 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053075 int context_loss_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053076 int power_mode;
77 bool workaround_enabled;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070078
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +020079 void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable);
Janusz Krzysztofik442af142018-07-19 01:57:08 +020080 void (*set_dataout_multiple)(struct gpio_bank *bank,
81 unsigned long *mask, unsigned long *bits);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053082 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070083
84 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085};
86
Charulatha Vc8eef652011-05-02 15:21:42 +053087#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010088
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020089#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020090#define LINE_USED(line, offset) (line & (BIT(offset)))
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020091
Tony Lindgren3d009c82015-01-16 14:50:50 -080092static void omap_gpio_unmask_irq(struct irq_data *d);
93
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020094static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -060095{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +020096 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
Linus Walleijd99f7ae2015-12-07 11:16:00 +010097 return gpiochip_get_data(chip);
Benoit Cousson25db7112012-02-23 21:50:10 +010098}
99
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200100static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
101 int is_input)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100102{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100103 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100104 u32 l;
105
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700106 reg += bank->regs->direction;
Victor Kamensky661553b2013-11-16 02:01:04 +0200107 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100108 if (is_input)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200109 l |= BIT(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100110 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200111 l &= ~(BIT(gpio));
Victor Kamensky661553b2013-11-16 02:01:04 +0200112 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530113 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100114}
115
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700116
117/* set data out value using dedicate set/clear register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200118static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200119 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100120{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100121 void __iomem *reg = bank->base;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200122 u32 l = BIT(offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100123
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530124 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700125 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530126 bank->context.dataout |= l;
127 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700128 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530129 bank->context.dataout &= ~l;
130 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700131
Victor Kamensky661553b2013-11-16 02:01:04 +0200132 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700133}
134
135/* set data out value using mask register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200136static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200137 int enable)
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700138{
139 void __iomem *reg = bank->base + bank->regs->dataout;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200140 u32 gpio_bit = BIT(offset);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700141 u32 l;
142
Victor Kamensky661553b2013-11-16 02:01:04 +0200143 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700144 if (enable)
145 l |= gpio_bit;
146 else
147 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200148 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530149 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150}
151
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200152static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700154 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100155
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200156 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100157}
158
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200159static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300160{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700161 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300162
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200163 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300164}
165
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200166/* set multiple data out values using dedicate set/clear register */
167static void omap_set_gpio_dataout_reg_multiple(struct gpio_bank *bank,
168 unsigned long *mask,
169 unsigned long *bits)
170{
171 void __iomem *reg = bank->base;
172 u32 l;
173
174 l = *bits & *mask;
175 writel_relaxed(l, reg + bank->regs->set_dataout);
176 bank->context.dataout |= l;
177
178 l = ~*bits & *mask;
179 writel_relaxed(l, reg + bank->regs->clr_dataout);
180 bank->context.dataout &= ~l;
181}
182
183/* set multiple data out values using mask register */
184static void omap_set_gpio_dataout_mask_multiple(struct gpio_bank *bank,
185 unsigned long *mask,
186 unsigned long *bits)
187{
188 void __iomem *reg = bank->base + bank->regs->dataout;
189 u32 l = (readl_relaxed(reg) & ~*mask) | (*bits & *mask);
190
191 writel_relaxed(l, reg);
192 bank->context.dataout = l;
193}
194
195static unsigned long omap_get_gpio_datain_multiple(struct gpio_bank *bank,
196 unsigned long *mask)
197{
198 void __iomem *reg = bank->base + bank->regs->datain;
199
200 return readl_relaxed(reg) & *mask;
201}
202
203static unsigned long omap_get_gpio_dataout_multiple(struct gpio_bank *bank,
204 unsigned long *mask)
205{
206 void __iomem *reg = bank->base + bank->regs->dataout;
207
208 return readl_relaxed(reg) & *mask;
209}
210
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200211static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700212{
Victor Kamensky661553b2013-11-16 02:01:04 +0200213 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700214
Benoit Cousson862ff642012-02-01 15:58:56 +0100215 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700216 l |= mask;
217 else
218 l &= ~mask;
219
Victor Kamensky661553b2013-11-16 02:01:04 +0200220 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700221}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100222
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200223static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530224{
225 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300226 clk_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530227 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300228
Victor Kamensky661553b2013-11-16 02:01:04 +0200229 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300230 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530231 }
232}
233
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200234static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530235{
236 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300237 /*
238 * Disable debounce before cutting it's clock. If debounce is
239 * enabled but the clock is not, GPIO module seems to be unable
240 * to detect events and generate interrupts at least on OMAP3.
241 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200242 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300243
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300244 clk_disable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530245 bank->dbck_enabled = false;
246 }
247}
248
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700249/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200250 * omap2_set_gpio_debounce - low level gpio debounce time
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700251 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200252 * @offset: the gpio number on this @bank
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700253 * @debounce: debounce time to use
254 *
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300255 * OMAP's debounce time is in 31us steps
256 * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
257 * so we need to convert and round up to the closest unit.
David Rivshin83977442017-04-24 18:56:50 -0400258 *
259 * Return: 0 on success, negative error otherwise.
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700260 */
David Rivshin83977442017-04-24 18:56:50 -0400261static int omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
262 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700263{
Kevin Hilman9942da02011-04-22 12:02:05 -0700264 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700265 u32 val;
266 u32 l;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300267 bool enable = !!debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700268
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800269 if (!bank->dbck_flag)
David Rivshin83977442017-04-24 18:56:50 -0400270 return -ENOTSUPP;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800271
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300272 if (enable) {
273 debounce = DIV_ROUND_UP(debounce, 31) - 1;
David Rivshin83977442017-04-24 18:56:50 -0400274 if ((debounce & OMAP4_GPIO_DEBOUNCINGTIME_MASK) != debounce)
275 return -EINVAL;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300276 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700277
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200278 l = BIT(offset);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700279
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300280 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700281 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200282 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700283
Kevin Hilman9942da02011-04-22 12:02:05 -0700284 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200285 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700286
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300287 if (enable)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700288 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530289 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700290 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300291 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700292
Victor Kamensky661553b2013-11-16 02:01:04 +0200293 writel_relaxed(val, reg);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300294 clk_disable(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530295 /*
296 * Enable debounce clock per module.
297 * This call is mandatory because in omap_gpio_request() when
298 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
299 * runtime callbck fails to turn on dbck because dbck_enable_mask
300 * used within _gpio_dbck_enable() is still not initialized at
301 * that point. Therefore we have to enable dbck here.
302 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200303 omap_gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530304 if (bank->dbck_enable_mask) {
305 bank->context.debounce = debounce;
306 bank->context.debounce_en = val;
307 }
David Rivshin83977442017-04-24 18:56:50 -0400308
309 return 0;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700310}
311
Jon Hunterc9c55d92012-10-26 14:26:04 -0500312/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200313 * omap_clear_gpio_debounce - clear debounce settings for a gpio
Jon Hunterc9c55d92012-10-26 14:26:04 -0500314 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200315 * @offset: the gpio number on this @bank
Jon Hunterc9c55d92012-10-26 14:26:04 -0500316 *
317 * If a gpio is using debounce, then clear the debounce enable bit and if
318 * this is the only gpio in this bank using debounce, then clear the debounce
319 * time too. The debounce clock will also be disabled when calling this function
320 * if this is the only gpio in the bank using debounce.
321 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200322static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
Jon Hunterc9c55d92012-10-26 14:26:04 -0500323{
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200324 u32 gpio_bit = BIT(offset);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500325
326 if (!bank->dbck_flag)
327 return;
328
329 if (!(bank->dbck_enable_mask & gpio_bit))
330 return;
331
332 bank->dbck_enable_mask &= ~gpio_bit;
333 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200334 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500335 bank->base + bank->regs->debounce_en);
336
337 if (!bank->dbck_enable_mask) {
338 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200339 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500340 bank->regs->debounce);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300341 clk_disable(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500342 bank->dbck_enabled = false;
343 }
344}
345
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200346static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530347 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800349 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200350 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100351
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200352 omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
353 trigger & IRQ_TYPE_LEVEL_LOW);
354 omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
355 trigger & IRQ_TYPE_LEVEL_HIGH);
356 omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
357 trigger & IRQ_TYPE_EDGE_RISING);
358 omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
359 trigger & IRQ_TYPE_EDGE_FALLING);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530360
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530361 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200362 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530363 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200364 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530365 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200366 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530367 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200368 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530369
370 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200371 omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530372 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200373 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530374 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530375
Ambresh K55b220c2011-06-15 13:40:45 -0700376 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530377 if (!bank->regs->irqctrl) {
378 /* On omap24xx proceed only when valid GPIO bit is set */
379 if (bank->non_wakeup_gpios) {
380 if (!(bank->non_wakeup_gpios & gpio_bit))
381 goto exit;
382 }
383
Chunqiu Wang699117a62009-06-24 17:13:39 +0000384 /*
385 * Log the edge gpio and manually trigger the IRQ
386 * after resume if the input level changes
387 * to avoid irq lost during PER RET/OFF mode
388 * Applies for omap2 non-wakeup gpio and all omap3 gpios
389 */
390 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800391 bank->enabled_non_wakeup_gpios |= gpio_bit;
392 else
393 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
394 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700395
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530396exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530397 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200398 readl_relaxed(bank->base + bank->regs->leveldetect0) |
399 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100400}
401
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800402#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800403/*
404 * This only applies to chips that can't do both rising and falling edge
405 * detection at once. For all other chips, this function is a noop.
406 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200407static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800408{
409 void __iomem *reg = bank->base;
410 u32 l = 0;
411
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530412 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800413 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530414
415 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800416
Victor Kamensky661553b2013-11-16 02:01:04 +0200417 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800418 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200419 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800420 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200421 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800422
Victor Kamensky661553b2013-11-16 02:01:04 +0200423 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800424}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530425#else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200426static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800427#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800428
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200429static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
430 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100431{
432 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530433 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100434 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100435
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530436 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200437 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530438 } else if (bank->regs->irqctrl) {
439 reg += bank->regs->irqctrl;
440
Victor Kamensky661553b2013-11-16 02:01:04 +0200441 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000442 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200443 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100444 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200445 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100446 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200447 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100448 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530449 return -EINVAL;
450
Victor Kamensky661553b2013-11-16 02:01:04 +0200451 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530452 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100453 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530454 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100455 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530456 reg += bank->regs->edgectrl1;
457
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100458 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200459 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100460 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100461 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100462 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100463 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200464 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530465
466 /* Enable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200467 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530468 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200469 readl_relaxed(bank->base + bank->regs->wkup_en);
470 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100471 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100472 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100473}
474
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200475static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200476{
477 if (bank->regs->pinctrl) {
478 void __iomem *reg = bank->base + bank->regs->pinctrl;
479
480 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200481 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200482 }
483
484 if (bank->regs->ctrl && !BANK_USED(bank)) {
485 void __iomem *reg = bank->base + bank->regs->ctrl;
486 u32 ctrl;
487
Victor Kamensky661553b2013-11-16 02:01:04 +0200488 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200489 /* Module is enabled, clocks are not gated */
490 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200491 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200492 bank->context.ctrl = ctrl;
493 }
494}
495
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200496static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200497{
498 void __iomem *base = bank->base;
499
500 if (bank->regs->wkup_en &&
501 !LINE_USED(bank->mod_usage, offset) &&
502 !LINE_USED(bank->irq_usage, offset)) {
503 /* Disable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200504 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200505 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200506 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200507 }
508
509 if (bank->regs->ctrl && !BANK_USED(bank)) {
510 void __iomem *reg = bank->base + bank->regs->ctrl;
511 u32 ctrl;
512
Victor Kamensky661553b2013-11-16 02:01:04 +0200513 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200514 /* Module is disabled, clocks are gated */
515 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200516 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200517 bank->context.ctrl = ctrl;
518 }
519}
520
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200521static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200522{
523 void __iomem *reg = bank->base + bank->regs->direction;
524
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200525 return readl_relaxed(reg) & BIT(offset);
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200526}
527
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200528static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
Tony Lindgren3d009c82015-01-16 14:50:50 -0800529{
530 if (!LINE_USED(bank->mod_usage, offset)) {
531 omap_enable_gpio_module(bank, offset);
532 omap_set_gpio_direction(bank, offset, 1);
533 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200534 bank->irq_usage |= BIT(offset);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800535}
536
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200537static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100538{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200539 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100540 int retval;
David Brownella6472532008-03-03 04:33:30 -0800541 unsigned long flags;
Grygorii Strashkoea5fbe82015-03-23 14:18:29 +0200542 unsigned offset = d->hwirq;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100543
David Brownelle5c56ed2006-12-06 17:13:59 -0800544 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100545 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800546
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530547 if (!bank->regs->leveldetect0 &&
548 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100549 return -EINVAL;
550
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200551 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200552 retval = omap_set_gpio_triggering(bank, offset, type);
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300553 if (retval) {
Axel Lin627c89b2015-08-05 22:37:41 +0800554 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300555 goto error;
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300556 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200557 omap_gpio_init_irq(bank, offset);
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200558 if (!omap_gpio_is_input(bank, offset)) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200559 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300560 retval = -EINVAL;
561 goto error;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200562 }
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200563 raw_spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800564
565 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner43ec2e42015-06-23 15:52:39 +0200566 irq_set_handler_locked(d, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800567 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500568 /*
569 * Edge IRQs are already cleared/acked in irq_handler and
570 * not need to be masked, as result handle_edge_irq()
571 * logic is excessed here and may cause lose of interrupts.
572 * So just use handle_simple_irq.
573 */
574 irq_set_handler_locked(d, handle_simple_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800575
Grygorii Strashko1562e462015-05-22 17:35:49 +0300576 return 0;
577
578error:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100579 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100580}
581
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200582static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100583{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100584 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100585
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700586 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200587 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300588
589 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700590 if (bank->regs->irqstatus2) {
591 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200592 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700593 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700594
595 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200596 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100597}
598
Grygorii Strashko9943f262015-03-23 14:18:27 +0200599static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
600 unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100601{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200602 omap_clear_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100603}
604
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200605static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
Imre Deakea6dedd2006-06-26 16:16:00 -0700606{
607 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700608 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200609 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700610
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700611 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200612 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700613 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700614 l = ~l;
615 l &= mask;
616 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700617}
618
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200619static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100620{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100621 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100622 u32 l;
623
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700624 if (bank->regs->set_irqenable) {
625 reg += bank->regs->set_irqenable;
626 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530627 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700628 } else {
629 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200630 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700631 if (bank->regs->irqenable_inv)
632 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100633 else
634 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530635 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100636 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700637
Victor Kamensky661553b2013-11-16 02:01:04 +0200638 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700639}
640
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200641static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700642{
643 void __iomem *reg = bank->base;
644 u32 l;
645
646 if (bank->regs->clr_irqenable) {
647 reg += bank->regs->clr_irqenable;
648 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530649 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700650 } else {
651 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200652 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700653 if (bank->regs->irqenable_inv)
654 l |= gpio_mask;
655 else
656 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530657 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700658 }
659
Victor Kamensky661553b2013-11-16 02:01:04 +0200660 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100661}
662
Grygorii Strashko9943f262015-03-23 14:18:27 +0200663static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
664 unsigned offset, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100665{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530666 if (enable)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200667 omap_enable_gpio_irqbank(bank, BIT(offset));
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530668 else
Grygorii Strashko9943f262015-03-23 14:18:27 +0200669 omap_disable_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670}
671
Tony Lindgren92105bb2005-09-07 17:20:26 +0100672/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200673static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100674{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200675 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100676
Grygorii Strashko0c0451e2016-04-12 13:52:31 +0300677 return irq_set_irq_wake(bank->irq, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100678}
679
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800680static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100681{
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100682 struct gpio_bank *bank = gpiochip_get_data(chip);
David Brownella6472532008-03-03 04:33:30 -0800683 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100684
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530685 /*
686 * If this is the first gpio_request for the bank,
687 * enable the bank module.
688 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200689 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200690 pm_runtime_get_sync(chip->parent);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100691
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200692 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashkoc3518172015-05-22 17:35:51 +0300693 omap_enable_gpio_module(bank, offset);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200694 bank->mod_usage |= BIT(offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200695 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100696
697 return 0;
698}
699
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800700static void omap_gpio_free(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
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200705 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200706 bank->mod_usage &= ~(BIT(offset));
Grygorii Strashko5f982c72015-05-22 17:35:48 +0300707 if (!LINE_USED(bank->irq_usage, offset)) {
708 omap_set_gpio_direction(bank, offset, 1);
709 omap_clear_gpio_debounce(bank, offset);
710 }
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200711 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200712 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530713
714 /*
715 * If this is the last gpio to be freed in the bank,
716 * disable the bank module.
717 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200718 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200719 pm_runtime_put(chip->parent);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100720}
721
722/*
723 * We need to unmask the GPIO bank interrupt as soon as possible to
724 * avoid missing GPIO interrupts for other lines in the bank.
725 * Then we need to mask-read-clear-unmask the triggered GPIO lines
726 * in the bank to avoid missing nested interrupts for a GPIO line.
727 * If we wait to unmask individual GPIO lines in the bank after the
728 * line's interrupt handler has been run, we may miss some nested
729 * interrupts.
730 */
Grygorii Strashko450fa542015-09-25 12:28:03 -0700731static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100733 void __iomem *isr_reg = NULL;
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500734 u32 enabled, isr, level_mask;
Jon Hunter3513cde2013-04-04 15:16:14 -0500735 unsigned int bit;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700736 struct gpio_bank *bank = gpiobank;
737 unsigned long wa_lock_flags;
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300738 unsigned long lock_flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100739
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700740 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800741 if (WARN_ON(!isr_reg))
742 goto exit;
743
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200744 pm_runtime_get_sync(bank->chip.parent);
Grygorii Strashko450fa542015-09-25 12:28:03 -0700745
Laurent Navete83507b2013-03-20 13:15:57 +0100746 while (1) {
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300747 raw_spin_lock_irqsave(&bank->lock, lock_flags);
748
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200749 enabled = omap_get_gpio_irqbank_mask(bank);
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500750 isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100751
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530752 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800753 level_mask = bank->level_mask & enabled;
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500754 else
755 level_mask = 0;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100756
757 /* clear edge sensitive interrupts before handler(s) are
758 called so that we don't miss any interrupt occurred while
759 executing them */
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500760 if (isr & ~level_mask)
761 omap_clear_gpio_irqbank(bank, isr & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100762
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300763 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
764
Tony Lindgren92105bb2005-09-07 17:20:26 +0100765 if (!isr)
766 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100767
Jon Hunter3513cde2013-04-04 15:16:14 -0500768 while (isr) {
769 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200770 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100771
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300772 raw_spin_lock_irqsave(&bank->lock, lock_flags);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800773 /*
774 * Some chips can't respond to both rising and falling
775 * at the same time. If this irq was requested with
776 * both flags, we need to flip the ICR data for the IRQ
777 * to respond to the IRQ for the opposite direction.
778 * This will be indicated in the bank toggle_mask.
779 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200780 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200781 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800782
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300783 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
784
Grygorii Strashko450fa542015-09-25 12:28:03 -0700785 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
786
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100787 generic_handle_irq(irq_find_mapping(bank->chip.irq.domain,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200788 bit));
Grygorii Strashko450fa542015-09-25 12:28:03 -0700789
790 raw_spin_unlock_irqrestore(&bank->wa_lock,
791 wa_lock_flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100792 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000793 }
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800794exit:
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200795 pm_runtime_put(bank->chip.parent);
Grygorii Strashko450fa542015-09-25 12:28:03 -0700796 return IRQ_HANDLED;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100797}
798
Tony Lindgren3d009c82015-01-16 14:50:50 -0800799static unsigned int omap_gpio_irq_startup(struct irq_data *d)
800{
801 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800802 unsigned long flags;
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200803 unsigned offset = d->hwirq;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800804
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200805 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300806
807 if (!LINE_USED(bank->mod_usage, offset))
808 omap_set_gpio_direction(bank, offset, 1);
809 else if (!omap_gpio_is_input(bank, offset))
810 goto err;
811 omap_enable_gpio_module(bank, offset);
812 bank->irq_usage |= BIT(offset);
813
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200814 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800815 omap_gpio_unmask_irq(d);
816
817 return 0;
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300818err:
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200819 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300820 return -EINVAL;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800821}
822
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200823static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300824{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200825 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700826 unsigned long flags;
Grygorii Strashko9943f262015-03-23 14:18:27 +0200827 unsigned offset = d->hwirq;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300828
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200829 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200830 bank->irq_usage &= ~(BIT(offset));
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300831 omap_set_gpio_irqenable(bank, offset, 0);
832 omap_clear_gpio_irqstatus(bank, offset);
833 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
834 if (!LINE_USED(bank->mod_usage, offset))
835 omap_clear_gpio_debounce(bank, offset);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200836 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200837 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700838}
839
840static void omap_gpio_irq_bus_lock(struct irq_data *data)
841{
842 struct gpio_bank *bank = omap_irq_data_get_bank(data);
843
844 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200845 pm_runtime_get_sync(bank->chip.parent);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700846}
847
848static void gpio_irq_bus_sync_unlock(struct irq_data *data)
849{
850 struct gpio_bank *bank = omap_irq_data_get_bank(data);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200851
852 /*
853 * If this is the last IRQ to be freed in the bank,
854 * disable the bank module.
855 */
856 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200857 pm_runtime_put(bank->chip.parent);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300858}
859
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200860static void omap_gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100861{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200862 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200863 unsigned offset = d->hwirq;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100864
Grygorii Strashko9943f262015-03-23 14:18:27 +0200865 omap_clear_gpio_irqstatus(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100866}
867
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200868static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100869{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200870 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200871 unsigned offset = d->hwirq;
Colin Cross85ec7b92011-06-06 13:38:18 -0700872 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100873
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200874 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200875 omap_set_gpio_irqenable(bank, offset, 0);
876 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200877 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100878}
879
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200880static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100881{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200882 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200883 unsigned offset = d->hwirq;
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100884 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700885 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700886
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200887 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700888 if (trigger)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200889 omap_set_gpio_triggering(bank, offset, trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800890
891 /* For level-triggered GPIOs, the clearing must be done after
892 * the HW source is cleared, thus after the handler has run */
Grygorii Strashko9943f262015-03-23 14:18:27 +0200893 if (bank->level_mask & BIT(offset)) {
894 omap_set_gpio_irqenable(bank, offset, 0);
895 omap_clear_gpio_irqstatus(bank, offset);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800896 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100897
Grygorii Strashko9943f262015-03-23 14:18:27 +0200898 omap_set_gpio_irqenable(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200899 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100900}
901
David Brownelle5c56ed2006-12-06 17:13:59 -0800902/*---------------------------------------------------------------------*/
903
Magnus Damm79ee0312009-07-08 13:22:04 +0200904static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800905{
Magnus Damm79ee0312009-07-08 13:22:04 +0200906 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800907 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800908 void __iomem *mask_reg = bank->base +
909 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800910 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800911
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200912 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200913 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200914 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800915
916 return 0;
917}
918
Magnus Damm79ee0312009-07-08 13:22:04 +0200919static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800920{
Magnus Damm79ee0312009-07-08 13:22:04 +0200921 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800922 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800923 void __iomem *mask_reg = bank->base +
924 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800925 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800926
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200927 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200928 writel_relaxed(bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200929 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800930
931 return 0;
932}
933
Alexey Dobriyan47145212009-12-14 18:00:08 -0800934static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200935 .suspend_noirq = omap_mpuio_suspend_noirq,
936 .resume_noirq = omap_mpuio_resume_noirq,
937};
938
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200939/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800940static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800941 .driver = {
942 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200943 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800944 },
945};
946
947static struct platform_device omap_mpuio_device = {
948 .name = "mpuio",
949 .id = -1,
950 .dev = {
951 .driver = &omap_mpuio_driver.driver,
952 }
953 /* could list the /proc/iomem resources */
954};
955
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200956static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800957{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800958 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700959
David Brownell11a78b72006-12-06 17:14:11 -0800960 if (platform_driver_register(&omap_mpuio_driver) == 0)
961 (void) platform_device_register(&omap_mpuio_device);
962}
963
David Brownelle5c56ed2006-12-06 17:13:59 -0800964/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100965
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200966static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200967{
968 struct gpio_bank *bank;
969 unsigned long flags;
970 void __iomem *reg;
971 int dir;
972
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100973 bank = gpiochip_get_data(chip);
Yegor Yefremov93700842014-04-24 08:57:39 +0200974 reg = bank->base + bank->regs->direction;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200975 raw_spin_lock_irqsave(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +0200976 dir = !!(readl_relaxed(reg) & BIT(offset));
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200977 raw_spin_unlock_irqrestore(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +0200978 return dir;
979}
980
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200981static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800982{
983 struct gpio_bank *bank;
984 unsigned long flags;
985
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100986 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200987 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200988 omap_set_gpio_direction(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200989 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800990 return 0;
991}
992
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200993static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800994{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300995 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300996
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100997 bank = gpiochip_get_data(chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300998
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200999 if (omap_gpio_is_input(bank, offset))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001000 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001001 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001002 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -08001003}
1004
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001005static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001006{
1007 struct gpio_bank *bank;
1008 unsigned long flags;
1009
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001010 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001011 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001012 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001013 omap_set_gpio_direction(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001014 raw_spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +02001015 return 0;
David Brownell52e31342008-03-03 12:43:23 -08001016}
1017
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001018static int omap_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
1019 unsigned long *bits)
1020{
1021 struct gpio_bank *bank = gpiochip_get_data(chip);
1022 void __iomem *reg = bank->base + bank->regs->direction;
1023 unsigned long in = readl_relaxed(reg), l;
1024
1025 *bits = 0;
1026
1027 l = in & *mask;
1028 if (l)
1029 *bits |= omap_get_gpio_datain_multiple(bank, &l);
1030
1031 l = ~in & *mask;
1032 if (l)
1033 *bits |= omap_get_gpio_dataout_multiple(bank, &l);
1034
1035 return 0;
1036}
1037
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001038static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
1039 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001040{
1041 struct gpio_bank *bank;
1042 unsigned long flags;
David Rivshin83977442017-04-24 18:56:50 -04001043 int ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001044
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001045 bank = gpiochip_get_data(chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001046
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001047 raw_spin_lock_irqsave(&bank->lock, flags);
David Rivshin83977442017-04-24 18:56:50 -04001048 ret = omap2_set_gpio_debounce(bank, offset, debounce);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001049 raw_spin_unlock_irqrestore(&bank->lock, flags);
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001050
David Rivshin83977442017-04-24 18:56:50 -04001051 if (ret)
1052 dev_info(chip->parent,
1053 "Could not set line %u debounce to %u microseconds (%d)",
1054 offset, debounce, ret);
1055
1056 return ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001057}
1058
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001059static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset,
1060 unsigned long config)
1061{
1062 u32 debounce;
1063
1064 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
1065 return -ENOTSUPP;
1066
1067 debounce = pinconf_to_config_argument(config);
1068 return omap_gpio_debounce(chip, offset, debounce);
1069}
1070
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001071static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -08001072{
1073 struct gpio_bank *bank;
1074 unsigned long flags;
1075
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001076 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001077 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001078 bank->set_dataout(bank, offset, value);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001079 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -08001080}
1081
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001082static void omap_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
1083 unsigned long *bits)
1084{
1085 struct gpio_bank *bank = gpiochip_get_data(chip);
1086 unsigned long flags;
1087
1088 raw_spin_lock_irqsave(&bank->lock, flags);
1089 bank->set_dataout_multiple(bank, mask, bits);
1090 raw_spin_unlock_irqrestore(&bank->lock, flags);
1091}
1092
David Brownell52e31342008-03-03 12:43:23 -08001093/*---------------------------------------------------------------------*/
1094
Arnd Bergmanne4b2ae72017-09-16 22:42:21 +02001095static void omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001096{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001097 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001098 u32 rev;
1099
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001100 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001101 return;
1102
Victor Kamensky661553b2013-11-16 02:01:04 +02001103 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001104 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001105 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001106
1107 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001108}
1109
Charulatha V03e128c2011-05-05 19:58:01 +05301110static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001111{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301112 void __iomem *base = bank->base;
1113 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001114
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301115 if (bank->width == 16)
1116 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001117
Charulatha Vd0d665a2011-08-31 00:02:21 +05301118 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001119 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301120 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001121 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301122
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001123 omap_gpio_rmw(base, bank->regs->irqenable, l,
1124 bank->regs->irqenable_inv);
1125 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1126 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301127 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001128 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301129
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301130 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001131 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301132 /* Initialize interface clk ungated, module enabled */
1133 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001134 writel_relaxed(0, base + bank->regs->ctrl);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001135}
1136
Nishanth Menon46824e222014-09-05 14:52:55 -05001137static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001138{
Grygorii Strashko81930322017-11-15 12:36:33 -06001139 struct gpio_irq_chip *irq;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001140 static int gpio;
Linus Walleij088413b2017-12-29 13:22:58 +01001141 const char *label;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001142 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001143 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001144
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001145 /*
1146 * REVISIT eventually switch from OMAP-specific gpio structs
1147 * over to the generic ones
1148 */
1149 bank->chip.request = omap_gpio_request;
1150 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001151 bank->chip.get_direction = omap_gpio_get_direction;
1152 bank->chip.direction_input = omap_gpio_input;
1153 bank->chip.get = omap_gpio_get;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001154 bank->chip.get_multiple = omap_gpio_get_multiple;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001155 bank->chip.direction_output = omap_gpio_output;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001156 bank->chip.set_config = omap_gpio_set_config;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001157 bank->chip.set = omap_gpio_set;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001158 bank->chip.set_multiple = omap_gpio_set_multiple;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301159 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001160 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301161 if (bank->regs->wkup_en)
Linus Walleij58383c782015-11-04 09:56:26 +01001162 bank->chip.parent = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001163 bank->chip.base = OMAP_MPUIO(0);
1164 } else {
Linus Walleij088413b2017-12-29 13:22:58 +01001165 label = devm_kasprintf(bank->chip.parent, GFP_KERNEL, "gpio-%d-%d",
1166 gpio, gpio + bank->width - 1);
1167 if (!label)
1168 return -ENOMEM;
1169 bank->chip.label = label;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001170 bank->chip.base = gpio;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001171 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001172 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001173
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001174#ifdef CONFIG_ARCH_OMAP1
1175 /*
1176 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1177 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1178 */
Bartosz Golaszewski2ed36f32017-03-04 17:23:31 +01001179 irq_base = devm_irq_alloc_descs(bank->chip.parent,
1180 -1, 0, bank->width, 0);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001181 if (irq_base < 0) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001182 dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n");
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001183 return -ENODEV;
1184 }
1185#endif
1186
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001187 /* MPUIO is a bit different, reading IRQ status clears it */
1188 if (bank->is_mpuio) {
1189 irqc->irq_ack = dummy_irq_chip.irq_ack;
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001190 if (!bank->regs->wkup_en)
1191 irqc->irq_set_wake = NULL;
1192 }
1193
Grygorii Strashko81930322017-11-15 12:36:33 -06001194 irq = &bank->chip.irq;
1195 irq->chip = irqc;
1196 irq->handler = handle_bad_irq;
1197 irq->default_type = IRQ_TYPE_NONE;
1198 irq->num_parents = 1;
1199 irq->parents = &bank->irq;
1200 irq->first = irq_base;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001201
Grygorii Strashko81930322017-11-15 12:36:33 -06001202 ret = gpiochip_add_data(&bank->chip, bank);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001203 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001204 dev_err(bank->chip.parent,
Grygorii Strashko81930322017-11-15 12:36:33 -06001205 "Could not register gpio chip %d\n", ret);
1206 return ret;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001207 }
1208
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001209 ret = devm_request_irq(bank->chip.parent, bank->irq,
1210 omap_gpio_irq_handler,
1211 0, dev_name(bank->chip.parent), bank);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001212 if (ret)
1213 gpiochip_remove(&bank->chip);
1214
Grygorii Strashko81930322017-11-15 12:36:33 -06001215 if (!bank->is_mpuio)
1216 gpio += bank->width;
1217
Grygorii Strashko450fa542015-09-25 12:28:03 -07001218 return ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001219}
1220
Benoit Cousson384ebe12011-08-16 11:53:02 +02001221static const struct of_device_id omap_gpio_match[];
1222
Bill Pemberton38363092012-11-19 13:22:34 -05001223static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001224{
Benoit Cousson862ff642012-02-01 15:58:56 +01001225 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001226 struct device_node *node = dev->of_node;
1227 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001228 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001229 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001230 struct gpio_bank *bank;
Nishanth Menon46824e222014-09-05 14:52:55 -05001231 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001232 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001233
Benoit Cousson384ebe12011-08-16 11:53:02 +02001234 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1235
Jingoo Hane56aee12013-07-30 17:08:05 +09001236 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001237 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001238 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001239
Markus Elfringf97364c2018-02-10 21:49:22 +01001240 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
Markus Elfring9117d402018-02-10 21:46:30 +01001241 if (!bank)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001242 return -ENOMEM;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001243
Nishanth Menon46824e222014-09-05 14:52:55 -05001244 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1245 if (!irqc)
1246 return -ENOMEM;
1247
Tony Lindgren3d009c82015-01-16 14:50:50 -08001248 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e222014-09-05 14:52:55 -05001249 irqc->irq_shutdown = omap_gpio_irq_shutdown,
1250 irqc->irq_ack = omap_gpio_ack_irq,
1251 irqc->irq_mask = omap_gpio_mask_irq,
1252 irqc->irq_unmask = omap_gpio_unmask_irq,
1253 irqc->irq_set_type = omap_gpio_irq_type,
1254 irqc->irq_set_wake = omap_gpio_wake_enable,
Grygorii Strashkoaca82d12015-09-25 12:28:02 -07001255 irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
1256 irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
Nishanth Menon46824e222014-09-05 14:52:55 -05001257 irqc->name = dev_name(&pdev->dev);
Grygorii Strashko0c0451e2016-04-12 13:52:31 +03001258 irqc->flags = IRQCHIP_MASK_ON_SUSPEND;
Nishanth Menon46824e222014-09-05 14:52:55 -05001259
Grygorii Strashko89d18e32015-08-18 14:10:53 +03001260 bank->irq = platform_get_irq(pdev, 0);
1261 if (bank->irq <= 0) {
1262 if (!bank->irq)
1263 bank->irq = -ENXIO;
1264 if (bank->irq != -EPROBE_DEFER)
1265 dev_err(dev,
1266 "can't get irq resource ret=%d\n", bank->irq);
1267 return bank->irq;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001268 }
1269
Linus Walleij58383c782015-11-04 09:56:26 +01001270 bank->chip.parent = dev;
Grygorii Strashkoc23837c2015-06-25 18:13:33 +03001271 bank->chip.owner = THIS_MODULE;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001272 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001273 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001274 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301275 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301276 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001277 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001278#ifdef CONFIG_OF_GPIO
1279 bank->chip.of_node = of_node_get(node);
1280#endif
Jon Huntera2797be2013-04-04 15:16:15 -05001281 if (node) {
1282 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1283 bank->loses_context = true;
1284 } else {
1285 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001286
1287 if (bank->loses_context)
1288 bank->get_context_loss_count =
1289 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001290 }
1291
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001292 if (bank->regs->set_dataout && bank->regs->clr_dataout) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001293 bank->set_dataout = omap_set_gpio_dataout_reg;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001294 bank->set_dataout_multiple = omap_set_gpio_dataout_reg_multiple;
1295 } else {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001296 bank->set_dataout = omap_set_gpio_dataout_mask;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001297 bank->set_dataout_multiple =
1298 omap_set_gpio_dataout_mask_multiple;
1299 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001300
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001301 raw_spin_lock_init(&bank->lock);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001302 raw_spin_lock_init(&bank->wa_lock);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001303
1304 /* Static mapping, never released */
1305 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001306 bank->base = devm_ioremap_resource(dev, res);
1307 if (IS_ERR(bank->base)) {
Jingoo Han717f70e2014-02-12 11:51:38 +09001308 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001309 }
1310
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001311 if (bank->dbck_flag) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001312 bank->dbck = devm_clk_get(dev, "dbclk");
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001313 if (IS_ERR(bank->dbck)) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001314 dev_err(dev,
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001315 "Could not get gpio dbck. Disable debounce\n");
1316 bank->dbck_flag = false;
1317 } else {
1318 clk_prepare(bank->dbck);
1319 }
1320 }
1321
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301322 platform_set_drvdata(pdev, bank);
1323
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001324 pm_runtime_enable(dev);
1325 pm_runtime_irq_safe(dev);
1326 pm_runtime_get_sync(dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001327
Charulatha Vd0d665a2011-08-31 00:02:21 +05301328 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001329 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301330
Charulatha V03e128c2011-05-05 19:58:01 +05301331 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001332
Nishanth Menon46824e222014-09-05 14:52:55 -05001333 ret = omap_gpio_chip_init(bank, irqc);
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001334 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001335 pm_runtime_put_sync(dev);
1336 pm_runtime_disable(dev);
Arvind Yadave2c3c192017-08-01 12:14:31 +05301337 if (bank->dbck_flag)
1338 clk_unprepare(bank->dbck);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001339 return ret;
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001340 }
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001341
Tony Lindgren9a748052010-12-07 16:26:56 -08001342 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001343
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001344 pm_runtime_put(dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301345
Charulatha V03e128c2011-05-05 19:58:01 +05301346 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001347
Jon Hunter879fe322013-04-04 15:16:12 -05001348 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001349}
1350
Tony Lindgrencac089f2015-04-23 16:56:22 -07001351static int omap_gpio_remove(struct platform_device *pdev)
1352{
1353 struct gpio_bank *bank = platform_get_drvdata(pdev);
1354
1355 list_del(&bank->node);
1356 gpiochip_remove(&bank->chip);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001357 pm_runtime_disable(&pdev->dev);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001358 if (bank->dbck_flag)
1359 clk_unprepare(bank->dbck);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001360
1361 return 0;
1362}
1363
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301364#ifdef CONFIG_ARCH_OMAP2PLUS
1365
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001366#if defined(CONFIG_PM)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301367static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001368
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301369static int omap_gpio_runtime_suspend(struct device *dev)
1370{
1371 struct platform_device *pdev = to_platform_device(dev);
1372 struct gpio_bank *bank = platform_get_drvdata(pdev);
1373 u32 l1 = 0, l2 = 0;
1374 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001375 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301376
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001377 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001378
1379 /*
1380 * Only edges can generate a wakeup event to the PRCM.
1381 *
1382 * Therefore, ensure any wake-up capable GPIOs have
1383 * edge-detection enabled before going idle to ensure a wakeup
1384 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1385 * NDA TRM 25.5.3.1)
1386 *
1387 * The normal values will be restored upon ->runtime_resume()
1388 * by writing back the values saved in bank->context.
1389 */
1390 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1391 if (wake_low)
Victor Kamensky661553b2013-11-16 02:01:04 +02001392 writel_relaxed(wake_low | bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001393 bank->base + bank->regs->fallingdetect);
1394 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1395 if (wake_hi)
Victor Kamensky661553b2013-11-16 02:01:04 +02001396 writel_relaxed(wake_hi | bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001397 bank->base + bank->regs->risingdetect);
1398
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001399 if (!bank->enabled_non_wakeup_gpios)
1400 goto update_gpio_context_count;
1401
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301402 if (bank->power_mode != OFF_MODE) {
1403 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301404 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301405 }
1406 /*
1407 * If going to OFF, remove triggering for all
1408 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1409 * generated. See OMAP2420 Errata item 1.101.
1410 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001411 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301412 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301413 l1 = bank->context.fallingdetect;
1414 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301415
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301416 l1 &= ~bank->enabled_non_wakeup_gpios;
1417 l2 &= ~bank->enabled_non_wakeup_gpios;
1418
Victor Kamensky661553b2013-11-16 02:01:04 +02001419 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1420 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301421
1422 bank->workaround_enabled = true;
1423
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301424update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301425 if (bank->get_context_loss_count)
1426 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001427 bank->get_context_loss_count(dev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301428
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001429 omap_gpio_dbck_disable(bank);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001430 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301431
1432 return 0;
1433}
1434
Jon Hunter352a2d52013-04-15 13:06:54 -05001435static void omap_gpio_init_context(struct gpio_bank *p);
1436
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301437static int omap_gpio_runtime_resume(struct device *dev)
1438{
1439 struct platform_device *pdev = to_platform_device(dev);
1440 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301441 u32 l = 0, gen, gen0, gen1;
1442 unsigned long flags;
Jon Huntera2797be2013-04-04 15:16:15 -05001443 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301444
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001445 raw_spin_lock_irqsave(&bank->lock, flags);
Jon Hunter352a2d52013-04-15 13:06:54 -05001446
1447 /*
1448 * On the first resume during the probe, the context has not
1449 * been initialised and so initialise it now. Also initialise
1450 * the context loss count.
1451 */
1452 if (bank->loses_context && !bank->context_valid) {
1453 omap_gpio_init_context(bank);
1454
1455 if (bank->get_context_loss_count)
1456 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001457 bank->get_context_loss_count(dev);
Jon Hunter352a2d52013-04-15 13:06:54 -05001458 }
1459
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001460 omap_gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001461
1462 /*
1463 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1464 * GPIOs were set to edge trigger also in order to be able to
1465 * generate a PRCM wakeup. Here we restore the
1466 * pre-runtime_suspend() values for edge triggering.
1467 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001468 writel_relaxed(bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001469 bank->base + bank->regs->fallingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001470 writel_relaxed(bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001471 bank->base + bank->regs->risingdetect);
1472
Jon Huntera2797be2013-04-04 15:16:15 -05001473 if (bank->loses_context) {
1474 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301475 omap_gpio_restore_context(bank);
1476 } else {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001477 c = bank->get_context_loss_count(dev);
Jon Huntera2797be2013-04-04 15:16:15 -05001478 if (c != bank->context_loss_count) {
1479 omap_gpio_restore_context(bank);
1480 } else {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001481 raw_spin_unlock_irqrestore(&bank->lock, flags);
Jon Huntera2797be2013-04-04 15:16:15 -05001482 return 0;
1483 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301484 }
1485 }
1486
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301487 if (!bank->workaround_enabled) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001488 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301489 return 0;
1490 }
1491
Victor Kamensky661553b2013-11-16 02:01:04 +02001492 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301493
1494 /*
1495 * Check if any of the non-wakeup interrupt GPIOs have changed
1496 * state. If so, generate an IRQ by software. This is
1497 * horribly racy, but it's the best we can do to work around
1498 * this silicon bug.
1499 */
1500 l ^= bank->saved_datain;
1501 l &= bank->enabled_non_wakeup_gpios;
1502
1503 /*
1504 * No need to generate IRQs for the rising edge for gpio IRQs
1505 * configured with falling edge only; and vice versa.
1506 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301507 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301508 gen0 &= bank->saved_datain;
1509
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301510 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301511 gen1 &= ~(bank->saved_datain);
1512
1513 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301514 gen = l & (~(bank->context.fallingdetect) &
1515 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301516 /* Consider all GPIO IRQs needed to be updated */
1517 gen |= gen0 | gen1;
1518
1519 if (gen) {
1520 u32 old0, old1;
1521
Victor Kamensky661553b2013-11-16 02:01:04 +02001522 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1523 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301524
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301525 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001526 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301527 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001528 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301529 bank->regs->leveldetect1);
1530 }
1531
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301532 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001533 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301534 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001535 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301536 bank->regs->leveldetect1);
1537 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001538 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1539 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301540 }
1541
1542 bank->workaround_enabled = false;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001543 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301544
1545 return 0;
1546}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001547#endif /* CONFIG_PM */
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301548
Tony Lindgrencac089f2015-04-23 16:56:22 -07001549#if IS_BUILTIN(CONFIG_GPIO_OMAP)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301550void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001551{
Charulatha V03e128c2011-05-05 19:58:01 +05301552 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001553
Charulatha V03e128c2011-05-05 19:58:01 +05301554 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001555 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301556 continue;
1557
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301558 bank->power_mode = pwr_mode;
1559
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001560 pm_runtime_put_sync_suspend(bank->chip.parent);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001561 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001562}
1563
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001564void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001565{
Charulatha V03e128c2011-05-05 19:58:01 +05301566 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001567
Charulatha V03e128c2011-05-05 19:58:01 +05301568 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001569 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301570 continue;
1571
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001572 pm_runtime_get_sync(bank->chip.parent);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001573 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001574}
Tony Lindgrencac089f2015-04-23 16:56:22 -07001575#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001576
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001577#if defined(CONFIG_PM)
Jon Hunter352a2d52013-04-15 13:06:54 -05001578static void omap_gpio_init_context(struct gpio_bank *p)
1579{
1580 struct omap_gpio_reg_offs *regs = p->regs;
1581 void __iomem *base = p->base;
1582
Victor Kamensky661553b2013-11-16 02:01:04 +02001583 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1584 p->context.oe = readl_relaxed(base + regs->direction);
1585 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1586 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1587 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1588 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1589 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1590 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1591 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001592
1593 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001594 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001595 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001596 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001597
1598 p->context_valid = true;
1599}
1600
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301601static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301602{
Victor Kamensky661553b2013-11-16 02:01:04 +02001603 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301604 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001605 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1606 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301607 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001608 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301609 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001610 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301611 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001612 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301613 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301614 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001615 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301616 bank->base + bank->regs->set_dataout);
1617 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001618 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301619 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001620 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301621
Nishanth Menonae547352011-09-09 19:08:58 +05301622 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001623 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301624 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001625 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301626 bank->base + bank->regs->debounce_en);
1627 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301628
Victor Kamensky661553b2013-11-16 02:01:04 +02001629 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301630 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001631 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301632 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301633}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001634#endif /* CONFIG_PM */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301635#else
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301636#define omap_gpio_runtime_suspend NULL
1637#define omap_gpio_runtime_resume NULL
Arnd Bergmannea4a21a2013-05-31 17:59:46 +02001638static inline void omap_gpio_init_context(struct gpio_bank *p) {}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301639#endif
1640
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301641static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301642 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1643 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301644};
1645
Benoit Cousson384ebe12011-08-16 11:53:02 +02001646#if defined(CONFIG_OF)
1647static struct omap_gpio_reg_offs omap2_gpio_regs = {
1648 .revision = OMAP24XX_GPIO_REVISION,
1649 .direction = OMAP24XX_GPIO_OE,
1650 .datain = OMAP24XX_GPIO_DATAIN,
1651 .dataout = OMAP24XX_GPIO_DATAOUT,
1652 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1653 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1654 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1655 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1656 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1657 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1658 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1659 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1660 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1661 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1662 .ctrl = OMAP24XX_GPIO_CTRL,
1663 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1664 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1665 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1666 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1667 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1668};
1669
1670static struct omap_gpio_reg_offs omap4_gpio_regs = {
1671 .revision = OMAP4_GPIO_REVISION,
1672 .direction = OMAP4_GPIO_OE,
1673 .datain = OMAP4_GPIO_DATAIN,
1674 .dataout = OMAP4_GPIO_DATAOUT,
1675 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1676 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1677 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1678 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1679 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1680 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1681 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1682 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1683 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1684 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1685 .ctrl = OMAP4_GPIO_CTRL,
1686 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1687 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1688 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1689 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1690 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1691};
1692
Chen Gange9a65bb2013-02-06 18:44:32 +08001693static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001694 .regs = &omap2_gpio_regs,
1695 .bank_width = 32,
1696 .dbck_flag = false,
1697};
1698
Chen Gange9a65bb2013-02-06 18:44:32 +08001699static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001700 .regs = &omap2_gpio_regs,
1701 .bank_width = 32,
1702 .dbck_flag = true,
1703};
1704
Chen Gange9a65bb2013-02-06 18:44:32 +08001705static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001706 .regs = &omap4_gpio_regs,
1707 .bank_width = 32,
1708 .dbck_flag = true,
1709};
1710
1711static const struct of_device_id omap_gpio_match[] = {
1712 {
1713 .compatible = "ti,omap4-gpio",
1714 .data = &omap4_pdata,
1715 },
1716 {
1717 .compatible = "ti,omap3-gpio",
1718 .data = &omap3_pdata,
1719 },
1720 {
1721 .compatible = "ti,omap2-gpio",
1722 .data = &omap2_pdata,
1723 },
1724 { },
1725};
1726MODULE_DEVICE_TABLE(of, omap_gpio_match);
1727#endif
1728
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001729static struct platform_driver omap_gpio_driver = {
1730 .probe = omap_gpio_probe,
Tony Lindgrencac089f2015-04-23 16:56:22 -07001731 .remove = omap_gpio_remove,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001732 .driver = {
1733 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301734 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001735 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001736 },
1737};
1738
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001739/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001740 * gpio driver register needs to be done before
1741 * machine_init functions access gpio APIs.
1742 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001743 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001744static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001745{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001746 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001747}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001748postcore_initcall(omap_gpio_drv_reg);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001749
1750static void __exit omap_gpio_exit(void)
1751{
1752 platform_driver_unregister(&omap_gpio_driver);
1753}
1754module_exit(omap_gpio_exit);
1755
1756MODULE_DESCRIPTION("omap gpio driver");
1757MODULE_ALIAS("platform:gpio-omap");
1758MODULE_LICENSE("GPL v2");