i2c-algo-bit.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 acknak(struct i2c_adapter *i2c_adap, int is_ack)
  315. {
  316. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  317. /* assert: sda is high */
  318. if (is_ack) /* send ack */
  319. setsda(adap, 0);
  320. udelay((adap->udelay + 1) / 2);
  321. if (sclhi(adap) < 0) { /* timeout */
  322. dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n");
  323. return -ETIMEDOUT;
  324. }
  325. scllo(adap);
  326. return 0;
  327. }
  328. static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  329. {
  330. int inval;
  331. int rdcount=0; /* counts bytes read */
  332. unsigned char *temp = msg->buf;
  333. int count = msg->len;
  334. const unsigned flags = msg->flags;
  335. while (count > 0) {
  336. inval = i2c_inb(i2c_adap);
  337. if (inval>=0) {
  338. *temp = inval;
  339. rdcount++;
  340. } else { /* read timed out */
  341. break;
  342. }
  343. temp++;
  344. count--;
  345. /* Some SMBus transactions require that we receive the
  346. transaction length as the first read byte. */
  347. if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) {
  348. if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) {
  349. if (!(flags & I2C_M_NO_RD_ACK))
  350. acknak(i2c_adap, 0);
  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. bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n",
  362. inval,
  363. (flags & I2C_M_NO_RD_ACK)
  364. ? "(no ack/nak)"
  365. : (count ? "A" : "NA"));
  366. if (!(flags & I2C_M_NO_RD_ACK)) {
  367. inval = acknak(i2c_adap, count);
  368. if (inval < 0)
  369. return inval;
  370. }
  371. }
  372. return rdcount;
  373. }
  374. /* doAddress initiates the transfer by generating the start condition (in
  375. * try_address) and transmits the address in the necessary format to handle
  376. * reads, writes as well as 10bit-addresses.
  377. * returns:
  378. * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
  379. * -x an error occurred (like: -EREMOTEIO if the device did not answer, or
  380. * -ETIMEDOUT, for example if the lines are stuck...)
  381. */
  382. static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  383. {
  384. unsigned short flags = msg->flags;
  385. unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
  386. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  387. unsigned char addr;
  388. int ret, retries;
  389. retries = nak_ok ? 0 : i2c_adap->retries;
  390. if ( (flags & I2C_M_TEN) ) {
  391. /* a ten bit address */
  392. addr = 0xf0 | (( msg->addr >> 7) & 0x03);
  393. bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr);
  394. /* try extended address code...*/
  395. ret = try_address(i2c_adap, addr, retries);
  396. if ((ret != 1) && !nak_ok) {
  397. dev_err(&i2c_adap->dev,
  398. "died at extended address code\n");
  399. return -EREMOTEIO;
  400. }
  401. /* the remaining 8 bit address */
  402. ret = i2c_outb(i2c_adap,msg->addr & 0x7f);
  403. if ((ret != 1) && !nak_ok) {
  404. /* the chip did not ack / xmission error occurred */
  405. dev_err(&i2c_adap->dev, "died at 2nd address code\n");
  406. return -EREMOTEIO;
  407. }
  408. if ( flags & I2C_M_RD ) {
  409. bit_dbg(3, &i2c_adap->dev, "emitting repeated "
  410. "start condition\n");
  411. i2c_repstart(adap);
  412. /* okay, now switch into reading mode */
  413. addr |= 0x01;
  414. ret = try_address(i2c_adap, addr, retries);
  415. if ((ret!=1) && !nak_ok) {
  416. dev_err(&i2c_adap->dev,
  417. "died at repeated address code\n");
  418. return -EREMOTEIO;
  419. }
  420. }
  421. } else { /* normal 7bit address */
  422. addr = ( msg->addr << 1 );
  423. if (flags & I2C_M_RD )
  424. addr |= 1;
  425. if (flags & I2C_M_REV_DIR_ADDR )
  426. addr ^= 1;
  427. ret = try_address(i2c_adap, addr, retries);
  428. if ((ret!=1) && !nak_ok)
  429. return -EREMOTEIO;
  430. }
  431. return 0;
  432. }
  433. static int bit_xfer(struct i2c_adapter *i2c_adap,
  434. struct i2c_msg msgs[], int num)
  435. {
  436. struct i2c_msg *pmsg;
  437. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  438. int i,ret;
  439. unsigned short nak_ok;
  440. bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
  441. i2c_start(adap);
  442. for (i=0;i<num;i++) {
  443. pmsg = &msgs[i];
  444. nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
  445. if (!(pmsg->flags & I2C_M_NOSTART)) {
  446. if (i) {
  447. bit_dbg(3, &i2c_adap->dev, "emitting "
  448. "repeated start condition\n");
  449. i2c_repstart(adap);
  450. }
  451. ret = bit_doAddress(i2c_adap, pmsg);
  452. if ((ret != 0) && !nak_ok) {
  453. bit_dbg(1, &i2c_adap->dev, "NAK from "
  454. "device addr 0x%02x msg #%d\n",
  455. msgs[i].addr, i);
  456. goto bailout;
  457. }
  458. }
  459. if (pmsg->flags & I2C_M_RD ) {
  460. /* read bytes into buffer*/
  461. ret = readbytes(i2c_adap, pmsg);
  462. if (ret >= 1)
  463. bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n",
  464. ret, ret == 1 ? "" : "s");
  465. if (ret < pmsg->len) {
  466. if (ret >= 0)
  467. ret = -EREMOTEIO;
  468. goto bailout;
  469. }
  470. } else {
  471. /* write bytes from buffer */
  472. ret = sendbytes(i2c_adap, pmsg);
  473. if (ret >= 1)
  474. bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n",
  475. ret, ret == 1 ? "" : "s");
  476. if (ret < pmsg->len) {
  477. if (ret >= 0)
  478. ret = -EREMOTEIO;
  479. goto bailout;
  480. }
  481. }
  482. }
  483. ret = i;
  484. bailout:
  485. bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
  486. i2c_stop(adap);
  487. return ret;
  488. }
  489. static u32 bit_func(struct i2c_adapter *adap)
  490. {
  491. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  492. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  493. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  494. I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  495. }
  496. /* -----exported algorithm data: ------------------------------------- */
  497. static const struct i2c_algorithm i2c_bit_algo = {
  498. .master_xfer = bit_xfer,
  499. .functionality = bit_func,
  500. };
  501. /*
  502. * registering functions to load algorithms at runtime
  503. */
  504. static int i2c_bit_prepare_bus(struct i2c_adapter *adap)
  505. {
  506. struct i2c_algo_bit_data *bit_adap = adap->algo_data;
  507. if (bit_test) {
  508. int ret = test_bus(bit_adap, adap->name);
  509. if (ret<0)
  510. return -ENODEV;
  511. }
  512. /* register new adapter to i2c module... */
  513. adap->algo = &i2c_bit_algo;
  514. adap->timeout = 100; /* default values, should */
  515. adap->retries = 3; /* be replaced by defines */
  516. return 0;
  517. }
  518. int i2c_bit_add_bus(struct i2c_adapter *adap)
  519. {
  520. int err;
  521. err = i2c_bit_prepare_bus(adap);
  522. if (err)
  523. return err;
  524. return i2c_add_adapter(adap);
  525. }
  526. EXPORT_SYMBOL(i2c_bit_add_bus);
  527. int i2c_bit_add_numbered_bus(struct i2c_adapter *adap)
  528. {
  529. int err;
  530. err = i2c_bit_prepare_bus(adap);
  531. if (err)
  532. return err;
  533. return i2c_add_numbered_adapter(adap);
  534. }
  535. EXPORT_SYMBOL(i2c_bit_add_numbered_bus);
  536. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  537. MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
  538. MODULE_LICENSE("GPL");