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