Home | History | Annotate | Line # | Download | only in netwalker
      1  1.1  hkenken /*	$NetBSD: netwalker_lid.c,v 1.1 2014/05/06 11:08:51 hkenken Exp $	*/
      2  1.1  hkenken 
      3  1.1  hkenken /*
      4  1.1  hkenken  * Copyright (c) 2014  Genetec Corporation.  All rights reserved.
      5  1.1  hkenken  * Written by Hashimoto Kenichi for Genetec Corporation.
      6  1.1  hkenken  *
      7  1.1  hkenken  * Redistribution and use in source and binary forms, with or without
      8  1.1  hkenken  * modification, are permitted provided that the following conditions
      9  1.1  hkenken  * are met:
     10  1.1  hkenken  * 1. Redistributions of source code must retain the above copyright
     11  1.1  hkenken  *    notice, this list of conditions and the following disclaimer.
     12  1.1  hkenken  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  hkenken  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  hkenken  *    documentation and/or other materials provided with the distribution.
     15  1.1  hkenken  *
     16  1.1  hkenken  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     17  1.1  hkenken  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  hkenken  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  hkenken  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     20  1.1  hkenken  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  hkenken  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  hkenken  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  hkenken  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  hkenken  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  hkenken  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  hkenken  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  hkenken  */
     28  1.1  hkenken 
     29  1.1  hkenken #include <sys/cdefs.h>
     30  1.1  hkenken __KERNEL_RCSID(0, "$NetBSD: netwalker_lid.c,v 1.1 2014/05/06 11:08:51 hkenken Exp $");
     31  1.1  hkenken 
     32  1.1  hkenken #include <sys/param.h>
     33  1.1  hkenken #include <sys/systm.h>
     34  1.1  hkenken #include <sys/device.h>
     35  1.1  hkenken #include <sys/gpio.h>
     36  1.1  hkenken 
     37  1.1  hkenken #include <dev/gpio/gpiovar.h>
     38  1.1  hkenken #include <dev/sysmon/sysmonvar.h>
     39  1.1  hkenken #include <dev/sysmon/sysmon_taskq.h>
     40  1.1  hkenken 
     41  1.1  hkenken #include <dev/gpio/gpiovar.h>
     42  1.1  hkenken 
     43  1.1  hkenken #include <evbarm/netwalker/netwalker.h>
     44  1.1  hkenken 
     45  1.1  hkenken #define LID_PIN_INPUT	0
     46  1.1  hkenken #define LID_NPINS	1
     47  1.1  hkenken 
     48  1.1  hkenken struct netwalker_lid_softc {
     49  1.1  hkenken 	device_t sc_dev;
     50  1.1  hkenken 	void *sc_gpio;
     51  1.1  hkenken 	void *sc_intr;
     52  1.1  hkenken 
     53  1.1  hkenken 	struct gpio_pinmap sc_map;
     54  1.1  hkenken 	int sc_map_pins[LID_NPINS];
     55  1.1  hkenken 
     56  1.1  hkenken 	struct sysmon_pswitch sc_smpsw;
     57  1.1  hkenken 	int sc_state;
     58  1.1  hkenken };
     59  1.1  hkenken 
     60  1.1  hkenken static int netwalker_lid_match(device_t, cfdata_t, void *);
     61  1.1  hkenken static void netwalker_lid_attach(device_t, device_t, void *);
     62  1.1  hkenken static int netwalker_lid_detach(device_t, int);
     63  1.1  hkenken 
     64  1.1  hkenken CFATTACH_DECL_NEW(netwalker_lid, sizeof(struct netwalker_lid_softc),
     65  1.1  hkenken     netwalker_lid_match, netwalker_lid_attach, netwalker_lid_detach, NULL);
     66  1.1  hkenken 
     67  1.1  hkenken static void netwalker_lid_refresh(void *);
     68  1.1  hkenken static int netwalker_lid_intr(void *);
     69  1.1  hkenken 
     70  1.1  hkenken 
     71  1.1  hkenken static int
     72  1.1  hkenken netwalker_lid_match(device_t parent, cfdata_t cf, void * aux)
     73  1.1  hkenken {
     74  1.1  hkenken 	struct gpio_attach_args *ga = aux;
     75  1.1  hkenken 
     76  1.1  hkenken 	if (strcmp(ga->ga_dvname, cf->cf_name))
     77  1.1  hkenken 		return 0;
     78  1.1  hkenken 	if (ga->ga_offset == -1)
     79  1.1  hkenken 		return 0;
     80  1.1  hkenken 	/* check that we have enough pins */
     81  1.1  hkenken 	if (gpio_npins(ga->ga_mask) != LID_NPINS) {
     82  1.1  hkenken 		aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
     83  1.1  hkenken 		    ga->ga_mask);
     84  1.1  hkenken 		return 0;
     85  1.1  hkenken 	}
     86  1.1  hkenken 
     87  1.1  hkenken 	return 1;
     88  1.1  hkenken }
     89  1.1  hkenken 
     90  1.1  hkenken static void
     91  1.1  hkenken netwalker_lid_attach(device_t parent, device_t self, void *aux)
     92  1.1  hkenken {
     93  1.1  hkenken 	struct netwalker_lid_softc *sc = device_private(self);
     94  1.1  hkenken 	struct gpio_attach_args *ga = aux;
     95  1.1  hkenken 	int caps;
     96  1.1  hkenken 
     97  1.1  hkenken 	sc->sc_dev = self;
     98  1.1  hkenken 	sc->sc_gpio = ga->ga_gpio;
     99  1.1  hkenken 
    100  1.1  hkenken 	/* map pins */
    101  1.1  hkenken 	sc->sc_map.pm_map = sc->sc_map_pins;
    102  1.1  hkenken 	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask, &sc->sc_map)) {
    103  1.1  hkenken 		aprint_error(": couldn't map the pins\n");
    104  1.1  hkenken 		return;
    105  1.1  hkenken 	}
    106  1.1  hkenken 
    107  1.1  hkenken 	/* configure the input pin */
    108  1.1  hkenken 	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, LID_PIN_INPUT);
    109  1.1  hkenken 	if (!(caps & GPIO_PIN_INPUT)) {
    110  1.1  hkenken 		aprint_error(": pin is unable to read input\n");
    111  1.1  hkenken 		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    112  1.1  hkenken 		return;
    113  1.1  hkenken 	}
    114  1.1  hkenken 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, LID_PIN_INPUT,
    115  1.1  hkenken 	    GPIO_PIN_INPUT);
    116  1.1  hkenken 
    117  1.1  hkenken 	sc->sc_intr = intr_establish(GPIO3_IRQBASE + ga->ga_offset,
    118  1.1  hkenken 	    IPL_VM, IST_EDGE_BOTH, netwalker_lid_intr, sc);
    119  1.1  hkenken 	if (sc->sc_intr == NULL) {
    120  1.1  hkenken 		aprint_error(": couldn't establish interrupt\n");
    121  1.1  hkenken 		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    122  1.1  hkenken 		return;
    123  1.1  hkenken 	}
    124  1.1  hkenken 
    125  1.1  hkenken 	aprint_normal(": NETWALKER lid switch\n");
    126  1.1  hkenken 	aprint_naive(": NETWALKER lid switch\n");
    127  1.1  hkenken 
    128  1.1  hkenken 	if (!pmf_device_register(sc->sc_dev, NULL, NULL)) {
    129  1.1  hkenken 		aprint_error_dev(sc->sc_dev,
    130  1.1  hkenken 		    "couldn't establish lid handler\n");
    131  1.1  hkenken 	}
    132  1.1  hkenken 
    133  1.1  hkenken 	sysmon_task_queue_init();
    134  1.1  hkenken 	sc->sc_smpsw.smpsw_name = device_xname(self);
    135  1.1  hkenken 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_LID;
    136  1.1  hkenken 	sc->sc_state = PSWITCH_EVENT_RELEASED;
    137  1.1  hkenken 	sysmon_pswitch_register(&sc->sc_smpsw);
    138  1.1  hkenken }
    139  1.1  hkenken 
    140  1.1  hkenken static int
    141  1.1  hkenken netwalker_lid_detach(device_t self, int flags)
    142  1.1  hkenken {
    143  1.1  hkenken 	struct netwalker_lid_softc *sc = device_private(self);
    144  1.1  hkenken 
    145  1.1  hkenken 	if (sc->sc_intr != NULL)
    146  1.1  hkenken 		intr_disestablish(sc->sc_intr);
    147  1.1  hkenken 
    148  1.1  hkenken 	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    149  1.1  hkenken 	pmf_device_deregister(self);
    150  1.1  hkenken 	sysmon_task_queue_fini();
    151  1.1  hkenken 	return 0;
    152  1.1  hkenken }
    153  1.1  hkenken 
    154  1.1  hkenken static int
    155  1.1  hkenken netwalker_lid_intr(void *v)
    156  1.1  hkenken {
    157  1.1  hkenken 	struct netwalker_lid_softc *sc = v;
    158  1.1  hkenken 
    159  1.1  hkenken 	sysmon_task_queue_sched(0, netwalker_lid_refresh, sc);
    160  1.1  hkenken 	return 1;
    161  1.1  hkenken }
    162  1.1  hkenken 
    163  1.1  hkenken static void
    164  1.1  hkenken netwalker_lid_refresh(void *v)
    165  1.1  hkenken {
    166  1.1  hkenken 	struct netwalker_lid_softc *sc = v;
    167  1.1  hkenken 	int lid;
    168  1.1  hkenken 	int event;
    169  1.1  hkenken 
    170  1.1  hkenken 	lid = gpio_pin_read(sc->sc_gpio, &sc->sc_map, LID_PIN_INPUT);
    171  1.1  hkenken 	event = (lid == GPIO_PIN_HIGH) ?
    172  1.1  hkenken 	    PSWITCH_EVENT_RELEASED : PSWITCH_EVENT_PRESSED;
    173  1.1  hkenken 
    174  1.1  hkenken 	/* crude way to avoid duplicate events */
    175  1.1  hkenken 	if(event == sc->sc_state)
    176  1.1  hkenken 		return;
    177  1.1  hkenken 
    178  1.1  hkenken 	sc->sc_state = event;
    179  1.1  hkenken 	/* report the event */
    180  1.1  hkenken 	sysmon_pswitch_event(&sc->sc_smpsw, event);
    181  1.1  hkenken }
    182