xref: /netbsd-src/sys/arch/evbarm/mpcsa/mpcsa_leds.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$Id: mpcsa_leds.c,v 1.3 2010/06/19 19:47:34 matt Exp $	*/
2 /*	$NetBSD: mpcsa_leds.c,v 1.3 2010/06/19 19:47:34 matt Exp $	*/
3 
4 /*
5  * Copyright (c) 2007 Embedtronics Oy. All rights reserved.
6  *
7  * Based on arch/arm/ep93xx/epgpio.c,
8  * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mpcsa_leds.c,v 1.3 2010/06/19 19:47:34 matt Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/lock.h>
40 #include <sys/gpio.h>
41 #include <dev/spi/spivar.h>
42 #include <dev/gpio/gpiovar.h>
43 #include <evbarm/mpcsa/mpcsa_leds_var.h>
44 #include <evbarm/mpcsa/mpcsa_io.h>
45 #include "gpio.h"
46 #if NGPIO > 0
47 #include <sys/gpio.h>
48 #endif
49 #include "locators.h"
50 
51 #ifdef MPCSA_LEDS_DEBUG
52 int mpcsa_leds_debug = MPCSA_LEDS_DEBUG;
53 #define DPRINTFN(n,x)		if (mpcsa_leds_debug>(n)) printf x;
54 #else
55 #define DPRINTFN(n,x)
56 #endif
57 
58 #define	MPCSA_LEDS_NPINS		16
59 #define	LEDS_UPDATE_INTERVAL		100	/* update interval in milliseconds */
60 
61 enum {
62   LMODE_NORMAL,					/* normal mode */
63   LMODE_COMM,					/* communication mode */
64   LMODE_BLINK					/* blink mode */
65 };
66 
67 typedef struct led_state {
68 	int			l_mode;		/* current command */
69 	union {
70 		struct {
71 			int	l_conn_cnt;	/* connection count */
72 			int	l_comm_cnt;	/* comm/pkt count */
73 		};
74 		struct {
75 			int	l_blink_int;	/* blink interval */
76 			int	l_blink_cnt;	/* blink counter */
77 		};
78 	};
79 } led_state_t;
80 
81 struct mpcsa_leds_softc {
82 	struct device		sc_dev;
83 	struct spi_handle	*sc_sh;
84 
85 #if NGPIO > 0
86 	struct gpio_chipset_tag	sc_gpio_chipset;
87 	gpio_pin_t		sc_pins[MPCSA_LEDS_NPINS];
88 #endif
89 
90 	callout_t		sc_c;
91 	led_state_t		sc_leds[MPCSA_LEDS_NPINS];
92 	uint16_t		sc_pinstate;
93 
94 	struct spi_chunk	sc_spi_chunk;
95 	struct spi_transfer	sc_spi_transfer;
96 };
97 
98 static int mpcsa_leds_match(device_t , cfdata_t , void *);
99 static void mpcsa_leds_attach(device_t , device_t , void *);
100 static void mpcsa_leds_timer(void *aux);
101 
102 #if NGPIO > 0
103 static int mpcsa_ledsbus_print(void *, const char *);
104 static int mpcsa_leds_pin_read(void *, int);
105 static void mpcsa_leds_pin_write(void *, int, int);
106 static void mpcsa_leds_pin_ctl(void *, int, int);
107 #endif
108 
109 #if 0
110 static int mpcsa_leds_search(device_t , cfdata_t , const int *, void *);
111 static int mpcsa_leds_print(void *, const char *);
112 #endif
113 
114 CFATTACH_DECL(mpcsa_leds, sizeof(struct mpcsa_leds_softc),
115 	      mpcsa_leds_match, mpcsa_leds_attach, NULL, NULL);
116 
117 static struct mpcsa_leds_softc *mpcsa_leds_sc;
118 
119 static int
120 mpcsa_leds_match(device_t parent, cfdata_t match, void *aux)
121 {
122 	struct spi_attach_args *sa = aux;
123 
124 	if (strcmp(match->cf_name, "mpcsa_leds"))
125 		return 0;
126 
127 	if (spi_configure(sa->sa_handle, SPI_MODE_0, 10000000))
128 		return 0;
129 
130 	return 2;
131 }
132 
133 static void
134 mpcsa_leds_attach(device_t parent, device_t self, void *aux)
135 {
136 	struct mpcsa_leds_softc *sc = device_private(self);
137 	struct spi_attach_args *sa = aux;
138 #if NGPIO > 0
139 	struct gpiobus_attach_args gba;
140 #endif
141 	int n;
142 
143 	aprint_naive(": output buffer\n");
144 	aprint_normal(": 74HC595 or compatible shift register(s)\n");
145 
146 	sc->sc_sh = sa->sa_handle;
147 	sc->sc_pinstate = 0xffff;
148 	callout_init(&sc->sc_c, 0);
149 
150 #if NGPIO > 0
151 	/* initialize and attach gpio(4) */
152 	for (n = 0; n < MPCSA_LEDS_NPINS; n++) {
153 		sc->sc_pins[n].pin_num = n;
154 		sc->sc_pins[n].pin_caps = (GPIO_PIN_OUTPUT
155 					   | GPIO_PIN_PUSHPULL);
156 		sc->sc_pins[n].pin_flags = GPIO_PIN_OUTPUT | GPIO_PIN_LOW;
157 	}
158 
159 	sc->sc_gpio_chipset.gp_cookie = sc;
160 	sc->sc_gpio_chipset.gp_pin_read = mpcsa_leds_pin_read;
161 	sc->sc_gpio_chipset.gp_pin_write = mpcsa_leds_pin_write;
162 	sc->sc_gpio_chipset.gp_pin_ctl = mpcsa_leds_pin_ctl;
163 	gba.gba_gc = &sc->sc_gpio_chipset;
164 	gba.gba_pins = sc->sc_pins;
165 	gba.gba_npins = MPCSA_LEDS_NPINS;
166 	config_found_ia(self, "gpiobus", &gba, mpcsa_ledsbus_print);
167 #endif
168 
169 	/* attach device */
170 //	config_search_ia(mpcsa_leds_search, self, "mpcsa_leds", mpcsa_leds_print);
171 
172 	/* update leds ten times a second or so */
173 	mpcsa_leds_sc = sc; // @@@@
174 	sc->sc_spi_transfer.st_flags = SPI_F_DONE;
175 	callout_reset(&sc->sc_c, mstohz(LEDS_UPDATE_INTERVAL), mpcsa_leds_timer, sc);
176 
177 	mpcsa_blink_led(LED_HB, 500);
178 }
179 
180 
181 
182 
183 #if NGPIO > 0
184 static int
185 mpcsa_ledsbus_print(void *aux, const char *name)
186 {
187 	gpiobus_print(aux, name);
188 	return (UNCONF);
189 }
190 
191 
192 static int
193 mpcsa_leds_pin_read(void *arg, int pin)
194 {
195 	struct mpcsa_leds_softc *sc = arg;
196 	pin %= MPCSA_LEDS_NPINS;
197 	return (sc->sc_pinstate & htobe16(1U << pin)) ? 1 : 0;
198 }
199 
200 static void
201 mpcsa_leds_pin_write(void *arg, int pin, int val)
202 {
203 	struct mpcsa_leds_softc *sc = arg;
204 	int s;
205 
206 	pin %= MPCSA_LEDS_NPINS;
207 	if (!sc->sc_pins[pin].pin_caps)
208 		return;
209 
210 	s = splserial();
211 	if (val)
212 		sc->sc_pinstate |= htobe16(1U << pin);
213 	else
214 		sc->sc_pinstate &= htobe16(~(1U << pin));
215 	splx(s);
216 
217 	if (spi_send(sc->sc_sh, 2, (const void *)&sc->sc_pinstate) != 0) {
218 		// @@@ do what? @@@
219 	}
220 }
221 
222 
223 static void
224 mpcsa_leds_pin_ctl(void *arg, int pin, int flags)
225 {
226 	struct mpcsa_leds_softc *sc = arg;
227 
228 	pin %= MPCSA_LEDS_NPINS;
229 	if (!sc->sc_pins[pin].pin_caps)
230 		return;
231 
232 	// hmm, I think there's nothing to do
233 }
234 #endif
235 
236 static void mpcsa_leds_timer(void *aux)
237 {
238 	int n, s;
239 	struct mpcsa_leds_softc *sc = aux;
240 	u_int16_t pins;
241 
242 	callout_schedule(&sc->sc_c, mstohz(LEDS_UPDATE_INTERVAL));
243 
244 	s = splserial();
245 	if (!(sc->sc_spi_transfer.st_flags & SPI_F_DONE)) {
246 		splx(s);
247 		return;
248 	}
249 
250 
251 	pins = be16toh(sc->sc_pinstate);
252 
253 	for (n = 0; n < MPCSA_LEDS_NPINS; n++) {
254 		switch (sc->sc_leds[n].l_mode) {
255 		default:
256 			continue;
257 
258 		case LMODE_COMM:
259 			if (sc->sc_leds[n].l_comm_cnt > 0) {
260 				if (sc->sc_leds[n].l_comm_cnt < INFINITE_BLINK)
261 					sc->sc_leds[n].l_comm_cnt--;
262 				else
263 					sc->sc_leds[n].l_comm_cnt ^= 1;
264 			}
265 			if ((sc->sc_leds[n].l_conn_cnt > 0) ^ (sc->sc_leds[n].l_comm_cnt & 1))
266 				pins &= ~(1U << n);
267 			else
268 				pins |= (1U << n);
269 			break;
270 
271 		case LMODE_BLINK:
272 			if (--sc->sc_leds[n].l_blink_cnt <= 0) {
273 				pins ^= (1U << n);
274 				sc->sc_leds[n].l_blink_cnt = sc->sc_leds[n].l_blink_int;
275 			}
276 			break;
277 		}
278 	}
279 
280 	HTOBE16(pins);
281 	sc->sc_pinstate = pins;
282 	splx(s);
283 
284 	spi_transfer_init(&sc->sc_spi_transfer);
285 	spi_chunk_init(&sc->sc_spi_chunk, 2, (const void *)&sc->sc_pinstate, NULL);
286 	spi_transfer_add(&sc->sc_spi_transfer, &sc->sc_spi_chunk);
287 	if (spi_transfer(sc->sc_sh, &sc->sc_spi_transfer) != 0) {
288 		/* an error occurred! */
289 	}
290 }
291 
292 void mpcsa_comm_led(int num, int count)
293 {
294 	struct mpcsa_leds_softc *sc = mpcsa_leds_sc;
295 	if (!sc || num < 1 || num > MPCSA_LEDS_NPINS) {
296 		return;
297 	}
298 	num--;
299 	count *= 2;
300 	int s = splserial();
301 	if (sc->sc_leds[num].l_mode != LMODE_COMM) {
302 		sc->sc_leds[num].l_mode = LMODE_COMM;
303 		sc->sc_leds[num].l_conn_cnt = 0;
304 		sc->sc_leds[num].l_comm_cnt = 0;
305 	}
306 	if (sc->sc_leds[num].l_comm_cnt < (count * 2 - 1))
307 		sc->sc_leds[num].l_comm_cnt += count;
308 	else if ((count * 2) < sc->sc_leds[num].l_comm_cnt)
309 		sc->sc_leds[num].l_comm_cnt = count * 2 - 2 - (sc->sc_leds[num].l_comm_cnt % 2);
310 	splx(s);
311 }
312 
313 void mpcsa_conn_led(int num, int ok)
314 {
315 	struct mpcsa_leds_softc *sc = mpcsa_leds_sc;
316 	if (!sc || num < 1 || num > MPCSA_LEDS_NPINS)
317 		return;
318 	num--;
319 	int s = splserial();
320 	if (sc->sc_leds[num].l_mode != LMODE_COMM) {
321 		sc->sc_leds[num].l_mode = LMODE_COMM;
322 		sc->sc_leds[num].l_conn_cnt = 0;
323 		sc->sc_leds[num].l_comm_cnt = 0;
324 	}
325 	if (ok)
326 		sc->sc_leds[num].l_conn_cnt++;
327 	else
328 		sc->sc_leds[num].l_conn_cnt--;
329 	splx(s);
330 }
331 
332 void mpcsa_blink_led(int num, int interval)
333 {
334 	struct mpcsa_leds_softc *sc = mpcsa_leds_sc;
335 	if (!sc || num < 1 || num > MPCSA_LEDS_NPINS) {
336 		return;
337 	}
338 	interval = (interval + LEDS_UPDATE_INTERVAL + 1) / LEDS_UPDATE_INTERVAL;
339 	num--;
340 	int s = splserial();
341 	if (sc->sc_leds[num].l_mode != LMODE_COMM) {
342 		sc->sc_leds[num].l_mode = LMODE_BLINK;
343 		sc->sc_leds[num].l_blink_cnt = interval;
344 	}
345 	sc->sc_leds[num].l_blink_int = interval;
346 	splx(s);
347 }
348