i2c-algo-bit.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * i2c-algo-bit.c: i2c driver algorithms for bit-shift adapters
  4. *
  5. * Copyright (C) 1995-2000 Simon G. Vogl
  6. *
  7. * With some changes from Frodo Looijaard <[email protected]>, Kyösti Mälkki
  8. * <[email protected]> and Jean Delvare <[email protected]>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/errno.h>
  14. #include <linux/sched.h>
  15. #include <linux/i2c.h>
  16. #include <linux/i2c-algo-bit.h>
  17. /* ----- global defines ----------------------------------------------- */
  18. #ifdef DEBUG
  19. #define bit_dbg(level, dev, format, args...) \
  20. do { \
  21. if (i2c_debug >= level) \
  22. dev_dbg(dev, format, ##args); \
  23. } while (0)
  24. #else
  25. #define bit_dbg(level, dev, format, args...) \
  26. do {} while (0)
  27. #endif /* DEBUG */
  28. /* ----- global variables --------------------------------------------- */
  29. static int bit_test; /* see if the line-setting functions work */
  30. module_param(bit_test, int, S_IRUGO);
  31. MODULE_PARM_DESC(bit_test, "lines testing - 0 off; 1 report; 2 fail if stuck");
  32. #ifdef DEBUG
  33. static int i2c_debug = 1;
  34. module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
  35. MODULE_PARM_DESC(i2c_debug,
  36. "debug level - 0 off; 1 normal; 2 verbose; 3 very verbose");
  37. #endif
  38. /* --- setting states on the bus with the right timing: --------------- */
  39. #define setsda(adap, val) adap->setsda(adap->data, val)
  40. #define setscl(adap, val) adap->setscl(adap->data, val)
  41. #define getsda(adap) adap->getsda(adap->data)
  42. #define getscl(adap) adap->getscl(adap->data)
  43. static inline void sdalo(struct i2c_algo_bit_data *adap)
  44. {
  45. setsda(adap, 0);
  46. udelay((adap->udelay + 1) / 2);
  47. }
  48. static inline void sdahi(struct i2c_algo_bit_data *adap)
  49. {
  50. setsda(adap, 1);
  51. udelay((adap->udelay + 1) / 2);
  52. }
  53. static inline void scllo(struct i2c_algo_bit_data *adap)
  54. {
  55. setscl(adap, 0);
  56. udelay(adap->udelay / 2);
  57. }
  58. /*
  59. * Raise scl line, and do checking for delays. This is necessary for slower
  60. * devices.
  61. */
  62. static int sclhi(struct i2c_algo_bit_data *adap)
  63. {
  64. unsigned long start;
  65. setscl(adap, 1);
  66. /* Not all adapters have scl sense line... */
  67. if (!adap->getscl)
  68. goto done;
  69. start = jiffies;
  70. while (!getscl(adap)) {
  71. /* This hw knows how to read the clock line, so we wait
  72. * until it actually gets high. This is safer as some
  73. * chips may hold it low ("clock stretching") while they
  74. * are processing data internally.
  75. */
  76. if (time_after(jiffies, start + adap->timeout)) {
  77. /* Test one last time, as we may have been preempted
  78. * between last check and timeout test.
  79. */
  80. if (getscl(adap))
  81. break;
  82. return -ETIMEDOUT;
  83. }
  84. cpu_relax();
  85. }
  86. #ifdef DEBUG
  87. if (jiffies != start && i2c_debug >= 3)
  88. pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go high\n",
  89. jiffies - start);
  90. #endif
  91. done:
  92. udelay(adap->udelay);
  93. return 0;
  94. }
  95. /* --- other auxiliary functions -------------------------------------- */
  96. static void i2c_start(struct i2c_algo_bit_data *adap)
  97. {
  98. /* assert: scl, sda are high */
  99. setsda(adap, 0);
  100. udelay(adap->udelay);
  101. scllo(adap);
  102. }
  103. static void i2c_repstart(struct i2c_algo_bit_data *adap)
  104. {
  105. /* assert: scl is low */
  106. sdahi(adap);
  107. sclhi(adap);
  108. setsda(adap, 0);
  109. udelay(adap->udelay);
  110. scllo(adap);
  111. }
  112. static void i2c_stop(struct i2c_algo_bit_data *adap)
  113. {
  114. /* assert: scl is low */
  115. sdalo(adap);
  116. sclhi(adap);
  117. setsda(adap, 1);
  118. udelay(adap->udelay);
  119. }
  120. /* send a byte without start cond., look for arbitration,
  121. check ackn. from slave */
  122. /* returns:
  123. * 1 if the device acknowledged
  124. * 0 if the device did not ack
  125. * -ETIMEDOUT if an error occurred (while raising the scl line)
  126. */
  127. static int i2c_outb(struct i2c_adapter *i2c_adap, unsigned char c)
  128. {
  129. int i;
  130. int sb;
  131. int ack;
  132. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  133. /* assert: scl is low */
  134. for (i = 7; i >= 0; i--) {
  135. sb = (c >> i) & 1;
  136. setsda(adap, sb);
  137. udelay((adap->udelay + 1) / 2);
  138. if (sclhi(adap) < 0) { /* timed out */
  139. bit_dbg(1, &i2c_adap->dev,
  140. "i2c_outb: 0x%02x, timeout at bit #%d\n",
  141. (int)c, i);
  142. return -ETIMEDOUT;
  143. }
  144. /* FIXME do arbitration here:
  145. * if (sb && !getsda(adap)) -> ouch! Get out of here.
  146. *
  147. * Report a unique code, so higher level code can retry
  148. * the whole (combined) message and *NOT* issue STOP.
  149. */
  150. scllo(adap);
  151. }
  152. sdahi(adap);
  153. if (sclhi(adap) < 0) { /* timeout */
  154. bit_dbg(1, &i2c_adap->dev,
  155. "i2c_outb: 0x%02x, timeout at ack\n", (int)c);
  156. return -ETIMEDOUT;
  157. }
  158. /* read ack: SDA should be pulled down by slave, or it may
  159. * NAK (usually to report problems with the data we wrote).
  160. */
  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,
  180. "i2c_inb: timeout at bit #%d\n",
  181. 7 - i);
  182. return -ETIMEDOUT;
  183. }
  184. indata *= 2;
  185. if (getsda(adap))
  186. indata |= 0x01;
  187. setscl(adap, 0);
  188. udelay(i == 7 ? adap->udelay / 2 : adap->udelay);
  189. }
  190. /* assert: scl is low */
  191. return indata;
  192. }
  193. /*
  194. * Sanity check for the adapter hardware - check the reaction of
  195. * the bus lines only if it seems to be idle.
  196. */
  197. static int test_bus(struct i2c_adapter *i2c_adap)
  198. {
  199. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  200. const char *name = i2c_adap->name;
  201. int scl, sda, ret;
  202. if (adap->pre_xfer) {
  203. ret = adap->pre_xfer(i2c_adap);
  204. if (ret < 0)
  205. return -ENODEV;
  206. }
  207. if (adap->getscl == NULL)
  208. pr_info("%s: Testing SDA only, SCL is not readable\n", name);
  209. sda = getsda(adap);
  210. scl = (adap->getscl == NULL) ? 1 : getscl(adap);
  211. if (!scl || !sda) {
  212. printk(KERN_WARNING
  213. "%s: bus seems to be busy (scl=%d, sda=%d)\n",
  214. name, scl, sda);
  215. goto bailout;
  216. }
  217. sdalo(adap);
  218. sda = getsda(adap);
  219. scl = (adap->getscl == NULL) ? 1 : getscl(adap);
  220. if (sda) {
  221. printk(KERN_WARNING "%s: SDA stuck high!\n", name);
  222. goto bailout;
  223. }
  224. if (!scl) {
  225. printk(KERN_WARNING
  226. "%s: SCL unexpected low while pulling SDA low!\n",
  227. name);
  228. goto bailout;
  229. }
  230. sdahi(adap);
  231. sda = getsda(adap);
  232. scl = (adap->getscl == NULL) ? 1 : getscl(adap);
  233. if (!sda) {
  234. printk(KERN_WARNING "%s: SDA stuck low!\n", name);
  235. goto bailout;
  236. }
  237. if (!scl) {
  238. printk(KERN_WARNING
  239. "%s: SCL unexpected low while pulling SDA high!\n",
  240. name);
  241. goto bailout;
  242. }
  243. scllo(adap);
  244. sda = getsda(adap);
  245. scl = (adap->getscl == NULL) ? 0 : getscl(adap);
  246. if (scl) {
  247. printk(KERN_WARNING "%s: SCL stuck high!\n", name);
  248. goto bailout;
  249. }
  250. if (!sda) {
  251. printk(KERN_WARNING
  252. "%s: SDA unexpected low while pulling SCL low!\n",
  253. name);
  254. goto bailout;
  255. }
  256. sclhi(adap);
  257. sda = getsda(adap);
  258. scl = (adap->getscl == NULL) ? 1 : getscl(adap);
  259. if (!scl) {
  260. printk(KERN_WARNING "%s: SCL stuck low!\n", name);
  261. goto bailout;
  262. }
  263. if (!sda) {
  264. printk(KERN_WARNING
  265. "%s: SDA unexpected low while pulling SCL high!\n",
  266. name);
  267. goto bailout;
  268. }
  269. if (adap->post_xfer)
  270. adap->post_xfer(i2c_adap);
  271. pr_info("%s: Test OK\n", name);
  272. return 0;
  273. bailout:
  274. sdahi(adap);
  275. sclhi(adap);
  276. if (adap->post_xfer)
  277. adap->post_xfer(i2c_adap);
  278. return -ENODEV;
  279. }
  280. /* ----- Utility functions
  281. */
  282. /* try_address tries to contact a chip for a number of
  283. * times before it gives up.
  284. * return values:
  285. * 1 chip answered
  286. * 0 chip did not answer
  287. * -x transmission error
  288. */
  289. static int try_address(struct i2c_adapter *i2c_adap,
  290. unsigned char addr, int retries)
  291. {
  292. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  293. int i, ret = 0;
  294. for (i = 0; i <= retries; i++) {
  295. ret = i2c_outb(i2c_adap, addr);
  296. if (ret == 1 || i == retries)
  297. break;
  298. bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
  299. i2c_stop(adap);
  300. udelay(adap->udelay);
  301. yield();
  302. bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
  303. i2c_start(adap);
  304. }
  305. if (i && ret)
  306. bit_dbg(1, &i2c_adap->dev,
  307. "Used %d tries to %s client at 0x%02x: %s\n", i + 1,
  308. addr & 1 ? "read from" : "write to", addr >> 1,
  309. ret == 1 ? "success" : "failed, timeout?");
  310. return ret;
  311. }
  312. static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  313. {
  314. const unsigned char *temp = msg->buf;
  315. int count = msg->len;
  316. unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
  317. int retval;
  318. int wrcount = 0;
  319. while (count > 0) {
  320. retval = i2c_outb(i2c_adap, *temp);
  321. /* OK/ACK; or ignored NAK */
  322. if ((retval > 0) || (nak_ok && (retval == 0))) {
  323. count--;
  324. temp++;
  325. wrcount++;
  326. /* A slave NAKing the master means the slave didn't like
  327. * something about the data it saw. For example, maybe
  328. * the SMBus PEC was wrong.
  329. */
  330. } else if (retval == 0) {
  331. dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n");
  332. return -EIO;
  333. /* Timeout; or (someday) lost arbitration
  334. *
  335. * FIXME Lost ARB implies retrying the transaction from
  336. * the first message, after the "winning" master issues
  337. * its STOP. As a rule, upper layer code has no reason
  338. * to know or care about this ... it is *NOT* an error.
  339. */
  340. } else {
  341. dev_err(&i2c_adap->dev, "sendbytes: error %d\n",
  342. retval);
  343. return retval;
  344. }
  345. }
  346. return wrcount;
  347. }
  348. static int acknak(struct i2c_adapter *i2c_adap, int is_ack)
  349. {
  350. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  351. /* assert: sda is high */
  352. if (is_ack) /* send ack */
  353. setsda(adap, 0);
  354. udelay((adap->udelay + 1) / 2);
  355. if (sclhi(adap) < 0) { /* timeout */
  356. dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n");
  357. return -ETIMEDOUT;
  358. }
  359. scllo(adap);
  360. return 0;
  361. }
  362. static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  363. {
  364. int inval;
  365. int rdcount = 0; /* counts bytes read */
  366. unsigned char *temp = msg->buf;
  367. int count = msg->len;
  368. const unsigned flags = msg->flags;
  369. while (count > 0) {
  370. inval = i2c_inb(i2c_adap);
  371. if (inval >= 0) {
  372. *temp = inval;
  373. rdcount++;
  374. } else { /* read timed out */
  375. break;
  376. }
  377. temp++;
  378. count--;
  379. /* Some SMBus transactions require that we receive the
  380. transaction length as the first read byte. */
  381. if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) {
  382. if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) {
  383. if (!(flags & I2C_M_NO_RD_ACK))
  384. acknak(i2c_adap, 0);
  385. dev_err(&i2c_adap->dev,
  386. "readbytes: invalid block length (%d)\n",
  387. inval);
  388. return -EPROTO;
  389. }
  390. /* The original count value accounts for the extra
  391. bytes, that is, either 1 for a regular transaction,
  392. or 2 for a PEC transaction. */
  393. count += inval;
  394. msg->len += inval;
  395. }
  396. bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n",
  397. inval,
  398. (flags & I2C_M_NO_RD_ACK)
  399. ? "(no ack/nak)"
  400. : (count ? "A" : "NA"));
  401. if (!(flags & I2C_M_NO_RD_ACK)) {
  402. inval = acknak(i2c_adap, count);
  403. if (inval < 0)
  404. return inval;
  405. }
  406. }
  407. return rdcount;
  408. }
  409. /* doAddress initiates the transfer by generating the start condition (in
  410. * try_address) and transmits the address in the necessary format to handle
  411. * reads, writes as well as 10bit-addresses.
  412. * returns:
  413. * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
  414. * -x an error occurred (like: -ENXIO if the device did not answer, or
  415. * -ETIMEDOUT, for example if the lines are stuck...)
  416. */
  417. static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  418. {
  419. unsigned short flags = msg->flags;
  420. unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
  421. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  422. unsigned char addr;
  423. int ret, retries;
  424. retries = nak_ok ? 0 : i2c_adap->retries;
  425. if (flags & I2C_M_TEN) {
  426. /* a ten bit address */
  427. addr = 0xf0 | ((msg->addr >> 7) & 0x06);
  428. bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr);
  429. /* try extended address code...*/
  430. ret = try_address(i2c_adap, addr, retries);
  431. if ((ret != 1) && !nak_ok) {
  432. dev_err(&i2c_adap->dev,
  433. "died at extended address code\n");
  434. return -ENXIO;
  435. }
  436. /* the remaining 8 bit address */
  437. ret = i2c_outb(i2c_adap, msg->addr & 0xff);
  438. if ((ret != 1) && !nak_ok) {
  439. /* the chip did not ack / xmission error occurred */
  440. dev_err(&i2c_adap->dev, "died at 2nd address code\n");
  441. return -ENXIO;
  442. }
  443. if (flags & I2C_M_RD) {
  444. bit_dbg(3, &i2c_adap->dev,
  445. "emitting repeated start condition\n");
  446. i2c_repstart(adap);
  447. /* okay, now switch into reading mode */
  448. addr |= 0x01;
  449. ret = try_address(i2c_adap, addr, retries);
  450. if ((ret != 1) && !nak_ok) {
  451. dev_err(&i2c_adap->dev,
  452. "died at repeated address code\n");
  453. return -EIO;
  454. }
  455. }
  456. } else { /* normal 7bit address */
  457. addr = i2c_8bit_addr_from_msg(msg);
  458. if (flags & I2C_M_REV_DIR_ADDR)
  459. addr ^= 1;
  460. ret = try_address(i2c_adap, addr, retries);
  461. if ((ret != 1) && !nak_ok)
  462. return -ENXIO;
  463. }
  464. return 0;
  465. }
  466. static int bit_xfer(struct i2c_adapter *i2c_adap,
  467. struct i2c_msg msgs[], int num)
  468. {
  469. struct i2c_msg *pmsg;
  470. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  471. int i, ret;
  472. unsigned short nak_ok;
  473. if (adap->pre_xfer) {
  474. ret = adap->pre_xfer(i2c_adap);
  475. if (ret < 0)
  476. return ret;
  477. }
  478. bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
  479. i2c_start(adap);
  480. for (i = 0; i < num; i++) {
  481. pmsg = &msgs[i];
  482. nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
  483. if (!(pmsg->flags & I2C_M_NOSTART)) {
  484. if (i) {
  485. if (msgs[i - 1].flags & I2C_M_STOP) {
  486. bit_dbg(3, &i2c_adap->dev,
  487. "emitting enforced stop/start condition\n");
  488. i2c_stop(adap);
  489. i2c_start(adap);
  490. } else {
  491. bit_dbg(3, &i2c_adap->dev,
  492. "emitting repeated start condition\n");
  493. i2c_repstart(adap);
  494. }
  495. }
  496. ret = bit_doAddress(i2c_adap, pmsg);
  497. if ((ret != 0) && !nak_ok) {
  498. bit_dbg(1, &i2c_adap->dev,
  499. "NAK from device addr 0x%02x msg #%d\n",
  500. msgs[i].addr, i);
  501. goto bailout;
  502. }
  503. }
  504. if (pmsg->flags & I2C_M_RD) {
  505. /* read bytes into buffer*/
  506. ret = readbytes(i2c_adap, pmsg);
  507. if (ret >= 1)
  508. bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n",
  509. ret, ret == 1 ? "" : "s");
  510. if (ret < pmsg->len) {
  511. if (ret >= 0)
  512. ret = -EIO;
  513. goto bailout;
  514. }
  515. } else {
  516. /* write bytes from buffer */
  517. ret = sendbytes(i2c_adap, pmsg);
  518. if (ret >= 1)
  519. bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n",
  520. ret, ret == 1 ? "" : "s");
  521. if (ret < pmsg->len) {
  522. if (ret >= 0)
  523. ret = -EIO;
  524. goto bailout;
  525. }
  526. }
  527. }
  528. ret = i;
  529. bailout:
  530. bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
  531. i2c_stop(adap);
  532. if (adap->post_xfer)
  533. adap->post_xfer(i2c_adap);
  534. return ret;
  535. }
  536. /*
  537. * We print a warning when we are not flagged to support atomic transfers but
  538. * will try anyhow. That's what the I2C core would do as well. Sadly, we can't
  539. * modify the algorithm struct at probe time because this struct is exported
  540. * 'const'.
  541. */
  542. static int bit_xfer_atomic(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[],
  543. int num)
  544. {
  545. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  546. if (!adap->can_do_atomic)
  547. dev_warn(&i2c_adap->dev, "not flagged for atomic transfers\n");
  548. return bit_xfer(i2c_adap, msgs, num);
  549. }
  550. static u32 bit_func(struct i2c_adapter *adap)
  551. {
  552. return I2C_FUNC_I2C | I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_EMUL_ALL |
  553. I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  554. }
  555. /* -----exported algorithm data: ------------------------------------- */
  556. const struct i2c_algorithm i2c_bit_algo = {
  557. .master_xfer = bit_xfer,
  558. .master_xfer_atomic = bit_xfer_atomic,
  559. .functionality = bit_func,
  560. };
  561. EXPORT_SYMBOL(i2c_bit_algo);
  562. static const struct i2c_adapter_quirks i2c_bit_quirk_no_clk_stretch = {
  563. .flags = I2C_AQ_NO_CLK_STRETCH,
  564. };
  565. /*
  566. * registering functions to load algorithms at runtime
  567. */
  568. static int __i2c_bit_add_bus(struct i2c_adapter *adap,
  569. int (*add_adapter)(struct i2c_adapter *))
  570. {
  571. struct i2c_algo_bit_data *bit_adap = adap->algo_data;
  572. int ret;
  573. if (bit_test) {
  574. ret = test_bus(adap);
  575. if (bit_test >= 2 && ret < 0)
  576. return -ENODEV;
  577. }
  578. /* register new adapter to i2c module... */
  579. adap->algo = &i2c_bit_algo;
  580. adap->retries = 3;
  581. if (bit_adap->getscl == NULL)
  582. adap->quirks = &i2c_bit_quirk_no_clk_stretch;
  583. /*
  584. * We tried forcing SCL/SDA to an initial state here. But that caused a
  585. * regression, sadly. Check Bugzilla #200045 for details.
  586. */
  587. ret = add_adapter(adap);
  588. if (ret < 0)
  589. return ret;
  590. /* Complain if SCL can't be read */
  591. if (bit_adap->getscl == NULL) {
  592. dev_warn(&adap->dev, "Not I2C compliant: can't read SCL\n");
  593. dev_warn(&adap->dev, "Bus may be unreliable\n");
  594. }
  595. return 0;
  596. }
  597. int i2c_bit_add_bus(struct i2c_adapter *adap)
  598. {
  599. return __i2c_bit_add_bus(adap, i2c_add_adapter);
  600. }
  601. EXPORT_SYMBOL(i2c_bit_add_bus);
  602. int i2c_bit_add_numbered_bus(struct i2c_adapter *adap)
  603. {
  604. return __i2c_bit_add_bus(adap, i2c_add_numbered_adapter);
  605. }
  606. EXPORT_SYMBOL(i2c_bit_add_numbered_bus);
  607. MODULE_AUTHOR("Simon G. Vogl <[email protected]>");
  608. MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
  609. MODULE_LICENSE("GPL");