2 * Copyright © 2016 Intel Corporation
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:
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
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.
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"
30 static struct intel_dp *lspcon_to_intel_dp(struct intel_lspcon *lspcon)
32 struct intel_digital_port *dig_port =
33 container_of(lspcon, struct intel_digital_port, lspcon);
38 static const char *lspcon_mode_name(enum drm_lspcon_mode mode)
41 case DRM_LSPCON_MODE_PCON:
43 case DRM_LSPCON_MODE_LS:
45 case DRM_LSPCON_MODE_INVALID:
53 static enum drm_lspcon_mode lspcon_get_current_mode(struct intel_lspcon *lspcon)
55 enum drm_lspcon_mode current_mode;
56 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
58 if (drm_lspcon_get_mode(adapter, ¤t_mode)) {
59 DRM_ERROR("Error reading LSPCON mode\n");
60 return DRM_LSPCON_MODE_INVALID;
65 static enum drm_lspcon_mode lspcon_wait_mode(struct intel_lspcon *lspcon,
66 enum drm_lspcon_mode mode)
68 enum drm_lspcon_mode current_mode;
70 current_mode = lspcon_get_current_mode(lspcon);
71 if (current_mode == mode || current_mode == DRM_LSPCON_MODE_INVALID)
74 DRM_DEBUG_KMS("Waiting for LSPCON mode %s to settle\n",
75 lspcon_mode_name(mode));
77 wait_for((current_mode = lspcon_get_current_mode(lspcon)) == mode ||
78 current_mode == DRM_LSPCON_MODE_INVALID, 100);
79 if (current_mode != mode)
80 DRM_DEBUG_KMS("LSPCON mode hasn't settled\n");
83 DRM_DEBUG_KMS("Current LSPCON mode %s\n",
84 lspcon_mode_name(current_mode));
89 static int lspcon_change_mode(struct intel_lspcon *lspcon,
90 enum drm_lspcon_mode mode)
93 enum drm_lspcon_mode current_mode;
94 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
96 err = drm_lspcon_get_mode(adapter, ¤t_mode);
98 DRM_ERROR("Error reading LSPCON mode\n");
102 if (current_mode == mode) {
103 DRM_DEBUG_KMS("Current mode = desired LSPCON mode\n");
107 err = drm_lspcon_set_mode(adapter, mode);
109 DRM_ERROR("LSPCON mode change failed\n");
114 DRM_DEBUG_KMS("LSPCON mode changed done\n");
118 static bool lspcon_wake_native_aux_ch(struct intel_lspcon *lspcon)
122 if (drm_dp_dpcd_readb(&lspcon_to_intel_dp(lspcon)->aux, DP_DPCD_REV,
124 DRM_DEBUG_KMS("Native AUX CH down\n");
128 DRM_DEBUG_KMS("Native AUX CH up, DPCD version: %d.%d\n",
129 rev >> 4, rev & 0xf);
134 static bool lspcon_probe(struct intel_lspcon *lspcon)
136 enum drm_dp_dual_mode_type adaptor_type;
137 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
138 enum drm_lspcon_mode expected_mode;
140 expected_mode = lspcon_wake_native_aux_ch(lspcon) ?
141 DRM_LSPCON_MODE_PCON : DRM_LSPCON_MODE_LS;
143 /* Lets probe the adaptor and check its type */
144 adaptor_type = drm_dp_dual_mode_detect(adapter);
145 if (adaptor_type != DRM_DP_DUAL_MODE_LSPCON) {
146 DRM_DEBUG_KMS("No LSPCON detected, found %s\n",
147 drm_dp_get_dual_mode_type_name(adaptor_type));
151 /* Yay ... got a LSPCON device */
152 DRM_DEBUG_KMS("LSPCON detected\n");
153 lspcon->mode = lspcon_wait_mode(lspcon, expected_mode);
154 lspcon->active = true;
158 static void lspcon_resume_in_pcon_wa(struct intel_lspcon *lspcon)
160 struct intel_dp *intel_dp = lspcon_to_intel_dp(lspcon);
161 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
162 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
163 unsigned long start = jiffies;
165 if (!lspcon->desc_valid)
169 struct intel_dp_desc desc;
172 * The w/a only applies in PCON mode and we don't expect any
175 if (!__intel_dp_read_desc(intel_dp, &desc))
178 if (intel_digital_port_connected(dev_priv, dig_port) &&
179 !memcmp(&intel_dp->desc, &desc, sizeof(desc))) {
180 DRM_DEBUG_KMS("LSPCON recovering in PCON mode after %u ms\n",
181 jiffies_to_msecs(jiffies - start));
185 if (time_after(jiffies, start + msecs_to_jiffies(1000)))
188 usleep_range(10000, 15000);
191 DRM_DEBUG_KMS("LSPCON DP descriptor mismatch after resume\n");
194 void lspcon_resume(struct intel_lspcon *lspcon)
196 enum drm_lspcon_mode expected_mode;
198 if (lspcon_wake_native_aux_ch(lspcon)) {
199 expected_mode = DRM_LSPCON_MODE_PCON;
200 lspcon_resume_in_pcon_wa(lspcon);
202 expected_mode = DRM_LSPCON_MODE_LS;
205 if (lspcon_wait_mode(lspcon, expected_mode) == DRM_LSPCON_MODE_PCON)
208 if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON))
209 DRM_ERROR("LSPCON resume failed\n");
211 DRM_DEBUG_KMS("LSPCON resume success\n");
214 void lspcon_wait_pcon_mode(struct intel_lspcon *lspcon)
216 lspcon_wait_mode(lspcon, DRM_LSPCON_MODE_PCON);
219 bool lspcon_init(struct intel_digital_port *intel_dig_port)
221 struct intel_dp *dp = &intel_dig_port->dp;
222 struct intel_lspcon *lspcon = &intel_dig_port->lspcon;
223 struct drm_device *dev = intel_dig_port->base.base.dev;
224 struct drm_i915_private *dev_priv = to_i915(dev);
226 if (!IS_GEN9(dev_priv)) {
227 DRM_ERROR("LSPCON is supported on GEN9 only\n");
231 lspcon->active = false;
232 lspcon->mode = DRM_LSPCON_MODE_INVALID;
234 if (!lspcon_probe(lspcon)) {
235 DRM_ERROR("Failed to probe lspcon\n");
240 * In the SW state machine, lets Put LSPCON in PCON mode only.
241 * In this way, it will work with both HDMI 1.4 sinks as well as HDMI
244 if (lspcon->active && lspcon->mode != DRM_LSPCON_MODE_PCON) {
245 if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON) < 0) {
246 DRM_ERROR("LSPCON mode change to PCON failed\n");
251 if (!intel_dp_read_dpcd(dp)) {
252 DRM_ERROR("LSPCON DPCD read failed\n");
256 lspcon->desc_valid = intel_dp_read_desc(dp);
258 DRM_DEBUG_KMS("Success: LSPCON init\n");