Merge branches 'x86-fixes-for-linus', 'perf-fixes-for-linus' and 'sched-fixes-for...
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_dp.c
CommitLineData
a4fc5ed6
KP
1/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Keith Packard <keithp@keithp.com>
25 *
26 */
27
28#include <linux/i2c.h>
5a0e3ad6 29#include <linux/slab.h>
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
a4fc5ed6
KP
587 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
588 for (clock = 0; clock <= max_clock; clock++) {
fe27d53e 589 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
a4fc5ed6 590
ea5b213a 591 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
885a5fb5 592 <= link_avail) {
ea5b213a
CW
593 intel_dp->link_bw = bws[clock];
594 intel_dp->lane_count = lane_count;
595 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
28c97730
ZY
596 DRM_DEBUG_KMS("Display port link bw %02x lane "
597 "count %d clock %d\n",
ea5b213a 598 intel_dp->link_bw, intel_dp->lane_count,
a4fc5ed6
KP
599 adjusted_mode->clock);
600 return true;
601 }
602 }
603 }
fe27d53e 604
3cf2efb1
CW
605 if (is_edp(intel_dp)) {
606 /* okay we failed just pick the highest */
607 intel_dp->lane_count = max_lane_count;
608 intel_dp->link_bw = bws[max_clock];
609 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
610 DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
611 "count %d clock %d\n",
612 intel_dp->link_bw, intel_dp->lane_count,
613 adjusted_mode->clock);
614
615 return true;
616 }
617
a4fc5ed6
KP
618 return false;
619}
620
621struct intel_dp_m_n {
622 uint32_t tu;
623 uint32_t gmch_m;
624 uint32_t gmch_n;
625 uint32_t link_m;
626 uint32_t link_n;
627};
628
629static void
630intel_reduce_ratio(uint32_t *num, uint32_t *den)
631{
632 while (*num > 0xffffff || *den > 0xffffff) {
633 *num >>= 1;
634 *den >>= 1;
635 }
636}
637
638static void
36e83a18 639intel_dp_compute_m_n(int bpp,
a4fc5ed6
KP
640 int nlanes,
641 int pixel_clock,
642 int link_clock,
643 struct intel_dp_m_n *m_n)
644{
645 m_n->tu = 64;
36e83a18 646 m_n->gmch_m = (pixel_clock * bpp) >> 3;
a4fc5ed6
KP
647 m_n->gmch_n = link_clock * nlanes;
648 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
649 m_n->link_m = pixel_clock;
650 m_n->link_n = link_clock;
651 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
652}
653
654void
655intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
656 struct drm_display_mode *adjusted_mode)
657{
658 struct drm_device *dev = crtc->dev;
659 struct drm_mode_config *mode_config = &dev->mode_config;
55f78c43 660 struct drm_encoder *encoder;
a4fc5ed6
KP
661 struct drm_i915_private *dev_priv = dev->dev_private;
662 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
36e83a18 663 int lane_count = 4, bpp = 24;
a4fc5ed6
KP
664 struct intel_dp_m_n m_n;
665
666 /*
21d40d37 667 * Find the lane count in the intel_encoder private
a4fc5ed6 668 */
55f78c43 669 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
ea5b213a 670 struct intel_dp *intel_dp;
a4fc5ed6 671
d8201ab6 672 if (encoder->crtc != crtc)
a4fc5ed6
KP
673 continue;
674
ea5b213a
CW
675 intel_dp = enc_to_intel_dp(encoder);
676 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
677 lane_count = intel_dp->lane_count;
51190667
JB
678 break;
679 } else if (is_edp(intel_dp)) {
680 lane_count = dev_priv->edp.lanes;
681 bpp = dev_priv->edp.bpp;
a4fc5ed6
KP
682 break;
683 }
684 }
685
686 /*
687 * Compute the GMCH and Link ratios. The '3' here is
688 * the number of bytes_per_pixel post-LUT, which we always
689 * set up for 8-bits of R/G/B, or 3 bytes total.
690 */
36e83a18 691 intel_dp_compute_m_n(bpp, lane_count,
a4fc5ed6
KP
692 mode->clock, adjusted_mode->clock, &m_n);
693
c619eed4 694 if (HAS_PCH_SPLIT(dev)) {
5eb08b69
ZW
695 if (intel_crtc->pipe == 0) {
696 I915_WRITE(TRANSA_DATA_M1,
697 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
698 m_n.gmch_m);
699 I915_WRITE(TRANSA_DATA_N1, m_n.gmch_n);
700 I915_WRITE(TRANSA_DP_LINK_M1, m_n.link_m);
701 I915_WRITE(TRANSA_DP_LINK_N1, m_n.link_n);
702 } else {
703 I915_WRITE(TRANSB_DATA_M1,
704 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
705 m_n.gmch_m);
706 I915_WRITE(TRANSB_DATA_N1, m_n.gmch_n);
707 I915_WRITE(TRANSB_DP_LINK_M1, m_n.link_m);
708 I915_WRITE(TRANSB_DP_LINK_N1, m_n.link_n);
709 }
a4fc5ed6 710 } else {
5eb08b69
ZW
711 if (intel_crtc->pipe == 0) {
712 I915_WRITE(PIPEA_GMCH_DATA_M,
713 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
714 m_n.gmch_m);
715 I915_WRITE(PIPEA_GMCH_DATA_N,
716 m_n.gmch_n);
717 I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
718 I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
719 } else {
720 I915_WRITE(PIPEB_GMCH_DATA_M,
721 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
722 m_n.gmch_m);
723 I915_WRITE(PIPEB_GMCH_DATA_N,
724 m_n.gmch_n);
725 I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
726 I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
727 }
a4fc5ed6
KP
728 }
729}
730
731static void
732intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
733 struct drm_display_mode *adjusted_mode)
734{
e3421a18 735 struct drm_device *dev = encoder->dev;
ea5b213a 736 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4ef69c7a 737 struct drm_crtc *crtc = intel_dp->base.base.crtc;
a4fc5ed6
KP
738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
739
ea5b213a 740 intel_dp->DP = (DP_VOLTAGE_0_4 |
9c9e7927
AJ
741 DP_PRE_EMPHASIS_0);
742
743 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
ea5b213a 744 intel_dp->DP |= DP_SYNC_HS_HIGH;
9c9e7927 745 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
ea5b213a 746 intel_dp->DP |= DP_SYNC_VS_HIGH;
a4fc5ed6 747
cfcb0fc9 748 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
ea5b213a 749 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
e3421a18 750 else
ea5b213a 751 intel_dp->DP |= DP_LINK_TRAIN_OFF;
a4fc5ed6 752
ea5b213a 753 switch (intel_dp->lane_count) {
a4fc5ed6 754 case 1:
ea5b213a 755 intel_dp->DP |= DP_PORT_WIDTH_1;
a4fc5ed6
KP
756 break;
757 case 2:
ea5b213a 758 intel_dp->DP |= DP_PORT_WIDTH_2;
a4fc5ed6
KP
759 break;
760 case 4:
ea5b213a 761 intel_dp->DP |= DP_PORT_WIDTH_4;
a4fc5ed6
KP
762 break;
763 }
ea5b213a
CW
764 if (intel_dp->has_audio)
765 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
a4fc5ed6 766
ea5b213a
CW
767 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
768 intel_dp->link_configuration[0] = intel_dp->link_bw;
769 intel_dp->link_configuration[1] = intel_dp->lane_count;
a4fc5ed6
KP
770
771 /*
9962c925 772 * Check for DPCD version > 1.1 and enhanced framing support
a4fc5ed6 773 */
ea5b213a
CW
774 if (intel_dp->dpcd[0] >= 0x11 && (intel_dp->dpcd[2] & DP_ENHANCED_FRAME_CAP)) {
775 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
776 intel_dp->DP |= DP_ENHANCED_FRAMING;
a4fc5ed6
KP
777 }
778
e3421a18
ZW
779 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
780 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
ea5b213a 781 intel_dp->DP |= DP_PIPEB_SELECT;
32f9d658 782
895692be 783 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
32f9d658 784 /* don't miss out required setting for eDP */
ea5b213a 785 intel_dp->DP |= DP_PLL_ENABLE;
32f9d658 786 if (adjusted_mode->clock < 200000)
ea5b213a 787 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
32f9d658 788 else
ea5b213a 789 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
32f9d658 790 }
a4fc5ed6
KP
791}
792
7eaf5547 793/* Returns true if the panel was already on when called */
01cb9ea6 794static bool ironlake_edp_panel_on (struct intel_dp *intel_dp)
9934c132 795{
01cb9ea6 796 struct drm_device *dev = intel_dp->base.base.dev;
9934c132 797 struct drm_i915_private *dev_priv = dev->dev_private;
01cb9ea6 798 u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_STATE_ON_IDLE;
9934c132 799
913d8d11 800 if (I915_READ(PCH_PP_STATUS) & PP_ON)
7eaf5547 801 return true;
9934c132
JB
802
803 pp = I915_READ(PCH_PP_CONTROL);
37c6c9b0
JB
804
805 /* ILK workaround: disable reset around power sequence */
806 pp &= ~PANEL_POWER_RESET;
807 I915_WRITE(PCH_PP_CONTROL, pp);
808 POSTING_READ(PCH_PP_CONTROL);
809
01cb9ea6 810 pp |= PANEL_UNLOCK_REGS | POWER_TARGET_ON;
9934c132 811 I915_WRITE(PCH_PP_CONTROL, pp);
01cb9ea6 812 POSTING_READ(PCH_PP_CONTROL);
9934c132 813
27d64339
HV
814 /* Ouch. We need to wait here for some panels, like Dell e6510
815 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
816 */
817 msleep(300);
818
01cb9ea6
JB
819 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_on_mask) == idle_on_mask,
820 5000))
913d8d11
CW
821 DRM_ERROR("panel on wait timed out: 0x%08x\n",
822 I915_READ(PCH_PP_STATUS));
9934c132 823
37c6c9b0 824 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
9934c132 825 I915_WRITE(PCH_PP_CONTROL, pp);
37c6c9b0 826 POSTING_READ(PCH_PP_CONTROL);
7eaf5547
JB
827
828 return false;
9934c132
JB
829}
830
831static void ironlake_edp_panel_off (struct drm_device *dev)
832{
833 struct drm_i915_private *dev_priv = dev->dev_private;
01cb9ea6
JB
834 u32 pp, idle_off_mask = PP_ON | PP_SEQUENCE_MASK |
835 PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK;
9934c132
JB
836
837 pp = I915_READ(PCH_PP_CONTROL);
37c6c9b0
JB
838
839 /* ILK workaround: disable reset around power sequence */
840 pp &= ~PANEL_POWER_RESET;
841 I915_WRITE(PCH_PP_CONTROL, pp);
842 POSTING_READ(PCH_PP_CONTROL);
843
9934c132
JB
844 pp &= ~POWER_TARGET_ON;
845 I915_WRITE(PCH_PP_CONTROL, pp);
01cb9ea6 846 POSTING_READ(PCH_PP_CONTROL);
9934c132 847
01cb9ea6 848 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_off_mask) == 0, 5000))
913d8d11
CW
849 DRM_ERROR("panel off wait timed out: 0x%08x\n",
850 I915_READ(PCH_PP_STATUS));
9934c132 851
3969c9c9 852 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
9934c132 853 I915_WRITE(PCH_PP_CONTROL, pp);
37c6c9b0 854 POSTING_READ(PCH_PP_CONTROL);
27d64339
HV
855
856 /* Ouch. We need to wait here for some panels, like Dell e6510
857 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
858 */
859 msleep(300);
9934c132
JB
860}
861
f2b115e6 862static void ironlake_edp_backlight_on (struct drm_device *dev)
32f9d658
ZW
863{
864 struct drm_i915_private *dev_priv = dev->dev_private;
865 u32 pp;
866
28c97730 867 DRM_DEBUG_KMS("\n");
01cb9ea6
JB
868 /*
869 * If we enable the backlight right away following a panel power
870 * on, we may see slight flicker as the panel syncs with the eDP
871 * link. So delay a bit to make sure the image is solid before
872 * allowing it to appear.
873 */
874 msleep(300);
32f9d658
ZW
875 pp = I915_READ(PCH_PP_CONTROL);
876 pp |= EDP_BLC_ENABLE;
877 I915_WRITE(PCH_PP_CONTROL, pp);
878}
879
f2b115e6 880static void ironlake_edp_backlight_off (struct drm_device *dev)
32f9d658
ZW
881{
882 struct drm_i915_private *dev_priv = dev->dev_private;
883 u32 pp;
884
28c97730 885 DRM_DEBUG_KMS("\n");
32f9d658
ZW
886 pp = I915_READ(PCH_PP_CONTROL);
887 pp &= ~EDP_BLC_ENABLE;
888 I915_WRITE(PCH_PP_CONTROL, pp);
889}
a4fc5ed6 890
d240f20f
JB
891static void ironlake_edp_pll_on(struct drm_encoder *encoder)
892{
893 struct drm_device *dev = encoder->dev;
894 struct drm_i915_private *dev_priv = dev->dev_private;
895 u32 dpa_ctl;
896
897 DRM_DEBUG_KMS("\n");
898 dpa_ctl = I915_READ(DP_A);
298b0b39 899 dpa_ctl |= DP_PLL_ENABLE;
d240f20f 900 I915_WRITE(DP_A, dpa_ctl);
298b0b39
JB
901 POSTING_READ(DP_A);
902 udelay(200);
d240f20f
JB
903}
904
905static void ironlake_edp_pll_off(struct drm_encoder *encoder)
906{
907 struct drm_device *dev = encoder->dev;
908 struct drm_i915_private *dev_priv = dev->dev_private;
909 u32 dpa_ctl;
910
911 dpa_ctl = I915_READ(DP_A);
298b0b39 912 dpa_ctl &= ~DP_PLL_ENABLE;
d240f20f 913 I915_WRITE(DP_A, dpa_ctl);
1af5fa1b 914 POSTING_READ(DP_A);
d240f20f
JB
915 udelay(200);
916}
917
918static void intel_dp_prepare(struct drm_encoder *encoder)
919{
920 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
921 struct drm_device *dev = encoder->dev;
d240f20f 922
4d926461 923 if (is_edp(intel_dp)) {
d240f20f 924 ironlake_edp_backlight_off(dev);
01cb9ea6
JB
925 ironlake_edp_panel_on(intel_dp);
926 if (!is_pch_edp(intel_dp))
927 ironlake_edp_pll_on(encoder);
928 else
929 ironlake_edp_pll_off(encoder);
d240f20f 930 }
736085bc 931 intel_dp_link_down(intel_dp);
d240f20f
JB
932}
933
934static void intel_dp_commit(struct drm_encoder *encoder)
935{
936 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
937 struct drm_device *dev = encoder->dev;
d240f20f 938
33a34e4e
JB
939 intel_dp_start_link_train(intel_dp);
940
4d926461 941 if (is_edp(intel_dp))
01cb9ea6 942 ironlake_edp_panel_on(intel_dp);
33a34e4e
JB
943
944 intel_dp_complete_link_train(intel_dp);
945
4d926461 946 if (is_edp(intel_dp))
d240f20f
JB
947 ironlake_edp_backlight_on(dev);
948}
949
a4fc5ed6
KP
950static void
951intel_dp_dpms(struct drm_encoder *encoder, int mode)
952{
ea5b213a 953 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
55f78c43 954 struct drm_device *dev = encoder->dev;
a4fc5ed6 955 struct drm_i915_private *dev_priv = dev->dev_private;
ea5b213a 956 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
a4fc5ed6
KP
957
958 if (mode != DRM_MODE_DPMS_ON) {
01cb9ea6 959 if (is_edp(intel_dp))
7643a7fa 960 ironlake_edp_backlight_off(dev);
736085bc 961 intel_dp_link_down(intel_dp);
4d926461 962 if (is_edp(intel_dp))
01cb9ea6
JB
963 ironlake_edp_panel_off(dev);
964 if (is_edp(intel_dp) && !is_pch_edp(intel_dp))
d240f20f 965 ironlake_edp_pll_off(encoder);
a4fc5ed6 966 } else {
736085bc
JB
967 if (is_edp(intel_dp))
968 ironlake_edp_panel_on(intel_dp);
32f9d658 969 if (!(dp_reg & DP_PORT_EN)) {
01cb9ea6 970 intel_dp_start_link_train(intel_dp);
33a34e4e 971 intel_dp_complete_link_train(intel_dp);
32f9d658 972 }
736085bc
JB
973 if (is_edp(intel_dp))
974 ironlake_edp_backlight_on(dev);
a4fc5ed6 975 }
ea5b213a 976 intel_dp->dpms_mode = mode;
a4fc5ed6
KP
977}
978
979/*
980 * Fetch AUX CH registers 0x202 - 0x207 which contain
981 * link status information
982 */
983static bool
33a34e4e 984intel_dp_get_link_status(struct intel_dp *intel_dp)
a4fc5ed6
KP
985{
986 int ret;
987
ea5b213a 988 ret = intel_dp_aux_native_read(intel_dp,
a4fc5ed6 989 DP_LANE0_1_STATUS,
33a34e4e 990 intel_dp->link_status, DP_LINK_STATUS_SIZE);
a4fc5ed6
KP
991 if (ret != DP_LINK_STATUS_SIZE)
992 return false;
993 return true;
994}
995
996static uint8_t
997intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
998 int r)
999{
1000 return link_status[r - DP_LANE0_1_STATUS];
1001}
1002
a4fc5ed6
KP
1003static uint8_t
1004intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
1005 int lane)
1006{
1007 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1008 int s = ((lane & 1) ?
1009 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1010 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1011 uint8_t l = intel_dp_link_status(link_status, i);
1012
1013 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1014}
1015
1016static uint8_t
1017intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1018 int lane)
1019{
1020 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1021 int s = ((lane & 1) ?
1022 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1023 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1024 uint8_t l = intel_dp_link_status(link_status, i);
1025
1026 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1027}
1028
1029
1030#if 0
1031static char *voltage_names[] = {
1032 "0.4V", "0.6V", "0.8V", "1.2V"
1033};
1034static char *pre_emph_names[] = {
1035 "0dB", "3.5dB", "6dB", "9.5dB"
1036};
1037static char *link_train_names[] = {
1038 "pattern 1", "pattern 2", "idle", "off"
1039};
1040#endif
1041
1042/*
1043 * These are source-specific values; current Intel hardware supports
1044 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1045 */
1046#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1047
1048static uint8_t
1049intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1050{
1051 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1052 case DP_TRAIN_VOLTAGE_SWING_400:
1053 return DP_TRAIN_PRE_EMPHASIS_6;
1054 case DP_TRAIN_VOLTAGE_SWING_600:
1055 return DP_TRAIN_PRE_EMPHASIS_6;
1056 case DP_TRAIN_VOLTAGE_SWING_800:
1057 return DP_TRAIN_PRE_EMPHASIS_3_5;
1058 case DP_TRAIN_VOLTAGE_SWING_1200:
1059 default:
1060 return DP_TRAIN_PRE_EMPHASIS_0;
1061 }
1062}
1063
1064static void
33a34e4e 1065intel_get_adjust_train(struct intel_dp *intel_dp)
a4fc5ed6
KP
1066{
1067 uint8_t v = 0;
1068 uint8_t p = 0;
1069 int lane;
1070
33a34e4e
JB
1071 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1072 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1073 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
a4fc5ed6
KP
1074
1075 if (this_v > v)
1076 v = this_v;
1077 if (this_p > p)
1078 p = this_p;
1079 }
1080
1081 if (v >= I830_DP_VOLTAGE_MAX)
1082 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1083
1084 if (p >= intel_dp_pre_emphasis_max(v))
1085 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1086
1087 for (lane = 0; lane < 4; lane++)
33a34e4e 1088 intel_dp->train_set[lane] = v | p;
a4fc5ed6
KP
1089}
1090
1091static uint32_t
3cf2efb1 1092intel_dp_signal_levels(uint8_t train_set, int lane_count)
a4fc5ed6 1093{
3cf2efb1 1094 uint32_t signal_levels = 0;
a4fc5ed6 1095
3cf2efb1 1096 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
a4fc5ed6
KP
1097 case DP_TRAIN_VOLTAGE_SWING_400:
1098 default:
1099 signal_levels |= DP_VOLTAGE_0_4;
1100 break;
1101 case DP_TRAIN_VOLTAGE_SWING_600:
1102 signal_levels |= DP_VOLTAGE_0_6;
1103 break;
1104 case DP_TRAIN_VOLTAGE_SWING_800:
1105 signal_levels |= DP_VOLTAGE_0_8;
1106 break;
1107 case DP_TRAIN_VOLTAGE_SWING_1200:
1108 signal_levels |= DP_VOLTAGE_1_2;
1109 break;
1110 }
3cf2efb1 1111 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
a4fc5ed6
KP
1112 case DP_TRAIN_PRE_EMPHASIS_0:
1113 default:
1114 signal_levels |= DP_PRE_EMPHASIS_0;
1115 break;
1116 case DP_TRAIN_PRE_EMPHASIS_3_5:
1117 signal_levels |= DP_PRE_EMPHASIS_3_5;
1118 break;
1119 case DP_TRAIN_PRE_EMPHASIS_6:
1120 signal_levels |= DP_PRE_EMPHASIS_6;
1121 break;
1122 case DP_TRAIN_PRE_EMPHASIS_9_5:
1123 signal_levels |= DP_PRE_EMPHASIS_9_5;
1124 break;
1125 }
1126 return signal_levels;
1127}
1128
e3421a18
ZW
1129/* Gen6's DP voltage swing and pre-emphasis control */
1130static uint32_t
1131intel_gen6_edp_signal_levels(uint8_t train_set)
1132{
1133 switch (train_set & (DP_TRAIN_VOLTAGE_SWING_MASK|DP_TRAIN_PRE_EMPHASIS_MASK)) {
1134 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1135 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1136 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1137 return EDP_LINK_TRAIN_400MV_6DB_SNB_B;
1138 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1139 return EDP_LINK_TRAIN_600MV_3_5DB_SNB_B;
1140 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1141 return EDP_LINK_TRAIN_800MV_0DB_SNB_B;
1142 default:
1143 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level\n");
1144 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1145 }
1146}
1147
a4fc5ed6
KP
1148static uint8_t
1149intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1150 int lane)
1151{
1152 int i = DP_LANE0_1_STATUS + (lane >> 1);
1153 int s = (lane & 1) * 4;
1154 uint8_t l = intel_dp_link_status(link_status, i);
1155
1156 return (l >> s) & 0xf;
1157}
1158
1159/* Check for clock recovery is done on all channels */
1160static bool
1161intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1162{
1163 int lane;
1164 uint8_t lane_status;
1165
1166 for (lane = 0; lane < lane_count; lane++) {
1167 lane_status = intel_get_lane_status(link_status, lane);
1168 if ((lane_status & DP_LANE_CR_DONE) == 0)
1169 return false;
1170 }
1171 return true;
1172}
1173
1174/* Check to see if channel eq is done on all channels */
1175#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1176 DP_LANE_CHANNEL_EQ_DONE|\
1177 DP_LANE_SYMBOL_LOCKED)
1178static bool
33a34e4e 1179intel_channel_eq_ok(struct intel_dp *intel_dp)
a4fc5ed6
KP
1180{
1181 uint8_t lane_align;
1182 uint8_t lane_status;
1183 int lane;
1184
33a34e4e 1185 lane_align = intel_dp_link_status(intel_dp->link_status,
a4fc5ed6
KP
1186 DP_LANE_ALIGN_STATUS_UPDATED);
1187 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1188 return false;
33a34e4e
JB
1189 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1190 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
a4fc5ed6
KP
1191 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1192 return false;
1193 }
1194 return true;
1195}
1196
1197static bool
ea5b213a 1198intel_dp_set_link_train(struct intel_dp *intel_dp,
a4fc5ed6 1199 uint32_t dp_reg_value,
58e10eb9 1200 uint8_t dp_train_pat)
a4fc5ed6 1201{
4ef69c7a 1202 struct drm_device *dev = intel_dp->base.base.dev;
a4fc5ed6 1203 struct drm_i915_private *dev_priv = dev->dev_private;
a4fc5ed6
KP
1204 int ret;
1205
ea5b213a
CW
1206 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1207 POSTING_READ(intel_dp->output_reg);
a4fc5ed6 1208
ea5b213a 1209 intel_dp_aux_native_write_1(intel_dp,
a4fc5ed6
KP
1210 DP_TRAINING_PATTERN_SET,
1211 dp_train_pat);
1212
ea5b213a 1213 ret = intel_dp_aux_native_write(intel_dp,
58e10eb9
CW
1214 DP_TRAINING_LANE0_SET,
1215 intel_dp->train_set, 4);
a4fc5ed6
KP
1216 if (ret != 4)
1217 return false;
1218
1219 return true;
1220}
1221
33a34e4e 1222/* Enable corresponding port and start training pattern 1 */
a4fc5ed6 1223static void
33a34e4e 1224intel_dp_start_link_train(struct intel_dp *intel_dp)
a4fc5ed6 1225{
4ef69c7a 1226 struct drm_device *dev = intel_dp->base.base.dev;
a4fc5ed6 1227 struct drm_i915_private *dev_priv = dev->dev_private;
58e10eb9 1228 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
a4fc5ed6
KP
1229 int i;
1230 uint8_t voltage;
1231 bool clock_recovery = false;
a4fc5ed6 1232 int tries;
e3421a18 1233 u32 reg;
ea5b213a 1234 uint32_t DP = intel_dp->DP;
a4fc5ed6 1235
b99a9d9b
KP
1236 /* Enable output, wait for it to become active */
1237 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1238 POSTING_READ(intel_dp->output_reg);
1239 intel_wait_for_vblank(dev, intel_crtc->pipe);
a4fc5ed6 1240
3cf2efb1
CW
1241 /* Write the link configuration data */
1242 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1243 intel_dp->link_configuration,
1244 DP_LINK_CONFIGURATION_SIZE);
a4fc5ed6
KP
1245
1246 DP |= DP_PORT_EN;
cfcb0fc9 1247 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
e3421a18
ZW
1248 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1249 else
1250 DP &= ~DP_LINK_TRAIN_MASK;
33a34e4e 1251 memset(intel_dp->train_set, 0, 4);
a4fc5ed6
KP
1252 voltage = 0xff;
1253 tries = 0;
1254 clock_recovery = false;
1255 for (;;) {
33a34e4e 1256 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
e3421a18 1257 uint32_t signal_levels;
cfcb0fc9 1258 if (IS_GEN6(dev) && is_edp(intel_dp)) {
33a34e4e 1259 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
e3421a18
ZW
1260 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1261 } else {
3cf2efb1 1262 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
e3421a18
ZW
1263 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1264 }
a4fc5ed6 1265
cfcb0fc9 1266 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
e3421a18
ZW
1267 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1268 else
1269 reg = DP | DP_LINK_TRAIN_PAT_1;
1270
ea5b213a 1271 if (!intel_dp_set_link_train(intel_dp, reg,
58e10eb9 1272 DP_TRAINING_PATTERN_1))
a4fc5ed6 1273 break;
a4fc5ed6
KP
1274 /* Set training pattern 1 */
1275
3cf2efb1
CW
1276 udelay(100);
1277 if (!intel_dp_get_link_status(intel_dp))
a4fc5ed6 1278 break;
a4fc5ed6 1279
3cf2efb1
CW
1280 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1281 clock_recovery = true;
1282 break;
1283 }
1284
1285 /* Check to see if we've tried the max voltage */
1286 for (i = 0; i < intel_dp->lane_count; i++)
1287 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
a4fc5ed6 1288 break;
3cf2efb1
CW
1289 if (i == intel_dp->lane_count)
1290 break;
a4fc5ed6 1291
3cf2efb1
CW
1292 /* Check to see if we've tried the same voltage 5 times */
1293 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1294 ++tries;
1295 if (tries == 5)
a4fc5ed6 1296 break;
3cf2efb1
CW
1297 } else
1298 tries = 0;
1299 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
a4fc5ed6 1300
3cf2efb1
CW
1301 /* Compute new intel_dp->train_set as requested by target */
1302 intel_get_adjust_train(intel_dp);
a4fc5ed6
KP
1303 }
1304
33a34e4e
JB
1305 intel_dp->DP = DP;
1306}
1307
1308static void
1309intel_dp_complete_link_train(struct intel_dp *intel_dp)
1310{
4ef69c7a 1311 struct drm_device *dev = intel_dp->base.base.dev;
33a34e4e
JB
1312 struct drm_i915_private *dev_priv = dev->dev_private;
1313 bool channel_eq = false;
1314 int tries;
1315 u32 reg;
1316 uint32_t DP = intel_dp->DP;
1317
a4fc5ed6
KP
1318 /* channel equalization */
1319 tries = 0;
1320 channel_eq = false;
1321 for (;;) {
33a34e4e 1322 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
e3421a18
ZW
1323 uint32_t signal_levels;
1324
cfcb0fc9 1325 if (IS_GEN6(dev) && is_edp(intel_dp)) {
33a34e4e 1326 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
e3421a18
ZW
1327 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1328 } else {
3cf2efb1 1329 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
e3421a18
ZW
1330 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1331 }
1332
cfcb0fc9 1333 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
e3421a18
ZW
1334 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1335 else
1336 reg = DP | DP_LINK_TRAIN_PAT_2;
a4fc5ed6
KP
1337
1338 /* channel eq pattern */
ea5b213a 1339 if (!intel_dp_set_link_train(intel_dp, reg,
58e10eb9 1340 DP_TRAINING_PATTERN_2))
a4fc5ed6
KP
1341 break;
1342
3cf2efb1
CW
1343 udelay(400);
1344 if (!intel_dp_get_link_status(intel_dp))
a4fc5ed6 1345 break;
a4fc5ed6 1346
3cf2efb1
CW
1347 if (intel_channel_eq_ok(intel_dp)) {
1348 channel_eq = true;
1349 break;
1350 }
a4fc5ed6 1351
3cf2efb1
CW
1352 /* Try 5 times */
1353 if (tries > 5)
1354 break;
a4fc5ed6 1355
3cf2efb1
CW
1356 /* Compute new intel_dp->train_set as requested by target */
1357 intel_get_adjust_train(intel_dp);
1358 ++tries;
869184a6 1359 }
3cf2efb1 1360
cfcb0fc9 1361 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
e3421a18
ZW
1362 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1363 else
1364 reg = DP | DP_LINK_TRAIN_OFF;
1365
ea5b213a
CW
1366 I915_WRITE(intel_dp->output_reg, reg);
1367 POSTING_READ(intel_dp->output_reg);
1368 intel_dp_aux_native_write_1(intel_dp,
a4fc5ed6
KP
1369 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1370}
1371
1372static void
ea5b213a 1373intel_dp_link_down(struct intel_dp *intel_dp)
a4fc5ed6 1374{
4ef69c7a 1375 struct drm_device *dev = intel_dp->base.base.dev;
a4fc5ed6 1376 struct drm_i915_private *dev_priv = dev->dev_private;
ea5b213a 1377 uint32_t DP = intel_dp->DP;
a4fc5ed6 1378
1b39d6f3
CW
1379 if ((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0)
1380 return;
1381
28c97730 1382 DRM_DEBUG_KMS("\n");
32f9d658 1383
cfcb0fc9 1384 if (is_edp(intel_dp)) {
32f9d658 1385 DP &= ~DP_PLL_ENABLE;
ea5b213a
CW
1386 I915_WRITE(intel_dp->output_reg, DP);
1387 POSTING_READ(intel_dp->output_reg);
32f9d658
ZW
1388 udelay(100);
1389 }
1390
cfcb0fc9 1391 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp)) {
e3421a18 1392 DP &= ~DP_LINK_TRAIN_MASK_CPT;
ea5b213a 1393 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
e3421a18
ZW
1394 } else {
1395 DP &= ~DP_LINK_TRAIN_MASK;
ea5b213a 1396 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
e3421a18 1397 }
fe255d00 1398 POSTING_READ(intel_dp->output_reg);
5eb08b69 1399
fe255d00 1400 msleep(17);
5eb08b69 1401
cfcb0fc9 1402 if (is_edp(intel_dp))
32f9d658 1403 DP |= DP_LINK_TRAIN_OFF;
5bddd17f 1404
1b39d6f3
CW
1405 if (!HAS_PCH_CPT(dev) &&
1406 I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
1407 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
5bddd17f
EA
1408 /* Hardware workaround: leaving our transcoder select
1409 * set to transcoder B while it's off will prevent the
1410 * corresponding HDMI output on transcoder A.
1411 *
1412 * Combine this with another hardware workaround:
1413 * transcoder select bit can only be cleared while the
1414 * port is enabled.
1415 */
1416 DP &= ~DP_PIPEB_SELECT;
1417 I915_WRITE(intel_dp->output_reg, DP);
1418
1419 /* Changes to enable or select take place the vblank
1420 * after being written.
1421 */
1422 intel_wait_for_vblank(intel_dp->base.base.dev,
1423 intel_crtc->pipe);
1424 }
1425
ea5b213a
CW
1426 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1427 POSTING_READ(intel_dp->output_reg);
a4fc5ed6
KP
1428}
1429
a4fc5ed6
KP
1430/*
1431 * According to DP spec
1432 * 5.1.2:
1433 * 1. Read DPCD
1434 * 2. Configure link according to Receiver Capabilities
1435 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1436 * 4. Check link status on receipt of hot-plug interrupt
1437 */
1438
1439static void
ea5b213a 1440intel_dp_check_link_status(struct intel_dp *intel_dp)
a4fc5ed6 1441{
4ef69c7a 1442 if (!intel_dp->base.base.crtc)
a4fc5ed6
KP
1443 return;
1444
33a34e4e 1445 if (!intel_dp_get_link_status(intel_dp)) {
ea5b213a 1446 intel_dp_link_down(intel_dp);
a4fc5ed6
KP
1447 return;
1448 }
1449
33a34e4e
JB
1450 if (!intel_channel_eq_ok(intel_dp)) {
1451 intel_dp_start_link_train(intel_dp);
1452 intel_dp_complete_link_train(intel_dp);
1453 }
a4fc5ed6 1454}
a4fc5ed6 1455
5eb08b69 1456static enum drm_connector_status
a9756bb5 1457ironlake_dp_detect(struct intel_dp *intel_dp)
5eb08b69 1458{
5eb08b69
ZW
1459 enum drm_connector_status status;
1460
01cb9ea6 1461 /* Can't disconnect eDP */
4d926461 1462 if (is_edp(intel_dp))
01cb9ea6
JB
1463 return connector_status_connected;
1464
5eb08b69 1465 status = connector_status_disconnected;
ea5b213a
CW
1466 if (intel_dp_aux_native_read(intel_dp,
1467 0x000, intel_dp->dpcd,
a9756bb5
ZW
1468 sizeof (intel_dp->dpcd))
1469 == sizeof(intel_dp->dpcd)) {
ea5b213a 1470 if (intel_dp->dpcd[0] != 0)
5eb08b69
ZW
1471 status = connector_status_connected;
1472 }
ea5b213a
CW
1473 DRM_DEBUG_KMS("DPCD: %hx%hx%hx%hx\n", intel_dp->dpcd[0],
1474 intel_dp->dpcd[1], intel_dp->dpcd[2], intel_dp->dpcd[3]);
5eb08b69
ZW
1475 return status;
1476}
1477
a4fc5ed6 1478static enum drm_connector_status
a9756bb5 1479g4x_dp_detect(struct intel_dp *intel_dp)
a4fc5ed6 1480{
4ef69c7a 1481 struct drm_device *dev = intel_dp->base.base.dev;
a4fc5ed6 1482 struct drm_i915_private *dev_priv = dev->dev_private;
a4fc5ed6 1483 enum drm_connector_status status;
a9756bb5 1484 uint32_t temp, bit;
5eb08b69 1485
ea5b213a 1486 switch (intel_dp->output_reg) {
a4fc5ed6
KP
1487 case DP_B:
1488 bit = DPB_HOTPLUG_INT_STATUS;
1489 break;
1490 case DP_C:
1491 bit = DPC_HOTPLUG_INT_STATUS;
1492 break;
1493 case DP_D:
1494 bit = DPD_HOTPLUG_INT_STATUS;
1495 break;
1496 default:
1497 return connector_status_unknown;
1498 }
1499
1500 temp = I915_READ(PORT_HOTPLUG_STAT);
1501
1502 if ((temp & bit) == 0)
1503 return connector_status_disconnected;
1504
1505 status = connector_status_disconnected;
a9756bb5 1506 if (intel_dp_aux_native_read(intel_dp, 0x000, intel_dp->dpcd,
ea5b213a 1507 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
a4fc5ed6 1508 {
ea5b213a 1509 if (intel_dp->dpcd[0] != 0)
a4fc5ed6
KP
1510 status = connector_status_connected;
1511 }
a9756bb5 1512
dd2b379f 1513 return status;
a9756bb5
ZW
1514}
1515
1516/**
1517 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1518 *
1519 * \return true if DP port is connected.
1520 * \return false if DP port is disconnected.
1521 */
1522static enum drm_connector_status
1523intel_dp_detect(struct drm_connector *connector, bool force)
1524{
1525 struct intel_dp *intel_dp = intel_attached_dp(connector);
1526 struct drm_device *dev = intel_dp->base.base.dev;
1527 enum drm_connector_status status;
1528 struct edid *edid = NULL;
1529
1530 intel_dp->has_audio = false;
1531
1532 if (HAS_PCH_SPLIT(dev))
1533 status = ironlake_dp_detect(intel_dp);
1534 else
1535 status = g4x_dp_detect(intel_dp);
1536 if (status != connector_status_connected)
1537 return status;
1538
f684960e
CW
1539 if (intel_dp->force_audio) {
1540 intel_dp->has_audio = intel_dp->force_audio > 0;
1541 } else {
1542 edid = drm_get_edid(connector, &intel_dp->adapter);
1543 if (edid) {
1544 intel_dp->has_audio = drm_detect_monitor_audio(edid);
1545 connector->display_info.raw_edid = NULL;
1546 kfree(edid);
1547 }
a9756bb5
ZW
1548 }
1549
1550 return connector_status_connected;
a4fc5ed6
KP
1551}
1552
1553static int intel_dp_get_modes(struct drm_connector *connector)
1554{
df0e9248 1555 struct intel_dp *intel_dp = intel_attached_dp(connector);
4ef69c7a 1556 struct drm_device *dev = intel_dp->base.base.dev;
32f9d658
ZW
1557 struct drm_i915_private *dev_priv = dev->dev_private;
1558 int ret;
a4fc5ed6
KP
1559
1560 /* We should parse the EDID data and find out if it has an audio sink
1561 */
1562
f899fc64 1563 ret = intel_ddc_get_modes(connector, &intel_dp->adapter);
b9efc480 1564 if (ret) {
4d926461 1565 if (is_edp(intel_dp) && !dev_priv->panel_fixed_mode) {
b9efc480
ZY
1566 struct drm_display_mode *newmode;
1567 list_for_each_entry(newmode, &connector->probed_modes,
1568 head) {
1569 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1570 dev_priv->panel_fixed_mode =
1571 drm_mode_duplicate(dev, newmode);
1572 break;
1573 }
1574 }
1575 }
1576
32f9d658 1577 return ret;
b9efc480 1578 }
32f9d658
ZW
1579
1580 /* if eDP has no EDID, try to use fixed panel mode from VBT */
4d926461 1581 if (is_edp(intel_dp)) {
32f9d658
ZW
1582 if (dev_priv->panel_fixed_mode != NULL) {
1583 struct drm_display_mode *mode;
1584 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1585 drm_mode_probed_add(connector, mode);
1586 return 1;
1587 }
1588 }
1589 return 0;
a4fc5ed6
KP
1590}
1591
f684960e
CW
1592static int
1593intel_dp_set_property(struct drm_connector *connector,
1594 struct drm_property *property,
1595 uint64_t val)
1596{
1597 struct intel_dp *intel_dp = intel_attached_dp(connector);
1598 int ret;
1599
1600 ret = drm_connector_property_set_value(connector, property, val);
1601 if (ret)
1602 return ret;
1603
1604 if (property == intel_dp->force_audio_property) {
1605 if (val == intel_dp->force_audio)
1606 return 0;
1607
1608 intel_dp->force_audio = val;
1609
1610 if (val > 0 && intel_dp->has_audio)
1611 return 0;
1612 if (val < 0 && !intel_dp->has_audio)
1613 return 0;
1614
1615 intel_dp->has_audio = val > 0;
1616 goto done;
1617 }
1618
1619 return -EINVAL;
1620
1621done:
1622 if (intel_dp->base.base.crtc) {
1623 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1624 drm_crtc_helper_set_mode(crtc, &crtc->mode,
1625 crtc->x, crtc->y,
1626 crtc->fb);
1627 }
1628
1629 return 0;
1630}
1631
a4fc5ed6
KP
1632static void
1633intel_dp_destroy (struct drm_connector *connector)
1634{
a4fc5ed6
KP
1635 drm_sysfs_connector_remove(connector);
1636 drm_connector_cleanup(connector);
55f78c43 1637 kfree(connector);
a4fc5ed6
KP
1638}
1639
24d05927
DV
1640static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1641{
1642 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1643
1644 i2c_del_adapter(&intel_dp->adapter);
1645 drm_encoder_cleanup(encoder);
1646 kfree(intel_dp);
1647}
1648
a4fc5ed6
KP
1649static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1650 .dpms = intel_dp_dpms,
1651 .mode_fixup = intel_dp_mode_fixup,
d240f20f 1652 .prepare = intel_dp_prepare,
a4fc5ed6 1653 .mode_set = intel_dp_mode_set,
d240f20f 1654 .commit = intel_dp_commit,
a4fc5ed6
KP
1655};
1656
1657static const struct drm_connector_funcs intel_dp_connector_funcs = {
1658 .dpms = drm_helper_connector_dpms,
a4fc5ed6
KP
1659 .detect = intel_dp_detect,
1660 .fill_modes = drm_helper_probe_single_connector_modes,
f684960e 1661 .set_property = intel_dp_set_property,
a4fc5ed6
KP
1662 .destroy = intel_dp_destroy,
1663};
1664
1665static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1666 .get_modes = intel_dp_get_modes,
1667 .mode_valid = intel_dp_mode_valid,
df0e9248 1668 .best_encoder = intel_best_encoder,
a4fc5ed6
KP
1669};
1670
a4fc5ed6 1671static const struct drm_encoder_funcs intel_dp_enc_funcs = {
24d05927 1672 .destroy = intel_dp_encoder_destroy,
a4fc5ed6
KP
1673};
1674
995b6762 1675static void
21d40d37 1676intel_dp_hot_plug(struct intel_encoder *intel_encoder)
c8110e52 1677{
ea5b213a 1678 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
c8110e52 1679
ea5b213a
CW
1680 if (intel_dp->dpms_mode == DRM_MODE_DPMS_ON)
1681 intel_dp_check_link_status(intel_dp);
c8110e52 1682}
6207937d 1683
e3421a18
ZW
1684/* Return which DP Port should be selected for Transcoder DP control */
1685int
1686intel_trans_dp_port_sel (struct drm_crtc *crtc)
1687{
1688 struct drm_device *dev = crtc->dev;
1689 struct drm_mode_config *mode_config = &dev->mode_config;
1690 struct drm_encoder *encoder;
e3421a18
ZW
1691
1692 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
ea5b213a
CW
1693 struct intel_dp *intel_dp;
1694
d8201ab6 1695 if (encoder->crtc != crtc)
e3421a18
ZW
1696 continue;
1697
ea5b213a
CW
1698 intel_dp = enc_to_intel_dp(encoder);
1699 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
1700 return intel_dp->output_reg;
e3421a18 1701 }
ea5b213a 1702
e3421a18
ZW
1703 return -1;
1704}
1705
36e83a18 1706/* check the VBT to see whether the eDP is on DP-D port */
cb0953d7 1707bool intel_dpd_is_edp(struct drm_device *dev)
36e83a18
ZY
1708{
1709 struct drm_i915_private *dev_priv = dev->dev_private;
1710 struct child_device_config *p_child;
1711 int i;
1712
1713 if (!dev_priv->child_dev_num)
1714 return false;
1715
1716 for (i = 0; i < dev_priv->child_dev_num; i++) {
1717 p_child = dev_priv->child_dev + i;
1718
1719 if (p_child->dvo_port == PORT_IDPD &&
1720 p_child->device_type == DEVICE_TYPE_eDP)
1721 return true;
1722 }
1723 return false;
1724}
1725
f684960e
CW
1726static void
1727intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
1728{
1729 struct drm_device *dev = connector->dev;
1730
1731 intel_dp->force_audio_property =
1732 drm_property_create(dev, DRM_MODE_PROP_RANGE, "force_audio", 2);
1733 if (intel_dp->force_audio_property) {
1734 intel_dp->force_audio_property->values[0] = -1;
1735 intel_dp->force_audio_property->values[1] = 1;
1736 drm_connector_attach_property(connector, intel_dp->force_audio_property, 0);
1737 }
1738}
1739
a4fc5ed6
KP
1740void
1741intel_dp_init(struct drm_device *dev, int output_reg)
1742{
1743 struct drm_i915_private *dev_priv = dev->dev_private;
1744 struct drm_connector *connector;
ea5b213a 1745 struct intel_dp *intel_dp;
21d40d37 1746 struct intel_encoder *intel_encoder;
55f78c43 1747 struct intel_connector *intel_connector;
5eb08b69 1748 const char *name = NULL;
b329530c 1749 int type;
a4fc5ed6 1750
ea5b213a
CW
1751 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
1752 if (!intel_dp)
a4fc5ed6
KP
1753 return;
1754
55f78c43
ZW
1755 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1756 if (!intel_connector) {
ea5b213a 1757 kfree(intel_dp);
55f78c43
ZW
1758 return;
1759 }
ea5b213a 1760 intel_encoder = &intel_dp->base;
55f78c43 1761
ea5b213a 1762 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
b329530c 1763 if (intel_dpd_is_edp(dev))
ea5b213a 1764 intel_dp->is_pch_edp = true;
b329530c 1765
cfcb0fc9 1766 if (output_reg == DP_A || is_pch_edp(intel_dp)) {
b329530c
AJ
1767 type = DRM_MODE_CONNECTOR_eDP;
1768 intel_encoder->type = INTEL_OUTPUT_EDP;
1769 } else {
1770 type = DRM_MODE_CONNECTOR_DisplayPort;
1771 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1772 }
1773
55f78c43 1774 connector = &intel_connector->base;
b329530c 1775 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
a4fc5ed6
KP
1776 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1777
eb1f8e4f
DA
1778 connector->polled = DRM_CONNECTOR_POLL_HPD;
1779
652af9d7 1780 if (output_reg == DP_B || output_reg == PCH_DP_B)
21d40d37 1781 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
652af9d7 1782 else if (output_reg == DP_C || output_reg == PCH_DP_C)
21d40d37 1783 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
652af9d7 1784 else if (output_reg == DP_D || output_reg == PCH_DP_D)
21d40d37 1785 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
f8aed700 1786
cfcb0fc9 1787 if (is_edp(intel_dp))
21d40d37 1788 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
6251ec0a 1789
21d40d37 1790 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
a4fc5ed6
KP
1791 connector->interlace_allowed = true;
1792 connector->doublescan_allowed = 0;
1793
ea5b213a
CW
1794 intel_dp->output_reg = output_reg;
1795 intel_dp->has_audio = false;
1796 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
a4fc5ed6 1797
4ef69c7a 1798 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
a4fc5ed6 1799 DRM_MODE_ENCODER_TMDS);
4ef69c7a 1800 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
a4fc5ed6 1801
df0e9248 1802 intel_connector_attach_encoder(intel_connector, intel_encoder);
a4fc5ed6
KP
1803 drm_sysfs_connector_add(connector);
1804
1805 /* Set up the DDC bus. */
5eb08b69 1806 switch (output_reg) {
32f9d658
ZW
1807 case DP_A:
1808 name = "DPDDC-A";
1809 break;
5eb08b69
ZW
1810 case DP_B:
1811 case PCH_DP_B:
b01f2c3a
JB
1812 dev_priv->hotplug_supported_mask |=
1813 HDMIB_HOTPLUG_INT_STATUS;
5eb08b69
ZW
1814 name = "DPDDC-B";
1815 break;
1816 case DP_C:
1817 case PCH_DP_C:
b01f2c3a
JB
1818 dev_priv->hotplug_supported_mask |=
1819 HDMIC_HOTPLUG_INT_STATUS;
5eb08b69
ZW
1820 name = "DPDDC-C";
1821 break;
1822 case DP_D:
1823 case PCH_DP_D:
b01f2c3a
JB
1824 dev_priv->hotplug_supported_mask |=
1825 HDMID_HOTPLUG_INT_STATUS;
5eb08b69
ZW
1826 name = "DPDDC-D";
1827 break;
1828 }
1829
ea5b213a 1830 intel_dp_i2c_init(intel_dp, intel_connector, name);
32f9d658 1831
89667383
JB
1832 /* Cache some DPCD data in the eDP case */
1833 if (is_edp(intel_dp)) {
1834 int ret;
1835 bool was_on;
1836
1837 was_on = ironlake_edp_panel_on(intel_dp);
1838 ret = intel_dp_aux_native_read(intel_dp, DP_DPCD_REV,
1839 intel_dp->dpcd,
1840 sizeof(intel_dp->dpcd));
1841 if (ret == sizeof(intel_dp->dpcd)) {
1842 if (intel_dp->dpcd[0] >= 0x11)
1843 dev_priv->no_aux_handshake = intel_dp->dpcd[3] &
1844 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
1845 } else {
1846 DRM_ERROR("failed to retrieve link info\n");
1847 }
1848 if (!was_on)
1849 ironlake_edp_panel_off(dev);
1850 }
1851
21d40d37 1852 intel_encoder->hot_plug = intel_dp_hot_plug;
a4fc5ed6 1853
4d926461 1854 if (is_edp(intel_dp)) {
32f9d658
ZW
1855 /* initialize panel mode from VBT if available for eDP */
1856 if (dev_priv->lfp_lvds_vbt_mode) {
1857 dev_priv->panel_fixed_mode =
1858 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1859 if (dev_priv->panel_fixed_mode) {
1860 dev_priv->panel_fixed_mode->type |=
1861 DRM_MODE_TYPE_PREFERRED;
1862 }
1863 }
1864 }
1865
f684960e
CW
1866 intel_dp_add_properties(intel_dp, connector);
1867
a4fc5ed6
KP
1868 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1869 * 0xd. Failure to do so will result in spurious interrupts being
1870 * generated on the port when a cable is not attached.
1871 */
1872 if (IS_G4X(dev) && !IS_GM45(dev)) {
1873 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
1874 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
1875 }
1876}
This page took 0.26554 seconds and 5 git commands to generate.