blob: ada7319f0eaf63343a60711b66d72deca5dc4230 [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 Barnes814948a2010-10-07 16:01:09 -070098/**
99 * intel_encoder_is_pch_edp - is the given encoder a PCH attached eDP?
100 * @encoder: DRM encoder
101 *
102 * Return true if @encoder corresponds to a PCH attached eDP panel. Needed
103 * by intel_display.c.
104 */
105bool intel_encoder_is_pch_edp(struct drm_encoder *encoder)
106{
107 struct intel_dp *intel_dp;
108
109 if (!encoder)
110 return false;
111
112 intel_dp = enc_to_intel_dp(encoder);
113
114 return is_pch_edp(intel_dp);
115}
116
Jesse Barnes33a34e42010-09-08 12:42:02 -0700117static void intel_dp_start_link_train(struct intel_dp *intel_dp);
118static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100119static void intel_dp_link_down(struct intel_dp *intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700120
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800121void
Eric Anholt21d40d32010-03-25 11:11:14 -0700122intel_edp_link_config (struct intel_encoder *intel_encoder,
Chris Wilsonea5b2132010-08-04 13:50:23 +0100123 int *lane_num, int *link_bw)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800124{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100125 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800126
Chris Wilsonea5b2132010-08-04 13:50:23 +0100127 *lane_num = intel_dp->lane_count;
128 if (intel_dp->link_bw == DP_LINK_BW_1_62)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800129 *link_bw = 162000;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100130 else if (intel_dp->link_bw == DP_LINK_BW_2_7)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800131 *link_bw = 270000;
132}
133
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700134static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100135intel_dp_max_lane_count(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700136{
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700137 int max_lane_count = 4;
138
Chris Wilsonea5b2132010-08-04 13:50:23 +0100139 if (intel_dp->dpcd[0] >= 0x11) {
140 max_lane_count = intel_dp->dpcd[2] & 0x1f;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700141 switch (max_lane_count) {
142 case 1: case 2: case 4:
143 break;
144 default:
145 max_lane_count = 4;
146 }
147 }
148 return max_lane_count;
149}
150
151static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100152intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700153{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100154 int max_link_bw = intel_dp->dpcd[1];
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700155
156 switch (max_link_bw) {
157 case DP_LINK_BW_1_62:
158 case DP_LINK_BW_2_7:
159 break;
160 default:
161 max_link_bw = DP_LINK_BW_1_62;
162 break;
163 }
164 return max_link_bw;
165}
166
167static int
168intel_dp_link_clock(uint8_t link_bw)
169{
170 if (link_bw == DP_LINK_BW_2_7)
171 return 270000;
172 else
173 return 162000;
174}
175
176/* I think this is a fiction */
177static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100178intel_dp_link_required(struct drm_device *dev, struct intel_dp *intel_dp, int pixel_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700179{
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800180 struct drm_i915_private *dev_priv = dev->dev_private;
181
Jesse Barnes4d926462010-10-07 16:01:07 -0700182 if (is_edp(intel_dp))
Chris Wilson5ceb0f92010-09-24 10:24:28 +0100183 return (pixel_clock * dev_priv->edp.bpp + 7) / 8;
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800184 else
185 return pixel_clock * 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700186}
187
188static int
Dave Airliefe27d532010-06-30 11:46:17 +1000189intel_dp_max_data_rate(int max_link_clock, int max_lanes)
190{
191 return (max_link_clock * max_lanes * 8) / 10;
192}
193
194static int
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700195intel_dp_mode_valid(struct drm_connector *connector,
196 struct drm_display_mode *mode)
197{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100198 struct intel_dp *intel_dp = intel_attached_dp(connector);
Zhao Yakui7de56f42010-07-19 09:43:14 +0100199 struct drm_device *dev = connector->dev;
200 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100201 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
202 int max_lanes = intel_dp_max_lane_count(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700203
Jesse Barnes4d926462010-10-07 16:01:07 -0700204 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Zhao Yakui7de56f42010-07-19 09:43:14 +0100205 if (mode->hdisplay > dev_priv->panel_fixed_mode->hdisplay)
206 return MODE_PANEL;
207
208 if (mode->vdisplay > dev_priv->panel_fixed_mode->vdisplay)
209 return MODE_PANEL;
210 }
211
Dave Airliefe27d532010-06-30 11:46:17 +1000212 /* only refuse the mode on non eDP since we have seen some wierd eDP panels
213 which are outside spec tolerances but somehow work by magic */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700214 if (!is_edp(intel_dp) &&
Chris Wilsonea5b2132010-08-04 13:50:23 +0100215 (intel_dp_link_required(connector->dev, intel_dp, mode->clock)
Dave Airliefe27d532010-06-30 11:46:17 +1000216 > intel_dp_max_data_rate(max_link_clock, max_lanes)))
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700217 return MODE_CLOCK_HIGH;
218
219 if (mode->clock < 10000)
220 return MODE_CLOCK_LOW;
221
222 return MODE_OK;
223}
224
225static uint32_t
226pack_aux(uint8_t *src, int src_bytes)
227{
228 int i;
229 uint32_t v = 0;
230
231 if (src_bytes > 4)
232 src_bytes = 4;
233 for (i = 0; i < src_bytes; i++)
234 v |= ((uint32_t) src[i]) << ((3-i) * 8);
235 return v;
236}
237
238static void
239unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
240{
241 int i;
242 if (dst_bytes > 4)
243 dst_bytes = 4;
244 for (i = 0; i < dst_bytes; i++)
245 dst[i] = src >> ((3-i) * 8);
246}
247
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700248/* hrawclock is 1/4 the FSB frequency */
249static int
250intel_hrawclk(struct drm_device *dev)
251{
252 struct drm_i915_private *dev_priv = dev->dev_private;
253 uint32_t clkcfg;
254
255 clkcfg = I915_READ(CLKCFG);
256 switch (clkcfg & CLKCFG_FSB_MASK) {
257 case CLKCFG_FSB_400:
258 return 100;
259 case CLKCFG_FSB_533:
260 return 133;
261 case CLKCFG_FSB_667:
262 return 166;
263 case CLKCFG_FSB_800:
264 return 200;
265 case CLKCFG_FSB_1067:
266 return 266;
267 case CLKCFG_FSB_1333:
268 return 333;
269 /* these two are just a guess; one of them might be right */
270 case CLKCFG_FSB_1600:
271 case CLKCFG_FSB_1600_ALT:
272 return 400;
273 default:
274 return 133;
275 }
276}
277
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700278static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100279intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700280 uint8_t *send, int send_bytes,
281 uint8_t *recv, int recv_size)
282{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100283 uint32_t output_reg = intel_dp->output_reg;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100284 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700285 struct drm_i915_private *dev_priv = dev->dev_private;
286 uint32_t ch_ctl = output_reg + 0x10;
287 uint32_t ch_data = ch_ctl + 4;
288 int i;
289 int recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700290 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700291 uint32_t aux_clock_divider;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800292 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700293
294 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700295 * and would like to run at 2MHz. So, take the
296 * hrawclk value and divide by 2 and use that
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700297 *
298 * Note that PCH attached eDP panels should use a 125MHz input
299 * clock divider.
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700300 */
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700301 if (is_edp(intel_dp) && !is_pch_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +0800302 if (IS_GEN6(dev))
303 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
304 else
305 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
306 } else if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500307 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800308 else
309 aux_clock_divider = intel_hrawclk(dev) / 2;
310
Zhenyu Wange3421a12010-04-08 09:43:27 +0800311 if (IS_GEN6(dev))
312 precharge = 3;
313 else
314 precharge = 5;
315
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100316 if (I915_READ(ch_ctl) & DP_AUX_CH_CTL_SEND_BUSY) {
317 DRM_ERROR("dp_aux_ch not started status 0x%08x\n",
318 I915_READ(ch_ctl));
319 return -EBUSY;
320 }
321
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700322 /* Must try at least 3 times according to DP spec */
323 for (try = 0; try < 5; try++) {
324 /* Load the send data into the aux channel data registers */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100325 for (i = 0; i < send_bytes; i += 4)
326 I915_WRITE(ch_data + i,
327 pack_aux(send + i, send_bytes - i));
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700328
329 /* Send the command and wait for it to complete */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100330 I915_WRITE(ch_ctl,
331 DP_AUX_CH_CTL_SEND_BUSY |
332 DP_AUX_CH_CTL_TIME_OUT_400us |
333 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
334 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
335 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
336 DP_AUX_CH_CTL_DONE |
337 DP_AUX_CH_CTL_TIME_OUT_ERROR |
338 DP_AUX_CH_CTL_RECEIVE_ERROR);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700339 for (;;) {
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700340 status = I915_READ(ch_ctl);
341 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
342 break;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100343 udelay(100);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700344 }
345
346 /* Clear done status and any errors */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100347 I915_WRITE(ch_ctl,
348 status |
349 DP_AUX_CH_CTL_DONE |
350 DP_AUX_CH_CTL_TIME_OUT_ERROR |
351 DP_AUX_CH_CTL_RECEIVE_ERROR);
352 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700353 break;
354 }
355
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700356 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700357 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700358 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700359 }
360
361 /* Check for timeout or receive error.
362 * Timeouts occur when the sink is not connected
363 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700364 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700365 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700366 return -EIO;
367 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700368
369 /* Timeouts occur when the device isn't connected, so they're
370 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700371 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800372 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700373 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700374 }
375
376 /* Unload any bytes sent back from the other side */
377 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
378 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700379 if (recv_bytes > recv_size)
380 recv_bytes = recv_size;
381
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100382 for (i = 0; i < recv_bytes; i += 4)
383 unpack_aux(I915_READ(ch_data + i),
384 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700385
386 return recv_bytes;
387}
388
389/* Write data to the aux channel in native mode */
390static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100391intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700392 uint16_t address, uint8_t *send, int send_bytes)
393{
394 int ret;
395 uint8_t msg[20];
396 int msg_bytes;
397 uint8_t ack;
398
399 if (send_bytes > 16)
400 return -1;
401 msg[0] = AUX_NATIVE_WRITE << 4;
402 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800403 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700404 msg[3] = send_bytes - 1;
405 memcpy(&msg[4], send, send_bytes);
406 msg_bytes = send_bytes + 4;
407 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100408 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700409 if (ret < 0)
410 return ret;
411 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
412 break;
413 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
414 udelay(100);
415 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700416 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700417 }
418 return send_bytes;
419}
420
421/* Write a single byte to the aux channel in native mode */
422static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100423intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700424 uint16_t address, uint8_t byte)
425{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100426 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700427}
428
429/* read bytes from a native aux channel */
430static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100431intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700432 uint16_t address, uint8_t *recv, int recv_bytes)
433{
434 uint8_t msg[4];
435 int msg_bytes;
436 uint8_t reply[20];
437 int reply_bytes;
438 uint8_t ack;
439 int ret;
440
441 msg[0] = AUX_NATIVE_READ << 4;
442 msg[1] = address >> 8;
443 msg[2] = address & 0xff;
444 msg[3] = recv_bytes - 1;
445
446 msg_bytes = 4;
447 reply_bytes = recv_bytes + 1;
448
449 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100450 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700451 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700452 if (ret == 0)
453 return -EPROTO;
454 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700455 return ret;
456 ack = reply[0];
457 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
458 memcpy(recv, reply + 1, ret - 1);
459 return ret - 1;
460 }
461 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
462 udelay(100);
463 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700464 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700465 }
466}
467
468static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000469intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
470 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700471{
Dave Airlieab2c0672009-12-04 10:55:24 +1000472 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100473 struct intel_dp *intel_dp = container_of(adapter,
474 struct intel_dp,
475 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000476 uint16_t address = algo_data->address;
477 uint8_t msg[5];
478 uint8_t reply[2];
479 int msg_bytes;
480 int reply_bytes;
481 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700482
Dave Airlieab2c0672009-12-04 10:55:24 +1000483 /* Set up the command byte */
484 if (mode & MODE_I2C_READ)
485 msg[0] = AUX_I2C_READ << 4;
486 else
487 msg[0] = AUX_I2C_WRITE << 4;
488
489 if (!(mode & MODE_I2C_STOP))
490 msg[0] |= AUX_I2C_MOT << 4;
491
492 msg[1] = address >> 8;
493 msg[2] = address;
494
495 switch (mode) {
496 case MODE_I2C_WRITE:
497 msg[3] = 0;
498 msg[4] = write_byte;
499 msg_bytes = 5;
500 reply_bytes = 1;
501 break;
502 case MODE_I2C_READ:
503 msg[3] = 0;
504 msg_bytes = 4;
505 reply_bytes = 2;
506 break;
507 default:
508 msg_bytes = 3;
509 reply_bytes = 1;
510 break;
511 }
512
513 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100514 ret = intel_dp_aux_ch(intel_dp,
Dave Airlieab2c0672009-12-04 10:55:24 +1000515 msg, msg_bytes,
516 reply, reply_bytes);
517 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000518 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000519 return ret;
520 }
521 switch (reply[0] & AUX_I2C_REPLY_MASK) {
522 case AUX_I2C_REPLY_ACK:
523 if (mode == MODE_I2C_READ) {
524 *read_byte = reply[1];
525 }
526 return reply_bytes - 1;
527 case AUX_I2C_REPLY_NACK:
Dave Airlie3ff99162009-12-08 14:03:47 +1000528 DRM_DEBUG_KMS("aux_ch nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000529 return -EREMOTEIO;
530 case AUX_I2C_REPLY_DEFER:
Dave Airlie3ff99162009-12-08 14:03:47 +1000531 DRM_DEBUG_KMS("aux_ch defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000532 udelay(100);
533 break;
534 default:
535 DRM_ERROR("aux_ch invalid reply 0x%02x\n", reply[0]);
536 return -EREMOTEIO;
537 }
538 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700539}
540
541static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100542intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800543 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700544{
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800545 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100546 intel_dp->algo.running = false;
547 intel_dp->algo.address = 0;
548 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700549
Chris Wilsonea5b2132010-08-04 13:50:23 +0100550 memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
551 intel_dp->adapter.owner = THIS_MODULE;
552 intel_dp->adapter.class = I2C_CLASS_DDC;
553 strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
554 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
555 intel_dp->adapter.algo_data = &intel_dp->algo;
556 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
557
558 return i2c_dp_aux_add_bus(&intel_dp->adapter);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700559}
560
561static bool
562intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
563 struct drm_display_mode *adjusted_mode)
564{
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100565 struct drm_device *dev = encoder->dev;
566 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100567 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700568 int lane_count, clock;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100569 int max_lane_count = intel_dp_max_lane_count(intel_dp);
570 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700571 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
572
Jesse Barnes4d926462010-10-07 16:01:07 -0700573 if (is_edp(intel_dp) && dev_priv->panel_fixed_mode) {
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100574 intel_fixed_panel_mode(dev_priv->panel_fixed_mode, adjusted_mode);
575 intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
576 mode, adjusted_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100577 /*
578 * the mode->clock is used to calculate the Data&Link M/N
579 * of the pipe. For the eDP the fixed clock should be used.
580 */
581 mode->clock = dev_priv->panel_fixed_mode->clock;
582 }
583
Jesse Barnes869184a2010-10-07 16:01:22 -0700584 /* Just use VBT values for eDP */
585 if (is_edp(intel_dp)) {
586 intel_dp->lane_count = dev_priv->edp.lanes;
587 intel_dp->link_bw = dev_priv->edp.rate;
588 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
589 DRM_DEBUG_KMS("eDP link bw %02x lane count %d clock %d\n",
590 intel_dp->link_bw, intel_dp->lane_count,
591 adjusted_mode->clock);
592 return true;
593 }
594
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700595 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
596 for (clock = 0; clock <= max_clock; clock++) {
Dave Airliefe27d532010-06-30 11:46:17 +1000597 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700598
Chris Wilsonea5b2132010-08-04 13:50:23 +0100599 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800600 <= link_avail) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100601 intel_dp->link_bw = bws[clock];
602 intel_dp->lane_count = lane_count;
603 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Zhao Yakui28c97732009-10-09 11:39:41 +0800604 DRM_DEBUG_KMS("Display port link bw %02x lane "
605 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100606 intel_dp->link_bw, intel_dp->lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700607 adjusted_mode->clock);
608 return true;
609 }
610 }
611 }
Dave Airliefe27d532010-06-30 11:46:17 +1000612
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700613 return false;
614}
615
616struct intel_dp_m_n {
617 uint32_t tu;
618 uint32_t gmch_m;
619 uint32_t gmch_n;
620 uint32_t link_m;
621 uint32_t link_n;
622};
623
624static void
625intel_reduce_ratio(uint32_t *num, uint32_t *den)
626{
627 while (*num > 0xffffff || *den > 0xffffff) {
628 *num >>= 1;
629 *den >>= 1;
630 }
631}
632
633static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800634intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700635 int nlanes,
636 int pixel_clock,
637 int link_clock,
638 struct intel_dp_m_n *m_n)
639{
640 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800641 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700642 m_n->gmch_n = link_clock * nlanes;
643 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
644 m_n->link_m = pixel_clock;
645 m_n->link_n = link_clock;
646 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
647}
648
649void
650intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
651 struct drm_display_mode *adjusted_mode)
652{
653 struct drm_device *dev = crtc->dev;
654 struct drm_mode_config *mode_config = &dev->mode_config;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800655 struct drm_encoder *encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700656 struct drm_i915_private *dev_priv = dev->dev_private;
657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Zhao Yakui36e83a12010-06-12 14:32:21 +0800658 int lane_count = 4, bpp = 24;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700659 struct intel_dp_m_n m_n;
660
661 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700662 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700663 */
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800664 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100665 struct intel_dp *intel_dp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700666
Dan Carpenterd8201ab2010-05-07 10:39:00 +0200667 if (encoder->crtc != crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700668 continue;
669
Chris Wilsonea5b2132010-08-04 13:50:23 +0100670 intel_dp = enc_to_intel_dp(encoder);
671 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
672 lane_count = intel_dp->lane_count;
Jesse Barnes51190662010-10-07 16:01:08 -0700673 break;
674 } else if (is_edp(intel_dp)) {
675 lane_count = dev_priv->edp.lanes;
676 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 Barnes895692b2010-10-07 16:01:23 -0700778 if (is_edp(intel_dp) && !is_pch_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 */
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700789static bool ironlake_edp_panel_on (struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -0700790{
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700791 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes9934c132010-07-22 13:18:19 -0700792 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700793 u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_STATE_ON_IDLE;
Jesse Barnes9934c132010-07-22 13:18:19 -0700794
Chris Wilson913d8d12010-08-07 11:01:35 +0100795 if (I915_READ(PCH_PP_STATUS) & PP_ON)
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700796 return true;
Jesse Barnes9934c132010-07-22 13:18:19 -0700797
798 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700799
800 /* ILK workaround: disable reset around power sequence */
801 pp &= ~PANEL_POWER_RESET;
802 I915_WRITE(PCH_PP_CONTROL, pp);
803 POSTING_READ(PCH_PP_CONTROL);
804
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700805 pp |= PANEL_UNLOCK_REGS | POWER_TARGET_ON;
Jesse Barnes9934c132010-07-22 13:18:19 -0700806 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700807 POSTING_READ(PCH_PP_CONTROL);
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
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700814 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_on_mask) == idle_on_mask,
815 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100816 DRM_ERROR("panel on wait timed out: 0x%08x\n",
817 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700818
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;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700829 u32 pp, idle_off_mask = PP_ON | PP_SEQUENCE_MASK |
830 PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK;
Jesse Barnes9934c132010-07-22 13:18:19 -0700831
832 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700833
834 /* ILK workaround: disable reset around power sequence */
835 pp &= ~PANEL_POWER_RESET;
836 I915_WRITE(PCH_PP_CONTROL, pp);
837 POSTING_READ(PCH_PP_CONTROL);
838
Jesse Barnes9934c132010-07-22 13:18:19 -0700839 pp &= ~POWER_TARGET_ON;
840 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700841 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700842
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700843 if (wait_for((I915_READ(PCH_PP_STATUS) & idle_off_mask) == 0, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100844 DRM_ERROR("panel off wait timed out: 0x%08x\n",
845 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700846
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700847 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700848 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700849 POSTING_READ(PCH_PP_CONTROL);
Hette Visser27d64332010-09-24 10:51:30 +0100850
851 /* Ouch. We need to wait here for some panels, like Dell e6510
852 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
853 */
854 msleep(300);
Jesse Barnes9934c132010-07-22 13:18:19 -0700855}
856
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500857static void ironlake_edp_backlight_on (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800858{
859 struct drm_i915_private *dev_priv = dev->dev_private;
860 u32 pp;
861
Zhao Yakui28c97732009-10-09 11:39:41 +0800862 DRM_DEBUG_KMS("\n");
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700863 /*
864 * If we enable the backlight right away following a panel power
865 * on, we may see slight flicker as the panel syncs with the eDP
866 * link. So delay a bit to make sure the image is solid before
867 * allowing it to appear.
868 */
869 msleep(300);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800870 pp = I915_READ(PCH_PP_CONTROL);
871 pp |= EDP_BLC_ENABLE;
872 I915_WRITE(PCH_PP_CONTROL, pp);
873}
874
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500875static void ironlake_edp_backlight_off (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800876{
877 struct drm_i915_private *dev_priv = dev->dev_private;
878 u32 pp;
879
Zhao Yakui28c97732009-10-09 11:39:41 +0800880 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800881 pp = I915_READ(PCH_PP_CONTROL);
882 pp &= ~EDP_BLC_ENABLE;
883 I915_WRITE(PCH_PP_CONTROL, pp);
884}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700885
Jesse Barnesd240f202010-08-13 15:43:26 -0700886static void ironlake_edp_pll_on(struct drm_encoder *encoder)
887{
888 struct drm_device *dev = encoder->dev;
889 struct drm_i915_private *dev_priv = dev->dev_private;
890 u32 dpa_ctl;
891
892 DRM_DEBUG_KMS("\n");
893 dpa_ctl = I915_READ(DP_A);
894 dpa_ctl &= ~DP_PLL_ENABLE;
895 I915_WRITE(DP_A, dpa_ctl);
896}
897
898static void ironlake_edp_pll_off(struct drm_encoder *encoder)
899{
900 struct drm_device *dev = encoder->dev;
901 struct drm_i915_private *dev_priv = dev->dev_private;
902 u32 dpa_ctl;
903
904 dpa_ctl = I915_READ(DP_A);
905 dpa_ctl |= DP_PLL_ENABLE;
906 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +0100907 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -0700908 udelay(200);
909}
910
911static void intel_dp_prepare(struct drm_encoder *encoder)
912{
913 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
914 struct drm_device *dev = encoder->dev;
915 struct drm_i915_private *dev_priv = dev->dev_private;
916 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
917
Jesse Barnes4d926462010-10-07 16:01:07 -0700918 if (is_edp(intel_dp)) {
Jesse Barnesd240f202010-08-13 15:43:26 -0700919 ironlake_edp_backlight_off(dev);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700920 ironlake_edp_panel_on(intel_dp);
921 if (!is_pch_edp(intel_dp))
922 ironlake_edp_pll_on(encoder);
923 else
924 ironlake_edp_pll_off(encoder);
Jesse Barnesd240f202010-08-13 15:43:26 -0700925 }
926 if (dp_reg & DP_PORT_EN)
927 intel_dp_link_down(intel_dp);
928}
929
930static void intel_dp_commit(struct drm_encoder *encoder)
931{
932 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
933 struct drm_device *dev = encoder->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -0700934
Jesse Barnes33a34e42010-09-08 12:42:02 -0700935 intel_dp_start_link_train(intel_dp);
936
Jesse Barnes4d926462010-10-07 16:01:07 -0700937 if (is_edp(intel_dp))
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700938 ironlake_edp_panel_on(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700939
940 intel_dp_complete_link_train(intel_dp);
941
Jesse Barnes4d926462010-10-07 16:01:07 -0700942 if (is_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -0700943 ironlake_edp_backlight_on(dev);
944}
945
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700946static void
947intel_dp_dpms(struct drm_encoder *encoder, int mode)
948{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100949 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800950 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700951 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100952 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700953
954 if (mode != DRM_MODE_DPMS_ON) {
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700955 if (is_edp(intel_dp))
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700956 ironlake_edp_backlight_off(dev);
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700957 if (dp_reg & DP_PORT_EN)
958 intel_dp_link_down(intel_dp);
Jesse Barnes4d926462010-10-07 16:01:07 -0700959 if (is_edp(intel_dp))
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700960 ironlake_edp_panel_off(dev);
961 if (is_edp(intel_dp) && !is_pch_edp(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -0700962 ironlake_edp_pll_off(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700963 } else {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800964 if (!(dp_reg & DP_PORT_EN)) {
Jesse Barnes4d926462010-10-07 16:01:07 -0700965 if (is_edp(intel_dp))
Jesse Barnes01cb9ea2010-10-07 16:01:12 -0700966 ironlake_edp_panel_on(intel_dp);
967 intel_dp_start_link_train(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700968 intel_dp_complete_link_train(intel_dp);
Jesse Barnes4d926462010-10-07 16:01:07 -0700969 if (is_edp(intel_dp))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500970 ironlake_edp_backlight_on(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800971 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700972 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100973 intel_dp->dpms_mode = mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700974}
975
976/*
977 * Fetch AUX CH registers 0x202 - 0x207 which contain
978 * link status information
979 */
980static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -0700981intel_dp_get_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700982{
983 int ret;
984
Chris Wilsonea5b2132010-08-04 13:50:23 +0100985 ret = intel_dp_aux_native_read(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700986 DP_LANE0_1_STATUS,
Jesse Barnes33a34e42010-09-08 12:42:02 -0700987 intel_dp->link_status, DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700988 if (ret != DP_LINK_STATUS_SIZE)
989 return false;
990 return true;
991}
992
993static uint8_t
994intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
995 int r)
996{
997 return link_status[r - DP_LANE0_1_STATUS];
998}
999
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001000static uint8_t
1001intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
1002 int lane)
1003{
1004 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1005 int s = ((lane & 1) ?
1006 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1007 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1008 uint8_t l = intel_dp_link_status(link_status, i);
1009
1010 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1011}
1012
1013static uint8_t
1014intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1015 int lane)
1016{
1017 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1018 int s = ((lane & 1) ?
1019 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1020 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1021 uint8_t l = intel_dp_link_status(link_status, i);
1022
1023 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1024}
1025
1026
1027#if 0
1028static char *voltage_names[] = {
1029 "0.4V", "0.6V", "0.8V", "1.2V"
1030};
1031static char *pre_emph_names[] = {
1032 "0dB", "3.5dB", "6dB", "9.5dB"
1033};
1034static char *link_train_names[] = {
1035 "pattern 1", "pattern 2", "idle", "off"
1036};
1037#endif
1038
1039/*
1040 * These are source-specific values; current Intel hardware supports
1041 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1042 */
1043#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1044
1045static uint8_t
1046intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1047{
1048 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1049 case DP_TRAIN_VOLTAGE_SWING_400:
1050 return DP_TRAIN_PRE_EMPHASIS_6;
1051 case DP_TRAIN_VOLTAGE_SWING_600:
1052 return DP_TRAIN_PRE_EMPHASIS_6;
1053 case DP_TRAIN_VOLTAGE_SWING_800:
1054 return DP_TRAIN_PRE_EMPHASIS_3_5;
1055 case DP_TRAIN_VOLTAGE_SWING_1200:
1056 default:
1057 return DP_TRAIN_PRE_EMPHASIS_0;
1058 }
1059}
1060
1061static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001062intel_get_adjust_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001063{
1064 uint8_t v = 0;
1065 uint8_t p = 0;
1066 int lane;
1067
Jesse Barnes33a34e42010-09-08 12:42:02 -07001068 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1069 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1070 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001071
1072 if (this_v > v)
1073 v = this_v;
1074 if (this_p > p)
1075 p = this_p;
1076 }
1077
1078 if (v >= I830_DP_VOLTAGE_MAX)
1079 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1080
1081 if (p >= intel_dp_pre_emphasis_max(v))
1082 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1083
1084 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001085 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001086}
1087
1088static uint32_t
Jesse Barnes869184a2010-10-07 16:01:22 -07001089intel_dp_signal_levels(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001090{
Jesse Barnes869184a2010-10-07 16:01:22 -07001091 struct drm_device *dev = intel_dp->base.base.dev;
1092 struct drm_i915_private *dev_priv = dev->dev_private;
1093 uint32_t signal_levels = 0;
1094 u8 train_set = intel_dp->train_set[0];
1095 u32 vswing = train_set & DP_TRAIN_VOLTAGE_SWING_MASK;
1096 u32 preemphasis = train_set & DP_TRAIN_PRE_EMPHASIS_MASK;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001097
Jesse Barnes869184a2010-10-07 16:01:22 -07001098 if (is_edp(intel_dp)) {
1099 vswing = dev_priv->edp.vswing;
1100 preemphasis = dev_priv->edp.preemphasis;
1101 }
1102
1103 switch (vswing) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001104 case DP_TRAIN_VOLTAGE_SWING_400:
1105 default:
1106 signal_levels |= DP_VOLTAGE_0_4;
1107 break;
1108 case DP_TRAIN_VOLTAGE_SWING_600:
1109 signal_levels |= DP_VOLTAGE_0_6;
1110 break;
1111 case DP_TRAIN_VOLTAGE_SWING_800:
1112 signal_levels |= DP_VOLTAGE_0_8;
1113 break;
1114 case DP_TRAIN_VOLTAGE_SWING_1200:
1115 signal_levels |= DP_VOLTAGE_1_2;
1116 break;
1117 }
Jesse Barnes869184a2010-10-07 16:01:22 -07001118 switch (preemphasis) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001119 case DP_TRAIN_PRE_EMPHASIS_0:
1120 default:
1121 signal_levels |= DP_PRE_EMPHASIS_0;
1122 break;
1123 case DP_TRAIN_PRE_EMPHASIS_3_5:
1124 signal_levels |= DP_PRE_EMPHASIS_3_5;
1125 break;
1126 case DP_TRAIN_PRE_EMPHASIS_6:
1127 signal_levels |= DP_PRE_EMPHASIS_6;
1128 break;
1129 case DP_TRAIN_PRE_EMPHASIS_9_5:
1130 signal_levels |= DP_PRE_EMPHASIS_9_5;
1131 break;
1132 }
1133 return signal_levels;
1134}
1135
Zhenyu Wange3421a12010-04-08 09:43:27 +08001136/* Gen6's DP voltage swing and pre-emphasis control */
1137static uint32_t
1138intel_gen6_edp_signal_levels(uint8_t train_set)
1139{
1140 switch (train_set & (DP_TRAIN_VOLTAGE_SWING_MASK|DP_TRAIN_PRE_EMPHASIS_MASK)) {
1141 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1142 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1143 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1144 return EDP_LINK_TRAIN_400MV_6DB_SNB_B;
1145 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1146 return EDP_LINK_TRAIN_600MV_3_5DB_SNB_B;
1147 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1148 return EDP_LINK_TRAIN_800MV_0DB_SNB_B;
1149 default:
1150 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level\n");
1151 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1152 }
1153}
1154
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001155static uint8_t
1156intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1157 int lane)
1158{
1159 int i = DP_LANE0_1_STATUS + (lane >> 1);
1160 int s = (lane & 1) * 4;
1161 uint8_t l = intel_dp_link_status(link_status, i);
1162
1163 return (l >> s) & 0xf;
1164}
1165
1166/* Check for clock recovery is done on all channels */
1167static bool
1168intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1169{
1170 int lane;
1171 uint8_t lane_status;
1172
1173 for (lane = 0; lane < lane_count; lane++) {
1174 lane_status = intel_get_lane_status(link_status, lane);
1175 if ((lane_status & DP_LANE_CR_DONE) == 0)
1176 return false;
1177 }
1178 return true;
1179}
1180
1181/* Check to see if channel eq is done on all channels */
1182#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1183 DP_LANE_CHANNEL_EQ_DONE|\
1184 DP_LANE_SYMBOL_LOCKED)
1185static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001186intel_channel_eq_ok(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001187{
1188 uint8_t lane_align;
1189 uint8_t lane_status;
1190 int lane;
1191
Jesse Barnes33a34e42010-09-08 12:42:02 -07001192 lane_align = intel_dp_link_status(intel_dp->link_status,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001193 DP_LANE_ALIGN_STATUS_UPDATED);
1194 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1195 return false;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001196 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1197 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001198 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1199 return false;
1200 }
1201 return true;
1202}
1203
1204static bool
Jesse Barnes869184a2010-10-07 16:01:22 -07001205intel_dp_aux_handshake_required(struct intel_dp *intel_dp)
1206{
1207 struct drm_device *dev = intel_dp->base.base.dev;
1208 struct drm_i915_private *dev_priv = dev->dev_private;
1209
1210 if (is_edp(intel_dp) && dev_priv->no_aux_handshake)
1211 return false;
1212
1213 return true;
1214}
1215
1216static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001217intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001218 uint32_t dp_reg_value,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001219 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001220{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001221 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001222 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001223 int ret;
1224
Chris Wilsonea5b2132010-08-04 13:50:23 +01001225 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1226 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001227
Jesse Barnes869184a2010-10-07 16:01:22 -07001228 if (!intel_dp_aux_handshake_required(intel_dp))
1229 return true;
1230
Chris Wilsonea5b2132010-08-04 13:50:23 +01001231 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001232 DP_TRAINING_PATTERN_SET,
1233 dp_train_pat);
1234
Chris Wilsonea5b2132010-08-04 13:50:23 +01001235 ret = intel_dp_aux_native_write(intel_dp,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001236 DP_TRAINING_LANE0_SET,
1237 intel_dp->train_set, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001238 if (ret != 4)
1239 return false;
1240
1241 return true;
1242}
1243
Jesse Barnes33a34e42010-09-08 12:42:02 -07001244/* Enable corresponding port and start training pattern 1 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001245static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001246intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001247{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001248 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001249 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson58e10eb2010-10-03 10:56:11 +01001250 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001251 int i;
1252 uint8_t voltage;
1253 bool clock_recovery = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001254 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001255 u32 reg;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001256 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001257
Keith Packardb99a9d92010-10-03 00:33:05 -07001258 /* Enable output, wait for it to become active */
1259 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
1260 POSTING_READ(intel_dp->output_reg);
1261 intel_wait_for_vblank(dev, intel_crtc->pipe);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001262
Jesse Barnes869184a2010-10-07 16:01:22 -07001263 if (intel_dp_aux_handshake_required(intel_dp))
1264 /* Write the link configuration data */
1265 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1266 intel_dp->link_configuration,
1267 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001268
1269 DP |= DP_PORT_EN;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001270 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001271 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1272 else
1273 DP &= ~DP_LINK_TRAIN_MASK;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001274 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001275 voltage = 0xff;
1276 tries = 0;
1277 clock_recovery = false;
1278 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001279 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001280 uint32_t signal_levels;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001281 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001282 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001283 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1284 } else {
Jesse Barnes869184a2010-10-07 16:01:22 -07001285 signal_levels = intel_dp_signal_levels(intel_dp);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001286 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1287 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001288
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001289 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001290 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1291 else
1292 reg = DP | DP_LINK_TRAIN_PAT_1;
1293
Chris Wilsonea5b2132010-08-04 13:50:23 +01001294 if (!intel_dp_set_link_train(intel_dp, reg,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001295 DP_TRAINING_PATTERN_1))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001296 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001297 /* Set training pattern 1 */
1298
Jesse Barnes869184a2010-10-07 16:01:22 -07001299 udelay(500);
1300 if (intel_dp_aux_handshake_required(intel_dp)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001301 break;
Jesse Barnes869184a2010-10-07 16:01:22 -07001302 } else {
1303 if (!intel_dp_get_link_status(intel_dp))
1304 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001305
Jesse Barnes869184a2010-10-07 16:01:22 -07001306 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
1307 clock_recovery = true;
1308 break;
1309 }
1310
1311 /* Check to see if we've tried the max voltage */
1312 for (i = 0; i < intel_dp->lane_count; i++)
1313 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1314 break;
1315 if (i == intel_dp->lane_count)
1316 break;
1317
1318 /* Check to see if we've tried the same voltage 5 times */
1319 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1320 ++tries;
1321 if (tries == 5)
1322 break;
1323 } else
1324 tries = 0;
1325 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1326
1327 /* Compute new intel_dp->train_set as requested by target */
1328 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001329 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001330 }
1331
Jesse Barnes33a34e42010-09-08 12:42:02 -07001332 intel_dp->DP = DP;
1333}
1334
1335static void
1336intel_dp_complete_link_train(struct intel_dp *intel_dp)
1337{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001338 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001339 struct drm_i915_private *dev_priv = dev->dev_private;
1340 bool channel_eq = false;
1341 int tries;
1342 u32 reg;
1343 uint32_t DP = intel_dp->DP;
1344
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001345 /* channel equalization */
1346 tries = 0;
1347 channel_eq = false;
1348 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001349 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001350 uint32_t signal_levels;
1351
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001352 if (IS_GEN6(dev) && is_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001353 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001354 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1355 } else {
Jesse Barnes869184a2010-10-07 16:01:22 -07001356 signal_levels = intel_dp_signal_levels(intel_dp);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001357 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1358 }
1359
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001360 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001361 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1362 else
1363 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001364
1365 /* channel eq pattern */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001366 if (!intel_dp_set_link_train(intel_dp, reg,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001367 DP_TRAINING_PATTERN_2))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001368 break;
1369
Jesse Barnes869184a2010-10-07 16:01:22 -07001370 udelay(500);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001371
Jesse Barnes869184a2010-10-07 16:01:22 -07001372 if (!intel_dp_aux_handshake_required(intel_dp)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001373 break;
Jesse Barnes869184a2010-10-07 16:01:22 -07001374 } else {
1375 if (!intel_dp_get_link_status(intel_dp))
1376 break;
1377
1378 if (intel_channel_eq_ok(intel_dp)) {
1379 channel_eq = true;
1380 break;
1381 }
1382
1383 /* Try 5 times */
1384 if (tries > 5)
1385 break;
1386
1387 /* Compute new intel_dp->train_set as requested by target */
1388 intel_get_adjust_train(intel_dp);
1389 ++tries;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001390 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001391 }
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001392 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001393 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1394 else
1395 reg = DP | DP_LINK_TRAIN_OFF;
1396
Chris Wilsonea5b2132010-08-04 13:50:23 +01001397 I915_WRITE(intel_dp->output_reg, reg);
1398 POSTING_READ(intel_dp->output_reg);
1399 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001400 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1401}
1402
1403static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001404intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001405{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001406 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001407 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001408 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001409
Zhao Yakui28c97732009-10-09 11:39:41 +08001410 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001411
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001412 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001413 DP &= ~DP_PLL_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001414 I915_WRITE(intel_dp->output_reg, DP);
1415 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001416 udelay(100);
1417 }
1418
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001419 if (HAS_PCH_CPT(dev) && !is_edp(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001420 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001421 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001422 } else {
1423 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001424 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001425 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01001426 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001427
Chris Wilsonfe255d02010-09-11 21:37:48 +01001428 msleep(17);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001429
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001430 if (is_edp(intel_dp))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001431 DP |= DP_LINK_TRAIN_OFF;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001432 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1433 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001434}
1435
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001436/*
1437 * According to DP spec
1438 * 5.1.2:
1439 * 1. Read DPCD
1440 * 2. Configure link according to Receiver Capabilities
1441 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1442 * 4. Check link status on receipt of hot-plug interrupt
1443 */
1444
1445static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001446intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001447{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001448 if (!intel_dp->base.base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001449 return;
1450
Jesse Barnes33a34e42010-09-08 12:42:02 -07001451 if (!intel_dp_get_link_status(intel_dp)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001452 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001453 return;
1454 }
1455
Jesse Barnes33a34e42010-09-08 12:42:02 -07001456 if (!intel_channel_eq_ok(intel_dp)) {
1457 intel_dp_start_link_train(intel_dp);
1458 intel_dp_complete_link_train(intel_dp);
1459 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001460}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001461
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001462static enum drm_connector_status
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001463ironlake_dp_detect(struct drm_connector *connector)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001464{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001465 struct intel_dp *intel_dp = intel_attached_dp(connector);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001466 enum drm_connector_status status;
1467
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001468 /* Can't disconnect eDP */
Jesse Barnes4d926462010-10-07 16:01:07 -07001469 if (is_edp(intel_dp))
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001470 return connector_status_connected;
1471
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001472 status = connector_status_disconnected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001473 if (intel_dp_aux_native_read(intel_dp,
1474 0x000, intel_dp->dpcd,
1475 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001476 {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001477 if (intel_dp->dpcd[0] != 0)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001478 status = connector_status_connected;
1479 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001480 DRM_DEBUG_KMS("DPCD: %hx%hx%hx%hx\n", intel_dp->dpcd[0],
1481 intel_dp->dpcd[1], intel_dp->dpcd[2], intel_dp->dpcd[3]);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001482 return status;
1483}
1484
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001485/**
1486 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1487 *
1488 * \return true if DP port is connected.
1489 * \return false if DP port is disconnected.
1490 */
1491static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +01001492intel_dp_detect(struct drm_connector *connector, bool force)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001493{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001494 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001495 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001496 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001497 uint32_t temp, bit;
1498 enum drm_connector_status status;
1499
Chris Wilsonea5b2132010-08-04 13:50:23 +01001500 intel_dp->has_audio = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001501
Eric Anholtc619eed2010-01-28 16:45:52 -08001502 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001503 return ironlake_dp_detect(connector);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001504
Chris Wilsonea5b2132010-08-04 13:50:23 +01001505 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001506 case DP_B:
1507 bit = DPB_HOTPLUG_INT_STATUS;
1508 break;
1509 case DP_C:
1510 bit = DPC_HOTPLUG_INT_STATUS;
1511 break;
1512 case DP_D:
1513 bit = DPD_HOTPLUG_INT_STATUS;
1514 break;
1515 default:
1516 return connector_status_unknown;
1517 }
1518
1519 temp = I915_READ(PORT_HOTPLUG_STAT);
1520
1521 if ((temp & bit) == 0)
1522 return connector_status_disconnected;
1523
1524 status = connector_status_disconnected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001525 if (intel_dp_aux_native_read(intel_dp,
1526 0x000, intel_dp->dpcd,
1527 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001528 {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001529 if (intel_dp->dpcd[0] != 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001530 status = connector_status_connected;
1531 }
1532 return status;
1533}
1534
1535static int intel_dp_get_modes(struct drm_connector *connector)
1536{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001537 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001538 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001539 struct drm_i915_private *dev_priv = dev->dev_private;
1540 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001541
1542 /* We should parse the EDID data and find out if it has an audio sink
1543 */
1544
Chris Wilsonf899fc62010-07-20 15:44:45 -07001545 ret = intel_ddc_get_modes(connector, &intel_dp->adapter);
Zhao Yakuib9efc482010-07-19 09:43:11 +01001546 if (ret) {
Jesse Barnes4d926462010-10-07 16:01:07 -07001547 if (is_edp(intel_dp) && !dev_priv->panel_fixed_mode) {
Zhao Yakuib9efc482010-07-19 09:43:11 +01001548 struct drm_display_mode *newmode;
1549 list_for_each_entry(newmode, &connector->probed_modes,
1550 head) {
1551 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1552 dev_priv->panel_fixed_mode =
1553 drm_mode_duplicate(dev, newmode);
1554 break;
1555 }
1556 }
1557 }
1558
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001559 return ret;
Zhao Yakuib9efc482010-07-19 09:43:11 +01001560 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001561
1562 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Jesse Barnes4d926462010-10-07 16:01:07 -07001563 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001564 if (dev_priv->panel_fixed_mode != NULL) {
1565 struct drm_display_mode *mode;
1566 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1567 drm_mode_probed_add(connector, mode);
1568 return 1;
1569 }
1570 }
1571 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001572}
1573
1574static void
1575intel_dp_destroy (struct drm_connector *connector)
1576{
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001577 drm_sysfs_connector_remove(connector);
1578 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001579 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001580}
1581
Daniel Vetter24d05922010-08-20 18:08:28 +02001582static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1583{
1584 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1585
1586 i2c_del_adapter(&intel_dp->adapter);
1587 drm_encoder_cleanup(encoder);
1588 kfree(intel_dp);
1589}
1590
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001591static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1592 .dpms = intel_dp_dpms,
1593 .mode_fixup = intel_dp_mode_fixup,
Jesse Barnesd240f202010-08-13 15:43:26 -07001594 .prepare = intel_dp_prepare,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001595 .mode_set = intel_dp_mode_set,
Jesse Barnesd240f202010-08-13 15:43:26 -07001596 .commit = intel_dp_commit,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001597};
1598
1599static const struct drm_connector_funcs intel_dp_connector_funcs = {
1600 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001601 .detect = intel_dp_detect,
1602 .fill_modes = drm_helper_probe_single_connector_modes,
1603 .destroy = intel_dp_destroy,
1604};
1605
1606static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1607 .get_modes = intel_dp_get_modes,
1608 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01001609 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001610};
1611
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001612static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02001613 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001614};
1615
Chris Wilson995b6762010-08-20 13:23:26 +01001616static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001617intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07001618{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001619 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Keith Packardc8110e52009-05-06 11:51:10 -07001620
Chris Wilsonea5b2132010-08-04 13:50:23 +01001621 if (intel_dp->dpms_mode == DRM_MODE_DPMS_ON)
1622 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07001623}
1624
Zhenyu Wange3421a12010-04-08 09:43:27 +08001625/* Return which DP Port should be selected for Transcoder DP control */
1626int
1627intel_trans_dp_port_sel (struct drm_crtc *crtc)
1628{
1629 struct drm_device *dev = crtc->dev;
1630 struct drm_mode_config *mode_config = &dev->mode_config;
1631 struct drm_encoder *encoder;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001632
1633 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001634 struct intel_dp *intel_dp;
1635
Dan Carpenterd8201ab2010-05-07 10:39:00 +02001636 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08001637 continue;
1638
Chris Wilsonea5b2132010-08-04 13:50:23 +01001639 intel_dp = enc_to_intel_dp(encoder);
1640 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
1641 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001642 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001643
Zhenyu Wange3421a12010-04-08 09:43:27 +08001644 return -1;
1645}
1646
Zhao Yakui36e83a12010-06-12 14:32:21 +08001647/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04001648bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08001649{
1650 struct drm_i915_private *dev_priv = dev->dev_private;
1651 struct child_device_config *p_child;
1652 int i;
1653
1654 if (!dev_priv->child_dev_num)
1655 return false;
1656
1657 for (i = 0; i < dev_priv->child_dev_num; i++) {
1658 p_child = dev_priv->child_dev + i;
1659
1660 if (p_child->dvo_port == PORT_IDPD &&
1661 p_child->device_type == DEVICE_TYPE_eDP)
1662 return true;
1663 }
1664 return false;
1665}
1666
Keith Packardc8110e52009-05-06 11:51:10 -07001667void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001668intel_dp_init(struct drm_device *dev, int output_reg)
1669{
1670 struct drm_i915_private *dev_priv = dev->dev_private;
1671 struct drm_connector *connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001672 struct intel_dp *intel_dp;
Eric Anholt21d40d32010-03-25 11:11:14 -07001673 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001674 struct intel_connector *intel_connector;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001675 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04001676 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001677
Chris Wilsonea5b2132010-08-04 13:50:23 +01001678 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
1679 if (!intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001680 return;
1681
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001682 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1683 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001684 kfree(intel_dp);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001685 return;
1686 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001687 intel_encoder = &intel_dp->base;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001688
Chris Wilsonea5b2132010-08-04 13:50:23 +01001689 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04001690 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01001691 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04001692
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001693 if (output_reg == DP_A || is_pch_edp(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04001694 type = DRM_MODE_CONNECTOR_eDP;
1695 intel_encoder->type = INTEL_OUTPUT_EDP;
1696 } else {
1697 type = DRM_MODE_CONNECTOR_DisplayPort;
1698 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1699 }
1700
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001701 connector = &intel_connector->base;
Adam Jacksonb3295302010-07-16 14:46:28 -04001702 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001703 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1704
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001705 connector->polled = DRM_CONNECTOR_POLL_HPD;
1706
Zhao Yakui652af9d2009-12-02 10:03:33 +08001707 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07001708 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001709 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07001710 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001711 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07001712 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08001713
Jesse Barnescfcb0fc2010-10-07 16:01:06 -07001714 if (is_edp(intel_dp))
Eric Anholt21d40d32010-03-25 11:11:14 -07001715 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08001716
Eric Anholt21d40d32010-03-25 11:11:14 -07001717 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001718 connector->interlace_allowed = true;
1719 connector->doublescan_allowed = 0;
1720
Chris Wilsonea5b2132010-08-04 13:50:23 +01001721 intel_dp->output_reg = output_reg;
1722 intel_dp->has_audio = false;
1723 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001724
Chris Wilson4ef69c72010-09-09 15:14:28 +01001725 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001726 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001727 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001728
Chris Wilsondf0e9242010-09-09 16:20:55 +01001729 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001730 drm_sysfs_connector_add(connector);
1731
1732 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001733 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001734 case DP_A:
1735 name = "DPDDC-A";
1736 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001737 case DP_B:
1738 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001739 dev_priv->hotplug_supported_mask |=
1740 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001741 name = "DPDDC-B";
1742 break;
1743 case DP_C:
1744 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001745 dev_priv->hotplug_supported_mask |=
1746 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001747 name = "DPDDC-C";
1748 break;
1749 case DP_D:
1750 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001751 dev_priv->hotplug_supported_mask |=
1752 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001753 name = "DPDDC-D";
1754 break;
1755 }
1756
Chris Wilsonea5b2132010-08-04 13:50:23 +01001757 intel_dp_i2c_init(intel_dp, intel_connector, name);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001758
Jesse Barnes89667382010-10-07 16:01:21 -07001759 /* Cache some DPCD data in the eDP case */
1760 if (is_edp(intel_dp)) {
1761 int ret;
1762 bool was_on;
1763
1764 was_on = ironlake_edp_panel_on(intel_dp);
1765 ret = intel_dp_aux_native_read(intel_dp, DP_DPCD_REV,
1766 intel_dp->dpcd,
1767 sizeof(intel_dp->dpcd));
1768 if (ret == sizeof(intel_dp->dpcd)) {
1769 if (intel_dp->dpcd[0] >= 0x11)
1770 dev_priv->no_aux_handshake = intel_dp->dpcd[3] &
1771 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
1772 } else {
1773 DRM_ERROR("failed to retrieve link info\n");
1774 }
1775 if (!was_on)
1776 ironlake_edp_panel_off(dev);
1777 }
1778
Eric Anholt21d40d32010-03-25 11:11:14 -07001779 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001780
Jesse Barnes4d926462010-10-07 16:01:07 -07001781 if (is_edp(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001782 /* initialize panel mode from VBT if available for eDP */
1783 if (dev_priv->lfp_lvds_vbt_mode) {
1784 dev_priv->panel_fixed_mode =
1785 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1786 if (dev_priv->panel_fixed_mode) {
1787 dev_priv->panel_fixed_mode->type |=
1788 DRM_MODE_TYPE_PREFERRED;
1789 }
1790 }
1791 }
1792
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001793 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1794 * 0xd. Failure to do so will result in spurious interrupts being
1795 * generated on the port when a cable is not attached.
1796 */
1797 if (IS_G4X(dev) && !IS_GM45(dev)) {
1798 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
1799 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
1800 }
1801}