i2c-algo-bit.c 15 KB

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