i2c-uniphier.c 11 KB

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