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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/* $NetBSD: obsled.c,v 1.10 2014/02/25 18:30:08 pooka Exp $ */
/*
* Copyright (c) 2004 Shigeyuki Fukushima.
* 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.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* 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/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: obsled.c,v 1.10 2014/02/25 18:30:08 pooka Exp $");
#include <sys/param.h>
#include <sys/device.h>
#include <sys/queue.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <machine/obs266.h>
#include <powerpc/ibm4xx/dev/gpiovar.h>
struct obsled_softc {
device_t sc_dev;
gpio_tag_t sc_tag;
int sc_addr;
int sc_led_state; /* LED status (ON=1/OFF=0) */
int sc_led_state_mib;
};
static void obsled_attach(device_t, device_t, void *);
static int obsled_match(device_t, cfdata_t, void *);
static int obsled_sysctl_verify(SYSCTLFN_PROTO);
static void obsled_set_state(struct obsled_softc *);
CFATTACH_DECL_NEW(obsled, sizeof(struct obsled_softc),
obsled_match, obsled_attach, NULL, NULL);
static int
obsled_match(device_t parent, cfdata_t cf, void *aux)
{
struct gpio_attach_args *ga = aux;
/* XXX: support only OpenBlockS266 LED */
if (ga->ga_addr == OBS266_GPIO_LED1)
return (1);
else if (ga->ga_addr == OBS266_GPIO_LED2)
return (1);
else if (ga->ga_addr == OBS266_GPIO_LED4)
return (1);
return (0);
}
static void
obsled_attach(device_t parent, device_t self, void *aux)
{
struct obsled_softc *sc = device_private(self);
struct gpio_attach_args *ga = aux;
struct sysctlnode *node;
int err, node_mib;
char led_name[5];
/* int led = (1 << device_unit(sc->sc_dev)); */
snprintf(led_name, sizeof(led_name),
"led%d", (1 << device_unit(sc->sc_dev)) & 0x7);
aprint_naive(": OpenBlockS %s\n", led_name);
aprint_normal(": OpenBlockS %s\n", led_name);
sc->sc_dev = self;
sc->sc_tag = ga->ga_tag;
sc->sc_addr = ga->ga_addr;
sc->sc_led_state = 0;
obs266_led_set(OBS266_LED_OFF);
/* add sysctl interface */
err = sysctl_createv(NULL, 0,
NULL, (const struct sysctlnode **)&node,
0, CTLTYPE_NODE,
"obsled",
NULL,
NULL, 0, NULL, 0,
CTL_HW, CTL_CREATE, CTL_EOL);
if (err != 0)
return;
node_mib = node->sysctl_num;
err = sysctl_createv(NULL, 0,
NULL, (const struct sysctlnode **)&node,
CTLFLAG_READWRITE, CTLTYPE_INT,
led_name,
SYSCTL_DESCR("OpenBlockS LED state (0=off, 1=on)"),
obsled_sysctl_verify, 0, (void *)sc, 0,
CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
if (err != 0)
return;
sc->sc_led_state_mib = node->sysctl_num;
#if 0
{
gpio_tag_t tag = sc->sc_tag;
(*(tag)->io_or_write)((tag)->cookie, sc->sc_addr, 0);
}
#endif
}
static int
obsled_sysctl_verify(SYSCTLFN_ARGS)
{
struct obsled_softc *sc = rnode->sysctl_data;
struct sysctlnode node;
int err, state;
node = *rnode;
state = sc->sc_led_state;
node.sysctl_data = &state;
err = sysctl_lookup(SYSCTLFN_CALL(&node));
if (err != 0 || newp == NULL)
return err;
if (node.sysctl_num == sc->sc_led_state_mib) {
if (state < 0 || state > 1)
return EINVAL;
sc->sc_led_state = state;
obsled_set_state(sc);
}
return 0;
}
static void
obsled_set_state(struct obsled_softc *sc)
{
gpio_tag_t tag = sc->sc_tag;
(*(tag)->io_or_write)((tag)->cookie,
sc->sc_addr,
(~(sc->sc_led_state) & 1));
/* GPIO LED input ON=0/OFF=1 */
}
/*
* Setting LED interface for inside kernel.
* Argumnt `led' is 3-bit LED state (led=0-7/ON=1/OFF=0).
*/
void
obs266_led_set(int led)
{
device_t dv;
deviter_t di;
/*
* Sarching "obsled" devices from device tree.
* Do you have something better idea?
*/
for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
dv = deviter_next(&di)) {
if (device_is_a(dv, "obsles")) {
struct obsled_softc *sc = device_private(dv);
sc->sc_led_state =
(led & (1 << device_unit(dv))) >> device_unit(dv);
obsled_set_state(sc);
}
}
deviter_release(&di);
}
|