xref: /NextBSD/sys/dev/drm2/i915/intel_dp.c (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
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 <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <dev/drm2/drmP.h>
32 #include <dev/drm2/drm.h>
33 #include <dev/drm2/drm_crtc.h>
34 #include <dev/drm2/drm_crtc_helper.h>
35 #include <dev/drm2/i915/i915_drm.h>
36 #include <dev/drm2/i915/i915_drv.h>
37 #include <dev/drm2/i915/intel_drv.h>
38 #include <dev/drm2/drm_dp_helper.h>
39 
40 #define DP_RECEIVER_CAP_SIZE	0xf
41 #define DP_LINK_STATUS_SIZE	6
42 #define DP_LINK_CHECK_TIMEOUT	(10 * 1000)
43 
44 #define DP_LINK_CONFIGURATION_SIZE	9
45 
46 struct intel_dp {
47 	struct intel_encoder base;
48 	uint32_t output_reg;
49 	uint32_t DP;
50 	uint8_t  link_configuration[DP_LINK_CONFIGURATION_SIZE];
51 	bool has_audio;
52 	enum hdmi_force_audio force_audio;
53 	uint32_t color_range;
54 	int dpms_mode;
55 	uint8_t link_bw;
56 	uint8_t lane_count;
57 	uint8_t dpcd[DP_RECEIVER_CAP_SIZE];
58 	device_t dp_iic_bus;
59 	device_t adapter;
60 	bool is_pch_edp;
61 	uint8_t	train_set[4];
62 	int panel_power_up_delay;
63 	int panel_power_down_delay;
64 	int panel_power_cycle_delay;
65 	int backlight_on_delay;
66 	int backlight_off_delay;
67 	struct drm_display_mode *panel_fixed_mode;  /* for eDP */
68 	struct timeout_task panel_vdd_task;
69 	bool want_panel_vdd;
70 };
71 
72 /**
73  * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
74  * @intel_dp: DP struct
75  *
76  * If a CPU or PCH DP output is attached to an eDP panel, this function
77  * will return true, and false otherwise.
78  */
is_edp(struct intel_dp * intel_dp)79 static bool is_edp(struct intel_dp *intel_dp)
80 {
81 	return intel_dp->base.type == INTEL_OUTPUT_EDP;
82 }
83 
84 /**
85  * is_pch_edp - is the port on the PCH and attached to an eDP panel?
86  * @intel_dp: DP struct
87  *
88  * Returns true if the given DP struct corresponds to a PCH DP port attached
89  * to an eDP panel, false otherwise.  Helpful for determining whether we
90  * may need FDI resources for a given DP output or not.
91  */
is_pch_edp(struct intel_dp * intel_dp)92 static bool is_pch_edp(struct intel_dp *intel_dp)
93 {
94 	return intel_dp->is_pch_edp;
95 }
96 
97 /**
98  * is_cpu_edp - is the port on the CPU and attached to an eDP panel?
99  * @intel_dp: DP struct
100  *
101  * Returns true if the given DP struct corresponds to a CPU eDP port.
102  */
is_cpu_edp(struct intel_dp * intel_dp)103 static bool is_cpu_edp(struct intel_dp *intel_dp)
104 {
105 	return is_edp(intel_dp) && !is_pch_edp(intel_dp);
106 }
107 
enc_to_intel_dp(struct drm_encoder * encoder)108 static struct intel_dp *enc_to_intel_dp(struct drm_encoder *encoder)
109 {
110 	return container_of(encoder, struct intel_dp, base.base);
111 }
112 
intel_attached_dp(struct drm_connector * connector)113 static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
114 {
115 	return container_of(intel_attached_encoder(connector),
116 			    struct intel_dp, base);
117 }
118 
119 /**
120  * intel_encoder_is_pch_edp - is the given encoder a PCH attached eDP?
121  * @encoder: DRM encoder
122  *
123  * Return true if @encoder corresponds to a PCH attached eDP panel.  Needed
124  * by intel_display.c.
125  */
intel_encoder_is_pch_edp(struct drm_encoder * encoder)126 bool intel_encoder_is_pch_edp(struct drm_encoder *encoder)
127 {
128 	struct intel_dp *intel_dp;
129 
130 	if (!encoder)
131 		return false;
132 
133 	intel_dp = enc_to_intel_dp(encoder);
134 
135 	return is_pch_edp(intel_dp);
136 }
137 
138 static void intel_dp_start_link_train(struct intel_dp *intel_dp);
139 static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
140 static void intel_dp_link_down(struct intel_dp *intel_dp);
141 
142 void
intel_edp_link_config(struct intel_encoder * intel_encoder,int * lane_num,int * link_bw)143 intel_edp_link_config(struct intel_encoder *intel_encoder,
144 		       int *lane_num, int *link_bw)
145 {
146 	struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
147 
148 	*lane_num = intel_dp->lane_count;
149 	if (intel_dp->link_bw == DP_LINK_BW_1_62)
150 		*link_bw = 162000;
151 	else if (intel_dp->link_bw == DP_LINK_BW_2_7)
152 		*link_bw = 270000;
153 }
154 
155 static int
intel_dp_max_lane_count(struct intel_dp * intel_dp)156 intel_dp_max_lane_count(struct intel_dp *intel_dp)
157 {
158 	int max_lane_count = intel_dp->dpcd[DP_MAX_LANE_COUNT] & 0x1f;
159 	switch (max_lane_count) {
160 	case 1: case 2: case 4:
161 		break;
162 	default:
163 		max_lane_count = 4;
164 	}
165 	return max_lane_count;
166 }
167 
168 static int
intel_dp_max_link_bw(struct intel_dp * intel_dp)169 intel_dp_max_link_bw(struct intel_dp *intel_dp)
170 {
171 	int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
172 
173 	switch (max_link_bw) {
174 	case DP_LINK_BW_1_62:
175 	case DP_LINK_BW_2_7:
176 		break;
177 	default:
178 		max_link_bw = DP_LINK_BW_1_62;
179 		break;
180 	}
181 	return max_link_bw;
182 }
183 
184 static int
intel_dp_link_clock(uint8_t link_bw)185 intel_dp_link_clock(uint8_t link_bw)
186 {
187 	if (link_bw == DP_LINK_BW_2_7)
188 		return 270000;
189 	else
190 		return 162000;
191 }
192 
193 /*
194  * The units on the numbers in the next two are... bizarre.  Examples will
195  * make it clearer; this one parallels an example in the eDP spec.
196  *
197  * intel_dp_max_data_rate for one lane of 2.7GHz evaluates as:
198  *
199  *     270000 * 1 * 8 / 10 == 216000
200  *
201  * The actual data capacity of that configuration is 2.16Gbit/s, so the
202  * units are decakilobits.  ->clock in a drm_display_mode is in kilohertz -
203  * or equivalently, kilopixels per second - so for 1680x1050R it'd be
204  * 119000.  At 18bpp that's 2142000 kilobits per second.
205  *
206  * Thus the strange-looking division by 10 in intel_dp_link_required, to
207  * get the result in decakilobits instead of kilobits.
208  */
209 
210 static int
intel_dp_link_required(int pixel_clock,int bpp)211 intel_dp_link_required(int pixel_clock, int bpp)
212 {
213 	return (pixel_clock * bpp + 9) / 10;
214 }
215 
216 static int
intel_dp_max_data_rate(int max_link_clock,int max_lanes)217 intel_dp_max_data_rate(int max_link_clock, int max_lanes)
218 {
219 	return (max_link_clock * max_lanes * 8) / 10;
220 }
221 
222 static bool
intel_dp_adjust_dithering(struct intel_dp * intel_dp,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)223 intel_dp_adjust_dithering(struct intel_dp *intel_dp,
224 			  const struct drm_display_mode *mode,
225 			  struct drm_display_mode *adjusted_mode)
226 {
227 	int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
228 	int max_lanes = intel_dp_max_lane_count(intel_dp);
229 	int max_rate, mode_rate;
230 
231 	mode_rate = intel_dp_link_required(mode->clock, 24);
232 	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
233 
234 	if (mode_rate > max_rate) {
235 		mode_rate = intel_dp_link_required(mode->clock, 18);
236 		if (mode_rate > max_rate)
237 			return false;
238 
239 		if (adjusted_mode)
240 			adjusted_mode->private_flags
241 				|= INTEL_MODE_DP_FORCE_6BPC;
242 
243 		return true;
244 	}
245 
246 	return true;
247 }
248 
249 static int
intel_dp_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)250 intel_dp_mode_valid(struct drm_connector *connector,
251 		    struct drm_display_mode *mode)
252 {
253 	struct intel_dp *intel_dp = intel_attached_dp(connector);
254 
255 	if (is_edp(intel_dp) && intel_dp->panel_fixed_mode) {
256 		if (mode->hdisplay > intel_dp->panel_fixed_mode->hdisplay)
257 			return MODE_PANEL;
258 
259 		if (mode->vdisplay > intel_dp->panel_fixed_mode->vdisplay)
260 			return MODE_PANEL;
261 	}
262 
263 	if (!intel_dp_adjust_dithering(intel_dp, mode, NULL))
264 		return MODE_CLOCK_HIGH;
265 
266 	if (mode->clock < 10000)
267 		return MODE_CLOCK_LOW;
268 
269 	return MODE_OK;
270 }
271 
272 static uint32_t
pack_aux(uint8_t * src,int src_bytes)273 pack_aux(uint8_t *src, int src_bytes)
274 {
275 	int	i;
276 	uint32_t v = 0;
277 
278 	if (src_bytes > 4)
279 		src_bytes = 4;
280 	for (i = 0; i < src_bytes; i++)
281 		v |= ((uint32_t) src[i]) << ((3-i) * 8);
282 	return v;
283 }
284 
285 static void
unpack_aux(uint32_t src,uint8_t * dst,int dst_bytes)286 unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
287 {
288 	int i;
289 	if (dst_bytes > 4)
290 		dst_bytes = 4;
291 	for (i = 0; i < dst_bytes; i++)
292 		dst[i] = src >> ((3-i) * 8);
293 }
294 
295 /* hrawclock is 1/4 the FSB frequency */
296 static int
intel_hrawclk(struct drm_device * dev)297 intel_hrawclk(struct drm_device *dev)
298 {
299 	struct drm_i915_private *dev_priv = dev->dev_private;
300 	uint32_t clkcfg;
301 
302 	clkcfg = I915_READ(CLKCFG);
303 	switch (clkcfg & CLKCFG_FSB_MASK) {
304 	case CLKCFG_FSB_400:
305 		return 100;
306 	case CLKCFG_FSB_533:
307 		return 133;
308 	case CLKCFG_FSB_667:
309 		return 166;
310 	case CLKCFG_FSB_800:
311 		return 200;
312 	case CLKCFG_FSB_1067:
313 		return 266;
314 	case CLKCFG_FSB_1333:
315 		return 333;
316 	/* these two are just a guess; one of them might be right */
317 	case CLKCFG_FSB_1600:
318 	case CLKCFG_FSB_1600_ALT:
319 		return 400;
320 	default:
321 		return 133;
322 	}
323 }
324 
ironlake_edp_have_panel_power(struct intel_dp * intel_dp)325 static bool ironlake_edp_have_panel_power(struct intel_dp *intel_dp)
326 {
327 	struct drm_device *dev = intel_dp->base.base.dev;
328 	struct drm_i915_private *dev_priv = dev->dev_private;
329 
330 	return (I915_READ(PCH_PP_STATUS) & PP_ON) != 0;
331 }
332 
ironlake_edp_have_panel_vdd(struct intel_dp * intel_dp)333 static bool ironlake_edp_have_panel_vdd(struct intel_dp *intel_dp)
334 {
335 	struct drm_device *dev = intel_dp->base.base.dev;
336 	struct drm_i915_private *dev_priv = dev->dev_private;
337 
338 	return (I915_READ(PCH_PP_CONTROL) & EDP_FORCE_VDD) != 0;
339 }
340 
341 static void
intel_dp_check_edp(struct intel_dp * intel_dp)342 intel_dp_check_edp(struct intel_dp *intel_dp)
343 {
344 	struct drm_device *dev = intel_dp->base.base.dev;
345 	struct drm_i915_private *dev_priv = dev->dev_private;
346 
347 	if (!is_edp(intel_dp))
348 		return;
349 	if (!ironlake_edp_have_panel_power(intel_dp) && !ironlake_edp_have_panel_vdd(intel_dp)) {
350 		printf("eDP powered off while attempting aux channel communication.\n");
351 		DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
352 			      I915_READ(PCH_PP_STATUS),
353 			      I915_READ(PCH_PP_CONTROL));
354 	}
355 }
356 
357 static int
intel_dp_aux_ch(struct intel_dp * intel_dp,uint8_t * send,int send_bytes,uint8_t * recv,int recv_size)358 intel_dp_aux_ch(struct intel_dp *intel_dp,
359 		uint8_t *send, int send_bytes,
360 		uint8_t *recv, int recv_size)
361 {
362 	uint32_t output_reg = intel_dp->output_reg;
363 	struct drm_device *dev = intel_dp->base.base.dev;
364 	struct drm_i915_private *dev_priv = dev->dev_private;
365 	uint32_t ch_ctl = output_reg + 0x10;
366 	uint32_t ch_data = ch_ctl + 4;
367 	int i;
368 	int recv_bytes;
369 	uint32_t status;
370 	uint32_t aux_clock_divider;
371 	int try, precharge = 5;
372 
373 	intel_dp_check_edp(intel_dp);
374 	/* The clock divider is based off the hrawclk,
375 	 * and would like to run at 2MHz. So, take the
376 	 * hrawclk value and divide by 2 and use that
377 	 *
378 	 * Note that PCH attached eDP panels should use a 125MHz input
379 	 * clock divider.
380 	 */
381 	if (is_cpu_edp(intel_dp)) {
382 		if (IS_GEN6(dev) || IS_GEN7(dev))
383 			aux_clock_divider = 200; /* SNB & IVB eDP input clock at 400Mhz */
384 		else
385 			aux_clock_divider = 225; /* eDP input clock at 450Mhz */
386 	} else if (HAS_PCH_SPLIT(dev))
387 		aux_clock_divider = 63; /* IRL input clock fixed at 125Mhz */
388 	else
389 		aux_clock_divider = intel_hrawclk(dev) / 2;
390 
391 	/* Try to wait for any previous AUX channel activity */
392 	for (try = 0; try < 3; try++) {
393 		status = I915_READ(ch_ctl);
394 		if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
395 			break;
396 		drm_msleep(1, "915ach");
397 	}
398 
399 	if (try == 3) {
400 		printf("dp_aux_ch not started status 0x%08x\n",
401 		     I915_READ(ch_ctl));
402 		return -EBUSY;
403 	}
404 
405 	/* Must try at least 3 times according to DP spec */
406 	for (try = 0; try < 5; try++) {
407 		/* Load the send data into the aux channel data registers */
408 		for (i = 0; i < send_bytes; i += 4)
409 			I915_WRITE(ch_data + i,
410 				   pack_aux(send + i, send_bytes - i));
411 
412 		/* Send the command and wait for it to complete */
413 		I915_WRITE(ch_ctl,
414 			   DP_AUX_CH_CTL_SEND_BUSY |
415 			   DP_AUX_CH_CTL_TIME_OUT_400us |
416 			   (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
417 			   (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
418 			   (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
419 			   DP_AUX_CH_CTL_DONE |
420 			   DP_AUX_CH_CTL_TIME_OUT_ERROR |
421 			   DP_AUX_CH_CTL_RECEIVE_ERROR);
422 		for (;;) {
423 			status = I915_READ(ch_ctl);
424 			if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
425 				break;
426 			DELAY(100);
427 		}
428 
429 		/* Clear done status and any errors */
430 		I915_WRITE(ch_ctl,
431 			   status |
432 			   DP_AUX_CH_CTL_DONE |
433 			   DP_AUX_CH_CTL_TIME_OUT_ERROR |
434 			   DP_AUX_CH_CTL_RECEIVE_ERROR);
435 
436 		if (status & (DP_AUX_CH_CTL_TIME_OUT_ERROR |
437 			      DP_AUX_CH_CTL_RECEIVE_ERROR))
438 			continue;
439 		if (status & DP_AUX_CH_CTL_DONE)
440 			break;
441 	}
442 
443 	if ((status & DP_AUX_CH_CTL_DONE) == 0) {
444 		DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
445 		return -EBUSY;
446 	}
447 
448 	/* Check for timeout or receive error.
449 	 * Timeouts occur when the sink is not connected
450 	 */
451 	if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
452 		DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
453 		return -EIO;
454 	}
455 
456 	/* Timeouts occur when the device isn't connected, so they're
457 	 * "normal" -- don't fill the kernel log with these */
458 	if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
459 		DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
460 		return -ETIMEDOUT;
461 	}
462 
463 	/* Unload any bytes sent back from the other side */
464 	recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
465 		      DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
466 	if (recv_bytes > recv_size)
467 		recv_bytes = recv_size;
468 
469 	for (i = 0; i < recv_bytes; i += 4)
470 		unpack_aux(I915_READ(ch_data + i),
471 			   recv + i, recv_bytes - i);
472 
473 	return recv_bytes;
474 }
475 
476 /* Write data to the aux channel in native mode */
477 static int
intel_dp_aux_native_write(struct intel_dp * intel_dp,uint16_t address,uint8_t * send,int send_bytes)478 intel_dp_aux_native_write(struct intel_dp *intel_dp,
479 			  uint16_t address, uint8_t *send, int send_bytes)
480 {
481 	int ret;
482 	uint8_t	msg[20];
483 	int msg_bytes;
484 	uint8_t	ack;
485 
486 	intel_dp_check_edp(intel_dp);
487 	if (send_bytes > 16)
488 		return -1;
489 	msg[0] = AUX_NATIVE_WRITE << 4;
490 	msg[1] = address >> 8;
491 	msg[2] = address & 0xff;
492 	msg[3] = send_bytes - 1;
493 	memcpy(&msg[4], send, send_bytes);
494 	msg_bytes = send_bytes + 4;
495 	for (;;) {
496 		ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
497 		if (ret < 0)
498 			return ret;
499 		if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
500 			break;
501 		else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
502 			DELAY(100);
503 		else
504 			return -EIO;
505 	}
506 	return send_bytes;
507 }
508 
509 /* Write a single byte to the aux channel in native mode */
510 static int
intel_dp_aux_native_write_1(struct intel_dp * intel_dp,uint16_t address,uint8_t byte)511 intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
512 			    uint16_t address, uint8_t byte)
513 {
514 	return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
515 }
516 
517 /* read bytes from a native aux channel */
518 static int
intel_dp_aux_native_read(struct intel_dp * intel_dp,uint16_t address,uint8_t * recv,int recv_bytes)519 intel_dp_aux_native_read(struct intel_dp *intel_dp,
520 			 uint16_t address, uint8_t *recv, int recv_bytes)
521 {
522 	uint8_t msg[4];
523 	int msg_bytes;
524 	uint8_t reply[20];
525 	int reply_bytes;
526 	uint8_t ack;
527 	int ret;
528 
529 	intel_dp_check_edp(intel_dp);
530 	msg[0] = AUX_NATIVE_READ << 4;
531 	msg[1] = address >> 8;
532 	msg[2] = address & 0xff;
533 	msg[3] = recv_bytes - 1;
534 
535 	msg_bytes = 4;
536 	reply_bytes = recv_bytes + 1;
537 
538 	for (;;) {
539 		ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
540 				      reply, reply_bytes);
541 		if (ret == 0)
542 			return -EPROTO;
543 		if (ret < 0)
544 			return ret;
545 		ack = reply[0];
546 		if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
547 			memcpy(recv, reply + 1, ret - 1);
548 			return ret - 1;
549 		}
550 		else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
551 			DELAY(100);
552 		else
553 			return -EIO;
554 	}
555 }
556 
557 static int
intel_dp_i2c_aux_ch(device_t adapter,int mode,uint8_t write_byte,uint8_t * read_byte)558 intel_dp_i2c_aux_ch(device_t adapter, int mode,
559 		    uint8_t write_byte, uint8_t *read_byte)
560 {
561 	struct iic_dp_aux_data *data = device_get_softc(adapter);
562 	struct intel_dp *intel_dp = data->priv;
563 	uint16_t address = data->address;
564 	uint8_t msg[5];
565 	uint8_t reply[2];
566 	unsigned retry;
567 	int msg_bytes;
568 	int reply_bytes;
569 	int ret;
570 
571 	intel_dp_check_edp(intel_dp);
572 	/* Set up the command byte */
573 	if (mode & MODE_I2C_READ)
574 		msg[0] = AUX_I2C_READ << 4;
575 	else
576 		msg[0] = AUX_I2C_WRITE << 4;
577 
578 	if (!(mode & MODE_I2C_STOP))
579 		msg[0] |= AUX_I2C_MOT << 4;
580 
581 	msg[1] = address >> 8;
582 	msg[2] = address;
583 
584 	switch (mode) {
585 	case MODE_I2C_WRITE:
586 		msg[3] = 0;
587 		msg[4] = write_byte;
588 		msg_bytes = 5;
589 		reply_bytes = 1;
590 		break;
591 	case MODE_I2C_READ:
592 		msg[3] = 0;
593 		msg_bytes = 4;
594 		reply_bytes = 2;
595 		break;
596 	default:
597 		msg_bytes = 3;
598 		reply_bytes = 1;
599 		break;
600 	}
601 
602 	for (retry = 0; retry < 5; retry++) {
603 		ret = intel_dp_aux_ch(intel_dp,
604 				      msg, msg_bytes,
605 				      reply, reply_bytes);
606 		if (ret < 0) {
607 			DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
608 			return ret;
609 		}
610 
611 		switch (reply[0] & AUX_NATIVE_REPLY_MASK) {
612 		case AUX_NATIVE_REPLY_ACK:
613 			/* I2C-over-AUX Reply field is only valid
614 			 * when paired with AUX ACK.
615 			 */
616 			break;
617 		case AUX_NATIVE_REPLY_NACK:
618 			DRM_DEBUG_KMS("aux_ch native nack\n");
619 			return -EREMOTEIO;
620 		case AUX_NATIVE_REPLY_DEFER:
621 			DELAY(100);
622 			continue;
623 		default:
624 			DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
625 				  reply[0]);
626 			return -EREMOTEIO;
627 		}
628 
629 		switch (reply[0] & AUX_I2C_REPLY_MASK) {
630 		case AUX_I2C_REPLY_ACK:
631 			if (mode == MODE_I2C_READ) {
632 				*read_byte = reply[1];
633 			}
634 			return (0/*reply_bytes - 1*/);
635 		case AUX_I2C_REPLY_NACK:
636 			DRM_DEBUG_KMS("aux_i2c nack\n");
637 			return -EREMOTEIO;
638 		case AUX_I2C_REPLY_DEFER:
639 			DRM_DEBUG_KMS("aux_i2c defer\n");
640 			DELAY(100);
641 			break;
642 		default:
643 			DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
644 			return -EREMOTEIO;
645 		}
646 	}
647 
648 	DRM_ERROR("too many retries, giving up\n");
649 	return -EREMOTEIO;
650 }
651 
652 static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp);
653 static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync);
654 
655 static int
intel_dp_i2c_init(struct intel_dp * intel_dp,struct intel_connector * intel_connector,const char * name)656 intel_dp_i2c_init(struct intel_dp *intel_dp,
657 		  struct intel_connector *intel_connector, const char *name)
658 {
659 	int	ret;
660 
661 	DRM_DEBUG_KMS("i2c_init %s\n", name);
662 
663 	ironlake_edp_panel_vdd_on(intel_dp);
664 	ret = iic_dp_aux_add_bus(intel_connector->base.dev->dev, name,
665 	    intel_dp_i2c_aux_ch, intel_dp, &intel_dp->dp_iic_bus,
666 	    &intel_dp->adapter);
667 	ironlake_edp_panel_vdd_off(intel_dp, false);
668 	return ret;
669 }
670 
671 static bool
intel_dp_mode_fixup(struct drm_encoder * encoder,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)672 intel_dp_mode_fixup(struct drm_encoder *encoder, const struct drm_display_mode *mode,
673 		    struct drm_display_mode *adjusted_mode)
674 {
675 	struct drm_device *dev = encoder->dev;
676 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
677 	int lane_count, clock;
678 	int max_lane_count = intel_dp_max_lane_count(intel_dp);
679 	int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
680 	int bpp, mode_rate;
681 	static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
682 
683 	if (is_edp(intel_dp) && intel_dp->panel_fixed_mode) {
684 		intel_fixed_panel_mode(intel_dp->panel_fixed_mode, adjusted_mode);
685 		intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
686 					mode, adjusted_mode);
687 	}
688 
689 	DRM_DEBUG_KMS("DP link computation with max lane count %i "
690 		      "max bw %02x pixel clock %iKHz\n",
691 		      max_lane_count, bws[max_clock], mode->clock);
692 
693 	if (!intel_dp_adjust_dithering(intel_dp, adjusted_mode, adjusted_mode))
694 		return false;
695 
696 	bpp = adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC ? 18 : 24;
697 	mode_rate = intel_dp_link_required(adjusted_mode->clock, bpp);
698 
699 	for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
700 		for (clock = 0; clock <= max_clock; clock++) {
701 			int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
702 
703 			if (mode_rate <= link_avail) {
704 				intel_dp->link_bw = bws[clock];
705 				intel_dp->lane_count = lane_count;
706 				adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
707 				DRM_DEBUG_KMS("DP link bw %02x lane "
708 						"count %d clock %d bpp %d\n",
709 				       intel_dp->link_bw, intel_dp->lane_count,
710 				       adjusted_mode->clock, bpp);
711 				DRM_DEBUG_KMS("DP link bw required %i available %i\n",
712 					      mode_rate, link_avail);
713 				return true;
714 			}
715 		}
716 	}
717 
718 	return false;
719 }
720 
721 struct intel_dp_m_n {
722 	uint32_t	tu;
723 	uint32_t	gmch_m;
724 	uint32_t	gmch_n;
725 	uint32_t	link_m;
726 	uint32_t	link_n;
727 };
728 
729 static void
intel_reduce_ratio(uint32_t * num,uint32_t * den)730 intel_reduce_ratio(uint32_t *num, uint32_t *den)
731 {
732 	while (*num > 0xffffff || *den > 0xffffff) {
733 		*num >>= 1;
734 		*den >>= 1;
735 	}
736 }
737 
738 static void
intel_dp_compute_m_n(int bpp,int nlanes,int pixel_clock,int link_clock,struct intel_dp_m_n * m_n)739 intel_dp_compute_m_n(int bpp,
740 		     int nlanes,
741 		     int pixel_clock,
742 		     int link_clock,
743 		     struct intel_dp_m_n *m_n)
744 {
745 	m_n->tu = 64;
746 	m_n->gmch_m = (pixel_clock * bpp) >> 3;
747 	m_n->gmch_n = link_clock * nlanes;
748 	intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
749 	m_n->link_m = pixel_clock;
750 	m_n->link_n = link_clock;
751 	intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
752 }
753 
754 void
intel_dp_set_m_n(struct drm_crtc * crtc,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)755 intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
756 		 struct drm_display_mode *adjusted_mode)
757 {
758 	struct drm_device *dev = crtc->dev;
759 	struct drm_mode_config *mode_config = &dev->mode_config;
760 	struct drm_encoder *encoder;
761 	struct drm_i915_private *dev_priv = dev->dev_private;
762 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
763 	int lane_count = 4;
764 	struct intel_dp_m_n m_n;
765 	int pipe = intel_crtc->pipe;
766 
767 	/*
768 	 * Find the lane count in the intel_encoder private
769 	 */
770 	list_for_each_entry(encoder, &mode_config->encoder_list, head) {
771 		struct intel_dp *intel_dp;
772 
773 		if (encoder->crtc != crtc)
774 			continue;
775 
776 		intel_dp = enc_to_intel_dp(encoder);
777 		if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT ||
778 		    intel_dp->base.type == INTEL_OUTPUT_EDP)
779 		{
780 			lane_count = intel_dp->lane_count;
781 			break;
782 		}
783 	}
784 
785 	/*
786 	 * Compute the GMCH and Link ratios. The '3' here is
787 	 * the number of bytes_per_pixel post-LUT, which we always
788 	 * set up for 8-bits of R/G/B, or 3 bytes total.
789 	 */
790 	intel_dp_compute_m_n(intel_crtc->bpp, lane_count,
791 			     mode->clock, adjusted_mode->clock, &m_n);
792 
793 	if (HAS_PCH_SPLIT(dev)) {
794 		I915_WRITE(TRANSDATA_M1(pipe),
795 			   ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
796 			   m_n.gmch_m);
797 		I915_WRITE(TRANSDATA_N1(pipe), m_n.gmch_n);
798 		I915_WRITE(TRANSDPLINK_M1(pipe), m_n.link_m);
799 		I915_WRITE(TRANSDPLINK_N1(pipe), m_n.link_n);
800 	} else {
801 		I915_WRITE(PIPE_GMCH_DATA_M(pipe),
802 			   ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
803 			   m_n.gmch_m);
804 		I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n.gmch_n);
805 		I915_WRITE(PIPE_DP_LINK_M(pipe), m_n.link_m);
806 		I915_WRITE(PIPE_DP_LINK_N(pipe), m_n.link_n);
807 	}
808 }
809 
810 static void ironlake_edp_pll_on(struct drm_encoder *encoder);
811 static void ironlake_edp_pll_off(struct drm_encoder *encoder);
812 
813 static void
intel_dp_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)814 intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
815 		  struct drm_display_mode *adjusted_mode)
816 {
817 	struct drm_device *dev = encoder->dev;
818 	struct drm_i915_private *dev_priv = dev->dev_private;
819 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
820 	struct drm_crtc *crtc = intel_dp->base.base.crtc;
821 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
822 
823 	/* Turn on the eDP PLL if needed */
824 	if (is_edp(intel_dp)) {
825 		if (!is_pch_edp(intel_dp))
826 			ironlake_edp_pll_on(encoder);
827 		else
828 			ironlake_edp_pll_off(encoder);
829 	}
830 
831 	/*
832 	 * There are four kinds of DP registers:
833 	 *
834 	 * 	IBX PCH
835 	 * 	SNB CPU
836 	 *	IVB CPU
837 	 * 	CPT PCH
838 	 *
839 	 * IBX PCH and CPU are the same for almost everything,
840 	 * except that the CPU DP PLL is configured in this
841 	 * register
842 	 *
843 	 * CPT PCH is quite different, having many bits moved
844 	 * to the TRANS_DP_CTL register instead. That
845 	 * configuration happens (oddly) in ironlake_pch_enable
846 	 */
847 
848 	/* Preserve the BIOS-computed detected bit. This is
849 	 * supposed to be read-only.
850 	 */
851 	intel_dp->DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
852 	intel_dp->DP |=  DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
853 
854 	/* Handle DP bits in common between all three register formats */
855 
856 	intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
857 
858 	switch (intel_dp->lane_count) {
859 	case 1:
860 		intel_dp->DP |= DP_PORT_WIDTH_1;
861 		break;
862 	case 2:
863 		intel_dp->DP |= DP_PORT_WIDTH_2;
864 		break;
865 	case 4:
866 		intel_dp->DP |= DP_PORT_WIDTH_4;
867 		break;
868 	}
869 	if (intel_dp->has_audio) {
870 		DRM_DEBUG_KMS("Enabling DP audio on pipe %c\n",
871 				 pipe_name(intel_crtc->pipe));
872 		intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
873 		intel_write_eld(encoder, adjusted_mode);
874 	}
875 	memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
876 	intel_dp->link_configuration[0] = intel_dp->link_bw;
877 	intel_dp->link_configuration[1] = intel_dp->lane_count;
878 	/*
879 	 * Check for DPCD version > 1.1 and enhanced framing support
880 	 */
881 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
882 	    (intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)) {
883 		intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
884 	}
885 
886 	/* Split out the IBX/CPU vs CPT settings */
887 
888 	if (is_cpu_edp(intel_dp) && IS_GEN7(dev)) {
889 		if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
890 			intel_dp->DP |= DP_SYNC_HS_HIGH;
891 		if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
892 			intel_dp->DP |= DP_SYNC_VS_HIGH;
893 		intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
894 
895 		if (intel_dp->link_configuration[1] & DP_LANE_COUNT_ENHANCED_FRAME_EN)
896 			intel_dp->DP |= DP_ENHANCED_FRAMING;
897 
898 		intel_dp->DP |= intel_crtc->pipe << 29;
899 
900 		/* don't miss out required setting for eDP */
901 		intel_dp->DP |= DP_PLL_ENABLE;
902 		if (adjusted_mode->clock < 200000)
903 			intel_dp->DP |= DP_PLL_FREQ_160MHZ;
904 		else
905 			intel_dp->DP |= DP_PLL_FREQ_270MHZ;
906 	} else if (!HAS_PCH_CPT(dev) || is_cpu_edp(intel_dp)) {
907 		intel_dp->DP |= intel_dp->color_range;
908 
909 		if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
910 			intel_dp->DP |= DP_SYNC_HS_HIGH;
911 		if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
912 			intel_dp->DP |= DP_SYNC_VS_HIGH;
913 		intel_dp->DP |= DP_LINK_TRAIN_OFF;
914 
915 		if (intel_dp->link_configuration[1] & DP_LANE_COUNT_ENHANCED_FRAME_EN)
916 			intel_dp->DP |= DP_ENHANCED_FRAMING;
917 
918 		if (intel_crtc->pipe == 1)
919 			intel_dp->DP |= DP_PIPEB_SELECT;
920 
921 		if (is_cpu_edp(intel_dp)) {
922 			/* don't miss out required setting for eDP */
923 			intel_dp->DP |= DP_PLL_ENABLE;
924 			if (adjusted_mode->clock < 200000)
925 				intel_dp->DP |= DP_PLL_FREQ_160MHZ;
926 			else
927 				intel_dp->DP |= DP_PLL_FREQ_270MHZ;
928 		}
929 	} else {
930 		intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
931 	}
932 }
933 
934 #define IDLE_ON_MASK		(PP_ON | 0 	  | PP_SEQUENCE_MASK | 0                     | PP_SEQUENCE_STATE_MASK)
935 #define IDLE_ON_VALUE   	(PP_ON | 0 	  | PP_SEQUENCE_NONE | 0                     | PP_SEQUENCE_STATE_ON_IDLE)
936 
937 #define IDLE_OFF_MASK		(PP_ON | 0        | PP_SEQUENCE_MASK | 0                     | PP_SEQUENCE_STATE_MASK)
938 #define IDLE_OFF_VALUE		(0     | 0        | PP_SEQUENCE_NONE | 0                     | PP_SEQUENCE_STATE_OFF_IDLE)
939 
940 #define IDLE_CYCLE_MASK		(PP_ON | 0        | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
941 #define IDLE_CYCLE_VALUE	(0     | 0        | PP_SEQUENCE_NONE | 0                     | PP_SEQUENCE_STATE_OFF_IDLE)
942 
ironlake_wait_panel_status(struct intel_dp * intel_dp,u32 mask,u32 value)943 static void ironlake_wait_panel_status(struct intel_dp *intel_dp,
944 				       u32 mask,
945 				       u32 value)
946 {
947 	struct drm_device *dev = intel_dp->base.base.dev;
948 	struct drm_i915_private *dev_priv = dev->dev_private;
949 
950 	DRM_DEBUG_KMS("mask %08x value %08x status %08x control %08x\n",
951 		      mask, value,
952 		      I915_READ(PCH_PP_STATUS),
953 		      I915_READ(PCH_PP_CONTROL));
954 
955 	if (_intel_wait_for(dev, (I915_READ(PCH_PP_STATUS) & mask) == value, 5000, 10, "915iwp")) {
956 		DRM_ERROR("Panel status timeout: status %08x control %08x\n",
957 			  I915_READ(PCH_PP_STATUS),
958 			  I915_READ(PCH_PP_CONTROL));
959 	}
960 }
961 
ironlake_wait_panel_on(struct intel_dp * intel_dp)962 static void ironlake_wait_panel_on(struct intel_dp *intel_dp)
963 {
964 	DRM_DEBUG_KMS("Wait for panel power on\n");
965 	ironlake_wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
966 }
967 
ironlake_wait_panel_off(struct intel_dp * intel_dp)968 static void ironlake_wait_panel_off(struct intel_dp *intel_dp)
969 {
970 	DRM_DEBUG_KMS("Wait for panel power off time\n");
971 	ironlake_wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
972 }
973 
ironlake_wait_panel_power_cycle(struct intel_dp * intel_dp)974 static void ironlake_wait_panel_power_cycle(struct intel_dp *intel_dp)
975 {
976 	DRM_DEBUG_KMS("Wait for panel power cycle\n");
977 	ironlake_wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
978 }
979 
980 
981 /* Read the current pp_control value, unlocking the register if it
982  * is locked
983  */
984 
ironlake_get_pp_control(struct drm_i915_private * dev_priv)985 static  u32 ironlake_get_pp_control(struct drm_i915_private *dev_priv)
986 {
987 	u32	control = I915_READ(PCH_PP_CONTROL);
988 
989 	control &= ~PANEL_UNLOCK_MASK;
990 	control |= PANEL_UNLOCK_REGS;
991 	return control;
992 }
993 
ironlake_edp_panel_vdd_on(struct intel_dp * intel_dp)994 static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp)
995 {
996 	struct drm_device *dev = intel_dp->base.base.dev;
997 	struct drm_i915_private *dev_priv = dev->dev_private;
998 	u32 pp;
999 
1000 	if (!is_edp(intel_dp))
1001 		return;
1002 	DRM_DEBUG_KMS("Turn eDP VDD on\n");
1003 
1004 	if (intel_dp->want_panel_vdd)
1005 		printf("eDP VDD already requested on\n");
1006 
1007 	intel_dp->want_panel_vdd = true;
1008 
1009 	if (ironlake_edp_have_panel_vdd(intel_dp)) {
1010 		DRM_DEBUG_KMS("eDP VDD already on\n");
1011 		return;
1012 	}
1013 
1014 	if (!ironlake_edp_have_panel_power(intel_dp))
1015 		ironlake_wait_panel_power_cycle(intel_dp);
1016 
1017 	pp = ironlake_get_pp_control(dev_priv);
1018 	pp |= EDP_FORCE_VDD;
1019 	I915_WRITE(PCH_PP_CONTROL, pp);
1020 	POSTING_READ(PCH_PP_CONTROL);
1021 	DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
1022 		      I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
1023 
1024 	/*
1025 	 * If the panel wasn't on, delay before accessing aux channel
1026 	 */
1027 	if (!ironlake_edp_have_panel_power(intel_dp)) {
1028 		DRM_DEBUG_KMS("eDP was not running\n");
1029 		drm_msleep(intel_dp->panel_power_up_delay, "915edpon");
1030 	}
1031 }
1032 
ironlake_panel_vdd_off_sync(struct intel_dp * intel_dp)1033 static void ironlake_panel_vdd_off_sync(struct intel_dp *intel_dp)
1034 {
1035 	struct drm_device *dev = intel_dp->base.base.dev;
1036 	struct drm_i915_private *dev_priv = dev->dev_private;
1037 	u32 pp;
1038 
1039 	if (!intel_dp->want_panel_vdd && ironlake_edp_have_panel_vdd(intel_dp)) {
1040 		pp = ironlake_get_pp_control(dev_priv);
1041 		pp &= ~EDP_FORCE_VDD;
1042 		I915_WRITE(PCH_PP_CONTROL, pp);
1043 		POSTING_READ(PCH_PP_CONTROL);
1044 
1045 		/* Make sure sequencer is idle before allowing subsequent activity */
1046 		DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
1047 			      I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
1048 
1049 		drm_msleep(intel_dp->panel_power_down_delay, "915vddo");
1050 	}
1051 }
1052 
ironlake_panel_vdd_work(void * arg,int pending __unused)1053 static void ironlake_panel_vdd_work(void *arg, int pending __unused)
1054 {
1055 	struct intel_dp *intel_dp = arg;
1056 	struct drm_device *dev = intel_dp->base.base.dev;
1057 
1058 	sx_xlock(&dev->mode_config.mutex);
1059 	ironlake_panel_vdd_off_sync(intel_dp);
1060 	sx_xunlock(&dev->mode_config.mutex);
1061 }
1062 
ironlake_edp_panel_vdd_off(struct intel_dp * intel_dp,bool sync)1063 static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
1064 {
1065 	if (!is_edp(intel_dp))
1066 		return;
1067 
1068 	DRM_DEBUG_KMS("Turn eDP VDD off %d\n", intel_dp->want_panel_vdd);
1069 	if (!intel_dp->want_panel_vdd)
1070 		printf("eDP VDD not forced on\n");
1071 
1072 	intel_dp->want_panel_vdd = false;
1073 
1074 	if (sync) {
1075 		ironlake_panel_vdd_off_sync(intel_dp);
1076 	} else {
1077 		/*
1078 		 * Queue the timer to fire a long
1079 		 * time from now (relative to the power down delay)
1080 		 * to keep the panel power up across a sequence of operations
1081 		 */
1082 		struct drm_i915_private *dev_priv = intel_dp->base.base.dev->dev_private;
1083 		taskqueue_enqueue_timeout(dev_priv->tq,
1084 		    &intel_dp->panel_vdd_task,
1085 		    msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5));
1086 	}
1087 }
1088 
ironlake_edp_panel_on(struct intel_dp * intel_dp)1089 static void ironlake_edp_panel_on(struct intel_dp *intel_dp)
1090 {
1091 	struct drm_device *dev = intel_dp->base.base.dev;
1092 	struct drm_i915_private *dev_priv = dev->dev_private;
1093 	u32 pp;
1094 
1095 	if (!is_edp(intel_dp))
1096 		return;
1097 
1098 	DRM_DEBUG_KMS("Turn eDP power on\n");
1099 
1100 	if (ironlake_edp_have_panel_power(intel_dp)) {
1101 		DRM_DEBUG_KMS("eDP power already on\n");
1102 		return;
1103 	}
1104 
1105 	ironlake_wait_panel_power_cycle(intel_dp);
1106 
1107 	pp = ironlake_get_pp_control(dev_priv);
1108 	if (IS_GEN5(dev)) {
1109 		/* ILK workaround: disable reset around power sequence */
1110 		pp &= ~PANEL_POWER_RESET;
1111 		I915_WRITE(PCH_PP_CONTROL, pp);
1112 		POSTING_READ(PCH_PP_CONTROL);
1113 	}
1114 
1115 	pp |= POWER_TARGET_ON;
1116 	if (!IS_GEN5(dev))
1117 		pp |= PANEL_POWER_RESET;
1118 
1119 	I915_WRITE(PCH_PP_CONTROL, pp);
1120 	POSTING_READ(PCH_PP_CONTROL);
1121 
1122 	ironlake_wait_panel_on(intel_dp);
1123 
1124 	if (IS_GEN5(dev)) {
1125 		pp |= PANEL_POWER_RESET; /* restore panel reset bit */
1126 		I915_WRITE(PCH_PP_CONTROL, pp);
1127 		POSTING_READ(PCH_PP_CONTROL);
1128 	}
1129 }
1130 
ironlake_edp_panel_off(struct intel_dp * intel_dp)1131 static void ironlake_edp_panel_off(struct intel_dp *intel_dp)
1132 {
1133 	struct drm_device *dev = intel_dp->base.base.dev;
1134 	struct drm_i915_private *dev_priv = dev->dev_private;
1135 	u32 pp;
1136 
1137 	if (!is_edp(intel_dp))
1138 		return;
1139 
1140 	DRM_DEBUG_KMS("Turn eDP power off\n");
1141 
1142 	if (intel_dp->want_panel_vdd)
1143 		printf("Cannot turn power off while VDD is on\n");
1144 	ironlake_panel_vdd_off_sync(intel_dp); /* finish any pending work */
1145 
1146 	pp = ironlake_get_pp_control(dev_priv);
1147 	pp &= ~(POWER_TARGET_ON | EDP_FORCE_VDD | PANEL_POWER_RESET | EDP_BLC_ENABLE);
1148 	I915_WRITE(PCH_PP_CONTROL, pp);
1149 	POSTING_READ(PCH_PP_CONTROL);
1150 
1151 	ironlake_wait_panel_off(intel_dp);
1152 }
1153 
ironlake_edp_backlight_on(struct intel_dp * intel_dp)1154 static void ironlake_edp_backlight_on(struct intel_dp *intel_dp)
1155 {
1156 	struct drm_device *dev = intel_dp->base.base.dev;
1157 	struct drm_i915_private *dev_priv = dev->dev_private;
1158 	u32 pp;
1159 
1160 	if (!is_edp(intel_dp))
1161 		return;
1162 
1163 	DRM_DEBUG_KMS("\n");
1164 	/*
1165 	 * If we enable the backlight right away following a panel power
1166 	 * on, we may see slight flicker as the panel syncs with the eDP
1167 	 * link.  So delay a bit to make sure the image is solid before
1168 	 * allowing it to appear.
1169 	 */
1170 	drm_msleep(intel_dp->backlight_on_delay, "915ebo");
1171 	pp = ironlake_get_pp_control(dev_priv);
1172 	pp |= EDP_BLC_ENABLE;
1173 	I915_WRITE(PCH_PP_CONTROL, pp);
1174 	POSTING_READ(PCH_PP_CONTROL);
1175 }
1176 
ironlake_edp_backlight_off(struct intel_dp * intel_dp)1177 static void ironlake_edp_backlight_off(struct intel_dp *intel_dp)
1178 {
1179 	struct drm_device *dev = intel_dp->base.base.dev;
1180 	struct drm_i915_private *dev_priv = dev->dev_private;
1181 	u32 pp;
1182 
1183 	if (!is_edp(intel_dp))
1184 		return;
1185 
1186 	DRM_DEBUG_KMS("\n");
1187 	pp = ironlake_get_pp_control(dev_priv);
1188 	pp &= ~EDP_BLC_ENABLE;
1189 	I915_WRITE(PCH_PP_CONTROL, pp);
1190 	POSTING_READ(PCH_PP_CONTROL);
1191 	drm_msleep(intel_dp->backlight_off_delay, "915bo1");
1192 }
1193 
ironlake_edp_pll_on(struct drm_encoder * encoder)1194 static void ironlake_edp_pll_on(struct drm_encoder *encoder)
1195 {
1196 	struct drm_device *dev = encoder->dev;
1197 	struct drm_i915_private *dev_priv = dev->dev_private;
1198 	u32 dpa_ctl;
1199 
1200 	DRM_DEBUG_KMS("\n");
1201 	dpa_ctl = I915_READ(DP_A);
1202 	dpa_ctl |= DP_PLL_ENABLE;
1203 	I915_WRITE(DP_A, dpa_ctl);
1204 	POSTING_READ(DP_A);
1205 	DELAY(200);
1206 }
1207 
ironlake_edp_pll_off(struct drm_encoder * encoder)1208 static void ironlake_edp_pll_off(struct drm_encoder *encoder)
1209 {
1210 	struct drm_device *dev = encoder->dev;
1211 	struct drm_i915_private *dev_priv = dev->dev_private;
1212 	u32 dpa_ctl;
1213 
1214 	dpa_ctl = I915_READ(DP_A);
1215 	dpa_ctl &= ~DP_PLL_ENABLE;
1216 	I915_WRITE(DP_A, dpa_ctl);
1217 	POSTING_READ(DP_A);
1218 	DELAY(200);
1219 }
1220 
1221 /* If the sink supports it, try to set the power state appropriately */
intel_dp_sink_dpms(struct intel_dp * intel_dp,int mode)1222 static void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
1223 {
1224 	int ret, i;
1225 
1226 	/* Should have a valid DPCD by this point */
1227 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
1228 		return;
1229 
1230 	if (mode != DRM_MODE_DPMS_ON) {
1231 		ret = intel_dp_aux_native_write_1(intel_dp, DP_SET_POWER,
1232 						  DP_SET_POWER_D3);
1233 		if (ret != 1)
1234 			DRM_DEBUG("failed to write sink power state\n");
1235 	} else {
1236 		/*
1237 		 * When turning on, we need to retry for 1ms to give the sink
1238 		 * time to wake up.
1239 		 */
1240 		for (i = 0; i < 3; i++) {
1241 			ret = intel_dp_aux_native_write_1(intel_dp,
1242 							  DP_SET_POWER,
1243 							  DP_SET_POWER_D0);
1244 			if (ret == 1)
1245 				break;
1246 			drm_msleep(1, "915dps");
1247 		}
1248 	}
1249 }
1250 
intel_dp_prepare(struct drm_encoder * encoder)1251 static void intel_dp_prepare(struct drm_encoder *encoder)
1252 {
1253 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1254 
1255 	ironlake_edp_backlight_off(intel_dp);
1256 	ironlake_edp_panel_off(intel_dp);
1257 
1258 	/* Wake up the sink first */
1259 	ironlake_edp_panel_vdd_on(intel_dp);
1260 	intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
1261 	intel_dp_link_down(intel_dp);
1262 	ironlake_edp_panel_vdd_off(intel_dp, false);
1263 
1264 	/* Make sure the panel is off before trying to
1265 	 * change the mode
1266 	 */
1267 }
1268 
intel_dp_commit(struct drm_encoder * encoder)1269 static void intel_dp_commit(struct drm_encoder *encoder)
1270 {
1271 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1272 	struct drm_device *dev = encoder->dev;
1273 	struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
1274 
1275 	ironlake_edp_panel_vdd_on(intel_dp);
1276 	intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
1277 	intel_dp_start_link_train(intel_dp);
1278 	ironlake_edp_panel_on(intel_dp);
1279 	ironlake_edp_panel_vdd_off(intel_dp, true);
1280 	intel_dp_complete_link_train(intel_dp);
1281 	ironlake_edp_backlight_on(intel_dp);
1282 
1283 	intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
1284 
1285 	if (HAS_PCH_CPT(dev))
1286 		intel_cpt_verify_modeset(dev, intel_crtc->pipe);
1287 }
1288 
1289 static void
intel_dp_dpms(struct drm_encoder * encoder,int mode)1290 intel_dp_dpms(struct drm_encoder *encoder, int mode)
1291 {
1292 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1293 	struct drm_device *dev = encoder->dev;
1294 	struct drm_i915_private *dev_priv = dev->dev_private;
1295 	uint32_t dp_reg = I915_READ(intel_dp->output_reg);
1296 
1297 	if (mode != DRM_MODE_DPMS_ON) {
1298 		ironlake_edp_backlight_off(intel_dp);
1299 		ironlake_edp_panel_off(intel_dp);
1300 
1301 		ironlake_edp_panel_vdd_on(intel_dp);
1302 		intel_dp_sink_dpms(intel_dp, mode);
1303 		intel_dp_link_down(intel_dp);
1304 		ironlake_edp_panel_vdd_off(intel_dp, false);
1305 
1306 		if (is_cpu_edp(intel_dp))
1307 			ironlake_edp_pll_off(encoder);
1308 	} else {
1309 		if (is_cpu_edp(intel_dp))
1310 			ironlake_edp_pll_on(encoder);
1311 
1312 		ironlake_edp_panel_vdd_on(intel_dp);
1313 		intel_dp_sink_dpms(intel_dp, mode);
1314 		if (!(dp_reg & DP_PORT_EN)) {
1315 			intel_dp_start_link_train(intel_dp);
1316 			ironlake_edp_panel_on(intel_dp);
1317 			ironlake_edp_panel_vdd_off(intel_dp, true);
1318 			intel_dp_complete_link_train(intel_dp);
1319 		} else
1320 			ironlake_edp_panel_vdd_off(intel_dp, false);
1321 		ironlake_edp_backlight_on(intel_dp);
1322 	}
1323 	intel_dp->dpms_mode = mode;
1324 }
1325 /*
1326  * Native read with retry for link status and receiver capability reads for
1327  * cases where the sink may still be asleep.
1328  */
1329 static bool
intel_dp_aux_native_read_retry(struct intel_dp * intel_dp,uint16_t address,uint8_t * recv,int recv_bytes)1330 intel_dp_aux_native_read_retry(struct intel_dp *intel_dp, uint16_t address,
1331 			       uint8_t *recv, int recv_bytes)
1332 {
1333 	int ret, i;
1334 
1335 	/*
1336 	 * Sinks are *supposed* to come up within 1ms from an off state,
1337 	 * but we're also supposed to retry 3 times per the spec.
1338 	 */
1339 	for (i = 0; i < 3; i++) {
1340 		ret = intel_dp_aux_native_read(intel_dp, address, recv,
1341 					       recv_bytes);
1342 		if (ret == recv_bytes)
1343 			return true;
1344 		drm_msleep(1, "915dpl");
1345 	}
1346 
1347 	return false;
1348 }
1349 
1350 /*
1351  * Fetch AUX CH registers 0x202 - 0x207 which contain
1352  * link status information
1353  */
1354 static bool
intel_dp_get_link_status(struct intel_dp * intel_dp,uint8_t link_status[DP_LINK_STATUS_SIZE])1355 intel_dp_get_link_status(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
1356 {
1357 	return intel_dp_aux_native_read_retry(intel_dp,
1358 					      DP_LANE0_1_STATUS,
1359 					      link_status,
1360 					      DP_LINK_STATUS_SIZE);
1361 }
1362 
1363 static uint8_t
intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],int r)1364 intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1365 		     int r)
1366 {
1367 	return link_status[r - DP_LANE0_1_STATUS];
1368 }
1369 
1370 static uint8_t
intel_get_adjust_request_voltage(uint8_t adjust_request[2],int lane)1371 intel_get_adjust_request_voltage(uint8_t adjust_request[2],
1372 				 int lane)
1373 {
1374 	int	    s = ((lane & 1) ?
1375 			 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1376 			 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1377 	uint8_t l = adjust_request[lane>>1];
1378 
1379 	return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1380 }
1381 
1382 static uint8_t
intel_get_adjust_request_pre_emphasis(uint8_t adjust_request[2],int lane)1383 intel_get_adjust_request_pre_emphasis(uint8_t adjust_request[2],
1384 				      int lane)
1385 {
1386 	int	    s = ((lane & 1) ?
1387 			 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1388 			 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1389 	uint8_t l = adjust_request[lane>>1];
1390 
1391 	return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1392 }
1393 
1394 
1395 #if 0
1396 static char	*voltage_names[] = {
1397 	"0.4V", "0.6V", "0.8V", "1.2V"
1398 };
1399 static char	*pre_emph_names[] = {
1400 	"0dB", "3.5dB", "6dB", "9.5dB"
1401 };
1402 static char	*link_train_names[] = {
1403 	"pattern 1", "pattern 2", "idle", "off"
1404 };
1405 #endif
1406 
1407 /*
1408  * These are source-specific values; current Intel hardware supports
1409  * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1410  */
1411 
1412 static uint8_t
intel_dp_voltage_max(struct intel_dp * intel_dp)1413 intel_dp_voltage_max(struct intel_dp *intel_dp)
1414 {
1415 	struct drm_device *dev = intel_dp->base.base.dev;
1416 
1417 	if (IS_GEN7(dev) && is_cpu_edp(intel_dp))
1418 		return DP_TRAIN_VOLTAGE_SWING_800;
1419 	else if (HAS_PCH_CPT(dev) && !is_cpu_edp(intel_dp))
1420 		return DP_TRAIN_VOLTAGE_SWING_1200;
1421 	else
1422 		return DP_TRAIN_VOLTAGE_SWING_800;
1423 }
1424 
1425 static uint8_t
intel_dp_pre_emphasis_max(struct intel_dp * intel_dp,uint8_t voltage_swing)1426 intel_dp_pre_emphasis_max(struct intel_dp *intel_dp, uint8_t voltage_swing)
1427 {
1428 	struct drm_device *dev = intel_dp->base.base.dev;
1429 
1430 	if (IS_GEN7(dev) && is_cpu_edp(intel_dp)) {
1431 		switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1432 		case DP_TRAIN_VOLTAGE_SWING_400:
1433 			return DP_TRAIN_PRE_EMPHASIS_6;
1434 		case DP_TRAIN_VOLTAGE_SWING_600:
1435 		case DP_TRAIN_VOLTAGE_SWING_800:
1436 			return DP_TRAIN_PRE_EMPHASIS_3_5;
1437 		default:
1438 			return DP_TRAIN_PRE_EMPHASIS_0;
1439 		}
1440 	} else {
1441 		switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1442 		case DP_TRAIN_VOLTAGE_SWING_400:
1443 			return DP_TRAIN_PRE_EMPHASIS_6;
1444 		case DP_TRAIN_VOLTAGE_SWING_600:
1445 			return DP_TRAIN_PRE_EMPHASIS_6;
1446 		case DP_TRAIN_VOLTAGE_SWING_800:
1447 			return DP_TRAIN_PRE_EMPHASIS_3_5;
1448 		case DP_TRAIN_VOLTAGE_SWING_1200:
1449 		default:
1450 			return DP_TRAIN_PRE_EMPHASIS_0;
1451 		}
1452 	}
1453 }
1454 
1455 static void
intel_get_adjust_train(struct intel_dp * intel_dp,uint8_t link_status[DP_LINK_STATUS_SIZE])1456 intel_get_adjust_train(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
1457 {
1458 	uint8_t v = 0;
1459 	uint8_t p = 0;
1460 	int lane;
1461 	uint8_t	*adjust_request = link_status + (DP_ADJUST_REQUEST_LANE0_1 - DP_LANE0_1_STATUS);
1462 	uint8_t voltage_max;
1463 	uint8_t preemph_max;
1464 
1465 	for (lane = 0; lane < intel_dp->lane_count; lane++) {
1466 		uint8_t this_v = intel_get_adjust_request_voltage(adjust_request, lane);
1467 		uint8_t this_p = intel_get_adjust_request_pre_emphasis(adjust_request, lane);
1468 
1469 		if (this_v > v)
1470 			v = this_v;
1471 		if (this_p > p)
1472 			p = this_p;
1473 	}
1474 
1475 	voltage_max = intel_dp_voltage_max(intel_dp);
1476 	if (v >= voltage_max)
1477 		v = voltage_max | DP_TRAIN_MAX_SWING_REACHED;
1478 
1479 	preemph_max = intel_dp_pre_emphasis_max(intel_dp, v);
1480 	if (p >= preemph_max)
1481 		p = preemph_max | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1482 
1483 	for (lane = 0; lane < 4; lane++)
1484 		intel_dp->train_set[lane] = v | p;
1485 }
1486 
1487 static uint32_t
intel_dp_signal_levels(uint8_t train_set)1488 intel_dp_signal_levels(uint8_t train_set)
1489 {
1490 	uint32_t	signal_levels = 0;
1491 
1492 	switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
1493 	case DP_TRAIN_VOLTAGE_SWING_400:
1494 	default:
1495 		signal_levels |= DP_VOLTAGE_0_4;
1496 		break;
1497 	case DP_TRAIN_VOLTAGE_SWING_600:
1498 		signal_levels |= DP_VOLTAGE_0_6;
1499 		break;
1500 	case DP_TRAIN_VOLTAGE_SWING_800:
1501 		signal_levels |= DP_VOLTAGE_0_8;
1502 		break;
1503 	case DP_TRAIN_VOLTAGE_SWING_1200:
1504 		signal_levels |= DP_VOLTAGE_1_2;
1505 		break;
1506 	}
1507 	switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
1508 	case DP_TRAIN_PRE_EMPHASIS_0:
1509 	default:
1510 		signal_levels |= DP_PRE_EMPHASIS_0;
1511 		break;
1512 	case DP_TRAIN_PRE_EMPHASIS_3_5:
1513 		signal_levels |= DP_PRE_EMPHASIS_3_5;
1514 		break;
1515 	case DP_TRAIN_PRE_EMPHASIS_6:
1516 		signal_levels |= DP_PRE_EMPHASIS_6;
1517 		break;
1518 	case DP_TRAIN_PRE_EMPHASIS_9_5:
1519 		signal_levels |= DP_PRE_EMPHASIS_9_5;
1520 		break;
1521 	}
1522 	return signal_levels;
1523 }
1524 
1525 /* Gen6's DP voltage swing and pre-emphasis control */
1526 static uint32_t
intel_gen6_edp_signal_levels(uint8_t train_set)1527 intel_gen6_edp_signal_levels(uint8_t train_set)
1528 {
1529 	int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1530 					 DP_TRAIN_PRE_EMPHASIS_MASK);
1531 	switch (signal_levels) {
1532 	case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1533 	case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
1534 		return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
1535 	case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
1536 		return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
1537 	case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1538 	case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
1539 		return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
1540 	case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1541 	case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
1542 		return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
1543 	case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1544 	case DP_TRAIN_VOLTAGE_SWING_1200 | DP_TRAIN_PRE_EMPHASIS_0:
1545 		return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
1546 	default:
1547 		DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
1548 			      "0x%x\n", signal_levels);
1549 		return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
1550 	}
1551 }
1552 
1553 /* Gen7's DP voltage swing and pre-emphasis control */
1554 static uint32_t
intel_gen7_edp_signal_levels(uint8_t train_set)1555 intel_gen7_edp_signal_levels(uint8_t train_set)
1556 {
1557 	int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1558 					 DP_TRAIN_PRE_EMPHASIS_MASK);
1559 	switch (signal_levels) {
1560 	case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1561 		return EDP_LINK_TRAIN_400MV_0DB_IVB;
1562 	case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
1563 		return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
1564 	case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1565 		return EDP_LINK_TRAIN_400MV_6DB_IVB;
1566 
1567 	case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
1568 		return EDP_LINK_TRAIN_600MV_0DB_IVB;
1569 	case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1570 		return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
1571 
1572 	case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1573 		return EDP_LINK_TRAIN_800MV_0DB_IVB;
1574 	case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
1575 		return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
1576 
1577 	default:
1578 		DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
1579 			      "0x%x\n", signal_levels);
1580 		return EDP_LINK_TRAIN_500MV_0DB_IVB;
1581 	}
1582 }
1583 
1584 static uint8_t
intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],int lane)1585 intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1586 		      int lane)
1587 {
1588 	int s = (lane & 1) * 4;
1589 	uint8_t l = link_status[lane>>1];
1590 
1591 	return (l >> s) & 0xf;
1592 }
1593 
1594 /* Check for clock recovery is done on all channels */
1595 static bool
intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE],int lane_count)1596 intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1597 {
1598 	int lane;
1599 	uint8_t lane_status;
1600 
1601 	for (lane = 0; lane < lane_count; lane++) {
1602 		lane_status = intel_get_lane_status(link_status, lane);
1603 		if ((lane_status & DP_LANE_CR_DONE) == 0)
1604 			return false;
1605 	}
1606 	return true;
1607 }
1608 
1609 /* Check to see if channel eq is done on all channels */
1610 #define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1611 			 DP_LANE_CHANNEL_EQ_DONE|\
1612 			 DP_LANE_SYMBOL_LOCKED)
1613 static bool
intel_channel_eq_ok(struct intel_dp * intel_dp,uint8_t link_status[DP_LINK_STATUS_SIZE])1614 intel_channel_eq_ok(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
1615 {
1616 	uint8_t lane_align;
1617 	uint8_t lane_status;
1618 	int lane;
1619 
1620 	lane_align = intel_dp_link_status(link_status,
1621 					  DP_LANE_ALIGN_STATUS_UPDATED);
1622 	if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1623 		return false;
1624 	for (lane = 0; lane < intel_dp->lane_count; lane++) {
1625 		lane_status = intel_get_lane_status(link_status, lane);
1626 		if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1627 			return false;
1628 	}
1629 	return true;
1630 }
1631 
1632 static bool
intel_dp_set_link_train(struct intel_dp * intel_dp,uint32_t dp_reg_value,uint8_t dp_train_pat)1633 intel_dp_set_link_train(struct intel_dp *intel_dp,
1634 			uint32_t dp_reg_value,
1635 			uint8_t dp_train_pat)
1636 {
1637 	struct drm_device *dev = intel_dp->base.base.dev;
1638 	struct drm_i915_private *dev_priv = dev->dev_private;
1639 	int ret;
1640 
1641 	I915_WRITE(intel_dp->output_reg, dp_reg_value);
1642 	POSTING_READ(intel_dp->output_reg);
1643 
1644 	intel_dp_aux_native_write_1(intel_dp,
1645 				    DP_TRAINING_PATTERN_SET,
1646 				    dp_train_pat);
1647 
1648 	ret = intel_dp_aux_native_write(intel_dp,
1649 					DP_TRAINING_LANE0_SET,
1650 					intel_dp->train_set,
1651 					intel_dp->lane_count);
1652 	if (ret != intel_dp->lane_count)
1653 		return false;
1654 
1655 	return true;
1656 }
1657 
1658 /* Enable corresponding port and start training pattern 1 */
1659 static void
intel_dp_start_link_train(struct intel_dp * intel_dp)1660 intel_dp_start_link_train(struct intel_dp *intel_dp)
1661 {
1662 	struct drm_device *dev = intel_dp->base.base.dev;
1663 	struct drm_i915_private *dev_priv = dev->dev_private;
1664 	struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
1665 	int i;
1666 	uint8_t voltage;
1667 	bool clock_recovery = false;
1668 	int voltage_tries, loop_tries;
1669 	u32 reg;
1670 	uint32_t DP = intel_dp->DP;
1671 
1672 	/* Enable output, wait for it to become active */
1673 	I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1674 	POSTING_READ(intel_dp->output_reg);
1675 	intel_wait_for_vblank(dev, intel_crtc->pipe);
1676 
1677 	/* Write the link configuration data */
1678 	intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1679 				  intel_dp->link_configuration,
1680 				  DP_LINK_CONFIGURATION_SIZE);
1681 
1682 	DP |= DP_PORT_EN;
1683 
1684 	if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp)))
1685 		DP &= ~DP_LINK_TRAIN_MASK_CPT;
1686 	else
1687 		DP &= ~DP_LINK_TRAIN_MASK;
1688 	memset(intel_dp->train_set, 0, 4);
1689 	voltage = 0xff;
1690 	voltage_tries = 0;
1691 	loop_tries = 0;
1692 	clock_recovery = false;
1693 	for (;;) {
1694 		/* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
1695 		uint8_t	    link_status[DP_LINK_STATUS_SIZE];
1696 		uint32_t    signal_levels;
1697 
1698 
1699 		if (IS_GEN7(dev) && is_cpu_edp(intel_dp)) {
1700 			signal_levels = intel_gen7_edp_signal_levels(intel_dp->train_set[0]);
1701 			DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_IVB) | signal_levels;
1702 		} else if (IS_GEN6(dev) && is_cpu_edp(intel_dp)) {
1703 			signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
1704 			DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1705 		} else {
1706 			signal_levels = intel_dp_signal_levels(intel_dp->train_set[0]);
1707 			DRM_DEBUG_KMS("training pattern 1 signal levels %08x\n", signal_levels);
1708 			DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1709 		}
1710 
1711 		if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp)))
1712 			reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1713 		else
1714 			reg = DP | DP_LINK_TRAIN_PAT_1;
1715 
1716 		if (!intel_dp_set_link_train(intel_dp, reg,
1717 					     DP_TRAINING_PATTERN_1))
1718 			break;
1719 		/* Set training pattern 1 */
1720 
1721 		DELAY(100);
1722 		if (!intel_dp_get_link_status(intel_dp, link_status)) {
1723 			DRM_ERROR("failed to get link status\n");
1724 			break;
1725 		}
1726 
1727 		if (intel_clock_recovery_ok(link_status, intel_dp->lane_count)) {
1728 			DRM_DEBUG_KMS("clock recovery OK\n");
1729 			clock_recovery = true;
1730 			break;
1731 		}
1732 
1733 		/* Check to see if we've tried the max voltage */
1734 		for (i = 0; i < intel_dp->lane_count; i++)
1735 			if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1736 				break;
1737 		if (i == intel_dp->lane_count) {
1738 			++loop_tries;
1739 			if (loop_tries == 5) {
1740 				DRM_DEBUG_KMS("too many full retries, give up\n");
1741 				break;
1742 			}
1743 			memset(intel_dp->train_set, 0, 4);
1744 			voltage_tries = 0;
1745 			continue;
1746 		}
1747 
1748 		/* Check to see if we've tried the same voltage 5 times */
1749 		if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1750 			++voltage_tries;
1751 			if (voltage_tries == 5) {
1752 				DRM_DEBUG_KMS("too many voltage retries, give up\n");
1753 				break;
1754 			}
1755 		} else
1756 			voltage_tries = 0;
1757 		voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1758 
1759 		/* Compute new intel_dp->train_set as requested by target */
1760 		intel_get_adjust_train(intel_dp, link_status);
1761 	}
1762 
1763 	intel_dp->DP = DP;
1764 }
1765 
1766 static void
intel_dp_complete_link_train(struct intel_dp * intel_dp)1767 intel_dp_complete_link_train(struct intel_dp *intel_dp)
1768 {
1769 	struct drm_device *dev = intel_dp->base.base.dev;
1770 	struct drm_i915_private *dev_priv = dev->dev_private;
1771 	bool channel_eq = false;
1772 	int tries, cr_tries;
1773 	u32 reg;
1774 	uint32_t DP = intel_dp->DP;
1775 
1776 	/* channel equalization */
1777 	tries = 0;
1778 	cr_tries = 0;
1779 	channel_eq = false;
1780 	for (;;) {
1781 		/* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
1782 		uint32_t    signal_levels;
1783 		uint8_t	    link_status[DP_LINK_STATUS_SIZE];
1784 
1785 		if (cr_tries > 5) {
1786 			DRM_ERROR("failed to train DP, aborting\n");
1787 			intel_dp_link_down(intel_dp);
1788 			break;
1789 		}
1790 
1791 		if (IS_GEN7(dev) && is_cpu_edp(intel_dp)) {
1792 			signal_levels = intel_gen7_edp_signal_levels(intel_dp->train_set[0]);
1793 			DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_IVB) | signal_levels;
1794 		} else if (IS_GEN6(dev) && is_cpu_edp(intel_dp)) {
1795 			signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
1796 			DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1797 		} else {
1798 			signal_levels = intel_dp_signal_levels(intel_dp->train_set[0]);
1799 			DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1800 		}
1801 
1802 		if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp)))
1803 			reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1804 		else
1805 			reg = DP | DP_LINK_TRAIN_PAT_2;
1806 
1807 		/* channel eq pattern */
1808 		if (!intel_dp_set_link_train(intel_dp, reg,
1809 					     DP_TRAINING_PATTERN_2))
1810 			break;
1811 
1812 		DELAY(400);
1813 		if (!intel_dp_get_link_status(intel_dp, link_status))
1814 			break;
1815 
1816 		/* Make sure clock is still ok */
1817 		if (!intel_clock_recovery_ok(link_status, intel_dp->lane_count)) {
1818 			intel_dp_start_link_train(intel_dp);
1819 			cr_tries++;
1820 			continue;
1821 		}
1822 
1823 		if (intel_channel_eq_ok(intel_dp, link_status)) {
1824 			channel_eq = true;
1825 			break;
1826 		}
1827 
1828 		/* Try 5 times, then try clock recovery if that fails */
1829 		if (tries > 5) {
1830 			intel_dp_link_down(intel_dp);
1831 			intel_dp_start_link_train(intel_dp);
1832 			tries = 0;
1833 			cr_tries++;
1834 			continue;
1835 		}
1836 
1837 		/* Compute new intel_dp->train_set as requested by target */
1838 		intel_get_adjust_train(intel_dp, link_status);
1839 		++tries;
1840 	}
1841 
1842 	if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp)))
1843 		reg = DP | DP_LINK_TRAIN_OFF_CPT;
1844 	else
1845 		reg = DP | DP_LINK_TRAIN_OFF;
1846 
1847 	I915_WRITE(intel_dp->output_reg, reg);
1848 	POSTING_READ(intel_dp->output_reg);
1849 	intel_dp_aux_native_write_1(intel_dp,
1850 				    DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1851 }
1852 
1853 static void
intel_dp_link_down(struct intel_dp * intel_dp)1854 intel_dp_link_down(struct intel_dp *intel_dp)
1855 {
1856 	struct drm_device *dev = intel_dp->base.base.dev;
1857 	struct drm_i915_private *dev_priv = dev->dev_private;
1858 	uint32_t DP = intel_dp->DP;
1859 
1860 	if ((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0)
1861 		return;
1862 
1863 	DRM_DEBUG_KMS("\n");
1864 
1865 	if (is_edp(intel_dp)) {
1866 		DP &= ~DP_PLL_ENABLE;
1867 		I915_WRITE(intel_dp->output_reg, DP);
1868 		POSTING_READ(intel_dp->output_reg);
1869 		DELAY(100);
1870 	}
1871 
1872 	if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp))) {
1873 		DP &= ~DP_LINK_TRAIN_MASK_CPT;
1874 		I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
1875 	} else {
1876 		DP &= ~DP_LINK_TRAIN_MASK;
1877 		I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
1878 	}
1879 	POSTING_READ(intel_dp->output_reg);
1880 
1881 	drm_msleep(17, "915dlo");
1882 
1883 	if (is_edp(intel_dp)) {
1884 		if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp)))
1885 			DP |= DP_LINK_TRAIN_OFF_CPT;
1886 		else
1887 			DP |= DP_LINK_TRAIN_OFF;
1888 	}
1889 
1890 
1891 	if (!HAS_PCH_CPT(dev) &&
1892 	    I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
1893 		struct drm_crtc *crtc = intel_dp->base.base.crtc;
1894 
1895 		/* Hardware workaround: leaving our transcoder select
1896 		 * set to transcoder B while it's off will prevent the
1897 		 * corresponding HDMI output on transcoder A.
1898 		 *
1899 		 * Combine this with another hardware workaround:
1900 		 * transcoder select bit can only be cleared while the
1901 		 * port is enabled.
1902 		 */
1903 		DP &= ~DP_PIPEB_SELECT;
1904 		I915_WRITE(intel_dp->output_reg, DP);
1905 
1906 		/* Changes to enable or select take place the vblank
1907 		 * after being written.
1908 		 */
1909 		if (crtc == NULL) {
1910 			/* We can arrive here never having been attached
1911 			 * to a CRTC, for instance, due to inheriting
1912 			 * random state from the BIOS.
1913 			 *
1914 			 * If the pipe is not running, play safe and
1915 			 * wait for the clocks to stabilise before
1916 			 * continuing.
1917 			 */
1918 			POSTING_READ(intel_dp->output_reg);
1919 			drm_msleep(50, "915dla");
1920 		} else
1921 			intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
1922 	}
1923 
1924 	DP &= ~DP_AUDIO_OUTPUT_ENABLE;
1925 	I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1926 	POSTING_READ(intel_dp->output_reg);
1927 	drm_msleep(intel_dp->panel_power_down_delay, "915ldo");
1928 }
1929 
1930 static bool
intel_dp_get_dpcd(struct intel_dp * intel_dp)1931 intel_dp_get_dpcd(struct intel_dp *intel_dp)
1932 {
1933 	if (intel_dp_aux_native_read_retry(intel_dp, 0x000, intel_dp->dpcd,
1934 					   sizeof(intel_dp->dpcd)) &&
1935 	    (intel_dp->dpcd[DP_DPCD_REV] != 0)) {
1936 		return true;
1937 	}
1938 
1939 	return false;
1940 }
1941 
1942 static void
intel_dp_probe_oui(struct intel_dp * intel_dp)1943 intel_dp_probe_oui(struct intel_dp *intel_dp)
1944 {
1945 	u8 buf[3];
1946 
1947 	if (!(intel_dp->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
1948 		return;
1949 
1950 	if (intel_dp_aux_native_read_retry(intel_dp, DP_SINK_OUI, buf, 3))
1951 		DRM_DEBUG_KMS("Sink OUI: %02x%02x%02x\n",
1952 			      buf[0], buf[1], buf[2]);
1953 
1954 	if (intel_dp_aux_native_read_retry(intel_dp, DP_BRANCH_OUI, buf, 3))
1955 		DRM_DEBUG_KMS("Branch OUI: %02x%02x%02x\n",
1956 			      buf[0], buf[1], buf[2]);
1957 }
1958 
1959 static bool
intel_dp_get_sink_irq(struct intel_dp * intel_dp,u8 * sink_irq_vector)1960 intel_dp_get_sink_irq(struct intel_dp *intel_dp, u8 *sink_irq_vector)
1961 {
1962 	int ret;
1963 
1964 	ret = intel_dp_aux_native_read_retry(intel_dp,
1965 					     DP_DEVICE_SERVICE_IRQ_VECTOR,
1966 					     sink_irq_vector, 1);
1967 	if (!ret)
1968 		return false;
1969 
1970 	return true;
1971 }
1972 
1973 static void
intel_dp_handle_test_request(struct intel_dp * intel_dp)1974 intel_dp_handle_test_request(struct intel_dp *intel_dp)
1975 {
1976 	/* NAK by default */
1977 	intel_dp_aux_native_write_1(intel_dp, DP_TEST_RESPONSE, DP_TEST_ACK);
1978 }
1979 
1980 /*
1981  * According to DP spec
1982  * 5.1.2:
1983  *  1. Read DPCD
1984  *  2. Configure link according to Receiver Capabilities
1985  *  3. Use Link Training from 2.5.3.3 and 3.5.1.3
1986  *  4. Check link status on receipt of hot-plug interrupt
1987  */
1988 
1989 static void
intel_dp_check_link_status(struct intel_dp * intel_dp)1990 intel_dp_check_link_status(struct intel_dp *intel_dp)
1991 {
1992 	u8 sink_irq_vector;
1993 	u8 link_status[DP_LINK_STATUS_SIZE];
1994 
1995 	if (intel_dp->dpms_mode != DRM_MODE_DPMS_ON)
1996 		return;
1997 
1998 	if (!intel_dp->base.base.crtc)
1999 		return;
2000 
2001 	/* Try to read receiver status if the link appears to be up */
2002 	if (!intel_dp_get_link_status(intel_dp, link_status)) {
2003 		intel_dp_link_down(intel_dp);
2004 		return;
2005 	}
2006 
2007 	/* Now read the DPCD to see if it's actually running */
2008 	if (!intel_dp_get_dpcd(intel_dp)) {
2009 		intel_dp_link_down(intel_dp);
2010 		return;
2011 	}
2012 
2013 	/* Try to read the source of the interrupt */
2014 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
2015 	    intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
2016 		/* Clear interrupt source */
2017 		intel_dp_aux_native_write_1(intel_dp,
2018 					    DP_DEVICE_SERVICE_IRQ_VECTOR,
2019 					    sink_irq_vector);
2020 
2021 		if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
2022 			intel_dp_handle_test_request(intel_dp);
2023 		if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
2024 			DRM_DEBUG_KMS("CP or sink specific irq unhandled\n");
2025 	}
2026 
2027 	if (!intel_channel_eq_ok(intel_dp, link_status)) {
2028 		DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
2029 			      drm_get_encoder_name(&intel_dp->base.base));
2030  		intel_dp_start_link_train(intel_dp);
2031 		intel_dp_complete_link_train(intel_dp);
2032 	}
2033 }
2034 
2035 static enum drm_connector_status
intel_dp_detect_dpcd(struct intel_dp * intel_dp)2036 intel_dp_detect_dpcd(struct intel_dp *intel_dp)
2037 {
2038 	if (intel_dp_get_dpcd(intel_dp))
2039 		return connector_status_connected;
2040 	return connector_status_disconnected;
2041 }
2042 
2043 static enum drm_connector_status
ironlake_dp_detect(struct intel_dp * intel_dp)2044 ironlake_dp_detect(struct intel_dp *intel_dp)
2045 {
2046 	enum drm_connector_status status;
2047 
2048 	/* Can't disconnect eDP, but you can close the lid... */
2049 	if (is_edp(intel_dp)) {
2050 		status = intel_panel_detect(intel_dp->base.base.dev);
2051 		if (status == connector_status_unknown)
2052 			status = connector_status_connected;
2053 		return status;
2054 	}
2055 
2056 	return intel_dp_detect_dpcd(intel_dp);
2057 }
2058 
2059 static enum drm_connector_status
g4x_dp_detect(struct intel_dp * intel_dp)2060 g4x_dp_detect(struct intel_dp *intel_dp)
2061 {
2062 	struct drm_device *dev = intel_dp->base.base.dev;
2063 	struct drm_i915_private *dev_priv = dev->dev_private;
2064 	uint32_t temp, bit;
2065 
2066 	switch (intel_dp->output_reg) {
2067 	case DP_B:
2068 		bit = DPB_HOTPLUG_INT_STATUS;
2069 		break;
2070 	case DP_C:
2071 		bit = DPC_HOTPLUG_INT_STATUS;
2072 		break;
2073 	case DP_D:
2074 		bit = DPD_HOTPLUG_INT_STATUS;
2075 		break;
2076 	default:
2077 		return connector_status_unknown;
2078 	}
2079 
2080 	temp = I915_READ(PORT_HOTPLUG_STAT);
2081 
2082 	if ((temp & bit) == 0)
2083 		return connector_status_disconnected;
2084 
2085 	return intel_dp_detect_dpcd(intel_dp);
2086 }
2087 
2088 static struct edid *
intel_dp_get_edid(struct drm_connector * connector,device_t adapter)2089 intel_dp_get_edid(struct drm_connector *connector, device_t adapter)
2090 {
2091 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2092 	struct edid	*edid;
2093 
2094 	ironlake_edp_panel_vdd_on(intel_dp);
2095 	edid = drm_get_edid(connector, adapter);
2096 	ironlake_edp_panel_vdd_off(intel_dp, false);
2097 	return edid;
2098 }
2099 
2100 static int
intel_dp_get_edid_modes(struct drm_connector * connector,device_t adapter)2101 intel_dp_get_edid_modes(struct drm_connector *connector, device_t adapter)
2102 {
2103 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2104 	int	ret;
2105 
2106 	ironlake_edp_panel_vdd_on(intel_dp);
2107 	ret = intel_ddc_get_modes(connector, adapter);
2108 	ironlake_edp_panel_vdd_off(intel_dp, false);
2109 	return ret;
2110 }
2111 
2112 
2113 /**
2114  * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
2115  *
2116  * \return true if DP port is connected.
2117  * \return false if DP port is disconnected.
2118  */
2119 static enum drm_connector_status
intel_dp_detect(struct drm_connector * connector,bool force)2120 intel_dp_detect(struct drm_connector *connector, bool force)
2121 {
2122 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2123 	struct drm_device *dev = intel_dp->base.base.dev;
2124 	enum drm_connector_status status;
2125 	struct edid *edid = NULL;
2126 
2127 	intel_dp->has_audio = false;
2128 
2129 	if (HAS_PCH_SPLIT(dev))
2130 		status = ironlake_dp_detect(intel_dp);
2131 	else
2132 		status = g4x_dp_detect(intel_dp);
2133 	if (status != connector_status_connected)
2134 		return status;
2135 
2136 	intel_dp_probe_oui(intel_dp);
2137 
2138 	if (intel_dp->force_audio != HDMI_AUDIO_AUTO) {
2139 		intel_dp->has_audio = (intel_dp->force_audio == HDMI_AUDIO_ON);
2140 	} else {
2141 		edid = intel_dp_get_edid(connector, intel_dp->adapter);
2142 		if (edid) {
2143 			intel_dp->has_audio = drm_detect_monitor_audio(edid);
2144 			free(edid, DRM_MEM_KMS);
2145 		}
2146 	}
2147 
2148 	return connector_status_connected;
2149 }
2150 
intel_dp_get_modes(struct drm_connector * connector)2151 static int intel_dp_get_modes(struct drm_connector *connector)
2152 {
2153 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2154 	struct drm_device *dev = intel_dp->base.base.dev;
2155 	struct drm_i915_private *dev_priv = dev->dev_private;
2156 	int ret;
2157 
2158 	/* We should parse the EDID data and find out if it has an audio sink
2159 	 */
2160 
2161 	ret = intel_dp_get_edid_modes(connector, intel_dp->adapter);
2162 	if (ret) {
2163 		if (is_edp(intel_dp) && !intel_dp->panel_fixed_mode) {
2164 			struct drm_display_mode *newmode;
2165 			list_for_each_entry(newmode, &connector->probed_modes,
2166 					    head) {
2167 				if ((newmode->type & DRM_MODE_TYPE_PREFERRED)) {
2168 					intel_dp->panel_fixed_mode =
2169 						drm_mode_duplicate(dev, newmode);
2170 					break;
2171 				}
2172 			}
2173 		}
2174 		return ret;
2175 	}
2176 
2177 	/* if eDP has no EDID, try to use fixed panel mode from VBT */
2178 	if (is_edp(intel_dp)) {
2179 		/* initialize panel mode from VBT if available for eDP */
2180 		if (intel_dp->panel_fixed_mode == NULL && dev_priv->lfp_lvds_vbt_mode != NULL) {
2181 			intel_dp->panel_fixed_mode =
2182 				drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
2183 			if (intel_dp->panel_fixed_mode) {
2184 				intel_dp->panel_fixed_mode->type |=
2185 					DRM_MODE_TYPE_PREFERRED;
2186 			}
2187 		}
2188 		if (intel_dp->panel_fixed_mode) {
2189 			struct drm_display_mode *mode;
2190 			mode = drm_mode_duplicate(dev, intel_dp->panel_fixed_mode);
2191 			drm_mode_probed_add(connector, mode);
2192 			return 1;
2193 		}
2194 	}
2195 	return 0;
2196 }
2197 
2198 static bool
intel_dp_detect_audio(struct drm_connector * connector)2199 intel_dp_detect_audio(struct drm_connector *connector)
2200 {
2201 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2202 	struct edid *edid;
2203 	bool has_audio = false;
2204 
2205 	edid = intel_dp_get_edid(connector, intel_dp->adapter);
2206 	if (edid) {
2207 		has_audio = drm_detect_monitor_audio(edid);
2208 
2209 		free(edid, DRM_MEM_KMS);
2210 	}
2211 
2212 	return has_audio;
2213 }
2214 
2215 static int
intel_dp_set_property(struct drm_connector * connector,struct drm_property * property,uint64_t val)2216 intel_dp_set_property(struct drm_connector *connector,
2217 		      struct drm_property *property,
2218 		      uint64_t val)
2219 {
2220 	struct drm_i915_private *dev_priv = connector->dev->dev_private;
2221 	struct intel_dp *intel_dp = intel_attached_dp(connector);
2222 	int ret;
2223 
2224 	ret = drm_object_property_set_value(&connector->base, property, val);
2225 	if (ret)
2226 		return ret;
2227 
2228 	if (property == dev_priv->force_audio_property) {
2229 		int i = val;
2230 		bool has_audio;
2231 
2232 		if (i == intel_dp->force_audio)
2233 			return 0;
2234 
2235 		intel_dp->force_audio = i;
2236 
2237 		if (i == HDMI_AUDIO_AUTO)
2238 			has_audio = intel_dp_detect_audio(connector);
2239 		else
2240 			has_audio = (i == HDMI_AUDIO_ON);
2241 
2242 		if (has_audio == intel_dp->has_audio)
2243 			return 0;
2244 
2245 		intel_dp->has_audio = has_audio;
2246 		goto done;
2247 	}
2248 
2249 	if (property == dev_priv->broadcast_rgb_property) {
2250 		if (val == !!intel_dp->color_range)
2251 			return 0;
2252 
2253 		intel_dp->color_range = val ? DP_COLOR_RANGE_16_235 : 0;
2254 		goto done;
2255 	}
2256 
2257 	return -EINVAL;
2258 
2259 done:
2260 	if (intel_dp->base.base.crtc) {
2261 		struct drm_crtc *crtc = intel_dp->base.base.crtc;
2262 		drm_crtc_helper_set_mode(crtc, &crtc->mode,
2263 					 crtc->x, crtc->y,
2264 					 crtc->fb);
2265 	}
2266 
2267 	return 0;
2268 }
2269 
2270 static void
intel_dp_destroy(struct drm_connector * connector)2271 intel_dp_destroy(struct drm_connector *connector)
2272 {
2273 	struct drm_device *dev = connector->dev;
2274 
2275 	if (intel_dpd_is_edp(dev))
2276 		intel_panel_destroy_backlight(dev);
2277 
2278 #if 0
2279 	drm_sysfs_connector_remove(connector);
2280 #endif
2281 	drm_connector_cleanup(connector);
2282 	free(connector, DRM_MEM_KMS);
2283 }
2284 
intel_dp_encoder_destroy(struct drm_encoder * encoder)2285 static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
2286 {
2287 	struct drm_device *dev;
2288 	struct intel_dp *intel_dp;
2289 
2290 	intel_dp = enc_to_intel_dp(encoder);
2291 	dev = encoder->dev;
2292 
2293 	if (intel_dp->dp_iic_bus != NULL) {
2294 		if (intel_dp->adapter != NULL) {
2295 			device_delete_child(intel_dp->dp_iic_bus,
2296 			    intel_dp->adapter);
2297 		}
2298 		device_delete_child(dev->dev, intel_dp->dp_iic_bus);
2299 	}
2300 	drm_encoder_cleanup(encoder);
2301 	if (is_edp(intel_dp)) {
2302 		struct drm_i915_private *dev_priv = intel_dp->base.base.dev->dev_private;
2303 
2304 		taskqueue_cancel_timeout(dev_priv->tq,
2305 		    &intel_dp->panel_vdd_task, NULL);
2306 		taskqueue_drain_timeout(dev_priv->tq,
2307 		    &intel_dp->panel_vdd_task);
2308 		ironlake_panel_vdd_off_sync(intel_dp);
2309 	}
2310 	free(intel_dp, DRM_MEM_KMS);
2311 }
2312 
2313 static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
2314 	.dpms = intel_dp_dpms,
2315 	.mode_fixup = intel_dp_mode_fixup,
2316 	.prepare = intel_dp_prepare,
2317 	.mode_set = intel_dp_mode_set,
2318 	.commit = intel_dp_commit,
2319 };
2320 
2321 static const struct drm_connector_funcs intel_dp_connector_funcs = {
2322 	.dpms = drm_helper_connector_dpms,
2323 	.detect = intel_dp_detect,
2324 	.fill_modes = drm_helper_probe_single_connector_modes,
2325 	.set_property = intel_dp_set_property,
2326 	.destroy = intel_dp_destroy,
2327 };
2328 
2329 static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
2330 	.get_modes = intel_dp_get_modes,
2331 	.mode_valid = intel_dp_mode_valid,
2332 	.best_encoder = intel_best_encoder,
2333 };
2334 
2335 static const struct drm_encoder_funcs intel_dp_enc_funcs = {
2336 	.destroy = intel_dp_encoder_destroy,
2337 };
2338 
2339 static void
intel_dp_hot_plug(struct intel_encoder * intel_encoder)2340 intel_dp_hot_plug(struct intel_encoder *intel_encoder)
2341 {
2342 	struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
2343 
2344 	intel_dp_check_link_status(intel_dp);
2345 }
2346 
2347 /* Return which DP Port should be selected for Transcoder DP control */
2348 int
intel_trans_dp_port_sel(struct drm_crtc * crtc)2349 intel_trans_dp_port_sel(struct drm_crtc *crtc)
2350 {
2351 	struct drm_device *dev = crtc->dev;
2352 	struct drm_mode_config *mode_config = &dev->mode_config;
2353 	struct drm_encoder *encoder;
2354 
2355 	list_for_each_entry(encoder, &mode_config->encoder_list, head) {
2356 		struct intel_dp *intel_dp;
2357 
2358 		if (encoder->crtc != crtc)
2359 			continue;
2360 
2361 		intel_dp = enc_to_intel_dp(encoder);
2362 		if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT ||
2363 		    intel_dp->base.type == INTEL_OUTPUT_EDP)
2364 			return intel_dp->output_reg;
2365 	}
2366 
2367 	return -1;
2368 }
2369 
2370 /* check the VBT to see whether the eDP is on DP-D port */
intel_dpd_is_edp(struct drm_device * dev)2371 bool intel_dpd_is_edp(struct drm_device *dev)
2372 {
2373 	struct drm_i915_private *dev_priv = dev->dev_private;
2374 	struct child_device_config *p_child;
2375 	int i;
2376 
2377 	if (!dev_priv->child_dev_num)
2378 		return false;
2379 
2380 	for (i = 0; i < dev_priv->child_dev_num; i++) {
2381 		p_child = dev_priv->child_dev + i;
2382 
2383 		if (p_child->dvo_port == PORT_IDPD &&
2384 		    p_child->device_type == DEVICE_TYPE_eDP)
2385 			return true;
2386 	}
2387 	return false;
2388 }
2389 
2390 static void
intel_dp_add_properties(struct intel_dp * intel_dp,struct drm_connector * connector)2391 intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
2392 {
2393 	intel_attach_force_audio_property(connector);
2394 	intel_attach_broadcast_rgb_property(connector);
2395 }
2396 
2397 void
intel_dp_init(struct drm_device * dev,int output_reg)2398 intel_dp_init(struct drm_device *dev, int output_reg)
2399 {
2400 	struct drm_i915_private *dev_priv = dev->dev_private;
2401 	struct drm_connector *connector;
2402 	struct intel_dp *intel_dp;
2403 	struct intel_encoder *intel_encoder;
2404 	struct intel_connector *intel_connector;
2405 	const char *name = NULL;
2406 	int type;
2407 
2408 	intel_dp = malloc(sizeof(struct intel_dp), DRM_MEM_KMS,
2409 	    M_WAITOK | M_ZERO);
2410 
2411 	intel_dp->output_reg = output_reg;
2412 	intel_dp->dpms_mode = -1;
2413 
2414 	intel_connector = malloc(sizeof(struct intel_connector), DRM_MEM_KMS,
2415 	    M_WAITOK | M_ZERO);
2416 	intel_encoder = &intel_dp->base;
2417 
2418 	if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
2419 		if (intel_dpd_is_edp(dev))
2420 			intel_dp->is_pch_edp = true;
2421 
2422 	if (output_reg == DP_A || is_pch_edp(intel_dp)) {
2423 		type = DRM_MODE_CONNECTOR_eDP;
2424 		intel_encoder->type = INTEL_OUTPUT_EDP;
2425 	} else {
2426 		type = DRM_MODE_CONNECTOR_DisplayPort;
2427 		intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
2428 	}
2429 
2430 	connector = &intel_connector->base;
2431 	drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
2432 	drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
2433 
2434 	connector->polled = DRM_CONNECTOR_POLL_HPD;
2435 
2436 	if (output_reg == DP_B || output_reg == PCH_DP_B)
2437 		intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
2438 	else if (output_reg == DP_C || output_reg == PCH_DP_C)
2439 		intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
2440 	else if (output_reg == DP_D || output_reg == PCH_DP_D)
2441 		intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
2442 
2443 	if (is_edp(intel_dp)) {
2444 		intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
2445 		TIMEOUT_TASK_INIT(dev_priv->tq, &intel_dp->panel_vdd_task, 0,
2446 		    ironlake_panel_vdd_work, intel_dp);
2447 	}
2448 
2449 	intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
2450 
2451 	connector->interlace_allowed = true;
2452 	connector->doublescan_allowed = 0;
2453 
2454 	drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
2455 			 DRM_MODE_ENCODER_TMDS);
2456 	drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
2457 
2458 	intel_connector_attach_encoder(intel_connector, intel_encoder);
2459 #if 0
2460 	drm_sysfs_connector_add(connector);
2461 #endif
2462 
2463 	/* Set up the DDC bus. */
2464 	switch (output_reg) {
2465 		case DP_A:
2466 			name = "DPDDC-A";
2467 			break;
2468 		case DP_B:
2469 		case PCH_DP_B:
2470 			dev_priv->hotplug_supported_mask |=
2471 				HDMIB_HOTPLUG_INT_STATUS;
2472 			name = "DPDDC-B";
2473 			break;
2474 		case DP_C:
2475 		case PCH_DP_C:
2476 			dev_priv->hotplug_supported_mask |=
2477 				HDMIC_HOTPLUG_INT_STATUS;
2478 			name = "DPDDC-C";
2479 			break;
2480 		case DP_D:
2481 		case PCH_DP_D:
2482 			dev_priv->hotplug_supported_mask |=
2483 				HDMID_HOTPLUG_INT_STATUS;
2484 			name = "DPDDC-D";
2485 			break;
2486 	}
2487 
2488 	/* Cache some DPCD data in the eDP case */
2489 	if (is_edp(intel_dp)) {
2490 		bool ret;
2491 		struct edp_power_seq	cur, vbt;
2492 		u32 pp_on, pp_off, pp_div;
2493 
2494 		pp_on = I915_READ(PCH_PP_ON_DELAYS);
2495 		pp_off = I915_READ(PCH_PP_OFF_DELAYS);
2496 		pp_div = I915_READ(PCH_PP_DIVISOR);
2497 
2498 		if (!pp_on || !pp_off || !pp_div) {
2499 			DRM_INFO("bad panel power sequencing delays, disabling panel\n");
2500 			intel_dp_encoder_destroy(&intel_dp->base.base);
2501 			intel_dp_destroy(&intel_connector->base);
2502 			return;
2503 		}
2504 
2505 		/* Pull timing values out of registers */
2506 		cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
2507 			PANEL_POWER_UP_DELAY_SHIFT;
2508 
2509 		cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
2510 			PANEL_LIGHT_ON_DELAY_SHIFT;
2511 
2512 		cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
2513 			PANEL_LIGHT_OFF_DELAY_SHIFT;
2514 
2515 		cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
2516 			PANEL_POWER_DOWN_DELAY_SHIFT;
2517 
2518 		cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
2519 			       PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
2520 
2521 		DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
2522 			      cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
2523 
2524 		vbt = dev_priv->edp.pps;
2525 
2526 		DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
2527 			      vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
2528 
2529 #define get_delay(field)	((max(cur.field, vbt.field) + 9) / 10)
2530 
2531 		intel_dp->panel_power_up_delay = get_delay(t1_t3);
2532 		intel_dp->backlight_on_delay = get_delay(t8);
2533 		intel_dp->backlight_off_delay = get_delay(t9);
2534 		intel_dp->panel_power_down_delay = get_delay(t10);
2535 		intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
2536 
2537 		DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
2538 			      intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
2539 			      intel_dp->panel_power_cycle_delay);
2540 
2541 		DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
2542 			      intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
2543 
2544 		ironlake_edp_panel_vdd_on(intel_dp);
2545 		ret = intel_dp_get_dpcd(intel_dp);
2546 		ironlake_edp_panel_vdd_off(intel_dp, false);
2547 
2548 		if (ret) {
2549 			if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
2550 				dev_priv->no_aux_handshake =
2551 					intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
2552 					DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
2553 		} else {
2554 			/* if this fails, presume the device is a ghost */
2555 			DRM_INFO("failed to retrieve link info, disabling eDP\n");
2556 			intel_dp_encoder_destroy(&intel_dp->base.base);
2557 			intel_dp_destroy(&intel_connector->base);
2558 			return;
2559 		}
2560 	}
2561 
2562 	intel_dp_i2c_init(intel_dp, intel_connector, name);
2563 
2564 	intel_encoder->hot_plug = intel_dp_hot_plug;
2565 
2566 	if (is_edp(intel_dp)) {
2567 		dev_priv->int_edp_connector = connector;
2568 		intel_panel_setup_backlight(dev);
2569 	}
2570 
2571 	intel_dp_add_properties(intel_dp, connector);
2572 
2573 	/* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
2574 	 * 0xd.  Failure to do so will result in spurious interrupts being
2575 	 * generated on the port when a cable is not attached.
2576 	 */
2577 	if (IS_G4X(dev) && !IS_GM45(dev)) {
2578 		u32 temp = I915_READ(PEG_BAND_GAP_DATA);
2579 		I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
2580 	}
2581 }
2582