gpioled.c revision 273917
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: head/sys/dev/gpio/gpioled.c 273917 2014-10-31 19:15:14Z loos $");
29
30#include "opt_platform.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/bio.h>
35#include <sys/bus.h>
36#include <sys/conf.h>
37#include <sys/kernel.h>
38#include <sys/kthread.h>
39#include <sys/lock.h>
40#include <sys/malloc.h>
41#include <sys/module.h>
42#include <sys/mutex.h>
43
44#ifdef FDT
45#include <dev/fdt/fdt_common.h>
46#include <dev/gpio/gpiobusvar.h>
47#include <dev/ofw/ofw_bus.h>
48#endif
49
50#include <dev/led/led.h>
51#include <sys/gpio.h>
52#include "gpiobus_if.h"
53
54/*
55 * Only one pin for led
56 */
57#define	GPIOLED_PIN	0
58
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), \
63	    "gpioled", MTX_DEF)
64#define GPIOLED_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
65
66struct gpioled_softc
67{
68	device_t	sc_dev;
69	device_t	sc_busdev;
70	struct mtx	sc_mtx;
71	struct cdev	*sc_leddev;
72};
73
74static void gpioled_control(void *, int);
75static int gpioled_probe(device_t);
76static int gpioled_attach(device_t);
77static int gpioled_detach(device_t);
78
79static void
80gpioled_control(void *priv, int onoff)
81{
82	int error;
83	struct gpioled_softc *sc;
84
85	sc = (struct gpioled_softc *)priv;
86	GPIOLED_LOCK(sc);
87	error = GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev,
88	    GPIOBUS_DONTWAIT);
89	if (error != 0) {
90		GPIOLED_UNLOCK(sc);
91		return;
92	}
93	error = GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev,
94	    GPIOLED_PIN, GPIO_PIN_OUTPUT);
95	if (error == 0)
96		GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN,
97		    onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
98	GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
99	GPIOLED_UNLOCK(sc);
100}
101
102#ifdef FDT
103static void
104gpioled_identify(driver_t *driver, device_t bus)
105{
106	phandle_t child, leds, root;
107
108	root = OF_finddevice("/");
109	if (root == 0)
110		return;
111	leds = fdt_find_compatible(root, "gpio-leds", 1);
112	if (leds == 0)
113		return;
114
115	/* Traverse the 'gpio-leds' node and add its children. */
116	for (child = OF_child(leds); child != 0; child = OF_peer(child))
117		if (ofw_gpiobus_add_fdt_child(bus, child) == NULL)
118			continue;
119}
120#endif
121
122static int
123gpioled_probe(device_t dev)
124{
125#ifdef FDT
126	int match;
127	phandle_t node;
128	char *compat;
129
130	/*
131	 * We can match against our own node compatible string and also against
132	 * our parent node compatible string.  The first is normally used to
133	 * describe leds on a gpiobus and the later when there is a common node
134	 * compatible with 'gpio-leds' which is used to concentrate all the
135	 * leds nodes on the dts.
136	 */
137	match = 0;
138	if (ofw_bus_is_compatible(dev, "gpioled"))
139		match = 1;
140
141	if (match == 0) {
142		if ((node = ofw_bus_get_node(dev)) == -1)
143			return (ENXIO);
144		if ((node = OF_parent(node)) == -1)
145			return (ENXIO);
146		if (OF_getprop_alloc(node, "compatible", 1,
147		    (void **)&compat) == -1)
148			return (ENXIO);
149
150		if (strcasecmp(compat, "gpio-leds") == 0)
151			match = 1;
152
153		free(compat, M_OFWPROP);
154	}
155
156	if (match == 0)
157		return (ENXIO);
158#endif
159	device_set_desc(dev, "GPIO led");
160
161	return (0);
162}
163
164static int
165gpioled_attach(device_t dev)
166{
167	struct gpioled_softc *sc;
168#ifdef FDT
169	phandle_t node;
170	char *name;
171#else
172	const char *name;
173#endif
174
175	sc = device_get_softc(dev);
176	sc->sc_dev = dev;
177	sc->sc_busdev = device_get_parent(dev);
178	GPIOLED_LOCK_INIT(sc);
179#ifdef FDT
180	name = NULL;
181	if ((node = ofw_bus_get_node(dev)) == -1)
182		return (ENXIO);
183	if (OF_getprop_alloc(node, "label", 1, (void **)&name) == -1)
184		OF_getprop_alloc(node, "name", 1, (void **)&name);
185#else
186	if (resource_string_value(device_get_name(dev),
187	    device_get_unit(dev), "name", &name))
188		name = NULL;
189#endif
190
191	sc->sc_leddev = led_create(gpioled_control, sc, name ? name :
192	    device_get_nameunit(dev));
193#ifdef FDT
194	if (name != NULL)
195		free(name, M_OFWPROP);
196#endif
197
198	return (0);
199}
200
201static int
202gpioled_detach(device_t dev)
203{
204	struct gpioled_softc *sc;
205
206	sc = device_get_softc(dev);
207	if (sc->sc_leddev) {
208		led_destroy(sc->sc_leddev);
209		sc->sc_leddev = NULL;
210	}
211	GPIOLED_LOCK_DESTROY(sc);
212	return (0);
213}
214
215static devclass_t gpioled_devclass;
216
217static device_method_t gpioled_methods[] = {
218	/* Device interface */
219#ifdef FDT
220	DEVMETHOD(device_identify,	gpioled_identify),
221#endif
222	DEVMETHOD(device_probe,		gpioled_probe),
223	DEVMETHOD(device_attach,	gpioled_attach),
224	DEVMETHOD(device_detach,	gpioled_detach),
225
226	{ 0, 0 }
227};
228
229static driver_t gpioled_driver = {
230	"gpioled",
231	gpioled_methods,
232	sizeof(struct gpioled_softc),
233};
234
235DRIVER_MODULE(gpioled, gpiobus, gpioled_driver, gpioled_devclass, 0, 0);
236