1 /*- 2 * Copyright (c) 1998, 2001 Nicolas Souchu 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 /* 31 * Generic I2C bit-banging code 32 * 33 * Example: 34 * 35 * iicbus 36 * / \ 37 * iicbb pcf 38 * | \ 39 * bti2c lpbb 40 * 41 * From Linux I2C generic interface 42 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de> 43 * 44 */ 45 46 #include <sys/param.h> 47 #include <sys/kernel.h> 48 #include <sys/systm.h> 49 #include <sys/module.h> 50 #include <sys/bus.h> 51 #include <sys/uio.h> 52 53 54 #include <dev/iicbus/iiconf.h> 55 #include <dev/iicbus/iicbus.h> 56 57 #include <dev/smbus/smbconf.h> 58 59 #include "iicbus_if.h" 60 #include "iicbb_if.h" 61 62 struct iicbb_softc { 63 device_t iicbus; 64 int udelay; /* signal toggle delay in usec */ 65 }; 66 67 static int iicbb_attach(device_t); 68 static void iicbb_child_detached(device_t, device_t); 69 static int iicbb_detach(device_t); 70 static int iicbb_print_child(device_t, device_t); 71 static int iicbb_probe(device_t); 72 73 static int iicbb_callback(device_t, int, caddr_t); 74 static int iicbb_start(device_t, u_char, int); 75 static int iicbb_stop(device_t); 76 static int iicbb_write(device_t, const char *, int, int *, int); 77 static int iicbb_read(device_t, char *, int, int *, int, int); 78 static int iicbb_reset(device_t, u_char, u_char, u_char *); 79 80 static device_method_t iicbb_methods[] = { 81 /* device interface */ 82 DEVMETHOD(device_probe, iicbb_probe), 83 DEVMETHOD(device_attach, iicbb_attach), 84 DEVMETHOD(device_detach, iicbb_detach), 85 86 /* bus interface */ 87 DEVMETHOD(bus_child_detached, iicbb_child_detached), 88 DEVMETHOD(bus_print_child, iicbb_print_child), 89 90 /* iicbus interface */ 91 DEVMETHOD(iicbus_callback, iicbb_callback), 92 DEVMETHOD(iicbus_start, iicbb_start), 93 DEVMETHOD(iicbus_repeated_start, iicbb_start), 94 DEVMETHOD(iicbus_stop, iicbb_stop), 95 DEVMETHOD(iicbus_write, iicbb_write), 96 DEVMETHOD(iicbus_read, iicbb_read), 97 DEVMETHOD(iicbus_reset, iicbb_reset), 98 DEVMETHOD(iicbus_transfer, iicbus_transfer_gen), 99 100 { 0, 0 } 101 }; 102 103 driver_t iicbb_driver = { 104 "iicbb", 105 iicbb_methods, 106 sizeof(struct iicbb_softc), 107 }; 108 109 devclass_t iicbb_devclass; 110 111 static int 112 iicbb_probe(device_t dev) 113 { 114 device_set_desc(dev, "I2C bit-banging driver"); 115 116 return (0); 117 } 118 119 static int 120 iicbb_attach(device_t dev) 121 { 122 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 123 124 sc->iicbus = device_add_child(dev, "iicbus", -1); 125 if (!sc->iicbus) 126 return (ENXIO); 127 sc->udelay = 10; /* 10 uS default */ 128 bus_generic_attach(dev); 129 130 return (0); 131 } 132 133 static int 134 iicbb_detach(device_t dev) 135 { 136 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 137 device_t child; 138 139 /* 140 * We need to save child because the detach indirectly causes 141 * sc->iicbus to be zeroed. Since we added the device 142 * unconditionally in iicbb_attach, we need to make sure we 143 * delete it here. See iicbb_child_detached. We need that 144 * callback in case newbus detached our children w/o detaching 145 * us (say iicbus is a module and unloaded w/o iicbb being 146 * unloaded). 147 */ 148 child = sc->iicbus; 149 bus_generic_detach(dev); 150 if (child) 151 device_delete_child(dev, child); 152 153 return (0); 154 } 155 156 static void 157 iicbb_child_detached( device_t dev, device_t child ) 158 { 159 struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 160 161 if (child == sc->iicbus) 162 sc->iicbus = NULL; 163 } 164 165 static int 166 iicbb_print_child(device_t bus, device_t dev) 167 { 168 int error; 169 int retval = 0; 170 u_char oldaddr; 171 172 retval += bus_print_child_header(bus, dev); 173 /* retrieve the interface I2C address */ 174 error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr); 175 if (error == IIC_ENOADDR) { 176 retval += printf(" on %s master-only\n", 177 device_get_nameunit(bus)); 178 } else { 179 /* restore the address */ 180 IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL); 181 182 retval += printf(" on %s addr 0x%x\n", 183 device_get_nameunit(bus), oldaddr & 0xff); 184 } 185 186 return (retval); 187 } 188 189 #define I2C_SETSDA(sc,dev,val) do { \ 190 IICBB_SETSDA(device_get_parent(dev), val); \ 191 DELAY(sc->udelay); \ 192 } while (0) 193 194 #define I2C_SETSCL(dev,val) do { \ 195 iicbb_setscl(dev, val, 100); \ 196 } while (0) 197 198 #define I2C_SET(sc,dev,ctrl,data) do { \ 199 I2C_SETSCL(dev, ctrl); \ 200 I2C_SETSDA(sc, dev, data); \ 201 } while (0) 202 203 #define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev))) 204 205 #define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev))) 206 207 static int i2c_debug = 0; 208 #define I2C_DEBUG(x) do { \ 209 if (i2c_debug) (x); \ 210 } while (0) 211 212 #define I2C_LOG(format,args...) do { \ 213 printf(format, args); \ 214 } while (0) 215 216 static void 217 iicbb_setscl(device_t dev, int val, int timeout) 218 { 219 struct iicbb_softc *sc = device_get_softc(dev); 220 int k = 0; 221 222 IICBB_SETSCL(device_get_parent(dev), val); 223 DELAY(sc->udelay); 224 225 while (val && !I2C_GETSCL(dev) && k++ < timeout) { 226 IICBB_SETSCL(device_get_parent(dev), val); 227 DELAY(sc->udelay); 228 } 229 230 return; 231 } 232 233 static void 234 iicbb_one(device_t dev, int timeout) 235 { 236 struct iicbb_softc *sc = device_get_softc(dev); 237 238 I2C_SET(sc,dev,0,1); 239 I2C_SET(sc,dev,1,1); 240 I2C_SET(sc,dev,0,1); 241 return; 242 } 243 244 static void 245 iicbb_zero(device_t dev, int timeout) 246 { 247 struct iicbb_softc *sc = device_get_softc(dev); 248 249 I2C_SET(sc,dev,0,0); 250 I2C_SET(sc,dev,1,0); 251 I2C_SET(sc,dev,0,0); 252 return; 253 } 254 255 /* 256 * Waiting for ACKNOWLEDGE. 257 * 258 * When a chip is being addressed or has received data it will issue an 259 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line 260 * (set it to high level) and then release the CLOCK line. 261 * Now it must wait for the SLAVE to pull the DATA line low. 262 * Actually on the bus this looks like a START condition so nothing happens 263 * because of the fact that the IC's that have not been addressed are doing 264 * nothing. 265 * 266 * When the SLAVE has pulled this line low the MASTER will take the CLOCK 267 * line low and then the SLAVE will release the SDA (data) line. 268 */ 269 static int 270 iicbb_ack(device_t dev, int timeout) 271 { 272 struct iicbb_softc *sc = device_get_softc(dev); 273 int noack; 274 int k = 0; 275 276 I2C_SET(sc,dev,0,1); 277 I2C_SET(sc,dev,1,1); 278 do { 279 noack = I2C_GETSDA(dev); 280 if (!noack) 281 break; 282 DELAY(1); 283 k++; 284 } while (k < timeout); 285 286 I2C_SET(sc,dev,0,1); 287 I2C_DEBUG(printf("%c ",noack?'-':'+')); 288 289 return (noack); 290 } 291 292 static void 293 iicbb_sendbyte(device_t dev, u_char data, int timeout) 294 { 295 int i; 296 297 for (i=7; i>=0; i--) { 298 if (data&(1<<i)) { 299 iicbb_one(dev, timeout); 300 } else { 301 iicbb_zero(dev, timeout); 302 } 303 } 304 I2C_DEBUG(printf("w%02x",(int)data)); 305 return; 306 } 307 308 static u_char 309 iicbb_readbyte(device_t dev, int last, int timeout) 310 { 311 struct iicbb_softc *sc = device_get_softc(dev); 312 int i; 313 unsigned char data=0; 314 315 I2C_SET(sc,dev,0,1); 316 for (i=7; i>=0; i--) 317 { 318 I2C_SET(sc,dev,1,1); 319 if (I2C_GETSDA(dev)) 320 data |= (1<<i); 321 I2C_SET(sc,dev,0,1); 322 } 323 if (last) { 324 iicbb_one(dev, timeout); 325 } else { 326 iicbb_zero(dev, timeout); 327 } 328 I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+')); 329 return data; 330 } 331 332 static int 333 iicbb_callback(device_t dev, int index, caddr_t data) 334 { 335 return (IICBB_CALLBACK(device_get_parent(dev), index, data)); 336 } 337 338 static int 339 iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) 340 { 341 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr)); 342 } 343 344 static int 345 iicbb_start(device_t dev, u_char slave, int timeout) 346 { 347 struct iicbb_softc *sc = device_get_softc(dev); 348 int error; 349 350 I2C_DEBUG(printf("<")); 351 352 I2C_SET(sc,dev,1,1); 353 I2C_SET(sc,dev,1,0); 354 I2C_SET(sc,dev,0,0); 355 356 /* send address */ 357 iicbb_sendbyte(dev, slave, timeout); 358 359 /* check for ack */ 360 if (iicbb_ack(dev, timeout)) { 361 error = IIC_ENOACK; 362 goto error; 363 } 364 365 return(0); 366 367 error: 368 iicbb_stop(dev); 369 return (error); 370 } 371 372 static int 373 iicbb_stop(device_t dev) 374 { 375 struct iicbb_softc *sc = device_get_softc(dev); 376 377 I2C_SET(sc,dev,0,0); 378 I2C_SET(sc,dev,1,0); 379 I2C_SET(sc,dev,1,1); 380 I2C_DEBUG(printf(">")); 381 I2C_DEBUG(printf("\n")); 382 return (0); 383 } 384 385 static int 386 iicbb_write(device_t dev, const char *buf, int len, int *sent, int timeout) 387 { 388 int bytes, error = 0; 389 390 bytes = 0; 391 while (len) { 392 /* send byte */ 393 iicbb_sendbyte(dev,(u_char)*buf++, timeout); 394 395 /* check for ack */ 396 if (iicbb_ack(dev, timeout)) { 397 error = IIC_ENOACK; 398 goto error; 399 } 400 bytes ++; 401 len --; 402 } 403 404 error: 405 *sent = bytes; 406 return (error); 407 } 408 409 static int 410 iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay) 411 { 412 int bytes; 413 414 bytes = 0; 415 while (len) { 416 /* XXX should insert delay here */ 417 *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay); 418 419 bytes ++; 420 len --; 421 } 422 423 *read = bytes; 424 return (0); 425 } 426 427 DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0); 428 429 MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 430 MODULE_VERSION(iicbb, IICBB_MODVER); 431