xref: /freebsd/sys/dev/gpio/gpiobus.c (revision e44885ac0116d05f7380543569d5093985c92f06)
1 /*-
2  * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
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 
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 *, char *, size_t);
53 static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int);
54 static int gpiobus_probe(device_t);
55 static int gpiobus_attach(device_t);
56 static int gpiobus_detach(device_t);
57 static int gpiobus_suspend(device_t);
58 static int gpiobus_resume(device_t);
59 static void gpiobus_probe_nomatch(device_t, device_t);
60 static int gpiobus_print_child(device_t, device_t);
61 static int gpiobus_child_location_str(device_t, device_t, char *, size_t);
62 static int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t);
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  * XXX -> Move me to better place - gpio_subr.c?
80  * Also, this function must be changed when interrupt configuration
81  * data will be moved into struct resource.
82  */
83 #ifdef INTRNG
84 
85 struct resource *
86 gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags,
87     gpio_pin_t pin, uint32_t intr_mode)
88 {
89 	u_int irq;
90 	struct intr_map_data_gpio *gpio_data;
91 	struct resource *res;
92 
93 	gpio_data = (struct intr_map_data_gpio *)intr_alloc_map_data(
94 	    INTR_MAP_DATA_GPIO, sizeof(*gpio_data), M_WAITOK | M_ZERO);
95 	gpio_data->gpio_pin_num = pin->pin;
96 	gpio_data->gpio_pin_flags = pin->flags;
97 	gpio_data->gpio_intr_mode = intr_mode;
98 
99 	irq = intr_map_irq(pin->dev, 0, (struct intr_map_data *)gpio_data);
100 	res = bus_alloc_resource(consumer_dev, SYS_RES_IRQ, rid, irq, irq, 1,
101 	    alloc_flags);
102 	if (res == NULL) {
103 		intr_free_intr_map_data((struct intr_map_data *)gpio_data);
104 		return (NULL);
105 	}
106 	rman_set_virtual(res, gpio_data);
107 	return (res);
108 }
109 #else
110 struct resource *
111 gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags,
112     gpio_pin_t pin, uint32_t intr_mode)
113 {
114 
115 	return (NULL);
116 }
117 #endif
118 
119 int
120 gpio_check_flags(uint32_t caps, uint32_t flags)
121 {
122 
123 	/* Filter unwanted flags. */
124 	flags &= caps;
125 
126 	/* Cannot mix input/output together. */
127 	if (flags & GPIO_PIN_INPUT && flags & GPIO_PIN_OUTPUT)
128 		return (EINVAL);
129 	/* Cannot mix pull-up/pull-down together. */
130 	if (flags & GPIO_PIN_PULLUP && flags & GPIO_PIN_PULLDOWN)
131 		return (EINVAL);
132 
133 	return (0);
134 }
135 
136 static void
137 gpiobus_print_pins(struct gpiobus_ivar *devi, char *buf, size_t buflen)
138 {
139 	char tmp[128];
140 	int i, range_start, range_stop, need_coma;
141 
142 	if (devi->npins == 0)
143 		return;
144 
145 	need_coma = 0;
146 	range_start = range_stop = devi->pins[0];
147 	for (i = 1; i < devi->npins; i++) {
148 		if (devi->pins[i] != (range_stop + 1)) {
149 			if (need_coma)
150 				strlcat(buf, ",", buflen);
151 			memset(tmp, 0, sizeof(tmp));
152 			if (range_start != range_stop)
153 				snprintf(tmp, sizeof(tmp) - 1, "%d-%d",
154 				    range_start, range_stop);
155 			else
156 				snprintf(tmp, sizeof(tmp) - 1, "%d",
157 				    range_start);
158 			strlcat(buf, tmp, buflen);
159 
160 			range_start = range_stop = devi->pins[i];
161 			need_coma = 1;
162 		}
163 		else
164 			range_stop++;
165 	}
166 
167 	if (need_coma)
168 		strlcat(buf, ",", buflen);
169 	memset(tmp, 0, sizeof(tmp));
170 	if (range_start != range_stop)
171 		snprintf(tmp, sizeof(tmp) - 1, "%d-%d",
172 		    range_start, range_stop);
173 	else
174 		snprintf(tmp, sizeof(tmp) - 1, "%d",
175 		    range_start);
176 	strlcat(buf, tmp, buflen);
177 }
178 
179 device_t
180 gpiobus_attach_bus(device_t dev)
181 {
182 	device_t busdev;
183 
184 	busdev = device_add_child(dev, "gpiobus", -1);
185 	if (busdev == NULL)
186 		return (NULL);
187 	if (device_add_child(dev, "gpioc", -1) == NULL) {
188 		device_delete_child(dev, busdev);
189 		return (NULL);
190 	}
191 #ifdef FDT
192 	ofw_gpiobus_register_provider(dev);
193 #endif
194 	bus_generic_attach(dev);
195 
196 	return (busdev);
197 }
198 
199 int
200 gpiobus_detach_bus(device_t dev)
201 {
202 	int err;
203 
204 #ifdef FDT
205 	ofw_gpiobus_unregister_provider(dev);
206 #endif
207 	err = bus_generic_detach(dev);
208 	if (err != 0)
209 		return (err);
210 
211 	return (device_delete_children(dev));
212 }
213 
214 int
215 gpiobus_init_softc(device_t dev)
216 {
217 	struct gpiobus_softc *sc;
218 
219 	sc = GPIOBUS_SOFTC(dev);
220 	sc->sc_busdev = dev;
221 	sc->sc_dev = device_get_parent(dev);
222 	sc->sc_intr_rman.rm_type = RMAN_ARRAY;
223 	sc->sc_intr_rman.rm_descr = "GPIO Interrupts";
224 	if (rman_init(&sc->sc_intr_rman) != 0 ||
225 	    rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0)
226 		panic("%s: failed to set up rman.", __func__);
227 
228 	if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0)
229 		return (ENXIO);
230 
231 	KASSERT(sc->sc_npins >= 0, ("GPIO device with no pins"));
232 
233 	/* Pins = GPIO_PIN_MAX() + 1 */
234 	sc->sc_npins++;
235 
236 	sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF,
237 	    M_NOWAIT | M_ZERO);
238 	if (sc->sc_pins == NULL)
239 		return (ENOMEM);
240 
241 	/* Initialize the bus lock. */
242 	GPIOBUS_LOCK_INIT(sc);
243 
244 	return (0);
245 }
246 
247 int
248 gpiobus_alloc_ivars(struct gpiobus_ivar *devi)
249 {
250 
251 	/* Allocate pins and flags memory. */
252 	devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF,
253 	    M_NOWAIT | M_ZERO);
254 	if (devi->pins == NULL)
255 		return (ENOMEM);
256 	devi->flags = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF,
257 	    M_NOWAIT | M_ZERO);
258 	if (devi->flags == NULL) {
259 		free(devi->pins, M_DEVBUF);
260 		return (ENOMEM);
261 	}
262 
263 	return (0);
264 }
265 
266 void
267 gpiobus_free_ivars(struct gpiobus_ivar *devi)
268 {
269 
270 	if (devi->flags) {
271 		free(devi->flags, M_DEVBUF);
272 		devi->flags = NULL;
273 	}
274 	if (devi->pins) {
275 		free(devi->pins, M_DEVBUF);
276 		devi->pins = NULL;
277 	}
278 }
279 
280 int
281 gpiobus_acquire_pin(device_t bus, uint32_t pin)
282 {
283 	struct gpiobus_softc *sc;
284 
285 	sc = device_get_softc(bus);
286 	/* Consistency check. */
287 	if (pin >= sc->sc_npins) {
288 		device_printf(bus,
289 		    "invalid pin %d, max: %d\n", pin, sc->sc_npins - 1);
290 		return (-1);
291 	}
292 	/* Mark pin as mapped and give warning if it's already mapped. */
293 	if (sc->sc_pins[pin].mapped) {
294 		device_printf(bus, "warning: pin %d is already mapped\n", pin);
295 		return (-1);
296 	}
297 	sc->sc_pins[pin].mapped = 1;
298 
299 	return (0);
300 }
301 
302 /* Release mapped pin */
303 int
304 gpiobus_release_pin(device_t bus, uint32_t pin)
305 {
306 	struct gpiobus_softc *sc;
307 
308 	sc = device_get_softc(bus);
309 	/* Consistency check. */
310 	if (pin >= sc->sc_npins) {
311 		device_printf(bus,
312 		    "gpiobus_acquire_pin: invalid pin %d, max=%d\n",
313 		    pin, sc->sc_npins - 1);
314 		return (-1);
315 	}
316 
317 	if (!sc->sc_pins[pin].mapped) {
318 		device_printf(bus, "gpiobus_acquire_pin: pin %d is not mapped\n", pin);
319 		return (-1);
320 	}
321 	sc->sc_pins[pin].mapped = 0;
322 
323 	return (0);
324 }
325 
326 static int
327 gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask)
328 {
329 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
330 	int i, npins;
331 
332 	npins = 0;
333 	for (i = 0; i < 32; i++) {
334 		if (mask & (1 << i))
335 			npins++;
336 	}
337 	if (npins == 0) {
338 		device_printf(child, "empty pin mask\n");
339 		return (EINVAL);
340 	}
341 	devi->npins = npins;
342 	if (gpiobus_alloc_ivars(devi) != 0) {
343 		device_printf(child, "cannot allocate device ivars\n");
344 		return (EINVAL);
345 	}
346 	npins = 0;
347 	for (i = 0; i < 32; i++) {
348 		if ((mask & (1 << i)) == 0)
349 			continue;
350 		/* Reserve the GPIO pin. */
351 		if (gpiobus_acquire_pin(sc->sc_busdev, i) != 0) {
352 			gpiobus_free_ivars(devi);
353 			return (EINVAL);
354 		}
355 		devi->pins[npins++] = i;
356 		/* Use the child name as pin name. */
357 		GPIOBUS_PIN_SETNAME(sc->sc_busdev, i,
358 		    device_get_nameunit(child));
359 	}
360 
361 	return (0);
362 }
363 
364 static int
365 gpiobus_probe(device_t dev)
366 {
367 	device_set_desc(dev, "GPIO bus");
368 
369 	return (BUS_PROBE_GENERIC);
370 }
371 
372 static int
373 gpiobus_attach(device_t dev)
374 {
375 	int err;
376 
377 	err = gpiobus_init_softc(dev);
378 	if (err != 0)
379 		return (err);
380 
381 	/*
382 	 * Get parent's pins and mark them as unmapped
383 	 */
384 	bus_generic_probe(dev);
385 	bus_enumerate_hinted_children(dev);
386 
387 	return (bus_generic_attach(dev));
388 }
389 
390 /*
391  * Since this is not a self-enumerating bus, and since we always add
392  * children in attach, we have to always delete children here.
393  */
394 static int
395 gpiobus_detach(device_t dev)
396 {
397 	struct gpiobus_softc *sc;
398 	struct gpiobus_ivar *devi;
399 	device_t *devlist;
400 	int i, err, ndevs;
401 
402 	sc = GPIOBUS_SOFTC(dev);
403 	KASSERT(mtx_initialized(&sc->sc_mtx),
404 	    ("gpiobus mutex not initialized"));
405 	GPIOBUS_LOCK_DESTROY(sc);
406 
407 	if ((err = bus_generic_detach(dev)) != 0)
408 		return (err);
409 
410 	if ((err = device_get_children(dev, &devlist, &ndevs)) != 0)
411 		return (err);
412 	for (i = 0; i < ndevs; i++) {
413 		devi = GPIOBUS_IVAR(devlist[i]);
414 		gpiobus_free_ivars(devi);
415 		resource_list_free(&devi->rl);
416 		free(devi, M_DEVBUF);
417 		device_delete_child(dev, devlist[i]);
418 	}
419 	free(devlist, M_TEMP);
420 	rman_fini(&sc->sc_intr_rman);
421 	if (sc->sc_pins) {
422 		for (i = 0; i < sc->sc_npins; i++) {
423 			if (sc->sc_pins[i].name != NULL)
424 				free(sc->sc_pins[i].name, M_DEVBUF);
425 			sc->sc_pins[i].name = NULL;
426 		}
427 		free(sc->sc_pins, M_DEVBUF);
428 		sc->sc_pins = NULL;
429 	}
430 
431 	return (0);
432 }
433 
434 static int
435 gpiobus_suspend(device_t dev)
436 {
437 
438 	return (bus_generic_suspend(dev));
439 }
440 
441 static int
442 gpiobus_resume(device_t dev)
443 {
444 
445 	return (bus_generic_resume(dev));
446 }
447 
448 static void
449 gpiobus_probe_nomatch(device_t dev, device_t child)
450 {
451 	char pins[128];
452 	struct gpiobus_ivar *devi;
453 
454 	devi = GPIOBUS_IVAR(child);
455 	memset(pins, 0, sizeof(pins));
456 	gpiobus_print_pins(devi, pins, sizeof(pins));
457 	if (devi->npins > 1)
458 		device_printf(dev, "<unknown device> at pins %s", pins);
459 	else
460 		device_printf(dev, "<unknown device> at pin %s", pins);
461 	resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
462 	printf("\n");
463 }
464 
465 static int
466 gpiobus_print_child(device_t dev, device_t child)
467 {
468 	char pins[128];
469 	int retval = 0;
470 	struct gpiobus_ivar *devi;
471 
472 	devi = GPIOBUS_IVAR(child);
473 	memset(pins, 0, sizeof(pins));
474 	retval += bus_print_child_header(dev, child);
475 	if (devi->npins > 0) {
476 		if (devi->npins > 1)
477 			retval += printf(" at pins ");
478 		else
479 			retval += printf(" at pin ");
480 		gpiobus_print_pins(devi, pins, sizeof(pins));
481 		retval += printf("%s", pins);
482 	}
483 	resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
484 	retval += bus_print_child_footer(dev, child);
485 
486 	return (retval);
487 }
488 
489 static int
490 gpiobus_child_location_str(device_t bus, device_t child, char *buf,
491     size_t buflen)
492 {
493 	struct gpiobus_ivar *devi;
494 
495 	devi = GPIOBUS_IVAR(child);
496 	if (devi->npins > 1)
497 		strlcpy(buf, "pins=", buflen);
498 	else
499 		strlcpy(buf, "pin=", buflen);
500 	gpiobus_print_pins(devi, buf, buflen);
501 
502 	return (0);
503 }
504 
505 static int
506 gpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
507     size_t buflen)
508 {
509 
510 	*buf = '\0';
511 	return (0);
512 }
513 
514 static device_t
515 gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
516 {
517 	device_t child;
518 	struct gpiobus_ivar *devi;
519 
520 	child = device_add_child_ordered(dev, order, name, unit);
521 	if (child == NULL)
522 		return (child);
523 	devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
524 	if (devi == NULL) {
525 		device_delete_child(dev, child);
526 		return (NULL);
527 	}
528 	resource_list_init(&devi->rl);
529 	device_set_ivars(child, devi);
530 
531 	return (child);
532 }
533 
534 static void
535 gpiobus_hinted_child(device_t bus, const char *dname, int dunit)
536 {
537 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus);
538 	struct gpiobus_ivar *devi;
539 	device_t child;
540 	int irq, pins;
541 
542 	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
543 	devi = GPIOBUS_IVAR(child);
544 	resource_int_value(dname, dunit, "pins", &pins);
545 	if (gpiobus_parse_pins(sc, child, pins)) {
546 		resource_list_free(&devi->rl);
547 		free(devi, M_DEVBUF);
548 		device_delete_child(bus, child);
549 	}
550 	if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
551 		if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0)
552 			device_printf(bus,
553 			    "warning: bus_set_resource() failed\n");
554 	}
555 }
556 
557 static int
558 gpiobus_set_resource(device_t dev, device_t child, int type, int rid,
559     rman_res_t start, rman_res_t count)
560 {
561 	struct gpiobus_ivar *devi;
562 	struct resource_list_entry *rle;
563 
564 	dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n",
565 	    __func__, dev, child, type, rid, (void *)(intptr_t)start, count);
566 	devi = GPIOBUS_IVAR(child);
567 	rle = resource_list_add(&devi->rl, type, rid, start,
568 	    start + count - 1, count);
569 	if (rle == NULL)
570 		return (ENXIO);
571 
572 	return (0);
573 }
574 
575 static struct resource *
576 gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid,
577     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
578 {
579 	struct gpiobus_softc *sc;
580 	struct resource *rv;
581 	struct resource_list *rl;
582 	struct resource_list_entry *rle;
583 	int isdefault;
584 
585 	if (type != SYS_RES_IRQ)
586 		return (NULL);
587 	isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1);
588 	rle = NULL;
589 	if (isdefault) {
590 		rl = BUS_GET_RESOURCE_LIST(bus, child);
591 		if (rl == NULL)
592 			return (NULL);
593 		rle = resource_list_find(rl, type, *rid);
594 		if (rle == NULL)
595 			return (NULL);
596 		if (rle->res != NULL)
597 			panic("%s: resource entry is busy", __func__);
598 		start = rle->start;
599 		count = rle->count;
600 		end = rle->end;
601 	}
602 	sc = device_get_softc(bus);
603 	rv = rman_reserve_resource(&sc->sc_intr_rman, start, end, count, flags,
604 	    child);
605 	if (rv == NULL)
606 		return (NULL);
607 	rman_set_rid(rv, *rid);
608 	if ((flags & RF_ACTIVE) != 0 &&
609 	    bus_activate_resource(child, type, *rid, rv) != 0) {
610 		rman_release_resource(rv);
611 		return (NULL);
612 	}
613 
614 	return (rv);
615 }
616 
617 static int
618 gpiobus_release_resource(device_t bus __unused, device_t child, int type,
619     int rid, struct resource *r)
620 {
621 	int error;
622 
623 	if (rman_get_flags(r) & RF_ACTIVE) {
624 		error = bus_deactivate_resource(child, type, rid, r);
625 		if (error)
626 			return (error);
627 	}
628 
629 	return (rman_release_resource(r));
630 }
631 
632 static struct resource_list *
633 gpiobus_get_resource_list(device_t bus __unused, device_t child)
634 {
635 	struct gpiobus_ivar *ivar;
636 
637 	ivar = GPIOBUS_IVAR(child);
638 
639 	return (&ivar->rl);
640 }
641 
642 static int
643 gpiobus_acquire_bus(device_t busdev, device_t child, int how)
644 {
645 	struct gpiobus_softc *sc;
646 
647 	sc = device_get_softc(busdev);
648 	GPIOBUS_ASSERT_UNLOCKED(sc);
649 	GPIOBUS_LOCK(sc);
650 	if (sc->sc_owner != NULL) {
651 		if (sc->sc_owner == child)
652 			panic("%s: %s still owns the bus.",
653 			    device_get_nameunit(busdev),
654 			    device_get_nameunit(child));
655 		if (how == GPIOBUS_DONTWAIT) {
656 			GPIOBUS_UNLOCK(sc);
657 			return (EWOULDBLOCK);
658 		}
659 		while (sc->sc_owner != NULL)
660 			mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0);
661 	}
662 	sc->sc_owner = child;
663 	GPIOBUS_UNLOCK(sc);
664 
665 	return (0);
666 }
667 
668 static void
669 gpiobus_release_bus(device_t busdev, device_t child)
670 {
671 	struct gpiobus_softc *sc;
672 
673 	sc = device_get_softc(busdev);
674 	GPIOBUS_ASSERT_UNLOCKED(sc);
675 	GPIOBUS_LOCK(sc);
676 	if (sc->sc_owner == NULL)
677 		panic("%s: %s releasing unowned bus.",
678 		    device_get_nameunit(busdev),
679 		    device_get_nameunit(child));
680 	if (sc->sc_owner != child)
681 		panic("%s: %s trying to release bus owned by %s",
682 		    device_get_nameunit(busdev),
683 		    device_get_nameunit(child),
684 		    device_get_nameunit(sc->sc_owner));
685 	sc->sc_owner = NULL;
686 	wakeup(sc);
687 	GPIOBUS_UNLOCK(sc);
688 }
689 
690 static int
691 gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin,
692     uint32_t flags)
693 {
694 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
695 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
696 	uint32_t caps;
697 
698 	if (pin >= devi->npins)
699 		return (EINVAL);
700 	if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0)
701 		return (EINVAL);
702 	if (gpio_check_flags(caps, flags) != 0)
703 		return (EINVAL);
704 
705 	return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags));
706 }
707 
708 static int
709 gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin,
710     uint32_t *flags)
711 {
712 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
713 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
714 
715 	if (pin >= devi->npins)
716 		return (EINVAL);
717 
718 	return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags);
719 }
720 
721 static int
722 gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin,
723     uint32_t *caps)
724 {
725 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
726 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
727 
728 	if (pin >= devi->npins)
729 		return (EINVAL);
730 
731 	return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps);
732 }
733 
734 static int
735 gpiobus_pin_set(device_t dev, device_t child, uint32_t pin,
736     unsigned int value)
737 {
738 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
739 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
740 
741 	if (pin >= devi->npins)
742 		return (EINVAL);
743 
744 	return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value);
745 }
746 
747 static int
748 gpiobus_pin_get(device_t dev, device_t child, uint32_t pin,
749     unsigned int *value)
750 {
751 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
752 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
753 
754 	if (pin >= devi->npins)
755 		return (EINVAL);
756 
757 	return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value);
758 }
759 
760 static int
761 gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin)
762 {
763 	struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
764 	struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
765 
766 	if (pin >= devi->npins)
767 		return (EINVAL);
768 
769 	return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]);
770 }
771 
772 static int
773 gpiobus_pin_getname(device_t dev, uint32_t pin, char *name)
774 {
775 	struct gpiobus_softc *sc;
776 
777 	sc = GPIOBUS_SOFTC(dev);
778 	if (pin > sc->sc_npins)
779 		return (EINVAL);
780 	/* Did we have a name for this pin ? */
781 	if (sc->sc_pins[pin].name != NULL) {
782 		memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME);
783 		return (0);
784 	}
785 
786 	/* Return the default pin name. */
787 	return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name));
788 }
789 
790 static int
791 gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name)
792 {
793 	struct gpiobus_softc *sc;
794 
795 	sc = GPIOBUS_SOFTC(dev);
796 	if (pin > sc->sc_npins)
797 		return (EINVAL);
798 	if (name == NULL)
799 		return (EINVAL);
800 	/* Save the pin name. */
801 	if (sc->sc_pins[pin].name == NULL)
802 		sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF,
803 		    M_WAITOK | M_ZERO);
804 	strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME);
805 
806 	return (0);
807 }
808 
809 static device_method_t gpiobus_methods[] = {
810 	/* Device interface */
811 	DEVMETHOD(device_probe,		gpiobus_probe),
812 	DEVMETHOD(device_attach,	gpiobus_attach),
813 	DEVMETHOD(device_detach,	gpiobus_detach),
814 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
815 	DEVMETHOD(device_suspend,	gpiobus_suspend),
816 	DEVMETHOD(device_resume,	gpiobus_resume),
817 
818 	/* Bus interface */
819 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
820 	DEVMETHOD(bus_config_intr,	bus_generic_config_intr),
821 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
822 	DEVMETHOD(bus_set_resource,	gpiobus_set_resource),
823 	DEVMETHOD(bus_alloc_resource,	gpiobus_alloc_resource),
824 	DEVMETHOD(bus_release_resource,	gpiobus_release_resource),
825 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
826 	DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
827 	DEVMETHOD(bus_get_resource_list,	gpiobus_get_resource_list),
828 	DEVMETHOD(bus_add_child,	gpiobus_add_child),
829 	DEVMETHOD(bus_probe_nomatch,	gpiobus_probe_nomatch),
830 	DEVMETHOD(bus_print_child,	gpiobus_print_child),
831 	DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str),
832 	DEVMETHOD(bus_child_location_str, gpiobus_child_location_str),
833 	DEVMETHOD(bus_hinted_child,	gpiobus_hinted_child),
834 
835 	/* GPIO protocol */
836 	DEVMETHOD(gpiobus_acquire_bus,	gpiobus_acquire_bus),
837 	DEVMETHOD(gpiobus_release_bus,	gpiobus_release_bus),
838 	DEVMETHOD(gpiobus_pin_getflags,	gpiobus_pin_getflags),
839 	DEVMETHOD(gpiobus_pin_getcaps,	gpiobus_pin_getcaps),
840 	DEVMETHOD(gpiobus_pin_setflags,	gpiobus_pin_setflags),
841 	DEVMETHOD(gpiobus_pin_get,	gpiobus_pin_get),
842 	DEVMETHOD(gpiobus_pin_set,	gpiobus_pin_set),
843 	DEVMETHOD(gpiobus_pin_toggle,	gpiobus_pin_toggle),
844 	DEVMETHOD(gpiobus_pin_getname,	gpiobus_pin_getname),
845 	DEVMETHOD(gpiobus_pin_setname,	gpiobus_pin_setname),
846 
847 	DEVMETHOD_END
848 };
849 
850 driver_t gpiobus_driver = {
851 	"gpiobus",
852 	gpiobus_methods,
853 	sizeof(struct gpiobus_softc)
854 };
855 
856 devclass_t	gpiobus_devclass;
857 
858 EARLY_DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0,
859     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
860 MODULE_VERSION(gpiobus, 1);
861