blob: ad682a5ffe8c2560ff097983c9a85a8af05831fc [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 Packardf01eca22011-09-28 16:48:10 -070062 int panel_power_up_delay;
63 int panel_power_down_delay;
64 int panel_power_cycle_delay;
65 int backlight_on_delay;
66 int backlight_off_delay;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070067};
68
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070069/**
70 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
71 * @intel_dp: DP struct
72 *
73 * If a CPU or PCH DP output is attached to an eDP panel, this function
74 * will return true, and false otherwise.
75 */
76static bool is_edp(struct intel_dp *intel_dp)
77{
78 return intel_dp->base.type == INTEL_OUTPUT_EDP;
79}
80
81/**
82 * is_pch_edp - is the port on the PCH and attached to an eDP panel?
83 * @intel_dp: DP struct
84 *
85 * Returns true if the given DP struct corresponds to a PCH DP port attached
86 * to an eDP panel, false otherwise. Helpful for determining whether we
87 * may need FDI resources for a given DP output or not.
88 */
89static bool is_pch_edp(struct intel_dp *intel_dp)
90{
91 return intel_dp->is_pch_edp;
92}
93
Chris Wilsonea5b2132010-08-04 13:50:23 +010094static struct intel_dp *enc_to_intel_dp(struct drm_encoder *encoder)
95{
Chris Wilson4ef69c72010-09-09 15:14:28 +010096 return container_of(encoder, struct intel_dp, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010097}
Keith Packarda4fc5ed2009-04-07 16:16:42 -070098
Chris Wilsondf0e9242010-09-09 16:20:55 +010099static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
100{
101 return container_of(intel_attached_encoder(connector),
102 struct intel_dp, base);
103}
104
Jesse Barnes814948a2010-10-07 16:01:09 -0700105/**
106 * intel_encoder_is_pch_edp - is the given encoder a PCH attached eDP?
107 * @encoder: DRM encoder
108 *
109 * Return true if @encoder corresponds to a PCH attached eDP panel. Needed
110 * by intel_display.c.
111 */
112bool intel_encoder_is_pch_edp(struct drm_encoder *encoder)
113{
114 struct intel_dp *intel_dp;
115
116 if (!encoder)
117 return false;
118
119 intel_dp = enc_to_intel_dp(encoder);
120
121 return is_pch_edp(intel_dp);
122}
123
Jesse Barnes33a34e42010-09-08 12:42:02 -0700124static void intel_dp_start_link_train(struct intel_dp *intel_dp);
125static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100126static void intel_dp_link_down(struct intel_dp *intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700127
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800128void
Eric Anholt21d40d32010-03-25 11:11:14 -0700129intel_edp_link_config (struct intel_encoder *intel_encoder,
Chris Wilsonea5b2132010-08-04 13:50:23 +0100130 int *lane_num, int *link_bw)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800131{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100132 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800133
Chris Wilsonea5b2132010-08-04 13:50:23 +0100134 *lane_num = intel_dp->lane_count;
135 if (intel_dp->link_bw == DP_LINK_BW_1_62)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800136 *link_bw = 162000;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100137 else if (intel_dp->link_bw == DP_LINK_BW_2_7)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800138 *link_bw = 270000;
139}
140
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700141static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100142intel_dp_max_lane_count(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700143{
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700144 int max_lane_count = 4;
145
Jesse Barnes7183dc22011-07-07 11:10:58 -0700146 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
147 max_lane_count = intel_dp->dpcd[DP_MAX_LANE_COUNT] & 0x1f;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700148 switch (max_lane_count) {
149 case 1: case 2: case 4:
150 break;
151 default:
152 max_lane_count = 4;
153 }
154 }
155 return max_lane_count;
156}
157
158static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100159intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700160{
Jesse Barnes7183dc22011-07-07 11:10:58 -0700161 int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700162
163 switch (max_link_bw) {
164 case DP_LINK_BW_1_62:
165 case DP_LINK_BW_2_7:
166 break;
167 default:
168 max_link_bw = DP_LINK_BW_1_62;
169 break;
170 }
171 return max_link_bw;
172}
173
174static int
175intel_dp_link_clock(uint8_t link_bw)
176{
177 if (link_bw == DP_LINK_BW_2_7)
178 return 270000;
179 else
180 return 162000;
181}
182
183/* I think this is a fiction */
184static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100185intel_dp_link_required(struct drm_device *dev, struct intel_dp *intel_dp, int pixel_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700186{
Jesse Barnes89c61432011-06-24 12:19:28 -0700187 struct drm_crtc *crtc = intel_dp->base.base.crtc;
188 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
189 int bpp = 24;
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800190
Jesse Barnes89c61432011-06-24 12:19:28 -0700191 if (intel_crtc)
192 bpp = intel_crtc->bpp;
193
194 return (pixel_clock * bpp + 7) / 8;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700195}
196
197static int
Dave Airliefe27d532010-06-30 11:46:17 +1000198intel_dp_max_data_rate(int max_link_clock, int max_lanes)
199{
200 return (max_link_clock * max_lanes * 8) / 10;
201}
202
203static int
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700204intel_dp_mode_valid(struct drm_connector *connector,
205 struct drm_display_mode *mode)
206{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100207 struct intel_dp *intel_dp = intel_attached_dp(connector);
Zhao Yakui7de56f42010-07-19 09:43:14 +0100208 struct drm_device *dev = connector->dev;
209 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100210 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
211 int max_lanes = intel_dp_max_lane_count(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700212
Jesse Barnes4d926462010-10-07 16:01:07 -0700213 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Zhao Yakui7de56f42010-07-19 09:43:14 +0100214 if (mode->hdisplay > dev_priv->panel_fixed_mode->hdisplay)
215 return MODE_PANEL;
216
217 if (mode->vdisplay > dev_priv->panel_fixed_mode->vdisplay)
218 return MODE_PANEL;
219 }
220
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300221 /* only refuse the mode on non eDP since we have seen some weird eDP panels
Dave Airliefe27d532010-06-30 11:46:17 +1000222 which are outside spec tolerances but somehow work by magic */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700223 if (!is_edp(intel_dp) &&
Chris Wilsonea5b2132010-08-04 13:50:23 +0100224 (intel_dp_link_required(connector->dev, intel_dp, mode->clock)
Dave Airliefe27d532010-06-30 11:46:17 +1000225 > intel_dp_max_data_rate(max_link_clock, max_lanes)))
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700226 return MODE_CLOCK_HIGH;
227
228 if (mode->clock < 10000)
229 return MODE_CLOCK_LOW;
230
231 return MODE_OK;
232}
233
234static uint32_t
235pack_aux(uint8_t *src, int src_bytes)
236{
237 int i;
238 uint32_t v = 0;
239
240 if (src_bytes > 4)
241 src_bytes = 4;
242 for (i = 0; i < src_bytes; i++)
243 v |= ((uint32_t) src[i]) << ((3-i) * 8);
244 return v;
245}
246
247static void
248unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
249{
250 int i;
251 if (dst_bytes > 4)
252 dst_bytes = 4;
253 for (i = 0; i < dst_bytes; i++)
254 dst[i] = src >> ((3-i) * 8);
255}
256
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700257/* hrawclock is 1/4 the FSB frequency */
258static int
259intel_hrawclk(struct drm_device *dev)
260{
261 struct drm_i915_private *dev_priv = dev->dev_private;
262 uint32_t clkcfg;
263
264 clkcfg = I915_READ(CLKCFG);
265 switch (clkcfg & CLKCFG_FSB_MASK) {
266 case CLKCFG_FSB_400:
267 return 100;
268 case CLKCFG_FSB_533:
269 return 133;
270 case CLKCFG_FSB_667:
271 return 166;
272 case CLKCFG_FSB_800:
273 return 200;
274 case CLKCFG_FSB_1067:
275 return 266;
276 case CLKCFG_FSB_1333:
277 return 333;
278 /* these two are just a guess; one of them might be right */
279 case CLKCFG_FSB_1600:
280 case CLKCFG_FSB_1600_ALT:
281 return 400;
282 default:
283 return 133;
284 }
285}
286
Keith Packard9b984da2011-09-19 13:54:47 -0700287static void
288intel_dp_check_edp(struct intel_dp *intel_dp)
289{
290 struct drm_device *dev = intel_dp->base.base.dev;
291 struct drm_i915_private *dev_priv = dev->dev_private;
292 u32 pp_status, pp_control;
293 if (!is_edp(intel_dp))
294 return;
295 pp_status = I915_READ(PCH_PP_STATUS);
296 pp_control = I915_READ(PCH_PP_CONTROL);
297 if ((pp_status & PP_ON) == 0 && (pp_control & EDP_FORCE_VDD) == 0) {
298 WARN(1, "eDP powered off while attempting aux channel communication.\n");
299 DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
300 pp_status,
301 I915_READ(PCH_PP_CONTROL));
302 }
303}
304
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700305static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100306intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700307 uint8_t *send, int send_bytes,
308 uint8_t *recv, int recv_size)
309{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100310 uint32_t output_reg = intel_dp->output_reg;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100311 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700312 struct drm_i915_private *dev_priv = dev->dev_private;
313 uint32_t ch_ctl = output_reg + 0x10;
314 uint32_t ch_data = ch_ctl + 4;
315 int i;
316 int recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700317 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700318 uint32_t aux_clock_divider;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800319 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700320
Keith Packard9b984da2011-09-19 13:54:47 -0700321 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700322 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700323 * and would like to run at 2MHz. So, take the
324 * hrawclk value and divide by 2 and use that
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700325 *
326 * Note that PCH attached eDP panels should use a 125MHz input
327 * clock divider.
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700328 */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700329 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +0800330 if (IS_GEN6(dev))
331 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
332 else
333 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
334 } else if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500335 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800336 else
337 aux_clock_divider = intel_hrawclk(dev) / 2;
338
Zhenyu Wange3421a12010-04-08 09:43:27 +0800339 if (IS_GEN6(dev))
340 precharge = 3;
341 else
342 precharge = 5;
343
Jesse Barnes11bee432011-08-01 15:02:20 -0700344 /* Try to wait for any previous AUX channel activity */
345 for (try = 0; try < 3; try++) {
346 status = I915_READ(ch_ctl);
347 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
348 break;
349 msleep(1);
350 }
351
352 if (try == 3) {
353 WARN(1, "dp_aux_ch not started status 0x%08x\n",
354 I915_READ(ch_ctl));
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100355 return -EBUSY;
356 }
357
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700358 /* Must try at least 3 times according to DP spec */
359 for (try = 0; try < 5; try++) {
360 /* Load the send data into the aux channel data registers */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100361 for (i = 0; i < send_bytes; i += 4)
362 I915_WRITE(ch_data + i,
363 pack_aux(send + i, send_bytes - i));
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700364
365 /* Send the command and wait for it to complete */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100366 I915_WRITE(ch_ctl,
367 DP_AUX_CH_CTL_SEND_BUSY |
368 DP_AUX_CH_CTL_TIME_OUT_400us |
369 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
370 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
371 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
372 DP_AUX_CH_CTL_DONE |
373 DP_AUX_CH_CTL_TIME_OUT_ERROR |
374 DP_AUX_CH_CTL_RECEIVE_ERROR);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700375 for (;;) {
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700376 status = I915_READ(ch_ctl);
377 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
378 break;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100379 udelay(100);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700380 }
381
382 /* Clear done status and any errors */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100383 I915_WRITE(ch_ctl,
384 status |
385 DP_AUX_CH_CTL_DONE |
386 DP_AUX_CH_CTL_TIME_OUT_ERROR |
387 DP_AUX_CH_CTL_RECEIVE_ERROR);
388 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700389 break;
390 }
391
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700392 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700393 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700394 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700395 }
396
397 /* Check for timeout or receive error.
398 * Timeouts occur when the sink is not connected
399 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700400 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700401 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700402 return -EIO;
403 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700404
405 /* Timeouts occur when the device isn't connected, so they're
406 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700407 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800408 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700409 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700410 }
411
412 /* Unload any bytes sent back from the other side */
413 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
414 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700415 if (recv_bytes > recv_size)
416 recv_bytes = recv_size;
417
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100418 for (i = 0; i < recv_bytes; i += 4)
419 unpack_aux(I915_READ(ch_data + i),
420 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700421
422 return recv_bytes;
423}
424
425/* Write data to the aux channel in native mode */
426static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100427intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700428 uint16_t address, uint8_t *send, int send_bytes)
429{
430 int ret;
431 uint8_t msg[20];
432 int msg_bytes;
433 uint8_t ack;
434
Keith Packard9b984da2011-09-19 13:54:47 -0700435 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700436 if (send_bytes > 16)
437 return -1;
438 msg[0] = AUX_NATIVE_WRITE << 4;
439 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800440 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700441 msg[3] = send_bytes - 1;
442 memcpy(&msg[4], send, send_bytes);
443 msg_bytes = send_bytes + 4;
444 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100445 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700446 if (ret < 0)
447 return ret;
448 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
449 break;
450 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
451 udelay(100);
452 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700453 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700454 }
455 return send_bytes;
456}
457
458/* Write a single byte to the aux channel in native mode */
459static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100460intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700461 uint16_t address, uint8_t byte)
462{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100463 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700464}
465
466/* read bytes from a native aux channel */
467static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100468intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700469 uint16_t address, uint8_t *recv, int recv_bytes)
470{
471 uint8_t msg[4];
472 int msg_bytes;
473 uint8_t reply[20];
474 int reply_bytes;
475 uint8_t ack;
476 int ret;
477
Keith Packard9b984da2011-09-19 13:54:47 -0700478 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700479 msg[0] = AUX_NATIVE_READ << 4;
480 msg[1] = address >> 8;
481 msg[2] = address & 0xff;
482 msg[3] = recv_bytes - 1;
483
484 msg_bytes = 4;
485 reply_bytes = recv_bytes + 1;
486
487 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100488 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700489 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700490 if (ret == 0)
491 return -EPROTO;
492 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700493 return ret;
494 ack = reply[0];
495 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
496 memcpy(recv, reply + 1, ret - 1);
497 return ret - 1;
498 }
499 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
500 udelay(100);
501 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700502 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700503 }
504}
505
506static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000507intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
508 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700509{
Dave Airlieab2c0672009-12-04 10:55:24 +1000510 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100511 struct intel_dp *intel_dp = container_of(adapter,
512 struct intel_dp,
513 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000514 uint16_t address = algo_data->address;
515 uint8_t msg[5];
516 uint8_t reply[2];
David Flynn8316f332010-12-08 16:10:21 +0000517 unsigned retry;
Dave Airlieab2c0672009-12-04 10:55:24 +1000518 int msg_bytes;
519 int reply_bytes;
520 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700521
Keith Packard9b984da2011-09-19 13:54:47 -0700522 intel_dp_check_edp(intel_dp);
Dave Airlieab2c0672009-12-04 10:55:24 +1000523 /* Set up the command byte */
524 if (mode & MODE_I2C_READ)
525 msg[0] = AUX_I2C_READ << 4;
526 else
527 msg[0] = AUX_I2C_WRITE << 4;
528
529 if (!(mode & MODE_I2C_STOP))
530 msg[0] |= AUX_I2C_MOT << 4;
531
532 msg[1] = address >> 8;
533 msg[2] = address;
534
535 switch (mode) {
536 case MODE_I2C_WRITE:
537 msg[3] = 0;
538 msg[4] = write_byte;
539 msg_bytes = 5;
540 reply_bytes = 1;
541 break;
542 case MODE_I2C_READ:
543 msg[3] = 0;
544 msg_bytes = 4;
545 reply_bytes = 2;
546 break;
547 default:
548 msg_bytes = 3;
549 reply_bytes = 1;
550 break;
551 }
552
David Flynn8316f332010-12-08 16:10:21 +0000553 for (retry = 0; retry < 5; retry++) {
554 ret = intel_dp_aux_ch(intel_dp,
555 msg, msg_bytes,
556 reply, reply_bytes);
Dave Airlieab2c0672009-12-04 10:55:24 +1000557 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000558 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000559 return ret;
560 }
David Flynn8316f332010-12-08 16:10:21 +0000561
562 switch (reply[0] & AUX_NATIVE_REPLY_MASK) {
563 case AUX_NATIVE_REPLY_ACK:
564 /* I2C-over-AUX Reply field is only valid
565 * when paired with AUX ACK.
566 */
567 break;
568 case AUX_NATIVE_REPLY_NACK:
569 DRM_DEBUG_KMS("aux_ch native nack\n");
570 return -EREMOTEIO;
571 case AUX_NATIVE_REPLY_DEFER:
572 udelay(100);
573 continue;
574 default:
575 DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
576 reply[0]);
577 return -EREMOTEIO;
578 }
579
Dave Airlieab2c0672009-12-04 10:55:24 +1000580 switch (reply[0] & AUX_I2C_REPLY_MASK) {
581 case AUX_I2C_REPLY_ACK:
582 if (mode == MODE_I2C_READ) {
583 *read_byte = reply[1];
584 }
585 return reply_bytes - 1;
586 case AUX_I2C_REPLY_NACK:
David Flynn8316f332010-12-08 16:10:21 +0000587 DRM_DEBUG_KMS("aux_i2c nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000588 return -EREMOTEIO;
589 case AUX_I2C_REPLY_DEFER:
David Flynn8316f332010-12-08 16:10:21 +0000590 DRM_DEBUG_KMS("aux_i2c defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000591 udelay(100);
592 break;
593 default:
David Flynn8316f332010-12-08 16:10:21 +0000594 DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
Dave Airlieab2c0672009-12-04 10:55:24 +1000595 return -EREMOTEIO;
596 }
597 }
David Flynn8316f332010-12-08 16:10:21 +0000598
599 DRM_ERROR("too many retries, giving up\n");
600 return -EREMOTEIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700601}
602
Keith Packard0b5c5412011-09-28 16:41:05 -0700603static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp);
604static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp);
605
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700606static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100607intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800608 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700609{
Keith Packard0b5c5412011-09-28 16:41:05 -0700610 int ret;
611
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800612 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100613 intel_dp->algo.running = false;
614 intel_dp->algo.address = 0;
615 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700616
Chris Wilsonea5b2132010-08-04 13:50:23 +0100617 memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
618 intel_dp->adapter.owner = THIS_MODULE;
619 intel_dp->adapter.class = I2C_CLASS_DDC;
620 strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
621 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
622 intel_dp->adapter.algo_data = &intel_dp->algo;
623 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
624
Keith Packard0b5c5412011-09-28 16:41:05 -0700625 ironlake_edp_panel_vdd_on(intel_dp);
626 ret = i2c_dp_aux_add_bus(&intel_dp->adapter);
627 ironlake_edp_panel_vdd_off(intel_dp);
628 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700629}
630
631static bool
632intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
633 struct drm_display_mode *adjusted_mode)
634{
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100635 struct drm_device *dev = encoder->dev;
636 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100637 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700638 int lane_count, clock;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100639 int max_lane_count = intel_dp_max_lane_count(intel_dp);
640 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700641 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
642
Jesse Barnes4d926462010-10-07 16:01:07 -0700643 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100644 intel_fixed_panel_mode(dev_priv->panel_fixed_mode, adjusted_mode);
645 intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
646 mode, adjusted_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100647 /*
648 * the mode->clock is used to calculate the Data&Link M/N
649 * of the pipe. For the eDP the fixed clock should be used.
650 */
651 mode->clock = dev_priv->panel_fixed_mode->clock;
652 }
653
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700654 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
655 for (clock = 0; clock <= max_clock; clock++) {
Dave Airliefe27d532010-06-30 11:46:17 +1000656 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700657
Chris Wilsonea5b2132010-08-04 13:50:23 +0100658 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800659 <= link_avail) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100660 intel_dp->link_bw = bws[clock];
661 intel_dp->lane_count = lane_count;
662 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Zhao Yakui28c97732009-10-09 11:39:41 +0800663 DRM_DEBUG_KMS("Display port link bw %02x lane "
664 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100665 intel_dp->link_bw, intel_dp->lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700666 adjusted_mode->clock);
667 return true;
668 }
669 }
670 }
Dave Airliefe27d532010-06-30 11:46:17 +1000671
Chris Wilson3cf2efb2010-11-29 10:09:55 +0000672 if (is_edp(intel_dp)) {
673 /* okay we failed just pick the highest */
674 intel_dp->lane_count = max_lane_count;
675 intel_dp->link_bw = bws[max_clock];
676 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
677 DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
678 "count %d clock %d\n",
679 intel_dp->link_bw, intel_dp->lane_count,
680 adjusted_mode->clock);
681
682 return true;
683 }
684
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700685 return false;
686}
687
688struct intel_dp_m_n {
689 uint32_t tu;
690 uint32_t gmch_m;
691 uint32_t gmch_n;
692 uint32_t link_m;
693 uint32_t link_n;
694};
695
696static void
697intel_reduce_ratio(uint32_t *num, uint32_t *den)
698{
699 while (*num > 0xffffff || *den > 0xffffff) {
700 *num >>= 1;
701 *den >>= 1;
702 }
703}
704
705static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800706intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700707 int nlanes,
708 int pixel_clock,
709 int link_clock,
710 struct intel_dp_m_n *m_n)
711{
712 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800713 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700714 m_n->gmch_n = link_clock * nlanes;
715 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
716 m_n->link_m = pixel_clock;
717 m_n->link_n = link_clock;
718 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
719}
720
721void
722intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
723 struct drm_display_mode *adjusted_mode)
724{
725 struct drm_device *dev = crtc->dev;
726 struct drm_mode_config *mode_config = &dev->mode_config;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800727 struct drm_encoder *encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700728 struct drm_i915_private *dev_priv = dev->dev_private;
729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes858fa0352011-06-24 12:19:24 -0700730 int lane_count = 4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700731 struct intel_dp_m_n m_n;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800732 int pipe = intel_crtc->pipe;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700733
734 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700735 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700736 */
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800737 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100738 struct intel_dp *intel_dp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700739
Dan Carpenterd8201ab2010-05-07 10:39:00 +0200740 if (encoder->crtc != crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700741 continue;
742
Chris Wilsonea5b2132010-08-04 13:50:23 +0100743 intel_dp = enc_to_intel_dp(encoder);
744 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
745 lane_count = intel_dp->lane_count;
Jesse Barnes51190662010-10-07 16:01:08 -0700746 break;
747 } else if (is_edp(intel_dp)) {
748 lane_count = dev_priv->edp.lanes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700749 break;
750 }
751 }
752
753 /*
754 * Compute the GMCH and Link ratios. The '3' here is
755 * the number of bytes_per_pixel post-LUT, which we always
756 * set up for 8-bits of R/G/B, or 3 bytes total.
757 */
Jesse Barnes858fa0352011-06-24 12:19:24 -0700758 intel_dp_compute_m_n(intel_crtc->bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700759 mode->clock, adjusted_mode->clock, &m_n);
760
Eric Anholtc619eed2010-01-28 16:45:52 -0800761 if (HAS_PCH_SPLIT(dev)) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800762 I915_WRITE(TRANSDATA_M1(pipe),
763 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
764 m_n.gmch_m);
765 I915_WRITE(TRANSDATA_N1(pipe), m_n.gmch_n);
766 I915_WRITE(TRANSDPLINK_M1(pipe), m_n.link_m);
767 I915_WRITE(TRANSDPLINK_N1(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700768 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800769 I915_WRITE(PIPE_GMCH_DATA_M(pipe),
770 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
771 m_n.gmch_m);
772 I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n.gmch_n);
773 I915_WRITE(PIPE_DP_LINK_M(pipe), m_n.link_m);
774 I915_WRITE(PIPE_DP_LINK_N(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700775 }
776}
777
Keith Packardf01eca22011-09-28 16:48:10 -0700778static void ironlake_edp_pll_on(struct drm_encoder *encoder);
779static void ironlake_edp_pll_off(struct drm_encoder *encoder);
780
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700781static void
782intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
783 struct drm_display_mode *adjusted_mode)
784{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800785 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100786 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +0100787 struct drm_crtc *crtc = intel_dp->base.base.crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
789
Keith Packardf01eca22011-09-28 16:48:10 -0700790 /* Turn on the eDP PLL if needed */
791 if (is_edp(intel_dp)) {
792 if (!is_pch_edp(intel_dp))
793 ironlake_edp_pll_on(encoder);
794 else
795 ironlake_edp_pll_off(encoder);
796 }
797
Chris Wilsone953fd72011-02-21 22:23:52 +0000798 intel_dp->DP = DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
799 intel_dp->DP |= intel_dp->color_range;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400800
801 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100802 intel_dp->DP |= DP_SYNC_HS_HIGH;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400803 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100804 intel_dp->DP |= DP_SYNC_VS_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700805
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700806 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100807 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800808 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100809 intel_dp->DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700810
Chris Wilsonea5b2132010-08-04 13:50:23 +0100811 switch (intel_dp->lane_count) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700812 case 1:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100813 intel_dp->DP |= DP_PORT_WIDTH_1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700814 break;
815 case 2:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100816 intel_dp->DP |= DP_PORT_WIDTH_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700817 break;
818 case 4:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100819 intel_dp->DP |= DP_PORT_WIDTH_4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700820 break;
821 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100822 if (intel_dp->has_audio)
823 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700824
Chris Wilsonea5b2132010-08-04 13:50:23 +0100825 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
826 intel_dp->link_configuration[0] = intel_dp->link_bw;
827 intel_dp->link_configuration[1] = intel_dp->lane_count;
Adam Jacksona2cab1b2011-07-12 17:38:05 -0400828 intel_dp->link_configuration[8] = DP_SET_ANSI_8B10B;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700829
830 /*
Adam Jackson9962c922010-05-13 14:45:42 -0400831 * Check for DPCD version > 1.1 and enhanced framing support
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700832 */
Jesse Barnes7183dc22011-07-07 11:10:58 -0700833 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
834 (intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100835 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
836 intel_dp->DP |= DP_ENHANCED_FRAMING;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700837 }
838
Zhenyu Wange3421a12010-04-08 09:43:27 +0800839 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
840 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100841 intel_dp->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800842
Jesse Barnes895692b2010-10-07 16:01:23 -0700843 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800844 /* don't miss out required setting for eDP */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100845 intel_dp->DP |= DP_PLL_ENABLE;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800846 if (adjusted_mode->clock < 200000)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100847 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800848 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100849 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800850 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700851}
852
Jesse Barnes5d613502011-01-24 17:10:54 -0800853static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp)
854{
855 struct drm_device *dev = intel_dp->base.base.dev;
856 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packardf01eca22011-09-28 16:48:10 -0700857 u32 pp, pp_status;
Jesse Barnes5d613502011-01-24 17:10:54 -0800858
Keith Packard97af61f572011-09-28 16:23:51 -0700859 if (!is_edp(intel_dp))
860 return;
Keith Packardf01eca22011-09-28 16:48:10 -0700861 DRM_DEBUG_KMS("Turn eDP VDD on\n");
Jesse Barnes5d613502011-01-24 17:10:54 -0800862 /*
863 * If the panel wasn't on, make sure there's not a currently
864 * active PP sequence before enabling AUX VDD.
865 */
Keith Packardf01eca22011-09-28 16:48:10 -0700866 pp_status = I915_READ(PCH_PP_STATUS);
Jesse Barnes5d613502011-01-24 17:10:54 -0800867
868 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700869 pp &= ~PANEL_UNLOCK_MASK;
870 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes5d613502011-01-24 17:10:54 -0800871 pp |= EDP_FORCE_VDD;
872 I915_WRITE(PCH_PP_CONTROL, pp);
873 POSTING_READ(PCH_PP_CONTROL);
Keith Packardf01eca22011-09-28 16:48:10 -0700874 DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
875 I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
876 if (!(pp_status & PP_ON)) {
877 msleep(intel_dp->panel_power_up_delay);
878 DRM_DEBUG_KMS("eDP VDD was not on\n");
879 }
Jesse Barnes5d613502011-01-24 17:10:54 -0800880}
881
882static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp)
883{
884 struct drm_device *dev = intel_dp->base.base.dev;
885 struct drm_i915_private *dev_priv = dev->dev_private;
886 u32 pp;
887
Keith Packard97af61f572011-09-28 16:23:51 -0700888 if (!is_edp(intel_dp))
889 return;
Keith Packardf01eca22011-09-28 16:48:10 -0700890 DRM_DEBUG_KMS("Turn eDP VDD off\n");
Jesse Barnes5d613502011-01-24 17:10:54 -0800891 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700892 pp &= ~PANEL_UNLOCK_MASK;
893 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes5d613502011-01-24 17:10:54 -0800894 pp &= ~EDP_FORCE_VDD;
895 I915_WRITE(PCH_PP_CONTROL, pp);
896 POSTING_READ(PCH_PP_CONTROL);
897
898 /* Make sure sequencer is idle before allowing subsequent activity */
Keith Packardf01eca22011-09-28 16:48:10 -0700899 DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
900 I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
901 msleep(intel_dp->panel_power_cycle_delay);
Jesse Barnes5d613502011-01-24 17:10:54 -0800902}
903
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700904/* Returns true if the panel was already on when called */
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700905static bool ironlake_edp_panel_on (struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -0700906{
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700907 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes9934c132010-07-22 13:18:19 -0700908 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700909 u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_STATE_ON_IDLE;
Jesse Barnes9934c132010-07-22 13:18:19 -0700910
Keith Packard97af61f572011-09-28 16:23:51 -0700911 if (!is_edp(intel_dp))
Keith Packardf01eca22011-09-28 16:48:10 -0700912 return true;
Chris Wilson913d8d12010-08-07 11:01:35 +0100913 if (I915_READ(PCH_PP_STATUS) & PP_ON)
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700914 return true;
Jesse Barnes9934c132010-07-22 13:18:19 -0700915
916 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700917 pp &= ~PANEL_UNLOCK_MASK;
918 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700919
920 /* ILK workaround: disable reset around power sequence */
921 pp &= ~PANEL_POWER_RESET;
922 I915_WRITE(PCH_PP_CONTROL, pp);
923 POSTING_READ(PCH_PP_CONTROL);
924
Keith Packard1c0ae802011-09-19 13:59:29 -0700925 pp |= POWER_TARGET_ON;
Jesse Barnes9934c132010-07-22 13:18:19 -0700926 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700927 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700928
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700929 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_on_mask) == idle_on_mask,
930 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100931 DRM_ERROR("panel on wait timed out: 0x%08x\n",
932 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700933
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700934 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700935 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700936 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700937
938 return false;
Jesse Barnes9934c132010-07-22 13:18:19 -0700939}
940
Keith Packardf01eca22011-09-28 16:48:10 -0700941static void ironlake_edp_panel_off(struct drm_encoder *encoder)
Jesse Barnes9934c132010-07-22 13:18:19 -0700942{
Keith Packardf01eca22011-09-28 16:48:10 -0700943 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
944 struct drm_device *dev = encoder->dev;
Jesse Barnes9934c132010-07-22 13:18:19 -0700945 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700946 u32 pp, idle_off_mask = PP_ON | PP_SEQUENCE_MASK |
947 PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK;
Jesse Barnes9934c132010-07-22 13:18:19 -0700948
Keith Packard97af61f572011-09-28 16:23:51 -0700949 if (!is_edp(intel_dp))
950 return;
Jesse Barnes9934c132010-07-22 13:18:19 -0700951 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700952 pp &= ~PANEL_UNLOCK_MASK;
953 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700954
955 /* ILK workaround: disable reset around power sequence */
956 pp &= ~PANEL_POWER_RESET;
957 I915_WRITE(PCH_PP_CONTROL, pp);
958 POSTING_READ(PCH_PP_CONTROL);
959
Jesse Barnes9934c132010-07-22 13:18:19 -0700960 pp &= ~POWER_TARGET_ON;
961 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700962 POSTING_READ(PCH_PP_CONTROL);
Keith Packardf01eca22011-09-28 16:48:10 -0700963 msleep(intel_dp->panel_power_cycle_delay);
Jesse Barnes9934c132010-07-22 13:18:19 -0700964
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700965 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_off_mask) == 0, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100966 DRM_ERROR("panel off wait timed out: 0x%08x\n",
967 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700968
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700969 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700970 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700971 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700972}
973
Keith Packardf01eca22011-09-28 16:48:10 -0700974static void ironlake_edp_backlight_on (struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800975{
Keith Packardf01eca22011-09-28 16:48:10 -0700976 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800977 struct drm_i915_private *dev_priv = dev->dev_private;
978 u32 pp;
979
Keith Packardf01eca22011-09-28 16:48:10 -0700980 if (!is_edp(intel_dp))
981 return;
982
Zhao Yakui28c97732009-10-09 11:39:41 +0800983 DRM_DEBUG_KMS("\n");
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700984 /*
985 * If we enable the backlight right away following a panel power
986 * on, we may see slight flicker as the panel syncs with the eDP
987 * link. So delay a bit to make sure the image is solid before
988 * allowing it to appear.
989 */
Keith Packardf01eca22011-09-28 16:48:10 -0700990 msleep(intel_dp->backlight_on_delay);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800991 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700992 pp &= ~PANEL_UNLOCK_MASK;
993 pp |= PANEL_UNLOCK_REGS;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800994 pp |= EDP_BLC_ENABLE;
995 I915_WRITE(PCH_PP_CONTROL, pp);
Keith Packardf01eca22011-09-28 16:48:10 -0700996 POSTING_READ(PCH_PP_CONTROL);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800997}
998
Keith Packardf01eca22011-09-28 16:48:10 -0700999static void ironlake_edp_backlight_off (struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001000{
Keith Packardf01eca22011-09-28 16:48:10 -07001001 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001002 struct drm_i915_private *dev_priv = dev->dev_private;
1003 u32 pp;
1004
Keith Packardf01eca22011-09-28 16:48:10 -07001005 if (!is_edp(intel_dp))
1006 return;
1007
Zhao Yakui28c97732009-10-09 11:39:41 +08001008 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001009 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -07001010 pp &= ~PANEL_UNLOCK_MASK;
1011 pp |= PANEL_UNLOCK_REGS;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001012 pp &= ~EDP_BLC_ENABLE;
1013 I915_WRITE(PCH_PP_CONTROL, pp);
Keith Packardf01eca22011-09-28 16:48:10 -07001014 POSTING_READ(PCH_PP_CONTROL);
1015 msleep(intel_dp->backlight_off_delay);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001016}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001017
Jesse Barnesd240f202010-08-13 15:43:26 -07001018static void ironlake_edp_pll_on(struct drm_encoder *encoder)
1019{
1020 struct drm_device *dev = encoder->dev;
1021 struct drm_i915_private *dev_priv = dev->dev_private;
1022 u32 dpa_ctl;
1023
1024 DRM_DEBUG_KMS("\n");
1025 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -07001026 dpa_ctl |= DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -07001027 I915_WRITE(DP_A, dpa_ctl);
Jesse Barnes298b0b32010-10-07 16:01:24 -07001028 POSTING_READ(DP_A);
1029 udelay(200);
Jesse Barnesd240f202010-08-13 15:43:26 -07001030}
1031
1032static void ironlake_edp_pll_off(struct drm_encoder *encoder)
1033{
1034 struct drm_device *dev = encoder->dev;
1035 struct drm_i915_private *dev_priv = dev->dev_private;
1036 u32 dpa_ctl;
1037
1038 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -07001039 dpa_ctl &= ~DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -07001040 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +01001041 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -07001042 udelay(200);
1043}
1044
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001045/* If the sink supports it, try to set the power state appropriately */
1046static void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
1047{
1048 int ret, i;
1049
1050 /* Should have a valid DPCD by this point */
1051 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
1052 return;
1053
1054 if (mode != DRM_MODE_DPMS_ON) {
1055 ret = intel_dp_aux_native_write_1(intel_dp, DP_SET_POWER,
1056 DP_SET_POWER_D3);
1057 if (ret != 1)
1058 DRM_DEBUG_DRIVER("failed to write sink power state\n");
1059 } else {
1060 /*
1061 * When turning on, we need to retry for 1ms to give the sink
1062 * time to wake up.
1063 */
1064 for (i = 0; i < 3; i++) {
1065 ret = intel_dp_aux_native_write_1(intel_dp,
1066 DP_SET_POWER,
1067 DP_SET_POWER_D0);
1068 if (ret == 1)
1069 break;
1070 msleep(1);
1071 }
1072 }
1073}
1074
Jesse Barnesd240f202010-08-13 15:43:26 -07001075static void intel_dp_prepare(struct drm_encoder *encoder)
1076{
1077 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -07001078
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001079 /* Wake up the sink first */
Keith Packardf58ff852011-09-28 16:44:14 -07001080 ironlake_edp_panel_vdd_on(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001081 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
Keith Packardf58ff852011-09-28 16:44:14 -07001082 ironlake_edp_panel_vdd_off(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001083
Keith Packardf01eca22011-09-28 16:48:10 -07001084 /* Make sure the panel is off before trying to
1085 * change the mode
1086 */
1087 ironlake_edp_backlight_off(intel_dp);
Jesse Barnes736085b2010-10-08 10:35:55 -07001088 intel_dp_link_down(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001089 ironlake_edp_panel_off(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -07001090}
1091
1092static void intel_dp_commit(struct drm_encoder *encoder)
1093{
1094 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -07001095
Keith Packard97af61f572011-09-28 16:23:51 -07001096 ironlake_edp_panel_vdd_on(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001097 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001098 intel_dp_start_link_train(intel_dp);
Keith Packard97af61f572011-09-28 16:23:51 -07001099 ironlake_edp_panel_on(intel_dp);
1100 ironlake_edp_panel_vdd_off(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001101 intel_dp_complete_link_train(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001102 ironlake_edp_backlight_on(intel_dp);
Keith Packardd2b996a2011-07-25 22:37:51 -07001103
1104 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
Jesse Barnesd240f202010-08-13 15:43:26 -07001105}
1106
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001107static void
1108intel_dp_dpms(struct drm_encoder *encoder, int mode)
1109{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001110 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001111 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001112 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001113 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001114
1115 if (mode != DRM_MODE_DPMS_ON) {
Keith Packard245e2702011-10-05 19:53:09 -07001116 ironlake_edp_panel_vdd_on(intel_dp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001117 if (is_edp(intel_dp))
Keith Packardf01eca22011-09-28 16:48:10 -07001118 ironlake_edp_backlight_off(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001119 intel_dp_sink_dpms(intel_dp, mode);
Jesse Barnes736085b2010-10-08 10:35:55 -07001120 intel_dp_link_down(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001121 ironlake_edp_panel_off(encoder);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001122 if (is_edp(intel_dp) && !is_pch_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -07001123 ironlake_edp_pll_off(encoder);
Keith Packard245e2702011-10-05 19:53:09 -07001124 ironlake_edp_panel_vdd_off(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001125 } else {
Keith Packard97af61f572011-09-28 16:23:51 -07001126 ironlake_edp_panel_vdd_on(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001127 intel_dp_sink_dpms(intel_dp, mode);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001128 if (!(dp_reg & DP_PORT_EN)) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001129 intel_dp_start_link_train(intel_dp);
Keith Packard97af61f572011-09-28 16:23:51 -07001130 ironlake_edp_panel_on(intel_dp);
1131 ironlake_edp_panel_vdd_off(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001132 intel_dp_complete_link_train(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001133 ironlake_edp_backlight_on(intel_dp);
Keith Packardbee7eb22011-09-28 16:28:00 -07001134 } else
1135 ironlake_edp_panel_vdd_off(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001136 }
Keith Packardd2b996a2011-07-25 22:37:51 -07001137 intel_dp->dpms_mode = mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001138}
1139
1140/*
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001141 * Native read with retry for link status and receiver capability reads for
1142 * cases where the sink may still be asleep.
1143 */
1144static bool
1145intel_dp_aux_native_read_retry(struct intel_dp *intel_dp, uint16_t address,
1146 uint8_t *recv, int recv_bytes)
1147{
1148 int ret, i;
1149
1150 /*
1151 * Sinks are *supposed* to come up within 1ms from an off state,
1152 * but we're also supposed to retry 3 times per the spec.
1153 */
1154 for (i = 0; i < 3; i++) {
1155 ret = intel_dp_aux_native_read(intel_dp, address, recv,
1156 recv_bytes);
1157 if (ret == recv_bytes)
1158 return true;
1159 msleep(1);
1160 }
1161
1162 return false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001163}
1164
1165/*
1166 * Fetch AUX CH registers 0x202 - 0x207 which contain
1167 * link status information
1168 */
1169static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001170intel_dp_get_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001171{
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001172 return intel_dp_aux_native_read_retry(intel_dp,
1173 DP_LANE0_1_STATUS,
1174 intel_dp->link_status,
1175 DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001176}
1177
1178static uint8_t
1179intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1180 int r)
1181{
1182 return link_status[r - DP_LANE0_1_STATUS];
1183}
1184
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001185static uint8_t
1186intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
1187 int lane)
1188{
1189 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1190 int s = ((lane & 1) ?
1191 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1192 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1193 uint8_t l = intel_dp_link_status(link_status, i);
1194
1195 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1196}
1197
1198static uint8_t
1199intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1200 int lane)
1201{
1202 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1203 int s = ((lane & 1) ?
1204 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1205 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1206 uint8_t l = intel_dp_link_status(link_status, i);
1207
1208 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1209}
1210
1211
1212#if 0
1213static char *voltage_names[] = {
1214 "0.4V", "0.6V", "0.8V", "1.2V"
1215};
1216static char *pre_emph_names[] = {
1217 "0dB", "3.5dB", "6dB", "9.5dB"
1218};
1219static char *link_train_names[] = {
1220 "pattern 1", "pattern 2", "idle", "off"
1221};
1222#endif
1223
1224/*
1225 * These are source-specific values; current Intel hardware supports
1226 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1227 */
1228#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1229
1230static uint8_t
1231intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1232{
1233 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1234 case DP_TRAIN_VOLTAGE_SWING_400:
1235 return DP_TRAIN_PRE_EMPHASIS_6;
1236 case DP_TRAIN_VOLTAGE_SWING_600:
1237 return DP_TRAIN_PRE_EMPHASIS_6;
1238 case DP_TRAIN_VOLTAGE_SWING_800:
1239 return DP_TRAIN_PRE_EMPHASIS_3_5;
1240 case DP_TRAIN_VOLTAGE_SWING_1200:
1241 default:
1242 return DP_TRAIN_PRE_EMPHASIS_0;
1243 }
1244}
1245
1246static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001247intel_get_adjust_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001248{
1249 uint8_t v = 0;
1250 uint8_t p = 0;
1251 int lane;
1252
Jesse Barnes33a34e42010-09-08 12:42:02 -07001253 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1254 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1255 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001256
1257 if (this_v > v)
1258 v = this_v;
1259 if (this_p > p)
1260 p = this_p;
1261 }
1262
1263 if (v >= I830_DP_VOLTAGE_MAX)
1264 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1265
1266 if (p >= intel_dp_pre_emphasis_max(v))
1267 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1268
1269 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001270 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001271}
1272
1273static uint32_t
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001274intel_dp_signal_levels(uint8_t train_set, int lane_count)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001275{
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001276 uint32_t signal_levels = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001277
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001278 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001279 case DP_TRAIN_VOLTAGE_SWING_400:
1280 default:
1281 signal_levels |= DP_VOLTAGE_0_4;
1282 break;
1283 case DP_TRAIN_VOLTAGE_SWING_600:
1284 signal_levels |= DP_VOLTAGE_0_6;
1285 break;
1286 case DP_TRAIN_VOLTAGE_SWING_800:
1287 signal_levels |= DP_VOLTAGE_0_8;
1288 break;
1289 case DP_TRAIN_VOLTAGE_SWING_1200:
1290 signal_levels |= DP_VOLTAGE_1_2;
1291 break;
1292 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001293 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001294 case DP_TRAIN_PRE_EMPHASIS_0:
1295 default:
1296 signal_levels |= DP_PRE_EMPHASIS_0;
1297 break;
1298 case DP_TRAIN_PRE_EMPHASIS_3_5:
1299 signal_levels |= DP_PRE_EMPHASIS_3_5;
1300 break;
1301 case DP_TRAIN_PRE_EMPHASIS_6:
1302 signal_levels |= DP_PRE_EMPHASIS_6;
1303 break;
1304 case DP_TRAIN_PRE_EMPHASIS_9_5:
1305 signal_levels |= DP_PRE_EMPHASIS_9_5;
1306 break;
1307 }
1308 return signal_levels;
1309}
1310
Zhenyu Wange3421a12010-04-08 09:43:27 +08001311/* Gen6's DP voltage swing and pre-emphasis control */
1312static uint32_t
1313intel_gen6_edp_signal_levels(uint8_t train_set)
1314{
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001315 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1316 DP_TRAIN_PRE_EMPHASIS_MASK);
1317 switch (signal_levels) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001318 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001319 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
1320 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
1321 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
1322 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001323 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001324 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
1325 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001326 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001327 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
1328 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001329 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001330 case DP_TRAIN_VOLTAGE_SWING_1200 | DP_TRAIN_PRE_EMPHASIS_0:
1331 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001332 default:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001333 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
1334 "0x%x\n", signal_levels);
1335 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001336 }
1337}
1338
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001339static uint8_t
1340intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1341 int lane)
1342{
1343 int i = DP_LANE0_1_STATUS + (lane >> 1);
1344 int s = (lane & 1) * 4;
1345 uint8_t l = intel_dp_link_status(link_status, i);
1346
1347 return (l >> s) & 0xf;
1348}
1349
1350/* Check for clock recovery is done on all channels */
1351static bool
1352intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1353{
1354 int lane;
1355 uint8_t lane_status;
1356
1357 for (lane = 0; lane < lane_count; lane++) {
1358 lane_status = intel_get_lane_status(link_status, lane);
1359 if ((lane_status & DP_LANE_CR_DONE) == 0)
1360 return false;
1361 }
1362 return true;
1363}
1364
1365/* Check to see if channel eq is done on all channels */
1366#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1367 DP_LANE_CHANNEL_EQ_DONE|\
1368 DP_LANE_SYMBOL_LOCKED)
1369static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001370intel_channel_eq_ok(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001371{
1372 uint8_t lane_align;
1373 uint8_t lane_status;
1374 int lane;
1375
Jesse Barnes33a34e42010-09-08 12:42:02 -07001376 lane_align = intel_dp_link_status(intel_dp->link_status,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001377 DP_LANE_ALIGN_STATUS_UPDATED);
1378 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1379 return false;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001380 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1381 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001382 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1383 return false;
1384 }
1385 return true;
1386}
1387
1388static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001389intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001390 uint32_t dp_reg_value,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001391 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001392{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001393 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001394 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001395 int ret;
1396
Chris Wilsonea5b2132010-08-04 13:50:23 +01001397 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1398 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001399
Chris Wilsonea5b2132010-08-04 13:50:23 +01001400 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001401 DP_TRAINING_PATTERN_SET,
1402 dp_train_pat);
1403
Chris Wilsonea5b2132010-08-04 13:50:23 +01001404 ret = intel_dp_aux_native_write(intel_dp,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001405 DP_TRAINING_LANE0_SET,
1406 intel_dp->train_set, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001407 if (ret != 4)
1408 return false;
1409
1410 return true;
1411}
1412
Jesse Barnes33a34e42010-09-08 12:42:02 -07001413/* Enable corresponding port and start training pattern 1 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001414static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001415intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001416{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001417 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001418 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson58e10eb2010-10-03 10:56:11 +01001419 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001420 int i;
1421 uint8_t voltage;
1422 bool clock_recovery = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001423 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001424 u32 reg;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001425 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001426
Adam Jacksone8519462011-07-21 17:48:38 -04001427 /*
1428 * On CPT we have to enable the port in training pattern 1, which
1429 * will happen below in intel_dp_set_link_train. Otherwise, enable
1430 * the port and wait for it to become active.
1431 */
1432 if (!HAS_PCH_CPT(dev)) {
1433 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1434 POSTING_READ(intel_dp->output_reg);
1435 intel_wait_for_vblank(dev, intel_crtc->pipe);
1436 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001437
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001438 /* Write the link configuration data */
1439 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1440 intel_dp->link_configuration,
1441 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001442
1443 DP |= DP_PORT_EN;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001444 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001445 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1446 else
1447 DP &= ~DP_LINK_TRAIN_MASK;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001448 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001449 voltage = 0xff;
1450 tries = 0;
1451 clock_recovery = false;
1452 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001453 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001454 uint32_t signal_levels;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001455 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001456 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001457 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1458 } else {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001459 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001460 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1461 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001462
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001463 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001464 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1465 else
1466 reg = DP | DP_LINK_TRAIN_PAT_1;
1467
Chris Wilsonea5b2132010-08-04 13:50:23 +01001468 if (!intel_dp_set_link_train(intel_dp, reg,
Adam Jackson81055852011-07-21 17:48:37 -04001469 DP_TRAINING_PATTERN_1 |
1470 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001471 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001472 /* Set training pattern 1 */
1473
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001474 udelay(100);
1475 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001476 break;
1477
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001478 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1479 clock_recovery = true;
1480 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001481 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001482
1483 /* Check to see if we've tried the max voltage */
1484 for (i = 0; i < intel_dp->lane_count; i++)
1485 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1486 break;
1487 if (i == intel_dp->lane_count)
1488 break;
1489
1490 /* Check to see if we've tried the same voltage 5 times */
1491 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1492 ++tries;
1493 if (tries == 5)
1494 break;
1495 } else
1496 tries = 0;
1497 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1498
1499 /* Compute new intel_dp->train_set as requested by target */
1500 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001501 }
1502
Jesse Barnes33a34e42010-09-08 12:42:02 -07001503 intel_dp->DP = DP;
1504}
1505
1506static void
1507intel_dp_complete_link_train(struct intel_dp *intel_dp)
1508{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001509 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001510 struct drm_i915_private *dev_priv = dev->dev_private;
1511 bool channel_eq = false;
Jesse Barnes37f80972011-01-05 14:45:24 -08001512 int tries, cr_tries;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001513 u32 reg;
1514 uint32_t DP = intel_dp->DP;
1515
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001516 /* channel equalization */
1517 tries = 0;
Jesse Barnes37f80972011-01-05 14:45:24 -08001518 cr_tries = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001519 channel_eq = false;
1520 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001521 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001522 uint32_t signal_levels;
1523
Jesse Barnes37f80972011-01-05 14:45:24 -08001524 if (cr_tries > 5) {
1525 DRM_ERROR("failed to train DP, aborting\n");
1526 intel_dp_link_down(intel_dp);
1527 break;
1528 }
1529
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001530 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001531 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001532 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1533 } else {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001534 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001535 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1536 }
1537
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001538 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001539 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1540 else
1541 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001542
1543 /* channel eq pattern */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001544 if (!intel_dp_set_link_train(intel_dp, reg,
Adam Jackson81055852011-07-21 17:48:37 -04001545 DP_TRAINING_PATTERN_2 |
1546 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001547 break;
1548
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001549 udelay(400);
1550 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001551 break;
Jesse Barnes869184a2010-10-07 16:01:22 -07001552
Jesse Barnes37f80972011-01-05 14:45:24 -08001553 /* Make sure clock is still ok */
1554 if (!intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1555 intel_dp_start_link_train(intel_dp);
1556 cr_tries++;
1557 continue;
1558 }
1559
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001560 if (intel_channel_eq_ok(intel_dp)) {
1561 channel_eq = true;
1562 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001563 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001564
Jesse Barnes37f80972011-01-05 14:45:24 -08001565 /* Try 5 times, then try clock recovery if that fails */
1566 if (tries > 5) {
1567 intel_dp_link_down(intel_dp);
1568 intel_dp_start_link_train(intel_dp);
1569 tries = 0;
1570 cr_tries++;
1571 continue;
1572 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001573
1574 /* Compute new intel_dp->train_set as requested by target */
1575 intel_get_adjust_train(intel_dp);
1576 ++tries;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001577 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001578
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001579 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001580 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1581 else
1582 reg = DP | DP_LINK_TRAIN_OFF;
1583
Chris Wilsonea5b2132010-08-04 13:50:23 +01001584 I915_WRITE(intel_dp->output_reg, reg);
1585 POSTING_READ(intel_dp->output_reg);
1586 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001587 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1588}
1589
1590static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001591intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001592{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001593 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001594 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001595 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001596
Chris Wilson1b39d6f2010-12-06 11:20:45 +00001597 if ((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0)
1598 return;
1599
Zhao Yakui28c97732009-10-09 11:39:41 +08001600 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001601
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001602 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001603 DP &= ~DP_PLL_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001604 I915_WRITE(intel_dp->output_reg, DP);
1605 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001606 udelay(100);
1607 }
1608
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001609 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001610 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001611 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001612 } else {
1613 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001614 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001615 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01001616 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001617
Chris Wilsonfe255d02010-09-11 21:37:48 +01001618 msleep(17);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001619
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001620 if (is_edp(intel_dp))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001621 DP |= DP_LINK_TRAIN_OFF;
Eric Anholt5bddd172010-11-18 09:32:59 +08001622
Chris Wilson1b39d6f2010-12-06 11:20:45 +00001623 if (!HAS_PCH_CPT(dev) &&
1624 I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
Chris Wilson31acbcc2011-04-17 06:38:35 +01001625 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1626
Eric Anholt5bddd172010-11-18 09:32:59 +08001627 /* Hardware workaround: leaving our transcoder select
1628 * set to transcoder B while it's off will prevent the
1629 * corresponding HDMI output on transcoder A.
1630 *
1631 * Combine this with another hardware workaround:
1632 * transcoder select bit can only be cleared while the
1633 * port is enabled.
1634 */
1635 DP &= ~DP_PIPEB_SELECT;
1636 I915_WRITE(intel_dp->output_reg, DP);
1637
1638 /* Changes to enable or select take place the vblank
1639 * after being written.
1640 */
Chris Wilson31acbcc2011-04-17 06:38:35 +01001641 if (crtc == NULL) {
1642 /* We can arrive here never having been attached
1643 * to a CRTC, for instance, due to inheriting
1644 * random state from the BIOS.
1645 *
1646 * If the pipe is not running, play safe and
1647 * wait for the clocks to stabilise before
1648 * continuing.
1649 */
1650 POSTING_READ(intel_dp->output_reg);
1651 msleep(50);
1652 } else
1653 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
Eric Anholt5bddd172010-11-18 09:32:59 +08001654 }
1655
Chris Wilsonea5b2132010-08-04 13:50:23 +01001656 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1657 POSTING_READ(intel_dp->output_reg);
Keith Packardf01eca22011-09-28 16:48:10 -07001658 msleep(intel_dp->panel_power_down_delay);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001659}
1660
Keith Packard26d61aa2011-07-25 20:01:09 -07001661static bool
1662intel_dp_get_dpcd(struct intel_dp *intel_dp)
Keith Packard92fd8fd2011-07-25 19:50:10 -07001663{
Keith Packard92fd8fd2011-07-25 19:50:10 -07001664 if (intel_dp_aux_native_read_retry(intel_dp, 0x000, intel_dp->dpcd,
1665 sizeof (intel_dp->dpcd)) &&
1666 (intel_dp->dpcd[DP_DPCD_REV] != 0)) {
Keith Packard26d61aa2011-07-25 20:01:09 -07001667 return true;
Keith Packard92fd8fd2011-07-25 19:50:10 -07001668 }
1669
Keith Packard26d61aa2011-07-25 20:01:09 -07001670 return false;
Keith Packard92fd8fd2011-07-25 19:50:10 -07001671}
1672
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001673/*
1674 * According to DP spec
1675 * 5.1.2:
1676 * 1. Read DPCD
1677 * 2. Configure link according to Receiver Capabilities
1678 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1679 * 4. Check link status on receipt of hot-plug interrupt
1680 */
1681
1682static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001683intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001684{
Keith Packardd2b996a2011-07-25 22:37:51 -07001685 if (intel_dp->dpms_mode != DRM_MODE_DPMS_ON)
1686 return;
Jesse Barnes59cd09e2011-07-07 11:10:59 -07001687
Chris Wilson4ef69c72010-09-09 15:14:28 +01001688 if (!intel_dp->base.base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001689 return;
1690
Keith Packard92fd8fd2011-07-25 19:50:10 -07001691 /* Try to read receiver status if the link appears to be up */
Jesse Barnes33a34e42010-09-08 12:42:02 -07001692 if (!intel_dp_get_link_status(intel_dp)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001693 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001694 return;
1695 }
1696
Keith Packard92fd8fd2011-07-25 19:50:10 -07001697 /* Now read the DPCD to see if it's actually running */
Keith Packard26d61aa2011-07-25 20:01:09 -07001698 if (!intel_dp_get_dpcd(intel_dp)) {
Jesse Barnes59cd09e2011-07-07 11:10:59 -07001699 intel_dp_link_down(intel_dp);
1700 return;
1701 }
1702
Jesse Barnes33a34e42010-09-08 12:42:02 -07001703 if (!intel_channel_eq_ok(intel_dp)) {
Keith Packard92fd8fd2011-07-25 19:50:10 -07001704 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
1705 drm_get_encoder_name(&intel_dp->base.base));
Jesse Barnes33a34e42010-09-08 12:42:02 -07001706 intel_dp_start_link_train(intel_dp);
1707 intel_dp_complete_link_train(intel_dp);
1708 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001709}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001710
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001711static enum drm_connector_status
Keith Packard26d61aa2011-07-25 20:01:09 -07001712intel_dp_detect_dpcd(struct intel_dp *intel_dp)
Adam Jackson71ba90002011-07-12 17:38:04 -04001713{
Keith Packard26d61aa2011-07-25 20:01:09 -07001714 if (intel_dp_get_dpcd(intel_dp))
1715 return connector_status_connected;
1716 return connector_status_disconnected;
Adam Jackson71ba90002011-07-12 17:38:04 -04001717}
1718
1719static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001720ironlake_dp_detect(struct intel_dp *intel_dp)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001721{
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001722 enum drm_connector_status status;
1723
Chris Wilsonfe16d942011-02-12 10:29:38 +00001724 /* Can't disconnect eDP, but you can close the lid... */
1725 if (is_edp(intel_dp)) {
1726 status = intel_panel_detect(intel_dp->base.base.dev);
1727 if (status == connector_status_unknown)
1728 status = connector_status_connected;
1729 return status;
1730 }
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001731
Keith Packard26d61aa2011-07-25 20:01:09 -07001732 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001733}
1734
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001735static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001736g4x_dp_detect(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001737{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001738 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001739 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001740 uint32_t temp, bit;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001741
Chris Wilsonea5b2132010-08-04 13:50:23 +01001742 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001743 case DP_B:
1744 bit = DPB_HOTPLUG_INT_STATUS;
1745 break;
1746 case DP_C:
1747 bit = DPC_HOTPLUG_INT_STATUS;
1748 break;
1749 case DP_D:
1750 bit = DPD_HOTPLUG_INT_STATUS;
1751 break;
1752 default:
1753 return connector_status_unknown;
1754 }
1755
1756 temp = I915_READ(PORT_HOTPLUG_STAT);
1757
1758 if ((temp & bit) == 0)
1759 return connector_status_disconnected;
1760
Keith Packard26d61aa2011-07-25 20:01:09 -07001761 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001762}
1763
Keith Packard8c241fe2011-09-28 16:38:44 -07001764static struct edid *
1765intel_dp_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
1766{
1767 struct intel_dp *intel_dp = intel_attached_dp(connector);
1768 struct edid *edid;
1769
1770 ironlake_edp_panel_vdd_on(intel_dp);
1771 edid = drm_get_edid(connector, adapter);
1772 ironlake_edp_panel_vdd_off(intel_dp);
1773 return edid;
1774}
1775
1776static int
1777intel_dp_get_edid_modes(struct drm_connector *connector, struct i2c_adapter *adapter)
1778{
1779 struct intel_dp *intel_dp = intel_attached_dp(connector);
1780 int ret;
1781
1782 ironlake_edp_panel_vdd_on(intel_dp);
1783 ret = intel_ddc_get_modes(connector, adapter);
1784 ironlake_edp_panel_vdd_off(intel_dp);
1785 return ret;
1786}
1787
1788
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001789/**
1790 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1791 *
1792 * \return true if DP port is connected.
1793 * \return false if DP port is disconnected.
1794 */
1795static enum drm_connector_status
1796intel_dp_detect(struct drm_connector *connector, bool force)
1797{
1798 struct intel_dp *intel_dp = intel_attached_dp(connector);
1799 struct drm_device *dev = intel_dp->base.base.dev;
1800 enum drm_connector_status status;
1801 struct edid *edid = NULL;
1802
1803 intel_dp->has_audio = false;
1804
1805 if (HAS_PCH_SPLIT(dev))
1806 status = ironlake_dp_detect(intel_dp);
1807 else
1808 status = g4x_dp_detect(intel_dp);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04001809
Adam Jacksonac66ae82011-07-12 17:38:03 -04001810 DRM_DEBUG_KMS("DPCD: %02hx%02hx%02hx%02hx%02hx%02hx%02hx%02hx\n",
1811 intel_dp->dpcd[0], intel_dp->dpcd[1], intel_dp->dpcd[2],
1812 intel_dp->dpcd[3], intel_dp->dpcd[4], intel_dp->dpcd[5],
1813 intel_dp->dpcd[6], intel_dp->dpcd[7]);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04001814
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001815 if (status != connector_status_connected)
1816 return status;
1817
Chris Wilsonf6849602010-09-19 09:29:33 +01001818 if (intel_dp->force_audio) {
1819 intel_dp->has_audio = intel_dp->force_audio > 0;
1820 } else {
Keith Packard8c241fe2011-09-28 16:38:44 -07001821 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
Chris Wilsonf6849602010-09-19 09:29:33 +01001822 if (edid) {
1823 intel_dp->has_audio = drm_detect_monitor_audio(edid);
1824 connector->display_info.raw_edid = NULL;
1825 kfree(edid);
1826 }
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001827 }
1828
1829 return connector_status_connected;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001830}
1831
1832static int intel_dp_get_modes(struct drm_connector *connector)
1833{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001834 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001835 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001836 struct drm_i915_private *dev_priv = dev->dev_private;
1837 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001838
1839 /* We should parse the EDID data and find out if it has an audio sink
1840 */
1841
Keith Packard8c241fe2011-09-28 16:38:44 -07001842 ret = intel_dp_get_edid_modes(connector, &intel_dp->adapter);
Zhao Yakuib9efc482010-07-19 09:43:11 +01001843 if (ret) {
Jesse Barnes4d926462010-10-07 16:01:07 -07001844 if (is_edp(intel_dp) && !dev_priv->panel_fixed_mode) {
Zhao Yakuib9efc482010-07-19 09:43:11 +01001845 struct drm_display_mode *newmode;
1846 list_for_each_entry(newmode, &connector->probed_modes,
1847 head) {
1848 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1849 dev_priv->panel_fixed_mode =
1850 drm_mode_duplicate(dev, newmode);
1851 break;
1852 }
1853 }
1854 }
1855
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001856 return ret;
Zhao Yakuib9efc482010-07-19 09:43:11 +01001857 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001858
1859 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Jesse Barnes4d926462010-10-07 16:01:07 -07001860 if (is_edp(intel_dp)) {
Keith Packard47f0eb22011-09-19 14:33:26 -07001861 /* initialize panel mode from VBT if available for eDP */
1862 if (dev_priv->panel_fixed_mode == NULL && dev_priv->lfp_lvds_vbt_mode != NULL) {
1863 dev_priv->panel_fixed_mode =
1864 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1865 if (dev_priv->panel_fixed_mode) {
1866 dev_priv->panel_fixed_mode->type |=
1867 DRM_MODE_TYPE_PREFERRED;
1868 }
1869 }
1870 if (dev_priv->panel_fixed_mode) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001871 struct drm_display_mode *mode;
1872 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1873 drm_mode_probed_add(connector, mode);
1874 return 1;
1875 }
1876 }
1877 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001878}
1879
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001880static bool
1881intel_dp_detect_audio(struct drm_connector *connector)
1882{
1883 struct intel_dp *intel_dp = intel_attached_dp(connector);
1884 struct edid *edid;
1885 bool has_audio = false;
1886
Keith Packard8c241fe2011-09-28 16:38:44 -07001887 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001888 if (edid) {
1889 has_audio = drm_detect_monitor_audio(edid);
1890
1891 connector->display_info.raw_edid = NULL;
1892 kfree(edid);
1893 }
1894
1895 return has_audio;
1896}
1897
Chris Wilsonf6849602010-09-19 09:29:33 +01001898static int
1899intel_dp_set_property(struct drm_connector *connector,
1900 struct drm_property *property,
1901 uint64_t val)
1902{
Chris Wilsone953fd72011-02-21 22:23:52 +00001903 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Chris Wilsonf6849602010-09-19 09:29:33 +01001904 struct intel_dp *intel_dp = intel_attached_dp(connector);
1905 int ret;
1906
1907 ret = drm_connector_property_set_value(connector, property, val);
1908 if (ret)
1909 return ret;
1910
Chris Wilson3f43c482011-05-12 22:17:24 +01001911 if (property == dev_priv->force_audio_property) {
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001912 int i = val;
1913 bool has_audio;
1914
1915 if (i == intel_dp->force_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01001916 return 0;
1917
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001918 intel_dp->force_audio = i;
Chris Wilsonf6849602010-09-19 09:29:33 +01001919
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001920 if (i == 0)
1921 has_audio = intel_dp_detect_audio(connector);
1922 else
1923 has_audio = i > 0;
1924
1925 if (has_audio == intel_dp->has_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01001926 return 0;
1927
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001928 intel_dp->has_audio = has_audio;
Chris Wilsonf6849602010-09-19 09:29:33 +01001929 goto done;
1930 }
1931
Chris Wilsone953fd72011-02-21 22:23:52 +00001932 if (property == dev_priv->broadcast_rgb_property) {
1933 if (val == !!intel_dp->color_range)
1934 return 0;
1935
1936 intel_dp->color_range = val ? DP_COLOR_RANGE_16_235 : 0;
1937 goto done;
1938 }
1939
Chris Wilsonf6849602010-09-19 09:29:33 +01001940 return -EINVAL;
1941
1942done:
1943 if (intel_dp->base.base.crtc) {
1944 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1945 drm_crtc_helper_set_mode(crtc, &crtc->mode,
1946 crtc->x, crtc->y,
1947 crtc->fb);
1948 }
1949
1950 return 0;
1951}
1952
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001953static void
1954intel_dp_destroy (struct drm_connector *connector)
1955{
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001956 struct drm_device *dev = connector->dev;
1957
1958 if (intel_dpd_is_edp(dev))
1959 intel_panel_destroy_backlight(dev);
1960
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001961 drm_sysfs_connector_remove(connector);
1962 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001963 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001964}
1965
Daniel Vetter24d05922010-08-20 18:08:28 +02001966static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1967{
1968 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1969
1970 i2c_del_adapter(&intel_dp->adapter);
1971 drm_encoder_cleanup(encoder);
1972 kfree(intel_dp);
1973}
1974
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001975static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1976 .dpms = intel_dp_dpms,
1977 .mode_fixup = intel_dp_mode_fixup,
Jesse Barnesd240f202010-08-13 15:43:26 -07001978 .prepare = intel_dp_prepare,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001979 .mode_set = intel_dp_mode_set,
Jesse Barnesd240f202010-08-13 15:43:26 -07001980 .commit = intel_dp_commit,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001981};
1982
1983static const struct drm_connector_funcs intel_dp_connector_funcs = {
1984 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001985 .detect = intel_dp_detect,
1986 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilsonf6849602010-09-19 09:29:33 +01001987 .set_property = intel_dp_set_property,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001988 .destroy = intel_dp_destroy,
1989};
1990
1991static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1992 .get_modes = intel_dp_get_modes,
1993 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01001994 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001995};
1996
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001997static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02001998 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001999};
2000
Chris Wilson995b6762010-08-20 13:23:26 +01002001static void
Eric Anholt21d40d32010-03-25 11:11:14 -07002002intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07002003{
Chris Wilsonea5b2132010-08-04 13:50:23 +01002004 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Keith Packardc8110e52009-05-06 11:51:10 -07002005
Jesse Barnes885a5012011-07-07 11:11:01 -07002006 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07002007}
2008
Zhenyu Wange3421a12010-04-08 09:43:27 +08002009/* Return which DP Port should be selected for Transcoder DP control */
2010int
2011intel_trans_dp_port_sel (struct drm_crtc *crtc)
2012{
2013 struct drm_device *dev = crtc->dev;
2014 struct drm_mode_config *mode_config = &dev->mode_config;
2015 struct drm_encoder *encoder;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002016
2017 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002018 struct intel_dp *intel_dp;
2019
Dan Carpenterd8201ab2010-05-07 10:39:00 +02002020 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08002021 continue;
2022
Chris Wilsonea5b2132010-08-04 13:50:23 +01002023 intel_dp = enc_to_intel_dp(encoder);
2024 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
2025 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002026 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01002027
Zhenyu Wange3421a12010-04-08 09:43:27 +08002028 return -1;
2029}
2030
Zhao Yakui36e83a12010-06-12 14:32:21 +08002031/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04002032bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08002033{
2034 struct drm_i915_private *dev_priv = dev->dev_private;
2035 struct child_device_config *p_child;
2036 int i;
2037
2038 if (!dev_priv->child_dev_num)
2039 return false;
2040
2041 for (i = 0; i < dev_priv->child_dev_num; i++) {
2042 p_child = dev_priv->child_dev + i;
2043
2044 if (p_child->dvo_port == PORT_IDPD &&
2045 p_child->device_type == DEVICE_TYPE_eDP)
2046 return true;
2047 }
2048 return false;
2049}
2050
Chris Wilsonf6849602010-09-19 09:29:33 +01002051static void
2052intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
2053{
Chris Wilson3f43c482011-05-12 22:17:24 +01002054 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +00002055 intel_attach_broadcast_rgb_property(connector);
Chris Wilsonf6849602010-09-19 09:29:33 +01002056}
2057
Keith Packardc8110e52009-05-06 11:51:10 -07002058void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002059intel_dp_init(struct drm_device *dev, int output_reg)
2060{
2061 struct drm_i915_private *dev_priv = dev->dev_private;
2062 struct drm_connector *connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +01002063 struct intel_dp *intel_dp;
Eric Anholt21d40d32010-03-25 11:11:14 -07002064 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002065 struct intel_connector *intel_connector;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002066 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04002067 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002068
Chris Wilsonea5b2132010-08-04 13:50:23 +01002069 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
2070 if (!intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002071 return;
2072
Chris Wilson3d3dc142011-02-12 10:33:12 +00002073 intel_dp->output_reg = output_reg;
Keith Packardd2b996a2011-07-25 22:37:51 -07002074 intel_dp->dpms_mode = -1;
Chris Wilson3d3dc142011-02-12 10:33:12 +00002075
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002076 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
2077 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002078 kfree(intel_dp);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002079 return;
2080 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01002081 intel_encoder = &intel_dp->base;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002082
Chris Wilsonea5b2132010-08-04 13:50:23 +01002083 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04002084 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01002085 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04002086
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07002087 if (output_reg == DP_A || is_pch_edp(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04002088 type = DRM_MODE_CONNECTOR_eDP;
2089 intel_encoder->type = INTEL_OUTPUT_EDP;
2090 } else {
2091 type = DRM_MODE_CONNECTOR_DisplayPort;
2092 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
2093 }
2094
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002095 connector = &intel_connector->base;
Adam Jacksonb3295302010-07-16 14:46:28 -04002096 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002097 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
2098
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002099 connector->polled = DRM_CONNECTOR_POLL_HPD;
2100
Zhao Yakui652af9d2009-12-02 10:03:33 +08002101 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07002102 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08002103 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07002104 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08002105 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07002106 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08002107
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07002108 if (is_edp(intel_dp))
Eric Anholt21d40d32010-03-25 11:11:14 -07002109 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08002110
Eric Anholt21d40d32010-03-25 11:11:14 -07002111 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002112 connector->interlace_allowed = true;
2113 connector->doublescan_allowed = 0;
2114
Chris Wilson4ef69c72010-09-09 15:14:28 +01002115 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002116 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +01002117 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002118
Chris Wilsondf0e9242010-09-09 16:20:55 +01002119 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002120 drm_sysfs_connector_add(connector);
2121
2122 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002123 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002124 case DP_A:
2125 name = "DPDDC-A";
2126 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002127 case DP_B:
2128 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002129 dev_priv->hotplug_supported_mask |=
2130 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002131 name = "DPDDC-B";
2132 break;
2133 case DP_C:
2134 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002135 dev_priv->hotplug_supported_mask |=
2136 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002137 name = "DPDDC-C";
2138 break;
2139 case DP_D:
2140 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002141 dev_priv->hotplug_supported_mask |=
2142 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002143 name = "DPDDC-D";
2144 break;
2145 }
2146
Jesse Barnes89667382010-10-07 16:01:21 -07002147 /* Cache some DPCD data in the eDP case */
2148 if (is_edp(intel_dp)) {
Keith Packard59f3e272011-07-25 20:01:56 -07002149 bool ret;
Keith Packardf01eca22011-09-28 16:48:10 -07002150 struct edp_power_seq cur, vbt;
2151 u32 pp_on, pp_off, pp_div;
Jesse Barnes89667382010-10-07 16:01:21 -07002152
Jesse Barnes5d613502011-01-24 17:10:54 -08002153 pp_on = I915_READ(PCH_PP_ON_DELAYS);
Keith Packardf01eca22011-09-28 16:48:10 -07002154 pp_off = I915_READ(PCH_PP_OFF_DELAYS);
Jesse Barnes5d613502011-01-24 17:10:54 -08002155 pp_div = I915_READ(PCH_PP_DIVISOR);
2156
Keith Packardf01eca22011-09-28 16:48:10 -07002157 /* Pull timing values out of registers */
2158 cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
2159 PANEL_POWER_UP_DELAY_SHIFT;
2160
2161 cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
2162 PANEL_LIGHT_ON_DELAY_SHIFT;
2163
2164 cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
2165 PANEL_LIGHT_OFF_DELAY_SHIFT;
2166
2167 cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
2168 PANEL_POWER_DOWN_DELAY_SHIFT;
2169
2170 cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
2171 PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
2172
2173 DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
2174 cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
2175
2176 vbt = dev_priv->edp.pps;
2177
2178 DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
2179 vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
2180
2181#define get_delay(field) ((max(cur.field, vbt.field) + 9) / 10)
2182
2183 intel_dp->panel_power_up_delay = get_delay(t1_t3);
2184 intel_dp->backlight_on_delay = get_delay(t8);
2185 intel_dp->backlight_off_delay = get_delay(t9);
2186 intel_dp->panel_power_down_delay = get_delay(t10);
2187 intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
2188
2189 DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
2190 intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
2191 intel_dp->panel_power_cycle_delay);
2192
2193 DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
2194 intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
Jesse Barnes5d613502011-01-24 17:10:54 -08002195
2196 ironlake_edp_panel_vdd_on(intel_dp);
Keith Packard59f3e272011-07-25 20:01:56 -07002197 ret = intel_dp_get_dpcd(intel_dp);
Chris Wilson3d3dc142011-02-12 10:33:12 +00002198 ironlake_edp_panel_vdd_off(intel_dp);
Keith Packard59f3e272011-07-25 20:01:56 -07002199 if (ret) {
Jesse Barnes7183dc22011-07-07 11:10:58 -07002200 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
2201 dev_priv->no_aux_handshake =
2202 intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
Jesse Barnes89667382010-10-07 16:01:21 -07002203 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
2204 } else {
Chris Wilson3d3dc142011-02-12 10:33:12 +00002205 /* if this fails, presume the device is a ghost */
Takashi Iwai48898b02011-03-18 09:06:49 +00002206 DRM_INFO("failed to retrieve link info, disabling eDP\n");
Chris Wilson3d3dc142011-02-12 10:33:12 +00002207 intel_dp_encoder_destroy(&intel_dp->base.base);
Takashi Iwai48898b02011-03-18 09:06:49 +00002208 intel_dp_destroy(&intel_connector->base);
Chris Wilson3d3dc142011-02-12 10:33:12 +00002209 return;
Jesse Barnes89667382010-10-07 16:01:21 -07002210 }
Jesse Barnes89667382010-10-07 16:01:21 -07002211 }
2212
Keith Packard552fb0b2011-09-28 16:31:53 -07002213 intel_dp_i2c_init(intel_dp, intel_connector, name);
2214
Eric Anholt21d40d32010-03-25 11:11:14 -07002215 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002216
Jesse Barnes4d926462010-10-07 16:01:07 -07002217 if (is_edp(intel_dp)) {
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02002218 dev_priv->int_edp_connector = connector;
2219 intel_panel_setup_backlight(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002220 }
2221
Chris Wilsonf6849602010-09-19 09:29:33 +01002222 intel_dp_add_properties(intel_dp, connector);
2223
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002224 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
2225 * 0xd. Failure to do so will result in spurious interrupts being
2226 * generated on the port when a cable is not attached.
2227 */
2228 if (IS_G4X(dev) && !IS_GM45(dev)) {
2229 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
2230 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
2231 }
2232}