gpio: omap: irq_startup() must not return error codes
[linux-2.6-block.git] / drivers / gpio / gpio-omap.c
CommitLineData
5e1c5ff4 1/*
5e1c5ff4
TL
2 * Support functions for OMAP GPIO
3 *
92105bb7 4 * Copyright (C) 2003-2005 Nokia Corporation
96de0e25 5 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
5e1c5ff4 6 *
44169075
SS
7 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9 *
5e1c5ff4
TL
10 * 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
5e1c5ff4
TL
15#include <linux/init.h>
16#include <linux/module.h>
5e1c5ff4 17#include <linux/interrupt.h>
3c437ffd 18#include <linux/syscore_ops.h>
92105bb7 19#include <linux/err.h>
f8ce2547 20#include <linux/clk.h>
fced80c7 21#include <linux/io.h>
b764a586 22#include <linux/cpu_pm.h>
96751fcb 23#include <linux/device.h>
77640aab 24#include <linux/pm_runtime.h>
55b93c32 25#include <linux/pm.h>
384ebe1c
BC
26#include <linux/of.h>
27#include <linux/of_device.h>
b7351b07 28#include <linux/gpio/driver.h>
9370084e 29#include <linux/bitops.h>
4b25408f 30#include <linux/platform_data/gpio-omap.h>
5e1c5ff4 31
e85ec6c3 32#define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF
2dc983c5 33
6d62e216
C
34struct gpio_regs {
35 u32 irqenable1;
36 u32 irqenable2;
37 u32 wake_en;
38 u32 ctrl;
39 u32 oe;
40 u32 leveldetect0;
41 u32 leveldetect1;
42 u32 risingdetect;
43 u32 fallingdetect;
44 u32 dataout;
ae547354
NM
45 u32 debounce;
46 u32 debounce_en;
6d62e216
C
47};
48
5e1c5ff4 49struct gpio_bank {
92105bb7 50 void __iomem *base;
18bd49c4
RK
51 const struct omap_gpio_reg_offs *regs;
52
30cefeac 53 int irq;
3ac4fa99
JY
54 u32 non_wakeup_gpios;
55 u32 enabled_non_wakeup_gpios;
6d62e216 56 struct gpio_regs context;
3ac4fa99 57 u32 saved_datain;
b144ff6f 58 u32 level_mask;
4318f36b 59 u32 toggle_mask;
4dbada2b 60 raw_spinlock_t lock;
450fa54c 61 raw_spinlock_t wa_lock;
52e31344 62 struct gpio_chip chip;
89db9482 63 struct clk *dbck;
b764a586
TL
64 struct notifier_block nb;
65 unsigned int is_suspended:1;
058af1ea 66 u32 mod_usage;
fa365e4d 67 u32 irq_usage;
8865b9b6 68 u32 dbck_enable_mask;
72f83af9 69 bool dbck_enabled;
d0d665a8 70 bool is_mpuio;
77640aab 71 bool dbck_flag;
0cde8d03 72 bool loses_context;
352a2d5b 73 bool context_valid;
5de62b86 74 int stride;
d5f46247 75 u32 width;
60a3437d 76 int context_loss_count;
fa87931a 77
04ebcbd8 78 void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable);
60a3437d 79 int (*get_context_loss_count)(struct device *dev);
5e1c5ff4
TL
80};
81
c8eef65a 82#define GPIO_MOD_CTRL_BIT BIT(0)
5e1c5ff4 83
fa365e4d 84#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
b1e9fec2 85#define LINE_USED(line, offset) (line & (BIT(offset)))
fa365e4d 86
3d009c8c
TL
87static void omap_gpio_unmask_irq(struct irq_data *d);
88
a0e827c6 89static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
ede4d7a5 90{
fb655f57 91 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
d99f7aec 92 return gpiochip_get_data(chip);
25db711d
BC
93}
94
8ee1de65 95static inline u32 omap_gpio_rmw(void __iomem *reg, u32 mask, bool set)
5e1c5ff4 96{
8ee1de65 97 u32 val = readl_relaxed(reg);
5e1c5ff4 98
8ee1de65
RK
99 if (set)
100 val |= mask;
5e1c5ff4 101 else
8ee1de65
RK
102 val &= ~mask;
103
104 writel_relaxed(val, reg);
105
106 return val;
107}
108
109static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
110 int is_input)
111{
112 bank->context.oe = omap_gpio_rmw(bank->base + bank->regs->direction,
113 BIT(gpio), is_input);
5e1c5ff4
TL
114}
115
fa87931a
KH
116
117/* set data out value using dedicate set/clear register */
04ebcbd8 118static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
a0e827c6 119 int enable)
5e1c5ff4 120{
92105bb7 121 void __iomem *reg = bank->base;
04ebcbd8 122 u32 l = BIT(offset);
5e1c5ff4 123
2c836f7e 124 if (enable) {
fa87931a 125 reg += bank->regs->set_dataout;
2c836f7e
TKD
126 bank->context.dataout |= l;
127 } else {
fa87931a 128 reg += bank->regs->clr_dataout;
2c836f7e
TKD
129 bank->context.dataout &= ~l;
130 }
5e1c5ff4 131
661553b9 132 writel_relaxed(l, reg);
5e1c5ff4
TL
133}
134
fa87931a 135/* set data out value using mask register */
04ebcbd8 136static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
a0e827c6 137 int enable)
5e1c5ff4 138{
8ee1de65
RK
139 bank->context.dataout = omap_gpio_rmw(bank->base + bank->regs->dataout,
140 BIT(offset), enable);
ece9528e 141}
92105bb7 142
a0e827c6 143static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
72f83af9
TKD
144{
145 if (bank->dbck_enable_mask && !bank->dbck_enabled) {
5d9452e7 146 clk_enable(bank->dbck);
72f83af9 147 bank->dbck_enabled = true;
9e303f22 148
661553b9 149 writel_relaxed(bank->dbck_enable_mask,
9e303f22 150 bank->base + bank->regs->debounce_en);
72f83af9
TKD
151 }
152}
153
a0e827c6 154static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
72f83af9
TKD
155{
156 if (bank->dbck_enable_mask && bank->dbck_enabled) {
9e303f22
GI
157 /*
158 * Disable debounce before cutting it's clock. If debounce is
159 * enabled but the clock is not, GPIO module seems to be unable
160 * to detect events and generate interrupts at least on OMAP3.
161 */
661553b9 162 writel_relaxed(0, bank->base + bank->regs->debounce_en);
9e303f22 163
5d9452e7 164 clk_disable(bank->dbck);
72f83af9
TKD
165 bank->dbck_enabled = false;
166 }
167}
168
168ef3d9 169/**
a0e827c6 170 * omap2_set_gpio_debounce - low level gpio debounce time
168ef3d9 171 * @bank: the gpio bank we're acting upon
4a58d229 172 * @offset: the gpio number on this @bank
168ef3d9
FB
173 * @debounce: debounce time to use
174 *
e85ec6c3
GS
175 * OMAP's debounce time is in 31us steps
176 * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
177 * so we need to convert and round up to the closest unit.
83977443
DR
178 *
179 * Return: 0 on success, negative error otherwise.
168ef3d9 180 */
83977443
DR
181static int omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
182 unsigned debounce)
168ef3d9 183{
9942da0e 184 void __iomem *reg;
168ef3d9
FB
185 u32 val;
186 u32 l;
e85ec6c3 187 bool enable = !!debounce;
168ef3d9 188
77640aab 189 if (!bank->dbck_flag)
83977443 190 return -ENOTSUPP;
77640aab 191
e85ec6c3
GS
192 if (enable) {
193 debounce = DIV_ROUND_UP(debounce, 31) - 1;
83977443
DR
194 if ((debounce & OMAP4_GPIO_DEBOUNCINGTIME_MASK) != debounce)
195 return -EINVAL;
e85ec6c3 196 }
168ef3d9 197
4a58d229 198 l = BIT(offset);
168ef3d9 199
5d9452e7 200 clk_enable(bank->dbck);
9942da0e 201 reg = bank->base + bank->regs->debounce;
661553b9 202 writel_relaxed(debounce, reg);
168ef3d9 203
8ee1de65 204 val = omap_gpio_rmw(bank->base + bank->regs->debounce_en, l, enable);
f7ec0b0b 205 bank->dbck_enable_mask = val;
168ef3d9 206
5d9452e7 207 clk_disable(bank->dbck);
6fd9c421
TKD
208 /*
209 * Enable debounce clock per module.
210 * This call is mandatory because in omap_gpio_request() when
211 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
212 * runtime callbck fails to turn on dbck because dbck_enable_mask
213 * used within _gpio_dbck_enable() is still not initialized at
214 * that point. Therefore we have to enable dbck here.
215 */
a0e827c6 216 omap_gpio_dbck_enable(bank);
ae547354
NM
217 if (bank->dbck_enable_mask) {
218 bank->context.debounce = debounce;
219 bank->context.debounce_en = val;
220 }
83977443
DR
221
222 return 0;
168ef3d9
FB
223}
224
c9c55d92 225/**
a0e827c6 226 * omap_clear_gpio_debounce - clear debounce settings for a gpio
c9c55d92 227 * @bank: the gpio bank we're acting upon
4a58d229 228 * @offset: the gpio number on this @bank
c9c55d92
JH
229 *
230 * If a gpio is using debounce, then clear the debounce enable bit and if
231 * this is the only gpio in this bank using debounce, then clear the debounce
232 * time too. The debounce clock will also be disabled when calling this function
233 * if this is the only gpio in the bank using debounce.
234 */
4a58d229 235static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
c9c55d92 236{
4a58d229 237 u32 gpio_bit = BIT(offset);
c9c55d92
JH
238
239 if (!bank->dbck_flag)
240 return;
241
242 if (!(bank->dbck_enable_mask & gpio_bit))
243 return;
244
245 bank->dbck_enable_mask &= ~gpio_bit;
246 bank->context.debounce_en &= ~gpio_bit;
661553b9 247 writel_relaxed(bank->context.debounce_en,
c9c55d92
JH
248 bank->base + bank->regs->debounce_en);
249
250 if (!bank->dbck_enable_mask) {
251 bank->context.debounce = 0;
661553b9 252 writel_relaxed(bank->context.debounce, bank->base +
c9c55d92 253 bank->regs->debounce);
5d9452e7 254 clk_disable(bank->dbck);
c9c55d92
JH
255 bank->dbck_enabled = false;
256 }
257}
258
da38ef3e
TL
259/*
260 * Off mode wake-up capable GPIOs in bank(s) that are in the wakeup domain.
261 * See TRM section for GPIO for "Wake-Up Generation" for the list of GPIOs
262 * in wakeup domain. If bank->non_wakeup_gpios is not configured, assume none
263 * are capable waking up the system from off mode.
264 */
265static bool omap_gpio_is_off_wakeup_capable(struct gpio_bank *bank, u32 gpio_mask)
266{
267 u32 no_wake = bank->non_wakeup_gpios;
268
269 if (no_wake)
270 return !!(~no_wake & gpio_mask);
271
272 return false;
273}
274
a0e827c6 275static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
00ece7e4 276 unsigned trigger)
5e1c5ff4 277{
3ac4fa99 278 void __iomem *base = bank->base;
b1e9fec2 279 u32 gpio_bit = BIT(gpio);
92105bb7 280
8ee1de65 281 omap_gpio_rmw(base + bank->regs->leveldetect0, gpio_bit,
a0e827c6 282 trigger & IRQ_TYPE_LEVEL_LOW);
8ee1de65 283 omap_gpio_rmw(base + bank->regs->leveldetect1, gpio_bit,
a0e827c6 284 trigger & IRQ_TYPE_LEVEL_HIGH);
e6818d29
RK
285
286 /*
287 * We need the edge detection enabled for to allow the GPIO block
288 * to be woken from idle state. Set the appropriate edge detection
289 * in addition to the level detection.
290 */
8ee1de65 291 omap_gpio_rmw(base + bank->regs->risingdetect, gpio_bit,
e6818d29 292 trigger & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH));
8ee1de65 293 omap_gpio_rmw(base + bank->regs->fallingdetect, gpio_bit,
e6818d29 294 trigger & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW));
5e571f38 295
41d87cbd 296 bank->context.leveldetect0 =
661553b9 297 readl_relaxed(bank->base + bank->regs->leveldetect0);
41d87cbd 298 bank->context.leveldetect1 =
661553b9 299 readl_relaxed(bank->base + bank->regs->leveldetect1);
41d87cbd 300 bank->context.risingdetect =
661553b9 301 readl_relaxed(bank->base + bank->regs->risingdetect);
41d87cbd 302 bank->context.fallingdetect =
661553b9 303 readl_relaxed(bank->base + bank->regs->fallingdetect);
41d87cbd 304
a0e881e2
RK
305 bank->level_mask = bank->context.leveldetect0 |
306 bank->context.leveldetect1;
307
55b220ca 308 /* This part needs to be executed always for OMAP{34xx, 44xx} */
da38ef3e 309 if (!bank->regs->irqctrl && !omap_gpio_is_off_wakeup_capable(bank, gpio)) {
699117a6
CW
310 /*
311 * Log the edge gpio and manually trigger the IRQ
312 * after resume if the input level changes
313 * to avoid irq lost during PER RET/OFF mode
314 * Applies for omap2 non-wakeup gpio and all omap3 gpios
315 */
316 if (trigger & IRQ_TYPE_EDGE_BOTH)
3ac4fa99
JY
317 bank->enabled_non_wakeup_gpios |= gpio_bit;
318 else
319 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
320 }
92105bb7
TL
321}
322
4318f36b
CM
323/*
324 * This only applies to chips that can't do both rising and falling edge
325 * detection at once. For all other chips, this function is a noop.
326 */
a0e827c6 327static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
4318f36b 328{
a47b9158
RK
329 if (IS_ENABLED(CONFIG_ARCH_OMAP1) && bank->regs->irqctrl) {
330 void __iomem *reg = bank->base + bank->regs->irqctrl;
4318f36b 331
a47b9158
RK
332 writel_relaxed(readl_relaxed(reg) ^ BIT(gpio), reg);
333 }
4318f36b
CM
334}
335
a0e827c6
JMC
336static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
337 unsigned trigger)
92105bb7
TL
338{
339 void __iomem *reg = bank->base;
340 u32 l = 0;
5e1c5ff4 341
5e571f38 342 if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
a0e827c6 343 omap_set_gpio_trigger(bank, gpio, trigger);
5e571f38
TKD
344 } else if (bank->regs->irqctrl) {
345 reg += bank->regs->irqctrl;
346
661553b9 347 l = readl_relaxed(reg);
29501577 348 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
b1e9fec2 349 bank->toggle_mask |= BIT(gpio);
6cab4860 350 if (trigger & IRQ_TYPE_EDGE_RISING)
b1e9fec2 351 l |= BIT(gpio);
6cab4860 352 else if (trigger & IRQ_TYPE_EDGE_FALLING)
b1e9fec2 353 l &= ~(BIT(gpio));
92105bb7 354 else
5e571f38
TKD
355 return -EINVAL;
356
661553b9 357 writel_relaxed(l, reg);
5e571f38 358 } else if (bank->regs->edgectrl1) {
5e1c5ff4 359 if (gpio & 0x08)
5e571f38 360 reg += bank->regs->edgectrl2;
5e1c5ff4 361 else
5e571f38
TKD
362 reg += bank->regs->edgectrl1;
363
5e1c5ff4 364 gpio &= 0x07;
661553b9 365 l = readl_relaxed(reg);
5e1c5ff4 366 l &= ~(3 << (gpio << 1));
6cab4860 367 if (trigger & IRQ_TYPE_EDGE_RISING)
6e60e79a 368 l |= 2 << (gpio << 1);
6cab4860 369 if (trigger & IRQ_TYPE_EDGE_FALLING)
b1e9fec2 370 l |= BIT(gpio << 1);
661553b9 371 writel_relaxed(l, reg);
5e1c5ff4 372 }
92105bb7 373 return 0;
5e1c5ff4
TL
374}
375
a0e827c6 376static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
fac7fa16
JMC
377{
378 if (bank->regs->pinctrl) {
379 void __iomem *reg = bank->base + bank->regs->pinctrl;
380
381 /* Claim the pin for MPU */
b1e9fec2 382 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
fac7fa16
JMC
383 }
384
385 if (bank->regs->ctrl && !BANK_USED(bank)) {
386 void __iomem *reg = bank->base + bank->regs->ctrl;
387 u32 ctrl;
388
661553b9 389 ctrl = readl_relaxed(reg);
fac7fa16
JMC
390 /* Module is enabled, clocks are not gated */
391 ctrl &= ~GPIO_MOD_CTRL_BIT;
661553b9 392 writel_relaxed(ctrl, reg);
fac7fa16
JMC
393 bank->context.ctrl = ctrl;
394 }
395}
396
a0e827c6 397static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
fac7fa16 398{
fac7fa16
JMC
399 if (bank->regs->ctrl && !BANK_USED(bank)) {
400 void __iomem *reg = bank->base + bank->regs->ctrl;
401 u32 ctrl;
402
661553b9 403 ctrl = readl_relaxed(reg);
fac7fa16
JMC
404 /* Module is disabled, clocks are gated */
405 ctrl |= GPIO_MOD_CTRL_BIT;
661553b9 406 writel_relaxed(ctrl, reg);
fac7fa16
JMC
407 bank->context.ctrl = ctrl;
408 }
409}
410
b2b20045 411static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
fa365e4d
JMC
412{
413 void __iomem *reg = bank->base + bank->regs->direction;
414
b2b20045 415 return readl_relaxed(reg) & BIT(offset);
fa365e4d
JMC
416}
417
37e14ecf 418static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
3d009c8c
TL
419{
420 if (!LINE_USED(bank->mod_usage, offset)) {
421 omap_enable_gpio_module(bank, offset);
422 omap_set_gpio_direction(bank, offset, 1);
423 }
37e14ecf 424 bank->irq_usage |= BIT(offset);
3d009c8c
TL
425}
426
a0e827c6 427static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
5e1c5ff4 428{
a0e827c6 429 struct gpio_bank *bank = omap_irq_data_get_bank(d);
92105bb7 430 int retval;
a6472533 431 unsigned long flags;
ea5fbe8d 432 unsigned offset = d->hwirq;
92105bb7 433
e5c56ed3 434 if (type & ~IRQ_TYPE_SENSE_MASK)
6e60e79a 435 return -EINVAL;
e5c56ed3 436
9ea14d8c
TKD
437 if (!bank->regs->leveldetect0 &&
438 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
92105bb7
TL
439 return -EINVAL;
440
4dbada2b 441 raw_spin_lock_irqsave(&bank->lock, flags);
a0e827c6 442 retval = omap_set_gpio_triggering(bank, offset, type);
977bd8a9 443 if (retval) {
627c89b4 444 raw_spin_unlock_irqrestore(&bank->lock, flags);
1562e461 445 goto error;
977bd8a9 446 }
37e14ecf 447 omap_gpio_init_irq(bank, offset);
b2b20045 448 if (!omap_gpio_is_input(bank, offset)) {
4dbada2b 449 raw_spin_unlock_irqrestore(&bank->lock, flags);
1562e461
GS
450 retval = -EINVAL;
451 goto error;
fac7fa16 452 }
4dbada2b 453 raw_spin_unlock_irqrestore(&bank->lock, flags);
672e302e
KH
454
455 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
43ec2e43 456 irq_set_handler_locked(d, handle_level_irq);
672e302e 457 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
80ac93c2
GS
458 /*
459 * Edge IRQs are already cleared/acked in irq_handler and
460 * not need to be masked, as result handle_edge_irq()
461 * logic is excessed here and may cause lose of interrupts.
462 * So just use handle_simple_irq.
463 */
464 irq_set_handler_locked(d, handle_simple_irq);
672e302e 465
1562e461
GS
466 return 0;
467
468error:
92105bb7 469 return retval;
5e1c5ff4
TL
470}
471
a0e827c6 472static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
5e1c5ff4 473{
92105bb7 474 void __iomem *reg = bank->base;
5e1c5ff4 475
eef4bec7 476 reg += bank->regs->irqstatus;
661553b9 477 writel_relaxed(gpio_mask, reg);
bee7930f
HD
478
479 /* Workaround for clearing DSP GPIO interrupts to allow retention */
eef4bec7
KH
480 if (bank->regs->irqstatus2) {
481 reg = bank->base + bank->regs->irqstatus2;
661553b9 482 writel_relaxed(gpio_mask, reg);
eef4bec7 483 }
bedfd154
RQ
484
485 /* Flush posted write for the irq status to avoid spurious interrupts */
661553b9 486 readl_relaxed(reg);
5e1c5ff4
TL
487}
488
9943f261
GS
489static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
490 unsigned offset)
5e1c5ff4 491{
9943f261 492 omap_clear_gpio_irqbank(bank, BIT(offset));
5e1c5ff4
TL
493}
494
a0e827c6 495static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
ea6dedd7
ID
496{
497 void __iomem *reg = bank->base;
99c47707 498 u32 l;
b1e9fec2 499 u32 mask = (BIT(bank->width)) - 1;
ea6dedd7 500
28f3b5a0 501 reg += bank->regs->irqenable;
661553b9 502 l = readl_relaxed(reg);
28f3b5a0 503 if (bank->regs->irqenable_inv)
99c47707
ID
504 l = ~l;
505 l &= mask;
506 return l;
ea6dedd7
ID
507}
508
31b2d7f7
RK
509static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
510 unsigned offset, int enable)
28f3b5a0
KH
511{
512 void __iomem *reg = bank->base;
31b2d7f7
RK
513 u32 gpio_mask = BIT(offset);
514
515 if (bank->regs->set_irqenable && bank->regs->clr_irqenable) {
516 if (enable) {
517 reg += bank->regs->set_irqenable;
518 bank->context.irqenable1 |= gpio_mask;
519 } else {
520 reg += bank->regs->clr_irqenable;
521 bank->context.irqenable1 &= ~gpio_mask;
522 }
523 writel_relaxed(gpio_mask, reg);
28f3b5a0 524 } else {
31b2d7f7
RK
525 bank->context.irqenable1 =
526 omap_gpio_rmw(reg + bank->regs->irqenable, gpio_mask,
527 enable ^ bank->regs->irqenable_inv);
5e1c5ff4 528 }
40fd422a
RK
529
530 /*
531 * Program GPIO wakeup along with IRQ enable to satisfy OMAP4430 TRM
532 * note requiring correlation between the IRQ enable registers and
533 * the wakeup registers. In any case, we want wakeup from idle
534 * enabled for the GPIOs which support this feature.
535 */
536 if (bank->regs->wkup_en &&
537 (bank->regs->edgectrl1 || !(bank->non_wakeup_gpios & gpio_mask))) {
538 bank->context.wake_en =
539 omap_gpio_rmw(bank->base + bank->regs->wkup_en,
540 gpio_mask, enable);
541 }
5e1c5ff4
TL
542}
543
92105bb7 544/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
a0e827c6 545static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
92105bb7 546{
a0e827c6 547 struct gpio_bank *bank = omap_irq_data_get_bank(d);
450fa54c 548
0c0451e7 549 return irq_set_irq_wake(bank->irq, enable);
92105bb7
TL
550}
551
5e1c5ff4
TL
552/*
553 * We need to unmask the GPIO bank interrupt as soon as possible to
554 * avoid missing GPIO interrupts for other lines in the bank.
555 * Then we need to mask-read-clear-unmask the triggered GPIO lines
556 * in the bank to avoid missing nested interrupts for a GPIO line.
557 * If we wait to unmask individual GPIO lines in the bank after the
558 * line's interrupt handler has been run, we may miss some nested
559 * interrupts.
560 */
450fa54c 561static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
5e1c5ff4 562{
92105bb7 563 void __iomem *isr_reg = NULL;
395373c7 564 u32 enabled, isr, edge;
3513cdec 565 unsigned int bit;
450fa54c
GS
566 struct gpio_bank *bank = gpiobank;
567 unsigned long wa_lock_flags;
235f1eb1 568 unsigned long lock_flags;
5e1c5ff4 569
eef4bec7 570 isr_reg = bank->base + bank->regs->irqstatus;
b1cc4c55
EK
571 if (WARN_ON(!isr_reg))
572 goto exit;
573
5284521a
TL
574 if (WARN_ONCE(!pm_runtime_active(bank->chip.parent),
575 "gpio irq%i while runtime suspended?\n", irq))
576 return IRQ_NONE;
450fa54c 577
e83507b7 578 while (1) {
235f1eb1
GS
579 raw_spin_lock_irqsave(&bank->lock, lock_flags);
580
a0e827c6 581 enabled = omap_get_gpio_irqbank_mask(bank);
80ac93c2 582 isr = readl_relaxed(isr_reg) & enabled;
6e60e79a 583
395373c7
RK
584 /*
585 * Clear edge sensitive interrupts before calling handler(s)
586 * so subsequent edge transitions are not missed while the
587 * handlers are running.
588 */
589 edge = isr & ~bank->level_mask;
590 if (edge)
591 omap_clear_gpio_irqbank(bank, edge);
6e60e79a 592
235f1eb1
GS
593 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
594
92105bb7
TL
595 if (!isr)
596 break;
597
3513cdec
JH
598 while (isr) {
599 bit = __ffs(isr);
b1e9fec2 600 isr &= ~(BIT(bit));
25db711d 601
235f1eb1 602 raw_spin_lock_irqsave(&bank->lock, lock_flags);
4318f36b
CM
603 /*
604 * Some chips can't respond to both rising and falling
605 * at the same time. If this irq was requested with
606 * both flags, we need to flip the ICR data for the IRQ
607 * to respond to the IRQ for the opposite direction.
608 * This will be indicated in the bank toggle_mask.
609 */
b1e9fec2 610 if (bank->toggle_mask & (BIT(bit)))
a0e827c6 611 omap_toggle_gpio_edge_triggering(bank, bit);
4318f36b 612
235f1eb1
GS
613 raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
614
450fa54c
GS
615 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
616
f0fbe7bc 617 generic_handle_irq(irq_find_mapping(bank->chip.irq.domain,
fb655f57 618 bit));
450fa54c
GS
619
620 raw_spin_unlock_irqrestore(&bank->wa_lock,
621 wa_lock_flags);
92105bb7 622 }
1a8bfa1e 623 }
b1cc4c55 624exit:
450fa54c 625 return IRQ_HANDLED;
5e1c5ff4
TL
626}
627
3d009c8c
TL
628static unsigned int omap_gpio_irq_startup(struct irq_data *d)
629{
630 struct gpio_bank *bank = omap_irq_data_get_bank(d);
3d009c8c 631 unsigned long flags;
37e14ecf 632 unsigned offset = d->hwirq;
3d009c8c 633
4dbada2b 634 raw_spin_lock_irqsave(&bank->lock, flags);
121dcb76
GS
635
636 if (!LINE_USED(bank->mod_usage, offset))
637 omap_set_gpio_direction(bank, offset, 1);
121dcb76
GS
638 omap_enable_gpio_module(bank, offset);
639 bank->irq_usage |= BIT(offset);
640
4dbada2b 641 raw_spin_unlock_irqrestore(&bank->lock, flags);
3d009c8c
TL
642 omap_gpio_unmask_irq(d);
643
644 return 0;
645}
646
a0e827c6 647static void omap_gpio_irq_shutdown(struct irq_data *d)
4196dd6b 648{
a0e827c6 649 struct gpio_bank *bank = omap_irq_data_get_bank(d);
85ec7b97 650 unsigned long flags;
9943f261 651 unsigned offset = d->hwirq;
4196dd6b 652
4dbada2b 653 raw_spin_lock_irqsave(&bank->lock, flags);
b1e9fec2 654 bank->irq_usage &= ~(BIT(offset));
6e96c1b5 655 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
c859e0d4
RK
656 omap_clear_gpio_irqstatus(bank, offset);
657 omap_set_gpio_irqenable(bank, offset, 0);
6e96c1b5
GS
658 if (!LINE_USED(bank->mod_usage, offset))
659 omap_clear_gpio_debounce(bank, offset);
a0e827c6 660 omap_disable_gpio_module(bank, offset);
4dbada2b 661 raw_spin_unlock_irqrestore(&bank->lock, flags);
aca82d1c
GS
662}
663
664static void omap_gpio_irq_bus_lock(struct irq_data *data)
665{
666 struct gpio_bank *bank = omap_irq_data_get_bank(data);
667
46748073 668 pm_runtime_get_sync(bank->chip.parent);
aca82d1c
GS
669}
670
671static void gpio_irq_bus_sync_unlock(struct irq_data *data)
672{
673 struct gpio_bank *bank = omap_irq_data_get_bank(data);
fac7fa16 674
46748073 675 pm_runtime_put(bank->chip.parent);
4196dd6b
TL
676}
677
a0e827c6 678static void omap_gpio_mask_irq(struct irq_data *d)
5e1c5ff4 679{
a0e827c6 680 struct gpio_bank *bank = omap_irq_data_get_bank(d);
9943f261 681 unsigned offset = d->hwirq;
85ec7b97 682 unsigned long flags;
5e1c5ff4 683
4dbada2b 684 raw_spin_lock_irqsave(&bank->lock, flags);
9943f261 685 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
c859e0d4 686 omap_set_gpio_irqenable(bank, offset, 0);
4dbada2b 687 raw_spin_unlock_irqrestore(&bank->lock, flags);
5e1c5ff4
TL
688}
689
a0e827c6 690static void omap_gpio_unmask_irq(struct irq_data *d)
5e1c5ff4 691{
a0e827c6 692 struct gpio_bank *bank = omap_irq_data_get_bank(d);
9943f261 693 unsigned offset = d->hwirq;
8c04a176 694 u32 trigger = irqd_get_trigger_type(d);
85ec7b97 695 unsigned long flags;
55b6019a 696
4dbada2b 697 raw_spin_lock_irqsave(&bank->lock, flags);
d01849f7
RK
698 omap_set_gpio_irqenable(bank, offset, 1);
699
700 /*
701 * For level-triggered GPIOs, clearing must be done after the source
702 * is cleared, thus after the handler has run. OMAP4 needs this done
703 * after enabing the interrupt to clear the wakeup status.
704 */
c859e0d4
RK
705 if (bank->regs->leveldetect0 && bank->regs->wkup_en &&
706 trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
9943f261 707 omap_clear_gpio_irqstatus(bank, offset);
5e1c5ff4 708
c859e0d4
RK
709 if (trigger)
710 omap_set_gpio_triggering(bank, offset, trigger);
711
4dbada2b 712 raw_spin_unlock_irqrestore(&bank->lock, flags);
5e1c5ff4
TL
713}
714
e5c56ed3
DB
715/*---------------------------------------------------------------------*/
716
79ee031f 717static int omap_mpuio_suspend_noirq(struct device *dev)
11a78b79 718{
a3f4f728 719 struct gpio_bank *bank = dev_get_drvdata(dev);
5de62b86
TL
720 void __iomem *mask_reg = bank->base +
721 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
a6472533 722 unsigned long flags;
11a78b79 723
4dbada2b 724 raw_spin_lock_irqsave(&bank->lock, flags);
661553b9 725 writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
4dbada2b 726 raw_spin_unlock_irqrestore(&bank->lock, flags);
11a78b79
DB
727
728 return 0;
729}
730
79ee031f 731static int omap_mpuio_resume_noirq(struct device *dev)
11a78b79 732{
a3f4f728 733 struct gpio_bank *bank = dev_get_drvdata(dev);
5de62b86
TL
734 void __iomem *mask_reg = bank->base +
735 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
a6472533 736 unsigned long flags;
11a78b79 737
4dbada2b 738 raw_spin_lock_irqsave(&bank->lock, flags);
661553b9 739 writel_relaxed(bank->context.wake_en, mask_reg);
4dbada2b 740 raw_spin_unlock_irqrestore(&bank->lock, flags);
11a78b79
DB
741
742 return 0;
743}
744
47145210 745static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
79ee031f
MD
746 .suspend_noirq = omap_mpuio_suspend_noirq,
747 .resume_noirq = omap_mpuio_resume_noirq,
748};
749
3c437ffd 750/* use platform_driver for this. */
11a78b79 751static struct platform_driver omap_mpuio_driver = {
11a78b79
DB
752 .driver = {
753 .name = "mpuio",
79ee031f 754 .pm = &omap_mpuio_dev_pm_ops,
11a78b79
DB
755 },
756};
757
758static struct platform_device omap_mpuio_device = {
759 .name = "mpuio",
760 .id = -1,
761 .dev = {
762 .driver = &omap_mpuio_driver.driver,
763 }
764 /* could list the /proc/iomem resources */
765};
766
a0e827c6 767static inline void omap_mpuio_init(struct gpio_bank *bank)
11a78b79 768{
77640aab 769 platform_set_drvdata(&omap_mpuio_device, bank);
fcf126d8 770
11a78b79
DB
771 if (platform_driver_register(&omap_mpuio_driver) == 0)
772 (void) platform_device_register(&omap_mpuio_device);
773}
774
e5c56ed3 775/*---------------------------------------------------------------------*/
5e1c5ff4 776
dfbc6c7a
RK
777static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
778{
779 struct gpio_bank *bank = gpiochip_get_data(chip);
780 unsigned long flags;
781
782 pm_runtime_get_sync(chip->parent);
783
784 raw_spin_lock_irqsave(&bank->lock, flags);
785 omap_enable_gpio_module(bank, offset);
786 bank->mod_usage |= BIT(offset);
787 raw_spin_unlock_irqrestore(&bank->lock, flags);
788
789 return 0;
790}
791
792static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
793{
794 struct gpio_bank *bank = gpiochip_get_data(chip);
795 unsigned long flags;
796
797 raw_spin_lock_irqsave(&bank->lock, flags);
798 bank->mod_usage &= ~(BIT(offset));
799 if (!LINE_USED(bank->irq_usage, offset)) {
800 omap_set_gpio_direction(bank, offset, 1);
801 omap_clear_gpio_debounce(bank, offset);
802 }
803 omap_disable_gpio_module(bank, offset);
804 raw_spin_unlock_irqrestore(&bank->lock, flags);
805
806 pm_runtime_put(chip->parent);
807}
808
a0e827c6 809static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
9370084e 810{
40bb2273 811 struct gpio_bank *bank = gpiochip_get_data(chip);
9370084e 812
40bb2273
RK
813 return !!(readl_relaxed(bank->base + bank->regs->direction) &
814 BIT(offset));
9370084e
YY
815}
816
a0e827c6 817static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
52e31344
DB
818{
819 struct gpio_bank *bank;
820 unsigned long flags;
821
d99f7aec 822 bank = gpiochip_get_data(chip);
4dbada2b 823 raw_spin_lock_irqsave(&bank->lock, flags);
a0e827c6 824 omap_set_gpio_direction(bank, offset, 1);
4dbada2b 825 raw_spin_unlock_irqrestore(&bank->lock, flags);
52e31344
DB
826 return 0;
827}
828
a0e827c6 829static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
52e31344 830{
5ca5f92c
RK
831 struct gpio_bank *bank = gpiochip_get_data(chip);
832 void __iomem *reg;
b37c45b8 833
b2b20045 834 if (omap_gpio_is_input(bank, offset))
5ca5f92c 835 reg = bank->base + bank->regs->datain;
b37c45b8 836 else
5ca5f92c
RK
837 reg = bank->base + bank->regs->dataout;
838
839 return (readl_relaxed(reg) & BIT(offset)) != 0;
52e31344
DB
840}
841
a0e827c6 842static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
52e31344
DB
843{
844 struct gpio_bank *bank;
845 unsigned long flags;
846
d99f7aec 847 bank = gpiochip_get_data(chip);
4dbada2b 848 raw_spin_lock_irqsave(&bank->lock, flags);
fa87931a 849 bank->set_dataout(bank, offset, value);
a0e827c6 850 omap_set_gpio_direction(bank, offset, 0);
4dbada2b 851 raw_spin_unlock_irqrestore(&bank->lock, flags);
2f56e0a5 852 return 0;
52e31344
DB
853}
854
442af140
JK
855static int omap_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
856 unsigned long *bits)
857{
858 struct gpio_bank *bank = gpiochip_get_data(chip);
6653dd88
RK
859 void __iomem *base = bank->base;
860 u32 direction, m, val = 0;
861
862 direction = readl_relaxed(base + bank->regs->direction);
442af140 863
6653dd88
RK
864 m = direction & *mask;
865 if (m)
866 val |= readl_relaxed(base + bank->regs->datain) & m;
442af140 867
6653dd88
RK
868 m = ~direction & *mask;
869 if (m)
870 val |= readl_relaxed(base + bank->regs->dataout) & m;
442af140 871
6653dd88 872 *bits = val;
442af140
JK
873
874 return 0;
875}
876
a0e827c6
JMC
877static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
878 unsigned debounce)
168ef3d9
FB
879{
880 struct gpio_bank *bank;
881 unsigned long flags;
83977443 882 int ret;
168ef3d9 883
d99f7aec 884 bank = gpiochip_get_data(chip);
77640aab 885
4dbada2b 886 raw_spin_lock_irqsave(&bank->lock, flags);
83977443 887 ret = omap2_set_gpio_debounce(bank, offset, debounce);
4dbada2b 888 raw_spin_unlock_irqrestore(&bank->lock, flags);
168ef3d9 889
83977443
DR
890 if (ret)
891 dev_info(chip->parent,
892 "Could not set line %u debounce to %u microseconds (%d)",
893 offset, debounce, ret);
894
895 return ret;
168ef3d9
FB
896}
897
2956b5d9
MW
898static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset,
899 unsigned long config)
900{
901 u32 debounce;
902
903 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
904 return -ENOTSUPP;
905
906 debounce = pinconf_to_config_argument(config);
907 return omap_gpio_debounce(chip, offset, debounce);
908}
909
a0e827c6 910static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
52e31344
DB
911{
912 struct gpio_bank *bank;
913 unsigned long flags;
914
d99f7aec 915 bank = gpiochip_get_data(chip);
4dbada2b 916 raw_spin_lock_irqsave(&bank->lock, flags);
fa87931a 917 bank->set_dataout(bank, offset, value);
4dbada2b 918 raw_spin_unlock_irqrestore(&bank->lock, flags);
52e31344
DB
919}
920
442af140
JK
921static void omap_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
922 unsigned long *bits)
923{
924 struct gpio_bank *bank = gpiochip_get_data(chip);
8ba70595 925 void __iomem *reg = bank->base + bank->regs->dataout;
442af140 926 unsigned long flags;
8ba70595 927 u32 l;
442af140
JK
928
929 raw_spin_lock_irqsave(&bank->lock, flags);
8ba70595
RK
930 l = (readl_relaxed(reg) & ~*mask) | (*bits & *mask);
931 writel_relaxed(l, reg);
932 bank->context.dataout = l;
442af140
JK
933 raw_spin_unlock_irqrestore(&bank->lock, flags);
934}
935
52e31344
DB
936/*---------------------------------------------------------------------*/
937
e4b2ae7a 938static void omap_gpio_show_rev(struct gpio_bank *bank)
9f7065da 939{
e5ff4440 940 static bool called;
9f7065da
TL
941 u32 rev;
942
e5ff4440 943 if (called || bank->regs->revision == USHRT_MAX)
9f7065da
TL
944 return;
945
661553b9 946 rev = readw_relaxed(bank->base + bank->regs->revision);
e5ff4440 947 pr_info("OMAP GPIO hardware version %d.%d\n",
9f7065da 948 (rev >> 4) & 0x0f, rev & 0x0f);
e5ff4440
KH
949
950 called = true;
9f7065da
TL
951}
952
03e128ca 953static void omap_gpio_mod_init(struct gpio_bank *bank)
2fae7fbe 954{
ab985f0f
TKD
955 void __iomem *base = bank->base;
956 u32 l = 0xffffffff;
2fae7fbe 957
ab985f0f
TKD
958 if (bank->width == 16)
959 l = 0xffff;
960
d0d665a8 961 if (bank->is_mpuio) {
661553b9 962 writel_relaxed(l, bank->base + bank->regs->irqenable);
ab985f0f 963 return;
2fae7fbe 964 }
ab985f0f 965
8ee1de65 966 omap_gpio_rmw(base + bank->regs->irqenable, l,
a0e827c6 967 bank->regs->irqenable_inv);
8ee1de65 968 omap_gpio_rmw(base + bank->regs->irqstatus, l,
a0e827c6 969 !bank->regs->irqenable_inv);
ab985f0f 970 if (bank->regs->debounce_en)
661553b9 971 writel_relaxed(0, base + bank->regs->debounce_en);
ab985f0f 972
2dc983c5 973 /* Save OE default value (0xffffffff) in the context */
661553b9 974 bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
ab985f0f
TKD
975 /* Initialize interface clk ungated, module enabled */
976 if (bank->regs->ctrl)
661553b9 977 writel_relaxed(0, base + bank->regs->ctrl);
2fae7fbe
VC
978}
979
46824e22 980static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
2fae7fbe 981{
81930328 982 struct gpio_irq_chip *irq;
2fae7fbe 983 static int gpio;
088413bc 984 const char *label;
fb655f57 985 int irq_base = 0;
6ef7f385 986 int ret;
2fae7fbe 987
2fae7fbe
VC
988 /*
989 * REVISIT eventually switch from OMAP-specific gpio structs
990 * over to the generic ones
991 */
992 bank->chip.request = omap_gpio_request;
993 bank->chip.free = omap_gpio_free;
a0e827c6
JMC
994 bank->chip.get_direction = omap_gpio_get_direction;
995 bank->chip.direction_input = omap_gpio_input;
996 bank->chip.get = omap_gpio_get;
442af140 997 bank->chip.get_multiple = omap_gpio_get_multiple;
a0e827c6 998 bank->chip.direction_output = omap_gpio_output;
2956b5d9 999 bank->chip.set_config = omap_gpio_set_config;
a0e827c6 1000 bank->chip.set = omap_gpio_set;
442af140 1001 bank->chip.set_multiple = omap_gpio_set_multiple;
d0d665a8 1002 if (bank->is_mpuio) {
2fae7fbe 1003 bank->chip.label = "mpuio";
6ed87c5b 1004 if (bank->regs->wkup_en)
58383c78 1005 bank->chip.parent = &omap_mpuio_device.dev;
2fae7fbe
VC
1006 bank->chip.base = OMAP_MPUIO(0);
1007 } else {
088413bc
LW
1008 label = devm_kasprintf(bank->chip.parent, GFP_KERNEL, "gpio-%d-%d",
1009 gpio, gpio + bank->width - 1);
1010 if (!label)
1011 return -ENOMEM;
1012 bank->chip.label = label;
2fae7fbe 1013 bank->chip.base = gpio;
2fae7fbe 1014 }
d5f46247 1015 bank->chip.ngpio = bank->width;
2fae7fbe 1016
fb655f57
JMC
1017#ifdef CONFIG_ARCH_OMAP1
1018 /*
1019 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1020 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1021 */
2ed36f30
BG
1022 irq_base = devm_irq_alloc_descs(bank->chip.parent,
1023 -1, 0, bank->width, 0);
fb655f57 1024 if (irq_base < 0) {
7b1e5dc8 1025 dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n");
fb655f57
JMC
1026 return -ENODEV;
1027 }
1028#endif
1029
d2d05c65 1030 /* MPUIO is a bit different, reading IRQ status clears it */
693de831
RK
1031 if (bank->is_mpuio && !bank->regs->wkup_en)
1032 irqc->irq_set_wake = NULL;
d2d05c65 1033
81930328
GS
1034 irq = &bank->chip.irq;
1035 irq->chip = irqc;
1036 irq->handler = handle_bad_irq;
1037 irq->default_type = IRQ_TYPE_NONE;
1038 irq->num_parents = 1;
1039 irq->parents = &bank->irq;
1040 irq->first = irq_base;
fb655f57 1041
81930328 1042 ret = gpiochip_add_data(&bank->chip, bank);
fb655f57 1043 if (ret) {
7b1e5dc8 1044 dev_err(bank->chip.parent,
81930328
GS
1045 "Could not register gpio chip %d\n", ret);
1046 return ret;
fb655f57
JMC
1047 }
1048
7b1e5dc8
GS
1049 ret = devm_request_irq(bank->chip.parent, bank->irq,
1050 omap_gpio_irq_handler,
1051 0, dev_name(bank->chip.parent), bank);
450fa54c
GS
1052 if (ret)
1053 gpiochip_remove(&bank->chip);
1054
81930328
GS
1055 if (!bank->is_mpuio)
1056 gpio += bank->width;
1057
450fa54c 1058 return ret;
2fae7fbe
VC
1059}
1060
7c68571f 1061static void omap_gpio_init_context(struct gpio_bank *p)
b764a586 1062{
18bd49c4 1063 const struct omap_gpio_reg_offs *regs = p->regs;
7c68571f 1064 void __iomem *base = p->base;
b764a586 1065
7c68571f
AB
1066 p->context.ctrl = readl_relaxed(base + regs->ctrl);
1067 p->context.oe = readl_relaxed(base + regs->direction);
1068 p->context.wake_en = readl_relaxed(base + regs->wkup_en);
1069 p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1070 p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1071 p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1072 p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1073 p->context.irqenable1 = readl_relaxed(base + regs->irqenable);
1074 p->context.irqenable2 = readl_relaxed(base + regs->irqenable2);
9a302781 1075 p->context.dataout = readl_relaxed(base + regs->dataout);
b764a586 1076
7c68571f 1077 p->context_valid = true;
b764a586
TL
1078}
1079
7c68571f 1080static void omap_gpio_restore_context(struct gpio_bank *bank)
5e1c5ff4 1081{
18bd49c4 1082 const struct omap_gpio_reg_offs *regs = bank->regs;
9c7f798d
RK
1083 void __iomem *base = bank->base;
1084
1085 writel_relaxed(bank->context.wake_en, base + regs->wkup_en);
1086 writel_relaxed(bank->context.ctrl, base + regs->ctrl);
1087 writel_relaxed(bank->context.leveldetect0, base + regs->leveldetect0);
1088 writel_relaxed(bank->context.leveldetect1, base + regs->leveldetect1);
1089 writel_relaxed(bank->context.risingdetect, base + regs->risingdetect);
1090 writel_relaxed(bank->context.fallingdetect, base + regs->fallingdetect);
1091 writel_relaxed(bank->context.dataout, base + regs->dataout);
1092 writel_relaxed(bank->context.oe, base + regs->direction);
9f7065da 1093
7c68571f 1094 if (bank->dbck_enable_mask) {
9c7f798d 1095 writel_relaxed(bank->context.debounce, base + regs->debounce);
7c68571f 1096 writel_relaxed(bank->context.debounce_en,
9c7f798d 1097 base + regs->debounce_en);
b764a586
TL
1098 }
1099
9c7f798d
RK
1100 writel_relaxed(bank->context.irqenable1, base + regs->irqenable);
1101 writel_relaxed(bank->context.irqenable2, base + regs->irqenable2);
cac089f9
TL
1102}
1103
b764a586 1104static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context)
3ac4fa99 1105{
b764a586 1106 struct device *dev = bank->chip.parent;
21e2118f
TL
1107 void __iomem *base = bank->base;
1108 u32 nowake;
1109
1110 bank->saved_datain = readl_relaxed(base + bank->regs->datain);
68942edb 1111
b3c64bc3
KH
1112 if (!bank->enabled_non_wakeup_gpios)
1113 goto update_gpio_context_count;
1114
b764a586 1115 if (!may_lose_context)
41d87cbd 1116 goto update_gpio_context_count;
b764a586 1117
2dc983c5 1118 /*
21e2118f 1119 * If going to OFF, remove triggering for all wkup domain
2dc983c5
TKD
1120 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1121 * generated. See OMAP2420 Errata item 1.101.
1122 */
21e2118f
TL
1123 if (!bank->loses_context && bank->enabled_non_wakeup_gpios) {
1124 nowake = bank->enabled_non_wakeup_gpios;
8ee1de65
RK
1125 omap_gpio_rmw(base + bank->regs->fallingdetect, nowake, ~nowake);
1126 omap_gpio_rmw(base + bank->regs->risingdetect, nowake, ~nowake);
21e2118f 1127 }
3f1686a9 1128
41d87cbd 1129update_gpio_context_count:
2dc983c5
TKD
1130 if (bank->get_context_loss_count)
1131 bank->context_loss_count =
7b1e5dc8 1132 bank->get_context_loss_count(dev);
60a3437d 1133
a0e827c6 1134 omap_gpio_dbck_disable(bank);
3ac4fa99
JY
1135}
1136
b764a586 1137static void omap_gpio_unidle(struct gpio_bank *bank)
3ac4fa99 1138{
b764a586 1139 struct device *dev = bank->chip.parent;
2dc983c5 1140 u32 l = 0, gen, gen0, gen1;
a2797bea 1141 int c;
8865b9b6 1142
352a2d5b
JH
1143 /*
1144 * On the first resume during the probe, the context has not
1145 * been initialised and so initialise it now. Also initialise
1146 * the context loss count.
1147 */
1148 if (bank->loses_context && !bank->context_valid) {
1149 omap_gpio_init_context(bank);
1150
1151 if (bank->get_context_loss_count)
1152 bank->context_loss_count =
7b1e5dc8 1153 bank->get_context_loss_count(dev);
352a2d5b
JH
1154 }
1155
a0e827c6 1156 omap_gpio_dbck_enable(bank);
68942edb 1157
a2797bea
JH
1158 if (bank->loses_context) {
1159 if (!bank->get_context_loss_count) {
2dc983c5
TKD
1160 omap_gpio_restore_context(bank);
1161 } else {
7b1e5dc8 1162 c = bank->get_context_loss_count(dev);
a2797bea
JH
1163 if (c != bank->context_loss_count) {
1164 omap_gpio_restore_context(bank);
1165 } else {
b764a586 1166 return;
a2797bea 1167 }
60a3437d 1168 }
21e2118f
TL
1169 } else {
1170 /* Restore changes done for OMAP2420 errata 1.101 */
1171 writel_relaxed(bank->context.fallingdetect,
1172 bank->base + bank->regs->fallingdetect);
1173 writel_relaxed(bank->context.risingdetect,
1174 bank->base + bank->regs->risingdetect);
2dc983c5 1175 }
43ffcd9a 1176
661553b9 1177 l = readl_relaxed(bank->base + bank->regs->datain);
3f1686a9 1178
2dc983c5
TKD
1179 /*
1180 * Check if any of the non-wakeup interrupt GPIOs have changed
1181 * state. If so, generate an IRQ by software. This is
1182 * horribly racy, but it's the best we can do to work around
1183 * this silicon bug.
1184 */
1185 l ^= bank->saved_datain;
1186 l &= bank->enabled_non_wakeup_gpios;
3f1686a9 1187
2dc983c5
TKD
1188 /*
1189 * No need to generate IRQs for the rising edge for gpio IRQs
1190 * configured with falling edge only; and vice versa.
1191 */
c6f31c9e 1192 gen0 = l & bank->context.fallingdetect;
2dc983c5 1193 gen0 &= bank->saved_datain;
82dbb9d3 1194
c6f31c9e 1195 gen1 = l & bank->context.risingdetect;
2dc983c5 1196 gen1 &= ~(bank->saved_datain);
82dbb9d3 1197
2dc983c5 1198 /* FIXME: Consider GPIO IRQs with level detections properly! */
c6f31c9e
TKD
1199 gen = l & (~(bank->context.fallingdetect) &
1200 ~(bank->context.risingdetect));
2dc983c5
TKD
1201 /* Consider all GPIO IRQs needed to be updated */
1202 gen |= gen0 | gen1;
82dbb9d3 1203
2dc983c5
TKD
1204 if (gen) {
1205 u32 old0, old1;
82dbb9d3 1206
661553b9
VK
1207 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1208 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
3f1686a9 1209
4e962e89 1210 if (!bank->regs->irqstatus_raw0) {
661553b9 1211 writel_relaxed(old0 | gen, bank->base +
9ea14d8c 1212 bank->regs->leveldetect0);
661553b9 1213 writel_relaxed(old1 | gen, bank->base +
9ea14d8c 1214 bank->regs->leveldetect1);
2dc983c5 1215 }
9ea14d8c 1216
4e962e89 1217 if (bank->regs->irqstatus_raw0) {
661553b9 1218 writel_relaxed(old0 | l, bank->base +
9ea14d8c 1219 bank->regs->leveldetect0);
661553b9 1220 writel_relaxed(old1 | l, bank->base +
9ea14d8c 1221 bank->regs->leveldetect1);
3ac4fa99 1222 }
661553b9
VK
1223 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1224 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
2dc983c5 1225 }
2dc983c5 1226}
2dc983c5 1227
7c68571f
AB
1228static int gpio_omap_cpu_notifier(struct notifier_block *nb,
1229 unsigned long cmd, void *v)
352a2d5b 1230{
7c68571f
AB
1231 struct gpio_bank *bank;
1232 unsigned long flags;
352a2d5b 1233
7c68571f 1234 bank = container_of(nb, struct gpio_bank, nb);
352a2d5b 1235
7c68571f
AB
1236 raw_spin_lock_irqsave(&bank->lock, flags);
1237 switch (cmd) {
1238 case CPU_CLUSTER_PM_ENTER:
1239 if (bank->is_suspended)
1240 break;
1241 omap_gpio_idle(bank, true);
1242 break;
1243 case CPU_CLUSTER_PM_ENTER_FAILED:
1244 case CPU_CLUSTER_PM_EXIT:
1245 if (bank->is_suspended)
1246 break;
1247 omap_gpio_unidle(bank);
1248 break;
1249 }
1250 raw_spin_unlock_irqrestore(&bank->lock, flags);
352a2d5b 1251
7c68571f 1252 return NOTIFY_OK;
b764a586
TL
1253}
1254
18bd49c4 1255static const struct omap_gpio_reg_offs omap2_gpio_regs = {
384ebe1c
BC
1256 .revision = OMAP24XX_GPIO_REVISION,
1257 .direction = OMAP24XX_GPIO_OE,
1258 .datain = OMAP24XX_GPIO_DATAIN,
1259 .dataout = OMAP24XX_GPIO_DATAOUT,
1260 .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
1261 .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
1262 .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
1263 .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
1264 .irqenable = OMAP24XX_GPIO_IRQENABLE1,
1265 .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
1266 .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
1267 .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
1268 .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
1269 .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
1270 .ctrl = OMAP24XX_GPIO_CTRL,
1271 .wkup_en = OMAP24XX_GPIO_WAKE_EN,
1272 .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
1273 .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
1274 .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
1275 .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
1276};
1277
18bd49c4 1278static const struct omap_gpio_reg_offs omap4_gpio_regs = {
384ebe1c
BC
1279 .revision = OMAP4_GPIO_REVISION,
1280 .direction = OMAP4_GPIO_OE,
1281 .datain = OMAP4_GPIO_DATAIN,
1282 .dataout = OMAP4_GPIO_DATAOUT,
1283 .set_dataout = OMAP4_GPIO_SETDATAOUT,
1284 .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
1285 .irqstatus = OMAP4_GPIO_IRQSTATUS0,
1286 .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
64ea3e90
RK
1287 .irqstatus_raw0 = OMAP4_GPIO_IRQSTATUSRAW0,
1288 .irqstatus_raw1 = OMAP4_GPIO_IRQSTATUSRAW1,
384ebe1c
BC
1289 .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1290 .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
1291 .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
1292 .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
1293 .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
1294 .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
1295 .ctrl = OMAP4_GPIO_CTRL,
1296 .wkup_en = OMAP4_GPIO_IRQWAKEN0,
1297 .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
1298 .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
1299 .risingdetect = OMAP4_GPIO_RISINGDETECT,
1300 .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
1301};
1302
e9a65bb6 1303static const struct omap_gpio_platform_data omap2_pdata = {
384ebe1c
BC
1304 .regs = &omap2_gpio_regs,
1305 .bank_width = 32,
1306 .dbck_flag = false,
1307};
1308
e9a65bb6 1309static const struct omap_gpio_platform_data omap3_pdata = {
384ebe1c
BC
1310 .regs = &omap2_gpio_regs,
1311 .bank_width = 32,
1312 .dbck_flag = true,
1313};
1314
e9a65bb6 1315static const struct omap_gpio_platform_data omap4_pdata = {
384ebe1c
BC
1316 .regs = &omap4_gpio_regs,
1317 .bank_width = 32,
1318 .dbck_flag = true,
1319};
1320
1321static const struct of_device_id omap_gpio_match[] = {
1322 {
1323 .compatible = "ti,omap4-gpio",
1324 .data = &omap4_pdata,
1325 },
1326 {
1327 .compatible = "ti,omap3-gpio",
1328 .data = &omap3_pdata,
1329 },
1330 {
1331 .compatible = "ti,omap2-gpio",
1332 .data = &omap2_pdata,
1333 },
1334 { },
1335};
1336MODULE_DEVICE_TABLE(of, omap_gpio_match);
7c68571f
AB
1337
1338static int omap_gpio_probe(struct platform_device *pdev)
1339{
1340 struct device *dev = &pdev->dev;
1341 struct device_node *node = dev->of_node;
1342 const struct of_device_id *match;
1343 const struct omap_gpio_platform_data *pdata;
7c68571f
AB
1344 struct gpio_bank *bank;
1345 struct irq_chip *irqc;
1346 int ret;
1347
1348 match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1349
1350 pdata = match ? match->data : dev_get_platdata(dev);
1351 if (!pdata)
1352 return -EINVAL;
1353
1354 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
1355 if (!bank)
1356 return -ENOMEM;
1357
1358 irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1359 if (!irqc)
1360 return -ENOMEM;
1361
1362 irqc->irq_startup = omap_gpio_irq_startup,
1363 irqc->irq_shutdown = omap_gpio_irq_shutdown,
693de831 1364 irqc->irq_ack = dummy_irq_chip.irq_ack,
7c68571f
AB
1365 irqc->irq_mask = omap_gpio_mask_irq,
1366 irqc->irq_unmask = omap_gpio_unmask_irq,
1367 irqc->irq_set_type = omap_gpio_irq_type,
1368 irqc->irq_set_wake = omap_gpio_wake_enable,
1369 irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
1370 irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
1371 irqc->name = dev_name(&pdev->dev);
1372 irqc->flags = IRQCHIP_MASK_ON_SUSPEND;
1373 irqc->parent_device = dev;
1374
1375 bank->irq = platform_get_irq(pdev, 0);
1376 if (bank->irq <= 0) {
1377 if (!bank->irq)
1378 bank->irq = -ENXIO;
1379 if (bank->irq != -EPROBE_DEFER)
1380 dev_err(dev,
1381 "can't get irq resource ret=%d\n", bank->irq);
1382 return bank->irq;
1383 }
1384
1385 bank->chip.parent = dev;
1386 bank->chip.owner = THIS_MODULE;
1387 bank->dbck_flag = pdata->dbck_flag;
7c68571f
AB
1388 bank->stride = pdata->bank_stride;
1389 bank->width = pdata->bank_width;
1390 bank->is_mpuio = pdata->is_mpuio;
1391 bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
1392 bank->regs = pdata->regs;
1393#ifdef CONFIG_OF_GPIO
1394 bank->chip.of_node = of_node_get(node);
384ebe1c
BC
1395#endif
1396
7c68571f
AB
1397 if (node) {
1398 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1399 bank->loses_context = true;
1400 } else {
1401 bank->loses_context = pdata->loses_context;
1402
1403 if (bank->loses_context)
1404 bank->get_context_loss_count =
1405 pdata->get_context_loss_count;
1406 }
1407
8ba70595 1408 if (bank->regs->set_dataout && bank->regs->clr_dataout)
7c68571f 1409 bank->set_dataout = omap_set_gpio_dataout_reg;
8ba70595 1410 else
7c68571f 1411 bank->set_dataout = omap_set_gpio_dataout_mask;
7c68571f 1412
7c68571f
AB
1413 raw_spin_lock_init(&bank->lock);
1414 raw_spin_lock_init(&bank->wa_lock);
1415
1416 /* Static mapping, never released */
58f57f86 1417 bank->base = devm_platform_ioremap_resource(pdev, 0);
7c68571f
AB
1418 if (IS_ERR(bank->base)) {
1419 return PTR_ERR(bank->base);
1420 }
1421
1422 if (bank->dbck_flag) {
1423 bank->dbck = devm_clk_get(dev, "dbclk");
1424 if (IS_ERR(bank->dbck)) {
1425 dev_err(dev,
1426 "Could not get gpio dbck. Disable debounce\n");
1427 bank->dbck_flag = false;
1428 } else {
1429 clk_prepare(bank->dbck);
1430 }
1431 }
1432
1433 platform_set_drvdata(pdev, bank);
1434
1435 pm_runtime_enable(dev);
1436 pm_runtime_get_sync(dev);
1437
1438 if (bank->is_mpuio)
1439 omap_mpuio_init(bank);
1440
1441 omap_gpio_mod_init(bank);
1442
1443 ret = omap_gpio_chip_init(bank, irqc);
1444 if (ret) {
1445 pm_runtime_put_sync(dev);
1446 pm_runtime_disable(dev);
1447 if (bank->dbck_flag)
1448 clk_unprepare(bank->dbck);
1449 return ret;
1450 }
1451
1452 omap_gpio_show_rev(bank);
1453
e6818d29
RK
1454 bank->nb.notifier_call = gpio_omap_cpu_notifier;
1455 cpu_pm_register_notifier(&bank->nb);
7c68571f
AB
1456
1457 pm_runtime_put(dev);
1458
1459 return 0;
1460}
1461
1462static int omap_gpio_remove(struct platform_device *pdev)
1463{
1464 struct gpio_bank *bank = platform_get_drvdata(pdev);
1465
e6818d29 1466 cpu_pm_unregister_notifier(&bank->nb);
7c68571f
AB
1467 gpiochip_remove(&bank->chip);
1468 pm_runtime_disable(&pdev->dev);
1469 if (bank->dbck_flag)
1470 clk_unprepare(bank->dbck);
1471
1472 return 0;
1473}
1474
1475static int __maybe_unused omap_gpio_runtime_suspend(struct device *dev)
1476{
1477 struct gpio_bank *bank = dev_get_drvdata(dev);
1478 unsigned long flags;
7c68571f
AB
1479
1480 raw_spin_lock_irqsave(&bank->lock, flags);
7c68571f
AB
1481 omap_gpio_idle(bank, true);
1482 bank->is_suspended = true;
7c68571f
AB
1483 raw_spin_unlock_irqrestore(&bank->lock, flags);
1484
044e499a 1485 return 0;
7c68571f
AB
1486}
1487
1488static int __maybe_unused omap_gpio_runtime_resume(struct device *dev)
1489{
1490 struct gpio_bank *bank = dev_get_drvdata(dev);
1491 unsigned long flags;
7c68571f
AB
1492
1493 raw_spin_lock_irqsave(&bank->lock, flags);
7c68571f
AB
1494 omap_gpio_unidle(bank);
1495 bank->is_suspended = false;
7c68571f
AB
1496 raw_spin_unlock_irqrestore(&bank->lock, flags);
1497
044e499a 1498 return 0;
7c68571f
AB
1499}
1500
1501static const struct dev_pm_ops gpio_pm_ops = {
1502 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1503 NULL)
1504};
1505
77640aab
VC
1506static struct platform_driver omap_gpio_driver = {
1507 .probe = omap_gpio_probe,
cac089f9 1508 .remove = omap_gpio_remove,
77640aab
VC
1509 .driver = {
1510 .name = "omap_gpio",
55b93c32 1511 .pm = &gpio_pm_ops,
7c68571f 1512 .of_match_table = omap_gpio_match,
77640aab
VC
1513 },
1514};
1515
5e1c5ff4 1516/*
77640aab
VC
1517 * gpio driver register needs to be done before
1518 * machine_init functions access gpio APIs.
1519 * Hence omap_gpio_drv_reg() is a postcore_initcall.
5e1c5ff4 1520 */
77640aab 1521static int __init omap_gpio_drv_reg(void)
5e1c5ff4 1522{
77640aab 1523 return platform_driver_register(&omap_gpio_driver);
5e1c5ff4 1524}
77640aab 1525postcore_initcall(omap_gpio_drv_reg);
cac089f9
TL
1526
1527static void __exit omap_gpio_exit(void)
1528{
1529 platform_driver_unregister(&omap_gpio_driver);
1530}
1531module_exit(omap_gpio_exit);
1532
1533MODULE_DESCRIPTION("omap gpio driver");
1534MODULE_ALIAS("platform:gpio-omap");
1535MODULE_LICENSE("GPL v2");