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