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 (C) 2004-2010 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>
23 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/gpio.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <mach/hardware.h>
30 #include <asm-generic/bug.h>
32 struct mxc_gpio_port
{
33 struct list_head node
;
37 int virtual_irq_start
;
38 struct gpio_chip chip
;
44 * MX2 has one interrupt *for all* gpio ports. The list is used
45 * to save the references to all ports, so that mx2_gpio_irq_handler
46 * can walk through all interrupt status registers.
48 static LIST_HEAD(mxc_gpio_ports
);
50 #define cpu_is_mx1_mx2() (cpu_is_mx1() || cpu_is_mx2())
52 #define GPIO_DR (cpu_is_mx1_mx2() ? 0x1c : 0x00)
53 #define GPIO_GDIR (cpu_is_mx1_mx2() ? 0x00 : 0x04)
54 #define GPIO_PSR (cpu_is_mx1_mx2() ? 0x24 : 0x08)
55 #define GPIO_ICR1 (cpu_is_mx1_mx2() ? 0x28 : 0x0C)
56 #define GPIO_ICR2 (cpu_is_mx1_mx2() ? 0x2C : 0x10)
57 #define GPIO_IMR (cpu_is_mx1_mx2() ? 0x30 : 0x14)
58 #define GPIO_ISR (cpu_is_mx1_mx2() ? 0x34 : 0x18)
60 #define GPIO_INT_LOW_LEV (cpu_is_mx1_mx2() ? 0x3 : 0x0)
61 #define GPIO_INT_HIGH_LEV (cpu_is_mx1_mx2() ? 0x2 : 0x1)
62 #define GPIO_INT_RISE_EDGE (cpu_is_mx1_mx2() ? 0x0 : 0x2)
63 #define GPIO_INT_FALL_EDGE (cpu_is_mx1_mx2() ? 0x1 : 0x3)
64 #define GPIO_INT_NONE 0x4
66 /* Note: This driver assumes 32 GPIOs are handled in one register */
68 static void _clear_gpio_irqstatus(struct mxc_gpio_port
*port
, u32 index
)
70 writel(1 << index
, port
->base
+ GPIO_ISR
);
73 static void _set_gpio_irqenable(struct mxc_gpio_port
*port
, u32 index
,
78 l
= readl(port
->base
+ GPIO_IMR
);
79 l
= (l
& (~(1 << index
))) | (!!enable
<< index
);
80 writel(l
, port
->base
+ GPIO_IMR
);
83 static void gpio_ack_irq(struct irq_data
*d
)
85 struct mxc_gpio_port
*port
= irq_data_get_irq_chip_data(d
);
86 u32 gpio
= irq_to_gpio(d
->irq
);
87 _clear_gpio_irqstatus(port
, gpio
& 0x1f);
90 static void gpio_mask_irq(struct irq_data
*d
)
92 struct mxc_gpio_port
*port
= irq_data_get_irq_chip_data(d
);
93 u32 gpio
= irq_to_gpio(d
->irq
);
94 _set_gpio_irqenable(port
, gpio
& 0x1f, 0);
97 static void gpio_unmask_irq(struct irq_data
*d
)
99 struct mxc_gpio_port
*port
= irq_data_get_irq_chip_data(d
);
100 u32 gpio
= irq_to_gpio(d
->irq
);
101 _set_gpio_irqenable(port
, gpio
& 0x1f, 1);
104 static int mxc_gpio_get(struct gpio_chip
*chip
, unsigned offset
);
106 static int gpio_set_irq_type(struct irq_data
*d
, u32 type
)
108 u32 gpio
= irq_to_gpio(d
->irq
);
109 struct mxc_gpio_port
*port
= irq_data_get_irq_chip_data(d
);
112 void __iomem
*reg
= port
->base
;
114 port
->both_edges
&= ~(1 << (gpio
& 31));
116 case IRQ_TYPE_EDGE_RISING
:
117 edge
= GPIO_INT_RISE_EDGE
;
119 case IRQ_TYPE_EDGE_FALLING
:
120 edge
= GPIO_INT_FALL_EDGE
;
122 case IRQ_TYPE_EDGE_BOTH
:
123 val
= mxc_gpio_get(&port
->chip
, gpio
& 31);
125 edge
= GPIO_INT_LOW_LEV
;
126 pr_debug("mxc: set GPIO %d to low trigger\n", gpio
);
128 edge
= GPIO_INT_HIGH_LEV
;
129 pr_debug("mxc: set GPIO %d to high trigger\n", gpio
);
131 port
->both_edges
|= 1 << (gpio
& 31);
133 case IRQ_TYPE_LEVEL_LOW
:
134 edge
= GPIO_INT_LOW_LEV
;
136 case IRQ_TYPE_LEVEL_HIGH
:
137 edge
= GPIO_INT_HIGH_LEV
;
143 reg
+= GPIO_ICR1
+ ((gpio
& 0x10) >> 2); /* lower or upper register */
145 val
= readl(reg
) & ~(0x3 << (bit
<< 1));
146 writel(val
| (edge
<< (bit
<< 1)), reg
);
147 _clear_gpio_irqstatus(port
, gpio
& 0x1f);
152 static void mxc_flip_edge(struct mxc_gpio_port
*port
, u32 gpio
)
154 void __iomem
*reg
= port
->base
;
158 reg
+= GPIO_ICR1
+ ((gpio
& 0x10) >> 2); /* lower or upper register */
161 edge
= (val
>> (bit
<< 1)) & 3;
162 val
&= ~(0x3 << (bit
<< 1));
163 if (edge
== GPIO_INT_HIGH_LEV
) {
164 edge
= GPIO_INT_LOW_LEV
;
165 pr_debug("mxc: switch GPIO %d to low trigger\n", gpio
);
166 } else if (edge
== GPIO_INT_LOW_LEV
) {
167 edge
= GPIO_INT_HIGH_LEV
;
168 pr_debug("mxc: switch GPIO %d to high trigger\n", gpio
);
170 pr_err("mxc: invalid configuration for GPIO %d: %x\n",
174 writel(val
| (edge
<< (bit
<< 1)), reg
);
177 /* handle 32 interrupts in one status register */
178 static void mxc_gpio_irq_handler(struct mxc_gpio_port
*port
, u32 irq_stat
)
180 u32 gpio_irq_no_base
= port
->virtual_irq_start
;
182 while (irq_stat
!= 0) {
183 int irqoffset
= fls(irq_stat
) - 1;
185 if (port
->both_edges
& (1 << irqoffset
))
186 mxc_flip_edge(port
, irqoffset
);
188 generic_handle_irq(gpio_irq_no_base
+ irqoffset
);
190 irq_stat
&= ~(1 << irqoffset
);
194 /* MX1 and MX3 has one interrupt *per* gpio port */
195 static void mx3_gpio_irq_handler(u32 irq
, struct irq_desc
*desc
)
198 struct mxc_gpio_port
*port
= irq_get_handler_data(irq
);
200 irq_stat
= readl(port
->base
+ GPIO_ISR
) & readl(port
->base
+ GPIO_IMR
);
202 mxc_gpio_irq_handler(port
, irq_stat
);
205 /* MX2 has one interrupt *for all* gpio ports */
206 static void mx2_gpio_irq_handler(u32 irq
, struct irq_desc
*desc
)
208 u32 irq_msk
, irq_stat
;
209 struct mxc_gpio_port
*port
;
211 /* walk through all interrupt status registers */
212 list_for_each_entry(port
, &mxc_gpio_ports
, node
) {
213 irq_msk
= readl(port
->base
+ GPIO_IMR
);
217 irq_stat
= readl(port
->base
+ GPIO_ISR
) & irq_msk
;
219 mxc_gpio_irq_handler(port
, irq_stat
);
224 * Set interrupt number "irq" in the GPIO as a wake-up source.
225 * While system is running, all registered GPIO interrupts need to have
226 * wake-up enabled. When system is suspended, only selected GPIO interrupts
227 * need to have wake-up enabled.
228 * @param irq interrupt source number
229 * @param enable enable as wake-up if equal to non-zero
230 * @return This function returns 0 on success.
232 static int gpio_set_wake_irq(struct irq_data
*d
, u32 enable
)
234 u32 gpio
= irq_to_gpio(d
->irq
);
235 u32 gpio_idx
= gpio
& 0x1F;
236 struct mxc_gpio_port
*port
= irq_data_get_irq_chip_data(d
);
239 if (port
->irq_high
&& (gpio_idx
>= 16))
240 enable_irq_wake(port
->irq_high
);
242 enable_irq_wake(port
->irq
);
244 if (port
->irq_high
&& (gpio_idx
>= 16))
245 disable_irq_wake(port
->irq_high
);
247 disable_irq_wake(port
->irq
);
253 static struct irq_chip gpio_irq_chip
= {
255 .irq_ack
= gpio_ack_irq
,
256 .irq_mask
= gpio_mask_irq
,
257 .irq_unmask
= gpio_unmask_irq
,
258 .irq_set_type
= gpio_set_irq_type
,
259 .irq_set_wake
= gpio_set_wake_irq
,
262 static void _set_gpio_direction(struct gpio_chip
*chip
, unsigned offset
,
265 struct mxc_gpio_port
*port
=
266 container_of(chip
, struct mxc_gpio_port
, chip
);
270 spin_lock_irqsave(&port
->lock
, flags
);
271 l
= readl(port
->base
+ GPIO_GDIR
);
276 writel(l
, port
->base
+ GPIO_GDIR
);
277 spin_unlock_irqrestore(&port
->lock
, flags
);
280 static void mxc_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
282 struct mxc_gpio_port
*port
=
283 container_of(chip
, struct mxc_gpio_port
, chip
);
284 void __iomem
*reg
= port
->base
+ GPIO_DR
;
288 spin_lock_irqsave(&port
->lock
, flags
);
289 l
= (readl(reg
) & (~(1 << offset
))) | (!!value
<< offset
);
291 spin_unlock_irqrestore(&port
->lock
, flags
);
294 static int mxc_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
296 struct mxc_gpio_port
*port
=
297 container_of(chip
, struct mxc_gpio_port
, chip
);
299 return (readl(port
->base
+ GPIO_PSR
) >> offset
) & 1;
302 static int mxc_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
304 _set_gpio_direction(chip
, offset
, 0);
308 static int mxc_gpio_direction_output(struct gpio_chip
*chip
,
309 unsigned offset
, int value
)
311 mxc_gpio_set(chip
, offset
, value
);
312 _set_gpio_direction(chip
, offset
, 1);
317 * This lock class tells lockdep that GPIO irqs are in a different
318 * category than their parents, so it won't report false recursion.
320 static struct lock_class_key gpio_lock_class
;
322 static int __devinit
mxc_gpio_probe(struct platform_device
*pdev
)
324 struct mxc_gpio_port
*port
;
325 struct resource
*iores
;
328 port
= kzalloc(sizeof(struct mxc_gpio_port
), GFP_KERNEL
);
332 port
->virtual_irq_start
= MXC_GPIO_IRQ_START
+ pdev
->id
* 32;
334 iores
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
340 if (!request_mem_region(iores
->start
, resource_size(iores
),
346 port
->base
= ioremap(iores
->start
, resource_size(iores
));
349 goto out_release_mem
;
352 port
->irq_high
= platform_get_irq(pdev
, 1);
353 port
->irq
= platform_get_irq(pdev
, 0);
359 /* disable the interrupt and clear the status */
360 writel(0, port
->base
+ GPIO_IMR
);
361 writel(~0, port
->base
+ GPIO_ISR
);
363 for (i
= port
->virtual_irq_start
;
364 i
< port
->virtual_irq_start
+ 32; i
++) {
365 irq_set_lockdep_class(i
, &gpio_lock_class
);
366 irq_set_chip_and_handler(i
, &gpio_irq_chip
, handle_level_irq
);
367 set_irq_flags(i
, IRQF_VALID
);
368 irq_set_chip_data(i
, port
);
372 /* setup one handler for all GPIO interrupts */
374 irq_set_chained_handler(port
->irq
,
375 mx2_gpio_irq_handler
);
377 /* setup one handler for each entry */
378 irq_set_chained_handler(port
->irq
, mx3_gpio_irq_handler
);
379 irq_set_handler_data(port
->irq
, port
);
380 if (port
->irq_high
> 0) {
381 /* setup handler for GPIO 16 to 31 */
382 irq_set_chained_handler(port
->irq_high
,
383 mx3_gpio_irq_handler
);
384 irq_set_handler_data(port
->irq_high
, port
);
388 /* register gpio chip */
389 port
->chip
.direction_input
= mxc_gpio_direction_input
;
390 port
->chip
.direction_output
= mxc_gpio_direction_output
;
391 port
->chip
.get
= mxc_gpio_get
;
392 port
->chip
.set
= mxc_gpio_set
;
393 port
->chip
.base
= pdev
->id
* 32;
394 port
->chip
.ngpio
= 32;
396 spin_lock_init(&port
->lock
);
398 err
= gpiochip_add(&port
->chip
);
402 list_add_tail(&port
->node
, &mxc_gpio_ports
);
409 release_mem_region(iores
->start
, resource_size(iores
));
412 dev_info(&pdev
->dev
, "%s failed with errno %d\n", __func__
, err
);
416 static struct platform_driver mxc_gpio_driver
= {
419 .owner
= THIS_MODULE
,
421 .probe
= mxc_gpio_probe
,
424 static int __init
gpio_mxc_init(void)
426 return platform_driver_register(&mxc_gpio_driver
);
428 postcore_initcall(gpio_mxc_init
);
430 MODULE_AUTHOR("Freescale Semiconductor, "
431 "Daniel Mack <danielncaiaq.de>, "
432 "Juergen Beisert <kernel@pengutronix.de>");
433 MODULE_DESCRIPTION("Freescale MXC GPIO");
434 MODULE_LICENSE("GPL");