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