GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / gpio / gpio-grgpio.c
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_platform.h>
30 #include <linux/gpio/driver.h>
31 #include <linux/slab.h>
32 #include <linux/err.h>
33 #include <linux/gpio/driver.h>
34 #include <linux/interrupt.h>
35 #include <linux/irq.h>
36 #include <linux/irqdomain.h>
37 #include <linux/bitops.h>
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
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
65 struct grgpio_priv {
66         struct gpio_chip gc;
67         void __iomem *regs;
68         struct device *dev;
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];
93 };
94
95 static void grgpio_set_imask(struct grgpio_priv *priv, unsigned int offset,
96                              int val)
97 {
98         struct gpio_chip *gc = &priv->gc;
99
100         if (val)
101                 priv->imask |= BIT(offset);
102         else
103                 priv->imask &= ~BIT(offset);
104         gc->write_reg(priv->regs + GRGPIO_IMASK, priv->imask);
105 }
106
107 static int grgpio_to_irq(struct gpio_chip *gc, unsigned offset)
108 {
109         struct grgpio_priv *priv = gpiochip_get_data(gc);
110
111         if (offset >= gc->ngpio)
112                 return -ENXIO;
113
114         if (priv->lirqs[offset].index < 0)
115                 return -ENXIO;
116
117         return irq_create_mapping(priv->domain, offset);
118 }
119
120 /* -------------------- IRQ chip functions -------------------- */
121
122 static int grgpio_irq_set_type(struct irq_data *d, unsigned int type)
123 {
124         struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
125         unsigned long flags;
126         u32 mask = BIT(d->hwirq);
127         u32 ipol;
128         u32 iedge;
129         u32 pol;
130         u32 edge;
131
132         switch (type) {
133         case IRQ_TYPE_LEVEL_LOW:
134                 pol = 0;
135                 edge = 0;
136                 break;
137         case IRQ_TYPE_LEVEL_HIGH:
138                 pol = mask;
139                 edge = 0;
140                 break;
141         case IRQ_TYPE_EDGE_FALLING:
142                 pol = 0;
143                 edge = mask;
144                 break;
145         case IRQ_TYPE_EDGE_RISING:
146                 pol = mask;
147                 edge = mask;
148                 break;
149         default:
150                 return -EINVAL;
151         }
152
153         spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
154
155         ipol = priv->gc.read_reg(priv->regs + GRGPIO_IPOL) & ~mask;
156         iedge = priv->gc.read_reg(priv->regs + GRGPIO_IEDGE) & ~mask;
157
158         priv->gc.write_reg(priv->regs + GRGPIO_IPOL, ipol | pol);
159         priv->gc.write_reg(priv->regs + GRGPIO_IEDGE, iedge | edge);
160
161         spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
162
163         return 0;
164 }
165
166 static void grgpio_irq_mask(struct irq_data *d)
167 {
168         struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
169         int offset = d->hwirq;
170         unsigned long flags;
171
172         spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
173
174         grgpio_set_imask(priv, offset, 0);
175
176         spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
177 }
178
179 static void grgpio_irq_unmask(struct irq_data *d)
180 {
181         struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
182         int offset = d->hwirq;
183         unsigned long flags;
184
185         spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
186
187         grgpio_set_imask(priv, offset, 1);
188
189         spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
190 }
191
192 static struct irq_chip grgpio_irq_chip = {
193         .name                   = "grgpio",
194         .irq_mask               = grgpio_irq_mask,
195         .irq_unmask             = grgpio_irq_unmask,
196         .irq_set_type           = grgpio_irq_set_type,
197 };
198
199 static irqreturn_t grgpio_irq_handler(int irq, void *dev)
200 {
201         struct grgpio_priv *priv = dev;
202         int ngpio = priv->gc.ngpio;
203         unsigned long flags;
204         int i;
205         int match = 0;
206
207         spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
208
209         /*
210          * For each gpio line, call its interrupt handler if it its underlying
211          * irq matches the current irq that is handled.
212          */
213         for (i = 0; i < ngpio; i++) {
214                 struct grgpio_lirq *lirq = &priv->lirqs[i];
215
216                 if (priv->imask & BIT(i) && lirq->index >= 0 &&
217                     priv->uirqs[lirq->index].uirq == irq) {
218                         generic_handle_irq(lirq->irq);
219                         match = 1;
220                 }
221         }
222
223         spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
224
225         if (!match)
226                 dev_warn(priv->dev, "No gpio line matched irq %d\n", irq);
227
228         return IRQ_HANDLED;
229 }
230
231 /*
232  * This function will be called as a consequence of the call to
233  * irq_create_mapping in grgpio_to_irq
234  */
235 static int grgpio_irq_map(struct irq_domain *d, unsigned int irq,
236                           irq_hw_number_t hwirq)
237 {
238         struct grgpio_priv *priv = d->host_data;
239         struct grgpio_lirq *lirq;
240         struct grgpio_uirq *uirq;
241         unsigned long flags;
242         int offset = hwirq;
243         int ret = 0;
244
245         if (!priv)
246                 return -EINVAL;
247
248         lirq = &priv->lirqs[offset];
249         if (lirq->index < 0)
250                 return -EINVAL;
251
252         dev_dbg(priv->dev, "Mapping irq %d for gpio line %d\n",
253                 irq, offset);
254
255         spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
256
257         /* Request underlying irq if not already requested */
258         lirq->irq = irq;
259         uirq = &priv->uirqs[lirq->index];
260         if (uirq->refcnt == 0) {
261                 spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
262                 ret = request_irq(uirq->uirq, grgpio_irq_handler, 0,
263                                   dev_name(priv->dev), priv);
264                 if (ret) {
265                         dev_err(priv->dev,
266                                 "Could not request underlying irq %d\n",
267                                 uirq->uirq);
268                         return ret;
269                 }
270                 spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
271         }
272         uirq->refcnt++;
273
274         spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
275
276         /* Setup irq  */
277         irq_set_chip_data(irq, priv);
278         irq_set_chip_and_handler(irq, &grgpio_irq_chip,
279                                  handle_simple_irq);
280         irq_set_noprobe(irq);
281
282         return ret;
283 }
284
285 static void grgpio_irq_unmap(struct irq_domain *d, unsigned int irq)
286 {
287         struct grgpio_priv *priv = d->host_data;
288         int index;
289         struct grgpio_lirq *lirq;
290         struct grgpio_uirq *uirq;
291         unsigned long flags;
292         int ngpio = priv->gc.ngpio;
293         int i;
294
295         irq_set_chip_and_handler(irq, NULL, NULL);
296         irq_set_chip_data(irq, NULL);
297
298         spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
299
300         /* Free underlying irq if last user unmapped */
301         index = -1;
302         for (i = 0; i < ngpio; i++) {
303                 lirq = &priv->lirqs[i];
304                 if (lirq->irq == irq) {
305                         grgpio_set_imask(priv, i, 0);
306                         lirq->irq = 0;
307                         index = lirq->index;
308                         break;
309                 }
310         }
311         WARN_ON(index < 0);
312
313         if (index >= 0) {
314                 uirq = &priv->uirqs[lirq->index];
315                 uirq->refcnt--;
316                 if (uirq->refcnt == 0) {
317                         spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
318                         free_irq(uirq->uirq, priv);
319                         return;
320                 }
321         }
322
323         spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
324 }
325
326 static const struct irq_domain_ops grgpio_irq_domain_ops = {
327         .map    = grgpio_irq_map,
328         .unmap  = grgpio_irq_unmap,
329 };
330
331 /* ------------------------------------------------------------ */
332
333 static int grgpio_probe(struct platform_device *ofdev)
334 {
335         struct device_node *np = ofdev->dev.of_node;
336         void  __iomem *regs;
337         struct gpio_chip *gc;
338         struct grgpio_priv *priv;
339         struct resource *res;
340         int err;
341         u32 prop;
342         s32 *irqmap;
343         int size;
344         int i;
345
346         priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL);
347         if (!priv)
348                 return -ENOMEM;
349
350         res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
351         regs = devm_ioremap_resource(&ofdev->dev, res);
352         if (IS_ERR(regs))
353                 return PTR_ERR(regs);
354
355         gc = &priv->gc;
356         err = bgpio_init(gc, &ofdev->dev, 4, regs + GRGPIO_DATA,
357                          regs + GRGPIO_OUTPUT, NULL, regs + GRGPIO_DIR, NULL,
358                          BGPIOF_BIG_ENDIAN_BYTE_ORDER);
359         if (err) {
360                 dev_err(&ofdev->dev, "bgpio_init() failed\n");
361                 return err;
362         }
363
364         priv->regs = regs;
365         priv->imask = gc->read_reg(regs + GRGPIO_IMASK);
366         priv->dev = &ofdev->dev;
367
368         gc->of_node = np;
369         gc->owner = THIS_MODULE;
370         gc->to_irq = grgpio_to_irq;
371         gc->label = devm_kasprintf(&ofdev->dev, GFP_KERNEL, "%pOF", np);
372         gc->base = -1;
373
374         err = of_property_read_u32(np, "nbits", &prop);
375         if (err || prop <= 0 || prop > GRGPIO_MAX_NGPIO) {
376                 gc->ngpio = GRGPIO_MAX_NGPIO;
377                 dev_dbg(&ofdev->dev,
378                         "No or invalid nbits property: assume %d\n", gc->ngpio);
379         } else {
380                 gc->ngpio = prop;
381         }
382
383         /*
384          * The irqmap contains the index values indicating which underlying irq,
385          * if anyone, is connected to that line
386          */
387         irqmap = (s32 *)of_get_property(np, "irqmap", &size);
388         if (irqmap) {
389                 if (size < gc->ngpio) {
390                         dev_err(&ofdev->dev,
391                                 "irqmap shorter than ngpio (%d < %d)\n",
392                                 size, gc->ngpio);
393                         return -EINVAL;
394                 }
395
396                 priv->domain = irq_domain_add_linear(np, gc->ngpio,
397                                                      &grgpio_irq_domain_ops,
398                                                      priv);
399                 if (!priv->domain) {
400                         dev_err(&ofdev->dev, "Could not add irq domain\n");
401                         return -EINVAL;
402                 }
403
404                 for (i = 0; i < gc->ngpio; i++) {
405                         struct grgpio_lirq *lirq;
406                         int ret;
407
408                         lirq = &priv->lirqs[i];
409                         lirq->index = irqmap[i];
410
411                         if (lirq->index < 0)
412                                 continue;
413
414                         ret = platform_get_irq(ofdev, lirq->index);
415                         if (ret <= 0) {
416                                 /*
417                                  * Continue without irq functionality for that
418                                  * gpio line
419                                  */
420                                 dev_err(priv->dev,
421                                         "Failed to get irq for offset %d\n", i);
422                                 continue;
423                         }
424                         priv->uirqs[lirq->index].uirq = ret;
425                 }
426         }
427
428         platform_set_drvdata(ofdev, priv);
429
430         err = gpiochip_add_data(gc, priv);
431         if (err) {
432                 dev_err(&ofdev->dev, "Could not add gpiochip\n");
433                 if (priv->domain)
434                         irq_domain_remove(priv->domain);
435                 return err;
436         }
437
438         dev_info(&ofdev->dev, "regs=0x%p, base=%d, ngpio=%d, irqs=%s\n",
439                  priv->regs, gc->base, gc->ngpio, priv->domain ? "on" : "off");
440
441         return 0;
442 }
443
444 static int grgpio_remove(struct platform_device *ofdev)
445 {
446         struct grgpio_priv *priv = platform_get_drvdata(ofdev);
447         unsigned long flags;
448         int i;
449         int ret = 0;
450
451         spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
452
453         if (priv->domain) {
454                 for (i = 0; i < GRGPIO_MAX_NGPIO; i++) {
455                         if (priv->uirqs[i].refcnt != 0) {
456                                 ret = -EBUSY;
457                                 goto out;
458                         }
459                 }
460         }
461
462         gpiochip_remove(&priv->gc);
463
464         if (priv->domain)
465                 irq_domain_remove(priv->domain);
466
467 out:
468         spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
469
470         return ret;
471 }
472
473 static const struct of_device_id grgpio_match[] = {
474         {.name = "GAISLER_GPIO"},
475         {.name = "01_01a"},
476         {},
477 };
478
479 MODULE_DEVICE_TABLE(of, grgpio_match);
480
481 static struct platform_driver grgpio_driver = {
482         .driver = {
483                 .name = "grgpio",
484                 .of_match_table = grgpio_match,
485         },
486         .probe = grgpio_probe,
487         .remove = grgpio_remove,
488 };
489 module_platform_driver(grgpio_driver);
490
491 MODULE_AUTHOR("Aeroflex Gaisler AB.");
492 MODULE_DESCRIPTION("Driver for Aeroflex Gaisler GRGPIO");
493 MODULE_LICENSE("GPL");