| 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 Kyösti Mälkki <kmalkki@cc.hut.fi> and even |
|---|
| 22 | Frodo Looijaard <frodol@dds.nl> */ |
|---|
| 23 | |
|---|
| 24 | /* $Id$ */ |
|---|
| 25 | |
|---|
| 26 | #include <linux/kernel.h> |
|---|
| 27 | #include <linux/module.h> |
|---|
| 28 | #include <linux/delay.h> |
|---|
| 29 | #include <linux/slab.h> |
|---|
| 30 | #include <linux/version.h> |
|---|
| 31 | #include <linux/init.h> |
|---|
| 32 | #include <asm/uaccess.h> |
|---|
| 33 | #include <linux/ioport.h> |
|---|
| 34 | #include <linux/errno.h> |
|---|
| 35 | #include <linux/sched.h> |
|---|
| 36 | |
|---|
| 37 | #include "i2c.h" |
|---|
| 38 | #include "i2c-algo-bit.h" |
|---|
| 39 | |
|---|
| 40 | /* ----- global defines ----------------------------------------------- */ |
|---|
| 41 | #define DEB(x) if (i2c_debug>=1) x; |
|---|
| 42 | #define DEB2(x) if (i2c_debug>=2) x; |
|---|
| 43 | #define DEBSTAT(x) if (i2c_debug>=3) x; /* print several statistical values*/ |
|---|
| 44 | #define DEBPROTO(x) if (i2c_debug>=9) { x; } |
|---|
| 45 | /* debug the protocol by showing transferred bits */ |
|---|
| 46 | |
|---|
| 47 | /* debugging - slow down transfer to have a look at the data .. */ |
|---|
| 48 | /* I use this with two leds&resistors, each one connected to sda,scl */ |
|---|
| 49 | /* respectively. This makes sure that the algorithm works. Some chips */ |
|---|
| 50 | /* might not like this, as they have an internal timeout of some mils */ |
|---|
| 51 | /* |
|---|
| 52 | #define SLO_IO jif=jiffies;while(jiffies<=jif+i2c_table[minor].veryslow)\ |
|---|
| 53 | if (need_resched) schedule(); |
|---|
| 54 | */ |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | /* ----- global variables --------------------------------------------- */ |
|---|
| 58 | |
|---|
| 59 | #ifdef SLO_IO |
|---|
| 60 | int jif; |
|---|
| 61 | #endif |
|---|
| 62 | |
|---|
| 63 | /* module parameters: |
|---|
| 64 | */ |
|---|
| 65 | static int i2c_debug; |
|---|
| 66 | static int bit_test; /* see if the line-setting functions work */ |
|---|
| 67 | static int bit_scan; /* have a look at what's hanging 'round */ |
|---|
| 68 | |
|---|
| 69 | /* --- setting states on the bus with the right timing: --------------- */ |
|---|
| 70 | |
|---|
| 71 | #define setsda(adap,val) adap->setsda(adap->data, val) |
|---|
| 72 | #define setscl(adap,val) adap->setscl(adap->data, val) |
|---|
| 73 | #define getsda(adap) adap->getsda(adap->data) |
|---|
| 74 | #define getscl(adap) adap->getscl(adap->data) |
|---|
| 75 | |
|---|
| 76 | static inline void sdalo(struct i2c_algo_bit_data *adap) |
|---|
| 77 | { |
|---|
| 78 | setsda(adap,0); |
|---|
| 79 | udelay(adap->udelay); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | static inline void sdahi(struct i2c_algo_bit_data *adap) |
|---|
| 83 | { |
|---|
| 84 | setsda(adap,1); |
|---|
| 85 | udelay(adap->udelay); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | static inline void scllo(struct i2c_algo_bit_data *adap) |
|---|
| 89 | { |
|---|
| 90 | setscl(adap,0); |
|---|
| 91 | udelay(adap->udelay); |
|---|
| 92 | #ifdef SLO_IO |
|---|
| 93 | SLO_IO |
|---|
| 94 | #endif |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /* |
|---|
| 98 | * Raise scl line, and do checking for delays. This is necessary for slower |
|---|
| 99 | * devices. |
|---|
| 100 | */ |
|---|
| 101 | static inline int sclhi(struct i2c_algo_bit_data *adap) |
|---|
| 102 | { |
|---|
| 103 | int start=jiffies; |
|---|
| 104 | |
|---|
| 105 | setscl(adap,1); |
|---|
| 106 | |
|---|
| 107 | udelay(adap->udelay); |
|---|
| 108 | |
|---|
| 109 | /* Not all adapters have scl sense line... */ |
|---|
| 110 | if (adap->getscl == NULL ) |
|---|
| 111 | return 0; |
|---|
| 112 | |
|---|
| 113 | while (! getscl(adap) ) { |
|---|
| 114 | /* the hw knows how to read the clock line, |
|---|
| 115 | * so we wait until it actually gets high. |
|---|
| 116 | * This is safer as some chips may hold it low |
|---|
| 117 | * while they are processing data internally. |
|---|
| 118 | */ |
|---|
| 119 | setscl(adap,1); |
|---|
| 120 | if (start+adap->timeout <= jiffies) { |
|---|
| 121 | return -ETIMEDOUT; |
|---|
| 122 | } |
|---|
| 123 | if (current->need_resched) |
|---|
| 124 | schedule(); |
|---|
| 125 | } |
|---|
| 126 | DEBSTAT(printk("needed %ld jiffies\n", jiffies-start)); |
|---|
| 127 | #ifdef SLO_IO |
|---|
| 128 | SLO_IO |
|---|
| 129 | #endif |
|---|
| 130 | return 0; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | /* --- other auxiliary functions -------------------------------------- */ |
|---|
| 135 | static void i2c_start(struct i2c_algo_bit_data *adap) |
|---|
| 136 | { |
|---|
| 137 | /* assert: scl, sda are high */ |
|---|
| 138 | DEBPROTO(printk("S ")); |
|---|
| 139 | sdalo(adap); |
|---|
| 140 | scllo(adap); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | static void i2c_repstart(struct i2c_algo_bit_data *adap) |
|---|
| 144 | { |
|---|
| 145 | /* scl, sda may not be high */ |
|---|
| 146 | DEBPROTO(printk(" Sr ")); |
|---|
| 147 | setsda(adap,1); |
|---|
| 148 | setscl(adap,1); |
|---|
| 149 | udelay(adap->udelay); |
|---|
| 150 | |
|---|
| 151 | sdalo(adap); |
|---|
| 152 | scllo(adap); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | static void i2c_stop(struct i2c_algo_bit_data *adap) |
|---|
| 157 | { |
|---|
| 158 | DEBPROTO(printk("P\n")); |
|---|
| 159 | /* assert: scl is low */ |
|---|
| 160 | sdalo(adap); |
|---|
| 161 | sclhi(adap); |
|---|
| 162 | sdahi(adap); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | /* send a byte without start cond., look for arbitration, |
|---|
| 168 | check ackn. from slave */ |
|---|
| 169 | /* returns: |
|---|
| 170 | * 1 if the device acknowledged |
|---|
| 171 | * 0 if the device did not ack |
|---|
| 172 | * -ETIMEDOUT if an error occurred (while raising the scl line) |
|---|
| 173 | */ |
|---|
| 174 | static int i2c_outb(struct i2c_adapter *i2c_adap, char c) |
|---|
| 175 | { |
|---|
| 176 | int i; |
|---|
| 177 | int sb; |
|---|
| 178 | int ack; |
|---|
| 179 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
|---|
| 180 | |
|---|
| 181 | /* assert: scl is low */ |
|---|
| 182 | DEB2(printk(" i2c_outb:%2.2X\n",c&0xff)); |
|---|
| 183 | for ( i=7 ; i>=0 ; i-- ) { |
|---|
| 184 | sb = c & ( 1 << i ); |
|---|
| 185 | setsda(adap,sb); |
|---|
| 186 | udelay(adap->udelay); |
|---|
| 187 | DEBPROTO(printk("%d",sb!=0)); |
|---|
| 188 | if (sclhi(adap)<0) { /* timed out */ |
|---|
| 189 | sdahi(adap); /* we don't want to block the net */ |
|---|
| 190 | return -ETIMEDOUT; |
|---|
| 191 | }; |
|---|
| 192 | /* do arbitration here: |
|---|
| 193 | * if ( sb && ! getsda(adap) ) -> ouch! Get out of here. |
|---|
| 194 | */ |
|---|
| 195 | setscl(adap, 0 ); |
|---|
| 196 | udelay(adap->udelay); |
|---|
| 197 | } |
|---|
| 198 | sdahi(adap); |
|---|
| 199 | if (sclhi(adap)<0){ /* timeout */ |
|---|
| 200 | return -ETIMEDOUT; |
|---|
| 201 | }; |
|---|
| 202 | /* read ack: SDA should be pulled down by slave */ |
|---|
| 203 | ack=getsda(adap); /* ack: sda is pulled low ->success. */ |
|---|
| 204 | DEB2(printk(" i2c_outb: getsda() = 0x%2.2x\n", ~ack )); |
|---|
| 205 | |
|---|
| 206 | DEBPROTO( printk("[%2.2x]",c&0xff) ); |
|---|
| 207 | DEBPROTO(if (0==ack){ printk(" A ");} else printk(" NA ") ); |
|---|
| 208 | scllo(adap); |
|---|
| 209 | return 0==ack; /* return 1 if device acked */ |
|---|
| 210 | /* assert: scl is low (sda undef) */ |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | static int i2c_inb(struct i2c_adapter *i2c_adap) |
|---|
| 215 | { |
|---|
| 216 | /* read byte via i2c port, without start/stop sequence */ |
|---|
| 217 | /* acknowledge is sent in i2c_read. */ |
|---|
| 218 | int i; |
|---|
| 219 | unsigned char indata=0; |
|---|
| 220 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
|---|
| 221 | |
|---|
| 222 | /* assert: scl is low */ |
|---|
| 223 | DEB2(printk("i2c_inb.\n")); |
|---|
| 224 | |
|---|
| 225 | sdahi(adap); |
|---|
| 226 | for (i=0;i<8;i++) { |
|---|
| 227 | if (sclhi(adap)<0) { /* timeout */ |
|---|
| 228 | return -ETIMEDOUT; |
|---|
| 229 | }; |
|---|
| 230 | indata *= 2; |
|---|
| 231 | if ( getsda(adap) ) |
|---|
| 232 | indata |= 0x01; |
|---|
| 233 | scllo(adap); |
|---|
| 234 | } |
|---|
| 235 | /* assert: scl is low */ |
|---|
| 236 | DEBPROTO(printk(" %2.2x", indata & 0xff)); |
|---|
| 237 | return (int) (indata & 0xff); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | /* |
|---|
| 241 | * Sanity check for the adapter hardware - check the reaction of |
|---|
| 242 | * the bus lines only if it seems to be idle. |
|---|
| 243 | */ |
|---|
| 244 | static int test_bus(struct i2c_algo_bit_data *adap, char* name) { |
|---|
| 245 | int scl,sda; |
|---|
| 246 | sda=getsda(adap); |
|---|
| 247 | if (adap->getscl==NULL) { |
|---|
| 248 | printk("i2c-algo-bit.o: Warning: Adapter can't read from clock line - skipping test.\n"); |
|---|
| 249 | return 0; |
|---|
| 250 | } |
|---|
| 251 | scl=getscl(adap); |
|---|
| 252 | printk("i2c-algo-bit.o: Adapter: %s scl: %d sda: %d -- testing...\n", |
|---|
| 253 | name,getscl(adap),getsda(adap)); |
|---|
| 254 | if (!scl || !sda ) { |
|---|
| 255 | printk("i2c-algo-bit.o: %s seems to be busy.\n",name); |
|---|
| 256 | goto bailout; |
|---|
| 257 | } |
|---|
| 258 | sdalo(adap); |
|---|
| 259 | printk("i2c-algo-bit.o:1 scl: %d sda: %d \n",getscl(adap), |
|---|
| 260 | getsda(adap)); |
|---|
| 261 | if ( 0 != getsda(adap) ) { |
|---|
| 262 | printk("i2c-algo-bit.o: %s SDA stuck high!\n",name); |
|---|
| 263 | sdahi(adap); |
|---|
| 264 | goto bailout; |
|---|
| 265 | } |
|---|
| 266 | if ( 0 == getscl(adap) ) { |
|---|
| 267 | printk("i2c-algo-bit.o: %s SCL unexpected low while pulling SDA low!\n", |
|---|
| 268 | name); |
|---|
| 269 | goto bailout; |
|---|
| 270 | } |
|---|
| 271 | sdahi(adap); |
|---|
| 272 | printk("i2c-algo-bit.o:2 scl: %d sda: %d \n",getscl(adap), |
|---|
| 273 | getsda(adap)); |
|---|
| 274 | if ( 0 == getsda(adap) ) { |
|---|
| 275 | printk("i2c-algo-bit.o: %s SDA stuck low!\n",name); |
|---|
| 276 | sdahi(adap); |
|---|
| 277 | goto bailout; |
|---|
| 278 | } |
|---|
| 279 | if ( 0 == getscl(adap) ) { |
|---|
| 280 | printk("i2c-algo-bit.o: %s SCL unexpected low while SDA high!\n", |
|---|
| 281 | name); |
|---|
| 282 | goto bailout; |
|---|
| 283 | } |
|---|
| 284 | scllo(adap); |
|---|
| 285 | printk("i2c-algo-bit.o:3 scl: %d sda: %d \n",getscl(adap), |
|---|
| 286 | getsda(adap)); |
|---|
| 287 | if ( 0 != getscl(adap) ) { |
|---|
| 288 | printk("i2c-algo-bit.o: %s SCL stuck high!\n",name); |
|---|
| 289 | sclhi(adap); |
|---|
| 290 | goto bailout; |
|---|
| 291 | } |
|---|
| 292 | if ( 0 == getsda(adap) ) { |
|---|
| 293 | printk("i2c-algo-bit.o: %s SDA unexpected low while pulling SCL low!\n", |
|---|
| 294 | name); |
|---|
| 295 | goto bailout; |
|---|
| 296 | } |
|---|
| 297 | sclhi(adap); |
|---|
| 298 | printk("i2c-algo-bit.o:4 scl: %d sda: %d \n",getscl(adap), |
|---|
| 299 | getsda(adap)); |
|---|
| 300 | if ( 0 == getscl(adap) ) { |
|---|
| 301 | printk("i2c-algo-bit.o: %s SCL stuck low!\n",name); |
|---|
| 302 | sclhi(adap); |
|---|
| 303 | goto bailout; |
|---|
| 304 | } |
|---|
| 305 | if ( 0 == getsda(adap) ) { |
|---|
| 306 | printk("i2c-algo-bit.o: %s SDA unexpected low while SCL high!\n", |
|---|
| 307 | name); |
|---|
| 308 | goto bailout; |
|---|
| 309 | } |
|---|
| 310 | printk("i2c-algo-bit.o: %s passed test.\n",name); |
|---|
| 311 | return 0; |
|---|
| 312 | bailout: |
|---|
| 313 | sdahi(adap); |
|---|
| 314 | sclhi(adap); |
|---|
| 315 | return -ENODEV; |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | /* ----- Utility functions |
|---|
| 319 | */ |
|---|
| 320 | |
|---|
| 321 | /* try_address tries to contact a chip for a number of |
|---|
| 322 | * times before it gives up. |
|---|
| 323 | * return values: |
|---|
| 324 | * 1 chip answered |
|---|
| 325 | * 0 chip did not answer |
|---|
| 326 | * -x transmission error |
|---|
| 327 | */ |
|---|
| 328 | static inline int try_address(struct i2c_adapter *i2c_adap, |
|---|
| 329 | unsigned char addr, int retries) |
|---|
| 330 | { |
|---|
| 331 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
|---|
| 332 | int i,ret = -1; |
|---|
| 333 | for (i=0;i<=retries;i++) { |
|---|
| 334 | ret = i2c_outb(i2c_adap,addr); |
|---|
| 335 | if (ret==1) |
|---|
| 336 | break; /* success! */ |
|---|
| 337 | i2c_stop(adap); |
|---|
| 338 | udelay(5/*adap->udelay*/); |
|---|
| 339 | if (i==retries) /* no success */ |
|---|
| 340 | break; |
|---|
| 341 | i2c_start(adap); |
|---|
| 342 | udelay(adap->udelay); |
|---|
| 343 | } |
|---|
| 344 | DEB2(if (i) printk("i2c-algo-bit.o: needed %d retries for %d\n", |
|---|
| 345 | i,addr)); |
|---|
| 346 | return ret; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | static int sendbytes(struct i2c_adapter *i2c_adap,const char *buf, int count) |
|---|
| 350 | { |
|---|
| 351 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
|---|
| 352 | char c; |
|---|
| 353 | const char *temp = buf; |
|---|
| 354 | int retval; |
|---|
| 355 | int wrcount=0; |
|---|
| 356 | |
|---|
| 357 | while (count > 0) { |
|---|
| 358 | c = *temp; |
|---|
| 359 | DEB2(printk("i2c-algo-bit.o: %s i2c_write: writing %2.2X\n", |
|---|
| 360 | i2c_adap->name, c&0xff)); |
|---|
| 361 | retval = i2c_outb(i2c_adap,c); |
|---|
| 362 | if (retval>0) { |
|---|
| 363 | count--; |
|---|
| 364 | temp++; |
|---|
| 365 | wrcount++; |
|---|
| 366 | } else { /* arbitration or no acknowledge */ |
|---|
| 367 | printk("i2c-algo-bit.o: %s i2c_write: error - bailout.\n", |
|---|
| 368 | i2c_adap->name); |
|---|
| 369 | i2c_stop(adap); |
|---|
| 370 | return (retval<0)? retval : -EFAULT; |
|---|
| 371 | /* got a better one ?? */ |
|---|
| 372 | } |
|---|
| 373 | #if 0 |
|---|
| 374 | /* from asm/delay.h */ |
|---|
| 375 | __delay(adap->mdelay * (loops_per_sec / 1000) ); |
|---|
| 376 | #endif |
|---|
| 377 | } |
|---|
| 378 | return wrcount; |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | static inline int readbytes(struct i2c_adapter *i2c_adap,char *buf,int count) |
|---|
| 382 | { |
|---|
| 383 | char *temp = buf; |
|---|
| 384 | int inval; |
|---|
| 385 | int rdcount=0; /* counts bytes read */ |
|---|
| 386 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
|---|
| 387 | |
|---|
| 388 | while (count > 0) { |
|---|
| 389 | inval = i2c_inb(i2c_adap); |
|---|
| 390 | /*printk("%#02x ",inval); if ( ! (count % 16) ) printk("\n"); */ |
|---|
| 391 | if (inval>=0) { |
|---|
| 392 | *temp = inval; |
|---|
| 393 | rdcount++; |
|---|
| 394 | } else { /* read timed out */ |
|---|
| 395 | printk("i2c-algo-bit.o: i2c_read: i2c_inb timed out.\n"); |
|---|
| 396 | break; |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | if ( count > 1 ) { /* send ack */ |
|---|
| 400 | sdalo(adap); |
|---|
| 401 | DEBPROTO(printk(" Am ")); |
|---|
| 402 | } else { |
|---|
| 403 | sdahi(adap); /* neg. ack on last byte */ |
|---|
| 404 | DEBPROTO(printk(" NAm ")); |
|---|
| 405 | } |
|---|
| 406 | if (sclhi(adap)<0) { /* timeout */ |
|---|
| 407 | sdahi(adap); |
|---|
| 408 | printk("i2c-algo-bit.o: i2c_read: Timeout at ack\n"); |
|---|
| 409 | return -ETIMEDOUT; |
|---|
| 410 | }; |
|---|
| 411 | scllo(adap); |
|---|
| 412 | sdahi(adap); |
|---|
| 413 | temp++; |
|---|
| 414 | count--; |
|---|
| 415 | } |
|---|
| 416 | return rdcount; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | /* doAddress initiates the transfer by generating the start condition (in |
|---|
| 420 | * try_address) and transmits the address in the necessary format to handle |
|---|
| 421 | * reads, writes as well as 10bit-addresses. |
|---|
| 422 | * returns: |
|---|
| 423 | * 0 everything went okay, the chip ack'ed |
|---|
| 424 | * -x an error occurred (like: -EREMOTEIO if the device did not answer, or |
|---|
| 425 | * -ETIMEDOUT, for example if the lines are stuck...) |
|---|
| 426 | */ |
|---|
| 427 | static inline int bit_doAddress(struct i2c_adapter *i2c_adap, |
|---|
| 428 | struct i2c_msg *msg, int retries) |
|---|
| 429 | { |
|---|
| 430 | unsigned short flags = msg->flags; |
|---|
| 431 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
|---|
| 432 | |
|---|
| 433 | unsigned char addr; |
|---|
| 434 | int ret; |
|---|
| 435 | if ( (flags & I2C_M_TEN) ) { |
|---|
| 436 | /* a ten bit address */ |
|---|
| 437 | addr = 0xf0 | (( msg->addr >> 7) & 0x03); |
|---|
| 438 | DEB2(printk("addr0: %d\n",addr)); |
|---|
| 439 | /* try extended address code...*/ |
|---|
| 440 | ret = try_address(i2c_adap, addr, retries); |
|---|
| 441 | if (ret!=1) { |
|---|
| 442 | printk("died at extended address code.\n"); |
|---|
| 443 | return -EREMOTEIO; |
|---|
| 444 | } |
|---|
| 445 | /* the remaining 8 bit address */ |
|---|
| 446 | ret = i2c_outb(i2c_adap,msg->addr & 0x7f); |
|---|
| 447 | if (ret != 1) { |
|---|
| 448 | /* the chip did not ack / xmission error occurred */ |
|---|
| 449 | printk("died at 2nd address code.\n"); |
|---|
| 450 | return -EREMOTEIO; |
|---|
| 451 | } |
|---|
| 452 | if ( flags & I2C_M_RD ) { |
|---|
| 453 | i2c_repstart(adap); |
|---|
| 454 | /* okay, now switch into reading mode */ |
|---|
| 455 | addr |= 0x01; |
|---|
| 456 | ret = try_address(i2c_adap, addr, retries); |
|---|
| 457 | if (ret!=1) { |
|---|
| 458 | printk("died at extended address code.\n"); |
|---|
| 459 | return -EREMOTEIO; |
|---|
| 460 | } |
|---|
| 461 | } |
|---|
| 462 | } else { /* normal 7bit address */ |
|---|
| 463 | addr = ( msg->addr << 1 ); |
|---|
| 464 | if (flags & I2C_M_RD ) |
|---|
| 465 | addr |= 1; |
|---|
| 466 | if (flags & I2C_M_REV_DIR_ADDR ) |
|---|
| 467 | addr ^= 1; |
|---|
| 468 | ret = try_address(i2c_adap, addr, retries); |
|---|
| 469 | if (ret!=1) { |
|---|
| 470 | return -EREMOTEIO; |
|---|
| 471 | } |
|---|
| 472 | } |
|---|
| 473 | return 0; |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | static int bit_xfer(struct i2c_adapter *i2c_adap, |
|---|
| 477 | struct i2c_msg msgs[], int num) |
|---|
| 478 | { |
|---|
| 479 | struct i2c_msg *pmsg; |
|---|
| 480 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
|---|
| 481 | |
|---|
| 482 | int i,ret; |
|---|
| 483 | |
|---|
| 484 | i2c_start(adap); |
|---|
| 485 | for (i=0;i<num;i++) { |
|---|
| 486 | pmsg = &msgs[i]; |
|---|
| 487 | if (!(pmsg->flags & I2C_M_NOSTART)) { |
|---|
| 488 | if (i) { |
|---|
| 489 | i2c_repstart(adap); |
|---|
| 490 | } |
|---|
| 491 | ret = bit_doAddress(i2c_adap,pmsg,i2c_adap->retries); |
|---|
| 492 | if (ret != 0) { |
|---|
| 493 | DEB2(printk("i2c-algo-bit.o: NAK from device adr %#2x msg #%d\n" |
|---|
| 494 | ,msgs[i].addr,i)); |
|---|
| 495 | return (ret<0) ? ret : -EREMOTEIO; |
|---|
| 496 | } |
|---|
| 497 | } |
|---|
| 498 | if (pmsg->flags & I2C_M_RD ) { |
|---|
| 499 | /* read bytes into buffer*/ |
|---|
| 500 | ret = readbytes(i2c_adap,pmsg->buf,pmsg->len); |
|---|
| 501 | DEB2(printk("i2c-algo-bit.o: read %d bytes.\n",ret)); |
|---|
| 502 | if (ret < pmsg->len ) { |
|---|
| 503 | return (ret<0)? ret : -EREMOTEIO; |
|---|
| 504 | } |
|---|
| 505 | } else { |
|---|
| 506 | /* write bytes from buffer */ |
|---|
| 507 | ret = sendbytes(i2c_adap,pmsg->buf,pmsg->len); |
|---|
| 508 | DEB2(printk("i2c-algo-bit.o: wrote %d bytes.\n",ret)); |
|---|
| 509 | if (ret < pmsg->len ) { |
|---|
| 510 | return (ret<0) ? ret : -EREMOTEIO; |
|---|
| 511 | } |
|---|
| 512 | } |
|---|
| 513 | } |
|---|
| 514 | i2c_stop(adap); |
|---|
| 515 | return num; |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | static int algo_control(struct i2c_adapter *adapter, |
|---|
| 519 | unsigned int cmd, unsigned long arg) |
|---|
| 520 | { |
|---|
| 521 | return 0; |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | static u32 bit_func(struct i2c_adapter *adap) |
|---|
| 525 | { |
|---|
| 526 | return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | |
|---|
| 527 | I2C_FUNC_PROTOCOL_MANGLING; |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | /* -----exported algorithm data: ------------------------------------- */ |
|---|
| 532 | |
|---|
| 533 | static struct i2c_algorithm i2c_bit_algo = { |
|---|
| 534 | "Bit-shift algorithm", |
|---|
| 535 | I2C_ALGO_BIT, |
|---|
| 536 | bit_xfer, |
|---|
| 537 | NULL, |
|---|
| 538 | NULL, /* slave_xmit */ |
|---|
| 539 | NULL, /* slave_recv */ |
|---|
| 540 | algo_control, /* ioctl */ |
|---|
| 541 | bit_func, /* functionality */ |
|---|
| 542 | }; |
|---|
| 543 | |
|---|
| 544 | /* |
|---|
| 545 | * registering functions to load algorithms at runtime |
|---|
| 546 | */ |
|---|
| 547 | int i2c_bit_add_bus(struct i2c_adapter *adap) |
|---|
| 548 | { |
|---|
| 549 | int i; |
|---|
| 550 | struct i2c_algo_bit_data *bit_adap = adap->algo_data; |
|---|
| 551 | |
|---|
| 552 | if (bit_test) { |
|---|
| 553 | int ret = test_bus(bit_adap, adap->name); |
|---|
| 554 | if (ret<0) |
|---|
| 555 | return -ENODEV; |
|---|
| 556 | } |
|---|
| 557 | |
|---|
| 558 | DEB2(printk("i2c-algo-bit.o: hw routines for %s registered.\n", |
|---|
| 559 | adap->name)); |
|---|
| 560 | |
|---|
| 561 | /* register new adapter to i2c module... */ |
|---|
| 562 | |
|---|
| 563 | adap->id |= i2c_bit_algo.id; |
|---|
| 564 | adap->algo = &i2c_bit_algo; |
|---|
| 565 | |
|---|
| 566 | adap->timeout = 100; /* default values, should */ |
|---|
| 567 | adap->retries = 3; /* be replaced by defines */ |
|---|
| 568 | |
|---|
| 569 | /* scan bus */ |
|---|
| 570 | if (bit_scan) { |
|---|
| 571 | int ack; |
|---|
| 572 | printk(KERN_INFO " i2c-algo-bit.o: scanning bus %s.\n", |
|---|
| 573 | adap->name); |
|---|
| 574 | for (i = 0x00; i < 0xff; i+=2) { |
|---|
| 575 | i2c_start(bit_adap); |
|---|
| 576 | ack = i2c_outb(adap,i); |
|---|
| 577 | i2c_stop(bit_adap); |
|---|
| 578 | if (ack>0) { |
|---|
| 579 | printk("(%02x)",i>>1); |
|---|
| 580 | } else |
|---|
| 581 | printk("."); |
|---|
| 582 | } |
|---|
| 583 | printk("\n"); |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | #ifdef MODULE |
|---|
| 587 | MOD_INC_USE_COUNT; |
|---|
| 588 | #endif |
|---|
| 589 | i2c_add_adapter(adap); |
|---|
| 590 | |
|---|
| 591 | return 0; |
|---|
| 592 | } |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | int i2c_bit_del_bus(struct i2c_adapter *adap) |
|---|
| 596 | { |
|---|
| 597 | int res; |
|---|
| 598 | |
|---|
| 599 | if ((res = i2c_del_adapter(adap)) < 0) |
|---|
| 600 | return res; |
|---|
| 601 | |
|---|
| 602 | DEB2(printk("i2c-algo-bit.o: adapter unregistered: %s\n",adap->name)); |
|---|
| 603 | |
|---|
| 604 | #ifdef MODULE |
|---|
| 605 | MOD_DEC_USE_COUNT; |
|---|
| 606 | #endif |
|---|
| 607 | return 0; |
|---|
| 608 | } |
|---|
| 609 | |
|---|
| 610 | int __init i2c_algo_bit_init (void) |
|---|
| 611 | { |
|---|
| 612 | printk("i2c-algo-bit.o: i2c bit algorithm module\n"); |
|---|
| 613 | return 0; |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | |
|---|
| 618 | EXPORT_SYMBOL(i2c_bit_add_bus); |
|---|
| 619 | EXPORT_SYMBOL(i2c_bit_del_bus); |
|---|
| 620 | |
|---|
| 621 | #ifdef MODULE |
|---|
| 622 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
|---|
| 623 | MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm"); |
|---|
| 624 | |
|---|
| 625 | MODULE_PARM(bit_test, "i"); |
|---|
| 626 | MODULE_PARM(bit_scan, "i"); |
|---|
| 627 | MODULE_PARM(i2c_debug,"i"); |
|---|
| 628 | |
|---|
| 629 | MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck"); |
|---|
| 630 | MODULE_PARM_DESC(bit_scan, "Scan for active chips on the bus"); |
|---|
| 631 | MODULE_PARM_DESC(i2c_debug, |
|---|
| 632 | "debug level - 0 off; 1 normal; 2,3 more verbose; 9 bit-protocol"); |
|---|
| 633 | |
|---|
| 634 | int init_module(void) |
|---|
| 635 | { |
|---|
| 636 | return i2c_algo_bit_init(); |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | void cleanup_module(void) |
|---|
| 640 | { |
|---|
| 641 | } |
|---|
| 642 | #endif |
|---|