]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-pxa/gpio.c
[ARM] pxa: move GPIO sysdev outside of generic.c into gpio.c
[linux-2.6-omap-h63xx.git] / arch / arm / mach-pxa / gpio.c
1 /*
2  *  linux/arch/arm/mach-pxa/gpio.c
3  *
4  *  Generic PXA GPIO handling
5  *
6  *  Author:     Nicolas Pitre
7  *  Created:    Jun 15, 2001
8  *  Copyright:  MontaVista Software Inc.
9  *
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.
13  */
14
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/irq.h>
18 #include <linux/sysdev.h>
19
20 #include <asm/gpio.h>
21 #include <asm/hardware.h>
22 #include <asm/io.h>
23 #include <asm/arch/pxa-regs.h>
24
25 #include "generic.h"
26
27
28 struct pxa_gpio_chip {
29         struct gpio_chip chip;
30         void __iomem     *regbase;
31 };
32
33 int pxa_last_gpio;
34
35 /*
36  * Configure pins for GPIO or other functions
37  */
38 int pxa_gpio_mode(int gpio_mode)
39 {
40         unsigned long flags;
41         int gpio = gpio_mode & GPIO_MD_MASK_NR;
42         int fn = (gpio_mode & GPIO_MD_MASK_FN) >> 8;
43         int gafr;
44
45         if (gpio > pxa_last_gpio)
46                 return -EINVAL;
47
48         local_irq_save(flags);
49         if (gpio_mode & GPIO_DFLT_LOW)
50                 GPCR(gpio) = GPIO_bit(gpio);
51         else if (gpio_mode & GPIO_DFLT_HIGH)
52                 GPSR(gpio) = GPIO_bit(gpio);
53         if (gpio_mode & GPIO_MD_MASK_DIR)
54                 GPDR(gpio) |= GPIO_bit(gpio);
55         else
56                 GPDR(gpio) &= ~GPIO_bit(gpio);
57         gafr = GAFR(gpio) & ~(0x3 << (((gpio) & 0xf)*2));
58         GAFR(gpio) = gafr |  (fn  << (((gpio) & 0xf)*2));
59         local_irq_restore(flags);
60
61         return 0;
62 }
63 EXPORT_SYMBOL(pxa_gpio_mode);
64
65 static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
66 {
67         unsigned long        flags;
68         u32                  mask = 1 << offset;
69         u32                  value;
70         struct pxa_gpio_chip *pxa;
71         void __iomem         *gpdr;
72
73         pxa = container_of(chip, struct pxa_gpio_chip, chip);
74         gpdr = pxa->regbase + GPDR_OFFSET;
75         local_irq_save(flags);
76         value = __raw_readl(gpdr);
77         value &= ~mask;
78         __raw_writel(value, gpdr);
79         local_irq_restore(flags);
80
81         return 0;
82 }
83
84 static int pxa_gpio_direction_output(struct gpio_chip *chip,
85                                         unsigned offset, int value)
86 {
87         unsigned long        flags;
88         u32                  mask = 1 << offset;
89         u32                  tmp;
90         struct pxa_gpio_chip *pxa;
91         void __iomem         *gpdr;
92
93         pxa = container_of(chip, struct pxa_gpio_chip, chip);
94         __raw_writel(mask,
95                         pxa->regbase + (value ? GPSR_OFFSET : GPCR_OFFSET));
96         gpdr = pxa->regbase + GPDR_OFFSET;
97         local_irq_save(flags);
98         tmp = __raw_readl(gpdr);
99         tmp |= mask;
100         __raw_writel(tmp, gpdr);
101         local_irq_restore(flags);
102
103         return 0;
104 }
105
106 /*
107  * Return GPIO level
108  */
109 static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
110 {
111         u32                  mask = 1 << offset;
112         struct pxa_gpio_chip *pxa;
113
114         pxa = container_of(chip, struct pxa_gpio_chip, chip);
115         return __raw_readl(pxa->regbase + GPLR_OFFSET) & mask;
116 }
117
118 /*
119  * Set output GPIO level
120  */
121 static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
122 {
123         u32                  mask = 1 << offset;
124         struct pxa_gpio_chip *pxa;
125
126         pxa = container_of(chip, struct pxa_gpio_chip, chip);
127
128         if (value)
129                 __raw_writel(mask, pxa->regbase + GPSR_OFFSET);
130         else
131                 __raw_writel(mask, pxa->regbase + GPCR_OFFSET);
132 }
133
134 #define GPIO_CHIP(_n)                                                   \
135         [_n] = {                                                        \
136                 .regbase = GPIO##_n##_BASE,                             \
137                 .chip = {                                               \
138                         .label            = "gpio-" #_n,                \
139                         .direction_input  = pxa_gpio_direction_input,   \
140                         .direction_output = pxa_gpio_direction_output,  \
141                         .get              = pxa_gpio_get,               \
142                         .set              = pxa_gpio_set,               \
143                         .base             = (_n) * 32,                  \
144                         .ngpio            = 32,                         \
145                 },                                                      \
146         }
147
148 static struct pxa_gpio_chip pxa_gpio_chip[] = {
149         GPIO_CHIP(0),
150         GPIO_CHIP(1),
151         GPIO_CHIP(2),
152 #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
153         GPIO_CHIP(3),
154 #endif
155 };
156
157 /*
158  * PXA GPIO edge detection for IRQs:
159  * IRQs are generated on Falling-Edge, Rising-Edge, or both.
160  * Use this instead of directly setting GRER/GFER.
161  */
162
163 static long GPIO_IRQ_rising_edge[4];
164 static long GPIO_IRQ_falling_edge[4];
165 static long GPIO_IRQ_mask[4];
166
167 static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
168 {
169         int gpio, idx;
170
171         gpio = IRQ_TO_GPIO(irq);
172         idx = gpio >> 5;
173
174         if (type == IRQ_TYPE_PROBE) {
175                 /* Don't mess with enabled GPIOs using preconfigured edges or
176                  * GPIOs set to alternate function or to output during probe
177                  */
178                 if ((GPIO_IRQ_rising_edge[idx] |
179                      GPIO_IRQ_falling_edge[idx] |
180                      GPDR(gpio)) & GPIO_bit(gpio))
181                         return 0;
182                 if (GAFR(gpio) & (0x3 << (((gpio) & 0xf)*2)))
183                         return 0;
184                 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
185         }
186
187         pxa_gpio_mode(gpio | GPIO_IN);
188
189         if (type & IRQ_TYPE_EDGE_RISING)
190                 __set_bit(gpio, GPIO_IRQ_rising_edge);
191         else
192                 __clear_bit(gpio, GPIO_IRQ_rising_edge);
193
194         if (type & IRQ_TYPE_EDGE_FALLING)
195                 __set_bit(gpio, GPIO_IRQ_falling_edge);
196         else
197                 __clear_bit(gpio, GPIO_IRQ_falling_edge);
198
199         GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
200         GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
201
202         pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, irq, gpio,
203                 ((type & IRQ_TYPE_EDGE_RISING)  ? " rising"  : ""),
204                 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
205         return 0;
206 }
207
208 /*
209  * GPIO IRQs must be acknowledged.  This is for GPIO 0 and 1.
210  */
211
212 static void pxa_ack_low_gpio(unsigned int irq)
213 {
214         GEDR0 = (1 << (irq - IRQ_GPIO0));
215 }
216
217 static void pxa_mask_low_gpio(unsigned int irq)
218 {
219         ICMR &= ~(1 << (irq - PXA_IRQ(0)));
220 }
221
222 static void pxa_unmask_low_gpio(unsigned int irq)
223 {
224         ICMR |= 1 << (irq - PXA_IRQ(0));
225 }
226
227 static struct irq_chip pxa_low_gpio_chip = {
228         .name           = "GPIO-l",
229         .ack            = pxa_ack_low_gpio,
230         .mask           = pxa_mask_low_gpio,
231         .unmask         = pxa_unmask_low_gpio,
232         .set_type       = pxa_gpio_irq_type,
233 };
234
235 /*
236  * Demux handler for GPIO>=2 edge detect interrupts
237  */
238
239 #define GEDR_BITS       (sizeof(gedr) * BITS_PER_BYTE)
240
241 static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
242 {
243         int loop, bit, n;
244         unsigned long gedr[4];
245
246         do {
247                 gedr[0] = GEDR0 & GPIO_IRQ_mask[0] & ~3;
248                 gedr[1] = GEDR1 & GPIO_IRQ_mask[1];
249                 gedr[2] = GEDR2 & GPIO_IRQ_mask[2];
250                 gedr[3] = GEDR3 & GPIO_IRQ_mask[3];
251
252                 GEDR0 = gedr[0]; GEDR1 = gedr[1];
253                 GEDR2 = gedr[2]; GEDR3 = gedr[3];
254
255                 loop = 0;
256                 bit = find_first_bit(gedr, GEDR_BITS);
257                 while (bit < GEDR_BITS) {
258                         loop = 1;
259
260                         n = PXA_GPIO_IRQ_BASE + bit;
261                         desc_handle_irq(n, irq_desc + n);
262
263                         bit = find_next_bit(gedr, GEDR_BITS, bit + 1);
264                 }
265         } while (loop);
266 }
267
268 static void pxa_ack_muxed_gpio(unsigned int irq)
269 {
270         int gpio = irq - IRQ_GPIO(2) + 2;
271         GEDR(gpio) = GPIO_bit(gpio);
272 }
273
274 static void pxa_mask_muxed_gpio(unsigned int irq)
275 {
276         int gpio = irq - IRQ_GPIO(2) + 2;
277         __clear_bit(gpio, GPIO_IRQ_mask);
278         GRER(gpio) &= ~GPIO_bit(gpio);
279         GFER(gpio) &= ~GPIO_bit(gpio);
280 }
281
282 static void pxa_unmask_muxed_gpio(unsigned int irq)
283 {
284         int gpio = irq - IRQ_GPIO(2) + 2;
285         int idx = gpio >> 5;
286         __set_bit(gpio, GPIO_IRQ_mask);
287         GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
288         GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
289 }
290
291 static struct irq_chip pxa_muxed_gpio_chip = {
292         .name           = "GPIO",
293         .ack            = pxa_ack_muxed_gpio,
294         .mask           = pxa_mask_muxed_gpio,
295         .unmask         = pxa_unmask_muxed_gpio,
296         .set_type       = pxa_gpio_irq_type,
297 };
298
299 void __init pxa_init_gpio(int gpio_nr, set_wake_t fn)
300 {
301         int irq, i, gpio;
302
303         pxa_last_gpio = gpio_nr - 1;
304
305         /* clear all GPIO edge detects */
306         for (i = 0; i < gpio_nr; i += 32) {
307                 GFER(i) = 0;
308                 GRER(i) = 0;
309                 GEDR(i) = GEDR(i);
310         }
311
312         /* GPIO 0 and 1 must have their mask bit always set */
313         GPIO_IRQ_mask[0] = 3;
314
315         for (irq = IRQ_GPIO0; irq <= IRQ_GPIO1; irq++) {
316                 set_irq_chip(irq, &pxa_low_gpio_chip);
317                 set_irq_handler(irq, handle_edge_irq);
318                 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
319         }
320
321         for (irq = IRQ_GPIO(2); irq < IRQ_GPIO(gpio_nr); irq++) {
322                 set_irq_chip(irq, &pxa_muxed_gpio_chip);
323                 set_irq_handler(irq, handle_edge_irq);
324                 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
325         }
326
327         /* Install handler for GPIO>=2 edge detect interrupts */
328         set_irq_chained_handler(IRQ_GPIO_2_x, pxa_gpio_demux_handler);
329
330         pxa_low_gpio_chip.set_wake = fn;
331         pxa_muxed_gpio_chip.set_wake = fn;
332
333         /* add a GPIO chip for each register bank.
334          * the last PXA25x register only contains 21 GPIOs
335          */
336         for (gpio = 0, i = 0; gpio < gpio_nr; gpio += 32, i++) {
337                 if (gpio + 32 > gpio_nr)
338                         pxa_gpio_chip[i].chip.ngpio = gpio_nr - gpio;
339                 gpiochip_add(&pxa_gpio_chip[i].chip);
340         }
341 }
342
343 #ifdef CONFIG_PM
344
345 static unsigned long saved_gplr[4];
346 static unsigned long saved_gpdr[4];
347 static unsigned long saved_grer[4];
348 static unsigned long saved_gfer[4];
349
350 static int pxa_gpio_suspend(struct sys_device *dev, pm_message_t state)
351 {
352         int i, gpio;
353
354         for (gpio = 0, i = 0; gpio < pxa_last_gpio; gpio += 32, i++) {
355                 saved_gplr[i] = GPLR(gpio);
356                 saved_gpdr[i] = GPDR(gpio);
357                 saved_grer[i] = GRER(gpio);
358                 saved_gfer[i] = GFER(gpio);
359
360                 /* Clear GPIO transition detect bits */
361                 GEDR(gpio) = GEDR(gpio);
362         }
363         return 0;
364 }
365
366 static int pxa_gpio_resume(struct sys_device *dev)
367 {
368         int i, gpio;
369
370         for (gpio = 0, i = 0; gpio < pxa_last_gpio; gpio += 32, i++) {
371                 /* restore level with set/clear */
372                 GPSR(gpio) = saved_gplr[i];
373                 GPCR(gpio) = ~saved_gplr[i];
374
375                 GRER(gpio) = saved_grer[i];
376                 GFER(gpio) = saved_gfer[i];
377                 GPDR(gpio) = saved_gpdr[i];
378         }
379         return 0;
380 }
381 #else
382 #define pxa_gpio_suspend        NULL
383 #define pxa_gpio_resume         NULL
384 #endif
385
386 struct sysdev_class pxa_gpio_sysclass = {
387         .name           = "gpio",
388         .suspend        = pxa_gpio_suspend,
389         .resume         = pxa_gpio_resume,
390 };
391
392 static int __init pxa_gpio_init(void)
393 {
394         return sysdev_class_register(&pxa_gpio_sysclass);
395 }
396
397 core_initcall(pxa_gpio_init);