blob: 844079a83f25630f3390dc291357bc245b521c93 [file] [log] [blame]
Juergen Beisert07bd1a62008-07-05 10:02:49 +02001/*
2 * MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
3 * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
4 *
5 * Based on code from Freescale,
Dinh Nguyene24798e2010-04-22 16:28:42 +03006 * Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
Juergen Beisert07bd1a62008-07-05 10:02:49 +02007 *
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.
16 *
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.
20 */
21
22#include <linux/init.h>
Dinh Nguyena3484ff2010-10-23 09:12:48 -050023#include <linux/interrupt.h>
Juergen Beisert07bd1a62008-07-05 10:02:49 +020024#include <linux/io.h>
25#include <linux/irq.h>
26#include <linux/gpio.h>
Shawn Guob78d8e52011-06-06 00:07:55 +080027#include <linux/platform_device.h>
28#include <linux/slab.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/hardware.h>
Juergen Beisert07bd1a62008-07-05 10:02:49 +020030#include <asm-generic/bug.h>
31
Shawn Guob78d8e52011-06-06 00:07:55 +080032struct mxc_gpio_port {
33 struct list_head node;
34 void __iomem *base;
35 int irq;
36 int irq_high;
37 int virtual_irq_start;
38 struct gpio_chip chip;
39 u32 both_edges;
40 spinlock_t lock;
41};
42
43/*
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.
47 */
48static LIST_HEAD(mxc_gpio_ports);
Juergen Beisert07bd1a62008-07-05 10:02:49 +020049
Sascha Hauer494f22d2009-05-27 18:26:51 +020050#define cpu_is_mx1_mx2() (cpu_is_mx1() || cpu_is_mx2())
51
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)
Sascha Hauer494f22d2009-05-27 18:26:51 +020059
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
65
Juergen Beisert07bd1a62008-07-05 10:02:49 +020066/* Note: This driver assumes 32 GPIOs are handled in one register */
67
68static void _clear_gpio_irqstatus(struct mxc_gpio_port *port, u32 index)
69{
Shawn Guob78d8e52011-06-06 00:07:55 +080070 writel(1 << index, port->base + GPIO_ISR);
Juergen Beisert07bd1a62008-07-05 10:02:49 +020071}
72
73static void _set_gpio_irqenable(struct mxc_gpio_port *port, u32 index,
74 int enable)
75{
76 u32 l;
77
Shawn Guob78d8e52011-06-06 00:07:55 +080078 l = readl(port->base + GPIO_IMR);
Juergen Beisert07bd1a62008-07-05 10:02:49 +020079 l = (l & (~(1 << index))) | (!!enable << index);
Shawn Guob78d8e52011-06-06 00:07:55 +080080 writel(l, port->base + GPIO_IMR);
Juergen Beisert07bd1a62008-07-05 10:02:49 +020081}
82
Lennert Buytenhek4d935792010-11-29 11:16:23 +010083static void gpio_ack_irq(struct irq_data *d)
Juergen Beisert07bd1a62008-07-05 10:02:49 +020084{
Shawn Guob78d8e52011-06-06 00:07:55 +080085 struct mxc_gpio_port *port = irq_data_get_irq_chip_data(d);
Lennert Buytenhek4d935792010-11-29 11:16:23 +010086 u32 gpio = irq_to_gpio(d->irq);
Shawn Guob78d8e52011-06-06 00:07:55 +080087 _clear_gpio_irqstatus(port, gpio & 0x1f);
Juergen Beisert07bd1a62008-07-05 10:02:49 +020088}
89
Lennert Buytenhek4d935792010-11-29 11:16:23 +010090static void gpio_mask_irq(struct irq_data *d)
Juergen Beisert07bd1a62008-07-05 10:02:49 +020091{
Shawn Guob78d8e52011-06-06 00:07:55 +080092 struct mxc_gpio_port *port = irq_data_get_irq_chip_data(d);
Lennert Buytenhek4d935792010-11-29 11:16:23 +010093 u32 gpio = irq_to_gpio(d->irq);
Shawn Guob78d8e52011-06-06 00:07:55 +080094 _set_gpio_irqenable(port, gpio & 0x1f, 0);
Juergen Beisert07bd1a62008-07-05 10:02:49 +020095}
96
Lennert Buytenhek4d935792010-11-29 11:16:23 +010097static void gpio_unmask_irq(struct irq_data *d)
Juergen Beisert07bd1a62008-07-05 10:02:49 +020098{
Shawn Guob78d8e52011-06-06 00:07:55 +080099 struct mxc_gpio_port *port = irq_data_get_irq_chip_data(d);
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100100 u32 gpio = irq_to_gpio(d->irq);
Shawn Guob78d8e52011-06-06 00:07:55 +0800101 _set_gpio_irqenable(port, gpio & 0x1f, 1);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200102}
103
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100104static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset);
105
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100106static int gpio_set_irq_type(struct irq_data *d, u32 type)
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200107{
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100108 u32 gpio = irq_to_gpio(d->irq);
Shawn Guob78d8e52011-06-06 00:07:55 +0800109 struct mxc_gpio_port *port = irq_data_get_irq_chip_data(d);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200110 u32 bit, val;
111 int edge;
112 void __iomem *reg = port->base;
113
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100114 port->both_edges &= ~(1 << (gpio & 31));
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200115 switch (type) {
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100116 case IRQ_TYPE_EDGE_RISING:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200117 edge = GPIO_INT_RISE_EDGE;
118 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100119 case IRQ_TYPE_EDGE_FALLING:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200120 edge = GPIO_INT_FALL_EDGE;
121 break;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100122 case IRQ_TYPE_EDGE_BOTH:
123 val = mxc_gpio_get(&port->chip, gpio & 31);
124 if (val) {
125 edge = GPIO_INT_LOW_LEV;
126 pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
127 } else {
128 edge = GPIO_INT_HIGH_LEV;
129 pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
130 }
131 port->both_edges |= 1 << (gpio & 31);
132 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100133 case IRQ_TYPE_LEVEL_LOW:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200134 edge = GPIO_INT_LOW_LEV;
135 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100136 case IRQ_TYPE_LEVEL_HIGH:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200137 edge = GPIO_INT_HIGH_LEV;
138 break;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100139 default:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200140 return -EINVAL;
141 }
142
143 reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
144 bit = gpio & 0xf;
Shawn Guob78d8e52011-06-06 00:07:55 +0800145 val = readl(reg) & ~(0x3 << (bit << 1));
146 writel(val | (edge << (bit << 1)), reg);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200147 _clear_gpio_irqstatus(port, gpio & 0x1f);
148
149 return 0;
150}
151
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100152static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
153{
154 void __iomem *reg = port->base;
155 u32 bit, val;
156 int edge;
157
158 reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
159 bit = gpio & 0xf;
Shawn Guob78d8e52011-06-06 00:07:55 +0800160 val = readl(reg);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100161 edge = (val >> (bit << 1)) & 3;
162 val &= ~(0x3 << (bit << 1));
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100163 if (edge == GPIO_INT_HIGH_LEV) {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100164 edge = GPIO_INT_LOW_LEV;
165 pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100166 } else if (edge == GPIO_INT_LOW_LEV) {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100167 edge = GPIO_INT_HIGH_LEV;
168 pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100169 } else {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100170 pr_err("mxc: invalid configuration for GPIO %d: %x\n",
171 gpio, edge);
172 return;
173 }
Shawn Guob78d8e52011-06-06 00:07:55 +0800174 writel(val | (edge << (bit << 1)), reg);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100175}
176
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100177/* handle 32 interrupts in one status register */
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200178static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
179{
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100180 u32 gpio_irq_no_base = port->virtual_irq_start;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200181
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100182 while (irq_stat != 0) {
183 int irqoffset = fls(irq_stat) - 1;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200184
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100185 if (port->both_edges & (1 << irqoffset))
186 mxc_flip_edge(port, irqoffset);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100187
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100188 generic_handle_irq(gpio_irq_no_base + irqoffset);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100189
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100190 irq_stat &= ~(1 << irqoffset);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200191 }
192}
193
Paulius Zaleckascfca8b52008-11-14 11:01:38 +0100194/* MX1 and MX3 has one interrupt *per* gpio port */
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200195static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
196{
197 u32 irq_stat;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100198 struct mxc_gpio_port *port = irq_get_handler_data(irq);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200199
Shawn Guob78d8e52011-06-06 00:07:55 +0800200 irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
Sascha Hauere2c97e72009-04-21 12:39:59 +0200201
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200202 mxc_gpio_irq_handler(port, irq_stat);
203}
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200204
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200205/* MX2 has one interrupt *for all* gpio ports */
206static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
207{
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200208 u32 irq_msk, irq_stat;
Shawn Guob78d8e52011-06-06 00:07:55 +0800209 struct mxc_gpio_port *port;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200210
211 /* walk through all interrupt status registers */
Shawn Guob78d8e52011-06-06 00:07:55 +0800212 list_for_each_entry(port, &mxc_gpio_ports, node) {
213 irq_msk = readl(port->base + GPIO_IMR);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200214 if (!irq_msk)
215 continue;
216
Shawn Guob78d8e52011-06-06 00:07:55 +0800217 irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200218 if (irq_stat)
Shawn Guob78d8e52011-06-06 00:07:55 +0800219 mxc_gpio_irq_handler(port, irq_stat);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200220 }
221}
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200222
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500223/*
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.
231 */
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100232static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500233{
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100234 u32 gpio = irq_to_gpio(d->irq);
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500235 u32 gpio_idx = gpio & 0x1F;
Shawn Guob78d8e52011-06-06 00:07:55 +0800236 struct mxc_gpio_port *port = irq_data_get_irq_chip_data(d);
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500237
238 if (enable) {
239 if (port->irq_high && (gpio_idx >= 16))
240 enable_irq_wake(port->irq_high);
241 else
242 enable_irq_wake(port->irq);
243 } else {
244 if (port->irq_high && (gpio_idx >= 16))
245 disable_irq_wake(port->irq_high);
246 else
247 disable_irq_wake(port->irq);
248 }
249
250 return 0;
251}
252
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200253static struct irq_chip gpio_irq_chip = {
Alexander Stein039c4642011-01-31 17:00:24 +0100254 .name = "GPIO",
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100255 .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,
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200260};
261
262static void _set_gpio_direction(struct gpio_chip *chip, unsigned offset,
263 int dir)
264{
265 struct mxc_gpio_port *port =
266 container_of(chip, struct mxc_gpio_port, chip);
267 u32 l;
Baruch Siach14cb0de2010-07-06 14:03:22 +0300268 unsigned long flags;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200269
Baruch Siach14cb0de2010-07-06 14:03:22 +0300270 spin_lock_irqsave(&port->lock, flags);
Shawn Guob78d8e52011-06-06 00:07:55 +0800271 l = readl(port->base + GPIO_GDIR);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200272 if (dir)
273 l |= 1 << offset;
274 else
275 l &= ~(1 << offset);
Shawn Guob78d8e52011-06-06 00:07:55 +0800276 writel(l, port->base + GPIO_GDIR);
Baruch Siach14cb0de2010-07-06 14:03:22 +0300277 spin_unlock_irqrestore(&port->lock, flags);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200278}
279
280static void mxc_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
281{
282 struct mxc_gpio_port *port =
283 container_of(chip, struct mxc_gpio_port, chip);
284 void __iomem *reg = port->base + GPIO_DR;
285 u32 l;
Baruch Siach14cb0de2010-07-06 14:03:22 +0300286 unsigned long flags;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200287
Baruch Siach14cb0de2010-07-06 14:03:22 +0300288 spin_lock_irqsave(&port->lock, flags);
Shawn Guob78d8e52011-06-06 00:07:55 +0800289 l = (readl(reg) & (~(1 << offset))) | (!!value << offset);
290 writel(l, reg);
Baruch Siach14cb0de2010-07-06 14:03:22 +0300291 spin_unlock_irqrestore(&port->lock, flags);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200292}
293
294static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset)
295{
296 struct mxc_gpio_port *port =
297 container_of(chip, struct mxc_gpio_port, chip);
298
Shawn Guob78d8e52011-06-06 00:07:55 +0800299 return (readl(port->base + GPIO_PSR) >> offset) & 1;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200300}
301
302static int mxc_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
303{
304 _set_gpio_direction(chip, offset, 0);
305 return 0;
306}
307
308static int mxc_gpio_direction_output(struct gpio_chip *chip,
309 unsigned offset, int value)
310{
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200311 mxc_gpio_set(chip, offset, value);
Guennadi Liakhovetski999981d2009-02-12 14:27:22 +0100312 _set_gpio_direction(chip, offset, 1);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200313 return 0;
314}
315
Thomas Gleixnerb5eee2f2011-04-04 14:29:58 +0200316/*
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.
319 */
320static struct lock_class_key gpio_lock_class;
321
Shawn Guob78d8e52011-06-06 00:07:55 +0800322static int __devinit mxc_gpio_probe(struct platform_device *pdev)
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200323{
Shawn Guob78d8e52011-06-06 00:07:55 +0800324 struct mxc_gpio_port *port;
325 struct resource *iores;
326 int err, i;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200327
Shawn Guob78d8e52011-06-06 00:07:55 +0800328 port = kzalloc(sizeof(struct mxc_gpio_port), GFP_KERNEL);
329 if (!port)
330 return -ENOMEM;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200331
Shawn Guob78d8e52011-06-06 00:07:55 +0800332 port->virtual_irq_start = MXC_GPIO_IRQ_START + pdev->id * 32;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200333
Shawn Guob78d8e52011-06-06 00:07:55 +0800334 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
335 if (!iores) {
336 err = -ENODEV;
337 goto out_kfree;
338 }
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200339
Shawn Guob78d8e52011-06-06 00:07:55 +0800340 if (!request_mem_region(iores->start, resource_size(iores),
341 pdev->name)) {
342 err = -EBUSY;
343 goto out_kfree;
344 }
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200345
Shawn Guob78d8e52011-06-06 00:07:55 +0800346 port->base = ioremap(iores->start, resource_size(iores));
347 if (!port->base) {
348 err = -ENOMEM;
349 goto out_release_mem;
350 }
Baruch Siach14cb0de2010-07-06 14:03:22 +0300351
Shawn Guob78d8e52011-06-06 00:07:55 +0800352 port->irq_high = platform_get_irq(pdev, 1);
353 port->irq = platform_get_irq(pdev, 0);
354 if (port->irq < 0) {
355 err = -EINVAL;
356 goto out_iounmap;
357 }
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200358
Shawn Guob78d8e52011-06-06 00:07:55 +0800359 /* disable the interrupt and clear the status */
360 writel(0, port->base + GPIO_IMR);
361 writel(~0, port->base + GPIO_ISR);
362
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);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200369 }
370
Sascha Hauer8afaada2009-06-15 12:36:25 +0200371 if (cpu_is_mx2()) {
372 /* setup one handler for all GPIO interrupts */
Shawn Guob78d8e52011-06-06 00:07:55 +0800373 if (pdev->id == 0)
374 irq_set_chained_handler(port->irq,
375 mx2_gpio_irq_handler);
376 } else {
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);
385 }
Sascha Hauer8afaada2009-06-15 12:36:25 +0200386 }
387
Shawn Guob78d8e52011-06-06 00:07:55 +0800388 /* 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;
395
396 spin_lock_init(&port->lock);
397
398 err = gpiochip_add(&port->chip);
399 if (err)
400 goto out_iounmap;
401
402 list_add_tail(&port->node, &mxc_gpio_ports);
403
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200404 return 0;
Shawn Guob78d8e52011-06-06 00:07:55 +0800405
406out_iounmap:
407 iounmap(port->base);
408out_release_mem:
409 release_mem_region(iores->start, resource_size(iores));
410out_kfree:
411 kfree(port);
412 dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
413 return err;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200414}
Shawn Guob78d8e52011-06-06 00:07:55 +0800415
416static struct platform_driver mxc_gpio_driver = {
417 .driver = {
418 .name = "gpio-mxc",
419 .owner = THIS_MODULE,
420 },
421 .probe = mxc_gpio_probe,
422};
423
424static int __init gpio_mxc_init(void)
425{
426 return platform_driver_register(&mxc_gpio_driver);
427}
428postcore_initcall(gpio_mxc_init);
429
430MODULE_AUTHOR("Freescale Semiconductor, "
431 "Daniel Mack <danielncaiaq.de>, "
432 "Juergen Beisert <kernel@pengutronix.de>");
433MODULE_DESCRIPTION("Freescale MXC GPIO");
434MODULE_LICENSE("GPL");