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 (onoff == -1) /* Keep the current state. */ 79 return; 80 if (sc->sc_softinvert) 81 onoff = !onoff; 82 GPIOLED_LOCK(sc); 83 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, 84 onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW); 85 GPIOLED_UNLOCK(sc); 86 } 87 88 static int 89 gpioled_probe(device_t dev) 90 { 91 device_set_desc(dev, "GPIO led"); 92 93 return (BUS_PROBE_DEFAULT); 94 } 95 96 static int 97 gpioled_inv(device_t dev, uint32_t *pin_flags) 98 { 99 struct gpioled_softc *sc; 100 int invert; 101 uint32_t pin_caps; 102 103 sc = device_get_softc(dev); 104 105 if (resource_int_value(device_get_name(dev), 106 device_get_unit(dev), "invert", &invert)) 107 invert = 0; 108 109 if (GPIOBUS_PIN_GETCAPS(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, 110 &pin_caps) != 0) { 111 if (bootverbose) 112 device_printf(sc->sc_dev, "unable to get pin caps\n"); 113 return (-1); 114 } 115 if (pin_caps & GPIO_PIN_INVOUT) 116 *pin_flags &= ~GPIO_PIN_INVOUT; 117 sc->sc_softinvert = 0; 118 if (invert) { 119 const char *invmode; 120 121 if (resource_string_value(device_get_name(dev), 122 device_get_unit(dev), "invmode", &invmode)) 123 invmode = NULL; 124 125 if (invmode) { 126 if (!strcmp(invmode, "sw")) 127 sc->sc_softinvert = 1; 128 else if (!strcmp(invmode, "hw")) { 129 if (pin_caps & GPIO_PIN_INVOUT) 130 *pin_flags |= GPIO_PIN_INVOUT; 131 else { 132 device_printf(sc->sc_dev, "hardware pin inversion not supported\n"); 133 return (-1); 134 } 135 } else { 136 if (strcmp(invmode, "auto") != 0) 137 device_printf(sc->sc_dev, "invalid pin inversion mode\n"); 138 invmode = NULL; 139 } 140 } 141 /* 142 * auto inversion mode: use hardware support if available, else fallback to 143 * software emulation. 144 */ 145 if (invmode == NULL) { 146 if (pin_caps & GPIO_PIN_INVOUT) 147 *pin_flags |= GPIO_PIN_INVOUT; 148 else 149 sc->sc_softinvert = 1; 150 } 151 } 152 MPASS(!invert || 153 (((*pin_flags & GPIO_PIN_INVOUT) != 0) && !sc->sc_softinvert) || 154 (((*pin_flags & GPIO_PIN_INVOUT) == 0) && sc->sc_softinvert)); 155 return (invert); 156 } 157 158 static int 159 gpioled_attach(device_t dev) 160 { 161 struct gpioled_softc *sc; 162 int state; 163 const char *name; 164 uint32_t pin_flags; 165 int invert; 166 167 sc = device_get_softc(dev); 168 sc->sc_dev = dev; 169 sc->sc_busdev = device_get_parent(dev); 170 GPIOLED_LOCK_INIT(sc); 171 172 if (resource_string_value(device_get_name(dev), 173 device_get_unit(dev), "name", &name)) 174 name = NULL; 175 176 if (resource_int_value(device_get_name(dev), 177 device_get_unit(dev), "state", &state)) 178 state = 0; 179 180 pin_flags = GPIO_PIN_OUTPUT; 181 invert = gpioled_inv(dev, &pin_flags); 182 if (invert < 0) 183 return (ENXIO); 184 device_printf(sc->sc_dev, "state %d invert %s\n", 185 state, (invert ? (sc->sc_softinvert ? "sw" : "hw") : "no")); 186 if (GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, 187 pin_flags) != 0) { 188 if (bootverbose) 189 device_printf(sc->sc_dev, "unable to set pin flags, %#x\n", pin_flags); 190 return (ENXIO); 191 } 192 193 sc->sc_leddev = led_create_state(gpioled_control, sc, name ? name : 194 device_get_nameunit(dev), state); 195 196 return (0); 197 } 198 199 static int 200 gpioled_detach(device_t dev) 201 { 202 struct gpioled_softc *sc; 203 204 sc = device_get_softc(dev); 205 if (sc->sc_leddev) { 206 led_destroy(sc->sc_leddev); 207 sc->sc_leddev = NULL; 208 } 209 GPIOLED_LOCK_DESTROY(sc); 210 return (0); 211 } 212 213 static device_method_t gpioled_methods[] = { 214 /* Device interface */ 215 DEVMETHOD(device_probe, gpioled_probe), 216 DEVMETHOD(device_attach, gpioled_attach), 217 DEVMETHOD(device_detach, gpioled_detach), 218 219 DEVMETHOD_END 220 }; 221 222 static driver_t gpioled_driver = { 223 "gpioled", 224 gpioled_methods, 225 sizeof(struct gpioled_softc), 226 }; 227 228 DRIVER_MODULE(gpioled, gpiobus, gpioled_driver, 0, 0); 229 MODULE_DEPEND(gpioled, gpiobus, 1, 1, 1); 230 MODULE_VERSION(gpioled, 1); 231