intel_lspcon.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_DEBUG_KMS("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)
  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, 400);
  69. if (current_mode != mode)
  70. DRM_ERROR("LSPCON mode hasn't settled\n");
  71. out:
  72. DRM_DEBUG_KMS("Current LSPCON mode %s\n",
  73. lspcon_mode_name(current_mode));
  74. return current_mode;
  75. }
  76. static int lspcon_change_mode(struct intel_lspcon *lspcon,
  77. enum drm_lspcon_mode mode)
  78. {
  79. int err;
  80. enum drm_lspcon_mode current_mode;
  81. struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
  82. err = drm_lspcon_get_mode(adapter, &current_mode);
  83. if (err) {
  84. DRM_ERROR("Error reading LSPCON mode\n");
  85. return err;
  86. }
  87. if (current_mode == mode) {
  88. DRM_DEBUG_KMS("Current mode = desired LSPCON mode\n");
  89. return 0;
  90. }
  91. err = drm_lspcon_set_mode(adapter, mode);
  92. if (err < 0) {
  93. DRM_ERROR("LSPCON mode change failed\n");
  94. return err;
  95. }
  96. lspcon->mode = mode;
  97. DRM_DEBUG_KMS("LSPCON mode changed done\n");
  98. return 0;
  99. }
  100. static bool lspcon_wake_native_aux_ch(struct intel_lspcon *lspcon)
  101. {
  102. u8 rev;
  103. if (drm_dp_dpcd_readb(&lspcon_to_intel_dp(lspcon)->aux, DP_DPCD_REV,
  104. &rev) != 1) {
  105. DRM_DEBUG_KMS("Native AUX CH down\n");
  106. return false;
  107. }
  108. DRM_DEBUG_KMS("Native AUX CH up, DPCD version: %d.%d\n",
  109. rev >> 4, rev & 0xf);
  110. return true;
  111. }
  112. static bool lspcon_probe(struct intel_lspcon *lspcon)
  113. {
  114. int retry;
  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. for (retry = 0; retry < 6; retry++) {
  122. if (retry)
  123. usleep_range(500, 1000);
  124. adaptor_type = drm_dp_dual_mode_detect(adapter);
  125. if (adaptor_type == DRM_DP_DUAL_MODE_LSPCON)
  126. break;
  127. }
  128. if (adaptor_type != DRM_DP_DUAL_MODE_LSPCON) {
  129. DRM_DEBUG_KMS("No LSPCON detected, found %s\n",
  130. drm_dp_get_dual_mode_type_name(adaptor_type));
  131. return false;
  132. }
  133. /* Yay ... got a LSPCON device */
  134. DRM_DEBUG_KMS("LSPCON detected\n");
  135. lspcon->mode = lspcon_wait_mode(lspcon, expected_mode);
  136. lspcon->active = true;
  137. return true;
  138. }
  139. static void lspcon_resume_in_pcon_wa(struct intel_lspcon *lspcon)
  140. {
  141. struct intel_dp *intel_dp = lspcon_to_intel_dp(lspcon);
  142. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  143. unsigned long start = jiffies;
  144. while (1) {
  145. if (intel_digital_port_connected(&dig_port->base)) {
  146. DRM_DEBUG_KMS("LSPCON recovering in PCON mode after %u ms\n",
  147. jiffies_to_msecs(jiffies - start));
  148. return;
  149. }
  150. if (time_after(jiffies, start + msecs_to_jiffies(1000)))
  151. break;
  152. usleep_range(10000, 15000);
  153. }
  154. DRM_DEBUG_KMS("LSPCON DP descriptor mismatch after resume\n");
  155. }
  156. void lspcon_resume(struct intel_lspcon *lspcon)
  157. {
  158. enum drm_lspcon_mode expected_mode;
  159. if (lspcon_wake_native_aux_ch(lspcon)) {
  160. expected_mode = DRM_LSPCON_MODE_PCON;
  161. lspcon_resume_in_pcon_wa(lspcon);
  162. } else {
  163. expected_mode = DRM_LSPCON_MODE_LS;
  164. }
  165. if (lspcon_wait_mode(lspcon, expected_mode) == DRM_LSPCON_MODE_PCON)
  166. return;
  167. if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON))
  168. DRM_ERROR("LSPCON resume failed\n");
  169. else
  170. DRM_DEBUG_KMS("LSPCON resume success\n");
  171. }
  172. void lspcon_wait_pcon_mode(struct intel_lspcon *lspcon)
  173. {
  174. lspcon_wait_mode(lspcon, DRM_LSPCON_MODE_PCON);
  175. }
  176. bool lspcon_init(struct intel_digital_port *intel_dig_port)
  177. {
  178. struct intel_dp *dp = &intel_dig_port->dp;
  179. struct intel_lspcon *lspcon = &intel_dig_port->lspcon;
  180. struct drm_device *dev = intel_dig_port->base.base.dev;
  181. struct drm_i915_private *dev_priv = to_i915(dev);
  182. if (!HAS_LSPCON(dev_priv)) {
  183. DRM_ERROR("LSPCON is not supported on this platform\n");
  184. return false;
  185. }
  186. lspcon->active = false;
  187. lspcon->mode = DRM_LSPCON_MODE_INVALID;
  188. if (!lspcon_probe(lspcon)) {
  189. DRM_ERROR("Failed to probe lspcon\n");
  190. return false;
  191. }
  192. /*
  193. * In the SW state machine, lets Put LSPCON in PCON mode only.
  194. * In this way, it will work with both HDMI 1.4 sinks as well as HDMI
  195. * 2.0 sinks.
  196. */
  197. if (lspcon->active && lspcon->mode != DRM_LSPCON_MODE_PCON) {
  198. if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON) < 0) {
  199. DRM_ERROR("LSPCON mode change to PCON failed\n");
  200. return false;
  201. }
  202. }
  203. if (!intel_dp_read_dpcd(dp)) {
  204. DRM_ERROR("LSPCON DPCD read failed\n");
  205. return false;
  206. }
  207. drm_dp_read_desc(&dp->aux, &dp->desc, drm_dp_is_branch(dp->dpcd));
  208. DRM_DEBUG_KMS("Success: LSPCON init\n");
  209. return true;
  210. }