blob: 281127d9f495c00f54d37a821795c540483060c7 [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 Sharmadbe9e612016-10-14 19:56:49 +053059 DRM_ERROR("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);
71 if (current_mode == mode || current_mode == DRM_LSPCON_MODE_INVALID)
72 goto out;
73
74 DRM_DEBUG_KMS("Waiting for LSPCON mode %s to settle\n",
75 lspcon_mode_name(mode));
76
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");
81
82out:
83 DRM_DEBUG_KMS("Current LSPCON mode %s\n",
84 lspcon_mode_name(current_mode));
85
Shashank Sharmadbe9e612016-10-14 19:56:49 +053086 return current_mode;
87}
88
89static int lspcon_change_mode(struct intel_lspcon *lspcon,
90 enum drm_lspcon_mode mode, bool force)
91{
92 int err;
93 enum drm_lspcon_mode current_mode;
Imre Deaka5d94b832016-10-24 19:33:30 +030094 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
Shashank Sharmadbe9e612016-10-14 19:56:49 +053095
96 err = drm_lspcon_get_mode(adapter, &current_mode);
97 if (err) {
98 DRM_ERROR("Error reading LSPCON mode\n");
99 return err;
100 }
101
102 if (current_mode == mode) {
103 DRM_DEBUG_KMS("Current mode = desired LSPCON mode\n");
104 return 0;
105 }
106
107 err = drm_lspcon_set_mode(adapter, mode);
108 if (err < 0) {
109 DRM_ERROR("LSPCON mode change failed\n");
110 return err;
111 }
112
113 lspcon->mode = mode;
114 DRM_DEBUG_KMS("LSPCON mode changed done\n");
115 return 0;
116}
117
Imre Deakf2b667b2016-11-21 21:15:04 +0200118static bool lspcon_wake_native_aux_ch(struct intel_lspcon *lspcon)
119{
120 uint8_t rev;
121
122 if (drm_dp_dpcd_readb(&lspcon_to_intel_dp(lspcon)->aux, DP_DPCD_REV,
123 &rev) != 1) {
124 DRM_DEBUG_KMS("Native AUX CH down\n");
125 return false;
126 }
127
128 DRM_DEBUG_KMS("Native AUX CH up, DPCD version: %d.%d\n",
129 rev >> 4, rev & 0xf);
130
131 return true;
132}
133
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530134static bool lspcon_probe(struct intel_lspcon *lspcon)
135{
136 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 */
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));
148 return false;
149 }
150
151 /* Yay ... got a LSPCON device */
152 DRM_DEBUG_KMS("LSPCON detected\n");
Imre Deak357c0ae2016-11-21 21:15:06 +0200153 lspcon->mode = lspcon_wait_mode(lspcon, expected_mode);
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530154 lspcon->active = true;
155 return true;
156}
157
Imre Deak489375c2016-10-24 19:33:31 +0300158static void lspcon_resume_in_pcon_wa(struct intel_lspcon *lspcon)
159{
160 struct intel_dp *intel_dp = lspcon_to_intel_dp(lspcon);
161 unsigned long start = jiffies;
162
163 if (!lspcon->desc_valid)
164 return;
165
166 while (1) {
167 struct intel_dp_desc desc;
168
169 /*
170 * The w/a only applies in PCON mode and we don't expect any
171 * AUX errors.
172 */
173 if (!__intel_dp_read_desc(intel_dp, &desc))
174 return;
175
176 if (!memcmp(&intel_dp->desc, &desc, sizeof(desc))) {
177 DRM_DEBUG_KMS("LSPCON recovering in PCON mode after %u ms\n",
178 jiffies_to_msecs(jiffies - start));
179 return;
180 }
181
182 if (time_after(jiffies, start + msecs_to_jiffies(1000)))
183 break;
184
185 usleep_range(10000, 15000);
186 }
187
188 DRM_DEBUG_KMS("LSPCON DP descriptor mismatch after resume\n");
189}
190
Shashank Sharma910530c2016-10-14 19:56:52 +0530191void lspcon_resume(struct intel_lspcon *lspcon)
192{
Imre Deak357c0ae2016-11-21 21:15:06 +0200193 enum drm_lspcon_mode expected_mode;
194
195 if (lspcon_wake_native_aux_ch(lspcon)) {
196 expected_mode = DRM_LSPCON_MODE_PCON;
Imre Deakf2b667b2016-11-21 21:15:04 +0200197 lspcon_resume_in_pcon_wa(lspcon);
Imre Deak357c0ae2016-11-21 21:15:06 +0200198 } else {
199 expected_mode = DRM_LSPCON_MODE_LS;
200 }
201
202 if (lspcon_wait_mode(lspcon, expected_mode) == DRM_LSPCON_MODE_PCON)
203 return;
Imre Deak489375c2016-10-24 19:33:31 +0300204
Shashank Sharma910530c2016-10-14 19:56:52 +0530205 if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON, true))
206 DRM_ERROR("LSPCON resume failed\n");
207 else
208 DRM_DEBUG_KMS("LSPCON resume success\n");
209}
210
Imre Deak357c0ae2016-11-21 21:15:06 +0200211void lspcon_wait_pcon_mode(struct intel_lspcon *lspcon)
212{
213 lspcon_wait_mode(lspcon, DRM_LSPCON_MODE_PCON);
214}
215
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530216bool lspcon_init(struct intel_digital_port *intel_dig_port)
217{
218 struct intel_dp *dp = &intel_dig_port->dp;
219 struct intel_lspcon *lspcon = &intel_dig_port->lspcon;
220 struct drm_device *dev = intel_dig_port->base.base.dev;
221 struct drm_i915_private *dev_priv = to_i915(dev);
222
223 if (!IS_GEN9(dev_priv)) {
224 DRM_ERROR("LSPCON is supported on GEN9 only\n");
225 return false;
226 }
227
228 lspcon->active = false;
229 lspcon->mode = DRM_LSPCON_MODE_INVALID;
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530230
231 if (!lspcon_probe(lspcon)) {
232 DRM_ERROR("Failed to probe lspcon\n");
233 return false;
234 }
235
236 /*
237 * In the SW state machine, lets Put LSPCON in PCON mode only.
238 * In this way, it will work with both HDMI 1.4 sinks as well as HDMI
239 * 2.0 sinks.
240 */
241 if (lspcon->active && lspcon->mode != DRM_LSPCON_MODE_PCON) {
242 if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON,
243 true) < 0) {
244 DRM_ERROR("LSPCON mode change to PCON failed\n");
245 return false;
246 }
247 }
248
Imre Deak24e807e2016-10-24 19:33:28 +0300249 if (!intel_dp_read_dpcd(dp)) {
250 DRM_ERROR("LSPCON DPCD read failed\n");
251 return false;
252 }
253
Imre Deak489375c2016-10-24 19:33:31 +0300254 lspcon->desc_valid = intel_dp_read_desc(dp);
Imre Deak12a47a422016-10-24 19:33:29 +0300255
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530256 DRM_DEBUG_KMS("Success: LSPCON init\n");
257 return true;
258}