i2c-algo-pcf.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters
  4. *
  5. * Copyright (C) 1995-1997 Simon G. Vogl
  6. * 1998-2000 Hans Berglund
  7. *
  8. * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
  9. * Frodo Looijaard <frodol@dds.nl>, and also from Martin Bailey
  10. * <mbailey@littlefeet-inc.com>
  11. *
  12. * Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
  13. * messages, proper stop/repstart signaling during receive, added detect code
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/delay.h>
  18. #include <linux/errno.h>
  19. #include <linux/i2c.h>
  20. #include <linux/i2c-algo-pcf.h>
  21. #include <linux/string_choices.h>
  22. #include "i2c-algo-pcf.h"
  23. #define DEF_TIMEOUT 16
  24. /* setting states on the bus with the right timing: */
  25. #define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
  26. #define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
  27. #define get_own(adap) adap->getown(adap->data)
  28. #define get_clock(adap) adap->getclock(adap->data)
  29. #define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
  30. #define i2c_inb(adap) adap->getpcf(adap->data, 0)
  31. /* other auxiliary functions */
  32. static void i2c_start(struct i2c_algo_pcf_data *adap)
  33. {
  34. set_pcf(adap, 1, I2C_PCF_START);
  35. }
  36. static void i2c_repstart(struct i2c_algo_pcf_data *adap)
  37. {
  38. set_pcf(adap, 1, I2C_PCF_REPSTART);
  39. }
  40. static void i2c_stop(struct i2c_algo_pcf_data *adap)
  41. {
  42. set_pcf(adap, 1, I2C_PCF_STOP);
  43. }
  44. static void handle_lab(struct i2c_algo_pcf_data *adap, const int *status)
  45. {
  46. /*
  47. * Cleanup from LAB -- reset and enable ESO.
  48. * This resets the PCF8584; since we've lost the bus, no
  49. * further attempts should be made by callers to clean up
  50. * (no i2c_stop() etc.)
  51. */
  52. set_pcf(adap, 1, I2C_PCF_PIN);
  53. set_pcf(adap, 1, I2C_PCF_ESO);
  54. /*
  55. * We pause for a time period sufficient for any running
  56. * I2C transaction to complete -- the arbitration logic won't
  57. * work properly until the next START is seen.
  58. * It is assumed the bus driver or client has set a proper value.
  59. *
  60. * REVISIT: should probably use msleep instead of mdelay if we
  61. * know we can sleep.
  62. */
  63. if (adap->lab_mdelay)
  64. mdelay(adap->lab_mdelay);
  65. }
  66. static int wait_for_bb(struct i2c_algo_pcf_data *adap)
  67. {
  68. int timeout = DEF_TIMEOUT;
  69. int status;
  70. status = get_pcf(adap, 1);
  71. while (!(status & I2C_PCF_BB) && --timeout) {
  72. udelay(100); /* wait for 100 us */
  73. status = get_pcf(adap, 1);
  74. }
  75. if (timeout == 0) {
  76. printk(KERN_ERR "Timeout waiting for Bus Busy\n");
  77. return -ETIMEDOUT;
  78. }
  79. return 0;
  80. }
  81. static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status)
  82. {
  83. int timeout = DEF_TIMEOUT;
  84. *status = get_pcf(adap, 1);
  85. while ((*status & I2C_PCF_PIN) && --timeout) {
  86. adap->waitforpin(adap->data);
  87. *status = get_pcf(adap, 1);
  88. }
  89. if (*status & I2C_PCF_LAB) {
  90. handle_lab(adap, status);
  91. return -EINTR;
  92. }
  93. if (timeout == 0)
  94. return -ETIMEDOUT;
  95. return 0;
  96. }
  97. /*
  98. * This should perform the 'PCF8584 initialization sequence' as described
  99. * in the Philips IC12 data book (1995, Aug 29).
  100. * There should be a 30 clock cycle wait after reset, I assume this
  101. * has been fulfilled.
  102. * There should be a delay at the end equal to the longest I2C message
  103. * to synchronize the BB-bit (in multimaster systems). How long is
  104. * this? I assume 1 second is always long enough.
  105. *
  106. * vdovikin: added detect code for PCF8584
  107. */
  108. static int pcf_init_8584(struct i2c_algo_pcf_data *adap)
  109. {
  110. unsigned char temp;
  111. /* S1=0x80: S0 selected, serial interface off */
  112. set_pcf(adap, 1, I2C_PCF_PIN);
  113. /*
  114. * check to see S1 now used as R/W ctrl -
  115. * PCF8584 does that when ESO is zero
  116. */
  117. temp = get_pcf(adap, 1);
  118. if ((temp & 0x7f) != 0)
  119. return -ENXIO; /* definitely not PCF8584 */
  120. /* load own address in S0, effective address is (own << 1) */
  121. i2c_outb(adap, get_own(adap));
  122. /* check it's really written */
  123. temp = i2c_inb(adap);
  124. if (temp != get_own(adap))
  125. return -ENXIO;
  126. /* S1=0xA0, next byte in S2 */
  127. set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
  128. /* check to see S2 now selected */
  129. temp = get_pcf(adap, 1);
  130. if ((temp & 0x7f) != I2C_PCF_ES1)
  131. return -ENXIO;
  132. /* load clock register S2 */
  133. i2c_outb(adap, get_clock(adap));
  134. /* check it's really written, the only 5 lowest bits does matter */
  135. temp = i2c_inb(adap);
  136. if ((temp & 0x1f) != get_clock(adap))
  137. return -ENXIO;
  138. /* Enable serial interface, idle, S0 selected */
  139. set_pcf(adap, 1, I2C_PCF_IDLE);
  140. /* check to see PCF is really idled and we can access status register */
  141. temp = get_pcf(adap, 1);
  142. if (temp != (I2C_PCF_PIN | I2C_PCF_BB))
  143. return -ENXIO;
  144. printk(KERN_DEBUG "i2c-algo-pcf.o: detected and initialized PCF8584.\n");
  145. return 0;
  146. }
  147. static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
  148. int count, int last)
  149. {
  150. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  151. int wrcount, status, timeout;
  152. for (wrcount = 0; wrcount < count; ++wrcount) {
  153. i2c_outb(adap, buf[wrcount]);
  154. timeout = wait_for_pin(adap, &status);
  155. if (timeout) {
  156. if (timeout == -EINTR)
  157. return -EINTR; /* arbitration lost */
  158. i2c_stop(adap);
  159. dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
  160. return -EREMOTEIO; /* got a better one ?? */
  161. }
  162. if (status & I2C_PCF_LRB) {
  163. i2c_stop(adap);
  164. dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
  165. return -EREMOTEIO; /* got a better one ?? */
  166. }
  167. }
  168. if (last)
  169. i2c_stop(adap);
  170. else
  171. i2c_repstart(adap);
  172. return wrcount;
  173. }
  174. static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
  175. int count, int last)
  176. {
  177. int i, status;
  178. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  179. int wfp;
  180. /* increment number of bytes to read by one -- read dummy byte */
  181. for (i = 0; i <= count; i++) {
  182. wfp = wait_for_pin(adap, &status);
  183. if (wfp) {
  184. if (wfp == -EINTR)
  185. return -EINTR; /* arbitration lost */
  186. i2c_stop(adap);
  187. dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
  188. return -1;
  189. }
  190. if ((status & I2C_PCF_LRB) && (i != count)) {
  191. i2c_stop(adap);
  192. dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
  193. return -1;
  194. }
  195. if (i == count - 1) {
  196. set_pcf(adap, 1, I2C_PCF_ESO);
  197. } else if (i == count) {
  198. if (last)
  199. i2c_stop(adap);
  200. else
  201. i2c_repstart(adap);
  202. }
  203. if (i)
  204. buf[i - 1] = i2c_inb(adap);
  205. else
  206. i2c_inb(adap); /* dummy read */
  207. }
  208. return i - 1;
  209. }
  210. static void pcf_send_address(struct i2c_algo_pcf_data *adap,
  211. struct i2c_msg *msg)
  212. {
  213. unsigned char addr = i2c_8bit_addr_from_msg(msg);
  214. if (msg->flags & I2C_M_REV_DIR_ADDR)
  215. addr ^= 1;
  216. i2c_outb(adap, addr);
  217. }
  218. static int pcf_xfer(struct i2c_adapter *i2c_adap,
  219. struct i2c_msg *msgs,
  220. int num)
  221. {
  222. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  223. struct i2c_msg *pmsg;
  224. int i;
  225. int timeout, status;
  226. if (adap->xfer_begin)
  227. adap->xfer_begin(adap->data);
  228. /* Check for bus busy */
  229. timeout = wait_for_bb(adap);
  230. if (timeout) {
  231. i = -EIO;
  232. goto out;
  233. }
  234. for (i = 0; i < num; i++) {
  235. int ret;
  236. pmsg = &msgs[i];
  237. pcf_send_address(adap, pmsg);
  238. /* Send START */
  239. if (i == 0)
  240. i2c_start(adap);
  241. /* Wait for PIN (pending interrupt NOT) */
  242. timeout = wait_for_pin(adap, &status);
  243. if (timeout) {
  244. if (timeout == -EINTR) {
  245. /* arbitration lost */
  246. i = -EINTR;
  247. goto out;
  248. }
  249. i2c_stop(adap);
  250. i = -EREMOTEIO;
  251. goto out;
  252. }
  253. /* Check LRB (last rcvd bit - slave ack) */
  254. if (status & I2C_PCF_LRB) {
  255. i2c_stop(adap);
  256. i = -EREMOTEIO;
  257. goto out;
  258. }
  259. if (pmsg->flags & I2C_M_RD) {
  260. ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
  261. (i + 1 == num));
  262. } else {
  263. ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
  264. (i + 1 == num));
  265. }
  266. if (ret < 0)
  267. goto out;
  268. }
  269. out:
  270. if (adap->xfer_end)
  271. adap->xfer_end(adap->data);
  272. return i;
  273. }
  274. static u32 pcf_func(struct i2c_adapter *adap)
  275. {
  276. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  277. I2C_FUNC_PROTOCOL_MANGLING;
  278. }
  279. /* exported algorithm data: */
  280. static const struct i2c_algorithm pcf_algo = {
  281. .xfer = pcf_xfer,
  282. .functionality = pcf_func,
  283. };
  284. /*
  285. * registering functions to load algorithms at runtime
  286. */
  287. int i2c_pcf_add_bus(struct i2c_adapter *adap)
  288. {
  289. struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
  290. int rval;
  291. /* register new adapter to i2c module... */
  292. adap->algo = &pcf_algo;
  293. rval = pcf_init_8584(pcf_adap);
  294. if (rval)
  295. return rval;
  296. rval = i2c_add_adapter(adap);
  297. return rval;
  298. }
  299. EXPORT_SYMBOL(i2c_pcf_add_bus);
  300. MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
  301. MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
  302. MODULE_LICENSE("GPL");