11e12ee3bSFrançois Tigeot /*
21e12ee3bSFrançois Tigeot * Copyright © 2016 Intel Corporation
31e12ee3bSFrançois Tigeot *
41e12ee3bSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a
51e12ee3bSFrançois Tigeot * copy of this software and associated documentation files (the "Software"),
61e12ee3bSFrançois Tigeot * to deal in the Software without restriction, including without limitation
71e12ee3bSFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81e12ee3bSFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the
91e12ee3bSFrançois Tigeot * Software is furnished to do so, subject to the following conditions:
101e12ee3bSFrançois Tigeot *
111e12ee3bSFrançois Tigeot * The above copyright notice and this permission notice (including the next
121e12ee3bSFrançois Tigeot * paragraph) shall be included in all copies or substantial portions of the
131e12ee3bSFrançois Tigeot * Software.
141e12ee3bSFrançois Tigeot *
151e12ee3bSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161e12ee3bSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171e12ee3bSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
181e12ee3bSFrançois Tigeot * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191e12ee3bSFrançois Tigeot * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201e12ee3bSFrançois Tigeot * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
211e12ee3bSFrançois Tigeot * DEALINGS IN THE SOFTWARE.
221e12ee3bSFrançois Tigeot *
231e12ee3bSFrançois Tigeot *
241e12ee3bSFrançois Tigeot */
251e12ee3bSFrançois Tigeot #include <drm/drm_edid.h>
261e12ee3bSFrançois Tigeot #include <drm/drm_atomic_helper.h>
271e12ee3bSFrançois Tigeot #include <drm/drm_dp_dual_mode_helper.h>
281e12ee3bSFrançois Tigeot #include "intel_drv.h"
291e12ee3bSFrançois Tigeot
lspcon_to_intel_dp(struct intel_lspcon * lspcon)304be47400SFrançois Tigeot static struct intel_dp *lspcon_to_intel_dp(struct intel_lspcon *lspcon)
314be47400SFrançois Tigeot {
324be47400SFrançois Tigeot struct intel_digital_port *dig_port =
334be47400SFrançois Tigeot container_of(lspcon, struct intel_digital_port, lspcon);
344be47400SFrançois Tigeot
354be47400SFrançois Tigeot return &dig_port->dp;
364be47400SFrançois Tigeot }
374be47400SFrançois Tigeot
lspcon_mode_name(enum drm_lspcon_mode mode)38a85cb24fSFrançois Tigeot static const char *lspcon_mode_name(enum drm_lspcon_mode mode)
39a85cb24fSFrançois Tigeot {
40a85cb24fSFrançois Tigeot switch (mode) {
41a85cb24fSFrançois Tigeot case DRM_LSPCON_MODE_PCON:
42a85cb24fSFrançois Tigeot return "PCON";
43a85cb24fSFrançois Tigeot case DRM_LSPCON_MODE_LS:
44a85cb24fSFrançois Tigeot return "LS";
45a85cb24fSFrançois Tigeot case DRM_LSPCON_MODE_INVALID:
46a85cb24fSFrançois Tigeot return "INVALID";
47a85cb24fSFrançois Tigeot default:
48a85cb24fSFrançois Tigeot MISSING_CASE(mode);
49a85cb24fSFrançois Tigeot return "INVALID";
50a85cb24fSFrançois Tigeot }
51a85cb24fSFrançois Tigeot }
52a85cb24fSFrançois Tigeot
lspcon_get_current_mode(struct intel_lspcon * lspcon)531e12ee3bSFrançois Tigeot static enum drm_lspcon_mode lspcon_get_current_mode(struct intel_lspcon *lspcon)
541e12ee3bSFrançois Tigeot {
55a85cb24fSFrançois Tigeot enum drm_lspcon_mode current_mode;
564be47400SFrançois Tigeot struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
571e12ee3bSFrançois Tigeot
58a85cb24fSFrançois Tigeot if (drm_lspcon_get_mode(adapter, ¤t_mode)) {
59*3f2dd94aSFrançois Tigeot DRM_DEBUG_KMS("Error reading LSPCON mode\n");
60a85cb24fSFrançois Tigeot return DRM_LSPCON_MODE_INVALID;
61a85cb24fSFrançois Tigeot }
62a85cb24fSFrançois Tigeot return current_mode;
63a85cb24fSFrançois Tigeot }
64a85cb24fSFrançois Tigeot
lspcon_wait_mode(struct intel_lspcon * lspcon,enum drm_lspcon_mode mode)65a85cb24fSFrançois Tigeot static enum drm_lspcon_mode lspcon_wait_mode(struct intel_lspcon *lspcon,
66a85cb24fSFrançois Tigeot enum drm_lspcon_mode mode)
67a85cb24fSFrançois Tigeot {
68a85cb24fSFrançois Tigeot enum drm_lspcon_mode current_mode;
69a85cb24fSFrançois Tigeot
70a85cb24fSFrançois Tigeot current_mode = lspcon_get_current_mode(lspcon);
71*3f2dd94aSFrançois Tigeot if (current_mode == mode)
72a85cb24fSFrançois Tigeot goto out;
73a85cb24fSFrançois Tigeot
74a85cb24fSFrançois Tigeot DRM_DEBUG_KMS("Waiting for LSPCON mode %s to settle\n",
75a85cb24fSFrançois Tigeot lspcon_mode_name(mode));
76a85cb24fSFrançois Tigeot
77*3f2dd94aSFrançois Tigeot wait_for((current_mode = lspcon_get_current_mode(lspcon)) == mode, 100);
78a85cb24fSFrançois Tigeot if (current_mode != mode)
79*3f2dd94aSFrançois Tigeot DRM_ERROR("LSPCON mode hasn't settled\n");
80a85cb24fSFrançois Tigeot
81a85cb24fSFrançois Tigeot out:
821e12ee3bSFrançois Tigeot DRM_DEBUG_KMS("Current LSPCON mode %s\n",
83a85cb24fSFrançois Tigeot lspcon_mode_name(current_mode));
84a85cb24fSFrançois Tigeot
851e12ee3bSFrançois Tigeot return current_mode;
861e12ee3bSFrançois Tigeot }
871e12ee3bSFrançois Tigeot
lspcon_change_mode(struct intel_lspcon * lspcon,enum drm_lspcon_mode mode)881e12ee3bSFrançois Tigeot static int lspcon_change_mode(struct intel_lspcon *lspcon,
89a85cb24fSFrançois Tigeot enum drm_lspcon_mode mode)
901e12ee3bSFrançois Tigeot {
911e12ee3bSFrançois Tigeot int err;
921e12ee3bSFrançois Tigeot enum drm_lspcon_mode current_mode;
934be47400SFrançois Tigeot struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
941e12ee3bSFrançois Tigeot
951e12ee3bSFrançois Tigeot err = drm_lspcon_get_mode(adapter, ¤t_mode);
961e12ee3bSFrançois Tigeot if (err) {
971e12ee3bSFrançois Tigeot DRM_ERROR("Error reading LSPCON mode\n");
981e12ee3bSFrançois Tigeot return err;
991e12ee3bSFrançois Tigeot }
1001e12ee3bSFrançois Tigeot
1011e12ee3bSFrançois Tigeot if (current_mode == mode) {
1021e12ee3bSFrançois Tigeot DRM_DEBUG_KMS("Current mode = desired LSPCON mode\n");
1031e12ee3bSFrançois Tigeot return 0;
1041e12ee3bSFrançois Tigeot }
1051e12ee3bSFrançois Tigeot
1061e12ee3bSFrançois Tigeot err = drm_lspcon_set_mode(adapter, mode);
1071e12ee3bSFrançois Tigeot if (err < 0) {
1081e12ee3bSFrançois Tigeot DRM_ERROR("LSPCON mode change failed\n");
1091e12ee3bSFrançois Tigeot return err;
1101e12ee3bSFrançois Tigeot }
1111e12ee3bSFrançois Tigeot
1121e12ee3bSFrançois Tigeot lspcon->mode = mode;
1131e12ee3bSFrançois Tigeot DRM_DEBUG_KMS("LSPCON mode changed done\n");
1141e12ee3bSFrançois Tigeot return 0;
1151e12ee3bSFrançois Tigeot }
1161e12ee3bSFrançois Tigeot
lspcon_wake_native_aux_ch(struct intel_lspcon * lspcon)117a85cb24fSFrançois Tigeot static bool lspcon_wake_native_aux_ch(struct intel_lspcon *lspcon)
118a85cb24fSFrançois Tigeot {
119a85cb24fSFrançois Tigeot uint8_t rev;
120a85cb24fSFrançois Tigeot
121a85cb24fSFrançois Tigeot if (drm_dp_dpcd_readb(&lspcon_to_intel_dp(lspcon)->aux, DP_DPCD_REV,
122a85cb24fSFrançois Tigeot &rev) != 1) {
123a85cb24fSFrançois Tigeot DRM_DEBUG_KMS("Native AUX CH down\n");
124a85cb24fSFrançois Tigeot return false;
125a85cb24fSFrançois Tigeot }
126a85cb24fSFrançois Tigeot
127a85cb24fSFrançois Tigeot DRM_DEBUG_KMS("Native AUX CH up, DPCD version: %d.%d\n",
128a85cb24fSFrançois Tigeot rev >> 4, rev & 0xf);
129a85cb24fSFrançois Tigeot
130a85cb24fSFrançois Tigeot return true;
131a85cb24fSFrançois Tigeot }
132a85cb24fSFrançois Tigeot
lspcon_probe(struct intel_lspcon * lspcon)1331e12ee3bSFrançois Tigeot static bool lspcon_probe(struct intel_lspcon *lspcon)
1341e12ee3bSFrançois Tigeot {
135*3f2dd94aSFrançois Tigeot int retry;
1361e12ee3bSFrançois Tigeot enum drm_dp_dual_mode_type adaptor_type;
1374be47400SFrançois Tigeot struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
138a85cb24fSFrançois Tigeot enum drm_lspcon_mode expected_mode;
139a85cb24fSFrançois Tigeot
140a85cb24fSFrançois Tigeot expected_mode = lspcon_wake_native_aux_ch(lspcon) ?
141a85cb24fSFrançois Tigeot DRM_LSPCON_MODE_PCON : DRM_LSPCON_MODE_LS;
1421e12ee3bSFrançois Tigeot
1431e12ee3bSFrançois Tigeot /* Lets probe the adaptor and check its type */
144*3f2dd94aSFrançois Tigeot for (retry = 0; retry < 6; retry++) {
145*3f2dd94aSFrançois Tigeot if (retry)
146*3f2dd94aSFrançois Tigeot usleep_range(500, 1000);
147*3f2dd94aSFrançois Tigeot
1481e12ee3bSFrançois Tigeot adaptor_type = drm_dp_dual_mode_detect(adapter);
149*3f2dd94aSFrançois Tigeot if (adaptor_type == DRM_DP_DUAL_MODE_LSPCON)
150*3f2dd94aSFrançois Tigeot break;
151*3f2dd94aSFrançois Tigeot }
152*3f2dd94aSFrançois Tigeot
1531e12ee3bSFrançois Tigeot if (adaptor_type != DRM_DP_DUAL_MODE_LSPCON) {
1541e12ee3bSFrançois Tigeot DRM_DEBUG_KMS("No LSPCON detected, found %s\n",
1551e12ee3bSFrançois Tigeot drm_dp_get_dual_mode_type_name(adaptor_type));
1561e12ee3bSFrançois Tigeot return false;
1571e12ee3bSFrançois Tigeot }
1581e12ee3bSFrançois Tigeot
1591e12ee3bSFrançois Tigeot /* Yay ... got a LSPCON device */
1601e12ee3bSFrançois Tigeot DRM_DEBUG_KMS("LSPCON detected\n");
161a85cb24fSFrançois Tigeot lspcon->mode = lspcon_wait_mode(lspcon, expected_mode);
1621e12ee3bSFrançois Tigeot lspcon->active = true;
1631e12ee3bSFrançois Tigeot return true;
1641e12ee3bSFrançois Tigeot }
1651e12ee3bSFrançois Tigeot
lspcon_resume_in_pcon_wa(struct intel_lspcon * lspcon)1664be47400SFrançois Tigeot static void lspcon_resume_in_pcon_wa(struct intel_lspcon *lspcon)
1674be47400SFrançois Tigeot {
1684be47400SFrançois Tigeot struct intel_dp *intel_dp = lspcon_to_intel_dp(lspcon);
1694be47400SFrançois Tigeot struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1704be47400SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
1714be47400SFrançois Tigeot unsigned long start = jiffies;
1724be47400SFrançois Tigeot
1734be47400SFrançois Tigeot while (1) {
174a85cb24fSFrançois Tigeot if (intel_digital_port_connected(dev_priv, dig_port)) {
1754be47400SFrançois Tigeot DRM_DEBUG_KMS("LSPCON recovering in PCON mode after %lu ms\n",
1764be47400SFrançois Tigeot jiffies_to_msecs(jiffies - start));
1774be47400SFrançois Tigeot return;
1784be47400SFrançois Tigeot }
1794be47400SFrançois Tigeot
1804be47400SFrançois Tigeot if (time_after(jiffies, start + msecs_to_jiffies(1000)))
1814be47400SFrançois Tigeot break;
1824be47400SFrançois Tigeot
1834be47400SFrançois Tigeot usleep_range(10000, 15000);
1844be47400SFrançois Tigeot }
1854be47400SFrançois Tigeot
1864be47400SFrançois Tigeot DRM_DEBUG_KMS("LSPCON DP descriptor mismatch after resume\n");
1874be47400SFrançois Tigeot }
1884be47400SFrançois Tigeot
lspcon_resume(struct intel_lspcon * lspcon)1891e12ee3bSFrançois Tigeot void lspcon_resume(struct intel_lspcon *lspcon)
1901e12ee3bSFrançois Tigeot {
191a85cb24fSFrançois Tigeot enum drm_lspcon_mode expected_mode;
1924be47400SFrançois Tigeot
193a85cb24fSFrançois Tigeot if (lspcon_wake_native_aux_ch(lspcon)) {
194a85cb24fSFrançois Tigeot expected_mode = DRM_LSPCON_MODE_PCON;
195a85cb24fSFrançois Tigeot lspcon_resume_in_pcon_wa(lspcon);
196a85cb24fSFrançois Tigeot } else {
197a85cb24fSFrançois Tigeot expected_mode = DRM_LSPCON_MODE_LS;
198a85cb24fSFrançois Tigeot }
199a85cb24fSFrançois Tigeot
200a85cb24fSFrançois Tigeot if (lspcon_wait_mode(lspcon, expected_mode) == DRM_LSPCON_MODE_PCON)
201a85cb24fSFrançois Tigeot return;
202a85cb24fSFrançois Tigeot
203a85cb24fSFrançois Tigeot if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON))
2041e12ee3bSFrançois Tigeot DRM_ERROR("LSPCON resume failed\n");
2051e12ee3bSFrançois Tigeot else
2061e12ee3bSFrançois Tigeot DRM_DEBUG_KMS("LSPCON resume success\n");
2071e12ee3bSFrançois Tigeot }
2081e12ee3bSFrançois Tigeot
lspcon_wait_pcon_mode(struct intel_lspcon * lspcon)209a85cb24fSFrançois Tigeot void lspcon_wait_pcon_mode(struct intel_lspcon *lspcon)
210a85cb24fSFrançois Tigeot {
211a85cb24fSFrançois Tigeot lspcon_wait_mode(lspcon, DRM_LSPCON_MODE_PCON);
212a85cb24fSFrançois Tigeot }
213a85cb24fSFrançois Tigeot
lspcon_init(struct intel_digital_port * intel_dig_port)2141e12ee3bSFrançois Tigeot bool lspcon_init(struct intel_digital_port *intel_dig_port)
2151e12ee3bSFrançois Tigeot {
2161e12ee3bSFrançois Tigeot struct intel_dp *dp = &intel_dig_port->dp;
2171e12ee3bSFrançois Tigeot struct intel_lspcon *lspcon = &intel_dig_port->lspcon;
2181e12ee3bSFrançois Tigeot struct drm_device *dev = intel_dig_port->base.base.dev;
2191e12ee3bSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dev);
2201e12ee3bSFrançois Tigeot
221*3f2dd94aSFrançois Tigeot if (!HAS_LSPCON(dev_priv)) {
222*3f2dd94aSFrançois Tigeot DRM_ERROR("LSPCON is not supported on this platform\n");
2231e12ee3bSFrançois Tigeot return false;
2241e12ee3bSFrançois Tigeot }
2251e12ee3bSFrançois Tigeot
2261e12ee3bSFrançois Tigeot lspcon->active = false;
2271e12ee3bSFrançois Tigeot lspcon->mode = DRM_LSPCON_MODE_INVALID;
2281e12ee3bSFrançois Tigeot
2291e12ee3bSFrançois Tigeot if (!lspcon_probe(lspcon)) {
2301e12ee3bSFrançois Tigeot DRM_ERROR("Failed to probe lspcon\n");
2311e12ee3bSFrançois Tigeot return false;
2321e12ee3bSFrançois Tigeot }
2331e12ee3bSFrançois Tigeot
2341e12ee3bSFrançois Tigeot /*
2351e12ee3bSFrançois Tigeot * In the SW state machine, lets Put LSPCON in PCON mode only.
2361e12ee3bSFrançois Tigeot * In this way, it will work with both HDMI 1.4 sinks as well as HDMI
2371e12ee3bSFrançois Tigeot * 2.0 sinks.
2381e12ee3bSFrançois Tigeot */
2391e12ee3bSFrançois Tigeot if (lspcon->active && lspcon->mode != DRM_LSPCON_MODE_PCON) {
240a85cb24fSFrançois Tigeot if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON) < 0) {
2411e12ee3bSFrançois Tigeot DRM_ERROR("LSPCON mode change to PCON failed\n");
2421e12ee3bSFrançois Tigeot return false;
2431e12ee3bSFrançois Tigeot }
2441e12ee3bSFrançois Tigeot }
2451e12ee3bSFrançois Tigeot
2464be47400SFrançois Tigeot if (!intel_dp_read_dpcd(dp)) {
2474be47400SFrançois Tigeot DRM_ERROR("LSPCON DPCD read failed\n");
2484be47400SFrançois Tigeot return false;
2494be47400SFrançois Tigeot }
2504be47400SFrançois Tigeot
251a85cb24fSFrançois Tigeot drm_dp_read_desc(&dp->aux, &dp->desc, drm_dp_is_branch(dp->dpcd));
2524be47400SFrançois Tigeot
2531e12ee3bSFrançois Tigeot DRM_DEBUG_KMS("Success: LSPCON init\n");
2541e12ee3bSFrançois Tigeot return true;
2551e12ee3bSFrançois Tigeot }
256