i2c-uniphier.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. /* If next message is read, skip the stop condition */
  207. bool stop = !(msg + 1 < emsg && msg[1].flags & I2C_M_RD);
  208. /* but, force it if I2C_M_STOP is set */
  209. if (msg->flags & I2C_M_STOP)
  210. stop = true;
  211. ret = uniphier_i2c_master_xfer_one(adap, msg, stop);
  212. if (ret)
  213. return ret;
  214. }
  215. return num;
  216. }
  217. static u32 uniphier_i2c_functionality(struct i2c_adapter *adap)
  218. {
  219. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  220. }
  221. static const struct i2c_algorithm uniphier_i2c_algo = {
  222. .master_xfer = uniphier_i2c_master_xfer,
  223. .functionality = uniphier_i2c_functionality,
  224. };
  225. static void uniphier_i2c_reset(struct uniphier_i2c_priv *priv, bool reset_on)
  226. {
  227. u32 val = UNIPHIER_I2C_BRST_RSCL;
  228. val |= reset_on ? 0 : UNIPHIER_I2C_BRST_FOEN;
  229. writel(val, priv->membase + UNIPHIER_I2C_BRST);
  230. }
  231. static int uniphier_i2c_get_scl(struct i2c_adapter *adap)
  232. {
  233. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  234. return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
  235. UNIPHIER_I2C_BSTS_SCL);
  236. }
  237. static void uniphier_i2c_set_scl(struct i2c_adapter *adap, int val)
  238. {
  239. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  240. writel(val ? UNIPHIER_I2C_BRST_RSCL : 0,
  241. priv->membase + UNIPHIER_I2C_BRST);
  242. }
  243. static int uniphier_i2c_get_sda(struct i2c_adapter *adap)
  244. {
  245. struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
  246. return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
  247. UNIPHIER_I2C_BSTS_SDA);
  248. }
  249. static void uniphier_i2c_unprepare_recovery(struct i2c_adapter *adap)
  250. {
  251. uniphier_i2c_reset(i2c_get_adapdata(adap), false);
  252. }
  253. static struct i2c_bus_recovery_info uniphier_i2c_bus_recovery_info = {
  254. .recover_bus = i2c_generic_scl_recovery,
  255. .get_scl = uniphier_i2c_get_scl,
  256. .set_scl = uniphier_i2c_set_scl,
  257. .get_sda = uniphier_i2c_get_sda,
  258. .unprepare_recovery = uniphier_i2c_unprepare_recovery,
  259. };
  260. static void uniphier_i2c_hw_init(struct uniphier_i2c_priv *priv,
  261. u32 bus_speed, unsigned long clk_rate)
  262. {
  263. uniphier_i2c_reset(priv, true);
  264. writel((clk_rate / bus_speed / 2 << 16) | (clk_rate / bus_speed),
  265. 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 err;
  307. }
  308. init_completion(&priv->comp);
  309. priv->adap.owner = THIS_MODULE;
  310. priv->adap.algo = &uniphier_i2c_algo;
  311. priv->adap.dev.parent = dev;
  312. priv->adap.dev.of_node = dev->of_node;
  313. strlcpy(priv->adap.name, "UniPhier I2C", sizeof(priv->adap.name));
  314. priv->adap.bus_recovery_info = &uniphier_i2c_bus_recovery_info;
  315. i2c_set_adapdata(&priv->adap, priv);
  316. platform_set_drvdata(pdev, priv);
  317. uniphier_i2c_hw_init(priv, bus_speed, clk_rate);
  318. ret = devm_request_irq(dev, irq, uniphier_i2c_interrupt, 0, pdev->name,
  319. priv);
  320. if (ret) {
  321. dev_err(dev, "failed to request irq %d\n", irq);
  322. goto err;
  323. }
  324. ret = i2c_add_adapter(&priv->adap);
  325. err:
  326. if (ret)
  327. clk_disable_unprepare(priv->clk);
  328. return ret;
  329. }
  330. static int uniphier_i2c_remove(struct platform_device *pdev)
  331. {
  332. struct uniphier_i2c_priv *priv = platform_get_drvdata(pdev);
  333. i2c_del_adapter(&priv->adap);
  334. clk_disable_unprepare(priv->clk);
  335. return 0;
  336. }
  337. static const struct of_device_id uniphier_i2c_match[] = {
  338. { .compatible = "socionext,uniphier-i2c" },
  339. { /* sentinel */ }
  340. };
  341. MODULE_DEVICE_TABLE(of, uniphier_i2c_match);
  342. static struct platform_driver uniphier_i2c_drv = {
  343. .probe = uniphier_i2c_probe,
  344. .remove = uniphier_i2c_remove,
  345. .driver = {
  346. .name = "uniphier-i2c",
  347. .of_match_table = uniphier_i2c_match,
  348. },
  349. };
  350. module_platform_driver(uniphier_i2c_drv);
  351. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  352. MODULE_DESCRIPTION("UniPhier I2C bus driver");
  353. MODULE_LICENSE("GPL");