]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/mikrotik/rb_led.c
zfs: merge openzfs/zfs@95f71c019
[FreeBSD/FreeBSD.git] / sys / powerpc / mikrotik / rb_led.c
1 /*-
2  * Copyright (c) 2017 Justin Hibbits
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 ``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
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/openfirm.h>
38 #include <dev/led/led.h>
39
40 #include "gpio_if.h"
41
42 struct rbled_softc {
43         struct cdev     *sc_led;
44         device_t         sc_gpio;
45         uint32_t         sc_ledpin;
46 };
47
48 static int      rbled_probe(device_t);
49 static int      rbled_attach(device_t);
50 static int      rbled_detach(device_t);
51 static void     rbled_toggle(void *, int);
52
53 static device_method_t  rbled_methods[] = {
54         /* Device interface */
55         DEVMETHOD(device_probe,         rbled_probe),
56         DEVMETHOD(device_attach,        rbled_attach),
57         DEVMETHOD(device_detach,        rbled_detach),
58
59         DEVMETHOD_END
60 };
61
62 static driver_t rbled_driver = {
63         "rbled",
64         rbled_methods,
65         sizeof(struct rbled_softc),
66 };
67
68 DRIVER_MODULE(rbled, simplebus, rbled_driver, 0, 0);
69
70 static int
71 rbled_probe(device_t dev)
72 {
73         phandle_t node;
74         const char *name;
75         cell_t gp[2];
76         char model[6];
77
78         node = ofw_bus_get_node(dev);
79
80         name = ofw_bus_get_name(dev);
81         if (name == NULL)
82                 return (ENXIO);
83         if (strcmp(name, "led") != 0)
84                 return (ENXIO);
85
86         if (OF_getprop(node, "user_led", gp, sizeof(gp)) <= 0)
87                 return (ENXIO);
88
89         /* Check root model. */
90         node = OF_peer(0);
91         if (OF_getprop(node, "model", model, sizeof(model)) <= 0)
92                 return (ENXIO);
93         if (strcmp(model, "RB800") != 0)
94                 return (ENXIO);
95
96         device_set_desc(dev, "RouterBoard LED");
97         return (0);
98 }
99
100 static int
101 rbled_attach(device_t dev)
102 {
103         struct rbled_softc *sc;
104         phandle_t node;
105         cell_t gp[2];
106
107         sc = device_get_softc(dev);
108         node = ofw_bus_get_node(dev);
109
110         if (OF_getprop(node, "user_led", gp, sizeof(gp)) <= 0)
111                 return (ENXIO);
112
113         sc->sc_gpio = OF_device_from_xref(gp[0]);
114         if (sc->sc_gpio == NULL) {
115                 device_printf(dev, "No GPIO resource found!\n");
116                 return (ENXIO);
117         }
118         sc->sc_ledpin = gp[1];
119
120         sc->sc_led = led_create(rbled_toggle, sc, "user_led");
121
122         if (sc->sc_led == NULL)
123                 return (ENXIO);
124
125         return (0);
126 }
127
128 static int
129 rbled_detach(device_t dev)
130 {
131         struct rbled_softc *sc;
132
133         sc = device_get_softc(dev);
134         led_destroy(sc->sc_led);
135
136         return (0);
137 }
138
139 static void
140 rbled_toggle(void *priv, int onoff)
141 {
142         struct rbled_softc *sc = priv;
143
144         GPIO_PIN_SET(sc->sc_gpio, sc->sc_ledpin, onoff);
145 }