i2c-emev2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * I2C driver for the Renesas EMEV2 SoC
  4. *
  5. * Copyright (C) 2015 Wolfram Sang <[email protected]>
  6. * Copyright 2013 Codethink Ltd.
  7. * Copyright 2010-2015 Renesas Electronics Corporation
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/completion.h>
  11. #include <linux/device.h>
  12. #include <linux/i2c.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of_device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/sched.h>
  21. /* I2C Registers */
  22. #define I2C_OFS_IICACT0 0x00 /* start */
  23. #define I2C_OFS_IIC0 0x04 /* shift */
  24. #define I2C_OFS_IICC0 0x08 /* control */
  25. #define I2C_OFS_SVA0 0x0c /* slave address */
  26. #define I2C_OFS_IICCL0 0x10 /* clock select */
  27. #define I2C_OFS_IICX0 0x14 /* extension */
  28. #define I2C_OFS_IICS0 0x18 /* status */
  29. #define I2C_OFS_IICSE0 0x1c /* status For emulation */
  30. #define I2C_OFS_IICF0 0x20 /* IIC flag */
  31. /* I2C IICACT0 Masks */
  32. #define I2C_BIT_IICE0 0x0001
  33. /* I2C IICC0 Masks */
  34. #define I2C_BIT_LREL0 0x0040
  35. #define I2C_BIT_WREL0 0x0020
  36. #define I2C_BIT_SPIE0 0x0010
  37. #define I2C_BIT_WTIM0 0x0008
  38. #define I2C_BIT_ACKE0 0x0004
  39. #define I2C_BIT_STT0 0x0002
  40. #define I2C_BIT_SPT0 0x0001
  41. /* I2C IICCL0 Masks */
  42. #define I2C_BIT_SMC0 0x0008
  43. #define I2C_BIT_DFC0 0x0004
  44. /* I2C IICSE0 Masks */
  45. #define I2C_BIT_MSTS0 0x0080
  46. #define I2C_BIT_ALD0 0x0040
  47. #define I2C_BIT_EXC0 0x0020
  48. #define I2C_BIT_COI0 0x0010
  49. #define I2C_BIT_TRC0 0x0008
  50. #define I2C_BIT_ACKD0 0x0004
  51. #define I2C_BIT_STD0 0x0002
  52. #define I2C_BIT_SPD0 0x0001
  53. /* I2C IICF0 Masks */
  54. #define I2C_BIT_STCF 0x0080
  55. #define I2C_BIT_IICBSY 0x0040
  56. #define I2C_BIT_STCEN 0x0002
  57. #define I2C_BIT_IICRSV 0x0001
  58. struct em_i2c_device {
  59. void __iomem *base;
  60. struct i2c_adapter adap;
  61. struct completion msg_done;
  62. struct clk *sclk;
  63. struct i2c_client *slave;
  64. int irq;
  65. };
  66. static inline void em_clear_set_bit(struct em_i2c_device *priv, u8 clear, u8 set, u8 reg)
  67. {
  68. writeb((readb(priv->base + reg) & ~clear) | set, priv->base + reg);
  69. }
  70. static int em_i2c_wait_for_event(struct em_i2c_device *priv)
  71. {
  72. unsigned long time_left;
  73. int status;
  74. reinit_completion(&priv->msg_done);
  75. time_left = wait_for_completion_timeout(&priv->msg_done, priv->adap.timeout);
  76. if (!time_left)
  77. return -ETIMEDOUT;
  78. status = readb(priv->base + I2C_OFS_IICSE0);
  79. return status & I2C_BIT_ALD0 ? -EAGAIN : status;
  80. }
  81. static void em_i2c_stop(struct em_i2c_device *priv)
  82. {
  83. /* Send Stop condition */
  84. em_clear_set_bit(priv, 0, I2C_BIT_SPT0 | I2C_BIT_SPIE0, I2C_OFS_IICC0);
  85. /* Wait for stop condition */
  86. em_i2c_wait_for_event(priv);
  87. }
  88. static void em_i2c_reset(struct i2c_adapter *adap)
  89. {
  90. struct em_i2c_device *priv = i2c_get_adapdata(adap);
  91. int retr;
  92. /* If I2C active */
  93. if (readb(priv->base + I2C_OFS_IICACT0) & I2C_BIT_IICE0) {
  94. /* Disable I2C operation */
  95. writeb(0, priv->base + I2C_OFS_IICACT0);
  96. retr = 1000;
  97. while (readb(priv->base + I2C_OFS_IICACT0) == 1 && retr)
  98. retr--;
  99. WARN_ON(retr == 0);
  100. }
  101. /* Transfer mode set */
  102. writeb(I2C_BIT_DFC0, priv->base + I2C_OFS_IICCL0);
  103. /* Can Issue start without detecting a stop, Reservation disabled. */
  104. writeb(I2C_BIT_STCEN | I2C_BIT_IICRSV, priv->base + I2C_OFS_IICF0);
  105. /* I2C enable, 9 bit interrupt mode */
  106. writeb(I2C_BIT_WTIM0, priv->base + I2C_OFS_IICC0);
  107. /* Enable I2C operation */
  108. writeb(I2C_BIT_IICE0, priv->base + I2C_OFS_IICACT0);
  109. retr = 1000;
  110. while (readb(priv->base + I2C_OFS_IICACT0) == 0 && retr)
  111. retr--;
  112. WARN_ON(retr == 0);
  113. }
  114. static int __em_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  115. int stop)
  116. {
  117. struct em_i2c_device *priv = i2c_get_adapdata(adap);
  118. int count, status, read = !!(msg->flags & I2C_M_RD);
  119. /* Send start condition */
  120. em_clear_set_bit(priv, 0, I2C_BIT_ACKE0 | I2C_BIT_WTIM0, I2C_OFS_IICC0);
  121. em_clear_set_bit(priv, 0, I2C_BIT_STT0, I2C_OFS_IICC0);
  122. /* Send slave address and R/W type */
  123. writeb(i2c_8bit_addr_from_msg(msg), priv->base + I2C_OFS_IIC0);
  124. /* Wait for transaction */
  125. status = em_i2c_wait_for_event(priv);
  126. if (status < 0)
  127. goto out_reset;
  128. /* Received NACK (result of setting slave address and R/W) */
  129. if (!(status & I2C_BIT_ACKD0)) {
  130. em_i2c_stop(priv);
  131. goto out;
  132. }
  133. /* Extra setup for read transactions */
  134. if (read) {
  135. /* 8 bit interrupt mode */
  136. em_clear_set_bit(priv, I2C_BIT_WTIM0, I2C_BIT_ACKE0, I2C_OFS_IICC0);
  137. em_clear_set_bit(priv, I2C_BIT_WTIM0, I2C_BIT_WREL0, I2C_OFS_IICC0);
  138. /* Wait for transaction */
  139. status = em_i2c_wait_for_event(priv);
  140. if (status < 0)
  141. goto out_reset;
  142. }
  143. /* Send / receive data */
  144. for (count = 0; count < msg->len; count++) {
  145. if (read) { /* Read transaction */
  146. msg->buf[count] = readb(priv->base + I2C_OFS_IIC0);
  147. em_clear_set_bit(priv, 0, I2C_BIT_WREL0, I2C_OFS_IICC0);
  148. } else { /* Write transaction */
  149. /* Received NACK */
  150. if (!(status & I2C_BIT_ACKD0)) {
  151. em_i2c_stop(priv);
  152. goto out;
  153. }
  154. /* Write data */
  155. writeb(msg->buf[count], priv->base + I2C_OFS_IIC0);
  156. }
  157. /* Wait for R/W transaction */
  158. status = em_i2c_wait_for_event(priv);
  159. if (status < 0)
  160. goto out_reset;
  161. }
  162. if (stop)
  163. em_i2c_stop(priv);
  164. return count;
  165. out_reset:
  166. em_i2c_reset(adap);
  167. out:
  168. return status < 0 ? status : -ENXIO;
  169. }
  170. static int em_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  171. int num)
  172. {
  173. struct em_i2c_device *priv = i2c_get_adapdata(adap);
  174. int ret, i;
  175. if (readb(priv->base + I2C_OFS_IICF0) & I2C_BIT_IICBSY)
  176. return -EAGAIN;
  177. for (i = 0; i < num; i++) {
  178. ret = __em_i2c_xfer(adap, &msgs[i], (i == (num - 1)));
  179. if (ret < 0)
  180. return ret;
  181. }
  182. /* I2C transfer completed */
  183. return num;
  184. }
  185. static bool em_i2c_slave_irq(struct em_i2c_device *priv)
  186. {
  187. u8 status, value;
  188. enum i2c_slave_event event;
  189. int ret;
  190. if (!priv->slave)
  191. return false;
  192. status = readb(priv->base + I2C_OFS_IICSE0);
  193. /* Extension code, do not participate */
  194. if (status & I2C_BIT_EXC0) {
  195. em_clear_set_bit(priv, 0, I2C_BIT_LREL0, I2C_OFS_IICC0);
  196. return true;
  197. }
  198. /* Stop detected, we don't know if it's for slave or master */
  199. if (status & I2C_BIT_SPD0) {
  200. /* Notify slave device */
  201. i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
  202. /* Pretend we did not handle the interrupt */
  203. return false;
  204. }
  205. /* Only handle interrupts addressed to us */
  206. if (!(status & I2C_BIT_COI0))
  207. return false;
  208. /* Enable stop interrupts */
  209. em_clear_set_bit(priv, 0, I2C_BIT_SPIE0, I2C_OFS_IICC0);
  210. /* Transmission or Reception */
  211. if (status & I2C_BIT_TRC0) {
  212. if (status & I2C_BIT_ACKD0) {
  213. /* 9 bit interrupt mode */
  214. em_clear_set_bit(priv, 0, I2C_BIT_WTIM0, I2C_OFS_IICC0);
  215. /* Send data */
  216. event = status & I2C_BIT_STD0 ?
  217. I2C_SLAVE_READ_REQUESTED :
  218. I2C_SLAVE_READ_PROCESSED;
  219. i2c_slave_event(priv->slave, event, &value);
  220. writeb(value, priv->base + I2C_OFS_IIC0);
  221. } else {
  222. /* NACK, stop transmitting */
  223. em_clear_set_bit(priv, 0, I2C_BIT_LREL0, I2C_OFS_IICC0);
  224. }
  225. } else {
  226. /* 8 bit interrupt mode */
  227. em_clear_set_bit(priv, I2C_BIT_WTIM0, I2C_BIT_ACKE0,
  228. I2C_OFS_IICC0);
  229. em_clear_set_bit(priv, I2C_BIT_WTIM0, I2C_BIT_WREL0,
  230. I2C_OFS_IICC0);
  231. if (status & I2C_BIT_STD0) {
  232. i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED,
  233. &value);
  234. } else {
  235. /* Recv data */
  236. value = readb(priv->base + I2C_OFS_IIC0);
  237. ret = i2c_slave_event(priv->slave,
  238. I2C_SLAVE_WRITE_RECEIVED, &value);
  239. if (ret < 0)
  240. em_clear_set_bit(priv, I2C_BIT_ACKE0, 0,
  241. I2C_OFS_IICC0);
  242. }
  243. }
  244. return true;
  245. }
  246. static irqreturn_t em_i2c_irq_handler(int this_irq, void *dev_id)
  247. {
  248. struct em_i2c_device *priv = dev_id;
  249. if (em_i2c_slave_irq(priv))
  250. return IRQ_HANDLED;
  251. complete(&priv->msg_done);
  252. return IRQ_HANDLED;
  253. }
  254. static u32 em_i2c_func(struct i2c_adapter *adap)
  255. {
  256. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SLAVE;
  257. }
  258. static int em_i2c_reg_slave(struct i2c_client *slave)
  259. {
  260. struct em_i2c_device *priv = i2c_get_adapdata(slave->adapter);
  261. if (priv->slave)
  262. return -EBUSY;
  263. if (slave->flags & I2C_CLIENT_TEN)
  264. return -EAFNOSUPPORT;
  265. priv->slave = slave;
  266. /* Set slave address */
  267. writeb(slave->addr << 1, priv->base + I2C_OFS_SVA0);
  268. return 0;
  269. }
  270. static int em_i2c_unreg_slave(struct i2c_client *slave)
  271. {
  272. struct em_i2c_device *priv = i2c_get_adapdata(slave->adapter);
  273. WARN_ON(!priv->slave);
  274. writeb(0, priv->base + I2C_OFS_SVA0);
  275. /*
  276. * Wait for interrupt to finish. New slave irqs cannot happen because we
  277. * cleared the slave address and, thus, only extension codes will be
  278. * detected which do not use the slave ptr.
  279. */
  280. synchronize_irq(priv->irq);
  281. priv->slave = NULL;
  282. return 0;
  283. }
  284. static const struct i2c_algorithm em_i2c_algo = {
  285. .master_xfer = em_i2c_xfer,
  286. .functionality = em_i2c_func,
  287. .reg_slave = em_i2c_reg_slave,
  288. .unreg_slave = em_i2c_unreg_slave,
  289. };
  290. static int em_i2c_probe(struct platform_device *pdev)
  291. {
  292. struct em_i2c_device *priv;
  293. int ret;
  294. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  295. if (!priv)
  296. return -ENOMEM;
  297. priv->base = devm_platform_ioremap_resource(pdev, 0);
  298. if (IS_ERR(priv->base))
  299. return PTR_ERR(priv->base);
  300. strscpy(priv->adap.name, "EMEV2 I2C", sizeof(priv->adap.name));
  301. priv->sclk = devm_clk_get(&pdev->dev, "sclk");
  302. if (IS_ERR(priv->sclk))
  303. return PTR_ERR(priv->sclk);
  304. ret = clk_prepare_enable(priv->sclk);
  305. if (ret)
  306. return ret;
  307. priv->adap.timeout = msecs_to_jiffies(100);
  308. priv->adap.retries = 5;
  309. priv->adap.dev.parent = &pdev->dev;
  310. priv->adap.algo = &em_i2c_algo;
  311. priv->adap.owner = THIS_MODULE;
  312. priv->adap.dev.of_node = pdev->dev.of_node;
  313. init_completion(&priv->msg_done);
  314. platform_set_drvdata(pdev, priv);
  315. i2c_set_adapdata(&priv->adap, priv);
  316. em_i2c_reset(&priv->adap);
  317. ret = platform_get_irq(pdev, 0);
  318. if (ret < 0)
  319. goto err_clk;
  320. priv->irq = ret;
  321. ret = devm_request_irq(&pdev->dev, priv->irq, em_i2c_irq_handler, 0,
  322. "em_i2c", priv);
  323. if (ret)
  324. goto err_clk;
  325. ret = i2c_add_adapter(&priv->adap);
  326. if (ret)
  327. goto err_clk;
  328. dev_info(&pdev->dev, "Added i2c controller %d, irq %d\n", priv->adap.nr,
  329. priv->irq);
  330. return 0;
  331. err_clk:
  332. clk_disable_unprepare(priv->sclk);
  333. return ret;
  334. }
  335. static int em_i2c_remove(struct platform_device *dev)
  336. {
  337. struct em_i2c_device *priv = platform_get_drvdata(dev);
  338. i2c_del_adapter(&priv->adap);
  339. clk_disable_unprepare(priv->sclk);
  340. return 0;
  341. }
  342. static const struct of_device_id em_i2c_ids[] = {
  343. { .compatible = "renesas,iic-emev2", },
  344. { }
  345. };
  346. static struct platform_driver em_i2c_driver = {
  347. .probe = em_i2c_probe,
  348. .remove = em_i2c_remove,
  349. .driver = {
  350. .name = "em-i2c",
  351. .of_match_table = em_i2c_ids,
  352. }
  353. };
  354. module_platform_driver(em_i2c_driver);
  355. MODULE_DESCRIPTION("EMEV2 I2C bus driver");
  356. MODULE_AUTHOR("Ian Molton");
  357. MODULE_AUTHOR("Wolfram Sang <[email protected]>");
  358. MODULE_LICENSE("GPL v2");
  359. MODULE_DEVICE_TABLE(of, em_i2c_ids);