blob: 5d6a5744352fdb212d5c26356575a7cadbba3550 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002 * Support functions for OMAP GPIO
3 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01004 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07007 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010015#include <linux/init.h>
16#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/interrupt.h>
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +020018#include <linux/syscore_ops.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Benoit Cousson96751fc2012-02-01 16:01:39 +010022#include <linux/device.h>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080023#include <linux/pm_runtime.h>
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +053024#include <linux/pm.h>
Benoit Cousson384ebe12011-08-16 11:53:02 +020025#include <linux/of.h>
26#include <linux/of_device.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070027#include <linux/gpio.h>
Yegor Yefremov93700842014-04-24 08:57:39 +020028#include <linux/bitops.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070029#include <linux/platform_data/gpio-omap.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010030
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053031#define OFF_MODE 1
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +030032#define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053033
Charulatha V03e128c2011-05-05 19:58:01 +053034static LIST_HEAD(omap_gpio_list);
35
Charulatha V6d62e212011-04-18 15:06:51 +000036struct gpio_regs {
37 u32 irqenable1;
38 u32 irqenable2;
39 u32 wake_en;
40 u32 ctrl;
41 u32 oe;
42 u32 leveldetect0;
43 u32 leveldetect1;
44 u32 risingdetect;
45 u32 fallingdetect;
46 u32 dataout;
Nishanth Menonae547352011-09-09 19:08:58 +053047 u32 debounce;
48 u32 debounce_en;
Charulatha V6d62e212011-04-18 15:06:51 +000049};
50
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010051struct gpio_bank {
Charulatha V03e128c2011-05-05 19:58:01 +053052 struct list_head node;
Tony Lindgren92105bb2005-09-07 17:20:26 +010053 void __iomem *base;
Grygorii Strashko30cefea2015-09-25 12:06:02 -070054 int irq;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080055 u32 non_wakeup_gpios;
56 u32 enabled_non_wakeup_gpios;
Charulatha V6d62e212011-04-18 15:06:51 +000057 struct gpio_regs context;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -080058 u32 saved_datain;
Kevin Hilmanb144ff62008-01-16 21:56:15 -080059 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -080060 u32 toggle_mask;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +020061 raw_spinlock_t lock;
Grygorii Strashko450fa542015-09-25 12:28:03 -070062 raw_spinlock_t wa_lock;
David Brownell52e31342008-03-03 12:43:23 -080063 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -080064 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -080065 u32 mod_usage;
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020066 u32 irq_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -080067 u32 dbck_enable_mask;
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +053068 bool dbck_enabled;
Charulatha Vd0d665a2011-08-31 00:02:21 +053069 bool is_mpuio;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080070 bool dbck_flag;
Charulatha V0cde8d02011-05-05 20:15:16 +053071 bool loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -050072 bool context_valid;
Tony Lindgren5de62b82010-12-07 16:26:58 -080073 int stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -070074 u32 width;
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053075 int context_loss_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +053076 int power_mode;
77 bool workaround_enabled;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070078
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +020079 void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable);
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +053080 int (*get_context_loss_count)(struct device *dev);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -070081
82 struct omap_gpio_reg_offs *regs;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010083};
84
Charulatha Vc8eef652011-05-02 15:21:42 +053085#define GPIO_MOD_CTRL_BIT BIT(0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010086
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020087#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +020088#define LINE_USED(line, offset) (line & (BIT(offset)))
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +020089
Tony Lindgren3d009c82015-01-16 14:50:50 -080090static void omap_gpio_unmask_irq(struct irq_data *d);
91
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020092static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
Jon Hunterede4d7a2013-03-01 11:22:47 -060093{
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +020094 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
Linus Walleijd99f7ae2015-12-07 11:16:00 +010095 return gpiochip_get_data(chip);
Benoit Cousson25db7112012-02-23 21:50:10 +010096}
97
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +020098static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
99 int is_input)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100100{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100101 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100102 u32 l;
103
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700104 reg += bank->regs->direction;
Victor Kamensky661553b2013-11-16 02:01:04 +0200105 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100106 if (is_input)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200107 l |= BIT(gpio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100108 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200109 l &= ~(BIT(gpio));
Victor Kamensky661553b2013-11-16 02:01:04 +0200110 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530111 bank->context.oe = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100112}
113
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700114
115/* set data out value using dedicate set/clear register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200116static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200117 int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100118{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100119 void __iomem *reg = bank->base;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200120 u32 l = BIT(offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530122 if (enable) {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700123 reg += bank->regs->set_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530124 bank->context.dataout |= l;
125 } else {
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700126 reg += bank->regs->clr_dataout;
Tarun Kanti DebBarma2c836f72012-03-02 12:52:52 +0530127 bank->context.dataout &= ~l;
128 }
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700129
Victor Kamensky661553b2013-11-16 02:01:04 +0200130 writel_relaxed(l, reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700131}
132
133/* set data out value using mask register */
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200134static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200135 int enable)
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700136{
137 void __iomem *reg = bank->base + bank->regs->dataout;
Grygorii Strashko04ebcbd2015-03-23 14:18:24 +0200138 u32 gpio_bit = BIT(offset);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700139 u32 l;
140
Victor Kamensky661553b2013-11-16 02:01:04 +0200141 l = readl_relaxed(reg);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700142 if (enable)
143 l |= gpio_bit;
144 else
145 l &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200146 writel_relaxed(l, reg);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530147 bank->context.dataout = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100148}
149
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200150static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100151{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700152 void __iomem *reg = bank->base + bank->regs->datain;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200154 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100155}
156
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200157static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300158{
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700159 void __iomem *reg = bank->base + bank->regs->dataout;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300160
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200161 return (readl_relaxed(reg) & (BIT(offset))) != 0;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300162}
163
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200164static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700165{
Victor Kamensky661553b2013-11-16 02:01:04 +0200166 int l = readl_relaxed(base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700167
Benoit Cousson862ff642012-02-01 15:58:56 +0100168 if (set)
Kevin Hilmanece95282011-07-12 08:18:15 -0700169 l |= mask;
170 else
171 l &= ~mask;
172
Victor Kamensky661553b2013-11-16 02:01:04 +0200173 writel_relaxed(l, base + reg);
Kevin Hilmanece95282011-07-12 08:18:15 -0700174}
Tony Lindgren92105bb2005-09-07 17:20:26 +0100175
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200176static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530177{
178 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300179 clk_enable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530180 bank->dbck_enabled = true;
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300181
Victor Kamensky661553b2013-11-16 02:01:04 +0200182 writel_relaxed(bank->dbck_enable_mask,
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300183 bank->base + bank->regs->debounce_en);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530184 }
185}
186
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200187static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530188{
189 if (bank->dbck_enable_mask && bank->dbck_enabled) {
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300190 /*
191 * Disable debounce before cutting it's clock. If debounce is
192 * enabled but the clock is not, GPIO module seems to be unable
193 * to detect events and generate interrupts at least on OMAP3.
194 */
Victor Kamensky661553b2013-11-16 02:01:04 +0200195 writel_relaxed(0, bank->base + bank->regs->debounce_en);
Grazvydas Ignotas9e303f22012-06-16 22:01:25 +0300196
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300197 clk_disable(bank->dbck);
Tarun Kanti DebBarma72f83af92011-11-24 03:03:28 +0530198 bank->dbck_enabled = false;
199 }
200}
201
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700202/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200203 * omap2_set_gpio_debounce - low level gpio debounce time
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700204 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200205 * @offset: the gpio number on this @bank
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700206 * @debounce: debounce time to use
207 *
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300208 * OMAP's debounce time is in 31us steps
209 * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
210 * so we need to convert and round up to the closest unit.
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700211 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200212static void omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200213 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700214{
Kevin Hilman9942da02011-04-22 12:02:05 -0700215 void __iomem *reg;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700216 u32 val;
217 u32 l;
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300218 bool enable = !!debounce;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700219
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800220 if (!bank->dbck_flag)
221 return;
222
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300223 if (enable) {
224 debounce = DIV_ROUND_UP(debounce, 31) - 1;
225 debounce &= OMAP4_GPIO_DEBOUNCINGTIME_MASK;
226 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700227
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200228 l = BIT(offset);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700229
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300230 clk_enable(bank->dbck);
Kevin Hilman9942da02011-04-22 12:02:05 -0700231 reg = bank->base + bank->regs->debounce;
Victor Kamensky661553b2013-11-16 02:01:04 +0200232 writel_relaxed(debounce, reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700233
Kevin Hilman9942da02011-04-22 12:02:05 -0700234 reg = bank->base + bank->regs->debounce_en;
Victor Kamensky661553b2013-11-16 02:01:04 +0200235 val = readl_relaxed(reg);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700236
Grygorii Strashkoe85ec6c2015-08-18 14:10:54 +0300237 if (enable)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700238 val |= l;
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530239 else
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700240 val &= ~l;
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300241 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700242
Victor Kamensky661553b2013-11-16 02:01:04 +0200243 writel_relaxed(val, reg);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300244 clk_disable(bank->dbck);
Tarun Kanti DebBarma6fd9c422011-11-24 03:58:54 +0530245 /*
246 * Enable debounce clock per module.
247 * This call is mandatory because in omap_gpio_request() when
248 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
249 * runtime callbck fails to turn on dbck because dbck_enable_mask
250 * used within _gpio_dbck_enable() is still not initialized at
251 * that point. Therefore we have to enable dbck here.
252 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200253 omap_gpio_dbck_enable(bank);
Nishanth Menonae547352011-09-09 19:08:58 +0530254 if (bank->dbck_enable_mask) {
255 bank->context.debounce = debounce;
256 bank->context.debounce_en = val;
257 }
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700258}
259
Jon Hunterc9c55d92012-10-26 14:26:04 -0500260/**
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200261 * omap_clear_gpio_debounce - clear debounce settings for a gpio
Jon Hunterc9c55d92012-10-26 14:26:04 -0500262 * @bank: the gpio bank we're acting upon
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200263 * @offset: the gpio number on this @bank
Jon Hunterc9c55d92012-10-26 14:26:04 -0500264 *
265 * If a gpio is using debounce, then clear the debounce enable bit and if
266 * this is the only gpio in this bank using debounce, then clear the debounce
267 * time too. The debounce clock will also be disabled when calling this function
268 * if this is the only gpio in the bank using debounce.
269 */
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200270static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
Jon Hunterc9c55d92012-10-26 14:26:04 -0500271{
Grygorii Strashko4a58d222015-03-23 14:18:25 +0200272 u32 gpio_bit = BIT(offset);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500273
274 if (!bank->dbck_flag)
275 return;
276
277 if (!(bank->dbck_enable_mask & gpio_bit))
278 return;
279
280 bank->dbck_enable_mask &= ~gpio_bit;
281 bank->context.debounce_en &= ~gpio_bit;
Victor Kamensky661553b2013-11-16 02:01:04 +0200282 writel_relaxed(bank->context.debounce_en,
Jon Hunterc9c55d92012-10-26 14:26:04 -0500283 bank->base + bank->regs->debounce_en);
284
285 if (!bank->dbck_enable_mask) {
286 bank->context.debounce = 0;
Victor Kamensky661553b2013-11-16 02:01:04 +0200287 writel_relaxed(bank->context.debounce, bank->base +
Jon Hunterc9c55d92012-10-26 14:26:04 -0500288 bank->regs->debounce);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +0300289 clk_disable(bank->dbck);
Jon Hunterc9c55d92012-10-26 14:26:04 -0500290 bank->dbck_enabled = false;
291 }
292}
293
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200294static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
Tarun Kanti DebBarma00ece7e2011-11-25 15:41:06 +0530295 unsigned trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100296{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800297 void __iomem *base = bank->base;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200298 u32 gpio_bit = BIT(gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100299
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200300 omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
301 trigger & IRQ_TYPE_LEVEL_LOW);
302 omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
303 trigger & IRQ_TYPE_LEVEL_HIGH);
304 omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
305 trigger & IRQ_TYPE_EDGE_RISING);
306 omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
307 trigger & IRQ_TYPE_EDGE_FALLING);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530308
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530309 bank->context.leveldetect0 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200310 readl_relaxed(bank->base + bank->regs->leveldetect0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530311 bank->context.leveldetect1 =
Victor Kamensky661553b2013-11-16 02:01:04 +0200312 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530313 bank->context.risingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200314 readl_relaxed(bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530315 bank->context.fallingdetect =
Victor Kamensky661553b2013-11-16 02:01:04 +0200316 readl_relaxed(bank->base + bank->regs->fallingdetect);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530317
318 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200319 omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530320 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200321 readl_relaxed(bank->base + bank->regs->wkup_en);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530322 }
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530323
Ambresh K55b220c2011-06-15 13:40:45 -0700324 /* This part needs to be executed always for OMAP{34xx, 44xx} */
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530325 if (!bank->regs->irqctrl) {
326 /* On omap24xx proceed only when valid GPIO bit is set */
327 if (bank->non_wakeup_gpios) {
328 if (!(bank->non_wakeup_gpios & gpio_bit))
329 goto exit;
330 }
331
Chunqiu Wang699117a62009-06-24 17:13:39 +0000332 /*
333 * Log the edge gpio and manually trigger the IRQ
334 * after resume if the input level changes
335 * to avoid irq lost during PER RET/OFF mode
336 * Applies for omap2 non-wakeup gpio and all omap3 gpios
337 */
338 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800339 bank->enabled_non_wakeup_gpios |= gpio_bit;
340 else
341 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
342 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700343
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530344exit:
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530345 bank->level_mask =
Victor Kamensky661553b2013-11-16 02:01:04 +0200346 readl_relaxed(bank->base + bank->regs->leveldetect0) |
347 readl_relaxed(bank->base + bank->regs->leveldetect1);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100348}
349
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800350#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800351/*
352 * This only applies to chips that can't do both rising and falling edge
353 * detection at once. For all other chips, this function is a noop.
354 */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200355static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800356{
357 void __iomem *reg = bank->base;
358 u32 l = 0;
359
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530360 if (!bank->regs->irqctrl)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800361 return;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530362
363 reg += bank->regs->irqctrl;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800364
Victor Kamensky661553b2013-11-16 02:01:04 +0200365 l = readl_relaxed(reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800366 if ((l >> gpio) & 1)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200367 l &= ~(BIT(gpio));
Cory Maccarrone4318f362010-01-08 10:29:04 -0800368 else
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200369 l |= BIT(gpio);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800370
Victor Kamensky661553b2013-11-16 02:01:04 +0200371 writel_relaxed(l, reg);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800372}
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530373#else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200374static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800375#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800376
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200377static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
378 unsigned trigger)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100379{
380 void __iomem *reg = bank->base;
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530381 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100382 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100383
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530384 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200385 omap_set_gpio_trigger(bank, gpio, trigger);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530386 } else if (bank->regs->irqctrl) {
387 reg += bank->regs->irqctrl;
388
Victor Kamensky661553b2013-11-16 02:01:04 +0200389 l = readl_relaxed(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000390 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200391 bank->toggle_mask |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100392 if (trigger & IRQ_TYPE_EDGE_RISING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200393 l |= BIT(gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100394 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200395 l &= ~(BIT(gpio));
Tony Lindgren92105bb2005-09-07 17:20:26 +0100396 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530397 return -EINVAL;
398
Victor Kamensky661553b2013-11-16 02:01:04 +0200399 writel_relaxed(l, reg);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530400 } else if (bank->regs->edgectrl1) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100401 if (gpio & 0x08)
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530402 reg += bank->regs->edgectrl2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100403 else
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530404 reg += bank->regs->edgectrl1;
405
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100406 gpio &= 0x07;
Victor Kamensky661553b2013-11-16 02:01:04 +0200407 l = readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100408 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100409 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100410 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100411 if (trigger & IRQ_TYPE_EDGE_FALLING)
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200412 l |= BIT(gpio << 1);
Tarun Kanti DebBarma5e571f32011-09-13 15:02:14 +0530413
414 /* Enable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200415 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +0530416 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200417 readl_relaxed(bank->base + bank->regs->wkup_en);
418 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100419 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100420 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100421}
422
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200423static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200424{
425 if (bank->regs->pinctrl) {
426 void __iomem *reg = bank->base + bank->regs->pinctrl;
427
428 /* Claim the pin for MPU */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200429 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200430 }
431
432 if (bank->regs->ctrl && !BANK_USED(bank)) {
433 void __iomem *reg = bank->base + bank->regs->ctrl;
434 u32 ctrl;
435
Victor Kamensky661553b2013-11-16 02:01:04 +0200436 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200437 /* Module is enabled, clocks are not gated */
438 ctrl &= ~GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200439 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200440 bank->context.ctrl = ctrl;
441 }
442}
443
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200444static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200445{
446 void __iomem *base = bank->base;
447
448 if (bank->regs->wkup_en &&
449 !LINE_USED(bank->mod_usage, offset) &&
450 !LINE_USED(bank->irq_usage, offset)) {
451 /* Disable wake-up during idle for dynamic tick */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200452 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200453 bank->context.wake_en =
Victor Kamensky661553b2013-11-16 02:01:04 +0200454 readl_relaxed(bank->base + bank->regs->wkup_en);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200455 }
456
457 if (bank->regs->ctrl && !BANK_USED(bank)) {
458 void __iomem *reg = bank->base + bank->regs->ctrl;
459 u32 ctrl;
460
Victor Kamensky661553b2013-11-16 02:01:04 +0200461 ctrl = readl_relaxed(reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200462 /* Module is disabled, clocks are gated */
463 ctrl |= GPIO_MOD_CTRL_BIT;
Victor Kamensky661553b2013-11-16 02:01:04 +0200464 writel_relaxed(ctrl, reg);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200465 bank->context.ctrl = ctrl;
466 }
467}
468
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200469static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200470{
471 void __iomem *reg = bank->base + bank->regs->direction;
472
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200473 return readl_relaxed(reg) & BIT(offset);
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200474}
475
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200476static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
Tony Lindgren3d009c82015-01-16 14:50:50 -0800477{
478 if (!LINE_USED(bank->mod_usage, offset)) {
479 omap_enable_gpio_module(bank, offset);
480 omap_set_gpio_direction(bank, offset, 1);
481 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200482 bank->irq_usage |= BIT(offset);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800483}
484
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200485static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100486{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200487 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100488 int retval;
David Brownella6472532008-03-03 04:33:30 -0800489 unsigned long flags;
Grygorii Strashkoea5fbe82015-03-23 14:18:29 +0200490 unsigned offset = d->hwirq;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100491
David Brownelle5c56ed2006-12-06 17:13:59 -0800492 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100493 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800494
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530495 if (!bank->regs->leveldetect0 &&
496 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100497 return -EINVAL;
498
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200499 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200500 retval = omap_set_gpio_triggering(bank, offset, type);
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300501 if (retval) {
Axel Lin627c89b2015-08-05 22:37:41 +0800502 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300503 goto error;
Grygorii Strashko977bd8a2015-06-24 17:54:17 +0300504 }
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200505 omap_gpio_init_irq(bank, offset);
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200506 if (!omap_gpio_is_input(bank, offset)) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200507 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko1562e462015-05-22 17:35:49 +0300508 retval = -EINVAL;
509 goto error;
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200510 }
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200511 raw_spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800512
513 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner43ec2e42015-06-23 15:52:39 +0200514 irq_set_handler_locked(d, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800515 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner43ec2e42015-06-23 15:52:39 +0200516 irq_set_handler_locked(d, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800517
Grygorii Strashko1562e462015-05-22 17:35:49 +0300518 return 0;
519
520error:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100521 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100522}
523
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200524static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100525{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100526 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100527
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700528 reg += bank->regs->irqstatus;
Victor Kamensky661553b2013-11-16 02:01:04 +0200529 writel_relaxed(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300530
531 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700532 if (bank->regs->irqstatus2) {
533 reg = bank->base + bank->regs->irqstatus2;
Victor Kamensky661553b2013-11-16 02:01:04 +0200534 writel_relaxed(gpio_mask, reg);
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700535 }
Roger Quadrosbedfd152009-04-23 11:10:50 -0700536
537 /* Flush posted write for the irq status to avoid spurious interrupts */
Victor Kamensky661553b2013-11-16 02:01:04 +0200538 readl_relaxed(reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100539}
540
Grygorii Strashko9943f262015-03-23 14:18:27 +0200541static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
542 unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100543{
Grygorii Strashko9943f262015-03-23 14:18:27 +0200544 omap_clear_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100545}
546
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200547static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
Imre Deakea6dedd2006-06-26 16:16:00 -0700548{
549 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700550 u32 l;
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200551 u32 mask = (BIT(bank->width)) - 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700552
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700553 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200554 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700555 if (bank->regs->irqenable_inv)
Imre Deak99c47702006-06-26 16:16:07 -0700556 l = ~l;
557 l &= mask;
558 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700559}
560
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200561static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100562{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100563 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564 u32 l;
565
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700566 if (bank->regs->set_irqenable) {
567 reg += bank->regs->set_irqenable;
568 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530569 bank->context.irqenable1 |= gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700570 } else {
571 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200572 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700573 if (bank->regs->irqenable_inv)
574 l &= ~gpio_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100575 else
576 l |= gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530577 bank->context.irqenable1 = l;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578 }
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700579
Victor Kamensky661553b2013-11-16 02:01:04 +0200580 writel_relaxed(l, reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700581}
582
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200583static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700584{
585 void __iomem *reg = bank->base;
586 u32 l;
587
588 if (bank->regs->clr_irqenable) {
589 reg += bank->regs->clr_irqenable;
590 l = gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530591 bank->context.irqenable1 &= ~gpio_mask;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700592 } else {
593 reg += bank->regs->irqenable;
Victor Kamensky661553b2013-11-16 02:01:04 +0200594 l = readl_relaxed(reg);
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700595 if (bank->regs->irqenable_inv)
596 l |= gpio_mask;
597 else
598 l &= ~gpio_mask;
Tarun Kanti DebBarma2a900eb2012-03-06 12:08:16 +0530599 bank->context.irqenable1 = l;
Kevin Hilman28f3b5a2011-04-21 09:53:06 -0700600 }
601
Victor Kamensky661553b2013-11-16 02:01:04 +0200602 writel_relaxed(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100603}
604
Grygorii Strashko9943f262015-03-23 14:18:27 +0200605static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
606 unsigned offset, int enable)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100607{
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530608 if (enable)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200609 omap_enable_gpio_irqbank(bank, BIT(offset));
Tarun Kanti DebBarma8276536c2011-11-25 15:27:37 +0530610 else
Grygorii Strashko9943f262015-03-23 14:18:27 +0200611 omap_disable_gpio_irqbank(bank, BIT(offset));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100612}
613
Tony Lindgren92105bb2005-09-07 17:20:26 +0100614/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200615static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100616{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200617 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100618
Grygorii Strashko0c0451e2016-04-12 13:52:31 +0300619 return irq_set_irq_wake(bank->irq, enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100620}
621
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800622static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100623{
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100624 struct gpio_bank *bank = gpiochip_get_data(chip);
David Brownella6472532008-03-03 04:33:30 -0800625 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100626
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530627 /*
628 * If this is the first gpio_request for the bank,
629 * enable the bank module.
630 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200631 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200632 pm_runtime_get_sync(chip->parent);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100633
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200634 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashkoc3518172015-05-22 17:35:51 +0300635 omap_enable_gpio_module(bank, offset);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200636 bank->mod_usage |= BIT(offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200637 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100638
639 return 0;
640}
641
Jarkko Nikula3ff164e2008-12-10 17:35:27 -0800642static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100643{
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100644 struct gpio_bank *bank = gpiochip_get_data(chip);
David Brownella6472532008-03-03 04:33:30 -0800645 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100646
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200647 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200648 bank->mod_usage &= ~(BIT(offset));
Grygorii Strashko5f982c72015-05-22 17:35:48 +0300649 if (!LINE_USED(bank->irq_usage, offset)) {
650 omap_set_gpio_direction(bank, offset, 1);
651 omap_clear_gpio_debounce(bank, offset);
652 }
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200653 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200654 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +0530655
656 /*
657 * If this is the last gpio to be freed in the bank,
658 * disable the bank module.
659 */
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +0200660 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200661 pm_runtime_put(chip->parent);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100662}
663
664/*
665 * We need to unmask the GPIO bank interrupt as soon as possible to
666 * avoid missing GPIO interrupts for other lines in the bank.
667 * Then we need to mask-read-clear-unmask the triggered GPIO lines
668 * in the bank to avoid missing nested interrupts for a GPIO line.
669 * If we wait to unmask individual GPIO lines in the bank after the
670 * line's interrupt handler has been run, we may miss some nested
671 * interrupts.
672 */
Grygorii Strashko450fa542015-09-25 12:28:03 -0700673static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100674{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100675 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100676 u32 isr;
Jon Hunter3513cde2013-04-04 15:16:14 -0500677 unsigned int bit;
Grygorii Strashko450fa542015-09-25 12:28:03 -0700678 struct gpio_bank *bank = gpiobank;
679 unsigned long wa_lock_flags;
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300680 unsigned long lock_flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100681
Kevin Hilmaneef4bec2011-04-21 09:17:35 -0700682 isr_reg = bank->base + bank->regs->irqstatus;
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800683 if (WARN_ON(!isr_reg))
684 goto exit;
685
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200686 pm_runtime_get_sync(bank->chip.parent);
Grygorii Strashko450fa542015-09-25 12:28:03 -0700687
Laurent Navete83507b2013-03-20 13:15:57 +0100688 while (1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +0100689 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -0700690 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100691
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300692 raw_spin_lock_irqsave(&bank->lock, lock_flags);
693
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200694 enabled = omap_get_gpio_irqbank_mask(bank);
Victor Kamensky661553b2013-11-16 02:01:04 +0200695 isr_saved = isr = readl_relaxed(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100696
Tarun Kanti DebBarma9ea14d82011-08-30 15:05:44 +0530697 if (bank->level_mask)
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800698 level_mask = bank->level_mask & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +0100699
700 /* clear edge sensitive interrupts before handler(s) are
701 called so that we don't miss any interrupt occurred while
702 executing them */
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200703 omap_disable_gpio_irqbank(bank, isr_saved & ~level_mask);
704 omap_clear_gpio_irqbank(bank, isr_saved & ~level_mask);
705 omap_enable_gpio_irqbank(bank, isr_saved & ~level_mask);
Tony Lindgren6e60e792006-04-02 17:46:23 +0100706
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300707 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
708
Tony Lindgren92105bb2005-09-07 17:20:26 +0100709 if (!isr)
710 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100711
Jon Hunter3513cde2013-04-04 15:16:14 -0500712 while (isr) {
713 bit = __ffs(isr);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200714 isr &= ~(BIT(bit));
Benoit Cousson25db7112012-02-23 21:50:10 +0100715
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300716 raw_spin_lock_irqsave(&bank->lock, lock_flags);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800717 /*
718 * Some chips can't respond to both rising and falling
719 * at the same time. If this irq was requested with
720 * both flags, we need to flip the ICR data for the IRQ
721 * to respond to the IRQ for the opposite direction.
722 * This will be indicated in the bank toggle_mask.
723 */
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200724 if (bank->toggle_mask & (BIT(bit)))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200725 omap_toggle_gpio_edge_triggering(bank, bit);
Cory Maccarrone4318f362010-01-08 10:29:04 -0800726
Grygorii Strashko235f1eb2015-08-18 14:10:55 +0300727 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
728
Grygorii Strashko450fa542015-09-25 12:28:03 -0700729 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
730
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +0200731 generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
732 bit));
Grygorii Strashko450fa542015-09-25 12:28:03 -0700733
734 raw_spin_unlock_irqrestore(&bank->wa_lock,
735 wa_lock_flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100736 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000737 }
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -0800738exit:
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200739 pm_runtime_put(bank->chip.parent);
Grygorii Strashko450fa542015-09-25 12:28:03 -0700740 return IRQ_HANDLED;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100741}
742
Tony Lindgren3d009c82015-01-16 14:50:50 -0800743static unsigned int omap_gpio_irq_startup(struct irq_data *d)
744{
745 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800746 unsigned long flags;
Grygorii Strashko37e14ec2015-03-23 14:18:26 +0200747 unsigned offset = d->hwirq;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800748
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200749 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300750
751 if (!LINE_USED(bank->mod_usage, offset))
752 omap_set_gpio_direction(bank, offset, 1);
753 else if (!omap_gpio_is_input(bank, offset))
754 goto err;
755 omap_enable_gpio_module(bank, offset);
756 bank->irq_usage |= BIT(offset);
757
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200758 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren3d009c82015-01-16 14:50:50 -0800759 omap_gpio_unmask_irq(d);
760
761 return 0;
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300762err:
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200763 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashko121dcb72015-05-22 17:35:52 +0300764 return -EINVAL;
Tony Lindgren3d009c82015-01-16 14:50:50 -0800765}
766
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200767static void omap_gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +0300768{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200769 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700770 unsigned long flags;
Grygorii Strashko9943f262015-03-23 14:18:27 +0200771 unsigned offset = d->hwirq;
Tony Lindgren4196dd62006-09-25 12:41:38 +0300772
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200773 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasb1e9fec2014-04-27 02:00:49 +0200774 bank->irq_usage &= ~(BIT(offset));
Grygorii Strashko6e96c1b2015-05-22 17:35:50 +0300775 omap_set_gpio_irqenable(bank, offset, 0);
776 omap_clear_gpio_irqstatus(bank, offset);
777 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
778 if (!LINE_USED(bank->mod_usage, offset))
779 omap_clear_gpio_debounce(bank, offset);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200780 omap_disable_gpio_module(bank, offset);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200781 raw_spin_unlock_irqrestore(&bank->lock, flags);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700782}
783
784static void omap_gpio_irq_bus_lock(struct irq_data *data)
785{
786 struct gpio_bank *bank = omap_irq_data_get_bank(data);
787
788 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200789 pm_runtime_get_sync(bank->chip.parent);
Grygorii Strashkoaca82d12015-09-25 12:28:02 -0700790}
791
792static void gpio_irq_bus_sync_unlock(struct irq_data *data)
793{
794 struct gpio_bank *bank = omap_irq_data_get_bank(data);
Javier Martinez Canillasfac7fa12013-09-25 02:36:54 +0200795
796 /*
797 * If this is the last IRQ to be freed in the bank,
798 * disable the bank module.
799 */
800 if (!BANK_USED(bank))
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +0200801 pm_runtime_put(bank->chip.parent);
Tony Lindgren4196dd62006-09-25 12:41:38 +0300802}
803
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200804static void omap_gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100805{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200806 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200807 unsigned offset = d->hwirq;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100808
Grygorii Strashko9943f262015-03-23 14:18:27 +0200809 omap_clear_gpio_irqstatus(bank, offset);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100810}
811
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200812static void omap_gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100813{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200814 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200815 unsigned offset = d->hwirq;
Colin Cross85ec7b92011-06-06 13:38:18 -0700816 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100817
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200818 raw_spin_lock_irqsave(&bank->lock, flags);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200819 omap_set_gpio_irqenable(bank, offset, 0);
820 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200821 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100822}
823
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200824static void omap_gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100825{
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200826 struct gpio_bank *bank = omap_irq_data_get_bank(d);
Grygorii Strashko9943f262015-03-23 14:18:27 +0200827 unsigned offset = d->hwirq;
Thomas Gleixner8c04a172011-03-24 12:40:15 +0100828 u32 trigger = irqd_get_trigger_type(d);
Colin Cross85ec7b92011-06-06 13:38:18 -0700829 unsigned long flags;
Kevin Hilman55b60192009-06-04 15:57:10 -0700830
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200831 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman55b60192009-06-04 15:57:10 -0700832 if (trigger)
Grygorii Strashko9943f262015-03-23 14:18:27 +0200833 omap_set_gpio_triggering(bank, offset, trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800834
835 /* For level-triggered GPIOs, the clearing must be done after
836 * the HW source is cleared, thus after the handler has run */
Grygorii Strashko9943f262015-03-23 14:18:27 +0200837 if (bank->level_mask & BIT(offset)) {
838 omap_set_gpio_irqenable(bank, offset, 0);
839 omap_clear_gpio_irqstatus(bank, offset);
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800840 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100841
Grygorii Strashko9943f262015-03-23 14:18:27 +0200842 omap_set_gpio_irqenable(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200843 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100844}
845
David Brownelle5c56ed2006-12-06 17:13:59 -0800846/*---------------------------------------------------------------------*/
847
Magnus Damm79ee0312009-07-08 13:22:04 +0200848static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800849{
Magnus Damm79ee0312009-07-08 13:22:04 +0200850 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800851 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800852 void __iomem *mask_reg = bank->base +
853 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800854 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800855
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200856 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200857 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200858 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800859
860 return 0;
861}
862
Magnus Damm79ee0312009-07-08 13:22:04 +0200863static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -0800864{
Magnus Damm79ee0312009-07-08 13:22:04 +0200865 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -0800866 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -0800867 void __iomem *mask_reg = bank->base +
868 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -0800869 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -0800870
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200871 raw_spin_lock_irqsave(&bank->lock, flags);
Victor Kamensky661553b2013-11-16 02:01:04 +0200872 writel_relaxed(bank->context.wake_en, mask_reg);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200873 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -0800874
875 return 0;
876}
877
Alexey Dobriyan47145212009-12-14 18:00:08 -0800878static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +0200879 .suspend_noirq = omap_mpuio_suspend_noirq,
880 .resume_noirq = omap_mpuio_resume_noirq,
881};
882
Rafael J. Wysocki3c437ff2011-04-22 22:02:46 +0200883/* use platform_driver for this. */
David Brownell11a78b72006-12-06 17:14:11 -0800884static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -0800885 .driver = {
886 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +0200887 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -0800888 },
889};
890
891static struct platform_device omap_mpuio_device = {
892 .name = "mpuio",
893 .id = -1,
894 .dev = {
895 .driver = &omap_mpuio_driver.driver,
896 }
897 /* could list the /proc/iomem resources */
898};
899
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200900static inline void omap_mpuio_init(struct gpio_bank *bank)
David Brownell11a78b72006-12-06 17:14:11 -0800901{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800902 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -0700903
David Brownell11a78b72006-12-06 17:14:11 -0800904 if (platform_driver_register(&omap_mpuio_driver) == 0)
905 (void) platform_device_register(&omap_mpuio_device);
906}
907
David Brownelle5c56ed2006-12-06 17:13:59 -0800908/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100909
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200910static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
Yegor Yefremov93700842014-04-24 08:57:39 +0200911{
912 struct gpio_bank *bank;
913 unsigned long flags;
914 void __iomem *reg;
915 int dir;
916
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100917 bank = gpiochip_get_data(chip);
Yegor Yefremov93700842014-04-24 08:57:39 +0200918 reg = bank->base + bank->regs->direction;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200919 raw_spin_lock_irqsave(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +0200920 dir = !!(readl_relaxed(reg) & BIT(offset));
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200921 raw_spin_unlock_irqrestore(&bank->lock, flags);
Yegor Yefremov93700842014-04-24 08:57:39 +0200922 return dir;
923}
924
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200925static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
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);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200932 omap_set_gpio_direction(bank, offset, 1);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200933 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800934 return 0;
935}
936
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200937static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
David Brownell52e31342008-03-03 12:43:23 -0800938{
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300939 struct gpio_bank *bank;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300940
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100941 bank = gpiochip_get_data(chip);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300942
Grygorii Strashkob2b20042015-03-23 14:18:23 +0200943 if (omap_gpio_is_input(bank, offset))
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200944 return omap_get_gpio_datain(bank, offset);
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300945 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200946 return omap_get_gpio_dataout(bank, offset);
David Brownell52e31342008-03-03 12:43:23 -0800947}
948
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200949static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800950{
951 struct gpio_bank *bank;
952 unsigned long flags;
953
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100954 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200955 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700956 bank->set_dataout(bank, offset, value);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200957 omap_set_gpio_direction(bank, offset, 0);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200958 raw_spin_unlock_irqrestore(&bank->lock, flags);
Javier Martinez Canillas2f56e0a2013-10-16 02:47:30 +0200959 return 0;
David Brownell52e31342008-03-03 12:43:23 -0800960}
961
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200962static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
963 unsigned debounce)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700964{
965 struct gpio_bank *bank;
966 unsigned long flags;
967
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100968 bank = gpiochip_get_data(chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800969
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200970 raw_spin_lock_irqsave(&bank->lock, flags);
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200971 omap2_set_gpio_debounce(bank, offset, debounce);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200972 raw_spin_unlock_irqrestore(&bank->lock, flags);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700973
974 return 0;
975}
976
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300977static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset,
978 unsigned long config)
979{
980 u32 debounce;
981
982 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
983 return -ENOTSUPP;
984
985 debounce = pinconf_to_config_argument(config);
986 return omap_gpio_debounce(chip, offset, debounce);
987}
988
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +0200989static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
David Brownell52e31342008-03-03 12:43:23 -0800990{
991 struct gpio_bank *bank;
992 unsigned long flags;
993
Linus Walleijd99f7ae2015-12-07 11:16:00 +0100994 bank = gpiochip_get_data(chip);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200995 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanfa87931a2011-04-20 16:31:23 -0700996 bank->set_dataout(bank, offset, value);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +0200997 raw_spin_unlock_irqrestore(&bank->lock, flags);
David Brownell52e31342008-03-03 12:43:23 -0800998}
999
1000/*---------------------------------------------------------------------*/
1001
Tony Lindgren9a748052010-12-07 16:26:56 -08001002static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001003{
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001004 static bool called;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001005 u32 rev;
1006
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001007 if (called || bank->regs->revision == USHRT_MAX)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001008 return;
1009
Victor Kamensky661553b2013-11-16 02:01:04 +02001010 rev = readw_relaxed(bank->base + bank->regs->revision);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001011 pr_info("OMAP GPIO hardware version %d.%d\n",
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001012 (rev >> 4) & 0x0f, rev & 0x0f);
Kevin Hilmane5ff4442011-04-22 14:37:16 -07001013
1014 called = true;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001015}
1016
Charulatha V03e128c2011-05-05 19:58:01 +05301017static void omap_gpio_mod_init(struct gpio_bank *bank)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001018{
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301019 void __iomem *base = bank->base;
1020 u32 l = 0xffffffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001021
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301022 if (bank->width == 16)
1023 l = 0xffff;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001024
Charulatha Vd0d665a2011-08-31 00:02:21 +05301025 if (bank->is_mpuio) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001026 writel_relaxed(l, bank->base + bank->regs->irqenable);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301027 return;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001028 }
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301029
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001030 omap_gpio_rmw(base, bank->regs->irqenable, l,
1031 bank->regs->irqenable_inv);
1032 omap_gpio_rmw(base, bank->regs->irqstatus, l,
1033 !bank->regs->irqenable_inv);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301034 if (bank->regs->debounce_en)
Victor Kamensky661553b2013-11-16 02:01:04 +02001035 writel_relaxed(0, base + bank->regs->debounce_en);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301036
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301037 /* Save OE default value (0xffffffff) in the context */
Victor Kamensky661553b2013-11-16 02:01:04 +02001038 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301039 /* Initialize interface clk ungated, module enabled */
1040 if (bank->regs->ctrl)
Victor Kamensky661553b2013-11-16 02:01:04 +02001041 writel_relaxed(0, base + bank->regs->ctrl);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001042}
1043
Nishanth Menon46824e222014-09-05 14:52:55 -05001044static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001045{
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001046 static int gpio;
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001047 int irq_base = 0;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001048 int ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001049
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001050 /*
1051 * REVISIT eventually switch from OMAP-specific gpio structs
1052 * over to the generic ones
1053 */
1054 bank->chip.request = omap_gpio_request;
1055 bank->chip.free = omap_gpio_free;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001056 bank->chip.get_direction = omap_gpio_get_direction;
1057 bank->chip.direction_input = omap_gpio_input;
1058 bank->chip.get = omap_gpio_get;
1059 bank->chip.direction_output = omap_gpio_output;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001060 bank->chip.set_config = omap_gpio_set_config;
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001061 bank->chip.set = omap_gpio_set;
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 {
1068 bank->chip.label = "gpio";
1069 bank->chip.base = gpio;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001070 }
Kevin Hilmand5f46242011-04-21 09:23:00 -07001071 bank->chip.ngpio = bank->width;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001072
Linus Walleijd99f7ae2015-12-07 11:16:00 +01001073 ret = gpiochip_add_data(&bank->chip, bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001074 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001075 dev_err(bank->chip.parent,
1076 "Could not register gpio chip %d\n", ret);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001077 return ret;
1078 }
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001079
Tony Lindgren46d4f7c2015-09-03 10:31:27 -07001080 if (!bank->is_mpuio)
1081 gpio += bank->width;
1082
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001083#ifdef CONFIG_ARCH_OMAP1
1084 /*
1085 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1086 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1087 */
Bartosz Golaszewski2ed36f32017-03-04 17:23:31 +01001088 irq_base = devm_irq_alloc_descs(bank->chip.parent,
1089 -1, 0, bank->width, 0);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001090 if (irq_base < 0) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001091 dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n");
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001092 return -ENODEV;
1093 }
1094#endif
1095
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001096 /* MPUIO is a bit different, reading IRQ status clears it */
1097 if (bank->is_mpuio) {
1098 irqc->irq_ack = dummy_irq_chip.irq_ack;
Tony Lindgrend2d05c62015-04-23 16:54:17 -07001099 if (!bank->regs->wkup_en)
1100 irqc->irq_set_wake = NULL;
1101 }
1102
Nishanth Menon46824e222014-09-05 14:52:55 -05001103 ret = gpiochip_irqchip_add(&bank->chip, irqc,
Grygorii Strashko450fa542015-09-25 12:28:03 -07001104 irq_base, handle_bad_irq,
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001105 IRQ_TYPE_NONE);
1106
1107 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001108 dev_err(bank->chip.parent,
1109 "Couldn't add irqchip to gpiochip %d\n", ret);
Linus Walleijda26d5d2014-09-16 15:11:41 -07001110 gpiochip_remove(&bank->chip);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001111 return -ENODEV;
1112 }
1113
Grygorii Strashko450fa542015-09-25 12:28:03 -07001114 gpiochip_set_chained_irqchip(&bank->chip, irqc, bank->irq, NULL);
Javier Martinez Canillasfb655f52014-04-06 16:58:16 +02001115
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001116 ret = devm_request_irq(bank->chip.parent, bank->irq,
1117 omap_gpio_irq_handler,
1118 0, dev_name(bank->chip.parent), bank);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001119 if (ret)
1120 gpiochip_remove(&bank->chip);
1121
1122 return ret;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001123}
1124
Benoit Cousson384ebe12011-08-16 11:53:02 +02001125static const struct of_device_id omap_gpio_match[];
1126
Bill Pemberton38363092012-11-19 13:22:34 -05001127static int omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001128{
Benoit Cousson862ff642012-02-01 15:58:56 +01001129 struct device *dev = &pdev->dev;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001130 struct device_node *node = dev->of_node;
1131 const struct of_device_id *match;
Uwe Kleine-Königf6817a22012-05-21 21:57:39 +02001132 const struct omap_gpio_platform_data *pdata;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001133 struct resource *res;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001134 struct gpio_bank *bank;
Nishanth Menon46824e222014-09-05 14:52:55 -05001135 struct irq_chip *irqc;
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001136 int ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001137
Benoit Cousson384ebe12011-08-16 11:53:02 +02001138 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1139
Jingoo Hane56aee12013-07-30 17:08:05 +09001140 pdata = match ? match->data : dev_get_platdata(dev);
Benoit Cousson384ebe12011-08-16 11:53:02 +02001141 if (!pdata)
Benoit Cousson96751fc2012-02-01 16:01:39 +01001142 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001143
Tobias Klauser086d5852012-10-05 11:37:38 +02001144 bank = devm_kzalloc(dev, sizeof(struct gpio_bank), GFP_KERNEL);
Charulatha V03e128c2011-05-05 19:58:01 +05301145 if (!bank) {
Benoit Cousson862ff642012-02-01 15:58:56 +01001146 dev_err(dev, "Memory alloc failed\n");
Benoit Cousson96751fc2012-02-01 16:01:39 +01001147 return -ENOMEM;
Charulatha V03e128c2011-05-05 19:58:01 +05301148 }
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001149
Nishanth Menon46824e222014-09-05 14:52:55 -05001150 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1151 if (!irqc)
1152 return -ENOMEM;
1153
Tony Lindgren3d009c82015-01-16 14:50:50 -08001154 irqc->irq_startup = omap_gpio_irq_startup,
Nishanth Menon46824e222014-09-05 14:52:55 -05001155 irqc->irq_shutdown = omap_gpio_irq_shutdown,
1156 irqc->irq_ack = omap_gpio_ack_irq,
1157 irqc->irq_mask = omap_gpio_mask_irq,
1158 irqc->irq_unmask = omap_gpio_unmask_irq,
1159 irqc->irq_set_type = omap_gpio_irq_type,
1160 irqc->irq_set_wake = omap_gpio_wake_enable,
Grygorii Strashkoaca82d12015-09-25 12:28:02 -07001161 irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
1162 irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
Nishanth Menon46824e222014-09-05 14:52:55 -05001163 irqc->name = dev_name(&pdev->dev);
Grygorii Strashko0c0451e2016-04-12 13:52:31 +03001164 irqc->flags = IRQCHIP_MASK_ON_SUSPEND;
Nishanth Menon46824e222014-09-05 14:52:55 -05001165
Grygorii Strashko89d18e32015-08-18 14:10:53 +03001166 bank->irq = platform_get_irq(pdev, 0);
1167 if (bank->irq <= 0) {
1168 if (!bank->irq)
1169 bank->irq = -ENXIO;
1170 if (bank->irq != -EPROBE_DEFER)
1171 dev_err(dev,
1172 "can't get irq resource ret=%d\n", bank->irq);
1173 return bank->irq;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001174 }
1175
Linus Walleij58383c782015-11-04 09:56:26 +01001176 bank->chip.parent = dev;
Grygorii Strashkoc23837c2015-06-25 18:13:33 +03001177 bank->chip.owner = THIS_MODULE;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001178 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001179 bank->stride = pdata->bank_stride;
Kevin Hilmand5f46242011-04-21 09:23:00 -07001180 bank->width = pdata->bank_width;
Charulatha Vd0d665a2011-08-31 00:02:21 +05301181 bank->is_mpuio = pdata->is_mpuio;
Charulatha V803a2432011-05-05 17:04:12 +05301182 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001183 bank->regs = pdata->regs;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001184#ifdef CONFIG_OF_GPIO
1185 bank->chip.of_node = of_node_get(node);
1186#endif
Jon Huntera2797be2013-04-04 15:16:15 -05001187 if (node) {
1188 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1189 bank->loses_context = true;
1190 } else {
1191 bank->loses_context = pdata->loses_context;
Jon Hunter352a2d52013-04-15 13:06:54 -05001192
1193 if (bank->loses_context)
1194 bank->get_context_loss_count =
1195 pdata->get_context_loss_count;
Benoit Cousson384ebe12011-08-16 11:53:02 +02001196 }
1197
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001198 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001199 bank->set_dataout = omap_set_gpio_dataout_reg;
Kevin Hilmanfa87931a2011-04-20 16:31:23 -07001200 else
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001201 bank->set_dataout = omap_set_gpio_dataout_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001202
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001203 raw_spin_lock_init(&bank->lock);
Grygorii Strashko450fa542015-09-25 12:28:03 -07001204 raw_spin_lock_init(&bank->wa_lock);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001205
1206 /* Static mapping, never released */
1207 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han717f70e2014-02-12 11:51:38 +09001208 bank->base = devm_ioremap_resource(dev, res);
1209 if (IS_ERR(bank->base)) {
Jingoo Han717f70e2014-02-12 11:51:38 +09001210 return PTR_ERR(bank->base);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001211 }
1212
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001213 if (bank->dbck_flag) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001214 bank->dbck = devm_clk_get(dev, "dbclk");
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001215 if (IS_ERR(bank->dbck)) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001216 dev_err(dev,
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001217 "Could not get gpio dbck. Disable debounce\n");
1218 bank->dbck_flag = false;
1219 } else {
1220 clk_prepare(bank->dbck);
1221 }
1222 }
1223
Tarun Kanti DebBarma065cd792011-11-24 01:48:52 +05301224 platform_set_drvdata(pdev, bank);
1225
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001226 pm_runtime_enable(dev);
1227 pm_runtime_irq_safe(dev);
1228 pm_runtime_get_sync(dev);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001229
Charulatha Vd0d665a2011-08-31 00:02:21 +05301230 if (bank->is_mpuio)
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001231 omap_mpuio_init(bank);
Tarun Kanti DebBarmaab985f02011-09-13 15:12:05 +05301232
Charulatha V03e128c2011-05-05 19:58:01 +05301233 omap_gpio_mod_init(bank);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001234
Nishanth Menon46824e222014-09-05 14:52:55 -05001235 ret = omap_gpio_chip_init(bank, irqc);
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001236 if (ret) {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001237 pm_runtime_put_sync(dev);
1238 pm_runtime_disable(dev);
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001239 return ret;
Tony Lindgren5e606ab2015-08-28 11:44:49 -07001240 }
Javier Martinez Canillas6ef7f382014-04-06 16:58:14 +02001241
Tony Lindgren9a748052010-12-07 16:26:56 -08001242 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001243
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001244 pm_runtime_put(dev);
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301245
Charulatha V03e128c2011-05-05 19:58:01 +05301246 list_add_tail(&bank->node, &omap_gpio_list);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001247
Jon Hunter879fe322013-04-04 15:16:12 -05001248 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001249}
1250
Tony Lindgrencac089f2015-04-23 16:56:22 -07001251static int omap_gpio_remove(struct platform_device *pdev)
1252{
1253 struct gpio_bank *bank = platform_get_drvdata(pdev);
1254
1255 list_del(&bank->node);
1256 gpiochip_remove(&bank->chip);
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001257 pm_runtime_disable(&pdev->dev);
Grygorii Strashko5d9452e2015-08-18 14:10:56 +03001258 if (bank->dbck_flag)
1259 clk_unprepare(bank->dbck);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001260
1261 return 0;
1262}
1263
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301264#ifdef CONFIG_ARCH_OMAP2PLUS
1265
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001266#if defined(CONFIG_PM)
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301267static void omap_gpio_restore_context(struct gpio_bank *bank);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001268
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301269static int omap_gpio_runtime_suspend(struct device *dev)
1270{
1271 struct platform_device *pdev = to_platform_device(dev);
1272 struct gpio_bank *bank = platform_get_drvdata(pdev);
1273 u32 l1 = 0, l2 = 0;
1274 unsigned long flags;
Kevin Hilman68942ed2012-03-05 15:10:04 -08001275 u32 wake_low, wake_hi;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301276
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001277 raw_spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001278
1279 /*
1280 * Only edges can generate a wakeup event to the PRCM.
1281 *
1282 * Therefore, ensure any wake-up capable GPIOs have
1283 * edge-detection enabled before going idle to ensure a wakeup
1284 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1285 * NDA TRM 25.5.3.1)
1286 *
1287 * The normal values will be restored upon ->runtime_resume()
1288 * by writing back the values saved in bank->context.
1289 */
1290 wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1291 if (wake_low)
Victor Kamensky661553b2013-11-16 02:01:04 +02001292 writel_relaxed(wake_low | bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001293 bank->base + bank->regs->fallingdetect);
1294 wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1295 if (wake_hi)
Victor Kamensky661553b2013-11-16 02:01:04 +02001296 writel_relaxed(wake_hi | bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001297 bank->base + bank->regs->risingdetect);
1298
Kevin Hilmanb3c64bc2012-05-17 16:42:16 -07001299 if (!bank->enabled_non_wakeup_gpios)
1300 goto update_gpio_context_count;
1301
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301302 if (bank->power_mode != OFF_MODE) {
1303 bank->power_mode = 0;
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301304 goto update_gpio_context_count;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301305 }
1306 /*
1307 * If going to OFF, remove triggering for all
1308 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1309 * generated. See OMAP2420 Errata item 1.101.
1310 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001311 bank->saved_datain = readl_relaxed(bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301312 bank->regs->datain);
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301313 l1 = bank->context.fallingdetect;
1314 l2 = bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301315
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301316 l1 &= ~bank->enabled_non_wakeup_gpios;
1317 l2 &= ~bank->enabled_non_wakeup_gpios;
1318
Victor Kamensky661553b2013-11-16 02:01:04 +02001319 writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1320 writel_relaxed(l2, bank->base + bank->regs->risingdetect);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301321
1322 bank->workaround_enabled = true;
1323
Tarun Kanti DebBarma41d87cb2011-11-15 12:52:38 +05301324update_gpio_context_count:
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301325 if (bank->get_context_loss_count)
1326 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001327 bank->get_context_loss_count(dev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301328
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001329 omap_gpio_dbck_disable(bank);
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001330 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301331
1332 return 0;
1333}
1334
Jon Hunter352a2d52013-04-15 13:06:54 -05001335static void omap_gpio_init_context(struct gpio_bank *p);
1336
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301337static int omap_gpio_runtime_resume(struct device *dev)
1338{
1339 struct platform_device *pdev = to_platform_device(dev);
1340 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301341 u32 l = 0, gen, gen0, gen1;
1342 unsigned long flags;
Jon Huntera2797be2013-04-04 15:16:15 -05001343 int c;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301344
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001345 raw_spin_lock_irqsave(&bank->lock, flags);
Jon Hunter352a2d52013-04-15 13:06:54 -05001346
1347 /*
1348 * On the first resume during the probe, the context has not
1349 * been initialised and so initialise it now. Also initialise
1350 * the context loss count.
1351 */
1352 if (bank->loses_context && !bank->context_valid) {
1353 omap_gpio_init_context(bank);
1354
1355 if (bank->get_context_loss_count)
1356 bank->context_loss_count =
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001357 bank->get_context_loss_count(dev);
Jon Hunter352a2d52013-04-15 13:06:54 -05001358 }
1359
Javier Martinez Canillasa0e827c2014-06-27 22:17:37 +02001360 omap_gpio_dbck_enable(bank);
Kevin Hilman68942ed2012-03-05 15:10:04 -08001361
1362 /*
1363 * In ->runtime_suspend(), level-triggered, wakeup-enabled
1364 * GPIOs were set to edge trigger also in order to be able to
1365 * generate a PRCM wakeup. Here we restore the
1366 * pre-runtime_suspend() values for edge triggering.
1367 */
Victor Kamensky661553b2013-11-16 02:01:04 +02001368 writel_relaxed(bank->context.fallingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001369 bank->base + bank->regs->fallingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001370 writel_relaxed(bank->context.risingdetect,
Kevin Hilman68942ed2012-03-05 15:10:04 -08001371 bank->base + bank->regs->risingdetect);
1372
Jon Huntera2797be2013-04-04 15:16:15 -05001373 if (bank->loses_context) {
1374 if (!bank->get_context_loss_count) {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301375 omap_gpio_restore_context(bank);
1376 } else {
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001377 c = bank->get_context_loss_count(dev);
Jon Huntera2797be2013-04-04 15:16:15 -05001378 if (c != bank->context_loss_count) {
1379 omap_gpio_restore_context(bank);
1380 } else {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001381 raw_spin_unlock_irqrestore(&bank->lock, flags);
Jon Huntera2797be2013-04-04 15:16:15 -05001382 return 0;
1383 }
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301384 }
1385 }
1386
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301387 if (!bank->workaround_enabled) {
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001388 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma1b1287032012-04-27 19:43:38 +05301389 return 0;
1390 }
1391
Victor Kamensky661553b2013-11-16 02:01:04 +02001392 l = readl_relaxed(bank->base + bank->regs->datain);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301393
1394 /*
1395 * Check if any of the non-wakeup interrupt GPIOs have changed
1396 * state. If so, generate an IRQ by software. This is
1397 * horribly racy, but it's the best we can do to work around
1398 * this silicon bug.
1399 */
1400 l ^= bank->saved_datain;
1401 l &= bank->enabled_non_wakeup_gpios;
1402
1403 /*
1404 * No need to generate IRQs for the rising edge for gpio IRQs
1405 * configured with falling edge only; and vice versa.
1406 */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301407 gen0 = l & bank->context.fallingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301408 gen0 &= bank->saved_datain;
1409
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301410 gen1 = l & bank->context.risingdetect;
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301411 gen1 &= ~(bank->saved_datain);
1412
1413 /* FIXME: Consider GPIO IRQs with level detections properly! */
Tarun Kanti DebBarmac6f31c92012-04-27 19:43:32 +05301414 gen = l & (~(bank->context.fallingdetect) &
1415 ~(bank->context.risingdetect));
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301416 /* Consider all GPIO IRQs needed to be updated */
1417 gen |= gen0 | gen1;
1418
1419 if (gen) {
1420 u32 old0, old1;
1421
Victor Kamensky661553b2013-11-16 02:01:04 +02001422 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1423 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301424
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301425 if (!bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001426 writel_relaxed(old0 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301427 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001428 writel_relaxed(old1 | gen, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301429 bank->regs->leveldetect1);
1430 }
1431
Tarun Kanti DebBarma4e962e82012-04-27 19:43:37 +05301432 if (bank->regs->irqstatus_raw0) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001433 writel_relaxed(old0 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301434 bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001435 writel_relaxed(old1 | l, bank->base +
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301436 bank->regs->leveldetect1);
1437 }
Victor Kamensky661553b2013-11-16 02:01:04 +02001438 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1439 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301440 }
1441
1442 bank->workaround_enabled = false;
Sebastian Andrzej Siewior4dbada22015-07-21 18:26:51 +02001443 raw_spin_unlock_irqrestore(&bank->lock, flags);
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301444
1445 return 0;
1446}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001447#endif /* CONFIG_PM */
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301448
Tony Lindgrencac089f2015-04-23 16:56:22 -07001449#if IS_BUILTIN(CONFIG_GPIO_OMAP)
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301450void omap2_gpio_prepare_for_idle(int pwr_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001451{
Charulatha V03e128c2011-05-05 19:58:01 +05301452 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001453
Charulatha V03e128c2011-05-05 19:58:01 +05301454 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001455 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301456 continue;
1457
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301458 bank->power_mode = pwr_mode;
1459
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001460 pm_runtime_put_sync_suspend(bank->chip.parent);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001461 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001462}
1463
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001464void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001465{
Charulatha V03e128c2011-05-05 19:58:01 +05301466 struct gpio_bank *bank;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001467
Charulatha V03e128c2011-05-05 19:58:01 +05301468 list_for_each_entry(bank, &omap_gpio_list, node) {
Javier Martinez Canillasfa365e42013-09-25 02:36:52 +02001469 if (!BANK_USED(bank) || !bank->loses_context)
Charulatha V03e128c2011-05-05 19:58:01 +05301470 continue;
1471
Grygorii Strashko7b1e5dc2016-03-04 17:25:35 +02001472 pm_runtime_get_sync(bank->chip.parent);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001473 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001474}
Tony Lindgrencac089f2015-04-23 16:56:22 -07001475#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001476
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001477#if defined(CONFIG_PM)
Jon Hunter352a2d52013-04-15 13:06:54 -05001478static void omap_gpio_init_context(struct gpio_bank *p)
1479{
1480 struct omap_gpio_reg_offs *regs = p->regs;
1481 void __iomem *base = p->base;
1482
Victor Kamensky661553b2013-11-16 02:01:04 +02001483 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1484 p->context.oe = readl_relaxed(base + regs->direction);
1485 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1486 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1487 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1488 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1489 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1490 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1491 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
Jon Hunter352a2d52013-04-15 13:06:54 -05001492
1493 if (regs->set_dataout && p->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001494 p->context.dataout = readl_relaxed(base + regs->set_dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001495 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001496 p->context.dataout = readl_relaxed(base + regs->dataout);
Jon Hunter352a2d52013-04-15 13:06:54 -05001497
1498 p->context_valid = true;
1499}
1500
Tarun Kanti DebBarma60a34372011-09-29 04:47:25 +05301501static void omap_gpio_restore_context(struct gpio_bank *bank)
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301502{
Victor Kamensky661553b2013-11-16 02:01:04 +02001503 writel_relaxed(bank->context.wake_en,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301504 bank->base + bank->regs->wkup_en);
Victor Kamensky661553b2013-11-16 02:01:04 +02001505 writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1506 writel_relaxed(bank->context.leveldetect0,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301507 bank->base + bank->regs->leveldetect0);
Victor Kamensky661553b2013-11-16 02:01:04 +02001508 writel_relaxed(bank->context.leveldetect1,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301509 bank->base + bank->regs->leveldetect1);
Victor Kamensky661553b2013-11-16 02:01:04 +02001510 writel_relaxed(bank->context.risingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301511 bank->base + bank->regs->risingdetect);
Victor Kamensky661553b2013-11-16 02:01:04 +02001512 writel_relaxed(bank->context.fallingdetect,
Tarun Kanti DebBarmaae10f232011-08-30 15:24:27 +05301513 bank->base + bank->regs->fallingdetect);
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301514 if (bank->regs->set_dataout && bank->regs->clr_dataout)
Victor Kamensky661553b2013-11-16 02:01:04 +02001515 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301516 bank->base + bank->regs->set_dataout);
1517 else
Victor Kamensky661553b2013-11-16 02:01:04 +02001518 writel_relaxed(bank->context.dataout,
Nishanth Menonf86bcc32011-09-09 19:14:08 +05301519 bank->base + bank->regs->dataout);
Victor Kamensky661553b2013-11-16 02:01:04 +02001520 writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
Nishanth Menon6d13eaa2011-08-29 18:54:50 +05301521
Nishanth Menonae547352011-09-09 19:08:58 +05301522 if (bank->dbck_enable_mask) {
Victor Kamensky661553b2013-11-16 02:01:04 +02001523 writel_relaxed(bank->context.debounce, bank->base +
Nishanth Menonae547352011-09-09 19:08:58 +05301524 bank->regs->debounce);
Victor Kamensky661553b2013-11-16 02:01:04 +02001525 writel_relaxed(bank->context.debounce_en,
Nishanth Menonae547352011-09-09 19:08:58 +05301526 bank->base + bank->regs->debounce_en);
1527 }
Nishanth Menonba805be2011-08-29 18:41:08 +05301528
Victor Kamensky661553b2013-11-16 02:01:04 +02001529 writel_relaxed(bank->context.irqenable1,
Nishanth Menonba805be2011-08-29 18:41:08 +05301530 bank->base + bank->regs->irqenable);
Victor Kamensky661553b2013-11-16 02:01:04 +02001531 writel_relaxed(bank->context.irqenable2,
Nishanth Menonba805be2011-08-29 18:41:08 +05301532 bank->base + bank->regs->irqenable2);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301533}
Rafael J. Wysockiecb23122014-12-04 01:03:40 +01001534#endif /* CONFIG_PM */
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301535#else
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301536#define omap_gpio_runtime_suspend NULL
1537#define omap_gpio_runtime_resume NULL
Arnd Bergmannea4a21a2013-05-31 17:59:46 +02001538static inline void omap_gpio_init_context(struct gpio_bank *p) {}
Rajendra Nayak40c670f2008-09-26 17:47:48 +05301539#endif
1540
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301541static const struct dev_pm_ops gpio_pm_ops = {
Tarun Kanti DebBarma2dc983c2011-11-24 02:44:29 +05301542 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1543 NULL)
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301544};
1545
Benoit Cousson384ebe12011-08-16 11:53:02 +02001546#if defined(CONFIG_OF)
1547static struct omap_gpio_reg_offs omap2_gpio_regs = {
1548 .revision = OMAP24XX_GPIO_REVISION,
1549 .direction = OMAP24XX_GPIO_OE,
1550 .datain = OMAP24XX_GPIO_DATAIN,
1551 .dataout = OMAP24XX_GPIO_DATAOUT,
1552 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1553 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1554 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1555 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1556 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1557 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1558 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1559 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1560 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1561 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1562 .ctrl = OMAP24XX_GPIO_CTRL,
1563 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1564 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1565 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1566 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1567 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1568};
1569
1570static struct omap_gpio_reg_offs omap4_gpio_regs = {
1571 .revision = OMAP4_GPIO_REVISION,
1572 .direction = OMAP4_GPIO_OE,
1573 .datain = OMAP4_GPIO_DATAIN,
1574 .dataout = OMAP4_GPIO_DATAOUT,
1575 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1576 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1577 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1578 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
1579 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1580 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1581 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1582 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1583 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1584 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1585 .ctrl = OMAP4_GPIO_CTRL,
1586 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1587 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1588 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1589 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1590 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1591};
1592
Chen Gange9a65bb2013-02-06 18:44:32 +08001593static const struct omap_gpio_platform_data omap2_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001594 .regs = &omap2_gpio_regs,
1595 .bank_width = 32,
1596 .dbck_flag = false,
1597};
1598
Chen Gange9a65bb2013-02-06 18:44:32 +08001599static const struct omap_gpio_platform_data omap3_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001600 .regs = &omap2_gpio_regs,
1601 .bank_width = 32,
1602 .dbck_flag = true,
1603};
1604
Chen Gange9a65bb2013-02-06 18:44:32 +08001605static const struct omap_gpio_platform_data omap4_pdata = {
Benoit Cousson384ebe12011-08-16 11:53:02 +02001606 .regs = &omap4_gpio_regs,
1607 .bank_width = 32,
1608 .dbck_flag = true,
1609};
1610
1611static const struct of_device_id omap_gpio_match[] = {
1612 {
1613 .compatible = "ti,omap4-gpio",
1614 .data = &omap4_pdata,
1615 },
1616 {
1617 .compatible = "ti,omap3-gpio",
1618 .data = &omap3_pdata,
1619 },
1620 {
1621 .compatible = "ti,omap2-gpio",
1622 .data = &omap2_pdata,
1623 },
1624 { },
1625};
1626MODULE_DEVICE_TABLE(of, omap_gpio_match);
1627#endif
1628
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001629static struct platform_driver omap_gpio_driver = {
1630 .probe = omap_gpio_probe,
Tony Lindgrencac089f2015-04-23 16:56:22 -07001631 .remove = omap_gpio_remove,
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001632 .driver = {
1633 .name = "omap_gpio",
Tarun Kanti DebBarma55b93c32011-09-29 07:23:22 +05301634 .pm = &gpio_pm_ops,
Benoit Cousson384ebe12011-08-16 11:53:02 +02001635 .of_match_table = of_match_ptr(omap_gpio_match),
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001636 },
1637};
1638
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001639/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001640 * gpio driver register needs to be done before
1641 * machine_init functions access gpio APIs.
1642 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001643 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001644static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001645{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001646 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001647}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001648postcore_initcall(omap_gpio_drv_reg);
Tony Lindgrencac089f2015-04-23 16:56:22 -07001649
1650static void __exit omap_gpio_exit(void)
1651{
1652 platform_driver_unregister(&omap_gpio_driver);
1653}
1654module_exit(omap_gpio_exit);
1655
1656MODULE_DESCRIPTION("omap gpio driver");
1657MODULE_ALIAS("platform:gpio-omap");
1658MODULE_LICENSE("GPL v2");