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