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