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