2 * Support functions for OMAP GPIO
4 * Copyright (C) 2003-2005 Nokia Corporation
5 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
7 * Copyright (C) 2009 Texas Instruments
8 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
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.
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>
22 #include <linux/slab.h>
23 #include <linux/pm_runtime.h>
26 #include <mach/hardware.h>
28 #include <mach/irqs.h>
30 #include <asm/mach/irq.h>
34 static LIST_HEAD(omap_gpio_list
);
52 struct list_head node
;
56 u16 virtual_irq_start
;
60 u32 enabled_non_wakeup_gpios
;
61 struct gpio_regs context
;
63 u32 saved_fallingdetect
;
64 u32 saved_risingdetect
;
68 struct gpio_chip chip
;
79 int context_loss_count
;
82 bool workaround_enabled
;
84 void (*set_dataout
)(struct gpio_bank
*bank
, int gpio
, int enable
);
85 int (*get_context_loss_count
)(struct device
*dev
);
87 struct omap_gpio_reg_offs
*regs
;
90 #define GPIO_INDEX(bank, gpio) (gpio % bank->width)
91 #define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
92 #define GPIO_MOD_CTRL_BIT BIT(0)
94 static void _set_gpio_direction(struct gpio_bank
*bank
, int gpio
, int is_input
)
96 void __iomem
*reg
= bank
->base
;
99 reg
+= bank
->regs
->direction
;
100 l
= __raw_readl(reg
);
105 __raw_writel(l
, reg
);
106 bank
->context
.oe
= l
;
110 /* set data out value using dedicate set/clear register */
111 static void _set_gpio_dataout_reg(struct gpio_bank
*bank
, int gpio
, int enable
)
113 void __iomem
*reg
= bank
->base
;
114 u32 l
= GPIO_BIT(bank
, gpio
);
117 reg
+= bank
->regs
->set_dataout
;
119 reg
+= bank
->regs
->clr_dataout
;
121 __raw_writel(l
, reg
);
124 /* set data out value using mask register */
125 static void _set_gpio_dataout_mask(struct gpio_bank
*bank
, int gpio
, int enable
)
127 void __iomem
*reg
= bank
->base
+ bank
->regs
->dataout
;
128 u32 gpio_bit
= GPIO_BIT(bank
, gpio
);
131 l
= __raw_readl(reg
);
136 __raw_writel(l
, reg
);
137 bank
->context
.dataout
= l
;
140 static int _get_gpio_datain(struct gpio_bank
*bank
, int gpio
)
142 void __iomem
*reg
= bank
->base
+ bank
->regs
->datain
;
144 return (__raw_readl(reg
) & GPIO_BIT(bank
, gpio
)) != 0;
147 static int _get_gpio_dataout(struct gpio_bank
*bank
, int gpio
)
149 void __iomem
*reg
= bank
->base
+ bank
->regs
->dataout
;
151 return (__raw_readl(reg
) & GPIO_BIT(bank
, gpio
)) != 0;
154 static inline void _gpio_rmw(void __iomem
*base
, u32 reg
, u32 mask
, bool set
)
156 int l
= __raw_readl(base
+ reg
);
163 __raw_writel(l
, base
+ reg
);
166 static inline void _gpio_dbck_enable(struct gpio_bank
*bank
)
168 if (bank
->dbck_enable_mask
&& !bank
->dbck_enabled
) {
169 clk_enable(bank
->dbck
);
170 bank
->dbck_enabled
= true;
174 static inline void _gpio_dbck_disable(struct gpio_bank
*bank
)
176 if (bank
->dbck_enable_mask
&& bank
->dbck_enabled
) {
177 clk_disable(bank
->dbck
);
178 bank
->dbck_enabled
= false;
183 * _set_gpio_debounce - low level gpio debounce time
184 * @bank: the gpio bank we're acting upon
185 * @gpio: the gpio number on this @gpio
186 * @debounce: debounce time to use
188 * OMAP's debounce time is in 31us steps so we need
189 * to convert and round up to the closest unit.
191 static void _set_gpio_debounce(struct gpio_bank
*bank
, unsigned gpio
,
198 if (!bank
->dbck_flag
)
203 else if (debounce
> 7936)
206 debounce
= (debounce
/ 0x1f) - 1;
208 l
= GPIO_BIT(bank
, gpio
);
210 clk_enable(bank
->dbck
);
211 reg
= bank
->base
+ bank
->regs
->debounce
;
212 __raw_writel(debounce
, reg
);
214 reg
= bank
->base
+ bank
->regs
->debounce_en
;
215 val
= __raw_readl(reg
);
221 bank
->dbck_enable_mask
= val
;
223 __raw_writel(val
, reg
);
224 clk_disable(bank
->dbck
);
226 * Enable debounce clock per module.
227 * This call is mandatory because in omap_gpio_request() when
228 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
229 * runtime callbck fails to turn on dbck because dbck_enable_mask
230 * used within _gpio_dbck_enable() is still not initialized at
231 * that point. Therefore we have to enable dbck here.
233 _gpio_dbck_enable(bank
);
234 if (bank
->dbck_enable_mask
) {
235 bank
->context
.debounce
= debounce
;
236 bank
->context
.debounce_en
= val
;
240 static inline void set_gpio_trigger(struct gpio_bank
*bank
, int gpio
,
243 void __iomem
*base
= bank
->base
;
244 u32 gpio_bit
= 1 << gpio
;
246 _gpio_rmw(base
, bank
->regs
->leveldetect0
, gpio_bit
,
247 trigger
& IRQ_TYPE_LEVEL_LOW
);
248 _gpio_rmw(base
, bank
->regs
->leveldetect1
, gpio_bit
,
249 trigger
& IRQ_TYPE_LEVEL_HIGH
);
250 _gpio_rmw(base
, bank
->regs
->risingdetect
, gpio_bit
,
251 trigger
& IRQ_TYPE_EDGE_RISING
);
252 _gpio_rmw(base
, bank
->regs
->fallingdetect
, gpio_bit
,
253 trigger
& IRQ_TYPE_EDGE_FALLING
);
255 bank
->context
.leveldetect0
=
256 __raw_readl(bank
->base
+ bank
->regs
->leveldetect0
);
257 bank
->context
.leveldetect1
=
258 __raw_readl(bank
->base
+ bank
->regs
->leveldetect1
);
259 bank
->context
.risingdetect
=
260 __raw_readl(bank
->base
+ bank
->regs
->risingdetect
);
261 bank
->context
.fallingdetect
=
262 __raw_readl(bank
->base
+ bank
->regs
->fallingdetect
);
264 if (likely(!(bank
->non_wakeup_gpios
& gpio_bit
))) {
265 _gpio_rmw(base
, bank
->regs
->wkup_en
, gpio_bit
, trigger
!= 0);
266 bank
->context
.wake_en
=
267 __raw_readl(bank
->base
+ bank
->regs
->wkup_en
);
270 /* This part needs to be executed always for OMAP{34xx, 44xx} */
271 if (!bank
->regs
->irqctrl
) {
272 /* On omap24xx proceed only when valid GPIO bit is set */
273 if (bank
->non_wakeup_gpios
) {
274 if (!(bank
->non_wakeup_gpios
& gpio_bit
))
279 * Log the edge gpio and manually trigger the IRQ
280 * after resume if the input level changes
281 * to avoid irq lost during PER RET/OFF mode
282 * Applies for omap2 non-wakeup gpio and all omap3 gpios
284 if (trigger
& IRQ_TYPE_EDGE_BOTH
)
285 bank
->enabled_non_wakeup_gpios
|= gpio_bit
;
287 bank
->enabled_non_wakeup_gpios
&= ~gpio_bit
;
292 __raw_readl(bank
->base
+ bank
->regs
->leveldetect0
) |
293 __raw_readl(bank
->base
+ bank
->regs
->leveldetect1
);
296 #ifdef CONFIG_ARCH_OMAP1
298 * This only applies to chips that can't do both rising and falling edge
299 * detection at once. For all other chips, this function is a noop.
301 static void _toggle_gpio_edge_triggering(struct gpio_bank
*bank
, int gpio
)
303 void __iomem
*reg
= bank
->base
;
306 if (!bank
->regs
->irqctrl
)
309 reg
+= bank
->regs
->irqctrl
;
311 l
= __raw_readl(reg
);
317 __raw_writel(l
, reg
);
320 static void _toggle_gpio_edge_triggering(struct gpio_bank
*bank
, int gpio
) {}
323 static int _set_gpio_triggering(struct gpio_bank
*bank
, int gpio
, int trigger
)
325 void __iomem
*reg
= bank
->base
;
326 void __iomem
*base
= bank
->base
;
329 if (bank
->regs
->leveldetect0
&& bank
->regs
->wkup_en
) {
330 set_gpio_trigger(bank
, gpio
, trigger
);
331 } else if (bank
->regs
->irqctrl
) {
332 reg
+= bank
->regs
->irqctrl
;
334 l
= __raw_readl(reg
);
335 if ((trigger
& IRQ_TYPE_SENSE_MASK
) == IRQ_TYPE_EDGE_BOTH
)
336 bank
->toggle_mask
|= 1 << gpio
;
337 if (trigger
& IRQ_TYPE_EDGE_RISING
)
339 else if (trigger
& IRQ_TYPE_EDGE_FALLING
)
344 __raw_writel(l
, reg
);
345 } else if (bank
->regs
->edgectrl1
) {
347 reg
+= bank
->regs
->edgectrl2
;
349 reg
+= bank
->regs
->edgectrl1
;
352 l
= __raw_readl(reg
);
353 l
&= ~(3 << (gpio
<< 1));
354 if (trigger
& IRQ_TYPE_EDGE_RISING
)
355 l
|= 2 << (gpio
<< 1);
356 if (trigger
& IRQ_TYPE_EDGE_FALLING
)
357 l
|= 1 << (gpio
<< 1);
359 /* Enable wake-up during idle for dynamic tick */
360 _gpio_rmw(base
, bank
->regs
->wkup_en
, 1 << gpio
, trigger
);
361 bank
->context
.wake_en
=
362 __raw_readl(bank
->base
+ bank
->regs
->wkup_en
);
363 __raw_writel(l
, reg
);
368 static int gpio_irq_type(struct irq_data
*d
, unsigned type
)
370 struct gpio_bank
*bank
;
375 if (!cpu_class_is_omap2() && d
->irq
> IH_MPUIO_BASE
)
376 gpio
= OMAP_MPUIO(d
->irq
- IH_MPUIO_BASE
);
378 gpio
= d
->irq
- IH_GPIO_BASE
;
380 if (type
& ~IRQ_TYPE_SENSE_MASK
)
383 bank
= irq_data_get_irq_chip_data(d
);
385 if (!bank
->regs
->leveldetect0
&&
386 (type
& (IRQ_TYPE_LEVEL_LOW
|IRQ_TYPE_LEVEL_HIGH
)))
389 spin_lock_irqsave(&bank
->lock
, flags
);
390 retval
= _set_gpio_triggering(bank
, GPIO_INDEX(bank
, gpio
), type
);
391 spin_unlock_irqrestore(&bank
->lock
, flags
);
393 if (type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
394 __irq_set_handler_locked(d
->irq
, handle_level_irq
);
395 else if (type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
396 __irq_set_handler_locked(d
->irq
, handle_edge_irq
);
401 static void _clear_gpio_irqbank(struct gpio_bank
*bank
, int gpio_mask
)
403 void __iomem
*reg
= bank
->base
;
405 reg
+= bank
->regs
->irqstatus
;
406 __raw_writel(gpio_mask
, reg
);
408 /* Workaround for clearing DSP GPIO interrupts to allow retention */
409 if (bank
->regs
->irqstatus2
) {
410 reg
= bank
->base
+ bank
->regs
->irqstatus2
;
411 __raw_writel(gpio_mask
, reg
);
414 /* Flush posted write for the irq status to avoid spurious interrupts */
418 static inline void _clear_gpio_irqstatus(struct gpio_bank
*bank
, int gpio
)
420 _clear_gpio_irqbank(bank
, GPIO_BIT(bank
, gpio
));
423 static u32
_get_gpio_irqbank_mask(struct gpio_bank
*bank
)
425 void __iomem
*reg
= bank
->base
;
427 u32 mask
= (1 << bank
->width
) - 1;
429 reg
+= bank
->regs
->irqenable
;
430 l
= __raw_readl(reg
);
431 if (bank
->regs
->irqenable_inv
)
437 static void _enable_gpio_irqbank(struct gpio_bank
*bank
, int gpio_mask
)
439 void __iomem
*reg
= bank
->base
;
442 if (bank
->regs
->set_irqenable
) {
443 reg
+= bank
->regs
->set_irqenable
;
446 reg
+= bank
->regs
->irqenable
;
447 l
= __raw_readl(reg
);
448 if (bank
->regs
->irqenable_inv
)
454 __raw_writel(l
, reg
);
455 bank
->context
.irqenable1
= l
;
458 static void _disable_gpio_irqbank(struct gpio_bank
*bank
, int gpio_mask
)
460 void __iomem
*reg
= bank
->base
;
463 if (bank
->regs
->clr_irqenable
) {
464 reg
+= bank
->regs
->clr_irqenable
;
467 reg
+= bank
->regs
->irqenable
;
468 l
= __raw_readl(reg
);
469 if (bank
->regs
->irqenable_inv
)
475 __raw_writel(l
, reg
);
476 bank
->context
.irqenable1
= l
;
479 static inline void _set_gpio_irqenable(struct gpio_bank
*bank
, int gpio
, int enable
)
481 _enable_gpio_irqbank(bank
, GPIO_BIT(bank
, gpio
));
485 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
486 * 1510 does not seem to have a wake-up register. If JTAG is connected
487 * to the target, system will wake up always on GPIO events. While
488 * system is running all registered GPIO interrupts need to have wake-up
489 * enabled. When system is suspended, only selected GPIO interrupts need
490 * to have wake-up enabled.
492 static int _set_gpio_wakeup(struct gpio_bank
*bank
, int gpio
, int enable
)
494 u32 gpio_bit
= GPIO_BIT(bank
, gpio
);
497 if (bank
->non_wakeup_gpios
& gpio_bit
) {
499 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio
);
503 spin_lock_irqsave(&bank
->lock
, flags
);
505 bank
->suspend_wakeup
|= gpio_bit
;
507 bank
->suspend_wakeup
&= ~gpio_bit
;
509 spin_unlock_irqrestore(&bank
->lock
, flags
);
514 static void _reset_gpio(struct gpio_bank
*bank
, int gpio
)
516 _set_gpio_direction(bank
, GPIO_INDEX(bank
, gpio
), 1);
517 _set_gpio_irqenable(bank
, gpio
, 0);
518 _clear_gpio_irqstatus(bank
, gpio
);
519 _set_gpio_triggering(bank
, GPIO_INDEX(bank
, gpio
), IRQ_TYPE_NONE
);
522 /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
523 static int gpio_wake_enable(struct irq_data
*d
, unsigned int enable
)
525 unsigned int gpio
= d
->irq
- IH_GPIO_BASE
;
526 struct gpio_bank
*bank
;
529 bank
= irq_data_get_irq_chip_data(d
);
530 retval
= _set_gpio_wakeup(bank
, gpio
, enable
);
535 static int omap_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
537 struct gpio_bank
*bank
= container_of(chip
, struct gpio_bank
, chip
);
541 * If this is the first gpio_request for the bank,
542 * enable the bank module.
544 if (!bank
->mod_usage
)
545 pm_runtime_get_sync(bank
->dev
);
547 spin_lock_irqsave(&bank
->lock
, flags
);
548 /* Set trigger to none. You need to enable the desired trigger with
549 * request_irq() or set_irq_type().
551 _set_gpio_triggering(bank
, offset
, IRQ_TYPE_NONE
);
553 if (bank
->regs
->pinctrl
) {
554 void __iomem
*reg
= bank
->base
+ bank
->regs
->pinctrl
;
556 /* Claim the pin for MPU */
557 __raw_writel(__raw_readl(reg
) | (1 << offset
), reg
);
560 if (bank
->regs
->ctrl
&& !bank
->mod_usage
) {
561 void __iomem
*reg
= bank
->base
+ bank
->regs
->ctrl
;
564 ctrl
= __raw_readl(reg
);
565 /* Module is enabled, clocks are not gated */
566 ctrl
&= ~GPIO_MOD_CTRL_BIT
;
567 __raw_writel(ctrl
, reg
);
568 bank
->context
.ctrl
= ctrl
;
571 bank
->mod_usage
|= 1 << offset
;
573 spin_unlock_irqrestore(&bank
->lock
, flags
);
578 static void omap_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
580 struct gpio_bank
*bank
= container_of(chip
, struct gpio_bank
, chip
);
581 void __iomem
*base
= bank
->base
;
584 spin_lock_irqsave(&bank
->lock
, flags
);
586 if (bank
->regs
->wkup_en
) {
587 /* Disable wake-up during idle for dynamic tick */
588 _gpio_rmw(base
, bank
->regs
->wkup_en
, 1 << offset
, 0);
589 bank
->context
.wake_en
=
590 __raw_readl(bank
->base
+ bank
->regs
->wkup_en
);
593 bank
->mod_usage
&= ~(1 << offset
);
595 if (bank
->regs
->ctrl
&& !bank
->mod_usage
) {
596 void __iomem
*reg
= bank
->base
+ bank
->regs
->ctrl
;
599 ctrl
= __raw_readl(reg
);
600 /* Module is disabled, clocks are gated */
601 ctrl
|= GPIO_MOD_CTRL_BIT
;
602 __raw_writel(ctrl
, reg
);
603 bank
->context
.ctrl
= ctrl
;
606 _reset_gpio(bank
, bank
->chip
.base
+ offset
);
607 spin_unlock_irqrestore(&bank
->lock
, flags
);
610 * If this is the last gpio to be freed in the bank,
611 * disable the bank module.
613 if (!bank
->mod_usage
)
614 pm_runtime_put(bank
->dev
);
618 * We need to unmask the GPIO bank interrupt as soon as possible to
619 * avoid missing GPIO interrupts for other lines in the bank.
620 * Then we need to mask-read-clear-unmask the triggered GPIO lines
621 * in the bank to avoid missing nested interrupts for a GPIO line.
622 * If we wait to unmask individual GPIO lines in the bank after the
623 * line's interrupt handler has been run, we may miss some nested
626 static void gpio_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
628 void __iomem
*isr_reg
= NULL
;
630 unsigned int gpio_irq
, gpio_index
;
631 struct gpio_bank
*bank
;
634 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
636 chained_irq_enter(chip
, desc
);
638 bank
= irq_get_handler_data(irq
);
639 isr_reg
= bank
->base
+ bank
->regs
->irqstatus
;
640 pm_runtime_get_sync(bank
->dev
);
642 if (WARN_ON(!isr_reg
))
646 u32 isr_saved
, level_mask
= 0;
649 enabled
= _get_gpio_irqbank_mask(bank
);
650 isr_saved
= isr
= __raw_readl(isr_reg
) & enabled
;
652 if (bank
->level_mask
)
653 level_mask
= bank
->level_mask
& enabled
;
655 /* clear edge sensitive interrupts before handler(s) are
656 called so that we don't miss any interrupt occurred while
658 _disable_gpio_irqbank(bank
, isr_saved
& ~level_mask
);
659 _clear_gpio_irqbank(bank
, isr_saved
& ~level_mask
);
660 _enable_gpio_irqbank(bank
, isr_saved
& ~level_mask
);
662 /* if there is only edge sensitive GPIO pin interrupts
663 configured, we could unmask GPIO bank interrupt immediately */
664 if (!level_mask
&& !unmasked
) {
666 chained_irq_exit(chip
, desc
);
674 gpio_irq
= bank
->virtual_irq_start
;
675 for (; isr
!= 0; isr
>>= 1, gpio_irq
++) {
676 gpio_index
= GPIO_INDEX(bank
, irq_to_gpio(gpio_irq
));
682 * Some chips can't respond to both rising and falling
683 * at the same time. If this irq was requested with
684 * both flags, we need to flip the ICR data for the IRQ
685 * to respond to the IRQ for the opposite direction.
686 * This will be indicated in the bank toggle_mask.
688 if (bank
->toggle_mask
& (1 << gpio_index
))
689 _toggle_gpio_edge_triggering(bank
, gpio_index
);
691 generic_handle_irq(gpio_irq
);
694 /* if bank has any level sensitive GPIO pin interrupt
695 configured, we must unmask the bank interrupt only after
696 handler(s) are executed in order to avoid spurious bank
700 chained_irq_exit(chip
, desc
);
701 pm_runtime_put(bank
->dev
);
704 static void gpio_irq_shutdown(struct irq_data
*d
)
706 unsigned int gpio
= d
->irq
- IH_GPIO_BASE
;
707 struct gpio_bank
*bank
= irq_data_get_irq_chip_data(d
);
710 spin_lock_irqsave(&bank
->lock
, flags
);
711 _reset_gpio(bank
, gpio
);
712 spin_unlock_irqrestore(&bank
->lock
, flags
);
715 static void gpio_ack_irq(struct irq_data
*d
)
717 unsigned int gpio
= d
->irq
- IH_GPIO_BASE
;
718 struct gpio_bank
*bank
= irq_data_get_irq_chip_data(d
);
720 _clear_gpio_irqstatus(bank
, gpio
);
723 static void gpio_mask_irq(struct irq_data
*d
)
725 unsigned int gpio
= d
->irq
- IH_GPIO_BASE
;
726 struct gpio_bank
*bank
= irq_data_get_irq_chip_data(d
);
729 spin_lock_irqsave(&bank
->lock
, flags
);
730 _set_gpio_irqenable(bank
, gpio
, 0);
731 _set_gpio_triggering(bank
, GPIO_INDEX(bank
, gpio
), IRQ_TYPE_NONE
);
732 spin_unlock_irqrestore(&bank
->lock
, flags
);
735 static void gpio_unmask_irq(struct irq_data
*d
)
737 unsigned int gpio
= d
->irq
- IH_GPIO_BASE
;
738 struct gpio_bank
*bank
= irq_data_get_irq_chip_data(d
);
739 unsigned int irq_mask
= GPIO_BIT(bank
, gpio
);
740 u32 trigger
= irqd_get_trigger_type(d
);
743 spin_lock_irqsave(&bank
->lock
, flags
);
745 _set_gpio_triggering(bank
, GPIO_INDEX(bank
, gpio
), trigger
);
747 /* For level-triggered GPIOs, the clearing must be done after
748 * the HW source is cleared, thus after the handler has run */
749 if (bank
->level_mask
& irq_mask
) {
750 _set_gpio_irqenable(bank
, gpio
, 0);
751 _clear_gpio_irqstatus(bank
, gpio
);
754 _set_gpio_irqenable(bank
, gpio
, 1);
755 spin_unlock_irqrestore(&bank
->lock
, flags
);
758 static struct irq_chip gpio_irq_chip
= {
760 .irq_shutdown
= gpio_irq_shutdown
,
761 .irq_ack
= gpio_ack_irq
,
762 .irq_mask
= gpio_mask_irq
,
763 .irq_unmask
= gpio_unmask_irq
,
764 .irq_set_type
= gpio_irq_type
,
765 .irq_set_wake
= gpio_wake_enable
,
768 /*---------------------------------------------------------------------*/
770 static int omap_mpuio_suspend_noirq(struct device
*dev
)
772 struct platform_device
*pdev
= to_platform_device(dev
);
773 struct gpio_bank
*bank
= platform_get_drvdata(pdev
);
774 void __iomem
*mask_reg
= bank
->base
+
775 OMAP_MPUIO_GPIO_MASKIT
/ bank
->stride
;
778 spin_lock_irqsave(&bank
->lock
, flags
);
779 bank
->saved_wakeup
= __raw_readl(mask_reg
);
780 __raw_writel(0xffff & ~bank
->suspend_wakeup
, mask_reg
);
781 spin_unlock_irqrestore(&bank
->lock
, flags
);
786 static int omap_mpuio_resume_noirq(struct device
*dev
)
788 struct platform_device
*pdev
= to_platform_device(dev
);
789 struct gpio_bank
*bank
= platform_get_drvdata(pdev
);
790 void __iomem
*mask_reg
= bank
->base
+
791 OMAP_MPUIO_GPIO_MASKIT
/ bank
->stride
;
794 spin_lock_irqsave(&bank
->lock
, flags
);
795 __raw_writel(bank
->saved_wakeup
, mask_reg
);
796 spin_unlock_irqrestore(&bank
->lock
, flags
);
801 static const struct dev_pm_ops omap_mpuio_dev_pm_ops
= {
802 .suspend_noirq
= omap_mpuio_suspend_noirq
,
803 .resume_noirq
= omap_mpuio_resume_noirq
,
806 /* use platform_driver for this. */
807 static struct platform_driver omap_mpuio_driver
= {
810 .pm
= &omap_mpuio_dev_pm_ops
,
814 static struct platform_device omap_mpuio_device
= {
818 .driver
= &omap_mpuio_driver
.driver
,
820 /* could list the /proc/iomem resources */
823 static inline void mpuio_init(struct gpio_bank
*bank
)
825 platform_set_drvdata(&omap_mpuio_device
, bank
);
827 if (platform_driver_register(&omap_mpuio_driver
) == 0)
828 (void) platform_device_register(&omap_mpuio_device
);
831 /*---------------------------------------------------------------------*/
833 static int gpio_input(struct gpio_chip
*chip
, unsigned offset
)
835 struct gpio_bank
*bank
;
838 bank
= container_of(chip
, struct gpio_bank
, chip
);
839 spin_lock_irqsave(&bank
->lock
, flags
);
840 _set_gpio_direction(bank
, offset
, 1);
841 spin_unlock_irqrestore(&bank
->lock
, flags
);
845 static int gpio_is_input(struct gpio_bank
*bank
, int mask
)
847 void __iomem
*reg
= bank
->base
+ bank
->regs
->direction
;
849 return __raw_readl(reg
) & mask
;
852 static int gpio_get(struct gpio_chip
*chip
, unsigned offset
)
854 struct gpio_bank
*bank
;
859 gpio
= chip
->base
+ offset
;
860 bank
= container_of(chip
, struct gpio_bank
, chip
);
862 mask
= GPIO_BIT(bank
, gpio
);
864 if (gpio_is_input(bank
, mask
))
865 return _get_gpio_datain(bank
, gpio
);
867 return _get_gpio_dataout(bank
, gpio
);
870 static int gpio_output(struct gpio_chip
*chip
, unsigned offset
, int value
)
872 struct gpio_bank
*bank
;
875 bank
= container_of(chip
, struct gpio_bank
, chip
);
876 spin_lock_irqsave(&bank
->lock
, flags
);
877 bank
->set_dataout(bank
, offset
, value
);
878 _set_gpio_direction(bank
, offset
, 0);
879 spin_unlock_irqrestore(&bank
->lock
, flags
);
883 static int gpio_debounce(struct gpio_chip
*chip
, unsigned offset
,
886 struct gpio_bank
*bank
;
889 bank
= container_of(chip
, struct gpio_bank
, chip
);
892 bank
->dbck
= clk_get(bank
->dev
, "dbclk");
893 if (IS_ERR(bank
->dbck
))
894 dev_err(bank
->dev
, "Could not get gpio dbck\n");
897 spin_lock_irqsave(&bank
->lock
, flags
);
898 _set_gpio_debounce(bank
, offset
, debounce
);
899 spin_unlock_irqrestore(&bank
->lock
, flags
);
904 static void gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
906 struct gpio_bank
*bank
;
909 bank
= container_of(chip
, struct gpio_bank
, chip
);
910 spin_lock_irqsave(&bank
->lock
, flags
);
911 bank
->set_dataout(bank
, offset
, value
);
912 spin_unlock_irqrestore(&bank
->lock
, flags
);
915 static int gpio_2irq(struct gpio_chip
*chip
, unsigned offset
)
917 struct gpio_bank
*bank
;
919 bank
= container_of(chip
, struct gpio_bank
, chip
);
920 return bank
->virtual_irq_start
+ offset
;
923 /*---------------------------------------------------------------------*/
925 static void __init
omap_gpio_show_rev(struct gpio_bank
*bank
)
930 if (called
|| bank
->regs
->revision
== USHRT_MAX
)
933 rev
= __raw_readw(bank
->base
+ bank
->regs
->revision
);
934 pr_info("OMAP GPIO hardware version %d.%d\n",
935 (rev
>> 4) & 0x0f, rev
& 0x0f);
940 /* This lock class tells lockdep that GPIO irqs are in a different
941 * category than their parents, so it won't report false recursion.
943 static struct lock_class_key gpio_lock_class
;
945 static void omap_gpio_mod_init(struct gpio_bank
*bank
)
947 void __iomem
*base
= bank
->base
;
950 if (bank
->width
== 16)
953 if (bank
->is_mpuio
) {
954 __raw_writel(l
, bank
->base
+ bank
->regs
->irqenable
);
958 _gpio_rmw(base
, bank
->regs
->irqenable
, l
, bank
->regs
->irqenable_inv
);
959 _gpio_rmw(base
, bank
->regs
->irqstatus
, l
,
960 bank
->regs
->irqenable_inv
== false);
961 _gpio_rmw(base
, bank
->regs
->irqenable
, l
, bank
->regs
->debounce_en
!= 0);
962 _gpio_rmw(base
, bank
->regs
->irqenable
, l
, bank
->regs
->ctrl
!= 0);
963 if (bank
->regs
->debounce_en
)
964 _gpio_rmw(base
, bank
->regs
->debounce_en
, 0, 1);
966 /* Save OE default value (0xffffffff) in the context */
967 bank
->context
.oe
= __raw_readl(bank
->base
+ bank
->regs
->direction
);
968 /* Initialize interface clk ungated, module enabled */
969 if (bank
->regs
->ctrl
)
970 _gpio_rmw(base
, bank
->regs
->ctrl
, 0, 1);
974 omap_mpuio_alloc_gc(struct gpio_bank
*bank
, unsigned int irq_start
,
977 struct irq_chip_generic
*gc
;
978 struct irq_chip_type
*ct
;
980 gc
= irq_alloc_generic_chip("MPUIO", 1, irq_start
, bank
->base
,
983 dev_err(bank
->dev
, "Memory alloc failed for gc\n");
989 /* NOTE: No ack required, reading IRQ status clears it. */
990 ct
->chip
.irq_mask
= irq_gc_mask_set_bit
;
991 ct
->chip
.irq_unmask
= irq_gc_mask_clr_bit
;
992 ct
->chip
.irq_set_type
= gpio_irq_type
;
994 if (bank
->regs
->wkup_en
)
995 ct
->chip
.irq_set_wake
= gpio_wake_enable
,
997 ct
->regs
.mask
= OMAP_MPUIO_GPIO_INT
/ bank
->stride
;
998 irq_setup_generic_chip(gc
, IRQ_MSK(num
), IRQ_GC_INIT_MASK_CACHE
,
999 IRQ_NOREQUEST
| IRQ_NOPROBE
, 0);
1002 static void __devinit
omap_gpio_chip_init(struct gpio_bank
*bank
)
1008 * REVISIT eventually switch from OMAP-specific gpio structs
1009 * over to the generic ones
1011 bank
->chip
.request
= omap_gpio_request
;
1012 bank
->chip
.free
= omap_gpio_free
;
1013 bank
->chip
.direction_input
= gpio_input
;
1014 bank
->chip
.get
= gpio_get
;
1015 bank
->chip
.direction_output
= gpio_output
;
1016 bank
->chip
.set_debounce
= gpio_debounce
;
1017 bank
->chip
.set
= gpio_set
;
1018 bank
->chip
.to_irq
= gpio_2irq
;
1019 if (bank
->is_mpuio
) {
1020 bank
->chip
.label
= "mpuio";
1021 if (bank
->regs
->wkup_en
)
1022 bank
->chip
.dev
= &omap_mpuio_device
.dev
;
1023 bank
->chip
.base
= OMAP_MPUIO(0);
1025 bank
->chip
.label
= "gpio";
1026 bank
->chip
.base
= gpio
;
1027 gpio
+= bank
->width
;
1029 bank
->chip
.ngpio
= bank
->width
;
1031 gpiochip_add(&bank
->chip
);
1033 for (j
= bank
->virtual_irq_start
;
1034 j
< bank
->virtual_irq_start
+ bank
->width
; j
++) {
1035 irq_set_lockdep_class(j
, &gpio_lock_class
);
1036 irq_set_chip_data(j
, bank
);
1037 if (bank
->is_mpuio
) {
1038 omap_mpuio_alloc_gc(bank
, j
, bank
->width
);
1040 irq_set_chip(j
, &gpio_irq_chip
);
1041 irq_set_handler(j
, handle_simple_irq
);
1042 set_irq_flags(j
, IRQF_VALID
);
1045 irq_set_chained_handler(bank
->irq
, gpio_irq_handler
);
1046 irq_set_handler_data(bank
->irq
, bank
);
1049 static int __devinit
omap_gpio_probe(struct platform_device
*pdev
)
1051 struct omap_gpio_platform_data
*pdata
;
1052 struct resource
*res
;
1053 struct gpio_bank
*bank
;
1056 if (!pdev
->dev
.platform_data
) {
1061 bank
= kzalloc(sizeof(struct gpio_bank
), GFP_KERNEL
);
1063 dev_err(&pdev
->dev
, "Memory alloc failed for gpio_bank\n");
1068 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1069 if (unlikely(!res
)) {
1070 dev_err(&pdev
->dev
, "GPIO Bank %i Invalid IRQ resource\n",
1076 bank
->irq
= res
->start
;
1077 bank
->id
= pdev
->id
;
1079 pdata
= pdev
->dev
.platform_data
;
1080 bank
->virtual_irq_start
= pdata
->virtual_irq_start
;
1081 bank
->dev
= &pdev
->dev
;
1082 bank
->dbck_flag
= pdata
->dbck_flag
;
1083 bank
->stride
= pdata
->bank_stride
;
1084 bank
->width
= pdata
->bank_width
;
1085 bank
->is_mpuio
= pdata
->is_mpuio
;
1086 bank
->non_wakeup_gpios
= pdata
->non_wakeup_gpios
;
1087 bank
->loses_context
= pdata
->loses_context
;
1088 bank
->get_context_loss_count
= pdata
->get_context_loss_count
;
1089 bank
->regs
= pdata
->regs
;
1091 if (bank
->regs
->set_dataout
&& bank
->regs
->clr_dataout
)
1092 bank
->set_dataout
= _set_gpio_dataout_reg
;
1094 bank
->set_dataout
= _set_gpio_dataout_mask
;
1096 spin_lock_init(&bank
->lock
);
1098 /* Static mapping, never released */
1099 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1100 if (unlikely(!res
)) {
1101 dev_err(&pdev
->dev
, "GPIO Bank %i Invalid mem resource\n",
1107 bank
->base
= ioremap(res
->start
, resource_size(res
));
1109 dev_err(&pdev
->dev
, "Could not ioremap gpio bank%i\n",
1115 platform_set_drvdata(pdev
, bank
);
1117 pm_runtime_enable(bank
->dev
);
1118 pm_runtime_irq_safe(bank
->dev
);
1119 pm_runtime_get_sync(bank
->dev
);
1124 omap_gpio_mod_init(bank
);
1125 omap_gpio_chip_init(bank
);
1126 omap_gpio_show_rev(bank
);
1128 pm_runtime_put(bank
->dev
);
1130 list_add_tail(&bank
->node
, &omap_gpio_list
);
1140 #ifdef CONFIG_ARCH_OMAP2PLUS
1142 #if defined(CONFIG_PM_SLEEP)
1143 static int omap_gpio_suspend(struct device
*dev
)
1145 struct platform_device
*pdev
= to_platform_device(dev
);
1146 struct gpio_bank
*bank
= platform_get_drvdata(pdev
);
1147 void __iomem
*base
= bank
->base
;
1148 void __iomem
*wakeup_enable
;
1149 unsigned long flags
;
1151 if (!bank
->mod_usage
|| !bank
->loses_context
)
1154 if (!bank
->regs
->wkup_en
|| !bank
->suspend_wakeup
)
1157 wakeup_enable
= bank
->base
+ bank
->regs
->wkup_en
;
1159 spin_lock_irqsave(&bank
->lock
, flags
);
1160 bank
->saved_wakeup
= __raw_readl(wakeup_enable
);
1161 _gpio_rmw(base
, bank
->regs
->wkup_en
, 0xffffffff, 0);
1162 _gpio_rmw(base
, bank
->regs
->wkup_en
, bank
->suspend_wakeup
, 1);
1163 spin_unlock_irqrestore(&bank
->lock
, flags
);
1168 static int omap_gpio_resume(struct device
*dev
)
1170 struct platform_device
*pdev
= to_platform_device(dev
);
1171 struct gpio_bank
*bank
= platform_get_drvdata(pdev
);
1172 void __iomem
*base
= bank
->base
;
1173 unsigned long flags
;
1175 if (!bank
->mod_usage
|| !bank
->loses_context
)
1178 if (!bank
->regs
->wkup_en
|| !bank
->saved_wakeup
)
1181 spin_lock_irqsave(&bank
->lock
, flags
);
1182 _gpio_rmw(base
, bank
->regs
->wkup_en
, 0xffffffff, 0);
1183 _gpio_rmw(base
, bank
->regs
->wkup_en
, bank
->saved_wakeup
, 1);
1184 spin_unlock_irqrestore(&bank
->lock
, flags
);
1188 #endif /* CONFIG_PM_SLEEP */
1190 #if defined(CONFIG_PM_RUNTIME)
1191 static void omap_gpio_restore_context(struct gpio_bank
*bank
);
1193 static int omap_gpio_runtime_suspend(struct device
*dev
)
1195 struct platform_device
*pdev
= to_platform_device(dev
);
1196 struct gpio_bank
*bank
= platform_get_drvdata(pdev
);
1198 unsigned long flags
;
1200 spin_lock_irqsave(&bank
->lock
, flags
);
1201 if (bank
->power_mode
!= OFF_MODE
) {
1202 bank
->power_mode
= 0;
1203 goto update_gpio_context_count
;
1206 * If going to OFF, remove triggering for all
1207 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1208 * generated. See OMAP2420 Errata item 1.101.
1210 if (!(bank
->enabled_non_wakeup_gpios
))
1211 goto update_gpio_context_count
;
1213 bank
->saved_datain
= __raw_readl(bank
->base
+
1214 bank
->regs
->datain
);
1215 l1
= __raw_readl(bank
->base
+ bank
->regs
->fallingdetect
);
1216 l2
= __raw_readl(bank
->base
+ bank
->regs
->risingdetect
);
1218 bank
->saved_fallingdetect
= l1
;
1219 bank
->saved_risingdetect
= l2
;
1220 l1
&= ~bank
->enabled_non_wakeup_gpios
;
1221 l2
&= ~bank
->enabled_non_wakeup_gpios
;
1223 __raw_writel(l1
, bank
->base
+ bank
->regs
->fallingdetect
);
1224 __raw_writel(l2
, bank
->base
+ bank
->regs
->risingdetect
);
1226 bank
->workaround_enabled
= true;
1228 update_gpio_context_count
:
1229 if (bank
->get_context_loss_count
)
1230 bank
->context_loss_count
=
1231 bank
->get_context_loss_count(bank
->dev
);
1233 _gpio_dbck_disable(bank
);
1234 spin_unlock_irqrestore(&bank
->lock
, flags
);
1239 static int omap_gpio_runtime_resume(struct device
*dev
)
1241 struct platform_device
*pdev
= to_platform_device(dev
);
1242 struct gpio_bank
*bank
= platform_get_drvdata(pdev
);
1243 int context_lost_cnt_after
;
1244 u32 l
= 0, gen
, gen0
, gen1
;
1245 unsigned long flags
;
1247 spin_lock_irqsave(&bank
->lock
, flags
);
1248 _gpio_dbck_enable(bank
);
1249 if (!bank
->enabled_non_wakeup_gpios
|| !bank
->workaround_enabled
) {
1250 spin_unlock_irqrestore(&bank
->lock
, flags
);
1254 if (bank
->get_context_loss_count
) {
1255 context_lost_cnt_after
=
1256 bank
->get_context_loss_count(bank
->dev
);
1257 if (context_lost_cnt_after
!= bank
->context_loss_count
||
1258 !context_lost_cnt_after
) {
1259 omap_gpio_restore_context(bank
);
1261 spin_unlock_irqrestore(&bank
->lock
, flags
);
1266 __raw_writel(bank
->saved_fallingdetect
,
1267 bank
->base
+ bank
->regs
->fallingdetect
);
1268 __raw_writel(bank
->saved_risingdetect
,
1269 bank
->base
+ bank
->regs
->risingdetect
);
1270 l
= __raw_readl(bank
->base
+ bank
->regs
->datain
);
1273 * Check if any of the non-wakeup interrupt GPIOs have changed
1274 * state. If so, generate an IRQ by software. This is
1275 * horribly racy, but it's the best we can do to work around
1278 l
^= bank
->saved_datain
;
1279 l
&= bank
->enabled_non_wakeup_gpios
;
1282 * No need to generate IRQs for the rising edge for gpio IRQs
1283 * configured with falling edge only; and vice versa.
1285 gen0
= l
& bank
->saved_fallingdetect
;
1286 gen0
&= bank
->saved_datain
;
1288 gen1
= l
& bank
->saved_risingdetect
;
1289 gen1
&= ~(bank
->saved_datain
);
1291 /* FIXME: Consider GPIO IRQs with level detections properly! */
1292 gen
= l
& (~(bank
->saved_fallingdetect
) & ~(bank
->saved_risingdetect
));
1293 /* Consider all GPIO IRQs needed to be updated */
1299 old0
= __raw_readl(bank
->base
+ bank
->regs
->leveldetect0
);
1300 old1
= __raw_readl(bank
->base
+ bank
->regs
->leveldetect1
);
1302 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1303 __raw_writel(old0
| gen
, bank
->base
+
1304 bank
->regs
->leveldetect0
);
1305 __raw_writel(old1
| gen
, bank
->base
+
1306 bank
->regs
->leveldetect1
);
1309 if (cpu_is_omap44xx()) {
1310 __raw_writel(old0
| l
, bank
->base
+
1311 bank
->regs
->leveldetect0
);
1312 __raw_writel(old1
| l
, bank
->base
+
1313 bank
->regs
->leveldetect1
);
1315 __raw_writel(old0
, bank
->base
+ bank
->regs
->leveldetect0
);
1316 __raw_writel(old1
, bank
->base
+ bank
->regs
->leveldetect1
);
1319 bank
->workaround_enabled
= false;
1320 spin_unlock_irqrestore(&bank
->lock
, flags
);
1324 #endif /* CONFIG_PM_RUNTIME */
1326 void omap2_gpio_prepare_for_idle(int pwr_mode
)
1328 struct gpio_bank
*bank
;
1330 list_for_each_entry(bank
, &omap_gpio_list
, node
) {
1331 if (!bank
->mod_usage
|| !bank
->loses_context
)
1334 bank
->power_mode
= pwr_mode
;
1336 pm_runtime_put_sync_suspend(bank
->dev
);
1340 void omap2_gpio_resume_after_idle(void)
1342 struct gpio_bank
*bank
;
1344 list_for_each_entry(bank
, &omap_gpio_list
, node
) {
1345 if (!bank
->mod_usage
|| !bank
->loses_context
)
1348 pm_runtime_get_sync(bank
->dev
);
1352 #if defined(CONFIG_PM_RUNTIME)
1353 static void omap_gpio_restore_context(struct gpio_bank
*bank
)
1355 __raw_writel(bank
->context
.wake_en
,
1356 bank
->base
+ bank
->regs
->wkup_en
);
1357 __raw_writel(bank
->context
.ctrl
, bank
->base
+ bank
->regs
->ctrl
);
1358 __raw_writel(bank
->context
.leveldetect0
,
1359 bank
->base
+ bank
->regs
->leveldetect0
);
1360 __raw_writel(bank
->context
.leveldetect1
,
1361 bank
->base
+ bank
->regs
->leveldetect1
);
1362 __raw_writel(bank
->context
.risingdetect
,
1363 bank
->base
+ bank
->regs
->risingdetect
);
1364 __raw_writel(bank
->context
.fallingdetect
,
1365 bank
->base
+ bank
->regs
->fallingdetect
);
1366 if (bank
->regs
->set_dataout
&& bank
->regs
->clr_dataout
)
1367 __raw_writel(bank
->context
.dataout
,
1368 bank
->base
+ bank
->regs
->set_dataout
);
1370 __raw_writel(bank
->context
.dataout
,
1371 bank
->base
+ bank
->regs
->dataout
);
1372 __raw_writel(bank
->context
.oe
, bank
->base
+ bank
->regs
->direction
);
1374 if (bank
->dbck_enable_mask
) {
1375 __raw_writel(bank
->context
.debounce
, bank
->base
+
1376 bank
->regs
->debounce
);
1377 __raw_writel(bank
->context
.debounce_en
,
1378 bank
->base
+ bank
->regs
->debounce_en
);
1381 __raw_writel(bank
->context
.irqenable1
,
1382 bank
->base
+ bank
->regs
->irqenable
);
1383 __raw_writel(bank
->context
.irqenable2
,
1384 bank
->base
+ bank
->regs
->irqenable2
);
1386 #endif /* CONFIG_PM_RUNTIME */
1388 #define omap_gpio_suspend NULL
1389 #define omap_gpio_resume NULL
1390 #define omap_gpio_runtime_suspend NULL
1391 #define omap_gpio_runtime_resume NULL
1394 static const struct dev_pm_ops gpio_pm_ops
= {
1395 SET_SYSTEM_SLEEP_PM_OPS(omap_gpio_suspend
, omap_gpio_resume
)
1396 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend
, omap_gpio_runtime_resume
,
1400 static struct platform_driver omap_gpio_driver
= {
1401 .probe
= omap_gpio_probe
,
1403 .name
= "omap_gpio",
1409 * gpio driver register needs to be done before
1410 * machine_init functions access gpio APIs.
1411 * Hence omap_gpio_drv_reg() is a postcore_initcall.
1413 static int __init
omap_gpio_drv_reg(void)
1415 return platform_driver_register(&omap_gpio_driver
);
1417 postcore_initcall(omap_gpio_drv_reg
);