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