blob: 198246019028db2c48c4fef78c40d984cbb06828 [file] [log] [blame]
Philipp Zabel1c44f5f2008-02-04 22:28:22 -08001/*
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>
eric miaoe3630db2008-03-04 11:42:26 +080017#include <linux/irq.h>
eric miao663707c2008-03-04 16:13:58 +080018#include <linux/sysdev.h>
Russell Kingfced80c2008-09-06 12:10:45 +010019#include <linux/io.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080020
21#include <asm/gpio.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010022#include <mach/hardware.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010023#include <mach/pxa-regs.h>
24#include <mach/pxa2xx-gpio.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080025
26#include "generic.h"
27
Eric Miaof1647e42008-11-28 14:54:39 +080028#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))
32
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
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080040
41struct pxa_gpio_chip {
42 struct gpio_chip chip;
43 void __iomem *regbase;
44};
45
46int pxa_last_gpio;
47
48/*
49 * Configure pins for GPIO or other functions
50 */
51int pxa_gpio_mode(int gpio_mode)
52{
53 unsigned long flags;
54 int gpio = gpio_mode & GPIO_MD_MASK_NR;
55 int fn = (gpio_mode & GPIO_MD_MASK_FN) >> 8;
56 int gafr;
57
58 if (gpio > pxa_last_gpio)
59 return -EINVAL;
60
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);
68 else
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);
73
74 return 0;
75}
76EXPORT_SYMBOL(pxa_gpio_mode);
77
78static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
79{
80 unsigned long flags;
81 u32 mask = 1 << offset;
82 u32 value;
83 struct pxa_gpio_chip *pxa;
84 void __iomem *gpdr;
85
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);
Eric Miao067455a2008-11-26 18:12:04 +080090 if (__gpio_is_inverted(chip->base + offset))
91 value |= mask;
92 else
93 value &= ~mask;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080094 __raw_writel(value, gpdr);
95 local_irq_restore(flags);
96
97 return 0;
98}
99
100static int pxa_gpio_direction_output(struct gpio_chip *chip,
101 unsigned offset, int value)
102{
103 unsigned long flags;
104 u32 mask = 1 << offset;
105 u32 tmp;
106 struct pxa_gpio_chip *pxa;
107 void __iomem *gpdr;
108
109 pxa = container_of(chip, struct pxa_gpio_chip, chip);
110 __raw_writel(mask,
111 pxa->regbase + (value ? GPSR_OFFSET : GPCR_OFFSET));
112 gpdr = pxa->regbase + GPDR_OFFSET;
113 local_irq_save(flags);
114 tmp = __raw_readl(gpdr);
Eric Miao067455a2008-11-26 18:12:04 +0800115 if (__gpio_is_inverted(chip->base + offset))
116 tmp &= ~mask;
117 else
118 tmp |= mask;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800119 __raw_writel(tmp, gpdr);
120 local_irq_restore(flags);
121
122 return 0;
123}
124
125/*
126 * Return GPIO level
127 */
128static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
129{
130 u32 mask = 1 << offset;
131 struct pxa_gpio_chip *pxa;
132
133 pxa = container_of(chip, struct pxa_gpio_chip, chip);
134 return __raw_readl(pxa->regbase + GPLR_OFFSET) & mask;
135}
136
137/*
138 * Set output GPIO level
139 */
140static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
141{
142 u32 mask = 1 << offset;
143 struct pxa_gpio_chip *pxa;
144
145 pxa = container_of(chip, struct pxa_gpio_chip, chip);
146
147 if (value)
148 __raw_writel(mask, pxa->regbase + GPSR_OFFSET);
149 else
150 __raw_writel(mask, pxa->regbase + GPCR_OFFSET);
151}
152
eric miao0e037bb2008-03-03 13:20:20 +0800153#define GPIO_CHIP(_n) \
154 [_n] = { \
155 .regbase = GPIO##_n##_BASE, \
156 .chip = { \
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, \
162 .base = (_n) * 32, \
163 .ngpio = 32, \
164 }, \
165 }
166
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800167static struct pxa_gpio_chip pxa_gpio_chip[] = {
eric miao0e037bb2008-03-03 13:20:20 +0800168 GPIO_CHIP(0),
169 GPIO_CHIP(1),
170 GPIO_CHIP(2),
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800171#if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
eric miao0e037bb2008-03-03 13:20:20 +0800172 GPIO_CHIP(3),
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800173#endif
174};
175
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800176static void __init pxa_init_gpio_chip(int gpio_nr)
177{
178 int i, gpio;
179
180 /* add a GPIO chip for each register bank.
181 * the last PXA25x register only contains 21 GPIOs
182 */
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);
187 }
188}
189
eric miaoe3630db2008-03-04 11:42:26 +0800190/*
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.
194 */
195
Dmitry Baryshkovd8a42fc2008-04-19 10:42:18 +0100196static unsigned long GPIO_IRQ_rising_edge[4];
197static unsigned long GPIO_IRQ_falling_edge[4];
198static unsigned long GPIO_IRQ_mask[4];
eric miaoe3630db2008-03-04 11:42:26 +0800199
200static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
201{
202 int gpio, idx;
203
204 gpio = IRQ_TO_GPIO(irq);
205 idx = gpio >> 5;
206
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
210 */
Eric Miao067455a2008-11-26 18:12:04 +0800211 if ((GPIO_IRQ_rising_edge[idx] & GPIO_bit(gpio)) ||
212 (GPIO_IRQ_falling_edge[idx] & GPIO_bit(gpio)))
eric miaoe3630db2008-03-04 11:42:26 +0800213 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800214
215 if (__gpio_is_occupied(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800216 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800217
eric miaoe3630db2008-03-04 11:42:26 +0800218 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
219 }
220
Eric Miao067455a2008-11-26 18:12:04 +0800221 if (__gpio_is_inverted(gpio))
222 GPDR(gpio) |= GPIO_bit(gpio);
223 else
224 GPDR(gpio) &= ~GPIO_bit(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800225
226 if (type & IRQ_TYPE_EDGE_RISING)
227 __set_bit(gpio, GPIO_IRQ_rising_edge);
228 else
229 __clear_bit(gpio, GPIO_IRQ_rising_edge);
230
231 if (type & IRQ_TYPE_EDGE_FALLING)
232 __set_bit(gpio, GPIO_IRQ_falling_edge);
233 else
234 __clear_bit(gpio, GPIO_IRQ_falling_edge);
235
236 GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
237 GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
238
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" : ""));
242 return 0;
243}
244
245/*
eric miaoe3630db2008-03-04 11:42:26 +0800246 * Demux handler for GPIO>=2 edge detect interrupts
247 */
248
249#define GEDR_BITS (sizeof(gedr) * BITS_PER_BYTE)
250
251static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
252{
253 int loop, bit, n;
254 unsigned long gedr[4];
255
256 do {
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];
261
262 GEDR0 = gedr[0]; GEDR1 = gedr[1];
263 GEDR2 = gedr[2]; GEDR3 = gedr[3];
264
265 loop = 0;
266 bit = find_first_bit(gedr, GEDR_BITS);
267 while (bit < GEDR_BITS) {
268 loop = 1;
269
270 n = PXA_GPIO_IRQ_BASE + bit;
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100271 generic_handle_irq(n);
eric miaoe3630db2008-03-04 11:42:26 +0800272
273 bit = find_next_bit(gedr, GEDR_BITS, bit + 1);
274 }
275 } while (loop);
276}
277
278static void pxa_ack_muxed_gpio(unsigned int irq)
279{
280 int gpio = irq - IRQ_GPIO(2) + 2;
281 GEDR(gpio) = GPIO_bit(gpio);
282}
283
284static void pxa_mask_muxed_gpio(unsigned int irq)
285{
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);
290}
291
292static void pxa_unmask_muxed_gpio(unsigned int irq)
293{
294 int gpio = irq - IRQ_GPIO(2) + 2;
295 int idx = gpio >> 5;
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];
299}
300
301static struct irq_chip pxa_muxed_gpio_chip = {
302 .name = "GPIO",
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,
307};
308
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800309void __init pxa_init_gpio(int mux_irq, int start, int end, set_wake_t fn)
eric miaoe3630db2008-03-04 11:42:26 +0800310{
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800311 int irq, i;
eric miaoe3630db2008-03-04 11:42:26 +0800312
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800313 pxa_last_gpio = end;
eric miaoe3630db2008-03-04 11:42:26 +0800314
315 /* clear all GPIO edge detects */
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800316 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];
eric miaoe3630db2008-03-04 11:42:26 +0800320 }
321
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800322 for (irq = gpio_to_irq(start); irq <= gpio_to_irq(end); irq++) {
eric miaoe3630db2008-03-04 11:42:26 +0800323 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);
326 }
327
328 /* Install handler for GPIO>=2 edge detect interrupts */
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800329 set_irq_chained_handler(mux_irq, pxa_gpio_demux_handler);
eric miaob9e25ac2008-03-04 14:19:58 +0800330 pxa_muxed_gpio_chip.set_wake = fn;
eric miaoe3630db2008-03-04 11:42:26 +0800331
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800332 /* Initialize GPIO chips */
333 pxa_init_gpio_chip(end + 1);
eric miaoe3630db2008-03-04 11:42:26 +0800334}
eric miao663707c2008-03-04 16:13:58 +0800335
336#ifdef CONFIG_PM
337
338static unsigned long saved_gplr[4];
339static unsigned long saved_gpdr[4];
340static unsigned long saved_grer[4];
341static unsigned long saved_gfer[4];
342
343static int pxa_gpio_suspend(struct sys_device *dev, pm_message_t state)
344{
345 int i, gpio;
346
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);
352
353 /* Clear GPIO transition detect bits */
354 GEDR(gpio) = GEDR(gpio);
355 }
356 return 0;
357}
358
359static int pxa_gpio_resume(struct sys_device *dev)
360{
361 int i, gpio;
362
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];
367
368 GRER(gpio) = saved_grer[i];
369 GFER(gpio) = saved_gfer[i];
370 GPDR(gpio) = saved_gpdr[i];
371 }
372 return 0;
373}
374#else
375#define pxa_gpio_suspend NULL
376#define pxa_gpio_resume NULL
377#endif
378
379struct sysdev_class pxa_gpio_sysclass = {
380 .name = "gpio",
381 .suspend = pxa_gpio_suspend,
382 .resume = pxa_gpio_resume,
383};
384
385static int __init pxa_gpio_init(void)
386{
387 return sysdev_class_register(&pxa_gpio_sysclass);
388}
389
390core_initcall(pxa_gpio_init);