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/cpu_pm.h>
23 #include <linux/device.h>
24 #include <linux/pm_runtime.h>
27 #include <linux/of_device.h>
28 #include <linux/gpio/driver.h>
29 #include <linux/bitops.h>
30 #include <linux/platform_data/gpio-omap.h>
32 #define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF
50 struct list_head node
;
54 u32 enabled_non_wakeup_gpios
;
55 struct gpio_regs context
;
60 raw_spinlock_t wa_lock
;
61 struct gpio_chip chip
;
63 struct notifier_block nb
;
64 unsigned int is_suspended
:1;
75 int context_loss_count
;
77 void (*set_dataout
)(struct gpio_bank
*bank
, unsigned gpio
, int enable
);
78 void (*set_dataout_multiple
)(struct gpio_bank
*bank
,
79 unsigned long *mask
, unsigned long *bits
);
80 int (*get_context_loss_count
)(struct device
*dev
);
82 struct omap_gpio_reg_offs
*regs
;
85 #define GPIO_MOD_CTRL_BIT BIT(0)
87 #define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
88 #define LINE_USED(line, offset) (line & (BIT(offset)))
90 static void omap_gpio_unmask_irq(struct irq_data
*d
);
92 static inline struct gpio_bank
*omap_irq_data_get_bank(struct irq_data
*d
)
94 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(d
);
95 return gpiochip_get_data(chip
);
98 static void omap_set_gpio_direction(struct gpio_bank
*bank
, int gpio
,
101 void __iomem
*reg
= bank
->base
;
104 reg
+= bank
->regs
->direction
;
105 l
= readl_relaxed(reg
);
110 writel_relaxed(l
, reg
);
111 bank
->context
.oe
= l
;
115 /* set data out value using dedicate set/clear register */
116 static void omap_set_gpio_dataout_reg(struct gpio_bank
*bank
, unsigned offset
,
119 void __iomem
*reg
= bank
->base
;
123 reg
+= bank
->regs
->set_dataout
;
124 bank
->context
.dataout
|= l
;
126 reg
+= bank
->regs
->clr_dataout
;
127 bank
->context
.dataout
&= ~l
;
130 writel_relaxed(l
, reg
);
133 /* set data out value using mask register */
134 static void omap_set_gpio_dataout_mask(struct gpio_bank
*bank
, unsigned offset
,
137 void __iomem
*reg
= bank
->base
+ bank
->regs
->dataout
;
138 u32 gpio_bit
= BIT(offset
);
141 l
= readl_relaxed(reg
);
146 writel_relaxed(l
, reg
);
147 bank
->context
.dataout
= l
;
150 static int omap_get_gpio_datain(struct gpio_bank
*bank
, int offset
)
152 void __iomem
*reg
= bank
->base
+ bank
->regs
->datain
;
154 return (readl_relaxed(reg
) & (BIT(offset
))) != 0;
157 static int omap_get_gpio_dataout(struct gpio_bank
*bank
, int offset
)
159 void __iomem
*reg
= bank
->base
+ bank
->regs
->dataout
;
161 return (readl_relaxed(reg
) & (BIT(offset
))) != 0;
164 /* set multiple data out values using dedicate set/clear register */
165 static void omap_set_gpio_dataout_reg_multiple(struct gpio_bank
*bank
,
169 void __iomem
*reg
= bank
->base
;
173 writel_relaxed(l
, reg
+ bank
->regs
->set_dataout
);
174 bank
->context
.dataout
|= l
;
177 writel_relaxed(l
, reg
+ bank
->regs
->clr_dataout
);
178 bank
->context
.dataout
&= ~l
;
181 /* set multiple data out values using mask register */
182 static void omap_set_gpio_dataout_mask_multiple(struct gpio_bank
*bank
,
186 void __iomem
*reg
= bank
->base
+ bank
->regs
->dataout
;
187 u32 l
= (readl_relaxed(reg
) & ~*mask
) | (*bits
& *mask
);
189 writel_relaxed(l
, reg
);
190 bank
->context
.dataout
= l
;
193 static unsigned long omap_get_gpio_datain_multiple(struct gpio_bank
*bank
,
196 void __iomem
*reg
= bank
->base
+ bank
->regs
->datain
;
198 return readl_relaxed(reg
) & *mask
;
201 static unsigned long omap_get_gpio_dataout_multiple(struct gpio_bank
*bank
,
204 void __iomem
*reg
= bank
->base
+ bank
->regs
->dataout
;
206 return readl_relaxed(reg
) & *mask
;
209 static inline void omap_gpio_rmw(void __iomem
*base
, u32 reg
, u32 mask
, bool set
)
211 int l
= readl_relaxed(base
+ reg
);
218 writel_relaxed(l
, base
+ reg
);
221 static inline void omap_gpio_dbck_enable(struct gpio_bank
*bank
)
223 if (bank
->dbck_enable_mask
&& !bank
->dbck_enabled
) {
224 clk_enable(bank
->dbck
);
225 bank
->dbck_enabled
= true;
227 writel_relaxed(bank
->dbck_enable_mask
,
228 bank
->base
+ bank
->regs
->debounce_en
);
232 static inline void omap_gpio_dbck_disable(struct gpio_bank
*bank
)
234 if (bank
->dbck_enable_mask
&& bank
->dbck_enabled
) {
236 * Disable debounce before cutting it's clock. If debounce is
237 * enabled but the clock is not, GPIO module seems to be unable
238 * to detect events and generate interrupts at least on OMAP3.
240 writel_relaxed(0, bank
->base
+ bank
->regs
->debounce_en
);
242 clk_disable(bank
->dbck
);
243 bank
->dbck_enabled
= false;
248 * omap2_set_gpio_debounce - low level gpio debounce time
249 * @bank: the gpio bank we're acting upon
250 * @offset: the gpio number on this @bank
251 * @debounce: debounce time to use
253 * OMAP's debounce time is in 31us steps
254 * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
255 * so we need to convert and round up to the closest unit.
257 * Return: 0 on success, negative error otherwise.
259 static int omap2_set_gpio_debounce(struct gpio_bank
*bank
, unsigned offset
,
265 bool enable
= !!debounce
;
267 if (!bank
->dbck_flag
)
271 debounce
= DIV_ROUND_UP(debounce
, 31) - 1;
272 if ((debounce
& OMAP4_GPIO_DEBOUNCINGTIME_MASK
) != debounce
)
278 clk_enable(bank
->dbck
);
279 reg
= bank
->base
+ bank
->regs
->debounce
;
280 writel_relaxed(debounce
, reg
);
282 reg
= bank
->base
+ bank
->regs
->debounce_en
;
283 val
= readl_relaxed(reg
);
289 bank
->dbck_enable_mask
= val
;
291 writel_relaxed(val
, reg
);
292 clk_disable(bank
->dbck
);
294 * Enable debounce clock per module.
295 * This call is mandatory because in omap_gpio_request() when
296 * *_runtime_get_sync() is called, _gpio_dbck_enable() within
297 * runtime callbck fails to turn on dbck because dbck_enable_mask
298 * used within _gpio_dbck_enable() is still not initialized at
299 * that point. Therefore we have to enable dbck here.
301 omap_gpio_dbck_enable(bank
);
302 if (bank
->dbck_enable_mask
) {
303 bank
->context
.debounce
= debounce
;
304 bank
->context
.debounce_en
= val
;
311 * omap_clear_gpio_debounce - clear debounce settings for a gpio
312 * @bank: the gpio bank we're acting upon
313 * @offset: the gpio number on this @bank
315 * If a gpio is using debounce, then clear the debounce enable bit and if
316 * this is the only gpio in this bank using debounce, then clear the debounce
317 * time too. The debounce clock will also be disabled when calling this function
318 * if this is the only gpio in the bank using debounce.
320 static void omap_clear_gpio_debounce(struct gpio_bank
*bank
, unsigned offset
)
322 u32 gpio_bit
= BIT(offset
);
324 if (!bank
->dbck_flag
)
327 if (!(bank
->dbck_enable_mask
& gpio_bit
))
330 bank
->dbck_enable_mask
&= ~gpio_bit
;
331 bank
->context
.debounce_en
&= ~gpio_bit
;
332 writel_relaxed(bank
->context
.debounce_en
,
333 bank
->base
+ bank
->regs
->debounce_en
);
335 if (!bank
->dbck_enable_mask
) {
336 bank
->context
.debounce
= 0;
337 writel_relaxed(bank
->context
.debounce
, bank
->base
+
338 bank
->regs
->debounce
);
339 clk_disable(bank
->dbck
);
340 bank
->dbck_enabled
= false;
345 * Off mode wake-up capable GPIOs in bank(s) that are in the wakeup domain.
346 * See TRM section for GPIO for "Wake-Up Generation" for the list of GPIOs
347 * in wakeup domain. If bank->non_wakeup_gpios is not configured, assume none
348 * are capable waking up the system from off mode.
350 static bool omap_gpio_is_off_wakeup_capable(struct gpio_bank
*bank
, u32 gpio_mask
)
352 u32 no_wake
= bank
->non_wakeup_gpios
;
355 return !!(~no_wake
& gpio_mask
);
360 static inline void omap_set_gpio_trigger(struct gpio_bank
*bank
, int gpio
,
363 void __iomem
*base
= bank
->base
;
364 u32 gpio_bit
= BIT(gpio
);
366 omap_gpio_rmw(base
, bank
->regs
->leveldetect0
, gpio_bit
,
367 trigger
& IRQ_TYPE_LEVEL_LOW
);
368 omap_gpio_rmw(base
, bank
->regs
->leveldetect1
, gpio_bit
,
369 trigger
& IRQ_TYPE_LEVEL_HIGH
);
372 * We need the edge detection enabled for to allow the GPIO block
373 * to be woken from idle state. Set the appropriate edge detection
374 * in addition to the level detection.
376 omap_gpio_rmw(base
, bank
->regs
->risingdetect
, gpio_bit
,
377 trigger
& (IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_LEVEL_HIGH
));
378 omap_gpio_rmw(base
, bank
->regs
->fallingdetect
, gpio_bit
,
379 trigger
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_LEVEL_LOW
));
381 bank
->context
.leveldetect0
=
382 readl_relaxed(bank
->base
+ bank
->regs
->leveldetect0
);
383 bank
->context
.leveldetect1
=
384 readl_relaxed(bank
->base
+ bank
->regs
->leveldetect1
);
385 bank
->context
.risingdetect
=
386 readl_relaxed(bank
->base
+ bank
->regs
->risingdetect
);
387 bank
->context
.fallingdetect
=
388 readl_relaxed(bank
->base
+ bank
->regs
->fallingdetect
);
390 if (likely(!(bank
->non_wakeup_gpios
& gpio_bit
))) {
391 omap_gpio_rmw(base
, bank
->regs
->wkup_en
, gpio_bit
, trigger
!= 0);
392 bank
->context
.wake_en
=
393 readl_relaxed(bank
->base
+ bank
->regs
->wkup_en
);
396 /* This part needs to be executed always for OMAP{34xx, 44xx} */
397 if (!bank
->regs
->irqctrl
&& !omap_gpio_is_off_wakeup_capable(bank
, gpio
)) {
399 * Log the edge gpio and manually trigger the IRQ
400 * after resume if the input level changes
401 * to avoid irq lost during PER RET/OFF mode
402 * Applies for omap2 non-wakeup gpio and all omap3 gpios
404 if (trigger
& IRQ_TYPE_EDGE_BOTH
)
405 bank
->enabled_non_wakeup_gpios
|= gpio_bit
;
407 bank
->enabled_non_wakeup_gpios
&= ~gpio_bit
;
411 readl_relaxed(bank
->base
+ bank
->regs
->leveldetect0
) |
412 readl_relaxed(bank
->base
+ bank
->regs
->leveldetect1
);
415 #ifdef CONFIG_ARCH_OMAP1
417 * This only applies to chips that can't do both rising and falling edge
418 * detection at once. For all other chips, this function is a noop.
420 static void omap_toggle_gpio_edge_triggering(struct gpio_bank
*bank
, int gpio
)
422 void __iomem
*reg
= bank
->base
;
425 if (!bank
->regs
->irqctrl
)
428 reg
+= bank
->regs
->irqctrl
;
430 l
= readl_relaxed(reg
);
436 writel_relaxed(l
, reg
);
439 static void omap_toggle_gpio_edge_triggering(struct gpio_bank
*bank
, int gpio
) {}
442 static int omap_set_gpio_triggering(struct gpio_bank
*bank
, int gpio
,
445 void __iomem
*reg
= bank
->base
;
446 void __iomem
*base
= bank
->base
;
449 if (bank
->regs
->leveldetect0
&& bank
->regs
->wkup_en
) {
450 omap_set_gpio_trigger(bank
, gpio
, trigger
);
451 } else if (bank
->regs
->irqctrl
) {
452 reg
+= bank
->regs
->irqctrl
;
454 l
= readl_relaxed(reg
);
455 if ((trigger
& IRQ_TYPE_SENSE_MASK
) == IRQ_TYPE_EDGE_BOTH
)
456 bank
->toggle_mask
|= BIT(gpio
);
457 if (trigger
& IRQ_TYPE_EDGE_RISING
)
459 else if (trigger
& IRQ_TYPE_EDGE_FALLING
)
464 writel_relaxed(l
, reg
);
465 } else if (bank
->regs
->edgectrl1
) {
467 reg
+= bank
->regs
->edgectrl2
;
469 reg
+= bank
->regs
->edgectrl1
;
472 l
= readl_relaxed(reg
);
473 l
&= ~(3 << (gpio
<< 1));
474 if (trigger
& IRQ_TYPE_EDGE_RISING
)
475 l
|= 2 << (gpio
<< 1);
476 if (trigger
& IRQ_TYPE_EDGE_FALLING
)
479 /* Enable wake-up during idle for dynamic tick */
480 omap_gpio_rmw(base
, bank
->regs
->wkup_en
, BIT(gpio
), trigger
);
481 bank
->context
.wake_en
=
482 readl_relaxed(bank
->base
+ bank
->regs
->wkup_en
);
483 writel_relaxed(l
, reg
);
488 static void omap_enable_gpio_module(struct gpio_bank
*bank
, unsigned offset
)
490 if (bank
->regs
->pinctrl
) {
491 void __iomem
*reg
= bank
->base
+ bank
->regs
->pinctrl
;
493 /* Claim the pin for MPU */
494 writel_relaxed(readl_relaxed(reg
) | (BIT(offset
)), reg
);
497 if (bank
->regs
->ctrl
&& !BANK_USED(bank
)) {
498 void __iomem
*reg
= bank
->base
+ bank
->regs
->ctrl
;
501 ctrl
= readl_relaxed(reg
);
502 /* Module is enabled, clocks are not gated */
503 ctrl
&= ~GPIO_MOD_CTRL_BIT
;
504 writel_relaxed(ctrl
, reg
);
505 bank
->context
.ctrl
= ctrl
;
509 static void omap_disable_gpio_module(struct gpio_bank
*bank
, unsigned offset
)
511 void __iomem
*base
= bank
->base
;
513 if (bank
->regs
->wkup_en
&&
514 !LINE_USED(bank
->mod_usage
, offset
) &&
515 !LINE_USED(bank
->irq_usage
, offset
)) {
516 /* Disable wake-up during idle for dynamic tick */
517 omap_gpio_rmw(base
, bank
->regs
->wkup_en
, BIT(offset
), 0);
518 bank
->context
.wake_en
=
519 readl_relaxed(bank
->base
+ bank
->regs
->wkup_en
);
522 if (bank
->regs
->ctrl
&& !BANK_USED(bank
)) {
523 void __iomem
*reg
= bank
->base
+ bank
->regs
->ctrl
;
526 ctrl
= readl_relaxed(reg
);
527 /* Module is disabled, clocks are gated */
528 ctrl
|= GPIO_MOD_CTRL_BIT
;
529 writel_relaxed(ctrl
, reg
);
530 bank
->context
.ctrl
= ctrl
;
534 static int omap_gpio_is_input(struct gpio_bank
*bank
, unsigned offset
)
536 void __iomem
*reg
= bank
->base
+ bank
->regs
->direction
;
538 return readl_relaxed(reg
) & BIT(offset
);
541 static void omap_gpio_init_irq(struct gpio_bank
*bank
, unsigned offset
)
543 if (!LINE_USED(bank
->mod_usage
, offset
)) {
544 omap_enable_gpio_module(bank
, offset
);
545 omap_set_gpio_direction(bank
, offset
, 1);
547 bank
->irq_usage
|= BIT(offset
);
550 static int omap_gpio_irq_type(struct irq_data
*d
, unsigned type
)
552 struct gpio_bank
*bank
= omap_irq_data_get_bank(d
);
555 unsigned offset
= d
->hwirq
;
557 if (type
& ~IRQ_TYPE_SENSE_MASK
)
560 if (!bank
->regs
->leveldetect0
&&
561 (type
& (IRQ_TYPE_LEVEL_LOW
|IRQ_TYPE_LEVEL_HIGH
)))
564 raw_spin_lock_irqsave(&bank
->lock
, flags
);
565 retval
= omap_set_gpio_triggering(bank
, offset
, type
);
567 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
570 omap_gpio_init_irq(bank
, offset
);
571 if (!omap_gpio_is_input(bank
, offset
)) {
572 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
576 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
578 if (type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
579 irq_set_handler_locked(d
, handle_level_irq
);
580 else if (type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
582 * Edge IRQs are already cleared/acked in irq_handler and
583 * not need to be masked, as result handle_edge_irq()
584 * logic is excessed here and may cause lose of interrupts.
585 * So just use handle_simple_irq.
587 irq_set_handler_locked(d
, handle_simple_irq
);
595 static void omap_clear_gpio_irqbank(struct gpio_bank
*bank
, int gpio_mask
)
597 void __iomem
*reg
= bank
->base
;
599 reg
+= bank
->regs
->irqstatus
;
600 writel_relaxed(gpio_mask
, reg
);
602 /* Workaround for clearing DSP GPIO interrupts to allow retention */
603 if (bank
->regs
->irqstatus2
) {
604 reg
= bank
->base
+ bank
->regs
->irqstatus2
;
605 writel_relaxed(gpio_mask
, reg
);
608 /* Flush posted write for the irq status to avoid spurious interrupts */
612 static inline void omap_clear_gpio_irqstatus(struct gpio_bank
*bank
,
615 omap_clear_gpio_irqbank(bank
, BIT(offset
));
618 static u32
omap_get_gpio_irqbank_mask(struct gpio_bank
*bank
)
620 void __iomem
*reg
= bank
->base
;
622 u32 mask
= (BIT(bank
->width
)) - 1;
624 reg
+= bank
->regs
->irqenable
;
625 l
= readl_relaxed(reg
);
626 if (bank
->regs
->irqenable_inv
)
632 static void omap_enable_gpio_irqbank(struct gpio_bank
*bank
, int gpio_mask
)
634 void __iomem
*reg
= bank
->base
;
637 if (bank
->regs
->set_irqenable
) {
638 reg
+= bank
->regs
->set_irqenable
;
640 bank
->context
.irqenable1
|= gpio_mask
;
642 reg
+= bank
->regs
->irqenable
;
643 l
= readl_relaxed(reg
);
644 if (bank
->regs
->irqenable_inv
)
648 bank
->context
.irqenable1
= l
;
651 writel_relaxed(l
, reg
);
654 static void omap_disable_gpio_irqbank(struct gpio_bank
*bank
, int gpio_mask
)
656 void __iomem
*reg
= bank
->base
;
659 if (bank
->regs
->clr_irqenable
) {
660 reg
+= bank
->regs
->clr_irqenable
;
662 bank
->context
.irqenable1
&= ~gpio_mask
;
664 reg
+= bank
->regs
->irqenable
;
665 l
= readl_relaxed(reg
);
666 if (bank
->regs
->irqenable_inv
)
670 bank
->context
.irqenable1
= l
;
673 writel_relaxed(l
, reg
);
676 static inline void omap_set_gpio_irqenable(struct gpio_bank
*bank
,
677 unsigned offset
, int enable
)
680 omap_enable_gpio_irqbank(bank
, BIT(offset
));
682 omap_disable_gpio_irqbank(bank
, BIT(offset
));
685 /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
686 static int omap_gpio_wake_enable(struct irq_data
*d
, unsigned int enable
)
688 struct gpio_bank
*bank
= omap_irq_data_get_bank(d
);
690 return irq_set_irq_wake(bank
->irq
, enable
);
693 static int omap_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
695 struct gpio_bank
*bank
= gpiochip_get_data(chip
);
698 pm_runtime_get_sync(chip
->parent
);
700 raw_spin_lock_irqsave(&bank
->lock
, flags
);
701 omap_enable_gpio_module(bank
, offset
);
702 bank
->mod_usage
|= BIT(offset
);
703 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
708 static void omap_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
710 struct gpio_bank
*bank
= gpiochip_get_data(chip
);
713 raw_spin_lock_irqsave(&bank
->lock
, flags
);
714 bank
->mod_usage
&= ~(BIT(offset
));
715 if (!LINE_USED(bank
->irq_usage
, offset
)) {
716 omap_set_gpio_direction(bank
, offset
, 1);
717 omap_clear_gpio_debounce(bank
, offset
);
719 omap_disable_gpio_module(bank
, offset
);
720 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
722 pm_runtime_put(chip
->parent
);
726 * We need to unmask the GPIO bank interrupt as soon as possible to
727 * avoid missing GPIO interrupts for other lines in the bank.
728 * Then we need to mask-read-clear-unmask the triggered GPIO lines
729 * in the bank to avoid missing nested interrupts for a GPIO line.
730 * If we wait to unmask individual GPIO lines in the bank after the
731 * line's interrupt handler has been run, we may miss some nested
734 static irqreturn_t
omap_gpio_irq_handler(int irq
, void *gpiobank
)
736 void __iomem
*isr_reg
= NULL
;
737 u32 enabled
, isr
, level_mask
;
739 struct gpio_bank
*bank
= gpiobank
;
740 unsigned long wa_lock_flags
;
741 unsigned long lock_flags
;
743 isr_reg
= bank
->base
+ bank
->regs
->irqstatus
;
744 if (WARN_ON(!isr_reg
))
747 if (WARN_ONCE(!pm_runtime_active(bank
->chip
.parent
),
748 "gpio irq%i while runtime suspended?\n", irq
))
752 raw_spin_lock_irqsave(&bank
->lock
, lock_flags
);
754 enabled
= omap_get_gpio_irqbank_mask(bank
);
755 isr
= readl_relaxed(isr_reg
) & enabled
;
757 if (bank
->level_mask
)
758 level_mask
= bank
->level_mask
& enabled
;
762 /* clear edge sensitive interrupts before handler(s) are
763 called so that we don't miss any interrupt occurred while
765 if (isr
& ~level_mask
)
766 omap_clear_gpio_irqbank(bank
, isr
& ~level_mask
);
768 raw_spin_unlock_irqrestore(&bank
->lock
, lock_flags
);
777 raw_spin_lock_irqsave(&bank
->lock
, lock_flags
);
779 * Some chips can't respond to both rising and falling
780 * at the same time. If this irq was requested with
781 * both flags, we need to flip the ICR data for the IRQ
782 * to respond to the IRQ for the opposite direction.
783 * This will be indicated in the bank toggle_mask.
785 if (bank
->toggle_mask
& (BIT(bit
)))
786 omap_toggle_gpio_edge_triggering(bank
, bit
);
788 raw_spin_unlock_irqrestore(&bank
->lock
, lock_flags
);
790 raw_spin_lock_irqsave(&bank
->wa_lock
, wa_lock_flags
);
792 generic_handle_irq(irq_find_mapping(bank
->chip
.irq
.domain
,
795 raw_spin_unlock_irqrestore(&bank
->wa_lock
,
803 static unsigned int omap_gpio_irq_startup(struct irq_data
*d
)
805 struct gpio_bank
*bank
= omap_irq_data_get_bank(d
);
807 unsigned offset
= d
->hwirq
;
809 raw_spin_lock_irqsave(&bank
->lock
, flags
);
811 if (!LINE_USED(bank
->mod_usage
, offset
))
812 omap_set_gpio_direction(bank
, offset
, 1);
813 else if (!omap_gpio_is_input(bank
, offset
))
815 omap_enable_gpio_module(bank
, offset
);
816 bank
->irq_usage
|= BIT(offset
);
818 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
819 omap_gpio_unmask_irq(d
);
823 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
827 static void omap_gpio_irq_shutdown(struct irq_data
*d
)
829 struct gpio_bank
*bank
= omap_irq_data_get_bank(d
);
831 unsigned offset
= d
->hwirq
;
833 raw_spin_lock_irqsave(&bank
->lock
, flags
);
834 bank
->irq_usage
&= ~(BIT(offset
));
835 omap_set_gpio_irqenable(bank
, offset
, 0);
836 omap_clear_gpio_irqstatus(bank
, offset
);
837 omap_set_gpio_triggering(bank
, offset
, IRQ_TYPE_NONE
);
838 if (!LINE_USED(bank
->mod_usage
, offset
))
839 omap_clear_gpio_debounce(bank
, offset
);
840 omap_disable_gpio_module(bank
, offset
);
841 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
844 static void omap_gpio_irq_bus_lock(struct irq_data
*data
)
846 struct gpio_bank
*bank
= omap_irq_data_get_bank(data
);
848 pm_runtime_get_sync(bank
->chip
.parent
);
851 static void gpio_irq_bus_sync_unlock(struct irq_data
*data
)
853 struct gpio_bank
*bank
= omap_irq_data_get_bank(data
);
855 pm_runtime_put(bank
->chip
.parent
);
858 static void omap_gpio_ack_irq(struct irq_data
*d
)
860 struct gpio_bank
*bank
= omap_irq_data_get_bank(d
);
861 unsigned offset
= d
->hwirq
;
863 omap_clear_gpio_irqstatus(bank
, offset
);
866 static void omap_gpio_mask_irq(struct irq_data
*d
)
868 struct gpio_bank
*bank
= omap_irq_data_get_bank(d
);
869 unsigned offset
= d
->hwirq
;
872 raw_spin_lock_irqsave(&bank
->lock
, flags
);
873 omap_set_gpio_irqenable(bank
, offset
, 0);
874 omap_set_gpio_triggering(bank
, offset
, IRQ_TYPE_NONE
);
875 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
878 static void omap_gpio_unmask_irq(struct irq_data
*d
)
880 struct gpio_bank
*bank
= omap_irq_data_get_bank(d
);
881 unsigned offset
= d
->hwirq
;
882 u32 trigger
= irqd_get_trigger_type(d
);
885 raw_spin_lock_irqsave(&bank
->lock
, flags
);
887 omap_set_gpio_triggering(bank
, offset
, trigger
);
889 omap_set_gpio_irqenable(bank
, offset
, 1);
892 * For level-triggered GPIOs, clearing must be done after the source
893 * is cleared, thus after the handler has run. OMAP4 needs this done
894 * after enabing the interrupt to clear the wakeup status.
896 if (bank
->level_mask
& BIT(offset
))
897 omap_clear_gpio_irqstatus(bank
, offset
);
899 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
902 /*---------------------------------------------------------------------*/
904 static int omap_mpuio_suspend_noirq(struct device
*dev
)
906 struct gpio_bank
*bank
= dev_get_drvdata(dev
);
907 void __iomem
*mask_reg
= bank
->base
+
908 OMAP_MPUIO_GPIO_MASKIT
/ bank
->stride
;
911 raw_spin_lock_irqsave(&bank
->lock
, flags
);
912 writel_relaxed(0xffff & ~bank
->context
.wake_en
, mask_reg
);
913 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
918 static int omap_mpuio_resume_noirq(struct device
*dev
)
920 struct gpio_bank
*bank
= dev_get_drvdata(dev
);
921 void __iomem
*mask_reg
= bank
->base
+
922 OMAP_MPUIO_GPIO_MASKIT
/ bank
->stride
;
925 raw_spin_lock_irqsave(&bank
->lock
, flags
);
926 writel_relaxed(bank
->context
.wake_en
, mask_reg
);
927 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
932 static const struct dev_pm_ops omap_mpuio_dev_pm_ops
= {
933 .suspend_noirq
= omap_mpuio_suspend_noirq
,
934 .resume_noirq
= omap_mpuio_resume_noirq
,
937 /* use platform_driver for this. */
938 static struct platform_driver omap_mpuio_driver
= {
941 .pm
= &omap_mpuio_dev_pm_ops
,
945 static struct platform_device omap_mpuio_device
= {
949 .driver
= &omap_mpuio_driver
.driver
,
951 /* could list the /proc/iomem resources */
954 static inline void omap_mpuio_init(struct gpio_bank
*bank
)
956 platform_set_drvdata(&omap_mpuio_device
, bank
);
958 if (platform_driver_register(&omap_mpuio_driver
) == 0)
959 (void) platform_device_register(&omap_mpuio_device
);
962 /*---------------------------------------------------------------------*/
964 static int omap_gpio_get_direction(struct gpio_chip
*chip
, unsigned offset
)
966 struct gpio_bank
*bank
;
971 bank
= gpiochip_get_data(chip
);
972 reg
= bank
->base
+ bank
->regs
->direction
;
973 raw_spin_lock_irqsave(&bank
->lock
, flags
);
974 dir
= !!(readl_relaxed(reg
) & BIT(offset
));
975 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
979 static int omap_gpio_input(struct gpio_chip
*chip
, unsigned offset
)
981 struct gpio_bank
*bank
;
984 bank
= gpiochip_get_data(chip
);
985 raw_spin_lock_irqsave(&bank
->lock
, flags
);
986 omap_set_gpio_direction(bank
, offset
, 1);
987 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
991 static int omap_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
993 struct gpio_bank
*bank
;
995 bank
= gpiochip_get_data(chip
);
997 if (omap_gpio_is_input(bank
, offset
))
998 return omap_get_gpio_datain(bank
, offset
);
1000 return omap_get_gpio_dataout(bank
, offset
);
1003 static int omap_gpio_output(struct gpio_chip
*chip
, unsigned offset
, int value
)
1005 struct gpio_bank
*bank
;
1006 unsigned long flags
;
1008 bank
= gpiochip_get_data(chip
);
1009 raw_spin_lock_irqsave(&bank
->lock
, flags
);
1010 bank
->set_dataout(bank
, offset
, value
);
1011 omap_set_gpio_direction(bank
, offset
, 0);
1012 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
1016 static int omap_gpio_get_multiple(struct gpio_chip
*chip
, unsigned long *mask
,
1017 unsigned long *bits
)
1019 struct gpio_bank
*bank
= gpiochip_get_data(chip
);
1020 void __iomem
*reg
= bank
->base
+ bank
->regs
->direction
;
1021 unsigned long in
= readl_relaxed(reg
), l
;
1027 *bits
|= omap_get_gpio_datain_multiple(bank
, &l
);
1031 *bits
|= omap_get_gpio_dataout_multiple(bank
, &l
);
1036 static int omap_gpio_debounce(struct gpio_chip
*chip
, unsigned offset
,
1039 struct gpio_bank
*bank
;
1040 unsigned long flags
;
1043 bank
= gpiochip_get_data(chip
);
1045 raw_spin_lock_irqsave(&bank
->lock
, flags
);
1046 ret
= omap2_set_gpio_debounce(bank
, offset
, debounce
);
1047 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
1050 dev_info(chip
->parent
,
1051 "Could not set line %u debounce to %u microseconds (%d)",
1052 offset
, debounce
, ret
);
1057 static int omap_gpio_set_config(struct gpio_chip
*chip
, unsigned offset
,
1058 unsigned long config
)
1062 if (pinconf_to_config_param(config
) != PIN_CONFIG_INPUT_DEBOUNCE
)
1065 debounce
= pinconf_to_config_argument(config
);
1066 return omap_gpio_debounce(chip
, offset
, debounce
);
1069 static void omap_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
1071 struct gpio_bank
*bank
;
1072 unsigned long flags
;
1074 bank
= gpiochip_get_data(chip
);
1075 raw_spin_lock_irqsave(&bank
->lock
, flags
);
1076 bank
->set_dataout(bank
, offset
, value
);
1077 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
1080 static void omap_gpio_set_multiple(struct gpio_chip
*chip
, unsigned long *mask
,
1081 unsigned long *bits
)
1083 struct gpio_bank
*bank
= gpiochip_get_data(chip
);
1084 unsigned long flags
;
1086 raw_spin_lock_irqsave(&bank
->lock
, flags
);
1087 bank
->set_dataout_multiple(bank
, mask
, bits
);
1088 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
1091 /*---------------------------------------------------------------------*/
1093 static void omap_gpio_show_rev(struct gpio_bank
*bank
)
1098 if (called
|| bank
->regs
->revision
== USHRT_MAX
)
1101 rev
= readw_relaxed(bank
->base
+ bank
->regs
->revision
);
1102 pr_info("OMAP GPIO hardware version %d.%d\n",
1103 (rev
>> 4) & 0x0f, rev
& 0x0f);
1108 static void omap_gpio_mod_init(struct gpio_bank
*bank
)
1110 void __iomem
*base
= bank
->base
;
1113 if (bank
->width
== 16)
1116 if (bank
->is_mpuio
) {
1117 writel_relaxed(l
, bank
->base
+ bank
->regs
->irqenable
);
1121 omap_gpio_rmw(base
, bank
->regs
->irqenable
, l
,
1122 bank
->regs
->irqenable_inv
);
1123 omap_gpio_rmw(base
, bank
->regs
->irqstatus
, l
,
1124 !bank
->regs
->irqenable_inv
);
1125 if (bank
->regs
->debounce_en
)
1126 writel_relaxed(0, base
+ bank
->regs
->debounce_en
);
1128 /* Save OE default value (0xffffffff) in the context */
1129 bank
->context
.oe
= readl_relaxed(bank
->base
+ bank
->regs
->direction
);
1130 /* Initialize interface clk ungated, module enabled */
1131 if (bank
->regs
->ctrl
)
1132 writel_relaxed(0, base
+ bank
->regs
->ctrl
);
1135 static int omap_gpio_chip_init(struct gpio_bank
*bank
, struct irq_chip
*irqc
)
1137 struct gpio_irq_chip
*irq
;
1144 * REVISIT eventually switch from OMAP-specific gpio structs
1145 * over to the generic ones
1147 bank
->chip
.request
= omap_gpio_request
;
1148 bank
->chip
.free
= omap_gpio_free
;
1149 bank
->chip
.get_direction
= omap_gpio_get_direction
;
1150 bank
->chip
.direction_input
= omap_gpio_input
;
1151 bank
->chip
.get
= omap_gpio_get
;
1152 bank
->chip
.get_multiple
= omap_gpio_get_multiple
;
1153 bank
->chip
.direction_output
= omap_gpio_output
;
1154 bank
->chip
.set_config
= omap_gpio_set_config
;
1155 bank
->chip
.set
= omap_gpio_set
;
1156 bank
->chip
.set_multiple
= omap_gpio_set_multiple
;
1157 if (bank
->is_mpuio
) {
1158 bank
->chip
.label
= "mpuio";
1159 if (bank
->regs
->wkup_en
)
1160 bank
->chip
.parent
= &omap_mpuio_device
.dev
;
1161 bank
->chip
.base
= OMAP_MPUIO(0);
1163 label
= devm_kasprintf(bank
->chip
.parent
, GFP_KERNEL
, "gpio-%d-%d",
1164 gpio
, gpio
+ bank
->width
- 1);
1167 bank
->chip
.label
= label
;
1168 bank
->chip
.base
= gpio
;
1170 bank
->chip
.ngpio
= bank
->width
;
1172 #ifdef CONFIG_ARCH_OMAP1
1174 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
1175 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
1177 irq_base
= devm_irq_alloc_descs(bank
->chip
.parent
,
1178 -1, 0, bank
->width
, 0);
1180 dev_err(bank
->chip
.parent
, "Couldn't allocate IRQ numbers\n");
1185 /* MPUIO is a bit different, reading IRQ status clears it */
1186 if (bank
->is_mpuio
) {
1187 irqc
->irq_ack
= dummy_irq_chip
.irq_ack
;
1188 if (!bank
->regs
->wkup_en
)
1189 irqc
->irq_set_wake
= NULL
;
1192 irq
= &bank
->chip
.irq
;
1194 irq
->handler
= handle_bad_irq
;
1195 irq
->default_type
= IRQ_TYPE_NONE
;
1196 irq
->num_parents
= 1;
1197 irq
->parents
= &bank
->irq
;
1198 irq
->first
= irq_base
;
1200 ret
= gpiochip_add_data(&bank
->chip
, bank
);
1202 dev_err(bank
->chip
.parent
,
1203 "Could not register gpio chip %d\n", ret
);
1207 ret
= devm_request_irq(bank
->chip
.parent
, bank
->irq
,
1208 omap_gpio_irq_handler
,
1209 0, dev_name(bank
->chip
.parent
), bank
);
1211 gpiochip_remove(&bank
->chip
);
1213 if (!bank
->is_mpuio
)
1214 gpio
+= bank
->width
;
1219 static void omap_gpio_init_context(struct gpio_bank
*p
)
1221 struct omap_gpio_reg_offs
*regs
= p
->regs
;
1222 void __iomem
*base
= p
->base
;
1224 p
->context
.ctrl
= readl_relaxed(base
+ regs
->ctrl
);
1225 p
->context
.oe
= readl_relaxed(base
+ regs
->direction
);
1226 p
->context
.wake_en
= readl_relaxed(base
+ regs
->wkup_en
);
1227 p
->context
.leveldetect0
= readl_relaxed(base
+ regs
->leveldetect0
);
1228 p
->context
.leveldetect1
= readl_relaxed(base
+ regs
->leveldetect1
);
1229 p
->context
.risingdetect
= readl_relaxed(base
+ regs
->risingdetect
);
1230 p
->context
.fallingdetect
= readl_relaxed(base
+ regs
->fallingdetect
);
1231 p
->context
.irqenable1
= readl_relaxed(base
+ regs
->irqenable
);
1232 p
->context
.irqenable2
= readl_relaxed(base
+ regs
->irqenable2
);
1234 if (regs
->set_dataout
&& p
->regs
->clr_dataout
)
1235 p
->context
.dataout
= readl_relaxed(base
+ regs
->set_dataout
);
1237 p
->context
.dataout
= readl_relaxed(base
+ regs
->dataout
);
1239 p
->context_valid
= true;
1242 static void omap_gpio_restore_context(struct gpio_bank
*bank
)
1244 writel_relaxed(bank
->context
.wake_en
,
1245 bank
->base
+ bank
->regs
->wkup_en
);
1246 writel_relaxed(bank
->context
.ctrl
, bank
->base
+ bank
->regs
->ctrl
);
1247 writel_relaxed(bank
->context
.leveldetect0
,
1248 bank
->base
+ bank
->regs
->leveldetect0
);
1249 writel_relaxed(bank
->context
.leveldetect1
,
1250 bank
->base
+ bank
->regs
->leveldetect1
);
1251 writel_relaxed(bank
->context
.risingdetect
,
1252 bank
->base
+ bank
->regs
->risingdetect
);
1253 writel_relaxed(bank
->context
.fallingdetect
,
1254 bank
->base
+ bank
->regs
->fallingdetect
);
1255 if (bank
->regs
->set_dataout
&& bank
->regs
->clr_dataout
)
1256 writel_relaxed(bank
->context
.dataout
,
1257 bank
->base
+ bank
->regs
->set_dataout
);
1259 writel_relaxed(bank
->context
.dataout
,
1260 bank
->base
+ bank
->regs
->dataout
);
1261 writel_relaxed(bank
->context
.oe
, bank
->base
+ bank
->regs
->direction
);
1263 if (bank
->dbck_enable_mask
) {
1264 writel_relaxed(bank
->context
.debounce
, bank
->base
+
1265 bank
->regs
->debounce
);
1266 writel_relaxed(bank
->context
.debounce_en
,
1267 bank
->base
+ bank
->regs
->debounce_en
);
1270 writel_relaxed(bank
->context
.irqenable1
,
1271 bank
->base
+ bank
->regs
->irqenable
);
1272 writel_relaxed(bank
->context
.irqenable2
,
1273 bank
->base
+ bank
->regs
->irqenable2
);
1276 static void omap_gpio_idle(struct gpio_bank
*bank
, bool may_lose_context
)
1278 struct device
*dev
= bank
->chip
.parent
;
1279 void __iomem
*base
= bank
->base
;
1282 bank
->saved_datain
= readl_relaxed(base
+ bank
->regs
->datain
);
1284 if (!bank
->enabled_non_wakeup_gpios
)
1285 goto update_gpio_context_count
;
1287 if (!may_lose_context
)
1288 goto update_gpio_context_count
;
1291 * If going to OFF, remove triggering for all wkup domain
1292 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1293 * generated. See OMAP2420 Errata item 1.101.
1295 if (!bank
->loses_context
&& bank
->enabled_non_wakeup_gpios
) {
1296 nowake
= bank
->enabled_non_wakeup_gpios
;
1297 omap_gpio_rmw(base
, bank
->regs
->fallingdetect
, nowake
, ~nowake
);
1298 omap_gpio_rmw(base
, bank
->regs
->risingdetect
, nowake
, ~nowake
);
1301 update_gpio_context_count
:
1302 if (bank
->get_context_loss_count
)
1303 bank
->context_loss_count
=
1304 bank
->get_context_loss_count(dev
);
1306 omap_gpio_dbck_disable(bank
);
1309 static void omap_gpio_unidle(struct gpio_bank
*bank
)
1311 struct device
*dev
= bank
->chip
.parent
;
1312 u32 l
= 0, gen
, gen0
, gen1
;
1316 * On the first resume during the probe, the context has not
1317 * been initialised and so initialise it now. Also initialise
1318 * the context loss count.
1320 if (bank
->loses_context
&& !bank
->context_valid
) {
1321 omap_gpio_init_context(bank
);
1323 if (bank
->get_context_loss_count
)
1324 bank
->context_loss_count
=
1325 bank
->get_context_loss_count(dev
);
1328 omap_gpio_dbck_enable(bank
);
1330 if (bank
->loses_context
) {
1331 if (!bank
->get_context_loss_count
) {
1332 omap_gpio_restore_context(bank
);
1334 c
= bank
->get_context_loss_count(dev
);
1335 if (c
!= bank
->context_loss_count
) {
1336 omap_gpio_restore_context(bank
);
1342 /* Restore changes done for OMAP2420 errata 1.101 */
1343 writel_relaxed(bank
->context
.fallingdetect
,
1344 bank
->base
+ bank
->regs
->fallingdetect
);
1345 writel_relaxed(bank
->context
.risingdetect
,
1346 bank
->base
+ bank
->regs
->risingdetect
);
1349 l
= readl_relaxed(bank
->base
+ bank
->regs
->datain
);
1352 * Check if any of the non-wakeup interrupt GPIOs have changed
1353 * state. If so, generate an IRQ by software. This is
1354 * horribly racy, but it's the best we can do to work around
1357 l
^= bank
->saved_datain
;
1358 l
&= bank
->enabled_non_wakeup_gpios
;
1361 * No need to generate IRQs for the rising edge for gpio IRQs
1362 * configured with falling edge only; and vice versa.
1364 gen0
= l
& bank
->context
.fallingdetect
;
1365 gen0
&= bank
->saved_datain
;
1367 gen1
= l
& bank
->context
.risingdetect
;
1368 gen1
&= ~(bank
->saved_datain
);
1370 /* FIXME: Consider GPIO IRQs with level detections properly! */
1371 gen
= l
& (~(bank
->context
.fallingdetect
) &
1372 ~(bank
->context
.risingdetect
));
1373 /* Consider all GPIO IRQs needed to be updated */
1379 old0
= readl_relaxed(bank
->base
+ bank
->regs
->leveldetect0
);
1380 old1
= readl_relaxed(bank
->base
+ bank
->regs
->leveldetect1
);
1382 if (!bank
->regs
->irqstatus_raw0
) {
1383 writel_relaxed(old0
| gen
, bank
->base
+
1384 bank
->regs
->leveldetect0
);
1385 writel_relaxed(old1
| gen
, bank
->base
+
1386 bank
->regs
->leveldetect1
);
1389 if (bank
->regs
->irqstatus_raw0
) {
1390 writel_relaxed(old0
| l
, bank
->base
+
1391 bank
->regs
->leveldetect0
);
1392 writel_relaxed(old1
| l
, bank
->base
+
1393 bank
->regs
->leveldetect1
);
1395 writel_relaxed(old0
, bank
->base
+ bank
->regs
->leveldetect0
);
1396 writel_relaxed(old1
, bank
->base
+ bank
->regs
->leveldetect1
);
1400 static int gpio_omap_cpu_notifier(struct notifier_block
*nb
,
1401 unsigned long cmd
, void *v
)
1403 struct gpio_bank
*bank
;
1404 unsigned long flags
;
1406 bank
= container_of(nb
, struct gpio_bank
, nb
);
1408 raw_spin_lock_irqsave(&bank
->lock
, flags
);
1410 case CPU_CLUSTER_PM_ENTER
:
1411 if (bank
->is_suspended
)
1413 omap_gpio_idle(bank
, true);
1415 case CPU_CLUSTER_PM_ENTER_FAILED
:
1416 case CPU_CLUSTER_PM_EXIT
:
1417 if (bank
->is_suspended
)
1419 omap_gpio_unidle(bank
);
1422 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
1427 static struct omap_gpio_reg_offs omap2_gpio_regs
= {
1428 .revision
= OMAP24XX_GPIO_REVISION
,
1429 .direction
= OMAP24XX_GPIO_OE
,
1430 .datain
= OMAP24XX_GPIO_DATAIN
,
1431 .dataout
= OMAP24XX_GPIO_DATAOUT
,
1432 .set_dataout
= OMAP24XX_GPIO_SETDATAOUT
,
1433 .clr_dataout
= OMAP24XX_GPIO_CLEARDATAOUT
,
1434 .irqstatus
= OMAP24XX_GPIO_IRQSTATUS1
,
1435 .irqstatus2
= OMAP24XX_GPIO_IRQSTATUS2
,
1436 .irqenable
= OMAP24XX_GPIO_IRQENABLE1
,
1437 .irqenable2
= OMAP24XX_GPIO_IRQENABLE2
,
1438 .set_irqenable
= OMAP24XX_GPIO_SETIRQENABLE1
,
1439 .clr_irqenable
= OMAP24XX_GPIO_CLEARIRQENABLE1
,
1440 .debounce
= OMAP24XX_GPIO_DEBOUNCE_VAL
,
1441 .debounce_en
= OMAP24XX_GPIO_DEBOUNCE_EN
,
1442 .ctrl
= OMAP24XX_GPIO_CTRL
,
1443 .wkup_en
= OMAP24XX_GPIO_WAKE_EN
,
1444 .leveldetect0
= OMAP24XX_GPIO_LEVELDETECT0
,
1445 .leveldetect1
= OMAP24XX_GPIO_LEVELDETECT1
,
1446 .risingdetect
= OMAP24XX_GPIO_RISINGDETECT
,
1447 .fallingdetect
= OMAP24XX_GPIO_FALLINGDETECT
,
1450 static struct omap_gpio_reg_offs omap4_gpio_regs
= {
1451 .revision
= OMAP4_GPIO_REVISION
,
1452 .direction
= OMAP4_GPIO_OE
,
1453 .datain
= OMAP4_GPIO_DATAIN
,
1454 .dataout
= OMAP4_GPIO_DATAOUT
,
1455 .set_dataout
= OMAP4_GPIO_SETDATAOUT
,
1456 .clr_dataout
= OMAP4_GPIO_CLEARDATAOUT
,
1457 .irqstatus
= OMAP4_GPIO_IRQSTATUS0
,
1458 .irqstatus2
= OMAP4_GPIO_IRQSTATUS1
,
1459 .irqenable
= OMAP4_GPIO_IRQSTATUSSET0
,
1460 .irqenable2
= OMAP4_GPIO_IRQSTATUSSET1
,
1461 .set_irqenable
= OMAP4_GPIO_IRQSTATUSSET0
,
1462 .clr_irqenable
= OMAP4_GPIO_IRQSTATUSCLR0
,
1463 .debounce
= OMAP4_GPIO_DEBOUNCINGTIME
,
1464 .debounce_en
= OMAP4_GPIO_DEBOUNCENABLE
,
1465 .ctrl
= OMAP4_GPIO_CTRL
,
1466 .wkup_en
= OMAP4_GPIO_IRQWAKEN0
,
1467 .leveldetect0
= OMAP4_GPIO_LEVELDETECT0
,
1468 .leveldetect1
= OMAP4_GPIO_LEVELDETECT1
,
1469 .risingdetect
= OMAP4_GPIO_RISINGDETECT
,
1470 .fallingdetect
= OMAP4_GPIO_FALLINGDETECT
,
1473 static const struct omap_gpio_platform_data omap2_pdata
= {
1474 .regs
= &omap2_gpio_regs
,
1479 static const struct omap_gpio_platform_data omap3_pdata
= {
1480 .regs
= &omap2_gpio_regs
,
1485 static const struct omap_gpio_platform_data omap4_pdata
= {
1486 .regs
= &omap4_gpio_regs
,
1491 static const struct of_device_id omap_gpio_match
[] = {
1493 .compatible
= "ti,omap4-gpio",
1494 .data
= &omap4_pdata
,
1497 .compatible
= "ti,omap3-gpio",
1498 .data
= &omap3_pdata
,
1501 .compatible
= "ti,omap2-gpio",
1502 .data
= &omap2_pdata
,
1506 MODULE_DEVICE_TABLE(of
, omap_gpio_match
);
1508 static int omap_gpio_probe(struct platform_device
*pdev
)
1510 struct device
*dev
= &pdev
->dev
;
1511 struct device_node
*node
= dev
->of_node
;
1512 const struct of_device_id
*match
;
1513 const struct omap_gpio_platform_data
*pdata
;
1514 struct gpio_bank
*bank
;
1515 struct irq_chip
*irqc
;
1518 match
= of_match_device(of_match_ptr(omap_gpio_match
), dev
);
1520 pdata
= match
? match
->data
: dev_get_platdata(dev
);
1524 bank
= devm_kzalloc(dev
, sizeof(*bank
), GFP_KERNEL
);
1528 irqc
= devm_kzalloc(dev
, sizeof(*irqc
), GFP_KERNEL
);
1532 irqc
->irq_startup
= omap_gpio_irq_startup
,
1533 irqc
->irq_shutdown
= omap_gpio_irq_shutdown
,
1534 irqc
->irq_ack
= omap_gpio_ack_irq
,
1535 irqc
->irq_mask
= omap_gpio_mask_irq
,
1536 irqc
->irq_unmask
= omap_gpio_unmask_irq
,
1537 irqc
->irq_set_type
= omap_gpio_irq_type
,
1538 irqc
->irq_set_wake
= omap_gpio_wake_enable
,
1539 irqc
->irq_bus_lock
= omap_gpio_irq_bus_lock
,
1540 irqc
->irq_bus_sync_unlock
= gpio_irq_bus_sync_unlock
,
1541 irqc
->name
= dev_name(&pdev
->dev
);
1542 irqc
->flags
= IRQCHIP_MASK_ON_SUSPEND
;
1543 irqc
->parent_device
= dev
;
1545 bank
->irq
= platform_get_irq(pdev
, 0);
1546 if (bank
->irq
<= 0) {
1549 if (bank
->irq
!= -EPROBE_DEFER
)
1551 "can't get irq resource ret=%d\n", bank
->irq
);
1555 bank
->chip
.parent
= dev
;
1556 bank
->chip
.owner
= THIS_MODULE
;
1557 bank
->dbck_flag
= pdata
->dbck_flag
;
1558 bank
->stride
= pdata
->bank_stride
;
1559 bank
->width
= pdata
->bank_width
;
1560 bank
->is_mpuio
= pdata
->is_mpuio
;
1561 bank
->non_wakeup_gpios
= pdata
->non_wakeup_gpios
;
1562 bank
->regs
= pdata
->regs
;
1563 #ifdef CONFIG_OF_GPIO
1564 bank
->chip
.of_node
= of_node_get(node
);
1568 if (!of_property_read_bool(node
, "ti,gpio-always-on"))
1569 bank
->loses_context
= true;
1571 bank
->loses_context
= pdata
->loses_context
;
1573 if (bank
->loses_context
)
1574 bank
->get_context_loss_count
=
1575 pdata
->get_context_loss_count
;
1578 if (bank
->regs
->set_dataout
&& bank
->regs
->clr_dataout
) {
1579 bank
->set_dataout
= omap_set_gpio_dataout_reg
;
1580 bank
->set_dataout_multiple
= omap_set_gpio_dataout_reg_multiple
;
1582 bank
->set_dataout
= omap_set_gpio_dataout_mask
;
1583 bank
->set_dataout_multiple
=
1584 omap_set_gpio_dataout_mask_multiple
;
1587 raw_spin_lock_init(&bank
->lock
);
1588 raw_spin_lock_init(&bank
->wa_lock
);
1590 /* Static mapping, never released */
1591 bank
->base
= devm_platform_ioremap_resource(pdev
, 0);
1592 if (IS_ERR(bank
->base
)) {
1593 return PTR_ERR(bank
->base
);
1596 if (bank
->dbck_flag
) {
1597 bank
->dbck
= devm_clk_get(dev
, "dbclk");
1598 if (IS_ERR(bank
->dbck
)) {
1600 "Could not get gpio dbck. Disable debounce\n");
1601 bank
->dbck_flag
= false;
1603 clk_prepare(bank
->dbck
);
1607 platform_set_drvdata(pdev
, bank
);
1609 pm_runtime_enable(dev
);
1610 pm_runtime_get_sync(dev
);
1613 omap_mpuio_init(bank
);
1615 omap_gpio_mod_init(bank
);
1617 ret
= omap_gpio_chip_init(bank
, irqc
);
1619 pm_runtime_put_sync(dev
);
1620 pm_runtime_disable(dev
);
1621 if (bank
->dbck_flag
)
1622 clk_unprepare(bank
->dbck
);
1626 omap_gpio_show_rev(bank
);
1628 bank
->nb
.notifier_call
= gpio_omap_cpu_notifier
;
1629 cpu_pm_register_notifier(&bank
->nb
);
1631 pm_runtime_put(dev
);
1636 static int omap_gpio_remove(struct platform_device
*pdev
)
1638 struct gpio_bank
*bank
= platform_get_drvdata(pdev
);
1640 cpu_pm_unregister_notifier(&bank
->nb
);
1641 list_del(&bank
->node
);
1642 gpiochip_remove(&bank
->chip
);
1643 pm_runtime_disable(&pdev
->dev
);
1644 if (bank
->dbck_flag
)
1645 clk_unprepare(bank
->dbck
);
1650 static int __maybe_unused
omap_gpio_runtime_suspend(struct device
*dev
)
1652 struct gpio_bank
*bank
= dev_get_drvdata(dev
);
1653 unsigned long flags
;
1655 raw_spin_lock_irqsave(&bank
->lock
, flags
);
1656 omap_gpio_idle(bank
, true);
1657 bank
->is_suspended
= true;
1658 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
1663 static int __maybe_unused
omap_gpio_runtime_resume(struct device
*dev
)
1665 struct gpio_bank
*bank
= dev_get_drvdata(dev
);
1666 unsigned long flags
;
1668 raw_spin_lock_irqsave(&bank
->lock
, flags
);
1669 omap_gpio_unidle(bank
);
1670 bank
->is_suspended
= false;
1671 raw_spin_unlock_irqrestore(&bank
->lock
, flags
);
1676 static const struct dev_pm_ops gpio_pm_ops
= {
1677 SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend
, omap_gpio_runtime_resume
,
1681 static struct platform_driver omap_gpio_driver
= {
1682 .probe
= omap_gpio_probe
,
1683 .remove
= omap_gpio_remove
,
1685 .name
= "omap_gpio",
1687 .of_match_table
= omap_gpio_match
,
1692 * gpio driver register needs to be done before
1693 * machine_init functions access gpio APIs.
1694 * Hence omap_gpio_drv_reg() is a postcore_initcall.
1696 static int __init
omap_gpio_drv_reg(void)
1698 return platform_driver_register(&omap_gpio_driver
);
1700 postcore_initcall(omap_gpio_drv_reg
);
1702 static void __exit
omap_gpio_exit(void)
1704 platform_driver_unregister(&omap_gpio_driver
);
1706 module_exit(omap_gpio_exit
);
1708 MODULE_DESCRIPTION("omap gpio driver");
1709 MODULE_ALIAS("platform:gpio-omap");
1710 MODULE_LICENSE("GPL v2");