blob: 1a0890586b45287ec0f8309ca035dc7d5e3eaf09 [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
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800329#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800330/*
331 * This only applies to chips that can't do both rising and falling edge
332 * detection at once. For all other chips, this function is a noop.
333 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200334static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800335{
336 void __iomem *reg = bank->base;
337 u32 l = 0;
338
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530339 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800340 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530341
342 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800343
Victor Kamensky661553b2013-11-16 02:01:04 +0200344 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800345 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200346 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800347 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200348 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800349
Victor Kamensky661553b2013-11-16 02:01:04 +0200350 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800351}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530352#else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200353static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800354#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800355
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200356static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
357 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100358{
359 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530360 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100361 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100362
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530363 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200364 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530365 } else if (bank->regs->irqctrl) {
366 reg += bank->regs->irqctrl;
367
Victor Kamensky661553b2013-11-16 02:01:04 +0200368 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000369 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200370 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100371 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200372 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100373 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200374 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100375 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530376 return -EINVAL;
377
Victor Kamensky661553b2013-11-16 02:01:04 +0200378 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530379 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100380 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530381 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100382 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530383 reg += bank->regs->edgectrl1;
384
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100385 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200386 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100387 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100388 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100389 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100390 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200391 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530392
393 /* Enable wake-up during idle for dynamic tick */
Russell King8ee1de62019-06-10 20:10:55 +0300394 omap_gpio_rmw(base + bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530395 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200396 readl_relaxed(bank->base + bank->regs->wkup_en);
397 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100398 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100399 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100400}
401
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200402static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200403{
404 if (bank->regs->pinctrl) {
405 void __iomem *reg = bank->base + bank->regs->pinctrl;
406
407 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200408 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200409 }
410
411 if (bank->regs->ctrl && !BANK_USED(bank)) {
412 void __iomem *reg = bank->base + bank->regs->ctrl;
413 u32 ctrl;
414
Victor Kamensky661553b2013-11-16 02:01:04 +0200415 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200416 /* Module is enabled, clocks are not gated */
417 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200418 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200419 bank->context.ctrl = ctrl;
420 }
421}
422
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200423static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200424{
425 void __iomem *base = bank->base;
426
427 if (bank->regs->wkup_en &&
428 !LINE_USED(bank->mod_usage, offset) &&
429 !LINE_USED(bank->irq_usage, offset)) {
430 /* Disable wake-up during idle for dynamic tick */
Russell King8ee1de62019-06-10 20:10:55 +0300431 omap_gpio_rmw(base + bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200432 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200433 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200434 }
435
436 if (bank->regs->ctrl && !BANK_USED(bank)) {
437 void __iomem *reg = bank->base + bank->regs->ctrl;
438 u32 ctrl;
439
Victor Kamensky661553b2013-11-16 02:01:04 +0200440 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200441 /* Module is disabled, clocks are gated */
442 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200443 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200444 bank->context.ctrl = ctrl;
445 }
446}
447
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200448static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200449{
450 void __iomem *reg = bank->base + bank->regs->direction;
451
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200452 return readl_relaxed(reg) & BIT(offset);
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200453}
454
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200455static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
Tony Lindgren3d009c82015-01-16 14:50:50 -0800456{
457 if (!LINE_USED(bank->mod_usage, offset)) {
458 omap_enable_gpio_module(bank, offset);
459 omap_set_gpio_direction(bank, offset, 1);
460 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200461 bank->irq_usage |= BIT(offset);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800462}
463
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200464static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100465{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200466 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100467 int retval;
David Brownella6472532008-03-03 04:33:30 -0800468 unsigned long flags;
Grygorii Strashkoea5fbe82015-03-23 14:18:29 +0200469 unsigned offset = d->hwirq;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100470
David Brownelle5c56ed2006-12-06 17:13:59 -0800471 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100472 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800473
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530474 if (!bank->regs->leveldetect0 &&
475 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100476 return -EINVAL;
477
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200478 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200479 retval = omap_set_gpio_triggering(bank, offset, type);
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300480 if (retval) {
Axel Lin627c89b2015-08-05 22:37:41 +0800481 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300482 goto error;
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300483 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200484 omap_gpio_init_irq(bank, offset);
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200485 if (!omap_gpio_is_input(bank, offset)) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200486 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300487 retval = -EINVAL;
488 goto error;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200489 }
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200490 raw_spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800491
492 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner43ec2e42015-06-23 15:52:39 +0200493 irq_set_handler_locked(d, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800494 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500495 /*
496 * Edge IRQs are already cleared/acked in irq_handler and
497 * not need to be masked, as result handle_edge_irq()
498 * logic is excessed here and may cause lose of interrupts.
499 * So just use handle_simple_irq.
500 */
501 irq_set_handler_locked(d, handle_simple_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800502
Grygorii Strashko1562e462015-05-22 17:35:49 +0300503 return 0;
504
505error:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100506 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100507}
508
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200509static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100511 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100512
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700513 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200514 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300515
516 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700517 if (bank->regs->irqstatus2) {
518 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200519 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700520 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700521
522 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200523 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100524}
525
Grygorii Strashko9943f262015-03-23 14:18:27 +0200526static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
527 unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100528{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200529 omap_clear_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100530}
531
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200532static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
Imre Deakea6dedd2006-06-26 16:16:00 -0700533{
534 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700535 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200536 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700537
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700538 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200539 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700540 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700541 l = ~l;
542 l &= mask;
543 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700544}
545
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200546static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100547{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100548 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100549 u32 l;
550
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700551 if (bank->regs->set_irqenable) {
552 reg += bank->regs->set_irqenable;
553 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530554 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700555 } else {
556 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200557 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700558 if (bank->regs->irqenable_inv)
559 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100560 else
561 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530562 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100563 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700564
Victor Kamensky661553b2013-11-16 02:01:04 +0200565 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700566}
567
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200568static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700569{
570 void __iomem *reg = bank->base;
571 u32 l;
572
573 if (bank->regs->clr_irqenable) {
574 reg += bank->regs->clr_irqenable;
575 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530576 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700577 } else {
578 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200579 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700580 if (bank->regs->irqenable_inv)
581 l |= gpio_mask;
582 else
583 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530584 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700585 }
586
Victor Kamensky661553b2013-11-16 02:01:04 +0200587 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100588}
589
Grygorii Strashko9943f262015-03-23 14:18:27 +0200590static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
591 unsigned offset, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100592{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530593 if (enable)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200594 omap_enable_gpio_irqbank(bank, BIT(offset));
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530595 else
Grygorii Strashko9943f262015-03-23 14:18:27 +0200596 omap_disable_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100597}
598
Tony Lindgren92105bb2005-09-07 17:20:26 +0100599/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200600static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100601{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200602 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100603
Grygorii Strashko0c0451e2016-04-12 13:52:31 +0300604 return irq_set_irq_wake(bank->irq, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100605}
606
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100607/*
608 * We need to unmask the GPIO bank interrupt as soon as possible to
609 * avoid missing GPIO interrupts for other lines in the bank.
610 * Then we need to mask-read-clear-unmask the triggered GPIO lines
611 * in the bank to avoid missing nested interrupts for a GPIO line.
612 * If we wait to unmask individual GPIO lines in the bank after the
613 * line's interrupt handler has been run, we may miss some nested
614 * interrupts.
615 */
Grygorii Strashko450fa542015-09-25 12:28:03 -0700616static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100617{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100618 void __iomem *isr_reg = NULL;
Russell King395373c2019-06-10 20:10:47 +0300619 u32 enabled, isr, edge;
Jon Hunter3513cde2013-04-04 15:16:14 -0500620 unsigned int bit;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700621 struct gpio_bank *bank = gpiobank;
622 unsigned long wa_lock_flags;
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300623 unsigned long lock_flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100624
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700625 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800626 if (WARN_ON(!isr_reg))
627 goto exit;
628
Tony Lindgren52845212018-09-20 12:35:32 -0700629 if (WARN_ONCE(!pm_runtime_active(bank->chip.parent),
630 "gpio irq%i while runtime suspended?\n", irq))
631 return IRQ_NONE;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700632
Laurent Navete83507b2013-03-20 13:15:57 +0100633 while (1) {
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300634 raw_spin_lock_irqsave(&bank->lock, lock_flags);
635
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200636 enabled = omap_get_gpio_irqbank_mask(bank);
Grygorii Strashko80ac93c2017-10-03 11:17:05 -0500637 isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100638
Russell King395373c2019-06-10 20:10:47 +0300639 /*
640 * Clear edge sensitive interrupts before calling handler(s)
641 * so subsequent edge transitions are not missed while the
642 * handlers are running.
643 */
644 edge = isr & ~bank->level_mask;
645 if (edge)
646 omap_clear_gpio_irqbank(bank, edge);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100647
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300648 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
649
Tony Lindgren92105bb2005-09-07 17:20:26 +0100650 if (!isr)
651 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100652
Jon Hunter3513cde2013-04-04 15:16:14 -0500653 while (isr) {
654 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200655 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100656
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300657 raw_spin_lock_irqsave(&bank->lock, lock_flags);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800658 /*
659 * Some chips can't respond to both rising and falling
660 * at the same time. If this irq was requested with
661 * both flags, we need to flip the ICR data for the IRQ
662 * to respond to the IRQ for the opposite direction.
663 * This will be indicated in the bank toggle_mask.
664 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200665 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200666 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800667
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300668 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
669
Grygorii Strashko450fa542015-09-25 12:28:03 -0700670 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
671
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100672 generic_handle_irq(irq_find_mapping(bank->chip.irq.domain,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200673 bit));
Grygorii Strashko450fa542015-09-25 12:28:03 -0700674
675 raw_spin_unlock_irqrestore(&bank->wa_lock,
676 wa_lock_flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100677 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000678 }
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800679exit:
Grygorii Strashko450fa542015-09-25 12:28:03 -0700680 return IRQ_HANDLED;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100681}
682
Tony Lindgren3d009c82015-01-16 14:50:50 -0800683static unsigned int omap_gpio_irq_startup(struct irq_data *d)
684{
685 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800686 unsigned long flags;
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200687 unsigned offset = d->hwirq;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800688
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200689 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300690
691 if (!LINE_USED(bank->mod_usage, offset))
692 omap_set_gpio_direction(bank, offset, 1);
693 else if (!omap_gpio_is_input(bank, offset))
694 goto err;
695 omap_enable_gpio_module(bank, offset);
696 bank->irq_usage |= BIT(offset);
697
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200698 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800699 omap_gpio_unmask_irq(d);
700
701 return 0;
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300702err:
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200703 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300704 return -EINVAL;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800705}
706
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200707static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300708{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200709 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700710 unsigned long flags;
Grygorii Strashko9943f262015-03-23 14:18:27 +0200711 unsigned offset = d->hwirq;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300712
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200713 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200714 bank->irq_usage &= ~(BIT(offset));
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300715 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Russell Kingc859e0d2019-06-10 20:10:44 +0300716 omap_clear_gpio_irqstatus(bank, offset);
717 omap_set_gpio_irqenable(bank, offset, 0);
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300718 if (!LINE_USED(bank->mod_usage, offset))
719 omap_clear_gpio_debounce(bank, offset);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200720 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200721 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700722}
723
724static void omap_gpio_irq_bus_lock(struct irq_data *data)
725{
726 struct gpio_bank *bank = omap_irq_data_get_bank(data);
727
Grygorii Strashko46748072018-09-28 16:39:50 -0500728 pm_runtime_get_sync(bank->chip.parent);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700729}
730
731static void gpio_irq_bus_sync_unlock(struct irq_data *data)
732{
733 struct gpio_bank *bank = omap_irq_data_get_bank(data);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200734
Grygorii Strashko46748072018-09-28 16:39:50 -0500735 pm_runtime_put(bank->chip.parent);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300736}
737
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200738static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100739{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200740 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200741 unsigned offset = d->hwirq;
Colin Cross85ec7b92011-06-06 13:38:18 -0700742 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100743
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200744 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200745 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Russell Kingc859e0d2019-06-10 20:10:44 +0300746 omap_set_gpio_irqenable(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200747 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100748}
749
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200750static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100751{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200752 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200753 unsigned offset = d->hwirq;
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100754 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700755 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700756
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200757 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200758 omap_set_gpio_irqenable(bank, offset, 1);
Russell Kingd01849f2019-03-01 11:02:52 -0800759
760 /*
761 * For level-triggered GPIOs, clearing must be done after the source
762 * is cleared, thus after the handler has run. OMAP4 needs this done
763 * after enabing the interrupt to clear the wakeup status.
764 */
Russell Kingc859e0d2019-06-10 20:10:44 +0300765 if (bank->regs->leveldetect0 && bank->regs->wkup_en &&
766 trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
Russell Kingd01849f2019-03-01 11:02:52 -0800767 omap_clear_gpio_irqstatus(bank, offset);
768
Russell Kingc859e0d2019-06-10 20:10:44 +0300769 if (trigger)
770 omap_set_gpio_triggering(bank, offset, trigger);
771
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200772 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100773}
774
David Brownelle5c56ed2006-12-06 17:13:59 -0800775/*---------------------------------------------------------------------*/
776
Magnus Damm79ee0312009-07-08 13:22:04 +0200777static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800778{
Wolfram Sanga3f4f722018-10-21 21:59:59 +0200779 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800780 void __iomem *mask_reg = bank->base +
781 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800782 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800783
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200784 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200785 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200786 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800787
788 return 0;
789}
790
Magnus Damm79ee0312009-07-08 13:22:04 +0200791static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800792{
Wolfram Sanga3f4f722018-10-21 21:59:59 +0200793 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800794 void __iomem *mask_reg = bank->base +
795 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800796 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800797
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200798 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200799 writel_relaxed(bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200800 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800801
802 return 0;
803}
804
Alexey Dobriyan47145212009-12-14 18:00:08 -0800805static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200806 .suspend_noirq = omap_mpuio_suspend_noirq,
807 .resume_noirq = omap_mpuio_resume_noirq,
808};
809
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200810/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800811static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800812 .driver = {
813 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200814 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800815 },
816};
817
818static struct platform_device omap_mpuio_device = {
819 .name = "mpuio",
820 .id = -1,
821 .dev = {
822 .driver = &omap_mpuio_driver.driver,
823 }
824 /* could list the /proc/iomem resources */
825};
826
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200827static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800828{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800829 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700830
David Brownell11a78b72006-12-06 17:14:11 -0800831 if (platform_driver_register(&omap_mpuio_driver) == 0)
832 (void) platform_device_register(&omap_mpuio_device);
833}
834
David Brownelle5c56ed2006-12-06 17:13:59 -0800835/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100836
Russell Kingdfbc6c72019-06-10 20:10:49 +0300837static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
838{
839 struct gpio_bank *bank = gpiochip_get_data(chip);
840 unsigned long flags;
841
842 pm_runtime_get_sync(chip->parent);
843
844 raw_spin_lock_irqsave(&bank->lock, flags);
845 omap_enable_gpio_module(bank, offset);
846 bank->mod_usage |= BIT(offset);
847 raw_spin_unlock_irqrestore(&bank->lock, flags);
848
849 return 0;
850}
851
852static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
853{
854 struct gpio_bank *bank = gpiochip_get_data(chip);
855 unsigned long flags;
856
857 raw_spin_lock_irqsave(&bank->lock, flags);
858 bank->mod_usage &= ~(BIT(offset));
859 if (!LINE_USED(bank->irq_usage, offset)) {
860 omap_set_gpio_direction(bank, offset, 1);
861 omap_clear_gpio_debounce(bank, offset);
862 }
863 omap_disable_gpio_module(bank, offset);
864 raw_spin_unlock_irqrestore(&bank->lock, flags);
865
866 pm_runtime_put(chip->parent);
867}
868
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200869static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200870{
Russell King40bb2272019-06-10 20:10:50 +0300871 struct gpio_bank *bank = gpiochip_get_data(chip);
Yegor Yefremov93700842014-04-24 08:57:39 +0200872
Russell King40bb2272019-06-10 20:10:50 +0300873 return !!(readl_relaxed(bank->base + bank->regs->direction) &
874 BIT(offset));
Yegor Yefremov93700842014-04-24 08:57:39 +0200875}
876
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200877static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800878{
879 struct gpio_bank *bank;
880 unsigned long flags;
881
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100882 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200883 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200884 omap_set_gpio_direction(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200885 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800886 return 0;
887}
888
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200889static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800890{
Russell King5ca5f922019-06-10 20:10:51 +0300891 struct gpio_bank *bank = gpiochip_get_data(chip);
892 void __iomem *reg;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300893
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200894 if (omap_gpio_is_input(bank, offset))
Russell King5ca5f922019-06-10 20:10:51 +0300895 reg = bank->base + bank->regs->datain;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300896 else
Russell King5ca5f922019-06-10 20:10:51 +0300897 reg = bank->base + bank->regs->dataout;
898
899 return (readl_relaxed(reg) & BIT(offset)) != 0;
David Brownell52e31342008-03-03 12:43:23 -0800900}
901
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200902static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800903{
904 struct gpio_bank *bank;
905 unsigned long flags;
906
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100907 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200908 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700909 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200910 omap_set_gpio_direction(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200911 raw_spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200912 return 0;
David Brownell52e31342008-03-03 12:43:23 -0800913}
914
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200915static int omap_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
916 unsigned long *bits)
917{
918 struct gpio_bank *bank = gpiochip_get_data(chip);
Russell King6653dd82019-06-10 20:10:52 +0300919 void __iomem *base = bank->base;
920 u32 direction, m, val = 0;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200921
Russell King6653dd82019-06-10 20:10:52 +0300922 direction = readl_relaxed(base + bank->regs->direction);
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200923
Russell King6653dd82019-06-10 20:10:52 +0300924 m = direction & *mask;
925 if (m)
926 val |= readl_relaxed(base + bank->regs->datain) & m;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200927
Russell King6653dd82019-06-10 20:10:52 +0300928 m = ~direction & *mask;
929 if (m)
930 val |= readl_relaxed(base + bank->regs->dataout) & m;
931
932 *bits = val;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200933
934 return 0;
935}
936
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200937static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
938 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700939{
940 struct gpio_bank *bank;
941 unsigned long flags;
David Rivshin83977442017-04-24 18:56:50 -0400942 int ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700943
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100944 bank = gpiochip_get_data(chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800945
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200946 raw_spin_lock_irqsave(&bank->lock, flags);
David Rivshin83977442017-04-24 18:56:50 -0400947 ret = omap2_set_gpio_debounce(bank, offset, debounce);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200948 raw_spin_unlock_irqrestore(&bank->lock, flags);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700949
David Rivshin83977442017-04-24 18:56:50 -0400950 if (ret)
951 dev_info(chip->parent,
952 "Could not set line %u debounce to %u microseconds (%d)",
953 offset, debounce, ret);
954
955 return ret;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700956}
957
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300958static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset,
959 unsigned long config)
960{
961 u32 debounce;
962
963 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
964 return -ENOTSUPP;
965
966 debounce = pinconf_to_config_argument(config);
967 return omap_gpio_debounce(chip, offset, debounce);
968}
969
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200970static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800971{
972 struct gpio_bank *bank;
973 unsigned long flags;
974
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100975 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200976 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700977 bank->set_dataout(bank, offset, value);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200978 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800979}
980
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200981static void omap_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
982 unsigned long *bits)
983{
984 struct gpio_bank *bank = gpiochip_get_data(chip);
Russell King8ba70592019-06-10 20:10:53 +0300985 void __iomem *reg = bank->base + bank->regs->dataout;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200986 unsigned long flags;
Russell King8ba70592019-06-10 20:10:53 +0300987 u32 l;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200988
989 raw_spin_lock_irqsave(&bank->lock, flags);
Russell King8ba70592019-06-10 20:10:53 +0300990 l = (readl_relaxed(reg) & ~*mask) | (*bits & *mask);
991 writel_relaxed(l, reg);
992 bank->context.dataout = l;
Janusz Krzysztofik442af142018-07-19 01:57:08 +0200993 raw_spin_unlock_irqrestore(&bank->lock, flags);
994}
995
David Brownell52e31342008-03-03 12:43:23 -0800996/*---------------------------------------------------------------------*/
997
Arnd Bergmanne4b2ae72017-09-16 22:42:21 +0200998static void omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700999{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001000 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001001 u32 rev;
1002
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001003 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001004 return;
1005
Victor Kamensky661553b2013-11-16 02:01:04 +02001006 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001007 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001008 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001009
1010 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001011}
1012
Charulatha V03e128c2011-05-05 19:58:01 +05301013static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001014{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301015 void __iomem *base = bank->base;
1016 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001017
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301018 if (bank->width == 16)
1019 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001020
Charulatha Vd0d665a2011-08-31 00:02:21 +05301021 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001022 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301023 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001024 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301025
Russell King8ee1de62019-06-10 20:10:55 +03001026 omap_gpio_rmw(base + bank->regs->irqenable, l,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001027 bank->regs->irqenable_inv);
Russell King8ee1de62019-06-10 20:10:55 +03001028 omap_gpio_rmw(base + bank->regs->irqstatus, l,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001029 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301030 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001031 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301032
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301033 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001034 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301035 /* Initialize interface clk ungated, module enabled */
1036 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001037 writel_relaxed(0, base + bank->regs->ctrl);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001038}
1039
Nishanth Menon46824e222014-09-05 14:52:55 -05001040static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001041{
Grygorii Strashko81930322017-11-15 12:36:33 -06001042 struct gpio_irq_chip *irq;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001043 static int gpio;
Linus Walleij088413b2017-12-29 13:22:58 +01001044 const char *label;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001045 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001046 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001047
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001048 /*
1049 * REVISIT eventually switch from OMAP-specific gpio structs
1050 * over to the generic ones
1051 */
1052 bank->chip.request = omap_gpio_request;
1053 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001054 bank->chip.get_direction = omap_gpio_get_direction;
1055 bank->chip.direction_input = omap_gpio_input;
1056 bank->chip.get = omap_gpio_get;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001057 bank->chip.get_multiple = omap_gpio_get_multiple;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001058 bank->chip.direction_output = omap_gpio_output;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001059 bank->chip.set_config = omap_gpio_set_config;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001060 bank->chip.set = omap_gpio_set;
Janusz Krzysztofik442af142018-07-19 01:57:08 +02001061 bank->chip.set_multiple = omap_gpio_set_multiple;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301062 if (bank->is_mpuio) {
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001063 bank->chip.label = "mpuio";
Tarun Kanti DebBarma6ed87c52011-09-13 14:41:44 +05301064 if (bank->regs->wkup_en)
Linus Walleij58383c782015-11-04 09:56:26 +01001065 bank->chip.parent = &omap_mpuio_device.dev;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001066 bank->chip.base = OMAP_MPUIO(0);
1067 } else {
Linus Walleij088413b2017-12-29 13:22:58 +01001068 label = devm_kasprintf(bank->chip.parent, GFP_KERNEL, "gpio-%d-%d",
1069 gpio, gpio + bank->width - 1);
1070 if (!label)
1071 return -ENOMEM;
1072 bank->chip.label = label;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001073 bank->chip.base = gpio;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001074 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001075 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001076
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001077#ifdef CONFIG_ARCH_OMAP1
1078 /*
1079 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1080 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1081 */
Bartosz Golaszewski2ed36f32017-03-04 17:23:31 +01001082 irq_base = devm_irq_alloc_descs(bank->chip.parent,
1083 -1, 0, bank->width, 0);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001084 if (irq_base < 0) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001085 dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n");
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001086 return -ENODEV;
1087 }
1088#endif
1089
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001090 /* MPUIO is a bit different, reading IRQ status clears it */
Russell King693de832019-06-10 20:10:48 +03001091 if (bank->is_mpuio && !bank->regs->wkup_en)
1092 irqc->irq_set_wake = NULL;
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001093
Grygorii Strashko81930322017-11-15 12:36:33 -06001094 irq = &bank->chip.irq;
1095 irq->chip = irqc;
1096 irq->handler = handle_bad_irq;
1097 irq->default_type = IRQ_TYPE_NONE;
1098 irq->num_parents = 1;
1099 irq->parents = &bank->irq;
1100 irq->first = irq_base;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001101
Grygorii Strashko81930322017-11-15 12:36:33 -06001102 ret = gpiochip_add_data(&bank->chip, bank);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001103 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001104 dev_err(bank->chip.parent,
Grygorii Strashko81930322017-11-15 12:36:33 -06001105 "Could not register gpio chip %d\n", ret);
1106 return ret;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001107 }
1108
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001109 ret = devm_request_irq(bank->chip.parent, bank->irq,
1110 omap_gpio_irq_handler,
1111 0, dev_name(bank->chip.parent), bank);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001112 if (ret)
1113 gpiochip_remove(&bank->chip);
1114
Grygorii Strashko81930322017-11-15 12:36:33 -06001115 if (!bank->is_mpuio)
1116 gpio += bank->width;
1117
Grygorii Strashko450fa542015-09-25 12:28:03 -07001118 return ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001119}
1120
Arnd Bergmann7c685712019-03-07 11:33:32 +01001121static void omap_gpio_init_context(struct gpio_bank *p)
1122{
1123 struct omap_gpio_reg_offs *regs = p->regs;
1124 void __iomem *base = p->base;
1125
1126 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1127 p->context.oe = readl_relaxed(base + regs->direction);
1128 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1129 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1130 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1131 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1132 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1133 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1134 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
1135
1136 if (regs->set_dataout && p->regs->clr_dataout)
1137 p->context.dataout = readl_relaxed(base + regs->set_dataout);
1138 else
1139 p->context.dataout = readl_relaxed(base + regs->dataout);
1140
1141 p->context_valid = true;
1142}
1143
1144static void omap_gpio_restore_context(struct gpio_bank *bank)
1145{
1146 writel_relaxed(bank->context.wake_en,
1147 bank->base + bank->regs->wkup_en);
1148 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1149 writel_relaxed(bank->context.leveldetect0,
1150 bank->base + bank->regs->leveldetect0);
1151 writel_relaxed(bank->context.leveldetect1,
1152 bank->base + bank->regs->leveldetect1);
1153 writel_relaxed(bank->context.risingdetect,
1154 bank->base + bank->regs->risingdetect);
1155 writel_relaxed(bank->context.fallingdetect,
1156 bank->base + bank->regs->fallingdetect);
1157 if (bank->regs->set_dataout && bank->regs->clr_dataout)
1158 writel_relaxed(bank->context.dataout,
1159 bank->base + bank->regs->set_dataout);
1160 else
1161 writel_relaxed(bank->context.dataout,
1162 bank->base + bank->regs->dataout);
1163 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
1164
1165 if (bank->dbck_enable_mask) {
1166 writel_relaxed(bank->context.debounce, bank->base +
1167 bank->regs->debounce);
1168 writel_relaxed(bank->context.debounce_en,
1169 bank->base + bank->regs->debounce_en);
1170 }
1171
1172 writel_relaxed(bank->context.irqenable1,
1173 bank->base + bank->regs->irqenable);
1174 writel_relaxed(bank->context.irqenable2,
1175 bank->base + bank->regs->irqenable2);
1176}
1177
1178static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context)
1179{
1180 struct device *dev = bank->chip.parent;
Tony Lindgren21e21182019-03-25 15:43:16 -07001181 void __iomem *base = bank->base;
1182 u32 nowake;
1183
1184 bank->saved_datain = readl_relaxed(base + bank->regs->datain);
Arnd Bergmann7c685712019-03-07 11:33:32 +01001185
Arnd Bergmann7c685712019-03-07 11:33:32 +01001186 if (!bank->enabled_non_wakeup_gpios)
1187 goto update_gpio_context_count;
1188
1189 if (!may_lose_context)
1190 goto update_gpio_context_count;
1191
1192 /*
Tony Lindgren21e21182019-03-25 15:43:16 -07001193 * If going to OFF, remove triggering for all wkup domain
Arnd Bergmann7c685712019-03-07 11:33:32 +01001194 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1195 * generated. See OMAP2420 Errata item 1.101.
1196 */
Tony Lindgren21e21182019-03-25 15:43:16 -07001197 if (!bank->loses_context && bank->enabled_non_wakeup_gpios) {
1198 nowake = bank->enabled_non_wakeup_gpios;
Russell King8ee1de62019-06-10 20:10:55 +03001199 omap_gpio_rmw(base + bank->regs->fallingdetect, nowake, ~nowake);
1200 omap_gpio_rmw(base + bank->regs->risingdetect, nowake, ~nowake);
Tony Lindgren21e21182019-03-25 15:43:16 -07001201 }
Arnd Bergmann7c685712019-03-07 11:33:32 +01001202
1203update_gpio_context_count:
1204 if (bank->get_context_loss_count)
1205 bank->context_loss_count =
1206 bank->get_context_loss_count(dev);
1207
1208 omap_gpio_dbck_disable(bank);
1209}
1210
1211static void omap_gpio_unidle(struct gpio_bank *bank)
1212{
1213 struct device *dev = bank->chip.parent;
1214 u32 l = 0, gen, gen0, gen1;
1215 int c;
1216
1217 /*
1218 * On the first resume during the probe, the context has not
1219 * been initialised and so initialise it now. Also initialise
1220 * the context loss count.
1221 */
1222 if (bank->loses_context && !bank->context_valid) {
1223 omap_gpio_init_context(bank);
1224
1225 if (bank->get_context_loss_count)
1226 bank->context_loss_count =
1227 bank->get_context_loss_count(dev);
1228 }
1229
1230 omap_gpio_dbck_enable(bank);
1231
Arnd Bergmann7c685712019-03-07 11:33:32 +01001232 if (bank->loses_context) {
1233 if (!bank->get_context_loss_count) {
1234 omap_gpio_restore_context(bank);
1235 } else {
1236 c = bank->get_context_loss_count(dev);
1237 if (c != bank->context_loss_count) {
1238 omap_gpio_restore_context(bank);
1239 } else {
1240 return;
1241 }
1242 }
Tony Lindgren21e21182019-03-25 15:43:16 -07001243 } else {
1244 /* Restore changes done for OMAP2420 errata 1.101 */
1245 writel_relaxed(bank->context.fallingdetect,
1246 bank->base + bank->regs->fallingdetect);
1247 writel_relaxed(bank->context.risingdetect,
1248 bank->base + bank->regs->risingdetect);
Arnd Bergmann7c685712019-03-07 11:33:32 +01001249 }
1250
Arnd Bergmann7c685712019-03-07 11:33:32 +01001251 l = readl_relaxed(bank->base + bank->regs->datain);
1252
1253 /*
1254 * Check if any of the non-wakeup interrupt GPIOs have changed
1255 * state. If so, generate an IRQ by software. This is
1256 * horribly racy, but it's the best we can do to work around
1257 * this silicon bug.
1258 */
1259 l ^= bank->saved_datain;
1260 l &= bank->enabled_non_wakeup_gpios;
1261
1262 /*
1263 * No need to generate IRQs for the rising edge for gpio IRQs
1264 * configured with falling edge only; and vice versa.
1265 */
1266 gen0 = l & bank->context.fallingdetect;
1267 gen0 &= bank->saved_datain;
1268
1269 gen1 = l & bank->context.risingdetect;
1270 gen1 &= ~(bank->saved_datain);
1271
1272 /* FIXME: Consider GPIO IRQs with level detections properly! */
1273 gen = l & (~(bank->context.fallingdetect) &
1274 ~(bank->context.risingdetect));
1275 /* Consider all GPIO IRQs needed to be updated */
1276 gen |= gen0 | gen1;
1277
1278 if (gen) {
1279 u32 old0, old1;
1280
1281 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1282 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
1283
1284 if (!bank->regs->irqstatus_raw0) {
1285 writel_relaxed(old0 | gen, bank->base +
1286 bank->regs->leveldetect0);
1287 writel_relaxed(old1 | gen, bank->base +
1288 bank->regs->leveldetect1);
1289 }
1290
1291 if (bank->regs->irqstatus_raw0) {
1292 writel_relaxed(old0 | l, bank->base +
1293 bank->regs->leveldetect0);
1294 writel_relaxed(old1 | l, bank->base +
1295 bank->regs->leveldetect1);
1296 }
1297 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1298 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
1299 }
Arnd Bergmann7c685712019-03-07 11:33:32 +01001300}
Tony Lindgrenb764a582018-09-20 12:35:31 -07001301
1302static int gpio_omap_cpu_notifier(struct notifier_block *nb,
1303 unsigned long cmd, void *v)
1304{
1305 struct gpio_bank *bank;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001306 unsigned long flags;
1307
1308 bank = container_of(nb, struct gpio_bank, nb);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001309
1310 raw_spin_lock_irqsave(&bank->lock, flags);
1311 switch (cmd) {
1312 case CPU_CLUSTER_PM_ENTER:
1313 if (bank->is_suspended)
1314 break;
1315 omap_gpio_idle(bank, true);
1316 break;
1317 case CPU_CLUSTER_PM_ENTER_FAILED:
1318 case CPU_CLUSTER_PM_EXIT:
1319 if (bank->is_suspended)
1320 break;
1321 omap_gpio_unidle(bank);
1322 break;
1323 }
1324 raw_spin_unlock_irqrestore(&bank->lock, flags);
1325
1326 return NOTIFY_OK;
1327}
1328
Arnd Bergmann7c685712019-03-07 11:33:32 +01001329static struct omap_gpio_reg_offs omap2_gpio_regs = {
1330 .revision = OMAP24XX_GPIO_REVISION,
1331 .direction = OMAP24XX_GPIO_OE,
1332 .datain = OMAP24XX_GPIO_DATAIN,
1333 .dataout = OMAP24XX_GPIO_DATAOUT,
1334 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1335 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1336 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1337 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1338 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1339 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1340 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1341 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1342 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1343 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1344 .ctrl = OMAP24XX_GPIO_CTRL,
1345 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1346 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1347 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1348 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1349 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1350};
1351
1352static struct omap_gpio_reg_offs omap4_gpio_regs = {
1353 .revision = OMAP4_GPIO_REVISION,
1354 .direction = OMAP4_GPIO_OE,
1355 .datain = OMAP4_GPIO_DATAIN,
1356 .dataout = OMAP4_GPIO_DATAOUT,
1357 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1358 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1359 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1360 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
Russell King64ea3e92019-06-10 20:10:45 +03001361 .irqstatus_raw0 = OMAP4_GPIO_IRQSTATUSRAW0,
1362 .irqstatus_raw1 = OMAP4_GPIO_IRQSTATUSRAW1,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001363 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1364 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1365 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1366 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1367 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1368 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1369 .ctrl = OMAP4_GPIO_CTRL,
1370 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1371 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1372 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1373 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1374 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1375};
1376
Arnd Bergmann7c685712019-03-07 11:33:32 +01001377static const struct omap_gpio_platform_data omap2_pdata = {
1378 .regs = &omap2_gpio_regs,
1379 .bank_width = 32,
1380 .dbck_flag = false,
1381};
1382
1383static const struct omap_gpio_platform_data omap3_pdata = {
1384 .regs = &omap2_gpio_regs,
1385 .bank_width = 32,
1386 .dbck_flag = true,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001387};
1388
1389static const struct omap_gpio_platform_data omap4_pdata = {
1390 .regs = &omap4_gpio_regs,
1391 .bank_width = 32,
1392 .dbck_flag = true,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001393};
1394
1395static const struct of_device_id omap_gpio_match[] = {
1396 {
1397 .compatible = "ti,omap4-gpio",
1398 .data = &omap4_pdata,
1399 },
1400 {
1401 .compatible = "ti,omap3-gpio",
1402 .data = &omap3_pdata,
1403 },
1404 {
1405 .compatible = "ti,omap2-gpio",
1406 .data = &omap2_pdata,
1407 },
1408 { },
1409};
1410MODULE_DEVICE_TABLE(of, omap_gpio_match);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001411
Bill Pemberton38363092012-11-19 13:22:34 -05001412static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001413{
Benoit Cousson862ff642012-02-01 15:58:56 +01001414 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001415 struct device_node *node = dev->of_node;
1416 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001417 const struct omap_gpio_platform_data *pdata;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001418 struct gpio_bank *bank;
Nishanth Menon46824e222014-09-05 14:52:55 -05001419 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001420 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001421
Benoit Cousson384ebe12011-08-16 11:53:02 +02001422 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1423
Jingoo Hane56aee12013-07-30 17:08:05 +09001424 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001425 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001426 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001427
Markus Elfringf97364c2018-02-10 21:49:22 +01001428 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
Markus Elfring9117d402018-02-10 21:46:30 +01001429 if (!bank)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001430 return -ENOMEM;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001431
Nishanth Menon46824e222014-09-05 14:52:55 -05001432 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1433 if (!irqc)
1434 return -ENOMEM;
1435
Tony Lindgren3d009c82015-01-16 14:50:50 -08001436 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e222014-09-05 14:52:55 -05001437 irqc->irq_shutdown = omap_gpio_irq_shutdown,
Russell King693de832019-06-10 20:10:48 +03001438 irqc->irq_ack = dummy_irq_chip.irq_ack,
Nishanth Menon46824e222014-09-05 14:52:55 -05001439 irqc->irq_mask = omap_gpio_mask_irq,
1440 irqc->irq_unmask = omap_gpio_unmask_irq,
1441 irqc->irq_set_type = omap_gpio_irq_type,
1442 irqc->irq_set_wake = omap_gpio_wake_enable,
Grygorii Strashkoaca82d12015-09-25 12:28:02 -07001443 irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
1444 irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
Nishanth Menon46824e222014-09-05 14:52:55 -05001445 irqc->name = dev_name(&pdev->dev);
Grygorii Strashko0c0451e2016-04-12 13:52:31 +03001446 irqc->flags = IRQCHIP_MASK_ON_SUSPEND;
Grygorii Strashko46748072018-09-28 16:39:50 -05001447 irqc->parent_device = dev;
Nishanth Menon46824e222014-09-05 14:52:55 -05001448
Grygorii Strashko89d18e32015-08-18 14:10:53 +03001449 bank->irq = platform_get_irq(pdev, 0);
1450 if (bank->irq <= 0) {
1451 if (!bank->irq)
1452 bank->irq = -ENXIO;
1453 if (bank->irq != -EPROBE_DEFER)
1454 dev_err(dev,
1455 "can't get irq resource ret=%d\n", bank->irq);
1456 return bank->irq;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001457 }
1458
Linus Walleij58383c782015-11-04 09:56:26 +01001459 bank->chip.parent = dev;
Grygorii Strashkoc23837c2015-06-25 18:13:33 +03001460 bank->chip.owner = THIS_MODULE;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001461 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001462 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001463 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301464 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301465 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001466 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001467#ifdef CONFIG_OF_GPIO
1468 bank->chip.of_node = of_node_get(node);
1469#endif
Tony Lindgrenec0daae2018-09-20 12:35:30 -07001470
Jon Huntera2797be2013-04-04 15:16:15 -05001471 if (node) {
1472 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1473 bank->loses_context = true;
1474 } else {
1475 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001476
1477 if (bank->loses_context)
1478 bank->get_context_loss_count =
1479 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001480 }
1481
Russell King8ba70592019-06-10 20:10:53 +03001482 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001483 bank->set_dataout = omap_set_gpio_dataout_reg;
Russell King8ba70592019-06-10 20:10:53 +03001484 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001485 bank->set_dataout = omap_set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001486
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001487 raw_spin_lock_init(&bank->lock);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001488 raw_spin_lock_init(&bank->wa_lock);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001489
1490 /* Static mapping, never released */
Enrico Weigelt, metux IT consult58f57f82019-03-11 20:50:05 +01001491 bank->base = devm_platform_ioremap_resource(pdev, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001492 if (IS_ERR(bank->base)) {
Jingoo Han717f70e2014-02-12 11:51:38 +09001493 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001494 }
1495
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001496 if (bank->dbck_flag) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001497 bank->dbck = devm_clk_get(dev, "dbclk");
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001498 if (IS_ERR(bank->dbck)) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001499 dev_err(dev,
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001500 "Could not get gpio dbck. Disable debounce\n");
1501 bank->dbck_flag = false;
1502 } else {
1503 clk_prepare(bank->dbck);
1504 }
1505 }
1506
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301507 platform_set_drvdata(pdev, bank);
1508
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001509 pm_runtime_enable(dev);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001510 pm_runtime_get_sync(dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001511
Charulatha Vd0d665a2011-08-31 00:02:21 +05301512 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001513 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301514
Charulatha V03e128c2011-05-05 19:58:01 +05301515 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001516
Nishanth Menon46824e222014-09-05 14:52:55 -05001517 ret = omap_gpio_chip_init(bank, irqc);
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001518 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001519 pm_runtime_put_sync(dev);
1520 pm_runtime_disable(dev);
Arvind Yadave2c3c192017-08-01 12:14:31 +05301521 if (bank->dbck_flag)
1522 clk_unprepare(bank->dbck);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001523 return ret;
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001524 }
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001525
Tony Lindgren9a748052010-12-07 16:26:56 -08001526 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001527
Russell Kinge6818d22019-04-08 12:46:53 -07001528 bank->nb.notifier_call = gpio_omap_cpu_notifier;
1529 cpu_pm_register_notifier(&bank->nb);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001530
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001531 pm_runtime_put(dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301532
Jon Hunter879fe322013-04-04 15:16:12 -05001533 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001534}
1535
Tony Lindgrencac089f2015-04-23 16:56:22 -07001536static int omap_gpio_remove(struct platform_device *pdev)
1537{
1538 struct gpio_bank *bank = platform_get_drvdata(pdev);
1539
Russell Kinge6818d22019-04-08 12:46:53 -07001540 cpu_pm_unregister_notifier(&bank->nb);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001541 gpiochip_remove(&bank->chip);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001542 pm_runtime_disable(&pdev->dev);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001543 if (bank->dbck_flag)
1544 clk_unprepare(bank->dbck);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001545
1546 return 0;
1547}
1548
Tony Lindgrenb764a582018-09-20 12:35:31 -07001549static int __maybe_unused omap_gpio_runtime_suspend(struct device *dev)
1550{
Wolfram Sanga3f4f722018-10-21 21:59:59 +02001551 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001552 unsigned long flags;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001553
1554 raw_spin_lock_irqsave(&bank->lock, flags);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001555 omap_gpio_idle(bank, true);
1556 bank->is_suspended = true;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001557 raw_spin_unlock_irqrestore(&bank->lock, flags);
1558
Russell King044e4992019-04-10 12:51:13 -07001559 return 0;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001560}
1561
1562static int __maybe_unused omap_gpio_runtime_resume(struct device *dev)
1563{
Wolfram Sanga3f4f722018-10-21 21:59:59 +02001564 struct gpio_bank *bank = dev_get_drvdata(dev);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001565 unsigned long flags;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001566
1567 raw_spin_lock_irqsave(&bank->lock, flags);
Tony Lindgrenb764a582018-09-20 12:35:31 -07001568 omap_gpio_unidle(bank);
1569 bank->is_suspended = false;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001570 raw_spin_unlock_irqrestore(&bank->lock, flags);
1571
Russell King044e4992019-04-10 12:51:13 -07001572 return 0;
Tony Lindgrenb764a582018-09-20 12:35:31 -07001573}
1574
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301575static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301576 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1577 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301578};
Benoit Cousson384ebe12011-08-16 11:53:02 +02001579
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001580static struct platform_driver omap_gpio_driver = {
1581 .probe = omap_gpio_probe,
Tony Lindgrencac089f2015-04-23 16:56:22 -07001582 .remove = omap_gpio_remove,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001583 .driver = {
1584 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301585 .pm = &gpio_pm_ops,
Arnd Bergmann7c685712019-03-07 11:33:32 +01001586 .of_match_table = omap_gpio_match,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001587 },
1588};
1589
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001590/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001591 * gpio driver register needs to be done before
1592 * machine_init functions access gpio APIs.
1593 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001594 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001595static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001596{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001597 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001598}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001599postcore_initcall(omap_gpio_drv_reg);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001600
1601static void __exit omap_gpio_exit(void)
1602{
1603 platform_driver_unregister(&omap_gpio_driver);
1604}
1605module_exit(omap_gpio_exit);
1606
1607MODULE_DESCRIPTION("omap gpio driver");
1608MODULE_ALIAS("platform:gpio-omap");
1609MODULE_LICENSE("GPL v2");