obsled.c revision 1.1 1 /* $NetBSD: obsled.c,v 1.1 2005/01/24 18:47:37 shige Exp $ */
2
3 /*
4 * Copyright (c) 2004 Shigeyuki Fukushima.
5 * All rights reserved.
6 *
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.
19 *
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.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: obsled.c,v 1.1 2005/01/24 18:47:37 shige Exp $");
35
36 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/queue.h>
39 #include <sys/systm.h>
40
41 #include <machine/obs405.h>
42
43 #include <powerpc/ibm4xx/dev/gpiovar.h>
44
45 struct obsled_softc {
46 struct device sc_dev;
47 gpio_tag_t sc_tag;
48 int sc_addr;
49 };
50
51 static void obsled_attach(struct device *, struct device *, void *);
52 static int obsled_match(struct device *, struct cfdata *, void *);
53
54 CFATTACH_DECL(obsled, sizeof(struct obsled_softc),
55 obsled_match, obsled_attach, NULL, NULL);
56
57 static int
58 obsled_match(struct device *parent, struct cfdata *cf, void *aux)
59 {
60 struct gpio_attach_args *ga = aux;
61
62 /* XXX: support only OpenBlockS266 LED */
63 if (ga->ga_addr == OBS405_GPIO_LED1)
64 return (1);
65 else if (ga->ga_addr == OBS405_GPIO_LED2)
66 return (1);
67 else if (ga->ga_addr == OBS405_GPIO_LED4)
68 return (1);
69
70 return (0);
71 }
72
73 static void
74 obsled_attach(struct device *parent, struct device *self, void *aux)
75 {
76 struct obsled_softc *sc = (struct obsled_softc *)self;
77 struct gpio_attach_args *ga = aux;
78 int led = (1 << sc->sc_dev.dv_unit);
79
80 aprint_naive(": OpenBlockS LED%d\n", led);
81 aprint_normal(": OpenBlockS LED%d\n", led);
82
83 sc->sc_tag = ga->ga_tag;
84 sc->sc_addr = ga->ga_addr;
85
86 obs405_led_set(OBS405_LED_OFF);
87 #if 0
88 {
89 gpio_tag_t tag = sc->sc_tag;
90 (*(tag)->io_or_write)((tag)->cookie, sc->sc_addr, 0);
91 }
92 #endif
93 }
94
95 void
96 obs405_led_set(int led)
97 {
98 struct device *dp = NULL;
99 struct devicelist *dlp = &alldevs;
100
101 for (dp = TAILQ_FIRST(dlp); dp != NULL; dp = TAILQ_NEXT(dp, dv_list)) {
102 if (dp->dv_cfdata != NULL
103 && strcmp(dp->dv_cfdata->cf_name, "obsled") == 0) {
104 struct obsled_softc *sc = (struct obsled_softc *)dp;
105 gpio_tag_t tag = sc->sc_tag;
106 int bit = (led & (1 << dp->dv_unit)) >> dp->dv_unit;
107 (*(tag)->io_or_write)((tag)->cookie,
108 sc->sc_addr, (~bit & 1));
109 }
110 }
111 }
112