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 "opt_platform.h" 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/gpio.h> 36 #include <sys/kernel.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/mutex.h> 41 42 #include <dev/gpio/gpiobusvar.h> 43 #include <dev/led/led.h> 44 45 #include "gpiobus_if.h" 46 47 /* 48 * Only one pin for led 49 */ 50 #define GPIOLED_PIN 0 51 52 #define GPIOLED_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 53 #define GPIOLED_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 54 #define GPIOLED_LOCK_INIT(_sc) mtx_init(&(_sc)->sc_mtx, \ 55 device_get_nameunit((_sc)->sc_dev), "gpioled", MTX_DEF) 56 #define GPIOLED_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx) 57 58 struct gpioled_softc 59 { 60 device_t sc_dev; 61 device_t sc_busdev; 62 struct mtx sc_mtx; 63 struct cdev *sc_leddev; 64 int sc_softinvert; 65 }; 66 67 static void gpioled_control(void *, int); 68 static int gpioled_probe(device_t); 69 static int gpioled_attach(device_t); 70 static int gpioled_detach(device_t); 71 72 static void 73 gpioled_control(void *priv, int onoff) 74 { 75 struct gpioled_softc *sc; 76 77 sc = (struct gpioled_softc *)priv; 78 if (sc->sc_softinvert) 79 onoff = !onoff; 80 GPIOLED_LOCK(sc); 81 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, 82 onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW); 83 GPIOLED_UNLOCK(sc); 84 } 85 86 static int 87 gpioled_probe(device_t dev) 88 { 89 device_set_desc(dev, "GPIO led"); 90 91 return (BUS_PROBE_DEFAULT); 92 } 93 94 static int 95 gpioled_inv(device_t dev, uint32_t *pin_flags) 96 { 97 struct gpioled_softc *sc; 98 int invert; 99 uint32_t pin_caps; 100 101 sc = device_get_softc(dev); 102 103 if (resource_int_value(device_get_name(dev), 104 device_get_unit(dev), "invert", &invert)) 105 invert = 0; 106 107 if (GPIOBUS_PIN_GETCAPS(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, 108 &pin_caps) != 0) { 109 if (bootverbose) 110 device_printf(sc->sc_dev, "unable to get pin caps\n"); 111 return (-1); 112 } 113 if (pin_caps & GPIO_PIN_INVOUT) 114 *pin_flags &= ~GPIO_PIN_INVOUT; 115 sc->sc_softinvert = 0; 116 if (invert) { 117 const char *invmode; 118 119 if (resource_string_value(device_get_name(dev), 120 device_get_unit(dev), "invmode", &invmode)) 121 invmode = NULL; 122 123 if (invmode) { 124 if (!strcmp(invmode, "sw")) 125 sc->sc_softinvert = 1; 126 else if (!strcmp(invmode, "hw")) { 127 if (pin_caps & GPIO_PIN_INVOUT) 128 *pin_flags |= GPIO_PIN_INVOUT; 129 else { 130 device_printf(sc->sc_dev, "hardware pin inversion not supported\n"); 131 return (-1); 132 } 133 } else { 134 if (strcmp(invmode, "auto") != 0) 135 device_printf(sc->sc_dev, "invalid pin inversion mode\n"); 136 invmode = NULL; 137 } 138 } 139 /* 140 * auto inversion mode: use hardware support if available, else fallback to 141 * software emulation. 142 */ 143 if (invmode == NULL) { 144 if (pin_caps & GPIO_PIN_INVOUT) 145 *pin_flags |= GPIO_PIN_INVOUT; 146 else 147 sc->sc_softinvert = 1; 148 } 149 } 150 MPASS(!invert || 151 (((*pin_flags & GPIO_PIN_INVOUT) != 0) && !sc->sc_softinvert) || 152 (((*pin_flags & GPIO_PIN_INVOUT) == 0) && sc->sc_softinvert)); 153 return (invert); 154 } 155 156 static int 157 gpioled_attach(device_t dev) 158 { 159 struct gpioled_softc *sc; 160 int state; 161 const char *name; 162 uint32_t pin_flags; 163 int invert; 164 165 sc = device_get_softc(dev); 166 sc->sc_dev = dev; 167 sc->sc_busdev = device_get_parent(dev); 168 GPIOLED_LOCK_INIT(sc); 169 170 if (resource_string_value(device_get_name(dev), 171 device_get_unit(dev), "name", &name)) 172 name = NULL; 173 174 if (resource_int_value(device_get_name(dev), 175 device_get_unit(dev), "state", &state)) 176 state = 0; 177 178 pin_flags = GPIO_PIN_OUTPUT; 179 invert = gpioled_inv(dev, &pin_flags); 180 if (invert < 0) 181 return (ENXIO); 182 device_printf(sc->sc_dev, "state %d invert %s\n", 183 state, (invert ? (sc->sc_softinvert ? "sw" : "hw") : "no")); 184 if (GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, 185 pin_flags) != 0) { 186 if (bootverbose) 187 device_printf(sc->sc_dev, "unable to set pin flags, %#x\n", pin_flags); 188 return (ENXIO); 189 } 190 191 sc->sc_leddev = led_create_state(gpioled_control, sc, name ? name : 192 device_get_nameunit(dev), state); 193 194 return (0); 195 } 196 197 static int 198 gpioled_detach(device_t dev) 199 { 200 struct gpioled_softc *sc; 201 202 sc = device_get_softc(dev); 203 if (sc->sc_leddev) { 204 led_destroy(sc->sc_leddev); 205 sc->sc_leddev = NULL; 206 } 207 GPIOLED_LOCK_DESTROY(sc); 208 return (0); 209 } 210 211 static device_method_t gpioled_methods[] = { 212 /* Device interface */ 213 DEVMETHOD(device_probe, gpioled_probe), 214 DEVMETHOD(device_attach, gpioled_attach), 215 DEVMETHOD(device_detach, gpioled_detach), 216 217 DEVMETHOD_END 218 }; 219 220 static driver_t gpioled_driver = { 221 "gpioled", 222 gpioled_methods, 223 sizeof(struct gpioled_softc), 224 }; 225 226 DRIVER_MODULE(gpioled, gpiobus, gpioled_driver, 0, 0); 227 MODULE_DEPEND(gpioled, gpiobus, 1, 1, 1); 228 MODULE_VERSION(gpioled, 1); 229