Index: stable/10/sys/dev/gpio/gpiobus.c =================================================================== --- stable/10/sys/dev/gpio/gpiobus.c (revision 278782) +++ stable/10/sys/dev/gpio/gpiobus.c (revision 278783) @@ -1,495 +1,481 @@ /*- * Copyright (c) 2009 Oleksandr Tymoshenko * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include "gpiobus_if.h" static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int); static int gpiobus_probe(device_t); static int gpiobus_attach(device_t); static int gpiobus_detach(device_t); static int gpiobus_suspend(device_t); static int gpiobus_resume(device_t); static int gpiobus_print_child(device_t, device_t); static int gpiobus_child_location_str(device_t, device_t, char *, size_t); static int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t); static device_t gpiobus_add_child(device_t, u_int, const char *, int); static void gpiobus_hinted_child(device_t, const char *, int); /* * GPIOBUS interface */ -static void gpiobus_lock_bus(device_t); -static void gpiobus_unlock_bus(device_t); -static void gpiobus_acquire_bus(device_t, device_t); +static int gpiobus_acquire_bus(device_t, device_t, int); static void gpiobus_release_bus(device_t, device_t); static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t); static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*); static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*); static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int); static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*); static int gpiobus_pin_toggle(device_t, device_t, uint32_t); void gpiobus_print_pins(struct gpiobus_ivar *devi) { int range_start, range_stop, need_coma; int i; if (devi->npins == 0) return; need_coma = 0; range_start = range_stop = devi->pins[0]; for (i = 1; i < devi->npins; i++) { if (devi->pins[i] != (range_stop + 1)) { if (need_coma) printf(","); if (range_start != range_stop) printf("%d-%d", range_start, range_stop); else printf("%d", range_start); range_start = range_stop = devi->pins[i]; need_coma = 1; } else range_stop++; } if (need_coma) printf(","); if (range_start != range_stop) printf("%d-%d", range_start, range_stop); else printf("%d", range_start); } int gpiobus_init_softc(device_t dev) { struct gpiobus_softc *sc; sc = GPIOBUS_SOFTC(dev); sc->sc_busdev = dev; sc->sc_dev = device_get_parent(dev); if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0) return (ENXIO); KASSERT(sc->sc_npins != 0, ("GPIO device with no pins")); /* Pins = GPIO_PIN_MAX() + 1 */ sc->sc_npins++; sc->sc_pins_mapped = malloc(sizeof(int) * sc->sc_npins, M_DEVBUF, M_NOWAIT | M_ZERO); if (sc->sc_pins_mapped == NULL) return (ENOMEM); /* Initialize the bus lock. */ GPIOBUS_LOCK_INIT(sc); return (0); } static int gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask) { struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); int i, npins; npins = 0; for (i = 0; i < 32; i++) { if (mask & (1 << i)) npins++; } if (npins == 0) { device_printf(child, "empty pin mask\n"); return (EINVAL); } devi->npins = npins; devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF, M_NOWAIT | M_ZERO); if (!devi->pins) return (ENOMEM); npins = 0; for (i = 0; i < 32; i++) { if ((mask & (1 << i)) == 0) continue; if (i >= sc->sc_npins) { device_printf(child, "invalid pin %d, max: %d\n", i, sc->sc_npins - 1); free(devi->pins, M_DEVBUF); return (EINVAL); } devi->pins[npins++] = i; /* * Mark pin as mapped and give warning if it's already mapped */ if (sc->sc_pins_mapped[i]) { device_printf(child, "warning: pin %d is already mapped\n", i); free(devi->pins, M_DEVBUF); return (EINVAL); } sc->sc_pins_mapped[i] = 1; } return (0); } static int gpiobus_probe(device_t dev) { device_set_desc(dev, "GPIO bus"); return (BUS_PROBE_GENERIC); } static int gpiobus_attach(device_t dev) { int err; err = gpiobus_init_softc(dev); if (err != 0) return (err); /* * Get parent's pins and mark them as unmapped */ bus_generic_probe(dev); bus_enumerate_hinted_children(dev); return (bus_generic_attach(dev)); } /* * Since this is not a self-enumerating bus, and since we always add * children in attach, we have to always delete children here. */ static int gpiobus_detach(device_t dev) { struct gpiobus_softc *sc; struct gpiobus_ivar *devi; device_t *devlist; int i, err, ndevs; sc = GPIOBUS_SOFTC(dev); KASSERT(mtx_initialized(&sc->sc_mtx), ("gpiobus mutex not initialized")); GPIOBUS_LOCK_DESTROY(sc); if ((err = bus_generic_detach(dev)) != 0) return (err); if ((err = device_get_children(dev, &devlist, &ndevs)) != 0) return (err); for (i = 0; i < ndevs; i++) { device_delete_child(dev, devlist[i]); devi = GPIOBUS_IVAR(devlist[i]); if (devi->pins) { free(devi->pins, M_DEVBUF); devi->pins = NULL; } } free(devlist, M_TEMP); if (sc->sc_pins_mapped) { free(sc->sc_pins_mapped, M_DEVBUF); sc->sc_pins_mapped = NULL; } return (0); } static int gpiobus_suspend(device_t dev) { return (bus_generic_suspend(dev)); } static int gpiobus_resume(device_t dev) { return (bus_generic_resume(dev)); } static int gpiobus_print_child(device_t dev, device_t child) { struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); int retval = 0; retval += bus_print_child_header(dev, child); retval += printf(" at pin(s) "); gpiobus_print_pins(devi); retval += bus_print_child_footer(dev, child); return (retval); } static int gpiobus_child_location_str(device_t bus, device_t child, char *buf, size_t buflen) { snprintf(buf, buflen, "pins=?"); return (0); } static int gpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf, size_t buflen) { *buf = '\0'; return (0); } static device_t gpiobus_add_child(device_t dev, u_int order, const char *name, int unit) { device_t child; struct gpiobus_ivar *devi; child = device_add_child_ordered(dev, order, name, unit); if (child == NULL) return (child); devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO); if (devi == NULL) { device_delete_child(dev, child); return (0); } device_set_ivars(child, devi); return (child); } static void gpiobus_hinted_child(device_t bus, const char *dname, int dunit) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus); struct gpiobus_ivar *devi; device_t child; int pins; child = BUS_ADD_CHILD(bus, 0, dname, dunit); devi = GPIOBUS_IVAR(child); resource_int_value(dname, dunit, "pins", &pins); if (gpiobus_parse_pins(sc, child, pins)) device_delete_child(bus, child); } -static void -gpiobus_lock_bus(device_t busdev) +static int +gpiobus_acquire_bus(device_t busdev, device_t child, int how) { struct gpiobus_softc *sc; sc = device_get_softc(busdev); GPIOBUS_ASSERT_UNLOCKED(sc); GPIOBUS_LOCK(sc); -} - -static void -gpiobus_unlock_bus(device_t busdev) -{ - struct gpiobus_softc *sc; - - sc = device_get_softc(busdev); - GPIOBUS_ASSERT_LOCKED(sc); + if (sc->sc_owner != NULL) { + if (how == GPIOBUS_DONTWAIT) { + GPIOBUS_UNLOCK(sc); + return (EWOULDBLOCK); + } + while (sc->sc_owner != NULL) + mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0); + } + sc->sc_owner = child; GPIOBUS_UNLOCK(sc); -} -static void -gpiobus_acquire_bus(device_t busdev, device_t child) -{ - struct gpiobus_softc *sc; - - sc = device_get_softc(busdev); - GPIOBUS_ASSERT_LOCKED(sc); - - if (sc->sc_owner) - panic("gpiobus: cannot serialize the access to device."); - sc->sc_owner = child; + return (0); } static void gpiobus_release_bus(device_t busdev, device_t child) { struct gpiobus_softc *sc; sc = device_get_softc(busdev); - GPIOBUS_ASSERT_LOCKED(sc); - - if (!sc->sc_owner) + GPIOBUS_ASSERT_UNLOCKED(sc); + GPIOBUS_LOCK(sc); + if (sc->sc_owner == NULL) panic("gpiobus: releasing unowned bus."); if (sc->sc_owner != child) panic("gpiobus: you don't own the bus. game over."); - sc->sc_owner = NULL; + wakeup(sc); + GPIOBUS_UNLOCK(sc); } static int gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin, uint32_t flags) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); if (pin >= devi->npins) return (EINVAL); return GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags); } static int gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin, uint32_t *flags) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); if (pin >= devi->npins) return (EINVAL); return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags); } static int gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin, uint32_t *caps) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); if (pin >= devi->npins) return (EINVAL); return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps); } static int gpiobus_pin_set(device_t dev, device_t child, uint32_t pin, unsigned int value) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); if (pin >= devi->npins) return (EINVAL); return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value); } static int gpiobus_pin_get(device_t dev, device_t child, uint32_t pin, unsigned int *value) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); if (pin >= devi->npins) return (EINVAL); return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value); } static int gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin) { struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); if (pin >= devi->npins) return (EINVAL); return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]); } static device_method_t gpiobus_methods[] = { /* Device interface */ DEVMETHOD(device_probe, gpiobus_probe), DEVMETHOD(device_attach, gpiobus_attach), DEVMETHOD(device_detach, gpiobus_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), DEVMETHOD(device_suspend, gpiobus_suspend), DEVMETHOD(device_resume, gpiobus_resume), /* Bus interface */ DEVMETHOD(bus_add_child, gpiobus_add_child), DEVMETHOD(bus_print_child, gpiobus_print_child), DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str), DEVMETHOD(bus_child_location_str, gpiobus_child_location_str), DEVMETHOD(bus_hinted_child, gpiobus_hinted_child), /* GPIO protocol */ - DEVMETHOD(gpiobus_lock_bus, gpiobus_lock_bus), - DEVMETHOD(gpiobus_unlock_bus, gpiobus_unlock_bus), DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus), DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus), DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags), DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps), DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags), DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get), DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set), DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle), DEVMETHOD_END }; driver_t gpiobus_driver = { "gpiobus", gpiobus_methods, sizeof(struct gpiobus_softc) }; devclass_t gpiobus_devclass; DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0); MODULE_VERSION(gpiobus, 1); Index: stable/10/sys/dev/gpio/gpiobus_if.m =================================================================== --- stable/10/sys/dev/gpio/gpiobus_if.m (revision 278782) +++ stable/10/sys/dev/gpio/gpiobus_if.m (revision 278783) @@ -1,121 +1,108 @@ #- # Copyright (c) 2009 Oleksandr Tymoshenko # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # $FreeBSD$ # #include #include INTERFACE gpiobus; # -# Lock the gpio bus -# -METHOD void lock_bus { - device_t busdev; -}; - -# -# Unlock the gpio bus -# -METHOD void unlock_bus { - device_t busdev; -}; - -# # Dedicate the gpio bus control for a child # -METHOD void acquire_bus { +METHOD int acquire_bus { device_t busdev; device_t dev; + int how; }; # # Release the bus # METHOD void release_bus { device_t busdev; device_t dev; }; # # Set value of pin specifed by pin_num # METHOD int pin_set { device_t dev; device_t child; uint32_t pin_num; uint32_t pin_value; }; # # Get value of pin specifed by pin_num # METHOD int pin_get { device_t dev; device_t child; uint32_t pin_num; uint32_t *pin_value; }; # # Toggle value of pin specifed by pin_num # METHOD int pin_toggle { device_t dev; device_t child; uint32_t pin_num; }; # # Get pin capabilities # METHOD int pin_getcaps { device_t dev; device_t child; uint32_t pin_num; uint32_t *caps; }; # # Get pin flags # METHOD int pin_getflags { device_t dev; device_t child; uint32_t pin_num; uint32_t *flags; }; # # Set current configuration and capabilities # METHOD int pin_setflags { device_t dev; device_t child; uint32_t pin_num; uint32_t flags; }; Index: stable/10/sys/dev/gpio/gpiobusvar.h =================================================================== --- stable/10/sys/dev/gpio/gpiobusvar.h (revision 278782) +++ stable/10/sys/dev/gpio/gpiobusvar.h (revision 278783) @@ -1,96 +1,99 @@ /*- * Copyright (c) 2009 Oleksandr Tymoshenko * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ * */ #ifndef __GPIOBUS_H__ #define __GPIOBUS_H__ #include "opt_platform.h" #include #include #ifdef FDT #include #endif #include "gpio_if.h" #ifdef FDT #define GPIOBUS_IVAR(d) (struct gpiobus_ivar *) \ &((struct ofw_gpiobus_devinfo *)device_get_ivars(d))->opd_dinfo #else #define GPIOBUS_IVAR(d) (struct gpiobus_ivar *) device_get_ivars(d) #endif #define GPIOBUS_SOFTC(d) (struct gpiobus_softc *) device_get_softc(d) #define GPIOBUS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) #define GPIOBUS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) #define GPIOBUS_LOCK_INIT(_sc) mtx_init(&_sc->sc_mtx, \ device_get_nameunit(_sc->sc_dev), "gpiobus", MTX_DEF) #define GPIOBUS_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx) #define GPIOBUS_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED) #define GPIOBUS_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED) +#define GPIOBUS_WAIT 1 +#define GPIOBUS_DONTWAIT 2 + struct gpiobus_softc { struct mtx sc_mtx; /* bus mutex */ device_t sc_busdev; /* bus device */ device_t sc_owner; /* bus owner */ device_t sc_dev; /* driver device */ int sc_npins; /* total pins on bus */ int *sc_pins_mapped; /* mark mapped pins */ }; struct gpiobus_ivar { uint32_t npins; /* pins total */ uint32_t *flags; /* pins flags */ uint32_t *pins; /* pins map */ }; #ifdef FDT struct ofw_gpiobus_devinfo { struct gpiobus_ivar opd_dinfo; struct ofw_bus_devinfo opd_obdinfo; }; static __inline int gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent, int gcells, pcell_t *gpios, uint32_t *pin, uint32_t *flags) { return (GPIO_MAP_GPIOS(bus, dev, gparent, gcells, gpios, pin, flags)); } device_t ofw_gpiobus_add_fdt_child(device_t, phandle_t); #endif void gpiobus_print_pins(struct gpiobus_ivar *); int gpiobus_init_softc(device_t); extern driver_t gpiobus_driver; #endif /* __GPIOBUS_H__ */ Index: stable/10/sys/dev/gpio/gpioiic.c =================================================================== --- stable/10/sys/dev/gpio/gpioiic.c (revision 278782) +++ stable/10/sys/dev/gpio/gpioiic.c (revision 278783) @@ -1,287 +1,287 @@ /*- * Copyright (c) 2009 Oleksandr Tymoshenko * Copyright (c) 2010 Luiz Otavio O Souza * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "opt_platform.h" #include #include #include #include #include #include #include #include "gpiobus_if.h" #ifdef FDT #include #include #include #endif #include #include #include #include "iicbb_if.h" #define SCL_PIN_DEFAULT 0 /* default index of SCL pin on gpiobus */ #define SDA_PIN_DEFAULT 1 struct gpioiic_softc { device_t sc_dev; device_t sc_busdev; int scl_pin; int sda_pin; }; static int gpioiic_probe(device_t); static int gpioiic_attach(device_t); /* iicbb interface */ static void gpioiic_reset_bus(device_t); static int gpioiic_callback(device_t, int, caddr_t); static void gpioiic_setsda(device_t, int); static void gpioiic_setscl(device_t, int); static int gpioiic_getsda(device_t); static int gpioiic_getscl(device_t); static int gpioiic_reset(device_t, u_char, u_char, u_char *); static int gpioiic_probe(device_t dev) { #ifdef FDT if (!ofw_bus_is_compatible(dev, "gpioiic")) return (ENXIO); #endif device_set_desc(dev, "GPIO I2C bit-banging driver"); return (0); } static int gpioiic_attach(device_t dev) { device_t bitbang; #ifdef FDT phandle_t node; pcell_t pin; #endif struct gpiobus_ivar *devi; struct gpioiic_softc *sc; sc = device_get_softc(dev); sc->sc_dev = dev; sc->sc_busdev = device_get_parent(dev); if (resource_int_value(device_get_name(dev), device_get_unit(dev), "scl", &sc->scl_pin)) sc->scl_pin = SCL_PIN_DEFAULT; if (resource_int_value(device_get_name(dev), device_get_unit(dev), "sda", &sc->sda_pin)) sc->sda_pin = SDA_PIN_DEFAULT; #ifdef FDT if ((node = ofw_bus_get_node(dev)) == -1) return (ENXIO); if (OF_getencprop(node, "scl", &pin, sizeof(pin)) > 0) sc->scl_pin = (int)pin; if (OF_getencprop(node, "sda", &pin, sizeof(pin)) > 0) sc->sda_pin = (int)pin; #endif if (sc->scl_pin < 0 || sc->scl_pin > 1) sc->scl_pin = SCL_PIN_DEFAULT; if (sc->sda_pin < 0 || sc->sda_pin > 1) sc->sda_pin = SDA_PIN_DEFAULT; devi = GPIOBUS_IVAR(dev); device_printf(dev, "SCL pin: %d, SDA pin: %d\n", devi->pins[sc->scl_pin], devi->pins[sc->sda_pin]); /* add generic bit-banging code */ bitbang = device_add_child(dev, "iicbb", -1); device_probe_and_attach(bitbang); return (0); } /* * Reset bus by setting SDA first and then SCL. * Must always be called with gpio bus locked. */ static void gpioiic_reset_bus(device_t dev) { struct gpioiic_softc *sc = device_get_softc(dev); GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin, GPIO_PIN_INPUT); GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin, GPIO_PIN_INPUT); } static int gpioiic_callback(device_t dev, int index, caddr_t data) { struct gpioiic_softc *sc = device_get_softc(dev); - int error = 0; + int error, how; + how = GPIOBUS_DONTWAIT; + if (data != NULL && (int)*data == IIC_WAIT) + how = GPIOBUS_WAIT; + error = 0; switch (index) { case IIC_REQUEST_BUS: - GPIOBUS_LOCK_BUS(sc->sc_busdev); - GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev); - GPIOBUS_UNLOCK_BUS(sc->sc_busdev); + error = GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev, how); break; case IIC_RELEASE_BUS: - GPIOBUS_LOCK_BUS(sc->sc_busdev); GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev); - GPIOBUS_UNLOCK_BUS(sc->sc_busdev); break; default: error = EINVAL; } return (error); } static void gpioiic_setsda(device_t dev, int val) { struct gpioiic_softc *sc = device_get_softc(dev); if (val == 0) { GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, 0); GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin, GPIO_PIN_OUTPUT); } else { GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin, GPIO_PIN_INPUT); } } static void gpioiic_setscl(device_t dev, int val) { struct gpioiic_softc *sc = device_get_softc(dev); if (val == 0) { GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, 0); GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin, GPIO_PIN_OUTPUT); } else { GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin, GPIO_PIN_INPUT); } } static int gpioiic_getscl(device_t dev) { struct gpioiic_softc *sc = device_get_softc(dev); unsigned int val; GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin, GPIO_PIN_INPUT); GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, &val); return ((int)val); } static int gpioiic_getsda(device_t dev) { struct gpioiic_softc *sc = device_get_softc(dev); unsigned int val; GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin, GPIO_PIN_INPUT); GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, &val); return ((int)val); } static int gpioiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) { struct gpioiic_softc *sc; sc = device_get_softc(dev); gpioiic_reset_bus(sc->sc_dev); return (IIC_ENOADDR); } #ifdef FDT static phandle_t gpioiic_get_node(device_t bus, device_t dev) { /* We only have one child, the iicbb, which needs our own node. */ return (ofw_bus_get_node(bus)); } #endif static devclass_t gpioiic_devclass; static device_method_t gpioiic_methods[] = { /* Device interface */ DEVMETHOD(device_probe, gpioiic_probe), DEVMETHOD(device_attach, gpioiic_attach), DEVMETHOD(device_detach, bus_generic_detach), /* iicbb interface */ DEVMETHOD(iicbb_callback, gpioiic_callback), DEVMETHOD(iicbb_setsda, gpioiic_setsda), DEVMETHOD(iicbb_setscl, gpioiic_setscl), DEVMETHOD(iicbb_getsda, gpioiic_getsda), DEVMETHOD(iicbb_getscl, gpioiic_getscl), DEVMETHOD(iicbb_reset, gpioiic_reset), #ifdef FDT /* OFW bus interface */ DEVMETHOD(ofw_bus_get_node, gpioiic_get_node), #endif { 0, 0 } }; static driver_t gpioiic_driver = { "gpioiic", gpioiic_methods, sizeof(struct gpioiic_softc), }; DRIVER_MODULE(gpioiic, gpiobus, gpioiic_driver, gpioiic_devclass, 0, 0); DRIVER_MODULE(iicbb, gpioiic, iicbb_driver, iicbb_devclass, 0, 0); MODULE_DEPEND(gpioiic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER); MODULE_DEPEND(gpioiic, gpiobus, 1, 1, 1); Index: stable/10/sys/dev/gpio/gpioled.c =================================================================== --- stable/10/sys/dev/gpio/gpioled.c (revision 278782) +++ stable/10/sys/dev/gpio/gpioled.c (revision 278783) @@ -1,228 +1,233 @@ /*- * Copyright (c) 2009 Oleksandr Tymoshenko * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "opt_platform.h" #include #include -#include #include -#include +#include #include -#include #include #include #include #include #ifdef FDT #include -#include #include #endif +#include #include -#include + #include "gpiobus_if.h" /* * Only one pin for led */ #define GPIOLED_PIN 0 #define GPIOLED_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) #define GPIOLED_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) #define GPIOLED_LOCK_INIT(_sc) \ mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \ "gpioled", MTX_DEF) #define GPIOLED_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); struct gpioled_softc { device_t sc_dev; device_t sc_busdev; struct mtx sc_mtx; struct cdev *sc_leddev; }; static void gpioled_control(void *, int); static int gpioled_probe(device_t); static int gpioled_attach(device_t); static int gpioled_detach(device_t); static void gpioled_control(void *priv, int onoff) { - struct gpioled_softc *sc = priv; + int error; + struct gpioled_softc *sc; + + sc = (struct gpioled_softc *)priv; GPIOLED_LOCK(sc); - GPIOBUS_LOCK_BUS(sc->sc_busdev); - GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev); - GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, - GPIO_PIN_OUTPUT); - GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, - onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW); + error = GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev, + GPIOBUS_DONTWAIT); + if (error != 0) { + GPIOLED_UNLOCK(sc); + return; + } + error = GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, + GPIOLED_PIN, GPIO_PIN_OUTPUT); + if (error == 0) + GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, + onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW); GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev); - GPIOBUS_UNLOCK_BUS(sc->sc_busdev); GPIOLED_UNLOCK(sc); } #ifdef FDT static void gpioled_identify(driver_t *driver, device_t bus) { phandle_t child, leds, root; root = OF_finddevice("/"); if (root == 0) return; leds = fdt_find_compatible(root, "gpio-leds", 1); if (leds == 0) return; /* Traverse the 'gpio-leds' node and add its children. */ for (child = OF_child(leds); child != 0; child = OF_peer(child)) if (ofw_gpiobus_add_fdt_child(bus, child) == NULL) continue; } #endif static int gpioled_probe(device_t dev) { #ifdef FDT int match; phandle_t node; char *compat; /* * We can match against our own node compatible string and also against * our parent node compatible string. The first is normally used to * describe leds on a gpiobus and the later when there is a common node * compatible with 'gpio-leds' which is used to concentrate all the * leds nodes on the dts. */ match = 0; if (ofw_bus_is_compatible(dev, "gpioled")) match = 1; if (match == 0) { if ((node = ofw_bus_get_node(dev)) == -1) return (ENXIO); if ((node = OF_parent(node)) == -1) return (ENXIO); if (OF_getprop_alloc(node, "compatible", 1, (void **)&compat) == -1) return (ENXIO); if (strcasecmp(compat, "gpio-leds") == 0) match = 1; free(compat, M_OFWPROP); } if (match == 0) return (ENXIO); #endif device_set_desc(dev, "GPIO led"); return (0); } static int gpioled_attach(device_t dev) { struct gpioled_softc *sc; #ifdef FDT phandle_t node; char *name; #else const char *name; #endif sc = device_get_softc(dev); sc->sc_dev = dev; sc->sc_busdev = device_get_parent(dev); GPIOLED_LOCK_INIT(sc); #ifdef FDT name = NULL; if ((node = ofw_bus_get_node(dev)) == -1) return (ENXIO); if (OF_getprop_alloc(node, "label", 1, (void **)&name) == -1) OF_getprop_alloc(node, "name", 1, (void **)&name); #else if (resource_string_value(device_get_name(dev), device_get_unit(dev), "name", &name)) name = NULL; #endif sc->sc_leddev = led_create(gpioled_control, sc, name ? name : device_get_nameunit(dev)); #ifdef FDT if (name != NULL) free(name, M_OFWPROP); #endif return (0); } static int gpioled_detach(device_t dev) { struct gpioled_softc *sc; sc = device_get_softc(dev); if (sc->sc_leddev) { led_destroy(sc->sc_leddev); sc->sc_leddev = NULL; } GPIOLED_LOCK_DESTROY(sc); return (0); } static devclass_t gpioled_devclass; static device_method_t gpioled_methods[] = { /* Device interface */ #ifdef FDT DEVMETHOD(device_identify, gpioled_identify), #endif DEVMETHOD(device_probe, gpioled_probe), DEVMETHOD(device_attach, gpioled_attach), DEVMETHOD(device_detach, gpioled_detach), { 0, 0 } }; static driver_t gpioled_driver = { "gpioled", gpioled_methods, sizeof(struct gpioled_softc), }; DRIVER_MODULE(gpioled, gpiobus, gpioled_driver, gpioled_devclass, 0, 0); Index: stable/10 =================================================================== --- stable/10 (revision 278782) +++ stable/10 (revision 278783) Property changes on: stable/10 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r273917,273926