i2c-algo-pcf.c 11 KB

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