gpioled.c revision 213237
1250863Smarcel/*-
2250863Smarcel * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
3250863Smarcel * All rights reserved.
4250863Smarcel *
5250863Smarcel * Redistribution and use in source and binary forms, with or without
6250863Smarcel * modification, are permitted provided that the following conditions
7250863Smarcel * are met:
8250863Smarcel * 1. Redistributions of source code must retain the above copyright
9250863Smarcel *    notice, this list of conditions and the following disclaimer.
10250863Smarcel * 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 ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/sys/dev/gpio/gpioled.c 213237 2010-09-28 03:24:53Z gonzo $");
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/bio.h>
32#include <sys/bus.h>
33#include <sys/conf.h>
34#include <sys/kernel.h>
35#include <sys/kthread.h>
36#include <sys/lock.h>
37#include <sys/malloc.h>
38#include <sys/module.h>
39#include <sys/mutex.h>
40
41#include <dev/led/led.h>
42#include <sys/gpio.h>
43#include "gpiobus_if.h"
44
45/*
46 * Only one pin for led
47 */
48#define	GPIOLED_PIN	0
49
50#define GPIOLED_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
51#define	GPIOLED_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
52#define GPIOLED_LOCK_INIT(_sc) \
53	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
54	    "gpioled", MTX_DEF)
55#define GPIOLED_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
56
57struct gpioled_softc
58{
59	device_t	sc_dev;
60	device_t	sc_busdev;
61	struct mtx	sc_mtx;
62	struct cdev	*sc_leddev;
63};
64
65static void gpioled_control(void *, int);
66static int gpioled_probe(device_t);
67static int gpioled_attach(device_t);
68static int gpioled_detach(device_t);
69
70static void
71gpioled_control(void *priv, int onoff)
72{
73	struct gpioled_softc *sc = priv;
74	GPIOLED_LOCK(sc);
75	GPIOBUS_LOCK_BUS(sc->sc_busdev);
76	GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev);
77	GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN,
78	    onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
79	GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
80	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
81	GPIOLED_UNLOCK(sc);
82}
83
84static int
85gpioled_probe(device_t dev)
86{
87	device_set_desc(dev, "GPIO led");
88	return (0);
89}
90
91static int
92gpioled_attach(device_t dev)
93{
94	struct gpioled_softc *sc;
95	const char *name;
96
97	sc = device_get_softc(dev);
98	sc->sc_dev = dev;
99	sc->sc_busdev = device_get_parent(dev);
100	GPIOLED_LOCK_INIT(sc);
101	if (resource_string_value(device_get_name(dev),
102	    device_get_unit(dev), "name", &name))
103		name = NULL;
104
105	sc->sc_leddev = led_create(gpioled_control, sc, name ? name :
106	    device_get_nameunit(dev));
107
108	return (0);
109}
110
111static int
112gpioled_detach(device_t dev)
113{
114	struct gpioled_softc *sc;
115
116	sc = device_get_softc(dev);
117	if (sc->sc_leddev) {
118		led_destroy(sc->sc_leddev);
119		sc->sc_leddev = NULL;
120	}
121	GPIOLED_LOCK_DESTROY(sc);
122	return (0);
123}
124
125static devclass_t gpioled_devclass;
126
127static device_method_t gpioled_methods[] = {
128	/* Device interface */
129	DEVMETHOD(device_probe,		gpioled_probe),
130	DEVMETHOD(device_attach,	gpioled_attach),
131	DEVMETHOD(device_detach,	gpioled_detach),
132
133	{ 0, 0 }
134};
135
136static driver_t gpioled_driver = {
137	"gpioled",
138	gpioled_methods,
139	sizeof(struct gpioled_softc),
140};
141
142DRIVER_MODULE(gpioled, gpiobus, gpioled_driver, gpioled_devclass, 0, 0);
143