Home | History | Annotate | Line # | Download | only in dev
obsled.c revision 1.2.4.1
      1  1.2.4.1   yamt /*	$NetBSD: obsled.c,v 1.2.4.1 2006/06/21 14:51:08 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.2.4.1   yamt __KERNEL_RCSID(0, "$NetBSD: obsled.c,v 1.2.4.1 2006/06/21 14:51:08 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.2.4.1   yamt #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.2.4.1   yamt 	int sc_led_state;	/* LED status (ON=1/OFF=0) */
     51  1.2.4.1   yamt 	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.2.4.1   yamt static int	obsled_sysctl_verify(SYSCTLFN_PROTO);
     57  1.2.4.1   yamt 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.2.4.1   yamt 	struct sysctlnode *node;
     84  1.2.4.1   yamt 	int err, node_mib;
     85  1.2.4.1   yamt 	char led_name[5];
     86  1.2.4.1   yamt 	/* int led = (1 << device_unit(&sc->sc_dev)); */
     87  1.2.4.1   yamt 
     88  1.2.4.1   yamt 	snprintf(led_name, sizeof(led_name),
     89  1.2.4.1   yamt 		"led%d", (1 << device_unit(&sc->sc_dev)) & 0x7);
     90  1.2.4.1   yamt         aprint_naive(": OpenBlockS %s\n", led_name);
     91  1.2.4.1   yamt         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.2.4.1   yamt 	sc->sc_led_state = 0;
     96      1.1  shige 
     97      1.2  shige 	obs266_led_set(OBS266_LED_OFF);
     98  1.2.4.1   yamt 
     99  1.2.4.1   yamt 	/* add sysctl interface */
    100  1.2.4.1   yamt 	err = sysctl_createv(NULL, 0,
    101  1.2.4.1   yamt 			NULL, NULL,
    102  1.2.4.1   yamt 			0, CTLTYPE_NODE,
    103  1.2.4.1   yamt 			"hw",
    104  1.2.4.1   yamt 			NULL,
    105  1.2.4.1   yamt 			NULL, 0, NULL, 0,
    106  1.2.4.1   yamt 			CTL_HW, CTL_EOL);
    107  1.2.4.1   yamt 	if (err != 0)
    108  1.2.4.1   yamt 		return;
    109  1.2.4.1   yamt 	err = sysctl_createv(NULL, 0,
    110  1.2.4.1   yamt 			NULL, (const struct sysctlnode **)&node,
    111  1.2.4.1   yamt 			0, CTLTYPE_NODE,
    112  1.2.4.1   yamt 			"obsled",
    113  1.2.4.1   yamt 			NULL,
    114  1.2.4.1   yamt 			NULL, 0, NULL, 0,
    115  1.2.4.1   yamt 			CTL_HW, CTL_CREATE, CTL_EOL);
    116  1.2.4.1   yamt 	if (err  != 0)
    117  1.2.4.1   yamt 		return;
    118  1.2.4.1   yamt 	node_mib = node->sysctl_num;
    119  1.2.4.1   yamt 	err = sysctl_createv(NULL, 0,
    120  1.2.4.1   yamt 			NULL, (const struct sysctlnode **)&node,
    121  1.2.4.1   yamt 			CTLFLAG_READWRITE, CTLTYPE_INT,
    122  1.2.4.1   yamt 			led_name,
    123  1.2.4.1   yamt 			SYSCTL_DESCR("OpenBlockS LED state (0=off, 1=on)"),
    124  1.2.4.1   yamt 			obsled_sysctl_verify, 0, sc, 0,
    125  1.2.4.1   yamt 			CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
    126  1.2.4.1   yamt 	if (err  != 0)
    127  1.2.4.1   yamt 		return;
    128  1.2.4.1   yamt 
    129  1.2.4.1   yamt 	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.2.4.1   yamt static int
    139  1.2.4.1   yamt obsled_sysctl_verify(SYSCTLFN_ARGS)
    140  1.2.4.1   yamt {
    141  1.2.4.1   yamt 	struct obsled_softc *sc = rnode->sysctl_data;
    142  1.2.4.1   yamt 	struct sysctlnode node;
    143  1.2.4.1   yamt 	int err, state;
    144  1.2.4.1   yamt 
    145  1.2.4.1   yamt 	node = *rnode;
    146  1.2.4.1   yamt 	state = sc->sc_led_state;
    147  1.2.4.1   yamt 	node.sysctl_data = &state;
    148  1.2.4.1   yamt 
    149  1.2.4.1   yamt 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
    150  1.2.4.1   yamt 	if (err != 0 || newp == NULL)
    151  1.2.4.1   yamt 		return err;
    152  1.2.4.1   yamt 
    153  1.2.4.1   yamt 	if (node.sysctl_num == sc->sc_led_state_mib) {
    154  1.2.4.1   yamt 		if (state < 0 || state > 1)
    155  1.2.4.1   yamt 			return EINVAL;
    156  1.2.4.1   yamt 		sc->sc_led_state = state;
    157  1.2.4.1   yamt 		obsled_set_state(sc);
    158  1.2.4.1   yamt 	}
    159  1.2.4.1   yamt 
    160  1.2.4.1   yamt 	return 0;
    161  1.2.4.1   yamt }
    162  1.2.4.1   yamt 
    163  1.2.4.1   yamt static void
    164  1.2.4.1   yamt obsled_set_state(struct obsled_softc *sc)
    165  1.2.4.1   yamt {
    166  1.2.4.1   yamt 	gpio_tag_t tag = sc->sc_tag;
    167  1.2.4.1   yamt 
    168  1.2.4.1   yamt 	(*(tag)->io_or_write)((tag)->cookie,
    169  1.2.4.1   yamt 				sc->sc_addr,
    170  1.2.4.1   yamt 				(~(sc->sc_led_state) & 1));
    171  1.2.4.1   yamt 	/* GPIO LED input ON=0/OFF=1 */
    172  1.2.4.1   yamt }
    173  1.2.4.1   yamt 
    174  1.2.4.1   yamt /*
    175  1.2.4.1   yamt  * Setting LED interface for inside kernel.
    176  1.2.4.1   yamt  * Argumnt `led' is 3-bit LED state (led=0-7/ON=1/OFF=0).
    177  1.2.4.1   yamt  */
    178      1.1  shige void
    179      1.2  shige obs266_led_set(int led)
    180      1.1  shige {
    181      1.1  shige 	struct device *dp = NULL;
    182      1.1  shige 	struct devicelist *dlp = &alldevs;
    183      1.1  shige 
    184  1.2.4.1   yamt 	/*
    185  1.2.4.1   yamt 	 * Sarching "obsled" devices from device tree.
    186  1.2.4.1   yamt 	 * Do you have something better idea?
    187  1.2.4.1   yamt 	 */
    188      1.1  shige         for (dp = TAILQ_FIRST(dlp); dp != NULL; dp = TAILQ_NEXT(dp, dv_list)) {
    189  1.2.4.1   yamt 		if (device_is_a(dp, "obsles")) {
    190      1.1  shige 			struct obsled_softc *sc = (struct obsled_softc *)dp;
    191  1.2.4.1   yamt 			sc->sc_led_state =
    192  1.2.4.1   yamt 			    (led & (1 << device_unit(dp))) >> device_unit(dp);
    193  1.2.4.1   yamt 			obsled_set_state(sc);
    194      1.1  shige 		}
    195      1.1  shige 	}
    196      1.1  shige }
    197