Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de> |
| 3 | * Copyright 2008 Juergen Beisert, kernel@pengutronix.de |
| 4 | * |
| 5 | * Based on code from Freescale, |
Dinh Nguyen | e24798e | 2010-04-22 16:28:42 +0300 | [diff] [blame] | 6 | * Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved. |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version 2 |
| 11 | * of the License, or (at your option) any later version. |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/init.h> |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 24 | #include <linux/io.h> |
| 25 | #include <linux/irq.h> |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 26 | #include <linux/irqdomain.h> |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 27 | #include <linux/gpio.h> |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/slab.h> |
Shawn Guo | 2ce420d | 2011-06-06 13:22:41 +0800 | [diff] [blame] | 30 | #include <linux/basic_mmio_gpio.h> |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 31 | #include <linux/of.h> |
| 32 | #include <linux/of_device.h> |
Paul Gortmaker | bb207ef | 2011-07-03 13:38:09 -0400 | [diff] [blame] | 33 | #include <linux/module.h> |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 34 | #include <asm-generic/bug.h> |
Shawn Guo | 0e44b6e | 2011-09-21 21:24:04 +0800 | [diff] [blame] | 35 | #include <asm/mach/irq.h> |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 36 | |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 37 | enum mxc_gpio_hwtype { |
| 38 | IMX1_GPIO, /* runs on i.mx1 */ |
| 39 | IMX21_GPIO, /* runs on i.mx21 and i.mx27 */ |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 40 | IMX31_GPIO, /* runs on i.mx31 */ |
| 41 | IMX35_GPIO, /* runs on all other i.mx */ |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /* device type dependent stuff */ |
| 45 | struct mxc_gpio_hwdata { |
| 46 | unsigned dr_reg; |
| 47 | unsigned gdir_reg; |
| 48 | unsigned psr_reg; |
| 49 | unsigned icr1_reg; |
| 50 | unsigned icr2_reg; |
| 51 | unsigned imr_reg; |
| 52 | unsigned isr_reg; |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 53 | int edge_sel_reg; |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 54 | unsigned low_level; |
| 55 | unsigned high_level; |
| 56 | unsigned rise_edge; |
| 57 | unsigned fall_edge; |
| 58 | }; |
| 59 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 60 | struct mxc_gpio_port { |
| 61 | struct list_head node; |
| 62 | void __iomem *base; |
| 63 | int irq; |
| 64 | int irq_high; |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 65 | struct irq_domain *domain; |
Shawn Guo | 2ce420d | 2011-06-06 13:22:41 +0800 | [diff] [blame] | 66 | struct bgpio_chip bgc; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 67 | u32 both_edges; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 68 | }; |
| 69 | |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 70 | static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = { |
| 71 | .dr_reg = 0x1c, |
| 72 | .gdir_reg = 0x00, |
| 73 | .psr_reg = 0x24, |
| 74 | .icr1_reg = 0x28, |
| 75 | .icr2_reg = 0x2c, |
| 76 | .imr_reg = 0x30, |
| 77 | .isr_reg = 0x34, |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 78 | .edge_sel_reg = -EINVAL, |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 79 | .low_level = 0x03, |
| 80 | .high_level = 0x02, |
| 81 | .rise_edge = 0x00, |
| 82 | .fall_edge = 0x01, |
| 83 | }; |
| 84 | |
| 85 | static struct mxc_gpio_hwdata imx31_gpio_hwdata = { |
| 86 | .dr_reg = 0x00, |
| 87 | .gdir_reg = 0x04, |
| 88 | .psr_reg = 0x08, |
| 89 | .icr1_reg = 0x0c, |
| 90 | .icr2_reg = 0x10, |
| 91 | .imr_reg = 0x14, |
| 92 | .isr_reg = 0x18, |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 93 | .edge_sel_reg = -EINVAL, |
| 94 | .low_level = 0x00, |
| 95 | .high_level = 0x01, |
| 96 | .rise_edge = 0x02, |
| 97 | .fall_edge = 0x03, |
| 98 | }; |
| 99 | |
| 100 | static struct mxc_gpio_hwdata imx35_gpio_hwdata = { |
| 101 | .dr_reg = 0x00, |
| 102 | .gdir_reg = 0x04, |
| 103 | .psr_reg = 0x08, |
| 104 | .icr1_reg = 0x0c, |
| 105 | .icr2_reg = 0x10, |
| 106 | .imr_reg = 0x14, |
| 107 | .isr_reg = 0x18, |
| 108 | .edge_sel_reg = 0x1c, |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 109 | .low_level = 0x00, |
| 110 | .high_level = 0x01, |
| 111 | .rise_edge = 0x02, |
| 112 | .fall_edge = 0x03, |
| 113 | }; |
| 114 | |
| 115 | static enum mxc_gpio_hwtype mxc_gpio_hwtype; |
| 116 | static struct mxc_gpio_hwdata *mxc_gpio_hwdata; |
| 117 | |
| 118 | #define GPIO_DR (mxc_gpio_hwdata->dr_reg) |
| 119 | #define GPIO_GDIR (mxc_gpio_hwdata->gdir_reg) |
| 120 | #define GPIO_PSR (mxc_gpio_hwdata->psr_reg) |
| 121 | #define GPIO_ICR1 (mxc_gpio_hwdata->icr1_reg) |
| 122 | #define GPIO_ICR2 (mxc_gpio_hwdata->icr2_reg) |
| 123 | #define GPIO_IMR (mxc_gpio_hwdata->imr_reg) |
| 124 | #define GPIO_ISR (mxc_gpio_hwdata->isr_reg) |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 125 | #define GPIO_EDGE_SEL (mxc_gpio_hwdata->edge_sel_reg) |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 126 | |
| 127 | #define GPIO_INT_LOW_LEV (mxc_gpio_hwdata->low_level) |
| 128 | #define GPIO_INT_HIGH_LEV (mxc_gpio_hwdata->high_level) |
| 129 | #define GPIO_INT_RISE_EDGE (mxc_gpio_hwdata->rise_edge) |
| 130 | #define GPIO_INT_FALL_EDGE (mxc_gpio_hwdata->fall_edge) |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 131 | #define GPIO_INT_BOTH_EDGES 0x4 |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 132 | |
| 133 | static struct platform_device_id mxc_gpio_devtype[] = { |
| 134 | { |
| 135 | .name = "imx1-gpio", |
| 136 | .driver_data = IMX1_GPIO, |
| 137 | }, { |
| 138 | .name = "imx21-gpio", |
| 139 | .driver_data = IMX21_GPIO, |
| 140 | }, { |
| 141 | .name = "imx31-gpio", |
| 142 | .driver_data = IMX31_GPIO, |
| 143 | }, { |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 144 | .name = "imx35-gpio", |
| 145 | .driver_data = IMX35_GPIO, |
| 146 | }, { |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 147 | /* sentinel */ |
| 148 | } |
| 149 | }; |
| 150 | |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 151 | static const struct of_device_id mxc_gpio_dt_ids[] = { |
| 152 | { .compatible = "fsl,imx1-gpio", .data = &mxc_gpio_devtype[IMX1_GPIO], }, |
| 153 | { .compatible = "fsl,imx21-gpio", .data = &mxc_gpio_devtype[IMX21_GPIO], }, |
| 154 | { .compatible = "fsl,imx31-gpio", .data = &mxc_gpio_devtype[IMX31_GPIO], }, |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 155 | { .compatible = "fsl,imx35-gpio", .data = &mxc_gpio_devtype[IMX35_GPIO], }, |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 156 | { /* sentinel */ } |
| 157 | }; |
| 158 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 159 | /* |
| 160 | * MX2 has one interrupt *for all* gpio ports. The list is used |
| 161 | * to save the references to all ports, so that mx2_gpio_irq_handler |
| 162 | * can walk through all interrupt status registers. |
| 163 | */ |
| 164 | static LIST_HEAD(mxc_gpio_ports); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 165 | |
| 166 | /* Note: This driver assumes 32 GPIOs are handled in one register */ |
| 167 | |
Lennert Buytenhek | 4d93579 | 2010-11-29 11:16:23 +0100 | [diff] [blame] | 168 | static int gpio_set_irq_type(struct irq_data *d, u32 type) |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 169 | { |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 170 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 171 | struct mxc_gpio_port *port = gc->private; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 172 | u32 bit, val; |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 173 | u32 gpio_idx = d->hwirq; |
| 174 | u32 gpio = port->bgc.gc.base + gpio_idx; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 175 | int edge; |
| 176 | void __iomem *reg = port->base; |
| 177 | |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 178 | port->both_edges &= ~(1 << gpio_idx); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 179 | switch (type) { |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 180 | case IRQ_TYPE_EDGE_RISING: |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 181 | edge = GPIO_INT_RISE_EDGE; |
| 182 | break; |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 183 | case IRQ_TYPE_EDGE_FALLING: |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 184 | edge = GPIO_INT_FALL_EDGE; |
| 185 | break; |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 186 | case IRQ_TYPE_EDGE_BOTH: |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 187 | if (GPIO_EDGE_SEL >= 0) { |
| 188 | edge = GPIO_INT_BOTH_EDGES; |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 189 | } else { |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 190 | val = gpio_get_value(gpio); |
| 191 | if (val) { |
| 192 | edge = GPIO_INT_LOW_LEV; |
| 193 | pr_debug("mxc: set GPIO %d to low trigger\n", gpio); |
| 194 | } else { |
| 195 | edge = GPIO_INT_HIGH_LEV; |
| 196 | pr_debug("mxc: set GPIO %d to high trigger\n", gpio); |
| 197 | } |
Linus Torvalds | f948ad0 | 2012-07-26 13:56:38 -0700 | [diff] [blame] | 198 | port->both_edges |= 1 << gpio_idx; |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 199 | } |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 200 | break; |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 201 | case IRQ_TYPE_LEVEL_LOW: |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 202 | edge = GPIO_INT_LOW_LEV; |
| 203 | break; |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 204 | case IRQ_TYPE_LEVEL_HIGH: |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 205 | edge = GPIO_INT_HIGH_LEV; |
| 206 | break; |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 207 | default: |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 208 | return -EINVAL; |
| 209 | } |
| 210 | |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 211 | if (GPIO_EDGE_SEL >= 0) { |
| 212 | val = readl(port->base + GPIO_EDGE_SEL); |
| 213 | if (edge == GPIO_INT_BOTH_EDGES) |
Linus Torvalds | f948ad0 | 2012-07-26 13:56:38 -0700 | [diff] [blame] | 214 | writel(val | (1 << gpio_idx), |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 215 | port->base + GPIO_EDGE_SEL); |
| 216 | else |
Linus Torvalds | f948ad0 | 2012-07-26 13:56:38 -0700 | [diff] [blame] | 217 | writel(val & ~(1 << gpio_idx), |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 218 | port->base + GPIO_EDGE_SEL); |
| 219 | } |
| 220 | |
| 221 | if (edge != GPIO_INT_BOTH_EDGES) { |
Linus Torvalds | f948ad0 | 2012-07-26 13:56:38 -0700 | [diff] [blame] | 222 | reg += GPIO_ICR1 + ((gpio_idx & 0x10) >> 2); /* lower or upper register */ |
| 223 | bit = gpio_idx & 0xf; |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 224 | val = readl(reg) & ~(0x3 << (bit << 1)); |
| 225 | writel(val | (edge << (bit << 1)), reg); |
| 226 | } |
| 227 | |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 228 | writel(1 << gpio_idx, port->base + GPIO_ISR); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 233 | static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio) |
| 234 | { |
| 235 | void __iomem *reg = port->base; |
| 236 | u32 bit, val; |
| 237 | int edge; |
| 238 | |
| 239 | reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */ |
| 240 | bit = gpio & 0xf; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 241 | val = readl(reg); |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 242 | edge = (val >> (bit << 1)) & 3; |
| 243 | val &= ~(0x3 << (bit << 1)); |
Uwe Kleine-König | 3d40f7f | 2010-02-05 22:14:37 +0100 | [diff] [blame] | 244 | if (edge == GPIO_INT_HIGH_LEV) { |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 245 | edge = GPIO_INT_LOW_LEV; |
| 246 | 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] | 247 | } else if (edge == GPIO_INT_LOW_LEV) { |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 248 | edge = GPIO_INT_HIGH_LEV; |
| 249 | 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] | 250 | } else { |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 251 | pr_err("mxc: invalid configuration for GPIO %d: %x\n", |
| 252 | gpio, edge); |
| 253 | return; |
| 254 | } |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 255 | writel(val | (edge << (bit << 1)), reg); |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Uwe Kleine-König | 3621f18 | 2010-02-08 21:02:30 +0100 | [diff] [blame] | 258 | /* handle 32 interrupts in one status register */ |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 259 | static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat) |
| 260 | { |
Uwe Kleine-König | 3621f18 | 2010-02-08 21:02:30 +0100 | [diff] [blame] | 261 | while (irq_stat != 0) { |
| 262 | int irqoffset = fls(irq_stat) - 1; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 263 | |
Uwe Kleine-König | 3621f18 | 2010-02-08 21:02:30 +0100 | [diff] [blame] | 264 | if (port->both_edges & (1 << irqoffset)) |
| 265 | mxc_flip_edge(port, irqoffset); |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 266 | |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 267 | generic_handle_irq(irq_find_mapping(port->domain, irqoffset)); |
Guennadi Liakhovetski | 910862e | 2009-03-12 12:46:41 +0100 | [diff] [blame] | 268 | |
Uwe Kleine-König | 3621f18 | 2010-02-08 21:02:30 +0100 | [diff] [blame] | 269 | irq_stat &= ~(1 << irqoffset); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Paulius Zaleckas | cfca8b5 | 2008-11-14 11:01:38 +0100 | [diff] [blame] | 273 | /* MX1 and MX3 has one interrupt *per* gpio port */ |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 274 | static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc) |
| 275 | { |
| 276 | u32 irq_stat; |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 277 | struct mxc_gpio_port *port = irq_get_handler_data(irq); |
Shawn Guo | 0e44b6e | 2011-09-21 21:24:04 +0800 | [diff] [blame] | 278 | struct irq_chip *chip = irq_get_chip(irq); |
| 279 | |
| 280 | chained_irq_enter(chip, desc); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 281 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 282 | irq_stat = readl(port->base + GPIO_ISR) & readl(port->base + GPIO_IMR); |
Sascha Hauer | e2c97e7 | 2009-04-21 12:39:59 +0200 | [diff] [blame] | 283 | |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 284 | mxc_gpio_irq_handler(port, irq_stat); |
Shawn Guo | 0e44b6e | 2011-09-21 21:24:04 +0800 | [diff] [blame] | 285 | |
| 286 | chained_irq_exit(chip, desc); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 287 | } |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 288 | |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 289 | /* MX2 has one interrupt *for all* gpio ports */ |
| 290 | static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc) |
| 291 | { |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 292 | u32 irq_msk, irq_stat; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 293 | struct mxc_gpio_port *port; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 294 | |
| 295 | /* walk through all interrupt status registers */ |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 296 | list_for_each_entry(port, &mxc_gpio_ports, node) { |
| 297 | irq_msk = readl(port->base + GPIO_IMR); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 298 | if (!irq_msk) |
| 299 | continue; |
| 300 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 301 | irq_stat = readl(port->base + GPIO_ISR) & irq_msk; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 302 | if (irq_stat) |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 303 | mxc_gpio_irq_handler(port, irq_stat); |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 304 | } |
| 305 | } |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 306 | |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 307 | /* |
| 308 | * Set interrupt number "irq" in the GPIO as a wake-up source. |
| 309 | * While system is running, all registered GPIO interrupts need to have |
| 310 | * wake-up enabled. When system is suspended, only selected GPIO interrupts |
| 311 | * need to have wake-up enabled. |
| 312 | * @param irq interrupt source number |
| 313 | * @param enable enable as wake-up if equal to non-zero |
| 314 | * @return This function returns 0 on success. |
| 315 | */ |
Lennert Buytenhek | 4d93579 | 2010-11-29 11:16:23 +0100 | [diff] [blame] | 316 | static int gpio_set_wake_irq(struct irq_data *d, u32 enable) |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 317 | { |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 318 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 319 | struct mxc_gpio_port *port = gc->private; |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 320 | u32 gpio_idx = d->hwirq; |
Dinh Nguyen | a3484ff | 2010-10-23 09:12:48 -0500 | [diff] [blame] | 321 | |
| 322 | if (enable) { |
| 323 | if (port->irq_high && (gpio_idx >= 16)) |
| 324 | enable_irq_wake(port->irq_high); |
| 325 | else |
| 326 | enable_irq_wake(port->irq); |
| 327 | } else { |
| 328 | if (port->irq_high && (gpio_idx >= 16)) |
| 329 | disable_irq_wake(port->irq_high); |
| 330 | else |
| 331 | disable_irq_wake(port->irq); |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 337 | static void __init mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base) |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 338 | { |
| 339 | struct irq_chip_generic *gc; |
| 340 | struct irq_chip_type *ct; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 341 | |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 342 | gc = irq_alloc_generic_chip("gpio-mxc", 1, irq_base, |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 343 | port->base, handle_level_irq); |
| 344 | gc->private = port; |
| 345 | |
| 346 | ct = gc->chip_types; |
Shawn Guo | 591567a | 2011-07-19 21:16:56 +0800 | [diff] [blame] | 347 | ct->chip.irq_ack = irq_gc_ack_set_bit; |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 348 | ct->chip.irq_mask = irq_gc_mask_clr_bit; |
| 349 | ct->chip.irq_unmask = irq_gc_mask_set_bit; |
| 350 | ct->chip.irq_set_type = gpio_set_irq_type; |
Shawn Guo | 591567a | 2011-07-19 21:16:56 +0800 | [diff] [blame] | 351 | ct->chip.irq_set_wake = gpio_set_wake_irq; |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 352 | ct->regs.ack = GPIO_ISR; |
| 353 | ct->regs.mask = GPIO_IMR; |
| 354 | |
| 355 | irq_setup_generic_chip(gc, IRQ_MSK(32), IRQ_GC_INIT_NESTED_LOCK, |
| 356 | IRQ_NOREQUEST, 0); |
| 357 | } |
Thomas Gleixner | b5eee2f | 2011-04-04 14:29:58 +0200 | [diff] [blame] | 358 | |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 359 | static void __devinit mxc_gpio_get_hw(struct platform_device *pdev) |
| 360 | { |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 361 | const struct of_device_id *of_id = |
| 362 | of_match_device(mxc_gpio_dt_ids, &pdev->dev); |
| 363 | enum mxc_gpio_hwtype hwtype; |
| 364 | |
| 365 | if (of_id) |
| 366 | pdev->id_entry = of_id->data; |
| 367 | hwtype = pdev->id_entry->driver_data; |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 368 | |
| 369 | if (mxc_gpio_hwtype) { |
| 370 | /* |
| 371 | * The driver works with a reasonable presupposition, |
| 372 | * that is all gpio ports must be the same type when |
| 373 | * running on one soc. |
| 374 | */ |
| 375 | BUG_ON(mxc_gpio_hwtype != hwtype); |
| 376 | return; |
| 377 | } |
| 378 | |
Benoît Thébaudeau | aeb2774 | 2012-06-22 21:04:06 +0200 | [diff] [blame] | 379 | if (hwtype == IMX35_GPIO) |
| 380 | mxc_gpio_hwdata = &imx35_gpio_hwdata; |
| 381 | else if (hwtype == IMX31_GPIO) |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 382 | mxc_gpio_hwdata = &imx31_gpio_hwdata; |
| 383 | else |
| 384 | mxc_gpio_hwdata = &imx1_imx21_gpio_hwdata; |
| 385 | |
| 386 | mxc_gpio_hwtype = hwtype; |
| 387 | } |
| 388 | |
Shawn Guo | 09ad803 | 2011-08-14 00:14:02 +0800 | [diff] [blame] | 389 | static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset) |
| 390 | { |
| 391 | struct bgpio_chip *bgc = to_bgpio_chip(gc); |
| 392 | struct mxc_gpio_port *port = |
| 393 | container_of(bgc, struct mxc_gpio_port, bgc); |
| 394 | |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 395 | return irq_find_mapping(port->domain, offset); |
Shawn Guo | 09ad803 | 2011-08-14 00:14:02 +0800 | [diff] [blame] | 396 | } |
| 397 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 398 | static int __devinit mxc_gpio_probe(struct platform_device *pdev) |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 399 | { |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 400 | struct device_node *np = pdev->dev.of_node; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 401 | struct mxc_gpio_port *port; |
| 402 | struct resource *iores; |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 403 | int irq_base; |
Shawn Guo | e4ea933 | 2011-06-07 16:25:37 +0800 | [diff] [blame] | 404 | int err; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 405 | |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 406 | mxc_gpio_get_hw(pdev); |
| 407 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 408 | port = kzalloc(sizeof(struct mxc_gpio_port), GFP_KERNEL); |
| 409 | if (!port) |
| 410 | return -ENOMEM; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 411 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 412 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 413 | if (!iores) { |
| 414 | err = -ENODEV; |
| 415 | goto out_kfree; |
| 416 | } |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 417 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 418 | if (!request_mem_region(iores->start, resource_size(iores), |
| 419 | pdev->name)) { |
| 420 | err = -EBUSY; |
| 421 | goto out_kfree; |
| 422 | } |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 423 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 424 | port->base = ioremap(iores->start, resource_size(iores)); |
| 425 | if (!port->base) { |
| 426 | err = -ENOMEM; |
| 427 | goto out_release_mem; |
| 428 | } |
Baruch Siach | 14cb0de | 2010-07-06 14:03:22 +0300 | [diff] [blame] | 429 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 430 | port->irq_high = platform_get_irq(pdev, 1); |
| 431 | port->irq = platform_get_irq(pdev, 0); |
| 432 | if (port->irq < 0) { |
| 433 | err = -EINVAL; |
| 434 | goto out_iounmap; |
| 435 | } |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 436 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 437 | /* disable the interrupt and clear the status */ |
| 438 | writel(0, port->base + GPIO_IMR); |
| 439 | writel(~0, port->base + GPIO_ISR); |
| 440 | |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 441 | if (mxc_gpio_hwtype == IMX21_GPIO) { |
Uwe Kleine-König | 33a4e98 | 2012-06-06 11:49:23 +0200 | [diff] [blame] | 442 | /* |
| 443 | * Setup one handler for all GPIO interrupts. Actually setting |
| 444 | * the handler is needed only once, but doing it for every port |
| 445 | * is more robust and easier. |
| 446 | */ |
| 447 | irq_set_chained_handler(port->irq, mx2_gpio_irq_handler); |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 448 | } else { |
| 449 | /* setup one handler for each entry */ |
| 450 | irq_set_chained_handler(port->irq, mx3_gpio_irq_handler); |
| 451 | irq_set_handler_data(port->irq, port); |
| 452 | if (port->irq_high > 0) { |
| 453 | /* setup handler for GPIO 16 to 31 */ |
| 454 | irq_set_chained_handler(port->irq_high, |
| 455 | mx3_gpio_irq_handler); |
| 456 | irq_set_handler_data(port->irq_high, port); |
| 457 | } |
Sascha Hauer | 8afaada | 2009-06-15 12:36:25 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Shawn Guo | 2ce420d | 2011-06-06 13:22:41 +0800 | [diff] [blame] | 460 | err = bgpio_init(&port->bgc, &pdev->dev, 4, |
| 461 | port->base + GPIO_PSR, |
| 462 | port->base + GPIO_DR, NULL, |
Shawn Guo | 3e11f7b | 2012-05-19 21:34:58 +0800 | [diff] [blame] | 463 | port->base + GPIO_GDIR, NULL, 0); |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 464 | if (err) |
| 465 | goto out_iounmap; |
| 466 | |
Shawn Guo | 09ad803 | 2011-08-14 00:14:02 +0800 | [diff] [blame] | 467 | port->bgc.gc.to_irq = mxc_gpio_to_irq; |
Shawn Guo | 7e6086d | 2012-08-05 14:01:26 +0800 | [diff] [blame] | 468 | port->bgc.gc.base = (pdev->id < 0) ? of_alias_get_id(np, "gpio") * 32 : |
| 469 | pdev->id * 32; |
Shawn Guo | 2ce420d | 2011-06-06 13:22:41 +0800 | [diff] [blame] | 470 | |
| 471 | err = gpiochip_add(&port->bgc.gc); |
| 472 | if (err) |
| 473 | goto out_bgpio_remove; |
| 474 | |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 475 | irq_base = irq_alloc_descs(-1, 0, 32, numa_node_id()); |
| 476 | if (irq_base < 0) { |
| 477 | err = irq_base; |
| 478 | goto out_gpiochip_remove; |
| 479 | } |
| 480 | |
| 481 | port->domain = irq_domain_add_legacy(np, 32, irq_base, 0, |
| 482 | &irq_domain_simple_ops, NULL); |
| 483 | if (!port->domain) { |
| 484 | err = -ENODEV; |
| 485 | goto out_irqdesc_free; |
| 486 | } |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 487 | |
| 488 | /* gpio-mxc can be a generic irq chip */ |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 489 | mxc_gpio_init_gc(port, irq_base); |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 490 | |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 491 | list_add_tail(&port->node, &mxc_gpio_ports); |
| 492 | |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 493 | return 0; |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 494 | |
Shawn Guo | 1ab7ef1 | 2012-06-13 09:04:03 +0800 | [diff] [blame] | 495 | out_irqdesc_free: |
| 496 | irq_free_descs(irq_base, 32); |
| 497 | out_gpiochip_remove: |
| 498 | WARN_ON(gpiochip_remove(&port->bgc.gc) < 0); |
Shawn Guo | 2ce420d | 2011-06-06 13:22:41 +0800 | [diff] [blame] | 499 | out_bgpio_remove: |
| 500 | bgpio_remove(&port->bgc); |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 501 | out_iounmap: |
| 502 | iounmap(port->base); |
| 503 | out_release_mem: |
| 504 | release_mem_region(iores->start, resource_size(iores)); |
| 505 | out_kfree: |
| 506 | kfree(port); |
| 507 | dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, err); |
| 508 | return err; |
Juergen Beisert | 07bd1a6 | 2008-07-05 10:02:49 +0200 | [diff] [blame] | 509 | } |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 510 | |
| 511 | static struct platform_driver mxc_gpio_driver = { |
| 512 | .driver = { |
| 513 | .name = "gpio-mxc", |
| 514 | .owner = THIS_MODULE, |
Shawn Guo | 8937cb6 | 2011-07-07 00:37:43 +0800 | [diff] [blame] | 515 | .of_match_table = mxc_gpio_dt_ids, |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 516 | }, |
| 517 | .probe = mxc_gpio_probe, |
Shawn Guo | e7fc6ae | 2011-07-07 00:37:41 +0800 | [diff] [blame] | 518 | .id_table = mxc_gpio_devtype, |
Shawn Guo | b78d8e5 | 2011-06-06 00:07:55 +0800 | [diff] [blame] | 519 | }; |
| 520 | |
| 521 | static int __init gpio_mxc_init(void) |
| 522 | { |
| 523 | return platform_driver_register(&mxc_gpio_driver); |
| 524 | } |
| 525 | postcore_initcall(gpio_mxc_init); |
| 526 | |
| 527 | MODULE_AUTHOR("Freescale Semiconductor, " |
| 528 | "Daniel Mack <danielncaiaq.de>, " |
| 529 | "Juergen Beisert <kernel@pengutronix.de>"); |
| 530 | MODULE_DESCRIPTION("Freescale MXC GPIO"); |
| 531 | MODULE_LICENSE("GPL"); |