2 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_platform.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
42 #include <dev/fdt/fdt_common.h>
43 #include <dev/ofw/ofw_bus.h>
45 #include <dev/gpio/gpiobusvar.h>
46 #include <dev/led/led.h>
48 #include "gpiobus_if.h"
52 struct gpioleds_softc *parent_sc;
61 struct gpioled *sc_leds;
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);
71 gpioled_control(void *priv, int onoff)
75 led = (struct gpioled *)priv;
77 gpio_pin_set_active(led->pin, onoff);
81 gpioleds_attach_led(struct gpioleds_softc *sc, phandle_t node,
91 if (OF_getprop_alloc(node, "default-state",
92 (void **)&default_state) != -1) {
93 if (strcasecmp(default_state, "on") == 0)
95 else if (strcasecmp(default_state, "off") == 0)
97 else if (strcasecmp(default_state, "keep") == 0)
101 device_printf(sc->sc_dev,
102 "unknown value for default-state in FDT\n");
104 OF_prop_free(default_state);
108 if (OF_getprop_alloc(node, "label", (void **)&name) == -1)
109 OF_getprop_alloc(node, "name", (void **)&name);
112 device_printf(sc->sc_dev,
113 "no name provided for gpio LED, skipping\n");
117 err = gpio_pin_get_by_ofw_idx(sc->sc_dev, node, 0, &led->pin);
119 device_printf(sc->sc_dev, "<%s> failed to map pin\n", name);
124 gpio_pin_setflags(led->pin, GPIO_PIN_OUTPUT);
126 led->leddev = led_create_state(gpioled_control, led, name,
134 gpioleds_detach_led(struct gpioled *led)
137 if (led->leddev != NULL)
138 led_destroy(led->leddev);
141 gpio_pin_release(led->pin);
145 gpioled_probe(device_t dev)
147 if (!ofw_bus_is_compatible(dev, "gpio-leds"))
150 device_set_desc(dev, "GPIO LEDs");
152 return (BUS_PROBE_DEFAULT);
156 gpioled_attach(device_t dev)
158 struct gpioleds_softc *sc;
159 phandle_t child, leds;
162 if ((leds = ofw_bus_get_node(dev)) == -1)
165 sc = device_get_softc(dev);
167 sc->sc_busdev = device_get_parent(dev);
169 /* Traverse the 'gpio-leds' node and count leds */
171 for (child = OF_child(leds); child != 0; child = OF_peer(child)) {
172 if (!OF_hasprop(child, "gpios"))
178 sc->sc_leds = malloc(sizeof(struct gpioled) * total_leds,
179 M_DEVBUF, M_WAITOK | M_ZERO);
181 sc->sc_total_leds = 0;
182 /* Traverse the 'gpio-leds' node and count leds */
183 for (child = OF_child(leds); child != 0; child = OF_peer(child)) {
184 if (!OF_hasprop(child, "gpios"))
186 gpioleds_attach_led(sc, child, &sc->sc_leds[sc->sc_total_leds]);
195 gpioled_detach(device_t dev)
197 struct gpioleds_softc *sc;
200 sc = device_get_softc(dev);
202 for (i = 0; i < sc->sc_total_leds; i++)
203 gpioleds_detach_led(&sc->sc_leds[i]);
206 free(sc->sc_leds, M_DEVBUF);
211 static devclass_t gpioled_devclass;
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),
222 static driver_t gpioled_driver = {
225 sizeof(struct gpioleds_softc),
228 DRIVER_MODULE(gpioled, ofwbus, gpioled_driver, gpioled_devclass, 0, 0);
229 DRIVER_MODULE(gpioled, simplebus, gpioled_driver, gpioled_devclass, 0, 0);
230 MODULE_DEPEND(gpioled, gpiobus, 1, 1, 1);