i2c-algo-pcf.c 11 KB

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