i2c-uniphier.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #define UNIPHIER_I2C_DTRM 0x00 /* TX register */
  21. #define UNIPHIER_I2C_DTRM_IRQEN BIT(11) /* enable interrupt */
  22. #define UNIPHIER_I2C_DTRM_STA BIT(10) /* start condition */
  23. #define UNIPHIER_I2C_DTRM_STO BIT(9) /* stop condition */
  24. #define UNIPHIER_I2C_DTRM_NACK BIT(8) /* do not return ACK */
  25. #define UNIPHIER_I2C_DTRM_RD BIT(0) /* read transaction */
  26. #define UNIPHIER_I2C_DREC 0x04 /* RX register */
  27. #define UNIPHIER_I2C_DREC_MST BIT(14) /* 1 = master, 0 = slave */
  28. #define UNIPHIER_I2C_DREC_TX BIT(13) /* 1 = transmit, 0 = receive */
  29. #define UNIPHIER_I2C_DREC_STS BIT(12) /* stop condition detected */
  30. #define UNIPHIER_I2C_DREC_LRB BIT(11) /* no ACK */
  31. #define UNIPHIER_I2C_DREC_LAB BIT(9) /* arbitration lost */
  32. #define UNIPHIER_I2C_DREC_BBN BIT(8) /* bus not busy */
  33. #define UNIPHIER_I2C_MYAD 0x08 /* slave address */
  34. #define UNIPHIER_I2C_CLK 0x0c /* clock frequency control */
  35. #define UNIPHIER_I2C_BRST 0x10 /* bus reset */
  36. #define UNIPHIER_I2C_BRST_FOEN BIT(1) /* normal operation */
  37. #define UNIPHIER_I2C_BRST_RSCL BIT(0) /* release SCL */
  38. #define UNIPHIER_I2C_HOLD 0x14 /* hold time control */
  39. #define UNIPHIER_I2C_BSTS 0x18 /* bus status monitor */
  40. #define UNIPHIER_I2C_BSTS_SDA BIT(1) /* readback of SDA line */
  41. #define UNIPHIER_I2C_BSTS_SCL BIT(0) /* readback of SCL line */
  42. #define UNIPHIER_I2C_NOISE 0x1c /* noise filter control */
  43. #define UNIPHIER_I2C_SETUP 0x20 /* setup time control */
  44. #define UNIPHIER_I2C_DEFAULT_SPEED 100000
  45. #define UNIPHIER_I2C_MAX_SPEED 400000
  46. struct uniphier_i2c_priv {
  47. struct completion comp;
  48. struct i2c_adapter adap;
  49. void __iomem *membase;
  50. struct clk *clk;
  51. unsigned int busy_cnt;
  52. };
  53. static irqreturn_t uniphier_i2c_interrupt(int irq, void *dev_id)
  54. {
  55. struct uniphier_i2c_priv *priv = dev_id;
  56. /*
  57. * This hardware uses edge triggered interrupt. Do not touch the
  58. * hardware registers in this handler to make sure to catch the next
  59. * interrupt edge. Just send a complete signal and return.
  60. */
  61. complete(&priv->comp);
  62. return IRQ_HANDLED;
  63. }
  64. static int uniphier_i2c_xfer_byte(struct i2c_adapter *adap, u32 txdata,
  65. u32 *rxdatap)
  66. {
  67. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  68. unsigned long time_left;
  69. u32 rxdata;
  70. reinit_completion(&priv->comp);
  71. txdata |= UNIPHIER_I2C_DTRM_IRQEN;
  72. dev_dbg(&adap->dev, "write data: 0x%04x\n", txdata);
  73. writel(txdata, priv->membase + UNIPHIER_I2C_DTRM);
  74. time_left = wait_for_completion_timeout(&priv->comp, adap->timeout);
  75. if (unlikely(!time_left)) {
  76. dev_err(&adap->dev, "transaction timeout\n");
  77. return -ETIMEDOUT;
  78. }
  79. rxdata = readl(priv->membase + UNIPHIER_I2C_DREC);
  80. dev_dbg(&adap->dev, "read data: 0x%04x\n", rxdata);
  81. if (rxdatap)
  82. *rxdatap = rxdata;
  83. return 0;
  84. }
  85. static int uniphier_i2c_send_byte(struct i2c_adapter *adap, u32 txdata)
  86. {
  87. u32 rxdata;
  88. int ret;
  89. ret = uniphier_i2c_xfer_byte(adap, txdata, &rxdata);
  90. if (ret)
  91. return ret;
  92. if (unlikely(rxdata & UNIPHIER_I2C_DREC_LAB)) {
  93. dev_dbg(&adap->dev, "arbitration lost\n");
  94. return -EAGAIN;
  95. }
  96. if (unlikely(rxdata & UNIPHIER_I2C_DREC_LRB)) {
  97. dev_dbg(&adap->dev, "could not get ACK\n");
  98. return -ENXIO;
  99. }
  100. return 0;
  101. }
  102. static int uniphier_i2c_tx(struct i2c_adapter *adap, u16 addr, u16 len,
  103. const u8 *buf)
  104. {
  105. int ret;
  106. dev_dbg(&adap->dev, "start condition\n");
  107. ret = uniphier_i2c_send_byte(adap, addr << 1 |
  108. UNIPHIER_I2C_DTRM_STA |
  109. UNIPHIER_I2C_DTRM_NACK);
  110. if (ret)
  111. return ret;
  112. while (len--) {
  113. ret = uniphier_i2c_send_byte(adap,
  114. UNIPHIER_I2C_DTRM_NACK | *buf++);
  115. if (ret)
  116. return ret;
  117. }
  118. return 0;
  119. }
  120. static int uniphier_i2c_rx(struct i2c_adapter *adap, u16 addr, u16 len,
  121. u8 *buf)
  122. {
  123. int ret;
  124. dev_dbg(&adap->dev, "start condition\n");
  125. ret = uniphier_i2c_send_byte(adap, addr << 1 |
  126. UNIPHIER_I2C_DTRM_STA |
  127. UNIPHIER_I2C_DTRM_NACK |
  128. UNIPHIER_I2C_DTRM_RD);
  129. if (ret)
  130. return ret;
  131. while (len--) {
  132. u32 rxdata;
  133. ret = uniphier_i2c_xfer_byte(adap,
  134. len ? 0 : UNIPHIER_I2C_DTRM_NACK,
  135. &rxdata);
  136. if (ret)
  137. return ret;
  138. *buf++ = rxdata;
  139. }
  140. return 0;
  141. }
  142. static int uniphier_i2c_stop(struct i2c_adapter *adap)
  143. {
  144. dev_dbg(&adap->dev, "stop condition\n");
  145. return uniphier_i2c_send_byte(adap, UNIPHIER_I2C_DTRM_STO |
  146. UNIPHIER_I2C_DTRM_NACK);
  147. }
  148. static int uniphier_i2c_master_xfer_one(struct i2c_adapter *adap,
  149. struct i2c_msg *msg, bool stop)
  150. {
  151. bool is_read = msg->flags & I2C_M_RD;
  152. bool recovery = false;
  153. int ret;
  154. dev_dbg(&adap->dev, "%s: addr=0x%02x, len=%d, stop=%d\n",
  155. is_read ? "receive" : "transmit", msg->addr, msg->len, stop);
  156. if (is_read)
  157. ret = uniphier_i2c_rx(adap, msg->addr, msg->len, msg->buf);
  158. else
  159. ret = uniphier_i2c_tx(adap, msg->addr, msg->len, msg->buf);
  160. if (ret == -EAGAIN) /* could not acquire bus. bail out without STOP */
  161. return ret;
  162. if (ret == -ETIMEDOUT) {
  163. /* This error is fatal. Needs recovery. */
  164. stop = false;
  165. recovery = true;
  166. }
  167. if (stop) {
  168. int ret2 = uniphier_i2c_stop(adap);
  169. if (ret2) {
  170. /* Failed to issue STOP. The bus needs recovery. */
  171. recovery = true;
  172. ret = ret ?: ret2;
  173. }
  174. }
  175. if (recovery)
  176. i2c_recover_bus(adap);
  177. return ret;
  178. }
  179. static int uniphier_i2c_check_bus_busy(struct i2c_adapter *adap)
  180. {
  181. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  182. if (!(readl(priv->membase + UNIPHIER_I2C_DREC) &
  183. UNIPHIER_I2C_DREC_BBN)) {
  184. if (priv->busy_cnt++ > 3) {
  185. /*
  186. * If bus busy continues too long, it is probably
  187. * in a wrong state. Try bus recovery.
  188. */
  189. i2c_recover_bus(adap);
  190. priv->busy_cnt = 0;
  191. }
  192. return -EAGAIN;
  193. }
  194. priv->busy_cnt = 0;
  195. return 0;
  196. }
  197. static int uniphier_i2c_master_xfer(struct i2c_adapter *adap,
  198. struct i2c_msg *msgs, int num)
  199. {
  200. struct i2c_msg *msg, *emsg = msgs + num;
  201. int ret;
  202. ret = uniphier_i2c_check_bus_busy(adap);
  203. if (ret)
  204. return ret;
  205. for (msg = msgs; msg < emsg; msg++) {
  206. /* Emit STOP if it is the last message or I2C_M_STOP is set. */
  207. bool stop = (msg + 1 == emsg) || (msg->flags & I2C_M_STOP);
  208. ret = uniphier_i2c_master_xfer_one(adap, msg, stop);
  209. if (ret)
  210. return ret;
  211. }
  212. return num;
  213. }
  214. static u32 uniphier_i2c_functionality(struct i2c_adapter *adap)
  215. {
  216. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  217. }
  218. static const struct i2c_algorithm uniphier_i2c_algo = {
  219. .master_xfer = uniphier_i2c_master_xfer,
  220. .functionality = uniphier_i2c_functionality,
  221. };
  222. static void uniphier_i2c_reset(struct uniphier_i2c_priv *priv, bool reset_on)
  223. {
  224. u32 val = UNIPHIER_I2C_BRST_RSCL;
  225. val |= reset_on ? 0 : UNIPHIER_I2C_BRST_FOEN;
  226. writel(val, priv->membase + UNIPHIER_I2C_BRST);
  227. }
  228. static int uniphier_i2c_get_scl(struct i2c_adapter *adap)
  229. {
  230. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  231. return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
  232. UNIPHIER_I2C_BSTS_SCL);
  233. }
  234. static void uniphier_i2c_set_scl(struct i2c_adapter *adap, int val)
  235. {
  236. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  237. writel(val ? UNIPHIER_I2C_BRST_RSCL : 0,
  238. priv->membase + UNIPHIER_I2C_BRST);
  239. }
  240. static int uniphier_i2c_get_sda(struct i2c_adapter *adap)
  241. {
  242. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  243. return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
  244. UNIPHIER_I2C_BSTS_SDA);
  245. }
  246. static void uniphier_i2c_unprepare_recovery(struct i2c_adapter *adap)
  247. {
  248. uniphier_i2c_reset(i2c_get_adapdata(adap), false);
  249. }
  250. static struct i2c_bus_recovery_info uniphier_i2c_bus_recovery_info = {
  251. .recover_bus = i2c_generic_scl_recovery,
  252. .get_scl = uniphier_i2c_get_scl,
  253. .set_scl = uniphier_i2c_set_scl,
  254. .get_sda = uniphier_i2c_get_sda,
  255. .unprepare_recovery = uniphier_i2c_unprepare_recovery,
  256. };
  257. static int uniphier_i2c_clk_init(struct device *dev,
  258. struct uniphier_i2c_priv *priv)
  259. {
  260. struct device_node *np = dev->of_node;
  261. unsigned long clk_rate;
  262. u32 bus_speed;
  263. int ret;
  264. if (of_property_read_u32(np, "clock-frequency", &bus_speed))
  265. bus_speed = UNIPHIER_I2C_DEFAULT_SPEED;
  266. if (bus_speed > UNIPHIER_I2C_MAX_SPEED)
  267. bus_speed = UNIPHIER_I2C_MAX_SPEED;
  268. /* Get input clk rate through clk driver */
  269. priv->clk = devm_clk_get(dev, NULL);
  270. if (IS_ERR(priv->clk)) {
  271. dev_err(dev, "failed to get clock\n");
  272. return PTR_ERR(priv->clk);
  273. }
  274. ret = clk_prepare_enable(priv->clk);
  275. if (ret)
  276. return ret;
  277. clk_rate = clk_get_rate(priv->clk);
  278. uniphier_i2c_reset(priv, true);
  279. writel((clk_rate / bus_speed / 2 << 16) | (clk_rate / bus_speed),
  280. priv->membase + UNIPHIER_I2C_CLK);
  281. uniphier_i2c_reset(priv, false);
  282. return 0;
  283. }
  284. static int uniphier_i2c_probe(struct platform_device *pdev)
  285. {
  286. struct device *dev = &pdev->dev;
  287. struct uniphier_i2c_priv *priv;
  288. struct resource *regs;
  289. int irq;
  290. int ret;
  291. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  292. if (!priv)
  293. return -ENOMEM;
  294. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  295. priv->membase = devm_ioremap_resource(dev, regs);
  296. if (IS_ERR(priv->membase))
  297. return PTR_ERR(priv->membase);
  298. irq = platform_get_irq(pdev, 0);
  299. if (irq < 0) {
  300. dev_err(dev, "failed to get IRQ number");
  301. return irq;
  302. }
  303. init_completion(&priv->comp);
  304. priv->adap.owner = THIS_MODULE;
  305. priv->adap.algo = &uniphier_i2c_algo;
  306. priv->adap.dev.parent = dev;
  307. priv->adap.dev.of_node = dev->of_node;
  308. strlcpy(priv->adap.name, "UniPhier I2C", sizeof(priv->adap.name));
  309. priv->adap.bus_recovery_info = &uniphier_i2c_bus_recovery_info;
  310. i2c_set_adapdata(&priv->adap, priv);
  311. platform_set_drvdata(pdev, priv);
  312. ret = uniphier_i2c_clk_init(dev, priv);
  313. if (ret)
  314. return ret;
  315. ret = devm_request_irq(dev, irq, uniphier_i2c_interrupt, 0, pdev->name,
  316. priv);
  317. if (ret) {
  318. dev_err(dev, "failed to request irq %d\n", irq);
  319. goto err;
  320. }
  321. ret = i2c_add_adapter(&priv->adap);
  322. if (ret) {
  323. dev_err(dev, "failed to add I2C adapter\n");
  324. goto err;
  325. }
  326. err:
  327. if (ret)
  328. clk_disable_unprepare(priv->clk);
  329. return ret;
  330. }
  331. static int uniphier_i2c_remove(struct platform_device *pdev)
  332. {
  333. struct uniphier_i2c_priv *priv = platform_get_drvdata(pdev);
  334. i2c_del_adapter(&priv->adap);
  335. clk_disable_unprepare(priv->clk);
  336. return 0;
  337. }
  338. static const struct of_device_id uniphier_i2c_match[] = {
  339. { .compatible = "socionext,uniphier-i2c" },
  340. { /* sentinel */ }
  341. };
  342. MODULE_DEVICE_TABLE(of, uniphier_i2c_match);
  343. static struct platform_driver uniphier_i2c_drv = {
  344. .probe = uniphier_i2c_probe,
  345. .remove = uniphier_i2c_remove,
  346. .driver = {
  347. .name = "uniphier-i2c",
  348. .of_match_table = uniphier_i2c_match,
  349. },
  350. };
  351. module_platform_driver(uniphier_i2c_drv);
  352. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  353. MODULE_DESCRIPTION("UniPhier I2C bus driver");
  354. MODULE_LICENSE("GPL");