forked from freebsd/freebsd-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrb_led.c
144 lines (117 loc) · 3.45 KB
/
rb_led.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*-
* Copyright (c) 2017 Justin Hibbits
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/openfirm.h>
#include <dev/led/led.h>
#include "gpio_if.h"
struct rbled_softc {
struct cdev *sc_led;
device_t sc_gpio;
uint32_t sc_ledpin;
};
static int rbled_probe(device_t);
static int rbled_attach(device_t);
static int rbled_detach(device_t);
static void rbled_toggle(void *, int);
static device_method_t rbled_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, rbled_probe),
DEVMETHOD(device_attach, rbled_attach),
DEVMETHOD(device_detach, rbled_detach),
DEVMETHOD_END
};
static driver_t rbled_driver = {
"rbled",
rbled_methods,
sizeof(struct rbled_softc),
};
DRIVER_MODULE(rbled, simplebus, rbled_driver, 0, 0);
static int
rbled_probe(device_t dev)
{
phandle_t node;
const char *name;
cell_t gp[2];
char model[6];
node = ofw_bus_get_node(dev);
name = ofw_bus_get_name(dev);
if (name == NULL)
return (ENXIO);
if (strcmp(name, "led") != 0)
return (ENXIO);
if (OF_getprop(node, "user_led", gp, sizeof(gp)) <= 0)
return (ENXIO);
/* Check root model. */
node = OF_peer(0);
if (OF_getprop(node, "model", model, sizeof(model)) <= 0)
return (ENXIO);
if (strcmp(model, "RB800") != 0)
return (ENXIO);
device_set_desc(dev, "RouterBoard LED");
return (0);
}
static int
rbled_attach(device_t dev)
{
struct rbled_softc *sc;
phandle_t node;
cell_t gp[2];
sc = device_get_softc(dev);
node = ofw_bus_get_node(dev);
if (OF_getprop(node, "user_led", gp, sizeof(gp)) <= 0)
return (ENXIO);
sc->sc_gpio = OF_device_from_xref(gp[0]);
if (sc->sc_gpio == NULL) {
device_printf(dev, "No GPIO resource found!\n");
return (ENXIO);
}
sc->sc_ledpin = gp[1];
sc->sc_led = led_create(rbled_toggle, sc, "user_led");
if (sc->sc_led == NULL)
return (ENXIO);
return (0);
}
static int
rbled_detach(device_t dev)
{
struct rbled_softc *sc;
sc = device_get_softc(dev);
led_destroy(sc->sc_led);
return (0);
}
static void
rbled_toggle(void *priv, int onoff)
{
struct rbled_softc *sc = priv;
GPIO_PIN_SET(sc->sc_gpio, sc->sc_ledpin, onoff);
}