i2c-uniphier.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /* Emit STOP if it is the last message or I2C_M_STOP is set. */
  208. bool stop = (msg + 1 == emsg) || (msg->flags & I2C_M_STOP);
  209. ret = uniphier_i2c_master_xfer_one(adap, msg, stop);
  210. if (ret)
  211. return ret;
  212. }
  213. return num;
  214. }
  215. static u32 uniphier_i2c_functionality(struct i2c_adapter *adap)
  216. {
  217. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  218. }
  219. static const struct i2c_algorithm uniphier_i2c_algo = {
  220. .master_xfer = uniphier_i2c_master_xfer,
  221. .functionality = uniphier_i2c_functionality,
  222. };
  223. static void uniphier_i2c_reset(struct uniphier_i2c_priv *priv, bool reset_on)
  224. {
  225. u32 val = UNIPHIER_I2C_BRST_RSCL;
  226. val |= reset_on ? 0 : UNIPHIER_I2C_BRST_FOEN;
  227. writel(val, priv->membase + UNIPHIER_I2C_BRST);
  228. }
  229. static int uniphier_i2c_get_scl(struct i2c_adapter *adap)
  230. {
  231. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  232. return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
  233. UNIPHIER_I2C_BSTS_SCL);
  234. }
  235. static void uniphier_i2c_set_scl(struct i2c_adapter *adap, int val)
  236. {
  237. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  238. writel(val ? UNIPHIER_I2C_BRST_RSCL : 0,
  239. priv->membase + UNIPHIER_I2C_BRST);
  240. }
  241. static int uniphier_i2c_get_sda(struct i2c_adapter *adap)
  242. {
  243. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  244. return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
  245. UNIPHIER_I2C_BSTS_SDA);
  246. }
  247. static void uniphier_i2c_unprepare_recovery(struct i2c_adapter *adap)
  248. {
  249. uniphier_i2c_reset(i2c_get_adapdata(adap), false);
  250. }
  251. static struct i2c_bus_recovery_info uniphier_i2c_bus_recovery_info = {
  252. .recover_bus = i2c_generic_scl_recovery,
  253. .get_scl = uniphier_i2c_get_scl,
  254. .set_scl = uniphier_i2c_set_scl,
  255. .get_sda = uniphier_i2c_get_sda,
  256. .unprepare_recovery = uniphier_i2c_unprepare_recovery,
  257. };
  258. static void uniphier_i2c_hw_init(struct uniphier_i2c_priv *priv)
  259. {
  260. unsigned int cyc = priv->clk_cycle;
  261. uniphier_i2c_reset(priv, true);
  262. /*
  263. * Bit30-16: clock cycles of tLOW.
  264. * Standard-mode: tLOW = 4.7 us, tHIGH = 4.0 us
  265. * Fast-mode: tLOW = 1.3 us, tHIGH = 0.6 us
  266. * "tLow/tHIGH = 5/4" meets both.
  267. */
  268. writel((cyc * 5 / 9 << 16) | cyc, priv->membase + UNIPHIER_I2C_CLK);
  269. uniphier_i2c_reset(priv, false);
  270. }
  271. static int uniphier_i2c_probe(struct platform_device *pdev)
  272. {
  273. struct device *dev = &pdev->dev;
  274. struct uniphier_i2c_priv *priv;
  275. struct resource *regs;
  276. u32 bus_speed;
  277. unsigned long clk_rate;
  278. int irq, ret;
  279. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  280. if (!priv)
  281. return -ENOMEM;
  282. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  283. priv->membase = devm_ioremap_resource(dev, regs);
  284. if (IS_ERR(priv->membase))
  285. return PTR_ERR(priv->membase);
  286. irq = platform_get_irq(pdev, 0);
  287. if (irq < 0) {
  288. dev_err(dev, "failed to get IRQ number\n");
  289. return irq;
  290. }
  291. if (of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed))
  292. bus_speed = UNIPHIER_I2C_DEFAULT_SPEED;
  293. if (!bus_speed || bus_speed > UNIPHIER_I2C_MAX_SPEED) {
  294. dev_err(dev, "invalid clock-frequency %d\n", bus_speed);
  295. return -EINVAL;
  296. }
  297. priv->clk = devm_clk_get(dev, NULL);
  298. if (IS_ERR(priv->clk)) {
  299. dev_err(dev, "failed to get clock\n");
  300. return PTR_ERR(priv->clk);
  301. }
  302. ret = clk_prepare_enable(priv->clk);
  303. if (ret)
  304. return ret;
  305. clk_rate = clk_get_rate(priv->clk);
  306. if (!clk_rate) {
  307. dev_err(dev, "input clock rate should not be zero\n");
  308. ret = -EINVAL;
  309. goto disable_clk;
  310. }
  311. priv->clk_cycle = clk_rate / bus_speed;
  312. init_completion(&priv->comp);
  313. priv->adap.owner = THIS_MODULE;
  314. priv->adap.algo = &uniphier_i2c_algo;
  315. priv->adap.dev.parent = dev;
  316. priv->adap.dev.of_node = dev->of_node;
  317. strlcpy(priv->adap.name, "UniPhier I2C", sizeof(priv->adap.name));
  318. priv->adap.bus_recovery_info = &uniphier_i2c_bus_recovery_info;
  319. i2c_set_adapdata(&priv->adap, priv);
  320. platform_set_drvdata(pdev, priv);
  321. uniphier_i2c_hw_init(priv);
  322. ret = devm_request_irq(dev, irq, uniphier_i2c_interrupt, 0, pdev->name,
  323. priv);
  324. if (ret) {
  325. dev_err(dev, "failed to request irq %d\n", irq);
  326. goto disable_clk;
  327. }
  328. ret = i2c_add_adapter(&priv->adap);
  329. disable_clk:
  330. if (ret)
  331. clk_disable_unprepare(priv->clk);
  332. return ret;
  333. }
  334. static int uniphier_i2c_remove(struct platform_device *pdev)
  335. {
  336. struct uniphier_i2c_priv *priv = platform_get_drvdata(pdev);
  337. i2c_del_adapter(&priv->adap);
  338. clk_disable_unprepare(priv->clk);
  339. return 0;
  340. }
  341. static int __maybe_unused uniphier_i2c_suspend(struct device *dev)
  342. {
  343. struct uniphier_i2c_priv *priv = dev_get_drvdata(dev);
  344. clk_disable_unprepare(priv->clk);
  345. return 0;
  346. }
  347. static int __maybe_unused uniphier_i2c_resume(struct device *dev)
  348. {
  349. struct uniphier_i2c_priv *priv = dev_get_drvdata(dev);
  350. int ret;
  351. ret = clk_prepare_enable(priv->clk);
  352. if (ret)
  353. return ret;
  354. uniphier_i2c_hw_init(priv);
  355. return 0;
  356. }
  357. static const struct dev_pm_ops uniphier_i2c_pm_ops = {
  358. SET_SYSTEM_SLEEP_PM_OPS(uniphier_i2c_suspend, uniphier_i2c_resume)
  359. };
  360. static const struct of_device_id uniphier_i2c_match[] = {
  361. { .compatible = "socionext,uniphier-i2c" },
  362. { /* sentinel */ }
  363. };
  364. MODULE_DEVICE_TABLE(of, uniphier_i2c_match);
  365. static struct platform_driver uniphier_i2c_drv = {
  366. .probe = uniphier_i2c_probe,
  367. .remove = uniphier_i2c_remove,
  368. .driver = {
  369. .name = "uniphier-i2c",
  370. .of_match_table = uniphier_i2c_match,
  371. .pm = &uniphier_i2c_pm_ops,
  372. },
  373. };
  374. module_platform_driver(uniphier_i2c_drv);
  375. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  376. MODULE_DESCRIPTION("UniPhier I2C bus driver");
  377. MODULE_LICENSE("GPL");