Linus Walleij | 36e2add | 2018-09-07 22:04:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 2 | /* |
Paul Gortmaker | adaaf63 | 2016-08-22 12:48:33 -0400 | [diff] [blame] | 3 | * Freescale vf610 GPIO support through PORT and GPIO |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2014 Toradex AG. |
| 6 | * |
| 7 | * Author: Stefan Agner <stefan@agner.ch>. |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 8 | */ |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 9 | #include <linux/bitops.h> |
| 10 | #include <linux/err.h> |
Linus Walleij | 45e8296 | 2018-09-07 22:03:07 +0200 | [diff] [blame] | 11 | #include <linux/gpio/driver.h> |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/ioport.h> |
| 16 | #include <linux/irq.h> |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/of_device.h> |
| 20 | #include <linux/of_irq.h> |
| 21 | |
| 22 | #define VF610_GPIO_PER_PORT 32 |
| 23 | |
Dong Aisheng | 659d8a6 | 2017-08-01 22:04:17 +0800 | [diff] [blame] | 24 | struct fsl_gpio_soc_data { |
| 25 | /* SoCs has a Port Data Direction Register (PDDR) */ |
| 26 | bool have_paddr; |
| 27 | }; |
| 28 | |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 29 | struct vf610_gpio_port { |
| 30 | struct gpio_chip gc; |
| 31 | void __iomem *base; |
| 32 | void __iomem *gpio_base; |
Dong Aisheng | 659d8a6 | 2017-08-01 22:04:17 +0800 | [diff] [blame] | 33 | const struct fsl_gpio_soc_data *sdata; |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 34 | u8 irqc[VF610_GPIO_PER_PORT]; |
| 35 | int irq; |
| 36 | }; |
| 37 | |
| 38 | #define GPIO_PDOR 0x00 |
| 39 | #define GPIO_PSOR 0x04 |
| 40 | #define GPIO_PCOR 0x08 |
| 41 | #define GPIO_PTOR 0x0c |
| 42 | #define GPIO_PDIR 0x10 |
Dong Aisheng | 659d8a6 | 2017-08-01 22:04:17 +0800 | [diff] [blame] | 43 | #define GPIO_PDDR 0x14 |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 44 | |
| 45 | #define PORT_PCR(n) ((n) * 0x4) |
| 46 | #define PORT_PCR_IRQC_OFFSET 16 |
| 47 | |
| 48 | #define PORT_ISFR 0xa0 |
| 49 | #define PORT_DFER 0xc0 |
| 50 | #define PORT_DFCR 0xc4 |
| 51 | #define PORT_DFWR 0xc8 |
| 52 | |
| 53 | #define PORT_INT_OFF 0x0 |
| 54 | #define PORT_INT_LOGIC_ZERO 0x8 |
| 55 | #define PORT_INT_RISING_EDGE 0x9 |
| 56 | #define PORT_INT_FALLING_EDGE 0xa |
| 57 | #define PORT_INT_EITHER_EDGE 0xb |
| 58 | #define PORT_INT_LOGIC_ONE 0xc |
| 59 | |
Stefan Agner | fd96811 | 2015-08-21 15:56:42 -0700 | [diff] [blame] | 60 | static struct irq_chip vf610_gpio_irq_chip; |
| 61 | |
Dong Aisheng | 659d8a6 | 2017-08-01 22:04:17 +0800 | [diff] [blame] | 62 | static const struct fsl_gpio_soc_data imx_data = { |
| 63 | .have_paddr = true, |
| 64 | }; |
| 65 | |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 66 | static const struct of_device_id vf610_gpio_dt_ids[] = { |
Dong Aisheng | 659d8a6 | 2017-08-01 22:04:17 +0800 | [diff] [blame] | 67 | { .compatible = "fsl,vf610-gpio", .data = NULL, }, |
| 68 | { .compatible = "fsl,imx7ulp-gpio", .data = &imx_data, }, |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 69 | { /* sentinel */ } |
| 70 | }; |
| 71 | |
| 72 | static inline void vf610_gpio_writel(u32 val, void __iomem *reg) |
| 73 | { |
| 74 | writel_relaxed(val, reg); |
| 75 | } |
| 76 | |
| 77 | static inline u32 vf610_gpio_readl(void __iomem *reg) |
| 78 | { |
| 79 | return readl_relaxed(reg); |
| 80 | } |
| 81 | |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 82 | static int vf610_gpio_get(struct gpio_chip *gc, unsigned int gpio) |
| 83 | { |
Linus Walleij | 65389b4 | 2015-12-07 15:03:30 +0100 | [diff] [blame] | 84 | struct vf610_gpio_port *port = gpiochip_get_data(gc); |
Dong Aisheng | 659d8a6 | 2017-08-01 22:04:17 +0800 | [diff] [blame] | 85 | unsigned long mask = BIT(gpio); |
| 86 | void __iomem *addr; |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 87 | |
Dong Aisheng | 659d8a6 | 2017-08-01 22:04:17 +0800 | [diff] [blame] | 88 | if (port->sdata && port->sdata->have_paddr) { |
| 89 | mask &= vf610_gpio_readl(port->gpio_base + GPIO_PDDR); |
| 90 | addr = mask ? port->gpio_base + GPIO_PDOR : |
| 91 | port->gpio_base + GPIO_PDIR; |
| 92 | return !!(vf610_gpio_readl(addr) & BIT(gpio)); |
| 93 | } else { |
| 94 | return !!(vf610_gpio_readl(port->gpio_base + GPIO_PDIR) |
| 95 | & BIT(gpio)); |
| 96 | } |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void vf610_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val) |
| 100 | { |
Linus Walleij | 65389b4 | 2015-12-07 15:03:30 +0100 | [diff] [blame] | 101 | struct vf610_gpio_port *port = gpiochip_get_data(gc); |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 102 | unsigned long mask = BIT(gpio); |
| 103 | |
| 104 | if (val) |
| 105 | vf610_gpio_writel(mask, port->gpio_base + GPIO_PSOR); |
| 106 | else |
| 107 | vf610_gpio_writel(mask, port->gpio_base + GPIO_PCOR); |
| 108 | } |
| 109 | |
| 110 | static int vf610_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) |
| 111 | { |
Dong Aisheng | 659d8a6 | 2017-08-01 22:04:17 +0800 | [diff] [blame] | 112 | struct vf610_gpio_port *port = gpiochip_get_data(chip); |
| 113 | unsigned long mask = BIT(gpio); |
| 114 | u32 val; |
| 115 | |
| 116 | if (port->sdata && port->sdata->have_paddr) { |
| 117 | val = vf610_gpio_readl(port->gpio_base + GPIO_PDDR); |
| 118 | val &= ~mask; |
| 119 | vf610_gpio_writel(val, port->gpio_base + GPIO_PDDR); |
| 120 | } |
| 121 | |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 122 | return pinctrl_gpio_direction_input(chip->base + gpio); |
| 123 | } |
| 124 | |
| 125 | static int vf610_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, |
| 126 | int value) |
| 127 | { |
Dong Aisheng | 659d8a6 | 2017-08-01 22:04:17 +0800 | [diff] [blame] | 128 | struct vf610_gpio_port *port = gpiochip_get_data(chip); |
| 129 | unsigned long mask = BIT(gpio); |
| 130 | |
| 131 | if (port->sdata && port->sdata->have_paddr) |
| 132 | vf610_gpio_writel(mask, port->gpio_base + GPIO_PDDR); |
| 133 | |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 134 | vf610_gpio_set(chip, gpio, value); |
| 135 | |
| 136 | return pinctrl_gpio_direction_output(chip->base + gpio); |
| 137 | } |
| 138 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 139 | static void vf610_gpio_irq_handler(struct irq_desc *desc) |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 140 | { |
Linus Walleij | 2f93064 | 2015-08-27 14:13:46 +0200 | [diff] [blame] | 141 | struct vf610_gpio_port *port = |
Linus Walleij | 65389b4 | 2015-12-07 15:03:30 +0100 | [diff] [blame] | 142 | gpiochip_get_data(irq_desc_get_handler_data(desc)); |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 143 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 144 | int pin; |
| 145 | unsigned long irq_isfr; |
| 146 | |
| 147 | chained_irq_enter(chip, desc); |
| 148 | |
| 149 | irq_isfr = vf610_gpio_readl(port->base + PORT_ISFR); |
| 150 | |
| 151 | for_each_set_bit(pin, &irq_isfr, VF610_GPIO_PER_PORT) { |
| 152 | vf610_gpio_writel(BIT(pin), port->base + PORT_ISFR); |
| 153 | |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 154 | generic_handle_irq(irq_find_mapping(port->gc.irq.domain, pin)); |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | chained_irq_exit(chip, desc); |
| 158 | } |
| 159 | |
| 160 | static void vf610_gpio_irq_ack(struct irq_data *d) |
| 161 | { |
Linus Walleij | 2f93064 | 2015-08-27 14:13:46 +0200 | [diff] [blame] | 162 | struct vf610_gpio_port *port = |
Linus Walleij | 65389b4 | 2015-12-07 15:03:30 +0100 | [diff] [blame] | 163 | gpiochip_get_data(irq_data_get_irq_chip_data(d)); |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 164 | int gpio = d->hwirq; |
| 165 | |
| 166 | vf610_gpio_writel(BIT(gpio), port->base + PORT_ISFR); |
| 167 | } |
| 168 | |
| 169 | static int vf610_gpio_irq_set_type(struct irq_data *d, u32 type) |
| 170 | { |
Linus Walleij | 2f93064 | 2015-08-27 14:13:46 +0200 | [diff] [blame] | 171 | struct vf610_gpio_port *port = |
Linus Walleij | 65389b4 | 2015-12-07 15:03:30 +0100 | [diff] [blame] | 172 | gpiochip_get_data(irq_data_get_irq_chip_data(d)); |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 173 | u8 irqc; |
| 174 | |
| 175 | switch (type) { |
| 176 | case IRQ_TYPE_EDGE_RISING: |
| 177 | irqc = PORT_INT_RISING_EDGE; |
| 178 | break; |
| 179 | case IRQ_TYPE_EDGE_FALLING: |
| 180 | irqc = PORT_INT_FALLING_EDGE; |
| 181 | break; |
| 182 | case IRQ_TYPE_EDGE_BOTH: |
| 183 | irqc = PORT_INT_EITHER_EDGE; |
| 184 | break; |
| 185 | case IRQ_TYPE_LEVEL_LOW: |
| 186 | irqc = PORT_INT_LOGIC_ZERO; |
| 187 | break; |
| 188 | case IRQ_TYPE_LEVEL_HIGH: |
| 189 | irqc = PORT_INT_LOGIC_ONE; |
| 190 | break; |
| 191 | default: |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | |
| 195 | port->irqc[d->hwirq] = irqc; |
| 196 | |
Stefan Agner | fd96811 | 2015-08-21 15:56:42 -0700 | [diff] [blame] | 197 | if (type & IRQ_TYPE_LEVEL_MASK) |
Thomas Gleixner | a7147db | 2015-09-16 12:51:00 +0200 | [diff] [blame] | 198 | irq_set_handler_locked(d, handle_level_irq); |
Stefan Agner | fd96811 | 2015-08-21 15:56:42 -0700 | [diff] [blame] | 199 | else |
Thomas Gleixner | a7147db | 2015-09-16 12:51:00 +0200 | [diff] [blame] | 200 | irq_set_handler_locked(d, handle_edge_irq); |
Stefan Agner | fd96811 | 2015-08-21 15:56:42 -0700 | [diff] [blame] | 201 | |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static void vf610_gpio_irq_mask(struct irq_data *d) |
| 206 | { |
Linus Walleij | 2f93064 | 2015-08-27 14:13:46 +0200 | [diff] [blame] | 207 | struct vf610_gpio_port *port = |
Linus Walleij | 65389b4 | 2015-12-07 15:03:30 +0100 | [diff] [blame] | 208 | gpiochip_get_data(irq_data_get_irq_chip_data(d)); |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 209 | void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq); |
| 210 | |
| 211 | vf610_gpio_writel(0, pcr_base); |
| 212 | } |
| 213 | |
| 214 | static void vf610_gpio_irq_unmask(struct irq_data *d) |
| 215 | { |
Linus Walleij | 2f93064 | 2015-08-27 14:13:46 +0200 | [diff] [blame] | 216 | struct vf610_gpio_port *port = |
Linus Walleij | 65389b4 | 2015-12-07 15:03:30 +0100 | [diff] [blame] | 217 | gpiochip_get_data(irq_data_get_irq_chip_data(d)); |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 218 | void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq); |
| 219 | |
| 220 | vf610_gpio_writel(port->irqc[d->hwirq] << PORT_PCR_IRQC_OFFSET, |
| 221 | pcr_base); |
| 222 | } |
| 223 | |
| 224 | static int vf610_gpio_irq_set_wake(struct irq_data *d, u32 enable) |
| 225 | { |
Linus Walleij | 2f93064 | 2015-08-27 14:13:46 +0200 | [diff] [blame] | 226 | struct vf610_gpio_port *port = |
Linus Walleij | 65389b4 | 2015-12-07 15:03:30 +0100 | [diff] [blame] | 227 | gpiochip_get_data(irq_data_get_irq_chip_data(d)); |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 228 | |
| 229 | if (enable) |
| 230 | enable_irq_wake(port->irq); |
| 231 | else |
| 232 | disable_irq_wake(port->irq); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static struct irq_chip vf610_gpio_irq_chip = { |
| 238 | .name = "gpio-vf610", |
| 239 | .irq_ack = vf610_gpio_irq_ack, |
| 240 | .irq_mask = vf610_gpio_irq_mask, |
| 241 | .irq_unmask = vf610_gpio_irq_unmask, |
| 242 | .irq_set_type = vf610_gpio_irq_set_type, |
| 243 | .irq_set_wake = vf610_gpio_irq_set_wake, |
| 244 | }; |
| 245 | |
| 246 | static int vf610_gpio_probe(struct platform_device *pdev) |
| 247 | { |
| 248 | struct device *dev = &pdev->dev; |
| 249 | struct device_node *np = dev->of_node; |
| 250 | struct vf610_gpio_port *port; |
| 251 | struct resource *iores; |
| 252 | struct gpio_chip *gc; |
| 253 | int ret; |
| 254 | |
| 255 | port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL); |
| 256 | if (!port) |
| 257 | return -ENOMEM; |
| 258 | |
Thierry Reding | 23e577e | 2018-04-30 09:38:18 +0200 | [diff] [blame] | 259 | port->sdata = of_device_get_match_data(dev); |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 260 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 261 | port->base = devm_ioremap_resource(dev, iores); |
| 262 | if (IS_ERR(port->base)) |
| 263 | return PTR_ERR(port->base); |
| 264 | |
| 265 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 266 | port->gpio_base = devm_ioremap_resource(dev, iores); |
| 267 | if (IS_ERR(port->gpio_base)) |
| 268 | return PTR_ERR(port->gpio_base); |
| 269 | |
| 270 | port->irq = platform_get_irq(pdev, 0); |
| 271 | if (port->irq < 0) |
| 272 | return port->irq; |
| 273 | |
| 274 | gc = &port->gc; |
| 275 | gc->of_node = np; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 276 | gc->parent = dev; |
Axel Lin | d32efe3 | 2015-02-13 21:04:42 +0800 | [diff] [blame] | 277 | gc->label = "vf610-gpio"; |
| 278 | gc->ngpio = VF610_GPIO_PER_PORT; |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 279 | gc->base = of_alias_get_id(np, "gpio") * VF610_GPIO_PER_PORT; |
| 280 | |
Jonas Gorski | 203f0da | 2015-10-11 17:34:16 +0200 | [diff] [blame] | 281 | gc->request = gpiochip_generic_request; |
| 282 | gc->free = gpiochip_generic_free; |
Axel Lin | d32efe3 | 2015-02-13 21:04:42 +0800 | [diff] [blame] | 283 | gc->direction_input = vf610_gpio_direction_input; |
| 284 | gc->get = vf610_gpio_get; |
| 285 | gc->direction_output = vf610_gpio_direction_output; |
| 286 | gc->set = vf610_gpio_set; |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 287 | |
Linus Walleij | 65389b4 | 2015-12-07 15:03:30 +0100 | [diff] [blame] | 288 | ret = gpiochip_add_data(gc, port); |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 289 | if (ret < 0) |
| 290 | return ret; |
| 291 | |
| 292 | /* Clear the interrupt status register for all GPIO's */ |
| 293 | vf610_gpio_writel(~0, port->base + PORT_ISFR); |
| 294 | |
| 295 | ret = gpiochip_irqchip_add(gc, &vf610_gpio_irq_chip, 0, |
Stefan Agner | fd96811 | 2015-08-21 15:56:42 -0700 | [diff] [blame] | 296 | handle_edge_irq, IRQ_TYPE_NONE); |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 297 | if (ret) { |
| 298 | dev_err(dev, "failed to add irqchip\n"); |
| 299 | gpiochip_remove(gc); |
| 300 | return ret; |
| 301 | } |
| 302 | gpiochip_set_chained_irqchip(gc, &vf610_gpio_irq_chip, port->irq, |
| 303 | vf610_gpio_irq_handler); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static struct platform_driver vf610_gpio_driver = { |
| 309 | .driver = { |
| 310 | .name = "gpio-vf610", |
Stefan Agner | 7f2691a | 2014-10-16 21:47:58 +0200 | [diff] [blame] | 311 | .of_match_table = vf610_gpio_dt_ids, |
| 312 | }, |
| 313 | .probe = vf610_gpio_probe, |
| 314 | }; |
| 315 | |
Geliang Tang | df950da | 2016-11-23 22:47:48 +0800 | [diff] [blame] | 316 | builtin_platform_driver(vf610_gpio_driver); |