blob: 5dae16ccd9f1015fe80abc2c41d98937f3aaef89 [file] [log] [blame]
Shashank Sharmadbe9e612016-10-14 19:56:49 +05301/*
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
Imre Deaka5d94b832016-10-24 19:33:30 +030030static struct intel_dp *lspcon_to_intel_dp(struct intel_lspcon *lspcon)
31{
32 struct intel_digital_port *dig_port =
33 container_of(lspcon, struct intel_digital_port, lspcon);
34
35 return &dig_port->dp;
36}
37
Imre Deak357c0ae2016-11-21 21:15:06 +020038static const char *lspcon_mode_name(enum drm_lspcon_mode mode)
39{
40 switch (mode) {
41 case DRM_LSPCON_MODE_PCON:
42 return "PCON";
43 case DRM_LSPCON_MODE_LS:
44 return "LS";
45 case DRM_LSPCON_MODE_INVALID:
46 return "INVALID";
47 default:
48 MISSING_CASE(mode);
49 return "INVALID";
50 }
51}
52
Jani Nikula1dc16aa2016-10-18 14:21:51 +030053static enum drm_lspcon_mode lspcon_get_current_mode(struct intel_lspcon *lspcon)
Shashank Sharmadbe9e612016-10-14 19:56:49 +053054{
Imre Deak357c0ae2016-11-21 21:15:06 +020055 enum drm_lspcon_mode current_mode;
Imre Deaka5d94b832016-10-24 19:33:30 +030056 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
Shashank Sharmadbe9e612016-10-14 19:56:49 +053057
Imre Deak357c0ae2016-11-21 21:15:06 +020058 if (drm_lspcon_get_mode(adapter, &current_mode)) {
Shashank Sharmad18aef02017-10-10 15:37:43 +053059 DRM_DEBUG_KMS("Error reading LSPCON mode\n");
Imre Deak357c0ae2016-11-21 21:15:06 +020060 return DRM_LSPCON_MODE_INVALID;
61 }
62 return current_mode;
63}
64
65static enum drm_lspcon_mode lspcon_wait_mode(struct intel_lspcon *lspcon,
66 enum drm_lspcon_mode mode)
67{
68 enum drm_lspcon_mode current_mode;
69
70 current_mode = lspcon_get_current_mode(lspcon);
Shashank Sharmad18aef02017-10-10 15:37:43 +053071 if (current_mode == mode)
Imre Deak357c0ae2016-11-21 21:15:06 +020072 goto out;
73
74 DRM_DEBUG_KMS("Waiting for LSPCON mode %s to settle\n",
75 lspcon_mode_name(mode));
76
Shashank Sharmad18aef02017-10-10 15:37:43 +053077 wait_for((current_mode = lspcon_get_current_mode(lspcon)) == mode, 100);
Imre Deak357c0ae2016-11-21 21:15:06 +020078 if (current_mode != mode)
Shashank Sharmad18aef02017-10-10 15:37:43 +053079 DRM_ERROR("LSPCON mode hasn't settled\n");
Imre Deak357c0ae2016-11-21 21:15:06 +020080
81out:
82 DRM_DEBUG_KMS("Current LSPCON mode %s\n",
83 lspcon_mode_name(current_mode));
84
Shashank Sharmadbe9e612016-10-14 19:56:49 +053085 return current_mode;
86}
87
88static int lspcon_change_mode(struct intel_lspcon *lspcon,
Imre Deak90248802016-11-21 21:15:07 +020089 enum drm_lspcon_mode mode)
Shashank Sharmadbe9e612016-10-14 19:56:49 +053090{
91 int err;
92 enum drm_lspcon_mode current_mode;
Imre Deaka5d94b832016-10-24 19:33:30 +030093 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
Shashank Sharmadbe9e612016-10-14 19:56:49 +053094
95 err = drm_lspcon_get_mode(adapter, &current_mode);
96 if (err) {
97 DRM_ERROR("Error reading LSPCON mode\n");
98 return err;
99 }
100
101 if (current_mode == mode) {
102 DRM_DEBUG_KMS("Current mode = desired LSPCON mode\n");
103 return 0;
104 }
105
106 err = drm_lspcon_set_mode(adapter, mode);
107 if (err < 0) {
108 DRM_ERROR("LSPCON mode change failed\n");
109 return err;
110 }
111
112 lspcon->mode = mode;
113 DRM_DEBUG_KMS("LSPCON mode changed done\n");
114 return 0;
115}
116
Imre Deakf2b667b2016-11-21 21:15:04 +0200117static bool lspcon_wake_native_aux_ch(struct intel_lspcon *lspcon)
118{
Jani Nikula02f361f2018-06-12 12:19:35 +0300119 u8 rev;
Imre Deakf2b667b2016-11-21 21:15:04 +0200120
121 if (drm_dp_dpcd_readb(&lspcon_to_intel_dp(lspcon)->aux, DP_DPCD_REV,
122 &rev) != 1) {
123 DRM_DEBUG_KMS("Native AUX CH down\n");
124 return false;
125 }
126
127 DRM_DEBUG_KMS("Native AUX CH up, DPCD version: %d.%d\n",
128 rev >> 4, rev & 0xf);
129
130 return true;
131}
132
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530133static bool lspcon_probe(struct intel_lspcon *lspcon)
134{
Shashank Sharmaa2fc4bd2017-10-10 15:37:44 +0530135 int retry;
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530136 enum drm_dp_dual_mode_type adaptor_type;
Imre Deaka5d94b832016-10-24 19:33:30 +0300137 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
Imre Deak357c0ae2016-11-21 21:15:06 +0200138 enum drm_lspcon_mode expected_mode;
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530139
Imre Deak357c0ae2016-11-21 21:15:06 +0200140 expected_mode = lspcon_wake_native_aux_ch(lspcon) ?
141 DRM_LSPCON_MODE_PCON : DRM_LSPCON_MODE_LS;
Imre Deakf2b667b2016-11-21 21:15:04 +0200142
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530143 /* Lets probe the adaptor and check its type */
Shashank Sharmaa2fc4bd2017-10-10 15:37:44 +0530144 for (retry = 0; retry < 6; retry++) {
145 if (retry)
146 usleep_range(500, 1000);
147
148 adaptor_type = drm_dp_dual_mode_detect(adapter);
149 if (adaptor_type == DRM_DP_DUAL_MODE_LSPCON)
150 break;
151 }
152
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530153 if (adaptor_type != DRM_DP_DUAL_MODE_LSPCON) {
154 DRM_DEBUG_KMS("No LSPCON detected, found %s\n",
Shashank Sharmaa2fc4bd2017-10-10 15:37:44 +0530155 drm_dp_get_dual_mode_type_name(adaptor_type));
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530156 return false;
157 }
158
159 /* Yay ... got a LSPCON device */
160 DRM_DEBUG_KMS("LSPCON detected\n");
Imre Deak357c0ae2016-11-21 21:15:06 +0200161 lspcon->mode = lspcon_wait_mode(lspcon, expected_mode);
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530162 lspcon->active = true;
163 return true;
164}
165
Imre Deak489375c2016-10-24 19:33:31 +0300166static void lspcon_resume_in_pcon_wa(struct intel_lspcon *lspcon)
167{
168 struct intel_dp *intel_dp = lspcon_to_intel_dp(lspcon);
Imre Deak390b4e02017-01-27 11:39:19 +0200169 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
Imre Deak489375c2016-10-24 19:33:31 +0300170 unsigned long start = jiffies;
171
Imre Deak489375c2016-10-24 19:33:31 +0300172 while (1) {
Rodrigo Vivi7533eb42018-01-29 15:22:20 -0800173 if (intel_digital_port_connected(&dig_port->base)) {
Imre Deak489375c2016-10-24 19:33:31 +0300174 DRM_DEBUG_KMS("LSPCON recovering in PCON mode after %u ms\n",
175 jiffies_to_msecs(jiffies - start));
176 return;
177 }
178
179 if (time_after(jiffies, start + msecs_to_jiffies(1000)))
180 break;
181
182 usleep_range(10000, 15000);
183 }
184
185 DRM_DEBUG_KMS("LSPCON DP descriptor mismatch after resume\n");
186}
187
Shashank Sharma910530c2016-10-14 19:56:52 +0530188void lspcon_resume(struct intel_lspcon *lspcon)
189{
Imre Deak357c0ae2016-11-21 21:15:06 +0200190 enum drm_lspcon_mode expected_mode;
191
192 if (lspcon_wake_native_aux_ch(lspcon)) {
193 expected_mode = DRM_LSPCON_MODE_PCON;
Imre Deakf2b667b2016-11-21 21:15:04 +0200194 lspcon_resume_in_pcon_wa(lspcon);
Imre Deak357c0ae2016-11-21 21:15:06 +0200195 } else {
196 expected_mode = DRM_LSPCON_MODE_LS;
197 }
198
199 if (lspcon_wait_mode(lspcon, expected_mode) == DRM_LSPCON_MODE_PCON)
200 return;
Imre Deak489375c2016-10-24 19:33:31 +0300201
Imre Deak90248802016-11-21 21:15:07 +0200202 if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON))
Shashank Sharma910530c2016-10-14 19:56:52 +0530203 DRM_ERROR("LSPCON resume failed\n");
204 else
205 DRM_DEBUG_KMS("LSPCON resume success\n");
206}
207
Imre Deak357c0ae2016-11-21 21:15:06 +0200208void lspcon_wait_pcon_mode(struct intel_lspcon *lspcon)
209{
210 lspcon_wait_mode(lspcon, DRM_LSPCON_MODE_PCON);
211}
212
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530213bool lspcon_init(struct intel_digital_port *intel_dig_port)
214{
215 struct intel_dp *dp = &intel_dig_port->dp;
216 struct intel_lspcon *lspcon = &intel_dig_port->lspcon;
217 struct drm_device *dev = intel_dig_port->base.base.dev;
218 struct drm_i915_private *dev_priv = to_i915(dev);
219
Rodrigo Viviacf58d42017-08-15 20:04:03 -0700220 if (!HAS_LSPCON(dev_priv)) {
221 DRM_ERROR("LSPCON is not supported on this platform\n");
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530222 return false;
223 }
224
225 lspcon->active = false;
226 lspcon->mode = DRM_LSPCON_MODE_INVALID;
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530227
228 if (!lspcon_probe(lspcon)) {
229 DRM_ERROR("Failed to probe lspcon\n");
230 return false;
231 }
232
233 /*
234 * In the SW state machine, lets Put LSPCON in PCON mode only.
235 * In this way, it will work with both HDMI 1.4 sinks as well as HDMI
236 * 2.0 sinks.
237 */
238 if (lspcon->active && lspcon->mode != DRM_LSPCON_MODE_PCON) {
Imre Deak90248802016-11-21 21:15:07 +0200239 if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON) < 0) {
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530240 DRM_ERROR("LSPCON mode change to PCON failed\n");
241 return false;
242 }
243 }
244
Imre Deak24e807e2016-10-24 19:33:28 +0300245 if (!intel_dp_read_dpcd(dp)) {
246 DRM_ERROR("LSPCON DPCD read failed\n");
247 return false;
248 }
249
Jani Nikula84c36752017-05-18 14:10:23 +0300250 drm_dp_read_desc(&dp->aux, &dp->desc, drm_dp_is_branch(dp->dpcd));
Imre Deak12a47a422016-10-24 19:33:29 +0300251
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530252 DRM_DEBUG_KMS("Success: LSPCON init\n");
253 return true;
254}