Home | History | Annotate | Line # | Download | only in dev
obsled.c revision 1.11
      1  1.11  andvar /*	$NetBSD: obsled.c,v 1.11 2024/01/15 19:44:07 andvar Exp $	*/
      2   1.1   shige 
      3   1.1   shige /*
      4   1.1   shige  * Copyright (c) 2004 Shigeyuki Fukushima.
      5   1.1   shige  * All rights reserved.
      6   1.1   shige  *
      7   1.1   shige  * Redistribution and use in source and binary forms, with or without
      8   1.1   shige  * modification, are permitted provided that the following conditions
      9   1.1   shige  * are met:
     10   1.1   shige  * 1. Redistributions of source code must retain the above copyright
     11   1.1   shige  *    notice, this list of conditions and the following disclaimer.
     12   1.1   shige  * 2. Redistributions in binary form must reproduce the above
     13   1.1   shige  *    copyright notice, this list of conditions and the following
     14   1.1   shige  *    disclaimer in the documentation and/or other materials provided
     15   1.1   shige  *    with the distribution.
     16   1.1   shige  * 3. The name of the author may not be used to endorse or promote
     17   1.1   shige  *    products derived from this software without specific prior
     18   1.1   shige  *    written permission.
     19   1.1   shige  *
     20   1.1   shige  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     21   1.1   shige  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     22   1.1   shige  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1   shige  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     24   1.1   shige  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1   shige  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     26   1.1   shige  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1   shige  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     28   1.1   shige  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29   1.1   shige  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30   1.1   shige  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1   shige  */
     32   1.1   shige 
     33   1.1   shige #include <sys/cdefs.h>
     34  1.11  andvar __KERNEL_RCSID(0, "$NetBSD: obsled.c,v 1.11 2024/01/15 19:44:07 andvar Exp $");
     35   1.1   shige 
     36   1.1   shige #include <sys/param.h>
     37   1.1   shige #include <sys/device.h>
     38   1.1   shige #include <sys/queue.h>
     39   1.3   shige #include <sys/sysctl.h>
     40   1.1   shige #include <sys/systm.h>
     41   1.1   shige 
     42   1.2   shige #include <machine/obs266.h>
     43   1.1   shige 
     44   1.1   shige #include <powerpc/ibm4xx/dev/gpiovar.h>
     45   1.1   shige 
     46   1.1   shige struct obsled_softc {
     47   1.8    matt         device_t sc_dev;
     48   1.1   shige 	gpio_tag_t sc_tag;
     49   1.1   shige         int sc_addr;
     50   1.3   shige 	int sc_led_state;	/* LED status (ON=1/OFF=0) */
     51   1.3   shige 	int sc_led_state_mib;
     52   1.1   shige };
     53   1.1   shige 
     54   1.8    matt static void	obsled_attach(device_t, device_t, void *);
     55   1.8    matt static int	obsled_match(device_t, cfdata_t, void *);
     56   1.3   shige static int	obsled_sysctl_verify(SYSCTLFN_PROTO);
     57   1.3   shige static void	obsled_set_state(struct obsled_softc *);
     58   1.1   shige 
     59   1.8    matt CFATTACH_DECL_NEW(obsled, sizeof(struct obsled_softc),
     60   1.1   shige 	obsled_match, obsled_attach, NULL, NULL);
     61   1.1   shige 
     62   1.1   shige static int
     63   1.8    matt obsled_match(device_t parent, cfdata_t cf, void *aux)
     64   1.1   shige {
     65   1.1   shige         struct gpio_attach_args *ga = aux;
     66   1.1   shige 
     67   1.1   shige 	/* XXX: support only OpenBlockS266 LED */
     68   1.2   shige         if (ga->ga_addr == OBS266_GPIO_LED1)
     69   1.1   shige                 return (1);
     70   1.2   shige         else if (ga->ga_addr == OBS266_GPIO_LED2)
     71   1.1   shige                 return (1);
     72   1.2   shige         else if (ga->ga_addr == OBS266_GPIO_LED4)
     73   1.1   shige                 return (1);
     74   1.1   shige 
     75   1.1   shige         return (0);
     76   1.1   shige }
     77   1.1   shige 
     78   1.1   shige static void
     79   1.8    matt obsled_attach(device_t parent, device_t self, void *aux)
     80   1.1   shige {
     81   1.8    matt         struct obsled_softc *sc = device_private(self);
     82   1.1   shige         struct gpio_attach_args *ga = aux;
     83   1.3   shige 	struct sysctlnode *node;
     84   1.3   shige 	int err, node_mib;
     85   1.3   shige 	char led_name[5];
     86   1.8    matt 	/* int led = (1 << device_unit(sc->sc_dev)); */
     87   1.3   shige 
     88   1.3   shige 	snprintf(led_name, sizeof(led_name),
     89   1.8    matt 		"led%d", (1 << device_unit(sc->sc_dev)) & 0x7);
     90   1.3   shige         aprint_naive(": OpenBlockS %s\n", led_name);
     91   1.3   shige         aprint_normal(": OpenBlockS %s\n", led_name);
     92   1.1   shige 
     93   1.8    matt 	sc->sc_dev = self;
     94   1.1   shige         sc->sc_tag = ga->ga_tag;
     95   1.1   shige         sc->sc_addr = ga->ga_addr;
     96   1.3   shige 	sc->sc_led_state = 0;
     97   1.1   shige 
     98   1.2   shige 	obs266_led_set(OBS266_LED_OFF);
     99   1.3   shige 
    100   1.3   shige 	/* add sysctl interface */
    101   1.3   shige 	err = sysctl_createv(NULL, 0,
    102   1.3   shige 			NULL, (const struct sysctlnode **)&node,
    103   1.3   shige 			0, CTLTYPE_NODE,
    104   1.3   shige 			"obsled",
    105   1.3   shige 			NULL,
    106   1.3   shige 			NULL, 0, NULL, 0,
    107   1.3   shige 			CTL_HW, CTL_CREATE, CTL_EOL);
    108   1.3   shige 	if (err  != 0)
    109   1.3   shige 		return;
    110   1.3   shige 	node_mib = node->sysctl_num;
    111   1.3   shige 	err = sysctl_createv(NULL, 0,
    112   1.3   shige 			NULL, (const struct sysctlnode **)&node,
    113   1.3   shige 			CTLFLAG_READWRITE, CTLTYPE_INT,
    114   1.3   shige 			led_name,
    115   1.3   shige 			SYSCTL_DESCR("OpenBlockS LED state (0=off, 1=on)"),
    116   1.9     dsl 			obsled_sysctl_verify, 0, (void *)sc, 0,
    117   1.3   shige 			CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
    118   1.3   shige 	if (err  != 0)
    119   1.3   shige 		return;
    120   1.3   shige 
    121   1.3   shige 	sc->sc_led_state_mib = node->sysctl_num;
    122   1.1   shige #if 0
    123   1.1   shige 	{
    124   1.1   shige 		gpio_tag_t tag = sc->sc_tag;
    125   1.1   shige 	 	(*(tag)->io_or_write)((tag)->cookie, sc->sc_addr, 0);
    126   1.1   shige 	}
    127   1.1   shige #endif
    128   1.1   shige }
    129   1.1   shige 
    130   1.3   shige static int
    131   1.3   shige obsled_sysctl_verify(SYSCTLFN_ARGS)
    132   1.3   shige {
    133   1.3   shige 	struct obsled_softc *sc = rnode->sysctl_data;
    134   1.3   shige 	struct sysctlnode node;
    135   1.3   shige 	int err, state;
    136   1.3   shige 
    137   1.3   shige 	node = *rnode;
    138   1.3   shige 	state = sc->sc_led_state;
    139   1.3   shige 	node.sysctl_data = &state;
    140   1.3   shige 
    141   1.3   shige 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
    142   1.3   shige 	if (err != 0 || newp == NULL)
    143   1.3   shige 		return err;
    144   1.3   shige 
    145   1.3   shige 	if (node.sysctl_num == sc->sc_led_state_mib) {
    146   1.3   shige 		if (state < 0 || state > 1)
    147   1.3   shige 			return EINVAL;
    148   1.3   shige 		sc->sc_led_state = state;
    149   1.3   shige 		obsled_set_state(sc);
    150   1.3   shige 	}
    151   1.3   shige 
    152   1.3   shige 	return 0;
    153   1.3   shige }
    154   1.3   shige 
    155   1.3   shige static void
    156   1.3   shige obsled_set_state(struct obsled_softc *sc)
    157   1.3   shige {
    158   1.3   shige 	gpio_tag_t tag = sc->sc_tag;
    159   1.3   shige 
    160   1.3   shige 	(*(tag)->io_or_write)((tag)->cookie,
    161   1.3   shige 				sc->sc_addr,
    162   1.3   shige 				(~(sc->sc_led_state) & 1));
    163   1.3   shige 	/* GPIO LED input ON=0/OFF=1 */
    164   1.3   shige }
    165   1.3   shige 
    166   1.3   shige /*
    167   1.3   shige  * Setting LED interface for inside kernel.
    168  1.11  andvar  * Argument `led' is 3-bit LED state (led=0-7/ON=1/OFF=0).
    169   1.3   shige  */
    170   1.1   shige void
    171   1.2   shige obs266_led_set(int led)
    172   1.1   shige {
    173   1.7  dyoung 	device_t dv;
    174   1.7  dyoung 	deviter_t di;
    175   1.1   shige 
    176   1.3   shige 	/*
    177   1.3   shige 	 * Sarching "obsled" devices from device tree.
    178   1.3   shige 	 * Do you have something better idea?
    179   1.3   shige 	 */
    180   1.7  dyoung         for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
    181   1.7  dyoung 	     dv = deviter_next(&di)) {
    182   1.7  dyoung 		if (device_is_a(dv, "obsles")) {
    183   1.7  dyoung 			struct obsled_softc *sc = device_private(dv);
    184   1.3   shige 			sc->sc_led_state =
    185   1.7  dyoung 			    (led & (1 << device_unit(dv))) >> device_unit(dv);
    186   1.3   shige 			obsled_set_state(sc);
    187   1.1   shige 		}
    188   1.1   shige 	}
    189   1.7  dyoung 	deviter_release(&di);
    190   1.1   shige }
    191