i2c-algo-pcf.c 11 KB

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