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