blob: ecb976451a21f090b29fc1c90667b12f3a98b517 [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 Packardebf33b12011-09-29 15:53:27 -0700286static bool ironlake_edp_have_panel_power(struct intel_dp *intel_dp)
287{
288 struct drm_device *dev = intel_dp->base.base.dev;
289 struct drm_i915_private *dev_priv = dev->dev_private;
290
291 return (I915_READ(PCH_PP_STATUS) & PP_ON) != 0;
292}
293
294static bool ironlake_edp_have_panel_vdd(struct intel_dp *intel_dp)
295{
296 struct drm_device *dev = intel_dp->base.base.dev;
297 struct drm_i915_private *dev_priv = dev->dev_private;
298
299 return (I915_READ(PCH_PP_CONTROL) & EDP_FORCE_VDD) != 0;
300}
301
Keith Packard9b984da2011-09-19 13:54:47 -0700302static void
303intel_dp_check_edp(struct intel_dp *intel_dp)
304{
305 struct drm_device *dev = intel_dp->base.base.dev;
306 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packardebf33b12011-09-29 15:53:27 -0700307
Keith Packard9b984da2011-09-19 13:54:47 -0700308 if (!is_edp(intel_dp))
309 return;
Keith Packardebf33b12011-09-29 15:53:27 -0700310 if (!ironlake_edp_have_panel_power(intel_dp) && !ironlake_edp_have_panel_vdd(intel_dp)) {
Keith Packard9b984da2011-09-19 13:54:47 -0700311 WARN(1, "eDP powered off while attempting aux channel communication.\n");
312 DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
Keith Packardebf33b12011-09-29 15:53:27 -0700313 I915_READ(PCH_PP_STATUS),
Keith Packard9b984da2011-09-19 13:54:47 -0700314 I915_READ(PCH_PP_CONTROL));
315 }
316}
317
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700318static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100319intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700320 uint8_t *send, int send_bytes,
321 uint8_t *recv, int recv_size)
322{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100323 uint32_t output_reg = intel_dp->output_reg;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100324 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700325 struct drm_i915_private *dev_priv = dev->dev_private;
326 uint32_t ch_ctl = output_reg + 0x10;
327 uint32_t ch_data = ch_ctl + 4;
328 int i;
329 int recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700330 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700331 uint32_t aux_clock_divider;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800332 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700333
Keith Packard9b984da2011-09-19 13:54:47 -0700334 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700335 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700336 * and would like to run at 2MHz. So, take the
337 * hrawclk value and divide by 2 and use that
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700338 *
339 * Note that PCH attached eDP panels should use a 125MHz input
340 * clock divider.
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700341 */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700342 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +0800343 if (IS_GEN6(dev))
344 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
345 else
346 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
347 } else if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500348 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800349 else
350 aux_clock_divider = intel_hrawclk(dev) / 2;
351
Zhenyu Wange3421a12010-04-08 09:43:27 +0800352 if (IS_GEN6(dev))
353 precharge = 3;
354 else
355 precharge = 5;
356
Jesse Barnes11bee432011-08-01 15:02:20 -0700357 /* Try to wait for any previous AUX channel activity */
358 for (try = 0; try < 3; try++) {
359 status = I915_READ(ch_ctl);
360 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
361 break;
362 msleep(1);
363 }
364
365 if (try == 3) {
366 WARN(1, "dp_aux_ch not started status 0x%08x\n",
367 I915_READ(ch_ctl));
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100368 return -EBUSY;
369 }
370
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700371 /* Must try at least 3 times according to DP spec */
372 for (try = 0; try < 5; try++) {
373 /* Load the send data into the aux channel data registers */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100374 for (i = 0; i < send_bytes; i += 4)
375 I915_WRITE(ch_data + i,
376 pack_aux(send + i, send_bytes - i));
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700377
378 /* Send the command and wait for it to complete */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100379 I915_WRITE(ch_ctl,
380 DP_AUX_CH_CTL_SEND_BUSY |
381 DP_AUX_CH_CTL_TIME_OUT_400us |
382 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
383 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
384 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
385 DP_AUX_CH_CTL_DONE |
386 DP_AUX_CH_CTL_TIME_OUT_ERROR |
387 DP_AUX_CH_CTL_RECEIVE_ERROR);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700388 for (;;) {
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700389 status = I915_READ(ch_ctl);
390 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
391 break;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100392 udelay(100);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700393 }
394
395 /* Clear done status and any errors */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100396 I915_WRITE(ch_ctl,
397 status |
398 DP_AUX_CH_CTL_DONE |
399 DP_AUX_CH_CTL_TIME_OUT_ERROR |
400 DP_AUX_CH_CTL_RECEIVE_ERROR);
401 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700402 break;
403 }
404
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700405 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700406 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700407 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700408 }
409
410 /* Check for timeout or receive error.
411 * Timeouts occur when the sink is not connected
412 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700413 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700414 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700415 return -EIO;
416 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700417
418 /* Timeouts occur when the device isn't connected, so they're
419 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700420 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800421 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700422 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700423 }
424
425 /* Unload any bytes sent back from the other side */
426 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
427 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700428 if (recv_bytes > recv_size)
429 recv_bytes = recv_size;
430
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100431 for (i = 0; i < recv_bytes; i += 4)
432 unpack_aux(I915_READ(ch_data + i),
433 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700434
435 return recv_bytes;
436}
437
438/* Write data to the aux channel in native mode */
439static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100440intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700441 uint16_t address, uint8_t *send, int send_bytes)
442{
443 int ret;
444 uint8_t msg[20];
445 int msg_bytes;
446 uint8_t ack;
447
Keith Packard9b984da2011-09-19 13:54:47 -0700448 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700449 if (send_bytes > 16)
450 return -1;
451 msg[0] = AUX_NATIVE_WRITE << 4;
452 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800453 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700454 msg[3] = send_bytes - 1;
455 memcpy(&msg[4], send, send_bytes);
456 msg_bytes = send_bytes + 4;
457 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100458 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700459 if (ret < 0)
460 return ret;
461 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
462 break;
463 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
464 udelay(100);
465 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700466 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700467 }
468 return send_bytes;
469}
470
471/* Write a single byte to the aux channel in native mode */
472static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100473intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700474 uint16_t address, uint8_t byte)
475{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100476 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700477}
478
479/* read bytes from a native aux channel */
480static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100481intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700482 uint16_t address, uint8_t *recv, int recv_bytes)
483{
484 uint8_t msg[4];
485 int msg_bytes;
486 uint8_t reply[20];
487 int reply_bytes;
488 uint8_t ack;
489 int ret;
490
Keith Packard9b984da2011-09-19 13:54:47 -0700491 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700492 msg[0] = AUX_NATIVE_READ << 4;
493 msg[1] = address >> 8;
494 msg[2] = address & 0xff;
495 msg[3] = recv_bytes - 1;
496
497 msg_bytes = 4;
498 reply_bytes = recv_bytes + 1;
499
500 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100501 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700502 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700503 if (ret == 0)
504 return -EPROTO;
505 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700506 return ret;
507 ack = reply[0];
508 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
509 memcpy(recv, reply + 1, ret - 1);
510 return ret - 1;
511 }
512 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
513 udelay(100);
514 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700515 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700516 }
517}
518
519static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000520intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
521 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700522{
Dave Airlieab2c0672009-12-04 10:55:24 +1000523 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100524 struct intel_dp *intel_dp = container_of(adapter,
525 struct intel_dp,
526 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000527 uint16_t address = algo_data->address;
528 uint8_t msg[5];
529 uint8_t reply[2];
David Flynn8316f332010-12-08 16:10:21 +0000530 unsigned retry;
Dave Airlieab2c0672009-12-04 10:55:24 +1000531 int msg_bytes;
532 int reply_bytes;
533 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700534
Keith Packard9b984da2011-09-19 13:54:47 -0700535 intel_dp_check_edp(intel_dp);
Dave Airlieab2c0672009-12-04 10:55:24 +1000536 /* Set up the command byte */
537 if (mode & MODE_I2C_READ)
538 msg[0] = AUX_I2C_READ << 4;
539 else
540 msg[0] = AUX_I2C_WRITE << 4;
541
542 if (!(mode & MODE_I2C_STOP))
543 msg[0] |= AUX_I2C_MOT << 4;
544
545 msg[1] = address >> 8;
546 msg[2] = address;
547
548 switch (mode) {
549 case MODE_I2C_WRITE:
550 msg[3] = 0;
551 msg[4] = write_byte;
552 msg_bytes = 5;
553 reply_bytes = 1;
554 break;
555 case MODE_I2C_READ:
556 msg[3] = 0;
557 msg_bytes = 4;
558 reply_bytes = 2;
559 break;
560 default:
561 msg_bytes = 3;
562 reply_bytes = 1;
563 break;
564 }
565
David Flynn8316f332010-12-08 16:10:21 +0000566 for (retry = 0; retry < 5; retry++) {
567 ret = intel_dp_aux_ch(intel_dp,
568 msg, msg_bytes,
569 reply, reply_bytes);
Dave Airlieab2c0672009-12-04 10:55:24 +1000570 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000571 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000572 return ret;
573 }
David Flynn8316f332010-12-08 16:10:21 +0000574
575 switch (reply[0] & AUX_NATIVE_REPLY_MASK) {
576 case AUX_NATIVE_REPLY_ACK:
577 /* I2C-over-AUX Reply field is only valid
578 * when paired with AUX ACK.
579 */
580 break;
581 case AUX_NATIVE_REPLY_NACK:
582 DRM_DEBUG_KMS("aux_ch native nack\n");
583 return -EREMOTEIO;
584 case AUX_NATIVE_REPLY_DEFER:
585 udelay(100);
586 continue;
587 default:
588 DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
589 reply[0]);
590 return -EREMOTEIO;
591 }
592
Dave Airlieab2c0672009-12-04 10:55:24 +1000593 switch (reply[0] & AUX_I2C_REPLY_MASK) {
594 case AUX_I2C_REPLY_ACK:
595 if (mode == MODE_I2C_READ) {
596 *read_byte = reply[1];
597 }
598 return reply_bytes - 1;
599 case AUX_I2C_REPLY_NACK:
David Flynn8316f332010-12-08 16:10:21 +0000600 DRM_DEBUG_KMS("aux_i2c nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000601 return -EREMOTEIO;
602 case AUX_I2C_REPLY_DEFER:
David Flynn8316f332010-12-08 16:10:21 +0000603 DRM_DEBUG_KMS("aux_i2c defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000604 udelay(100);
605 break;
606 default:
David Flynn8316f332010-12-08 16:10:21 +0000607 DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
Dave Airlieab2c0672009-12-04 10:55:24 +1000608 return -EREMOTEIO;
609 }
610 }
David Flynn8316f332010-12-08 16:10:21 +0000611
612 DRM_ERROR("too many retries, giving up\n");
613 return -EREMOTEIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700614}
615
Keith Packard0b5c5412011-09-28 16:41:05 -0700616static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp);
617static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp);
618
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700619static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100620intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800621 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700622{
Keith Packard0b5c5412011-09-28 16:41:05 -0700623 int ret;
624
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800625 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100626 intel_dp->algo.running = false;
627 intel_dp->algo.address = 0;
628 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700629
Chris Wilsonea5b2132010-08-04 13:50:23 +0100630 memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
631 intel_dp->adapter.owner = THIS_MODULE;
632 intel_dp->adapter.class = I2C_CLASS_DDC;
633 strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
634 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
635 intel_dp->adapter.algo_data = &intel_dp->algo;
636 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
637
Keith Packard0b5c5412011-09-28 16:41:05 -0700638 ironlake_edp_panel_vdd_on(intel_dp);
639 ret = i2c_dp_aux_add_bus(&intel_dp->adapter);
640 ironlake_edp_panel_vdd_off(intel_dp);
641 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700642}
643
644static bool
645intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
646 struct drm_display_mode *adjusted_mode)
647{
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100648 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100649 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700650 int lane_count, clock;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100651 int max_lane_count = intel_dp_max_lane_count(intel_dp);
652 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700653 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
654
Keith Packardd15456d2011-09-18 17:35:47 -0700655 if (is_edp(intel_dp) && intel_dp->panel_fixed_mode) {
656 intel_fixed_panel_mode(intel_dp->panel_fixed_mode, adjusted_mode);
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100657 intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
658 mode, adjusted_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100659 /*
660 * the mode->clock is used to calculate the Data&Link M/N
661 * of the pipe. For the eDP the fixed clock should be used.
662 */
Keith Packardd15456d2011-09-18 17:35:47 -0700663 mode->clock = intel_dp->panel_fixed_mode->clock;
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100664 }
665
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700666 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
667 for (clock = 0; clock <= max_clock; clock++) {
Dave Airliefe27d532010-06-30 11:46:17 +1000668 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700669
Chris Wilsonea5b2132010-08-04 13:50:23 +0100670 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800671 <= link_avail) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100672 intel_dp->link_bw = bws[clock];
673 intel_dp->lane_count = lane_count;
674 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Zhao Yakui28c97732009-10-09 11:39:41 +0800675 DRM_DEBUG_KMS("Display port link bw %02x lane "
676 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100677 intel_dp->link_bw, intel_dp->lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700678 adjusted_mode->clock);
679 return true;
680 }
681 }
682 }
Dave Airliefe27d532010-06-30 11:46:17 +1000683
Chris Wilson3cf2efb2010-11-29 10:09:55 +0000684 if (is_edp(intel_dp)) {
685 /* okay we failed just pick the highest */
686 intel_dp->lane_count = max_lane_count;
687 intel_dp->link_bw = bws[max_clock];
688 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
689 DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
690 "count %d clock %d\n",
691 intel_dp->link_bw, intel_dp->lane_count,
692 adjusted_mode->clock);
693
694 return true;
695 }
696
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700697 return false;
698}
699
700struct intel_dp_m_n {
701 uint32_t tu;
702 uint32_t gmch_m;
703 uint32_t gmch_n;
704 uint32_t link_m;
705 uint32_t link_n;
706};
707
708static void
709intel_reduce_ratio(uint32_t *num, uint32_t *den)
710{
711 while (*num > 0xffffff || *den > 0xffffff) {
712 *num >>= 1;
713 *den >>= 1;
714 }
715}
716
717static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800718intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700719 int nlanes,
720 int pixel_clock,
721 int link_clock,
722 struct intel_dp_m_n *m_n)
723{
724 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800725 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700726 m_n->gmch_n = link_clock * nlanes;
727 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
728 m_n->link_m = pixel_clock;
729 m_n->link_n = link_clock;
730 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
731}
732
733void
734intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
735 struct drm_display_mode *adjusted_mode)
736{
737 struct drm_device *dev = crtc->dev;
738 struct drm_mode_config *mode_config = &dev->mode_config;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800739 struct drm_encoder *encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700740 struct drm_i915_private *dev_priv = dev->dev_private;
741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes858fa0352011-06-24 12:19:24 -0700742 int lane_count = 4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700743 struct intel_dp_m_n m_n;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800744 int pipe = intel_crtc->pipe;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700745
746 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700747 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700748 */
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800749 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100750 struct intel_dp *intel_dp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700751
Dan Carpenterd8201ab2010-05-07 10:39:00 +0200752 if (encoder->crtc != crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700753 continue;
754
Chris Wilsonea5b2132010-08-04 13:50:23 +0100755 intel_dp = enc_to_intel_dp(encoder);
756 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
757 lane_count = intel_dp->lane_count;
Jesse Barnes51190662010-10-07 16:01:08 -0700758 break;
759 } else if (is_edp(intel_dp)) {
760 lane_count = dev_priv->edp.lanes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700761 break;
762 }
763 }
764
765 /*
766 * Compute the GMCH and Link ratios. The '3' here is
767 * the number of bytes_per_pixel post-LUT, which we always
768 * set up for 8-bits of R/G/B, or 3 bytes total.
769 */
Jesse Barnes858fa0352011-06-24 12:19:24 -0700770 intel_dp_compute_m_n(intel_crtc->bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700771 mode->clock, adjusted_mode->clock, &m_n);
772
Eric Anholtc619eed2010-01-28 16:45:52 -0800773 if (HAS_PCH_SPLIT(dev)) {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800774 I915_WRITE(TRANSDATA_M1(pipe),
775 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
776 m_n.gmch_m);
777 I915_WRITE(TRANSDATA_N1(pipe), m_n.gmch_n);
778 I915_WRITE(TRANSDPLINK_M1(pipe), m_n.link_m);
779 I915_WRITE(TRANSDPLINK_N1(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700780 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800781 I915_WRITE(PIPE_GMCH_DATA_M(pipe),
782 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
783 m_n.gmch_m);
784 I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n.gmch_n);
785 I915_WRITE(PIPE_DP_LINK_M(pipe), m_n.link_m);
786 I915_WRITE(PIPE_DP_LINK_N(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700787 }
788}
789
Keith Packardf01eca22011-09-28 16:48:10 -0700790static void ironlake_edp_pll_on(struct drm_encoder *encoder);
791static void ironlake_edp_pll_off(struct drm_encoder *encoder);
792
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700793static void
794intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
795 struct drm_display_mode *adjusted_mode)
796{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800797 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100798 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +0100799 struct drm_crtc *crtc = intel_dp->base.base.crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700800 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
801
Keith Packardf01eca22011-09-28 16:48:10 -0700802 /* Turn on the eDP PLL if needed */
803 if (is_edp(intel_dp)) {
804 if (!is_pch_edp(intel_dp))
805 ironlake_edp_pll_on(encoder);
806 else
807 ironlake_edp_pll_off(encoder);
808 }
809
Chris Wilsone953fd72011-02-21 22:23:52 +0000810 intel_dp->DP = DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
811 intel_dp->DP |= intel_dp->color_range;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400812
813 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100814 intel_dp->DP |= DP_SYNC_HS_HIGH;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400815 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100816 intel_dp->DP |= DP_SYNC_VS_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700817
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700818 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100819 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800820 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100821 intel_dp->DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700822
Chris Wilsonea5b2132010-08-04 13:50:23 +0100823 switch (intel_dp->lane_count) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700824 case 1:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100825 intel_dp->DP |= DP_PORT_WIDTH_1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700826 break;
827 case 2:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100828 intel_dp->DP |= DP_PORT_WIDTH_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700829 break;
830 case 4:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100831 intel_dp->DP |= DP_PORT_WIDTH_4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700832 break;
833 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100834 if (intel_dp->has_audio)
835 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700836
Chris Wilsonea5b2132010-08-04 13:50:23 +0100837 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
838 intel_dp->link_configuration[0] = intel_dp->link_bw;
839 intel_dp->link_configuration[1] = intel_dp->lane_count;
Adam Jacksona2cab1b2011-07-12 17:38:05 -0400840 intel_dp->link_configuration[8] = DP_SET_ANSI_8B10B;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700841
842 /*
Adam Jackson9962c922010-05-13 14:45:42 -0400843 * Check for DPCD version > 1.1 and enhanced framing support
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700844 */
Jesse Barnes7183dc22011-07-07 11:10:58 -0700845 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
846 (intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100847 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
848 intel_dp->DP |= DP_ENHANCED_FRAMING;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700849 }
850
Zhenyu Wange3421a12010-04-08 09:43:27 +0800851 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
852 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100853 intel_dp->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800854
Jesse Barnes895692b2010-10-07 16:01:23 -0700855 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800856 /* don't miss out required setting for eDP */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100857 intel_dp->DP |= DP_PLL_ENABLE;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800858 if (adjusted_mode->clock < 200000)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100859 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800860 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100861 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800862 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700863}
864
Jesse Barnes5d613502011-01-24 17:10:54 -0800865static void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp)
866{
867 struct drm_device *dev = intel_dp->base.base.dev;
868 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packardebf33b12011-09-29 15:53:27 -0700869 u32 pp;
Jesse Barnes5d613502011-01-24 17:10:54 -0800870
Keith Packard97af61f572011-09-28 16:23:51 -0700871 if (!is_edp(intel_dp))
872 return;
Keith Packardf01eca22011-09-28 16:48:10 -0700873 DRM_DEBUG_KMS("Turn eDP VDD on\n");
Jesse Barnes5d613502011-01-24 17:10:54 -0800874
875 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700876 pp &= ~PANEL_UNLOCK_MASK;
877 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes5d613502011-01-24 17:10:54 -0800878 pp |= EDP_FORCE_VDD;
879 I915_WRITE(PCH_PP_CONTROL, pp);
880 POSTING_READ(PCH_PP_CONTROL);
Keith Packardf01eca22011-09-28 16:48:10 -0700881 DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
882 I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
Keith Packardebf33b12011-09-29 15:53:27 -0700883
884 /*
885 * If the panel wasn't on, delay before accessing aux channel
886 */
887 if (!ironlake_edp_have_panel_power(intel_dp)) {
Keith Packardf01eca22011-09-28 16:48:10 -0700888 msleep(intel_dp->panel_power_up_delay);
889 DRM_DEBUG_KMS("eDP VDD was not on\n");
890 }
Jesse Barnes5d613502011-01-24 17:10:54 -0800891}
892
893static void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp)
894{
895 struct drm_device *dev = intel_dp->base.base.dev;
896 struct drm_i915_private *dev_priv = dev->dev_private;
897 u32 pp;
898
Keith Packard97af61f572011-09-28 16:23:51 -0700899 if (!is_edp(intel_dp))
900 return;
Keith Packardf01eca22011-09-28 16:48:10 -0700901 DRM_DEBUG_KMS("Turn eDP VDD off\n");
Jesse Barnes5d613502011-01-24 17:10:54 -0800902 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700903 pp &= ~PANEL_UNLOCK_MASK;
904 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes5d613502011-01-24 17:10:54 -0800905 pp &= ~EDP_FORCE_VDD;
906 I915_WRITE(PCH_PP_CONTROL, pp);
907 POSTING_READ(PCH_PP_CONTROL);
908
909 /* Make sure sequencer is idle before allowing subsequent activity */
Keith Packardf01eca22011-09-28 16:48:10 -0700910 DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
911 I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
912 msleep(intel_dp->panel_power_cycle_delay);
Jesse Barnes5d613502011-01-24 17:10:54 -0800913}
914
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700915/* Returns true if the panel was already on when called */
Keith Packard7d639f32011-09-29 16:05:34 -0700916static void ironlake_edp_panel_on (struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -0700917{
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700918 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes9934c132010-07-22 13:18:19 -0700919 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700920 u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_STATE_ON_IDLE;
Jesse Barnes9934c132010-07-22 13:18:19 -0700921
Keith Packard97af61f572011-09-28 16:23:51 -0700922 if (!is_edp(intel_dp))
Keith Packardf01eca22011-09-28 16:48:10 -0700923 return true;
Keith Packardebf33b12011-09-29 15:53:27 -0700924 if (ironlake_edp_have_panel_power(intel_dp))
Keith Packard7d639f32011-09-29 16:05:34 -0700925 return;
Jesse Barnes9934c132010-07-22 13:18:19 -0700926
927 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700928 pp &= ~PANEL_UNLOCK_MASK;
929 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700930
931 /* ILK workaround: disable reset around power sequence */
932 pp &= ~PANEL_POWER_RESET;
933 I915_WRITE(PCH_PP_CONTROL, pp);
934 POSTING_READ(PCH_PP_CONTROL);
935
Keith Packard1c0ae802011-09-19 13:59:29 -0700936 pp |= POWER_TARGET_ON;
Jesse Barnes9934c132010-07-22 13:18:19 -0700937 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700938 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700939
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700940 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_on_mask) == idle_on_mask,
941 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100942 DRM_ERROR("panel on wait timed out: 0x%08x\n",
943 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700944
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700945 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700946 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700947 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700948}
949
Keith Packardf01eca22011-09-28 16:48:10 -0700950static void ironlake_edp_panel_off(struct drm_encoder *encoder)
Jesse Barnes9934c132010-07-22 13:18:19 -0700951{
Keith Packardf01eca22011-09-28 16:48:10 -0700952 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
953 struct drm_device *dev = encoder->dev;
Jesse Barnes9934c132010-07-22 13:18:19 -0700954 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700955 u32 pp, idle_off_mask = PP_ON | PP_SEQUENCE_MASK |
956 PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK;
Jesse Barnes9934c132010-07-22 13:18:19 -0700957
Keith Packard97af61f572011-09-28 16:23:51 -0700958 if (!is_edp(intel_dp))
959 return;
Jesse Barnes9934c132010-07-22 13:18:19 -0700960 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -0700961 pp &= ~PANEL_UNLOCK_MASK;
962 pp |= PANEL_UNLOCK_REGS;
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700963
964 /* ILK workaround: disable reset around power sequence */
965 pp &= ~PANEL_POWER_RESET;
966 I915_WRITE(PCH_PP_CONTROL, pp);
967 POSTING_READ(PCH_PP_CONTROL);
968
Jesse Barnes9934c132010-07-22 13:18:19 -0700969 pp &= ~POWER_TARGET_ON;
970 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700971 POSTING_READ(PCH_PP_CONTROL);
Keith Packardf01eca22011-09-28 16:48:10 -0700972 msleep(intel_dp->panel_power_cycle_delay);
Jesse Barnes9934c132010-07-22 13:18:19 -0700973
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700974 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_off_mask) == 0, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100975 DRM_ERROR("panel off wait timed out: 0x%08x\n",
976 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700977
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700978 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700979 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700980 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700981}
982
Keith Packardf01eca22011-09-28 16:48:10 -0700983static void ironlake_edp_backlight_on (struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800984{
Keith Packardf01eca22011-09-28 16:48:10 -0700985 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800986 struct drm_i915_private *dev_priv = dev->dev_private;
987 u32 pp;
988
Keith Packardf01eca22011-09-28 16:48:10 -0700989 if (!is_edp(intel_dp))
990 return;
991
Zhao Yakui28c97732009-10-09 11:39:41 +0800992 DRM_DEBUG_KMS("\n");
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700993 /*
994 * If we enable the backlight right away following a panel power
995 * on, we may see slight flicker as the panel syncs with the eDP
996 * link. So delay a bit to make sure the image is solid before
997 * allowing it to appear.
998 */
Keith Packardf01eca22011-09-28 16:48:10 -0700999 msleep(intel_dp->backlight_on_delay);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001000 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -07001001 pp &= ~PANEL_UNLOCK_MASK;
1002 pp |= PANEL_UNLOCK_REGS;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001003 pp |= EDP_BLC_ENABLE;
1004 I915_WRITE(PCH_PP_CONTROL, pp);
Keith Packardf01eca22011-09-28 16:48:10 -07001005 POSTING_READ(PCH_PP_CONTROL);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001006}
1007
Keith Packardf01eca22011-09-28 16:48:10 -07001008static void ironlake_edp_backlight_off (struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001009{
Keith Packardf01eca22011-09-28 16:48:10 -07001010 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001011 struct drm_i915_private *dev_priv = dev->dev_private;
1012 u32 pp;
1013
Keith Packardf01eca22011-09-28 16:48:10 -07001014 if (!is_edp(intel_dp))
1015 return;
1016
Zhao Yakui28c97732009-10-09 11:39:41 +08001017 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001018 pp = I915_READ(PCH_PP_CONTROL);
Keith Packard1c0ae802011-09-19 13:59:29 -07001019 pp &= ~PANEL_UNLOCK_MASK;
1020 pp |= PANEL_UNLOCK_REGS;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001021 pp &= ~EDP_BLC_ENABLE;
1022 I915_WRITE(PCH_PP_CONTROL, pp);
Keith Packardf01eca22011-09-28 16:48:10 -07001023 POSTING_READ(PCH_PP_CONTROL);
1024 msleep(intel_dp->backlight_off_delay);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001025}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001026
Jesse Barnesd240f202010-08-13 15:43:26 -07001027static void ironlake_edp_pll_on(struct drm_encoder *encoder)
1028{
1029 struct drm_device *dev = encoder->dev;
1030 struct drm_i915_private *dev_priv = dev->dev_private;
1031 u32 dpa_ctl;
1032
1033 DRM_DEBUG_KMS("\n");
1034 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -07001035 dpa_ctl |= DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -07001036 I915_WRITE(DP_A, dpa_ctl);
Jesse Barnes298b0b32010-10-07 16:01:24 -07001037 POSTING_READ(DP_A);
1038 udelay(200);
Jesse Barnesd240f202010-08-13 15:43:26 -07001039}
1040
1041static void ironlake_edp_pll_off(struct drm_encoder *encoder)
1042{
1043 struct drm_device *dev = encoder->dev;
1044 struct drm_i915_private *dev_priv = dev->dev_private;
1045 u32 dpa_ctl;
1046
1047 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -07001048 dpa_ctl &= ~DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -07001049 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +01001050 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -07001051 udelay(200);
1052}
1053
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001054/* If the sink supports it, try to set the power state appropriately */
1055static void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
1056{
1057 int ret, i;
1058
1059 /* Should have a valid DPCD by this point */
1060 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
1061 return;
1062
1063 if (mode != DRM_MODE_DPMS_ON) {
1064 ret = intel_dp_aux_native_write_1(intel_dp, DP_SET_POWER,
1065 DP_SET_POWER_D3);
1066 if (ret != 1)
1067 DRM_DEBUG_DRIVER("failed to write sink power state\n");
1068 } else {
1069 /*
1070 * When turning on, we need to retry for 1ms to give the sink
1071 * time to wake up.
1072 */
1073 for (i = 0; i < 3; i++) {
1074 ret = intel_dp_aux_native_write_1(intel_dp,
1075 DP_SET_POWER,
1076 DP_SET_POWER_D0);
1077 if (ret == 1)
1078 break;
1079 msleep(1);
1080 }
1081 }
1082}
1083
Jesse Barnesd240f202010-08-13 15:43:26 -07001084static void intel_dp_prepare(struct drm_encoder *encoder)
1085{
1086 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -07001087
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001088 /* Wake up the sink first */
Keith Packardf58ff852011-09-28 16:44:14 -07001089 ironlake_edp_panel_vdd_on(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001090 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
Keith Packardf58ff852011-09-28 16:44:14 -07001091 ironlake_edp_panel_vdd_off(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001092
Keith Packardf01eca22011-09-28 16:48:10 -07001093 /* Make sure the panel is off before trying to
1094 * change the mode
1095 */
1096 ironlake_edp_backlight_off(intel_dp);
Jesse Barnes736085b2010-10-08 10:35:55 -07001097 intel_dp_link_down(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001098 ironlake_edp_panel_off(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -07001099}
1100
1101static void intel_dp_commit(struct drm_encoder *encoder)
1102{
1103 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -07001104
Keith Packard97af61f572011-09-28 16:23:51 -07001105 ironlake_edp_panel_vdd_on(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001106 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001107 intel_dp_start_link_train(intel_dp);
Keith Packard97af61f572011-09-28 16:23:51 -07001108 ironlake_edp_panel_on(intel_dp);
1109 ironlake_edp_panel_vdd_off(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001110 intel_dp_complete_link_train(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001111 ironlake_edp_backlight_on(intel_dp);
Keith Packardd2b996a2011-07-25 22:37:51 -07001112
1113 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
Jesse Barnesd240f202010-08-13 15:43:26 -07001114}
1115
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001116static void
1117intel_dp_dpms(struct drm_encoder *encoder, int mode)
1118{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001119 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001120 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001121 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001122 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001123
1124 if (mode != DRM_MODE_DPMS_ON) {
Keith Packard245e2702011-10-05 19:53:09 -07001125 ironlake_edp_panel_vdd_on(intel_dp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001126 if (is_edp(intel_dp))
Keith Packardf01eca22011-09-28 16:48:10 -07001127 ironlake_edp_backlight_off(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001128 intel_dp_sink_dpms(intel_dp, mode);
Jesse Barnes736085b2010-10-08 10:35:55 -07001129 intel_dp_link_down(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001130 ironlake_edp_panel_off(encoder);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001131 if (is_edp(intel_dp) && !is_pch_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -07001132 ironlake_edp_pll_off(encoder);
Keith Packard245e2702011-10-05 19:53:09 -07001133 ironlake_edp_panel_vdd_off(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001134 } else {
Keith Packard97af61f572011-09-28 16:23:51 -07001135 ironlake_edp_panel_vdd_on(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001136 intel_dp_sink_dpms(intel_dp, mode);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001137 if (!(dp_reg & DP_PORT_EN)) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001138 intel_dp_start_link_train(intel_dp);
Keith Packard97af61f572011-09-28 16:23:51 -07001139 ironlake_edp_panel_on(intel_dp);
1140 ironlake_edp_panel_vdd_off(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001141 intel_dp_complete_link_train(intel_dp);
Keith Packardf01eca22011-09-28 16:48:10 -07001142 ironlake_edp_backlight_on(intel_dp);
Keith Packardbee7eb22011-09-28 16:28:00 -07001143 } else
1144 ironlake_edp_panel_vdd_off(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001145 }
Keith Packardd2b996a2011-07-25 22:37:51 -07001146 intel_dp->dpms_mode = mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001147}
1148
1149/*
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001150 * Native read with retry for link status and receiver capability reads for
1151 * cases where the sink may still be asleep.
1152 */
1153static bool
1154intel_dp_aux_native_read_retry(struct intel_dp *intel_dp, uint16_t address,
1155 uint8_t *recv, int recv_bytes)
1156{
1157 int ret, i;
1158
1159 /*
1160 * Sinks are *supposed* to come up within 1ms from an off state,
1161 * but we're also supposed to retry 3 times per the spec.
1162 */
1163 for (i = 0; i < 3; i++) {
1164 ret = intel_dp_aux_native_read(intel_dp, address, recv,
1165 recv_bytes);
1166 if (ret == recv_bytes)
1167 return true;
1168 msleep(1);
1169 }
1170
1171 return false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001172}
1173
1174/*
1175 * Fetch AUX CH registers 0x202 - 0x207 which contain
1176 * link status information
1177 */
1178static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001179intel_dp_get_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001180{
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001181 return intel_dp_aux_native_read_retry(intel_dp,
1182 DP_LANE0_1_STATUS,
1183 intel_dp->link_status,
1184 DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001185}
1186
1187static uint8_t
1188intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1189 int r)
1190{
1191 return link_status[r - DP_LANE0_1_STATUS];
1192}
1193
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001194static uint8_t
1195intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
1196 int lane)
1197{
1198 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1199 int s = ((lane & 1) ?
1200 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1201 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1202 uint8_t l = intel_dp_link_status(link_status, i);
1203
1204 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1205}
1206
1207static uint8_t
1208intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1209 int lane)
1210{
1211 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1212 int s = ((lane & 1) ?
1213 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1214 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1215 uint8_t l = intel_dp_link_status(link_status, i);
1216
1217 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1218}
1219
1220
1221#if 0
1222static char *voltage_names[] = {
1223 "0.4V", "0.6V", "0.8V", "1.2V"
1224};
1225static char *pre_emph_names[] = {
1226 "0dB", "3.5dB", "6dB", "9.5dB"
1227};
1228static char *link_train_names[] = {
1229 "pattern 1", "pattern 2", "idle", "off"
1230};
1231#endif
1232
1233/*
1234 * These are source-specific values; current Intel hardware supports
1235 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1236 */
1237#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1238
1239static uint8_t
1240intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1241{
1242 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1243 case DP_TRAIN_VOLTAGE_SWING_400:
1244 return DP_TRAIN_PRE_EMPHASIS_6;
1245 case DP_TRAIN_VOLTAGE_SWING_600:
1246 return DP_TRAIN_PRE_EMPHASIS_6;
1247 case DP_TRAIN_VOLTAGE_SWING_800:
1248 return DP_TRAIN_PRE_EMPHASIS_3_5;
1249 case DP_TRAIN_VOLTAGE_SWING_1200:
1250 default:
1251 return DP_TRAIN_PRE_EMPHASIS_0;
1252 }
1253}
1254
1255static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001256intel_get_adjust_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001257{
1258 uint8_t v = 0;
1259 uint8_t p = 0;
1260 int lane;
1261
Jesse Barnes33a34e42010-09-08 12:42:02 -07001262 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1263 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1264 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001265
1266 if (this_v > v)
1267 v = this_v;
1268 if (this_p > p)
1269 p = this_p;
1270 }
1271
1272 if (v >= I830_DP_VOLTAGE_MAX)
1273 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1274
1275 if (p >= intel_dp_pre_emphasis_max(v))
1276 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1277
1278 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001279 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001280}
1281
1282static uint32_t
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001283intel_dp_signal_levels(uint8_t train_set, int lane_count)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001284{
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001285 uint32_t signal_levels = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001286
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001287 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001288 case DP_TRAIN_VOLTAGE_SWING_400:
1289 default:
1290 signal_levels |= DP_VOLTAGE_0_4;
1291 break;
1292 case DP_TRAIN_VOLTAGE_SWING_600:
1293 signal_levels |= DP_VOLTAGE_0_6;
1294 break;
1295 case DP_TRAIN_VOLTAGE_SWING_800:
1296 signal_levels |= DP_VOLTAGE_0_8;
1297 break;
1298 case DP_TRAIN_VOLTAGE_SWING_1200:
1299 signal_levels |= DP_VOLTAGE_1_2;
1300 break;
1301 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001302 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001303 case DP_TRAIN_PRE_EMPHASIS_0:
1304 default:
1305 signal_levels |= DP_PRE_EMPHASIS_0;
1306 break;
1307 case DP_TRAIN_PRE_EMPHASIS_3_5:
1308 signal_levels |= DP_PRE_EMPHASIS_3_5;
1309 break;
1310 case DP_TRAIN_PRE_EMPHASIS_6:
1311 signal_levels |= DP_PRE_EMPHASIS_6;
1312 break;
1313 case DP_TRAIN_PRE_EMPHASIS_9_5:
1314 signal_levels |= DP_PRE_EMPHASIS_9_5;
1315 break;
1316 }
1317 return signal_levels;
1318}
1319
Zhenyu Wange3421a12010-04-08 09:43:27 +08001320/* Gen6's DP voltage swing and pre-emphasis control */
1321static uint32_t
1322intel_gen6_edp_signal_levels(uint8_t train_set)
1323{
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001324 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1325 DP_TRAIN_PRE_EMPHASIS_MASK);
1326 switch (signal_levels) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001327 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001328 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
1329 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
1330 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
1331 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001332 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001333 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
1334 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001335 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001336 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
1337 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001338 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001339 case DP_TRAIN_VOLTAGE_SWING_1200 | DP_TRAIN_PRE_EMPHASIS_0:
1340 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001341 default:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001342 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
1343 "0x%x\n", signal_levels);
1344 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001345 }
1346}
1347
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001348static uint8_t
1349intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1350 int lane)
1351{
1352 int i = DP_LANE0_1_STATUS + (lane >> 1);
1353 int s = (lane & 1) * 4;
1354 uint8_t l = intel_dp_link_status(link_status, i);
1355
1356 return (l >> s) & 0xf;
1357}
1358
1359/* Check for clock recovery is done on all channels */
1360static bool
1361intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1362{
1363 int lane;
1364 uint8_t lane_status;
1365
1366 for (lane = 0; lane < lane_count; lane++) {
1367 lane_status = intel_get_lane_status(link_status, lane);
1368 if ((lane_status & DP_LANE_CR_DONE) == 0)
1369 return false;
1370 }
1371 return true;
1372}
1373
1374/* Check to see if channel eq is done on all channels */
1375#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1376 DP_LANE_CHANNEL_EQ_DONE|\
1377 DP_LANE_SYMBOL_LOCKED)
1378static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001379intel_channel_eq_ok(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001380{
1381 uint8_t lane_align;
1382 uint8_t lane_status;
1383 int lane;
1384
Jesse Barnes33a34e42010-09-08 12:42:02 -07001385 lane_align = intel_dp_link_status(intel_dp->link_status,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001386 DP_LANE_ALIGN_STATUS_UPDATED);
1387 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1388 return false;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001389 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1390 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001391 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1392 return false;
1393 }
1394 return true;
1395}
1396
1397static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001398intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001399 uint32_t dp_reg_value,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001400 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001401{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001402 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001403 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001404 int ret;
1405
Chris Wilsonea5b2132010-08-04 13:50:23 +01001406 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1407 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001408
Chris Wilsonea5b2132010-08-04 13:50:23 +01001409 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001410 DP_TRAINING_PATTERN_SET,
1411 dp_train_pat);
1412
Chris Wilsonea5b2132010-08-04 13:50:23 +01001413 ret = intel_dp_aux_native_write(intel_dp,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001414 DP_TRAINING_LANE0_SET,
1415 intel_dp->train_set, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001416 if (ret != 4)
1417 return false;
1418
1419 return true;
1420}
1421
Jesse Barnes33a34e42010-09-08 12:42:02 -07001422/* Enable corresponding port and start training pattern 1 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001423static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001424intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001425{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001426 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001427 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson58e10eb2010-10-03 10:56:11 +01001428 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001429 int i;
1430 uint8_t voltage;
1431 bool clock_recovery = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001432 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001433 u32 reg;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001434 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001435
Adam Jacksone8519462011-07-21 17:48:38 -04001436 /*
1437 * On CPT we have to enable the port in training pattern 1, which
1438 * will happen below in intel_dp_set_link_train. Otherwise, enable
1439 * the port and wait for it to become active.
1440 */
1441 if (!HAS_PCH_CPT(dev)) {
1442 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1443 POSTING_READ(intel_dp->output_reg);
1444 intel_wait_for_vblank(dev, intel_crtc->pipe);
1445 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001446
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001447 /* Write the link configuration data */
1448 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1449 intel_dp->link_configuration,
1450 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001451
1452 DP |= DP_PORT_EN;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001453 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001454 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1455 else
1456 DP &= ~DP_LINK_TRAIN_MASK;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001457 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001458 voltage = 0xff;
1459 tries = 0;
1460 clock_recovery = false;
1461 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001462 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001463 uint32_t signal_levels;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001464 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001465 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001466 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1467 } else {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001468 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001469 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1470 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001471
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001472 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001473 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1474 else
1475 reg = DP | DP_LINK_TRAIN_PAT_1;
1476
Chris Wilsonea5b2132010-08-04 13:50:23 +01001477 if (!intel_dp_set_link_train(intel_dp, reg,
Adam Jackson81055852011-07-21 17:48:37 -04001478 DP_TRAINING_PATTERN_1 |
1479 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001480 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001481 /* Set training pattern 1 */
1482
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001483 udelay(100);
1484 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001485 break;
1486
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001487 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1488 clock_recovery = true;
1489 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001490 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001491
1492 /* Check to see if we've tried the max voltage */
1493 for (i = 0; i < intel_dp->lane_count; i++)
1494 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1495 break;
1496 if (i == intel_dp->lane_count)
1497 break;
1498
1499 /* Check to see if we've tried the same voltage 5 times */
1500 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1501 ++tries;
1502 if (tries == 5)
1503 break;
1504 } else
1505 tries = 0;
1506 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1507
1508 /* Compute new intel_dp->train_set as requested by target */
1509 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001510 }
1511
Jesse Barnes33a34e42010-09-08 12:42:02 -07001512 intel_dp->DP = DP;
1513}
1514
1515static void
1516intel_dp_complete_link_train(struct intel_dp *intel_dp)
1517{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001518 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001519 struct drm_i915_private *dev_priv = dev->dev_private;
1520 bool channel_eq = false;
Jesse Barnes37f80972011-01-05 14:45:24 -08001521 int tries, cr_tries;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001522 u32 reg;
1523 uint32_t DP = intel_dp->DP;
1524
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001525 /* channel equalization */
1526 tries = 0;
Jesse Barnes37f80972011-01-05 14:45:24 -08001527 cr_tries = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001528 channel_eq = false;
1529 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001530 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001531 uint32_t signal_levels;
1532
Jesse Barnes37f80972011-01-05 14:45:24 -08001533 if (cr_tries > 5) {
1534 DRM_ERROR("failed to train DP, aborting\n");
1535 intel_dp_link_down(intel_dp);
1536 break;
1537 }
1538
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001539 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001540 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001541 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1542 } else {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001543 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001544 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1545 }
1546
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001547 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001548 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1549 else
1550 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001551
1552 /* channel eq pattern */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001553 if (!intel_dp_set_link_train(intel_dp, reg,
Adam Jackson81055852011-07-21 17:48:37 -04001554 DP_TRAINING_PATTERN_2 |
1555 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001556 break;
1557
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001558 udelay(400);
1559 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001560 break;
Jesse Barnes869184a2010-10-07 16:01:22 -07001561
Jesse Barnes37f80972011-01-05 14:45:24 -08001562 /* Make sure clock is still ok */
1563 if (!intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1564 intel_dp_start_link_train(intel_dp);
1565 cr_tries++;
1566 continue;
1567 }
1568
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001569 if (intel_channel_eq_ok(intel_dp)) {
1570 channel_eq = true;
1571 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001572 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001573
Jesse Barnes37f80972011-01-05 14:45:24 -08001574 /* Try 5 times, then try clock recovery if that fails */
1575 if (tries > 5) {
1576 intel_dp_link_down(intel_dp);
1577 intel_dp_start_link_train(intel_dp);
1578 tries = 0;
1579 cr_tries++;
1580 continue;
1581 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001582
1583 /* Compute new intel_dp->train_set as requested by target */
1584 intel_get_adjust_train(intel_dp);
1585 ++tries;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001586 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001587
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001588 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001589 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1590 else
1591 reg = DP | DP_LINK_TRAIN_OFF;
1592
Chris Wilsonea5b2132010-08-04 13:50:23 +01001593 I915_WRITE(intel_dp->output_reg, reg);
1594 POSTING_READ(intel_dp->output_reg);
1595 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001596 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1597}
1598
1599static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001600intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001601{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001602 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001603 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001604 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001605
Chris Wilson1b39d6f2010-12-06 11:20:45 +00001606 if ((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0)
1607 return;
1608
Zhao Yakui28c97732009-10-09 11:39:41 +08001609 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001610
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001611 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001612 DP &= ~DP_PLL_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001613 I915_WRITE(intel_dp->output_reg, DP);
1614 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001615 udelay(100);
1616 }
1617
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001618 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001619 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001620 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001621 } else {
1622 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001623 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001624 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01001625 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001626
Chris Wilsonfe255d02010-09-11 21:37:48 +01001627 msleep(17);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001628
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001629 if (is_edp(intel_dp))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001630 DP |= DP_LINK_TRAIN_OFF;
Eric Anholt5bddd172010-11-18 09:32:59 +08001631
Chris Wilson1b39d6f2010-12-06 11:20:45 +00001632 if (!HAS_PCH_CPT(dev) &&
1633 I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
Chris Wilson31acbcc2011-04-17 06:38:35 +01001634 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1635
Eric Anholt5bddd172010-11-18 09:32:59 +08001636 /* Hardware workaround: leaving our transcoder select
1637 * set to transcoder B while it's off will prevent the
1638 * corresponding HDMI output on transcoder A.
1639 *
1640 * Combine this with another hardware workaround:
1641 * transcoder select bit can only be cleared while the
1642 * port is enabled.
1643 */
1644 DP &= ~DP_PIPEB_SELECT;
1645 I915_WRITE(intel_dp->output_reg, DP);
1646
1647 /* Changes to enable or select take place the vblank
1648 * after being written.
1649 */
Chris Wilson31acbcc2011-04-17 06:38:35 +01001650 if (crtc == NULL) {
1651 /* We can arrive here never having been attached
1652 * to a CRTC, for instance, due to inheriting
1653 * random state from the BIOS.
1654 *
1655 * If the pipe is not running, play safe and
1656 * wait for the clocks to stabilise before
1657 * continuing.
1658 */
1659 POSTING_READ(intel_dp->output_reg);
1660 msleep(50);
1661 } else
1662 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
Eric Anholt5bddd172010-11-18 09:32:59 +08001663 }
1664
Chris Wilsonea5b2132010-08-04 13:50:23 +01001665 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1666 POSTING_READ(intel_dp->output_reg);
Keith Packardf01eca22011-09-28 16:48:10 -07001667 msleep(intel_dp->panel_power_down_delay);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001668}
1669
Keith Packard26d61aa2011-07-25 20:01:09 -07001670static bool
1671intel_dp_get_dpcd(struct intel_dp *intel_dp)
Keith Packard92fd8fd2011-07-25 19:50:10 -07001672{
Keith Packard92fd8fd2011-07-25 19:50:10 -07001673 if (intel_dp_aux_native_read_retry(intel_dp, 0x000, intel_dp->dpcd,
1674 sizeof (intel_dp->dpcd)) &&
1675 (intel_dp->dpcd[DP_DPCD_REV] != 0)) {
Keith Packard26d61aa2011-07-25 20:01:09 -07001676 return true;
Keith Packard92fd8fd2011-07-25 19:50:10 -07001677 }
1678
Keith Packard26d61aa2011-07-25 20:01:09 -07001679 return false;
Keith Packard92fd8fd2011-07-25 19:50:10 -07001680}
1681
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001682/*
1683 * According to DP spec
1684 * 5.1.2:
1685 * 1. Read DPCD
1686 * 2. Configure link according to Receiver Capabilities
1687 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1688 * 4. Check link status on receipt of hot-plug interrupt
1689 */
1690
1691static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001692intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001693{
Keith Packardd2b996a2011-07-25 22:37:51 -07001694 if (intel_dp->dpms_mode != DRM_MODE_DPMS_ON)
1695 return;
Jesse Barnes59cd09e2011-07-07 11:10:59 -07001696
Chris Wilson4ef69c72010-09-09 15:14:28 +01001697 if (!intel_dp->base.base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001698 return;
1699
Keith Packard92fd8fd2011-07-25 19:50:10 -07001700 /* Try to read receiver status if the link appears to be up */
Jesse Barnes33a34e42010-09-08 12:42:02 -07001701 if (!intel_dp_get_link_status(intel_dp)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001702 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001703 return;
1704 }
1705
Keith Packard92fd8fd2011-07-25 19:50:10 -07001706 /* Now read the DPCD to see if it's actually running */
Keith Packard26d61aa2011-07-25 20:01:09 -07001707 if (!intel_dp_get_dpcd(intel_dp)) {
Jesse Barnes59cd09e2011-07-07 11:10:59 -07001708 intel_dp_link_down(intel_dp);
1709 return;
1710 }
1711
Jesse Barnes33a34e42010-09-08 12:42:02 -07001712 if (!intel_channel_eq_ok(intel_dp)) {
Keith Packard92fd8fd2011-07-25 19:50:10 -07001713 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
1714 drm_get_encoder_name(&intel_dp->base.base));
Jesse Barnes33a34e42010-09-08 12:42:02 -07001715 intel_dp_start_link_train(intel_dp);
1716 intel_dp_complete_link_train(intel_dp);
1717 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001718}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001719
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001720static enum drm_connector_status
Keith Packard26d61aa2011-07-25 20:01:09 -07001721intel_dp_detect_dpcd(struct intel_dp *intel_dp)
Adam Jackson71ba9002011-07-12 17:38:04 -04001722{
Keith Packard26d61aa2011-07-25 20:01:09 -07001723 if (intel_dp_get_dpcd(intel_dp))
1724 return connector_status_connected;
1725 return connector_status_disconnected;
Adam Jackson71ba9002011-07-12 17:38:04 -04001726}
1727
1728static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001729ironlake_dp_detect(struct intel_dp *intel_dp)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001730{
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001731 enum drm_connector_status status;
1732
Chris Wilsonfe16d942011-02-12 10:29:38 +00001733 /* Can't disconnect eDP, but you can close the lid... */
1734 if (is_edp(intel_dp)) {
1735 status = intel_panel_detect(intel_dp->base.base.dev);
1736 if (status == connector_status_unknown)
1737 status = connector_status_connected;
1738 return status;
1739 }
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001740
Keith Packard26d61aa2011-07-25 20:01:09 -07001741 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001742}
1743
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001744static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001745g4x_dp_detect(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001746{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001747 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001748 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001749 uint32_t temp, bit;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001750
Chris Wilsonea5b2132010-08-04 13:50:23 +01001751 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001752 case DP_B:
1753 bit = DPB_HOTPLUG_INT_STATUS;
1754 break;
1755 case DP_C:
1756 bit = DPC_HOTPLUG_INT_STATUS;
1757 break;
1758 case DP_D:
1759 bit = DPD_HOTPLUG_INT_STATUS;
1760 break;
1761 default:
1762 return connector_status_unknown;
1763 }
1764
1765 temp = I915_READ(PORT_HOTPLUG_STAT);
1766
1767 if ((temp & bit) == 0)
1768 return connector_status_disconnected;
1769
Keith Packard26d61aa2011-07-25 20:01:09 -07001770 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001771}
1772
Keith Packard8c241fe2011-09-28 16:38:44 -07001773static struct edid *
1774intel_dp_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
1775{
1776 struct intel_dp *intel_dp = intel_attached_dp(connector);
1777 struct edid *edid;
1778
1779 ironlake_edp_panel_vdd_on(intel_dp);
1780 edid = drm_get_edid(connector, adapter);
1781 ironlake_edp_panel_vdd_off(intel_dp);
1782 return edid;
1783}
1784
1785static int
1786intel_dp_get_edid_modes(struct drm_connector *connector, struct i2c_adapter *adapter)
1787{
1788 struct intel_dp *intel_dp = intel_attached_dp(connector);
1789 int ret;
1790
1791 ironlake_edp_panel_vdd_on(intel_dp);
1792 ret = intel_ddc_get_modes(connector, adapter);
1793 ironlake_edp_panel_vdd_off(intel_dp);
1794 return ret;
1795}
1796
1797
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001798/**
1799 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1800 *
1801 * \return true if DP port is connected.
1802 * \return false if DP port is disconnected.
1803 */
1804static enum drm_connector_status
1805intel_dp_detect(struct drm_connector *connector, bool force)
1806{
1807 struct intel_dp *intel_dp = intel_attached_dp(connector);
1808 struct drm_device *dev = intel_dp->base.base.dev;
1809 enum drm_connector_status status;
1810 struct edid *edid = NULL;
1811
1812 intel_dp->has_audio = false;
1813
1814 if (HAS_PCH_SPLIT(dev))
1815 status = ironlake_dp_detect(intel_dp);
1816 else
1817 status = g4x_dp_detect(intel_dp);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04001818
Adam Jacksonac66ae82011-07-12 17:38:03 -04001819 DRM_DEBUG_KMS("DPCD: %02hx%02hx%02hx%02hx%02hx%02hx%02hx%02hx\n",
1820 intel_dp->dpcd[0], intel_dp->dpcd[1], intel_dp->dpcd[2],
1821 intel_dp->dpcd[3], intel_dp->dpcd[4], intel_dp->dpcd[5],
1822 intel_dp->dpcd[6], intel_dp->dpcd[7]);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04001823
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001824 if (status != connector_status_connected)
1825 return status;
1826
Chris Wilsonf6849602010-09-19 09:29:33 +01001827 if (intel_dp->force_audio) {
1828 intel_dp->has_audio = intel_dp->force_audio > 0;
1829 } else {
Keith Packard8c241fe2011-09-28 16:38:44 -07001830 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
Chris Wilsonf6849602010-09-19 09:29:33 +01001831 if (edid) {
1832 intel_dp->has_audio = drm_detect_monitor_audio(edid);
1833 connector->display_info.raw_edid = NULL;
1834 kfree(edid);
1835 }
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001836 }
1837
1838 return connector_status_connected;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001839}
1840
1841static int intel_dp_get_modes(struct drm_connector *connector)
1842{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001843 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001844 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001845 struct drm_i915_private *dev_priv = dev->dev_private;
1846 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001847
1848 /* We should parse the EDID data and find out if it has an audio sink
1849 */
1850
Keith Packard8c241fe2011-09-28 16:38:44 -07001851 ret = intel_dp_get_edid_modes(connector, &intel_dp->adapter);
Zhao Yakuib9efc482010-07-19 09:43:11 +01001852 if (ret) {
Keith Packardd15456d2011-09-18 17:35:47 -07001853 if (is_edp(intel_dp) && !intel_dp->panel_fixed_mode) {
Zhao Yakuib9efc482010-07-19 09:43:11 +01001854 struct drm_display_mode *newmode;
1855 list_for_each_entry(newmode, &connector->probed_modes,
1856 head) {
Keith Packardd15456d2011-09-18 17:35:47 -07001857 if ((newmode->type & DRM_MODE_TYPE_PREFERRED)) {
1858 intel_dp->panel_fixed_mode =
Zhao Yakuib9efc482010-07-19 09:43:11 +01001859 drm_mode_duplicate(dev, newmode);
1860 break;
1861 }
1862 }
1863 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001864 return ret;
Zhao Yakuib9efc482010-07-19 09:43:11 +01001865 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001866
1867 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Jesse Barnes4d926462010-10-07 16:01:07 -07001868 if (is_edp(intel_dp)) {
Keith Packard47f0eb22011-09-19 14:33:26 -07001869 /* initialize panel mode from VBT if available for eDP */
Keith Packardd15456d2011-09-18 17:35:47 -07001870 if (intel_dp->panel_fixed_mode == NULL && dev_priv->lfp_lvds_vbt_mode != NULL) {
1871 intel_dp->panel_fixed_mode =
Keith Packard47f0eb22011-09-19 14:33:26 -07001872 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
Keith Packardd15456d2011-09-18 17:35:47 -07001873 if (intel_dp->panel_fixed_mode) {
1874 intel_dp->panel_fixed_mode->type |=
Keith Packard47f0eb22011-09-19 14:33:26 -07001875 DRM_MODE_TYPE_PREFERRED;
1876 }
1877 }
Keith Packardd15456d2011-09-18 17:35:47 -07001878 if (intel_dp->panel_fixed_mode) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001879 struct drm_display_mode *mode;
Keith Packardd15456d2011-09-18 17:35:47 -07001880 mode = drm_mode_duplicate(dev, intel_dp->panel_fixed_mode);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001881 drm_mode_probed_add(connector, mode);
1882 return 1;
1883 }
1884 }
1885 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001886}
1887
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001888static bool
1889intel_dp_detect_audio(struct drm_connector *connector)
1890{
1891 struct intel_dp *intel_dp = intel_attached_dp(connector);
1892 struct edid *edid;
1893 bool has_audio = false;
1894
Keith Packard8c241fe2011-09-28 16:38:44 -07001895 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001896 if (edid) {
1897 has_audio = drm_detect_monitor_audio(edid);
1898
1899 connector->display_info.raw_edid = NULL;
1900 kfree(edid);
1901 }
1902
1903 return has_audio;
1904}
1905
Chris Wilsonf6849602010-09-19 09:29:33 +01001906static int
1907intel_dp_set_property(struct drm_connector *connector,
1908 struct drm_property *property,
1909 uint64_t val)
1910{
Chris Wilsone953fd72011-02-21 22:23:52 +00001911 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Chris Wilsonf6849602010-09-19 09:29:33 +01001912 struct intel_dp *intel_dp = intel_attached_dp(connector);
1913 int ret;
1914
1915 ret = drm_connector_property_set_value(connector, property, val);
1916 if (ret)
1917 return ret;
1918
Chris Wilson3f43c482011-05-12 22:17:24 +01001919 if (property == dev_priv->force_audio_property) {
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001920 int i = val;
1921 bool has_audio;
1922
1923 if (i == intel_dp->force_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01001924 return 0;
1925
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001926 intel_dp->force_audio = i;
Chris Wilsonf6849602010-09-19 09:29:33 +01001927
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001928 if (i == 0)
1929 has_audio = intel_dp_detect_audio(connector);
1930 else
1931 has_audio = i > 0;
1932
1933 if (has_audio == intel_dp->has_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01001934 return 0;
1935
Chris Wilson1aad7ac2011-02-09 18:46:58 +00001936 intel_dp->has_audio = has_audio;
Chris Wilsonf6849602010-09-19 09:29:33 +01001937 goto done;
1938 }
1939
Chris Wilsone953fd72011-02-21 22:23:52 +00001940 if (property == dev_priv->broadcast_rgb_property) {
1941 if (val == !!intel_dp->color_range)
1942 return 0;
1943
1944 intel_dp->color_range = val ? DP_COLOR_RANGE_16_235 : 0;
1945 goto done;
1946 }
1947
Chris Wilsonf6849602010-09-19 09:29:33 +01001948 return -EINVAL;
1949
1950done:
1951 if (intel_dp->base.base.crtc) {
1952 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1953 drm_crtc_helper_set_mode(crtc, &crtc->mode,
1954 crtc->x, crtc->y,
1955 crtc->fb);
1956 }
1957
1958 return 0;
1959}
1960
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001961static void
1962intel_dp_destroy (struct drm_connector *connector)
1963{
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001964 struct drm_device *dev = connector->dev;
1965
1966 if (intel_dpd_is_edp(dev))
1967 intel_panel_destroy_backlight(dev);
1968
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001969 drm_sysfs_connector_remove(connector);
1970 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001971 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001972}
1973
Daniel Vetter24d05922010-08-20 18:08:28 +02001974static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1975{
1976 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1977
1978 i2c_del_adapter(&intel_dp->adapter);
1979 drm_encoder_cleanup(encoder);
1980 kfree(intel_dp);
1981}
1982
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001983static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1984 .dpms = intel_dp_dpms,
1985 .mode_fixup = intel_dp_mode_fixup,
Jesse Barnesd240f202010-08-13 15:43:26 -07001986 .prepare = intel_dp_prepare,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001987 .mode_set = intel_dp_mode_set,
Jesse Barnesd240f202010-08-13 15:43:26 -07001988 .commit = intel_dp_commit,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001989};
1990
1991static const struct drm_connector_funcs intel_dp_connector_funcs = {
1992 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001993 .detect = intel_dp_detect,
1994 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilsonf6849602010-09-19 09:29:33 +01001995 .set_property = intel_dp_set_property,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001996 .destroy = intel_dp_destroy,
1997};
1998
1999static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
2000 .get_modes = intel_dp_get_modes,
2001 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01002002 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002003};
2004
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002005static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02002006 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002007};
2008
Chris Wilson995b6762010-08-20 13:23:26 +01002009static void
Eric Anholt21d40d32010-03-25 11:11:14 -07002010intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07002011{
Chris Wilsonea5b2132010-08-04 13:50:23 +01002012 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Keith Packardc8110e52009-05-06 11:51:10 -07002013
Jesse Barnes885a5012011-07-07 11:11:01 -07002014 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07002015}
2016
Zhenyu Wange3421a12010-04-08 09:43:27 +08002017/* Return which DP Port should be selected for Transcoder DP control */
2018int
2019intel_trans_dp_port_sel (struct drm_crtc *crtc)
2020{
2021 struct drm_device *dev = crtc->dev;
2022 struct drm_mode_config *mode_config = &dev->mode_config;
2023 struct drm_encoder *encoder;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002024
2025 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002026 struct intel_dp *intel_dp;
2027
Dan Carpenterd8201ab2010-05-07 10:39:00 +02002028 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08002029 continue;
2030
Chris Wilsonea5b2132010-08-04 13:50:23 +01002031 intel_dp = enc_to_intel_dp(encoder);
2032 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
2033 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002034 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01002035
Zhenyu Wange3421a12010-04-08 09:43:27 +08002036 return -1;
2037}
2038
Zhao Yakui36e83a12010-06-12 14:32:21 +08002039/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04002040bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08002041{
2042 struct drm_i915_private *dev_priv = dev->dev_private;
2043 struct child_device_config *p_child;
2044 int i;
2045
2046 if (!dev_priv->child_dev_num)
2047 return false;
2048
2049 for (i = 0; i < dev_priv->child_dev_num; i++) {
2050 p_child = dev_priv->child_dev + i;
2051
2052 if (p_child->dvo_port == PORT_IDPD &&
2053 p_child->device_type == DEVICE_TYPE_eDP)
2054 return true;
2055 }
2056 return false;
2057}
2058
Chris Wilsonf6849602010-09-19 09:29:33 +01002059static void
2060intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
2061{
Chris Wilson3f43c482011-05-12 22:17:24 +01002062 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +00002063 intel_attach_broadcast_rgb_property(connector);
Chris Wilsonf6849602010-09-19 09:29:33 +01002064}
2065
Keith Packardc8110e52009-05-06 11:51:10 -07002066void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002067intel_dp_init(struct drm_device *dev, int output_reg)
2068{
2069 struct drm_i915_private *dev_priv = dev->dev_private;
2070 struct drm_connector *connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +01002071 struct intel_dp *intel_dp;
Eric Anholt21d40d32010-03-25 11:11:14 -07002072 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002073 struct intel_connector *intel_connector;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002074 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04002075 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002076
Chris Wilsonea5b2132010-08-04 13:50:23 +01002077 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
2078 if (!intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002079 return;
2080
Chris Wilson3d3dc142011-02-12 10:33:12 +00002081 intel_dp->output_reg = output_reg;
Keith Packardd2b996a2011-07-25 22:37:51 -07002082 intel_dp->dpms_mode = -1;
Chris Wilson3d3dc142011-02-12 10:33:12 +00002083
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002084 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
2085 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002086 kfree(intel_dp);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002087 return;
2088 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01002089 intel_encoder = &intel_dp->base;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002090
Chris Wilsonea5b2132010-08-04 13:50:23 +01002091 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04002092 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01002093 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04002094
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07002095 if (output_reg == DP_A || is_pch_edp(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04002096 type = DRM_MODE_CONNECTOR_eDP;
2097 intel_encoder->type = INTEL_OUTPUT_EDP;
2098 } else {
2099 type = DRM_MODE_CONNECTOR_DisplayPort;
2100 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
2101 }
2102
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002103 connector = &intel_connector->base;
Adam Jacksonb3295302010-07-16 14:46:28 -04002104 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002105 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
2106
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002107 connector->polled = DRM_CONNECTOR_POLL_HPD;
2108
Zhao Yakui652af9d2009-12-02 10:03:33 +08002109 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07002110 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08002111 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07002112 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08002113 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07002114 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08002115
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07002116 if (is_edp(intel_dp))
Eric Anholt21d40d32010-03-25 11:11:14 -07002117 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08002118
Eric Anholt21d40d32010-03-25 11:11:14 -07002119 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002120 connector->interlace_allowed = true;
2121 connector->doublescan_allowed = 0;
2122
Chris Wilson4ef69c72010-09-09 15:14:28 +01002123 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002124 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +01002125 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002126
Chris Wilsondf0e9242010-09-09 16:20:55 +01002127 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002128 drm_sysfs_connector_add(connector);
2129
2130 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002131 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002132 case DP_A:
2133 name = "DPDDC-A";
2134 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002135 case DP_B:
2136 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002137 dev_priv->hotplug_supported_mask |=
2138 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002139 name = "DPDDC-B";
2140 break;
2141 case DP_C:
2142 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002143 dev_priv->hotplug_supported_mask |=
2144 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002145 name = "DPDDC-C";
2146 break;
2147 case DP_D:
2148 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002149 dev_priv->hotplug_supported_mask |=
2150 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002151 name = "DPDDC-D";
2152 break;
2153 }
2154
Jesse Barnes89667382010-10-07 16:01:21 -07002155 /* Cache some DPCD data in the eDP case */
2156 if (is_edp(intel_dp)) {
Keith Packard59f3e272011-07-25 20:01:56 -07002157 bool ret;
Keith Packardf01eca22011-09-28 16:48:10 -07002158 struct edp_power_seq cur, vbt;
2159 u32 pp_on, pp_off, pp_div;
Jesse Barnes89667382010-10-07 16:01:21 -07002160
Jesse Barnes5d613502011-01-24 17:10:54 -08002161 pp_on = I915_READ(PCH_PP_ON_DELAYS);
Keith Packardf01eca22011-09-28 16:48:10 -07002162 pp_off = I915_READ(PCH_PP_OFF_DELAYS);
Jesse Barnes5d613502011-01-24 17:10:54 -08002163 pp_div = I915_READ(PCH_PP_DIVISOR);
2164
Keith Packardf01eca22011-09-28 16:48:10 -07002165 /* Pull timing values out of registers */
2166 cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
2167 PANEL_POWER_UP_DELAY_SHIFT;
2168
2169 cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
2170 PANEL_LIGHT_ON_DELAY_SHIFT;
2171
2172 cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
2173 PANEL_LIGHT_OFF_DELAY_SHIFT;
2174
2175 cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
2176 PANEL_POWER_DOWN_DELAY_SHIFT;
2177
2178 cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
2179 PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
2180
2181 DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
2182 cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
2183
2184 vbt = dev_priv->edp.pps;
2185
2186 DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
2187 vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
2188
2189#define get_delay(field) ((max(cur.field, vbt.field) + 9) / 10)
2190
2191 intel_dp->panel_power_up_delay = get_delay(t1_t3);
2192 intel_dp->backlight_on_delay = get_delay(t8);
2193 intel_dp->backlight_off_delay = get_delay(t9);
2194 intel_dp->panel_power_down_delay = get_delay(t10);
2195 intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
2196
2197 DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
2198 intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
2199 intel_dp->panel_power_cycle_delay);
2200
2201 DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
2202 intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
Jesse Barnes5d613502011-01-24 17:10:54 -08002203
2204 ironlake_edp_panel_vdd_on(intel_dp);
Keith Packard59f3e272011-07-25 20:01:56 -07002205 ret = intel_dp_get_dpcd(intel_dp);
Chris Wilson3d3dc142011-02-12 10:33:12 +00002206 ironlake_edp_panel_vdd_off(intel_dp);
Keith Packard59f3e272011-07-25 20:01:56 -07002207 if (ret) {
Jesse Barnes7183dc22011-07-07 11:10:58 -07002208 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
2209 dev_priv->no_aux_handshake =
2210 intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
Jesse Barnes89667382010-10-07 16:01:21 -07002211 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
2212 } else {
Chris Wilson3d3dc142011-02-12 10:33:12 +00002213 /* if this fails, presume the device is a ghost */
Takashi Iwai48898b02011-03-18 09:06:49 +00002214 DRM_INFO("failed to retrieve link info, disabling eDP\n");
Chris Wilson3d3dc142011-02-12 10:33:12 +00002215 intel_dp_encoder_destroy(&intel_dp->base.base);
Takashi Iwai48898b02011-03-18 09:06:49 +00002216 intel_dp_destroy(&intel_connector->base);
Chris Wilson3d3dc142011-02-12 10:33:12 +00002217 return;
Jesse Barnes89667382010-10-07 16:01:21 -07002218 }
Jesse Barnes89667382010-10-07 16:01:21 -07002219 }
2220
Keith Packard552fb0b2011-09-28 16:31:53 -07002221 intel_dp_i2c_init(intel_dp, intel_connector, name);
2222
Eric Anholt21d40d32010-03-25 11:11:14 -07002223 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002224
Jesse Barnes4d926462010-10-07 16:01:07 -07002225 if (is_edp(intel_dp)) {
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02002226 dev_priv->int_edp_connector = connector;
2227 intel_panel_setup_backlight(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002228 }
2229
Chris Wilsonf6849602010-09-19 09:29:33 +01002230 intel_dp_add_properties(intel_dp, connector);
2231
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002232 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
2233 * 0xd. Failure to do so will result in spurious interrupts being
2234 * generated on the port when a cable is not attached.
2235 */
2236 if (IS_G4X(dev) && !IS_GM45(dev)) {
2237 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
2238 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
2239 }
2240}