2 * Generic GPIO driver for logic cells found in the Nomadik SoC
4 * Copyright (C) 2008,2009 STMicroelectronics
5 * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
6 * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
7 * Copyright (C) 2011 Linus Walleij <linus.walleij@linaro.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
21 #include <linux/gpio.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <linux/slab.h>
27 #include <asm/mach/irq.h>
29 #include <plat/pincfg.h>
30 #include <plat/gpio-nomadik.h>
31 #include <mach/hardware.h>
35 * The GPIO module in the Nomadik family of Systems-on-Chip is an
36 * AMBA device, managing 32 pins and alternate functions. The logic block
37 * is currently used in the Nomadik and ux500.
39 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
42 #define NMK_GPIO_PER_CHIP 32
44 struct nmk_gpio_chip
{
45 struct gpio_chip chip
;
49 unsigned int parent_irq
;
50 int secondary_parent_irq
;
51 u32 (*get_secondary_status
)(unsigned int bank
);
52 void (*set_ioforce
)(bool enable
);
55 /* Keep track of configured edges */
66 static struct nmk_gpio_chip
*
67 nmk_gpio_chips
[DIV_ROUND_UP(ARCH_NR_GPIOS
, NMK_GPIO_PER_CHIP
)];
69 static DEFINE_SPINLOCK(nmk_gpio_slpm_lock
);
71 #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
73 static void __nmk_gpio_set_mode(struct nmk_gpio_chip
*nmk_chip
,
74 unsigned offset
, int gpio_mode
)
76 u32 bit
= 1 << offset
;
79 afunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLA
) & ~bit
;
80 bfunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLB
) & ~bit
;
81 if (gpio_mode
& NMK_GPIO_ALT_A
)
83 if (gpio_mode
& NMK_GPIO_ALT_B
)
85 writel(afunc
, nmk_chip
->addr
+ NMK_GPIO_AFSLA
);
86 writel(bfunc
, nmk_chip
->addr
+ NMK_GPIO_AFSLB
);
89 static void __nmk_gpio_set_slpm(struct nmk_gpio_chip
*nmk_chip
,
90 unsigned offset
, enum nmk_gpio_slpm mode
)
92 u32 bit
= 1 << offset
;
95 slpm
= readl(nmk_chip
->addr
+ NMK_GPIO_SLPC
);
96 if (mode
== NMK_GPIO_SLPM_NOCHANGE
)
100 writel(slpm
, nmk_chip
->addr
+ NMK_GPIO_SLPC
);
103 static void __nmk_gpio_set_pull(struct nmk_gpio_chip
*nmk_chip
,
104 unsigned offset
, enum nmk_gpio_pull pull
)
106 u32 bit
= 1 << offset
;
109 pdis
= readl(nmk_chip
->addr
+ NMK_GPIO_PDIS
);
110 if (pull
== NMK_GPIO_PULL_NONE
) {
112 nmk_chip
->pull_up
&= ~bit
;
117 writel(pdis
, nmk_chip
->addr
+ NMK_GPIO_PDIS
);
119 if (pull
== NMK_GPIO_PULL_UP
) {
120 nmk_chip
->pull_up
|= bit
;
121 writel(bit
, nmk_chip
->addr
+ NMK_GPIO_DATS
);
122 } else if (pull
== NMK_GPIO_PULL_DOWN
) {
123 nmk_chip
->pull_up
&= ~bit
;
124 writel(bit
, nmk_chip
->addr
+ NMK_GPIO_DATC
);
128 static void __nmk_gpio_make_input(struct nmk_gpio_chip
*nmk_chip
,
131 writel(1 << offset
, nmk_chip
->addr
+ NMK_GPIO_DIRC
);
134 static void __nmk_gpio_set_output(struct nmk_gpio_chip
*nmk_chip
,
135 unsigned offset
, int val
)
138 writel(1 << offset
, nmk_chip
->addr
+ NMK_GPIO_DATS
);
140 writel(1 << offset
, nmk_chip
->addr
+ NMK_GPIO_DATC
);
143 static void __nmk_gpio_make_output(struct nmk_gpio_chip
*nmk_chip
,
144 unsigned offset
, int val
)
146 writel(1 << offset
, nmk_chip
->addr
+ NMK_GPIO_DIRS
);
147 __nmk_gpio_set_output(nmk_chip
, offset
, val
);
150 static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip
*nmk_chip
,
151 unsigned offset
, int gpio_mode
,
154 u32 rwimsc
= readl(nmk_chip
->addr
+ NMK_GPIO_RWIMSC
);
155 u32 fwimsc
= readl(nmk_chip
->addr
+ NMK_GPIO_FWIMSC
);
157 if (glitch
&& nmk_chip
->set_ioforce
) {
158 u32 bit
= BIT(offset
);
160 /* Prevent spurious wakeups */
161 writel(rwimsc
& ~bit
, nmk_chip
->addr
+ NMK_GPIO_RWIMSC
);
162 writel(fwimsc
& ~bit
, nmk_chip
->addr
+ NMK_GPIO_FWIMSC
);
164 nmk_chip
->set_ioforce(true);
167 __nmk_gpio_set_mode(nmk_chip
, offset
, gpio_mode
);
169 if (glitch
&& nmk_chip
->set_ioforce
) {
170 nmk_chip
->set_ioforce(false);
172 writel(rwimsc
, nmk_chip
->addr
+ NMK_GPIO_RWIMSC
);
173 writel(fwimsc
, nmk_chip
->addr
+ NMK_GPIO_FWIMSC
);
177 static void __nmk_config_pin(struct nmk_gpio_chip
*nmk_chip
, unsigned offset
,
178 pin_cfg_t cfg
, bool sleep
, unsigned int *slpmregs
)
180 static const char *afnames
[] = {
181 [NMK_GPIO_ALT_GPIO
] = "GPIO",
182 [NMK_GPIO_ALT_A
] = "A",
183 [NMK_GPIO_ALT_B
] = "B",
184 [NMK_GPIO_ALT_C
] = "C"
186 static const char *pullnames
[] = {
187 [NMK_GPIO_PULL_NONE
] = "none",
188 [NMK_GPIO_PULL_UP
] = "up",
189 [NMK_GPIO_PULL_DOWN
] = "down",
190 [3] /* illegal */ = "??"
192 static const char *slpmnames
[] = {
193 [NMK_GPIO_SLPM_INPUT
] = "input/wakeup",
194 [NMK_GPIO_SLPM_NOCHANGE
] = "no-change/no-wakeup",
197 int pin
= PIN_NUM(cfg
);
198 int pull
= PIN_PULL(cfg
);
199 int af
= PIN_ALT(cfg
);
200 int slpm
= PIN_SLPM(cfg
);
201 int output
= PIN_DIR(cfg
);
202 int val
= PIN_VAL(cfg
);
203 bool glitch
= af
== NMK_GPIO_ALT_C
;
205 dev_dbg(nmk_chip
->chip
.dev
, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
206 pin
, cfg
, afnames
[af
], pullnames
[pull
], slpmnames
[slpm
],
207 output
? "output " : "input",
208 output
? (val
? "high" : "low") : "");
211 int slpm_pull
= PIN_SLPM_PULL(cfg
);
212 int slpm_output
= PIN_SLPM_DIR(cfg
);
213 int slpm_val
= PIN_SLPM_VAL(cfg
);
215 af
= NMK_GPIO_ALT_GPIO
;
218 * The SLPM_* values are normal values + 1 to allow zero to
219 * mean "same as normal".
222 pull
= slpm_pull
- 1;
224 output
= slpm_output
- 1;
228 dev_dbg(nmk_chip
->chip
.dev
, "pin %d: sleep pull %s, dir %s, val %s\n",
230 slpm_pull
? pullnames
[pull
] : "same",
231 slpm_output
? (output
? "output" : "input") : "same",
232 slpm_val
? (val
? "high" : "low") : "same");
236 __nmk_gpio_make_output(nmk_chip
, offset
, val
);
238 __nmk_gpio_make_input(nmk_chip
, offset
);
239 __nmk_gpio_set_pull(nmk_chip
, offset
, pull
);
243 * If we've backed up the SLPM registers (glitch workaround), modify
244 * the backups since they will be restored.
247 if (slpm
== NMK_GPIO_SLPM_NOCHANGE
)
248 slpmregs
[nmk_chip
->bank
] |= BIT(offset
);
250 slpmregs
[nmk_chip
->bank
] &= ~BIT(offset
);
252 __nmk_gpio_set_slpm(nmk_chip
, offset
, slpm
);
254 __nmk_gpio_set_mode_safe(nmk_chip
, offset
, af
, glitch
);
258 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
259 * - Save SLPM registers
260 * - Set SLPM=0 for the IOs you want to switch and others to 1
261 * - Configure the GPIO registers for the IOs that are being switched
263 * - Modify the AFLSA/B registers for the IOs that are being switched
265 * - Restore SLPM registers
266 * - Any spurious wake up event during switch sequence to be ignored and
269 static void nmk_gpio_glitch_slpm_init(unsigned int *slpm
)
273 for (i
= 0; i
< NUM_BANKS
; i
++) {
274 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
275 unsigned int temp
= slpm
[i
];
280 slpm
[i
] = readl(chip
->addr
+ NMK_GPIO_SLPC
);
281 writel(temp
, chip
->addr
+ NMK_GPIO_SLPC
);
285 static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm
)
289 for (i
= 0; i
< NUM_BANKS
; i
++) {
290 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
295 writel(slpm
[i
], chip
->addr
+ NMK_GPIO_SLPC
);
299 static int __nmk_config_pins(pin_cfg_t
*cfgs
, int num
, bool sleep
)
301 static unsigned int slpm
[NUM_BANKS
];
307 for (i
= 0; i
< num
; i
++) {
308 if (PIN_ALT(cfgs
[i
]) == NMK_GPIO_ALT_C
) {
314 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
317 memset(slpm
, 0xff, sizeof(slpm
));
319 for (i
= 0; i
< num
; i
++) {
320 int pin
= PIN_NUM(cfgs
[i
]);
321 int offset
= pin
% NMK_GPIO_PER_CHIP
;
323 if (PIN_ALT(cfgs
[i
]) == NMK_GPIO_ALT_C
)
324 slpm
[pin
/ NMK_GPIO_PER_CHIP
] &= ~BIT(offset
);
327 nmk_gpio_glitch_slpm_init(slpm
);
330 for (i
= 0; i
< num
; i
++) {
331 struct nmk_gpio_chip
*nmk_chip
;
332 int pin
= PIN_NUM(cfgs
[i
]);
334 nmk_chip
= irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(pin
));
340 spin_lock(&nmk_chip
->lock
);
341 __nmk_config_pin(nmk_chip
, pin
- nmk_chip
->chip
.base
,
342 cfgs
[i
], sleep
, glitch
? slpm
: NULL
);
343 spin_unlock(&nmk_chip
->lock
);
347 nmk_gpio_glitch_slpm_restore(slpm
);
349 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
355 * nmk_config_pin - configure a pin's mux attributes
356 * @cfg: pin confguration
358 * Configures a pin's mode (alternate function or GPIO), its pull up status,
359 * and its sleep mode based on the specified configuration. The @cfg is
360 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
361 * are constructed using, and can be further enhanced with, the macros in
364 * If a pin's mode is set to GPIO, it is configured as an input to avoid
365 * side-effects. The gpio can be manipulated later using standard GPIO API
368 int nmk_config_pin(pin_cfg_t cfg
, bool sleep
)
370 return __nmk_config_pins(&cfg
, 1, sleep
);
372 EXPORT_SYMBOL(nmk_config_pin
);
375 * nmk_config_pins - configure several pins at once
376 * @cfgs: array of pin configurations
377 * @num: number of elments in the array
379 * Configures several pins using nmk_config_pin(). Refer to that function for
380 * further information.
382 int nmk_config_pins(pin_cfg_t
*cfgs
, int num
)
384 return __nmk_config_pins(cfgs
, num
, false);
386 EXPORT_SYMBOL(nmk_config_pins
);
388 int nmk_config_pins_sleep(pin_cfg_t
*cfgs
, int num
)
390 return __nmk_config_pins(cfgs
, num
, true);
392 EXPORT_SYMBOL(nmk_config_pins_sleep
);
395 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
397 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
399 * This register is actually in the pinmux layer, not the GPIO block itself.
400 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
401 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
402 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
403 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
404 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
405 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
407 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
408 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
409 * entered) regardless of the altfunction selected. Also wake-up detection is
412 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
413 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
414 * (for altfunction GPIO) or respective on-chip peripherals (for other
415 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
417 * Note that enable_irq_wake() will automatically enable wakeup detection.
419 int nmk_gpio_set_slpm(int gpio
, enum nmk_gpio_slpm mode
)
421 struct nmk_gpio_chip
*nmk_chip
;
424 nmk_chip
= irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio
));
428 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
429 spin_lock(&nmk_chip
->lock
);
431 __nmk_gpio_set_slpm(nmk_chip
, gpio
- nmk_chip
->chip
.base
, mode
);
433 spin_unlock(&nmk_chip
->lock
);
434 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
440 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
442 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
444 * Enables/disables pull up/down on a specified pin. This only takes effect if
445 * the pin is configured as an input (either explicitly or by the alternate
448 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
449 * configured as an input. Otherwise, due to the way the controller registers
450 * work, this function will change the value output on the pin.
452 int nmk_gpio_set_pull(int gpio
, enum nmk_gpio_pull pull
)
454 struct nmk_gpio_chip
*nmk_chip
;
457 nmk_chip
= irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio
));
461 spin_lock_irqsave(&nmk_chip
->lock
, flags
);
462 __nmk_gpio_set_pull(nmk_chip
, gpio
- nmk_chip
->chip
.base
, pull
);
463 spin_unlock_irqrestore(&nmk_chip
->lock
, flags
);
470 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
472 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
473 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
475 * Sets the mode of the specified pin to one of the alternate functions or
478 int nmk_gpio_set_mode(int gpio
, int gpio_mode
)
480 struct nmk_gpio_chip
*nmk_chip
;
483 nmk_chip
= irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio
));
487 spin_lock_irqsave(&nmk_chip
->lock
, flags
);
488 __nmk_gpio_set_mode(nmk_chip
, gpio
- nmk_chip
->chip
.base
, gpio_mode
);
489 spin_unlock_irqrestore(&nmk_chip
->lock
, flags
);
493 EXPORT_SYMBOL(nmk_gpio_set_mode
);
495 int nmk_gpio_get_mode(int gpio
)
497 struct nmk_gpio_chip
*nmk_chip
;
498 u32 afunc
, bfunc
, bit
;
500 nmk_chip
= irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio
));
504 bit
= 1 << (gpio
- nmk_chip
->chip
.base
);
506 afunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLA
) & bit
;
507 bfunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLB
) & bit
;
509 return (afunc
? NMK_GPIO_ALT_A
: 0) | (bfunc
? NMK_GPIO_ALT_B
: 0);
511 EXPORT_SYMBOL(nmk_gpio_get_mode
);
515 static inline int nmk_gpio_get_bitmask(int gpio
)
517 return 1 << (gpio
% 32);
520 static void nmk_gpio_irq_ack(struct irq_data
*d
)
523 struct nmk_gpio_chip
*nmk_chip
;
525 gpio
= NOMADIK_IRQ_TO_GPIO(d
->irq
);
526 nmk_chip
= irq_data_get_irq_chip_data(d
);
529 writel(nmk_gpio_get_bitmask(gpio
), nmk_chip
->addr
+ NMK_GPIO_IC
);
532 enum nmk_gpio_irq_type
{
537 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip
*nmk_chip
,
538 int gpio
, enum nmk_gpio_irq_type which
,
541 u32 rimsc
= which
== WAKE
? NMK_GPIO_RWIMSC
: NMK_GPIO_RIMSC
;
542 u32 fimsc
= which
== WAKE
? NMK_GPIO_FWIMSC
: NMK_GPIO_FIMSC
;
543 u32 bitmask
= nmk_gpio_get_bitmask(gpio
);
546 /* we must individually set/clear the two edges */
547 if (nmk_chip
->edge_rising
& bitmask
) {
548 reg
= readl(nmk_chip
->addr
+ rimsc
);
553 writel(reg
, nmk_chip
->addr
+ rimsc
);
555 if (nmk_chip
->edge_falling
& bitmask
) {
556 reg
= readl(nmk_chip
->addr
+ fimsc
);
561 writel(reg
, nmk_chip
->addr
+ fimsc
);
565 static void __nmk_gpio_set_wake(struct nmk_gpio_chip
*nmk_chip
,
568 if (nmk_chip
->sleepmode
) {
569 __nmk_gpio_set_slpm(nmk_chip
, gpio
- nmk_chip
->chip
.base
,
570 on
? NMK_GPIO_SLPM_WAKEUP_ENABLE
571 : NMK_GPIO_SLPM_WAKEUP_DISABLE
);
574 __nmk_gpio_irq_modify(nmk_chip
, gpio
, WAKE
, on
);
577 static int nmk_gpio_irq_maskunmask(struct irq_data
*d
, bool enable
)
580 struct nmk_gpio_chip
*nmk_chip
;
584 gpio
= NOMADIK_IRQ_TO_GPIO(d
->irq
);
585 nmk_chip
= irq_data_get_irq_chip_data(d
);
586 bitmask
= nmk_gpio_get_bitmask(gpio
);
591 nmk_chip
->enabled
|= bitmask
;
593 nmk_chip
->enabled
&= ~bitmask
;
595 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
596 spin_lock(&nmk_chip
->lock
);
598 __nmk_gpio_irq_modify(nmk_chip
, gpio
, NORMAL
, enable
);
600 if (!(nmk_chip
->real_wake
& bitmask
))
601 __nmk_gpio_set_wake(nmk_chip
, gpio
, enable
);
603 spin_unlock(&nmk_chip
->lock
);
604 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
609 static void nmk_gpio_irq_mask(struct irq_data
*d
)
611 nmk_gpio_irq_maskunmask(d
, false);
614 static void nmk_gpio_irq_unmask(struct irq_data
*d
)
616 nmk_gpio_irq_maskunmask(d
, true);
619 static int nmk_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
621 struct nmk_gpio_chip
*nmk_chip
;
626 gpio
= NOMADIK_IRQ_TO_GPIO(d
->irq
);
627 nmk_chip
= irq_data_get_irq_chip_data(d
);
630 bitmask
= nmk_gpio_get_bitmask(gpio
);
632 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
633 spin_lock(&nmk_chip
->lock
);
635 if (!(nmk_chip
->enabled
& bitmask
))
636 __nmk_gpio_set_wake(nmk_chip
, gpio
, on
);
639 nmk_chip
->real_wake
|= bitmask
;
641 nmk_chip
->real_wake
&= ~bitmask
;
643 spin_unlock(&nmk_chip
->lock
);
644 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
649 static int nmk_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
651 bool enabled
, wake
= irqd_is_wakeup_set(d
);
653 struct nmk_gpio_chip
*nmk_chip
;
657 gpio
= NOMADIK_IRQ_TO_GPIO(d
->irq
);
658 nmk_chip
= irq_data_get_irq_chip_data(d
);
659 bitmask
= nmk_gpio_get_bitmask(gpio
);
663 if (type
& IRQ_TYPE_LEVEL_HIGH
)
665 if (type
& IRQ_TYPE_LEVEL_LOW
)
668 enabled
= nmk_chip
->enabled
& bitmask
;
670 spin_lock_irqsave(&nmk_chip
->lock
, flags
);
673 __nmk_gpio_irq_modify(nmk_chip
, gpio
, NORMAL
, false);
676 __nmk_gpio_irq_modify(nmk_chip
, gpio
, WAKE
, false);
678 nmk_chip
->edge_rising
&= ~bitmask
;
679 if (type
& IRQ_TYPE_EDGE_RISING
)
680 nmk_chip
->edge_rising
|= bitmask
;
682 nmk_chip
->edge_falling
&= ~bitmask
;
683 if (type
& IRQ_TYPE_EDGE_FALLING
)
684 nmk_chip
->edge_falling
|= bitmask
;
687 __nmk_gpio_irq_modify(nmk_chip
, gpio
, NORMAL
, true);
690 __nmk_gpio_irq_modify(nmk_chip
, gpio
, WAKE
, true);
692 spin_unlock_irqrestore(&nmk_chip
->lock
, flags
);
697 static struct irq_chip nmk_gpio_irq_chip
= {
698 .name
= "Nomadik-GPIO",
699 .irq_ack
= nmk_gpio_irq_ack
,
700 .irq_mask
= nmk_gpio_irq_mask
,
701 .irq_unmask
= nmk_gpio_irq_unmask
,
702 .irq_set_type
= nmk_gpio_irq_set_type
,
703 .irq_set_wake
= nmk_gpio_irq_set_wake
,
706 static void __nmk_gpio_irq_handler(unsigned int irq
, struct irq_desc
*desc
,
709 struct nmk_gpio_chip
*nmk_chip
;
710 struct irq_chip
*host_chip
= irq_get_chip(irq
);
711 unsigned int first_irq
;
713 chained_irq_enter(host_chip
, desc
);
715 nmk_chip
= irq_get_handler_data(irq
);
716 first_irq
= NOMADIK_GPIO_TO_IRQ(nmk_chip
->chip
.base
);
718 int bit
= __ffs(status
);
720 generic_handle_irq(first_irq
+ bit
);
724 chained_irq_exit(host_chip
, desc
);
727 static void nmk_gpio_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
729 struct nmk_gpio_chip
*nmk_chip
= irq_get_handler_data(irq
);
730 u32 status
= readl(nmk_chip
->addr
+ NMK_GPIO_IS
);
732 __nmk_gpio_irq_handler(irq
, desc
, status
);
735 static void nmk_gpio_secondary_irq_handler(unsigned int irq
,
736 struct irq_desc
*desc
)
738 struct nmk_gpio_chip
*nmk_chip
= irq_get_handler_data(irq
);
739 u32 status
= nmk_chip
->get_secondary_status(nmk_chip
->bank
);
741 __nmk_gpio_irq_handler(irq
, desc
, status
);
744 static int nmk_gpio_init_irq(struct nmk_gpio_chip
*nmk_chip
)
746 unsigned int first_irq
;
749 first_irq
= NOMADIK_GPIO_TO_IRQ(nmk_chip
->chip
.base
);
750 for (i
= first_irq
; i
< first_irq
+ nmk_chip
->chip
.ngpio
; i
++) {
751 irq_set_chip_and_handler(i
, &nmk_gpio_irq_chip
,
753 set_irq_flags(i
, IRQF_VALID
);
754 irq_set_chip_data(i
, nmk_chip
);
755 irq_set_irq_type(i
, IRQ_TYPE_EDGE_FALLING
);
758 irq_set_chained_handler(nmk_chip
->parent_irq
, nmk_gpio_irq_handler
);
759 irq_set_handler_data(nmk_chip
->parent_irq
, nmk_chip
);
761 if (nmk_chip
->secondary_parent_irq
>= 0) {
762 irq_set_chained_handler(nmk_chip
->secondary_parent_irq
,
763 nmk_gpio_secondary_irq_handler
);
764 irq_set_handler_data(nmk_chip
->secondary_parent_irq
, nmk_chip
);
771 static int nmk_gpio_make_input(struct gpio_chip
*chip
, unsigned offset
)
773 struct nmk_gpio_chip
*nmk_chip
=
774 container_of(chip
, struct nmk_gpio_chip
, chip
);
776 writel(1 << offset
, nmk_chip
->addr
+ NMK_GPIO_DIRC
);
780 static int nmk_gpio_get_input(struct gpio_chip
*chip
, unsigned offset
)
782 struct nmk_gpio_chip
*nmk_chip
=
783 container_of(chip
, struct nmk_gpio_chip
, chip
);
784 u32 bit
= 1 << offset
;
786 return (readl(nmk_chip
->addr
+ NMK_GPIO_DAT
) & bit
) != 0;
789 static void nmk_gpio_set_output(struct gpio_chip
*chip
, unsigned offset
,
792 struct nmk_gpio_chip
*nmk_chip
=
793 container_of(chip
, struct nmk_gpio_chip
, chip
);
795 __nmk_gpio_set_output(nmk_chip
, offset
, val
);
798 static int nmk_gpio_make_output(struct gpio_chip
*chip
, unsigned offset
,
801 struct nmk_gpio_chip
*nmk_chip
=
802 container_of(chip
, struct nmk_gpio_chip
, chip
);
804 __nmk_gpio_make_output(nmk_chip
, offset
, val
);
809 static int nmk_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
811 struct nmk_gpio_chip
*nmk_chip
=
812 container_of(chip
, struct nmk_gpio_chip
, chip
);
814 return NOMADIK_GPIO_TO_IRQ(nmk_chip
->chip
.base
) + offset
;
817 #ifdef CONFIG_DEBUG_FS
819 #include <linux/seq_file.h>
821 static void nmk_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
825 unsigned gpio
= chip
->base
;
827 struct nmk_gpio_chip
*nmk_chip
=
828 container_of(chip
, struct nmk_gpio_chip
, chip
);
829 const char *modes
[] = {
830 [NMK_GPIO_ALT_GPIO
] = "gpio",
831 [NMK_GPIO_ALT_A
] = "altA",
832 [NMK_GPIO_ALT_B
] = "altB",
833 [NMK_GPIO_ALT_C
] = "altC",
836 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++) {
837 const char *label
= gpiochip_is_requested(chip
, i
);
841 is_out
= readl(nmk_chip
->addr
+ NMK_GPIO_DIR
) & bit
;
842 pull
= !(readl(nmk_chip
->addr
+ NMK_GPIO_PDIS
) & bit
);
843 mode
= nmk_gpio_get_mode(gpio
);
844 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s %s %s",
845 gpio
, label
?: "(none)",
846 is_out
? "out" : "in ",
848 ? (chip
->get(chip
, i
) ? "hi" : "lo")
850 (mode
< 0) ? "unknown" : modes
[mode
],
851 pull
? "pull" : "none");
853 if (label
&& !is_out
) {
854 int irq
= gpio_to_irq(gpio
);
855 struct irq_desc
*desc
= irq_to_desc(irq
);
857 /* This races with request_irq(), set_irq_type(),
858 * and set_irq_wake() ... but those are "rare".
860 if (irq
>= 0 && desc
->action
) {
862 u32 bitmask
= nmk_gpio_get_bitmask(gpio
);
864 if (nmk_chip
->edge_rising
& bitmask
)
865 trigger
= "edge-rising";
866 else if (nmk_chip
->edge_falling
& bitmask
)
867 trigger
= "edge-falling";
869 trigger
= "edge-undefined";
871 seq_printf(s
, " irq-%d %s%s",
873 irqd_is_wakeup_set(&desc
->irq_data
)
883 #define nmk_gpio_dbg_show NULL
886 /* This structure is replicated for each GPIO block allocated at probe time */
887 static struct gpio_chip nmk_gpio_template
= {
888 .direction_input
= nmk_gpio_make_input
,
889 .get
= nmk_gpio_get_input
,
890 .direction_output
= nmk_gpio_make_output
,
891 .set
= nmk_gpio_set_output
,
892 .to_irq
= nmk_gpio_to_irq
,
893 .dbg_show
= nmk_gpio_dbg_show
,
898 * Called from the suspend/resume path to only keep the real wakeup interrupts
899 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
900 * and not the rest of the interrupts which we needed to have as wakeups for
903 * PM ops are not used since this needs to be done at the end, after all the
904 * other drivers are done with their suspend callbacks.
906 void nmk_gpio_wakeups_suspend(void)
910 for (i
= 0; i
< NUM_BANKS
; i
++) {
911 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
916 chip
->rwimsc
= readl(chip
->addr
+ NMK_GPIO_RWIMSC
);
917 chip
->fwimsc
= readl(chip
->addr
+ NMK_GPIO_FWIMSC
);
919 writel(chip
->rwimsc
& chip
->real_wake
,
920 chip
->addr
+ NMK_GPIO_RWIMSC
);
921 writel(chip
->fwimsc
& chip
->real_wake
,
922 chip
->addr
+ NMK_GPIO_FWIMSC
);
924 if (chip
->sleepmode
) {
925 chip
->slpm
= readl(chip
->addr
+ NMK_GPIO_SLPC
);
927 /* 0 -> wakeup enable */
928 writel(~chip
->real_wake
, chip
->addr
+ NMK_GPIO_SLPC
);
933 void nmk_gpio_wakeups_resume(void)
937 for (i
= 0; i
< NUM_BANKS
; i
++) {
938 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
943 writel(chip
->rwimsc
, chip
->addr
+ NMK_GPIO_RWIMSC
);
944 writel(chip
->fwimsc
, chip
->addr
+ NMK_GPIO_FWIMSC
);
947 writel(chip
->slpm
, chip
->addr
+ NMK_GPIO_SLPC
);
952 * Read the pull up/pull down status.
953 * A bit set in 'pull_up' means that pull up
954 * is selected if pull is enabled in PDIS register.
955 * Note: only pull up/down set via this driver can
956 * be detected due to HW limitations.
958 void nmk_gpio_read_pull(int gpio_bank
, u32
*pull_up
)
960 if (gpio_bank
< NUM_BANKS
) {
961 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[gpio_bank
];
966 *pull_up
= chip
->pull_up
;
970 static int __devinit
nmk_gpio_probe(struct platform_device
*dev
)
972 struct nmk_gpio_platform_data
*pdata
= dev
->dev
.platform_data
;
973 struct nmk_gpio_chip
*nmk_chip
;
974 struct gpio_chip
*chip
;
975 struct resource
*res
;
984 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
990 irq
= platform_get_irq(dev
, 0);
996 secondary_irq
= platform_get_irq(dev
, 1);
997 if (secondary_irq
>= 0 && !pdata
->get_secondary_status
) {
1002 if (request_mem_region(res
->start
, resource_size(res
),
1003 dev_name(&dev
->dev
)) == NULL
) {
1008 clk
= clk_get(&dev
->dev
, NULL
);
1016 nmk_chip
= kzalloc(sizeof(*nmk_chip
), GFP_KERNEL
);
1022 * The virt address in nmk_chip->addr is in the nomadik register space,
1023 * so we can simply convert the resource address, without remapping
1025 nmk_chip
->bank
= dev
->id
;
1026 nmk_chip
->clk
= clk
;
1027 nmk_chip
->addr
= io_p2v(res
->start
);
1028 nmk_chip
->chip
= nmk_gpio_template
;
1029 nmk_chip
->parent_irq
= irq
;
1030 nmk_chip
->secondary_parent_irq
= secondary_irq
;
1031 nmk_chip
->get_secondary_status
= pdata
->get_secondary_status
;
1032 nmk_chip
->set_ioforce
= pdata
->set_ioforce
;
1033 nmk_chip
->sleepmode
= pdata
->supports_sleepmode
;
1034 spin_lock_init(&nmk_chip
->lock
);
1036 chip
= &nmk_chip
->chip
;
1037 chip
->base
= pdata
->first_gpio
;
1038 chip
->ngpio
= pdata
->num_gpio
;
1039 chip
->label
= pdata
->name
?: dev_name(&dev
->dev
);
1040 chip
->dev
= &dev
->dev
;
1041 chip
->owner
= THIS_MODULE
;
1043 ret
= gpiochip_add(&nmk_chip
->chip
);
1047 BUG_ON(nmk_chip
->bank
>= ARRAY_SIZE(nmk_gpio_chips
));
1049 nmk_gpio_chips
[nmk_chip
->bank
] = nmk_chip
;
1050 platform_set_drvdata(dev
, nmk_chip
);
1052 nmk_gpio_init_irq(nmk_chip
);
1054 dev_info(&dev
->dev
, "Bits %i-%i at address %p\n",
1055 nmk_chip
->chip
.base
, nmk_chip
->chip
.base
+31, nmk_chip
->addr
);
1064 release_mem_region(res
->start
, resource_size(res
));
1066 dev_err(&dev
->dev
, "Failure %i for GPIO %i-%i\n", ret
,
1067 pdata
->first_gpio
, pdata
->first_gpio
+31);
1071 static struct platform_driver nmk_gpio_driver
= {
1073 .owner
= THIS_MODULE
,
1076 .probe
= nmk_gpio_probe
,
1079 static int __init
nmk_gpio_init(void)
1081 return platform_driver_register(&nmk_gpio_driver
);
1084 core_initcall(nmk_gpio_init
);
1086 MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1087 MODULE_DESCRIPTION("Nomadik GPIO Driver");
1088 MODULE_LICENSE("GPL");