2 * Driver for Aeroflex Gaisler GRGPIO General Purpose I/O cores.
4 * 2013 (c) Aeroflex Gaisler AB
6 * This driver supports the GRGPIO GPIO core available in the GRLIB VHDL
9 * Full documentation of the GRGPIO core can be found here:
10 * http://www.gaisler.com/products/grlib/grip.pdf
12 * See "Documentation/devicetree/bindings/gpio/gpio-grgpio.txt" for
13 * information on open firmware properties.
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.
20 * Contributors: Andreas Larsson <andreas@gaisler.com>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/spinlock.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>
34 #include <linux/gpio/driver.h>
35 #include <linux/interrupt.h>
36 #include <linux/irq.h>
37 #include <linux/irqdomain.h>
38 #include <linux/bitops.h>
40 #define GRGPIO_MAX_NGPIO 32
42 #define GRGPIO_DATA 0x00
43 #define GRGPIO_OUTPUT 0x04
44 #define GRGPIO_DIR 0x08
45 #define GRGPIO_IMASK 0x0c
46 #define GRGPIO_IPOL 0x10
47 #define GRGPIO_IEDGE 0x14
48 #define GRGPIO_BYPASS 0x18
49 #define GRGPIO_IMAP_BASE 0x20
51 /* Structure for an irq of the core - called an underlying irq */
53 u8 refcnt; /* Reference counter to manage requesting/freeing of uirq */
54 u8 uirq; /* Underlying irq of the gpio driver */
58 * Structure for an irq of a gpio line handed out by this driver. The index is
59 * used to map to the corresponding underlying irq.
62 s8 index; /* Index into struct grgpio_priv's uirqs, or -1 */
63 u8 irq; /* irq for the gpio line */
71 u32 imask; /* irq mask shadow register */
74 * The grgpio core can have multiple "underlying" irqs. The gpio lines
75 * can be mapped to any one or none of these underlying irqs
76 * independently of each other. This driver sets up an irq domain and
77 * hands out separate irqs to each gpio line
79 struct irq_domain *domain;
82 * This array contains information on each underlying irq, each
83 * irq of the grgpio core itself.
85 struct grgpio_uirq uirqs[GRGPIO_MAX_NGPIO];
88 * This array contains information for each gpio line on the irqs
89 * obtains from this driver. An index value of -1 for a certain gpio
90 * line indicates that the line has no irq. Otherwise the index connects
91 * the irq to the underlying irq by pointing into the uirqs array.
93 struct grgpio_lirq lirqs[GRGPIO_MAX_NGPIO];
96 static void grgpio_set_imask(struct grgpio_priv *priv, unsigned int offset,
99 struct gpio_chip *gc = &priv->gc;
102 priv->imask |= BIT(offset);
104 priv->imask &= ~BIT(offset);
105 gc->write_reg(priv->regs + GRGPIO_IMASK, priv->imask);
108 static int grgpio_to_irq(struct gpio_chip *gc, unsigned offset)
110 struct grgpio_priv *priv = gpiochip_get_data(gc);
112 if (offset >= gc->ngpio)
115 if (priv->lirqs[offset].index < 0)
118 return irq_create_mapping(priv->domain, offset);
121 /* -------------------- IRQ chip functions -------------------- */
123 static int grgpio_irq_set_type(struct irq_data *d, unsigned int type)
125 struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
127 u32 mask = BIT(d->hwirq);
134 case IRQ_TYPE_LEVEL_LOW:
138 case IRQ_TYPE_LEVEL_HIGH:
142 case IRQ_TYPE_EDGE_FALLING:
146 case IRQ_TYPE_EDGE_RISING:
154 spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
156 ipol = priv->gc.read_reg(priv->regs + GRGPIO_IPOL) & ~mask;
157 iedge = priv->gc.read_reg(priv->regs + GRGPIO_IEDGE) & ~mask;
159 priv->gc.write_reg(priv->regs + GRGPIO_IPOL, ipol | pol);
160 priv->gc.write_reg(priv->regs + GRGPIO_IEDGE, iedge | edge);
162 spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
167 static void grgpio_irq_mask(struct irq_data *d)
169 struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
170 int offset = d->hwirq;
173 spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
175 grgpio_set_imask(priv, offset, 0);
177 spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
180 static void grgpio_irq_unmask(struct irq_data *d)
182 struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
183 int offset = d->hwirq;
186 spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
188 grgpio_set_imask(priv, offset, 1);
190 spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
193 static struct irq_chip grgpio_irq_chip = {
195 .irq_mask = grgpio_irq_mask,
196 .irq_unmask = grgpio_irq_unmask,
197 .irq_set_type = grgpio_irq_set_type,
200 static irqreturn_t grgpio_irq_handler(int irq, void *dev)
202 struct grgpio_priv *priv = dev;
203 int ngpio = priv->gc.ngpio;
208 spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
211 * For each gpio line, call its interrupt handler if it its underlying
212 * irq matches the current irq that is handled.
214 for (i = 0; i < ngpio; i++) {
215 struct grgpio_lirq *lirq = &priv->lirqs[i];
217 if (priv->imask & BIT(i) && lirq->index >= 0 &&
218 priv->uirqs[lirq->index].uirq == irq) {
219 generic_handle_irq(lirq->irq);
224 spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
227 dev_warn(priv->dev, "No gpio line matched irq %d\n", irq);
233 * This function will be called as a consequence of the call to
234 * irq_create_mapping in grgpio_to_irq
236 static int grgpio_irq_map(struct irq_domain *d, unsigned int irq,
237 irq_hw_number_t hwirq)
239 struct grgpio_priv *priv = d->host_data;
240 struct grgpio_lirq *lirq;
241 struct grgpio_uirq *uirq;
249 lirq = &priv->lirqs[offset];
253 dev_dbg(priv->dev, "Mapping irq %d for gpio line %d\n",
256 spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
258 /* Request underlying irq if not already requested */
260 uirq = &priv->uirqs[lirq->index];
261 if (uirq->refcnt == 0) {
262 ret = request_irq(uirq->uirq, grgpio_irq_handler, 0,
263 dev_name(priv->dev), priv);
266 "Could not request underlying irq %d\n",
269 spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
276 spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
279 irq_set_chip_data(irq, priv);
280 irq_set_chip_and_handler(irq, &grgpio_irq_chip,
282 irq_set_noprobe(irq);
287 static void grgpio_irq_unmap(struct irq_domain *d, unsigned int irq)
289 struct grgpio_priv *priv = d->host_data;
291 struct grgpio_lirq *lirq;
292 struct grgpio_uirq *uirq;
294 int ngpio = priv->gc.ngpio;
297 irq_set_chip_and_handler(irq, NULL, NULL);
298 irq_set_chip_data(irq, NULL);
300 spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
302 /* Free underlying irq if last user unmapped */
304 for (i = 0; i < ngpio; i++) {
305 lirq = &priv->lirqs[i];
306 if (lirq->irq == irq) {
307 grgpio_set_imask(priv, i, 0);
316 uirq = &priv->uirqs[lirq->index];
318 if (uirq->refcnt == 0)
319 free_irq(uirq->uirq, priv);
322 spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
325 static const struct irq_domain_ops grgpio_irq_domain_ops = {
326 .map = grgpio_irq_map,
327 .unmap = grgpio_irq_unmap,
330 /* ------------------------------------------------------------ */
332 static int grgpio_probe(struct platform_device *ofdev)
334 struct device_node *np = ofdev->dev.of_node;
336 struct gpio_chip *gc;
337 struct grgpio_priv *priv;
338 struct resource *res;
345 priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL);
349 res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
350 regs = devm_ioremap_resource(&ofdev->dev, res);
352 return PTR_ERR(regs);
355 err = bgpio_init(gc, &ofdev->dev, 4, regs + GRGPIO_DATA,
356 regs + GRGPIO_OUTPUT, NULL, regs + GRGPIO_DIR, NULL,
357 BGPIOF_BIG_ENDIAN_BYTE_ORDER);
359 dev_err(&ofdev->dev, "bgpio_init() failed\n");
364 priv->imask = gc->read_reg(regs + GRGPIO_IMASK);
365 priv->dev = &ofdev->dev;
368 gc->owner = THIS_MODULE;
369 gc->to_irq = grgpio_to_irq;
370 gc->label = devm_kasprintf(&ofdev->dev, GFP_KERNEL, "%pOF", np);
373 err = of_property_read_u32(np, "nbits", &prop);
374 if (err || prop <= 0 || prop > GRGPIO_MAX_NGPIO) {
375 gc->ngpio = GRGPIO_MAX_NGPIO;
377 "No or invalid nbits property: assume %d\n", gc->ngpio);
383 * The irqmap contains the index values indicating which underlying irq,
384 * if anyone, is connected to that line
386 irqmap = (s32 *)of_get_property(np, "irqmap", &size);
388 if (size < gc->ngpio) {
390 "irqmap shorter than ngpio (%d < %d)\n",
395 priv->domain = irq_domain_add_linear(np, gc->ngpio,
396 &grgpio_irq_domain_ops,
399 dev_err(&ofdev->dev, "Could not add irq domain\n");
403 for (i = 0; i < gc->ngpio; i++) {
404 struct grgpio_lirq *lirq;
407 lirq = &priv->lirqs[i];
408 lirq->index = irqmap[i];
413 ret = platform_get_irq(ofdev, lirq->index);
416 * Continue without irq functionality for that
420 "Failed to get irq for offset %d\n", i);
423 priv->uirqs[lirq->index].uirq = ret;
427 platform_set_drvdata(ofdev, priv);
429 err = gpiochip_add_data(gc, priv);
431 dev_err(&ofdev->dev, "Could not add gpiochip\n");
433 irq_domain_remove(priv->domain);
437 dev_info(&ofdev->dev, "regs=0x%p, base=%d, ngpio=%d, irqs=%s\n",
438 priv->regs, gc->base, gc->ngpio, priv->domain ? "on" : "off");
443 static int grgpio_remove(struct platform_device *ofdev)
445 struct grgpio_priv *priv = platform_get_drvdata(ofdev);
450 spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
453 for (i = 0; i < GRGPIO_MAX_NGPIO; i++) {
454 if (priv->uirqs[i].refcnt != 0) {
461 gpiochip_remove(&priv->gc);
464 irq_domain_remove(priv->domain);
467 spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
472 static const struct of_device_id grgpio_match[] = {
473 {.name = "GAISLER_GPIO"},
478 MODULE_DEVICE_TABLE(of, grgpio_match);
480 static struct platform_driver grgpio_driver = {
483 .of_match_table = grgpio_match,
485 .probe = grgpio_probe,
486 .remove = grgpio_remove,
488 module_platform_driver(grgpio_driver);
490 MODULE_AUTHOR("Aeroflex Gaisler AB.");
491 MODULE_DESCRIPTION("Driver for Aeroflex Gaisler GRGPIO");
492 MODULE_LICENSE("GPL");