i2c-algo-bit.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:16k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

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