drm/i915: Make i9xx_crtc_clock_get() work for PCH DPLLs
[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>
760285e7
DH
31#include <drm/drmP.h>
32#include <drm/drm_crtc.h>
33#include <drm/drm_crtc_helper.h>
34#include <drm/drm_edid.h>
a4fc5ed6 35#include "intel_drv.h"
760285e7 36#include <drm/i915_drm.h>
a4fc5ed6 37#include "i915_drv.h"
a4fc5ed6 38
a4fc5ed6
KP
39#define DP_LINK_CHECK_TIMEOUT (10 * 1000)
40
9dd4ffdf
CML
41struct dp_link_dpll {
42 int link_bw;
43 struct dpll dpll;
44};
45
46static const struct dp_link_dpll gen4_dpll[] = {
47 { DP_LINK_BW_1_62,
48 { .p1 = 2, .p2 = 10, .n = 2, .m1 = 23, .m2 = 8 } },
49 { DP_LINK_BW_2_7,
50 { .p1 = 1, .p2 = 10, .n = 1, .m1 = 14, .m2 = 2 } }
51};
52
53static const struct dp_link_dpll pch_dpll[] = {
54 { DP_LINK_BW_1_62,
55 { .p1 = 2, .p2 = 10, .n = 1, .m1 = 12, .m2 = 9 } },
56 { DP_LINK_BW_2_7,
57 { .p1 = 1, .p2 = 10, .n = 2, .m1 = 14, .m2 = 8 } }
58};
59
65ce4bf5
CML
60static const struct dp_link_dpll vlv_dpll[] = {
61 { DP_LINK_BW_1_62,
62 { .p1 = 3, .p2 = 2, .n = 5, .m1 = 5, .m2 = 3 } },
63 { DP_LINK_BW_2_7,
64 { .p1 = 2, .p2 = 2, .n = 1, .m1 = 2, .m2 = 27 } }
65};
66
cfcb0fc9
JB
67/**
68 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
69 * @intel_dp: DP struct
70 *
71 * If a CPU or PCH DP output is attached to an eDP panel, this function
72 * will return true, and false otherwise.
73 */
74static bool is_edp(struct intel_dp *intel_dp)
75{
da63a9f2
PZ
76 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
77
78 return intel_dig_port->base.type == INTEL_OUTPUT_EDP;
cfcb0fc9
JB
79}
80
68b4d824 81static struct drm_device *intel_dp_to_dev(struct intel_dp *intel_dp)
cfcb0fc9 82{
68b4d824
ID
83 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
84
85 return intel_dig_port->base.base.dev;
cfcb0fc9
JB
86}
87
df0e9248
CW
88static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
89{
fa90ecef 90 return enc_to_intel_dp(&intel_attached_encoder(connector)->base);
df0e9248
CW
91}
92
ea5b213a 93static void intel_dp_link_down(struct intel_dp *intel_dp);
a4fc5ed6 94
a4fc5ed6 95static int
ea5b213a 96intel_dp_max_link_bw(struct intel_dp *intel_dp)
a4fc5ed6 97{
7183dc29 98 int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
a4fc5ed6
KP
99
100 switch (max_link_bw) {
101 case DP_LINK_BW_1_62:
102 case DP_LINK_BW_2_7:
103 break;
d4eead50
ID
104 case DP_LINK_BW_5_4: /* 1.2 capable displays may advertise higher bw */
105 max_link_bw = DP_LINK_BW_2_7;
106 break;
a4fc5ed6 107 default:
d4eead50
ID
108 WARN(1, "invalid max DP link bw val %x, using 1.62Gbps\n",
109 max_link_bw);
a4fc5ed6
KP
110 max_link_bw = DP_LINK_BW_1_62;
111 break;
112 }
113 return max_link_bw;
114}
115
cd9dde44
AJ
116/*
117 * The units on the numbers in the next two are... bizarre. Examples will
118 * make it clearer; this one parallels an example in the eDP spec.
119 *
120 * intel_dp_max_data_rate for one lane of 2.7GHz evaluates as:
121 *
122 * 270000 * 1 * 8 / 10 == 216000
123 *
124 * The actual data capacity of that configuration is 2.16Gbit/s, so the
125 * units are decakilobits. ->clock in a drm_display_mode is in kilohertz -
126 * or equivalently, kilopixels per second - so for 1680x1050R it'd be
127 * 119000. At 18bpp that's 2142000 kilobits per second.
128 *
129 * Thus the strange-looking division by 10 in intel_dp_link_required, to
130 * get the result in decakilobits instead of kilobits.
131 */
132
a4fc5ed6 133static int
c898261c 134intel_dp_link_required(int pixel_clock, int bpp)
a4fc5ed6 135{
cd9dde44 136 return (pixel_clock * bpp + 9) / 10;
a4fc5ed6
KP
137}
138
fe27d53e
DA
139static int
140intel_dp_max_data_rate(int max_link_clock, int max_lanes)
141{
142 return (max_link_clock * max_lanes * 8) / 10;
143}
144
a4fc5ed6
KP
145static int
146intel_dp_mode_valid(struct drm_connector *connector,
147 struct drm_display_mode *mode)
148{
df0e9248 149 struct intel_dp *intel_dp = intel_attached_dp(connector);
dd06f90e
JN
150 struct intel_connector *intel_connector = to_intel_connector(connector);
151 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
36008365
DV
152 int target_clock = mode->clock;
153 int max_rate, mode_rate, max_lanes, max_link_clock;
a4fc5ed6 154
dd06f90e
JN
155 if (is_edp(intel_dp) && fixed_mode) {
156 if (mode->hdisplay > fixed_mode->hdisplay)
7de56f43
ZY
157 return MODE_PANEL;
158
dd06f90e 159 if (mode->vdisplay > fixed_mode->vdisplay)
7de56f43 160 return MODE_PANEL;
03afc4a2
DV
161
162 target_clock = fixed_mode->clock;
7de56f43
ZY
163 }
164
36008365
DV
165 max_link_clock = drm_dp_bw_code_to_link_rate(intel_dp_max_link_bw(intel_dp));
166 max_lanes = drm_dp_max_lane_count(intel_dp->dpcd);
167
168 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
169 mode_rate = intel_dp_link_required(target_clock, 18);
170
171 if (mode_rate > max_rate)
c4867936 172 return MODE_CLOCK_HIGH;
a4fc5ed6
KP
173
174 if (mode->clock < 10000)
175 return MODE_CLOCK_LOW;
176
0af78a2b
DV
177 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
178 return MODE_H_ILLEGAL;
179
a4fc5ed6
KP
180 return MODE_OK;
181}
182
183static uint32_t
184pack_aux(uint8_t *src, int src_bytes)
185{
186 int i;
187 uint32_t v = 0;
188
189 if (src_bytes > 4)
190 src_bytes = 4;
191 for (i = 0; i < src_bytes; i++)
192 v |= ((uint32_t) src[i]) << ((3-i) * 8);
193 return v;
194}
195
196static void
197unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
198{
199 int i;
200 if (dst_bytes > 4)
201 dst_bytes = 4;
202 for (i = 0; i < dst_bytes; i++)
203 dst[i] = src >> ((3-i) * 8);
204}
205
fb0f8fbf
KP
206/* hrawclock is 1/4 the FSB frequency */
207static int
208intel_hrawclk(struct drm_device *dev)
209{
210 struct drm_i915_private *dev_priv = dev->dev_private;
211 uint32_t clkcfg;
212
9473c8f4
VP
213 /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
214 if (IS_VALLEYVIEW(dev))
215 return 200;
216
fb0f8fbf
KP
217 clkcfg = I915_READ(CLKCFG);
218 switch (clkcfg & CLKCFG_FSB_MASK) {
219 case CLKCFG_FSB_400:
220 return 100;
221 case CLKCFG_FSB_533:
222 return 133;
223 case CLKCFG_FSB_667:
224 return 166;
225 case CLKCFG_FSB_800:
226 return 200;
227 case CLKCFG_FSB_1067:
228 return 266;
229 case CLKCFG_FSB_1333:
230 return 333;
231 /* these two are just a guess; one of them might be right */
232 case CLKCFG_FSB_1600:
233 case CLKCFG_FSB_1600_ALT:
234 return 400;
235 default:
236 return 133;
237 }
238}
239
bf13e81b
JN
240static void
241intel_dp_init_panel_power_sequencer(struct drm_device *dev,
242 struct intel_dp *intel_dp,
243 struct edp_power_seq *out);
244static void
245intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
246 struct intel_dp *intel_dp,
247 struct edp_power_seq *out);
248
249static enum pipe
250vlv_power_sequencer_pipe(struct intel_dp *intel_dp)
251{
252 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
253 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
254 struct drm_device *dev = intel_dig_port->base.base.dev;
255 struct drm_i915_private *dev_priv = dev->dev_private;
256 enum port port = intel_dig_port->port;
257 enum pipe pipe;
258
259 /* modeset should have pipe */
260 if (crtc)
261 return to_intel_crtc(crtc)->pipe;
262
263 /* init time, try to find a pipe with this port selected */
264 for (pipe = PIPE_A; pipe <= PIPE_B; pipe++) {
265 u32 port_sel = I915_READ(VLV_PIPE_PP_ON_DELAYS(pipe)) &
266 PANEL_PORT_SELECT_MASK;
267 if (port_sel == PANEL_PORT_SELECT_DPB_VLV && port == PORT_B)
268 return pipe;
269 if (port_sel == PANEL_PORT_SELECT_DPC_VLV && port == PORT_C)
270 return pipe;
271 }
272
273 /* shrug */
274 return PIPE_A;
275}
276
277static u32 _pp_ctrl_reg(struct intel_dp *intel_dp)
278{
279 struct drm_device *dev = intel_dp_to_dev(intel_dp);
280
281 if (HAS_PCH_SPLIT(dev))
282 return PCH_PP_CONTROL;
283 else
284 return VLV_PIPE_PP_CONTROL(vlv_power_sequencer_pipe(intel_dp));
285}
286
287static u32 _pp_stat_reg(struct intel_dp *intel_dp)
288{
289 struct drm_device *dev = intel_dp_to_dev(intel_dp);
290
291 if (HAS_PCH_SPLIT(dev))
292 return PCH_PP_STATUS;
293 else
294 return VLV_PIPE_PP_STATUS(vlv_power_sequencer_pipe(intel_dp));
295}
296
ebf33b18
KP
297static bool ironlake_edp_have_panel_power(struct intel_dp *intel_dp)
298{
30add22d 299 struct drm_device *dev = intel_dp_to_dev(intel_dp);
ebf33b18
KP
300 struct drm_i915_private *dev_priv = dev->dev_private;
301
bf13e81b 302 return (I915_READ(_pp_stat_reg(intel_dp)) & PP_ON) != 0;
ebf33b18
KP
303}
304
305static bool ironlake_edp_have_panel_vdd(struct intel_dp *intel_dp)
306{
30add22d 307 struct drm_device *dev = intel_dp_to_dev(intel_dp);
ebf33b18
KP
308 struct drm_i915_private *dev_priv = dev->dev_private;
309
bf13e81b 310 return (I915_READ(_pp_ctrl_reg(intel_dp)) & EDP_FORCE_VDD) != 0;
ebf33b18
KP
311}
312
9b984dae
KP
313static void
314intel_dp_check_edp(struct intel_dp *intel_dp)
315{
30add22d 316 struct drm_device *dev = intel_dp_to_dev(intel_dp);
9b984dae 317 struct drm_i915_private *dev_priv = dev->dev_private;
ebf33b18 318
9b984dae
KP
319 if (!is_edp(intel_dp))
320 return;
453c5420 321
ebf33b18 322 if (!ironlake_edp_have_panel_power(intel_dp) && !ironlake_edp_have_panel_vdd(intel_dp)) {
9b984dae
KP
323 WARN(1, "eDP powered off while attempting aux channel communication.\n");
324 DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
bf13e81b
JN
325 I915_READ(_pp_stat_reg(intel_dp)),
326 I915_READ(_pp_ctrl_reg(intel_dp)));
9b984dae
KP
327 }
328}
329
9ee32fea
DV
330static uint32_t
331intel_dp_aux_wait_done(struct intel_dp *intel_dp, bool has_aux_irq)
332{
333 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
334 struct drm_device *dev = intel_dig_port->base.base.dev;
335 struct drm_i915_private *dev_priv = dev->dev_private;
9ed35ab1 336 uint32_t ch_ctl = intel_dp->aux_ch_ctl_reg;
9ee32fea
DV
337 uint32_t status;
338 bool done;
339
ef04f00d 340#define C (((status = I915_READ_NOTRACE(ch_ctl)) & DP_AUX_CH_CTL_SEND_BUSY) == 0)
9ee32fea 341 if (has_aux_irq)
b18ac466 342 done = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
3598706b 343 msecs_to_jiffies_timeout(10));
9ee32fea
DV
344 else
345 done = wait_for_atomic(C, 10) == 0;
346 if (!done)
347 DRM_ERROR("dp aux hw did not signal timeout (has irq: %i)!\n",
348 has_aux_irq);
349#undef C
350
351 return status;
352}
353
bc86625a
CW
354static uint32_t get_aux_clock_divider(struct intel_dp *intel_dp,
355 int index)
a4fc5ed6 356{
174edf1f
PZ
357 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
358 struct drm_device *dev = intel_dig_port->base.base.dev;
a4fc5ed6 359 struct drm_i915_private *dev_priv = dev->dev_private;
9ee32fea 360
a4fc5ed6 361 /* The clock divider is based off the hrawclk,
fb0f8fbf
KP
362 * and would like to run at 2MHz. So, take the
363 * hrawclk value and divide by 2 and use that
6176b8f9
JB
364 *
365 * Note that PCH attached eDP panels should use a 125MHz input
366 * clock divider.
a4fc5ed6 367 */
a62d0834 368 if (IS_VALLEYVIEW(dev)) {
bc86625a 369 return index ? 0 : 100;
a62d0834 370 } else if (intel_dig_port->port == PORT_A) {
bc86625a
CW
371 if (index)
372 return 0;
affa9354 373 if (HAS_DDI(dev))
bc86625a 374 return DIV_ROUND_CLOSEST(intel_ddi_get_cdclk_freq(dev_priv), 2000);
9473c8f4 375 else if (IS_GEN6(dev) || IS_GEN7(dev))
b84a1cf8 376 return 200; /* SNB & IVB eDP input clock at 400Mhz */
e3421a18 377 else
b84a1cf8 378 return 225; /* eDP input clock at 450Mhz */
2c55c336
JN
379 } else if (dev_priv->pch_id == INTEL_PCH_LPT_DEVICE_ID_TYPE) {
380 /* Workaround for non-ULT HSW */
bc86625a
CW
381 switch (index) {
382 case 0: return 63;
383 case 1: return 72;
384 default: return 0;
385 }
2c55c336 386 } else if (HAS_PCH_SPLIT(dev)) {
bc86625a 387 return index ? 0 : DIV_ROUND_UP(intel_pch_rawclk(dev), 2);
2c55c336 388 } else {
bc86625a 389 return index ? 0 :intel_hrawclk(dev) / 2;
2c55c336 390 }
b84a1cf8
RV
391}
392
393static int
394intel_dp_aux_ch(struct intel_dp *intel_dp,
395 uint8_t *send, int send_bytes,
396 uint8_t *recv, int recv_size)
397{
398 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
399 struct drm_device *dev = intel_dig_port->base.base.dev;
400 struct drm_i915_private *dev_priv = dev->dev_private;
401 uint32_t ch_ctl = intel_dp->aux_ch_ctl_reg;
402 uint32_t ch_data = ch_ctl + 4;
bc86625a 403 uint32_t aux_clock_divider;
b84a1cf8
RV
404 int i, ret, recv_bytes;
405 uint32_t status;
bc86625a 406 int try, precharge, clock = 0;
b84a1cf8
RV
407 bool has_aux_irq = INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev);
408
409 /* dp aux is extremely sensitive to irq latency, hence request the
410 * lowest possible wakeup latency and so prevent the cpu from going into
411 * deep sleep states.
412 */
413 pm_qos_update_request(&dev_priv->pm_qos, 0);
414
415 intel_dp_check_edp(intel_dp);
5eb08b69 416
6b4e0a93
DV
417 if (IS_GEN6(dev))
418 precharge = 3;
419 else
420 precharge = 5;
421
c67a470b
PZ
422 intel_aux_display_runtime_get(dev_priv);
423
11bee43e
JB
424 /* Try to wait for any previous AUX channel activity */
425 for (try = 0; try < 3; try++) {
ef04f00d 426 status = I915_READ_NOTRACE(ch_ctl);
11bee43e
JB
427 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
428 break;
429 msleep(1);
430 }
431
432 if (try == 3) {
433 WARN(1, "dp_aux_ch not started status 0x%08x\n",
434 I915_READ(ch_ctl));
9ee32fea
DV
435 ret = -EBUSY;
436 goto out;
4f7f7b7e
CW
437 }
438
bc86625a
CW
439 while ((aux_clock_divider = get_aux_clock_divider(intel_dp, clock++))) {
440 /* Must try at least 3 times according to DP spec */
441 for (try = 0; try < 5; try++) {
442 /* Load the send data into the aux channel data registers */
443 for (i = 0; i < send_bytes; i += 4)
444 I915_WRITE(ch_data + i,
445 pack_aux(send + i, send_bytes - i));
446
447 /* Send the command and wait for it to complete */
448 I915_WRITE(ch_ctl,
449 DP_AUX_CH_CTL_SEND_BUSY |
450 (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
451 DP_AUX_CH_CTL_TIME_OUT_400us |
452 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
453 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
454 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
455 DP_AUX_CH_CTL_DONE |
456 DP_AUX_CH_CTL_TIME_OUT_ERROR |
457 DP_AUX_CH_CTL_RECEIVE_ERROR);
458
459 status = intel_dp_aux_wait_done(intel_dp, has_aux_irq);
460
461 /* Clear done status and any errors */
462 I915_WRITE(ch_ctl,
463 status |
464 DP_AUX_CH_CTL_DONE |
465 DP_AUX_CH_CTL_TIME_OUT_ERROR |
466 DP_AUX_CH_CTL_RECEIVE_ERROR);
467
468 if (status & (DP_AUX_CH_CTL_TIME_OUT_ERROR |
469 DP_AUX_CH_CTL_RECEIVE_ERROR))
470 continue;
471 if (status & DP_AUX_CH_CTL_DONE)
472 break;
473 }
4f7f7b7e 474 if (status & DP_AUX_CH_CTL_DONE)
a4fc5ed6
KP
475 break;
476 }
477
a4fc5ed6 478 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
1ae8c0a5 479 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
9ee32fea
DV
480 ret = -EBUSY;
481 goto out;
a4fc5ed6
KP
482 }
483
484 /* Check for timeout or receive error.
485 * Timeouts occur when the sink is not connected
486 */
a5b3da54 487 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
1ae8c0a5 488 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
9ee32fea
DV
489 ret = -EIO;
490 goto out;
a5b3da54 491 }
1ae8c0a5
KP
492
493 /* Timeouts occur when the device isn't connected, so they're
494 * "normal" -- don't fill the kernel log with these */
a5b3da54 495 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
28c97730 496 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
9ee32fea
DV
497 ret = -ETIMEDOUT;
498 goto out;
a4fc5ed6
KP
499 }
500
501 /* Unload any bytes sent back from the other side */
502 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
503 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
a4fc5ed6
KP
504 if (recv_bytes > recv_size)
505 recv_bytes = recv_size;
0206e353 506
4f7f7b7e
CW
507 for (i = 0; i < recv_bytes; i += 4)
508 unpack_aux(I915_READ(ch_data + i),
509 recv + i, recv_bytes - i);
a4fc5ed6 510
9ee32fea
DV
511 ret = recv_bytes;
512out:
513 pm_qos_update_request(&dev_priv->pm_qos, PM_QOS_DEFAULT_VALUE);
c67a470b 514 intel_aux_display_runtime_put(dev_priv);
9ee32fea
DV
515
516 return ret;
a4fc5ed6
KP
517}
518
519/* Write data to the aux channel in native mode */
520static int
ea5b213a 521intel_dp_aux_native_write(struct intel_dp *intel_dp,
a4fc5ed6
KP
522 uint16_t address, uint8_t *send, int send_bytes)
523{
524 int ret;
525 uint8_t msg[20];
526 int msg_bytes;
527 uint8_t ack;
528
9b984dae 529 intel_dp_check_edp(intel_dp);
a4fc5ed6
KP
530 if (send_bytes > 16)
531 return -1;
532 msg[0] = AUX_NATIVE_WRITE << 4;
533 msg[1] = address >> 8;
eebc863e 534 msg[2] = address & 0xff;
a4fc5ed6
KP
535 msg[3] = send_bytes - 1;
536 memcpy(&msg[4], send, send_bytes);
537 msg_bytes = send_bytes + 4;
538 for (;;) {
ea5b213a 539 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
a4fc5ed6
KP
540 if (ret < 0)
541 return ret;
542 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
543 break;
544 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
545 udelay(100);
546 else
a5b3da54 547 return -EIO;
a4fc5ed6
KP
548 }
549 return send_bytes;
550}
551
552/* Write a single byte to the aux channel in native mode */
553static int
ea5b213a 554intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
a4fc5ed6
KP
555 uint16_t address, uint8_t byte)
556{
ea5b213a 557 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
a4fc5ed6
KP
558}
559
560/* read bytes from a native aux channel */
561static int
ea5b213a 562intel_dp_aux_native_read(struct intel_dp *intel_dp,
a4fc5ed6
KP
563 uint16_t address, uint8_t *recv, int recv_bytes)
564{
565 uint8_t msg[4];
566 int msg_bytes;
567 uint8_t reply[20];
568 int reply_bytes;
569 uint8_t ack;
570 int ret;
571
9b984dae 572 intel_dp_check_edp(intel_dp);
a4fc5ed6
KP
573 msg[0] = AUX_NATIVE_READ << 4;
574 msg[1] = address >> 8;
575 msg[2] = address & 0xff;
576 msg[3] = recv_bytes - 1;
577
578 msg_bytes = 4;
579 reply_bytes = recv_bytes + 1;
580
581 for (;;) {
ea5b213a 582 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
a4fc5ed6 583 reply, reply_bytes);
a5b3da54
KP
584 if (ret == 0)
585 return -EPROTO;
586 if (ret < 0)
a4fc5ed6
KP
587 return ret;
588 ack = reply[0];
589 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
590 memcpy(recv, reply + 1, ret - 1);
591 return ret - 1;
592 }
593 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
594 udelay(100);
595 else
a5b3da54 596 return -EIO;
a4fc5ed6
KP
597 }
598}
599
600static int
ab2c0672
DA
601intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
602 uint8_t write_byte, uint8_t *read_byte)
a4fc5ed6 603{
ab2c0672 604 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
ea5b213a
CW
605 struct intel_dp *intel_dp = container_of(adapter,
606 struct intel_dp,
607 adapter);
ab2c0672
DA
608 uint16_t address = algo_data->address;
609 uint8_t msg[5];
610 uint8_t reply[2];
8316f337 611 unsigned retry;
ab2c0672
DA
612 int msg_bytes;
613 int reply_bytes;
614 int ret;
615
9b984dae 616 intel_dp_check_edp(intel_dp);
ab2c0672
DA
617 /* Set up the command byte */
618 if (mode & MODE_I2C_READ)
619 msg[0] = AUX_I2C_READ << 4;
620 else
621 msg[0] = AUX_I2C_WRITE << 4;
622
623 if (!(mode & MODE_I2C_STOP))
624 msg[0] |= AUX_I2C_MOT << 4;
a4fc5ed6 625
ab2c0672
DA
626 msg[1] = address >> 8;
627 msg[2] = address;
628
629 switch (mode) {
630 case MODE_I2C_WRITE:
631 msg[3] = 0;
632 msg[4] = write_byte;
633 msg_bytes = 5;
634 reply_bytes = 1;
635 break;
636 case MODE_I2C_READ:
637 msg[3] = 0;
638 msg_bytes = 4;
639 reply_bytes = 2;
640 break;
641 default:
642 msg_bytes = 3;
643 reply_bytes = 1;
644 break;
645 }
646
8316f337
DF
647 for (retry = 0; retry < 5; retry++) {
648 ret = intel_dp_aux_ch(intel_dp,
649 msg, msg_bytes,
650 reply, reply_bytes);
ab2c0672 651 if (ret < 0) {
3ff99164 652 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
ab2c0672
DA
653 return ret;
654 }
8316f337
DF
655
656 switch (reply[0] & AUX_NATIVE_REPLY_MASK) {
657 case AUX_NATIVE_REPLY_ACK:
658 /* I2C-over-AUX Reply field is only valid
659 * when paired with AUX ACK.
660 */
661 break;
662 case AUX_NATIVE_REPLY_NACK:
663 DRM_DEBUG_KMS("aux_ch native nack\n");
664 return -EREMOTEIO;
665 case AUX_NATIVE_REPLY_DEFER:
666 udelay(100);
667 continue;
668 default:
669 DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
670 reply[0]);
671 return -EREMOTEIO;
672 }
673
ab2c0672
DA
674 switch (reply[0] & AUX_I2C_REPLY_MASK) {
675 case AUX_I2C_REPLY_ACK:
676 if (mode == MODE_I2C_READ) {
677 *read_byte = reply[1];
678 }
679 return reply_bytes - 1;
680 case AUX_I2C_REPLY_NACK:
8316f337 681 DRM_DEBUG_KMS("aux_i2c nack\n");
ab2c0672
DA
682 return -EREMOTEIO;
683 case AUX_I2C_REPLY_DEFER:
8316f337 684 DRM_DEBUG_KMS("aux_i2c defer\n");
ab2c0672
DA
685 udelay(100);
686 break;
687 default:
8316f337 688 DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
ab2c0672
DA
689 return -EREMOTEIO;
690 }
691 }
8316f337
DF
692
693 DRM_ERROR("too many retries, giving up\n");
694 return -EREMOTEIO;
a4fc5ed6
KP
695}
696
697static int
ea5b213a 698intel_dp_i2c_init(struct intel_dp *intel_dp,
55f78c43 699 struct intel_connector *intel_connector, const char *name)
a4fc5ed6 700{
0b5c541b
KP
701 int ret;
702
d54e9d28 703 DRM_DEBUG_KMS("i2c_init %s\n", name);
ea5b213a
CW
704 intel_dp->algo.running = false;
705 intel_dp->algo.address = 0;
706 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
707
0206e353 708 memset(&intel_dp->adapter, '\0', sizeof(intel_dp->adapter));
ea5b213a
CW
709 intel_dp->adapter.owner = THIS_MODULE;
710 intel_dp->adapter.class = I2C_CLASS_DDC;
0206e353 711 strncpy(intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
ea5b213a
CW
712 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
713 intel_dp->adapter.algo_data = &intel_dp->algo;
714 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
715
0b5c541b
KP
716 ironlake_edp_panel_vdd_on(intel_dp);
717 ret = i2c_dp_aux_add_bus(&intel_dp->adapter);
bd943159 718 ironlake_edp_panel_vdd_off(intel_dp, false);
0b5c541b 719 return ret;
a4fc5ed6
KP
720}
721
c6bb3538
DV
722static void
723intel_dp_set_clock(struct intel_encoder *encoder,
724 struct intel_crtc_config *pipe_config, int link_bw)
725{
726 struct drm_device *dev = encoder->base.dev;
9dd4ffdf
CML
727 const struct dp_link_dpll *divisor = NULL;
728 int i, count = 0;
c6bb3538
DV
729
730 if (IS_G4X(dev)) {
9dd4ffdf
CML
731 divisor = gen4_dpll;
732 count = ARRAY_SIZE(gen4_dpll);
c6bb3538
DV
733 } else if (IS_HASWELL(dev)) {
734 /* Haswell has special-purpose DP DDI clocks. */
735 } else if (HAS_PCH_SPLIT(dev)) {
9dd4ffdf
CML
736 divisor = pch_dpll;
737 count = ARRAY_SIZE(pch_dpll);
c6bb3538 738 } else if (IS_VALLEYVIEW(dev)) {
65ce4bf5
CML
739 divisor = vlv_dpll;
740 count = ARRAY_SIZE(vlv_dpll);
c6bb3538 741 }
9dd4ffdf
CML
742
743 if (divisor && count) {
744 for (i = 0; i < count; i++) {
745 if (link_bw == divisor[i].link_bw) {
746 pipe_config->dpll = divisor[i].dpll;
747 pipe_config->clock_set = true;
748 break;
749 }
750 }
751 }
c6bb3538
DV
752}
753
00c09d70 754bool
5bfe2ac0
DV
755intel_dp_compute_config(struct intel_encoder *encoder,
756 struct intel_crtc_config *pipe_config)
a4fc5ed6 757{
5bfe2ac0 758 struct drm_device *dev = encoder->base.dev;
36008365 759 struct drm_i915_private *dev_priv = dev->dev_private;
5bfe2ac0 760 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
5bfe2ac0 761 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
bc7d38a4 762 enum port port = dp_to_dig_port(intel_dp)->port;
2dd24552 763 struct intel_crtc *intel_crtc = encoder->new_crtc;
dd06f90e 764 struct intel_connector *intel_connector = intel_dp->attached_connector;
a4fc5ed6 765 int lane_count, clock;
397fe157 766 int max_lane_count = drm_dp_max_lane_count(intel_dp->dpcd);
ea5b213a 767 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
083f9560 768 int bpp, mode_rate;
a4fc5ed6 769 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
ff9a6750 770 int link_avail, link_clock;
a4fc5ed6 771
bc7d38a4 772 if (HAS_PCH_SPLIT(dev) && !HAS_DDI(dev) && port != PORT_A)
5bfe2ac0
DV
773 pipe_config->has_pch_encoder = true;
774
03afc4a2 775 pipe_config->has_dp_encoder = true;
a4fc5ed6 776
dd06f90e
JN
777 if (is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
778 intel_fixed_panel_mode(intel_connector->panel.fixed_mode,
779 adjusted_mode);
2dd24552
JB
780 if (!HAS_PCH_SPLIT(dev))
781 intel_gmch_panel_fitting(intel_crtc, pipe_config,
782 intel_connector->panel.fitting_mode);
783 else
b074cec8
JB
784 intel_pch_panel_fitting(intel_crtc, pipe_config,
785 intel_connector->panel.fitting_mode);
0d3a1bee
ZY
786 }
787
cb1793ce 788 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
0af78a2b
DV
789 return false;
790
083f9560
DV
791 DRM_DEBUG_KMS("DP link computation with max lane count %i "
792 "max bw %02x pixel clock %iKHz\n",
71244653 793 max_lane_count, bws[max_clock], adjusted_mode->clock);
083f9560 794
36008365
DV
795 /* Walk through all bpp values. Luckily they're all nicely spaced with 2
796 * bpc in between. */
3e7ca985 797 bpp = pipe_config->pipe_bpp;
7984211e
ID
798 if (is_edp(intel_dp) && dev_priv->vbt.edp_bpp) {
799 DRM_DEBUG_KMS("clamping bpp for eDP panel to BIOS-provided %i\n",
800 dev_priv->vbt.edp_bpp);
e1b73cba 801 bpp = min_t(int, bpp, dev_priv->vbt.edp_bpp);
7984211e 802 }
657445fe 803
36008365 804 for (; bpp >= 6*3; bpp -= 2*3) {
ff9a6750 805 mode_rate = intel_dp_link_required(adjusted_mode->clock, bpp);
36008365
DV
806
807 for (clock = 0; clock <= max_clock; clock++) {
808 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
809 link_clock = drm_dp_bw_code_to_link_rate(bws[clock]);
810 link_avail = intel_dp_max_data_rate(link_clock,
811 lane_count);
812
813 if (mode_rate <= link_avail) {
814 goto found;
815 }
816 }
817 }
818 }
c4867936 819
36008365 820 return false;
3685a8f3 821
36008365 822found:
55bc60db
VS
823 if (intel_dp->color_range_auto) {
824 /*
825 * See:
826 * CEA-861-E - 5.1 Default Encoding Parameters
827 * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
828 */
18316c8c 829 if (bpp != 18 && drm_match_cea_mode(adjusted_mode) > 1)
55bc60db
VS
830 intel_dp->color_range = DP_COLOR_RANGE_16_235;
831 else
832 intel_dp->color_range = 0;
833 }
834
3685a8f3 835 if (intel_dp->color_range)
50f3b016 836 pipe_config->limited_color_range = true;
a4fc5ed6 837
36008365
DV
838 intel_dp->link_bw = bws[clock];
839 intel_dp->lane_count = lane_count;
657445fe 840 pipe_config->pipe_bpp = bpp;
ff9a6750 841 pipe_config->port_clock = drm_dp_bw_code_to_link_rate(intel_dp->link_bw);
a4fc5ed6 842
36008365
DV
843 DRM_DEBUG_KMS("DP link bw %02x lane count %d clock %d bpp %d\n",
844 intel_dp->link_bw, intel_dp->lane_count,
ff9a6750 845 pipe_config->port_clock, bpp);
36008365
DV
846 DRM_DEBUG_KMS("DP link bw required %i available %i\n",
847 mode_rate, link_avail);
a4fc5ed6 848
03afc4a2 849 intel_link_compute_m_n(bpp, lane_count,
ff9a6750 850 adjusted_mode->clock, pipe_config->port_clock,
03afc4a2 851 &pipe_config->dp_m_n);
9d1a455b 852
c6bb3538
DV
853 intel_dp_set_clock(encoder, pipe_config, intel_dp->link_bw);
854
03afc4a2 855 return true;
a4fc5ed6
KP
856}
857
247d89f6
PZ
858void intel_dp_init_link_config(struct intel_dp *intel_dp)
859{
860 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
861 intel_dp->link_configuration[0] = intel_dp->link_bw;
862 intel_dp->link_configuration[1] = intel_dp->lane_count;
863 intel_dp->link_configuration[8] = DP_SET_ANSI_8B10B;
864 /*
865 * Check for DPCD version > 1.1 and enhanced framing support
866 */
867 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
868 (intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)) {
869 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
870 }
871}
872
7c62a164 873static void ironlake_set_pll_cpu_edp(struct intel_dp *intel_dp)
ea9b6006 874{
7c62a164
DV
875 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
876 struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
877 struct drm_device *dev = crtc->base.dev;
ea9b6006
DV
878 struct drm_i915_private *dev_priv = dev->dev_private;
879 u32 dpa_ctl;
880
ff9a6750 881 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", crtc->config.port_clock);
ea9b6006
DV
882 dpa_ctl = I915_READ(DP_A);
883 dpa_ctl &= ~DP_PLL_FREQ_MASK;
884
ff9a6750 885 if (crtc->config.port_clock == 162000) {
1ce17038
DV
886 /* For a long time we've carried around a ILK-DevA w/a for the
887 * 160MHz clock. If we're really unlucky, it's still required.
888 */
889 DRM_DEBUG_KMS("160MHz cpu eDP clock, might need ilk devA w/a\n");
ea9b6006 890 dpa_ctl |= DP_PLL_FREQ_160MHZ;
7c62a164 891 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
ea9b6006
DV
892 } else {
893 dpa_ctl |= DP_PLL_FREQ_270MHZ;
7c62a164 894 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
ea9b6006 895 }
1ce17038 896
ea9b6006
DV
897 I915_WRITE(DP_A, dpa_ctl);
898
899 POSTING_READ(DP_A);
900 udelay(500);
901}
902
b934223d 903static void intel_dp_mode_set(struct intel_encoder *encoder)
a4fc5ed6 904{
b934223d 905 struct drm_device *dev = encoder->base.dev;
417e822d 906 struct drm_i915_private *dev_priv = dev->dev_private;
b934223d 907 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
bc7d38a4 908 enum port port = dp_to_dig_port(intel_dp)->port;
b934223d
DV
909 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
910 struct drm_display_mode *adjusted_mode = &crtc->config.adjusted_mode;
a4fc5ed6 911
417e822d 912 /*
1a2eb460 913 * There are four kinds of DP registers:
417e822d
KP
914 *
915 * IBX PCH
1a2eb460
KP
916 * SNB CPU
917 * IVB CPU
417e822d
KP
918 * CPT PCH
919 *
920 * IBX PCH and CPU are the same for almost everything,
921 * except that the CPU DP PLL is configured in this
922 * register
923 *
924 * CPT PCH is quite different, having many bits moved
925 * to the TRANS_DP_CTL register instead. That
926 * configuration happens (oddly) in ironlake_pch_enable
927 */
9c9e7927 928
417e822d
KP
929 /* Preserve the BIOS-computed detected bit. This is
930 * supposed to be read-only.
931 */
932 intel_dp->DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
a4fc5ed6 933
417e822d 934 /* Handle DP bits in common between all three register formats */
417e822d 935 intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
17aa6be9 936 intel_dp->DP |= DP_PORT_WIDTH(intel_dp->lane_count);
a4fc5ed6 937
e0dac65e
WF
938 if (intel_dp->has_audio) {
939 DRM_DEBUG_DRIVER("Enabling DP audio on pipe %c\n",
7c62a164 940 pipe_name(crtc->pipe));
ea5b213a 941 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
b934223d 942 intel_write_eld(&encoder->base, adjusted_mode);
e0dac65e 943 }
247d89f6
PZ
944
945 intel_dp_init_link_config(intel_dp);
a4fc5ed6 946
417e822d 947 /* Split out the IBX/CPU vs CPT settings */
32f9d658 948
bc7d38a4 949 if (port == PORT_A && IS_GEN7(dev) && !IS_VALLEYVIEW(dev)) {
1a2eb460
KP
950 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
951 intel_dp->DP |= DP_SYNC_HS_HIGH;
952 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
953 intel_dp->DP |= DP_SYNC_VS_HIGH;
954 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
955
956 if (intel_dp->link_configuration[1] & DP_LANE_COUNT_ENHANCED_FRAME_EN)
957 intel_dp->DP |= DP_ENHANCED_FRAMING;
958
7c62a164 959 intel_dp->DP |= crtc->pipe << 29;
bc7d38a4 960 } else if (!HAS_PCH_CPT(dev) || port == PORT_A) {
b2634017 961 if (!HAS_PCH_SPLIT(dev) && !IS_VALLEYVIEW(dev))
3685a8f3 962 intel_dp->DP |= intel_dp->color_range;
417e822d
KP
963
964 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
965 intel_dp->DP |= DP_SYNC_HS_HIGH;
966 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
967 intel_dp->DP |= DP_SYNC_VS_HIGH;
968 intel_dp->DP |= DP_LINK_TRAIN_OFF;
969
970 if (intel_dp->link_configuration[1] & DP_LANE_COUNT_ENHANCED_FRAME_EN)
971 intel_dp->DP |= DP_ENHANCED_FRAMING;
972
7c62a164 973 if (crtc->pipe == 1)
417e822d 974 intel_dp->DP |= DP_PIPEB_SELECT;
417e822d
KP
975 } else {
976 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
32f9d658 977 }
ea9b6006 978
bc7d38a4 979 if (port == PORT_A && !IS_VALLEYVIEW(dev))
7c62a164 980 ironlake_set_pll_cpu_edp(intel_dp);
a4fc5ed6
KP
981}
982
99ea7127
KP
983#define IDLE_ON_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
984#define IDLE_ON_VALUE (PP_ON | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_ON_IDLE)
985
986#define IDLE_OFF_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
987#define IDLE_OFF_VALUE (0 | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
988
989#define IDLE_CYCLE_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
990#define IDLE_CYCLE_VALUE (0 | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
991
992static void ironlake_wait_panel_status(struct intel_dp *intel_dp,
993 u32 mask,
994 u32 value)
bd943159 995{
30add22d 996 struct drm_device *dev = intel_dp_to_dev(intel_dp);
99ea7127 997 struct drm_i915_private *dev_priv = dev->dev_private;
453c5420
JB
998 u32 pp_stat_reg, pp_ctrl_reg;
999
bf13e81b
JN
1000 pp_stat_reg = _pp_stat_reg(intel_dp);
1001 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
32ce697c 1002
99ea7127 1003 DRM_DEBUG_KMS("mask %08x value %08x status %08x control %08x\n",
453c5420
JB
1004 mask, value,
1005 I915_READ(pp_stat_reg),
1006 I915_READ(pp_ctrl_reg));
32ce697c 1007
453c5420 1008 if (_wait_for((I915_READ(pp_stat_reg) & mask) == value, 5000, 10)) {
99ea7127 1009 DRM_ERROR("Panel status timeout: status %08x control %08x\n",
453c5420
JB
1010 I915_READ(pp_stat_reg),
1011 I915_READ(pp_ctrl_reg));
32ce697c 1012 }
99ea7127 1013}
32ce697c 1014
99ea7127
KP
1015static void ironlake_wait_panel_on(struct intel_dp *intel_dp)
1016{
1017 DRM_DEBUG_KMS("Wait for panel power on\n");
1018 ironlake_wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
bd943159
KP
1019}
1020
99ea7127
KP
1021static void ironlake_wait_panel_off(struct intel_dp *intel_dp)
1022{
1023 DRM_DEBUG_KMS("Wait for panel power off time\n");
1024 ironlake_wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
1025}
1026
1027static void ironlake_wait_panel_power_cycle(struct intel_dp *intel_dp)
1028{
1029 DRM_DEBUG_KMS("Wait for panel power cycle\n");
1030 ironlake_wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
1031}
1032
1033
832dd3c1
KP
1034/* Read the current pp_control value, unlocking the register if it
1035 * is locked
1036 */
1037
453c5420 1038static u32 ironlake_get_pp_control(struct intel_dp *intel_dp)
832dd3c1 1039{
453c5420
JB
1040 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1041 struct drm_i915_private *dev_priv = dev->dev_private;
1042 u32 control;
832dd3c1 1043
bf13e81b 1044 control = I915_READ(_pp_ctrl_reg(intel_dp));
832dd3c1
KP
1045 control &= ~PANEL_UNLOCK_MASK;
1046 control |= PANEL_UNLOCK_REGS;
1047 return control;
bd943159
KP
1048}
1049
82a4d9c0 1050void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp)
5d613501 1051{
30add22d 1052 struct drm_device *dev = intel_dp_to_dev(intel_dp);
5d613501
JB
1053 struct drm_i915_private *dev_priv = dev->dev_private;
1054 u32 pp;
453c5420 1055 u32 pp_stat_reg, pp_ctrl_reg;
5d613501 1056
97af61f5
KP
1057 if (!is_edp(intel_dp))
1058 return;
f01eca2e 1059 DRM_DEBUG_KMS("Turn eDP VDD on\n");
5d613501 1060
bd943159
KP
1061 WARN(intel_dp->want_panel_vdd,
1062 "eDP VDD already requested on\n");
1063
1064 intel_dp->want_panel_vdd = true;
99ea7127 1065
bd943159
KP
1066 if (ironlake_edp_have_panel_vdd(intel_dp)) {
1067 DRM_DEBUG_KMS("eDP VDD already on\n");
1068 return;
1069 }
1070
99ea7127
KP
1071 if (!ironlake_edp_have_panel_power(intel_dp))
1072 ironlake_wait_panel_power_cycle(intel_dp);
1073
453c5420 1074 pp = ironlake_get_pp_control(intel_dp);
5d613501 1075 pp |= EDP_FORCE_VDD;
ebf33b18 1076
bf13e81b
JN
1077 pp_stat_reg = _pp_stat_reg(intel_dp);
1078 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420
JB
1079
1080 I915_WRITE(pp_ctrl_reg, pp);
1081 POSTING_READ(pp_ctrl_reg);
1082 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
1083 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
ebf33b18
KP
1084 /*
1085 * If the panel wasn't on, delay before accessing aux channel
1086 */
1087 if (!ironlake_edp_have_panel_power(intel_dp)) {
bd943159 1088 DRM_DEBUG_KMS("eDP was not running\n");
f01eca2e 1089 msleep(intel_dp->panel_power_up_delay);
f01eca2e 1090 }
5d613501
JB
1091}
1092
bd943159 1093static void ironlake_panel_vdd_off_sync(struct intel_dp *intel_dp)
5d613501 1094{
30add22d 1095 struct drm_device *dev = intel_dp_to_dev(intel_dp);
5d613501
JB
1096 struct drm_i915_private *dev_priv = dev->dev_private;
1097 u32 pp;
453c5420 1098 u32 pp_stat_reg, pp_ctrl_reg;
5d613501 1099
a0e99e68
DV
1100 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
1101
bd943159 1102 if (!intel_dp->want_panel_vdd && ironlake_edp_have_panel_vdd(intel_dp)) {
453c5420 1103 pp = ironlake_get_pp_control(intel_dp);
bd943159 1104 pp &= ~EDP_FORCE_VDD;
bd943159 1105
bf13e81b
JN
1106 pp_stat_reg = _pp_ctrl_reg(intel_dp);
1107 pp_ctrl_reg = _pp_stat_reg(intel_dp);
453c5420
JB
1108
1109 I915_WRITE(pp_ctrl_reg, pp);
1110 POSTING_READ(pp_ctrl_reg);
99ea7127 1111
453c5420
JB
1112 /* Make sure sequencer is idle before allowing subsequent activity */
1113 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
1114 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
99ea7127 1115 msleep(intel_dp->panel_power_down_delay);
bd943159
KP
1116 }
1117}
5d613501 1118
bd943159
KP
1119static void ironlake_panel_vdd_work(struct work_struct *__work)
1120{
1121 struct intel_dp *intel_dp = container_of(to_delayed_work(__work),
1122 struct intel_dp, panel_vdd_work);
30add22d 1123 struct drm_device *dev = intel_dp_to_dev(intel_dp);
bd943159 1124
627f7675 1125 mutex_lock(&dev->mode_config.mutex);
bd943159 1126 ironlake_panel_vdd_off_sync(intel_dp);
627f7675 1127 mutex_unlock(&dev->mode_config.mutex);
bd943159
KP
1128}
1129
82a4d9c0 1130void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
bd943159 1131{
97af61f5
KP
1132 if (!is_edp(intel_dp))
1133 return;
5d613501 1134
bd943159
KP
1135 DRM_DEBUG_KMS("Turn eDP VDD off %d\n", intel_dp->want_panel_vdd);
1136 WARN(!intel_dp->want_panel_vdd, "eDP VDD not forced on");
f2e8b18a 1137
bd943159
KP
1138 intel_dp->want_panel_vdd = false;
1139
1140 if (sync) {
1141 ironlake_panel_vdd_off_sync(intel_dp);
1142 } else {
1143 /*
1144 * Queue the timer to fire a long
1145 * time from now (relative to the power down delay)
1146 * to keep the panel power up across a sequence of operations
1147 */
1148 schedule_delayed_work(&intel_dp->panel_vdd_work,
1149 msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5));
1150 }
5d613501
JB
1151}
1152
82a4d9c0 1153void ironlake_edp_panel_on(struct intel_dp *intel_dp)
9934c132 1154{
30add22d 1155 struct drm_device *dev = intel_dp_to_dev(intel_dp);
9934c132 1156 struct drm_i915_private *dev_priv = dev->dev_private;
99ea7127 1157 u32 pp;
453c5420 1158 u32 pp_ctrl_reg;
9934c132 1159
97af61f5 1160 if (!is_edp(intel_dp))
bd943159 1161 return;
99ea7127
KP
1162
1163 DRM_DEBUG_KMS("Turn eDP power on\n");
1164
1165 if (ironlake_edp_have_panel_power(intel_dp)) {
1166 DRM_DEBUG_KMS("eDP power already on\n");
7d639f35 1167 return;
99ea7127 1168 }
9934c132 1169
99ea7127 1170 ironlake_wait_panel_power_cycle(intel_dp);
37c6c9b0 1171
bf13e81b 1172 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420 1173 pp = ironlake_get_pp_control(intel_dp);
05ce1a49
KP
1174 if (IS_GEN5(dev)) {
1175 /* ILK workaround: disable reset around power sequence */
1176 pp &= ~PANEL_POWER_RESET;
bf13e81b
JN
1177 I915_WRITE(pp_ctrl_reg, pp);
1178 POSTING_READ(pp_ctrl_reg);
05ce1a49 1179 }
37c6c9b0 1180
1c0ae80a 1181 pp |= POWER_TARGET_ON;
99ea7127
KP
1182 if (!IS_GEN5(dev))
1183 pp |= PANEL_POWER_RESET;
1184
453c5420
JB
1185 I915_WRITE(pp_ctrl_reg, pp);
1186 POSTING_READ(pp_ctrl_reg);
9934c132 1187
99ea7127 1188 ironlake_wait_panel_on(intel_dp);
9934c132 1189
05ce1a49
KP
1190 if (IS_GEN5(dev)) {
1191 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
bf13e81b
JN
1192 I915_WRITE(pp_ctrl_reg, pp);
1193 POSTING_READ(pp_ctrl_reg);
05ce1a49 1194 }
9934c132
JB
1195}
1196
82a4d9c0 1197void ironlake_edp_panel_off(struct intel_dp *intel_dp)
9934c132 1198{
30add22d 1199 struct drm_device *dev = intel_dp_to_dev(intel_dp);
9934c132 1200 struct drm_i915_private *dev_priv = dev->dev_private;
99ea7127 1201 u32 pp;
453c5420 1202 u32 pp_ctrl_reg;
9934c132 1203
97af61f5
KP
1204 if (!is_edp(intel_dp))
1205 return;
37c6c9b0 1206
99ea7127 1207 DRM_DEBUG_KMS("Turn eDP power off\n");
37c6c9b0 1208
6cb49835 1209 WARN(!intel_dp->want_panel_vdd, "Need VDD to turn off panel\n");
37c6c9b0 1210
453c5420 1211 pp = ironlake_get_pp_control(intel_dp);
35a38556
DV
1212 /* We need to switch off panel power _and_ force vdd, for otherwise some
1213 * panels get very unhappy and cease to work. */
1214 pp &= ~(POWER_TARGET_ON | EDP_FORCE_VDD | PANEL_POWER_RESET | EDP_BLC_ENABLE);
453c5420 1215
bf13e81b 1216 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420
JB
1217
1218 I915_WRITE(pp_ctrl_reg, pp);
1219 POSTING_READ(pp_ctrl_reg);
9934c132 1220
35a38556
DV
1221 intel_dp->want_panel_vdd = false;
1222
99ea7127 1223 ironlake_wait_panel_off(intel_dp);
9934c132
JB
1224}
1225
d6c50ff8 1226void ironlake_edp_backlight_on(struct intel_dp *intel_dp)
32f9d658 1227{
da63a9f2
PZ
1228 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1229 struct drm_device *dev = intel_dig_port->base.base.dev;
32f9d658 1230 struct drm_i915_private *dev_priv = dev->dev_private;
da63a9f2 1231 int pipe = to_intel_crtc(intel_dig_port->base.base.crtc)->pipe;
32f9d658 1232 u32 pp;
453c5420 1233 u32 pp_ctrl_reg;
32f9d658 1234
f01eca2e
KP
1235 if (!is_edp(intel_dp))
1236 return;
1237
28c97730 1238 DRM_DEBUG_KMS("\n");
01cb9ea6
JB
1239 /*
1240 * If we enable the backlight right away following a panel power
1241 * on, we may see slight flicker as the panel syncs with the eDP
1242 * link. So delay a bit to make sure the image is solid before
1243 * allowing it to appear.
1244 */
f01eca2e 1245 msleep(intel_dp->backlight_on_delay);
453c5420 1246 pp = ironlake_get_pp_control(intel_dp);
32f9d658 1247 pp |= EDP_BLC_ENABLE;
453c5420 1248
bf13e81b 1249 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420
JB
1250
1251 I915_WRITE(pp_ctrl_reg, pp);
1252 POSTING_READ(pp_ctrl_reg);
035aa3de
DV
1253
1254 intel_panel_enable_backlight(dev, pipe);
32f9d658
ZW
1255}
1256
d6c50ff8 1257void ironlake_edp_backlight_off(struct intel_dp *intel_dp)
32f9d658 1258{
30add22d 1259 struct drm_device *dev = intel_dp_to_dev(intel_dp);
32f9d658
ZW
1260 struct drm_i915_private *dev_priv = dev->dev_private;
1261 u32 pp;
453c5420 1262 u32 pp_ctrl_reg;
32f9d658 1263
f01eca2e
KP
1264 if (!is_edp(intel_dp))
1265 return;
1266
035aa3de
DV
1267 intel_panel_disable_backlight(dev);
1268
28c97730 1269 DRM_DEBUG_KMS("\n");
453c5420 1270 pp = ironlake_get_pp_control(intel_dp);
32f9d658 1271 pp &= ~EDP_BLC_ENABLE;
453c5420 1272
bf13e81b 1273 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
453c5420
JB
1274
1275 I915_WRITE(pp_ctrl_reg, pp);
1276 POSTING_READ(pp_ctrl_reg);
f01eca2e 1277 msleep(intel_dp->backlight_off_delay);
32f9d658 1278}
a4fc5ed6 1279
2bd2ad64 1280static void ironlake_edp_pll_on(struct intel_dp *intel_dp)
d240f20f 1281{
da63a9f2
PZ
1282 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1283 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
1284 struct drm_device *dev = crtc->dev;
d240f20f
JB
1285 struct drm_i915_private *dev_priv = dev->dev_private;
1286 u32 dpa_ctl;
1287
2bd2ad64
DV
1288 assert_pipe_disabled(dev_priv,
1289 to_intel_crtc(crtc)->pipe);
1290
d240f20f
JB
1291 DRM_DEBUG_KMS("\n");
1292 dpa_ctl = I915_READ(DP_A);
0767935e
DV
1293 WARN(dpa_ctl & DP_PLL_ENABLE, "dp pll on, should be off\n");
1294 WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
1295
1296 /* We don't adjust intel_dp->DP while tearing down the link, to
1297 * facilitate link retraining (e.g. after hotplug). Hence clear all
1298 * enable bits here to ensure that we don't enable too much. */
1299 intel_dp->DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
1300 intel_dp->DP |= DP_PLL_ENABLE;
1301 I915_WRITE(DP_A, intel_dp->DP);
298b0b39
JB
1302 POSTING_READ(DP_A);
1303 udelay(200);
d240f20f
JB
1304}
1305
2bd2ad64 1306static void ironlake_edp_pll_off(struct intel_dp *intel_dp)
d240f20f 1307{
da63a9f2
PZ
1308 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1309 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
1310 struct drm_device *dev = crtc->dev;
d240f20f
JB
1311 struct drm_i915_private *dev_priv = dev->dev_private;
1312 u32 dpa_ctl;
1313
2bd2ad64
DV
1314 assert_pipe_disabled(dev_priv,
1315 to_intel_crtc(crtc)->pipe);
1316
d240f20f 1317 dpa_ctl = I915_READ(DP_A);
0767935e
DV
1318 WARN((dpa_ctl & DP_PLL_ENABLE) == 0,
1319 "dp pll off, should be on\n");
1320 WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
1321
1322 /* We can't rely on the value tracked for the DP register in
1323 * intel_dp->DP because link_down must not change that (otherwise link
1324 * re-training will fail. */
298b0b39 1325 dpa_ctl &= ~DP_PLL_ENABLE;
d240f20f 1326 I915_WRITE(DP_A, dpa_ctl);
1af5fa1b 1327 POSTING_READ(DP_A);
d240f20f
JB
1328 udelay(200);
1329}
1330
c7ad3810 1331/* If the sink supports it, try to set the power state appropriately */
c19b0669 1332void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
c7ad3810
JB
1333{
1334 int ret, i;
1335
1336 /* Should have a valid DPCD by this point */
1337 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
1338 return;
1339
1340 if (mode != DRM_MODE_DPMS_ON) {
1341 ret = intel_dp_aux_native_write_1(intel_dp, DP_SET_POWER,
1342 DP_SET_POWER_D3);
1343 if (ret != 1)
1344 DRM_DEBUG_DRIVER("failed to write sink power state\n");
1345 } else {
1346 /*
1347 * When turning on, we need to retry for 1ms to give the sink
1348 * time to wake up.
1349 */
1350 for (i = 0; i < 3; i++) {
1351 ret = intel_dp_aux_native_write_1(intel_dp,
1352 DP_SET_POWER,
1353 DP_SET_POWER_D0);
1354 if (ret == 1)
1355 break;
1356 msleep(1);
1357 }
1358 }
1359}
1360
19d8fe15
DV
1361static bool intel_dp_get_hw_state(struct intel_encoder *encoder,
1362 enum pipe *pipe)
d240f20f 1363{
19d8fe15 1364 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
bc7d38a4 1365 enum port port = dp_to_dig_port(intel_dp)->port;
19d8fe15
DV
1366 struct drm_device *dev = encoder->base.dev;
1367 struct drm_i915_private *dev_priv = dev->dev_private;
1368 u32 tmp = I915_READ(intel_dp->output_reg);
1369
1370 if (!(tmp & DP_PORT_EN))
1371 return false;
1372
bc7d38a4 1373 if (port == PORT_A && IS_GEN7(dev) && !IS_VALLEYVIEW(dev)) {
19d8fe15 1374 *pipe = PORT_TO_PIPE_CPT(tmp);
bc7d38a4 1375 } else if (!HAS_PCH_CPT(dev) || port == PORT_A) {
19d8fe15
DV
1376 *pipe = PORT_TO_PIPE(tmp);
1377 } else {
1378 u32 trans_sel;
1379 u32 trans_dp;
1380 int i;
1381
1382 switch (intel_dp->output_reg) {
1383 case PCH_DP_B:
1384 trans_sel = TRANS_DP_PORT_SEL_B;
1385 break;
1386 case PCH_DP_C:
1387 trans_sel = TRANS_DP_PORT_SEL_C;
1388 break;
1389 case PCH_DP_D:
1390 trans_sel = TRANS_DP_PORT_SEL_D;
1391 break;
1392 default:
1393 return true;
1394 }
1395
1396 for_each_pipe(i) {
1397 trans_dp = I915_READ(TRANS_DP_CTL(i));
1398 if ((trans_dp & TRANS_DP_PORT_SEL_MASK) == trans_sel) {
1399 *pipe = i;
1400 return true;
1401 }
1402 }
19d8fe15 1403
4a0833ec
DV
1404 DRM_DEBUG_KMS("No pipe for dp port 0x%x found\n",
1405 intel_dp->output_reg);
1406 }
d240f20f 1407
19d8fe15
DV
1408 return true;
1409}
d240f20f 1410
045ac3b5
JB
1411static void intel_dp_get_config(struct intel_encoder *encoder,
1412 struct intel_crtc_config *pipe_config)
1413{
1414 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
045ac3b5 1415 u32 tmp, flags = 0;
63000ef6
XZ
1416 struct drm_device *dev = encoder->base.dev;
1417 struct drm_i915_private *dev_priv = dev->dev_private;
1418 enum port port = dp_to_dig_port(intel_dp)->port;
1419 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
045ac3b5 1420
63000ef6
XZ
1421 if ((port == PORT_A) || !HAS_PCH_CPT(dev)) {
1422 tmp = I915_READ(intel_dp->output_reg);
1423 if (tmp & DP_SYNC_HS_HIGH)
1424 flags |= DRM_MODE_FLAG_PHSYNC;
1425 else
1426 flags |= DRM_MODE_FLAG_NHSYNC;
045ac3b5 1427
63000ef6
XZ
1428 if (tmp & DP_SYNC_VS_HIGH)
1429 flags |= DRM_MODE_FLAG_PVSYNC;
1430 else
1431 flags |= DRM_MODE_FLAG_NVSYNC;
1432 } else {
1433 tmp = I915_READ(TRANS_DP_CTL(crtc->pipe));
1434 if (tmp & TRANS_DP_HSYNC_ACTIVE_HIGH)
1435 flags |= DRM_MODE_FLAG_PHSYNC;
1436 else
1437 flags |= DRM_MODE_FLAG_NHSYNC;
045ac3b5 1438
63000ef6
XZ
1439 if (tmp & TRANS_DP_VSYNC_ACTIVE_HIGH)
1440 flags |= DRM_MODE_FLAG_PVSYNC;
1441 else
1442 flags |= DRM_MODE_FLAG_NVSYNC;
1443 }
045ac3b5
JB
1444
1445 pipe_config->adjusted_mode.flags |= flags;
f1f644dc 1446
eb14cb74
VS
1447 pipe_config->has_dp_encoder = true;
1448
1449 intel_dp_get_m_n(crtc, pipe_config);
1450
f1f644dc
JB
1451 if (dp_to_dig_port(intel_dp)->port == PORT_A) {
1452 if ((I915_READ(DP_A) & DP_PLL_FREQ_MASK) == DP_PLL_FREQ_160MHZ)
1453 pipe_config->port_clock = 162000;
1454 else
1455 pipe_config->port_clock = 270000;
1456 }
045ac3b5
JB
1457}
1458
2293bb5c
SK
1459static bool is_edp_psr(struct intel_dp *intel_dp)
1460{
1461 return is_edp(intel_dp) &&
1462 intel_dp->psr_dpcd[0] & DP_PSR_IS_SUPPORTED;
1463}
1464
2b28bb1b
RV
1465static bool intel_edp_is_psr_enabled(struct drm_device *dev)
1466{
1467 struct drm_i915_private *dev_priv = dev->dev_private;
1468
1469 if (!IS_HASWELL(dev))
1470 return false;
1471
1472 return I915_READ(EDP_PSR_CTL) & EDP_PSR_ENABLE;
1473}
1474
1475static void intel_edp_psr_write_vsc(struct intel_dp *intel_dp,
1476 struct edp_vsc_psr *vsc_psr)
1477{
1478 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1479 struct drm_device *dev = dig_port->base.base.dev;
1480 struct drm_i915_private *dev_priv = dev->dev_private;
1481 struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
1482 u32 ctl_reg = HSW_TVIDEO_DIP_CTL(crtc->config.cpu_transcoder);
1483 u32 data_reg = HSW_TVIDEO_DIP_VSC_DATA(crtc->config.cpu_transcoder);
1484 uint32_t *data = (uint32_t *) vsc_psr;
1485 unsigned int i;
1486
1487 /* As per BSPec (Pipe Video Data Island Packet), we need to disable
1488 the video DIP being updated before program video DIP data buffer
1489 registers for DIP being updated. */
1490 I915_WRITE(ctl_reg, 0);
1491 POSTING_READ(ctl_reg);
1492
1493 for (i = 0; i < VIDEO_DIP_VSC_DATA_SIZE; i += 4) {
1494 if (i < sizeof(struct edp_vsc_psr))
1495 I915_WRITE(data_reg + i, *data++);
1496 else
1497 I915_WRITE(data_reg + i, 0);
1498 }
1499
1500 I915_WRITE(ctl_reg, VIDEO_DIP_ENABLE_VSC_HSW);
1501 POSTING_READ(ctl_reg);
1502}
1503
1504static void intel_edp_psr_setup(struct intel_dp *intel_dp)
1505{
1506 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1507 struct drm_i915_private *dev_priv = dev->dev_private;
1508 struct edp_vsc_psr psr_vsc;
1509
1510 if (intel_dp->psr_setup_done)
1511 return;
1512
1513 /* Prepare VSC packet as per EDP 1.3 spec, Table 3.10 */
1514 memset(&psr_vsc, 0, sizeof(psr_vsc));
1515 psr_vsc.sdp_header.HB0 = 0;
1516 psr_vsc.sdp_header.HB1 = 0x7;
1517 psr_vsc.sdp_header.HB2 = 0x2;
1518 psr_vsc.sdp_header.HB3 = 0x8;
1519 intel_edp_psr_write_vsc(intel_dp, &psr_vsc);
1520
1521 /* Avoid continuous PSR exit by masking memup and hpd */
1522 I915_WRITE(EDP_PSR_DEBUG_CTL, EDP_PSR_DEBUG_MASK_MEMUP |
1523 EDP_PSR_DEBUG_MASK_HPD);
1524
1525 intel_dp->psr_setup_done = true;
1526}
1527
1528static void intel_edp_psr_enable_sink(struct intel_dp *intel_dp)
1529{
1530 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1531 struct drm_i915_private *dev_priv = dev->dev_private;
bc86625a 1532 uint32_t aux_clock_divider = get_aux_clock_divider(intel_dp, 0);
2b28bb1b
RV
1533 int precharge = 0x3;
1534 int msg_size = 5; /* Header(4) + Message(1) */
1535
1536 /* Enable PSR in sink */
1537 if (intel_dp->psr_dpcd[1] & DP_PSR_NO_TRAIN_ON_EXIT)
1538 intel_dp_aux_native_write_1(intel_dp, DP_PSR_EN_CFG,
1539 DP_PSR_ENABLE &
1540 ~DP_PSR_MAIN_LINK_ACTIVE);
1541 else
1542 intel_dp_aux_native_write_1(intel_dp, DP_PSR_EN_CFG,
1543 DP_PSR_ENABLE |
1544 DP_PSR_MAIN_LINK_ACTIVE);
1545
1546 /* Setup AUX registers */
1547 I915_WRITE(EDP_PSR_AUX_DATA1, EDP_PSR_DPCD_COMMAND);
1548 I915_WRITE(EDP_PSR_AUX_DATA2, EDP_PSR_DPCD_NORMAL_OPERATION);
1549 I915_WRITE(EDP_PSR_AUX_CTL,
1550 DP_AUX_CH_CTL_TIME_OUT_400us |
1551 (msg_size << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
1552 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
1553 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT));
1554}
1555
1556static void intel_edp_psr_enable_source(struct intel_dp *intel_dp)
1557{
1558 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1559 struct drm_i915_private *dev_priv = dev->dev_private;
1560 uint32_t max_sleep_time = 0x1f;
1561 uint32_t idle_frames = 1;
1562 uint32_t val = 0x0;
1563
1564 if (intel_dp->psr_dpcd[1] & DP_PSR_NO_TRAIN_ON_EXIT) {
1565 val |= EDP_PSR_LINK_STANDBY;
1566 val |= EDP_PSR_TP2_TP3_TIME_0us;
1567 val |= EDP_PSR_TP1_TIME_0us;
1568 val |= EDP_PSR_SKIP_AUX_EXIT;
1569 } else
1570 val |= EDP_PSR_LINK_DISABLE;
1571
1572 I915_WRITE(EDP_PSR_CTL, val |
1573 EDP_PSR_MIN_LINK_ENTRY_TIME_8_LINES |
1574 max_sleep_time << EDP_PSR_MAX_SLEEP_TIME_SHIFT |
1575 idle_frames << EDP_PSR_IDLE_FRAME_SHIFT |
1576 EDP_PSR_ENABLE);
1577}
1578
3f51e471
RV
1579static bool intel_edp_psr_match_conditions(struct intel_dp *intel_dp)
1580{
1581 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1582 struct drm_device *dev = dig_port->base.base.dev;
1583 struct drm_i915_private *dev_priv = dev->dev_private;
1584 struct drm_crtc *crtc = dig_port->base.base.crtc;
1585 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1586 struct drm_i915_gem_object *obj = to_intel_framebuffer(crtc->fb)->obj;
1587 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
1588
1589 if (!IS_HASWELL(dev)) {
1590 DRM_DEBUG_KMS("PSR not supported on this platform\n");
1591 dev_priv->no_psr_reason = PSR_NO_SOURCE;
1592 return false;
1593 }
1594
1595 if ((intel_encoder->type != INTEL_OUTPUT_EDP) ||
1596 (dig_port->port != PORT_A)) {
1597 DRM_DEBUG_KMS("HSW ties PSR to DDI A (eDP)\n");
1598 dev_priv->no_psr_reason = PSR_HSW_NOT_DDIA;
1599 return false;
1600 }
1601
1602 if (!is_edp_psr(intel_dp)) {
1603 DRM_DEBUG_KMS("PSR not supported by this panel\n");
1604 dev_priv->no_psr_reason = PSR_NO_SINK;
1605 return false;
1606 }
1607
105b7c11
RV
1608 if (!i915_enable_psr) {
1609 DRM_DEBUG_KMS("PSR disable by flag\n");
1610 dev_priv->no_psr_reason = PSR_MODULE_PARAM;
1611 return false;
1612 }
1613
cd234b0b
CW
1614 crtc = dig_port->base.base.crtc;
1615 if (crtc == NULL) {
1616 DRM_DEBUG_KMS("crtc not active for PSR\n");
1617 dev_priv->no_psr_reason = PSR_CRTC_NOT_ACTIVE;
1618 return false;
1619 }
1620
1621 intel_crtc = to_intel_crtc(crtc);
3f51e471
RV
1622 if (!intel_crtc->active || !crtc->fb || !crtc->mode.clock) {
1623 DRM_DEBUG_KMS("crtc not active for PSR\n");
1624 dev_priv->no_psr_reason = PSR_CRTC_NOT_ACTIVE;
1625 return false;
1626 }
1627
cd234b0b 1628 obj = to_intel_framebuffer(crtc->fb)->obj;
3f51e471
RV
1629 if (obj->tiling_mode != I915_TILING_X ||
1630 obj->fence_reg == I915_FENCE_REG_NONE) {
1631 DRM_DEBUG_KMS("PSR condition failed: fb not tiled or fenced\n");
1632 dev_priv->no_psr_reason = PSR_NOT_TILED;
1633 return false;
1634 }
1635
1636 if (I915_READ(SPRCTL(intel_crtc->pipe)) & SPRITE_ENABLE) {
1637 DRM_DEBUG_KMS("PSR condition failed: Sprite is Enabled\n");
1638 dev_priv->no_psr_reason = PSR_SPRITE_ENABLED;
1639 return false;
1640 }
1641
1642 if (I915_READ(HSW_STEREO_3D_CTL(intel_crtc->config.cpu_transcoder)) &
1643 S3D_ENABLE) {
1644 DRM_DEBUG_KMS("PSR condition failed: Stereo 3D is Enabled\n");
1645 dev_priv->no_psr_reason = PSR_S3D_ENABLED;
1646 return false;
1647 }
1648
1649 if (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) {
1650 DRM_DEBUG_KMS("PSR condition failed: Interlaced is Enabled\n");
1651 dev_priv->no_psr_reason = PSR_INTERLACED_ENABLED;
1652 return false;
1653 }
1654
1655 return true;
1656}
1657
3d739d92 1658static void intel_edp_psr_do_enable(struct intel_dp *intel_dp)
2b28bb1b
RV
1659{
1660 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1661
3f51e471
RV
1662 if (!intel_edp_psr_match_conditions(intel_dp) ||
1663 intel_edp_is_psr_enabled(dev))
2b28bb1b
RV
1664 return;
1665
1666 /* Setup PSR once */
1667 intel_edp_psr_setup(intel_dp);
1668
1669 /* Enable PSR on the panel */
1670 intel_edp_psr_enable_sink(intel_dp);
1671
1672 /* Enable PSR on the host */
1673 intel_edp_psr_enable_source(intel_dp);
1674}
1675
3d739d92
RV
1676void intel_edp_psr_enable(struct intel_dp *intel_dp)
1677{
1678 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1679
1680 if (intel_edp_psr_match_conditions(intel_dp) &&
1681 !intel_edp_is_psr_enabled(dev))
1682 intel_edp_psr_do_enable(intel_dp);
1683}
1684
2b28bb1b
RV
1685void intel_edp_psr_disable(struct intel_dp *intel_dp)
1686{
1687 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1688 struct drm_i915_private *dev_priv = dev->dev_private;
1689
1690 if (!intel_edp_is_psr_enabled(dev))
1691 return;
1692
1693 I915_WRITE(EDP_PSR_CTL, I915_READ(EDP_PSR_CTL) & ~EDP_PSR_ENABLE);
1694
1695 /* Wait till PSR is idle */
1696 if (_wait_for((I915_READ(EDP_PSR_STATUS_CTL) &
1697 EDP_PSR_STATUS_STATE_MASK) == 0, 2000, 10))
1698 DRM_ERROR("Timed out waiting for PSR Idle State\n");
1699}
1700
3d739d92
RV
1701void intel_edp_psr_update(struct drm_device *dev)
1702{
1703 struct intel_encoder *encoder;
1704 struct intel_dp *intel_dp = NULL;
1705
1706 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head)
1707 if (encoder->type == INTEL_OUTPUT_EDP) {
1708 intel_dp = enc_to_intel_dp(&encoder->base);
1709
1710 if (!is_edp_psr(intel_dp))
1711 return;
1712
1713 if (!intel_edp_psr_match_conditions(intel_dp))
1714 intel_edp_psr_disable(intel_dp);
1715 else
1716 if (!intel_edp_is_psr_enabled(dev))
1717 intel_edp_psr_do_enable(intel_dp);
1718 }
1719}
1720
e8cb4558 1721static void intel_disable_dp(struct intel_encoder *encoder)
d240f20f 1722{
e8cb4558 1723 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
982a3866
ID
1724 enum port port = dp_to_dig_port(intel_dp)->port;
1725 struct drm_device *dev = encoder->base.dev;
6cb49835
DV
1726
1727 /* Make sure the panel is off before trying to change the mode. But also
1728 * ensure that we have vdd while we switch off the panel. */
1729 ironlake_edp_panel_vdd_on(intel_dp);
21264c63 1730 ironlake_edp_backlight_off(intel_dp);
c7ad3810 1731 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
35a38556 1732 ironlake_edp_panel_off(intel_dp);
3739850b
DV
1733
1734 /* cpu edp my only be disable _after_ the cpu pipe/plane is disabled. */
982a3866 1735 if (!(port == PORT_A || IS_VALLEYVIEW(dev)))
3739850b 1736 intel_dp_link_down(intel_dp);
d240f20f
JB
1737}
1738
2bd2ad64 1739static void intel_post_disable_dp(struct intel_encoder *encoder)
d240f20f 1740{
2bd2ad64 1741 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
982a3866 1742 enum port port = dp_to_dig_port(intel_dp)->port;
b2634017 1743 struct drm_device *dev = encoder->base.dev;
2bd2ad64 1744
982a3866 1745 if (port == PORT_A || IS_VALLEYVIEW(dev)) {
3739850b 1746 intel_dp_link_down(intel_dp);
b2634017
JB
1747 if (!IS_VALLEYVIEW(dev))
1748 ironlake_edp_pll_off(intel_dp);
3739850b 1749 }
2bd2ad64
DV
1750}
1751
e8cb4558 1752static void intel_enable_dp(struct intel_encoder *encoder)
d240f20f 1753{
e8cb4558
DV
1754 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1755 struct drm_device *dev = encoder->base.dev;
1756 struct drm_i915_private *dev_priv = dev->dev_private;
1757 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
5d613501 1758
0c33d8d7
DV
1759 if (WARN_ON(dp_reg & DP_PORT_EN))
1760 return;
5d613501 1761
97af61f5 1762 ironlake_edp_panel_vdd_on(intel_dp);
f01eca2e 1763 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
33a34e4e 1764 intel_dp_start_link_train(intel_dp);
97af61f5 1765 ironlake_edp_panel_on(intel_dp);
bd943159 1766 ironlake_edp_panel_vdd_off(intel_dp, true);
33a34e4e 1767 intel_dp_complete_link_train(intel_dp);
3ab9c637 1768 intel_dp_stop_link_train(intel_dp);
ab1f90f9 1769}
89b667f8 1770
ecff4f3b
JN
1771static void g4x_enable_dp(struct intel_encoder *encoder)
1772{
828f5c6e
JN
1773 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1774
ecff4f3b 1775 intel_enable_dp(encoder);
828f5c6e 1776 ironlake_edp_backlight_on(intel_dp);
ecff4f3b
JN
1777}
1778
ab1f90f9
JN
1779static void vlv_enable_dp(struct intel_encoder *encoder)
1780{
828f5c6e
JN
1781 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1782
1783 ironlake_edp_backlight_on(intel_dp);
d240f20f
JB
1784}
1785
ecff4f3b 1786static void g4x_pre_enable_dp(struct intel_encoder *encoder)
ab1f90f9
JN
1787{
1788 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1789 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
1790
1791 if (dport->port == PORT_A)
1792 ironlake_edp_pll_on(intel_dp);
1793}
1794
1795static void vlv_pre_enable_dp(struct intel_encoder *encoder)
a4fc5ed6 1796{
2bd2ad64 1797 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
bc7d38a4 1798 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
b2634017 1799 struct drm_device *dev = encoder->base.dev;
89b667f8 1800 struct drm_i915_private *dev_priv = dev->dev_private;
ab1f90f9
JN
1801 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
1802 int port = vlv_dport_to_channel(dport);
1803 int pipe = intel_crtc->pipe;
bf13e81b 1804 struct edp_power_seq power_seq;
ab1f90f9 1805 u32 val;
a4fc5ed6 1806
ab1f90f9 1807 mutex_lock(&dev_priv->dpio_lock);
89b667f8 1808
5e69f97f 1809 val = vlv_dpio_read(dev_priv, pipe, DPIO_DATA_LANE_A(port));
ab1f90f9
JN
1810 val = 0;
1811 if (pipe)
1812 val |= (1<<21);
1813 else
1814 val &= ~(1<<21);
1815 val |= 0x001000c4;
5e69f97f
CML
1816 vlv_dpio_write(dev_priv, pipe, DPIO_DATA_CHANNEL(port), val);
1817 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_CLOCKBUF0(port), 0x00760018);
1818 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_CLOCKBUF8(port), 0x00400888);
89b667f8 1819
ab1f90f9
JN
1820 mutex_unlock(&dev_priv->dpio_lock);
1821
bf13e81b
JN
1822 /* init power sequencer on this pipe and port */
1823 intel_dp_init_panel_power_sequencer(dev, intel_dp, &power_seq);
1824 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp,
1825 &power_seq);
1826
ab1f90f9
JN
1827 intel_enable_dp(encoder);
1828
1829 vlv_wait_port_ready(dev_priv, port);
89b667f8
JB
1830}
1831
ecff4f3b 1832static void vlv_dp_pre_pll_enable(struct intel_encoder *encoder)
89b667f8
JB
1833{
1834 struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
1835 struct drm_device *dev = encoder->base.dev;
1836 struct drm_i915_private *dev_priv = dev->dev_private;
5e69f97f
CML
1837 struct intel_crtc *intel_crtc =
1838 to_intel_crtc(encoder->base.crtc);
89b667f8 1839 int port = vlv_dport_to_channel(dport);
5e69f97f 1840 int pipe = intel_crtc->pipe;
89b667f8 1841
89b667f8 1842 /* Program Tx lane resets to default */
0980a60f 1843 mutex_lock(&dev_priv->dpio_lock);
5e69f97f 1844 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_TX(port),
89b667f8
JB
1845 DPIO_PCS_TX_LANE2_RESET |
1846 DPIO_PCS_TX_LANE1_RESET);
5e69f97f 1847 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_CLK(port),
89b667f8
JB
1848 DPIO_PCS_CLK_CRI_RXEB_EIOS_EN |
1849 DPIO_PCS_CLK_CRI_RXDIGFILTSG_EN |
1850 (1<<DPIO_PCS_CLK_DATAWIDTH_SHIFT) |
1851 DPIO_PCS_CLK_SOFT_RESET);
1852
1853 /* Fix up inter-pair skew failure */
5e69f97f
CML
1854 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_STAGGER1(port), 0x00750f00);
1855 vlv_dpio_write(dev_priv, pipe, DPIO_TX_CTL(port), 0x00001500);
1856 vlv_dpio_write(dev_priv, pipe, DPIO_TX_LANE(port), 0x40400000);
0980a60f 1857 mutex_unlock(&dev_priv->dpio_lock);
a4fc5ed6
KP
1858}
1859
1860/*
df0c237d
JB
1861 * Native read with retry for link status and receiver capability reads for
1862 * cases where the sink may still be asleep.
a4fc5ed6
KP
1863 */
1864static bool
df0c237d
JB
1865intel_dp_aux_native_read_retry(struct intel_dp *intel_dp, uint16_t address,
1866 uint8_t *recv, int recv_bytes)
a4fc5ed6 1867{
61da5fab
JB
1868 int ret, i;
1869
df0c237d
JB
1870 /*
1871 * Sinks are *supposed* to come up within 1ms from an off state,
1872 * but we're also supposed to retry 3 times per the spec.
1873 */
61da5fab 1874 for (i = 0; i < 3; i++) {
df0c237d
JB
1875 ret = intel_dp_aux_native_read(intel_dp, address, recv,
1876 recv_bytes);
1877 if (ret == recv_bytes)
61da5fab
JB
1878 return true;
1879 msleep(1);
1880 }
a4fc5ed6 1881
61da5fab 1882 return false;
a4fc5ed6
KP
1883}
1884
1885/*
1886 * Fetch AUX CH registers 0x202 - 0x207 which contain
1887 * link status information
1888 */
1889static bool
93f62dad 1890intel_dp_get_link_status(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
a4fc5ed6 1891{
df0c237d
JB
1892 return intel_dp_aux_native_read_retry(intel_dp,
1893 DP_LANE0_1_STATUS,
93f62dad 1894 link_status,
df0c237d 1895 DP_LINK_STATUS_SIZE);
a4fc5ed6
KP
1896}
1897
a4fc5ed6
KP
1898#if 0
1899static char *voltage_names[] = {
1900 "0.4V", "0.6V", "0.8V", "1.2V"
1901};
1902static char *pre_emph_names[] = {
1903 "0dB", "3.5dB", "6dB", "9.5dB"
1904};
1905static char *link_train_names[] = {
1906 "pattern 1", "pattern 2", "idle", "off"
1907};
1908#endif
1909
1910/*
1911 * These are source-specific values; current Intel hardware supports
1912 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1913 */
a4fc5ed6
KP
1914
1915static uint8_t
1a2eb460 1916intel_dp_voltage_max(struct intel_dp *intel_dp)
a4fc5ed6 1917{
30add22d 1918 struct drm_device *dev = intel_dp_to_dev(intel_dp);
bc7d38a4 1919 enum port port = dp_to_dig_port(intel_dp)->port;
1a2eb460 1920
e2fa6fba
P
1921 if (IS_VALLEYVIEW(dev))
1922 return DP_TRAIN_VOLTAGE_SWING_1200;
bc7d38a4 1923 else if (IS_GEN7(dev) && port == PORT_A)
1a2eb460 1924 return DP_TRAIN_VOLTAGE_SWING_800;
bc7d38a4 1925 else if (HAS_PCH_CPT(dev) && port != PORT_A)
1a2eb460
KP
1926 return DP_TRAIN_VOLTAGE_SWING_1200;
1927 else
1928 return DP_TRAIN_VOLTAGE_SWING_800;
1929}
1930
1931static uint8_t
1932intel_dp_pre_emphasis_max(struct intel_dp *intel_dp, uint8_t voltage_swing)
1933{
30add22d 1934 struct drm_device *dev = intel_dp_to_dev(intel_dp);
bc7d38a4 1935 enum port port = dp_to_dig_port(intel_dp)->port;
1a2eb460 1936
22b8bf17 1937 if (HAS_DDI(dev)) {
d6c0d722
PZ
1938 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1939 case DP_TRAIN_VOLTAGE_SWING_400:
1940 return DP_TRAIN_PRE_EMPHASIS_9_5;
1941 case DP_TRAIN_VOLTAGE_SWING_600:
1942 return DP_TRAIN_PRE_EMPHASIS_6;
1943 case DP_TRAIN_VOLTAGE_SWING_800:
1944 return DP_TRAIN_PRE_EMPHASIS_3_5;
1945 case DP_TRAIN_VOLTAGE_SWING_1200:
1946 default:
1947 return DP_TRAIN_PRE_EMPHASIS_0;
1948 }
e2fa6fba
P
1949 } else if (IS_VALLEYVIEW(dev)) {
1950 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1951 case DP_TRAIN_VOLTAGE_SWING_400:
1952 return DP_TRAIN_PRE_EMPHASIS_9_5;
1953 case DP_TRAIN_VOLTAGE_SWING_600:
1954 return DP_TRAIN_PRE_EMPHASIS_6;
1955 case DP_TRAIN_VOLTAGE_SWING_800:
1956 return DP_TRAIN_PRE_EMPHASIS_3_5;
1957 case DP_TRAIN_VOLTAGE_SWING_1200:
1958 default:
1959 return DP_TRAIN_PRE_EMPHASIS_0;
1960 }
bc7d38a4 1961 } else if (IS_GEN7(dev) && port == PORT_A) {
1a2eb460
KP
1962 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1963 case DP_TRAIN_VOLTAGE_SWING_400:
1964 return DP_TRAIN_PRE_EMPHASIS_6;
1965 case DP_TRAIN_VOLTAGE_SWING_600:
1966 case DP_TRAIN_VOLTAGE_SWING_800:
1967 return DP_TRAIN_PRE_EMPHASIS_3_5;
1968 default:
1969 return DP_TRAIN_PRE_EMPHASIS_0;
1970 }
1971 } else {
1972 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1973 case DP_TRAIN_VOLTAGE_SWING_400:
1974 return DP_TRAIN_PRE_EMPHASIS_6;
1975 case DP_TRAIN_VOLTAGE_SWING_600:
1976 return DP_TRAIN_PRE_EMPHASIS_6;
1977 case DP_TRAIN_VOLTAGE_SWING_800:
1978 return DP_TRAIN_PRE_EMPHASIS_3_5;
1979 case DP_TRAIN_VOLTAGE_SWING_1200:
1980 default:
1981 return DP_TRAIN_PRE_EMPHASIS_0;
1982 }
a4fc5ed6
KP
1983 }
1984}
1985
e2fa6fba
P
1986static uint32_t intel_vlv_signal_levels(struct intel_dp *intel_dp)
1987{
1988 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1989 struct drm_i915_private *dev_priv = dev->dev_private;
1990 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
5e69f97f
CML
1991 struct intel_crtc *intel_crtc =
1992 to_intel_crtc(dport->base.base.crtc);
e2fa6fba
P
1993 unsigned long demph_reg_value, preemph_reg_value,
1994 uniqtranscale_reg_value;
1995 uint8_t train_set = intel_dp->train_set[0];
cece5d58 1996 int port = vlv_dport_to_channel(dport);
5e69f97f 1997 int pipe = intel_crtc->pipe;
e2fa6fba
P
1998
1999 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
2000 case DP_TRAIN_PRE_EMPHASIS_0:
2001 preemph_reg_value = 0x0004000;
2002 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
2003 case DP_TRAIN_VOLTAGE_SWING_400:
2004 demph_reg_value = 0x2B405555;
2005 uniqtranscale_reg_value = 0x552AB83A;
2006 break;
2007 case DP_TRAIN_VOLTAGE_SWING_600:
2008 demph_reg_value = 0x2B404040;
2009 uniqtranscale_reg_value = 0x5548B83A;
2010 break;
2011 case DP_TRAIN_VOLTAGE_SWING_800:
2012 demph_reg_value = 0x2B245555;
2013 uniqtranscale_reg_value = 0x5560B83A;
2014 break;
2015 case DP_TRAIN_VOLTAGE_SWING_1200:
2016 demph_reg_value = 0x2B405555;
2017 uniqtranscale_reg_value = 0x5598DA3A;
2018 break;
2019 default:
2020 return 0;
2021 }
2022 break;
2023 case DP_TRAIN_PRE_EMPHASIS_3_5:
2024 preemph_reg_value = 0x0002000;
2025 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
2026 case DP_TRAIN_VOLTAGE_SWING_400:
2027 demph_reg_value = 0x2B404040;
2028 uniqtranscale_reg_value = 0x5552B83A;
2029 break;
2030 case DP_TRAIN_VOLTAGE_SWING_600:
2031 demph_reg_value = 0x2B404848;
2032 uniqtranscale_reg_value = 0x5580B83A;
2033 break;
2034 case DP_TRAIN_VOLTAGE_SWING_800:
2035 demph_reg_value = 0x2B404040;
2036 uniqtranscale_reg_value = 0x55ADDA3A;
2037 break;
2038 default:
2039 return 0;
2040 }
2041 break;
2042 case DP_TRAIN_PRE_EMPHASIS_6:
2043 preemph_reg_value = 0x0000000;
2044 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
2045 case DP_TRAIN_VOLTAGE_SWING_400:
2046 demph_reg_value = 0x2B305555;
2047 uniqtranscale_reg_value = 0x5570B83A;
2048 break;
2049 case DP_TRAIN_VOLTAGE_SWING_600:
2050 demph_reg_value = 0x2B2B4040;
2051 uniqtranscale_reg_value = 0x55ADDA3A;
2052 break;
2053 default:
2054 return 0;
2055 }
2056 break;
2057 case DP_TRAIN_PRE_EMPHASIS_9_5:
2058 preemph_reg_value = 0x0006000;
2059 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
2060 case DP_TRAIN_VOLTAGE_SWING_400:
2061 demph_reg_value = 0x1B405555;
2062 uniqtranscale_reg_value = 0x55ADDA3A;
2063 break;
2064 default:
2065 return 0;
2066 }
2067 break;
2068 default:
2069 return 0;
2070 }
2071
0980a60f 2072 mutex_lock(&dev_priv->dpio_lock);
5e69f97f
CML
2073 vlv_dpio_write(dev_priv, pipe, DPIO_TX_OCALINIT(port), 0x00000000);
2074 vlv_dpio_write(dev_priv, pipe, DPIO_TX_SWING_CTL4(port), demph_reg_value);
2075 vlv_dpio_write(dev_priv, pipe, DPIO_TX_SWING_CTL2(port),
e2fa6fba 2076 uniqtranscale_reg_value);
5e69f97f
CML
2077 vlv_dpio_write(dev_priv, pipe, DPIO_TX_SWING_CTL3(port), 0x0C782040);
2078 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_STAGGER0(port), 0x00030000);
2079 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_CTL_OVER1(port), preemph_reg_value);
2080 vlv_dpio_write(dev_priv, pipe, DPIO_TX_OCALINIT(port), 0x80000000);
0980a60f 2081 mutex_unlock(&dev_priv->dpio_lock);
e2fa6fba
P
2082
2083 return 0;
2084}
2085
a4fc5ed6 2086static void
93f62dad 2087intel_get_adjust_train(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
a4fc5ed6
KP
2088{
2089 uint8_t v = 0;
2090 uint8_t p = 0;
2091 int lane;
1a2eb460
KP
2092 uint8_t voltage_max;
2093 uint8_t preemph_max;
a4fc5ed6 2094
33a34e4e 2095 for (lane = 0; lane < intel_dp->lane_count; lane++) {
0f037bde
DV
2096 uint8_t this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
2097 uint8_t this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
a4fc5ed6
KP
2098
2099 if (this_v > v)
2100 v = this_v;
2101 if (this_p > p)
2102 p = this_p;
2103 }
2104
1a2eb460 2105 voltage_max = intel_dp_voltage_max(intel_dp);
417e822d
KP
2106 if (v >= voltage_max)
2107 v = voltage_max | DP_TRAIN_MAX_SWING_REACHED;
a4fc5ed6 2108
1a2eb460
KP
2109 preemph_max = intel_dp_pre_emphasis_max(intel_dp, v);
2110 if (p >= preemph_max)
2111 p = preemph_max | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
a4fc5ed6
KP
2112
2113 for (lane = 0; lane < 4; lane++)
33a34e4e 2114 intel_dp->train_set[lane] = v | p;
a4fc5ed6
KP
2115}
2116
2117static uint32_t
f0a3424e 2118intel_gen4_signal_levels(uint8_t train_set)
a4fc5ed6 2119{
3cf2efb1 2120 uint32_t signal_levels = 0;
a4fc5ed6 2121
3cf2efb1 2122 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
a4fc5ed6
KP
2123 case DP_TRAIN_VOLTAGE_SWING_400:
2124 default:
2125 signal_levels |= DP_VOLTAGE_0_4;
2126 break;
2127 case DP_TRAIN_VOLTAGE_SWING_600:
2128 signal_levels |= DP_VOLTAGE_0_6;
2129 break;
2130 case DP_TRAIN_VOLTAGE_SWING_800:
2131 signal_levels |= DP_VOLTAGE_0_8;
2132 break;
2133 case DP_TRAIN_VOLTAGE_SWING_1200:
2134 signal_levels |= DP_VOLTAGE_1_2;
2135 break;
2136 }
3cf2efb1 2137 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
a4fc5ed6
KP
2138 case DP_TRAIN_PRE_EMPHASIS_0:
2139 default:
2140 signal_levels |= DP_PRE_EMPHASIS_0;
2141 break;
2142 case DP_TRAIN_PRE_EMPHASIS_3_5:
2143 signal_levels |= DP_PRE_EMPHASIS_3_5;
2144 break;
2145 case DP_TRAIN_PRE_EMPHASIS_6:
2146 signal_levels |= DP_PRE_EMPHASIS_6;
2147 break;
2148 case DP_TRAIN_PRE_EMPHASIS_9_5:
2149 signal_levels |= DP_PRE_EMPHASIS_9_5;
2150 break;
2151 }
2152 return signal_levels;
2153}
2154
e3421a18
ZW
2155/* Gen6's DP voltage swing and pre-emphasis control */
2156static uint32_t
2157intel_gen6_edp_signal_levels(uint8_t train_set)
2158{
3c5a62b5
YL
2159 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
2160 DP_TRAIN_PRE_EMPHASIS_MASK);
2161 switch (signal_levels) {
e3421a18 2162 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
3c5a62b5
YL
2163 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
2164 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
2165 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
2166 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
e3421a18 2167 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
3c5a62b5
YL
2168 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
2169 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
e3421a18 2170 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
3c5a62b5
YL
2171 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
2172 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
e3421a18 2173 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
3c5a62b5
YL
2174 case DP_TRAIN_VOLTAGE_SWING_1200 | DP_TRAIN_PRE_EMPHASIS_0:
2175 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
e3421a18 2176 default:
3c5a62b5
YL
2177 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
2178 "0x%x\n", signal_levels);
2179 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
e3421a18
ZW
2180 }
2181}
2182
1a2eb460
KP
2183/* Gen7's DP voltage swing and pre-emphasis control */
2184static uint32_t
2185intel_gen7_edp_signal_levels(uint8_t train_set)
2186{
2187 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
2188 DP_TRAIN_PRE_EMPHASIS_MASK);
2189 switch (signal_levels) {
2190 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
2191 return EDP_LINK_TRAIN_400MV_0DB_IVB;
2192 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
2193 return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
2194 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
2195 return EDP_LINK_TRAIN_400MV_6DB_IVB;
2196
2197 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
2198 return EDP_LINK_TRAIN_600MV_0DB_IVB;
2199 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
2200 return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
2201
2202 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
2203 return EDP_LINK_TRAIN_800MV_0DB_IVB;
2204 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
2205 return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
2206
2207 default:
2208 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
2209 "0x%x\n", signal_levels);
2210 return EDP_LINK_TRAIN_500MV_0DB_IVB;
2211 }
2212}
2213
d6c0d722
PZ
2214/* Gen7.5's (HSW) DP voltage swing and pre-emphasis control */
2215static uint32_t
f0a3424e 2216intel_hsw_signal_levels(uint8_t train_set)
a4fc5ed6 2217{
d6c0d722
PZ
2218 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
2219 DP_TRAIN_PRE_EMPHASIS_MASK);
2220 switch (signal_levels) {
2221 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
2222 return DDI_BUF_EMP_400MV_0DB_HSW;
2223 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
2224 return DDI_BUF_EMP_400MV_3_5DB_HSW;
2225 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
2226 return DDI_BUF_EMP_400MV_6DB_HSW;
2227 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_9_5:
2228 return DDI_BUF_EMP_400MV_9_5DB_HSW;
a4fc5ed6 2229
d6c0d722
PZ
2230 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
2231 return DDI_BUF_EMP_600MV_0DB_HSW;
2232 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
2233 return DDI_BUF_EMP_600MV_3_5DB_HSW;
2234 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
2235 return DDI_BUF_EMP_600MV_6DB_HSW;
a4fc5ed6 2236
d6c0d722
PZ
2237 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
2238 return DDI_BUF_EMP_800MV_0DB_HSW;
2239 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
2240 return DDI_BUF_EMP_800MV_3_5DB_HSW;
2241 default:
2242 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
2243 "0x%x\n", signal_levels);
2244 return DDI_BUF_EMP_400MV_0DB_HSW;
a4fc5ed6 2245 }
a4fc5ed6
KP
2246}
2247
f0a3424e
PZ
2248/* Properly updates "DP" with the correct signal levels. */
2249static void
2250intel_dp_set_signal_levels(struct intel_dp *intel_dp, uint32_t *DP)
2251{
2252 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
bc7d38a4 2253 enum port port = intel_dig_port->port;
f0a3424e
PZ
2254 struct drm_device *dev = intel_dig_port->base.base.dev;
2255 uint32_t signal_levels, mask;
2256 uint8_t train_set = intel_dp->train_set[0];
2257
22b8bf17 2258 if (HAS_DDI(dev)) {
f0a3424e
PZ
2259 signal_levels = intel_hsw_signal_levels(train_set);
2260 mask = DDI_BUF_EMP_MASK;
e2fa6fba
P
2261 } else if (IS_VALLEYVIEW(dev)) {
2262 signal_levels = intel_vlv_signal_levels(intel_dp);
2263 mask = 0;
bc7d38a4 2264 } else if (IS_GEN7(dev) && port == PORT_A) {
f0a3424e
PZ
2265 signal_levels = intel_gen7_edp_signal_levels(train_set);
2266 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_IVB;
bc7d38a4 2267 } else if (IS_GEN6(dev) && port == PORT_A) {
f0a3424e
PZ
2268 signal_levels = intel_gen6_edp_signal_levels(train_set);
2269 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_SNB;
2270 } else {
2271 signal_levels = intel_gen4_signal_levels(train_set);
2272 mask = DP_VOLTAGE_MASK | DP_PRE_EMPHASIS_MASK;
2273 }
2274
2275 DRM_DEBUG_KMS("Using signal levels %08x\n", signal_levels);
2276
2277 *DP = (*DP & ~mask) | signal_levels;
2278}
2279
a4fc5ed6 2280static bool
ea5b213a 2281intel_dp_set_link_train(struct intel_dp *intel_dp,
a4fc5ed6 2282 uint32_t dp_reg_value,
58e10eb9 2283 uint8_t dp_train_pat)
a4fc5ed6 2284{
174edf1f
PZ
2285 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2286 struct drm_device *dev = intel_dig_port->base.base.dev;
a4fc5ed6 2287 struct drm_i915_private *dev_priv = dev->dev_private;
174edf1f 2288 enum port port = intel_dig_port->port;
a4fc5ed6
KP
2289 int ret;
2290
22b8bf17 2291 if (HAS_DDI(dev)) {
3ab9c637 2292 uint32_t temp = I915_READ(DP_TP_CTL(port));
d6c0d722
PZ
2293
2294 if (dp_train_pat & DP_LINK_SCRAMBLING_DISABLE)
2295 temp |= DP_TP_CTL_SCRAMBLE_DISABLE;
2296 else
2297 temp &= ~DP_TP_CTL_SCRAMBLE_DISABLE;
2298
2299 temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
2300 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2301 case DP_TRAINING_PATTERN_DISABLE:
d6c0d722
PZ
2302 temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
2303
2304 break;
2305 case DP_TRAINING_PATTERN_1:
2306 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
2307 break;
2308 case DP_TRAINING_PATTERN_2:
2309 temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
2310 break;
2311 case DP_TRAINING_PATTERN_3:
2312 temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
2313 break;
2314 }
174edf1f 2315 I915_WRITE(DP_TP_CTL(port), temp);
d6c0d722 2316
bc7d38a4 2317 } else if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || port != PORT_A)) {
47ea7542
PZ
2318 dp_reg_value &= ~DP_LINK_TRAIN_MASK_CPT;
2319
2320 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2321 case DP_TRAINING_PATTERN_DISABLE:
2322 dp_reg_value |= DP_LINK_TRAIN_OFF_CPT;
2323 break;
2324 case DP_TRAINING_PATTERN_1:
2325 dp_reg_value |= DP_LINK_TRAIN_PAT_1_CPT;
2326 break;
2327 case DP_TRAINING_PATTERN_2:
2328 dp_reg_value |= DP_LINK_TRAIN_PAT_2_CPT;
2329 break;
2330 case DP_TRAINING_PATTERN_3:
2331 DRM_ERROR("DP training pattern 3 not supported\n");
2332 dp_reg_value |= DP_LINK_TRAIN_PAT_2_CPT;
2333 break;
2334 }
2335
2336 } else {
2337 dp_reg_value &= ~DP_LINK_TRAIN_MASK;
2338
2339 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2340 case DP_TRAINING_PATTERN_DISABLE:
2341 dp_reg_value |= DP_LINK_TRAIN_OFF;
2342 break;
2343 case DP_TRAINING_PATTERN_1:
2344 dp_reg_value |= DP_LINK_TRAIN_PAT_1;
2345 break;
2346 case DP_TRAINING_PATTERN_2:
2347 dp_reg_value |= DP_LINK_TRAIN_PAT_2;
2348 break;
2349 case DP_TRAINING_PATTERN_3:
2350 DRM_ERROR("DP training pattern 3 not supported\n");
2351 dp_reg_value |= DP_LINK_TRAIN_PAT_2;
2352 break;
2353 }
2354 }
2355
ea5b213a
CW
2356 I915_WRITE(intel_dp->output_reg, dp_reg_value);
2357 POSTING_READ(intel_dp->output_reg);
a4fc5ed6 2358
ea5b213a 2359 intel_dp_aux_native_write_1(intel_dp,
a4fc5ed6
KP
2360 DP_TRAINING_PATTERN_SET,
2361 dp_train_pat);
2362
47ea7542
PZ
2363 if ((dp_train_pat & DP_TRAINING_PATTERN_MASK) !=
2364 DP_TRAINING_PATTERN_DISABLE) {
2365 ret = intel_dp_aux_native_write(intel_dp,
2366 DP_TRAINING_LANE0_SET,
2367 intel_dp->train_set,
2368 intel_dp->lane_count);
2369 if (ret != intel_dp->lane_count)
2370 return false;
2371 }
a4fc5ed6
KP
2372
2373 return true;
2374}
2375
3ab9c637
ID
2376static void intel_dp_set_idle_link_train(struct intel_dp *intel_dp)
2377{
2378 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2379 struct drm_device *dev = intel_dig_port->base.base.dev;
2380 struct drm_i915_private *dev_priv = dev->dev_private;
2381 enum port port = intel_dig_port->port;
2382 uint32_t val;
2383
2384 if (!HAS_DDI(dev))
2385 return;
2386
2387 val = I915_READ(DP_TP_CTL(port));
2388 val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
2389 val |= DP_TP_CTL_LINK_TRAIN_IDLE;
2390 I915_WRITE(DP_TP_CTL(port), val);
2391
2392 /*
2393 * On PORT_A we can have only eDP in SST mode. There the only reason
2394 * we need to set idle transmission mode is to work around a HW issue
2395 * where we enable the pipe while not in idle link-training mode.
2396 * In this case there is requirement to wait for a minimum number of
2397 * idle patterns to be sent.
2398 */
2399 if (port == PORT_A)
2400 return;
2401
2402 if (wait_for((I915_READ(DP_TP_STATUS(port)) & DP_TP_STATUS_IDLE_DONE),
2403 1))
2404 DRM_ERROR("Timed out waiting for DP idle patterns\n");
2405}
2406
33a34e4e 2407/* Enable corresponding port and start training pattern 1 */
c19b0669 2408void
33a34e4e 2409intel_dp_start_link_train(struct intel_dp *intel_dp)
a4fc5ed6 2410{
da63a9f2 2411 struct drm_encoder *encoder = &dp_to_dig_port(intel_dp)->base.base;
c19b0669 2412 struct drm_device *dev = encoder->dev;
a4fc5ed6
KP
2413 int i;
2414 uint8_t voltage;
cdb0e95b 2415 int voltage_tries, loop_tries;
ea5b213a 2416 uint32_t DP = intel_dp->DP;
a4fc5ed6 2417
affa9354 2418 if (HAS_DDI(dev))
c19b0669
PZ
2419 intel_ddi_prepare_link_retrain(encoder);
2420
3cf2efb1
CW
2421 /* Write the link configuration data */
2422 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
2423 intel_dp->link_configuration,
2424 DP_LINK_CONFIGURATION_SIZE);
a4fc5ed6
KP
2425
2426 DP |= DP_PORT_EN;
1a2eb460 2427
33a34e4e 2428 memset(intel_dp->train_set, 0, 4);
a4fc5ed6 2429 voltage = 0xff;
cdb0e95b
KP
2430 voltage_tries = 0;
2431 loop_tries = 0;
a4fc5ed6 2432 for (;;) {
33a34e4e 2433 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
93f62dad 2434 uint8_t link_status[DP_LINK_STATUS_SIZE];
f0a3424e
PZ
2435
2436 intel_dp_set_signal_levels(intel_dp, &DP);
a4fc5ed6 2437
a7c9655f 2438 /* Set training pattern 1 */
47ea7542 2439 if (!intel_dp_set_link_train(intel_dp, DP,
81055854
AJ
2440 DP_TRAINING_PATTERN_1 |
2441 DP_LINK_SCRAMBLING_DISABLE))
a4fc5ed6 2442 break;
a4fc5ed6 2443
a7c9655f 2444 drm_dp_link_train_clock_recovery_delay(intel_dp->dpcd);
93f62dad
KP
2445 if (!intel_dp_get_link_status(intel_dp, link_status)) {
2446 DRM_ERROR("failed to get link status\n");
a4fc5ed6 2447 break;
93f62dad 2448 }
a4fc5ed6 2449
01916270 2450 if (drm_dp_clock_recovery_ok(link_status, intel_dp->lane_count)) {
93f62dad 2451 DRM_DEBUG_KMS("clock recovery OK\n");
3cf2efb1
CW
2452 break;
2453 }
2454
2455 /* Check to see if we've tried the max voltage */
2456 for (i = 0; i < intel_dp->lane_count; i++)
2457 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
a4fc5ed6 2458 break;
3b4f819d 2459 if (i == intel_dp->lane_count) {
b06fbda3
DV
2460 ++loop_tries;
2461 if (loop_tries == 5) {
cdb0e95b
KP
2462 DRM_DEBUG_KMS("too many full retries, give up\n");
2463 break;
2464 }
2465 memset(intel_dp->train_set, 0, 4);
2466 voltage_tries = 0;
2467 continue;
2468 }
a4fc5ed6 2469
3cf2efb1 2470 /* Check to see if we've tried the same voltage 5 times */
b06fbda3 2471 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
24773670 2472 ++voltage_tries;
b06fbda3
DV
2473 if (voltage_tries == 5) {
2474 DRM_DEBUG_KMS("too many voltage retries, give up\n");
2475 break;
2476 }
2477 } else
2478 voltage_tries = 0;
2479 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
a4fc5ed6 2480
3cf2efb1 2481 /* Compute new intel_dp->train_set as requested by target */
93f62dad 2482 intel_get_adjust_train(intel_dp, link_status);
a4fc5ed6
KP
2483 }
2484
33a34e4e
JB
2485 intel_dp->DP = DP;
2486}
2487
c19b0669 2488void
33a34e4e
JB
2489intel_dp_complete_link_train(struct intel_dp *intel_dp)
2490{
33a34e4e 2491 bool channel_eq = false;
37f80975 2492 int tries, cr_tries;
33a34e4e
JB
2493 uint32_t DP = intel_dp->DP;
2494
a4fc5ed6
KP
2495 /* channel equalization */
2496 tries = 0;
37f80975 2497 cr_tries = 0;
a4fc5ed6
KP
2498 channel_eq = false;
2499 for (;;) {
93f62dad 2500 uint8_t link_status[DP_LINK_STATUS_SIZE];
e3421a18 2501
37f80975
JB
2502 if (cr_tries > 5) {
2503 DRM_ERROR("failed to train DP, aborting\n");
2504 intel_dp_link_down(intel_dp);
2505 break;
2506 }
2507
f0a3424e 2508 intel_dp_set_signal_levels(intel_dp, &DP);
e3421a18 2509
a4fc5ed6 2510 /* channel eq pattern */
47ea7542 2511 if (!intel_dp_set_link_train(intel_dp, DP,
81055854
AJ
2512 DP_TRAINING_PATTERN_2 |
2513 DP_LINK_SCRAMBLING_DISABLE))
a4fc5ed6
KP
2514 break;
2515
a7c9655f 2516 drm_dp_link_train_channel_eq_delay(intel_dp->dpcd);
93f62dad 2517 if (!intel_dp_get_link_status(intel_dp, link_status))
a4fc5ed6 2518 break;
a4fc5ed6 2519
37f80975 2520 /* Make sure clock is still ok */
01916270 2521 if (!drm_dp_clock_recovery_ok(link_status, intel_dp->lane_count)) {
37f80975
JB
2522 intel_dp_start_link_train(intel_dp);
2523 cr_tries++;
2524 continue;
2525 }
2526
1ffdff13 2527 if (drm_dp_channel_eq_ok(link_status, intel_dp->lane_count)) {
3cf2efb1
CW
2528 channel_eq = true;
2529 break;
2530 }
a4fc5ed6 2531
37f80975
JB
2532 /* Try 5 times, then try clock recovery if that fails */
2533 if (tries > 5) {
2534 intel_dp_link_down(intel_dp);
2535 intel_dp_start_link_train(intel_dp);
2536 tries = 0;
2537 cr_tries++;
2538 continue;
2539 }
a4fc5ed6 2540
3cf2efb1 2541 /* Compute new intel_dp->train_set as requested by target */
93f62dad 2542 intel_get_adjust_train(intel_dp, link_status);
3cf2efb1 2543 ++tries;
869184a6 2544 }
3cf2efb1 2545
3ab9c637
ID
2546 intel_dp_set_idle_link_train(intel_dp);
2547
2548 intel_dp->DP = DP;
2549
d6c0d722 2550 if (channel_eq)
07f42258 2551 DRM_DEBUG_KMS("Channel EQ done. DP Training successful\n");
d6c0d722 2552
3ab9c637
ID
2553}
2554
2555void intel_dp_stop_link_train(struct intel_dp *intel_dp)
2556{
2557 intel_dp_set_link_train(intel_dp, intel_dp->DP,
2558 DP_TRAINING_PATTERN_DISABLE);
a4fc5ed6
KP
2559}
2560
2561static void
ea5b213a 2562intel_dp_link_down(struct intel_dp *intel_dp)
a4fc5ed6 2563{
da63a9f2 2564 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
bc7d38a4 2565 enum port port = intel_dig_port->port;
da63a9f2 2566 struct drm_device *dev = intel_dig_port->base.base.dev;
a4fc5ed6 2567 struct drm_i915_private *dev_priv = dev->dev_private;
ab527efc
DV
2568 struct intel_crtc *intel_crtc =
2569 to_intel_crtc(intel_dig_port->base.base.crtc);
ea5b213a 2570 uint32_t DP = intel_dp->DP;
a4fc5ed6 2571
c19b0669
PZ
2572 /*
2573 * DDI code has a strict mode set sequence and we should try to respect
2574 * it, otherwise we might hang the machine in many different ways. So we
2575 * really should be disabling the port only on a complete crtc_disable
2576 * sequence. This function is just called under two conditions on DDI
2577 * code:
2578 * - Link train failed while doing crtc_enable, and on this case we
2579 * really should respect the mode set sequence and wait for a
2580 * crtc_disable.
2581 * - Someone turned the monitor off and intel_dp_check_link_status
2582 * called us. We don't need to disable the whole port on this case, so
2583 * when someone turns the monitor on again,
2584 * intel_ddi_prepare_link_retrain will take care of redoing the link
2585 * train.
2586 */
affa9354 2587 if (HAS_DDI(dev))
c19b0669
PZ
2588 return;
2589
0c33d8d7 2590 if (WARN_ON((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0))
1b39d6f3
CW
2591 return;
2592
28c97730 2593 DRM_DEBUG_KMS("\n");
32f9d658 2594
bc7d38a4 2595 if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || port != PORT_A)) {
e3421a18 2596 DP &= ~DP_LINK_TRAIN_MASK_CPT;
ea5b213a 2597 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
e3421a18
ZW
2598 } else {
2599 DP &= ~DP_LINK_TRAIN_MASK;
ea5b213a 2600 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
e3421a18 2601 }
fe255d00 2602 POSTING_READ(intel_dp->output_reg);
5eb08b69 2603
ab527efc
DV
2604 /* We don't really know why we're doing this */
2605 intel_wait_for_vblank(dev, intel_crtc->pipe);
5eb08b69 2606
493a7081 2607 if (HAS_PCH_IBX(dev) &&
1b39d6f3 2608 I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
da63a9f2 2609 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
31acbcc4 2610
5bddd17f
EA
2611 /* Hardware workaround: leaving our transcoder select
2612 * set to transcoder B while it's off will prevent the
2613 * corresponding HDMI output on transcoder A.
2614 *
2615 * Combine this with another hardware workaround:
2616 * transcoder select bit can only be cleared while the
2617 * port is enabled.
2618 */
2619 DP &= ~DP_PIPEB_SELECT;
2620 I915_WRITE(intel_dp->output_reg, DP);
2621
2622 /* Changes to enable or select take place the vblank
2623 * after being written.
2624 */
ff50afe9
DV
2625 if (WARN_ON(crtc == NULL)) {
2626 /* We should never try to disable a port without a crtc
2627 * attached. For paranoia keep the code around for a
2628 * bit. */
31acbcc4
CW
2629 POSTING_READ(intel_dp->output_reg);
2630 msleep(50);
2631 } else
ab527efc 2632 intel_wait_for_vblank(dev, intel_crtc->pipe);
5bddd17f
EA
2633 }
2634
832afda6 2635 DP &= ~DP_AUDIO_OUTPUT_ENABLE;
ea5b213a
CW
2636 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
2637 POSTING_READ(intel_dp->output_reg);
f01eca2e 2638 msleep(intel_dp->panel_power_down_delay);
a4fc5ed6
KP
2639}
2640
26d61aad
KP
2641static bool
2642intel_dp_get_dpcd(struct intel_dp *intel_dp)
92fd8fd1 2643{
577c7a50
DL
2644 char dpcd_hex_dump[sizeof(intel_dp->dpcd) * 3];
2645
92fd8fd1 2646 if (intel_dp_aux_native_read_retry(intel_dp, 0x000, intel_dp->dpcd,
edb39244
AJ
2647 sizeof(intel_dp->dpcd)) == 0)
2648 return false; /* aux transfer failed */
92fd8fd1 2649
577c7a50
DL
2650 hex_dump_to_buffer(intel_dp->dpcd, sizeof(intel_dp->dpcd),
2651 32, 1, dpcd_hex_dump, sizeof(dpcd_hex_dump), false);
2652 DRM_DEBUG_KMS("DPCD: %s\n", dpcd_hex_dump);
2653
edb39244
AJ
2654 if (intel_dp->dpcd[DP_DPCD_REV] == 0)
2655 return false; /* DPCD not present */
2656
2293bb5c
SK
2657 /* Check if the panel supports PSR */
2658 memset(intel_dp->psr_dpcd, 0, sizeof(intel_dp->psr_dpcd));
2659 intel_dp_aux_native_read_retry(intel_dp, DP_PSR_SUPPORT,
2660 intel_dp->psr_dpcd,
2661 sizeof(intel_dp->psr_dpcd));
2662 if (is_edp_psr(intel_dp))
2663 DRM_DEBUG_KMS("Detected EDP PSR Panel.\n");
edb39244
AJ
2664 if (!(intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
2665 DP_DWN_STRM_PORT_PRESENT))
2666 return true; /* native DP sink */
2667
2668 if (intel_dp->dpcd[DP_DPCD_REV] == 0x10)
2669 return true; /* no per-port downstream info */
2670
2671 if (intel_dp_aux_native_read_retry(intel_dp, DP_DOWNSTREAM_PORT_0,
2672 intel_dp->downstream_ports,
2673 DP_MAX_DOWNSTREAM_PORTS) == 0)
2674 return false; /* downstream port status fetch failed */
2675
2676 return true;
92fd8fd1
KP
2677}
2678
0d198328
AJ
2679static void
2680intel_dp_probe_oui(struct intel_dp *intel_dp)
2681{
2682 u8 buf[3];
2683
2684 if (!(intel_dp->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
2685 return;
2686
351cfc34
DV
2687 ironlake_edp_panel_vdd_on(intel_dp);
2688
0d198328
AJ
2689 if (intel_dp_aux_native_read_retry(intel_dp, DP_SINK_OUI, buf, 3))
2690 DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
2691 buf[0], buf[1], buf[2]);
2692
2693 if (intel_dp_aux_native_read_retry(intel_dp, DP_BRANCH_OUI, buf, 3))
2694 DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
2695 buf[0], buf[1], buf[2]);
351cfc34
DV
2696
2697 ironlake_edp_panel_vdd_off(intel_dp, false);
0d198328
AJ
2698}
2699
a60f0e38
JB
2700static bool
2701intel_dp_get_sink_irq(struct intel_dp *intel_dp, u8 *sink_irq_vector)
2702{
2703 int ret;
2704
2705 ret = intel_dp_aux_native_read_retry(intel_dp,
2706 DP_DEVICE_SERVICE_IRQ_VECTOR,
2707 sink_irq_vector, 1);
2708 if (!ret)
2709 return false;
2710
2711 return true;
2712}
2713
2714static void
2715intel_dp_handle_test_request(struct intel_dp *intel_dp)
2716{
2717 /* NAK by default */
9324cf7f 2718 intel_dp_aux_native_write_1(intel_dp, DP_TEST_RESPONSE, DP_TEST_NAK);
a60f0e38
JB
2719}
2720
a4fc5ed6
KP
2721/*
2722 * According to DP spec
2723 * 5.1.2:
2724 * 1. Read DPCD
2725 * 2. Configure link according to Receiver Capabilities
2726 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
2727 * 4. Check link status on receipt of hot-plug interrupt
2728 */
2729
00c09d70 2730void
ea5b213a 2731intel_dp_check_link_status(struct intel_dp *intel_dp)
a4fc5ed6 2732{
da63a9f2 2733 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
a60f0e38 2734 u8 sink_irq_vector;
93f62dad 2735 u8 link_status[DP_LINK_STATUS_SIZE];
a60f0e38 2736
da63a9f2 2737 if (!intel_encoder->connectors_active)
d2b996ac 2738 return;
59cd09e1 2739
da63a9f2 2740 if (WARN_ON(!intel_encoder->base.crtc))
a4fc5ed6
KP
2741 return;
2742
92fd8fd1 2743 /* Try to read receiver status if the link appears to be up */
93f62dad 2744 if (!intel_dp_get_link_status(intel_dp, link_status)) {
ea5b213a 2745 intel_dp_link_down(intel_dp);
a4fc5ed6
KP
2746 return;
2747 }
2748
92fd8fd1 2749 /* Now read the DPCD to see if it's actually running */
26d61aad 2750 if (!intel_dp_get_dpcd(intel_dp)) {
59cd09e1
JB
2751 intel_dp_link_down(intel_dp);
2752 return;
2753 }
2754
a60f0e38
JB
2755 /* Try to read the source of the interrupt */
2756 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
2757 intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
2758 /* Clear interrupt source */
2759 intel_dp_aux_native_write_1(intel_dp,
2760 DP_DEVICE_SERVICE_IRQ_VECTOR,
2761 sink_irq_vector);
2762
2763 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
2764 intel_dp_handle_test_request(intel_dp);
2765 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
2766 DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
2767 }
2768
1ffdff13 2769 if (!drm_dp_channel_eq_ok(link_status, intel_dp->lane_count)) {
92fd8fd1 2770 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
da63a9f2 2771 drm_get_encoder_name(&intel_encoder->base));
33a34e4e
JB
2772 intel_dp_start_link_train(intel_dp);
2773 intel_dp_complete_link_train(intel_dp);
3ab9c637 2774 intel_dp_stop_link_train(intel_dp);
33a34e4e 2775 }
a4fc5ed6 2776}
a4fc5ed6 2777
caf9ab24 2778/* XXX this is probably wrong for multiple downstream ports */
71ba9000 2779static enum drm_connector_status
26d61aad 2780intel_dp_detect_dpcd(struct intel_dp *intel_dp)
71ba9000 2781{
caf9ab24
AJ
2782 uint8_t *dpcd = intel_dp->dpcd;
2783 bool hpd;
2784 uint8_t type;
2785
2786 if (!intel_dp_get_dpcd(intel_dp))
2787 return connector_status_disconnected;
2788
2789 /* if there's no downstream port, we're done */
2790 if (!(dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT))
26d61aad 2791 return connector_status_connected;
caf9ab24
AJ
2792
2793 /* If we're HPD-aware, SINK_COUNT changes dynamically */
2794 hpd = !!(intel_dp->downstream_ports[0] & DP_DS_PORT_HPD);
2795 if (hpd) {
23235177 2796 uint8_t reg;
caf9ab24 2797 if (!intel_dp_aux_native_read_retry(intel_dp, DP_SINK_COUNT,
23235177 2798 &reg, 1))
caf9ab24 2799 return connector_status_unknown;
23235177
AJ
2800 return DP_GET_SINK_COUNT(reg) ? connector_status_connected
2801 : connector_status_disconnected;
caf9ab24
AJ
2802 }
2803
2804 /* If no HPD, poke DDC gently */
2805 if (drm_probe_ddc(&intel_dp->adapter))
26d61aad 2806 return connector_status_connected;
caf9ab24
AJ
2807
2808 /* Well we tried, say unknown for unreliable port types */
2809 type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
2810 if (type == DP_DS_PORT_TYPE_VGA || type == DP_DS_PORT_TYPE_NON_EDID)
2811 return connector_status_unknown;
2812
2813 /* Anything else is out of spec, warn and ignore */
2814 DRM_DEBUG_KMS("Broken DP branch device, ignoring\n");
26d61aad 2815 return connector_status_disconnected;
71ba9000
AJ
2816}
2817
5eb08b69 2818static enum drm_connector_status
a9756bb5 2819ironlake_dp_detect(struct intel_dp *intel_dp)
5eb08b69 2820{
30add22d 2821 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1b469639
DL
2822 struct drm_i915_private *dev_priv = dev->dev_private;
2823 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
5eb08b69
ZW
2824 enum drm_connector_status status;
2825
fe16d949
CW
2826 /* Can't disconnect eDP, but you can close the lid... */
2827 if (is_edp(intel_dp)) {
30add22d 2828 status = intel_panel_detect(dev);
fe16d949
CW
2829 if (status == connector_status_unknown)
2830 status = connector_status_connected;
2831 return status;
2832 }
01cb9ea6 2833
1b469639
DL
2834 if (!ibx_digital_port_connected(dev_priv, intel_dig_port))
2835 return connector_status_disconnected;
2836
26d61aad 2837 return intel_dp_detect_dpcd(intel_dp);
5eb08b69
ZW
2838}
2839
a4fc5ed6 2840static enum drm_connector_status
a9756bb5 2841g4x_dp_detect(struct intel_dp *intel_dp)
a4fc5ed6 2842{
30add22d 2843 struct drm_device *dev = intel_dp_to_dev(intel_dp);
a4fc5ed6 2844 struct drm_i915_private *dev_priv = dev->dev_private;
34f2be46 2845 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
10f76a38 2846 uint32_t bit;
5eb08b69 2847
35aad75f
JB
2848 /* Can't disconnect eDP, but you can close the lid... */
2849 if (is_edp(intel_dp)) {
2850 enum drm_connector_status status;
2851
2852 status = intel_panel_detect(dev);
2853 if (status == connector_status_unknown)
2854 status = connector_status_connected;
2855 return status;
2856 }
2857
34f2be46
VS
2858 switch (intel_dig_port->port) {
2859 case PORT_B:
26739f12 2860 bit = PORTB_HOTPLUG_LIVE_STATUS;
a4fc5ed6 2861 break;
34f2be46 2862 case PORT_C:
26739f12 2863 bit = PORTC_HOTPLUG_LIVE_STATUS;
a4fc5ed6 2864 break;
34f2be46 2865 case PORT_D:
26739f12 2866 bit = PORTD_HOTPLUG_LIVE_STATUS;
a4fc5ed6
KP
2867 break;
2868 default:
2869 return connector_status_unknown;
2870 }
2871
10f76a38 2872 if ((I915_READ(PORT_HOTPLUG_STAT) & bit) == 0)
a4fc5ed6
KP
2873 return connector_status_disconnected;
2874
26d61aad 2875 return intel_dp_detect_dpcd(intel_dp);
a9756bb5
ZW
2876}
2877
8c241fef
KP
2878static struct edid *
2879intel_dp_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
2880{
9cd300e0 2881 struct intel_connector *intel_connector = to_intel_connector(connector);
d6f24d0f 2882
9cd300e0
JN
2883 /* use cached edid if we have one */
2884 if (intel_connector->edid) {
2885 struct edid *edid;
2886 int size;
2887
2888 /* invalid edid */
2889 if (IS_ERR(intel_connector->edid))
d6f24d0f
JB
2890 return NULL;
2891
9cd300e0 2892 size = (intel_connector->edid->extensions + 1) * EDID_LENGTH;
edbe1581 2893 edid = kmemdup(intel_connector->edid, size, GFP_KERNEL);
d6f24d0f
JB
2894 if (!edid)
2895 return NULL;
2896
d6f24d0f
JB
2897 return edid;
2898 }
8c241fef 2899
9cd300e0 2900 return drm_get_edid(connector, adapter);
8c241fef
KP
2901}
2902
2903static int
2904intel_dp_get_edid_modes(struct drm_connector *connector, struct i2c_adapter *adapter)
2905{
9cd300e0 2906 struct intel_connector *intel_connector = to_intel_connector(connector);
8c241fef 2907
9cd300e0
JN
2908 /* use cached edid if we have one */
2909 if (intel_connector->edid) {
2910 /* invalid edid */
2911 if (IS_ERR(intel_connector->edid))
2912 return 0;
2913
2914 return intel_connector_update_modes(connector,
2915 intel_connector->edid);
d6f24d0f
JB
2916 }
2917
9cd300e0 2918 return intel_ddc_get_modes(connector, adapter);
8c241fef
KP
2919}
2920
a9756bb5
ZW
2921static enum drm_connector_status
2922intel_dp_detect(struct drm_connector *connector, bool force)
2923{
2924 struct intel_dp *intel_dp = intel_attached_dp(connector);
d63885da
PZ
2925 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2926 struct intel_encoder *intel_encoder = &intel_dig_port->base;
fa90ecef 2927 struct drm_device *dev = connector->dev;
a9756bb5
ZW
2928 enum drm_connector_status status;
2929 struct edid *edid = NULL;
2930
164c8598
CW
2931 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2932 connector->base.id, drm_get_connector_name(connector));
2933
a9756bb5
ZW
2934 intel_dp->has_audio = false;
2935
2936 if (HAS_PCH_SPLIT(dev))
2937 status = ironlake_dp_detect(intel_dp);
2938 else
2939 status = g4x_dp_detect(intel_dp);
1b9be9d0 2940
a9756bb5
ZW
2941 if (status != connector_status_connected)
2942 return status;
2943
0d198328
AJ
2944 intel_dp_probe_oui(intel_dp);
2945
c3e5f67b
DV
2946 if (intel_dp->force_audio != HDMI_AUDIO_AUTO) {
2947 intel_dp->has_audio = (intel_dp->force_audio == HDMI_AUDIO_ON);
f684960e 2948 } else {
8c241fef 2949 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
f684960e
CW
2950 if (edid) {
2951 intel_dp->has_audio = drm_detect_monitor_audio(edid);
f684960e
CW
2952 kfree(edid);
2953 }
a9756bb5
ZW
2954 }
2955
d63885da
PZ
2956 if (intel_encoder->type != INTEL_OUTPUT_EDP)
2957 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
a9756bb5 2958 return connector_status_connected;
a4fc5ed6
KP
2959}
2960
2961static int intel_dp_get_modes(struct drm_connector *connector)
2962{
df0e9248 2963 struct intel_dp *intel_dp = intel_attached_dp(connector);
dd06f90e 2964 struct intel_connector *intel_connector = to_intel_connector(connector);
fa90ecef 2965 struct drm_device *dev = connector->dev;
32f9d658 2966 int ret;
a4fc5ed6
KP
2967
2968 /* We should parse the EDID data and find out if it has an audio sink
2969 */
2970
8c241fef 2971 ret = intel_dp_get_edid_modes(connector, &intel_dp->adapter);
f8779fda 2972 if (ret)
32f9d658
ZW
2973 return ret;
2974
f8779fda 2975 /* if eDP has no EDID, fall back to fixed mode */
dd06f90e 2976 if (is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
f8779fda 2977 struct drm_display_mode *mode;
dd06f90e
JN
2978 mode = drm_mode_duplicate(dev,
2979 intel_connector->panel.fixed_mode);
f8779fda 2980 if (mode) {
32f9d658
ZW
2981 drm_mode_probed_add(connector, mode);
2982 return 1;
2983 }
2984 }
2985 return 0;
a4fc5ed6
KP
2986}
2987
1aad7ac0
CW
2988static bool
2989intel_dp_detect_audio(struct drm_connector *connector)
2990{
2991 struct intel_dp *intel_dp = intel_attached_dp(connector);
2992 struct edid *edid;
2993 bool has_audio = false;
2994
8c241fef 2995 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
1aad7ac0
CW
2996 if (edid) {
2997 has_audio = drm_detect_monitor_audio(edid);
1aad7ac0
CW
2998 kfree(edid);
2999 }
3000
3001 return has_audio;
3002}
3003
f684960e
CW
3004static int
3005intel_dp_set_property(struct drm_connector *connector,
3006 struct drm_property *property,
3007 uint64_t val)
3008{
e953fd7b 3009 struct drm_i915_private *dev_priv = connector->dev->dev_private;
53b41837 3010 struct intel_connector *intel_connector = to_intel_connector(connector);
da63a9f2
PZ
3011 struct intel_encoder *intel_encoder = intel_attached_encoder(connector);
3012 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
f684960e
CW
3013 int ret;
3014
662595df 3015 ret = drm_object_property_set_value(&connector->base, property, val);
f684960e
CW
3016 if (ret)
3017 return ret;
3018
3f43c48d 3019 if (property == dev_priv->force_audio_property) {
1aad7ac0
CW
3020 int i = val;
3021 bool has_audio;
3022
3023 if (i == intel_dp->force_audio)
f684960e
CW
3024 return 0;
3025
1aad7ac0 3026 intel_dp->force_audio = i;
f684960e 3027
c3e5f67b 3028 if (i == HDMI_AUDIO_AUTO)
1aad7ac0
CW
3029 has_audio = intel_dp_detect_audio(connector);
3030 else
c3e5f67b 3031 has_audio = (i == HDMI_AUDIO_ON);
1aad7ac0
CW
3032
3033 if (has_audio == intel_dp->has_audio)
f684960e
CW
3034 return 0;
3035
1aad7ac0 3036 intel_dp->has_audio = has_audio;
f684960e
CW
3037 goto done;
3038 }
3039
e953fd7b 3040 if (property == dev_priv->broadcast_rgb_property) {
ae4edb80
DV
3041 bool old_auto = intel_dp->color_range_auto;
3042 uint32_t old_range = intel_dp->color_range;
3043
55bc60db
VS
3044 switch (val) {
3045 case INTEL_BROADCAST_RGB_AUTO:
3046 intel_dp->color_range_auto = true;
3047 break;
3048 case INTEL_BROADCAST_RGB_FULL:
3049 intel_dp->color_range_auto = false;
3050 intel_dp->color_range = 0;
3051 break;
3052 case INTEL_BROADCAST_RGB_LIMITED:
3053 intel_dp->color_range_auto = false;
3054 intel_dp->color_range = DP_COLOR_RANGE_16_235;
3055 break;
3056 default:
3057 return -EINVAL;
3058 }
ae4edb80
DV
3059
3060 if (old_auto == intel_dp->color_range_auto &&
3061 old_range == intel_dp->color_range)
3062 return 0;
3063
e953fd7b
CW
3064 goto done;
3065 }
3066
53b41837
YN
3067 if (is_edp(intel_dp) &&
3068 property == connector->dev->mode_config.scaling_mode_property) {
3069 if (val == DRM_MODE_SCALE_NONE) {
3070 DRM_DEBUG_KMS("no scaling not supported\n");
3071 return -EINVAL;
3072 }
3073
3074 if (intel_connector->panel.fitting_mode == val) {
3075 /* the eDP scaling property is not changed */
3076 return 0;
3077 }
3078 intel_connector->panel.fitting_mode = val;
3079
3080 goto done;
3081 }
3082
f684960e
CW
3083 return -EINVAL;
3084
3085done:
c0c36b94
CW
3086 if (intel_encoder->base.crtc)
3087 intel_crtc_restore_mode(intel_encoder->base.crtc);
f684960e
CW
3088
3089 return 0;
3090}
3091
a4fc5ed6 3092static void
73845adf 3093intel_dp_connector_destroy(struct drm_connector *connector)
a4fc5ed6 3094{
1d508706 3095 struct intel_connector *intel_connector = to_intel_connector(connector);
aaa6fd2a 3096
9cd300e0
JN
3097 if (!IS_ERR_OR_NULL(intel_connector->edid))
3098 kfree(intel_connector->edid);
3099
acd8db10
PZ
3100 /* Can't call is_edp() since the encoder may have been destroyed
3101 * already. */
3102 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
1d508706 3103 intel_panel_fini(&intel_connector->panel);
aaa6fd2a 3104
a4fc5ed6
KP
3105 drm_sysfs_connector_remove(connector);
3106 drm_connector_cleanup(connector);
55f78c43 3107 kfree(connector);
a4fc5ed6
KP
3108}
3109
00c09d70 3110void intel_dp_encoder_destroy(struct drm_encoder *encoder)
24d05927 3111{
da63a9f2
PZ
3112 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
3113 struct intel_dp *intel_dp = &intel_dig_port->dp;
bd173813 3114 struct drm_device *dev = intel_dp_to_dev(intel_dp);
24d05927
DV
3115
3116 i2c_del_adapter(&intel_dp->adapter);
3117 drm_encoder_cleanup(encoder);
bd943159
KP
3118 if (is_edp(intel_dp)) {
3119 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
bd173813 3120 mutex_lock(&dev->mode_config.mutex);
bd943159 3121 ironlake_panel_vdd_off_sync(intel_dp);
bd173813 3122 mutex_unlock(&dev->mode_config.mutex);
bd943159 3123 }
da63a9f2 3124 kfree(intel_dig_port);
24d05927
DV
3125}
3126
a4fc5ed6 3127static const struct drm_connector_funcs intel_dp_connector_funcs = {
2bd2ad64 3128 .dpms = intel_connector_dpms,
a4fc5ed6
KP
3129 .detect = intel_dp_detect,
3130 .fill_modes = drm_helper_probe_single_connector_modes,
f684960e 3131 .set_property = intel_dp_set_property,
73845adf 3132 .destroy = intel_dp_connector_destroy,
a4fc5ed6
KP
3133};
3134
3135static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
3136 .get_modes = intel_dp_get_modes,
3137 .mode_valid = intel_dp_mode_valid,
df0e9248 3138 .best_encoder = intel_best_encoder,
a4fc5ed6
KP
3139};
3140
a4fc5ed6 3141static const struct drm_encoder_funcs intel_dp_enc_funcs = {
24d05927 3142 .destroy = intel_dp_encoder_destroy,
a4fc5ed6
KP
3143};
3144
995b6762 3145static void
21d40d37 3146intel_dp_hot_plug(struct intel_encoder *intel_encoder)
c8110e52 3147{
fa90ecef 3148 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
c8110e52 3149
885a5014 3150 intel_dp_check_link_status(intel_dp);
c8110e52 3151}
6207937d 3152
e3421a18
ZW
3153/* Return which DP Port should be selected for Transcoder DP control */
3154int
0206e353 3155intel_trans_dp_port_sel(struct drm_crtc *crtc)
e3421a18
ZW
3156{
3157 struct drm_device *dev = crtc->dev;
fa90ecef
PZ
3158 struct intel_encoder *intel_encoder;
3159 struct intel_dp *intel_dp;
e3421a18 3160
fa90ecef
PZ
3161 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
3162 intel_dp = enc_to_intel_dp(&intel_encoder->base);
e3421a18 3163
fa90ecef
PZ
3164 if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
3165 intel_encoder->type == INTEL_OUTPUT_EDP)
ea5b213a 3166 return intel_dp->output_reg;
e3421a18 3167 }
ea5b213a 3168
e3421a18
ZW
3169 return -1;
3170}
3171
36e83a18 3172/* check the VBT to see whether the eDP is on DP-D port */
cb0953d7 3173bool intel_dpd_is_edp(struct drm_device *dev)
36e83a18
ZY
3174{
3175 struct drm_i915_private *dev_priv = dev->dev_private;
3176 struct child_device_config *p_child;
3177 int i;
3178
41aa3448 3179 if (!dev_priv->vbt.child_dev_num)
36e83a18
ZY
3180 return false;
3181
41aa3448
RV
3182 for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
3183 p_child = dev_priv->vbt.child_dev + i;
36e83a18
ZY
3184
3185 if (p_child->dvo_port == PORT_IDPD &&
3186 p_child->device_type == DEVICE_TYPE_eDP)
3187 return true;
3188 }
3189 return false;
3190}
3191
f684960e
CW
3192static void
3193intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
3194{
53b41837
YN
3195 struct intel_connector *intel_connector = to_intel_connector(connector);
3196
3f43c48d 3197 intel_attach_force_audio_property(connector);
e953fd7b 3198 intel_attach_broadcast_rgb_property(connector);
55bc60db 3199 intel_dp->color_range_auto = true;
53b41837
YN
3200
3201 if (is_edp(intel_dp)) {
3202 drm_mode_create_scaling_mode_property(connector->dev);
6de6d846
RC
3203 drm_object_attach_property(
3204 &connector->base,
53b41837 3205 connector->dev->mode_config.scaling_mode_property,
8e740cd1
YN
3206 DRM_MODE_SCALE_ASPECT);
3207 intel_connector->panel.fitting_mode = DRM_MODE_SCALE_ASPECT;
53b41837 3208 }
f684960e
CW
3209}
3210
67a54566
DV
3211static void
3212intel_dp_init_panel_power_sequencer(struct drm_device *dev,
f30d26e4
JN
3213 struct intel_dp *intel_dp,
3214 struct edp_power_seq *out)
67a54566
DV
3215{
3216 struct drm_i915_private *dev_priv = dev->dev_private;
3217 struct edp_power_seq cur, vbt, spec, final;
3218 u32 pp_on, pp_off, pp_div, pp;
bf13e81b 3219 int pp_ctrl_reg, pp_on_reg, pp_off_reg, pp_div_reg;
453c5420
JB
3220
3221 if (HAS_PCH_SPLIT(dev)) {
bf13e81b 3222 pp_ctrl_reg = PCH_PP_CONTROL;
453c5420
JB
3223 pp_on_reg = PCH_PP_ON_DELAYS;
3224 pp_off_reg = PCH_PP_OFF_DELAYS;
3225 pp_div_reg = PCH_PP_DIVISOR;
3226 } else {
bf13e81b
JN
3227 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
3228
3229 pp_ctrl_reg = VLV_PIPE_PP_CONTROL(pipe);
3230 pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
3231 pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
3232 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
453c5420 3233 }
67a54566
DV
3234
3235 /* Workaround: Need to write PP_CONTROL with the unlock key as
3236 * the very first thing. */
453c5420 3237 pp = ironlake_get_pp_control(intel_dp);
bf13e81b 3238 I915_WRITE(pp_ctrl_reg, pp);
67a54566 3239
453c5420
JB
3240 pp_on = I915_READ(pp_on_reg);
3241 pp_off = I915_READ(pp_off_reg);
3242 pp_div = I915_READ(pp_div_reg);
67a54566
DV
3243
3244 /* Pull timing values out of registers */
3245 cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
3246 PANEL_POWER_UP_DELAY_SHIFT;
3247
3248 cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
3249 PANEL_LIGHT_ON_DELAY_SHIFT;
3250
3251 cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
3252 PANEL_LIGHT_OFF_DELAY_SHIFT;
3253
3254 cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
3255 PANEL_POWER_DOWN_DELAY_SHIFT;
3256
3257 cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
3258 PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
3259
3260 DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
3261 cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
3262
41aa3448 3263 vbt = dev_priv->vbt.edp_pps;
67a54566
DV
3264
3265 /* Upper limits from eDP 1.3 spec. Note that we use the clunky units of
3266 * our hw here, which are all in 100usec. */
3267 spec.t1_t3 = 210 * 10;
3268 spec.t8 = 50 * 10; /* no limit for t8, use t7 instead */
3269 spec.t9 = 50 * 10; /* no limit for t9, make it symmetric with t8 */
3270 spec.t10 = 500 * 10;
3271 /* This one is special and actually in units of 100ms, but zero
3272 * based in the hw (so we need to add 100 ms). But the sw vbt
3273 * table multiplies it with 1000 to make it in units of 100usec,
3274 * too. */
3275 spec.t11_t12 = (510 + 100) * 10;
3276
3277 DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
3278 vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
3279
3280 /* Use the max of the register settings and vbt. If both are
3281 * unset, fall back to the spec limits. */
3282#define assign_final(field) final.field = (max(cur.field, vbt.field) == 0 ? \
3283 spec.field : \
3284 max(cur.field, vbt.field))
3285 assign_final(t1_t3);
3286 assign_final(t8);
3287 assign_final(t9);
3288 assign_final(t10);
3289 assign_final(t11_t12);
3290#undef assign_final
3291
3292#define get_delay(field) (DIV_ROUND_UP(final.field, 10))
3293 intel_dp->panel_power_up_delay = get_delay(t1_t3);
3294 intel_dp->backlight_on_delay = get_delay(t8);
3295 intel_dp->backlight_off_delay = get_delay(t9);
3296 intel_dp->panel_power_down_delay = get_delay(t10);
3297 intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
3298#undef get_delay
3299
f30d26e4
JN
3300 DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
3301 intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
3302 intel_dp->panel_power_cycle_delay);
3303
3304 DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
3305 intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
3306
3307 if (out)
3308 *out = final;
3309}
3310
3311static void
3312intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
3313 struct intel_dp *intel_dp,
3314 struct edp_power_seq *seq)
3315{
3316 struct drm_i915_private *dev_priv = dev->dev_private;
453c5420
JB
3317 u32 pp_on, pp_off, pp_div, port_sel = 0;
3318 int div = HAS_PCH_SPLIT(dev) ? intel_pch_rawclk(dev) : intel_hrawclk(dev);
3319 int pp_on_reg, pp_off_reg, pp_div_reg;
3320
3321 if (HAS_PCH_SPLIT(dev)) {
3322 pp_on_reg = PCH_PP_ON_DELAYS;
3323 pp_off_reg = PCH_PP_OFF_DELAYS;
3324 pp_div_reg = PCH_PP_DIVISOR;
3325 } else {
bf13e81b
JN
3326 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
3327
3328 pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
3329 pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
3330 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
453c5420
JB
3331 }
3332
67a54566 3333 /* And finally store the new values in the power sequencer. */
f30d26e4
JN
3334 pp_on = (seq->t1_t3 << PANEL_POWER_UP_DELAY_SHIFT) |
3335 (seq->t8 << PANEL_LIGHT_ON_DELAY_SHIFT);
3336 pp_off = (seq->t9 << PANEL_LIGHT_OFF_DELAY_SHIFT) |
3337 (seq->t10 << PANEL_POWER_DOWN_DELAY_SHIFT);
67a54566
DV
3338 /* Compute the divisor for the pp clock, simply match the Bspec
3339 * formula. */
453c5420 3340 pp_div = ((100 * div)/2 - 1) << PP_REFERENCE_DIVIDER_SHIFT;
f30d26e4 3341 pp_div |= (DIV_ROUND_UP(seq->t11_t12, 1000)
67a54566
DV
3342 << PANEL_POWER_CYCLE_DELAY_SHIFT);
3343
3344 /* Haswell doesn't have any port selection bits for the panel
3345 * power sequencer any more. */
bc7d38a4 3346 if (IS_VALLEYVIEW(dev)) {
bf13e81b
JN
3347 if (dp_to_dig_port(intel_dp)->port == PORT_B)
3348 port_sel = PANEL_PORT_SELECT_DPB_VLV;
3349 else
3350 port_sel = PANEL_PORT_SELECT_DPC_VLV;
bc7d38a4
ID
3351 } else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
3352 if (dp_to_dig_port(intel_dp)->port == PORT_A)
a24c144c 3353 port_sel = PANEL_PORT_SELECT_DPA;
67a54566 3354 else
a24c144c 3355 port_sel = PANEL_PORT_SELECT_DPD;
67a54566
DV
3356 }
3357
453c5420
JB
3358 pp_on |= port_sel;
3359
3360 I915_WRITE(pp_on_reg, pp_on);
3361 I915_WRITE(pp_off_reg, pp_off);
3362 I915_WRITE(pp_div_reg, pp_div);
67a54566 3363
67a54566 3364 DRM_DEBUG_KMS("panel power sequencer register settings: PP_ON %#x, PP_OFF %#x, PP_DIV %#x\n",
453c5420
JB
3365 I915_READ(pp_on_reg),
3366 I915_READ(pp_off_reg),
3367 I915_READ(pp_div_reg));
f684960e
CW
3368}
3369
ed92f0b2
PZ
3370static bool intel_edp_init_connector(struct intel_dp *intel_dp,
3371 struct intel_connector *intel_connector)
3372{
3373 struct drm_connector *connector = &intel_connector->base;
3374 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3375 struct drm_device *dev = intel_dig_port->base.base.dev;
3376 struct drm_i915_private *dev_priv = dev->dev_private;
3377 struct drm_display_mode *fixed_mode = NULL;
3378 struct edp_power_seq power_seq = { 0 };
3379 bool has_dpcd;
3380 struct drm_display_mode *scan;
3381 struct edid *edid;
3382
3383 if (!is_edp(intel_dp))
3384 return true;
3385
3386 intel_dp_init_panel_power_sequencer(dev, intel_dp, &power_seq);
3387
3388 /* Cache DPCD and EDID for edp. */
3389 ironlake_edp_panel_vdd_on(intel_dp);
3390 has_dpcd = intel_dp_get_dpcd(intel_dp);
3391 ironlake_edp_panel_vdd_off(intel_dp, false);
3392
3393 if (has_dpcd) {
3394 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
3395 dev_priv->no_aux_handshake =
3396 intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
3397 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
3398 } else {
3399 /* if this fails, presume the device is a ghost */
3400 DRM_INFO("failed to retrieve link info, disabling eDP\n");
ed92f0b2
PZ
3401 return false;
3402 }
3403
3404 /* We now know it's not a ghost, init power sequence regs. */
3405 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp,
3406 &power_seq);
3407
3408 ironlake_edp_panel_vdd_on(intel_dp);
3409 edid = drm_get_edid(connector, &intel_dp->adapter);
3410 if (edid) {
3411 if (drm_add_edid_modes(connector, edid)) {
3412 drm_mode_connector_update_edid_property(connector,
3413 edid);
3414 drm_edid_to_eld(connector, edid);
3415 } else {
3416 kfree(edid);
3417 edid = ERR_PTR(-EINVAL);
3418 }
3419 } else {
3420 edid = ERR_PTR(-ENOENT);
3421 }
3422 intel_connector->edid = edid;
3423
3424 /* prefer fixed mode from EDID if available */
3425 list_for_each_entry(scan, &connector->probed_modes, head) {
3426 if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
3427 fixed_mode = drm_mode_duplicate(dev, scan);
3428 break;
3429 }
3430 }
3431
3432 /* fallback to VBT if available for eDP */
3433 if (!fixed_mode && dev_priv->vbt.lfp_lvds_vbt_mode) {
3434 fixed_mode = drm_mode_duplicate(dev,
3435 dev_priv->vbt.lfp_lvds_vbt_mode);
3436 if (fixed_mode)
3437 fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
3438 }
3439
3440 ironlake_edp_panel_vdd_off(intel_dp, false);
3441
3442 intel_panel_init(&intel_connector->panel, fixed_mode);
3443 intel_panel_setup_backlight(connector);
3444
3445 return true;
3446}
3447
16c25533 3448bool
f0fec3f2
PZ
3449intel_dp_init_connector(struct intel_digital_port *intel_dig_port,
3450 struct intel_connector *intel_connector)
a4fc5ed6 3451{
f0fec3f2
PZ
3452 struct drm_connector *connector = &intel_connector->base;
3453 struct intel_dp *intel_dp = &intel_dig_port->dp;
3454 struct intel_encoder *intel_encoder = &intel_dig_port->base;
3455 struct drm_device *dev = intel_encoder->base.dev;
a4fc5ed6 3456 struct drm_i915_private *dev_priv = dev->dev_private;
174edf1f 3457 enum port port = intel_dig_port->port;
5eb08b69 3458 const char *name = NULL;
b2a14755 3459 int type, error;
a4fc5ed6 3460
0767935e
DV
3461 /* Preserve the current hw state. */
3462 intel_dp->DP = I915_READ(intel_dp->output_reg);
dd06f90e 3463 intel_dp->attached_connector = intel_connector;
3d3dc149 3464
f7d24902 3465 type = DRM_MODE_CONNECTOR_DisplayPort;
19c03924
GB
3466 /*
3467 * FIXME : We need to initialize built-in panels before external panels.
3468 * For X0, DP_C is fixed as eDP. Revisit this as part of VLV eDP cleanup
3469 */
f7d24902
ID
3470 switch (port) {
3471 case PORT_A:
b329530c 3472 type = DRM_MODE_CONNECTOR_eDP;
f7d24902
ID
3473 break;
3474 case PORT_C:
3475 if (IS_VALLEYVIEW(dev))
3476 type = DRM_MODE_CONNECTOR_eDP;
3477 break;
3478 case PORT_D:
3479 if (HAS_PCH_SPLIT(dev) && intel_dpd_is_edp(dev))
3480 type = DRM_MODE_CONNECTOR_eDP;
3481 break;
3482 default: /* silence GCC warning */
3483 break;
b329530c
AJ
3484 }
3485
f7d24902
ID
3486 /*
3487 * For eDP we always set the encoder type to INTEL_OUTPUT_EDP, but
3488 * for DP the encoder type can be set by the caller to
3489 * INTEL_OUTPUT_UNKNOWN for DDI, so don't rewrite it.
3490 */
3491 if (type == DRM_MODE_CONNECTOR_eDP)
3492 intel_encoder->type = INTEL_OUTPUT_EDP;
3493
e7281eab
ID
3494 DRM_DEBUG_KMS("Adding %s connector on port %c\n",
3495 type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
3496 port_name(port));
3497
b329530c 3498 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
a4fc5ed6
KP
3499 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
3500
a4fc5ed6
KP
3501 connector->interlace_allowed = true;
3502 connector->doublescan_allowed = 0;
3503
f0fec3f2
PZ
3504 INIT_DELAYED_WORK(&intel_dp->panel_vdd_work,
3505 ironlake_panel_vdd_work);
a4fc5ed6 3506
df0e9248 3507 intel_connector_attach_encoder(intel_connector, intel_encoder);
a4fc5ed6
KP
3508 drm_sysfs_connector_add(connector);
3509
affa9354 3510 if (HAS_DDI(dev))
bcbc889b
PZ
3511 intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
3512 else
3513 intel_connector->get_hw_state = intel_connector_get_hw_state;
3514
9ed35ab1
PZ
3515 intel_dp->aux_ch_ctl_reg = intel_dp->output_reg + 0x10;
3516 if (HAS_DDI(dev)) {
3517 switch (intel_dig_port->port) {
3518 case PORT_A:
3519 intel_dp->aux_ch_ctl_reg = DPA_AUX_CH_CTL;
3520 break;
3521 case PORT_B:
3522 intel_dp->aux_ch_ctl_reg = PCH_DPB_AUX_CH_CTL;
3523 break;
3524 case PORT_C:
3525 intel_dp->aux_ch_ctl_reg = PCH_DPC_AUX_CH_CTL;
3526 break;
3527 case PORT_D:
3528 intel_dp->aux_ch_ctl_reg = PCH_DPD_AUX_CH_CTL;
3529 break;
3530 default:
3531 BUG();
3532 }
3533 }
e8cb4558 3534
a4fc5ed6 3535 /* Set up the DDC bus. */
ab9d7c30
PZ
3536 switch (port) {
3537 case PORT_A:
1d843f9d 3538 intel_encoder->hpd_pin = HPD_PORT_A;
ab9d7c30
PZ
3539 name = "DPDDC-A";
3540 break;
3541 case PORT_B:
1d843f9d 3542 intel_encoder->hpd_pin = HPD_PORT_B;
ab9d7c30
PZ
3543 name = "DPDDC-B";
3544 break;
3545 case PORT_C:
1d843f9d 3546 intel_encoder->hpd_pin = HPD_PORT_C;
ab9d7c30
PZ
3547 name = "DPDDC-C";
3548 break;
3549 case PORT_D:
1d843f9d 3550 intel_encoder->hpd_pin = HPD_PORT_D;
ab9d7c30
PZ
3551 name = "DPDDC-D";
3552 break;
3553 default:
ad1c0b19 3554 BUG();
5eb08b69
ZW
3555 }
3556
b2a14755
PZ
3557 error = intel_dp_i2c_init(intel_dp, intel_connector, name);
3558 WARN(error, "intel_dp_i2c_init failed with error %d for port %c\n",
3559 error, port_name(port));
c1f05264 3560
2b28bb1b
RV
3561 intel_dp->psr_setup_done = false;
3562
b2f246a8 3563 if (!intel_edp_init_connector(intel_dp, intel_connector)) {
15b1d171
PZ
3564 i2c_del_adapter(&intel_dp->adapter);
3565 if (is_edp(intel_dp)) {
3566 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
3567 mutex_lock(&dev->mode_config.mutex);
3568 ironlake_panel_vdd_off_sync(intel_dp);
3569 mutex_unlock(&dev->mode_config.mutex);
3570 }
b2f246a8
PZ
3571 drm_sysfs_connector_remove(connector);
3572 drm_connector_cleanup(connector);
16c25533 3573 return false;
b2f246a8 3574 }
32f9d658 3575
f684960e
CW
3576 intel_dp_add_properties(intel_dp, connector);
3577
a4fc5ed6
KP
3578 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
3579 * 0xd. Failure to do so will result in spurious interrupts being
3580 * generated on the port when a cable is not attached.
3581 */
3582 if (IS_G4X(dev) && !IS_GM45(dev)) {
3583 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
3584 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
3585 }
16c25533
PZ
3586
3587 return true;
a4fc5ed6 3588}
f0fec3f2
PZ
3589
3590void
3591intel_dp_init(struct drm_device *dev, int output_reg, enum port port)
3592{
3593 struct intel_digital_port *intel_dig_port;
3594 struct intel_encoder *intel_encoder;
3595 struct drm_encoder *encoder;
3596 struct intel_connector *intel_connector;
3597
3598 intel_dig_port = kzalloc(sizeof(struct intel_digital_port), GFP_KERNEL);
3599 if (!intel_dig_port)
3600 return;
3601
3602 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
3603 if (!intel_connector) {
3604 kfree(intel_dig_port);
3605 return;
3606 }
3607
3608 intel_encoder = &intel_dig_port->base;
3609 encoder = &intel_encoder->base;
3610
3611 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
3612 DRM_MODE_ENCODER_TMDS);
3613
5bfe2ac0 3614 intel_encoder->compute_config = intel_dp_compute_config;
b934223d 3615 intel_encoder->mode_set = intel_dp_mode_set;
00c09d70
PZ
3616 intel_encoder->disable = intel_disable_dp;
3617 intel_encoder->post_disable = intel_post_disable_dp;
3618 intel_encoder->get_hw_state = intel_dp_get_hw_state;
045ac3b5 3619 intel_encoder->get_config = intel_dp_get_config;
ab1f90f9 3620 if (IS_VALLEYVIEW(dev)) {
ecff4f3b 3621 intel_encoder->pre_pll_enable = vlv_dp_pre_pll_enable;
ab1f90f9
JN
3622 intel_encoder->pre_enable = vlv_pre_enable_dp;
3623 intel_encoder->enable = vlv_enable_dp;
3624 } else {
ecff4f3b
JN
3625 intel_encoder->pre_enable = g4x_pre_enable_dp;
3626 intel_encoder->enable = g4x_enable_dp;
ab1f90f9 3627 }
f0fec3f2 3628
174edf1f 3629 intel_dig_port->port = port;
f0fec3f2
PZ
3630 intel_dig_port->dp.output_reg = output_reg;
3631
00c09d70 3632 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
f0fec3f2
PZ
3633 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
3634 intel_encoder->cloneable = false;
3635 intel_encoder->hot_plug = intel_dp_hot_plug;
3636
15b1d171
PZ
3637 if (!intel_dp_init_connector(intel_dig_port, intel_connector)) {
3638 drm_encoder_cleanup(encoder);
3639 kfree(intel_dig_port);
b2f246a8 3640 kfree(intel_connector);
15b1d171 3641 }
f0fec3f2 3642}
This page took 0.772345 seconds and 5 git commands to generate.