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