i2c-algo-pcf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 <[email protected]> and
  9. * Frodo Looijaard <[email protected]>, and also from Martin Bailey
  10. * <[email protected]>
  11. *
  12. * Partially rewriten by Oleg I. Vdovikin <[email protected]> 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 "i2c-algo-pcf.h"
  22. #define DEB2(x) if (i2c_debug >= 2) x
  23. #define DEB3(x) if (i2c_debug >= 3) x /* print several statistical values */
  24. #define DEBPROTO(x) if (i2c_debug >= 9) x;
  25. /* debug the protocol by showing transferred bits */
  26. #define DEF_TIMEOUT 16
  27. /*
  28. * module parameters:
  29. */
  30. static int i2c_debug;
  31. /* setting states on the bus with the right timing: */
  32. #define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
  33. #define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
  34. #define get_own(adap) adap->getown(adap->data)
  35. #define get_clock(adap) adap->getclock(adap->data)
  36. #define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
  37. #define i2c_inb(adap) adap->getpcf(adap->data, 0)
  38. /* other auxiliary functions */
  39. static void i2c_start(struct i2c_algo_pcf_data *adap)
  40. {
  41. DEBPROTO(printk(KERN_DEBUG "S "));
  42. set_pcf(adap, 1, I2C_PCF_START);
  43. }
  44. static void i2c_repstart(struct i2c_algo_pcf_data *adap)
  45. {
  46. DEBPROTO(printk(" Sr "));
  47. set_pcf(adap, 1, I2C_PCF_REPSTART);
  48. }
  49. static void i2c_stop(struct i2c_algo_pcf_data *adap)
  50. {
  51. DEBPROTO(printk("P\n"));
  52. set_pcf(adap, 1, I2C_PCF_STOP);
  53. }
  54. static void handle_lab(struct i2c_algo_pcf_data *adap, const int *status)
  55. {
  56. DEB2(printk(KERN_INFO
  57. "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
  58. *status));
  59. /*
  60. * Cleanup from LAB -- reset and enable ESO.
  61. * This resets the PCF8584; since we've lost the bus, no
  62. * further attempts should be made by callers to clean up
  63. * (no i2c_stop() etc.)
  64. */
  65. set_pcf(adap, 1, I2C_PCF_PIN);
  66. set_pcf(adap, 1, I2C_PCF_ESO);
  67. /*
  68. * We pause for a time period sufficient for any running
  69. * I2C transaction to complete -- the arbitration logic won't
  70. * work properly until the next START is seen.
  71. * It is assumed the bus driver or client has set a proper value.
  72. *
  73. * REVISIT: should probably use msleep instead of mdelay if we
  74. * know we can sleep.
  75. */
  76. if (adap->lab_mdelay)
  77. mdelay(adap->lab_mdelay);
  78. DEB2(printk(KERN_INFO
  79. "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
  80. get_pcf(adap, 1)));
  81. }
  82. static int wait_for_bb(struct i2c_algo_pcf_data *adap)
  83. {
  84. int timeout = DEF_TIMEOUT;
  85. int status;
  86. status = get_pcf(adap, 1);
  87. while (!(status & I2C_PCF_BB) && --timeout) {
  88. udelay(100); /* wait for 100 us */
  89. status = get_pcf(adap, 1);
  90. }
  91. if (timeout == 0) {
  92. printk(KERN_ERR "Timeout waiting for Bus Busy\n");
  93. return -ETIMEDOUT;
  94. }
  95. return 0;
  96. }
  97. static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status)
  98. {
  99. int timeout = DEF_TIMEOUT;
  100. *status = get_pcf(adap, 1);
  101. while ((*status & I2C_PCF_PIN) && --timeout) {
  102. adap->waitforpin(adap->data);
  103. *status = get_pcf(adap, 1);
  104. }
  105. if (*status & I2C_PCF_LAB) {
  106. handle_lab(adap, status);
  107. return -EINTR;
  108. }
  109. if (timeout == 0)
  110. return -ETIMEDOUT;
  111. return 0;
  112. }
  113. /*
  114. * This should perform the 'PCF8584 initialization sequence' as described
  115. * in the Philips IC12 data book (1995, Aug 29).
  116. * There should be a 30 clock cycle wait after reset, I assume this
  117. * has been fulfilled.
  118. * There should be a delay at the end equal to the longest I2C message
  119. * to synchronize the BB-bit (in multimaster systems). How long is
  120. * this? I assume 1 second is always long enough.
  121. *
  122. * vdovikin: added detect code for PCF8584
  123. */
  124. static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
  125. {
  126. unsigned char temp;
  127. DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n",
  128. get_pcf(adap, 1)));
  129. /* S1=0x80: S0 selected, serial interface off */
  130. set_pcf(adap, 1, I2C_PCF_PIN);
  131. /*
  132. * check to see S1 now used as R/W ctrl -
  133. * PCF8584 does that when ESO is zero
  134. */
  135. if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) {
  136. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
  137. return -ENXIO; /* definitely not PCF8584 */
  138. }
  139. /* load own address in S0, effective address is (own << 1) */
  140. i2c_outb(adap, get_own(adap));
  141. /* check it's really written */
  142. if ((temp = i2c_inb(adap)) != get_own(adap)) {
  143. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
  144. return -ENXIO;
  145. }
  146. /* S1=0xA0, next byte in S2 */
  147. set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
  148. /* check to see S2 now selected */
  149. if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) {
  150. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
  151. return -ENXIO;
  152. }
  153. /* load clock register S2 */
  154. i2c_outb(adap, get_clock(adap));
  155. /* check it's really written, the only 5 lowest bits does matter */
  156. if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
  157. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
  158. return -ENXIO;
  159. }
  160. /* Enable serial interface, idle, S0 selected */
  161. set_pcf(adap, 1, I2C_PCF_IDLE);
  162. /* check to see PCF is really idled and we can access status register */
  163. if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
  164. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
  165. return -ENXIO;
  166. }
  167. printk(KERN_DEBUG "i2c-algo-pcf.o: detected and initialized PCF8584.\n");
  168. return 0;
  169. }
  170. static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
  171. int count, int last)
  172. {
  173. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  174. int wrcount, status, timeout;
  175. for (wrcount=0; wrcount<count; ++wrcount) {
  176. DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
  177. buf[wrcount] & 0xff));
  178. i2c_outb(adap, buf[wrcount]);
  179. timeout = wait_for_pin(adap, &status);
  180. if (timeout) {
  181. if (timeout == -EINTR)
  182. return -EINTR; /* arbitration lost */
  183. i2c_stop(adap);
  184. dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
  185. return -EREMOTEIO; /* got a better one ?? */
  186. }
  187. if (status & I2C_PCF_LRB) {
  188. i2c_stop(adap);
  189. dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
  190. return -EREMOTEIO; /* got a better one ?? */
  191. }
  192. }
  193. if (last)
  194. i2c_stop(adap);
  195. else
  196. i2c_repstart(adap);
  197. return wrcount;
  198. }
  199. static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
  200. int count, int last)
  201. {
  202. int i, status;
  203. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  204. int wfp;
  205. /* increment number of bytes to read by one -- read dummy byte */
  206. for (i = 0; i <= count; i++) {
  207. if ((wfp = wait_for_pin(adap, &status))) {
  208. if (wfp == -EINTR)
  209. return -EINTR; /* arbitration lost */
  210. i2c_stop(adap);
  211. dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
  212. return -1;
  213. }
  214. if ((status & I2C_PCF_LRB) && (i != count)) {
  215. i2c_stop(adap);
  216. dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
  217. return -1;
  218. }
  219. if (i == count - 1) {
  220. set_pcf(adap, 1, I2C_PCF_ESO);
  221. } else if (i == count) {
  222. if (last)
  223. i2c_stop(adap);
  224. else
  225. i2c_repstart(adap);
  226. }
  227. if (i)
  228. buf[i - 1] = i2c_inb(adap);
  229. else
  230. i2c_inb(adap); /* dummy read */
  231. }
  232. return i - 1;
  233. }
  234. static int pcf_doAddress(struct i2c_algo_pcf_data *adap,
  235. struct i2c_msg *msg)
  236. {
  237. unsigned char addr = i2c_8bit_addr_from_msg(msg);
  238. if (msg->flags & I2C_M_REV_DIR_ADDR)
  239. addr ^= 1;
  240. i2c_outb(adap, addr);
  241. return 0;
  242. }
  243. static int pcf_xfer(struct i2c_adapter *i2c_adap,
  244. struct i2c_msg *msgs,
  245. int num)
  246. {
  247. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  248. struct i2c_msg *pmsg;
  249. int i;
  250. int ret=0, timeout, status;
  251. if (adap->xfer_begin)
  252. adap->xfer_begin(adap->data);
  253. /* Check for bus busy */
  254. timeout = wait_for_bb(adap);
  255. if (timeout) {
  256. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
  257. "Timeout waiting for BB in pcf_xfer\n");)
  258. i = -EIO;
  259. goto out;
  260. }
  261. for (i = 0;ret >= 0 && i < num; i++) {
  262. pmsg = &msgs[i];
  263. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
  264. pmsg->flags & I2C_M_RD ? "read" : "write",
  265. pmsg->len, pmsg->addr, i + 1, num);)
  266. ret = pcf_doAddress(adap, pmsg);
  267. /* Send START */
  268. if (i == 0)
  269. i2c_start(adap);
  270. /* Wait for PIN (pending interrupt NOT) */
  271. timeout = wait_for_pin(adap, &status);
  272. if (timeout) {
  273. if (timeout == -EINTR) {
  274. /* arbitration lost */
  275. i = -EINTR;
  276. goto out;
  277. }
  278. i2c_stop(adap);
  279. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
  280. "for PIN(1) in pcf_xfer\n");)
  281. i = -EREMOTEIO;
  282. goto out;
  283. }
  284. /* Check LRB (last rcvd bit - slave ack) */
  285. if (status & I2C_PCF_LRB) {
  286. i2c_stop(adap);
  287. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
  288. i = -EREMOTEIO;
  289. goto out;
  290. }
  291. DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
  292. i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
  293. if (pmsg->flags & I2C_M_RD) {
  294. ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
  295. (i + 1 == num));
  296. if (ret != pmsg->len) {
  297. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
  298. "only read %d bytes.\n",ret));
  299. } else {
  300. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
  301. }
  302. } else {
  303. ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
  304. (i + 1 == num));
  305. if (ret != pmsg->len) {
  306. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
  307. "only wrote %d bytes.\n",ret));
  308. } else {
  309. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
  310. }
  311. }
  312. }
  313. out:
  314. if (adap->xfer_end)
  315. adap->xfer_end(adap->data);
  316. return i;
  317. }
  318. static u32 pcf_func(struct i2c_adapter *adap)
  319. {
  320. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  321. I2C_FUNC_PROTOCOL_MANGLING;
  322. }
  323. /* exported algorithm data: */
  324. static const struct i2c_algorithm pcf_algo = {
  325. .master_xfer = pcf_xfer,
  326. .functionality = pcf_func,
  327. };
  328. /*
  329. * registering functions to load algorithms at runtime
  330. */
  331. int i2c_pcf_add_bus(struct i2c_adapter *adap)
  332. {
  333. struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
  334. int rval;
  335. DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
  336. /* register new adapter to i2c module... */
  337. adap->algo = &pcf_algo;
  338. if ((rval = pcf_init_8584(pcf_adap)))
  339. return rval;
  340. rval = i2c_add_adapter(adap);
  341. return rval;
  342. }
  343. EXPORT_SYMBOL(i2c_pcf_add_bus);
  344. MODULE_AUTHOR("Hans Berglund <[email protected]>");
  345. MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
  346. MODULE_LICENSE("GPL");
  347. module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
  348. MODULE_PARM_DESC(i2c_debug,
  349. "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");