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