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