1 1.5 riastrad /* $NetBSD: gpiolock.c,v 1.5 2017/10/28 04:53:56 riastradh Exp $ */ 2 1.1 mbalmer 3 1.1 mbalmer /* 4 1.1 mbalmer * Copyright (c) 2009 Marc Balmer <marc (at) msys.ch> 5 1.1 mbalmer * All rights reserved. 6 1.1 mbalmer * 7 1.1 mbalmer * Redistribution and use in source and binary forms, with or without 8 1.1 mbalmer * modification, are permitted provided that the following conditions 9 1.1 mbalmer * are met: 10 1.1 mbalmer * 1. Redistributions of source code must retain the above copyright 11 1.1 mbalmer * notice, this list of conditions and the following disclaimer. 12 1.1 mbalmer * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 mbalmer * notice, this list of conditions and the following disclaimer in the 14 1.1 mbalmer * documentation and/or other materials provided with the distribution. 15 1.1 mbalmer * 16 1.1 mbalmer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 mbalmer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 mbalmer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 mbalmer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 mbalmer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.1 mbalmer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 mbalmer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 mbalmer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 mbalmer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.1 mbalmer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 mbalmer */ 27 1.1 mbalmer 28 1.1 mbalmer /* 29 1.1 mbalmer * Driver for multi-position keylocks on GPIO pins 30 1.1 mbalmer */ 31 1.1 mbalmer 32 1.1 mbalmer #include "opt_keylock.h" 33 1.1 mbalmer 34 1.1 mbalmer #include <sys/param.h> 35 1.1 mbalmer #include <sys/device.h> 36 1.1 mbalmer #include <sys/gpio.h> 37 1.1 mbalmer 38 1.1 mbalmer #include <dev/gpio/gpiovar.h> 39 1.2 mbalmer #include <dev/keylock.h> 40 1.1 mbalmer 41 1.5 riastrad #include "ioconf.h" 42 1.5 riastrad 43 1.1 mbalmer #define GPIOLOCK_MAXPINS 4 44 1.1 mbalmer #define GPIOLOCK_MINPINS 2 45 1.1 mbalmer 46 1.1 mbalmer struct gpiolock_softc { 47 1.1 mbalmer void * sc_gpio; 48 1.1 mbalmer struct gpio_pinmap sc_map; 49 1.1 mbalmer int _map[GPIOLOCK_MAXPINS]; 50 1.1 mbalmer 51 1.1 mbalmer int sc_npins; 52 1.1 mbalmer int sc_data; 53 1.1 mbalmer int sc_dying; 54 1.1 mbalmer }; 55 1.1 mbalmer 56 1.1 mbalmer int gpiolock_match(device_t, cfdata_t, void *); 57 1.1 mbalmer void gpiolock_attach(device_t, device_t, void *); 58 1.1 mbalmer int gpiolock_detach(device_t, int); 59 1.1 mbalmer int gpiolock_activate(device_t, enum devact); 60 1.1 mbalmer int gpiolock_position(void *); 61 1.1 mbalmer 62 1.1 mbalmer CFATTACH_DECL_NEW(gpiolock, sizeof(struct gpiolock_softc), 63 1.1 mbalmer gpiolock_match, gpiolock_attach, gpiolock_detach, gpiolock_activate); 64 1.1 mbalmer 65 1.1 mbalmer int 66 1.1 mbalmer gpiolock_match(device_t parent, cfdata_t cf, 67 1.1 mbalmer void *aux) 68 1.1 mbalmer { 69 1.1 mbalmer struct gpio_attach_args *ga = aux; 70 1.1 mbalmer int npins; 71 1.1 mbalmer 72 1.1 mbalmer if (strcmp(ga->ga_dvname, cf->cf_name)) 73 1.1 mbalmer return 0; 74 1.1 mbalmer 75 1.1 mbalmer if (ga->ga_offset == -1) 76 1.1 mbalmer return 0; 77 1.1 mbalmer 78 1.1 mbalmer /* Check number of pins */ 79 1.1 mbalmer npins = gpio_npins(ga->ga_mask); 80 1.1 mbalmer if (npins < GPIOLOCK_MINPINS || npins > GPIOLOCK_MAXPINS) { 81 1.1 mbalmer aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name, 82 1.1 mbalmer ga->ga_mask); 83 1.1 mbalmer return 0; 84 1.1 mbalmer } 85 1.1 mbalmer 86 1.1 mbalmer return 1; 87 1.1 mbalmer } 88 1.1 mbalmer 89 1.1 mbalmer void 90 1.1 mbalmer gpiolock_attach(device_t parent, device_t self, void *aux) 91 1.1 mbalmer { 92 1.1 mbalmer struct gpiolock_softc *sc = device_private(self); 93 1.1 mbalmer struct gpio_attach_args *ga = aux; 94 1.1 mbalmer int pin, caps; 95 1.1 mbalmer 96 1.1 mbalmer sc->sc_npins = gpio_npins(ga->ga_mask); 97 1.1 mbalmer 98 1.1 mbalmer /* Map pins */ 99 1.1 mbalmer sc->sc_gpio = ga->ga_gpio; 100 1.1 mbalmer sc->sc_map.pm_map = sc->_map; 101 1.1 mbalmer if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask, 102 1.1 mbalmer &sc->sc_map)) { 103 1.1 mbalmer aprint_error(": can't map pins\n"); 104 1.1 mbalmer return; 105 1.1 mbalmer } 106 1.1 mbalmer 107 1.1 mbalmer /* Configure data pins */ 108 1.1 mbalmer for (pin = 0; pin < sc->sc_npins; pin++) { 109 1.1 mbalmer caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, pin); 110 1.1 mbalmer if (!(caps & GPIO_PIN_INPUT)) { 111 1.1 mbalmer aprint_error(": data pin is unable to read input\n"); 112 1.1 mbalmer goto fail; 113 1.1 mbalmer } 114 1.1 mbalmer aprint_normal(" [%d]", sc->sc_map.pm_map[pin]); 115 1.1 mbalmer sc->sc_data = GPIO_PIN_INPUT; 116 1.1 mbalmer gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, pin, sc->sc_data); 117 1.1 mbalmer } 118 1.1 mbalmer 119 1.1 mbalmer #ifdef KEYLOCK 120 1.1 mbalmer /* Register keylock */ 121 1.1 mbalmer if (keylock_register(self, sc->sc_npins, gpiolock_position)) { 122 1.1 mbalmer aprint_error(": can't register keylock\n"); 123 1.1 mbalmer goto fail; 124 1.1 mbalmer } 125 1.1 mbalmer #endif 126 1.4 maya if (!pmf_device_register(self, NULL, NULL)) 127 1.4 maya aprint_error_dev(self, "couldn't establish power handler\n"); 128 1.1 mbalmer 129 1.1 mbalmer aprint_normal("\n"); 130 1.1 mbalmer return; 131 1.1 mbalmer 132 1.1 mbalmer fail: 133 1.1 mbalmer gpio_pin_unmap(sc->sc_gpio, &sc->sc_map); 134 1.1 mbalmer } 135 1.1 mbalmer 136 1.1 mbalmer int 137 1.1 mbalmer gpiolock_detach(device_t self, int flags) 138 1.1 mbalmer { 139 1.1 mbalmer struct gpiolock_softc *sc = device_private(self); 140 1.1 mbalmer 141 1.1 mbalmer pmf_device_deregister(self); 142 1.1 mbalmer #ifdef KEYLOCK 143 1.1 mbalmer keylock_unregister(self, gpiolock_position); 144 1.1 mbalmer #endif 145 1.1 mbalmer gpio_pin_unmap(sc->sc_gpio, &sc->sc_map); 146 1.1 mbalmer 147 1.1 mbalmer return 0; 148 1.1 mbalmer } 149 1.1 mbalmer 150 1.1 mbalmer int 151 1.1 mbalmer gpiolock_activate(device_t self, enum devact act) 152 1.1 mbalmer { 153 1.1 mbalmer struct gpiolock_softc *sc = device_private(self); 154 1.1 mbalmer 155 1.1 mbalmer switch (act) { 156 1.1 mbalmer case DVACT_DEACTIVATE: 157 1.1 mbalmer sc->sc_dying = 1; 158 1.3 dyoung return 0; 159 1.3 dyoung default: 160 1.3 dyoung return EOPNOTSUPP; 161 1.1 mbalmer } 162 1.1 mbalmer 163 1.1 mbalmer } 164 1.1 mbalmer 165 1.1 mbalmer int 166 1.1 mbalmer gpiolock_position(void *arg) 167 1.1 mbalmer { 168 1.1 mbalmer struct gpiolock_softc *sc = device_private((device_t)arg); 169 1.1 mbalmer int pos, pin; 170 1.1 mbalmer 171 1.1 mbalmer for (pos = pin = 0; pin < sc->sc_npins; pin++) { 172 1.1 mbalmer if (gpio_pin_read(sc->sc_gpio, &sc->sc_map, pin) == 173 1.1 mbalmer GPIO_PIN_HIGH) 174 1.1 mbalmer pos = pin + 1; 175 1.1 mbalmer } 176 1.1 mbalmer return pos; 177 1.1 mbalmer } 178 1.1 mbalmer 179