1 /*- 2 * Copyright (c) 2015 Oleksandr Tymoshenko <gonzo@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 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 #include <sys/sysctl.h> 42 43 #include <dev/fdt/fdt_common.h> 44 #include <dev/ofw/ofw_bus.h> 45 46 #include <dev/gpio/gpiobusvar.h> 47 48 #include "gpiobus_if.h" 49 50 /* 51 * Only one pin for led 52 */ 53 #define GPIOBL_PIN 0 54 55 #define GPIOBL_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 56 #define GPIOBL_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 57 #define GPIOBL_LOCK_INIT(_sc) \ 58 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \ 59 "gpiobacklight", MTX_DEF) 60 #define GPIOBL_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 61 62 struct gpiobacklight_softc 63 { 64 device_t sc_dev; 65 device_t sc_busdev; 66 struct mtx sc_mtx; 67 68 struct sysctl_oid *sc_oid; 69 int sc_brightness; 70 }; 71 72 static int gpiobacklight_sysctl(SYSCTL_HANDLER_ARGS); 73 static void gpiobacklight_update_brightness(struct gpiobacklight_softc *); 74 static int gpiobacklight_probe(device_t); 75 static int gpiobacklight_attach(device_t); 76 static int gpiobacklight_detach(device_t); 77 78 static void 79 gpiobacklight_update_brightness(struct gpiobacklight_softc *sc) 80 { 81 int error; 82 83 GPIOBL_LOCK(sc); 84 error = GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev, 85 GPIOBUS_DONTWAIT); 86 if (error != 0) { 87 GPIOBL_UNLOCK(sc); 88 return; 89 } 90 error = GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, 91 GPIOBL_PIN, GPIO_PIN_OUTPUT); 92 if (error == 0) 93 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOBL_PIN, 94 sc->sc_brightness ? GPIO_PIN_HIGH : GPIO_PIN_LOW); 95 GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev); 96 GPIOBL_UNLOCK(sc); 97 } 98 99 static int 100 gpiobacklight_sysctl(SYSCTL_HANDLER_ARGS) 101 { 102 struct gpiobacklight_softc *sc; 103 int error; 104 int brightness; 105 106 sc = (struct gpiobacklight_softc*)arg1; 107 brightness = sc->sc_brightness; 108 error = sysctl_handle_int(oidp, &brightness, 0, req); 109 110 if (error != 0 || req->newptr == NULL) 111 return (error); 112 113 if (brightness <= 0) 114 sc->sc_brightness = 0; 115 else 116 sc->sc_brightness = 1; 117 118 gpiobacklight_update_brightness(sc); 119 120 return (error); 121 } 122 123 static void 124 gpiobacklight_identify(driver_t *driver, device_t bus) 125 { 126 phandle_t node, root; 127 128 root = OF_finddevice("/"); 129 if (root == 0) 130 return; 131 for (node = OF_child(root); node != 0; node = OF_peer(node)) { 132 if (!fdt_is_compatible_strict(node, "gpio-backlight")) 133 continue; 134 ofw_gpiobus_add_fdt_child(bus, driver->name, node); 135 } 136 } 137 138 static int 139 gpiobacklight_probe(device_t dev) 140 { 141 142 if (!ofw_bus_is_compatible(dev, "gpio-backlight")) 143 return (ENXIO); 144 145 device_set_desc(dev, "GPIO backlight"); 146 147 return (0); 148 } 149 150 static int 151 gpiobacklight_attach(device_t dev) 152 { 153 struct gpiobacklight_softc *sc; 154 struct sysctl_ctx_list *ctx; 155 struct sysctl_oid *tree; 156 phandle_t node; 157 158 sc = device_get_softc(dev); 159 sc->sc_dev = dev; 160 sc->sc_busdev = device_get_parent(dev); 161 162 if ((node = ofw_bus_get_node(dev)) == -1) 163 return (ENXIO); 164 165 GPIOBL_LOCK_INIT(sc); 166 if (OF_hasprop(node, "default-on")) 167 sc->sc_brightness = 1; 168 else 169 sc->sc_brightness = 0; 170 171 /* Init backlight interface */ 172 ctx = device_get_sysctl_ctx(sc->sc_dev); 173 tree = device_get_sysctl_tree(sc->sc_dev); 174 sc->sc_oid = SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 175 "brightness", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 176 gpiobacklight_sysctl, "I", "backlight brightness"); 177 178 gpiobacklight_update_brightness(sc); 179 180 return (0); 181 } 182 183 static int 184 gpiobacklight_detach(device_t dev) 185 { 186 struct gpiobacklight_softc *sc; 187 188 sc = device_get_softc(dev); 189 GPIOBL_LOCK_DESTROY(sc); 190 return (0); 191 } 192 193 static devclass_t gpiobacklight_devclass; 194 195 static device_method_t gpiobacklight_methods[] = { 196 /* Device interface */ 197 DEVMETHOD(device_identify, gpiobacklight_identify), 198 DEVMETHOD(device_probe, gpiobacklight_probe), 199 DEVMETHOD(device_attach, gpiobacklight_attach), 200 DEVMETHOD(device_detach, gpiobacklight_detach), 201 202 DEVMETHOD_END 203 }; 204 205 static driver_t gpiobacklight_driver = { 206 "gpiobacklight", 207 gpiobacklight_methods, 208 sizeof(struct gpiobacklight_softc), 209 }; 210 211 DRIVER_MODULE(gpiobacklight, gpiobus, gpiobacklight_driver, gpiobacklight_devclass, 0, 0); 212