blob: addbea09aece6c241bd3eb949ffb5b2b8e109bde [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>
Shawn Guo2ce420d2011-06-06 13:22:41 +080029#include <linux/basic_mmio_gpio.h>
Shawn Guo8937cb62011-07-07 00:37:43 +080030#include <linux/of.h>
31#include <linux/of_device.h>
Paul Gortmakerbb207ef2011-07-03 13:38:09 -040032#include <linux/module.h>
Juergen Beisert07bd1a62008-07-05 10:02:49 +020033#include <asm-generic/bug.h>
Shawn Guo0e44b6e2011-09-21 21:24:04 +080034#include <asm/mach/irq.h>
Juergen Beisert07bd1a62008-07-05 10:02:49 +020035
Shawn Guoe7fc6ae2011-07-07 00:37:41 +080036enum mxc_gpio_hwtype {
37 IMX1_GPIO, /* runs on i.mx1 */
38 IMX21_GPIO, /* runs on i.mx21 and i.mx27 */
39 IMX31_GPIO, /* runs on all other i.mx */
40};
41
42/* device type dependent stuff */
43struct mxc_gpio_hwdata {
44 unsigned dr_reg;
45 unsigned gdir_reg;
46 unsigned psr_reg;
47 unsigned icr1_reg;
48 unsigned icr2_reg;
49 unsigned imr_reg;
50 unsigned isr_reg;
51 unsigned low_level;
52 unsigned high_level;
53 unsigned rise_edge;
54 unsigned fall_edge;
55};
56
Shawn Guob78d8e52011-06-06 00:07:55 +080057struct mxc_gpio_port {
58 struct list_head node;
59 void __iomem *base;
60 int irq;
61 int irq_high;
62 int virtual_irq_start;
Shawn Guo2ce420d2011-06-06 13:22:41 +080063 struct bgpio_chip bgc;
Shawn Guob78d8e52011-06-06 00:07:55 +080064 u32 both_edges;
Shawn Guob78d8e52011-06-06 00:07:55 +080065};
66
Shawn Guoe7fc6ae2011-07-07 00:37:41 +080067static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
68 .dr_reg = 0x1c,
69 .gdir_reg = 0x00,
70 .psr_reg = 0x24,
71 .icr1_reg = 0x28,
72 .icr2_reg = 0x2c,
73 .imr_reg = 0x30,
74 .isr_reg = 0x34,
75 .low_level = 0x03,
76 .high_level = 0x02,
77 .rise_edge = 0x00,
78 .fall_edge = 0x01,
79};
80
81static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
82 .dr_reg = 0x00,
83 .gdir_reg = 0x04,
84 .psr_reg = 0x08,
85 .icr1_reg = 0x0c,
86 .icr2_reg = 0x10,
87 .imr_reg = 0x14,
88 .isr_reg = 0x18,
89 .low_level = 0x00,
90 .high_level = 0x01,
91 .rise_edge = 0x02,
92 .fall_edge = 0x03,
93};
94
95static enum mxc_gpio_hwtype mxc_gpio_hwtype;
96static struct mxc_gpio_hwdata *mxc_gpio_hwdata;
97
98#define GPIO_DR (mxc_gpio_hwdata->dr_reg)
99#define GPIO_GDIR (mxc_gpio_hwdata->gdir_reg)
100#define GPIO_PSR (mxc_gpio_hwdata->psr_reg)
101#define GPIO_ICR1 (mxc_gpio_hwdata->icr1_reg)
102#define GPIO_ICR2 (mxc_gpio_hwdata->icr2_reg)
103#define GPIO_IMR (mxc_gpio_hwdata->imr_reg)
104#define GPIO_ISR (mxc_gpio_hwdata->isr_reg)
105
106#define GPIO_INT_LOW_LEV (mxc_gpio_hwdata->low_level)
107#define GPIO_INT_HIGH_LEV (mxc_gpio_hwdata->high_level)
108#define GPIO_INT_RISE_EDGE (mxc_gpio_hwdata->rise_edge)
109#define GPIO_INT_FALL_EDGE (mxc_gpio_hwdata->fall_edge)
110#define GPIO_INT_NONE 0x4
111
112static struct platform_device_id mxc_gpio_devtype[] = {
113 {
114 .name = "imx1-gpio",
115 .driver_data = IMX1_GPIO,
116 }, {
117 .name = "imx21-gpio",
118 .driver_data = IMX21_GPIO,
119 }, {
120 .name = "imx31-gpio",
121 .driver_data = IMX31_GPIO,
122 }, {
123 /* sentinel */
124 }
125};
126
Shawn Guo8937cb62011-07-07 00:37:43 +0800127static const struct of_device_id mxc_gpio_dt_ids[] = {
128 { .compatible = "fsl,imx1-gpio", .data = &mxc_gpio_devtype[IMX1_GPIO], },
129 { .compatible = "fsl,imx21-gpio", .data = &mxc_gpio_devtype[IMX21_GPIO], },
130 { .compatible = "fsl,imx31-gpio", .data = &mxc_gpio_devtype[IMX31_GPIO], },
131 { /* sentinel */ }
132};
133
Shawn Guob78d8e52011-06-06 00:07:55 +0800134/*
135 * MX2 has one interrupt *for all* gpio ports. The list is used
136 * to save the references to all ports, so that mx2_gpio_irq_handler
137 * can walk through all interrupt status registers.
138 */
139static LIST_HEAD(mxc_gpio_ports);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200140
141/* Note: This driver assumes 32 GPIOs are handled in one register */
142
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100143static int gpio_set_irq_type(struct irq_data *d, u32 type)
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200144{
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100145 u32 gpio = irq_to_gpio(d->irq);
Shawn Guoe4ea9332011-06-07 16:25:37 +0800146 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
147 struct mxc_gpio_port *port = gc->private;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200148 u32 bit, val;
149 int edge;
150 void __iomem *reg = port->base;
151
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100152 port->both_edges &= ~(1 << (gpio & 31));
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200153 switch (type) {
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100154 case IRQ_TYPE_EDGE_RISING:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200155 edge = GPIO_INT_RISE_EDGE;
156 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100157 case IRQ_TYPE_EDGE_FALLING:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200158 edge = GPIO_INT_FALL_EDGE;
159 break;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100160 case IRQ_TYPE_EDGE_BOTH:
Shawn Guo5523f862011-06-12 01:33:29 +0800161 val = gpio_get_value(gpio);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100162 if (val) {
163 edge = GPIO_INT_LOW_LEV;
164 pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
165 } else {
166 edge = GPIO_INT_HIGH_LEV;
167 pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
168 }
169 port->both_edges |= 1 << (gpio & 31);
170 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100171 case IRQ_TYPE_LEVEL_LOW:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200172 edge = GPIO_INT_LOW_LEV;
173 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100174 case IRQ_TYPE_LEVEL_HIGH:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200175 edge = GPIO_INT_HIGH_LEV;
176 break;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100177 default:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200178 return -EINVAL;
179 }
180
181 reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
182 bit = gpio & 0xf;
Shawn Guob78d8e52011-06-06 00:07:55 +0800183 val = readl(reg) & ~(0x3 << (bit << 1));
184 writel(val | (edge << (bit << 1)), reg);
Shawn Guoe4ea9332011-06-07 16:25:37 +0800185 writel(1 << (gpio & 0x1f), port->base + GPIO_ISR);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200186
187 return 0;
188}
189
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100190static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
191{
192 void __iomem *reg = port->base;
193 u32 bit, val;
194 int edge;
195
196 reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
197 bit = gpio & 0xf;
Shawn Guob78d8e52011-06-06 00:07:55 +0800198 val = readl(reg);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100199 edge = (val >> (bit << 1)) & 3;
200 val &= ~(0x3 << (bit << 1));
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100201 if (edge == GPIO_INT_HIGH_LEV) {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100202 edge = GPIO_INT_LOW_LEV;
203 pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100204 } else if (edge == GPIO_INT_LOW_LEV) {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100205 edge = GPIO_INT_HIGH_LEV;
206 pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100207 } else {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100208 pr_err("mxc: invalid configuration for GPIO %d: %x\n",
209 gpio, edge);
210 return;
211 }
Shawn Guob78d8e52011-06-06 00:07:55 +0800212 writel(val | (edge << (bit << 1)), reg);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100213}
214
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100215/* handle 32 interrupts in one status register */
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200216static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
217{
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100218 u32 gpio_irq_no_base = port->virtual_irq_start;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200219
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100220 while (irq_stat != 0) {
221 int irqoffset = fls(irq_stat) - 1;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200222
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100223 if (port->both_edges & (1 << irqoffset))
224 mxc_flip_edge(port, irqoffset);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100225
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100226 generic_handle_irq(gpio_irq_no_base + irqoffset);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100227
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100228 irq_stat &= ~(1 << irqoffset);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200229 }
230}
231
Paulius Zaleckascfca8b52008-11-14 11:01:38 +0100232/* MX1 and MX3 has one interrupt *per* gpio port */
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200233static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
234{
235 u32 irq_stat;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100236 struct mxc_gpio_port *port = irq_get_handler_data(irq);
Shawn Guo0e44b6e2011-09-21 21:24:04 +0800237 struct irq_chip *chip = irq_get_chip(irq);
238
239 chained_irq_enter(chip, desc);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200240
Shawn Guob78d8e52011-06-06 00:07:55 +0800241 irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
Sascha Hauere2c97e72009-04-21 12:39:59 +0200242
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200243 mxc_gpio_irq_handler(port, irq_stat);
Shawn Guo0e44b6e2011-09-21 21:24:04 +0800244
245 chained_irq_exit(chip, desc);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200246}
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200247
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200248/* MX2 has one interrupt *for all* gpio ports */
249static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
250{
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200251 u32 irq_msk, irq_stat;
Shawn Guob78d8e52011-06-06 00:07:55 +0800252 struct mxc_gpio_port *port;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200253
254 /* walk through all interrupt status registers */
Shawn Guob78d8e52011-06-06 00:07:55 +0800255 list_for_each_entry(port, &mxc_gpio_ports, node) {
256 irq_msk = readl(port->base + GPIO_IMR);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200257 if (!irq_msk)
258 continue;
259
Shawn Guob78d8e52011-06-06 00:07:55 +0800260 irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200261 if (irq_stat)
Shawn Guob78d8e52011-06-06 00:07:55 +0800262 mxc_gpio_irq_handler(port, irq_stat);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200263 }
264}
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200265
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500266/*
267 * Set interrupt number "irq" in the GPIO as a wake-up source.
268 * While system is running, all registered GPIO interrupts need to have
269 * wake-up enabled. When system is suspended, only selected GPIO interrupts
270 * need to have wake-up enabled.
271 * @param irq interrupt source number
272 * @param enable enable as wake-up if equal to non-zero
273 * @return This function returns 0 on success.
274 */
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100275static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500276{
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100277 u32 gpio = irq_to_gpio(d->irq);
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500278 u32 gpio_idx = gpio & 0x1F;
Shawn Guoe4ea9332011-06-07 16:25:37 +0800279 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
280 struct mxc_gpio_port *port = gc->private;
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500281
282 if (enable) {
283 if (port->irq_high && (gpio_idx >= 16))
284 enable_irq_wake(port->irq_high);
285 else
286 enable_irq_wake(port->irq);
287 } else {
288 if (port->irq_high && (gpio_idx >= 16))
289 disable_irq_wake(port->irq_high);
290 else
291 disable_irq_wake(port->irq);
292 }
293
294 return 0;
295}
296
Shawn Guoe4ea9332011-06-07 16:25:37 +0800297static void __init mxc_gpio_init_gc(struct mxc_gpio_port *port)
298{
299 struct irq_chip_generic *gc;
300 struct irq_chip_type *ct;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200301
Shawn Guoe4ea9332011-06-07 16:25:37 +0800302 gc = irq_alloc_generic_chip("gpio-mxc", 1, port->virtual_irq_start,
303 port->base, handle_level_irq);
304 gc->private = port;
305
306 ct = gc->chip_types;
Shawn Guo591567a2011-07-19 21:16:56 +0800307 ct->chip.irq_ack = irq_gc_ack_set_bit;
Shawn Guoe4ea9332011-06-07 16:25:37 +0800308 ct->chip.irq_mask = irq_gc_mask_clr_bit;
309 ct->chip.irq_unmask = irq_gc_mask_set_bit;
310 ct->chip.irq_set_type = gpio_set_irq_type;
Shawn Guo591567a2011-07-19 21:16:56 +0800311 ct->chip.irq_set_wake = gpio_set_wake_irq;
Shawn Guoe4ea9332011-06-07 16:25:37 +0800312 ct->regs.ack = GPIO_ISR;
313 ct->regs.mask = GPIO_IMR;
314
315 irq_setup_generic_chip(gc, IRQ_MSK(32), IRQ_GC_INIT_NESTED_LOCK,
316 IRQ_NOREQUEST, 0);
317}
Thomas Gleixnerb5eee2f2011-04-04 14:29:58 +0200318
Shawn Guoe7fc6ae2011-07-07 00:37:41 +0800319static void __devinit mxc_gpio_get_hw(struct platform_device *pdev)
320{
Shawn Guo8937cb62011-07-07 00:37:43 +0800321 const struct of_device_id *of_id =
322 of_match_device(mxc_gpio_dt_ids, &pdev->dev);
323 enum mxc_gpio_hwtype hwtype;
324
325 if (of_id)
326 pdev->id_entry = of_id->data;
327 hwtype = pdev->id_entry->driver_data;
Shawn Guoe7fc6ae2011-07-07 00:37:41 +0800328
329 if (mxc_gpio_hwtype) {
330 /*
331 * The driver works with a reasonable presupposition,
332 * that is all gpio ports must be the same type when
333 * running on one soc.
334 */
335 BUG_ON(mxc_gpio_hwtype != hwtype);
336 return;
337 }
338
339 if (hwtype == IMX31_GPIO)
340 mxc_gpio_hwdata = &imx31_gpio_hwdata;
341 else
342 mxc_gpio_hwdata = &imx1_imx21_gpio_hwdata;
343
344 mxc_gpio_hwtype = hwtype;
345}
346
Shawn Guob78d8e52011-06-06 00:07:55 +0800347static int __devinit mxc_gpio_probe(struct platform_device *pdev)
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200348{
Shawn Guo8937cb62011-07-07 00:37:43 +0800349 struct device_node *np = pdev->dev.of_node;
Shawn Guob78d8e52011-06-06 00:07:55 +0800350 struct mxc_gpio_port *port;
351 struct resource *iores;
Shawn Guoe4ea9332011-06-07 16:25:37 +0800352 int err;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200353
Shawn Guoe7fc6ae2011-07-07 00:37:41 +0800354 mxc_gpio_get_hw(pdev);
355
Shawn Guob78d8e52011-06-06 00:07:55 +0800356 port = kzalloc(sizeof(struct mxc_gpio_port), GFP_KERNEL);
357 if (!port)
358 return -ENOMEM;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200359
Shawn Guob78d8e52011-06-06 00:07:55 +0800360 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
361 if (!iores) {
362 err = -ENODEV;
363 goto out_kfree;
364 }
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200365
Shawn Guob78d8e52011-06-06 00:07:55 +0800366 if (!request_mem_region(iores->start, resource_size(iores),
367 pdev->name)) {
368 err = -EBUSY;
369 goto out_kfree;
370 }
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200371
Shawn Guob78d8e52011-06-06 00:07:55 +0800372 port->base = ioremap(iores->start, resource_size(iores));
373 if (!port->base) {
374 err = -ENOMEM;
375 goto out_release_mem;
376 }
Baruch Siach14cb0de2010-07-06 14:03:22 +0300377
Shawn Guob78d8e52011-06-06 00:07:55 +0800378 port->irq_high = platform_get_irq(pdev, 1);
379 port->irq = platform_get_irq(pdev, 0);
380 if (port->irq < 0) {
381 err = -EINVAL;
382 goto out_iounmap;
383 }
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200384
Shawn Guob78d8e52011-06-06 00:07:55 +0800385 /* disable the interrupt and clear the status */
386 writel(0, port->base + GPIO_IMR);
387 writel(~0, port->base + GPIO_ISR);
388
Shawn Guoe7fc6ae2011-07-07 00:37:41 +0800389 if (mxc_gpio_hwtype == IMX21_GPIO) {
Sascha Hauer8afaada2009-06-15 12:36:25 +0200390 /* setup one handler for all GPIO interrupts */
Shawn Guob78d8e52011-06-06 00:07:55 +0800391 if (pdev->id == 0)
392 irq_set_chained_handler(port->irq,
393 mx2_gpio_irq_handler);
394 } else {
395 /* setup one handler for each entry */
396 irq_set_chained_handler(port->irq, mx3_gpio_irq_handler);
397 irq_set_handler_data(port->irq, port);
398 if (port->irq_high > 0) {
399 /* setup handler for GPIO 16 to 31 */
400 irq_set_chained_handler(port->irq_high,
401 mx3_gpio_irq_handler);
402 irq_set_handler_data(port->irq_high, port);
403 }
Sascha Hauer8afaada2009-06-15 12:36:25 +0200404 }
405
Shawn Guo2ce420d2011-06-06 13:22:41 +0800406 err = bgpio_init(&port->bgc, &pdev->dev, 4,
407 port->base + GPIO_PSR,
408 port->base + GPIO_DR, NULL,
409 port->base + GPIO_GDIR, NULL, false);
Shawn Guob78d8e52011-06-06 00:07:55 +0800410 if (err)
411 goto out_iounmap;
412
Shawn Guo2ce420d2011-06-06 13:22:41 +0800413 port->bgc.gc.base = pdev->id * 32;
Lothar Waßmannfb149212011-07-07 14:50:16 +0200414 port->bgc.dir = port->bgc.read_reg(port->bgc.reg_dir);
415 port->bgc.data = port->bgc.read_reg(port->bgc.reg_set);
Shawn Guo2ce420d2011-06-06 13:22:41 +0800416
417 err = gpiochip_add(&port->bgc.gc);
418 if (err)
419 goto out_bgpio_remove;
420
Shawn Guo8937cb62011-07-07 00:37:43 +0800421 /*
422 * In dt case, we use gpio number range dynamically
423 * allocated by gpio core.
424 */
425 port->virtual_irq_start = MXC_GPIO_IRQ_START + (np ? port->bgc.gc.base :
426 pdev->id * 32);
427
428 /* gpio-mxc can be a generic irq chip */
429 mxc_gpio_init_gc(port);
430
Shawn Guob78d8e52011-06-06 00:07:55 +0800431 list_add_tail(&port->node, &mxc_gpio_ports);
432
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200433 return 0;
Shawn Guob78d8e52011-06-06 00:07:55 +0800434
Shawn Guo2ce420d2011-06-06 13:22:41 +0800435out_bgpio_remove:
436 bgpio_remove(&port->bgc);
Shawn Guob78d8e52011-06-06 00:07:55 +0800437out_iounmap:
438 iounmap(port->base);
439out_release_mem:
440 release_mem_region(iores->start, resource_size(iores));
441out_kfree:
442 kfree(port);
443 dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
444 return err;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200445}
Shawn Guob78d8e52011-06-06 00:07:55 +0800446
447static struct platform_driver mxc_gpio_driver = {
448 .driver = {
449 .name = "gpio-mxc",
450 .owner = THIS_MODULE,
Shawn Guo8937cb62011-07-07 00:37:43 +0800451 .of_match_table = mxc_gpio_dt_ids,
Shawn Guob78d8e52011-06-06 00:07:55 +0800452 },
453 .probe = mxc_gpio_probe,
Shawn Guoe7fc6ae2011-07-07 00:37:41 +0800454 .id_table = mxc_gpio_devtype,
Shawn Guob78d8e52011-06-06 00:07:55 +0800455};
456
457static int __init gpio_mxc_init(void)
458{
459 return platform_driver_register(&mxc_gpio_driver);
460}
461postcore_initcall(gpio_mxc_init);
462
463MODULE_AUTHOR("Freescale Semiconductor, "
464 "Daniel Mack <danielncaiaq.de>, "
465 "Juergen Beisert <kernel@pengutronix.de>");
466MODULE_DESCRIPTION("Freescale MXC GPIO");
467MODULE_LICENSE("GPL");