blob: 714e553960fd728d5a737581507e82b526b66f14 [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 Barnes51190662010-10-07 16:01:08 -0700675 break;
676 } else if (is_edp(intel_dp)) {
677 lane_count = dev_priv->edp.lanes;
678 bpp = dev_priv->edp.bpp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700679 break;
680 }
681 }
682
683 /*
684 * Compute the GMCH and Link ratios. The '3' here is
685 * the number of bytes_per_pixel post-LUT, which we always
686 * set up for 8-bits of R/G/B, or 3 bytes total.
687 */
Zhao Yakui36e83a12010-06-12 14:32:21 +0800688 intel_dp_compute_m_n(bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700689 mode->clock, adjusted_mode->clock, &m_n);
690
Eric Anholtc619eed2010-01-28 16:45:52 -0800691 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800692 if (intel_crtc->pipe == 0) {
693 I915_WRITE(TRANSA_DATA_M1,
694 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
695 m_n.gmch_m);
696 I915_WRITE(TRANSA_DATA_N1, m_n.gmch_n);
697 I915_WRITE(TRANSA_DP_LINK_M1, m_n.link_m);
698 I915_WRITE(TRANSA_DP_LINK_N1, m_n.link_n);
699 } else {
700 I915_WRITE(TRANSB_DATA_M1,
701 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
702 m_n.gmch_m);
703 I915_WRITE(TRANSB_DATA_N1, m_n.gmch_n);
704 I915_WRITE(TRANSB_DP_LINK_M1, m_n.link_m);
705 I915_WRITE(TRANSB_DP_LINK_N1, m_n.link_n);
706 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700707 } else {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800708 if (intel_crtc->pipe == 0) {
709 I915_WRITE(PIPEA_GMCH_DATA_M,
710 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
711 m_n.gmch_m);
712 I915_WRITE(PIPEA_GMCH_DATA_N,
713 m_n.gmch_n);
714 I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
715 I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
716 } else {
717 I915_WRITE(PIPEB_GMCH_DATA_M,
718 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
719 m_n.gmch_m);
720 I915_WRITE(PIPEB_GMCH_DATA_N,
721 m_n.gmch_n);
722 I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
723 I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
724 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700725 }
726}
727
728static void
729intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
730 struct drm_display_mode *adjusted_mode)
731{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800732 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100733 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +0100734 struct drm_crtc *crtc = intel_dp->base.base.crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700735 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
736
Chris Wilsonea5b2132010-08-04 13:50:23 +0100737 intel_dp->DP = (DP_VOLTAGE_0_4 |
Adam Jackson9c9e7922010-04-05 17:57:59 -0400738 DP_PRE_EMPHASIS_0);
739
740 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100741 intel_dp->DP |= DP_SYNC_HS_HIGH;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400742 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100743 intel_dp->DP |= DP_SYNC_VS_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700744
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700745 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100746 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800747 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100748 intel_dp->DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700749
Chris Wilsonea5b2132010-08-04 13:50:23 +0100750 switch (intel_dp->lane_count) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700751 case 1:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100752 intel_dp->DP |= DP_PORT_WIDTH_1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700753 break;
754 case 2:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100755 intel_dp->DP |= DP_PORT_WIDTH_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700756 break;
757 case 4:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100758 intel_dp->DP |= DP_PORT_WIDTH_4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700759 break;
760 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100761 if (intel_dp->has_audio)
762 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700763
Chris Wilsonea5b2132010-08-04 13:50:23 +0100764 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
765 intel_dp->link_configuration[0] = intel_dp->link_bw;
766 intel_dp->link_configuration[1] = intel_dp->lane_count;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700767
768 /*
Adam Jackson9962c922010-05-13 14:45:42 -0400769 * Check for DPCD version > 1.1 and enhanced framing support
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700770 */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100771 if (intel_dp->dpcd[0] >= 0x11 && (intel_dp->dpcd[2] & DP_ENHANCED_FRAME_CAP)) {
772 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
773 intel_dp->DP |= DP_ENHANCED_FRAMING;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700774 }
775
Zhenyu Wange3421a12010-04-08 09:43:27 +0800776 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
777 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100778 intel_dp->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800779
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700780 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800781 /* don't miss out required setting for eDP */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100782 intel_dp->DP |= DP_PLL_ENABLE;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800783 if (adjusted_mode->clock < 200000)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100784 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800785 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100786 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800787 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700788}
789
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700790/* Returns true if the panel was already on when called */
791static bool ironlake_edp_panel_on (struct drm_device *dev)
Jesse Barnes9934c132010-07-22 13:18:19 -0700792{
793 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson913d8d12010-08-07 11:01:35 +0100794 u32 pp;
Jesse Barnes9934c132010-07-22 13:18:19 -0700795
Chris Wilson913d8d12010-08-07 11:01:35 +0100796 if (I915_READ(PCH_PP_STATUS) & PP_ON)
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700797 return true;
Jesse Barnes9934c132010-07-22 13:18:19 -0700798
799 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700800
801 /* ILK workaround: disable reset around power sequence */
802 pp &= ~PANEL_POWER_RESET;
803 I915_WRITE(PCH_PP_CONTROL, pp);
804 POSTING_READ(PCH_PP_CONTROL);
805
Jesse Barnes4d12fe02010-09-10 10:46:45 -0700806 pp |= POWER_TARGET_ON;
Jesse Barnes9934c132010-07-22 13:18:19 -0700807 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes9934c132010-07-22 13:18:19 -0700808
Hette Visser27d64332010-09-24 10:51:30 +0100809 /* Ouch. We need to wait here for some panels, like Dell e6510
810 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
811 */
812 msleep(300);
813
Chris Wilson481b6af2010-08-23 17:43:35 +0100814 if (wait_for(I915_READ(PCH_PP_STATUS) & PP_ON, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100815 DRM_ERROR("panel on wait timed out: 0x%08x\n",
816 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700817
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700818 pp &= ~(PANEL_UNLOCK_REGS);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700819 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700820 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700821 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700822
823 return false;
Jesse Barnes9934c132010-07-22 13:18:19 -0700824}
825
826static void ironlake_edp_panel_off (struct drm_device *dev)
827{
828 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson913d8d12010-08-07 11:01:35 +0100829 u32 pp;
Jesse Barnes9934c132010-07-22 13:18:19 -0700830
831 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700832
833 /* ILK workaround: disable reset around power sequence */
834 pp &= ~PANEL_POWER_RESET;
835 I915_WRITE(PCH_PP_CONTROL, pp);
836 POSTING_READ(PCH_PP_CONTROL);
837
Jesse Barnes9934c132010-07-22 13:18:19 -0700838 pp &= ~POWER_TARGET_ON;
839 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes9934c132010-07-22 13:18:19 -0700840
Chris Wilson481b6af2010-08-23 17:43:35 +0100841 if (wait_for((I915_READ(PCH_PP_STATUS) & PP_ON) == 0, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100842 DRM_ERROR("panel off wait timed out: 0x%08x\n",
843 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700844
845 /* Make sure VDD is enabled so DP AUX will work */
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700846 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700847 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700848 POSTING_READ(PCH_PP_CONTROL);
Hette Visser27d64332010-09-24 10:51:30 +0100849
850 /* Ouch. We need to wait here for some panels, like Dell e6510
851 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
852 */
853 msleep(300);
Jesse Barnes9934c132010-07-22 13:18:19 -0700854}
855
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700856static void ironlake_edp_panel_vdd_on(struct drm_device *dev)
857{
858 struct drm_i915_private *dev_priv = dev->dev_private;
859 u32 pp;
860
861 pp = I915_READ(PCH_PP_CONTROL);
862 pp |= EDP_FORCE_VDD;
863 I915_WRITE(PCH_PP_CONTROL, pp);
864 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes3ba5c562010-08-25 13:09:48 -0700865 msleep(300);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700866}
867
868static void ironlake_edp_panel_vdd_off(struct drm_device *dev)
869{
870 struct drm_i915_private *dev_priv = dev->dev_private;
871 u32 pp;
872
873 pp = I915_READ(PCH_PP_CONTROL);
874 pp &= ~EDP_FORCE_VDD;
875 I915_WRITE(PCH_PP_CONTROL, pp);
876 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes3ba5c562010-08-25 13:09:48 -0700877 msleep(300);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700878}
879
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500880static void ironlake_edp_backlight_on (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800881{
882 struct drm_i915_private *dev_priv = dev->dev_private;
883 u32 pp;
884
Zhao Yakui28c97732009-10-09 11:39:41 +0800885 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800886 pp = I915_READ(PCH_PP_CONTROL);
887 pp |= EDP_BLC_ENABLE;
888 I915_WRITE(PCH_PP_CONTROL, pp);
889}
890
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500891static void ironlake_edp_backlight_off (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800892{
893 struct drm_i915_private *dev_priv = dev->dev_private;
894 u32 pp;
895
Zhao Yakui28c97732009-10-09 11:39:41 +0800896 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800897 pp = I915_READ(PCH_PP_CONTROL);
898 pp &= ~EDP_BLC_ENABLE;
899 I915_WRITE(PCH_PP_CONTROL, pp);
900}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700901
Jesse Barnesd240f202010-08-13 15:43:26 -0700902static void ironlake_edp_pll_on(struct drm_encoder *encoder)
903{
904 struct drm_device *dev = encoder->dev;
905 struct drm_i915_private *dev_priv = dev->dev_private;
906 u32 dpa_ctl;
907
908 DRM_DEBUG_KMS("\n");
909 dpa_ctl = I915_READ(DP_A);
910 dpa_ctl &= ~DP_PLL_ENABLE;
911 I915_WRITE(DP_A, dpa_ctl);
912}
913
914static void ironlake_edp_pll_off(struct drm_encoder *encoder)
915{
916 struct drm_device *dev = encoder->dev;
917 struct drm_i915_private *dev_priv = dev->dev_private;
918 u32 dpa_ctl;
919
920 dpa_ctl = I915_READ(DP_A);
921 dpa_ctl |= DP_PLL_ENABLE;
922 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +0100923 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -0700924 udelay(200);
925}
926
927static void intel_dp_prepare(struct drm_encoder *encoder)
928{
929 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
930 struct drm_device *dev = encoder->dev;
931 struct drm_i915_private *dev_priv = dev->dev_private;
932 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
933
Jesse Barnes4d926462010-10-07 16:01:07 -0700934 if (is_edp(intel_dp)) {
Jesse Barnes2c9d9752010-09-08 12:42:05 -0700935 ironlake_edp_panel_off(dev);
Jesse Barnesd240f202010-08-13 15:43:26 -0700936 ironlake_edp_backlight_off(dev);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700937 ironlake_edp_panel_vdd_on(dev);
Jesse Barnesd240f202010-08-13 15:43:26 -0700938 ironlake_edp_pll_on(encoder);
939 }
940 if (dp_reg & DP_PORT_EN)
941 intel_dp_link_down(intel_dp);
942}
943
944static void intel_dp_commit(struct drm_encoder *encoder)
945{
946 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
947 struct drm_device *dev = encoder->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -0700948
Jesse Barnes33a34e42010-09-08 12:42:02 -0700949 intel_dp_start_link_train(intel_dp);
950
Jesse Barnes4d926462010-10-07 16:01:07 -0700951 if (is_edp(intel_dp))
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700952 ironlake_edp_panel_on(dev);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700953
954 intel_dp_complete_link_train(intel_dp);
955
Jesse Barnes4d926462010-10-07 16:01:07 -0700956 if (is_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -0700957 ironlake_edp_backlight_on(dev);
958}
959
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700960static void
961intel_dp_dpms(struct drm_encoder *encoder, int mode)
962{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100963 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800964 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700965 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100966 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700967
968 if (mode != DRM_MODE_DPMS_ON) {
Jesse Barnes4d926462010-10-07 16:01:07 -0700969 if (is_edp(intel_dp)) {
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700970 ironlake_edp_backlight_off(dev);
971 ironlake_edp_panel_off(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800972 }
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700973 if (dp_reg & DP_PORT_EN)
974 intel_dp_link_down(intel_dp);
Jesse Barnes4d926462010-10-07 16:01:07 -0700975 if (is_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -0700976 ironlake_edp_pll_off(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700977 } else {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800978 if (!(dp_reg & DP_PORT_EN)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -0700979 intel_dp_start_link_train(intel_dp);
Jesse Barnes4d926462010-10-07 16:01:07 -0700980 if (is_edp(intel_dp))
Jesse Barnes9934c132010-07-22 13:18:19 -0700981 ironlake_edp_panel_on(dev);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700982 intel_dp_complete_link_train(intel_dp);
Jesse Barnes4d926462010-10-07 16:01:07 -0700983 if (is_edp(intel_dp))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500984 ironlake_edp_backlight_on(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800985 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700986 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100987 intel_dp->dpms_mode = mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700988}
989
990/*
991 * Fetch AUX CH registers 0x202 - 0x207 which contain
992 * link status information
993 */
994static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -0700995intel_dp_get_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700996{
997 int ret;
998
Chris Wilsonea5b2132010-08-04 13:50:23 +0100999 ret = intel_dp_aux_native_read(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001000 DP_LANE0_1_STATUS,
Jesse Barnes33a34e42010-09-08 12:42:02 -07001001 intel_dp->link_status, DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001002 if (ret != DP_LINK_STATUS_SIZE)
1003 return false;
1004 return true;
1005}
1006
1007static uint8_t
1008intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1009 int r)
1010{
1011 return link_status[r - DP_LANE0_1_STATUS];
1012}
1013
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001014static uint8_t
1015intel_get_adjust_request_voltage(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_VOLTAGE_SWING_LANE1_SHIFT :
1021 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1022 uint8_t l = intel_dp_link_status(link_status, i);
1023
1024 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1025}
1026
1027static uint8_t
1028intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1029 int lane)
1030{
1031 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1032 int s = ((lane & 1) ?
1033 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1034 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1035 uint8_t l = intel_dp_link_status(link_status, i);
1036
1037 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1038}
1039
1040
1041#if 0
1042static char *voltage_names[] = {
1043 "0.4V", "0.6V", "0.8V", "1.2V"
1044};
1045static char *pre_emph_names[] = {
1046 "0dB", "3.5dB", "6dB", "9.5dB"
1047};
1048static char *link_train_names[] = {
1049 "pattern 1", "pattern 2", "idle", "off"
1050};
1051#endif
1052
1053/*
1054 * These are source-specific values; current Intel hardware supports
1055 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1056 */
1057#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1058
1059static uint8_t
1060intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1061{
1062 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1063 case DP_TRAIN_VOLTAGE_SWING_400:
1064 return DP_TRAIN_PRE_EMPHASIS_6;
1065 case DP_TRAIN_VOLTAGE_SWING_600:
1066 return DP_TRAIN_PRE_EMPHASIS_6;
1067 case DP_TRAIN_VOLTAGE_SWING_800:
1068 return DP_TRAIN_PRE_EMPHASIS_3_5;
1069 case DP_TRAIN_VOLTAGE_SWING_1200:
1070 default:
1071 return DP_TRAIN_PRE_EMPHASIS_0;
1072 }
1073}
1074
1075static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001076intel_get_adjust_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001077{
1078 uint8_t v = 0;
1079 uint8_t p = 0;
1080 int lane;
1081
Jesse Barnes33a34e42010-09-08 12:42:02 -07001082 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1083 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1084 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001085
1086 if (this_v > v)
1087 v = this_v;
1088 if (this_p > p)
1089 p = this_p;
1090 }
1091
1092 if (v >= I830_DP_VOLTAGE_MAX)
1093 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1094
1095 if (p >= intel_dp_pre_emphasis_max(v))
1096 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1097
1098 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001099 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001100}
1101
1102static uint32_t
1103intel_dp_signal_levels(uint8_t train_set, int lane_count)
1104{
1105 uint32_t signal_levels = 0;
1106
1107 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
1108 case DP_TRAIN_VOLTAGE_SWING_400:
1109 default:
1110 signal_levels |= DP_VOLTAGE_0_4;
1111 break;
1112 case DP_TRAIN_VOLTAGE_SWING_600:
1113 signal_levels |= DP_VOLTAGE_0_6;
1114 break;
1115 case DP_TRAIN_VOLTAGE_SWING_800:
1116 signal_levels |= DP_VOLTAGE_0_8;
1117 break;
1118 case DP_TRAIN_VOLTAGE_SWING_1200:
1119 signal_levels |= DP_VOLTAGE_1_2;
1120 break;
1121 }
1122 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
1123 case DP_TRAIN_PRE_EMPHASIS_0:
1124 default:
1125 signal_levels |= DP_PRE_EMPHASIS_0;
1126 break;
1127 case DP_TRAIN_PRE_EMPHASIS_3_5:
1128 signal_levels |= DP_PRE_EMPHASIS_3_5;
1129 break;
1130 case DP_TRAIN_PRE_EMPHASIS_6:
1131 signal_levels |= DP_PRE_EMPHASIS_6;
1132 break;
1133 case DP_TRAIN_PRE_EMPHASIS_9_5:
1134 signal_levels |= DP_PRE_EMPHASIS_9_5;
1135 break;
1136 }
1137 return signal_levels;
1138}
1139
Zhenyu Wange3421a12010-04-08 09:43:27 +08001140/* Gen6's DP voltage swing and pre-emphasis control */
1141static uint32_t
1142intel_gen6_edp_signal_levels(uint8_t train_set)
1143{
1144 switch (train_set & (DP_TRAIN_VOLTAGE_SWING_MASK|DP_TRAIN_PRE_EMPHASIS_MASK)) {
1145 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1146 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1147 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1148 return EDP_LINK_TRAIN_400MV_6DB_SNB_B;
1149 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1150 return EDP_LINK_TRAIN_600MV_3_5DB_SNB_B;
1151 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1152 return EDP_LINK_TRAIN_800MV_0DB_SNB_B;
1153 default:
1154 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level\n");
1155 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1156 }
1157}
1158
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001159static uint8_t
1160intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1161 int lane)
1162{
1163 int i = DP_LANE0_1_STATUS + (lane >> 1);
1164 int s = (lane & 1) * 4;
1165 uint8_t l = intel_dp_link_status(link_status, i);
1166
1167 return (l >> s) & 0xf;
1168}
1169
1170/* Check for clock recovery is done on all channels */
1171static bool
1172intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1173{
1174 int lane;
1175 uint8_t lane_status;
1176
1177 for (lane = 0; lane < lane_count; lane++) {
1178 lane_status = intel_get_lane_status(link_status, lane);
1179 if ((lane_status & DP_LANE_CR_DONE) == 0)
1180 return false;
1181 }
1182 return true;
1183}
1184
1185/* Check to see if channel eq is done on all channels */
1186#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1187 DP_LANE_CHANNEL_EQ_DONE|\
1188 DP_LANE_SYMBOL_LOCKED)
1189static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001190intel_channel_eq_ok(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001191{
1192 uint8_t lane_align;
1193 uint8_t lane_status;
1194 int lane;
1195
Jesse Barnes33a34e42010-09-08 12:42:02 -07001196 lane_align = intel_dp_link_status(intel_dp->link_status,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001197 DP_LANE_ALIGN_STATUS_UPDATED);
1198 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1199 return false;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001200 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1201 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001202 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1203 return false;
1204 }
1205 return true;
1206}
1207
1208static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001209intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001210 uint32_t dp_reg_value,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001211 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001212{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001213 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001214 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001215 int ret;
1216
Chris Wilsonea5b2132010-08-04 13:50:23 +01001217 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1218 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001219
Chris Wilsonea5b2132010-08-04 13:50:23 +01001220 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001221 DP_TRAINING_PATTERN_SET,
1222 dp_train_pat);
1223
Chris Wilsonea5b2132010-08-04 13:50:23 +01001224 ret = intel_dp_aux_native_write(intel_dp,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001225 DP_TRAINING_LANE0_SET,
1226 intel_dp->train_set, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001227 if (ret != 4)
1228 return false;
1229
1230 return true;
1231}
1232
Jesse Barnes33a34e42010-09-08 12:42:02 -07001233/* Enable corresponding port and start training pattern 1 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001234static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001235intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001236{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001237 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001238 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson58e10eb2010-10-03 10:56:11 +01001239 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001240 int i;
1241 uint8_t voltage;
1242 bool clock_recovery = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001243 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001244 u32 reg;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001245 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001246
Keith Packardb99a9d92010-10-03 00:33:05 -07001247 /* Enable output, wait for it to become active */
1248 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1249 POSTING_READ(intel_dp->output_reg);
1250 intel_wait_for_vblank(dev, intel_crtc->pipe);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001251
1252 /* Write the link configuration data */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001253 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1254 intel_dp->link_configuration,
1255 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001256
1257 DP |= DP_PORT_EN;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001258 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001259 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1260 else
1261 DP &= ~DP_LINK_TRAIN_MASK;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001262 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001263 voltage = 0xff;
1264 tries = 0;
1265 clock_recovery = false;
1266 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001267 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001268 uint32_t signal_levels;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001269 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001270 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001271 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1272 } else {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001273 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001274 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1275 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001276
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001277 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001278 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1279 else
1280 reg = DP | DP_LINK_TRAIN_PAT_1;
1281
Chris Wilsonea5b2132010-08-04 13:50:23 +01001282 if (!intel_dp_set_link_train(intel_dp, reg,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001283 DP_TRAINING_PATTERN_1))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001284 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001285 /* Set training pattern 1 */
1286
1287 udelay(100);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001288 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001289 break;
1290
Jesse Barnes33a34e42010-09-08 12:42:02 -07001291 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001292 clock_recovery = true;
1293 break;
1294 }
1295
1296 /* Check to see if we've tried the max voltage */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001297 for (i = 0; i < intel_dp->lane_count; i++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001298 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001299 break;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001300 if (i == intel_dp->lane_count)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001301 break;
1302
1303 /* Check to see if we've tried the same voltage 5 times */
Jesse Barnes33a34e42010-09-08 12:42:02 -07001304 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001305 ++tries;
1306 if (tries == 5)
1307 break;
1308 } else
1309 tries = 0;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001310 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001311
Jesse Barnes33a34e42010-09-08 12:42:02 -07001312 /* Compute new intel_dp->train_set as requested by target */
1313 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001314 }
1315
Jesse Barnes33a34e42010-09-08 12:42:02 -07001316 intel_dp->DP = DP;
1317}
1318
1319static void
1320intel_dp_complete_link_train(struct intel_dp *intel_dp)
1321{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001322 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001323 struct drm_i915_private *dev_priv = dev->dev_private;
1324 bool channel_eq = false;
1325 int tries;
1326 u32 reg;
1327 uint32_t DP = intel_dp->DP;
1328
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001329 /* channel equalization */
1330 tries = 0;
1331 channel_eq = false;
1332 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001333 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001334 uint32_t signal_levels;
1335
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001336 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001337 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001338 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1339 } else {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001340 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001341 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1342 }
1343
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001344 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001345 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1346 else
1347 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001348
1349 /* channel eq pattern */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001350 if (!intel_dp_set_link_train(intel_dp, reg,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001351 DP_TRAINING_PATTERN_2))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001352 break;
1353
1354 udelay(400);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001355 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001356 break;
1357
Jesse Barnes33a34e42010-09-08 12:42:02 -07001358 if (intel_channel_eq_ok(intel_dp)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001359 channel_eq = true;
1360 break;
1361 }
1362
1363 /* Try 5 times */
1364 if (tries > 5)
1365 break;
1366
Jesse Barnes33a34e42010-09-08 12:42:02 -07001367 /* Compute new intel_dp->train_set as requested by target */
1368 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001369 ++tries;
1370 }
1371
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001372 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001373 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1374 else
1375 reg = DP | DP_LINK_TRAIN_OFF;
1376
Chris Wilsonea5b2132010-08-04 13:50:23 +01001377 I915_WRITE(intel_dp->output_reg, reg);
1378 POSTING_READ(intel_dp->output_reg);
1379 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001380 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1381}
1382
1383static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001384intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001385{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001386 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001387 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001388 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001389
Zhao Yakui28c97732009-10-09 11:39:41 +08001390 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001391
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001392 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001393 DP &= ~DP_PLL_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001394 I915_WRITE(intel_dp->output_reg, DP);
1395 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001396 udelay(100);
1397 }
1398
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001399 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001400 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001401 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001402 } else {
1403 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001404 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001405 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01001406 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001407
Chris Wilsonfe255d02010-09-11 21:37:48 +01001408 msleep(17);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001409
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001410 if (is_edp(intel_dp))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001411 DP |= DP_LINK_TRAIN_OFF;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001412 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1413 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001414}
1415
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001416/*
1417 * According to DP spec
1418 * 5.1.2:
1419 * 1. Read DPCD
1420 * 2. Configure link according to Receiver Capabilities
1421 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1422 * 4. Check link status on receipt of hot-plug interrupt
1423 */
1424
1425static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001426intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001427{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001428 if (!intel_dp->base.base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001429 return;
1430
Jesse Barnes33a34e42010-09-08 12:42:02 -07001431 if (!intel_dp_get_link_status(intel_dp)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001432 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001433 return;
1434 }
1435
Jesse Barnes33a34e42010-09-08 12:42:02 -07001436 if (!intel_channel_eq_ok(intel_dp)) {
1437 intel_dp_start_link_train(intel_dp);
1438 intel_dp_complete_link_train(intel_dp);
1439 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001440}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001441
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001442static enum drm_connector_status
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001443ironlake_dp_detect(struct drm_connector *connector)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001444{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001445 struct intel_dp *intel_dp = intel_attached_dp(connector);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001446 enum drm_connector_status status;
1447
Jesse Barnes7eaf5542010-09-08 12:41:59 -07001448 /* Panel needs power for AUX to work */
Jesse Barnes4d926462010-10-07 16:01:07 -07001449 if (is_edp(intel_dp))
Jesse Barnesb2094bb2010-09-08 12:42:01 -07001450 ironlake_edp_panel_vdd_on(connector->dev);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001451 status = connector_status_disconnected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001452 if (intel_dp_aux_native_read(intel_dp,
1453 0x000, intel_dp->dpcd,
1454 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001455 {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001456 if (intel_dp->dpcd[0] != 0)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001457 status = connector_status_connected;
1458 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001459 DRM_DEBUG_KMS("DPCD: %hx%hx%hx%hx\n", intel_dp->dpcd[0],
1460 intel_dp->dpcd[1], intel_dp->dpcd[2], intel_dp->dpcd[3]);
Jesse Barnes4d926462010-10-07 16:01:07 -07001461 if (is_edp(intel_dp))
Jesse Barnesb2094bb2010-09-08 12:42:01 -07001462 ironlake_edp_panel_vdd_off(connector->dev);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001463 return status;
1464}
1465
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001466/**
1467 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1468 *
1469 * \return true if DP port is connected.
1470 * \return false if DP port is disconnected.
1471 */
1472static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +01001473intel_dp_detect(struct drm_connector *connector, bool force)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001474{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001475 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001476 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001477 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001478 uint32_t temp, bit;
1479 enum drm_connector_status status;
1480
Chris Wilsonea5b2132010-08-04 13:50:23 +01001481 intel_dp->has_audio = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001482
Eric Anholtc619eed2010-01-28 16:45:52 -08001483 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001484 return ironlake_dp_detect(connector);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001485
Chris Wilsonea5b2132010-08-04 13:50:23 +01001486 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001487 case DP_B:
1488 bit = DPB_HOTPLUG_INT_STATUS;
1489 break;
1490 case DP_C:
1491 bit = DPC_HOTPLUG_INT_STATUS;
1492 break;
1493 case DP_D:
1494 bit = DPD_HOTPLUG_INT_STATUS;
1495 break;
1496 default:
1497 return connector_status_unknown;
1498 }
1499
1500 temp = I915_READ(PORT_HOTPLUG_STAT);
1501
1502 if ((temp & bit) == 0)
1503 return connector_status_disconnected;
1504
1505 status = connector_status_disconnected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001506 if (intel_dp_aux_native_read(intel_dp,
1507 0x000, intel_dp->dpcd,
1508 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001509 {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001510 if (intel_dp->dpcd[0] != 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001511 status = connector_status_connected;
1512 }
1513 return status;
1514}
1515
1516static int intel_dp_get_modes(struct drm_connector *connector)
1517{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001518 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001519 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001520 struct drm_i915_private *dev_priv = dev->dev_private;
1521 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001522
1523 /* We should parse the EDID data and find out if it has an audio sink
1524 */
1525
Chris Wilsonf899fc62010-07-20 15:44:45 -07001526 ret = intel_ddc_get_modes(connector, &intel_dp->adapter);
Zhao Yakuib9efc482010-07-19 09:43:11 +01001527 if (ret) {
Jesse Barnes4d926462010-10-07 16:01:07 -07001528 if (is_edp(intel_dp) && !dev_priv->panel_fixed_mode) {
Zhao Yakuib9efc482010-07-19 09:43:11 +01001529 struct drm_display_mode *newmode;
1530 list_for_each_entry(newmode, &connector->probed_modes,
1531 head) {
1532 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1533 dev_priv->panel_fixed_mode =
1534 drm_mode_duplicate(dev, newmode);
1535 break;
1536 }
1537 }
1538 }
1539
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001540 return ret;
Zhao Yakuib9efc482010-07-19 09:43:11 +01001541 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001542
1543 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Jesse Barnes4d926462010-10-07 16:01:07 -07001544 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001545 if (dev_priv->panel_fixed_mode != NULL) {
1546 struct drm_display_mode *mode;
1547 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1548 drm_mode_probed_add(connector, mode);
1549 return 1;
1550 }
1551 }
1552 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001553}
1554
1555static void
1556intel_dp_destroy (struct drm_connector *connector)
1557{
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001558 drm_sysfs_connector_remove(connector);
1559 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001560 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001561}
1562
Daniel Vetter24d05922010-08-20 18:08:28 +02001563static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1564{
1565 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1566
1567 i2c_del_adapter(&intel_dp->adapter);
1568 drm_encoder_cleanup(encoder);
1569 kfree(intel_dp);
1570}
1571
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001572static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1573 .dpms = intel_dp_dpms,
1574 .mode_fixup = intel_dp_mode_fixup,
Jesse Barnesd240f202010-08-13 15:43:26 -07001575 .prepare = intel_dp_prepare,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001576 .mode_set = intel_dp_mode_set,
Jesse Barnesd240f202010-08-13 15:43:26 -07001577 .commit = intel_dp_commit,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001578};
1579
1580static const struct drm_connector_funcs intel_dp_connector_funcs = {
1581 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001582 .detect = intel_dp_detect,
1583 .fill_modes = drm_helper_probe_single_connector_modes,
1584 .destroy = intel_dp_destroy,
1585};
1586
1587static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1588 .get_modes = intel_dp_get_modes,
1589 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01001590 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001591};
1592
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001593static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02001594 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001595};
1596
Chris Wilson995b6762010-08-20 13:23:26 +01001597static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001598intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07001599{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001600 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Keith Packardc8110e52009-05-06 11:51:10 -07001601
Chris Wilsonea5b2132010-08-04 13:50:23 +01001602 if (intel_dp->dpms_mode == DRM_MODE_DPMS_ON)
1603 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07001604}
1605
Zhenyu Wange3421a12010-04-08 09:43:27 +08001606/* Return which DP Port should be selected for Transcoder DP control */
1607int
1608intel_trans_dp_port_sel (struct drm_crtc *crtc)
1609{
1610 struct drm_device *dev = crtc->dev;
1611 struct drm_mode_config *mode_config = &dev->mode_config;
1612 struct drm_encoder *encoder;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001613
1614 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001615 struct intel_dp *intel_dp;
1616
Dan Carpenterd8201ab2010-05-07 10:39:00 +02001617 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08001618 continue;
1619
Chris Wilsonea5b2132010-08-04 13:50:23 +01001620 intel_dp = enc_to_intel_dp(encoder);
1621 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
1622 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001623 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001624
Zhenyu Wange3421a12010-04-08 09:43:27 +08001625 return -1;
1626}
1627
Zhao Yakui36e83a12010-06-12 14:32:21 +08001628/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04001629bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08001630{
1631 struct drm_i915_private *dev_priv = dev->dev_private;
1632 struct child_device_config *p_child;
1633 int i;
1634
1635 if (!dev_priv->child_dev_num)
1636 return false;
1637
1638 for (i = 0; i < dev_priv->child_dev_num; i++) {
1639 p_child = dev_priv->child_dev + i;
1640
1641 if (p_child->dvo_port == PORT_IDPD &&
1642 p_child->device_type == DEVICE_TYPE_eDP)
1643 return true;
1644 }
1645 return false;
1646}
1647
Keith Packardc8110e52009-05-06 11:51:10 -07001648void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001649intel_dp_init(struct drm_device *dev, int output_reg)
1650{
1651 struct drm_i915_private *dev_priv = dev->dev_private;
1652 struct drm_connector *connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001653 struct intel_dp *intel_dp;
Eric Anholt21d40d32010-03-25 11:11:14 -07001654 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001655 struct intel_connector *intel_connector;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001656 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04001657 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001658
Chris Wilsonea5b2132010-08-04 13:50:23 +01001659 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
1660 if (!intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001661 return;
1662
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001663 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1664 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001665 kfree(intel_dp);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001666 return;
1667 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001668 intel_encoder = &intel_dp->base;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001669
Chris Wilsonea5b2132010-08-04 13:50:23 +01001670 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04001671 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01001672 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04001673
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001674 if (output_reg == DP_A || is_pch_edp(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04001675 type = DRM_MODE_CONNECTOR_eDP;
1676 intel_encoder->type = INTEL_OUTPUT_EDP;
1677 } else {
1678 type = DRM_MODE_CONNECTOR_DisplayPort;
1679 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1680 }
1681
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001682 connector = &intel_connector->base;
Adam Jacksonb3295302010-07-16 14:46:28 -04001683 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001684 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1685
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001686 connector->polled = DRM_CONNECTOR_POLL_HPD;
1687
Zhao Yakui652af9d2009-12-02 10:03:33 +08001688 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07001689 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001690 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07001691 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001692 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07001693 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08001694
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001695 if (is_edp(intel_dp))
Eric Anholt21d40d32010-03-25 11:11:14 -07001696 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08001697
Eric Anholt21d40d32010-03-25 11:11:14 -07001698 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001699 connector->interlace_allowed = true;
1700 connector->doublescan_allowed = 0;
1701
Chris Wilsonea5b2132010-08-04 13:50:23 +01001702 intel_dp->output_reg = output_reg;
1703 intel_dp->has_audio = false;
1704 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001705
Chris Wilson4ef69c72010-09-09 15:14:28 +01001706 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001707 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001708 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001709
Chris Wilsondf0e9242010-09-09 16:20:55 +01001710 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001711 drm_sysfs_connector_add(connector);
1712
1713 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001714 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001715 case DP_A:
1716 name = "DPDDC-A";
1717 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001718 case DP_B:
1719 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001720 dev_priv->hotplug_supported_mask |=
1721 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001722 name = "DPDDC-B";
1723 break;
1724 case DP_C:
1725 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001726 dev_priv->hotplug_supported_mask |=
1727 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001728 name = "DPDDC-C";
1729 break;
1730 case DP_D:
1731 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001732 dev_priv->hotplug_supported_mask |=
1733 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001734 name = "DPDDC-D";
1735 break;
1736 }
1737
Chris Wilsonea5b2132010-08-04 13:50:23 +01001738 intel_dp_i2c_init(intel_dp, intel_connector, name);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001739
Eric Anholt21d40d32010-03-25 11:11:14 -07001740 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001741
Jesse Barnes4d926462010-10-07 16:01:07 -07001742 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001743 /* initialize panel mode from VBT if available for eDP */
1744 if (dev_priv->lfp_lvds_vbt_mode) {
1745 dev_priv->panel_fixed_mode =
1746 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1747 if (dev_priv->panel_fixed_mode) {
1748 dev_priv->panel_fixed_mode->type |=
1749 DRM_MODE_TYPE_PREFERRED;
1750 }
1751 }
1752 }
1753
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001754 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1755 * 0xd. Failure to do so will result in spurious interrupts being
1756 * generated on the port when a cable is not attached.
1757 */
1758 if (IS_G4X(dev) && !IS_GM45(dev)) {
1759 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
1760 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
1761 }
1762}