gpioled.c revision 267654
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: releng/9.3/sys/dev/gpio/gpioled.c 214971 2010-11-08 03:08:01Z thompsa $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bio.h>
33#include <sys/bus.h>
34#include <sys/conf.h>
35#include <sys/kernel.h>
36#include <sys/kthread.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/led/led.h>
43#include <sys/gpio.h>
44#include "gpiobus_if.h"
45
46/*
47 * Only one pin for led
48 */
49#define	GPIOLED_PIN	0
50
51#define GPIOLED_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
52#define	GPIOLED_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
53#define GPIOLED_LOCK_INIT(_sc) \
54	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
55	    "gpioled", MTX_DEF)
56#define GPIOLED_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
57
58struct gpioled_softc
59{
60	device_t	sc_dev;
61	device_t	sc_busdev;
62	struct mtx	sc_mtx;
63	struct cdev	*sc_leddev;
64};
65
66static void gpioled_control(void *, int);
67static int gpioled_probe(device_t);
68static int gpioled_attach(device_t);
69static int gpioled_detach(device_t);
70
71static void
72gpioled_control(void *priv, int onoff)
73{
74	struct gpioled_softc *sc = priv;
75	GPIOLED_LOCK(sc);
76	GPIOBUS_LOCK_BUS(sc->sc_busdev);
77	GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev);
78	GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN,
79	    onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
80	GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
81	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
82	GPIOLED_UNLOCK(sc);
83}
84
85static int
86gpioled_probe(device_t dev)
87{
88	device_set_desc(dev, "GPIO led");
89	return (0);
90}
91
92static int
93gpioled_attach(device_t dev)
94{
95	struct gpioled_softc *sc;
96	const char *name;
97
98	sc = device_get_softc(dev);
99	sc->sc_dev = dev;
100	sc->sc_busdev = device_get_parent(dev);
101	GPIOLED_LOCK_INIT(sc);
102	if (resource_string_value(device_get_name(dev),
103	    device_get_unit(dev), "name", &name))
104		name = NULL;
105
106	GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN,
107	    GPIO_PIN_OUTPUT);
108
109	sc->sc_leddev = led_create(gpioled_control, sc, name ? name :
110	    device_get_nameunit(dev));
111
112	return (0);
113}
114
115static int
116gpioled_detach(device_t dev)
117{
118	struct gpioled_softc *sc;
119
120	sc = device_get_softc(dev);
121	if (sc->sc_leddev) {
122		led_destroy(sc->sc_leddev);
123		sc->sc_leddev = NULL;
124	}
125	GPIOLED_LOCK_DESTROY(sc);
126	return (0);
127}
128
129static devclass_t gpioled_devclass;
130
131static device_method_t gpioled_methods[] = {
132	/* Device interface */
133	DEVMETHOD(device_probe,		gpioled_probe),
134	DEVMETHOD(device_attach,	gpioled_attach),
135	DEVMETHOD(device_detach,	gpioled_detach),
136
137	{ 0, 0 }
138};
139
140static driver_t gpioled_driver = {
141	"gpioled",
142	gpioled_methods,
143	sizeof(struct gpioled_softc),
144};
145
146DRIVER_MODULE(gpioled, gpiobus, gpioled_driver, gpioled_devclass, 0, 0);
147