1 /* $OpenBSD: gpioiic.c,v 1.3 2006/01/15 22:18:48 grange Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * I2C bus bit-banging through GPIO pins. 21 */ 22 23 #include <sys/param.h> 24 #include <sys/systm.h> 25 #include <sys/device.h> 26 #include <sys/gpio.h> 27 28 #include <dev/gpio/gpiovar.h> 29 30 #include <dev/i2c/i2cvar.h> 31 #include <dev/i2c/i2c_bitbang.h> 32 33 #define GPIOIIC_PIN_SDA 0 34 #define GPIOIIC_PIN_SCL 1 35 #define GPIOIIC_NPINS 2 36 37 #define GPIOIIC_SDA 0x01 38 #define GPIOIIC_SCL 0x02 39 40 struct gpioiic_softc { 41 struct device sc_dev; 42 43 void * sc_gpio; 44 struct gpio_pinmap sc_map; 45 int __map[GPIOIIC_NPINS]; 46 47 struct i2c_controller sc_i2c_tag; 48 struct lock sc_i2c_lock; 49 50 int sc_sda; 51 int sc_scl; 52 }; 53 54 int gpioiic_match(struct device *, void *, void *); 55 void gpioiic_attach(struct device *, struct device *, void *); 56 57 int gpioiic_i2c_acquire_bus(void *, int); 58 void gpioiic_i2c_release_bus(void *, int); 59 int gpioiic_i2c_send_start(void *, int); 60 int gpioiic_i2c_send_stop(void *, int); 61 int gpioiic_i2c_initiate_xfer(void *, i2c_addr_t, int); 62 int gpioiic_i2c_read_byte(void *, u_int8_t *, int); 63 int gpioiic_i2c_write_byte(void *, u_int8_t, int); 64 65 void gpioiic_bb_set_bits(void *, u_int32_t); 66 void gpioiic_bb_set_dir(void *, u_int32_t); 67 u_int32_t gpioiic_bb_read_bits(void *); 68 69 struct cfattach gpioiic_ca = { 70 sizeof(struct gpioiic_softc), 71 gpioiic_match, 72 gpioiic_attach 73 }; 74 75 struct cfdriver gpioiic_cd = { 76 NULL, "gpioiic", DV_DULL 77 }; 78 79 static const struct i2c_bitbang_ops gpioiic_bbops = { 80 gpioiic_bb_set_bits, 81 gpioiic_bb_set_dir, 82 gpioiic_bb_read_bits, 83 { GPIOIIC_SDA, GPIOIIC_SCL, GPIOIIC_SDA, 0 } 84 }; 85 86 int 87 gpioiic_match(struct device *parent, void *match, void *aux) 88 { 89 struct cfdata *cf = match; 90 91 return (strcmp(cf->cf_driver->cd_name, "gpioiic") == 0); 92 } 93 94 void 95 gpioiic_attach(struct device *parent, struct device *self, void *aux) 96 { 97 struct gpioiic_softc *sc = (struct gpioiic_softc *)self; 98 struct gpio_attach_args *ga = aux; 99 struct i2cbus_attach_args iba; 100 int caps; 101 102 /* Check that we have enough pins */ 103 if (gpio_npins(ga->ga_mask) != GPIOIIC_NPINS) { 104 printf(": invalid pin mask\n"); 105 return; 106 } 107 108 /* Map pins */ 109 sc->sc_gpio = ga->ga_gpio; 110 sc->sc_map.pm_map = sc->__map; 111 if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask, 112 &sc->sc_map)) { 113 printf(": can't map pins\n"); 114 return; 115 } 116 117 /* Configure SDA pin */ 118 caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIOIIC_PIN_SDA); 119 if (!(caps & GPIO_PIN_OUTPUT)) { 120 printf(": SDA pin is unable to drive output\n"); 121 goto fail; 122 } 123 printf(": SDA[%d]", sc->sc_map.pm_map[GPIOIIC_PIN_SDA]); 124 sc->sc_sda = GPIO_PIN_OUTPUT; 125 if (caps & GPIO_PIN_OPENDRAIN) { 126 printf(" open-drain"); 127 sc->sc_sda |= GPIO_PIN_OPENDRAIN; 128 if (caps & GPIO_PIN_PULLUP) { 129 printf(" pull-up"); 130 sc->sc_sda |= GPIO_PIN_PULLUP; 131 } 132 } 133 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOIIC_PIN_SDA, sc->sc_sda); 134 135 /* Configure SCL pin */ 136 caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIOIIC_PIN_SCL); 137 if (!(caps & GPIO_PIN_OUTPUT)) { 138 printf(": SCL pin is unable to drive output\n"); 139 goto fail; 140 } 141 printf(", SCL[%d]", sc->sc_map.pm_map[GPIOIIC_PIN_SCL]); 142 sc->sc_scl = GPIO_PIN_OUTPUT; 143 if (caps & GPIO_PIN_OPENDRAIN) { 144 printf(" open-drain"); 145 sc->sc_scl |= GPIO_PIN_OPENDRAIN; 146 if (caps & GPIO_PIN_PULLUP) { 147 printf(" pull-up"); 148 sc->sc_scl |= GPIO_PIN_PULLUP; 149 } 150 } else if (caps & GPIO_PIN_PUSHPULL) { 151 printf(" push-pull"); 152 sc->sc_scl |= GPIO_PIN_PUSHPULL; 153 } 154 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOIIC_PIN_SCL, sc->sc_scl); 155 156 printf("\n"); 157 158 /* Attach I2C bus */ 159 lockinit(&sc->sc_i2c_lock, PRIBIO | PCATCH, "iiclk", 0, 0); 160 sc->sc_i2c_tag.ic_cookie = sc; 161 sc->sc_i2c_tag.ic_acquire_bus = gpioiic_i2c_acquire_bus; 162 sc->sc_i2c_tag.ic_release_bus = gpioiic_i2c_release_bus; 163 sc->sc_i2c_tag.ic_send_start = gpioiic_i2c_send_start; 164 sc->sc_i2c_tag.ic_send_stop = gpioiic_i2c_send_stop; 165 sc->sc_i2c_tag.ic_initiate_xfer = gpioiic_i2c_initiate_xfer; 166 sc->sc_i2c_tag.ic_read_byte = gpioiic_i2c_read_byte; 167 sc->sc_i2c_tag.ic_write_byte = gpioiic_i2c_write_byte; 168 169 bzero(&iba, sizeof(iba)); 170 iba.iba_name = "iic"; 171 iba.iba_tag = &sc->sc_i2c_tag; 172 config_found(self, &iba, iicbus_print); 173 174 return; 175 176 fail: 177 gpio_pin_unmap(sc->sc_gpio, &sc->sc_map); 178 } 179 180 int 181 gpioiic_i2c_acquire_bus(void *cookie, int flags) 182 { 183 struct gpioiic_softc *sc = cookie; 184 185 if (cold || (flags & I2C_F_POLL)) 186 return (0); 187 188 return (lockmgr(&sc->sc_i2c_lock, LK_EXCLUSIVE, NULL)); 189 } 190 191 void 192 gpioiic_i2c_release_bus(void *cookie, int flags) 193 { 194 struct gpioiic_softc *sc = cookie; 195 196 if (cold || (flags & I2C_F_POLL)) 197 return; 198 199 lockmgr(&sc->sc_i2c_lock, LK_RELEASE, NULL); 200 } 201 202 int 203 gpioiic_i2c_send_start(void *cookie, int flags) 204 { 205 return (i2c_bitbang_send_start(cookie, flags, &gpioiic_bbops)); 206 } 207 208 int 209 gpioiic_i2c_send_stop(void *cookie, int flags) 210 { 211 return (i2c_bitbang_send_stop(cookie, flags, &gpioiic_bbops)); 212 } 213 214 int 215 gpioiic_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags) 216 { 217 return (i2c_bitbang_initiate_xfer(cookie, addr, flags, &gpioiic_bbops)); 218 } 219 220 int 221 gpioiic_i2c_read_byte(void *cookie, u_int8_t *bytep, int flags) 222 { 223 return (i2c_bitbang_read_byte(cookie, bytep, flags, &gpioiic_bbops)); 224 } 225 226 int 227 gpioiic_i2c_write_byte(void *cookie, u_int8_t byte, int flags) 228 { 229 return (i2c_bitbang_write_byte(cookie, byte, flags, &gpioiic_bbops)); 230 } 231 232 void 233 gpioiic_bb_set_bits(void *cookie, u_int32_t bits) 234 { 235 struct gpioiic_softc *sc = cookie; 236 237 gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOIIC_PIN_SDA, 238 bits & GPIOIIC_SDA ? GPIO_PIN_HIGH : GPIO_PIN_LOW); 239 gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOIIC_PIN_SCL, 240 bits & GPIOIIC_SCL ? GPIO_PIN_HIGH : GPIO_PIN_LOW); 241 } 242 243 void 244 gpioiic_bb_set_dir(void *cookie, u_int32_t bits) 245 { 246 struct gpioiic_softc *sc = cookie; 247 248 if (!(sc->sc_sda & GPIO_PIN_OPENDRAIN)) { 249 sc->sc_sda &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT); 250 sc->sc_sda |= (bits & GPIOIIC_SDA ? GPIO_PIN_OUTPUT : 251 GPIO_PIN_INPUT); 252 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOIIC_PIN_SDA, 253 sc->sc_sda); 254 } 255 } 256 257 u_int32_t 258 gpioiic_bb_read_bits(void *cookie) 259 { 260 struct gpioiic_softc *sc = cookie; 261 262 return (gpio_pin_read(sc->sc_gpio, &sc->sc_map, 263 GPIOIIC_PIN_SDA) == GPIO_PIN_HIGH ? GPIOIIC_SDA : 0); 264 } 265