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