i2c-riic.c 12 KB

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