1 /* $NetBSD: gpioow.c,v 1.3 2006/11/16 01:32:50 christos 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.3 2006/11/16 01:32:50 christos 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 32 #include <dev/gpio/gpiovar.h> 33 34 #include <dev/onewire/onewirevar.h> 35 36 #define GPIOOW_NPINS 1 37 #define GPIOOW_PIN_DATA 0 38 39 struct gpioow_softc { 40 struct device sc_dev; 41 42 void * sc_gpio; 43 struct gpio_pinmap sc_map; 44 int __map[GPIOOW_NPINS]; 45 46 struct onewire_bus sc_ow_bus; 47 struct device * sc_ow_dev; 48 49 int sc_data; 50 int sc_dying; 51 }; 52 53 int gpioow_match(struct device *, struct cfdata *, void *); 54 void gpioow_attach(struct device *, struct device *, void *); 55 int gpioow_detach(struct device *, int); 56 int gpioow_activate(struct device *, enum devact); 57 58 int gpioow_ow_reset(void *); 59 int gpioow_ow_bit(void *, int); 60 61 void gpioow_bb_rx(void *); 62 void gpioow_bb_tx(void *); 63 int gpioow_bb_get(void *); 64 void gpioow_bb_set(void *, int); 65 66 CFATTACH_DECL(gpioow, sizeof(struct gpioow_softc), 67 gpioow_match, gpioow_attach, gpioow_detach, gpioow_activate); 68 69 extern struct cfdriver gpioow_cd; 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 int 79 gpioow_match(struct device *parent, struct cfdata *cf, 80 void *aux) 81 { 82 return 1; 83 } 84 85 void 86 gpioow_attach(struct device *parent, struct device *self, void *aux) 87 { 88 struct gpioow_softc *sc = device_private(self); 89 struct gpio_attach_args *ga = aux; 90 struct onewirebus_attach_args oba; 91 int caps; 92 93 /* Check that we have enough pins */ 94 if (gpio_npins(ga->ga_mask) != GPIOOW_NPINS) { 95 printf(": invalid pin mask\n"); 96 return; 97 } 98 99 /* Map pins */ 100 sc->sc_gpio = ga->ga_gpio; 101 sc->sc_map.pm_map = sc->__map; 102 if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask, 103 &sc->sc_map)) { 104 printf(": can't map pins\n"); 105 return; 106 } 107 108 /* Configure data pin */ 109 caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA); 110 if (!(caps & GPIO_PIN_OUTPUT)) { 111 printf(": data pin is unable to drive output\n"); 112 goto fail; 113 } 114 if (!(caps & GPIO_PIN_INPUT)) { 115 printf(": data pin is unable to read input\n"); 116 goto fail; 117 } 118 printf(": DATA[%d]", sc->sc_map.pm_map[GPIOOW_PIN_DATA]); 119 sc->sc_data = GPIO_PIN_OUTPUT; 120 if (caps & GPIO_PIN_OPENDRAIN) { 121 printf(" open-drain"); 122 sc->sc_data |= GPIO_PIN_OPENDRAIN; 123 } else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) { 124 printf(" push-pull tri-state"); 125 sc->sc_data |= GPIO_PIN_PUSHPULL; 126 } 127 if (caps & GPIO_PIN_PULLUP) { 128 printf(" pull-up"); 129 sc->sc_data |= GPIO_PIN_PULLUP; 130 } 131 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, sc->sc_data); 132 133 printf("\n"); 134 135 /* Attach 1-Wire bus */ 136 sc->sc_ow_bus.bus_cookie = sc; 137 sc->sc_ow_bus.bus_reset = gpioow_ow_reset; 138 sc->sc_ow_bus.bus_bit = gpioow_ow_bit; 139 140 bzero(&oba, sizeof(oba)); 141 oba.oba_bus = &sc->sc_ow_bus; 142 sc->sc_ow_dev = config_found(self, &oba, onewirebus_print); 143 144 return; 145 146 fail: 147 gpio_pin_unmap(sc->sc_gpio, &sc->sc_map); 148 } 149 150 int 151 gpioow_detach(struct device *self, int flags) 152 { 153 struct gpioow_softc *sc = device_private(self); 154 int rv = 0; 155 156 if (sc->sc_ow_dev != NULL) 157 rv = config_detach(sc->sc_ow_dev, flags); 158 159 return (rv); 160 } 161 162 int 163 gpioow_activate(struct device *self, enum devact act) 164 { 165 struct gpioow_softc *sc = device_private(self); 166 int rv = 0; 167 168 switch (act) { 169 case DVACT_ACTIVATE: 170 return (EOPNOTSUPP); 171 case DVACT_DEACTIVATE: 172 sc->sc_dying = 1; 173 if (sc->sc_ow_dev != NULL) 174 rv = config_deactivate(sc->sc_ow_dev); 175 break; 176 } 177 178 return (rv); 179 } 180 181 int 182 gpioow_ow_reset(void *arg) 183 { 184 return (onewire_bb_reset(&gpioow_bbops, arg)); 185 } 186 187 int 188 gpioow_ow_bit(void *arg, int value) 189 { 190 return (onewire_bb_bit(&gpioow_bbops, arg, value)); 191 } 192 193 void 194 gpioow_bb_rx(void *arg) 195 { 196 struct gpioow_softc *sc = arg; 197 int data = sc->sc_data; 198 199 data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE); 200 data |= GPIO_PIN_INPUT; 201 if (data & GPIO_PIN_PUSHPULL) 202 data |= GPIO_PIN_TRISTATE; 203 if (sc->sc_data != data) { 204 sc->sc_data = data; 205 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, 206 sc->sc_data); 207 } 208 } 209 210 void 211 gpioow_bb_tx(void *arg) 212 { 213 struct gpioow_softc *sc = arg; 214 int data = sc->sc_data; 215 216 data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE); 217 data |= GPIO_PIN_OUTPUT; 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 int 226 gpioow_bb_get(void *arg) 227 { 228 struct gpioow_softc *sc = arg; 229 230 return (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA) == 231 GPIO_PIN_HIGH ? 1 : 0); 232 } 233 234 void 235 gpioow_bb_set(void *arg, int value) 236 { 237 struct gpioow_softc *sc = arg; 238 239 gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, 240 value ? GPIO_PIN_HIGH : GPIO_PIN_LOW); 241 } 242