1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 31 32 #include "opt_platform.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/bus.h> 37 #include <sys/gpio.h> 38 #include <sys/kernel.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/mutex.h> 43 44 #include <dev/gpio/gpiobusvar.h> 45 #include <dev/led/led.h> 46 47 #include "gpiobus_if.h" 48 49 /* 50 * Only one pin for led 51 */ 52 #define GPIOLED_PIN 0 53 54 #define GPIOLED_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 55 #define GPIOLED_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 56 #define GPIOLED_LOCK_INIT(_sc) mtx_init(&(_sc)->sc_mtx, \ 57 device_get_nameunit((_sc)->sc_dev), "gpioled", MTX_DEF) 58 #define GPIOLED_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx) 59 60 struct gpioled_softc 61 { 62 device_t sc_dev; 63 device_t sc_busdev; 64 struct mtx sc_mtx; 65 struct cdev *sc_leddev; 66 int sc_invert; 67 }; 68 69 static void gpioled_control(void *, int); 70 static int gpioled_probe(device_t); 71 static int gpioled_attach(device_t); 72 static int gpioled_detach(device_t); 73 74 static void 75 gpioled_control(void *priv, int onoff) 76 { 77 struct gpioled_softc *sc; 78 79 sc = (struct gpioled_softc *)priv; 80 GPIOLED_LOCK(sc); 81 if (GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, 82 GPIO_PIN_OUTPUT) == 0) { 83 if (sc->sc_invert) 84 onoff = !onoff; 85 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, 86 onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW); 87 } 88 GPIOLED_UNLOCK(sc); 89 } 90 91 static int 92 gpioled_probe(device_t dev) 93 { 94 device_set_desc(dev, "GPIO led"); 95 96 return (BUS_PROBE_DEFAULT); 97 } 98 99 static int 100 gpioled_attach(device_t dev) 101 { 102 struct gpioled_softc *sc; 103 int state; 104 const char *name; 105 106 sc = device_get_softc(dev); 107 sc->sc_dev = dev; 108 sc->sc_busdev = device_get_parent(dev); 109 GPIOLED_LOCK_INIT(sc); 110 111 state = 0; 112 113 if (resource_string_value(device_get_name(dev), 114 device_get_unit(dev), "name", &name)) 115 name = NULL; 116 resource_int_value(device_get_name(dev), 117 device_get_unit(dev), "invert", &sc->sc_invert); 118 resource_int_value(device_get_name(dev), 119 device_get_unit(dev), "state", &state); 120 121 sc->sc_leddev = led_create_state(gpioled_control, sc, name ? name : 122 device_get_nameunit(dev), state); 123 124 return (0); 125 } 126 127 static int 128 gpioled_detach(device_t dev) 129 { 130 struct gpioled_softc *sc; 131 132 sc = device_get_softc(dev); 133 if (sc->sc_leddev) { 134 led_destroy(sc->sc_leddev); 135 sc->sc_leddev = NULL; 136 } 137 GPIOLED_LOCK_DESTROY(sc); 138 return (0); 139 } 140 141 static devclass_t gpioled_devclass; 142 143 static device_method_t gpioled_methods[] = { 144 /* Device interface */ 145 DEVMETHOD(device_probe, gpioled_probe), 146 DEVMETHOD(device_attach, gpioled_attach), 147 DEVMETHOD(device_detach, gpioled_detach), 148 149 DEVMETHOD_END 150 }; 151 152 static driver_t gpioled_driver = { 153 "gpioled", 154 gpioled_methods, 155 sizeof(struct gpioled_softc), 156 }; 157 158 DRIVER_MODULE(gpioled, gpiobus, gpioled_driver, gpioled_devclass, 0, 0); 159 MODULE_DEPEND(gpioled, gpiobus, 1, 1, 1); 160 MODULE_VERSION(gpioled, 1); 161