i2c-riic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 ICSR2_NACKF 0x10
  76. /* ICBRx (@ PCLK 33MHz) */
  77. #define ICBR_RESERVED 0xe0 /* Should be 1 on writes */
  78. #define ICBRL_SP100K (19 | ICBR_RESERVED)
  79. #define ICBRH_SP100K (16 | ICBR_RESERVED)
  80. #define ICBRL_SP400K (21 | ICBR_RESERVED)
  81. #define ICBRH_SP400K (9 | ICBR_RESERVED)
  82. #define RIIC_INIT_MSG -1
  83. struct riic_dev {
  84. void __iomem *base;
  85. u8 *buf;
  86. struct i2c_msg *msg;
  87. int bytes_left;
  88. int err;
  89. int is_last;
  90. struct completion msg_done;
  91. struct i2c_adapter adapter;
  92. struct clk *clk;
  93. };
  94. struct riic_irq_desc {
  95. int res_num;
  96. irq_handler_t isr;
  97. char *name;
  98. };
  99. static inline void riic_clear_set_bit(struct riic_dev *riic, u8 clear, u8 set, u8 reg)
  100. {
  101. writeb((readb(riic->base + reg) & ~clear) | set, riic->base + reg);
  102. }
  103. static int riic_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  104. {
  105. struct riic_dev *riic = i2c_get_adapdata(adap);
  106. unsigned long time_left;
  107. int i, ret;
  108. u8 start_bit;
  109. ret = clk_prepare_enable(riic->clk);
  110. if (ret)
  111. return ret;
  112. if (readb(riic->base + RIIC_ICCR2) & ICCR2_BBSY) {
  113. riic->err = -EBUSY;
  114. goto out;
  115. }
  116. reinit_completion(&riic->msg_done);
  117. riic->err = 0;
  118. writeb(0, riic->base + RIIC_ICSR2);
  119. for (i = 0, start_bit = ICCR2_ST; i < num; i++) {
  120. riic->bytes_left = RIIC_INIT_MSG;
  121. riic->buf = msgs[i].buf;
  122. riic->msg = &msgs[i];
  123. riic->is_last = (i == num - 1);
  124. writeb(ICIER_NAKIE | ICIER_TIE, riic->base + RIIC_ICIER);
  125. writeb(start_bit, riic->base + RIIC_ICCR2);
  126. time_left = wait_for_completion_timeout(&riic->msg_done, riic->adapter.timeout);
  127. if (time_left == 0)
  128. riic->err = -ETIMEDOUT;
  129. if (riic->err)
  130. break;
  131. start_bit = ICCR2_RS;
  132. }
  133. out:
  134. clk_disable_unprepare(riic->clk);
  135. return riic->err ?: num;
  136. }
  137. static irqreturn_t riic_tdre_isr(int irq, void *data)
  138. {
  139. struct riic_dev *riic = data;
  140. u8 val;
  141. if (!riic->bytes_left)
  142. return IRQ_NONE;
  143. if (riic->bytes_left == RIIC_INIT_MSG) {
  144. val = !!(riic->msg->flags & I2C_M_RD);
  145. if (val)
  146. /* On read, switch over to receive interrupt */
  147. riic_clear_set_bit(riic, ICIER_TIE, ICIER_RIE, RIIC_ICIER);
  148. else
  149. /* On write, initialize length */
  150. riic->bytes_left = riic->msg->len;
  151. val |= (riic->msg->addr << 1);
  152. } else {
  153. val = *riic->buf;
  154. riic->buf++;
  155. riic->bytes_left--;
  156. }
  157. /*
  158. * Switch to transmission ended interrupt when done. Do check here
  159. * after bytes_left was initialized to support SMBUS_QUICK (new msg has
  160. * 0 length then)
  161. */
  162. if (riic->bytes_left == 0)
  163. riic_clear_set_bit(riic, ICIER_TIE, ICIER_TEIE, RIIC_ICIER);
  164. /*
  165. * This acks the TIE interrupt. We get another TIE immediately if our
  166. * value could be moved to the shadow shift register right away. So
  167. * this must be after updates to ICIER (where we want to disable TIE)!
  168. */
  169. writeb(val, riic->base + RIIC_ICDRT);
  170. return IRQ_HANDLED;
  171. }
  172. static irqreturn_t riic_tend_isr(int irq, void *data)
  173. {
  174. struct riic_dev *riic = data;
  175. if (readb(riic->base + RIIC_ICSR2) & ICSR2_NACKF) {
  176. /* We got a NACKIE */
  177. readb(riic->base + RIIC_ICDRR); /* dummy read */
  178. riic->err = -ENXIO;
  179. } else if (riic->bytes_left) {
  180. return IRQ_NONE;
  181. }
  182. if (riic->is_last || riic->err)
  183. writeb(ICCR2_SP, riic->base + RIIC_ICCR2);
  184. writeb(0, riic->base + RIIC_ICIER);
  185. complete(&riic->msg_done);
  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. writeb(ICCR2_SP, riic->base + RIIC_ICCR2);
  202. riic_clear_set_bit(riic, 0, ICMR3_ACKBT, RIIC_ICMR3);
  203. writeb(0, riic->base + RIIC_ICIER);
  204. complete(&riic->msg_done);
  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 u32 riic_func(struct i2c_adapter *adap)
  215. {
  216. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  217. }
  218. static const struct i2c_algorithm riic_algo = {
  219. .master_xfer = riic_xfer,
  220. .functionality = riic_func,
  221. };
  222. static int riic_init_hw(struct riic_dev *riic, u32 spd)
  223. {
  224. int ret;
  225. unsigned long rate;
  226. ret = clk_prepare_enable(riic->clk);
  227. if (ret)
  228. return ret;
  229. /*
  230. * TODO: Implement formula to calculate the timing values depending on
  231. * variable parent clock rate and arbitrary bus speed
  232. */
  233. rate = clk_get_rate(riic->clk);
  234. if (rate != 33325000) {
  235. dev_err(&riic->adapter.dev,
  236. "invalid parent clk (%lu). Must be 33325000Hz\n", rate);
  237. clk_disable_unprepare(riic->clk);
  238. return -EINVAL;
  239. }
  240. /* Changing the order of accessing IICRST and ICE may break things! */
  241. writeb(ICCR1_IICRST | ICCR1_SOWP, riic->base + RIIC_ICCR1);
  242. riic_clear_set_bit(riic, 0, ICCR1_ICE, RIIC_ICCR1);
  243. switch (spd) {
  244. case 100000:
  245. writeb(ICMR1_CKS(3), riic->base + RIIC_ICMR1);
  246. writeb(ICBRH_SP100K, riic->base + RIIC_ICBRH);
  247. writeb(ICBRL_SP100K, riic->base + RIIC_ICBRL);
  248. break;
  249. case 400000:
  250. writeb(ICMR1_CKS(1), riic->base + RIIC_ICMR1);
  251. writeb(ICBRH_SP400K, riic->base + RIIC_ICBRH);
  252. writeb(ICBRL_SP400K, riic->base + RIIC_ICBRL);
  253. break;
  254. default:
  255. dev_err(&riic->adapter.dev,
  256. "unsupported bus speed (%dHz). Use 100000 or 400000\n", spd);
  257. clk_disable_unprepare(riic->clk);
  258. return -EINVAL;
  259. }
  260. writeb(0, riic->base + RIIC_ICSER);
  261. writeb(ICMR3_ACKWP | ICMR3_RDRFS, riic->base + RIIC_ICMR3);
  262. riic_clear_set_bit(riic, ICCR1_IICRST, 0, RIIC_ICCR1);
  263. clk_disable_unprepare(riic->clk);
  264. return 0;
  265. }
  266. static struct riic_irq_desc riic_irqs[] = {
  267. { .res_num = 0, .isr = riic_tend_isr, .name = "riic-tend" },
  268. { .res_num = 1, .isr = riic_rdrf_isr, .name = "riic-rdrf" },
  269. { .res_num = 2, .isr = riic_tdre_isr, .name = "riic-tdre" },
  270. { .res_num = 5, .isr = riic_tend_isr, .name = "riic-nack" },
  271. };
  272. static int riic_i2c_probe(struct platform_device *pdev)
  273. {
  274. struct device_node *np = pdev->dev.of_node;
  275. struct riic_dev *riic;
  276. struct i2c_adapter *adap;
  277. struct resource *res;
  278. u32 bus_rate = 0;
  279. int i, ret;
  280. riic = devm_kzalloc(&pdev->dev, sizeof(*riic), GFP_KERNEL);
  281. if (!riic)
  282. return -ENOMEM;
  283. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  284. riic->base = devm_ioremap_resource(&pdev->dev, res);
  285. if (IS_ERR(riic->base))
  286. return PTR_ERR(riic->base);
  287. riic->clk = devm_clk_get(&pdev->dev, NULL);
  288. if (IS_ERR(riic->clk)) {
  289. dev_err(&pdev->dev, "missing controller clock");
  290. return PTR_ERR(riic->clk);
  291. }
  292. for (i = 0; i < ARRAY_SIZE(riic_irqs); i++) {
  293. res = platform_get_resource(pdev, IORESOURCE_IRQ, riic_irqs[i].res_num);
  294. if (!res)
  295. return -ENODEV;
  296. ret = devm_request_irq(&pdev->dev, res->start, riic_irqs[i].isr,
  297. 0, riic_irqs[i].name, riic);
  298. if (ret) {
  299. dev_err(&pdev->dev, "failed to request irq %s\n", riic_irqs[i].name);
  300. return ret;
  301. }
  302. }
  303. adap = &riic->adapter;
  304. i2c_set_adapdata(adap, riic);
  305. strlcpy(adap->name, "Renesas RIIC adapter", sizeof(adap->name));
  306. adap->owner = THIS_MODULE;
  307. adap->algo = &riic_algo;
  308. adap->dev.parent = &pdev->dev;
  309. adap->dev.of_node = pdev->dev.of_node;
  310. init_completion(&riic->msg_done);
  311. of_property_read_u32(np, "clock-frequency", &bus_rate);
  312. ret = riic_init_hw(riic, bus_rate);
  313. if (ret)
  314. return ret;
  315. ret = i2c_add_adapter(adap);
  316. if (ret)
  317. return ret;
  318. platform_set_drvdata(pdev, riic);
  319. dev_info(&pdev->dev, "registered with %dHz bus speed\n", bus_rate);
  320. return 0;
  321. }
  322. static int riic_i2c_remove(struct platform_device *pdev)
  323. {
  324. struct riic_dev *riic = platform_get_drvdata(pdev);
  325. writeb(0, riic->base + RIIC_ICIER);
  326. i2c_del_adapter(&riic->adapter);
  327. return 0;
  328. }
  329. static const struct of_device_id riic_i2c_dt_ids[] = {
  330. { .compatible = "renesas,riic-rz" },
  331. { /* Sentinel */ },
  332. };
  333. static struct platform_driver riic_i2c_driver = {
  334. .probe = riic_i2c_probe,
  335. .remove = riic_i2c_remove,
  336. .driver = {
  337. .name = "i2c-riic",
  338. .of_match_table = riic_i2c_dt_ids,
  339. },
  340. };
  341. module_platform_driver(riic_i2c_driver);
  342. MODULE_DESCRIPTION("Renesas RIIC adapter");
  343. MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
  344. MODULE_LICENSE("GPL v2");
  345. MODULE_DEVICE_TABLE(of, riic_i2c_dt_ids);