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>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
45 #include <dev/fdt/fdt_common.h>
46 #include <dev/gpio/gpiobusvar.h>
47 #include <dev/ofw/ofw_bus.h>
50 #include <dev/led/led.h>
52 #include "gpiobus_if.h"
55 * Only one pin for led
59 #define GPIOLED_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
60 #define GPIOLED_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
61 #define GPIOLED_LOCK_INIT(_sc) \
62 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
64 #define GPIOLED_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
71 struct cdev *sc_leddev;
74 static void gpioled_control(void *, int);
75 static int gpioled_probe(device_t);
76 static int gpioled_attach(device_t);
77 static int gpioled_detach(device_t);
80 gpioled_control(void *priv, int onoff)
82 struct gpioled_softc *sc = priv;
84 GPIOBUS_LOCK_BUS(sc->sc_busdev);
85 GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev);
86 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN,
88 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN,
89 onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
90 GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
91 GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
97 gpioled_identify(driver_t *driver, device_t bus)
99 phandle_t child, leds, root;
101 root = OF_finddevice("/");
104 leds = fdt_find_compatible(root, "gpio-leds", 1);
108 /* Traverse the 'gpio-leds' node and add its children. */
109 for (child = OF_child(leds); child != 0; child = OF_peer(child))
110 if (ofw_gpiobus_add_fdt_child(bus, child) == NULL)
116 gpioled_probe(device_t dev)
124 * We can match against our own node compatible string and also against
125 * our parent node compatible string. The first is normally used to
126 * describe leds on a gpiobus and the later when there is a common node
127 * compatible with 'gpio-leds' which is used to concentrate all the
128 * leds nodes on the dts.
131 if (ofw_bus_is_compatible(dev, "gpioled"))
135 if ((node = ofw_bus_get_node(dev)) == -1)
137 if ((node = OF_parent(node)) == -1)
139 if (OF_getprop_alloc(node, "compatible", 1,
140 (void **)&compat) == -1)
143 if (strcasecmp(compat, "gpio-leds") == 0)
146 free(compat, M_OFWPROP);
152 device_set_desc(dev, "GPIO led");
158 gpioled_attach(device_t dev)
160 struct gpioled_softc *sc;
168 sc = device_get_softc(dev);
170 sc->sc_busdev = device_get_parent(dev);
171 GPIOLED_LOCK_INIT(sc);
174 if ((node = ofw_bus_get_node(dev)) == -1)
176 if (OF_getprop_alloc(node, "label", 1, (void **)&name) == -1)
177 OF_getprop_alloc(node, "name", 1, (void **)&name);
179 if (resource_string_value(device_get_name(dev),
180 device_get_unit(dev), "name", &name))
184 sc->sc_leddev = led_create(gpioled_control, sc, name ? name :
185 device_get_nameunit(dev));
188 free(name, M_OFWPROP);
195 gpioled_detach(device_t dev)
197 struct gpioled_softc *sc;
199 sc = device_get_softc(dev);
201 led_destroy(sc->sc_leddev);
202 sc->sc_leddev = NULL;
204 GPIOLED_LOCK_DESTROY(sc);
208 static devclass_t gpioled_devclass;
210 static device_method_t gpioled_methods[] = {
211 /* Device interface */
213 DEVMETHOD(device_identify, gpioled_identify),
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 gpioled_softc),
228 DRIVER_MODULE(gpioled, gpiobus, gpioled_driver, gpioled_devclass, 0, 0);