i2c-algo-pca.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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=0;
  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. state = pca_status(adap);
  167. if ( state != 0xF8 ) {
  168. dev_dbg(&i2c_adap->dev, "bus is not idle. status is %#04x\n", state );
  169. /* FIXME: what to do. Force stop ? */
  170. return -EREMOTEIO;
  171. }
  172. DEB1("{{{ XFER %d messages\n", num);
  173. if (i2c_debug>=2) {
  174. for (curmsg = 0; curmsg < num; curmsg++) {
  175. int addr, i;
  176. msg = &msgs[curmsg];
  177. addr = (0x7f & msg->addr) ;
  178. if (msg->flags & I2C_M_RD )
  179. printk(KERN_INFO " [%02d] RD %d bytes from %#02x [%#02x, ...]\n",
  180. curmsg, msg->len, addr, (addr<<1) | 1);
  181. else {
  182. printk(KERN_INFO " [%02d] WR %d bytes to %#02x [%#02x%s",
  183. curmsg, msg->len, addr, addr<<1,
  184. msg->len == 0 ? "" : ", ");
  185. for(i=0; i < msg->len; i++)
  186. printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", ");
  187. printk("]\n");
  188. }
  189. }
  190. }
  191. curmsg = 0;
  192. ret = -EREMOTEIO;
  193. while (curmsg < num) {
  194. state = pca_status(adap);
  195. DEB3("STATE is 0x%02x\n", state);
  196. msg = &msgs[curmsg];
  197. switch (state) {
  198. case 0xf8: /* On reset or stop the bus is idle */
  199. pca_start(adap);
  200. break;
  201. case 0x08: /* A START condition has been transmitted */
  202. case 0x10: /* A repeated start condition has been transmitted */
  203. pca_address(adap, msg);
  204. break;
  205. case 0x18: /* SLA+W has been transmitted; ACK has been received */
  206. case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
  207. if (numbytes < msg->len) {
  208. pca_tx_byte(adap, msg->buf[numbytes]);
  209. numbytes++;
  210. break;
  211. }
  212. curmsg++; numbytes = 0;
  213. if (curmsg == num)
  214. pca_stop(adap);
  215. else
  216. pca_repeated_start(adap);
  217. break;
  218. case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
  219. DEB2("NOT ACK received after SLA+W\n");
  220. pca_stop(adap);
  221. goto out;
  222. case 0x40: /* SLA+R has been transmitted; ACK has been received */
  223. pca_rx_ack(adap, msg->len > 1);
  224. break;
  225. case 0x50: /* Data bytes has been received; ACK has been returned */
  226. if (numbytes < msg->len) {
  227. pca_rx_byte(adap, &msg->buf[numbytes], 1);
  228. numbytes++;
  229. pca_rx_ack(adap, numbytes < msg->len - 1);
  230. break;
  231. }
  232. curmsg++; numbytes = 0;
  233. if (curmsg == num)
  234. pca_stop(adap);
  235. else
  236. pca_repeated_start(adap);
  237. break;
  238. case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
  239. DEB2("NOT ACK received after SLA+R\n");
  240. pca_stop(adap);
  241. goto out;
  242. case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
  243. DEB2("NOT ACK received after data byte\n");
  244. goto out;
  245. case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
  246. DEB2("Arbitration lost\n");
  247. goto out;
  248. case 0x58: /* Data byte has been received; NOT ACK has been returned */
  249. if ( numbytes == msg->len - 1 ) {
  250. pca_rx_byte(adap, &msg->buf[numbytes], 0);
  251. curmsg++; numbytes = 0;
  252. if (curmsg == num)
  253. pca_stop(adap);
  254. else
  255. pca_repeated_start(adap);
  256. } else {
  257. DEB2("NOT ACK sent after data byte received. "
  258. "Not final byte. numbytes %d. len %d\n",
  259. numbytes, msg->len);
  260. pca_stop(adap);
  261. goto out;
  262. }
  263. break;
  264. case 0x70: /* Bus error - SDA stuck low */
  265. DEB2("BUS ERROR - SDA Stuck low\n");
  266. pca_reset(adap);
  267. goto out;
  268. case 0x90: /* Bus error - SCL stuck low */
  269. DEB2("BUS ERROR - SCL Stuck low\n");
  270. pca_reset(adap);
  271. goto out;
  272. case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */
  273. DEB2("BUS ERROR - Illegal START or STOP\n");
  274. pca_reset(adap);
  275. goto out;
  276. default:
  277. printk(KERN_ERR DRIVER ": unhandled SIO state 0x%02x\n", state);
  278. break;
  279. }
  280. }
  281. ret = curmsg;
  282. out:
  283. DEB1(KERN_CRIT "}}} transfered %d/%d messages. "
  284. "status is %#04x. control is %#04x\n",
  285. curmsg, num, pca_status(adap),
  286. pca_get_con(adap));
  287. return ret;
  288. }
  289. static u32 pca_func(struct i2c_adapter *adap)
  290. {
  291. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  292. }
  293. static int pca_init(struct i2c_algo_pca_data *adap)
  294. {
  295. static int freqs[] = {330,288,217,146,88,59,44,36};
  296. int own, clock;
  297. own = pca_own(adap);
  298. clock = pca_clock(adap);
  299. DEB1(KERN_INFO DRIVER ": own address is %#04x\n", own);
  300. DEB1(KERN_INFO DRIVER ": clock freqeuncy is %dkHz\n", freqs[clock]);
  301. pca_outw(adap, I2C_PCA_ADR, own << 1);
  302. pca_set_con(adap, I2C_PCA_CON_ENSIO | clock);
  303. udelay(500); /* 500 µs for oscilator to stabilise */
  304. return 0;
  305. }
  306. static struct i2c_algorithm pca_algo = {
  307. .name = "PCA9564 algorithm",
  308. .id = I2C_ALGO_PCA,
  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->id |= pca_algo.id;
  321. adap->algo = &pca_algo;
  322. adap->timeout = 100; /* default values, should */
  323. adap->retries = 3; /* be replaced by defines */
  324. rval = pca_init(pca_adap);
  325. if (!rval)
  326. i2c_add_adapter(adap);
  327. return rval;
  328. }
  329. int i2c_pca_del_bus(struct i2c_adapter *adap)
  330. {
  331. return i2c_del_adapter(adap);
  332. }
  333. EXPORT_SYMBOL(i2c_pca_add_bus);
  334. EXPORT_SYMBOL(i2c_pca_del_bus);
  335. MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
  336. MODULE_DESCRIPTION("I2C-Bus PCA9564 algorithm");
  337. MODULE_LICENSE("GPL");
  338. module_param(i2c_debug, int, 0);