i2c-algo-pcf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 int wait_for_bb(struct i2c_algo_pcf_data *adap) {
  65. int timeout = DEF_TIMEOUT;
  66. int status;
  67. status = get_pcf(adap, 1);
  68. #ifndef STUB_I2C
  69. while (timeout-- && !(status & I2C_PCF_BB)) {
  70. udelay(100); /* wait for 100 us */
  71. status = get_pcf(adap, 1);
  72. }
  73. #endif
  74. if (timeout <= 0) {
  75. printk(KERN_ERR "Timeout waiting for Bus Busy\n");
  76. }
  77. return (timeout<=0);
  78. }
  79. static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
  80. int timeout = DEF_TIMEOUT;
  81. *status = get_pcf(adap, 1);
  82. #ifndef STUB_I2C
  83. while (timeout-- && (*status & I2C_PCF_PIN)) {
  84. adap->waitforpin();
  85. *status = get_pcf(adap, 1);
  86. }
  87. if (*status & I2C_PCF_LAB) {
  88. DEB2(printk(KERN_INFO
  89. "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
  90. *status));
  91. /* Cleanup from LAB-- reset and enable ESO.
  92. * This resets the PCF8584; since we've lost the bus, no
  93. * further attempts should be made by callers to clean up
  94. * (no i2c_stop() etc.)
  95. */
  96. set_pcf(adap, 1, I2C_PCF_PIN);
  97. set_pcf(adap, 1, I2C_PCF_ESO);
  98. /* TODO: we should pause for a time period sufficient for any
  99. * running I2C transaction to complete-- the arbitration
  100. * logic won't work properly until the next START is seen.
  101. */
  102. DEB2(printk(KERN_INFO
  103. "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
  104. get_pcf(adap,1)));
  105. return(-EINTR);
  106. }
  107. #endif
  108. if (timeout <= 0)
  109. return(-1);
  110. else
  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", get_pcf(adap, 1)));
  128. /* S1=0x80: S0 selected, serial interface off */
  129. set_pcf(adap, 1, I2C_PCF_PIN);
  130. /* check to see S1 now used as R/W ctrl -
  131. PCF8584 does that when ESO is zero */
  132. if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) {
  133. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
  134. return -ENXIO; /* definetly not PCF8584 */
  135. }
  136. /* load own address in S0, effective address is (own << 1) */
  137. i2c_outb(adap, get_own(adap));
  138. /* check it's really written */
  139. if ((temp = i2c_inb(adap)) != get_own(adap)) {
  140. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
  141. return -ENXIO;
  142. }
  143. /* S1=0xA0, next byte in S2 */
  144. set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
  145. /* check to see S2 now selected */
  146. if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) {
  147. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
  148. return -ENXIO;
  149. }
  150. /* load clock register S2 */
  151. i2c_outb(adap, get_clock(adap));
  152. /* check it's really written, the only 5 lowest bits does matter */
  153. if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
  154. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
  155. return -ENXIO;
  156. }
  157. /* Enable serial interface, idle, S0 selected */
  158. set_pcf(adap, 1, I2C_PCF_IDLE);
  159. /* check to see PCF is really idled and we can access status register */
  160. if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
  161. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
  162. return -ENXIO;
  163. }
  164. printk(KERN_DEBUG "i2c-algo-pcf.o: deteted and initialized PCF8584.\n");
  165. return 0;
  166. }
  167. /* ----- Utility functions
  168. */
  169. static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
  170. int count, int last)
  171. {
  172. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  173. int wrcount, status, timeout;
  174. for (wrcount=0; wrcount<count; ++wrcount) {
  175. DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
  176. buf[wrcount]&0xff));
  177. i2c_outb(adap, buf[wrcount]);
  178. timeout = wait_for_pin(adap, &status);
  179. if (timeout) {
  180. if (timeout == -EINTR) {
  181. /* arbitration lost */
  182. return -EINTR;
  183. }
  184. i2c_stop(adap);
  185. dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
  186. return -EREMOTEIO; /* got a better one ?? */
  187. }
  188. #ifndef STUB_I2C
  189. if (status & I2C_PCF_LRB) {
  190. i2c_stop(adap);
  191. dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
  192. return -EREMOTEIO; /* got a better one ?? */
  193. }
  194. #endif
  195. }
  196. if (last) {
  197. i2c_stop(adap);
  198. }
  199. else {
  200. i2c_repstart(adap);
  201. }
  202. return (wrcount);
  203. }
  204. static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
  205. int count, int last)
  206. {
  207. int i, status;
  208. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  209. int wfp;
  210. /* increment number of bytes to read by one -- read dummy byte */
  211. for (i = 0; i <= count; i++) {
  212. if ((wfp = wait_for_pin(adap, &status))) {
  213. if (wfp == -EINTR) {
  214. /* arbitration lost */
  215. return -EINTR;
  216. }
  217. i2c_stop(adap);
  218. dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
  219. return (-1);
  220. }
  221. #ifndef STUB_I2C
  222. if ((status & I2C_PCF_LRB) && (i != count)) {
  223. i2c_stop(adap);
  224. dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
  225. return (-1);
  226. }
  227. #endif
  228. if (i == count - 1) {
  229. set_pcf(adap, 1, I2C_PCF_ESO);
  230. } else
  231. if (i == count) {
  232. if (last) {
  233. i2c_stop(adap);
  234. } else {
  235. i2c_repstart(adap);
  236. }
  237. };
  238. if (i) {
  239. buf[i - 1] = i2c_inb(adap);
  240. } else {
  241. i2c_inb(adap); /* dummy read */
  242. }
  243. }
  244. return (i - 1);
  245. }
  246. static int pcf_doAddress(struct i2c_algo_pcf_data *adap,
  247. struct i2c_msg *msg)
  248. {
  249. unsigned short flags = msg->flags;
  250. unsigned char addr;
  251. addr = msg->addr << 1;
  252. if (flags & I2C_M_RD)
  253. addr |= 1;
  254. if (flags & I2C_M_REV_DIR_ADDR)
  255. addr ^= 1;
  256. i2c_outb(adap, addr);
  257. return 0;
  258. }
  259. static int pcf_xfer(struct i2c_adapter *i2c_adap,
  260. struct i2c_msg *msgs,
  261. int num)
  262. {
  263. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  264. struct i2c_msg *pmsg;
  265. int i;
  266. int ret=0, timeout, status;
  267. /* Check for bus busy */
  268. timeout = wait_for_bb(adap);
  269. if (timeout) {
  270. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
  271. "Timeout waiting for BB in pcf_xfer\n");)
  272. return -EIO;
  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. }
  284. /* Wait for PIN (pending interrupt NOT) */
  285. timeout = wait_for_pin(adap, &status);
  286. if (timeout) {
  287. if (timeout == -EINTR) {
  288. /* arbitration lost */
  289. return (-EINTR);
  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. return (-EREMOTEIO);
  295. }
  296. #ifndef STUB_I2C
  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. return (-EREMOTEIO);
  302. }
  303. #endif
  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. /* Read */
  307. if (pmsg->flags & I2C_M_RD) {
  308. /* read bytes into buffer*/
  309. ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
  310. (i + 1 == num));
  311. if (ret != pmsg->len) {
  312. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
  313. "only read %d bytes.\n",ret));
  314. } else {
  315. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
  316. }
  317. } else { /* Write */
  318. ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
  319. (i + 1 == num));
  320. if (ret != pmsg->len) {
  321. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
  322. "only wrote %d bytes.\n",ret));
  323. } else {
  324. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
  325. }
  326. }
  327. }
  328. return (i);
  329. }
  330. static u32 pcf_func(struct i2c_adapter *adap)
  331. {
  332. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  333. I2C_FUNC_PROTOCOL_MANGLING;
  334. }
  335. /* -----exported algorithm data: ------------------------------------- */
  336. static const struct i2c_algorithm pcf_algo = {
  337. .master_xfer = pcf_xfer,
  338. .functionality = pcf_func,
  339. };
  340. /*
  341. * registering functions to load algorithms at runtime
  342. */
  343. int i2c_pcf_add_bus(struct i2c_adapter *adap)
  344. {
  345. struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
  346. int rval;
  347. DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
  348. /* register new adapter to i2c module... */
  349. adap->algo = &pcf_algo;
  350. adap->timeout = 100;
  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");