xref: /linux/drivers/gpio/gpio-grgpio.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
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  *
16  * Contributors: Andreas Larsson <andreas@gaisler.com>
17  */
18 
19 #include <linux/bitops.h>
20 #include <linux/err.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/gpio/generic.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/io.h>
26 #include <linux/irq.h>
27 #include <linux/irqdomain.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/of.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
34 #include <linux/string_choices.h>
35 
36 #define GRGPIO_MAX_NGPIO 32
37 
38 #define GRGPIO_DATA		0x00
39 #define GRGPIO_OUTPUT		0x04
40 #define GRGPIO_DIR		0x08
41 #define GRGPIO_IMASK		0x0c
42 #define GRGPIO_IPOL		0x10
43 #define GRGPIO_IEDGE		0x14
44 #define GRGPIO_BYPASS		0x18
45 #define GRGPIO_IMAP_BASE	0x20
46 
47 /* Structure for an irq of the core - called an underlying irq */
48 struct grgpio_uirq {
49 	u8 refcnt; /* Reference counter to manage requesting/freeing of uirq */
50 	u8 uirq; /* Underlying irq of the gpio driver */
51 };
52 
53 /*
54  * Structure for an irq of a gpio line handed out by this driver. The index is
55  * used to map to the corresponding underlying irq.
56  */
57 struct grgpio_lirq {
58 	s8 index; /* Index into struct grgpio_priv's uirqs, or -1 */
59 	u8 irq; /* irq for the gpio line */
60 };
61 
62 struct grgpio_priv {
63 	struct gpio_generic_chip chip;
64 	void __iomem *regs;
65 	struct device *dev;
66 
67 	u32 imask; /* irq mask shadow register */
68 
69 	/*
70 	 * The grgpio core can have multiple "underlying" irqs. The gpio lines
71 	 * can be mapped to any one or none of these underlying irqs
72 	 * independently of each other. This driver sets up an irq domain and
73 	 * hands out separate irqs to each gpio line
74 	 */
75 	struct irq_domain *domain;
76 
77 	/*
78 	 * This array contains information on each underlying irq, each
79 	 * irq of the grgpio core itself.
80 	 */
81 	struct grgpio_uirq uirqs[GRGPIO_MAX_NGPIO];
82 
83 	/*
84 	 * This array contains information for each gpio line on the irqs
85 	 * obtains from this driver. An index value of -1 for a certain gpio
86 	 * line indicates that the line has no irq. Otherwise the index connects
87 	 * the irq to the underlying irq by pointing into the uirqs array.
88 	 */
89 	struct grgpio_lirq lirqs[GRGPIO_MAX_NGPIO];
90 };
91 
92 static void grgpio_set_imask(struct grgpio_priv *priv, unsigned int offset,
93 			     int val)
94 {
95 	if (val)
96 		priv->imask |= BIT(offset);
97 	else
98 		priv->imask &= ~BIT(offset);
99 
100 	gpio_generic_write_reg(&priv->chip, priv->regs + GRGPIO_IMASK, priv->imask);
101 }
102 
103 static int grgpio_to_irq(struct gpio_chip *gc, unsigned offset)
104 {
105 	struct grgpio_priv *priv = gpiochip_get_data(gc);
106 
107 	if (offset >= gc->ngpio)
108 		return -ENXIO;
109 
110 	if (priv->lirqs[offset].index < 0)
111 		return -ENXIO;
112 
113 	return irq_create_mapping(priv->domain, offset);
114 }
115 
116 /* -------------------- IRQ chip functions -------------------- */
117 
118 static int grgpio_irq_set_type(struct irq_data *d, unsigned int type)
119 {
120 	struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
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 
148 	guard(gpio_generic_lock_irqsave)(&priv->chip);
149 
150 	ipol = gpio_generic_read_reg(&priv->chip, priv->regs + GRGPIO_IPOL) & ~mask;
151 	iedge = gpio_generic_read_reg(&priv->chip, priv->regs + GRGPIO_IEDGE) & ~mask;
152 
153 	gpio_generic_write_reg(&priv->chip, priv->regs + GRGPIO_IPOL, ipol | pol);
154 	gpio_generic_write_reg(&priv->chip, priv->regs + GRGPIO_IEDGE, iedge | edge);
155 
156 	return 0;
157 }
158 
159 static void grgpio_irq_mask(struct irq_data *d)
160 {
161 	struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
162 	int offset = d->hwirq;
163 
164 	scoped_guard(gpio_generic_lock_irqsave, &priv->chip)
165 		grgpio_set_imask(priv, offset, 0);
166 
167 	gpiochip_disable_irq(&priv->chip.gc, d->hwirq);
168 }
169 
170 static void grgpio_irq_unmask(struct irq_data *d)
171 {
172 	struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
173 	int offset = d->hwirq;
174 
175 	gpiochip_enable_irq(&priv->chip.gc, d->hwirq);
176 
177 	guard(gpio_generic_lock_irqsave)(&priv->chip);
178 
179 	grgpio_set_imask(priv, offset, 1);
180 }
181 
182 static const struct irq_chip grgpio_irq_chip = {
183 	.name			= "grgpio",
184 	.irq_mask		= grgpio_irq_mask,
185 	.irq_unmask		= grgpio_irq_unmask,
186 	.irq_set_type		= grgpio_irq_set_type,
187 	.flags = IRQCHIP_IMMUTABLE,
188 	GPIOCHIP_IRQ_RESOURCE_HELPERS,
189 };
190 
191 static irqreturn_t grgpio_irq_handler(int irq, void *dev)
192 {
193 	struct grgpio_priv *priv = dev;
194 	int ngpio = priv->chip.gc.ngpio;
195 	int i;
196 	int match = 0;
197 
198 	guard(gpio_generic_lock_irqsave)(&priv->chip);
199 
200 	/*
201 	 * For each gpio line, call its interrupt handler if it its underlying
202 	 * irq matches the current irq that is handled.
203 	 */
204 	for (i = 0; i < ngpio; i++) {
205 		struct grgpio_lirq *lirq = &priv->lirqs[i];
206 
207 		if (priv->imask & BIT(i) && lirq->index >= 0 &&
208 		    priv->uirqs[lirq->index].uirq == irq) {
209 			generic_handle_irq(lirq->irq);
210 			match = 1;
211 		}
212 	}
213 
214 	if (!match)
215 		dev_warn(priv->dev, "No gpio line matched irq %d\n", irq);
216 
217 	return IRQ_HANDLED;
218 }
219 
220 /*
221  * This function will be called as a consequence of the call to
222  * irq_create_mapping in grgpio_to_irq
223  */
224 static int grgpio_irq_map(struct irq_domain *d, unsigned int irq,
225 			  irq_hw_number_t hwirq)
226 {
227 	struct grgpio_priv *priv = d->host_data;
228 	struct grgpio_lirq *lirq;
229 	struct grgpio_uirq *uirq;
230 	unsigned long flags;
231 	int offset = hwirq;
232 	int ret = 0;
233 
234 	if (!priv)
235 		return -EINVAL;
236 
237 	lirq = &priv->lirqs[offset];
238 	if (lirq->index < 0)
239 		return -EINVAL;
240 
241 	dev_dbg(priv->dev, "Mapping irq %d for gpio line %d\n",
242 		irq, offset);
243 
244 	gpio_generic_chip_lock_irqsave(&priv->chip, flags);
245 
246 	/* Request underlying irq if not already requested */
247 	lirq->irq = irq;
248 	uirq = &priv->uirqs[lirq->index];
249 	if (uirq->refcnt == 0) {
250 		/*
251 		 * FIXME: This is not how locking works at all, you can't just
252 		 * release the lock for a moment to do something that can't
253 		 * sleep...
254 		 */
255 		gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
256 		ret = request_irq(uirq->uirq, grgpio_irq_handler, 0,
257 				  dev_name(priv->dev), priv);
258 		if (ret) {
259 			dev_err(priv->dev,
260 				"Could not request underlying irq %d\n",
261 				uirq->uirq);
262 			return ret;
263 		}
264 		gpio_generic_chip_lock_irqsave(&priv->chip, flags);
265 	}
266 	uirq->refcnt++;
267 
268 	gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
269 
270 	/* Setup irq  */
271 	irq_set_chip_data(irq, priv);
272 	irq_set_chip_and_handler(irq, &grgpio_irq_chip,
273 				 handle_simple_irq);
274 	irq_set_noprobe(irq);
275 
276 	return ret;
277 }
278 
279 static void grgpio_irq_unmap(struct irq_domain *d, unsigned int irq)
280 {
281 	struct grgpio_priv *priv = d->host_data;
282 	int index;
283 	struct grgpio_lirq *lirq;
284 	struct grgpio_uirq *uirq;
285 	unsigned long flags;
286 	int ngpio = priv->chip.gc.ngpio;
287 	int i;
288 
289 	irq_set_chip_and_handler(irq, NULL, NULL);
290 	irq_set_chip_data(irq, NULL);
291 
292 	gpio_generic_chip_lock_irqsave(&priv->chip, flags);
293 
294 	/* Free underlying irq if last user unmapped */
295 	index = -1;
296 	for (i = 0; i < ngpio; i++) {
297 		lirq = &priv->lirqs[i];
298 		if (lirq->irq == irq) {
299 			grgpio_set_imask(priv, i, 0);
300 			lirq->irq = 0;
301 			index = lirq->index;
302 			break;
303 		}
304 	}
305 	WARN_ON(index < 0);
306 
307 	if (index >= 0) {
308 		uirq = &priv->uirqs[lirq->index];
309 		uirq->refcnt--;
310 		if (uirq->refcnt == 0) {
311 			gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
312 			free_irq(uirq->uirq, priv);
313 			return;
314 		}
315 	}
316 
317 	gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
318 }
319 
320 static void grgpio_irq_domain_remove(void *data)
321 {
322 	struct irq_domain *domain = data;
323 
324 	irq_domain_remove(domain);
325 }
326 
327 static const struct irq_domain_ops grgpio_irq_domain_ops = {
328 	.map	= grgpio_irq_map,
329 	.unmap	= grgpio_irq_unmap,
330 };
331 
332 /* ------------------------------------------------------------ */
333 
334 static int grgpio_probe(struct platform_device *ofdev)
335 {
336 	struct device_node *np = ofdev->dev.of_node;
337 	struct gpio_generic_chip_config config;
338 	struct device *dev = &ofdev->dev;
339 	void  __iomem *regs;
340 	struct gpio_chip *gc;
341 	struct grgpio_priv *priv;
342 	int err;
343 	u32 prop;
344 	s32 *irqmap;
345 	int size;
346 	int i;
347 
348 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
349 	if (!priv)
350 		return -ENOMEM;
351 
352 	regs = devm_platform_ioremap_resource(ofdev, 0);
353 	if (IS_ERR(regs))
354 		return PTR_ERR(regs);
355 
356 	config = (struct gpio_generic_chip_config) {
357 		.dev = dev,
358 		.sz = 4,
359 		.dat = regs + GRGPIO_DATA,
360 		.set = regs + GRGPIO_OUTPUT,
361 		.dirout = regs + GRGPIO_DIR,
362 		.flags = GPIO_GENERIC_BIG_ENDIAN_BYTE_ORDER,
363 	};
364 
365 	gc = &priv->chip.gc;
366 	err = gpio_generic_chip_init(&priv->chip, &config);
367 	if (err) {
368 		dev_err(dev, "failed to initialize the generic GPIO chip\n");
369 		return err;
370 	}
371 
372 	priv->regs = regs;
373 	priv->imask = gpio_generic_read_reg(&priv->chip, regs + GRGPIO_IMASK);
374 	priv->dev = dev;
375 
376 	gc->owner = THIS_MODULE;
377 	gc->to_irq = grgpio_to_irq;
378 	gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", np);
379 	if (!gc->label)
380 		return -ENOMEM;
381 
382 	gc->base = -1;
383 
384 	err = of_property_read_u32(np, "nbits", &prop);
385 	if (err || prop <= 0 || prop > GRGPIO_MAX_NGPIO) {
386 		gc->ngpio = GRGPIO_MAX_NGPIO;
387 		dev_dbg(dev, "No or invalid nbits property: assume %d\n",
388 			gc->ngpio);
389 	} else {
390 		gc->ngpio = prop;
391 	}
392 
393 	/*
394 	 * The irqmap contains the index values indicating which underlying irq,
395 	 * if anyone, is connected to that line
396 	 */
397 	irqmap = (s32 *)of_get_property(np, "irqmap", &size);
398 	if (irqmap) {
399 		if (size < gc->ngpio) {
400 			dev_err(dev,
401 				"irqmap shorter than ngpio (%d < %d)\n",
402 				size, gc->ngpio);
403 			return -EINVAL;
404 		}
405 
406 		priv->domain = irq_domain_create_linear(dev_fwnode(&ofdev->dev), gc->ngpio,
407 							&grgpio_irq_domain_ops, priv);
408 		if (!priv->domain) {
409 			dev_err(dev, "Could not add irq domain\n");
410 			return -EINVAL;
411 		}
412 
413 		err = devm_add_action_or_reset(dev, grgpio_irq_domain_remove,
414 					       priv->domain);
415 		if (err)
416 			return err;
417 
418 		for (i = 0; i < gc->ngpio; i++) {
419 			struct grgpio_lirq *lirq;
420 			int ret;
421 
422 			lirq = &priv->lirqs[i];
423 			lirq->index = irqmap[i];
424 
425 			if (lirq->index < 0)
426 				continue;
427 
428 			ret = platform_get_irq(ofdev, lirq->index);
429 			if (ret <= 0) {
430 				/*
431 				 * Continue without irq functionality for that
432 				 * gpio line
433 				 */
434 				continue;
435 			}
436 			priv->uirqs[lirq->index].uirq = ret;
437 		}
438 	}
439 
440 	err = devm_gpiochip_add_data(dev, gc, priv);
441 	if (err) {
442 		dev_err(dev, "Could not add gpiochip\n");
443 		return err;
444 	}
445 
446 	dev_info(dev, "regs=0x%p, base=%d, ngpio=%d, irqs=%s\n",
447 		 priv->regs, gc->base, gc->ngpio, str_on_off(priv->domain));
448 
449 	return 0;
450 }
451 
452 static const struct of_device_id grgpio_match[] = {
453 	{.name = "GAISLER_GPIO"},
454 	{.name = "01_01a"},
455 	{},
456 };
457 
458 MODULE_DEVICE_TABLE(of, grgpio_match);
459 
460 static struct platform_driver grgpio_driver = {
461 	.driver = {
462 		.name = "grgpio",
463 		.of_match_table = grgpio_match,
464 	},
465 	.probe = grgpio_probe,
466 };
467 module_platform_driver(grgpio_driver);
468 
469 MODULE_AUTHOR("Aeroflex Gaisler AB.");
470 MODULE_DESCRIPTION("Driver for Aeroflex Gaisler GRGPIO");
471 MODULE_LICENSE("GPL");
472