i2c-algo-pca.c 15 KB

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