i2c-algo-pca.c 9.9 KB

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