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