obsled.c revision 1.8 1 1.8 matt /* $NetBSD: obsled.c,v 1.8 2011/06/18 06:44:27 matt Exp $ */
2 1.1 shige
3 1.1 shige /*
4 1.1 shige * Copyright (c) 2004 Shigeyuki Fukushima.
5 1.1 shige * All rights reserved.
6 1.1 shige *
7 1.1 shige * Redistribution and use in source and binary forms, with or without
8 1.1 shige * modification, are permitted provided that the following conditions
9 1.1 shige * are met:
10 1.1 shige * 1. Redistributions of source code must retain the above copyright
11 1.1 shige * notice, this list of conditions and the following disclaimer.
12 1.1 shige * 2. Redistributions in binary form must reproduce the above
13 1.1 shige * copyright notice, this list of conditions and the following
14 1.1 shige * disclaimer in the documentation and/or other materials provided
15 1.1 shige * with the distribution.
16 1.1 shige * 3. The name of the author may not be used to endorse or promote
17 1.1 shige * products derived from this software without specific prior
18 1.1 shige * written permission.
19 1.1 shige *
20 1.1 shige * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21 1.1 shige * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 1.1 shige * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 shige * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 1.1 shige * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 shige * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 1.1 shige * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 shige * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 1.1 shige * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 1.1 shige * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 1.1 shige * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 shige */
32 1.1 shige
33 1.1 shige #include <sys/cdefs.h>
34 1.8 matt __KERNEL_RCSID(0, "$NetBSD: obsled.c,v 1.8 2011/06/18 06:44:27 matt Exp $");
35 1.1 shige
36 1.1 shige #include <sys/param.h>
37 1.1 shige #include <sys/device.h>
38 1.1 shige #include <sys/queue.h>
39 1.3 shige #include <sys/sysctl.h>
40 1.1 shige #include <sys/systm.h>
41 1.1 shige
42 1.2 shige #include <machine/obs266.h>
43 1.1 shige
44 1.1 shige #include <powerpc/ibm4xx/dev/gpiovar.h>
45 1.1 shige
46 1.1 shige struct obsled_softc {
47 1.8 matt device_t sc_dev;
48 1.1 shige gpio_tag_t sc_tag;
49 1.1 shige int sc_addr;
50 1.3 shige int sc_led_state; /* LED status (ON=1/OFF=0) */
51 1.3 shige int sc_led_state_mib;
52 1.1 shige };
53 1.1 shige
54 1.8 matt static void obsled_attach(device_t, device_t, void *);
55 1.8 matt static int obsled_match(device_t, cfdata_t, void *);
56 1.3 shige static int obsled_sysctl_verify(SYSCTLFN_PROTO);
57 1.3 shige static void obsled_set_state(struct obsled_softc *);
58 1.1 shige
59 1.8 matt CFATTACH_DECL_NEW(obsled, sizeof(struct obsled_softc),
60 1.1 shige obsled_match, obsled_attach, NULL, NULL);
61 1.1 shige
62 1.1 shige static int
63 1.8 matt obsled_match(device_t parent, cfdata_t cf, void *aux)
64 1.1 shige {
65 1.1 shige struct gpio_attach_args *ga = aux;
66 1.1 shige
67 1.1 shige /* XXX: support only OpenBlockS266 LED */
68 1.2 shige if (ga->ga_addr == OBS266_GPIO_LED1)
69 1.1 shige return (1);
70 1.2 shige else if (ga->ga_addr == OBS266_GPIO_LED2)
71 1.1 shige return (1);
72 1.2 shige else if (ga->ga_addr == OBS266_GPIO_LED4)
73 1.1 shige return (1);
74 1.1 shige
75 1.1 shige return (0);
76 1.1 shige }
77 1.1 shige
78 1.1 shige static void
79 1.8 matt obsled_attach(device_t parent, device_t self, void *aux)
80 1.1 shige {
81 1.8 matt struct obsled_softc *sc = device_private(self);
82 1.1 shige struct gpio_attach_args *ga = aux;
83 1.3 shige struct sysctlnode *node;
84 1.3 shige int err, node_mib;
85 1.3 shige char led_name[5];
86 1.8 matt /* int led = (1 << device_unit(sc->sc_dev)); */
87 1.3 shige
88 1.3 shige snprintf(led_name, sizeof(led_name),
89 1.8 matt "led%d", (1 << device_unit(sc->sc_dev)) & 0x7);
90 1.3 shige aprint_naive(": OpenBlockS %s\n", led_name);
91 1.3 shige aprint_normal(": OpenBlockS %s\n", led_name);
92 1.1 shige
93 1.8 matt sc->sc_dev = self;
94 1.1 shige sc->sc_tag = ga->ga_tag;
95 1.1 shige sc->sc_addr = ga->ga_addr;
96 1.3 shige sc->sc_led_state = 0;
97 1.1 shige
98 1.2 shige obs266_led_set(OBS266_LED_OFF);
99 1.3 shige
100 1.3 shige /* add sysctl interface */
101 1.3 shige err = sysctl_createv(NULL, 0,
102 1.3 shige NULL, NULL,
103 1.3 shige 0, CTLTYPE_NODE,
104 1.3 shige "hw",
105 1.3 shige NULL,
106 1.3 shige NULL, 0, NULL, 0,
107 1.3 shige CTL_HW, CTL_EOL);
108 1.3 shige if (err != 0)
109 1.3 shige return;
110 1.3 shige err = sysctl_createv(NULL, 0,
111 1.3 shige NULL, (const struct sysctlnode **)&node,
112 1.3 shige 0, CTLTYPE_NODE,
113 1.3 shige "obsled",
114 1.3 shige NULL,
115 1.3 shige NULL, 0, NULL, 0,
116 1.3 shige CTL_HW, CTL_CREATE, CTL_EOL);
117 1.3 shige if (err != 0)
118 1.3 shige return;
119 1.3 shige node_mib = node->sysctl_num;
120 1.3 shige err = sysctl_createv(NULL, 0,
121 1.3 shige NULL, (const struct sysctlnode **)&node,
122 1.3 shige CTLFLAG_READWRITE, CTLTYPE_INT,
123 1.3 shige led_name,
124 1.3 shige SYSCTL_DESCR("OpenBlockS LED state (0=off, 1=on)"),
125 1.3 shige obsled_sysctl_verify, 0, sc, 0,
126 1.3 shige CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
127 1.3 shige if (err != 0)
128 1.3 shige return;
129 1.3 shige
130 1.3 shige sc->sc_led_state_mib = node->sysctl_num;
131 1.1 shige #if 0
132 1.1 shige {
133 1.1 shige gpio_tag_t tag = sc->sc_tag;
134 1.1 shige (*(tag)->io_or_write)((tag)->cookie, sc->sc_addr, 0);
135 1.1 shige }
136 1.1 shige #endif
137 1.1 shige }
138 1.1 shige
139 1.3 shige static int
140 1.3 shige obsled_sysctl_verify(SYSCTLFN_ARGS)
141 1.3 shige {
142 1.3 shige struct obsled_softc *sc = rnode->sysctl_data;
143 1.3 shige struct sysctlnode node;
144 1.3 shige int err, state;
145 1.3 shige
146 1.3 shige node = *rnode;
147 1.3 shige state = sc->sc_led_state;
148 1.3 shige node.sysctl_data = &state;
149 1.3 shige
150 1.3 shige err = sysctl_lookup(SYSCTLFN_CALL(&node));
151 1.3 shige if (err != 0 || newp == NULL)
152 1.3 shige return err;
153 1.3 shige
154 1.3 shige if (node.sysctl_num == sc->sc_led_state_mib) {
155 1.3 shige if (state < 0 || state > 1)
156 1.3 shige return EINVAL;
157 1.3 shige sc->sc_led_state = state;
158 1.3 shige obsled_set_state(sc);
159 1.3 shige }
160 1.3 shige
161 1.3 shige return 0;
162 1.3 shige }
163 1.3 shige
164 1.3 shige static void
165 1.3 shige obsled_set_state(struct obsled_softc *sc)
166 1.3 shige {
167 1.3 shige gpio_tag_t tag = sc->sc_tag;
168 1.3 shige
169 1.3 shige (*(tag)->io_or_write)((tag)->cookie,
170 1.3 shige sc->sc_addr,
171 1.3 shige (~(sc->sc_led_state) & 1));
172 1.3 shige /* GPIO LED input ON=0/OFF=1 */
173 1.3 shige }
174 1.3 shige
175 1.3 shige /*
176 1.3 shige * Setting LED interface for inside kernel.
177 1.3 shige * Argumnt `led' is 3-bit LED state (led=0-7/ON=1/OFF=0).
178 1.3 shige */
179 1.1 shige void
180 1.2 shige obs266_led_set(int led)
181 1.1 shige {
182 1.7 dyoung device_t dv;
183 1.7 dyoung deviter_t di;
184 1.1 shige
185 1.3 shige /*
186 1.3 shige * Sarching "obsled" devices from device tree.
187 1.3 shige * Do you have something better idea?
188 1.3 shige */
189 1.7 dyoung for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
190 1.7 dyoung dv = deviter_next(&di)) {
191 1.7 dyoung if (device_is_a(dv, "obsles")) {
192 1.7 dyoung struct obsled_softc *sc = device_private(dv);
193 1.3 shige sc->sc_led_state =
194 1.7 dyoung (led & (1 << device_unit(dv))) >> device_unit(dv);
195 1.3 shige obsled_set_state(sc);
196 1.1 shige }
197 1.1 shige }
198 1.7 dyoung deviter_release(&di);
199 1.1 shige }
200