]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/gpu/drm/i915/intel_dp.c
drm/i915: Clear pfit registers when not used by any outputs
[mirror_ubuntu-focal-kernel.git] / drivers / gpu / drm / i915 / intel_dp.c
CommitLineData
a4fc5ed6
KP
1/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Keith Packard <keithp@keithp.com>
25 *
26 */
27
28#include <linux/i2c.h>
5a0e3ad6 29#include <linux/slab.h>
a4fc5ed6
KP
30#include "drmP.h"
31#include "drm.h"
32#include "drm_crtc.h"
33#include "drm_crtc_helper.h"
34#include "intel_drv.h"
35#include "i915_drm.h"
36#include "i915_drv.h"
ab2c0672 37#include "drm_dp_helper.h"
a4fc5ed6 38
ae266c98 39
a4fc5ed6
KP
40#define DP_LINK_STATUS_SIZE 6
41#define DP_LINK_CHECK_TIMEOUT (10 * 1000)
42
43#define DP_LINK_CONFIGURATION_SIZE 9
44
ea5b213a
CW
45struct intel_dp {
46 struct intel_encoder base;
a4fc5ed6
KP
47 uint32_t output_reg;
48 uint32_t DP;
49 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
a4fc5ed6 50 bool has_audio;
f684960e 51 int force_audio;
c8110e52 52 int dpms_mode;
a4fc5ed6
KP
53 uint8_t link_bw;
54 uint8_t lane_count;
55 uint8_t dpcd[4];
a4fc5ed6
KP
56 struct i2c_adapter adapter;
57 struct i2c_algo_dp_aux_data algo;
f0917379 58 bool is_pch_edp;
33a34e4e
JB
59 uint8_t train_set[4];
60 uint8_t link_status[DP_LINK_STATUS_SIZE];
f684960e
CW
61
62 struct drm_property *force_audio_property;
a4fc5ed6
KP
63};
64
cfcb0fc9
JB
65/**
66 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
67 * @intel_dp: DP struct
68 *
69 * If a CPU or PCH DP output is attached to an eDP panel, this function
70 * will return true, and false otherwise.
71 */
72static bool is_edp(struct intel_dp *intel_dp)
73{
74 return intel_dp->base.type == INTEL_OUTPUT_EDP;
75}
76
77/**
78 * is_pch_edp - is the port on the PCH and attached to an eDP panel?
79 * @intel_dp: DP struct
80 *
81 * Returns true if the given DP struct corresponds to a PCH DP port attached
82 * to an eDP panel, false otherwise. Helpful for determining whether we
83 * may need FDI resources for a given DP output or not.
84 */
85static bool is_pch_edp(struct intel_dp *intel_dp)
86{
87 return intel_dp->is_pch_edp;
88}
89
ea5b213a
CW
90static struct intel_dp *enc_to_intel_dp(struct drm_encoder *encoder)
91{
4ef69c7a 92 return container_of(encoder, struct intel_dp, base.base);
ea5b213a 93}
a4fc5ed6 94
df0e9248
CW
95static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
96{
97 return container_of(intel_attached_encoder(connector),
98 struct intel_dp, base);
99}
100
814948ad
JB
101/**
102 * intel_encoder_is_pch_edp - is the given encoder a PCH attached eDP?
103 * @encoder: DRM encoder
104 *
105 * Return true if @encoder corresponds to a PCH attached eDP panel. Needed
106 * by intel_display.c.
107 */
108bool intel_encoder_is_pch_edp(struct drm_encoder *encoder)
109{
110 struct intel_dp *intel_dp;
111
112 if (!encoder)
113 return false;
114
115 intel_dp = enc_to_intel_dp(encoder);
116
117 return is_pch_edp(intel_dp);
118}
119
33a34e4e
JB
120static void intel_dp_start_link_train(struct intel_dp *intel_dp);
121static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
ea5b213a 122static void intel_dp_link_down(struct intel_dp *intel_dp);
a4fc5ed6 123
32f9d658 124void
21d40d37 125intel_edp_link_config (struct intel_encoder *intel_encoder,
ea5b213a 126 int *lane_num, int *link_bw)
32f9d658 127{
ea5b213a 128 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
32f9d658 129
ea5b213a
CW
130 *lane_num = intel_dp->lane_count;
131 if (intel_dp->link_bw == DP_LINK_BW_1_62)
32f9d658 132 *link_bw = 162000;
ea5b213a 133 else if (intel_dp->link_bw == DP_LINK_BW_2_7)
32f9d658
ZW
134 *link_bw = 270000;
135}
136
a4fc5ed6 137static int
ea5b213a 138intel_dp_max_lane_count(struct intel_dp *intel_dp)
a4fc5ed6 139{
a4fc5ed6
KP
140 int max_lane_count = 4;
141
ea5b213a
CW
142 if (intel_dp->dpcd[0] >= 0x11) {
143 max_lane_count = intel_dp->dpcd[2] & 0x1f;
a4fc5ed6
KP
144 switch (max_lane_count) {
145 case 1: case 2: case 4:
146 break;
147 default:
148 max_lane_count = 4;
149 }
150 }
151 return max_lane_count;
152}
153
154static int
ea5b213a 155intel_dp_max_link_bw(struct intel_dp *intel_dp)
a4fc5ed6 156{
ea5b213a 157 int max_link_bw = intel_dp->dpcd[1];
a4fc5ed6
KP
158
159 switch (max_link_bw) {
160 case DP_LINK_BW_1_62:
161 case DP_LINK_BW_2_7:
162 break;
163 default:
164 max_link_bw = DP_LINK_BW_1_62;
165 break;
166 }
167 return max_link_bw;
168}
169
170static int
171intel_dp_link_clock(uint8_t link_bw)
172{
173 if (link_bw == DP_LINK_BW_2_7)
174 return 270000;
175 else
176 return 162000;
177}
178
179/* I think this is a fiction */
180static int
ea5b213a 181intel_dp_link_required(struct drm_device *dev, struct intel_dp *intel_dp, int pixel_clock)
a4fc5ed6 182{
885a5fb5
ZW
183 struct drm_i915_private *dev_priv = dev->dev_private;
184
4d926461 185 if (is_edp(intel_dp))
5ceb0f9b 186 return (pixel_clock * dev_priv->edp.bpp + 7) / 8;
885a5fb5
ZW
187 else
188 return pixel_clock * 3;
a4fc5ed6
KP
189}
190
fe27d53e
DA
191static int
192intel_dp_max_data_rate(int max_link_clock, int max_lanes)
193{
194 return (max_link_clock * max_lanes * 8) / 10;
195}
196
a4fc5ed6
KP
197static int
198intel_dp_mode_valid(struct drm_connector *connector,
199 struct drm_display_mode *mode)
200{
df0e9248 201 struct intel_dp *intel_dp = intel_attached_dp(connector);
7de56f43
ZY
202 struct drm_device *dev = connector->dev;
203 struct drm_i915_private *dev_priv = dev->dev_private;
ea5b213a
CW
204 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
205 int max_lanes = intel_dp_max_lane_count(intel_dp);
a4fc5ed6 206
4d926461 207 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
7de56f43
ZY
208 if (mode->hdisplay > dev_priv->panel_fixed_mode->hdisplay)
209 return MODE_PANEL;
210
211 if (mode->vdisplay > dev_priv->panel_fixed_mode->vdisplay)
212 return MODE_PANEL;
213 }
214
fe27d53e
DA
215 /* only refuse the mode on non eDP since we have seen some wierd eDP panels
216 which are outside spec tolerances but somehow work by magic */
cfcb0fc9 217 if (!is_edp(intel_dp) &&
ea5b213a 218 (intel_dp_link_required(connector->dev, intel_dp, mode->clock)
fe27d53e 219 > intel_dp_max_data_rate(max_link_clock, max_lanes)))
a4fc5ed6
KP
220 return MODE_CLOCK_HIGH;
221
222 if (mode->clock < 10000)
223 return MODE_CLOCK_LOW;
224
225 return MODE_OK;
226}
227
228static uint32_t
229pack_aux(uint8_t *src, int src_bytes)
230{
231 int i;
232 uint32_t v = 0;
233
234 if (src_bytes > 4)
235 src_bytes = 4;
236 for (i = 0; i < src_bytes; i++)
237 v |= ((uint32_t) src[i]) << ((3-i) * 8);
238 return v;
239}
240
241static void
242unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
243{
244 int i;
245 if (dst_bytes > 4)
246 dst_bytes = 4;
247 for (i = 0; i < dst_bytes; i++)
248 dst[i] = src >> ((3-i) * 8);
249}
250
fb0f8fbf
KP
251/* hrawclock is 1/4 the FSB frequency */
252static int
253intel_hrawclk(struct drm_device *dev)
254{
255 struct drm_i915_private *dev_priv = dev->dev_private;
256 uint32_t clkcfg;
257
258 clkcfg = I915_READ(CLKCFG);
259 switch (clkcfg & CLKCFG_FSB_MASK) {
260 case CLKCFG_FSB_400:
261 return 100;
262 case CLKCFG_FSB_533:
263 return 133;
264 case CLKCFG_FSB_667:
265 return 166;
266 case CLKCFG_FSB_800:
267 return 200;
268 case CLKCFG_FSB_1067:
269 return 266;
270 case CLKCFG_FSB_1333:
271 return 333;
272 /* these two are just a guess; one of them might be right */
273 case CLKCFG_FSB_1600:
274 case CLKCFG_FSB_1600_ALT:
275 return 400;
276 default:
277 return 133;
278 }
279}
280
a4fc5ed6 281static int
ea5b213a 282intel_dp_aux_ch(struct intel_dp *intel_dp,
a4fc5ed6
KP
283 uint8_t *send, int send_bytes,
284 uint8_t *recv, int recv_size)
285{
ea5b213a 286 uint32_t output_reg = intel_dp->output_reg;
4ef69c7a 287 struct drm_device *dev = intel_dp->base.base.dev;
a4fc5ed6
KP
288 struct drm_i915_private *dev_priv = dev->dev_private;
289 uint32_t ch_ctl = output_reg + 0x10;
290 uint32_t ch_data = ch_ctl + 4;
291 int i;
292 int recv_bytes;
a4fc5ed6 293 uint32_t status;
fb0f8fbf 294 uint32_t aux_clock_divider;
e3421a18 295 int try, precharge;
a4fc5ed6
KP
296
297 /* The clock divider is based off the hrawclk,
fb0f8fbf
KP
298 * and would like to run at 2MHz. So, take the
299 * hrawclk value and divide by 2 and use that
6176b8f9
JB
300 *
301 * Note that PCH attached eDP panels should use a 125MHz input
302 * clock divider.
a4fc5ed6 303 */
cfcb0fc9 304 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
e3421a18
ZW
305 if (IS_GEN6(dev))
306 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
307 else
308 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
309 } else if (HAS_PCH_SPLIT(dev))
f2b115e6 310 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
5eb08b69
ZW
311 else
312 aux_clock_divider = intel_hrawclk(dev) / 2;
313
e3421a18
ZW
314 if (IS_GEN6(dev))
315 precharge = 3;
316 else
317 precharge = 5;
318
4f7f7b7e
CW
319 if (I915_READ(ch_ctl) & DP_AUX_CH_CTL_SEND_BUSY) {
320 DRM_ERROR("dp_aux_ch not started status 0x%08x\n",
321 I915_READ(ch_ctl));
322 return -EBUSY;
323 }
324
fb0f8fbf
KP
325 /* Must try at least 3 times according to DP spec */
326 for (try = 0; try < 5; try++) {
327 /* Load the send data into the aux channel data registers */
4f7f7b7e
CW
328 for (i = 0; i < send_bytes; i += 4)
329 I915_WRITE(ch_data + i,
330 pack_aux(send + i, send_bytes - i));
fb0f8fbf
KP
331
332 /* Send the command and wait for it to complete */
4f7f7b7e
CW
333 I915_WRITE(ch_ctl,
334 DP_AUX_CH_CTL_SEND_BUSY |
335 DP_AUX_CH_CTL_TIME_OUT_400us |
336 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
337 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
338 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
339 DP_AUX_CH_CTL_DONE |
340 DP_AUX_CH_CTL_TIME_OUT_ERROR |
341 DP_AUX_CH_CTL_RECEIVE_ERROR);
fb0f8fbf 342 for (;;) {
fb0f8fbf
KP
343 status = I915_READ(ch_ctl);
344 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
345 break;
4f7f7b7e 346 udelay(100);
fb0f8fbf
KP
347 }
348
349 /* Clear done status and any errors */
4f7f7b7e
CW
350 I915_WRITE(ch_ctl,
351 status |
352 DP_AUX_CH_CTL_DONE |
353 DP_AUX_CH_CTL_TIME_OUT_ERROR |
354 DP_AUX_CH_CTL_RECEIVE_ERROR);
355 if (status & DP_AUX_CH_CTL_DONE)
a4fc5ed6
KP
356 break;
357 }
358
a4fc5ed6 359 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
1ae8c0a5 360 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
a5b3da54 361 return -EBUSY;
a4fc5ed6
KP
362 }
363
364 /* Check for timeout or receive error.
365 * Timeouts occur when the sink is not connected
366 */
a5b3da54 367 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
1ae8c0a5 368 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
a5b3da54
KP
369 return -EIO;
370 }
1ae8c0a5
KP
371
372 /* Timeouts occur when the device isn't connected, so they're
373 * "normal" -- don't fill the kernel log with these */
a5b3da54 374 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
28c97730 375 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
a5b3da54 376 return -ETIMEDOUT;
a4fc5ed6
KP
377 }
378
379 /* Unload any bytes sent back from the other side */
380 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
381 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
a4fc5ed6
KP
382 if (recv_bytes > recv_size)
383 recv_bytes = recv_size;
384
4f7f7b7e
CW
385 for (i = 0; i < recv_bytes; i += 4)
386 unpack_aux(I915_READ(ch_data + i),
387 recv + i, recv_bytes - i);
a4fc5ed6
KP
388
389 return recv_bytes;
390}
391
392/* Write data to the aux channel in native mode */
393static int
ea5b213a 394intel_dp_aux_native_write(struct intel_dp *intel_dp,
a4fc5ed6
KP
395 uint16_t address, uint8_t *send, int send_bytes)
396{
397 int ret;
398 uint8_t msg[20];
399 int msg_bytes;
400 uint8_t ack;
401
402 if (send_bytes > 16)
403 return -1;
404 msg[0] = AUX_NATIVE_WRITE << 4;
405 msg[1] = address >> 8;
eebc863e 406 msg[2] = address & 0xff;
a4fc5ed6
KP
407 msg[3] = send_bytes - 1;
408 memcpy(&msg[4], send, send_bytes);
409 msg_bytes = send_bytes + 4;
410 for (;;) {
ea5b213a 411 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
a4fc5ed6
KP
412 if (ret < 0)
413 return ret;
414 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
415 break;
416 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
417 udelay(100);
418 else
a5b3da54 419 return -EIO;
a4fc5ed6
KP
420 }
421 return send_bytes;
422}
423
424/* Write a single byte to the aux channel in native mode */
425static int
ea5b213a 426intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
a4fc5ed6
KP
427 uint16_t address, uint8_t byte)
428{
ea5b213a 429 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
a4fc5ed6
KP
430}
431
432/* read bytes from a native aux channel */
433static int
ea5b213a 434intel_dp_aux_native_read(struct intel_dp *intel_dp,
a4fc5ed6
KP
435 uint16_t address, uint8_t *recv, int recv_bytes)
436{
437 uint8_t msg[4];
438 int msg_bytes;
439 uint8_t reply[20];
440 int reply_bytes;
441 uint8_t ack;
442 int ret;
443
444 msg[0] = AUX_NATIVE_READ << 4;
445 msg[1] = address >> 8;
446 msg[2] = address & 0xff;
447 msg[3] = recv_bytes - 1;
448
449 msg_bytes = 4;
450 reply_bytes = recv_bytes + 1;
451
452 for (;;) {
ea5b213a 453 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
a4fc5ed6 454 reply, reply_bytes);
a5b3da54
KP
455 if (ret == 0)
456 return -EPROTO;
457 if (ret < 0)
a4fc5ed6
KP
458 return ret;
459 ack = reply[0];
460 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
461 memcpy(recv, reply + 1, ret - 1);
462 return ret - 1;
463 }
464 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
465 udelay(100);
466 else
a5b3da54 467 return -EIO;
a4fc5ed6
KP
468 }
469}
470
471static int
ab2c0672
DA
472intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
473 uint8_t write_byte, uint8_t *read_byte)
a4fc5ed6 474{
ab2c0672 475 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
ea5b213a
CW
476 struct intel_dp *intel_dp = container_of(adapter,
477 struct intel_dp,
478 adapter);
ab2c0672
DA
479 uint16_t address = algo_data->address;
480 uint8_t msg[5];
481 uint8_t reply[2];
482 int msg_bytes;
483 int reply_bytes;
484 int ret;
485
486 /* Set up the command byte */
487 if (mode & MODE_I2C_READ)
488 msg[0] = AUX_I2C_READ << 4;
489 else
490 msg[0] = AUX_I2C_WRITE << 4;
491
492 if (!(mode & MODE_I2C_STOP))
493 msg[0] |= AUX_I2C_MOT << 4;
a4fc5ed6 494
ab2c0672
DA
495 msg[1] = address >> 8;
496 msg[2] = address;
497
498 switch (mode) {
499 case MODE_I2C_WRITE:
500 msg[3] = 0;
501 msg[4] = write_byte;
502 msg_bytes = 5;
503 reply_bytes = 1;
504 break;
505 case MODE_I2C_READ:
506 msg[3] = 0;
507 msg_bytes = 4;
508 reply_bytes = 2;
509 break;
510 default:
511 msg_bytes = 3;
512 reply_bytes = 1;
513 break;
514 }
515
516 for (;;) {
ea5b213a 517 ret = intel_dp_aux_ch(intel_dp,
ab2c0672
DA
518 msg, msg_bytes,
519 reply, reply_bytes);
520 if (ret < 0) {
3ff99164 521 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
ab2c0672
DA
522 return ret;
523 }
524 switch (reply[0] & AUX_I2C_REPLY_MASK) {
525 case AUX_I2C_REPLY_ACK:
526 if (mode == MODE_I2C_READ) {
527 *read_byte = reply[1];
528 }
529 return reply_bytes - 1;
530 case AUX_I2C_REPLY_NACK:
3ff99164 531 DRM_DEBUG_KMS("aux_ch nack\n");
ab2c0672
DA
532 return -EREMOTEIO;
533 case AUX_I2C_REPLY_DEFER:
3ff99164 534 DRM_DEBUG_KMS("aux_ch defer\n");
ab2c0672
DA
535 udelay(100);
536 break;
537 default:
538 DRM_ERROR("aux_ch invalid reply 0x%02x\n", reply[0]);
539 return -EREMOTEIO;
540 }
541 }
a4fc5ed6
KP
542}
543
544static int
ea5b213a 545intel_dp_i2c_init(struct intel_dp *intel_dp,
55f78c43 546 struct intel_connector *intel_connector, const char *name)
a4fc5ed6 547{
d54e9d28 548 DRM_DEBUG_KMS("i2c_init %s\n", name);
ea5b213a
CW
549 intel_dp->algo.running = false;
550 intel_dp->algo.address = 0;
551 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
552
553 memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
554 intel_dp->adapter.owner = THIS_MODULE;
555 intel_dp->adapter.class = I2C_CLASS_DDC;
556 strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
557 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
558 intel_dp->adapter.algo_data = &intel_dp->algo;
559 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
560
561 return i2c_dp_aux_add_bus(&intel_dp->adapter);
a4fc5ed6
KP
562}
563
564static bool
565intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
566 struct drm_display_mode *adjusted_mode)
567{
0d3a1bee
ZY
568 struct drm_device *dev = encoder->dev;
569 struct drm_i915_private *dev_priv = dev->dev_private;
ea5b213a 570 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
a4fc5ed6 571 int lane_count, clock;
ea5b213a
CW
572 int max_lane_count = intel_dp_max_lane_count(intel_dp);
573 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
a4fc5ed6
KP
574 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
575
4d926461 576 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
1d8e1c75
CW
577 intel_fixed_panel_mode(dev_priv->panel_fixed_mode, adjusted_mode);
578 intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
579 mode, adjusted_mode);
0d3a1bee
ZY
580 /*
581 * the mode->clock is used to calculate the Data&Link M/N
582 * of the pipe. For the eDP the fixed clock should be used.
583 */
584 mode->clock = dev_priv->panel_fixed_mode->clock;
585 }
586
869184a6
JB
587 /* Just use VBT values for eDP */
588 if (is_edp(intel_dp)) {
589 intel_dp->lane_count = dev_priv->edp.lanes;
590 intel_dp->link_bw = dev_priv->edp.rate;
591 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
592 DRM_DEBUG_KMS("eDP link bw %02x lane count %d clock %d\n",
593 intel_dp->link_bw, intel_dp->lane_count,
594 adjusted_mode->clock);
595 return true;
596 }
597
a4fc5ed6
KP
598 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
599 for (clock = 0; clock <= max_clock; clock++) {
fe27d53e 600 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
a4fc5ed6 601
ea5b213a 602 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
885a5fb5 603 <= link_avail) {
ea5b213a
CW
604 intel_dp->link_bw = bws[clock];
605 intel_dp->lane_count = lane_count;
606 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
28c97730
ZY
607 DRM_DEBUG_KMS("Display port link bw %02x lane "
608 "count %d clock %d\n",
ea5b213a 609 intel_dp->link_bw, intel_dp->lane_count,
a4fc5ed6
KP
610 adjusted_mode->clock);
611 return true;
612 }
613 }
614 }
fe27d53e 615
a4fc5ed6
KP
616 return false;
617}
618
619struct intel_dp_m_n {
620 uint32_t tu;
621 uint32_t gmch_m;
622 uint32_t gmch_n;
623 uint32_t link_m;
624 uint32_t link_n;
625};
626
627static void
628intel_reduce_ratio(uint32_t *num, uint32_t *den)
629{
630 while (*num > 0xffffff || *den > 0xffffff) {
631 *num >>= 1;
632 *den >>= 1;
633 }
634}
635
636static void
36e83a18 637intel_dp_compute_m_n(int bpp,
a4fc5ed6
KP
638 int nlanes,
639 int pixel_clock,
640 int link_clock,
641 struct intel_dp_m_n *m_n)
642{
643 m_n->tu = 64;
36e83a18 644 m_n->gmch_m = (pixel_clock * bpp) >> 3;
a4fc5ed6
KP
645 m_n->gmch_n = link_clock * nlanes;
646 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
647 m_n->link_m = pixel_clock;
648 m_n->link_n = link_clock;
649 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
650}
651
652void
653intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
654 struct drm_display_mode *adjusted_mode)
655{
656 struct drm_device *dev = crtc->dev;
657 struct drm_mode_config *mode_config = &dev->mode_config;
55f78c43 658 struct drm_encoder *encoder;
a4fc5ed6
KP
659 struct drm_i915_private *dev_priv = dev->dev_private;
660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
36e83a18 661 int lane_count = 4, bpp = 24;
a4fc5ed6
KP
662 struct intel_dp_m_n m_n;
663
664 /*
21d40d37 665 * Find the lane count in the intel_encoder private
a4fc5ed6 666 */
55f78c43 667 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
ea5b213a 668 struct intel_dp *intel_dp;
a4fc5ed6 669
d8201ab6 670 if (encoder->crtc != crtc)
a4fc5ed6
KP
671 continue;
672
ea5b213a
CW
673 intel_dp = enc_to_intel_dp(encoder);
674 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
675 lane_count = intel_dp->lane_count;
51190667
JB
676 break;
677 } else if (is_edp(intel_dp)) {
678 lane_count = dev_priv->edp.lanes;
679 bpp = dev_priv->edp.bpp;
a4fc5ed6
KP
680 break;
681 }
682 }
683
684 /*
685 * Compute the GMCH and Link ratios. The '3' here is
686 * the number of bytes_per_pixel post-LUT, which we always
687 * set up for 8-bits of R/G/B, or 3 bytes total.
688 */
36e83a18 689 intel_dp_compute_m_n(bpp, lane_count,
a4fc5ed6
KP
690 mode->clock, adjusted_mode->clock, &m_n);
691
c619eed4 692 if (HAS_PCH_SPLIT(dev)) {
5eb08b69
ZW
693 if (intel_crtc->pipe == 0) {
694 I915_WRITE(TRANSA_DATA_M1,
695 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
696 m_n.gmch_m);
697 I915_WRITE(TRANSA_DATA_N1, m_n.gmch_n);
698 I915_WRITE(TRANSA_DP_LINK_M1, m_n.link_m);
699 I915_WRITE(TRANSA_DP_LINK_N1, m_n.link_n);
700 } else {
701 I915_WRITE(TRANSB_DATA_M1,
702 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
703 m_n.gmch_m);
704 I915_WRITE(TRANSB_DATA_N1, m_n.gmch_n);
705 I915_WRITE(TRANSB_DP_LINK_M1, m_n.link_m);
706 I915_WRITE(TRANSB_DP_LINK_N1, m_n.link_n);
707 }
a4fc5ed6 708 } else {
5eb08b69
ZW
709 if (intel_crtc->pipe == 0) {
710 I915_WRITE(PIPEA_GMCH_DATA_M,
711 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
712 m_n.gmch_m);
713 I915_WRITE(PIPEA_GMCH_DATA_N,
714 m_n.gmch_n);
715 I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
716 I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
717 } else {
718 I915_WRITE(PIPEB_GMCH_DATA_M,
719 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
720 m_n.gmch_m);
721 I915_WRITE(PIPEB_GMCH_DATA_N,
722 m_n.gmch_n);
723 I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
724 I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
725 }
a4fc5ed6
KP
726 }
727}
728
729static void
730intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
731 struct drm_display_mode *adjusted_mode)
732{
e3421a18 733 struct drm_device *dev = encoder->dev;
ea5b213a 734 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4ef69c7a 735 struct drm_crtc *crtc = intel_dp->base.base.crtc;
a4fc5ed6
KP
736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
737
ea5b213a 738 intel_dp->DP = (DP_VOLTAGE_0_4 |
9c9e7927
AJ
739 DP_PRE_EMPHASIS_0);
740
741 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
ea5b213a 742 intel_dp->DP |= DP_SYNC_HS_HIGH;
9c9e7927 743 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
ea5b213a 744 intel_dp->DP |= DP_SYNC_VS_HIGH;
a4fc5ed6 745
cfcb0fc9 746 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
ea5b213a 747 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
e3421a18 748 else
ea5b213a 749 intel_dp->DP |= DP_LINK_TRAIN_OFF;
a4fc5ed6 750
ea5b213a 751 switch (intel_dp->lane_count) {
a4fc5ed6 752 case 1:
ea5b213a 753 intel_dp->DP |= DP_PORT_WIDTH_1;
a4fc5ed6
KP
754 break;
755 case 2:
ea5b213a 756 intel_dp->DP |= DP_PORT_WIDTH_2;
a4fc5ed6
KP
757 break;
758 case 4:
ea5b213a 759 intel_dp->DP |= DP_PORT_WIDTH_4;
a4fc5ed6
KP
760 break;
761 }
ea5b213a
CW
762 if (intel_dp->has_audio)
763 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
a4fc5ed6 764
ea5b213a
CW
765 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
766 intel_dp->link_configuration[0] = intel_dp->link_bw;
767 intel_dp->link_configuration[1] = intel_dp->lane_count;
a4fc5ed6
KP
768
769 /*
9962c925 770 * Check for DPCD version > 1.1 and enhanced framing support
a4fc5ed6 771 */
ea5b213a
CW
772 if (intel_dp->dpcd[0] >= 0x11 && (intel_dp->dpcd[2] & DP_ENHANCED_FRAME_CAP)) {
773 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
774 intel_dp->DP |= DP_ENHANCED_FRAMING;
a4fc5ed6
KP
775 }
776
e3421a18
ZW
777 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
778 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
ea5b213a 779 intel_dp->DP |= DP_PIPEB_SELECT;
32f9d658 780
895692be 781 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
32f9d658 782 /* don't miss out required setting for eDP */
ea5b213a 783 intel_dp->DP |= DP_PLL_ENABLE;
32f9d658 784 if (adjusted_mode->clock < 200000)
ea5b213a 785 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
32f9d658 786 else
ea5b213a 787 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
32f9d658 788 }
a4fc5ed6
KP
789}
790
7eaf5547 791/* Returns true if the panel was already on when called */
01cb9ea6 792static bool ironlake_edp_panel_on (struct intel_dp *intel_dp)
9934c132 793{
01cb9ea6 794 struct drm_device *dev = intel_dp->base.base.dev;
9934c132 795 struct drm_i915_private *dev_priv = dev->dev_private;
01cb9ea6 796 u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_STATE_ON_IDLE;
9934c132 797
913d8d11 798 if (I915_READ(PCH_PP_STATUS) & PP_ON)
7eaf5547 799 return true;
9934c132
JB
800
801 pp = I915_READ(PCH_PP_CONTROL);
37c6c9b0
JB
802
803 /* ILK workaround: disable reset around power sequence */
804 pp &= ~PANEL_POWER_RESET;
805 I915_WRITE(PCH_PP_CONTROL, pp);
806 POSTING_READ(PCH_PP_CONTROL);
807
01cb9ea6 808 pp |= PANEL_UNLOCK_REGS | POWER_TARGET_ON;
9934c132 809 I915_WRITE(PCH_PP_CONTROL, pp);
01cb9ea6 810 POSTING_READ(PCH_PP_CONTROL);
9934c132 811
27d64339
HV
812 /* Ouch. We need to wait here for some panels, like Dell e6510
813 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
814 */
815 msleep(300);
816
01cb9ea6
JB
817 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_on_mask) == idle_on_mask,
818 5000))
913d8d11
CW
819 DRM_ERROR("panel on wait timed out: 0x%08x\n",
820 I915_READ(PCH_PP_STATUS));
9934c132 821
37c6c9b0 822 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
9934c132 823 I915_WRITE(PCH_PP_CONTROL, pp);
37c6c9b0 824 POSTING_READ(PCH_PP_CONTROL);
7eaf5547
JB
825
826 return false;
9934c132
JB
827}
828
829static void ironlake_edp_panel_off (struct drm_device *dev)
830{
831 struct drm_i915_private *dev_priv = dev->dev_private;
01cb9ea6
JB
832 u32 pp, idle_off_mask = PP_ON | PP_SEQUENCE_MASK |
833 PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK;
9934c132
JB
834
835 pp = I915_READ(PCH_PP_CONTROL);
37c6c9b0
JB
836
837 /* ILK workaround: disable reset around power sequence */
838 pp &= ~PANEL_POWER_RESET;
839 I915_WRITE(PCH_PP_CONTROL, pp);
840 POSTING_READ(PCH_PP_CONTROL);
841
9934c132
JB
842 pp &= ~POWER_TARGET_ON;
843 I915_WRITE(PCH_PP_CONTROL, pp);
01cb9ea6 844 POSTING_READ(PCH_PP_CONTROL);
9934c132 845
01cb9ea6 846 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_off_mask) == 0, 5000))
913d8d11
CW
847 DRM_ERROR("panel off wait timed out: 0x%08x\n",
848 I915_READ(PCH_PP_STATUS));
9934c132 849
3969c9c9 850 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
9934c132 851 I915_WRITE(PCH_PP_CONTROL, pp);
37c6c9b0 852 POSTING_READ(PCH_PP_CONTROL);
27d64339
HV
853
854 /* Ouch. We need to wait here for some panels, like Dell e6510
855 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
856 */
857 msleep(300);
9934c132
JB
858}
859
f2b115e6 860static void ironlake_edp_backlight_on (struct drm_device *dev)
32f9d658
ZW
861{
862 struct drm_i915_private *dev_priv = dev->dev_private;
863 u32 pp;
864
28c97730 865 DRM_DEBUG_KMS("\n");
01cb9ea6
JB
866 /*
867 * If we enable the backlight right away following a panel power
868 * on, we may see slight flicker as the panel syncs with the eDP
869 * link. So delay a bit to make sure the image is solid before
870 * allowing it to appear.
871 */
872 msleep(300);
32f9d658
ZW
873 pp = I915_READ(PCH_PP_CONTROL);
874 pp |= EDP_BLC_ENABLE;
875 I915_WRITE(PCH_PP_CONTROL, pp);
876}
877
f2b115e6 878static void ironlake_edp_backlight_off (struct drm_device *dev)
32f9d658
ZW
879{
880 struct drm_i915_private *dev_priv = dev->dev_private;
881 u32 pp;
882
28c97730 883 DRM_DEBUG_KMS("\n");
32f9d658
ZW
884 pp = I915_READ(PCH_PP_CONTROL);
885 pp &= ~EDP_BLC_ENABLE;
886 I915_WRITE(PCH_PP_CONTROL, pp);
887}
a4fc5ed6 888
d240f20f
JB
889static void ironlake_edp_pll_on(struct drm_encoder *encoder)
890{
891 struct drm_device *dev = encoder->dev;
892 struct drm_i915_private *dev_priv = dev->dev_private;
893 u32 dpa_ctl;
894
895 DRM_DEBUG_KMS("\n");
896 dpa_ctl = I915_READ(DP_A);
298b0b39 897 dpa_ctl |= DP_PLL_ENABLE;
d240f20f 898 I915_WRITE(DP_A, dpa_ctl);
298b0b39
JB
899 POSTING_READ(DP_A);
900 udelay(200);
d240f20f
JB
901}
902
903static void ironlake_edp_pll_off(struct drm_encoder *encoder)
904{
905 struct drm_device *dev = encoder->dev;
906 struct drm_i915_private *dev_priv = dev->dev_private;
907 u32 dpa_ctl;
908
909 dpa_ctl = I915_READ(DP_A);
298b0b39 910 dpa_ctl &= ~DP_PLL_ENABLE;
d240f20f 911 I915_WRITE(DP_A, dpa_ctl);
1af5fa1b 912 POSTING_READ(DP_A);
d240f20f
JB
913 udelay(200);
914}
915
916static void intel_dp_prepare(struct drm_encoder *encoder)
917{
918 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
919 struct drm_device *dev = encoder->dev;
d240f20f 920
4d926461 921 if (is_edp(intel_dp)) {
d240f20f 922 ironlake_edp_backlight_off(dev);
01cb9ea6
JB
923 ironlake_edp_panel_on(intel_dp);
924 if (!is_pch_edp(intel_dp))
925 ironlake_edp_pll_on(encoder);
926 else
927 ironlake_edp_pll_off(encoder);
d240f20f 928 }
736085bc 929 intel_dp_link_down(intel_dp);
d240f20f
JB
930}
931
932static void intel_dp_commit(struct drm_encoder *encoder)
933{
934 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
935 struct drm_device *dev = encoder->dev;
d240f20f 936
33a34e4e
JB
937 intel_dp_start_link_train(intel_dp);
938
4d926461 939 if (is_edp(intel_dp))
01cb9ea6 940 ironlake_edp_panel_on(intel_dp);
33a34e4e
JB
941
942 intel_dp_complete_link_train(intel_dp);
943
4d926461 944 if (is_edp(intel_dp))
d240f20f
JB
945 ironlake_edp_backlight_on(dev);
946}
947
a4fc5ed6
KP
948static void
949intel_dp_dpms(struct drm_encoder *encoder, int mode)
950{
ea5b213a 951 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
55f78c43 952 struct drm_device *dev = encoder->dev;
a4fc5ed6 953 struct drm_i915_private *dev_priv = dev->dev_private;
ea5b213a 954 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
a4fc5ed6
KP
955
956 if (mode != DRM_MODE_DPMS_ON) {
01cb9ea6 957 if (is_edp(intel_dp))
7643a7fa 958 ironlake_edp_backlight_off(dev);
736085bc 959 intel_dp_link_down(intel_dp);
4d926461 960 if (is_edp(intel_dp))
01cb9ea6
JB
961 ironlake_edp_panel_off(dev);
962 if (is_edp(intel_dp) && !is_pch_edp(intel_dp))
d240f20f 963 ironlake_edp_pll_off(encoder);
a4fc5ed6 964 } else {
736085bc
JB
965 if (is_edp(intel_dp))
966 ironlake_edp_panel_on(intel_dp);
32f9d658 967 if (!(dp_reg & DP_PORT_EN)) {
01cb9ea6 968 intel_dp_start_link_train(intel_dp);
33a34e4e 969 intel_dp_complete_link_train(intel_dp);
32f9d658 970 }
736085bc
JB
971 if (is_edp(intel_dp))
972 ironlake_edp_backlight_on(dev);
a4fc5ed6 973 }
ea5b213a 974 intel_dp->dpms_mode = mode;
a4fc5ed6
KP
975}
976
977/*
978 * Fetch AUX CH registers 0x202 - 0x207 which contain
979 * link status information
980 */
981static bool
33a34e4e 982intel_dp_get_link_status(struct intel_dp *intel_dp)
a4fc5ed6
KP
983{
984 int ret;
985
ea5b213a 986 ret = intel_dp_aux_native_read(intel_dp,
a4fc5ed6 987 DP_LANE0_1_STATUS,
33a34e4e 988 intel_dp->link_status, DP_LINK_STATUS_SIZE);
a4fc5ed6
KP
989 if (ret != DP_LINK_STATUS_SIZE)
990 return false;
991 return true;
992}
993
994static uint8_t
995intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
996 int r)
997{
998 return link_status[r - DP_LANE0_1_STATUS];
999}
1000
a4fc5ed6
KP
1001static uint8_t
1002intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
1003 int lane)
1004{
1005 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1006 int s = ((lane & 1) ?
1007 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1008 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1009 uint8_t l = intel_dp_link_status(link_status, i);
1010
1011 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1012}
1013
1014static uint8_t
1015intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1016 int lane)
1017{
1018 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1019 int s = ((lane & 1) ?
1020 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1021 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1022 uint8_t l = intel_dp_link_status(link_status, i);
1023
1024 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1025}
1026
1027
1028#if 0
1029static char *voltage_names[] = {
1030 "0.4V", "0.6V", "0.8V", "1.2V"
1031};
1032static char *pre_emph_names[] = {
1033 "0dB", "3.5dB", "6dB", "9.5dB"
1034};
1035static char *link_train_names[] = {
1036 "pattern 1", "pattern 2", "idle", "off"
1037};
1038#endif
1039
1040/*
1041 * These are source-specific values; current Intel hardware supports
1042 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1043 */
1044#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1045
1046static uint8_t
1047intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1048{
1049 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1050 case DP_TRAIN_VOLTAGE_SWING_400:
1051 return DP_TRAIN_PRE_EMPHASIS_6;
1052 case DP_TRAIN_VOLTAGE_SWING_600:
1053 return DP_TRAIN_PRE_EMPHASIS_6;
1054 case DP_TRAIN_VOLTAGE_SWING_800:
1055 return DP_TRAIN_PRE_EMPHASIS_3_5;
1056 case DP_TRAIN_VOLTAGE_SWING_1200:
1057 default:
1058 return DP_TRAIN_PRE_EMPHASIS_0;
1059 }
1060}
1061
1062static void
33a34e4e 1063intel_get_adjust_train(struct intel_dp *intel_dp)
a4fc5ed6
KP
1064{
1065 uint8_t v = 0;
1066 uint8_t p = 0;
1067 int lane;
1068
33a34e4e
JB
1069 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1070 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1071 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
a4fc5ed6
KP
1072
1073 if (this_v > v)
1074 v = this_v;
1075 if (this_p > p)
1076 p = this_p;
1077 }
1078
1079 if (v >= I830_DP_VOLTAGE_MAX)
1080 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1081
1082 if (p >= intel_dp_pre_emphasis_max(v))
1083 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1084
1085 for (lane = 0; lane < 4; lane++)
33a34e4e 1086 intel_dp->train_set[lane] = v | p;
a4fc5ed6
KP
1087}
1088
1089static uint32_t
869184a6 1090intel_dp_signal_levels(struct intel_dp *intel_dp)
a4fc5ed6 1091{
869184a6
JB
1092 struct drm_device *dev = intel_dp->base.base.dev;
1093 struct drm_i915_private *dev_priv = dev->dev_private;
1094 uint32_t signal_levels = 0;
1095 u8 train_set = intel_dp->train_set[0];
1096 u32 vswing = train_set & DP_TRAIN_VOLTAGE_SWING_MASK;
1097 u32 preemphasis = train_set & DP_TRAIN_PRE_EMPHASIS_MASK;
a4fc5ed6 1098
869184a6
JB
1099 if (is_edp(intel_dp)) {
1100 vswing = dev_priv->edp.vswing;
1101 preemphasis = dev_priv->edp.preemphasis;
1102 }
1103
1104 switch (vswing) {
a4fc5ed6
KP
1105 case DP_TRAIN_VOLTAGE_SWING_400:
1106 default:
1107 signal_levels |= DP_VOLTAGE_0_4;
1108 break;
1109 case DP_TRAIN_VOLTAGE_SWING_600:
1110 signal_levels |= DP_VOLTAGE_0_6;
1111 break;
1112 case DP_TRAIN_VOLTAGE_SWING_800:
1113 signal_levels |= DP_VOLTAGE_0_8;
1114 break;
1115 case DP_TRAIN_VOLTAGE_SWING_1200:
1116 signal_levels |= DP_VOLTAGE_1_2;
1117 break;
1118 }
869184a6 1119 switch (preemphasis) {
a4fc5ed6
KP
1120 case DP_TRAIN_PRE_EMPHASIS_0:
1121 default:
1122 signal_levels |= DP_PRE_EMPHASIS_0;
1123 break;
1124 case DP_TRAIN_PRE_EMPHASIS_3_5:
1125 signal_levels |= DP_PRE_EMPHASIS_3_5;
1126 break;
1127 case DP_TRAIN_PRE_EMPHASIS_6:
1128 signal_levels |= DP_PRE_EMPHASIS_6;
1129 break;
1130 case DP_TRAIN_PRE_EMPHASIS_9_5:
1131 signal_levels |= DP_PRE_EMPHASIS_9_5;
1132 break;
1133 }
1134 return signal_levels;
1135}
1136
e3421a18
ZW
1137/* Gen6's DP voltage swing and pre-emphasis control */
1138static uint32_t
1139intel_gen6_edp_signal_levels(uint8_t train_set)
1140{
1141 switch (train_set & (DP_TRAIN_VOLTAGE_SWING_MASK|DP_TRAIN_PRE_EMPHASIS_MASK)) {
1142 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1143 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1144 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1145 return EDP_LINK_TRAIN_400MV_6DB_SNB_B;
1146 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1147 return EDP_LINK_TRAIN_600MV_3_5DB_SNB_B;
1148 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1149 return EDP_LINK_TRAIN_800MV_0DB_SNB_B;
1150 default:
1151 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level\n");
1152 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1153 }
1154}
1155
a4fc5ed6
KP
1156static uint8_t
1157intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1158 int lane)
1159{
1160 int i = DP_LANE0_1_STATUS + (lane >> 1);
1161 int s = (lane & 1) * 4;
1162 uint8_t l = intel_dp_link_status(link_status, i);
1163
1164 return (l >> s) & 0xf;
1165}
1166
1167/* Check for clock recovery is done on all channels */
1168static bool
1169intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1170{
1171 int lane;
1172 uint8_t lane_status;
1173
1174 for (lane = 0; lane < lane_count; lane++) {
1175 lane_status = intel_get_lane_status(link_status, lane);
1176 if ((lane_status & DP_LANE_CR_DONE) == 0)
1177 return false;
1178 }
1179 return true;
1180}
1181
1182/* Check to see if channel eq is done on all channels */
1183#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1184 DP_LANE_CHANNEL_EQ_DONE|\
1185 DP_LANE_SYMBOL_LOCKED)
1186static bool
33a34e4e 1187intel_channel_eq_ok(struct intel_dp *intel_dp)
a4fc5ed6
KP
1188{
1189 uint8_t lane_align;
1190 uint8_t lane_status;
1191 int lane;
1192
33a34e4e 1193 lane_align = intel_dp_link_status(intel_dp->link_status,
a4fc5ed6
KP
1194 DP_LANE_ALIGN_STATUS_UPDATED);
1195 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1196 return false;
33a34e4e
JB
1197 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1198 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
a4fc5ed6
KP
1199 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1200 return false;
1201 }
1202 return true;
1203}
1204
869184a6
JB
1205static bool
1206intel_dp_aux_handshake_required(struct intel_dp *intel_dp)
1207{
1208 struct drm_device *dev = intel_dp->base.base.dev;
1209 struct drm_i915_private *dev_priv = dev->dev_private;
1210
1211 if (is_edp(intel_dp) && dev_priv->no_aux_handshake)
1212 return false;
1213
1214 return true;
1215}
1216
a4fc5ed6 1217static bool
ea5b213a 1218intel_dp_set_link_train(struct intel_dp *intel_dp,
a4fc5ed6 1219 uint32_t dp_reg_value,
58e10eb9 1220 uint8_t dp_train_pat)
a4fc5ed6 1221{
4ef69c7a 1222 struct drm_device *dev = intel_dp->base.base.dev;
a4fc5ed6 1223 struct drm_i915_private *dev_priv = dev->dev_private;
a4fc5ed6
KP
1224 int ret;
1225
ea5b213a
CW
1226 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1227 POSTING_READ(intel_dp->output_reg);
a4fc5ed6 1228
869184a6
JB
1229 if (!intel_dp_aux_handshake_required(intel_dp))
1230 return true;
1231
ea5b213a 1232 intel_dp_aux_native_write_1(intel_dp,
a4fc5ed6
KP
1233 DP_TRAINING_PATTERN_SET,
1234 dp_train_pat);
1235
ea5b213a 1236 ret = intel_dp_aux_native_write(intel_dp,
58e10eb9
CW
1237 DP_TRAINING_LANE0_SET,
1238 intel_dp->train_set, 4);
a4fc5ed6
KP
1239 if (ret != 4)
1240 return false;
1241
1242 return true;
1243}
1244
33a34e4e 1245/* Enable corresponding port and start training pattern 1 */
a4fc5ed6 1246static void
33a34e4e 1247intel_dp_start_link_train(struct intel_dp *intel_dp)
a4fc5ed6 1248{
4ef69c7a 1249 struct drm_device *dev = intel_dp->base.base.dev;
a4fc5ed6 1250 struct drm_i915_private *dev_priv = dev->dev_private;
58e10eb9 1251 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
a4fc5ed6
KP
1252 int i;
1253 uint8_t voltage;
1254 bool clock_recovery = false;
a4fc5ed6 1255 int tries;
e3421a18 1256 u32 reg;
ea5b213a 1257 uint32_t DP = intel_dp->DP;
a4fc5ed6 1258
b99a9d9b
KP
1259 /* Enable output, wait for it to become active */
1260 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1261 POSTING_READ(intel_dp->output_reg);
1262 intel_wait_for_vblank(dev, intel_crtc->pipe);
a4fc5ed6 1263
869184a6
JB
1264 if (intel_dp_aux_handshake_required(intel_dp))
1265 /* Write the link configuration data */
1266 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1267 intel_dp->link_configuration,
1268 DP_LINK_CONFIGURATION_SIZE);
a4fc5ed6
KP
1269
1270 DP |= DP_PORT_EN;
cfcb0fc9 1271 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
e3421a18
ZW
1272 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1273 else
1274 DP &= ~DP_LINK_TRAIN_MASK;
33a34e4e 1275 memset(intel_dp->train_set, 0, 4);
a4fc5ed6
KP
1276 voltage = 0xff;
1277 tries = 0;
1278 clock_recovery = false;
1279 for (;;) {
33a34e4e 1280 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
e3421a18 1281 uint32_t signal_levels;
cfcb0fc9 1282 if (IS_GEN6(dev) && is_edp(intel_dp)) {
33a34e4e 1283 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
e3421a18
ZW
1284 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1285 } else {
869184a6 1286 signal_levels = intel_dp_signal_levels(intel_dp);
e3421a18
ZW
1287 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1288 }
a4fc5ed6 1289
cfcb0fc9 1290 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
e3421a18
ZW
1291 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1292 else
1293 reg = DP | DP_LINK_TRAIN_PAT_1;
1294
ea5b213a 1295 if (!intel_dp_set_link_train(intel_dp, reg,
58e10eb9 1296 DP_TRAINING_PATTERN_1))
a4fc5ed6 1297 break;
a4fc5ed6
KP
1298 /* Set training pattern 1 */
1299
869184a6
JB
1300 udelay(500);
1301 if (intel_dp_aux_handshake_required(intel_dp)) {
a4fc5ed6 1302 break;
869184a6
JB
1303 } else {
1304 if (!intel_dp_get_link_status(intel_dp))
1305 break;
a4fc5ed6 1306
869184a6
JB
1307 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1308 clock_recovery = true;
a4fc5ed6 1309 break;
869184a6 1310 }
a4fc5ed6 1311
869184a6
JB
1312 /* Check to see if we've tried the max voltage */
1313 for (i = 0; i < intel_dp->lane_count; i++)
1314 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1315 break;
1316 if (i == intel_dp->lane_count)
a4fc5ed6 1317 break;
a4fc5ed6 1318
869184a6
JB
1319 /* Check to see if we've tried the same voltage 5 times */
1320 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1321 ++tries;
1322 if (tries == 5)
1323 break;
1324 } else
1325 tries = 0;
1326 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1327
1328 /* Compute new intel_dp->train_set as requested by target */
1329 intel_get_adjust_train(intel_dp);
1330 }
a4fc5ed6
KP
1331 }
1332
33a34e4e
JB
1333 intel_dp->DP = DP;
1334}
1335
1336static void
1337intel_dp_complete_link_train(struct intel_dp *intel_dp)
1338{
4ef69c7a 1339 struct drm_device *dev = intel_dp->base.base.dev;
33a34e4e
JB
1340 struct drm_i915_private *dev_priv = dev->dev_private;
1341 bool channel_eq = false;
1342 int tries;
1343 u32 reg;
1344 uint32_t DP = intel_dp->DP;
1345
a4fc5ed6
KP
1346 /* channel equalization */
1347 tries = 0;
1348 channel_eq = false;
1349 for (;;) {
33a34e4e 1350 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
e3421a18
ZW
1351 uint32_t signal_levels;
1352
cfcb0fc9 1353 if (IS_GEN6(dev) && is_edp(intel_dp)) {
33a34e4e 1354 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
e3421a18
ZW
1355 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1356 } else {
869184a6 1357 signal_levels = intel_dp_signal_levels(intel_dp);
e3421a18
ZW
1358 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1359 }
1360
cfcb0fc9 1361 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
e3421a18
ZW
1362 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1363 else
1364 reg = DP | DP_LINK_TRAIN_PAT_2;
a4fc5ed6
KP
1365
1366 /* channel eq pattern */
ea5b213a 1367 if (!intel_dp_set_link_train(intel_dp, reg,
58e10eb9 1368 DP_TRAINING_PATTERN_2))
a4fc5ed6
KP
1369 break;
1370
869184a6 1371 udelay(500);
a4fc5ed6 1372
869184a6 1373 if (!intel_dp_aux_handshake_required(intel_dp)) {
a4fc5ed6 1374 break;
869184a6
JB
1375 } else {
1376 if (!intel_dp_get_link_status(intel_dp))
1377 break;
a4fc5ed6 1378
869184a6
JB
1379 if (intel_channel_eq_ok(intel_dp)) {
1380 channel_eq = true;
1381 break;
1382 }
a4fc5ed6 1383
869184a6
JB
1384 /* Try 5 times */
1385 if (tries > 5)
1386 break;
a4fc5ed6 1387
869184a6
JB
1388 /* Compute new intel_dp->train_set as requested by target */
1389 intel_get_adjust_train(intel_dp);
1390 ++tries;
1391 }
1392 }
cfcb0fc9 1393 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
e3421a18
ZW
1394 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1395 else
1396 reg = DP | DP_LINK_TRAIN_OFF;
1397
ea5b213a
CW
1398 I915_WRITE(intel_dp->output_reg, reg);
1399 POSTING_READ(intel_dp->output_reg);
1400 intel_dp_aux_native_write_1(intel_dp,
a4fc5ed6
KP
1401 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1402}
1403
1404static void
ea5b213a 1405intel_dp_link_down(struct intel_dp *intel_dp)
a4fc5ed6 1406{
4ef69c7a 1407 struct drm_device *dev = intel_dp->base.base.dev;
a4fc5ed6 1408 struct drm_i915_private *dev_priv = dev->dev_private;
ea5b213a 1409 uint32_t DP = intel_dp->DP;
a4fc5ed6 1410
28c97730 1411 DRM_DEBUG_KMS("\n");
32f9d658 1412
cfcb0fc9 1413 if (is_edp(intel_dp)) {
32f9d658 1414 DP &= ~DP_PLL_ENABLE;
ea5b213a
CW
1415 I915_WRITE(intel_dp->output_reg, DP);
1416 POSTING_READ(intel_dp->output_reg);
32f9d658
ZW
1417 udelay(100);
1418 }
1419
cfcb0fc9 1420 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp)) {
e3421a18 1421 DP &= ~DP_LINK_TRAIN_MASK_CPT;
ea5b213a 1422 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
e3421a18
ZW
1423 } else {
1424 DP &= ~DP_LINK_TRAIN_MASK;
ea5b213a 1425 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
e3421a18 1426 }
fe255d00 1427 POSTING_READ(intel_dp->output_reg);
5eb08b69 1428
fe255d00 1429 msleep(17);
5eb08b69 1430
cfcb0fc9 1431 if (is_edp(intel_dp))
32f9d658 1432 DP |= DP_LINK_TRAIN_OFF;
ea5b213a
CW
1433 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1434 POSTING_READ(intel_dp->output_reg);
a4fc5ed6
KP
1435}
1436
a4fc5ed6
KP
1437/*
1438 * According to DP spec
1439 * 5.1.2:
1440 * 1. Read DPCD
1441 * 2. Configure link according to Receiver Capabilities
1442 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1443 * 4. Check link status on receipt of hot-plug interrupt
1444 */
1445
1446static void
ea5b213a 1447intel_dp_check_link_status(struct intel_dp *intel_dp)
a4fc5ed6 1448{
4ef69c7a 1449 if (!intel_dp->base.base.crtc)
a4fc5ed6
KP
1450 return;
1451
33a34e4e 1452 if (!intel_dp_get_link_status(intel_dp)) {
ea5b213a 1453 intel_dp_link_down(intel_dp);
a4fc5ed6
KP
1454 return;
1455 }
1456
33a34e4e
JB
1457 if (!intel_channel_eq_ok(intel_dp)) {
1458 intel_dp_start_link_train(intel_dp);
1459 intel_dp_complete_link_train(intel_dp);
1460 }
a4fc5ed6 1461}
a4fc5ed6 1462
5eb08b69 1463static enum drm_connector_status
a9756bb5 1464ironlake_dp_detect(struct intel_dp *intel_dp)
5eb08b69 1465{
5eb08b69
ZW
1466 enum drm_connector_status status;
1467
01cb9ea6 1468 /* Can't disconnect eDP */
4d926461 1469 if (is_edp(intel_dp))
01cb9ea6
JB
1470 return connector_status_connected;
1471
5eb08b69 1472 status = connector_status_disconnected;
ea5b213a
CW
1473 if (intel_dp_aux_native_read(intel_dp,
1474 0x000, intel_dp->dpcd,
a9756bb5
ZW
1475 sizeof (intel_dp->dpcd))
1476 == sizeof(intel_dp->dpcd)) {
ea5b213a 1477 if (intel_dp->dpcd[0] != 0)
5eb08b69
ZW
1478 status = connector_status_connected;
1479 }
ea5b213a
CW
1480 DRM_DEBUG_KMS("DPCD: %hx%hx%hx%hx\n", intel_dp->dpcd[0],
1481 intel_dp->dpcd[1], intel_dp->dpcd[2], intel_dp->dpcd[3]);
5eb08b69
ZW
1482 return status;
1483}
1484
a4fc5ed6 1485static enum drm_connector_status
a9756bb5 1486g4x_dp_detect(struct intel_dp *intel_dp)
a4fc5ed6 1487{
4ef69c7a 1488 struct drm_device *dev = intel_dp->base.base.dev;
a4fc5ed6 1489 struct drm_i915_private *dev_priv = dev->dev_private;
a4fc5ed6 1490 enum drm_connector_status status;
a9756bb5 1491 uint32_t temp, bit;
5eb08b69 1492
ea5b213a 1493 switch (intel_dp->output_reg) {
a4fc5ed6
KP
1494 case DP_B:
1495 bit = DPB_HOTPLUG_INT_STATUS;
1496 break;
1497 case DP_C:
1498 bit = DPC_HOTPLUG_INT_STATUS;
1499 break;
1500 case DP_D:
1501 bit = DPD_HOTPLUG_INT_STATUS;
1502 break;
1503 default:
1504 return connector_status_unknown;
1505 }
1506
1507 temp = I915_READ(PORT_HOTPLUG_STAT);
1508
1509 if ((temp & bit) == 0)
1510 return connector_status_disconnected;
1511
1512 status = connector_status_disconnected;
a9756bb5 1513 if (intel_dp_aux_native_read(intel_dp, 0x000, intel_dp->dpcd,
ea5b213a 1514 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
a4fc5ed6 1515 {
ea5b213a 1516 if (intel_dp->dpcd[0] != 0)
a4fc5ed6
KP
1517 status = connector_status_connected;
1518 }
a9756bb5 1519
dd2b379f 1520 return status;
a9756bb5
ZW
1521}
1522
1523/**
1524 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1525 *
1526 * \return true if DP port is connected.
1527 * \return false if DP port is disconnected.
1528 */
1529static enum drm_connector_status
1530intel_dp_detect(struct drm_connector *connector, bool force)
1531{
1532 struct intel_dp *intel_dp = intel_attached_dp(connector);
1533 struct drm_device *dev = intel_dp->base.base.dev;
1534 enum drm_connector_status status;
1535 struct edid *edid = NULL;
1536
1537 intel_dp->has_audio = false;
1538
1539 if (HAS_PCH_SPLIT(dev))
1540 status = ironlake_dp_detect(intel_dp);
1541 else
1542 status = g4x_dp_detect(intel_dp);
1543 if (status != connector_status_connected)
1544 return status;
1545
f684960e
CW
1546 if (intel_dp->force_audio) {
1547 intel_dp->has_audio = intel_dp->force_audio > 0;
1548 } else {
1549 edid = drm_get_edid(connector, &intel_dp->adapter);
1550 if (edid) {
1551 intel_dp->has_audio = drm_detect_monitor_audio(edid);
1552 connector->display_info.raw_edid = NULL;
1553 kfree(edid);
1554 }
a9756bb5
ZW
1555 }
1556
1557 return connector_status_connected;
a4fc5ed6
KP
1558}
1559
1560static int intel_dp_get_modes(struct drm_connector *connector)
1561{
df0e9248 1562 struct intel_dp *intel_dp = intel_attached_dp(connector);
4ef69c7a 1563 struct drm_device *dev = intel_dp->base.base.dev;
32f9d658
ZW
1564 struct drm_i915_private *dev_priv = dev->dev_private;
1565 int ret;
a4fc5ed6
KP
1566
1567 /* We should parse the EDID data and find out if it has an audio sink
1568 */
1569
f899fc64 1570 ret = intel_ddc_get_modes(connector, &intel_dp->adapter);
b9efc480 1571 if (ret) {
4d926461 1572 if (is_edp(intel_dp) && !dev_priv->panel_fixed_mode) {
b9efc480
ZY
1573 struct drm_display_mode *newmode;
1574 list_for_each_entry(newmode, &connector->probed_modes,
1575 head) {
1576 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1577 dev_priv->panel_fixed_mode =
1578 drm_mode_duplicate(dev, newmode);
1579 break;
1580 }
1581 }
1582 }
1583
32f9d658 1584 return ret;
b9efc480 1585 }
32f9d658
ZW
1586
1587 /* if eDP has no EDID, try to use fixed panel mode from VBT */
4d926461 1588 if (is_edp(intel_dp)) {
32f9d658
ZW
1589 if (dev_priv->panel_fixed_mode != NULL) {
1590 struct drm_display_mode *mode;
1591 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1592 drm_mode_probed_add(connector, mode);
1593 return 1;
1594 }
1595 }
1596 return 0;
a4fc5ed6
KP
1597}
1598
f684960e
CW
1599static int
1600intel_dp_set_property(struct drm_connector *connector,
1601 struct drm_property *property,
1602 uint64_t val)
1603{
1604 struct intel_dp *intel_dp = intel_attached_dp(connector);
1605 int ret;
1606
1607 ret = drm_connector_property_set_value(connector, property, val);
1608 if (ret)
1609 return ret;
1610
1611 if (property == intel_dp->force_audio_property) {
1612 if (val == intel_dp->force_audio)
1613 return 0;
1614
1615 intel_dp->force_audio = val;
1616
1617 if (val > 0 && intel_dp->has_audio)
1618 return 0;
1619 if (val < 0 && !intel_dp->has_audio)
1620 return 0;
1621
1622 intel_dp->has_audio = val > 0;
1623 goto done;
1624 }
1625
1626 return -EINVAL;
1627
1628done:
1629 if (intel_dp->base.base.crtc) {
1630 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1631 drm_crtc_helper_set_mode(crtc, &crtc->mode,
1632 crtc->x, crtc->y,
1633 crtc->fb);
1634 }
1635
1636 return 0;
1637}
1638
a4fc5ed6
KP
1639static void
1640intel_dp_destroy (struct drm_connector *connector)
1641{
a4fc5ed6
KP
1642 drm_sysfs_connector_remove(connector);
1643 drm_connector_cleanup(connector);
55f78c43 1644 kfree(connector);
a4fc5ed6
KP
1645}
1646
24d05927
DV
1647static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1648{
1649 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1650
1651 i2c_del_adapter(&intel_dp->adapter);
1652 drm_encoder_cleanup(encoder);
1653 kfree(intel_dp);
1654}
1655
a4fc5ed6
KP
1656static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1657 .dpms = intel_dp_dpms,
1658 .mode_fixup = intel_dp_mode_fixup,
d240f20f 1659 .prepare = intel_dp_prepare,
a4fc5ed6 1660 .mode_set = intel_dp_mode_set,
d240f20f 1661 .commit = intel_dp_commit,
a4fc5ed6
KP
1662};
1663
1664static const struct drm_connector_funcs intel_dp_connector_funcs = {
1665 .dpms = drm_helper_connector_dpms,
a4fc5ed6
KP
1666 .detect = intel_dp_detect,
1667 .fill_modes = drm_helper_probe_single_connector_modes,
f684960e 1668 .set_property = intel_dp_set_property,
a4fc5ed6
KP
1669 .destroy = intel_dp_destroy,
1670};
1671
1672static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1673 .get_modes = intel_dp_get_modes,
1674 .mode_valid = intel_dp_mode_valid,
df0e9248 1675 .best_encoder = intel_best_encoder,
a4fc5ed6
KP
1676};
1677
a4fc5ed6 1678static const struct drm_encoder_funcs intel_dp_enc_funcs = {
24d05927 1679 .destroy = intel_dp_encoder_destroy,
a4fc5ed6
KP
1680};
1681
995b6762 1682static void
21d40d37 1683intel_dp_hot_plug(struct intel_encoder *intel_encoder)
c8110e52 1684{
ea5b213a 1685 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
c8110e52 1686
ea5b213a
CW
1687 if (intel_dp->dpms_mode == DRM_MODE_DPMS_ON)
1688 intel_dp_check_link_status(intel_dp);
c8110e52 1689}
6207937d 1690
e3421a18
ZW
1691/* Return which DP Port should be selected for Transcoder DP control */
1692int
1693intel_trans_dp_port_sel (struct drm_crtc *crtc)
1694{
1695 struct drm_device *dev = crtc->dev;
1696 struct drm_mode_config *mode_config = &dev->mode_config;
1697 struct drm_encoder *encoder;
e3421a18
ZW
1698
1699 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
ea5b213a
CW
1700 struct intel_dp *intel_dp;
1701
d8201ab6 1702 if (encoder->crtc != crtc)
e3421a18
ZW
1703 continue;
1704
ea5b213a
CW
1705 intel_dp = enc_to_intel_dp(encoder);
1706 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
1707 return intel_dp->output_reg;
e3421a18 1708 }
ea5b213a 1709
e3421a18
ZW
1710 return -1;
1711}
1712
36e83a18 1713/* check the VBT to see whether the eDP is on DP-D port */
cb0953d7 1714bool intel_dpd_is_edp(struct drm_device *dev)
36e83a18
ZY
1715{
1716 struct drm_i915_private *dev_priv = dev->dev_private;
1717 struct child_device_config *p_child;
1718 int i;
1719
1720 if (!dev_priv->child_dev_num)
1721 return false;
1722
1723 for (i = 0; i < dev_priv->child_dev_num; i++) {
1724 p_child = dev_priv->child_dev + i;
1725
1726 if (p_child->dvo_port == PORT_IDPD &&
1727 p_child->device_type == DEVICE_TYPE_eDP)
1728 return true;
1729 }
1730 return false;
1731}
1732
f684960e
CW
1733static void
1734intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
1735{
1736 struct drm_device *dev = connector->dev;
1737
1738 intel_dp->force_audio_property =
1739 drm_property_create(dev, DRM_MODE_PROP_RANGE, "force_audio", 2);
1740 if (intel_dp->force_audio_property) {
1741 intel_dp->force_audio_property->values[0] = -1;
1742 intel_dp->force_audio_property->values[1] = 1;
1743 drm_connector_attach_property(connector, intel_dp->force_audio_property, 0);
1744 }
1745}
1746
a4fc5ed6
KP
1747void
1748intel_dp_init(struct drm_device *dev, int output_reg)
1749{
1750 struct drm_i915_private *dev_priv = dev->dev_private;
1751 struct drm_connector *connector;
ea5b213a 1752 struct intel_dp *intel_dp;
21d40d37 1753 struct intel_encoder *intel_encoder;
55f78c43 1754 struct intel_connector *intel_connector;
5eb08b69 1755 const char *name = NULL;
b329530c 1756 int type;
a4fc5ed6 1757
ea5b213a
CW
1758 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
1759 if (!intel_dp)
a4fc5ed6
KP
1760 return;
1761
55f78c43
ZW
1762 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1763 if (!intel_connector) {
ea5b213a 1764 kfree(intel_dp);
55f78c43
ZW
1765 return;
1766 }
ea5b213a 1767 intel_encoder = &intel_dp->base;
55f78c43 1768
ea5b213a 1769 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
b329530c 1770 if (intel_dpd_is_edp(dev))
ea5b213a 1771 intel_dp->is_pch_edp = true;
b329530c 1772
cfcb0fc9 1773 if (output_reg == DP_A || is_pch_edp(intel_dp)) {
b329530c
AJ
1774 type = DRM_MODE_CONNECTOR_eDP;
1775 intel_encoder->type = INTEL_OUTPUT_EDP;
1776 } else {
1777 type = DRM_MODE_CONNECTOR_DisplayPort;
1778 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1779 }
1780
55f78c43 1781 connector = &intel_connector->base;
b329530c 1782 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
a4fc5ed6
KP
1783 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1784
eb1f8e4f
DA
1785 connector->polled = DRM_CONNECTOR_POLL_HPD;
1786
652af9d7 1787 if (output_reg == DP_B || output_reg == PCH_DP_B)
21d40d37 1788 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
652af9d7 1789 else if (output_reg == DP_C || output_reg == PCH_DP_C)
21d40d37 1790 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
652af9d7 1791 else if (output_reg == DP_D || output_reg == PCH_DP_D)
21d40d37 1792 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
f8aed700 1793
cfcb0fc9 1794 if (is_edp(intel_dp))
21d40d37 1795 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
6251ec0a 1796
21d40d37 1797 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
a4fc5ed6
KP
1798 connector->interlace_allowed = true;
1799 connector->doublescan_allowed = 0;
1800
ea5b213a
CW
1801 intel_dp->output_reg = output_reg;
1802 intel_dp->has_audio = false;
1803 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
a4fc5ed6 1804
4ef69c7a 1805 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
a4fc5ed6 1806 DRM_MODE_ENCODER_TMDS);
4ef69c7a 1807 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
a4fc5ed6 1808
df0e9248 1809 intel_connector_attach_encoder(intel_connector, intel_encoder);
a4fc5ed6
KP
1810 drm_sysfs_connector_add(connector);
1811
1812 /* Set up the DDC bus. */
5eb08b69 1813 switch (output_reg) {
32f9d658
ZW
1814 case DP_A:
1815 name = "DPDDC-A";
1816 break;
5eb08b69
ZW
1817 case DP_B:
1818 case PCH_DP_B:
b01f2c3a
JB
1819 dev_priv->hotplug_supported_mask |=
1820 HDMIB_HOTPLUG_INT_STATUS;
5eb08b69
ZW
1821 name = "DPDDC-B";
1822 break;
1823 case DP_C:
1824 case PCH_DP_C:
b01f2c3a
JB
1825 dev_priv->hotplug_supported_mask |=
1826 HDMIC_HOTPLUG_INT_STATUS;
5eb08b69
ZW
1827 name = "DPDDC-C";
1828 break;
1829 case DP_D:
1830 case PCH_DP_D:
b01f2c3a
JB
1831 dev_priv->hotplug_supported_mask |=
1832 HDMID_HOTPLUG_INT_STATUS;
5eb08b69
ZW
1833 name = "DPDDC-D";
1834 break;
1835 }
1836
ea5b213a 1837 intel_dp_i2c_init(intel_dp, intel_connector, name);
32f9d658 1838
89667383
JB
1839 /* Cache some DPCD data in the eDP case */
1840 if (is_edp(intel_dp)) {
1841 int ret;
1842 bool was_on;
1843
1844 was_on = ironlake_edp_panel_on(intel_dp);
1845 ret = intel_dp_aux_native_read(intel_dp, DP_DPCD_REV,
1846 intel_dp->dpcd,
1847 sizeof(intel_dp->dpcd));
1848 if (ret == sizeof(intel_dp->dpcd)) {
1849 if (intel_dp->dpcd[0] >= 0x11)
1850 dev_priv->no_aux_handshake = intel_dp->dpcd[3] &
1851 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
1852 } else {
1853 DRM_ERROR("failed to retrieve link info\n");
1854 }
1855 if (!was_on)
1856 ironlake_edp_panel_off(dev);
1857 }
1858
21d40d37 1859 intel_encoder->hot_plug = intel_dp_hot_plug;
a4fc5ed6 1860
4d926461 1861 if (is_edp(intel_dp)) {
32f9d658
ZW
1862 /* initialize panel mode from VBT if available for eDP */
1863 if (dev_priv->lfp_lvds_vbt_mode) {
1864 dev_priv->panel_fixed_mode =
1865 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1866 if (dev_priv->panel_fixed_mode) {
1867 dev_priv->panel_fixed_mode->type |=
1868 DRM_MODE_TYPE_PREFERRED;
1869 }
1870 }
1871 }
1872
f684960e
CW
1873 intel_dp_add_properties(intel_dp, connector);
1874
a4fc5ed6
KP
1875 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1876 * 0xd. Failure to do so will result in spurious interrupts being
1877 * generated on the port when a cable is not attached.
1878 */
1879 if (IS_G4X(dev) && !IS_GM45(dev)) {
1880 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
1881 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
1882 }
1883}