xref: /freebsd/sys/dev/gpio/gpiobus.c (revision 53b70c86)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/gpio.h>
36 #ifdef INTRNG
37 #include <sys/intr.h>
38 #endif
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/sbuf.h>
43 
44 #include <dev/gpio/gpiobusvar.h>
45 
46 #include "gpiobus_if.h"
47 
48 #undef GPIOBUS_DEBUG
49 #ifdef GPIOBUS_DEBUG
50 #define	dprintf printf
51 #else
52 #define	dprintf(x, arg...)
53 #endif
54 
55 static void gpiobus_print_pins(struct gpiobus_ivar *, struct sbuf *);
56 static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int);
57 static int gpiobus_probe(device_t);
58 static int gpiobus_attach(device_t);
59 static int gpiobus_detach(device_t);
60 static int gpiobus_suspend(device_t);
61 static int gpiobus_resume(device_t);
62 static void gpiobus_probe_nomatch(device_t, device_t);
63 static int gpiobus_print_child(device_t, device_t);
64 static int gpiobus_child_location(device_t, device_t, struct sbuf *);
65 static device_t gpiobus_add_child(device_t, u_int, const char *, int);
66 static void gpiobus_hinted_child(device_t, const char *, int);
67 
68 /*
69  * GPIOBUS interface
70  */
71 static int gpiobus_acquire_bus(device_t, device_t, int);
72 static void gpiobus_release_bus(device_t, device_t);
73 static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t);
74 static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*);
75 static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*);
76 static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int);
77 static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*);
78 static int gpiobus_pin_toggle(device_t, device_t, uint32_t);
79 
80 /*
81  * gpiobus_pin flags
82  *  The flags in struct gpiobus_pin are not related to the flags used by the
83  *  low-level controller driver in struct gpio_pin.  Currently, only pins
84  *  acquired via FDT data have gpiobus_pin.flags set, sourced from the flags in
85  *  the FDT properties.  In theory, these flags are defined per-platform.  In
86  *  practice they are always the flags from the dt-bindings/gpio/gpio.h file.
87  *  The only one of those flags we currently support is for handling active-low
88  *  pins, so we just define that flag here instead of including a GPL'd header.
89  */
90 #define	GPIO_ACTIVE_LOW 1
91 
92 /*
93  * XXX -> Move me to better place - gpio_subr.c?
94  * Also, this function must be changed when interrupt configuration
95  * data will be moved into struct resource.
96  */
97 #ifdef INTRNG
98 
99 struct resource *
100 gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags,
101     gpio_pin_t pin, uint32_t intr_mode)
102 {
103 	u_int irq;
104 	struct intr_map_data_gpio *gpio_data;
105 	struct resource *res;
106 
107 	gpio_data = (struct intr_map_data_gpio *)intr_alloc_map_data(
108 	    INTR_MAP_DATA_GPIO, sizeof(*gpio_data), M_WAITOK | M_ZERO);
109 	gpio_data->gpio_pin_num = pin->pin;
110 	gpio_data->gpio_pin_flags = pin->flags;
111 	gpio_data->gpio_intr_mode = intr_mode;
112 
113 	irq = intr_map_irq(pin->dev, 0, (struct intr_map_data *)gpio_data);
114 	res = bus_alloc_resource(consumer_dev, SYS_RES_IRQ, rid, irq, irq, 1,
115 	    alloc_flags);
116 	if (res == NULL) {
117 		intr_free_intr_map_data((struct intr_map_data *)gpio_data);
118 		return (NULL);
119 	}
120 	rman_set_virtual(res, gpio_data);
121 	return (res);
122 }
123 #else
124 struct resource *
125 gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags,
126     gpio_pin_t pin, uint32_t intr_mode)
127 {
128 
129 	return (NULL);
130 }
131 #endif
132 
133 int
134 gpio_check_flags(uint32_t caps, uint32_t flags)
135 {
136 
137 	/* Filter unwanted flags. */
138 	flags &= caps;
139 
140 	/* Cannot mix input/output together. */
141 	if (flags & GPIO_PIN_INPUT && flags & GPIO_PIN_OUTPUT)
142 		return (EINVAL);
143 	/* Cannot mix pull-up/pull-down together. */
144 	if (flags & GPIO_PIN_PULLUP && flags & GPIO_PIN_PULLDOWN)
145 		return (EINVAL);
146 	/* Cannot mix output and interrupt flags together */
147 	if (flags & GPIO_PIN_OUTPUT && flags & GPIO_INTR_MASK)
148 		return (EINVAL);
149 	/* Only one interrupt flag can be defined at once */
150 	if ((flags & GPIO_INTR_MASK) & ((flags & GPIO_INTR_MASK) - 1))
151 		return (EINVAL);
152 	/* The interrupt attached flag cannot be set */
153 	if (flags & GPIO_INTR_ATTACHED)
154 		return (EINVAL);
155 
156 	return (0);
157 }
158 
159 int
160 gpio_pin_get_by_bus_pinnum(device_t busdev, uint32_t pinnum, gpio_pin_t *ppin)
161 {
162 	gpio_pin_t pin;
163 	int err;
164 
165 	err = gpiobus_acquire_pin(busdev, pinnum);
166 	if (err != 0)
167 		return (EBUSY);
168 
169 	pin = malloc(sizeof(*pin), M_DEVBUF, M_WAITOK | M_ZERO);
170 
171 	pin->dev = device_get_parent(busdev);
172 	pin->pin = pinnum;
173 	pin->flags = 0;
174 
175 	*ppin = pin;
176 	return (0);
177 }
178 
179 int
180 gpio_pin_get_by_child_index(device_t childdev, uint32_t idx, gpio_pin_t *ppin)
181 {
182 	struct gpiobus_ivar *devi;
183 
184 	devi = GPIOBUS_IVAR(childdev);
185 	if (idx >= devi->npins)
186 		return (EINVAL);
187 
188 	return (gpio_pin_get_by_bus_pinnum(device_get_parent(childdev),
189 	    devi->pins[idx], ppin));
190 }
191 
192 int
193 gpio_pin_getcaps(gpio_pin_t pin, uint32_t *caps)
194 {
195 
196 	KASSERT(pin != NULL, ("GPIO pin is NULL."));
197 	KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
198 	return (GPIO_PIN_GETCAPS(pin->dev, pin->pin, caps));
199 }
200 
201 int
202 gpio_pin_is_active(gpio_pin_t pin, bool *active)
203 {
204 	int rv;
205 	uint32_t tmp;
206 
207 	KASSERT(pin != NULL, ("GPIO pin is NULL."));
208 	KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
209 	rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp);
210 	if (rv  != 0) {
211 		return (rv);
212 	}
213 
214 	if (pin->flags & GPIO_ACTIVE_LOW)
215 		*active = tmp == 0;
216 	else
217 		*active = tmp != 0;
218 	return (0);
219 }
220 
221 void
222 gpio_pin_release(gpio_pin_t gpio)
223 {
224 	device_t busdev;
225 
226 	if (gpio == NULL)
227 		return;
228 
229 	KASSERT(gpio->dev != NULL, ("GPIO pin device is NULL."));
230 
231 	busdev = GPIO_GET_BUS(gpio->dev);
232 	if (busdev != NULL)
233 		gpiobus_release_pin(busdev, gpio->pin);
234 
235 	free(gpio, M_DEVBUF);
236 }
237 
238 int
239 gpio_pin_set_active(gpio_pin_t pin, bool active)
240 {
241 	int rv;
242 	uint32_t tmp;
243 
244 	if (pin->flags & GPIO_ACTIVE_LOW)
245 		tmp = active ? 0 : 1;
246 	else
247 		tmp = active ? 1 : 0;
248 
249 	KASSERT(pin != NULL, ("GPIO pin is NULL."));
250 	KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
251 	rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp);
252 	return (rv);
253 }
254 
255 int
256 gpio_pin_setflags(gpio_pin_t pin, uint32_t flags)
257 {
258 	int rv;
259 
260 	KASSERT(pin != NULL, ("GPIO pin is NULL."));
261 	KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
262 
263 	rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
264 	return (rv);
265 }
266 
267 static void
268 gpiobus_print_pins(struct gpiobus_ivar *devi, struct sbuf *sb)
269 {
270 	int i, range_start, range_stop, need_coma;
271 
272 	if (devi->npins == 0)
273 		return;
274 
275 	need_coma = 0;
276 	range_start = range_stop = devi->pins[0];
277 	for (i = 1; i < devi->npins; i++) {
278 		if (devi->pins[i] != (range_stop + 1)) {
279 			if (need_coma)
280 				sbuf_cat(sb, ",");
281 			if (range_start != range_stop)
282 				sbuf_printf(sb, "%d-%d", range_start, range_stop);
283 			else
284 				sbuf_printf(sb, "%d", range_start);
285 			range_start = range_stop = devi->pins[i];
286 			need_coma = 1;
287 		}
288 		else
289 			range_stop++;
290 	}
291 
292 	if (need_coma)
293 		sbuf_cat(sb, ",");
294 	if (range_start != range_stop)
295 		sbuf_printf(sb, "%d-%d", range_start, range_stop);
296 	else
297 		sbuf_printf(sb, "%d", range_start);
298 }
299 
300 device_t
301 gpiobus_attach_bus(device_t dev)
302 {
303 	device_t busdev;
304 
305 	busdev = device_add_child(dev, "gpiobus", -1);
306 	if (busdev == NULL)
307 		return (NULL);
308 	if (device_add_child(dev, "gpioc", -1) == NULL) {
309 		device_delete_child(dev, busdev);
310 		return (NULL);
311 	}
312 #ifdef FDT
313 	ofw_gpiobus_register_provider(dev);
314 #endif
315 	bus_generic_attach(dev);
316 
317 	return (busdev);
318 }
319 
320 int
321 gpiobus_detach_bus(device_t dev)
322 {
323 	int err;
324 
325 #ifdef FDT
326 	ofw_gpiobus_unregister_provider(dev);
327 #endif
328 	err = bus_generic_detach(dev);
329 	if (err != 0)
330 		return (err);
331 
332 	return (device_delete_children(dev));
333 }
334 
335 int
336 gpiobus_init_softc(device_t dev)
337 {
338 	struct gpiobus_softc *sc;
339 
340 	sc = GPIOBUS_SOFTC(dev);
341 	sc->sc_busdev = dev;
342 	sc->sc_dev = device_get_parent(dev);
343 	sc->sc_intr_rman.rm_type = RMAN_ARRAY;
344 	sc->sc_intr_rman.rm_descr = "GPIO Interrupts";
345 	if (rman_init(&sc->sc_intr_rman) != 0 ||
346 	    rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0)
347 		panic("%s: failed to set up rman.", __func__);
348 
349 	if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0)
350 		return (ENXIO);
351 
352 	KASSERT(sc->sc_npins >= 0, ("GPIO device with no pins"));
353 
354 	/* Pins = GPIO_PIN_MAX() + 1 */
355 	sc->sc_npins++;
356 
357 	sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF,
358 	    M_NOWAIT | M_ZERO);
359 	if (sc->sc_pins == NULL)
360 		return (ENOMEM);
361 
362 	/* Initialize the bus lock. */
363 	GPIOBUS_LOCK_INIT(sc);
364 
365 	return (0);
366 }
367 
368 int
369 gpiobus_alloc_ivars(struct gpiobus_ivar *devi)
370 {
371 
372 	/* Allocate pins and flags memory. */
373 	devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF,
374 	    M_NOWAIT | M_ZERO);
375 	if (devi->pins == NULL)
376 		return (ENOMEM);
377 	return (0);
378 }
379 
380 void
381 gpiobus_free_ivars(struct gpiobus_ivar *devi)
382 {
383 
384 	if (devi->pins) {
385 		free(devi->pins, M_DEVBUF);
386 		devi->pins = NULL;
387 	}
388 	devi->npins = 0;
389 }
390 
391 int
392 gpiobus_acquire_pin(device_t bus, uint32_t pin)
393 {
394 	struct gpiobus_softc *sc;
395 
396 	sc = device_get_softc(bus);
397 	/* Consistency check. */
398 	if (pin >= sc->sc_npins) {
399 		device_printf(bus,
400 		    "invalid pin %d, max: %d\n", pin, sc->sc_npins - 1);
401 		return (-1);
402 	}
403 	/* Mark pin as mapped and give warning if it's already mapped. */
404 	if (sc->sc_pins[pin].mapped) {
405 		device_printf(bus, "warning: pin %d is already mapped\n", pin);
406 		return (-1);
407 	}
408 	sc->sc_pins[pin].mapped = 1;
409 
410 	return (0);
411 }
412 
413 /* Release mapped pin */
414 int
415 gpiobus_release_pin(device_t bus, uint32_t pin)
416 {
417 	struct gpiobus_softc *sc;
418 
419 	sc = device_get_softc(bus);
420 	/* Consistency check. */
421 	if (pin >= sc->sc_npins) {
422 		device_printf(bus,
423 		    "invalid pin %d, max=%d\n",
424 		    pin, sc->sc_npins - 1);
425 		return (-1);
426 	}
427 
428 	if (!sc->sc_pins[pin].mapped) {
429 		device_printf(bus, "pin %d is not mapped\n", pin);
430 		return (-1);
431 	}
432 	sc->sc_pins[pin].mapped = 0;
433 
434 	return (0);
435 }
436 
437 static int
438 gpiobus_acquire_child_pins(device_t dev, device_t child)
439 {
440 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
441 	int i;
442 
443 	for (i = 0; i < devi->npins; i++) {
444 		/* Reserve the GPIO pin. */
445 		if (gpiobus_acquire_pin(dev, devi->pins[i]) != 0) {
446 			device_printf(child, "cannot acquire pin %d\n",
447 			    devi->pins[i]);
448 			while (--i >= 0) {
449 				(void)gpiobus_release_pin(dev,
450 				    devi->pins[i]);
451 			}
452 			gpiobus_free_ivars(devi);
453 			return (EBUSY);
454 		}
455 	}
456 	for (i = 0; i < devi->npins; i++) {
457 		/* Use the child name as pin name. */
458 		GPIOBUS_PIN_SETNAME(dev, devi->pins[i],
459 		    device_get_nameunit(child));
460 
461 	}
462 	return (0);
463 }
464 
465 static int
466 gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask)
467 {
468 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
469 	int i, npins;
470 
471 	npins = 0;
472 	for (i = 0; i < 32; i++) {
473 		if (mask & (1 << i))
474 			npins++;
475 	}
476 	if (npins == 0) {
477 		device_printf(child, "empty pin mask\n");
478 		return (EINVAL);
479 	}
480 	devi->npins = npins;
481 	if (gpiobus_alloc_ivars(devi) != 0) {
482 		device_printf(child, "cannot allocate device ivars\n");
483 		return (EINVAL);
484 	}
485 	npins = 0;
486 	for (i = 0; i < 32; i++) {
487 		if ((mask & (1 << i)) == 0)
488 			continue;
489 		devi->pins[npins++] = i;
490 	}
491 
492 	return (0);
493 }
494 
495 static int
496 gpiobus_parse_pin_list(struct gpiobus_softc *sc, device_t child,
497     const char *pins)
498 {
499 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
500 	const char *p;
501 	char *endp;
502 	unsigned long pin;
503 	int i, npins;
504 
505 	npins = 0;
506 	p = pins;
507 	for (;;) {
508 		pin = strtoul(p, &endp, 0);
509 		if (endp == p)
510 			break;
511 		npins++;
512 		if (*endp == '\0')
513 			break;
514 		p = endp + 1;
515 	}
516 
517 	if (*endp != '\0') {
518 		device_printf(child, "garbage in the pin list: %s\n", endp);
519 		return (EINVAL);
520 	}
521 	if (npins == 0) {
522 		device_printf(child, "empty pin list\n");
523 		return (EINVAL);
524 	}
525 
526 	devi->npins = npins;
527 	if (gpiobus_alloc_ivars(devi) != 0) {
528 		device_printf(child, "cannot allocate device ivars\n");
529 		return (EINVAL);
530 	}
531 
532 	i = 0;
533 	p = pins;
534 	for (;;) {
535 		pin = strtoul(p, &endp, 0);
536 
537 		devi->pins[i] = pin;
538 
539 		if (*endp == '\0')
540 			break;
541 		i++;
542 		p = endp + 1;
543 	}
544 
545 	return (0);
546 }
547 
548 static int
549 gpiobus_probe(device_t dev)
550 {
551 	device_set_desc(dev, "GPIO bus");
552 
553 	return (BUS_PROBE_GENERIC);
554 }
555 
556 static int
557 gpiobus_attach(device_t dev)
558 {
559 	int err;
560 
561 	err = gpiobus_init_softc(dev);
562 	if (err != 0)
563 		return (err);
564 
565 	/*
566 	 * Get parent's pins and mark them as unmapped
567 	 */
568 	bus_generic_probe(dev);
569 	bus_enumerate_hinted_children(dev);
570 
571 	return (bus_generic_attach(dev));
572 }
573 
574 /*
575  * Since this is not a self-enumerating bus, and since we always add
576  * children in attach, we have to always delete children here.
577  */
578 static int
579 gpiobus_detach(device_t dev)
580 {
581 	struct gpiobus_softc *sc;
582 	struct gpiobus_ivar *devi;
583 	device_t *devlist;
584 	int i, err, ndevs;
585 
586 	sc = GPIOBUS_SOFTC(dev);
587 	KASSERT(mtx_initialized(&sc->sc_mtx),
588 	    ("gpiobus mutex not initialized"));
589 	GPIOBUS_LOCK_DESTROY(sc);
590 
591 	if ((err = bus_generic_detach(dev)) != 0)
592 		return (err);
593 
594 	if ((err = device_get_children(dev, &devlist, &ndevs)) != 0)
595 		return (err);
596 	for (i = 0; i < ndevs; i++) {
597 		devi = GPIOBUS_IVAR(devlist[i]);
598 		gpiobus_free_ivars(devi);
599 		resource_list_free(&devi->rl);
600 		free(devi, M_DEVBUF);
601 		device_delete_child(dev, devlist[i]);
602 	}
603 	free(devlist, M_TEMP);
604 	rman_fini(&sc->sc_intr_rman);
605 	if (sc->sc_pins) {
606 		for (i = 0; i < sc->sc_npins; i++) {
607 			if (sc->sc_pins[i].name != NULL)
608 				free(sc->sc_pins[i].name, M_DEVBUF);
609 			sc->sc_pins[i].name = NULL;
610 		}
611 		free(sc->sc_pins, M_DEVBUF);
612 		sc->sc_pins = NULL;
613 	}
614 
615 	return (0);
616 }
617 
618 static int
619 gpiobus_suspend(device_t dev)
620 {
621 
622 	return (bus_generic_suspend(dev));
623 }
624 
625 static int
626 gpiobus_resume(device_t dev)
627 {
628 
629 	return (bus_generic_resume(dev));
630 }
631 
632 static void
633 gpiobus_probe_nomatch(device_t dev, device_t child)
634 {
635 	char pins[128];
636 	struct sbuf sb;
637 	struct gpiobus_ivar *devi;
638 
639 	devi = GPIOBUS_IVAR(child);
640 	sbuf_new(&sb, pins, sizeof(pins), SBUF_FIXEDLEN);
641 	gpiobus_print_pins(devi, &sb);
642 	sbuf_finish(&sb);
643 	device_printf(dev, "<unknown device> at pin%s %s",
644 	    devi->npins > 1 ? "s" : "", sbuf_data(&sb));
645 	resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
646 	printf("\n");
647 }
648 
649 static int
650 gpiobus_print_child(device_t dev, device_t child)
651 {
652 	char pins[128];
653 	struct sbuf sb;
654 	int retval = 0;
655 	struct gpiobus_ivar *devi;
656 
657 	devi = GPIOBUS_IVAR(child);
658 	retval += bus_print_child_header(dev, child);
659 	if (devi->npins > 0) {
660 		if (devi->npins > 1)
661 			retval += printf(" at pins ");
662 		else
663 			retval += printf(" at pin ");
664 		sbuf_new(&sb, pins, sizeof(pins), SBUF_FIXEDLEN);
665 		gpiobus_print_pins(devi, &sb);
666 		sbuf_finish(&sb);
667 		retval += printf("%s", sbuf_data(&sb));
668 	}
669 	resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
670 	retval += bus_print_child_footer(dev, child);
671 
672 	return (retval);
673 }
674 
675 static int
676 gpiobus_child_location(device_t bus, device_t child, struct sbuf *sb)
677 {
678 	struct gpiobus_ivar *devi;
679 
680 	devi = GPIOBUS_IVAR(child);
681 	sbuf_printf(sb, "pins=");
682 	gpiobus_print_pins(devi, sb);
683 
684 	return (0);
685 }
686 
687 static device_t
688 gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
689 {
690 	device_t child;
691 	struct gpiobus_ivar *devi;
692 
693 	child = device_add_child_ordered(dev, order, name, unit);
694 	if (child == NULL)
695 		return (child);
696 	devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
697 	if (devi == NULL) {
698 		device_delete_child(dev, child);
699 		return (NULL);
700 	}
701 	resource_list_init(&devi->rl);
702 	device_set_ivars(child, devi);
703 
704 	return (child);
705 }
706 
707 static int
708 gpiobus_rescan(device_t dev)
709 {
710 
711 	/*
712 	 * Re-scan is supposed to remove and add children, but if someone has
713 	 * deleted the hints for a child we attached earlier, we have no easy
714 	 * way to handle that.  So this just attaches new children for whom new
715 	 * hints or drivers have arrived since we last tried.
716 	 */
717 	bus_enumerate_hinted_children(dev);
718 	bus_generic_attach(dev);
719 	return (0);
720 }
721 
722 static void
723 gpiobus_hinted_child(device_t bus, const char *dname, int dunit)
724 {
725 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus);
726 	struct gpiobus_ivar *devi;
727 	device_t child;
728 	const char *pins;
729 	int irq, pinmask;
730 
731 	if (device_find_child(bus, dname, dunit) != NULL) {
732 		return;
733 	}
734 
735 	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
736 	devi = GPIOBUS_IVAR(child);
737 	if (resource_int_value(dname, dunit, "pins", &pinmask) == 0) {
738 		if (gpiobus_parse_pins(sc, child, pinmask)) {
739 			resource_list_free(&devi->rl);
740 			free(devi, M_DEVBUF);
741 			device_delete_child(bus, child);
742 			return;
743 		}
744 	}
745 	else if (resource_string_value(dname, dunit, "pin_list", &pins) == 0) {
746 		if (gpiobus_parse_pin_list(sc, child, pins)) {
747 			resource_list_free(&devi->rl);
748 			free(devi, M_DEVBUF);
749 			device_delete_child(bus, child);
750 			return;
751 		}
752 	}
753 	if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
754 		if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0)
755 			device_printf(bus,
756 			    "warning: bus_set_resource() failed\n");
757 	}
758 }
759 
760 static int
761 gpiobus_set_resource(device_t dev, device_t child, int type, int rid,
762     rman_res_t start, rman_res_t count)
763 {
764 	struct gpiobus_ivar *devi;
765 	struct resource_list_entry *rle;
766 
767 	dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n",
768 	    __func__, dev, child, type, rid, (void *)(intptr_t)start, count);
769 	devi = GPIOBUS_IVAR(child);
770 	rle = resource_list_add(&devi->rl, type, rid, start,
771 	    start + count - 1, count);
772 	if (rle == NULL)
773 		return (ENXIO);
774 
775 	return (0);
776 }
777 
778 static int
779 gpiobus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
780 {
781 	struct gpiobus_ivar *devi;
782 
783 	devi = GPIOBUS_IVAR(child);
784         switch (which) {
785 	case GPIOBUS_IVAR_NPINS:
786 		*result = devi->npins;
787 		break;
788 	case GPIOBUS_IVAR_PINS:
789 		/* Children do not ever need to directly examine this. */
790 		return (ENOTSUP);
791         default:
792                 return (ENOENT);
793         }
794 
795 	return (0);
796 }
797 
798 static int
799 gpiobus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
800 {
801 	struct gpiobus_ivar *devi;
802 	const uint32_t *ptr;
803 	int i;
804 
805 	devi = GPIOBUS_IVAR(child);
806         switch (which) {
807 	case GPIOBUS_IVAR_NPINS:
808 		/* GPIO ivars are set once. */
809 		if (devi->npins != 0) {
810 			return (EBUSY);
811 		}
812 		devi->npins = value;
813 		if (gpiobus_alloc_ivars(devi) != 0) {
814 			device_printf(child, "cannot allocate device ivars\n");
815 			devi->npins = 0;
816 			return (ENOMEM);
817 		}
818 		break;
819 	case GPIOBUS_IVAR_PINS:
820 		ptr = (const uint32_t *)value;
821 		for (i = 0; i < devi->npins; i++)
822 			devi->pins[i] = ptr[i];
823 		if (gpiobus_acquire_child_pins(dev, child) != 0)
824 			return (EBUSY);
825 		break;
826         default:
827                 return (ENOENT);
828         }
829 
830         return (0);
831 }
832 
833 static struct resource *
834 gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid,
835     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
836 {
837 	struct gpiobus_softc *sc;
838 	struct resource *rv;
839 	struct resource_list *rl;
840 	struct resource_list_entry *rle;
841 	int isdefault;
842 
843 	if (type != SYS_RES_IRQ)
844 		return (NULL);
845 	isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1);
846 	rle = NULL;
847 	if (isdefault) {
848 		rl = BUS_GET_RESOURCE_LIST(bus, child);
849 		if (rl == NULL)
850 			return (NULL);
851 		rle = resource_list_find(rl, type, *rid);
852 		if (rle == NULL)
853 			return (NULL);
854 		if (rle->res != NULL)
855 			panic("%s: resource entry is busy", __func__);
856 		start = rle->start;
857 		count = rle->count;
858 		end = rle->end;
859 	}
860 	sc = device_get_softc(bus);
861 	rv = rman_reserve_resource(&sc->sc_intr_rman, start, end, count, flags,
862 	    child);
863 	if (rv == NULL)
864 		return (NULL);
865 	rman_set_rid(rv, *rid);
866 	if ((flags & RF_ACTIVE) != 0 &&
867 	    bus_activate_resource(child, type, *rid, rv) != 0) {
868 		rman_release_resource(rv);
869 		return (NULL);
870 	}
871 
872 	return (rv);
873 }
874 
875 static int
876 gpiobus_release_resource(device_t bus __unused, device_t child, int type,
877     int rid, struct resource *r)
878 {
879 	int error;
880 
881 	if (rman_get_flags(r) & RF_ACTIVE) {
882 		error = bus_deactivate_resource(child, type, rid, r);
883 		if (error)
884 			return (error);
885 	}
886 
887 	return (rman_release_resource(r));
888 }
889 
890 static struct resource_list *
891 gpiobus_get_resource_list(device_t bus __unused, device_t child)
892 {
893 	struct gpiobus_ivar *ivar;
894 
895 	ivar = GPIOBUS_IVAR(child);
896 
897 	return (&ivar->rl);
898 }
899 
900 static int
901 gpiobus_acquire_bus(device_t busdev, device_t child, int how)
902 {
903 	struct gpiobus_softc *sc;
904 
905 	sc = device_get_softc(busdev);
906 	GPIOBUS_ASSERT_UNLOCKED(sc);
907 	GPIOBUS_LOCK(sc);
908 	if (sc->sc_owner != NULL) {
909 		if (sc->sc_owner == child)
910 			panic("%s: %s still owns the bus.",
911 			    device_get_nameunit(busdev),
912 			    device_get_nameunit(child));
913 		if (how == GPIOBUS_DONTWAIT) {
914 			GPIOBUS_UNLOCK(sc);
915 			return (EWOULDBLOCK);
916 		}
917 		while (sc->sc_owner != NULL)
918 			mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0);
919 	}
920 	sc->sc_owner = child;
921 	GPIOBUS_UNLOCK(sc);
922 
923 	return (0);
924 }
925 
926 static void
927 gpiobus_release_bus(device_t busdev, device_t child)
928 {
929 	struct gpiobus_softc *sc;
930 
931 	sc = device_get_softc(busdev);
932 	GPIOBUS_ASSERT_UNLOCKED(sc);
933 	GPIOBUS_LOCK(sc);
934 	if (sc->sc_owner == NULL)
935 		panic("%s: %s releasing unowned bus.",
936 		    device_get_nameunit(busdev),
937 		    device_get_nameunit(child));
938 	if (sc->sc_owner != child)
939 		panic("%s: %s trying to release bus owned by %s",
940 		    device_get_nameunit(busdev),
941 		    device_get_nameunit(child),
942 		    device_get_nameunit(sc->sc_owner));
943 	sc->sc_owner = NULL;
944 	wakeup(sc);
945 	GPIOBUS_UNLOCK(sc);
946 }
947 
948 static int
949 gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin,
950     uint32_t flags)
951 {
952 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
953 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
954 	uint32_t caps;
955 
956 	if (pin >= devi->npins)
957 		return (EINVAL);
958 	if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0)
959 		return (EINVAL);
960 	if (gpio_check_flags(caps, flags) != 0)
961 		return (EINVAL);
962 
963 	return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags));
964 }
965 
966 static int
967 gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin,
968     uint32_t *flags)
969 {
970 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
971 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
972 
973 	if (pin >= devi->npins)
974 		return (EINVAL);
975 
976 	return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags);
977 }
978 
979 static int
980 gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin,
981     uint32_t *caps)
982 {
983 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
984 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
985 
986 	if (pin >= devi->npins)
987 		return (EINVAL);
988 
989 	return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps);
990 }
991 
992 static int
993 gpiobus_pin_set(device_t dev, device_t child, uint32_t pin,
994     unsigned int value)
995 {
996 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
997 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
998 
999 	if (pin >= devi->npins)
1000 		return (EINVAL);
1001 
1002 	return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value);
1003 }
1004 
1005 static int
1006 gpiobus_pin_get(device_t dev, device_t child, uint32_t pin,
1007     unsigned int *value)
1008 {
1009 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
1010 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
1011 
1012 	if (pin >= devi->npins)
1013 		return (EINVAL);
1014 
1015 	return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value);
1016 }
1017 
1018 static int
1019 gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin)
1020 {
1021 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
1022 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
1023 
1024 	if (pin >= devi->npins)
1025 		return (EINVAL);
1026 
1027 	return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]);
1028 }
1029 
1030 static int
1031 gpiobus_pin_getname(device_t dev, uint32_t pin, char *name)
1032 {
1033 	struct gpiobus_softc *sc;
1034 
1035 	sc = GPIOBUS_SOFTC(dev);
1036 	if (pin > sc->sc_npins)
1037 		return (EINVAL);
1038 	/* Did we have a name for this pin ? */
1039 	if (sc->sc_pins[pin].name != NULL) {
1040 		memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME);
1041 		return (0);
1042 	}
1043 
1044 	/* Return the default pin name. */
1045 	return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name));
1046 }
1047 
1048 static int
1049 gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name)
1050 {
1051 	struct gpiobus_softc *sc;
1052 
1053 	sc = GPIOBUS_SOFTC(dev);
1054 	if (pin > sc->sc_npins)
1055 		return (EINVAL);
1056 	if (name == NULL)
1057 		return (EINVAL);
1058 	/* Save the pin name. */
1059 	if (sc->sc_pins[pin].name == NULL)
1060 		sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF,
1061 		    M_WAITOK | M_ZERO);
1062 	strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME);
1063 
1064 	return (0);
1065 }
1066 
1067 static device_method_t gpiobus_methods[] = {
1068 	/* Device interface */
1069 	DEVMETHOD(device_probe,		gpiobus_probe),
1070 	DEVMETHOD(device_attach,	gpiobus_attach),
1071 	DEVMETHOD(device_detach,	gpiobus_detach),
1072 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1073 	DEVMETHOD(device_suspend,	gpiobus_suspend),
1074 	DEVMETHOD(device_resume,	gpiobus_resume),
1075 
1076 	/* Bus interface */
1077 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
1078 	DEVMETHOD(bus_config_intr,	bus_generic_config_intr),
1079 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
1080 	DEVMETHOD(bus_set_resource,	gpiobus_set_resource),
1081 	DEVMETHOD(bus_alloc_resource,	gpiobus_alloc_resource),
1082 	DEVMETHOD(bus_release_resource,	gpiobus_release_resource),
1083 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
1084 	DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
1085 	DEVMETHOD(bus_get_resource_list,	gpiobus_get_resource_list),
1086 	DEVMETHOD(bus_add_child,	gpiobus_add_child),
1087 	DEVMETHOD(bus_rescan,		gpiobus_rescan),
1088 	DEVMETHOD(bus_probe_nomatch,	gpiobus_probe_nomatch),
1089 	DEVMETHOD(bus_print_child,	gpiobus_print_child),
1090 	DEVMETHOD(bus_child_location,	gpiobus_child_location),
1091 	DEVMETHOD(bus_hinted_child,	gpiobus_hinted_child),
1092 	DEVMETHOD(bus_read_ivar,        gpiobus_read_ivar),
1093 	DEVMETHOD(bus_write_ivar,       gpiobus_write_ivar),
1094 
1095 	/* GPIO protocol */
1096 	DEVMETHOD(gpiobus_acquire_bus,	gpiobus_acquire_bus),
1097 	DEVMETHOD(gpiobus_release_bus,	gpiobus_release_bus),
1098 	DEVMETHOD(gpiobus_pin_getflags,	gpiobus_pin_getflags),
1099 	DEVMETHOD(gpiobus_pin_getcaps,	gpiobus_pin_getcaps),
1100 	DEVMETHOD(gpiobus_pin_setflags,	gpiobus_pin_setflags),
1101 	DEVMETHOD(gpiobus_pin_get,	gpiobus_pin_get),
1102 	DEVMETHOD(gpiobus_pin_set,	gpiobus_pin_set),
1103 	DEVMETHOD(gpiobus_pin_toggle,	gpiobus_pin_toggle),
1104 	DEVMETHOD(gpiobus_pin_getname,	gpiobus_pin_getname),
1105 	DEVMETHOD(gpiobus_pin_setname,	gpiobus_pin_setname),
1106 
1107 	DEVMETHOD_END
1108 };
1109 
1110 driver_t gpiobus_driver = {
1111 	"gpiobus",
1112 	gpiobus_methods,
1113 	sizeof(struct gpiobus_softc)
1114 };
1115 
1116 devclass_t	gpiobus_devclass;
1117 
1118 EARLY_DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0,
1119     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
1120 MODULE_VERSION(gpiobus, 1);
1121