edp_aux.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
  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 version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include "edp.h"
  14. #include "edp.xml.h"
  15. #define AUX_CMD_FIFO_LEN 144
  16. #define AUX_CMD_NATIVE_MAX 16
  17. #define AUX_CMD_I2C_MAX 128
  18. #define EDP_INTR_AUX_I2C_ERR \
  19. (EDP_INTERRUPT_REG_1_WRONG_ADDR | EDP_INTERRUPT_REG_1_TIMEOUT | \
  20. EDP_INTERRUPT_REG_1_NACK_DEFER | EDP_INTERRUPT_REG_1_WRONG_DATA_CNT | \
  21. EDP_INTERRUPT_REG_1_I2C_NACK | EDP_INTERRUPT_REG_1_I2C_DEFER)
  22. #define EDP_INTR_TRANS_STATUS \
  23. (EDP_INTERRUPT_REG_1_AUX_I2C_DONE | EDP_INTR_AUX_I2C_ERR)
  24. struct edp_aux {
  25. void __iomem *base;
  26. bool msg_err;
  27. struct completion msg_comp;
  28. /* To prevent the message transaction routine from reentry. */
  29. struct mutex msg_mutex;
  30. struct drm_dp_aux drm_aux;
  31. };
  32. #define to_edp_aux(x) container_of(x, struct edp_aux, drm_aux)
  33. static int edp_msg_fifo_tx(struct edp_aux *aux, struct drm_dp_aux_msg *msg)
  34. {
  35. u32 data[4];
  36. u32 reg, len;
  37. bool native = msg->request & (DP_AUX_NATIVE_WRITE & DP_AUX_NATIVE_READ);
  38. bool read = msg->request & (DP_AUX_I2C_READ & DP_AUX_NATIVE_READ);
  39. u8 *msgdata = msg->buffer;
  40. int i;
  41. if (read)
  42. len = 4;
  43. else
  44. len = msg->size + 4;
  45. /*
  46. * cmd fifo only has depth of 144 bytes
  47. */
  48. if (len > AUX_CMD_FIFO_LEN)
  49. return -EINVAL;
  50. /* Pack cmd and write to HW */
  51. data[0] = (msg->address >> 16) & 0xf; /* addr[19:16] */
  52. if (read)
  53. data[0] |= BIT(4); /* R/W */
  54. data[1] = (msg->address >> 8) & 0xff; /* addr[15:8] */
  55. data[2] = msg->address & 0xff; /* addr[7:0] */
  56. data[3] = (msg->size - 1) & 0xff; /* len[7:0] */
  57. for (i = 0; i < len; i++) {
  58. reg = (i < 4) ? data[i] : msgdata[i - 4];
  59. reg = EDP_AUX_DATA_DATA(reg); /* index = 0, write */
  60. if (i == 0)
  61. reg |= EDP_AUX_DATA_INDEX_WRITE;
  62. edp_write(aux->base + REG_EDP_AUX_DATA, reg);
  63. }
  64. reg = 0; /* Transaction number is always 1 */
  65. if (!native) /* i2c */
  66. reg |= EDP_AUX_TRANS_CTRL_I2C;
  67. reg |= EDP_AUX_TRANS_CTRL_GO;
  68. edp_write(aux->base + REG_EDP_AUX_TRANS_CTRL, reg);
  69. return 0;
  70. }
  71. static int edp_msg_fifo_rx(struct edp_aux *aux, struct drm_dp_aux_msg *msg)
  72. {
  73. u32 data;
  74. u8 *dp;
  75. int i;
  76. u32 len = msg->size;
  77. edp_write(aux->base + REG_EDP_AUX_DATA,
  78. EDP_AUX_DATA_INDEX_WRITE | EDP_AUX_DATA_READ); /* index = 0 */
  79. dp = msg->buffer;
  80. /* discard first byte */
  81. data = edp_read(aux->base + REG_EDP_AUX_DATA);
  82. for (i = 0; i < len; i++) {
  83. data = edp_read(aux->base + REG_EDP_AUX_DATA);
  84. dp[i] = (u8)((data >> 8) & 0xff);
  85. }
  86. return 0;
  87. }
  88. /*
  89. * This function does the real job to process an AUX transaction.
  90. * It will call msm_edp_aux_ctrl() function to reset the AUX channel,
  91. * if the waiting is timeout.
  92. * The caller who triggers the transaction should avoid the
  93. * msm_edp_aux_ctrl() running concurrently in other threads, i.e.
  94. * start transaction only when AUX channel is fully enabled.
  95. */
  96. static ssize_t edp_aux_transfer(struct drm_dp_aux *drm_aux,
  97. struct drm_dp_aux_msg *msg)
  98. {
  99. struct edp_aux *aux = to_edp_aux(drm_aux);
  100. ssize_t ret;
  101. unsigned long time_left;
  102. bool native = msg->request & (DP_AUX_NATIVE_WRITE & DP_AUX_NATIVE_READ);
  103. bool read = msg->request & (DP_AUX_I2C_READ & DP_AUX_NATIVE_READ);
  104. /* Ignore address only message */
  105. if ((msg->size == 0) || (msg->buffer == NULL)) {
  106. msg->reply = native ?
  107. DP_AUX_NATIVE_REPLY_ACK : DP_AUX_I2C_REPLY_ACK;
  108. return msg->size;
  109. }
  110. /* msg sanity check */
  111. if ((native && (msg->size > AUX_CMD_NATIVE_MAX)) ||
  112. (msg->size > AUX_CMD_I2C_MAX)) {
  113. pr_err("%s: invalid msg: size(%zu), request(%x)\n",
  114. __func__, msg->size, msg->request);
  115. return -EINVAL;
  116. }
  117. mutex_lock(&aux->msg_mutex);
  118. aux->msg_err = false;
  119. reinit_completion(&aux->msg_comp);
  120. ret = edp_msg_fifo_tx(aux, msg);
  121. if (ret < 0)
  122. goto unlock_exit;
  123. DBG("wait_for_completion");
  124. time_left = wait_for_completion_timeout(&aux->msg_comp,
  125. msecs_to_jiffies(300));
  126. if (!time_left) {
  127. /*
  128. * Clear GO and reset AUX channel
  129. * to cancel the current transaction.
  130. */
  131. edp_write(aux->base + REG_EDP_AUX_TRANS_CTRL, 0);
  132. msm_edp_aux_ctrl(aux, 1);
  133. pr_err("%s: aux timeout,\n", __func__);
  134. ret = -ETIMEDOUT;
  135. goto unlock_exit;
  136. }
  137. DBG("completion");
  138. if (!aux->msg_err) {
  139. if (read) {
  140. ret = edp_msg_fifo_rx(aux, msg);
  141. if (ret < 0)
  142. goto unlock_exit;
  143. }
  144. msg->reply = native ?
  145. DP_AUX_NATIVE_REPLY_ACK : DP_AUX_I2C_REPLY_ACK;
  146. } else {
  147. /* Reply defer to retry */
  148. msg->reply = native ?
  149. DP_AUX_NATIVE_REPLY_DEFER : DP_AUX_I2C_REPLY_DEFER;
  150. /*
  151. * The sleep time in caller is not long enough to make sure
  152. * our H/W completes transactions. Add more defer time here.
  153. */
  154. msleep(100);
  155. }
  156. /* Return requested size for success or retry */
  157. ret = msg->size;
  158. unlock_exit:
  159. mutex_unlock(&aux->msg_mutex);
  160. return ret;
  161. }
  162. void *msm_edp_aux_init(struct device *dev, void __iomem *regbase,
  163. struct drm_dp_aux **drm_aux)
  164. {
  165. struct edp_aux *aux = NULL;
  166. int ret;
  167. DBG("");
  168. aux = devm_kzalloc(dev, sizeof(*aux), GFP_KERNEL);
  169. if (!aux)
  170. return NULL;
  171. aux->base = regbase;
  172. mutex_init(&aux->msg_mutex);
  173. init_completion(&aux->msg_comp);
  174. aux->drm_aux.name = "msm_edp_aux";
  175. aux->drm_aux.dev = dev;
  176. aux->drm_aux.transfer = edp_aux_transfer;
  177. ret = drm_dp_aux_register(&aux->drm_aux);
  178. if (ret) {
  179. pr_err("%s: failed to register drm aux: %d\n", __func__, ret);
  180. mutex_destroy(&aux->msg_mutex);
  181. }
  182. if (drm_aux && aux)
  183. *drm_aux = &aux->drm_aux;
  184. return aux;
  185. }
  186. void msm_edp_aux_destroy(struct device *dev, struct edp_aux *aux)
  187. {
  188. if (aux) {
  189. drm_dp_aux_unregister(&aux->drm_aux);
  190. mutex_destroy(&aux->msg_mutex);
  191. }
  192. }
  193. irqreturn_t msm_edp_aux_irq(struct edp_aux *aux, u32 isr)
  194. {
  195. if (isr & EDP_INTR_TRANS_STATUS) {
  196. DBG("isr=%x", isr);
  197. edp_write(aux->base + REG_EDP_AUX_TRANS_CTRL, 0);
  198. if (isr & EDP_INTR_AUX_I2C_ERR)
  199. aux->msg_err = true;
  200. else
  201. aux->msg_err = false;
  202. complete(&aux->msg_comp);
  203. }
  204. return IRQ_HANDLED;
  205. }
  206. void msm_edp_aux_ctrl(struct edp_aux *aux, int enable)
  207. {
  208. u32 data;
  209. DBG("enable=%d", enable);
  210. data = edp_read(aux->base + REG_EDP_AUX_CTRL);
  211. if (enable) {
  212. data |= EDP_AUX_CTRL_RESET;
  213. edp_write(aux->base + REG_EDP_AUX_CTRL, data);
  214. /* Make sure full reset */
  215. wmb();
  216. usleep_range(500, 1000);
  217. data &= ~EDP_AUX_CTRL_RESET;
  218. data |= EDP_AUX_CTRL_ENABLE;
  219. edp_write(aux->base + REG_EDP_AUX_CTRL, data);
  220. } else {
  221. data &= ~EDP_AUX_CTRL_ENABLE;
  222. edp_write(aux->base + REG_EDP_AUX_CTRL, data);
  223. }
  224. }