blob: f252ed2354af2ab64fc5f102d0da69847b3cf99b [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 Packardd15456d2011-09-18 17:35:47 -070067 struct drm_display_mode *panel_fixed_mode; /* for eDP */
Keith Packarda4fc5ed2009-04-07 16:16:42 -070068};
69
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070070/**
71 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
72 * @intel_dp: DP struct
73 *
74 * If a CPU or PCH DP output is attached to an eDP panel, this function
75 * will return true, and false otherwise.
76 */
77static bool is_edp(struct intel_dp *intel_dp)
78{
79 return intel_dp->base.type == INTEL_OUTPUT_EDP;
80}
81
82/**
83 * is_pch_edp - is the port on the PCH and attached to an eDP panel?
84 * @intel_dp: DP struct
85 *
86 * Returns true if the given DP struct corresponds to a PCH DP port attached
87 * to an eDP panel, false otherwise. Helpful for determining whether we
88 * may need FDI resources for a given DP output or not.
89 */
90static bool is_pch_edp(struct intel_dp *intel_dp)
91{
92 return intel_dp->is_pch_edp;
93}
94
Chris Wilsonea5b2132010-08-04 13:50:23 +010095static struct intel_dp *enc_to_intel_dp(struct drm_encoder *encoder)
96{
Chris Wilson4ef69c72010-09-09 15:14:28 +010097 return container_of(encoder, struct intel_dp, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010098}
Keith Packarda4fc5ed2009-04-07 16:16:42 -070099
Chris Wilsondf0e9242010-09-09 16:20:55 +0100100static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
101{
102 return container_of(intel_attached_encoder(connector),
103 struct intel_dp, base);
104}
105
Jesse Barnes814948a2010-10-07 16:01:09 -0700106/**
107 * intel_encoder_is_pch_edp - is the given encoder a PCH attached eDP?
108 * @encoder: DRM encoder
109 *
110 * Return true if @encoder corresponds to a PCH attached eDP panel. Needed
111 * by intel_display.c.
112 */
113bool intel_encoder_is_pch_edp(struct drm_encoder *encoder)
114{
115 struct intel_dp *intel_dp;
116
117 if (!encoder)
118 return false;
119
120 intel_dp = enc_to_intel_dp(encoder);
121
122 return is_pch_edp(intel_dp);
123}
124
Jesse Barnes33a34e42010-09-08 12:42:02 -0700125static void intel_dp_start_link_train(struct intel_dp *intel_dp);
126static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100127static void intel_dp_link_down(struct intel_dp *intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700128
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800129void
Eric Anholt21d40d32010-03-25 11:11:14 -0700130intel_edp_link_config (struct intel_encoder *intel_encoder,
Chris Wilsonea5b2132010-08-04 13:50:23 +0100131 int *lane_num, int *link_bw)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800132{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100133 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800134
Chris Wilsonea5b2132010-08-04 13:50:23 +0100135 *lane_num = intel_dp->lane_count;
136 if (intel_dp->link_bw == DP_LINK_BW_1_62)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800137 *link_bw = 162000;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100138 else if (intel_dp->link_bw == DP_LINK_BW_2_7)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800139 *link_bw = 270000;
140}
141
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700142static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100143intel_dp_max_lane_count(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700144{
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700145 int max_lane_count = 4;
146
Jesse Barnes7183dc22011-07-07 11:10:58 -0700147 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
148 max_lane_count = intel_dp->dpcd[DP_MAX_LANE_COUNT] & 0x1f;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700149 switch (max_lane_count) {
150 case 1: case 2: case 4:
151 break;
152 default:
153 max_lane_count = 4;
154 }
155 }
156 return max_lane_count;
157}
158
159static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100160intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700161{
Jesse Barnes7183dc22011-07-07 11:10:58 -0700162 int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700163
164 switch (max_link_bw) {
165 case DP_LINK_BW_1_62:
166 case DP_LINK_BW_2_7:
167 break;
168 default:
169 max_link_bw = DP_LINK_BW_1_62;
170 break;
171 }
172 return max_link_bw;
173}
174
175static int
176intel_dp_link_clock(uint8_t link_bw)
177{
178 if (link_bw == DP_LINK_BW_2_7)
179 return 270000;
180 else
181 return 162000;
182}
183
184/* I think this is a fiction */
185static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100186intel_dp_link_required(struct drm_device *dev, struct intel_dp *intel_dp, int pixel_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700187{
Jesse Barnes89c61432011-06-24 12:19:28 -0700188 struct drm_crtc *crtc = intel_dp->base.base.crtc;
189 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
190 int bpp = 24;
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800191
Jesse Barnes89c61432011-06-24 12:19:28 -0700192 if (intel_crtc)
193 bpp = intel_crtc->bpp;
194
195 return (pixel_clock * bpp + 7) / 8;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700196}
197
198static int
Dave Airliefe27d532010-06-30 11:46:17 +1000199intel_dp_max_data_rate(int max_link_clock, int max_lanes)
200{
201 return (max_link_clock * max_lanes * 8) / 10;
202}
203
204static int
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700205intel_dp_mode_valid(struct drm_connector *connector,
206 struct drm_display_mode *mode)
207{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100208 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100209 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
210 int max_lanes = intel_dp_max_lane_count(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700211
Keith Packardd15456d2011-09-18 17:35:47 -0700212 if (is_edp(intel_dp) && intel_dp->panel_fixed_mode) {
213 if (mode->hdisplay > intel_dp->panel_fixed_mode->hdisplay)
Zhao Yakui7de56f42010-07-19 09:43:14 +0100214 return MODE_PANEL;
215
Keith Packardd15456d2011-09-18 17:35:47 -0700216 if (mode->vdisplay > intel_dp->panel_fixed_mode->vdisplay)
Zhao Yakui7de56f42010-07-19 09:43:14 +0100217 return MODE_PANEL;
218 }
219
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300220 /* only refuse the mode on non eDP since we have seen some weird eDP panels
Dave Airliefe27d532010-06-30 11:46:17 +1000221 which are outside spec tolerances but somehow work by magic */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700222 if (!is_edp(intel_dp) &&
Chris Wilsonea5b2132010-08-04 13:50:23 +0100223 (intel_dp_link_required(connector->dev, intel_dp, mode->clock)
Dave Airliefe27d532010-06-30 11:46:17 +1000224 > intel_dp_max_data_rate(max_link_clock, max_lanes)))
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700225 return MODE_CLOCK_HIGH;
226
227 if (mode->clock < 10000)
228 return MODE_CLOCK_LOW;
229
230 return MODE_OK;
231}
232
233static uint32_t
234pack_aux(uint8_t *src, int src_bytes)
235{
236 int i;
237 uint32_t v = 0;
238
239 if (src_bytes > 4)
240 src_bytes = 4;
241 for (i = 0; i < src_bytes; i++)
242 v |= ((uint32_t) src[i]) << ((3-i) * 8);
243 return v;
244}
245
246static void
247unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
248{
249 int i;
250 if (dst_bytes > 4)
251 dst_bytes = 4;
252 for (i = 0; i < dst_bytes; i++)
253 dst[i] = src >> ((3-i) * 8);
254}
255
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700256/* hrawclock is 1/4 the FSB frequency */
257static int
258intel_hrawclk(struct drm_device *dev)
259{
260 struct drm_i915_private *dev_priv = dev->dev_private;
261 uint32_t clkcfg;
262
263 clkcfg = I915_READ(CLKCFG);
264 switch (clkcfg & CLKCFG_FSB_MASK) {
265 case CLKCFG_FSB_400:
266 return 100;
267 case CLKCFG_FSB_533:
268 return 133;
269 case CLKCFG_FSB_667:
270 return 166;
271 case CLKCFG_FSB_800:
272 return 200;
273 case CLKCFG_FSB_1067:
274 return 266;
275 case CLKCFG_FSB_1333:
276 return 333;
277 /* these two are just a guess; one of them might be right */
278 case CLKCFG_FSB_1600:
279 case CLKCFG_FSB_1600_ALT:
280 return 400;
281 default:
282 return 133;
283 }
284}
285
Keith Packard9b984da2011-09-19 13:54:47 -0700286static void
287intel_dp_check_edp(struct intel_dp *intel_dp)
288{
289 struct drm_device *dev = intel_dp->base.base.dev;
290 struct drm_i915_private *dev_priv = dev->dev_private;
291 u32 pp_status, pp_control;
292 if (!is_edp(intel_dp))
293 return;
294 pp_status = I915_READ(PCH_PP_STATUS);
295 pp_control = I915_READ(PCH_PP_CONTROL);
296 if ((pp_status & PP_ON) == 0 && (pp_control & EDP_FORCE_VDD) == 0) {
297 WARN(1, "eDP powered off while attempting aux channel communication.\n");
298 DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
299 pp_status,
300 I915_READ(PCH_PP_CONTROL));
301 }
302}
303
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700304static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100305intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700306 uint8_t *send, int send_bytes,
307 uint8_t *recv, int recv_size)
308{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100309 uint32_t output_reg = intel_dp->output_reg;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100310 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700311 struct drm_i915_private *dev_priv = dev->dev_private;
312 uint32_t ch_ctl = output_reg + 0x10;
313 uint32_t ch_data = ch_ctl + 4;
314 int i;
315 int recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700316 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700317 uint32_t aux_clock_divider;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800318 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700319
Keith Packard9b984da2011-09-19 13:54:47 -0700320 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700321 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700322 * and would like to run at 2MHz. So, take the
323 * hrawclk value and divide by 2 and use that
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700324 *
325 * Note that PCH attached eDP panels should use a 125MHz input
326 * clock divider.
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700327 */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700328 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +0800329 if (IS_GEN6(dev))
330 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
331 else
332 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
333 } else if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500334 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800335 else
336 aux_clock_divider = intel_hrawclk(dev) / 2;
337
Zhenyu Wange3421a12010-04-08 09:43:27 +0800338 if (IS_GEN6(dev))
339 precharge = 3;
340 else
341 precharge = 5;
342
Jesse Barnes11bee432011-08-01 15:02:20 -0700343 /* Try to wait for any previous AUX channel activity */
344 for (try = 0; try < 3; try++) {
345 status = I915_READ(ch_ctl);
346 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
347 break;
348 msleep(1);
349 }
350
351 if (try == 3) {
352 WARN(1, "dp_aux_ch not started status 0x%08x\n",
353 I915_READ(ch_ctl));
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100354 return -EBUSY;
355 }
356
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700357 /* Must try at least 3 times according to DP spec */
358 for (try = 0; try < 5; try++) {
359 /* Load the send data into the aux channel data registers */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100360 for (i = 0; i < send_bytes; i += 4)
361 I915_WRITE(ch_data + i,
362 pack_aux(send + i, send_bytes - i));
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700363
364 /* Send the command and wait for it to complete */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100365 I915_WRITE(ch_ctl,
366 DP_AUX_CH_CTL_SEND_BUSY |
367 DP_AUX_CH_CTL_TIME_OUT_400us |
368 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
369 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
370 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
371 DP_AUX_CH_CTL_DONE |
372 DP_AUX_CH_CTL_TIME_OUT_ERROR |
373 DP_AUX_CH_CTL_RECEIVE_ERROR);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700374 for (;;) {
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700375 status = I915_READ(ch_ctl);
376 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
377 break;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100378 udelay(100);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700379 }
380
381 /* Clear done status and any errors */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100382 I915_WRITE(ch_ctl,
383 status |
384 DP_AUX_CH_CTL_DONE |
385 DP_AUX_CH_CTL_TIME_OUT_ERROR |
386 DP_AUX_CH_CTL_RECEIVE_ERROR);
387 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700388 break;
389 }
390
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700391 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700392 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700393 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700394 }
395
396 /* Check for timeout or receive error.
397 * Timeouts occur when the sink is not connected
398 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700399 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700400 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700401 return -EIO;
402 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700403
404 /* Timeouts occur when the device isn't connected, so they're
405 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700406 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800407 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700408 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700409 }
410
411 /* Unload any bytes sent back from the other side */
412 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
413 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700414 if (recv_bytes > recv_size)
415 recv_bytes = recv_size;
416
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100417 for (i = 0; i < recv_bytes; i += 4)
418 unpack_aux(I915_READ(ch_data + i),
419 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700420
421 return recv_bytes;
422}
423
424/* Write data to the aux channel in native mode */
425static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100426intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700427 uint16_t address, uint8_t *send, int send_bytes)
428{
429 int ret;
430 uint8_t msg[20];
431 int msg_bytes;
432 uint8_t ack;
433
Keith Packard9b984da2011-09-19 13:54:47 -0700434 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700435 if (send_bytes > 16)
436 return -1;
437 msg[0] = AUX_NATIVE_WRITE << 4;
438 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800439 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700440 msg[3] = send_bytes - 1;
441 memcpy(&msg[4], send, send_bytes);
442 msg_bytes = send_bytes + 4;
443 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100444 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700445 if (ret < 0)
446 return ret;
447 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
448 break;
449 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
450 udelay(100);
451 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700452 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700453 }
454 return send_bytes;
455}
456
457/* Write a single byte to the aux channel in native mode */
458static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100459intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700460 uint16_t address, uint8_t byte)
461{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100462 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700463}
464
465/* read bytes from a native aux channel */
466static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100467intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700468 uint16_t address, uint8_t *recv, int recv_bytes)
469{
470 uint8_t msg[4];
471 int msg_bytes;
472 uint8_t reply[20];
473 int reply_bytes;
474 uint8_t ack;
475 int ret;
476
Keith Packard9b984da2011-09-19 13:54:47 -0700477 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700478 msg[0] = AUX_NATIVE_READ << 4;
479 msg[1] = address >> 8;
480 msg[2] = address & 0xff;
481 msg[3] = recv_bytes - 1;
482
483 msg_bytes = 4;
484 reply_bytes = recv_bytes + 1;
485
486 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100487 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700488 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700489 if (ret == 0)
490 return -EPROTO;
491 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700492 return ret;
493 ack = reply[0];
494 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
495 memcpy(recv, reply + 1, ret - 1);
496 return ret - 1;
497 }
498 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
499 udelay(100);
500 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700501 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700502 }
503}
504
505static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000506intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
507 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700508{
Dave Airlieab2c0672009-12-04 10:55:24 +1000509 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100510 struct intel_dp *intel_dp = container_of(adapter,
511 struct intel_dp,
512 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000513 uint16_t address = algo_data->address;
514 uint8_t msg[5];
515 uint8_t reply[2];
David Flynn8316f332010-12-08 16:10:21 +0000516 unsigned retry;
Dave Airlieab2c0672009-12-04 10:55:24 +1000517 int msg_bytes;
518 int reply_bytes;
519 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700520
Keith Packard9b984da2011-09-19 13:54:47 -0700521 intel_dp_check_edp(intel_dp);
Dave Airlieab2c0672009-12-04 10:55:24 +1000522 /* Set up the command byte */
523 if (mode & MODE_I2C_READ)
524 msg[0] = AUX_I2C_READ << 4;
525 else
526 msg[0] = AUX_I2C_WRITE << 4;
527
528 if (!(mode & MODE_I2C_STOP))
529 msg[0] |= AUX_I2C_MOT << 4;
530
531 msg[1] = address >> 8;
532 msg[2] = address;
533
534 switch (mode) {
535 case MODE_I2C_WRITE:
536 msg[3] = 0;
537 msg[4] = write_byte;
538 msg_bytes = 5;
539 reply_bytes = 1;
540 break;
541 case MODE_I2C_READ:
542 msg[3] = 0;
543 msg_bytes = 4;
544 reply_bytes = 2;
545 break;
546 default:
547 msg_bytes = 3;
548 reply_bytes = 1;
549 break;
550 }
551
David Flynn8316f332010-12-08 16:10:21 +0000552 for (retry = 0; retry < 5; retry++) {
553 ret = intel_dp_aux_ch(intel_dp,
554 msg, msg_bytes,
555 reply, reply_bytes);
Dave Airlieab2c0672009-12-04 10:55:24 +1000556 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000557 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000558 return ret;
559 }
David Flynn8316f332010-12-08 16:10:21 +0000560
561 switch (reply[0] & AUX_NATIVE_REPLY_MASK) {
562 case AUX_NATIVE_REPLY_ACK:
563 /* I2C-over-AUX Reply field is only valid
564 * when paired with AUX ACK.
565 */
566 break;
567 case AUX_NATIVE_REPLY_NACK:
568 DRM_DEBUG_KMS("aux_ch native nack\n");
569 return -EREMOTEIO;
570 case AUX_NATIVE_REPLY_DEFER:
571 udelay(100);
572 continue;
573 default:
574 DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
575 reply[0]);
576 return -EREMOTEIO;
577 }
578
Dave Airlieab2c0672009-12-04 10:55:24 +1000579 switch (reply[0] & AUX_I2C_REPLY_MASK) {
580 case AUX_I2C_REPLY_ACK:
581 if (mode == MODE_I2C_READ) {
582 *read_byte = reply[1];
583 }
584 return reply_bytes - 1;
585 case AUX_I2C_REPLY_NACK:
David Flynn8316f332010-12-08 16:10:21 +0000586 DRM_DEBUG_KMS("aux_i2c nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000587 return -EREMOTEIO;
588 case AUX_I2C_REPLY_DEFER:
David Flynn8316f332010-12-08 16:10:21 +0000589 DRM_DEBUG_KMS("aux_i2c defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000590 udelay(100);
591 break;
592 default:
David Flynn8316f332010-12-08 16:10:21 +0000593 DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
Dave Airlieab2c0672009-12-04 10:55:24 +1000594 return -EREMOTEIO;
595 }
596 }
David Flynn8316f332010-12-08 16:10:21 +0000597
598 DRM_ERROR("too many retries, giving up\n");
599 return -EREMOTEIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700600}
601
Keith Packard0b5c5412011-09-28 16:41:05 -0700602static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp);
603static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp);
604
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700605static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100606intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800607 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700608{
Keith Packard0b5c5412011-09-28 16:41:05 -0700609 int ret;
610
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800611 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100612 intel_dp->algo.running = false;
613 intel_dp->algo.address = 0;
614 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700615
Chris Wilsonea5b2132010-08-04 13:50:23 +0100616 memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
617 intel_dp->adapter.owner = THIS_MODULE;
618 intel_dp->adapter.class = I2C_CLASS_DDC;
619 strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
620 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
621 intel_dp->adapter.algo_data = &intel_dp->algo;
622 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
623
Keith Packard0b5c5412011-09-28 16:41:05 -0700624 ironlake_edp_panel_vdd_on(intel_dp);
625 ret = i2c_dp_aux_add_bus(&intel_dp->adapter);
626 ironlake_edp_panel_vdd_off(intel_dp);
627 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700628}
629
630static bool
631intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
632 struct drm_display_mode *adjusted_mode)
633{
Zhao Yakui0d3a1bee2010-07-19 09:43:13 +0100634 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100635 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700636 int lane_count, clock;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100637 int max_lane_count = intel_dp_max_lane_count(intel_dp);
638 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700639 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
640
Keith Packardd15456d2011-09-18 17:35:47 -0700641 if (is_edp(intel_dp) && intel_dp->panel_fixed_mode) {
642 intel_fixed_panel_mode(intel_dp->panel_fixed_mode, adjusted_mode);
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100643 intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
644 mode, adjusted_mode);
Zhao Yakui0d3a1bee2010-07-19 09:43:13 +0100645 /*
646 * the mode->clock is used to calculate the Data&Link M/N
647 * of the pipe. For the eDP the fixed clock should be used.
648 */
Keith Packardd15456d2011-09-18 17:35:47 -0700649 mode->clock = intel_dp->panel_fixed_mode->clock;
Zhao Yakui0d3a1bee2010-07-19 09:43:13 +0100650 }
651
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700652 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
653 for (clock = 0; clock <= max_clock; clock++) {
Dave Airliefe27d532010-06-30 11:46:17 +1000654 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700655
Chris Wilsonea5b2132010-08-04 13:50:23 +0100656 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800657 <= link_avail) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100658 intel_dp->link_bw = bws[clock];
659 intel_dp->lane_count = lane_count;
660 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Zhao Yakui28c97732009-10-09 11:39:41 +0800661 DRM_DEBUG_KMS("Display port link bw %02x lane "
662 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100663 intel_dp->link_bw, intel_dp->lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700664 adjusted_mode->clock);
665 return true;
666 }
667 }
668 }
Dave Airliefe27d532010-06-30 11:46:17 +1000669
Chris Wilson3cf2efb2010-11-29 10:09:55 +0000670 if (is_edp(intel_dp)) {
671 /* okay we failed just pick the highest */
672 intel_dp->lane_count = max_lane_count;
673 intel_dp->link_bw = bws[max_clock];
674 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
675 DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
676 "count %d clock %d\n",
677 intel_dp->link_bw, intel_dp->lane_count,
678 adjusted_mode->clock);
679
680 return true;
681 }
682
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700683 return false;
684}
685
686struct intel_dp_m_n {
687 uint32_t tu;
688 uint32_t gmch_m;
689 uint32_t gmch_n;
690 uint32_t link_m;
691 uint32_t link_n;
692};
693
694static void
695intel_reduce_ratio(uint32_t *num, uint32_t *den)
696{
697 while (*num > 0xffffff || *den > 0xffffff) {
698 *num >>= 1;
699 *den >>= 1;
700 }
701}
702
703static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800704intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700705 int nlanes,
706 int pixel_clock,
707 int link_clock,
708 struct intel_dp_m_n *m_n)
709{
710 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800711 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700712 m_n->gmch_n = link_clock * nlanes;
713 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
714 m_n->link_m = pixel_clock;
715 m_n->link_n = link_clock;
716 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
717}
718
719void
720intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
721 struct drm_display_mode *adjusted_mode)
722{
723 struct drm_device *dev = crtc->dev;
724 struct drm_mode_config *mode_config = &dev->mode_config;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800725 struct drm_encoder *encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700726 struct drm_i915_private *dev_priv = dev->dev_private;
727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes858fa0352011-06-24 12:19:24 -0700728 int lane_count = 4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700729 struct intel_dp_m_n m_n;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800730 int pipe = intel_crtc->pipe;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700731
732 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700733 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700734 */
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800735 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100736 struct intel_dp *intel_dp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700737
Dan Carpenterd8201ab2010-05-07 10:39:00 +0200738 if (encoder->crtc != crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700739 continue;
740
Chris Wilsonea5b2132010-08-04 13:50:23 +0100741 intel_dp = enc_to_intel_dp(encoder);
742 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
743 lane_count = intel_dp->lane_count;
Jesse Barnes51190662010-10-07 16:01:08 -0700744 break;
745 } else if (is_edp(intel_dp)) {
746 lane_count = dev_priv->edp.lanes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700747 break;
748 }
749 }
750
751 /*
752 * Compute the GMCH and Link ratios. The '3' here is
753 * the number of bytes_per_pixel post-LUT, which we always
754 * set up for 8-bits of R/G/B, or 3 bytes total.
755 */
Jesse Barnes858fa0352011-06-24 12:19:24 -0700756 intel_dp_compute_m_n(intel_crtc->bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700757 mode->clock, adjusted_mode->clock, &m_n);
758
Eric Anholtc619eed2010-01-28 16:45:52 -0800759 if (HAS_PCH_SPLIT(dev)) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800760 I915_WRITE(TRANSDATA_M1(pipe),
761 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
762 m_n.gmch_m);
763 I915_WRITE(TRANSDATA_N1(pipe), m_n.gmch_n);
764 I915_WRITE(TRANSDPLINK_M1(pipe), m_n.link_m);
765 I915_WRITE(TRANSDPLINK_N1(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700766 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800767 I915_WRITE(PIPE_GMCH_DATA_M(pipe),
768 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
769 m_n.gmch_m);
770 I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n.gmch_n);
771 I915_WRITE(PIPE_DP_LINK_M(pipe), m_n.link_m);
772 I915_WRITE(PIPE_DP_LINK_N(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700773 }
774}
775
Keith Packardf01eca22011-09-28 16:48:10 -0700776static void ironlake_edp_pll_on(struct drm_encoder *encoder);
777static void ironlake_edp_pll_off(struct drm_encoder *encoder);
778
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700779static void
780intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
781 struct drm_display_mode *adjusted_mode)
782{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800783 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100784 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +0100785 struct drm_crtc *crtc = intel_dp->base.base.crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
787
Keith Packardf01eca22011-09-28 16:48:10 -0700788 /* Turn on the eDP PLL if needed */
789 if (is_edp(intel_dp)) {
790 if (!is_pch_edp(intel_dp))
791 ironlake_edp_pll_on(encoder);
792 else
793 ironlake_edp_pll_off(encoder);
794 }
795
Chris Wilsone953fd72011-02-21 22:23:52 +0000796 intel_dp->DP = DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
797 intel_dp->DP |= intel_dp->color_range;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400798
799 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100800 intel_dp->DP |= DP_SYNC_HS_HIGH;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400801 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100802 intel_dp->DP |= DP_SYNC_VS_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700803
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700804 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100805 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800806 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100807 intel_dp->DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700808
Chris Wilsonea5b2132010-08-04 13:50:23 +0100809 switch (intel_dp->lane_count) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700810 case 1:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100811 intel_dp->DP |= DP_PORT_WIDTH_1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700812 break;
813 case 2:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100814 intel_dp->DP |= DP_PORT_WIDTH_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700815 break;
816 case 4:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100817 intel_dp->DP |= DP_PORT_WIDTH_4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700818 break;
819 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100820 if (intel_dp->has_audio)
821 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700822
Chris Wilsonea5b2132010-08-04 13:50:23 +0100823 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
824 intel_dp->link_configuration[0] = intel_dp->link_bw;
825 intel_dp->link_configuration[1] = intel_dp->lane_count;
Adam Jacksona2cab1b2011-07-12 17:38:05 -0400826 intel_dp->link_configuration[8] = DP_SET_ANSI_8B10B;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700827
828 /*
Adam Jackson9962c922010-05-13 14:45:42 -0400829 * Check for DPCD version > 1.1 and enhanced framing support
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700830 */
Jesse Barnes7183dc22011-07-07 11:10:58 -0700831 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
832 (intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100833 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
834 intel_dp->DP |= DP_ENHANCED_FRAMING;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700835 }
836
Zhenyu Wange3421a12010-04-08 09:43:27 +0800837 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
838 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100839 intel_dp->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800840
Jesse Barnes895692b2010-10-07 16:01:23 -0700841 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800842 /* don't miss out required setting for eDP */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100843 intel_dp->DP |= DP_PLL_ENABLE;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800844 if (adjusted_mode->clock < 200000)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100845 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800846 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100847 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800848 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700849}
850
Jesse Barnes5d613502011-01-24 17:10:54 -0800851static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp)
852{
853 struct drm_device *dev = intel_dp->base.base.dev;
854 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packardf01eca22011-09-28 16:48:10 -0700855 u32 pp, pp_status;
Jesse Barnes5d613502011-01-24 17:10:54 -0800856
Keith Packard97af61f572011-09-28 16:23:51 -0700857 if (!is_edp(intel_dp))
858 return;
Keith Packardf01eca22011-09-28 16:48:10 -0700859 DRM_DEBUG_KMS("Turn eDP VDD on\n");
Jesse Barnes5d613502011-01-24 17:10:54 -0800860 /*
861 * If the panel wasn't on, make sure there's not a currently
862 * active PP sequence before enabling AUX VDD.
863 */
Keith Packardf01eca22011-09-28 16:48:10 -0700864 pp_status = I915_READ(PCH_PP_STATUS);
Jesse Barnes5d613502011-01-24 17:10:54 -0800865
866 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700867 pp &= ~PANEL_UNLOCK_MASK;
868 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes5d613502011-01-24 17:10:54 -0800869 pp |= EDP_FORCE_VDD;
870 I915_WRITE(PCH_PP_CONTROL, pp);
871 POSTING_READ(PCH_PP_CONTROL);
Keith Packardf01eca22011-09-28 16:48:10 -0700872 DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
873 I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
874 if (!(pp_status & PP_ON)) {
875 msleep(intel_dp->panel_power_up_delay);
876 DRM_DEBUG_KMS("eDP VDD was not on\n");
877 }
Jesse Barnes5d613502011-01-24 17:10:54 -0800878}
879
880static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp)
881{
882 struct drm_device *dev = intel_dp->base.base.dev;
883 struct drm_i915_private *dev_priv = dev->dev_private;
884 u32 pp;
885
Keith Packard97af61f572011-09-28 16:23:51 -0700886 if (!is_edp(intel_dp))
887 return;
Keith Packardf01eca22011-09-28 16:48:10 -0700888 DRM_DEBUG_KMS("Turn eDP VDD off\n");
Jesse Barnes5d613502011-01-24 17:10:54 -0800889 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700890 pp &= ~PANEL_UNLOCK_MASK;
891 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes5d613502011-01-24 17:10:54 -0800892 pp &= ~EDP_FORCE_VDD;
893 I915_WRITE(PCH_PP_CONTROL, pp);
894 POSTING_READ(PCH_PP_CONTROL);
895
896 /* Make sure sequencer is idle before allowing subsequent activity */
Keith Packardf01eca22011-09-28 16:48:10 -0700897 DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
898 I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
899 msleep(intel_dp->panel_power_cycle_delay);
Jesse Barnes5d613502011-01-24 17:10:54 -0800900}
901
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700902/* Returns true if the panel was already on when called */
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700903static bool ironlake_edp_panel_on (struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -0700904{
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700905 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes9934c132010-07-22 13:18:19 -0700906 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700907 u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_STATE_ON_IDLE;
Jesse Barnes9934c132010-07-22 13:18:19 -0700908
Keith Packard97af61f572011-09-28 16:23:51 -0700909 if (!is_edp(intel_dp))
Keith Packardf01eca22011-09-28 16:48:10 -0700910 return true;
Chris Wilson913d8d12010-08-07 11:01:35 +0100911 if (I915_READ(PCH_PP_STATUS) & PP_ON)
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700912 return true;
Jesse Barnes9934c132010-07-22 13:18:19 -0700913
914 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700915 pp &= ~PANEL_UNLOCK_MASK;
916 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700917
918 /* ILK workaround: disable reset around power sequence */
919 pp &= ~PANEL_POWER_RESET;
920 I915_WRITE(PCH_PP_CONTROL, pp);
921 POSTING_READ(PCH_PP_CONTROL);
922
Keith Packard1c0ae802011-09-19 13:59:29 -0700923 pp |= POWER_TARGET_ON;
Jesse Barnes9934c132010-07-22 13:18:19 -0700924 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700925 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700926
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700927 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_on_mask) == idle_on_mask,
928 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100929 DRM_ERROR("panel on wait timed out: 0x%08x\n",
930 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700931
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700932 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700933 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700934 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700935
936 return false;
Jesse Barnes9934c132010-07-22 13:18:19 -0700937}
938
Keith Packardf01eca22011-09-28 16:48:10 -0700939static void ironlake_edp_panel_off(struct drm_encoder *encoder)
Jesse Barnes9934c132010-07-22 13:18:19 -0700940{
Keith Packardf01eca22011-09-28 16:48:10 -0700941 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
942 struct drm_device *dev = encoder->dev;
Jesse Barnes9934c132010-07-22 13:18:19 -0700943 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700944 u32 pp, idle_off_mask = PP_ON | PP_SEQUENCE_MASK |
945 PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK;
Jesse Barnes9934c132010-07-22 13:18:19 -0700946
Keith Packard97af61f572011-09-28 16:23:51 -0700947 if (!is_edp(intel_dp))
948 return;
Jesse Barnes9934c132010-07-22 13:18:19 -0700949 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700950 pp &= ~PANEL_UNLOCK_MASK;
951 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700952
953 /* ILK workaround: disable reset around power sequence */
954 pp &= ~PANEL_POWER_RESET;
955 I915_WRITE(PCH_PP_CONTROL, pp);
956 POSTING_READ(PCH_PP_CONTROL);
957
Jesse Barnes9934c132010-07-22 13:18:19 -0700958 pp &= ~POWER_TARGET_ON;
959 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700960 POSTING_READ(PCH_PP_CONTROL);
Keith Packardf01eca22011-09-28 16:48:10 -0700961 msleep(intel_dp->panel_power_cycle_delay);
Jesse Barnes9934c132010-07-22 13:18:19 -0700962
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700963 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_off_mask) == 0, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100964 DRM_ERROR("panel off wait timed out: 0x%08x\n",
965 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700966
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700967 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700968 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700969 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700970}
971
Keith Packardf01eca22011-09-28 16:48:10 -0700972static void ironlake_edp_backlight_on (struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800973{
Keith Packardf01eca22011-09-28 16:48:10 -0700974 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800975 struct drm_i915_private *dev_priv = dev->dev_private;
976 u32 pp;
977
Keith Packardf01eca22011-09-28 16:48:10 -0700978 if (!is_edp(intel_dp))
979 return;
980
Zhao Yakui28c97732009-10-09 11:39:41 +0800981 DRM_DEBUG_KMS("\n");
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700982 /*
983 * If we enable the backlight right away following a panel power
984 * on, we may see slight flicker as the panel syncs with the eDP
985 * link. So delay a bit to make sure the image is solid before
986 * allowing it to appear.
987 */
Keith Packardf01eca22011-09-28 16:48:10 -0700988 msleep(intel_dp->backlight_on_delay);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800989 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700990 pp &= ~PANEL_UNLOCK_MASK;
991 pp |= PANEL_UNLOCK_REGS;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800992 pp |= EDP_BLC_ENABLE;
993 I915_WRITE(PCH_PP_CONTROL, pp);
Keith Packardf01eca22011-09-28 16:48:10 -0700994 POSTING_READ(PCH_PP_CONTROL);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800995}
996
Keith Packardf01eca22011-09-28 16:48:10 -0700997static void ironlake_edp_backlight_off (struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800998{
Keith Packardf01eca22011-09-28 16:48:10 -0700999 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001000 struct drm_i915_private *dev_priv = dev->dev_private;
1001 u32 pp;
1002
Keith Packardf01eca22011-09-28 16:48:10 -07001003 if (!is_edp(intel_dp))
1004 return;
1005
Zhao Yakui28c97732009-10-09 11:39:41 +08001006 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001007 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -07001008 pp &= ~PANEL_UNLOCK_MASK;
1009 pp |= PANEL_UNLOCK_REGS;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001010 pp &= ~EDP_BLC_ENABLE;
1011 I915_WRITE(PCH_PP_CONTROL, pp);
Keith Packardf01eca22011-09-28 16:48:10 -07001012 POSTING_READ(PCH_PP_CONTROL);
1013 msleep(intel_dp->backlight_off_delay);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001014}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001015
Jesse Barnesd240f202010-08-13 15:43:26 -07001016static void ironlake_edp_pll_on(struct drm_encoder *encoder)
1017{
1018 struct drm_device *dev = encoder->dev;
1019 struct drm_i915_private *dev_priv = dev->dev_private;
1020 u32 dpa_ctl;
1021
1022 DRM_DEBUG_KMS("\n");
1023 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -07001024 dpa_ctl |= DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -07001025 I915_WRITE(DP_A, dpa_ctl);
Jesse Barnes298b0b32010-10-07 16:01:24 -07001026 POSTING_READ(DP_A);
1027 udelay(200);
Jesse Barnesd240f202010-08-13 15:43:26 -07001028}
1029
1030static void ironlake_edp_pll_off(struct drm_encoder *encoder)
1031{
1032 struct drm_device *dev = encoder->dev;
1033 struct drm_i915_private *dev_priv = dev->dev_private;
1034 u32 dpa_ctl;
1035
1036 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -07001037 dpa_ctl &= ~DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -07001038 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +01001039 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -07001040 udelay(200);
1041}
1042
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001043/* If the sink supports it, try to set the power state appropriately */
1044static void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
1045{
1046 int ret, i;
1047
1048 /* Should have a valid DPCD by this point */
1049 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
1050 return;
1051
1052 if (mode != DRM_MODE_DPMS_ON) {
1053 ret = intel_dp_aux_native_write_1(intel_dp, DP_SET_POWER,
1054 DP_SET_POWER_D3);
1055 if (ret != 1)
1056 DRM_DEBUG_DRIVER("failed to write sink power state\n");
1057 } else {
1058 /*
1059 * When turning on, we need to retry for 1ms to give the sink
1060 * time to wake up.
1061 */
1062 for (i = 0; i < 3; i++) {
1063 ret = intel_dp_aux_native_write_1(intel_dp,
1064 DP_SET_POWER,
1065 DP_SET_POWER_D0);
1066 if (ret == 1)
1067 break;
1068 msleep(1);
1069 }
1070 }
1071}
1072
Jesse Barnesd240f202010-08-13 15:43:26 -07001073static void intel_dp_prepare(struct drm_encoder *encoder)
1074{
1075 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -07001076
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001077 /* Wake up the sink first */
Keith Packardf58ff852011-09-28 16:44:14 -07001078 ironlake_edp_panel_vdd_on(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001079 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
Keith Packardf58ff852011-09-28 16:44:14 -07001080 ironlake_edp_panel_vdd_off(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001081
Keith Packardf01eca22011-09-28 16:48:10 -07001082 /* Make sure the panel is off before trying to
1083 * change the mode
1084 */
1085 ironlake_edp_backlight_off(intel_dp);
Jesse Barnes736085b2010-10-08 10:35:55 -07001086 intel_dp_link_down(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001087 ironlake_edp_panel_off(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -07001088}
1089
1090static void intel_dp_commit(struct drm_encoder *encoder)
1091{
1092 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -07001093
Keith Packard97af61f572011-09-28 16:23:51 -07001094 ironlake_edp_panel_vdd_on(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001095 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001096 intel_dp_start_link_train(intel_dp);
Keith Packard97af61f572011-09-28 16:23:51 -07001097 ironlake_edp_panel_on(intel_dp);
1098 ironlake_edp_panel_vdd_off(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001099 intel_dp_complete_link_train(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001100 ironlake_edp_backlight_on(intel_dp);
Keith Packardd2b996a2011-07-25 22:37:51 -07001101
1102 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
Jesse Barnesd240f202010-08-13 15:43:26 -07001103}
1104
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001105static void
1106intel_dp_dpms(struct drm_encoder *encoder, int mode)
1107{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001108 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001109 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001110 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001111 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001112
1113 if (mode != DRM_MODE_DPMS_ON) {
Keith Packard245e2702011-10-05 19:53:09 -07001114 ironlake_edp_panel_vdd_on(intel_dp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001115 if (is_edp(intel_dp))
Keith Packardf01eca22011-09-28 16:48:10 -07001116 ironlake_edp_backlight_off(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001117 intel_dp_sink_dpms(intel_dp, mode);
Jesse Barnes736085b2010-10-08 10:35:55 -07001118 intel_dp_link_down(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001119 ironlake_edp_panel_off(encoder);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001120 if (is_edp(intel_dp) && !is_pch_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -07001121 ironlake_edp_pll_off(encoder);
Keith Packard245e2702011-10-05 19:53:09 -07001122 ironlake_edp_panel_vdd_off(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001123 } else {
Keith Packard97af61f572011-09-28 16:23:51 -07001124 ironlake_edp_panel_vdd_on(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001125 intel_dp_sink_dpms(intel_dp, mode);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001126 if (!(dp_reg & DP_PORT_EN)) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001127 intel_dp_start_link_train(intel_dp);
Keith Packard97af61f572011-09-28 16:23:51 -07001128 ironlake_edp_panel_on(intel_dp);
1129 ironlake_edp_panel_vdd_off(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001130 intel_dp_complete_link_train(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001131 ironlake_edp_backlight_on(intel_dp);
Keith Packardbee7eb22011-09-28 16:28:00 -07001132 } else
1133 ironlake_edp_panel_vdd_off(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001134 }
Keith Packardd2b996a2011-07-25 22:37:51 -07001135 intel_dp->dpms_mode = mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001136}
1137
1138/*
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001139 * Native read with retry for link status and receiver capability reads for
1140 * cases where the sink may still be asleep.
1141 */
1142static bool
1143intel_dp_aux_native_read_retry(struct intel_dp *intel_dp, uint16_t address,
1144 uint8_t *recv, int recv_bytes)
1145{
1146 int ret, i;
1147
1148 /*
1149 * Sinks are *supposed* to come up within 1ms from an off state,
1150 * but we're also supposed to retry 3 times per the spec.
1151 */
1152 for (i = 0; i < 3; i++) {
1153 ret = intel_dp_aux_native_read(intel_dp, address, recv,
1154 recv_bytes);
1155 if (ret == recv_bytes)
1156 return true;
1157 msleep(1);
1158 }
1159
1160 return false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001161}
1162
1163/*
1164 * Fetch AUX CH registers 0x202 - 0x207 which contain
1165 * link status information
1166 */
1167static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001168intel_dp_get_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001169{
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001170 return intel_dp_aux_native_read_retry(intel_dp,
1171 DP_LANE0_1_STATUS,
1172 intel_dp->link_status,
1173 DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001174}
1175
1176static uint8_t
1177intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1178 int r)
1179{
1180 return link_status[r - DP_LANE0_1_STATUS];
1181}
1182
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001183static uint8_t
1184intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
1185 int lane)
1186{
1187 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1188 int s = ((lane & 1) ?
1189 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1190 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1191 uint8_t l = intel_dp_link_status(link_status, i);
1192
1193 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1194}
1195
1196static uint8_t
1197intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1198 int lane)
1199{
1200 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1201 int s = ((lane & 1) ?
1202 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1203 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1204 uint8_t l = intel_dp_link_status(link_status, i);
1205
1206 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1207}
1208
1209
1210#if 0
1211static char *voltage_names[] = {
1212 "0.4V", "0.6V", "0.8V", "1.2V"
1213};
1214static char *pre_emph_names[] = {
1215 "0dB", "3.5dB", "6dB", "9.5dB"
1216};
1217static char *link_train_names[] = {
1218 "pattern 1", "pattern 2", "idle", "off"
1219};
1220#endif
1221
1222/*
1223 * These are source-specific values; current Intel hardware supports
1224 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1225 */
1226#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1227
1228static uint8_t
1229intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1230{
1231 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1232 case DP_TRAIN_VOLTAGE_SWING_400:
1233 return DP_TRAIN_PRE_EMPHASIS_6;
1234 case DP_TRAIN_VOLTAGE_SWING_600:
1235 return DP_TRAIN_PRE_EMPHASIS_6;
1236 case DP_TRAIN_VOLTAGE_SWING_800:
1237 return DP_TRAIN_PRE_EMPHASIS_3_5;
1238 case DP_TRAIN_VOLTAGE_SWING_1200:
1239 default:
1240 return DP_TRAIN_PRE_EMPHASIS_0;
1241 }
1242}
1243
1244static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001245intel_get_adjust_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001246{
1247 uint8_t v = 0;
1248 uint8_t p = 0;
1249 int lane;
1250
Jesse Barnes33a34e42010-09-08 12:42:02 -07001251 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1252 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1253 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001254
1255 if (this_v > v)
1256 v = this_v;
1257 if (this_p > p)
1258 p = this_p;
1259 }
1260
1261 if (v >= I830_DP_VOLTAGE_MAX)
1262 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1263
1264 if (p >= intel_dp_pre_emphasis_max(v))
1265 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1266
1267 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001268 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001269}
1270
1271static uint32_t
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001272intel_dp_signal_levels(uint8_t train_set, int lane_count)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001273{
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001274 uint32_t signal_levels = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001275
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001276 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001277 case DP_TRAIN_VOLTAGE_SWING_400:
1278 default:
1279 signal_levels |= DP_VOLTAGE_0_4;
1280 break;
1281 case DP_TRAIN_VOLTAGE_SWING_600:
1282 signal_levels |= DP_VOLTAGE_0_6;
1283 break;
1284 case DP_TRAIN_VOLTAGE_SWING_800:
1285 signal_levels |= DP_VOLTAGE_0_8;
1286 break;
1287 case DP_TRAIN_VOLTAGE_SWING_1200:
1288 signal_levels |= DP_VOLTAGE_1_2;
1289 break;
1290 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001291 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001292 case DP_TRAIN_PRE_EMPHASIS_0:
1293 default:
1294 signal_levels |= DP_PRE_EMPHASIS_0;
1295 break;
1296 case DP_TRAIN_PRE_EMPHASIS_3_5:
1297 signal_levels |= DP_PRE_EMPHASIS_3_5;
1298 break;
1299 case DP_TRAIN_PRE_EMPHASIS_6:
1300 signal_levels |= DP_PRE_EMPHASIS_6;
1301 break;
1302 case DP_TRAIN_PRE_EMPHASIS_9_5:
1303 signal_levels |= DP_PRE_EMPHASIS_9_5;
1304 break;
1305 }
1306 return signal_levels;
1307}
1308
Zhenyu Wange3421a12010-04-08 09:43:27 +08001309/* Gen6's DP voltage swing and pre-emphasis control */
1310static uint32_t
1311intel_gen6_edp_signal_levels(uint8_t train_set)
1312{
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001313 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1314 DP_TRAIN_PRE_EMPHASIS_MASK);
1315 switch (signal_levels) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001316 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001317 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
1318 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
1319 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
1320 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001321 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001322 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
1323 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001324 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001325 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
1326 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001327 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001328 case DP_TRAIN_VOLTAGE_SWING_1200 | DP_TRAIN_PRE_EMPHASIS_0:
1329 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001330 default:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001331 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
1332 "0x%x\n", signal_levels);
1333 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001334 }
1335}
1336
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001337static uint8_t
1338intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1339 int lane)
1340{
1341 int i = DP_LANE0_1_STATUS + (lane >> 1);
1342 int s = (lane & 1) * 4;
1343 uint8_t l = intel_dp_link_status(link_status, i);
1344
1345 return (l >> s) & 0xf;
1346}
1347
1348/* Check for clock recovery is done on all channels */
1349static bool
1350intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1351{
1352 int lane;
1353 uint8_t lane_status;
1354
1355 for (lane = 0; lane < lane_count; lane++) {
1356 lane_status = intel_get_lane_status(link_status, lane);
1357 if ((lane_status & DP_LANE_CR_DONE) == 0)
1358 return false;
1359 }
1360 return true;
1361}
1362
1363/* Check to see if channel eq is done on all channels */
1364#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1365 DP_LANE_CHANNEL_EQ_DONE|\
1366 DP_LANE_SYMBOL_LOCKED)
1367static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001368intel_channel_eq_ok(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001369{
1370 uint8_t lane_align;
1371 uint8_t lane_status;
1372 int lane;
1373
Jesse Barnes33a34e42010-09-08 12:42:02 -07001374 lane_align = intel_dp_link_status(intel_dp->link_status,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001375 DP_LANE_ALIGN_STATUS_UPDATED);
1376 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1377 return false;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001378 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1379 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001380 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1381 return false;
1382 }
1383 return true;
1384}
1385
1386static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001387intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001388 uint32_t dp_reg_value,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001389 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001390{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001391 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001392 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001393 int ret;
1394
Chris Wilsonea5b2132010-08-04 13:50:23 +01001395 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1396 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001397
Chris Wilsonea5b2132010-08-04 13:50:23 +01001398 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001399 DP_TRAINING_PATTERN_SET,
1400 dp_train_pat);
1401
Chris Wilsonea5b2132010-08-04 13:50:23 +01001402 ret = intel_dp_aux_native_write(intel_dp,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001403 DP_TRAINING_LANE0_SET,
1404 intel_dp->train_set, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001405 if (ret != 4)
1406 return false;
1407
1408 return true;
1409}
1410
Jesse Barnes33a34e42010-09-08 12:42:02 -07001411/* Enable corresponding port and start training pattern 1 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001412static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001413intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001414{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001415 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001416 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson58e10eb2010-10-03 10:56:11 +01001417 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001418 int i;
1419 uint8_t voltage;
1420 bool clock_recovery = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001421 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001422 u32 reg;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001423 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001424
Adam Jacksone8519462011-07-21 17:48:38 -04001425 /*
1426 * On CPT we have to enable the port in training pattern 1, which
1427 * will happen below in intel_dp_set_link_train. Otherwise, enable
1428 * the port and wait for it to become active.
1429 */
1430 if (!HAS_PCH_CPT(dev)) {
1431 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1432 POSTING_READ(intel_dp->output_reg);
1433 intel_wait_for_vblank(dev, intel_crtc->pipe);
1434 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001435
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001436 /* Write the link configuration data */
1437 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1438 intel_dp->link_configuration,
1439 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001440
1441 DP |= DP_PORT_EN;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001442 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001443 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1444 else
1445 DP &= ~DP_LINK_TRAIN_MASK;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001446 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001447 voltage = 0xff;
1448 tries = 0;
1449 clock_recovery = false;
1450 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001451 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001452 uint32_t signal_levels;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001453 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001454 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001455 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1456 } else {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001457 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001458 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1459 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001460
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001461 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001462 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1463 else
1464 reg = DP | DP_LINK_TRAIN_PAT_1;
1465
Chris Wilsonea5b2132010-08-04 13:50:23 +01001466 if (!intel_dp_set_link_train(intel_dp, reg,
Adam Jackson81055852011-07-21 17:48:37 -04001467 DP_TRAINING_PATTERN_1 |
1468 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001469 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001470 /* Set training pattern 1 */
1471
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001472 udelay(100);
1473 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001474 break;
1475
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001476 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1477 clock_recovery = true;
1478 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001479 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001480
1481 /* Check to see if we've tried the max voltage */
1482 for (i = 0; i < intel_dp->lane_count; i++)
1483 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1484 break;
1485 if (i == intel_dp->lane_count)
1486 break;
1487
1488 /* Check to see if we've tried the same voltage 5 times */
1489 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1490 ++tries;
1491 if (tries == 5)
1492 break;
1493 } else
1494 tries = 0;
1495 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1496
1497 /* Compute new intel_dp->train_set as requested by target */
1498 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001499 }
1500
Jesse Barnes33a34e42010-09-08 12:42:02 -07001501 intel_dp->DP = DP;
1502}
1503
1504static void
1505intel_dp_complete_link_train(struct intel_dp *intel_dp)
1506{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001507 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001508 struct drm_i915_private *dev_priv = dev->dev_private;
1509 bool channel_eq = false;
Jesse Barnes37f80972011-01-05 14:45:24 -08001510 int tries, cr_tries;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001511 u32 reg;
1512 uint32_t DP = intel_dp->DP;
1513
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001514 /* channel equalization */
1515 tries = 0;
Jesse Barnes37f80972011-01-05 14:45:24 -08001516 cr_tries = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001517 channel_eq = false;
1518 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001519 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001520 uint32_t signal_levels;
1521
Jesse Barnes37f80972011-01-05 14:45:24 -08001522 if (cr_tries > 5) {
1523 DRM_ERROR("failed to train DP, aborting\n");
1524 intel_dp_link_down(intel_dp);
1525 break;
1526 }
1527
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001528 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001529 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001530 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1531 } else {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001532 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001533 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1534 }
1535
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001536 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001537 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1538 else
1539 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001540
1541 /* channel eq pattern */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001542 if (!intel_dp_set_link_train(intel_dp, reg,
Adam Jackson81055852011-07-21 17:48:37 -04001543 DP_TRAINING_PATTERN_2 |
1544 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001545 break;
1546
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001547 udelay(400);
1548 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001549 break;
Jesse Barnes869184a2010-10-07 16:01:22 -07001550
Jesse Barnes37f80972011-01-05 14:45:24 -08001551 /* Make sure clock is still ok */
1552 if (!intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1553 intel_dp_start_link_train(intel_dp);
1554 cr_tries++;
1555 continue;
1556 }
1557
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001558 if (intel_channel_eq_ok(intel_dp)) {
1559 channel_eq = true;
1560 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001561 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001562
Jesse Barnes37f80972011-01-05 14:45:24 -08001563 /* Try 5 times, then try clock recovery if that fails */
1564 if (tries > 5) {
1565 intel_dp_link_down(intel_dp);
1566 intel_dp_start_link_train(intel_dp);
1567 tries = 0;
1568 cr_tries++;
1569 continue;
1570 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001571
1572 /* Compute new intel_dp->train_set as requested by target */
1573 intel_get_adjust_train(intel_dp);
1574 ++tries;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001575 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001576
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001577 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001578 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1579 else
1580 reg = DP | DP_LINK_TRAIN_OFF;
1581
Chris Wilsonea5b2132010-08-04 13:50:23 +01001582 I915_WRITE(intel_dp->output_reg, reg);
1583 POSTING_READ(intel_dp->output_reg);
1584 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001585 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1586}
1587
1588static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001589intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001590{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001591 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001592 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001593 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001594
Chris Wilson1b39d6f2010-12-06 11:20:45 +00001595 if ((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0)
1596 return;
1597
Zhao Yakui28c97732009-10-09 11:39:41 +08001598 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001599
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001600 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001601 DP &= ~DP_PLL_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001602 I915_WRITE(intel_dp->output_reg, DP);
1603 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001604 udelay(100);
1605 }
1606
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001607 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001608 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001609 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001610 } else {
1611 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001612 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001613 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01001614 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001615
Chris Wilsonfe255d02010-09-11 21:37:48 +01001616 msleep(17);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001617
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001618 if (is_edp(intel_dp))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001619 DP |= DP_LINK_TRAIN_OFF;
Eric Anholt5bddd172010-11-18 09:32:59 +08001620
Chris Wilson1b39d6f2010-12-06 11:20:45 +00001621 if (!HAS_PCH_CPT(dev) &&
1622 I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
Chris Wilson31acbcc2011-04-17 06:38:35 +01001623 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1624
Eric Anholt5bddd172010-11-18 09:32:59 +08001625 /* Hardware workaround: leaving our transcoder select
1626 * set to transcoder B while it's off will prevent the
1627 * corresponding HDMI output on transcoder A.
1628 *
1629 * Combine this with another hardware workaround:
1630 * transcoder select bit can only be cleared while the
1631 * port is enabled.
1632 */
1633 DP &= ~DP_PIPEB_SELECT;
1634 I915_WRITE(intel_dp->output_reg, DP);
1635
1636 /* Changes to enable or select take place the vblank
1637 * after being written.
1638 */
Chris Wilson31acbcc2011-04-17 06:38:35 +01001639 if (crtc == NULL) {
1640 /* We can arrive here never having been attached
1641 * to a CRTC, for instance, due to inheriting
1642 * random state from the BIOS.
1643 *
1644 * If the pipe is not running, play safe and
1645 * wait for the clocks to stabilise before
1646 * continuing.
1647 */
1648 POSTING_READ(intel_dp->output_reg);
1649 msleep(50);
1650 } else
1651 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
Eric Anholt5bddd172010-11-18 09:32:59 +08001652 }
1653
Chris Wilsonea5b2132010-08-04 13:50:23 +01001654 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1655 POSTING_READ(intel_dp->output_reg);
Keith Packardf01eca22011-09-28 16:48:10 -07001656 msleep(intel_dp->panel_power_down_delay);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001657}
1658
Keith Packard26d61aa2011-07-25 20:01:09 -07001659static bool
1660intel_dp_get_dpcd(struct intel_dp *intel_dp)
Keith Packard92fd8fd2011-07-25 19:50:10 -07001661{
Keith Packard92fd8fd2011-07-25 19:50:10 -07001662 if (intel_dp_aux_native_read_retry(intel_dp, 0x000, intel_dp->dpcd,
1663 sizeof (intel_dp->dpcd)) &&
1664 (intel_dp->dpcd[DP_DPCD_REV] != 0)) {
Keith Packard26d61aa2011-07-25 20:01:09 -07001665 return true;
Keith Packard92fd8fd2011-07-25 19:50:10 -07001666 }
1667
Keith Packard26d61aa2011-07-25 20:01:09 -07001668 return false;
Keith Packard92fd8fd2011-07-25 19:50:10 -07001669}
1670
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001671/*
1672 * According to DP spec
1673 * 5.1.2:
1674 * 1. Read DPCD
1675 * 2. Configure link according to Receiver Capabilities
1676 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1677 * 4. Check link status on receipt of hot-plug interrupt
1678 */
1679
1680static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001681intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001682{
Keith Packardd2b996a2011-07-25 22:37:51 -07001683 if (intel_dp->dpms_mode != DRM_MODE_DPMS_ON)
1684 return;
Jesse Barnes59cd09e2011-07-07 11:10:59 -07001685
Chris Wilson4ef69c72010-09-09 15:14:28 +01001686 if (!intel_dp->base.base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001687 return;
1688
Keith Packard92fd8fd2011-07-25 19:50:10 -07001689 /* Try to read receiver status if the link appears to be up */
Jesse Barnes33a34e42010-09-08 12:42:02 -07001690 if (!intel_dp_get_link_status(intel_dp)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001691 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001692 return;
1693 }
1694
Keith Packard92fd8fd2011-07-25 19:50:10 -07001695 /* Now read the DPCD to see if it's actually running */
Keith Packard26d61aa2011-07-25 20:01:09 -07001696 if (!intel_dp_get_dpcd(intel_dp)) {
Jesse Barnes59cd09e2011-07-07 11:10:59 -07001697 intel_dp_link_down(intel_dp);
1698 return;
1699 }
1700
Jesse Barnes33a34e42010-09-08 12:42:02 -07001701 if (!intel_channel_eq_ok(intel_dp)) {
Keith Packard92fd8fd2011-07-25 19:50:10 -07001702 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
1703 drm_get_encoder_name(&intel_dp->base.base));
Jesse Barnes33a34e42010-09-08 12:42:02 -07001704 intel_dp_start_link_train(intel_dp);
1705 intel_dp_complete_link_train(intel_dp);
1706 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001707}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001708
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001709static enum drm_connector_status
Keith Packard26d61aa2011-07-25 20:01:09 -07001710intel_dp_detect_dpcd(struct intel_dp *intel_dp)
Adam Jackson71ba90002011-07-12 17:38:04 -04001711{
Keith Packard26d61aa2011-07-25 20:01:09 -07001712 if (intel_dp_get_dpcd(intel_dp))
1713 return connector_status_connected;
1714 return connector_status_disconnected;
Adam Jackson71ba90002011-07-12 17:38:04 -04001715}
1716
1717static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001718ironlake_dp_detect(struct intel_dp *intel_dp)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001719{
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001720 enum drm_connector_status status;
1721
Chris Wilsonfe16d942011-02-12 10:29:38 +00001722 /* Can't disconnect eDP, but you can close the lid... */
1723 if (is_edp(intel_dp)) {
1724 status = intel_panel_detect(intel_dp->base.base.dev);
1725 if (status == connector_status_unknown)
1726 status = connector_status_connected;
1727 return status;
1728 }
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001729
Keith Packard26d61aa2011-07-25 20:01:09 -07001730 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001731}
1732
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001733static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001734g4x_dp_detect(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001735{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001736 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001737 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001738 uint32_t temp, bit;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001739
Chris Wilsonea5b2132010-08-04 13:50:23 +01001740 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001741 case DP_B:
1742 bit = DPB_HOTPLUG_INT_STATUS;
1743 break;
1744 case DP_C:
1745 bit = DPC_HOTPLUG_INT_STATUS;
1746 break;
1747 case DP_D:
1748 bit = DPD_HOTPLUG_INT_STATUS;
1749 break;
1750 default:
1751 return connector_status_unknown;
1752 }
1753
1754 temp = I915_READ(PORT_HOTPLUG_STAT);
1755
1756 if ((temp & bit) == 0)
1757 return connector_status_disconnected;
1758
Keith Packard26d61aa2011-07-25 20:01:09 -07001759 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001760}
1761
Keith Packard8c241fe2011-09-28 16:38:44 -07001762static struct edid *
1763intel_dp_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
1764{
1765 struct intel_dp *intel_dp = intel_attached_dp(connector);
1766 struct edid *edid;
1767
1768 ironlake_edp_panel_vdd_on(intel_dp);
1769 edid = drm_get_edid(connector, adapter);
1770 ironlake_edp_panel_vdd_off(intel_dp);
1771 return edid;
1772}
1773
1774static int
1775intel_dp_get_edid_modes(struct drm_connector *connector, struct i2c_adapter *adapter)
1776{
1777 struct intel_dp *intel_dp = intel_attached_dp(connector);
1778 int ret;
1779
1780 ironlake_edp_panel_vdd_on(intel_dp);
1781 ret = intel_ddc_get_modes(connector, adapter);
1782 ironlake_edp_panel_vdd_off(intel_dp);
1783 return ret;
1784}
1785
1786
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001787/**
1788 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1789 *
1790 * \return true if DP port is connected.
1791 * \return false if DP port is disconnected.
1792 */
1793static enum drm_connector_status
1794intel_dp_detect(struct drm_connector *connector, bool force)
1795{
1796 struct intel_dp *intel_dp = intel_attached_dp(connector);
1797 struct drm_device *dev = intel_dp->base.base.dev;
1798 enum drm_connector_status status;
1799 struct edid *edid = NULL;
1800
1801 intel_dp->has_audio = false;
1802
1803 if (HAS_PCH_SPLIT(dev))
1804 status = ironlake_dp_detect(intel_dp);
1805 else
1806 status = g4x_dp_detect(intel_dp);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04001807
Adam Jacksonac66ae82011-07-12 17:38:03 -04001808 DRM_DEBUG_KMS("DPCD: %02hx%02hx%02hx%02hx%02hx%02hx%02hx%02hx\n",
1809 intel_dp->dpcd[0], intel_dp->dpcd[1], intel_dp->dpcd[2],
1810 intel_dp->dpcd[3], intel_dp->dpcd[4], intel_dp->dpcd[5],
1811 intel_dp->dpcd[6], intel_dp->dpcd[7]);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04001812
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001813 if (status != connector_status_connected)
1814 return status;
1815
Chris Wilsonf6849602010-09-19 09:29:33 +01001816 if (intel_dp->force_audio) {
1817 intel_dp->has_audio = intel_dp->force_audio > 0;
1818 } else {
Keith Packard8c241fe2011-09-28 16:38:44 -07001819 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
Chris Wilsonf6849602010-09-19 09:29:33 +01001820 if (edid) {
1821 intel_dp->has_audio = drm_detect_monitor_audio(edid);
1822 connector->display_info.raw_edid = NULL;
1823 kfree(edid);
1824 }
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001825 }
1826
1827 return connector_status_connected;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001828}
1829
1830static int intel_dp_get_modes(struct drm_connector *connector)
1831{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001832 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001833 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001834 struct drm_i915_private *dev_priv = dev->dev_private;
1835 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001836
1837 /* We should parse the EDID data and find out if it has an audio sink
1838 */
1839
Keith Packard8c241fe2011-09-28 16:38:44 -07001840 ret = intel_dp_get_edid_modes(connector, &intel_dp->adapter);
Zhao Yakuib9efc482010-07-19 09:43:11 +01001841 if (ret) {
Keith Packardd15456d2011-09-18 17:35:47 -07001842 if (is_edp(intel_dp) && !intel_dp->panel_fixed_mode) {
Zhao Yakuib9efc482010-07-19 09:43:11 +01001843 struct drm_display_mode *newmode;
1844 list_for_each_entry(newmode, &connector->probed_modes,
1845 head) {
Keith Packardd15456d2011-09-18 17:35:47 -07001846 if ((newmode->type & DRM_MODE_TYPE_PREFERRED)) {
1847 intel_dp->panel_fixed_mode =
Zhao Yakuib9efc482010-07-19 09:43:11 +01001848 drm_mode_duplicate(dev, newmode);
1849 break;
1850 }
1851 }
1852 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001853 return ret;
Zhao Yakuib9efc482010-07-19 09:43:11 +01001854 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001855
1856 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Jesse Barnes4d926462010-10-07 16:01:07 -07001857 if (is_edp(intel_dp)) {
Keith Packard47f0eb22011-09-19 14:33:26 -07001858 /* initialize panel mode from VBT if available for eDP */
Keith Packardd15456d2011-09-18 17:35:47 -07001859 if (intel_dp->panel_fixed_mode == NULL && dev_priv->lfp_lvds_vbt_mode != NULL) {
1860 intel_dp->panel_fixed_mode =
Keith Packard47f0eb22011-09-19 14:33:26 -07001861 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
Keith Packardd15456d2011-09-18 17:35:47 -07001862 if (intel_dp->panel_fixed_mode) {
1863 intel_dp->panel_fixed_mode->type |=
Keith Packard47f0eb22011-09-19 14:33:26 -07001864 DRM_MODE_TYPE_PREFERRED;
1865 }
1866 }
Keith Packardd15456d2011-09-18 17:35:47 -07001867 if (intel_dp->panel_fixed_mode) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001868 struct drm_display_mode *mode;
Keith Packardd15456d2011-09-18 17:35:47 -07001869 mode = drm_mode_duplicate(dev, intel_dp->panel_fixed_mode);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001870 drm_mode_probed_add(connector, mode);
1871 return 1;
1872 }
1873 }
1874 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001875}
1876
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001877static bool
1878intel_dp_detect_audio(struct drm_connector *connector)
1879{
1880 struct intel_dp *intel_dp = intel_attached_dp(connector);
1881 struct edid *edid;
1882 bool has_audio = false;
1883
Keith Packard8c241fe2011-09-28 16:38:44 -07001884 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001885 if (edid) {
1886 has_audio = drm_detect_monitor_audio(edid);
1887
1888 connector->display_info.raw_edid = NULL;
1889 kfree(edid);
1890 }
1891
1892 return has_audio;
1893}
1894
Chris Wilsonf6849602010-09-19 09:29:33 +01001895static int
1896intel_dp_set_property(struct drm_connector *connector,
1897 struct drm_property *property,
1898 uint64_t val)
1899{
Chris Wilsone953fd72011-02-21 22:23:52 +00001900 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Chris Wilsonf6849602010-09-19 09:29:33 +01001901 struct intel_dp *intel_dp = intel_attached_dp(connector);
1902 int ret;
1903
1904 ret = drm_connector_property_set_value(connector, property, val);
1905 if (ret)
1906 return ret;
1907
Chris Wilson3f43c482011-05-12 22:17:24 +01001908 if (property == dev_priv->force_audio_property) {
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001909 int i = val;
1910 bool has_audio;
1911
1912 if (i == intel_dp->force_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01001913 return 0;
1914
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001915 intel_dp->force_audio = i;
Chris Wilsonf6849602010-09-19 09:29:33 +01001916
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001917 if (i == 0)
1918 has_audio = intel_dp_detect_audio(connector);
1919 else
1920 has_audio = i > 0;
1921
1922 if (has_audio == intel_dp->has_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01001923 return 0;
1924
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001925 intel_dp->has_audio = has_audio;
Chris Wilsonf6849602010-09-19 09:29:33 +01001926 goto done;
1927 }
1928
Chris Wilsone953fd72011-02-21 22:23:52 +00001929 if (property == dev_priv->broadcast_rgb_property) {
1930 if (val == !!intel_dp->color_range)
1931 return 0;
1932
1933 intel_dp->color_range = val ? DP_COLOR_RANGE_16_235 : 0;
1934 goto done;
1935 }
1936
Chris Wilsonf6849602010-09-19 09:29:33 +01001937 return -EINVAL;
1938
1939done:
1940 if (intel_dp->base.base.crtc) {
1941 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1942 drm_crtc_helper_set_mode(crtc, &crtc->mode,
1943 crtc->x, crtc->y,
1944 crtc->fb);
1945 }
1946
1947 return 0;
1948}
1949
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001950static void
1951intel_dp_destroy (struct drm_connector *connector)
1952{
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001953 struct drm_device *dev = connector->dev;
1954
1955 if (intel_dpd_is_edp(dev))
1956 intel_panel_destroy_backlight(dev);
1957
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001958 drm_sysfs_connector_remove(connector);
1959 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001960 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001961}
1962
Daniel Vetter24d05922010-08-20 18:08:28 +02001963static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1964{
1965 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1966
1967 i2c_del_adapter(&intel_dp->adapter);
1968 drm_encoder_cleanup(encoder);
1969 kfree(intel_dp);
1970}
1971
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001972static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1973 .dpms = intel_dp_dpms,
1974 .mode_fixup = intel_dp_mode_fixup,
Jesse Barnesd240f202010-08-13 15:43:26 -07001975 .prepare = intel_dp_prepare,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001976 .mode_set = intel_dp_mode_set,
Jesse Barnesd240f202010-08-13 15:43:26 -07001977 .commit = intel_dp_commit,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001978};
1979
1980static const struct drm_connector_funcs intel_dp_connector_funcs = {
1981 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001982 .detect = intel_dp_detect,
1983 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilsonf6849602010-09-19 09:29:33 +01001984 .set_property = intel_dp_set_property,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001985 .destroy = intel_dp_destroy,
1986};
1987
1988static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1989 .get_modes = intel_dp_get_modes,
1990 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01001991 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001992};
1993
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001994static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02001995 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001996};
1997
Chris Wilson995b6762010-08-20 13:23:26 +01001998static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001999intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07002000{
Chris Wilsonea5b2132010-08-04 13:50:23 +01002001 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Keith Packardc8110e52009-05-06 11:51:10 -07002002
Jesse Barnes885a5012011-07-07 11:11:01 -07002003 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07002004}
2005
Zhenyu Wange3421a12010-04-08 09:43:27 +08002006/* Return which DP Port should be selected for Transcoder DP control */
2007int
2008intel_trans_dp_port_sel (struct drm_crtc *crtc)
2009{
2010 struct drm_device *dev = crtc->dev;
2011 struct drm_mode_config *mode_config = &dev->mode_config;
2012 struct drm_encoder *encoder;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002013
2014 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002015 struct intel_dp *intel_dp;
2016
Dan Carpenterd8201ab2010-05-07 10:39:00 +02002017 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08002018 continue;
2019
Chris Wilsonea5b2132010-08-04 13:50:23 +01002020 intel_dp = enc_to_intel_dp(encoder);
2021 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
2022 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002023 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01002024
Zhenyu Wange3421a12010-04-08 09:43:27 +08002025 return -1;
2026}
2027
Zhao Yakui36e83a12010-06-12 14:32:21 +08002028/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04002029bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08002030{
2031 struct drm_i915_private *dev_priv = dev->dev_private;
2032 struct child_device_config *p_child;
2033 int i;
2034
2035 if (!dev_priv->child_dev_num)
2036 return false;
2037
2038 for (i = 0; i < dev_priv->child_dev_num; i++) {
2039 p_child = dev_priv->child_dev + i;
2040
2041 if (p_child->dvo_port == PORT_IDPD &&
2042 p_child->device_type == DEVICE_TYPE_eDP)
2043 return true;
2044 }
2045 return false;
2046}
2047
Chris Wilsonf6849602010-09-19 09:29:33 +01002048static void
2049intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
2050{
Chris Wilson3f43c482011-05-12 22:17:24 +01002051 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +00002052 intel_attach_broadcast_rgb_property(connector);
Chris Wilsonf6849602010-09-19 09:29:33 +01002053}
2054
Keith Packardc8110e52009-05-06 11:51:10 -07002055void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002056intel_dp_init(struct drm_device *dev, int output_reg)
2057{
2058 struct drm_i915_private *dev_priv = dev->dev_private;
2059 struct drm_connector *connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +01002060 struct intel_dp *intel_dp;
Eric Anholt21d40d32010-03-25 11:11:14 -07002061 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002062 struct intel_connector *intel_connector;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002063 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04002064 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002065
Chris Wilsonea5b2132010-08-04 13:50:23 +01002066 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
2067 if (!intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002068 return;
2069
Chris Wilson3d3dc142011-02-12 10:33:12 +00002070 intel_dp->output_reg = output_reg;
Keith Packardd2b996a2011-07-25 22:37:51 -07002071 intel_dp->dpms_mode = -1;
Chris Wilson3d3dc142011-02-12 10:33:12 +00002072
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002073 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
2074 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002075 kfree(intel_dp);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002076 return;
2077 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01002078 intel_encoder = &intel_dp->base;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002079
Chris Wilsonea5b2132010-08-04 13:50:23 +01002080 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04002081 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01002082 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04002083
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07002084 if (output_reg == DP_A || is_pch_edp(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04002085 type = DRM_MODE_CONNECTOR_eDP;
2086 intel_encoder->type = INTEL_OUTPUT_EDP;
2087 } else {
2088 type = DRM_MODE_CONNECTOR_DisplayPort;
2089 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
2090 }
2091
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002092 connector = &intel_connector->base;
Adam Jacksonb3295302010-07-16 14:46:28 -04002093 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002094 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
2095
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002096 connector->polled = DRM_CONNECTOR_POLL_HPD;
2097
Zhao Yakui652af9d2009-12-02 10:03:33 +08002098 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07002099 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08002100 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07002101 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08002102 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07002103 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08002104
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07002105 if (is_edp(intel_dp))
Eric Anholt21d40d32010-03-25 11:11:14 -07002106 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08002107
Eric Anholt21d40d32010-03-25 11:11:14 -07002108 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002109 connector->interlace_allowed = true;
2110 connector->doublescan_allowed = 0;
2111
Chris Wilson4ef69c72010-09-09 15:14:28 +01002112 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002113 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +01002114 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002115
Chris Wilsondf0e9242010-09-09 16:20:55 +01002116 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002117 drm_sysfs_connector_add(connector);
2118
2119 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002120 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002121 case DP_A:
2122 name = "DPDDC-A";
2123 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002124 case DP_B:
2125 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002126 dev_priv->hotplug_supported_mask |=
2127 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002128 name = "DPDDC-B";
2129 break;
2130 case DP_C:
2131 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002132 dev_priv->hotplug_supported_mask |=
2133 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002134 name = "DPDDC-C";
2135 break;
2136 case DP_D:
2137 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002138 dev_priv->hotplug_supported_mask |=
2139 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002140 name = "DPDDC-D";
2141 break;
2142 }
2143
Jesse Barnes89667382010-10-07 16:01:21 -07002144 /* Cache some DPCD data in the eDP case */
2145 if (is_edp(intel_dp)) {
Keith Packard59f3e272011-07-25 20:01:56 -07002146 bool ret;
Keith Packardf01eca22011-09-28 16:48:10 -07002147 struct edp_power_seq cur, vbt;
2148 u32 pp_on, pp_off, pp_div;
Jesse Barnes89667382010-10-07 16:01:21 -07002149
Jesse Barnes5d613502011-01-24 17:10:54 -08002150 pp_on = I915_READ(PCH_PP_ON_DELAYS);
Keith Packardf01eca22011-09-28 16:48:10 -07002151 pp_off = I915_READ(PCH_PP_OFF_DELAYS);
Jesse Barnes5d613502011-01-24 17:10:54 -08002152 pp_div = I915_READ(PCH_PP_DIVISOR);
2153
Keith Packardf01eca22011-09-28 16:48:10 -07002154 /* Pull timing values out of registers */
2155 cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
2156 PANEL_POWER_UP_DELAY_SHIFT;
2157
2158 cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
2159 PANEL_LIGHT_ON_DELAY_SHIFT;
2160
2161 cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
2162 PANEL_LIGHT_OFF_DELAY_SHIFT;
2163
2164 cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
2165 PANEL_POWER_DOWN_DELAY_SHIFT;
2166
2167 cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
2168 PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
2169
2170 DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
2171 cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
2172
2173 vbt = dev_priv->edp.pps;
2174
2175 DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
2176 vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
2177
2178#define get_delay(field) ((max(cur.field, vbt.field) + 9) / 10)
2179
2180 intel_dp->panel_power_up_delay = get_delay(t1_t3);
2181 intel_dp->backlight_on_delay = get_delay(t8);
2182 intel_dp->backlight_off_delay = get_delay(t9);
2183 intel_dp->panel_power_down_delay = get_delay(t10);
2184 intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
2185
2186 DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
2187 intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
2188 intel_dp->panel_power_cycle_delay);
2189
2190 DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
2191 intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
Jesse Barnes5d613502011-01-24 17:10:54 -08002192
2193 ironlake_edp_panel_vdd_on(intel_dp);
Keith Packard59f3e272011-07-25 20:01:56 -07002194 ret = intel_dp_get_dpcd(intel_dp);
Chris Wilson3d3dc142011-02-12 10:33:12 +00002195 ironlake_edp_panel_vdd_off(intel_dp);
Keith Packard59f3e272011-07-25 20:01:56 -07002196 if (ret) {
Jesse Barnes7183dc22011-07-07 11:10:58 -07002197 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
2198 dev_priv->no_aux_handshake =
2199 intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
Jesse Barnes89667382010-10-07 16:01:21 -07002200 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
2201 } else {
Chris Wilson3d3dc142011-02-12 10:33:12 +00002202 /* if this fails, presume the device is a ghost */
Takashi Iwai48898b02011-03-18 09:06:49 +00002203 DRM_INFO("failed to retrieve link info, disabling eDP\n");
Chris Wilson3d3dc142011-02-12 10:33:12 +00002204 intel_dp_encoder_destroy(&intel_dp->base.base);
Takashi Iwai48898b02011-03-18 09:06:49 +00002205 intel_dp_destroy(&intel_connector->base);
Chris Wilson3d3dc142011-02-12 10:33:12 +00002206 return;
Jesse Barnes89667382010-10-07 16:01:21 -07002207 }
Jesse Barnes89667382010-10-07 16:01:21 -07002208 }
2209
Keith Packard552fb0b2011-09-28 16:31:53 -07002210 intel_dp_i2c_init(intel_dp, intel_connector, name);
2211
Eric Anholt21d40d32010-03-25 11:11:14 -07002212 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002213
Jesse Barnes4d926462010-10-07 16:01:07 -07002214 if (is_edp(intel_dp)) {
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02002215 dev_priv->int_edp_connector = connector;
2216 intel_panel_setup_backlight(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002217 }
2218
Chris Wilsonf6849602010-09-19 09:29:33 +01002219 intel_dp_add_properties(intel_dp, connector);
2220
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002221 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
2222 * 0xd. Failure to do so will result in spurious interrupts being
2223 * generated on the port when a cable is not attached.
2224 */
2225 if (IS_G4X(dev) && !IS_GM45(dev)) {
2226 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
2227 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
2228 }
2229}