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