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