Home | History | Annotate | Line # | Download | only in dev
obsled.c revision 1.6.66.1
      1  1.6.66.1     yamt /*	$NetBSD: obsled.c,v 1.6.66.1 2010/03/11 15:02:20 yamt 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.6.66.1     yamt __KERNEL_RCSID(0, "$NetBSD: obsled.c,v 1.6.66.1 2010/03/11 15:02:20 yamt 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.1    shige         struct device 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.1    shige static void	obsled_attach(struct device *, struct device *, void *);
     55       1.1    shige static int	obsled_match(struct device *, struct cfdata *, 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.1    shige CFATTACH_DECL(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.1    shige obsled_match(struct device *parent, struct cfdata *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.1    shige obsled_attach(struct device *parent, struct device *self, void *aux)
     80       1.1    shige {
     81       1.1    shige         struct obsled_softc *sc = (struct obsled_softc *)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.6  thorpej 	/* int led = (1 << device_unit(&sc->sc_dev)); */
     87       1.3    shige 
     88       1.3    shige 	snprintf(led_name, sizeof(led_name),
     89       1.6  thorpej 		"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.1    shige         sc->sc_tag = ga->ga_tag;
     94       1.1    shige         sc->sc_addr = ga->ga_addr;
     95       1.3    shige 	sc->sc_led_state = 0;
     96       1.1    shige 
     97       1.2    shige 	obs266_led_set(OBS266_LED_OFF);
     98       1.3    shige 
     99       1.3    shige 	/* add sysctl interface */
    100       1.3    shige 	err = sysctl_createv(NULL, 0,
    101       1.3    shige 			NULL, NULL,
    102       1.3    shige 			0, CTLTYPE_NODE,
    103       1.3    shige 			"hw",
    104       1.3    shige 			NULL,
    105       1.3    shige 			NULL, 0, NULL, 0,
    106       1.3    shige 			CTL_HW, CTL_EOL);
    107       1.3    shige 	if (err != 0)
    108       1.3    shige 		return;
    109       1.3    shige 	err = sysctl_createv(NULL, 0,
    110       1.3    shige 			NULL, (const struct sysctlnode **)&node,
    111       1.3    shige 			0, CTLTYPE_NODE,
    112       1.3    shige 			"obsled",
    113       1.3    shige 			NULL,
    114       1.3    shige 			NULL, 0, NULL, 0,
    115       1.3    shige 			CTL_HW, CTL_CREATE, CTL_EOL);
    116       1.3    shige 	if (err  != 0)
    117       1.3    shige 		return;
    118       1.3    shige 	node_mib = node->sysctl_num;
    119       1.3    shige 	err = sysctl_createv(NULL, 0,
    120       1.3    shige 			NULL, (const struct sysctlnode **)&node,
    121       1.3    shige 			CTLFLAG_READWRITE, CTLTYPE_INT,
    122       1.3    shige 			led_name,
    123       1.3    shige 			SYSCTL_DESCR("OpenBlockS LED state (0=off, 1=on)"),
    124       1.3    shige 			obsled_sysctl_verify, 0, sc, 0,
    125       1.3    shige 			CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
    126       1.3    shige 	if (err  != 0)
    127       1.3    shige 		return;
    128       1.3    shige 
    129       1.3    shige 	sc->sc_led_state_mib = node->sysctl_num;
    130       1.1    shige #if 0
    131       1.1    shige 	{
    132       1.1    shige 		gpio_tag_t tag = sc->sc_tag;
    133       1.1    shige 	 	(*(tag)->io_or_write)((tag)->cookie, sc->sc_addr, 0);
    134       1.1    shige 	}
    135       1.1    shige #endif
    136       1.1    shige }
    137       1.1    shige 
    138       1.3    shige static int
    139       1.3    shige obsled_sysctl_verify(SYSCTLFN_ARGS)
    140       1.3    shige {
    141       1.3    shige 	struct obsled_softc *sc = rnode->sysctl_data;
    142       1.3    shige 	struct sysctlnode node;
    143       1.3    shige 	int err, state;
    144       1.3    shige 
    145       1.3    shige 	node = *rnode;
    146       1.3    shige 	state = sc->sc_led_state;
    147       1.3    shige 	node.sysctl_data = &state;
    148       1.3    shige 
    149       1.3    shige 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
    150       1.3    shige 	if (err != 0 || newp == NULL)
    151       1.3    shige 		return err;
    152       1.3    shige 
    153       1.3    shige 	if (node.sysctl_num == sc->sc_led_state_mib) {
    154       1.3    shige 		if (state < 0 || state > 1)
    155       1.3    shige 			return EINVAL;
    156       1.3    shige 		sc->sc_led_state = state;
    157       1.3    shige 		obsled_set_state(sc);
    158       1.3    shige 	}
    159       1.3    shige 
    160       1.3    shige 	return 0;
    161       1.3    shige }
    162       1.3    shige 
    163       1.3    shige static void
    164       1.3    shige obsled_set_state(struct obsled_softc *sc)
    165       1.3    shige {
    166       1.3    shige 	gpio_tag_t tag = sc->sc_tag;
    167       1.3    shige 
    168       1.3    shige 	(*(tag)->io_or_write)((tag)->cookie,
    169       1.3    shige 				sc->sc_addr,
    170       1.3    shige 				(~(sc->sc_led_state) & 1));
    171       1.3    shige 	/* GPIO LED input ON=0/OFF=1 */
    172       1.3    shige }
    173       1.3    shige 
    174       1.3    shige /*
    175       1.3    shige  * Setting LED interface for inside kernel.
    176       1.3    shige  * Argumnt `led' is 3-bit LED state (led=0-7/ON=1/OFF=0).
    177       1.3    shige  */
    178       1.1    shige void
    179       1.2    shige obs266_led_set(int led)
    180       1.1    shige {
    181  1.6.66.1     yamt 	device_t dv;
    182  1.6.66.1     yamt 	deviter_t di;
    183       1.1    shige 
    184       1.3    shige 	/*
    185       1.3    shige 	 * Sarching "obsled" devices from device tree.
    186       1.3    shige 	 * Do you have something better idea?
    187       1.3    shige 	 */
    188  1.6.66.1     yamt         for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
    189  1.6.66.1     yamt 	     dv = deviter_next(&di)) {
    190  1.6.66.1     yamt 		if (device_is_a(dv, "obsles")) {
    191  1.6.66.1     yamt 			struct obsled_softc *sc = device_private(dv);
    192       1.3    shige 			sc->sc_led_state =
    193  1.6.66.1     yamt 			    (led & (1 << device_unit(dv))) >> device_unit(dv);
    194       1.3    shige 			obsled_set_state(sc);
    195       1.1    shige 		}
    196       1.1    shige 	}
    197  1.6.66.1     yamt 	deviter_release(&di);
    198       1.1    shige }
    199