blob: 1b736637e13e4d643c67550c760dab063e80ead9 [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;
Keith Packardc8110e52009-05-06 11:51:10 -070051 int dpms_mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070052 uint8_t link_bw;
53 uint8_t lane_count;
54 uint8_t dpcd[4];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070055 struct i2c_adapter adapter;
56 struct i2c_algo_dp_aux_data algo;
Adam Jacksonf0917372010-07-16 14:46:27 -040057 bool is_pch_edp;
Jesse Barnes33a34e42010-09-08 12:42:02 -070058 uint8_t train_set[4];
59 uint8_t link_status[DP_LINK_STATUS_SIZE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070060};
61
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070062/**
63 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
64 * @intel_dp: DP struct
65 *
66 * If a CPU or PCH DP output is attached to an eDP panel, this function
67 * will return true, and false otherwise.
68 */
69static bool is_edp(struct intel_dp *intel_dp)
70{
71 return intel_dp->base.type == INTEL_OUTPUT_EDP;
72}
73
74/**
75 * is_pch_edp - is the port on the PCH and attached to an eDP panel?
76 * @intel_dp: DP struct
77 *
78 * Returns true if the given DP struct corresponds to a PCH DP port attached
79 * to an eDP panel, false otherwise. Helpful for determining whether we
80 * may need FDI resources for a given DP output or not.
81 */
82static bool is_pch_edp(struct intel_dp *intel_dp)
83{
84 return intel_dp->is_pch_edp;
85}
86
Chris Wilsonea5b2132010-08-04 13:50:23 +010087static struct intel_dp *enc_to_intel_dp(struct drm_encoder *encoder)
88{
Chris Wilson4ef69c72010-09-09 15:14:28 +010089 return container_of(encoder, struct intel_dp, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010090}
Keith Packarda4fc5ed2009-04-07 16:16:42 -070091
Chris Wilsondf0e9242010-09-09 16:20:55 +010092static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
93{
94 return container_of(intel_attached_encoder(connector),
95 struct intel_dp, base);
96}
97
Jesse Barnes33a34e42010-09-08 12:42:02 -070098static void intel_dp_start_link_train(struct intel_dp *intel_dp);
99static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100100static void intel_dp_link_down(struct intel_dp *intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700101
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800102void
Eric Anholt21d40d32010-03-25 11:11:14 -0700103intel_edp_link_config (struct intel_encoder *intel_encoder,
Chris Wilsonea5b2132010-08-04 13:50:23 +0100104 int *lane_num, int *link_bw)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800105{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100106 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800107
Chris Wilsonea5b2132010-08-04 13:50:23 +0100108 *lane_num = intel_dp->lane_count;
109 if (intel_dp->link_bw == DP_LINK_BW_1_62)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800110 *link_bw = 162000;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100111 else if (intel_dp->link_bw == DP_LINK_BW_2_7)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800112 *link_bw = 270000;
113}
114
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700115static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100116intel_dp_max_lane_count(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700117{
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700118 int max_lane_count = 4;
119
Chris Wilsonea5b2132010-08-04 13:50:23 +0100120 if (intel_dp->dpcd[0] >= 0x11) {
121 max_lane_count = intel_dp->dpcd[2] & 0x1f;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700122 switch (max_lane_count) {
123 case 1: case 2: case 4:
124 break;
125 default:
126 max_lane_count = 4;
127 }
128 }
129 return max_lane_count;
130}
131
132static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100133intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700134{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100135 int max_link_bw = intel_dp->dpcd[1];
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700136
137 switch (max_link_bw) {
138 case DP_LINK_BW_1_62:
139 case DP_LINK_BW_2_7:
140 break;
141 default:
142 max_link_bw = DP_LINK_BW_1_62;
143 break;
144 }
145 return max_link_bw;
146}
147
148static int
149intel_dp_link_clock(uint8_t link_bw)
150{
151 if (link_bw == DP_LINK_BW_2_7)
152 return 270000;
153 else
154 return 162000;
155}
156
157/* I think this is a fiction */
158static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100159intel_dp_link_required(struct drm_device *dev, struct intel_dp *intel_dp, int pixel_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700160{
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800161 struct drm_i915_private *dev_priv = dev->dev_private;
162
Jesse Barnes4d926462010-10-07 16:01:07 -0700163 if (is_edp(intel_dp))
Chris Wilson5ceb0f92010-09-24 10:24:28 +0100164 return (pixel_clock * dev_priv->edp.bpp + 7) / 8;
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800165 else
166 return pixel_clock * 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700167}
168
169static int
Dave Airliefe27d532010-06-30 11:46:17 +1000170intel_dp_max_data_rate(int max_link_clock, int max_lanes)
171{
172 return (max_link_clock * max_lanes * 8) / 10;
173}
174
175static int
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700176intel_dp_mode_valid(struct drm_connector *connector,
177 struct drm_display_mode *mode)
178{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100179 struct intel_dp *intel_dp = intel_attached_dp(connector);
Zhao Yakui7de56f42010-07-19 09:43:14 +0100180 struct drm_device *dev = connector->dev;
181 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100182 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
183 int max_lanes = intel_dp_max_lane_count(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700184
Jesse Barnes4d926462010-10-07 16:01:07 -0700185 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Zhao Yakui7de56f42010-07-19 09:43:14 +0100186 if (mode->hdisplay > dev_priv->panel_fixed_mode->hdisplay)
187 return MODE_PANEL;
188
189 if (mode->vdisplay > dev_priv->panel_fixed_mode->vdisplay)
190 return MODE_PANEL;
191 }
192
Dave Airliefe27d532010-06-30 11:46:17 +1000193 /* only refuse the mode on non eDP since we have seen some wierd eDP panels
194 which are outside spec tolerances but somehow work by magic */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700195 if (!is_edp(intel_dp) &&
Chris Wilsonea5b2132010-08-04 13:50:23 +0100196 (intel_dp_link_required(connector->dev, intel_dp, mode->clock)
Dave Airliefe27d532010-06-30 11:46:17 +1000197 > intel_dp_max_data_rate(max_link_clock, max_lanes)))
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700198 return MODE_CLOCK_HIGH;
199
200 if (mode->clock < 10000)
201 return MODE_CLOCK_LOW;
202
203 return MODE_OK;
204}
205
206static uint32_t
207pack_aux(uint8_t *src, int src_bytes)
208{
209 int i;
210 uint32_t v = 0;
211
212 if (src_bytes > 4)
213 src_bytes = 4;
214 for (i = 0; i < src_bytes; i++)
215 v |= ((uint32_t) src[i]) << ((3-i) * 8);
216 return v;
217}
218
219static void
220unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
221{
222 int i;
223 if (dst_bytes > 4)
224 dst_bytes = 4;
225 for (i = 0; i < dst_bytes; i++)
226 dst[i] = src >> ((3-i) * 8);
227}
228
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700229/* hrawclock is 1/4 the FSB frequency */
230static int
231intel_hrawclk(struct drm_device *dev)
232{
233 struct drm_i915_private *dev_priv = dev->dev_private;
234 uint32_t clkcfg;
235
236 clkcfg = I915_READ(CLKCFG);
237 switch (clkcfg & CLKCFG_FSB_MASK) {
238 case CLKCFG_FSB_400:
239 return 100;
240 case CLKCFG_FSB_533:
241 return 133;
242 case CLKCFG_FSB_667:
243 return 166;
244 case CLKCFG_FSB_800:
245 return 200;
246 case CLKCFG_FSB_1067:
247 return 266;
248 case CLKCFG_FSB_1333:
249 return 333;
250 /* these two are just a guess; one of them might be right */
251 case CLKCFG_FSB_1600:
252 case CLKCFG_FSB_1600_ALT:
253 return 400;
254 default:
255 return 133;
256 }
257}
258
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700259static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100260intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700261 uint8_t *send, int send_bytes,
262 uint8_t *recv, int recv_size)
263{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100264 uint32_t output_reg = intel_dp->output_reg;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100265 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700266 struct drm_i915_private *dev_priv = dev->dev_private;
267 uint32_t ch_ctl = output_reg + 0x10;
268 uint32_t ch_data = ch_ctl + 4;
269 int i;
270 int recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700271 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700272 uint32_t aux_clock_divider;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800273 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700274
275 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700276 * and would like to run at 2MHz. So, take the
277 * hrawclk value and divide by 2 and use that
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700278 *
279 * Note that PCH attached eDP panels should use a 125MHz input
280 * clock divider.
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700281 */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700282 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +0800283 if (IS_GEN6(dev))
284 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
285 else
286 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
287 } else if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500288 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800289 else
290 aux_clock_divider = intel_hrawclk(dev) / 2;
291
Zhenyu Wange3421a12010-04-08 09:43:27 +0800292 if (IS_GEN6(dev))
293 precharge = 3;
294 else
295 precharge = 5;
296
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100297 if (I915_READ(ch_ctl) & DP_AUX_CH_CTL_SEND_BUSY) {
298 DRM_ERROR("dp_aux_ch not started status 0x%08x\n",
299 I915_READ(ch_ctl));
300 return -EBUSY;
301 }
302
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700303 /* Must try at least 3 times according to DP spec */
304 for (try = 0; try < 5; try++) {
305 /* Load the send data into the aux channel data registers */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100306 for (i = 0; i < send_bytes; i += 4)
307 I915_WRITE(ch_data + i,
308 pack_aux(send + i, send_bytes - i));
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700309
310 /* Send the command and wait for it to complete */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100311 I915_WRITE(ch_ctl,
312 DP_AUX_CH_CTL_SEND_BUSY |
313 DP_AUX_CH_CTL_TIME_OUT_400us |
314 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
315 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
316 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
317 DP_AUX_CH_CTL_DONE |
318 DP_AUX_CH_CTL_TIME_OUT_ERROR |
319 DP_AUX_CH_CTL_RECEIVE_ERROR);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700320 for (;;) {
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700321 status = I915_READ(ch_ctl);
322 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
323 break;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100324 udelay(100);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700325 }
326
327 /* Clear done status and any errors */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100328 I915_WRITE(ch_ctl,
329 status |
330 DP_AUX_CH_CTL_DONE |
331 DP_AUX_CH_CTL_TIME_OUT_ERROR |
332 DP_AUX_CH_CTL_RECEIVE_ERROR);
333 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700334 break;
335 }
336
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700337 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700338 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700339 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700340 }
341
342 /* Check for timeout or receive error.
343 * Timeouts occur when the sink is not connected
344 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700345 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700346 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700347 return -EIO;
348 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700349
350 /* Timeouts occur when the device isn't connected, so they're
351 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700352 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800353 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700354 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700355 }
356
357 /* Unload any bytes sent back from the other side */
358 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
359 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700360 if (recv_bytes > recv_size)
361 recv_bytes = recv_size;
362
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100363 for (i = 0; i < recv_bytes; i += 4)
364 unpack_aux(I915_READ(ch_data + i),
365 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700366
367 return recv_bytes;
368}
369
370/* Write data to the aux channel in native mode */
371static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100372intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700373 uint16_t address, uint8_t *send, int send_bytes)
374{
375 int ret;
376 uint8_t msg[20];
377 int msg_bytes;
378 uint8_t ack;
379
380 if (send_bytes > 16)
381 return -1;
382 msg[0] = AUX_NATIVE_WRITE << 4;
383 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800384 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700385 msg[3] = send_bytes - 1;
386 memcpy(&msg[4], send, send_bytes);
387 msg_bytes = send_bytes + 4;
388 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100389 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700390 if (ret < 0)
391 return ret;
392 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
393 break;
394 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
395 udelay(100);
396 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700397 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700398 }
399 return send_bytes;
400}
401
402/* Write a single byte to the aux channel in native mode */
403static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100404intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700405 uint16_t address, uint8_t byte)
406{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100407 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700408}
409
410/* read bytes from a native aux channel */
411static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100412intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700413 uint16_t address, uint8_t *recv, int recv_bytes)
414{
415 uint8_t msg[4];
416 int msg_bytes;
417 uint8_t reply[20];
418 int reply_bytes;
419 uint8_t ack;
420 int ret;
421
422 msg[0] = AUX_NATIVE_READ << 4;
423 msg[1] = address >> 8;
424 msg[2] = address & 0xff;
425 msg[3] = recv_bytes - 1;
426
427 msg_bytes = 4;
428 reply_bytes = recv_bytes + 1;
429
430 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100431 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700432 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700433 if (ret == 0)
434 return -EPROTO;
435 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700436 return ret;
437 ack = reply[0];
438 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
439 memcpy(recv, reply + 1, ret - 1);
440 return ret - 1;
441 }
442 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
443 udelay(100);
444 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700445 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700446 }
447}
448
449static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000450intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
451 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700452{
Dave Airlieab2c0672009-12-04 10:55:24 +1000453 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100454 struct intel_dp *intel_dp = container_of(adapter,
455 struct intel_dp,
456 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000457 uint16_t address = algo_data->address;
458 uint8_t msg[5];
459 uint8_t reply[2];
460 int msg_bytes;
461 int reply_bytes;
462 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700463
Dave Airlieab2c0672009-12-04 10:55:24 +1000464 /* Set up the command byte */
465 if (mode & MODE_I2C_READ)
466 msg[0] = AUX_I2C_READ << 4;
467 else
468 msg[0] = AUX_I2C_WRITE << 4;
469
470 if (!(mode & MODE_I2C_STOP))
471 msg[0] |= AUX_I2C_MOT << 4;
472
473 msg[1] = address >> 8;
474 msg[2] = address;
475
476 switch (mode) {
477 case MODE_I2C_WRITE:
478 msg[3] = 0;
479 msg[4] = write_byte;
480 msg_bytes = 5;
481 reply_bytes = 1;
482 break;
483 case MODE_I2C_READ:
484 msg[3] = 0;
485 msg_bytes = 4;
486 reply_bytes = 2;
487 break;
488 default:
489 msg_bytes = 3;
490 reply_bytes = 1;
491 break;
492 }
493
494 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100495 ret = intel_dp_aux_ch(intel_dp,
Dave Airlieab2c0672009-12-04 10:55:24 +1000496 msg, msg_bytes,
497 reply, reply_bytes);
498 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000499 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000500 return ret;
501 }
502 switch (reply[0] & AUX_I2C_REPLY_MASK) {
503 case AUX_I2C_REPLY_ACK:
504 if (mode == MODE_I2C_READ) {
505 *read_byte = reply[1];
506 }
507 return reply_bytes - 1;
508 case AUX_I2C_REPLY_NACK:
Dave Airlie3ff99162009-12-08 14:03:47 +1000509 DRM_DEBUG_KMS("aux_ch nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000510 return -EREMOTEIO;
511 case AUX_I2C_REPLY_DEFER:
Dave Airlie3ff99162009-12-08 14:03:47 +1000512 DRM_DEBUG_KMS("aux_ch defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000513 udelay(100);
514 break;
515 default:
516 DRM_ERROR("aux_ch invalid reply 0x%02x\n", reply[0]);
517 return -EREMOTEIO;
518 }
519 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700520}
521
522static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100523intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800524 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700525{
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800526 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100527 intel_dp->algo.running = false;
528 intel_dp->algo.address = 0;
529 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700530
Chris Wilsonea5b2132010-08-04 13:50:23 +0100531 memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
532 intel_dp->adapter.owner = THIS_MODULE;
533 intel_dp->adapter.class = I2C_CLASS_DDC;
534 strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
535 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
536 intel_dp->adapter.algo_data = &intel_dp->algo;
537 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
538
539 return i2c_dp_aux_add_bus(&intel_dp->adapter);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700540}
541
542static bool
543intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
544 struct drm_display_mode *adjusted_mode)
545{
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100546 struct drm_device *dev = encoder->dev;
547 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100548 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700549 int lane_count, clock;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100550 int max_lane_count = intel_dp_max_lane_count(intel_dp);
551 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700552 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
553
Jesse Barnes4d926462010-10-07 16:01:07 -0700554 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100555 intel_fixed_panel_mode(dev_priv->panel_fixed_mode, adjusted_mode);
556 intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
557 mode, adjusted_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100558 /*
559 * the mode->clock is used to calculate the Data&Link M/N
560 * of the pipe. For the eDP the fixed clock should be used.
561 */
562 mode->clock = dev_priv->panel_fixed_mode->clock;
563 }
564
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700565 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
566 for (clock = 0; clock <= max_clock; clock++) {
Dave Airliefe27d532010-06-30 11:46:17 +1000567 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700568
Chris Wilsonea5b2132010-08-04 13:50:23 +0100569 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800570 <= link_avail) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100571 intel_dp->link_bw = bws[clock];
572 intel_dp->lane_count = lane_count;
573 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Zhao Yakui28c97732009-10-09 11:39:41 +0800574 DRM_DEBUG_KMS("Display port link bw %02x lane "
575 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100576 intel_dp->link_bw, intel_dp->lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700577 adjusted_mode->clock);
578 return true;
579 }
580 }
581 }
Dave Airliefe27d532010-06-30 11:46:17 +1000582
Jesse Barnes4d926462010-10-07 16:01:07 -0700583 if (is_edp(intel_dp)) {
Dave Airliefe27d532010-06-30 11:46:17 +1000584 /* okay we failed just pick the highest */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100585 intel_dp->lane_count = max_lane_count;
586 intel_dp->link_bw = bws[max_clock];
587 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Dave Airliefe27d532010-06-30 11:46:17 +1000588 DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
589 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100590 intel_dp->link_bw, intel_dp->lane_count,
Dave Airliefe27d532010-06-30 11:46:17 +1000591 adjusted_mode->clock);
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100592
Dave Airliefe27d532010-06-30 11:46:17 +1000593 return true;
594 }
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100595
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700596 return false;
597}
598
599struct intel_dp_m_n {
600 uint32_t tu;
601 uint32_t gmch_m;
602 uint32_t gmch_n;
603 uint32_t link_m;
604 uint32_t link_n;
605};
606
607static void
608intel_reduce_ratio(uint32_t *num, uint32_t *den)
609{
610 while (*num > 0xffffff || *den > 0xffffff) {
611 *num >>= 1;
612 *den >>= 1;
613 }
614}
615
616static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800617intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700618 int nlanes,
619 int pixel_clock,
620 int link_clock,
621 struct intel_dp_m_n *m_n)
622{
623 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800624 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700625 m_n->gmch_n = link_clock * nlanes;
626 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
627 m_n->link_m = pixel_clock;
628 m_n->link_n = link_clock;
629 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
630}
631
Zhao Yakui36e83a12010-06-12 14:32:21 +0800632bool intel_pch_has_edp(struct drm_crtc *crtc)
633{
634 struct drm_device *dev = crtc->dev;
635 struct drm_mode_config *mode_config = &dev->mode_config;
636 struct drm_encoder *encoder;
637
638 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100639 struct intel_dp *intel_dp;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800640
Chris Wilsonea5b2132010-08-04 13:50:23 +0100641 if (encoder->crtc != crtc)
Zhao Yakui36e83a12010-06-12 14:32:21 +0800642 continue;
643
Chris Wilsonea5b2132010-08-04 13:50:23 +0100644 intel_dp = enc_to_intel_dp(encoder);
645 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
646 return intel_dp->is_pch_edp;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800647 }
648 return false;
649}
650
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700651void
652intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
653 struct drm_display_mode *adjusted_mode)
654{
655 struct drm_device *dev = crtc->dev;
656 struct drm_mode_config *mode_config = &dev->mode_config;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800657 struct drm_encoder *encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700658 struct drm_i915_private *dev_priv = dev->dev_private;
659 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Zhao Yakui36e83a12010-06-12 14:32:21 +0800660 int lane_count = 4, bpp = 24;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700661 struct intel_dp_m_n m_n;
662
663 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700664 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700665 */
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800666 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100667 struct intel_dp *intel_dp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700668
Dan Carpenterd8201ab2010-05-07 10:39:00 +0200669 if (encoder->crtc != crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700670 continue;
671
Chris Wilsonea5b2132010-08-04 13:50:23 +0100672 intel_dp = enc_to_intel_dp(encoder);
673 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
674 lane_count = intel_dp->lane_count;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700675 if (is_pch_edp(intel_dp))
Chris Wilson5ceb0f92010-09-24 10:24:28 +0100676 bpp = dev_priv->edp.bpp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700677 break;
678 }
679 }
680
681 /*
682 * Compute the GMCH and Link ratios. The '3' here is
683 * the number of bytes_per_pixel post-LUT, which we always
684 * set up for 8-bits of R/G/B, or 3 bytes total.
685 */
Zhao Yakui36e83a12010-06-12 14:32:21 +0800686 intel_dp_compute_m_n(bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700687 mode->clock, adjusted_mode->clock, &m_n);
688
Eric Anholtc619eed2010-01-28 16:45:52 -0800689 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800690 if (intel_crtc->pipe == 0) {
691 I915_WRITE(TRANSA_DATA_M1,
692 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
693 m_n.gmch_m);
694 I915_WRITE(TRANSA_DATA_N1, m_n.gmch_n);
695 I915_WRITE(TRANSA_DP_LINK_M1, m_n.link_m);
696 I915_WRITE(TRANSA_DP_LINK_N1, m_n.link_n);
697 } else {
698 I915_WRITE(TRANSB_DATA_M1,
699 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
700 m_n.gmch_m);
701 I915_WRITE(TRANSB_DATA_N1, m_n.gmch_n);
702 I915_WRITE(TRANSB_DP_LINK_M1, m_n.link_m);
703 I915_WRITE(TRANSB_DP_LINK_N1, m_n.link_n);
704 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700705 } else {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800706 if (intel_crtc->pipe == 0) {
707 I915_WRITE(PIPEA_GMCH_DATA_M,
708 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
709 m_n.gmch_m);
710 I915_WRITE(PIPEA_GMCH_DATA_N,
711 m_n.gmch_n);
712 I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
713 I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
714 } else {
715 I915_WRITE(PIPEB_GMCH_DATA_M,
716 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
717 m_n.gmch_m);
718 I915_WRITE(PIPEB_GMCH_DATA_N,
719 m_n.gmch_n);
720 I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
721 I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
722 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700723 }
724}
725
726static void
727intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
728 struct drm_display_mode *adjusted_mode)
729{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800730 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100731 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +0100732 struct drm_crtc *crtc = intel_dp->base.base.crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
734
Chris Wilsonea5b2132010-08-04 13:50:23 +0100735 intel_dp->DP = (DP_VOLTAGE_0_4 |
Adam Jackson9c9e7922010-04-05 17:57:59 -0400736 DP_PRE_EMPHASIS_0);
737
738 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100739 intel_dp->DP |= DP_SYNC_HS_HIGH;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400740 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100741 intel_dp->DP |= DP_SYNC_VS_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700742
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700743 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100744 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800745 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100746 intel_dp->DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700747
Chris Wilsonea5b2132010-08-04 13:50:23 +0100748 switch (intel_dp->lane_count) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700749 case 1:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100750 intel_dp->DP |= DP_PORT_WIDTH_1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700751 break;
752 case 2:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100753 intel_dp->DP |= DP_PORT_WIDTH_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700754 break;
755 case 4:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100756 intel_dp->DP |= DP_PORT_WIDTH_4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700757 break;
758 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100759 if (intel_dp->has_audio)
760 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700761
Chris Wilsonea5b2132010-08-04 13:50:23 +0100762 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
763 intel_dp->link_configuration[0] = intel_dp->link_bw;
764 intel_dp->link_configuration[1] = intel_dp->lane_count;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700765
766 /*
Adam Jackson9962c922010-05-13 14:45:42 -0400767 * Check for DPCD version > 1.1 and enhanced framing support
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700768 */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100769 if (intel_dp->dpcd[0] >= 0x11 && (intel_dp->dpcd[2] & DP_ENHANCED_FRAME_CAP)) {
770 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
771 intel_dp->DP |= DP_ENHANCED_FRAMING;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700772 }
773
Zhenyu Wange3421a12010-04-08 09:43:27 +0800774 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
775 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100776 intel_dp->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800777
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700778 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800779 /* don't miss out required setting for eDP */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100780 intel_dp->DP |= DP_PLL_ENABLE;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800781 if (adjusted_mode->clock < 200000)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100782 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800783 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100784 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800785 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700786}
787
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700788/* Returns true if the panel was already on when called */
789static bool ironlake_edp_panel_on (struct drm_device *dev)
Jesse Barnes9934c132010-07-22 13:18:19 -0700790{
791 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson913d8d12010-08-07 11:01:35 +0100792 u32 pp;
Jesse Barnes9934c132010-07-22 13:18:19 -0700793
Chris Wilson913d8d12010-08-07 11:01:35 +0100794 if (I915_READ(PCH_PP_STATUS) & PP_ON)
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700795 return true;
Jesse Barnes9934c132010-07-22 13:18:19 -0700796
797 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700798
799 /* ILK workaround: disable reset around power sequence */
800 pp &= ~PANEL_POWER_RESET;
801 I915_WRITE(PCH_PP_CONTROL, pp);
802 POSTING_READ(PCH_PP_CONTROL);
803
Jesse Barnes4d12fe02010-09-10 10:46:45 -0700804 pp |= POWER_TARGET_ON;
Jesse Barnes9934c132010-07-22 13:18:19 -0700805 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes9934c132010-07-22 13:18:19 -0700806
Hette Visser27d64332010-09-24 10:51:30 +0100807 /* Ouch. We need to wait here for some panels, like Dell e6510
808 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
809 */
810 msleep(300);
811
Chris Wilson481b6af2010-08-23 17:43:35 +0100812 if (wait_for(I915_READ(PCH_PP_STATUS) & PP_ON, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100813 DRM_ERROR("panel on wait timed out: 0x%08x\n",
814 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700815
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700816 pp &= ~(PANEL_UNLOCK_REGS);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700817 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700818 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700819 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700820
821 return false;
Jesse Barnes9934c132010-07-22 13:18:19 -0700822}
823
824static void ironlake_edp_panel_off (struct drm_device *dev)
825{
826 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson913d8d12010-08-07 11:01:35 +0100827 u32 pp;
Jesse Barnes9934c132010-07-22 13:18:19 -0700828
829 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700830
831 /* ILK workaround: disable reset around power sequence */
832 pp &= ~PANEL_POWER_RESET;
833 I915_WRITE(PCH_PP_CONTROL, pp);
834 POSTING_READ(PCH_PP_CONTROL);
835
Jesse Barnes9934c132010-07-22 13:18:19 -0700836 pp &= ~POWER_TARGET_ON;
837 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes9934c132010-07-22 13:18:19 -0700838
Chris Wilson481b6af2010-08-23 17:43:35 +0100839 if (wait_for((I915_READ(PCH_PP_STATUS) & PP_ON) == 0, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100840 DRM_ERROR("panel off wait timed out: 0x%08x\n",
841 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700842
843 /* Make sure VDD is enabled so DP AUX will work */
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700844 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700845 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700846 POSTING_READ(PCH_PP_CONTROL);
Hette Visser27d64332010-09-24 10:51:30 +0100847
848 /* Ouch. We need to wait here for some panels, like Dell e6510
849 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
850 */
851 msleep(300);
Jesse Barnes9934c132010-07-22 13:18:19 -0700852}
853
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700854static void ironlake_edp_panel_vdd_on(struct drm_device *dev)
855{
856 struct drm_i915_private *dev_priv = dev->dev_private;
857 u32 pp;
858
859 pp = I915_READ(PCH_PP_CONTROL);
860 pp |= EDP_FORCE_VDD;
861 I915_WRITE(PCH_PP_CONTROL, pp);
862 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes3ba5c562010-08-25 13:09:48 -0700863 msleep(300);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700864}
865
866static void ironlake_edp_panel_vdd_off(struct drm_device *dev)
867{
868 struct drm_i915_private *dev_priv = dev->dev_private;
869 u32 pp;
870
871 pp = I915_READ(PCH_PP_CONTROL);
872 pp &= ~EDP_FORCE_VDD;
873 I915_WRITE(PCH_PP_CONTROL, pp);
874 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes3ba5c562010-08-25 13:09:48 -0700875 msleep(300);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700876}
877
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500878static void ironlake_edp_backlight_on (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}
888
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500889static void ironlake_edp_backlight_off (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800890{
891 struct drm_i915_private *dev_priv = dev->dev_private;
892 u32 pp;
893
Zhao Yakui28c97732009-10-09 11:39:41 +0800894 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800895 pp = I915_READ(PCH_PP_CONTROL);
896 pp &= ~EDP_BLC_ENABLE;
897 I915_WRITE(PCH_PP_CONTROL, pp);
898}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700899
Jesse Barnesd240f202010-08-13 15:43:26 -0700900static void ironlake_edp_pll_on(struct drm_encoder *encoder)
901{
902 struct drm_device *dev = encoder->dev;
903 struct drm_i915_private *dev_priv = dev->dev_private;
904 u32 dpa_ctl;
905
906 DRM_DEBUG_KMS("\n");
907 dpa_ctl = I915_READ(DP_A);
908 dpa_ctl &= ~DP_PLL_ENABLE;
909 I915_WRITE(DP_A, dpa_ctl);
910}
911
912static void ironlake_edp_pll_off(struct drm_encoder *encoder)
913{
914 struct drm_device *dev = encoder->dev;
915 struct drm_i915_private *dev_priv = dev->dev_private;
916 u32 dpa_ctl;
917
918 dpa_ctl = I915_READ(DP_A);
919 dpa_ctl |= DP_PLL_ENABLE;
920 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +0100921 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -0700922 udelay(200);
923}
924
925static void intel_dp_prepare(struct drm_encoder *encoder)
926{
927 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
928 struct drm_device *dev = encoder->dev;
929 struct drm_i915_private *dev_priv = dev->dev_private;
930 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
931
Jesse Barnes4d926462010-10-07 16:01:07 -0700932 if (is_edp(intel_dp)) {
Jesse Barnes2c9d9752010-09-08 12:42:05 -0700933 ironlake_edp_panel_off(dev);
Jesse Barnesd240f202010-08-13 15:43:26 -0700934 ironlake_edp_backlight_off(dev);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700935 ironlake_edp_panel_vdd_on(dev);
Jesse Barnesd240f202010-08-13 15:43:26 -0700936 ironlake_edp_pll_on(encoder);
937 }
938 if (dp_reg & DP_PORT_EN)
939 intel_dp_link_down(intel_dp);
940}
941
942static void intel_dp_commit(struct drm_encoder *encoder)
943{
944 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
945 struct drm_device *dev = encoder->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -0700946
Jesse Barnes33a34e42010-09-08 12:42:02 -0700947 intel_dp_start_link_train(intel_dp);
948
Jesse Barnes4d926462010-10-07 16:01:07 -0700949 if (is_edp(intel_dp))
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700950 ironlake_edp_panel_on(dev);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700951
952 intel_dp_complete_link_train(intel_dp);
953
Jesse Barnes4d926462010-10-07 16:01:07 -0700954 if (is_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -0700955 ironlake_edp_backlight_on(dev);
956}
957
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700958static void
959intel_dp_dpms(struct drm_encoder *encoder, int mode)
960{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100961 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800962 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700963 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100964 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700965
966 if (mode != DRM_MODE_DPMS_ON) {
Jesse Barnes4d926462010-10-07 16:01:07 -0700967 if (is_edp(intel_dp)) {
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700968 ironlake_edp_backlight_off(dev);
969 ironlake_edp_panel_off(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800970 }
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700971 if (dp_reg & DP_PORT_EN)
972 intel_dp_link_down(intel_dp);
Jesse Barnes4d926462010-10-07 16:01:07 -0700973 if (is_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -0700974 ironlake_edp_pll_off(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700975 } else {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800976 if (!(dp_reg & DP_PORT_EN)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -0700977 intel_dp_start_link_train(intel_dp);
Jesse Barnes4d926462010-10-07 16:01:07 -0700978 if (is_edp(intel_dp))
Jesse Barnes9934c132010-07-22 13:18:19 -0700979 ironlake_edp_panel_on(dev);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700980 intel_dp_complete_link_train(intel_dp);
Jesse Barnes4d926462010-10-07 16:01:07 -0700981 if (is_edp(intel_dp))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500982 ironlake_edp_backlight_on(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800983 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700984 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100985 intel_dp->dpms_mode = mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700986}
987
988/*
989 * Fetch AUX CH registers 0x202 - 0x207 which contain
990 * link status information
991 */
992static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -0700993intel_dp_get_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700994{
995 int ret;
996
Chris Wilsonea5b2132010-08-04 13:50:23 +0100997 ret = intel_dp_aux_native_read(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700998 DP_LANE0_1_STATUS,
Jesse Barnes33a34e42010-09-08 12:42:02 -0700999 intel_dp->link_status, DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001000 if (ret != DP_LINK_STATUS_SIZE)
1001 return false;
1002 return true;
1003}
1004
1005static uint8_t
1006intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1007 int r)
1008{
1009 return link_status[r - DP_LANE0_1_STATUS];
1010}
1011
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001012static uint8_t
1013intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
1014 int lane)
1015{
1016 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1017 int s = ((lane & 1) ?
1018 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1019 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1020 uint8_t l = intel_dp_link_status(link_status, i);
1021
1022 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1023}
1024
1025static uint8_t
1026intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1027 int lane)
1028{
1029 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1030 int s = ((lane & 1) ?
1031 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1032 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1033 uint8_t l = intel_dp_link_status(link_status, i);
1034
1035 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1036}
1037
1038
1039#if 0
1040static char *voltage_names[] = {
1041 "0.4V", "0.6V", "0.8V", "1.2V"
1042};
1043static char *pre_emph_names[] = {
1044 "0dB", "3.5dB", "6dB", "9.5dB"
1045};
1046static char *link_train_names[] = {
1047 "pattern 1", "pattern 2", "idle", "off"
1048};
1049#endif
1050
1051/*
1052 * These are source-specific values; current Intel hardware supports
1053 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1054 */
1055#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1056
1057static uint8_t
1058intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1059{
1060 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1061 case DP_TRAIN_VOLTAGE_SWING_400:
1062 return DP_TRAIN_PRE_EMPHASIS_6;
1063 case DP_TRAIN_VOLTAGE_SWING_600:
1064 return DP_TRAIN_PRE_EMPHASIS_6;
1065 case DP_TRAIN_VOLTAGE_SWING_800:
1066 return DP_TRAIN_PRE_EMPHASIS_3_5;
1067 case DP_TRAIN_VOLTAGE_SWING_1200:
1068 default:
1069 return DP_TRAIN_PRE_EMPHASIS_0;
1070 }
1071}
1072
1073static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001074intel_get_adjust_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001075{
1076 uint8_t v = 0;
1077 uint8_t p = 0;
1078 int lane;
1079
Jesse Barnes33a34e42010-09-08 12:42:02 -07001080 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1081 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1082 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001083
1084 if (this_v > v)
1085 v = this_v;
1086 if (this_p > p)
1087 p = this_p;
1088 }
1089
1090 if (v >= I830_DP_VOLTAGE_MAX)
1091 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1092
1093 if (p >= intel_dp_pre_emphasis_max(v))
1094 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1095
1096 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001097 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001098}
1099
1100static uint32_t
1101intel_dp_signal_levels(uint8_t train_set, int lane_count)
1102{
1103 uint32_t signal_levels = 0;
1104
1105 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
1106 case DP_TRAIN_VOLTAGE_SWING_400:
1107 default:
1108 signal_levels |= DP_VOLTAGE_0_4;
1109 break;
1110 case DP_TRAIN_VOLTAGE_SWING_600:
1111 signal_levels |= DP_VOLTAGE_0_6;
1112 break;
1113 case DP_TRAIN_VOLTAGE_SWING_800:
1114 signal_levels |= DP_VOLTAGE_0_8;
1115 break;
1116 case DP_TRAIN_VOLTAGE_SWING_1200:
1117 signal_levels |= DP_VOLTAGE_1_2;
1118 break;
1119 }
1120 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
1121 case DP_TRAIN_PRE_EMPHASIS_0:
1122 default:
1123 signal_levels |= DP_PRE_EMPHASIS_0;
1124 break;
1125 case DP_TRAIN_PRE_EMPHASIS_3_5:
1126 signal_levels |= DP_PRE_EMPHASIS_3_5;
1127 break;
1128 case DP_TRAIN_PRE_EMPHASIS_6:
1129 signal_levels |= DP_PRE_EMPHASIS_6;
1130 break;
1131 case DP_TRAIN_PRE_EMPHASIS_9_5:
1132 signal_levels |= DP_PRE_EMPHASIS_9_5;
1133 break;
1134 }
1135 return signal_levels;
1136}
1137
Zhenyu Wange3421a12010-04-08 09:43:27 +08001138/* Gen6's DP voltage swing and pre-emphasis control */
1139static uint32_t
1140intel_gen6_edp_signal_levels(uint8_t train_set)
1141{
1142 switch (train_set & (DP_TRAIN_VOLTAGE_SWING_MASK|DP_TRAIN_PRE_EMPHASIS_MASK)) {
1143 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1144 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1145 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1146 return EDP_LINK_TRAIN_400MV_6DB_SNB_B;
1147 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1148 return EDP_LINK_TRAIN_600MV_3_5DB_SNB_B;
1149 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1150 return EDP_LINK_TRAIN_800MV_0DB_SNB_B;
1151 default:
1152 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level\n");
1153 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1154 }
1155}
1156
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001157static uint8_t
1158intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1159 int lane)
1160{
1161 int i = DP_LANE0_1_STATUS + (lane >> 1);
1162 int s = (lane & 1) * 4;
1163 uint8_t l = intel_dp_link_status(link_status, i);
1164
1165 return (l >> s) & 0xf;
1166}
1167
1168/* Check for clock recovery is done on all channels */
1169static bool
1170intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1171{
1172 int lane;
1173 uint8_t lane_status;
1174
1175 for (lane = 0; lane < lane_count; lane++) {
1176 lane_status = intel_get_lane_status(link_status, lane);
1177 if ((lane_status & DP_LANE_CR_DONE) == 0)
1178 return false;
1179 }
1180 return true;
1181}
1182
1183/* Check to see if channel eq is done on all channels */
1184#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1185 DP_LANE_CHANNEL_EQ_DONE|\
1186 DP_LANE_SYMBOL_LOCKED)
1187static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001188intel_channel_eq_ok(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001189{
1190 uint8_t lane_align;
1191 uint8_t lane_status;
1192 int lane;
1193
Jesse Barnes33a34e42010-09-08 12:42:02 -07001194 lane_align = intel_dp_link_status(intel_dp->link_status,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001195 DP_LANE_ALIGN_STATUS_UPDATED);
1196 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1197 return false;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001198 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1199 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001200 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1201 return false;
1202 }
1203 return true;
1204}
1205
1206static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001207intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001208 uint32_t dp_reg_value,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001209 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001210{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001211 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001212 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001213 int ret;
1214
Chris Wilsonea5b2132010-08-04 13:50:23 +01001215 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1216 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001217
Chris Wilsonea5b2132010-08-04 13:50:23 +01001218 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001219 DP_TRAINING_PATTERN_SET,
1220 dp_train_pat);
1221
Chris Wilsonea5b2132010-08-04 13:50:23 +01001222 ret = intel_dp_aux_native_write(intel_dp,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001223 DP_TRAINING_LANE0_SET,
1224 intel_dp->train_set, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001225 if (ret != 4)
1226 return false;
1227
1228 return true;
1229}
1230
Jesse Barnes33a34e42010-09-08 12:42:02 -07001231/* Enable corresponding port and start training pattern 1 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001232static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001233intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001234{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001235 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001236 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson58e10eb2010-10-03 10:56:11 +01001237 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001238 int i;
1239 uint8_t voltage;
1240 bool clock_recovery = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001241 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001242 u32 reg;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001243 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001244
Keith Packardb99a9d92010-10-03 00:33:05 -07001245 /* Enable output, wait for it to become active */
1246 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1247 POSTING_READ(intel_dp->output_reg);
1248 intel_wait_for_vblank(dev, intel_crtc->pipe);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001249
1250 /* Write the link configuration data */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001251 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1252 intel_dp->link_configuration,
1253 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001254
1255 DP |= DP_PORT_EN;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001256 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001257 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1258 else
1259 DP &= ~DP_LINK_TRAIN_MASK;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001260 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001261 voltage = 0xff;
1262 tries = 0;
1263 clock_recovery = false;
1264 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001265 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001266 uint32_t signal_levels;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001267 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001268 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001269 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1270 } else {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001271 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001272 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1273 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001274
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001275 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001276 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1277 else
1278 reg = DP | DP_LINK_TRAIN_PAT_1;
1279
Chris Wilsonea5b2132010-08-04 13:50:23 +01001280 if (!intel_dp_set_link_train(intel_dp, reg,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001281 DP_TRAINING_PATTERN_1))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001282 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001283 /* Set training pattern 1 */
1284
1285 udelay(100);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001286 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001287 break;
1288
Jesse Barnes33a34e42010-09-08 12:42:02 -07001289 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001290 clock_recovery = true;
1291 break;
1292 }
1293
1294 /* Check to see if we've tried the max voltage */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001295 for (i = 0; i < intel_dp->lane_count; i++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001296 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001297 break;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001298 if (i == intel_dp->lane_count)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001299 break;
1300
1301 /* Check to see if we've tried the same voltage 5 times */
Jesse Barnes33a34e42010-09-08 12:42:02 -07001302 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001303 ++tries;
1304 if (tries == 5)
1305 break;
1306 } else
1307 tries = 0;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001308 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001309
Jesse Barnes33a34e42010-09-08 12:42:02 -07001310 /* Compute new intel_dp->train_set as requested by target */
1311 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001312 }
1313
Jesse Barnes33a34e42010-09-08 12:42:02 -07001314 intel_dp->DP = DP;
1315}
1316
1317static void
1318intel_dp_complete_link_train(struct intel_dp *intel_dp)
1319{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001320 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001321 struct drm_i915_private *dev_priv = dev->dev_private;
1322 bool channel_eq = false;
1323 int tries;
1324 u32 reg;
1325 uint32_t DP = intel_dp->DP;
1326
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001327 /* channel equalization */
1328 tries = 0;
1329 channel_eq = false;
1330 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001331 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001332 uint32_t signal_levels;
1333
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001334 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001335 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001336 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1337 } else {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001338 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001339 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1340 }
1341
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001342 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001343 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1344 else
1345 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001346
1347 /* channel eq pattern */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001348 if (!intel_dp_set_link_train(intel_dp, reg,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001349 DP_TRAINING_PATTERN_2))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001350 break;
1351
1352 udelay(400);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001353 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001354 break;
1355
Jesse Barnes33a34e42010-09-08 12:42:02 -07001356 if (intel_channel_eq_ok(intel_dp)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001357 channel_eq = true;
1358 break;
1359 }
1360
1361 /* Try 5 times */
1362 if (tries > 5)
1363 break;
1364
Jesse Barnes33a34e42010-09-08 12:42:02 -07001365 /* Compute new intel_dp->train_set as requested by target */
1366 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001367 ++tries;
1368 }
1369
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001370 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001371 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1372 else
1373 reg = DP | DP_LINK_TRAIN_OFF;
1374
Chris Wilsonea5b2132010-08-04 13:50:23 +01001375 I915_WRITE(intel_dp->output_reg, reg);
1376 POSTING_READ(intel_dp->output_reg);
1377 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001378 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1379}
1380
1381static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001382intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001383{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001384 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001385 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001386 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001387
Zhao Yakui28c97732009-10-09 11:39:41 +08001388 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001389
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001390 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001391 DP &= ~DP_PLL_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001392 I915_WRITE(intel_dp->output_reg, DP);
1393 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001394 udelay(100);
1395 }
1396
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001397 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001398 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001399 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001400 } else {
1401 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001402 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001403 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01001404 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001405
Chris Wilsonfe255d02010-09-11 21:37:48 +01001406 msleep(17);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001407
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001408 if (is_edp(intel_dp))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001409 DP |= DP_LINK_TRAIN_OFF;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001410 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1411 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001412}
1413
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001414/*
1415 * According to DP spec
1416 * 5.1.2:
1417 * 1. Read DPCD
1418 * 2. Configure link according to Receiver Capabilities
1419 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1420 * 4. Check link status on receipt of hot-plug interrupt
1421 */
1422
1423static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001424intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001425{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001426 if (!intel_dp->base.base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001427 return;
1428
Jesse Barnes33a34e42010-09-08 12:42:02 -07001429 if (!intel_dp_get_link_status(intel_dp)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001430 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001431 return;
1432 }
1433
Jesse Barnes33a34e42010-09-08 12:42:02 -07001434 if (!intel_channel_eq_ok(intel_dp)) {
1435 intel_dp_start_link_train(intel_dp);
1436 intel_dp_complete_link_train(intel_dp);
1437 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001438}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001439
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001440static enum drm_connector_status
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001441ironlake_dp_detect(struct drm_connector *connector)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001442{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001443 struct intel_dp *intel_dp = intel_attached_dp(connector);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001444 enum drm_connector_status status;
1445
Jesse Barnes7eaf5542010-09-08 12:41:59 -07001446 /* Panel needs power for AUX to work */
Jesse Barnes4d926462010-10-07 16:01:07 -07001447 if (is_edp(intel_dp))
Jesse Barnesb2094bb2010-09-08 12:42:01 -07001448 ironlake_edp_panel_vdd_on(connector->dev);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001449 status = connector_status_disconnected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001450 if (intel_dp_aux_native_read(intel_dp,
1451 0x000, intel_dp->dpcd,
1452 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001453 {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001454 if (intel_dp->dpcd[0] != 0)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001455 status = connector_status_connected;
1456 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001457 DRM_DEBUG_KMS("DPCD: %hx%hx%hx%hx\n", intel_dp->dpcd[0],
1458 intel_dp->dpcd[1], intel_dp->dpcd[2], intel_dp->dpcd[3]);
Jesse Barnes4d926462010-10-07 16:01:07 -07001459 if (is_edp(intel_dp))
Jesse Barnesb2094bb2010-09-08 12:42:01 -07001460 ironlake_edp_panel_vdd_off(connector->dev);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001461 return status;
1462}
1463
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001464/**
1465 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1466 *
1467 * \return true if DP port is connected.
1468 * \return false if DP port is disconnected.
1469 */
1470static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +01001471intel_dp_detect(struct drm_connector *connector, bool force)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001472{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001473 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001474 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001475 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001476 uint32_t temp, bit;
1477 enum drm_connector_status status;
1478
Chris Wilsonea5b2132010-08-04 13:50:23 +01001479 intel_dp->has_audio = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001480
Eric Anholtc619eed2010-01-28 16:45:52 -08001481 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001482 return ironlake_dp_detect(connector);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001483
Chris Wilsonea5b2132010-08-04 13:50:23 +01001484 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001485 case DP_B:
1486 bit = DPB_HOTPLUG_INT_STATUS;
1487 break;
1488 case DP_C:
1489 bit = DPC_HOTPLUG_INT_STATUS;
1490 break;
1491 case DP_D:
1492 bit = DPD_HOTPLUG_INT_STATUS;
1493 break;
1494 default:
1495 return connector_status_unknown;
1496 }
1497
1498 temp = I915_READ(PORT_HOTPLUG_STAT);
1499
1500 if ((temp & bit) == 0)
1501 return connector_status_disconnected;
1502
1503 status = connector_status_disconnected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001504 if (intel_dp_aux_native_read(intel_dp,
1505 0x000, intel_dp->dpcd,
1506 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001507 {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001508 if (intel_dp->dpcd[0] != 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001509 status = connector_status_connected;
1510 }
1511 return status;
1512}
1513
1514static int intel_dp_get_modes(struct drm_connector *connector)
1515{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001516 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001517 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001518 struct drm_i915_private *dev_priv = dev->dev_private;
1519 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001520
1521 /* We should parse the EDID data and find out if it has an audio sink
1522 */
1523
Chris Wilsonf899fc62010-07-20 15:44:45 -07001524 ret = intel_ddc_get_modes(connector, &intel_dp->adapter);
Zhao Yakuib9efc482010-07-19 09:43:11 +01001525 if (ret) {
Jesse Barnes4d926462010-10-07 16:01:07 -07001526 if (is_edp(intel_dp) && !dev_priv->panel_fixed_mode) {
Zhao Yakuib9efc482010-07-19 09:43:11 +01001527 struct drm_display_mode *newmode;
1528 list_for_each_entry(newmode, &connector->probed_modes,
1529 head) {
1530 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1531 dev_priv->panel_fixed_mode =
1532 drm_mode_duplicate(dev, newmode);
1533 break;
1534 }
1535 }
1536 }
1537
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001538 return ret;
Zhao Yakuib9efc482010-07-19 09:43:11 +01001539 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001540
1541 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Jesse Barnes4d926462010-10-07 16:01:07 -07001542 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001543 if (dev_priv->panel_fixed_mode != NULL) {
1544 struct drm_display_mode *mode;
1545 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1546 drm_mode_probed_add(connector, mode);
1547 return 1;
1548 }
1549 }
1550 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001551}
1552
1553static void
1554intel_dp_destroy (struct drm_connector *connector)
1555{
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001556 drm_sysfs_connector_remove(connector);
1557 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001558 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001559}
1560
Daniel Vetter24d05922010-08-20 18:08:28 +02001561static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1562{
1563 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1564
1565 i2c_del_adapter(&intel_dp->adapter);
1566 drm_encoder_cleanup(encoder);
1567 kfree(intel_dp);
1568}
1569
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001570static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1571 .dpms = intel_dp_dpms,
1572 .mode_fixup = intel_dp_mode_fixup,
Jesse Barnesd240f202010-08-13 15:43:26 -07001573 .prepare = intel_dp_prepare,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001574 .mode_set = intel_dp_mode_set,
Jesse Barnesd240f202010-08-13 15:43:26 -07001575 .commit = intel_dp_commit,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001576};
1577
1578static const struct drm_connector_funcs intel_dp_connector_funcs = {
1579 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001580 .detect = intel_dp_detect,
1581 .fill_modes = drm_helper_probe_single_connector_modes,
1582 .destroy = intel_dp_destroy,
1583};
1584
1585static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1586 .get_modes = intel_dp_get_modes,
1587 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01001588 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001589};
1590
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001591static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02001592 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001593};
1594
Chris Wilson995b6762010-08-20 13:23:26 +01001595static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001596intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07001597{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001598 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Keith Packardc8110e52009-05-06 11:51:10 -07001599
Chris Wilsonea5b2132010-08-04 13:50:23 +01001600 if (intel_dp->dpms_mode == DRM_MODE_DPMS_ON)
1601 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07001602}
1603
Zhenyu Wange3421a12010-04-08 09:43:27 +08001604/* Return which DP Port should be selected for Transcoder DP control */
1605int
1606intel_trans_dp_port_sel (struct drm_crtc *crtc)
1607{
1608 struct drm_device *dev = crtc->dev;
1609 struct drm_mode_config *mode_config = &dev->mode_config;
1610 struct drm_encoder *encoder;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001611
1612 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001613 struct intel_dp *intel_dp;
1614
Dan Carpenterd8201ab2010-05-07 10:39:00 +02001615 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08001616 continue;
1617
Chris Wilsonea5b2132010-08-04 13:50:23 +01001618 intel_dp = enc_to_intel_dp(encoder);
1619 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
1620 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001621 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001622
Zhenyu Wange3421a12010-04-08 09:43:27 +08001623 return -1;
1624}
1625
Zhao Yakui36e83a12010-06-12 14:32:21 +08001626/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04001627bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08001628{
1629 struct drm_i915_private *dev_priv = dev->dev_private;
1630 struct child_device_config *p_child;
1631 int i;
1632
1633 if (!dev_priv->child_dev_num)
1634 return false;
1635
1636 for (i = 0; i < dev_priv->child_dev_num; i++) {
1637 p_child = dev_priv->child_dev + i;
1638
1639 if (p_child->dvo_port == PORT_IDPD &&
1640 p_child->device_type == DEVICE_TYPE_eDP)
1641 return true;
1642 }
1643 return false;
1644}
1645
Keith Packardc8110e52009-05-06 11:51:10 -07001646void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001647intel_dp_init(struct drm_device *dev, int output_reg)
1648{
1649 struct drm_i915_private *dev_priv = dev->dev_private;
1650 struct drm_connector *connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001651 struct intel_dp *intel_dp;
Eric Anholt21d40d32010-03-25 11:11:14 -07001652 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001653 struct intel_connector *intel_connector;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001654 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04001655 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001656
Chris Wilsonea5b2132010-08-04 13:50:23 +01001657 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
1658 if (!intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001659 return;
1660
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001661 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1662 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001663 kfree(intel_dp);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001664 return;
1665 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001666 intel_encoder = &intel_dp->base;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001667
Chris Wilsonea5b2132010-08-04 13:50:23 +01001668 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04001669 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01001670 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04001671
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001672 if (output_reg == DP_A || is_pch_edp(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04001673 type = DRM_MODE_CONNECTOR_eDP;
1674 intel_encoder->type = INTEL_OUTPUT_EDP;
1675 } else {
1676 type = DRM_MODE_CONNECTOR_DisplayPort;
1677 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1678 }
1679
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001680 connector = &intel_connector->base;
Adam Jacksonb3295302010-07-16 14:46:28 -04001681 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001682 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1683
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001684 connector->polled = DRM_CONNECTOR_POLL_HPD;
1685
Zhao Yakui652af9d2009-12-02 10:03:33 +08001686 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07001687 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001688 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07001689 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001690 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07001691 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08001692
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001693 if (is_edp(intel_dp))
Eric Anholt21d40d32010-03-25 11:11:14 -07001694 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08001695
Eric Anholt21d40d32010-03-25 11:11:14 -07001696 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001697 connector->interlace_allowed = true;
1698 connector->doublescan_allowed = 0;
1699
Chris Wilsonea5b2132010-08-04 13:50:23 +01001700 intel_dp->output_reg = output_reg;
1701 intel_dp->has_audio = false;
1702 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001703
Chris Wilson4ef69c72010-09-09 15:14:28 +01001704 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001705 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001706 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001707
Chris Wilsondf0e9242010-09-09 16:20:55 +01001708 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001709 drm_sysfs_connector_add(connector);
1710
1711 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001712 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001713 case DP_A:
1714 name = "DPDDC-A";
1715 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001716 case DP_B:
1717 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001718 dev_priv->hotplug_supported_mask |=
1719 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001720 name = "DPDDC-B";
1721 break;
1722 case DP_C:
1723 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001724 dev_priv->hotplug_supported_mask |=
1725 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001726 name = "DPDDC-C";
1727 break;
1728 case DP_D:
1729 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001730 dev_priv->hotplug_supported_mask |=
1731 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001732 name = "DPDDC-D";
1733 break;
1734 }
1735
Chris Wilsonea5b2132010-08-04 13:50:23 +01001736 intel_dp_i2c_init(intel_dp, intel_connector, name);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001737
Eric Anholt21d40d32010-03-25 11:11:14 -07001738 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001739
Jesse Barnes4d926462010-10-07 16:01:07 -07001740 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001741 /* initialize panel mode from VBT if available for eDP */
1742 if (dev_priv->lfp_lvds_vbt_mode) {
1743 dev_priv->panel_fixed_mode =
1744 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1745 if (dev_priv->panel_fixed_mode) {
1746 dev_priv->panel_fixed_mode->type |=
1747 DRM_MODE_TYPE_PREFERRED;
1748 }
1749 }
1750 }
1751
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001752 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1753 * 0xd. Failure to do so will result in spurious interrupts being
1754 * generated on the port when a cable is not attached.
1755 */
1756 if (IS_G4X(dev) && !IS_GM45(dev)) {
1757 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
1758 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
1759 }
1760}