1 /* $NetBSD: obsled.c,v 1.6 2006/03/28 17:38:24 thorpej Exp $ */
4 * Copyright (c) 2004 Shigeyuki Fukushima.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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>
50 int sc_led_state
; /* LED status (ON=1/OFF=0) */
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
);
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
)
70 else if (ga
->ga_addr
== OBS266_GPIO_LED2
)
72 else if (ga
->ga_addr
== OBS266_GPIO_LED4
)
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
;
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
;
97 obs266_led_set(OBS266_LED_OFF
);
99 /* add sysctl interface */
100 err
= sysctl_createv(NULL
, 0,
109 err
= sysctl_createv(NULL
, 0,
110 NULL
, (const struct sysctlnode
**)&node
,
115 CTL_HW
, CTL_CREATE
, CTL_EOL
);
118 node_mib
= node
->sysctl_num
;
119 err
= sysctl_createv(NULL
, 0,
120 NULL
, (const struct sysctlnode
**)&node
,
121 CTLFLAG_READWRITE
, CTLTYPE_INT
,
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
);
129 sc
->sc_led_state_mib
= node
->sysctl_num
;
132 gpio_tag_t tag
= sc
->sc_tag
;
133 (*(tag
)->io_or_write
)((tag
)->cookie
, sc
->sc_addr
, 0);
139 obsled_sysctl_verify(SYSCTLFN_ARGS
)
141 struct obsled_softc
*sc
= rnode
->sysctl_data
;
142 struct sysctlnode node
;
146 state
= sc
->sc_led_state
;
147 node
.sysctl_data
= &state
;
149 err
= sysctl_lookup(SYSCTLFN_CALL(&node
));
150 if (err
!= 0 || newp
== NULL
)
153 if (node
.sysctl_num
== sc
->sc_led_state_mib
) {
154 if (state
< 0 || state
> 1)
156 sc
->sc_led_state
= state
;
157 obsled_set_state(sc
);
164 obsled_set_state(struct obsled_softc
*sc
)
166 gpio_tag_t tag
= sc
->sc_tag
;
168 (*(tag
)->io_or_write
)((tag
)->cookie
,
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).
179 obs266_led_set(int led
)
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
);
193 (led
& (1 << device_unit(dv
))) >> device_unit(dv
);
194 obsled_set_state(sc
);
197 deviter_release(&di
);