2 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
37 #include <dev/gpio/gpiobusvar.h>
39 #include "gpiobus_if.h"
41 static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int);
42 static int gpiobus_probe(device_t);
43 static int gpiobus_attach(device_t);
44 static int gpiobus_detach(device_t);
45 static int gpiobus_suspend(device_t);
46 static int gpiobus_resume(device_t);
47 static int gpiobus_print_child(device_t, device_t);
48 static int gpiobus_child_location_str(device_t, device_t, char *, size_t);
49 static int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t);
50 static device_t gpiobus_add_child(device_t, u_int, const char *, int);
51 static void gpiobus_hinted_child(device_t, const char *, int);
56 static void gpiobus_lock_bus(device_t);
57 static void gpiobus_unlock_bus(device_t);
58 static void gpiobus_acquire_bus(device_t, device_t);
59 static void gpiobus_release_bus(device_t, device_t);
60 static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t);
61 static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*);
62 static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*);
63 static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int);
64 static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*);
65 static int gpiobus_pin_toggle(device_t, device_t, uint32_t);
68 gpiobus_print_pins(struct gpiobus_ivar *devi)
70 int range_start, range_stop, need_coma;
77 range_start = range_stop = devi->pins[0];
78 for (i = 1; i < devi->npins; i++) {
79 if (devi->pins[i] != (range_stop + 1)) {
82 if (range_start != range_stop)
83 printf("%d-%d", range_start, range_stop);
85 printf("%d", range_start);
87 range_start = range_stop = devi->pins[i];
96 if (range_start != range_stop)
97 printf("%d-%d", range_start, range_stop);
99 printf("%d", range_start);
103 gpiobus_init_softc(device_t dev)
105 struct gpiobus_softc *sc;
107 sc = GPIOBUS_SOFTC(dev);
109 sc->sc_dev = device_get_parent(dev);
111 if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0)
114 KASSERT(sc->sc_npins != 0, ("GPIO device with no pins"));
116 /* Pins = GPIO_PIN_MAX() + 1 */
119 sc->sc_pins_mapped = malloc(sizeof(int) * sc->sc_npins, M_DEVBUF,
121 if (sc->sc_pins_mapped == NULL)
124 /* Initialize the bus lock. */
125 GPIOBUS_LOCK_INIT(sc);
131 gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask)
133 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
137 for (i = 0; i < 32; i++) {
143 device_printf(child, "empty pin mask\n");
148 devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF,
155 for (i = 0; i < 32; i++) {
157 if ((mask & (1 << i)) == 0)
160 if (i >= sc->sc_npins) {
162 "invalid pin %d, max: %d\n", i, sc->sc_npins - 1);
163 free(devi->pins, M_DEVBUF);
167 devi->pins[npins++] = i;
169 * Mark pin as mapped and give warning if it's already mapped
171 if (sc->sc_pins_mapped[i]) {
173 "warning: pin %d is already mapped\n", i);
174 free(devi->pins, M_DEVBUF);
177 sc->sc_pins_mapped[i] = 1;
184 gpiobus_probe(device_t dev)
186 device_set_desc(dev, "GPIO bus");
188 return (BUS_PROBE_GENERIC);
192 gpiobus_attach(device_t dev)
196 err = gpiobus_init_softc(dev);
201 * Get parent's pins and mark them as unmapped
203 bus_generic_probe(dev);
204 bus_enumerate_hinted_children(dev);
206 return (bus_generic_attach(dev));
210 * Since this is not a self-enumerating bus, and since we always add
211 * children in attach, we have to always delete children here.
214 gpiobus_detach(device_t dev)
216 struct gpiobus_softc *sc;
217 struct gpiobus_ivar *devi;
221 sc = GPIOBUS_SOFTC(dev);
222 KASSERT(mtx_initialized(&sc->sc_mtx),
223 ("gpiobus mutex not initialized"));
224 GPIOBUS_LOCK_DESTROY(sc);
226 if ((err = bus_generic_detach(dev)) != 0)
229 if ((err = device_get_children(dev, &devlist, &ndevs)) != 0)
231 for (i = 0; i < ndevs; i++) {
232 device_delete_child(dev, devlist[i]);
233 devi = GPIOBUS_IVAR(devlist[i]);
235 free(devi->pins, M_DEVBUF);
239 free(devlist, M_TEMP);
241 if (sc->sc_pins_mapped) {
242 free(sc->sc_pins_mapped, M_DEVBUF);
243 sc->sc_pins_mapped = NULL;
250 gpiobus_suspend(device_t dev)
253 return (bus_generic_suspend(dev));
257 gpiobus_resume(device_t dev)
260 return (bus_generic_resume(dev));
264 gpiobus_print_child(device_t dev, device_t child)
266 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
269 retval += bus_print_child_header(dev, child);
270 retval += printf(" at pin(s) ");
271 gpiobus_print_pins(devi);
272 retval += bus_print_child_footer(dev, child);
278 gpiobus_child_location_str(device_t bus, device_t child, char *buf,
282 snprintf(buf, buflen, "pins=?");
287 gpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
296 gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
299 struct gpiobus_ivar *devi;
301 child = device_add_child_ordered(dev, order, name, unit);
304 devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
306 device_delete_child(dev, child);
309 device_set_ivars(child, devi);
314 gpiobus_hinted_child(device_t bus, const char *dname, int dunit)
316 struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus);
317 struct gpiobus_ivar *devi;
322 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
323 devi = GPIOBUS_IVAR(child);
324 resource_int_value(dname, dunit, "pins", &pins);
325 if (gpiobus_parse_pins(sc, child, pins))
326 device_delete_child(bus, child);
330 gpiobus_lock_bus(device_t busdev)
332 struct gpiobus_softc *sc;
334 sc = device_get_softc(busdev);
335 GPIOBUS_ASSERT_UNLOCKED(sc);
340 gpiobus_unlock_bus(device_t busdev)
342 struct gpiobus_softc *sc;
344 sc = device_get_softc(busdev);
345 GPIOBUS_ASSERT_LOCKED(sc);
350 gpiobus_acquire_bus(device_t busdev, device_t child)
352 struct gpiobus_softc *sc;
354 sc = device_get_softc(busdev);
355 GPIOBUS_ASSERT_LOCKED(sc);
358 panic("gpiobus: cannot serialize the access to device.");
359 sc->sc_owner = child;
363 gpiobus_release_bus(device_t busdev, device_t child)
365 struct gpiobus_softc *sc;
367 sc = device_get_softc(busdev);
368 GPIOBUS_ASSERT_LOCKED(sc);
371 panic("gpiobus: releasing unowned bus.");
372 if (sc->sc_owner != child)
373 panic("gpiobus: you don't own the bus. game over.");
379 gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin,
382 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
383 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
385 if (pin >= devi->npins)
388 return GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags);
392 gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin,
395 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
396 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
398 if (pin >= devi->npins)
401 return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags);
405 gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin,
408 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
409 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
411 if (pin >= devi->npins)
414 return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps);
418 gpiobus_pin_set(device_t dev, device_t child, uint32_t pin,
421 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
422 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
424 if (pin >= devi->npins)
427 return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value);
431 gpiobus_pin_get(device_t dev, device_t child, uint32_t pin,
434 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
435 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
437 if (pin >= devi->npins)
440 return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value);
444 gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin)
446 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
447 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
449 if (pin >= devi->npins)
452 return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]);
455 static device_method_t gpiobus_methods[] = {
456 /* Device interface */
457 DEVMETHOD(device_probe, gpiobus_probe),
458 DEVMETHOD(device_attach, gpiobus_attach),
459 DEVMETHOD(device_detach, gpiobus_detach),
460 DEVMETHOD(device_shutdown, bus_generic_shutdown),
461 DEVMETHOD(device_suspend, gpiobus_suspend),
462 DEVMETHOD(device_resume, gpiobus_resume),
465 DEVMETHOD(bus_add_child, gpiobus_add_child),
466 DEVMETHOD(bus_print_child, gpiobus_print_child),
467 DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str),
468 DEVMETHOD(bus_child_location_str, gpiobus_child_location_str),
469 DEVMETHOD(bus_hinted_child, gpiobus_hinted_child),
472 DEVMETHOD(gpiobus_lock_bus, gpiobus_lock_bus),
473 DEVMETHOD(gpiobus_unlock_bus, gpiobus_unlock_bus),
474 DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus),
475 DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus),
476 DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags),
477 DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps),
478 DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags),
479 DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get),
480 DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set),
481 DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle),
486 driver_t gpiobus_driver = {
489 sizeof(struct gpiobus_softc)
492 devclass_t gpiobus_devclass;
494 DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0);
495 MODULE_VERSION(gpiobus, 1);