intel_lspcon.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright © 2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. *
  24. */
  25. #include <drm/drm_edid.h>
  26. #include <drm/drm_atomic_helper.h>
  27. #include <drm/drm_dp_dual_mode_helper.h>
  28. #include "intel_drv.h"
  29. static struct intel_dp *lspcon_to_intel_dp(struct intel_lspcon *lspcon)
  30. {
  31. struct intel_digital_port *dig_port =
  32. container_of(lspcon, struct intel_digital_port, lspcon);
  33. return &dig_port->dp;
  34. }
  35. static const char *lspcon_mode_name(enum drm_lspcon_mode mode)
  36. {
  37. switch (mode) {
  38. case DRM_LSPCON_MODE_PCON:
  39. return "PCON";
  40. case DRM_LSPCON_MODE_LS:
  41. return "LS";
  42. case DRM_LSPCON_MODE_INVALID:
  43. return "INVALID";
  44. default:
  45. MISSING_CASE(mode);
  46. return "INVALID";
  47. }
  48. }
  49. static enum drm_lspcon_mode lspcon_get_current_mode(struct intel_lspcon *lspcon)
  50. {
  51. enum drm_lspcon_mode current_mode;
  52. struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
  53. if (drm_lspcon_get_mode(adapter, &current_mode)) {
  54. DRM_ERROR("Error reading LSPCON mode\n");
  55. return DRM_LSPCON_MODE_INVALID;
  56. }
  57. return current_mode;
  58. }
  59. static enum drm_lspcon_mode lspcon_wait_mode(struct intel_lspcon *lspcon,
  60. enum drm_lspcon_mode mode)
  61. {
  62. enum drm_lspcon_mode current_mode;
  63. current_mode = lspcon_get_current_mode(lspcon);
  64. if (current_mode == mode || current_mode == DRM_LSPCON_MODE_INVALID)
  65. goto out;
  66. DRM_DEBUG_KMS("Waiting for LSPCON mode %s to settle\n",
  67. lspcon_mode_name(mode));
  68. wait_for((current_mode = lspcon_get_current_mode(lspcon)) == mode ||
  69. current_mode == DRM_LSPCON_MODE_INVALID, 100);
  70. if (current_mode != mode)
  71. DRM_DEBUG_KMS("LSPCON mode hasn't settled\n");
  72. out:
  73. DRM_DEBUG_KMS("Current LSPCON mode %s\n",
  74. lspcon_mode_name(current_mode));
  75. return current_mode;
  76. }
  77. static int lspcon_change_mode(struct intel_lspcon *lspcon,
  78. enum drm_lspcon_mode mode)
  79. {
  80. int err;
  81. enum drm_lspcon_mode current_mode;
  82. struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
  83. err = drm_lspcon_get_mode(adapter, &current_mode);
  84. if (err) {
  85. DRM_ERROR("Error reading LSPCON mode\n");
  86. return err;
  87. }
  88. if (current_mode == mode) {
  89. DRM_DEBUG_KMS("Current mode = desired LSPCON mode\n");
  90. return 0;
  91. }
  92. err = drm_lspcon_set_mode(adapter, mode);
  93. if (err < 0) {
  94. DRM_ERROR("LSPCON mode change failed\n");
  95. return err;
  96. }
  97. lspcon->mode = mode;
  98. DRM_DEBUG_KMS("LSPCON mode changed done\n");
  99. return 0;
  100. }
  101. static bool lspcon_wake_native_aux_ch(struct intel_lspcon *lspcon)
  102. {
  103. uint8_t rev;
  104. if (drm_dp_dpcd_readb(&lspcon_to_intel_dp(lspcon)->aux, DP_DPCD_REV,
  105. &rev) != 1) {
  106. DRM_DEBUG_KMS("Native AUX CH down\n");
  107. return false;
  108. }
  109. DRM_DEBUG_KMS("Native AUX CH up, DPCD version: %d.%d\n",
  110. rev >> 4, rev & 0xf);
  111. return true;
  112. }
  113. static bool lspcon_probe(struct intel_lspcon *lspcon)
  114. {
  115. enum drm_dp_dual_mode_type adaptor_type;
  116. struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
  117. enum drm_lspcon_mode expected_mode;
  118. expected_mode = lspcon_wake_native_aux_ch(lspcon) ?
  119. DRM_LSPCON_MODE_PCON : DRM_LSPCON_MODE_LS;
  120. /* Lets probe the adaptor and check its type */
  121. adaptor_type = drm_dp_dual_mode_detect(adapter);
  122. if (adaptor_type != DRM_DP_DUAL_MODE_LSPCON) {
  123. DRM_DEBUG_KMS("No LSPCON detected, found %s\n",
  124. drm_dp_get_dual_mode_type_name(adaptor_type));
  125. return false;
  126. }
  127. /* Yay ... got a LSPCON device */
  128. DRM_DEBUG_KMS("LSPCON detected\n");
  129. lspcon->mode = lspcon_wait_mode(lspcon, expected_mode);
  130. lspcon->active = true;
  131. return true;
  132. }
  133. static void lspcon_resume_in_pcon_wa(struct intel_lspcon *lspcon)
  134. {
  135. struct intel_dp *intel_dp = lspcon_to_intel_dp(lspcon);
  136. unsigned long start = jiffies;
  137. if (!lspcon->desc_valid)
  138. return;
  139. while (1) {
  140. struct intel_dp_desc desc;
  141. /*
  142. * The w/a only applies in PCON mode and we don't expect any
  143. * AUX errors.
  144. */
  145. if (!__intel_dp_read_desc(intel_dp, &desc))
  146. return;
  147. if (!memcmp(&intel_dp->desc, &desc, sizeof(desc))) {
  148. DRM_DEBUG_KMS("LSPCON recovering in PCON mode after %u ms\n",
  149. jiffies_to_msecs(jiffies - start));
  150. return;
  151. }
  152. if (time_after(jiffies, start + msecs_to_jiffies(1000)))
  153. break;
  154. usleep_range(10000, 15000);
  155. }
  156. DRM_DEBUG_KMS("LSPCON DP descriptor mismatch after resume\n");
  157. }
  158. void lspcon_resume(struct intel_lspcon *lspcon)
  159. {
  160. enum drm_lspcon_mode expected_mode;
  161. if (lspcon_wake_native_aux_ch(lspcon)) {
  162. expected_mode = DRM_LSPCON_MODE_PCON;
  163. lspcon_resume_in_pcon_wa(lspcon);
  164. } else {
  165. expected_mode = DRM_LSPCON_MODE_LS;
  166. }
  167. if (lspcon_wait_mode(lspcon, expected_mode) == DRM_LSPCON_MODE_PCON)
  168. return;
  169. if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON))
  170. DRM_ERROR("LSPCON resume failed\n");
  171. else
  172. DRM_DEBUG_KMS("LSPCON resume success\n");
  173. }
  174. void lspcon_wait_pcon_mode(struct intel_lspcon *lspcon)
  175. {
  176. lspcon_wait_mode(lspcon, DRM_LSPCON_MODE_PCON);
  177. }
  178. bool lspcon_init(struct intel_digital_port *intel_dig_port)
  179. {
  180. struct intel_dp *dp = &intel_dig_port->dp;
  181. struct intel_lspcon *lspcon = &intel_dig_port->lspcon;
  182. struct drm_device *dev = intel_dig_port->base.base.dev;
  183. struct drm_i915_private *dev_priv = to_i915(dev);
  184. if (!IS_GEN9(dev_priv)) {
  185. DRM_ERROR("LSPCON is supported on GEN9 only\n");
  186. return false;
  187. }
  188. lspcon->active = false;
  189. lspcon->mode = DRM_LSPCON_MODE_INVALID;
  190. if (!lspcon_probe(lspcon)) {
  191. DRM_ERROR("Failed to probe lspcon\n");
  192. return false;
  193. }
  194. /*
  195. * In the SW state machine, lets Put LSPCON in PCON mode only.
  196. * In this way, it will work with both HDMI 1.4 sinks as well as HDMI
  197. * 2.0 sinks.
  198. */
  199. if (lspcon->active && lspcon->mode != DRM_LSPCON_MODE_PCON) {
  200. if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON) < 0) {
  201. DRM_ERROR("LSPCON mode change to PCON failed\n");
  202. return false;
  203. }
  204. }
  205. if (!intel_dp_read_dpcd(dp)) {
  206. DRM_ERROR("LSPCON DPCD read failed\n");
  207. return false;
  208. }
  209. lspcon->desc_valid = intel_dp_read_desc(dp);
  210. DRM_DEBUG_KMS("Success: LSPCON init\n");
  211. return true;
  212. }