[ARM] pxa: cleanup the coding style of pxa_gpio_set_type()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / mach-pxa / irq.c
blob4fd4560dd3ad72d6ac847b500475371fc01a4ccf
1 /*
2 * linux/arch/arm/mach-pxa/irq.c
4 * Generic PXA IRQ handling, GPIO IRQ demultiplexing, etc.
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/interrupt.h>
18 #include <linux/sysdev.h>
20 #include <asm/hardware.h>
21 #include <asm/irq.h>
22 #include <asm/mach/irq.h>
23 #include <asm/arch/pxa-regs.h>
25 #include "generic.h"
29 * This is for peripheral IRQs internal to the PXA chip.
32 static void pxa_mask_low_irq(unsigned int irq)
34 ICMR &= ~(1 << irq);
37 static void pxa_unmask_low_irq(unsigned int irq)
39 ICMR |= (1 << irq);
42 static struct irq_chip pxa_internal_chip_low = {
43 .name = "SC",
44 .ack = pxa_mask_low_irq,
45 .mask = pxa_mask_low_irq,
46 .unmask = pxa_unmask_low_irq,
49 void __init pxa_init_irq_low(void)
51 int irq;
53 /* disable all IRQs */
54 ICMR = 0;
56 /* all IRQs are IRQ, not FIQ */
57 ICLR = 0;
59 /* only unmasked interrupts kick us out of idle */
60 ICCR = 1;
62 for (irq = PXA_IRQ(0); irq <= PXA_IRQ(31); irq++) {
63 set_irq_chip(irq, &pxa_internal_chip_low);
64 set_irq_handler(irq, handle_level_irq);
65 set_irq_flags(irq, IRQF_VALID);
69 #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
72 * This is for the second set of internal IRQs as found on the PXA27x.
75 static void pxa_mask_high_irq(unsigned int irq)
77 ICMR2 &= ~(1 << (irq - 32));
80 static void pxa_unmask_high_irq(unsigned int irq)
82 ICMR2 |= (1 << (irq - 32));
85 static struct irq_chip pxa_internal_chip_high = {
86 .name = "SC-hi",
87 .ack = pxa_mask_high_irq,
88 .mask = pxa_mask_high_irq,
89 .unmask = pxa_unmask_high_irq,
92 void __init pxa_init_irq_high(void)
94 int irq;
96 ICMR2 = 0;
97 ICLR2 = 0;
99 for (irq = PXA_IRQ(32); irq < PXA_IRQ(64); irq++) {
100 set_irq_chip(irq, &pxa_internal_chip_high);
101 set_irq_handler(irq, handle_level_irq);
102 set_irq_flags(irq, IRQF_VALID);
105 #endif
108 * PXA GPIO edge detection for IRQs:
109 * IRQs are generated on Falling-Edge, Rising-Edge, or both.
110 * Use this instead of directly setting GRER/GFER.
113 static long GPIO_IRQ_rising_edge[4];
114 static long GPIO_IRQ_falling_edge[4];
115 static long GPIO_IRQ_mask[4];
117 static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
119 int gpio, idx;
121 gpio = IRQ_TO_GPIO(irq);
122 idx = gpio >> 5;
124 if (type == IRQ_TYPE_PROBE) {
125 /* Don't mess with enabled GPIOs using preconfigured edges or
126 * GPIOs set to alternate function or to output during probe
128 if ((GPIO_IRQ_rising_edge[idx] |
129 GPIO_IRQ_falling_edge[idx] |
130 GPDR(gpio)) & GPIO_bit(gpio))
131 return 0;
132 if (GAFR(gpio) & (0x3 << (((gpio) & 0xf)*2)))
133 return 0;
134 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
137 pxa_gpio_mode(gpio | GPIO_IN);
139 if (type & IRQ_TYPE_EDGE_RISING)
140 __set_bit(gpio, GPIO_IRQ_rising_edge);
141 else
142 __clear_bit(gpio, GPIO_IRQ_rising_edge);
144 if (type & IRQ_TYPE_EDGE_FALLING)
145 __set_bit(gpio, GPIO_IRQ_falling_edge);
146 else
147 __clear_bit(gpio, GPIO_IRQ_falling_edge);
149 GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
150 GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
152 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, irq, gpio,
153 ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
154 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
155 return 0;
159 * GPIO IRQs must be acknowledged. This is for GPIO 0 and 1.
162 static void pxa_ack_low_gpio(unsigned int irq)
164 GEDR0 = (1 << (irq - IRQ_GPIO0));
167 static void pxa_mask_low_gpio(unsigned int irq)
169 ICMR &= ~(1 << (irq - PXA_IRQ(0)));
172 static void pxa_unmask_low_gpio(unsigned int irq)
174 ICMR |= 1 << (irq - PXA_IRQ(0));
177 static struct irq_chip pxa_low_gpio_chip = {
178 .name = "GPIO-l",
179 .ack = pxa_ack_low_gpio,
180 .mask = pxa_mask_low_gpio,
181 .unmask = pxa_unmask_low_gpio,
182 .set_type = pxa_gpio_irq_type,
186 * Demux handler for GPIO>=2 edge detect interrupts
189 #define GEDR_BITS (sizeof(gedr) * BITS_PER_BYTE)
191 static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
193 int loop, bit, n;
194 unsigned long gedr[4];
196 do {
197 gedr[0] = GEDR0 & GPIO_IRQ_mask[0] & ~3;
198 gedr[1] = GEDR1 & GPIO_IRQ_mask[1];
199 gedr[2] = GEDR2 & GPIO_IRQ_mask[2];
200 gedr[3] = GEDR3 & GPIO_IRQ_mask[3];
202 GEDR0 = gedr[0]; GEDR1 = gedr[1];
203 GEDR2 = gedr[2]; GEDR3 = gedr[3];
205 loop = 0;
206 bit = find_first_bit(gedr, GEDR_BITS);
207 while (bit < GEDR_BITS) {
208 loop = 1;
210 n = PXA_GPIO_IRQ_BASE + bit;
211 desc_handle_irq(n, irq_desc + n);
213 bit = find_next_bit(gedr, GEDR_BITS, bit + 1);
215 } while (loop);
218 static void pxa_ack_muxed_gpio(unsigned int irq)
220 int gpio = irq - IRQ_GPIO(2) + 2;
221 GEDR(gpio) = GPIO_bit(gpio);
224 static void pxa_mask_muxed_gpio(unsigned int irq)
226 int gpio = irq - IRQ_GPIO(2) + 2;
227 __clear_bit(gpio, GPIO_IRQ_mask);
228 GRER(gpio) &= ~GPIO_bit(gpio);
229 GFER(gpio) &= ~GPIO_bit(gpio);
232 static void pxa_unmask_muxed_gpio(unsigned int irq)
234 int gpio = irq - IRQ_GPIO(2) + 2;
235 int idx = gpio >> 5;
236 __set_bit(gpio, GPIO_IRQ_mask);
237 GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
238 GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
241 static struct irq_chip pxa_muxed_gpio_chip = {
242 .name = "GPIO",
243 .ack = pxa_ack_muxed_gpio,
244 .mask = pxa_mask_muxed_gpio,
245 .unmask = pxa_unmask_muxed_gpio,
246 .set_type = pxa_gpio_irq_type,
249 void __init pxa_init_irq_gpio(int gpio_nr)
251 int irq, i;
253 pxa_last_gpio = gpio_nr - 1;
255 /* clear all GPIO edge detects */
256 for (i = 0; i < gpio_nr; i += 32) {
257 GFER(i) = 0;
258 GRER(i) = 0;
259 GEDR(i) = GEDR(i);
262 /* GPIO 0 and 1 must have their mask bit always set */
263 GPIO_IRQ_mask[0] = 3;
265 for (irq = IRQ_GPIO0; irq <= IRQ_GPIO1; irq++) {
266 set_irq_chip(irq, &pxa_low_gpio_chip);
267 set_irq_handler(irq, handle_edge_irq);
268 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
271 for (irq = IRQ_GPIO(2); irq < IRQ_GPIO(gpio_nr); irq++) {
272 set_irq_chip(irq, &pxa_muxed_gpio_chip);
273 set_irq_handler(irq, handle_edge_irq);
274 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
277 /* Install handler for GPIO>=2 edge detect interrupts */
278 set_irq_chained_handler(IRQ_GPIO_2_x, pxa_gpio_demux_handler);
280 pxa_init_gpio(gpio_nr);
283 void __init pxa_init_gpio_set_wake(int (*set_wake)(unsigned int, unsigned int))
285 pxa_low_gpio_chip.set_wake = set_wake;
286 pxa_muxed_gpio_chip.set_wake = set_wake;
289 void __init pxa_init_irq_set_wake(int (*set_wake)(unsigned int, unsigned int))
291 pxa_internal_chip_low.set_wake = set_wake;
292 #ifdef CONFIG_PXA27x
293 pxa_internal_chip_high.set_wake = set_wake;
294 #endif
295 pxa_init_gpio_set_wake(set_wake);
298 #ifdef CONFIG_PM
299 static unsigned long saved_icmr[2];
301 static int pxa_irq_suspend(struct sys_device *dev, pm_message_t state)
303 switch (dev->id) {
304 case 0:
305 saved_icmr[0] = ICMR;
306 ICMR = 0;
307 break;
308 #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
309 case 1:
310 saved_icmr[1] = ICMR2;
311 ICMR2 = 0;
312 break;
313 #endif
314 default:
315 return -EINVAL;
318 return 0;
321 static int pxa_irq_resume(struct sys_device *dev)
323 switch (dev->id) {
324 case 0:
325 ICMR = saved_icmr[0];
326 ICLR = 0;
327 ICCR = 1;
328 break;
329 #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
330 case 1:
331 ICMR2 = saved_icmr[1];
332 ICLR2 = 0;
333 break;
334 #endif
335 default:
336 return -EINVAL;
339 return 0;
341 #else
342 #define pxa_irq_suspend NULL
343 #define pxa_irq_resume NULL
344 #endif
346 struct sysdev_class pxa_irq_sysclass = {
347 .name = "irq",
348 .suspend = pxa_irq_suspend,
349 .resume = pxa_irq_resume,
352 static int __init pxa_irq_init(void)
354 return sysdev_class_register(&pxa_irq_sysclass);
357 core_initcall(pxa_irq_init);