1 /* $NetBSD: gpioow.c,v 1.20 2023/05/10 00:09:31 riastradh Exp $ */ 2 /* $OpenBSD: gpioow.c,v 1.1 2006/03/04 16:27:03 grange Exp $ */ 3 4 /* 5 * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/cdefs.h> 21 __KERNEL_RCSID(0, "$NetBSD: gpioow.c,v 1.20 2023/05/10 00:09:31 riastradh Exp $"); 22 23 /* 24 * 1-Wire bus bit-banging through GPIO pin. 25 */ 26 27 #include <sys/param.h> 28 #include <sys/systm.h> 29 #include <sys/device.h> 30 #include <sys/gpio.h> 31 #include <sys/module.h> 32 33 #include <dev/gpio/gpiovar.h> 34 35 #include <dev/onewire/onewirevar.h> 36 37 #include "ioconf.h" 38 39 #define GPIOOW_NPINS 1 40 #define GPIOOW_PIN_DATA 0 41 42 struct gpioow_softc { 43 void * sc_gpio; 44 struct gpio_pinmap sc_map; 45 int _map[GPIOOW_NPINS]; 46 47 struct onewire_bus sc_ow_bus; 48 device_t sc_ow_dev; 49 50 int sc_data; 51 int sc_dying; 52 }; 53 54 static int gpioow_match(device_t, cfdata_t, void *); 55 static void gpioow_attach(device_t, device_t, void *); 56 static int gpioow_detach(device_t, int); 57 static int gpioow_activate(device_t, enum devact); 58 59 static int gpioow_ow_reset(void *); 60 static int gpioow_ow_read_bit(void *); 61 static void gpioow_ow_write_bit(void *, int); 62 63 static void gpioow_bb_rx(void *); 64 static void gpioow_bb_tx(void *); 65 static int gpioow_bb_get(void *); 66 static void gpioow_bb_set(void *, int); 67 68 CFATTACH_DECL_NEW(gpioow, sizeof(struct gpioow_softc), 69 gpioow_match, gpioow_attach, gpioow_detach, gpioow_activate); 70 71 static const struct onewire_bbops gpioow_bbops = { 72 gpioow_bb_rx, 73 gpioow_bb_tx, 74 gpioow_bb_get, 75 gpioow_bb_set 76 }; 77 78 static int 79 gpioow_match(device_t parent, cfdata_t cf, void *aux) 80 { 81 struct gpio_attach_args *ga = aux; 82 83 if (strcmp(ga->ga_dvname, cf->cf_name)) 84 return 0; 85 86 if (ga->ga_offset == -1) 87 return 0; 88 89 /* Check that we have enough pins */ 90 if (gpio_npins(ga->ga_mask) != GPIOOW_NPINS) { 91 aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name, 92 ga->ga_mask); 93 return 0; 94 } 95 return 1; 96 } 97 98 static void 99 gpioow_attach(device_t parent, device_t self, void *aux) 100 { 101 struct gpioow_softc *sc = device_private(self); 102 struct gpio_attach_args *ga = aux; 103 struct onewirebus_attach_args oba; 104 int caps; 105 106 /* Map pins */ 107 sc->sc_gpio = ga->ga_gpio; 108 sc->sc_map.pm_map = sc->_map; 109 if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask, 110 &sc->sc_map)) { 111 aprint_error(": can't map pins\n"); 112 goto finish; 113 } 114 115 /* Configure data pin */ 116 caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA); 117 if (!(caps & GPIO_PIN_OUTPUT)) { 118 aprint_error(": data pin is unable to drive output\n"); 119 gpio_pin_unmap(sc->sc_gpio, &sc->sc_map); 120 goto finish; 121 } 122 if (!(caps & GPIO_PIN_INPUT)) { 123 aprint_error(": data pin is unable to read input\n"); 124 gpio_pin_unmap(sc->sc_gpio, &sc->sc_map); 125 goto finish; 126 } 127 aprint_normal(": DATA[%d]", sc->sc_map.pm_map[GPIOOW_PIN_DATA]); 128 sc->sc_data = GPIO_PIN_OUTPUT; 129 if (caps & GPIO_PIN_OPENDRAIN) { 130 aprint_normal(" open-drain"); 131 sc->sc_data |= GPIO_PIN_OPENDRAIN; 132 } else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) { 133 aprint_normal(" push-pull tri-state"); 134 sc->sc_data |= GPIO_PIN_PUSHPULL; 135 } 136 if (caps & GPIO_PIN_PULLUP) { 137 aprint_normal(" pull-up"); 138 sc->sc_data |= GPIO_PIN_PULLUP; 139 } 140 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, sc->sc_data); 141 142 aprint_normal("\n"); 143 144 /* Attach 1-Wire bus */ 145 sc->sc_ow_bus.bus_cookie = sc; 146 sc->sc_ow_bus.bus_reset = gpioow_ow_reset; 147 sc->sc_ow_bus.bus_read_bit = gpioow_ow_read_bit; 148 sc->sc_ow_bus.bus_write_bit = gpioow_ow_write_bit; 149 150 memset(&oba, 0, sizeof(oba)); 151 oba.oba_bus = &sc->sc_ow_bus; 152 sc->sc_ow_dev = config_found(self, &oba, onewirebus_print, CFARGS_NONE); 153 154 if (!pmf_device_register(self, NULL, NULL)) 155 aprint_error("%s: could not establish power handler\n", 156 device_xname(self)); 157 finish: 158 return; 159 } 160 161 static int 162 gpioow_detach(device_t self, int flags) 163 { 164 struct gpioow_softc *sc = device_private(self); 165 int error; 166 167 error = config_detach_children(self, flags); 168 if (error) 169 return error; 170 171 gpio_pin_unmap(sc->sc_gpio, &sc->sc_map); 172 pmf_device_deregister(self); 173 return 0; 174 } 175 176 static int 177 gpioow_activate(device_t self, enum devact act) 178 { 179 struct gpioow_softc *sc = device_private(self); 180 181 switch (act) { 182 case DVACT_DEACTIVATE: 183 sc->sc_dying = 1; 184 return 0; 185 default: 186 return EOPNOTSUPP; 187 } 188 } 189 190 static int 191 gpioow_ow_reset(void *arg) 192 { 193 return (onewire_bb_reset(&gpioow_bbops, arg)); 194 } 195 196 static int 197 gpioow_ow_read_bit(void *arg) 198 { 199 return (onewire_bb_read_bit(&gpioow_bbops, arg)); 200 } 201 202 static void 203 gpioow_ow_write_bit(void *arg, int value) 204 { 205 onewire_bb_write_bit(&gpioow_bbops, arg, value); 206 } 207 208 static void 209 gpioow_bb_rx(void *arg) 210 { 211 struct gpioow_softc *sc = arg; 212 int data = sc->sc_data; 213 214 data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE); 215 data |= GPIO_PIN_INPUT; 216 if (data & GPIO_PIN_PUSHPULL) 217 data |= GPIO_PIN_TRISTATE; 218 if (sc->sc_data != data) { 219 sc->sc_data = data; 220 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, 221 sc->sc_data); 222 } 223 } 224 225 static void 226 gpioow_bb_tx(void *arg) 227 { 228 struct gpioow_softc *sc = arg; 229 int data = sc->sc_data; 230 231 data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE); 232 data |= GPIO_PIN_OUTPUT; 233 if (sc->sc_data != data) { 234 sc->sc_data = data; 235 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, 236 sc->sc_data); 237 } 238 } 239 240 static int 241 gpioow_bb_get(void *arg) 242 { 243 struct gpioow_softc *sc = arg; 244 245 return (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA) == 246 GPIO_PIN_HIGH ? 1 : 0); 247 } 248 249 static void 250 gpioow_bb_set(void *arg, int value) 251 { 252 struct gpioow_softc *sc = arg; 253 254 gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, 255 value ? GPIO_PIN_HIGH : GPIO_PIN_LOW); 256 } 257 258 MODULE(MODULE_CLASS_DRIVER, gpioow, "gpio,onewire"); 259 260 #ifdef _MODULE 261 #include "ioconf.c" 262 #endif 263 264 static int 265 gpioow_modcmd(modcmd_t cmd, void *opaque) 266 { 267 int error; 268 269 error = 0; 270 switch (cmd) { 271 case MODULE_CMD_INIT: 272 #ifdef _MODULE 273 error = config_init_component(cfdriver_ioconf_gpioow, 274 cfattach_ioconf_gpioow, cfdata_ioconf_gpioow); 275 if (error) 276 aprint_error("%s: unable to init component\n", 277 gpioow_cd.cd_name); 278 #endif 279 break; 280 case MODULE_CMD_FINI: 281 #ifdef _MODULE 282 config_fini_component(cfdriver_ioconf_gpioow, 283 cfattach_ioconf_gpioow, cfdata_ioconf_gpioow); 284 #endif 285 break; 286 default: 287 error = ENOTTY; 288 } 289 return error; 290 } 291