sun4i_hdmi_i2c.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2016 Maxime Ripard <maxime.ripard@free-electrons.com>
  3. * Copyright (C) 2017 Jonathan Liu <net147@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/i2c.h>
  12. #include <linux/iopoll.h>
  13. #include "sun4i_hdmi.h"
  14. #define SUN4I_HDMI_DDC_INT_STATUS_ERROR_MASK ( \
  15. SUN4I_HDMI_DDC_INT_STATUS_ILLEGAL_FIFO_OPERATION | \
  16. SUN4I_HDMI_DDC_INT_STATUS_DDC_RX_FIFO_UNDERFLOW | \
  17. SUN4I_HDMI_DDC_INT_STATUS_DDC_TX_FIFO_OVERFLOW | \
  18. SUN4I_HDMI_DDC_INT_STATUS_ARBITRATION_ERROR | \
  19. SUN4I_HDMI_DDC_INT_STATUS_ACK_ERROR | \
  20. SUN4I_HDMI_DDC_INT_STATUS_BUS_ERROR \
  21. )
  22. /* FIFO request bit is set when FIFO level is above RX_THRESHOLD during read */
  23. #define RX_THRESHOLD SUN4I_HDMI_DDC_FIFO_CTRL_RX_THRES_MAX
  24. /* FIFO request bit is set when FIFO level is below TX_THRESHOLD during write */
  25. #define TX_THRESHOLD 1
  26. static int fifo_transfer(struct sun4i_hdmi *hdmi, u8 *buf, int len, bool read)
  27. {
  28. /*
  29. * 1 byte takes 9 clock cycles (8 bits + 1 ACK) = 90 us for 100 kHz
  30. * clock. As clock rate is fixed, just round it up to 100 us.
  31. */
  32. const unsigned long byte_time_ns = 100;
  33. const u32 mask = SUN4I_HDMI_DDC_INT_STATUS_ERROR_MASK |
  34. SUN4I_HDMI_DDC_INT_STATUS_FIFO_REQUEST |
  35. SUN4I_HDMI_DDC_INT_STATUS_TRANSFER_COMPLETE;
  36. u32 reg;
  37. /* Limit transfer length by FIFO threshold */
  38. len = min_t(int, len, read ? (RX_THRESHOLD + 1) :
  39. (SUN4I_HDMI_DDC_FIFO_SIZE - TX_THRESHOLD + 1));
  40. /* Wait until error, FIFO request bit set or transfer complete */
  41. if (readl_poll_timeout(hdmi->base + SUN4I_HDMI_DDC_INT_STATUS_REG, reg,
  42. reg & mask, len * byte_time_ns, 100000))
  43. return -ETIMEDOUT;
  44. if (reg & SUN4I_HDMI_DDC_INT_STATUS_ERROR_MASK)
  45. return -EIO;
  46. if (read)
  47. readsb(hdmi->base + SUN4I_HDMI_DDC_FIFO_DATA_REG, buf, len);
  48. else
  49. writesb(hdmi->base + SUN4I_HDMI_DDC_FIFO_DATA_REG, buf, len);
  50. /* Clear FIFO request bit */
  51. writel(SUN4I_HDMI_DDC_INT_STATUS_FIFO_REQUEST,
  52. hdmi->base + SUN4I_HDMI_DDC_INT_STATUS_REG);
  53. return len;
  54. }
  55. static int xfer_msg(struct sun4i_hdmi *hdmi, struct i2c_msg *msg)
  56. {
  57. int i, len;
  58. u32 reg;
  59. /* Set FIFO direction */
  60. reg = readl(hdmi->base + SUN4I_HDMI_DDC_CTRL_REG);
  61. reg &= ~SUN4I_HDMI_DDC_CTRL_FIFO_DIR_MASK;
  62. reg |= (msg->flags & I2C_M_RD) ?
  63. SUN4I_HDMI_DDC_CTRL_FIFO_DIR_READ :
  64. SUN4I_HDMI_DDC_CTRL_FIFO_DIR_WRITE;
  65. writel(reg, hdmi->base + SUN4I_HDMI_DDC_CTRL_REG);
  66. /* Set I2C address */
  67. writel(SUN4I_HDMI_DDC_ADDR_SLAVE(msg->addr),
  68. hdmi->base + SUN4I_HDMI_DDC_ADDR_REG);
  69. /* Set FIFO RX/TX thresholds and clear FIFO */
  70. reg = readl(hdmi->base + SUN4I_HDMI_DDC_FIFO_CTRL_REG);
  71. reg |= SUN4I_HDMI_DDC_FIFO_CTRL_CLEAR;
  72. reg &= ~SUN4I_HDMI_DDC_FIFO_CTRL_RX_THRES_MASK;
  73. reg |= SUN4I_HDMI_DDC_FIFO_CTRL_RX_THRES(RX_THRESHOLD);
  74. reg &= ~SUN4I_HDMI_DDC_FIFO_CTRL_TX_THRES_MASK;
  75. reg |= SUN4I_HDMI_DDC_FIFO_CTRL_TX_THRES(TX_THRESHOLD);
  76. writel(reg, hdmi->base + SUN4I_HDMI_DDC_FIFO_CTRL_REG);
  77. if (readl_poll_timeout(hdmi->base + SUN4I_HDMI_DDC_FIFO_CTRL_REG,
  78. reg,
  79. !(reg & SUN4I_HDMI_DDC_FIFO_CTRL_CLEAR),
  80. 100, 2000))
  81. return -EIO;
  82. /* Set transfer length */
  83. writel(msg->len, hdmi->base + SUN4I_HDMI_DDC_BYTE_COUNT_REG);
  84. /* Set command */
  85. writel(msg->flags & I2C_M_RD ?
  86. SUN4I_HDMI_DDC_CMD_IMPLICIT_READ :
  87. SUN4I_HDMI_DDC_CMD_IMPLICIT_WRITE,
  88. hdmi->base + SUN4I_HDMI_DDC_CMD_REG);
  89. /* Clear interrupt status bits */
  90. writel(SUN4I_HDMI_DDC_INT_STATUS_ERROR_MASK |
  91. SUN4I_HDMI_DDC_INT_STATUS_FIFO_REQUEST |
  92. SUN4I_HDMI_DDC_INT_STATUS_TRANSFER_COMPLETE,
  93. hdmi->base + SUN4I_HDMI_DDC_INT_STATUS_REG);
  94. /* Start command */
  95. reg = readl(hdmi->base + SUN4I_HDMI_DDC_CTRL_REG);
  96. writel(reg | SUN4I_HDMI_DDC_CTRL_START_CMD,
  97. hdmi->base + SUN4I_HDMI_DDC_CTRL_REG);
  98. /* Transfer bytes */
  99. for (i = 0; i < msg->len; i += len) {
  100. len = fifo_transfer(hdmi, msg->buf + i, msg->len - i,
  101. msg->flags & I2C_M_RD);
  102. if (len <= 0)
  103. return len;
  104. }
  105. /* Wait for command to finish */
  106. if (readl_poll_timeout(hdmi->base + SUN4I_HDMI_DDC_CTRL_REG,
  107. reg,
  108. !(reg & SUN4I_HDMI_DDC_CTRL_START_CMD),
  109. 100, 100000))
  110. return -EIO;
  111. /* Check for errors */
  112. reg = readl(hdmi->base + SUN4I_HDMI_DDC_INT_STATUS_REG);
  113. if ((reg & SUN4I_HDMI_DDC_INT_STATUS_ERROR_MASK) ||
  114. !(reg & SUN4I_HDMI_DDC_INT_STATUS_TRANSFER_COMPLETE)) {
  115. return -EIO;
  116. }
  117. return 0;
  118. }
  119. static int sun4i_hdmi_i2c_xfer(struct i2c_adapter *adap,
  120. struct i2c_msg *msgs, int num)
  121. {
  122. struct sun4i_hdmi *hdmi = i2c_get_adapdata(adap);
  123. u32 reg;
  124. int err, i, ret = num;
  125. for (i = 0; i < num; i++) {
  126. if (!msgs[i].len)
  127. return -EINVAL;
  128. if (msgs[i].len > SUN4I_HDMI_DDC_BYTE_COUNT_MAX)
  129. return -EINVAL;
  130. }
  131. /* Reset I2C controller */
  132. writel(SUN4I_HDMI_DDC_CTRL_ENABLE | SUN4I_HDMI_DDC_CTRL_RESET,
  133. hdmi->base + SUN4I_HDMI_DDC_CTRL_REG);
  134. if (readl_poll_timeout(hdmi->base + SUN4I_HDMI_DDC_CTRL_REG, reg,
  135. !(reg & SUN4I_HDMI_DDC_CTRL_RESET),
  136. 100, 2000))
  137. return -EIO;
  138. writel(SUN4I_HDMI_DDC_LINE_CTRL_SDA_ENABLE |
  139. SUN4I_HDMI_DDC_LINE_CTRL_SCL_ENABLE,
  140. hdmi->base + SUN4I_HDMI_DDC_LINE_CTRL_REG);
  141. clk_prepare_enable(hdmi->ddc_clk);
  142. clk_set_rate(hdmi->ddc_clk, 100000);
  143. for (i = 0; i < num; i++) {
  144. err = xfer_msg(hdmi, &msgs[i]);
  145. if (err) {
  146. ret = err;
  147. break;
  148. }
  149. }
  150. clk_disable_unprepare(hdmi->ddc_clk);
  151. return ret;
  152. }
  153. static u32 sun4i_hdmi_i2c_func(struct i2c_adapter *adap)
  154. {
  155. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  156. }
  157. static const struct i2c_algorithm sun4i_hdmi_i2c_algorithm = {
  158. .master_xfer = sun4i_hdmi_i2c_xfer,
  159. .functionality = sun4i_hdmi_i2c_func,
  160. };
  161. int sun4i_hdmi_i2c_create(struct device *dev, struct sun4i_hdmi *hdmi)
  162. {
  163. struct i2c_adapter *adap;
  164. int ret = 0;
  165. ret = sun4i_ddc_create(hdmi, hdmi->tmds_clk);
  166. if (ret)
  167. return ret;
  168. adap = devm_kzalloc(dev, sizeof(*adap), GFP_KERNEL);
  169. if (!adap)
  170. return -ENOMEM;
  171. adap->owner = THIS_MODULE;
  172. adap->class = I2C_CLASS_DDC;
  173. adap->algo = &sun4i_hdmi_i2c_algorithm;
  174. strlcpy(adap->name, "sun4i_hdmi_i2c adapter", sizeof(adap->name));
  175. i2c_set_adapdata(adap, hdmi);
  176. ret = i2c_add_adapter(adap);
  177. if (ret)
  178. return ret;
  179. hdmi->i2c = adap;
  180. return ret;
  181. }