]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_dp.c
drm/i915: Check num_pipes before initializing audio component
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_dp.c
CommitLineData
a4fc5ed6
KP
1/*
2 * Copyright © 2008 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 DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Keith Packard <keithp@keithp.com>
25 *
26 */
27
28#include <linux/i2c.h>
5a0e3ad6 29#include <linux/slab.h>
2d1a8a48 30#include <linux/export.h>
01527b31
CT
31#include <linux/notifier.h>
32#include <linux/reboot.h>
760285e7 33#include <drm/drmP.h>
c6f95f27 34#include <drm/drm_atomic_helper.h>
760285e7
DH
35#include <drm/drm_crtc.h>
36#include <drm/drm_crtc_helper.h>
37#include <drm/drm_edid.h>
a4fc5ed6 38#include "intel_drv.h"
760285e7 39#include <drm/i915_drm.h>
a4fc5ed6 40#include "i915_drv.h"
a4fc5ed6 41
a4fc5ed6
KP
42#define DP_LINK_CHECK_TIMEOUT (10 * 1000)
43
559be30c
TP
44/* Compliance test status bits */
45#define INTEL_DP_RESOLUTION_SHIFT_MASK 0
46#define INTEL_DP_RESOLUTION_PREFERRED (1 << INTEL_DP_RESOLUTION_SHIFT_MASK)
47#define INTEL_DP_RESOLUTION_STANDARD (2 << INTEL_DP_RESOLUTION_SHIFT_MASK)
48#define INTEL_DP_RESOLUTION_FAILSAFE (3 << INTEL_DP_RESOLUTION_SHIFT_MASK)
49
9dd4ffdf 50struct dp_link_dpll {
840b32b7 51 int clock;
9dd4ffdf
CML
52 struct dpll dpll;
53};
54
55static const struct dp_link_dpll gen4_dpll[] = {
840b32b7 56 { 162000,
9dd4ffdf 57 { .p1 = 2, .p2 = 10, .n = 2, .m1 = 23, .m2 = 8 } },
840b32b7 58 { 270000,
9dd4ffdf
CML
59 { .p1 = 1, .p2 = 10, .n = 1, .m1 = 14, .m2 = 2 } }
60};
61
62static const struct dp_link_dpll pch_dpll[] = {
840b32b7 63 { 162000,
9dd4ffdf 64 { .p1 = 2, .p2 = 10, .n = 1, .m1 = 12, .m2 = 9 } },
840b32b7 65 { 270000,
9dd4ffdf
CML
66 { .p1 = 1, .p2 = 10, .n = 2, .m1 = 14, .m2 = 8 } }
67};
68
65ce4bf5 69static const struct dp_link_dpll vlv_dpll[] = {
840b32b7 70 { 162000,
58f6e632 71 { .p1 = 3, .p2 = 2, .n = 5, .m1 = 3, .m2 = 81 } },
840b32b7 72 { 270000,
65ce4bf5
CML
73 { .p1 = 2, .p2 = 2, .n = 1, .m1 = 2, .m2 = 27 } }
74};
75
ef9348c8
CML
76/*
77 * CHV supports eDP 1.4 that have more link rates.
78 * Below only provides the fixed rate but exclude variable rate.
79 */
80static const struct dp_link_dpll chv_dpll[] = {
81 /*
82 * CHV requires to program fractional division for m2.
83 * m2 is stored in fixed point format using formula below
84 * (m2_int << 22) | m2_fraction
85 */
840b32b7 86 { 162000, /* m2_int = 32, m2_fraction = 1677722 */
ef9348c8 87 { .p1 = 4, .p2 = 2, .n = 1, .m1 = 2, .m2 = 0x819999a } },
840b32b7 88 { 270000, /* m2_int = 27, m2_fraction = 0 */
ef9348c8 89 { .p1 = 4, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } },
840b32b7 90 { 540000, /* m2_int = 27, m2_fraction = 0 */
ef9348c8
CML
91 { .p1 = 2, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } }
92};
637a9c63 93
64987fc5
SJ
94static const int bxt_rates[] = { 162000, 216000, 243000, 270000,
95 324000, 432000, 540000 };
637a9c63 96static const int skl_rates[] = { 162000, 216000, 270000,
f4896f15
VS
97 324000, 432000, 540000 };
98static const int default_rates[] = { 162000, 270000, 540000 };
ef9348c8 99
cfcb0fc9
JB
100/**
101 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
102 * @intel_dp: DP struct
103 *
104 * If a CPU or PCH DP output is attached to an eDP panel, this function
105 * will return true, and false otherwise.
106 */
107static bool is_edp(struct intel_dp *intel_dp)
108{
da63a9f2
PZ
109 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
110
111 return intel_dig_port->base.type == INTEL_OUTPUT_EDP;
cfcb0fc9
JB
112}
113
68b4d824 114static struct drm_device *intel_dp_to_dev(struct intel_dp *intel_dp)
cfcb0fc9 115{
68b4d824
ID
116 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
117
118 return intel_dig_port->base.base.dev;
cfcb0fc9
JB
119}
120
df0e9248
CW
121static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
122{
fa90ecef 123 return enc_to_intel_dp(&intel_attached_encoder(connector)->base);
df0e9248
CW
124}
125
ea5b213a 126static void intel_dp_link_down(struct intel_dp *intel_dp);
1e0560e0 127static bool edp_panel_vdd_on(struct intel_dp *intel_dp);
4be73780 128static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync);
093e3f13 129static void vlv_init_panel_power_sequencer(struct intel_dp *intel_dp);
a8c3344e
VS
130static void vlv_steal_power_sequencer(struct drm_device *dev,
131 enum pipe pipe);
f21a2198 132static void intel_dp_unset_edid(struct intel_dp *intel_dp);
a4fc5ed6 133
ed4e9c1d
VS
134static int
135intel_dp_max_link_bw(struct intel_dp *intel_dp)
a4fc5ed6 136{
7183dc29 137 int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
a4fc5ed6
KP
138
139 switch (max_link_bw) {
140 case DP_LINK_BW_1_62:
141 case DP_LINK_BW_2_7:
1db10e28 142 case DP_LINK_BW_5_4:
d4eead50 143 break;
a4fc5ed6 144 default:
d4eead50
ID
145 WARN(1, "invalid max DP link bw val %x, using 1.62Gbps\n",
146 max_link_bw);
a4fc5ed6
KP
147 max_link_bw = DP_LINK_BW_1_62;
148 break;
149 }
150 return max_link_bw;
151}
152
eeb6324d
PZ
153static u8 intel_dp_max_lane_count(struct intel_dp *intel_dp)
154{
155 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
eeb6324d
PZ
156 u8 source_max, sink_max;
157
ccb1a831 158 source_max = intel_dig_port->max_lanes;
f482984a 159 sink_max = intel_dp->max_sink_lane_count;
eeb6324d
PZ
160
161 return min(source_max, sink_max);
162}
163
22a2c8e0 164int
c898261c 165intel_dp_link_required(int pixel_clock, int bpp)
a4fc5ed6 166{
fd81c44e
DP
167 /* pixel_clock is in kHz, divide bpp by 8 for bit to Byte conversion */
168 return DIV_ROUND_UP(pixel_clock * bpp, 8);
a4fc5ed6
KP
169}
170
22a2c8e0 171int
fe27d53e
DA
172intel_dp_max_data_rate(int max_link_clock, int max_lanes)
173{
fd81c44e
DP
174 /* max_link_clock is the link symbol clock (LS_Clk) in kHz and not the
175 * link rate that is generally expressed in Gbps. Since, 8 bits of data
176 * is transmitted every LS_Clk per lane, there is no need to account for
177 * the channel encoding that is done in the PHY layer here.
178 */
179
180 return max_link_clock * max_lanes;
fe27d53e
DA
181}
182
70ec0645
MK
183static int
184intel_dp_downstream_max_dotclock(struct intel_dp *intel_dp)
185{
186 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
187 struct intel_encoder *encoder = &intel_dig_port->base;
188 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
189 int max_dotclk = dev_priv->max_dotclk_freq;
190 int ds_max_dotclk;
191
192 int type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
193
194 if (type != DP_DS_PORT_TYPE_VGA)
195 return max_dotclk;
196
197 ds_max_dotclk = drm_dp_downstream_max_clock(intel_dp->dpcd,
198 intel_dp->downstream_ports);
199
200 if (ds_max_dotclk != 0)
201 max_dotclk = min(max_dotclk, ds_max_dotclk);
202
203 return max_dotclk;
204}
205
40dba341
NM
206static int
207intel_dp_sink_rates(struct intel_dp *intel_dp, const int **sink_rates)
208{
209 if (intel_dp->num_sink_rates) {
210 *sink_rates = intel_dp->sink_rates;
211 return intel_dp->num_sink_rates;
212 }
213
214 *sink_rates = default_rates;
215
f482984a 216 return (intel_dp->max_sink_link_bw >> 3) + 1;
40dba341
NM
217}
218
219static int
220intel_dp_source_rates(struct intel_dp *intel_dp, const int **source_rates)
221{
222 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
223 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
224 int size;
225
cc3f90f0 226 if (IS_GEN9_LP(dev_priv)) {
40dba341
NM
227 *source_rates = bxt_rates;
228 size = ARRAY_SIZE(bxt_rates);
229 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
230 *source_rates = skl_rates;
231 size = ARRAY_SIZE(skl_rates);
232 } else {
233 *source_rates = default_rates;
234 size = ARRAY_SIZE(default_rates);
235 }
236
237 /* This depends on the fact that 5.4 is last value in the array */
238 if (!intel_dp_source_supports_hbr2(intel_dp))
239 size--;
240
241 return size;
242}
243
244static int intersect_rates(const int *source_rates, int source_len,
245 const int *sink_rates, int sink_len,
246 int *common_rates)
247{
248 int i = 0, j = 0, k = 0;
249
250 while (i < source_len && j < sink_len) {
251 if (source_rates[i] == sink_rates[j]) {
252 if (WARN_ON(k >= DP_MAX_SUPPORTED_RATES))
253 return k;
254 common_rates[k] = source_rates[i];
255 ++k;
256 ++i;
257 ++j;
258 } else if (source_rates[i] < sink_rates[j]) {
259 ++i;
260 } else {
261 ++j;
262 }
263 }
264 return k;
265}
266
267static int intel_dp_common_rates(struct intel_dp *intel_dp,
268 int *common_rates)
269{
270 const int *source_rates, *sink_rates;
271 int source_len, sink_len;
272
273 sink_len = intel_dp_sink_rates(intel_dp, &sink_rates);
274 source_len = intel_dp_source_rates(intel_dp, &source_rates);
275
276 return intersect_rates(source_rates, source_len,
277 sink_rates, sink_len,
278 common_rates);
279}
280
fdb14d33
MN
281static int intel_dp_link_rate_index(struct intel_dp *intel_dp,
282 int *common_rates, int link_rate)
283{
284 int common_len;
285 int index;
286
287 common_len = intel_dp_common_rates(intel_dp, common_rates);
288 for (index = 0; index < common_len; index++) {
289 if (link_rate == common_rates[common_len - index - 1])
290 return common_len - index - 1;
291 }
292
293 return -1;
294}
295
296int intel_dp_get_link_train_fallback_values(struct intel_dp *intel_dp,
297 int link_rate, uint8_t lane_count)
298{
299 int common_rates[DP_MAX_SUPPORTED_RATES];
300 int link_rate_index;
301
302 link_rate_index = intel_dp_link_rate_index(intel_dp,
303 common_rates,
304 link_rate);
305 if (link_rate_index > 0) {
306 intel_dp->max_sink_link_bw = drm_dp_link_rate_to_bw_code(common_rates[link_rate_index - 1]);
307 intel_dp->max_sink_lane_count = lane_count;
308 } else if (lane_count > 1) {
309 intel_dp->max_sink_link_bw = intel_dp_max_link_bw(intel_dp);
310 intel_dp->max_sink_lane_count = lane_count >> 1;
311 } else {
312 DRM_ERROR("Link Training Unsuccessful\n");
313 return -1;
314 }
315
316 return 0;
317}
318
c19de8eb 319static enum drm_mode_status
a4fc5ed6
KP
320intel_dp_mode_valid(struct drm_connector *connector,
321 struct drm_display_mode *mode)
322{
df0e9248 323 struct intel_dp *intel_dp = intel_attached_dp(connector);
dd06f90e
JN
324 struct intel_connector *intel_connector = to_intel_connector(connector);
325 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
36008365
DV
326 int target_clock = mode->clock;
327 int max_rate, mode_rate, max_lanes, max_link_clock;
70ec0645
MK
328 int max_dotclk;
329
330 max_dotclk = intel_dp_downstream_max_dotclock(intel_dp);
a4fc5ed6 331
dd06f90e
JN
332 if (is_edp(intel_dp) && fixed_mode) {
333 if (mode->hdisplay > fixed_mode->hdisplay)
7de56f43
ZY
334 return MODE_PANEL;
335
dd06f90e 336 if (mode->vdisplay > fixed_mode->vdisplay)
7de56f43 337 return MODE_PANEL;
03afc4a2
DV
338
339 target_clock = fixed_mode->clock;
7de56f43
ZY
340 }
341
50fec21a 342 max_link_clock = intel_dp_max_link_rate(intel_dp);
eeb6324d 343 max_lanes = intel_dp_max_lane_count(intel_dp);
36008365
DV
344
345 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
346 mode_rate = intel_dp_link_required(target_clock, 18);
347
799487f5 348 if (mode_rate > max_rate || target_clock > max_dotclk)
c4867936 349 return MODE_CLOCK_HIGH;
a4fc5ed6
KP
350
351 if (mode->clock < 10000)
352 return MODE_CLOCK_LOW;
353
0af78a2b
DV
354 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
355 return MODE_H_ILLEGAL;
356
a4fc5ed6
KP
357 return MODE_OK;
358}
359
a4f1289e 360uint32_t intel_dp_pack_aux(const uint8_t *src, int src_bytes)
a4fc5ed6
KP
361{
362 int i;
363 uint32_t v = 0;
364
365 if (src_bytes > 4)
366 src_bytes = 4;
367 for (i = 0; i < src_bytes; i++)
368 v |= ((uint32_t) src[i]) << ((3-i) * 8);
369 return v;
370}
371
c2af70e2 372static void intel_dp_unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
a4fc5ed6
KP
373{
374 int i;
375 if (dst_bytes > 4)
376 dst_bytes = 4;
377 for (i = 0; i < dst_bytes; i++)
378 dst[i] = src >> ((3-i) * 8);
379}
380
bf13e81b
JN
381static void
382intel_dp_init_panel_power_sequencer(struct drm_device *dev,
36b5f425 383 struct intel_dp *intel_dp);
bf13e81b
JN
384static void
385intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
36b5f425 386 struct intel_dp *intel_dp);
335f752b
ID
387static void
388intel_dp_pps_init(struct drm_device *dev, struct intel_dp *intel_dp);
bf13e81b 389
773538e8
VS
390static void pps_lock(struct intel_dp *intel_dp)
391{
392 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
393 struct intel_encoder *encoder = &intel_dig_port->base;
394 struct drm_device *dev = encoder->base.dev;
fac5e23e 395 struct drm_i915_private *dev_priv = to_i915(dev);
773538e8
VS
396 enum intel_display_power_domain power_domain;
397
398 /*
399 * See vlv_power_sequencer_reset() why we need
400 * a power domain reference here.
401 */
25f78f58 402 power_domain = intel_display_port_aux_power_domain(encoder);
773538e8
VS
403 intel_display_power_get(dev_priv, power_domain);
404
405 mutex_lock(&dev_priv->pps_mutex);
406}
407
408static void pps_unlock(struct intel_dp *intel_dp)
409{
410 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
411 struct intel_encoder *encoder = &intel_dig_port->base;
412 struct drm_device *dev = encoder->base.dev;
fac5e23e 413 struct drm_i915_private *dev_priv = to_i915(dev);
773538e8
VS
414 enum intel_display_power_domain power_domain;
415
416 mutex_unlock(&dev_priv->pps_mutex);
417
25f78f58 418 power_domain = intel_display_port_aux_power_domain(encoder);
773538e8
VS
419 intel_display_power_put(dev_priv, power_domain);
420}
421
961a0db0
VS
422static void
423vlv_power_sequencer_kick(struct intel_dp *intel_dp)
424{
425 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
30ad9814 426 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
961a0db0 427 enum pipe pipe = intel_dp->pps_pipe;
0047eedc
VS
428 bool pll_enabled, release_cl_override = false;
429 enum dpio_phy phy = DPIO_PHY(pipe);
430 enum dpio_channel ch = vlv_pipe_to_channel(pipe);
961a0db0
VS
431 uint32_t DP;
432
433 if (WARN(I915_READ(intel_dp->output_reg) & DP_PORT_EN,
434 "skipping pipe %c power seqeuncer kick due to port %c being active\n",
435 pipe_name(pipe), port_name(intel_dig_port->port)))
436 return;
437
438 DRM_DEBUG_KMS("kicking pipe %c power sequencer for port %c\n",
439 pipe_name(pipe), port_name(intel_dig_port->port));
440
441 /* Preserve the BIOS-computed detected bit. This is
442 * supposed to be read-only.
443 */
444 DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
445 DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
446 DP |= DP_PORT_WIDTH(1);
447 DP |= DP_LINK_TRAIN_PAT_1;
448
920a14b2 449 if (IS_CHERRYVIEW(dev_priv))
961a0db0
VS
450 DP |= DP_PIPE_SELECT_CHV(pipe);
451 else if (pipe == PIPE_B)
452 DP |= DP_PIPEB_SELECT;
453
d288f65f
VS
454 pll_enabled = I915_READ(DPLL(pipe)) & DPLL_VCO_ENABLE;
455
456 /*
457 * The DPLL for the pipe must be enabled for this to work.
458 * So enable temporarily it if it's not already enabled.
459 */
0047eedc 460 if (!pll_enabled) {
920a14b2 461 release_cl_override = IS_CHERRYVIEW(dev_priv) &&
0047eedc
VS
462 !chv_phy_powergate_ch(dev_priv, phy, ch, true);
463
30ad9814 464 if (vlv_force_pll_on(dev_priv, pipe, IS_CHERRYVIEW(dev_priv) ?
3f36b937
TU
465 &chv_dpll[0].dpll : &vlv_dpll[0].dpll)) {
466 DRM_ERROR("Failed to force on pll for pipe %c!\n",
467 pipe_name(pipe));
468 return;
469 }
0047eedc 470 }
d288f65f 471
961a0db0
VS
472 /*
473 * Similar magic as in intel_dp_enable_port().
474 * We _must_ do this port enable + disable trick
475 * to make this power seqeuencer lock onto the port.
476 * Otherwise even VDD force bit won't work.
477 */
478 I915_WRITE(intel_dp->output_reg, DP);
479 POSTING_READ(intel_dp->output_reg);
480
481 I915_WRITE(intel_dp->output_reg, DP | DP_PORT_EN);
482 POSTING_READ(intel_dp->output_reg);
483
484 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
485 POSTING_READ(intel_dp->output_reg);
d288f65f 486
0047eedc 487 if (!pll_enabled) {
30ad9814 488 vlv_force_pll_off(dev_priv, pipe);
0047eedc
VS
489
490 if (release_cl_override)
491 chv_phy_powergate_ch(dev_priv, phy, ch, false);
492 }
961a0db0
VS
493}
494
9f2bdb00
VS
495static enum pipe vlv_find_free_pps(struct drm_i915_private *dev_priv)
496{
497 struct intel_encoder *encoder;
498 unsigned int pipes = (1 << PIPE_A) | (1 << PIPE_B);
499
500 /*
501 * We don't have power sequencer currently.
502 * Pick one that's not used by other ports.
503 */
504 for_each_intel_encoder(&dev_priv->drm, encoder) {
505 struct intel_dp *intel_dp;
506
507 if (encoder->type != INTEL_OUTPUT_DP &&
508 encoder->type != INTEL_OUTPUT_EDP)
509 continue;
510
511 intel_dp = enc_to_intel_dp(&encoder->base);
512
513 if (encoder->type == INTEL_OUTPUT_EDP) {
514 WARN_ON(intel_dp->active_pipe != INVALID_PIPE &&
515 intel_dp->active_pipe != intel_dp->pps_pipe);
516
517 if (intel_dp->pps_pipe != INVALID_PIPE)
518 pipes &= ~(1 << intel_dp->pps_pipe);
519 } else {
520 WARN_ON(intel_dp->pps_pipe != INVALID_PIPE);
521
522 if (intel_dp->active_pipe != INVALID_PIPE)
523 pipes &= ~(1 << intel_dp->active_pipe);
524 }
525 }
526
527 if (pipes == 0)
528 return INVALID_PIPE;
529
530 return ffs(pipes) - 1;
531}
532
bf13e81b
JN
533static enum pipe
534vlv_power_sequencer_pipe(struct intel_dp *intel_dp)
535{
536 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
bf13e81b 537 struct drm_device *dev = intel_dig_port->base.base.dev;
fac5e23e 538 struct drm_i915_private *dev_priv = to_i915(dev);
a8c3344e 539 enum pipe pipe;
bf13e81b 540
e39b999a 541 lockdep_assert_held(&dev_priv->pps_mutex);
bf13e81b 542
a8c3344e
VS
543 /* We should never land here with regular DP ports */
544 WARN_ON(!is_edp(intel_dp));
545
9f2bdb00
VS
546 WARN_ON(intel_dp->active_pipe != INVALID_PIPE &&
547 intel_dp->active_pipe != intel_dp->pps_pipe);
548
a4a5d2f8
VS
549 if (intel_dp->pps_pipe != INVALID_PIPE)
550 return intel_dp->pps_pipe;
551
9f2bdb00 552 pipe = vlv_find_free_pps(dev_priv);
a4a5d2f8
VS
553
554 /*
555 * Didn't find one. This should not happen since there
556 * are two power sequencers and up to two eDP ports.
557 */
9f2bdb00 558 if (WARN_ON(pipe == INVALID_PIPE))
a8c3344e 559 pipe = PIPE_A;
a4a5d2f8 560
a8c3344e
VS
561 vlv_steal_power_sequencer(dev, pipe);
562 intel_dp->pps_pipe = pipe;
a4a5d2f8
VS
563
564 DRM_DEBUG_KMS("picked pipe %c power sequencer for port %c\n",
565 pipe_name(intel_dp->pps_pipe),
566 port_name(intel_dig_port->port));
567
568 /* init power sequencer on this pipe and port */
36b5f425
VS
569 intel_dp_init_panel_power_sequencer(dev, intel_dp);
570 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
a4a5d2f8 571
961a0db0
VS
572 /*
573 * Even vdd force doesn't work until we've made
574 * the power sequencer lock in on the port.
575 */
576 vlv_power_sequencer_kick(intel_dp);
a4a5d2f8
VS
577
578 return intel_dp->pps_pipe;
579}
580
78597996
ID
581static int
582bxt_power_sequencer_idx(struct intel_dp *intel_dp)
583{
584 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
585 struct drm_device *dev = intel_dig_port->base.base.dev;
fac5e23e 586 struct drm_i915_private *dev_priv = to_i915(dev);
78597996
ID
587
588 lockdep_assert_held(&dev_priv->pps_mutex);
589
590 /* We should never land here with regular DP ports */
591 WARN_ON(!is_edp(intel_dp));
592
593 /*
594 * TODO: BXT has 2 PPS instances. The correct port->PPS instance
595 * mapping needs to be retrieved from VBT, for now just hard-code to
596 * use instance #0 always.
597 */
598 if (!intel_dp->pps_reset)
599 return 0;
600
601 intel_dp->pps_reset = false;
602
603 /*
604 * Only the HW needs to be reprogrammed, the SW state is fixed and
605 * has been setup during connector init.
606 */
607 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
608
609 return 0;
610}
611
6491ab27
VS
612typedef bool (*vlv_pipe_check)(struct drm_i915_private *dev_priv,
613 enum pipe pipe);
614
615static bool vlv_pipe_has_pp_on(struct drm_i915_private *dev_priv,
616 enum pipe pipe)
617{
44cb734c 618 return I915_READ(PP_STATUS(pipe)) & PP_ON;
6491ab27
VS
619}
620
621static bool vlv_pipe_has_vdd_on(struct drm_i915_private *dev_priv,
622 enum pipe pipe)
623{
44cb734c 624 return I915_READ(PP_CONTROL(pipe)) & EDP_FORCE_VDD;
6491ab27
VS
625}
626
627static bool vlv_pipe_any(struct drm_i915_private *dev_priv,
628 enum pipe pipe)
629{
630 return true;
631}
bf13e81b 632
a4a5d2f8 633static enum pipe
6491ab27
VS
634vlv_initial_pps_pipe(struct drm_i915_private *dev_priv,
635 enum port port,
636 vlv_pipe_check pipe_check)
a4a5d2f8
VS
637{
638 enum pipe pipe;
bf13e81b 639
bf13e81b 640 for (pipe = PIPE_A; pipe <= PIPE_B; pipe++) {
44cb734c 641 u32 port_sel = I915_READ(PP_ON_DELAYS(pipe)) &
bf13e81b 642 PANEL_PORT_SELECT_MASK;
a4a5d2f8
VS
643
644 if (port_sel != PANEL_PORT_SELECT_VLV(port))
645 continue;
646
6491ab27
VS
647 if (!pipe_check(dev_priv, pipe))
648 continue;
649
a4a5d2f8 650 return pipe;
bf13e81b
JN
651 }
652
a4a5d2f8
VS
653 return INVALID_PIPE;
654}
655
656static void
657vlv_initial_power_sequencer_setup(struct intel_dp *intel_dp)
658{
659 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
660 struct drm_device *dev = intel_dig_port->base.base.dev;
fac5e23e 661 struct drm_i915_private *dev_priv = to_i915(dev);
a4a5d2f8
VS
662 enum port port = intel_dig_port->port;
663
664 lockdep_assert_held(&dev_priv->pps_mutex);
665
666 /* try to find a pipe with this port selected */
6491ab27
VS
667 /* first pick one where the panel is on */
668 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
669 vlv_pipe_has_pp_on);
670 /* didn't find one? pick one where vdd is on */
671 if (intel_dp->pps_pipe == INVALID_PIPE)
672 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
673 vlv_pipe_has_vdd_on);
674 /* didn't find one? pick one with just the correct port */
675 if (intel_dp->pps_pipe == INVALID_PIPE)
676 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
677 vlv_pipe_any);
a4a5d2f8
VS
678
679 /* didn't find one? just let vlv_power_sequencer_pipe() pick one when needed */
680 if (intel_dp->pps_pipe == INVALID_PIPE) {
681 DRM_DEBUG_KMS("no initial power sequencer for port %c\n",
682 port_name(port));
683 return;
bf13e81b
JN
684 }
685
a4a5d2f8
VS
686 DRM_DEBUG_KMS("initial power sequencer for port %c: pipe %c\n",
687 port_name(port), pipe_name(intel_dp->pps_pipe));
688
36b5f425
VS
689 intel_dp_init_panel_power_sequencer(dev, intel_dp);
690 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
bf13e81b
JN
691}
692
78597996 693void intel_power_sequencer_reset(struct drm_i915_private *dev_priv)
773538e8 694{
91c8a326 695 struct drm_device *dev = &dev_priv->drm;
773538e8
VS
696 struct intel_encoder *encoder;
697
920a14b2 698 if (WARN_ON(!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 699 !IS_GEN9_LP(dev_priv)))
773538e8
VS
700 return;
701
702 /*
703 * We can't grab pps_mutex here due to deadlock with power_domain
704 * mutex when power_domain functions are called while holding pps_mutex.
705 * That also means that in order to use pps_pipe the code needs to
706 * hold both a power domain reference and pps_mutex, and the power domain
707 * reference get/put must be done while _not_ holding pps_mutex.
708 * pps_{lock,unlock}() do these steps in the correct order, so one
709 * should use them always.
710 */
711
19c8054c 712 for_each_intel_encoder(dev, encoder) {
773538e8
VS
713 struct intel_dp *intel_dp;
714
9f2bdb00
VS
715 if (encoder->type != INTEL_OUTPUT_DP &&
716 encoder->type != INTEL_OUTPUT_EDP)
773538e8
VS
717 continue;
718
719 intel_dp = enc_to_intel_dp(&encoder->base);
9f2bdb00
VS
720
721 WARN_ON(intel_dp->active_pipe != INVALID_PIPE);
722
723 if (encoder->type != INTEL_OUTPUT_EDP)
724 continue;
725
cc3f90f0 726 if (IS_GEN9_LP(dev_priv))
78597996
ID
727 intel_dp->pps_reset = true;
728 else
729 intel_dp->pps_pipe = INVALID_PIPE;
773538e8 730 }
bf13e81b
JN
731}
732
8e8232d5
ID
733struct pps_registers {
734 i915_reg_t pp_ctrl;
735 i915_reg_t pp_stat;
736 i915_reg_t pp_on;
737 i915_reg_t pp_off;
738 i915_reg_t pp_div;
739};
740
741static void intel_pps_get_registers(struct drm_i915_private *dev_priv,
742 struct intel_dp *intel_dp,
743 struct pps_registers *regs)
744{
44cb734c
ID
745 int pps_idx = 0;
746
8e8232d5
ID
747 memset(regs, 0, sizeof(*regs));
748
cc3f90f0 749 if (IS_GEN9_LP(dev_priv))
44cb734c
ID
750 pps_idx = bxt_power_sequencer_idx(intel_dp);
751 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
752 pps_idx = vlv_power_sequencer_pipe(intel_dp);
8e8232d5 753
44cb734c
ID
754 regs->pp_ctrl = PP_CONTROL(pps_idx);
755 regs->pp_stat = PP_STATUS(pps_idx);
756 regs->pp_on = PP_ON_DELAYS(pps_idx);
757 regs->pp_off = PP_OFF_DELAYS(pps_idx);
cc3f90f0 758 if (!IS_GEN9_LP(dev_priv))
44cb734c 759 regs->pp_div = PP_DIVISOR(pps_idx);
8e8232d5
ID
760}
761
f0f59a00
VS
762static i915_reg_t
763_pp_ctrl_reg(struct intel_dp *intel_dp)
bf13e81b 764{
8e8232d5 765 struct pps_registers regs;
bf13e81b 766
8e8232d5
ID
767 intel_pps_get_registers(to_i915(intel_dp_to_dev(intel_dp)), intel_dp,
768 &regs);
769
770 return regs.pp_ctrl;
bf13e81b
JN
771}
772
f0f59a00
VS
773static i915_reg_t
774_pp_stat_reg(struct intel_dp *intel_dp)
bf13e81b 775{
8e8232d5 776 struct pps_registers regs;
bf13e81b 777
8e8232d5
ID
778 intel_pps_get_registers(to_i915(intel_dp_to_dev(intel_dp)), intel_dp,
779 &regs);
780
781 return regs.pp_stat;
bf13e81b
JN
782}
783
01527b31
CT
784/* Reboot notifier handler to shutdown panel power to guarantee T12 timing
785 This function only applicable when panel PM state is not to be tracked */
786static int edp_notify_handler(struct notifier_block *this, unsigned long code,
787 void *unused)
788{
789 struct intel_dp *intel_dp = container_of(this, typeof(* intel_dp),
790 edp_notifier);
791 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 792 struct drm_i915_private *dev_priv = to_i915(dev);
01527b31
CT
793
794 if (!is_edp(intel_dp) || code != SYS_RESTART)
795 return 0;
796
773538e8 797 pps_lock(intel_dp);
e39b999a 798
920a14b2 799 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
e39b999a 800 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
f0f59a00 801 i915_reg_t pp_ctrl_reg, pp_div_reg;
649636ef 802 u32 pp_div;
e39b999a 803
44cb734c
ID
804 pp_ctrl_reg = PP_CONTROL(pipe);
805 pp_div_reg = PP_DIVISOR(pipe);
01527b31
CT
806 pp_div = I915_READ(pp_div_reg);
807 pp_div &= PP_REFERENCE_DIVIDER_MASK;
808
809 /* 0x1F write to PP_DIV_REG sets max cycle delay */
810 I915_WRITE(pp_div_reg, pp_div | 0x1F);
811 I915_WRITE(pp_ctrl_reg, PANEL_UNLOCK_REGS | PANEL_POWER_OFF);
812 msleep(intel_dp->panel_power_cycle_delay);
813 }
814
773538e8 815 pps_unlock(intel_dp);
e39b999a 816
01527b31
CT
817 return 0;
818}
819
4be73780 820static bool edp_have_panel_power(struct intel_dp *intel_dp)
ebf33b18 821{
30add22d 822 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 823 struct drm_i915_private *dev_priv = to_i915(dev);
ebf33b18 824
e39b999a
VS
825 lockdep_assert_held(&dev_priv->pps_mutex);
826
920a14b2 827 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
9a42356b
VS
828 intel_dp->pps_pipe == INVALID_PIPE)
829 return false;
830
bf13e81b 831 return (I915_READ(_pp_stat_reg(intel_dp)) & PP_ON) != 0;
ebf33b18
KP
832}
833
4be73780 834static bool edp_have_panel_vdd(struct intel_dp *intel_dp)
ebf33b18 835{
30add22d 836 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 837 struct drm_i915_private *dev_priv = to_i915(dev);
ebf33b18 838
e39b999a
VS
839 lockdep_assert_held(&dev_priv->pps_mutex);
840
920a14b2 841 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
9a42356b
VS
842 intel_dp->pps_pipe == INVALID_PIPE)
843 return false;
844
773538e8 845 return I915_READ(_pp_ctrl_reg(intel_dp)) & EDP_FORCE_VDD;
ebf33b18
KP
846}
847
9b984dae
KP
848static void
849intel_dp_check_edp(struct intel_dp *intel_dp)
850{
30add22d 851 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 852 struct drm_i915_private *dev_priv = to_i915(dev);
ebf33b18 853
9b984dae
KP
854 if (!is_edp(intel_dp))
855 return;
453c5420 856
4be73780 857 if (!edp_have_panel_power(intel_dp) && !edp_have_panel_vdd(intel_dp)) {
9b984dae
KP
858 WARN(1, "eDP powered off while attempting aux channel communication.\n");
859 DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
bf13e81b
JN
860 I915_READ(_pp_stat_reg(intel_dp)),
861 I915_READ(_pp_ctrl_reg(intel_dp)));
9b984dae
KP
862 }
863}
864
9ee32fea
DV
865static uint32_t
866intel_dp_aux_wait_done(struct intel_dp *intel_dp, bool has_aux_irq)
867{
868 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
869 struct drm_device *dev = intel_dig_port->base.base.dev;
fac5e23e 870 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 871 i915_reg_t ch_ctl = intel_dp->aux_ch_ctl_reg;
9ee32fea
DV
872 uint32_t status;
873 bool done;
874
ef04f00d 875#define C (((status = I915_READ_NOTRACE(ch_ctl)) & DP_AUX_CH_CTL_SEND_BUSY) == 0)
9ee32fea 876 if (has_aux_irq)
b18ac466 877 done = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
3598706b 878 msecs_to_jiffies_timeout(10));
9ee32fea 879 else
713a6b66 880 done = wait_for(C, 10) == 0;
9ee32fea
DV
881 if (!done)
882 DRM_ERROR("dp aux hw did not signal timeout (has irq: %i)!\n",
883 has_aux_irq);
884#undef C
885
886 return status;
887}
888
6ffb1be7 889static uint32_t g4x_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
a4fc5ed6 890{
174edf1f 891 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
e7dc33f3 892 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
9ee32fea 893
a457f54b
VS
894 if (index)
895 return 0;
896
ec5b01dd
DL
897 /*
898 * The clock divider is based off the hrawclk, and would like to run at
a457f54b 899 * 2MHz. So, take the hrawclk value and divide by 2000 and use that
a4fc5ed6 900 */
a457f54b 901 return DIV_ROUND_CLOSEST(dev_priv->rawclk_freq, 2000);
ec5b01dd
DL
902}
903
904static uint32_t ilk_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
905{
906 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
a457f54b 907 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
ec5b01dd
DL
908
909 if (index)
910 return 0;
911
a457f54b
VS
912 /*
913 * The clock divider is based off the cdclk or PCH rawclk, and would
914 * like to run at 2MHz. So, take the cdclk or PCH rawclk value and
915 * divide by 2000 and use that
916 */
e7dc33f3 917 if (intel_dig_port->port == PORT_A)
fce18c4c 918 return DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 2000);
e7dc33f3
VS
919 else
920 return DIV_ROUND_CLOSEST(dev_priv->rawclk_freq, 2000);
ec5b01dd
DL
921}
922
923static uint32_t hsw_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
924{
925 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
a457f54b 926 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
ec5b01dd 927
a457f54b 928 if (intel_dig_port->port != PORT_A && HAS_PCH_LPT_H(dev_priv)) {
2c55c336 929 /* Workaround for non-ULT HSW */
bc86625a
CW
930 switch (index) {
931 case 0: return 63;
932 case 1: return 72;
933 default: return 0;
934 }
2c55c336 935 }
a457f54b
VS
936
937 return ilk_get_aux_clock_divider(intel_dp, index);
b84a1cf8
RV
938}
939
b6b5e383
DL
940static uint32_t skl_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
941{
942 /*
943 * SKL doesn't need us to program the AUX clock divider (Hardware will
944 * derive the clock from CDCLK automatically). We still implement the
945 * get_aux_clock_divider vfunc to plug-in into the existing code.
946 */
947 return index ? 0 : 1;
948}
949
6ffb1be7
VS
950static uint32_t g4x_get_aux_send_ctl(struct intel_dp *intel_dp,
951 bool has_aux_irq,
952 int send_bytes,
953 uint32_t aux_clock_divider)
5ed12a19
DL
954{
955 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
8652744b
TU
956 struct drm_i915_private *dev_priv =
957 to_i915(intel_dig_port->base.base.dev);
5ed12a19
DL
958 uint32_t precharge, timeout;
959
8652744b 960 if (IS_GEN6(dev_priv))
5ed12a19
DL
961 precharge = 3;
962 else
963 precharge = 5;
964
8652744b 965 if (IS_BROADWELL(dev_priv) && intel_dig_port->port == PORT_A)
5ed12a19
DL
966 timeout = DP_AUX_CH_CTL_TIME_OUT_600us;
967 else
968 timeout = DP_AUX_CH_CTL_TIME_OUT_400us;
969
970 return DP_AUX_CH_CTL_SEND_BUSY |
788d4433 971 DP_AUX_CH_CTL_DONE |
5ed12a19 972 (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
788d4433 973 DP_AUX_CH_CTL_TIME_OUT_ERROR |
5ed12a19 974 timeout |
788d4433 975 DP_AUX_CH_CTL_RECEIVE_ERROR |
5ed12a19
DL
976 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
977 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
788d4433 978 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT);
5ed12a19
DL
979}
980
b9ca5fad
DL
981static uint32_t skl_get_aux_send_ctl(struct intel_dp *intel_dp,
982 bool has_aux_irq,
983 int send_bytes,
984 uint32_t unused)
985{
986 return DP_AUX_CH_CTL_SEND_BUSY |
987 DP_AUX_CH_CTL_DONE |
988 (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
989 DP_AUX_CH_CTL_TIME_OUT_ERROR |
990 DP_AUX_CH_CTL_TIME_OUT_1600us |
991 DP_AUX_CH_CTL_RECEIVE_ERROR |
992 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
d4dcbdce 993 DP_AUX_CH_CTL_FW_SYNC_PULSE_SKL(32) |
b9ca5fad
DL
994 DP_AUX_CH_CTL_SYNC_PULSE_SKL(32);
995}
996
b84a1cf8
RV
997static int
998intel_dp_aux_ch(struct intel_dp *intel_dp,
bd9f74a5 999 const uint8_t *send, int send_bytes,
b84a1cf8
RV
1000 uint8_t *recv, int recv_size)
1001{
1002 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
0031fb96
TU
1003 struct drm_i915_private *dev_priv =
1004 to_i915(intel_dig_port->base.base.dev);
f0f59a00 1005 i915_reg_t ch_ctl = intel_dp->aux_ch_ctl_reg;
bc86625a 1006 uint32_t aux_clock_divider;
b84a1cf8
RV
1007 int i, ret, recv_bytes;
1008 uint32_t status;
5ed12a19 1009 int try, clock = 0;
0031fb96 1010 bool has_aux_irq = HAS_AUX_IRQ(dev_priv);
884f19e9
JN
1011 bool vdd;
1012
773538e8 1013 pps_lock(intel_dp);
e39b999a 1014
72c3500a
VS
1015 /*
1016 * We will be called with VDD already enabled for dpcd/edid/oui reads.
1017 * In such cases we want to leave VDD enabled and it's up to upper layers
1018 * to turn it off. But for eg. i2c-dev access we need to turn it on/off
1019 * ourselves.
1020 */
1e0560e0 1021 vdd = edp_panel_vdd_on(intel_dp);
b84a1cf8
RV
1022
1023 /* dp aux is extremely sensitive to irq latency, hence request the
1024 * lowest possible wakeup latency and so prevent the cpu from going into
1025 * deep sleep states.
1026 */
1027 pm_qos_update_request(&dev_priv->pm_qos, 0);
1028
1029 intel_dp_check_edp(intel_dp);
5eb08b69 1030
11bee43e
JB
1031 /* Try to wait for any previous AUX channel activity */
1032 for (try = 0; try < 3; try++) {
ef04f00d 1033 status = I915_READ_NOTRACE(ch_ctl);
11bee43e
JB
1034 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
1035 break;
1036 msleep(1);
1037 }
1038
1039 if (try == 3) {
02196c77
MK
1040 static u32 last_status = -1;
1041 const u32 status = I915_READ(ch_ctl);
1042
1043 if (status != last_status) {
1044 WARN(1, "dp_aux_ch not started status 0x%08x\n",
1045 status);
1046 last_status = status;
1047 }
1048
9ee32fea
DV
1049 ret = -EBUSY;
1050 goto out;
4f7f7b7e
CW
1051 }
1052
46a5ae9f
PZ
1053 /* Only 5 data registers! */
1054 if (WARN_ON(send_bytes > 20 || recv_size > 20)) {
1055 ret = -E2BIG;
1056 goto out;
1057 }
1058
ec5b01dd 1059 while ((aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, clock++))) {
153b1100
DL
1060 u32 send_ctl = intel_dp->get_aux_send_ctl(intel_dp,
1061 has_aux_irq,
1062 send_bytes,
1063 aux_clock_divider);
5ed12a19 1064
bc86625a
CW
1065 /* Must try at least 3 times according to DP spec */
1066 for (try = 0; try < 5; try++) {
1067 /* Load the send data into the aux channel data registers */
1068 for (i = 0; i < send_bytes; i += 4)
330e20ec 1069 I915_WRITE(intel_dp->aux_ch_data_reg[i >> 2],
a4f1289e
RV
1070 intel_dp_pack_aux(send + i,
1071 send_bytes - i));
bc86625a
CW
1072
1073 /* Send the command and wait for it to complete */
5ed12a19 1074 I915_WRITE(ch_ctl, send_ctl);
bc86625a
CW
1075
1076 status = intel_dp_aux_wait_done(intel_dp, has_aux_irq);
1077
1078 /* Clear done status and any errors */
1079 I915_WRITE(ch_ctl,
1080 status |
1081 DP_AUX_CH_CTL_DONE |
1082 DP_AUX_CH_CTL_TIME_OUT_ERROR |
1083 DP_AUX_CH_CTL_RECEIVE_ERROR);
1084
74ebf294 1085 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR)
bc86625a 1086 continue;
74ebf294
TP
1087
1088 /* DP CTS 1.2 Core Rev 1.1, 4.2.1.1 & 4.2.1.2
1089 * 400us delay required for errors and timeouts
1090 * Timeout errors from the HW already meet this
1091 * requirement so skip to next iteration
1092 */
1093 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
1094 usleep_range(400, 500);
bc86625a 1095 continue;
74ebf294 1096 }
bc86625a 1097 if (status & DP_AUX_CH_CTL_DONE)
e058c945 1098 goto done;
bc86625a 1099 }
a4fc5ed6
KP
1100 }
1101
a4fc5ed6 1102 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
1ae8c0a5 1103 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
9ee32fea
DV
1104 ret = -EBUSY;
1105 goto out;
a4fc5ed6
KP
1106 }
1107
e058c945 1108done:
a4fc5ed6
KP
1109 /* Check for timeout or receive error.
1110 * Timeouts occur when the sink is not connected
1111 */
a5b3da54 1112 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
1ae8c0a5 1113 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
9ee32fea
DV
1114 ret = -EIO;
1115 goto out;
a5b3da54 1116 }
1ae8c0a5
KP
1117
1118 /* Timeouts occur when the device isn't connected, so they're
1119 * "normal" -- don't fill the kernel log with these */
a5b3da54 1120 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
28c97730 1121 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
9ee32fea
DV
1122 ret = -ETIMEDOUT;
1123 goto out;
a4fc5ed6
KP
1124 }
1125
1126 /* Unload any bytes sent back from the other side */
1127 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
1128 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
14e01889
RV
1129
1130 /*
1131 * By BSpec: "Message sizes of 0 or >20 are not allowed."
1132 * We have no idea of what happened so we return -EBUSY so
1133 * drm layer takes care for the necessary retries.
1134 */
1135 if (recv_bytes == 0 || recv_bytes > 20) {
1136 DRM_DEBUG_KMS("Forbidden recv_bytes = %d on aux transaction\n",
1137 recv_bytes);
1138 /*
1139 * FIXME: This patch was created on top of a series that
1140 * organize the retries at drm level. There EBUSY should
1141 * also take care for 1ms wait before retrying.
1142 * That aux retries re-org is still needed and after that is
1143 * merged we remove this sleep from here.
1144 */
1145 usleep_range(1000, 1500);
1146 ret = -EBUSY;
1147 goto out;
1148 }
1149
a4fc5ed6
KP
1150 if (recv_bytes > recv_size)
1151 recv_bytes = recv_size;
0206e353 1152
4f7f7b7e 1153 for (i = 0; i < recv_bytes; i += 4)
330e20ec 1154 intel_dp_unpack_aux(I915_READ(intel_dp->aux_ch_data_reg[i >> 2]),
a4f1289e 1155 recv + i, recv_bytes - i);
a4fc5ed6 1156
9ee32fea
DV
1157 ret = recv_bytes;
1158out:
1159 pm_qos_update_request(&dev_priv->pm_qos, PM_QOS_DEFAULT_VALUE);
1160
884f19e9
JN
1161 if (vdd)
1162 edp_panel_vdd_off(intel_dp, false);
1163
773538e8 1164 pps_unlock(intel_dp);
e39b999a 1165
9ee32fea 1166 return ret;
a4fc5ed6
KP
1167}
1168
a6c8aff0
JN
1169#define BARE_ADDRESS_SIZE 3
1170#define HEADER_SIZE (BARE_ADDRESS_SIZE + 1)
9d1a1031
JN
1171static ssize_t
1172intel_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
a4fc5ed6 1173{
9d1a1031
JN
1174 struct intel_dp *intel_dp = container_of(aux, struct intel_dp, aux);
1175 uint8_t txbuf[20], rxbuf[20];
1176 size_t txsize, rxsize;
a4fc5ed6 1177 int ret;
a4fc5ed6 1178
d2d9cbbd
VS
1179 txbuf[0] = (msg->request << 4) |
1180 ((msg->address >> 16) & 0xf);
1181 txbuf[1] = (msg->address >> 8) & 0xff;
9d1a1031
JN
1182 txbuf[2] = msg->address & 0xff;
1183 txbuf[3] = msg->size - 1;
46a5ae9f 1184
9d1a1031
JN
1185 switch (msg->request & ~DP_AUX_I2C_MOT) {
1186 case DP_AUX_NATIVE_WRITE:
1187 case DP_AUX_I2C_WRITE:
c1e74122 1188 case DP_AUX_I2C_WRITE_STATUS_UPDATE:
a6c8aff0 1189 txsize = msg->size ? HEADER_SIZE + msg->size : BARE_ADDRESS_SIZE;
a1ddefd8 1190 rxsize = 2; /* 0 or 1 data bytes */
f51a44b9 1191
9d1a1031
JN
1192 if (WARN_ON(txsize > 20))
1193 return -E2BIG;
a4fc5ed6 1194
dd788090
VS
1195 WARN_ON(!msg->buffer != !msg->size);
1196
d81a67cc
ID
1197 if (msg->buffer)
1198 memcpy(txbuf + HEADER_SIZE, msg->buffer, msg->size);
a4fc5ed6 1199
9d1a1031
JN
1200 ret = intel_dp_aux_ch(intel_dp, txbuf, txsize, rxbuf, rxsize);
1201 if (ret > 0) {
1202 msg->reply = rxbuf[0] >> 4;
a4fc5ed6 1203
a1ddefd8
JN
1204 if (ret > 1) {
1205 /* Number of bytes written in a short write. */
1206 ret = clamp_t(int, rxbuf[1], 0, msg->size);
1207 } else {
1208 /* Return payload size. */
1209 ret = msg->size;
1210 }
9d1a1031
JN
1211 }
1212 break;
46a5ae9f 1213
9d1a1031
JN
1214 case DP_AUX_NATIVE_READ:
1215 case DP_AUX_I2C_READ:
a6c8aff0 1216 txsize = msg->size ? HEADER_SIZE : BARE_ADDRESS_SIZE;
9d1a1031 1217 rxsize = msg->size + 1;
a4fc5ed6 1218
9d1a1031
JN
1219 if (WARN_ON(rxsize > 20))
1220 return -E2BIG;
a4fc5ed6 1221
9d1a1031
JN
1222 ret = intel_dp_aux_ch(intel_dp, txbuf, txsize, rxbuf, rxsize);
1223 if (ret > 0) {
1224 msg->reply = rxbuf[0] >> 4;
1225 /*
1226 * Assume happy day, and copy the data. The caller is
1227 * expected to check msg->reply before touching it.
1228 *
1229 * Return payload size.
1230 */
1231 ret--;
1232 memcpy(msg->buffer, rxbuf + 1, ret);
a4fc5ed6 1233 }
9d1a1031
JN
1234 break;
1235
1236 default:
1237 ret = -EINVAL;
1238 break;
a4fc5ed6 1239 }
f51a44b9 1240
9d1a1031 1241 return ret;
a4fc5ed6
KP
1242}
1243
8f7ce038
VS
1244static enum port intel_aux_port(struct drm_i915_private *dev_priv,
1245 enum port port)
1246{
1247 const struct ddi_vbt_port_info *info =
1248 &dev_priv->vbt.ddi_port_info[port];
1249 enum port aux_port;
1250
1251 if (!info->alternate_aux_channel) {
1252 DRM_DEBUG_KMS("using AUX %c for port %c (platform default)\n",
1253 port_name(port), port_name(port));
1254 return port;
1255 }
1256
1257 switch (info->alternate_aux_channel) {
1258 case DP_AUX_A:
1259 aux_port = PORT_A;
1260 break;
1261 case DP_AUX_B:
1262 aux_port = PORT_B;
1263 break;
1264 case DP_AUX_C:
1265 aux_port = PORT_C;
1266 break;
1267 case DP_AUX_D:
1268 aux_port = PORT_D;
1269 break;
1270 default:
1271 MISSING_CASE(info->alternate_aux_channel);
1272 aux_port = PORT_A;
1273 break;
1274 }
1275
1276 DRM_DEBUG_KMS("using AUX %c for port %c (VBT)\n",
1277 port_name(aux_port), port_name(port));
1278
1279 return aux_port;
1280}
1281
f0f59a00 1282static i915_reg_t g4x_aux_ctl_reg(struct drm_i915_private *dev_priv,
c8a89b08 1283 enum port port)
da00bdcf
VS
1284{
1285 switch (port) {
1286 case PORT_B:
1287 case PORT_C:
1288 case PORT_D:
1289 return DP_AUX_CH_CTL(port);
1290 default:
1291 MISSING_CASE(port);
1292 return DP_AUX_CH_CTL(PORT_B);
1293 }
1294}
1295
f0f59a00 1296static i915_reg_t g4x_aux_data_reg(struct drm_i915_private *dev_priv,
c8a89b08 1297 enum port port, int index)
330e20ec
VS
1298{
1299 switch (port) {
1300 case PORT_B:
1301 case PORT_C:
1302 case PORT_D:
1303 return DP_AUX_CH_DATA(port, index);
1304 default:
1305 MISSING_CASE(port);
1306 return DP_AUX_CH_DATA(PORT_B, index);
1307 }
1308}
1309
f0f59a00 1310static i915_reg_t ilk_aux_ctl_reg(struct drm_i915_private *dev_priv,
c8a89b08 1311 enum port port)
da00bdcf
VS
1312{
1313 switch (port) {
1314 case PORT_A:
1315 return DP_AUX_CH_CTL(port);
1316 case PORT_B:
1317 case PORT_C:
1318 case PORT_D:
1319 return PCH_DP_AUX_CH_CTL(port);
1320 default:
1321 MISSING_CASE(port);
1322 return DP_AUX_CH_CTL(PORT_A);
1323 }
1324}
1325
f0f59a00 1326static i915_reg_t ilk_aux_data_reg(struct drm_i915_private *dev_priv,
c8a89b08 1327 enum port port, int index)
330e20ec
VS
1328{
1329 switch (port) {
1330 case PORT_A:
1331 return DP_AUX_CH_DATA(port, index);
1332 case PORT_B:
1333 case PORT_C:
1334 case PORT_D:
1335 return PCH_DP_AUX_CH_DATA(port, index);
1336 default:
1337 MISSING_CASE(port);
1338 return DP_AUX_CH_DATA(PORT_A, index);
1339 }
1340}
1341
f0f59a00 1342static i915_reg_t skl_aux_ctl_reg(struct drm_i915_private *dev_priv,
c8a89b08 1343 enum port port)
da00bdcf 1344{
da00bdcf
VS
1345 switch (port) {
1346 case PORT_A:
1347 case PORT_B:
1348 case PORT_C:
1349 case PORT_D:
1350 return DP_AUX_CH_CTL(port);
1351 default:
1352 MISSING_CASE(port);
1353 return DP_AUX_CH_CTL(PORT_A);
1354 }
1355}
1356
f0f59a00 1357static i915_reg_t skl_aux_data_reg(struct drm_i915_private *dev_priv,
c8a89b08 1358 enum port port, int index)
330e20ec 1359{
330e20ec
VS
1360 switch (port) {
1361 case PORT_A:
1362 case PORT_B:
1363 case PORT_C:
1364 case PORT_D:
1365 return DP_AUX_CH_DATA(port, index);
1366 default:
1367 MISSING_CASE(port);
1368 return DP_AUX_CH_DATA(PORT_A, index);
1369 }
1370}
1371
f0f59a00 1372static i915_reg_t intel_aux_ctl_reg(struct drm_i915_private *dev_priv,
c8a89b08 1373 enum port port)
330e20ec
VS
1374{
1375 if (INTEL_INFO(dev_priv)->gen >= 9)
1376 return skl_aux_ctl_reg(dev_priv, port);
1377 else if (HAS_PCH_SPLIT(dev_priv))
1378 return ilk_aux_ctl_reg(dev_priv, port);
1379 else
1380 return g4x_aux_ctl_reg(dev_priv, port);
1381}
1382
f0f59a00 1383static i915_reg_t intel_aux_data_reg(struct drm_i915_private *dev_priv,
c8a89b08 1384 enum port port, int index)
330e20ec
VS
1385{
1386 if (INTEL_INFO(dev_priv)->gen >= 9)
1387 return skl_aux_data_reg(dev_priv, port, index);
1388 else if (HAS_PCH_SPLIT(dev_priv))
1389 return ilk_aux_data_reg(dev_priv, port, index);
1390 else
1391 return g4x_aux_data_reg(dev_priv, port, index);
1392}
1393
1394static void intel_aux_reg_init(struct intel_dp *intel_dp)
1395{
1396 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
8f7ce038
VS
1397 enum port port = intel_aux_port(dev_priv,
1398 dp_to_dig_port(intel_dp)->port);
330e20ec
VS
1399 int i;
1400
1401 intel_dp->aux_ch_ctl_reg = intel_aux_ctl_reg(dev_priv, port);
1402 for (i = 0; i < ARRAY_SIZE(intel_dp->aux_ch_data_reg); i++)
1403 intel_dp->aux_ch_data_reg[i] = intel_aux_data_reg(dev_priv, port, i);
1404}
1405
9d1a1031 1406static void
a121f4e5
VS
1407intel_dp_aux_fini(struct intel_dp *intel_dp)
1408{
a121f4e5
VS
1409 kfree(intel_dp->aux.name);
1410}
1411
7a418e34 1412static void
b6339585 1413intel_dp_aux_init(struct intel_dp *intel_dp)
9d1a1031 1414{
33ad6626
JN
1415 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1416 enum port port = intel_dig_port->port;
ab2c0672 1417
330e20ec 1418 intel_aux_reg_init(intel_dp);
7a418e34 1419 drm_dp_aux_init(&intel_dp->aux);
8316f337 1420
7a418e34 1421 /* Failure to allocate our preferred name is not critical */
a121f4e5 1422 intel_dp->aux.name = kasprintf(GFP_KERNEL, "DPDDC-%c", port_name(port));
9d1a1031 1423 intel_dp->aux.transfer = intel_dp_aux_transfer;
a4fc5ed6
KP
1424}
1425
e588fa18 1426bool intel_dp_source_supports_hbr2(struct intel_dp *intel_dp)
ed63baaf 1427{
e588fa18 1428 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
577c5430 1429 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
e588fa18 1430
577c5430
NM
1431 if ((IS_HASWELL(dev_priv) && !IS_HSW_ULX(dev_priv)) ||
1432 IS_BROADWELL(dev_priv) || (INTEL_GEN(dev_priv) >= 9))
ed63baaf
TS
1433 return true;
1434 else
1435 return false;
1436}
1437
c6bb3538
DV
1438static void
1439intel_dp_set_clock(struct intel_encoder *encoder,
840b32b7 1440 struct intel_crtc_state *pipe_config)
c6bb3538
DV
1441{
1442 struct drm_device *dev = encoder->base.dev;
6e266956 1443 struct drm_i915_private *dev_priv = to_i915(dev);
9dd4ffdf
CML
1444 const struct dp_link_dpll *divisor = NULL;
1445 int i, count = 0;
c6bb3538 1446
9beb5fea 1447 if (IS_G4X(dev_priv)) {
9dd4ffdf
CML
1448 divisor = gen4_dpll;
1449 count = ARRAY_SIZE(gen4_dpll);
6e266956 1450 } else if (HAS_PCH_SPLIT(dev_priv)) {
9dd4ffdf
CML
1451 divisor = pch_dpll;
1452 count = ARRAY_SIZE(pch_dpll);
920a14b2 1453 } else if (IS_CHERRYVIEW(dev_priv)) {
ef9348c8
CML
1454 divisor = chv_dpll;
1455 count = ARRAY_SIZE(chv_dpll);
11a914c2 1456 } else if (IS_VALLEYVIEW(dev_priv)) {
65ce4bf5
CML
1457 divisor = vlv_dpll;
1458 count = ARRAY_SIZE(vlv_dpll);
c6bb3538 1459 }
9dd4ffdf
CML
1460
1461 if (divisor && count) {
1462 for (i = 0; i < count; i++) {
840b32b7 1463 if (pipe_config->port_clock == divisor[i].clock) {
9dd4ffdf
CML
1464 pipe_config->dpll = divisor[i].dpll;
1465 pipe_config->clock_set = true;
1466 break;
1467 }
1468 }
c6bb3538
DV
1469 }
1470}
1471
0336400e
VS
1472static void snprintf_int_array(char *str, size_t len,
1473 const int *array, int nelem)
1474{
1475 int i;
1476
1477 str[0] = '\0';
1478
1479 for (i = 0; i < nelem; i++) {
b2f505be 1480 int r = snprintf(str, len, "%s%d", i ? ", " : "", array[i]);
0336400e
VS
1481 if (r >= len)
1482 return;
1483 str += r;
1484 len -= r;
1485 }
1486}
1487
1488static void intel_dp_print_rates(struct intel_dp *intel_dp)
1489{
0336400e 1490 const int *source_rates, *sink_rates;
94ca719e
VS
1491 int source_len, sink_len, common_len;
1492 int common_rates[DP_MAX_SUPPORTED_RATES];
0336400e
VS
1493 char str[128]; /* FIXME: too big for stack? */
1494
1495 if ((drm_debug & DRM_UT_KMS) == 0)
1496 return;
1497
e588fa18 1498 source_len = intel_dp_source_rates(intel_dp, &source_rates);
0336400e
VS
1499 snprintf_int_array(str, sizeof(str), source_rates, source_len);
1500 DRM_DEBUG_KMS("source rates: %s\n", str);
1501
1502 sink_len = intel_dp_sink_rates(intel_dp, &sink_rates);
1503 snprintf_int_array(str, sizeof(str), sink_rates, sink_len);
1504 DRM_DEBUG_KMS("sink rates: %s\n", str);
1505
94ca719e
VS
1506 common_len = intel_dp_common_rates(intel_dp, common_rates);
1507 snprintf_int_array(str, sizeof(str), common_rates, common_len);
1508 DRM_DEBUG_KMS("common rates: %s\n", str);
0336400e
VS
1509}
1510
489375c8 1511bool
7b3fc170 1512__intel_dp_read_desc(struct intel_dp *intel_dp, struct intel_dp_desc *desc)
0e390a33 1513{
7b3fc170
ID
1514 u32 base = drm_dp_is_branch(intel_dp->dpcd) ? DP_BRANCH_OUI :
1515 DP_SINK_OUI;
0e390a33 1516
7b3fc170
ID
1517 return drm_dp_dpcd_read(&intel_dp->aux, base, desc, sizeof(*desc)) ==
1518 sizeof(*desc);
0e390a33
MK
1519}
1520
12a47a42 1521bool intel_dp_read_desc(struct intel_dp *intel_dp)
1a2724fa 1522{
7b3fc170
ID
1523 struct intel_dp_desc *desc = &intel_dp->desc;
1524 bool oui_sup = intel_dp->dpcd[DP_DOWN_STREAM_PORT_COUNT] &
1525 DP_OUI_SUPPORT;
1526 int dev_id_len;
1a2724fa 1527
7b3fc170
ID
1528 if (!__intel_dp_read_desc(intel_dp, desc))
1529 return false;
1a2724fa 1530
7b3fc170
ID
1531 dev_id_len = strnlen(desc->device_id, sizeof(desc->device_id));
1532 DRM_DEBUG_KMS("DP %s: OUI %*phD%s dev-ID %*pE HW-rev %d.%d SW-rev %d.%d\n",
1533 drm_dp_is_branch(intel_dp->dpcd) ? "branch" : "sink",
1534 (int)sizeof(desc->oui), desc->oui, oui_sup ? "" : "(NS)",
1535 dev_id_len, desc->device_id,
1536 desc->hw_rev >> 4, desc->hw_rev & 0xf,
1537 desc->sw_major_rev, desc->sw_minor_rev);
1a2724fa 1538
7b3fc170 1539 return true;
1a2724fa
MK
1540}
1541
f4896f15 1542static int rate_to_index(int find, const int *rates)
a8f3ef61
SJ
1543{
1544 int i = 0;
1545
1546 for (i = 0; i < DP_MAX_SUPPORTED_RATES; ++i)
1547 if (find == rates[i])
1548 break;
1549
1550 return i;
1551}
1552
50fec21a
VS
1553int
1554intel_dp_max_link_rate(struct intel_dp *intel_dp)
1555{
1556 int rates[DP_MAX_SUPPORTED_RATES] = {};
1557 int len;
1558
94ca719e 1559 len = intel_dp_common_rates(intel_dp, rates);
50fec21a
VS
1560 if (WARN_ON(len <= 0))
1561 return 162000;
1562
1354f734 1563 return rates[len - 1];
50fec21a
VS
1564}
1565
ed4e9c1d
VS
1566int intel_dp_rate_select(struct intel_dp *intel_dp, int rate)
1567{
94ca719e 1568 return rate_to_index(rate, intel_dp->sink_rates);
ed4e9c1d
VS
1569}
1570
94223d04
ACO
1571void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
1572 uint8_t *link_bw, uint8_t *rate_select)
04a60f9f
VS
1573{
1574 if (intel_dp->num_sink_rates) {
1575 *link_bw = 0;
1576 *rate_select =
1577 intel_dp_rate_select(intel_dp, port_clock);
1578 } else {
1579 *link_bw = drm_dp_link_rate_to_bw_code(port_clock);
1580 *rate_select = 0;
1581 }
1582}
1583
f580bea9
JN
1584static int intel_dp_compute_bpp(struct intel_dp *intel_dp,
1585 struct intel_crtc_state *pipe_config)
f9bb705e
MK
1586{
1587 int bpp, bpc;
1588
1589 bpp = pipe_config->pipe_bpp;
1590 bpc = drm_dp_downstream_max_bpc(intel_dp->dpcd, intel_dp->downstream_ports);
1591
1592 if (bpc > 0)
1593 bpp = min(bpp, 3*bpc);
1594
1595 return bpp;
1596}
1597
00c09d70 1598bool
5bfe2ac0 1599intel_dp_compute_config(struct intel_encoder *encoder,
0a478c27
ML
1600 struct intel_crtc_state *pipe_config,
1601 struct drm_connector_state *conn_state)
a4fc5ed6 1602{
dd11bc10 1603 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2d112de7 1604 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
5bfe2ac0 1605 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
bc7d38a4 1606 enum port port = dp_to_dig_port(intel_dp)->port;
84556d58 1607 struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
dd06f90e 1608 struct intel_connector *intel_connector = intel_dp->attached_connector;
a4fc5ed6 1609 int lane_count, clock;
56071a20 1610 int min_lane_count = 1;
eeb6324d 1611 int max_lane_count = intel_dp_max_lane_count(intel_dp);
06ea66b6 1612 /* Conveniently, the link BW constants become indices with a shift...*/
56071a20 1613 int min_clock = 0;
a8f3ef61 1614 int max_clock;
083f9560 1615 int bpp, mode_rate;
ff9a6750 1616 int link_avail, link_clock;
94ca719e
VS
1617 int common_rates[DP_MAX_SUPPORTED_RATES] = {};
1618 int common_len;
04a60f9f 1619 uint8_t link_bw, rate_select;
a8f3ef61 1620
94ca719e 1621 common_len = intel_dp_common_rates(intel_dp, common_rates);
a8f3ef61
SJ
1622
1623 /* No common link rates between source and sink */
94ca719e 1624 WARN_ON(common_len <= 0);
a8f3ef61 1625
94ca719e 1626 max_clock = common_len - 1;
a4fc5ed6 1627
4f8036a2 1628 if (HAS_PCH_SPLIT(dev_priv) && !HAS_DDI(dev_priv) && port != PORT_A)
5bfe2ac0
DV
1629 pipe_config->has_pch_encoder = true;
1630
f769cd24 1631 pipe_config->has_drrs = false;
9fcb1704 1632 pipe_config->has_audio = intel_dp->has_audio && port != PORT_A;
a4fc5ed6 1633
dd06f90e
JN
1634 if (is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
1635 intel_fixed_panel_mode(intel_connector->panel.fixed_mode,
1636 adjusted_mode);
a1b2278e 1637
dd11bc10 1638 if (INTEL_GEN(dev_priv) >= 9) {
a1b2278e 1639 int ret;
e435d6e5 1640 ret = skl_update_scaler_crtc(pipe_config);
a1b2278e
CK
1641 if (ret)
1642 return ret;
1643 }
1644
49cff963 1645 if (HAS_GMCH_DISPLAY(dev_priv))
2dd24552
JB
1646 intel_gmch_panel_fitting(intel_crtc, pipe_config,
1647 intel_connector->panel.fitting_mode);
1648 else
b074cec8
JB
1649 intel_pch_panel_fitting(intel_crtc, pipe_config,
1650 intel_connector->panel.fitting_mode);
0d3a1bee
ZY
1651 }
1652
cb1793ce 1653 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
0af78a2b
DV
1654 return false;
1655
083f9560 1656 DRM_DEBUG_KMS("DP link computation with max lane count %i "
a8f3ef61 1657 "max bw %d pixel clock %iKHz\n",
94ca719e 1658 max_lane_count, common_rates[max_clock],
241bfc38 1659 adjusted_mode->crtc_clock);
083f9560 1660
36008365
DV
1661 /* Walk through all bpp values. Luckily they're all nicely spaced with 2
1662 * bpc in between. */
f9bb705e 1663 bpp = intel_dp_compute_bpp(intel_dp, pipe_config);
56071a20 1664 if (is_edp(intel_dp)) {
22ce5628
TS
1665
1666 /* Get bpp from vbt only for panels that dont have bpp in edid */
1667 if (intel_connector->base.display_info.bpc == 0 &&
6aa23e65 1668 (dev_priv->vbt.edp.bpp && dev_priv->vbt.edp.bpp < bpp)) {
56071a20 1669 DRM_DEBUG_KMS("clamping bpp for eDP panel to BIOS-provided %i\n",
6aa23e65
JN
1670 dev_priv->vbt.edp.bpp);
1671 bpp = dev_priv->vbt.edp.bpp;
56071a20
JN
1672 }
1673
344c5bbc
JN
1674 /*
1675 * Use the maximum clock and number of lanes the eDP panel
1676 * advertizes being capable of. The panels are generally
1677 * designed to support only a single clock and lane
1678 * configuration, and typically these values correspond to the
1679 * native resolution of the panel.
1680 */
1681 min_lane_count = max_lane_count;
1682 min_clock = max_clock;
7984211e 1683 }
657445fe 1684
36008365 1685 for (; bpp >= 6*3; bpp -= 2*3) {
241bfc38
DL
1686 mode_rate = intel_dp_link_required(adjusted_mode->crtc_clock,
1687 bpp);
36008365 1688
c6930992 1689 for (clock = min_clock; clock <= max_clock; clock++) {
a8f3ef61
SJ
1690 for (lane_count = min_lane_count;
1691 lane_count <= max_lane_count;
1692 lane_count <<= 1) {
1693
94ca719e 1694 link_clock = common_rates[clock];
36008365
DV
1695 link_avail = intel_dp_max_data_rate(link_clock,
1696 lane_count);
1697
1698 if (mode_rate <= link_avail) {
1699 goto found;
1700 }
1701 }
1702 }
1703 }
c4867936 1704
36008365 1705 return false;
3685a8f3 1706
36008365 1707found:
55bc60db
VS
1708 if (intel_dp->color_range_auto) {
1709 /*
1710 * See:
1711 * CEA-861-E - 5.1 Default Encoding Parameters
1712 * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
1713 */
0f2a2a75
VS
1714 pipe_config->limited_color_range =
1715 bpp != 18 && drm_match_cea_mode(adjusted_mode) > 1;
1716 } else {
1717 pipe_config->limited_color_range =
1718 intel_dp->limited_color_range;
55bc60db
VS
1719 }
1720
90a6b7b0 1721 pipe_config->lane_count = lane_count;
a8f3ef61 1722
657445fe 1723 pipe_config->pipe_bpp = bpp;
94ca719e 1724 pipe_config->port_clock = common_rates[clock];
a4fc5ed6 1725
04a60f9f
VS
1726 intel_dp_compute_rate(intel_dp, pipe_config->port_clock,
1727 &link_bw, &rate_select);
1728
1729 DRM_DEBUG_KMS("DP link bw %02x rate select %02x lane count %d clock %d bpp %d\n",
1730 link_bw, rate_select, pipe_config->lane_count,
ff9a6750 1731 pipe_config->port_clock, bpp);
36008365
DV
1732 DRM_DEBUG_KMS("DP link bw required %i available %i\n",
1733 mode_rate, link_avail);
a4fc5ed6 1734
03afc4a2 1735 intel_link_compute_m_n(bpp, lane_count,
241bfc38
DL
1736 adjusted_mode->crtc_clock,
1737 pipe_config->port_clock,
03afc4a2 1738 &pipe_config->dp_m_n);
9d1a455b 1739
439d7ac0 1740 if (intel_connector->panel.downclock_mode != NULL &&
96178eeb 1741 dev_priv->drrs.type == SEAMLESS_DRRS_SUPPORT) {
f769cd24 1742 pipe_config->has_drrs = true;
439d7ac0
PB
1743 intel_link_compute_m_n(bpp, lane_count,
1744 intel_connector->panel.downclock_mode->clock,
1745 pipe_config->port_clock,
1746 &pipe_config->dp_m2_n2);
1747 }
1748
14d41b3b
VS
1749 /*
1750 * DPLL0 VCO may need to be adjusted to get the correct
1751 * clock for eDP. This will affect cdclk as well.
1752 */
1753 if (is_edp(intel_dp) &&
1754 (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))) {
1755 int vco;
1756
1757 switch (pipe_config->port_clock / 2) {
1758 case 108000:
1759 case 216000:
63911d72 1760 vco = 8640000;
14d41b3b
VS
1761 break;
1762 default:
63911d72 1763 vco = 8100000;
14d41b3b
VS
1764 break;
1765 }
1766
1767 to_intel_atomic_state(pipe_config->base.state)->cdclk_pll_vco = vco;
1768 }
1769
4f8036a2 1770 if (!HAS_DDI(dev_priv))
840b32b7 1771 intel_dp_set_clock(encoder, pipe_config);
c6bb3538 1772
03afc4a2 1773 return true;
a4fc5ed6
KP
1774}
1775
901c2daf 1776void intel_dp_set_link_params(struct intel_dp *intel_dp,
dfa10480
ACO
1777 int link_rate, uint8_t lane_count,
1778 bool link_mst)
901c2daf 1779{
dfa10480
ACO
1780 intel_dp->link_rate = link_rate;
1781 intel_dp->lane_count = lane_count;
1782 intel_dp->link_mst = link_mst;
901c2daf
VS
1783}
1784
85cb48a1
ML
1785static void intel_dp_prepare(struct intel_encoder *encoder,
1786 struct intel_crtc_state *pipe_config)
a4fc5ed6 1787{
b934223d 1788 struct drm_device *dev = encoder->base.dev;
fac5e23e 1789 struct drm_i915_private *dev_priv = to_i915(dev);
b934223d 1790 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
bc7d38a4 1791 enum port port = dp_to_dig_port(intel_dp)->port;
b934223d 1792 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
85cb48a1 1793 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
a4fc5ed6 1794
dfa10480
ACO
1795 intel_dp_set_link_params(intel_dp, pipe_config->port_clock,
1796 pipe_config->lane_count,
1797 intel_crtc_has_type(pipe_config,
1798 INTEL_OUTPUT_DP_MST));
901c2daf 1799
417e822d 1800 /*
1a2eb460 1801 * There are four kinds of DP registers:
417e822d
KP
1802 *
1803 * IBX PCH
1a2eb460
KP
1804 * SNB CPU
1805 * IVB CPU
417e822d
KP
1806 * CPT PCH
1807 *
1808 * IBX PCH and CPU are the same for almost everything,
1809 * except that the CPU DP PLL is configured in this
1810 * register
1811 *
1812 * CPT PCH is quite different, having many bits moved
1813 * to the TRANS_DP_CTL register instead. That
1814 * configuration happens (oddly) in ironlake_pch_enable
1815 */
9c9e7927 1816
417e822d
KP
1817 /* Preserve the BIOS-computed detected bit. This is
1818 * supposed to be read-only.
1819 */
1820 intel_dp->DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
a4fc5ed6 1821
417e822d 1822 /* Handle DP bits in common between all three register formats */
417e822d 1823 intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
85cb48a1 1824 intel_dp->DP |= DP_PORT_WIDTH(pipe_config->lane_count);
a4fc5ed6 1825
417e822d 1826 /* Split out the IBX/CPU vs CPT settings */
32f9d658 1827
5db94019 1828 if (IS_GEN7(dev_priv) && port == PORT_A) {
1a2eb460
KP
1829 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
1830 intel_dp->DP |= DP_SYNC_HS_HIGH;
1831 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
1832 intel_dp->DP |= DP_SYNC_VS_HIGH;
1833 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
1834
6aba5b6c 1835 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
1a2eb460
KP
1836 intel_dp->DP |= DP_ENHANCED_FRAMING;
1837
7c62a164 1838 intel_dp->DP |= crtc->pipe << 29;
6e266956 1839 } else if (HAS_PCH_CPT(dev_priv) && port != PORT_A) {
e3ef4479
VS
1840 u32 trans_dp;
1841
39e5fa88 1842 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
e3ef4479
VS
1843
1844 trans_dp = I915_READ(TRANS_DP_CTL(crtc->pipe));
1845 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
1846 trans_dp |= TRANS_DP_ENH_FRAMING;
1847 else
1848 trans_dp &= ~TRANS_DP_ENH_FRAMING;
1849 I915_WRITE(TRANS_DP_CTL(crtc->pipe), trans_dp);
39e5fa88 1850 } else {
c99f53f7 1851 if (IS_G4X(dev_priv) && pipe_config->limited_color_range)
0f2a2a75 1852 intel_dp->DP |= DP_COLOR_RANGE_16_235;
417e822d
KP
1853
1854 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
1855 intel_dp->DP |= DP_SYNC_HS_HIGH;
1856 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
1857 intel_dp->DP |= DP_SYNC_VS_HIGH;
1858 intel_dp->DP |= DP_LINK_TRAIN_OFF;
1859
6aba5b6c 1860 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
417e822d
KP
1861 intel_dp->DP |= DP_ENHANCED_FRAMING;
1862
920a14b2 1863 if (IS_CHERRYVIEW(dev_priv))
44f37d1f 1864 intel_dp->DP |= DP_PIPE_SELECT_CHV(crtc->pipe);
39e5fa88
VS
1865 else if (crtc->pipe == PIPE_B)
1866 intel_dp->DP |= DP_PIPEB_SELECT;
32f9d658 1867 }
a4fc5ed6
KP
1868}
1869
ffd6749d
PZ
1870#define IDLE_ON_MASK (PP_ON | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
1871#define IDLE_ON_VALUE (PP_ON | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_ON_IDLE)
99ea7127 1872
1a5ef5b7
PZ
1873#define IDLE_OFF_MASK (PP_ON | PP_SEQUENCE_MASK | 0 | 0)
1874#define IDLE_OFF_VALUE (0 | PP_SEQUENCE_NONE | 0 | 0)
99ea7127 1875
ffd6749d
PZ
1876#define IDLE_CYCLE_MASK (PP_ON | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
1877#define IDLE_CYCLE_VALUE (0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
99ea7127 1878
de9c1b6b
ID
1879static void intel_pps_verify_state(struct drm_i915_private *dev_priv,
1880 struct intel_dp *intel_dp);
1881
4be73780 1882static void wait_panel_status(struct intel_dp *intel_dp,
99ea7127
KP
1883 u32 mask,
1884 u32 value)
bd943159 1885{
30add22d 1886 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 1887 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 1888 i915_reg_t pp_stat_reg, pp_ctrl_reg;
453c5420 1889
e39b999a
VS
1890 lockdep_assert_held(&dev_priv->pps_mutex);
1891
de9c1b6b
ID
1892 intel_pps_verify_state(dev_priv, intel_dp);
1893
bf13e81b
JN
1894 pp_stat_reg = _pp_stat_reg(intel_dp);
1895 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
32ce697c 1896
99ea7127 1897 DRM_DEBUG_KMS("mask %08x value %08x status %08x control %08x\n",
453c5420
JB
1898 mask, value,
1899 I915_READ(pp_stat_reg),
1900 I915_READ(pp_ctrl_reg));
32ce697c 1901
9036ff06
CW
1902 if (intel_wait_for_register(dev_priv,
1903 pp_stat_reg, mask, value,
1904 5000))
99ea7127 1905 DRM_ERROR("Panel status timeout: status %08x control %08x\n",
453c5420
JB
1906 I915_READ(pp_stat_reg),
1907 I915_READ(pp_ctrl_reg));
54c136d4
CW
1908
1909 DRM_DEBUG_KMS("Wait complete\n");
99ea7127 1910}
32ce697c 1911
4be73780 1912static void wait_panel_on(struct intel_dp *intel_dp)
99ea7127
KP
1913{
1914 DRM_DEBUG_KMS("Wait for panel power on\n");
4be73780 1915 wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
bd943159
KP
1916}
1917
4be73780 1918static void wait_panel_off(struct intel_dp *intel_dp)
99ea7127
KP
1919{
1920 DRM_DEBUG_KMS("Wait for panel power off time\n");
4be73780 1921 wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
99ea7127
KP
1922}
1923
4be73780 1924static void wait_panel_power_cycle(struct intel_dp *intel_dp)
99ea7127 1925{
d28d4731
AK
1926 ktime_t panel_power_on_time;
1927 s64 panel_power_off_duration;
1928
99ea7127 1929 DRM_DEBUG_KMS("Wait for panel power cycle\n");
dce56b3c 1930
d28d4731
AK
1931 /* take the difference of currrent time and panel power off time
1932 * and then make panel wait for t11_t12 if needed. */
1933 panel_power_on_time = ktime_get_boottime();
1934 panel_power_off_duration = ktime_ms_delta(panel_power_on_time, intel_dp->panel_power_off_time);
1935
dce56b3c
PZ
1936 /* When we disable the VDD override bit last we have to do the manual
1937 * wait. */
d28d4731
AK
1938 if (panel_power_off_duration < (s64)intel_dp->panel_power_cycle_delay)
1939 wait_remaining_ms_from_jiffies(jiffies,
1940 intel_dp->panel_power_cycle_delay - panel_power_off_duration);
dce56b3c 1941
4be73780 1942 wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
99ea7127
KP
1943}
1944
4be73780 1945static void wait_backlight_on(struct intel_dp *intel_dp)
dce56b3c
PZ
1946{
1947 wait_remaining_ms_from_jiffies(intel_dp->last_power_on,
1948 intel_dp->backlight_on_delay);
1949}
1950
4be73780 1951static void edp_wait_backlight_off(struct intel_dp *intel_dp)
dce56b3c
PZ
1952{
1953 wait_remaining_ms_from_jiffies(intel_dp->last_backlight_off,
1954 intel_dp->backlight_off_delay);
1955}
99ea7127 1956
832dd3c1
KP
1957/* Read the current pp_control value, unlocking the register if it
1958 * is locked
1959 */
1960
453c5420 1961static u32 ironlake_get_pp_control(struct intel_dp *intel_dp)
832dd3c1 1962{
453c5420 1963 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 1964 struct drm_i915_private *dev_priv = to_i915(dev);
453c5420 1965 u32 control;
832dd3c1 1966
e39b999a
VS
1967 lockdep_assert_held(&dev_priv->pps_mutex);
1968
bf13e81b 1969 control = I915_READ(_pp_ctrl_reg(intel_dp));
8090ba8c
ID
1970 if (WARN_ON(!HAS_DDI(dev_priv) &&
1971 (control & PANEL_UNLOCK_MASK) != PANEL_UNLOCK_REGS)) {
b0a08bec
VK
1972 control &= ~PANEL_UNLOCK_MASK;
1973 control |= PANEL_UNLOCK_REGS;
1974 }
832dd3c1 1975 return control;
bd943159
KP
1976}
1977
951468f3
VS
1978/*
1979 * Must be paired with edp_panel_vdd_off().
1980 * Must hold pps_mutex around the whole on/off sequence.
1981 * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
1982 */
1e0560e0 1983static bool edp_panel_vdd_on(struct intel_dp *intel_dp)
5d613501 1984{
30add22d 1985 struct drm_device *dev = intel_dp_to_dev(intel_dp);
4e6e1a54
ID
1986 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1987 struct intel_encoder *intel_encoder = &intel_dig_port->base;
fac5e23e 1988 struct drm_i915_private *dev_priv = to_i915(dev);
4e6e1a54 1989 enum intel_display_power_domain power_domain;
5d613501 1990 u32 pp;
f0f59a00 1991 i915_reg_t pp_stat_reg, pp_ctrl_reg;
adddaaf4 1992 bool need_to_disable = !intel_dp->want_panel_vdd;
5d613501 1993
e39b999a
VS
1994 lockdep_assert_held(&dev_priv->pps_mutex);
1995
97af61f5 1996 if (!is_edp(intel_dp))
adddaaf4 1997 return false;
bd943159 1998
2c623c11 1999 cancel_delayed_work(&intel_dp->panel_vdd_work);
bd943159 2000 intel_dp->want_panel_vdd = true;
99ea7127 2001
4be73780 2002 if (edp_have_panel_vdd(intel_dp))
adddaaf4 2003 return need_to_disable;
b0665d57 2004
25f78f58 2005 power_domain = intel_display_port_aux_power_domain(intel_encoder);
4e6e1a54 2006 intel_display_power_get(dev_priv, power_domain);
e9cb81a2 2007
3936fcf4
VS
2008 DRM_DEBUG_KMS("Turning eDP port %c VDD on\n",
2009 port_name(intel_dig_port->port));
bd943159 2010
4be73780
DV
2011 if (!edp_have_panel_power(intel_dp))
2012 wait_panel_power_cycle(intel_dp);
99ea7127 2013
453c5420 2014 pp = ironlake_get_pp_control(intel_dp);
5d613501 2015 pp |= EDP_FORCE_VDD;
ebf33b18 2016
bf13e81b
JN
2017 pp_stat_reg = _pp_stat_reg(intel_dp);
2018 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420
JB
2019
2020 I915_WRITE(pp_ctrl_reg, pp);
2021 POSTING_READ(pp_ctrl_reg);
2022 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
2023 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
ebf33b18
KP
2024 /*
2025 * If the panel wasn't on, delay before accessing aux channel
2026 */
4be73780 2027 if (!edp_have_panel_power(intel_dp)) {
3936fcf4
VS
2028 DRM_DEBUG_KMS("eDP port %c panel power wasn't enabled\n",
2029 port_name(intel_dig_port->port));
f01eca2e 2030 msleep(intel_dp->panel_power_up_delay);
f01eca2e 2031 }
adddaaf4
JN
2032
2033 return need_to_disable;
2034}
2035
951468f3
VS
2036/*
2037 * Must be paired with intel_edp_panel_vdd_off() or
2038 * intel_edp_panel_off().
2039 * Nested calls to these functions are not allowed since
2040 * we drop the lock. Caller must use some higher level
2041 * locking to prevent nested calls from other threads.
2042 */
b80d6c78 2043void intel_edp_panel_vdd_on(struct intel_dp *intel_dp)
adddaaf4 2044{
c695b6b6 2045 bool vdd;
adddaaf4 2046
c695b6b6
VS
2047 if (!is_edp(intel_dp))
2048 return;
2049
773538e8 2050 pps_lock(intel_dp);
c695b6b6 2051 vdd = edp_panel_vdd_on(intel_dp);
773538e8 2052 pps_unlock(intel_dp);
c695b6b6 2053
e2c719b7 2054 I915_STATE_WARN(!vdd, "eDP port %c VDD already requested on\n",
3936fcf4 2055 port_name(dp_to_dig_port(intel_dp)->port));
5d613501
JB
2056}
2057
4be73780 2058static void edp_panel_vdd_off_sync(struct intel_dp *intel_dp)
5d613501 2059{
30add22d 2060 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 2061 struct drm_i915_private *dev_priv = to_i915(dev);
be2c9196
VS
2062 struct intel_digital_port *intel_dig_port =
2063 dp_to_dig_port(intel_dp);
2064 struct intel_encoder *intel_encoder = &intel_dig_port->base;
2065 enum intel_display_power_domain power_domain;
5d613501 2066 u32 pp;
f0f59a00 2067 i915_reg_t pp_stat_reg, pp_ctrl_reg;
5d613501 2068
e39b999a 2069 lockdep_assert_held(&dev_priv->pps_mutex);
a0e99e68 2070
15e899a0 2071 WARN_ON(intel_dp->want_panel_vdd);
4e6e1a54 2072
15e899a0 2073 if (!edp_have_panel_vdd(intel_dp))
be2c9196 2074 return;
b0665d57 2075
3936fcf4
VS
2076 DRM_DEBUG_KMS("Turning eDP port %c VDD off\n",
2077 port_name(intel_dig_port->port));
bd943159 2078
be2c9196
VS
2079 pp = ironlake_get_pp_control(intel_dp);
2080 pp &= ~EDP_FORCE_VDD;
453c5420 2081
be2c9196
VS
2082 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
2083 pp_stat_reg = _pp_stat_reg(intel_dp);
99ea7127 2084
be2c9196
VS
2085 I915_WRITE(pp_ctrl_reg, pp);
2086 POSTING_READ(pp_ctrl_reg);
90791a5c 2087
be2c9196
VS
2088 /* Make sure sequencer is idle before allowing subsequent activity */
2089 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
2090 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
e9cb81a2 2091
5a162e22 2092 if ((pp & PANEL_POWER_ON) == 0)
d28d4731 2093 intel_dp->panel_power_off_time = ktime_get_boottime();
e9cb81a2 2094
25f78f58 2095 power_domain = intel_display_port_aux_power_domain(intel_encoder);
be2c9196 2096 intel_display_power_put(dev_priv, power_domain);
bd943159 2097}
5d613501 2098
4be73780 2099static void edp_panel_vdd_work(struct work_struct *__work)
bd943159
KP
2100{
2101 struct intel_dp *intel_dp = container_of(to_delayed_work(__work),
2102 struct intel_dp, panel_vdd_work);
bd943159 2103
773538e8 2104 pps_lock(intel_dp);
15e899a0
VS
2105 if (!intel_dp->want_panel_vdd)
2106 edp_panel_vdd_off_sync(intel_dp);
773538e8 2107 pps_unlock(intel_dp);
bd943159
KP
2108}
2109
aba86890
ID
2110static void edp_panel_vdd_schedule_off(struct intel_dp *intel_dp)
2111{
2112 unsigned long delay;
2113
2114 /*
2115 * Queue the timer to fire a long time from now (relative to the power
2116 * down delay) to keep the panel power up across a sequence of
2117 * operations.
2118 */
2119 delay = msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5);
2120 schedule_delayed_work(&intel_dp->panel_vdd_work, delay);
2121}
2122
951468f3
VS
2123/*
2124 * Must be paired with edp_panel_vdd_on().
2125 * Must hold pps_mutex around the whole on/off sequence.
2126 * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
2127 */
4be73780 2128static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
bd943159 2129{
fac5e23e 2130 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
e39b999a
VS
2131
2132 lockdep_assert_held(&dev_priv->pps_mutex);
2133
97af61f5
KP
2134 if (!is_edp(intel_dp))
2135 return;
5d613501 2136
e2c719b7 2137 I915_STATE_WARN(!intel_dp->want_panel_vdd, "eDP port %c VDD not forced on",
3936fcf4 2138 port_name(dp_to_dig_port(intel_dp)->port));
f2e8b18a 2139
bd943159
KP
2140 intel_dp->want_panel_vdd = false;
2141
aba86890 2142 if (sync)
4be73780 2143 edp_panel_vdd_off_sync(intel_dp);
aba86890
ID
2144 else
2145 edp_panel_vdd_schedule_off(intel_dp);
5d613501
JB
2146}
2147
9f0fb5be 2148static void edp_panel_on(struct intel_dp *intel_dp)
9934c132 2149{
30add22d 2150 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 2151 struct drm_i915_private *dev_priv = to_i915(dev);
99ea7127 2152 u32 pp;
f0f59a00 2153 i915_reg_t pp_ctrl_reg;
9934c132 2154
9f0fb5be
VS
2155 lockdep_assert_held(&dev_priv->pps_mutex);
2156
97af61f5 2157 if (!is_edp(intel_dp))
bd943159 2158 return;
99ea7127 2159
3936fcf4
VS
2160 DRM_DEBUG_KMS("Turn eDP port %c panel power on\n",
2161 port_name(dp_to_dig_port(intel_dp)->port));
e39b999a 2162
e7a89ace
VS
2163 if (WARN(edp_have_panel_power(intel_dp),
2164 "eDP port %c panel power already on\n",
2165 port_name(dp_to_dig_port(intel_dp)->port)))
9f0fb5be 2166 return;
9934c132 2167
4be73780 2168 wait_panel_power_cycle(intel_dp);
37c6c9b0 2169
bf13e81b 2170 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420 2171 pp = ironlake_get_pp_control(intel_dp);
5db94019 2172 if (IS_GEN5(dev_priv)) {
05ce1a49
KP
2173 /* ILK workaround: disable reset around power sequence */
2174 pp &= ~PANEL_POWER_RESET;
bf13e81b
JN
2175 I915_WRITE(pp_ctrl_reg, pp);
2176 POSTING_READ(pp_ctrl_reg);
05ce1a49 2177 }
37c6c9b0 2178
5a162e22 2179 pp |= PANEL_POWER_ON;
5db94019 2180 if (!IS_GEN5(dev_priv))
99ea7127
KP
2181 pp |= PANEL_POWER_RESET;
2182
453c5420
JB
2183 I915_WRITE(pp_ctrl_reg, pp);
2184 POSTING_READ(pp_ctrl_reg);
9934c132 2185
4be73780 2186 wait_panel_on(intel_dp);
dce56b3c 2187 intel_dp->last_power_on = jiffies;
9934c132 2188
5db94019 2189 if (IS_GEN5(dev_priv)) {
05ce1a49 2190 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
bf13e81b
JN
2191 I915_WRITE(pp_ctrl_reg, pp);
2192 POSTING_READ(pp_ctrl_reg);
05ce1a49 2193 }
9f0fb5be 2194}
e39b999a 2195
9f0fb5be
VS
2196void intel_edp_panel_on(struct intel_dp *intel_dp)
2197{
2198 if (!is_edp(intel_dp))
2199 return;
2200
2201 pps_lock(intel_dp);
2202 edp_panel_on(intel_dp);
773538e8 2203 pps_unlock(intel_dp);
9934c132
JB
2204}
2205
9f0fb5be
VS
2206
2207static void edp_panel_off(struct intel_dp *intel_dp)
9934c132 2208{
4e6e1a54
ID
2209 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2210 struct intel_encoder *intel_encoder = &intel_dig_port->base;
30add22d 2211 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 2212 struct drm_i915_private *dev_priv = to_i915(dev);
4e6e1a54 2213 enum intel_display_power_domain power_domain;
99ea7127 2214 u32 pp;
f0f59a00 2215 i915_reg_t pp_ctrl_reg;
9934c132 2216
9f0fb5be
VS
2217 lockdep_assert_held(&dev_priv->pps_mutex);
2218
97af61f5
KP
2219 if (!is_edp(intel_dp))
2220 return;
37c6c9b0 2221
3936fcf4
VS
2222 DRM_DEBUG_KMS("Turn eDP port %c panel power off\n",
2223 port_name(dp_to_dig_port(intel_dp)->port));
37c6c9b0 2224
3936fcf4
VS
2225 WARN(!intel_dp->want_panel_vdd, "Need eDP port %c VDD to turn off panel\n",
2226 port_name(dp_to_dig_port(intel_dp)->port));
24f3e092 2227
453c5420 2228 pp = ironlake_get_pp_control(intel_dp);
35a38556
DV
2229 /* We need to switch off panel power _and_ force vdd, for otherwise some
2230 * panels get very unhappy and cease to work. */
5a162e22 2231 pp &= ~(PANEL_POWER_ON | PANEL_POWER_RESET | EDP_FORCE_VDD |
b3064154 2232 EDP_BLC_ENABLE);
453c5420 2233
bf13e81b 2234 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420 2235
849e39f5
PZ
2236 intel_dp->want_panel_vdd = false;
2237
453c5420
JB
2238 I915_WRITE(pp_ctrl_reg, pp);
2239 POSTING_READ(pp_ctrl_reg);
9934c132 2240
d28d4731 2241 intel_dp->panel_power_off_time = ktime_get_boottime();
4be73780 2242 wait_panel_off(intel_dp);
849e39f5
PZ
2243
2244 /* We got a reference when we enabled the VDD. */
25f78f58 2245 power_domain = intel_display_port_aux_power_domain(intel_encoder);
4e6e1a54 2246 intel_display_power_put(dev_priv, power_domain);
9f0fb5be 2247}
e39b999a 2248
9f0fb5be
VS
2249void intel_edp_panel_off(struct intel_dp *intel_dp)
2250{
2251 if (!is_edp(intel_dp))
2252 return;
e39b999a 2253
9f0fb5be
VS
2254 pps_lock(intel_dp);
2255 edp_panel_off(intel_dp);
773538e8 2256 pps_unlock(intel_dp);
9934c132
JB
2257}
2258
1250d107
JN
2259/* Enable backlight in the panel power control. */
2260static void _intel_edp_backlight_on(struct intel_dp *intel_dp)
32f9d658 2261{
da63a9f2
PZ
2262 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2263 struct drm_device *dev = intel_dig_port->base.base.dev;
fac5e23e 2264 struct drm_i915_private *dev_priv = to_i915(dev);
32f9d658 2265 u32 pp;
f0f59a00 2266 i915_reg_t pp_ctrl_reg;
32f9d658 2267
01cb9ea6
JB
2268 /*
2269 * If we enable the backlight right away following a panel power
2270 * on, we may see slight flicker as the panel syncs with the eDP
2271 * link. So delay a bit to make sure the image is solid before
2272 * allowing it to appear.
2273 */
4be73780 2274 wait_backlight_on(intel_dp);
e39b999a 2275
773538e8 2276 pps_lock(intel_dp);
e39b999a 2277
453c5420 2278 pp = ironlake_get_pp_control(intel_dp);
32f9d658 2279 pp |= EDP_BLC_ENABLE;
453c5420 2280
bf13e81b 2281 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420
JB
2282
2283 I915_WRITE(pp_ctrl_reg, pp);
2284 POSTING_READ(pp_ctrl_reg);
e39b999a 2285
773538e8 2286 pps_unlock(intel_dp);
32f9d658
ZW
2287}
2288
1250d107
JN
2289/* Enable backlight PWM and backlight PP control. */
2290void intel_edp_backlight_on(struct intel_dp *intel_dp)
2291{
2292 if (!is_edp(intel_dp))
2293 return;
2294
2295 DRM_DEBUG_KMS("\n");
2296
2297 intel_panel_enable_backlight(intel_dp->attached_connector);
2298 _intel_edp_backlight_on(intel_dp);
2299}
2300
2301/* Disable backlight in the panel power control. */
2302static void _intel_edp_backlight_off(struct intel_dp *intel_dp)
32f9d658 2303{
30add22d 2304 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 2305 struct drm_i915_private *dev_priv = to_i915(dev);
32f9d658 2306 u32 pp;
f0f59a00 2307 i915_reg_t pp_ctrl_reg;
32f9d658 2308
f01eca2e
KP
2309 if (!is_edp(intel_dp))
2310 return;
2311
773538e8 2312 pps_lock(intel_dp);
e39b999a 2313
453c5420 2314 pp = ironlake_get_pp_control(intel_dp);
32f9d658 2315 pp &= ~EDP_BLC_ENABLE;
453c5420 2316
bf13e81b 2317 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420
JB
2318
2319 I915_WRITE(pp_ctrl_reg, pp);
2320 POSTING_READ(pp_ctrl_reg);
f7d2323c 2321
773538e8 2322 pps_unlock(intel_dp);
e39b999a
VS
2323
2324 intel_dp->last_backlight_off = jiffies;
f7d2323c 2325 edp_wait_backlight_off(intel_dp);
1250d107 2326}
f7d2323c 2327
1250d107
JN
2328/* Disable backlight PP control and backlight PWM. */
2329void intel_edp_backlight_off(struct intel_dp *intel_dp)
2330{
2331 if (!is_edp(intel_dp))
2332 return;
2333
2334 DRM_DEBUG_KMS("\n");
f7d2323c 2335
1250d107 2336 _intel_edp_backlight_off(intel_dp);
f7d2323c 2337 intel_panel_disable_backlight(intel_dp->attached_connector);
32f9d658 2338}
a4fc5ed6 2339
73580fb7
JN
2340/*
2341 * Hook for controlling the panel power control backlight through the bl_power
2342 * sysfs attribute. Take care to handle multiple calls.
2343 */
2344static void intel_edp_backlight_power(struct intel_connector *connector,
2345 bool enable)
2346{
2347 struct intel_dp *intel_dp = intel_attached_dp(&connector->base);
e39b999a
VS
2348 bool is_enabled;
2349
773538e8 2350 pps_lock(intel_dp);
e39b999a 2351 is_enabled = ironlake_get_pp_control(intel_dp) & EDP_BLC_ENABLE;
773538e8 2352 pps_unlock(intel_dp);
73580fb7
JN
2353
2354 if (is_enabled == enable)
2355 return;
2356
23ba9373
JN
2357 DRM_DEBUG_KMS("panel power control backlight %s\n",
2358 enable ? "enable" : "disable");
73580fb7
JN
2359
2360 if (enable)
2361 _intel_edp_backlight_on(intel_dp);
2362 else
2363 _intel_edp_backlight_off(intel_dp);
2364}
2365
64e1077a
VS
2366static void assert_dp_port(struct intel_dp *intel_dp, bool state)
2367{
2368 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2369 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
2370 bool cur_state = I915_READ(intel_dp->output_reg) & DP_PORT_EN;
2371
2372 I915_STATE_WARN(cur_state != state,
2373 "DP port %c state assertion failure (expected %s, current %s)\n",
2374 port_name(dig_port->port),
87ad3212 2375 onoff(state), onoff(cur_state));
64e1077a
VS
2376}
2377#define assert_dp_port_disabled(d) assert_dp_port((d), false)
2378
2379static void assert_edp_pll(struct drm_i915_private *dev_priv, bool state)
2380{
2381 bool cur_state = I915_READ(DP_A) & DP_PLL_ENABLE;
2382
2383 I915_STATE_WARN(cur_state != state,
2384 "eDP PLL state assertion failure (expected %s, current %s)\n",
87ad3212 2385 onoff(state), onoff(cur_state));
64e1077a
VS
2386}
2387#define assert_edp_pll_enabled(d) assert_edp_pll((d), true)
2388#define assert_edp_pll_disabled(d) assert_edp_pll((d), false)
2389
85cb48a1
ML
2390static void ironlake_edp_pll_on(struct intel_dp *intel_dp,
2391 struct intel_crtc_state *pipe_config)
d240f20f 2392{
85cb48a1 2393 struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
64e1077a 2394 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d240f20f 2395
64e1077a
VS
2396 assert_pipe_disabled(dev_priv, crtc->pipe);
2397 assert_dp_port_disabled(intel_dp);
2398 assert_edp_pll_disabled(dev_priv);
2bd2ad64 2399
abfce949 2400 DRM_DEBUG_KMS("enabling eDP PLL for clock %d\n",
85cb48a1 2401 pipe_config->port_clock);
abfce949
VS
2402
2403 intel_dp->DP &= ~DP_PLL_FREQ_MASK;
2404
85cb48a1 2405 if (pipe_config->port_clock == 162000)
abfce949
VS
2406 intel_dp->DP |= DP_PLL_FREQ_162MHZ;
2407 else
2408 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
2409
2410 I915_WRITE(DP_A, intel_dp->DP);
2411 POSTING_READ(DP_A);
2412 udelay(500);
2413
6b23f3e8
VS
2414 /*
2415 * [DevILK] Work around required when enabling DP PLL
2416 * while a pipe is enabled going to FDI:
2417 * 1. Wait for the start of vertical blank on the enabled pipe going to FDI
2418 * 2. Program DP PLL enable
2419 */
2420 if (IS_GEN5(dev_priv))
0f0f74bc 2421 intel_wait_for_vblank_if_active(dev_priv, !crtc->pipe);
6b23f3e8 2422
0767935e 2423 intel_dp->DP |= DP_PLL_ENABLE;
6fec7662 2424
0767935e 2425 I915_WRITE(DP_A, intel_dp->DP);
298b0b39
JB
2426 POSTING_READ(DP_A);
2427 udelay(200);
d240f20f
JB
2428}
2429
2bd2ad64 2430static void ironlake_edp_pll_off(struct intel_dp *intel_dp)
d240f20f 2431{
da63a9f2 2432 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
64e1077a
VS
2433 struct intel_crtc *crtc = to_intel_crtc(intel_dig_port->base.base.crtc);
2434 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d240f20f 2435
64e1077a
VS
2436 assert_pipe_disabled(dev_priv, crtc->pipe);
2437 assert_dp_port_disabled(intel_dp);
2438 assert_edp_pll_enabled(dev_priv);
2bd2ad64 2439
abfce949
VS
2440 DRM_DEBUG_KMS("disabling eDP PLL\n");
2441
6fec7662 2442 intel_dp->DP &= ~DP_PLL_ENABLE;
0767935e 2443
6fec7662 2444 I915_WRITE(DP_A, intel_dp->DP);
1af5fa1b 2445 POSTING_READ(DP_A);
d240f20f
JB
2446 udelay(200);
2447}
2448
c7ad3810 2449/* If the sink supports it, try to set the power state appropriately */
c19b0669 2450void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
c7ad3810
JB
2451{
2452 int ret, i;
2453
2454 /* Should have a valid DPCD by this point */
2455 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
2456 return;
2457
2458 if (mode != DRM_MODE_DPMS_ON) {
9d1a1031
JN
2459 ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
2460 DP_SET_POWER_D3);
c7ad3810 2461 } else {
357c0ae9
ID
2462 struct intel_lspcon *lspcon = dp_to_lspcon(intel_dp);
2463
c7ad3810
JB
2464 /*
2465 * When turning on, we need to retry for 1ms to give the sink
2466 * time to wake up.
2467 */
2468 for (i = 0; i < 3; i++) {
9d1a1031
JN
2469 ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
2470 DP_SET_POWER_D0);
c7ad3810
JB
2471 if (ret == 1)
2472 break;
2473 msleep(1);
2474 }
357c0ae9
ID
2475
2476 if (ret == 1 && lspcon->active)
2477 lspcon_wait_pcon_mode(lspcon);
c7ad3810 2478 }
f9cac721
JN
2479
2480 if (ret != 1)
2481 DRM_DEBUG_KMS("failed to %s sink power state\n",
2482 mode == DRM_MODE_DPMS_ON ? "enable" : "disable");
c7ad3810
JB
2483}
2484
19d8fe15
DV
2485static bool intel_dp_get_hw_state(struct intel_encoder *encoder,
2486 enum pipe *pipe)
d240f20f 2487{
19d8fe15 2488 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
bc7d38a4 2489 enum port port = dp_to_dig_port(intel_dp)->port;
19d8fe15 2490 struct drm_device *dev = encoder->base.dev;
fac5e23e 2491 struct drm_i915_private *dev_priv = to_i915(dev);
6d129bea
ID
2492 enum intel_display_power_domain power_domain;
2493 u32 tmp;
6fa9a5ec 2494 bool ret;
6d129bea
ID
2495
2496 power_domain = intel_display_port_power_domain(encoder);
6fa9a5ec 2497 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
6d129bea
ID
2498 return false;
2499
6fa9a5ec
ID
2500 ret = false;
2501
6d129bea 2502 tmp = I915_READ(intel_dp->output_reg);
19d8fe15
DV
2503
2504 if (!(tmp & DP_PORT_EN))
6fa9a5ec 2505 goto out;
19d8fe15 2506
5db94019 2507 if (IS_GEN7(dev_priv) && port == PORT_A) {
19d8fe15 2508 *pipe = PORT_TO_PIPE_CPT(tmp);
6e266956 2509 } else if (HAS_PCH_CPT(dev_priv) && port != PORT_A) {
adc289d7 2510 enum pipe p;
19d8fe15 2511
adc289d7
VS
2512 for_each_pipe(dev_priv, p) {
2513 u32 trans_dp = I915_READ(TRANS_DP_CTL(p));
2514 if (TRANS_DP_PIPE_TO_PORT(trans_dp) == port) {
2515 *pipe = p;
6fa9a5ec
ID
2516 ret = true;
2517
2518 goto out;
19d8fe15
DV
2519 }
2520 }
19d8fe15 2521
4a0833ec 2522 DRM_DEBUG_KMS("No pipe for dp port 0x%x found\n",
f0f59a00 2523 i915_mmio_reg_offset(intel_dp->output_reg));
920a14b2 2524 } else if (IS_CHERRYVIEW(dev_priv)) {
39e5fa88
VS
2525 *pipe = DP_PORT_TO_PIPE_CHV(tmp);
2526 } else {
2527 *pipe = PORT_TO_PIPE(tmp);
4a0833ec 2528 }
d240f20f 2529
6fa9a5ec
ID
2530 ret = true;
2531
2532out:
2533 intel_display_power_put(dev_priv, power_domain);
2534
2535 return ret;
19d8fe15 2536}
d240f20f 2537
045ac3b5 2538static void intel_dp_get_config(struct intel_encoder *encoder,
5cec258b 2539 struct intel_crtc_state *pipe_config)
045ac3b5
JB
2540{
2541 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
045ac3b5 2542 u32 tmp, flags = 0;
63000ef6 2543 struct drm_device *dev = encoder->base.dev;
fac5e23e 2544 struct drm_i915_private *dev_priv = to_i915(dev);
63000ef6
XZ
2545 enum port port = dp_to_dig_port(intel_dp)->port;
2546 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
045ac3b5 2547
9ed109a7 2548 tmp = I915_READ(intel_dp->output_reg);
9fcb1704
JN
2549
2550 pipe_config->has_audio = tmp & DP_AUDIO_OUTPUT_ENABLE && port != PORT_A;
9ed109a7 2551
6e266956 2552 if (HAS_PCH_CPT(dev_priv) && port != PORT_A) {
b81e34c2
VS
2553 u32 trans_dp = I915_READ(TRANS_DP_CTL(crtc->pipe));
2554
2555 if (trans_dp & TRANS_DP_HSYNC_ACTIVE_HIGH)
63000ef6
XZ
2556 flags |= DRM_MODE_FLAG_PHSYNC;
2557 else
2558 flags |= DRM_MODE_FLAG_NHSYNC;
045ac3b5 2559
b81e34c2 2560 if (trans_dp & TRANS_DP_VSYNC_ACTIVE_HIGH)
63000ef6
XZ
2561 flags |= DRM_MODE_FLAG_PVSYNC;
2562 else
2563 flags |= DRM_MODE_FLAG_NVSYNC;
2564 } else {
39e5fa88 2565 if (tmp & DP_SYNC_HS_HIGH)
63000ef6
XZ
2566 flags |= DRM_MODE_FLAG_PHSYNC;
2567 else
2568 flags |= DRM_MODE_FLAG_NHSYNC;
045ac3b5 2569
39e5fa88 2570 if (tmp & DP_SYNC_VS_HIGH)
63000ef6
XZ
2571 flags |= DRM_MODE_FLAG_PVSYNC;
2572 else
2573 flags |= DRM_MODE_FLAG_NVSYNC;
2574 }
045ac3b5 2575
2d112de7 2576 pipe_config->base.adjusted_mode.flags |= flags;
f1f644dc 2577
c99f53f7 2578 if (IS_G4X(dev_priv) && tmp & DP_COLOR_RANGE_16_235)
8c875fca
VS
2579 pipe_config->limited_color_range = true;
2580
90a6b7b0
VS
2581 pipe_config->lane_count =
2582 ((tmp & DP_PORT_WIDTH_MASK) >> DP_PORT_WIDTH_SHIFT) + 1;
2583
eb14cb74
VS
2584 intel_dp_get_m_n(crtc, pipe_config);
2585
18442d08 2586 if (port == PORT_A) {
b377e0df 2587 if ((I915_READ(DP_A) & DP_PLL_FREQ_MASK) == DP_PLL_FREQ_162MHZ)
f1f644dc
JB
2588 pipe_config->port_clock = 162000;
2589 else
2590 pipe_config->port_clock = 270000;
2591 }
18442d08 2592
e3b247da
VS
2593 pipe_config->base.adjusted_mode.crtc_clock =
2594 intel_dotclock_calculate(pipe_config->port_clock,
2595 &pipe_config->dp_m_n);
7f16e5c1 2596
6aa23e65
JN
2597 if (is_edp(intel_dp) && dev_priv->vbt.edp.bpp &&
2598 pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
c6cd2ee2
JN
2599 /*
2600 * This is a big fat ugly hack.
2601 *
2602 * Some machines in UEFI boot mode provide us a VBT that has 18
2603 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
2604 * unknown we fail to light up. Yet the same BIOS boots up with
2605 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
2606 * max, not what it tells us to use.
2607 *
2608 * Note: This will still be broken if the eDP panel is not lit
2609 * up by the BIOS, and thus we can't get the mode at module
2610 * load.
2611 */
2612 DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
6aa23e65
JN
2613 pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
2614 dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
c6cd2ee2 2615 }
045ac3b5
JB
2616}
2617
fd6bbda9
ML
2618static void intel_disable_dp(struct intel_encoder *encoder,
2619 struct intel_crtc_state *old_crtc_state,
2620 struct drm_connector_state *old_conn_state)
d240f20f 2621{
e8cb4558 2622 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
85cb48a1 2623 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
495a5bb8 2624
85cb48a1 2625 if (old_crtc_state->has_audio)
495a5bb8 2626 intel_audio_codec_disable(encoder);
6cb49835 2627
85cb48a1 2628 if (HAS_PSR(dev_priv) && !HAS_DDI(dev_priv))
b32c6f48
RV
2629 intel_psr_disable(intel_dp);
2630
6cb49835
DV
2631 /* Make sure the panel is off before trying to change the mode. But also
2632 * ensure that we have vdd while we switch off the panel. */
24f3e092 2633 intel_edp_panel_vdd_on(intel_dp);
4be73780 2634 intel_edp_backlight_off(intel_dp);
fdbc3b1f 2635 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
4be73780 2636 intel_edp_panel_off(intel_dp);
3739850b 2637
08aff3fe 2638 /* disable the port before the pipe on g4x */
85cb48a1 2639 if (INTEL_GEN(dev_priv) < 5)
3739850b 2640 intel_dp_link_down(intel_dp);
d240f20f
JB
2641}
2642
fd6bbda9
ML
2643static void ilk_post_disable_dp(struct intel_encoder *encoder,
2644 struct intel_crtc_state *old_crtc_state,
2645 struct drm_connector_state *old_conn_state)
d240f20f 2646{
2bd2ad64 2647 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
982a3866 2648 enum port port = dp_to_dig_port(intel_dp)->port;
2bd2ad64 2649
49277c31 2650 intel_dp_link_down(intel_dp);
abfce949
VS
2651
2652 /* Only ilk+ has port A */
08aff3fe
VS
2653 if (port == PORT_A)
2654 ironlake_edp_pll_off(intel_dp);
49277c31
VS
2655}
2656
fd6bbda9
ML
2657static void vlv_post_disable_dp(struct intel_encoder *encoder,
2658 struct intel_crtc_state *old_crtc_state,
2659 struct drm_connector_state *old_conn_state)
49277c31
VS
2660{
2661 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2662
2663 intel_dp_link_down(intel_dp);
2bd2ad64
DV
2664}
2665
fd6bbda9
ML
2666static void chv_post_disable_dp(struct intel_encoder *encoder,
2667 struct intel_crtc_state *old_crtc_state,
2668 struct drm_connector_state *old_conn_state)
a8f327fb
VS
2669{
2670 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2671 struct drm_device *dev = encoder->base.dev;
fac5e23e 2672 struct drm_i915_private *dev_priv = to_i915(dev);
97fd4d5c 2673
a8f327fb
VS
2674 intel_dp_link_down(intel_dp);
2675
2676 mutex_lock(&dev_priv->sb_lock);
2677
2678 /* Assert data lane reset */
2679 chv_data_lane_soft_reset(encoder, true);
580d3811 2680
a580516d 2681 mutex_unlock(&dev_priv->sb_lock);
580d3811
VS
2682}
2683
7b13b58a
VS
2684static void
2685_intel_dp_set_link_train(struct intel_dp *intel_dp,
2686 uint32_t *DP,
2687 uint8_t dp_train_pat)
2688{
2689 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2690 struct drm_device *dev = intel_dig_port->base.base.dev;
fac5e23e 2691 struct drm_i915_private *dev_priv = to_i915(dev);
7b13b58a
VS
2692 enum port port = intel_dig_port->port;
2693
8b0878a0
PD
2694 if (dp_train_pat & DP_TRAINING_PATTERN_MASK)
2695 DRM_DEBUG_KMS("Using DP training pattern TPS%d\n",
2696 dp_train_pat & DP_TRAINING_PATTERN_MASK);
2697
4f8036a2 2698 if (HAS_DDI(dev_priv)) {
7b13b58a
VS
2699 uint32_t temp = I915_READ(DP_TP_CTL(port));
2700
2701 if (dp_train_pat & DP_LINK_SCRAMBLING_DISABLE)
2702 temp |= DP_TP_CTL_SCRAMBLE_DISABLE;
2703 else
2704 temp &= ~DP_TP_CTL_SCRAMBLE_DISABLE;
2705
2706 temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
2707 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2708 case DP_TRAINING_PATTERN_DISABLE:
2709 temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
2710
2711 break;
2712 case DP_TRAINING_PATTERN_1:
2713 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
2714 break;
2715 case DP_TRAINING_PATTERN_2:
2716 temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
2717 break;
2718 case DP_TRAINING_PATTERN_3:
2719 temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
2720 break;
2721 }
2722 I915_WRITE(DP_TP_CTL(port), temp);
2723
5db94019 2724 } else if ((IS_GEN7(dev_priv) && port == PORT_A) ||
6e266956 2725 (HAS_PCH_CPT(dev_priv) && port != PORT_A)) {
7b13b58a
VS
2726 *DP &= ~DP_LINK_TRAIN_MASK_CPT;
2727
2728 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2729 case DP_TRAINING_PATTERN_DISABLE:
2730 *DP |= DP_LINK_TRAIN_OFF_CPT;
2731 break;
2732 case DP_TRAINING_PATTERN_1:
2733 *DP |= DP_LINK_TRAIN_PAT_1_CPT;
2734 break;
2735 case DP_TRAINING_PATTERN_2:
2736 *DP |= DP_LINK_TRAIN_PAT_2_CPT;
2737 break;
2738 case DP_TRAINING_PATTERN_3:
8b0878a0 2739 DRM_DEBUG_KMS("TPS3 not supported, using TPS2 instead\n");
7b13b58a
VS
2740 *DP |= DP_LINK_TRAIN_PAT_2_CPT;
2741 break;
2742 }
2743
2744 } else {
920a14b2 2745 if (IS_CHERRYVIEW(dev_priv))
7b13b58a
VS
2746 *DP &= ~DP_LINK_TRAIN_MASK_CHV;
2747 else
2748 *DP &= ~DP_LINK_TRAIN_MASK;
2749
2750 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2751 case DP_TRAINING_PATTERN_DISABLE:
2752 *DP |= DP_LINK_TRAIN_OFF;
2753 break;
2754 case DP_TRAINING_PATTERN_1:
2755 *DP |= DP_LINK_TRAIN_PAT_1;
2756 break;
2757 case DP_TRAINING_PATTERN_2:
2758 *DP |= DP_LINK_TRAIN_PAT_2;
2759 break;
2760 case DP_TRAINING_PATTERN_3:
920a14b2 2761 if (IS_CHERRYVIEW(dev_priv)) {
7b13b58a
VS
2762 *DP |= DP_LINK_TRAIN_PAT_3_CHV;
2763 } else {
8b0878a0 2764 DRM_DEBUG_KMS("TPS3 not supported, using TPS2 instead\n");
7b13b58a
VS
2765 *DP |= DP_LINK_TRAIN_PAT_2;
2766 }
2767 break;
2768 }
2769 }
2770}
2771
85cb48a1
ML
2772static void intel_dp_enable_port(struct intel_dp *intel_dp,
2773 struct intel_crtc_state *old_crtc_state)
7b13b58a
VS
2774{
2775 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 2776 struct drm_i915_private *dev_priv = to_i915(dev);
7b13b58a 2777
7b13b58a 2778 /* enable with pattern 1 (as per spec) */
7b13b58a 2779
8b0878a0 2780 intel_dp_program_link_training_pattern(intel_dp, DP_TRAINING_PATTERN_1);
7b713f50
VS
2781
2782 /*
2783 * Magic for VLV/CHV. We _must_ first set up the register
2784 * without actually enabling the port, and then do another
2785 * write to enable the port. Otherwise link training will
2786 * fail when the power sequencer is freshly used for this port.
2787 */
2788 intel_dp->DP |= DP_PORT_EN;
85cb48a1 2789 if (old_crtc_state->has_audio)
6fec7662 2790 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
7b713f50
VS
2791
2792 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
2793 POSTING_READ(intel_dp->output_reg);
580d3811
VS
2794}
2795
85cb48a1 2796static void intel_enable_dp(struct intel_encoder *encoder,
bbf35e9d
ML
2797 struct intel_crtc_state *pipe_config,
2798 struct drm_connector_state *conn_state)
d240f20f 2799{
e8cb4558
DV
2800 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2801 struct drm_device *dev = encoder->base.dev;
fac5e23e 2802 struct drm_i915_private *dev_priv = to_i915(dev);
c1dec79a 2803 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
e8cb4558 2804 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
d6fbdd15 2805 enum pipe pipe = crtc->pipe;
5d613501 2806
0c33d8d7
DV
2807 if (WARN_ON(dp_reg & DP_PORT_EN))
2808 return;
5d613501 2809
093e3f13
VS
2810 pps_lock(intel_dp);
2811
920a14b2 2812 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
093e3f13
VS
2813 vlv_init_panel_power_sequencer(intel_dp);
2814
85cb48a1 2815 intel_dp_enable_port(intel_dp, pipe_config);
093e3f13
VS
2816
2817 edp_panel_vdd_on(intel_dp);
2818 edp_panel_on(intel_dp);
2819 edp_panel_vdd_off(intel_dp, true);
2820
2821 pps_unlock(intel_dp);
2822
920a14b2 2823 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
e0fce78f
VS
2824 unsigned int lane_mask = 0x0;
2825
920a14b2 2826 if (IS_CHERRYVIEW(dev_priv))
85cb48a1 2827 lane_mask = intel_dp_unused_lane_mask(pipe_config->lane_count);
e0fce78f 2828
9b6de0a1
VS
2829 vlv_wait_port_ready(dev_priv, dp_to_dig_port(intel_dp),
2830 lane_mask);
e0fce78f 2831 }
61234fa5 2832
f01eca2e 2833 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
33a34e4e 2834 intel_dp_start_link_train(intel_dp);
3ab9c637 2835 intel_dp_stop_link_train(intel_dp);
c1dec79a 2836
85cb48a1 2837 if (pipe_config->has_audio) {
c1dec79a 2838 DRM_DEBUG_DRIVER("Enabling DP audio on pipe %c\n",
d6fbdd15 2839 pipe_name(pipe));
bbf35e9d 2840 intel_audio_codec_enable(encoder, pipe_config, conn_state);
c1dec79a 2841 }
ab1f90f9 2842}
89b667f8 2843
fd6bbda9
ML
2844static void g4x_enable_dp(struct intel_encoder *encoder,
2845 struct intel_crtc_state *pipe_config,
2846 struct drm_connector_state *conn_state)
ecff4f3b 2847{
828f5c6e
JN
2848 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2849
bbf35e9d 2850 intel_enable_dp(encoder, pipe_config, conn_state);
4be73780 2851 intel_edp_backlight_on(intel_dp);
ab1f90f9 2852}
89b667f8 2853
fd6bbda9
ML
2854static void vlv_enable_dp(struct intel_encoder *encoder,
2855 struct intel_crtc_state *pipe_config,
2856 struct drm_connector_state *conn_state)
ab1f90f9 2857{
828f5c6e
JN
2858 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2859
4be73780 2860 intel_edp_backlight_on(intel_dp);
b32c6f48 2861 intel_psr_enable(intel_dp);
d240f20f
JB
2862}
2863
fd6bbda9
ML
2864static void g4x_pre_enable_dp(struct intel_encoder *encoder,
2865 struct intel_crtc_state *pipe_config,
2866 struct drm_connector_state *conn_state)
ab1f90f9
JN
2867{
2868 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
d6fbdd15 2869 enum port port = dp_to_dig_port(intel_dp)->port;
ab1f90f9 2870
85cb48a1 2871 intel_dp_prepare(encoder, pipe_config);
8ac33ed3 2872
d41f1efb 2873 /* Only ilk+ has port A */
abfce949 2874 if (port == PORT_A)
85cb48a1 2875 ironlake_edp_pll_on(intel_dp, pipe_config);
ab1f90f9
JN
2876}
2877
83b84597
VS
2878static void vlv_detach_power_sequencer(struct intel_dp *intel_dp)
2879{
2880 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
fac5e23e 2881 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
83b84597 2882 enum pipe pipe = intel_dp->pps_pipe;
44cb734c 2883 i915_reg_t pp_on_reg = PP_ON_DELAYS(pipe);
83b84597 2884
9f2bdb00
VS
2885 WARN_ON(intel_dp->active_pipe != INVALID_PIPE);
2886
83b84597
VS
2887 edp_panel_vdd_off_sync(intel_dp);
2888
2889 /*
2890 * VLV seems to get confused when multiple power seqeuencers
2891 * have the same port selected (even if only one has power/vdd
2892 * enabled). The failure manifests as vlv_wait_port_ready() failing
2893 * CHV on the other hand doesn't seem to mind having the same port
2894 * selected in multiple power seqeuencers, but let's clear the
2895 * port select always when logically disconnecting a power sequencer
2896 * from a port.
2897 */
2898 DRM_DEBUG_KMS("detaching pipe %c power sequencer from port %c\n",
2899 pipe_name(pipe), port_name(intel_dig_port->port));
2900 I915_WRITE(pp_on_reg, 0);
2901 POSTING_READ(pp_on_reg);
2902
2903 intel_dp->pps_pipe = INVALID_PIPE;
2904}
2905
a4a5d2f8
VS
2906static void vlv_steal_power_sequencer(struct drm_device *dev,
2907 enum pipe pipe)
2908{
fac5e23e 2909 struct drm_i915_private *dev_priv = to_i915(dev);
a4a5d2f8
VS
2910 struct intel_encoder *encoder;
2911
2912 lockdep_assert_held(&dev_priv->pps_mutex);
2913
ac3c12e4
VS
2914 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
2915 return;
2916
19c8054c 2917 for_each_intel_encoder(dev, encoder) {
a4a5d2f8 2918 struct intel_dp *intel_dp;
773538e8 2919 enum port port;
a4a5d2f8 2920
9f2bdb00
VS
2921 if (encoder->type != INTEL_OUTPUT_DP &&
2922 encoder->type != INTEL_OUTPUT_EDP)
a4a5d2f8
VS
2923 continue;
2924
2925 intel_dp = enc_to_intel_dp(&encoder->base);
773538e8 2926 port = dp_to_dig_port(intel_dp)->port;
a4a5d2f8 2927
9f2bdb00
VS
2928 WARN(intel_dp->active_pipe == pipe,
2929 "stealing pipe %c power sequencer from active (e)DP port %c\n",
2930 pipe_name(pipe), port_name(port));
2931
a4a5d2f8
VS
2932 if (intel_dp->pps_pipe != pipe)
2933 continue;
2934
2935 DRM_DEBUG_KMS("stealing pipe %c power sequencer from port %c\n",
773538e8 2936 pipe_name(pipe), port_name(port));
a4a5d2f8
VS
2937
2938 /* make sure vdd is off before we steal it */
83b84597 2939 vlv_detach_power_sequencer(intel_dp);
a4a5d2f8
VS
2940 }
2941}
2942
2943static void vlv_init_panel_power_sequencer(struct intel_dp *intel_dp)
2944{
2945 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2946 struct intel_encoder *encoder = &intel_dig_port->base;
2947 struct drm_device *dev = encoder->base.dev;
fac5e23e 2948 struct drm_i915_private *dev_priv = to_i915(dev);
a4a5d2f8 2949 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
a4a5d2f8
VS
2950
2951 lockdep_assert_held(&dev_priv->pps_mutex);
2952
9f2bdb00 2953 WARN_ON(intel_dp->active_pipe != INVALID_PIPE);
093e3f13 2954
9f2bdb00
VS
2955 if (intel_dp->pps_pipe != INVALID_PIPE &&
2956 intel_dp->pps_pipe != crtc->pipe) {
2957 /*
2958 * If another power sequencer was being used on this
2959 * port previously make sure to turn off vdd there while
2960 * we still have control of it.
2961 */
83b84597 2962 vlv_detach_power_sequencer(intel_dp);
9f2bdb00 2963 }
a4a5d2f8
VS
2964
2965 /*
2966 * We may be stealing the power
2967 * sequencer from another port.
2968 */
2969 vlv_steal_power_sequencer(dev, crtc->pipe);
2970
9f2bdb00
VS
2971 intel_dp->active_pipe = crtc->pipe;
2972
2973 if (!is_edp(intel_dp))
2974 return;
2975
a4a5d2f8
VS
2976 /* now it's all ours */
2977 intel_dp->pps_pipe = crtc->pipe;
2978
2979 DRM_DEBUG_KMS("initializing pipe %c power sequencer for port %c\n",
2980 pipe_name(intel_dp->pps_pipe), port_name(intel_dig_port->port));
2981
2982 /* init power sequencer on this pipe and port */
36b5f425
VS
2983 intel_dp_init_panel_power_sequencer(dev, intel_dp);
2984 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
a4a5d2f8
VS
2985}
2986
fd6bbda9
ML
2987static void vlv_pre_enable_dp(struct intel_encoder *encoder,
2988 struct intel_crtc_state *pipe_config,
2989 struct drm_connector_state *conn_state)
a4fc5ed6 2990{
5f68c275 2991 vlv_phy_pre_encoder_enable(encoder);
ab1f90f9 2992
bbf35e9d 2993 intel_enable_dp(encoder, pipe_config, conn_state);
89b667f8
JB
2994}
2995
fd6bbda9
ML
2996static void vlv_dp_pre_pll_enable(struct intel_encoder *encoder,
2997 struct intel_crtc_state *pipe_config,
2998 struct drm_connector_state *conn_state)
89b667f8 2999{
85cb48a1 3000 intel_dp_prepare(encoder, pipe_config);
8ac33ed3 3001
6da2e616 3002 vlv_phy_pre_pll_enable(encoder);
a4fc5ed6
KP
3003}
3004
fd6bbda9
ML
3005static void chv_pre_enable_dp(struct intel_encoder *encoder,
3006 struct intel_crtc_state *pipe_config,
3007 struct drm_connector_state *conn_state)
e4a1d846 3008{
e7d2a717 3009 chv_phy_pre_encoder_enable(encoder);
e4a1d846 3010
bbf35e9d 3011 intel_enable_dp(encoder, pipe_config, conn_state);
b0b33846
VS
3012
3013 /* Second common lane will stay alive on its own now */
e7d2a717 3014 chv_phy_release_cl2_override(encoder);
e4a1d846
CML
3015}
3016
fd6bbda9
ML
3017static void chv_dp_pre_pll_enable(struct intel_encoder *encoder,
3018 struct intel_crtc_state *pipe_config,
3019 struct drm_connector_state *conn_state)
9197c88b 3020{
85cb48a1 3021 intel_dp_prepare(encoder, pipe_config);
625695f8 3022
419b1b7a 3023 chv_phy_pre_pll_enable(encoder);
9197c88b
VS
3024}
3025
fd6bbda9
ML
3026static void chv_dp_post_pll_disable(struct intel_encoder *encoder,
3027 struct intel_crtc_state *pipe_config,
3028 struct drm_connector_state *conn_state)
d6db995f 3029{
204970b5 3030 chv_phy_post_pll_disable(encoder);
d6db995f
VS
3031}
3032
a4fc5ed6
KP
3033/*
3034 * Fetch AUX CH registers 0x202 - 0x207 which contain
3035 * link status information
3036 */
94223d04 3037bool
93f62dad 3038intel_dp_get_link_status(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
a4fc5ed6 3039{
9f085ebb
L
3040 return drm_dp_dpcd_read(&intel_dp->aux, DP_LANE0_1_STATUS, link_status,
3041 DP_LINK_STATUS_SIZE) == DP_LINK_STATUS_SIZE;
a4fc5ed6
KP
3042}
3043
1100244e 3044/* These are source-specific values. */
94223d04 3045uint8_t
1a2eb460 3046intel_dp_voltage_max(struct intel_dp *intel_dp)
a4fc5ed6 3047{
dd11bc10 3048 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
bc7d38a4 3049 enum port port = dp_to_dig_port(intel_dp)->port;
1a2eb460 3050
cc3f90f0 3051 if (IS_GEN9_LP(dev_priv))
9314726b 3052 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
dd11bc10 3053 else if (INTEL_GEN(dev_priv) >= 9) {
06411f08 3054 if (dev_priv->vbt.edp.low_vswing && port == PORT_A)
7ad14a29 3055 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
5a9d1f1a 3056 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
920a14b2 3057 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
bd60018a 3058 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
5db94019 3059 else if (IS_GEN7(dev_priv) && port == PORT_A)
bd60018a 3060 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
6e266956 3061 else if (HAS_PCH_CPT(dev_priv) && port != PORT_A)
bd60018a 3062 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
1a2eb460 3063 else
bd60018a 3064 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
1a2eb460
KP
3065}
3066
94223d04 3067uint8_t
1a2eb460
KP
3068intel_dp_pre_emphasis_max(struct intel_dp *intel_dp, uint8_t voltage_swing)
3069{
8652744b 3070 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
bc7d38a4 3071 enum port port = dp_to_dig_port(intel_dp)->port;
1a2eb460 3072
8652744b 3073 if (INTEL_GEN(dev_priv) >= 9) {
5a9d1f1a
DL
3074 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
3075 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3076 return DP_TRAIN_PRE_EMPH_LEVEL_3;
3077 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3078 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3079 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3080 return DP_TRAIN_PRE_EMPH_LEVEL_1;
7ad14a29
SJ
3081 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
3082 return DP_TRAIN_PRE_EMPH_LEVEL_0;
5a9d1f1a
DL
3083 default:
3084 return DP_TRAIN_PRE_EMPH_LEVEL_0;
3085 }
8652744b 3086 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
d6c0d722 3087 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a
SJ
3088 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3089 return DP_TRAIN_PRE_EMPH_LEVEL_3;
3090 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3091 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3092 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3093 return DP_TRAIN_PRE_EMPH_LEVEL_1;
3094 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
d6c0d722 3095 default:
bd60018a 3096 return DP_TRAIN_PRE_EMPH_LEVEL_0;
d6c0d722 3097 }
8652744b 3098 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
e2fa6fba 3099 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a
SJ
3100 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3101 return DP_TRAIN_PRE_EMPH_LEVEL_3;
3102 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3103 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3104 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3105 return DP_TRAIN_PRE_EMPH_LEVEL_1;
3106 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
e2fa6fba 3107 default:
bd60018a 3108 return DP_TRAIN_PRE_EMPH_LEVEL_0;
e2fa6fba 3109 }
8652744b 3110 } else if (IS_GEN7(dev_priv) && port == PORT_A) {
1a2eb460 3111 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a
SJ
3112 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3113 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3114 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3115 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3116 return DP_TRAIN_PRE_EMPH_LEVEL_1;
1a2eb460 3117 default:
bd60018a 3118 return DP_TRAIN_PRE_EMPH_LEVEL_0;
1a2eb460
KP
3119 }
3120 } else {
3121 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a
SJ
3122 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3123 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3124 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3125 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3126 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3127 return DP_TRAIN_PRE_EMPH_LEVEL_1;
3128 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
1a2eb460 3129 default:
bd60018a 3130 return DP_TRAIN_PRE_EMPH_LEVEL_0;
1a2eb460 3131 }
a4fc5ed6
KP
3132 }
3133}
3134
5829975c 3135static uint32_t vlv_signal_levels(struct intel_dp *intel_dp)
e2fa6fba 3136{
53d98725 3137 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
e2fa6fba
P
3138 unsigned long demph_reg_value, preemph_reg_value,
3139 uniqtranscale_reg_value;
3140 uint8_t train_set = intel_dp->train_set[0];
e2fa6fba
P
3141
3142 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
bd60018a 3143 case DP_TRAIN_PRE_EMPH_LEVEL_0:
e2fa6fba
P
3144 preemph_reg_value = 0x0004000;
3145 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3146 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e2fa6fba
P
3147 demph_reg_value = 0x2B405555;
3148 uniqtranscale_reg_value = 0x552AB83A;
3149 break;
bd60018a 3150 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e2fa6fba
P
3151 demph_reg_value = 0x2B404040;
3152 uniqtranscale_reg_value = 0x5548B83A;
3153 break;
bd60018a 3154 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
e2fa6fba
P
3155 demph_reg_value = 0x2B245555;
3156 uniqtranscale_reg_value = 0x5560B83A;
3157 break;
bd60018a 3158 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
e2fa6fba
P
3159 demph_reg_value = 0x2B405555;
3160 uniqtranscale_reg_value = 0x5598DA3A;
3161 break;
3162 default:
3163 return 0;
3164 }
3165 break;
bd60018a 3166 case DP_TRAIN_PRE_EMPH_LEVEL_1:
e2fa6fba
P
3167 preemph_reg_value = 0x0002000;
3168 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3169 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e2fa6fba
P
3170 demph_reg_value = 0x2B404040;
3171 uniqtranscale_reg_value = 0x5552B83A;
3172 break;
bd60018a 3173 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e2fa6fba
P
3174 demph_reg_value = 0x2B404848;
3175 uniqtranscale_reg_value = 0x5580B83A;
3176 break;
bd60018a 3177 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
e2fa6fba
P
3178 demph_reg_value = 0x2B404040;
3179 uniqtranscale_reg_value = 0x55ADDA3A;
3180 break;
3181 default:
3182 return 0;
3183 }
3184 break;
bd60018a 3185 case DP_TRAIN_PRE_EMPH_LEVEL_2:
e2fa6fba
P
3186 preemph_reg_value = 0x0000000;
3187 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3188 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e2fa6fba
P
3189 demph_reg_value = 0x2B305555;
3190 uniqtranscale_reg_value = 0x5570B83A;
3191 break;
bd60018a 3192 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e2fa6fba
P
3193 demph_reg_value = 0x2B2B4040;
3194 uniqtranscale_reg_value = 0x55ADDA3A;
3195 break;
3196 default:
3197 return 0;
3198 }
3199 break;
bd60018a 3200 case DP_TRAIN_PRE_EMPH_LEVEL_3:
e2fa6fba
P
3201 preemph_reg_value = 0x0006000;
3202 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3203 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e2fa6fba
P
3204 demph_reg_value = 0x1B405555;
3205 uniqtranscale_reg_value = 0x55ADDA3A;
3206 break;
3207 default:
3208 return 0;
3209 }
3210 break;
3211 default:
3212 return 0;
3213 }
3214
53d98725
ACO
3215 vlv_set_phy_signal_level(encoder, demph_reg_value, preemph_reg_value,
3216 uniqtranscale_reg_value, 0);
e2fa6fba
P
3217
3218 return 0;
3219}
3220
5829975c 3221static uint32_t chv_signal_levels(struct intel_dp *intel_dp)
e4a1d846 3222{
b7fa22d8
ACO
3223 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3224 u32 deemph_reg_value, margin_reg_value;
3225 bool uniq_trans_scale = false;
e4a1d846 3226 uint8_t train_set = intel_dp->train_set[0];
e4a1d846
CML
3227
3228 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
bd60018a 3229 case DP_TRAIN_PRE_EMPH_LEVEL_0:
e4a1d846 3230 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3231 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e4a1d846
CML
3232 deemph_reg_value = 128;
3233 margin_reg_value = 52;
3234 break;
bd60018a 3235 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e4a1d846
CML
3236 deemph_reg_value = 128;
3237 margin_reg_value = 77;
3238 break;
bd60018a 3239 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
e4a1d846
CML
3240 deemph_reg_value = 128;
3241 margin_reg_value = 102;
3242 break;
bd60018a 3243 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
e4a1d846
CML
3244 deemph_reg_value = 128;
3245 margin_reg_value = 154;
b7fa22d8 3246 uniq_trans_scale = true;
e4a1d846
CML
3247 break;
3248 default:
3249 return 0;
3250 }
3251 break;
bd60018a 3252 case DP_TRAIN_PRE_EMPH_LEVEL_1:
e4a1d846 3253 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3254 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e4a1d846
CML
3255 deemph_reg_value = 85;
3256 margin_reg_value = 78;
3257 break;
bd60018a 3258 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e4a1d846
CML
3259 deemph_reg_value = 85;
3260 margin_reg_value = 116;
3261 break;
bd60018a 3262 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
e4a1d846
CML
3263 deemph_reg_value = 85;
3264 margin_reg_value = 154;
3265 break;
3266 default:
3267 return 0;
3268 }
3269 break;
bd60018a 3270 case DP_TRAIN_PRE_EMPH_LEVEL_2:
e4a1d846 3271 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3272 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e4a1d846
CML
3273 deemph_reg_value = 64;
3274 margin_reg_value = 104;
3275 break;
bd60018a 3276 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
e4a1d846
CML
3277 deemph_reg_value = 64;
3278 margin_reg_value = 154;
3279 break;
3280 default:
3281 return 0;
3282 }
3283 break;
bd60018a 3284 case DP_TRAIN_PRE_EMPH_LEVEL_3:
e4a1d846 3285 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3286 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
e4a1d846
CML
3287 deemph_reg_value = 43;
3288 margin_reg_value = 154;
3289 break;
3290 default:
3291 return 0;
3292 }
3293 break;
3294 default:
3295 return 0;
3296 }
3297
b7fa22d8
ACO
3298 chv_set_phy_signal_level(encoder, deemph_reg_value,
3299 margin_reg_value, uniq_trans_scale);
e4a1d846
CML
3300
3301 return 0;
3302}
3303
a4fc5ed6 3304static uint32_t
5829975c 3305gen4_signal_levels(uint8_t train_set)
a4fc5ed6 3306{
3cf2efb1 3307 uint32_t signal_levels = 0;
a4fc5ed6 3308
3cf2efb1 3309 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
bd60018a 3310 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
a4fc5ed6
KP
3311 default:
3312 signal_levels |= DP_VOLTAGE_0_4;
3313 break;
bd60018a 3314 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
a4fc5ed6
KP
3315 signal_levels |= DP_VOLTAGE_0_6;
3316 break;
bd60018a 3317 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
a4fc5ed6
KP
3318 signal_levels |= DP_VOLTAGE_0_8;
3319 break;
bd60018a 3320 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
a4fc5ed6
KP
3321 signal_levels |= DP_VOLTAGE_1_2;
3322 break;
3323 }
3cf2efb1 3324 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
bd60018a 3325 case DP_TRAIN_PRE_EMPH_LEVEL_0:
a4fc5ed6
KP
3326 default:
3327 signal_levels |= DP_PRE_EMPHASIS_0;
3328 break;
bd60018a 3329 case DP_TRAIN_PRE_EMPH_LEVEL_1:
a4fc5ed6
KP
3330 signal_levels |= DP_PRE_EMPHASIS_3_5;
3331 break;
bd60018a 3332 case DP_TRAIN_PRE_EMPH_LEVEL_2:
a4fc5ed6
KP
3333 signal_levels |= DP_PRE_EMPHASIS_6;
3334 break;
bd60018a 3335 case DP_TRAIN_PRE_EMPH_LEVEL_3:
a4fc5ed6
KP
3336 signal_levels |= DP_PRE_EMPHASIS_9_5;
3337 break;
3338 }
3339 return signal_levels;
3340}
3341
e3421a18
ZW
3342/* Gen6's DP voltage swing and pre-emphasis control */
3343static uint32_t
5829975c 3344gen6_edp_signal_levels(uint8_t train_set)
e3421a18 3345{
3c5a62b5
YL
3346 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
3347 DP_TRAIN_PRE_EMPHASIS_MASK);
3348 switch (signal_levels) {
bd60018a
SJ
3349 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3350 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3c5a62b5 3351 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
bd60018a 3352 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
3c5a62b5 3353 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
bd60018a
SJ
3354 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
3355 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2:
3c5a62b5 3356 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
bd60018a
SJ
3357 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
3358 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
3c5a62b5 3359 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
bd60018a
SJ
3360 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3361 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3c5a62b5 3362 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
e3421a18 3363 default:
3c5a62b5
YL
3364 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
3365 "0x%x\n", signal_levels);
3366 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
e3421a18
ZW
3367 }
3368}
3369
1a2eb460
KP
3370/* Gen7's DP voltage swing and pre-emphasis control */
3371static uint32_t
5829975c 3372gen7_edp_signal_levels(uint8_t train_set)
1a2eb460
KP
3373{
3374 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
3375 DP_TRAIN_PRE_EMPHASIS_MASK);
3376 switch (signal_levels) {
bd60018a 3377 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
1a2eb460 3378 return EDP_LINK_TRAIN_400MV_0DB_IVB;
bd60018a 3379 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
1a2eb460 3380 return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
bd60018a 3381 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
1a2eb460
KP
3382 return EDP_LINK_TRAIN_400MV_6DB_IVB;
3383
bd60018a 3384 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
1a2eb460 3385 return EDP_LINK_TRAIN_600MV_0DB_IVB;
bd60018a 3386 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
1a2eb460
KP
3387 return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
3388
bd60018a 3389 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
1a2eb460 3390 return EDP_LINK_TRAIN_800MV_0DB_IVB;
bd60018a 3391 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
1a2eb460
KP
3392 return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
3393
3394 default:
3395 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
3396 "0x%x\n", signal_levels);
3397 return EDP_LINK_TRAIN_500MV_0DB_IVB;
3398 }
3399}
3400
94223d04 3401void
f4eb692e 3402intel_dp_set_signal_levels(struct intel_dp *intel_dp)
f0a3424e
PZ
3403{
3404 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
bc7d38a4 3405 enum port port = intel_dig_port->port;
f0a3424e 3406 struct drm_device *dev = intel_dig_port->base.base.dev;
b905a915 3407 struct drm_i915_private *dev_priv = to_i915(dev);
f8896f5d 3408 uint32_t signal_levels, mask = 0;
f0a3424e
PZ
3409 uint8_t train_set = intel_dp->train_set[0];
3410
4f8036a2 3411 if (HAS_DDI(dev_priv)) {
f8896f5d
DW
3412 signal_levels = ddi_signal_levels(intel_dp);
3413
e2d214ae 3414 if (IS_BROXTON(dev_priv))
f8896f5d
DW
3415 signal_levels = 0;
3416 else
3417 mask = DDI_BUF_EMP_MASK;
920a14b2 3418 } else if (IS_CHERRYVIEW(dev_priv)) {
5829975c 3419 signal_levels = chv_signal_levels(intel_dp);
11a914c2 3420 } else if (IS_VALLEYVIEW(dev_priv)) {
5829975c 3421 signal_levels = vlv_signal_levels(intel_dp);
5db94019 3422 } else if (IS_GEN7(dev_priv) && port == PORT_A) {
5829975c 3423 signal_levels = gen7_edp_signal_levels(train_set);
f0a3424e 3424 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_IVB;
5db94019 3425 } else if (IS_GEN6(dev_priv) && port == PORT_A) {
5829975c 3426 signal_levels = gen6_edp_signal_levels(train_set);
f0a3424e
PZ
3427 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_SNB;
3428 } else {
5829975c 3429 signal_levels = gen4_signal_levels(train_set);
f0a3424e
PZ
3430 mask = DP_VOLTAGE_MASK | DP_PRE_EMPHASIS_MASK;
3431 }
3432
96fb9f9b
VK
3433 if (mask)
3434 DRM_DEBUG_KMS("Using signal levels %08x\n", signal_levels);
3435
3436 DRM_DEBUG_KMS("Using vswing level %d\n",
3437 train_set & DP_TRAIN_VOLTAGE_SWING_MASK);
3438 DRM_DEBUG_KMS("Using pre-emphasis level %d\n",
3439 (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) >>
3440 DP_TRAIN_PRE_EMPHASIS_SHIFT);
f0a3424e 3441
f4eb692e 3442 intel_dp->DP = (intel_dp->DP & ~mask) | signal_levels;
b905a915
ACO
3443
3444 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
3445 POSTING_READ(intel_dp->output_reg);
f0a3424e
PZ
3446}
3447
94223d04 3448void
e9c176d5
ACO
3449intel_dp_program_link_training_pattern(struct intel_dp *intel_dp,
3450 uint8_t dp_train_pat)
a4fc5ed6 3451{
174edf1f 3452 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
90a6b7b0
VS
3453 struct drm_i915_private *dev_priv =
3454 to_i915(intel_dig_port->base.base.dev);
a4fc5ed6 3455
f4eb692e 3456 _intel_dp_set_link_train(intel_dp, &intel_dp->DP, dp_train_pat);
47ea7542 3457
f4eb692e 3458 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
ea5b213a 3459 POSTING_READ(intel_dp->output_reg);
e9c176d5
ACO
3460}
3461
94223d04 3462void intel_dp_set_idle_link_train(struct intel_dp *intel_dp)
3ab9c637
ID
3463{
3464 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3465 struct drm_device *dev = intel_dig_port->base.base.dev;
fac5e23e 3466 struct drm_i915_private *dev_priv = to_i915(dev);
3ab9c637
ID
3467 enum port port = intel_dig_port->port;
3468 uint32_t val;
3469
4f8036a2 3470 if (!HAS_DDI(dev_priv))
3ab9c637
ID
3471 return;
3472
3473 val = I915_READ(DP_TP_CTL(port));
3474 val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
3475 val |= DP_TP_CTL_LINK_TRAIN_IDLE;
3476 I915_WRITE(DP_TP_CTL(port), val);
3477
3478 /*
3479 * On PORT_A we can have only eDP in SST mode. There the only reason
3480 * we need to set idle transmission mode is to work around a HW issue
3481 * where we enable the pipe while not in idle link-training mode.
3482 * In this case there is requirement to wait for a minimum number of
3483 * idle patterns to be sent.
3484 */
3485 if (port == PORT_A)
3486 return;
3487
a767017f
CW
3488 if (intel_wait_for_register(dev_priv,DP_TP_STATUS(port),
3489 DP_TP_STATUS_IDLE_DONE,
3490 DP_TP_STATUS_IDLE_DONE,
3491 1))
3ab9c637
ID
3492 DRM_ERROR("Timed out waiting for DP idle patterns\n");
3493}
3494
a4fc5ed6 3495static void
ea5b213a 3496intel_dp_link_down(struct intel_dp *intel_dp)
a4fc5ed6 3497{
da63a9f2 3498 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1612c8bd 3499 struct intel_crtc *crtc = to_intel_crtc(intel_dig_port->base.base.crtc);
bc7d38a4 3500 enum port port = intel_dig_port->port;
da63a9f2 3501 struct drm_device *dev = intel_dig_port->base.base.dev;
fac5e23e 3502 struct drm_i915_private *dev_priv = to_i915(dev);
ea5b213a 3503 uint32_t DP = intel_dp->DP;
a4fc5ed6 3504
4f8036a2 3505 if (WARN_ON(HAS_DDI(dev_priv)))
c19b0669
PZ
3506 return;
3507
0c33d8d7 3508 if (WARN_ON((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0))
1b39d6f3
CW
3509 return;
3510
28c97730 3511 DRM_DEBUG_KMS("\n");
32f9d658 3512
5db94019 3513 if ((IS_GEN7(dev_priv) && port == PORT_A) ||
6e266956 3514 (HAS_PCH_CPT(dev_priv) && port != PORT_A)) {
e3421a18 3515 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1612c8bd 3516 DP |= DP_LINK_TRAIN_PAT_IDLE_CPT;
e3421a18 3517 } else {
920a14b2 3518 if (IS_CHERRYVIEW(dev_priv))
aad3d14d
VS
3519 DP &= ~DP_LINK_TRAIN_MASK_CHV;
3520 else
3521 DP &= ~DP_LINK_TRAIN_MASK;
1612c8bd 3522 DP |= DP_LINK_TRAIN_PAT_IDLE;
e3421a18 3523 }
1612c8bd 3524 I915_WRITE(intel_dp->output_reg, DP);
fe255d00 3525 POSTING_READ(intel_dp->output_reg);
5eb08b69 3526
1612c8bd
VS
3527 DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
3528 I915_WRITE(intel_dp->output_reg, DP);
3529 POSTING_READ(intel_dp->output_reg);
3530
3531 /*
3532 * HW workaround for IBX, we need to move the port
3533 * to transcoder A after disabling it to allow the
3534 * matching HDMI port to be enabled on transcoder A.
3535 */
6e266956 3536 if (HAS_PCH_IBX(dev_priv) && crtc->pipe == PIPE_B && port != PORT_A) {
0c241d5b
VS
3537 /*
3538 * We get CPU/PCH FIFO underruns on the other pipe when
3539 * doing the workaround. Sweep them under the rug.
3540 */
3541 intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, false);
3542 intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
3543
1612c8bd
VS
3544 /* always enable with pattern 1 (as per spec) */
3545 DP &= ~(DP_PIPEB_SELECT | DP_LINK_TRAIN_MASK);
3546 DP |= DP_PORT_EN | DP_LINK_TRAIN_PAT_1;
3547 I915_WRITE(intel_dp->output_reg, DP);
3548 POSTING_READ(intel_dp->output_reg);
3549
3550 DP &= ~DP_PORT_EN;
5bddd17f 3551 I915_WRITE(intel_dp->output_reg, DP);
0ca09685 3552 POSTING_READ(intel_dp->output_reg);
0c241d5b 3553
0f0f74bc 3554 intel_wait_for_vblank_if_active(dev_priv, PIPE_A);
0c241d5b
VS
3555 intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, true);
3556 intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
5bddd17f
EA
3557 }
3558
f01eca2e 3559 msleep(intel_dp->panel_power_down_delay);
6fec7662
VS
3560
3561 intel_dp->DP = DP;
9f2bdb00
VS
3562
3563 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3564 pps_lock(intel_dp);
3565 intel_dp->active_pipe = INVALID_PIPE;
3566 pps_unlock(intel_dp);
3567 }
a4fc5ed6
KP
3568}
3569
24e807e7 3570bool
fe5a66f9 3571intel_dp_read_dpcd(struct intel_dp *intel_dp)
92fd8fd1 3572{
9f085ebb
L
3573 if (drm_dp_dpcd_read(&intel_dp->aux, 0x000, intel_dp->dpcd,
3574 sizeof(intel_dp->dpcd)) < 0)
edb39244 3575 return false; /* aux transfer failed */
92fd8fd1 3576
a8e98153 3577 DRM_DEBUG_KMS("DPCD: %*ph\n", (int) sizeof(intel_dp->dpcd), intel_dp->dpcd);
577c7a50 3578
fe5a66f9
VS
3579 return intel_dp->dpcd[DP_DPCD_REV] != 0;
3580}
edb39244 3581
fe5a66f9
VS
3582static bool
3583intel_edp_init_dpcd(struct intel_dp *intel_dp)
3584{
3585 struct drm_i915_private *dev_priv =
3586 to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
30d9aa42 3587
fe5a66f9
VS
3588 /* this function is meant to be called only once */
3589 WARN_ON(intel_dp->dpcd[DP_DPCD_REV] != 0);
30d9aa42 3590
fe5a66f9 3591 if (!intel_dp_read_dpcd(intel_dp))
30d9aa42
SS
3592 return false;
3593
12a47a42
ID
3594 intel_dp_read_desc(intel_dp);
3595
fe5a66f9
VS
3596 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
3597 dev_priv->no_aux_handshake = intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
3598 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
474d1ec4 3599
fe5a66f9
VS
3600 /* Check if the panel supports PSR */
3601 drm_dp_dpcd_read(&intel_dp->aux, DP_PSR_SUPPORT,
3602 intel_dp->psr_dpcd,
3603 sizeof(intel_dp->psr_dpcd));
3604 if (intel_dp->psr_dpcd[0] & DP_PSR_IS_SUPPORTED) {
3605 dev_priv->psr.sink_support = true;
3606 DRM_DEBUG_KMS("Detected EDP PSR Panel.\n");
3607 }
86ee27b5 3608
fe5a66f9
VS
3609 if (INTEL_GEN(dev_priv) >= 9 &&
3610 (intel_dp->psr_dpcd[0] & DP_PSR2_IS_SUPPORTED)) {
3611 uint8_t frame_sync_cap;
3612
3613 dev_priv->psr.sink_support = true;
3614 drm_dp_dpcd_read(&intel_dp->aux,
3615 DP_SINK_DEVICE_AUX_FRAME_SYNC_CAP,
3616 &frame_sync_cap, 1);
3617 dev_priv->psr.aux_frame_sync = frame_sync_cap ? true : false;
3618 /* PSR2 needs frame sync as well */
3619 dev_priv->psr.psr2_support = dev_priv->psr.aux_frame_sync;
3620 DRM_DEBUG_KMS("PSR2 %s on sink",
3621 dev_priv->psr.psr2_support ? "supported" : "not supported");
50003939
JN
3622 }
3623
fe5a66f9
VS
3624 /* Read the eDP Display control capabilities registers */
3625 if ((intel_dp->dpcd[DP_EDP_CONFIGURATION_CAP] & DP_DPCD_DISPLAY_CONTROL_CAPABLE) &&
3626 drm_dp_dpcd_read(&intel_dp->aux, DP_EDP_DPCD_REV,
f7170e2e
DC
3627 intel_dp->edp_dpcd, sizeof(intel_dp->edp_dpcd)) ==
3628 sizeof(intel_dp->edp_dpcd))
fe5a66f9
VS
3629 DRM_DEBUG_KMS("EDP DPCD : %*ph\n", (int) sizeof(intel_dp->edp_dpcd),
3630 intel_dp->edp_dpcd);
06ea66b6 3631
fc0f8e25 3632 /* Intermediate frequency support */
fe5a66f9 3633 if (intel_dp->edp_dpcd[0] >= 0x03) { /* eDp v1.4 or higher */
94ca719e 3634 __le16 sink_rates[DP_MAX_SUPPORTED_RATES];
ea2d8a42
VS
3635 int i;
3636
9f085ebb
L
3637 drm_dp_dpcd_read(&intel_dp->aux, DP_SUPPORTED_LINK_RATES,
3638 sink_rates, sizeof(sink_rates));
ea2d8a42 3639
94ca719e
VS
3640 for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
3641 int val = le16_to_cpu(sink_rates[i]);
ea2d8a42
VS
3642
3643 if (val == 0)
3644 break;
3645
fd81c44e
DP
3646 /* Value read multiplied by 200kHz gives the per-lane
3647 * link rate in kHz. The source rates are, however,
3648 * stored in terms of LS_Clk kHz. The full conversion
3649 * back to symbols is
3650 * (val * 200kHz)*(8/10 ch. encoding)*(1/8 bit to Byte)
3651 */
af77b974 3652 intel_dp->sink_rates[i] = (val * 200) / 10;
ea2d8a42 3653 }
94ca719e 3654 intel_dp->num_sink_rates = i;
fc0f8e25 3655 }
0336400e 3656
fe5a66f9
VS
3657 return true;
3658}
3659
3660
3661static bool
3662intel_dp_get_dpcd(struct intel_dp *intel_dp)
3663{
3664 if (!intel_dp_read_dpcd(intel_dp))
3665 return false;
3666
3667 if (drm_dp_dpcd_read(&intel_dp->aux, DP_SINK_COUNT,
3668 &intel_dp->sink_count, 1) < 0)
3669 return false;
3670
3671 /*
3672 * Sink count can change between short pulse hpd hence
3673 * a member variable in intel_dp will track any changes
3674 * between short pulse interrupts.
3675 */
3676 intel_dp->sink_count = DP_GET_SINK_COUNT(intel_dp->sink_count);
3677
3678 /*
3679 * SINK_COUNT == 0 and DOWNSTREAM_PORT_PRESENT == 1 implies that
3680 * a dongle is present but no display. Unless we require to know
3681 * if a dongle is present or not, we don't need to update
3682 * downstream port information. So, an early return here saves
3683 * time from performing other operations which are not required.
3684 */
3685 if (!is_edp(intel_dp) && !intel_dp->sink_count)
3686 return false;
0336400e 3687
c726ad01 3688 if (!drm_dp_is_branch(intel_dp->dpcd))
edb39244
AJ
3689 return true; /* native DP sink */
3690
3691 if (intel_dp->dpcd[DP_DPCD_REV] == 0x10)
3692 return true; /* no per-port downstream info */
3693
9f085ebb
L
3694 if (drm_dp_dpcd_read(&intel_dp->aux, DP_DOWNSTREAM_PORT_0,
3695 intel_dp->downstream_ports,
3696 DP_MAX_DOWNSTREAM_PORTS) < 0)
edb39244
AJ
3697 return false; /* downstream port status fetch failed */
3698
3699 return true;
92fd8fd1
KP
3700}
3701
0e32b39c 3702static bool
c4e3170a 3703intel_dp_can_mst(struct intel_dp *intel_dp)
0e32b39c
DA
3704{
3705 u8 buf[1];
3706
7cc96139
NS
3707 if (!i915.enable_dp_mst)
3708 return false;
3709
0e32b39c
DA
3710 if (!intel_dp->can_mst)
3711 return false;
3712
3713 if (intel_dp->dpcd[DP_DPCD_REV] < 0x12)
3714 return false;
3715
c4e3170a
VS
3716 if (drm_dp_dpcd_read(&intel_dp->aux, DP_MSTM_CAP, buf, 1) != 1)
3717 return false;
0e32b39c 3718
c4e3170a
VS
3719 return buf[0] & DP_MST_CAP;
3720}
3721
3722static void
3723intel_dp_configure_mst(struct intel_dp *intel_dp)
3724{
3725 if (!i915.enable_dp_mst)
3726 return;
3727
3728 if (!intel_dp->can_mst)
3729 return;
3730
3731 intel_dp->is_mst = intel_dp_can_mst(intel_dp);
3732
3733 if (intel_dp->is_mst)
3734 DRM_DEBUG_KMS("Sink is MST capable\n");
3735 else
3736 DRM_DEBUG_KMS("Sink is not MST capable\n");
3737
3738 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
3739 intel_dp->is_mst);
0e32b39c
DA
3740}
3741
e5a1cab5 3742static int intel_dp_sink_crc_stop(struct intel_dp *intel_dp)
d2e216d0 3743{
082dcc7c 3744 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
0f0f74bc 3745 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
082dcc7c 3746 struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
ad9dc91b 3747 u8 buf;
e5a1cab5 3748 int ret = 0;
c6297843
RV
3749 int count = 0;
3750 int attempts = 10;
d2e216d0 3751
082dcc7c
RV
3752 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0) {
3753 DRM_DEBUG_KMS("Sink CRC couldn't be stopped properly\n");
e5a1cab5
RV
3754 ret = -EIO;
3755 goto out;
4373f0f2
PZ
3756 }
3757
082dcc7c 3758 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
e5a1cab5 3759 buf & ~DP_TEST_SINK_START) < 0) {
082dcc7c 3760 DRM_DEBUG_KMS("Sink CRC couldn't be stopped properly\n");
e5a1cab5
RV
3761 ret = -EIO;
3762 goto out;
3763 }
d2e216d0 3764
c6297843 3765 do {
0f0f74bc 3766 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
c6297843
RV
3767
3768 if (drm_dp_dpcd_readb(&intel_dp->aux,
3769 DP_TEST_SINK_MISC, &buf) < 0) {
3770 ret = -EIO;
3771 goto out;
3772 }
3773 count = buf & DP_TEST_COUNT_MASK;
3774 } while (--attempts && count);
3775
3776 if (attempts == 0) {
dc5a9037 3777 DRM_DEBUG_KMS("TIMEOUT: Sink CRC counter is not zeroed after calculation is stopped\n");
c6297843
RV
3778 ret = -ETIMEDOUT;
3779 }
3780
e5a1cab5 3781 out:
082dcc7c 3782 hsw_enable_ips(intel_crtc);
e5a1cab5 3783 return ret;
082dcc7c
RV
3784}
3785
3786static int intel_dp_sink_crc_start(struct intel_dp *intel_dp)
3787{
3788 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
0f0f74bc 3789 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
082dcc7c
RV
3790 struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
3791 u8 buf;
e5a1cab5
RV
3792 int ret;
3793
082dcc7c
RV
3794 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK_MISC, &buf) < 0)
3795 return -EIO;
3796
3797 if (!(buf & DP_TEST_CRC_SUPPORTED))
3798 return -ENOTTY;
3799
3800 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0)
3801 return -EIO;
3802
6d8175da
RV
3803 if (buf & DP_TEST_SINK_START) {
3804 ret = intel_dp_sink_crc_stop(intel_dp);
3805 if (ret)
3806 return ret;
3807 }
3808
082dcc7c 3809 hsw_disable_ips(intel_crtc);
1dda5f93 3810
9d1a1031 3811 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
082dcc7c
RV
3812 buf | DP_TEST_SINK_START) < 0) {
3813 hsw_enable_ips(intel_crtc);
3814 return -EIO;
4373f0f2
PZ
3815 }
3816
0f0f74bc 3817 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
082dcc7c
RV
3818 return 0;
3819}
3820
3821int intel_dp_sink_crc(struct intel_dp *intel_dp, u8 *crc)
3822{
3823 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
0f0f74bc 3824 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
082dcc7c
RV
3825 struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
3826 u8 buf;
621d4c76 3827 int count, ret;
082dcc7c 3828 int attempts = 6;
082dcc7c
RV
3829
3830 ret = intel_dp_sink_crc_start(intel_dp);
3831 if (ret)
3832 return ret;
3833
ad9dc91b 3834 do {
0f0f74bc 3835 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
621d4c76 3836
1dda5f93 3837 if (drm_dp_dpcd_readb(&intel_dp->aux,
4373f0f2
PZ
3838 DP_TEST_SINK_MISC, &buf) < 0) {
3839 ret = -EIO;
afe0d67e 3840 goto stop;
4373f0f2 3841 }
621d4c76 3842 count = buf & DP_TEST_COUNT_MASK;
aabc95dc 3843
7e38eeff 3844 } while (--attempts && count == 0);
ad9dc91b
RV
3845
3846 if (attempts == 0) {
7e38eeff
RV
3847 DRM_ERROR("Panel is unable to calculate any CRC after 6 vblanks\n");
3848 ret = -ETIMEDOUT;
3849 goto stop;
3850 }
3851
3852 if (drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_CRC_R_CR, crc, 6) < 0) {
3853 ret = -EIO;
3854 goto stop;
ad9dc91b 3855 }
d2e216d0 3856
afe0d67e 3857stop:
082dcc7c 3858 intel_dp_sink_crc_stop(intel_dp);
4373f0f2 3859 return ret;
d2e216d0
RV
3860}
3861
a60f0e38
JB
3862static bool
3863intel_dp_get_sink_irq(struct intel_dp *intel_dp, u8 *sink_irq_vector)
3864{
9f085ebb 3865 return drm_dp_dpcd_read(&intel_dp->aux,
9d1a1031
JN
3866 DP_DEVICE_SERVICE_IRQ_VECTOR,
3867 sink_irq_vector, 1) == 1;
a60f0e38
JB
3868}
3869
0e32b39c
DA
3870static bool
3871intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *sink_irq_vector)
3872{
3873 int ret;
3874
9f085ebb 3875 ret = drm_dp_dpcd_read(&intel_dp->aux,
0e32b39c
DA
3876 DP_SINK_COUNT_ESI,
3877 sink_irq_vector, 14);
3878 if (ret != 14)
3879 return false;
3880
3881 return true;
3882}
3883
c5d5ab7a
TP
3884static uint8_t intel_dp_autotest_link_training(struct intel_dp *intel_dp)
3885{
3886 uint8_t test_result = DP_TEST_ACK;
3887 return test_result;
3888}
3889
3890static uint8_t intel_dp_autotest_video_pattern(struct intel_dp *intel_dp)
3891{
3892 uint8_t test_result = DP_TEST_NAK;
3893 return test_result;
3894}
3895
3896static uint8_t intel_dp_autotest_edid(struct intel_dp *intel_dp)
a60f0e38 3897{
c5d5ab7a 3898 uint8_t test_result = DP_TEST_NAK;
559be30c
TP
3899 struct intel_connector *intel_connector = intel_dp->attached_connector;
3900 struct drm_connector *connector = &intel_connector->base;
3901
3902 if (intel_connector->detect_edid == NULL ||
ac6f2e29 3903 connector->edid_corrupt ||
559be30c
TP
3904 intel_dp->aux.i2c_defer_count > 6) {
3905 /* Check EDID read for NACKs, DEFERs and corruption
3906 * (DP CTS 1.2 Core r1.1)
3907 * 4.2.2.4 : Failed EDID read, I2C_NAK
3908 * 4.2.2.5 : Failed EDID read, I2C_DEFER
3909 * 4.2.2.6 : EDID corruption detected
3910 * Use failsafe mode for all cases
3911 */
3912 if (intel_dp->aux.i2c_nack_count > 0 ||
3913 intel_dp->aux.i2c_defer_count > 0)
3914 DRM_DEBUG_KMS("EDID read had %d NACKs, %d DEFERs\n",
3915 intel_dp->aux.i2c_nack_count,
3916 intel_dp->aux.i2c_defer_count);
c1617abc 3917 intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_FAILSAFE;
559be30c 3918 } else {
f79b468e
TS
3919 struct edid *block = intel_connector->detect_edid;
3920
3921 /* We have to write the checksum
3922 * of the last block read
3923 */
3924 block += intel_connector->detect_edid->extensions;
3925
559be30c
TP
3926 if (!drm_dp_dpcd_write(&intel_dp->aux,
3927 DP_TEST_EDID_CHECKSUM,
f79b468e 3928 &block->checksum,
5a1cc655 3929 1))
559be30c
TP
3930 DRM_DEBUG_KMS("Failed to write EDID checksum\n");
3931
3932 test_result = DP_TEST_ACK | DP_TEST_EDID_CHECKSUM_WRITE;
c1617abc 3933 intel_dp->compliance.test_data.edid = INTEL_DP_RESOLUTION_STANDARD;
559be30c
TP
3934 }
3935
3936 /* Set test active flag here so userspace doesn't interrupt things */
c1617abc 3937 intel_dp->compliance.test_active = 1;
559be30c 3938
c5d5ab7a
TP
3939 return test_result;
3940}
3941
3942static uint8_t intel_dp_autotest_phy_pattern(struct intel_dp *intel_dp)
a60f0e38 3943{
c5d5ab7a
TP
3944 uint8_t test_result = DP_TEST_NAK;
3945 return test_result;
3946}
3947
3948static void intel_dp_handle_test_request(struct intel_dp *intel_dp)
3949{
3950 uint8_t response = DP_TEST_NAK;
3951 uint8_t rxdata = 0;
3952 int status = 0;
3953
c5d5ab7a
TP
3954 status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_REQUEST, &rxdata, 1);
3955 if (status <= 0) {
3956 DRM_DEBUG_KMS("Could not read test request from sink\n");
3957 goto update_status;
3958 }
3959
3960 switch (rxdata) {
3961 case DP_TEST_LINK_TRAINING:
3962 DRM_DEBUG_KMS("LINK_TRAINING test requested\n");
c1617abc 3963 intel_dp->compliance.test_type = DP_TEST_LINK_TRAINING;
c5d5ab7a
TP
3964 response = intel_dp_autotest_link_training(intel_dp);
3965 break;
3966 case DP_TEST_LINK_VIDEO_PATTERN:
3967 DRM_DEBUG_KMS("TEST_PATTERN test requested\n");
c1617abc 3968 intel_dp->compliance.test_type = DP_TEST_LINK_VIDEO_PATTERN;
c5d5ab7a
TP
3969 response = intel_dp_autotest_video_pattern(intel_dp);
3970 break;
3971 case DP_TEST_LINK_EDID_READ:
3972 DRM_DEBUG_KMS("EDID test requested\n");
c1617abc 3973 intel_dp->compliance.test_type = DP_TEST_LINK_EDID_READ;
c5d5ab7a
TP
3974 response = intel_dp_autotest_edid(intel_dp);
3975 break;
3976 case DP_TEST_LINK_PHY_TEST_PATTERN:
3977 DRM_DEBUG_KMS("PHY_PATTERN test requested\n");
c1617abc 3978 intel_dp->compliance.test_type = DP_TEST_LINK_PHY_TEST_PATTERN;
c5d5ab7a
TP
3979 response = intel_dp_autotest_phy_pattern(intel_dp);
3980 break;
3981 default:
3982 DRM_DEBUG_KMS("Invalid test request '%02x'\n", rxdata);
3983 break;
3984 }
3985
3986update_status:
3987 status = drm_dp_dpcd_write(&intel_dp->aux,
3988 DP_TEST_RESPONSE,
3989 &response, 1);
3990 if (status <= 0)
3991 DRM_DEBUG_KMS("Could not write test response to sink\n");
a60f0e38
JB
3992}
3993
0e32b39c
DA
3994static int
3995intel_dp_check_mst_status(struct intel_dp *intel_dp)
3996{
3997 bool bret;
3998
3999 if (intel_dp->is_mst) {
4000 u8 esi[16] = { 0 };
4001 int ret = 0;
4002 int retry;
4003 bool handled;
4004 bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
4005go_again:
4006 if (bret == true) {
4007
4008 /* check link status - esi[10] = 0x200c */
19e0b4ca 4009 if (intel_dp->active_mst_links &&
901c2daf 4010 !drm_dp_channel_eq_ok(&esi[10], intel_dp->lane_count)) {
0e32b39c
DA
4011 DRM_DEBUG_KMS("channel EQ not ok, retraining\n");
4012 intel_dp_start_link_train(intel_dp);
0e32b39c
DA
4013 intel_dp_stop_link_train(intel_dp);
4014 }
4015
6f34cc39 4016 DRM_DEBUG_KMS("got esi %3ph\n", esi);
0e32b39c
DA
4017 ret = drm_dp_mst_hpd_irq(&intel_dp->mst_mgr, esi, &handled);
4018
4019 if (handled) {
4020 for (retry = 0; retry < 3; retry++) {
4021 int wret;
4022 wret = drm_dp_dpcd_write(&intel_dp->aux,
4023 DP_SINK_COUNT_ESI+1,
4024 &esi[1], 3);
4025 if (wret == 3) {
4026 break;
4027 }
4028 }
4029
4030 bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
4031 if (bret == true) {
6f34cc39 4032 DRM_DEBUG_KMS("got esi2 %3ph\n", esi);
0e32b39c
DA
4033 goto go_again;
4034 }
4035 } else
4036 ret = 0;
4037
4038 return ret;
4039 } else {
4040 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4041 DRM_DEBUG_KMS("failed to get ESI - device may have failed\n");
4042 intel_dp->is_mst = false;
4043 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
4044 /* send a hotplug event */
4045 drm_kms_helper_hotplug_event(intel_dig_port->base.base.dev);
4046 }
4047 }
4048 return -EINVAL;
4049}
4050
bfd02b3c
VS
4051static void
4052intel_dp_retrain_link(struct intel_dp *intel_dp)
4053{
4054 struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
4055 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4056 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
4057
4058 /* Suppress underruns caused by re-training */
4059 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
4060 if (crtc->config->has_pch_encoder)
4061 intel_set_pch_fifo_underrun_reporting(dev_priv,
4062 intel_crtc_pch_transcoder(crtc), false);
4063
4064 intel_dp_start_link_train(intel_dp);
4065 intel_dp_stop_link_train(intel_dp);
4066
4067 /* Keep underrun reporting disabled until things are stable */
0f0f74bc 4068 intel_wait_for_vblank(dev_priv, crtc->pipe);
bfd02b3c
VS
4069
4070 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
4071 if (crtc->config->has_pch_encoder)
4072 intel_set_pch_fifo_underrun_reporting(dev_priv,
4073 intel_crtc_pch_transcoder(crtc), true);
4074}
4075
5c9114d0
SS
4076static void
4077intel_dp_check_link_status(struct intel_dp *intel_dp)
4078{
4079 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
4080 struct drm_device *dev = intel_dp_to_dev(intel_dp);
4081 u8 link_status[DP_LINK_STATUS_SIZE];
4082
4083 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
4084
4085 if (!intel_dp_get_link_status(intel_dp, link_status)) {
4086 DRM_ERROR("Failed to get link status\n");
4087 return;
4088 }
4089
4090 if (!intel_encoder->base.crtc)
4091 return;
4092
4093 if (!to_intel_crtc(intel_encoder->base.crtc)->active)
4094 return;
4095
d4cb3fd9 4096 /* FIXME: we need to synchronize this sort of stuff with hardware
2dd85aeb
DV
4097 * readout. Currently fast link training doesn't work on boot-up. */
4098 if (!intel_dp->lane_count)
d4cb3fd9
MA
4099 return;
4100
5c9114d0 4101 /* if link training is requested we should perform it always */
c1617abc 4102 if ((intel_dp->compliance.test_type == DP_TEST_LINK_TRAINING) ||
5c9114d0
SS
4103 (!drm_dp_channel_eq_ok(link_status, intel_dp->lane_count))) {
4104 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
4105 intel_encoder->base.name);
bfd02b3c
VS
4106
4107 intel_dp_retrain_link(intel_dp);
5c9114d0
SS
4108 }
4109}
4110
a4fc5ed6
KP
4111/*
4112 * According to DP spec
4113 * 5.1.2:
4114 * 1. Read DPCD
4115 * 2. Configure link according to Receiver Capabilities
4116 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
4117 * 4. Check link status on receipt of hot-plug interrupt
39ff747b
SS
4118 *
4119 * intel_dp_short_pulse - handles short pulse interrupts
4120 * when full detection is not required.
4121 * Returns %true if short pulse is handled and full detection
4122 * is NOT required and %false otherwise.
a4fc5ed6 4123 */
39ff747b 4124static bool
5c9114d0 4125intel_dp_short_pulse(struct intel_dp *intel_dp)
a4fc5ed6 4126{
5b215bcf 4127 struct drm_device *dev = intel_dp_to_dev(intel_dp);
65fbb4e7 4128 u8 sink_irq_vector = 0;
39ff747b
SS
4129 u8 old_sink_count = intel_dp->sink_count;
4130 bool ret;
5b215bcf 4131
4df6960e
SS
4132 /*
4133 * Clearing compliance test variables to allow capturing
4134 * of values for next automated test request.
4135 */
c1617abc 4136 memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
4df6960e 4137
39ff747b
SS
4138 /*
4139 * Now read the DPCD to see if it's actually running
4140 * If the current value of sink count doesn't match with
4141 * the value that was stored earlier or dpcd read failed
4142 * we need to do full detection
4143 */
4144 ret = intel_dp_get_dpcd(intel_dp);
4145
4146 if ((old_sink_count != intel_dp->sink_count) || !ret) {
4147 /* No need to proceed if we are going to do full detect */
4148 return false;
59cd09e1
JB
4149 }
4150
a60f0e38
JB
4151 /* Try to read the source of the interrupt */
4152 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
65fbb4e7
VS
4153 intel_dp_get_sink_irq(intel_dp, &sink_irq_vector) &&
4154 sink_irq_vector != 0) {
a60f0e38 4155 /* Clear interrupt source */
9d1a1031
JN
4156 drm_dp_dpcd_writeb(&intel_dp->aux,
4157 DP_DEVICE_SERVICE_IRQ_VECTOR,
4158 sink_irq_vector);
a60f0e38
JB
4159
4160 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
09b1eb13 4161 DRM_DEBUG_DRIVER("Test request in short pulse not handled\n");
a60f0e38
JB
4162 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
4163 DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
4164 }
4165
5c9114d0
SS
4166 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
4167 intel_dp_check_link_status(intel_dp);
4168 drm_modeset_unlock(&dev->mode_config.connection_mutex);
39ff747b
SS
4169
4170 return true;
a4fc5ed6 4171}
a4fc5ed6 4172
caf9ab24 4173/* XXX this is probably wrong for multiple downstream ports */
71ba9000 4174static enum drm_connector_status
26d61aad 4175intel_dp_detect_dpcd(struct intel_dp *intel_dp)
71ba9000 4176{
caf9ab24 4177 uint8_t *dpcd = intel_dp->dpcd;
caf9ab24
AJ
4178 uint8_t type;
4179
4180 if (!intel_dp_get_dpcd(intel_dp))
4181 return connector_status_disconnected;
4182
1034ce70
SS
4183 if (is_edp(intel_dp))
4184 return connector_status_connected;
4185
caf9ab24 4186 /* if there's no downstream port, we're done */
c726ad01 4187 if (!drm_dp_is_branch(dpcd))
26d61aad 4188 return connector_status_connected;
caf9ab24
AJ
4189
4190 /* If we're HPD-aware, SINK_COUNT changes dynamically */
c9ff160b
JN
4191 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
4192 intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
9d1a1031 4193
30d9aa42
SS
4194 return intel_dp->sink_count ?
4195 connector_status_connected : connector_status_disconnected;
caf9ab24
AJ
4196 }
4197
c4e3170a
VS
4198 if (intel_dp_can_mst(intel_dp))
4199 return connector_status_connected;
4200
caf9ab24 4201 /* If no HPD, poke DDC gently */
0b99836f 4202 if (drm_probe_ddc(&intel_dp->aux.ddc))
26d61aad 4203 return connector_status_connected;
caf9ab24
AJ
4204
4205 /* Well we tried, say unknown for unreliable port types */
c9ff160b
JN
4206 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
4207 type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
4208 if (type == DP_DS_PORT_TYPE_VGA ||
4209 type == DP_DS_PORT_TYPE_NON_EDID)
4210 return connector_status_unknown;
4211 } else {
4212 type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
4213 DP_DWN_STRM_PORT_TYPE_MASK;
4214 if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
4215 type == DP_DWN_STRM_PORT_TYPE_OTHER)
4216 return connector_status_unknown;
4217 }
caf9ab24
AJ
4218
4219 /* Anything else is out of spec, warn and ignore */
4220 DRM_DEBUG_KMS("Broken DP branch device, ignoring\n");
26d61aad 4221 return connector_status_disconnected;
71ba9000
AJ
4222}
4223
d410b56d
CW
4224static enum drm_connector_status
4225edp_detect(struct intel_dp *intel_dp)
4226{
4227 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1650be74 4228 struct drm_i915_private *dev_priv = to_i915(dev);
d410b56d
CW
4229 enum drm_connector_status status;
4230
1650be74 4231 status = intel_panel_detect(dev_priv);
d410b56d
CW
4232 if (status == connector_status_unknown)
4233 status = connector_status_connected;
4234
4235 return status;
4236}
4237
b93433cc
JN
4238static bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
4239 struct intel_digital_port *port)
5eb08b69 4240{
b93433cc 4241 u32 bit;
01cb9ea6 4242
0df53b77
JN
4243 switch (port->port) {
4244 case PORT_A:
4245 return true;
4246 case PORT_B:
4247 bit = SDE_PORTB_HOTPLUG;
4248 break;
4249 case PORT_C:
4250 bit = SDE_PORTC_HOTPLUG;
4251 break;
4252 case PORT_D:
4253 bit = SDE_PORTD_HOTPLUG;
4254 break;
4255 default:
4256 MISSING_CASE(port->port);
4257 return false;
4258 }
4259
4260 return I915_READ(SDEISR) & bit;
4261}
4262
4263static bool cpt_digital_port_connected(struct drm_i915_private *dev_priv,
4264 struct intel_digital_port *port)
4265{
4266 u32 bit;
4267
4268 switch (port->port) {
4269 case PORT_A:
4270 return true;
4271 case PORT_B:
4272 bit = SDE_PORTB_HOTPLUG_CPT;
4273 break;
4274 case PORT_C:
4275 bit = SDE_PORTC_HOTPLUG_CPT;
4276 break;
4277 case PORT_D:
4278 bit = SDE_PORTD_HOTPLUG_CPT;
4279 break;
a78695d3
JN
4280 case PORT_E:
4281 bit = SDE_PORTE_HOTPLUG_SPT;
4282 break;
0df53b77
JN
4283 default:
4284 MISSING_CASE(port->port);
4285 return false;
b93433cc 4286 }
1b469639 4287
b93433cc 4288 return I915_READ(SDEISR) & bit;
5eb08b69
ZW
4289}
4290
7e66bcf2 4291static bool g4x_digital_port_connected(struct drm_i915_private *dev_priv,
1d245987 4292 struct intel_digital_port *port)
a4fc5ed6 4293{
9642c81c 4294 u32 bit;
5eb08b69 4295
9642c81c
JN
4296 switch (port->port) {
4297 case PORT_B:
4298 bit = PORTB_HOTPLUG_LIVE_STATUS_G4X;
4299 break;
4300 case PORT_C:
4301 bit = PORTC_HOTPLUG_LIVE_STATUS_G4X;
4302 break;
4303 case PORT_D:
4304 bit = PORTD_HOTPLUG_LIVE_STATUS_G4X;
4305 break;
4306 default:
4307 MISSING_CASE(port->port);
4308 return false;
4309 }
4310
4311 return I915_READ(PORT_HOTPLUG_STAT) & bit;
4312}
4313
0780cd36
VS
4314static bool gm45_digital_port_connected(struct drm_i915_private *dev_priv,
4315 struct intel_digital_port *port)
9642c81c
JN
4316{
4317 u32 bit;
4318
4319 switch (port->port) {
4320 case PORT_B:
0780cd36 4321 bit = PORTB_HOTPLUG_LIVE_STATUS_GM45;
9642c81c
JN
4322 break;
4323 case PORT_C:
0780cd36 4324 bit = PORTC_HOTPLUG_LIVE_STATUS_GM45;
9642c81c
JN
4325 break;
4326 case PORT_D:
0780cd36 4327 bit = PORTD_HOTPLUG_LIVE_STATUS_GM45;
9642c81c
JN
4328 break;
4329 default:
4330 MISSING_CASE(port->port);
4331 return false;
a4fc5ed6
KP
4332 }
4333
1d245987 4334 return I915_READ(PORT_HOTPLUG_STAT) & bit;
2a592bec
DA
4335}
4336
e464bfde 4337static bool bxt_digital_port_connected(struct drm_i915_private *dev_priv,
e2ec35a5 4338 struct intel_digital_port *intel_dig_port)
e464bfde 4339{
e2ec35a5
SJ
4340 struct intel_encoder *intel_encoder = &intel_dig_port->base;
4341 enum port port;
e464bfde
JN
4342 u32 bit;
4343
e2ec35a5
SJ
4344 intel_hpd_pin_to_port(intel_encoder->hpd_pin, &port);
4345 switch (port) {
e464bfde
JN
4346 case PORT_A:
4347 bit = BXT_DE_PORT_HP_DDIA;
4348 break;
4349 case PORT_B:
4350 bit = BXT_DE_PORT_HP_DDIB;
4351 break;
4352 case PORT_C:
4353 bit = BXT_DE_PORT_HP_DDIC;
4354 break;
4355 default:
e2ec35a5 4356 MISSING_CASE(port);
e464bfde
JN
4357 return false;
4358 }
4359
4360 return I915_READ(GEN8_DE_PORT_ISR) & bit;
4361}
4362
7e66bcf2
JN
4363/*
4364 * intel_digital_port_connected - is the specified port connected?
4365 * @dev_priv: i915 private structure
4366 * @port: the port to test
4367 *
4368 * Return %true if @port is connected, %false otherwise.
4369 */
23f889bd 4370static bool intel_digital_port_connected(struct drm_i915_private *dev_priv,
7e66bcf2
JN
4371 struct intel_digital_port *port)
4372{
0df53b77 4373 if (HAS_PCH_IBX(dev_priv))
7e66bcf2 4374 return ibx_digital_port_connected(dev_priv, port);
22824fac 4375 else if (HAS_PCH_SPLIT(dev_priv))
0df53b77 4376 return cpt_digital_port_connected(dev_priv, port);
cc3f90f0 4377 else if (IS_GEN9_LP(dev_priv))
e464bfde 4378 return bxt_digital_port_connected(dev_priv, port);
0780cd36
VS
4379 else if (IS_GM45(dev_priv))
4380 return gm45_digital_port_connected(dev_priv, port);
7e66bcf2
JN
4381 else
4382 return g4x_digital_port_connected(dev_priv, port);
4383}
4384
8c241fef 4385static struct edid *
beb60608 4386intel_dp_get_edid(struct intel_dp *intel_dp)
8c241fef 4387{
beb60608 4388 struct intel_connector *intel_connector = intel_dp->attached_connector;
d6f24d0f 4389
9cd300e0
JN
4390 /* use cached edid if we have one */
4391 if (intel_connector->edid) {
9cd300e0
JN
4392 /* invalid edid */
4393 if (IS_ERR(intel_connector->edid))
d6f24d0f
JB
4394 return NULL;
4395
55e9edeb 4396 return drm_edid_duplicate(intel_connector->edid);
beb60608
CW
4397 } else
4398 return drm_get_edid(&intel_connector->base,
4399 &intel_dp->aux.ddc);
4400}
8c241fef 4401
beb60608
CW
4402static void
4403intel_dp_set_edid(struct intel_dp *intel_dp)
4404{
4405 struct intel_connector *intel_connector = intel_dp->attached_connector;
4406 struct edid *edid;
8c241fef 4407
f21a2198 4408 intel_dp_unset_edid(intel_dp);
beb60608
CW
4409 edid = intel_dp_get_edid(intel_dp);
4410 intel_connector->detect_edid = edid;
4411
4412 if (intel_dp->force_audio != HDMI_AUDIO_AUTO)
4413 intel_dp->has_audio = intel_dp->force_audio == HDMI_AUDIO_ON;
4414 else
4415 intel_dp->has_audio = drm_detect_monitor_audio(edid);
8c241fef
KP
4416}
4417
beb60608
CW
4418static void
4419intel_dp_unset_edid(struct intel_dp *intel_dp)
8c241fef 4420{
beb60608 4421 struct intel_connector *intel_connector = intel_dp->attached_connector;
8c241fef 4422
beb60608
CW
4423 kfree(intel_connector->detect_edid);
4424 intel_connector->detect_edid = NULL;
9cd300e0 4425
beb60608
CW
4426 intel_dp->has_audio = false;
4427}
d6f24d0f 4428
5cb651a7 4429static enum drm_connector_status
f21a2198 4430intel_dp_long_pulse(struct intel_connector *intel_connector)
a9756bb5 4431{
f21a2198 4432 struct drm_connector *connector = &intel_connector->base;
a9756bb5 4433 struct intel_dp *intel_dp = intel_attached_dp(connector);
d63885da
PZ
4434 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4435 struct intel_encoder *intel_encoder = &intel_dig_port->base;
fa90ecef 4436 struct drm_device *dev = connector->dev;
a9756bb5 4437 enum drm_connector_status status;
671dedd2 4438 enum intel_display_power_domain power_domain;
65fbb4e7 4439 u8 sink_irq_vector = 0;
a9756bb5 4440
25f78f58
VS
4441 power_domain = intel_display_port_aux_power_domain(intel_encoder);
4442 intel_display_power_get(to_i915(dev), power_domain);
a9756bb5 4443
d410b56d
CW
4444 /* Can't disconnect eDP, but you can close the lid... */
4445 if (is_edp(intel_dp))
4446 status = edp_detect(intel_dp);
c555a81d
ACO
4447 else if (intel_digital_port_connected(to_i915(dev),
4448 dp_to_dig_port(intel_dp)))
4449 status = intel_dp_detect_dpcd(intel_dp);
a9756bb5 4450 else
c555a81d
ACO
4451 status = connector_status_disconnected;
4452
5cb651a7 4453 if (status == connector_status_disconnected) {
c1617abc 4454 memset(&intel_dp->compliance, 0, sizeof(intel_dp->compliance));
4df6960e 4455
0e505a08 4456 if (intel_dp->is_mst) {
4457 DRM_DEBUG_KMS("MST device may have disappeared %d vs %d\n",
4458 intel_dp->is_mst,
4459 intel_dp->mst_mgr.mst_state);
4460 intel_dp->is_mst = false;
4461 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
4462 intel_dp->is_mst);
4463 }
4464
c8c8fb33 4465 goto out;
4df6960e 4466 }
a9756bb5 4467
f21a2198 4468 if (intel_encoder->type != INTEL_OUTPUT_EDP)
cca0502b 4469 intel_encoder->type = INTEL_OUTPUT_DP;
f21a2198 4470
fe5a66f9
VS
4471 DRM_DEBUG_KMS("Display Port TPS3 support: source %s, sink %s\n",
4472 yesno(intel_dp_source_supports_hbr2(intel_dp)),
4473 yesno(drm_dp_tps3_supported(intel_dp->dpcd)));
4474
f482984a
MN
4475 /* Set the max lane count for sink */
4476 intel_dp->max_sink_lane_count = drm_dp_max_lane_count(intel_dp->dpcd);
4477
4478 /* Set the max link BW for sink */
4479 intel_dp->max_sink_link_bw = intel_dp_max_link_bw(intel_dp);
4480
fe5a66f9
VS
4481 intel_dp_print_rates(intel_dp);
4482
7b3fc170 4483 intel_dp_read_desc(intel_dp);
0e390a33 4484
c4e3170a
VS
4485 intel_dp_configure_mst(intel_dp);
4486
4487 if (intel_dp->is_mst) {
f21a2198
SS
4488 /*
4489 * If we are in MST mode then this connector
4490 * won't appear connected or have anything
4491 * with EDID on it
4492 */
0e32b39c
DA
4493 status = connector_status_disconnected;
4494 goto out;
7d23e3c3
SS
4495 } else if (connector->status == connector_status_connected) {
4496 /*
4497 * If display was connected already and is still connected
4498 * check links status, there has been known issues of
4499 * link loss triggerring long pulse!!!!
4500 */
4501 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
4502 intel_dp_check_link_status(intel_dp);
4503 drm_modeset_unlock(&dev->mode_config.connection_mutex);
4504 goto out;
0e32b39c
DA
4505 }
4506
4df6960e
SS
4507 /*
4508 * Clearing NACK and defer counts to get their exact values
4509 * while reading EDID which are required by Compliance tests
4510 * 4.2.2.4 and 4.2.2.5
4511 */
4512 intel_dp->aux.i2c_nack_count = 0;
4513 intel_dp->aux.i2c_defer_count = 0;
4514
beb60608 4515 intel_dp_set_edid(intel_dp);
5cb651a7
VS
4516 if (is_edp(intel_dp) || intel_connector->detect_edid)
4517 status = connector_status_connected;
7d23e3c3 4518 intel_dp->detect_done = true;
c8c8fb33 4519
09b1eb13
TP
4520 /* Try to read the source of the interrupt */
4521 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
65fbb4e7
VS
4522 intel_dp_get_sink_irq(intel_dp, &sink_irq_vector) &&
4523 sink_irq_vector != 0) {
09b1eb13
TP
4524 /* Clear interrupt source */
4525 drm_dp_dpcd_writeb(&intel_dp->aux,
4526 DP_DEVICE_SERVICE_IRQ_VECTOR,
4527 sink_irq_vector);
4528
4529 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
4530 intel_dp_handle_test_request(intel_dp);
4531 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
4532 DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
4533 }
4534
c8c8fb33 4535out:
5cb651a7 4536 if (status != connector_status_connected && !intel_dp->is_mst)
f21a2198 4537 intel_dp_unset_edid(intel_dp);
7d23e3c3 4538
25f78f58 4539 intel_display_power_put(to_i915(dev), power_domain);
5cb651a7 4540 return status;
f21a2198
SS
4541}
4542
4543static enum drm_connector_status
4544intel_dp_detect(struct drm_connector *connector, bool force)
4545{
4546 struct intel_dp *intel_dp = intel_attached_dp(connector);
5cb651a7 4547 enum drm_connector_status status = connector->status;
f21a2198
SS
4548
4549 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4550 connector->base.id, connector->name);
4551
7d23e3c3
SS
4552 /* If full detect is not performed yet, do a full detect */
4553 if (!intel_dp->detect_done)
5cb651a7 4554 status = intel_dp_long_pulse(intel_dp->attached_connector);
7d23e3c3
SS
4555
4556 intel_dp->detect_done = false;
f21a2198 4557
5cb651a7 4558 return status;
a4fc5ed6
KP
4559}
4560
beb60608
CW
4561static void
4562intel_dp_force(struct drm_connector *connector)
a4fc5ed6 4563{
df0e9248 4564 struct intel_dp *intel_dp = intel_attached_dp(connector);
beb60608 4565 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
25f78f58 4566 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
671dedd2 4567 enum intel_display_power_domain power_domain;
a4fc5ed6 4568
beb60608
CW
4569 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4570 connector->base.id, connector->name);
4571 intel_dp_unset_edid(intel_dp);
a4fc5ed6 4572
beb60608
CW
4573 if (connector->status != connector_status_connected)
4574 return;
671dedd2 4575
25f78f58
VS
4576 power_domain = intel_display_port_aux_power_domain(intel_encoder);
4577 intel_display_power_get(dev_priv, power_domain);
beb60608
CW
4578
4579 intel_dp_set_edid(intel_dp);
4580
25f78f58 4581 intel_display_power_put(dev_priv, power_domain);
beb60608
CW
4582
4583 if (intel_encoder->type != INTEL_OUTPUT_EDP)
cca0502b 4584 intel_encoder->type = INTEL_OUTPUT_DP;
beb60608
CW
4585}
4586
4587static int intel_dp_get_modes(struct drm_connector *connector)
4588{
4589 struct intel_connector *intel_connector = to_intel_connector(connector);
4590 struct edid *edid;
4591
4592 edid = intel_connector->detect_edid;
4593 if (edid) {
4594 int ret = intel_connector_update_modes(connector, edid);
4595 if (ret)
4596 return ret;
4597 }
32f9d658 4598
f8779fda 4599 /* if eDP has no EDID, fall back to fixed mode */
beb60608
CW
4600 if (is_edp(intel_attached_dp(connector)) &&
4601 intel_connector->panel.fixed_mode) {
f8779fda 4602 struct drm_display_mode *mode;
beb60608
CW
4603
4604 mode = drm_mode_duplicate(connector->dev,
dd06f90e 4605 intel_connector->panel.fixed_mode);
f8779fda 4606 if (mode) {
32f9d658
ZW
4607 drm_mode_probed_add(connector, mode);
4608 return 1;
4609 }
4610 }
beb60608 4611
32f9d658 4612 return 0;
a4fc5ed6
KP
4613}
4614
1aad7ac0
CW
4615static bool
4616intel_dp_detect_audio(struct drm_connector *connector)
4617{
1aad7ac0 4618 bool has_audio = false;
beb60608 4619 struct edid *edid;
1aad7ac0 4620
beb60608
CW
4621 edid = to_intel_connector(connector)->detect_edid;
4622 if (edid)
1aad7ac0 4623 has_audio = drm_detect_monitor_audio(edid);
671dedd2 4624
1aad7ac0
CW
4625 return has_audio;
4626}
4627
f684960e
CW
4628static int
4629intel_dp_set_property(struct drm_connector *connector,
4630 struct drm_property *property,
4631 uint64_t val)
4632{
fac5e23e 4633 struct drm_i915_private *dev_priv = to_i915(connector->dev);
53b41837 4634 struct intel_connector *intel_connector = to_intel_connector(connector);
da63a9f2
PZ
4635 struct intel_encoder *intel_encoder = intel_attached_encoder(connector);
4636 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
f684960e
CW
4637 int ret;
4638
662595df 4639 ret = drm_object_property_set_value(&connector->base, property, val);
f684960e
CW
4640 if (ret)
4641 return ret;
4642
3f43c48d 4643 if (property == dev_priv->force_audio_property) {
1aad7ac0
CW
4644 int i = val;
4645 bool has_audio;
4646
4647 if (i == intel_dp->force_audio)
f684960e
CW
4648 return 0;
4649
1aad7ac0 4650 intel_dp->force_audio = i;
f684960e 4651
c3e5f67b 4652 if (i == HDMI_AUDIO_AUTO)
1aad7ac0
CW
4653 has_audio = intel_dp_detect_audio(connector);
4654 else
c3e5f67b 4655 has_audio = (i == HDMI_AUDIO_ON);
1aad7ac0
CW
4656
4657 if (has_audio == intel_dp->has_audio)
f684960e
CW
4658 return 0;
4659
1aad7ac0 4660 intel_dp->has_audio = has_audio;
f684960e
CW
4661 goto done;
4662 }
4663
e953fd7b 4664 if (property == dev_priv->broadcast_rgb_property) {
ae4edb80 4665 bool old_auto = intel_dp->color_range_auto;
0f2a2a75 4666 bool old_range = intel_dp->limited_color_range;
ae4edb80 4667
55bc60db
VS
4668 switch (val) {
4669 case INTEL_BROADCAST_RGB_AUTO:
4670 intel_dp->color_range_auto = true;
4671 break;
4672 case INTEL_BROADCAST_RGB_FULL:
4673 intel_dp->color_range_auto = false;
0f2a2a75 4674 intel_dp->limited_color_range = false;
55bc60db
VS
4675 break;
4676 case INTEL_BROADCAST_RGB_LIMITED:
4677 intel_dp->color_range_auto = false;
0f2a2a75 4678 intel_dp->limited_color_range = true;
55bc60db
VS
4679 break;
4680 default:
4681 return -EINVAL;
4682 }
ae4edb80
DV
4683
4684 if (old_auto == intel_dp->color_range_auto &&
0f2a2a75 4685 old_range == intel_dp->limited_color_range)
ae4edb80
DV
4686 return 0;
4687
e953fd7b
CW
4688 goto done;
4689 }
4690
53b41837
YN
4691 if (is_edp(intel_dp) &&
4692 property == connector->dev->mode_config.scaling_mode_property) {
4693 if (val == DRM_MODE_SCALE_NONE) {
4694 DRM_DEBUG_KMS("no scaling not supported\n");
4695 return -EINVAL;
4696 }
234126c6
VS
4697 if (HAS_GMCH_DISPLAY(dev_priv) &&
4698 val == DRM_MODE_SCALE_CENTER) {
4699 DRM_DEBUG_KMS("centering not supported\n");
4700 return -EINVAL;
4701 }
53b41837
YN
4702
4703 if (intel_connector->panel.fitting_mode == val) {
4704 /* the eDP scaling property is not changed */
4705 return 0;
4706 }
4707 intel_connector->panel.fitting_mode = val;
4708
4709 goto done;
4710 }
4711
f684960e
CW
4712 return -EINVAL;
4713
4714done:
c0c36b94
CW
4715 if (intel_encoder->base.crtc)
4716 intel_crtc_restore_mode(intel_encoder->base.crtc);
f684960e
CW
4717
4718 return 0;
4719}
4720
7a418e34
CW
4721static int
4722intel_dp_connector_register(struct drm_connector *connector)
4723{
4724 struct intel_dp *intel_dp = intel_attached_dp(connector);
1ebaa0b9
CW
4725 int ret;
4726
4727 ret = intel_connector_register(connector);
4728 if (ret)
4729 return ret;
7a418e34
CW
4730
4731 i915_debugfs_connector_add(connector);
4732
4733 DRM_DEBUG_KMS("registering %s bus for %s\n",
4734 intel_dp->aux.name, connector->kdev->kobj.name);
4735
4736 intel_dp->aux.dev = connector->kdev;
4737 return drm_dp_aux_register(&intel_dp->aux);
4738}
4739
c191eca1
CW
4740static void
4741intel_dp_connector_unregister(struct drm_connector *connector)
4742{
4743 drm_dp_aux_unregister(&intel_attached_dp(connector)->aux);
4744 intel_connector_unregister(connector);
4745}
4746
a4fc5ed6 4747static void
73845adf 4748intel_dp_connector_destroy(struct drm_connector *connector)
a4fc5ed6 4749{
1d508706 4750 struct intel_connector *intel_connector = to_intel_connector(connector);
aaa6fd2a 4751
10e972d3 4752 kfree(intel_connector->detect_edid);
beb60608 4753
9cd300e0
JN
4754 if (!IS_ERR_OR_NULL(intel_connector->edid))
4755 kfree(intel_connector->edid);
4756
acd8db10
PZ
4757 /* Can't call is_edp() since the encoder may have been destroyed
4758 * already. */
4759 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
1d508706 4760 intel_panel_fini(&intel_connector->panel);
aaa6fd2a 4761
a4fc5ed6 4762 drm_connector_cleanup(connector);
55f78c43 4763 kfree(connector);
a4fc5ed6
KP
4764}
4765
00c09d70 4766void intel_dp_encoder_destroy(struct drm_encoder *encoder)
24d05927 4767{
da63a9f2
PZ
4768 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
4769 struct intel_dp *intel_dp = &intel_dig_port->dp;
24d05927 4770
0e32b39c 4771 intel_dp_mst_encoder_cleanup(intel_dig_port);
bd943159
KP
4772 if (is_edp(intel_dp)) {
4773 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
951468f3
VS
4774 /*
4775 * vdd might still be enabled do to the delayed vdd off.
4776 * Make sure vdd is actually turned off here.
4777 */
773538e8 4778 pps_lock(intel_dp);
4be73780 4779 edp_panel_vdd_off_sync(intel_dp);
773538e8
VS
4780 pps_unlock(intel_dp);
4781
01527b31
CT
4782 if (intel_dp->edp_notifier.notifier_call) {
4783 unregister_reboot_notifier(&intel_dp->edp_notifier);
4784 intel_dp->edp_notifier.notifier_call = NULL;
4785 }
bd943159 4786 }
99681886
CW
4787
4788 intel_dp_aux_fini(intel_dp);
4789
c8bd0e49 4790 drm_encoder_cleanup(encoder);
da63a9f2 4791 kfree(intel_dig_port);
24d05927
DV
4792}
4793
bf93ba67 4794void intel_dp_encoder_suspend(struct intel_encoder *intel_encoder)
07f9cd0b
ID
4795{
4796 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
4797
4798 if (!is_edp(intel_dp))
4799 return;
4800
951468f3
VS
4801 /*
4802 * vdd might still be enabled do to the delayed vdd off.
4803 * Make sure vdd is actually turned off here.
4804 */
afa4e53a 4805 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
773538e8 4806 pps_lock(intel_dp);
07f9cd0b 4807 edp_panel_vdd_off_sync(intel_dp);
773538e8 4808 pps_unlock(intel_dp);
07f9cd0b
ID
4809}
4810
49e6bc51
VS
4811static void intel_edp_panel_vdd_sanitize(struct intel_dp *intel_dp)
4812{
4813 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4814 struct drm_device *dev = intel_dig_port->base.base.dev;
fac5e23e 4815 struct drm_i915_private *dev_priv = to_i915(dev);
49e6bc51
VS
4816 enum intel_display_power_domain power_domain;
4817
4818 lockdep_assert_held(&dev_priv->pps_mutex);
4819
4820 if (!edp_have_panel_vdd(intel_dp))
4821 return;
4822
4823 /*
4824 * The VDD bit needs a power domain reference, so if the bit is
4825 * already enabled when we boot or resume, grab this reference and
4826 * schedule a vdd off, so we don't hold on to the reference
4827 * indefinitely.
4828 */
4829 DRM_DEBUG_KMS("VDD left on by BIOS, adjusting state tracking\n");
25f78f58 4830 power_domain = intel_display_port_aux_power_domain(&intel_dig_port->base);
49e6bc51
VS
4831 intel_display_power_get(dev_priv, power_domain);
4832
4833 edp_panel_vdd_schedule_off(intel_dp);
4834}
4835
9f2bdb00
VS
4836static enum pipe vlv_active_pipe(struct intel_dp *intel_dp)
4837{
4838 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
4839
4840 if ((intel_dp->DP & DP_PORT_EN) == 0)
4841 return INVALID_PIPE;
4842
4843 if (IS_CHERRYVIEW(dev_priv))
4844 return DP_PORT_TO_PIPE_CHV(intel_dp->DP);
4845 else
4846 return PORT_TO_PIPE(intel_dp->DP);
4847}
4848
bf93ba67 4849void intel_dp_encoder_reset(struct drm_encoder *encoder)
6d93c0c4 4850{
64989ca4 4851 struct drm_i915_private *dev_priv = to_i915(encoder->dev);
dd75f6dd
ID
4852 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4853 struct intel_lspcon *lspcon = dp_to_lspcon(intel_dp);
64989ca4
VS
4854
4855 if (!HAS_DDI(dev_priv))
4856 intel_dp->DP = I915_READ(intel_dp->output_reg);
49e6bc51 4857
dd75f6dd 4858 if (lspcon->active)
910530c0
SS
4859 lspcon_resume(lspcon);
4860
49e6bc51
VS
4861 pps_lock(intel_dp);
4862
9f2bdb00
VS
4863 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4864 intel_dp->active_pipe = vlv_active_pipe(intel_dp);
4865
4866 if (is_edp(intel_dp)) {
4867 /* Reinit the power sequencer, in case BIOS did something with it. */
4868 intel_dp_pps_init(encoder->dev, intel_dp);
4869 intel_edp_panel_vdd_sanitize(intel_dp);
4870 }
49e6bc51
VS
4871
4872 pps_unlock(intel_dp);
6d93c0c4
ID
4873}
4874
a4fc5ed6 4875static const struct drm_connector_funcs intel_dp_connector_funcs = {
4d688a2a 4876 .dpms = drm_atomic_helper_connector_dpms,
a4fc5ed6 4877 .detect = intel_dp_detect,
beb60608 4878 .force = intel_dp_force,
a4fc5ed6 4879 .fill_modes = drm_helper_probe_single_connector_modes,
f684960e 4880 .set_property = intel_dp_set_property,
2545e4a6 4881 .atomic_get_property = intel_connector_atomic_get_property,
7a418e34 4882 .late_register = intel_dp_connector_register,
c191eca1 4883 .early_unregister = intel_dp_connector_unregister,
73845adf 4884 .destroy = intel_dp_connector_destroy,
c6f95f27 4885 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
98969725 4886 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
a4fc5ed6
KP
4887};
4888
4889static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
4890 .get_modes = intel_dp_get_modes,
4891 .mode_valid = intel_dp_mode_valid,
a4fc5ed6
KP
4892};
4893
a4fc5ed6 4894static const struct drm_encoder_funcs intel_dp_enc_funcs = {
6d93c0c4 4895 .reset = intel_dp_encoder_reset,
24d05927 4896 .destroy = intel_dp_encoder_destroy,
a4fc5ed6
KP
4897};
4898
b2c5c181 4899enum irqreturn
13cf5504
DA
4900intel_dp_hpd_pulse(struct intel_digital_port *intel_dig_port, bool long_hpd)
4901{
4902 struct intel_dp *intel_dp = &intel_dig_port->dp;
1c767b33 4903 struct intel_encoder *intel_encoder = &intel_dig_port->base;
0e32b39c 4904 struct drm_device *dev = intel_dig_port->base.base.dev;
fac5e23e 4905 struct drm_i915_private *dev_priv = to_i915(dev);
1c767b33 4906 enum intel_display_power_domain power_domain;
b2c5c181 4907 enum irqreturn ret = IRQ_NONE;
1c767b33 4908
2540058f
TI
4909 if (intel_dig_port->base.type != INTEL_OUTPUT_EDP &&
4910 intel_dig_port->base.type != INTEL_OUTPUT_HDMI)
cca0502b 4911 intel_dig_port->base.type = INTEL_OUTPUT_DP;
13cf5504 4912
7a7f84cc
VS
4913 if (long_hpd && intel_dig_port->base.type == INTEL_OUTPUT_EDP) {
4914 /*
4915 * vdd off can generate a long pulse on eDP which
4916 * would require vdd on to handle it, and thus we
4917 * would end up in an endless cycle of
4918 * "vdd off -> long hpd -> vdd on -> detect -> vdd off -> ..."
4919 */
4920 DRM_DEBUG_KMS("ignoring long hpd on eDP port %c\n",
4921 port_name(intel_dig_port->port));
a8b3d52f 4922 return IRQ_HANDLED;
7a7f84cc
VS
4923 }
4924
26fbb774
VS
4925 DRM_DEBUG_KMS("got hpd irq on port %c - %s\n",
4926 port_name(intel_dig_port->port),
0e32b39c 4927 long_hpd ? "long" : "short");
13cf5504 4928
27d4efc5
VS
4929 if (long_hpd) {
4930 intel_dp->detect_done = false;
4931 return IRQ_NONE;
4932 }
4933
25f78f58 4934 power_domain = intel_display_port_aux_power_domain(intel_encoder);
1c767b33
ID
4935 intel_display_power_get(dev_priv, power_domain);
4936
27d4efc5
VS
4937 if (intel_dp->is_mst) {
4938 if (intel_dp_check_mst_status(intel_dp) == -EINVAL) {
4939 /*
4940 * If we were in MST mode, and device is not
4941 * there, get out of MST mode
4942 */
4943 DRM_DEBUG_KMS("MST device may have disappeared %d vs %d\n",
4944 intel_dp->is_mst, intel_dp->mst_mgr.mst_state);
4945 intel_dp->is_mst = false;
4946 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr,
4947 intel_dp->is_mst);
4948 intel_dp->detect_done = false;
4949 goto put_power;
0e32b39c 4950 }
27d4efc5 4951 }
0e32b39c 4952
27d4efc5
VS
4953 if (!intel_dp->is_mst) {
4954 if (!intel_dp_short_pulse(intel_dp)) {
4955 intel_dp->detect_done = false;
4956 goto put_power;
39ff747b 4957 }
0e32b39c 4958 }
b2c5c181
DV
4959
4960 ret = IRQ_HANDLED;
4961
1c767b33
ID
4962put_power:
4963 intel_display_power_put(dev_priv, power_domain);
4964
4965 return ret;
13cf5504
DA
4966}
4967
477ec328 4968/* check the VBT to see whether the eDP is on another port */
dd11bc10 4969bool intel_dp_is_edp(struct drm_i915_private *dev_priv, enum port port)
36e83a18 4970{
53ce81a7
VS
4971 /*
4972 * eDP not supported on g4x. so bail out early just
4973 * for a bit extra safety in case the VBT is bonkers.
4974 */
dd11bc10 4975 if (INTEL_GEN(dev_priv) < 5)
53ce81a7
VS
4976 return false;
4977
3b32a35b
VS
4978 if (port == PORT_A)
4979 return true;
4980
951d9efe 4981 return intel_bios_is_port_edp(dev_priv, port);
36e83a18
ZY
4982}
4983
0e32b39c 4984void
f684960e
CW
4985intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
4986{
53b41837
YN
4987 struct intel_connector *intel_connector = to_intel_connector(connector);
4988
3f43c48d 4989 intel_attach_force_audio_property(connector);
e953fd7b 4990 intel_attach_broadcast_rgb_property(connector);
55bc60db 4991 intel_dp->color_range_auto = true;
53b41837
YN
4992
4993 if (is_edp(intel_dp)) {
4994 drm_mode_create_scaling_mode_property(connector->dev);
6de6d846
RC
4995 drm_object_attach_property(
4996 &connector->base,
53b41837 4997 connector->dev->mode_config.scaling_mode_property,
8e740cd1
YN
4998 DRM_MODE_SCALE_ASPECT);
4999 intel_connector->panel.fitting_mode = DRM_MODE_SCALE_ASPECT;
53b41837 5000 }
f684960e
CW
5001}
5002
dada1a9f
ID
5003static void intel_dp_init_panel_power_timestamps(struct intel_dp *intel_dp)
5004{
d28d4731 5005 intel_dp->panel_power_off_time = ktime_get_boottime();
dada1a9f
ID
5006 intel_dp->last_power_on = jiffies;
5007 intel_dp->last_backlight_off = jiffies;
5008}
5009
67a54566 5010static void
54648618
ID
5011intel_pps_readout_hw_state(struct drm_i915_private *dev_priv,
5012 struct intel_dp *intel_dp, struct edp_power_seq *seq)
67a54566 5013{
b0a08bec 5014 u32 pp_on, pp_off, pp_div = 0, pp_ctl = 0;
8e8232d5 5015 struct pps_registers regs;
453c5420 5016
8e8232d5 5017 intel_pps_get_registers(dev_priv, intel_dp, &regs);
67a54566
DV
5018
5019 /* Workaround: Need to write PP_CONTROL with the unlock key as
5020 * the very first thing. */
b0a08bec 5021 pp_ctl = ironlake_get_pp_control(intel_dp);
67a54566 5022
8e8232d5
ID
5023 pp_on = I915_READ(regs.pp_on);
5024 pp_off = I915_READ(regs.pp_off);
cc3f90f0 5025 if (!IS_GEN9_LP(dev_priv)) {
8e8232d5
ID
5026 I915_WRITE(regs.pp_ctrl, pp_ctl);
5027 pp_div = I915_READ(regs.pp_div);
b0a08bec 5028 }
67a54566
DV
5029
5030 /* Pull timing values out of registers */
54648618
ID
5031 seq->t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
5032 PANEL_POWER_UP_DELAY_SHIFT;
67a54566 5033
54648618
ID
5034 seq->t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
5035 PANEL_LIGHT_ON_DELAY_SHIFT;
67a54566 5036
54648618
ID
5037 seq->t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
5038 PANEL_LIGHT_OFF_DELAY_SHIFT;
67a54566 5039
54648618
ID
5040 seq->t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
5041 PANEL_POWER_DOWN_DELAY_SHIFT;
67a54566 5042
cc3f90f0 5043 if (IS_GEN9_LP(dev_priv)) {
b0a08bec
VK
5044 u16 tmp = (pp_ctl & BXT_POWER_CYCLE_DELAY_MASK) >>
5045 BXT_POWER_CYCLE_DELAY_SHIFT;
5046 if (tmp > 0)
54648618 5047 seq->t11_t12 = (tmp - 1) * 1000;
b0a08bec 5048 else
54648618 5049 seq->t11_t12 = 0;
b0a08bec 5050 } else {
54648618 5051 seq->t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
67a54566 5052 PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
b0a08bec 5053 }
54648618
ID
5054}
5055
de9c1b6b
ID
5056static void
5057intel_pps_dump_state(const char *state_name, const struct edp_power_seq *seq)
5058{
5059 DRM_DEBUG_KMS("%s t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
5060 state_name,
5061 seq->t1_t3, seq->t8, seq->t9, seq->t10, seq->t11_t12);
5062}
5063
5064static void
5065intel_pps_verify_state(struct drm_i915_private *dev_priv,
5066 struct intel_dp *intel_dp)
5067{
5068 struct edp_power_seq hw;
5069 struct edp_power_seq *sw = &intel_dp->pps_delays;
5070
5071 intel_pps_readout_hw_state(dev_priv, intel_dp, &hw);
5072
5073 if (hw.t1_t3 != sw->t1_t3 || hw.t8 != sw->t8 || hw.t9 != sw->t9 ||
5074 hw.t10 != sw->t10 || hw.t11_t12 != sw->t11_t12) {
5075 DRM_ERROR("PPS state mismatch\n");
5076 intel_pps_dump_state("sw", sw);
5077 intel_pps_dump_state("hw", &hw);
5078 }
5079}
5080
54648618
ID
5081static void
5082intel_dp_init_panel_power_sequencer(struct drm_device *dev,
5083 struct intel_dp *intel_dp)
5084{
fac5e23e 5085 struct drm_i915_private *dev_priv = to_i915(dev);
54648618
ID
5086 struct edp_power_seq cur, vbt, spec,
5087 *final = &intel_dp->pps_delays;
5088
5089 lockdep_assert_held(&dev_priv->pps_mutex);
5090
5091 /* already initialized? */
5092 if (final->t11_t12 != 0)
5093 return;
5094
5095 intel_pps_readout_hw_state(dev_priv, intel_dp, &cur);
67a54566 5096
de9c1b6b 5097 intel_pps_dump_state("cur", &cur);
67a54566 5098
6aa23e65 5099 vbt = dev_priv->vbt.edp.pps;
67a54566
DV
5100
5101 /* Upper limits from eDP 1.3 spec. Note that we use the clunky units of
5102 * our hw here, which are all in 100usec. */
5103 spec.t1_t3 = 210 * 10;
5104 spec.t8 = 50 * 10; /* no limit for t8, use t7 instead */
5105 spec.t9 = 50 * 10; /* no limit for t9, make it symmetric with t8 */
5106 spec.t10 = 500 * 10;
5107 /* This one is special and actually in units of 100ms, but zero
5108 * based in the hw (so we need to add 100 ms). But the sw vbt
5109 * table multiplies it with 1000 to make it in units of 100usec,
5110 * too. */
5111 spec.t11_t12 = (510 + 100) * 10;
5112
de9c1b6b 5113 intel_pps_dump_state("vbt", &vbt);
67a54566
DV
5114
5115 /* Use the max of the register settings and vbt. If both are
5116 * unset, fall back to the spec limits. */
36b5f425 5117#define assign_final(field) final->field = (max(cur.field, vbt.field) == 0 ? \
67a54566
DV
5118 spec.field : \
5119 max(cur.field, vbt.field))
5120 assign_final(t1_t3);
5121 assign_final(t8);
5122 assign_final(t9);
5123 assign_final(t10);
5124 assign_final(t11_t12);
5125#undef assign_final
5126
36b5f425 5127#define get_delay(field) (DIV_ROUND_UP(final->field, 10))
67a54566
DV
5128 intel_dp->panel_power_up_delay = get_delay(t1_t3);
5129 intel_dp->backlight_on_delay = get_delay(t8);
5130 intel_dp->backlight_off_delay = get_delay(t9);
5131 intel_dp->panel_power_down_delay = get_delay(t10);
5132 intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
5133#undef get_delay
5134
f30d26e4
JN
5135 DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
5136 intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
5137 intel_dp->panel_power_cycle_delay);
5138
5139 DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
5140 intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
de9c1b6b
ID
5141
5142 /*
5143 * We override the HW backlight delays to 1 because we do manual waits
5144 * on them. For T8, even BSpec recommends doing it. For T9, if we
5145 * don't do this, we'll end up waiting for the backlight off delay
5146 * twice: once when we do the manual sleep, and once when we disable
5147 * the panel and wait for the PP_STATUS bit to become zero.
5148 */
5149 final->t8 = 1;
5150 final->t9 = 1;
f30d26e4
JN
5151}
5152
5153static void
5154intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
36b5f425 5155 struct intel_dp *intel_dp)
f30d26e4 5156{
fac5e23e 5157 struct drm_i915_private *dev_priv = to_i915(dev);
453c5420 5158 u32 pp_on, pp_off, pp_div, port_sel = 0;
e7dc33f3 5159 int div = dev_priv->rawclk_freq / 1000;
8e8232d5 5160 struct pps_registers regs;
ad933b56 5161 enum port port = dp_to_dig_port(intel_dp)->port;
36b5f425 5162 const struct edp_power_seq *seq = &intel_dp->pps_delays;
453c5420 5163
e39b999a 5164 lockdep_assert_held(&dev_priv->pps_mutex);
453c5420 5165
8e8232d5 5166 intel_pps_get_registers(dev_priv, intel_dp, &regs);
453c5420 5167
f30d26e4 5168 pp_on = (seq->t1_t3 << PANEL_POWER_UP_DELAY_SHIFT) |
de9c1b6b
ID
5169 (seq->t8 << PANEL_LIGHT_ON_DELAY_SHIFT);
5170 pp_off = (seq->t9 << PANEL_LIGHT_OFF_DELAY_SHIFT) |
f30d26e4 5171 (seq->t10 << PANEL_POWER_DOWN_DELAY_SHIFT);
67a54566
DV
5172 /* Compute the divisor for the pp clock, simply match the Bspec
5173 * formula. */
cc3f90f0 5174 if (IS_GEN9_LP(dev_priv)) {
8e8232d5 5175 pp_div = I915_READ(regs.pp_ctrl);
b0a08bec
VK
5176 pp_div &= ~BXT_POWER_CYCLE_DELAY_MASK;
5177 pp_div |= (DIV_ROUND_UP((seq->t11_t12 + 1), 1000)
5178 << BXT_POWER_CYCLE_DELAY_SHIFT);
5179 } else {
5180 pp_div = ((100 * div)/2 - 1) << PP_REFERENCE_DIVIDER_SHIFT;
5181 pp_div |= (DIV_ROUND_UP(seq->t11_t12, 1000)
5182 << PANEL_POWER_CYCLE_DELAY_SHIFT);
5183 }
67a54566
DV
5184
5185 /* Haswell doesn't have any port selection bits for the panel
5186 * power sequencer any more. */
920a14b2 5187 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
ad933b56 5188 port_sel = PANEL_PORT_SELECT_VLV(port);
6e266956 5189 } else if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
ad933b56 5190 if (port == PORT_A)
a24c144c 5191 port_sel = PANEL_PORT_SELECT_DPA;
67a54566 5192 else
a24c144c 5193 port_sel = PANEL_PORT_SELECT_DPD;
67a54566
DV
5194 }
5195
453c5420
JB
5196 pp_on |= port_sel;
5197
8e8232d5
ID
5198 I915_WRITE(regs.pp_on, pp_on);
5199 I915_WRITE(regs.pp_off, pp_off);
cc3f90f0 5200 if (IS_GEN9_LP(dev_priv))
8e8232d5 5201 I915_WRITE(regs.pp_ctrl, pp_div);
b0a08bec 5202 else
8e8232d5 5203 I915_WRITE(regs.pp_div, pp_div);
67a54566 5204
67a54566 5205 DRM_DEBUG_KMS("panel power sequencer register settings: PP_ON %#x, PP_OFF %#x, PP_DIV %#x\n",
8e8232d5
ID
5206 I915_READ(regs.pp_on),
5207 I915_READ(regs.pp_off),
cc3f90f0 5208 IS_GEN9_LP(dev_priv) ?
8e8232d5
ID
5209 (I915_READ(regs.pp_ctrl) & BXT_POWER_CYCLE_DELAY_MASK) :
5210 I915_READ(regs.pp_div));
f684960e
CW
5211}
5212
335f752b
ID
5213static void intel_dp_pps_init(struct drm_device *dev,
5214 struct intel_dp *intel_dp)
5215{
920a14b2
TU
5216 struct drm_i915_private *dev_priv = to_i915(dev);
5217
5218 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
335f752b
ID
5219 vlv_initial_power_sequencer_setup(intel_dp);
5220 } else {
5221 intel_dp_init_panel_power_sequencer(dev, intel_dp);
5222 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
5223 }
5224}
5225
b33a2815
VK
5226/**
5227 * intel_dp_set_drrs_state - program registers for RR switch to take effect
5423adf1 5228 * @dev_priv: i915 device
e896402c 5229 * @crtc_state: a pointer to the active intel_crtc_state
b33a2815
VK
5230 * @refresh_rate: RR to be programmed
5231 *
5232 * This function gets called when refresh rate (RR) has to be changed from
5233 * one frequency to another. Switches can be between high and low RR
5234 * supported by the panel or to any other RR based on media playback (in
5235 * this case, RR value needs to be passed from user space).
5236 *
5237 * The caller of this function needs to take a lock on dev_priv->drrs.
5238 */
85cb48a1
ML
5239static void intel_dp_set_drrs_state(struct drm_i915_private *dev_priv,
5240 struct intel_crtc_state *crtc_state,
5241 int refresh_rate)
439d7ac0 5242{
439d7ac0 5243 struct intel_encoder *encoder;
96178eeb
VK
5244 struct intel_digital_port *dig_port = NULL;
5245 struct intel_dp *intel_dp = dev_priv->drrs.dp;
85cb48a1 5246 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
96178eeb 5247 enum drrs_refresh_rate_type index = DRRS_HIGH_RR;
439d7ac0
PB
5248
5249 if (refresh_rate <= 0) {
5250 DRM_DEBUG_KMS("Refresh rate should be positive non-zero.\n");
5251 return;
5252 }
5253
96178eeb
VK
5254 if (intel_dp == NULL) {
5255 DRM_DEBUG_KMS("DRRS not supported.\n");
439d7ac0
PB
5256 return;
5257 }
5258
1fcc9d1c 5259 /*
e4d59f6b
RV
5260 * FIXME: This needs proper synchronization with psr state for some
5261 * platforms that cannot have PSR and DRRS enabled at the same time.
1fcc9d1c 5262 */
439d7ac0 5263
96178eeb
VK
5264 dig_port = dp_to_dig_port(intel_dp);
5265 encoder = &dig_port->base;
723f9aab 5266 intel_crtc = to_intel_crtc(encoder->base.crtc);
439d7ac0
PB
5267
5268 if (!intel_crtc) {
5269 DRM_DEBUG_KMS("DRRS: intel_crtc not initialized\n");
5270 return;
5271 }
5272
96178eeb 5273 if (dev_priv->drrs.type < SEAMLESS_DRRS_SUPPORT) {
439d7ac0
PB
5274 DRM_DEBUG_KMS("Only Seamless DRRS supported.\n");
5275 return;
5276 }
5277
96178eeb
VK
5278 if (intel_dp->attached_connector->panel.downclock_mode->vrefresh ==
5279 refresh_rate)
439d7ac0
PB
5280 index = DRRS_LOW_RR;
5281
96178eeb 5282 if (index == dev_priv->drrs.refresh_rate_type) {
439d7ac0
PB
5283 DRM_DEBUG_KMS(
5284 "DRRS requested for previously set RR...ignoring\n");
5285 return;
5286 }
5287
85cb48a1 5288 if (!crtc_state->base.active) {
439d7ac0
PB
5289 DRM_DEBUG_KMS("eDP encoder disabled. CRTC not Active\n");
5290 return;
5291 }
5292
85cb48a1 5293 if (INTEL_GEN(dev_priv) >= 8 && !IS_CHERRYVIEW(dev_priv)) {
a4c30b1d
VK
5294 switch (index) {
5295 case DRRS_HIGH_RR:
5296 intel_dp_set_m_n(intel_crtc, M1_N1);
5297 break;
5298 case DRRS_LOW_RR:
5299 intel_dp_set_m_n(intel_crtc, M2_N2);
5300 break;
5301 case DRRS_MAX_RR:
5302 default:
5303 DRM_ERROR("Unsupported refreshrate type\n");
5304 }
85cb48a1
ML
5305 } else if (INTEL_GEN(dev_priv) > 6) {
5306 i915_reg_t reg = PIPECONF(crtc_state->cpu_transcoder);
649636ef 5307 u32 val;
a4c30b1d 5308
649636ef 5309 val = I915_READ(reg);
439d7ac0 5310 if (index > DRRS_HIGH_RR) {
85cb48a1 5311 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6fa7aec1
VK
5312 val |= PIPECONF_EDP_RR_MODE_SWITCH_VLV;
5313 else
5314 val |= PIPECONF_EDP_RR_MODE_SWITCH;
439d7ac0 5315 } else {
85cb48a1 5316 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6fa7aec1
VK
5317 val &= ~PIPECONF_EDP_RR_MODE_SWITCH_VLV;
5318 else
5319 val &= ~PIPECONF_EDP_RR_MODE_SWITCH;
439d7ac0
PB
5320 }
5321 I915_WRITE(reg, val);
5322 }
5323
4e9ac947
VK
5324 dev_priv->drrs.refresh_rate_type = index;
5325
5326 DRM_DEBUG_KMS("eDP Refresh Rate set to : %dHz\n", refresh_rate);
5327}
5328
b33a2815
VK
5329/**
5330 * intel_edp_drrs_enable - init drrs struct if supported
5331 * @intel_dp: DP struct
5423adf1 5332 * @crtc_state: A pointer to the active crtc state.
b33a2815
VK
5333 *
5334 * Initializes frontbuffer_bits and drrs.dp
5335 */
85cb48a1
ML
5336void intel_edp_drrs_enable(struct intel_dp *intel_dp,
5337 struct intel_crtc_state *crtc_state)
c395578e
VK
5338{
5339 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 5340 struct drm_i915_private *dev_priv = to_i915(dev);
c395578e 5341
85cb48a1 5342 if (!crtc_state->has_drrs) {
c395578e
VK
5343 DRM_DEBUG_KMS("Panel doesn't support DRRS\n");
5344 return;
5345 }
5346
5347 mutex_lock(&dev_priv->drrs.mutex);
5348 if (WARN_ON(dev_priv->drrs.dp)) {
5349 DRM_ERROR("DRRS already enabled\n");
5350 goto unlock;
5351 }
5352
5353 dev_priv->drrs.busy_frontbuffer_bits = 0;
5354
5355 dev_priv->drrs.dp = intel_dp;
5356
5357unlock:
5358 mutex_unlock(&dev_priv->drrs.mutex);
5359}
5360
b33a2815
VK
5361/**
5362 * intel_edp_drrs_disable - Disable DRRS
5363 * @intel_dp: DP struct
5423adf1 5364 * @old_crtc_state: Pointer to old crtc_state.
b33a2815
VK
5365 *
5366 */
85cb48a1
ML
5367void intel_edp_drrs_disable(struct intel_dp *intel_dp,
5368 struct intel_crtc_state *old_crtc_state)
c395578e
VK
5369{
5370 struct drm_device *dev = intel_dp_to_dev(intel_dp);
fac5e23e 5371 struct drm_i915_private *dev_priv = to_i915(dev);
c395578e 5372
85cb48a1 5373 if (!old_crtc_state->has_drrs)
c395578e
VK
5374 return;
5375
5376 mutex_lock(&dev_priv->drrs.mutex);
5377 if (!dev_priv->drrs.dp) {
5378 mutex_unlock(&dev_priv->drrs.mutex);
5379 return;
5380 }
5381
5382 if (dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
85cb48a1
ML
5383 intel_dp_set_drrs_state(dev_priv, old_crtc_state,
5384 intel_dp->attached_connector->panel.fixed_mode->vrefresh);
c395578e
VK
5385
5386 dev_priv->drrs.dp = NULL;
5387 mutex_unlock(&dev_priv->drrs.mutex);
5388
5389 cancel_delayed_work_sync(&dev_priv->drrs.work);
5390}
5391
4e9ac947
VK
5392static void intel_edp_drrs_downclock_work(struct work_struct *work)
5393{
5394 struct drm_i915_private *dev_priv =
5395 container_of(work, typeof(*dev_priv), drrs.work.work);
5396 struct intel_dp *intel_dp;
5397
5398 mutex_lock(&dev_priv->drrs.mutex);
5399
5400 intel_dp = dev_priv->drrs.dp;
5401
5402 if (!intel_dp)
5403 goto unlock;
5404
439d7ac0 5405 /*
4e9ac947
VK
5406 * The delayed work can race with an invalidate hence we need to
5407 * recheck.
439d7ac0
PB
5408 */
5409
4e9ac947
VK
5410 if (dev_priv->drrs.busy_frontbuffer_bits)
5411 goto unlock;
439d7ac0 5412
85cb48a1
ML
5413 if (dev_priv->drrs.refresh_rate_type != DRRS_LOW_RR) {
5414 struct drm_crtc *crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
5415
5416 intel_dp_set_drrs_state(dev_priv, to_intel_crtc(crtc)->config,
5417 intel_dp->attached_connector->panel.downclock_mode->vrefresh);
5418 }
439d7ac0 5419
4e9ac947 5420unlock:
4e9ac947 5421 mutex_unlock(&dev_priv->drrs.mutex);
439d7ac0
PB
5422}
5423
b33a2815 5424/**
0ddfd203 5425 * intel_edp_drrs_invalidate - Disable Idleness DRRS
5748b6a1 5426 * @dev_priv: i915 device
b33a2815
VK
5427 * @frontbuffer_bits: frontbuffer plane tracking bits
5428 *
0ddfd203
R
5429 * This function gets called everytime rendering on the given planes start.
5430 * Hence DRRS needs to be Upclocked, i.e. (LOW_RR -> HIGH_RR).
b33a2815
VK
5431 *
5432 * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
5433 */
5748b6a1
CW
5434void intel_edp_drrs_invalidate(struct drm_i915_private *dev_priv,
5435 unsigned int frontbuffer_bits)
a93fad0f 5436{
a93fad0f
VK
5437 struct drm_crtc *crtc;
5438 enum pipe pipe;
5439
9da7d693 5440 if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
a93fad0f
VK
5441 return;
5442
88f933a8 5443 cancel_delayed_work(&dev_priv->drrs.work);
3954e733 5444
a93fad0f 5445 mutex_lock(&dev_priv->drrs.mutex);
9da7d693
DV
5446 if (!dev_priv->drrs.dp) {
5447 mutex_unlock(&dev_priv->drrs.mutex);
5448 return;
5449 }
5450
a93fad0f
VK
5451 crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
5452 pipe = to_intel_crtc(crtc)->pipe;
5453
c1d038c6
DV
5454 frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
5455 dev_priv->drrs.busy_frontbuffer_bits |= frontbuffer_bits;
5456
0ddfd203 5457 /* invalidate means busy screen hence upclock */
c1d038c6 5458 if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
85cb48a1
ML
5459 intel_dp_set_drrs_state(dev_priv, to_intel_crtc(crtc)->config,
5460 dev_priv->drrs.dp->attached_connector->panel.fixed_mode->vrefresh);
a93fad0f 5461
a93fad0f
VK
5462 mutex_unlock(&dev_priv->drrs.mutex);
5463}
5464
b33a2815 5465/**
0ddfd203 5466 * intel_edp_drrs_flush - Restart Idleness DRRS
5748b6a1 5467 * @dev_priv: i915 device
b33a2815
VK
5468 * @frontbuffer_bits: frontbuffer plane tracking bits
5469 *
0ddfd203
R
5470 * This function gets called every time rendering on the given planes has
5471 * completed or flip on a crtc is completed. So DRRS should be upclocked
5472 * (LOW_RR -> HIGH_RR). And also Idleness detection should be started again,
5473 * if no other planes are dirty.
b33a2815
VK
5474 *
5475 * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
5476 */
5748b6a1
CW
5477void intel_edp_drrs_flush(struct drm_i915_private *dev_priv,
5478 unsigned int frontbuffer_bits)
a93fad0f 5479{
a93fad0f
VK
5480 struct drm_crtc *crtc;
5481 enum pipe pipe;
5482
9da7d693 5483 if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
a93fad0f
VK
5484 return;
5485
88f933a8 5486 cancel_delayed_work(&dev_priv->drrs.work);
3954e733 5487
a93fad0f 5488 mutex_lock(&dev_priv->drrs.mutex);
9da7d693
DV
5489 if (!dev_priv->drrs.dp) {
5490 mutex_unlock(&dev_priv->drrs.mutex);
5491 return;
5492 }
5493
a93fad0f
VK
5494 crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
5495 pipe = to_intel_crtc(crtc)->pipe;
c1d038c6
DV
5496
5497 frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
a93fad0f
VK
5498 dev_priv->drrs.busy_frontbuffer_bits &= ~frontbuffer_bits;
5499
0ddfd203 5500 /* flush means busy screen hence upclock */
c1d038c6 5501 if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
85cb48a1
ML
5502 intel_dp_set_drrs_state(dev_priv, to_intel_crtc(crtc)->config,
5503 dev_priv->drrs.dp->attached_connector->panel.fixed_mode->vrefresh);
0ddfd203
R
5504
5505 /*
5506 * flush also means no more activity hence schedule downclock, if all
5507 * other fbs are quiescent too
5508 */
5509 if (!dev_priv->drrs.busy_frontbuffer_bits)
a93fad0f
VK
5510 schedule_delayed_work(&dev_priv->drrs.work,
5511 msecs_to_jiffies(1000));
5512 mutex_unlock(&dev_priv->drrs.mutex);
5513}
5514
b33a2815
VK
5515/**
5516 * DOC: Display Refresh Rate Switching (DRRS)
5517 *
5518 * Display Refresh Rate Switching (DRRS) is a power conservation feature
5519 * which enables swtching between low and high refresh rates,
5520 * dynamically, based on the usage scenario. This feature is applicable
5521 * for internal panels.
5522 *
5523 * Indication that the panel supports DRRS is given by the panel EDID, which
5524 * would list multiple refresh rates for one resolution.
5525 *
5526 * DRRS is of 2 types - static and seamless.
5527 * Static DRRS involves changing refresh rate (RR) by doing a full modeset
5528 * (may appear as a blink on screen) and is used in dock-undock scenario.
5529 * Seamless DRRS involves changing RR without any visual effect to the user
5530 * and can be used during normal system usage. This is done by programming
5531 * certain registers.
5532 *
5533 * Support for static/seamless DRRS may be indicated in the VBT based on
5534 * inputs from the panel spec.
5535 *
5536 * DRRS saves power by switching to low RR based on usage scenarios.
5537 *
2e7a5701
DV
5538 * The implementation is based on frontbuffer tracking implementation. When
5539 * there is a disturbance on the screen triggered by user activity or a periodic
5540 * system activity, DRRS is disabled (RR is changed to high RR). When there is
5541 * no movement on screen, after a timeout of 1 second, a switch to low RR is
5542 * made.
5543 *
5544 * For integration with frontbuffer tracking code, intel_edp_drrs_invalidate()
5545 * and intel_edp_drrs_flush() are called.
b33a2815
VK
5546 *
5547 * DRRS can be further extended to support other internal panels and also
5548 * the scenario of video playback wherein RR is set based on the rate
5549 * requested by userspace.
5550 */
5551
5552/**
5553 * intel_dp_drrs_init - Init basic DRRS work and mutex.
5554 * @intel_connector: eDP connector
5555 * @fixed_mode: preferred mode of panel
5556 *
5557 * This function is called only once at driver load to initialize basic
5558 * DRRS stuff.
5559 *
5560 * Returns:
5561 * Downclock mode if panel supports it, else return NULL.
5562 * DRRS support is determined by the presence of downclock mode (apart
5563 * from VBT setting).
5564 */
4f9db5b5 5565static struct drm_display_mode *
96178eeb
VK
5566intel_dp_drrs_init(struct intel_connector *intel_connector,
5567 struct drm_display_mode *fixed_mode)
4f9db5b5
PB
5568{
5569 struct drm_connector *connector = &intel_connector->base;
96178eeb 5570 struct drm_device *dev = connector->dev;
fac5e23e 5571 struct drm_i915_private *dev_priv = to_i915(dev);
4f9db5b5
PB
5572 struct drm_display_mode *downclock_mode = NULL;
5573
9da7d693
DV
5574 INIT_DELAYED_WORK(&dev_priv->drrs.work, intel_edp_drrs_downclock_work);
5575 mutex_init(&dev_priv->drrs.mutex);
5576
dd11bc10 5577 if (INTEL_GEN(dev_priv) <= 6) {
4f9db5b5
PB
5578 DRM_DEBUG_KMS("DRRS supported for Gen7 and above\n");
5579 return NULL;
5580 }
5581
5582 if (dev_priv->vbt.drrs_type != SEAMLESS_DRRS_SUPPORT) {
4079b8d1 5583 DRM_DEBUG_KMS("VBT doesn't support DRRS\n");
4f9db5b5
PB
5584 return NULL;
5585 }
5586
5587 downclock_mode = intel_find_panel_downclock
a318b4c4 5588 (dev_priv, fixed_mode, connector);
4f9db5b5
PB
5589
5590 if (!downclock_mode) {
a1d26342 5591 DRM_DEBUG_KMS("Downclock mode is not found. DRRS not supported\n");
4f9db5b5
PB
5592 return NULL;
5593 }
5594
96178eeb 5595 dev_priv->drrs.type = dev_priv->vbt.drrs_type;
4f9db5b5 5596
96178eeb 5597 dev_priv->drrs.refresh_rate_type = DRRS_HIGH_RR;
4079b8d1 5598 DRM_DEBUG_KMS("seamless DRRS supported for eDP panel.\n");
4f9db5b5
PB
5599 return downclock_mode;
5600}
5601
ed92f0b2 5602static bool intel_edp_init_connector(struct intel_dp *intel_dp,
36b5f425 5603 struct intel_connector *intel_connector)
ed92f0b2
PZ
5604{
5605 struct drm_connector *connector = &intel_connector->base;
5606 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
63635217
PZ
5607 struct intel_encoder *intel_encoder = &intel_dig_port->base;
5608 struct drm_device *dev = intel_encoder->base.dev;
fac5e23e 5609 struct drm_i915_private *dev_priv = to_i915(dev);
ed92f0b2 5610 struct drm_display_mode *fixed_mode = NULL;
4f9db5b5 5611 struct drm_display_mode *downclock_mode = NULL;
ed92f0b2
PZ
5612 bool has_dpcd;
5613 struct drm_display_mode *scan;
5614 struct edid *edid;
6517d273 5615 enum pipe pipe = INVALID_PIPE;
ed92f0b2
PZ
5616
5617 if (!is_edp(intel_dp))
5618 return true;
5619
97a824e1
ID
5620 /*
5621 * On IBX/CPT we may get here with LVDS already registered. Since the
5622 * driver uses the only internal power sequencer available for both
5623 * eDP and LVDS bail out early in this case to prevent interfering
5624 * with an already powered-on LVDS power sequencer.
5625 */
5626 if (intel_get_lvds_encoder(dev)) {
5627 WARN_ON(!(HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)));
5628 DRM_INFO("LVDS was detected, not registering eDP\n");
5629
5630 return false;
5631 }
5632
49e6bc51 5633 pps_lock(intel_dp);
b4d06ede
ID
5634
5635 intel_dp_init_panel_power_timestamps(intel_dp);
335f752b 5636 intel_dp_pps_init(dev, intel_dp);
49e6bc51 5637 intel_edp_panel_vdd_sanitize(intel_dp);
b4d06ede 5638
49e6bc51 5639 pps_unlock(intel_dp);
63635217 5640
ed92f0b2 5641 /* Cache DPCD and EDID for edp. */
fe5a66f9 5642 has_dpcd = intel_edp_init_dpcd(intel_dp);
ed92f0b2 5643
fe5a66f9 5644 if (!has_dpcd) {
ed92f0b2
PZ
5645 /* if this fails, presume the device is a ghost */
5646 DRM_INFO("failed to retrieve link info, disabling eDP\n");
b4d06ede 5647 goto out_vdd_off;
ed92f0b2
PZ
5648 }
5649
060c8778 5650 mutex_lock(&dev->mode_config.mutex);
0b99836f 5651 edid = drm_get_edid(connector, &intel_dp->aux.ddc);
ed92f0b2
PZ
5652 if (edid) {
5653 if (drm_add_edid_modes(connector, edid)) {
5654 drm_mode_connector_update_edid_property(connector,
5655 edid);
5656 drm_edid_to_eld(connector, edid);
5657 } else {
5658 kfree(edid);
5659 edid = ERR_PTR(-EINVAL);
5660 }
5661 } else {
5662 edid = ERR_PTR(-ENOENT);
5663 }
5664 intel_connector->edid = edid;
5665
5666 /* prefer fixed mode from EDID if available */
5667 list_for_each_entry(scan, &connector->probed_modes, head) {
5668 if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
5669 fixed_mode = drm_mode_duplicate(dev, scan);
4f9db5b5 5670 downclock_mode = intel_dp_drrs_init(
4f9db5b5 5671 intel_connector, fixed_mode);
ed92f0b2
PZ
5672 break;
5673 }
5674 }
5675
5676 /* fallback to VBT if available for eDP */
5677 if (!fixed_mode && dev_priv->vbt.lfp_lvds_vbt_mode) {
5678 fixed_mode = drm_mode_duplicate(dev,
5679 dev_priv->vbt.lfp_lvds_vbt_mode);
df457245 5680 if (fixed_mode) {
ed92f0b2 5681 fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
df457245
VS
5682 connector->display_info.width_mm = fixed_mode->width_mm;
5683 connector->display_info.height_mm = fixed_mode->height_mm;
5684 }
ed92f0b2 5685 }
060c8778 5686 mutex_unlock(&dev->mode_config.mutex);
ed92f0b2 5687
920a14b2 5688 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
01527b31
CT
5689 intel_dp->edp_notifier.notifier_call = edp_notify_handler;
5690 register_reboot_notifier(&intel_dp->edp_notifier);
6517d273
VS
5691
5692 /*
5693 * Figure out the current pipe for the initial backlight setup.
5694 * If the current pipe isn't valid, try the PPS pipe, and if that
5695 * fails just assume pipe A.
5696 */
9f2bdb00 5697 pipe = vlv_active_pipe(intel_dp);
6517d273
VS
5698
5699 if (pipe != PIPE_A && pipe != PIPE_B)
5700 pipe = intel_dp->pps_pipe;
5701
5702 if (pipe != PIPE_A && pipe != PIPE_B)
5703 pipe = PIPE_A;
5704
5705 DRM_DEBUG_KMS("using pipe %c for initial backlight setup\n",
5706 pipe_name(pipe));
01527b31
CT
5707 }
5708
4f9db5b5 5709 intel_panel_init(&intel_connector->panel, fixed_mode, downclock_mode);
5507faeb 5710 intel_connector->panel.backlight.power = intel_edp_backlight_power;
6517d273 5711 intel_panel_setup_backlight(connector, pipe);
ed92f0b2
PZ
5712
5713 return true;
b4d06ede
ID
5714
5715out_vdd_off:
5716 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
5717 /*
5718 * vdd might still be enabled do to the delayed vdd off.
5719 * Make sure vdd is actually turned off here.
5720 */
5721 pps_lock(intel_dp);
5722 edp_panel_vdd_off_sync(intel_dp);
5723 pps_unlock(intel_dp);
5724
5725 return false;
ed92f0b2
PZ
5726}
5727
16c25533 5728bool
f0fec3f2
PZ
5729intel_dp_init_connector(struct intel_digital_port *intel_dig_port,
5730 struct intel_connector *intel_connector)
a4fc5ed6 5731{
f0fec3f2
PZ
5732 struct drm_connector *connector = &intel_connector->base;
5733 struct intel_dp *intel_dp = &intel_dig_port->dp;
5734 struct intel_encoder *intel_encoder = &intel_dig_port->base;
5735 struct drm_device *dev = intel_encoder->base.dev;
fac5e23e 5736 struct drm_i915_private *dev_priv = to_i915(dev);
174edf1f 5737 enum port port = intel_dig_port->port;
7a418e34 5738 int type;
a4fc5ed6 5739
ccb1a831
VS
5740 if (WARN(intel_dig_port->max_lanes < 1,
5741 "Not enough lanes (%d) for DP on port %c\n",
5742 intel_dig_port->max_lanes, port_name(port)))
5743 return false;
5744
a4a5d2f8 5745 intel_dp->pps_pipe = INVALID_PIPE;
9f2bdb00 5746 intel_dp->active_pipe = INVALID_PIPE;
a4a5d2f8 5747
ec5b01dd 5748 /* intel_dp vfuncs */
dd11bc10 5749 if (INTEL_GEN(dev_priv) >= 9)
b6b5e383 5750 intel_dp->get_aux_clock_divider = skl_get_aux_clock_divider;
8652744b 5751 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ec5b01dd 5752 intel_dp->get_aux_clock_divider = hsw_get_aux_clock_divider;
6e266956 5753 else if (HAS_PCH_SPLIT(dev_priv))
ec5b01dd
DL
5754 intel_dp->get_aux_clock_divider = ilk_get_aux_clock_divider;
5755 else
6ffb1be7 5756 intel_dp->get_aux_clock_divider = g4x_get_aux_clock_divider;
ec5b01dd 5757
dd11bc10 5758 if (INTEL_GEN(dev_priv) >= 9)
b9ca5fad
DL
5759 intel_dp->get_aux_send_ctl = skl_get_aux_send_ctl;
5760 else
6ffb1be7 5761 intel_dp->get_aux_send_ctl = g4x_get_aux_send_ctl;
153b1100 5762
4f8036a2 5763 if (HAS_DDI(dev_priv))
ad64217b
ACO
5764 intel_dp->prepare_link_retrain = intel_ddi_prepare_link_retrain;
5765
0767935e
DV
5766 /* Preserve the current hw state. */
5767 intel_dp->DP = I915_READ(intel_dp->output_reg);
dd06f90e 5768 intel_dp->attached_connector = intel_connector;
3d3dc149 5769
dd11bc10 5770 if (intel_dp_is_edp(dev_priv, port))
b329530c 5771 type = DRM_MODE_CONNECTOR_eDP;
3b32a35b
VS
5772 else
5773 type = DRM_MODE_CONNECTOR_DisplayPort;
b329530c 5774
9f2bdb00
VS
5775 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5776 intel_dp->active_pipe = vlv_active_pipe(intel_dp);
5777
f7d24902
ID
5778 /*
5779 * For eDP we always set the encoder type to INTEL_OUTPUT_EDP, but
5780 * for DP the encoder type can be set by the caller to
5781 * INTEL_OUTPUT_UNKNOWN for DDI, so don't rewrite it.
5782 */
5783 if (type == DRM_MODE_CONNECTOR_eDP)
5784 intel_encoder->type = INTEL_OUTPUT_EDP;
5785
c17ed5b5 5786 /* eDP only on port B and/or C on vlv/chv */
920a14b2 5787 if (WARN_ON((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 5788 is_edp(intel_dp) && port != PORT_B && port != PORT_C))
c17ed5b5
VS
5789 return false;
5790
e7281eab
ID
5791 DRM_DEBUG_KMS("Adding %s connector on port %c\n",
5792 type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
5793 port_name(port));
5794
b329530c 5795 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
a4fc5ed6
KP
5796 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
5797
a4fc5ed6
KP
5798 connector->interlace_allowed = true;
5799 connector->doublescan_allowed = 0;
5800
b6339585 5801 intel_dp_aux_init(intel_dp);
7a418e34 5802
f0fec3f2 5803 INIT_DELAYED_WORK(&intel_dp->panel_vdd_work,
4be73780 5804 edp_panel_vdd_work);
a4fc5ed6 5805
df0e9248 5806 intel_connector_attach_encoder(intel_connector, intel_encoder);
a4fc5ed6 5807
4f8036a2 5808 if (HAS_DDI(dev_priv))
bcbc889b
PZ
5809 intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
5810 else
5811 intel_connector->get_hw_state = intel_connector_get_hw_state;
5812
0b99836f 5813 /* Set up the hotplug pin. */
ab9d7c30
PZ
5814 switch (port) {
5815 case PORT_A:
1d843f9d 5816 intel_encoder->hpd_pin = HPD_PORT_A;
ab9d7c30
PZ
5817 break;
5818 case PORT_B:
1d843f9d 5819 intel_encoder->hpd_pin = HPD_PORT_B;
e2d214ae 5820 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
cf1d5883 5821 intel_encoder->hpd_pin = HPD_PORT_A;
ab9d7c30
PZ
5822 break;
5823 case PORT_C:
1d843f9d 5824 intel_encoder->hpd_pin = HPD_PORT_C;
ab9d7c30
PZ
5825 break;
5826 case PORT_D:
1d843f9d 5827 intel_encoder->hpd_pin = HPD_PORT_D;
ab9d7c30 5828 break;
26951caf
XZ
5829 case PORT_E:
5830 intel_encoder->hpd_pin = HPD_PORT_E;
5831 break;
ab9d7c30 5832 default:
ad1c0b19 5833 BUG();
5eb08b69
ZW
5834 }
5835
0e32b39c 5836 /* init MST on ports that can support it */
56b857a5 5837 if (HAS_DP_MST(dev_priv) && !is_edp(intel_dp) &&
0c9b3715
JN
5838 (port == PORT_B || port == PORT_C || port == PORT_D))
5839 intel_dp_mst_encoder_init(intel_dig_port,
5840 intel_connector->base.base.id);
0e32b39c 5841
36b5f425 5842 if (!intel_edp_init_connector(intel_dp, intel_connector)) {
a121f4e5
VS
5843 intel_dp_aux_fini(intel_dp);
5844 intel_dp_mst_encoder_cleanup(intel_dig_port);
5845 goto fail;
b2f246a8 5846 }
32f9d658 5847
f684960e
CW
5848 intel_dp_add_properties(intel_dp, connector);
5849
a4fc5ed6
KP
5850 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
5851 * 0xd. Failure to do so will result in spurious interrupts being
5852 * generated on the port when a cable is not attached.
5853 */
50a0bc90 5854 if (IS_G4X(dev_priv) && !IS_GM45(dev_priv)) {
a4fc5ed6
KP
5855 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
5856 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
5857 }
16c25533
PZ
5858
5859 return true;
a121f4e5
VS
5860
5861fail:
a121f4e5
VS
5862 drm_connector_cleanup(connector);
5863
5864 return false;
a4fc5ed6 5865}
f0fec3f2 5866
c39055b0 5867bool intel_dp_init(struct drm_i915_private *dev_priv,
457c52d8
CW
5868 i915_reg_t output_reg,
5869 enum port port)
f0fec3f2
PZ
5870{
5871 struct intel_digital_port *intel_dig_port;
5872 struct intel_encoder *intel_encoder;
5873 struct drm_encoder *encoder;
5874 struct intel_connector *intel_connector;
5875
b14c5679 5876 intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
f0fec3f2 5877 if (!intel_dig_port)
457c52d8 5878 return false;
f0fec3f2 5879
08d9bc92 5880 intel_connector = intel_connector_alloc();
11aee0f6
SM
5881 if (!intel_connector)
5882 goto err_connector_alloc;
f0fec3f2
PZ
5883
5884 intel_encoder = &intel_dig_port->base;
5885 encoder = &intel_encoder->base;
5886
c39055b0
ACO
5887 if (drm_encoder_init(&dev_priv->drm, &intel_encoder->base,
5888 &intel_dp_enc_funcs, DRM_MODE_ENCODER_TMDS,
5889 "DP %c", port_name(port)))
893da0c9 5890 goto err_encoder_init;
f0fec3f2 5891
5bfe2ac0 5892 intel_encoder->compute_config = intel_dp_compute_config;
00c09d70 5893 intel_encoder->disable = intel_disable_dp;
00c09d70 5894 intel_encoder->get_hw_state = intel_dp_get_hw_state;
045ac3b5 5895 intel_encoder->get_config = intel_dp_get_config;
07f9cd0b 5896 intel_encoder->suspend = intel_dp_encoder_suspend;
920a14b2 5897 if (IS_CHERRYVIEW(dev_priv)) {
9197c88b 5898 intel_encoder->pre_pll_enable = chv_dp_pre_pll_enable;
e4a1d846
CML
5899 intel_encoder->pre_enable = chv_pre_enable_dp;
5900 intel_encoder->enable = vlv_enable_dp;
580d3811 5901 intel_encoder->post_disable = chv_post_disable_dp;
d6db995f 5902 intel_encoder->post_pll_disable = chv_dp_post_pll_disable;
11a914c2 5903 } else if (IS_VALLEYVIEW(dev_priv)) {
ecff4f3b 5904 intel_encoder->pre_pll_enable = vlv_dp_pre_pll_enable;
ab1f90f9
JN
5905 intel_encoder->pre_enable = vlv_pre_enable_dp;
5906 intel_encoder->enable = vlv_enable_dp;
49277c31 5907 intel_encoder->post_disable = vlv_post_disable_dp;
ab1f90f9 5908 } else {
ecff4f3b
JN
5909 intel_encoder->pre_enable = g4x_pre_enable_dp;
5910 intel_encoder->enable = g4x_enable_dp;
dd11bc10 5911 if (INTEL_GEN(dev_priv) >= 5)
08aff3fe 5912 intel_encoder->post_disable = ilk_post_disable_dp;
ab1f90f9 5913 }
f0fec3f2 5914
174edf1f 5915 intel_dig_port->port = port;
f0fec3f2 5916 intel_dig_port->dp.output_reg = output_reg;
ccb1a831 5917 intel_dig_port->max_lanes = 4;
f0fec3f2 5918
cca0502b 5919 intel_encoder->type = INTEL_OUTPUT_DP;
920a14b2 5920 if (IS_CHERRYVIEW(dev_priv)) {
882ec384
VS
5921 if (port == PORT_D)
5922 intel_encoder->crtc_mask = 1 << 2;
5923 else
5924 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
5925 } else {
5926 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
5927 }
bc079e8b 5928 intel_encoder->cloneable = 0;
03cdc1d4 5929 intel_encoder->port = port;
f0fec3f2 5930
13cf5504 5931 intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
5fcece80 5932 dev_priv->hotplug.irq_port[port] = intel_dig_port;
13cf5504 5933
11aee0f6
SM
5934 if (!intel_dp_init_connector(intel_dig_port, intel_connector))
5935 goto err_init_connector;
5936
457c52d8 5937 return true;
11aee0f6
SM
5938
5939err_init_connector:
5940 drm_encoder_cleanup(encoder);
893da0c9 5941err_encoder_init:
11aee0f6
SM
5942 kfree(intel_connector);
5943err_connector_alloc:
5944 kfree(intel_dig_port);
457c52d8 5945 return false;
f0fec3f2 5946}
0e32b39c
DA
5947
5948void intel_dp_mst_suspend(struct drm_device *dev)
5949{
fac5e23e 5950 struct drm_i915_private *dev_priv = to_i915(dev);
0e32b39c
DA
5951 int i;
5952
5953 /* disable MST */
5954 for (i = 0; i < I915_MAX_PORTS; i++) {
5fcece80 5955 struct intel_digital_port *intel_dig_port = dev_priv->hotplug.irq_port[i];
5aa56969
VS
5956
5957 if (!intel_dig_port || !intel_dig_port->dp.can_mst)
0e32b39c
DA
5958 continue;
5959
5aa56969
VS
5960 if (intel_dig_port->dp.is_mst)
5961 drm_dp_mst_topology_mgr_suspend(&intel_dig_port->dp.mst_mgr);
0e32b39c
DA
5962 }
5963}
5964
5965void intel_dp_mst_resume(struct drm_device *dev)
5966{
fac5e23e 5967 struct drm_i915_private *dev_priv = to_i915(dev);
0e32b39c
DA
5968 int i;
5969
5970 for (i = 0; i < I915_MAX_PORTS; i++) {
5fcece80 5971 struct intel_digital_port *intel_dig_port = dev_priv->hotplug.irq_port[i];
5aa56969 5972 int ret;
0e32b39c 5973
5aa56969
VS
5974 if (!intel_dig_port || !intel_dig_port->dp.can_mst)
5975 continue;
0e32b39c 5976
5aa56969
VS
5977 ret = drm_dp_mst_topology_mgr_resume(&intel_dig_port->dp.mst_mgr);
5978 if (ret)
5979 intel_dp_check_mst_status(&intel_dig_port->dp);
0e32b39c
DA
5980 }
5981}