1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Aeroflex Gaisler GRGPIO General Purpose I/O cores.
5 * 2013 (c) Aeroflex Gaisler AB
7 * This driver supports the GRGPIO GPIO core available in the GRLIB VHDL
10 * Full documentation of the GRGPIO core can be found here:
11 * http://www.gaisler.com/products/grlib/grip.pdf
13 * See "Documentation/devicetree/bindings/gpio/gpio-grgpio.txt" for
14 * information on open firmware properties.
16 * Contributors: Andreas Larsson <andreas@gaisler.com>
19 #include <linux/bitops.h>
20 #include <linux/err.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/irqdomain.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
34 #define GRGPIO_MAX_NGPIO 32
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
45 /* Structure for an irq of the core - called an underlying irq */
47 u8 refcnt
; /* Reference counter to manage requesting/freeing of uirq */
48 u8 uirq
; /* Underlying irq of the gpio driver */
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.
56 s8 index
; /* Index into struct grgpio_priv's uirqs, or -1 */
57 u8 irq
; /* irq for the gpio line */
65 u32 imask
; /* irq mask shadow register */
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
73 struct irq_domain
*domain
;
76 * This array contains information on each underlying irq, each
77 * irq of the grgpio core itself.
79 struct grgpio_uirq uirqs
[GRGPIO_MAX_NGPIO
];
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.
87 struct grgpio_lirq lirqs
[GRGPIO_MAX_NGPIO
];
90 static void grgpio_set_imask(struct grgpio_priv
*priv
, unsigned int offset
,
93 struct gpio_chip
*gc
= &priv
->gc
;
96 priv
->imask
|= BIT(offset
);
98 priv
->imask
&= ~BIT(offset
);
99 gc
->write_reg(priv
->regs
+ GRGPIO_IMASK
, priv
->imask
);
102 static int grgpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
104 struct grgpio_priv
*priv
= gpiochip_get_data(gc
);
106 if (offset
>= gc
->ngpio
)
109 if (priv
->lirqs
[offset
].index
< 0)
112 return irq_create_mapping(priv
->domain
, offset
);
115 /* -------------------- IRQ chip functions -------------------- */
117 static int grgpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
119 struct grgpio_priv
*priv
= irq_data_get_irq_chip_data(d
);
121 u32 mask
= BIT(d
->hwirq
);
128 case IRQ_TYPE_LEVEL_LOW
:
132 case IRQ_TYPE_LEVEL_HIGH
:
136 case IRQ_TYPE_EDGE_FALLING
:
140 case IRQ_TYPE_EDGE_RISING
:
148 raw_spin_lock_irqsave(&priv
->gc
.bgpio_lock
, flags
);
150 ipol
= priv
->gc
.read_reg(priv
->regs
+ GRGPIO_IPOL
) & ~mask
;
151 iedge
= priv
->gc
.read_reg(priv
->regs
+ GRGPIO_IEDGE
) & ~mask
;
153 priv
->gc
.write_reg(priv
->regs
+ GRGPIO_IPOL
, ipol
| pol
);
154 priv
->gc
.write_reg(priv
->regs
+ GRGPIO_IEDGE
, iedge
| edge
);
156 raw_spin_unlock_irqrestore(&priv
->gc
.bgpio_lock
, flags
);
161 static void grgpio_irq_mask(struct irq_data
*d
)
163 struct grgpio_priv
*priv
= irq_data_get_irq_chip_data(d
);
164 int offset
= d
->hwirq
;
167 raw_spin_lock_irqsave(&priv
->gc
.bgpio_lock
, flags
);
169 grgpio_set_imask(priv
, offset
, 0);
171 raw_spin_unlock_irqrestore(&priv
->gc
.bgpio_lock
, flags
);
174 static void grgpio_irq_unmask(struct irq_data
*d
)
176 struct grgpio_priv
*priv
= irq_data_get_irq_chip_data(d
);
177 int offset
= d
->hwirq
;
180 raw_spin_lock_irqsave(&priv
->gc
.bgpio_lock
, flags
);
182 grgpio_set_imask(priv
, offset
, 1);
184 raw_spin_unlock_irqrestore(&priv
->gc
.bgpio_lock
, flags
);
187 static struct irq_chip grgpio_irq_chip
= {
189 .irq_mask
= grgpio_irq_mask
,
190 .irq_unmask
= grgpio_irq_unmask
,
191 .irq_set_type
= grgpio_irq_set_type
,
194 static irqreturn_t
grgpio_irq_handler(int irq
, void *dev
)
196 struct grgpio_priv
*priv
= dev
;
197 int ngpio
= priv
->gc
.ngpio
;
202 raw_spin_lock_irqsave(&priv
->gc
.bgpio_lock
, flags
);
205 * For each gpio line, call its interrupt handler if it its underlying
206 * irq matches the current irq that is handled.
208 for (i
= 0; i
< ngpio
; i
++) {
209 struct grgpio_lirq
*lirq
= &priv
->lirqs
[i
];
211 if (priv
->imask
& BIT(i
) && lirq
->index
>= 0 &&
212 priv
->uirqs
[lirq
->index
].uirq
== irq
) {
213 generic_handle_irq(lirq
->irq
);
218 raw_spin_unlock_irqrestore(&priv
->gc
.bgpio_lock
, flags
);
221 dev_warn(priv
->dev
, "No gpio line matched irq %d\n", irq
);
227 * This function will be called as a consequence of the call to
228 * irq_create_mapping in grgpio_to_irq
230 static int grgpio_irq_map(struct irq_domain
*d
, unsigned int irq
,
231 irq_hw_number_t hwirq
)
233 struct grgpio_priv
*priv
= d
->host_data
;
234 struct grgpio_lirq
*lirq
;
235 struct grgpio_uirq
*uirq
;
243 lirq
= &priv
->lirqs
[offset
];
247 dev_dbg(priv
->dev
, "Mapping irq %d for gpio line %d\n",
250 raw_spin_lock_irqsave(&priv
->gc
.bgpio_lock
, flags
);
252 /* Request underlying irq if not already requested */
254 uirq
= &priv
->uirqs
[lirq
->index
];
255 if (uirq
->refcnt
== 0) {
256 raw_spin_unlock_irqrestore(&priv
->gc
.bgpio_lock
, flags
);
257 ret
= request_irq(uirq
->uirq
, grgpio_irq_handler
, 0,
258 dev_name(priv
->dev
), priv
);
261 "Could not request underlying irq %d\n",
265 raw_spin_lock_irqsave(&priv
->gc
.bgpio_lock
, flags
);
269 raw_spin_unlock_irqrestore(&priv
->gc
.bgpio_lock
, flags
);
272 irq_set_chip_data(irq
, priv
);
273 irq_set_chip_and_handler(irq
, &grgpio_irq_chip
,
275 irq_set_noprobe(irq
);
280 static void grgpio_irq_unmap(struct irq_domain
*d
, unsigned int irq
)
282 struct grgpio_priv
*priv
= d
->host_data
;
284 struct grgpio_lirq
*lirq
;
285 struct grgpio_uirq
*uirq
;
287 int ngpio
= priv
->gc
.ngpio
;
290 irq_set_chip_and_handler(irq
, NULL
, NULL
);
291 irq_set_chip_data(irq
, NULL
);
293 raw_spin_lock_irqsave(&priv
->gc
.bgpio_lock
, flags
);
295 /* Free underlying irq if last user unmapped */
297 for (i
= 0; i
< ngpio
; i
++) {
298 lirq
= &priv
->lirqs
[i
];
299 if (lirq
->irq
== irq
) {
300 grgpio_set_imask(priv
, i
, 0);
309 uirq
= &priv
->uirqs
[lirq
->index
];
311 if (uirq
->refcnt
== 0) {
312 raw_spin_unlock_irqrestore(&priv
->gc
.bgpio_lock
, flags
);
313 free_irq(uirq
->uirq
, priv
);
318 raw_spin_unlock_irqrestore(&priv
->gc
.bgpio_lock
, flags
);
321 static void grgpio_irq_domain_remove(void *data
)
323 struct irq_domain
*domain
= data
;
325 irq_domain_remove(domain
);
328 static const struct irq_domain_ops grgpio_irq_domain_ops
= {
329 .map
= grgpio_irq_map
,
330 .unmap
= grgpio_irq_unmap
,
333 /* ------------------------------------------------------------ */
335 static int grgpio_probe(struct platform_device
*ofdev
)
337 struct device_node
*np
= ofdev
->dev
.of_node
;
338 struct device
*dev
= &ofdev
->dev
;
340 struct gpio_chip
*gc
;
341 struct grgpio_priv
*priv
;
348 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
352 regs
= devm_platform_ioremap_resource(ofdev
, 0);
354 return PTR_ERR(regs
);
357 err
= bgpio_init(gc
, dev
, 4, regs
+ GRGPIO_DATA
,
358 regs
+ GRGPIO_OUTPUT
, NULL
, regs
+ GRGPIO_DIR
, NULL
,
359 BGPIOF_BIG_ENDIAN_BYTE_ORDER
);
361 dev_err(dev
, "bgpio_init() failed\n");
366 priv
->imask
= gc
->read_reg(regs
+ GRGPIO_IMASK
);
369 gc
->owner
= THIS_MODULE
;
370 gc
->to_irq
= grgpio_to_irq
;
371 gc
->label
= devm_kasprintf(dev
, GFP_KERNEL
, "%pOF", np
);
377 err
= of_property_read_u32(np
, "nbits", &prop
);
378 if (err
|| prop
<= 0 || prop
> GRGPIO_MAX_NGPIO
) {
379 gc
->ngpio
= GRGPIO_MAX_NGPIO
;
380 dev_dbg(dev
, "No or invalid nbits property: assume %d\n",
387 * The irqmap contains the index values indicating which underlying irq,
388 * if anyone, is connected to that line
390 irqmap
= (s32
*)of_get_property(np
, "irqmap", &size
);
392 if (size
< gc
->ngpio
) {
394 "irqmap shorter than ngpio (%d < %d)\n",
399 priv
->domain
= irq_domain_add_linear(np
, gc
->ngpio
,
400 &grgpio_irq_domain_ops
,
403 dev_err(dev
, "Could not add irq domain\n");
407 err
= devm_add_action_or_reset(dev
, grgpio_irq_domain_remove
,
412 for (i
= 0; i
< gc
->ngpio
; i
++) {
413 struct grgpio_lirq
*lirq
;
416 lirq
= &priv
->lirqs
[i
];
417 lirq
->index
= irqmap
[i
];
422 ret
= platform_get_irq(ofdev
, lirq
->index
);
425 * Continue without irq functionality for that
430 priv
->uirqs
[lirq
->index
].uirq
= ret
;
434 err
= devm_gpiochip_add_data(dev
, gc
, priv
);
436 dev_err(dev
, "Could not add gpiochip\n");
440 dev_info(dev
, "regs=0x%p, base=%d, ngpio=%d, irqs=%s\n",
441 priv
->regs
, gc
->base
, gc
->ngpio
, priv
->domain
? "on" : "off");
446 static const struct of_device_id grgpio_match
[] = {
447 {.name
= "GAISLER_GPIO"},
452 MODULE_DEVICE_TABLE(of
, grgpio_match
);
454 static struct platform_driver grgpio_driver
= {
457 .of_match_table
= grgpio_match
,
459 .probe
= grgpio_probe
,
461 module_platform_driver(grgpio_driver
);
463 MODULE_AUTHOR("Aeroflex Gaisler AB.");
464 MODULE_DESCRIPTION("Driver for Aeroflex Gaisler GRGPIO");
465 MODULE_LICENSE("GPL");