i2c-algo-bit.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 <frodol@dds.nl>, Kyösti Mälkki
  8. * <kmalkki@cc.hut.fi> and Jean Delvare <jdelvare@suse.de>
  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. * Always report ACK if SDA is write-only.
  161. */
  162. ack = !adap->getsda || !getsda(adap); /* ack: sda is pulled low -> success */
  163. bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c,
  164. ack ? "A" : "NA");
  165. scllo(adap);
  166. return ack;
  167. /* assert: scl is low (sda undef) */
  168. }
  169. static int i2c_inb(struct i2c_adapter *i2c_adap)
  170. {
  171. /* read byte via i2c port, without start/stop sequence */
  172. /* acknowledge is sent in i2c_read. */
  173. int i;
  174. unsigned char indata = 0;
  175. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  176. /* assert: scl is low */
  177. sdahi(adap);
  178. for (i = 0; i < 8; i++) {
  179. if (sclhi(adap) < 0) { /* timeout */
  180. bit_dbg(1, &i2c_adap->dev,
  181. "i2c_inb: timeout at bit #%d\n",
  182. 7 - i);
  183. return -ETIMEDOUT;
  184. }
  185. indata *= 2;
  186. if (getsda(adap))
  187. indata |= 0x01;
  188. setscl(adap, 0);
  189. udelay(i == 7 ? adap->udelay / 2 : adap->udelay);
  190. }
  191. /* assert: scl is low */
  192. return indata;
  193. }
  194. /*
  195. * Sanity check for the adapter hardware - check the reaction of
  196. * the bus lines only if it seems to be idle.
  197. */
  198. static int test_bus(struct i2c_adapter *i2c_adap)
  199. {
  200. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  201. const char *name = i2c_adap->name;
  202. int scl, sda, ret;
  203. if (adap->pre_xfer) {
  204. ret = adap->pre_xfer(i2c_adap);
  205. if (ret < 0)
  206. return -ENODEV;
  207. }
  208. if (adap->getsda == NULL)
  209. pr_info("%s: SDA is write-only, testing not possible\n", name);
  210. if (adap->getscl == NULL)
  211. pr_info("%s: SCL is write-only, testing not possible\n", name);
  212. sda = adap->getsda ? getsda(adap) : 1;
  213. scl = adap->getscl ? getscl(adap) : 1;
  214. if (!scl || !sda) {
  215. pr_warn("%s: bus seems to be busy (scl=%d, sda=%d)\n", name, scl, sda);
  216. goto bailout;
  217. }
  218. sdalo(adap);
  219. if (adap->getsda && getsda(adap)) {
  220. pr_warn("%s: SDA stuck high!\n", name);
  221. goto bailout;
  222. }
  223. if (adap->getscl && !getscl(adap)) {
  224. pr_warn("%s: SCL unexpected low while pulling SDA low!\n", name);
  225. goto bailout;
  226. }
  227. sdahi(adap);
  228. if (adap->getsda && !getsda(adap)) {
  229. pr_warn("%s: SDA stuck low!\n", name);
  230. goto bailout;
  231. }
  232. if (adap->getscl && !getscl(adap)) {
  233. pr_warn("%s: SCL unexpected low while pulling SDA high!\n", name);
  234. goto bailout;
  235. }
  236. scllo(adap);
  237. if (adap->getscl && getscl(adap)) {
  238. pr_warn("%s: SCL stuck high!\n", name);
  239. goto bailout;
  240. }
  241. if (adap->getsda && !getsda(adap)) {
  242. pr_warn("%s: SDA unexpected low while pulling SCL low!\n", name);
  243. goto bailout;
  244. }
  245. sclhi(adap);
  246. if (adap->getscl && !getscl(adap)) {
  247. pr_warn("%s: SCL stuck low!\n", name);
  248. goto bailout;
  249. }
  250. if (adap->getsda && !getsda(adap)) {
  251. pr_warn("%s: SDA unexpected low while pulling SCL high!\n", name);
  252. goto bailout;
  253. }
  254. if (adap->post_xfer)
  255. adap->post_xfer(i2c_adap);
  256. pr_info("%s: Test OK\n", name);
  257. return 0;
  258. bailout:
  259. sdahi(adap);
  260. sclhi(adap);
  261. if (adap->post_xfer)
  262. adap->post_xfer(i2c_adap);
  263. return -ENODEV;
  264. }
  265. /* ----- Utility functions
  266. */
  267. /* try_address tries to contact a chip for a number of
  268. * times before it gives up.
  269. * return values:
  270. * 1 chip answered
  271. * 0 chip did not answer
  272. * -x transmission error
  273. */
  274. static int try_address(struct i2c_adapter *i2c_adap,
  275. unsigned char addr, int retries)
  276. {
  277. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  278. int i, ret = 0;
  279. for (i = 0; i <= retries; i++) {
  280. ret = i2c_outb(i2c_adap, addr);
  281. if (ret == 1 || i == retries)
  282. break;
  283. bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
  284. i2c_stop(adap);
  285. udelay(adap->udelay);
  286. yield();
  287. bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
  288. i2c_start(adap);
  289. }
  290. if (i && ret)
  291. bit_dbg(1, &i2c_adap->dev,
  292. "Used %d tries to %s client at 0x%02x: %s\n", i + 1,
  293. addr & 1 ? "read from" : "write to", addr >> 1,
  294. ret == 1 ? "success" : "failed, timeout?");
  295. return ret;
  296. }
  297. static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  298. {
  299. const unsigned char *temp = msg->buf;
  300. int count = msg->len;
  301. unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
  302. int retval;
  303. int wrcount = 0;
  304. while (count > 0) {
  305. retval = i2c_outb(i2c_adap, *temp);
  306. /* OK/ACK; or ignored NAK */
  307. if ((retval > 0) || (nak_ok && (retval == 0))) {
  308. count--;
  309. temp++;
  310. wrcount++;
  311. /* A slave NAKing the master means the slave didn't like
  312. * something about the data it saw. For example, maybe
  313. * the SMBus PEC was wrong.
  314. */
  315. } else if (retval == 0) {
  316. dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n");
  317. return -EIO;
  318. /* Timeout; or (someday) lost arbitration
  319. *
  320. * FIXME Lost ARB implies retrying the transaction from
  321. * the first message, after the "winning" master issues
  322. * its STOP. As a rule, upper layer code has no reason
  323. * to know or care about this ... it is *NOT* an error.
  324. */
  325. } else {
  326. dev_err(&i2c_adap->dev, "sendbytes: error %d\n",
  327. retval);
  328. return retval;
  329. }
  330. }
  331. return wrcount;
  332. }
  333. static int acknak(struct i2c_adapter *i2c_adap, int is_ack)
  334. {
  335. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  336. /* assert: sda is high */
  337. if (is_ack) /* send ack */
  338. setsda(adap, 0);
  339. udelay((adap->udelay + 1) / 2);
  340. if (sclhi(adap) < 0) { /* timeout */
  341. dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n");
  342. return -ETIMEDOUT;
  343. }
  344. scllo(adap);
  345. return 0;
  346. }
  347. static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  348. {
  349. int inval;
  350. int rdcount = 0; /* counts bytes read */
  351. unsigned char *temp = msg->buf;
  352. int count = msg->len;
  353. const unsigned flags = msg->flags;
  354. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  355. if (!adap->getsda)
  356. return -EOPNOTSUPP;
  357. while (count > 0) {
  358. inval = i2c_inb(i2c_adap);
  359. if (inval >= 0) {
  360. *temp = inval;
  361. rdcount++;
  362. } else { /* read timed out */
  363. break;
  364. }
  365. temp++;
  366. count--;
  367. /* Some SMBus transactions require that we receive the
  368. transaction length as the first read byte. */
  369. if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) {
  370. if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) {
  371. if (!(flags & I2C_M_NO_RD_ACK))
  372. acknak(i2c_adap, 0);
  373. dev_err(&i2c_adap->dev,
  374. "readbytes: invalid block length (%d)\n",
  375. inval);
  376. return -EPROTO;
  377. }
  378. /* The original count value accounts for the extra
  379. bytes, that is, either 1 for a regular transaction,
  380. or 2 for a PEC transaction. */
  381. count += inval;
  382. msg->len += inval;
  383. }
  384. bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n",
  385. inval,
  386. (flags & I2C_M_NO_RD_ACK)
  387. ? "(no ack/nak)"
  388. : (count ? "A" : "NA"));
  389. if (!(flags & I2C_M_NO_RD_ACK)) {
  390. inval = acknak(i2c_adap, count);
  391. if (inval < 0)
  392. return inval;
  393. }
  394. }
  395. return rdcount;
  396. }
  397. /* doAddress initiates the transfer by generating the start condition (in
  398. * try_address) and transmits the address in the necessary format to handle
  399. * reads, writes as well as 10bit-addresses.
  400. * returns:
  401. * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
  402. * -x an error occurred (like: -ENXIO if the device did not answer, or
  403. * -ETIMEDOUT, for example if the lines are stuck...)
  404. */
  405. static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  406. {
  407. unsigned short flags = msg->flags;
  408. unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
  409. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  410. unsigned char addr;
  411. int ret, retries;
  412. retries = nak_ok ? 0 : i2c_adap->retries;
  413. if (flags & I2C_M_TEN) {
  414. /* a ten bit address */
  415. addr = 0xf0 | ((msg->addr >> 7) & 0x06);
  416. bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr);
  417. /* try extended address code...*/
  418. ret = try_address(i2c_adap, addr, retries);
  419. if ((ret != 1) && !nak_ok) {
  420. dev_err(&i2c_adap->dev,
  421. "died at extended address code\n");
  422. return -ENXIO;
  423. }
  424. /* the remaining 8 bit address */
  425. ret = i2c_outb(i2c_adap, msg->addr & 0xff);
  426. if ((ret != 1) && !nak_ok) {
  427. /* the chip did not ack / xmission error occurred */
  428. dev_err(&i2c_adap->dev, "died at 2nd address code\n");
  429. return -ENXIO;
  430. }
  431. if (flags & I2C_M_RD) {
  432. bit_dbg(3, &i2c_adap->dev,
  433. "emitting repeated start condition\n");
  434. i2c_repstart(adap);
  435. /* okay, now switch into reading mode */
  436. addr |= 0x01;
  437. ret = try_address(i2c_adap, addr, retries);
  438. if ((ret != 1) && !nak_ok) {
  439. dev_err(&i2c_adap->dev,
  440. "died at repeated address code\n");
  441. return -EIO;
  442. }
  443. }
  444. } else { /* normal 7bit address */
  445. addr = i2c_8bit_addr_from_msg(msg);
  446. if (flags & I2C_M_REV_DIR_ADDR)
  447. addr ^= 1;
  448. ret = try_address(i2c_adap, addr, retries);
  449. if ((ret != 1) && !nak_ok)
  450. return -ENXIO;
  451. }
  452. return 0;
  453. }
  454. static int bit_xfer(struct i2c_adapter *i2c_adap,
  455. struct i2c_msg msgs[], int num)
  456. {
  457. struct i2c_msg *pmsg;
  458. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  459. int i, ret;
  460. unsigned short nak_ok;
  461. if (adap->pre_xfer) {
  462. ret = adap->pre_xfer(i2c_adap);
  463. if (ret < 0)
  464. return ret;
  465. }
  466. bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
  467. i2c_start(adap);
  468. for (i = 0; i < num; i++) {
  469. pmsg = &msgs[i];
  470. nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
  471. if (!(pmsg->flags & I2C_M_NOSTART)) {
  472. if (i) {
  473. if (msgs[i - 1].flags & I2C_M_STOP) {
  474. bit_dbg(3, &i2c_adap->dev,
  475. "emitting enforced stop/start condition\n");
  476. i2c_stop(adap);
  477. i2c_start(adap);
  478. } else {
  479. bit_dbg(3, &i2c_adap->dev,
  480. "emitting repeated start condition\n");
  481. i2c_repstart(adap);
  482. }
  483. }
  484. ret = bit_doAddress(i2c_adap, pmsg);
  485. if ((ret != 0) && !nak_ok) {
  486. bit_dbg(1, &i2c_adap->dev,
  487. "NAK from device addr 0x%02x msg #%d\n",
  488. msgs[i].addr, i);
  489. goto bailout;
  490. }
  491. }
  492. if (pmsg->flags & I2C_M_RD) {
  493. /* read bytes into buffer*/
  494. ret = readbytes(i2c_adap, pmsg);
  495. if (ret >= 1)
  496. bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n",
  497. ret, ret == 1 ? "" : "s");
  498. if (ret < pmsg->len) {
  499. if (ret >= 0)
  500. ret = -EIO;
  501. goto bailout;
  502. }
  503. } else {
  504. /* write bytes from buffer */
  505. ret = sendbytes(i2c_adap, pmsg);
  506. if (ret >= 1)
  507. bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n",
  508. ret, ret == 1 ? "" : "s");
  509. if (ret < pmsg->len) {
  510. if (ret >= 0)
  511. ret = -EIO;
  512. goto bailout;
  513. }
  514. }
  515. }
  516. ret = i;
  517. bailout:
  518. bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
  519. i2c_stop(adap);
  520. if (adap->post_xfer)
  521. adap->post_xfer(i2c_adap);
  522. return ret;
  523. }
  524. /*
  525. * We print a warning when we are not flagged to support atomic transfers but
  526. * will try anyhow. That's what the I2C core would do as well. Sadly, we can't
  527. * modify the algorithm struct at probe time because this struct is exported
  528. * 'const'.
  529. */
  530. static int bit_xfer_atomic(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[],
  531. int num)
  532. {
  533. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  534. if (!adap->can_do_atomic)
  535. dev_warn(&i2c_adap->dev, "not flagged for atomic transfers\n");
  536. return bit_xfer(i2c_adap, msgs, num);
  537. }
  538. static u32 bit_func(struct i2c_adapter *adap)
  539. {
  540. return I2C_FUNC_I2C | I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_EMUL_ALL |
  541. I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  542. }
  543. /* -----exported algorithm data: ------------------------------------- */
  544. const struct i2c_algorithm i2c_bit_algo = {
  545. .master_xfer = bit_xfer,
  546. .master_xfer_atomic = bit_xfer_atomic,
  547. .functionality = bit_func,
  548. };
  549. EXPORT_SYMBOL(i2c_bit_algo);
  550. static const struct i2c_adapter_quirks i2c_bit_quirk_no_clk_stretch = {
  551. .flags = I2C_AQ_NO_CLK_STRETCH,
  552. };
  553. /*
  554. * registering functions to load algorithms at runtime
  555. */
  556. static int __i2c_bit_add_bus(struct i2c_adapter *adap,
  557. int (*add_adapter)(struct i2c_adapter *))
  558. {
  559. struct i2c_algo_bit_data *bit_adap = adap->algo_data;
  560. int ret;
  561. if (bit_test) {
  562. ret = test_bus(adap);
  563. if (bit_test >= 2 && ret < 0)
  564. return -ENODEV;
  565. }
  566. /* register new adapter to i2c module... */
  567. adap->algo = &i2c_bit_algo;
  568. adap->retries = 3;
  569. if (bit_adap->getscl == NULL)
  570. adap->quirks = &i2c_bit_quirk_no_clk_stretch;
  571. /*
  572. * We tried forcing SCL/SDA to an initial state here. But that caused a
  573. * regression, sadly. Check Bugzilla #200045 for details.
  574. */
  575. ret = add_adapter(adap);
  576. if (ret < 0)
  577. return ret;
  578. if (bit_adap->getsda == NULL)
  579. dev_warn(&adap->dev, "Not I2C compliant: can't read SDA\n");
  580. if (bit_adap->getscl == NULL)
  581. dev_warn(&adap->dev, "Not I2C compliant: can't read SCL\n");
  582. if (bit_adap->getsda == NULL || bit_adap->getscl == NULL)
  583. dev_warn(&adap->dev, "Bus may be unreliable\n");
  584. return 0;
  585. }
  586. int i2c_bit_add_bus(struct i2c_adapter *adap)
  587. {
  588. return __i2c_bit_add_bus(adap, i2c_add_adapter);
  589. }
  590. EXPORT_SYMBOL(i2c_bit_add_bus);
  591. int i2c_bit_add_numbered_bus(struct i2c_adapter *adap)
  592. {
  593. return __i2c_bit_add_bus(adap, i2c_add_numbered_adapter);
  594. }
  595. EXPORT_SYMBOL(i2c_bit_add_numbered_bus);
  596. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  597. MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
  598. MODULE_LICENSE("GPL");