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