i2c-algo-pca.c 15 KB

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