blob: c8e005553310a5eaeae98e4cb8ed349b4b204164 [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;
Keith Packardc8110e52009-05-06 11:51:10 -070052 int dpms_mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070053 uint8_t link_bw;
54 uint8_t lane_count;
55 uint8_t dpcd[4];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070056 struct i2c_adapter adapter;
57 struct i2c_algo_dp_aux_data algo;
Adam Jacksonf0917372010-07-16 14:46:27 -040058 bool is_pch_edp;
Jesse Barnes33a34e42010-09-08 12:42:02 -070059 uint8_t train_set[4];
60 uint8_t link_status[DP_LINK_STATUS_SIZE];
Chris Wilsonf6849602010-09-19 09:29:33 +010061
62 struct drm_property *force_audio_property;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070063};
64
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070065/**
66 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
67 * @intel_dp: DP struct
68 *
69 * If a CPU or PCH DP output is attached to an eDP panel, this function
70 * will return true, and false otherwise.
71 */
72static bool is_edp(struct intel_dp *intel_dp)
73{
74 return intel_dp->base.type == INTEL_OUTPUT_EDP;
75}
76
77/**
78 * is_pch_edp - is the port on the PCH and attached to an eDP panel?
79 * @intel_dp: DP struct
80 *
81 * Returns true if the given DP struct corresponds to a PCH DP port attached
82 * to an eDP panel, false otherwise. Helpful for determining whether we
83 * may need FDI resources for a given DP output or not.
84 */
85static bool is_pch_edp(struct intel_dp *intel_dp)
86{
87 return intel_dp->is_pch_edp;
88}
89
Chris Wilsonea5b2132010-08-04 13:50:23 +010090static struct intel_dp *enc_to_intel_dp(struct drm_encoder *encoder)
91{
Chris Wilson4ef69c72010-09-09 15:14:28 +010092 return container_of(encoder, struct intel_dp, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010093}
Keith Packarda4fc5ed2009-04-07 16:16:42 -070094
Chris Wilsondf0e9242010-09-09 16:20:55 +010095static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
96{
97 return container_of(intel_attached_encoder(connector),
98 struct intel_dp, base);
99}
100
Jesse Barnes814948a2010-10-07 16:01:09 -0700101/**
102 * intel_encoder_is_pch_edp - is the given encoder a PCH attached eDP?
103 * @encoder: DRM encoder
104 *
105 * Return true if @encoder corresponds to a PCH attached eDP panel. Needed
106 * by intel_display.c.
107 */
108bool intel_encoder_is_pch_edp(struct drm_encoder *encoder)
109{
110 struct intel_dp *intel_dp;
111
112 if (!encoder)
113 return false;
114
115 intel_dp = enc_to_intel_dp(encoder);
116
117 return is_pch_edp(intel_dp);
118}
119
Jesse Barnes33a34e42010-09-08 12:42:02 -0700120static void intel_dp_start_link_train(struct intel_dp *intel_dp);
121static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100122static void intel_dp_link_down(struct intel_dp *intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700123
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800124void
Eric Anholt21d40d32010-03-25 11:11:14 -0700125intel_edp_link_config (struct intel_encoder *intel_encoder,
Chris Wilsonea5b2132010-08-04 13:50:23 +0100126 int *lane_num, int *link_bw)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800127{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100128 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800129
Chris Wilsonea5b2132010-08-04 13:50:23 +0100130 *lane_num = intel_dp->lane_count;
131 if (intel_dp->link_bw == DP_LINK_BW_1_62)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800132 *link_bw = 162000;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100133 else if (intel_dp->link_bw == DP_LINK_BW_2_7)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800134 *link_bw = 270000;
135}
136
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700137static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100138intel_dp_max_lane_count(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700139{
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700140 int max_lane_count = 4;
141
Chris Wilsonea5b2132010-08-04 13:50:23 +0100142 if (intel_dp->dpcd[0] >= 0x11) {
143 max_lane_count = intel_dp->dpcd[2] & 0x1f;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700144 switch (max_lane_count) {
145 case 1: case 2: case 4:
146 break;
147 default:
148 max_lane_count = 4;
149 }
150 }
151 return max_lane_count;
152}
153
154static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100155intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700156{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100157 int max_link_bw = intel_dp->dpcd[1];
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700158
159 switch (max_link_bw) {
160 case DP_LINK_BW_1_62:
161 case DP_LINK_BW_2_7:
162 break;
163 default:
164 max_link_bw = DP_LINK_BW_1_62;
165 break;
166 }
167 return max_link_bw;
168}
169
170static int
171intel_dp_link_clock(uint8_t link_bw)
172{
173 if (link_bw == DP_LINK_BW_2_7)
174 return 270000;
175 else
176 return 162000;
177}
178
179/* I think this is a fiction */
180static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100181intel_dp_link_required(struct drm_device *dev, struct intel_dp *intel_dp, int pixel_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700182{
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800183 struct drm_i915_private *dev_priv = dev->dev_private;
184
Jesse Barnes4d926462010-10-07 16:01:07 -0700185 if (is_edp(intel_dp))
Chris Wilson5ceb0f92010-09-24 10:24:28 +0100186 return (pixel_clock * dev_priv->edp.bpp + 7) / 8;
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800187 else
188 return pixel_clock * 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700189}
190
191static int
Dave Airliefe27d532010-06-30 11:46:17 +1000192intel_dp_max_data_rate(int max_link_clock, int max_lanes)
193{
194 return (max_link_clock * max_lanes * 8) / 10;
195}
196
197static int
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700198intel_dp_mode_valid(struct drm_connector *connector,
199 struct drm_display_mode *mode)
200{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100201 struct intel_dp *intel_dp = intel_attached_dp(connector);
Zhao Yakui7de56f42010-07-19 09:43:14 +0100202 struct drm_device *dev = connector->dev;
203 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100204 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
205 int max_lanes = intel_dp_max_lane_count(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700206
Jesse Barnes4d926462010-10-07 16:01:07 -0700207 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Zhao Yakui7de56f42010-07-19 09:43:14 +0100208 if (mode->hdisplay > dev_priv->panel_fixed_mode->hdisplay)
209 return MODE_PANEL;
210
211 if (mode->vdisplay > dev_priv->panel_fixed_mode->vdisplay)
212 return MODE_PANEL;
213 }
214
Dave Airliefe27d532010-06-30 11:46:17 +1000215 /* only refuse the mode on non eDP since we have seen some wierd eDP panels
216 which are outside spec tolerances but somehow work by magic */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700217 if (!is_edp(intel_dp) &&
Chris Wilsonea5b2132010-08-04 13:50:23 +0100218 (intel_dp_link_required(connector->dev, intel_dp, mode->clock)
Dave Airliefe27d532010-06-30 11:46:17 +1000219 > intel_dp_max_data_rate(max_link_clock, max_lanes)))
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700220 return MODE_CLOCK_HIGH;
221
222 if (mode->clock < 10000)
223 return MODE_CLOCK_LOW;
224
225 return MODE_OK;
226}
227
228static uint32_t
229pack_aux(uint8_t *src, int src_bytes)
230{
231 int i;
232 uint32_t v = 0;
233
234 if (src_bytes > 4)
235 src_bytes = 4;
236 for (i = 0; i < src_bytes; i++)
237 v |= ((uint32_t) src[i]) << ((3-i) * 8);
238 return v;
239}
240
241static void
242unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
243{
244 int i;
245 if (dst_bytes > 4)
246 dst_bytes = 4;
247 for (i = 0; i < dst_bytes; i++)
248 dst[i] = src >> ((3-i) * 8);
249}
250
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700251/* hrawclock is 1/4 the FSB frequency */
252static int
253intel_hrawclk(struct drm_device *dev)
254{
255 struct drm_i915_private *dev_priv = dev->dev_private;
256 uint32_t clkcfg;
257
258 clkcfg = I915_READ(CLKCFG);
259 switch (clkcfg & CLKCFG_FSB_MASK) {
260 case CLKCFG_FSB_400:
261 return 100;
262 case CLKCFG_FSB_533:
263 return 133;
264 case CLKCFG_FSB_667:
265 return 166;
266 case CLKCFG_FSB_800:
267 return 200;
268 case CLKCFG_FSB_1067:
269 return 266;
270 case CLKCFG_FSB_1333:
271 return 333;
272 /* these two are just a guess; one of them might be right */
273 case CLKCFG_FSB_1600:
274 case CLKCFG_FSB_1600_ALT:
275 return 400;
276 default:
277 return 133;
278 }
279}
280
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700281static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100282intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700283 uint8_t *send, int send_bytes,
284 uint8_t *recv, int recv_size)
285{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100286 uint32_t output_reg = intel_dp->output_reg;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100287 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700288 struct drm_i915_private *dev_priv = dev->dev_private;
289 uint32_t ch_ctl = output_reg + 0x10;
290 uint32_t ch_data = ch_ctl + 4;
291 int i;
292 int recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700293 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700294 uint32_t aux_clock_divider;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800295 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700296
297 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700298 * and would like to run at 2MHz. So, take the
299 * hrawclk value and divide by 2 and use that
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700300 *
301 * Note that PCH attached eDP panels should use a 125MHz input
302 * clock divider.
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700303 */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700304 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +0800305 if (IS_GEN6(dev))
306 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
307 else
308 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
309 } else if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500310 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800311 else
312 aux_clock_divider = intel_hrawclk(dev) / 2;
313
Zhenyu Wange3421a12010-04-08 09:43:27 +0800314 if (IS_GEN6(dev))
315 precharge = 3;
316 else
317 precharge = 5;
318
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100319 if (I915_READ(ch_ctl) & DP_AUX_CH_CTL_SEND_BUSY) {
320 DRM_ERROR("dp_aux_ch not started status 0x%08x\n",
321 I915_READ(ch_ctl));
322 return -EBUSY;
323 }
324
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700325 /* Must try at least 3 times according to DP spec */
326 for (try = 0; try < 5; try++) {
327 /* Load the send data into the aux channel data registers */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100328 for (i = 0; i < send_bytes; i += 4)
329 I915_WRITE(ch_data + i,
330 pack_aux(send + i, send_bytes - i));
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700331
332 /* Send the command and wait for it to complete */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100333 I915_WRITE(ch_ctl,
334 DP_AUX_CH_CTL_SEND_BUSY |
335 DP_AUX_CH_CTL_TIME_OUT_400us |
336 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
337 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
338 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
339 DP_AUX_CH_CTL_DONE |
340 DP_AUX_CH_CTL_TIME_OUT_ERROR |
341 DP_AUX_CH_CTL_RECEIVE_ERROR);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700342 for (;;) {
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700343 status = I915_READ(ch_ctl);
344 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
345 break;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100346 udelay(100);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700347 }
348
349 /* Clear done status and any errors */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100350 I915_WRITE(ch_ctl,
351 status |
352 DP_AUX_CH_CTL_DONE |
353 DP_AUX_CH_CTL_TIME_OUT_ERROR |
354 DP_AUX_CH_CTL_RECEIVE_ERROR);
355 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700356 break;
357 }
358
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700359 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700360 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700361 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700362 }
363
364 /* Check for timeout or receive error.
365 * Timeouts occur when the sink is not connected
366 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700367 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700368 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700369 return -EIO;
370 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700371
372 /* Timeouts occur when the device isn't connected, so they're
373 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700374 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800375 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700376 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700377 }
378
379 /* Unload any bytes sent back from the other side */
380 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
381 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700382 if (recv_bytes > recv_size)
383 recv_bytes = recv_size;
384
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100385 for (i = 0; i < recv_bytes; i += 4)
386 unpack_aux(I915_READ(ch_data + i),
387 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700388
389 return recv_bytes;
390}
391
392/* Write data to the aux channel in native mode */
393static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100394intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700395 uint16_t address, uint8_t *send, int send_bytes)
396{
397 int ret;
398 uint8_t msg[20];
399 int msg_bytes;
400 uint8_t ack;
401
402 if (send_bytes > 16)
403 return -1;
404 msg[0] = AUX_NATIVE_WRITE << 4;
405 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800406 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700407 msg[3] = send_bytes - 1;
408 memcpy(&msg[4], send, send_bytes);
409 msg_bytes = send_bytes + 4;
410 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100411 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700412 if (ret < 0)
413 return ret;
414 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
415 break;
416 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
417 udelay(100);
418 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700419 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700420 }
421 return send_bytes;
422}
423
424/* Write a single byte to the aux channel in native mode */
425static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100426intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700427 uint16_t address, uint8_t byte)
428{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100429 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700430}
431
432/* read bytes from a native aux channel */
433static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100434intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700435 uint16_t address, uint8_t *recv, int recv_bytes)
436{
437 uint8_t msg[4];
438 int msg_bytes;
439 uint8_t reply[20];
440 int reply_bytes;
441 uint8_t ack;
442 int ret;
443
444 msg[0] = AUX_NATIVE_READ << 4;
445 msg[1] = address >> 8;
446 msg[2] = address & 0xff;
447 msg[3] = recv_bytes - 1;
448
449 msg_bytes = 4;
450 reply_bytes = recv_bytes + 1;
451
452 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100453 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700454 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700455 if (ret == 0)
456 return -EPROTO;
457 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700458 return ret;
459 ack = reply[0];
460 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
461 memcpy(recv, reply + 1, ret - 1);
462 return ret - 1;
463 }
464 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
465 udelay(100);
466 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700467 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700468 }
469}
470
471static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000472intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
473 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700474{
Dave Airlieab2c0672009-12-04 10:55:24 +1000475 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100476 struct intel_dp *intel_dp = container_of(adapter,
477 struct intel_dp,
478 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000479 uint16_t address = algo_data->address;
480 uint8_t msg[5];
481 uint8_t reply[2];
482 int msg_bytes;
483 int reply_bytes;
484 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700485
Dave Airlieab2c0672009-12-04 10:55:24 +1000486 /* Set up the command byte */
487 if (mode & MODE_I2C_READ)
488 msg[0] = AUX_I2C_READ << 4;
489 else
490 msg[0] = AUX_I2C_WRITE << 4;
491
492 if (!(mode & MODE_I2C_STOP))
493 msg[0] |= AUX_I2C_MOT << 4;
494
495 msg[1] = address >> 8;
496 msg[2] = address;
497
498 switch (mode) {
499 case MODE_I2C_WRITE:
500 msg[3] = 0;
501 msg[4] = write_byte;
502 msg_bytes = 5;
503 reply_bytes = 1;
504 break;
505 case MODE_I2C_READ:
506 msg[3] = 0;
507 msg_bytes = 4;
508 reply_bytes = 2;
509 break;
510 default:
511 msg_bytes = 3;
512 reply_bytes = 1;
513 break;
514 }
515
516 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100517 ret = intel_dp_aux_ch(intel_dp,
Dave Airlieab2c0672009-12-04 10:55:24 +1000518 msg, msg_bytes,
519 reply, reply_bytes);
520 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000521 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000522 return ret;
523 }
524 switch (reply[0] & AUX_I2C_REPLY_MASK) {
525 case AUX_I2C_REPLY_ACK:
526 if (mode == MODE_I2C_READ) {
527 *read_byte = reply[1];
528 }
529 return reply_bytes - 1;
530 case AUX_I2C_REPLY_NACK:
Dave Airlie3ff99162009-12-08 14:03:47 +1000531 DRM_DEBUG_KMS("aux_ch nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000532 return -EREMOTEIO;
533 case AUX_I2C_REPLY_DEFER:
Dave Airlie3ff99162009-12-08 14:03:47 +1000534 DRM_DEBUG_KMS("aux_ch defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000535 udelay(100);
536 break;
537 default:
538 DRM_ERROR("aux_ch invalid reply 0x%02x\n", reply[0]);
539 return -EREMOTEIO;
540 }
541 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700542}
543
544static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100545intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800546 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700547{
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800548 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100549 intel_dp->algo.running = false;
550 intel_dp->algo.address = 0;
551 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700552
Chris Wilsonea5b2132010-08-04 13:50:23 +0100553 memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
554 intel_dp->adapter.owner = THIS_MODULE;
555 intel_dp->adapter.class = I2C_CLASS_DDC;
556 strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
557 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
558 intel_dp->adapter.algo_data = &intel_dp->algo;
559 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
560
561 return i2c_dp_aux_add_bus(&intel_dp->adapter);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700562}
563
564static bool
565intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
566 struct drm_display_mode *adjusted_mode)
567{
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100568 struct drm_device *dev = encoder->dev;
569 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100570 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700571 int lane_count, clock;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100572 int max_lane_count = intel_dp_max_lane_count(intel_dp);
573 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700574 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
575
Jesse Barnes4d926462010-10-07 16:01:07 -0700576 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100577 intel_fixed_panel_mode(dev_priv->panel_fixed_mode, adjusted_mode);
578 intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
579 mode, adjusted_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100580 /*
581 * the mode->clock is used to calculate the Data&Link M/N
582 * of the pipe. For the eDP the fixed clock should be used.
583 */
584 mode->clock = dev_priv->panel_fixed_mode->clock;
585 }
586
Jesse Barnes869184a2010-10-07 16:01:22 -0700587 /* Just use VBT values for eDP */
588 if (is_edp(intel_dp)) {
589 intel_dp->lane_count = dev_priv->edp.lanes;
590 intel_dp->link_bw = dev_priv->edp.rate;
591 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
592 DRM_DEBUG_KMS("eDP link bw %02x lane count %d clock %d\n",
593 intel_dp->link_bw, intel_dp->lane_count,
594 adjusted_mode->clock);
595 return true;
596 }
597
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700598 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
599 for (clock = 0; clock <= max_clock; clock++) {
Dave Airliefe27d532010-06-30 11:46:17 +1000600 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700601
Chris Wilsonea5b2132010-08-04 13:50:23 +0100602 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800603 <= link_avail) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100604 intel_dp->link_bw = bws[clock];
605 intel_dp->lane_count = lane_count;
606 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Zhao Yakui28c97732009-10-09 11:39:41 +0800607 DRM_DEBUG_KMS("Display port link bw %02x lane "
608 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100609 intel_dp->link_bw, intel_dp->lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700610 adjusted_mode->clock);
611 return true;
612 }
613 }
614 }
Dave Airliefe27d532010-06-30 11:46:17 +1000615
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700616 return false;
617}
618
619struct intel_dp_m_n {
620 uint32_t tu;
621 uint32_t gmch_m;
622 uint32_t gmch_n;
623 uint32_t link_m;
624 uint32_t link_n;
625};
626
627static void
628intel_reduce_ratio(uint32_t *num, uint32_t *den)
629{
630 while (*num > 0xffffff || *den > 0xffffff) {
631 *num >>= 1;
632 *den >>= 1;
633 }
634}
635
636static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800637intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700638 int nlanes,
639 int pixel_clock,
640 int link_clock,
641 struct intel_dp_m_n *m_n)
642{
643 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800644 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700645 m_n->gmch_n = link_clock * nlanes;
646 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
647 m_n->link_m = pixel_clock;
648 m_n->link_n = link_clock;
649 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
650}
651
652void
653intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
654 struct drm_display_mode *adjusted_mode)
655{
656 struct drm_device *dev = crtc->dev;
657 struct drm_mode_config *mode_config = &dev->mode_config;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800658 struct drm_encoder *encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700659 struct drm_i915_private *dev_priv = dev->dev_private;
660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Zhao Yakui36e83a12010-06-12 14:32:21 +0800661 int lane_count = 4, bpp = 24;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700662 struct intel_dp_m_n m_n;
663
664 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700665 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700666 */
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800667 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100668 struct intel_dp *intel_dp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700669
Dan Carpenterd8201ab2010-05-07 10:39:00 +0200670 if (encoder->crtc != crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700671 continue;
672
Chris Wilsonea5b2132010-08-04 13:50:23 +0100673 intel_dp = enc_to_intel_dp(encoder);
674 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
675 lane_count = intel_dp->lane_count;
Jesse Barnes51190662010-10-07 16:01:08 -0700676 break;
677 } else if (is_edp(intel_dp)) {
678 lane_count = dev_priv->edp.lanes;
679 bpp = dev_priv->edp.bpp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700680 break;
681 }
682 }
683
684 /*
685 * Compute the GMCH and Link ratios. The '3' here is
686 * the number of bytes_per_pixel post-LUT, which we always
687 * set up for 8-bits of R/G/B, or 3 bytes total.
688 */
Zhao Yakui36e83a12010-06-12 14:32:21 +0800689 intel_dp_compute_m_n(bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700690 mode->clock, adjusted_mode->clock, &m_n);
691
Eric Anholtc619eed2010-01-28 16:45:52 -0800692 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800693 if (intel_crtc->pipe == 0) {
694 I915_WRITE(TRANSA_DATA_M1,
695 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
696 m_n.gmch_m);
697 I915_WRITE(TRANSA_DATA_N1, m_n.gmch_n);
698 I915_WRITE(TRANSA_DP_LINK_M1, m_n.link_m);
699 I915_WRITE(TRANSA_DP_LINK_N1, m_n.link_n);
700 } else {
701 I915_WRITE(TRANSB_DATA_M1,
702 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
703 m_n.gmch_m);
704 I915_WRITE(TRANSB_DATA_N1, m_n.gmch_n);
705 I915_WRITE(TRANSB_DP_LINK_M1, m_n.link_m);
706 I915_WRITE(TRANSB_DP_LINK_N1, m_n.link_n);
707 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700708 } else {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800709 if (intel_crtc->pipe == 0) {
710 I915_WRITE(PIPEA_GMCH_DATA_M,
711 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
712 m_n.gmch_m);
713 I915_WRITE(PIPEA_GMCH_DATA_N,
714 m_n.gmch_n);
715 I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
716 I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
717 } else {
718 I915_WRITE(PIPEB_GMCH_DATA_M,
719 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
720 m_n.gmch_m);
721 I915_WRITE(PIPEB_GMCH_DATA_N,
722 m_n.gmch_n);
723 I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
724 I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
725 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700726 }
727}
728
729static void
730intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
731 struct drm_display_mode *adjusted_mode)
732{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800733 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100734 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +0100735 struct drm_crtc *crtc = intel_dp->base.base.crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
737
Chris Wilsonea5b2132010-08-04 13:50:23 +0100738 intel_dp->DP = (DP_VOLTAGE_0_4 |
Adam Jackson9c9e7922010-04-05 17:57:59 -0400739 DP_PRE_EMPHASIS_0);
740
741 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100742 intel_dp->DP |= DP_SYNC_HS_HIGH;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400743 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100744 intel_dp->DP |= DP_SYNC_VS_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700745
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700746 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100747 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800748 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100749 intel_dp->DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700750
Chris Wilsonea5b2132010-08-04 13:50:23 +0100751 switch (intel_dp->lane_count) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700752 case 1:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100753 intel_dp->DP |= DP_PORT_WIDTH_1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700754 break;
755 case 2:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100756 intel_dp->DP |= DP_PORT_WIDTH_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700757 break;
758 case 4:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100759 intel_dp->DP |= DP_PORT_WIDTH_4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700760 break;
761 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100762 if (intel_dp->has_audio)
763 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700764
Chris Wilsonea5b2132010-08-04 13:50:23 +0100765 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
766 intel_dp->link_configuration[0] = intel_dp->link_bw;
767 intel_dp->link_configuration[1] = intel_dp->lane_count;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700768
769 /*
Adam Jackson9962c922010-05-13 14:45:42 -0400770 * Check for DPCD version > 1.1 and enhanced framing support
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700771 */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100772 if (intel_dp->dpcd[0] >= 0x11 && (intel_dp->dpcd[2] & DP_ENHANCED_FRAME_CAP)) {
773 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
774 intel_dp->DP |= DP_ENHANCED_FRAMING;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700775 }
776
Zhenyu Wange3421a12010-04-08 09:43:27 +0800777 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
778 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100779 intel_dp->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800780
Jesse Barnes895692b2010-10-07 16:01:23 -0700781 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800782 /* don't miss out required setting for eDP */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100783 intel_dp->DP |= DP_PLL_ENABLE;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800784 if (adjusted_mode->clock < 200000)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100785 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800786 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100787 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800788 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700789}
790
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700791/* Returns true if the panel was already on when called */
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700792static bool ironlake_edp_panel_on (struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -0700793{
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700794 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes9934c132010-07-22 13:18:19 -0700795 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700796 u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_STATE_ON_IDLE;
Jesse Barnes9934c132010-07-22 13:18:19 -0700797
Chris Wilson913d8d12010-08-07 11:01:35 +0100798 if (I915_READ(PCH_PP_STATUS) & PP_ON)
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700799 return true;
Jesse Barnes9934c132010-07-22 13:18:19 -0700800
801 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700802
803 /* ILK workaround: disable reset around power sequence */
804 pp &= ~PANEL_POWER_RESET;
805 I915_WRITE(PCH_PP_CONTROL, pp);
806 POSTING_READ(PCH_PP_CONTROL);
807
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700808 pp |= PANEL_UNLOCK_REGS | POWER_TARGET_ON;
Jesse Barnes9934c132010-07-22 13:18:19 -0700809 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700810 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700811
Hette Visser27d64332010-09-24 10:51:30 +0100812 /* Ouch. We need to wait here for some panels, like Dell e6510
813 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
814 */
815 msleep(300);
816
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700817 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_on_mask) == idle_on_mask,
818 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100819 DRM_ERROR("panel on wait timed out: 0x%08x\n",
820 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700821
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700822 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700823 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700824 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700825
826 return false;
Jesse Barnes9934c132010-07-22 13:18:19 -0700827}
828
829static void ironlake_edp_panel_off (struct drm_device *dev)
830{
831 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700832 u32 pp, idle_off_mask = PP_ON | PP_SEQUENCE_MASK |
833 PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK;
Jesse Barnes9934c132010-07-22 13:18:19 -0700834
835 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700836
837 /* ILK workaround: disable reset around power sequence */
838 pp &= ~PANEL_POWER_RESET;
839 I915_WRITE(PCH_PP_CONTROL, pp);
840 POSTING_READ(PCH_PP_CONTROL);
841
Jesse Barnes9934c132010-07-22 13:18:19 -0700842 pp &= ~POWER_TARGET_ON;
843 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700844 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700845
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700846 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_off_mask) == 0, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100847 DRM_ERROR("panel off wait timed out: 0x%08x\n",
848 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700849
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700850 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700851 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700852 POSTING_READ(PCH_PP_CONTROL);
Hette Visser27d64332010-09-24 10:51:30 +0100853
854 /* Ouch. We need to wait here for some panels, like Dell e6510
855 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
856 */
857 msleep(300);
Jesse Barnes9934c132010-07-22 13:18:19 -0700858}
859
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500860static void ironlake_edp_backlight_on (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800861{
862 struct drm_i915_private *dev_priv = dev->dev_private;
863 u32 pp;
864
Zhao Yakui28c97732009-10-09 11:39:41 +0800865 DRM_DEBUG_KMS("\n");
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700866 /*
867 * If we enable the backlight right away following a panel power
868 * on, we may see slight flicker as the panel syncs with the eDP
869 * link. So delay a bit to make sure the image is solid before
870 * allowing it to appear.
871 */
872 msleep(300);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800873 pp = I915_READ(PCH_PP_CONTROL);
874 pp |= EDP_BLC_ENABLE;
875 I915_WRITE(PCH_PP_CONTROL, pp);
876}
877
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500878static void ironlake_edp_backlight_off (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800879{
880 struct drm_i915_private *dev_priv = dev->dev_private;
881 u32 pp;
882
Zhao Yakui28c97732009-10-09 11:39:41 +0800883 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800884 pp = I915_READ(PCH_PP_CONTROL);
885 pp &= ~EDP_BLC_ENABLE;
886 I915_WRITE(PCH_PP_CONTROL, pp);
887}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700888
Jesse Barnesd240f202010-08-13 15:43:26 -0700889static void ironlake_edp_pll_on(struct drm_encoder *encoder)
890{
891 struct drm_device *dev = encoder->dev;
892 struct drm_i915_private *dev_priv = dev->dev_private;
893 u32 dpa_ctl;
894
895 DRM_DEBUG_KMS("\n");
896 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -0700897 dpa_ctl |= DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -0700898 I915_WRITE(DP_A, dpa_ctl);
Jesse Barnes298b0b32010-10-07 16:01:24 -0700899 POSTING_READ(DP_A);
900 udelay(200);
Jesse Barnesd240f202010-08-13 15:43:26 -0700901}
902
903static void ironlake_edp_pll_off(struct drm_encoder *encoder)
904{
905 struct drm_device *dev = encoder->dev;
906 struct drm_i915_private *dev_priv = dev->dev_private;
907 u32 dpa_ctl;
908
909 dpa_ctl = I915_READ(DP_A);
Jesse Barnes298b0b32010-10-07 16:01:24 -0700910 dpa_ctl &= ~DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -0700911 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +0100912 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -0700913 udelay(200);
914}
915
916static void intel_dp_prepare(struct drm_encoder *encoder)
917{
918 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
919 struct drm_device *dev = encoder->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -0700920
Jesse Barnes4d926462010-10-07 16:01:07 -0700921 if (is_edp(intel_dp)) {
Jesse Barnesd240f202010-08-13 15:43:26 -0700922 ironlake_edp_backlight_off(dev);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700923 ironlake_edp_panel_on(intel_dp);
924 if (!is_pch_edp(intel_dp))
925 ironlake_edp_pll_on(encoder);
926 else
927 ironlake_edp_pll_off(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -0700928 }
Jesse Barnes736085b2010-10-08 10:35:55 -0700929 intel_dp_link_down(intel_dp);
Jesse Barnesd240f202010-08-13 15:43:26 -0700930}
931
932static void intel_dp_commit(struct drm_encoder *encoder)
933{
934 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
935 struct drm_device *dev = encoder->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -0700936
Jesse Barnes33a34e42010-09-08 12:42:02 -0700937 intel_dp_start_link_train(intel_dp);
938
Jesse Barnes4d926462010-10-07 16:01:07 -0700939 if (is_edp(intel_dp))
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700940 ironlake_edp_panel_on(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700941
942 intel_dp_complete_link_train(intel_dp);
943
Jesse Barnes4d926462010-10-07 16:01:07 -0700944 if (is_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -0700945 ironlake_edp_backlight_on(dev);
946}
947
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700948static void
949intel_dp_dpms(struct drm_encoder *encoder, int mode)
950{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100951 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800952 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700953 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100954 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700955
956 if (mode != DRM_MODE_DPMS_ON) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700957 if (is_edp(intel_dp))
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700958 ironlake_edp_backlight_off(dev);
Jesse Barnes736085b2010-10-08 10:35:55 -0700959 intel_dp_link_down(intel_dp);
Jesse Barnes4d926462010-10-07 16:01:07 -0700960 if (is_edp(intel_dp))
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700961 ironlake_edp_panel_off(dev);
962 if (is_edp(intel_dp) && !is_pch_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -0700963 ironlake_edp_pll_off(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700964 } else {
Jesse Barnes736085b2010-10-08 10:35:55 -0700965 if (is_edp(intel_dp))
966 ironlake_edp_panel_on(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800967 if (!(dp_reg & DP_PORT_EN)) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700968 intel_dp_start_link_train(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700969 intel_dp_complete_link_train(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800970 }
Jesse Barnes736085b2010-10-08 10:35:55 -0700971 if (is_edp(intel_dp))
972 ironlake_edp_backlight_on(dev);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700973 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100974 intel_dp->dpms_mode = mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700975}
976
977/*
978 * Fetch AUX CH registers 0x202 - 0x207 which contain
979 * link status information
980 */
981static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -0700982intel_dp_get_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700983{
984 int ret;
985
Chris Wilsonea5b2132010-08-04 13:50:23 +0100986 ret = intel_dp_aux_native_read(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700987 DP_LANE0_1_STATUS,
Jesse Barnes33a34e42010-09-08 12:42:02 -0700988 intel_dp->link_status, DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700989 if (ret != DP_LINK_STATUS_SIZE)
990 return false;
991 return true;
992}
993
994static uint8_t
995intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
996 int r)
997{
998 return link_status[r - DP_LANE0_1_STATUS];
999}
1000
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001001static uint8_t
1002intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
1003 int lane)
1004{
1005 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1006 int s = ((lane & 1) ?
1007 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1008 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1009 uint8_t l = intel_dp_link_status(link_status, i);
1010
1011 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1012}
1013
1014static uint8_t
1015intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1016 int lane)
1017{
1018 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1019 int s = ((lane & 1) ?
1020 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1021 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1022 uint8_t l = intel_dp_link_status(link_status, i);
1023
1024 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1025}
1026
1027
1028#if 0
1029static char *voltage_names[] = {
1030 "0.4V", "0.6V", "0.8V", "1.2V"
1031};
1032static char *pre_emph_names[] = {
1033 "0dB", "3.5dB", "6dB", "9.5dB"
1034};
1035static char *link_train_names[] = {
1036 "pattern 1", "pattern 2", "idle", "off"
1037};
1038#endif
1039
1040/*
1041 * These are source-specific values; current Intel hardware supports
1042 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1043 */
1044#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1045
1046static uint8_t
1047intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1048{
1049 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1050 case DP_TRAIN_VOLTAGE_SWING_400:
1051 return DP_TRAIN_PRE_EMPHASIS_6;
1052 case DP_TRAIN_VOLTAGE_SWING_600:
1053 return DP_TRAIN_PRE_EMPHASIS_6;
1054 case DP_TRAIN_VOLTAGE_SWING_800:
1055 return DP_TRAIN_PRE_EMPHASIS_3_5;
1056 case DP_TRAIN_VOLTAGE_SWING_1200:
1057 default:
1058 return DP_TRAIN_PRE_EMPHASIS_0;
1059 }
1060}
1061
1062static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001063intel_get_adjust_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001064{
1065 uint8_t v = 0;
1066 uint8_t p = 0;
1067 int lane;
1068
Jesse Barnes33a34e42010-09-08 12:42:02 -07001069 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1070 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1071 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001072
1073 if (this_v > v)
1074 v = this_v;
1075 if (this_p > p)
1076 p = this_p;
1077 }
1078
1079 if (v >= I830_DP_VOLTAGE_MAX)
1080 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1081
1082 if (p >= intel_dp_pre_emphasis_max(v))
1083 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1084
1085 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001086 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001087}
1088
1089static uint32_t
Jesse Barnes869184a2010-10-07 16:01:22 -07001090intel_dp_signal_levels(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001091{
Jesse Barnes869184a2010-10-07 16:01:22 -07001092 struct drm_device *dev = intel_dp->base.base.dev;
1093 struct drm_i915_private *dev_priv = dev->dev_private;
1094 uint32_t signal_levels = 0;
1095 u8 train_set = intel_dp->train_set[0];
1096 u32 vswing = train_set & DP_TRAIN_VOLTAGE_SWING_MASK;
1097 u32 preemphasis = train_set & DP_TRAIN_PRE_EMPHASIS_MASK;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001098
Jesse Barnes869184a2010-10-07 16:01:22 -07001099 if (is_edp(intel_dp)) {
1100 vswing = dev_priv->edp.vswing;
1101 preemphasis = dev_priv->edp.preemphasis;
1102 }
1103
1104 switch (vswing) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001105 case DP_TRAIN_VOLTAGE_SWING_400:
1106 default:
1107 signal_levels |= DP_VOLTAGE_0_4;
1108 break;
1109 case DP_TRAIN_VOLTAGE_SWING_600:
1110 signal_levels |= DP_VOLTAGE_0_6;
1111 break;
1112 case DP_TRAIN_VOLTAGE_SWING_800:
1113 signal_levels |= DP_VOLTAGE_0_8;
1114 break;
1115 case DP_TRAIN_VOLTAGE_SWING_1200:
1116 signal_levels |= DP_VOLTAGE_1_2;
1117 break;
1118 }
Jesse Barnes869184a2010-10-07 16:01:22 -07001119 switch (preemphasis) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001120 case DP_TRAIN_PRE_EMPHASIS_0:
1121 default:
1122 signal_levels |= DP_PRE_EMPHASIS_0;
1123 break;
1124 case DP_TRAIN_PRE_EMPHASIS_3_5:
1125 signal_levels |= DP_PRE_EMPHASIS_3_5;
1126 break;
1127 case DP_TRAIN_PRE_EMPHASIS_6:
1128 signal_levels |= DP_PRE_EMPHASIS_6;
1129 break;
1130 case DP_TRAIN_PRE_EMPHASIS_9_5:
1131 signal_levels |= DP_PRE_EMPHASIS_9_5;
1132 break;
1133 }
1134 return signal_levels;
1135}
1136
Zhenyu Wange3421a12010-04-08 09:43:27 +08001137/* Gen6's DP voltage swing and pre-emphasis control */
1138static uint32_t
1139intel_gen6_edp_signal_levels(uint8_t train_set)
1140{
1141 switch (train_set & (DP_TRAIN_VOLTAGE_SWING_MASK|DP_TRAIN_PRE_EMPHASIS_MASK)) {
1142 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1143 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1144 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1145 return EDP_LINK_TRAIN_400MV_6DB_SNB_B;
1146 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1147 return EDP_LINK_TRAIN_600MV_3_5DB_SNB_B;
1148 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1149 return EDP_LINK_TRAIN_800MV_0DB_SNB_B;
1150 default:
1151 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level\n");
1152 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1153 }
1154}
1155
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001156static uint8_t
1157intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1158 int lane)
1159{
1160 int i = DP_LANE0_1_STATUS + (lane >> 1);
1161 int s = (lane & 1) * 4;
1162 uint8_t l = intel_dp_link_status(link_status, i);
1163
1164 return (l >> s) & 0xf;
1165}
1166
1167/* Check for clock recovery is done on all channels */
1168static bool
1169intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1170{
1171 int lane;
1172 uint8_t lane_status;
1173
1174 for (lane = 0; lane < lane_count; lane++) {
1175 lane_status = intel_get_lane_status(link_status, lane);
1176 if ((lane_status & DP_LANE_CR_DONE) == 0)
1177 return false;
1178 }
1179 return true;
1180}
1181
1182/* Check to see if channel eq is done on all channels */
1183#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1184 DP_LANE_CHANNEL_EQ_DONE|\
1185 DP_LANE_SYMBOL_LOCKED)
1186static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001187intel_channel_eq_ok(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001188{
1189 uint8_t lane_align;
1190 uint8_t lane_status;
1191 int lane;
1192
Jesse Barnes33a34e42010-09-08 12:42:02 -07001193 lane_align = intel_dp_link_status(intel_dp->link_status,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001194 DP_LANE_ALIGN_STATUS_UPDATED);
1195 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1196 return false;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001197 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1198 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001199 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1200 return false;
1201 }
1202 return true;
1203}
1204
1205static bool
Jesse Barnes869184a2010-10-07 16:01:22 -07001206intel_dp_aux_handshake_required(struct intel_dp *intel_dp)
1207{
1208 struct drm_device *dev = intel_dp->base.base.dev;
1209 struct drm_i915_private *dev_priv = dev->dev_private;
1210
1211 if (is_edp(intel_dp) && dev_priv->no_aux_handshake)
1212 return false;
1213
1214 return true;
1215}
1216
1217static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001218intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001219 uint32_t dp_reg_value,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001220 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001221{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001222 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001223 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001224 int ret;
1225
Chris Wilsonea5b2132010-08-04 13:50:23 +01001226 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1227 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001228
Jesse Barnes869184a2010-10-07 16:01:22 -07001229 if (!intel_dp_aux_handshake_required(intel_dp))
1230 return true;
1231
Chris Wilsonea5b2132010-08-04 13:50:23 +01001232 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001233 DP_TRAINING_PATTERN_SET,
1234 dp_train_pat);
1235
Chris Wilsonea5b2132010-08-04 13:50:23 +01001236 ret = intel_dp_aux_native_write(intel_dp,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001237 DP_TRAINING_LANE0_SET,
1238 intel_dp->train_set, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001239 if (ret != 4)
1240 return false;
1241
1242 return true;
1243}
1244
Jesse Barnes33a34e42010-09-08 12:42:02 -07001245/* Enable corresponding port and start training pattern 1 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001246static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001247intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001248{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001249 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001250 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson58e10eb2010-10-03 10:56:11 +01001251 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001252 int i;
1253 uint8_t voltage;
1254 bool clock_recovery = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001255 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001256 u32 reg;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001257 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001258
Keith Packardb99a9d92010-10-03 00:33:05 -07001259 /* Enable output, wait for it to become active */
1260 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1261 POSTING_READ(intel_dp->output_reg);
1262 intel_wait_for_vblank(dev, intel_crtc->pipe);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001263
Jesse Barnes869184a2010-10-07 16:01:22 -07001264 if (intel_dp_aux_handshake_required(intel_dp))
1265 /* Write the link configuration data */
1266 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1267 intel_dp->link_configuration,
1268 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001269
1270 DP |= DP_PORT_EN;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001271 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001272 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1273 else
1274 DP &= ~DP_LINK_TRAIN_MASK;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001275 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001276 voltage = 0xff;
1277 tries = 0;
1278 clock_recovery = false;
1279 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001280 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001281 uint32_t signal_levels;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001282 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001283 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001284 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1285 } else {
Jesse Barnes869184a2010-10-07 16:01:22 -07001286 signal_levels = intel_dp_signal_levels(intel_dp);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001287 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1288 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001289
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001290 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001291 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1292 else
1293 reg = DP | DP_LINK_TRAIN_PAT_1;
1294
Chris Wilsonea5b2132010-08-04 13:50:23 +01001295 if (!intel_dp_set_link_train(intel_dp, reg,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001296 DP_TRAINING_PATTERN_1))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001297 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001298 /* Set training pattern 1 */
1299
Jesse Barnes869184a2010-10-07 16:01:22 -07001300 udelay(500);
1301 if (intel_dp_aux_handshake_required(intel_dp)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001302 break;
Jesse Barnes869184a2010-10-07 16:01:22 -07001303 } else {
1304 if (!intel_dp_get_link_status(intel_dp))
1305 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001306
Jesse Barnes869184a2010-10-07 16:01:22 -07001307 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1308 clock_recovery = true;
1309 break;
1310 }
1311
1312 /* Check to see if we've tried the max voltage */
1313 for (i = 0; i < intel_dp->lane_count; i++)
1314 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1315 break;
1316 if (i == intel_dp->lane_count)
1317 break;
1318
1319 /* Check to see if we've tried the same voltage 5 times */
1320 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1321 ++tries;
1322 if (tries == 5)
1323 break;
1324 } else
1325 tries = 0;
1326 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1327
1328 /* Compute new intel_dp->train_set as requested by target */
1329 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001330 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001331 }
1332
Jesse Barnes33a34e42010-09-08 12:42:02 -07001333 intel_dp->DP = DP;
1334}
1335
1336static void
1337intel_dp_complete_link_train(struct intel_dp *intel_dp)
1338{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001339 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001340 struct drm_i915_private *dev_priv = dev->dev_private;
1341 bool channel_eq = false;
1342 int tries;
1343 u32 reg;
1344 uint32_t DP = intel_dp->DP;
1345
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001346 /* channel equalization */
1347 tries = 0;
1348 channel_eq = false;
1349 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001350 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001351 uint32_t signal_levels;
1352
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001353 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001354 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001355 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1356 } else {
Jesse Barnes869184a2010-10-07 16:01:22 -07001357 signal_levels = intel_dp_signal_levels(intel_dp);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001358 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1359 }
1360
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001361 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001362 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1363 else
1364 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001365
1366 /* channel eq pattern */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001367 if (!intel_dp_set_link_train(intel_dp, reg,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001368 DP_TRAINING_PATTERN_2))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001369 break;
1370
Jesse Barnes869184a2010-10-07 16:01:22 -07001371 udelay(500);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001372
Jesse Barnes869184a2010-10-07 16:01:22 -07001373 if (!intel_dp_aux_handshake_required(intel_dp)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001374 break;
Jesse Barnes869184a2010-10-07 16:01:22 -07001375 } else {
1376 if (!intel_dp_get_link_status(intel_dp))
1377 break;
1378
1379 if (intel_channel_eq_ok(intel_dp)) {
1380 channel_eq = true;
1381 break;
1382 }
1383
1384 /* Try 5 times */
1385 if (tries > 5)
1386 break;
1387
1388 /* Compute new intel_dp->train_set as requested by target */
1389 intel_get_adjust_train(intel_dp);
1390 ++tries;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001391 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001392 }
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001393 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001394 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1395 else
1396 reg = DP | DP_LINK_TRAIN_OFF;
1397
Chris Wilsonea5b2132010-08-04 13:50:23 +01001398 I915_WRITE(intel_dp->output_reg, reg);
1399 POSTING_READ(intel_dp->output_reg);
1400 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001401 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1402}
1403
1404static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001405intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001406{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001407 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001408 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001409 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001410
Zhao Yakui28c97732009-10-09 11:39:41 +08001411 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001412
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001413 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001414 DP &= ~DP_PLL_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001415 I915_WRITE(intel_dp->output_reg, DP);
1416 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001417 udelay(100);
1418 }
1419
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001420 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001421 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001422 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001423 } else {
1424 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001425 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001426 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01001427 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001428
Chris Wilsonfe255d02010-09-11 21:37:48 +01001429 msleep(17);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001430
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001431 if (is_edp(intel_dp))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001432 DP |= DP_LINK_TRAIN_OFF;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001433 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1434 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001435}
1436
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001437/*
1438 * According to DP spec
1439 * 5.1.2:
1440 * 1. Read DPCD
1441 * 2. Configure link according to Receiver Capabilities
1442 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1443 * 4. Check link status on receipt of hot-plug interrupt
1444 */
1445
1446static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001447intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001448{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001449 if (!intel_dp->base.base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001450 return;
1451
Jesse Barnes33a34e42010-09-08 12:42:02 -07001452 if (!intel_dp_get_link_status(intel_dp)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001453 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001454 return;
1455 }
1456
Jesse Barnes33a34e42010-09-08 12:42:02 -07001457 if (!intel_channel_eq_ok(intel_dp)) {
1458 intel_dp_start_link_train(intel_dp);
1459 intel_dp_complete_link_train(intel_dp);
1460 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001461}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001462
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001463static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001464ironlake_dp_detect(struct intel_dp *intel_dp)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001465{
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001466 enum drm_connector_status status;
1467
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001468 /* Can't disconnect eDP */
Jesse Barnes4d926462010-10-07 16:01:07 -07001469 if (is_edp(intel_dp))
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001470 return connector_status_connected;
1471
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001472 status = connector_status_disconnected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001473 if (intel_dp_aux_native_read(intel_dp,
1474 0x000, intel_dp->dpcd,
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001475 sizeof (intel_dp->dpcd))
1476 == sizeof(intel_dp->dpcd)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001477 if (intel_dp->dpcd[0] != 0)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001478 status = connector_status_connected;
1479 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001480 DRM_DEBUG_KMS("DPCD: %hx%hx%hx%hx\n", intel_dp->dpcd[0],
1481 intel_dp->dpcd[1], intel_dp->dpcd[2], intel_dp->dpcd[3]);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001482 return status;
1483}
1484
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001485static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001486g4x_dp_detect(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001487{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001488 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001489 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001490 enum drm_connector_status status;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001491 uint32_t temp, bit;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001492
Chris Wilsonea5b2132010-08-04 13:50:23 +01001493 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001494 case DP_B:
1495 bit = DPB_HOTPLUG_INT_STATUS;
1496 break;
1497 case DP_C:
1498 bit = DPC_HOTPLUG_INT_STATUS;
1499 break;
1500 case DP_D:
1501 bit = DPD_HOTPLUG_INT_STATUS;
1502 break;
1503 default:
1504 return connector_status_unknown;
1505 }
1506
1507 temp = I915_READ(PORT_HOTPLUG_STAT);
1508
1509 if ((temp & bit) == 0)
1510 return connector_status_disconnected;
1511
1512 status = connector_status_disconnected;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001513 if (intel_dp_aux_native_read(intel_dp, 0x000, intel_dp->dpcd,
Chris Wilsonea5b2132010-08-04 13:50:23 +01001514 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001515 {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001516 if (intel_dp->dpcd[0] != 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001517 status = connector_status_connected;
1518 }
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001519
Takashi Iwaidd2b3792010-10-26 17:14:36 +01001520 return status;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001521}
1522
1523/**
1524 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1525 *
1526 * \return true if DP port is connected.
1527 * \return false if DP port is disconnected.
1528 */
1529static enum drm_connector_status
1530intel_dp_detect(struct drm_connector *connector, bool force)
1531{
1532 struct intel_dp *intel_dp = intel_attached_dp(connector);
1533 struct drm_device *dev = intel_dp->base.base.dev;
1534 enum drm_connector_status status;
1535 struct edid *edid = NULL;
1536
1537 intel_dp->has_audio = false;
1538
1539 if (HAS_PCH_SPLIT(dev))
1540 status = ironlake_dp_detect(intel_dp);
1541 else
1542 status = g4x_dp_detect(intel_dp);
1543 if (status != connector_status_connected)
1544 return status;
1545
Chris Wilsonf6849602010-09-19 09:29:33 +01001546 if (intel_dp->force_audio) {
1547 intel_dp->has_audio = intel_dp->force_audio > 0;
1548 } else {
1549 edid = drm_get_edid(connector, &intel_dp->adapter);
1550 if (edid) {
1551 intel_dp->has_audio = drm_detect_monitor_audio(edid);
1552 connector->display_info.raw_edid = NULL;
1553 kfree(edid);
1554 }
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08001555 }
1556
1557 return connector_status_connected;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001558}
1559
1560static int intel_dp_get_modes(struct drm_connector *connector)
1561{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001562 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001563 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001564 struct drm_i915_private *dev_priv = dev->dev_private;
1565 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001566
1567 /* We should parse the EDID data and find out if it has an audio sink
1568 */
1569
Chris Wilsonf899fc62010-07-20 15:44:45 -07001570 ret = intel_ddc_get_modes(connector, &intel_dp->adapter);
Zhao Yakuib9efc482010-07-19 09:43:11 +01001571 if (ret) {
Jesse Barnes4d926462010-10-07 16:01:07 -07001572 if (is_edp(intel_dp) && !dev_priv->panel_fixed_mode) {
Zhao Yakuib9efc482010-07-19 09:43:11 +01001573 struct drm_display_mode *newmode;
1574 list_for_each_entry(newmode, &connector->probed_modes,
1575 head) {
1576 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1577 dev_priv->panel_fixed_mode =
1578 drm_mode_duplicate(dev, newmode);
1579 break;
1580 }
1581 }
1582 }
1583
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001584 return ret;
Zhao Yakuib9efc482010-07-19 09:43:11 +01001585 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001586
1587 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Jesse Barnes4d926462010-10-07 16:01:07 -07001588 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001589 if (dev_priv->panel_fixed_mode != NULL) {
1590 struct drm_display_mode *mode;
1591 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1592 drm_mode_probed_add(connector, mode);
1593 return 1;
1594 }
1595 }
1596 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001597}
1598
Chris Wilsonf6849602010-09-19 09:29:33 +01001599static int
1600intel_dp_set_property(struct drm_connector *connector,
1601 struct drm_property *property,
1602 uint64_t val)
1603{
1604 struct intel_dp *intel_dp = intel_attached_dp(connector);
1605 int ret;
1606
1607 ret = drm_connector_property_set_value(connector, property, val);
1608 if (ret)
1609 return ret;
1610
1611 if (property == intel_dp->force_audio_property) {
1612 if (val == intel_dp->force_audio)
1613 return 0;
1614
1615 intel_dp->force_audio = val;
1616
1617 if (val > 0 && intel_dp->has_audio)
1618 return 0;
1619 if (val < 0 && !intel_dp->has_audio)
1620 return 0;
1621
1622 intel_dp->has_audio = val > 0;
1623 goto done;
1624 }
1625
1626 return -EINVAL;
1627
1628done:
1629 if (intel_dp->base.base.crtc) {
1630 struct drm_crtc *crtc = intel_dp->base.base.crtc;
1631 drm_crtc_helper_set_mode(crtc, &crtc->mode,
1632 crtc->x, crtc->y,
1633 crtc->fb);
1634 }
1635
1636 return 0;
1637}
1638
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001639static void
1640intel_dp_destroy (struct drm_connector *connector)
1641{
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001642 drm_sysfs_connector_remove(connector);
1643 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001644 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001645}
1646
Daniel Vetter24d05922010-08-20 18:08:28 +02001647static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1648{
1649 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1650
1651 i2c_del_adapter(&intel_dp->adapter);
1652 drm_encoder_cleanup(encoder);
1653 kfree(intel_dp);
1654}
1655
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001656static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1657 .dpms = intel_dp_dpms,
1658 .mode_fixup = intel_dp_mode_fixup,
Jesse Barnesd240f202010-08-13 15:43:26 -07001659 .prepare = intel_dp_prepare,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001660 .mode_set = intel_dp_mode_set,
Jesse Barnesd240f202010-08-13 15:43:26 -07001661 .commit = intel_dp_commit,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001662};
1663
1664static const struct drm_connector_funcs intel_dp_connector_funcs = {
1665 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001666 .detect = intel_dp_detect,
1667 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilsonf6849602010-09-19 09:29:33 +01001668 .set_property = intel_dp_set_property,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001669 .destroy = intel_dp_destroy,
1670};
1671
1672static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1673 .get_modes = intel_dp_get_modes,
1674 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01001675 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001676};
1677
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001678static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02001679 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001680};
1681
Chris Wilson995b6762010-08-20 13:23:26 +01001682static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001683intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07001684{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001685 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Keith Packardc8110e52009-05-06 11:51:10 -07001686
Chris Wilsonea5b2132010-08-04 13:50:23 +01001687 if (intel_dp->dpms_mode == DRM_MODE_DPMS_ON)
1688 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07001689}
1690
Zhenyu Wange3421a12010-04-08 09:43:27 +08001691/* Return which DP Port should be selected for Transcoder DP control */
1692int
1693intel_trans_dp_port_sel (struct drm_crtc *crtc)
1694{
1695 struct drm_device *dev = crtc->dev;
1696 struct drm_mode_config *mode_config = &dev->mode_config;
1697 struct drm_encoder *encoder;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001698
1699 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001700 struct intel_dp *intel_dp;
1701
Dan Carpenterd8201ab2010-05-07 10:39:00 +02001702 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08001703 continue;
1704
Chris Wilsonea5b2132010-08-04 13:50:23 +01001705 intel_dp = enc_to_intel_dp(encoder);
1706 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
1707 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001708 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001709
Zhenyu Wange3421a12010-04-08 09:43:27 +08001710 return -1;
1711}
1712
Zhao Yakui36e83a12010-06-12 14:32:21 +08001713/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04001714bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08001715{
1716 struct drm_i915_private *dev_priv = dev->dev_private;
1717 struct child_device_config *p_child;
1718 int i;
1719
1720 if (!dev_priv->child_dev_num)
1721 return false;
1722
1723 for (i = 0; i < dev_priv->child_dev_num; i++) {
1724 p_child = dev_priv->child_dev + i;
1725
1726 if (p_child->dvo_port == PORT_IDPD &&
1727 p_child->device_type == DEVICE_TYPE_eDP)
1728 return true;
1729 }
1730 return false;
1731}
1732
Chris Wilsonf6849602010-09-19 09:29:33 +01001733static void
1734intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
1735{
1736 struct drm_device *dev = connector->dev;
1737
1738 intel_dp->force_audio_property =
1739 drm_property_create(dev, DRM_MODE_PROP_RANGE, "force_audio", 2);
1740 if (intel_dp->force_audio_property) {
1741 intel_dp->force_audio_property->values[0] = -1;
1742 intel_dp->force_audio_property->values[1] = 1;
1743 drm_connector_attach_property(connector, intel_dp->force_audio_property, 0);
1744 }
1745}
1746
Keith Packardc8110e52009-05-06 11:51:10 -07001747void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001748intel_dp_init(struct drm_device *dev, int output_reg)
1749{
1750 struct drm_i915_private *dev_priv = dev->dev_private;
1751 struct drm_connector *connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001752 struct intel_dp *intel_dp;
Eric Anholt21d40d32010-03-25 11:11:14 -07001753 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001754 struct intel_connector *intel_connector;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001755 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04001756 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001757
Chris Wilsonea5b2132010-08-04 13:50:23 +01001758 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
1759 if (!intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001760 return;
1761
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001762 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1763 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001764 kfree(intel_dp);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001765 return;
1766 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001767 intel_encoder = &intel_dp->base;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001768
Chris Wilsonea5b2132010-08-04 13:50:23 +01001769 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04001770 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01001771 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04001772
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001773 if (output_reg == DP_A || is_pch_edp(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04001774 type = DRM_MODE_CONNECTOR_eDP;
1775 intel_encoder->type = INTEL_OUTPUT_EDP;
1776 } else {
1777 type = DRM_MODE_CONNECTOR_DisplayPort;
1778 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1779 }
1780
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001781 connector = &intel_connector->base;
Adam Jacksonb3295302010-07-16 14:46:28 -04001782 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001783 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1784
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001785 connector->polled = DRM_CONNECTOR_POLL_HPD;
1786
Zhao Yakui652af9d2009-12-02 10:03:33 +08001787 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07001788 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001789 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07001790 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001791 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07001792 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08001793
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001794 if (is_edp(intel_dp))
Eric Anholt21d40d32010-03-25 11:11:14 -07001795 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08001796
Eric Anholt21d40d32010-03-25 11:11:14 -07001797 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001798 connector->interlace_allowed = true;
1799 connector->doublescan_allowed = 0;
1800
Chris Wilsonea5b2132010-08-04 13:50:23 +01001801 intel_dp->output_reg = output_reg;
1802 intel_dp->has_audio = false;
1803 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001804
Chris Wilson4ef69c72010-09-09 15:14:28 +01001805 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001806 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001807 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001808
Chris Wilsondf0e9242010-09-09 16:20:55 +01001809 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001810 drm_sysfs_connector_add(connector);
1811
1812 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001813 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001814 case DP_A:
1815 name = "DPDDC-A";
1816 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001817 case DP_B:
1818 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001819 dev_priv->hotplug_supported_mask |=
1820 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001821 name = "DPDDC-B";
1822 break;
1823 case DP_C:
1824 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001825 dev_priv->hotplug_supported_mask |=
1826 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001827 name = "DPDDC-C";
1828 break;
1829 case DP_D:
1830 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001831 dev_priv->hotplug_supported_mask |=
1832 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001833 name = "DPDDC-D";
1834 break;
1835 }
1836
Chris Wilsonea5b2132010-08-04 13:50:23 +01001837 intel_dp_i2c_init(intel_dp, intel_connector, name);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001838
Jesse Barnes89667382010-10-07 16:01:21 -07001839 /* Cache some DPCD data in the eDP case */
1840 if (is_edp(intel_dp)) {
1841 int ret;
1842 bool was_on;
1843
1844 was_on = ironlake_edp_panel_on(intel_dp);
1845 ret = intel_dp_aux_native_read(intel_dp, DP_DPCD_REV,
1846 intel_dp->dpcd,
1847 sizeof(intel_dp->dpcd));
1848 if (ret == sizeof(intel_dp->dpcd)) {
1849 if (intel_dp->dpcd[0] >= 0x11)
1850 dev_priv->no_aux_handshake = intel_dp->dpcd[3] &
1851 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
1852 } else {
1853 DRM_ERROR("failed to retrieve link info\n");
1854 }
1855 if (!was_on)
1856 ironlake_edp_panel_off(dev);
1857 }
1858
Eric Anholt21d40d32010-03-25 11:11:14 -07001859 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001860
Jesse Barnes4d926462010-10-07 16:01:07 -07001861 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001862 /* initialize panel mode from VBT if available for eDP */
1863 if (dev_priv->lfp_lvds_vbt_mode) {
1864 dev_priv->panel_fixed_mode =
1865 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1866 if (dev_priv->panel_fixed_mode) {
1867 dev_priv->panel_fixed_mode->type |=
1868 DRM_MODE_TYPE_PREFERRED;
1869 }
1870 }
1871 }
1872
Chris Wilsonf6849602010-09-19 09:29:33 +01001873 intel_dp_add_properties(intel_dp, connector);
1874
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001875 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1876 * 0xd. Failure to do so will result in spurious interrupts being
1877 * generated on the port when a cable is not attached.
1878 */
1879 if (IS_G4X(dev) && !IS_GM45(dev)) {
1880 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
1881 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
1882 }
1883}