1/* $Id: mpcsa_leds.c,v 1.10 2023/12/20 13:55:18 thorpej Exp $ */ 2/* $NetBSD: mpcsa_leds.c,v 1.10 2023/12/20 13:55:18 thorpej 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.10 2023/12/20 13:55:18 thorpej 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 52int 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 61enum { 62 LMODE_NORMAL, /* normal mode */ 63 LMODE_COMM, /* communication mode */ 64 LMODE_BLINK /* blink mode */ 65}; 66 67typedef 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 81struct mpcsa_leds_softc { 82 struct spi_handle *sc_sh; 83 84#if NGPIO > 0 85 struct gpio_chipset_tag sc_gpio_chipset; 86 gpio_pin_t sc_pins[MPCSA_LEDS_NPINS]; 87#endif 88 89 callout_t sc_c; 90 led_state_t sc_leds[MPCSA_LEDS_NPINS]; 91 uint16_t sc_pinstate; 92 93 struct spi_chunk sc_spi_chunk; 94 struct spi_transfer sc_spi_transfer; 95}; 96 97static int mpcsa_leds_match(device_t , cfdata_t , void *); 98static void mpcsa_leds_attach(device_t , device_t , void *); 99static void mpcsa_leds_timer(void *aux); 100 101#if NGPIO > 0 102static int mpcsa_ledsbus_print(void *, const char *); 103static int mpcsa_leds_pin_read(void *, int); 104static void mpcsa_leds_pin_write(void *, int, int); 105static void mpcsa_leds_pin_ctl(void *, int, int); 106#endif 107 108#if 0 109static int mpcsa_leds_search(device_t , cfdata_t , const int *, void *); 110static int mpcsa_leds_print(void *, const char *); 111#endif 112 113CFATTACH_DECL_NEW(mpcsa_leds, sizeof(struct mpcsa_leds_softc), 114 mpcsa_leds_match, mpcsa_leds_attach, NULL, NULL); 115 116static struct mpcsa_leds_softc *mpcsa_leds_sc; 117 118static int 119mpcsa_leds_match(device_t parent, cfdata_t match, void *aux) 120{ 121 122 if (strcmp(match->cf_name, "mpcsa_leds")) 123 return 0; 124 125 return 2; 126} 127 128static void 129mpcsa_leds_attach(device_t parent, device_t self, void *aux) 130{ 131 struct mpcsa_leds_softc *sc = device_private(self); 132 struct spi_attach_args *sa = aux; 133#if NGPIO > 0 134 struct gpiobus_attach_args gba; 135#endif 136 int n; 137 int error; 138 139 aprint_naive(": output buffer\n"); 140 aprint_normal(": 74HC595 or compatible shift register(s)\n"); 141 142 error = spi_configure(self, sa->sa_handle, SPI_MODE_0, 143 SPI_FREQ_MHz(10)); 144 if (error) { 145 return; 146 } 147 148 sc->sc_sh = sa->sa_handle; 149 sc->sc_pinstate = 0xffff; 150 callout_init(&sc->sc_c, 0); 151 152#if NGPIO > 0 153 /* initialize and attach gpio(4) */ 154 for (n = 0; n < MPCSA_LEDS_NPINS; n++) { 155 sc->sc_pins[n].pin_num = n; 156 sc->sc_pins[n].pin_caps = (GPIO_PIN_OUTPUT 157 | GPIO_PIN_PUSHPULL); 158 sc->sc_pins[n].pin_flags = GPIO_PIN_OUTPUT | GPIO_PIN_LOW; 159 } 160 161 sc->sc_gpio_chipset.gp_cookie = sc; 162 sc->sc_gpio_chipset.gp_pin_read = mpcsa_leds_pin_read; 163 sc->sc_gpio_chipset.gp_pin_write = mpcsa_leds_pin_write; 164 sc->sc_gpio_chipset.gp_pin_ctl = mpcsa_leds_pin_ctl; 165 gba.gba_gc = &sc->sc_gpio_chipset; 166 gba.gba_pins = sc->sc_pins; 167 gba.gba_npins = MPCSA_LEDS_NPINS; 168 config_found(self, &gba, mpcsa_ledsbus_print, CFARGS_NONE); 169#endif 170 171 /* attach device */ 172// config_search_ia(mpcsa_leds_search, self, "mpcsa_leds", mpcsa_leds_print); 173 174 /* update leds ten times a second or so */ 175 mpcsa_leds_sc = sc; // @@@@ 176 sc->sc_spi_transfer.st_flags = SPI_F_DONE; 177 callout_reset(&sc->sc_c, mstohz(LEDS_UPDATE_INTERVAL), mpcsa_leds_timer, sc); 178 179 mpcsa_blink_led(LED_HB, 500); 180} 181 182 183 184 185#if NGPIO > 0 186static int 187mpcsa_ledsbus_print(void *aux, const char *name) 188{ 189 gpiobus_print(aux, name); 190 return (UNCONF); 191} 192 193 194static int 195mpcsa_leds_pin_read(void *arg, int pin) 196{ 197 struct mpcsa_leds_softc *sc = arg; 198 pin %= MPCSA_LEDS_NPINS; 199 return (sc->sc_pinstate & htobe16(1U << pin)) ? 1 : 0; 200} 201 202static void 203mpcsa_leds_pin_write(void *arg, int pin, int val) 204{ 205 struct mpcsa_leds_softc *sc = arg; 206 int s; 207 208 pin %= MPCSA_LEDS_NPINS; 209 if (!sc->sc_pins[pin].pin_caps) 210 return; 211 212 s = splserial(); 213 if (val) 214 sc->sc_pinstate |= htobe16(1U << pin); 215 else 216 sc->sc_pinstate &= htobe16(~(1U << pin)); 217 splx(s); 218 219 if (spi_send(sc->sc_sh, 2, (const void *)&sc->sc_pinstate) != 0) { 220 // @@@ do what? @@@ 221 } 222} 223 224 225static void 226mpcsa_leds_pin_ctl(void *arg, int pin, int flags) 227{ 228 struct mpcsa_leds_softc *sc = arg; 229 230 pin %= MPCSA_LEDS_NPINS; 231 if (!sc->sc_pins[pin].pin_caps) 232 return; 233 234 // hmm, I think there's nothing to do 235} 236#endif 237 238static void mpcsa_leds_timer(void *aux) 239{ 240 int n, s; 241 struct mpcsa_leds_softc *sc = aux; 242 uint16_t pins; 243 244 callout_schedule(&sc->sc_c, mstohz(LEDS_UPDATE_INTERVAL)); 245 246 s = splserial(); 247 if (!(sc->sc_spi_transfer.st_flags & SPI_F_DONE)) { 248 splx(s); 249 return; 250 } 251 252 253 pins = be16toh(sc->sc_pinstate); 254 255 for (n = 0; n < MPCSA_LEDS_NPINS; n++) { 256 switch (sc->sc_leds[n].l_mode) { 257 default: 258 continue; 259 260 case LMODE_COMM: 261 if (sc->sc_leds[n].l_comm_cnt > 0) { 262 if (sc->sc_leds[n].l_comm_cnt < INFINITE_BLINK) 263 sc->sc_leds[n].l_comm_cnt--; 264 else 265 sc->sc_leds[n].l_comm_cnt ^= 1; 266 } 267 if ((sc->sc_leds[n].l_conn_cnt > 0) ^ (sc->sc_leds[n].l_comm_cnt & 1)) 268 pins &= ~(1U << n); 269 else 270 pins |= (1U << n); 271 break; 272 273 case LMODE_BLINK: 274 if (--sc->sc_leds[n].l_blink_cnt <= 0) { 275 pins ^= (1U << n); 276 sc->sc_leds[n].l_blink_cnt = sc->sc_leds[n].l_blink_int; 277 } 278 break; 279 } 280 } 281 282 HTOBE16(pins); 283 sc->sc_pinstate = pins; 284 splx(s); 285 286 spi_transfer_init(&sc->sc_spi_transfer); 287 spi_chunk_init(&sc->sc_spi_chunk, 2, (const void *)&sc->sc_pinstate, NULL); 288 spi_transfer_add(&sc->sc_spi_transfer, &sc->sc_spi_chunk); 289 if (spi_transfer(sc->sc_sh, &sc->sc_spi_transfer) != 0) { 290 /* an error occurred! */ 291 } 292} 293 294void mpcsa_comm_led(int num, int count) 295{ 296 struct mpcsa_leds_softc *sc = mpcsa_leds_sc; 297 if (!sc || num < 1 || num > MPCSA_LEDS_NPINS) { 298 return; 299 } 300 num--; 301 count *= 2; 302 int s = splserial(); 303 if (sc->sc_leds[num].l_mode != LMODE_COMM) { 304 sc->sc_leds[num].l_mode = LMODE_COMM; 305 sc->sc_leds[num].l_conn_cnt = 0; 306 sc->sc_leds[num].l_comm_cnt = 0; 307 } 308 if (sc->sc_leds[num].l_comm_cnt < (count * 2 - 1)) 309 sc->sc_leds[num].l_comm_cnt += count; 310 else if ((count * 2) < sc->sc_leds[num].l_comm_cnt) 311 sc->sc_leds[num].l_comm_cnt = count * 2 - 2 - (sc->sc_leds[num].l_comm_cnt % 2); 312 splx(s); 313} 314 315void mpcsa_conn_led(int num, int ok) 316{ 317 struct mpcsa_leds_softc *sc = mpcsa_leds_sc; 318 if (!sc || num < 1 || num > MPCSA_LEDS_NPINS) 319 return; 320 num--; 321 int s = splserial(); 322 if (sc->sc_leds[num].l_mode != LMODE_COMM) { 323 sc->sc_leds[num].l_mode = LMODE_COMM; 324 sc->sc_leds[num].l_conn_cnt = 0; 325 sc->sc_leds[num].l_comm_cnt = 0; 326 } 327 if (ok) 328 sc->sc_leds[num].l_conn_cnt++; 329 else 330 sc->sc_leds[num].l_conn_cnt--; 331 splx(s); 332} 333 334void mpcsa_blink_led(int num, int interval) 335{ 336 struct mpcsa_leds_softc *sc = mpcsa_leds_sc; 337 if (!sc || num < 1 || num > MPCSA_LEDS_NPINS) { 338 return; 339 } 340 interval = (interval + LEDS_UPDATE_INTERVAL + 1) / LEDS_UPDATE_INTERVAL; 341 num--; 342 int s = splserial(); 343 if (sc->sc_leds[num].l_mode != LMODE_COMM) { 344 sc->sc_leds[num].l_mode = LMODE_BLINK; 345 sc->sc_leds[num].l_blink_cnt = interval; 346 } 347 sc->sc_leds[num].l_blink_int = interval; 348 splx(s); 349} 350