xref: /openbmc/linux/drivers/gpio/gpio-mxc.c (revision 33a4e985)
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>
32bb207ef1SPaul Gortmaker #include <linux/module.h>
33d37a65bbSShawn Guo #include <asm-generic/bug.h>
340e44b6ecSShawn Guo #include <asm/mach/irq.h>
35d37a65bbSShawn Guo 
36a4395612SShawn Guo #define irq_to_gpio(irq)	((irq) - MXC_GPIO_IRQ_START)
37a4395612SShawn Guo 
38e7fc6ae7SShawn Guo enum mxc_gpio_hwtype {
39e7fc6ae7SShawn Guo 	IMX1_GPIO,	/* runs on i.mx1 */
40e7fc6ae7SShawn Guo 	IMX21_GPIO,	/* runs on i.mx21 and i.mx27 */
41e7fc6ae7SShawn Guo 	IMX31_GPIO,	/* runs on all other i.mx */
42e7fc6ae7SShawn Guo };
43e7fc6ae7SShawn Guo 
44e7fc6ae7SShawn Guo /* device type dependent stuff */
45e7fc6ae7SShawn Guo struct mxc_gpio_hwdata {
46e7fc6ae7SShawn Guo 	unsigned dr_reg;
47e7fc6ae7SShawn Guo 	unsigned gdir_reg;
48e7fc6ae7SShawn Guo 	unsigned psr_reg;
49e7fc6ae7SShawn Guo 	unsigned icr1_reg;
50e7fc6ae7SShawn Guo 	unsigned icr2_reg;
51e7fc6ae7SShawn Guo 	unsigned imr_reg;
52e7fc6ae7SShawn Guo 	unsigned isr_reg;
53e7fc6ae7SShawn Guo 	unsigned low_level;
54e7fc6ae7SShawn Guo 	unsigned high_level;
55e7fc6ae7SShawn Guo 	unsigned rise_edge;
56e7fc6ae7SShawn Guo 	unsigned fall_edge;
57e7fc6ae7SShawn Guo };
58e7fc6ae7SShawn Guo 
59b78d8e59SShawn Guo struct mxc_gpio_port {
60b78d8e59SShawn Guo 	struct list_head node;
61b78d8e59SShawn Guo 	void __iomem *base;
62b78d8e59SShawn Guo 	int irq;
63b78d8e59SShawn Guo 	int irq_high;
64b78d8e59SShawn Guo 	int virtual_irq_start;
652ce420daSShawn Guo 	struct bgpio_chip bgc;
66b78d8e59SShawn Guo 	u32 both_edges;
67b78d8e59SShawn Guo };
68b78d8e59SShawn Guo 
69e7fc6ae7SShawn Guo static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
70e7fc6ae7SShawn Guo 	.dr_reg		= 0x1c,
71e7fc6ae7SShawn Guo 	.gdir_reg	= 0x00,
72e7fc6ae7SShawn Guo 	.psr_reg	= 0x24,
73e7fc6ae7SShawn Guo 	.icr1_reg	= 0x28,
74e7fc6ae7SShawn Guo 	.icr2_reg	= 0x2c,
75e7fc6ae7SShawn Guo 	.imr_reg	= 0x30,
76e7fc6ae7SShawn Guo 	.isr_reg	= 0x34,
77e7fc6ae7SShawn Guo 	.low_level	= 0x03,
78e7fc6ae7SShawn Guo 	.high_level	= 0x02,
79e7fc6ae7SShawn Guo 	.rise_edge	= 0x00,
80e7fc6ae7SShawn Guo 	.fall_edge	= 0x01,
81e7fc6ae7SShawn Guo };
82e7fc6ae7SShawn Guo 
83e7fc6ae7SShawn Guo static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
84e7fc6ae7SShawn Guo 	.dr_reg		= 0x00,
85e7fc6ae7SShawn Guo 	.gdir_reg	= 0x04,
86e7fc6ae7SShawn Guo 	.psr_reg	= 0x08,
87e7fc6ae7SShawn Guo 	.icr1_reg	= 0x0c,
88e7fc6ae7SShawn Guo 	.icr2_reg	= 0x10,
89e7fc6ae7SShawn Guo 	.imr_reg	= 0x14,
90e7fc6ae7SShawn Guo 	.isr_reg	= 0x18,
91e7fc6ae7SShawn Guo 	.low_level	= 0x00,
92e7fc6ae7SShawn Guo 	.high_level	= 0x01,
93e7fc6ae7SShawn Guo 	.rise_edge	= 0x02,
94e7fc6ae7SShawn Guo 	.fall_edge	= 0x03,
95e7fc6ae7SShawn Guo };
96e7fc6ae7SShawn Guo 
97e7fc6ae7SShawn Guo static enum mxc_gpio_hwtype mxc_gpio_hwtype;
98e7fc6ae7SShawn Guo static struct mxc_gpio_hwdata *mxc_gpio_hwdata;
99e7fc6ae7SShawn Guo 
100e7fc6ae7SShawn Guo #define GPIO_DR			(mxc_gpio_hwdata->dr_reg)
101e7fc6ae7SShawn Guo #define GPIO_GDIR		(mxc_gpio_hwdata->gdir_reg)
102e7fc6ae7SShawn Guo #define GPIO_PSR		(mxc_gpio_hwdata->psr_reg)
103e7fc6ae7SShawn Guo #define GPIO_ICR1		(mxc_gpio_hwdata->icr1_reg)
104e7fc6ae7SShawn Guo #define GPIO_ICR2		(mxc_gpio_hwdata->icr2_reg)
105e7fc6ae7SShawn Guo #define GPIO_IMR		(mxc_gpio_hwdata->imr_reg)
106e7fc6ae7SShawn Guo #define GPIO_ISR		(mxc_gpio_hwdata->isr_reg)
107e7fc6ae7SShawn Guo 
108e7fc6ae7SShawn Guo #define GPIO_INT_LOW_LEV	(mxc_gpio_hwdata->low_level)
109e7fc6ae7SShawn Guo #define GPIO_INT_HIGH_LEV	(mxc_gpio_hwdata->high_level)
110e7fc6ae7SShawn Guo #define GPIO_INT_RISE_EDGE	(mxc_gpio_hwdata->rise_edge)
111e7fc6ae7SShawn Guo #define GPIO_INT_FALL_EDGE	(mxc_gpio_hwdata->fall_edge)
112e7fc6ae7SShawn Guo #define GPIO_INT_NONE		0x4
113e7fc6ae7SShawn Guo 
114e7fc6ae7SShawn Guo static struct platform_device_id mxc_gpio_devtype[] = {
115e7fc6ae7SShawn Guo 	{
116e7fc6ae7SShawn Guo 		.name = "imx1-gpio",
117e7fc6ae7SShawn Guo 		.driver_data = IMX1_GPIO,
118e7fc6ae7SShawn Guo 	}, {
119e7fc6ae7SShawn Guo 		.name = "imx21-gpio",
120e7fc6ae7SShawn Guo 		.driver_data = IMX21_GPIO,
121e7fc6ae7SShawn Guo 	}, {
122e7fc6ae7SShawn Guo 		.name = "imx31-gpio",
123e7fc6ae7SShawn Guo 		.driver_data = IMX31_GPIO,
124e7fc6ae7SShawn Guo 	}, {
125e7fc6ae7SShawn Guo 		/* sentinel */
126e7fc6ae7SShawn Guo 	}
127e7fc6ae7SShawn Guo };
128e7fc6ae7SShawn Guo 
1298937cb60SShawn Guo static const struct of_device_id mxc_gpio_dt_ids[] = {
1308937cb60SShawn Guo 	{ .compatible = "fsl,imx1-gpio", .data = &mxc_gpio_devtype[IMX1_GPIO], },
1318937cb60SShawn Guo 	{ .compatible = "fsl,imx21-gpio", .data = &mxc_gpio_devtype[IMX21_GPIO], },
1328937cb60SShawn Guo 	{ .compatible = "fsl,imx31-gpio", .data = &mxc_gpio_devtype[IMX31_GPIO], },
1338937cb60SShawn Guo 	{ /* sentinel */ }
1348937cb60SShawn Guo };
1358937cb60SShawn Guo 
136b78d8e59SShawn Guo /*
137b78d8e59SShawn Guo  * MX2 has one interrupt *for all* gpio ports. The list is used
138b78d8e59SShawn Guo  * to save the references to all ports, so that mx2_gpio_irq_handler
139b78d8e59SShawn Guo  * can walk through all interrupt status registers.
140b78d8e59SShawn Guo  */
141b78d8e59SShawn Guo static LIST_HEAD(mxc_gpio_ports);
142d37a65bbSShawn Guo 
143d37a65bbSShawn Guo /* Note: This driver assumes 32 GPIOs are handled in one register */
144d37a65bbSShawn Guo 
145d37a65bbSShawn Guo static int gpio_set_irq_type(struct irq_data *d, u32 type)
146d37a65bbSShawn Guo {
147d37a65bbSShawn Guo 	u32 gpio = irq_to_gpio(d->irq);
148e4ea9333SShawn Guo 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
149e4ea9333SShawn Guo 	struct mxc_gpio_port *port = gc->private;
150d37a65bbSShawn Guo 	u32 bit, val;
151d37a65bbSShawn Guo 	int edge;
152d37a65bbSShawn Guo 	void __iomem *reg = port->base;
153d37a65bbSShawn Guo 
154d37a65bbSShawn Guo 	port->both_edges &= ~(1 << (gpio & 31));
155d37a65bbSShawn Guo 	switch (type) {
156d37a65bbSShawn Guo 	case IRQ_TYPE_EDGE_RISING:
157d37a65bbSShawn Guo 		edge = GPIO_INT_RISE_EDGE;
158d37a65bbSShawn Guo 		break;
159d37a65bbSShawn Guo 	case IRQ_TYPE_EDGE_FALLING:
160d37a65bbSShawn Guo 		edge = GPIO_INT_FALL_EDGE;
161d37a65bbSShawn Guo 		break;
162d37a65bbSShawn Guo 	case IRQ_TYPE_EDGE_BOTH:
1635523f86bSShawn Guo 		val = gpio_get_value(gpio);
164d37a65bbSShawn Guo 		if (val) {
165d37a65bbSShawn Guo 			edge = GPIO_INT_LOW_LEV;
166d37a65bbSShawn Guo 			pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
167d37a65bbSShawn Guo 		} else {
168d37a65bbSShawn Guo 			edge = GPIO_INT_HIGH_LEV;
169d37a65bbSShawn Guo 			pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
170d37a65bbSShawn Guo 		}
171d37a65bbSShawn Guo 		port->both_edges |= 1 << (gpio & 31);
172d37a65bbSShawn Guo 		break;
173d37a65bbSShawn Guo 	case IRQ_TYPE_LEVEL_LOW:
174d37a65bbSShawn Guo 		edge = GPIO_INT_LOW_LEV;
175d37a65bbSShawn Guo 		break;
176d37a65bbSShawn Guo 	case IRQ_TYPE_LEVEL_HIGH:
177d37a65bbSShawn Guo 		edge = GPIO_INT_HIGH_LEV;
178d37a65bbSShawn Guo 		break;
179d37a65bbSShawn Guo 	default:
180d37a65bbSShawn Guo 		return -EINVAL;
181d37a65bbSShawn Guo 	}
182d37a65bbSShawn Guo 
183d37a65bbSShawn Guo 	reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
184d37a65bbSShawn Guo 	bit = gpio & 0xf;
185b78d8e59SShawn Guo 	val = readl(reg) & ~(0x3 << (bit << 1));
186b78d8e59SShawn Guo 	writel(val | (edge << (bit << 1)), reg);
187e4ea9333SShawn Guo 	writel(1 << (gpio & 0x1f), port->base + GPIO_ISR);
188d37a65bbSShawn Guo 
189d37a65bbSShawn Guo 	return 0;
190d37a65bbSShawn Guo }
191d37a65bbSShawn Guo 
192d37a65bbSShawn Guo static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
193d37a65bbSShawn Guo {
194d37a65bbSShawn Guo 	void __iomem *reg = port->base;
195d37a65bbSShawn Guo 	u32 bit, val;
196d37a65bbSShawn Guo 	int edge;
197d37a65bbSShawn Guo 
198d37a65bbSShawn Guo 	reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
199d37a65bbSShawn Guo 	bit = gpio & 0xf;
200b78d8e59SShawn Guo 	val = readl(reg);
201d37a65bbSShawn Guo 	edge = (val >> (bit << 1)) & 3;
202d37a65bbSShawn Guo 	val &= ~(0x3 << (bit << 1));
203d37a65bbSShawn Guo 	if (edge == GPIO_INT_HIGH_LEV) {
204d37a65bbSShawn Guo 		edge = GPIO_INT_LOW_LEV;
205d37a65bbSShawn Guo 		pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
206d37a65bbSShawn Guo 	} else if (edge == GPIO_INT_LOW_LEV) {
207d37a65bbSShawn Guo 		edge = GPIO_INT_HIGH_LEV;
208d37a65bbSShawn Guo 		pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
209d37a65bbSShawn Guo 	} else {
210d37a65bbSShawn Guo 		pr_err("mxc: invalid configuration for GPIO %d: %x\n",
211d37a65bbSShawn Guo 		       gpio, edge);
212d37a65bbSShawn Guo 		return;
213d37a65bbSShawn Guo 	}
214b78d8e59SShawn Guo 	writel(val | (edge << (bit << 1)), reg);
215d37a65bbSShawn Guo }
216d37a65bbSShawn Guo 
217d37a65bbSShawn Guo /* handle 32 interrupts in one status register */
218d37a65bbSShawn Guo static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
219d37a65bbSShawn Guo {
220d37a65bbSShawn Guo 	u32 gpio_irq_no_base = port->virtual_irq_start;
221d37a65bbSShawn Guo 
222d37a65bbSShawn Guo 	while (irq_stat != 0) {
223d37a65bbSShawn Guo 		int irqoffset = fls(irq_stat) - 1;
224d37a65bbSShawn Guo 
225d37a65bbSShawn Guo 		if (port->both_edges & (1 << irqoffset))
226d37a65bbSShawn Guo 			mxc_flip_edge(port, irqoffset);
227d37a65bbSShawn Guo 
228d37a65bbSShawn Guo 		generic_handle_irq(gpio_irq_no_base + irqoffset);
229d37a65bbSShawn Guo 
230d37a65bbSShawn Guo 		irq_stat &= ~(1 << irqoffset);
231d37a65bbSShawn Guo 	}
232d37a65bbSShawn Guo }
233d37a65bbSShawn Guo 
234d37a65bbSShawn Guo /* MX1 and MX3 has one interrupt *per* gpio port */
235d37a65bbSShawn Guo static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
236d37a65bbSShawn Guo {
237d37a65bbSShawn Guo 	u32 irq_stat;
238d37a65bbSShawn Guo 	struct mxc_gpio_port *port = irq_get_handler_data(irq);
2390e44b6ecSShawn Guo 	struct irq_chip *chip = irq_get_chip(irq);
2400e44b6ecSShawn Guo 
2410e44b6ecSShawn Guo 	chained_irq_enter(chip, desc);
242d37a65bbSShawn Guo 
243b78d8e59SShawn Guo 	irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
244d37a65bbSShawn Guo 
245d37a65bbSShawn Guo 	mxc_gpio_irq_handler(port, irq_stat);
2460e44b6ecSShawn Guo 
2470e44b6ecSShawn Guo 	chained_irq_exit(chip, desc);
248d37a65bbSShawn Guo }
249d37a65bbSShawn Guo 
250d37a65bbSShawn Guo /* MX2 has one interrupt *for all* gpio ports */
251d37a65bbSShawn Guo static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
252d37a65bbSShawn Guo {
253d37a65bbSShawn Guo 	u32 irq_msk, irq_stat;
254b78d8e59SShawn Guo 	struct mxc_gpio_port *port;
255d37a65bbSShawn Guo 
256d37a65bbSShawn Guo 	/* walk through all interrupt status registers */
257b78d8e59SShawn Guo 	list_for_each_entry(port, &mxc_gpio_ports, node) {
258b78d8e59SShawn Guo 		irq_msk = readl(port->base + GPIO_IMR);
259d37a65bbSShawn Guo 		if (!irq_msk)
260d37a65bbSShawn Guo 			continue;
261d37a65bbSShawn Guo 
262b78d8e59SShawn Guo 		irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
263d37a65bbSShawn Guo 		if (irq_stat)
264b78d8e59SShawn Guo 			mxc_gpio_irq_handler(port, irq_stat);
265d37a65bbSShawn Guo 	}
266d37a65bbSShawn Guo }
267d37a65bbSShawn Guo 
268d37a65bbSShawn Guo /*
269d37a65bbSShawn Guo  * Set interrupt number "irq" in the GPIO as a wake-up source.
270d37a65bbSShawn Guo  * While system is running, all registered GPIO interrupts need to have
271d37a65bbSShawn Guo  * wake-up enabled. When system is suspended, only selected GPIO interrupts
272d37a65bbSShawn Guo  * need to have wake-up enabled.
273d37a65bbSShawn Guo  * @param  irq          interrupt source number
274d37a65bbSShawn Guo  * @param  enable       enable as wake-up if equal to non-zero
275d37a65bbSShawn Guo  * @return       This function returns 0 on success.
276d37a65bbSShawn Guo  */
277d37a65bbSShawn Guo static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
278d37a65bbSShawn Guo {
279d37a65bbSShawn Guo 	u32 gpio = irq_to_gpio(d->irq);
280d37a65bbSShawn Guo 	u32 gpio_idx = gpio & 0x1F;
281e4ea9333SShawn Guo 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
282e4ea9333SShawn Guo 	struct mxc_gpio_port *port = gc->private;
283d37a65bbSShawn Guo 
284d37a65bbSShawn Guo 	if (enable) {
285d37a65bbSShawn Guo 		if (port->irq_high && (gpio_idx >= 16))
286d37a65bbSShawn Guo 			enable_irq_wake(port->irq_high);
287d37a65bbSShawn Guo 		else
288d37a65bbSShawn Guo 			enable_irq_wake(port->irq);
289d37a65bbSShawn Guo 	} else {
290d37a65bbSShawn Guo 		if (port->irq_high && (gpio_idx >= 16))
291d37a65bbSShawn Guo 			disable_irq_wake(port->irq_high);
292d37a65bbSShawn Guo 		else
293d37a65bbSShawn Guo 			disable_irq_wake(port->irq);
294d37a65bbSShawn Guo 	}
295d37a65bbSShawn Guo 
296d37a65bbSShawn Guo 	return 0;
297d37a65bbSShawn Guo }
298d37a65bbSShawn Guo 
299e4ea9333SShawn Guo static void __init mxc_gpio_init_gc(struct mxc_gpio_port *port)
300e4ea9333SShawn Guo {
301e4ea9333SShawn Guo 	struct irq_chip_generic *gc;
302e4ea9333SShawn Guo 	struct irq_chip_type *ct;
303d37a65bbSShawn Guo 
304e4ea9333SShawn Guo 	gc = irq_alloc_generic_chip("gpio-mxc", 1, port->virtual_irq_start,
305e4ea9333SShawn Guo 				    port->base, handle_level_irq);
306e4ea9333SShawn Guo 	gc->private = port;
307e4ea9333SShawn Guo 
308e4ea9333SShawn Guo 	ct = gc->chip_types;
309591567a5SShawn Guo 	ct->chip.irq_ack = irq_gc_ack_set_bit;
310e4ea9333SShawn Guo 	ct->chip.irq_mask = irq_gc_mask_clr_bit;
311e4ea9333SShawn Guo 	ct->chip.irq_unmask = irq_gc_mask_set_bit;
312e4ea9333SShawn Guo 	ct->chip.irq_set_type = gpio_set_irq_type;
313591567a5SShawn Guo 	ct->chip.irq_set_wake = gpio_set_wake_irq;
314e4ea9333SShawn Guo 	ct->regs.ack = GPIO_ISR;
315e4ea9333SShawn Guo 	ct->regs.mask = GPIO_IMR;
316e4ea9333SShawn Guo 
317e4ea9333SShawn Guo 	irq_setup_generic_chip(gc, IRQ_MSK(32), IRQ_GC_INIT_NESTED_LOCK,
318e4ea9333SShawn Guo 			       IRQ_NOREQUEST, 0);
319e4ea9333SShawn Guo }
320d37a65bbSShawn Guo 
321e7fc6ae7SShawn Guo static void __devinit mxc_gpio_get_hw(struct platform_device *pdev)
322e7fc6ae7SShawn Guo {
3238937cb60SShawn Guo 	const struct of_device_id *of_id =
3248937cb60SShawn Guo 			of_match_device(mxc_gpio_dt_ids, &pdev->dev);
3258937cb60SShawn Guo 	enum mxc_gpio_hwtype hwtype;
3268937cb60SShawn Guo 
3278937cb60SShawn Guo 	if (of_id)
3288937cb60SShawn Guo 		pdev->id_entry = of_id->data;
3298937cb60SShawn Guo 	hwtype = pdev->id_entry->driver_data;
330e7fc6ae7SShawn Guo 
331e7fc6ae7SShawn Guo 	if (mxc_gpio_hwtype) {
332e7fc6ae7SShawn Guo 		/*
333e7fc6ae7SShawn Guo 		 * The driver works with a reasonable presupposition,
334e7fc6ae7SShawn Guo 		 * that is all gpio ports must be the same type when
335e7fc6ae7SShawn Guo 		 * running on one soc.
336e7fc6ae7SShawn Guo 		 */
337e7fc6ae7SShawn Guo 		BUG_ON(mxc_gpio_hwtype != hwtype);
338e7fc6ae7SShawn Guo 		return;
339e7fc6ae7SShawn Guo 	}
340e7fc6ae7SShawn Guo 
341e7fc6ae7SShawn Guo 	if (hwtype == IMX31_GPIO)
342e7fc6ae7SShawn Guo 		mxc_gpio_hwdata = &imx31_gpio_hwdata;
343e7fc6ae7SShawn Guo 	else
344e7fc6ae7SShawn Guo 		mxc_gpio_hwdata = &imx1_imx21_gpio_hwdata;
345e7fc6ae7SShawn Guo 
346e7fc6ae7SShawn Guo 	mxc_gpio_hwtype = hwtype;
347e7fc6ae7SShawn Guo }
348e7fc6ae7SShawn Guo 
34909ad8039SShawn Guo static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
35009ad8039SShawn Guo {
35109ad8039SShawn Guo 	struct bgpio_chip *bgc = to_bgpio_chip(gc);
35209ad8039SShawn Guo 	struct mxc_gpio_port *port =
35309ad8039SShawn Guo 		container_of(bgc, struct mxc_gpio_port, bgc);
35409ad8039SShawn Guo 
35509ad8039SShawn Guo 	return port->virtual_irq_start + offset;
35609ad8039SShawn Guo }
35709ad8039SShawn Guo 
358b78d8e59SShawn Guo static int __devinit mxc_gpio_probe(struct platform_device *pdev)
359d37a65bbSShawn Guo {
3608937cb60SShawn Guo 	struct device_node *np = pdev->dev.of_node;
361b78d8e59SShawn Guo 	struct mxc_gpio_port *port;
362b78d8e59SShawn Guo 	struct resource *iores;
363e4ea9333SShawn Guo 	int err;
364d37a65bbSShawn Guo 
365e7fc6ae7SShawn Guo 	mxc_gpio_get_hw(pdev);
366e7fc6ae7SShawn Guo 
367b78d8e59SShawn Guo 	port = kzalloc(sizeof(struct mxc_gpio_port), GFP_KERNEL);
368b78d8e59SShawn Guo 	if (!port)
369b78d8e59SShawn Guo 		return -ENOMEM;
370d37a65bbSShawn Guo 
371b78d8e59SShawn Guo 	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
372b78d8e59SShawn Guo 	if (!iores) {
373b78d8e59SShawn Guo 		err = -ENODEV;
374b78d8e59SShawn Guo 		goto out_kfree;
375b78d8e59SShawn Guo 	}
376b78d8e59SShawn Guo 
377b78d8e59SShawn Guo 	if (!request_mem_region(iores->start, resource_size(iores),
378b78d8e59SShawn Guo 				pdev->name)) {
379b78d8e59SShawn Guo 		err = -EBUSY;
380b78d8e59SShawn Guo 		goto out_kfree;
381b78d8e59SShawn Guo 	}
382b78d8e59SShawn Guo 
383b78d8e59SShawn Guo 	port->base = ioremap(iores->start, resource_size(iores));
384b78d8e59SShawn Guo 	if (!port->base) {
385b78d8e59SShawn Guo 		err = -ENOMEM;
386b78d8e59SShawn Guo 		goto out_release_mem;
387b78d8e59SShawn Guo 	}
388b78d8e59SShawn Guo 
389b78d8e59SShawn Guo 	port->irq_high = platform_get_irq(pdev, 1);
390b78d8e59SShawn Guo 	port->irq = platform_get_irq(pdev, 0);
391b78d8e59SShawn Guo 	if (port->irq < 0) {
392b78d8e59SShawn Guo 		err = -EINVAL;
393b78d8e59SShawn Guo 		goto out_iounmap;
394b78d8e59SShawn Guo 	}
395b78d8e59SShawn Guo 
396d37a65bbSShawn Guo 	/* disable the interrupt and clear the status */
397b78d8e59SShawn Guo 	writel(0, port->base + GPIO_IMR);
398b78d8e59SShawn Guo 	writel(~0, port->base + GPIO_ISR);
399d37a65bbSShawn Guo 
400e7fc6ae7SShawn Guo 	if (mxc_gpio_hwtype == IMX21_GPIO) {
40133a4e985SUwe Kleine-König 		/*
40233a4e985SUwe Kleine-König 		 * Setup one handler for all GPIO interrupts. Actually setting
40333a4e985SUwe Kleine-König 		 * the handler is needed only once, but doing it for every port
40433a4e985SUwe Kleine-König 		 * is more robust and easier.
40533a4e985SUwe Kleine-König 		 */
40633a4e985SUwe Kleine-König 		irq_set_chained_handler(port->irq, mx2_gpio_irq_handler);
407b78d8e59SShawn Guo 	} else {
408b78d8e59SShawn Guo 		/* setup one handler for each entry */
409b78d8e59SShawn Guo 		irq_set_chained_handler(port->irq, mx3_gpio_irq_handler);
410b78d8e59SShawn Guo 		irq_set_handler_data(port->irq, port);
411b78d8e59SShawn Guo 		if (port->irq_high > 0) {
412b78d8e59SShawn Guo 			/* setup handler for GPIO 16 to 31 */
413b78d8e59SShawn Guo 			irq_set_chained_handler(port->irq_high,
414b78d8e59SShawn Guo 						mx3_gpio_irq_handler);
415b78d8e59SShawn Guo 			irq_set_handler_data(port->irq_high, port);
416b78d8e59SShawn Guo 		}
417d37a65bbSShawn Guo 	}
418d37a65bbSShawn Guo 
4192ce420daSShawn Guo 	err = bgpio_init(&port->bgc, &pdev->dev, 4,
4202ce420daSShawn Guo 			 port->base + GPIO_PSR,
4212ce420daSShawn Guo 			 port->base + GPIO_DR, NULL,
4223e11f7b8SShawn Guo 			 port->base + GPIO_GDIR, NULL, 0);
423b78d8e59SShawn Guo 	if (err)
424b78d8e59SShawn Guo 		goto out_iounmap;
425b78d8e59SShawn Guo 
42609ad8039SShawn Guo 	port->bgc.gc.to_irq = mxc_gpio_to_irq;
4272ce420daSShawn Guo 	port->bgc.gc.base = pdev->id * 32;
428fb149218SLothar Waßmann 	port->bgc.dir = port->bgc.read_reg(port->bgc.reg_dir);
429fb149218SLothar Waßmann 	port->bgc.data = port->bgc.read_reg(port->bgc.reg_set);
4302ce420daSShawn Guo 
4312ce420daSShawn Guo 	err = gpiochip_add(&port->bgc.gc);
4322ce420daSShawn Guo 	if (err)
4332ce420daSShawn Guo 		goto out_bgpio_remove;
4342ce420daSShawn Guo 
4358937cb60SShawn Guo 	/*
4368937cb60SShawn Guo 	 * In dt case, we use gpio number range dynamically
4378937cb60SShawn Guo 	 * allocated by gpio core.
4388937cb60SShawn Guo 	 */
4398937cb60SShawn Guo 	port->virtual_irq_start = MXC_GPIO_IRQ_START + (np ? port->bgc.gc.base :
4408937cb60SShawn Guo 							     pdev->id * 32);
4418937cb60SShawn Guo 
4428937cb60SShawn Guo 	/* gpio-mxc can be a generic irq chip */
4438937cb60SShawn Guo 	mxc_gpio_init_gc(port);
4448937cb60SShawn Guo 
445b78d8e59SShawn Guo 	list_add_tail(&port->node, &mxc_gpio_ports);
446b78d8e59SShawn Guo 
447d37a65bbSShawn Guo 	return 0;
448b78d8e59SShawn Guo 
4492ce420daSShawn Guo out_bgpio_remove:
4502ce420daSShawn Guo 	bgpio_remove(&port->bgc);
451b78d8e59SShawn Guo out_iounmap:
452b78d8e59SShawn Guo 	iounmap(port->base);
453b78d8e59SShawn Guo out_release_mem:
454b78d8e59SShawn Guo 	release_mem_region(iores->start, resource_size(iores));
455b78d8e59SShawn Guo out_kfree:
456b78d8e59SShawn Guo 	kfree(port);
457b78d8e59SShawn Guo 	dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
458b78d8e59SShawn Guo 	return err;
459d37a65bbSShawn Guo }
460b78d8e59SShawn Guo 
461b78d8e59SShawn Guo static struct platform_driver mxc_gpio_driver = {
462b78d8e59SShawn Guo 	.driver		= {
463b78d8e59SShawn Guo 		.name	= "gpio-mxc",
464b78d8e59SShawn Guo 		.owner	= THIS_MODULE,
4658937cb60SShawn Guo 		.of_match_table = mxc_gpio_dt_ids,
466b78d8e59SShawn Guo 	},
467b78d8e59SShawn Guo 	.probe		= mxc_gpio_probe,
468e7fc6ae7SShawn Guo 	.id_table	= mxc_gpio_devtype,
469b78d8e59SShawn Guo };
470b78d8e59SShawn Guo 
471b78d8e59SShawn Guo static int __init gpio_mxc_init(void)
472b78d8e59SShawn Guo {
473b78d8e59SShawn Guo 	return platform_driver_register(&mxc_gpio_driver);
474b78d8e59SShawn Guo }
475b78d8e59SShawn Guo postcore_initcall(gpio_mxc_init);
476b78d8e59SShawn Guo 
477b78d8e59SShawn Guo MODULE_AUTHOR("Freescale Semiconductor, "
478b78d8e59SShawn Guo 	      "Daniel Mack <danielncaiaq.de>, "
479b78d8e59SShawn Guo 	      "Juergen Beisert <kernel@pengutronix.de>");
480b78d8e59SShawn Guo MODULE_DESCRIPTION("Freescale MXC GPIO");
481b78d8e59SShawn Guo MODULE_LICENSE("GPL");
482