gpio: omap: convert debounce functions switch to use gpio offset
[firefly-linux-kernel-4.4.55.git] / drivers / gpio / gpio-omap.c
1 /*
2  * Support functions for OMAP GPIO
3  *
4  * Copyright (C) 2003-2005 Nokia Corporation
5  * Written by Juha Yrjölä <juha.yrjola@nokia.com>
6  *
7  * Copyright (C) 2009 Texas Instruments
8  * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
9  *
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
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/syscore_ops.h>
19 #include <linux/err.h>
20 #include <linux/clk.h>
21 #include <linux/io.h>
22 #include <linux/device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/pm.h>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
27 #include <linux/gpio.h>
28 #include <linux/bitops.h>
29 #include <linux/platform_data/gpio-omap.h>
30
31 #define OFF_MODE        1
32
33 static LIST_HEAD(omap_gpio_list);
34
35 struct gpio_regs {
36         u32 irqenable1;
37         u32 irqenable2;
38         u32 wake_en;
39         u32 ctrl;
40         u32 oe;
41         u32 leveldetect0;
42         u32 leveldetect1;
43         u32 risingdetect;
44         u32 fallingdetect;
45         u32 dataout;
46         u32 debounce;
47         u32 debounce_en;
48 };
49
50 struct gpio_bank {
51         struct list_head node;
52         void __iomem *base;
53         u16 irq;
54         u32 non_wakeup_gpios;
55         u32 enabled_non_wakeup_gpios;
56         struct gpio_regs context;
57         u32 saved_datain;
58         u32 level_mask;
59         u32 toggle_mask;
60         spinlock_t lock;
61         struct gpio_chip chip;
62         struct clk *dbck;
63         u32 mod_usage;
64         u32 irq_usage;
65         u32 dbck_enable_mask;
66         bool dbck_enabled;
67         struct device *dev;
68         bool is_mpuio;
69         bool dbck_flag;
70         bool loses_context;
71         bool context_valid;
72         int stride;
73         u32 width;
74         int context_loss_count;
75         int power_mode;
76         bool workaround_enabled;
77
78         void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable);
79         int (*get_context_loss_count)(struct device *dev);
80
81         struct omap_gpio_reg_offs *regs;
82 };
83
84 #define GPIO_INDEX(bank, gpio) (gpio % bank->width)
85 #define GPIO_BIT(bank, gpio) (BIT(GPIO_INDEX(bank, gpio)))
86 #define GPIO_MOD_CTRL_BIT       BIT(0)
87
88 #define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
89 #define LINE_USED(line, offset) (line & (BIT(offset)))
90
91 static void omap_gpio_unmask_irq(struct irq_data *d);
92
93 static int omap_irq_to_gpio(struct gpio_bank *bank, unsigned int gpio_irq)
94 {
95         return bank->chip.base + gpio_irq;
96 }
97
98 static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
99 {
100         struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
101         return container_of(chip, struct gpio_bank, chip);
102 }
103
104 static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
105                                     int is_input)
106 {
107         void __iomem *reg = bank->base;
108         u32 l;
109
110         reg += bank->regs->direction;
111         l = readl_relaxed(reg);
112         if (is_input)
113                 l |= BIT(gpio);
114         else
115                 l &= ~(BIT(gpio));
116         writel_relaxed(l, reg);
117         bank->context.oe = l;
118 }
119
120
121 /* set data out value using dedicate set/clear register */
122 static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
123                                       int enable)
124 {
125         void __iomem *reg = bank->base;
126         u32 l = BIT(offset);
127
128         if (enable) {
129                 reg += bank->regs->set_dataout;
130                 bank->context.dataout |= l;
131         } else {
132                 reg += bank->regs->clr_dataout;
133                 bank->context.dataout &= ~l;
134         }
135
136         writel_relaxed(l, reg);
137 }
138
139 /* set data out value using mask register */
140 static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
141                                        int enable)
142 {
143         void __iomem *reg = bank->base + bank->regs->dataout;
144         u32 gpio_bit = BIT(offset);
145         u32 l;
146
147         l = readl_relaxed(reg);
148         if (enable)
149                 l |= gpio_bit;
150         else
151                 l &= ~gpio_bit;
152         writel_relaxed(l, reg);
153         bank->context.dataout = l;
154 }
155
156 static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
157 {
158         void __iomem *reg = bank->base + bank->regs->datain;
159
160         return (readl_relaxed(reg) & (BIT(offset))) != 0;
161 }
162
163 static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
164 {
165         void __iomem *reg = bank->base + bank->regs->dataout;
166
167         return (readl_relaxed(reg) & (BIT(offset))) != 0;
168 }
169
170 static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
171 {
172         int l = readl_relaxed(base + reg);
173
174         if (set)
175                 l |= mask;
176         else
177                 l &= ~mask;
178
179         writel_relaxed(l, base + reg);
180 }
181
182 static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
183 {
184         if (bank->dbck_enable_mask && !bank->dbck_enabled) {
185                 clk_prepare_enable(bank->dbck);
186                 bank->dbck_enabled = true;
187
188                 writel_relaxed(bank->dbck_enable_mask,
189                              bank->base + bank->regs->debounce_en);
190         }
191 }
192
193 static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
194 {
195         if (bank->dbck_enable_mask && bank->dbck_enabled) {
196                 /*
197                  * Disable debounce before cutting it's clock. If debounce is
198                  * enabled but the clock is not, GPIO module seems to be unable
199                  * to detect events and generate interrupts at least on OMAP3.
200                  */
201                 writel_relaxed(0, bank->base + bank->regs->debounce_en);
202
203                 clk_disable_unprepare(bank->dbck);
204                 bank->dbck_enabled = false;
205         }
206 }
207
208 /**
209  * omap2_set_gpio_debounce - low level gpio debounce time
210  * @bank: the gpio bank we're acting upon
211  * @offset: the gpio number on this @bank
212  * @debounce: debounce time to use
213  *
214  * OMAP's debounce time is in 31us steps so we need
215  * to convert and round up to the closest unit.
216  */
217 static void omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
218                                     unsigned debounce)
219 {
220         void __iomem            *reg;
221         u32                     val;
222         u32                     l;
223
224         if (!bank->dbck_flag)
225                 return;
226
227         if (debounce < 32)
228                 debounce = 0x01;
229         else if (debounce > 7936)
230                 debounce = 0xff;
231         else
232                 debounce = (debounce / 0x1f) - 1;
233
234         l = BIT(offset);
235
236         clk_prepare_enable(bank->dbck);
237         reg = bank->base + bank->regs->debounce;
238         writel_relaxed(debounce, reg);
239
240         reg = bank->base + bank->regs->debounce_en;
241         val = readl_relaxed(reg);
242
243         if (debounce)
244                 val |= l;
245         else
246                 val &= ~l;
247         bank->dbck_enable_mask = val;
248
249         writel_relaxed(val, reg);
250         clk_disable_unprepare(bank->dbck);
251         /*
252          * Enable debounce clock per module.
253          * This call is mandatory because in omap_gpio_request() when
254          * *_runtime_get_sync() is called,  _gpio_dbck_enable() within
255          * runtime callbck fails to turn on dbck because dbck_enable_mask
256          * used within _gpio_dbck_enable() is still not initialized at
257          * that point. Therefore we have to enable dbck here.
258          */
259         omap_gpio_dbck_enable(bank);
260         if (bank->dbck_enable_mask) {
261                 bank->context.debounce = debounce;
262                 bank->context.debounce_en = val;
263         }
264 }
265
266 /**
267  * omap_clear_gpio_debounce - clear debounce settings for a gpio
268  * @bank: the gpio bank we're acting upon
269  * @offset: the gpio number on this @bank
270  *
271  * If a gpio is using debounce, then clear the debounce enable bit and if
272  * this is the only gpio in this bank using debounce, then clear the debounce
273  * time too. The debounce clock will also be disabled when calling this function
274  * if this is the only gpio in the bank using debounce.
275  */
276 static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
277 {
278         u32 gpio_bit = BIT(offset);
279
280         if (!bank->dbck_flag)
281                 return;
282
283         if (!(bank->dbck_enable_mask & gpio_bit))
284                 return;
285
286         bank->dbck_enable_mask &= ~gpio_bit;
287         bank->context.debounce_en &= ~gpio_bit;
288         writel_relaxed(bank->context.debounce_en,
289                      bank->base + bank->regs->debounce_en);
290
291         if (!bank->dbck_enable_mask) {
292                 bank->context.debounce = 0;
293                 writel_relaxed(bank->context.debounce, bank->base +
294                              bank->regs->debounce);
295                 clk_disable_unprepare(bank->dbck);
296                 bank->dbck_enabled = false;
297         }
298 }
299
300 static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
301                                                 unsigned trigger)
302 {
303         void __iomem *base = bank->base;
304         u32 gpio_bit = BIT(gpio);
305
306         omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
307                       trigger & IRQ_TYPE_LEVEL_LOW);
308         omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
309                       trigger & IRQ_TYPE_LEVEL_HIGH);
310         omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
311                       trigger & IRQ_TYPE_EDGE_RISING);
312         omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
313                       trigger & IRQ_TYPE_EDGE_FALLING);
314
315         bank->context.leveldetect0 =
316                         readl_relaxed(bank->base + bank->regs->leveldetect0);
317         bank->context.leveldetect1 =
318                         readl_relaxed(bank->base + bank->regs->leveldetect1);
319         bank->context.risingdetect =
320                         readl_relaxed(bank->base + bank->regs->risingdetect);
321         bank->context.fallingdetect =
322                         readl_relaxed(bank->base + bank->regs->fallingdetect);
323
324         if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
325                 omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
326                 bank->context.wake_en =
327                         readl_relaxed(bank->base + bank->regs->wkup_en);
328         }
329
330         /* This part needs to be executed always for OMAP{34xx, 44xx} */
331         if (!bank->regs->irqctrl) {
332                 /* On omap24xx proceed only when valid GPIO bit is set */
333                 if (bank->non_wakeup_gpios) {
334                         if (!(bank->non_wakeup_gpios & gpio_bit))
335                                 goto exit;
336                 }
337
338                 /*
339                  * Log the edge gpio and manually trigger the IRQ
340                  * after resume if the input level changes
341                  * to avoid irq lost during PER RET/OFF mode
342                  * Applies for omap2 non-wakeup gpio and all omap3 gpios
343                  */
344                 if (trigger & IRQ_TYPE_EDGE_BOTH)
345                         bank->enabled_non_wakeup_gpios |= gpio_bit;
346                 else
347                         bank->enabled_non_wakeup_gpios &= ~gpio_bit;
348         }
349
350 exit:
351         bank->level_mask =
352                 readl_relaxed(bank->base + bank->regs->leveldetect0) |
353                 readl_relaxed(bank->base + bank->regs->leveldetect1);
354 }
355
356 #ifdef CONFIG_ARCH_OMAP1
357 /*
358  * This only applies to chips that can't do both rising and falling edge
359  * detection at once.  For all other chips, this function is a noop.
360  */
361 static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
362 {
363         void __iomem *reg = bank->base;
364         u32 l = 0;
365
366         if (!bank->regs->irqctrl)
367                 return;
368
369         reg += bank->regs->irqctrl;
370
371         l = readl_relaxed(reg);
372         if ((l >> gpio) & 1)
373                 l &= ~(BIT(gpio));
374         else
375                 l |= BIT(gpio);
376
377         writel_relaxed(l, reg);
378 }
379 #else
380 static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
381 #endif
382
383 static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
384                                     unsigned trigger)
385 {
386         void __iomem *reg = bank->base;
387         void __iomem *base = bank->base;
388         u32 l = 0;
389
390         if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
391                 omap_set_gpio_trigger(bank, gpio, trigger);
392         } else if (bank->regs->irqctrl) {
393                 reg += bank->regs->irqctrl;
394
395                 l = readl_relaxed(reg);
396                 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
397                         bank->toggle_mask |= BIT(gpio);
398                 if (trigger & IRQ_TYPE_EDGE_RISING)
399                         l |= BIT(gpio);
400                 else if (trigger & IRQ_TYPE_EDGE_FALLING)
401                         l &= ~(BIT(gpio));
402                 else
403                         return -EINVAL;
404
405                 writel_relaxed(l, reg);
406         } else if (bank->regs->edgectrl1) {
407                 if (gpio & 0x08)
408                         reg += bank->regs->edgectrl2;
409                 else
410                         reg += bank->regs->edgectrl1;
411
412                 gpio &= 0x07;
413                 l = readl_relaxed(reg);
414                 l &= ~(3 << (gpio << 1));
415                 if (trigger & IRQ_TYPE_EDGE_RISING)
416                         l |= 2 << (gpio << 1);
417                 if (trigger & IRQ_TYPE_EDGE_FALLING)
418                         l |= BIT(gpio << 1);
419
420                 /* Enable wake-up during idle for dynamic tick */
421                 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
422                 bank->context.wake_en =
423                         readl_relaxed(bank->base + bank->regs->wkup_en);
424                 writel_relaxed(l, reg);
425         }
426         return 0;
427 }
428
429 static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
430 {
431         if (bank->regs->pinctrl) {
432                 void __iomem *reg = bank->base + bank->regs->pinctrl;
433
434                 /* Claim the pin for MPU */
435                 writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
436         }
437
438         if (bank->regs->ctrl && !BANK_USED(bank)) {
439                 void __iomem *reg = bank->base + bank->regs->ctrl;
440                 u32 ctrl;
441
442                 ctrl = readl_relaxed(reg);
443                 /* Module is enabled, clocks are not gated */
444                 ctrl &= ~GPIO_MOD_CTRL_BIT;
445                 writel_relaxed(ctrl, reg);
446                 bank->context.ctrl = ctrl;
447         }
448 }
449
450 static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
451 {
452         void __iomem *base = bank->base;
453
454         if (bank->regs->wkup_en &&
455             !LINE_USED(bank->mod_usage, offset) &&
456             !LINE_USED(bank->irq_usage, offset)) {
457                 /* Disable wake-up during idle for dynamic tick */
458                 omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
459                 bank->context.wake_en =
460                         readl_relaxed(bank->base + bank->regs->wkup_en);
461         }
462
463         if (bank->regs->ctrl && !BANK_USED(bank)) {
464                 void __iomem *reg = bank->base + bank->regs->ctrl;
465                 u32 ctrl;
466
467                 ctrl = readl_relaxed(reg);
468                 /* Module is disabled, clocks are gated */
469                 ctrl |= GPIO_MOD_CTRL_BIT;
470                 writel_relaxed(ctrl, reg);
471                 bank->context.ctrl = ctrl;
472         }
473 }
474
475 static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
476 {
477         void __iomem *reg = bank->base + bank->regs->direction;
478
479         return readl_relaxed(reg) & BIT(offset);
480 }
481
482 static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned gpio,
483                                unsigned offset)
484 {
485         if (!LINE_USED(bank->mod_usage, offset)) {
486                 omap_enable_gpio_module(bank, offset);
487                 omap_set_gpio_direction(bank, offset, 1);
488         }
489         bank->irq_usage |= BIT(GPIO_INDEX(bank, gpio));
490 }
491
492 static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
493 {
494         struct gpio_bank *bank = omap_irq_data_get_bank(d);
495         unsigned gpio = 0;
496         int retval;
497         unsigned long flags;
498         unsigned offset;
499
500         if (!BANK_USED(bank))
501                 pm_runtime_get_sync(bank->dev);
502
503 #ifdef CONFIG_ARCH_OMAP1
504         if (d->irq > IH_MPUIO_BASE)
505                 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
506 #endif
507
508         if (!gpio)
509                 gpio = omap_irq_to_gpio(bank, d->hwirq);
510
511         if (type & ~IRQ_TYPE_SENSE_MASK)
512                 return -EINVAL;
513
514         if (!bank->regs->leveldetect0 &&
515                 (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
516                 return -EINVAL;
517
518         spin_lock_irqsave(&bank->lock, flags);
519         offset = GPIO_INDEX(bank, gpio);
520         retval = omap_set_gpio_triggering(bank, offset, type);
521         omap_gpio_init_irq(bank, gpio, offset);
522         if (!omap_gpio_is_input(bank, offset)) {
523                 spin_unlock_irqrestore(&bank->lock, flags);
524                 return -EINVAL;
525         }
526         spin_unlock_irqrestore(&bank->lock, flags);
527
528         if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
529                 __irq_set_handler_locked(d->irq, handle_level_irq);
530         else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
531                 __irq_set_handler_locked(d->irq, handle_edge_irq);
532
533         return retval;
534 }
535
536 static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
537 {
538         void __iomem *reg = bank->base;
539
540         reg += bank->regs->irqstatus;
541         writel_relaxed(gpio_mask, reg);
542
543         /* Workaround for clearing DSP GPIO interrupts to allow retention */
544         if (bank->regs->irqstatus2) {
545                 reg = bank->base + bank->regs->irqstatus2;
546                 writel_relaxed(gpio_mask, reg);
547         }
548
549         /* Flush posted write for the irq status to avoid spurious interrupts */
550         readl_relaxed(reg);
551 }
552
553 static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
554 {
555         omap_clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
556 }
557
558 static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
559 {
560         void __iomem *reg = bank->base;
561         u32 l;
562         u32 mask = (BIT(bank->width)) - 1;
563
564         reg += bank->regs->irqenable;
565         l = readl_relaxed(reg);
566         if (bank->regs->irqenable_inv)
567                 l = ~l;
568         l &= mask;
569         return l;
570 }
571
572 static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
573 {
574         void __iomem *reg = bank->base;
575         u32 l;
576
577         if (bank->regs->set_irqenable) {
578                 reg += bank->regs->set_irqenable;
579                 l = gpio_mask;
580                 bank->context.irqenable1 |= gpio_mask;
581         } else {
582                 reg += bank->regs->irqenable;
583                 l = readl_relaxed(reg);
584                 if (bank->regs->irqenable_inv)
585                         l &= ~gpio_mask;
586                 else
587                         l |= gpio_mask;
588                 bank->context.irqenable1 = l;
589         }
590
591         writel_relaxed(l, reg);
592 }
593
594 static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
595 {
596         void __iomem *reg = bank->base;
597         u32 l;
598
599         if (bank->regs->clr_irqenable) {
600                 reg += bank->regs->clr_irqenable;
601                 l = gpio_mask;
602                 bank->context.irqenable1 &= ~gpio_mask;
603         } else {
604                 reg += bank->regs->irqenable;
605                 l = readl_relaxed(reg);
606                 if (bank->regs->irqenable_inv)
607                         l |= gpio_mask;
608                 else
609                         l &= ~gpio_mask;
610                 bank->context.irqenable1 = l;
611         }
612
613         writel_relaxed(l, reg);
614 }
615
616 static inline void omap_set_gpio_irqenable(struct gpio_bank *bank, int gpio,
617                                            int enable)
618 {
619         if (enable)
620                 omap_enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
621         else
622                 omap_disable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
623 }
624
625 /*
626  * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
627  * 1510 does not seem to have a wake-up register. If JTAG is connected
628  * to the target, system will wake up always on GPIO events. While
629  * system is running all registered GPIO interrupts need to have wake-up
630  * enabled. When system is suspended, only selected GPIO interrupts need
631  * to have wake-up enabled.
632  */
633 static int omap_set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
634 {
635         u32 gpio_bit = GPIO_BIT(bank, gpio);
636         unsigned long flags;
637
638         if (bank->non_wakeup_gpios & gpio_bit) {
639                 dev_err(bank->dev,
640                         "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
641                 return -EINVAL;
642         }
643
644         spin_lock_irqsave(&bank->lock, flags);
645         if (enable)
646                 bank->context.wake_en |= gpio_bit;
647         else
648                 bank->context.wake_en &= ~gpio_bit;
649
650         writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
651         spin_unlock_irqrestore(&bank->lock, flags);
652
653         return 0;
654 }
655
656 static void omap_reset_gpio(struct gpio_bank *bank, int gpio)
657 {
658         omap_set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
659         omap_set_gpio_irqenable(bank, gpio, 0);
660         omap_clear_gpio_irqstatus(bank, gpio);
661         omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
662         omap_clear_gpio_debounce(bank, GPIO_INDEX(bank, gpio));
663 }
664
665 /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
666 static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
667 {
668         struct gpio_bank *bank = omap_irq_data_get_bank(d);
669         unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
670
671         return omap_set_gpio_wakeup(bank, gpio, enable);
672 }
673
674 static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
675 {
676         struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
677         unsigned long flags;
678
679         /*
680          * If this is the first gpio_request for the bank,
681          * enable the bank module.
682          */
683         if (!BANK_USED(bank))
684                 pm_runtime_get_sync(bank->dev);
685
686         spin_lock_irqsave(&bank->lock, flags);
687         /* Set trigger to none. You need to enable the desired trigger with
688          * request_irq() or set_irq_type(). Only do this if the IRQ line has
689          * not already been requested.
690          */
691         if (!LINE_USED(bank->irq_usage, offset)) {
692                 omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
693                 omap_enable_gpio_module(bank, offset);
694         }
695         bank->mod_usage |= BIT(offset);
696         spin_unlock_irqrestore(&bank->lock, flags);
697
698         return 0;
699 }
700
701 static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
702 {
703         struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
704         unsigned long flags;
705
706         spin_lock_irqsave(&bank->lock, flags);
707         bank->mod_usage &= ~(BIT(offset));
708         omap_disable_gpio_module(bank, offset);
709         omap_reset_gpio(bank, bank->chip.base + offset);
710         spin_unlock_irqrestore(&bank->lock, flags);
711
712         /*
713          * If this is the last gpio to be freed in the bank,
714          * disable the bank module.
715          */
716         if (!BANK_USED(bank))
717                 pm_runtime_put(bank->dev);
718 }
719
720 /*
721  * We need to unmask the GPIO bank interrupt as soon as possible to
722  * avoid missing GPIO interrupts for other lines in the bank.
723  * Then we need to mask-read-clear-unmask the triggered GPIO lines
724  * in the bank to avoid missing nested interrupts for a GPIO line.
725  * If we wait to unmask individual GPIO lines in the bank after the
726  * line's interrupt handler has been run, we may miss some nested
727  * interrupts.
728  */
729 static void omap_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
730 {
731         void __iomem *isr_reg = NULL;
732         u32 isr;
733         unsigned int bit;
734         struct gpio_bank *bank;
735         int unmasked = 0;
736         struct irq_chip *irqchip = irq_desc_get_chip(desc);
737         struct gpio_chip *chip = irq_get_handler_data(irq);
738
739         chained_irq_enter(irqchip, desc);
740
741         bank = container_of(chip, struct gpio_bank, chip);
742         isr_reg = bank->base + bank->regs->irqstatus;
743         pm_runtime_get_sync(bank->dev);
744
745         if (WARN_ON(!isr_reg))
746                 goto exit;
747
748         while (1) {
749                 u32 isr_saved, level_mask = 0;
750                 u32 enabled;
751
752                 enabled = omap_get_gpio_irqbank_mask(bank);
753                 isr_saved = isr = readl_relaxed(isr_reg) & enabled;
754
755                 if (bank->level_mask)
756                         level_mask = bank->level_mask & enabled;
757
758                 /* clear edge sensitive interrupts before handler(s) are
759                 called so that we don't miss any interrupt occurred while
760                 executing them */
761                 omap_disable_gpio_irqbank(bank, isr_saved & ~level_mask);
762                 omap_clear_gpio_irqbank(bank, isr_saved & ~level_mask);
763                 omap_enable_gpio_irqbank(bank, isr_saved & ~level_mask);
764
765                 /* if there is only edge sensitive GPIO pin interrupts
766                 configured, we could unmask GPIO bank interrupt immediately */
767                 if (!level_mask && !unmasked) {
768                         unmasked = 1;
769                         chained_irq_exit(irqchip, desc);
770                 }
771
772                 if (!isr)
773                         break;
774
775                 while (isr) {
776                         bit = __ffs(isr);
777                         isr &= ~(BIT(bit));
778
779                         /*
780                          * Some chips can't respond to both rising and falling
781                          * at the same time.  If this irq was requested with
782                          * both flags, we need to flip the ICR data for the IRQ
783                          * to respond to the IRQ for the opposite direction.
784                          * This will be indicated in the bank toggle_mask.
785                          */
786                         if (bank->toggle_mask & (BIT(bit)))
787                                 omap_toggle_gpio_edge_triggering(bank, bit);
788
789                         generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
790                                                             bit));
791                 }
792         }
793         /* if bank has any level sensitive GPIO pin interrupt
794         configured, we must unmask the bank interrupt only after
795         handler(s) are executed in order to avoid spurious bank
796         interrupt */
797 exit:
798         if (!unmasked)
799                 chained_irq_exit(irqchip, desc);
800         pm_runtime_put(bank->dev);
801 }
802
803 static unsigned int omap_gpio_irq_startup(struct irq_data *d)
804 {
805         struct gpio_bank *bank = omap_irq_data_get_bank(d);
806         unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
807         unsigned long flags;
808         unsigned offset = GPIO_INDEX(bank, gpio);
809
810         if (!BANK_USED(bank))
811                 pm_runtime_get_sync(bank->dev);
812
813         spin_lock_irqsave(&bank->lock, flags);
814         omap_gpio_init_irq(bank, gpio, offset);
815         spin_unlock_irqrestore(&bank->lock, flags);
816         omap_gpio_unmask_irq(d);
817
818         return 0;
819 }
820
821 static void omap_gpio_irq_shutdown(struct irq_data *d)
822 {
823         struct gpio_bank *bank = omap_irq_data_get_bank(d);
824         unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
825         unsigned long flags;
826         unsigned offset = GPIO_INDEX(bank, gpio);
827
828         spin_lock_irqsave(&bank->lock, flags);
829         bank->irq_usage &= ~(BIT(offset));
830         omap_disable_gpio_module(bank, offset);
831         omap_reset_gpio(bank, gpio);
832         spin_unlock_irqrestore(&bank->lock, flags);
833
834         /*
835          * If this is the last IRQ to be freed in the bank,
836          * disable the bank module.
837          */
838         if (!BANK_USED(bank))
839                 pm_runtime_put(bank->dev);
840 }
841
842 static void omap_gpio_ack_irq(struct irq_data *d)
843 {
844         struct gpio_bank *bank = omap_irq_data_get_bank(d);
845         unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
846
847         omap_clear_gpio_irqstatus(bank, gpio);
848 }
849
850 static void omap_gpio_mask_irq(struct irq_data *d)
851 {
852         struct gpio_bank *bank = omap_irq_data_get_bank(d);
853         unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
854         unsigned long flags;
855
856         spin_lock_irqsave(&bank->lock, flags);
857         omap_set_gpio_irqenable(bank, gpio, 0);
858         omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
859         spin_unlock_irqrestore(&bank->lock, flags);
860 }
861
862 static void omap_gpio_unmask_irq(struct irq_data *d)
863 {
864         struct gpio_bank *bank = omap_irq_data_get_bank(d);
865         unsigned int gpio = omap_irq_to_gpio(bank, d->hwirq);
866         unsigned int irq_mask = GPIO_BIT(bank, gpio);
867         u32 trigger = irqd_get_trigger_type(d);
868         unsigned long flags;
869
870         spin_lock_irqsave(&bank->lock, flags);
871         if (trigger)
872                 omap_set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
873
874         /* For level-triggered GPIOs, the clearing must be done after
875          * the HW source is cleared, thus after the handler has run */
876         if (bank->level_mask & irq_mask) {
877                 omap_set_gpio_irqenable(bank, gpio, 0);
878                 omap_clear_gpio_irqstatus(bank, gpio);
879         }
880
881         omap_set_gpio_irqenable(bank, gpio, 1);
882         spin_unlock_irqrestore(&bank->lock, flags);
883 }
884
885 /*---------------------------------------------------------------------*/
886
887 static int omap_mpuio_suspend_noirq(struct device *dev)
888 {
889         struct platform_device *pdev = to_platform_device(dev);
890         struct gpio_bank        *bank = platform_get_drvdata(pdev);
891         void __iomem            *mask_reg = bank->base +
892                                         OMAP_MPUIO_GPIO_MASKIT / bank->stride;
893         unsigned long           flags;
894
895         spin_lock_irqsave(&bank->lock, flags);
896         writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
897         spin_unlock_irqrestore(&bank->lock, flags);
898
899         return 0;
900 }
901
902 static int omap_mpuio_resume_noirq(struct device *dev)
903 {
904         struct platform_device *pdev = to_platform_device(dev);
905         struct gpio_bank        *bank = platform_get_drvdata(pdev);
906         void __iomem            *mask_reg = bank->base +
907                                         OMAP_MPUIO_GPIO_MASKIT / bank->stride;
908         unsigned long           flags;
909
910         spin_lock_irqsave(&bank->lock, flags);
911         writel_relaxed(bank->context.wake_en, mask_reg);
912         spin_unlock_irqrestore(&bank->lock, flags);
913
914         return 0;
915 }
916
917 static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
918         .suspend_noirq = omap_mpuio_suspend_noirq,
919         .resume_noirq = omap_mpuio_resume_noirq,
920 };
921
922 /* use platform_driver for this. */
923 static struct platform_driver omap_mpuio_driver = {
924         .driver         = {
925                 .name   = "mpuio",
926                 .pm     = &omap_mpuio_dev_pm_ops,
927         },
928 };
929
930 static struct platform_device omap_mpuio_device = {
931         .name           = "mpuio",
932         .id             = -1,
933         .dev = {
934                 .driver = &omap_mpuio_driver.driver,
935         }
936         /* could list the /proc/iomem resources */
937 };
938
939 static inline void omap_mpuio_init(struct gpio_bank *bank)
940 {
941         platform_set_drvdata(&omap_mpuio_device, bank);
942
943         if (platform_driver_register(&omap_mpuio_driver) == 0)
944                 (void) platform_device_register(&omap_mpuio_device);
945 }
946
947 /*---------------------------------------------------------------------*/
948
949 static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
950 {
951         struct gpio_bank *bank;
952         unsigned long flags;
953         void __iomem *reg;
954         int dir;
955
956         bank = container_of(chip, struct gpio_bank, chip);
957         reg = bank->base + bank->regs->direction;
958         spin_lock_irqsave(&bank->lock, flags);
959         dir = !!(readl_relaxed(reg) & BIT(offset));
960         spin_unlock_irqrestore(&bank->lock, flags);
961         return dir;
962 }
963
964 static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
965 {
966         struct gpio_bank *bank;
967         unsigned long flags;
968
969         bank = container_of(chip, struct gpio_bank, chip);
970         spin_lock_irqsave(&bank->lock, flags);
971         omap_set_gpio_direction(bank, offset, 1);
972         spin_unlock_irqrestore(&bank->lock, flags);
973         return 0;
974 }
975
976 static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
977 {
978         struct gpio_bank *bank;
979
980         bank = container_of(chip, struct gpio_bank, chip);
981
982         if (omap_gpio_is_input(bank, offset))
983                 return omap_get_gpio_datain(bank, offset);
984         else
985                 return omap_get_gpio_dataout(bank, offset);
986 }
987
988 static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
989 {
990         struct gpio_bank *bank;
991         unsigned long flags;
992
993         bank = container_of(chip, struct gpio_bank, chip);
994         spin_lock_irqsave(&bank->lock, flags);
995         bank->set_dataout(bank, offset, value);
996         omap_set_gpio_direction(bank, offset, 0);
997         spin_unlock_irqrestore(&bank->lock, flags);
998         return 0;
999 }
1000
1001 static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
1002                               unsigned debounce)
1003 {
1004         struct gpio_bank *bank;
1005         unsigned long flags;
1006
1007         bank = container_of(chip, struct gpio_bank, chip);
1008
1009         spin_lock_irqsave(&bank->lock, flags);
1010         omap2_set_gpio_debounce(bank, offset, debounce);
1011         spin_unlock_irqrestore(&bank->lock, flags);
1012
1013         return 0;
1014 }
1015
1016 static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1017 {
1018         struct gpio_bank *bank;
1019         unsigned long flags;
1020
1021         bank = container_of(chip, struct gpio_bank, chip);
1022         spin_lock_irqsave(&bank->lock, flags);
1023         bank->set_dataout(bank, offset, value);
1024         spin_unlock_irqrestore(&bank->lock, flags);
1025 }
1026
1027 /*---------------------------------------------------------------------*/
1028
1029 static void __init omap_gpio_show_rev(struct gpio_bank *bank)
1030 {
1031         static bool called;
1032         u32 rev;
1033
1034         if (called || bank->regs->revision == USHRT_MAX)
1035                 return;
1036
1037         rev = readw_relaxed(bank->base + bank->regs->revision);
1038         pr_info("OMAP GPIO hardware version %d.%d\n",
1039                 (rev >> 4) & 0x0f, rev & 0x0f);
1040
1041         called = true;
1042 }
1043
1044 static void omap_gpio_mod_init(struct gpio_bank *bank)
1045 {
1046         void __iomem *base = bank->base;
1047         u32 l = 0xffffffff;
1048
1049         if (bank->width == 16)
1050                 l = 0xffff;
1051
1052         if (bank->is_mpuio) {
1053                 writel_relaxed(l, bank->base + bank->regs->irqenable);
1054                 return;
1055         }
1056
1057         omap_gpio_rmw(base, bank->regs->irqenable, l,
1058                       bank->regs->irqenable_inv);
1059         omap_gpio_rmw(base, bank->regs->irqstatus, l,
1060                       !bank->regs->irqenable_inv);
1061         if (bank->regs->debounce_en)
1062                 writel_relaxed(0, base + bank->regs->debounce_en);
1063
1064         /* Save OE default value (0xffffffff) in the context */
1065         bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
1066          /* Initialize interface clk ungated, module enabled */
1067         if (bank->regs->ctrl)
1068                 writel_relaxed(0, base + bank->regs->ctrl);
1069
1070         bank->dbck = clk_get(bank->dev, "dbclk");
1071         if (IS_ERR(bank->dbck))
1072                 dev_err(bank->dev, "Could not get gpio dbck\n");
1073 }
1074
1075 static void
1076 omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
1077                     unsigned int num)
1078 {
1079         struct irq_chip_generic *gc;
1080         struct irq_chip_type *ct;
1081
1082         gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
1083                                     handle_simple_irq);
1084         if (!gc) {
1085                 dev_err(bank->dev, "Memory alloc failed for gc\n");
1086                 return;
1087         }
1088
1089         ct = gc->chip_types;
1090
1091         /* NOTE: No ack required, reading IRQ status clears it. */
1092         ct->chip.irq_mask = irq_gc_mask_set_bit;
1093         ct->chip.irq_unmask = irq_gc_mask_clr_bit;
1094         ct->chip.irq_set_type = omap_gpio_irq_type;
1095
1096         if (bank->regs->wkup_en)
1097                 ct->chip.irq_set_wake = omap_gpio_wake_enable;
1098
1099         ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
1100         irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
1101                                IRQ_NOREQUEST | IRQ_NOPROBE, 0);
1102 }
1103
1104 static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
1105 {
1106         int j;
1107         static int gpio;
1108         int irq_base = 0;
1109         int ret;
1110
1111         /*
1112          * REVISIT eventually switch from OMAP-specific gpio structs
1113          * over to the generic ones
1114          */
1115         bank->chip.request = omap_gpio_request;
1116         bank->chip.free = omap_gpio_free;
1117         bank->chip.get_direction = omap_gpio_get_direction;
1118         bank->chip.direction_input = omap_gpio_input;
1119         bank->chip.get = omap_gpio_get;
1120         bank->chip.direction_output = omap_gpio_output;
1121         bank->chip.set_debounce = omap_gpio_debounce;
1122         bank->chip.set = omap_gpio_set;
1123         if (bank->is_mpuio) {
1124                 bank->chip.label = "mpuio";
1125                 if (bank->regs->wkup_en)
1126                         bank->chip.dev = &omap_mpuio_device.dev;
1127                 bank->chip.base = OMAP_MPUIO(0);
1128         } else {
1129                 bank->chip.label = "gpio";
1130                 bank->chip.base = gpio;
1131                 gpio += bank->width;
1132         }
1133         bank->chip.ngpio = bank->width;
1134
1135         ret = gpiochip_add(&bank->chip);
1136         if (ret) {
1137                 dev_err(bank->dev, "Could not register gpio chip %d\n", ret);
1138                 return ret;
1139         }
1140
1141 #ifdef CONFIG_ARCH_OMAP1
1142         /*
1143          * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1144          * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1145          */
1146         irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
1147         if (irq_base < 0) {
1148                 dev_err(bank->dev, "Couldn't allocate IRQ numbers\n");
1149                 return -ENODEV;
1150         }
1151 #endif
1152
1153         ret = gpiochip_irqchip_add(&bank->chip, irqc,
1154                                    irq_base, omap_gpio_irq_handler,
1155                                    IRQ_TYPE_NONE);
1156
1157         if (ret) {
1158                 dev_err(bank->dev, "Couldn't add irqchip to gpiochip %d\n", ret);
1159                 gpiochip_remove(&bank->chip);
1160                 return -ENODEV;
1161         }
1162
1163         gpiochip_set_chained_irqchip(&bank->chip, irqc,
1164                                      bank->irq, omap_gpio_irq_handler);
1165
1166         for (j = 0; j < bank->width; j++) {
1167                 int irq = irq_find_mapping(bank->chip.irqdomain, j);
1168                 if (bank->is_mpuio) {
1169                         omap_mpuio_alloc_gc(bank, irq, bank->width);
1170                         irq_set_chip_and_handler(irq, NULL, NULL);
1171                         set_irq_flags(irq, 0);
1172                 }
1173         }
1174
1175         return 0;
1176 }
1177
1178 static const struct of_device_id omap_gpio_match[];
1179
1180 static int omap_gpio_probe(struct platform_device *pdev)
1181 {
1182         struct device *dev = &pdev->dev;
1183         struct device_node *node = dev->of_node;
1184         const struct of_device_id *match;
1185         const struct omap_gpio_platform_data *pdata;
1186         struct resource *res;
1187         struct gpio_bank *bank;
1188         struct irq_chip *irqc;
1189         int ret;
1190
1191         match = of_match_device(of_match_ptr(omap_gpio_match), dev);
1192
1193         pdata = match ? match->data : dev_get_platdata(dev);
1194         if (!pdata)
1195                 return -EINVAL;
1196
1197         bank = devm_kzalloc(dev, sizeof(struct gpio_bank), GFP_KERNEL);
1198         if (!bank) {
1199                 dev_err(dev, "Memory alloc failed\n");
1200                 return -ENOMEM;
1201         }
1202
1203         irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
1204         if (!irqc)
1205                 return -ENOMEM;
1206
1207         irqc->irq_startup = omap_gpio_irq_startup,
1208         irqc->irq_shutdown = omap_gpio_irq_shutdown,
1209         irqc->irq_ack = omap_gpio_ack_irq,
1210         irqc->irq_mask = omap_gpio_mask_irq,
1211         irqc->irq_unmask = omap_gpio_unmask_irq,
1212         irqc->irq_set_type = omap_gpio_irq_type,
1213         irqc->irq_set_wake = omap_gpio_wake_enable,
1214         irqc->name = dev_name(&pdev->dev);
1215
1216         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1217         if (unlikely(!res)) {
1218                 dev_err(dev, "Invalid IRQ resource\n");
1219                 return -ENODEV;
1220         }
1221
1222         bank->irq = res->start;
1223         bank->dev = dev;
1224         bank->chip.dev = dev;
1225         bank->dbck_flag = pdata->dbck_flag;
1226         bank->stride = pdata->bank_stride;
1227         bank->width = pdata->bank_width;
1228         bank->is_mpuio = pdata->is_mpuio;
1229         bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
1230         bank->regs = pdata->regs;
1231 #ifdef CONFIG_OF_GPIO
1232         bank->chip.of_node = of_node_get(node);
1233 #endif
1234         if (node) {
1235                 if (!of_property_read_bool(node, "ti,gpio-always-on"))
1236                         bank->loses_context = true;
1237         } else {
1238                 bank->loses_context = pdata->loses_context;
1239
1240                 if (bank->loses_context)
1241                         bank->get_context_loss_count =
1242                                 pdata->get_context_loss_count;
1243         }
1244
1245         if (bank->regs->set_dataout && bank->regs->clr_dataout)
1246                 bank->set_dataout = omap_set_gpio_dataout_reg;
1247         else
1248                 bank->set_dataout = omap_set_gpio_dataout_mask;
1249
1250         spin_lock_init(&bank->lock);
1251
1252         /* Static mapping, never released */
1253         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1254         bank->base = devm_ioremap_resource(dev, res);
1255         if (IS_ERR(bank->base)) {
1256                 irq_domain_remove(bank->chip.irqdomain);
1257                 return PTR_ERR(bank->base);
1258         }
1259
1260         platform_set_drvdata(pdev, bank);
1261
1262         pm_runtime_enable(bank->dev);
1263         pm_runtime_irq_safe(bank->dev);
1264         pm_runtime_get_sync(bank->dev);
1265
1266         if (bank->is_mpuio)
1267                 omap_mpuio_init(bank);
1268
1269         omap_gpio_mod_init(bank);
1270
1271         ret = omap_gpio_chip_init(bank, irqc);
1272         if (ret)
1273                 return ret;
1274
1275         omap_gpio_show_rev(bank);
1276
1277         pm_runtime_put(bank->dev);
1278
1279         list_add_tail(&bank->node, &omap_gpio_list);
1280
1281         return 0;
1282 }
1283
1284 #ifdef CONFIG_ARCH_OMAP2PLUS
1285
1286 #if defined(CONFIG_PM)
1287 static void omap_gpio_restore_context(struct gpio_bank *bank);
1288
1289 static int omap_gpio_runtime_suspend(struct device *dev)
1290 {
1291         struct platform_device *pdev = to_platform_device(dev);
1292         struct gpio_bank *bank = platform_get_drvdata(pdev);
1293         u32 l1 = 0, l2 = 0;
1294         unsigned long flags;
1295         u32 wake_low, wake_hi;
1296
1297         spin_lock_irqsave(&bank->lock, flags);
1298
1299         /*
1300          * Only edges can generate a wakeup event to the PRCM.
1301          *
1302          * Therefore, ensure any wake-up capable GPIOs have
1303          * edge-detection enabled before going idle to ensure a wakeup
1304          * to the PRCM is generated on a GPIO transition. (c.f. 34xx
1305          * NDA TRM 25.5.3.1)
1306          *
1307          * The normal values will be restored upon ->runtime_resume()
1308          * by writing back the values saved in bank->context.
1309          */
1310         wake_low = bank->context.leveldetect0 & bank->context.wake_en;
1311         if (wake_low)
1312                 writel_relaxed(wake_low | bank->context.fallingdetect,
1313                              bank->base + bank->regs->fallingdetect);
1314         wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
1315         if (wake_hi)
1316                 writel_relaxed(wake_hi | bank->context.risingdetect,
1317                              bank->base + bank->regs->risingdetect);
1318
1319         if (!bank->enabled_non_wakeup_gpios)
1320                 goto update_gpio_context_count;
1321
1322         if (bank->power_mode != OFF_MODE) {
1323                 bank->power_mode = 0;
1324                 goto update_gpio_context_count;
1325         }
1326         /*
1327          * If going to OFF, remove triggering for all
1328          * non-wakeup GPIOs.  Otherwise spurious IRQs will be
1329          * generated.  See OMAP2420 Errata item 1.101.
1330          */
1331         bank->saved_datain = readl_relaxed(bank->base +
1332                                                 bank->regs->datain);
1333         l1 = bank->context.fallingdetect;
1334         l2 = bank->context.risingdetect;
1335
1336         l1 &= ~bank->enabled_non_wakeup_gpios;
1337         l2 &= ~bank->enabled_non_wakeup_gpios;
1338
1339         writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
1340         writel_relaxed(l2, bank->base + bank->regs->risingdetect);
1341
1342         bank->workaround_enabled = true;
1343
1344 update_gpio_context_count:
1345         if (bank->get_context_loss_count)
1346                 bank->context_loss_count =
1347                                 bank->get_context_loss_count(bank->dev);
1348
1349         omap_gpio_dbck_disable(bank);
1350         spin_unlock_irqrestore(&bank->lock, flags);
1351
1352         return 0;
1353 }
1354
1355 static void omap_gpio_init_context(struct gpio_bank *p);
1356
1357 static int omap_gpio_runtime_resume(struct device *dev)
1358 {
1359         struct platform_device *pdev = to_platform_device(dev);
1360         struct gpio_bank *bank = platform_get_drvdata(pdev);
1361         u32 l = 0, gen, gen0, gen1;
1362         unsigned long flags;
1363         int c;
1364
1365         spin_lock_irqsave(&bank->lock, flags);
1366
1367         /*
1368          * On the first resume during the probe, the context has not
1369          * been initialised and so initialise it now. Also initialise
1370          * the context loss count.
1371          */
1372         if (bank->loses_context && !bank->context_valid) {
1373                 omap_gpio_init_context(bank);
1374
1375                 if (bank->get_context_loss_count)
1376                         bank->context_loss_count =
1377                                 bank->get_context_loss_count(bank->dev);
1378         }
1379
1380         omap_gpio_dbck_enable(bank);
1381
1382         /*
1383          * In ->runtime_suspend(), level-triggered, wakeup-enabled
1384          * GPIOs were set to edge trigger also in order to be able to
1385          * generate a PRCM wakeup.  Here we restore the
1386          * pre-runtime_suspend() values for edge triggering.
1387          */
1388         writel_relaxed(bank->context.fallingdetect,
1389                      bank->base + bank->regs->fallingdetect);
1390         writel_relaxed(bank->context.risingdetect,
1391                      bank->base + bank->regs->risingdetect);
1392
1393         if (bank->loses_context) {
1394                 if (!bank->get_context_loss_count) {
1395                         omap_gpio_restore_context(bank);
1396                 } else {
1397                         c = bank->get_context_loss_count(bank->dev);
1398                         if (c != bank->context_loss_count) {
1399                                 omap_gpio_restore_context(bank);
1400                         } else {
1401                                 spin_unlock_irqrestore(&bank->lock, flags);
1402                                 return 0;
1403                         }
1404                 }
1405         }
1406
1407         if (!bank->workaround_enabled) {
1408                 spin_unlock_irqrestore(&bank->lock, flags);
1409                 return 0;
1410         }
1411
1412         l = readl_relaxed(bank->base + bank->regs->datain);
1413
1414         /*
1415          * Check if any of the non-wakeup interrupt GPIOs have changed
1416          * state.  If so, generate an IRQ by software.  This is
1417          * horribly racy, but it's the best we can do to work around
1418          * this silicon bug.
1419          */
1420         l ^= bank->saved_datain;
1421         l &= bank->enabled_non_wakeup_gpios;
1422
1423         /*
1424          * No need to generate IRQs for the rising edge for gpio IRQs
1425          * configured with falling edge only; and vice versa.
1426          */
1427         gen0 = l & bank->context.fallingdetect;
1428         gen0 &= bank->saved_datain;
1429
1430         gen1 = l & bank->context.risingdetect;
1431         gen1 &= ~(bank->saved_datain);
1432
1433         /* FIXME: Consider GPIO IRQs with level detections properly! */
1434         gen = l & (~(bank->context.fallingdetect) &
1435                                          ~(bank->context.risingdetect));
1436         /* Consider all GPIO IRQs needed to be updated */
1437         gen |= gen0 | gen1;
1438
1439         if (gen) {
1440                 u32 old0, old1;
1441
1442                 old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
1443                 old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
1444
1445                 if (!bank->regs->irqstatus_raw0) {
1446                         writel_relaxed(old0 | gen, bank->base +
1447                                                 bank->regs->leveldetect0);
1448                         writel_relaxed(old1 | gen, bank->base +
1449                                                 bank->regs->leveldetect1);
1450                 }
1451
1452                 if (bank->regs->irqstatus_raw0) {
1453                         writel_relaxed(old0 | l, bank->base +
1454                                                 bank->regs->leveldetect0);
1455                         writel_relaxed(old1 | l, bank->base +
1456                                                 bank->regs->leveldetect1);
1457                 }
1458                 writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
1459                 writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
1460         }
1461
1462         bank->workaround_enabled = false;
1463         spin_unlock_irqrestore(&bank->lock, flags);
1464
1465         return 0;
1466 }
1467 #endif /* CONFIG_PM */
1468
1469 void omap2_gpio_prepare_for_idle(int pwr_mode)
1470 {
1471         struct gpio_bank *bank;
1472
1473         list_for_each_entry(bank, &omap_gpio_list, node) {
1474                 if (!BANK_USED(bank) || !bank->loses_context)
1475                         continue;
1476
1477                 bank->power_mode = pwr_mode;
1478
1479                 pm_runtime_put_sync_suspend(bank->dev);
1480         }
1481 }
1482
1483 void omap2_gpio_resume_after_idle(void)
1484 {
1485         struct gpio_bank *bank;
1486
1487         list_for_each_entry(bank, &omap_gpio_list, node) {
1488                 if (!BANK_USED(bank) || !bank->loses_context)
1489                         continue;
1490
1491                 pm_runtime_get_sync(bank->dev);
1492         }
1493 }
1494
1495 #if defined(CONFIG_PM)
1496 static void omap_gpio_init_context(struct gpio_bank *p)
1497 {
1498         struct omap_gpio_reg_offs *regs = p->regs;
1499         void __iomem *base = p->base;
1500
1501         p->context.ctrl         = readl_relaxed(base + regs->ctrl);
1502         p->context.oe           = readl_relaxed(base + regs->direction);
1503         p->context.wake_en      = readl_relaxed(base + regs->wkup_en);
1504         p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0);
1505         p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1);
1506         p->context.risingdetect = readl_relaxed(base + regs->risingdetect);
1507         p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
1508         p->context.irqenable1   = readl_relaxed(base + regs->irqenable);
1509         p->context.irqenable2   = readl_relaxed(base + regs->irqenable2);
1510
1511         if (regs->set_dataout && p->regs->clr_dataout)
1512                 p->context.dataout = readl_relaxed(base + regs->set_dataout);
1513         else
1514                 p->context.dataout = readl_relaxed(base + regs->dataout);
1515
1516         p->context_valid = true;
1517 }
1518
1519 static void omap_gpio_restore_context(struct gpio_bank *bank)
1520 {
1521         writel_relaxed(bank->context.wake_en,
1522                                 bank->base + bank->regs->wkup_en);
1523         writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
1524         writel_relaxed(bank->context.leveldetect0,
1525                                 bank->base + bank->regs->leveldetect0);
1526         writel_relaxed(bank->context.leveldetect1,
1527                                 bank->base + bank->regs->leveldetect1);
1528         writel_relaxed(bank->context.risingdetect,
1529                                 bank->base + bank->regs->risingdetect);
1530         writel_relaxed(bank->context.fallingdetect,
1531                                 bank->base + bank->regs->fallingdetect);
1532         if (bank->regs->set_dataout && bank->regs->clr_dataout)
1533                 writel_relaxed(bank->context.dataout,
1534                                 bank->base + bank->regs->set_dataout);
1535         else
1536                 writel_relaxed(bank->context.dataout,
1537                                 bank->base + bank->regs->dataout);
1538         writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
1539
1540         if (bank->dbck_enable_mask) {
1541                 writel_relaxed(bank->context.debounce, bank->base +
1542                                         bank->regs->debounce);
1543                 writel_relaxed(bank->context.debounce_en,
1544                                         bank->base + bank->regs->debounce_en);
1545         }
1546
1547         writel_relaxed(bank->context.irqenable1,
1548                                 bank->base + bank->regs->irqenable);
1549         writel_relaxed(bank->context.irqenable2,
1550                                 bank->base + bank->regs->irqenable2);
1551 }
1552 #endif /* CONFIG_PM */
1553 #else
1554 #define omap_gpio_runtime_suspend NULL
1555 #define omap_gpio_runtime_resume NULL
1556 static inline void omap_gpio_init_context(struct gpio_bank *p) {}
1557 #endif
1558
1559 static const struct dev_pm_ops gpio_pm_ops = {
1560         SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
1561                                                                         NULL)
1562 };
1563
1564 #if defined(CONFIG_OF)
1565 static struct omap_gpio_reg_offs omap2_gpio_regs = {
1566         .revision =             OMAP24XX_GPIO_REVISION,
1567         .direction =            OMAP24XX_GPIO_OE,
1568         .datain =               OMAP24XX_GPIO_DATAIN,
1569         .dataout =              OMAP24XX_GPIO_DATAOUT,
1570         .set_dataout =          OMAP24XX_GPIO_SETDATAOUT,
1571         .clr_dataout =          OMAP24XX_GPIO_CLEARDATAOUT,
1572         .irqstatus =            OMAP24XX_GPIO_IRQSTATUS1,
1573         .irqstatus2 =           OMAP24XX_GPIO_IRQSTATUS2,
1574         .irqenable =            OMAP24XX_GPIO_IRQENABLE1,
1575         .irqenable2 =           OMAP24XX_GPIO_IRQENABLE2,
1576         .set_irqenable =        OMAP24XX_GPIO_SETIRQENABLE1,
1577         .clr_irqenable =        OMAP24XX_GPIO_CLEARIRQENABLE1,
1578         .debounce =             OMAP24XX_GPIO_DEBOUNCE_VAL,
1579         .debounce_en =          OMAP24XX_GPIO_DEBOUNCE_EN,
1580         .ctrl =                 OMAP24XX_GPIO_CTRL,
1581         .wkup_en =              OMAP24XX_GPIO_WAKE_EN,
1582         .leveldetect0 =         OMAP24XX_GPIO_LEVELDETECT0,
1583         .leveldetect1 =         OMAP24XX_GPIO_LEVELDETECT1,
1584         .risingdetect =         OMAP24XX_GPIO_RISINGDETECT,
1585         .fallingdetect =        OMAP24XX_GPIO_FALLINGDETECT,
1586 };
1587
1588 static struct omap_gpio_reg_offs omap4_gpio_regs = {
1589         .revision =             OMAP4_GPIO_REVISION,
1590         .direction =            OMAP4_GPIO_OE,
1591         .datain =               OMAP4_GPIO_DATAIN,
1592         .dataout =              OMAP4_GPIO_DATAOUT,
1593         .set_dataout =          OMAP4_GPIO_SETDATAOUT,
1594         .clr_dataout =          OMAP4_GPIO_CLEARDATAOUT,
1595         .irqstatus =            OMAP4_GPIO_IRQSTATUS0,
1596         .irqstatus2 =           OMAP4_GPIO_IRQSTATUS1,
1597         .irqenable =            OMAP4_GPIO_IRQSTATUSSET0,
1598         .irqenable2 =           OMAP4_GPIO_IRQSTATUSSET1,
1599         .set_irqenable =        OMAP4_GPIO_IRQSTATUSSET0,
1600         .clr_irqenable =        OMAP4_GPIO_IRQSTATUSCLR0,
1601         .debounce =             OMAP4_GPIO_DEBOUNCINGTIME,
1602         .debounce_en =          OMAP4_GPIO_DEBOUNCENABLE,
1603         .ctrl =                 OMAP4_GPIO_CTRL,
1604         .wkup_en =              OMAP4_GPIO_IRQWAKEN0,
1605         .leveldetect0 =         OMAP4_GPIO_LEVELDETECT0,
1606         .leveldetect1 =         OMAP4_GPIO_LEVELDETECT1,
1607         .risingdetect =         OMAP4_GPIO_RISINGDETECT,
1608         .fallingdetect =        OMAP4_GPIO_FALLINGDETECT,
1609 };
1610
1611 static const struct omap_gpio_platform_data omap2_pdata = {
1612         .regs = &omap2_gpio_regs,
1613         .bank_width = 32,
1614         .dbck_flag = false,
1615 };
1616
1617 static const struct omap_gpio_platform_data omap3_pdata = {
1618         .regs = &omap2_gpio_regs,
1619         .bank_width = 32,
1620         .dbck_flag = true,
1621 };
1622
1623 static const struct omap_gpio_platform_data omap4_pdata = {
1624         .regs = &omap4_gpio_regs,
1625         .bank_width = 32,
1626         .dbck_flag = true,
1627 };
1628
1629 static const struct of_device_id omap_gpio_match[] = {
1630         {
1631                 .compatible = "ti,omap4-gpio",
1632                 .data = &omap4_pdata,
1633         },
1634         {
1635                 .compatible = "ti,omap3-gpio",
1636                 .data = &omap3_pdata,
1637         },
1638         {
1639                 .compatible = "ti,omap2-gpio",
1640                 .data = &omap2_pdata,
1641         },
1642         { },
1643 };
1644 MODULE_DEVICE_TABLE(of, omap_gpio_match);
1645 #endif
1646
1647 static struct platform_driver omap_gpio_driver = {
1648         .probe          = omap_gpio_probe,
1649         .driver         = {
1650                 .name   = "omap_gpio",
1651                 .pm     = &gpio_pm_ops,
1652                 .of_match_table = of_match_ptr(omap_gpio_match),
1653         },
1654 };
1655
1656 /*
1657  * gpio driver register needs to be done before
1658  * machine_init functions access gpio APIs.
1659  * Hence omap_gpio_drv_reg() is a postcore_initcall.
1660  */
1661 static int __init omap_gpio_drv_reg(void)
1662 {
1663         return platform_driver_register(&omap_gpio_driver);
1664 }
1665 postcore_initcall(omap_gpio_drv_reg);