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