Fabio Estevam | 014e420 | 2018-05-21 23:32:54 -0300 | [diff] [blame] | 1 | // 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 Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 9 | |
Anson Huang | 2808801 | 2018-05-22 11:05:40 +0800 | [diff] [blame] | 10 | #include <linux/clk.h> |
Fabio Estevam | 18f92b1 | 2013-07-22 18:17:52 -0300 | [diff] [blame] | 11 | #include <linux/err.h> |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 12 | #include <linux/init.h> |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 14 | #include <linux/io.h> |
| 15 | #include <linux/irq.h> |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 16 | #include <linux/irqdomain.h> |
Catalin Marinas | de88cbb | 2013-01-18 15:31:37 +0000 | [diff] [blame] | 17 | #include <linux/irqchip/chained_irq.h> |
Anson Huang | 12d16b3 | 2020-09-17 13:33:46 +0800 | [diff] [blame] | 18 | #include <linux/module.h> |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/slab.h> |
Anson Huang | 1a5287a | 2018-11-09 04:56:56 +0000 | [diff] [blame] | 21 | #include <linux/syscore_ops.h> |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 22 | #include <linux/gpio/driver.h> |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 23 | #include <linux/of.h> |
| 24 | #include <linux/of_device.h> |
Christoph Hellwig | 16c3bd3 | 2015-08-28 09:27:22 +0200 | [diff] [blame] | 25 | #include <linux/bug.h> |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 26 | |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 27 | /* device type dependent stuff */ |
| 28 | struct 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ébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 36 | int edge_sel_reg; |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 37 | unsigned low_level; |
| 38 | unsigned high_level; |
| 39 | unsigned rise_edge; |
| 40 | unsigned fall_edge; |
| 41 | }; |
| 42 | |
Anson Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 43 | struct 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 Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 52 | struct mxc_gpio_port { |
| 53 | struct list_head node; |
| 54 | void __iomem *base; |
Anson Huang | 2808801 | 2018-05-22 11:05:40 +0800 | [diff] [blame] | 55 | struct clk *clk; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 56 | int irq; |
| 57 | int irq_high; |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 58 | struct irq_domain *domain; |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 59 | struct gpio_chip gc; |
Bartosz Golaszewski | db5270a | 2017-08-09 14:25:06 +0200 | [diff] [blame] | 60 | struct device *dev; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 61 | u32 both_edges; |
Anson Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 62 | struct mxc_gpio_reg_saved gpio_saved_reg; |
| 63 | bool power_off; |
Fabio Estevam | 0f2c7af | 2020-11-17 07:59:17 -0300 | [diff] [blame] | 64 | const struct mxc_gpio_hwdata *hwdata; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 65 | }; |
| 66 | |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 67 | static 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ébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 75 | .edge_sel_reg = -EINVAL, |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 76 | .low_level = 0x03, |
| 77 | .high_level = 0x02, |
| 78 | .rise_edge = 0x00, |
| 79 | .fall_edge = 0x01, |
| 80 | }; |
| 81 | |
| 82 | static 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ébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 90 | .edge_sel_reg = -EINVAL, |
| 91 | .low_level = 0x00, |
| 92 | .high_level = 0x01, |
| 93 | .rise_edge = 0x02, |
| 94 | .fall_edge = 0x03, |
| 95 | }; |
| 96 | |
| 97 | static 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 Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 106 | .low_level = 0x00, |
| 107 | .high_level = 0x01, |
| 108 | .rise_edge = 0x02, |
| 109 | .fall_edge = 0x03, |
| 110 | }; |
| 111 | |
Fabio Estevam | 0f2c7af | 2020-11-17 07:59:17 -0300 | [diff] [blame] | 112 | #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 Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 120 | |
Fabio Estevam | 0f2c7af | 2020-11-17 07:59:17 -0300 | [diff] [blame] | 121 | #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ébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 125 | #define GPIO_INT_BOTH_EDGES 0x4 |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 126 | |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 127 | static const struct of_device_id mxc_gpio_dt_ids[] = { |
Fabio Estevam | 0f2c7af | 2020-11-17 07:59:17 -0300 | [diff] [blame] | 128 | { .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 Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 133 | { /* sentinel */ } |
| 134 | }; |
Anson Huang | 12d16b3 | 2020-09-17 13:33:46 +0800 | [diff] [blame] | 135 | MODULE_DEVICE_TABLE(of, mxc_gpio_dt_ids); |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 136 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 137 | /* |
| 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 | */ |
| 142 | static LIST_HEAD(mxc_gpio_ports); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 143 | |
| 144 | /* Note: This driver assumes 32 GPIOs are handled in one register */ |
| 145 | |
Lennert Buytenhek | 4d93579 | 2010-11-29 11:16:23 +0100 | [diff] [blame] | 146 | static int gpio_set_irq_type(struct irq_data *d, u32 type) |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 147 | { |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 148 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 149 | struct mxc_gpio_port *port = gc->private; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 150 | u32 bit, val; |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 151 | u32 gpio_idx = d->hwirq; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 152 | int edge; |
| 153 | void __iomem *reg = port->base; |
| 154 | |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 155 | port->both_edges &= ~(1 << gpio_idx); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 156 | switch (type) { |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 157 | case IRQ_TYPE_EDGE_RISING: |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 158 | edge = GPIO_INT_RISE_EDGE; |
| 159 | break; |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 160 | case IRQ_TYPE_EDGE_FALLING: |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 161 | edge = GPIO_INT_FALL_EDGE; |
| 162 | break; |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 163 | case IRQ_TYPE_EDGE_BOTH: |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 164 | if (GPIO_EDGE_SEL >= 0) { |
| 165 | edge = GPIO_INT_BOTH_EDGES; |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 166 | } else { |
Linus Walleij | 8d0bd9a | 2018-04-15 22:25:00 +0200 | [diff] [blame] | 167 | val = port->gc.get(&port->gc, gpio_idx); |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 168 | if (val) { |
| 169 | edge = GPIO_INT_LOW_LEV; |
Linus Walleij | 8d0bd9a | 2018-04-15 22:25:00 +0200 | [diff] [blame] | 170 | pr_debug("mxc: set GPIO %d to low trigger\n", gpio_idx); |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 171 | } else { |
| 172 | edge = GPIO_INT_HIGH_LEV; |
Linus Walleij | 8d0bd9a | 2018-04-15 22:25:00 +0200 | [diff] [blame] | 173 | pr_debug("mxc: set GPIO %d to high trigger\n", gpio_idx); |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 174 | } |
Linus Torvalds | f948ad0 | 2012-07-26 13:56:38 -0700 | [diff] [blame] | 175 | port->both_edges |= 1 << gpio_idx; |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 176 | } |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 177 | break; |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 178 | case IRQ_TYPE_LEVEL_LOW: |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 179 | edge = GPIO_INT_LOW_LEV; |
| 180 | break; |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 181 | case IRQ_TYPE_LEVEL_HIGH: |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 182 | edge = GPIO_INT_HIGH_LEV; |
| 183 | break; |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 184 | default: |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 185 | return -EINVAL; |
| 186 | } |
| 187 | |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 188 | if (GPIO_EDGE_SEL >= 0) { |
| 189 | val = readl(port->base + GPIO_EDGE_SEL); |
| 190 | if (edge == GPIO_INT_BOTH_EDGES) |
Linus Torvalds | f948ad0 | 2012-07-26 13:56:38 -0700 | [diff] [blame] | 191 | writel(val | (1 << gpio_idx), |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 192 | port->base + GPIO_EDGE_SEL); |
| 193 | else |
Linus Torvalds | f948ad0 | 2012-07-26 13:56:38 -0700 | [diff] [blame] | 194 | writel(val & ~(1 << gpio_idx), |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 195 | port->base + GPIO_EDGE_SEL); |
| 196 | } |
| 197 | |
| 198 | if (edge != GPIO_INT_BOTH_EDGES) { |
Linus Torvalds | f948ad0 | 2012-07-26 13:56:38 -0700 | [diff] [blame] | 199 | reg += GPIO_ICR1 + ((gpio_idx & 0x10) >> 2); /* lower or upper register */ |
| 200 | bit = gpio_idx & 0xf; |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 201 | val = readl(reg) & ~(0x3 << (bit << 1)); |
| 202 | writel(val | (edge << (bit << 1)), reg); |
| 203 | } |
| 204 | |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 205 | writel(1 << gpio_idx, port->base + GPIO_ISR); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 210 | static 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 Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 218 | val = readl(reg); |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 219 | edge = (val >> (bit << 1)) & 3; |
| 220 | val &= ~(0x3 << (bit << 1)); |
Uwe Kleine-König | 3d40f7f | 2010-02-05 22:14:37 +0100 | [diff] [blame] | 221 | if (edge == GPIO_INT_HIGH_LEV) { |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 222 | edge = GPIO_INT_LOW_LEV; |
| 223 | pr_debug("mxc: switch GPIO %d to low trigger\n", gpio); |
Uwe Kleine-König | 3d40f7f | 2010-02-05 22:14:37 +0100 | [diff] [blame] | 224 | } else if (edge == GPIO_INT_LOW_LEV) { |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 225 | edge = GPIO_INT_HIGH_LEV; |
| 226 | pr_debug("mxc: switch GPIO %d to high trigger\n", gpio); |
Uwe Kleine-König | 3d40f7f | 2010-02-05 22:14:37 +0100 | [diff] [blame] | 227 | } else { |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 228 | pr_err("mxc: invalid configuration for GPIO %d: %x\n", |
| 229 | gpio, edge); |
| 230 | return; |
| 231 | } |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 232 | writel(val | (edge << (bit << 1)), reg); |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Uwe Kleine-König | 3621f18 | 2010-02-08 21:02:30 +0100 | [diff] [blame] | 235 | /* handle 32 interrupts in one status register */ |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 236 | static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat) |
| 237 | { |
Uwe Kleine-König | 3621f18 | 2010-02-08 21:02:30 +0100 | [diff] [blame] | 238 | while (irq_stat != 0) { |
| 239 | int irqoffset = fls(irq_stat) - 1; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 240 | |
Uwe Kleine-König | 3621f18 | 2010-02-08 21:02:30 +0100 | [diff] [blame] | 241 | if (port->both_edges & (1 << irqoffset)) |
| 242 | mxc_flip_edge(port, irqoffset); |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 243 | |
Marc Zyngier | dbd1c54 | 2021-05-04 17:42:18 +0100 | [diff] [blame] | 244 | generic_handle_domain_irq(port->domain, irqoffset); |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 245 | |
Uwe Kleine-König | 3621f18 | 2010-02-08 21:02:30 +0100 | [diff] [blame] | 246 | irq_stat &= ~(1 << irqoffset); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Paulius Zaleckas | cfca8b5 | 2008-11-14 11:01:38 +0100 | [diff] [blame] | 250 | /* MX1 and MX3 has one interrupt *per* gpio port */ |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 251 | static void mx3_gpio_irq_handler(struct irq_desc *desc) |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 252 | { |
| 253 | u32 irq_stat; |
Jiang Liu | 476f8b4 | 2015-06-04 12:13:15 +0800 | [diff] [blame] | 254 | struct mxc_gpio_port *port = irq_desc_get_handler_data(desc); |
| 255 | struct irq_chip *chip = irq_desc_get_chip(desc); |
Shawn Guo | 0e44b6e | 2011-09-21 21:24:04 +0800 | [diff] [blame] | 256 | |
| 257 | chained_irq_enter(chip, desc); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 258 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 259 | irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR); |
Sascha Hauer | e2c97e7 | 2009-04-21 12:39:59 +0200 | [diff] [blame] | 260 | |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 261 | mxc_gpio_irq_handler(port, irq_stat); |
Shawn Guo | 0e44b6e | 2011-09-21 21:24:04 +0800 | [diff] [blame] | 262 | |
| 263 | chained_irq_exit(chip, desc); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 264 | } |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 265 | |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 266 | /* MX2 has one interrupt *for all* gpio ports */ |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 267 | static void mx2_gpio_irq_handler(struct irq_desc *desc) |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 268 | { |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 269 | u32 irq_msk, irq_stat; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 270 | struct mxc_gpio_port *port; |
Jiang Liu | 476f8b4 | 2015-06-04 12:13:15 +0800 | [diff] [blame] | 271 | struct irq_chip *chip = irq_desc_get_chip(desc); |
Uwe Kleine-König | c0e811d | 2013-07-18 14:58:06 +0200 | [diff] [blame] | 272 | |
| 273 | chained_irq_enter(chip, desc); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 274 | |
| 275 | /* walk through all interrupt status registers */ |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 276 | list_for_each_entry(port, &mxc_gpio_ports, node) { |
| 277 | irq_msk = readl(port->base + GPIO_IMR); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 278 | if (!irq_msk) |
| 279 | continue; |
| 280 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 281 | irq_stat = readl(port->base + GPIO_ISR) & irq_msk; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 282 | if (irq_stat) |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 283 | mxc_gpio_irq_handler(port, irq_stat); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 284 | } |
Uwe Kleine-König | c0e811d | 2013-07-18 14:58:06 +0200 | [diff] [blame] | 285 | chained_irq_exit(chip, desc); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 286 | } |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 287 | |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 288 | /* |
| 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 Buytenhek | 4d93579 | 2010-11-29 11:16:23 +0100 | [diff] [blame] | 297 | static int gpio_set_wake_irq(struct irq_data *d, u32 enable) |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 298 | { |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 299 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 300 | struct mxc_gpio_port *port = gc->private; |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 301 | u32 gpio_idx = d->hwirq; |
Philipp Rosenberger | 77a4d75 | 2017-07-12 10:36:40 +0200 | [diff] [blame] | 302 | int ret; |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 303 | |
| 304 | if (enable) { |
| 305 | if (port->irq_high && (gpio_idx >= 16)) |
Philipp Rosenberger | 77a4d75 | 2017-07-12 10:36:40 +0200 | [diff] [blame] | 306 | ret = enable_irq_wake(port->irq_high); |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 307 | else |
Philipp Rosenberger | 77a4d75 | 2017-07-12 10:36:40 +0200 | [diff] [blame] | 308 | ret = enable_irq_wake(port->irq); |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 309 | } else { |
| 310 | if (port->irq_high && (gpio_idx >= 16)) |
Philipp Rosenberger | 77a4d75 | 2017-07-12 10:36:40 +0200 | [diff] [blame] | 311 | ret = disable_irq_wake(port->irq_high); |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 312 | else |
Philipp Rosenberger | 77a4d75 | 2017-07-12 10:36:40 +0200 | [diff] [blame] | 313 | ret = disable_irq_wake(port->irq); |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 314 | } |
| 315 | |
Philipp Rosenberger | 77a4d75 | 2017-07-12 10:36:40 +0200 | [diff] [blame] | 316 | return ret; |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 317 | } |
| 318 | |
Peng Fan | 9e26b0b | 2015-08-23 21:11:52 +0800 | [diff] [blame] | 319 | static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base) |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 320 | { |
| 321 | struct irq_chip_generic *gc; |
| 322 | struct irq_chip_type *ct; |
Bartosz Golaszewski | db5270a | 2017-08-09 14:25:06 +0200 | [diff] [blame] | 323 | int rv; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 324 | |
Bartosz Golaszewski | db5270a | 2017-08-09 14:25:06 +0200 | [diff] [blame] | 325 | gc = devm_irq_alloc_generic_chip(port->dev, "gpio-mxc", 1, irq_base, |
| 326 | port->base, handle_level_irq); |
Peng Fan | 9e26b0b | 2015-08-23 21:11:52 +0800 | [diff] [blame] | 327 | if (!gc) |
| 328 | return -ENOMEM; |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 329 | gc->private = port; |
| 330 | |
| 331 | ct = gc->chip_types; |
Shawn Guo | 591567a | 2011-07-19 21:16:56 +0800 | [diff] [blame] | 332 | ct->chip.irq_ack = irq_gc_ack_set_bit; |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 333 | 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 Guo | 591567a | 2011-07-19 21:16:56 +0800 | [diff] [blame] | 336 | ct->chip.irq_set_wake = gpio_set_wake_irq; |
Loic Poulain | 3093e6c | 2021-06-17 15:54:13 +0200 | [diff] [blame] | 337 | ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND; |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 338 | ct->regs.ack = GPIO_ISR; |
| 339 | ct->regs.mask = GPIO_IMR; |
| 340 | |
Bartosz Golaszewski | db5270a | 2017-08-09 14:25:06 +0200 | [diff] [blame] | 341 | rv = devm_irq_setup_generic_chip(port->dev, gc, IRQ_MSK(32), |
| 342 | IRQ_GC_INIT_NESTED_LOCK, |
| 343 | IRQ_NOREQUEST, 0); |
Peng Fan | 9e26b0b | 2015-08-23 21:11:52 +0800 | [diff] [blame] | 344 | |
Bartosz Golaszewski | db5270a | 2017-08-09 14:25:06 +0200 | [diff] [blame] | 345 | return rv; |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 346 | } |
Thomas Gleixner | b5eee2f | 2011-04-04 14:29:58 +0200 | [diff] [blame] | 347 | |
Shawn Guo | 09ad803 | 2011-08-14 00:14:02 +0800 | [diff] [blame] | 348 | static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset) |
| 349 | { |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 350 | struct mxc_gpio_port *port = gpiochip_get_data(gc); |
Shawn Guo | 09ad803 | 2011-08-14 00:14:02 +0800 | [diff] [blame] | 351 | |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 352 | return irq_find_mapping(port->domain, offset); |
Shawn Guo | 09ad803 | 2011-08-14 00:14:02 +0800 | [diff] [blame] | 353 | } |
| 354 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 355 | static int mxc_gpio_probe(struct platform_device *pdev) |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 356 | { |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 357 | struct device_node *np = pdev->dev.of_node; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 358 | struct mxc_gpio_port *port; |
Anson Huang | c8f3d14 | 2019-09-19 17:39:17 +0800 | [diff] [blame] | 359 | int irq_count; |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 360 | int irq_base; |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 361 | int err; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 362 | |
Fabio Estevam | 8cd73e4 | 2013-07-08 17:14:39 -0300 | [diff] [blame] | 363 | port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL); |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 364 | if (!port) |
| 365 | return -ENOMEM; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 366 | |
Bartosz Golaszewski | db5270a | 2017-08-09 14:25:06 +0200 | [diff] [blame] | 367 | port->dev = &pdev->dev; |
| 368 | |
Fabio Estevam | 0f2c7af | 2020-11-17 07:59:17 -0300 | [diff] [blame] | 369 | port->hwdata = device_get_match_data(&pdev->dev); |
| 370 | |
Enrico Weigelt, metux IT consult | 123ac0e | 2019-03-11 19:55:01 +0100 | [diff] [blame] | 371 | port->base = devm_platform_ioremap_resource(pdev, 0); |
Fabio Estevam | 8cd73e4 | 2013-07-08 17:14:39 -0300 | [diff] [blame] | 372 | if (IS_ERR(port->base)) |
| 373 | return PTR_ERR(port->base); |
Baruch Siach | 14cb0de | 2010-07-06 14:03:22 +0300 | [diff] [blame] | 374 | |
Anson Huang | c8f3d14 | 2019-09-19 17:39:17 +0800 | [diff] [blame] | 375 | 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 Rosenberger | cc9269f | 2017-07-12 10:36:39 +0200 | [diff] [blame] | 384 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 385 | port->irq = platform_get_irq(pdev, 0); |
Fabio Estevam | 8cd73e4 | 2013-07-08 17:14:39 -0300 | [diff] [blame] | 386 | if (port->irq < 0) |
Sachin Kamat | 5ea80e4 | 2013-12-21 13:05:57 +0530 | [diff] [blame] | 387 | return port->irq; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 388 | |
Anson Huang | 2808801 | 2018-05-22 11:05:40 +0800 | [diff] [blame] | 389 | /* the controller clock is optional */ |
Anson Huang | 7beb620 | 2019-08-01 16:44:39 +0800 | [diff] [blame] | 390 | port->clk = devm_clk_get_optional(&pdev->dev, NULL); |
| 391 | if (IS_ERR(port->clk)) |
| 392 | return PTR_ERR(port->clk); |
Anson Huang | 2808801 | 2018-05-22 11:05:40 +0800 | [diff] [blame] | 393 | |
| 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 Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 400 | if (of_device_is_compatible(np, "fsl,imx7d-gpio")) |
| 401 | port->power_off = true; |
| 402 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 403 | /* disable the interrupt and clear the status */ |
| 404 | writel(0, port->base + GPIO_IMR); |
| 405 | writel(~0, port->base + GPIO_ISR); |
| 406 | |
Fabio Estevam | 0f2c7af | 2020-11-17 07:59:17 -0300 | [diff] [blame] | 407 | if (of_device_is_compatible(np, "fsl,imx21-gpio")) { |
Uwe Kleine-König | 33a4e98 | 2012-06-06 11:49:23 +0200 | [diff] [blame] | 408 | /* |
| 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 Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 414 | } else { |
| 415 | /* setup one handler for each entry */ |
Russell King | e65eea5 | 2015-06-16 23:06:40 +0100 | [diff] [blame] | 416 | irq_set_chained_handler_and_data(port->irq, |
| 417 | mx3_gpio_irq_handler, port); |
| 418 | if (port->irq_high > 0) |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 419 | /* setup handler for GPIO 16 to 31 */ |
Russell King | e65eea5 | 2015-06-16 23:06:40 +0100 | [diff] [blame] | 420 | irq_set_chained_handler_and_data(port->irq_high, |
| 421 | mx3_gpio_irq_handler, |
| 422 | port); |
Sascha Hauer | 8afaada | 2009-06-15 12:36:25 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 425 | err = bgpio_init(&port->gc, &pdev->dev, 4, |
Shawn Guo | 2ce420d | 2011-06-06 13:22:41 +0800 | [diff] [blame] | 426 | port->base + GPIO_PSR, |
| 427 | port->base + GPIO_DR, NULL, |
Vladimir Zapolskiy | 442b249 | 2015-04-29 18:35:01 +0300 | [diff] [blame] | 428 | port->base + GPIO_GDIR, NULL, |
| 429 | BGPIOF_READ_OUTPUT_REG_SET); |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 430 | if (err) |
Fabio Estevam | 8cd73e4 | 2013-07-08 17:14:39 -0300 | [diff] [blame] | 431 | goto out_bgio; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 432 | |
Thierry Reding | f0254b5 | 2020-04-01 22:05:26 +0200 | [diff] [blame] | 433 | port->gc.request = gpiochip_generic_request; |
| 434 | port->gc.free = gpiochip_generic_free; |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 435 | port->gc.to_irq = mxc_gpio_to_irq; |
| 436 | port->gc.base = (pdev->id < 0) ? of_alias_get_id(np, "gpio") * 32 : |
Shawn Guo | 7e6086d | 2012-08-05 14:01:26 +0800 | [diff] [blame] | 437 | pdev->id * 32; |
Shawn Guo | 2ce420d | 2011-06-06 13:22:41 +0800 | [diff] [blame] | 438 | |
Laxman Dewangan | ffc5663 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 439 | err = devm_gpiochip_add_data(&pdev->dev, &port->gc, port); |
Shawn Guo | 2ce420d | 2011-06-06 13:22:41 +0800 | [diff] [blame] | 440 | if (err) |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 441 | goto out_bgio; |
Shawn Guo | 2ce420d | 2011-06-06 13:22:41 +0800 | [diff] [blame] | 442 | |
Bartosz Golaszewski | c553c3c | 2017-03-04 17:23:38 +0100 | [diff] [blame] | 443 | irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, 32, numa_node_id()); |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 444 | if (irq_base < 0) { |
| 445 | err = irq_base; |
Laxman Dewangan | ffc5663 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 446 | goto out_bgio; |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 447 | } |
| 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 Golaszewski | c553c3c | 2017-03-04 17:23:38 +0100 | [diff] [blame] | 453 | goto out_bgio; |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 454 | } |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 455 | |
| 456 | /* gpio-mxc can be a generic irq chip */ |
Peng Fan | 9e26b0b | 2015-08-23 21:11:52 +0800 | [diff] [blame] | 457 | err = mxc_gpio_init_gc(port, irq_base); |
| 458 | if (err < 0) |
| 459 | goto out_irqdomain_remove; |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 460 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 461 | list_add_tail(&port->node, &mxc_gpio_ports); |
| 462 | |
Anson Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 463 | platform_set_drvdata(pdev, port); |
| 464 | |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 465 | return 0; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 466 | |
Peng Fan | 9e26b0b | 2015-08-23 21:11:52 +0800 | [diff] [blame] | 467 | out_irqdomain_remove: |
| 468 | irq_domain_remove(port->domain); |
Fabio Estevam | 8cd73e4 | 2013-07-08 17:14:39 -0300 | [diff] [blame] | 469 | out_bgio: |
Anson Huang | 2808801 | 2018-05-22 11:05:40 +0800 | [diff] [blame] | 470 | clk_disable_unprepare(port->clk); |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 471 | dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err); |
| 472 | return err; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 473 | } |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 474 | |
Anson Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 475 | static 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 | |
| 488 | static 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 Huang | 1a5287a | 2018-11-09 04:56:56 +0000 | [diff] [blame] | 501 | static int mxc_gpio_syscore_suspend(void) |
Anson Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 502 | { |
Anson Huang | 1a5287a | 2018-11-09 04:56:56 +0000 | [diff] [blame] | 503 | struct mxc_gpio_port *port; |
Anson Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 504 | |
Anson Huang | 1a5287a | 2018-11-09 04:56:56 +0000 | [diff] [blame] | 505 | /* 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 Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
Anson Huang | 1a5287a | 2018-11-09 04:56:56 +0000 | [diff] [blame] | 514 | static void mxc_gpio_syscore_resume(void) |
Anson Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 515 | { |
Anson Huang | 1a5287a | 2018-11-09 04:56:56 +0000 | [diff] [blame] | 516 | struct mxc_gpio_port *port; |
Anson Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 517 | int ret; |
| 518 | |
Anson Huang | 1a5287a | 2018-11-09 04:56:56 +0000 | [diff] [blame] | 519 | /* 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 Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 528 | } |
| 529 | |
Anson Huang | 1a5287a | 2018-11-09 04:56:56 +0000 | [diff] [blame] | 530 | static struct syscore_ops mxc_gpio_syscore_ops = { |
| 531 | .suspend = mxc_gpio_syscore_suspend, |
| 532 | .resume = mxc_gpio_syscore_resume, |
Anson Huang | c19fdae | 2018-07-18 09:25:32 +0800 | [diff] [blame] | 533 | }; |
| 534 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 535 | static struct platform_driver mxc_gpio_driver = { |
| 536 | .driver = { |
| 537 | .name = "gpio-mxc", |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 538 | .of_match_table = mxc_gpio_dt_ids, |
Bartosz Golaszewski | 90e1fc4 | 2017-08-09 14:25:00 +0200 | [diff] [blame] | 539 | .suppress_bind_attrs = true, |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 540 | }, |
| 541 | .probe = mxc_gpio_probe, |
| 542 | }; |
| 543 | |
| 544 | static int __init gpio_mxc_init(void) |
| 545 | { |
Anson Huang | 1a5287a | 2018-11-09 04:56:56 +0000 | [diff] [blame] | 546 | register_syscore_ops(&mxc_gpio_syscore_ops); |
| 547 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 548 | return platform_driver_register(&mxc_gpio_driver); |
| 549 | } |
Vladimir Zapolskiy | e188cbf | 2016-09-08 04:48:15 +0300 | [diff] [blame] | 550 | subsys_initcall(gpio_mxc_init); |
Anson Huang | 12d16b3 | 2020-09-17 13:33:46 +0800 | [diff] [blame] | 551 | |
| 552 | MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>"); |
| 553 | MODULE_DESCRIPTION("i.MX GPIO Driver"); |
| 554 | MODULE_LICENSE("GPL"); |