i2c-algo-pca.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters
  3. * Copyright (C) 2004 Arcom Control Systems
  4. * Copyright (C) 2008 Pengutronix
  5. *
  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. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/i2c.h>
  27. #include <linux/i2c-algo-pca.h>
  28. #define DEB1(fmt, args...) do { if (i2c_debug>=1) printk(fmt, ## args); } while(0)
  29. #define DEB2(fmt, args...) do { if (i2c_debug>=2) printk(fmt, ## args); } while(0)
  30. #define DEB3(fmt, args...) do { if (i2c_debug>=3) printk(fmt, ## args); } while(0)
  31. static int i2c_debug;
  32. #define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)
  33. #define pca_inw(adap, reg) adap->read_byte(adap->data, reg)
  34. #define pca_status(adap) pca_inw(adap, I2C_PCA_STA)
  35. #define pca_clock(adap) adap->i2c_clock
  36. #define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)
  37. #define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)
  38. #define pca_wait(adap) adap->wait_for_completion(adap->data)
  39. #define pca_reset(adap) adap->reset_chip(adap->data)
  40. /*
  41. * Generate a start condition on the i2c bus.
  42. *
  43. * returns after the start condition has occurred
  44. */
  45. static void pca_start(struct i2c_algo_pca_data *adap)
  46. {
  47. int sta = pca_get_con(adap);
  48. DEB2("=== START\n");
  49. sta |= I2C_PCA_CON_STA;
  50. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
  51. pca_set_con(adap, sta);
  52. pca_wait(adap);
  53. }
  54. /*
  55. * Generate a repeated start condition on the i2c bus
  56. *
  57. * return after the repeated start condition has occurred
  58. */
  59. static void pca_repeated_start(struct i2c_algo_pca_data *adap)
  60. {
  61. int sta = pca_get_con(adap);
  62. DEB2("=== REPEATED START\n");
  63. sta |= I2C_PCA_CON_STA;
  64. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
  65. pca_set_con(adap, sta);
  66. pca_wait(adap);
  67. }
  68. /*
  69. * Generate a stop condition on the i2c bus
  70. *
  71. * returns after the stop condition has been generated
  72. *
  73. * STOPs do not generate an interrupt or set the SI flag, since the
  74. * part returns the idle state (0xf8). Hence we don't need to
  75. * pca_wait here.
  76. */
  77. static void pca_stop(struct i2c_algo_pca_data *adap)
  78. {
  79. int sta = pca_get_con(adap);
  80. DEB2("=== STOP\n");
  81. sta |= I2C_PCA_CON_STO;
  82. sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_SI);
  83. pca_set_con(adap, sta);
  84. }
  85. /*
  86. * Send the slave address and R/W bit
  87. *
  88. * returns after the address has been sent
  89. */
  90. static void pca_address(struct i2c_algo_pca_data *adap,
  91. struct i2c_msg *msg)
  92. {
  93. int sta = pca_get_con(adap);
  94. int addr;
  95. addr = ( (0x7f & msg->addr) << 1 );
  96. if (msg->flags & I2C_M_RD )
  97. addr |= 1;
  98. DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",
  99. msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);
  100. pca_outw(adap, I2C_PCA_DAT, addr);
  101. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
  102. pca_set_con(adap, sta);
  103. pca_wait(adap);
  104. }
  105. /*
  106. * Transmit a byte.
  107. *
  108. * Returns after the byte has been transmitted
  109. */
  110. static void pca_tx_byte(struct i2c_algo_pca_data *adap,
  111. __u8 b)
  112. {
  113. int sta = pca_get_con(adap);
  114. DEB2("=== WRITE %#04x\n", b);
  115. pca_outw(adap, I2C_PCA_DAT, b);
  116. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
  117. pca_set_con(adap, sta);
  118. pca_wait(adap);
  119. }
  120. /*
  121. * Receive a byte
  122. *
  123. * returns immediately.
  124. */
  125. static void pca_rx_byte(struct i2c_algo_pca_data *adap,
  126. __u8 *b, int ack)
  127. {
  128. *b = pca_inw(adap, I2C_PCA_DAT);
  129. DEB2("=== READ %#04x %s\n", *b, ack ? "ACK" : "NACK");
  130. }
  131. /*
  132. * Setup ACK or NACK for next received byte and wait for it to arrive.
  133. *
  134. * Returns after next byte has arrived.
  135. */
  136. static void pca_rx_ack(struct i2c_algo_pca_data *adap,
  137. int ack)
  138. {
  139. int sta = pca_get_con(adap);
  140. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI|I2C_PCA_CON_AA);
  141. if ( ack )
  142. sta |= I2C_PCA_CON_AA;
  143. pca_set_con(adap, sta);
  144. pca_wait(adap);
  145. }
  146. static int pca_xfer(struct i2c_adapter *i2c_adap,
  147. struct i2c_msg *msgs,
  148. int num)
  149. {
  150. struct i2c_algo_pca_data *adap = i2c_adap->algo_data;
  151. struct i2c_msg *msg = NULL;
  152. int curmsg;
  153. int numbytes = 0;
  154. int state;
  155. int ret;
  156. int timeout = i2c_adap->timeout;
  157. while ((state = pca_status(adap)) != 0xf8 && timeout--) {
  158. msleep(10);
  159. }
  160. if (state != 0xf8) {
  161. dev_dbg(&i2c_adap->dev, "bus is not idle. status is %#04x\n", state);
  162. return -EAGAIN;
  163. }
  164. DEB1("{{{ XFER %d messages\n", num);
  165. if (i2c_debug>=2) {
  166. for (curmsg = 0; curmsg < num; curmsg++) {
  167. int addr, i;
  168. msg = &msgs[curmsg];
  169. addr = (0x7f & msg->addr) ;
  170. if (msg->flags & I2C_M_RD )
  171. printk(KERN_INFO " [%02d] RD %d bytes from %#02x [%#02x, ...]\n",
  172. curmsg, msg->len, addr, (addr<<1) | 1);
  173. else {
  174. printk(KERN_INFO " [%02d] WR %d bytes to %#02x [%#02x%s",
  175. curmsg, msg->len, addr, addr<<1,
  176. msg->len == 0 ? "" : ", ");
  177. for(i=0; i < msg->len; i++)
  178. printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", ");
  179. printk("]\n");
  180. }
  181. }
  182. }
  183. curmsg = 0;
  184. ret = -EREMOTEIO;
  185. while (curmsg < num) {
  186. state = pca_status(adap);
  187. DEB3("STATE is 0x%02x\n", state);
  188. msg = &msgs[curmsg];
  189. switch (state) {
  190. case 0xf8: /* On reset or stop the bus is idle */
  191. pca_start(adap);
  192. break;
  193. case 0x08: /* A START condition has been transmitted */
  194. case 0x10: /* A repeated start condition has been transmitted */
  195. pca_address(adap, msg);
  196. break;
  197. case 0x18: /* SLA+W has been transmitted; ACK has been received */
  198. case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
  199. if (numbytes < msg->len) {
  200. pca_tx_byte(adap, msg->buf[numbytes]);
  201. numbytes++;
  202. break;
  203. }
  204. curmsg++; numbytes = 0;
  205. if (curmsg == num)
  206. pca_stop(adap);
  207. else
  208. pca_repeated_start(adap);
  209. break;
  210. case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
  211. DEB2("NOT ACK received after SLA+W\n");
  212. pca_stop(adap);
  213. goto out;
  214. case 0x40: /* SLA+R has been transmitted; ACK has been received */
  215. pca_rx_ack(adap, msg->len > 1);
  216. break;
  217. case 0x50: /* Data bytes has been received; ACK has been returned */
  218. if (numbytes < msg->len) {
  219. pca_rx_byte(adap, &msg->buf[numbytes], 1);
  220. numbytes++;
  221. pca_rx_ack(adap, numbytes < msg->len - 1);
  222. break;
  223. }
  224. curmsg++; numbytes = 0;
  225. if (curmsg == num)
  226. pca_stop(adap);
  227. else
  228. pca_repeated_start(adap);
  229. break;
  230. case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
  231. DEB2("NOT ACK received after SLA+R\n");
  232. pca_stop(adap);
  233. goto out;
  234. case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
  235. DEB2("NOT ACK received after data byte\n");
  236. goto out;
  237. case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
  238. DEB2("Arbitration lost\n");
  239. goto out;
  240. case 0x58: /* Data byte has been received; NOT ACK has been returned */
  241. if ( numbytes == msg->len - 1 ) {
  242. pca_rx_byte(adap, &msg->buf[numbytes], 0);
  243. curmsg++; numbytes = 0;
  244. if (curmsg == num)
  245. pca_stop(adap);
  246. else
  247. pca_repeated_start(adap);
  248. } else {
  249. DEB2("NOT ACK sent after data byte received. "
  250. "Not final byte. numbytes %d. len %d\n",
  251. numbytes, msg->len);
  252. pca_stop(adap);
  253. goto out;
  254. }
  255. break;
  256. case 0x70: /* Bus error - SDA stuck low */
  257. DEB2("BUS ERROR - SDA Stuck low\n");
  258. pca_reset(adap);
  259. goto out;
  260. case 0x90: /* Bus error - SCL stuck low */
  261. DEB2("BUS ERROR - SCL Stuck low\n");
  262. pca_reset(adap);
  263. goto out;
  264. case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */
  265. DEB2("BUS ERROR - Illegal START or STOP\n");
  266. pca_reset(adap);
  267. goto out;
  268. default:
  269. dev_err(&i2c_adap->dev, "unhandled SIO state 0x%02x\n", state);
  270. break;
  271. }
  272. }
  273. ret = curmsg;
  274. out:
  275. DEB1(KERN_CRIT "}}} transfered %d/%d messages. "
  276. "status is %#04x. control is %#04x\n",
  277. curmsg, num, pca_status(adap),
  278. pca_get_con(adap));
  279. return ret;
  280. }
  281. static u32 pca_func(struct i2c_adapter *adap)
  282. {
  283. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  284. }
  285. static const struct i2c_algorithm pca_algo = {
  286. .master_xfer = pca_xfer,
  287. .functionality = pca_func,
  288. };
  289. static int pca_init(struct i2c_adapter *adap)
  290. {
  291. static int freqs[] = {330,288,217,146,88,59,44,36};
  292. int clock;
  293. struct i2c_algo_pca_data *pca_data = adap->algo_data;
  294. if (pca_data->i2c_clock > 7) {
  295. printk(KERN_WARNING "%s: Invalid I2C clock speed selected. Trying default.\n",
  296. adap->name);
  297. pca_data->i2c_clock = I2C_PCA_CON_59kHz;
  298. }
  299. adap->algo = &pca_algo;
  300. pca_reset(pca_data);
  301. clock = pca_clock(pca_data);
  302. DEB1(KERN_INFO "%s: Clock frequency is %dkHz\n", adap->name, freqs[clock]);
  303. pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock);
  304. udelay(500); /* 500 us for oscilator to stabilise */
  305. return 0;
  306. }
  307. /*
  308. * registering functions to load algorithms at runtime
  309. */
  310. int i2c_pca_add_bus(struct i2c_adapter *adap)
  311. {
  312. int rval;
  313. rval = pca_init(adap);
  314. if (rval)
  315. return rval;
  316. return i2c_add_adapter(adap);
  317. }
  318. EXPORT_SYMBOL(i2c_pca_add_bus);
  319. int i2c_pca_add_numbered_bus(struct i2c_adapter *adap)
  320. {
  321. int rval;
  322. rval = pca_init(adap);
  323. if (rval)
  324. return rval;
  325. return i2c_add_numbered_adapter(adap);
  326. }
  327. EXPORT_SYMBOL(i2c_pca_add_numbered_bus);
  328. MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>, "
  329. "Wolfram Sang <w.sang@pengutronix.de>");
  330. MODULE_DESCRIPTION("I2C-Bus PCA9564 algorithm");
  331. MODULE_LICENSE("GPL");
  332. module_param(i2c_debug, int, 0);