i2c-emev2.c 10 KB

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