xref: /openbmc/linux/drivers/gpio/gpio-mxc.c (revision a4395612)
1d37a65bbSShawn Guo /*
2d37a65bbSShawn Guo  * MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
3d37a65bbSShawn Guo  * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
4d37a65bbSShawn Guo  *
5d37a65bbSShawn Guo  * Based on code from Freescale,
6d37a65bbSShawn Guo  * Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
7d37a65bbSShawn Guo  *
8d37a65bbSShawn Guo  * This program is free software; you can redistribute it and/or
9d37a65bbSShawn Guo  * modify it under the terms of the GNU General Public License
10d37a65bbSShawn Guo  * as published by the Free Software Foundation; either version 2
11d37a65bbSShawn Guo  * of the License, or (at your option) any later version.
12d37a65bbSShawn Guo  * This program is distributed in the hope that it will be useful,
13d37a65bbSShawn Guo  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14d37a65bbSShawn Guo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15d37a65bbSShawn Guo  * GNU General Public License for more details.
16d37a65bbSShawn Guo  *
17d37a65bbSShawn Guo  * You should have received a copy of the GNU General Public License
18d37a65bbSShawn Guo  * along with this program; if not, write to the Free Software
19d37a65bbSShawn Guo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20d37a65bbSShawn Guo  */
21d37a65bbSShawn Guo 
22d37a65bbSShawn Guo #include <linux/init.h>
23d37a65bbSShawn Guo #include <linux/interrupt.h>
24d37a65bbSShawn Guo #include <linux/io.h>
25d37a65bbSShawn Guo #include <linux/irq.h>
26d37a65bbSShawn Guo #include <linux/gpio.h>
27b78d8e59SShawn Guo #include <linux/platform_device.h>
28b78d8e59SShawn Guo #include <linux/slab.h>
292ce420daSShawn Guo #include <linux/basic_mmio_gpio.h>
308937cb60SShawn Guo #include <linux/of.h>
318937cb60SShawn Guo #include <linux/of_device.h>
32d37a65bbSShawn Guo #include <asm-generic/bug.h>
33d37a65bbSShawn Guo 
34a4395612SShawn Guo #define irq_to_gpio(irq)	((irq) - MXC_GPIO_IRQ_START)
35a4395612SShawn Guo 
36e7fc6ae7SShawn Guo enum mxc_gpio_hwtype {
37e7fc6ae7SShawn Guo 	IMX1_GPIO,	/* runs on i.mx1 */
38e7fc6ae7SShawn Guo 	IMX21_GPIO,	/* runs on i.mx21 and i.mx27 */
39e7fc6ae7SShawn Guo 	IMX31_GPIO,	/* runs on all other i.mx */
40e7fc6ae7SShawn Guo };
41e7fc6ae7SShawn Guo 
42e7fc6ae7SShawn Guo /* device type dependent stuff */
43e7fc6ae7SShawn Guo struct mxc_gpio_hwdata {
44e7fc6ae7SShawn Guo 	unsigned dr_reg;
45e7fc6ae7SShawn Guo 	unsigned gdir_reg;
46e7fc6ae7SShawn Guo 	unsigned psr_reg;
47e7fc6ae7SShawn Guo 	unsigned icr1_reg;
48e7fc6ae7SShawn Guo 	unsigned icr2_reg;
49e7fc6ae7SShawn Guo 	unsigned imr_reg;
50e7fc6ae7SShawn Guo 	unsigned isr_reg;
51e7fc6ae7SShawn Guo 	unsigned low_level;
52e7fc6ae7SShawn Guo 	unsigned high_level;
53e7fc6ae7SShawn Guo 	unsigned rise_edge;
54e7fc6ae7SShawn Guo 	unsigned fall_edge;
55e7fc6ae7SShawn Guo };
56e7fc6ae7SShawn Guo 
57b78d8e59SShawn Guo struct mxc_gpio_port {
58b78d8e59SShawn Guo 	struct list_head node;
59b78d8e59SShawn Guo 	void __iomem *base;
60b78d8e59SShawn Guo 	int irq;
61b78d8e59SShawn Guo 	int irq_high;
62b78d8e59SShawn Guo 	int virtual_irq_start;
632ce420daSShawn Guo 	struct bgpio_chip bgc;
64b78d8e59SShawn Guo 	u32 both_edges;
65b78d8e59SShawn Guo };
66b78d8e59SShawn Guo 
67e7fc6ae7SShawn Guo static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
68e7fc6ae7SShawn Guo 	.dr_reg		= 0x1c,
69e7fc6ae7SShawn Guo 	.gdir_reg	= 0x00,
70e7fc6ae7SShawn Guo 	.psr_reg	= 0x24,
71e7fc6ae7SShawn Guo 	.icr1_reg	= 0x28,
72e7fc6ae7SShawn Guo 	.icr2_reg	= 0x2c,
73e7fc6ae7SShawn Guo 	.imr_reg	= 0x30,
74e7fc6ae7SShawn Guo 	.isr_reg	= 0x34,
75e7fc6ae7SShawn Guo 	.low_level	= 0x03,
76e7fc6ae7SShawn Guo 	.high_level	= 0x02,
77e7fc6ae7SShawn Guo 	.rise_edge	= 0x00,
78e7fc6ae7SShawn Guo 	.fall_edge	= 0x01,
79e7fc6ae7SShawn Guo };
80e7fc6ae7SShawn Guo 
81e7fc6ae7SShawn Guo static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
82e7fc6ae7SShawn Guo 	.dr_reg		= 0x00,
83e7fc6ae7SShawn Guo 	.gdir_reg	= 0x04,
84e7fc6ae7SShawn Guo 	.psr_reg	= 0x08,
85e7fc6ae7SShawn Guo 	.icr1_reg	= 0x0c,
86e7fc6ae7SShawn Guo 	.icr2_reg	= 0x10,
87e7fc6ae7SShawn Guo 	.imr_reg	= 0x14,
88e7fc6ae7SShawn Guo 	.isr_reg	= 0x18,
89e7fc6ae7SShawn Guo 	.low_level	= 0x00,
90e7fc6ae7SShawn Guo 	.high_level	= 0x01,
91e7fc6ae7SShawn Guo 	.rise_edge	= 0x02,
92e7fc6ae7SShawn Guo 	.fall_edge	= 0x03,
93e7fc6ae7SShawn Guo };
94e7fc6ae7SShawn Guo 
95e7fc6ae7SShawn Guo static enum mxc_gpio_hwtype mxc_gpio_hwtype;
96e7fc6ae7SShawn Guo static struct mxc_gpio_hwdata *mxc_gpio_hwdata;
97e7fc6ae7SShawn Guo 
98e7fc6ae7SShawn Guo #define GPIO_DR			(mxc_gpio_hwdata->dr_reg)
99e7fc6ae7SShawn Guo #define GPIO_GDIR		(mxc_gpio_hwdata->gdir_reg)
100e7fc6ae7SShawn Guo #define GPIO_PSR		(mxc_gpio_hwdata->psr_reg)
101e7fc6ae7SShawn Guo #define GPIO_ICR1		(mxc_gpio_hwdata->icr1_reg)
102e7fc6ae7SShawn Guo #define GPIO_ICR2		(mxc_gpio_hwdata->icr2_reg)
103e7fc6ae7SShawn Guo #define GPIO_IMR		(mxc_gpio_hwdata->imr_reg)
104e7fc6ae7SShawn Guo #define GPIO_ISR		(mxc_gpio_hwdata->isr_reg)
105e7fc6ae7SShawn Guo 
106e7fc6ae7SShawn Guo #define GPIO_INT_LOW_LEV	(mxc_gpio_hwdata->low_level)
107e7fc6ae7SShawn Guo #define GPIO_INT_HIGH_LEV	(mxc_gpio_hwdata->high_level)
108e7fc6ae7SShawn Guo #define GPIO_INT_RISE_EDGE	(mxc_gpio_hwdata->rise_edge)
109e7fc6ae7SShawn Guo #define GPIO_INT_FALL_EDGE	(mxc_gpio_hwdata->fall_edge)
110e7fc6ae7SShawn Guo #define GPIO_INT_NONE		0x4
111e7fc6ae7SShawn Guo 
112e7fc6ae7SShawn Guo static struct platform_device_id mxc_gpio_devtype[] = {
113e7fc6ae7SShawn Guo 	{
114e7fc6ae7SShawn Guo 		.name = "imx1-gpio",
115e7fc6ae7SShawn Guo 		.driver_data = IMX1_GPIO,
116e7fc6ae7SShawn Guo 	}, {
117e7fc6ae7SShawn Guo 		.name = "imx21-gpio",
118e7fc6ae7SShawn Guo 		.driver_data = IMX21_GPIO,
119e7fc6ae7SShawn Guo 	}, {
120e7fc6ae7SShawn Guo 		.name = "imx31-gpio",
121e7fc6ae7SShawn Guo 		.driver_data = IMX31_GPIO,
122e7fc6ae7SShawn Guo 	}, {
123e7fc6ae7SShawn Guo 		/* sentinel */
124e7fc6ae7SShawn Guo 	}
125e7fc6ae7SShawn Guo };
126e7fc6ae7SShawn Guo 
1278937cb60SShawn Guo static const struct of_device_id mxc_gpio_dt_ids[] = {
1288937cb60SShawn Guo 	{ .compatible = "fsl,imx1-gpio", .data = &mxc_gpio_devtype[IMX1_GPIO], },
1298937cb60SShawn Guo 	{ .compatible = "fsl,imx21-gpio", .data = &mxc_gpio_devtype[IMX21_GPIO], },
1308937cb60SShawn Guo 	{ .compatible = "fsl,imx31-gpio", .data = &mxc_gpio_devtype[IMX31_GPIO], },
1318937cb60SShawn Guo 	{ /* sentinel */ }
1328937cb60SShawn Guo };
1338937cb60SShawn Guo 
134b78d8e59SShawn Guo /*
135b78d8e59SShawn Guo  * MX2 has one interrupt *for all* gpio ports. The list is used
136b78d8e59SShawn Guo  * to save the references to all ports, so that mx2_gpio_irq_handler
137b78d8e59SShawn Guo  * can walk through all interrupt status registers.
138b78d8e59SShawn Guo  */
139b78d8e59SShawn Guo static LIST_HEAD(mxc_gpio_ports);
140d37a65bbSShawn Guo 
141d37a65bbSShawn Guo /* Note: This driver assumes 32 GPIOs are handled in one register */
142d37a65bbSShawn Guo 
143d37a65bbSShawn Guo static int gpio_set_irq_type(struct irq_data *d, u32 type)
144d37a65bbSShawn Guo {
145d37a65bbSShawn Guo 	u32 gpio = irq_to_gpio(d->irq);
146e4ea9333SShawn Guo 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
147e4ea9333SShawn Guo 	struct mxc_gpio_port *port = gc->private;
148d37a65bbSShawn Guo 	u32 bit, val;
149d37a65bbSShawn Guo 	int edge;
150d37a65bbSShawn Guo 	void __iomem *reg = port->base;
151d37a65bbSShawn Guo 
152d37a65bbSShawn Guo 	port->both_edges &= ~(1 << (gpio & 31));
153d37a65bbSShawn Guo 	switch (type) {
154d37a65bbSShawn Guo 	case IRQ_TYPE_EDGE_RISING:
155d37a65bbSShawn Guo 		edge = GPIO_INT_RISE_EDGE;
156d37a65bbSShawn Guo 		break;
157d37a65bbSShawn Guo 	case IRQ_TYPE_EDGE_FALLING:
158d37a65bbSShawn Guo 		edge = GPIO_INT_FALL_EDGE;
159d37a65bbSShawn Guo 		break;
160d37a65bbSShawn Guo 	case IRQ_TYPE_EDGE_BOTH:
1615523f86bSShawn Guo 		val = gpio_get_value(gpio);
162d37a65bbSShawn Guo 		if (val) {
163d37a65bbSShawn Guo 			edge = GPIO_INT_LOW_LEV;
164d37a65bbSShawn Guo 			pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
165d37a65bbSShawn Guo 		} else {
166d37a65bbSShawn Guo 			edge = GPIO_INT_HIGH_LEV;
167d37a65bbSShawn Guo 			pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
168d37a65bbSShawn Guo 		}
169d37a65bbSShawn Guo 		port->both_edges |= 1 << (gpio & 31);
170d37a65bbSShawn Guo 		break;
171d37a65bbSShawn Guo 	case IRQ_TYPE_LEVEL_LOW:
172d37a65bbSShawn Guo 		edge = GPIO_INT_LOW_LEV;
173d37a65bbSShawn Guo 		break;
174d37a65bbSShawn Guo 	case IRQ_TYPE_LEVEL_HIGH:
175d37a65bbSShawn Guo 		edge = GPIO_INT_HIGH_LEV;
176d37a65bbSShawn Guo 		break;
177d37a65bbSShawn Guo 	default:
178d37a65bbSShawn Guo 		return -EINVAL;
179d37a65bbSShawn Guo 	}
180d37a65bbSShawn Guo 
181d37a65bbSShawn Guo 	reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
182d37a65bbSShawn Guo 	bit = gpio & 0xf;
183b78d8e59SShawn Guo 	val = readl(reg) & ~(0x3 << (bit << 1));
184b78d8e59SShawn Guo 	writel(val | (edge << (bit << 1)), reg);
185e4ea9333SShawn Guo 	writel(1 << (gpio & 0x1f), port->base + GPIO_ISR);
186d37a65bbSShawn Guo 
187d37a65bbSShawn Guo 	return 0;
188d37a65bbSShawn Guo }
189d37a65bbSShawn Guo 
190d37a65bbSShawn Guo static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
191d37a65bbSShawn Guo {
192d37a65bbSShawn Guo 	void __iomem *reg = port->base;
193d37a65bbSShawn Guo 	u32 bit, val;
194d37a65bbSShawn Guo 	int edge;
195d37a65bbSShawn Guo 
196d37a65bbSShawn Guo 	reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
197d37a65bbSShawn Guo 	bit = gpio & 0xf;
198b78d8e59SShawn Guo 	val = readl(reg);
199d37a65bbSShawn Guo 	edge = (val >> (bit << 1)) & 3;
200d37a65bbSShawn Guo 	val &= ~(0x3 << (bit << 1));
201d37a65bbSShawn Guo 	if (edge == GPIO_INT_HIGH_LEV) {
202d37a65bbSShawn Guo 		edge = GPIO_INT_LOW_LEV;
203d37a65bbSShawn Guo 		pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
204d37a65bbSShawn Guo 	} else if (edge == GPIO_INT_LOW_LEV) {
205d37a65bbSShawn Guo 		edge = GPIO_INT_HIGH_LEV;
206d37a65bbSShawn Guo 		pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
207d37a65bbSShawn Guo 	} else {
208d37a65bbSShawn Guo 		pr_err("mxc: invalid configuration for GPIO %d: %x\n",
209d37a65bbSShawn Guo 		       gpio, edge);
210d37a65bbSShawn Guo 		return;
211d37a65bbSShawn Guo 	}
212b78d8e59SShawn Guo 	writel(val | (edge << (bit << 1)), reg);
213d37a65bbSShawn Guo }
214d37a65bbSShawn Guo 
215d37a65bbSShawn Guo /* handle 32 interrupts in one status register */
216d37a65bbSShawn Guo static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
217d37a65bbSShawn Guo {
218d37a65bbSShawn Guo 	u32 gpio_irq_no_base = port->virtual_irq_start;
219d37a65bbSShawn Guo 
220d37a65bbSShawn Guo 	while (irq_stat != 0) {
221d37a65bbSShawn Guo 		int irqoffset = fls(irq_stat) - 1;
222d37a65bbSShawn Guo 
223d37a65bbSShawn Guo 		if (port->both_edges & (1 << irqoffset))
224d37a65bbSShawn Guo 			mxc_flip_edge(port, irqoffset);
225d37a65bbSShawn Guo 
226d37a65bbSShawn Guo 		generic_handle_irq(gpio_irq_no_base + irqoffset);
227d37a65bbSShawn Guo 
228d37a65bbSShawn Guo 		irq_stat &= ~(1 << irqoffset);
229d37a65bbSShawn Guo 	}
230d37a65bbSShawn Guo }
231d37a65bbSShawn Guo 
232d37a65bbSShawn Guo /* MX1 and MX3 has one interrupt *per* gpio port */
233d37a65bbSShawn Guo static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
234d37a65bbSShawn Guo {
235d37a65bbSShawn Guo 	u32 irq_stat;
236d37a65bbSShawn Guo 	struct mxc_gpio_port *port = irq_get_handler_data(irq);
237d37a65bbSShawn Guo 
238b78d8e59SShawn Guo 	irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
239d37a65bbSShawn Guo 
240d37a65bbSShawn Guo 	mxc_gpio_irq_handler(port, irq_stat);
241d37a65bbSShawn Guo }
242d37a65bbSShawn Guo 
243d37a65bbSShawn Guo /* MX2 has one interrupt *for all* gpio ports */
244d37a65bbSShawn Guo static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
245d37a65bbSShawn Guo {
246d37a65bbSShawn Guo 	u32 irq_msk, irq_stat;
247b78d8e59SShawn Guo 	struct mxc_gpio_port *port;
248d37a65bbSShawn Guo 
249d37a65bbSShawn Guo 	/* walk through all interrupt status registers */
250b78d8e59SShawn Guo 	list_for_each_entry(port, &mxc_gpio_ports, node) {
251b78d8e59SShawn Guo 		irq_msk = readl(port->base + GPIO_IMR);
252d37a65bbSShawn Guo 		if (!irq_msk)
253d37a65bbSShawn Guo 			continue;
254d37a65bbSShawn Guo 
255b78d8e59SShawn Guo 		irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
256d37a65bbSShawn Guo 		if (irq_stat)
257b78d8e59SShawn Guo 			mxc_gpio_irq_handler(port, irq_stat);
258d37a65bbSShawn Guo 	}
259d37a65bbSShawn Guo }
260d37a65bbSShawn Guo 
261d37a65bbSShawn Guo /*
262d37a65bbSShawn Guo  * Set interrupt number "irq" in the GPIO as a wake-up source.
263d37a65bbSShawn Guo  * While system is running, all registered GPIO interrupts need to have
264d37a65bbSShawn Guo  * wake-up enabled. When system is suspended, only selected GPIO interrupts
265d37a65bbSShawn Guo  * need to have wake-up enabled.
266d37a65bbSShawn Guo  * @param  irq          interrupt source number
267d37a65bbSShawn Guo  * @param  enable       enable as wake-up if equal to non-zero
268d37a65bbSShawn Guo  * @return       This function returns 0 on success.
269d37a65bbSShawn Guo  */
270d37a65bbSShawn Guo static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
271d37a65bbSShawn Guo {
272d37a65bbSShawn Guo 	u32 gpio = irq_to_gpio(d->irq);
273d37a65bbSShawn Guo 	u32 gpio_idx = gpio & 0x1F;
274e4ea9333SShawn Guo 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
275e4ea9333SShawn Guo 	struct mxc_gpio_port *port = gc->private;
276d37a65bbSShawn Guo 
277d37a65bbSShawn Guo 	if (enable) {
278d37a65bbSShawn Guo 		if (port->irq_high && (gpio_idx >= 16))
279d37a65bbSShawn Guo 			enable_irq_wake(port->irq_high);
280d37a65bbSShawn Guo 		else
281d37a65bbSShawn Guo 			enable_irq_wake(port->irq);
282d37a65bbSShawn Guo 	} else {
283d37a65bbSShawn Guo 		if (port->irq_high && (gpio_idx >= 16))
284d37a65bbSShawn Guo 			disable_irq_wake(port->irq_high);
285d37a65bbSShawn Guo 		else
286d37a65bbSShawn Guo 			disable_irq_wake(port->irq);
287d37a65bbSShawn Guo 	}
288d37a65bbSShawn Guo 
289d37a65bbSShawn Guo 	return 0;
290d37a65bbSShawn Guo }
291d37a65bbSShawn Guo 
292e4ea9333SShawn Guo static void __init mxc_gpio_init_gc(struct mxc_gpio_port *port)
293e4ea9333SShawn Guo {
294e4ea9333SShawn Guo 	struct irq_chip_generic *gc;
295e4ea9333SShawn Guo 	struct irq_chip_type *ct;
296d37a65bbSShawn Guo 
297e4ea9333SShawn Guo 	gc = irq_alloc_generic_chip("gpio-mxc", 1, port->virtual_irq_start,
298e4ea9333SShawn Guo 				    port->base, handle_level_irq);
299e4ea9333SShawn Guo 	gc->private = port;
300e4ea9333SShawn Guo 
301e4ea9333SShawn Guo 	ct = gc->chip_types;
302591567a5SShawn Guo 	ct->chip.irq_ack = irq_gc_ack_set_bit;
303e4ea9333SShawn Guo 	ct->chip.irq_mask = irq_gc_mask_clr_bit;
304e4ea9333SShawn Guo 	ct->chip.irq_unmask = irq_gc_mask_set_bit;
305e4ea9333SShawn Guo 	ct->chip.irq_set_type = gpio_set_irq_type;
306591567a5SShawn Guo 	ct->chip.irq_set_wake = gpio_set_wake_irq;
307e4ea9333SShawn Guo 	ct->regs.ack = GPIO_ISR;
308e4ea9333SShawn Guo 	ct->regs.mask = GPIO_IMR;
309e4ea9333SShawn Guo 
310e4ea9333SShawn Guo 	irq_setup_generic_chip(gc, IRQ_MSK(32), IRQ_GC_INIT_NESTED_LOCK,
311e4ea9333SShawn Guo 			       IRQ_NOREQUEST, 0);
312e4ea9333SShawn Guo }
313d37a65bbSShawn Guo 
314e7fc6ae7SShawn Guo static void __devinit mxc_gpio_get_hw(struct platform_device *pdev)
315e7fc6ae7SShawn Guo {
3168937cb60SShawn Guo 	const struct of_device_id *of_id =
3178937cb60SShawn Guo 			of_match_device(mxc_gpio_dt_ids, &pdev->dev);
3188937cb60SShawn Guo 	enum mxc_gpio_hwtype hwtype;
3198937cb60SShawn Guo 
3208937cb60SShawn Guo 	if (of_id)
3218937cb60SShawn Guo 		pdev->id_entry = of_id->data;
3228937cb60SShawn Guo 	hwtype = pdev->id_entry->driver_data;
323e7fc6ae7SShawn Guo 
324e7fc6ae7SShawn Guo 	if (mxc_gpio_hwtype) {
325e7fc6ae7SShawn Guo 		/*
326e7fc6ae7SShawn Guo 		 * The driver works with a reasonable presupposition,
327e7fc6ae7SShawn Guo 		 * that is all gpio ports must be the same type when
328e7fc6ae7SShawn Guo 		 * running on one soc.
329e7fc6ae7SShawn Guo 		 */
330e7fc6ae7SShawn Guo 		BUG_ON(mxc_gpio_hwtype != hwtype);
331e7fc6ae7SShawn Guo 		return;
332e7fc6ae7SShawn Guo 	}
333e7fc6ae7SShawn Guo 
334e7fc6ae7SShawn Guo 	if (hwtype == IMX31_GPIO)
335e7fc6ae7SShawn Guo 		mxc_gpio_hwdata = &imx31_gpio_hwdata;
336e7fc6ae7SShawn Guo 	else
337e7fc6ae7SShawn Guo 		mxc_gpio_hwdata = &imx1_imx21_gpio_hwdata;
338e7fc6ae7SShawn Guo 
339e7fc6ae7SShawn Guo 	mxc_gpio_hwtype = hwtype;
340e7fc6ae7SShawn Guo }
341e7fc6ae7SShawn Guo 
34209ad8039SShawn Guo static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
34309ad8039SShawn Guo {
34409ad8039SShawn Guo 	struct bgpio_chip *bgc = to_bgpio_chip(gc);
34509ad8039SShawn Guo 	struct mxc_gpio_port *port =
34609ad8039SShawn Guo 		container_of(bgc, struct mxc_gpio_port, bgc);
34709ad8039SShawn Guo 
34809ad8039SShawn Guo 	return port->virtual_irq_start + offset;
34909ad8039SShawn Guo }
35009ad8039SShawn Guo 
351b78d8e59SShawn Guo static int __devinit mxc_gpio_probe(struct platform_device *pdev)
352d37a65bbSShawn Guo {
3538937cb60SShawn Guo 	struct device_node *np = pdev->dev.of_node;
354b78d8e59SShawn Guo 	struct mxc_gpio_port *port;
355b78d8e59SShawn Guo 	struct resource *iores;
356e4ea9333SShawn Guo 	int err;
357d37a65bbSShawn Guo 
358e7fc6ae7SShawn Guo 	mxc_gpio_get_hw(pdev);
359e7fc6ae7SShawn Guo 
360b78d8e59SShawn Guo 	port = kzalloc(sizeof(struct mxc_gpio_port), GFP_KERNEL);
361b78d8e59SShawn Guo 	if (!port)
362b78d8e59SShawn Guo 		return -ENOMEM;
363d37a65bbSShawn Guo 
364b78d8e59SShawn Guo 	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
365b78d8e59SShawn Guo 	if (!iores) {
366b78d8e59SShawn Guo 		err = -ENODEV;
367b78d8e59SShawn Guo 		goto out_kfree;
368b78d8e59SShawn Guo 	}
369b78d8e59SShawn Guo 
370b78d8e59SShawn Guo 	if (!request_mem_region(iores->start, resource_size(iores),
371b78d8e59SShawn Guo 				pdev->name)) {
372b78d8e59SShawn Guo 		err = -EBUSY;
373b78d8e59SShawn Guo 		goto out_kfree;
374b78d8e59SShawn Guo 	}
375b78d8e59SShawn Guo 
376b78d8e59SShawn Guo 	port->base = ioremap(iores->start, resource_size(iores));
377b78d8e59SShawn Guo 	if (!port->base) {
378b78d8e59SShawn Guo 		err = -ENOMEM;
379b78d8e59SShawn Guo 		goto out_release_mem;
380b78d8e59SShawn Guo 	}
381b78d8e59SShawn Guo 
382b78d8e59SShawn Guo 	port->irq_high = platform_get_irq(pdev, 1);
383b78d8e59SShawn Guo 	port->irq = platform_get_irq(pdev, 0);
384b78d8e59SShawn Guo 	if (port->irq < 0) {
385b78d8e59SShawn Guo 		err = -EINVAL;
386b78d8e59SShawn Guo 		goto out_iounmap;
387b78d8e59SShawn Guo 	}
388b78d8e59SShawn Guo 
389d37a65bbSShawn Guo 	/* disable the interrupt and clear the status */
390b78d8e59SShawn Guo 	writel(0, port->base + GPIO_IMR);
391b78d8e59SShawn Guo 	writel(~0, port->base + GPIO_ISR);
392d37a65bbSShawn Guo 
393e7fc6ae7SShawn Guo 	if (mxc_gpio_hwtype == IMX21_GPIO) {
394d37a65bbSShawn Guo 		/* setup one handler for all GPIO interrupts */
395b78d8e59SShawn Guo 		if (pdev->id == 0)
396b78d8e59SShawn Guo 			irq_set_chained_handler(port->irq,
397b78d8e59SShawn Guo 						mx2_gpio_irq_handler);
398b78d8e59SShawn Guo 	} else {
399b78d8e59SShawn Guo 		/* setup one handler for each entry */
400b78d8e59SShawn Guo 		irq_set_chained_handler(port->irq, mx3_gpio_irq_handler);
401b78d8e59SShawn Guo 		irq_set_handler_data(port->irq, port);
402b78d8e59SShawn Guo 		if (port->irq_high > 0) {
403b78d8e59SShawn Guo 			/* setup handler for GPIO 16 to 31 */
404b78d8e59SShawn Guo 			irq_set_chained_handler(port->irq_high,
405b78d8e59SShawn Guo 						mx3_gpio_irq_handler);
406b78d8e59SShawn Guo 			irq_set_handler_data(port->irq_high, port);
407b78d8e59SShawn Guo 		}
408d37a65bbSShawn Guo 	}
409d37a65bbSShawn Guo 
4102ce420daSShawn Guo 	err = bgpio_init(&port->bgc, &pdev->dev, 4,
4112ce420daSShawn Guo 			 port->base + GPIO_PSR,
4122ce420daSShawn Guo 			 port->base + GPIO_DR, NULL,
4132ce420daSShawn Guo 			 port->base + GPIO_GDIR, NULL, false);
414b78d8e59SShawn Guo 	if (err)
415b78d8e59SShawn Guo 		goto out_iounmap;
416b78d8e59SShawn Guo 
41709ad8039SShawn Guo 	port->bgc.gc.to_irq = mxc_gpio_to_irq;
4182ce420daSShawn Guo 	port->bgc.gc.base = pdev->id * 32;
419fb149218SLothar Waßmann 	port->bgc.dir = port->bgc.read_reg(port->bgc.reg_dir);
420fb149218SLothar Waßmann 	port->bgc.data = port->bgc.read_reg(port->bgc.reg_set);
4212ce420daSShawn Guo 
4222ce420daSShawn Guo 	err = gpiochip_add(&port->bgc.gc);
4232ce420daSShawn Guo 	if (err)
4242ce420daSShawn Guo 		goto out_bgpio_remove;
4252ce420daSShawn Guo 
4268937cb60SShawn Guo 	/*
4278937cb60SShawn Guo 	 * In dt case, we use gpio number range dynamically
4288937cb60SShawn Guo 	 * allocated by gpio core.
4298937cb60SShawn Guo 	 */
4308937cb60SShawn Guo 	port->virtual_irq_start = MXC_GPIO_IRQ_START + (np ? port->bgc.gc.base :
4318937cb60SShawn Guo 							     pdev->id * 32);
4328937cb60SShawn Guo 
4338937cb60SShawn Guo 	/* gpio-mxc can be a generic irq chip */
4348937cb60SShawn Guo 	mxc_gpio_init_gc(port);
4358937cb60SShawn Guo 
436b78d8e59SShawn Guo 	list_add_tail(&port->node, &mxc_gpio_ports);
437b78d8e59SShawn Guo 
438d37a65bbSShawn Guo 	return 0;
439b78d8e59SShawn Guo 
4402ce420daSShawn Guo out_bgpio_remove:
4412ce420daSShawn Guo 	bgpio_remove(&port->bgc);
442b78d8e59SShawn Guo out_iounmap:
443b78d8e59SShawn Guo 	iounmap(port->base);
444b78d8e59SShawn Guo out_release_mem:
445b78d8e59SShawn Guo 	release_mem_region(iores->start, resource_size(iores));
446b78d8e59SShawn Guo out_kfree:
447b78d8e59SShawn Guo 	kfree(port);
448b78d8e59SShawn Guo 	dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
449b78d8e59SShawn Guo 	return err;
450d37a65bbSShawn Guo }
451b78d8e59SShawn Guo 
452b78d8e59SShawn Guo static struct platform_driver mxc_gpio_driver = {
453b78d8e59SShawn Guo 	.driver		= {
454b78d8e59SShawn Guo 		.name	= "gpio-mxc",
455b78d8e59SShawn Guo 		.owner	= THIS_MODULE,
4568937cb60SShawn Guo 		.of_match_table = mxc_gpio_dt_ids,
457b78d8e59SShawn Guo 	},
458b78d8e59SShawn Guo 	.probe		= mxc_gpio_probe,
459e7fc6ae7SShawn Guo 	.id_table	= mxc_gpio_devtype,
460b78d8e59SShawn Guo };
461b78d8e59SShawn Guo 
462b78d8e59SShawn Guo static int __init gpio_mxc_init(void)
463b78d8e59SShawn Guo {
464b78d8e59SShawn Guo 	return platform_driver_register(&mxc_gpio_driver);
465b78d8e59SShawn Guo }
466b78d8e59SShawn Guo postcore_initcall(gpio_mxc_init);
467b78d8e59SShawn Guo 
468b78d8e59SShawn Guo MODULE_AUTHOR("Freescale Semiconductor, "
469b78d8e59SShawn Guo 	      "Daniel Mack <danielncaiaq.de>, "
470b78d8e59SShawn Guo 	      "Juergen Beisert <kernel@pengutronix.de>");
471b78d8e59SShawn Guo MODULE_DESCRIPTION("Freescale MXC GPIO");
472b78d8e59SShawn Guo MODULE_LICENSE("GPL");
473