i2c-gpio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. #include <string.h>
  2. #include "FreeRTOS.h"
  3. #include "chip.h"
  4. #include "board.h"
  5. #include "errno.h"
  6. #include "timer.h"
  7. struct i2c_gpio_private_data {
  8. struct i2c_adapter adap;
  9. struct i2c_algo_bit_data bit_data;
  10. struct i2c_gpio_platform_data pdata;
  11. };
  12. /* Toggle SDA by changing the direction of the pin */
  13. static void i2c_gpio_setsda_dir(void *data, int state)
  14. {
  15. struct i2c_gpio_platform_data *pdata = data;
  16. if (state)
  17. gpio_direction_input(pdata->sda_pin);
  18. else
  19. gpio_direction_output(pdata->sda_pin, 0);
  20. }
  21. /*
  22. * Toggle SDA by changing the output value of the pin. This is only
  23. * valid for pins configured as open drain (i.e. setting the value
  24. * high effectively turns off the output driver.)
  25. */
  26. static void i2c_gpio_setsda_val(void *data, int state)
  27. {
  28. struct i2c_gpio_platform_data *pdata = data;
  29. gpio_direction_output(pdata->sda_pin, state);
  30. gpio_set_value(pdata->sda_pin, state);
  31. }
  32. /* Toggle SCL by changing the direction of the pin. */
  33. static void i2c_gpio_setscl_dir(void *data, int state)
  34. {
  35. struct i2c_gpio_platform_data *pdata = data;
  36. if (state)
  37. gpio_direction_input(pdata->scl_pin);
  38. else
  39. gpio_direction_output(pdata->scl_pin, 0);
  40. }
  41. /*
  42. * Toggle SCL by changing the output value of the pin. This is used
  43. * for pins that are configured as open drain and for output-only
  44. * pins. The latter case will break the i2c protocol, but it will
  45. * often work in practice.
  46. */
  47. static void i2c_gpio_setscl_val(void *data, int state)
  48. {
  49. struct i2c_gpio_platform_data *pdata = data;
  50. gpio_direction_output(pdata->scl_pin, state);
  51. gpio_set_value(pdata->scl_pin, state);
  52. }
  53. static int i2c_gpio_getsda(void *data)
  54. {
  55. struct i2c_gpio_platform_data *pdata = data;
  56. gpio_direction_input(pdata->sda_pin);
  57. return gpio_get_value(pdata->sda_pin);
  58. }
  59. static int i2c_gpio_getscl(void *data)
  60. {
  61. struct i2c_gpio_platform_data *pdata = data;
  62. gpio_direction_input(pdata->scl_pin);
  63. return gpio_get_value(pdata->scl_pin);
  64. }
  65. /* --- setting states on the bus with the right timing: --------------- */
  66. #define setsda(adap, val) adap->setsda(adap->data, val)
  67. #define setscl(adap, val) adap->setscl(adap->data, val)
  68. #define getsda(adap) adap->getsda(adap->data)
  69. #define getscl(adap) adap->getscl(adap->data)
  70. static __INLINE void sdalo(struct i2c_algo_bit_data *adap)
  71. {
  72. setsda(adap, 0);
  73. udelay((adap->udelay + 1) / 2);
  74. }
  75. static __INLINE void sdahi(struct i2c_algo_bit_data *adap)
  76. {
  77. setsda(adap, 1);
  78. udelay((adap->udelay + 1) / 2);
  79. }
  80. static __INLINE void scllo(struct i2c_algo_bit_data *adap)
  81. {
  82. setscl(adap, 0);
  83. udelay(adap->udelay / 2);
  84. }
  85. /*
  86. * Raise scl line, and do checking for delays. This is necessary for slower
  87. * devices.
  88. */
  89. static int sclhi(struct i2c_algo_bit_data *adap)
  90. {
  91. unsigned long start;
  92. setscl(adap, 1);
  93. /* Not all adapters have scl sense line... */
  94. if (!adap->getscl)
  95. goto done;
  96. start = xTaskGetTickCount();
  97. while (!getscl(adap)) {
  98. /* This hw knows how to read the clock line, so we wait
  99. * until it actually gets high. This is safer as some
  100. * chips may hold it low ("clock stretching") while they
  101. * are processing data internally.
  102. */
  103. if (xTaskGetTickCount() > start + adap->timeout) {
  104. /* Test one last time, as we may have been preempted
  105. * between last check and timeout test.
  106. */
  107. if (getscl(adap))
  108. break;
  109. return -ETIMEDOUT;
  110. }
  111. taskYIELD();
  112. }
  113. done:
  114. udelay(adap->udelay);
  115. return 0;
  116. }
  117. /* --- other auxiliary functions -------------------------------------- */
  118. static void i2c_start(struct i2c_algo_bit_data *adap)
  119. {
  120. /* assert: scl, sda are high */
  121. setsda(adap, 0);
  122. udelay(adap->udelay);
  123. scllo(adap);
  124. }
  125. static void i2c_repstart(struct i2c_algo_bit_data *adap)
  126. {
  127. /* assert: scl is low */
  128. sdahi(adap);
  129. sclhi(adap);
  130. setsda(adap, 0);
  131. udelay(adap->udelay);
  132. scllo(adap);
  133. }
  134. static void i2c_stop(struct i2c_algo_bit_data *adap)
  135. {
  136. /* assert: scl is low */
  137. sdalo(adap);
  138. sclhi(adap);
  139. setsda(adap, 1);
  140. udelay(adap->udelay);
  141. }
  142. /* send a byte without start cond., look for arbitration,
  143. check ackn. from slave */
  144. /* returns:
  145. * 1 if the device acknowledged
  146. * 0 if the device did not ack
  147. * -ETIMEDOUT if an error occurred (while raising the scl line)
  148. */
  149. static int i2c_outb(struct i2c_adapter *i2c_adap, unsigned char c)
  150. {
  151. int i;
  152. int sb;
  153. int ack;
  154. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  155. /* assert: scl is low */
  156. for (i = 7; i >= 0; i--) {
  157. sb = (c >> i) & 1;
  158. setsda(adap, sb);
  159. udelay((adap->udelay + 1) / 2);
  160. if (sclhi(adap) < 0) { /* timed out */
  161. TRACE_DEBUG("i2c_outb: 0x%02x, timeout at bit #%d\n", (int)c, i);
  162. return -ETIMEDOUT;
  163. }
  164. /* FIXME do arbitration here:
  165. * if (sb && !getsda(adap)) -> ouch! Get out of here.
  166. *
  167. * Report a unique code, so higher level code can retry
  168. * the whole (combined) message and *NOT* issue STOP.
  169. */
  170. scllo(adap);
  171. }
  172. sdahi(adap); //---
  173. //sdalo(adap); //+++
  174. if (sclhi(adap) < 0) { /* timeout */
  175. TRACE_DEBUG("i2c_outb: 0x%02x, timeout at ack\n", (int)c);
  176. return -ETIMEDOUT;
  177. }
  178. /* read ack: SDA should be pulled down by slave, or it may
  179. * NAK (usually to report problems with the data we wrote).
  180. */
  181. ack = !getsda(adap); /* ack: sda is pulled low -> success */
  182. TRACE_DEBUG("i2c_outb: 0x%02x %s\n", (int)c, ack ? "A" : "NA");
  183. scllo(adap);
  184. //sdalo(adap); //+++
  185. return ack;
  186. /* assert: scl is low (sda undef) */
  187. }
  188. static int i2c_inb(struct i2c_adapter *i2c_adap)
  189. {
  190. /* read byte via i2c port, without start/stop sequence */
  191. /* acknowledge is sent in i2c_read. */
  192. int i;
  193. unsigned char indata = 0;
  194. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  195. /* assert: scl is low */
  196. sdahi(adap); //---
  197. for (i = 0; i < 8; i++) {
  198. if (sclhi(adap) < 0) { /* timeout */
  199. TRACE_DEBUG("i2c_inb: timeout at bit #%d\n", 7 - i);
  200. return -ETIMEDOUT;
  201. }
  202. indata *= 2;
  203. if (getsda(adap))
  204. indata |= 0x01;
  205. setscl(adap, 0);
  206. udelay(i == 7 ? adap->udelay / 2 : adap->udelay);
  207. }
  208. /* assert: scl is low */
  209. return indata;
  210. }
  211. /* try_address tries to contact a chip for a number of
  212. * times before it gives up.
  213. * return values:
  214. * 1 chip answered
  215. * 0 chip did not answer
  216. * -x transmission error
  217. */
  218. static int try_address(struct i2c_adapter *i2c_adap,
  219. unsigned char addr, int retries)
  220. {
  221. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  222. int i, ret = 0;
  223. for (i = 0; i <= retries; i++) {
  224. ret = i2c_outb(i2c_adap, addr);
  225. if (ret == 1 || i == retries)
  226. break;
  227. TRACE_DEBUG("emitting stop condition\n");
  228. i2c_stop(adap);
  229. udelay(adap->udelay);
  230. taskYIELD();
  231. TRACE_DEBUG("emitting start condition\n");
  232. i2c_start(adap);
  233. }
  234. if (i && ret)
  235. TRACE_DEBUG("Used %d tries to %s client at "
  236. "0x%02x: %s\n", i + 1,
  237. addr & 1 ? "read from" : "write to", addr >> 1,
  238. ret == 1 ? "success" : "failed, timeout?");
  239. return ret;
  240. }
  241. static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  242. {
  243. const unsigned char *temp = msg->buf;
  244. int count = msg->len;
  245. unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
  246. int retval;
  247. int wrcount = 0;
  248. while (count > 0) {
  249. retval = i2c_outb(i2c_adap, *temp);
  250. /* OK/ACK; or ignored NAK */
  251. if ((retval > 0) || (nak_ok && (retval == 0))) {
  252. count--;
  253. temp++;
  254. wrcount++;
  255. /* A slave NAKing the master means the slave didn't like
  256. * something about the data it saw. For example, maybe
  257. * the SMBus PEC was wrong.
  258. */
  259. } else if (retval == 0) {
  260. TRACE_ERROR("sendbytes: NAK bailout.\n");
  261. return -EIO;
  262. /* Timeout; or (someday) lost arbitration
  263. *
  264. * FIXME Lost ARB implies retrying the transaction from
  265. * the first message, after the "winning" master issues
  266. * its STOP. As a rule, upper layer code has no reason
  267. * to know or care about this ... it is *NOT* an error.
  268. */
  269. } else {
  270. TRACE_ERROR("sendbytes: error %d\n", retval);
  271. return retval;
  272. }
  273. }
  274. return wrcount;
  275. }
  276. static int acknak(struct i2c_adapter *i2c_adap, int is_ack)
  277. {
  278. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  279. /* assert: sda is high */
  280. if (is_ack) /* send ack */
  281. setsda(adap, 0);
  282. udelay((adap->udelay + 1) / 2);
  283. if (sclhi(adap) < 0) { /* timeout */
  284. TRACE_ERROR("readbytes: ack/nak timeout\n");
  285. return -ETIMEDOUT;
  286. }
  287. scllo(adap);
  288. return 0;
  289. }
  290. static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  291. {
  292. int inval;
  293. int rdcount = 0; /* counts bytes read */
  294. unsigned char *temp = msg->buf;
  295. int count = msg->len;
  296. const unsigned flags = msg->flags;
  297. while (count > 0) {
  298. inval = i2c_inb(i2c_adap);
  299. if (inval >= 0) {
  300. *temp = inval;
  301. rdcount++;
  302. } else { /* read timed out */
  303. break;
  304. }
  305. temp++;
  306. count--;
  307. /* Some SMBus transactions require that we receive the
  308. transaction length as the first read byte. */
  309. if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) {
  310. if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) {
  311. if (!(flags & I2C_M_NO_RD_ACK))
  312. acknak(i2c_adap, 0);
  313. TRACE_ERROR("readbytes: invalid block length (%d)\n", inval);
  314. return -EPROTO;
  315. }
  316. /* The original count value accounts for the extra
  317. bytes, that is, either 1 for a regular transaction,
  318. or 2 for a PEC transaction. */
  319. count += inval;
  320. msg->len += inval;
  321. }
  322. TRACE_DEBUG( "readbytes: 0x%02x %s\n",
  323. inval,
  324. (flags & I2C_M_NO_RD_ACK)
  325. ? "(no ack/nak)"
  326. : (count ? "A" : "NA"));
  327. if (!(flags & I2C_M_NO_RD_ACK)) {
  328. inval = acknak(i2c_adap, count);
  329. if (inval < 0)
  330. return inval;
  331. }
  332. }
  333. return rdcount;
  334. }
  335. /* doAddress initiates the transfer by generating the start condition (in
  336. * try_address) and transmits the address in the necessary format to handle
  337. * reads, writes as well as 10bit-addresses.
  338. * returns:
  339. * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
  340. * -x an error occurred (like: -ENXIO if the device did not answer, or
  341. * -ETIMEDOUT, for example if the lines are stuck...)
  342. */
  343. static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
  344. {
  345. unsigned short flags = msg->flags;
  346. unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
  347. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  348. unsigned char addr;
  349. int ret, retries;
  350. retries = nak_ok ? 0 : i2c_adap->retries;
  351. if (flags & I2C_M_TEN) {
  352. /* a ten bit address */
  353. addr = 0xf0 | ((msg->addr >> 7) & 0x06);
  354. TRACE_DEBUG("addr0: %d\n", addr);
  355. /* try extended address code...*/
  356. ret = try_address(i2c_adap, addr, retries);
  357. if ((ret != 1) && !nak_ok) {
  358. TRACE_ERROR("died at extended address code\n");
  359. return -ENXIO;
  360. }
  361. /* the remaining 8 bit address */
  362. ret = i2c_outb(i2c_adap, msg->addr & 0xff);
  363. if ((ret != 1) && !nak_ok) {
  364. /* the chip did not ack / xmission error occurred */
  365. TRACE_ERROR("died at 2nd address code\n");
  366. return -ENXIO;
  367. }
  368. if (flags & I2C_M_RD) {
  369. TRACE_DEBUG("emitting repeated start condition\n");
  370. i2c_repstart(adap);
  371. /* okay, now switch into reading mode */
  372. addr |= 0x01;
  373. ret = try_address(i2c_adap, addr, retries);
  374. if ((ret != 1) && !nak_ok) {
  375. TRACE_ERROR("died at repeated address code\n");
  376. return -EIO;
  377. }
  378. }
  379. } else { /* normal 7bit address */
  380. addr = msg->addr << 1;
  381. if (flags & I2C_M_RD)
  382. addr |= 1;
  383. if (flags & I2C_M_REV_DIR_ADDR)
  384. addr ^= 1;
  385. ret = try_address(i2c_adap, addr, retries);
  386. if ((ret != 1) && !nak_ok)
  387. return -ENXIO;
  388. }
  389. return 0;
  390. }
  391. static int bit_xfer(struct i2c_adapter *i2c_adap,
  392. struct i2c_msg msgs[], int num)
  393. {
  394. struct i2c_msg *pmsg;
  395. struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  396. int i, ret;
  397. unsigned short nak_ok;
  398. TRACE_DEBUG("emitting start condition\n");
  399. i2c_start(adap);
  400. for (i = 0; i < num; i++) {
  401. pmsg = &msgs[i];
  402. nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
  403. if (!(pmsg->flags & I2C_M_NOSTART)) {
  404. if (i) {
  405. if (msgs[i - 1].flags & I2C_M_STOP) {
  406. TRACE_DEBUG("emitting enforced stop/start condition\n");
  407. i2c_stop(adap);
  408. i2c_start(adap);
  409. } else {
  410. TRACE_DEBUG("emitting repeated start condition\n");
  411. i2c_repstart(adap);
  412. }
  413. }
  414. ret = bit_doAddress(i2c_adap, pmsg);
  415. if ((ret != 0) && !nak_ok) {
  416. TRACE_DEBUG("NAK from device addr 0x%02x msg #%d\n", msgs[i].addr, i);
  417. goto bailout;
  418. }
  419. }
  420. if (pmsg->flags & I2C_M_RD) {
  421. /* read bytes into buffer*/
  422. ret = readbytes(i2c_adap, pmsg);
  423. if (ret >= 1)
  424. TRACE_DEBUG("read %d byte%s\n", ret, ret == 1 ? "" : "s");
  425. if (ret < pmsg->len) {
  426. if (ret >= 0)
  427. ret = -EIO;
  428. goto bailout;
  429. }
  430. } else {
  431. /* write bytes from buffer */
  432. ret = sendbytes(i2c_adap, pmsg);
  433. if (ret >= 1)
  434. TRACE_DEBUG("wrote %d byte%s\n", ret, ret == 1 ? "" : "s");
  435. if (ret < pmsg->len) {
  436. if (ret >= 0)
  437. ret = -EIO;
  438. goto bailout;
  439. }
  440. }
  441. }
  442. ret = i;
  443. bailout:
  444. TRACE_DEBUG("emitting stop condition\n");
  445. i2c_stop(adap);
  446. return ret;
  447. }
  448. struct i2c_gpio_platform_data i2c_gpio0 = {
  449. .devid = 0,
  450. .sda_pin = I2C_GPIO0_SDA_PIN,
  451. .scl_pin = I2C_GPIO0_SCL_PIN,
  452. .udelay = 5, /* clk freq 500/udelay kHz */
  453. .timeout = configTICK_RATE_HZ / 10, /* 100ms */
  454. .sda_is_open_drain = 0,
  455. .scl_is_open_drain = 0,
  456. .scl_is_output_only = 1,
  457. };
  458. const struct i2c_algorithm i2c_bit_algo = {
  459. .master_xfer = bit_xfer,
  460. };
  461. int i2c_bit_add_bus(struct i2c_adapter *adap)
  462. {
  463. int ret;
  464. /* register new adapter to i2c module... */
  465. adap->algo = &i2c_bit_algo;
  466. adap->retries = 3;
  467. ret = i2c_add_adapter(adap);
  468. if (ret < 0)
  469. return ret;
  470. return 0;
  471. }
  472. static int i2c_gpio_add_device(struct i2c_gpio_platform_data *pdevdata)
  473. {
  474. struct i2c_gpio_private_data *priv;
  475. struct i2c_gpio_platform_data *pdata;
  476. struct i2c_algo_bit_data *bit_data;
  477. struct i2c_adapter *adap;
  478. int ret;
  479. priv = pvPortMalloc(sizeof(*priv));
  480. if (!priv)
  481. return -ENOMEM;
  482. memset(priv, 0, sizeof(*priv));
  483. adap = &priv->adap;
  484. bit_data = &priv->bit_data;
  485. pdata = &priv->pdata;
  486. memcpy(pdata, pdevdata, sizeof(*pdata));
  487. gpio_request(pdata->sda_pin);
  488. gpio_request(pdata->scl_pin);
  489. if (pdata->sda_is_open_drain) {
  490. gpio_direction_output(pdata->sda_pin, 1);
  491. bit_data->setsda = i2c_gpio_setsda_val;
  492. } else {
  493. gpio_direction_input(pdata->sda_pin);
  494. bit_data->setsda = i2c_gpio_setsda_dir;
  495. }
  496. if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
  497. gpio_direction_output(pdata->scl_pin, 1);
  498. bit_data->setscl = i2c_gpio_setscl_val;
  499. } else {
  500. gpio_direction_input(pdata->scl_pin);
  501. bit_data->setscl = i2c_gpio_setscl_dir;
  502. }
  503. if (!pdata->scl_is_output_only)
  504. bit_data->getscl = i2c_gpio_getscl;
  505. bit_data->getsda = i2c_gpio_getsda;
  506. if (pdata->udelay)
  507. bit_data->udelay = pdata->udelay;
  508. else if (pdata->scl_is_output_only)
  509. bit_data->udelay = 50; /* 10 kHz */
  510. else
  511. bit_data->udelay = 5; /* 100 kHz */
  512. if (pdata->timeout)
  513. bit_data->timeout = pdata->timeout;
  514. else
  515. bit_data->timeout = configTICK_RATE_HZ / 10; /* 100 ms */
  516. bit_data->data = pdata;
  517. snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdata->devid);
  518. adap->algo_data = bit_data;
  519. ret = i2c_bit_add_bus(adap);
  520. if (ret)
  521. return ret;
  522. TRACE_INFO("using pins %u (SDA) and %u (SCL%s)\n",
  523. pdata->sda_pin, pdata->scl_pin,
  524. pdata->scl_is_output_only
  525. ? ", no clock stretching" : "");
  526. return 0;
  527. }
  528. void i2c_gpio_init(void)
  529. {
  530. i2c_gpio_add_device(&i2c_gpio0);
  531. }