2 * linux/arch/arm/plat-pxa/gpio.c
4 * Generic PXA GPIO handling
6 * Author: Nicolas Pitre
7 * Created: Jun 15, 2001
8 * Copyright: MontaVista Software Inc.
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.
14 #include <linux/module.h>
15 #include <linux/clk.h>
16 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/gpio-pxa.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/irqdomain.h>
23 #include <linux/irqchip/chained_irq.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/syscore_ops.h>
29 #include <linux/slab.h>
32 * We handle the GPIOs by banks, each bank covers up to 32 GPIOs with
33 * one set of registers. The register offsets are organized below:
35 * GPLR GPDR GPSR GPCR GRER GFER GEDR
36 * BANK 0 - 0x0000 0x000C 0x0018 0x0024 0x0030 0x003C 0x0048
37 * BANK 1 - 0x0004 0x0010 0x001C 0x0028 0x0034 0x0040 0x004C
38 * BANK 2 - 0x0008 0x0014 0x0020 0x002C 0x0038 0x0044 0x0050
40 * BANK 3 - 0x0100 0x010C 0x0118 0x0124 0x0130 0x013C 0x0148
41 * BANK 4 - 0x0104 0x0110 0x011C 0x0128 0x0134 0x0140 0x014C
42 * BANK 5 - 0x0108 0x0114 0x0120 0x012C 0x0138 0x0144 0x0150
44 * BANK 6 - 0x0200 0x020C 0x0218 0x0224 0x0230 0x023C 0x0248
47 * BANK 3 is only available on PXA27x and later processors.
48 * BANK 4 and 5 are only available on PXA935, PXA1928
49 * BANK 6 is only available on PXA1928
52 #define GPLR_OFFSET 0x00
53 #define GPDR_OFFSET 0x0C
54 #define GPSR_OFFSET 0x18
55 #define GPCR_OFFSET 0x24
56 #define GRER_OFFSET 0x30
57 #define GFER_OFFSET 0x3C
58 #define GEDR_OFFSET 0x48
59 #define GAFR_OFFSET 0x54
60 #define ED_MASK_OFFSET 0x9C /* GPIO edge detection for AP side */
62 #define BANK_OFF(n) (((n) / 3) << 8) + (((n) % 3) << 2)
68 static struct irq_domain
*domain
;
69 static struct device_node
*pxa_gpio_of_node
;
72 struct pxa_gpio_chip
{
73 struct gpio_chip chip
;
74 void __iomem
*regbase
;
77 unsigned long irq_mask
;
78 unsigned long irq_edge_rise
;
79 unsigned long irq_edge_fall
;
80 int (*set_wake
)(unsigned int gpio
, unsigned int on
);
83 unsigned long saved_gplr
;
84 unsigned long saved_gpdr
;
85 unsigned long saved_grer
;
86 unsigned long saved_gfer
;
102 enum pxa_gpio_type type
;
106 static DEFINE_SPINLOCK(gpio_lock
);
107 static struct pxa_gpio_chip
*pxa_gpio_chips
;
108 static enum pxa_gpio_type gpio_type
;
109 static void __iomem
*gpio_reg_base
;
111 static struct pxa_gpio_id pxa25x_id
= {
116 static struct pxa_gpio_id pxa26x_id
= {
121 static struct pxa_gpio_id pxa27x_id
= {
126 static struct pxa_gpio_id pxa3xx_id
= {
131 static struct pxa_gpio_id pxa93x_id
= {
136 static struct pxa_gpio_id mmp_id
= {
141 static struct pxa_gpio_id mmp2_id
= {
146 static struct pxa_gpio_id pxa1928_id
= {
147 .type
= PXA1928_GPIO
,
151 #define for_each_gpio_chip(i, c) \
152 for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++)
154 static inline void __iomem
*gpio_chip_base(struct gpio_chip
*c
)
156 return container_of(c
, struct pxa_gpio_chip
, chip
)->regbase
;
159 static inline struct pxa_gpio_chip
*gpio_to_pxachip(unsigned gpio
)
161 return &pxa_gpio_chips
[gpio_to_bank(gpio
)];
164 static inline int gpio_is_pxa_type(int type
)
166 return (type
& MMP_GPIO
) == 0;
169 static inline int gpio_is_mmp_type(int type
)
171 return (type
& MMP_GPIO
) != 0;
174 /* GPIO86/87/88/89 on PXA26x have their direction bits in PXA_GPDR(2 inverted,
175 * as well as their Alternate Function value being '1' for GPIO in GAFRx.
177 static inline int __gpio_is_inverted(int gpio
)
179 if ((gpio_type
== PXA26X_GPIO
) && (gpio
> 85))
185 * On PXA25x and PXA27x, GAFRx and GPDRx together decide the alternate
186 * function of a GPIO, and GPDRx cannot be altered once configured. It
187 * is attributed as "occupied" here (I know this terminology isn't
188 * accurate, you are welcome to propose a better one :-)
190 static inline int __gpio_is_occupied(unsigned gpio
)
192 struct pxa_gpio_chip
*pxachip
;
194 unsigned long gafr
= 0, gpdr
= 0;
195 int ret
, af
= 0, dir
= 0;
197 pxachip
= gpio_to_pxachip(gpio
);
198 base
= gpio_chip_base(&pxachip
->chip
);
199 gpdr
= readl_relaxed(base
+ GPDR_OFFSET
);
205 gafr
= readl_relaxed(base
+ GAFR_OFFSET
);
206 af
= (gafr
>> ((gpio
& 0xf) * 2)) & 0x3;
207 dir
= gpdr
& GPIO_bit(gpio
);
209 if (__gpio_is_inverted(gpio
))
210 ret
= (af
!= 1) || (dir
== 0);
212 ret
= (af
!= 0) || (dir
!= 0);
215 ret
= gpdr
& GPIO_bit(gpio
);
221 static int pxa_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
223 return chip
->base
+ offset
+ irq_base
;
226 int pxa_irq_to_gpio(int irq
)
228 return irq
- irq_base
;
231 static int pxa_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
233 void __iomem
*base
= gpio_chip_base(chip
);
234 uint32_t value
, mask
= 1 << offset
;
237 spin_lock_irqsave(&gpio_lock
, flags
);
239 value
= readl_relaxed(base
+ GPDR_OFFSET
);
240 if (__gpio_is_inverted(chip
->base
+ offset
))
244 writel_relaxed(value
, base
+ GPDR_OFFSET
);
246 spin_unlock_irqrestore(&gpio_lock
, flags
);
250 static int pxa_gpio_direction_output(struct gpio_chip
*chip
,
251 unsigned offset
, int value
)
253 void __iomem
*base
= gpio_chip_base(chip
);
254 uint32_t tmp
, mask
= 1 << offset
;
257 writel_relaxed(mask
, base
+ (value
? GPSR_OFFSET
: GPCR_OFFSET
));
259 spin_lock_irqsave(&gpio_lock
, flags
);
261 tmp
= readl_relaxed(base
+ GPDR_OFFSET
);
262 if (__gpio_is_inverted(chip
->base
+ offset
))
266 writel_relaxed(tmp
, base
+ GPDR_OFFSET
);
268 spin_unlock_irqrestore(&gpio_lock
, flags
);
272 static int pxa_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
274 u32 gplr
= readl_relaxed(gpio_chip_base(chip
) + GPLR_OFFSET
);
275 return !!(gplr
& (1 << offset
));
278 static void pxa_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
280 writel_relaxed(1 << offset
, gpio_chip_base(chip
) +
281 (value
? GPSR_OFFSET
: GPCR_OFFSET
));
284 #ifdef CONFIG_OF_GPIO
285 static int pxa_gpio_of_xlate(struct gpio_chip
*gc
,
286 const struct of_phandle_args
*gpiospec
,
289 if (gpiospec
->args
[0] > pxa_last_gpio
)
292 if (gc
!= &pxa_gpio_chips
[gpiospec
->args
[0] / 32].chip
)
296 *flags
= gpiospec
->args
[1];
298 return gpiospec
->args
[0] % 32;
302 static int pxa_init_gpio_chip(int gpio_end
,
303 int (*set_wake
)(unsigned int, unsigned int))
305 int i
, gpio
, nbanks
= gpio_to_bank(gpio_end
) + 1;
306 struct pxa_gpio_chip
*chips
;
308 chips
= kzalloc(nbanks
* sizeof(struct pxa_gpio_chip
), GFP_KERNEL
);
310 pr_err("%s: failed to allocate GPIO chips\n", __func__
);
314 for (i
= 0, gpio
= 0; i
< nbanks
; i
++, gpio
+= 32) {
315 struct gpio_chip
*c
= &chips
[i
].chip
;
317 sprintf(chips
[i
].label
, "gpio-%d", i
);
318 chips
[i
].regbase
= gpio_reg_base
+ BANK_OFF(i
);
319 chips
[i
].set_wake
= set_wake
;
322 c
->label
= chips
[i
].label
;
324 c
->direction_input
= pxa_gpio_direction_input
;
325 c
->direction_output
= pxa_gpio_direction_output
;
326 c
->get
= pxa_gpio_get
;
327 c
->set
= pxa_gpio_set
;
328 c
->to_irq
= pxa_gpio_to_irq
;
329 #ifdef CONFIG_OF_GPIO
330 c
->of_node
= pxa_gpio_of_node
;
331 c
->of_xlate
= pxa_gpio_of_xlate
;
332 c
->of_gpio_n_cells
= 2;
335 /* number of GPIOs on last bank may be less than 32 */
336 c
->ngpio
= (gpio
+ 31 > gpio_end
) ? (gpio_end
- gpio
+ 1) : 32;
339 pxa_gpio_chips
= chips
;
343 /* Update only those GRERx and GFERx edge detection register bits if those
344 * bits are set in c->irq_mask
346 static inline void update_edge_detect(struct pxa_gpio_chip
*c
)
350 grer
= readl_relaxed(c
->regbase
+ GRER_OFFSET
) & ~c
->irq_mask
;
351 gfer
= readl_relaxed(c
->regbase
+ GFER_OFFSET
) & ~c
->irq_mask
;
352 grer
|= c
->irq_edge_rise
& c
->irq_mask
;
353 gfer
|= c
->irq_edge_fall
& c
->irq_mask
;
354 writel_relaxed(grer
, c
->regbase
+ GRER_OFFSET
);
355 writel_relaxed(gfer
, c
->regbase
+ GFER_OFFSET
);
358 static int pxa_gpio_irq_type(struct irq_data
*d
, unsigned int type
)
360 struct pxa_gpio_chip
*c
;
361 int gpio
= pxa_irq_to_gpio(d
->irq
);
362 unsigned long gpdr
, mask
= GPIO_bit(gpio
);
364 c
= gpio_to_pxachip(gpio
);
366 if (type
== IRQ_TYPE_PROBE
) {
367 /* Don't mess with enabled GPIOs using preconfigured edges or
368 * GPIOs set to alternate function or to output during probe
370 if ((c
->irq_edge_rise
| c
->irq_edge_fall
) & GPIO_bit(gpio
))
373 if (__gpio_is_occupied(gpio
))
376 type
= IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
;
379 gpdr
= readl_relaxed(c
->regbase
+ GPDR_OFFSET
);
381 if (__gpio_is_inverted(gpio
))
382 writel_relaxed(gpdr
| mask
, c
->regbase
+ GPDR_OFFSET
);
384 writel_relaxed(gpdr
& ~mask
, c
->regbase
+ GPDR_OFFSET
);
386 if (type
& IRQ_TYPE_EDGE_RISING
)
387 c
->irq_edge_rise
|= mask
;
389 c
->irq_edge_rise
&= ~mask
;
391 if (type
& IRQ_TYPE_EDGE_FALLING
)
392 c
->irq_edge_fall
|= mask
;
394 c
->irq_edge_fall
&= ~mask
;
396 update_edge_detect(c
);
398 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__
, d
->irq
, gpio
,
399 ((type
& IRQ_TYPE_EDGE_RISING
) ? " rising" : ""),
400 ((type
& IRQ_TYPE_EDGE_FALLING
) ? " falling" : ""));
404 static void pxa_gpio_demux_handler(struct irq_desc
*desc
)
406 struct pxa_gpio_chip
*c
;
407 int loop
, gpio
, gpio_base
, n
;
409 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
411 chained_irq_enter(chip
, desc
);
415 for_each_gpio_chip(gpio
, c
) {
416 gpio_base
= c
->chip
.base
;
418 gedr
= readl_relaxed(c
->regbase
+ GEDR_OFFSET
);
419 gedr
= gedr
& c
->irq_mask
;
420 writel_relaxed(gedr
, c
->regbase
+ GEDR_OFFSET
);
422 for_each_set_bit(n
, &gedr
, BITS_PER_LONG
) {
425 generic_handle_irq(gpio_to_irq(gpio_base
+ n
));
430 chained_irq_exit(chip
, desc
);
433 static void pxa_ack_muxed_gpio(struct irq_data
*d
)
435 int gpio
= pxa_irq_to_gpio(d
->irq
);
436 struct pxa_gpio_chip
*c
= gpio_to_pxachip(gpio
);
438 writel_relaxed(GPIO_bit(gpio
), c
->regbase
+ GEDR_OFFSET
);
441 static void pxa_mask_muxed_gpio(struct irq_data
*d
)
443 int gpio
= pxa_irq_to_gpio(d
->irq
);
444 struct pxa_gpio_chip
*c
= gpio_to_pxachip(gpio
);
447 c
->irq_mask
&= ~GPIO_bit(gpio
);
449 grer
= readl_relaxed(c
->regbase
+ GRER_OFFSET
) & ~GPIO_bit(gpio
);
450 gfer
= readl_relaxed(c
->regbase
+ GFER_OFFSET
) & ~GPIO_bit(gpio
);
451 writel_relaxed(grer
, c
->regbase
+ GRER_OFFSET
);
452 writel_relaxed(gfer
, c
->regbase
+ GFER_OFFSET
);
455 static int pxa_gpio_set_wake(struct irq_data
*d
, unsigned int on
)
457 int gpio
= pxa_irq_to_gpio(d
->irq
);
458 struct pxa_gpio_chip
*c
= gpio_to_pxachip(gpio
);
461 return c
->set_wake(gpio
, on
);
466 static void pxa_unmask_muxed_gpio(struct irq_data
*d
)
468 int gpio
= pxa_irq_to_gpio(d
->irq
);
469 struct pxa_gpio_chip
*c
= gpio_to_pxachip(gpio
);
471 c
->irq_mask
|= GPIO_bit(gpio
);
472 update_edge_detect(c
);
475 static struct irq_chip pxa_muxed_gpio_chip
= {
477 .irq_ack
= pxa_ack_muxed_gpio
,
478 .irq_mask
= pxa_mask_muxed_gpio
,
479 .irq_unmask
= pxa_unmask_muxed_gpio
,
480 .irq_set_type
= pxa_gpio_irq_type
,
481 .irq_set_wake
= pxa_gpio_set_wake
,
484 static int pxa_gpio_nums(struct platform_device
*pdev
)
486 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
487 struct pxa_gpio_id
*pxa_id
= (struct pxa_gpio_id
*)id
->driver_data
;
490 switch (pxa_id
->type
) {
499 gpio_type
= pxa_id
->type
;
500 count
= pxa_id
->gpio_nums
- 1;
510 static const struct of_device_id pxa_gpio_dt_ids
[] = {
511 { .compatible
= "intel,pxa25x-gpio", .data
= &pxa25x_id
, },
512 { .compatible
= "intel,pxa26x-gpio", .data
= &pxa26x_id
, },
513 { .compatible
= "intel,pxa27x-gpio", .data
= &pxa27x_id
, },
514 { .compatible
= "intel,pxa3xx-gpio", .data
= &pxa3xx_id
, },
515 { .compatible
= "marvell,pxa93x-gpio", .data
= &pxa93x_id
, },
516 { .compatible
= "marvell,mmp-gpio", .data
= &mmp_id
, },
517 { .compatible
= "marvell,mmp2-gpio", .data
= &mmp2_id
, },
518 { .compatible
= "marvell,pxa1928-gpio", .data
= &pxa1928_id
, },
522 static int pxa_irq_domain_map(struct irq_domain
*d
, unsigned int irq
,
525 irq_set_chip_and_handler(irq
, &pxa_muxed_gpio_chip
,
527 irq_set_noprobe(irq
);
531 const struct irq_domain_ops pxa_irq_domain_ops
= {
532 .map
= pxa_irq_domain_map
,
533 .xlate
= irq_domain_xlate_twocell
,
536 static int pxa_gpio_probe_dt(struct platform_device
*pdev
)
538 int ret
= 0, nr_gpios
;
539 struct device_node
*np
= pdev
->dev
.of_node
;
540 const struct of_device_id
*of_id
=
541 of_match_device(pxa_gpio_dt_ids
, &pdev
->dev
);
542 const struct pxa_gpio_id
*gpio_id
;
544 if (!of_id
|| !of_id
->data
) {
545 dev_err(&pdev
->dev
, "Failed to find gpio controller\n");
548 gpio_id
= of_id
->data
;
549 gpio_type
= gpio_id
->type
;
551 nr_gpios
= gpio_id
->gpio_nums
;
552 pxa_last_gpio
= nr_gpios
- 1;
554 irq_base
= irq_alloc_descs(-1, 0, nr_gpios
, 0);
556 dev_err(&pdev
->dev
, "Failed to allocate IRQ numbers\n");
560 domain
= irq_domain_add_legacy(np
, nr_gpios
, irq_base
, 0,
561 &pxa_irq_domain_ops
, NULL
);
562 pxa_gpio_of_node
= np
;
565 iounmap(gpio_reg_base
);
569 #define pxa_gpio_probe_dt(pdev) (-1)
572 static int pxa_gpio_probe(struct platform_device
*pdev
)
574 struct pxa_gpio_chip
*c
;
575 struct resource
*res
;
577 struct pxa_gpio_platform_data
*info
;
578 int gpio
, irq
, ret
, use_of
= 0;
579 int irq0
= 0, irq1
= 0, irq_mux
, gpio_offset
= 0;
581 info
= dev_get_platdata(&pdev
->dev
);
583 irq_base
= info
->irq_base
;
586 pxa_last_gpio
= pxa_gpio_nums(pdev
);
590 ret
= pxa_gpio_probe_dt(pdev
);
598 irq0
= platform_get_irq_byname(pdev
, "gpio0");
599 irq1
= platform_get_irq_byname(pdev
, "gpio1");
600 irq_mux
= platform_get_irq_byname(pdev
, "gpio_mux");
601 if ((irq0
> 0 && irq1
<= 0) || (irq0
<= 0 && irq1
> 0)
604 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
607 gpio_reg_base
= ioremap(res
->start
, resource_size(res
));
614 clk
= clk_get(&pdev
->dev
, NULL
);
616 dev_err(&pdev
->dev
, "Error %ld to get gpio clock\n",
618 iounmap(gpio_reg_base
);
621 ret
= clk_prepare_enable(clk
);
624 iounmap(gpio_reg_base
);
628 /* Initialize GPIO chips */
629 pxa_init_gpio_chip(pxa_last_gpio
, info
? info
->gpio_set_wake
: NULL
);
631 /* clear all GPIO edge detects */
632 for_each_gpio_chip(gpio
, c
) {
633 writel_relaxed(0, c
->regbase
+ GFER_OFFSET
);
634 writel_relaxed(0, c
->regbase
+ GRER_OFFSET
);
635 writel_relaxed(~0, c
->regbase
+ GEDR_OFFSET
);
636 /* unmask GPIO edge detect for AP side */
637 if (gpio_is_mmp_type(gpio_type
))
638 writel_relaxed(~0, c
->regbase
+ ED_MASK_OFFSET
);
643 irq
= gpio_to_irq(0);
644 irq_set_chip_and_handler(irq
, &pxa_muxed_gpio_chip
,
646 irq_clear_status_flags(irq
, IRQ_NOREQUEST
| IRQ_NOPROBE
);
649 irq
= gpio_to_irq(1);
650 irq_set_chip_and_handler(irq
, &pxa_muxed_gpio_chip
,
652 irq_clear_status_flags(irq
, IRQ_NOREQUEST
| IRQ_NOPROBE
);
655 for (irq
= gpio_to_irq(gpio_offset
);
656 irq
<= gpio_to_irq(pxa_last_gpio
); irq
++) {
657 irq_set_chip_and_handler(irq
, &pxa_muxed_gpio_chip
,
659 irq_clear_status_flags(irq
, IRQ_NOREQUEST
| IRQ_NOPROBE
);
664 irq_set_chained_handler(irq0
, pxa_gpio_demux_handler
);
666 irq_set_chained_handler(irq1
, pxa_gpio_demux_handler
);
668 irq_set_chained_handler(irq_mux
, pxa_gpio_demux_handler
);
672 static const struct platform_device_id gpio_id_table
[] = {
673 { "pxa25x-gpio", (unsigned long)&pxa25x_id
},
674 { "pxa26x-gpio", (unsigned long)&pxa26x_id
},
675 { "pxa27x-gpio", (unsigned long)&pxa27x_id
},
676 { "pxa3xx-gpio", (unsigned long)&pxa3xx_id
},
677 { "pxa93x-gpio", (unsigned long)&pxa93x_id
},
678 { "mmp-gpio", (unsigned long)&mmp_id
},
679 { "mmp2-gpio", (unsigned long)&mmp2_id
},
680 { "pxa1928-gpio", (unsigned long)&pxa1928_id
},
684 static struct platform_driver pxa_gpio_driver
= {
685 .probe
= pxa_gpio_probe
,
688 .of_match_table
= of_match_ptr(pxa_gpio_dt_ids
),
690 .id_table
= gpio_id_table
,
693 static int __init
pxa_gpio_init(void)
695 return platform_driver_register(&pxa_gpio_driver
);
697 postcore_initcall(pxa_gpio_init
);
700 static int pxa_gpio_suspend(void)
702 struct pxa_gpio_chip
*c
;
705 for_each_gpio_chip(gpio
, c
) {
706 c
->saved_gplr
= readl_relaxed(c
->regbase
+ GPLR_OFFSET
);
707 c
->saved_gpdr
= readl_relaxed(c
->regbase
+ GPDR_OFFSET
);
708 c
->saved_grer
= readl_relaxed(c
->regbase
+ GRER_OFFSET
);
709 c
->saved_gfer
= readl_relaxed(c
->regbase
+ GFER_OFFSET
);
711 /* Clear GPIO transition detect bits */
712 writel_relaxed(0xffffffff, c
->regbase
+ GEDR_OFFSET
);
717 static void pxa_gpio_resume(void)
719 struct pxa_gpio_chip
*c
;
722 for_each_gpio_chip(gpio
, c
) {
723 /* restore level with set/clear */
724 writel_relaxed(c
->saved_gplr
, c
->regbase
+ GPSR_OFFSET
);
725 writel_relaxed(~c
->saved_gplr
, c
->regbase
+ GPCR_OFFSET
);
727 writel_relaxed(c
->saved_grer
, c
->regbase
+ GRER_OFFSET
);
728 writel_relaxed(c
->saved_gfer
, c
->regbase
+ GFER_OFFSET
);
729 writel_relaxed(c
->saved_gpdr
, c
->regbase
+ GPDR_OFFSET
);
733 #define pxa_gpio_suspend NULL
734 #define pxa_gpio_resume NULL
737 struct syscore_ops pxa_gpio_syscore_ops
= {
738 .suspend
= pxa_gpio_suspend
,
739 .resume
= pxa_gpio_resume
,
742 static int __init
pxa_gpio_sysinit(void)
744 register_syscore_ops(&pxa_gpio_syscore_ops
);
747 postcore_initcall(pxa_gpio_sysinit
);