blob: c871602fc5ba9bbdf309aa0f9c6bef98c1a2f08c [file] [log] [blame]
Fabio Estevam014e4202018-05-21 23:32:54 -03001// SPDX-License-Identifier: GPL-2.0+
2//
3// MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
4// Copyright 2008 Juergen Beisert, kernel@pengutronix.de
5//
6// Based on code from Freescale Semiconductor,
7// Authors: Daniel Mack, Juergen Beisert.
8// Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
Juergen Beisert07bd1a62008-07-05 10:02:49 +02009
Anson Huang28088012018-05-22 11:05:40 +080010#include <linux/clk.h>
Fabio Estevam18f92b12013-07-22 18:17:52 -030011#include <linux/err.h>
Juergen Beisert07bd1a62008-07-05 10:02:49 +020012#include <linux/init.h>
Dinh Nguyena3484ff2010-10-23 09:12:48 -050013#include <linux/interrupt.h>
Juergen Beisert07bd1a62008-07-05 10:02:49 +020014#include <linux/io.h>
15#include <linux/irq.h>
Shawn Guo1ab7ef12012-06-13 09:04:03 +080016#include <linux/irqdomain.h>
Catalin Marinasde88cbb2013-01-18 15:31:37 +000017#include <linux/irqchip/chained_irq.h>
Anson Huang12d16b32020-09-17 13:33:46 +080018#include <linux/module.h>
Shawn Guob78d8e52011-06-06 00:07:55 +080019#include <linux/platform_device.h>
20#include <linux/slab.h>
Anson Huang1a5287a2018-11-09 04:56:56 +000021#include <linux/syscore_ops.h>
Linus Walleij0f4630f2015-12-04 14:02:58 +010022#include <linux/gpio/driver.h>
Shawn Guo8937cb62011-07-07 00:37:43 +080023#include <linux/of.h>
24#include <linux/of_device.h>
Christoph Hellwig16c3bd32015-08-28 09:27:22 +020025#include <linux/bug.h>
Juergen Beisert07bd1a62008-07-05 10:02:49 +020026
Shawn Guoe7fc6ae2011-07-07 00:37:41 +080027/* device type dependent stuff */
28struct mxc_gpio_hwdata {
29 unsigned dr_reg;
30 unsigned gdir_reg;
31 unsigned psr_reg;
32 unsigned icr1_reg;
33 unsigned icr2_reg;
34 unsigned imr_reg;
35 unsigned isr_reg;
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +020036 int edge_sel_reg;
Shawn Guoe7fc6ae2011-07-07 00:37:41 +080037 unsigned low_level;
38 unsigned high_level;
39 unsigned rise_edge;
40 unsigned fall_edge;
41};
42
Anson Huangc19fdae2018-07-18 09:25:32 +080043struct mxc_gpio_reg_saved {
44 u32 icr1;
45 u32 icr2;
46 u32 imr;
47 u32 gdir;
48 u32 edge_sel;
49 u32 dr;
50};
51
Shawn Guob78d8e52011-06-06 00:07:55 +080052struct mxc_gpio_port {
53 struct list_head node;
54 void __iomem *base;
Anson Huang28088012018-05-22 11:05:40 +080055 struct clk *clk;
Shawn Guob78d8e52011-06-06 00:07:55 +080056 int irq;
57 int irq_high;
Shawn Guo1ab7ef12012-06-13 09:04:03 +080058 struct irq_domain *domain;
Linus Walleij0f4630f2015-12-04 14:02:58 +010059 struct gpio_chip gc;
Bartosz Golaszewskidb5270a2017-08-09 14:25:06 +020060 struct device *dev;
Shawn Guob78d8e52011-06-06 00:07:55 +080061 u32 both_edges;
Anson Huangc19fdae2018-07-18 09:25:32 +080062 struct mxc_gpio_reg_saved gpio_saved_reg;
63 bool power_off;
Fabio Estevam0f2c7af2020-11-17 07:59:17 -030064 const struct mxc_gpio_hwdata *hwdata;
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,
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +020075 .edge_sel_reg = -EINVAL,
Shawn Guoe7fc6ae2011-07-07 00:37:41 +080076 .low_level = 0x03,
77 .high_level = 0x02,
78 .rise_edge = 0x00,
79 .fall_edge = 0x01,
80};
81
82static struct mxc_gpio_hwdata imx31_gpio_hwdata = {
83 .dr_reg = 0x00,
84 .gdir_reg = 0x04,
85 .psr_reg = 0x08,
86 .icr1_reg = 0x0c,
87 .icr2_reg = 0x10,
88 .imr_reg = 0x14,
89 .isr_reg = 0x18,
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +020090 .edge_sel_reg = -EINVAL,
91 .low_level = 0x00,
92 .high_level = 0x01,
93 .rise_edge = 0x02,
94 .fall_edge = 0x03,
95};
96
97static struct mxc_gpio_hwdata imx35_gpio_hwdata = {
98 .dr_reg = 0x00,
99 .gdir_reg = 0x04,
100 .psr_reg = 0x08,
101 .icr1_reg = 0x0c,
102 .icr2_reg = 0x10,
103 .imr_reg = 0x14,
104 .isr_reg = 0x18,
105 .edge_sel_reg = 0x1c,
Shawn Guoe7fc6ae2011-07-07 00:37:41 +0800106 .low_level = 0x00,
107 .high_level = 0x01,
108 .rise_edge = 0x02,
109 .fall_edge = 0x03,
110};
111
Fabio Estevam0f2c7af2020-11-17 07:59:17 -0300112#define GPIO_DR (port->hwdata->dr_reg)
113#define GPIO_GDIR (port->hwdata->gdir_reg)
114#define GPIO_PSR (port->hwdata->psr_reg)
115#define GPIO_ICR1 (port->hwdata->icr1_reg)
116#define GPIO_ICR2 (port->hwdata->icr2_reg)
117#define GPIO_IMR (port->hwdata->imr_reg)
118#define GPIO_ISR (port->hwdata->isr_reg)
119#define GPIO_EDGE_SEL (port->hwdata->edge_sel_reg)
Shawn Guoe7fc6ae2011-07-07 00:37:41 +0800120
Fabio Estevam0f2c7af2020-11-17 07:59:17 -0300121#define GPIO_INT_LOW_LEV (port->hwdata->low_level)
122#define GPIO_INT_HIGH_LEV (port->hwdata->high_level)
123#define GPIO_INT_RISE_EDGE (port->hwdata->rise_edge)
124#define GPIO_INT_FALL_EDGE (port->hwdata->fall_edge)
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +0200125#define GPIO_INT_BOTH_EDGES 0x4
Shawn Guoe7fc6ae2011-07-07 00:37:41 +0800126
Shawn Guo8937cb62011-07-07 00:37:43 +0800127static const struct of_device_id mxc_gpio_dt_ids[] = {
Fabio Estevam0f2c7af2020-11-17 07:59:17 -0300128 { .compatible = "fsl,imx1-gpio", .data = &imx1_imx21_gpio_hwdata },
129 { .compatible = "fsl,imx21-gpio", .data = &imx1_imx21_gpio_hwdata },
130 { .compatible = "fsl,imx31-gpio", .data = &imx31_gpio_hwdata },
131 { .compatible = "fsl,imx35-gpio", .data = &imx35_gpio_hwdata },
132 { .compatible = "fsl,imx7d-gpio", .data = &imx35_gpio_hwdata },
Shawn Guo8937cb62011-07-07 00:37:43 +0800133 { /* sentinel */ }
134};
Anson Huang12d16b32020-09-17 13:33:46 +0800135MODULE_DEVICE_TABLE(of, mxc_gpio_dt_ids);
Shawn Guo8937cb62011-07-07 00:37:43 +0800136
Shawn Guob78d8e52011-06-06 00:07:55 +0800137/*
138 * MX2 has one interrupt *for all* gpio ports. The list is used
139 * to save the references to all ports, so that mx2_gpio_irq_handler
140 * can walk through all interrupt status registers.
141 */
142static LIST_HEAD(mxc_gpio_ports);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200143
144/* Note: This driver assumes 32 GPIOs are handled in one register */
145
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100146static int gpio_set_irq_type(struct irq_data *d, u32 type)
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200147{
Shawn Guoe4ea9332011-06-07 16:25:37 +0800148 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
149 struct mxc_gpio_port *port = gc->private;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200150 u32 bit, val;
Shawn Guo1ab7ef12012-06-13 09:04:03 +0800151 u32 gpio_idx = d->hwirq;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200152 int edge;
153 void __iomem *reg = port->base;
154
Shawn Guo1ab7ef12012-06-13 09:04:03 +0800155 port->both_edges &= ~(1 << gpio_idx);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200156 switch (type) {
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100157 case IRQ_TYPE_EDGE_RISING:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200158 edge = GPIO_INT_RISE_EDGE;
159 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100160 case IRQ_TYPE_EDGE_FALLING:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200161 edge = GPIO_INT_FALL_EDGE;
162 break;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100163 case IRQ_TYPE_EDGE_BOTH:
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +0200164 if (GPIO_EDGE_SEL >= 0) {
165 edge = GPIO_INT_BOTH_EDGES;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100166 } else {
Linus Walleij8d0bd9a2018-04-15 22:25:00 +0200167 val = port->gc.get(&port->gc, gpio_idx);
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +0200168 if (val) {
169 edge = GPIO_INT_LOW_LEV;
Linus Walleij8d0bd9a2018-04-15 22:25:00 +0200170 pr_debug("mxc: set GPIO %d to low trigger\n", gpio_idx);
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +0200171 } else {
172 edge = GPIO_INT_HIGH_LEV;
Linus Walleij8d0bd9a2018-04-15 22:25:00 +0200173 pr_debug("mxc: set GPIO %d to high trigger\n", gpio_idx);
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +0200174 }
Linus Torvaldsf948ad02012-07-26 13:56:38 -0700175 port->both_edges |= 1 << gpio_idx;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100176 }
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100177 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100178 case IRQ_TYPE_LEVEL_LOW:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200179 edge = GPIO_INT_LOW_LEV;
180 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100181 case IRQ_TYPE_LEVEL_HIGH:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200182 edge = GPIO_INT_HIGH_LEV;
183 break;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100184 default:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200185 return -EINVAL;
186 }
187
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +0200188 if (GPIO_EDGE_SEL >= 0) {
189 val = readl(port->base + GPIO_EDGE_SEL);
190 if (edge == GPIO_INT_BOTH_EDGES)
Linus Torvaldsf948ad02012-07-26 13:56:38 -0700191 writel(val | (1 << gpio_idx),
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +0200192 port->base + GPIO_EDGE_SEL);
193 else
Linus Torvaldsf948ad02012-07-26 13:56:38 -0700194 writel(val & ~(1 << gpio_idx),
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +0200195 port->base + GPIO_EDGE_SEL);
196 }
197
198 if (edge != GPIO_INT_BOTH_EDGES) {
Linus Torvaldsf948ad02012-07-26 13:56:38 -0700199 reg += GPIO_ICR1 + ((gpio_idx & 0x10) >> 2); /* lower or upper register */
200 bit = gpio_idx & 0xf;
Benoît Thébaudeauaeb27742012-06-22 21:04:06 +0200201 val = readl(reg) & ~(0x3 << (bit << 1));
202 writel(val | (edge << (bit << 1)), reg);
203 }
204
Shawn Guo1ab7ef12012-06-13 09:04:03 +0800205 writel(1 << gpio_idx, port->base + GPIO_ISR);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200206
207 return 0;
208}
209
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100210static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
211{
212 void __iomem *reg = port->base;
213 u32 bit, val;
214 int edge;
215
216 reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
217 bit = gpio & 0xf;
Shawn Guob78d8e52011-06-06 00:07:55 +0800218 val = readl(reg);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100219 edge = (val >> (bit << 1)) & 3;
220 val &= ~(0x3 << (bit << 1));
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100221 if (edge == GPIO_INT_HIGH_LEV) {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100222 edge = GPIO_INT_LOW_LEV;
223 pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100224 } else if (edge == GPIO_INT_LOW_LEV) {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100225 edge = GPIO_INT_HIGH_LEV;
226 pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100227 } else {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100228 pr_err("mxc: invalid configuration for GPIO %d: %x\n",
229 gpio, edge);
230 return;
231 }
Shawn Guob78d8e52011-06-06 00:07:55 +0800232 writel(val | (edge << (bit << 1)), reg);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100233}
234
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100235/* handle 32 interrupts in one status register */
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200236static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
237{
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100238 while (irq_stat != 0) {
239 int irqoffset = fls(irq_stat) - 1;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200240
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100241 if (port->both_edges & (1 << irqoffset))
242 mxc_flip_edge(port, irqoffset);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100243
Marc Zyngierdbd1c542021-05-04 17:42:18 +0100244 generic_handle_domain_irq(port->domain, irqoffset);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100245
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100246 irq_stat &= ~(1 << irqoffset);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200247 }
248}
249
Paulius Zaleckascfca8b52008-11-14 11:01:38 +0100250/* MX1 and MX3 has one interrupt *per* gpio port */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200251static void mx3_gpio_irq_handler(struct irq_desc *desc)
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200252{
253 u32 irq_stat;
Jiang Liu476f8b42015-06-04 12:13:15 +0800254 struct mxc_gpio_port *port = irq_desc_get_handler_data(desc);
255 struct irq_chip *chip = irq_desc_get_chip(desc);
Shawn Guo0e44b6e2011-09-21 21:24:04 +0800256
257 chained_irq_enter(chip, desc);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200258
Shawn Guob78d8e52011-06-06 00:07:55 +0800259 irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR);
Sascha Hauere2c97e72009-04-21 12:39:59 +0200260
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200261 mxc_gpio_irq_handler(port, irq_stat);
Shawn Guo0e44b6e2011-09-21 21:24:04 +0800262
263 chained_irq_exit(chip, desc);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200264}
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200265
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200266/* MX2 has one interrupt *for all* gpio ports */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200267static void mx2_gpio_irq_handler(struct irq_desc *desc)
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200268{
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200269 u32 irq_msk, irq_stat;
Shawn Guob78d8e52011-06-06 00:07:55 +0800270 struct mxc_gpio_port *port;
Jiang Liu476f8b42015-06-04 12:13:15 +0800271 struct irq_chip *chip = irq_desc_get_chip(desc);
Uwe Kleine-Königc0e811d2013-07-18 14:58:06 +0200272
273 chained_irq_enter(chip, desc);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200274
275 /* walk through all interrupt status registers */
Shawn Guob78d8e52011-06-06 00:07:55 +0800276 list_for_each_entry(port, &mxc_gpio_ports, node) {
277 irq_msk = readl(port->base + GPIO_IMR);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200278 if (!irq_msk)
279 continue;
280
Shawn Guob78d8e52011-06-06 00:07:55 +0800281 irq_stat = readl(port->base + GPIO_ISR) & irq_msk;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200282 if (irq_stat)
Shawn Guob78d8e52011-06-06 00:07:55 +0800283 mxc_gpio_irq_handler(port, irq_stat);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200284 }
Uwe Kleine-Königc0e811d2013-07-18 14:58:06 +0200285 chained_irq_exit(chip, desc);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200286}
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200287
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500288/*
289 * Set interrupt number "irq" in the GPIO as a wake-up source.
290 * While system is running, all registered GPIO interrupts need to have
291 * wake-up enabled. When system is suspended, only selected GPIO interrupts
292 * need to have wake-up enabled.
293 * @param irq interrupt source number
294 * @param enable enable as wake-up if equal to non-zero
295 * @return This function returns 0 on success.
296 */
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100297static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500298{
Shawn Guoe4ea9332011-06-07 16:25:37 +0800299 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
300 struct mxc_gpio_port *port = gc->private;
Shawn Guo1ab7ef12012-06-13 09:04:03 +0800301 u32 gpio_idx = d->hwirq;
Philipp Rosenberger77a4d752017-07-12 10:36:40 +0200302 int ret;
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500303
304 if (enable) {
305 if (port->irq_high && (gpio_idx >= 16))
Philipp Rosenberger77a4d752017-07-12 10:36:40 +0200306 ret = enable_irq_wake(port->irq_high);
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500307 else
Philipp Rosenberger77a4d752017-07-12 10:36:40 +0200308 ret = enable_irq_wake(port->irq);
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500309 } else {
310 if (port->irq_high && (gpio_idx >= 16))
Philipp Rosenberger77a4d752017-07-12 10:36:40 +0200311 ret = disable_irq_wake(port->irq_high);
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500312 else
Philipp Rosenberger77a4d752017-07-12 10:36:40 +0200313 ret = disable_irq_wake(port->irq);
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500314 }
315
Philipp Rosenberger77a4d752017-07-12 10:36:40 +0200316 return ret;
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500317}
318
Peng Fan9e26b0b2015-08-23 21:11:52 +0800319static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base)
Shawn Guoe4ea9332011-06-07 16:25:37 +0800320{
321 struct irq_chip_generic *gc;
322 struct irq_chip_type *ct;
Bartosz Golaszewskidb5270a2017-08-09 14:25:06 +0200323 int rv;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200324
Bartosz Golaszewskidb5270a2017-08-09 14:25:06 +0200325 gc = devm_irq_alloc_generic_chip(port->dev, "gpio-mxc", 1, irq_base,
326 port->base, handle_level_irq);
Peng Fan9e26b0b2015-08-23 21:11:52 +0800327 if (!gc)
328 return -ENOMEM;
Shawn Guoe4ea9332011-06-07 16:25:37 +0800329 gc->private = port;
330
331 ct = gc->chip_types;
Shawn Guo591567a2011-07-19 21:16:56 +0800332 ct->chip.irq_ack = irq_gc_ack_set_bit;
Shawn Guoe4ea9332011-06-07 16:25:37 +0800333 ct->chip.irq_mask = irq_gc_mask_clr_bit;
334 ct->chip.irq_unmask = irq_gc_mask_set_bit;
335 ct->chip.irq_set_type = gpio_set_irq_type;
Shawn Guo591567a2011-07-19 21:16:56 +0800336 ct->chip.irq_set_wake = gpio_set_wake_irq;
Loic Poulain3093e6c2021-06-17 15:54:13 +0200337 ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND;
Shawn Guoe4ea9332011-06-07 16:25:37 +0800338 ct->regs.ack = GPIO_ISR;
339 ct->regs.mask = GPIO_IMR;
340
Bartosz Golaszewskidb5270a2017-08-09 14:25:06 +0200341 rv = devm_irq_setup_generic_chip(port->dev, gc, IRQ_MSK(32),
342 IRQ_GC_INIT_NESTED_LOCK,
343 IRQ_NOREQUEST, 0);
Peng Fan9e26b0b2015-08-23 21:11:52 +0800344
Bartosz Golaszewskidb5270a2017-08-09 14:25:06 +0200345 return rv;
Shawn Guoe4ea9332011-06-07 16:25:37 +0800346}
Thomas Gleixnerb5eee2f2011-04-04 14:29:58 +0200347
Shawn Guo09ad8032011-08-14 00:14:02 +0800348static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
349{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100350 struct mxc_gpio_port *port = gpiochip_get_data(gc);
Shawn Guo09ad8032011-08-14 00:14:02 +0800351
Shawn Guo1ab7ef12012-06-13 09:04:03 +0800352 return irq_find_mapping(port->domain, offset);
Shawn Guo09ad8032011-08-14 00:14:02 +0800353}
354
Bill Pemberton38363092012-11-19 13:22:34 -0500355static int mxc_gpio_probe(struct platform_device *pdev)
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200356{
Shawn Guo8937cb62011-07-07 00:37:43 +0800357 struct device_node *np = pdev->dev.of_node;
Shawn Guob78d8e52011-06-06 00:07:55 +0800358 struct mxc_gpio_port *port;
Anson Huangc8f3d142019-09-19 17:39:17 +0800359 int irq_count;
Shawn Guo1ab7ef12012-06-13 09:04:03 +0800360 int irq_base;
Shawn Guoe4ea9332011-06-07 16:25:37 +0800361 int err;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200362
Fabio Estevam8cd73e42013-07-08 17:14:39 -0300363 port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
Shawn Guob78d8e52011-06-06 00:07:55 +0800364 if (!port)
365 return -ENOMEM;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200366
Bartosz Golaszewskidb5270a2017-08-09 14:25:06 +0200367 port->dev = &pdev->dev;
368
Fabio Estevam0f2c7af2020-11-17 07:59:17 -0300369 port->hwdata = device_get_match_data(&pdev->dev);
370
Enrico Weigelt, metux IT consult123ac0e2019-03-11 19:55:01 +0100371 port->base = devm_platform_ioremap_resource(pdev, 0);
Fabio Estevam8cd73e42013-07-08 17:14:39 -0300372 if (IS_ERR(port->base))
373 return PTR_ERR(port->base);
Baruch Siach14cb0de2010-07-06 14:03:22 +0300374
Anson Huangc8f3d142019-09-19 17:39:17 +0800375 irq_count = platform_irq_count(pdev);
376 if (irq_count < 0)
377 return irq_count;
378
379 if (irq_count > 1) {
380 port->irq_high = platform_get_irq(pdev, 1);
381 if (port->irq_high < 0)
382 port->irq_high = 0;
383 }
Philipp Rosenbergercc9269f2017-07-12 10:36:39 +0200384
Shawn Guob78d8e52011-06-06 00:07:55 +0800385 port->irq = platform_get_irq(pdev, 0);
Fabio Estevam8cd73e42013-07-08 17:14:39 -0300386 if (port->irq < 0)
Sachin Kamat5ea80e42013-12-21 13:05:57 +0530387 return port->irq;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200388
Anson Huang28088012018-05-22 11:05:40 +0800389 /* the controller clock is optional */
Anson Huang7beb6202019-08-01 16:44:39 +0800390 port->clk = devm_clk_get_optional(&pdev->dev, NULL);
391 if (IS_ERR(port->clk))
392 return PTR_ERR(port->clk);
Anson Huang28088012018-05-22 11:05:40 +0800393
394 err = clk_prepare_enable(port->clk);
395 if (err) {
396 dev_err(&pdev->dev, "Unable to enable clock.\n");
397 return err;
398 }
399
Anson Huangc19fdae2018-07-18 09:25:32 +0800400 if (of_device_is_compatible(np, "fsl,imx7d-gpio"))
401 port->power_off = true;
402
Shawn Guob78d8e52011-06-06 00:07:55 +0800403 /* disable the interrupt and clear the status */
404 writel(0, port->base + GPIO_IMR);
405 writel(~0, port->base + GPIO_ISR);
406
Fabio Estevam0f2c7af2020-11-17 07:59:17 -0300407 if (of_device_is_compatible(np, "fsl,imx21-gpio")) {
Uwe Kleine-König33a4e982012-06-06 11:49:23 +0200408 /*
409 * Setup one handler for all GPIO interrupts. Actually setting
410 * the handler is needed only once, but doing it for every port
411 * is more robust and easier.
412 */
413 irq_set_chained_handler(port->irq, mx2_gpio_irq_handler);
Shawn Guob78d8e52011-06-06 00:07:55 +0800414 } else {
415 /* setup one handler for each entry */
Russell Kinge65eea52015-06-16 23:06:40 +0100416 irq_set_chained_handler_and_data(port->irq,
417 mx3_gpio_irq_handler, port);
418 if (port->irq_high > 0)
Shawn Guob78d8e52011-06-06 00:07:55 +0800419 /* setup handler for GPIO 16 to 31 */
Russell Kinge65eea52015-06-16 23:06:40 +0100420 irq_set_chained_handler_and_data(port->irq_high,
421 mx3_gpio_irq_handler,
422 port);
Sascha Hauer8afaada2009-06-15 12:36:25 +0200423 }
424
Linus Walleij0f4630f2015-12-04 14:02:58 +0100425 err = bgpio_init(&port->gc, &pdev->dev, 4,
Shawn Guo2ce420d2011-06-06 13:22:41 +0800426 port->base + GPIO_PSR,
427 port->base + GPIO_DR, NULL,
Vladimir Zapolskiy442b2492015-04-29 18:35:01 +0300428 port->base + GPIO_GDIR, NULL,
429 BGPIOF_READ_OUTPUT_REG_SET);
Shawn Guob78d8e52011-06-06 00:07:55 +0800430 if (err)
Fabio Estevam8cd73e42013-07-08 17:14:39 -0300431 goto out_bgio;
Shawn Guob78d8e52011-06-06 00:07:55 +0800432
Thierry Redingf0254b52020-04-01 22:05:26 +0200433 port->gc.request = gpiochip_generic_request;
434 port->gc.free = gpiochip_generic_free;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100435 port->gc.to_irq = mxc_gpio_to_irq;
436 port->gc.base = (pdev->id < 0) ? of_alias_get_id(np, "gpio") * 32 :
Shawn Guo7e6086d2012-08-05 14:01:26 +0800437 pdev->id * 32;
Shawn Guo2ce420d2011-06-06 13:22:41 +0800438
Laxman Dewanganffc56632016-02-22 17:43:28 +0530439 err = devm_gpiochip_add_data(&pdev->dev, &port->gc, port);
Shawn Guo2ce420d2011-06-06 13:22:41 +0800440 if (err)
Linus Walleij0f4630f2015-12-04 14:02:58 +0100441 goto out_bgio;
Shawn Guo2ce420d2011-06-06 13:22:41 +0800442
Bartosz Golaszewskic553c3c2017-03-04 17:23:38 +0100443 irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, 32, numa_node_id());
Shawn Guo1ab7ef12012-06-13 09:04:03 +0800444 if (irq_base < 0) {
445 err = irq_base;
Laxman Dewanganffc56632016-02-22 17:43:28 +0530446 goto out_bgio;
Shawn Guo1ab7ef12012-06-13 09:04:03 +0800447 }
448
449 port->domain = irq_domain_add_legacy(np, 32, irq_base, 0,
450 &irq_domain_simple_ops, NULL);
451 if (!port->domain) {
452 err = -ENODEV;
Bartosz Golaszewskic553c3c2017-03-04 17:23:38 +0100453 goto out_bgio;
Shawn Guo1ab7ef12012-06-13 09:04:03 +0800454 }
Shawn Guo8937cb62011-07-07 00:37:43 +0800455
456 /* gpio-mxc can be a generic irq chip */
Peng Fan9e26b0b2015-08-23 21:11:52 +0800457 err = mxc_gpio_init_gc(port, irq_base);
458 if (err < 0)
459 goto out_irqdomain_remove;
Shawn Guo8937cb62011-07-07 00:37:43 +0800460
Shawn Guob78d8e52011-06-06 00:07:55 +0800461 list_add_tail(&port->node, &mxc_gpio_ports);
462
Anson Huangc19fdae2018-07-18 09:25:32 +0800463 platform_set_drvdata(pdev, port);
464
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200465 return 0;
Shawn Guob78d8e52011-06-06 00:07:55 +0800466
Peng Fan9e26b0b2015-08-23 21:11:52 +0800467out_irqdomain_remove:
468 irq_domain_remove(port->domain);
Fabio Estevam8cd73e42013-07-08 17:14:39 -0300469out_bgio:
Anson Huang28088012018-05-22 11:05:40 +0800470 clk_disable_unprepare(port->clk);
Shawn Guob78d8e52011-06-06 00:07:55 +0800471 dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err);
472 return err;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200473}
Shawn Guob78d8e52011-06-06 00:07:55 +0800474
Anson Huangc19fdae2018-07-18 09:25:32 +0800475static void mxc_gpio_save_regs(struct mxc_gpio_port *port)
476{
477 if (!port->power_off)
478 return;
479
480 port->gpio_saved_reg.icr1 = readl(port->base + GPIO_ICR1);
481 port->gpio_saved_reg.icr2 = readl(port->base + GPIO_ICR2);
482 port->gpio_saved_reg.imr = readl(port->base + GPIO_IMR);
483 port->gpio_saved_reg.gdir = readl(port->base + GPIO_GDIR);
484 port->gpio_saved_reg.edge_sel = readl(port->base + GPIO_EDGE_SEL);
485 port->gpio_saved_reg.dr = readl(port->base + GPIO_DR);
486}
487
488static void mxc_gpio_restore_regs(struct mxc_gpio_port *port)
489{
490 if (!port->power_off)
491 return;
492
493 writel(port->gpio_saved_reg.icr1, port->base + GPIO_ICR1);
494 writel(port->gpio_saved_reg.icr2, port->base + GPIO_ICR2);
495 writel(port->gpio_saved_reg.imr, port->base + GPIO_IMR);
496 writel(port->gpio_saved_reg.gdir, port->base + GPIO_GDIR);
497 writel(port->gpio_saved_reg.edge_sel, port->base + GPIO_EDGE_SEL);
498 writel(port->gpio_saved_reg.dr, port->base + GPIO_DR);
499}
500
Anson Huang1a5287a2018-11-09 04:56:56 +0000501static int mxc_gpio_syscore_suspend(void)
Anson Huangc19fdae2018-07-18 09:25:32 +0800502{
Anson Huang1a5287a2018-11-09 04:56:56 +0000503 struct mxc_gpio_port *port;
Anson Huangc19fdae2018-07-18 09:25:32 +0800504
Anson Huang1a5287a2018-11-09 04:56:56 +0000505 /* walk through all ports */
506 list_for_each_entry(port, &mxc_gpio_ports, node) {
507 mxc_gpio_save_regs(port);
508 clk_disable_unprepare(port->clk);
509 }
Anson Huangc19fdae2018-07-18 09:25:32 +0800510
511 return 0;
512}
513
Anson Huang1a5287a2018-11-09 04:56:56 +0000514static void mxc_gpio_syscore_resume(void)
Anson Huangc19fdae2018-07-18 09:25:32 +0800515{
Anson Huang1a5287a2018-11-09 04:56:56 +0000516 struct mxc_gpio_port *port;
Anson Huangc19fdae2018-07-18 09:25:32 +0800517 int ret;
518
Anson Huang1a5287a2018-11-09 04:56:56 +0000519 /* walk through all ports */
520 list_for_each_entry(port, &mxc_gpio_ports, node) {
521 ret = clk_prepare_enable(port->clk);
522 if (ret) {
523 pr_err("mxc: failed to enable gpio clock %d\n", ret);
524 return;
525 }
526 mxc_gpio_restore_regs(port);
527 }
Anson Huangc19fdae2018-07-18 09:25:32 +0800528}
529
Anson Huang1a5287a2018-11-09 04:56:56 +0000530static struct syscore_ops mxc_gpio_syscore_ops = {
531 .suspend = mxc_gpio_syscore_suspend,
532 .resume = mxc_gpio_syscore_resume,
Anson Huangc19fdae2018-07-18 09:25:32 +0800533};
534
Shawn Guob78d8e52011-06-06 00:07:55 +0800535static struct platform_driver mxc_gpio_driver = {
536 .driver = {
537 .name = "gpio-mxc",
Shawn Guo8937cb62011-07-07 00:37:43 +0800538 .of_match_table = mxc_gpio_dt_ids,
Bartosz Golaszewski90e1fc42017-08-09 14:25:00 +0200539 .suppress_bind_attrs = true,
Shawn Guob78d8e52011-06-06 00:07:55 +0800540 },
541 .probe = mxc_gpio_probe,
542};
543
544static int __init gpio_mxc_init(void)
545{
Anson Huang1a5287a2018-11-09 04:56:56 +0000546 register_syscore_ops(&mxc_gpio_syscore_ops);
547
Shawn Guob78d8e52011-06-06 00:07:55 +0800548 return platform_driver_register(&mxc_gpio_driver);
549}
Vladimir Zapolskiye188cbf2016-09-08 04:48:15 +0300550subsys_initcall(gpio_mxc_init);
Anson Huang12d16b32020-09-17 13:33:46 +0800551
552MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
553MODULE_DESCRIPTION("i.MX GPIO Driver");
554MODULE_LICENSE("GPL");