2 * linux/arch/arm/mach-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.
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/irq.h>
18 #include <linux/sysdev.h>
22 #include <mach/hardware.h>
23 #include <mach/pxa-regs.h>
24 #include <mach/pxa2xx-gpio.h>
28 #define GPIO0_BASE ((void __iomem *)io_p2v(0x40E00000))
29 #define GPIO1_BASE ((void __iomem *)io_p2v(0x40E00004))
30 #define GPIO2_BASE ((void __iomem *)io_p2v(0x40E00008))
31 #define GPIO3_BASE ((void __iomem *)io_p2v(0x40E00100))
33 #define GPLR_OFFSET 0x00
34 #define GPDR_OFFSET 0x0C
35 #define GPSR_OFFSET 0x18
36 #define GPCR_OFFSET 0x24
37 #define GRER_OFFSET 0x30
38 #define GFER_OFFSET 0x3C
39 #define GEDR_OFFSET 0x48
41 struct pxa_gpio_chip
{
42 struct gpio_chip chip
;
43 void __iomem
*regbase
;
48 #ifdef CONFIG_CPU_PXA26x
49 /* GPIO86/87/88/89 on PXA26x have their direction bits in GPDR2 inverted,
50 * as well as their Alternate Function value being '1' for GPIO in GAFRx.
52 static int __gpio_is_inverted(unsigned gpio
)
54 return cpu_is_pxa25x() && gpio
> 85;
57 #define __gpio_is_inverted(gpio) (0)
61 * Configure pins for GPIO or other functions
63 int pxa_gpio_mode(int gpio_mode
)
66 int gpio
= gpio_mode
& GPIO_MD_MASK_NR
;
67 int fn
= (gpio_mode
& GPIO_MD_MASK_FN
) >> 8;
70 if (gpio
> pxa_last_gpio
)
73 local_irq_save(flags
);
74 if (gpio_mode
& GPIO_DFLT_LOW
)
75 GPCR(gpio
) = GPIO_bit(gpio
);
76 else if (gpio_mode
& GPIO_DFLT_HIGH
)
77 GPSR(gpio
) = GPIO_bit(gpio
);
78 if (gpio_mode
& GPIO_MD_MASK_DIR
)
79 GPDR(gpio
) |= GPIO_bit(gpio
);
81 GPDR(gpio
) &= ~GPIO_bit(gpio
);
82 gafr
= GAFR(gpio
) & ~(0x3 << (((gpio
) & 0xf)*2));
83 GAFR(gpio
) = gafr
| (fn
<< (((gpio
) & 0xf)*2));
84 local_irq_restore(flags
);
88 EXPORT_SYMBOL(pxa_gpio_mode
);
90 static int pxa_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
93 u32 mask
= 1 << offset
;
95 struct pxa_gpio_chip
*pxa
;
98 pxa
= container_of(chip
, struct pxa_gpio_chip
, chip
);
99 gpdr
= pxa
->regbase
+ GPDR_OFFSET
;
100 local_irq_save(flags
);
101 value
= __raw_readl(gpdr
);
102 if (__gpio_is_inverted(chip
->base
+ offset
))
106 __raw_writel(value
, gpdr
);
107 local_irq_restore(flags
);
112 static int pxa_gpio_direction_output(struct gpio_chip
*chip
,
113 unsigned offset
, int value
)
116 u32 mask
= 1 << offset
;
118 struct pxa_gpio_chip
*pxa
;
121 pxa
= container_of(chip
, struct pxa_gpio_chip
, chip
);
123 pxa
->regbase
+ (value
? GPSR_OFFSET
: GPCR_OFFSET
));
124 gpdr
= pxa
->regbase
+ GPDR_OFFSET
;
125 local_irq_save(flags
);
126 tmp
= __raw_readl(gpdr
);
127 if (__gpio_is_inverted(chip
->base
+ offset
))
131 __raw_writel(tmp
, gpdr
);
132 local_irq_restore(flags
);
140 static int pxa_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
142 u32 mask
= 1 << offset
;
143 struct pxa_gpio_chip
*pxa
;
145 pxa
= container_of(chip
, struct pxa_gpio_chip
, chip
);
146 return __raw_readl(pxa
->regbase
+ GPLR_OFFSET
) & mask
;
150 * Set output GPIO level
152 static void pxa_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
154 u32 mask
= 1 << offset
;
155 struct pxa_gpio_chip
*pxa
;
157 pxa
= container_of(chip
, struct pxa_gpio_chip
, chip
);
160 __raw_writel(mask
, pxa
->regbase
+ GPSR_OFFSET
);
162 __raw_writel(mask
, pxa
->regbase
+ GPCR_OFFSET
);
165 #define GPIO_CHIP(_n) \
167 .regbase = GPIO##_n##_BASE, \
169 .label = "gpio-" #_n, \
170 .direction_input = pxa_gpio_direction_input, \
171 .direction_output = pxa_gpio_direction_output, \
172 .get = pxa_gpio_get, \
173 .set = pxa_gpio_set, \
179 static struct pxa_gpio_chip pxa_gpio_chip
[] = {
183 #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
189 * PXA GPIO edge detection for IRQs:
190 * IRQs are generated on Falling-Edge, Rising-Edge, or both.
191 * Use this instead of directly setting GRER/GFER.
194 static unsigned long GPIO_IRQ_rising_edge
[4];
195 static unsigned long GPIO_IRQ_falling_edge
[4];
196 static unsigned long GPIO_IRQ_mask
[4];
199 * On PXA25x and PXA27x, GAFRx and GPDRx together decide the alternate
200 * function of a GPIO, and GPDRx cannot be altered once configured. It
201 * is attributed as "occupied" here (I know this terminology isn't
202 * accurate, you are welcome to propose a better one :-)
204 static int __gpio_is_occupied(unsigned gpio
)
206 if (cpu_is_pxa27x() || cpu_is_pxa25x()) {
207 int af
= (GAFR(gpio
) >> ((gpio
& 0xf) * 2)) & 0x3;
208 int dir
= GPDR(gpio
) & GPIO_bit(gpio
);
210 if (__gpio_is_inverted(gpio
))
211 return af
!= 1 || dir
== 0;
213 return af
!= 0 || dir
!= 0;
219 static int pxa_gpio_irq_type(unsigned int irq
, unsigned int type
)
223 gpio
= IRQ_TO_GPIO(irq
);
226 if (type
== IRQ_TYPE_PROBE
) {
227 /* Don't mess with enabled GPIOs using preconfigured edges or
228 * GPIOs set to alternate function or to output during probe
230 if ((GPIO_IRQ_rising_edge
[idx
] & GPIO_bit(gpio
)) ||
231 (GPIO_IRQ_falling_edge
[idx
] & GPIO_bit(gpio
)))
234 if (__gpio_is_occupied(gpio
))
237 type
= IRQ_TYPE_EDGE_RISING
| IRQ_TYPE_EDGE_FALLING
;
240 if (__gpio_is_inverted(gpio
))
241 GPDR(gpio
) |= GPIO_bit(gpio
);
243 GPDR(gpio
) &= ~GPIO_bit(gpio
);
245 if (type
& IRQ_TYPE_EDGE_RISING
)
246 __set_bit(gpio
, GPIO_IRQ_rising_edge
);
248 __clear_bit(gpio
, GPIO_IRQ_rising_edge
);
250 if (type
& IRQ_TYPE_EDGE_FALLING
)
251 __set_bit(gpio
, GPIO_IRQ_falling_edge
);
253 __clear_bit(gpio
, GPIO_IRQ_falling_edge
);
255 GRER(gpio
) = GPIO_IRQ_rising_edge
[idx
] & GPIO_IRQ_mask
[idx
];
256 GFER(gpio
) = GPIO_IRQ_falling_edge
[idx
] & GPIO_IRQ_mask
[idx
];
258 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__
, irq
, gpio
,
259 ((type
& IRQ_TYPE_EDGE_RISING
) ? " rising" : ""),
260 ((type
& IRQ_TYPE_EDGE_FALLING
) ? " falling" : ""));
265 * GPIO IRQs must be acknowledged. This is for GPIO 0 and 1.
268 static void pxa_ack_low_gpio(unsigned int irq
)
270 GEDR0
= (1 << (irq
- IRQ_GPIO0
));
273 static void pxa_mask_low_gpio(unsigned int irq
)
275 ICMR
&= ~(1 << (irq
- PXA_IRQ(0)));
278 static void pxa_unmask_low_gpio(unsigned int irq
)
280 ICMR
|= 1 << (irq
- PXA_IRQ(0));
283 static struct irq_chip pxa_low_gpio_chip
= {
285 .ack
= pxa_ack_low_gpio
,
286 .mask
= pxa_mask_low_gpio
,
287 .unmask
= pxa_unmask_low_gpio
,
288 .set_type
= pxa_gpio_irq_type
,
292 * Demux handler for GPIO>=2 edge detect interrupts
295 #define GEDR_BITS (sizeof(gedr) * BITS_PER_BYTE)
297 static void pxa_gpio_demux_handler(unsigned int irq
, struct irq_desc
*desc
)
300 unsigned long gedr
[4];
303 gedr
[0] = GEDR0
& GPIO_IRQ_mask
[0] & ~3;
304 gedr
[1] = GEDR1
& GPIO_IRQ_mask
[1];
305 gedr
[2] = GEDR2
& GPIO_IRQ_mask
[2];
306 gedr
[3] = GEDR3
& GPIO_IRQ_mask
[3];
308 GEDR0
= gedr
[0]; GEDR1
= gedr
[1];
309 GEDR2
= gedr
[2]; GEDR3
= gedr
[3];
312 bit
= find_first_bit(gedr
, GEDR_BITS
);
313 while (bit
< GEDR_BITS
) {
316 n
= PXA_GPIO_IRQ_BASE
+ bit
;
317 generic_handle_irq(n
);
319 bit
= find_next_bit(gedr
, GEDR_BITS
, bit
+ 1);
324 static void pxa_ack_muxed_gpio(unsigned int irq
)
326 int gpio
= irq
- IRQ_GPIO(2) + 2;
327 GEDR(gpio
) = GPIO_bit(gpio
);
330 static void pxa_mask_muxed_gpio(unsigned int irq
)
332 int gpio
= irq
- IRQ_GPIO(2) + 2;
333 __clear_bit(gpio
, GPIO_IRQ_mask
);
334 GRER(gpio
) &= ~GPIO_bit(gpio
);
335 GFER(gpio
) &= ~GPIO_bit(gpio
);
338 static void pxa_unmask_muxed_gpio(unsigned int irq
)
340 int gpio
= irq
- IRQ_GPIO(2) + 2;
342 __set_bit(gpio
, GPIO_IRQ_mask
);
343 GRER(gpio
) = GPIO_IRQ_rising_edge
[idx
] & GPIO_IRQ_mask
[idx
];
344 GFER(gpio
) = GPIO_IRQ_falling_edge
[idx
] & GPIO_IRQ_mask
[idx
];
347 static struct irq_chip pxa_muxed_gpio_chip
= {
349 .ack
= pxa_ack_muxed_gpio
,
350 .mask
= pxa_mask_muxed_gpio
,
351 .unmask
= pxa_unmask_muxed_gpio
,
352 .set_type
= pxa_gpio_irq_type
,
355 void __init
pxa_init_gpio(int gpio_nr
, set_wake_t fn
)
359 pxa_last_gpio
= gpio_nr
- 1;
361 /* clear all GPIO edge detects */
362 for (i
= 0; i
< gpio_nr
; i
+= 32) {
368 /* GPIO 0 and 1 must have their mask bit always set */
369 GPIO_IRQ_mask
[0] = 3;
371 for (irq
= IRQ_GPIO0
; irq
<= IRQ_GPIO1
; irq
++) {
372 set_irq_chip(irq
, &pxa_low_gpio_chip
);
373 set_irq_handler(irq
, handle_edge_irq
);
374 set_irq_flags(irq
, IRQF_VALID
| IRQF_PROBE
);
377 for (irq
= IRQ_GPIO(2); irq
< IRQ_GPIO(gpio_nr
); irq
++) {
378 set_irq_chip(irq
, &pxa_muxed_gpio_chip
);
379 set_irq_handler(irq
, handle_edge_irq
);
380 set_irq_flags(irq
, IRQF_VALID
| IRQF_PROBE
);
383 /* Install handler for GPIO>=2 edge detect interrupts */
384 set_irq_chained_handler(IRQ_GPIO_2_x
, pxa_gpio_demux_handler
);
386 pxa_low_gpio_chip
.set_wake
= fn
;
387 pxa_muxed_gpio_chip
.set_wake
= fn
;
389 /* add a GPIO chip for each register bank.
390 * the last PXA25x register only contains 21 GPIOs
392 for (gpio
= 0, i
= 0; gpio
< gpio_nr
; gpio
+= 32, i
++) {
393 if (gpio
+ 32 > gpio_nr
)
394 pxa_gpio_chip
[i
].chip
.ngpio
= gpio_nr
- gpio
;
395 gpiochip_add(&pxa_gpio_chip
[i
].chip
);
401 static unsigned long saved_gplr
[4];
402 static unsigned long saved_gpdr
[4];
403 static unsigned long saved_grer
[4];
404 static unsigned long saved_gfer
[4];
406 static int pxa_gpio_suspend(struct sys_device
*dev
, pm_message_t state
)
410 for (gpio
= 0, i
= 0; gpio
< pxa_last_gpio
; gpio
+= 32, i
++) {
411 saved_gplr
[i
] = GPLR(gpio
);
412 saved_gpdr
[i
] = GPDR(gpio
);
413 saved_grer
[i
] = GRER(gpio
);
414 saved_gfer
[i
] = GFER(gpio
);
416 /* Clear GPIO transition detect bits */
417 GEDR(gpio
) = GEDR(gpio
);
422 static int pxa_gpio_resume(struct sys_device
*dev
)
426 for (gpio
= 0, i
= 0; gpio
< pxa_last_gpio
; gpio
+= 32, i
++) {
427 /* restore level with set/clear */
428 GPSR(gpio
) = saved_gplr
[i
];
429 GPCR(gpio
) = ~saved_gplr
[i
];
431 GRER(gpio
) = saved_grer
[i
];
432 GFER(gpio
) = saved_gfer
[i
];
433 GPDR(gpio
) = saved_gpdr
[i
];
438 #define pxa_gpio_suspend NULL
439 #define pxa_gpio_resume NULL
442 struct sysdev_class pxa_gpio_sysclass
= {
444 .suspend
= pxa_gpio_suspend
,
445 .resume
= pxa_gpio_resume
,
448 static int __init
pxa_gpio_init(void)
450 return sysdev_class_register(&pxa_gpio_sysclass
);
453 core_initcall(pxa_gpio_init
);