blob: ba72fbcbbbc95bd44fae14cf78c5bbfdef858275 [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 Packardd2b996a2011-07-25 22:37:51 -070053 int dpms_mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070054 uint8_t link_bw;
55 uint8_t lane_count;
Adam Jackson9de88e62011-07-12 17:38:02 -040056 uint8_t dpcd[8];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070057 struct i2c_adapter adapter;
58 struct i2c_algo_dp_aux_data algo;
Adam Jacksonf0917372010-07-16 14:46:27 -040059 bool is_pch_edp;
Jesse Barnes33a34e42010-09-08 12:42:02 -070060 uint8_t train_set[4];
61 uint8_t link_status[DP_LINK_STATUS_SIZE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070062};
63
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070064/**
65 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
66 * @intel_dp: DP struct
67 *
68 * If a CPU or PCH DP output is attached to an eDP panel, this function
69 * will return true, and false otherwise.
70 */
71static bool is_edp(struct intel_dp *intel_dp)
72{
73 return intel_dp->base.type == INTEL_OUTPUT_EDP;
74}
75
76/**
77 * is_pch_edp - is the port on the PCH and attached to an eDP panel?
78 * @intel_dp: DP struct
79 *
80 * Returns true if the given DP struct corresponds to a PCH DP port attached
81 * to an eDP panel, false otherwise. Helpful for determining whether we
82 * may need FDI resources for a given DP output or not.
83 */
84static bool is_pch_edp(struct intel_dp *intel_dp)
85{
86 return intel_dp->is_pch_edp;
87}
88
Chris Wilsonea5b2132010-08-04 13:50:23 +010089static struct intel_dp *enc_to_intel_dp(struct drm_encoder *encoder)
90{
Chris Wilson4ef69c72010-09-09 15:14:28 +010091 return container_of(encoder, struct intel_dp, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010092}
Keith Packarda4fc5ed2009-04-07 16:16:42 -070093
Chris Wilsondf0e9242010-09-09 16:20:55 +010094static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
95{
96 return container_of(intel_attached_encoder(connector),
97 struct intel_dp, base);
98}
99
Jesse Barnes814948a2010-10-07 16:01:09 -0700100/**
101 * intel_encoder_is_pch_edp - is the given encoder a PCH attached eDP?
102 * @encoder: DRM encoder
103 *
104 * Return true if @encoder corresponds to a PCH attached eDP panel. Needed
105 * by intel_display.c.
106 */
107bool intel_encoder_is_pch_edp(struct drm_encoder *encoder)
108{
109 struct intel_dp *intel_dp;
110
111 if (!encoder)
112 return false;
113
114 intel_dp = enc_to_intel_dp(encoder);
115
116 return is_pch_edp(intel_dp);
117}
118
Jesse Barnes33a34e42010-09-08 12:42:02 -0700119static void intel_dp_start_link_train(struct intel_dp *intel_dp);
120static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100121static void intel_dp_link_down(struct intel_dp *intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700122
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800123void
Eric Anholt21d40d32010-03-25 11:11:14 -0700124intel_edp_link_config (struct intel_encoder *intel_encoder,
Chris Wilsonea5b2132010-08-04 13:50:23 +0100125 int *lane_num, int *link_bw)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800126{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100127 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800128
Chris Wilsonea5b2132010-08-04 13:50:23 +0100129 *lane_num = intel_dp->lane_count;
130 if (intel_dp->link_bw == DP_LINK_BW_1_62)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800131 *link_bw = 162000;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100132 else if (intel_dp->link_bw == DP_LINK_BW_2_7)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800133 *link_bw = 270000;
134}
135
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700136static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100137intel_dp_max_lane_count(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700138{
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700139 int max_lane_count = 4;
140
Jesse Barnes7183dc22011-07-07 11:10:58 -0700141 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
142 max_lane_count = intel_dp->dpcd[DP_MAX_LANE_COUNT] & 0x1f;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700143 switch (max_lane_count) {
144 case 1: case 2: case 4:
145 break;
146 default:
147 max_lane_count = 4;
148 }
149 }
150 return max_lane_count;
151}
152
153static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100154intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700155{
Jesse Barnes7183dc22011-07-07 11:10:58 -0700156 int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700157
158 switch (max_link_bw) {
159 case DP_LINK_BW_1_62:
160 case DP_LINK_BW_2_7:
161 break;
162 default:
163 max_link_bw = DP_LINK_BW_1_62;
164 break;
165 }
166 return max_link_bw;
167}
168
169static int
170intel_dp_link_clock(uint8_t link_bw)
171{
172 if (link_bw == DP_LINK_BW_2_7)
173 return 270000;
174 else
175 return 162000;
176}
177
178/* I think this is a fiction */
179static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100180intel_dp_link_required(struct drm_device *dev, struct intel_dp *intel_dp, int pixel_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700181{
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800182 struct drm_i915_private *dev_priv = dev->dev_private;
183
Jesse Barnes4d926462010-10-07 16:01:07 -0700184 if (is_edp(intel_dp))
Chris Wilson5ceb0f92010-09-24 10:24:28 +0100185 return (pixel_clock * dev_priv->edp.bpp + 7) / 8;
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800186 else
187 return pixel_clock * 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700188}
189
190static int
Dave Airliefe27d532010-06-30 11:46:17 +1000191intel_dp_max_data_rate(int max_link_clock, int max_lanes)
192{
193 return (max_link_clock * max_lanes * 8) / 10;
194}
195
196static int
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700197intel_dp_mode_valid(struct drm_connector *connector,
198 struct drm_display_mode *mode)
199{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100200 struct intel_dp *intel_dp = intel_attached_dp(connector);
Zhao Yakui7de56f42010-07-19 09:43:14 +0100201 struct drm_device *dev = connector->dev;
202 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100203 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
204 int max_lanes = intel_dp_max_lane_count(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700205
Jesse Barnes4d926462010-10-07 16:01:07 -0700206 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Zhao Yakui7de56f42010-07-19 09:43:14 +0100207 if (mode->hdisplay > dev_priv->panel_fixed_mode->hdisplay)
208 return MODE_PANEL;
209
210 if (mode->vdisplay > dev_priv->panel_fixed_mode->vdisplay)
211 return MODE_PANEL;
212 }
213
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300214 /* only refuse the mode on non eDP since we have seen some weird eDP panels
Dave Airliefe27d532010-06-30 11:46:17 +1000215 which are outside spec tolerances but somehow work by magic */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700216 if (!is_edp(intel_dp) &&
Chris Wilsonea5b2132010-08-04 13:50:23 +0100217 (intel_dp_link_required(connector->dev, intel_dp, mode->clock)
Dave Airliefe27d532010-06-30 11:46:17 +1000218 > intel_dp_max_data_rate(max_link_clock, max_lanes)))
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700219 return MODE_CLOCK_HIGH;
220
221 if (mode->clock < 10000)
222 return MODE_CLOCK_LOW;
223
224 return MODE_OK;
225}
226
227static uint32_t
228pack_aux(uint8_t *src, int src_bytes)
229{
230 int i;
231 uint32_t v = 0;
232
233 if (src_bytes > 4)
234 src_bytes = 4;
235 for (i = 0; i < src_bytes; i++)
236 v |= ((uint32_t) src[i]) << ((3-i) * 8);
237 return v;
238}
239
240static void
241unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
242{
243 int i;
244 if (dst_bytes > 4)
245 dst_bytes = 4;
246 for (i = 0; i < dst_bytes; i++)
247 dst[i] = src >> ((3-i) * 8);
248}
249
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700250/* hrawclock is 1/4 the FSB frequency */
251static int
252intel_hrawclk(struct drm_device *dev)
253{
254 struct drm_i915_private *dev_priv = dev->dev_private;
255 uint32_t clkcfg;
256
257 clkcfg = I915_READ(CLKCFG);
258 switch (clkcfg & CLKCFG_FSB_MASK) {
259 case CLKCFG_FSB_400:
260 return 100;
261 case CLKCFG_FSB_533:
262 return 133;
263 case CLKCFG_FSB_667:
264 return 166;
265 case CLKCFG_FSB_800:
266 return 200;
267 case CLKCFG_FSB_1067:
268 return 266;
269 case CLKCFG_FSB_1333:
270 return 333;
271 /* these two are just a guess; one of them might be right */
272 case CLKCFG_FSB_1600:
273 case CLKCFG_FSB_1600_ALT:
274 return 400;
275 default:
276 return 133;
277 }
278}
279
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700280static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100281intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700282 uint8_t *send, int send_bytes,
283 uint8_t *recv, int recv_size)
284{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100285 uint32_t output_reg = intel_dp->output_reg;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100286 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700287 struct drm_i915_private *dev_priv = dev->dev_private;
288 uint32_t ch_ctl = output_reg + 0x10;
289 uint32_t ch_data = ch_ctl + 4;
290 int i;
291 int recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700292 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700293 uint32_t aux_clock_divider;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800294 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700295
296 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700297 * and would like to run at 2MHz. So, take the
298 * hrawclk value and divide by 2 and use that
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700299 *
300 * Note that PCH attached eDP panels should use a 125MHz input
301 * clock divider.
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700302 */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700303 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +0800304 if (IS_GEN6(dev))
305 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
306 else
307 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
308 } else if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500309 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800310 else
311 aux_clock_divider = intel_hrawclk(dev) / 2;
312
Zhenyu Wange3421a12010-04-08 09:43:27 +0800313 if (IS_GEN6(dev))
314 precharge = 3;
315 else
316 precharge = 5;
317
Jesse Barnes11bee432011-08-01 15:02:20 -0700318 /* Try to wait for any previous AUX channel activity */
319 for (try = 0; try < 3; try++) {
320 status = I915_READ(ch_ctl);
321 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
322 break;
323 msleep(1);
324 }
325
326 if (try == 3) {
327 WARN(1, "dp_aux_ch not started status 0x%08x\n",
328 I915_READ(ch_ctl));
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100329 return -EBUSY;
330 }
331
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700332 /* Must try at least 3 times according to DP spec */
333 for (try = 0; try < 5; try++) {
334 /* Load the send data into the aux channel data registers */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100335 for (i = 0; i < send_bytes; i += 4)
336 I915_WRITE(ch_data + i,
337 pack_aux(send + i, send_bytes - i));
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700338
339 /* Send the command and wait for it to complete */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100340 I915_WRITE(ch_ctl,
341 DP_AUX_CH_CTL_SEND_BUSY |
342 DP_AUX_CH_CTL_TIME_OUT_400us |
343 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
344 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
345 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
346 DP_AUX_CH_CTL_DONE |
347 DP_AUX_CH_CTL_TIME_OUT_ERROR |
348 DP_AUX_CH_CTL_RECEIVE_ERROR);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700349 for (;;) {
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700350 status = I915_READ(ch_ctl);
351 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
352 break;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100353 udelay(100);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700354 }
355
356 /* Clear done status and any errors */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100357 I915_WRITE(ch_ctl,
358 status |
359 DP_AUX_CH_CTL_DONE |
360 DP_AUX_CH_CTL_TIME_OUT_ERROR |
361 DP_AUX_CH_CTL_RECEIVE_ERROR);
362 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700363 break;
364 }
365
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700366 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700367 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700368 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700369 }
370
371 /* Check for timeout or receive error.
372 * Timeouts occur when the sink is not connected
373 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700374 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700375 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700376 return -EIO;
377 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700378
379 /* Timeouts occur when the device isn't connected, so they're
380 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700381 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800382 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700383 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700384 }
385
386 /* Unload any bytes sent back from the other side */
387 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
388 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700389 if (recv_bytes > recv_size)
390 recv_bytes = recv_size;
391
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100392 for (i = 0; i < recv_bytes; i += 4)
393 unpack_aux(I915_READ(ch_data + i),
394 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700395
396 return recv_bytes;
397}
398
399/* Write data to the aux channel in native mode */
400static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100401intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700402 uint16_t address, uint8_t *send, int send_bytes)
403{
404 int ret;
405 uint8_t msg[20];
406 int msg_bytes;
407 uint8_t ack;
408
409 if (send_bytes > 16)
410 return -1;
411 msg[0] = AUX_NATIVE_WRITE << 4;
412 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800413 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700414 msg[3] = send_bytes - 1;
415 memcpy(&msg[4], send, send_bytes);
416 msg_bytes = send_bytes + 4;
417 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100418 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700419 if (ret < 0)
420 return ret;
421 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
422 break;
423 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
424 udelay(100);
425 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700426 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700427 }
428 return send_bytes;
429}
430
431/* Write a single byte to the aux channel in native mode */
432static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100433intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700434 uint16_t address, uint8_t byte)
435{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100436 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700437}
438
439/* read bytes from a native aux channel */
440static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100441intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700442 uint16_t address, uint8_t *recv, int recv_bytes)
443{
444 uint8_t msg[4];
445 int msg_bytes;
446 uint8_t reply[20];
447 int reply_bytes;
448 uint8_t ack;
449 int ret;
450
451 msg[0] = AUX_NATIVE_READ << 4;
452 msg[1] = address >> 8;
453 msg[2] = address & 0xff;
454 msg[3] = recv_bytes - 1;
455
456 msg_bytes = 4;
457 reply_bytes = recv_bytes + 1;
458
459 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100460 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700461 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700462 if (ret == 0)
463 return -EPROTO;
464 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700465 return ret;
466 ack = reply[0];
467 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
468 memcpy(recv, reply + 1, ret - 1);
469 return ret - 1;
470 }
471 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
472 udelay(100);
473 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700474 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700475 }
476}
477
478static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000479intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
480 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700481{
Dave Airlieab2c0672009-12-04 10:55:24 +1000482 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100483 struct intel_dp *intel_dp = container_of(adapter,
484 struct intel_dp,
485 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000486 uint16_t address = algo_data->address;
487 uint8_t msg[5];
488 uint8_t reply[2];
David Flynn8316f332010-12-08 16:10:21 +0000489 unsigned retry;
Dave Airlieab2c0672009-12-04 10:55:24 +1000490 int msg_bytes;
491 int reply_bytes;
492 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700493
Dave Airlieab2c0672009-12-04 10:55:24 +1000494 /* Set up the command byte */
495 if (mode & MODE_I2C_READ)
496 msg[0] = AUX_I2C_READ << 4;
497 else
498 msg[0] = AUX_I2C_WRITE << 4;
499
500 if (!(mode & MODE_I2C_STOP))
501 msg[0] |= AUX_I2C_MOT << 4;
502
503 msg[1] = address >> 8;
504 msg[2] = address;
505
506 switch (mode) {
507 case MODE_I2C_WRITE:
508 msg[3] = 0;
509 msg[4] = write_byte;
510 msg_bytes = 5;
511 reply_bytes = 1;
512 break;
513 case MODE_I2C_READ:
514 msg[3] = 0;
515 msg_bytes = 4;
516 reply_bytes = 2;
517 break;
518 default:
519 msg_bytes = 3;
520 reply_bytes = 1;
521 break;
522 }
523
David Flynn8316f332010-12-08 16:10:21 +0000524 for (retry = 0; retry < 5; retry++) {
525 ret = intel_dp_aux_ch(intel_dp,
526 msg, msg_bytes,
527 reply, reply_bytes);
Dave Airlieab2c0672009-12-04 10:55:24 +1000528 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000529 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000530 return ret;
531 }
David Flynn8316f332010-12-08 16:10:21 +0000532
533 switch (reply[0] & AUX_NATIVE_REPLY_MASK) {
534 case AUX_NATIVE_REPLY_ACK:
535 /* I2C-over-AUX Reply field is only valid
536 * when paired with AUX ACK.
537 */
538 break;
539 case AUX_NATIVE_REPLY_NACK:
540 DRM_DEBUG_KMS("aux_ch native nack\n");
541 return -EREMOTEIO;
542 case AUX_NATIVE_REPLY_DEFER:
543 udelay(100);
544 continue;
545 default:
546 DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
547 reply[0]);
548 return -EREMOTEIO;
549 }
550
Dave Airlieab2c0672009-12-04 10:55:24 +1000551 switch (reply[0] & AUX_I2C_REPLY_MASK) {
552 case AUX_I2C_REPLY_ACK:
553 if (mode == MODE_I2C_READ) {
554 *read_byte = reply[1];
555 }
556 return reply_bytes - 1;
557 case AUX_I2C_REPLY_NACK:
David Flynn8316f332010-12-08 16:10:21 +0000558 DRM_DEBUG_KMS("aux_i2c nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000559 return -EREMOTEIO;
560 case AUX_I2C_REPLY_DEFER:
David Flynn8316f332010-12-08 16:10:21 +0000561 DRM_DEBUG_KMS("aux_i2c defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000562 udelay(100);
563 break;
564 default:
David Flynn8316f332010-12-08 16:10:21 +0000565 DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
Dave Airlieab2c0672009-12-04 10:55:24 +1000566 return -EREMOTEIO;
567 }
568 }
David Flynn8316f332010-12-08 16:10:21 +0000569
570 DRM_ERROR("too many retries, giving up\n");
571 return -EREMOTEIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700572}
573
574static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100575intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800576 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700577{
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800578 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100579 intel_dp->algo.running = false;
580 intel_dp->algo.address = 0;
581 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700582
Chris Wilsonea5b2132010-08-04 13:50:23 +0100583 memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
584 intel_dp->adapter.owner = THIS_MODULE;
585 intel_dp->adapter.class = I2C_CLASS_DDC;
586 strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
587 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
588 intel_dp->adapter.algo_data = &intel_dp->algo;
589 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
590
591 return i2c_dp_aux_add_bus(&intel_dp->adapter);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700592}
593
594static bool
595intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
596 struct drm_display_mode *adjusted_mode)
597{
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100598 struct drm_device *dev = encoder->dev;
599 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100600 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700601 int lane_count, clock;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100602 int max_lane_count = intel_dp_max_lane_count(intel_dp);
603 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700604 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
605
Jesse Barnes4d926462010-10-07 16:01:07 -0700606 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100607 intel_fixed_panel_mode(dev_priv->panel_fixed_mode, adjusted_mode);
608 intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
609 mode, adjusted_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100610 /*
611 * the mode->clock is used to calculate the Data&Link M/N
612 * of the pipe. For the eDP the fixed clock should be used.
613 */
614 mode->clock = dev_priv->panel_fixed_mode->clock;
615 }
616
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700617 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
618 for (clock = 0; clock <= max_clock; clock++) {
Dave Airliefe27d532010-06-30 11:46:17 +1000619 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700620
Chris Wilsonea5b2132010-08-04 13:50:23 +0100621 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800622 <= link_avail) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100623 intel_dp->link_bw = bws[clock];
624 intel_dp->lane_count = lane_count;
625 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Zhao Yakui28c97732009-10-09 11:39:41 +0800626 DRM_DEBUG_KMS("Display port link bw %02x lane "
627 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100628 intel_dp->link_bw, intel_dp->lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700629 adjusted_mode->clock);
630 return true;
631 }
632 }
633 }
Dave Airliefe27d532010-06-30 11:46:17 +1000634
Chris Wilson3cf2efb2010-11-29 10:09:55 +0000635 if (is_edp(intel_dp)) {
636 /* okay we failed just pick the highest */
637 intel_dp->lane_count = max_lane_count;
638 intel_dp->link_bw = bws[max_clock];
639 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
640 DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
641 "count %d clock %d\n",
642 intel_dp->link_bw, intel_dp->lane_count,
643 adjusted_mode->clock);
644
645 return true;
646 }
647
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700648 return false;
649}
650
651struct intel_dp_m_n {
652 uint32_t tu;
653 uint32_t gmch_m;
654 uint32_t gmch_n;
655 uint32_t link_m;
656 uint32_t link_n;
657};
658
659static void
660intel_reduce_ratio(uint32_t *num, uint32_t *den)
661{
662 while (*num > 0xffffff || *den > 0xffffff) {
663 *num >>= 1;
664 *den >>= 1;
665 }
666}
667
668static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800669intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700670 int nlanes,
671 int pixel_clock,
672 int link_clock,
673 struct intel_dp_m_n *m_n)
674{
675 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800676 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700677 m_n->gmch_n = link_clock * nlanes;
678 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
679 m_n->link_m = pixel_clock;
680 m_n->link_n = link_clock;
681 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
682}
683
684void
685intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
686 struct drm_display_mode *adjusted_mode)
687{
688 struct drm_device *dev = crtc->dev;
689 struct drm_mode_config *mode_config = &dev->mode_config;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800690 struct drm_encoder *encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700691 struct drm_i915_private *dev_priv = dev->dev_private;
692 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Zhao Yakui36e83a12010-06-12 14:32:21 +0800693 int lane_count = 4, bpp = 24;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700694 struct intel_dp_m_n m_n;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800695 int pipe = intel_crtc->pipe;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700696
697 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700698 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700699 */
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800700 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100701 struct intel_dp *intel_dp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700702
Dan Carpenterd8201ab2010-05-07 10:39:00 +0200703 if (encoder->crtc != crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700704 continue;
705
Chris Wilsonea5b2132010-08-04 13:50:23 +0100706 intel_dp = enc_to_intel_dp(encoder);
707 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
708 lane_count = intel_dp->lane_count;
Jesse Barnes51190662010-10-07 16:01:08 -0700709 break;
710 } else if (is_edp(intel_dp)) {
711 lane_count = dev_priv->edp.lanes;
712 bpp = dev_priv->edp.bpp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700713 break;
714 }
715 }
716
717 /*
718 * Compute the GMCH and Link ratios. The '3' here is
719 * the number of bytes_per_pixel post-LUT, which we always
720 * set up for 8-bits of R/G/B, or 3 bytes total.
721 */
Zhao Yakui36e83a12010-06-12 14:32:21 +0800722 intel_dp_compute_m_n(bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700723 mode->clock, adjusted_mode->clock, &m_n);
724
Eric Anholtc619eed2010-01-28 16:45:52 -0800725 if (HAS_PCH_SPLIT(dev)) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800726 I915_WRITE(TRANSDATA_M1(pipe),
727 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
728 m_n.gmch_m);
729 I915_WRITE(TRANSDATA_N1(pipe), m_n.gmch_n);
730 I915_WRITE(TRANSDPLINK_M1(pipe), m_n.link_m);
731 I915_WRITE(TRANSDPLINK_N1(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700732 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800733 I915_WRITE(PIPE_GMCH_DATA_M(pipe),
734 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
735 m_n.gmch_m);
736 I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n.gmch_n);
737 I915_WRITE(PIPE_DP_LINK_M(pipe), m_n.link_m);
738 I915_WRITE(PIPE_DP_LINK_N(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700739 }
740}
741
742static void
743intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
744 struct drm_display_mode *adjusted_mode)
745{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800746 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100747 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +0100748 struct drm_crtc *crtc = intel_dp->base.base.crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700749 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
750
Chris Wilsone953fd72011-02-21 22:23:52 +0000751 intel_dp->DP = DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
752 intel_dp->DP |= intel_dp->color_range;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400753
754 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100755 intel_dp->DP |= DP_SYNC_HS_HIGH;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400756 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100757 intel_dp->DP |= DP_SYNC_VS_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700758
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700759 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100760 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800761 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100762 intel_dp->DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700763
Chris Wilsonea5b2132010-08-04 13:50:23 +0100764 switch (intel_dp->lane_count) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700765 case 1:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100766 intel_dp->DP |= DP_PORT_WIDTH_1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700767 break;
768 case 2:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100769 intel_dp->DP |= DP_PORT_WIDTH_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700770 break;
771 case 4:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100772 intel_dp->DP |= DP_PORT_WIDTH_4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700773 break;
774 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100775 if (intel_dp->has_audio)
776 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700777
Chris Wilsonea5b2132010-08-04 13:50:23 +0100778 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
779 intel_dp->link_configuration[0] = intel_dp->link_bw;
780 intel_dp->link_configuration[1] = intel_dp->lane_count;
Adam Jacksona2cab1b2011-07-12 17:38:05 -0400781 intel_dp->link_configuration[8] = DP_SET_ANSI_8B10B;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700782
783 /*
Adam Jackson9962c922010-05-13 14:45:42 -0400784 * Check for DPCD version > 1.1 and enhanced framing support
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700785 */
Jesse Barnes7183dc22011-07-07 11:10:58 -0700786 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
787 (intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100788 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
789 intel_dp->DP |= DP_ENHANCED_FRAMING;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700790 }
791
Zhenyu Wange3421a12010-04-08 09:43:27 +0800792 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
793 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100794 intel_dp->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800795
Jesse Barnes895692b2010-10-07 16:01:23 -0700796 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800797 /* don't miss out required setting for eDP */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100798 intel_dp->DP |= DP_PLL_ENABLE;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800799 if (adjusted_mode->clock < 200000)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100800 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800801 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100802 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800803 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700804}
805
Jesse Barnes5d613502011-01-24 17:10:54 -0800806static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp)
807{
808 struct drm_device *dev = intel_dp->base.base.dev;
809 struct drm_i915_private *dev_priv = dev->dev_private;
810 u32 pp;
811
812 /*
813 * If the panel wasn't on, make sure there's not a currently
814 * active PP sequence before enabling AUX VDD.
815 */
816 if (!(I915_READ(PCH_PP_STATUS) & PP_ON))
817 msleep(dev_priv->panel_t3);
818
819 pp = I915_READ(PCH_PP_CONTROL);
820 pp |= EDP_FORCE_VDD;
821 I915_WRITE(PCH_PP_CONTROL, pp);
822 POSTING_READ(PCH_PP_CONTROL);
823}
824
825static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp)
826{
827 struct drm_device *dev = intel_dp->base.base.dev;
828 struct drm_i915_private *dev_priv = dev->dev_private;
829 u32 pp;
830
831 pp = I915_READ(PCH_PP_CONTROL);
832 pp &= ~EDP_FORCE_VDD;
833 I915_WRITE(PCH_PP_CONTROL, pp);
834 POSTING_READ(PCH_PP_CONTROL);
835
836 /* Make sure sequencer is idle before allowing subsequent activity */
837 msleep(dev_priv->panel_t12);
838}
839
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700840/* Returns true if the panel was already on when called */
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700841static bool ironlake_edp_panel_on (struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -0700842{
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700843 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes9934c132010-07-22 13:18:19 -0700844 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700845 u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_STATE_ON_IDLE;
Jesse Barnes9934c132010-07-22 13:18:19 -0700846
Chris Wilson913d8d12010-08-07 11:01:35 +0100847 if (I915_READ(PCH_PP_STATUS) & PP_ON)
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700848 return true;
Jesse Barnes9934c132010-07-22 13:18:19 -0700849
850 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700851
852 /* ILK workaround: disable reset around power sequence */
853 pp &= ~PANEL_POWER_RESET;
854 I915_WRITE(PCH_PP_CONTROL, pp);
855 POSTING_READ(PCH_PP_CONTROL);
856
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700857 pp |= PANEL_UNLOCK_REGS | POWER_TARGET_ON;
Jesse Barnes9934c132010-07-22 13:18:19 -0700858 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700859 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700860
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700861 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_on_mask) == idle_on_mask,
862 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100863 DRM_ERROR("panel on wait timed out: 0x%08x\n",
864 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700865
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700866 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700867 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700868 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700869
870 return false;
Jesse Barnes9934c132010-07-22 13:18:19 -0700871}
872
873static void ironlake_edp_panel_off (struct drm_device *dev)
874{
875 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700876 u32 pp, idle_off_mask = PP_ON | PP_SEQUENCE_MASK |
877 PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK;
Jesse Barnes9934c132010-07-22 13:18:19 -0700878
879 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700880
881 /* ILK workaround: disable reset around power sequence */
882 pp &= ~PANEL_POWER_RESET;
883 I915_WRITE(PCH_PP_CONTROL, pp);
884 POSTING_READ(PCH_PP_CONTROL);
885
Jesse Barnes9934c132010-07-22 13:18:19 -0700886 pp &= ~POWER_TARGET_ON;
887 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700888 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700889
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700890 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_off_mask) == 0, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100891 DRM_ERROR("panel off wait timed out: 0x%08x\n",
892 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700893
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700894 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700895 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700896 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700897}
898
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500899static void ironlake_edp_backlight_on (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800900{
901 struct drm_i915_private *dev_priv = dev->dev_private;
902 u32 pp;
903
Zhao Yakui28c97732009-10-09 11:39:41 +0800904 DRM_DEBUG_KMS("\n");
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700905 /*
906 * If we enable the backlight right away following a panel power
907 * on, we may see slight flicker as the panel syncs with the eDP
908 * link. So delay a bit to make sure the image is solid before
909 * allowing it to appear.
910 */
911 msleep(300);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800912 pp = I915_READ(PCH_PP_CONTROL);
913 pp |= EDP_BLC_ENABLE;
914 I915_WRITE(PCH_PP_CONTROL, pp);
915}
916
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500917static void ironlake_edp_backlight_off (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800918{
919 struct drm_i915_private *dev_priv = dev->dev_private;
920 u32 pp;
921
Zhao Yakui28c97732009-10-09 11:39:41 +0800922 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800923 pp = I915_READ(PCH_PP_CONTROL);
924 pp &= ~EDP_BLC_ENABLE;
925 I915_WRITE(PCH_PP_CONTROL, pp);
926}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700927
Jesse Barnesd240f202010-08-13 15:43:26 -0700928static void ironlake_edp_pll_on(struct drm_encoder *encoder)
929{
930 struct drm_device *dev = encoder->dev;
931 struct drm_i915_private *dev_priv = dev->dev_private;
932 u32 dpa_ctl;
933
934 DRM_DEBUG_KMS("\n");
935 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -0700936 dpa_ctl |= DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -0700937 I915_WRITE(DP_A, dpa_ctl);
Jesse Barnes298b0b32010-10-07 16:01:24 -0700938 POSTING_READ(DP_A);
939 udelay(200);
Jesse Barnesd240f202010-08-13 15:43:26 -0700940}
941
942static void ironlake_edp_pll_off(struct drm_encoder *encoder)
943{
944 struct drm_device *dev = encoder->dev;
945 struct drm_i915_private *dev_priv = dev->dev_private;
946 u32 dpa_ctl;
947
948 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -0700949 dpa_ctl &= ~DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -0700950 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +0100951 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -0700952 udelay(200);
953}
954
Jesse Barnesc7ad3812011-07-07 11:11:03 -0700955/* If the sink supports it, try to set the power state appropriately */
956static void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
957{
958 int ret, i;
959
960 /* Should have a valid DPCD by this point */
961 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
962 return;
963
964 if (mode != DRM_MODE_DPMS_ON) {
965 ret = intel_dp_aux_native_write_1(intel_dp, DP_SET_POWER,
966 DP_SET_POWER_D3);
967 if (ret != 1)
968 DRM_DEBUG_DRIVER("failed to write sink power state\n");
969 } else {
970 /*
971 * When turning on, we need to retry for 1ms to give the sink
972 * time to wake up.
973 */
974 for (i = 0; i < 3; i++) {
975 ret = intel_dp_aux_native_write_1(intel_dp,
976 DP_SET_POWER,
977 DP_SET_POWER_D0);
978 if (ret == 1)
979 break;
980 msleep(1);
981 }
982 }
983}
984
Jesse Barnesd240f202010-08-13 15:43:26 -0700985static void intel_dp_prepare(struct drm_encoder *encoder)
986{
987 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
988 struct drm_device *dev = encoder->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -0700989
Jesse Barnesc7ad3812011-07-07 11:11:03 -0700990 /* Wake up the sink first */
991 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
992
Jesse Barnes4d926462010-10-07 16:01:07 -0700993 if (is_edp(intel_dp)) {
Jesse Barnesd240f202010-08-13 15:43:26 -0700994 ironlake_edp_backlight_off(dev);
Jesse Barnes5d613502011-01-24 17:10:54 -0800995 ironlake_edp_panel_off(dev);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700996 if (!is_pch_edp(intel_dp))
997 ironlake_edp_pll_on(encoder);
998 else
999 ironlake_edp_pll_off(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -07001000 }
Jesse Barnes736085b2010-10-08 10:35:55 -07001001 intel_dp_link_down(intel_dp);
Jesse Barnesd240f202010-08-13 15:43:26 -07001002}
1003
1004static void intel_dp_commit(struct drm_encoder *encoder)
1005{
1006 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1007 struct drm_device *dev = encoder->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -07001008
Jesse Barnes5d613502011-01-24 17:10:54 -08001009 if (is_edp(intel_dp))
1010 ironlake_edp_panel_vdd_on(intel_dp);
1011
Jesse Barnes33a34e42010-09-08 12:42:02 -07001012 intel_dp_start_link_train(intel_dp);
1013
Jesse Barnes5d613502011-01-24 17:10:54 -08001014 if (is_edp(intel_dp)) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001015 ironlake_edp_panel_on(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001016 ironlake_edp_panel_vdd_off(intel_dp);
1017 }
Jesse Barnes33a34e42010-09-08 12:42:02 -07001018
1019 intel_dp_complete_link_train(intel_dp);
1020
Jesse Barnes4d926462010-10-07 16:01:07 -07001021 if (is_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -07001022 ironlake_edp_backlight_on(dev);
Keith Packardd2b996a2011-07-25 22:37:51 -07001023
1024 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
Jesse Barnesd240f202010-08-13 15:43:26 -07001025}
1026
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001027static void
1028intel_dp_dpms(struct drm_encoder *encoder, int mode)
1029{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001030 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001031 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001032 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001033 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001034
1035 if (mode != DRM_MODE_DPMS_ON) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001036 if (is_edp(intel_dp))
Jesse Barnes7643a7f2010-08-11 10:06:44 -07001037 ironlake_edp_backlight_off(dev);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001038 intel_dp_sink_dpms(intel_dp, mode);
Jesse Barnes736085b2010-10-08 10:35:55 -07001039 intel_dp_link_down(intel_dp);
Jesse Barnes4d926462010-10-07 16:01:07 -07001040 if (is_edp(intel_dp))
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001041 ironlake_edp_panel_off(dev);
1042 if (is_edp(intel_dp) && !is_pch_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -07001043 ironlake_edp_pll_off(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001044 } else {
Jesse Barnes736085b2010-10-08 10:35:55 -07001045 if (is_edp(intel_dp))
Jesse Barnes5d613502011-01-24 17:10:54 -08001046 ironlake_edp_panel_vdd_on(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001047 intel_dp_sink_dpms(intel_dp, mode);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001048 if (!(dp_reg & DP_PORT_EN)) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001049 intel_dp_start_link_train(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001050 if (is_edp(intel_dp)) {
1051 ironlake_edp_panel_on(intel_dp);
1052 ironlake_edp_panel_vdd_off(intel_dp);
1053 }
Jesse Barnes33a34e42010-09-08 12:42:02 -07001054 intel_dp_complete_link_train(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001055 }
Jesse Barnes736085b2010-10-08 10:35:55 -07001056 if (is_edp(intel_dp))
1057 ironlake_edp_backlight_on(dev);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001058 }
Keith Packardd2b996a2011-07-25 22:37:51 -07001059 intel_dp->dpms_mode = mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001060}
1061
1062/*
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001063 * Native read with retry for link status and receiver capability reads for
1064 * cases where the sink may still be asleep.
1065 */
1066static bool
1067intel_dp_aux_native_read_retry(struct intel_dp *intel_dp, uint16_t address,
1068 uint8_t *recv, int recv_bytes)
1069{
1070 int ret, i;
1071
1072 /*
1073 * Sinks are *supposed* to come up within 1ms from an off state,
1074 * but we're also supposed to retry 3 times per the spec.
1075 */
1076 for (i = 0; i < 3; i++) {
1077 ret = intel_dp_aux_native_read(intel_dp, address, recv,
1078 recv_bytes);
1079 if (ret == recv_bytes)
1080 return true;
1081 msleep(1);
1082 }
1083
1084 return false;
1085}
1086
1087/*
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001088 * Fetch AUX CH registers 0x202 - 0x207 which contain
1089 * link status information
1090 */
1091static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001092intel_dp_get_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001093{
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001094 return intel_dp_aux_native_read_retry(intel_dp,
1095 DP_LANE0_1_STATUS,
1096 intel_dp->link_status,
1097 DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001098}
1099
1100static uint8_t
1101intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1102 int r)
1103{
1104 return link_status[r - DP_LANE0_1_STATUS];
1105}
1106
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001107static uint8_t
1108intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
1109 int lane)
1110{
1111 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1112 int s = ((lane & 1) ?
1113 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1114 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1115 uint8_t l = intel_dp_link_status(link_status, i);
1116
1117 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1118}
1119
1120static uint8_t
1121intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1122 int lane)
1123{
1124 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1125 int s = ((lane & 1) ?
1126 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1127 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1128 uint8_t l = intel_dp_link_status(link_status, i);
1129
1130 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1131}
1132
1133
1134#if 0
1135static char *voltage_names[] = {
1136 "0.4V", "0.6V", "0.8V", "1.2V"
1137};
1138static char *pre_emph_names[] = {
1139 "0dB", "3.5dB", "6dB", "9.5dB"
1140};
1141static char *link_train_names[] = {
1142 "pattern 1", "pattern 2", "idle", "off"
1143};
1144#endif
1145
1146/*
1147 * These are source-specific values; current Intel hardware supports
1148 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1149 */
1150#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1151
1152static uint8_t
1153intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1154{
1155 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1156 case DP_TRAIN_VOLTAGE_SWING_400:
1157 return DP_TRAIN_PRE_EMPHASIS_6;
1158 case DP_TRAIN_VOLTAGE_SWING_600:
1159 return DP_TRAIN_PRE_EMPHASIS_6;
1160 case DP_TRAIN_VOLTAGE_SWING_800:
1161 return DP_TRAIN_PRE_EMPHASIS_3_5;
1162 case DP_TRAIN_VOLTAGE_SWING_1200:
1163 default:
1164 return DP_TRAIN_PRE_EMPHASIS_0;
1165 }
1166}
1167
1168static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001169intel_get_adjust_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001170{
1171 uint8_t v = 0;
1172 uint8_t p = 0;
1173 int lane;
1174
Jesse Barnes33a34e42010-09-08 12:42:02 -07001175 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1176 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1177 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001178
1179 if (this_v > v)
1180 v = this_v;
1181 if (this_p > p)
1182 p = this_p;
1183 }
1184
1185 if (v >= I830_DP_VOLTAGE_MAX)
1186 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1187
1188 if (p >= intel_dp_pre_emphasis_max(v))
1189 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1190
1191 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001192 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001193}
1194
1195static uint32_t
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001196intel_dp_signal_levels(uint8_t train_set, int lane_count)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001197{
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001198 uint32_t signal_levels = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001199
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001200 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001201 case DP_TRAIN_VOLTAGE_SWING_400:
1202 default:
1203 signal_levels |= DP_VOLTAGE_0_4;
1204 break;
1205 case DP_TRAIN_VOLTAGE_SWING_600:
1206 signal_levels |= DP_VOLTAGE_0_6;
1207 break;
1208 case DP_TRAIN_VOLTAGE_SWING_800:
1209 signal_levels |= DP_VOLTAGE_0_8;
1210 break;
1211 case DP_TRAIN_VOLTAGE_SWING_1200:
1212 signal_levels |= DP_VOLTAGE_1_2;
1213 break;
1214 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001215 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001216 case DP_TRAIN_PRE_EMPHASIS_0:
1217 default:
1218 signal_levels |= DP_PRE_EMPHASIS_0;
1219 break;
1220 case DP_TRAIN_PRE_EMPHASIS_3_5:
1221 signal_levels |= DP_PRE_EMPHASIS_3_5;
1222 break;
1223 case DP_TRAIN_PRE_EMPHASIS_6:
1224 signal_levels |= DP_PRE_EMPHASIS_6;
1225 break;
1226 case DP_TRAIN_PRE_EMPHASIS_9_5:
1227 signal_levels |= DP_PRE_EMPHASIS_9_5;
1228 break;
1229 }
1230 return signal_levels;
1231}
1232
Zhenyu Wange3421a12010-04-08 09:43:27 +08001233/* Gen6's DP voltage swing and pre-emphasis control */
1234static uint32_t
1235intel_gen6_edp_signal_levels(uint8_t train_set)
1236{
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001237 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1238 DP_TRAIN_PRE_EMPHASIS_MASK);
1239 switch (signal_levels) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001240 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001241 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
1242 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
1243 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
1244 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001245 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001246 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
1247 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001248 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001249 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
1250 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001251 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001252 case DP_TRAIN_VOLTAGE_SWING_1200 | DP_TRAIN_PRE_EMPHASIS_0:
1253 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001254 default:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001255 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
1256 "0x%x\n", signal_levels);
1257 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001258 }
1259}
1260
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001261static uint8_t
1262intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1263 int lane)
1264{
1265 int i = DP_LANE0_1_STATUS + (lane >> 1);
1266 int s = (lane & 1) * 4;
1267 uint8_t l = intel_dp_link_status(link_status, i);
1268
1269 return (l >> s) & 0xf;
1270}
1271
1272/* Check for clock recovery is done on all channels */
1273static bool
1274intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1275{
1276 int lane;
1277 uint8_t lane_status;
1278
1279 for (lane = 0; lane < lane_count; lane++) {
1280 lane_status = intel_get_lane_status(link_status, lane);
1281 if ((lane_status & DP_LANE_CR_DONE) == 0)
1282 return false;
1283 }
1284 return true;
1285}
1286
1287/* Check to see if channel eq is done on all channels */
1288#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1289 DP_LANE_CHANNEL_EQ_DONE|\
1290 DP_LANE_SYMBOL_LOCKED)
1291static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001292intel_channel_eq_ok(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001293{
1294 uint8_t lane_align;
1295 uint8_t lane_status;
1296 int lane;
1297
Jesse Barnes33a34e42010-09-08 12:42:02 -07001298 lane_align = intel_dp_link_status(intel_dp->link_status,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001299 DP_LANE_ALIGN_STATUS_UPDATED);
1300 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1301 return false;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001302 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1303 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001304 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1305 return false;
1306 }
1307 return true;
1308}
1309
1310static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001311intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001312 uint32_t dp_reg_value,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001313 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001314{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001315 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001316 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001317 int ret;
1318
Chris Wilsonea5b2132010-08-04 13:50:23 +01001319 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1320 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001321
Chris Wilsonea5b2132010-08-04 13:50:23 +01001322 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001323 DP_TRAINING_PATTERN_SET,
1324 dp_train_pat);
1325
Chris Wilsonea5b2132010-08-04 13:50:23 +01001326 ret = intel_dp_aux_native_write(intel_dp,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001327 DP_TRAINING_LANE0_SET,
1328 intel_dp->train_set, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001329 if (ret != 4)
1330 return false;
1331
1332 return true;
1333}
1334
Jesse Barnes33a34e42010-09-08 12:42:02 -07001335/* Enable corresponding port and start training pattern 1 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001336static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001337intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001338{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001339 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001340 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson58e10eb2010-10-03 10:56:11 +01001341 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001342 int i;
1343 uint8_t voltage;
1344 bool clock_recovery = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001345 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001346 u32 reg;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001347 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001348
Adam Jacksone8519462011-07-21 17:48:38 -04001349 /*
1350 * On CPT we have to enable the port in training pattern 1, which
1351 * will happen below in intel_dp_set_link_train. Otherwise, enable
1352 * the port and wait for it to become active.
1353 */
1354 if (!HAS_PCH_CPT(dev)) {
1355 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1356 POSTING_READ(intel_dp->output_reg);
1357 intel_wait_for_vblank(dev, intel_crtc->pipe);
1358 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001359
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001360 /* Write the link configuration data */
1361 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1362 intel_dp->link_configuration,
1363 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001364
1365 DP |= DP_PORT_EN;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001366 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001367 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1368 else
1369 DP &= ~DP_LINK_TRAIN_MASK;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001370 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001371 voltage = 0xff;
1372 tries = 0;
1373 clock_recovery = false;
1374 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001375 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001376 uint32_t signal_levels;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001377 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001378 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001379 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1380 } else {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001381 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001382 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1383 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001384
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001385 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001386 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1387 else
1388 reg = DP | DP_LINK_TRAIN_PAT_1;
1389
Chris Wilsonea5b2132010-08-04 13:50:23 +01001390 if (!intel_dp_set_link_train(intel_dp, reg,
Adam Jackson81055852011-07-21 17:48:37 -04001391 DP_TRAINING_PATTERN_1 |
1392 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001393 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001394 /* Set training pattern 1 */
1395
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001396 udelay(100);
1397 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001398 break;
1399
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001400 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1401 clock_recovery = true;
1402 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001403 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001404
1405 /* Check to see if we've tried the max voltage */
1406 for (i = 0; i < intel_dp->lane_count; i++)
1407 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1408 break;
1409 if (i == intel_dp->lane_count)
1410 break;
1411
1412 /* Check to see if we've tried the same voltage 5 times */
1413 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1414 ++tries;
1415 if (tries == 5)
1416 break;
1417 } else
1418 tries = 0;
1419 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1420
1421 /* Compute new intel_dp->train_set as requested by target */
1422 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001423 }
1424
Jesse Barnes33a34e42010-09-08 12:42:02 -07001425 intel_dp->DP = DP;
1426}
1427
1428static void
1429intel_dp_complete_link_train(struct intel_dp *intel_dp)
1430{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001431 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001432 struct drm_i915_private *dev_priv = dev->dev_private;
1433 bool channel_eq = false;
Jesse Barnes37f80972011-01-05 14:45:24 -08001434 int tries, cr_tries;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001435 u32 reg;
1436 uint32_t DP = intel_dp->DP;
1437
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001438 /* channel equalization */
1439 tries = 0;
Jesse Barnes37f80972011-01-05 14:45:24 -08001440 cr_tries = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001441 channel_eq = false;
1442 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001443 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001444 uint32_t signal_levels;
1445
Jesse Barnes37f80972011-01-05 14:45:24 -08001446 if (cr_tries > 5) {
1447 DRM_ERROR("failed to train DP, aborting\n");
1448 intel_dp_link_down(intel_dp);
1449 break;
1450 }
1451
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001452 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001453 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001454 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1455 } else {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001456 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001457 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1458 }
1459
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001460 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001461 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1462 else
1463 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001464
1465 /* channel eq pattern */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001466 if (!intel_dp_set_link_train(intel_dp, reg,
Adam Jackson81055852011-07-21 17:48:37 -04001467 DP_TRAINING_PATTERN_2 |
1468 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001469 break;
1470
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001471 udelay(400);
1472 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001473 break;
Jesse Barnes869184a2010-10-07 16:01:22 -07001474
Jesse Barnes37f80972011-01-05 14:45:24 -08001475 /* Make sure clock is still ok */
1476 if (!intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1477 intel_dp_start_link_train(intel_dp);
1478 cr_tries++;
1479 continue;
1480 }
1481
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001482 if (intel_channel_eq_ok(intel_dp)) {
1483 channel_eq = true;
1484 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001485 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001486
Jesse Barnes37f80972011-01-05 14:45:24 -08001487 /* Try 5 times, then try clock recovery if that fails */
1488 if (tries > 5) {
1489 intel_dp_link_down(intel_dp);
1490 intel_dp_start_link_train(intel_dp);
1491 tries = 0;
1492 cr_tries++;
1493 continue;
1494 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001495
1496 /* Compute new intel_dp->train_set as requested by target */
1497 intel_get_adjust_train(intel_dp);
1498 ++tries;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001499 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001500
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001501 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001502 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1503 else
1504 reg = DP | DP_LINK_TRAIN_OFF;
1505
Chris Wilsonea5b2132010-08-04 13:50:23 +01001506 I915_WRITE(intel_dp->output_reg, reg);
1507 POSTING_READ(intel_dp->output_reg);
1508 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001509 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1510}
1511
1512static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001513intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001514{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001515 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001516 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001517 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001518
Chris Wilson1b39d6f2010-12-06 11:20:45 +00001519 if ((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0)
1520 return;
1521
Zhao Yakui28c97732009-10-09 11:39:41 +08001522 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001523
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001524 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001525 DP &= ~DP_PLL_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001526 I915_WRITE(intel_dp->output_reg, DP);
1527 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001528 udelay(100);
1529 }
1530
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001531 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001532 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001533 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001534 } else {
1535 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001536 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001537 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01001538 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001539
Chris Wilsonfe255d02010-09-11 21:37:48 +01001540 msleep(17);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001541
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001542 if (is_edp(intel_dp))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001543 DP |= DP_LINK_TRAIN_OFF;
Eric Anholt5bddd172010-11-18 09:32:59 +08001544
Chris Wilson1b39d6f2010-12-06 11:20:45 +00001545 if (!HAS_PCH_CPT(dev) &&
1546 I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
Chris Wilson31acbcc2011-04-17 06:38:35 +01001547 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1548
Eric Anholt5bddd172010-11-18 09:32:59 +08001549 /* Hardware workaround: leaving our transcoder select
1550 * set to transcoder B while it's off will prevent the
1551 * corresponding HDMI output on transcoder A.
1552 *
1553 * Combine this with another hardware workaround:
1554 * transcoder select bit can only be cleared while the
1555 * port is enabled.
1556 */
1557 DP &= ~DP_PIPEB_SELECT;
1558 I915_WRITE(intel_dp->output_reg, DP);
1559
1560 /* Changes to enable or select take place the vblank
1561 * after being written.
1562 */
Chris Wilson31acbcc2011-04-17 06:38:35 +01001563 if (crtc == NULL) {
1564 /* We can arrive here never having been attached
1565 * to a CRTC, for instance, due to inheriting
1566 * random state from the BIOS.
1567 *
1568 * If the pipe is not running, play safe and
1569 * wait for the clocks to stabilise before
1570 * continuing.
1571 */
1572 POSTING_READ(intel_dp->output_reg);
1573 msleep(50);
1574 } else
1575 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
Eric Anholt5bddd172010-11-18 09:32:59 +08001576 }
1577
Chris Wilsonea5b2132010-08-04 13:50:23 +01001578 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1579 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001580}
1581
Keith Packard26d61aa2011-07-25 20:01:09 -07001582static bool
1583intel_dp_get_dpcd(struct intel_dp *intel_dp)
Keith Packard92fd8fd2011-07-25 19:50:10 -07001584{
Keith Packard92fd8fd2011-07-25 19:50:10 -07001585 if (intel_dp_aux_native_read_retry(intel_dp, 0x000, intel_dp->dpcd,
1586 sizeof (intel_dp->dpcd)) &&
1587 (intel_dp->dpcd[DP_DPCD_REV] != 0)) {
Keith Packard26d61aa2011-07-25 20:01:09 -07001588 return true;
Keith Packard92fd8fd2011-07-25 19:50:10 -07001589 }
1590
Keith Packard26d61aa2011-07-25 20:01:09 -07001591 return false;
Keith Packard92fd8fd2011-07-25 19:50:10 -07001592}
1593
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001594/*
1595 * According to DP spec
1596 * 5.1.2:
1597 * 1. Read DPCD
1598 * 2. Configure link according to Receiver Capabilities
1599 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1600 * 4. Check link status on receipt of hot-plug interrupt
1601 */
1602
1603static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001604intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001605{
Keith Packardd2b996a2011-07-25 22:37:51 -07001606 if (intel_dp->dpms_mode != DRM_MODE_DPMS_ON)
1607 return;
1608
Chris Wilson4ef69c72010-09-09 15:14:28 +01001609 if (!intel_dp->base.base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001610 return;
1611
Keith Packard92fd8fd2011-07-25 19:50:10 -07001612 /* Try to read receiver status if the link appears to be up */
Jesse Barnes33a34e42010-09-08 12:42:02 -07001613 if (!intel_dp_get_link_status(intel_dp)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001614 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001615 return;
1616 }
1617
Keith Packard92fd8fd2011-07-25 19:50:10 -07001618 /* Now read the DPCD to see if it's actually running */
Keith Packard26d61aa2011-07-25 20:01:09 -07001619 if (!intel_dp_get_dpcd(intel_dp)) {
Jesse Barnes59cd09e2011-07-07 11:10:59 -07001620 intel_dp_link_down(intel_dp);
1621 return;
1622 }
1623
Jesse Barnes33a34e42010-09-08 12:42:02 -07001624 if (!intel_channel_eq_ok(intel_dp)) {
Keith Packard92fd8fd2011-07-25 19:50:10 -07001625 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
1626 drm_get_encoder_name(&intel_dp->base.base));
Jesse Barnes33a34e42010-09-08 12:42:02 -07001627 intel_dp_start_link_train(intel_dp);
1628 intel_dp_complete_link_train(intel_dp);
1629 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001630}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001631
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001632static enum drm_connector_status
Keith Packard26d61aa2011-07-25 20:01:09 -07001633intel_dp_detect_dpcd(struct intel_dp *intel_dp)
1634{
1635 if (intel_dp_get_dpcd(intel_dp))
1636 return connector_status_connected;
1637 return connector_status_disconnected;
1638}
1639
1640static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001641ironlake_dp_detect(struct intel_dp *intel_dp)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001642{
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001643 enum drm_connector_status status;
1644
Chris Wilsonfe16d942011-02-12 10:29:38 +00001645 /* Can't disconnect eDP, but you can close the lid... */
1646 if (is_edp(intel_dp)) {
1647 status = intel_panel_detect(intel_dp->base.base.dev);
1648 if (status == connector_status_unknown)
1649 status = connector_status_connected;
1650 return status;
1651 }
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001652
Keith Packard26d61aa2011-07-25 20:01:09 -07001653 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001654}
1655
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001656static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001657g4x_dp_detect(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001658{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001659 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001660 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001661 uint32_t temp, bit;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001662
Chris Wilsonea5b2132010-08-04 13:50:23 +01001663 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001664 case DP_B:
1665 bit = DPB_HOTPLUG_INT_STATUS;
1666 break;
1667 case DP_C:
1668 bit = DPC_HOTPLUG_INT_STATUS;
1669 break;
1670 case DP_D:
1671 bit = DPD_HOTPLUG_INT_STATUS;
1672 break;
1673 default:
1674 return connector_status_unknown;
1675 }
1676
1677 temp = I915_READ(PORT_HOTPLUG_STAT);
1678
1679 if ((temp & bit) == 0)
1680 return connector_status_disconnected;
1681
Keith Packard26d61aa2011-07-25 20:01:09 -07001682 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001683}
1684
1685/**
1686 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1687 *
1688 * \return true if DP port is connected.
1689 * \return false if DP port is disconnected.
1690 */
1691static enum drm_connector_status
1692intel_dp_detect(struct drm_connector *connector, bool force)
1693{
1694 struct intel_dp *intel_dp = intel_attached_dp(connector);
1695 struct drm_device *dev = intel_dp->base.base.dev;
1696 enum drm_connector_status status;
1697 struct edid *edid = NULL;
1698
1699 intel_dp->has_audio = false;
Adam Jackson97cdd712011-07-12 17:38:00 -04001700 memset(intel_dp->dpcd, 0, sizeof(intel_dp->dpcd));
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001701
1702 if (HAS_PCH_SPLIT(dev))
1703 status = ironlake_dp_detect(intel_dp);
1704 else
1705 status = g4x_dp_detect(intel_dp);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04001706
Adam Jacksonac66ae82011-07-12 17:38:03 -04001707 DRM_DEBUG_KMS("DPCD: %02hx%02hx%02hx%02hx%02hx%02hx%02hx%02hx\n",
1708 intel_dp->dpcd[0], intel_dp->dpcd[1], intel_dp->dpcd[2],
1709 intel_dp->dpcd[3], intel_dp->dpcd[4], intel_dp->dpcd[5],
1710 intel_dp->dpcd[6], intel_dp->dpcd[7]);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04001711
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001712 if (status != connector_status_connected)
1713 return status;
1714
Chris Wilsonf6849602010-09-19 09:29:33 +01001715 if (intel_dp->force_audio) {
1716 intel_dp->has_audio = intel_dp->force_audio > 0;
1717 } else {
1718 edid = drm_get_edid(connector, &intel_dp->adapter);
1719 if (edid) {
1720 intel_dp->has_audio = drm_detect_monitor_audio(edid);
1721 connector->display_info.raw_edid = NULL;
1722 kfree(edid);
1723 }
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001724 }
1725
1726 return connector_status_connected;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001727}
1728
1729static int intel_dp_get_modes(struct drm_connector *connector)
1730{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001731 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001732 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001733 struct drm_i915_private *dev_priv = dev->dev_private;
1734 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001735
1736 /* We should parse the EDID data and find out if it has an audio sink
1737 */
1738
Chris Wilsonf899fc62010-07-20 15:44:45 -07001739 ret = intel_ddc_get_modes(connector, &intel_dp->adapter);
Zhao Yakuib9efc482010-07-19 09:43:11 +01001740 if (ret) {
Jesse Barnes4d926462010-10-07 16:01:07 -07001741 if (is_edp(intel_dp) && !dev_priv->panel_fixed_mode) {
Zhao Yakuib9efc482010-07-19 09:43:11 +01001742 struct drm_display_mode *newmode;
1743 list_for_each_entry(newmode, &connector->probed_modes,
1744 head) {
1745 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1746 dev_priv->panel_fixed_mode =
1747 drm_mode_duplicate(dev, newmode);
1748 break;
1749 }
1750 }
1751 }
1752
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001753 return ret;
Zhao Yakuib9efc482010-07-19 09:43:11 +01001754 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001755
1756 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Jesse Barnes4d926462010-10-07 16:01:07 -07001757 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001758 if (dev_priv->panel_fixed_mode != NULL) {
1759 struct drm_display_mode *mode;
1760 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1761 drm_mode_probed_add(connector, mode);
1762 return 1;
1763 }
1764 }
1765 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001766}
1767
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001768static bool
1769intel_dp_detect_audio(struct drm_connector *connector)
1770{
1771 struct intel_dp *intel_dp = intel_attached_dp(connector);
1772 struct edid *edid;
1773 bool has_audio = false;
1774
1775 edid = drm_get_edid(connector, &intel_dp->adapter);
1776 if (edid) {
1777 has_audio = drm_detect_monitor_audio(edid);
1778
1779 connector->display_info.raw_edid = NULL;
1780 kfree(edid);
1781 }
1782
1783 return has_audio;
1784}
1785
Chris Wilsonf6849602010-09-19 09:29:33 +01001786static int
1787intel_dp_set_property(struct drm_connector *connector,
1788 struct drm_property *property,
1789 uint64_t val)
1790{
Chris Wilsone953fd72011-02-21 22:23:52 +00001791 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Chris Wilsonf6849602010-09-19 09:29:33 +01001792 struct intel_dp *intel_dp = intel_attached_dp(connector);
1793 int ret;
1794
1795 ret = drm_connector_property_set_value(connector, property, val);
1796 if (ret)
1797 return ret;
1798
Chris Wilson3f43c482011-05-12 22:17:24 +01001799 if (property == dev_priv->force_audio_property) {
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001800 int i = val;
1801 bool has_audio;
1802
1803 if (i == intel_dp->force_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01001804 return 0;
1805
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001806 intel_dp->force_audio = i;
Chris Wilsonf6849602010-09-19 09:29:33 +01001807
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001808 if (i == 0)
1809 has_audio = intel_dp_detect_audio(connector);
1810 else
1811 has_audio = i > 0;
1812
1813 if (has_audio == intel_dp->has_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01001814 return 0;
1815
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001816 intel_dp->has_audio = has_audio;
Chris Wilsonf6849602010-09-19 09:29:33 +01001817 goto done;
1818 }
1819
Chris Wilsone953fd72011-02-21 22:23:52 +00001820 if (property == dev_priv->broadcast_rgb_property) {
1821 if (val == !!intel_dp->color_range)
1822 return 0;
1823
1824 intel_dp->color_range = val ? DP_COLOR_RANGE_16_235 : 0;
1825 goto done;
1826 }
1827
Chris Wilsonf6849602010-09-19 09:29:33 +01001828 return -EINVAL;
1829
1830done:
1831 if (intel_dp->base.base.crtc) {
1832 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1833 drm_crtc_helper_set_mode(crtc, &crtc->mode,
1834 crtc->x, crtc->y,
1835 crtc->fb);
1836 }
1837
1838 return 0;
1839}
1840
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001841static void
1842intel_dp_destroy (struct drm_connector *connector)
1843{
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001844 drm_sysfs_connector_remove(connector);
1845 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001846 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001847}
1848
Daniel Vetter24d05922010-08-20 18:08:28 +02001849static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1850{
1851 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1852
1853 i2c_del_adapter(&intel_dp->adapter);
1854 drm_encoder_cleanup(encoder);
1855 kfree(intel_dp);
1856}
1857
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001858static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1859 .dpms = intel_dp_dpms,
1860 .mode_fixup = intel_dp_mode_fixup,
Jesse Barnesd240f202010-08-13 15:43:26 -07001861 .prepare = intel_dp_prepare,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001862 .mode_set = intel_dp_mode_set,
Jesse Barnesd240f202010-08-13 15:43:26 -07001863 .commit = intel_dp_commit,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001864};
1865
1866static const struct drm_connector_funcs intel_dp_connector_funcs = {
1867 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001868 .detect = intel_dp_detect,
1869 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilsonf6849602010-09-19 09:29:33 +01001870 .set_property = intel_dp_set_property,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001871 .destroy = intel_dp_destroy,
1872};
1873
1874static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1875 .get_modes = intel_dp_get_modes,
1876 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01001877 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001878};
1879
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001880static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02001881 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001882};
1883
Chris Wilson995b6762010-08-20 13:23:26 +01001884static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001885intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07001886{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001887 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Keith Packardc8110e52009-05-06 11:51:10 -07001888
Jesse Barnes885a5012011-07-07 11:11:01 -07001889 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07001890}
1891
Zhenyu Wange3421a12010-04-08 09:43:27 +08001892/* Return which DP Port should be selected for Transcoder DP control */
1893int
1894intel_trans_dp_port_sel (struct drm_crtc *crtc)
1895{
1896 struct drm_device *dev = crtc->dev;
1897 struct drm_mode_config *mode_config = &dev->mode_config;
1898 struct drm_encoder *encoder;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001899
1900 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001901 struct intel_dp *intel_dp;
1902
Dan Carpenterd8201ab2010-05-07 10:39:00 +02001903 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08001904 continue;
1905
Chris Wilsonea5b2132010-08-04 13:50:23 +01001906 intel_dp = enc_to_intel_dp(encoder);
1907 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
1908 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001909 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001910
Zhenyu Wange3421a12010-04-08 09:43:27 +08001911 return -1;
1912}
1913
Zhao Yakui36e83a12010-06-12 14:32:21 +08001914/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04001915bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08001916{
1917 struct drm_i915_private *dev_priv = dev->dev_private;
1918 struct child_device_config *p_child;
1919 int i;
1920
1921 if (!dev_priv->child_dev_num)
1922 return false;
1923
1924 for (i = 0; i < dev_priv->child_dev_num; i++) {
1925 p_child = dev_priv->child_dev + i;
1926
1927 if (p_child->dvo_port == PORT_IDPD &&
1928 p_child->device_type == DEVICE_TYPE_eDP)
1929 return true;
1930 }
1931 return false;
1932}
1933
Chris Wilsonf6849602010-09-19 09:29:33 +01001934static void
1935intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
1936{
Chris Wilson3f43c482011-05-12 22:17:24 +01001937 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +00001938 intel_attach_broadcast_rgb_property(connector);
Chris Wilsonf6849602010-09-19 09:29:33 +01001939}
1940
Keith Packardc8110e52009-05-06 11:51:10 -07001941void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001942intel_dp_init(struct drm_device *dev, int output_reg)
1943{
1944 struct drm_i915_private *dev_priv = dev->dev_private;
1945 struct drm_connector *connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001946 struct intel_dp *intel_dp;
Eric Anholt21d40d32010-03-25 11:11:14 -07001947 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001948 struct intel_connector *intel_connector;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001949 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04001950 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001951
Chris Wilsonea5b2132010-08-04 13:50:23 +01001952 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
1953 if (!intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001954 return;
1955
Chris Wilson3d3dc142011-02-12 10:33:12 +00001956 intel_dp->output_reg = output_reg;
Keith Packardd2b996a2011-07-25 22:37:51 -07001957 intel_dp->dpms_mode = -1;
Chris Wilson3d3dc142011-02-12 10:33:12 +00001958
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001959 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1960 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001961 kfree(intel_dp);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001962 return;
1963 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001964 intel_encoder = &intel_dp->base;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001965
Chris Wilsonea5b2132010-08-04 13:50:23 +01001966 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04001967 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01001968 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04001969
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001970 if (output_reg == DP_A || is_pch_edp(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04001971 type = DRM_MODE_CONNECTOR_eDP;
1972 intel_encoder->type = INTEL_OUTPUT_EDP;
1973 } else {
1974 type = DRM_MODE_CONNECTOR_DisplayPort;
1975 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1976 }
1977
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001978 connector = &intel_connector->base;
Adam Jacksonb3295302010-07-16 14:46:28 -04001979 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001980 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1981
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001982 connector->polled = DRM_CONNECTOR_POLL_HPD;
1983
Zhao Yakui652af9d2009-12-02 10:03:33 +08001984 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07001985 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001986 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07001987 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001988 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07001989 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08001990
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001991 if (is_edp(intel_dp))
Eric Anholt21d40d32010-03-25 11:11:14 -07001992 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08001993
Eric Anholt21d40d32010-03-25 11:11:14 -07001994 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001995 connector->interlace_allowed = true;
1996 connector->doublescan_allowed = 0;
1997
Chris Wilson4ef69c72010-09-09 15:14:28 +01001998 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001999 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +01002000 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002001
Chris Wilsondf0e9242010-09-09 16:20:55 +01002002 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002003 drm_sysfs_connector_add(connector);
2004
2005 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002006 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002007 case DP_A:
2008 name = "DPDDC-A";
2009 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002010 case DP_B:
2011 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002012 dev_priv->hotplug_supported_mask |=
2013 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002014 name = "DPDDC-B";
2015 break;
2016 case DP_C:
2017 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002018 dev_priv->hotplug_supported_mask |=
2019 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002020 name = "DPDDC-C";
2021 break;
2022 case DP_D:
2023 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002024 dev_priv->hotplug_supported_mask |=
2025 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002026 name = "DPDDC-D";
2027 break;
2028 }
2029
Chris Wilsonea5b2132010-08-04 13:50:23 +01002030 intel_dp_i2c_init(intel_dp, intel_connector, name);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002031
Jesse Barnes89667382010-10-07 16:01:21 -07002032 /* Cache some DPCD data in the eDP case */
2033 if (is_edp(intel_dp)) {
Keith Packard59f3e272011-07-25 20:01:56 -07002034 bool ret;
Jesse Barnes5d613502011-01-24 17:10:54 -08002035 u32 pp_on, pp_div;
Jesse Barnes89667382010-10-07 16:01:21 -07002036
Jesse Barnes5d613502011-01-24 17:10:54 -08002037 pp_on = I915_READ(PCH_PP_ON_DELAYS);
2038 pp_div = I915_READ(PCH_PP_DIVISOR);
2039
2040 /* Get T3 & T12 values (note: VESA not bspec terminology) */
2041 dev_priv->panel_t3 = (pp_on & 0x1fff0000) >> 16;
2042 dev_priv->panel_t3 /= 10; /* t3 in 100us units */
2043 dev_priv->panel_t12 = pp_div & 0xf;
2044 dev_priv->panel_t12 *= 100; /* t12 in 100ms units */
2045
2046 ironlake_edp_panel_vdd_on(intel_dp);
Keith Packard59f3e272011-07-25 20:01:56 -07002047 ret = intel_dp_get_dpcd(intel_dp);
Chris Wilson3d3dc142011-02-12 10:33:12 +00002048 ironlake_edp_panel_vdd_off(intel_dp);
Keith Packard59f3e272011-07-25 20:01:56 -07002049 if (ret) {
Jesse Barnes7183dc22011-07-07 11:10:58 -07002050 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
2051 dev_priv->no_aux_handshake =
2052 intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
Jesse Barnes89667382010-10-07 16:01:21 -07002053 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
2054 } else {
Chris Wilson3d3dc142011-02-12 10:33:12 +00002055 /* if this fails, presume the device is a ghost */
Takashi Iwai48898b02011-03-18 09:06:49 +00002056 DRM_INFO("failed to retrieve link info, disabling eDP\n");
Chris Wilson3d3dc142011-02-12 10:33:12 +00002057 intel_dp_encoder_destroy(&intel_dp->base.base);
Takashi Iwai48898b02011-03-18 09:06:49 +00002058 intel_dp_destroy(&intel_connector->base);
Chris Wilson3d3dc142011-02-12 10:33:12 +00002059 return;
Jesse Barnes89667382010-10-07 16:01:21 -07002060 }
Jesse Barnes89667382010-10-07 16:01:21 -07002061 }
2062
Eric Anholt21d40d32010-03-25 11:11:14 -07002063 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002064
Jesse Barnes4d926462010-10-07 16:01:07 -07002065 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002066 /* initialize panel mode from VBT if available for eDP */
2067 if (dev_priv->lfp_lvds_vbt_mode) {
2068 dev_priv->panel_fixed_mode =
2069 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
2070 if (dev_priv->panel_fixed_mode) {
2071 dev_priv->panel_fixed_mode->type |=
2072 DRM_MODE_TYPE_PREFERRED;
2073 }
2074 }
2075 }
2076
Chris Wilsonf6849602010-09-19 09:29:33 +01002077 intel_dp_add_properties(intel_dp, connector);
2078
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002079 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
2080 * 0xd. Failure to do so will result in spurious interrupts being
2081 * generated on the port when a cable is not attached.
2082 */
2083 if (IS_G4X(dev) && !IS_GM45(dev)) {
2084 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
2085 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
2086 }
2087}