blob: 6cdc53de24acce6b12fb9b4a20a8ed0176320028 [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 +010045#define IS_eDP(i) ((i)->base.type == INTEL_OUTPUT_EDP)
46#define IS_PCH_eDP(i) ((i)->is_pch_edp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +080047
Chris Wilsonea5b2132010-08-04 13:50:23 +010048struct intel_dp {
49 struct intel_encoder base;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070050 uint32_t output_reg;
51 uint32_t DP;
52 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070053 bool has_audio;
Keith Packardc8110e52009-05-06 11:51:10 -070054 int dpms_mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070055 uint8_t link_bw;
56 uint8_t lane_count;
57 uint8_t dpcd[4];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070058 struct i2c_adapter adapter;
59 struct i2c_algo_dp_aux_data algo;
Adam Jacksonf0917372010-07-16 14:46:27 -040060 bool is_pch_edp;
Jesse Barnes33a34e42010-09-08 12:42:02 -070061 uint8_t train_set[4];
62 uint8_t link_status[DP_LINK_STATUS_SIZE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070063};
64
Chris Wilsonea5b2132010-08-04 13:50:23 +010065static struct intel_dp *enc_to_intel_dp(struct drm_encoder *encoder)
66{
Chris Wilson4ef69c72010-09-09 15:14:28 +010067 return container_of(encoder, struct intel_dp, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010068}
Keith Packarda4fc5ed2009-04-07 16:16:42 -070069
Jesse Barnes33a34e42010-09-08 12:42:02 -070070static void intel_dp_start_link_train(struct intel_dp *intel_dp);
71static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
Chris Wilsonea5b2132010-08-04 13:50:23 +010072static void intel_dp_link_down(struct intel_dp *intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -070073
Zhenyu Wang32f9d652009-07-24 01:00:32 +080074void
Eric Anholt21d40d32010-03-25 11:11:14 -070075intel_edp_link_config (struct intel_encoder *intel_encoder,
Chris Wilsonea5b2132010-08-04 13:50:23 +010076 int *lane_num, int *link_bw)
Zhenyu Wang32f9d652009-07-24 01:00:32 +080077{
Chris Wilsonea5b2132010-08-04 13:50:23 +010078 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Zhenyu Wang32f9d652009-07-24 01:00:32 +080079
Chris Wilsonea5b2132010-08-04 13:50:23 +010080 *lane_num = intel_dp->lane_count;
81 if (intel_dp->link_bw == DP_LINK_BW_1_62)
Zhenyu Wang32f9d652009-07-24 01:00:32 +080082 *link_bw = 162000;
Chris Wilsonea5b2132010-08-04 13:50:23 +010083 else if (intel_dp->link_bw == DP_LINK_BW_2_7)
Zhenyu Wang32f9d652009-07-24 01:00:32 +080084 *link_bw = 270000;
85}
86
Keith Packarda4fc5ed2009-04-07 16:16:42 -070087static int
Chris Wilsonea5b2132010-08-04 13:50:23 +010088intel_dp_max_lane_count(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -070089{
Keith Packarda4fc5ed2009-04-07 16:16:42 -070090 int max_lane_count = 4;
91
Chris Wilsonea5b2132010-08-04 13:50:23 +010092 if (intel_dp->dpcd[0] >= 0x11) {
93 max_lane_count = intel_dp->dpcd[2] & 0x1f;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070094 switch (max_lane_count) {
95 case 1: case 2: case 4:
96 break;
97 default:
98 max_lane_count = 4;
99 }
100 }
101 return max_lane_count;
102}
103
104static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100105intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700106{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100107 int max_link_bw = intel_dp->dpcd[1];
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700108
109 switch (max_link_bw) {
110 case DP_LINK_BW_1_62:
111 case DP_LINK_BW_2_7:
112 break;
113 default:
114 max_link_bw = DP_LINK_BW_1_62;
115 break;
116 }
117 return max_link_bw;
118}
119
120static int
121intel_dp_link_clock(uint8_t link_bw)
122{
123 if (link_bw == DP_LINK_BW_2_7)
124 return 270000;
125 else
126 return 162000;
127}
128
129/* I think this is a fiction */
130static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100131intel_dp_link_required(struct drm_device *dev, struct intel_dp *intel_dp, int pixel_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700132{
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800133 struct drm_i915_private *dev_priv = dev->dev_private;
134
Chris Wilsonea5b2132010-08-04 13:50:23 +0100135 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800136 return (pixel_clock * dev_priv->edp_bpp) / 8;
137 else
138 return pixel_clock * 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700139}
140
141static int
Dave Airliefe27d532010-06-30 11:46:17 +1000142intel_dp_max_data_rate(int max_link_clock, int max_lanes)
143{
144 return (max_link_clock * max_lanes * 8) / 10;
145}
146
147static int
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700148intel_dp_mode_valid(struct drm_connector *connector,
149 struct drm_display_mode *mode)
150{
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800151 struct drm_encoder *encoder = intel_attached_encoder(connector);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100152 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhao Yakui7de56f42010-07-19 09:43:14 +0100153 struct drm_device *dev = connector->dev;
154 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100155 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
156 int max_lanes = intel_dp_max_lane_count(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700157
Chris Wilsonea5b2132010-08-04 13:50:23 +0100158 if ((IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) &&
Zhao Yakui7de56f42010-07-19 09:43:14 +0100159 dev_priv->panel_fixed_mode) {
160 if (mode->hdisplay > dev_priv->panel_fixed_mode->hdisplay)
161 return MODE_PANEL;
162
163 if (mode->vdisplay > dev_priv->panel_fixed_mode->vdisplay)
164 return MODE_PANEL;
165 }
166
Dave Airliefe27d532010-06-30 11:46:17 +1000167 /* only refuse the mode on non eDP since we have seen some wierd eDP panels
168 which are outside spec tolerances but somehow work by magic */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100169 if (!IS_eDP(intel_dp) &&
170 (intel_dp_link_required(connector->dev, intel_dp, mode->clock)
Dave Airliefe27d532010-06-30 11:46:17 +1000171 > intel_dp_max_data_rate(max_link_clock, max_lanes)))
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700172 return MODE_CLOCK_HIGH;
173
174 if (mode->clock < 10000)
175 return MODE_CLOCK_LOW;
176
177 return MODE_OK;
178}
179
180static uint32_t
181pack_aux(uint8_t *src, int src_bytes)
182{
183 int i;
184 uint32_t v = 0;
185
186 if (src_bytes > 4)
187 src_bytes = 4;
188 for (i = 0; i < src_bytes; i++)
189 v |= ((uint32_t) src[i]) << ((3-i) * 8);
190 return v;
191}
192
193static void
194unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
195{
196 int i;
197 if (dst_bytes > 4)
198 dst_bytes = 4;
199 for (i = 0; i < dst_bytes; i++)
200 dst[i] = src >> ((3-i) * 8);
201}
202
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700203/* hrawclock is 1/4 the FSB frequency */
204static int
205intel_hrawclk(struct drm_device *dev)
206{
207 struct drm_i915_private *dev_priv = dev->dev_private;
208 uint32_t clkcfg;
209
210 clkcfg = I915_READ(CLKCFG);
211 switch (clkcfg & CLKCFG_FSB_MASK) {
212 case CLKCFG_FSB_400:
213 return 100;
214 case CLKCFG_FSB_533:
215 return 133;
216 case CLKCFG_FSB_667:
217 return 166;
218 case CLKCFG_FSB_800:
219 return 200;
220 case CLKCFG_FSB_1067:
221 return 266;
222 case CLKCFG_FSB_1333:
223 return 333;
224 /* these two are just a guess; one of them might be right */
225 case CLKCFG_FSB_1600:
226 case CLKCFG_FSB_1600_ALT:
227 return 400;
228 default:
229 return 133;
230 }
231}
232
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700233static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100234intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700235 uint8_t *send, int send_bytes,
236 uint8_t *recv, int recv_size)
237{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100238 uint32_t output_reg = intel_dp->output_reg;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100239 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700240 struct drm_i915_private *dev_priv = dev->dev_private;
241 uint32_t ch_ctl = output_reg + 0x10;
242 uint32_t ch_data = ch_ctl + 4;
243 int i;
244 int recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700245 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700246 uint32_t aux_clock_divider;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800247 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700248
249 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700250 * and would like to run at 2MHz. So, take the
251 * hrawclk value and divide by 2 and use that
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700252 *
253 * Note that PCH attached eDP panels should use a 125MHz input
254 * clock divider.
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700255 */
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700256 if (IS_eDP(intel_dp) && !IS_PCH_eDP(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +0800257 if (IS_GEN6(dev))
258 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
259 else
260 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
261 } else if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500262 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800263 else
264 aux_clock_divider = intel_hrawclk(dev) / 2;
265
Zhenyu Wange3421a12010-04-08 09:43:27 +0800266 if (IS_GEN6(dev))
267 precharge = 3;
268 else
269 precharge = 5;
270
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100271 if (I915_READ(ch_ctl) & DP_AUX_CH_CTL_SEND_BUSY) {
272 DRM_ERROR("dp_aux_ch not started status 0x%08x\n",
273 I915_READ(ch_ctl));
274 return -EBUSY;
275 }
276
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700277 /* Must try at least 3 times according to DP spec */
278 for (try = 0; try < 5; try++) {
279 /* Load the send data into the aux channel data registers */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100280 for (i = 0; i < send_bytes; i += 4)
281 I915_WRITE(ch_data + i,
282 pack_aux(send + i, send_bytes - i));
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700283
284 /* Send the command and wait for it to complete */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100285 I915_WRITE(ch_ctl,
286 DP_AUX_CH_CTL_SEND_BUSY |
287 DP_AUX_CH_CTL_TIME_OUT_400us |
288 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
289 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
290 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
291 DP_AUX_CH_CTL_DONE |
292 DP_AUX_CH_CTL_TIME_OUT_ERROR |
293 DP_AUX_CH_CTL_RECEIVE_ERROR);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700294 for (;;) {
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700295 status = I915_READ(ch_ctl);
296 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
297 break;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100298 udelay(100);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700299 }
300
301 /* Clear done status and any errors */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100302 I915_WRITE(ch_ctl,
303 status |
304 DP_AUX_CH_CTL_DONE |
305 DP_AUX_CH_CTL_TIME_OUT_ERROR |
306 DP_AUX_CH_CTL_RECEIVE_ERROR);
307 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700308 break;
309 }
310
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700311 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700312 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700313 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700314 }
315
316 /* Check for timeout or receive error.
317 * Timeouts occur when the sink is not connected
318 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700319 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700320 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700321 return -EIO;
322 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700323
324 /* Timeouts occur when the device isn't connected, so they're
325 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700326 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800327 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700328 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700329 }
330
331 /* Unload any bytes sent back from the other side */
332 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
333 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700334 if (recv_bytes > recv_size)
335 recv_bytes = recv_size;
336
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100337 for (i = 0; i < recv_bytes; i += 4)
338 unpack_aux(I915_READ(ch_data + i),
339 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700340
341 return recv_bytes;
342}
343
344/* Write data to the aux channel in native mode */
345static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100346intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700347 uint16_t address, uint8_t *send, int send_bytes)
348{
349 int ret;
350 uint8_t msg[20];
351 int msg_bytes;
352 uint8_t ack;
353
354 if (send_bytes > 16)
355 return -1;
356 msg[0] = AUX_NATIVE_WRITE << 4;
357 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800358 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700359 msg[3] = send_bytes - 1;
360 memcpy(&msg[4], send, send_bytes);
361 msg_bytes = send_bytes + 4;
362 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100363 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700364 if (ret < 0)
365 return ret;
366 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
367 break;
368 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
369 udelay(100);
370 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700371 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700372 }
373 return send_bytes;
374}
375
376/* Write a single byte to the aux channel in native mode */
377static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100378intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700379 uint16_t address, uint8_t byte)
380{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100381 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700382}
383
384/* read bytes from a native aux channel */
385static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100386intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700387 uint16_t address, uint8_t *recv, int recv_bytes)
388{
389 uint8_t msg[4];
390 int msg_bytes;
391 uint8_t reply[20];
392 int reply_bytes;
393 uint8_t ack;
394 int ret;
395
396 msg[0] = AUX_NATIVE_READ << 4;
397 msg[1] = address >> 8;
398 msg[2] = address & 0xff;
399 msg[3] = recv_bytes - 1;
400
401 msg_bytes = 4;
402 reply_bytes = recv_bytes + 1;
403
404 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100405 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700406 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700407 if (ret == 0)
408 return -EPROTO;
409 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700410 return ret;
411 ack = reply[0];
412 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
413 memcpy(recv, reply + 1, ret - 1);
414 return ret - 1;
415 }
416 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
417 udelay(100);
418 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700419 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700420 }
421}
422
423static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000424intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
425 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700426{
Dave Airlieab2c0672009-12-04 10:55:24 +1000427 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100428 struct intel_dp *intel_dp = container_of(adapter,
429 struct intel_dp,
430 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000431 uint16_t address = algo_data->address;
432 uint8_t msg[5];
433 uint8_t reply[2];
434 int msg_bytes;
435 int reply_bytes;
436 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700437
Dave Airlieab2c0672009-12-04 10:55:24 +1000438 /* Set up the command byte */
439 if (mode & MODE_I2C_READ)
440 msg[0] = AUX_I2C_READ << 4;
441 else
442 msg[0] = AUX_I2C_WRITE << 4;
443
444 if (!(mode & MODE_I2C_STOP))
445 msg[0] |= AUX_I2C_MOT << 4;
446
447 msg[1] = address >> 8;
448 msg[2] = address;
449
450 switch (mode) {
451 case MODE_I2C_WRITE:
452 msg[3] = 0;
453 msg[4] = write_byte;
454 msg_bytes = 5;
455 reply_bytes = 1;
456 break;
457 case MODE_I2C_READ:
458 msg[3] = 0;
459 msg_bytes = 4;
460 reply_bytes = 2;
461 break;
462 default:
463 msg_bytes = 3;
464 reply_bytes = 1;
465 break;
466 }
467
468 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100469 ret = intel_dp_aux_ch(intel_dp,
Dave Airlieab2c0672009-12-04 10:55:24 +1000470 msg, msg_bytes,
471 reply, reply_bytes);
472 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000473 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000474 return ret;
475 }
476 switch (reply[0] & AUX_I2C_REPLY_MASK) {
477 case AUX_I2C_REPLY_ACK:
478 if (mode == MODE_I2C_READ) {
479 *read_byte = reply[1];
480 }
481 return reply_bytes - 1;
482 case AUX_I2C_REPLY_NACK:
Dave Airlie3ff99162009-12-08 14:03:47 +1000483 DRM_DEBUG_KMS("aux_ch nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000484 return -EREMOTEIO;
485 case AUX_I2C_REPLY_DEFER:
Dave Airlie3ff99162009-12-08 14:03:47 +1000486 DRM_DEBUG_KMS("aux_ch defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000487 udelay(100);
488 break;
489 default:
490 DRM_ERROR("aux_ch invalid reply 0x%02x\n", reply[0]);
491 return -EREMOTEIO;
492 }
493 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700494}
495
496static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100497intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800498 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700499{
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800500 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100501 intel_dp->algo.running = false;
502 intel_dp->algo.address = 0;
503 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700504
Chris Wilsonea5b2132010-08-04 13:50:23 +0100505 memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
506 intel_dp->adapter.owner = THIS_MODULE;
507 intel_dp->adapter.class = I2C_CLASS_DDC;
508 strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
509 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
510 intel_dp->adapter.algo_data = &intel_dp->algo;
511 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
512
513 return i2c_dp_aux_add_bus(&intel_dp->adapter);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700514}
515
516static bool
517intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
518 struct drm_display_mode *adjusted_mode)
519{
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100520 struct drm_device *dev = encoder->dev;
521 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100522 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700523 int lane_count, clock;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100524 int max_lane_count = intel_dp_max_lane_count(intel_dp);
525 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700526 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
527
Chris Wilsonea5b2132010-08-04 13:50:23 +0100528 if ((IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) &&
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100529 dev_priv->panel_fixed_mode) {
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100530 intel_fixed_panel_mode(dev_priv->panel_fixed_mode, adjusted_mode);
531 intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
532 mode, adjusted_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100533 /*
534 * the mode->clock is used to calculate the Data&Link M/N
535 * of the pipe. For the eDP the fixed clock should be used.
536 */
537 mode->clock = dev_priv->panel_fixed_mode->clock;
538 }
539
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700540 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
541 for (clock = 0; clock <= max_clock; clock++) {
Dave Airliefe27d532010-06-30 11:46:17 +1000542 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700543
Chris Wilsonea5b2132010-08-04 13:50:23 +0100544 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800545 <= link_avail) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100546 intel_dp->link_bw = bws[clock];
547 intel_dp->lane_count = lane_count;
548 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Zhao Yakui28c97732009-10-09 11:39:41 +0800549 DRM_DEBUG_KMS("Display port link bw %02x lane "
550 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100551 intel_dp->link_bw, intel_dp->lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700552 adjusted_mode->clock);
553 return true;
554 }
555 }
556 }
Dave Airliefe27d532010-06-30 11:46:17 +1000557
Chris Wilsonea5b2132010-08-04 13:50:23 +0100558 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) {
Dave Airliefe27d532010-06-30 11:46:17 +1000559 /* okay we failed just pick the highest */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100560 intel_dp->lane_count = max_lane_count;
561 intel_dp->link_bw = bws[max_clock];
562 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Dave Airliefe27d532010-06-30 11:46:17 +1000563 DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
564 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100565 intel_dp->link_bw, intel_dp->lane_count,
Dave Airliefe27d532010-06-30 11:46:17 +1000566 adjusted_mode->clock);
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100567
Dave Airliefe27d532010-06-30 11:46:17 +1000568 return true;
569 }
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100570
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700571 return false;
572}
573
574struct intel_dp_m_n {
575 uint32_t tu;
576 uint32_t gmch_m;
577 uint32_t gmch_n;
578 uint32_t link_m;
579 uint32_t link_n;
580};
581
582static void
583intel_reduce_ratio(uint32_t *num, uint32_t *den)
584{
585 while (*num > 0xffffff || *den > 0xffffff) {
586 *num >>= 1;
587 *den >>= 1;
588 }
589}
590
591static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800592intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700593 int nlanes,
594 int pixel_clock,
595 int link_clock,
596 struct intel_dp_m_n *m_n)
597{
598 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800599 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700600 m_n->gmch_n = link_clock * nlanes;
601 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
602 m_n->link_m = pixel_clock;
603 m_n->link_n = link_clock;
604 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
605}
606
Zhao Yakui36e83a12010-06-12 14:32:21 +0800607bool intel_pch_has_edp(struct drm_crtc *crtc)
608{
609 struct drm_device *dev = crtc->dev;
610 struct drm_mode_config *mode_config = &dev->mode_config;
611 struct drm_encoder *encoder;
612
613 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100614 struct intel_dp *intel_dp;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800615
Chris Wilsonea5b2132010-08-04 13:50:23 +0100616 if (encoder->crtc != crtc)
Zhao Yakui36e83a12010-06-12 14:32:21 +0800617 continue;
618
Chris Wilsonea5b2132010-08-04 13:50:23 +0100619 intel_dp = enc_to_intel_dp(encoder);
620 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
621 return intel_dp->is_pch_edp;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800622 }
623 return false;
624}
625
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700626void
627intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
628 struct drm_display_mode *adjusted_mode)
629{
630 struct drm_device *dev = crtc->dev;
631 struct drm_mode_config *mode_config = &dev->mode_config;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800632 struct drm_encoder *encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700633 struct drm_i915_private *dev_priv = dev->dev_private;
634 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Zhao Yakui36e83a12010-06-12 14:32:21 +0800635 int lane_count = 4, bpp = 24;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700636 struct intel_dp_m_n m_n;
637
638 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700639 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700640 */
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800641 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100642 struct intel_dp *intel_dp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700643
Dan Carpenterd8201ab2010-05-07 10:39:00 +0200644 if (encoder->crtc != crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700645 continue;
646
Chris Wilsonea5b2132010-08-04 13:50:23 +0100647 intel_dp = enc_to_intel_dp(encoder);
648 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
649 lane_count = intel_dp->lane_count;
650 if (IS_PCH_eDP(intel_dp))
Zhao Yakui36e83a12010-06-12 14:32:21 +0800651 bpp = dev_priv->edp_bpp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700652 break;
653 }
654 }
655
656 /*
657 * Compute the GMCH and Link ratios. The '3' here is
658 * the number of bytes_per_pixel post-LUT, which we always
659 * set up for 8-bits of R/G/B, or 3 bytes total.
660 */
Zhao Yakui36e83a12010-06-12 14:32:21 +0800661 intel_dp_compute_m_n(bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700662 mode->clock, adjusted_mode->clock, &m_n);
663
Eric Anholtc619eed2010-01-28 16:45:52 -0800664 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800665 if (intel_crtc->pipe == 0) {
666 I915_WRITE(TRANSA_DATA_M1,
667 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
668 m_n.gmch_m);
669 I915_WRITE(TRANSA_DATA_N1, m_n.gmch_n);
670 I915_WRITE(TRANSA_DP_LINK_M1, m_n.link_m);
671 I915_WRITE(TRANSA_DP_LINK_N1, m_n.link_n);
672 } else {
673 I915_WRITE(TRANSB_DATA_M1,
674 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
675 m_n.gmch_m);
676 I915_WRITE(TRANSB_DATA_N1, m_n.gmch_n);
677 I915_WRITE(TRANSB_DP_LINK_M1, m_n.link_m);
678 I915_WRITE(TRANSB_DP_LINK_N1, m_n.link_n);
679 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700680 } else {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800681 if (intel_crtc->pipe == 0) {
682 I915_WRITE(PIPEA_GMCH_DATA_M,
683 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
684 m_n.gmch_m);
685 I915_WRITE(PIPEA_GMCH_DATA_N,
686 m_n.gmch_n);
687 I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
688 I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
689 } else {
690 I915_WRITE(PIPEB_GMCH_DATA_M,
691 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
692 m_n.gmch_m);
693 I915_WRITE(PIPEB_GMCH_DATA_N,
694 m_n.gmch_n);
695 I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
696 I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
697 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700698 }
699}
700
701static void
702intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
703 struct drm_display_mode *adjusted_mode)
704{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800705 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100706 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +0100707 struct drm_crtc *crtc = intel_dp->base.base.crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
709
Chris Wilsonea5b2132010-08-04 13:50:23 +0100710 intel_dp->DP = (DP_VOLTAGE_0_4 |
Adam Jackson9c9e7922010-04-05 17:57:59 -0400711 DP_PRE_EMPHASIS_0);
712
713 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100714 intel_dp->DP |= DP_SYNC_HS_HIGH;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400715 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100716 intel_dp->DP |= DP_SYNC_VS_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700717
Chris Wilsonea5b2132010-08-04 13:50:23 +0100718 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp))
719 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800720 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100721 intel_dp->DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700722
Chris Wilsonea5b2132010-08-04 13:50:23 +0100723 switch (intel_dp->lane_count) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700724 case 1:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100725 intel_dp->DP |= DP_PORT_WIDTH_1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700726 break;
727 case 2:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100728 intel_dp->DP |= DP_PORT_WIDTH_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700729 break;
730 case 4:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100731 intel_dp->DP |= DP_PORT_WIDTH_4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700732 break;
733 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100734 if (intel_dp->has_audio)
735 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700736
Chris Wilsonea5b2132010-08-04 13:50:23 +0100737 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
738 intel_dp->link_configuration[0] = intel_dp->link_bw;
739 intel_dp->link_configuration[1] = intel_dp->lane_count;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700740
741 /*
Adam Jackson9962c922010-05-13 14:45:42 -0400742 * Check for DPCD version > 1.1 and enhanced framing support
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700743 */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100744 if (intel_dp->dpcd[0] >= 0x11 && (intel_dp->dpcd[2] & DP_ENHANCED_FRAME_CAP)) {
745 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
746 intel_dp->DP |= DP_ENHANCED_FRAMING;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700747 }
748
Zhenyu Wange3421a12010-04-08 09:43:27 +0800749 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
750 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100751 intel_dp->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800752
Chris Wilsonea5b2132010-08-04 13:50:23 +0100753 if (IS_eDP(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800754 /* don't miss out required setting for eDP */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100755 intel_dp->DP |= DP_PLL_ENABLE;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800756 if (adjusted_mode->clock < 200000)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100757 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800758 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100759 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800760 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700761}
762
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700763/* Returns true if the panel was already on when called */
764static bool ironlake_edp_panel_on (struct drm_device *dev)
Jesse Barnes9934c132010-07-22 13:18:19 -0700765{
766 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson913d8d12010-08-07 11:01:35 +0100767 u32 pp;
Jesse Barnes9934c132010-07-22 13:18:19 -0700768
Chris Wilson913d8d12010-08-07 11:01:35 +0100769 if (I915_READ(PCH_PP_STATUS) & PP_ON)
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700770 return true;
Jesse Barnes9934c132010-07-22 13:18:19 -0700771
772 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700773
774 /* ILK workaround: disable reset around power sequence */
775 pp &= ~PANEL_POWER_RESET;
776 I915_WRITE(PCH_PP_CONTROL, pp);
777 POSTING_READ(PCH_PP_CONTROL);
778
Jesse Barnes9934c132010-07-22 13:18:19 -0700779 pp |= PANEL_UNLOCK_REGS | POWER_TARGET_ON;
780 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes9934c132010-07-22 13:18:19 -0700781
Chris Wilson481b6af2010-08-23 17:43:35 +0100782 if (wait_for(I915_READ(PCH_PP_STATUS) & PP_ON, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100783 DRM_ERROR("panel on wait timed out: 0x%08x\n",
784 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700785
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700786 pp &= ~(PANEL_UNLOCK_REGS);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700787 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700788 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700789 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700790
791 return false;
Jesse Barnes9934c132010-07-22 13:18:19 -0700792}
793
794static void ironlake_edp_panel_off (struct drm_device *dev)
795{
796 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson913d8d12010-08-07 11:01:35 +0100797 u32 pp;
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 Barnes9934c132010-07-22 13:18:19 -0700806 pp &= ~POWER_TARGET_ON;
807 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes9934c132010-07-22 13:18:19 -0700808
Chris Wilson481b6af2010-08-23 17:43:35 +0100809 if (wait_for((I915_READ(PCH_PP_STATUS) & PP_ON) == 0, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100810 DRM_ERROR("panel off wait timed out: 0x%08x\n",
811 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700812
813 /* Make sure VDD is enabled so DP AUX will work */
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700814 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700815 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700816 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -0700817}
818
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700819static void ironlake_edp_panel_vdd_on(struct drm_device *dev)
820{
821 struct drm_i915_private *dev_priv = dev->dev_private;
822 u32 pp;
823
824 pp = I915_READ(PCH_PP_CONTROL);
825 pp |= EDP_FORCE_VDD;
826 I915_WRITE(PCH_PP_CONTROL, pp);
827 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes3ba5c562010-08-25 13:09:48 -0700828 msleep(300);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700829}
830
831static void ironlake_edp_panel_vdd_off(struct drm_device *dev)
832{
833 struct drm_i915_private *dev_priv = dev->dev_private;
834 u32 pp;
835
836 pp = I915_READ(PCH_PP_CONTROL);
837 pp &= ~EDP_FORCE_VDD;
838 I915_WRITE(PCH_PP_CONTROL, pp);
839 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes3ba5c562010-08-25 13:09:48 -0700840 msleep(300);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700841}
842
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500843static void ironlake_edp_backlight_on (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800844{
845 struct drm_i915_private *dev_priv = dev->dev_private;
846 u32 pp;
847
Zhao Yakui28c97732009-10-09 11:39:41 +0800848 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800849 pp = I915_READ(PCH_PP_CONTROL);
850 pp |= EDP_BLC_ENABLE;
851 I915_WRITE(PCH_PP_CONTROL, pp);
852}
853
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500854static void ironlake_edp_backlight_off (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800855{
856 struct drm_i915_private *dev_priv = dev->dev_private;
857 u32 pp;
858
Zhao Yakui28c97732009-10-09 11:39:41 +0800859 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800860 pp = I915_READ(PCH_PP_CONTROL);
861 pp &= ~EDP_BLC_ENABLE;
862 I915_WRITE(PCH_PP_CONTROL, pp);
863}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700864
Jesse Barnesd240f202010-08-13 15:43:26 -0700865static void ironlake_edp_pll_on(struct drm_encoder *encoder)
866{
867 struct drm_device *dev = encoder->dev;
868 struct drm_i915_private *dev_priv = dev->dev_private;
869 u32 dpa_ctl;
870
871 DRM_DEBUG_KMS("\n");
872 dpa_ctl = I915_READ(DP_A);
873 dpa_ctl &= ~DP_PLL_ENABLE;
874 I915_WRITE(DP_A, dpa_ctl);
875}
876
877static void ironlake_edp_pll_off(struct drm_encoder *encoder)
878{
879 struct drm_device *dev = encoder->dev;
880 struct drm_i915_private *dev_priv = dev->dev_private;
881 u32 dpa_ctl;
882
883 dpa_ctl = I915_READ(DP_A);
884 dpa_ctl |= DP_PLL_ENABLE;
885 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +0100886 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -0700887 udelay(200);
888}
889
890static void intel_dp_prepare(struct drm_encoder *encoder)
891{
892 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
893 struct drm_device *dev = encoder->dev;
894 struct drm_i915_private *dev_priv = dev->dev_private;
895 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
896
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700897 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) {
Jesse Barnes2c9d9752010-09-08 12:42:05 -0700898 ironlake_edp_panel_off(dev);
Jesse Barnesd240f202010-08-13 15:43:26 -0700899 ironlake_edp_backlight_off(dev);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700900 ironlake_edp_panel_vdd_on(dev);
Jesse Barnesd240f202010-08-13 15:43:26 -0700901 ironlake_edp_pll_on(encoder);
902 }
903 if (dp_reg & DP_PORT_EN)
904 intel_dp_link_down(intel_dp);
905}
906
907static void intel_dp_commit(struct drm_encoder *encoder)
908{
909 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
910 struct drm_device *dev = encoder->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -0700911
Jesse Barnes33a34e42010-09-08 12:42:02 -0700912 intel_dp_start_link_train(intel_dp);
913
914 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700915 ironlake_edp_panel_on(dev);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700916
917 intel_dp_complete_link_train(intel_dp);
918
919 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -0700920 ironlake_edp_backlight_on(dev);
921}
922
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700923static void
924intel_dp_dpms(struct drm_encoder *encoder, int mode)
925{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100926 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800927 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700928 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100929 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700930
931 if (mode != DRM_MODE_DPMS_ON) {
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700932 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) {
933 ironlake_edp_backlight_off(dev);
934 ironlake_edp_panel_off(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800935 }
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700936 if (dp_reg & DP_PORT_EN)
937 intel_dp_link_down(intel_dp);
Jesse Barnesd240f202010-08-13 15:43:26 -0700938 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
939 ironlake_edp_pll_off(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700940 } else {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800941 if (!(dp_reg & DP_PORT_EN)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -0700942 intel_dp_start_link_train(intel_dp);
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700943 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Jesse Barnes9934c132010-07-22 13:18:19 -0700944 ironlake_edp_panel_on(dev);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700945 intel_dp_complete_link_train(intel_dp);
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700946 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500947 ironlake_edp_backlight_on(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800948 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700949 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100950 intel_dp->dpms_mode = mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700951}
952
953/*
954 * Fetch AUX CH registers 0x202 - 0x207 which contain
955 * link status information
956 */
957static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -0700958intel_dp_get_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700959{
960 int ret;
961
Chris Wilsonea5b2132010-08-04 13:50:23 +0100962 ret = intel_dp_aux_native_read(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700963 DP_LANE0_1_STATUS,
Jesse Barnes33a34e42010-09-08 12:42:02 -0700964 intel_dp->link_status, DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700965 if (ret != DP_LINK_STATUS_SIZE)
966 return false;
967 return true;
968}
969
970static uint8_t
971intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
972 int r)
973{
974 return link_status[r - DP_LANE0_1_STATUS];
975}
976
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700977static uint8_t
978intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
979 int lane)
980{
981 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
982 int s = ((lane & 1) ?
983 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
984 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
985 uint8_t l = intel_dp_link_status(link_status, i);
986
987 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
988}
989
990static uint8_t
991intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
992 int lane)
993{
994 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
995 int s = ((lane & 1) ?
996 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
997 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
998 uint8_t l = intel_dp_link_status(link_status, i);
999
1000 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1001}
1002
1003
1004#if 0
1005static char *voltage_names[] = {
1006 "0.4V", "0.6V", "0.8V", "1.2V"
1007};
1008static char *pre_emph_names[] = {
1009 "0dB", "3.5dB", "6dB", "9.5dB"
1010};
1011static char *link_train_names[] = {
1012 "pattern 1", "pattern 2", "idle", "off"
1013};
1014#endif
1015
1016/*
1017 * These are source-specific values; current Intel hardware supports
1018 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1019 */
1020#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1021
1022static uint8_t
1023intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1024{
1025 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1026 case DP_TRAIN_VOLTAGE_SWING_400:
1027 return DP_TRAIN_PRE_EMPHASIS_6;
1028 case DP_TRAIN_VOLTAGE_SWING_600:
1029 return DP_TRAIN_PRE_EMPHASIS_6;
1030 case DP_TRAIN_VOLTAGE_SWING_800:
1031 return DP_TRAIN_PRE_EMPHASIS_3_5;
1032 case DP_TRAIN_VOLTAGE_SWING_1200:
1033 default:
1034 return DP_TRAIN_PRE_EMPHASIS_0;
1035 }
1036}
1037
1038static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001039intel_get_adjust_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001040{
1041 uint8_t v = 0;
1042 uint8_t p = 0;
1043 int lane;
1044
Jesse Barnes33a34e42010-09-08 12:42:02 -07001045 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1046 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1047 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001048
1049 if (this_v > v)
1050 v = this_v;
1051 if (this_p > p)
1052 p = this_p;
1053 }
1054
1055 if (v >= I830_DP_VOLTAGE_MAX)
1056 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1057
1058 if (p >= intel_dp_pre_emphasis_max(v))
1059 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1060
1061 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001062 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001063}
1064
1065static uint32_t
1066intel_dp_signal_levels(uint8_t train_set, int lane_count)
1067{
1068 uint32_t signal_levels = 0;
1069
1070 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
1071 case DP_TRAIN_VOLTAGE_SWING_400:
1072 default:
1073 signal_levels |= DP_VOLTAGE_0_4;
1074 break;
1075 case DP_TRAIN_VOLTAGE_SWING_600:
1076 signal_levels |= DP_VOLTAGE_0_6;
1077 break;
1078 case DP_TRAIN_VOLTAGE_SWING_800:
1079 signal_levels |= DP_VOLTAGE_0_8;
1080 break;
1081 case DP_TRAIN_VOLTAGE_SWING_1200:
1082 signal_levels |= DP_VOLTAGE_1_2;
1083 break;
1084 }
1085 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
1086 case DP_TRAIN_PRE_EMPHASIS_0:
1087 default:
1088 signal_levels |= DP_PRE_EMPHASIS_0;
1089 break;
1090 case DP_TRAIN_PRE_EMPHASIS_3_5:
1091 signal_levels |= DP_PRE_EMPHASIS_3_5;
1092 break;
1093 case DP_TRAIN_PRE_EMPHASIS_6:
1094 signal_levels |= DP_PRE_EMPHASIS_6;
1095 break;
1096 case DP_TRAIN_PRE_EMPHASIS_9_5:
1097 signal_levels |= DP_PRE_EMPHASIS_9_5;
1098 break;
1099 }
1100 return signal_levels;
1101}
1102
Zhenyu Wange3421a12010-04-08 09:43:27 +08001103/* Gen6's DP voltage swing and pre-emphasis control */
1104static uint32_t
1105intel_gen6_edp_signal_levels(uint8_t train_set)
1106{
1107 switch (train_set & (DP_TRAIN_VOLTAGE_SWING_MASK|DP_TRAIN_PRE_EMPHASIS_MASK)) {
1108 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1109 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1110 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1111 return EDP_LINK_TRAIN_400MV_6DB_SNB_B;
1112 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1113 return EDP_LINK_TRAIN_600MV_3_5DB_SNB_B;
1114 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1115 return EDP_LINK_TRAIN_800MV_0DB_SNB_B;
1116 default:
1117 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level\n");
1118 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1119 }
1120}
1121
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001122static uint8_t
1123intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1124 int lane)
1125{
1126 int i = DP_LANE0_1_STATUS + (lane >> 1);
1127 int s = (lane & 1) * 4;
1128 uint8_t l = intel_dp_link_status(link_status, i);
1129
1130 return (l >> s) & 0xf;
1131}
1132
1133/* Check for clock recovery is done on all channels */
1134static bool
1135intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1136{
1137 int lane;
1138 uint8_t lane_status;
1139
1140 for (lane = 0; lane < lane_count; lane++) {
1141 lane_status = intel_get_lane_status(link_status, lane);
1142 if ((lane_status & DP_LANE_CR_DONE) == 0)
1143 return false;
1144 }
1145 return true;
1146}
1147
1148/* Check to see if channel eq is done on all channels */
1149#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1150 DP_LANE_CHANNEL_EQ_DONE|\
1151 DP_LANE_SYMBOL_LOCKED)
1152static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001153intel_channel_eq_ok(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001154{
1155 uint8_t lane_align;
1156 uint8_t lane_status;
1157 int lane;
1158
Jesse Barnes33a34e42010-09-08 12:42:02 -07001159 lane_align = intel_dp_link_status(intel_dp->link_status,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001160 DP_LANE_ALIGN_STATUS_UPDATED);
1161 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1162 return false;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001163 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1164 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001165 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1166 return false;
1167 }
1168 return true;
1169}
1170
1171static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001172intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001173 uint32_t dp_reg_value,
1174 uint8_t dp_train_pat,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001175 bool first)
1176{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001177 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001178 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01001179 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001180 int ret;
1181
Chris Wilsonea5b2132010-08-04 13:50:23 +01001182 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1183 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001184 if (first)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001185 intel_wait_for_vblank(dev, intel_crtc->pipe);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001186
Chris Wilsonea5b2132010-08-04 13:50:23 +01001187 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001188 DP_TRAINING_PATTERN_SET,
1189 dp_train_pat);
1190
Chris Wilsonea5b2132010-08-04 13:50:23 +01001191 ret = intel_dp_aux_native_write(intel_dp,
Jesse Barnes33a34e42010-09-08 12:42:02 -07001192 DP_TRAINING_LANE0_SET, intel_dp->train_set, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001193 if (ret != 4)
1194 return false;
1195
1196 return true;
1197}
1198
Jesse Barnes33a34e42010-09-08 12:42:02 -07001199/* Enable corresponding port and start training pattern 1 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001200static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001201intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001202{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001203 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001204 int i;
1205 uint8_t voltage;
1206 bool clock_recovery = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001207 bool first = true;
1208 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001209 u32 reg;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001210 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001211
1212 /* Write the link configuration data */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001213 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1214 intel_dp->link_configuration,
1215 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001216
1217 DP |= DP_PORT_EN;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001218 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001219 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1220 else
1221 DP &= ~DP_LINK_TRAIN_MASK;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001222 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001223 voltage = 0xff;
1224 tries = 0;
1225 clock_recovery = false;
1226 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001227 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001228 uint32_t signal_levels;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001229 if (IS_GEN6(dev) && IS_eDP(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001230 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001231 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1232 } else {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001233 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001234 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1235 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001236
Chris Wilsonea5b2132010-08-04 13:50:23 +01001237 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001238 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1239 else
1240 reg = DP | DP_LINK_TRAIN_PAT_1;
1241
Chris Wilsonea5b2132010-08-04 13:50:23 +01001242 if (!intel_dp_set_link_train(intel_dp, reg,
Jesse Barnes33a34e42010-09-08 12:42:02 -07001243 DP_TRAINING_PATTERN_1, first))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001244 break;
1245 first = false;
1246 /* Set training pattern 1 */
1247
1248 udelay(100);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001249 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001250 break;
1251
Jesse Barnes33a34e42010-09-08 12:42:02 -07001252 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001253 clock_recovery = true;
1254 break;
1255 }
1256
1257 /* Check to see if we've tried the max voltage */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001258 for (i = 0; i < intel_dp->lane_count; i++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001259 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001260 break;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001261 if (i == intel_dp->lane_count)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001262 break;
1263
1264 /* Check to see if we've tried the same voltage 5 times */
Jesse Barnes33a34e42010-09-08 12:42:02 -07001265 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001266 ++tries;
1267 if (tries == 5)
1268 break;
1269 } else
1270 tries = 0;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001271 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001272
Jesse Barnes33a34e42010-09-08 12:42:02 -07001273 /* Compute new intel_dp->train_set as requested by target */
1274 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001275 }
1276
Jesse Barnes33a34e42010-09-08 12:42:02 -07001277 intel_dp->DP = DP;
1278}
1279
1280static void
1281intel_dp_complete_link_train(struct intel_dp *intel_dp)
1282{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001283 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001284 struct drm_i915_private *dev_priv = dev->dev_private;
1285 bool channel_eq = false;
1286 int tries;
1287 u32 reg;
1288 uint32_t DP = intel_dp->DP;
1289
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001290 /* channel equalization */
1291 tries = 0;
1292 channel_eq = false;
1293 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001294 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001295 uint32_t signal_levels;
1296
Chris Wilsonea5b2132010-08-04 13:50:23 +01001297 if (IS_GEN6(dev) && IS_eDP(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001298 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001299 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1300 } else {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001301 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001302 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1303 }
1304
Chris Wilsonea5b2132010-08-04 13:50:23 +01001305 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001306 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1307 else
1308 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001309
1310 /* channel eq pattern */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001311 if (!intel_dp_set_link_train(intel_dp, reg,
Jesse Barnes33a34e42010-09-08 12:42:02 -07001312 DP_TRAINING_PATTERN_2,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001313 false))
1314 break;
1315
1316 udelay(400);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001317 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001318 break;
1319
Jesse Barnes33a34e42010-09-08 12:42:02 -07001320 if (intel_channel_eq_ok(intel_dp)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001321 channel_eq = true;
1322 break;
1323 }
1324
1325 /* Try 5 times */
1326 if (tries > 5)
1327 break;
1328
Jesse Barnes33a34e42010-09-08 12:42:02 -07001329 /* Compute new intel_dp->train_set as requested by target */
1330 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001331 ++tries;
1332 }
1333
Chris Wilsonea5b2132010-08-04 13:50:23 +01001334 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001335 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1336 else
1337 reg = DP | DP_LINK_TRAIN_OFF;
1338
Chris Wilsonea5b2132010-08-04 13:50:23 +01001339 I915_WRITE(intel_dp->output_reg, reg);
1340 POSTING_READ(intel_dp->output_reg);
1341 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001342 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1343}
1344
1345static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001346intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001347{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001348 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001349 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001350 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001351
Zhao Yakui28c97732009-10-09 11:39:41 +08001352 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001353
Chris Wilsonea5b2132010-08-04 13:50:23 +01001354 if (IS_eDP(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001355 DP &= ~DP_PLL_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001356 I915_WRITE(intel_dp->output_reg, DP);
1357 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001358 udelay(100);
1359 }
1360
Chris Wilsonea5b2132010-08-04 13:50:23 +01001361 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001362 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001363 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
1364 POSTING_READ(intel_dp->output_reg);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001365 } else {
1366 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001367 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
1368 POSTING_READ(intel_dp->output_reg);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001369 }
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001370
1371 udelay(17000);
1372
Chris Wilsonea5b2132010-08-04 13:50:23 +01001373 if (IS_eDP(intel_dp))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001374 DP |= DP_LINK_TRAIN_OFF;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001375 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1376 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001377}
1378
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001379/*
1380 * According to DP spec
1381 * 5.1.2:
1382 * 1. Read DPCD
1383 * 2. Configure link according to Receiver Capabilities
1384 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1385 * 4. Check link status on receipt of hot-plug interrupt
1386 */
1387
1388static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001389intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001390{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001391 if (!intel_dp->base.base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001392 return;
1393
Jesse Barnes33a34e42010-09-08 12:42:02 -07001394 if (!intel_dp_get_link_status(intel_dp)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001395 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001396 return;
1397 }
1398
Jesse Barnes33a34e42010-09-08 12:42:02 -07001399 if (!intel_channel_eq_ok(intel_dp)) {
1400 intel_dp_start_link_train(intel_dp);
1401 intel_dp_complete_link_train(intel_dp);
1402 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001403}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001404
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001405static enum drm_connector_status
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001406ironlake_dp_detect(struct drm_connector *connector)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001407{
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001408 struct drm_encoder *encoder = intel_attached_encoder(connector);
Chris Wilsonea5b2132010-08-04 13:50:23 +01001409 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001410 enum drm_connector_status status;
1411
Jesse Barnes7eaf5542010-09-08 12:41:59 -07001412 /* Panel needs power for AUX to work */
1413 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Jesse Barnesb2094bb2010-09-08 12:42:01 -07001414 ironlake_edp_panel_vdd_on(connector->dev);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001415 status = connector_status_disconnected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001416 if (intel_dp_aux_native_read(intel_dp,
1417 0x000, intel_dp->dpcd,
1418 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001419 {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001420 if (intel_dp->dpcd[0] != 0)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001421 status = connector_status_connected;
1422 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001423 DRM_DEBUG_KMS("DPCD: %hx%hx%hx%hx\n", intel_dp->dpcd[0],
1424 intel_dp->dpcd[1], intel_dp->dpcd[2], intel_dp->dpcd[3]);
Jesse Barnesb2094bb2010-09-08 12:42:01 -07001425 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
1426 ironlake_edp_panel_vdd_off(connector->dev);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001427 return status;
1428}
1429
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001430/**
1431 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1432 *
1433 * \return true if DP port is connected.
1434 * \return false if DP port is disconnected.
1435 */
1436static enum drm_connector_status
1437intel_dp_detect(struct drm_connector *connector)
1438{
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001439 struct drm_encoder *encoder = intel_attached_encoder(connector);
Chris Wilsonea5b2132010-08-04 13:50:23 +01001440 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001441 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001442 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001443 uint32_t temp, bit;
1444 enum drm_connector_status status;
1445
Chris Wilsonea5b2132010-08-04 13:50:23 +01001446 intel_dp->has_audio = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001447
Eric Anholtc619eed2010-01-28 16:45:52 -08001448 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001449 return ironlake_dp_detect(connector);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001450
Chris Wilsonea5b2132010-08-04 13:50:23 +01001451 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001452 case DP_B:
1453 bit = DPB_HOTPLUG_INT_STATUS;
1454 break;
1455 case DP_C:
1456 bit = DPC_HOTPLUG_INT_STATUS;
1457 break;
1458 case DP_D:
1459 bit = DPD_HOTPLUG_INT_STATUS;
1460 break;
1461 default:
1462 return connector_status_unknown;
1463 }
1464
1465 temp = I915_READ(PORT_HOTPLUG_STAT);
1466
1467 if ((temp & bit) == 0)
1468 return connector_status_disconnected;
1469
1470 status = connector_status_disconnected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001471 if (intel_dp_aux_native_read(intel_dp,
1472 0x000, intel_dp->dpcd,
1473 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001474 {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001475 if (intel_dp->dpcd[0] != 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001476 status = connector_status_connected;
1477 }
1478 return status;
1479}
1480
1481static int intel_dp_get_modes(struct drm_connector *connector)
1482{
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001483 struct drm_encoder *encoder = intel_attached_encoder(connector);
Chris Wilsonea5b2132010-08-04 13:50:23 +01001484 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001485 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001486 struct drm_i915_private *dev_priv = dev->dev_private;
1487 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001488
1489 /* We should parse the EDID data and find out if it has an audio sink
1490 */
1491
Chris Wilsonea5b2132010-08-04 13:50:23 +01001492 ret = intel_ddc_get_modes(connector, intel_dp->base.ddc_bus);
Zhao Yakuib9efc482010-07-19 09:43:11 +01001493 if (ret) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001494 if ((IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) &&
Zhao Yakuib9efc482010-07-19 09:43:11 +01001495 !dev_priv->panel_fixed_mode) {
1496 struct drm_display_mode *newmode;
1497 list_for_each_entry(newmode, &connector->probed_modes,
1498 head) {
1499 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1500 dev_priv->panel_fixed_mode =
1501 drm_mode_duplicate(dev, newmode);
1502 break;
1503 }
1504 }
1505 }
1506
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001507 return ret;
Zhao Yakuib9efc482010-07-19 09:43:11 +01001508 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001509
1510 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001511 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001512 if (dev_priv->panel_fixed_mode != NULL) {
1513 struct drm_display_mode *mode;
1514 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1515 drm_mode_probed_add(connector, mode);
1516 return 1;
1517 }
1518 }
1519 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001520}
1521
1522static void
1523intel_dp_destroy (struct drm_connector *connector)
1524{
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001525 drm_sysfs_connector_remove(connector);
1526 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001527 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001528}
1529
Daniel Vetter24d05922010-08-20 18:08:28 +02001530static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1531{
1532 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1533
1534 i2c_del_adapter(&intel_dp->adapter);
1535 drm_encoder_cleanup(encoder);
1536 kfree(intel_dp);
1537}
1538
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001539static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1540 .dpms = intel_dp_dpms,
1541 .mode_fixup = intel_dp_mode_fixup,
Jesse Barnesd240f202010-08-13 15:43:26 -07001542 .prepare = intel_dp_prepare,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001543 .mode_set = intel_dp_mode_set,
Jesse Barnesd240f202010-08-13 15:43:26 -07001544 .commit = intel_dp_commit,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001545};
1546
1547static const struct drm_connector_funcs intel_dp_connector_funcs = {
1548 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001549 .detect = intel_dp_detect,
1550 .fill_modes = drm_helper_probe_single_connector_modes,
1551 .destroy = intel_dp_destroy,
1552};
1553
1554static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1555 .get_modes = intel_dp_get_modes,
1556 .mode_valid = intel_dp_mode_valid,
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001557 .best_encoder = intel_attached_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001558};
1559
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001560static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02001561 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001562};
1563
Chris Wilson995b6762010-08-20 13:23:26 +01001564static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001565intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07001566{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001567 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Keith Packardc8110e52009-05-06 11:51:10 -07001568
Chris Wilsonea5b2132010-08-04 13:50:23 +01001569 if (intel_dp->dpms_mode == DRM_MODE_DPMS_ON)
1570 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07001571}
1572
Zhenyu Wange3421a12010-04-08 09:43:27 +08001573/* Return which DP Port should be selected for Transcoder DP control */
1574int
1575intel_trans_dp_port_sel (struct drm_crtc *crtc)
1576{
1577 struct drm_device *dev = crtc->dev;
1578 struct drm_mode_config *mode_config = &dev->mode_config;
1579 struct drm_encoder *encoder;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001580
1581 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001582 struct intel_dp *intel_dp;
1583
Dan Carpenterd8201ab2010-05-07 10:39:00 +02001584 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08001585 continue;
1586
Chris Wilsonea5b2132010-08-04 13:50:23 +01001587 intel_dp = enc_to_intel_dp(encoder);
1588 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
1589 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001590 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001591
Zhenyu Wange3421a12010-04-08 09:43:27 +08001592 return -1;
1593}
1594
Zhao Yakui36e83a12010-06-12 14:32:21 +08001595/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04001596bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08001597{
1598 struct drm_i915_private *dev_priv = dev->dev_private;
1599 struct child_device_config *p_child;
1600 int i;
1601
1602 if (!dev_priv->child_dev_num)
1603 return false;
1604
1605 for (i = 0; i < dev_priv->child_dev_num; i++) {
1606 p_child = dev_priv->child_dev + i;
1607
1608 if (p_child->dvo_port == PORT_IDPD &&
1609 p_child->device_type == DEVICE_TYPE_eDP)
1610 return true;
1611 }
1612 return false;
1613}
1614
Keith Packardc8110e52009-05-06 11:51:10 -07001615void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001616intel_dp_init(struct drm_device *dev, int output_reg)
1617{
1618 struct drm_i915_private *dev_priv = dev->dev_private;
1619 struct drm_connector *connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001620 struct intel_dp *intel_dp;
Eric Anholt21d40d32010-03-25 11:11:14 -07001621 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001622 struct intel_connector *intel_connector;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001623 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04001624 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001625
Chris Wilsonea5b2132010-08-04 13:50:23 +01001626 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
1627 if (!intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001628 return;
1629
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001630 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1631 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001632 kfree(intel_dp);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001633 return;
1634 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001635 intel_encoder = &intel_dp->base;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001636
Chris Wilsonea5b2132010-08-04 13:50:23 +01001637 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04001638 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01001639 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04001640
Chris Wilsonea5b2132010-08-04 13:50:23 +01001641 if (output_reg == DP_A || IS_PCH_eDP(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04001642 type = DRM_MODE_CONNECTOR_eDP;
1643 intel_encoder->type = INTEL_OUTPUT_EDP;
1644 } else {
1645 type = DRM_MODE_CONNECTOR_DisplayPort;
1646 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1647 }
1648
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001649 connector = &intel_connector->base;
Adam Jacksonb3295302010-07-16 14:46:28 -04001650 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001651 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1652
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001653 connector->polled = DRM_CONNECTOR_POLL_HPD;
1654
Zhao Yakui652af9d2009-12-02 10:03:33 +08001655 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07001656 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001657 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07001658 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001659 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07001660 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08001661
Chris Wilsonea5b2132010-08-04 13:50:23 +01001662 if (IS_eDP(intel_dp))
Eric Anholt21d40d32010-03-25 11:11:14 -07001663 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08001664
Eric Anholt21d40d32010-03-25 11:11:14 -07001665 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001666 connector->interlace_allowed = true;
1667 connector->doublescan_allowed = 0;
1668
Chris Wilsonea5b2132010-08-04 13:50:23 +01001669 intel_dp->output_reg = output_reg;
1670 intel_dp->has_audio = false;
1671 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001672
Chris Wilson4ef69c72010-09-09 15:14:28 +01001673 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001674 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001675 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001676
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001677 drm_mode_connector_attach_encoder(&intel_connector->base,
Chris Wilson4ef69c72010-09-09 15:14:28 +01001678 &intel_encoder->base);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001679 drm_sysfs_connector_add(connector);
1680
1681 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001682 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001683 case DP_A:
1684 name = "DPDDC-A";
1685 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001686 case DP_B:
1687 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001688 dev_priv->hotplug_supported_mask |=
1689 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001690 name = "DPDDC-B";
1691 break;
1692 case DP_C:
1693 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001694 dev_priv->hotplug_supported_mask |=
1695 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001696 name = "DPDDC-C";
1697 break;
1698 case DP_D:
1699 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001700 dev_priv->hotplug_supported_mask |=
1701 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001702 name = "DPDDC-D";
1703 break;
1704 }
1705
Chris Wilsonea5b2132010-08-04 13:50:23 +01001706 intel_dp_i2c_init(intel_dp, intel_connector, name);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001707
Chris Wilsonea5b2132010-08-04 13:50:23 +01001708 intel_encoder->ddc_bus = &intel_dp->adapter;
Eric Anholt21d40d32010-03-25 11:11:14 -07001709 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001710
Chris Wilsonea5b2132010-08-04 13:50:23 +01001711 if (output_reg == DP_A || IS_PCH_eDP(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001712 /* initialize panel mode from VBT if available for eDP */
1713 if (dev_priv->lfp_lvds_vbt_mode) {
1714 dev_priv->panel_fixed_mode =
1715 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1716 if (dev_priv->panel_fixed_mode) {
1717 dev_priv->panel_fixed_mode->type |=
1718 DRM_MODE_TYPE_PREFERRED;
1719 }
1720 }
1721 }
1722
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001723 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1724 * 0xd. Failure to do so will result in spurious interrupts being
1725 * generated on the port when a cable is not attached.
1726 */
1727 if (IS_G4X(dev) && !IS_GM45(dev)) {
1728 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
1729 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
1730 }
1731}