i2c-emev2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * I2C driver for the Renesas EMEV2 SoC
  4. *
  5. * Copyright (C) 2015 Wolfram Sang <wsa@sang-engineering.com>
  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. };
  65. static inline void em_clear_set_bit(struct em_i2c_device *priv, u8 clear, u8 set, u8 reg)
  66. {
  67. writeb((readb(priv->base + reg) & ~clear) | set, priv->base + reg);
  68. }
  69. static int em_i2c_wait_for_event(struct em_i2c_device *priv)
  70. {
  71. unsigned long time_left;
  72. int status;
  73. reinit_completion(&priv->msg_done);
  74. time_left = wait_for_completion_timeout(&priv->msg_done, priv->adap.timeout);
  75. if (!time_left)
  76. return -ETIMEDOUT;
  77. status = readb(priv->base + I2C_OFS_IICSE0);
  78. return status & I2C_BIT_ALD0 ? -EAGAIN : status;
  79. }
  80. static void em_i2c_stop(struct em_i2c_device *priv)
  81. {
  82. /* Send Stop condition */
  83. em_clear_set_bit(priv, 0, I2C_BIT_SPT0 | I2C_BIT_SPIE0, I2C_OFS_IICC0);
  84. /* Wait for stop condition */
  85. em_i2c_wait_for_event(priv);
  86. }
  87. static void em_i2c_reset(struct i2c_adapter *adap)
  88. {
  89. struct em_i2c_device *priv = i2c_get_adapdata(adap);
  90. int retr;
  91. /* If I2C active */
  92. if (readb(priv->base + I2C_OFS_IICACT0) & I2C_BIT_IICE0) {
  93. /* Disable I2C operation */
  94. writeb(0, priv->base + I2C_OFS_IICACT0);
  95. retr = 1000;
  96. while (readb(priv->base + I2C_OFS_IICACT0) == 1 && retr)
  97. retr--;
  98. WARN_ON(retr == 0);
  99. }
  100. /* Transfer mode set */
  101. writeb(I2C_BIT_DFC0, priv->base + I2C_OFS_IICCL0);
  102. /* Can Issue start without detecting a stop, Reservation disabled. */
  103. writeb(I2C_BIT_STCEN | I2C_BIT_IICRSV, priv->base + I2C_OFS_IICF0);
  104. /* I2C enable, 9 bit interrupt mode */
  105. writeb(I2C_BIT_WTIM0, priv->base + I2C_OFS_IICC0);
  106. /* Enable I2C operation */
  107. writeb(I2C_BIT_IICE0, priv->base + I2C_OFS_IICACT0);
  108. retr = 1000;
  109. while (readb(priv->base + I2C_OFS_IICACT0) == 0 && retr)
  110. retr--;
  111. WARN_ON(retr == 0);
  112. }
  113. static int __em_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  114. int stop)
  115. {
  116. struct em_i2c_device *priv = i2c_get_adapdata(adap);
  117. int count, status, read = !!(msg->flags & I2C_M_RD);
  118. /* Send start condition */
  119. em_clear_set_bit(priv, 0, I2C_BIT_ACKE0 | I2C_BIT_WTIM0, I2C_OFS_IICC0);
  120. em_clear_set_bit(priv, 0, I2C_BIT_STT0, I2C_OFS_IICC0);
  121. /* Send slave address and R/W type */
  122. writeb(i2c_8bit_addr_from_msg(msg), priv->base + I2C_OFS_IIC0);
  123. /* Wait for transaction */
  124. status = em_i2c_wait_for_event(priv);
  125. if (status < 0)
  126. goto out_reset;
  127. /* Received NACK (result of setting slave address and R/W) */
  128. if (!(status & I2C_BIT_ACKD0)) {
  129. em_i2c_stop(priv);
  130. goto out;
  131. }
  132. /* Extra setup for read transactions */
  133. if (read) {
  134. /* 8 bit interrupt mode */
  135. em_clear_set_bit(priv, I2C_BIT_WTIM0, I2C_BIT_ACKE0, I2C_OFS_IICC0);
  136. em_clear_set_bit(priv, I2C_BIT_WTIM0, I2C_BIT_WREL0, I2C_OFS_IICC0);
  137. /* Wait for transaction */
  138. status = em_i2c_wait_for_event(priv);
  139. if (status < 0)
  140. goto out_reset;
  141. }
  142. /* Send / receive data */
  143. for (count = 0; count < msg->len; count++) {
  144. if (read) { /* Read transaction */
  145. msg->buf[count] = readb(priv->base + I2C_OFS_IIC0);
  146. em_clear_set_bit(priv, 0, I2C_BIT_WREL0, I2C_OFS_IICC0);
  147. } else { /* Write transaction */
  148. /* Received NACK */
  149. if (!(status & I2C_BIT_ACKD0)) {
  150. em_i2c_stop(priv);
  151. goto out;
  152. }
  153. /* Write data */
  154. writeb(msg->buf[count], priv->base + I2C_OFS_IIC0);
  155. }
  156. /* Wait for R/W transaction */
  157. status = em_i2c_wait_for_event(priv);
  158. if (status < 0)
  159. goto out_reset;
  160. }
  161. if (stop)
  162. em_i2c_stop(priv);
  163. return count;
  164. out_reset:
  165. em_i2c_reset(adap);
  166. out:
  167. return status < 0 ? status : -ENXIO;
  168. }
  169. static int em_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  170. int num)
  171. {
  172. struct em_i2c_device *priv = i2c_get_adapdata(adap);
  173. int ret, i;
  174. if (readb(priv->base + I2C_OFS_IICF0) & I2C_BIT_IICBSY)
  175. return -EAGAIN;
  176. for (i = 0; i < num; i++) {
  177. ret = __em_i2c_xfer(adap, &msgs[i], (i == (num - 1)));
  178. if (ret < 0)
  179. return ret;
  180. }
  181. /* I2C transfer completed */
  182. return num;
  183. }
  184. static bool em_i2c_slave_irq(struct em_i2c_device *priv)
  185. {
  186. u8 status, value;
  187. enum i2c_slave_event event;
  188. int ret;
  189. if (!priv->slave)
  190. return false;
  191. status = readb(priv->base + I2C_OFS_IICSE0);
  192. /* Extension code, do not participate */
  193. if (status & I2C_BIT_EXC0) {
  194. em_clear_set_bit(priv, 0, I2C_BIT_LREL0, I2C_OFS_IICC0);
  195. return true;
  196. }
  197. /* Stop detected, we don't know if it's for slave or master */
  198. if (status & I2C_BIT_SPD0) {
  199. /* Notify slave device */
  200. i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
  201. /* Pretend we did not handle the interrupt */
  202. return false;
  203. }
  204. /* Only handle interrupts addressed to us */
  205. if (!(status & I2C_BIT_COI0))
  206. return false;
  207. /* Enable stop interrupts */
  208. em_clear_set_bit(priv, 0, I2C_BIT_SPIE0, I2C_OFS_IICC0);
  209. /* Transmission or Reception */
  210. if (status & I2C_BIT_TRC0) {
  211. if (status & I2C_BIT_ACKD0) {
  212. /* 9 bit interrupt mode */
  213. em_clear_set_bit(priv, 0, I2C_BIT_WTIM0, I2C_OFS_IICC0);
  214. /* Send data */
  215. event = status & I2C_BIT_STD0 ?
  216. I2C_SLAVE_READ_REQUESTED :
  217. I2C_SLAVE_READ_PROCESSED;
  218. i2c_slave_event(priv->slave, event, &value);
  219. writeb(value, priv->base + I2C_OFS_IIC0);
  220. } else {
  221. /* NACK, stop transmitting */
  222. em_clear_set_bit(priv, 0, I2C_BIT_LREL0, I2C_OFS_IICC0);
  223. }
  224. } else {
  225. /* 8 bit interrupt mode */
  226. em_clear_set_bit(priv, I2C_BIT_WTIM0, I2C_BIT_ACKE0,
  227. I2C_OFS_IICC0);
  228. em_clear_set_bit(priv, I2C_BIT_WTIM0, I2C_BIT_WREL0,
  229. I2C_OFS_IICC0);
  230. if (status & I2C_BIT_STD0) {
  231. i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED,
  232. &value);
  233. } else {
  234. /* Recv data */
  235. value = readb(priv->base + I2C_OFS_IIC0);
  236. ret = i2c_slave_event(priv->slave,
  237. I2C_SLAVE_WRITE_RECEIVED, &value);
  238. if (ret < 0)
  239. em_clear_set_bit(priv, I2C_BIT_ACKE0, 0,
  240. I2C_OFS_IICC0);
  241. }
  242. }
  243. return true;
  244. }
  245. static irqreturn_t em_i2c_irq_handler(int this_irq, void *dev_id)
  246. {
  247. struct em_i2c_device *priv = dev_id;
  248. if (em_i2c_slave_irq(priv))
  249. return IRQ_HANDLED;
  250. complete(&priv->msg_done);
  251. return IRQ_HANDLED;
  252. }
  253. static u32 em_i2c_func(struct i2c_adapter *adap)
  254. {
  255. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SLAVE;
  256. }
  257. static int em_i2c_reg_slave(struct i2c_client *slave)
  258. {
  259. struct em_i2c_device *priv = i2c_get_adapdata(slave->adapter);
  260. if (priv->slave)
  261. return -EBUSY;
  262. if (slave->flags & I2C_CLIENT_TEN)
  263. return -EAFNOSUPPORT;
  264. priv->slave = slave;
  265. /* Set slave address */
  266. writeb(slave->addr << 1, priv->base + I2C_OFS_SVA0);
  267. return 0;
  268. }
  269. static int em_i2c_unreg_slave(struct i2c_client *slave)
  270. {
  271. struct em_i2c_device *priv = i2c_get_adapdata(slave->adapter);
  272. WARN_ON(!priv->slave);
  273. writeb(0, priv->base + I2C_OFS_SVA0);
  274. priv->slave = NULL;
  275. return 0;
  276. }
  277. static const struct i2c_algorithm em_i2c_algo = {
  278. .master_xfer = em_i2c_xfer,
  279. .functionality = em_i2c_func,
  280. .reg_slave = em_i2c_reg_slave,
  281. .unreg_slave = em_i2c_unreg_slave,
  282. };
  283. static int em_i2c_probe(struct platform_device *pdev)
  284. {
  285. struct em_i2c_device *priv;
  286. struct resource *r;
  287. int irq, ret;
  288. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  289. if (!priv)
  290. return -ENOMEM;
  291. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  292. priv->base = devm_ioremap_resource(&pdev->dev, r);
  293. if (IS_ERR(priv->base))
  294. return PTR_ERR(priv->base);
  295. strlcpy(priv->adap.name, "EMEV2 I2C", sizeof(priv->adap.name));
  296. priv->sclk = devm_clk_get(&pdev->dev, "sclk");
  297. if (IS_ERR(priv->sclk))
  298. return PTR_ERR(priv->sclk);
  299. ret = clk_prepare_enable(priv->sclk);
  300. if (ret)
  301. return ret;
  302. priv->adap.timeout = msecs_to_jiffies(100);
  303. priv->adap.retries = 5;
  304. priv->adap.dev.parent = &pdev->dev;
  305. priv->adap.algo = &em_i2c_algo;
  306. priv->adap.owner = THIS_MODULE;
  307. priv->adap.dev.of_node = pdev->dev.of_node;
  308. init_completion(&priv->msg_done);
  309. platform_set_drvdata(pdev, priv);
  310. i2c_set_adapdata(&priv->adap, priv);
  311. em_i2c_reset(&priv->adap);
  312. irq = platform_get_irq(pdev, 0);
  313. ret = devm_request_irq(&pdev->dev, irq, em_i2c_irq_handler, 0,
  314. "em_i2c", priv);
  315. if (ret)
  316. goto err_clk;
  317. ret = i2c_add_adapter(&priv->adap);
  318. if (ret)
  319. goto err_clk;
  320. dev_info(&pdev->dev, "Added i2c controller %d, irq %d\n", priv->adap.nr, irq);
  321. return 0;
  322. err_clk:
  323. clk_disable_unprepare(priv->sclk);
  324. return ret;
  325. }
  326. static int em_i2c_remove(struct platform_device *dev)
  327. {
  328. struct em_i2c_device *priv = platform_get_drvdata(dev);
  329. i2c_del_adapter(&priv->adap);
  330. clk_disable_unprepare(priv->sclk);
  331. return 0;
  332. }
  333. static const struct of_device_id em_i2c_ids[] = {
  334. { .compatible = "renesas,iic-emev2", },
  335. { }
  336. };
  337. static struct platform_driver em_i2c_driver = {
  338. .probe = em_i2c_probe,
  339. .remove = em_i2c_remove,
  340. .driver = {
  341. .name = "em-i2c",
  342. .of_match_table = em_i2c_ids,
  343. }
  344. };
  345. module_platform_driver(em_i2c_driver);
  346. MODULE_DESCRIPTION("EMEV2 I2C bus driver");
  347. MODULE_AUTHOR("Ian Molton and Wolfram Sang <wsa@sang-engineering.com>");
  348. MODULE_LICENSE("GPL v2");
  349. MODULE_DEVICE_TABLE(of, em_i2c_ids);