xref: /OK3568_Linux_fs/kernel/drivers/gpio/gpio-mxc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
4*4882a593Smuzhiyun // Copyright 2008 Juergen Beisert, kernel@pengutronix.de
5*4882a593Smuzhiyun //
6*4882a593Smuzhiyun // Based on code from Freescale Semiconductor,
7*4882a593Smuzhiyun // Authors: Daniel Mack, Juergen Beisert.
8*4882a593Smuzhiyun // Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/clk.h>
11*4882a593Smuzhiyun #include <linux/err.h>
12*4882a593Smuzhiyun #include <linux/init.h>
13*4882a593Smuzhiyun #include <linux/interrupt.h>
14*4882a593Smuzhiyun #include <linux/io.h>
15*4882a593Smuzhiyun #include <linux/irq.h>
16*4882a593Smuzhiyun #include <linux/irqdomain.h>
17*4882a593Smuzhiyun #include <linux/irqchip/chained_irq.h>
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/platform_device.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun #include <linux/syscore_ops.h>
22*4882a593Smuzhiyun #include <linux/gpio/driver.h>
23*4882a593Smuzhiyun #include <linux/of.h>
24*4882a593Smuzhiyun #include <linux/of_device.h>
25*4882a593Smuzhiyun #include <linux/bug.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun enum mxc_gpio_hwtype {
28*4882a593Smuzhiyun 	IMX1_GPIO,	/* runs on i.mx1 */
29*4882a593Smuzhiyun 	IMX21_GPIO,	/* runs on i.mx21 and i.mx27 */
30*4882a593Smuzhiyun 	IMX31_GPIO,	/* runs on i.mx31 */
31*4882a593Smuzhiyun 	IMX35_GPIO,	/* runs on all other i.mx */
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /* device type dependent stuff */
35*4882a593Smuzhiyun struct mxc_gpio_hwdata {
36*4882a593Smuzhiyun 	unsigned dr_reg;
37*4882a593Smuzhiyun 	unsigned gdir_reg;
38*4882a593Smuzhiyun 	unsigned psr_reg;
39*4882a593Smuzhiyun 	unsigned icr1_reg;
40*4882a593Smuzhiyun 	unsigned icr2_reg;
41*4882a593Smuzhiyun 	unsigned imr_reg;
42*4882a593Smuzhiyun 	unsigned isr_reg;
43*4882a593Smuzhiyun 	int edge_sel_reg;
44*4882a593Smuzhiyun 	unsigned low_level;
45*4882a593Smuzhiyun 	unsigned high_level;
46*4882a593Smuzhiyun 	unsigned rise_edge;
47*4882a593Smuzhiyun 	unsigned fall_edge;
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun struct mxc_gpio_reg_saved {
51*4882a593Smuzhiyun 	u32 icr1;
52*4882a593Smuzhiyun 	u32 icr2;
53*4882a593Smuzhiyun 	u32 imr;
54*4882a593Smuzhiyun 	u32 gdir;
55*4882a593Smuzhiyun 	u32 edge_sel;
56*4882a593Smuzhiyun 	u32 dr;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun struct mxc_gpio_port {
60*4882a593Smuzhiyun 	struct list_head node;
61*4882a593Smuzhiyun 	void __iomem *base;
62*4882a593Smuzhiyun 	struct clk *clk;
63*4882a593Smuzhiyun 	int irq;
64*4882a593Smuzhiyun 	int irq_high;
65*4882a593Smuzhiyun 	struct irq_domain *domain;
66*4882a593Smuzhiyun 	struct gpio_chip gc;
67*4882a593Smuzhiyun 	struct device *dev;
68*4882a593Smuzhiyun 	u32 both_edges;
69*4882a593Smuzhiyun 	struct mxc_gpio_reg_saved gpio_saved_reg;
70*4882a593Smuzhiyun 	bool power_off;
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = {
74*4882a593Smuzhiyun 	.dr_reg		= 0x1c,
75*4882a593Smuzhiyun 	.gdir_reg	= 0x00,
76*4882a593Smuzhiyun 	.psr_reg	= 0x24,
77*4882a593Smuzhiyun 	.icr1_reg	= 0x28,
78*4882a593Smuzhiyun 	.icr2_reg	= 0x2c,
79*4882a593Smuzhiyun 	.imr_reg	= 0x30,
80*4882a593Smuzhiyun 	.isr_reg	= 0x34,
81*4882a593Smuzhiyun 	.edge_sel_reg	= -EINVAL,
82*4882a593Smuzhiyun 	.low_level	= 0x03,
83*4882a593Smuzhiyun 	.high_level	= 0x02,
84*4882a593Smuzhiyun 	.rise_edge	= 0x00,
85*4882a593Smuzhiyun 	.fall_edge	= 0x01,
86*4882a593Smuzhiyun };
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
89*4882a593Smuzhiyun 	.dr_reg		= 0x00,
90*4882a593Smuzhiyun 	.gdir_reg	= 0x04,
91*4882a593Smuzhiyun 	.psr_reg	= 0x08,
92*4882a593Smuzhiyun 	.icr1_reg	= 0x0c,
93*4882a593Smuzhiyun 	.icr2_reg	= 0x10,
94*4882a593Smuzhiyun 	.imr_reg	= 0x14,
95*4882a593Smuzhiyun 	.isr_reg	= 0x18,
96*4882a593Smuzhiyun 	.edge_sel_reg	= -EINVAL,
97*4882a593Smuzhiyun 	.low_level	= 0x00,
98*4882a593Smuzhiyun 	.high_level	= 0x01,
99*4882a593Smuzhiyun 	.rise_edge	= 0x02,
100*4882a593Smuzhiyun 	.fall_edge	= 0x03,
101*4882a593Smuzhiyun };
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun static struct mxc_gpio_hwdata imx35_gpio_hwdata = {
104*4882a593Smuzhiyun 	.dr_reg		= 0x00,
105*4882a593Smuzhiyun 	.gdir_reg	= 0x04,
106*4882a593Smuzhiyun 	.psr_reg	= 0x08,
107*4882a593Smuzhiyun 	.icr1_reg	= 0x0c,
108*4882a593Smuzhiyun 	.icr2_reg	= 0x10,
109*4882a593Smuzhiyun 	.imr_reg	= 0x14,
110*4882a593Smuzhiyun 	.isr_reg	= 0x18,
111*4882a593Smuzhiyun 	.edge_sel_reg	= 0x1c,
112*4882a593Smuzhiyun 	.low_level	= 0x00,
113*4882a593Smuzhiyun 	.high_level	= 0x01,
114*4882a593Smuzhiyun 	.rise_edge	= 0x02,
115*4882a593Smuzhiyun 	.fall_edge	= 0x03,
116*4882a593Smuzhiyun };
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun static enum mxc_gpio_hwtype mxc_gpio_hwtype;
119*4882a593Smuzhiyun static struct mxc_gpio_hwdata *mxc_gpio_hwdata;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun #define GPIO_DR			(mxc_gpio_hwdata->dr_reg)
122*4882a593Smuzhiyun #define GPIO_GDIR		(mxc_gpio_hwdata->gdir_reg)
123*4882a593Smuzhiyun #define GPIO_PSR		(mxc_gpio_hwdata->psr_reg)
124*4882a593Smuzhiyun #define GPIO_ICR1		(mxc_gpio_hwdata->icr1_reg)
125*4882a593Smuzhiyun #define GPIO_ICR2		(mxc_gpio_hwdata->icr2_reg)
126*4882a593Smuzhiyun #define GPIO_IMR		(mxc_gpio_hwdata->imr_reg)
127*4882a593Smuzhiyun #define GPIO_ISR		(mxc_gpio_hwdata->isr_reg)
128*4882a593Smuzhiyun #define GPIO_EDGE_SEL		(mxc_gpio_hwdata->edge_sel_reg)
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun #define GPIO_INT_LOW_LEV	(mxc_gpio_hwdata->low_level)
131*4882a593Smuzhiyun #define GPIO_INT_HIGH_LEV	(mxc_gpio_hwdata->high_level)
132*4882a593Smuzhiyun #define GPIO_INT_RISE_EDGE	(mxc_gpio_hwdata->rise_edge)
133*4882a593Smuzhiyun #define GPIO_INT_FALL_EDGE	(mxc_gpio_hwdata->fall_edge)
134*4882a593Smuzhiyun #define GPIO_INT_BOTH_EDGES	0x4
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun static const struct platform_device_id mxc_gpio_devtype[] = {
137*4882a593Smuzhiyun 	{
138*4882a593Smuzhiyun 		.name = "imx1-gpio",
139*4882a593Smuzhiyun 		.driver_data = IMX1_GPIO,
140*4882a593Smuzhiyun 	}, {
141*4882a593Smuzhiyun 		.name = "imx21-gpio",
142*4882a593Smuzhiyun 		.driver_data = IMX21_GPIO,
143*4882a593Smuzhiyun 	}, {
144*4882a593Smuzhiyun 		.name = "imx31-gpio",
145*4882a593Smuzhiyun 		.driver_data = IMX31_GPIO,
146*4882a593Smuzhiyun 	}, {
147*4882a593Smuzhiyun 		.name = "imx35-gpio",
148*4882a593Smuzhiyun 		.driver_data = IMX35_GPIO,
149*4882a593Smuzhiyun 	}, {
150*4882a593Smuzhiyun 		/* sentinel */
151*4882a593Smuzhiyun 	}
152*4882a593Smuzhiyun };
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun static const struct of_device_id mxc_gpio_dt_ids[] = {
155*4882a593Smuzhiyun 	{ .compatible = "fsl,imx1-gpio", .data = &mxc_gpio_devtype[IMX1_GPIO], },
156*4882a593Smuzhiyun 	{ .compatible = "fsl,imx21-gpio", .data = &mxc_gpio_devtype[IMX21_GPIO], },
157*4882a593Smuzhiyun 	{ .compatible = "fsl,imx31-gpio", .data = &mxc_gpio_devtype[IMX31_GPIO], },
158*4882a593Smuzhiyun 	{ .compatible = "fsl,imx35-gpio", .data = &mxc_gpio_devtype[IMX35_GPIO], },
159*4882a593Smuzhiyun 	{ .compatible = "fsl,imx7d-gpio", .data = &mxc_gpio_devtype[IMX35_GPIO], },
160*4882a593Smuzhiyun 	{ /* sentinel */ }
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, mxc_gpio_dt_ids);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /*
165*4882a593Smuzhiyun  * MX2 has one interrupt *for all* gpio ports. The list is used
166*4882a593Smuzhiyun  * to save the references to all ports, so that mx2_gpio_irq_handler
167*4882a593Smuzhiyun  * can walk through all interrupt status registers.
168*4882a593Smuzhiyun  */
169*4882a593Smuzhiyun static LIST_HEAD(mxc_gpio_ports);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /* Note: This driver assumes 32 GPIOs are handled in one register */
172*4882a593Smuzhiyun 
gpio_set_irq_type(struct irq_data * d,u32 type)173*4882a593Smuzhiyun static int gpio_set_irq_type(struct irq_data *d, u32 type)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
176*4882a593Smuzhiyun 	struct mxc_gpio_port *port = gc->private;
177*4882a593Smuzhiyun 	u32 bit, val;
178*4882a593Smuzhiyun 	u32 gpio_idx = d->hwirq;
179*4882a593Smuzhiyun 	int edge;
180*4882a593Smuzhiyun 	void __iomem *reg = port->base;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	port->both_edges &= ~(1 << gpio_idx);
183*4882a593Smuzhiyun 	switch (type) {
184*4882a593Smuzhiyun 	case IRQ_TYPE_EDGE_RISING:
185*4882a593Smuzhiyun 		edge = GPIO_INT_RISE_EDGE;
186*4882a593Smuzhiyun 		break;
187*4882a593Smuzhiyun 	case IRQ_TYPE_EDGE_FALLING:
188*4882a593Smuzhiyun 		edge = GPIO_INT_FALL_EDGE;
189*4882a593Smuzhiyun 		break;
190*4882a593Smuzhiyun 	case IRQ_TYPE_EDGE_BOTH:
191*4882a593Smuzhiyun 		if (GPIO_EDGE_SEL >= 0) {
192*4882a593Smuzhiyun 			edge = GPIO_INT_BOTH_EDGES;
193*4882a593Smuzhiyun 		} else {
194*4882a593Smuzhiyun 			val = port->gc.get(&port->gc, gpio_idx);
195*4882a593Smuzhiyun 			if (val) {
196*4882a593Smuzhiyun 				edge = GPIO_INT_LOW_LEV;
197*4882a593Smuzhiyun 				pr_debug("mxc: set GPIO %d to low trigger\n", gpio_idx);
198*4882a593Smuzhiyun 			} else {
199*4882a593Smuzhiyun 				edge = GPIO_INT_HIGH_LEV;
200*4882a593Smuzhiyun 				pr_debug("mxc: set GPIO %d to high trigger\n", gpio_idx);
201*4882a593Smuzhiyun 			}
202*4882a593Smuzhiyun 			port->both_edges |= 1 << gpio_idx;
203*4882a593Smuzhiyun 		}
204*4882a593Smuzhiyun 		break;
205*4882a593Smuzhiyun 	case IRQ_TYPE_LEVEL_LOW:
206*4882a593Smuzhiyun 		edge = GPIO_INT_LOW_LEV;
207*4882a593Smuzhiyun 		break;
208*4882a593Smuzhiyun 	case IRQ_TYPE_LEVEL_HIGH:
209*4882a593Smuzhiyun 		edge = GPIO_INT_HIGH_LEV;
210*4882a593Smuzhiyun 		break;
211*4882a593Smuzhiyun 	default:
212*4882a593Smuzhiyun 		return -EINVAL;
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	if (GPIO_EDGE_SEL >= 0) {
216*4882a593Smuzhiyun 		val = readl(port->base + GPIO_EDGE_SEL);
217*4882a593Smuzhiyun 		if (edge == GPIO_INT_BOTH_EDGES)
218*4882a593Smuzhiyun 			writel(val | (1 << gpio_idx),
219*4882a593Smuzhiyun 				port->base + GPIO_EDGE_SEL);
220*4882a593Smuzhiyun 		else
221*4882a593Smuzhiyun 			writel(val & ~(1 << gpio_idx),
222*4882a593Smuzhiyun 				port->base + GPIO_EDGE_SEL);
223*4882a593Smuzhiyun 	}
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	if (edge != GPIO_INT_BOTH_EDGES) {
226*4882a593Smuzhiyun 		reg += GPIO_ICR1 + ((gpio_idx & 0x10) >> 2); /* lower or upper register */
227*4882a593Smuzhiyun 		bit = gpio_idx & 0xf;
228*4882a593Smuzhiyun 		val = readl(reg) & ~(0x3 << (bit << 1));
229*4882a593Smuzhiyun 		writel(val | (edge << (bit << 1)), reg);
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	writel(1 << gpio_idx, port->base + GPIO_ISR);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	return 0;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
mxc_flip_edge(struct mxc_gpio_port * port,u32 gpio)237*4882a593Smuzhiyun static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	void __iomem *reg = port->base;
240*4882a593Smuzhiyun 	u32 bit, val;
241*4882a593Smuzhiyun 	int edge;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
244*4882a593Smuzhiyun 	bit = gpio & 0xf;
245*4882a593Smuzhiyun 	val = readl(reg);
246*4882a593Smuzhiyun 	edge = (val >> (bit << 1)) & 3;
247*4882a593Smuzhiyun 	val &= ~(0x3 << (bit << 1));
248*4882a593Smuzhiyun 	if (edge == GPIO_INT_HIGH_LEV) {
249*4882a593Smuzhiyun 		edge = GPIO_INT_LOW_LEV;
250*4882a593Smuzhiyun 		pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
251*4882a593Smuzhiyun 	} else if (edge == GPIO_INT_LOW_LEV) {
252*4882a593Smuzhiyun 		edge = GPIO_INT_HIGH_LEV;
253*4882a593Smuzhiyun 		pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
254*4882a593Smuzhiyun 	} else {
255*4882a593Smuzhiyun 		pr_err("mxc: invalid configuration for GPIO %d: %x\n",
256*4882a593Smuzhiyun 		       gpio, edge);
257*4882a593Smuzhiyun 		return;
258*4882a593Smuzhiyun 	}
259*4882a593Smuzhiyun 	writel(val | (edge << (bit << 1)), reg);
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /* handle 32 interrupts in one status register */
mxc_gpio_irq_handler(struct mxc_gpio_port * port,u32 irq_stat)263*4882a593Smuzhiyun static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun 	while (irq_stat != 0) {
266*4882a593Smuzhiyun 		int irqoffset = fls(irq_stat) - 1;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 		if (port->both_edges & (1 << irqoffset))
269*4882a593Smuzhiyun 			mxc_flip_edge(port, irqoffset);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 		generic_handle_irq(irq_find_mapping(port->domain, irqoffset));
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 		irq_stat &= ~(1 << irqoffset);
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun /* MX1 and MX3 has one interrupt *per* gpio port */
mx3_gpio_irq_handler(struct irq_desc * desc)278*4882a593Smuzhiyun static void mx3_gpio_irq_handler(struct irq_desc *desc)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	u32 irq_stat;
281*4882a593Smuzhiyun 	struct mxc_gpio_port *port = irq_desc_get_handler_data(desc);
282*4882a593Smuzhiyun 	struct irq_chip *chip = irq_desc_get_chip(desc);
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	chained_irq_enter(chip, desc);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	mxc_gpio_irq_handler(port, irq_stat);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	chained_irq_exit(chip, desc);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun /* MX2 has one interrupt *for all* gpio ports */
mx2_gpio_irq_handler(struct irq_desc * desc)294*4882a593Smuzhiyun static void mx2_gpio_irq_handler(struct irq_desc *desc)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	u32 irq_msk, irq_stat;
297*4882a593Smuzhiyun 	struct mxc_gpio_port *port;
298*4882a593Smuzhiyun 	struct irq_chip *chip = irq_desc_get_chip(desc);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	chained_irq_enter(chip, desc);
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	/* walk through all interrupt status registers */
303*4882a593Smuzhiyun 	list_for_each_entry(port, &mxc_gpio_ports, node) {
304*4882a593Smuzhiyun 		irq_msk = readl(port->base + GPIO_IMR);
305*4882a593Smuzhiyun 		if (!irq_msk)
306*4882a593Smuzhiyun 			continue;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 		irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
309*4882a593Smuzhiyun 		if (irq_stat)
310*4882a593Smuzhiyun 			mxc_gpio_irq_handler(port, irq_stat);
311*4882a593Smuzhiyun 	}
312*4882a593Smuzhiyun 	chained_irq_exit(chip, desc);
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun /*
316*4882a593Smuzhiyun  * Set interrupt number "irq" in the GPIO as a wake-up source.
317*4882a593Smuzhiyun  * While system is running, all registered GPIO interrupts need to have
318*4882a593Smuzhiyun  * wake-up enabled. When system is suspended, only selected GPIO interrupts
319*4882a593Smuzhiyun  * need to have wake-up enabled.
320*4882a593Smuzhiyun  * @param  irq          interrupt source number
321*4882a593Smuzhiyun  * @param  enable       enable as wake-up if equal to non-zero
322*4882a593Smuzhiyun  * @return       This function returns 0 on success.
323*4882a593Smuzhiyun  */
gpio_set_wake_irq(struct irq_data * d,u32 enable)324*4882a593Smuzhiyun static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
327*4882a593Smuzhiyun 	struct mxc_gpio_port *port = gc->private;
328*4882a593Smuzhiyun 	u32 gpio_idx = d->hwirq;
329*4882a593Smuzhiyun 	int ret;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	if (enable) {
332*4882a593Smuzhiyun 		if (port->irq_high && (gpio_idx >= 16))
333*4882a593Smuzhiyun 			ret = enable_irq_wake(port->irq_high);
334*4882a593Smuzhiyun 		else
335*4882a593Smuzhiyun 			ret = enable_irq_wake(port->irq);
336*4882a593Smuzhiyun 	} else {
337*4882a593Smuzhiyun 		if (port->irq_high && (gpio_idx >= 16))
338*4882a593Smuzhiyun 			ret = disable_irq_wake(port->irq_high);
339*4882a593Smuzhiyun 		else
340*4882a593Smuzhiyun 			ret = disable_irq_wake(port->irq);
341*4882a593Smuzhiyun 	}
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	return ret;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
mxc_gpio_init_gc(struct mxc_gpio_port * port,int irq_base)346*4882a593Smuzhiyun static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	struct irq_chip_generic *gc;
349*4882a593Smuzhiyun 	struct irq_chip_type *ct;
350*4882a593Smuzhiyun 	int rv;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	gc = devm_irq_alloc_generic_chip(port->dev, "gpio-mxc", 1, irq_base,
353*4882a593Smuzhiyun 					 port->base, handle_level_irq);
354*4882a593Smuzhiyun 	if (!gc)
355*4882a593Smuzhiyun 		return -ENOMEM;
356*4882a593Smuzhiyun 	gc->private = port;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	ct = gc->chip_types;
359*4882a593Smuzhiyun 	ct->chip.irq_ack = irq_gc_ack_set_bit;
360*4882a593Smuzhiyun 	ct->chip.irq_mask = irq_gc_mask_clr_bit;
361*4882a593Smuzhiyun 	ct->chip.irq_unmask = irq_gc_mask_set_bit;
362*4882a593Smuzhiyun 	ct->chip.irq_set_type = gpio_set_irq_type;
363*4882a593Smuzhiyun 	ct->chip.irq_set_wake = gpio_set_wake_irq;
364*4882a593Smuzhiyun 	ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND;
365*4882a593Smuzhiyun 	ct->regs.ack = GPIO_ISR;
366*4882a593Smuzhiyun 	ct->regs.mask = GPIO_IMR;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	rv = devm_irq_setup_generic_chip(port->dev, gc, IRQ_MSK(32),
369*4882a593Smuzhiyun 					 IRQ_GC_INIT_NESTED_LOCK,
370*4882a593Smuzhiyun 					 IRQ_NOREQUEST, 0);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	return rv;
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun 
mxc_gpio_get_hw(struct platform_device * pdev)375*4882a593Smuzhiyun static void mxc_gpio_get_hw(struct platform_device *pdev)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun 	const struct of_device_id *of_id =
378*4882a593Smuzhiyun 			of_match_device(mxc_gpio_dt_ids, &pdev->dev);
379*4882a593Smuzhiyun 	enum mxc_gpio_hwtype hwtype;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	if (of_id)
382*4882a593Smuzhiyun 		pdev->id_entry = of_id->data;
383*4882a593Smuzhiyun 	hwtype = pdev->id_entry->driver_data;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	if (mxc_gpio_hwtype) {
386*4882a593Smuzhiyun 		/*
387*4882a593Smuzhiyun 		 * The driver works with a reasonable presupposition,
388*4882a593Smuzhiyun 		 * that is all gpio ports must be the same type when
389*4882a593Smuzhiyun 		 * running on one soc.
390*4882a593Smuzhiyun 		 */
391*4882a593Smuzhiyun 		BUG_ON(mxc_gpio_hwtype != hwtype);
392*4882a593Smuzhiyun 		return;
393*4882a593Smuzhiyun 	}
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	if (hwtype == IMX35_GPIO)
396*4882a593Smuzhiyun 		mxc_gpio_hwdata = &imx35_gpio_hwdata;
397*4882a593Smuzhiyun 	else if (hwtype == IMX31_GPIO)
398*4882a593Smuzhiyun 		mxc_gpio_hwdata = &imx31_gpio_hwdata;
399*4882a593Smuzhiyun 	else
400*4882a593Smuzhiyun 		mxc_gpio_hwdata = &imx1_imx21_gpio_hwdata;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	mxc_gpio_hwtype = hwtype;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
mxc_gpio_to_irq(struct gpio_chip * gc,unsigned offset)405*4882a593Smuzhiyun static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	struct mxc_gpio_port *port = gpiochip_get_data(gc);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	return irq_find_mapping(port->domain, offset);
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
mxc_gpio_probe(struct platform_device * pdev)412*4882a593Smuzhiyun static int mxc_gpio_probe(struct platform_device *pdev)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun 	struct device_node *np = pdev->dev.of_node;
415*4882a593Smuzhiyun 	struct mxc_gpio_port *port;
416*4882a593Smuzhiyun 	int irq_count;
417*4882a593Smuzhiyun 	int irq_base;
418*4882a593Smuzhiyun 	int err;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	mxc_gpio_get_hw(pdev);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
423*4882a593Smuzhiyun 	if (!port)
424*4882a593Smuzhiyun 		return -ENOMEM;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	port->dev = &pdev->dev;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	port->base = devm_platform_ioremap_resource(pdev, 0);
429*4882a593Smuzhiyun 	if (IS_ERR(port->base))
430*4882a593Smuzhiyun 		return PTR_ERR(port->base);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	irq_count = platform_irq_count(pdev);
433*4882a593Smuzhiyun 	if (irq_count < 0)
434*4882a593Smuzhiyun 		return irq_count;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	if (irq_count > 1) {
437*4882a593Smuzhiyun 		port->irq_high = platform_get_irq(pdev, 1);
438*4882a593Smuzhiyun 		if (port->irq_high < 0)
439*4882a593Smuzhiyun 			port->irq_high = 0;
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	port->irq = platform_get_irq(pdev, 0);
443*4882a593Smuzhiyun 	if (port->irq < 0)
444*4882a593Smuzhiyun 		return port->irq;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	/* the controller clock is optional */
447*4882a593Smuzhiyun 	port->clk = devm_clk_get_optional(&pdev->dev, NULL);
448*4882a593Smuzhiyun 	if (IS_ERR(port->clk))
449*4882a593Smuzhiyun 		return PTR_ERR(port->clk);
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	err = clk_prepare_enable(port->clk);
452*4882a593Smuzhiyun 	if (err) {
453*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Unable to enable clock.\n");
454*4882a593Smuzhiyun 		return err;
455*4882a593Smuzhiyun 	}
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	if (of_device_is_compatible(np, "fsl,imx7d-gpio"))
458*4882a593Smuzhiyun 		port->power_off = true;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	/* disable the interrupt and clear the status */
461*4882a593Smuzhiyun 	writel(0, port->base + GPIO_IMR);
462*4882a593Smuzhiyun 	writel(~0, port->base + GPIO_ISR);
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	if (mxc_gpio_hwtype == IMX21_GPIO) {
465*4882a593Smuzhiyun 		/*
466*4882a593Smuzhiyun 		 * Setup one handler for all GPIO interrupts. Actually setting
467*4882a593Smuzhiyun 		 * the handler is needed only once, but doing it for every port
468*4882a593Smuzhiyun 		 * is more robust and easier.
469*4882a593Smuzhiyun 		 */
470*4882a593Smuzhiyun 		irq_set_chained_handler(port->irq, mx2_gpio_irq_handler);
471*4882a593Smuzhiyun 	} else {
472*4882a593Smuzhiyun 		/* setup one handler for each entry */
473*4882a593Smuzhiyun 		irq_set_chained_handler_and_data(port->irq,
474*4882a593Smuzhiyun 						 mx3_gpio_irq_handler, port);
475*4882a593Smuzhiyun 		if (port->irq_high > 0)
476*4882a593Smuzhiyun 			/* setup handler for GPIO 16 to 31 */
477*4882a593Smuzhiyun 			irq_set_chained_handler_and_data(port->irq_high,
478*4882a593Smuzhiyun 							 mx3_gpio_irq_handler,
479*4882a593Smuzhiyun 							 port);
480*4882a593Smuzhiyun 	}
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	err = bgpio_init(&port->gc, &pdev->dev, 4,
483*4882a593Smuzhiyun 			 port->base + GPIO_PSR,
484*4882a593Smuzhiyun 			 port->base + GPIO_DR, NULL,
485*4882a593Smuzhiyun 			 port->base + GPIO_GDIR, NULL,
486*4882a593Smuzhiyun 			 BGPIOF_READ_OUTPUT_REG_SET);
487*4882a593Smuzhiyun 	if (err)
488*4882a593Smuzhiyun 		goto out_bgio;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	port->gc.request = gpiochip_generic_request;
491*4882a593Smuzhiyun 	port->gc.free = gpiochip_generic_free;
492*4882a593Smuzhiyun 	port->gc.to_irq = mxc_gpio_to_irq;
493*4882a593Smuzhiyun 	port->gc.base = (pdev->id < 0) ? of_alias_get_id(np, "gpio") * 32 :
494*4882a593Smuzhiyun 					     pdev->id * 32;
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	err = devm_gpiochip_add_data(&pdev->dev, &port->gc, port);
497*4882a593Smuzhiyun 	if (err)
498*4882a593Smuzhiyun 		goto out_bgio;
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, 32, numa_node_id());
501*4882a593Smuzhiyun 	if (irq_base < 0) {
502*4882a593Smuzhiyun 		err = irq_base;
503*4882a593Smuzhiyun 		goto out_bgio;
504*4882a593Smuzhiyun 	}
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	port->domain = irq_domain_add_legacy(np, 32, irq_base, 0,
507*4882a593Smuzhiyun 					     &irq_domain_simple_ops, NULL);
508*4882a593Smuzhiyun 	if (!port->domain) {
509*4882a593Smuzhiyun 		err = -ENODEV;
510*4882a593Smuzhiyun 		goto out_bgio;
511*4882a593Smuzhiyun 	}
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	/* gpio-mxc can be a generic irq chip */
514*4882a593Smuzhiyun 	err = mxc_gpio_init_gc(port, irq_base);
515*4882a593Smuzhiyun 	if (err < 0)
516*4882a593Smuzhiyun 		goto out_irqdomain_remove;
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	list_add_tail(&port->node, &mxc_gpio_ports);
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	platform_set_drvdata(pdev, port);
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	return 0;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun out_irqdomain_remove:
525*4882a593Smuzhiyun 	irq_domain_remove(port->domain);
526*4882a593Smuzhiyun out_bgio:
527*4882a593Smuzhiyun 	clk_disable_unprepare(port->clk);
528*4882a593Smuzhiyun 	dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
529*4882a593Smuzhiyun 	return err;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun 
mxc_gpio_save_regs(struct mxc_gpio_port * port)532*4882a593Smuzhiyun static void mxc_gpio_save_regs(struct mxc_gpio_port *port)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun 	if (!port->power_off)
535*4882a593Smuzhiyun 		return;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	port->gpio_saved_reg.icr1 = readl(port->base + GPIO_ICR1);
538*4882a593Smuzhiyun 	port->gpio_saved_reg.icr2 = readl(port->base + GPIO_ICR2);
539*4882a593Smuzhiyun 	port->gpio_saved_reg.imr = readl(port->base + GPIO_IMR);
540*4882a593Smuzhiyun 	port->gpio_saved_reg.gdir = readl(port->base + GPIO_GDIR);
541*4882a593Smuzhiyun 	port->gpio_saved_reg.edge_sel = readl(port->base + GPIO_EDGE_SEL);
542*4882a593Smuzhiyun 	port->gpio_saved_reg.dr = readl(port->base + GPIO_DR);
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun 
mxc_gpio_restore_regs(struct mxc_gpio_port * port)545*4882a593Smuzhiyun static void mxc_gpio_restore_regs(struct mxc_gpio_port *port)
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun 	if (!port->power_off)
548*4882a593Smuzhiyun 		return;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	writel(port->gpio_saved_reg.icr1, port->base + GPIO_ICR1);
551*4882a593Smuzhiyun 	writel(port->gpio_saved_reg.icr2, port->base + GPIO_ICR2);
552*4882a593Smuzhiyun 	writel(port->gpio_saved_reg.imr, port->base + GPIO_IMR);
553*4882a593Smuzhiyun 	writel(port->gpio_saved_reg.gdir, port->base + GPIO_GDIR);
554*4882a593Smuzhiyun 	writel(port->gpio_saved_reg.edge_sel, port->base + GPIO_EDGE_SEL);
555*4882a593Smuzhiyun 	writel(port->gpio_saved_reg.dr, port->base + GPIO_DR);
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun 
mxc_gpio_syscore_suspend(void)558*4882a593Smuzhiyun static int mxc_gpio_syscore_suspend(void)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun 	struct mxc_gpio_port *port;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	/* walk through all ports */
563*4882a593Smuzhiyun 	list_for_each_entry(port, &mxc_gpio_ports, node) {
564*4882a593Smuzhiyun 		mxc_gpio_save_regs(port);
565*4882a593Smuzhiyun 		clk_disable_unprepare(port->clk);
566*4882a593Smuzhiyun 	}
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	return 0;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun 
mxc_gpio_syscore_resume(void)571*4882a593Smuzhiyun static void mxc_gpio_syscore_resume(void)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun 	struct mxc_gpio_port *port;
574*4882a593Smuzhiyun 	int ret;
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	/* walk through all ports */
577*4882a593Smuzhiyun 	list_for_each_entry(port, &mxc_gpio_ports, node) {
578*4882a593Smuzhiyun 		ret = clk_prepare_enable(port->clk);
579*4882a593Smuzhiyun 		if (ret) {
580*4882a593Smuzhiyun 			pr_err("mxc: failed to enable gpio clock %d\n", ret);
581*4882a593Smuzhiyun 			return;
582*4882a593Smuzhiyun 		}
583*4882a593Smuzhiyun 		mxc_gpio_restore_regs(port);
584*4882a593Smuzhiyun 	}
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun static struct syscore_ops mxc_gpio_syscore_ops = {
588*4882a593Smuzhiyun 	.suspend = mxc_gpio_syscore_suspend,
589*4882a593Smuzhiyun 	.resume = mxc_gpio_syscore_resume,
590*4882a593Smuzhiyun };
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun static struct platform_driver mxc_gpio_driver = {
593*4882a593Smuzhiyun 	.driver		= {
594*4882a593Smuzhiyun 		.name	= "gpio-mxc",
595*4882a593Smuzhiyun 		.of_match_table = mxc_gpio_dt_ids,
596*4882a593Smuzhiyun 		.suppress_bind_attrs = true,
597*4882a593Smuzhiyun 	},
598*4882a593Smuzhiyun 	.probe		= mxc_gpio_probe,
599*4882a593Smuzhiyun 	.id_table	= mxc_gpio_devtype,
600*4882a593Smuzhiyun };
601*4882a593Smuzhiyun 
gpio_mxc_init(void)602*4882a593Smuzhiyun static int __init gpio_mxc_init(void)
603*4882a593Smuzhiyun {
604*4882a593Smuzhiyun 	register_syscore_ops(&mxc_gpio_syscore_ops);
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	return platform_driver_register(&mxc_gpio_driver);
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun subsys_initcall(gpio_mxc_init);
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
611*4882a593Smuzhiyun MODULE_DESCRIPTION("i.MX GPIO Driver");
612*4882a593Smuzhiyun MODULE_LICENSE("GPL");
613