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;
49 * Configure pins for GPIO or other functions
51 int pxa_gpio_mode(int gpio_mode)
54 int gpio = gpio_mode & GPIO_MD_MASK_NR;
55 int fn = (gpio_mode & GPIO_MD_MASK_FN) >> 8;
58 if (gpio > pxa_last_gpio)
61 local_irq_save(flags);
62 if (gpio_mode & GPIO_DFLT_LOW)
63 GPCR(gpio) = GPIO_bit(gpio);
64 else if (gpio_mode & GPIO_DFLT_HIGH)
65 GPSR(gpio) = GPIO_bit(gpio);
66 if (gpio_mode & GPIO_MD_MASK_DIR)
67 GPDR(gpio) |= GPIO_bit(gpio);
69 GPDR(gpio) &= ~GPIO_bit(gpio);
70 gafr = GAFR(gpio) & ~(0x3 << (((gpio) & 0xf)*2));
71 GAFR(gpio) = gafr | (fn << (((gpio) & 0xf)*2));
72 local_irq_restore(flags);
76 EXPORT_SYMBOL(pxa_gpio_mode);
78 static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
81 u32 mask = 1 << offset;
83 struct pxa_gpio_chip *pxa;
86 pxa = container_of(chip, struct pxa_gpio_chip, chip);
87 gpdr = pxa->regbase + GPDR_OFFSET;
88 local_irq_save(flags);
89 value = __raw_readl(gpdr);
90 if (__gpio_is_inverted(chip->base + offset))
94 __raw_writel(value, gpdr);
95 local_irq_restore(flags);
100 static int pxa_gpio_direction_output(struct gpio_chip *chip,
101 unsigned offset, int value)
104 u32 mask = 1 << offset;
106 struct pxa_gpio_chip *pxa;
109 pxa = container_of(chip, struct pxa_gpio_chip, chip);
111 pxa->regbase + (value ? GPSR_OFFSET : GPCR_OFFSET));
112 gpdr = pxa->regbase + GPDR_OFFSET;
113 local_irq_save(flags);
114 tmp = __raw_readl(gpdr);
115 if (__gpio_is_inverted(chip->base + offset))
119 __raw_writel(tmp, gpdr);
120 local_irq_restore(flags);
128 static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
130 u32 mask = 1 << offset;
131 struct pxa_gpio_chip *pxa;
133 pxa = container_of(chip, struct pxa_gpio_chip, chip);
134 return __raw_readl(pxa->regbase + GPLR_OFFSET) & mask;
138 * Set output GPIO level
140 static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
142 u32 mask = 1 << offset;
143 struct pxa_gpio_chip *pxa;
145 pxa = container_of(chip, struct pxa_gpio_chip, chip);
148 __raw_writel(mask, pxa->regbase + GPSR_OFFSET);
150 __raw_writel(mask, pxa->regbase + GPCR_OFFSET);
153 #define GPIO_CHIP(_n) \
155 .regbase = GPIO##_n##_BASE, \
157 .label = "gpio-" #_n, \
158 .direction_input = pxa_gpio_direction_input, \
159 .direction_output = pxa_gpio_direction_output, \
160 .get = pxa_gpio_get, \
161 .set = pxa_gpio_set, \
167 static struct pxa_gpio_chip pxa_gpio_chip[] = {
171 #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
176 static void __init pxa_init_gpio_chip(int gpio_nr)
180 /* add a GPIO chip for each register bank.
181 * the last PXA25x register only contains 21 GPIOs
183 for (gpio = 0, i = 0; gpio < gpio_nr; gpio += 32, i++) {
184 if (gpio + 32 > gpio_nr)
185 pxa_gpio_chip[i].chip.ngpio = gpio_nr - gpio;
186 gpiochip_add(&pxa_gpio_chip[i].chip);
191 * PXA GPIO edge detection for IRQs:
192 * IRQs are generated on Falling-Edge, Rising-Edge, or both.
193 * Use this instead of directly setting GRER/GFER.
196 static unsigned long GPIO_IRQ_rising_edge[4];
197 static unsigned long GPIO_IRQ_falling_edge[4];
198 static unsigned long GPIO_IRQ_mask[4];
200 static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
204 gpio = IRQ_TO_GPIO(irq);
207 if (type == IRQ_TYPE_PROBE) {
208 /* Don't mess with enabled GPIOs using preconfigured edges or
209 * GPIOs set to alternate function or to output during probe
211 if ((GPIO_IRQ_rising_edge[idx] & GPIO_bit(gpio)) ||
212 (GPIO_IRQ_falling_edge[idx] & GPIO_bit(gpio)))
215 if (__gpio_is_occupied(gpio))
218 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
221 if (__gpio_is_inverted(gpio))
222 GPDR(gpio) |= GPIO_bit(gpio);
224 GPDR(gpio) &= ~GPIO_bit(gpio);
226 if (type & IRQ_TYPE_EDGE_RISING)
227 __set_bit(gpio, GPIO_IRQ_rising_edge);
229 __clear_bit(gpio, GPIO_IRQ_rising_edge);
231 if (type & IRQ_TYPE_EDGE_FALLING)
232 __set_bit(gpio, GPIO_IRQ_falling_edge);
234 __clear_bit(gpio, GPIO_IRQ_falling_edge);
236 GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
237 GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
239 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, irq, gpio,
240 ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
241 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
246 * Demux handler for GPIO>=2 edge detect interrupts
249 #define GEDR_BITS (sizeof(gedr) * BITS_PER_BYTE)
251 static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
254 unsigned long gedr[4];
257 gedr[0] = GEDR0 & GPIO_IRQ_mask[0] & ~3;
258 gedr[1] = GEDR1 & GPIO_IRQ_mask[1];
259 gedr[2] = GEDR2 & GPIO_IRQ_mask[2];
260 gedr[3] = GEDR3 & GPIO_IRQ_mask[3];
262 GEDR0 = gedr[0]; GEDR1 = gedr[1];
263 GEDR2 = gedr[2]; GEDR3 = gedr[3];
266 bit = find_first_bit(gedr, GEDR_BITS);
267 while (bit < GEDR_BITS) {
270 n = PXA_GPIO_IRQ_BASE + bit;
271 generic_handle_irq(n);
273 bit = find_next_bit(gedr, GEDR_BITS, bit + 1);
278 static void pxa_ack_muxed_gpio(unsigned int irq)
280 int gpio = irq - IRQ_GPIO(2) + 2;
281 GEDR(gpio) = GPIO_bit(gpio);
284 static void pxa_mask_muxed_gpio(unsigned int irq)
286 int gpio = irq - IRQ_GPIO(2) + 2;
287 __clear_bit(gpio, GPIO_IRQ_mask);
288 GRER(gpio) &= ~GPIO_bit(gpio);
289 GFER(gpio) &= ~GPIO_bit(gpio);
292 static void pxa_unmask_muxed_gpio(unsigned int irq)
294 int gpio = irq - IRQ_GPIO(2) + 2;
296 __set_bit(gpio, GPIO_IRQ_mask);
297 GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
298 GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
301 static struct irq_chip pxa_muxed_gpio_chip = {
303 .ack = pxa_ack_muxed_gpio,
304 .mask = pxa_mask_muxed_gpio,
305 .unmask = pxa_unmask_muxed_gpio,
306 .set_type = pxa_gpio_irq_type,
309 void __init pxa_init_gpio(int mux_irq, int start, int end, set_wake_t fn)
315 /* clear all GPIO edge detects */
316 for (i = start; i <= end; i += 32) {
317 GFER(i) &= ~GPIO_IRQ_mask[i];
318 GRER(i) &= ~GPIO_IRQ_mask[i];
319 GEDR(i) = GPIO_IRQ_mask[i];
322 for (irq = gpio_to_irq(start); irq <= gpio_to_irq(end); irq++) {
323 set_irq_chip(irq, &pxa_muxed_gpio_chip);
324 set_irq_handler(irq, handle_edge_irq);
325 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
328 /* Install handler for GPIO>=2 edge detect interrupts */
329 set_irq_chained_handler(mux_irq, pxa_gpio_demux_handler);
330 pxa_muxed_gpio_chip.set_wake = fn;
332 /* Initialize GPIO chips */
333 pxa_init_gpio_chip(end + 1);
338 static unsigned long saved_gplr[4];
339 static unsigned long saved_gpdr[4];
340 static unsigned long saved_grer[4];
341 static unsigned long saved_gfer[4];
343 static int pxa_gpio_suspend(struct sys_device *dev, pm_message_t state)
347 for (gpio = 0, i = 0; gpio < pxa_last_gpio; gpio += 32, i++) {
348 saved_gplr[i] = GPLR(gpio);
349 saved_gpdr[i] = GPDR(gpio);
350 saved_grer[i] = GRER(gpio);
351 saved_gfer[i] = GFER(gpio);
353 /* Clear GPIO transition detect bits */
354 GEDR(gpio) = GEDR(gpio);
359 static int pxa_gpio_resume(struct sys_device *dev)
363 for (gpio = 0, i = 0; gpio < pxa_last_gpio; gpio += 32, i++) {
364 /* restore level with set/clear */
365 GPSR(gpio) = saved_gplr[i];
366 GPCR(gpio) = ~saved_gplr[i];
368 GRER(gpio) = saved_grer[i];
369 GFER(gpio) = saved_gfer[i];
370 GPDR(gpio) = saved_gpdr[i];
375 #define pxa_gpio_suspend NULL
376 #define pxa_gpio_resume NULL
379 struct sysdev_class pxa_gpio_sysclass = {
381 .suspend = pxa_gpio_suspend,
382 .resume = pxa_gpio_resume,
385 static int __init pxa_gpio_init(void)
387 return sysdev_class_register(&pxa_gpio_sysclass);
390 core_initcall(pxa_gpio_init);