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"
36 #include "drm_dp_helper.h"
39 #define DP_LINK_STATUS_SIZE 6
40 #define DP_LINK_CHECK_TIMEOUT (10 * 1000)
42 #define DP_LINK_CONFIGURATION_SIZE 9
44 #define IS_eDP(i) ((i)->type == INTEL_OUTPUT_EDP)
46 struct intel_dp_priv {
49 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
55 struct intel_encoder *intel_encoder;
56 struct i2c_adapter adapter;
57 struct i2c_algo_dp_aux_data algo;
61 intel_dp_link_train(struct intel_encoder *intel_encoder, uint32_t DP,
62 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE]);
65 intel_dp_link_down(struct intel_encoder *intel_encoder, uint32_t DP);
68 intel_edp_link_config (struct intel_encoder *intel_encoder,
69 int *lane_num, int *link_bw)
71 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
73 *lane_num = dp_priv->lane_count;
74 if (dp_priv->link_bw == DP_LINK_BW_1_62)
76 else if (dp_priv->link_bw == DP_LINK_BW_2_7)
81 intel_dp_max_lane_count(struct intel_encoder *intel_encoder)
83 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
84 int max_lane_count = 4;
86 if (dp_priv->dpcd[0] >= 0x11) {
87 max_lane_count = dp_priv->dpcd[2] & 0x1f;
88 switch (max_lane_count) {
89 case 1: case 2: case 4:
95 return max_lane_count;
99 intel_dp_max_link_bw(struct intel_encoder *intel_encoder)
101 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
102 int max_link_bw = dp_priv->dpcd[1];
104 switch (max_link_bw) {
105 case DP_LINK_BW_1_62:
109 max_link_bw = DP_LINK_BW_1_62;
116 intel_dp_link_clock(uint8_t link_bw)
118 if (link_bw == DP_LINK_BW_2_7)
124 /* I think this is a fiction */
126 intel_dp_link_required(struct drm_device *dev,
127 struct intel_encoder *intel_encoder, int pixel_clock)
129 struct drm_i915_private *dev_priv = dev->dev_private;
131 if (IS_eDP(intel_encoder))
132 return (pixel_clock * dev_priv->edp_bpp) / 8;
134 return pixel_clock * 3;
138 intel_dp_mode_valid(struct drm_connector *connector,
139 struct drm_display_mode *mode)
141 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
142 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_encoder));
143 int max_lanes = intel_dp_max_lane_count(intel_encoder);
145 if (intel_dp_link_required(connector->dev, intel_encoder, mode->clock)
146 > max_link_clock * max_lanes)
147 return MODE_CLOCK_HIGH;
149 if (mode->clock < 10000)
150 return MODE_CLOCK_LOW;
156 pack_aux(uint8_t *src, int src_bytes)
163 for (i = 0; i < src_bytes; i++)
164 v |= ((uint32_t) src[i]) << ((3-i) * 8);
169 unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
174 for (i = 0; i < dst_bytes; i++)
175 dst[i] = src >> ((3-i) * 8);
178 /* hrawclock is 1/4 the FSB frequency */
180 intel_hrawclk(struct drm_device *dev)
182 struct drm_i915_private *dev_priv = dev->dev_private;
185 clkcfg = I915_READ(CLKCFG);
186 switch (clkcfg & CLKCFG_FSB_MASK) {
195 case CLKCFG_FSB_1067:
197 case CLKCFG_FSB_1333:
199 /* these two are just a guess; one of them might be right */
200 case CLKCFG_FSB_1600:
201 case CLKCFG_FSB_1600_ALT:
209 intel_dp_aux_ch(struct intel_encoder *intel_encoder,
210 uint8_t *send, int send_bytes,
211 uint8_t *recv, int recv_size)
213 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
214 uint32_t output_reg = dp_priv->output_reg;
215 struct drm_device *dev = intel_encoder->base.dev;
216 struct drm_i915_private *dev_priv = dev->dev_private;
217 uint32_t ch_ctl = output_reg + 0x10;
218 uint32_t ch_data = ch_ctl + 4;
223 uint32_t aux_clock_divider;
226 /* The clock divider is based off the hrawclk,
227 * and would like to run at 2MHz. So, take the
228 * hrawclk value and divide by 2 and use that
230 if (IS_eDP(intel_encoder))
231 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
232 else if (HAS_PCH_SPLIT(dev))
233 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
235 aux_clock_divider = intel_hrawclk(dev) / 2;
237 /* Must try at least 3 times according to DP spec */
238 for (try = 0; try < 5; try++) {
239 /* Load the send data into the aux channel data registers */
240 for (i = 0; i < send_bytes; i += 4) {
241 uint32_t d = pack_aux(send + i, send_bytes - i);
243 I915_WRITE(ch_data + i, d);
246 ctl = (DP_AUX_CH_CTL_SEND_BUSY |
247 DP_AUX_CH_CTL_TIME_OUT_400us |
248 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
249 (5 << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
250 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
252 DP_AUX_CH_CTL_TIME_OUT_ERROR |
253 DP_AUX_CH_CTL_RECEIVE_ERROR);
255 /* Send the command and wait for it to complete */
256 I915_WRITE(ch_ctl, ctl);
257 (void) I915_READ(ch_ctl);
260 status = I915_READ(ch_ctl);
261 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
265 /* Clear done status and any errors */
266 I915_WRITE(ch_ctl, (status |
268 DP_AUX_CH_CTL_TIME_OUT_ERROR |
269 DP_AUX_CH_CTL_RECEIVE_ERROR));
270 (void) I915_READ(ch_ctl);
271 if ((status & DP_AUX_CH_CTL_TIME_OUT_ERROR) == 0)
275 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
276 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
280 /* Check for timeout or receive error.
281 * Timeouts occur when the sink is not connected
283 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
284 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
288 /* Timeouts occur when the device isn't connected, so they're
289 * "normal" -- don't fill the kernel log with these */
290 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
291 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
295 /* Unload any bytes sent back from the other side */
296 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
297 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
299 if (recv_bytes > recv_size)
300 recv_bytes = recv_size;
302 for (i = 0; i < recv_bytes; i += 4) {
303 uint32_t d = I915_READ(ch_data + i);
305 unpack_aux(d, recv + i, recv_bytes - i);
311 /* Write data to the aux channel in native mode */
313 intel_dp_aux_native_write(struct intel_encoder *intel_encoder,
314 uint16_t address, uint8_t *send, int send_bytes)
323 msg[0] = AUX_NATIVE_WRITE << 4;
324 msg[1] = address >> 8;
325 msg[2] = address & 0xff;
326 msg[3] = send_bytes - 1;
327 memcpy(&msg[4], send, send_bytes);
328 msg_bytes = send_bytes + 4;
330 ret = intel_dp_aux_ch(intel_encoder, msg, msg_bytes, &ack, 1);
333 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
335 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
343 /* Write a single byte to the aux channel in native mode */
345 intel_dp_aux_native_write_1(struct intel_encoder *intel_encoder,
346 uint16_t address, uint8_t byte)
348 return intel_dp_aux_native_write(intel_encoder, address, &byte, 1);
351 /* read bytes from a native aux channel */
353 intel_dp_aux_native_read(struct intel_encoder *intel_encoder,
354 uint16_t address, uint8_t *recv, int recv_bytes)
363 msg[0] = AUX_NATIVE_READ << 4;
364 msg[1] = address >> 8;
365 msg[2] = address & 0xff;
366 msg[3] = recv_bytes - 1;
369 reply_bytes = recv_bytes + 1;
372 ret = intel_dp_aux_ch(intel_encoder, msg, msg_bytes,
379 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
380 memcpy(recv, reply + 1, ret - 1);
383 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
391 intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
392 uint8_t write_byte, uint8_t *read_byte)
394 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
395 struct intel_dp_priv *dp_priv = container_of(adapter,
396 struct intel_dp_priv,
398 struct intel_encoder *intel_encoder = dp_priv->intel_encoder;
399 uint16_t address = algo_data->address;
406 /* Set up the command byte */
407 if (mode & MODE_I2C_READ)
408 msg[0] = AUX_I2C_READ << 4;
410 msg[0] = AUX_I2C_WRITE << 4;
412 if (!(mode & MODE_I2C_STOP))
413 msg[0] |= AUX_I2C_MOT << 4;
415 msg[1] = address >> 8;
437 ret = intel_dp_aux_ch(intel_encoder,
441 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
444 switch (reply[0] & AUX_I2C_REPLY_MASK) {
445 case AUX_I2C_REPLY_ACK:
446 if (mode == MODE_I2C_READ) {
447 *read_byte = reply[1];
449 return reply_bytes - 1;
450 case AUX_I2C_REPLY_NACK:
451 DRM_DEBUG_KMS("aux_ch nack\n");
453 case AUX_I2C_REPLY_DEFER:
454 DRM_DEBUG_KMS("aux_ch defer\n");
458 DRM_ERROR("aux_ch invalid reply 0x%02x\n", reply[0]);
465 intel_dp_i2c_init(struct intel_encoder *intel_encoder, const char *name)
467 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
469 DRM_DEBUG_KMS("i2c_init %s\n", name);
470 dp_priv->algo.running = false;
471 dp_priv->algo.address = 0;
472 dp_priv->algo.aux_ch = intel_dp_i2c_aux_ch;
474 memset(&dp_priv->adapter, '\0', sizeof (dp_priv->adapter));
475 dp_priv->adapter.owner = THIS_MODULE;
476 dp_priv->adapter.class = I2C_CLASS_DDC;
477 strncpy (dp_priv->adapter.name, name, sizeof(dp_priv->adapter.name) - 1);
478 dp_priv->adapter.name[sizeof(dp_priv->adapter.name) - 1] = '\0';
479 dp_priv->adapter.algo_data = &dp_priv->algo;
480 dp_priv->adapter.dev.parent = &intel_encoder->base.kdev;
482 return i2c_dp_aux_add_bus(&dp_priv->adapter);
486 intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
487 struct drm_display_mode *adjusted_mode)
489 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
490 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
491 int lane_count, clock;
492 int max_lane_count = intel_dp_max_lane_count(intel_encoder);
493 int max_clock = intel_dp_max_link_bw(intel_encoder) == DP_LINK_BW_2_7 ? 1 : 0;
494 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
496 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
497 for (clock = 0; clock <= max_clock; clock++) {
498 int link_avail = intel_dp_link_clock(bws[clock]) * lane_count;
500 if (intel_dp_link_required(encoder->dev, intel_encoder, mode->clock)
502 dp_priv->link_bw = bws[clock];
503 dp_priv->lane_count = lane_count;
504 adjusted_mode->clock = intel_dp_link_clock(dp_priv->link_bw);
505 DRM_DEBUG_KMS("Display port link bw %02x lane "
506 "count %d clock %d\n",
507 dp_priv->link_bw, dp_priv->lane_count,
508 adjusted_mode->clock);
516 struct intel_dp_m_n {
525 intel_reduce_ratio(uint32_t *num, uint32_t *den)
527 while (*num > 0xffffff || *den > 0xffffff) {
534 intel_dp_compute_m_n(int bytes_per_pixel,
538 struct intel_dp_m_n *m_n)
541 m_n->gmch_m = pixel_clock * bytes_per_pixel;
542 m_n->gmch_n = link_clock * nlanes;
543 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
544 m_n->link_m = pixel_clock;
545 m_n->link_n = link_clock;
546 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
550 intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
551 struct drm_display_mode *adjusted_mode)
553 struct drm_device *dev = crtc->dev;
554 struct drm_mode_config *mode_config = &dev->mode_config;
555 struct drm_connector *connector;
556 struct drm_i915_private *dev_priv = dev->dev_private;
557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
559 struct intel_dp_m_n m_n;
562 * Find the lane count in the intel_encoder private
564 list_for_each_entry(connector, &mode_config->connector_list, head) {
565 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
566 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
568 if (!connector->encoder || connector->encoder->crtc != crtc)
571 if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT) {
572 lane_count = dp_priv->lane_count;
578 * Compute the GMCH and Link ratios. The '3' here is
579 * the number of bytes_per_pixel post-LUT, which we always
580 * set up for 8-bits of R/G/B, or 3 bytes total.
582 intel_dp_compute_m_n(3, lane_count,
583 mode->clock, adjusted_mode->clock, &m_n);
585 if (HAS_PCH_SPLIT(dev)) {
586 if (intel_crtc->pipe == 0) {
587 I915_WRITE(TRANSA_DATA_M1,
588 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
590 I915_WRITE(TRANSA_DATA_N1, m_n.gmch_n);
591 I915_WRITE(TRANSA_DP_LINK_M1, m_n.link_m);
592 I915_WRITE(TRANSA_DP_LINK_N1, m_n.link_n);
594 I915_WRITE(TRANSB_DATA_M1,
595 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
597 I915_WRITE(TRANSB_DATA_N1, m_n.gmch_n);
598 I915_WRITE(TRANSB_DP_LINK_M1, m_n.link_m);
599 I915_WRITE(TRANSB_DP_LINK_N1, m_n.link_n);
602 if (intel_crtc->pipe == 0) {
603 I915_WRITE(PIPEA_GMCH_DATA_M,
604 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
606 I915_WRITE(PIPEA_GMCH_DATA_N,
608 I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
609 I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
611 I915_WRITE(PIPEB_GMCH_DATA_M,
612 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
614 I915_WRITE(PIPEB_GMCH_DATA_N,
616 I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
617 I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
623 intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
624 struct drm_display_mode *adjusted_mode)
626 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
627 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
628 struct drm_crtc *crtc = intel_encoder->enc.crtc;
629 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
631 dp_priv->DP = (DP_LINK_TRAIN_OFF |
637 switch (dp_priv->lane_count) {
639 dp_priv->DP |= DP_PORT_WIDTH_1;
642 dp_priv->DP |= DP_PORT_WIDTH_2;
645 dp_priv->DP |= DP_PORT_WIDTH_4;
648 if (dp_priv->has_audio)
649 dp_priv->DP |= DP_AUDIO_OUTPUT_ENABLE;
651 memset(dp_priv->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
652 dp_priv->link_configuration[0] = dp_priv->link_bw;
653 dp_priv->link_configuration[1] = dp_priv->lane_count;
656 * Check for DPCD version > 1.1,
657 * enable enahanced frame stuff in that case
659 if (dp_priv->dpcd[0] >= 0x11) {
660 dp_priv->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
661 dp_priv->DP |= DP_ENHANCED_FRAMING;
664 if (intel_crtc->pipe == 1)
665 dp_priv->DP |= DP_PIPEB_SELECT;
667 if (IS_eDP(intel_encoder)) {
668 /* don't miss out required setting for eDP */
669 dp_priv->DP |= DP_PLL_ENABLE;
670 if (adjusted_mode->clock < 200000)
671 dp_priv->DP |= DP_PLL_FREQ_160MHZ;
673 dp_priv->DP |= DP_PLL_FREQ_270MHZ;
677 static void ironlake_edp_backlight_on (struct drm_device *dev)
679 struct drm_i915_private *dev_priv = dev->dev_private;
683 pp = I915_READ(PCH_PP_CONTROL);
684 pp |= EDP_BLC_ENABLE;
685 I915_WRITE(PCH_PP_CONTROL, pp);
688 static void ironlake_edp_backlight_off (struct drm_device *dev)
690 struct drm_i915_private *dev_priv = dev->dev_private;
694 pp = I915_READ(PCH_PP_CONTROL);
695 pp &= ~EDP_BLC_ENABLE;
696 I915_WRITE(PCH_PP_CONTROL, pp);
700 intel_dp_dpms(struct drm_encoder *encoder, int mode)
702 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
703 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
704 struct drm_device *dev = intel_encoder->base.dev;
705 struct drm_i915_private *dev_priv = dev->dev_private;
706 uint32_t dp_reg = I915_READ(dp_priv->output_reg);
708 if (mode != DRM_MODE_DPMS_ON) {
709 if (dp_reg & DP_PORT_EN) {
710 intel_dp_link_down(intel_encoder, dp_priv->DP);
711 if (IS_eDP(intel_encoder))
712 ironlake_edp_backlight_off(dev);
715 if (!(dp_reg & DP_PORT_EN)) {
716 intel_dp_link_train(intel_encoder, dp_priv->DP, dp_priv->link_configuration);
717 if (IS_eDP(intel_encoder))
718 ironlake_edp_backlight_on(dev);
721 dp_priv->dpms_mode = mode;
725 * Fetch AUX CH registers 0x202 - 0x207 which contain
726 * link status information
729 intel_dp_get_link_status(struct intel_encoder *intel_encoder,
730 uint8_t link_status[DP_LINK_STATUS_SIZE])
734 ret = intel_dp_aux_native_read(intel_encoder,
736 link_status, DP_LINK_STATUS_SIZE);
737 if (ret != DP_LINK_STATUS_SIZE)
743 intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
746 return link_status[r - DP_LANE0_1_STATUS];
750 intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
753 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
754 int s = ((lane & 1) ?
755 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
756 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
757 uint8_t l = intel_dp_link_status(link_status, i);
759 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
763 intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
766 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
767 int s = ((lane & 1) ?
768 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
769 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
770 uint8_t l = intel_dp_link_status(link_status, i);
772 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
777 static char *voltage_names[] = {
778 "0.4V", "0.6V", "0.8V", "1.2V"
780 static char *pre_emph_names[] = {
781 "0dB", "3.5dB", "6dB", "9.5dB"
783 static char *link_train_names[] = {
784 "pattern 1", "pattern 2", "idle", "off"
789 * These are source-specific values; current Intel hardware supports
790 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
792 #define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
795 intel_dp_pre_emphasis_max(uint8_t voltage_swing)
797 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
798 case DP_TRAIN_VOLTAGE_SWING_400:
799 return DP_TRAIN_PRE_EMPHASIS_6;
800 case DP_TRAIN_VOLTAGE_SWING_600:
801 return DP_TRAIN_PRE_EMPHASIS_6;
802 case DP_TRAIN_VOLTAGE_SWING_800:
803 return DP_TRAIN_PRE_EMPHASIS_3_5;
804 case DP_TRAIN_VOLTAGE_SWING_1200:
806 return DP_TRAIN_PRE_EMPHASIS_0;
811 intel_get_adjust_train(struct intel_encoder *intel_encoder,
812 uint8_t link_status[DP_LINK_STATUS_SIZE],
814 uint8_t train_set[4])
820 for (lane = 0; lane < lane_count; lane++) {
821 uint8_t this_v = intel_get_adjust_request_voltage(link_status, lane);
822 uint8_t this_p = intel_get_adjust_request_pre_emphasis(link_status, lane);
830 if (v >= I830_DP_VOLTAGE_MAX)
831 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
833 if (p >= intel_dp_pre_emphasis_max(v))
834 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
836 for (lane = 0; lane < 4; lane++)
837 train_set[lane] = v | p;
841 intel_dp_signal_levels(uint8_t train_set, int lane_count)
843 uint32_t signal_levels = 0;
845 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
846 case DP_TRAIN_VOLTAGE_SWING_400:
848 signal_levels |= DP_VOLTAGE_0_4;
850 case DP_TRAIN_VOLTAGE_SWING_600:
851 signal_levels |= DP_VOLTAGE_0_6;
853 case DP_TRAIN_VOLTAGE_SWING_800:
854 signal_levels |= DP_VOLTAGE_0_8;
856 case DP_TRAIN_VOLTAGE_SWING_1200:
857 signal_levels |= DP_VOLTAGE_1_2;
860 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
861 case DP_TRAIN_PRE_EMPHASIS_0:
863 signal_levels |= DP_PRE_EMPHASIS_0;
865 case DP_TRAIN_PRE_EMPHASIS_3_5:
866 signal_levels |= DP_PRE_EMPHASIS_3_5;
868 case DP_TRAIN_PRE_EMPHASIS_6:
869 signal_levels |= DP_PRE_EMPHASIS_6;
871 case DP_TRAIN_PRE_EMPHASIS_9_5:
872 signal_levels |= DP_PRE_EMPHASIS_9_5;
875 return signal_levels;
879 intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
882 int i = DP_LANE0_1_STATUS + (lane >> 1);
883 int s = (lane & 1) * 4;
884 uint8_t l = intel_dp_link_status(link_status, i);
886 return (l >> s) & 0xf;
889 /* Check for clock recovery is done on all channels */
891 intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
896 for (lane = 0; lane < lane_count; lane++) {
897 lane_status = intel_get_lane_status(link_status, lane);
898 if ((lane_status & DP_LANE_CR_DONE) == 0)
904 /* Check to see if channel eq is done on all channels */
905 #define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
906 DP_LANE_CHANNEL_EQ_DONE|\
907 DP_LANE_SYMBOL_LOCKED)
909 intel_channel_eq_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
915 lane_align = intel_dp_link_status(link_status,
916 DP_LANE_ALIGN_STATUS_UPDATED);
917 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
919 for (lane = 0; lane < lane_count; lane++) {
920 lane_status = intel_get_lane_status(link_status, lane);
921 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
928 intel_dp_set_link_train(struct intel_encoder *intel_encoder,
929 uint32_t dp_reg_value,
930 uint8_t dp_train_pat,
931 uint8_t train_set[4],
934 struct drm_device *dev = intel_encoder->base.dev;
935 struct drm_i915_private *dev_priv = dev->dev_private;
936 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
939 I915_WRITE(dp_priv->output_reg, dp_reg_value);
940 POSTING_READ(dp_priv->output_reg);
942 intel_wait_for_vblank(dev);
944 intel_dp_aux_native_write_1(intel_encoder,
945 DP_TRAINING_PATTERN_SET,
948 ret = intel_dp_aux_native_write(intel_encoder,
949 DP_TRAINING_LANE0_SET, train_set, 4);
957 intel_dp_link_train(struct intel_encoder *intel_encoder, uint32_t DP,
958 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE])
960 struct drm_device *dev = intel_encoder->base.dev;
961 struct drm_i915_private *dev_priv = dev->dev_private;
962 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
963 uint8_t train_set[4];
964 uint8_t link_status[DP_LINK_STATUS_SIZE];
967 bool clock_recovery = false;
968 bool channel_eq = false;
972 /* Write the link configuration data */
973 intel_dp_aux_native_write(intel_encoder, 0x100,
974 link_configuration, DP_LINK_CONFIGURATION_SIZE);
977 DP &= ~DP_LINK_TRAIN_MASK;
978 memset(train_set, 0, 4);
981 clock_recovery = false;
983 /* Use train_set[0] to set the voltage and pre emphasis values */
984 uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
985 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
987 if (!intel_dp_set_link_train(intel_encoder, DP | DP_LINK_TRAIN_PAT_1,
988 DP_TRAINING_PATTERN_1, train_set, first))
991 /* Set training pattern 1 */
994 if (!intel_dp_get_link_status(intel_encoder, link_status))
997 if (intel_clock_recovery_ok(link_status, dp_priv->lane_count)) {
998 clock_recovery = true;
1002 /* Check to see if we've tried the max voltage */
1003 for (i = 0; i < dp_priv->lane_count; i++)
1004 if ((train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1006 if (i == dp_priv->lane_count)
1009 /* Check to see if we've tried the same voltage 5 times */
1010 if ((train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1016 voltage = train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1018 /* Compute new train_set as requested by target */
1019 intel_get_adjust_train(intel_encoder, link_status, dp_priv->lane_count, train_set);
1022 /* channel equalization */
1026 /* Use train_set[0] to set the voltage and pre emphasis values */
1027 uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
1028 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1030 /* channel eq pattern */
1031 if (!intel_dp_set_link_train(intel_encoder, DP | DP_LINK_TRAIN_PAT_2,
1032 DP_TRAINING_PATTERN_2, train_set,
1037 if (!intel_dp_get_link_status(intel_encoder, link_status))
1040 if (intel_channel_eq_ok(link_status, dp_priv->lane_count)) {
1049 /* Compute new train_set as requested by target */
1050 intel_get_adjust_train(intel_encoder, link_status, dp_priv->lane_count, train_set);
1054 I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_OFF);
1055 POSTING_READ(dp_priv->output_reg);
1056 intel_dp_aux_native_write_1(intel_encoder,
1057 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1061 intel_dp_link_down(struct intel_encoder *intel_encoder, uint32_t DP)
1063 struct drm_device *dev = intel_encoder->base.dev;
1064 struct drm_i915_private *dev_priv = dev->dev_private;
1065 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
1067 DRM_DEBUG_KMS("\n");
1069 if (IS_eDP(intel_encoder)) {
1070 DP &= ~DP_PLL_ENABLE;
1071 I915_WRITE(dp_priv->output_reg, DP);
1072 POSTING_READ(dp_priv->output_reg);
1076 DP &= ~DP_LINK_TRAIN_MASK;
1077 I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
1078 POSTING_READ(dp_priv->output_reg);
1082 if (IS_eDP(intel_encoder))
1083 DP |= DP_LINK_TRAIN_OFF;
1084 I915_WRITE(dp_priv->output_reg, DP & ~DP_PORT_EN);
1085 POSTING_READ(dp_priv->output_reg);
1089 * According to DP spec
1092 * 2. Configure link according to Receiver Capabilities
1093 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1094 * 4. Check link status on receipt of hot-plug interrupt
1098 intel_dp_check_link_status(struct intel_encoder *intel_encoder)
1100 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
1101 uint8_t link_status[DP_LINK_STATUS_SIZE];
1103 if (!intel_encoder->enc.crtc)
1106 if (!intel_dp_get_link_status(intel_encoder, link_status)) {
1107 intel_dp_link_down(intel_encoder, dp_priv->DP);
1111 if (!intel_channel_eq_ok(link_status, dp_priv->lane_count))
1112 intel_dp_link_train(intel_encoder, dp_priv->DP, dp_priv->link_configuration);
1115 static enum drm_connector_status
1116 ironlake_dp_detect(struct drm_connector *connector)
1118 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1119 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
1120 enum drm_connector_status status;
1122 status = connector_status_disconnected;
1123 if (intel_dp_aux_native_read(intel_encoder,
1124 0x000, dp_priv->dpcd,
1125 sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
1127 if (dp_priv->dpcd[0] != 0)
1128 status = connector_status_connected;
1134 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1136 * \return true if DP port is connected.
1137 * \return false if DP port is disconnected.
1139 static enum drm_connector_status
1140 intel_dp_detect(struct drm_connector *connector)
1142 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1143 struct drm_device *dev = intel_encoder->base.dev;
1144 struct drm_i915_private *dev_priv = dev->dev_private;
1145 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
1147 enum drm_connector_status status;
1149 dp_priv->has_audio = false;
1151 if (HAS_PCH_SPLIT(dev))
1152 return ironlake_dp_detect(connector);
1154 temp = I915_READ(PORT_HOTPLUG_EN);
1156 I915_WRITE(PORT_HOTPLUG_EN,
1158 DPB_HOTPLUG_INT_EN |
1159 DPC_HOTPLUG_INT_EN |
1160 DPD_HOTPLUG_INT_EN);
1162 POSTING_READ(PORT_HOTPLUG_EN);
1164 switch (dp_priv->output_reg) {
1166 bit = DPB_HOTPLUG_INT_STATUS;
1169 bit = DPC_HOTPLUG_INT_STATUS;
1172 bit = DPD_HOTPLUG_INT_STATUS;
1175 return connector_status_unknown;
1178 temp = I915_READ(PORT_HOTPLUG_STAT);
1180 if ((temp & bit) == 0)
1181 return connector_status_disconnected;
1183 status = connector_status_disconnected;
1184 if (intel_dp_aux_native_read(intel_encoder,
1185 0x000, dp_priv->dpcd,
1186 sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
1188 if (dp_priv->dpcd[0] != 0)
1189 status = connector_status_connected;
1194 static int intel_dp_get_modes(struct drm_connector *connector)
1196 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1197 struct drm_device *dev = intel_encoder->base.dev;
1198 struct drm_i915_private *dev_priv = dev->dev_private;
1201 /* We should parse the EDID data and find out if it has an audio sink
1204 ret = intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
1208 /* if eDP has no EDID, try to use fixed panel mode from VBT */
1209 if (IS_eDP(intel_encoder)) {
1210 if (dev_priv->panel_fixed_mode != NULL) {
1211 struct drm_display_mode *mode;
1212 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1213 drm_mode_probed_add(connector, mode);
1221 intel_dp_destroy (struct drm_connector *connector)
1223 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1225 if (intel_encoder->i2c_bus)
1226 intel_i2c_destroy(intel_encoder->i2c_bus);
1227 drm_sysfs_connector_remove(connector);
1228 drm_connector_cleanup(connector);
1229 kfree(intel_encoder);
1232 static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1233 .dpms = intel_dp_dpms,
1234 .mode_fixup = intel_dp_mode_fixup,
1235 .prepare = intel_encoder_prepare,
1236 .mode_set = intel_dp_mode_set,
1237 .commit = intel_encoder_commit,
1240 static const struct drm_connector_funcs intel_dp_connector_funcs = {
1241 .dpms = drm_helper_connector_dpms,
1242 .detect = intel_dp_detect,
1243 .fill_modes = drm_helper_probe_single_connector_modes,
1244 .destroy = intel_dp_destroy,
1247 static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1248 .get_modes = intel_dp_get_modes,
1249 .mode_valid = intel_dp_mode_valid,
1250 .best_encoder = intel_best_encoder,
1253 static void intel_dp_enc_destroy(struct drm_encoder *encoder)
1255 drm_encoder_cleanup(encoder);
1258 static const struct drm_encoder_funcs intel_dp_enc_funcs = {
1259 .destroy = intel_dp_enc_destroy,
1263 intel_dp_hot_plug(struct intel_encoder *intel_encoder)
1265 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
1267 if (dp_priv->dpms_mode == DRM_MODE_DPMS_ON)
1268 intel_dp_check_link_status(intel_encoder);
1272 intel_dp_init(struct drm_device *dev, int output_reg)
1274 struct drm_i915_private *dev_priv = dev->dev_private;
1275 struct drm_connector *connector;
1276 struct intel_encoder *intel_encoder;
1277 struct intel_dp_priv *dp_priv;
1278 const char *name = NULL;
1280 intel_encoder = kcalloc(sizeof(struct intel_encoder) +
1281 sizeof(struct intel_dp_priv), 1, GFP_KERNEL);
1285 dp_priv = (struct intel_dp_priv *)(intel_encoder + 1);
1287 connector = &intel_encoder->base;
1288 drm_connector_init(dev, connector, &intel_dp_connector_funcs,
1289 DRM_MODE_CONNECTOR_DisplayPort);
1290 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1292 if (output_reg == DP_A)
1293 intel_encoder->type = INTEL_OUTPUT_EDP;
1295 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1297 if (output_reg == DP_B || output_reg == PCH_DP_B)
1298 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
1299 else if (output_reg == DP_C || output_reg == PCH_DP_C)
1300 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
1301 else if (output_reg == DP_D || output_reg == PCH_DP_D)
1302 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
1304 if (IS_eDP(intel_encoder))
1305 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
1307 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
1308 connector->interlace_allowed = true;
1309 connector->doublescan_allowed = 0;
1311 dp_priv->intel_encoder = intel_encoder;
1312 dp_priv->output_reg = output_reg;
1313 dp_priv->has_audio = false;
1314 dp_priv->dpms_mode = DRM_MODE_DPMS_ON;
1315 intel_encoder->dev_priv = dp_priv;
1317 drm_encoder_init(dev, &intel_encoder->enc, &intel_dp_enc_funcs,
1318 DRM_MODE_ENCODER_TMDS);
1319 drm_encoder_helper_add(&intel_encoder->enc, &intel_dp_helper_funcs);
1321 drm_mode_connector_attach_encoder(&intel_encoder->base,
1322 &intel_encoder->enc);
1323 drm_sysfs_connector_add(connector);
1325 /* Set up the DDC bus. */
1326 switch (output_reg) {
1332 dev_priv->hotplug_supported_mask |=
1333 HDMIB_HOTPLUG_INT_STATUS;
1338 dev_priv->hotplug_supported_mask |=
1339 HDMIC_HOTPLUG_INT_STATUS;
1344 dev_priv->hotplug_supported_mask |=
1345 HDMID_HOTPLUG_INT_STATUS;
1350 intel_dp_i2c_init(intel_encoder, name);
1352 intel_encoder->ddc_bus = &dp_priv->adapter;
1353 intel_encoder->hot_plug = intel_dp_hot_plug;
1355 if (output_reg == DP_A) {
1356 /* initialize panel mode from VBT if available for eDP */
1357 if (dev_priv->lfp_lvds_vbt_mode) {
1358 dev_priv->panel_fixed_mode =
1359 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1360 if (dev_priv->panel_fixed_mode) {
1361 dev_priv->panel_fixed_mode->type |=
1362 DRM_MODE_TYPE_PREFERRED;
1367 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1368 * 0xd. Failure to do so will result in spurious interrupts being
1369 * generated on the port when a cable is not attached.
1371 if (IS_G4X(dev) && !IS_GM45(dev)) {
1372 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
1373 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);