2 * MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
3 * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
5 * Based on code from Freescale,
6 * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <linux/init.h>
24 #include <linux/irq.h>
25 #include <linux/gpio.h>
26 #include <mach/hardware.h>
27 #include <asm-generic/bug.h>
29 static struct mxc_gpio_port
*mxc_gpio_ports
;
30 static int gpio_table_size
;
32 /* Note: This driver assumes 32 GPIOs are handled in one register */
34 static void _clear_gpio_irqstatus(struct mxc_gpio_port
*port
, u32 index
)
36 __raw_writel(1 << index
, port
->base
+ GPIO_ISR
);
39 static void _set_gpio_irqenable(struct mxc_gpio_port
*port
, u32 index
,
44 l
= __raw_readl(port
->base
+ GPIO_IMR
);
45 l
= (l
& (~(1 << index
))) | (!!enable
<< index
);
46 __raw_writel(l
, port
->base
+ GPIO_IMR
);
49 static void gpio_ack_irq(u32 irq
)
51 u32 gpio
= irq_to_gpio(irq
);
52 _clear_gpio_irqstatus(&mxc_gpio_ports
[gpio
/ 32], gpio
& 0x1f);
55 static void gpio_mask_irq(u32 irq
)
57 u32 gpio
= irq_to_gpio(irq
);
58 _set_gpio_irqenable(&mxc_gpio_ports
[gpio
/ 32], gpio
& 0x1f, 0);
61 static void gpio_unmask_irq(u32 irq
)
63 u32 gpio
= irq_to_gpio(irq
);
64 _set_gpio_irqenable(&mxc_gpio_ports
[gpio
/ 32], gpio
& 0x1f, 1);
67 static int mxc_gpio_get(struct gpio_chip
*chip
, unsigned offset
);
69 static int gpio_set_irq_type(u32 irq
, u32 type
)
71 u32 gpio
= irq_to_gpio(irq
);
72 struct mxc_gpio_port
*port
= &mxc_gpio_ports
[gpio
/ 32];
75 void __iomem
*reg
= port
->base
;
77 port
->both_edges
&= ~(1 << (gpio
& 31));
79 case IRQ_TYPE_EDGE_RISING
:
80 edge
= GPIO_INT_RISE_EDGE
;
82 case IRQ_TYPE_EDGE_FALLING
:
83 edge
= GPIO_INT_FALL_EDGE
;
85 case IRQ_TYPE_EDGE_BOTH
:
86 val
= mxc_gpio_get(&port
->chip
, gpio
& 31);
88 edge
= GPIO_INT_LOW_LEV
;
89 pr_debug("mxc: set GPIO %d to low trigger\n", gpio
);
91 edge
= GPIO_INT_HIGH_LEV
;
92 pr_debug("mxc: set GPIO %d to high trigger\n", gpio
);
94 port
->both_edges
|= 1 << (gpio
& 31);
96 case IRQ_TYPE_LEVEL_LOW
:
97 edge
= GPIO_INT_LOW_LEV
;
99 case IRQ_TYPE_LEVEL_HIGH
:
100 edge
= GPIO_INT_HIGH_LEV
;
106 reg
+= GPIO_ICR1
+ ((gpio
& 0x10) >> 2); /* lower or upper register */
108 val
= __raw_readl(reg
) & ~(0x3 << (bit
<< 1));
109 __raw_writel(val
| (edge
<< (bit
<< 1)), reg
);
110 _clear_gpio_irqstatus(port
, gpio
& 0x1f);
115 static void mxc_flip_edge(struct mxc_gpio_port
*port
, u32 gpio
)
117 void __iomem
*reg
= port
->base
;
121 reg
+= GPIO_ICR1
+ ((gpio
& 0x10) >> 2); /* lower or upper register */
123 val
= __raw_readl(reg
);
124 edge
= (val
>> (bit
<< 1)) & 3;
125 val
&= ~(0x3 << (bit
<< 1));
127 case GPIO_INT_HIGH_LEV
:
128 edge
= GPIO_INT_LOW_LEV
;
129 pr_debug("mxc: switch GPIO %d to low trigger\n", gpio
);
131 case GPIO_INT_LOW_LEV
:
132 edge
= GPIO_INT_HIGH_LEV
;
133 pr_debug("mxc: switch GPIO %d to high trigger\n", gpio
);
136 pr_err("mxc: invalid configuration for GPIO %d: %x\n",
140 __raw_writel(val
| (edge
<< (bit
<< 1)), reg
);
143 /* handle n interrupts in one status register */
144 static void mxc_gpio_irq_handler(struct mxc_gpio_port
*port
, u32 irq_stat
)
148 gpio_irq_no
= port
->virtual_irq_start
;
149 for (; irq_stat
!= 0; irq_stat
>>= 1, gpio_irq_no
++) {
150 u32 gpio
= irq_to_gpio(gpio_irq_no
);
152 if ((irq_stat
& 1) == 0)
155 BUG_ON(!(irq_desc
[gpio_irq_no
].handle_irq
));
157 if (port
->both_edges
& (1 << (gpio
& 31)))
158 mxc_flip_edge(port
, gpio
);
160 irq_desc
[gpio_irq_no
].handle_irq(gpio_irq_no
,
161 &irq_desc
[gpio_irq_no
]);
165 #if defined(CONFIG_ARCH_MX3) || defined(CONFIG_ARCH_MX1)
166 /* MX1 and MX3 has one interrupt *per* gpio port */
167 static void mx3_gpio_irq_handler(u32 irq
, struct irq_desc
*desc
)
170 struct mxc_gpio_port
*port
= (struct mxc_gpio_port
*)get_irq_data(irq
);
172 irq_stat
= __raw_readl(port
->base
+ GPIO_ISR
) &
173 __raw_readl(port
->base
+ GPIO_IMR
);
175 mxc_gpio_irq_handler(port
, irq_stat
);
179 #ifdef CONFIG_ARCH_MX2
180 /* MX2 has one interrupt *for all* gpio ports */
181 static void mx2_gpio_irq_handler(u32 irq
, struct irq_desc
*desc
)
184 u32 irq_msk
, irq_stat
;
185 struct mxc_gpio_port
*port
= (struct mxc_gpio_port
*)get_irq_data(irq
);
187 /* walk through all interrupt status registers */
188 for (i
= 0; i
< gpio_table_size
; i
++) {
189 irq_msk
= __raw_readl(port
[i
].base
+ GPIO_IMR
);
193 irq_stat
= __raw_readl(port
[i
].base
+ GPIO_ISR
) & irq_msk
;
195 mxc_gpio_irq_handler(&port
[i
], irq_stat
);
200 static struct irq_chip gpio_irq_chip
= {
202 .mask
= gpio_mask_irq
,
203 .unmask
= gpio_unmask_irq
,
204 .set_type
= gpio_set_irq_type
,
207 static void _set_gpio_direction(struct gpio_chip
*chip
, unsigned offset
,
210 struct mxc_gpio_port
*port
=
211 container_of(chip
, struct mxc_gpio_port
, chip
);
214 l
= __raw_readl(port
->base
+ GPIO_GDIR
);
219 __raw_writel(l
, port
->base
+ GPIO_GDIR
);
222 static void mxc_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
224 struct mxc_gpio_port
*port
=
225 container_of(chip
, struct mxc_gpio_port
, chip
);
226 void __iomem
*reg
= port
->base
+ GPIO_DR
;
229 l
= (__raw_readl(reg
) & (~(1 << offset
))) | (value
<< offset
);
230 __raw_writel(l
, reg
);
233 static int mxc_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
235 struct mxc_gpio_port
*port
=
236 container_of(chip
, struct mxc_gpio_port
, chip
);
238 return (__raw_readl(port
->base
+ GPIO_PSR
) >> offset
) & 1;
241 static int mxc_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
243 _set_gpio_direction(chip
, offset
, 0);
247 static int mxc_gpio_direction_output(struct gpio_chip
*chip
,
248 unsigned offset
, int value
)
250 mxc_gpio_set(chip
, offset
, value
);
251 _set_gpio_direction(chip
, offset
, 1);
255 int __init
mxc_gpio_init(struct mxc_gpio_port
*port
, int cnt
)
259 /* save for local usage */
260 mxc_gpio_ports
= port
;
261 gpio_table_size
= cnt
;
263 printk(KERN_INFO
"MXC GPIO hardware\n");
265 for (i
= 0; i
< cnt
; i
++) {
266 /* disable the interrupt and clear the status */
267 __raw_writel(0, port
[i
].base
+ GPIO_IMR
);
268 __raw_writel(~0, port
[i
].base
+ GPIO_ISR
);
269 for (j
= port
[i
].virtual_irq_start
;
270 j
< port
[i
].virtual_irq_start
+ 32; j
++) {
271 set_irq_chip(j
, &gpio_irq_chip
);
272 set_irq_handler(j
, handle_edge_irq
);
273 set_irq_flags(j
, IRQF_VALID
);
276 /* register gpio chip */
277 port
[i
].chip
.direction_input
= mxc_gpio_direction_input
;
278 port
[i
].chip
.direction_output
= mxc_gpio_direction_output
;
279 port
[i
].chip
.get
= mxc_gpio_get
;
280 port
[i
].chip
.set
= mxc_gpio_set
;
281 port
[i
].chip
.base
= i
* 32;
282 port
[i
].chip
.ngpio
= 32;
284 /* its a serious configuration bug when it fails */
285 BUG_ON( gpiochip_add(&port
[i
].chip
) < 0 );
287 #if defined(CONFIG_ARCH_MX3) || defined(CONFIG_ARCH_MX1)
288 /* setup one handler for each entry */
289 set_irq_chained_handler(port
[i
].irq
, mx3_gpio_irq_handler
);
290 set_irq_data(port
[i
].irq
, &port
[i
]);
294 #ifdef CONFIG_ARCH_MX2
295 /* setup one handler for all GPIO interrupts */
296 set_irq_chained_handler(port
[0].irq
, mx2_gpio_irq_handler
);
297 set_irq_data(port
[0].irq
, port
);