i2c-algo-pca.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters
  4. * Copyright (C) 2004 Arcom Control Systems
  5. * Copyright (C) 2008 Pengutronix
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/delay.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/errno.h>
  13. #include <linux/i2c.h>
  14. #include <linux/i2c-algo-pca.h>
  15. #define DEB1(fmt, args...) do { if (i2c_debug >= 1) \
  16. printk(KERN_DEBUG fmt, ## args); } while (0)
  17. #define DEB2(fmt, args...) do { if (i2c_debug >= 2) \
  18. printk(KERN_DEBUG fmt, ## args); } while (0)
  19. #define DEB3(fmt, args...) do { if (i2c_debug >= 3) \
  20. printk(KERN_DEBUG fmt, ## args); } while (0)
  21. static int i2c_debug;
  22. #define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)
  23. #define pca_inw(adap, reg) adap->read_byte(adap->data, reg)
  24. #define pca_status(adap) pca_inw(adap, I2C_PCA_STA)
  25. #define pca_clock(adap) adap->i2c_clock
  26. #define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)
  27. #define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)
  28. #define pca_wait(adap) adap->wait_for_completion(adap->data)
  29. static void pca_reset(struct i2c_algo_pca_data *adap)
  30. {
  31. if (adap->chip == I2C_PCA_CHIP_9665) {
  32. /* Ignore the reset function from the module,
  33. * we can use the parallel bus reset.
  34. */
  35. pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_IPRESET);
  36. pca_outw(adap, I2C_PCA_IND, 0xA5);
  37. pca_outw(adap, I2C_PCA_IND, 0x5A);
  38. /*
  39. * After a reset we need to re-apply any configuration
  40. * (calculated in pca_init) to get the bus in a working state.
  41. */
  42. pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_IMODE);
  43. pca_outw(adap, I2C_PCA_IND, adap->bus_settings.mode);
  44. pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_ISCLL);
  45. pca_outw(adap, I2C_PCA_IND, adap->bus_settings.tlow);
  46. pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_ISCLH);
  47. pca_outw(adap, I2C_PCA_IND, adap->bus_settings.thi);
  48. pca_set_con(adap, I2C_PCA_CON_ENSIO);
  49. } else {
  50. adap->reset_chip(adap->data);
  51. pca_set_con(adap, I2C_PCA_CON_ENSIO | adap->bus_settings.clock_freq);
  52. }
  53. }
  54. /*
  55. * Generate a start condition on the i2c bus.
  56. *
  57. * returns after the start condition has occurred
  58. */
  59. static int pca_start(struct i2c_algo_pca_data *adap)
  60. {
  61. int sta = pca_get_con(adap);
  62. DEB2("=== START\n");
  63. sta |= I2C_PCA_CON_STA;
  64. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
  65. pca_set_con(adap, sta);
  66. return pca_wait(adap);
  67. }
  68. /*
  69. * Generate a repeated start condition on the i2c bus
  70. *
  71. * return after the repeated start condition has occurred
  72. */
  73. static int pca_repeated_start(struct i2c_algo_pca_data *adap)
  74. {
  75. int sta = pca_get_con(adap);
  76. DEB2("=== REPEATED START\n");
  77. sta |= I2C_PCA_CON_STA;
  78. sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
  79. pca_set_con(adap, sta);
  80. return pca_wait(adap);
  81. }
  82. /*
  83. * Generate a stop condition on the i2c bus
  84. *
  85. * returns after the stop condition has been generated
  86. *
  87. * STOPs do not generate an interrupt or set the SI flag, since the
  88. * part returns the idle state (0xf8). Hence we don't need to
  89. * pca_wait here.
  90. */
  91. static void pca_stop(struct i2c_algo_pca_data *adap)
  92. {
  93. int sta = pca_get_con(adap);
  94. DEB2("=== STOP\n");
  95. sta |= I2C_PCA_CON_STO;
  96. sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_SI);
  97. pca_set_con(adap, sta);
  98. }
  99. /*
  100. * Send the slave address and R/W bit
  101. *
  102. * returns after the address has been sent
  103. */
  104. static int pca_address(struct i2c_algo_pca_data *adap,
  105. struct i2c_msg *msg)
  106. {
  107. int sta = pca_get_con(adap);
  108. int addr = i2c_8bit_addr_from_msg(msg);
  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 -EBUSY;
  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 = -EIO;
  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. ret = -ENXIO;
  229. goto out;
  230. case 0x40: /* SLA+R has been transmitted; ACK has been received */
  231. completed = pca_rx_ack(adap, msg->len > 1);
  232. break;
  233. case 0x50: /* Data bytes has been received; ACK has been returned */
  234. if (numbytes < msg->len) {
  235. pca_rx_byte(adap, &msg->buf[numbytes], 1);
  236. numbytes++;
  237. completed = pca_rx_ack(adap,
  238. numbytes < msg->len - 1);
  239. break;
  240. }
  241. curmsg++; numbytes = 0;
  242. if (curmsg == num)
  243. pca_stop(adap);
  244. else
  245. completed = pca_repeated_start(adap);
  246. break;
  247. case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
  248. DEB2("NOT ACK received after SLA+R\n");
  249. pca_stop(adap);
  250. ret = -ENXIO;
  251. goto out;
  252. case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
  253. DEB2("NOT ACK received after data byte\n");
  254. pca_stop(adap);
  255. goto out;
  256. case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
  257. DEB2("Arbitration lost\n");
  258. /*
  259. * The PCA9564 data sheet (2006-09-01) says "A
  260. * START condition will be transmitted when the
  261. * bus becomes free (STOP or SCL and SDA high)"
  262. * when the STA bit is set (p. 11).
  263. *
  264. * In case this won't work, try pca_reset()
  265. * instead.
  266. */
  267. pca_start(adap);
  268. goto out;
  269. case 0x58: /* Data byte has been received; NOT ACK has been returned */
  270. if (numbytes == msg->len - 1) {
  271. pca_rx_byte(adap, &msg->buf[numbytes], 0);
  272. curmsg++; numbytes = 0;
  273. if (curmsg == num)
  274. pca_stop(adap);
  275. else
  276. completed = pca_repeated_start(adap);
  277. } else {
  278. DEB2("NOT ACK sent after data byte received. "
  279. "Not final byte. numbytes %d. len %d\n",
  280. numbytes, msg->len);
  281. pca_stop(adap);
  282. goto out;
  283. }
  284. break;
  285. case 0x70: /* Bus error - SDA stuck low */
  286. DEB2("BUS ERROR - SDA Stuck low\n");
  287. pca_reset(adap);
  288. goto out;
  289. case 0x78: /* Bus error - SCL stuck low (PCA9665) */
  290. case 0x90: /* Bus error - SCL stuck low (PCA9564) */
  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. pca_data->chip = I2C_PCA_CHIP_9665;
  337. } else {
  338. printk(KERN_INFO "%s: PCA9564 detected.\n", adap->name);
  339. pca_data->chip = I2C_PCA_CHIP_9564;
  340. }
  341. return pca_data->chip;
  342. }
  343. static int pca_init(struct i2c_adapter *adap)
  344. {
  345. struct i2c_algo_pca_data *pca_data = adap->algo_data;
  346. adap->algo = &pca_algo;
  347. if (pca_probe_chip(adap) == I2C_PCA_CHIP_9564) {
  348. static int freqs[] = {330, 288, 217, 146, 88, 59, 44, 36};
  349. int clock;
  350. if (pca_data->i2c_clock > 7) {
  351. switch (pca_data->i2c_clock) {
  352. case 330000:
  353. pca_data->i2c_clock = I2C_PCA_CON_330kHz;
  354. break;
  355. case 288000:
  356. pca_data->i2c_clock = I2C_PCA_CON_288kHz;
  357. break;
  358. case 217000:
  359. pca_data->i2c_clock = I2C_PCA_CON_217kHz;
  360. break;
  361. case 146000:
  362. pca_data->i2c_clock = I2C_PCA_CON_146kHz;
  363. break;
  364. case 88000:
  365. pca_data->i2c_clock = I2C_PCA_CON_88kHz;
  366. break;
  367. case 59000:
  368. pca_data->i2c_clock = I2C_PCA_CON_59kHz;
  369. break;
  370. case 44000:
  371. pca_data->i2c_clock = I2C_PCA_CON_44kHz;
  372. break;
  373. case 36000:
  374. pca_data->i2c_clock = I2C_PCA_CON_36kHz;
  375. break;
  376. default:
  377. printk(KERN_WARNING
  378. "%s: Invalid I2C clock speed selected."
  379. " Using default 59kHz.\n", adap->name);
  380. pca_data->i2c_clock = I2C_PCA_CON_59kHz;
  381. }
  382. } else {
  383. printk(KERN_WARNING "%s: "
  384. "Choosing the clock frequency based on "
  385. "index is deprecated."
  386. " Use the nominal frequency.\n", adap->name);
  387. }
  388. clock = pca_clock(pca_data);
  389. printk(KERN_INFO "%s: Clock frequency is %dkHz\n",
  390. adap->name, freqs[clock]);
  391. /* Store settings as these will be needed when the PCA chip is reset */
  392. pca_data->bus_settings.clock_freq = clock;
  393. pca_reset(pca_data);
  394. } else {
  395. int clock;
  396. int mode;
  397. int tlow, thi;
  398. /* Values can be found on PCA9665 datasheet section 7.3.2.6 */
  399. int min_tlow, min_thi;
  400. /* These values are the maximum raise and fall values allowed
  401. * by the I2C operation mode (Standard, Fast or Fast+)
  402. * They are used (added) below to calculate the clock dividers
  403. * of PCA9665. Note that they are slightly different of the
  404. * real maximum, to allow the change on mode exactly on the
  405. * maximum clock rate for each mode
  406. */
  407. int raise_fall_time;
  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 > I2C_MAX_FAST_MODE_PLUS_FREQ) {
  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 > I2C_MAX_FAST_MODE_FREQ) {
  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 > I2C_MAX_STANDARD_MODE_FREQ) {
  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. /* Store settings as these will be needed when the PCA chip is reset */
  455. pca_data->bus_settings.mode = mode;
  456. pca_data->bus_settings.tlow = tlow;
  457. pca_data->bus_settings.thi = thi;
  458. pca_reset(pca_data);
  459. printk(KERN_INFO
  460. "%s: Clock frequency is %dHz\n", adap->name, clock * 100);
  461. }
  462. udelay(500); /* 500 us for oscillator to stabilise */
  463. return 0;
  464. }
  465. /*
  466. * registering functions to load algorithms at runtime
  467. */
  468. int i2c_pca_add_bus(struct i2c_adapter *adap)
  469. {
  470. int rval;
  471. rval = pca_init(adap);
  472. if (rval)
  473. return rval;
  474. return i2c_add_adapter(adap);
  475. }
  476. EXPORT_SYMBOL(i2c_pca_add_bus);
  477. int i2c_pca_add_numbered_bus(struct i2c_adapter *adap)
  478. {
  479. int rval;
  480. rval = pca_init(adap);
  481. if (rval)
  482. return rval;
  483. return i2c_add_numbered_adapter(adap);
  484. }
  485. EXPORT_SYMBOL(i2c_pca_add_numbered_bus);
  486. MODULE_AUTHOR("Ian Campbell <[email protected]>");
  487. MODULE_AUTHOR("Wolfram Sang <[email protected]>");
  488. MODULE_DESCRIPTION("I2C-Bus PCA9564/PCA9665 algorithm");
  489. MODULE_LICENSE("GPL");
  490. module_param(i2c_debug, int, 0);