i2c-designware-common.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Synopsys DesignWare I2C adapter driver.
  3. *
  4. * Based on the TI DAVINCI I2C adapter driver.
  5. *
  6. * Copyright (C) 2006 Texas Instruments.
  7. * Copyright (C) 2007 MontaVista Software Inc.
  8. * Copyright (C) 2009 Provigent Ltd.
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. * ----------------------------------------------------------------------------
  22. *
  23. */
  24. #include <linux/clk.h>
  25. #include <linux/delay.h>
  26. #include <linux/export.h>
  27. #include <linux/errno.h>
  28. #include <linux/err.h>
  29. #include <linux/i2c.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/io.h>
  32. #include <linux/module.h>
  33. #include <linux/pm_runtime.h>
  34. #include "i2c-designware-core.h"
  35. static char *abort_sources[] = {
  36. [ABRT_7B_ADDR_NOACK] =
  37. "slave address not acknowledged (7bit mode)",
  38. [ABRT_10ADDR1_NOACK] =
  39. "first address byte not acknowledged (10bit mode)",
  40. [ABRT_10ADDR2_NOACK] =
  41. "second address byte not acknowledged (10bit mode)",
  42. [ABRT_TXDATA_NOACK] =
  43. "data not acknowledged",
  44. [ABRT_GCALL_NOACK] =
  45. "no acknowledgement for a general call",
  46. [ABRT_GCALL_READ] =
  47. "read after general call",
  48. [ABRT_SBYTE_ACKDET] =
  49. "start byte acknowledged",
  50. [ABRT_SBYTE_NORSTRT] =
  51. "trying to send start byte when restart is disabled",
  52. [ABRT_10B_RD_NORSTRT] =
  53. "trying to read when restart is disabled (10bit mode)",
  54. [ABRT_MASTER_DIS] =
  55. "trying to use disabled adapter",
  56. [ARB_LOST] =
  57. "lost arbitration",
  58. [ABRT_SLAVE_FLUSH_TXFIFO] =
  59. "read command so flush old data in the TX FIFO",
  60. [ABRT_SLAVE_ARBLOST] =
  61. "slave lost the bus while transmitting data to a remote master",
  62. [ABRT_SLAVE_RD_INTX] =
  63. "incorrect slave-transmitter mode configuration",
  64. };
  65. u32 dw_readl(struct dw_i2c_dev *dev, int offset)
  66. {
  67. u32 value;
  68. if (dev->flags & ACCESS_16BIT)
  69. value = readw_relaxed(dev->base + offset) |
  70. (readw_relaxed(dev->base + offset + 2) << 16);
  71. else
  72. value = readl_relaxed(dev->base + offset);
  73. if (dev->flags & ACCESS_SWAP)
  74. return swab32(value);
  75. else
  76. return value;
  77. }
  78. void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
  79. {
  80. if (dev->flags & ACCESS_SWAP)
  81. b = swab32(b);
  82. if (dev->flags & ACCESS_16BIT) {
  83. writew_relaxed((u16)b, dev->base + offset);
  84. writew_relaxed((u16)(b >> 16), dev->base + offset + 2);
  85. } else {
  86. writel_relaxed(b, dev->base + offset);
  87. }
  88. }
  89. u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
  90. {
  91. /*
  92. * DesignWare I2C core doesn't seem to have solid strategy to meet
  93. * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
  94. * will result in violation of the tHD;STA spec.
  95. */
  96. if (cond)
  97. /*
  98. * Conditional expression:
  99. *
  100. * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
  101. *
  102. * This is based on the DW manuals, and represents an ideal
  103. * configuration. The resulting I2C bus speed will be
  104. * faster than any of the others.
  105. *
  106. * If your hardware is free from tHD;STA issue, try this one.
  107. */
  108. return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset;
  109. else
  110. /*
  111. * Conditional expression:
  112. *
  113. * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
  114. *
  115. * This is just experimental rule; the tHD;STA period turned
  116. * out to be proportinal to (_HCNT + 3). With this setting,
  117. * we could meet both tHIGH and tHD;STA timing specs.
  118. *
  119. * If unsure, you'd better to take this alternative.
  120. *
  121. * The reason why we need to take into account "tf" here,
  122. * is the same as described in i2c_dw_scl_lcnt().
  123. */
  124. return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000
  125. - 3 + offset;
  126. }
  127. u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
  128. {
  129. /*
  130. * Conditional expression:
  131. *
  132. * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
  133. *
  134. * DW I2C core starts counting the SCL CNTs for the LOW period
  135. * of the SCL clock (tLOW) as soon as it pulls the SCL line.
  136. * In order to meet the tLOW timing spec, we need to take into
  137. * account the fall time of SCL signal (tf). Default tf value
  138. * should be 0.3 us, for safety.
  139. */
  140. return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset;
  141. }
  142. void __i2c_dw_enable(struct dw_i2c_dev *dev, bool enable)
  143. {
  144. dw_writel(dev, enable, DW_IC_ENABLE);
  145. }
  146. void __i2c_dw_enable_and_wait(struct dw_i2c_dev *dev, bool enable)
  147. {
  148. int timeout = 100;
  149. do {
  150. __i2c_dw_enable(dev, enable);
  151. if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == enable)
  152. return;
  153. /*
  154. * Wait 10 times the signaling period of the highest I2C
  155. * transfer supported by the driver (for 400KHz this is
  156. * 25us) as described in the DesignWare I2C databook.
  157. */
  158. usleep_range(25, 250);
  159. } while (timeout--);
  160. dev_warn(dev->dev, "timeout in %sabling adapter\n",
  161. enable ? "en" : "dis");
  162. }
  163. unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev)
  164. {
  165. /*
  166. * Clock is not necessary if we got LCNT/HCNT values directly from
  167. * the platform code.
  168. */
  169. if (WARN_ON_ONCE(!dev->get_clk_rate_khz))
  170. return 0;
  171. return dev->get_clk_rate_khz(dev);
  172. }
  173. int i2c_dw_prepare_clk(struct dw_i2c_dev *dev, bool prepare)
  174. {
  175. if (IS_ERR(dev->clk))
  176. return PTR_ERR(dev->clk);
  177. if (prepare)
  178. return clk_prepare_enable(dev->clk);
  179. clk_disable_unprepare(dev->clk);
  180. return 0;
  181. }
  182. EXPORT_SYMBOL_GPL(i2c_dw_prepare_clk);
  183. int i2c_dw_acquire_lock(struct dw_i2c_dev *dev)
  184. {
  185. int ret;
  186. if (!dev->acquire_lock)
  187. return 0;
  188. ret = dev->acquire_lock(dev);
  189. if (!ret)
  190. return 0;
  191. dev_err(dev->dev, "couldn't acquire bus ownership\n");
  192. return ret;
  193. }
  194. void i2c_dw_release_lock(struct dw_i2c_dev *dev)
  195. {
  196. if (dev->release_lock)
  197. dev->release_lock(dev);
  198. }
  199. /*
  200. * Waiting for bus not busy
  201. */
  202. int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  203. {
  204. int timeout = TIMEOUT;
  205. while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  206. if (timeout <= 0) {
  207. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  208. i2c_recover_bus(&dev->adapter);
  209. if (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY)
  210. return -ETIMEDOUT;
  211. return 0;
  212. }
  213. timeout--;
  214. usleep_range(1000, 1100);
  215. }
  216. return 0;
  217. }
  218. int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  219. {
  220. unsigned long abort_source = dev->abort_source;
  221. int i;
  222. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  223. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  224. dev_dbg(dev->dev,
  225. "%s: %s\n", __func__, abort_sources[i]);
  226. return -EREMOTEIO;
  227. }
  228. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  229. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  230. if (abort_source & DW_IC_TX_ARB_LOST)
  231. return -EAGAIN;
  232. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  233. return -EINVAL; /* wrong msgs[] data */
  234. else
  235. return -EIO;
  236. }
  237. u32 i2c_dw_func(struct i2c_adapter *adap)
  238. {
  239. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  240. return dev->functionality;
  241. }
  242. void i2c_dw_disable(struct dw_i2c_dev *dev)
  243. {
  244. /* Disable controller */
  245. __i2c_dw_enable_and_wait(dev, false);
  246. /* Disable all interupts */
  247. dw_writel(dev, 0, DW_IC_INTR_MASK);
  248. dw_readl(dev, DW_IC_CLR_INTR);
  249. }
  250. void i2c_dw_disable_int(struct dw_i2c_dev *dev)
  251. {
  252. dw_writel(dev, 0, DW_IC_INTR_MASK);
  253. }
  254. u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
  255. {
  256. return dw_readl(dev, DW_IC_COMP_PARAM_1);
  257. }
  258. EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param);
  259. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core");
  260. MODULE_LICENSE("GPL");