2 * Copyright (c) 2017 Justin Hibbits
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 ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * 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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
36 #include <sys/kernel.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/openfirm.h>
40 #include <dev/led/led.h>
50 static int rbled_probe(device_t);
51 static int rbled_attach(device_t);
52 static int rbled_detach(device_t);
53 static void rbled_toggle(void *, int);
55 static device_method_t rbled_methods[] = {
56 /* Device interface */
57 DEVMETHOD(device_probe, rbled_probe),
58 DEVMETHOD(device_attach, rbled_attach),
59 DEVMETHOD(device_detach, rbled_detach),
64 static driver_t rbled_driver = {
67 sizeof(struct rbled_softc),
70 static devclass_t rbled_devclass;
72 DRIVER_MODULE(rbled, simplebus, rbled_driver, rbled_devclass, 0, 0);
75 rbled_probe(device_t dev)
82 node = ofw_bus_get_node(dev);
84 name = ofw_bus_get_name(dev);
87 if (strcmp(name, "led") != 0)
90 if (OF_getprop(node, "user_led", gp, sizeof(gp)) <= 0)
93 /* Check root model. */
95 if (OF_getprop(node, "model", model, sizeof(model)) <= 0)
97 if (strcmp(model, "RB800") != 0)
100 device_set_desc(dev, "RouterBoard LED");
105 rbled_attach(device_t dev)
107 struct rbled_softc *sc;
111 sc = device_get_softc(dev);
112 node = ofw_bus_get_node(dev);
114 if (OF_getprop(node, "user_led", gp, sizeof(gp)) <= 0)
117 sc->sc_gpio = OF_device_from_xref(gp[0]);
118 if (sc->sc_gpio == NULL) {
119 device_printf(dev, "No GPIO resource found!\n");
122 sc->sc_ledpin = gp[1];
124 sc->sc_led = led_create(rbled_toggle, sc, "user_led");
126 if (sc->sc_led == NULL)
133 rbled_detach(device_t dev)
135 struct rbled_softc *sc;
137 sc = device_get_softc(dev);
138 led_destroy(sc->sc_led);
144 rbled_toggle(void *priv, int onoff)
146 struct rbled_softc *sc = priv;
148 GPIO_PIN_SET(sc->sc_gpio, sc->sc_ledpin, onoff);