i2c-algo-bit.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. /* This hw knows how to read the clock line, so we wait
  83. * until it actually gets high. This is safer as some
  84. * chips may hold it low ("clock stretching") while they
  85. * are processing data internally.
  86. */
  87. if (time_after_eq(jiffies, start + adap->timeout))
  88. return -ETIMEDOUT;
  89. cond_resched();
  90. }
  91. #ifdef DEBUG
  92. if (jiffies != start && i2c_debug >= 3)
  93. pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go "
  94. "high\n", jiffies - start);
  95. #endif
  96. done:
  97. udelay(adap->udelay);
  98. return 0;
  99. }
  100. /* --- other auxiliary functions -------------------------------------- */
  101. static void i2c_start(struct i2c_algo_bit_data *adap)
  102. {
  103. /* assert: scl, sda are high */
  104. setsda(adap, 0);
  105. udelay(adap->udelay);
  106. scllo(adap);
  107. }
  108. static void i2c_repstart(struct i2c_algo_bit_data *adap)
  109. {
  110. /* assert: scl is low */
  111. sdahi(adap);
  112. sclhi(adap);
  113. setsda(adap, 0);
  114. udelay(adap->udelay);
  115. scllo(adap);
  116. }
  117. static void i2c_stop(struct i2c_algo_bit_data *adap)
  118. {
  119. /* assert: scl is low */
  120. sdalo(adap);
  121. sclhi(adap);
  122. setsda(adap, 1);
  123. udelay(adap->udelay);
  124. }
  125. /* send a byte without start cond., look for arbitration,
  126. check ackn. from slave */
  127. /* returns:
  128. * 1 if the device acknowledged
  129. * 0 if the device did not ack
  130. * -ETIMEDOUT if an error occurred (while raising the scl line)
  131. */
  132. static int i2c_outb(struct i2c_adapter *i2c_adap, unsigned char c)
  133. {
  134. int i;
  135. int sb;
  136. int ack;
  137. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  138. /* assert: scl is low */
  139. for (i = 7; i >= 0; i--) {
  140. sb = (c >> i) & 1;
  141. setsda(adap, sb);
  142. udelay((adap->udelay + 1) / 2);
  143. if (sclhi(adap) < 0) { /* timed out */
  144. bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "
  145. "timeout at bit #%d\n", (int)c, i);
  146. return -ETIMEDOUT;
  147. }
  148. /* FIXME do arbitration here:
  149. * if (sb && !getsda(adap)) -> ouch! Get out of here.
  150. *
  151. * Report a unique code, so higher level code can retry
  152. * the whole (combined) message and *NOT* issue STOP.
  153. */
  154. scllo(adap);
  155. }
  156. sdahi(adap);
  157. if (sclhi(adap) < 0) { /* timeout */
  158. bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "
  159. "timeout at ack\n", (int)c);
  160. return -ETIMEDOUT;
  161. }
  162. /* read ack: SDA should be pulled down by slave, or it may
  163. * NAK (usually to report problems with the data we wrote).
  164. */
  165. ack = !getsda(adap); /* ack: sda is pulled low -> success */
  166. bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c,
  167. ack ? "A" : "NA");
  168. scllo(adap);
  169. return ack;
  170. /* assert: scl is low (sda undef) */
  171. }
  172. static int i2c_inb(struct i2c_adapter *i2c_adap)
  173. {
  174. /* read byte via i2c port, without start/stop sequence */
  175. /* acknowledge is sent in i2c_read. */
  176. int i;
  177. unsigned char indata = 0;
  178. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  179. /* assert: scl is low */
  180. sdahi(adap);
  181. for (i = 0; i < 8; i++) {
  182. if (sclhi(adap) < 0) { /* timeout */
  183. bit_dbg(1, &i2c_adap->dev, "i2c_inb: timeout at bit "
  184. "#%d\n", 7 - i);
  185. return -ETIMEDOUT;
  186. }
  187. indata *= 2;
  188. if (getsda(adap))
  189. indata |= 0x01;
  190. setscl(adap, 0);
  191. udelay(i == 7 ? adap->udelay / 2 : adap->udelay);
  192. }
  193. /* assert: scl is low */
  194. return indata;
  195. }
  196. /*
  197. * Sanity check for the adapter hardware - check the reaction of
  198. * the bus lines only if it seems to be idle.
  199. */
  200. static int test_bus(struct i2c_algo_bit_data *adap, char *name)
  201. {
  202. int scl, sda;
  203. if (adap->getscl == NULL)
  204. pr_info("%s: Testing SDA only, SCL is not readable\n", name);
  205. sda = getsda(adap);
  206. scl = (adap->getscl == NULL) ? 1 : getscl(adap);
  207. if (!scl || !sda) {
  208. printk(KERN_WARNING "%s: bus seems to be busy\n", name);
  209. goto bailout;
  210. }
  211. sdalo(adap);
  212. sda = getsda(adap);
  213. scl = (adap->getscl == NULL) ? 1 : getscl(adap);
  214. if (sda) {
  215. printk(KERN_WARNING "%s: SDA stuck high!\n", name);
  216. goto bailout;
  217. }
  218. if (!scl) {
  219. printk(KERN_WARNING "%s: SCL unexpected low "
  220. "while pulling SDA low!\n", name);
  221. goto bailout;
  222. }
  223. sdahi(adap);
  224. sda = getsda(adap);
  225. scl = (adap->getscl == NULL) ? 1 : getscl(adap);
  226. if (!sda) {
  227. printk(KERN_WARNING "%s: SDA stuck low!\n", name);
  228. goto bailout;
  229. }
  230. if (!scl) {
  231. printk(KERN_WARNING "%s: SCL unexpected low "
  232. "while pulling SDA high!\n", name);
  233. goto bailout;
  234. }
  235. scllo(adap);
  236. sda = getsda(adap);
  237. scl = (adap->getscl == NULL) ? 0 : getscl(adap);
  238. if (scl) {
  239. printk(KERN_WARNING "%s: SCL stuck high!\n", name);
  240. goto bailout;
  241. }
  242. if (!sda) {
  243. printk(KERN_WARNING "%s: SDA unexpected low "
  244. "while pulling SCL low!\n", name);
  245. goto bailout;
  246. }
  247. sclhi(adap);
  248. sda = getsda(adap);
  249. scl = (adap->getscl == NULL) ? 1 : getscl(adap);
  250. if (!scl) {
  251. printk(KERN_WARNING "%s: SCL stuck low!\n", name);
  252. goto bailout;
  253. }
  254. if (!sda) {
  255. printk(KERN_WARNING "%s: SDA unexpected low "
  256. "while pulling SCL high!\n", name);
  257. goto bailout;
  258. }
  259. pr_info("%s: Test OK\n", name);
  260. return 0;
  261. bailout:
  262. sdahi(adap);
  263. sclhi(adap);
  264. return -ENODEV;
  265. }
  266. /* ----- Utility functions
  267. */
  268. /* try_address tries to contact a chip for a number of
  269. * times before it gives up.
  270. * return values:
  271. * 1 chip answered
  272. * 0 chip did not answer
  273. * -x transmission error
  274. */
  275. static int try_address(struct i2c_adapter *i2c_adap,
  276. unsigned char addr, int retries)
  277. {
  278. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  279. int i, ret = 0;
  280. for (i = 0; i <= retries; i++) {
  281. ret = i2c_outb(i2c_adap, addr);
  282. if (ret == 1 || i == retries)
  283. break;
  284. bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
  285. i2c_stop(adap);
  286. udelay(adap->udelay);
  287. yield();
  288. bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
  289. i2c_start(adap);
  290. }
  291. if (i && ret)
  292. bit_dbg(1, &i2c_adap->dev, "Used %d tries to %s client at "
  293. "0x%02x: %s\n", i + 1,
  294. addr & 1 ? "read from" : "write to", addr >> 1,
  295. ret == 1 ? "success" : "failed, timeout?");
  296. return ret;
  297. }
  298. static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  299. {
  300. const unsigned char *temp = msg->buf;
  301. int count = msg->len;
  302. unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
  303. int retval;
  304. int wrcount = 0;
  305. while (count > 0) {
  306. retval = i2c_outb(i2c_adap, *temp);
  307. /* OK/ACK; or ignored NAK */
  308. if ((retval > 0) || (nak_ok && (retval == 0))) {
  309. count--;
  310. temp++;
  311. wrcount++;
  312. /* A slave NAKing the master means the slave didn't like
  313. * something about the data it saw. For example, maybe
  314. * the SMBus PEC was wrong.
  315. */
  316. } else if (retval == 0) {
  317. dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n");
  318. return -EIO;
  319. /* Timeout; or (someday) lost arbitration
  320. *
  321. * FIXME Lost ARB implies retrying the transaction from
  322. * the first message, after the "winning" master issues
  323. * its STOP. As a rule, upper layer code has no reason
  324. * to know or care about this ... it is *NOT* an error.
  325. */
  326. } else {
  327. dev_err(&i2c_adap->dev, "sendbytes: error %d\n",
  328. retval);
  329. return retval;
  330. }
  331. }
  332. return wrcount;
  333. }
  334. static int acknak(struct i2c_adapter *i2c_adap, int is_ack)
  335. {
  336. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  337. /* assert: sda is high */
  338. if (is_ack) /* send ack */
  339. setsda(adap, 0);
  340. udelay((adap->udelay + 1) / 2);
  341. if (sclhi(adap) < 0) { /* timeout */
  342. dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n");
  343. return -ETIMEDOUT;
  344. }
  345. scllo(adap);
  346. return 0;
  347. }
  348. static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  349. {
  350. int inval;
  351. int rdcount = 0; /* counts bytes read */
  352. unsigned char *temp = msg->buf;
  353. int count = msg->len;
  354. const unsigned flags = msg->flags;
  355. while (count > 0) {
  356. inval = i2c_inb(i2c_adap);
  357. if (inval >= 0) {
  358. *temp = inval;
  359. rdcount++;
  360. } else { /* read timed out */
  361. break;
  362. }
  363. temp++;
  364. count--;
  365. /* Some SMBus transactions require that we receive the
  366. transaction length as the first read byte. */
  367. if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) {
  368. if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) {
  369. if (!(flags & I2C_M_NO_RD_ACK))
  370. acknak(i2c_adap, 0);
  371. dev_err(&i2c_adap->dev, "readbytes: invalid "
  372. "block length (%d)\n", inval);
  373. return -EREMOTEIO;
  374. }
  375. /* The original count value accounts for the extra
  376. bytes, that is, either 1 for a regular transaction,
  377. or 2 for a PEC transaction. */
  378. count += inval;
  379. msg->len += inval;
  380. }
  381. bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n",
  382. inval,
  383. (flags & I2C_M_NO_RD_ACK)
  384. ? "(no ack/nak)"
  385. : (count ? "A" : "NA"));
  386. if (!(flags & I2C_M_NO_RD_ACK)) {
  387. inval = acknak(i2c_adap, count);
  388. if (inval < 0)
  389. return inval;
  390. }
  391. }
  392. return rdcount;
  393. }
  394. /* doAddress initiates the transfer by generating the start condition (in
  395. * try_address) and transmits the address in the necessary format to handle
  396. * reads, writes as well as 10bit-addresses.
  397. * returns:
  398. * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
  399. * -x an error occurred (like: -EREMOTEIO if the device did not answer, or
  400. * -ETIMEDOUT, for example if the lines are stuck...)
  401. */
  402. static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  403. {
  404. unsigned short flags = msg->flags;
  405. unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
  406. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  407. unsigned char addr;
  408. int ret, retries;
  409. retries = nak_ok ? 0 : i2c_adap->retries;
  410. if (flags & I2C_M_TEN) {
  411. /* a ten bit address */
  412. addr = 0xf0 | ((msg->addr >> 7) & 0x03);
  413. bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr);
  414. /* try extended address code...*/
  415. ret = try_address(i2c_adap, addr, retries);
  416. if ((ret != 1) && !nak_ok) {
  417. dev_err(&i2c_adap->dev,
  418. "died at extended address code\n");
  419. return -EREMOTEIO;
  420. }
  421. /* the remaining 8 bit address */
  422. ret = i2c_outb(i2c_adap, msg->addr & 0x7f);
  423. if ((ret != 1) && !nak_ok) {
  424. /* the chip did not ack / xmission error occurred */
  425. dev_err(&i2c_adap->dev, "died at 2nd address code\n");
  426. return -EREMOTEIO;
  427. }
  428. if (flags & I2C_M_RD) {
  429. bit_dbg(3, &i2c_adap->dev, "emitting repeated "
  430. "start condition\n");
  431. i2c_repstart(adap);
  432. /* okay, now switch into reading mode */
  433. addr |= 0x01;
  434. ret = try_address(i2c_adap, addr, retries);
  435. if ((ret != 1) && !nak_ok) {
  436. dev_err(&i2c_adap->dev,
  437. "died at repeated address code\n");
  438. return -EREMOTEIO;
  439. }
  440. }
  441. } else { /* normal 7bit address */
  442. addr = msg->addr << 1;
  443. if (flags & I2C_M_RD)
  444. addr |= 1;
  445. if (flags & I2C_M_REV_DIR_ADDR)
  446. addr ^= 1;
  447. ret = try_address(i2c_adap, addr, retries);
  448. if ((ret != 1) && !nak_ok)
  449. return -ENXIO;
  450. }
  451. return 0;
  452. }
  453. static int bit_xfer(struct i2c_adapter *i2c_adap,
  454. struct i2c_msg msgs[], int num)
  455. {
  456. struct i2c_msg *pmsg;
  457. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  458. int i, ret;
  459. unsigned short nak_ok;
  460. bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
  461. i2c_start(adap);
  462. for (i = 0; i < num; i++) {
  463. pmsg = &msgs[i];
  464. nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
  465. if (!(pmsg->flags & I2C_M_NOSTART)) {
  466. if (i) {
  467. bit_dbg(3, &i2c_adap->dev, "emitting "
  468. "repeated start condition\n");
  469. i2c_repstart(adap);
  470. }
  471. ret = bit_doAddress(i2c_adap, pmsg);
  472. if ((ret != 0) && !nak_ok) {
  473. bit_dbg(1, &i2c_adap->dev, "NAK from "
  474. "device addr 0x%02x msg #%d\n",
  475. msgs[i].addr, i);
  476. goto bailout;
  477. }
  478. }
  479. if (pmsg->flags & I2C_M_RD) {
  480. /* read bytes into buffer*/
  481. ret = readbytes(i2c_adap, pmsg);
  482. if (ret >= 1)
  483. bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n",
  484. ret, ret == 1 ? "" : "s");
  485. if (ret < pmsg->len) {
  486. if (ret >= 0)
  487. ret = -EREMOTEIO;
  488. goto bailout;
  489. }
  490. } else {
  491. /* write bytes from buffer */
  492. ret = sendbytes(i2c_adap, pmsg);
  493. if (ret >= 1)
  494. bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n",
  495. ret, ret == 1 ? "" : "s");
  496. if (ret < pmsg->len) {
  497. if (ret >= 0)
  498. ret = -EREMOTEIO;
  499. goto bailout;
  500. }
  501. }
  502. }
  503. ret = i;
  504. bailout:
  505. bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
  506. i2c_stop(adap);
  507. return ret;
  508. }
  509. static u32 bit_func(struct i2c_adapter *adap)
  510. {
  511. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  512. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  513. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  514. I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  515. }
  516. /* -----exported algorithm data: ------------------------------------- */
  517. static const struct i2c_algorithm i2c_bit_algo = {
  518. .master_xfer = bit_xfer,
  519. .functionality = bit_func,
  520. };
  521. /*
  522. * registering functions to load algorithms at runtime
  523. */
  524. static int i2c_bit_prepare_bus(struct i2c_adapter *adap)
  525. {
  526. struct i2c_algo_bit_data *bit_adap = adap->algo_data;
  527. if (bit_test) {
  528. int ret = test_bus(bit_adap, adap->name);
  529. if (ret < 0)
  530. return -ENODEV;
  531. }
  532. /* register new adapter to i2c module... */
  533. adap->algo = &i2c_bit_algo;
  534. adap->timeout = 100; /* default values, should */
  535. adap->retries = 3; /* be replaced by defines */
  536. return 0;
  537. }
  538. int i2c_bit_add_bus(struct i2c_adapter *adap)
  539. {
  540. int err;
  541. err = i2c_bit_prepare_bus(adap);
  542. if (err)
  543. return err;
  544. return i2c_add_adapter(adap);
  545. }
  546. EXPORT_SYMBOL(i2c_bit_add_bus);
  547. int i2c_bit_add_numbered_bus(struct i2c_adapter *adap)
  548. {
  549. int err;
  550. err = i2c_bit_prepare_bus(adap);
  551. if (err)
  552. return err;
  553. return i2c_add_numbered_adapter(adap);
  554. }
  555. EXPORT_SYMBOL(i2c_bit_add_numbered_bus);
  556. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  557. MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
  558. MODULE_LICENSE("GPL");