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>
26 #include <linux/gpio/nomadik.h>
28 #include <mach/hardware.h>
29 #include <mach/gpio.h>
32 * The GPIO module in the Nomadik family of Systems-on-Chip is an
33 * AMBA device, managing 32 pins and alternate functions. The logic block
34 * is currently used in the Nomadik and ux500.
36 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
39 #define NMK_GPIO_PER_CHIP 32
41 /* Register in the logic block */
42 #define NMK_GPIO_DAT 0x00
43 #define NMK_GPIO_DATS 0x04
44 #define NMK_GPIO_DATC 0x08
45 #define NMK_GPIO_PDIS 0x0c
46 #define NMK_GPIO_DIR 0x10
47 #define NMK_GPIO_DIRS 0x14
48 #define NMK_GPIO_DIRC 0x18
49 #define NMK_GPIO_SLPC 0x1c
50 #define NMK_GPIO_AFSLA 0x20
51 #define NMK_GPIO_AFSLB 0x24
53 #define NMK_GPIO_RIMSC 0x40
54 #define NMK_GPIO_FIMSC 0x44
55 #define NMK_GPIO_IS 0x48
56 #define NMK_GPIO_IC 0x4c
57 #define NMK_GPIO_RWIMSC 0x50
58 #define NMK_GPIO_FWIMSC 0x54
59 #define NMK_GPIO_WKS 0x58
61 struct nmk_gpio_chip
{
62 struct gpio_chip chip
;
66 unsigned int parent_irq
;
67 int secondary_parent_irq
;
68 u32 (*get_secondary_status
)(unsigned int bank
);
69 void (*set_ioforce
)(bool enable
);
72 /* Keep track of configured edges */
82 /* FIXME: this is insane since it will create chips for the GPIO expanders! */
83 static struct nmk_gpio_chip
*
84 nmk_gpio_chips
[DIV_ROUND_UP(ARCH_NR_GPIOS
, NMK_GPIO_PER_CHIP
)];
86 static DEFINE_SPINLOCK(nmk_gpio_slpm_lock
);
88 #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
90 static inline struct nmk_gpio_chip
*to_nmk_chip(struct gpio_chip
*chip
)
92 struct nmk_gpio_chip
*nmk_chip
=
93 container_of(chip
, struct nmk_gpio_chip
, chip
);
98 static void __nmk_gpio_set_mode(struct nmk_gpio_chip
*nmk_chip
,
99 unsigned offset
, u16 altfunc
)
101 u32 bit
= 1 << offset
;
103 bool glitch
= (altfunc
== GPIO_CONFIG_NMK_ALTF_C
);
104 u32 rwimsc
= readl(nmk_chip
->addr
+ NMK_GPIO_RWIMSC
);
105 u32 fwimsc
= readl(nmk_chip
->addr
+ NMK_GPIO_FWIMSC
);
106 unsigned long flags
= 0;
110 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
111 * we may pass through an undesired state. In this case we take
114 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
115 * - Save SLPM registers (since we have a shadow register in the
116 * nmk_chip we're using that as backup)
117 * - Set SLPM=0 for the IOs you want to switch and others to 1
118 * - Configure the GPIO registers for the IOs that are being switched
120 * - Modify the AFLSA/B registers for the IOs that are being switched
122 * - Restore SLPM registers
123 * - Any spurious wake up event during switch sequence to be ignored
126 * TODO: Do we REALLY need to save ALL slpm registers?? Isn't it
127 * enough to save the one for the port we're modifying, really?
130 u32 bit
= BIT(offset
);
132 /* Take the special sleep mode spinlock */
133 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
134 for (i
= 0; i
< NUM_BANKS
; i
++) {
135 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
139 clk_enable(chip
->clk
);
140 writel(0xFFFFFFFFU
, chip
->addr
+ NMK_GPIO_SLPC
);
143 /* Set "my" sleep bit to 0 */
144 writel(0xFFFFFFFFU
& ~bit
, nmk_chip
->addr
+ NMK_GPIO_SLPC
);
145 /* Prevent spurious wakeups */
146 writel(rwimsc
& ~bit
, nmk_chip
->addr
+ NMK_GPIO_RWIMSC
);
147 writel(fwimsc
& ~bit
, nmk_chip
->addr
+ NMK_GPIO_FWIMSC
);
148 if (nmk_chip
->set_ioforce
)
149 nmk_chip
->set_ioforce(true);
152 /* Set desired altfunc */
153 afunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLA
) & ~bit
;
154 bfunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLB
) & ~bit
;
155 if ((altfunc
== GPIO_CONFIG_NMK_ALTF_A
) ||
156 (altfunc
== GPIO_CONFIG_NMK_ALTF_C
))
158 if ((altfunc
== GPIO_CONFIG_NMK_ALTF_B
) ||
159 (altfunc
== GPIO_CONFIG_NMK_ALTF_C
))
161 writel(afunc
, nmk_chip
->addr
+ NMK_GPIO_AFSLA
);
162 writel(bfunc
, nmk_chip
->addr
+ NMK_GPIO_AFSLB
);
165 if (nmk_chip
->set_ioforce
)
166 nmk_chip
->set_ioforce(false);
167 writel(rwimsc
, nmk_chip
->addr
+ NMK_GPIO_RWIMSC
);
168 writel(fwimsc
, nmk_chip
->addr
+ NMK_GPIO_FWIMSC
);
170 for (i
= 0; i
< NUM_BANKS
; i
++) {
171 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
175 writel(chip
->slpm
, chip
->addr
+ NMK_GPIO_SLPC
);
176 clk_disable(chip
->clk
);
178 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
182 static int __nmk_gpio_get_mode(struct nmk_gpio_chip
*nmk_chip
,
183 unsigned offset
, u16
*data
)
185 u32 afunc
, bfunc
, bit
;
189 afunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLA
) & bit
;
190 bfunc
= readl(nmk_chip
->addr
+ NMK_GPIO_AFSLB
) & bit
;
193 *data
= GPIO_CONFIG_NMK_ALTF_C
;
195 *data
= GPIO_CONFIG_NMK_ALTF_A
;
197 *data
= GPIO_CONFIG_NMK_ALTF_B
;
199 *data
= GPIO_CONFIG_NMK_ALTF_GPIO
;
204 * __nmk_gpio_set_slpm() - configure the sleep mode of a pin
205 * @offset: pin number
206 * @enable: enable wakeup on the pin on DB8500v2, or force the pin to retain
207 * its value in sleep mode (not become an input) on DB8500v1 if true
209 * Sets the sleep mode of a pin. On DB8500v1, if @enable is
210 * true, the pin is NOT changed to an input (with pullup/down enabled) in
211 * sleep and deep sleep. If @enable is false, the pin remains in the state
212 * it was configured even when in sleep and deep sleep.
214 * On DB8500v2 onwards, this setting loses the previous meaning and instead
215 * indicates if wakeup detection is enabled on the pin. Note that
216 * enable_irq_wake() will automatically enable wakeup detection.
218 static void __nmk_gpio_set_slpm(struct nmk_gpio_chip
*nmk_chip
,
219 unsigned offset
, bool enable
)
221 u32 bit
= 1 << offset
;
224 * NOTE: the meaning of "enable" is inverted, nominally every GPIO
225 * will wake up the system (or convert the pin to an input on DB8500v1)
228 nmk_chip
->slpm
&= ~bit
;
231 * So setting this bit disables wakeups (or makes the pin
232 * retain its value on DB8500v1)
234 nmk_chip
->slpm
|= bit
;
236 writel(nmk_chip
->slpm
, nmk_chip
->addr
+ NMK_GPIO_SLPC
);
239 static void __nmk_gpio_set_pull(struct nmk_gpio_chip
*nmk_chip
,
240 unsigned offset
, u16 pullmode
)
242 u32 bit
= 1 << offset
;
245 /* FIXME: use shadow registers instead of read-modify-write */
246 pdis
= readl(nmk_chip
->addr
+ NMK_GPIO_PDIS
);
247 if ((pullmode
== GPIO_CONFIG_BIAS_UNKNOWN
) ||
248 (pullmode
== GPIO_CONFIG_BIAS_FLOAT
)) {
250 nmk_chip
->pull_up
&= ~bit
;
255 writel(pdis
, nmk_chip
->addr
+ NMK_GPIO_PDIS
);
257 if (pullmode
== GPIO_CONFIG_BIAS_PULL_UP
) {
258 nmk_chip
->pull_up
|= bit
;
259 writel(bit
, nmk_chip
->addr
+ NMK_GPIO_DATS
);
260 } else if (pullmode
== GPIO_CONFIG_BIAS_PULL_DOWN
) {
261 nmk_chip
->pull_up
&= ~bit
;
262 writel(bit
, nmk_chip
->addr
+ NMK_GPIO_DATC
);
266 static void __nmk_gpio_make_input(struct nmk_gpio_chip
*nmk_chip
,
269 writel(1 << offset
, nmk_chip
->addr
+ NMK_GPIO_DIRC
);
272 static void __nmk_gpio_set_output(struct nmk_gpio_chip
*nmk_chip
,
273 unsigned offset
, int val
)
276 writel(1 << offset
, nmk_chip
->addr
+ NMK_GPIO_DATS
);
278 writel(1 << offset
, nmk_chip
->addr
+ NMK_GPIO_DATC
);
281 static void __nmk_gpio_make_output(struct nmk_gpio_chip
*nmk_chip
,
282 unsigned offset
, int val
)
284 writel(1 << offset
, nmk_chip
->addr
+ NMK_GPIO_DIRS
);
285 __nmk_gpio_set_output(nmk_chip
, offset
, val
);
289 static void __nmk_setup_pin(struct nmk_gpio_chip
*nmk_chip
, unsigned offset
,
290 struct nmk_gpio_pin_config
*cfg
)
292 static const char *afnames
[] = {
293 [GPIO_CONFIG_NMK_ALTF_GPIO
] = "GPIO",
294 [GPIO_CONFIG_NMK_ALTF_A
] = "A",
295 [GPIO_CONFIG_NMK_ALTF_B
] = "B",
296 [GPIO_CONFIG_NMK_ALTF_C
] = "C"
298 static const char *biasnames
[] = {
299 [GPIO_CONFIG_BIAS_UNKNOWN
] = "none",
300 [GPIO_CONFIG_BIAS_FLOAT
] = "none",
301 [GPIO_CONFIG_BIAS_PULL_UP
] = "up",
302 [GPIO_CONFIG_BIAS_PULL_DOWN
] = "down",
305 dev_dbg(nmk_chip
->chip
.dev
, "pin %d: af: %s, bias: %s, "
307 cfg
->pin
, afnames
[cfg
->altfunc
],
308 biasnames
[cfg
->bias_mode
],
309 cfg
->sleep_mode
? "input/wakeup" : "no-change/no-wakeup",
310 cfg
->output
? "output " : "input",
311 cfg
->output
? (cfg
->outval
? "high" : "low") : "");
314 __nmk_gpio_make_output(nmk_chip
, offset
, cfg
->outval
);
316 __nmk_gpio_make_input(nmk_chip
, offset
);
317 __nmk_gpio_set_pull(nmk_chip
, offset
, cfg
->bias_mode
);
319 __nmk_gpio_set_slpm(nmk_chip
, offset
, cfg
->sleep_mode
);
320 __nmk_gpio_set_mode(nmk_chip
, offset
, cfg
->altfunc
);
324 * This translates the old pin config parameter type into a config
325 * struct and passes to the new API
327 static void __nmk_setup_pin_legacy(struct nmk_gpio_chip
*nmk_chip
,
331 struct nmk_gpio_pin_config cfg
;
333 cfg
.pin
= PIN_NUM(pincfg
);
335 switch (PIN_ALT(pincfg
)) {
336 case NMK_GPIO_ALT_GPIO
:
337 cfg
.altfunc
= GPIO_CONFIG_NMK_ALTF_GPIO
;
340 cfg
.altfunc
= GPIO_CONFIG_NMK_ALTF_A
;
343 cfg
.altfunc
= GPIO_CONFIG_NMK_ALTF_B
;
346 cfg
.altfunc
= GPIO_CONFIG_NMK_ALTF_C
;
349 cfg
.altfunc
= GPIO_CONFIG_NMK_ALTF_GPIO
;
359 switch (PIN_PULL(pincfg
)) {
360 case NMK_GPIO_PULL_NONE
:
361 cfg
.bias_mode
= GPIO_CONFIG_BIAS_FLOAT
;
363 case NMK_GPIO_PULL_UP
:
364 cfg
.bias_mode
= GPIO_CONFIG_BIAS_PULL_UP
;
366 case NMK_GPIO_PULL_DOWN
:
367 cfg
.bias_mode
= GPIO_CONFIG_BIAS_PULL_DOWN
;
370 cfg
.bias_mode
= GPIO_CONFIG_BIAS_FLOAT
;
374 switch (PIN_SLPM(pincfg
)) {
375 case NMK_GPIO_SLPM_WAKEUP_ENABLE
:
376 /* Also NMK_GPIO_SLPM_INPUT */
378 case NMK_GPIO_SLPM_WAKEUP_DISABLE
:
379 /* Also NMK_GPIO_SLPM_NOCHANGE */
380 cfg
.sleep_mode
= true;
385 __nmk_setup_pin(nmk_chip
, offset
, &cfg
);
388 static int nmk_gpio_config(struct gpio_chip
*chip
, unsigned offset
,
389 u16 param
, unsigned long *data
)
391 struct nmk_gpio_chip
*nmk_chip
= to_nmk_chip(chip
);
397 clk_enable(nmk_chip
->clk
);
400 case GPIO_CONFIG_BIAS_UNKNOWN
:
401 case GPIO_CONFIG_BIAS_FLOAT
:
402 case GPIO_CONFIG_BIAS_PULL_UP
:
403 case GPIO_CONFIG_BIAS_PULL_DOWN
:
404 __nmk_gpio_set_pull(nmk_chip
, offset
, param
);
406 case GPIO_CONFIG_NMK_ALTF_GPIO
:
407 case GPIO_CONFIG_NMK_ALTF_A
:
408 case GPIO_CONFIG_NMK_ALTF_B
:
409 case GPIO_CONFIG_NMK_ALTF_C
:
410 __nmk_gpio_set_mode(nmk_chip
, offset
, param
);
412 case GPIO_CONFIG_NMK_GET_ALTF
:
413 __nmk_gpio_get_mode(nmk_chip
, offset
, (u16
*) data
);
415 case GPIO_CONFIG_NMK_SLPM_INPUT
:
416 case GPIO_CONFIG_NMK_WAKEUP_ENABLE
:
417 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
418 spin_lock(&nmk_chip
->lock
);
419 __nmk_gpio_set_slpm(nmk_chip
, offset
, true);
420 spin_unlock(&nmk_chip
->lock
);
421 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
423 case GPIO_CONFIG_NMK_SLPM_NOCHANGE
:
424 case GPIO_CONFIG_NMK_WAKEUP_DISABLE
:
425 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
426 spin_lock(&nmk_chip
->lock
);
427 __nmk_gpio_set_slpm(nmk_chip
, offset
, false);
428 spin_unlock(&nmk_chip
->lock
);
429 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
431 case GPIO_CONFIG_NMK_SETUP_PIN
:
432 __nmk_setup_pin(nmk_chip
, offset
,
433 (struct nmk_gpio_pin_config
*) data
);
435 case GPIO_CONFIG_NMK_SETUP_PIN_LEGACY
:
436 __nmk_setup_pin_legacy(nmk_chip
, offset
, (pin_cfg_t
) *data
);
439 dev_err(nmk_chip
->chip
.dev
,
440 "illegal configuration requested\n");
441 clk_disable(nmk_chip
->clk
);
445 clk_disable(nmk_chip
->clk
);
453 static inline int nmk_gpio_get_bitmask(int gpio
)
455 return 1 << (gpio
% 32);
458 static void nmk_gpio_irq_ack(struct irq_data
*d
)
461 struct nmk_gpio_chip
*nmk_chip
;
463 gpio
= NOMADIK_IRQ_TO_GPIO(d
->irq
);
464 nmk_chip
= irq_data_get_irq_chip_data(d
);
468 clk_enable(nmk_chip
->clk
);
469 writel(nmk_gpio_get_bitmask(gpio
), nmk_chip
->addr
+ NMK_GPIO_IC
);
470 clk_disable(nmk_chip
->clk
);
473 enum nmk_gpio_irq_type
{
478 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip
*nmk_chip
,
479 int gpio
, enum nmk_gpio_irq_type which
,
482 u32 rimsc
= which
== WAKE
? NMK_GPIO_RWIMSC
: NMK_GPIO_RIMSC
;
483 u32 fimsc
= which
== WAKE
? NMK_GPIO_FWIMSC
: NMK_GPIO_FIMSC
;
484 u32 bitmask
= nmk_gpio_get_bitmask(gpio
);
487 /* we must individually set/clear the two edges */
488 if (nmk_chip
->edge_rising
& bitmask
) {
489 reg
= readl(nmk_chip
->addr
+ rimsc
);
494 writel(reg
, nmk_chip
->addr
+ rimsc
);
496 if (nmk_chip
->edge_falling
& bitmask
) {
497 reg
= readl(nmk_chip
->addr
+ fimsc
);
502 writel(reg
, nmk_chip
->addr
+ fimsc
);
506 static void __nmk_gpio_set_wake(struct nmk_gpio_chip
*nmk_chip
,
509 if (nmk_chip
->sleepmode
)
510 __nmk_gpio_set_slpm(nmk_chip
, gpio
- nmk_chip
->chip
.base
, on
);
512 __nmk_gpio_irq_modify(nmk_chip
, gpio
, WAKE
, on
);
515 static int nmk_gpio_irq_maskunmask(struct irq_data
*d
, bool enable
)
518 struct nmk_gpio_chip
*nmk_chip
;
522 gpio
= NOMADIK_IRQ_TO_GPIO(d
->irq
);
523 nmk_chip
= irq_data_get_irq_chip_data(d
);
524 bitmask
= nmk_gpio_get_bitmask(gpio
);
528 clk_enable(nmk_chip
->clk
);
529 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
530 spin_lock(&nmk_chip
->lock
);
532 __nmk_gpio_irq_modify(nmk_chip
, gpio
, NORMAL
, enable
);
534 if (!(nmk_chip
->real_wake
& bitmask
))
535 __nmk_gpio_set_wake(nmk_chip
, gpio
, enable
);
537 spin_unlock(&nmk_chip
->lock
);
538 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
539 clk_disable(nmk_chip
->clk
);
544 static void nmk_gpio_irq_mask(struct irq_data
*d
)
546 nmk_gpio_irq_maskunmask(d
, false);
549 static void nmk_gpio_irq_unmask(struct irq_data
*d
)
551 nmk_gpio_irq_maskunmask(d
, true);
554 static int nmk_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
556 struct nmk_gpio_chip
*nmk_chip
;
561 gpio
= NOMADIK_IRQ_TO_GPIO(d
->irq
);
562 nmk_chip
= irq_data_get_irq_chip_data(d
);
565 bitmask
= nmk_gpio_get_bitmask(gpio
);
567 clk_enable(nmk_chip
->clk
);
568 spin_lock_irqsave(&nmk_gpio_slpm_lock
, flags
);
569 spin_lock(&nmk_chip
->lock
);
571 /* Make sure we wake up on this pin */
572 if (irqd_irq_disabled(d
))
573 __nmk_gpio_set_wake(nmk_chip
, gpio
, on
);
576 nmk_chip
->real_wake
|= bitmask
;
578 nmk_chip
->real_wake
&= ~bitmask
;
580 spin_unlock(&nmk_chip
->lock
);
581 spin_unlock_irqrestore(&nmk_gpio_slpm_lock
, flags
);
582 clk_disable(nmk_chip
->clk
);
587 static int nmk_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
589 bool enabled
= !irqd_irq_disabled(d
);
590 bool wake
= irqd_is_wakeup_set(d
);
592 struct nmk_gpio_chip
*nmk_chip
;
596 gpio
= NOMADIK_IRQ_TO_GPIO(d
->irq
);
597 nmk_chip
= irq_data_get_irq_chip_data(d
);
598 bitmask
= nmk_gpio_get_bitmask(gpio
);
602 if (type
& IRQ_TYPE_LEVEL_HIGH
)
604 if (type
& IRQ_TYPE_LEVEL_LOW
)
607 clk_enable(nmk_chip
->clk
);
608 spin_lock_irqsave(&nmk_chip
->lock
, flags
);
611 __nmk_gpio_irq_modify(nmk_chip
, gpio
, NORMAL
, false);
614 __nmk_gpio_irq_modify(nmk_chip
, gpio
, WAKE
, false);
616 nmk_chip
->edge_rising
&= ~bitmask
;
617 if (type
& IRQ_TYPE_EDGE_RISING
)
618 nmk_chip
->edge_rising
|= bitmask
;
620 nmk_chip
->edge_falling
&= ~bitmask
;
621 if (type
& IRQ_TYPE_EDGE_FALLING
)
622 nmk_chip
->edge_falling
|= bitmask
;
625 __nmk_gpio_irq_modify(nmk_chip
, gpio
, NORMAL
, true);
628 __nmk_gpio_irq_modify(nmk_chip
, gpio
, WAKE
, true);
630 spin_unlock_irqrestore(&nmk_chip
->lock
, flags
);
631 clk_disable(nmk_chip
->clk
);
636 static unsigned int nmk_gpio_irq_startup(struct irq_data
*d
)
638 struct nmk_gpio_chip
*nmk_chip
= irq_data_get_irq_chip_data(d
);
640 clk_enable(nmk_chip
->clk
);
641 nmk_gpio_irq_unmask(d
);
645 static void nmk_gpio_irq_shutdown(struct irq_data
*d
)
647 struct nmk_gpio_chip
*nmk_chip
= irq_data_get_irq_chip_data(d
);
649 nmk_gpio_irq_mask(d
);
650 clk_disable(nmk_chip
->clk
);
653 static struct irq_chip nmk_gpio_irq_chip
= {
654 .name
= "Nomadik-GPIO",
655 .irq_ack
= nmk_gpio_irq_ack
,
656 .irq_mask
= nmk_gpio_irq_mask
,
657 .irq_unmask
= nmk_gpio_irq_unmask
,
658 .irq_set_type
= nmk_gpio_irq_set_type
,
659 .irq_set_wake
= nmk_gpio_irq_set_wake
,
660 .irq_startup
= nmk_gpio_irq_startup
,
661 .irq_shutdown
= nmk_gpio_irq_shutdown
,
664 static void __nmk_gpio_irq_handler(unsigned int irq
, struct irq_desc
*desc
,
667 struct nmk_gpio_chip
*nmk_chip
;
668 struct irq_chip
*host_chip
= irq_get_chip(irq
);
669 unsigned int first_irq
;
671 if (host_chip
->irq_mask_ack
)
672 host_chip
->irq_mask_ack(&desc
->irq_data
);
674 host_chip
->irq_mask(&desc
->irq_data
);
675 if (host_chip
->irq_ack
)
676 host_chip
->irq_ack(&desc
->irq_data
);
679 nmk_chip
= irq_get_handler_data(irq
);
680 first_irq
= NOMADIK_GPIO_TO_IRQ(nmk_chip
->chip
.base
);
682 int bit
= __ffs(status
);
684 generic_handle_irq(first_irq
+ bit
);
688 host_chip
->irq_unmask(&desc
->irq_data
);
691 static void nmk_gpio_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
693 struct nmk_gpio_chip
*nmk_chip
= irq_get_handler_data(irq
);
696 clk_enable(nmk_chip
->clk
);
697 status
= readl(nmk_chip
->addr
+ NMK_GPIO_IS
);
698 clk_disable(nmk_chip
->clk
);
700 __nmk_gpio_irq_handler(irq
, desc
, status
);
703 static void nmk_gpio_secondary_irq_handler(unsigned int irq
,
704 struct irq_desc
*desc
)
706 struct nmk_gpio_chip
*nmk_chip
= irq_get_handler_data(irq
);
707 u32 status
= nmk_chip
->get_secondary_status(nmk_chip
->bank
);
709 __nmk_gpio_irq_handler(irq
, desc
, status
);
712 static int nmk_gpio_init_irq(struct nmk_gpio_chip
*nmk_chip
)
714 unsigned int first_irq
;
717 first_irq
= NOMADIK_GPIO_TO_IRQ(nmk_chip
->chip
.base
);
718 for (i
= first_irq
; i
< first_irq
+ nmk_chip
->chip
.ngpio
; i
++) {
719 irq_set_chip_and_handler(i
, &nmk_gpio_irq_chip
,
721 set_irq_flags(i
, IRQF_VALID
);
722 irq_set_chip_data(i
, nmk_chip
);
723 irq_set_irq_type(i
, IRQ_TYPE_EDGE_FALLING
);
726 irq_set_chained_handler(nmk_chip
->parent_irq
, nmk_gpio_irq_handler
);
727 irq_set_handler_data(nmk_chip
->parent_irq
, nmk_chip
);
729 if (nmk_chip
->secondary_parent_irq
>= 0) {
730 irq_set_chained_handler(nmk_chip
->secondary_parent_irq
,
731 nmk_gpio_secondary_irq_handler
);
732 irq_set_handler_data(nmk_chip
->secondary_parent_irq
, nmk_chip
);
739 static int nmk_gpio_make_input(struct gpio_chip
*chip
, unsigned offset
)
741 struct nmk_gpio_chip
*nmk_chip
= to_nmk_chip(chip
);
743 clk_enable(nmk_chip
->clk
);
745 writel(1 << offset
, nmk_chip
->addr
+ NMK_GPIO_DIRC
);
747 clk_disable(nmk_chip
->clk
);
752 static int nmk_gpio_get_input(struct gpio_chip
*chip
, unsigned offset
)
754 struct nmk_gpio_chip
*nmk_chip
= to_nmk_chip(chip
);
755 u32 bit
= 1 << offset
;
758 clk_enable(nmk_chip
->clk
);
760 value
= (readl(nmk_chip
->addr
+ NMK_GPIO_DAT
) & bit
) != 0;
762 clk_disable(nmk_chip
->clk
);
767 static void nmk_gpio_set_output(struct gpio_chip
*chip
, unsigned offset
,
770 struct nmk_gpio_chip
*nmk_chip
= to_nmk_chip(chip
);
772 clk_enable(nmk_chip
->clk
);
774 __nmk_gpio_set_output(nmk_chip
, offset
, val
);
776 clk_disable(nmk_chip
->clk
);
779 static int nmk_gpio_make_output(struct gpio_chip
*chip
, unsigned offset
,
782 struct nmk_gpio_chip
*nmk_chip
= to_nmk_chip(chip
);
784 clk_enable(nmk_chip
->clk
);
786 __nmk_gpio_make_output(nmk_chip
, offset
, val
);
788 clk_disable(nmk_chip
->clk
);
793 static int nmk_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
795 struct nmk_gpio_chip
*nmk_chip
= to_nmk_chip(chip
);
797 return NOMADIK_GPIO_TO_IRQ(nmk_chip
->chip
.base
) + offset
;
800 #ifdef CONFIG_DEBUG_FS
802 #include <linux/seq_file.h>
804 static void nmk_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
808 unsigned gpio
= chip
->base
;
810 struct nmk_gpio_chip
*nmk_chip
= to_nmk_chip(chip
);
812 clk_enable(nmk_chip
->clk
);
814 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++) {
815 const char *label
= gpiochip_is_requested(chip
, i
);
820 is_out
= readl(nmk_chip
->addr
+ NMK_GPIO_DIR
) & bit
;
821 pull
= !(readl(nmk_chip
->addr
+ NMK_GPIO_PDIS
) & bit
);
822 __nmk_gpio_get_mode(nmk_chip
, gpio
, &mode
);
824 case GPIO_CONFIG_NMK_ALTF_GPIO
:
827 case GPIO_CONFIG_NMK_ALTF_A
:
830 case GPIO_CONFIG_NMK_ALTF_B
:
833 case GPIO_CONFIG_NMK_ALTF_C
:
840 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s %s %s",
841 gpio
, label
?: "(none)",
842 is_out
? "out" : "in ",
844 ? (chip
->get(chip
, i
) ? "hi" : "lo")
846 (mode
< 0) ? "unknown" : modetxt
,
847 pull
? "pull" : "none");
849 if (label
&& !is_out
) {
850 int irq
= gpio_to_irq(gpio
);
851 struct irq_desc
*desc
= irq_to_desc(irq
);
853 /* This races with request_irq(), set_irq_type(),
854 * and set_irq_wake() ... but those are "rare".
856 if (irq
>= 0 && desc
->action
) {
858 u32 bitmask
= nmk_gpio_get_bitmask(gpio
);
860 if (nmk_chip
->edge_rising
& bitmask
)
861 trigger
= "edge-rising";
862 else if (nmk_chip
->edge_falling
& bitmask
)
863 trigger
= "edge-falling";
865 trigger
= "edge-undefined";
867 seq_printf(s
, " irq-%d %s%s",
869 irqd_is_wakeup_set(&desc
->irq_data
)
877 clk_disable(nmk_chip
->clk
);
881 #define nmk_gpio_dbg_show NULL
884 /* This structure is replicated for each GPIO block allocated at probe time */
885 static struct gpio_chip nmk_gpio_chip
= {
886 .direction_input
= nmk_gpio_make_input
,
887 .get
= nmk_gpio_get_input
,
888 .direction_output
= nmk_gpio_make_output
,
889 .set
= nmk_gpio_set_output
,
890 .config
= nmk_gpio_config
,
891 .to_irq
= nmk_gpio_to_irq
,
892 .dbg_show
= nmk_gpio_dbg_show
,
897 * nmk_gpio_clocks_enable() - enable the clocks on all GPIO blocks
899 void nmk_gpio_clocks_enable(void)
903 for (i
= 0; i
< NUM_BANKS
; i
++) {
904 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
909 clk_enable(chip
->clk
);
914 * nmk_gpio_clocks_disable() - disable the clocks on all GPIO blocks
916 void nmk_gpio_clocks_disable(void)
920 for (i
= 0; i
< NUM_BANKS
; i
++) {
921 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
926 clk_disable(chip
->clk
);
931 * Called from the suspend/resume path to only keep the real wakeup interrupts
932 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
933 * and not the rest of the interrupts which we needed to have as wakeups for
936 * PM ops are not used since this needs to be done at the end, after all the
937 * other drivers are done with their suspend callbacks.
939 void nmk_gpio_wakeups_suspend(void)
943 for (i
= 0; i
< NUM_BANKS
; i
++) {
944 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
949 clk_enable(chip
->clk
);
951 chip
->rwimsc
= readl(chip
->addr
+ NMK_GPIO_RWIMSC
);
952 chip
->fwimsc
= readl(chip
->addr
+ NMK_GPIO_FWIMSC
);
954 writel(chip
->rwimsc
& chip
->real_wake
,
955 chip
->addr
+ NMK_GPIO_RWIMSC
);
956 writel(chip
->fwimsc
& chip
->real_wake
,
957 chip
->addr
+ NMK_GPIO_FWIMSC
);
959 if (chip
->sleepmode
) {
960 chip
->slpm
= readl(chip
->addr
+ NMK_GPIO_SLPC
);
962 /* 0 -> wakeup enable */
963 writel(~chip
->real_wake
, chip
->addr
+ NMK_GPIO_SLPC
);
966 clk_disable(chip
->clk
);
970 void nmk_gpio_wakeups_resume(void)
974 for (i
= 0; i
< NUM_BANKS
; i
++) {
975 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[i
];
980 clk_enable(chip
->clk
);
982 writel(chip
->rwimsc
, chip
->addr
+ NMK_GPIO_RWIMSC
);
983 writel(chip
->fwimsc
, chip
->addr
+ NMK_GPIO_FWIMSC
);
986 writel(chip
->slpm
, chip
->addr
+ NMK_GPIO_SLPC
);
988 clk_disable(chip
->clk
);
993 * Read the pull up/pull down status.
994 * A bit set in 'pull_up' means that pull up
995 * is selected if pull is enabled in PDIS register.
996 * Note: only pull up/down set via this driver can
997 * be detected due to HW limitations.
999 void nmk_gpio_read_pull(int gpio_bank
, u32
*pull_up
)
1001 if (gpio_bank
< NUM_BANKS
) {
1002 struct nmk_gpio_chip
*chip
= nmk_gpio_chips
[gpio_bank
];
1007 *pull_up
= chip
->pull_up
;
1011 static int __devinit
nmk_gpio_probe(struct platform_device
*dev
)
1013 struct nmk_gpio_platform_data
*pdata
= dev
->dev
.platform_data
;
1014 struct nmk_gpio_chip
*nmk_chip
;
1015 struct gpio_chip
*chip
;
1016 struct resource
*res
;
1025 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
1031 irq
= platform_get_irq(dev
, 0);
1037 secondary_irq
= platform_get_irq(dev
, 1);
1038 if (secondary_irq
>= 0 && !pdata
->get_secondary_status
) {
1043 if (request_mem_region(res
->start
, resource_size(res
),
1044 dev_name(&dev
->dev
)) == NULL
) {
1049 clk
= clk_get(&dev
->dev
, NULL
);
1055 nmk_chip
= kzalloc(sizeof(*nmk_chip
), GFP_KERNEL
);
1061 * The virt address in nmk_chip->addr is in the nomadik register space,
1062 * so we can simply convert the resource address, without remapping
1064 nmk_chip
->bank
= dev
->id
;
1065 nmk_chip
->clk
= clk
;
1066 nmk_chip
->addr
= io_p2v(res
->start
);
1067 nmk_chip
->chip
= nmk_gpio_chip
;
1068 nmk_chip
->parent_irq
= irq
;
1069 nmk_chip
->secondary_parent_irq
= secondary_irq
;
1070 nmk_chip
->get_secondary_status
= pdata
->get_secondary_status
;
1071 nmk_chip
->set_ioforce
= pdata
->set_ioforce
;
1072 nmk_chip
->sleepmode
= pdata
->supports_sleepmode
;
1073 spin_lock_init(&nmk_chip
->lock
);
1075 chip
= &nmk_chip
->chip
;
1076 chip
->base
= pdata
->first_gpio
;
1077 chip
->ngpio
= pdata
->num_gpio
;
1078 chip
->label
= pdata
->name
?: dev_name(&dev
->dev
);
1079 chip
->dev
= &dev
->dev
;
1080 chip
->owner
= THIS_MODULE
;
1082 ret
= gpiochip_add(&nmk_chip
->chip
);
1086 BUG_ON(nmk_chip
->bank
>= ARRAY_SIZE(nmk_gpio_chips
));
1088 nmk_gpio_chips
[nmk_chip
->bank
] = nmk_chip
;
1089 platform_set_drvdata(dev
, nmk_chip
);
1091 nmk_gpio_init_irq(nmk_chip
);
1093 dev_info(&dev
->dev
, "Bits %i-%i at address %p\n",
1094 nmk_chip
->chip
.base
, nmk_chip
->chip
.base
+31, nmk_chip
->addr
);
1103 release_mem_region(res
->start
, resource_size(res
));
1105 dev_err(&dev
->dev
, "Failure %i for GPIO %i-%i\n", ret
,
1106 pdata
->first_gpio
, pdata
->first_gpio
+31);
1110 static struct platform_driver nmk_gpio_driver
= {
1112 .owner
= THIS_MODULE
,
1115 .probe
= nmk_gpio_probe
,
1118 static int __init
nmk_gpio_init(void)
1120 return platform_driver_register(&nmk_gpio_driver
);
1123 core_initcall(nmk_gpio_init
);
1125 MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1126 MODULE_DESCRIPTION("Nomadik GPIO Driver");
1127 MODULE_LICENSE("GPL");