i2c-uniphier.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. unsigned int clk_cycle;
  53. };
  54. static irqreturn_t uniphier_i2c_interrupt(int irq, void *dev_id)
  55. {
  56. struct uniphier_i2c_priv *priv = dev_id;
  57. /*
  58. * This hardware uses edge triggered interrupt. Do not touch the
  59. * hardware registers in this handler to make sure to catch the next
  60. * interrupt edge. Just send a complete signal and return.
  61. */
  62. complete(&priv->comp);
  63. return IRQ_HANDLED;
  64. }
  65. static int uniphier_i2c_xfer_byte(struct i2c_adapter *adap, u32 txdata,
  66. u32 *rxdatap)
  67. {
  68. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  69. unsigned long time_left;
  70. u32 rxdata;
  71. reinit_completion(&priv->comp);
  72. txdata |= UNIPHIER_I2C_DTRM_IRQEN;
  73. dev_dbg(&adap->dev, "write data: 0x%04x\n", txdata);
  74. writel(txdata, priv->membase + UNIPHIER_I2C_DTRM);
  75. time_left = wait_for_completion_timeout(&priv->comp, adap->timeout);
  76. if (unlikely(!time_left)) {
  77. dev_err(&adap->dev, "transaction timeout\n");
  78. return -ETIMEDOUT;
  79. }
  80. rxdata = readl(priv->membase + UNIPHIER_I2C_DREC);
  81. dev_dbg(&adap->dev, "read data: 0x%04x\n", rxdata);
  82. if (rxdatap)
  83. *rxdatap = rxdata;
  84. return 0;
  85. }
  86. static int uniphier_i2c_send_byte(struct i2c_adapter *adap, u32 txdata)
  87. {
  88. u32 rxdata;
  89. int ret;
  90. ret = uniphier_i2c_xfer_byte(adap, txdata, &rxdata);
  91. if (ret)
  92. return ret;
  93. if (unlikely(rxdata & UNIPHIER_I2C_DREC_LAB)) {
  94. dev_dbg(&adap->dev, "arbitration lost\n");
  95. return -EAGAIN;
  96. }
  97. if (unlikely(rxdata & UNIPHIER_I2C_DREC_LRB)) {
  98. dev_dbg(&adap->dev, "could not get ACK\n");
  99. return -ENXIO;
  100. }
  101. return 0;
  102. }
  103. static int uniphier_i2c_tx(struct i2c_adapter *adap, u16 addr, u16 len,
  104. const u8 *buf)
  105. {
  106. int ret;
  107. dev_dbg(&adap->dev, "start condition\n");
  108. ret = uniphier_i2c_send_byte(adap, addr << 1 |
  109. UNIPHIER_I2C_DTRM_STA |
  110. UNIPHIER_I2C_DTRM_NACK);
  111. if (ret)
  112. return ret;
  113. while (len--) {
  114. ret = uniphier_i2c_send_byte(adap,
  115. UNIPHIER_I2C_DTRM_NACK | *buf++);
  116. if (ret)
  117. return ret;
  118. }
  119. return 0;
  120. }
  121. static int uniphier_i2c_rx(struct i2c_adapter *adap, u16 addr, u16 len,
  122. u8 *buf)
  123. {
  124. int ret;
  125. dev_dbg(&adap->dev, "start condition\n");
  126. ret = uniphier_i2c_send_byte(adap, addr << 1 |
  127. UNIPHIER_I2C_DTRM_STA |
  128. UNIPHIER_I2C_DTRM_NACK |
  129. UNIPHIER_I2C_DTRM_RD);
  130. if (ret)
  131. return ret;
  132. while (len--) {
  133. u32 rxdata;
  134. ret = uniphier_i2c_xfer_byte(adap,
  135. len ? 0 : UNIPHIER_I2C_DTRM_NACK,
  136. &rxdata);
  137. if (ret)
  138. return ret;
  139. *buf++ = rxdata;
  140. }
  141. return 0;
  142. }
  143. static int uniphier_i2c_stop(struct i2c_adapter *adap)
  144. {
  145. dev_dbg(&adap->dev, "stop condition\n");
  146. return uniphier_i2c_send_byte(adap, UNIPHIER_I2C_DTRM_STO |
  147. UNIPHIER_I2C_DTRM_NACK);
  148. }
  149. static int uniphier_i2c_master_xfer_one(struct i2c_adapter *adap,
  150. struct i2c_msg *msg, bool stop)
  151. {
  152. bool is_read = msg->flags & I2C_M_RD;
  153. bool recovery = false;
  154. int ret;
  155. dev_dbg(&adap->dev, "%s: addr=0x%02x, len=%d, stop=%d\n",
  156. is_read ? "receive" : "transmit", msg->addr, msg->len, stop);
  157. if (is_read)
  158. ret = uniphier_i2c_rx(adap, msg->addr, msg->len, msg->buf);
  159. else
  160. ret = uniphier_i2c_tx(adap, msg->addr, msg->len, msg->buf);
  161. if (ret == -EAGAIN) /* could not acquire bus. bail out without STOP */
  162. return ret;
  163. if (ret == -ETIMEDOUT) {
  164. /* This error is fatal. Needs recovery. */
  165. stop = false;
  166. recovery = true;
  167. }
  168. if (stop) {
  169. int ret2 = uniphier_i2c_stop(adap);
  170. if (ret2) {
  171. /* Failed to issue STOP. The bus needs recovery. */
  172. recovery = true;
  173. ret = ret ?: ret2;
  174. }
  175. }
  176. if (recovery)
  177. i2c_recover_bus(adap);
  178. return ret;
  179. }
  180. static int uniphier_i2c_check_bus_busy(struct i2c_adapter *adap)
  181. {
  182. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  183. if (!(readl(priv->membase + UNIPHIER_I2C_DREC) &
  184. UNIPHIER_I2C_DREC_BBN)) {
  185. if (priv->busy_cnt++ > 3) {
  186. /*
  187. * If bus busy continues too long, it is probably
  188. * in a wrong state. Try bus recovery.
  189. */
  190. i2c_recover_bus(adap);
  191. priv->busy_cnt = 0;
  192. }
  193. return -EAGAIN;
  194. }
  195. priv->busy_cnt = 0;
  196. return 0;
  197. }
  198. static int uniphier_i2c_master_xfer(struct i2c_adapter *adap,
  199. struct i2c_msg *msgs, int num)
  200. {
  201. struct i2c_msg *msg, *emsg = msgs + num;
  202. int ret;
  203. ret = uniphier_i2c_check_bus_busy(adap);
  204. if (ret)
  205. return ret;
  206. for (msg = msgs; msg < emsg; msg++) {
  207. /* If next message is read, skip the stop condition */
  208. bool stop = !(msg + 1 < emsg && msg[1].flags & I2C_M_RD);
  209. /* but, force it if I2C_M_STOP is set */
  210. if (msg->flags & I2C_M_STOP)
  211. stop = true;
  212. ret = uniphier_i2c_master_xfer_one(adap, msg, stop);
  213. if (ret)
  214. return ret;
  215. }
  216. return num;
  217. }
  218. static u32 uniphier_i2c_functionality(struct i2c_adapter *adap)
  219. {
  220. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  221. }
  222. static const struct i2c_algorithm uniphier_i2c_algo = {
  223. .master_xfer = uniphier_i2c_master_xfer,
  224. .functionality = uniphier_i2c_functionality,
  225. };
  226. static void uniphier_i2c_reset(struct uniphier_i2c_priv *priv, bool reset_on)
  227. {
  228. u32 val = UNIPHIER_I2C_BRST_RSCL;
  229. val |= reset_on ? 0 : UNIPHIER_I2C_BRST_FOEN;
  230. writel(val, priv->membase + UNIPHIER_I2C_BRST);
  231. }
  232. static int uniphier_i2c_get_scl(struct i2c_adapter *adap)
  233. {
  234. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  235. return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
  236. UNIPHIER_I2C_BSTS_SCL);
  237. }
  238. static void uniphier_i2c_set_scl(struct i2c_adapter *adap, int val)
  239. {
  240. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  241. writel(val ? UNIPHIER_I2C_BRST_RSCL : 0,
  242. priv->membase + UNIPHIER_I2C_BRST);
  243. }
  244. static int uniphier_i2c_get_sda(struct i2c_adapter *adap)
  245. {
  246. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  247. return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
  248. UNIPHIER_I2C_BSTS_SDA);
  249. }
  250. static void uniphier_i2c_unprepare_recovery(struct i2c_adapter *adap)
  251. {
  252. uniphier_i2c_reset(i2c_get_adapdata(adap), false);
  253. }
  254. static struct i2c_bus_recovery_info uniphier_i2c_bus_recovery_info = {
  255. .recover_bus = i2c_generic_scl_recovery,
  256. .get_scl = uniphier_i2c_get_scl,
  257. .set_scl = uniphier_i2c_set_scl,
  258. .get_sda = uniphier_i2c_get_sda,
  259. .unprepare_recovery = uniphier_i2c_unprepare_recovery,
  260. };
  261. static void uniphier_i2c_hw_init(struct uniphier_i2c_priv *priv)
  262. {
  263. unsigned int cyc = priv->clk_cycle;
  264. uniphier_i2c_reset(priv, true);
  265. writel((cyc / 2 << 16) | cyc, priv->membase + UNIPHIER_I2C_CLK);
  266. uniphier_i2c_reset(priv, false);
  267. }
  268. static int uniphier_i2c_probe(struct platform_device *pdev)
  269. {
  270. struct device *dev = &pdev->dev;
  271. struct uniphier_i2c_priv *priv;
  272. struct resource *regs;
  273. u32 bus_speed;
  274. unsigned long clk_rate;
  275. int irq, ret;
  276. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  277. if (!priv)
  278. return -ENOMEM;
  279. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  280. priv->membase = devm_ioremap_resource(dev, regs);
  281. if (IS_ERR(priv->membase))
  282. return PTR_ERR(priv->membase);
  283. irq = platform_get_irq(pdev, 0);
  284. if (irq < 0) {
  285. dev_err(dev, "failed to get IRQ number\n");
  286. return irq;
  287. }
  288. if (of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed))
  289. bus_speed = UNIPHIER_I2C_DEFAULT_SPEED;
  290. if (!bus_speed || bus_speed > UNIPHIER_I2C_MAX_SPEED) {
  291. dev_err(dev, "invalid clock-frequency %d\n", bus_speed);
  292. return -EINVAL;
  293. }
  294. priv->clk = devm_clk_get(dev, NULL);
  295. if (IS_ERR(priv->clk)) {
  296. dev_err(dev, "failed to get clock\n");
  297. return PTR_ERR(priv->clk);
  298. }
  299. ret = clk_prepare_enable(priv->clk);
  300. if (ret)
  301. return ret;
  302. clk_rate = clk_get_rate(priv->clk);
  303. if (!clk_rate) {
  304. dev_err(dev, "input clock rate should not be zero\n");
  305. ret = -EINVAL;
  306. goto disable_clk;
  307. }
  308. priv->clk_cycle = clk_rate / bus_speed;
  309. init_completion(&priv->comp);
  310. priv->adap.owner = THIS_MODULE;
  311. priv->adap.algo = &uniphier_i2c_algo;
  312. priv->adap.dev.parent = dev;
  313. priv->adap.dev.of_node = dev->of_node;
  314. strlcpy(priv->adap.name, "UniPhier I2C", sizeof(priv->adap.name));
  315. priv->adap.bus_recovery_info = &uniphier_i2c_bus_recovery_info;
  316. i2c_set_adapdata(&priv->adap, priv);
  317. platform_set_drvdata(pdev, priv);
  318. uniphier_i2c_hw_init(priv);
  319. ret = devm_request_irq(dev, irq, uniphier_i2c_interrupt, 0, pdev->name,
  320. priv);
  321. if (ret) {
  322. dev_err(dev, "failed to request irq %d\n", irq);
  323. goto disable_clk;
  324. }
  325. ret = i2c_add_adapter(&priv->adap);
  326. disable_clk:
  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 int __maybe_unused uniphier_i2c_suspend(struct device *dev)
  339. {
  340. struct uniphier_i2c_priv *priv = dev_get_drvdata(dev);
  341. clk_disable_unprepare(priv->clk);
  342. return 0;
  343. }
  344. static int __maybe_unused uniphier_i2c_resume(struct device *dev)
  345. {
  346. struct uniphier_i2c_priv *priv = dev_get_drvdata(dev);
  347. int ret;
  348. ret = clk_prepare_enable(priv->clk);
  349. if (ret)
  350. return ret;
  351. uniphier_i2c_hw_init(priv);
  352. return 0;
  353. }
  354. static const struct dev_pm_ops uniphier_i2c_pm_ops = {
  355. SET_SYSTEM_SLEEP_PM_OPS(uniphier_i2c_suspend, uniphier_i2c_resume)
  356. };
  357. static const struct of_device_id uniphier_i2c_match[] = {
  358. { .compatible = "socionext,uniphier-i2c" },
  359. { /* sentinel */ }
  360. };
  361. MODULE_DEVICE_TABLE(of, uniphier_i2c_match);
  362. static struct platform_driver uniphier_i2c_drv = {
  363. .probe = uniphier_i2c_probe,
  364. .remove = uniphier_i2c_remove,
  365. .driver = {
  366. .name = "uniphier-i2c",
  367. .of_match_table = uniphier_i2c_match,
  368. .pm = &uniphier_i2c_pm_ops,
  369. },
  370. };
  371. module_platform_driver(uniphier_i2c_drv);
  372. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  373. MODULE_DESCRIPTION("UniPhier I2C bus driver");
  374. MODULE_LICENSE("GPL");