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