iicbb.c revision 181303
1285242Sachim/*- 2285242Sachim * Copyright (c) 1998, 2001 Nicolas Souchu 3285242Sachim * All rights reserved. 4285242Sachim * 5285242Sachim * Redistribution and use in source and binary forms, with or without 6285242Sachim * modification, are permitted provided that the following conditions 7285242Sachim * are met: 8285242Sachim * 1. Redistributions of source code must retain the above copyright 9285242Sachim * notice, this list of conditions and the following disclaimer. 10285242Sachim * 2. Redistributions in binary form must reproduce the above copyright 11285242Sachim * notice, this list of conditions and the following disclaimer in the 12285242Sachim * documentation and/or other materials provided with the distribution. 13285242Sachim * 14285242Sachim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15285242Sachim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16285242Sachim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17285242Sachim * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18285242Sachim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19285242Sachim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20285242Sachim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21285242Sachim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22285242Sachim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23285242Sachim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24285242Sachim * SUCH DAMAGE. 25285242Sachim */ 26285242Sachim 27285242Sachim#include <sys/cdefs.h> 28285242Sachim__FBSDID("$FreeBSD: head/sys/dev/iicbus/iicbb.c 181303 2008-08-04 20:46:15Z jhb $"); 29285242Sachim 30285242Sachim/* 31285242Sachim * Generic I2C bit-banging code 32285242Sachim * 33285242Sachim * Example: 34285242Sachim * 35285242Sachim * iicbus 36285242Sachim * / \ 37285242Sachim * iicbb pcf 38285242Sachim * | \ 39285242Sachim * bti2c lpbb 40285242Sachim * 41285242Sachim * From Linux I2C generic interface 42285242Sachim * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de> 43285242Sachim * 44285242Sachim */ 45285242Sachim 46285242Sachim#include <sys/param.h> 47285242Sachim#include <sys/kernel.h> 48285242Sachim#include <sys/systm.h> 49285242Sachim#include <sys/module.h> 50285242Sachim#include <sys/bus.h> 51285242Sachim#include <sys/uio.h> 52285242Sachim 53285242Sachim 54285242Sachim#include <dev/iicbus/iiconf.h> 55285242Sachim#include <dev/iicbus/iicbus.h> 56285242Sachim 57285242Sachim#include <dev/smbus/smbconf.h> 58285242Sachim 59285242Sachim#include "iicbus_if.h" 60285242Sachim#include "iicbb_if.h" 61285242Sachim 62285242Sachimstruct iicbb_softc { 63285242Sachim device_t iicbus; 64285242Sachim}; 65285242Sachim 66285242Sachimstatic int iicbb_attach(device_t); 67285242Sachimstatic void iicbb_child_detached(device_t, device_t); 68285242Sachimstatic int iicbb_detach(device_t); 69285242Sachimstatic int iicbb_print_child(device_t, device_t); 70285242Sachimstatic int iicbb_probe(device_t); 71285242Sachim 72285242Sachimstatic int iicbb_callback(device_t, int, caddr_t); 73285242Sachimstatic int iicbb_start(device_t, u_char, int); 74285242Sachimstatic int iicbb_stop(device_t); 75285242Sachimstatic int iicbb_write(device_t, char *, int, int *, int); 76285242Sachimstatic int iicbb_read(device_t, char *, int, int *, int, int); 77285242Sachimstatic int iicbb_reset(device_t, u_char, u_char, u_char *); 78285242Sachim 79285242Sachimstatic device_method_t iicbb_methods[] = { 80285242Sachim /* device interface */ 81285242Sachim DEVMETHOD(device_probe, iicbb_probe), 82285242Sachim DEVMETHOD(device_attach, iicbb_attach), 83285242Sachim DEVMETHOD(device_detach, iicbb_detach), 84285242Sachim 85285242Sachim /* bus interface */ 86285242Sachim DEVMETHOD(bus_child_detached, iicbb_child_detached), 87285242Sachim DEVMETHOD(bus_print_child, iicbb_print_child), 88285242Sachim 89285242Sachim /* iicbus interface */ 90285242Sachim DEVMETHOD(iicbus_callback, iicbb_callback), 91285242Sachim DEVMETHOD(iicbus_start, iicbb_start), 92285242Sachim DEVMETHOD(iicbus_repeated_start, iicbb_start), 93285242Sachim DEVMETHOD(iicbus_stop, iicbb_stop), 94285242Sachim DEVMETHOD(iicbus_write, iicbb_write), 95285242Sachim DEVMETHOD(iicbus_read, iicbb_read), 96285242Sachim DEVMETHOD(iicbus_reset, iicbb_reset), 97285242Sachim DEVMETHOD(iicbus_transfer, iicbus_transfer_gen), 98285242Sachim 99285242Sachim { 0, 0 } 100285242Sachim}; 101285242Sachim 102285242Sachimdriver_t iicbb_driver = { 103285242Sachim "iicbb", 104285242Sachim iicbb_methods, 105285242Sachim sizeof(struct iicbb_softc), 106285242Sachim}; 107285242Sachim 108285242Sachimdevclass_t iicbb_devclass; 109285242Sachim 110285242Sachimstatic int 111285242Sachimiicbb_probe(device_t dev) 112285242Sachim{ 113285242Sachim device_set_desc(dev, "I2C bit-banging driver"); 114285242Sachim 115285242Sachim return (0); 116285242Sachim} 117285242Sachim 118285242Sachimstatic int 119285242Sachimiicbb_attach(device_t dev) 120285242Sachim{ 121285242Sachim struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 122285242Sachim 123285242Sachim sc->iicbus = device_add_child(dev, "iicbus", -1); 124285242Sachim if (!sc->iicbus) 125285242Sachim return (ENXIO); 126285242Sachim bus_generic_attach(dev); 127285242Sachim 128285242Sachim return (0); 129285242Sachim} 130285242Sachim 131285242Sachimstatic int 132285242Sachimiicbb_detach(device_t dev) 133285242Sachim{ 134285242Sachim struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 135285242Sachim device_t child; 136285242Sachim 137285242Sachim /* 138285242Sachim * We need to save child because the detach indirectly causes 139285242Sachim * sc->iicbus to be zeroed. Since we added the device 140285242Sachim * unconditionally in iicbb_attach, we need to make sure we 141285242Sachim * delete it here. See iicbb_child_detached. We need that 142285242Sachim * callback in case newbus detached our children w/o detaching 143285242Sachim * us (say iicbus is a module and unloaded w/o iicbb being 144285242Sachim * unloaded). 145285242Sachim */ 146285242Sachim child = sc->iicbus; 147285242Sachim bus_generic_detach(dev); 148285242Sachim if (child) 149285242Sachim device_delete_child(dev, child); 150285242Sachim 151285242Sachim return (0); 152285242Sachim} 153285242Sachim 154285242Sachimstatic void 155285242Sachimiicbb_child_detached( device_t dev, device_t child ) 156285242Sachim{ 157285242Sachim struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 158285242Sachim 159285242Sachim if (child == sc->iicbus) 160285242Sachim sc->iicbus = NULL; 161285242Sachim} 162285242Sachim 163285242Sachimstatic int 164285242Sachimiicbb_print_child(device_t bus, device_t dev) 165285242Sachim{ 166285242Sachim int error; 167285242Sachim int retval = 0; 168285242Sachim u_char oldaddr; 169285242Sachim 170285242Sachim retval += bus_print_child_header(bus, dev); 171285242Sachim /* retrieve the interface I2C address */ 172285242Sachim error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr); 173285242Sachim if (error == IIC_ENOADDR) { 174285242Sachim retval += printf(" on %s master-only\n", 175285242Sachim device_get_nameunit(bus)); 176285242Sachim } else { 177285242Sachim /* restore the address */ 178285242Sachim IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL); 179285242Sachim 180285242Sachim retval += printf(" on %s addr 0x%x\n", 181285242Sachim device_get_nameunit(bus), oldaddr & 0xff); 182285242Sachim } 183285242Sachim 184285242Sachim return (retval); 185285242Sachim} 186285242Sachim 187285242Sachim#define IIC_DELAY 10 188285242Sachim 189285242Sachim#define I2C_SETSDA(dev,val) do { \ 190285242Sachim IICBB_SETSDA(device_get_parent(dev), val); \ 191285242Sachim DELAY(IIC_DELAY); \ 192285242Sachim } while (0) 193285242Sachim 194285242Sachim#define I2C_SETSCL(dev,val) do { \ 195285242Sachim iicbb_setscl(dev, val, 100); \ 196285242Sachim } while (0) 197285242Sachim 198285242Sachim#define I2C_SET(dev,ctrl,data) do { \ 199285242Sachim I2C_SETSCL(dev, ctrl); \ 200285242Sachim I2C_SETSDA(dev, data); \ 201285242Sachim } while (0) 202285242Sachim 203285242Sachim#define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev))) 204285242Sachim 205#define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev))) 206 207static 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 216static void 217iicbb_setscl(device_t dev, int val, int timeout) 218{ 219 int k = 0; 220 221 IICBB_SETSCL(device_get_parent(dev), val); 222 DELAY(IIC_DELAY); 223 224 while (val && !I2C_GETSCL(dev) && k++ < timeout) { 225 IICBB_SETSCL(device_get_parent(dev), val); 226 DELAY(IIC_DELAY); 227 } 228 229 return; 230} 231 232static void 233iicbb_one(device_t dev, int timeout) 234{ 235 I2C_SET(dev,0,1); 236 I2C_SET(dev,1,1); 237 I2C_SET(dev,0,1); 238 return; 239} 240 241static void 242iicbb_zero(device_t dev, int timeout) 243{ 244 I2C_SET(dev,0,0); 245 I2C_SET(dev,1,0); 246 I2C_SET(dev,0,0); 247 return; 248} 249 250/* 251 * Waiting for ACKNOWLEDGE. 252 * 253 * When a chip is being addressed or has received data it will issue an 254 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line 255 * (set it to high level) and then release the CLOCK line. 256 * Now it must wait for the SLAVE to pull the DATA line low. 257 * Actually on the bus this looks like a START condition so nothing happens 258 * because of the fact that the IC's that have not been addressed are doing 259 * nothing. 260 * 261 * When the SLAVE has pulled this line low the MASTER will take the CLOCK 262 * line low and then the SLAVE will release the SDA (data) line. 263 */ 264static int 265iicbb_ack(device_t dev, int timeout) 266{ 267 int noack; 268 int k = 0; 269 270 I2C_SET(dev,0,1); 271 I2C_SET(dev,1,1); 272 do { 273 noack = I2C_GETSDA(dev); 274 if (!noack) 275 break; 276 DELAY(10); 277 k += 10; 278 } while (k < timeout); 279 280 I2C_SET(dev,0,1); 281 I2C_DEBUG(printf("%c ",noack?'-':'+')); 282 283 return (noack); 284} 285 286static void 287iicbb_sendbyte(device_t dev, u_char data, int timeout) 288{ 289 int i; 290 291 for (i=7; i>=0; i--) { 292 if (data&(1<<i)) { 293 iicbb_one(dev, timeout); 294 } else { 295 iicbb_zero(dev, timeout); 296 } 297 } 298 I2C_DEBUG(printf("w%02x",(int)data)); 299 return; 300} 301 302static u_char 303iicbb_readbyte(device_t dev, int last, int timeout) 304{ 305 int i; 306 unsigned char data=0; 307 308 I2C_SET(dev,0,1); 309 for (i=7; i>=0; i--) 310 { 311 I2C_SET(dev,1,1); 312 if (I2C_GETSDA(dev)) 313 data |= (1<<i); 314 I2C_SET(dev,0,1); 315 } 316 if (last) { 317 iicbb_one(dev, timeout); 318 } else { 319 iicbb_zero(dev, timeout); 320 } 321 I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+')); 322 return data; 323} 324 325static int 326iicbb_callback(device_t dev, int index, caddr_t data) 327{ 328 return (IICBB_CALLBACK(device_get_parent(dev), index, data)); 329} 330 331static int 332iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) 333{ 334 return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr)); 335} 336 337static int 338iicbb_start(device_t dev, u_char slave, int timeout) 339{ 340 int error; 341 342 I2C_DEBUG(printf("<")); 343 344 I2C_SET(dev,1,1); 345 I2C_SET(dev,1,0); 346 I2C_SET(dev,0,0); 347 348 /* send address */ 349 iicbb_sendbyte(dev, slave, timeout); 350 351 /* check for ack */ 352 if (iicbb_ack(dev, timeout)) { 353 error = IIC_ENOACK; 354 goto error; 355 } 356 357 return(0); 358 359error: 360 iicbb_stop(dev); 361 return (error); 362} 363 364static int 365iicbb_stop(device_t dev) 366{ 367 I2C_SET(dev,0,0); 368 I2C_SET(dev,1,0); 369 I2C_SET(dev,1,1); 370 I2C_DEBUG(printf(">")); 371 return (0); 372} 373 374static int 375iicbb_write(device_t dev, char * buf, int len, int *sent, int timeout) 376{ 377 int bytes, error = 0; 378 379 bytes = 0; 380 while (len) { 381 /* send byte */ 382 iicbb_sendbyte(dev,(u_char)*buf++, timeout); 383 384 /* check for ack */ 385 if (iicbb_ack(dev, timeout)) { 386 error = IIC_ENOACK; 387 goto error; 388 } 389 bytes ++; 390 len --; 391 } 392 393error: 394 *sent = bytes; 395 return (error); 396} 397 398static int 399iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay) 400{ 401 int bytes; 402 403 bytes = 0; 404 while (len) { 405 /* XXX should insert delay here */ 406 *buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay); 407 408 bytes ++; 409 len --; 410 } 411 412 *read = bytes; 413 return (0); 414} 415 416DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0); 417 418MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 419MODULE_VERSION(iicbb, IICBB_MODVER); 420