iicbb.c revision 188461
1279377Simp/*- 2279377Simp * Copyright (c) 1998, 2001 Nicolas Souchu 3279377Simp * All rights reserved. 4279377Simp * 5279377Simp * Redistribution and use in source and binary forms, with or without 6279377Simp * modification, are permitted provided that the following conditions 7279377Simp * are met: 8279377Simp * 1. Redistributions of source code must retain the above copyright 9279377Simp * notice, this list of conditions and the following disclaimer. 10279377Simp * 2. Redistributions in binary form must reproduce the above copyright 11279377Simp * notice, this list of conditions and the following disclaimer in the 12279377Simp * documentation and/or other materials provided with the distribution. 13279377Simp * 14279377Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15279377Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16279377Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17279377Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18279377Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19279377Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20279377Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21279377Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22279377Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23279377Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24279377Simp * SUCH DAMAGE. 25279377Simp */ 26279377Simp 27279377Simp#include <sys/cdefs.h> 28279377Simp__FBSDID("$FreeBSD: head/sys/dev/iicbus/iicbb.c 188461 2009-02-10 22:50:23Z imp $"); 29279377Simp 30279377Simp/* 31279377Simp * Generic I2C bit-banging code 32279377Simp * 33279377Simp * Example: 34279377Simp * 35279377Simp * iicbus 36279377Simp * / \ 37279377Simp * iicbb pcf 38279377Simp * | \ 39279377Simp * bti2c lpbb 40279377Simp * 41279377Simp * From Linux I2C generic interface 42279377Simp * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de> 43279377Simp * 44279377Simp */ 45279377Simp 46279377Simp#include <sys/param.h> 47279377Simp#include <sys/kernel.h> 48279377Simp#include <sys/systm.h> 49279377Simp#include <sys/module.h> 50279377Simp#include <sys/bus.h> 51279377Simp#include <sys/uio.h> 52279377Simp 53279377Simp 54279377Simp#include <dev/iicbus/iiconf.h> 55279377Simp#include <dev/iicbus/iicbus.h> 56279377Simp 57279377Simp#include <dev/smbus/smbconf.h> 58279377Simp 59279377Simp#include "iicbus_if.h" 60279377Simp#include "iicbb_if.h" 61279377Simp 62279377Simpstruct iicbb_softc { 63279377Simp device_t iicbus; 64279377Simp}; 65279377Simp 66279377Simpstatic int iicbb_attach(device_t); 67279377Simpstatic void iicbb_child_detached(device_t, device_t); 68279377Simpstatic int iicbb_detach(device_t); 69279377Simpstatic int iicbb_print_child(device_t, device_t); 70279377Simpstatic int iicbb_probe(device_t); 71279377Simp 72279377Simpstatic int iicbb_callback(device_t, int, caddr_t); 73279377Simpstatic int iicbb_start(device_t, u_char, int); 74279377Simpstatic int iicbb_stop(device_t); 75279377Simpstatic int iicbb_write(device_t, const char *, int, int *, int); 76279377Simpstatic int iicbb_read(device_t, char *, int, int *, int, int); 77279377Simpstatic int iicbb_reset(device_t, u_char, u_char, u_char *); 78279377Simp 79279377Simpstatic device_method_t iicbb_methods[] = { 80279377Simp /* device interface */ 81279377Simp DEVMETHOD(device_probe, iicbb_probe), 82279377Simp DEVMETHOD(device_attach, iicbb_attach), 83279377Simp DEVMETHOD(device_detach, iicbb_detach), 84279377Simp 85279377Simp /* bus interface */ 86279377Simp DEVMETHOD(bus_child_detached, iicbb_child_detached), 87279377Simp DEVMETHOD(bus_print_child, iicbb_print_child), 88279377Simp 89279377Simp /* iicbus interface */ 90279377Simp DEVMETHOD(iicbus_callback, iicbb_callback), 91279377Simp DEVMETHOD(iicbus_start, iicbb_start), 92279377Simp DEVMETHOD(iicbus_repeated_start, iicbb_start), 93279377Simp DEVMETHOD(iicbus_stop, iicbb_stop), 94279377Simp DEVMETHOD(iicbus_write, iicbb_write), 95279377Simp DEVMETHOD(iicbus_read, iicbb_read), 96279377Simp DEVMETHOD(iicbus_reset, iicbb_reset), 97279377Simp DEVMETHOD(iicbus_transfer, iicbus_transfer_gen), 98279377Simp 99279377Simp { 0, 0 } 100279377Simp}; 101279377Simp 102279377Simpdriver_t iicbb_driver = { 103279377Simp "iicbb", 104279377Simp iicbb_methods, 105279377Simp sizeof(struct iicbb_softc), 106279377Simp}; 107279377Simp 108279377Simpdevclass_t iicbb_devclass; 109279377Simp 110279377Simpstatic int 111279377Simpiicbb_probe(device_t dev) 112279377Simp{ 113279377Simp device_set_desc(dev, "I2C bit-banging driver"); 114279377Simp 115279377Simp return (0); 116279377Simp} 117279377Simp 118279377Simpstatic int 119279377Simpiicbb_attach(device_t dev) 120279377Simp{ 121279377Simp struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 122279377Simp 123279377Simp sc->iicbus = device_add_child(dev, "iicbus", -1); 124279377Simp if (!sc->iicbus) 125279377Simp return (ENXIO); 126279377Simp bus_generic_attach(dev); 127279377Simp 128279377Simp return (0); 129279377Simp} 130279377Simp 131279377Simpstatic int 132279377Simpiicbb_detach(device_t dev) 133279377Simp{ 134279377Simp struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 135279377Simp device_t child; 136279377Simp 137279377Simp /* 138279377Simp * We need to save child because the detach indirectly causes 139279377Simp * sc->iicbus to be zeroed. Since we added the device 140279377Simp * unconditionally in iicbb_attach, we need to make sure we 141279377Simp * delete it here. See iicbb_child_detached. We need that 142279377Simp * callback in case newbus detached our children w/o detaching 143279377Simp * us (say iicbus is a module and unloaded w/o iicbb being 144279377Simp * unloaded). 145279377Simp */ 146279377Simp child = sc->iicbus; 147279377Simp bus_generic_detach(dev); 148279377Simp if (child) 149279377Simp device_delete_child(dev, child); 150279377Simp 151279377Simp return (0); 152279377Simp} 153279377Simp 154279377Simpstatic void 155279377Simpiicbb_child_detached( device_t dev, device_t child ) 156279377Simp{ 157279377Simp struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev); 158279377Simp 159279377Simp if (child == sc->iicbus) 160279377Simp sc->iicbus = NULL; 161279377Simp} 162279377Simp 163279377Simpstatic int 164279377Simpiicbb_print_child(device_t bus, device_t dev) 165279377Simp{ 166279377Simp int error; 167279377Simp int retval = 0; 168279377Simp u_char oldaddr; 169279377Simp 170279377Simp retval += bus_print_child_header(bus, dev); 171279377Simp /* retrieve the interface I2C address */ 172279377Simp error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr); 173279377Simp if (error == IIC_ENOADDR) { 174279377Simp retval += printf(" on %s master-only\n", 175279377Simp device_get_nameunit(bus)); 176279377Simp } else { 177279377Simp /* restore the address */ 178279377Simp IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL); 179279377Simp 180279377Simp retval += printf(" on %s addr 0x%x\n", 181279377Simp device_get_nameunit(bus), oldaddr & 0xff); 182279377Simp } 183279377Simp 184279377Simp return (retval); 185279377Simp} 186279377Simp 187279377Simp#define IIC_DELAY 10 188279377Simp 189279377Simp#define I2C_SETSDA(dev,val) do { \ 190279377Simp IICBB_SETSDA(device_get_parent(dev), val); \ 191279377Simp DELAY(IIC_DELAY); \ 192279377Simp } while (0) 193279377Simp 194279377Simp#define I2C_SETSCL(dev,val) do { \ 195279377Simp iicbb_setscl(dev, val, 100); \ 196279377Simp } while (0) 197279377Simp 198279377Simp#define I2C_SET(dev,ctrl,data) do { \ 199 I2C_SETSCL(dev, ctrl); \ 200 I2C_SETSDA(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 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, const 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