]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/gpio/gpioled.c
MFV r302260: expat 2.2.0
[FreeBSD/FreeBSD.git] / sys / dev / gpio / gpioled.c
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 #ifdef FDT
43 #include <dev/fdt/fdt_common.h>
44 #include <dev/ofw/ofw_bus.h>
45 #endif
46
47 #include <dev/gpio/gpiobusvar.h>
48 #include <dev/led/led.h>
49
50 #include "gpiobus_if.h"
51
52 /*
53  * Only one pin for led
54  */
55 #define GPIOLED_PIN     0
56
57 #define GPIOLED_LOCK(_sc)               mtx_lock(&(_sc)->sc_mtx)
58 #define GPIOLED_UNLOCK(_sc)             mtx_unlock(&(_sc)->sc_mtx)
59 #define GPIOLED_LOCK_INIT(_sc)          mtx_init(&(_sc)->sc_mtx,        \
60     device_get_nameunit((_sc)->sc_dev), "gpioled", MTX_DEF)
61 #define GPIOLED_LOCK_DESTROY(_sc)       mtx_destroy(&(_sc)->sc_mtx)
62
63 struct gpioled_softc 
64 {
65         device_t        sc_dev;
66         device_t        sc_busdev;
67         struct mtx      sc_mtx;
68         struct cdev     *sc_leddev;
69 };
70
71 static void gpioled_control(void *, int);
72 static int gpioled_probe(device_t);
73 static int gpioled_attach(device_t);
74 static int gpioled_detach(device_t);
75
76 static void 
77 gpioled_control(void *priv, int onoff)
78 {
79         struct gpioled_softc *sc;
80
81         sc = (struct gpioled_softc *)priv;
82         GPIOLED_LOCK(sc);
83         if (GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN,
84             GPIO_PIN_OUTPUT) == 0) {
85                 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN,
86                     onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
87         }
88         GPIOLED_UNLOCK(sc);
89 }
90
91 #ifdef FDT
92 static void
93 gpioled_identify(driver_t *driver, device_t bus)
94 {
95         phandle_t child, leds, root;
96
97         root = OF_finddevice("/");
98         if (root == 0)
99                 return;
100         for (leds = OF_child(root); leds != 0; leds = OF_peer(leds)) {
101                 if (!fdt_is_compatible_strict(leds, "gpio-leds"))
102                         continue;
103                 /* Traverse the 'gpio-leds' node and add its children. */
104                 for (child = OF_child(leds); child != 0; child = OF_peer(child)) {
105                         if (!OF_hasprop(child, "gpios"))
106                                 continue;
107                         if (ofw_gpiobus_add_fdt_child(bus, driver->name, child) == NULL)
108                                 continue;
109                 }
110         }
111 }
112 #endif
113
114 static int
115 gpioled_probe(device_t dev)
116 {
117 #ifdef FDT
118         int match;
119         phandle_t node;
120         char *compat;
121
122         /*
123          * We can match against our own node compatible string and also against
124          * our parent node compatible string.  The first is normally used to
125          * describe leds on a gpiobus and the later when there is a common node
126          * compatible with 'gpio-leds' which is used to concentrate all the
127          * leds nodes on the dts.
128          */
129         match = 0;
130         if (ofw_bus_is_compatible(dev, "gpioled"))
131                 match = 1;
132
133         if (match == 0) {
134                 if ((node = ofw_bus_get_node(dev)) == -1)
135                         return (ENXIO);
136                 if ((node = OF_parent(node)) == -1)
137                         return (ENXIO);
138                 if (OF_getprop_alloc(node, "compatible", 1,
139                     (void **)&compat) == -1)
140                         return (ENXIO);
141
142                 if (strcasecmp(compat, "gpio-leds") == 0)
143                         match = 1;
144
145                 OF_prop_free(compat);
146         }
147
148         if (match == 0)
149                 return (ENXIO);
150 #endif
151         device_set_desc(dev, "GPIO led");
152
153         return (BUS_PROBE_DEFAULT);
154 }
155
156 static int
157 gpioled_attach(device_t dev)
158 {
159         struct gpioled_softc *sc;
160         int state;
161 #ifdef FDT
162         phandle_t node;
163         char *default_state;
164         char *name;
165 #else
166         const char *name;
167 #endif
168
169         sc = device_get_softc(dev);
170         sc->sc_dev = dev;
171         sc->sc_busdev = device_get_parent(dev);
172         GPIOLED_LOCK_INIT(sc);
173
174         state = 0;
175
176 #ifdef FDT
177         if ((node = ofw_bus_get_node(dev)) == -1)
178                 return (ENXIO);
179
180         if (OF_getprop_alloc(node, "default-state",
181             sizeof(char), (void **)&default_state) != -1) {
182                 if (strcasecmp(default_state, "on") == 0)
183                         state = 1;
184                 else if (strcasecmp(default_state, "off") == 0)
185                         state = 0;
186                 else if (strcasecmp(default_state, "keep") == 0)
187                         state = -1;
188                 else {
189                         device_printf(dev,
190                             "unknown value for default-state in FDT\n");
191                 }
192                 OF_prop_free(default_state);
193         }
194
195         name = NULL;
196         if (OF_getprop_alloc(node, "label", 1, (void **)&name) == -1)
197                 OF_getprop_alloc(node, "name", 1, (void **)&name);
198 #else
199         if (resource_string_value(device_get_name(dev), 
200             device_get_unit(dev), "name", &name))
201                 name = NULL;
202 #endif
203
204         sc->sc_leddev = led_create_state(gpioled_control, sc, name ? name :
205             device_get_nameunit(dev), state);
206 #ifdef FDT
207         if (name != NULL)
208                 OF_prop_free(name);
209 #endif
210
211         return (0);
212 }
213
214 static int
215 gpioled_detach(device_t dev)
216 {
217         struct gpioled_softc *sc;
218
219         sc = device_get_softc(dev);
220         if (sc->sc_leddev) {
221                 led_destroy(sc->sc_leddev);
222                 sc->sc_leddev = NULL;
223         }
224         GPIOLED_LOCK_DESTROY(sc);
225         return (0);
226 }
227
228 static devclass_t gpioled_devclass;
229
230 static device_method_t gpioled_methods[] = {
231         /* Device interface */
232 #ifdef FDT
233         DEVMETHOD(device_identify,      gpioled_identify),
234 #endif
235         DEVMETHOD(device_probe,         gpioled_probe),
236         DEVMETHOD(device_attach,        gpioled_attach),
237         DEVMETHOD(device_detach,        gpioled_detach),
238
239         DEVMETHOD_END
240 };
241
242 static driver_t gpioled_driver = {
243         "gpioled",
244         gpioled_methods,
245         sizeof(struct gpioled_softc),
246 };
247
248 DRIVER_MODULE(gpioled, gpiobus, gpioled_driver, gpioled_devclass, 0, 0);
249 MODULE_DEPEND(gpioled, gpiobus, 1, 1, 1);