| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* ------------------------------------------------------------------------- */ | 
|  | 2 | /* i2c-algo-bit.c i2c driver algorithms for bit-shift adapters		     */ | 
|  | 3 | /* ------------------------------------------------------------------------- */ | 
|  | 4 | /*   Copyright (C) 1995-2000 Simon G. Vogl | 
|  | 5 |  | 
|  | 6 | This program is free software; you can redistribute it and/or modify | 
|  | 7 | it under the terms of the GNU General Public License as published by | 
|  | 8 | the Free Software Foundation; either version 2 of the License, or | 
|  | 9 | (at your option) any later version. | 
|  | 10 |  | 
|  | 11 | This program is distributed in the hope that it will be useful, | 
|  | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 14 | GNU General Public License for more details. | 
|  | 15 |  | 
|  | 16 | You should have received a copy of the GNU General Public License | 
|  | 17 | along with this program; if not, write to the Free Software | 
|  | 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.		     */ | 
|  | 19 | /* ------------------------------------------------------------------------- */ | 
|  | 20 |  | 
|  | 21 | /* With some changes from Frodo Looijaard <frodol@dds.nl>, Kyösti Mälkki | 
|  | 22 | <kmalkki@cc.hut.fi> and Jean Delvare <khali@linux-fr.org> */ | 
|  | 23 |  | 
|  | 24 | #include <linux/kernel.h> | 
|  | 25 | #include <linux/module.h> | 
|  | 26 | #include <linux/delay.h> | 
|  | 27 | #include <linux/slab.h> | 
|  | 28 | #include <linux/init.h> | 
|  | 29 | #include <linux/errno.h> | 
|  | 30 | #include <linux/sched.h> | 
|  | 31 | #include <linux/i2c.h> | 
|  | 32 | #include <linux/i2c-algo-bit.h> | 
|  | 33 |  | 
|  | 34 |  | 
|  | 35 | /* ----- global defines ----------------------------------------------- */ | 
|  | 36 | #define DEB(x) if (i2c_debug>=1) x; | 
|  | 37 | #define DEB2(x) if (i2c_debug>=2) x; | 
|  | 38 | #define DEBSTAT(x) if (i2c_debug>=3) x; /* print several statistical values*/ | 
|  | 39 | #define DEBPROTO(x) if (i2c_debug>=9) { x; } | 
|  | 40 | /* debug the protocol by showing transferred bits */ | 
|  | 41 |  | 
|  | 42 |  | 
|  | 43 | /* ----- global variables ---------------------------------------------	*/ | 
|  | 44 |  | 
|  | 45 | /* module parameters: | 
|  | 46 | */ | 
|  | 47 | static int i2c_debug; | 
|  | 48 | static int bit_test;	/* see if the line-setting functions work	*/ | 
|  | 49 |  | 
|  | 50 | /* --- setting states on the bus with the right timing: ---------------	*/ | 
|  | 51 |  | 
|  | 52 | #define setsda(adap,val) adap->setsda(adap->data, val) | 
|  | 53 | #define setscl(adap,val) adap->setscl(adap->data, val) | 
|  | 54 | #define getsda(adap) adap->getsda(adap->data) | 
|  | 55 | #define getscl(adap) adap->getscl(adap->data) | 
|  | 56 |  | 
|  | 57 | static inline void sdalo(struct i2c_algo_bit_data *adap) | 
|  | 58 | { | 
|  | 59 | setsda(adap,0); | 
|  | 60 | udelay(adap->udelay); | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | static inline void sdahi(struct i2c_algo_bit_data *adap) | 
|  | 64 | { | 
|  | 65 | setsda(adap,1); | 
|  | 66 | udelay(adap->udelay); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | static inline void scllo(struct i2c_algo_bit_data *adap) | 
|  | 70 | { | 
|  | 71 | setscl(adap,0); | 
|  | 72 | udelay(adap->udelay); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | /* | 
|  | 76 | * Raise scl line, and do checking for delays. This is necessary for slower | 
|  | 77 | * devices. | 
|  | 78 | */ | 
|  | 79 | static inline int sclhi(struct i2c_algo_bit_data *adap) | 
|  | 80 | { | 
|  | 81 | unsigned long start; | 
|  | 82 |  | 
|  | 83 | setscl(adap,1); | 
|  | 84 |  | 
|  | 85 | /* Not all adapters have scl sense line... */ | 
|  | 86 | if (adap->getscl == NULL ) { | 
|  | 87 | udelay(adap->udelay); | 
|  | 88 | return 0; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | start=jiffies; | 
|  | 92 | while (! getscl(adap) ) { | 
|  | 93 | /* the hw knows how to read the clock line, | 
|  | 94 | * so we wait until it actually gets high. | 
|  | 95 | * This is safer as some chips may hold it low | 
|  | 96 | * while they are processing data internally. | 
|  | 97 | */ | 
|  | 98 | if (time_after_eq(jiffies, start+adap->timeout)) { | 
|  | 99 | return -ETIMEDOUT; | 
|  | 100 | } | 
|  | 101 | cond_resched(); | 
|  | 102 | } | 
|  | 103 | DEBSTAT(printk(KERN_DEBUG "needed %ld jiffies\n", jiffies-start)); | 
|  | 104 | udelay(adap->udelay); | 
|  | 105 | return 0; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 |  | 
|  | 109 | /* --- other auxiliary functions --------------------------------------	*/ | 
|  | 110 | static void i2c_start(struct i2c_algo_bit_data *adap) | 
|  | 111 | { | 
|  | 112 | /* assert: scl, sda are high */ | 
|  | 113 | DEBPROTO(printk("S ")); | 
|  | 114 | sdalo(adap); | 
|  | 115 | scllo(adap); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | static void i2c_repstart(struct i2c_algo_bit_data *adap) | 
|  | 119 | { | 
|  | 120 | /* scl, sda may not be high */ | 
|  | 121 | DEBPROTO(printk(" Sr ")); | 
|  | 122 | setsda(adap,1); | 
|  | 123 | sclhi(adap); | 
|  | 124 | udelay(adap->udelay); | 
|  | 125 |  | 
|  | 126 | sdalo(adap); | 
|  | 127 | scllo(adap); | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 |  | 
|  | 131 | static void i2c_stop(struct i2c_algo_bit_data *adap) | 
|  | 132 | { | 
|  | 133 | DEBPROTO(printk("P\n")); | 
|  | 134 | /* assert: scl is low */ | 
|  | 135 | sdalo(adap); | 
|  | 136 | sclhi(adap); | 
|  | 137 | sdahi(adap); | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 |  | 
|  | 141 |  | 
|  | 142 | /* send a byte without start cond., look for arbitration, | 
|  | 143 | check ackn. from slave */ | 
|  | 144 | /* returns: | 
|  | 145 | * 1 if the device acknowledged | 
|  | 146 | * 0 if the device did not ack | 
|  | 147 | * -ETIMEDOUT if an error occurred (while raising the scl line) | 
|  | 148 | */ | 
|  | 149 | static int i2c_outb(struct i2c_adapter *i2c_adap, char c) | 
|  | 150 | { | 
|  | 151 | int i; | 
|  | 152 | int sb; | 
|  | 153 | int ack; | 
|  | 154 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; | 
|  | 155 |  | 
|  | 156 | /* assert: scl is low */ | 
|  | 157 | for ( i=7 ; i>=0 ; i-- ) { | 
|  | 158 | sb = c & ( 1 << i ); | 
|  | 159 | setsda(adap,sb); | 
|  | 160 | udelay(adap->udelay); | 
|  | 161 | DEBPROTO(printk(KERN_DEBUG "%d",sb!=0)); | 
|  | 162 | if (sclhi(adap)<0) { /* timed out */ | 
|  | 163 | sdahi(adap); /* we don't want to block the net */ | 
|  | 164 | DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at bit #%d\n", c&0xff, i)); | 
|  | 165 | return -ETIMEDOUT; | 
|  | 166 | }; | 
|  | 167 | /* do arbitration here: | 
|  | 168 | * if ( sb && ! getsda(adap) ) -> ouch! Get out of here. | 
|  | 169 | */ | 
|  | 170 | setscl(adap, 0 ); | 
|  | 171 | udelay(adap->udelay); | 
|  | 172 | } | 
|  | 173 | sdahi(adap); | 
|  | 174 | if (sclhi(adap)<0){ /* timeout */ | 
|  | 175 | DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at ack\n", c&0xff)); | 
|  | 176 | return -ETIMEDOUT; | 
|  | 177 | }; | 
|  | 178 | /* read ack: SDA should be pulled down by slave */ | 
|  | 179 | ack=getsda(adap);	/* ack: sda is pulled low ->success.	 */ | 
|  | 180 | DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x , getsda() = %d\n", c & 0xff, ack)); | 
|  | 181 |  | 
|  | 182 | DEBPROTO( printk(KERN_DEBUG "[%2.2x]",c&0xff) ); | 
|  | 183 | DEBPROTO(if (0==ack){ printk(KERN_DEBUG " A ");} else printk(KERN_DEBUG " NA ") ); | 
|  | 184 | scllo(adap); | 
|  | 185 | return 0==ack;		/* return 1 if device acked	 */ | 
|  | 186 | /* assert: scl is low (sda undef) */ | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 |  | 
|  | 190 | static int i2c_inb(struct i2c_adapter *i2c_adap) | 
|  | 191 | { | 
|  | 192 | /* read byte via i2c port, without start/stop sequence	*/ | 
|  | 193 | /* acknowledge is sent in i2c_read.			*/ | 
|  | 194 | int i; | 
|  | 195 | unsigned char indata=0; | 
|  | 196 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; | 
|  | 197 |  | 
|  | 198 | /* assert: scl is low */ | 
|  | 199 | sdahi(adap); | 
|  | 200 | for (i=0;i<8;i++) { | 
|  | 201 | if (sclhi(adap)<0) { /* timeout */ | 
|  | 202 | DEB2(printk(KERN_DEBUG " i2c_inb: timeout at bit #%d\n", 7-i)); | 
|  | 203 | return -ETIMEDOUT; | 
|  | 204 | }; | 
|  | 205 | indata *= 2; | 
|  | 206 | if ( getsda(adap) ) | 
|  | 207 | indata |= 0x01; | 
|  | 208 | scllo(adap); | 
|  | 209 | } | 
|  | 210 | /* assert: scl is low */ | 
|  | 211 | DEB2(printk(KERN_DEBUG "i2c_inb: 0x%02x\n", indata & 0xff)); | 
|  | 212 |  | 
|  | 213 | DEBPROTO(printk(KERN_DEBUG " 0x%02x", indata & 0xff)); | 
|  | 214 | return (int) (indata & 0xff); | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | /* | 
|  | 218 | * Sanity check for the adapter hardware - check the reaction of | 
|  | 219 | * the bus lines only if it seems to be idle. | 
|  | 220 | */ | 
|  | 221 | static int test_bus(struct i2c_algo_bit_data *adap, char* name) { | 
|  | 222 | int scl,sda; | 
|  | 223 |  | 
|  | 224 | if (adap->getscl==NULL) | 
|  | 225 | printk(KERN_INFO "i2c-algo-bit.o: Testing SDA only, " | 
|  | 226 | "SCL is not readable.\n"); | 
|  | 227 |  | 
|  | 228 | sda=getsda(adap); | 
|  | 229 | scl=(adap->getscl==NULL?1:getscl(adap)); | 
|  | 230 | printk(KERN_DEBUG "i2c-algo-bit.o: (0) scl=%d, sda=%d\n",scl,sda); | 
|  | 231 | if (!scl || !sda ) { | 
|  | 232 | printk(KERN_WARNING "i2c-algo-bit.o: %s seems to be busy.\n", name); | 
|  | 233 | goto bailout; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | sdalo(adap); | 
|  | 237 | sda=getsda(adap); | 
|  | 238 | scl=(adap->getscl==NULL?1:getscl(adap)); | 
|  | 239 | printk(KERN_DEBUG "i2c-algo-bit.o: (1) scl=%d, sda=%d\n",scl,sda); | 
|  | 240 | if ( 0 != sda ) { | 
|  | 241 | printk(KERN_WARNING "i2c-algo-bit.o: SDA stuck high!\n"); | 
|  | 242 | goto bailout; | 
|  | 243 | } | 
|  | 244 | if ( 0 == scl ) { | 
|  | 245 | printk(KERN_WARNING "i2c-algo-bit.o: SCL unexpected low " | 
|  | 246 | "while pulling SDA low!\n"); | 
|  | 247 | goto bailout; | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | sdahi(adap); | 
|  | 251 | sda=getsda(adap); | 
|  | 252 | scl=(adap->getscl==NULL?1:getscl(adap)); | 
|  | 253 | printk(KERN_DEBUG "i2c-algo-bit.o: (2) scl=%d, sda=%d\n",scl,sda); | 
|  | 254 | if ( 0 == sda ) { | 
|  | 255 | printk(KERN_WARNING "i2c-algo-bit.o: SDA stuck low!\n"); | 
|  | 256 | goto bailout; | 
|  | 257 | } | 
|  | 258 | if ( 0 == scl ) { | 
|  | 259 | printk(KERN_WARNING "i2c-algo-bit.o: SCL unexpected low " | 
|  | 260 | "while pulling SDA high!\n"); | 
|  | 261 | goto bailout; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | scllo(adap); | 
|  | 265 | sda=getsda(adap); | 
|  | 266 | scl=(adap->getscl==NULL?0:getscl(adap)); | 
|  | 267 | printk(KERN_DEBUG "i2c-algo-bit.o: (3) scl=%d, sda=%d\n",scl,sda); | 
|  | 268 | if ( 0 != scl ) { | 
|  | 269 | printk(KERN_WARNING "i2c-algo-bit.o: SCL stuck high!\n"); | 
|  | 270 | goto bailout; | 
|  | 271 | } | 
|  | 272 | if ( 0 == sda ) { | 
|  | 273 | printk(KERN_WARNING "i2c-algo-bit.o: SDA unexpected low " | 
|  | 274 | "while pulling SCL low!\n"); | 
|  | 275 | goto bailout; | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | sclhi(adap); | 
|  | 279 | sda=getsda(adap); | 
|  | 280 | scl=(adap->getscl==NULL?1:getscl(adap)); | 
|  | 281 | printk(KERN_DEBUG "i2c-algo-bit.o: (4) scl=%d, sda=%d\n",scl,sda); | 
|  | 282 | if ( 0 == scl ) { | 
|  | 283 | printk(KERN_WARNING "i2c-algo-bit.o: SCL stuck low!\n"); | 
|  | 284 | goto bailout; | 
|  | 285 | } | 
|  | 286 | if ( 0 == sda ) { | 
|  | 287 | printk(KERN_WARNING "i2c-algo-bit.o: SDA unexpected low " | 
|  | 288 | "while pulling SCL high!\n"); | 
|  | 289 | goto bailout; | 
|  | 290 | } | 
|  | 291 | printk(KERN_INFO "i2c-algo-bit.o: %s passed test.\n",name); | 
|  | 292 | return 0; | 
|  | 293 | bailout: | 
|  | 294 | sdahi(adap); | 
|  | 295 | sclhi(adap); | 
|  | 296 | return -ENODEV; | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | /* ----- Utility functions | 
|  | 300 | */ | 
|  | 301 |  | 
|  | 302 | /* try_address tries to contact a chip for a number of | 
|  | 303 | * times before it gives up. | 
|  | 304 | * return values: | 
|  | 305 | * 1 chip answered | 
|  | 306 | * 0 chip did not answer | 
|  | 307 | * -x transmission error | 
|  | 308 | */ | 
|  | 309 | static inline int try_address(struct i2c_adapter *i2c_adap, | 
|  | 310 | unsigned char addr, int retries) | 
|  | 311 | { | 
|  | 312 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; | 
|  | 313 | int i,ret = -1; | 
|  | 314 | for (i=0;i<=retries;i++) { | 
|  | 315 | ret = i2c_outb(i2c_adap,addr); | 
|  | 316 | if (ret==1) | 
|  | 317 | break;	/* success! */ | 
|  | 318 | i2c_stop(adap); | 
|  | 319 | udelay(5/*adap->udelay*/); | 
|  | 320 | if (i==retries)  /* no success */ | 
|  | 321 | break; | 
|  | 322 | i2c_start(adap); | 
|  | 323 | udelay(adap->udelay); | 
|  | 324 | } | 
|  | 325 | DEB2(if (i) | 
|  | 326 | printk(KERN_DEBUG "i2c-algo-bit.o: Used %d tries to %s client at 0x%02x : %s\n", | 
|  | 327 | i+1, addr & 1 ? "read" : "write", addr>>1, | 
|  | 328 | ret==1 ? "success" : ret==0 ? "no ack" : "failed, timeout?" ) | 
|  | 329 | ); | 
|  | 330 | return ret; | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) | 
|  | 334 | { | 
|  | 335 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; | 
|  | 336 | char c; | 
|  | 337 | const char *temp = msg->buf; | 
|  | 338 | int count = msg->len; | 
|  | 339 | unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK; | 
|  | 340 | int retval; | 
|  | 341 | int wrcount=0; | 
|  | 342 |  | 
|  | 343 | while (count > 0) { | 
|  | 344 | c = *temp; | 
|  | 345 | DEB2(dev_dbg(&i2c_adap->dev, "sendbytes: writing %2.2X\n", c&0xff)); | 
|  | 346 | retval = i2c_outb(i2c_adap,c); | 
|  | 347 | if ((retval>0) || (nak_ok && (retval==0)))  { /* ok or ignored NAK */ | 
|  | 348 | count--; | 
|  | 349 | temp++; | 
|  | 350 | wrcount++; | 
|  | 351 | } else { /* arbitration or no acknowledge */ | 
|  | 352 | dev_err(&i2c_adap->dev, "sendbytes: error - bailout.\n"); | 
|  | 353 | i2c_stop(adap); | 
|  | 354 | return (retval<0)? retval : -EFAULT; | 
|  | 355 | /* got a better one ?? */ | 
|  | 356 | } | 
|  | 357 | #if 0 | 
|  | 358 | /* from asm/delay.h */ | 
|  | 359 | __delay(adap->mdelay * (loops_per_sec / 1000) ); | 
|  | 360 | #endif | 
|  | 361 | } | 
|  | 362 | return wrcount; | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | static inline int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) | 
|  | 366 | { | 
|  | 367 | int inval; | 
|  | 368 | int rdcount=0;   	/* counts bytes read */ | 
|  | 369 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; | 
|  | 370 | char *temp = msg->buf; | 
|  | 371 | int count = msg->len; | 
|  | 372 |  | 
|  | 373 | while (count > 0) { | 
|  | 374 | inval = i2c_inb(i2c_adap); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | if (inval>=0) { | 
|  | 376 | *temp = inval; | 
|  | 377 | rdcount++; | 
|  | 378 | } else {   /* read timed out */ | 
|  | 379 | printk(KERN_ERR "i2c-algo-bit.o: readbytes: i2c_inb timed out.\n"); | 
|  | 380 | break; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | temp++; | 
|  | 384 | count--; | 
|  | 385 |  | 
|  | 386 | if (msg->flags & I2C_M_NO_RD_ACK) | 
|  | 387 | continue; | 
|  | 388 |  | 
|  | 389 | if ( count > 0 ) {		/* send ack */ | 
|  | 390 | sdalo(adap); | 
|  | 391 | DEBPROTO(printk(" Am ")); | 
|  | 392 | } else { | 
|  | 393 | sdahi(adap);	/* neg. ack on last byte */ | 
|  | 394 | DEBPROTO(printk(" NAm ")); | 
|  | 395 | } | 
|  | 396 | if (sclhi(adap)<0) {	/* timeout */ | 
|  | 397 | sdahi(adap); | 
|  | 398 | printk(KERN_ERR "i2c-algo-bit.o: readbytes: Timeout at ack\n"); | 
|  | 399 | return -ETIMEDOUT; | 
|  | 400 | }; | 
|  | 401 | scllo(adap); | 
|  | 402 | sdahi(adap); | 
|  | 403 | } | 
|  | 404 | return rdcount; | 
|  | 405 | } | 
|  | 406 |  | 
|  | 407 | /* doAddress initiates the transfer by generating the start condition (in | 
|  | 408 | * try_address) and transmits the address in the necessary format to handle | 
|  | 409 | * reads, writes as well as 10bit-addresses. | 
|  | 410 | * returns: | 
|  | 411 | *  0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set | 
|  | 412 | * -x an error occurred (like: -EREMOTEIO if the device did not answer, or | 
|  | 413 | *	-ETIMEDOUT, for example if the lines are stuck...) | 
|  | 414 | */ | 
|  | 415 | static inline int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) | 
|  | 416 | { | 
|  | 417 | unsigned short flags = msg->flags; | 
|  | 418 | unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK; | 
|  | 419 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; | 
|  | 420 |  | 
|  | 421 | unsigned char addr; | 
|  | 422 | int ret, retries; | 
|  | 423 |  | 
|  | 424 | retries = nak_ok ? 0 : i2c_adap->retries; | 
|  | 425 |  | 
|  | 426 | if ( (flags & I2C_M_TEN)  ) { | 
|  | 427 | /* a ten bit address */ | 
|  | 428 | addr = 0xf0 | (( msg->addr >> 7) & 0x03); | 
|  | 429 | DEB2(printk(KERN_DEBUG "addr0: %d\n",addr)); | 
|  | 430 | /* try extended address code...*/ | 
|  | 431 | ret = try_address(i2c_adap, addr, retries); | 
|  | 432 | if ((ret != 1) && !nak_ok)  { | 
|  | 433 | printk(KERN_ERR "died at extended address code.\n"); | 
|  | 434 | return -EREMOTEIO; | 
|  | 435 | } | 
|  | 436 | /* the remaining 8 bit address */ | 
|  | 437 | ret = i2c_outb(i2c_adap,msg->addr & 0x7f); | 
|  | 438 | if ((ret != 1) && !nak_ok) { | 
|  | 439 | /* the chip did not ack / xmission error occurred */ | 
|  | 440 | printk(KERN_ERR "died at 2nd address code.\n"); | 
|  | 441 | return -EREMOTEIO; | 
|  | 442 | } | 
|  | 443 | if ( flags & I2C_M_RD ) { | 
|  | 444 | i2c_repstart(adap); | 
|  | 445 | /* okay, now switch into reading mode */ | 
|  | 446 | addr |= 0x01; | 
|  | 447 | ret = try_address(i2c_adap, addr, retries); | 
|  | 448 | if ((ret!=1) && !nak_ok) { | 
|  | 449 | printk(KERN_ERR "died at extended address code.\n"); | 
|  | 450 | return -EREMOTEIO; | 
|  | 451 | } | 
|  | 452 | } | 
|  | 453 | } else {		/* normal 7bit address	*/ | 
|  | 454 | addr = ( msg->addr << 1 ); | 
|  | 455 | if (flags & I2C_M_RD ) | 
|  | 456 | addr |= 1; | 
|  | 457 | if (flags & I2C_M_REV_DIR_ADDR ) | 
|  | 458 | addr ^= 1; | 
|  | 459 | ret = try_address(i2c_adap, addr, retries); | 
|  | 460 | if ((ret!=1) && !nak_ok) | 
|  | 461 | return -EREMOTEIO; | 
|  | 462 | } | 
|  | 463 |  | 
|  | 464 | return 0; | 
|  | 465 | } | 
|  | 466 |  | 
|  | 467 | static int bit_xfer(struct i2c_adapter *i2c_adap, | 
|  | 468 | struct i2c_msg msgs[], int num) | 
|  | 469 | { | 
|  | 470 | struct i2c_msg *pmsg; | 
|  | 471 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; | 
|  | 472 |  | 
|  | 473 | int i,ret; | 
|  | 474 | unsigned short nak_ok; | 
|  | 475 |  | 
|  | 476 | i2c_start(adap); | 
|  | 477 | for (i=0;i<num;i++) { | 
|  | 478 | pmsg = &msgs[i]; | 
|  | 479 | nak_ok = pmsg->flags & I2C_M_IGNORE_NAK; | 
|  | 480 | if (!(pmsg->flags & I2C_M_NOSTART)) { | 
|  | 481 | if (i) { | 
|  | 482 | i2c_repstart(adap); | 
|  | 483 | } | 
|  | 484 | ret = bit_doAddress(i2c_adap, pmsg); | 
|  | 485 | if ((ret != 0) && !nak_ok) { | 
|  | 486 | DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: NAK from device addr %2.2x msg #%d\n" | 
|  | 487 | ,msgs[i].addr,i)); | 
|  | 488 | return (ret<0) ? ret : -EREMOTEIO; | 
|  | 489 | } | 
|  | 490 | } | 
|  | 491 | if (pmsg->flags & I2C_M_RD ) { | 
|  | 492 | /* read bytes into buffer*/ | 
|  | 493 | ret = readbytes(i2c_adap, pmsg); | 
|  | 494 | DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: read %d bytes.\n",ret)); | 
|  | 495 | if (ret < pmsg->len ) { | 
|  | 496 | return (ret<0)? ret : -EREMOTEIO; | 
|  | 497 | } | 
|  | 498 | } else { | 
|  | 499 | /* write bytes from buffer */ | 
|  | 500 | ret = sendbytes(i2c_adap, pmsg); | 
|  | 501 | DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: wrote %d bytes.\n",ret)); | 
|  | 502 | if (ret < pmsg->len ) { | 
|  | 503 | return (ret<0) ? ret : -EREMOTEIO; | 
|  | 504 | } | 
|  | 505 | } | 
|  | 506 | } | 
|  | 507 | i2c_stop(adap); | 
|  | 508 | return num; | 
|  | 509 | } | 
|  | 510 |  | 
|  | 511 | static u32 bit_func(struct i2c_adapter *adap) | 
|  | 512 | { | 
|  | 513 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | | 
|  | 514 | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING; | 
|  | 515 | } | 
|  | 516 |  | 
|  | 517 |  | 
|  | 518 | /* -----exported algorithm data: -------------------------------------	*/ | 
|  | 519 |  | 
|  | 520 | static struct i2c_algorithm i2c_bit_algo = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | .master_xfer	= bit_xfer, | 
|  | 522 | .functionality	= bit_func, | 
|  | 523 | }; | 
|  | 524 |  | 
|  | 525 | /* | 
|  | 526 | * registering functions to load algorithms at runtime | 
|  | 527 | */ | 
|  | 528 | int i2c_bit_add_bus(struct i2c_adapter *adap) | 
|  | 529 | { | 
|  | 530 | struct i2c_algo_bit_data *bit_adap = adap->algo_data; | 
|  | 531 |  | 
|  | 532 | if (bit_test) { | 
|  | 533 | int ret = test_bus(bit_adap, adap->name); | 
|  | 534 | if (ret<0) | 
|  | 535 | return -ENODEV; | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | DEB2(dev_dbg(&adap->dev, "hw routines registered.\n")); | 
|  | 539 |  | 
|  | 540 | /* register new adapter to i2c module... */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | adap->algo = &i2c_bit_algo; | 
|  | 542 |  | 
|  | 543 | adap->timeout = 100;	/* default values, should	*/ | 
|  | 544 | adap->retries = 3;	/* be replaced by defines	*/ | 
|  | 545 |  | 
| Mark M. Hoffman | b39ad0c | 2006-07-01 17:16:06 +0200 | [diff] [blame] | 546 | return i2c_add_adapter(adap); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } | 
|  | 548 |  | 
|  | 549 |  | 
|  | 550 | int i2c_bit_del_bus(struct i2c_adapter *adap) | 
|  | 551 | { | 
|  | 552 | return i2c_del_adapter(adap); | 
|  | 553 | } | 
|  | 554 |  | 
|  | 555 | EXPORT_SYMBOL(i2c_bit_add_bus); | 
|  | 556 | EXPORT_SYMBOL(i2c_bit_del_bus); | 
|  | 557 |  | 
|  | 558 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); | 
|  | 559 | MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm"); | 
|  | 560 | MODULE_LICENSE("GPL"); | 
|  | 561 |  | 
|  | 562 | module_param(bit_test, bool, 0); | 
|  | 563 | module_param(i2c_debug, int, S_IRUGO | S_IWUSR); | 
|  | 564 |  | 
|  | 565 | MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck"); | 
|  | 566 | MODULE_PARM_DESC(i2c_debug, | 
|  | 567 | "debug level - 0 off; 1 normal; 2,3 more verbose; 9 bit-protocol"); |