1 1.3 jmcneill /* $NetBSD: gpiobutton.c,v 1.3 2015/10/04 18:35:44 jmcneill Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jmcneill * SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include "locators.h" 30 1.1 jmcneill 31 1.1 jmcneill #include <sys/cdefs.h> 32 1.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: gpiobutton.c,v 1.3 2015/10/04 18:35:44 jmcneill Exp $"); 33 1.1 jmcneill 34 1.1 jmcneill #include <sys/param.h> 35 1.1 jmcneill #include <sys/bus.h> 36 1.1 jmcneill #include <sys/device.h> 37 1.1 jmcneill #include <sys/intr.h> 38 1.1 jmcneill #include <sys/systm.h> 39 1.1 jmcneill #include <sys/kernel.h> 40 1.1 jmcneill #include <sys/gpio.h> 41 1.1 jmcneill 42 1.1 jmcneill #include <dev/sysmon/sysmonvar.h> 43 1.3 jmcneill #include <dev/sysmon/sysmon_taskq.h> 44 1.1 jmcneill 45 1.1 jmcneill #include <dev/gpio/gpiovar.h> 46 1.1 jmcneill 47 1.1 jmcneill #define GPIOBUTTON_POLL_INTERVAL mstohz(200) 48 1.1 jmcneill 49 1.1 jmcneill #define GPIOBUTTON_POLARITY_MASK 0x80 50 1.1 jmcneill #define GPIOBUTTON_POLARITY_ACTIVE_LOW 0 51 1.1 jmcneill #define GPIOBUTTON_POLARITY_ACTIVE_HIGH 1 52 1.1 jmcneill #define GPIOBUTTON_TYPE_MASK 0x0f 53 1.1 jmcneill #define GPIOBUTTON_TYPE_POWER 1 54 1.1 jmcneill #define GPIOBUTTON_TYPE_SLEEP 2 55 1.1 jmcneill 56 1.1 jmcneill static int gpiobutton_match(device_t, cfdata_t, void *); 57 1.1 jmcneill static void gpiobutton_attach(device_t, device_t, void *); 58 1.1 jmcneill 59 1.1 jmcneill struct gpiobutton_softc { 60 1.1 jmcneill device_t sc_dev; 61 1.1 jmcneill void *sc_gpio; 62 1.1 jmcneill struct gpio_pinmap sc_map; 63 1.1 jmcneill int sc_pinmap[1]; 64 1.1 jmcneill bool sc_active_high; 65 1.1 jmcneill 66 1.1 jmcneill struct sysmon_pswitch sc_smpsw; 67 1.1 jmcneill 68 1.1 jmcneill callout_t sc_tick; 69 1.1 jmcneill bool sc_state; 70 1.1 jmcneill }; 71 1.1 jmcneill 72 1.1 jmcneill static bool gpiobutton_is_pressed(struct gpiobutton_softc *); 73 1.1 jmcneill static void gpiobutton_tick(void *); 74 1.3 jmcneill static void gpiobutton_task(void *); 75 1.1 jmcneill 76 1.1 jmcneill CFATTACH_DECL_NEW(gpiobutton, sizeof(struct gpiobutton_softc), 77 1.1 jmcneill gpiobutton_match, gpiobutton_attach, NULL, NULL); 78 1.1 jmcneill 79 1.1 jmcneill static int 80 1.1 jmcneill gpiobutton_match(device_t parent, cfdata_t cf, void *aux) 81 1.1 jmcneill { 82 1.1 jmcneill struct gpio_attach_args * const ga = aux; 83 1.1 jmcneill 84 1.1 jmcneill if (strcmp(ga->ga_dvname, cf->cf_name) != 0) 85 1.1 jmcneill return 0; 86 1.1 jmcneill 87 1.1 jmcneill if (ga->ga_offset == -1 || gpio_npins(ga->ga_mask) != 1) 88 1.1 jmcneill return 0; 89 1.1 jmcneill 90 1.1 jmcneill const u_int type = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_TYPE_MASK); 91 1.2 jmcneill 92 1.1 jmcneill switch (type) { 93 1.1 jmcneill case GPIOBUTTON_TYPE_POWER: 94 1.1 jmcneill case GPIOBUTTON_TYPE_SLEEP: 95 1.1 jmcneill return 1; 96 1.1 jmcneill default: 97 1.1 jmcneill return 0; 98 1.1 jmcneill } 99 1.1 jmcneill } 100 1.1 jmcneill 101 1.1 jmcneill static void 102 1.1 jmcneill gpiobutton_attach(device_t parent, device_t self, void *aux) 103 1.1 jmcneill { 104 1.1 jmcneill struct gpiobutton_softc * const sc = device_private(self); 105 1.1 jmcneill struct gpio_attach_args * const ga = aux; 106 1.1 jmcneill const char *desc; 107 1.3 jmcneill int caps; 108 1.1 jmcneill 109 1.1 jmcneill const u_int type = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_TYPE_MASK); 110 1.1 jmcneill const u_int pol = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_POLARITY_MASK); 111 1.1 jmcneill 112 1.1 jmcneill sc->sc_dev = self; 113 1.1 jmcneill sc->sc_gpio = ga->ga_gpio; 114 1.1 jmcneill sc->sc_map.pm_map = sc->sc_pinmap; 115 1.1 jmcneill if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask, 116 1.1 jmcneill &sc->sc_map)) { 117 1.1 jmcneill aprint_error(": couldn't map pins\n"); 118 1.1 jmcneill return; 119 1.1 jmcneill } 120 1.1 jmcneill sc->sc_active_high = pol == GPIOBUTTON_POLARITY_ACTIVE_HIGH; 121 1.1 jmcneill 122 1.1 jmcneill caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, 0); 123 1.1 jmcneill if ((caps & GPIO_PIN_INPUT) == 0) { 124 1.1 jmcneill aprint_error(": pin is not an input pin\n"); 125 1.1 jmcneill return; 126 1.1 jmcneill } 127 1.1 jmcneill 128 1.1 jmcneill gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_INPUT); 129 1.1 jmcneill 130 1.1 jmcneill sc->sc_smpsw.smpsw_name = device_xname(self); 131 1.1 jmcneill switch (type) { 132 1.1 jmcneill case GPIOBUTTON_TYPE_POWER: 133 1.1 jmcneill sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER; 134 1.1 jmcneill desc = "Power"; 135 1.1 jmcneill break; 136 1.1 jmcneill case GPIOBUTTON_TYPE_SLEEP: 137 1.1 jmcneill sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP; 138 1.1 jmcneill desc = "Sleep"; 139 1.1 jmcneill break; 140 1.1 jmcneill default: 141 1.1 jmcneill panic("%s: impossible", __func__); 142 1.1 jmcneill } 143 1.1 jmcneill 144 1.1 jmcneill aprint_naive("\n"); 145 1.1 jmcneill aprint_normal(": %s button\n", desc); 146 1.1 jmcneill 147 1.1 jmcneill sysmon_pswitch_register(&sc->sc_smpsw); 148 1.1 jmcneill 149 1.1 jmcneill callout_init(&sc->sc_tick, CALLOUT_MPSAFE); 150 1.1 jmcneill callout_setfunc(&sc->sc_tick, gpiobutton_tick, sc); 151 1.1 jmcneill 152 1.3 jmcneill gpiobutton_tick(sc); 153 1.1 jmcneill } 154 1.1 jmcneill 155 1.1 jmcneill static bool 156 1.1 jmcneill gpiobutton_is_pressed(struct gpiobutton_softc *sc) 157 1.1 jmcneill { 158 1.1 jmcneill int val; 159 1.1 jmcneill 160 1.1 jmcneill val = gpio_pin_read(sc->sc_gpio, &sc->sc_map, 0); 161 1.1 jmcneill if (!sc->sc_active_high) 162 1.1 jmcneill val = !val; 163 1.1 jmcneill 164 1.1 jmcneill return val; 165 1.1 jmcneill } 166 1.1 jmcneill 167 1.1 jmcneill static void 168 1.1 jmcneill gpiobutton_tick(void *priv) 169 1.1 jmcneill { 170 1.1 jmcneill struct gpiobutton_softc * const sc = priv; 171 1.1 jmcneill 172 1.3 jmcneill const bool new_state = gpiobutton_is_pressed(sc); 173 1.3 jmcneill if (new_state != sc->sc_state) { 174 1.3 jmcneill sc->sc_state = new_state; 175 1.3 jmcneill sysmon_task_queue_sched(0, gpiobutton_task, sc); 176 1.3 jmcneill } 177 1.3 jmcneill callout_schedule(&sc->sc_tick, GPIOBUTTON_POLL_INTERVAL); 178 1.1 jmcneill } 179 1.1 jmcneill 180 1.1 jmcneill static void 181 1.3 jmcneill gpiobutton_task(void *priv) 182 1.1 jmcneill { 183 1.1 jmcneill struct gpiobutton_softc * const sc = priv; 184 1.1 jmcneill 185 1.3 jmcneill sysmon_pswitch_event(&sc->sc_smpsw, 186 1.3 jmcneill sc->sc_state ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED); 187 1.1 jmcneill } 188