Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Aeroflex Gaisler GRGPIO General Purpose I/O cores. |
| 3 | * |
| 4 | * 2013 (c) Aeroflex Gaisler AB |
| 5 | * |
| 6 | * This driver supports the GRGPIO GPIO core available in the GRLIB VHDL |
| 7 | * IP core library. |
| 8 | * |
| 9 | * Full documentation of the GRGPIO core can be found here: |
| 10 | * http://www.gaisler.com/products/grlib/grip.pdf |
| 11 | * |
| 12 | * See "Documentation/devicetree/bindings/gpio/gpio-grgpio.txt" for |
| 13 | * information on open firmware properties. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify it |
| 16 | * under the terms of the GNU General Public License as published by the |
| 17 | * Free Software Foundation; either version 2 of the License, or (at your |
| 18 | * option) any later version. |
| 19 | * |
| 20 | * Contributors: Andreas Larsson <andreas@gaisler.com> |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/of.h> |
| 29 | #include <linux/of_gpio.h> |
| 30 | #include <linux/of_platform.h> |
| 31 | #include <linux/gpio.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/err.h> |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 34 | #include <linux/gpio/driver.h> |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 35 | #include <linux/interrupt.h> |
| 36 | #include <linux/irq.h> |
| 37 | #include <linux/irqdomain.h> |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 38 | |
| 39 | #define GRGPIO_MAX_NGPIO 32 |
| 40 | |
| 41 | #define GRGPIO_DATA 0x00 |
| 42 | #define GRGPIO_OUTPUT 0x04 |
| 43 | #define GRGPIO_DIR 0x08 |
| 44 | #define GRGPIO_IMASK 0x0c |
| 45 | #define GRGPIO_IPOL 0x10 |
| 46 | #define GRGPIO_IEDGE 0x14 |
| 47 | #define GRGPIO_BYPASS 0x18 |
| 48 | #define GRGPIO_IMAP_BASE 0x20 |
| 49 | |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 50 | /* Structure for an irq of the core - called an underlying irq */ |
| 51 | struct grgpio_uirq { |
| 52 | u8 refcnt; /* Reference counter to manage requesting/freeing of uirq */ |
| 53 | u8 uirq; /* Underlying irq of the gpio driver */ |
| 54 | }; |
| 55 | |
| 56 | /* |
| 57 | * Structure for an irq of a gpio line handed out by this driver. The index is |
| 58 | * used to map to the corresponding underlying irq. |
| 59 | */ |
| 60 | struct grgpio_lirq { |
| 61 | s8 index; /* Index into struct grgpio_priv's uirqs, or -1 */ |
| 62 | u8 irq; /* irq for the gpio line */ |
| 63 | }; |
| 64 | |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 65 | struct grgpio_priv { |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 66 | struct gpio_chip gc; |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 67 | void __iomem *regs; |
| 68 | struct device *dev; |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 69 | |
| 70 | u32 imask; /* irq mask shadow register */ |
| 71 | |
| 72 | /* |
| 73 | * The grgpio core can have multiple "underlying" irqs. The gpio lines |
| 74 | * can be mapped to any one or none of these underlying irqs |
| 75 | * independently of each other. This driver sets up an irq domain and |
| 76 | * hands out separate irqs to each gpio line |
| 77 | */ |
| 78 | struct irq_domain *domain; |
| 79 | |
| 80 | /* |
| 81 | * This array contains information on each underlying irq, each |
| 82 | * irq of the grgpio core itself. |
| 83 | */ |
| 84 | struct grgpio_uirq uirqs[GRGPIO_MAX_NGPIO]; |
| 85 | |
| 86 | /* |
| 87 | * This array contains information for each gpio line on the irqs |
| 88 | * obtains from this driver. An index value of -1 for a certain gpio |
| 89 | * line indicates that the line has no irq. Otherwise the index connects |
| 90 | * the irq to the underlying irq by pointing into the uirqs array. |
| 91 | */ |
| 92 | struct grgpio_lirq lirqs[GRGPIO_MAX_NGPIO]; |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 93 | }; |
| 94 | |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 95 | static void grgpio_set_imask(struct grgpio_priv *priv, unsigned int offset, |
| 96 | int val) |
| 97 | { |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 98 | struct gpio_chip *gc = &priv->gc; |
| 99 | unsigned long mask = gc->pin2mask(gc, offset); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 100 | |
| 101 | if (val) |
| 102 | priv->imask |= mask; |
| 103 | else |
| 104 | priv->imask &= ~mask; |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 105 | gc->write_reg(priv->regs + GRGPIO_IMASK, priv->imask); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static int grgpio_to_irq(struct gpio_chip *gc, unsigned offset) |
| 109 | { |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 110 | struct grgpio_priv *priv = gpiochip_get_data(gc); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 111 | |
Dan Carpenter | d3c2155 | 2014-12-17 02:53:59 +0300 | [diff] [blame] | 112 | if (offset >= gc->ngpio) |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 113 | return -ENXIO; |
| 114 | |
| 115 | if (priv->lirqs[offset].index < 0) |
| 116 | return -ENXIO; |
| 117 | |
| 118 | return irq_create_mapping(priv->domain, offset); |
| 119 | } |
| 120 | |
| 121 | /* -------------------- IRQ chip functions -------------------- */ |
| 122 | |
| 123 | static int grgpio_irq_set_type(struct irq_data *d, unsigned int type) |
| 124 | { |
| 125 | struct grgpio_priv *priv = irq_data_get_irq_chip_data(d); |
| 126 | unsigned long flags; |
| 127 | u32 mask = BIT(d->hwirq); |
| 128 | u32 ipol; |
| 129 | u32 iedge; |
| 130 | u32 pol; |
| 131 | u32 edge; |
| 132 | |
| 133 | switch (type) { |
| 134 | case IRQ_TYPE_LEVEL_LOW: |
| 135 | pol = 0; |
| 136 | edge = 0; |
| 137 | break; |
| 138 | case IRQ_TYPE_LEVEL_HIGH: |
| 139 | pol = mask; |
| 140 | edge = 0; |
| 141 | break; |
| 142 | case IRQ_TYPE_EDGE_FALLING: |
| 143 | pol = 0; |
| 144 | edge = mask; |
| 145 | break; |
| 146 | case IRQ_TYPE_EDGE_RISING: |
| 147 | pol = mask; |
| 148 | edge = mask; |
| 149 | break; |
| 150 | default: |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 154 | spin_lock_irqsave(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 155 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 156 | ipol = priv->gc.read_reg(priv->regs + GRGPIO_IPOL) & ~mask; |
| 157 | iedge = priv->gc.read_reg(priv->regs + GRGPIO_IEDGE) & ~mask; |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 158 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 159 | priv->gc.write_reg(priv->regs + GRGPIO_IPOL, ipol | pol); |
| 160 | priv->gc.write_reg(priv->regs + GRGPIO_IEDGE, iedge | edge); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 161 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 162 | spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static void grgpio_irq_mask(struct irq_data *d) |
| 168 | { |
| 169 | struct grgpio_priv *priv = irq_data_get_irq_chip_data(d); |
| 170 | int offset = d->hwirq; |
Alexandre Courbot | 7fa2593 | 2015-08-17 17:23:52 +0900 | [diff] [blame] | 171 | unsigned long flags; |
| 172 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 173 | spin_lock_irqsave(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 174 | |
| 175 | grgpio_set_imask(priv, offset, 0); |
Alexandre Courbot | 7fa2593 | 2015-08-17 17:23:52 +0900 | [diff] [blame] | 176 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 177 | spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void grgpio_irq_unmask(struct irq_data *d) |
| 181 | { |
| 182 | struct grgpio_priv *priv = irq_data_get_irq_chip_data(d); |
| 183 | int offset = d->hwirq; |
Alexandre Courbot | 7fa2593 | 2015-08-17 17:23:52 +0900 | [diff] [blame] | 184 | unsigned long flags; |
| 185 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 186 | spin_lock_irqsave(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 187 | |
| 188 | grgpio_set_imask(priv, offset, 1); |
Alexandre Courbot | 7fa2593 | 2015-08-17 17:23:52 +0900 | [diff] [blame] | 189 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 190 | spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static struct irq_chip grgpio_irq_chip = { |
| 194 | .name = "grgpio", |
| 195 | .irq_mask = grgpio_irq_mask, |
| 196 | .irq_unmask = grgpio_irq_unmask, |
| 197 | .irq_set_type = grgpio_irq_set_type, |
| 198 | }; |
| 199 | |
| 200 | static irqreturn_t grgpio_irq_handler(int irq, void *dev) |
| 201 | { |
| 202 | struct grgpio_priv *priv = dev; |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 203 | int ngpio = priv->gc.ngpio; |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 204 | unsigned long flags; |
| 205 | int i; |
| 206 | int match = 0; |
| 207 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 208 | spin_lock_irqsave(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * For each gpio line, call its interrupt handler if it its underlying |
| 212 | * irq matches the current irq that is handled. |
| 213 | */ |
| 214 | for (i = 0; i < ngpio; i++) { |
| 215 | struct grgpio_lirq *lirq = &priv->lirqs[i]; |
| 216 | |
| 217 | if (priv->imask & BIT(i) && lirq->index >= 0 && |
| 218 | priv->uirqs[lirq->index].uirq == irq) { |
| 219 | generic_handle_irq(lirq->irq); |
| 220 | match = 1; |
| 221 | } |
| 222 | } |
| 223 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 224 | spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 225 | |
| 226 | if (!match) |
| 227 | dev_warn(priv->dev, "No gpio line matched irq %d\n", irq); |
| 228 | |
| 229 | return IRQ_HANDLED; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * This function will be called as a consequence of the call to |
| 234 | * irq_create_mapping in grgpio_to_irq |
| 235 | */ |
Sachin Kamat | 61e3884 | 2013-06-18 17:07:03 +0530 | [diff] [blame] | 236 | static int grgpio_irq_map(struct irq_domain *d, unsigned int irq, |
| 237 | irq_hw_number_t hwirq) |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 238 | { |
| 239 | struct grgpio_priv *priv = d->host_data; |
| 240 | struct grgpio_lirq *lirq; |
| 241 | struct grgpio_uirq *uirq; |
| 242 | unsigned long flags; |
| 243 | int offset = hwirq; |
| 244 | int ret = 0; |
| 245 | |
| 246 | if (!priv) |
| 247 | return -EINVAL; |
| 248 | |
| 249 | lirq = &priv->lirqs[offset]; |
| 250 | if (lirq->index < 0) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | dev_dbg(priv->dev, "Mapping irq %d for gpio line %d\n", |
| 254 | irq, offset); |
| 255 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 256 | spin_lock_irqsave(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 257 | |
| 258 | /* Request underlying irq if not already requested */ |
| 259 | lirq->irq = irq; |
| 260 | uirq = &priv->uirqs[lirq->index]; |
| 261 | if (uirq->refcnt == 0) { |
Jia-Ju Bai | fd0faee | 2019-12-18 21:26:05 +0800 | [diff] [blame] | 262 | spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 263 | ret = request_irq(uirq->uirq, grgpio_irq_handler, 0, |
| 264 | dev_name(priv->dev), priv); |
| 265 | if (ret) { |
| 266 | dev_err(priv->dev, |
| 267 | "Could not request underlying irq %d\n", |
| 268 | uirq->uirq); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 269 | return ret; |
| 270 | } |
Jia-Ju Bai | fd0faee | 2019-12-18 21:26:05 +0800 | [diff] [blame] | 271 | spin_lock_irqsave(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 272 | } |
| 273 | uirq->refcnt++; |
| 274 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 275 | spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 276 | |
| 277 | /* Setup irq */ |
| 278 | irq_set_chip_data(irq, priv); |
| 279 | irq_set_chip_and_handler(irq, &grgpio_irq_chip, |
| 280 | handle_simple_irq); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 281 | irq_set_noprobe(irq); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
Sachin Kamat | 61e3884 | 2013-06-18 17:07:03 +0530 | [diff] [blame] | 286 | static void grgpio_irq_unmap(struct irq_domain *d, unsigned int irq) |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 287 | { |
| 288 | struct grgpio_priv *priv = d->host_data; |
| 289 | int index; |
| 290 | struct grgpio_lirq *lirq; |
| 291 | struct grgpio_uirq *uirq; |
| 292 | unsigned long flags; |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 293 | int ngpio = priv->gc.ngpio; |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 294 | int i; |
| 295 | |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 296 | irq_set_chip_and_handler(irq, NULL, NULL); |
| 297 | irq_set_chip_data(irq, NULL); |
| 298 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 299 | spin_lock_irqsave(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 300 | |
| 301 | /* Free underlying irq if last user unmapped */ |
| 302 | index = -1; |
| 303 | for (i = 0; i < ngpio; i++) { |
| 304 | lirq = &priv->lirqs[i]; |
| 305 | if (lirq->irq == irq) { |
| 306 | grgpio_set_imask(priv, i, 0); |
| 307 | lirq->irq = 0; |
| 308 | index = lirq->index; |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | WARN_ON(index < 0); |
| 313 | |
| 314 | if (index >= 0) { |
| 315 | uirq = &priv->uirqs[lirq->index]; |
| 316 | uirq->refcnt--; |
Jia-Ju Bai | fd0faee | 2019-12-18 21:26:05 +0800 | [diff] [blame] | 317 | if (uirq->refcnt == 0) { |
| 318 | spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 319 | free_irq(uirq->uirq, priv); |
Jia-Ju Bai | fd0faee | 2019-12-18 21:26:05 +0800 | [diff] [blame] | 320 | return; |
| 321 | } |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 324 | spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Krzysztof Kozlowski | 0b354dc | 2015-04-27 21:54:07 +0900 | [diff] [blame] | 327 | static const struct irq_domain_ops grgpio_irq_domain_ops = { |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 328 | .map = grgpio_irq_map, |
| 329 | .unmap = grgpio_irq_unmap, |
| 330 | }; |
| 331 | |
| 332 | /* ------------------------------------------------------------ */ |
| 333 | |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 334 | static int grgpio_probe(struct platform_device *ofdev) |
| 335 | { |
| 336 | struct device_node *np = ofdev->dev.of_node; |
| 337 | void __iomem *regs; |
| 338 | struct gpio_chip *gc; |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 339 | struct grgpio_priv *priv; |
| 340 | struct resource *res; |
| 341 | int err; |
| 342 | u32 prop; |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 343 | s32 *irqmap; |
| 344 | int size; |
| 345 | int i; |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 346 | |
| 347 | priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL); |
| 348 | if (!priv) |
| 349 | return -ENOMEM; |
| 350 | |
| 351 | res = platform_get_resource(ofdev, IORESOURCE_MEM, 0); |
| 352 | regs = devm_ioremap_resource(&ofdev->dev, res); |
| 353 | if (IS_ERR(regs)) |
| 354 | return PTR_ERR(regs); |
| 355 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 356 | gc = &priv->gc; |
| 357 | err = bgpio_init(gc, &ofdev->dev, 4, regs + GRGPIO_DATA, |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 358 | regs + GRGPIO_OUTPUT, NULL, regs + GRGPIO_DIR, NULL, |
| 359 | BGPIOF_BIG_ENDIAN_BYTE_ORDER); |
| 360 | if (err) { |
| 361 | dev_err(&ofdev->dev, "bgpio_init() failed\n"); |
| 362 | return err; |
| 363 | } |
| 364 | |
| 365 | priv->regs = regs; |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 366 | priv->imask = gc->read_reg(regs + GRGPIO_IMASK); |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 367 | priv->dev = &ofdev->dev; |
| 368 | |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 369 | gc->of_node = np; |
| 370 | gc->owner = THIS_MODULE; |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 371 | gc->to_irq = grgpio_to_irq; |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 372 | gc->label = np->full_name; |
| 373 | gc->base = -1; |
| 374 | |
| 375 | err = of_property_read_u32(np, "nbits", &prop); |
| 376 | if (err || prop <= 0 || prop > GRGPIO_MAX_NGPIO) { |
| 377 | gc->ngpio = GRGPIO_MAX_NGPIO; |
| 378 | dev_dbg(&ofdev->dev, |
| 379 | "No or invalid nbits property: assume %d\n", gc->ngpio); |
| 380 | } else { |
| 381 | gc->ngpio = prop; |
| 382 | } |
| 383 | |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 384 | /* |
| 385 | * The irqmap contains the index values indicating which underlying irq, |
| 386 | * if anyone, is connected to that line |
| 387 | */ |
| 388 | irqmap = (s32 *)of_get_property(np, "irqmap", &size); |
| 389 | if (irqmap) { |
| 390 | if (size < gc->ngpio) { |
| 391 | dev_err(&ofdev->dev, |
| 392 | "irqmap shorter than ngpio (%d < %d)\n", |
| 393 | size, gc->ngpio); |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | |
| 397 | priv->domain = irq_domain_add_linear(np, gc->ngpio, |
| 398 | &grgpio_irq_domain_ops, |
| 399 | priv); |
| 400 | if (!priv->domain) { |
| 401 | dev_err(&ofdev->dev, "Could not add irq domain\n"); |
| 402 | return -EINVAL; |
| 403 | } |
| 404 | |
| 405 | for (i = 0; i < gc->ngpio; i++) { |
| 406 | struct grgpio_lirq *lirq; |
| 407 | int ret; |
| 408 | |
| 409 | lirq = &priv->lirqs[i]; |
| 410 | lirq->index = irqmap[i]; |
| 411 | |
| 412 | if (lirq->index < 0) |
| 413 | continue; |
| 414 | |
| 415 | ret = platform_get_irq(ofdev, lirq->index); |
| 416 | if (ret <= 0) { |
| 417 | /* |
| 418 | * Continue without irq functionality for that |
| 419 | * gpio line |
| 420 | */ |
| 421 | dev_err(priv->dev, |
| 422 | "Failed to get irq for offset %d\n", i); |
| 423 | continue; |
| 424 | } |
| 425 | priv->uirqs[lirq->index].uirq = ret; |
| 426 | } |
| 427 | } |
| 428 | |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 429 | platform_set_drvdata(ofdev, priv); |
| 430 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 431 | err = gpiochip_add_data(gc, priv); |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 432 | if (err) { |
| 433 | dev_err(&ofdev->dev, "Could not add gpiochip\n"); |
Axel Lin | 879828c | 2014-12-20 22:47:07 +0800 | [diff] [blame] | 434 | if (priv->domain) |
| 435 | irq_domain_remove(priv->domain); |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 436 | return err; |
| 437 | } |
| 438 | |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 439 | dev_info(&ofdev->dev, "regs=0x%p, base=%d, ngpio=%d, irqs=%s\n", |
| 440 | priv->regs, gc->base, gc->ngpio, priv->domain ? "on" : "off"); |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static int grgpio_remove(struct platform_device *ofdev) |
| 446 | { |
| 447 | struct grgpio_priv *priv = platform_get_drvdata(ofdev); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 448 | unsigned long flags; |
| 449 | int i; |
| 450 | int ret = 0; |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 451 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 452 | spin_lock_irqsave(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 453 | |
| 454 | if (priv->domain) { |
| 455 | for (i = 0; i < GRGPIO_MAX_NGPIO; i++) { |
| 456 | if (priv->uirqs[i].refcnt != 0) { |
| 457 | ret = -EBUSY; |
| 458 | goto out; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 463 | gpiochip_remove(&priv->gc); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 464 | |
| 465 | if (priv->domain) |
| 466 | irq_domain_remove(priv->domain); |
| 467 | |
| 468 | out: |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 469 | spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags); |
Andreas Larsson | 08ffb22 | 2013-04-17 14:36:51 +0200 | [diff] [blame] | 470 | |
| 471 | return ret; |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Jingoo Han | f77b644 | 2014-05-07 18:03:30 +0900 | [diff] [blame] | 474 | static const struct of_device_id grgpio_match[] = { |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 475 | {.name = "GAISLER_GPIO"}, |
| 476 | {.name = "01_01a"}, |
| 477 | {}, |
| 478 | }; |
| 479 | |
| 480 | MODULE_DEVICE_TABLE(of, grgpio_match); |
| 481 | |
| 482 | static struct platform_driver grgpio_driver = { |
| 483 | .driver = { |
| 484 | .name = "grgpio", |
Andreas Larsson | ddb27f3 | 2013-04-17 14:36:50 +0200 | [diff] [blame] | 485 | .of_match_table = grgpio_match, |
| 486 | }, |
| 487 | .probe = grgpio_probe, |
| 488 | .remove = grgpio_remove, |
| 489 | }; |
| 490 | module_platform_driver(grgpio_driver); |
| 491 | |
| 492 | MODULE_AUTHOR("Aeroflex Gaisler AB."); |
| 493 | MODULE_DESCRIPTION("Driver for Aeroflex Gaisler GRGPIO"); |
| 494 | MODULE_LICENSE("GPL"); |