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