i2c-algo-pca.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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/jiffies.h>
  25. #include <linux/init.h>
  26. #include <linux/errno.h>
  27. #include <linux/i2c.h>
  28. #include <linux/i2c-algo-pca.h>
  29. #define DEB1(fmt, args...) do { if (i2c_debug >= 1) \
  30. printk(KERN_DEBUG fmt, ## args); } while (0)
  31. #define DEB2(fmt, args...) do { if (i2c_debug >= 2) \
  32. printk(KERN_DEBUG fmt, ## args); } while (0)
  33. #define DEB3(fmt, args...) do { if (i2c_debug >= 3) \
  34. printk(KERN_DEBUG fmt, ## args); } while (0)
  35. static int i2c_debug;
  36. #define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)
  37. #define pca_inw(adap, reg) adap->read_byte(adap->data, reg)
  38. #define pca_status(adap) pca_inw(adap, I2C_PCA_STA)
  39. #define pca_clock(adap) adap->i2c_clock
  40. #define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)
  41. #define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)
  42. #define pca_wait(adap) adap->wait_for_completion(adap->data)
  43. #define pca_reset(adap) adap->reset_chip(adap->data)
  44. static void pca9665_reset(void *pd)
  45. {
  46. struct i2c_algo_pca_data *adap = pd;
  47. pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_IPRESET);
  48. pca_outw(adap, I2C_PCA_IND, 0xA5);
  49. pca_outw(adap, I2C_PCA_IND, 0x5A);
  50. }
  51. /*
  52. * Generate a start condition on the i2c bus.
  53. *
  54. * returns after the start condition has occurred
  55. */
  56. static int pca_start(struct i2c_algo_pca_data *adap)
  57. {
  58. int sta = pca_get_con(adap);
  59. DEB2("=== START\n");
  60. sta |= I2C_PCA_CON_STA;
  61. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
  62. pca_set_con(adap, sta);
  63. return pca_wait(adap);
  64. }
  65. /*
  66. * Generate a repeated start condition on the i2c bus
  67. *
  68. * return after the repeated start condition has occurred
  69. */
  70. static int pca_repeated_start(struct i2c_algo_pca_data *adap)
  71. {
  72. int sta = pca_get_con(adap);
  73. DEB2("=== REPEATED START\n");
  74. sta |= I2C_PCA_CON_STA;
  75. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
  76. pca_set_con(adap, sta);
  77. return pca_wait(adap);
  78. }
  79. /*
  80. * Generate a stop condition on the i2c bus
  81. *
  82. * returns after the stop condition has been generated
  83. *
  84. * STOPs do not generate an interrupt or set the SI flag, since the
  85. * part returns the idle state (0xf8). Hence we don't need to
  86. * pca_wait here.
  87. */
  88. static void pca_stop(struct i2c_algo_pca_data *adap)
  89. {
  90. int sta = pca_get_con(adap);
  91. DEB2("=== STOP\n");
  92. sta |= I2C_PCA_CON_STO;
  93. sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_SI);
  94. pca_set_con(adap, sta);
  95. }
  96. /*
  97. * Send the slave address and R/W bit
  98. *
  99. * returns after the address has been sent
  100. */
  101. static int pca_address(struct i2c_algo_pca_data *adap,
  102. struct i2c_msg *msg)
  103. {
  104. int sta = pca_get_con(adap);
  105. int addr;
  106. addr = ( (0x7f & msg->addr) << 1 );
  107. if (msg->flags & I2C_M_RD )
  108. addr |= 1;
  109. DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",
  110. msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);
  111. pca_outw(adap, I2C_PCA_DAT, addr);
  112. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
  113. pca_set_con(adap, sta);
  114. return pca_wait(adap);
  115. }
  116. /*
  117. * Transmit a byte.
  118. *
  119. * Returns after the byte has been transmitted
  120. */
  121. static int pca_tx_byte(struct i2c_algo_pca_data *adap,
  122. __u8 b)
  123. {
  124. int sta = pca_get_con(adap);
  125. DEB2("=== WRITE %#04x\n", b);
  126. pca_outw(adap, I2C_PCA_DAT, b);
  127. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
  128. pca_set_con(adap, sta);
  129. return pca_wait(adap);
  130. }
  131. /*
  132. * Receive a byte
  133. *
  134. * returns immediately.
  135. */
  136. static void pca_rx_byte(struct i2c_algo_pca_data *adap,
  137. __u8 *b, int ack)
  138. {
  139. *b = pca_inw(adap, I2C_PCA_DAT);
  140. DEB2("=== READ %#04x %s\n", *b, ack ? "ACK" : "NACK");
  141. }
  142. /*
  143. * Setup ACK or NACK for next received byte and wait for it to arrive.
  144. *
  145. * Returns after next byte has arrived.
  146. */
  147. static int pca_rx_ack(struct i2c_algo_pca_data *adap,
  148. int ack)
  149. {
  150. int sta = pca_get_con(adap);
  151. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI|I2C_PCA_CON_AA);
  152. if ( ack )
  153. sta |= I2C_PCA_CON_AA;
  154. pca_set_con(adap, sta);
  155. return pca_wait(adap);
  156. }
  157. static int pca_xfer(struct i2c_adapter *i2c_adap,
  158. struct i2c_msg *msgs,
  159. int num)
  160. {
  161. struct i2c_algo_pca_data *adap = i2c_adap->algo_data;
  162. struct i2c_msg *msg = NULL;
  163. int curmsg;
  164. int numbytes = 0;
  165. int state;
  166. int ret;
  167. int completed = 1;
  168. unsigned long timeout = jiffies + i2c_adap->timeout;
  169. while ((state = pca_status(adap)) != 0xf8) {
  170. if (time_before(jiffies, timeout)) {
  171. msleep(10);
  172. } else {
  173. dev_dbg(&i2c_adap->dev, "bus is not idle. status is "
  174. "%#04x\n", state);
  175. return -EAGAIN;
  176. }
  177. }
  178. DEB1("{{{ XFER %d messages\n", num);
  179. if (i2c_debug>=2) {
  180. for (curmsg = 0; curmsg < num; curmsg++) {
  181. int addr, i;
  182. msg = &msgs[curmsg];
  183. addr = (0x7f & msg->addr) ;
  184. if (msg->flags & I2C_M_RD )
  185. printk(KERN_INFO " [%02d] RD %d bytes from %#02x [%#02x, ...]\n",
  186. curmsg, msg->len, addr, (addr<<1) | 1);
  187. else {
  188. printk(KERN_INFO " [%02d] WR %d bytes to %#02x [%#02x%s",
  189. curmsg, msg->len, addr, addr<<1,
  190. msg->len == 0 ? "" : ", ");
  191. for(i=0; i < msg->len; i++)
  192. printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", ");
  193. printk("]\n");
  194. }
  195. }
  196. }
  197. curmsg = 0;
  198. ret = -EREMOTEIO;
  199. while (curmsg < num) {
  200. state = pca_status(adap);
  201. DEB3("STATE is 0x%02x\n", state);
  202. msg = &msgs[curmsg];
  203. switch (state) {
  204. case 0xf8: /* On reset or stop the bus is idle */
  205. completed = pca_start(adap);
  206. break;
  207. case 0x08: /* A START condition has been transmitted */
  208. case 0x10: /* A repeated start condition has been transmitted */
  209. completed = pca_address(adap, msg);
  210. break;
  211. case 0x18: /* SLA+W has been transmitted; ACK has been received */
  212. case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
  213. if (numbytes < msg->len) {
  214. completed = pca_tx_byte(adap,
  215. msg->buf[numbytes]);
  216. numbytes++;
  217. break;
  218. }
  219. curmsg++; numbytes = 0;
  220. if (curmsg == num)
  221. pca_stop(adap);
  222. else
  223. completed = pca_repeated_start(adap);
  224. break;
  225. case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
  226. DEB2("NOT ACK received after SLA+W\n");
  227. pca_stop(adap);
  228. goto out;
  229. case 0x40: /* SLA+R has been transmitted; ACK has been received */
  230. completed = pca_rx_ack(adap, msg->len > 1);
  231. break;
  232. case 0x50: /* Data bytes has been received; ACK has been returned */
  233. if (numbytes < msg->len) {
  234. pca_rx_byte(adap, &msg->buf[numbytes], 1);
  235. numbytes++;
  236. completed = pca_rx_ack(adap,
  237. numbytes < msg->len - 1);
  238. break;
  239. }
  240. curmsg++; numbytes = 0;
  241. if (curmsg == num)
  242. pca_stop(adap);
  243. else
  244. completed = pca_repeated_start(adap);
  245. break;
  246. case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
  247. DEB2("NOT ACK received after SLA+R\n");
  248. pca_stop(adap);
  249. goto out;
  250. case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
  251. DEB2("NOT ACK received after data byte\n");
  252. pca_stop(adap);
  253. goto out;
  254. case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
  255. DEB2("Arbitration lost\n");
  256. /*
  257. * The PCA9564 data sheet (2006-09-01) says "A
  258. * START condition will be transmitted when the
  259. * bus becomes free (STOP or SCL and SDA high)"
  260. * when the STA bit is set (p. 11).
  261. *
  262. * In case this won't work, try pca_reset()
  263. * instead.
  264. */
  265. pca_start(adap);
  266. goto out;
  267. case 0x58: /* Data byte has been received; NOT ACK has been returned */
  268. if ( numbytes == msg->len - 1 ) {
  269. pca_rx_byte(adap, &msg->buf[numbytes], 0);
  270. curmsg++; numbytes = 0;
  271. if (curmsg == num)
  272. pca_stop(adap);
  273. else
  274. completed = pca_repeated_start(adap);
  275. } else {
  276. DEB2("NOT ACK sent after data byte received. "
  277. "Not final byte. numbytes %d. len %d\n",
  278. numbytes, msg->len);
  279. pca_stop(adap);
  280. goto out;
  281. }
  282. break;
  283. case 0x70: /* Bus error - SDA stuck low */
  284. DEB2("BUS ERROR - SDA Stuck low\n");
  285. pca_reset(adap);
  286. goto out;
  287. case 0x90: /* Bus error - SCL stuck low */
  288. DEB2("BUS ERROR - SCL Stuck low\n");
  289. pca_reset(adap);
  290. goto out;
  291. case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */
  292. DEB2("BUS ERROR - Illegal START or STOP\n");
  293. pca_reset(adap);
  294. goto out;
  295. default:
  296. dev_err(&i2c_adap->dev, "unhandled SIO state 0x%02x\n", state);
  297. break;
  298. }
  299. if (!completed)
  300. goto out;
  301. }
  302. ret = curmsg;
  303. out:
  304. DEB1("}}} transfered %d/%d messages. "
  305. "status is %#04x. control is %#04x\n",
  306. curmsg, num, pca_status(adap),
  307. pca_get_con(adap));
  308. return ret;
  309. }
  310. static u32 pca_func(struct i2c_adapter *adap)
  311. {
  312. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  313. }
  314. static const struct i2c_algorithm pca_algo = {
  315. .master_xfer = pca_xfer,
  316. .functionality = pca_func,
  317. };
  318. static unsigned int pca_probe_chip(struct i2c_adapter *adap)
  319. {
  320. struct i2c_algo_pca_data *pca_data = adap->algo_data;
  321. /* The trick here is to check if there is an indirect register
  322. * available. If there is one, we will read the value we first
  323. * wrote on I2C_PCA_IADR. Otherwise, we will read the last value
  324. * we wrote on I2C_PCA_ADR
  325. */
  326. pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);
  327. pca_outw(pca_data, I2C_PCA_IND, 0xAA);
  328. pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ITO);
  329. pca_outw(pca_data, I2C_PCA_IND, 0x00);
  330. pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);
  331. if (pca_inw(pca_data, I2C_PCA_IND) == 0xAA) {
  332. printk(KERN_INFO "%s: PCA9665 detected.\n", adap->name);
  333. return I2C_PCA_CHIP_9665;
  334. } else {
  335. printk(KERN_INFO "%s: PCA9564 detected.\n", adap->name);
  336. return I2C_PCA_CHIP_9564;
  337. }
  338. }
  339. static int pca_init(struct i2c_adapter *adap)
  340. {
  341. struct i2c_algo_pca_data *pca_data = adap->algo_data;
  342. adap->algo = &pca_algo;
  343. if (pca_probe_chip(adap) == I2C_PCA_CHIP_9564) {
  344. static int freqs[] = {330, 288, 217, 146, 88, 59, 44, 36};
  345. int clock;
  346. if (pca_data->i2c_clock > 7) {
  347. switch (pca_data->i2c_clock) {
  348. case 330000:
  349. pca_data->i2c_clock = I2C_PCA_CON_330kHz;
  350. break;
  351. case 288000:
  352. pca_data->i2c_clock = I2C_PCA_CON_288kHz;
  353. break;
  354. case 217000:
  355. pca_data->i2c_clock = I2C_PCA_CON_217kHz;
  356. break;
  357. case 146000:
  358. pca_data->i2c_clock = I2C_PCA_CON_146kHz;
  359. break;
  360. case 88000:
  361. pca_data->i2c_clock = I2C_PCA_CON_88kHz;
  362. break;
  363. case 59000:
  364. pca_data->i2c_clock = I2C_PCA_CON_59kHz;
  365. break;
  366. case 44000:
  367. pca_data->i2c_clock = I2C_PCA_CON_44kHz;
  368. break;
  369. case 36000:
  370. pca_data->i2c_clock = I2C_PCA_CON_36kHz;
  371. break;
  372. default:
  373. printk(KERN_WARNING
  374. "%s: Invalid I2C clock speed selected."
  375. " Using default 59kHz.\n", adap->name);
  376. pca_data->i2c_clock = I2C_PCA_CON_59kHz;
  377. }
  378. } else {
  379. printk(KERN_WARNING "%s: "
  380. "Choosing the clock frequency based on "
  381. "index is deprecated."
  382. " Use the nominal frequency.\n", adap->name);
  383. }
  384. pca_reset(pca_data);
  385. clock = pca_clock(pca_data);
  386. printk(KERN_INFO "%s: Clock frequency is %dkHz\n",
  387. adap->name, freqs[clock]);
  388. pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock);
  389. } else {
  390. int clock;
  391. int mode;
  392. int tlow, thi;
  393. /* Values can be found on PCA9665 datasheet section 7.3.2.6 */
  394. int min_tlow, min_thi;
  395. /* These values are the maximum raise and fall values allowed
  396. * by the I2C operation mode (Standard, Fast or Fast+)
  397. * They are used (added) below to calculate the clock dividers
  398. * of PCA9665. Note that they are slightly different of the
  399. * real maximum, to allow the change on mode exactly on the
  400. * maximum clock rate for each mode
  401. */
  402. int raise_fall_time;
  403. struct i2c_algo_pca_data *pca_data = adap->algo_data;
  404. /* Ignore the reset function from the module,
  405. * we can use the parallel bus reset
  406. */
  407. pca_data->reset_chip = pca9665_reset;
  408. if (pca_data->i2c_clock > 1265800) {
  409. printk(KERN_WARNING "%s: I2C clock speed too high."
  410. " Using 1265.8kHz.\n", adap->name);
  411. pca_data->i2c_clock = 1265800;
  412. }
  413. if (pca_data->i2c_clock < 60300) {
  414. printk(KERN_WARNING "%s: I2C clock speed too low."
  415. " Using 60.3kHz.\n", adap->name);
  416. pca_data->i2c_clock = 60300;
  417. }
  418. /* To avoid integer overflow, use clock/100 for calculations */
  419. clock = pca_clock(pca_data) / 100;
  420. if (pca_data->i2c_clock > 10000) {
  421. mode = I2C_PCA_MODE_TURBO;
  422. min_tlow = 14;
  423. min_thi = 5;
  424. raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */
  425. } else if (pca_data->i2c_clock > 4000) {
  426. mode = I2C_PCA_MODE_FASTP;
  427. min_tlow = 17;
  428. min_thi = 9;
  429. raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */
  430. } else if (pca_data->i2c_clock > 1000) {
  431. mode = I2C_PCA_MODE_FAST;
  432. min_tlow = 44;
  433. min_thi = 20;
  434. raise_fall_time = 58; /* Raise 29e-8s, Fall 29e-8s */
  435. } else {
  436. mode = I2C_PCA_MODE_STD;
  437. min_tlow = 157;
  438. min_thi = 134;
  439. raise_fall_time = 127; /* Raise 29e-8s, Fall 98e-8s */
  440. }
  441. /* The minimum clock that respects the thi/tlow = 134/157 is
  442. * 64800 Hz. Below that, we have to fix the tlow to 255 and
  443. * calculate the thi factor.
  444. */
  445. if (clock < 648) {
  446. tlow = 255;
  447. thi = 1000000 - clock * raise_fall_time;
  448. thi /= (I2C_PCA_OSC_PER * clock) - tlow;
  449. } else {
  450. tlow = (1000000 - clock * raise_fall_time) * min_tlow;
  451. tlow /= I2C_PCA_OSC_PER * clock * (min_thi + min_tlow);
  452. thi = tlow * min_thi / min_tlow;
  453. }
  454. pca_reset(pca_data);
  455. printk(KERN_INFO
  456. "%s: Clock frequency is %dHz\n", adap->name, clock * 100);
  457. pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IMODE);
  458. pca_outw(pca_data, I2C_PCA_IND, mode);
  459. pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ISCLL);
  460. pca_outw(pca_data, I2C_PCA_IND, tlow);
  461. pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ISCLH);
  462. pca_outw(pca_data, I2C_PCA_IND, thi);
  463. pca_set_con(pca_data, I2C_PCA_CON_ENSIO);
  464. }
  465. udelay(500); /* 500 us for oscilator to stabilise */
  466. return 0;
  467. }
  468. /*
  469. * registering functions to load algorithms at runtime
  470. */
  471. int i2c_pca_add_bus(struct i2c_adapter *adap)
  472. {
  473. int rval;
  474. rval = pca_init(adap);
  475. if (rval)
  476. return rval;
  477. return i2c_add_adapter(adap);
  478. }
  479. EXPORT_SYMBOL(i2c_pca_add_bus);
  480. int i2c_pca_add_numbered_bus(struct i2c_adapter *adap)
  481. {
  482. int rval;
  483. rval = pca_init(adap);
  484. if (rval)
  485. return rval;
  486. return i2c_add_numbered_adapter(adap);
  487. }
  488. EXPORT_SYMBOL(i2c_pca_add_numbered_bus);
  489. MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>, "
  490. "Wolfram Sang <w.sang@pengutronix.de>");
  491. MODULE_DESCRIPTION("I2C-Bus PCA9564/PCA9665 algorithm");
  492. MODULE_LICENSE("GPL");
  493. module_param(i2c_debug, int, 0);