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>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/kernel.h>
35 #include <sys/queue.h>
36 #include <sys/sysctl.h>
37 #include <sys/types.h>
40 #include <machine/bus.h>
42 #include <machine/resource.h>
45 #include <dev/gpio/gpiobusvar.h>
47 #include "gpiobus_if.h"
49 static void gpiobus_print_pins(struct gpiobus_ivar *);
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 int gpiobus_print_child(device_t, device_t);
57 static int gpiobus_child_location_str(device_t, device_t, char *, size_t);
58 static int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t);
59 static device_t gpiobus_add_child(device_t, u_int, const char *, int);
60 static void gpiobus_hinted_child(device_t, const char *, int);
65 static void gpiobus_lock_bus(device_t);
66 static void gpiobus_unlock_bus(device_t);
67 static void gpiobus_acquire_bus(device_t, device_t);
68 static void gpiobus_release_bus(device_t, device_t);
69 static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t);
70 static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*);
71 static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*);
72 static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int);
73 static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*);
74 static int gpiobus_pin_toggle(device_t, device_t, uint32_t);
76 #define GPIOBUS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
77 #define GPIOBUS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
78 #define GPIOBUS_LOCK_INIT(_sc) \
79 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
81 #define GPIOBUS_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
82 #define GPIOBUS_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
83 #define GPIOBUS_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
87 gpiobus_print_pins(struct gpiobus_ivar *devi)
89 int range_start, range_stop, need_coma;
96 range_start = range_stop = devi->pins[0];
97 for (i = 1; i < devi->npins; i++) {
98 if (devi->pins[i] != (range_stop + 1)) {
101 if (range_start != range_stop)
102 printf("%d-%d", range_start, range_stop);
104 printf("%d", range_start);
106 range_start = range_stop = devi->pins[i];
115 if (range_start != range_stop)
116 printf("%d-%d", range_start, range_stop);
118 printf("%d", range_start);
122 gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask)
124 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
128 for (i = 0; i < 32; i++) {
134 device_printf(child, "empty pin mask\n");
139 devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF,
146 for (i = 0; i < 32; i++) {
148 if ((mask & (1 << i)) == 0)
151 if (i >= sc->sc_npins) {
153 "invalid pin %d, max: %d\n", i, sc->sc_npins - 1);
154 free(devi->pins, M_DEVBUF);
158 devi->pins[npins++] = i;
160 * Mark pin as mapped and give warning if it's already mapped
162 if (sc->sc_pins_mapped[i]) {
164 "warning: pin %d is already mapped\n", i);
165 free(devi->pins, M_DEVBUF);
168 sc->sc_pins_mapped[i] = 1;
175 gpiobus_probe(device_t dev)
177 device_set_desc(dev, "GPIO bus");
182 gpiobus_attach(device_t dev)
184 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
188 sc->sc_dev = device_get_parent(dev);
189 res = GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins);
194 * Increase to get number of pins
198 KASSERT(sc->sc_npins != 0, ("GPIO device with no pins"));
200 sc->sc_pins_mapped = malloc(sizeof(int) * sc->sc_npins, M_DEVBUF,
203 if (!sc->sc_pins_mapped)
207 GPIOBUS_LOCK_INIT(sc);
210 * Get parent's pins and mark them as unmapped
212 bus_enumerate_hinted_children(dev);
213 return (bus_generic_attach(dev));
217 * Since this is not a self-enumerating bus, and since we always add
218 * children in attach, we have to always delete children here.
221 gpiobus_detach(device_t dev)
223 struct gpiobus_softc *sc;
224 struct gpiobus_ivar *devi;
228 sc = GPIOBUS_SOFTC(dev);
229 KASSERT(mtx_initialized(&sc->sc_mtx),
230 ("gpiobus mutex not initialized"));
231 GPIOBUS_LOCK_DESTROY(sc);
233 if ((err = bus_generic_detach(dev)) != 0)
236 if ((err = device_get_children(dev, &devlist, &ndevs)) != 0)
238 for (i = 0; i < ndevs; i++) {
239 device_delete_child(dev, devlist[i]);
240 devi = GPIOBUS_IVAR(devlist[i]);
242 free(devi->pins, M_DEVBUF);
246 free(devlist, M_TEMP);
248 if (sc->sc_pins_mapped) {
249 free(sc->sc_pins_mapped, M_DEVBUF);
250 sc->sc_pins_mapped = NULL;
257 gpiobus_suspend(device_t dev)
260 return (bus_generic_suspend(dev));
264 gpiobus_resume(device_t dev)
267 return (bus_generic_resume(dev));
271 gpiobus_print_child(device_t dev, device_t child)
273 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
276 retval += bus_print_child_header(dev, child);
277 retval += printf(" at pin(s) ");
278 gpiobus_print_pins(devi);
279 retval += bus_print_child_footer(dev, child);
285 gpiobus_child_location_str(device_t bus, device_t child, char *buf,
289 snprintf(buf, buflen, "pins=?");
294 gpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
303 gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
306 struct gpiobus_ivar *devi;
308 child = device_add_child_ordered(dev, order, name, unit);
311 devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
313 device_delete_child(dev, child);
316 device_set_ivars(child, devi);
321 gpiobus_hinted_child(device_t bus, const char *dname, int dunit)
323 struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus);
324 struct gpiobus_ivar *devi;
329 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
330 devi = GPIOBUS_IVAR(child);
331 resource_int_value(dname, dunit, "pins", &pins);
332 if (gpiobus_parse_pins(sc, child, pins))
333 device_delete_child(bus, child);
337 gpiobus_lock_bus(device_t busdev)
339 struct gpiobus_softc *sc;
341 sc = device_get_softc(busdev);
342 GPIOBUS_ASSERT_UNLOCKED(sc);
347 gpiobus_unlock_bus(device_t busdev)
349 struct gpiobus_softc *sc;
351 sc = device_get_softc(busdev);
352 GPIOBUS_ASSERT_LOCKED(sc);
357 gpiobus_acquire_bus(device_t busdev, device_t child)
359 struct gpiobus_softc *sc;
361 sc = device_get_softc(busdev);
362 GPIOBUS_ASSERT_LOCKED(sc);
365 panic("gpiobus: cannot serialize the access to device.");
366 sc->sc_owner = child;
370 gpiobus_release_bus(device_t busdev, device_t child)
372 struct gpiobus_softc *sc;
374 sc = device_get_softc(busdev);
375 GPIOBUS_ASSERT_LOCKED(sc);
378 panic("gpiobus: releasing unowned bus.");
379 if (sc->sc_owner != child)
380 panic("gpiobus: you don't own the bus. game over.");
386 gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin,
389 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
390 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
392 if (pin >= devi->npins)
395 return GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags);
399 gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin,
402 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
403 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
405 if (pin >= devi->npins)
408 return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags);
412 gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin,
415 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
416 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
418 if (pin >= devi->npins)
421 return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps);
425 gpiobus_pin_set(device_t dev, device_t child, uint32_t pin,
428 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
429 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
431 if (pin >= devi->npins)
434 return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value);
438 gpiobus_pin_get(device_t dev, device_t child, uint32_t pin,
441 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
442 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
444 if (pin >= devi->npins)
447 return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value);
451 gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin)
453 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
454 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
456 if (pin >= devi->npins)
459 return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]);
462 static device_method_t gpiobus_methods[] = {
463 /* Device interface */
464 DEVMETHOD(device_probe, gpiobus_probe),
465 DEVMETHOD(device_attach, gpiobus_attach),
466 DEVMETHOD(device_detach, gpiobus_detach),
467 DEVMETHOD(device_shutdown, bus_generic_shutdown),
468 DEVMETHOD(device_suspend, gpiobus_suspend),
469 DEVMETHOD(device_resume, gpiobus_resume),
472 DEVMETHOD(bus_add_child, gpiobus_add_child),
473 DEVMETHOD(bus_print_child, gpiobus_print_child),
474 DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str),
475 DEVMETHOD(bus_child_location_str, gpiobus_child_location_str),
476 DEVMETHOD(bus_hinted_child, gpiobus_hinted_child),
479 DEVMETHOD(gpiobus_lock_bus, gpiobus_lock_bus),
480 DEVMETHOD(gpiobus_unlock_bus, gpiobus_unlock_bus),
481 DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus),
482 DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus),
483 DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags),
484 DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps),
485 DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags),
486 DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get),
487 DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set),
488 DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle),
493 driver_t gpiobus_driver = {
496 sizeof(struct gpiobus_softc)
499 devclass_t gpiobus_devclass;
501 DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0);
502 MODULE_VERSION(gpiobus, 1);