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