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