i2c-algo-pcf.c 12 KB

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