i2c-algo-pca.c 9.9 KB

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