xref: /freebsd/sys/dev/gpio/gpioled.c (revision d6b92ffa)
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 #include <dev/gpio/gpiobusvar.h>
43 #include <dev/led/led.h>
44 
45 #include "gpiobus_if.h"
46 
47 /*
48  * Only one pin for led
49  */
50 #define	GPIOLED_PIN	0
51 
52 #define	GPIOLED_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
53 #define	GPIOLED_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
54 #define	GPIOLED_LOCK_INIT(_sc)		mtx_init(&(_sc)->sc_mtx,	\
55     device_get_nameunit((_sc)->sc_dev), "gpioled", MTX_DEF)
56 #define	GPIOLED_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->sc_mtx)
57 
58 struct gpioled_softc
59 {
60 	device_t	sc_dev;
61 	device_t	sc_busdev;
62 	struct mtx	sc_mtx;
63 	struct cdev	*sc_leddev;
64 	int		sc_invert;
65 };
66 
67 static void gpioled_control(void *, int);
68 static int gpioled_probe(device_t);
69 static int gpioled_attach(device_t);
70 static int gpioled_detach(device_t);
71 
72 static void
73 gpioled_control(void *priv, int onoff)
74 {
75 	struct gpioled_softc *sc;
76 
77 	sc = (struct gpioled_softc *)priv;
78 	GPIOLED_LOCK(sc);
79 	if (GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN,
80 	    GPIO_PIN_OUTPUT) == 0) {
81 		if (sc->sc_invert)
82 			onoff = !onoff;
83 		GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN,
84 		    onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
85 	}
86 	GPIOLED_UNLOCK(sc);
87 }
88 
89 static int
90 gpioled_probe(device_t dev)
91 {
92 	device_set_desc(dev, "GPIO led");
93 
94 	return (BUS_PROBE_DEFAULT);
95 }
96 
97 static int
98 gpioled_attach(device_t dev)
99 {
100 	struct gpioled_softc *sc;
101 	int state;
102 	const char *name;
103 
104 	sc = device_get_softc(dev);
105 	sc->sc_dev = dev;
106 	sc->sc_busdev = device_get_parent(dev);
107 	GPIOLED_LOCK_INIT(sc);
108 
109 	state = 0;
110 
111 	if (resource_string_value(device_get_name(dev),
112 	    device_get_unit(dev), "name", &name))
113 		name = NULL;
114 	resource_int_value(device_get_name(dev),
115 	    device_get_unit(dev), "invert", &sc->sc_invert);
116 
117 	sc->sc_leddev = led_create_state(gpioled_control, sc, name ? name :
118 	    device_get_nameunit(dev), state);
119 
120 	return (0);
121 }
122 
123 static int
124 gpioled_detach(device_t dev)
125 {
126 	struct gpioled_softc *sc;
127 
128 	sc = device_get_softc(dev);
129 	if (sc->sc_leddev) {
130 		led_destroy(sc->sc_leddev);
131 		sc->sc_leddev = NULL;
132 	}
133 	GPIOLED_LOCK_DESTROY(sc);
134 	return (0);
135 }
136 
137 static devclass_t gpioled_devclass;
138 
139 static device_method_t gpioled_methods[] = {
140 	/* Device interface */
141 	DEVMETHOD(device_probe,		gpioled_probe),
142 	DEVMETHOD(device_attach,	gpioled_attach),
143 	DEVMETHOD(device_detach,	gpioled_detach),
144 
145 	DEVMETHOD_END
146 };
147 
148 static driver_t gpioled_driver = {
149 	"gpioled",
150 	gpioled_methods,
151 	sizeof(struct gpioled_softc),
152 };
153 
154 DRIVER_MODULE(gpioled, gpiobus, gpioled_driver, gpioled_devclass, 0, 0);
155 MODULE_DEPEND(gpioled, gpiobus, 1, 1, 1);
156