2 * Copyright © 2008 Intel Corporation
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:
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
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
24 * Keith Packard <keithp@keithp.com>
28 #include <linux/i2c.h>
32 #include "drm_crtc_helper.h"
33 #include "intel_drv.h"
38 #define DP_LINK_STATUS_SIZE 6
39 #define DP_LINK_CHECK_TIMEOUT (10 * 1000)
41 #define DP_LINK_CONFIGURATION_SIZE 9
43 struct intel_dp_priv {
46 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
48 uint8_t save_link_configuration[DP_LINK_CONFIGURATION_SIZE];
53 struct intel_output *intel_output;
54 struct i2c_adapter adapter;
55 struct i2c_algo_dp_aux_data algo;
59 intel_dp_link_train(struct intel_output *intel_output, uint32_t DP,
60 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE]);
63 intel_dp_link_down(struct intel_output *intel_output, uint32_t DP);
66 intel_dp_max_lane_count(struct intel_output *intel_output)
68 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
69 int max_lane_count = 4;
71 if (dp_priv->dpcd[0] >= 0x11) {
72 max_lane_count = dp_priv->dpcd[2] & 0x1f;
73 switch (max_lane_count) {
74 case 1: case 2: case 4:
80 return max_lane_count;
84 intel_dp_max_link_bw(struct intel_output *intel_output)
86 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
87 int max_link_bw = dp_priv->dpcd[1];
89 switch (max_link_bw) {
94 max_link_bw = DP_LINK_BW_1_62;
101 intel_dp_link_clock(uint8_t link_bw)
103 if (link_bw == DP_LINK_BW_2_7)
109 /* I think this is a fiction */
111 intel_dp_link_required(int pixel_clock)
113 return pixel_clock * 3;
117 intel_dp_mode_valid(struct drm_connector *connector,
118 struct drm_display_mode *mode)
120 struct intel_output *intel_output = to_intel_output(connector);
121 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_output));
122 int max_lanes = intel_dp_max_lane_count(intel_output);
124 if (intel_dp_link_required(mode->clock) > max_link_clock * max_lanes)
125 return MODE_CLOCK_HIGH;
127 if (mode->clock < 10000)
128 return MODE_CLOCK_LOW;
134 pack_aux(uint8_t *src, int src_bytes)
141 for (i = 0; i < src_bytes; i++)
142 v |= ((uint32_t) src[i]) << ((3-i) * 8);
147 unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
152 for (i = 0; i < dst_bytes; i++)
153 dst[i] = src >> ((3-i) * 8);
157 intel_dp_aux_ch(struct intel_output *intel_output,
158 uint8_t *send, int send_bytes,
159 uint8_t *recv, int recv_size)
161 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
162 uint32_t output_reg = dp_priv->output_reg;
163 struct drm_device *dev = intel_output->base.dev;
164 struct drm_i915_private *dev_priv = dev->dev_private;
165 uint32_t ch_ctl = output_reg + 0x10;
166 uint32_t ch_data = ch_ctl + 4;
172 /* Load the send data into the aux channel data registers */
173 for (i = 0; i < send_bytes; i += 4) {
174 uint32_t d = pack_aux(send + i, send_bytes - i);;
176 I915_WRITE(ch_data + i, d);
179 /* The clock divider is based off the hrawclk,
180 * and would like to run at 2MHz. The 133 below assumes
181 * a 266MHz hrawclk; need to figure out how we're supposed
182 * to know what hrawclk is...
184 ctl = (DP_AUX_CH_CTL_SEND_BUSY |
185 DP_AUX_CH_CTL_TIME_OUT_1600us |
186 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
187 (5 << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
188 (133 << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
189 DP_AUX_CH_CTL_TIME_OUT_ERROR |
190 DP_AUX_CH_CTL_RECEIVE_ERROR);
192 /* Send the command and wait for it to complete */
193 I915_WRITE(ch_ctl, ctl);
194 (void) I915_READ(ch_ctl);
197 status = I915_READ(ch_ctl);
198 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
202 /* Clear done status and any errors */
203 I915_WRITE(ch_ctl, (ctl |
205 DP_AUX_CH_CTL_TIME_OUT_ERROR |
206 DP_AUX_CH_CTL_RECEIVE_ERROR));
207 (void) I915_READ(ch_ctl);
209 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
210 printk(KERN_ERR "dp_aux_ch not done status 0x%08x\n", status);
214 /* Check for timeout or receive error.
215 * Timeouts occur when the sink is not connected
217 if (status & (DP_AUX_CH_CTL_TIME_OUT_ERROR | DP_AUX_CH_CTL_RECEIVE_ERROR)) {
218 printk(KERN_ERR "dp_aux_ch error status 0x%08x\n", status);
222 /* Unload any bytes sent back from the other side */
223 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
224 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
226 if (recv_bytes > recv_size)
227 recv_bytes = recv_size;
229 for (i = 0; i < recv_bytes; i += 4) {
230 uint32_t d = I915_READ(ch_data + i);
232 unpack_aux(d, recv + i, recv_bytes - i);
238 /* Write data to the aux channel in native mode */
240 intel_dp_aux_native_write(struct intel_output *intel_output,
241 uint16_t address, uint8_t *send, int send_bytes)
250 msg[0] = AUX_NATIVE_WRITE << 4;
251 msg[1] = address >> 8;
253 msg[3] = send_bytes - 1;
254 memcpy(&msg[4], send, send_bytes);
255 msg_bytes = send_bytes + 4;
257 ret = intel_dp_aux_ch(intel_output, msg, msg_bytes, &ack, 1);
260 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
262 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
270 /* Write a single byte to the aux channel in native mode */
272 intel_dp_aux_native_write_1(struct intel_output *intel_output,
273 uint16_t address, uint8_t byte)
275 return intel_dp_aux_native_write(intel_output, address, &byte, 1);
278 /* read bytes from a native aux channel */
280 intel_dp_aux_native_read(struct intel_output *intel_output,
281 uint16_t address, uint8_t *recv, int recv_bytes)
290 msg[0] = AUX_NATIVE_READ << 4;
291 msg[1] = address >> 8;
292 msg[2] = address & 0xff;
293 msg[3] = recv_bytes - 1;
296 reply_bytes = recv_bytes + 1;
299 ret = intel_dp_aux_ch(intel_output, msg, msg_bytes,
304 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
305 memcpy(recv, reply + 1, ret - 1);
308 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
316 intel_dp_i2c_aux_ch(struct i2c_adapter *adapter,
317 uint8_t *send, int send_bytes,
318 uint8_t *recv, int recv_bytes)
320 struct intel_dp_priv *dp_priv = container_of(adapter,
321 struct intel_dp_priv,
323 struct intel_output *intel_output = dp_priv->intel_output;
325 return intel_dp_aux_ch(intel_output,
326 send, send_bytes, recv, recv_bytes);
330 intel_dp_i2c_init(struct intel_output *intel_output, const char *name)
332 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
334 DRM_ERROR("i2c_init %s\n", name);
335 dp_priv->algo.running = false;
336 dp_priv->algo.address = 0;
337 dp_priv->algo.aux_ch = intel_dp_i2c_aux_ch;
339 memset(&dp_priv->adapter, '\0', sizeof (dp_priv->adapter));
340 dp_priv->adapter.owner = THIS_MODULE;
341 dp_priv->adapter.class = I2C_CLASS_DDC;
342 strncpy (dp_priv->adapter.name, name, sizeof dp_priv->adapter.name - 1);
343 dp_priv->adapter.name[sizeof dp_priv->adapter.name - 1] = '\0';
344 dp_priv->adapter.algo_data = &dp_priv->algo;
345 dp_priv->adapter.dev.parent = &intel_output->base.kdev;
347 return i2c_dp_aux_add_bus(&dp_priv->adapter);
351 intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
352 struct drm_display_mode *adjusted_mode)
354 struct intel_output *intel_output = enc_to_intel_output(encoder);
355 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
356 int lane_count, clock;
357 int max_lane_count = intel_dp_max_lane_count(intel_output);
358 int max_clock = intel_dp_max_link_bw(intel_output) == DP_LINK_BW_2_7 ? 1 : 0;
359 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
361 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
362 for (clock = 0; clock <= max_clock; clock++) {
363 int link_avail = intel_dp_link_clock(bws[clock]) * lane_count;
365 if (intel_dp_link_required(mode->clock) <= link_avail) {
366 dp_priv->link_bw = bws[clock];
367 dp_priv->lane_count = lane_count;
368 adjusted_mode->clock = intel_dp_link_clock(dp_priv->link_bw);
369 printk(KERN_ERR "link bw %02x lane count %d clock %d\n",
370 dp_priv->link_bw, dp_priv->lane_count,
371 adjusted_mode->clock);
379 struct intel_dp_m_n {
388 intel_reduce_ratio(uint32_t *num, uint32_t *den)
390 while (*num > 0xffffff || *den > 0xffffff) {
397 intel_dp_compute_m_n(int bytes_per_pixel,
401 struct intel_dp_m_n *m_n)
404 m_n->gmch_m = pixel_clock * bytes_per_pixel;
405 m_n->gmch_n = link_clock * nlanes;
406 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
407 m_n->link_m = pixel_clock;
408 m_n->link_n = link_clock;
409 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
413 intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
414 struct drm_display_mode *adjusted_mode)
416 struct drm_device *dev = crtc->dev;
417 struct drm_mode_config *mode_config = &dev->mode_config;
418 struct drm_connector *connector;
419 struct drm_i915_private *dev_priv = dev->dev_private;
420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
422 struct intel_dp_m_n m_n;
425 * Find the lane count in the intel_output private
427 list_for_each_entry(connector, &mode_config->connector_list, head) {
428 struct intel_output *intel_output = to_intel_output(connector);
429 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
431 if (!connector->encoder || connector->encoder->crtc != crtc)
434 if (intel_output->type == INTEL_OUTPUT_DISPLAYPORT) {
435 lane_count = dp_priv->lane_count;
441 * Compute the GMCH and Link ratios. The '3' here is
442 * the number of bytes_per_pixel post-LUT, which we always
443 * set up for 8-bits of R/G/B, or 3 bytes total.
445 intel_dp_compute_m_n(3, lane_count,
446 mode->clock, adjusted_mode->clock, &m_n);
448 if (intel_crtc->pipe == 0) {
449 I915_WRITE(PIPEA_GMCH_DATA_M,
450 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
452 I915_WRITE(PIPEA_GMCH_DATA_N,
454 I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
455 I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
457 I915_WRITE(PIPEB_GMCH_DATA_M,
458 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
460 I915_WRITE(PIPEB_GMCH_DATA_N,
462 I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
463 I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
468 intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
469 struct drm_display_mode *adjusted_mode)
471 struct intel_output *intel_output = enc_to_intel_output(encoder);
472 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
473 struct drm_crtc *crtc = intel_output->enc.crtc;
474 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
476 dp_priv->DP = (DP_LINK_TRAIN_OFF |
482 switch (dp_priv->lane_count) {
484 dp_priv->DP |= DP_PORT_WIDTH_1;
487 dp_priv->DP |= DP_PORT_WIDTH_2;
490 dp_priv->DP |= DP_PORT_WIDTH_4;
493 if (dp_priv->has_audio)
494 dp_priv->DP |= DP_AUDIO_OUTPUT_ENABLE;
496 memset(dp_priv->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
497 dp_priv->link_configuration[0] = dp_priv->link_bw;
498 dp_priv->link_configuration[1] = dp_priv->lane_count;
501 * Check for DPCD version > 1.1,
502 * enable enahanced frame stuff in that case
504 if (dp_priv->dpcd[0] >= 0x11) {
505 dp_priv->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
506 dp_priv->DP |= DP_ENHANCED_FRAMING;
509 if (intel_crtc->pipe == 1)
510 dp_priv->DP |= DP_PIPEB_SELECT;
515 intel_dp_dpms(struct drm_encoder *encoder, int mode)
517 struct intel_output *intel_output = enc_to_intel_output(encoder);
518 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
519 struct drm_device *dev = intel_output->base.dev;
520 struct drm_i915_private *dev_priv = dev->dev_private;
521 uint32_t dp_reg = I915_READ(dp_priv->output_reg);
523 if (mode != DRM_MODE_DPMS_ON) {
524 if (dp_reg & DP_PORT_EN)
525 intel_dp_link_down(intel_output, dp_priv->DP);
527 if (!(dp_reg & DP_PORT_EN))
528 intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
533 * Fetch AUX CH registers 0x202 - 0x207 which contain
534 * link status information
537 intel_dp_get_link_status(struct intel_output *intel_output,
538 uint8_t link_status[DP_LINK_STATUS_SIZE])
542 ret = intel_dp_aux_native_read(intel_output,
544 link_status, DP_LINK_STATUS_SIZE);
545 if (ret != DP_LINK_STATUS_SIZE)
551 intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
554 return link_status[r - DP_LANE0_1_STATUS];
558 intel_dp_save(struct drm_connector *connector)
560 struct intel_output *intel_output = to_intel_output(connector);
561 struct drm_device *dev = intel_output->base.dev;
562 struct drm_i915_private *dev_priv = dev->dev_private;
563 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
565 dp_priv->save_DP = I915_READ(dp_priv->output_reg);
566 intel_dp_aux_native_read(intel_output, DP_LINK_BW_SET,
567 dp_priv->save_link_configuration,
568 sizeof (dp_priv->save_link_configuration));
572 intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
575 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
576 int s = ((lane & 1) ?
577 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
578 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
579 uint8_t l = intel_dp_link_status(link_status, i);
581 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
585 intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
588 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
589 int s = ((lane & 1) ?
590 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
591 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
592 uint8_t l = intel_dp_link_status(link_status, i);
594 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
599 static char *voltage_names[] = {
600 "0.4V", "0.6V", "0.8V", "1.2V"
602 static char *pre_emph_names[] = {
603 "0dB", "3.5dB", "6dB", "9.5dB"
605 static char *link_train_names[] = {
606 "pattern 1", "pattern 2", "idle", "off"
611 * These are source-specific values; current Intel hardware supports
612 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
614 #define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
617 intel_dp_pre_emphasis_max(uint8_t voltage_swing)
619 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
620 case DP_TRAIN_VOLTAGE_SWING_400:
621 return DP_TRAIN_PRE_EMPHASIS_6;
622 case DP_TRAIN_VOLTAGE_SWING_600:
623 return DP_TRAIN_PRE_EMPHASIS_6;
624 case DP_TRAIN_VOLTAGE_SWING_800:
625 return DP_TRAIN_PRE_EMPHASIS_3_5;
626 case DP_TRAIN_VOLTAGE_SWING_1200:
628 return DP_TRAIN_PRE_EMPHASIS_0;
633 intel_get_adjust_train(struct intel_output *intel_output,
634 uint8_t link_status[DP_LINK_STATUS_SIZE],
636 uint8_t train_set[4])
642 for (lane = 0; lane < lane_count; lane++) {
643 uint8_t this_v = intel_get_adjust_request_voltage(link_status, lane);
644 uint8_t this_p = intel_get_adjust_request_pre_emphasis(link_status, lane);
652 if (v >= I830_DP_VOLTAGE_MAX)
653 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
655 if (p >= intel_dp_pre_emphasis_max(v))
656 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
658 for (lane = 0; lane < 4; lane++)
659 train_set[lane] = v | p;
663 intel_dp_signal_levels(uint8_t train_set, int lane_count)
665 uint32_t signal_levels = 0;
667 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
668 case DP_TRAIN_VOLTAGE_SWING_400:
670 signal_levels |= DP_VOLTAGE_0_4;
672 case DP_TRAIN_VOLTAGE_SWING_600:
673 signal_levels |= DP_VOLTAGE_0_6;
675 case DP_TRAIN_VOLTAGE_SWING_800:
676 signal_levels |= DP_VOLTAGE_0_8;
678 case DP_TRAIN_VOLTAGE_SWING_1200:
679 signal_levels |= DP_VOLTAGE_1_2;
682 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
683 case DP_TRAIN_PRE_EMPHASIS_0:
685 signal_levels |= DP_PRE_EMPHASIS_0;
687 case DP_TRAIN_PRE_EMPHASIS_3_5:
688 signal_levels |= DP_PRE_EMPHASIS_3_5;
690 case DP_TRAIN_PRE_EMPHASIS_6:
691 signal_levels |= DP_PRE_EMPHASIS_6;
693 case DP_TRAIN_PRE_EMPHASIS_9_5:
694 signal_levels |= DP_PRE_EMPHASIS_9_5;
697 return signal_levels;
701 intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
704 int i = DP_LANE0_1_STATUS + (lane >> 1);
705 int s = (lane & 1) * 4;
706 uint8_t l = intel_dp_link_status(link_status, i);
708 return (l >> s) & 0xf;
711 /* Check for clock recovery is done on all channels */
713 intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
718 for (lane = 0; lane < lane_count; lane++) {
719 lane_status = intel_get_lane_status(link_status, lane);
720 if ((lane_status & DP_LANE_CR_DONE) == 0)
726 /* Check to see if channel eq is done on all channels */
727 #define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
728 DP_LANE_CHANNEL_EQ_DONE|\
729 DP_LANE_SYMBOL_LOCKED)
731 intel_channel_eq_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
737 lane_align = intel_dp_link_status(link_status,
738 DP_LANE_ALIGN_STATUS_UPDATED);
739 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
741 for (lane = 0; lane < lane_count; lane++) {
742 lane_status = intel_get_lane_status(link_status, lane);
743 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
750 intel_dp_set_link_train(struct intel_output *intel_output,
751 uint32_t dp_reg_value,
752 uint8_t dp_train_pat,
753 uint8_t train_set[4],
756 struct drm_device *dev = intel_output->base.dev;
757 struct drm_i915_private *dev_priv = dev->dev_private;
758 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
761 I915_WRITE(dp_priv->output_reg, dp_reg_value);
762 POSTING_READ(dp_priv->output_reg);
764 intel_wait_for_vblank(dev);
766 intel_dp_aux_native_write_1(intel_output,
767 DP_TRAINING_PATTERN_SET,
770 ret = intel_dp_aux_native_write(intel_output,
771 DP_TRAINING_LANE0_SET, train_set, 4);
779 intel_dp_link_train(struct intel_output *intel_output, uint32_t DP,
780 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE])
782 struct drm_device *dev = intel_output->base.dev;
783 struct drm_i915_private *dev_priv = dev->dev_private;
784 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
785 uint8_t train_set[4];
786 uint8_t link_status[DP_LINK_STATUS_SIZE];
789 bool clock_recovery = false;
790 bool channel_eq = false;
794 /* Write the link configuration data */
795 intel_dp_aux_native_write(intel_output, 0x100,
796 link_configuration, DP_LINK_CONFIGURATION_SIZE);
799 DP &= ~DP_LINK_TRAIN_MASK;
800 memset(train_set, 0, 4);
803 clock_recovery = false;
805 /* Use train_set[0] to set the voltage and pre emphasis values */
806 uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
807 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
809 if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_1,
810 DP_TRAINING_PATTERN_1, train_set, first))
813 /* Set training pattern 1 */
816 if (!intel_dp_get_link_status(intel_output, link_status))
819 if (intel_clock_recovery_ok(link_status, dp_priv->lane_count)) {
820 clock_recovery = true;
824 /* Check to see if we've tried the max voltage */
825 for (i = 0; i < dp_priv->lane_count; i++)
826 if ((train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
828 if (i == dp_priv->lane_count)
831 /* Check to see if we've tried the same voltage 5 times */
832 if ((train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
838 voltage = train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
840 /* Compute new train_set as requested by target */
841 intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
844 /* channel equalization */
848 /* Use train_set[0] to set the voltage and pre emphasis values */
849 uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
850 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
852 /* channel eq pattern */
853 if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_2,
854 DP_TRAINING_PATTERN_2, train_set,
859 if (!intel_dp_get_link_status(intel_output, link_status))
862 if (intel_channel_eq_ok(link_status, dp_priv->lane_count)) {
871 /* Compute new train_set as requested by target */
872 intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
876 I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_OFF);
877 POSTING_READ(dp_priv->output_reg);
878 intel_dp_aux_native_write_1(intel_output,
879 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
883 intel_dp_link_down(struct intel_output *intel_output, uint32_t DP)
885 struct drm_device *dev = intel_output->base.dev;
886 struct drm_i915_private *dev_priv = dev->dev_private;
887 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
889 I915_WRITE(dp_priv->output_reg, DP & ~DP_PORT_EN);
890 POSTING_READ(dp_priv->output_reg);
894 intel_dp_restore(struct drm_connector *connector)
896 struct intel_output *intel_output = to_intel_output(connector);
897 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
899 if (dp_priv->save_DP & DP_PORT_EN)
900 intel_dp_link_train(intel_output, dp_priv->save_DP, dp_priv->save_link_configuration);
902 intel_dp_link_down(intel_output, dp_priv->save_DP);
907 * According to DP spec
910 * 2. Configure link according to Receiver Capabilities
911 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
912 * 4. Check link status on receipt of hot-plug interrupt
916 intel_dp_check_link_status(struct intel_output *intel_output)
918 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
919 uint8_t link_status[DP_LINK_STATUS_SIZE];
921 if (!intel_output->enc.crtc)
924 if (!intel_dp_get_link_status(intel_output, link_status)) {
925 intel_dp_link_down(intel_output, dp_priv->DP);
929 if (!intel_channel_eq_ok(link_status, dp_priv->lane_count))
930 intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
935 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
937 * \return true if DP port is connected.
938 * \return false if DP port is disconnected.
940 static enum drm_connector_status
941 intel_dp_detect(struct drm_connector *connector)
943 struct intel_output *intel_output = to_intel_output(connector);
944 struct drm_device *dev = intel_output->base.dev;
945 struct drm_i915_private *dev_priv = dev->dev_private;
946 struct intel_dp_priv *dp_priv = intel_output->dev_priv;
948 enum drm_connector_status status;
950 dp_priv->has_audio = false;
952 temp = I915_READ(PORT_HOTPLUG_EN);
954 I915_WRITE(PORT_HOTPLUG_EN,
960 POSTING_READ(PORT_HOTPLUG_EN);
962 switch (dp_priv->output_reg) {
964 bit = DPB_HOTPLUG_INT_STATUS;
967 bit = DPC_HOTPLUG_INT_STATUS;
970 bit = DPD_HOTPLUG_INT_STATUS;
973 return connector_status_unknown;
976 temp = I915_READ(PORT_HOTPLUG_STAT);
978 if ((temp & bit) == 0)
979 return connector_status_disconnected;
981 status = connector_status_disconnected;
982 if (intel_dp_aux_native_read(intel_output,
983 0x000, dp_priv->dpcd,
984 sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
986 if (dp_priv->dpcd[0] != 0)
987 status = connector_status_connected;
992 static int intel_dp_get_modes(struct drm_connector *connector)
994 struct intel_output *intel_output = to_intel_output(connector);
996 /* We should parse the EDID data and find out if it has an audio sink
999 return intel_ddc_get_modes(intel_output);
1003 intel_dp_destroy (struct drm_connector *connector)
1005 struct intel_output *intel_output = to_intel_output(connector);
1007 if (intel_output->i2c_bus)
1008 intel_i2c_destroy(intel_output->i2c_bus);
1009 drm_sysfs_connector_remove(connector);
1010 drm_connector_cleanup(connector);
1011 kfree(intel_output);
1014 static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1015 .dpms = intel_dp_dpms,
1016 .mode_fixup = intel_dp_mode_fixup,
1017 .prepare = intel_encoder_prepare,
1018 .mode_set = intel_dp_mode_set,
1019 .commit = intel_encoder_commit,
1022 static const struct drm_connector_funcs intel_dp_connector_funcs = {
1023 .dpms = drm_helper_connector_dpms,
1024 .save = intel_dp_save,
1025 .restore = intel_dp_restore,
1026 .detect = intel_dp_detect,
1027 .fill_modes = drm_helper_probe_single_connector_modes,
1028 .destroy = intel_dp_destroy,
1031 static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1032 .get_modes = intel_dp_get_modes,
1033 .mode_valid = intel_dp_mode_valid,
1034 .best_encoder = intel_best_encoder,
1037 static void intel_dp_enc_destroy(struct drm_encoder *encoder)
1039 drm_encoder_cleanup(encoder);
1042 static const struct drm_encoder_funcs intel_dp_enc_funcs = {
1043 .destroy = intel_dp_enc_destroy,
1047 intel_dp_init(struct drm_device *dev, int output_reg)
1049 struct drm_i915_private *dev_priv = dev->dev_private;
1050 struct drm_connector *connector;
1051 struct intel_output *intel_output;
1052 struct intel_dp_priv *dp_priv;
1054 intel_output = kcalloc(sizeof(struct intel_output) +
1055 sizeof(struct intel_dp_priv), 1, GFP_KERNEL);
1059 dp_priv = (struct intel_dp_priv *)(intel_output + 1);
1061 connector = &intel_output->base;
1062 drm_connector_init(dev, connector, &intel_dp_connector_funcs,
1063 DRM_MODE_CONNECTOR_DisplayPort);
1064 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1066 intel_output->type = INTEL_OUTPUT_DISPLAYPORT;
1068 connector->interlace_allowed = true;
1069 connector->doublescan_allowed = 0;
1071 dp_priv->intel_output = intel_output;
1072 dp_priv->output_reg = output_reg;
1073 dp_priv->has_audio = false;
1074 intel_output->dev_priv = dp_priv;
1076 drm_encoder_init(dev, &intel_output->enc, &intel_dp_enc_funcs,
1077 DRM_MODE_ENCODER_TMDS);
1078 drm_encoder_helper_add(&intel_output->enc, &intel_dp_helper_funcs);
1080 drm_mode_connector_attach_encoder(&intel_output->base,
1081 &intel_output->enc);
1082 drm_sysfs_connector_add(connector);
1084 /* Set up the DDC bus. */
1085 intel_dp_i2c_init(intel_output,
1086 (output_reg == DP_B) ? "DPDDC-B" :
1087 (output_reg == DP_C) ? "DPDDC-C" : "DPDDC-D");
1088 intel_output->ddc_bus = &dp_priv->adapter;
1090 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1091 * 0xd. Failure to do so will result in spurious interrupts being
1092 * generated on the port when a cable is not attached.
1094 if (IS_G4X(dev) && !IS_GM45(dev)) {
1095 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
1096 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);