i2c-algo-pca.c 15 KB

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