blob: 9f134d2b7bec4dee348e122ec19ef4ad94271841 [file] [log] [blame]
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Keith Packard <keithp@keithp.com>
25 *
26 */
27
28#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Keith Packarda4fc5ed2009-04-07 16:16:42 -070030#include "drmP.h"
31#include "drm.h"
32#include "drm_crtc.h"
33#include "drm_crtc_helper.h"
34#include "intel_drv.h"
35#include "i915_drm.h"
36#include "i915_drv.h"
Dave Airlieab2c0672009-12-04 10:55:24 +100037#include "drm_dp_helper.h"
Keith Packarda4fc5ed2009-04-07 16:16:42 -070038
Zhao Yakuiae266c92009-11-24 09:48:46 +080039
Keith Packarda4fc5ed2009-04-07 16:16:42 -070040#define DP_LINK_STATUS_SIZE 6
41#define DP_LINK_CHECK_TIMEOUT (10 * 1000)
42
43#define DP_LINK_CONFIGURATION_SIZE 9
44
Chris Wilsonea5b2132010-08-04 13:50:23 +010045struct intel_dp {
46 struct intel_encoder base;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070047 uint32_t output_reg;
48 uint32_t DP;
49 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070050 bool has_audio;
Chris Wilsonf6849602010-09-19 09:29:33 +010051 int force_audio;
Chris Wilsone953fd72011-02-21 22:23:52 +000052 uint32_t color_range;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070053 uint8_t link_bw;
54 uint8_t lane_count;
Adam Jackson9de88e62011-07-12 17:38:02 -040055 uint8_t dpcd[8];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070056 struct i2c_adapter adapter;
57 struct i2c_algo_dp_aux_data algo;
Adam Jacksonf0917372010-07-16 14:46:27 -040058 bool is_pch_edp;
Jesse Barnes33a34e42010-09-08 12:42:02 -070059 uint8_t train_set[4];
60 uint8_t link_status[DP_LINK_STATUS_SIZE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070061};
62
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070063/**
64 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
65 * @intel_dp: DP struct
66 *
67 * If a CPU or PCH DP output is attached to an eDP panel, this function
68 * will return true, and false otherwise.
69 */
70static bool is_edp(struct intel_dp *intel_dp)
71{
72 return intel_dp->base.type == INTEL_OUTPUT_EDP;
73}
74
75/**
76 * is_pch_edp - is the port on the PCH and attached to an eDP panel?
77 * @intel_dp: DP struct
78 *
79 * Returns true if the given DP struct corresponds to a PCH DP port attached
80 * to an eDP panel, false otherwise. Helpful for determining whether we
81 * may need FDI resources for a given DP output or not.
82 */
83static bool is_pch_edp(struct intel_dp *intel_dp)
84{
85 return intel_dp->is_pch_edp;
86}
87
Chris Wilsonea5b2132010-08-04 13:50:23 +010088static struct intel_dp *enc_to_intel_dp(struct drm_encoder *encoder)
89{
Chris Wilson4ef69c72010-09-09 15:14:28 +010090 return container_of(encoder, struct intel_dp, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010091}
Keith Packarda4fc5ed2009-04-07 16:16:42 -070092
Chris Wilsondf0e9242010-09-09 16:20:55 +010093static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
94{
95 return container_of(intel_attached_encoder(connector),
96 struct intel_dp, base);
97}
98
Jesse Barnes814948a2010-10-07 16:01:09 -070099/**
100 * intel_encoder_is_pch_edp - is the given encoder a PCH attached eDP?
101 * @encoder: DRM encoder
102 *
103 * Return true if @encoder corresponds to a PCH attached eDP panel. Needed
104 * by intel_display.c.
105 */
106bool intel_encoder_is_pch_edp(struct drm_encoder *encoder)
107{
108 struct intel_dp *intel_dp;
109
110 if (!encoder)
111 return false;
112
113 intel_dp = enc_to_intel_dp(encoder);
114
115 return is_pch_edp(intel_dp);
116}
117
Jesse Barnes33a34e42010-09-08 12:42:02 -0700118static void intel_dp_start_link_train(struct intel_dp *intel_dp);
119static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100120static void intel_dp_link_down(struct intel_dp *intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700121
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800122void
Eric Anholt21d40d32010-03-25 11:11:14 -0700123intel_edp_link_config (struct intel_encoder *intel_encoder,
Chris Wilsonea5b2132010-08-04 13:50:23 +0100124 int *lane_num, int *link_bw)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800125{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100126 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800127
Chris Wilsonea5b2132010-08-04 13:50:23 +0100128 *lane_num = intel_dp->lane_count;
129 if (intel_dp->link_bw == DP_LINK_BW_1_62)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800130 *link_bw = 162000;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100131 else if (intel_dp->link_bw == DP_LINK_BW_2_7)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800132 *link_bw = 270000;
133}
134
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700135static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100136intel_dp_max_lane_count(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700137{
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700138 int max_lane_count = 4;
139
Jesse Barnes7183dc22011-07-07 11:10:58 -0700140 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
141 max_lane_count = intel_dp->dpcd[DP_MAX_LANE_COUNT] & 0x1f;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700142 switch (max_lane_count) {
143 case 1: case 2: case 4:
144 break;
145 default:
146 max_lane_count = 4;
147 }
148 }
149 return max_lane_count;
150}
151
152static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100153intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700154{
Jesse Barnes7183dc22011-07-07 11:10:58 -0700155 int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700156
157 switch (max_link_bw) {
158 case DP_LINK_BW_1_62:
159 case DP_LINK_BW_2_7:
160 break;
161 default:
162 max_link_bw = DP_LINK_BW_1_62;
163 break;
164 }
165 return max_link_bw;
166}
167
168static int
169intel_dp_link_clock(uint8_t link_bw)
170{
171 if (link_bw == DP_LINK_BW_2_7)
172 return 270000;
173 else
174 return 162000;
175}
176
177/* I think this is a fiction */
178static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100179intel_dp_link_required(struct drm_device *dev, struct intel_dp *intel_dp, int pixel_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700180{
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800181 struct drm_i915_private *dev_priv = dev->dev_private;
182
Jesse Barnes4d926462010-10-07 16:01:07 -0700183 if (is_edp(intel_dp))
Chris Wilson5ceb0f92010-09-24 10:24:28 +0100184 return (pixel_clock * dev_priv->edp.bpp + 7) / 8;
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800185 else
186 return pixel_clock * 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700187}
188
189static int
Dave Airliefe27d532010-06-30 11:46:17 +1000190intel_dp_max_data_rate(int max_link_clock, int max_lanes)
191{
192 return (max_link_clock * max_lanes * 8) / 10;
193}
194
195static int
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700196intel_dp_mode_valid(struct drm_connector *connector,
197 struct drm_display_mode *mode)
198{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100199 struct intel_dp *intel_dp = intel_attached_dp(connector);
Zhao Yakui7de56f42010-07-19 09:43:14 +0100200 struct drm_device *dev = connector->dev;
201 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100202 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
203 int max_lanes = intel_dp_max_lane_count(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700204
Jesse Barnes4d926462010-10-07 16:01:07 -0700205 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Zhao Yakui7de56f42010-07-19 09:43:14 +0100206 if (mode->hdisplay > dev_priv->panel_fixed_mode->hdisplay)
207 return MODE_PANEL;
208
209 if (mode->vdisplay > dev_priv->panel_fixed_mode->vdisplay)
210 return MODE_PANEL;
211 }
212
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300213 /* only refuse the mode on non eDP since we have seen some weird eDP panels
Dave Airliefe27d532010-06-30 11:46:17 +1000214 which are outside spec tolerances but somehow work by magic */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700215 if (!is_edp(intel_dp) &&
Chris Wilsonea5b2132010-08-04 13:50:23 +0100216 (intel_dp_link_required(connector->dev, intel_dp, mode->clock)
Dave Airliefe27d532010-06-30 11:46:17 +1000217 > intel_dp_max_data_rate(max_link_clock, max_lanes)))
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700218 return MODE_CLOCK_HIGH;
219
220 if (mode->clock < 10000)
221 return MODE_CLOCK_LOW;
222
223 return MODE_OK;
224}
225
226static uint32_t
227pack_aux(uint8_t *src, int src_bytes)
228{
229 int i;
230 uint32_t v = 0;
231
232 if (src_bytes > 4)
233 src_bytes = 4;
234 for (i = 0; i < src_bytes; i++)
235 v |= ((uint32_t) src[i]) << ((3-i) * 8);
236 return v;
237}
238
239static void
240unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
241{
242 int i;
243 if (dst_bytes > 4)
244 dst_bytes = 4;
245 for (i = 0; i < dst_bytes; i++)
246 dst[i] = src >> ((3-i) * 8);
247}
248
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700249/* hrawclock is 1/4 the FSB frequency */
250static int
251intel_hrawclk(struct drm_device *dev)
252{
253 struct drm_i915_private *dev_priv = dev->dev_private;
254 uint32_t clkcfg;
255
256 clkcfg = I915_READ(CLKCFG);
257 switch (clkcfg & CLKCFG_FSB_MASK) {
258 case CLKCFG_FSB_400:
259 return 100;
260 case CLKCFG_FSB_533:
261 return 133;
262 case CLKCFG_FSB_667:
263 return 166;
264 case CLKCFG_FSB_800:
265 return 200;
266 case CLKCFG_FSB_1067:
267 return 266;
268 case CLKCFG_FSB_1333:
269 return 333;
270 /* these two are just a guess; one of them might be right */
271 case CLKCFG_FSB_1600:
272 case CLKCFG_FSB_1600_ALT:
273 return 400;
274 default:
275 return 133;
276 }
277}
278
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700279static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100280intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700281 uint8_t *send, int send_bytes,
282 uint8_t *recv, int recv_size)
283{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100284 uint32_t output_reg = intel_dp->output_reg;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100285 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700286 struct drm_i915_private *dev_priv = dev->dev_private;
287 uint32_t ch_ctl = output_reg + 0x10;
288 uint32_t ch_data = ch_ctl + 4;
289 int i;
290 int recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700291 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700292 uint32_t aux_clock_divider;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800293 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700294
295 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700296 * and would like to run at 2MHz. So, take the
297 * hrawclk value and divide by 2 and use that
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700298 *
299 * Note that PCH attached eDP panels should use a 125MHz input
300 * clock divider.
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700301 */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700302 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +0800303 if (IS_GEN6(dev))
304 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
305 else
306 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
307 } else if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500308 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800309 else
310 aux_clock_divider = intel_hrawclk(dev) / 2;
311
Zhenyu Wange3421a12010-04-08 09:43:27 +0800312 if (IS_GEN6(dev))
313 precharge = 3;
314 else
315 precharge = 5;
316
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100317 if (I915_READ(ch_ctl) & DP_AUX_CH_CTL_SEND_BUSY) {
318 DRM_ERROR("dp_aux_ch not started status 0x%08x\n",
319 I915_READ(ch_ctl));
320 return -EBUSY;
321 }
322
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700323 /* Must try at least 3 times according to DP spec */
324 for (try = 0; try < 5; try++) {
325 /* Load the send data into the aux channel data registers */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100326 for (i = 0; i < send_bytes; i += 4)
327 I915_WRITE(ch_data + i,
328 pack_aux(send + i, send_bytes - i));
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700329
330 /* Send the command and wait for it to complete */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100331 I915_WRITE(ch_ctl,
332 DP_AUX_CH_CTL_SEND_BUSY |
333 DP_AUX_CH_CTL_TIME_OUT_400us |
334 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
335 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
336 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
337 DP_AUX_CH_CTL_DONE |
338 DP_AUX_CH_CTL_TIME_OUT_ERROR |
339 DP_AUX_CH_CTL_RECEIVE_ERROR);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700340 for (;;) {
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700341 status = I915_READ(ch_ctl);
342 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
343 break;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100344 udelay(100);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700345 }
346
347 /* Clear done status and any errors */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100348 I915_WRITE(ch_ctl,
349 status |
350 DP_AUX_CH_CTL_DONE |
351 DP_AUX_CH_CTL_TIME_OUT_ERROR |
352 DP_AUX_CH_CTL_RECEIVE_ERROR);
353 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700354 break;
355 }
356
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700357 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700358 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700359 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700360 }
361
362 /* Check for timeout or receive error.
363 * Timeouts occur when the sink is not connected
364 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700365 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700366 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700367 return -EIO;
368 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700369
370 /* Timeouts occur when the device isn't connected, so they're
371 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700372 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800373 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700374 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700375 }
376
377 /* Unload any bytes sent back from the other side */
378 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
379 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700380 if (recv_bytes > recv_size)
381 recv_bytes = recv_size;
382
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100383 for (i = 0; i < recv_bytes; i += 4)
384 unpack_aux(I915_READ(ch_data + i),
385 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700386
387 return recv_bytes;
388}
389
390/* Write data to the aux channel in native mode */
391static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100392intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700393 uint16_t address, uint8_t *send, int send_bytes)
394{
395 int ret;
396 uint8_t msg[20];
397 int msg_bytes;
398 uint8_t ack;
399
400 if (send_bytes > 16)
401 return -1;
402 msg[0] = AUX_NATIVE_WRITE << 4;
403 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800404 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700405 msg[3] = send_bytes - 1;
406 memcpy(&msg[4], send, send_bytes);
407 msg_bytes = send_bytes + 4;
408 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100409 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700410 if (ret < 0)
411 return ret;
412 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
413 break;
414 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
415 udelay(100);
416 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700417 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700418 }
419 return send_bytes;
420}
421
422/* Write a single byte to the aux channel in native mode */
423static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100424intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700425 uint16_t address, uint8_t byte)
426{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100427 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700428}
429
430/* read bytes from a native aux channel */
431static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100432intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700433 uint16_t address, uint8_t *recv, int recv_bytes)
434{
435 uint8_t msg[4];
436 int msg_bytes;
437 uint8_t reply[20];
438 int reply_bytes;
439 uint8_t ack;
440 int ret;
441
442 msg[0] = AUX_NATIVE_READ << 4;
443 msg[1] = address >> 8;
444 msg[2] = address & 0xff;
445 msg[3] = recv_bytes - 1;
446
447 msg_bytes = 4;
448 reply_bytes = recv_bytes + 1;
449
450 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100451 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700452 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700453 if (ret == 0)
454 return -EPROTO;
455 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700456 return ret;
457 ack = reply[0];
458 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
459 memcpy(recv, reply + 1, ret - 1);
460 return ret - 1;
461 }
462 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
463 udelay(100);
464 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700465 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700466 }
467}
468
469static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000470intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
471 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700472{
Dave Airlieab2c0672009-12-04 10:55:24 +1000473 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100474 struct intel_dp *intel_dp = container_of(adapter,
475 struct intel_dp,
476 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000477 uint16_t address = algo_data->address;
478 uint8_t msg[5];
479 uint8_t reply[2];
David Flynn8316f332010-12-08 16:10:21 +0000480 unsigned retry;
Dave Airlieab2c0672009-12-04 10:55:24 +1000481 int msg_bytes;
482 int reply_bytes;
483 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700484
Dave Airlieab2c0672009-12-04 10:55:24 +1000485 /* Set up the command byte */
486 if (mode & MODE_I2C_READ)
487 msg[0] = AUX_I2C_READ << 4;
488 else
489 msg[0] = AUX_I2C_WRITE << 4;
490
491 if (!(mode & MODE_I2C_STOP))
492 msg[0] |= AUX_I2C_MOT << 4;
493
494 msg[1] = address >> 8;
495 msg[2] = address;
496
497 switch (mode) {
498 case MODE_I2C_WRITE:
499 msg[3] = 0;
500 msg[4] = write_byte;
501 msg_bytes = 5;
502 reply_bytes = 1;
503 break;
504 case MODE_I2C_READ:
505 msg[3] = 0;
506 msg_bytes = 4;
507 reply_bytes = 2;
508 break;
509 default:
510 msg_bytes = 3;
511 reply_bytes = 1;
512 break;
513 }
514
David Flynn8316f332010-12-08 16:10:21 +0000515 for (retry = 0; retry < 5; retry++) {
516 ret = intel_dp_aux_ch(intel_dp,
517 msg, msg_bytes,
518 reply, reply_bytes);
Dave Airlieab2c0672009-12-04 10:55:24 +1000519 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000520 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000521 return ret;
522 }
David Flynn8316f332010-12-08 16:10:21 +0000523
524 switch (reply[0] & AUX_NATIVE_REPLY_MASK) {
525 case AUX_NATIVE_REPLY_ACK:
526 /* I2C-over-AUX Reply field is only valid
527 * when paired with AUX ACK.
528 */
529 break;
530 case AUX_NATIVE_REPLY_NACK:
531 DRM_DEBUG_KMS("aux_ch native nack\n");
532 return -EREMOTEIO;
533 case AUX_NATIVE_REPLY_DEFER:
534 udelay(100);
535 continue;
536 default:
537 DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
538 reply[0]);
539 return -EREMOTEIO;
540 }
541
Dave Airlieab2c0672009-12-04 10:55:24 +1000542 switch (reply[0] & AUX_I2C_REPLY_MASK) {
543 case AUX_I2C_REPLY_ACK:
544 if (mode == MODE_I2C_READ) {
545 *read_byte = reply[1];
546 }
547 return reply_bytes - 1;
548 case AUX_I2C_REPLY_NACK:
David Flynn8316f332010-12-08 16:10:21 +0000549 DRM_DEBUG_KMS("aux_i2c nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000550 return -EREMOTEIO;
551 case AUX_I2C_REPLY_DEFER:
David Flynn8316f332010-12-08 16:10:21 +0000552 DRM_DEBUG_KMS("aux_i2c defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000553 udelay(100);
554 break;
555 default:
David Flynn8316f332010-12-08 16:10:21 +0000556 DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
Dave Airlieab2c0672009-12-04 10:55:24 +1000557 return -EREMOTEIO;
558 }
559 }
David Flynn8316f332010-12-08 16:10:21 +0000560
561 DRM_ERROR("too many retries, giving up\n");
562 return -EREMOTEIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700563}
564
565static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100566intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800567 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700568{
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800569 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100570 intel_dp->algo.running = false;
571 intel_dp->algo.address = 0;
572 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700573
Chris Wilsonea5b2132010-08-04 13:50:23 +0100574 memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
575 intel_dp->adapter.owner = THIS_MODULE;
576 intel_dp->adapter.class = I2C_CLASS_DDC;
577 strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
578 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
579 intel_dp->adapter.algo_data = &intel_dp->algo;
580 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
581
582 return i2c_dp_aux_add_bus(&intel_dp->adapter);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700583}
584
585static bool
586intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
587 struct drm_display_mode *adjusted_mode)
588{
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100589 struct drm_device *dev = encoder->dev;
590 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100591 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700592 int lane_count, clock;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100593 int max_lane_count = intel_dp_max_lane_count(intel_dp);
594 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700595 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
596
Jesse Barnes4d926462010-10-07 16:01:07 -0700597 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100598 intel_fixed_panel_mode(dev_priv->panel_fixed_mode, adjusted_mode);
599 intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
600 mode, adjusted_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100601 /*
602 * the mode->clock is used to calculate the Data&Link M/N
603 * of the pipe. For the eDP the fixed clock should be used.
604 */
605 mode->clock = dev_priv->panel_fixed_mode->clock;
606 }
607
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700608 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
609 for (clock = 0; clock <= max_clock; clock++) {
Dave Airliefe27d532010-06-30 11:46:17 +1000610 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700611
Chris Wilsonea5b2132010-08-04 13:50:23 +0100612 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800613 <= link_avail) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100614 intel_dp->link_bw = bws[clock];
615 intel_dp->lane_count = lane_count;
616 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Zhao Yakui28c97732009-10-09 11:39:41 +0800617 DRM_DEBUG_KMS("Display port link bw %02x lane "
618 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100619 intel_dp->link_bw, intel_dp->lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700620 adjusted_mode->clock);
621 return true;
622 }
623 }
624 }
Dave Airliefe27d532010-06-30 11:46:17 +1000625
Chris Wilson3cf2efb2010-11-29 10:09:55 +0000626 if (is_edp(intel_dp)) {
627 /* okay we failed just pick the highest */
628 intel_dp->lane_count = max_lane_count;
629 intel_dp->link_bw = bws[max_clock];
630 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
631 DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
632 "count %d clock %d\n",
633 intel_dp->link_bw, intel_dp->lane_count,
634 adjusted_mode->clock);
635
636 return true;
637 }
638
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700639 return false;
640}
641
642struct intel_dp_m_n {
643 uint32_t tu;
644 uint32_t gmch_m;
645 uint32_t gmch_n;
646 uint32_t link_m;
647 uint32_t link_n;
648};
649
650static void
651intel_reduce_ratio(uint32_t *num, uint32_t *den)
652{
653 while (*num > 0xffffff || *den > 0xffffff) {
654 *num >>= 1;
655 *den >>= 1;
656 }
657}
658
659static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800660intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700661 int nlanes,
662 int pixel_clock,
663 int link_clock,
664 struct intel_dp_m_n *m_n)
665{
666 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800667 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700668 m_n->gmch_n = link_clock * nlanes;
669 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
670 m_n->link_m = pixel_clock;
671 m_n->link_n = link_clock;
672 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
673}
674
675void
676intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
677 struct drm_display_mode *adjusted_mode)
678{
679 struct drm_device *dev = crtc->dev;
680 struct drm_mode_config *mode_config = &dev->mode_config;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800681 struct drm_encoder *encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700682 struct drm_i915_private *dev_priv = dev->dev_private;
683 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Zhao Yakui36e83a12010-06-12 14:32:21 +0800684 int lane_count = 4, bpp = 24;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700685 struct intel_dp_m_n m_n;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800686 int pipe = intel_crtc->pipe;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700687
688 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700689 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700690 */
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800691 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100692 struct intel_dp *intel_dp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700693
Dan Carpenterd8201ab2010-05-07 10:39:00 +0200694 if (encoder->crtc != crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700695 continue;
696
Chris Wilsonea5b2132010-08-04 13:50:23 +0100697 intel_dp = enc_to_intel_dp(encoder);
698 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
699 lane_count = intel_dp->lane_count;
Jesse Barnes51190662010-10-07 16:01:08 -0700700 break;
701 } else if (is_edp(intel_dp)) {
702 lane_count = dev_priv->edp.lanes;
703 bpp = dev_priv->edp.bpp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700704 break;
705 }
706 }
707
708 /*
709 * Compute the GMCH and Link ratios. The '3' here is
710 * the number of bytes_per_pixel post-LUT, which we always
711 * set up for 8-bits of R/G/B, or 3 bytes total.
712 */
Zhao Yakui36e83a12010-06-12 14:32:21 +0800713 intel_dp_compute_m_n(bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700714 mode->clock, adjusted_mode->clock, &m_n);
715
Eric Anholtc619eed2010-01-28 16:45:52 -0800716 if (HAS_PCH_SPLIT(dev)) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800717 I915_WRITE(TRANSDATA_M1(pipe),
718 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
719 m_n.gmch_m);
720 I915_WRITE(TRANSDATA_N1(pipe), m_n.gmch_n);
721 I915_WRITE(TRANSDPLINK_M1(pipe), m_n.link_m);
722 I915_WRITE(TRANSDPLINK_N1(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700723 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800724 I915_WRITE(PIPE_GMCH_DATA_M(pipe),
725 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
726 m_n.gmch_m);
727 I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n.gmch_n);
728 I915_WRITE(PIPE_DP_LINK_M(pipe), m_n.link_m);
729 I915_WRITE(PIPE_DP_LINK_N(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700730 }
731}
732
733static void
734intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
735 struct drm_display_mode *adjusted_mode)
736{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800737 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100738 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +0100739 struct drm_crtc *crtc = intel_dp->base.base.crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700740 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
741
Chris Wilsone953fd72011-02-21 22:23:52 +0000742 intel_dp->DP = DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
743 intel_dp->DP |= intel_dp->color_range;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400744
745 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100746 intel_dp->DP |= DP_SYNC_HS_HIGH;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400747 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100748 intel_dp->DP |= DP_SYNC_VS_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700749
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700750 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100751 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800752 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100753 intel_dp->DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700754
Chris Wilsonea5b2132010-08-04 13:50:23 +0100755 switch (intel_dp->lane_count) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700756 case 1:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100757 intel_dp->DP |= DP_PORT_WIDTH_1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700758 break;
759 case 2:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100760 intel_dp->DP |= DP_PORT_WIDTH_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700761 break;
762 case 4:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100763 intel_dp->DP |= DP_PORT_WIDTH_4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700764 break;
765 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100766 if (intel_dp->has_audio)
767 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700768
Chris Wilsonea5b2132010-08-04 13:50:23 +0100769 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
770 intel_dp->link_configuration[0] = intel_dp->link_bw;
771 intel_dp->link_configuration[1] = intel_dp->lane_count;
Adam Jacksona2cab1b2011-07-12 17:38:05 -0400772 intel_dp->link_configuration[8] = DP_SET_ANSI_8B10B;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700773
774 /*
Adam Jackson9962c922010-05-13 14:45:42 -0400775 * Check for DPCD version > 1.1 and enhanced framing support
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700776 */
Jesse Barnes7183dc22011-07-07 11:10:58 -0700777 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
778 (intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100779 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
780 intel_dp->DP |= DP_ENHANCED_FRAMING;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700781 }
782
Zhenyu Wange3421a12010-04-08 09:43:27 +0800783 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
784 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100785 intel_dp->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800786
Jesse Barnes895692b2010-10-07 16:01:23 -0700787 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800788 /* don't miss out required setting for eDP */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100789 intel_dp->DP |= DP_PLL_ENABLE;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800790 if (adjusted_mode->clock < 200000)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100791 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800792 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100793 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800794 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700795}
796
Jesse Barnes5d613502011-01-24 17:10:54 -0800797static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp)
798{
799 struct drm_device *dev = intel_dp->base.base.dev;
800 struct drm_i915_private *dev_priv = dev->dev_private;
801 u32 pp;
802
803 /*
804 * If the panel wasn't on, make sure there's not a currently
805 * active PP sequence before enabling AUX VDD.
806 */
807 if (!(I915_READ(PCH_PP_STATUS) & PP_ON))
808 msleep(dev_priv->panel_t3);
809
810 pp = I915_READ(PCH_PP_CONTROL);
811 pp |= EDP_FORCE_VDD;
812 I915_WRITE(PCH_PP_CONTROL, pp);
813 POSTING_READ(PCH_PP_CONTROL);
814}
815
816static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp)
817{
818 struct drm_device *dev = intel_dp->base.base.dev;
819 struct drm_i915_private *dev_priv = dev->dev_private;
820 u32 pp;
821
822 pp = I915_READ(PCH_PP_CONTROL);
823 pp &= ~EDP_FORCE_VDD;
824 I915_WRITE(PCH_PP_CONTROL, pp);
825 POSTING_READ(PCH_PP_CONTROL);
826
827 /* Make sure sequencer is idle before allowing subsequent activity */
828 msleep(dev_priv->panel_t12);
829}
830
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700831/* Returns true if the panel was already on when called */
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700832static bool ironlake_edp_panel_on (struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -0700833{
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700834 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes9934c132010-07-22 13:18:19 -0700835 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700836 u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_STATE_ON_IDLE;
Jesse Barnes9934c132010-07-22 13:18:19 -0700837
Chris Wilson913d8d12010-08-07 11:01:35 +0100838 if (I915_READ(PCH_PP_STATUS) & PP_ON)
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700839 return true;
Jesse Barnes9934c132010-07-22 13:18:19 -0700840
841 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700842
843 /* ILK workaround: disable reset around power sequence */
844 pp &= ~PANEL_POWER_RESET;
845 I915_WRITE(PCH_PP_CONTROL, pp);
846 POSTING_READ(PCH_PP_CONTROL);
847
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700848 pp |= PANEL_UNLOCK_REGS | POWER_TARGET_ON;
Jesse Barnes9934c132010-07-22 13:18:19 -0700849 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700850 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700851
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700852 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_on_mask) == idle_on_mask,
853 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100854 DRM_ERROR("panel on wait timed out: 0x%08x\n",
855 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700856
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700857 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700858 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700859 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700860
861 return false;
Jesse Barnes9934c132010-07-22 13:18:19 -0700862}
863
864static void ironlake_edp_panel_off (struct drm_device *dev)
865{
866 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700867 u32 pp, idle_off_mask = PP_ON | PP_SEQUENCE_MASK |
868 PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK;
Jesse Barnes9934c132010-07-22 13:18:19 -0700869
870 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700871
872 /* ILK workaround: disable reset around power sequence */
873 pp &= ~PANEL_POWER_RESET;
874 I915_WRITE(PCH_PP_CONTROL, pp);
875 POSTING_READ(PCH_PP_CONTROL);
876
Jesse Barnes9934c132010-07-22 13:18:19 -0700877 pp &= ~POWER_TARGET_ON;
878 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700879 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700880
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700881 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_off_mask) == 0, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100882 DRM_ERROR("panel off wait timed out: 0x%08x\n",
883 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700884
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700885 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700886 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700887 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700888}
889
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500890static void ironlake_edp_backlight_on (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800891{
892 struct drm_i915_private *dev_priv = dev->dev_private;
893 u32 pp;
894
Zhao Yakui28c97732009-10-09 11:39:41 +0800895 DRM_DEBUG_KMS("\n");
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700896 /*
897 * If we enable the backlight right away following a panel power
898 * on, we may see slight flicker as the panel syncs with the eDP
899 * link. So delay a bit to make sure the image is solid before
900 * allowing it to appear.
901 */
902 msleep(300);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800903 pp = I915_READ(PCH_PP_CONTROL);
904 pp |= EDP_BLC_ENABLE;
905 I915_WRITE(PCH_PP_CONTROL, pp);
906}
907
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500908static void ironlake_edp_backlight_off (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800909{
910 struct drm_i915_private *dev_priv = dev->dev_private;
911 u32 pp;
912
Zhao Yakui28c97732009-10-09 11:39:41 +0800913 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800914 pp = I915_READ(PCH_PP_CONTROL);
915 pp &= ~EDP_BLC_ENABLE;
916 I915_WRITE(PCH_PP_CONTROL, pp);
917}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700918
Jesse Barnesd240f202010-08-13 15:43:26 -0700919static void ironlake_edp_pll_on(struct drm_encoder *encoder)
920{
921 struct drm_device *dev = encoder->dev;
922 struct drm_i915_private *dev_priv = dev->dev_private;
923 u32 dpa_ctl;
924
925 DRM_DEBUG_KMS("\n");
926 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -0700927 dpa_ctl |= DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -0700928 I915_WRITE(DP_A, dpa_ctl);
Jesse Barnes298b0b32010-10-07 16:01:24 -0700929 POSTING_READ(DP_A);
930 udelay(200);
Jesse Barnesd240f202010-08-13 15:43:26 -0700931}
932
933static void ironlake_edp_pll_off(struct drm_encoder *encoder)
934{
935 struct drm_device *dev = encoder->dev;
936 struct drm_i915_private *dev_priv = dev->dev_private;
937 u32 dpa_ctl;
938
939 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -0700940 dpa_ctl &= ~DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -0700941 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +0100942 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -0700943 udelay(200);
944}
945
Jesse Barnesc7ad3812011-07-07 11:11:03 -0700946/* If the sink supports it, try to set the power state appropriately */
947static void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
948{
949 int ret, i;
950
951 /* Should have a valid DPCD by this point */
952 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
953 return;
954
955 if (mode != DRM_MODE_DPMS_ON) {
956 ret = intel_dp_aux_native_write_1(intel_dp, DP_SET_POWER,
957 DP_SET_POWER_D3);
958 if (ret != 1)
959 DRM_DEBUG_DRIVER("failed to write sink power state\n");
960 } else {
961 /*
962 * When turning on, we need to retry for 1ms to give the sink
963 * time to wake up.
964 */
965 for (i = 0; i < 3; i++) {
966 ret = intel_dp_aux_native_write_1(intel_dp,
967 DP_SET_POWER,
968 DP_SET_POWER_D0);
969 if (ret == 1)
970 break;
971 msleep(1);
972 }
973 }
974}
975
Jesse Barnesd240f202010-08-13 15:43:26 -0700976static void intel_dp_prepare(struct drm_encoder *encoder)
977{
978 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
979 struct drm_device *dev = encoder->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -0700980
Jesse Barnesc7ad3812011-07-07 11:11:03 -0700981 /* Wake up the sink first */
982 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
983
Jesse Barnes4d926462010-10-07 16:01:07 -0700984 if (is_edp(intel_dp)) {
Jesse Barnesd240f202010-08-13 15:43:26 -0700985 ironlake_edp_backlight_off(dev);
Jesse Barnes5d613502011-01-24 17:10:54 -0800986 ironlake_edp_panel_off(dev);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700987 if (!is_pch_edp(intel_dp))
988 ironlake_edp_pll_on(encoder);
989 else
990 ironlake_edp_pll_off(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -0700991 }
Jesse Barnes736085b2010-10-08 10:35:55 -0700992 intel_dp_link_down(intel_dp);
Jesse Barnesd240f202010-08-13 15:43:26 -0700993}
994
995static void intel_dp_commit(struct drm_encoder *encoder)
996{
997 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
998 struct drm_device *dev = encoder->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -0700999
Jesse Barnes5d613502011-01-24 17:10:54 -08001000 if (is_edp(intel_dp))
1001 ironlake_edp_panel_vdd_on(intel_dp);
1002
Jesse Barnes33a34e42010-09-08 12:42:02 -07001003 intel_dp_start_link_train(intel_dp);
1004
Jesse Barnes5d613502011-01-24 17:10:54 -08001005 if (is_edp(intel_dp)) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001006 ironlake_edp_panel_on(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001007 ironlake_edp_panel_vdd_off(intel_dp);
1008 }
Jesse Barnes33a34e42010-09-08 12:42:02 -07001009
1010 intel_dp_complete_link_train(intel_dp);
1011
Jesse Barnes4d926462010-10-07 16:01:07 -07001012 if (is_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -07001013 ironlake_edp_backlight_on(dev);
1014}
1015
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001016static void
1017intel_dp_dpms(struct drm_encoder *encoder, int mode)
1018{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001019 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001020 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001021 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001022 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001023
1024 if (mode != DRM_MODE_DPMS_ON) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001025 if (is_edp(intel_dp))
Jesse Barnes7643a7f2010-08-11 10:06:44 -07001026 ironlake_edp_backlight_off(dev);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001027 intel_dp_sink_dpms(intel_dp, mode);
Jesse Barnes736085b2010-10-08 10:35:55 -07001028 intel_dp_link_down(intel_dp);
Jesse Barnes4d926462010-10-07 16:01:07 -07001029 if (is_edp(intel_dp))
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001030 ironlake_edp_panel_off(dev);
1031 if (is_edp(intel_dp) && !is_pch_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -07001032 ironlake_edp_pll_off(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001033 } else {
Jesse Barnes736085b2010-10-08 10:35:55 -07001034 if (is_edp(intel_dp))
Jesse Barnes5d613502011-01-24 17:10:54 -08001035 ironlake_edp_panel_vdd_on(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001036 intel_dp_sink_dpms(intel_dp, mode);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001037 if (!(dp_reg & DP_PORT_EN)) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001038 intel_dp_start_link_train(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001039 if (is_edp(intel_dp)) {
1040 ironlake_edp_panel_on(intel_dp);
1041 ironlake_edp_panel_vdd_off(intel_dp);
1042 }
Jesse Barnes33a34e42010-09-08 12:42:02 -07001043 intel_dp_complete_link_train(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001044 }
Jesse Barnes736085b2010-10-08 10:35:55 -07001045 if (is_edp(intel_dp))
1046 ironlake_edp_backlight_on(dev);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001047 }
1048}
1049
1050/*
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001051 * Native read with retry for link status and receiver capability reads for
1052 * cases where the sink may still be asleep.
1053 */
1054static bool
1055intel_dp_aux_native_read_retry(struct intel_dp *intel_dp, uint16_t address,
1056 uint8_t *recv, int recv_bytes)
1057{
1058 int ret, i;
1059
1060 /*
1061 * Sinks are *supposed* to come up within 1ms from an off state,
1062 * but we're also supposed to retry 3 times per the spec.
1063 */
1064 for (i = 0; i < 3; i++) {
1065 ret = intel_dp_aux_native_read(intel_dp, address, recv,
1066 recv_bytes);
1067 if (ret == recv_bytes)
1068 return true;
1069 msleep(1);
1070 }
1071
1072 return false;
1073}
1074
1075/*
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001076 * Fetch AUX CH registers 0x202 - 0x207 which contain
1077 * link status information
1078 */
1079static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001080intel_dp_get_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001081{
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001082 return intel_dp_aux_native_read_retry(intel_dp,
1083 DP_LANE0_1_STATUS,
1084 intel_dp->link_status,
1085 DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001086}
1087
1088static uint8_t
1089intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1090 int r)
1091{
1092 return link_status[r - DP_LANE0_1_STATUS];
1093}
1094
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001095static uint8_t
1096intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
1097 int lane)
1098{
1099 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1100 int s = ((lane & 1) ?
1101 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1102 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1103 uint8_t l = intel_dp_link_status(link_status, i);
1104
1105 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1106}
1107
1108static uint8_t
1109intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1110 int lane)
1111{
1112 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1113 int s = ((lane & 1) ?
1114 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1115 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1116 uint8_t l = intel_dp_link_status(link_status, i);
1117
1118 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1119}
1120
1121
1122#if 0
1123static char *voltage_names[] = {
1124 "0.4V", "0.6V", "0.8V", "1.2V"
1125};
1126static char *pre_emph_names[] = {
1127 "0dB", "3.5dB", "6dB", "9.5dB"
1128};
1129static char *link_train_names[] = {
1130 "pattern 1", "pattern 2", "idle", "off"
1131};
1132#endif
1133
1134/*
1135 * These are source-specific values; current Intel hardware supports
1136 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1137 */
1138#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1139
1140static uint8_t
1141intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1142{
1143 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1144 case DP_TRAIN_VOLTAGE_SWING_400:
1145 return DP_TRAIN_PRE_EMPHASIS_6;
1146 case DP_TRAIN_VOLTAGE_SWING_600:
1147 return DP_TRAIN_PRE_EMPHASIS_6;
1148 case DP_TRAIN_VOLTAGE_SWING_800:
1149 return DP_TRAIN_PRE_EMPHASIS_3_5;
1150 case DP_TRAIN_VOLTAGE_SWING_1200:
1151 default:
1152 return DP_TRAIN_PRE_EMPHASIS_0;
1153 }
1154}
1155
1156static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001157intel_get_adjust_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001158{
1159 uint8_t v = 0;
1160 uint8_t p = 0;
1161 int lane;
1162
Jesse Barnes33a34e42010-09-08 12:42:02 -07001163 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1164 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1165 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001166
1167 if (this_v > v)
1168 v = this_v;
1169 if (this_p > p)
1170 p = this_p;
1171 }
1172
1173 if (v >= I830_DP_VOLTAGE_MAX)
1174 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1175
1176 if (p >= intel_dp_pre_emphasis_max(v))
1177 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1178
1179 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001180 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001181}
1182
1183static uint32_t
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001184intel_dp_signal_levels(uint8_t train_set, int lane_count)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001185{
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001186 uint32_t signal_levels = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001187
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001188 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001189 case DP_TRAIN_VOLTAGE_SWING_400:
1190 default:
1191 signal_levels |= DP_VOLTAGE_0_4;
1192 break;
1193 case DP_TRAIN_VOLTAGE_SWING_600:
1194 signal_levels |= DP_VOLTAGE_0_6;
1195 break;
1196 case DP_TRAIN_VOLTAGE_SWING_800:
1197 signal_levels |= DP_VOLTAGE_0_8;
1198 break;
1199 case DP_TRAIN_VOLTAGE_SWING_1200:
1200 signal_levels |= DP_VOLTAGE_1_2;
1201 break;
1202 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001203 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001204 case DP_TRAIN_PRE_EMPHASIS_0:
1205 default:
1206 signal_levels |= DP_PRE_EMPHASIS_0;
1207 break;
1208 case DP_TRAIN_PRE_EMPHASIS_3_5:
1209 signal_levels |= DP_PRE_EMPHASIS_3_5;
1210 break;
1211 case DP_TRAIN_PRE_EMPHASIS_6:
1212 signal_levels |= DP_PRE_EMPHASIS_6;
1213 break;
1214 case DP_TRAIN_PRE_EMPHASIS_9_5:
1215 signal_levels |= DP_PRE_EMPHASIS_9_5;
1216 break;
1217 }
1218 return signal_levels;
1219}
1220
Zhenyu Wange3421a12010-04-08 09:43:27 +08001221/* Gen6's DP voltage swing and pre-emphasis control */
1222static uint32_t
1223intel_gen6_edp_signal_levels(uint8_t train_set)
1224{
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001225 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1226 DP_TRAIN_PRE_EMPHASIS_MASK);
1227 switch (signal_levels) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001228 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001229 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
1230 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
1231 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
1232 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001233 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001234 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
1235 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001236 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001237 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
1238 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001239 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001240 case DP_TRAIN_VOLTAGE_SWING_1200 | DP_TRAIN_PRE_EMPHASIS_0:
1241 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001242 default:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001243 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
1244 "0x%x\n", signal_levels);
1245 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001246 }
1247}
1248
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001249static uint8_t
1250intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1251 int lane)
1252{
1253 int i = DP_LANE0_1_STATUS + (lane >> 1);
1254 int s = (lane & 1) * 4;
1255 uint8_t l = intel_dp_link_status(link_status, i);
1256
1257 return (l >> s) & 0xf;
1258}
1259
1260/* Check for clock recovery is done on all channels */
1261static bool
1262intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1263{
1264 int lane;
1265 uint8_t lane_status;
1266
1267 for (lane = 0; lane < lane_count; lane++) {
1268 lane_status = intel_get_lane_status(link_status, lane);
1269 if ((lane_status & DP_LANE_CR_DONE) == 0)
1270 return false;
1271 }
1272 return true;
1273}
1274
1275/* Check to see if channel eq is done on all channels */
1276#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1277 DP_LANE_CHANNEL_EQ_DONE|\
1278 DP_LANE_SYMBOL_LOCKED)
1279static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001280intel_channel_eq_ok(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001281{
1282 uint8_t lane_align;
1283 uint8_t lane_status;
1284 int lane;
1285
Jesse Barnes33a34e42010-09-08 12:42:02 -07001286 lane_align = intel_dp_link_status(intel_dp->link_status,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001287 DP_LANE_ALIGN_STATUS_UPDATED);
1288 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1289 return false;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001290 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1291 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001292 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1293 return false;
1294 }
1295 return true;
1296}
1297
1298static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001299intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001300 uint32_t dp_reg_value,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001301 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001302{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001303 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001304 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001305 int ret;
1306
Chris Wilsonea5b2132010-08-04 13:50:23 +01001307 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1308 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001309
Chris Wilsonea5b2132010-08-04 13:50:23 +01001310 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001311 DP_TRAINING_PATTERN_SET,
1312 dp_train_pat);
1313
Chris Wilsonea5b2132010-08-04 13:50:23 +01001314 ret = intel_dp_aux_native_write(intel_dp,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001315 DP_TRAINING_LANE0_SET,
1316 intel_dp->train_set, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001317 if (ret != 4)
1318 return false;
1319
1320 return true;
1321}
1322
Jesse Barnes33a34e42010-09-08 12:42:02 -07001323/* Enable corresponding port and start training pattern 1 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001324static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001325intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001326{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001327 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001328 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson58e10eb2010-10-03 10:56:11 +01001329 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001330 int i;
1331 uint8_t voltage;
1332 bool clock_recovery = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001333 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001334 u32 reg;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001335 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001336
Adam Jacksone8519462011-07-21 17:48:38 -04001337 /*
1338 * On CPT we have to enable the port in training pattern 1, which
1339 * will happen below in intel_dp_set_link_train. Otherwise, enable
1340 * the port and wait for it to become active.
1341 */
1342 if (!HAS_PCH_CPT(dev)) {
1343 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1344 POSTING_READ(intel_dp->output_reg);
1345 intel_wait_for_vblank(dev, intel_crtc->pipe);
1346 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001347
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001348 /* Write the link configuration data */
1349 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1350 intel_dp->link_configuration,
1351 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001352
1353 DP |= DP_PORT_EN;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001354 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001355 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1356 else
1357 DP &= ~DP_LINK_TRAIN_MASK;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001358 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001359 voltage = 0xff;
1360 tries = 0;
1361 clock_recovery = false;
1362 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001363 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001364 uint32_t signal_levels;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001365 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001366 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001367 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1368 } else {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001369 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001370 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1371 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001372
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001373 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001374 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1375 else
1376 reg = DP | DP_LINK_TRAIN_PAT_1;
1377
Chris Wilsonea5b2132010-08-04 13:50:23 +01001378 if (!intel_dp_set_link_train(intel_dp, reg,
Adam Jackson81055852011-07-21 17:48:37 -04001379 DP_TRAINING_PATTERN_1 |
1380 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001381 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001382 /* Set training pattern 1 */
1383
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001384 udelay(100);
1385 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001386 break;
1387
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001388 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1389 clock_recovery = true;
1390 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001391 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001392
1393 /* Check to see if we've tried the max voltage */
1394 for (i = 0; i < intel_dp->lane_count; i++)
1395 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1396 break;
1397 if (i == intel_dp->lane_count)
1398 break;
1399
1400 /* Check to see if we've tried the same voltage 5 times */
1401 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1402 ++tries;
1403 if (tries == 5)
1404 break;
1405 } else
1406 tries = 0;
1407 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1408
1409 /* Compute new intel_dp->train_set as requested by target */
1410 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001411 }
1412
Jesse Barnes33a34e42010-09-08 12:42:02 -07001413 intel_dp->DP = DP;
1414}
1415
1416static void
1417intel_dp_complete_link_train(struct intel_dp *intel_dp)
1418{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001419 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001420 struct drm_i915_private *dev_priv = dev->dev_private;
1421 bool channel_eq = false;
Jesse Barnes37f80972011-01-05 14:45:24 -08001422 int tries, cr_tries;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001423 u32 reg;
1424 uint32_t DP = intel_dp->DP;
1425
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001426 /* channel equalization */
1427 tries = 0;
Jesse Barnes37f80972011-01-05 14:45:24 -08001428 cr_tries = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001429 channel_eq = false;
1430 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001431 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001432 uint32_t signal_levels;
1433
Jesse Barnes37f80972011-01-05 14:45:24 -08001434 if (cr_tries > 5) {
1435 DRM_ERROR("failed to train DP, aborting\n");
1436 intel_dp_link_down(intel_dp);
1437 break;
1438 }
1439
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001440 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001441 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001442 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1443 } else {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001444 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001445 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1446 }
1447
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001448 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001449 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1450 else
1451 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001452
1453 /* channel eq pattern */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001454 if (!intel_dp_set_link_train(intel_dp, reg,
Adam Jackson81055852011-07-21 17:48:37 -04001455 DP_TRAINING_PATTERN_2 |
1456 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001457 break;
1458
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001459 udelay(400);
1460 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001461 break;
Jesse Barnes869184a2010-10-07 16:01:22 -07001462
Jesse Barnes37f80972011-01-05 14:45:24 -08001463 /* Make sure clock is still ok */
1464 if (!intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1465 intel_dp_start_link_train(intel_dp);
1466 cr_tries++;
1467 continue;
1468 }
1469
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001470 if (intel_channel_eq_ok(intel_dp)) {
1471 channel_eq = true;
1472 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001473 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001474
Jesse Barnes37f80972011-01-05 14:45:24 -08001475 /* Try 5 times, then try clock recovery if that fails */
1476 if (tries > 5) {
1477 intel_dp_link_down(intel_dp);
1478 intel_dp_start_link_train(intel_dp);
1479 tries = 0;
1480 cr_tries++;
1481 continue;
1482 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001483
1484 /* Compute new intel_dp->train_set as requested by target */
1485 intel_get_adjust_train(intel_dp);
1486 ++tries;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001487 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001488
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001489 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001490 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1491 else
1492 reg = DP | DP_LINK_TRAIN_OFF;
1493
Chris Wilsonea5b2132010-08-04 13:50:23 +01001494 I915_WRITE(intel_dp->output_reg, reg);
1495 POSTING_READ(intel_dp->output_reg);
1496 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001497 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1498}
1499
1500static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001501intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001502{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001503 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001504 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001505 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001506
Chris Wilson1b39d6f2010-12-06 11:20:45 +00001507 if ((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0)
1508 return;
1509
Zhao Yakui28c97732009-10-09 11:39:41 +08001510 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001511
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001512 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001513 DP &= ~DP_PLL_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001514 I915_WRITE(intel_dp->output_reg, DP);
1515 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001516 udelay(100);
1517 }
1518
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001519 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001520 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001521 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001522 } else {
1523 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001524 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001525 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01001526 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001527
Chris Wilsonfe255d02010-09-11 21:37:48 +01001528 msleep(17);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001529
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001530 if (is_edp(intel_dp))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001531 DP |= DP_LINK_TRAIN_OFF;
Eric Anholt5bddd172010-11-18 09:32:59 +08001532
Chris Wilson1b39d6f2010-12-06 11:20:45 +00001533 if (!HAS_PCH_CPT(dev) &&
1534 I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
Chris Wilson31acbcc2011-04-17 06:38:35 +01001535 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1536
Eric Anholt5bddd172010-11-18 09:32:59 +08001537 /* Hardware workaround: leaving our transcoder select
1538 * set to transcoder B while it's off will prevent the
1539 * corresponding HDMI output on transcoder A.
1540 *
1541 * Combine this with another hardware workaround:
1542 * transcoder select bit can only be cleared while the
1543 * port is enabled.
1544 */
1545 DP &= ~DP_PIPEB_SELECT;
1546 I915_WRITE(intel_dp->output_reg, DP);
1547
1548 /* Changes to enable or select take place the vblank
1549 * after being written.
1550 */
Chris Wilson31acbcc2011-04-17 06:38:35 +01001551 if (crtc == NULL) {
1552 /* We can arrive here never having been attached
1553 * to a CRTC, for instance, due to inheriting
1554 * random state from the BIOS.
1555 *
1556 * If the pipe is not running, play safe and
1557 * wait for the clocks to stabilise before
1558 * continuing.
1559 */
1560 POSTING_READ(intel_dp->output_reg);
1561 msleep(50);
1562 } else
1563 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
Eric Anholt5bddd172010-11-18 09:32:59 +08001564 }
1565
Chris Wilsonea5b2132010-08-04 13:50:23 +01001566 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1567 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001568}
1569
Keith Packard26d61aa2011-07-25 20:01:09 -07001570static bool
1571intel_dp_get_dpcd(struct intel_dp *intel_dp)
Keith Packard92fd8fd2011-07-25 19:50:10 -07001572{
Keith Packard92fd8fd2011-07-25 19:50:10 -07001573 if (intel_dp_aux_native_read_retry(intel_dp, 0x000, intel_dp->dpcd,
1574 sizeof (intel_dp->dpcd)) &&
1575 (intel_dp->dpcd[DP_DPCD_REV] != 0)) {
Keith Packard26d61aa2011-07-25 20:01:09 -07001576 return true;
Keith Packard92fd8fd2011-07-25 19:50:10 -07001577 }
1578
Keith Packard26d61aa2011-07-25 20:01:09 -07001579 return false;
Keith Packard92fd8fd2011-07-25 19:50:10 -07001580}
1581
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001582/*
1583 * According to DP spec
1584 * 5.1.2:
1585 * 1. Read DPCD
1586 * 2. Configure link according to Receiver Capabilities
1587 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1588 * 4. Check link status on receipt of hot-plug interrupt
1589 */
1590
1591static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001592intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001593{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001594 if (!intel_dp->base.base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001595 return;
1596
Keith Packard92fd8fd2011-07-25 19:50:10 -07001597 /* Try to read receiver status if the link appears to be up */
Jesse Barnes33a34e42010-09-08 12:42:02 -07001598 if (!intel_dp_get_link_status(intel_dp)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001599 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001600 return;
1601 }
1602
Keith Packard92fd8fd2011-07-25 19:50:10 -07001603 /* Now read the DPCD to see if it's actually running */
Keith Packard26d61aa2011-07-25 20:01:09 -07001604 if (!intel_dp_get_dpcd(intel_dp)) {
Jesse Barnes59cd09e2011-07-07 11:10:59 -07001605 intel_dp_link_down(intel_dp);
1606 return;
1607 }
1608
Jesse Barnes33a34e42010-09-08 12:42:02 -07001609 if (!intel_channel_eq_ok(intel_dp)) {
Keith Packard92fd8fd2011-07-25 19:50:10 -07001610 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
1611 drm_get_encoder_name(&intel_dp->base.base));
Jesse Barnes33a34e42010-09-08 12:42:02 -07001612 intel_dp_start_link_train(intel_dp);
1613 intel_dp_complete_link_train(intel_dp);
1614 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001615}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001616
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001617static enum drm_connector_status
Keith Packard26d61aa2011-07-25 20:01:09 -07001618intel_dp_detect_dpcd(struct intel_dp *intel_dp)
1619{
1620 if (intel_dp_get_dpcd(intel_dp))
1621 return connector_status_connected;
1622 return connector_status_disconnected;
1623}
1624
1625static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001626ironlake_dp_detect(struct intel_dp *intel_dp)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001627{
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001628 enum drm_connector_status status;
1629
Chris Wilsonfe16d942011-02-12 10:29:38 +00001630 /* Can't disconnect eDP, but you can close the lid... */
1631 if (is_edp(intel_dp)) {
1632 status = intel_panel_detect(intel_dp->base.base.dev);
1633 if (status == connector_status_unknown)
1634 status = connector_status_connected;
1635 return status;
1636 }
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001637
Keith Packard26d61aa2011-07-25 20:01:09 -07001638 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001639}
1640
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001641static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001642g4x_dp_detect(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001643{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001644 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001645 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001646 uint32_t temp, bit;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001647
Chris Wilsonea5b2132010-08-04 13:50:23 +01001648 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001649 case DP_B:
1650 bit = DPB_HOTPLUG_INT_STATUS;
1651 break;
1652 case DP_C:
1653 bit = DPC_HOTPLUG_INT_STATUS;
1654 break;
1655 case DP_D:
1656 bit = DPD_HOTPLUG_INT_STATUS;
1657 break;
1658 default:
1659 return connector_status_unknown;
1660 }
1661
1662 temp = I915_READ(PORT_HOTPLUG_STAT);
1663
1664 if ((temp & bit) == 0)
1665 return connector_status_disconnected;
1666
Keith Packard26d61aa2011-07-25 20:01:09 -07001667 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001668}
1669
1670/**
1671 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1672 *
1673 * \return true if DP port is connected.
1674 * \return false if DP port is disconnected.
1675 */
1676static enum drm_connector_status
1677intel_dp_detect(struct drm_connector *connector, bool force)
1678{
1679 struct intel_dp *intel_dp = intel_attached_dp(connector);
1680 struct drm_device *dev = intel_dp->base.base.dev;
1681 enum drm_connector_status status;
1682 struct edid *edid = NULL;
1683
1684 intel_dp->has_audio = false;
Adam Jackson97cdd712011-07-12 17:38:00 -04001685 memset(intel_dp->dpcd, 0, sizeof(intel_dp->dpcd));
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001686
1687 if (HAS_PCH_SPLIT(dev))
1688 status = ironlake_dp_detect(intel_dp);
1689 else
1690 status = g4x_dp_detect(intel_dp);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04001691
Adam Jacksonac66ae82011-07-12 17:38:03 -04001692 DRM_DEBUG_KMS("DPCD: %02hx%02hx%02hx%02hx%02hx%02hx%02hx%02hx\n",
1693 intel_dp->dpcd[0], intel_dp->dpcd[1], intel_dp->dpcd[2],
1694 intel_dp->dpcd[3], intel_dp->dpcd[4], intel_dp->dpcd[5],
1695 intel_dp->dpcd[6], intel_dp->dpcd[7]);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04001696
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001697 if (status != connector_status_connected)
1698 return status;
1699
Chris Wilsonf6849602010-09-19 09:29:33 +01001700 if (intel_dp->force_audio) {
1701 intel_dp->has_audio = intel_dp->force_audio > 0;
1702 } else {
1703 edid = drm_get_edid(connector, &intel_dp->adapter);
1704 if (edid) {
1705 intel_dp->has_audio = drm_detect_monitor_audio(edid);
1706 connector->display_info.raw_edid = NULL;
1707 kfree(edid);
1708 }
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001709 }
1710
1711 return connector_status_connected;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001712}
1713
1714static int intel_dp_get_modes(struct drm_connector *connector)
1715{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001716 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001717 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001718 struct drm_i915_private *dev_priv = dev->dev_private;
1719 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001720
1721 /* We should parse the EDID data and find out if it has an audio sink
1722 */
1723
Chris Wilsonf899fc62010-07-20 15:44:45 -07001724 ret = intel_ddc_get_modes(connector, &intel_dp->adapter);
Zhao Yakuib9efc482010-07-19 09:43:11 +01001725 if (ret) {
Jesse Barnes4d926462010-10-07 16:01:07 -07001726 if (is_edp(intel_dp) && !dev_priv->panel_fixed_mode) {
Zhao Yakuib9efc482010-07-19 09:43:11 +01001727 struct drm_display_mode *newmode;
1728 list_for_each_entry(newmode, &connector->probed_modes,
1729 head) {
1730 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1731 dev_priv->panel_fixed_mode =
1732 drm_mode_duplicate(dev, newmode);
1733 break;
1734 }
1735 }
1736 }
1737
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001738 return ret;
Zhao Yakuib9efc482010-07-19 09:43:11 +01001739 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001740
1741 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Jesse Barnes4d926462010-10-07 16:01:07 -07001742 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001743 if (dev_priv->panel_fixed_mode != NULL) {
1744 struct drm_display_mode *mode;
1745 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1746 drm_mode_probed_add(connector, mode);
1747 return 1;
1748 }
1749 }
1750 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001751}
1752
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001753static bool
1754intel_dp_detect_audio(struct drm_connector *connector)
1755{
1756 struct intel_dp *intel_dp = intel_attached_dp(connector);
1757 struct edid *edid;
1758 bool has_audio = false;
1759
1760 edid = drm_get_edid(connector, &intel_dp->adapter);
1761 if (edid) {
1762 has_audio = drm_detect_monitor_audio(edid);
1763
1764 connector->display_info.raw_edid = NULL;
1765 kfree(edid);
1766 }
1767
1768 return has_audio;
1769}
1770
Chris Wilsonf6849602010-09-19 09:29:33 +01001771static int
1772intel_dp_set_property(struct drm_connector *connector,
1773 struct drm_property *property,
1774 uint64_t val)
1775{
Chris Wilsone953fd72011-02-21 22:23:52 +00001776 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Chris Wilsonf6849602010-09-19 09:29:33 +01001777 struct intel_dp *intel_dp = intel_attached_dp(connector);
1778 int ret;
1779
1780 ret = drm_connector_property_set_value(connector, property, val);
1781 if (ret)
1782 return ret;
1783
Chris Wilson3f43c482011-05-12 22:17:24 +01001784 if (property == dev_priv->force_audio_property) {
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001785 int i = val;
1786 bool has_audio;
1787
1788 if (i == intel_dp->force_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01001789 return 0;
1790
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001791 intel_dp->force_audio = i;
Chris Wilsonf6849602010-09-19 09:29:33 +01001792
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001793 if (i == 0)
1794 has_audio = intel_dp_detect_audio(connector);
1795 else
1796 has_audio = i > 0;
1797
1798 if (has_audio == intel_dp->has_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01001799 return 0;
1800
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001801 intel_dp->has_audio = has_audio;
Chris Wilsonf6849602010-09-19 09:29:33 +01001802 goto done;
1803 }
1804
Chris Wilsone953fd72011-02-21 22:23:52 +00001805 if (property == dev_priv->broadcast_rgb_property) {
1806 if (val == !!intel_dp->color_range)
1807 return 0;
1808
1809 intel_dp->color_range = val ? DP_COLOR_RANGE_16_235 : 0;
1810 goto done;
1811 }
1812
Chris Wilsonf6849602010-09-19 09:29:33 +01001813 return -EINVAL;
1814
1815done:
1816 if (intel_dp->base.base.crtc) {
1817 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1818 drm_crtc_helper_set_mode(crtc, &crtc->mode,
1819 crtc->x, crtc->y,
1820 crtc->fb);
1821 }
1822
1823 return 0;
1824}
1825
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001826static void
1827intel_dp_destroy (struct drm_connector *connector)
1828{
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001829 drm_sysfs_connector_remove(connector);
1830 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001831 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001832}
1833
Daniel Vetter24d05922010-08-20 18:08:28 +02001834static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1835{
1836 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1837
1838 i2c_del_adapter(&intel_dp->adapter);
1839 drm_encoder_cleanup(encoder);
1840 kfree(intel_dp);
1841}
1842
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001843static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1844 .dpms = intel_dp_dpms,
1845 .mode_fixup = intel_dp_mode_fixup,
Jesse Barnesd240f202010-08-13 15:43:26 -07001846 .prepare = intel_dp_prepare,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001847 .mode_set = intel_dp_mode_set,
Jesse Barnesd240f202010-08-13 15:43:26 -07001848 .commit = intel_dp_commit,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001849};
1850
1851static const struct drm_connector_funcs intel_dp_connector_funcs = {
1852 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001853 .detect = intel_dp_detect,
1854 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilsonf6849602010-09-19 09:29:33 +01001855 .set_property = intel_dp_set_property,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001856 .destroy = intel_dp_destroy,
1857};
1858
1859static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1860 .get_modes = intel_dp_get_modes,
1861 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01001862 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001863};
1864
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001865static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02001866 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001867};
1868
Chris Wilson995b6762010-08-20 13:23:26 +01001869static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001870intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07001871{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001872 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Keith Packardc8110e52009-05-06 11:51:10 -07001873
Jesse Barnes885a5012011-07-07 11:11:01 -07001874 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07001875}
1876
Zhenyu Wange3421a12010-04-08 09:43:27 +08001877/* Return which DP Port should be selected for Transcoder DP control */
1878int
1879intel_trans_dp_port_sel (struct drm_crtc *crtc)
1880{
1881 struct drm_device *dev = crtc->dev;
1882 struct drm_mode_config *mode_config = &dev->mode_config;
1883 struct drm_encoder *encoder;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001884
1885 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001886 struct intel_dp *intel_dp;
1887
Dan Carpenterd8201ab2010-05-07 10:39:00 +02001888 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08001889 continue;
1890
Chris Wilsonea5b2132010-08-04 13:50:23 +01001891 intel_dp = enc_to_intel_dp(encoder);
1892 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
1893 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001894 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001895
Zhenyu Wange3421a12010-04-08 09:43:27 +08001896 return -1;
1897}
1898
Zhao Yakui36e83a12010-06-12 14:32:21 +08001899/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04001900bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08001901{
1902 struct drm_i915_private *dev_priv = dev->dev_private;
1903 struct child_device_config *p_child;
1904 int i;
1905
1906 if (!dev_priv->child_dev_num)
1907 return false;
1908
1909 for (i = 0; i < dev_priv->child_dev_num; i++) {
1910 p_child = dev_priv->child_dev + i;
1911
1912 if (p_child->dvo_port == PORT_IDPD &&
1913 p_child->device_type == DEVICE_TYPE_eDP)
1914 return true;
1915 }
1916 return false;
1917}
1918
Chris Wilsonf6849602010-09-19 09:29:33 +01001919static void
1920intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
1921{
Chris Wilson3f43c482011-05-12 22:17:24 +01001922 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +00001923 intel_attach_broadcast_rgb_property(connector);
Chris Wilsonf6849602010-09-19 09:29:33 +01001924}
1925
Keith Packardc8110e52009-05-06 11:51:10 -07001926void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001927intel_dp_init(struct drm_device *dev, int output_reg)
1928{
1929 struct drm_i915_private *dev_priv = dev->dev_private;
1930 struct drm_connector *connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001931 struct intel_dp *intel_dp;
Eric Anholt21d40d32010-03-25 11:11:14 -07001932 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001933 struct intel_connector *intel_connector;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001934 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04001935 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001936
Chris Wilsonea5b2132010-08-04 13:50:23 +01001937 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
1938 if (!intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001939 return;
1940
Chris Wilson3d3dc142011-02-12 10:33:12 +00001941 intel_dp->output_reg = output_reg;
Chris Wilson3d3dc142011-02-12 10:33:12 +00001942
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001943 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1944 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001945 kfree(intel_dp);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001946 return;
1947 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001948 intel_encoder = &intel_dp->base;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001949
Chris Wilsonea5b2132010-08-04 13:50:23 +01001950 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04001951 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01001952 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04001953
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001954 if (output_reg == DP_A || is_pch_edp(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04001955 type = DRM_MODE_CONNECTOR_eDP;
1956 intel_encoder->type = INTEL_OUTPUT_EDP;
1957 } else {
1958 type = DRM_MODE_CONNECTOR_DisplayPort;
1959 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1960 }
1961
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001962 connector = &intel_connector->base;
Adam Jacksonb3295302010-07-16 14:46:28 -04001963 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001964 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1965
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001966 connector->polled = DRM_CONNECTOR_POLL_HPD;
1967
Zhao Yakui652af9d2009-12-02 10:03:33 +08001968 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07001969 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001970 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07001971 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001972 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07001973 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08001974
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001975 if (is_edp(intel_dp))
Eric Anholt21d40d32010-03-25 11:11:14 -07001976 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08001977
Eric Anholt21d40d32010-03-25 11:11:14 -07001978 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001979 connector->interlace_allowed = true;
1980 connector->doublescan_allowed = 0;
1981
Chris Wilson4ef69c72010-09-09 15:14:28 +01001982 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001983 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001984 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001985
Chris Wilsondf0e9242010-09-09 16:20:55 +01001986 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001987 drm_sysfs_connector_add(connector);
1988
1989 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001990 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001991 case DP_A:
1992 name = "DPDDC-A";
1993 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001994 case DP_B:
1995 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001996 dev_priv->hotplug_supported_mask |=
1997 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001998 name = "DPDDC-B";
1999 break;
2000 case DP_C:
2001 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002002 dev_priv->hotplug_supported_mask |=
2003 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002004 name = "DPDDC-C";
2005 break;
2006 case DP_D:
2007 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002008 dev_priv->hotplug_supported_mask |=
2009 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002010 name = "DPDDC-D";
2011 break;
2012 }
2013
Chris Wilsonea5b2132010-08-04 13:50:23 +01002014 intel_dp_i2c_init(intel_dp, intel_connector, name);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002015
Jesse Barnes89667382010-10-07 16:01:21 -07002016 /* Cache some DPCD data in the eDP case */
2017 if (is_edp(intel_dp)) {
Keith Packard59f3e272011-07-25 20:01:56 -07002018 bool ret;
Jesse Barnes5d613502011-01-24 17:10:54 -08002019 u32 pp_on, pp_div;
Jesse Barnes89667382010-10-07 16:01:21 -07002020
Jesse Barnes5d613502011-01-24 17:10:54 -08002021 pp_on = I915_READ(PCH_PP_ON_DELAYS);
2022 pp_div = I915_READ(PCH_PP_DIVISOR);
2023
2024 /* Get T3 & T12 values (note: VESA not bspec terminology) */
2025 dev_priv->panel_t3 = (pp_on & 0x1fff0000) >> 16;
2026 dev_priv->panel_t3 /= 10; /* t3 in 100us units */
2027 dev_priv->panel_t12 = pp_div & 0xf;
2028 dev_priv->panel_t12 *= 100; /* t12 in 100ms units */
2029
2030 ironlake_edp_panel_vdd_on(intel_dp);
Keith Packard59f3e272011-07-25 20:01:56 -07002031 ret = intel_dp_get_dpcd(intel_dp);
Chris Wilson3d3dc142011-02-12 10:33:12 +00002032 ironlake_edp_panel_vdd_off(intel_dp);
Keith Packard59f3e272011-07-25 20:01:56 -07002033 if (ret) {
Jesse Barnes7183dc22011-07-07 11:10:58 -07002034 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
2035 dev_priv->no_aux_handshake =
2036 intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
Jesse Barnes89667382010-10-07 16:01:21 -07002037 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
2038 } else {
Chris Wilson3d3dc142011-02-12 10:33:12 +00002039 /* if this fails, presume the device is a ghost */
Takashi Iwai48898b02011-03-18 09:06:49 +00002040 DRM_INFO("failed to retrieve link info, disabling eDP\n");
Chris Wilson3d3dc142011-02-12 10:33:12 +00002041 intel_dp_encoder_destroy(&intel_dp->base.base);
Takashi Iwai48898b02011-03-18 09:06:49 +00002042 intel_dp_destroy(&intel_connector->base);
Chris Wilson3d3dc142011-02-12 10:33:12 +00002043 return;
Jesse Barnes89667382010-10-07 16:01:21 -07002044 }
Jesse Barnes89667382010-10-07 16:01:21 -07002045 }
2046
Eric Anholt21d40d32010-03-25 11:11:14 -07002047 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002048
Jesse Barnes4d926462010-10-07 16:01:07 -07002049 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002050 /* initialize panel mode from VBT if available for eDP */
2051 if (dev_priv->lfp_lvds_vbt_mode) {
2052 dev_priv->panel_fixed_mode =
2053 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
2054 if (dev_priv->panel_fixed_mode) {
2055 dev_priv->panel_fixed_mode->type |=
2056 DRM_MODE_TYPE_PREFERRED;
2057 }
2058 }
2059 }
2060
Chris Wilsonf6849602010-09-19 09:29:33 +01002061 intel_dp_add_properties(intel_dp, connector);
2062
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002063 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
2064 * 0xd. Failure to do so will result in spurious interrupts being
2065 * generated on the port when a cable is not attached.
2066 */
2067 if (IS_G4X(dev) && !IS_GM45(dev)) {
2068 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
2069 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
2070 }
2071}