i2c-riic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Renesas RIIC driver
  3. *
  4. * Copyright (C) 2013 Wolfram Sang <wsa@sang-engineering.com>
  5. * Copyright (C) 2013 Renesas Solutions Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. /*
  12. * This i2c core has a lot of interrupts, namely 8. We use their chaining as
  13. * some kind of state machine.
  14. *
  15. * 1) The main xfer routine kicks off a transmission by putting the start bit
  16. * (or repeated start) on the bus and enabling the transmit interrupt (TIE)
  17. * since we need to send the slave address + RW bit in every case.
  18. *
  19. * 2) TIE sends slave address + RW bit and selects how to continue.
  20. *
  21. * 3a) Write case: We keep utilizing TIE as long as we have data to send. If we
  22. * are done, we switch over to the transmission done interrupt (TEIE) and mark
  23. * the message as completed (includes sending STOP) there.
  24. *
  25. * 3b) Read case: We switch over to receive interrupt (RIE). One dummy read is
  26. * needed to start clocking, then we keep receiving until we are done. Note
  27. * that we use the RDRFS mode all the time, i.e. we ACK/NACK every byte by
  28. * writing to the ACKBT bit. I tried using the RDRFS mode only at the end of a
  29. * message to create the final NACK as sketched in the datasheet. This caused
  30. * some subtle races (when byte n was processed and byte n+1 was already
  31. * waiting), though, and I started with the safe approach.
  32. *
  33. * 4) If we got a NACK somewhere, we flag the error and stop the transmission
  34. * via NAKIE.
  35. *
  36. * Also check the comments in the interrupt routines for some gory details.
  37. */
  38. #include <linux/clk.h>
  39. #include <linux/completion.h>
  40. #include <linux/err.h>
  41. #include <linux/i2c.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/io.h>
  44. #include <linux/module.h>
  45. #include <linux/of.h>
  46. #include <linux/platform_device.h>
  47. #define RIIC_ICCR1 0x00
  48. #define RIIC_ICCR2 0x04
  49. #define RIIC_ICMR1 0x08
  50. #define RIIC_ICMR3 0x10
  51. #define RIIC_ICSER 0x18
  52. #define RIIC_ICIER 0x1c
  53. #define RIIC_ICSR2 0x24
  54. #define RIIC_ICBRL 0x34
  55. #define RIIC_ICBRH 0x38
  56. #define RIIC_ICDRT 0x3c
  57. #define RIIC_ICDRR 0x40
  58. #define ICCR1_ICE 0x80
  59. #define ICCR1_IICRST 0x40
  60. #define ICCR1_SOWP 0x10
  61. #define ICCR2_BBSY 0x80
  62. #define ICCR2_SP 0x08
  63. #define ICCR2_RS 0x04
  64. #define ICCR2_ST 0x02
  65. #define ICMR1_CKS_MASK 0x70
  66. #define ICMR1_BCWP 0x08
  67. #define ICMR1_CKS(_x) ((((_x) << 4) & ICMR1_CKS_MASK) | ICMR1_BCWP)
  68. #define ICMR3_RDRFS 0x20
  69. #define ICMR3_ACKWP 0x10
  70. #define ICMR3_ACKBT 0x08
  71. #define ICIER_TIE 0x80
  72. #define ICIER_TEIE 0x40
  73. #define ICIER_RIE 0x20
  74. #define ICIER_NAKIE 0x10
  75. #define ICIER_SPIE 0x08
  76. #define ICSR2_NACKF 0x10
  77. #define ICBR_RESERVED 0xe0 /* Should be 1 on writes */
  78. #define RIIC_INIT_MSG -1
  79. struct riic_dev {
  80. void __iomem *base;
  81. u8 *buf;
  82. struct i2c_msg *msg;
  83. int bytes_left;
  84. int err;
  85. int is_last;
  86. struct completion msg_done;
  87. struct i2c_adapter adapter;
  88. struct clk *clk;
  89. };
  90. struct riic_irq_desc {
  91. int res_num;
  92. irq_handler_t isr;
  93. char *name;
  94. };
  95. static inline void riic_clear_set_bit(struct riic_dev *riic, u8 clear, u8 set, u8 reg)
  96. {
  97. writeb((readb(riic->base + reg) & ~clear) | set, riic->base + reg);
  98. }
  99. static int riic_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  100. {
  101. struct riic_dev *riic = i2c_get_adapdata(adap);
  102. unsigned long time_left;
  103. int i, ret;
  104. u8 start_bit;
  105. ret = clk_prepare_enable(riic->clk);
  106. if (ret)
  107. return ret;
  108. if (readb(riic->base + RIIC_ICCR2) & ICCR2_BBSY) {
  109. riic->err = -EBUSY;
  110. goto out;
  111. }
  112. reinit_completion(&riic->msg_done);
  113. riic->err = 0;
  114. writeb(0, riic->base + RIIC_ICSR2);
  115. for (i = 0, start_bit = ICCR2_ST; i < num; i++) {
  116. riic->bytes_left = RIIC_INIT_MSG;
  117. riic->buf = msgs[i].buf;
  118. riic->msg = &msgs[i];
  119. riic->is_last = (i == num - 1);
  120. writeb(ICIER_NAKIE | ICIER_TIE, riic->base + RIIC_ICIER);
  121. writeb(start_bit, riic->base + RIIC_ICCR2);
  122. time_left = wait_for_completion_timeout(&riic->msg_done, riic->adapter.timeout);
  123. if (time_left == 0)
  124. riic->err = -ETIMEDOUT;
  125. if (riic->err)
  126. break;
  127. start_bit = ICCR2_RS;
  128. }
  129. out:
  130. clk_disable_unprepare(riic->clk);
  131. return riic->err ?: num;
  132. }
  133. static irqreturn_t riic_tdre_isr(int irq, void *data)
  134. {
  135. struct riic_dev *riic = data;
  136. u8 val;
  137. if (!riic->bytes_left)
  138. return IRQ_NONE;
  139. if (riic->bytes_left == RIIC_INIT_MSG) {
  140. val = !!(riic->msg->flags & I2C_M_RD);
  141. if (val)
  142. /* On read, switch over to receive interrupt */
  143. riic_clear_set_bit(riic, ICIER_TIE, ICIER_RIE, RIIC_ICIER);
  144. else
  145. /* On write, initialize length */
  146. riic->bytes_left = riic->msg->len;
  147. val |= (riic->msg->addr << 1);
  148. } else {
  149. val = *riic->buf;
  150. riic->buf++;
  151. riic->bytes_left--;
  152. }
  153. /*
  154. * Switch to transmission ended interrupt when done. Do check here
  155. * after bytes_left was initialized to support SMBUS_QUICK (new msg has
  156. * 0 length then)
  157. */
  158. if (riic->bytes_left == 0)
  159. riic_clear_set_bit(riic, ICIER_TIE, ICIER_TEIE, RIIC_ICIER);
  160. /*
  161. * This acks the TIE interrupt. We get another TIE immediately if our
  162. * value could be moved to the shadow shift register right away. So
  163. * this must be after updates to ICIER (where we want to disable TIE)!
  164. */
  165. writeb(val, riic->base + RIIC_ICDRT);
  166. return IRQ_HANDLED;
  167. }
  168. static irqreturn_t riic_tend_isr(int irq, void *data)
  169. {
  170. struct riic_dev *riic = data;
  171. if (readb(riic->base + RIIC_ICSR2) & ICSR2_NACKF) {
  172. /* We got a NACKIE */
  173. readb(riic->base + RIIC_ICDRR); /* dummy read */
  174. riic->err = -ENXIO;
  175. } else if (riic->bytes_left) {
  176. return IRQ_NONE;
  177. }
  178. if (riic->is_last || riic->err) {
  179. riic_clear_set_bit(riic, ICIER_TEIE, ICIER_SPIE, RIIC_ICIER);
  180. writeb(ICCR2_SP, riic->base + RIIC_ICCR2);
  181. } else {
  182. /* Transfer is complete, but do not send STOP */
  183. riic_clear_set_bit(riic, ICIER_TEIE, 0, RIIC_ICIER);
  184. complete(&riic->msg_done);
  185. }
  186. return IRQ_HANDLED;
  187. }
  188. static irqreturn_t riic_rdrf_isr(int irq, void *data)
  189. {
  190. struct riic_dev *riic = data;
  191. if (!riic->bytes_left)
  192. return IRQ_NONE;
  193. if (riic->bytes_left == RIIC_INIT_MSG) {
  194. riic->bytes_left = riic->msg->len;
  195. readb(riic->base + RIIC_ICDRR); /* dummy read */
  196. return IRQ_HANDLED;
  197. }
  198. if (riic->bytes_left == 1) {
  199. /* STOP must come before we set ACKBT! */
  200. if (riic->is_last) {
  201. riic_clear_set_bit(riic, 0, ICIER_SPIE, RIIC_ICIER);
  202. writeb(ICCR2_SP, riic->base + RIIC_ICCR2);
  203. }
  204. riic_clear_set_bit(riic, 0, ICMR3_ACKBT, RIIC_ICMR3);
  205. } else {
  206. riic_clear_set_bit(riic, ICMR3_ACKBT, 0, RIIC_ICMR3);
  207. }
  208. /* Reading acks the RIE interrupt */
  209. *riic->buf = readb(riic->base + RIIC_ICDRR);
  210. riic->buf++;
  211. riic->bytes_left--;
  212. return IRQ_HANDLED;
  213. }
  214. static irqreturn_t riic_stop_isr(int irq, void *data)
  215. {
  216. struct riic_dev *riic = data;
  217. /* read back registers to confirm writes have fully propagated */
  218. writeb(0, riic->base + RIIC_ICSR2);
  219. readb(riic->base + RIIC_ICSR2);
  220. writeb(0, riic->base + RIIC_ICIER);
  221. readb(riic->base + RIIC_ICIER);
  222. complete(&riic->msg_done);
  223. return IRQ_HANDLED;
  224. }
  225. static u32 riic_func(struct i2c_adapter *adap)
  226. {
  227. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  228. }
  229. static const struct i2c_algorithm riic_algo = {
  230. .master_xfer = riic_xfer,
  231. .functionality = riic_func,
  232. };
  233. static int riic_init_hw(struct riic_dev *riic, struct i2c_timings *t)
  234. {
  235. int ret;
  236. unsigned long rate;
  237. int total_ticks, cks, brl, brh;
  238. ret = clk_prepare_enable(riic->clk);
  239. if (ret)
  240. return ret;
  241. if (t->bus_freq_hz > 400000) {
  242. dev_err(&riic->adapter.dev,
  243. "unsupported bus speed (%dHz). 400000 max\n",
  244. t->bus_freq_hz);
  245. clk_disable_unprepare(riic->clk);
  246. return -EINVAL;
  247. }
  248. rate = clk_get_rate(riic->clk);
  249. /*
  250. * Assume the default register settings:
  251. * FER.SCLE = 1 (SCL sync circuit enabled, adds 2 or 3 cycles)
  252. * FER.NFE = 1 (noise circuit enabled)
  253. * MR3.NF = 0 (1 cycle of noise filtered out)
  254. *
  255. * Freq (CKS=000) = (I2CCLK + tr + tf)/ (BRH + 3 + 1) + (BRL + 3 + 1)
  256. * Freq (CKS!=000) = (I2CCLK + tr + tf)/ (BRH + 2 + 1) + (BRL + 2 + 1)
  257. */
  258. /*
  259. * Determine reference clock rate. We must be able to get the desired
  260. * frequency with only 62 clock ticks max (31 high, 31 low).
  261. * Aim for a duty of 60% LOW, 40% HIGH.
  262. */
  263. total_ticks = DIV_ROUND_UP(rate, t->bus_freq_hz);
  264. for (cks = 0; cks < 7; cks++) {
  265. /*
  266. * 60% low time must be less than BRL + 2 + 1
  267. * BRL max register value is 0x1F.
  268. */
  269. brl = ((total_ticks * 6) / 10);
  270. if (brl <= (0x1F + 3))
  271. break;
  272. total_ticks /= 2;
  273. rate /= 2;
  274. }
  275. if (brl > (0x1F + 3)) {
  276. dev_err(&riic->adapter.dev, "invalid speed (%lu). Too slow.\n",
  277. (unsigned long)t->bus_freq_hz);
  278. clk_disable_unprepare(riic->clk);
  279. return -EINVAL;
  280. }
  281. brh = total_ticks - brl;
  282. /* Remove automatic clock ticks for sync circuit and NF */
  283. if (cks == 0) {
  284. brl -= 4;
  285. brh -= 4;
  286. } else {
  287. brl -= 3;
  288. brh -= 3;
  289. }
  290. /*
  291. * Remove clock ticks for rise and fall times. Convert ns to clock
  292. * ticks.
  293. */
  294. brl -= t->scl_fall_ns / (1000000000 / rate);
  295. brh -= t->scl_rise_ns / (1000000000 / rate);
  296. /* Adjust for min register values for when SCLE=1 and NFE=1 */
  297. if (brl < 1)
  298. brl = 1;
  299. if (brh < 1)
  300. brh = 1;
  301. pr_debug("i2c-riic: freq=%lu, duty=%d, fall=%lu, rise=%lu, cks=%d, brl=%d, brh=%d\n",
  302. rate / total_ticks, ((brl + 3) * 100) / (brl + brh + 6),
  303. t->scl_fall_ns / (1000000000 / rate),
  304. t->scl_rise_ns / (1000000000 / rate), cks, brl, brh);
  305. /* Changing the order of accessing IICRST and ICE may break things! */
  306. writeb(ICCR1_IICRST | ICCR1_SOWP, riic->base + RIIC_ICCR1);
  307. riic_clear_set_bit(riic, 0, ICCR1_ICE, RIIC_ICCR1);
  308. writeb(ICMR1_CKS(cks), riic->base + RIIC_ICMR1);
  309. writeb(brh | ICBR_RESERVED, riic->base + RIIC_ICBRH);
  310. writeb(brl | ICBR_RESERVED, riic->base + RIIC_ICBRL);
  311. writeb(0, riic->base + RIIC_ICSER);
  312. writeb(ICMR3_ACKWP | ICMR3_RDRFS, riic->base + RIIC_ICMR3);
  313. riic_clear_set_bit(riic, ICCR1_IICRST, 0, RIIC_ICCR1);
  314. clk_disable_unprepare(riic->clk);
  315. return 0;
  316. }
  317. static struct riic_irq_desc riic_irqs[] = {
  318. { .res_num = 0, .isr = riic_tend_isr, .name = "riic-tend" },
  319. { .res_num = 1, .isr = riic_rdrf_isr, .name = "riic-rdrf" },
  320. { .res_num = 2, .isr = riic_tdre_isr, .name = "riic-tdre" },
  321. { .res_num = 3, .isr = riic_stop_isr, .name = "riic-stop" },
  322. { .res_num = 5, .isr = riic_tend_isr, .name = "riic-nack" },
  323. };
  324. static int riic_i2c_probe(struct platform_device *pdev)
  325. {
  326. struct riic_dev *riic;
  327. struct i2c_adapter *adap;
  328. struct resource *res;
  329. struct i2c_timings i2c_t;
  330. int i, ret;
  331. riic = devm_kzalloc(&pdev->dev, sizeof(*riic), GFP_KERNEL);
  332. if (!riic)
  333. return -ENOMEM;
  334. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  335. riic->base = devm_ioremap_resource(&pdev->dev, res);
  336. if (IS_ERR(riic->base))
  337. return PTR_ERR(riic->base);
  338. riic->clk = devm_clk_get(&pdev->dev, NULL);
  339. if (IS_ERR(riic->clk)) {
  340. dev_err(&pdev->dev, "missing controller clock");
  341. return PTR_ERR(riic->clk);
  342. }
  343. for (i = 0; i < ARRAY_SIZE(riic_irqs); i++) {
  344. res = platform_get_resource(pdev, IORESOURCE_IRQ, riic_irqs[i].res_num);
  345. if (!res)
  346. return -ENODEV;
  347. ret = devm_request_irq(&pdev->dev, res->start, riic_irqs[i].isr,
  348. 0, riic_irqs[i].name, riic);
  349. if (ret) {
  350. dev_err(&pdev->dev, "failed to request irq %s\n", riic_irqs[i].name);
  351. return ret;
  352. }
  353. }
  354. adap = &riic->adapter;
  355. i2c_set_adapdata(adap, riic);
  356. strlcpy(adap->name, "Renesas RIIC adapter", sizeof(adap->name));
  357. adap->owner = THIS_MODULE;
  358. adap->algo = &riic_algo;
  359. adap->dev.parent = &pdev->dev;
  360. adap->dev.of_node = pdev->dev.of_node;
  361. init_completion(&riic->msg_done);
  362. i2c_parse_fw_timings(&pdev->dev, &i2c_t, true);
  363. ret = riic_init_hw(riic, &i2c_t);
  364. if (ret)
  365. return ret;
  366. ret = i2c_add_adapter(adap);
  367. if (ret)
  368. return ret;
  369. platform_set_drvdata(pdev, riic);
  370. dev_info(&pdev->dev, "registered with %dHz bus speed\n",
  371. i2c_t.bus_freq_hz);
  372. return 0;
  373. }
  374. static int riic_i2c_remove(struct platform_device *pdev)
  375. {
  376. struct riic_dev *riic = platform_get_drvdata(pdev);
  377. writeb(0, riic->base + RIIC_ICIER);
  378. i2c_del_adapter(&riic->adapter);
  379. return 0;
  380. }
  381. static const struct of_device_id riic_i2c_dt_ids[] = {
  382. { .compatible = "renesas,riic-rz" },
  383. { /* Sentinel */ },
  384. };
  385. static struct platform_driver riic_i2c_driver = {
  386. .probe = riic_i2c_probe,
  387. .remove = riic_i2c_remove,
  388. .driver = {
  389. .name = "i2c-riic",
  390. .of_match_table = riic_i2c_dt_ids,
  391. },
  392. };
  393. module_platform_driver(riic_i2c_driver);
  394. MODULE_DESCRIPTION("Renesas RIIC adapter");
  395. MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
  396. MODULE_LICENSE("GPL v2");
  397. MODULE_DEVICE_TABLE(of, riic_i2c_dt_ids);