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