blob: a90e27d7ce5e561596e4061b68bbc7989e874a2e [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Tony Lindgrenb764a582018-09-20 12:35:31 -070022#include <linux/cpu_pm.h>
Benoit Cousson96751fc2012-02-01 16:01:39 +010023#include <linux/device.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080024#include <linux/pm_runtime.h>
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +053025#include <linux/pm.h>
Benoit Cousson384ebe12011-08-16 11:53:02 +020026#include <linux/of.h>
27#include <linux/of_device.h>
Linus Walleijb7351b02018-05-24 14:24:00 +020028#include <linux/gpio/driver.h>
Yegor Yefremov93700842014-04-24 08:57:39 +020029#include <linux/bitops.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070030#include <linux/platform_data/gpio-omap.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +030032#define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053033
Charulatha V6d62e212011-04-18 15:06:51 +000034struct gpio_regs {
35 u32 irqenable1;
36 u32 irqenable2;
37 u32 wake_en;
38 u32 ctrl;
39 u32 oe;
40 u32 leveldetect0;
41 u32 leveldetect1;
42 u32 risingdetect;
43 u32 fallingdetect;
44 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053045 u32 debounce;
46 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000047};
48
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010049struct gpio_bank {
Tony Lindgren92105bb2005-09-07 17:20:26 +010050 void __iomem *base;
Grygorii Strashko30cefea2015-09-25 12:06:02 -070051 int irq;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080052 u32 non_wakeup_gpios;
53 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000054 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080055 u32 saved_datain;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080056 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080057 u32 toggle_mask;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +020058 raw_spinlock_t lock;
Grygorii Strashko450fa542015-09-25 12:28:03 -070059 raw_spinlock_t wa_lock;
David Brownell52e31342008-03-03 12:43:23 -080060 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080061 struct clk *dbck;
Tony Lindgrenb764a582018-09-20 12:35:31 -070062 struct notifier_block nb;
63 unsigned int is_suspended:1;
Charulatha V058af1e2009-11-22 10:11:25 -080064 u32 mod_usage;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020065 u32 irq_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080066 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +053067 bool dbck_enabled;
Charulatha Vd0d665a2011-08-31 00:02:21 +053068 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080069 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053070 bool loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -050071 bool context_valid;
Tony Lindgren5de62b82010-12-07 16:26:58 -080072 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070073 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053074 int context_loss_count;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070075
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +020076 void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053077 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070078
79 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010080};
81
Charulatha Vc8eef652011-05-02 15:21:42 +053082#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010083
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020084#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020085#define LINE_USED(line, offset) (line & (BIT(offset)))
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020086
Tony Lindgren3d009c82015-01-16 14:50:50 -080087static void omap_gpio_unmask_irq(struct irq_data *d);
88
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020089static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -060090{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +020091 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
Linus Walleijd99f7ae2015-12-07 11:16:00 +010092 return gpiochip_get_data(chip);
Benoit Cousson25db7112012-02-23 21:50:10 +010093}
94
Russell King8ee1de62019-06-10 20:10:55 +030095static inline u32 omap_gpio_rmw(void __iomem *reg, u32 mask, bool set)
96{
97 u32 val = readl_relaxed(reg);
98
99 if (set)
100 val |= mask;
101 else
102 val &= ~mask;
103
104 writel_relaxed(val, reg);
105
106 return val;
107}
108
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200109static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
110 int is_input)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100111{
Russell King8ee1de62019-06-10 20:10:55 +0300112 bank->context.oe = omap_gpio_rmw(bank->base + bank->regs->direction,
113 BIT(gpio), is_input);
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{
Russell King8ee1de62019-06-10 20:10:55 +0300139 bank->context.dataout = omap_gpio_rmw(bank->base + bank->regs->dataout,
140 BIT(offset), enable);
Kevin Hilmanece95282011-07-12 08:18:15 -0700141}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100142
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200143static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530144{
145 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300146 clk_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530147 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300148
Victor Kamensky661553b2013-11-16 02:01:04 +0200149 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300150 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530151 }
152}
153
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200154static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530155{
156 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300157 /*
158 * Disable debounce before cutting it's clock. If debounce is
159 * enabled but the clock is not, GPIO module seems to be unable
160 * to detect events and generate interrupts at least on OMAP3.
161 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200162 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300163
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300164 clk_disable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530165 bank->dbck_enabled = false;
166 }
167}
168
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700169/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200170 * omap2_set_gpio_debounce - low level gpio debounce time
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700171 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200172 * @offset: the gpio number on this @bank
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700173 * @debounce: debounce time to use
174 *
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300175 * OMAP's debounce time is in 31us steps
176 * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
177 * so we need to convert and round up to the closest unit.
David Rivshin83977442017-04-24 18:56:50 -0400178 *
179 * Return: 0 on success, negative error otherwise.
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700180 */
David Rivshin83977442017-04-24 18:56:50 -0400181static int omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
182 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700183{
Kevin Hilman9942da02011-04-22 12:02:05 -0700184 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700185 u32 val;
186 u32 l;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300187 bool enable = !!debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700188
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800189 if (!bank->dbck_flag)
David Rivshin83977442017-04-24 18:56:50 -0400190 return -ENOTSUPP;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800191
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300192 if (enable) {
193 debounce = DIV_ROUND_UP(debounce, 31) - 1;
David Rivshin83977442017-04-24 18:56:50 -0400194 if ((debounce & OMAP4_GPIO_DEBOUNCINGTIME_MASK) != debounce)
195 return -EINVAL;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300196 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700197
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200198 l = BIT(offset);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700199
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300200 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700201 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200202 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700203
Russell King8ee1de62019-06-10 20:10:55 +0300204 val = omap_gpio_rmw(bank->base + bank->regs->debounce_en, l, enable);
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300205 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700206
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300207 clk_disable(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530208 /*
209 * Enable debounce clock per module.
210 * This call is mandatory because in omap_gpio_request() when
211 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
212 * runtime callbck fails to turn on dbck because dbck_enable_mask
213 * used within _gpio_dbck_enable() is still not initialized at
214 * that point. Therefore we have to enable dbck here.
215 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200216 omap_gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530217 if (bank->dbck_enable_mask) {
218 bank->context.debounce = debounce;
219 bank->context.debounce_en = val;
220 }
David Rivshin83977442017-04-24 18:56:50 -0400221
222 return 0;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700223}
224
Jon Hunterc9c55d92012-10-26 14:26:04 -0500225/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200226 * omap_clear_gpio_debounce - clear debounce settings for a gpio
Jon Hunterc9c55d92012-10-26 14:26:04 -0500227 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200228 * @offset: the gpio number on this @bank
Jon Hunterc9c55d92012-10-26 14:26:04 -0500229 *
230 * If a gpio is using debounce, then clear the debounce enable bit and if
231 * this is the only gpio in this bank using debounce, then clear the debounce
232 * time too. The debounce clock will also be disabled when calling this function
233 * if this is the only gpio in the bank using debounce.
234 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200235static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
Jon Hunterc9c55d92012-10-26 14:26:04 -0500236{
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200237 u32 gpio_bit = BIT(offset);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500238
239 if (!bank->dbck_flag)
240 return;
241
242 if (!(bank->dbck_enable_mask & gpio_bit))
243 return;
244
245 bank->dbck_enable_mask &= ~gpio_bit;
246 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200247 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500248 bank->base + bank->regs->debounce_en);
249
250 if (!bank->dbck_enable_mask) {
251 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200252 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500253 bank->regs->debounce);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300254 clk_disable(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500255 bank->dbck_enabled = false;
256 }
257}
258
Tony Lindgrenda38ef32019-03-25 15:43:18 -0700259/*
260 * Off mode wake-up capable GPIOs in bank(s) that are in the wakeup domain.
261 * See TRM section for GPIO for "Wake-Up Generation" for the list of GPIOs
262 * in wakeup domain. If bank->non_wakeup_gpios is not configured, assume none
263 * are capable waking up the system from off mode.
264 */
265static bool omap_gpio_is_off_wakeup_capable(struct gpio_bank *bank, u32 gpio_mask)
266{
267 u32 no_wake = bank->non_wakeup_gpios;
268
269 if (no_wake)
270 return !!(~no_wake & gpio_mask);
271
272 return false;
273}
274
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200275static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530276 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100277{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800278 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200279 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100280
Russell King8ee1de62019-06-10 20:10:55 +0300281 omap_gpio_rmw(base + bank->regs->leveldetect0, gpio_bit,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200282 trigger & IRQ_TYPE_LEVEL_LOW);
Russell King8ee1de62019-06-10 20:10:55 +0300283 omap_gpio_rmw(base + bank->regs->leveldetect1, gpio_bit,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200284 trigger & IRQ_TYPE_LEVEL_HIGH);
Russell Kinge6818d22019-04-08 12:46:53 -0700285
286 /*
287 * We need the edge detection enabled for to allow the GPIO block
288 * to be woken from idle state. Set the appropriate edge detection
289 * in addition to the level detection.
290 */
Russell King8ee1de62019-06-10 20:10:55 +0300291 omap_gpio_rmw(base + bank->regs->risingdetect, gpio_bit,
Russell Kinge6818d22019-04-08 12:46:53 -0700292 trigger & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH));
Russell King8ee1de62019-06-10 20:10:55 +0300293 omap_gpio_rmw(base + bank->regs->fallingdetect, gpio_bit,
Russell Kinge6818d22019-04-08 12:46:53 -0700294 trigger & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW));
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530295
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530296 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200297 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530298 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200299 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530300 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200301 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530302 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200303 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530304
Russell Kinga0e881e2019-06-10 20:10:54 +0300305 bank->level_mask = bank->context.leveldetect0 |
306 bank->context.leveldetect1;
307
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530308 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Russell King8ee1de62019-06-10 20:10:55 +0300309 omap_gpio_rmw(base + bank->regs->wkup_en, gpio_bit, trigger != 0);
Tony Lindgren00ded242018-12-07 11:08:29 -0800310 bank->context.wake_en =
311 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530312 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530313
Ambresh K55b220c2011-06-15 13:40:45 -0700314 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tony Lindgrenda38ef32019-03-25 15:43:18 -0700315 if (!bank->regs->irqctrl && !omap_gpio_is_off_wakeup_capable(bank, gpio)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000316 /*
317 * Log the edge gpio and manually trigger the IRQ
318 * after resume if the input level changes
319 * to avoid irq lost during PER RET/OFF mode
320 * Applies for omap2 non-wakeup gpio and all omap3 gpios
321 */
322 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800323 bank->enabled_non_wakeup_gpios |= gpio_bit;
324 else
325 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
326 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100327}
328
Cory Maccarrone4318f362010-01-08 10:29:04 -0800329/*
330 * This only applies to chips that can't do both rising and falling edge
331 * detection at once. For all other chips, this function is a noop.
332 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200333static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800334{
Russell Kinga47b9152019-06-10 20:10:56 +0300335 if (IS_ENABLED(CONFIG_ARCH_OMAP1) && bank->regs->irqctrl) {
336 void __iomem *reg = bank->base + bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800337
Russell Kinga47b9152019-06-10 20:10:56 +0300338 writel_relaxed(readl_relaxed(reg) ^ BIT(gpio), reg);
339 }
Cory Maccarrone4318f362010-01-08 10:29:04 -0800340}
341
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200342static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
343 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100344{
345 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530346 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100347 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530349 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200350 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530351 } else if (bank->regs->irqctrl) {
352 reg += bank->regs->irqctrl;
353
Victor Kamensky661553b2013-11-16 02:01:04 +0200354 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000355 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200356 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100357 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200358 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100359 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200360 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100361 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530362 return -EINVAL;
363
Victor Kamensky661553b2013-11-16 02:01:04 +0200364 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530365 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100366 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530367 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100368 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530369 reg += bank->regs->edgectrl1;
370
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100371 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200372 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100373 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100374 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100375 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100376 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200377 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530378
379 /* Enable wake-up during idle for dynamic tick */
Russell King8ee1de62019-06-10 20:10:55 +0300380 omap_gpio_rmw(base + bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530381 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200382 readl_relaxed(bank->base + bank->regs->wkup_en);
383 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100384 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100385 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100386}
387
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200388static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200389{
390 if (bank->regs->pinctrl) {
391 void __iomem *reg = bank->base + bank->regs->pinctrl;
392
393 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200394 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200395 }
396
397 if (bank->regs->ctrl && !BANK_USED(bank)) {
398 void __iomem *reg = bank->base + bank->regs->ctrl;
399 u32 ctrl;
400
Victor Kamensky661553b2013-11-16 02:01:04 +0200401 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200402 /* Module is enabled, clocks are not gated */
403 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200404 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200405 bank->context.ctrl = ctrl;
406 }
407}
408
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200409static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200410{
411 void __iomem *base = bank->base;
412
413 if (bank->regs->wkup_en &&
414 !LINE_USED(bank->mod_usage, offset) &&
415 !LINE_USED(bank->irq_usage, offset)) {
416 /* Disable wake-up during idle for dynamic tick */
Russell King8ee1de62019-06-10 20:10:55 +0300417 omap_gpio_rmw(base + bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200418 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200419 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200420 }
421
422 if (bank->regs->ctrl && !BANK_USED(bank)) {
423 void __iomem *reg = bank->base + bank->regs->ctrl;
424 u32 ctrl;
425
Victor Kamensky661553b2013-11-16 02:01:04 +0200426 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200427 /* Module is disabled, clocks are gated */
428 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200429 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200430 bank->context.ctrl = ctrl;
431 }
432}
433
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200434static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200435{
436 void __iomem *reg = bank->base + bank->regs->direction;
437
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200438 return readl_relaxed(reg) & BIT(offset);
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200439}
440
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200441static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
Tony Lindgren3d009c82015-01-16 14:50:50 -0800442{
443 if (!LINE_USED(bank->mod_usage, offset)) {
444 omap_enable_gpio_module(bank, offset);
445 omap_set_gpio_direction(bank, offset, 1);
446 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200447 bank->irq_usage |= BIT(offset);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800448}
449
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200450static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100451{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200452 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100453 int retval;
David Brownella6472532008-03-03 04:33:30 -0800454 unsigned long flags;
Grygorii Strashkoea5fbe82015-03-23 14:18:29 +0200455 unsigned offset = d->hwirq;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100456
David Brownelle5c56ed2006-12-06 17:13:59 -0800457 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100458 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800459
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530460 if (!bank->regs->leveldetect0 &&
461 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100462 return -EINVAL;
463
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200464 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200465 retval = omap_set_gpio_triggering(bank, offset, type);
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300466 if (retval) {
Axel Lin627c89b2015-08-05 22:37:41 +0800467 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300468 goto error;
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300469 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200470 omap_gpio_init_irq(bank, offset);
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200471 if (!omap_gpio_is_input(bank, offset)) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200472 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300473 retval = -EINVAL;
474 goto error;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200475 }
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200476 raw_spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800477
478 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner43ec2e42015-06-23 15:52:39 +0200479 irq_set_handler_locked(d, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800480 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500481 /*
482 * Edge IRQs are already cleared/acked in irq_handler and
483 * not need to be masked, as result handle_edge_irq()
484 * logic is excessed here and may cause lose of interrupts.
485 * So just use handle_simple_irq.
486 */
487 irq_set_handler_locked(d, handle_simple_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800488
Grygorii Strashko1562e462015-05-22 17:35:49 +0300489 return 0;
490
491error:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100492 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100493}
494
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200495static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100496{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100497 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100498
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700499 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200500 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300501
502 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700503 if (bank->regs->irqstatus2) {
504 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200505 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700506 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700507
508 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200509 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510}
511
Grygorii Strashko9943f262015-03-23 14:18:27 +0200512static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
513 unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100514{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200515 omap_clear_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100516}
517
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200518static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
Imre Deakea6dedd2006-06-26 16:16:00 -0700519{
520 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700521 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200522 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700523
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700524 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200525 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700526 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700527 l = ~l;
528 l &= mask;
529 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700530}
531
Grygorii Strashko9943f262015-03-23 14:18:27 +0200532static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
533 unsigned offset, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100534{
Russell King31b2d7f2019-06-10 20:10:57 +0300535 void __iomem *reg = bank->base;
536 u32 gpio_mask = BIT(offset);
537
538 if (bank->regs->set_irqenable && bank->regs->clr_irqenable) {
539 if (enable) {
540 reg += bank->regs->set_irqenable;
541 bank->context.irqenable1 |= gpio_mask;
542 } else {
543 reg += bank->regs->clr_irqenable;
544 bank->context.irqenable1 &= ~gpio_mask;
545 }
546 writel_relaxed(gpio_mask, reg);
547 } else {
548 bank->context.irqenable1 =
549 omap_gpio_rmw(reg + bank->regs->irqenable, gpio_mask,
550 enable ^ bank->regs->irqenable_inv);
551 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100552}
553
Tony Lindgren92105bb2005-09-07 17:20:26 +0100554/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200555static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100556{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200557 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100558
Grygorii Strashko0c0451e2016-04-12 13:52:31 +0300559 return irq_set_irq_wake(bank->irq, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100560}
561
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100562/*
563 * We need to unmask the GPIO bank interrupt as soon as possible to
564 * avoid missing GPIO interrupts for other lines in the bank.
565 * Then we need to mask-read-clear-unmask the triggered GPIO lines
566 * in the bank to avoid missing nested interrupts for a GPIO line.
567 * If we wait to unmask individual GPIO lines in the bank after the
568 * line's interrupt handler has been run, we may miss some nested
569 * interrupts.
570 */
Grygorii Strashko450fa542015-09-25 12:28:03 -0700571static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100572{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100573 void __iomem *isr_reg = NULL;
Russell King395373c2019-06-10 20:10:47 +0300574 u32 enabled, isr, edge;
Jon Hunter3513cde2013-04-04 15:16:14 -0500575 unsigned int bit;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700576 struct gpio_bank *bank = gpiobank;
577 unsigned long wa_lock_flags;
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300578 unsigned long lock_flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100579
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700580 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800581 if (WARN_ON(!isr_reg))
582 goto exit;
583
Tony Lindgren52845212018-09-20 12:35:32 -0700584 if (WARN_ONCE(!pm_runtime_active(bank->chip.parent),
585 "gpio irq%i while runtime suspended?\n", irq))
586 return IRQ_NONE;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700587
Laurent Navete83507b2013-03-20 13:15:57 +0100588 while (1) {
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300589 raw_spin_lock_irqsave(&bank->lock, lock_flags);
590
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200591 enabled = omap_get_gpio_irqbank_mask(bank);
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500592 isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100593
Russell King395373c2019-06-10 20:10:47 +0300594 /*
595 * Clear edge sensitive interrupts before calling handler(s)
596 * so subsequent edge transitions are not missed while the
597 * handlers are running.
598 */
599 edge = isr & ~bank->level_mask;
600 if (edge)
601 omap_clear_gpio_irqbank(bank, edge);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100602
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300603 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
604
Tony Lindgren92105bb2005-09-07 17:20:26 +0100605 if (!isr)
606 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100607
Jon Hunter3513cde2013-04-04 15:16:14 -0500608 while (isr) {
609 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200610 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100611
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300612 raw_spin_lock_irqsave(&bank->lock, lock_flags);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800613 /*
614 * Some chips can't respond to both rising and falling
615 * at the same time. If this irq was requested with
616 * both flags, we need to flip the ICR data for the IRQ
617 * to respond to the IRQ for the opposite direction.
618 * This will be indicated in the bank toggle_mask.
619 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200620 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200621 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800622
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300623 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
624
Grygorii Strashko450fa542015-09-25 12:28:03 -0700625 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
626
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100627 generic_handle_irq(irq_find_mapping(bank->chip.irq.domain,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200628 bit));
Grygorii Strashko450fa542015-09-25 12:28:03 -0700629
630 raw_spin_unlock_irqrestore(&bank->wa_lock,
631 wa_lock_flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100632 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000633 }
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800634exit:
Grygorii Strashko450fa542015-09-25 12:28:03 -0700635 return IRQ_HANDLED;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100636}
637
Tony Lindgren3d009c82015-01-16 14:50:50 -0800638static unsigned int omap_gpio_irq_startup(struct irq_data *d)
639{
640 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800641 unsigned long flags;
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200642 unsigned offset = d->hwirq;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800643
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200644 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300645
646 if (!LINE_USED(bank->mod_usage, offset))
647 omap_set_gpio_direction(bank, offset, 1);
648 else if (!omap_gpio_is_input(bank, offset))
649 goto err;
650 omap_enable_gpio_module(bank, offset);
651 bank->irq_usage |= BIT(offset);
652
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200653 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800654 omap_gpio_unmask_irq(d);
655
656 return 0;
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300657err:
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200658 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300659 return -EINVAL;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800660}
661
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200662static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300663{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200664 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700665 unsigned long flags;
Grygorii Strashko9943f262015-03-23 14:18:27 +0200666 unsigned offset = d->hwirq;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300667
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200668 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200669 bank->irq_usage &= ~(BIT(offset));
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300670 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Russell Kingc859e0d2019-06-10 20:10:44 +0300671 omap_clear_gpio_irqstatus(bank, offset);
672 omap_set_gpio_irqenable(bank, offset, 0);
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300673 if (!LINE_USED(bank->mod_usage, offset))
674 omap_clear_gpio_debounce(bank, offset);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200675 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200676 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700677}
678
679static void omap_gpio_irq_bus_lock(struct irq_data *data)
680{
681 struct gpio_bank *bank = omap_irq_data_get_bank(data);
682
Grygorii Strashko46748072018-09-28 16:39:50 -0500683 pm_runtime_get_sync(bank->chip.parent);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700684}
685
686static void gpio_irq_bus_sync_unlock(struct irq_data *data)
687{
688 struct gpio_bank *bank = omap_irq_data_get_bank(data);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200689
Grygorii Strashko46748072018-09-28 16:39:50 -0500690 pm_runtime_put(bank->chip.parent);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300691}
692
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200693static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100694{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200695 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200696 unsigned offset = d->hwirq;
Colin Cross85ec7b92011-06-06 13:38:18 -0700697 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100698
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200699 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200700 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Russell Kingc859e0d2019-06-10 20:10:44 +0300701 omap_set_gpio_irqenable(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200702 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100703}
704
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200705static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100706{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200707 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200708 unsigned offset = d->hwirq;
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100709 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700710 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700711
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200712 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200713 omap_set_gpio_irqenable(bank, offset, 1);
Russell Kingd01849f2019-03-01 11:02:52 -0800714
715 /*
716 * For level-triggered GPIOs, clearing must be done after the source
717 * is cleared, thus after the handler has run. OMAP4 needs this done
718 * after enabing the interrupt to clear the wakeup status.
719 */
Russell Kingc859e0d2019-06-10 20:10:44 +0300720 if (bank->regs->leveldetect0 && bank->regs->wkup_en &&
721 trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
Russell Kingd01849f2019-03-01 11:02:52 -0800722 omap_clear_gpio_irqstatus(bank, offset);
723
Russell Kingc859e0d2019-06-10 20:10:44 +0300724 if (trigger)
725 omap_set_gpio_triggering(bank, offset, trigger);
726
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200727 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100728}
729
David Brownelle5c56ed2006-12-06 17:13:59 -0800730/*---------------------------------------------------------------------*/
731
Magnus Damm79ee0312009-07-08 13:22:04 +0200732static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800733{
Wolfram Sanga3f4f722018-10-21 21:59:59 +0200734 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800735 void __iomem *mask_reg = bank->base +
736 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800737 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800738
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200739 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200740 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200741 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800742
743 return 0;
744}
745
Magnus Damm79ee0312009-07-08 13:22:04 +0200746static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800747{
Wolfram Sanga3f4f722018-10-21 21:59:59 +0200748 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800749 void __iomem *mask_reg = bank->base +
750 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800751 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800752
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200753 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200754 writel_relaxed(bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200755 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800756
757 return 0;
758}
759
Alexey Dobriyan47145212009-12-14 18:00:08 -0800760static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200761 .suspend_noirq = omap_mpuio_suspend_noirq,
762 .resume_noirq = omap_mpuio_resume_noirq,
763};
764
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200765/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800766static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800767 .driver = {
768 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200769 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800770 },
771};
772
773static struct platform_device omap_mpuio_device = {
774 .name = "mpuio",
775 .id = -1,
776 .dev = {
777 .driver = &omap_mpuio_driver.driver,
778 }
779 /* could list the /proc/iomem resources */
780};
781
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200782static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800783{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800784 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700785
David Brownell11a78b72006-12-06 17:14:11 -0800786 if (platform_driver_register(&omap_mpuio_driver) == 0)
787 (void) platform_device_register(&omap_mpuio_device);
788}
789
David Brownelle5c56ed2006-12-06 17:13:59 -0800790/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100791
Russell Kingdfbc6c72019-06-10 20:10:49 +0300792static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
793{
794 struct gpio_bank *bank = gpiochip_get_data(chip);
795 unsigned long flags;
796
797 pm_runtime_get_sync(chip->parent);
798
799 raw_spin_lock_irqsave(&bank->lock, flags);
800 omap_enable_gpio_module(bank, offset);
801 bank->mod_usage |= BIT(offset);
802 raw_spin_unlock_irqrestore(&bank->lock, flags);
803
804 return 0;
805}
806
807static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
808{
809 struct gpio_bank *bank = gpiochip_get_data(chip);
810 unsigned long flags;
811
812 raw_spin_lock_irqsave(&bank->lock, flags);
813 bank->mod_usage &= ~(BIT(offset));
814 if (!LINE_USED(bank->irq_usage, offset)) {
815 omap_set_gpio_direction(bank, offset, 1);
816 omap_clear_gpio_debounce(bank, offset);
817 }
818 omap_disable_gpio_module(bank, offset);
819 raw_spin_unlock_irqrestore(&bank->lock, flags);
820
821 pm_runtime_put(chip->parent);
822}
823
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200824static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200825{
Russell King40bb2272019-06-10 20:10:50 +0300826 struct gpio_bank *bank = gpiochip_get_data(chip);
Yegor Yefremov93700842014-04-24 08:57:39 +0200827
Russell King40bb2272019-06-10 20:10:50 +0300828 return !!(readl_relaxed(bank->base + bank->regs->direction) &
829 BIT(offset));
Yegor Yefremov93700842014-04-24 08:57:39 +0200830}
831
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200832static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800833{
834 struct gpio_bank *bank;
835 unsigned long flags;
836
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100837 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200838 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200839 omap_set_gpio_direction(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200840 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800841 return 0;
842}
843
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200844static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800845{
Russell King5ca5f922019-06-10 20:10:51 +0300846 struct gpio_bank *bank = gpiochip_get_data(chip);
847 void __iomem *reg;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300848
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200849 if (omap_gpio_is_input(bank, offset))
Russell King5ca5f922019-06-10 20:10:51 +0300850 reg = bank->base + bank->regs->datain;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300851 else
Russell King5ca5f922019-06-10 20:10:51 +0300852 reg = bank->base + bank->regs->dataout;
853
854 return (readl_relaxed(reg) & BIT(offset)) != 0;
David Brownell52e31342008-03-03 12:43:23 -0800855}
856
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200857static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800858{
859 struct gpio_bank *bank;
860 unsigned long flags;
861
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100862 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200863 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700864 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200865 omap_set_gpio_direction(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200866 raw_spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200867 return 0;
David Brownell52e31342008-03-03 12:43:23 -0800868}
869
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200870static int omap_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
871 unsigned long *bits)
872{
873 struct gpio_bank *bank = gpiochip_get_data(chip);
Russell King6653dd82019-06-10 20:10:52 +0300874 void __iomem *base = bank->base;
875 u32 direction, m, val = 0;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200876
Russell King6653dd82019-06-10 20:10:52 +0300877 direction = readl_relaxed(base + bank->regs->direction);
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200878
Russell King6653dd82019-06-10 20:10:52 +0300879 m = direction & *mask;
880 if (m)
881 val |= readl_relaxed(base + bank->regs->datain) & m;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200882
Russell King6653dd82019-06-10 20:10:52 +0300883 m = ~direction & *mask;
884 if (m)
885 val |= readl_relaxed(base + bank->regs->dataout) & m;
886
887 *bits = val;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200888
889 return 0;
890}
891
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200892static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
893 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700894{
895 struct gpio_bank *bank;
896 unsigned long flags;
David Rivshin83977442017-04-24 18:56:50 -0400897 int ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700898
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100899 bank = gpiochip_get_data(chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800900
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200901 raw_spin_lock_irqsave(&bank->lock, flags);
David Rivshin83977442017-04-24 18:56:50 -0400902 ret = omap2_set_gpio_debounce(bank, offset, debounce);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200903 raw_spin_unlock_irqrestore(&bank->lock, flags);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700904
David Rivshin83977442017-04-24 18:56:50 -0400905 if (ret)
906 dev_info(chip->parent,
907 "Could not set line %u debounce to %u microseconds (%d)",
908 offset, debounce, ret);
909
910 return ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700911}
912
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300913static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset,
914 unsigned long config)
915{
916 u32 debounce;
917
918 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
919 return -ENOTSUPP;
920
921 debounce = pinconf_to_config_argument(config);
922 return omap_gpio_debounce(chip, offset, debounce);
923}
924
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200925static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800926{
927 struct gpio_bank *bank;
928 unsigned long flags;
929
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100930 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200931 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700932 bank->set_dataout(bank, offset, value);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200933 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800934}
935
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200936static void omap_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
937 unsigned long *bits)
938{
939 struct gpio_bank *bank = gpiochip_get_data(chip);
Russell King8ba70592019-06-10 20:10:53 +0300940 void __iomem *reg = bank->base + bank->regs->dataout;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200941 unsigned long flags;
Russell King8ba70592019-06-10 20:10:53 +0300942 u32 l;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200943
944 raw_spin_lock_irqsave(&bank->lock, flags);
Russell King8ba70592019-06-10 20:10:53 +0300945 l = (readl_relaxed(reg) & ~*mask) | (*bits & *mask);
946 writel_relaxed(l, reg);
947 bank->context.dataout = l;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200948 raw_spin_unlock_irqrestore(&bank->lock, flags);
949}
950
David Brownell52e31342008-03-03 12:43:23 -0800951/*---------------------------------------------------------------------*/
952
Arnd Bergmanne4b2ae72017-09-16 22:42:21 +0200953static void omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700954{
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700955 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700956 u32 rev;
957
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700958 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700959 return;
960
Victor Kamensky661553b2013-11-16 02:01:04 +0200961 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700962 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700963 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -0700964
965 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700966}
967
Charulatha V03e128c2011-05-05 19:58:01 +0530968static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800969{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530970 void __iomem *base = bank->base;
971 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800972
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530973 if (bank->width == 16)
974 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800975
Charulatha Vd0d665a2011-08-31 00:02:21 +0530976 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +0200977 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530978 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800979 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530980
Russell King8ee1de62019-06-10 20:10:55 +0300981 omap_gpio_rmw(base + bank->regs->irqenable, l,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200982 bank->regs->irqenable_inv);
Russell King8ee1de62019-06-10 20:10:55 +0300983 omap_gpio_rmw(base + bank->regs->irqstatus, l,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200984 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530985 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +0200986 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530987
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +0530988 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +0200989 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +0530990 /* Initialize interface clk ungated, module enabled */
991 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +0200992 writel_relaxed(0, base + bank->regs->ctrl);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800993}
994
Nishanth Menon46824e222014-09-05 14:52:55 -0500995static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800996{
Grygorii Strashko81930322017-11-15 12:36:33 -0600997 struct gpio_irq_chip *irq;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -0800998 static int gpio;
Linus Walleij088413b2017-12-29 13:22:58 +0100999 const char *label;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001000 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001001 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001002
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001003 /*
1004 * REVISIT eventually switch from OMAP-specific gpio structs
1005 * over to the generic ones
1006 */
1007 bank->chip.request = omap_gpio_request;
1008 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001009 bank->chip.get_direction = omap_gpio_get_direction;
1010 bank->chip.direction_input = omap_gpio_input;
1011 bank->chip.get = omap_gpio_get;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001012 bank->chip.get_multiple = omap_gpio_get_multiple;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001013 bank->chip.direction_output = omap_gpio_output;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001014 bank->chip.set_config = omap_gpio_set_config;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001015 bank->chip.set = omap_gpio_set;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001016 bank->chip.set_multiple = omap_gpio_set_multiple;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301017 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001018 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301019 if (bank->regs->wkup_en)
Linus Walleij58383c782015-11-04 09:56:26 +01001020 bank->chip.parent = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001021 bank->chip.base = OMAP_MPUIO(0);
1022 } else {
Linus Walleij088413b2017-12-29 13:22:58 +01001023 label = devm_kasprintf(bank->chip.parent, GFP_KERNEL, "gpio-%d-%d",
1024 gpio, gpio + bank->width - 1);
1025 if (!label)
1026 return -ENOMEM;
1027 bank->chip.label = label;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001028 bank->chip.base = gpio;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001029 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001030 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001031
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001032#ifdef CONFIG_ARCH_OMAP1
1033 /*
1034 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1035 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1036 */
Bartosz Golaszewski2ed36f32017-03-04 17:23:31 +01001037 irq_base = devm_irq_alloc_descs(bank->chip.parent,
1038 -1, 0, bank->width, 0);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001039 if (irq_base < 0) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001040 dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n");
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001041 return -ENODEV;
1042 }
1043#endif
1044
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001045 /* MPUIO is a bit different, reading IRQ status clears it */
Russell King693de832019-06-10 20:10:48 +03001046 if (bank->is_mpuio && !bank->regs->wkup_en)
1047 irqc->irq_set_wake = NULL;
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001048
Grygorii Strashko81930322017-11-15 12:36:33 -06001049 irq = &bank->chip.irq;
1050 irq->chip = irqc;
1051 irq->handler = handle_bad_irq;
1052 irq->default_type = IRQ_TYPE_NONE;
1053 irq->num_parents = 1;
1054 irq->parents = &bank->irq;
1055 irq->first = irq_base;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001056
Grygorii Strashko81930322017-11-15 12:36:33 -06001057 ret = gpiochip_add_data(&bank->chip, bank);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001058 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001059 dev_err(bank->chip.parent,
Grygorii Strashko81930322017-11-15 12:36:33 -06001060 "Could not register gpio chip %d\n", ret);
1061 return ret;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001062 }
1063
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001064 ret = devm_request_irq(bank->chip.parent, bank->irq,
1065 omap_gpio_irq_handler,
1066 0, dev_name(bank->chip.parent), bank);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001067 if (ret)
1068 gpiochip_remove(&bank->chip);
1069
Grygorii Strashko81930322017-11-15 12:36:33 -06001070 if (!bank->is_mpuio)
1071 gpio += bank->width;
1072
Grygorii Strashko450fa542015-09-25 12:28:03 -07001073 return ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001074}
1075
Arnd Bergmann7c685712019-03-07 11:33:32 +01001076static void omap_gpio_init_context(struct gpio_bank *p)
1077{
1078 struct omap_gpio_reg_offs *regs = p->regs;
1079 void __iomem *base = p->base;
1080
1081 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1082 p->context.oe = readl_relaxed(base + regs->direction);
1083 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1084 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1085 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1086 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1087 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1088 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1089 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
1090
1091 if (regs->set_dataout && p->regs->clr_dataout)
1092 p->context.dataout = readl_relaxed(base + regs->set_dataout);
1093 else
1094 p->context.dataout = readl_relaxed(base + regs->dataout);
1095
1096 p->context_valid = true;
1097}
1098
1099static void omap_gpio_restore_context(struct gpio_bank *bank)
1100{
1101 writel_relaxed(bank->context.wake_en,
1102 bank->base + bank->regs->wkup_en);
1103 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1104 writel_relaxed(bank->context.leveldetect0,
1105 bank->base + bank->regs->leveldetect0);
1106 writel_relaxed(bank->context.leveldetect1,
1107 bank->base + bank->regs->leveldetect1);
1108 writel_relaxed(bank->context.risingdetect,
1109 bank->base + bank->regs->risingdetect);
1110 writel_relaxed(bank->context.fallingdetect,
1111 bank->base + bank->regs->fallingdetect);
1112 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1113 writel_relaxed(bank->context.dataout,
1114 bank->base + bank->regs->set_dataout);
1115 else
1116 writel_relaxed(bank->context.dataout,
1117 bank->base + bank->regs->dataout);
1118 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
1119
1120 if (bank->dbck_enable_mask) {
1121 writel_relaxed(bank->context.debounce, bank->base +
1122 bank->regs->debounce);
1123 writel_relaxed(bank->context.debounce_en,
1124 bank->base + bank->regs->debounce_en);
1125 }
1126
1127 writel_relaxed(bank->context.irqenable1,
1128 bank->base + bank->regs->irqenable);
1129 writel_relaxed(bank->context.irqenable2,
1130 bank->base + bank->regs->irqenable2);
1131}
1132
1133static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context)
1134{
1135 struct device *dev = bank->chip.parent;
Tony Lindgren21e21182019-03-25 15:43:16 -07001136 void __iomem *base = bank->base;
1137 u32 nowake;
1138
1139 bank->saved_datain = readl_relaxed(base + bank->regs->datain);
Arnd Bergmann7c685712019-03-07 11:33:32 +01001140
Arnd Bergmann7c685712019-03-07 11:33:32 +01001141 if (!bank->enabled_non_wakeup_gpios)
1142 goto update_gpio_context_count;
1143
1144 if (!may_lose_context)
1145 goto update_gpio_context_count;
1146
1147 /*
Tony Lindgren21e21182019-03-25 15:43:16 -07001148 * If going to OFF, remove triggering for all wkup domain
Arnd Bergmann7c685712019-03-07 11:33:32 +01001149 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1150 * generated. See OMAP2420 Errata item 1.101.
1151 */
Tony Lindgren21e21182019-03-25 15:43:16 -07001152 if (!bank->loses_context && bank->enabled_non_wakeup_gpios) {
1153 nowake = bank->enabled_non_wakeup_gpios;
Russell King8ee1de62019-06-10 20:10:55 +03001154 omap_gpio_rmw(base + bank->regs->fallingdetect, nowake, ~nowake);
1155 omap_gpio_rmw(base + bank->regs->risingdetect, nowake, ~nowake);
Tony Lindgren21e21182019-03-25 15:43:16 -07001156 }
Arnd Bergmann7c685712019-03-07 11:33:32 +01001157
1158update_gpio_context_count:
1159 if (bank->get_context_loss_count)
1160 bank->context_loss_count =
1161 bank->get_context_loss_count(dev);
1162
1163 omap_gpio_dbck_disable(bank);
1164}
1165
1166static void omap_gpio_unidle(struct gpio_bank *bank)
1167{
1168 struct device *dev = bank->chip.parent;
1169 u32 l = 0, gen, gen0, gen1;
1170 int c;
1171
1172 /*
1173 * On the first resume during the probe, the context has not
1174 * been initialised and so initialise it now. Also initialise
1175 * the context loss count.
1176 */
1177 if (bank->loses_context && !bank->context_valid) {
1178 omap_gpio_init_context(bank);
1179
1180 if (bank->get_context_loss_count)
1181 bank->context_loss_count =
1182 bank->get_context_loss_count(dev);
1183 }
1184
1185 omap_gpio_dbck_enable(bank);
1186
Arnd Bergmann7c685712019-03-07 11:33:32 +01001187 if (bank->loses_context) {
1188 if (!bank->get_context_loss_count) {
1189 omap_gpio_restore_context(bank);
1190 } else {
1191 c = bank->get_context_loss_count(dev);
1192 if (c != bank->context_loss_count) {
1193 omap_gpio_restore_context(bank);
1194 } else {
1195 return;
1196 }
1197 }
Tony Lindgren21e21182019-03-25 15:43:16 -07001198 } else {
1199 /* Restore changes done for OMAP2420 errata 1.101 */
1200 writel_relaxed(bank->context.fallingdetect,
1201 bank->base + bank->regs->fallingdetect);
1202 writel_relaxed(bank->context.risingdetect,
1203 bank->base + bank->regs->risingdetect);
Arnd Bergmann7c685712019-03-07 11:33:32 +01001204 }
1205
Arnd Bergmann7c685712019-03-07 11:33:32 +01001206 l = readl_relaxed(bank->base + bank->regs->datain);
1207
1208 /*
1209 * Check if any of the non-wakeup interrupt GPIOs have changed
1210 * state. If so, generate an IRQ by software. This is
1211 * horribly racy, but it's the best we can do to work around
1212 * this silicon bug.
1213 */
1214 l ^= bank->saved_datain;
1215 l &= bank->enabled_non_wakeup_gpios;
1216
1217 /*
1218 * No need to generate IRQs for the rising edge for gpio IRQs
1219 * configured with falling edge only; and vice versa.
1220 */
1221 gen0 = l & bank->context.fallingdetect;
1222 gen0 &= bank->saved_datain;
1223
1224 gen1 = l & bank->context.risingdetect;
1225 gen1 &= ~(bank->saved_datain);
1226
1227 /* FIXME: Consider GPIO IRQs with level detections properly! */
1228 gen = l & (~(bank->context.fallingdetect) &
1229 ~(bank->context.risingdetect));
1230 /* Consider all GPIO IRQs needed to be updated */
1231 gen |= gen0 | gen1;
1232
1233 if (gen) {
1234 u32 old0, old1;
1235
1236 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1237 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
1238
1239 if (!bank->regs->irqstatus_raw0) {
1240 writel_relaxed(old0 | gen, bank->base +
1241 bank->regs->leveldetect0);
1242 writel_relaxed(old1 | gen, bank->base +
1243 bank->regs->leveldetect1);
1244 }
1245
1246 if (bank->regs->irqstatus_raw0) {
1247 writel_relaxed(old0 | l, bank->base +
1248 bank->regs->leveldetect0);
1249 writel_relaxed(old1 | l, bank->base +
1250 bank->regs->leveldetect1);
1251 }
1252 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1253 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
1254 }
Arnd Bergmann7c685712019-03-07 11:33:32 +01001255}
Tony Lindgrenb764a582018-09-20 12:35:31 -07001256
1257static int gpio_omap_cpu_notifier(struct notifier_block *nb,
1258 unsigned long cmd, void *v)
1259{
1260 struct gpio_bank *bank;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001261 unsigned long flags;
1262
1263 bank = container_of(nb, struct gpio_bank, nb);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001264
1265 raw_spin_lock_irqsave(&bank->lock, flags);
1266 switch (cmd) {
1267 case CPU_CLUSTER_PM_ENTER:
1268 if (bank->is_suspended)
1269 break;
1270 omap_gpio_idle(bank, true);
1271 break;
1272 case CPU_CLUSTER_PM_ENTER_FAILED:
1273 case CPU_CLUSTER_PM_EXIT:
1274 if (bank->is_suspended)
1275 break;
1276 omap_gpio_unidle(bank);
1277 break;
1278 }
1279 raw_spin_unlock_irqrestore(&bank->lock, flags);
1280
1281 return NOTIFY_OK;
1282}
1283
Arnd Bergmann7c685712019-03-07 11:33:32 +01001284static struct omap_gpio_reg_offs omap2_gpio_regs = {
1285 .revision = OMAP24XX_GPIO_REVISION,
1286 .direction = OMAP24XX_GPIO_OE,
1287 .datain = OMAP24XX_GPIO_DATAIN,
1288 .dataout = OMAP24XX_GPIO_DATAOUT,
1289 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1290 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1291 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1292 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1293 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1294 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1295 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1296 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1297 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1298 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1299 .ctrl = OMAP24XX_GPIO_CTRL,
1300 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1301 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1302 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1303 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1304 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1305};
1306
1307static struct omap_gpio_reg_offs omap4_gpio_regs = {
1308 .revision = OMAP4_GPIO_REVISION,
1309 .direction = OMAP4_GPIO_OE,
1310 .datain = OMAP4_GPIO_DATAIN,
1311 .dataout = OMAP4_GPIO_DATAOUT,
1312 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1313 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1314 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1315 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
Russell King64ea3e92019-06-10 20:10:45 +03001316 .irqstatus_raw0 = OMAP4_GPIO_IRQSTATUSRAW0,
1317 .irqstatus_raw1 = OMAP4_GPIO_IRQSTATUSRAW1,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001318 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1319 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1320 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1321 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1322 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1323 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1324 .ctrl = OMAP4_GPIO_CTRL,
1325 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1326 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1327 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1328 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1329 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1330};
1331
Arnd Bergmann7c685712019-03-07 11:33:32 +01001332static const struct omap_gpio_platform_data omap2_pdata = {
1333 .regs = &omap2_gpio_regs,
1334 .bank_width = 32,
1335 .dbck_flag = false,
1336};
1337
1338static const struct omap_gpio_platform_data omap3_pdata = {
1339 .regs = &omap2_gpio_regs,
1340 .bank_width = 32,
1341 .dbck_flag = true,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001342};
1343
1344static const struct omap_gpio_platform_data omap4_pdata = {
1345 .regs = &omap4_gpio_regs,
1346 .bank_width = 32,
1347 .dbck_flag = true,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001348};
1349
1350static const struct of_device_id omap_gpio_match[] = {
1351 {
1352 .compatible = "ti,omap4-gpio",
1353 .data = &omap4_pdata,
1354 },
1355 {
1356 .compatible = "ti,omap3-gpio",
1357 .data = &omap3_pdata,
1358 },
1359 {
1360 .compatible = "ti,omap2-gpio",
1361 .data = &omap2_pdata,
1362 },
1363 { },
1364};
1365MODULE_DEVICE_TABLE(of, omap_gpio_match);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001366
Bill Pemberton38363092012-11-19 13:22:34 -05001367static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001368{
Benoit Cousson862ff642012-02-01 15:58:56 +01001369 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001370 struct device_node *node = dev->of_node;
1371 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001372 const struct omap_gpio_platform_data *pdata;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001373 struct gpio_bank *bank;
Nishanth Menon46824e222014-09-05 14:52:55 -05001374 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001375 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001376
Benoit Cousson384ebe12011-08-16 11:53:02 +02001377 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1378
Jingoo Hane56aee12013-07-30 17:08:05 +09001379 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001380 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001381 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001382
Markus Elfringf97364c2018-02-10 21:49:22 +01001383 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
Markus Elfring9117d402018-02-10 21:46:30 +01001384 if (!bank)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001385 return -ENOMEM;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001386
Nishanth Menon46824e222014-09-05 14:52:55 -05001387 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1388 if (!irqc)
1389 return -ENOMEM;
1390
Tony Lindgren3d009c82015-01-16 14:50:50 -08001391 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e222014-09-05 14:52:55 -05001392 irqc->irq_shutdown = omap_gpio_irq_shutdown,
Russell King693de832019-06-10 20:10:48 +03001393 irqc->irq_ack = dummy_irq_chip.irq_ack,
Nishanth Menon46824e222014-09-05 14:52:55 -05001394 irqc->irq_mask = omap_gpio_mask_irq,
1395 irqc->irq_unmask = omap_gpio_unmask_irq,
1396 irqc->irq_set_type = omap_gpio_irq_type,
1397 irqc->irq_set_wake = omap_gpio_wake_enable,
Grygorii Strashkoaca82d12015-09-25 12:28:02 -07001398 irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
1399 irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
Nishanth Menon46824e222014-09-05 14:52:55 -05001400 irqc->name = dev_name(&pdev->dev);
Grygorii Strashko0c0451e2016-04-12 13:52:31 +03001401 irqc->flags = IRQCHIP_MASK_ON_SUSPEND;
Grygorii Strashko46748072018-09-28 16:39:50 -05001402 irqc->parent_device = dev;
Nishanth Menon46824e222014-09-05 14:52:55 -05001403
Grygorii Strashko89d18e32015-08-18 14:10:53 +03001404 bank->irq = platform_get_irq(pdev, 0);
1405 if (bank->irq <= 0) {
1406 if (!bank->irq)
1407 bank->irq = -ENXIO;
1408 if (bank->irq != -EPROBE_DEFER)
1409 dev_err(dev,
1410 "can't get irq resource ret=%d\n", bank->irq);
1411 return bank->irq;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001412 }
1413
Linus Walleij58383c782015-11-04 09:56:26 +01001414 bank->chip.parent = dev;
Grygorii Strashkoc23837c2015-06-25 18:13:33 +03001415 bank->chip.owner = THIS_MODULE;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001416 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001417 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001418 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301419 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301420 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001421 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001422#ifdef CONFIG_OF_GPIO
1423 bank->chip.of_node = of_node_get(node);
1424#endif
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001425
Jon Huntera2797be2013-04-04 15:16:15 -05001426 if (node) {
1427 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1428 bank->loses_context = true;
1429 } else {
1430 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001431
1432 if (bank->loses_context)
1433 bank->get_context_loss_count =
1434 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001435 }
1436
Russell King8ba70592019-06-10 20:10:53 +03001437 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001438 bank->set_dataout = omap_set_gpio_dataout_reg;
Russell King8ba70592019-06-10 20:10:53 +03001439 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001440 bank->set_dataout = omap_set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001441
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001442 raw_spin_lock_init(&bank->lock);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001443 raw_spin_lock_init(&bank->wa_lock);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001444
1445 /* Static mapping, never released */
Enrico Weigelt, metux IT consult58f57f82019-03-11 20:50:05 +01001446 bank->base = devm_platform_ioremap_resource(pdev, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001447 if (IS_ERR(bank->base)) {
Jingoo Han717f70e2014-02-12 11:51:38 +09001448 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001449 }
1450
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001451 if (bank->dbck_flag) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001452 bank->dbck = devm_clk_get(dev, "dbclk");
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001453 if (IS_ERR(bank->dbck)) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001454 dev_err(dev,
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001455 "Could not get gpio dbck. Disable debounce\n");
1456 bank->dbck_flag = false;
1457 } else {
1458 clk_prepare(bank->dbck);
1459 }
1460 }
1461
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301462 platform_set_drvdata(pdev, bank);
1463
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001464 pm_runtime_enable(dev);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001465 pm_runtime_get_sync(dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001466
Charulatha Vd0d665a2011-08-31 00:02:21 +05301467 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001468 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301469
Charulatha V03e128c2011-05-05 19:58:01 +05301470 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001471
Nishanth Menon46824e222014-09-05 14:52:55 -05001472 ret = omap_gpio_chip_init(bank, irqc);
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001473 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001474 pm_runtime_put_sync(dev);
1475 pm_runtime_disable(dev);
Arvind Yadave2c3c192017-08-01 12:14:31 +05301476 if (bank->dbck_flag)
1477 clk_unprepare(bank->dbck);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001478 return ret;
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001479 }
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001480
Tony Lindgren9a748052010-12-07 16:26:56 -08001481 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001482
Russell Kinge6818d22019-04-08 12:46:53 -07001483 bank->nb.notifier_call = gpio_omap_cpu_notifier;
1484 cpu_pm_register_notifier(&bank->nb);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001485
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001486 pm_runtime_put(dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301487
Jon Hunter879fe322013-04-04 15:16:12 -05001488 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001489}
1490
Tony Lindgrencac089f2015-04-23 16:56:22 -07001491static int omap_gpio_remove(struct platform_device *pdev)
1492{
1493 struct gpio_bank *bank = platform_get_drvdata(pdev);
1494
Russell Kinge6818d22019-04-08 12:46:53 -07001495 cpu_pm_unregister_notifier(&bank->nb);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001496 gpiochip_remove(&bank->chip);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001497 pm_runtime_disable(&pdev->dev);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001498 if (bank->dbck_flag)
1499 clk_unprepare(bank->dbck);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001500
1501 return 0;
1502}
1503
Tony Lindgrenb764a582018-09-20 12:35:31 -07001504static int __maybe_unused omap_gpio_runtime_suspend(struct device *dev)
1505{
Wolfram Sanga3f4f722018-10-21 21:59:59 +02001506 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001507 unsigned long flags;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001508
1509 raw_spin_lock_irqsave(&bank->lock, flags);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001510 omap_gpio_idle(bank, true);
1511 bank->is_suspended = true;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001512 raw_spin_unlock_irqrestore(&bank->lock, flags);
1513
Russell King044e4992019-04-10 12:51:13 -07001514 return 0;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001515}
1516
1517static int __maybe_unused omap_gpio_runtime_resume(struct device *dev)
1518{
Wolfram Sanga3f4f722018-10-21 21:59:59 +02001519 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001520 unsigned long flags;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001521
1522 raw_spin_lock_irqsave(&bank->lock, flags);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001523 omap_gpio_unidle(bank);
1524 bank->is_suspended = false;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001525 raw_spin_unlock_irqrestore(&bank->lock, flags);
1526
Russell King044e4992019-04-10 12:51:13 -07001527 return 0;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001528}
1529
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301530static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301531 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1532 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301533};
Benoit Cousson384ebe12011-08-16 11:53:02 +02001534
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001535static struct platform_driver omap_gpio_driver = {
1536 .probe = omap_gpio_probe,
Tony Lindgrencac089f2015-04-23 16:56:22 -07001537 .remove = omap_gpio_remove,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001538 .driver = {
1539 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301540 .pm = &gpio_pm_ops,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001541 .of_match_table = omap_gpio_match,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001542 },
1543};
1544
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001545/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001546 * gpio driver register needs to be done before
1547 * machine_init functions access gpio APIs.
1548 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001549 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001550static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001551{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001552 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001553}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001554postcore_initcall(omap_gpio_drv_reg);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001555
1556static void __exit omap_gpio_exit(void)
1557{
1558 platform_driver_unregister(&omap_gpio_driver);
1559}
1560module_exit(omap_gpio_exit);
1561
1562MODULE_DESCRIPTION("omap gpio driver");
1563MODULE_ALIAS("platform:gpio-omap");
1564MODULE_LICENSE("GPL v2");