i2c-emev2.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * I2C driver for the Renesas EMEV2 SoC
  3. *
  4. * Copyright (C) 2015 Wolfram Sang <wsa@sang-engineering.com>
  5. * Copyright 2013 Codethink Ltd.
  6. * Copyright 2010-2015 Renesas Electronics Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/completion.h>
  14. #include <linux/device.h>
  15. #include <linux/i2c.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/sched.h>
  24. /* I2C Registers */
  25. #define I2C_OFS_IICACT0 0x00 /* start */
  26. #define I2C_OFS_IIC0 0x04 /* shift */
  27. #define I2C_OFS_IICC0 0x08 /* control */
  28. #define I2C_OFS_SVA0 0x0c /* slave address */
  29. #define I2C_OFS_IICCL0 0x10 /* clock select */
  30. #define I2C_OFS_IICX0 0x14 /* extension */
  31. #define I2C_OFS_IICS0 0x18 /* status */
  32. #define I2C_OFS_IICSE0 0x1c /* status For emulation */
  33. #define I2C_OFS_IICF0 0x20 /* IIC flag */
  34. /* I2C IICACT0 Masks */
  35. #define I2C_BIT_IICE0 0x0001
  36. /* I2C IICC0 Masks */
  37. #define I2C_BIT_LREL0 0x0040
  38. #define I2C_BIT_WREL0 0x0020
  39. #define I2C_BIT_SPIE0 0x0010
  40. #define I2C_BIT_WTIM0 0x0008
  41. #define I2C_BIT_ACKE0 0x0004
  42. #define I2C_BIT_STT0 0x0002
  43. #define I2C_BIT_SPT0 0x0001
  44. /* I2C IICCL0 Masks */
  45. #define I2C_BIT_SMC0 0x0008
  46. #define I2C_BIT_DFC0 0x0004
  47. /* I2C IICSE0 Masks */
  48. #define I2C_BIT_MSTS0 0x0080
  49. #define I2C_BIT_ALD0 0x0040
  50. #define I2C_BIT_EXC0 0x0020
  51. #define I2C_BIT_COI0 0x0010
  52. #define I2C_BIT_TRC0 0x0008
  53. #define I2C_BIT_ACKD0 0x0004
  54. #define I2C_BIT_STD0 0x0002
  55. #define I2C_BIT_SPD0 0x0001
  56. /* I2C IICF0 Masks */
  57. #define I2C_BIT_STCF 0x0080
  58. #define I2C_BIT_IICBSY 0x0040
  59. #define I2C_BIT_STCEN 0x0002
  60. #define I2C_BIT_IICRSV 0x0001
  61. struct em_i2c_device {
  62. void __iomem *base;
  63. struct i2c_adapter adap;
  64. struct completion msg_done;
  65. struct clk *sclk;
  66. };
  67. static inline void em_clear_set_bit(struct em_i2c_device *priv, u8 clear, u8 set, u8 reg)
  68. {
  69. writeb((readb(priv->base + reg) & ~clear) | set, priv->base + reg);
  70. }
  71. static int em_i2c_wait_for_event(struct em_i2c_device *priv)
  72. {
  73. unsigned long time_left;
  74. int status;
  75. reinit_completion(&priv->msg_done);
  76. time_left = wait_for_completion_timeout(&priv->msg_done, priv->adap.timeout);
  77. if (!time_left)
  78. return -ETIMEDOUT;
  79. status = readb(priv->base + I2C_OFS_IICSE0);
  80. return status & I2C_BIT_ALD0 ? -EAGAIN : status;
  81. }
  82. static void em_i2c_stop(struct em_i2c_device *priv)
  83. {
  84. /* Send Stop condition */
  85. em_clear_set_bit(priv, 0, I2C_BIT_SPT0 | I2C_BIT_SPIE0, I2C_OFS_IICC0);
  86. /* Wait for stop condition */
  87. em_i2c_wait_for_event(priv);
  88. }
  89. static void em_i2c_reset(struct i2c_adapter *adap)
  90. {
  91. struct em_i2c_device *priv = i2c_get_adapdata(adap);
  92. int retr;
  93. /* If I2C active */
  94. if (readb(priv->base + I2C_OFS_IICACT0) & I2C_BIT_IICE0) {
  95. /* Disable I2C operation */
  96. writeb(0, priv->base + I2C_OFS_IICACT0);
  97. retr = 1000;
  98. while (readb(priv->base + I2C_OFS_IICACT0) == 1 && retr)
  99. retr--;
  100. WARN_ON(retr == 0);
  101. }
  102. /* Transfer mode set */
  103. writeb(I2C_BIT_DFC0, priv->base + I2C_OFS_IICCL0);
  104. /* Can Issue start without detecting a stop, Reservation disabled. */
  105. writeb(I2C_BIT_STCEN | I2C_BIT_IICRSV, priv->base + I2C_OFS_IICF0);
  106. /* I2C enable, 9 bit interrupt mode */
  107. writeb(I2C_BIT_WTIM0, priv->base + I2C_OFS_IICC0);
  108. /* Enable I2C operation */
  109. writeb(I2C_BIT_IICE0, priv->base + I2C_OFS_IICACT0);
  110. retr = 1000;
  111. while (readb(priv->base + I2C_OFS_IICACT0) == 0 && retr)
  112. retr--;
  113. WARN_ON(retr == 0);
  114. }
  115. static int __em_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  116. int stop)
  117. {
  118. struct em_i2c_device *priv = i2c_get_adapdata(adap);
  119. int count, status, read = !!(msg->flags & I2C_M_RD);
  120. /* Send start condition */
  121. em_clear_set_bit(priv, 0, I2C_BIT_ACKE0 | I2C_BIT_WTIM0, I2C_OFS_IICC0);
  122. em_clear_set_bit(priv, 0, I2C_BIT_STT0, I2C_OFS_IICC0);
  123. /* Send slave address and R/W type */
  124. writeb((msg->addr << 1) | read, priv->base + I2C_OFS_IIC0);
  125. /* Wait for transaction */
  126. status = em_i2c_wait_for_event(priv);
  127. if (status < 0)
  128. goto out_reset;
  129. /* Received NACK (result of setting slave address and R/W) */
  130. if (!(status & I2C_BIT_ACKD0)) {
  131. em_i2c_stop(priv);
  132. goto out;
  133. }
  134. /* Extra setup for read transactions */
  135. if (read) {
  136. /* 8 bit interrupt mode */
  137. em_clear_set_bit(priv, I2C_BIT_WTIM0, I2C_BIT_ACKE0, I2C_OFS_IICC0);
  138. em_clear_set_bit(priv, I2C_BIT_WTIM0, I2C_BIT_WREL0, I2C_OFS_IICC0);
  139. /* Wait for transaction */
  140. status = em_i2c_wait_for_event(priv);
  141. if (status < 0)
  142. goto out_reset;
  143. }
  144. /* Send / receive data */
  145. for (count = 0; count < msg->len; count++) {
  146. if (read) { /* Read transaction */
  147. msg->buf[count] = readb(priv->base + I2C_OFS_IIC0);
  148. em_clear_set_bit(priv, 0, I2C_BIT_WREL0, I2C_OFS_IICC0);
  149. } else { /* Write transaction */
  150. /* Received NACK */
  151. if (!(status & I2C_BIT_ACKD0)) {
  152. em_i2c_stop(priv);
  153. goto out;
  154. }
  155. /* Write data */
  156. writeb(msg->buf[count], priv->base + I2C_OFS_IIC0);
  157. }
  158. /* Wait for R/W transaction */
  159. status = em_i2c_wait_for_event(priv);
  160. if (status < 0)
  161. goto out_reset;
  162. }
  163. if (stop)
  164. em_i2c_stop(priv);
  165. return count;
  166. out_reset:
  167. em_i2c_reset(adap);
  168. out:
  169. return status < 0 ? status : -ENXIO;
  170. }
  171. static int em_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  172. int num)
  173. {
  174. struct em_i2c_device *priv = i2c_get_adapdata(adap);
  175. int ret, i;
  176. if (readb(priv->base + I2C_OFS_IICF0) & I2C_BIT_IICBSY)
  177. return -EAGAIN;
  178. for (i = 0; i < num; i++) {
  179. ret = __em_i2c_xfer(adap, &msgs[i], (i == (num - 1)));
  180. if (ret < 0)
  181. return ret;
  182. }
  183. /* I2C transfer completed */
  184. return num;
  185. }
  186. static irqreturn_t em_i2c_irq_handler(int this_irq, void *dev_id)
  187. {
  188. struct em_i2c_device *priv = dev_id;
  189. complete(&priv->msg_done);
  190. return IRQ_HANDLED;
  191. }
  192. static u32 em_i2c_func(struct i2c_adapter *adap)
  193. {
  194. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  195. }
  196. static struct i2c_algorithm em_i2c_algo = {
  197. .master_xfer = em_i2c_xfer,
  198. .functionality = em_i2c_func,
  199. };
  200. static int em_i2c_probe(struct platform_device *pdev)
  201. {
  202. struct em_i2c_device *priv;
  203. struct resource *r;
  204. int irq, ret;
  205. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  206. if (!priv)
  207. return -ENOMEM;
  208. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  209. priv->base = devm_ioremap_resource(&pdev->dev, r);
  210. if (IS_ERR(priv->base))
  211. return PTR_ERR(priv->base);
  212. strlcpy(priv->adap.name, "EMEV2 I2C", sizeof(priv->adap.name));
  213. priv->sclk = devm_clk_get(&pdev->dev, "sclk");
  214. if (IS_ERR(priv->sclk))
  215. return PTR_ERR(priv->sclk);
  216. clk_prepare_enable(priv->sclk);
  217. priv->adap.timeout = msecs_to_jiffies(100);
  218. priv->adap.retries = 5;
  219. priv->adap.dev.parent = &pdev->dev;
  220. priv->adap.algo = &em_i2c_algo;
  221. priv->adap.owner = THIS_MODULE;
  222. priv->adap.dev.of_node = pdev->dev.of_node;
  223. init_completion(&priv->msg_done);
  224. platform_set_drvdata(pdev, priv);
  225. i2c_set_adapdata(&priv->adap, priv);
  226. em_i2c_reset(&priv->adap);
  227. irq = platform_get_irq(pdev, 0);
  228. ret = devm_request_irq(&pdev->dev, irq, em_i2c_irq_handler, 0,
  229. "em_i2c", priv);
  230. if (ret)
  231. goto err_clk;
  232. ret = i2c_add_adapter(&priv->adap);
  233. if (ret)
  234. goto err_clk;
  235. dev_info(&pdev->dev, "Added i2c controller %d, irq %d\n", priv->adap.nr, irq);
  236. return 0;
  237. err_clk:
  238. clk_disable_unprepare(priv->sclk);
  239. return ret;
  240. }
  241. static int em_i2c_remove(struct platform_device *dev)
  242. {
  243. struct em_i2c_device *priv = platform_get_drvdata(dev);
  244. i2c_del_adapter(&priv->adap);
  245. clk_disable_unprepare(priv->sclk);
  246. return 0;
  247. }
  248. static const struct of_device_id em_i2c_ids[] = {
  249. { .compatible = "renesas,iic-emev2", },
  250. { }
  251. };
  252. static struct platform_driver em_i2c_driver = {
  253. .probe = em_i2c_probe,
  254. .remove = em_i2c_remove,
  255. .driver = {
  256. .name = "em-i2c",
  257. .of_match_table = em_i2c_ids,
  258. }
  259. };
  260. module_platform_driver(em_i2c_driver);
  261. MODULE_DESCRIPTION("EMEV2 I2C bus driver");
  262. MODULE_AUTHOR("Ian Molton and Wolfram Sang <wsa@sang-engineering.com>");
  263. MODULE_LICENSE("GPL v2");
  264. MODULE_DEVICE_TABLE(of, em_i2c_ids);