xref: /netbsd-src/sys/arch/evbppc/obs405/dev/obsled.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: obsled.c,v 1.9 2012/06/02 21:36:41 dsl 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.9 2012/06/02 21:36:41 dsl 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         device_t 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(device_t, device_t, void *);
55 static int	obsled_match(device_t, cfdata_t, void *);
56 static int	obsled_sysctl_verify(SYSCTLFN_PROTO);
57 static void	obsled_set_state(struct obsled_softc *);
58 
59 CFATTACH_DECL_NEW(obsled, sizeof(struct obsled_softc),
60 	obsled_match, obsled_attach, NULL, NULL);
61 
62 static int
63 obsled_match(device_t parent, cfdata_t 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(device_t parent, device_t self, void *aux)
80 {
81         struct obsled_softc *sc = device_private(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_dev = self;
94         sc->sc_tag = ga->ga_tag;
95         sc->sc_addr = ga->ga_addr;
96 	sc->sc_led_state = 0;
97 
98 	obs266_led_set(OBS266_LED_OFF);
99 
100 	/* add sysctl interface */
101 	err = sysctl_createv(NULL, 0,
102 			NULL, NULL,
103 			0, CTLTYPE_NODE,
104 			"hw",
105 			NULL,
106 			NULL, 0, NULL, 0,
107 			CTL_HW, CTL_EOL);
108 	if (err != 0)
109 		return;
110 	err = sysctl_createv(NULL, 0,
111 			NULL, (const struct sysctlnode **)&node,
112 			0, CTLTYPE_NODE,
113 			"obsled",
114 			NULL,
115 			NULL, 0, NULL, 0,
116 			CTL_HW, CTL_CREATE, CTL_EOL);
117 	if (err  != 0)
118 		return;
119 	node_mib = node->sysctl_num;
120 	err = sysctl_createv(NULL, 0,
121 			NULL, (const struct sysctlnode **)&node,
122 			CTLFLAG_READWRITE, CTLTYPE_INT,
123 			led_name,
124 			SYSCTL_DESCR("OpenBlockS LED state (0=off, 1=on)"),
125 			obsled_sysctl_verify, 0, (void *)sc, 0,
126 			CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
127 	if (err  != 0)
128 		return;
129 
130 	sc->sc_led_state_mib = node->sysctl_num;
131 #if 0
132 	{
133 		gpio_tag_t tag = sc->sc_tag;
134 	 	(*(tag)->io_or_write)((tag)->cookie, sc->sc_addr, 0);
135 	}
136 #endif
137 }
138 
139 static int
140 obsled_sysctl_verify(SYSCTLFN_ARGS)
141 {
142 	struct obsled_softc *sc = rnode->sysctl_data;
143 	struct sysctlnode node;
144 	int err, state;
145 
146 	node = *rnode;
147 	state = sc->sc_led_state;
148 	node.sysctl_data = &state;
149 
150 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
151 	if (err != 0 || newp == NULL)
152 		return err;
153 
154 	if (node.sysctl_num == sc->sc_led_state_mib) {
155 		if (state < 0 || state > 1)
156 			return EINVAL;
157 		sc->sc_led_state = state;
158 		obsled_set_state(sc);
159 	}
160 
161 	return 0;
162 }
163 
164 static void
165 obsled_set_state(struct obsled_softc *sc)
166 {
167 	gpio_tag_t tag = sc->sc_tag;
168 
169 	(*(tag)->io_or_write)((tag)->cookie,
170 				sc->sc_addr,
171 				(~(sc->sc_led_state) & 1));
172 	/* GPIO LED input ON=0/OFF=1 */
173 }
174 
175 /*
176  * Setting LED interface for inside kernel.
177  * Argumnt `led' is 3-bit LED state (led=0-7/ON=1/OFF=0).
178  */
179 void
180 obs266_led_set(int led)
181 {
182 	device_t dv;
183 	deviter_t di;
184 
185 	/*
186 	 * Sarching "obsled" devices from device tree.
187 	 * Do you have something better idea?
188 	 */
189         for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
190 	     dv = deviter_next(&di)) {
191 		if (device_is_a(dv, "obsles")) {
192 			struct obsled_softc *sc = device_private(dv);
193 			sc->sc_led_state =
194 			    (led & (1 << device_unit(dv))) >> device_unit(dv);
195 			obsled_set_state(sc);
196 		}
197 	}
198 	deviter_release(&di);
199 }
200