xref: /netbsd-src/sys/arch/evbppc/obs405/dev/obsled.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: obsled.c,v 1.6 2006/03/28 17:38:24 thorpej 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.6 2006/03/28 17:38:24 thorpej Exp $");
35 
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>
41 
42 #include <machine/obs266.h>
43 
44 #include <powerpc/ibm4xx/dev/gpiovar.h>
45 
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;
52 };
53 
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 *);
58 
59 CFATTACH_DECL(obsled, sizeof(struct obsled_softc),
60 	obsled_match, obsled_attach, NULL, NULL);
61 
62 static int
63 obsled_match(struct device *parent, struct cfdata *cf, void *aux)
64 {
65         struct gpio_attach_args *ga = aux;
66 
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);
74 
75         return (0);
76 }
77 
78 static void
79 obsled_attach(struct device *parent, struct device *self, void *aux)
80 {
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)); */
87 
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);
92 
93         sc->sc_tag = ga->ga_tag;
94         sc->sc_addr = ga->ga_addr;
95 	sc->sc_led_state = 0;
96 
97 	obs266_led_set(OBS266_LED_OFF);
98 
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;
128 
129 	sc->sc_led_state_mib = node->sysctl_num;
130 #if 0
131 	{
132 		gpio_tag_t tag = sc->sc_tag;
133 	 	(*(tag)->io_or_write)((tag)->cookie, sc->sc_addr, 0);
134 	}
135 #endif
136 }
137 
138 static int
139 obsled_sysctl_verify(SYSCTLFN_ARGS)
140 {
141 	struct obsled_softc *sc = rnode->sysctl_data;
142 	struct sysctlnode node;
143 	int err, state;
144 
145 	node = *rnode;
146 	state = sc->sc_led_state;
147 	node.sysctl_data = &state;
148 
149 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
150 	if (err != 0 || newp == NULL)
151 		return err;
152 
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);
158 	}
159 
160 	return 0;
161 }
162 
163 static void
164 obsled_set_state(struct obsled_softc *sc)
165 {
166 	gpio_tag_t tag = sc->sc_tag;
167 
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 */
172 }
173 
174 /*
175  * Setting LED interface for inside kernel.
176  * Argumnt `led' is 3-bit LED state (led=0-7/ON=1/OFF=0).
177  */
178 void
179 obs266_led_set(int led)
180 {
181 	struct device *dp = NULL;
182 	struct devicelist *dlp = &alldevs;
183 
184 	/*
185 	 * Sarching "obsled" devices from device tree.
186 	 * Do you have something better idea?
187 	 */
188         for (dp = TAILQ_FIRST(dlp); dp != NULL; dp = TAILQ_NEXT(dp, dv_list)) {
189 		if (device_is_a(dp, "obsles")) {
190 			struct obsled_softc *sc = (struct obsled_softc *)dp;
191 			sc->sc_led_state =
192 			    (led & (1 << device_unit(dp))) >> device_unit(dp);
193 			obsled_set_state(sc);
194 		}
195 	}
196 }
197