1 /*- 2 * Copyright (c) 2009 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 42 #include <dev/fdt/fdt_common.h> 43 #include <dev/ofw/ofw_bus.h> 44 45 #include <dev/gpio/gpiobusvar.h> 46 #include <dev/led/led.h> 47 48 #include "gpiobus_if.h" 49 50 struct gpioled 51 { 52 struct gpioleds_softc *parent_sc; 53 gpio_pin_t pin; 54 struct cdev *leddev; 55 }; 56 57 struct gpioleds_softc 58 { 59 device_t sc_dev; 60 device_t sc_busdev; 61 struct gpioled *sc_leds; 62 int sc_total_leds; 63 }; 64 65 static void gpioled_control(void *, int); 66 static int gpioled_probe(device_t); 67 static int gpioled_attach(device_t); 68 static int gpioled_detach(device_t); 69 70 static void 71 gpioled_control(void *priv, int onoff) 72 { 73 struct gpioled *led; 74 75 led = (struct gpioled *)priv; 76 if (led->pin) 77 gpio_pin_set_active(led->pin, onoff); 78 } 79 80 static void 81 gpioleds_attach_led(struct gpioleds_softc *sc, phandle_t node, 82 struct gpioled *led) 83 { 84 char *name; 85 int state, err; 86 char *default_state; 87 88 led->parent_sc = sc; 89 90 state = 0; 91 if (OF_getprop_alloc(node, "default-state", 92 (void **)&default_state) != -1) { 93 if (strcasecmp(default_state, "on") == 0) 94 state = 1; 95 else if (strcasecmp(default_state, "off") == 0) 96 state = 0; 97 else if (strcasecmp(default_state, "keep") == 0) 98 state = -1; 99 else { 100 state = -1; 101 device_printf(sc->sc_dev, 102 "unknown value for default-state in FDT\n"); 103 } 104 OF_prop_free(default_state); 105 } 106 107 name = NULL; 108 if (OF_getprop_alloc(node, "label", (void **)&name) == -1) 109 OF_getprop_alloc(node, "name", (void **)&name); 110 111 if (name == NULL) { 112 device_printf(sc->sc_dev, 113 "no name provided for gpio LED, skipping\n"); 114 return; 115 } 116 117 err = gpio_pin_get_by_ofw_idx(sc->sc_dev, node, 0, &led->pin); 118 if (err) { 119 device_printf(sc->sc_dev, "<%s> failed to map pin\n", name); 120 if (name) 121 OF_prop_free(name); 122 return; 123 } 124 gpio_pin_setflags(led->pin, GPIO_PIN_OUTPUT); 125 126 led->leddev = led_create_state(gpioled_control, led, name, 127 state); 128 129 if (name != NULL) 130 OF_prop_free(name); 131 } 132 133 static void 134 gpioleds_detach_led(struct gpioled *led) 135 { 136 137 if (led->leddev != NULL) 138 led_destroy(led->leddev); 139 140 if (led->pin) 141 gpio_pin_release(led->pin); 142 } 143 144 static int 145 gpioled_probe(device_t dev) 146 { 147 if (!ofw_bus_status_okay(dev)) 148 return (ENXIO); 149 if (!ofw_bus_is_compatible(dev, "gpio-leds")) 150 return (ENXIO); 151 152 device_set_desc(dev, "GPIO LEDs"); 153 154 return (BUS_PROBE_DEFAULT); 155 } 156 157 static int 158 gpioled_attach(device_t dev) 159 { 160 struct gpioleds_softc *sc; 161 phandle_t child, leds; 162 int total_leds; 163 164 if ((leds = ofw_bus_get_node(dev)) == -1) 165 return (ENXIO); 166 167 sc = device_get_softc(dev); 168 sc->sc_dev = dev; 169 sc->sc_busdev = device_get_parent(dev); 170 171 /* Traverse the 'gpio-leds' node and count leds */ 172 total_leds = 0; 173 for (child = OF_child(leds); child != 0; child = OF_peer(child)) { 174 if (!OF_hasprop(child, "gpios")) 175 continue; 176 total_leds++; 177 } 178 179 if (total_leds) { 180 sc->sc_leds = malloc(sizeof(struct gpioled) * total_leds, 181 M_DEVBUF, M_WAITOK | M_ZERO); 182 183 sc->sc_total_leds = 0; 184 /* Traverse the 'gpio-leds' node and count leds */ 185 for (child = OF_child(leds); child != 0; child = OF_peer(child)) { 186 if (!OF_hasprop(child, "gpios")) 187 continue; 188 gpioleds_attach_led(sc, child, &sc->sc_leds[sc->sc_total_leds]); 189 sc->sc_total_leds++; 190 } 191 } 192 193 return (0); 194 } 195 196 static int 197 gpioled_detach(device_t dev) 198 { 199 struct gpioleds_softc *sc; 200 int i; 201 202 sc = device_get_softc(dev); 203 204 for (i = 0; i < sc->sc_total_leds; i++) 205 gpioleds_detach_led(&sc->sc_leds[i]); 206 207 if (sc->sc_leds) 208 free(sc->sc_leds, M_DEVBUF); 209 210 return (0); 211 } 212 213 static devclass_t gpioled_devclass; 214 215 static device_method_t gpioled_methods[] = { 216 /* Device interface */ 217 DEVMETHOD(device_probe, gpioled_probe), 218 DEVMETHOD(device_attach, gpioled_attach), 219 DEVMETHOD(device_detach, gpioled_detach), 220 221 DEVMETHOD_END 222 }; 223 224 static driver_t gpioled_driver = { 225 "gpioled", 226 gpioled_methods, 227 sizeof(struct gpioleds_softc), 228 }; 229 230 DRIVER_MODULE(gpioled, ofwbus, gpioled_driver, gpioled_devclass, 0, 0); 231 DRIVER_MODULE(gpioled, simplebus, gpioled_driver, gpioled_devclass, 0, 0); 232 MODULE_DEPEND(gpioled, gpiobus, 1, 1, 1); 233