i2c-algo-pcf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 inline int try_address(struct i2c_algo_pcf_data *adap,
  170. unsigned char addr, int retries)
  171. {
  172. int i, status, ret = -1;
  173. int wfp;
  174. for (i=0;i<retries;i++) {
  175. i2c_outb(adap, addr);
  176. i2c_start(adap);
  177. status = get_pcf(adap, 1);
  178. if ((wfp = wait_for_pin(adap, &status)) >= 0) {
  179. if ((status & I2C_PCF_LRB) == 0) {
  180. i2c_stop(adap);
  181. break; /* success! */
  182. }
  183. }
  184. if (wfp == -EINTR) {
  185. /* arbitration lost */
  186. udelay(adap->udelay);
  187. return -EINTR;
  188. }
  189. i2c_stop(adap);
  190. udelay(adap->udelay);
  191. }
  192. DEB2(if (i) printk(KERN_DEBUG "i2c-algo-pcf.o: needed %d retries for %d\n",i,
  193. addr));
  194. return ret;
  195. }
  196. static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
  197. int count, int last)
  198. {
  199. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  200. int wrcount, status, timeout;
  201. for (wrcount=0; wrcount<count; ++wrcount) {
  202. DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
  203. buf[wrcount]&0xff));
  204. i2c_outb(adap, buf[wrcount]);
  205. timeout = wait_for_pin(adap, &status);
  206. if (timeout) {
  207. if (timeout == -EINTR) {
  208. /* arbitration lost */
  209. return -EINTR;
  210. }
  211. i2c_stop(adap);
  212. dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
  213. return -EREMOTEIO; /* got a better one ?? */
  214. }
  215. #ifndef STUB_I2C
  216. if (status & I2C_PCF_LRB) {
  217. i2c_stop(adap);
  218. dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
  219. return -EREMOTEIO; /* got a better one ?? */
  220. }
  221. #endif
  222. }
  223. if (last) {
  224. i2c_stop(adap);
  225. }
  226. else {
  227. i2c_repstart(adap);
  228. }
  229. return (wrcount);
  230. }
  231. static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
  232. int count, int last)
  233. {
  234. int i, status;
  235. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  236. int wfp;
  237. /* increment number of bytes to read by one -- read dummy byte */
  238. for (i = 0; i <= count; i++) {
  239. if ((wfp = wait_for_pin(adap, &status))) {
  240. if (wfp == -EINTR) {
  241. /* arbitration lost */
  242. return -EINTR;
  243. }
  244. i2c_stop(adap);
  245. dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
  246. return (-1);
  247. }
  248. #ifndef STUB_I2C
  249. if ((status & I2C_PCF_LRB) && (i != count)) {
  250. i2c_stop(adap);
  251. dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
  252. return (-1);
  253. }
  254. #endif
  255. if (i == count - 1) {
  256. set_pcf(adap, 1, I2C_PCF_ESO);
  257. } else
  258. if (i == count) {
  259. if (last) {
  260. i2c_stop(adap);
  261. } else {
  262. i2c_repstart(adap);
  263. }
  264. };
  265. if (i) {
  266. buf[i - 1] = i2c_inb(adap);
  267. } else {
  268. i2c_inb(adap); /* dummy read */
  269. }
  270. }
  271. return (i - 1);
  272. }
  273. static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
  274. struct i2c_msg *msg, int retries)
  275. {
  276. unsigned short flags = msg->flags;
  277. unsigned char addr;
  278. int ret;
  279. if ( (flags & I2C_M_TEN) ) {
  280. /* a ten bit address */
  281. addr = 0xf0 | (( msg->addr >> 7) & 0x03);
  282. DEB2(printk(KERN_DEBUG "addr0: %d\n",addr));
  283. /* try extended address code...*/
  284. ret = try_address(adap, addr, retries);
  285. if (ret!=1) {
  286. printk(KERN_ERR "died at extended address code.\n");
  287. return -EREMOTEIO;
  288. }
  289. /* the remaining 8 bit address */
  290. i2c_outb(adap,msg->addr & 0x7f);
  291. /* Status check comes here */
  292. if (ret != 1) {
  293. printk(KERN_ERR "died at 2nd address code.\n");
  294. return -EREMOTEIO;
  295. }
  296. if ( flags & I2C_M_RD ) {
  297. i2c_repstart(adap);
  298. /* okay, now switch into reading mode */
  299. addr |= 0x01;
  300. ret = try_address(adap, addr, retries);
  301. if (ret!=1) {
  302. printk(KERN_ERR "died at extended address code.\n");
  303. return -EREMOTEIO;
  304. }
  305. }
  306. } else { /* normal 7bit address */
  307. addr = ( msg->addr << 1 );
  308. if (flags & I2C_M_RD )
  309. addr |= 1;
  310. if (flags & I2C_M_REV_DIR_ADDR )
  311. addr ^= 1;
  312. i2c_outb(adap, addr);
  313. }
  314. return 0;
  315. }
  316. static int pcf_xfer(struct i2c_adapter *i2c_adap,
  317. struct i2c_msg *msgs,
  318. int num)
  319. {
  320. struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  321. struct i2c_msg *pmsg;
  322. int i;
  323. int ret=0, timeout, status;
  324. /* Check for bus busy */
  325. timeout = wait_for_bb(adap);
  326. if (timeout) {
  327. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
  328. "Timeout waiting for BB in pcf_xfer\n");)
  329. return -EIO;
  330. }
  331. for (i = 0;ret >= 0 && i < num; i++) {
  332. pmsg = &msgs[i];
  333. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
  334. pmsg->flags & I2C_M_RD ? "read" : "write",
  335. pmsg->len, pmsg->addr, i + 1, num);)
  336. ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
  337. /* Send START */
  338. if (i == 0) {
  339. i2c_start(adap);
  340. }
  341. /* Wait for PIN (pending interrupt NOT) */
  342. timeout = wait_for_pin(adap, &status);
  343. if (timeout) {
  344. if (timeout == -EINTR) {
  345. /* arbitration lost */
  346. return (-EINTR);
  347. }
  348. i2c_stop(adap);
  349. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
  350. "for PIN(1) in pcf_xfer\n");)
  351. return (-EREMOTEIO);
  352. }
  353. #ifndef STUB_I2C
  354. /* Check LRB (last rcvd bit - slave ack) */
  355. if (status & I2C_PCF_LRB) {
  356. i2c_stop(adap);
  357. DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
  358. return (-EREMOTEIO);
  359. }
  360. #endif
  361. DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
  362. i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
  363. /* Read */
  364. if (pmsg->flags & I2C_M_RD) {
  365. /* read bytes into buffer*/
  366. ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
  367. (i + 1 == num));
  368. if (ret != pmsg->len) {
  369. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
  370. "only read %d bytes.\n",ret));
  371. } else {
  372. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
  373. }
  374. } else { /* Write */
  375. ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
  376. (i + 1 == num));
  377. if (ret != pmsg->len) {
  378. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
  379. "only wrote %d bytes.\n",ret));
  380. } else {
  381. DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
  382. }
  383. }
  384. }
  385. return (i);
  386. }
  387. static u32 pcf_func(struct i2c_adapter *adap)
  388. {
  389. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  390. I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  391. }
  392. /* -----exported algorithm data: ------------------------------------- */
  393. static const struct i2c_algorithm pcf_algo = {
  394. .master_xfer = pcf_xfer,
  395. .functionality = pcf_func,
  396. };
  397. /*
  398. * registering functions to load algorithms at runtime
  399. */
  400. int i2c_pcf_add_bus(struct i2c_adapter *adap)
  401. {
  402. struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
  403. int rval;
  404. DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
  405. /* register new adapter to i2c module... */
  406. adap->algo = &pcf_algo;
  407. adap->timeout = 100; /* default values, should */
  408. adap->retries = 3; /* be replaced by defines */
  409. if ((rval = pcf_init_8584(pcf_adap)))
  410. return rval;
  411. rval = i2c_add_adapter(adap);
  412. return rval;
  413. }
  414. EXPORT_SYMBOL(i2c_pcf_add_bus);
  415. MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
  416. MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
  417. MODULE_LICENSE("GPL");
  418. module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
  419. MODULE_PARM_DESC(i2c_debug,
  420. "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");