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