blob: d19334aa66adcb77deccc2631317d3dcce169538 [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
Chris Wilsondf0e9242010-09-09 16:20:55 +010070static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
71{
72 return container_of(intel_attached_encoder(connector),
73 struct intel_dp, base);
74}
75
Jesse Barnes33a34e42010-09-08 12:42:02 -070076static void intel_dp_start_link_train(struct intel_dp *intel_dp);
77static void intel_dp_complete_link_train(struct intel_dp *intel_dp);
Chris Wilsonea5b2132010-08-04 13:50:23 +010078static void intel_dp_link_down(struct intel_dp *intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -070079
Zhenyu Wang32f9d652009-07-24 01:00:32 +080080void
Eric Anholt21d40d32010-03-25 11:11:14 -070081intel_edp_link_config (struct intel_encoder *intel_encoder,
Chris Wilsonea5b2132010-08-04 13:50:23 +010082 int *lane_num, int *link_bw)
Zhenyu Wang32f9d652009-07-24 01:00:32 +080083{
Chris Wilsonea5b2132010-08-04 13:50:23 +010084 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Zhenyu Wang32f9d652009-07-24 01:00:32 +080085
Chris Wilsonea5b2132010-08-04 13:50:23 +010086 *lane_num = intel_dp->lane_count;
87 if (intel_dp->link_bw == DP_LINK_BW_1_62)
Zhenyu Wang32f9d652009-07-24 01:00:32 +080088 *link_bw = 162000;
Chris Wilsonea5b2132010-08-04 13:50:23 +010089 else if (intel_dp->link_bw == DP_LINK_BW_2_7)
Zhenyu Wang32f9d652009-07-24 01:00:32 +080090 *link_bw = 270000;
91}
92
Keith Packarda4fc5ed2009-04-07 16:16:42 -070093static int
Chris Wilsonea5b2132010-08-04 13:50:23 +010094intel_dp_max_lane_count(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -070095{
Keith Packarda4fc5ed2009-04-07 16:16:42 -070096 int max_lane_count = 4;
97
Chris Wilsonea5b2132010-08-04 13:50:23 +010098 if (intel_dp->dpcd[0] >= 0x11) {
99 max_lane_count = intel_dp->dpcd[2] & 0x1f;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700100 switch (max_lane_count) {
101 case 1: case 2: case 4:
102 break;
103 default:
104 max_lane_count = 4;
105 }
106 }
107 return max_lane_count;
108}
109
110static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100111intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700112{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100113 int max_link_bw = intel_dp->dpcd[1];
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700114
115 switch (max_link_bw) {
116 case DP_LINK_BW_1_62:
117 case DP_LINK_BW_2_7:
118 break;
119 default:
120 max_link_bw = DP_LINK_BW_1_62;
121 break;
122 }
123 return max_link_bw;
124}
125
126static int
127intel_dp_link_clock(uint8_t link_bw)
128{
129 if (link_bw == DP_LINK_BW_2_7)
130 return 270000;
131 else
132 return 162000;
133}
134
135/* I think this is a fiction */
136static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100137intel_dp_link_required(struct drm_device *dev, struct intel_dp *intel_dp, int pixel_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700138{
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800139 struct drm_i915_private *dev_priv = dev->dev_private;
140
Chris Wilsonea5b2132010-08-04 13:50:23 +0100141 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Chris Wilson5ceb0f92010-09-24 10:24:28 +0100142 return (pixel_clock * dev_priv->edp.bpp + 7) / 8;
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800143 else
144 return pixel_clock * 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700145}
146
147static int
Dave Airliefe27d532010-06-30 11:46:17 +1000148intel_dp_max_data_rate(int max_link_clock, int max_lanes)
149{
150 return (max_link_clock * max_lanes * 8) / 10;
151}
152
153static int
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700154intel_dp_mode_valid(struct drm_connector *connector,
155 struct drm_display_mode *mode)
156{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100157 struct intel_dp *intel_dp = intel_attached_dp(connector);
Zhao Yakui7de56f42010-07-19 09:43:14 +0100158 struct drm_device *dev = connector->dev;
159 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100160 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
161 int max_lanes = intel_dp_max_lane_count(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700162
Chris Wilsonea5b2132010-08-04 13:50:23 +0100163 if ((IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) &&
Zhao Yakui7de56f42010-07-19 09:43:14 +0100164 dev_priv->panel_fixed_mode) {
165 if (mode->hdisplay > dev_priv->panel_fixed_mode->hdisplay)
166 return MODE_PANEL;
167
168 if (mode->vdisplay > dev_priv->panel_fixed_mode->vdisplay)
169 return MODE_PANEL;
170 }
171
Dave Airliefe27d532010-06-30 11:46:17 +1000172 /* only refuse the mode on non eDP since we have seen some wierd eDP panels
173 which are outside spec tolerances but somehow work by magic */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100174 if (!IS_eDP(intel_dp) &&
175 (intel_dp_link_required(connector->dev, intel_dp, mode->clock)
Dave Airliefe27d532010-06-30 11:46:17 +1000176 > intel_dp_max_data_rate(max_link_clock, max_lanes)))
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700177 return MODE_CLOCK_HIGH;
178
179 if (mode->clock < 10000)
180 return MODE_CLOCK_LOW;
181
182 return MODE_OK;
183}
184
185static uint32_t
186pack_aux(uint8_t *src, int src_bytes)
187{
188 int i;
189 uint32_t v = 0;
190
191 if (src_bytes > 4)
192 src_bytes = 4;
193 for (i = 0; i < src_bytes; i++)
194 v |= ((uint32_t) src[i]) << ((3-i) * 8);
195 return v;
196}
197
198static void
199unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
200{
201 int i;
202 if (dst_bytes > 4)
203 dst_bytes = 4;
204 for (i = 0; i < dst_bytes; i++)
205 dst[i] = src >> ((3-i) * 8);
206}
207
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700208/* hrawclock is 1/4 the FSB frequency */
209static int
210intel_hrawclk(struct drm_device *dev)
211{
212 struct drm_i915_private *dev_priv = dev->dev_private;
213 uint32_t clkcfg;
214
215 clkcfg = I915_READ(CLKCFG);
216 switch (clkcfg & CLKCFG_FSB_MASK) {
217 case CLKCFG_FSB_400:
218 return 100;
219 case CLKCFG_FSB_533:
220 return 133;
221 case CLKCFG_FSB_667:
222 return 166;
223 case CLKCFG_FSB_800:
224 return 200;
225 case CLKCFG_FSB_1067:
226 return 266;
227 case CLKCFG_FSB_1333:
228 return 333;
229 /* these two are just a guess; one of them might be right */
230 case CLKCFG_FSB_1600:
231 case CLKCFG_FSB_1600_ALT:
232 return 400;
233 default:
234 return 133;
235 }
236}
237
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700238static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100239intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700240 uint8_t *send, int send_bytes,
241 uint8_t *recv, int recv_size)
242{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100243 uint32_t output_reg = intel_dp->output_reg;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100244 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700245 struct drm_i915_private *dev_priv = dev->dev_private;
246 uint32_t ch_ctl = output_reg + 0x10;
247 uint32_t ch_data = ch_ctl + 4;
248 int i;
249 int recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700250 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700251 uint32_t aux_clock_divider;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800252 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700253
254 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700255 * and would like to run at 2MHz. So, take the
256 * hrawclk value and divide by 2 and use that
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700257 *
258 * Note that PCH attached eDP panels should use a 125MHz input
259 * clock divider.
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700260 */
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700261 if (IS_eDP(intel_dp) && !IS_PCH_eDP(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +0800262 if (IS_GEN6(dev))
263 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
264 else
265 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
266 } else if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500267 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800268 else
269 aux_clock_divider = intel_hrawclk(dev) / 2;
270
Zhenyu Wange3421a12010-04-08 09:43:27 +0800271 if (IS_GEN6(dev))
272 precharge = 3;
273 else
274 precharge = 5;
275
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100276 if (I915_READ(ch_ctl) & DP_AUX_CH_CTL_SEND_BUSY) {
277 DRM_ERROR("dp_aux_ch not started status 0x%08x\n",
278 I915_READ(ch_ctl));
279 return -EBUSY;
280 }
281
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700282 /* Must try at least 3 times according to DP spec */
283 for (try = 0; try < 5; try++) {
284 /* Load the send data into the aux channel data registers */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100285 for (i = 0; i < send_bytes; i += 4)
286 I915_WRITE(ch_data + i,
287 pack_aux(send + i, send_bytes - i));
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700288
289 /* Send the command and wait for it to complete */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100290 I915_WRITE(ch_ctl,
291 DP_AUX_CH_CTL_SEND_BUSY |
292 DP_AUX_CH_CTL_TIME_OUT_400us |
293 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
294 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
295 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
296 DP_AUX_CH_CTL_DONE |
297 DP_AUX_CH_CTL_TIME_OUT_ERROR |
298 DP_AUX_CH_CTL_RECEIVE_ERROR);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700299 for (;;) {
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700300 status = I915_READ(ch_ctl);
301 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
302 break;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100303 udelay(100);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700304 }
305
306 /* Clear done status and any errors */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100307 I915_WRITE(ch_ctl,
308 status |
309 DP_AUX_CH_CTL_DONE |
310 DP_AUX_CH_CTL_TIME_OUT_ERROR |
311 DP_AUX_CH_CTL_RECEIVE_ERROR);
312 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700313 break;
314 }
315
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700316 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700317 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700318 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700319 }
320
321 /* Check for timeout or receive error.
322 * Timeouts occur when the sink is not connected
323 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700324 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700325 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700326 return -EIO;
327 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700328
329 /* Timeouts occur when the device isn't connected, so they're
330 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700331 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800332 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700333 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700334 }
335
336 /* Unload any bytes sent back from the other side */
337 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
338 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700339 if (recv_bytes > recv_size)
340 recv_bytes = recv_size;
341
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100342 for (i = 0; i < recv_bytes; i += 4)
343 unpack_aux(I915_READ(ch_data + i),
344 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700345
346 return recv_bytes;
347}
348
349/* Write data to the aux channel in native mode */
350static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100351intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700352 uint16_t address, uint8_t *send, int send_bytes)
353{
354 int ret;
355 uint8_t msg[20];
356 int msg_bytes;
357 uint8_t ack;
358
359 if (send_bytes > 16)
360 return -1;
361 msg[0] = AUX_NATIVE_WRITE << 4;
362 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800363 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700364 msg[3] = send_bytes - 1;
365 memcpy(&msg[4], send, send_bytes);
366 msg_bytes = send_bytes + 4;
367 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100368 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700369 if (ret < 0)
370 return ret;
371 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
372 break;
373 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
374 udelay(100);
375 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700376 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700377 }
378 return send_bytes;
379}
380
381/* Write a single byte to the aux channel in native mode */
382static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100383intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700384 uint16_t address, uint8_t byte)
385{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100386 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700387}
388
389/* read bytes from a native aux channel */
390static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100391intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700392 uint16_t address, uint8_t *recv, int recv_bytes)
393{
394 uint8_t msg[4];
395 int msg_bytes;
396 uint8_t reply[20];
397 int reply_bytes;
398 uint8_t ack;
399 int ret;
400
401 msg[0] = AUX_NATIVE_READ << 4;
402 msg[1] = address >> 8;
403 msg[2] = address & 0xff;
404 msg[3] = recv_bytes - 1;
405
406 msg_bytes = 4;
407 reply_bytes = recv_bytes + 1;
408
409 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100410 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700411 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700412 if (ret == 0)
413 return -EPROTO;
414 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700415 return ret;
416 ack = reply[0];
417 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
418 memcpy(recv, reply + 1, ret - 1);
419 return ret - 1;
420 }
421 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
422 udelay(100);
423 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700424 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700425 }
426}
427
428static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000429intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
430 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700431{
Dave Airlieab2c0672009-12-04 10:55:24 +1000432 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100433 struct intel_dp *intel_dp = container_of(adapter,
434 struct intel_dp,
435 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000436 uint16_t address = algo_data->address;
437 uint8_t msg[5];
438 uint8_t reply[2];
439 int msg_bytes;
440 int reply_bytes;
441 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700442
Dave Airlieab2c0672009-12-04 10:55:24 +1000443 /* Set up the command byte */
444 if (mode & MODE_I2C_READ)
445 msg[0] = AUX_I2C_READ << 4;
446 else
447 msg[0] = AUX_I2C_WRITE << 4;
448
449 if (!(mode & MODE_I2C_STOP))
450 msg[0] |= AUX_I2C_MOT << 4;
451
452 msg[1] = address >> 8;
453 msg[2] = address;
454
455 switch (mode) {
456 case MODE_I2C_WRITE:
457 msg[3] = 0;
458 msg[4] = write_byte;
459 msg_bytes = 5;
460 reply_bytes = 1;
461 break;
462 case MODE_I2C_READ:
463 msg[3] = 0;
464 msg_bytes = 4;
465 reply_bytes = 2;
466 break;
467 default:
468 msg_bytes = 3;
469 reply_bytes = 1;
470 break;
471 }
472
473 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100474 ret = intel_dp_aux_ch(intel_dp,
Dave Airlieab2c0672009-12-04 10:55:24 +1000475 msg, msg_bytes,
476 reply, reply_bytes);
477 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000478 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000479 return ret;
480 }
481 switch (reply[0] & AUX_I2C_REPLY_MASK) {
482 case AUX_I2C_REPLY_ACK:
483 if (mode == MODE_I2C_READ) {
484 *read_byte = reply[1];
485 }
486 return reply_bytes - 1;
487 case AUX_I2C_REPLY_NACK:
Dave Airlie3ff99162009-12-08 14:03:47 +1000488 DRM_DEBUG_KMS("aux_ch nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000489 return -EREMOTEIO;
490 case AUX_I2C_REPLY_DEFER:
Dave Airlie3ff99162009-12-08 14:03:47 +1000491 DRM_DEBUG_KMS("aux_ch defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000492 udelay(100);
493 break;
494 default:
495 DRM_ERROR("aux_ch invalid reply 0x%02x\n", reply[0]);
496 return -EREMOTEIO;
497 }
498 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700499}
500
501static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100502intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800503 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700504{
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800505 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100506 intel_dp->algo.running = false;
507 intel_dp->algo.address = 0;
508 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700509
Chris Wilsonea5b2132010-08-04 13:50:23 +0100510 memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
511 intel_dp->adapter.owner = THIS_MODULE;
512 intel_dp->adapter.class = I2C_CLASS_DDC;
513 strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
514 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
515 intel_dp->adapter.algo_data = &intel_dp->algo;
516 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
517
518 return i2c_dp_aux_add_bus(&intel_dp->adapter);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700519}
520
521static bool
522intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
523 struct drm_display_mode *adjusted_mode)
524{
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100525 struct drm_device *dev = encoder->dev;
526 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100527 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700528 int lane_count, clock;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100529 int max_lane_count = intel_dp_max_lane_count(intel_dp);
530 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700531 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
532
Chris Wilsonea5b2132010-08-04 13:50:23 +0100533 if ((IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) &&
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100534 dev_priv->panel_fixed_mode) {
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100535 intel_fixed_panel_mode(dev_priv->panel_fixed_mode, adjusted_mode);
536 intel_pch_panel_fitting(dev, DRM_MODE_SCALE_FULLSCREEN,
537 mode, adjusted_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100538 /*
539 * the mode->clock is used to calculate the Data&Link M/N
540 * of the pipe. For the eDP the fixed clock should be used.
541 */
542 mode->clock = dev_priv->panel_fixed_mode->clock;
543 }
544
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700545 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
546 for (clock = 0; clock <= max_clock; clock++) {
Dave Airliefe27d532010-06-30 11:46:17 +1000547 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700548
Chris Wilsonea5b2132010-08-04 13:50:23 +0100549 if (intel_dp_link_required(encoder->dev, intel_dp, mode->clock)
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800550 <= link_avail) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100551 intel_dp->link_bw = bws[clock];
552 intel_dp->lane_count = lane_count;
553 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Zhao Yakui28c97732009-10-09 11:39:41 +0800554 DRM_DEBUG_KMS("Display port link bw %02x lane "
555 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100556 intel_dp->link_bw, intel_dp->lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700557 adjusted_mode->clock);
558 return true;
559 }
560 }
561 }
Dave Airliefe27d532010-06-30 11:46:17 +1000562
Chris Wilsonea5b2132010-08-04 13:50:23 +0100563 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) {
Dave Airliefe27d532010-06-30 11:46:17 +1000564 /* okay we failed just pick the highest */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100565 intel_dp->lane_count = max_lane_count;
566 intel_dp->link_bw = bws[max_clock];
567 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Dave Airliefe27d532010-06-30 11:46:17 +1000568 DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
569 "count %d clock %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100570 intel_dp->link_bw, intel_dp->lane_count,
Dave Airliefe27d532010-06-30 11:46:17 +1000571 adjusted_mode->clock);
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100572
Dave Airliefe27d532010-06-30 11:46:17 +1000573 return true;
574 }
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100575
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700576 return false;
577}
578
579struct intel_dp_m_n {
580 uint32_t tu;
581 uint32_t gmch_m;
582 uint32_t gmch_n;
583 uint32_t link_m;
584 uint32_t link_n;
585};
586
587static void
588intel_reduce_ratio(uint32_t *num, uint32_t *den)
589{
590 while (*num > 0xffffff || *den > 0xffffff) {
591 *num >>= 1;
592 *den >>= 1;
593 }
594}
595
596static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800597intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700598 int nlanes,
599 int pixel_clock,
600 int link_clock,
601 struct intel_dp_m_n *m_n)
602{
603 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800604 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700605 m_n->gmch_n = link_clock * nlanes;
606 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
607 m_n->link_m = pixel_clock;
608 m_n->link_n = link_clock;
609 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
610}
611
Zhao Yakui36e83a12010-06-12 14:32:21 +0800612bool intel_pch_has_edp(struct drm_crtc *crtc)
613{
614 struct drm_device *dev = crtc->dev;
615 struct drm_mode_config *mode_config = &dev->mode_config;
616 struct drm_encoder *encoder;
617
618 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100619 struct intel_dp *intel_dp;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800620
Chris Wilsonea5b2132010-08-04 13:50:23 +0100621 if (encoder->crtc != crtc)
Zhao Yakui36e83a12010-06-12 14:32:21 +0800622 continue;
623
Chris Wilsonea5b2132010-08-04 13:50:23 +0100624 intel_dp = enc_to_intel_dp(encoder);
625 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
626 return intel_dp->is_pch_edp;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800627 }
628 return false;
629}
630
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700631void
632intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
633 struct drm_display_mode *adjusted_mode)
634{
635 struct drm_device *dev = crtc->dev;
636 struct drm_mode_config *mode_config = &dev->mode_config;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800637 struct drm_encoder *encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700638 struct drm_i915_private *dev_priv = dev->dev_private;
639 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Zhao Yakui36e83a12010-06-12 14:32:21 +0800640 int lane_count = 4, bpp = 24;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700641 struct intel_dp_m_n m_n;
642
643 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700644 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700645 */
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800646 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100647 struct intel_dp *intel_dp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700648
Dan Carpenterd8201ab2010-05-07 10:39:00 +0200649 if (encoder->crtc != crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700650 continue;
651
Chris Wilsonea5b2132010-08-04 13:50:23 +0100652 intel_dp = enc_to_intel_dp(encoder);
653 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT) {
654 lane_count = intel_dp->lane_count;
655 if (IS_PCH_eDP(intel_dp))
Chris Wilson5ceb0f92010-09-24 10:24:28 +0100656 bpp = dev_priv->edp.bpp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700657 break;
658 }
659 }
660
661 /*
662 * Compute the GMCH and Link ratios. The '3' here is
663 * the number of bytes_per_pixel post-LUT, which we always
664 * set up for 8-bits of R/G/B, or 3 bytes total.
665 */
Zhao Yakui36e83a12010-06-12 14:32:21 +0800666 intel_dp_compute_m_n(bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700667 mode->clock, adjusted_mode->clock, &m_n);
668
Eric Anholtc619eed2010-01-28 16:45:52 -0800669 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800670 if (intel_crtc->pipe == 0) {
671 I915_WRITE(TRANSA_DATA_M1,
672 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
673 m_n.gmch_m);
674 I915_WRITE(TRANSA_DATA_N1, m_n.gmch_n);
675 I915_WRITE(TRANSA_DP_LINK_M1, m_n.link_m);
676 I915_WRITE(TRANSA_DP_LINK_N1, m_n.link_n);
677 } else {
678 I915_WRITE(TRANSB_DATA_M1,
679 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
680 m_n.gmch_m);
681 I915_WRITE(TRANSB_DATA_N1, m_n.gmch_n);
682 I915_WRITE(TRANSB_DP_LINK_M1, m_n.link_m);
683 I915_WRITE(TRANSB_DP_LINK_N1, m_n.link_n);
684 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700685 } else {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800686 if (intel_crtc->pipe == 0) {
687 I915_WRITE(PIPEA_GMCH_DATA_M,
688 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
689 m_n.gmch_m);
690 I915_WRITE(PIPEA_GMCH_DATA_N,
691 m_n.gmch_n);
692 I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
693 I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
694 } else {
695 I915_WRITE(PIPEB_GMCH_DATA_M,
696 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
697 m_n.gmch_m);
698 I915_WRITE(PIPEB_GMCH_DATA_N,
699 m_n.gmch_n);
700 I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
701 I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
702 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700703 }
704}
705
706static void
707intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
708 struct drm_display_mode *adjusted_mode)
709{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800710 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100711 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Chris Wilson4ef69c72010-09-09 15:14:28 +0100712 struct drm_crtc *crtc = intel_dp->base.base.crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
714
Chris Wilsonea5b2132010-08-04 13:50:23 +0100715 intel_dp->DP = (DP_VOLTAGE_0_4 |
Adam Jackson9c9e7922010-04-05 17:57:59 -0400716 DP_PRE_EMPHASIS_0);
717
718 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100719 intel_dp->DP |= DP_SYNC_HS_HIGH;
Adam Jackson9c9e7922010-04-05 17:57:59 -0400720 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100721 intel_dp->DP |= DP_SYNC_VS_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700722
Chris Wilsonea5b2132010-08-04 13:50:23 +0100723 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp))
724 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800725 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100726 intel_dp->DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700727
Chris Wilsonea5b2132010-08-04 13:50:23 +0100728 switch (intel_dp->lane_count) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700729 case 1:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100730 intel_dp->DP |= DP_PORT_WIDTH_1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700731 break;
732 case 2:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100733 intel_dp->DP |= DP_PORT_WIDTH_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700734 break;
735 case 4:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100736 intel_dp->DP |= DP_PORT_WIDTH_4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700737 break;
738 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100739 if (intel_dp->has_audio)
740 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700741
Chris Wilsonea5b2132010-08-04 13:50:23 +0100742 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
743 intel_dp->link_configuration[0] = intel_dp->link_bw;
744 intel_dp->link_configuration[1] = intel_dp->lane_count;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700745
746 /*
Adam Jackson9962c922010-05-13 14:45:42 -0400747 * Check for DPCD version > 1.1 and enhanced framing support
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700748 */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100749 if (intel_dp->dpcd[0] >= 0x11 && (intel_dp->dpcd[2] & DP_ENHANCED_FRAME_CAP)) {
750 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
751 intel_dp->DP |= DP_ENHANCED_FRAMING;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700752 }
753
Zhenyu Wange3421a12010-04-08 09:43:27 +0800754 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
755 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +0100756 intel_dp->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800757
Chris Wilsonea5b2132010-08-04 13:50:23 +0100758 if (IS_eDP(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800759 /* don't miss out required setting for eDP */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100760 intel_dp->DP |= DP_PLL_ENABLE;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800761 if (adjusted_mode->clock < 200000)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100762 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800763 else
Chris Wilsonea5b2132010-08-04 13:50:23 +0100764 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800765 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700766}
767
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700768/* Returns true if the panel was already on when called */
769static bool ironlake_edp_panel_on (struct drm_device *dev)
Jesse Barnes9934c132010-07-22 13:18:19 -0700770{
771 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson913d8d12010-08-07 11:01:35 +0100772 u32 pp;
Jesse Barnes9934c132010-07-22 13:18:19 -0700773
Chris Wilson913d8d12010-08-07 11:01:35 +0100774 if (I915_READ(PCH_PP_STATUS) & PP_ON)
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700775 return true;
Jesse Barnes9934c132010-07-22 13:18:19 -0700776
777 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700778
779 /* ILK workaround: disable reset around power sequence */
780 pp &= ~PANEL_POWER_RESET;
781 I915_WRITE(PCH_PP_CONTROL, pp);
782 POSTING_READ(PCH_PP_CONTROL);
783
Jesse Barnes4d12fe02010-09-10 10:46:45 -0700784 pp |= POWER_TARGET_ON;
Jesse Barnes9934c132010-07-22 13:18:19 -0700785 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes9934c132010-07-22 13:18:19 -0700786
Hette Visser27d64332010-09-24 10:51:30 +0100787 /* Ouch. We need to wait here for some panels, like Dell e6510
788 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
789 */
790 msleep(300);
791
Chris Wilson481b6af2010-08-23 17:43:35 +0100792 if (wait_for(I915_READ(PCH_PP_STATUS) & PP_ON, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100793 DRM_ERROR("panel on wait timed out: 0x%08x\n",
794 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700795
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700796 pp &= ~(PANEL_UNLOCK_REGS);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700797 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700798 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700799 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700800
801 return false;
Jesse Barnes9934c132010-07-22 13:18:19 -0700802}
803
804static void ironlake_edp_panel_off (struct drm_device *dev)
805{
806 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson913d8d12010-08-07 11:01:35 +0100807 u32 pp;
Jesse Barnes9934c132010-07-22 13:18:19 -0700808
809 pp = I915_READ(PCH_PP_CONTROL);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700810
811 /* ILK workaround: disable reset around power sequence */
812 pp &= ~PANEL_POWER_RESET;
813 I915_WRITE(PCH_PP_CONTROL, pp);
814 POSTING_READ(PCH_PP_CONTROL);
815
Jesse Barnes9934c132010-07-22 13:18:19 -0700816 pp &= ~POWER_TARGET_ON;
817 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes9934c132010-07-22 13:18:19 -0700818
Chris Wilson481b6af2010-08-23 17:43:35 +0100819 if (wait_for((I915_READ(PCH_PP_STATUS) & PP_ON) == 0, 5000))
Chris Wilson913d8d12010-08-07 11:01:35 +0100820 DRM_ERROR("panel off wait timed out: 0x%08x\n",
821 I915_READ(PCH_PP_STATUS));
Jesse Barnes9934c132010-07-22 13:18:19 -0700822
823 /* Make sure VDD is enabled so DP AUX will work */
Jesse Barnes3969c9c92010-09-08 12:42:03 -0700824 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jesse Barnes9934c132010-07-22 13:18:19 -0700825 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -0700826 POSTING_READ(PCH_PP_CONTROL);
Hette Visser27d64332010-09-24 10:51:30 +0100827
828 /* Ouch. We need to wait here for some panels, like Dell e6510
829 * https://bugs.freedesktop.org/show_bug.cgi?id=29278i
830 */
831 msleep(300);
Jesse Barnes9934c132010-07-22 13:18:19 -0700832}
833
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700834static void ironlake_edp_panel_vdd_on(struct drm_device *dev)
835{
836 struct drm_i915_private *dev_priv = dev->dev_private;
837 u32 pp;
838
839 pp = I915_READ(PCH_PP_CONTROL);
840 pp |= EDP_FORCE_VDD;
841 I915_WRITE(PCH_PP_CONTROL, pp);
842 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes3ba5c562010-08-25 13:09:48 -0700843 msleep(300);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700844}
845
846static void ironlake_edp_panel_vdd_off(struct drm_device *dev)
847{
848 struct drm_i915_private *dev_priv = dev->dev_private;
849 u32 pp;
850
851 pp = I915_READ(PCH_PP_CONTROL);
852 pp &= ~EDP_FORCE_VDD;
853 I915_WRITE(PCH_PP_CONTROL, pp);
854 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes3ba5c562010-08-25 13:09:48 -0700855 msleep(300);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700856}
857
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500858static void ironlake_edp_backlight_on (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800859{
860 struct drm_i915_private *dev_priv = dev->dev_private;
861 u32 pp;
862
Zhao Yakui28c97732009-10-09 11:39:41 +0800863 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800864 pp = I915_READ(PCH_PP_CONTROL);
865 pp |= EDP_BLC_ENABLE;
866 I915_WRITE(PCH_PP_CONTROL, pp);
867}
868
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500869static void ironlake_edp_backlight_off (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800870{
871 struct drm_i915_private *dev_priv = dev->dev_private;
872 u32 pp;
873
Zhao Yakui28c97732009-10-09 11:39:41 +0800874 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800875 pp = I915_READ(PCH_PP_CONTROL);
876 pp &= ~EDP_BLC_ENABLE;
877 I915_WRITE(PCH_PP_CONTROL, pp);
878}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700879
Jesse Barnesd240f202010-08-13 15:43:26 -0700880static void ironlake_edp_pll_on(struct drm_encoder *encoder)
881{
882 struct drm_device *dev = encoder->dev;
883 struct drm_i915_private *dev_priv = dev->dev_private;
884 u32 dpa_ctl;
885
886 DRM_DEBUG_KMS("\n");
887 dpa_ctl = I915_READ(DP_A);
888 dpa_ctl &= ~DP_PLL_ENABLE;
889 I915_WRITE(DP_A, dpa_ctl);
890}
891
892static void ironlake_edp_pll_off(struct drm_encoder *encoder)
893{
894 struct drm_device *dev = encoder->dev;
895 struct drm_i915_private *dev_priv = dev->dev_private;
896 u32 dpa_ctl;
897
898 dpa_ctl = I915_READ(DP_A);
899 dpa_ctl |= DP_PLL_ENABLE;
900 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +0100901 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -0700902 udelay(200);
903}
904
905static void intel_dp_prepare(struct drm_encoder *encoder)
906{
907 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
908 struct drm_device *dev = encoder->dev;
909 struct drm_i915_private *dev_priv = dev->dev_private;
910 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
911
Jesse Barnes7eaf5542010-09-08 12:41:59 -0700912 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) {
Jesse Barnes2c9d9752010-09-08 12:42:05 -0700913 ironlake_edp_panel_off(dev);
Jesse Barnesd240f202010-08-13 15:43:26 -0700914 ironlake_edp_backlight_off(dev);
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700915 ironlake_edp_panel_vdd_on(dev);
Jesse Barnesd240f202010-08-13 15:43:26 -0700916 ironlake_edp_pll_on(encoder);
917 }
918 if (dp_reg & DP_PORT_EN)
919 intel_dp_link_down(intel_dp);
920}
921
922static void intel_dp_commit(struct drm_encoder *encoder)
923{
924 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
925 struct drm_device *dev = encoder->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -0700926
Jesse Barnes33a34e42010-09-08 12:42:02 -0700927 intel_dp_start_link_train(intel_dp);
928
929 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Jesse Barnesb2094bb2010-09-08 12:42:01 -0700930 ironlake_edp_panel_on(dev);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700931
932 intel_dp_complete_link_train(intel_dp);
933
934 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Jesse Barnesd240f202010-08-13 15:43:26 -0700935 ironlake_edp_backlight_on(dev);
936}
937
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700938static void
939intel_dp_dpms(struct drm_encoder *encoder, int mode)
940{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100941 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800942 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700943 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100944 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700945
946 if (mode != DRM_MODE_DPMS_ON) {
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700947 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) {
948 ironlake_edp_backlight_off(dev);
949 ironlake_edp_panel_off(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800950 }
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700951 if (dp_reg & DP_PORT_EN)
952 intel_dp_link_down(intel_dp);
Jesse Barnesd240f202010-08-13 15:43:26 -0700953 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
954 ironlake_edp_pll_off(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700955 } else {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800956 if (!(dp_reg & DP_PORT_EN)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -0700957 intel_dp_start_link_train(intel_dp);
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700958 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Jesse Barnes9934c132010-07-22 13:18:19 -0700959 ironlake_edp_panel_on(dev);
Jesse Barnes33a34e42010-09-08 12:42:02 -0700960 intel_dp_complete_link_train(intel_dp);
Jesse Barnes7643a7f2010-08-11 10:06:44 -0700961 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500962 ironlake_edp_backlight_on(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800963 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700964 }
Chris Wilsonea5b2132010-08-04 13:50:23 +0100965 intel_dp->dpms_mode = mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700966}
967
968/*
969 * Fetch AUX CH registers 0x202 - 0x207 which contain
970 * link status information
971 */
972static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -0700973intel_dp_get_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700974{
975 int ret;
976
Chris Wilsonea5b2132010-08-04 13:50:23 +0100977 ret = intel_dp_aux_native_read(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700978 DP_LANE0_1_STATUS,
Jesse Barnes33a34e42010-09-08 12:42:02 -0700979 intel_dp->link_status, DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700980 if (ret != DP_LINK_STATUS_SIZE)
981 return false;
982 return true;
983}
984
985static uint8_t
986intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
987 int r)
988{
989 return link_status[r - DP_LANE0_1_STATUS];
990}
991
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700992static uint8_t
993intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
994 int lane)
995{
996 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
997 int s = ((lane & 1) ?
998 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
999 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1000 uint8_t l = intel_dp_link_status(link_status, i);
1001
1002 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1003}
1004
1005static uint8_t
1006intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
1007 int lane)
1008{
1009 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1010 int s = ((lane & 1) ?
1011 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1012 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1013 uint8_t l = intel_dp_link_status(link_status, i);
1014
1015 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1016}
1017
1018
1019#if 0
1020static char *voltage_names[] = {
1021 "0.4V", "0.6V", "0.8V", "1.2V"
1022};
1023static char *pre_emph_names[] = {
1024 "0dB", "3.5dB", "6dB", "9.5dB"
1025};
1026static char *link_train_names[] = {
1027 "pattern 1", "pattern 2", "idle", "off"
1028};
1029#endif
1030
1031/*
1032 * These are source-specific values; current Intel hardware supports
1033 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1034 */
1035#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
1036
1037static uint8_t
1038intel_dp_pre_emphasis_max(uint8_t voltage_swing)
1039{
1040 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1041 case DP_TRAIN_VOLTAGE_SWING_400:
1042 return DP_TRAIN_PRE_EMPHASIS_6;
1043 case DP_TRAIN_VOLTAGE_SWING_600:
1044 return DP_TRAIN_PRE_EMPHASIS_6;
1045 case DP_TRAIN_VOLTAGE_SWING_800:
1046 return DP_TRAIN_PRE_EMPHASIS_3_5;
1047 case DP_TRAIN_VOLTAGE_SWING_1200:
1048 default:
1049 return DP_TRAIN_PRE_EMPHASIS_0;
1050 }
1051}
1052
1053static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001054intel_get_adjust_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001055{
1056 uint8_t v = 0;
1057 uint8_t p = 0;
1058 int lane;
1059
Jesse Barnes33a34e42010-09-08 12:42:02 -07001060 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1061 uint8_t this_v = intel_get_adjust_request_voltage(intel_dp->link_status, lane);
1062 uint8_t this_p = intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001063
1064 if (this_v > v)
1065 v = this_v;
1066 if (this_p > p)
1067 p = this_p;
1068 }
1069
1070 if (v >= I830_DP_VOLTAGE_MAX)
1071 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
1072
1073 if (p >= intel_dp_pre_emphasis_max(v))
1074 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1075
1076 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001077 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001078}
1079
1080static uint32_t
1081intel_dp_signal_levels(uint8_t train_set, int lane_count)
1082{
1083 uint32_t signal_levels = 0;
1084
1085 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
1086 case DP_TRAIN_VOLTAGE_SWING_400:
1087 default:
1088 signal_levels |= DP_VOLTAGE_0_4;
1089 break;
1090 case DP_TRAIN_VOLTAGE_SWING_600:
1091 signal_levels |= DP_VOLTAGE_0_6;
1092 break;
1093 case DP_TRAIN_VOLTAGE_SWING_800:
1094 signal_levels |= DP_VOLTAGE_0_8;
1095 break;
1096 case DP_TRAIN_VOLTAGE_SWING_1200:
1097 signal_levels |= DP_VOLTAGE_1_2;
1098 break;
1099 }
1100 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
1101 case DP_TRAIN_PRE_EMPHASIS_0:
1102 default:
1103 signal_levels |= DP_PRE_EMPHASIS_0;
1104 break;
1105 case DP_TRAIN_PRE_EMPHASIS_3_5:
1106 signal_levels |= DP_PRE_EMPHASIS_3_5;
1107 break;
1108 case DP_TRAIN_PRE_EMPHASIS_6:
1109 signal_levels |= DP_PRE_EMPHASIS_6;
1110 break;
1111 case DP_TRAIN_PRE_EMPHASIS_9_5:
1112 signal_levels |= DP_PRE_EMPHASIS_9_5;
1113 break;
1114 }
1115 return signal_levels;
1116}
1117
Zhenyu Wange3421a12010-04-08 09:43:27 +08001118/* Gen6's DP voltage swing and pre-emphasis control */
1119static uint32_t
1120intel_gen6_edp_signal_levels(uint8_t train_set)
1121{
1122 switch (train_set & (DP_TRAIN_VOLTAGE_SWING_MASK|DP_TRAIN_PRE_EMPHASIS_MASK)) {
1123 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1124 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1125 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1126 return EDP_LINK_TRAIN_400MV_6DB_SNB_B;
1127 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1128 return EDP_LINK_TRAIN_600MV_3_5DB_SNB_B;
1129 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1130 return EDP_LINK_TRAIN_800MV_0DB_SNB_B;
1131 default:
1132 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level\n");
1133 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
1134 }
1135}
1136
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001137static uint8_t
1138intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
1139 int lane)
1140{
1141 int i = DP_LANE0_1_STATUS + (lane >> 1);
1142 int s = (lane & 1) * 4;
1143 uint8_t l = intel_dp_link_status(link_status, i);
1144
1145 return (l >> s) & 0xf;
1146}
1147
1148/* Check for clock recovery is done on all channels */
1149static bool
1150intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
1151{
1152 int lane;
1153 uint8_t lane_status;
1154
1155 for (lane = 0; lane < lane_count; lane++) {
1156 lane_status = intel_get_lane_status(link_status, lane);
1157 if ((lane_status & DP_LANE_CR_DONE) == 0)
1158 return false;
1159 }
1160 return true;
1161}
1162
1163/* Check to see if channel eq is done on all channels */
1164#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
1165 DP_LANE_CHANNEL_EQ_DONE|\
1166 DP_LANE_SYMBOL_LOCKED)
1167static bool
Jesse Barnes33a34e42010-09-08 12:42:02 -07001168intel_channel_eq_ok(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001169{
1170 uint8_t lane_align;
1171 uint8_t lane_status;
1172 int lane;
1173
Jesse Barnes33a34e42010-09-08 12:42:02 -07001174 lane_align = intel_dp_link_status(intel_dp->link_status,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001175 DP_LANE_ALIGN_STATUS_UPDATED);
1176 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1177 return false;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001178 for (lane = 0; lane < intel_dp->lane_count; lane++) {
1179 lane_status = intel_get_lane_status(intel_dp->link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001180 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1181 return false;
1182 }
1183 return true;
1184}
1185
1186static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001187intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001188 uint32_t dp_reg_value,
1189 uint8_t dp_train_pat,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001190 bool first)
1191{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001192 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001193 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01001194 struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.base.crtc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001195 int ret;
1196
Chris Wilsonea5b2132010-08-04 13:50:23 +01001197 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1198 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001199 if (first)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001200 intel_wait_for_vblank(dev, intel_crtc->pipe);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001201
Chris Wilsonea5b2132010-08-04 13:50:23 +01001202 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001203 DP_TRAINING_PATTERN_SET,
1204 dp_train_pat);
1205
Chris Wilsonea5b2132010-08-04 13:50:23 +01001206 ret = intel_dp_aux_native_write(intel_dp,
Jesse Barnes33a34e42010-09-08 12:42:02 -07001207 DP_TRAINING_LANE0_SET, intel_dp->train_set, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001208 if (ret != 4)
1209 return false;
1210
1211 return true;
1212}
1213
Jesse Barnes33a34e42010-09-08 12:42:02 -07001214/* Enable corresponding port and start training pattern 1 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001215static void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001216intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001217{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001218 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001219 int i;
1220 uint8_t voltage;
1221 bool clock_recovery = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001222 bool first = true;
1223 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001224 u32 reg;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001225 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001226
1227 /* Write the link configuration data */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001228 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1229 intel_dp->link_configuration,
1230 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001231
1232 DP |= DP_PORT_EN;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001233 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001234 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1235 else
1236 DP &= ~DP_LINK_TRAIN_MASK;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001237 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001238 voltage = 0xff;
1239 tries = 0;
1240 clock_recovery = false;
1241 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001242 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001243 uint32_t signal_levels;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001244 if (IS_GEN6(dev) && IS_eDP(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001245 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001246 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1247 } else {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001248 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001249 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1250 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001251
Chris Wilsonea5b2132010-08-04 13:50:23 +01001252 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001253 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1254 else
1255 reg = DP | DP_LINK_TRAIN_PAT_1;
1256
Chris Wilsonea5b2132010-08-04 13:50:23 +01001257 if (!intel_dp_set_link_train(intel_dp, reg,
Jesse Barnes33a34e42010-09-08 12:42:02 -07001258 DP_TRAINING_PATTERN_1, first))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001259 break;
1260 first = false;
1261 /* Set training pattern 1 */
1262
1263 udelay(100);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001264 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001265 break;
1266
Jesse Barnes33a34e42010-09-08 12:42:02 -07001267 if (intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001268 clock_recovery = true;
1269 break;
1270 }
1271
1272 /* Check to see if we've tried the max voltage */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001273 for (i = 0; i < intel_dp->lane_count; i++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001274 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001275 break;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001276 if (i == intel_dp->lane_count)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001277 break;
1278
1279 /* Check to see if we've tried the same voltage 5 times */
Jesse Barnes33a34e42010-09-08 12:42:02 -07001280 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001281 ++tries;
1282 if (tries == 5)
1283 break;
1284 } else
1285 tries = 0;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001286 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001287
Jesse Barnes33a34e42010-09-08 12:42:02 -07001288 /* Compute new intel_dp->train_set as requested by target */
1289 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001290 }
1291
Jesse Barnes33a34e42010-09-08 12:42:02 -07001292 intel_dp->DP = DP;
1293}
1294
1295static void
1296intel_dp_complete_link_train(struct intel_dp *intel_dp)
1297{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001298 struct drm_device *dev = intel_dp->base.base.dev;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001299 struct drm_i915_private *dev_priv = dev->dev_private;
1300 bool channel_eq = false;
1301 int tries;
1302 u32 reg;
1303 uint32_t DP = intel_dp->DP;
1304
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001305 /* channel equalization */
1306 tries = 0;
1307 channel_eq = false;
1308 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001309 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001310 uint32_t signal_levels;
1311
Chris Wilsonea5b2132010-08-04 13:50:23 +01001312 if (IS_GEN6(dev) && IS_eDP(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001313 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001314 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1315 } else {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001316 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0], intel_dp->lane_count);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001317 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1318 }
1319
Chris Wilsonea5b2132010-08-04 13:50:23 +01001320 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001321 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1322 else
1323 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001324
1325 /* channel eq pattern */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001326 if (!intel_dp_set_link_train(intel_dp, reg,
Jesse Barnes33a34e42010-09-08 12:42:02 -07001327 DP_TRAINING_PATTERN_2,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001328 false))
1329 break;
1330
1331 udelay(400);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001332 if (!intel_dp_get_link_status(intel_dp))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001333 break;
1334
Jesse Barnes33a34e42010-09-08 12:42:02 -07001335 if (intel_channel_eq_ok(intel_dp)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001336 channel_eq = true;
1337 break;
1338 }
1339
1340 /* Try 5 times */
1341 if (tries > 5)
1342 break;
1343
Jesse Barnes33a34e42010-09-08 12:42:02 -07001344 /* Compute new intel_dp->train_set as requested by target */
1345 intel_get_adjust_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001346 ++tries;
1347 }
1348
Chris Wilsonea5b2132010-08-04 13:50:23 +01001349 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp))
Zhenyu Wange3421a12010-04-08 09:43:27 +08001350 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1351 else
1352 reg = DP | DP_LINK_TRAIN_OFF;
1353
Chris Wilsonea5b2132010-08-04 13:50:23 +01001354 I915_WRITE(intel_dp->output_reg, reg);
1355 POSTING_READ(intel_dp->output_reg);
1356 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001357 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1358}
1359
1360static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001361intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001362{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001363 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001364 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001365 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001366
Zhao Yakui28c97732009-10-09 11:39:41 +08001367 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001368
Chris Wilsonea5b2132010-08-04 13:50:23 +01001369 if (IS_eDP(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001370 DP &= ~DP_PLL_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001371 I915_WRITE(intel_dp->output_reg, DP);
1372 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001373 udelay(100);
1374 }
1375
Chris Wilsonea5b2132010-08-04 13:50:23 +01001376 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_dp)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001377 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001378 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001379 } else {
1380 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001381 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001382 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01001383 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001384
Chris Wilsonfe255d02010-09-11 21:37:48 +01001385 msleep(17);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001386
Chris Wilsonea5b2132010-08-04 13:50:23 +01001387 if (IS_eDP(intel_dp))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001388 DP |= DP_LINK_TRAIN_OFF;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001389 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
1390 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001391}
1392
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001393/*
1394 * According to DP spec
1395 * 5.1.2:
1396 * 1. Read DPCD
1397 * 2. Configure link according to Receiver Capabilities
1398 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1399 * 4. Check link status on receipt of hot-plug interrupt
1400 */
1401
1402static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001403intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001404{
Chris Wilson4ef69c72010-09-09 15:14:28 +01001405 if (!intel_dp->base.base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001406 return;
1407
Jesse Barnes33a34e42010-09-08 12:42:02 -07001408 if (!intel_dp_get_link_status(intel_dp)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001409 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001410 return;
1411 }
1412
Jesse Barnes33a34e42010-09-08 12:42:02 -07001413 if (!intel_channel_eq_ok(intel_dp)) {
1414 intel_dp_start_link_train(intel_dp);
1415 intel_dp_complete_link_train(intel_dp);
1416 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001417}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001418
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001419static enum drm_connector_status
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001420ironlake_dp_detect(struct drm_connector *connector)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001421{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001422 struct intel_dp *intel_dp = intel_attached_dp(connector);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001423 enum drm_connector_status status;
1424
Jesse Barnes7eaf5542010-09-08 12:41:59 -07001425 /* Panel needs power for AUX to work */
1426 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
Jesse Barnesb2094bb2010-09-08 12:42:01 -07001427 ironlake_edp_panel_vdd_on(connector->dev);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001428 status = connector_status_disconnected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001429 if (intel_dp_aux_native_read(intel_dp,
1430 0x000, intel_dp->dpcd,
1431 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001432 {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001433 if (intel_dp->dpcd[0] != 0)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001434 status = connector_status_connected;
1435 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001436 DRM_DEBUG_KMS("DPCD: %hx%hx%hx%hx\n", intel_dp->dpcd[0],
1437 intel_dp->dpcd[1], intel_dp->dpcd[2], intel_dp->dpcd[3]);
Jesse Barnesb2094bb2010-09-08 12:42:01 -07001438 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp))
1439 ironlake_edp_panel_vdd_off(connector->dev);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001440 return status;
1441}
1442
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001443/**
1444 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1445 *
1446 * \return true if DP port is connected.
1447 * \return false if DP port is disconnected.
1448 */
1449static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +01001450intel_dp_detect(struct drm_connector *connector, bool force)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001451{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001452 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001453 struct drm_device *dev = intel_dp->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001454 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001455 uint32_t temp, bit;
1456 enum drm_connector_status status;
1457
Chris Wilsonea5b2132010-08-04 13:50:23 +01001458 intel_dp->has_audio = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001459
Eric Anholtc619eed2010-01-28 16:45:52 -08001460 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001461 return ironlake_dp_detect(connector);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001462
Chris Wilsonea5b2132010-08-04 13:50:23 +01001463 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001464 case DP_B:
1465 bit = DPB_HOTPLUG_INT_STATUS;
1466 break;
1467 case DP_C:
1468 bit = DPC_HOTPLUG_INT_STATUS;
1469 break;
1470 case DP_D:
1471 bit = DPD_HOTPLUG_INT_STATUS;
1472 break;
1473 default:
1474 return connector_status_unknown;
1475 }
1476
1477 temp = I915_READ(PORT_HOTPLUG_STAT);
1478
1479 if ((temp & bit) == 0)
1480 return connector_status_disconnected;
1481
1482 status = connector_status_disconnected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001483 if (intel_dp_aux_native_read(intel_dp,
1484 0x000, intel_dp->dpcd,
1485 sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001486 {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001487 if (intel_dp->dpcd[0] != 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001488 status = connector_status_connected;
1489 }
1490 return status;
1491}
1492
1493static int intel_dp_get_modes(struct drm_connector *connector)
1494{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001495 struct intel_dp *intel_dp = intel_attached_dp(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001496 struct drm_device *dev = intel_dp->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001497 struct drm_i915_private *dev_priv = dev->dev_private;
1498 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001499
1500 /* We should parse the EDID data and find out if it has an audio sink
1501 */
1502
Chris Wilsonf899fc62010-07-20 15:44:45 -07001503 ret = intel_ddc_get_modes(connector, &intel_dp->adapter);
Zhao Yakuib9efc482010-07-19 09:43:11 +01001504 if (ret) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001505 if ((IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) &&
Zhao Yakuib9efc482010-07-19 09:43:11 +01001506 !dev_priv->panel_fixed_mode) {
1507 struct drm_display_mode *newmode;
1508 list_for_each_entry(newmode, &connector->probed_modes,
1509 head) {
1510 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1511 dev_priv->panel_fixed_mode =
1512 drm_mode_duplicate(dev, newmode);
1513 break;
1514 }
1515 }
1516 }
1517
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001518 return ret;
Zhao Yakuib9efc482010-07-19 09:43:11 +01001519 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001520
1521 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001522 if (IS_eDP(intel_dp) || IS_PCH_eDP(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001523 if (dev_priv->panel_fixed_mode != NULL) {
1524 struct drm_display_mode *mode;
1525 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1526 drm_mode_probed_add(connector, mode);
1527 return 1;
1528 }
1529 }
1530 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001531}
1532
1533static void
1534intel_dp_destroy (struct drm_connector *connector)
1535{
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001536 drm_sysfs_connector_remove(connector);
1537 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001538 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001539}
1540
Daniel Vetter24d05922010-08-20 18:08:28 +02001541static void intel_dp_encoder_destroy(struct drm_encoder *encoder)
1542{
1543 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1544
1545 i2c_del_adapter(&intel_dp->adapter);
1546 drm_encoder_cleanup(encoder);
1547 kfree(intel_dp);
1548}
1549
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001550static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1551 .dpms = intel_dp_dpms,
1552 .mode_fixup = intel_dp_mode_fixup,
Jesse Barnesd240f202010-08-13 15:43:26 -07001553 .prepare = intel_dp_prepare,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001554 .mode_set = intel_dp_mode_set,
Jesse Barnesd240f202010-08-13 15:43:26 -07001555 .commit = intel_dp_commit,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001556};
1557
1558static const struct drm_connector_funcs intel_dp_connector_funcs = {
1559 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001560 .detect = intel_dp_detect,
1561 .fill_modes = drm_helper_probe_single_connector_modes,
1562 .destroy = intel_dp_destroy,
1563};
1564
1565static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1566 .get_modes = intel_dp_get_modes,
1567 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01001568 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001569};
1570
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001571static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02001572 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001573};
1574
Chris Wilson995b6762010-08-20 13:23:26 +01001575static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001576intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07001577{
Chris Wilsonea5b2132010-08-04 13:50:23 +01001578 struct intel_dp *intel_dp = container_of(intel_encoder, struct intel_dp, base);
Keith Packardc8110e52009-05-06 11:51:10 -07001579
Chris Wilsonea5b2132010-08-04 13:50:23 +01001580 if (intel_dp->dpms_mode == DRM_MODE_DPMS_ON)
1581 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07001582}
1583
Zhenyu Wange3421a12010-04-08 09:43:27 +08001584/* Return which DP Port should be selected for Transcoder DP control */
1585int
1586intel_trans_dp_port_sel (struct drm_crtc *crtc)
1587{
1588 struct drm_device *dev = crtc->dev;
1589 struct drm_mode_config *mode_config = &dev->mode_config;
1590 struct drm_encoder *encoder;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001591
1592 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001593 struct intel_dp *intel_dp;
1594
Dan Carpenterd8201ab2010-05-07 10:39:00 +02001595 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08001596 continue;
1597
Chris Wilsonea5b2132010-08-04 13:50:23 +01001598 intel_dp = enc_to_intel_dp(encoder);
1599 if (intel_dp->base.type == INTEL_OUTPUT_DISPLAYPORT)
1600 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001601 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001602
Zhenyu Wange3421a12010-04-08 09:43:27 +08001603 return -1;
1604}
1605
Zhao Yakui36e83a12010-06-12 14:32:21 +08001606/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04001607bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08001608{
1609 struct drm_i915_private *dev_priv = dev->dev_private;
1610 struct child_device_config *p_child;
1611 int i;
1612
1613 if (!dev_priv->child_dev_num)
1614 return false;
1615
1616 for (i = 0; i < dev_priv->child_dev_num; i++) {
1617 p_child = dev_priv->child_dev + i;
1618
1619 if (p_child->dvo_port == PORT_IDPD &&
1620 p_child->device_type == DEVICE_TYPE_eDP)
1621 return true;
1622 }
1623 return false;
1624}
1625
Keith Packardc8110e52009-05-06 11:51:10 -07001626void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001627intel_dp_init(struct drm_device *dev, int output_reg)
1628{
1629 struct drm_i915_private *dev_priv = dev->dev_private;
1630 struct drm_connector *connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001631 struct intel_dp *intel_dp;
Eric Anholt21d40d32010-03-25 11:11:14 -07001632 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001633 struct intel_connector *intel_connector;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001634 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04001635 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001636
Chris Wilsonea5b2132010-08-04 13:50:23 +01001637 intel_dp = kzalloc(sizeof(struct intel_dp), GFP_KERNEL);
1638 if (!intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001639 return;
1640
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001641 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1642 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001643 kfree(intel_dp);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001644 return;
1645 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01001646 intel_encoder = &intel_dp->base;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001647
Chris Wilsonea5b2132010-08-04 13:50:23 +01001648 if (HAS_PCH_SPLIT(dev) && output_reg == PCH_DP_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04001649 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01001650 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04001651
Chris Wilsonea5b2132010-08-04 13:50:23 +01001652 if (output_reg == DP_A || IS_PCH_eDP(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04001653 type = DRM_MODE_CONNECTOR_eDP;
1654 intel_encoder->type = INTEL_OUTPUT_EDP;
1655 } else {
1656 type = DRM_MODE_CONNECTOR_DisplayPort;
1657 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1658 }
1659
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001660 connector = &intel_connector->base;
Adam Jacksonb3295302010-07-16 14:46:28 -04001661 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001662 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1663
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001664 connector->polled = DRM_CONNECTOR_POLL_HPD;
1665
Zhao Yakui652af9d2009-12-02 10:03:33 +08001666 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07001667 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001668 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07001669 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001670 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07001671 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08001672
Chris Wilsonea5b2132010-08-04 13:50:23 +01001673 if (IS_eDP(intel_dp))
Eric Anholt21d40d32010-03-25 11:11:14 -07001674 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08001675
Eric Anholt21d40d32010-03-25 11:11:14 -07001676 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001677 connector->interlace_allowed = true;
1678 connector->doublescan_allowed = 0;
1679
Chris Wilsonea5b2132010-08-04 13:50:23 +01001680 intel_dp->output_reg = output_reg;
1681 intel_dp->has_audio = false;
1682 intel_dp->dpms_mode = DRM_MODE_DPMS_ON;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001683
Chris Wilson4ef69c72010-09-09 15:14:28 +01001684 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001685 DRM_MODE_ENCODER_TMDS);
Chris Wilson4ef69c72010-09-09 15:14:28 +01001686 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001687
Chris Wilsondf0e9242010-09-09 16:20:55 +01001688 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001689 drm_sysfs_connector_add(connector);
1690
1691 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001692 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001693 case DP_A:
1694 name = "DPDDC-A";
1695 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001696 case DP_B:
1697 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001698 dev_priv->hotplug_supported_mask |=
1699 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001700 name = "DPDDC-B";
1701 break;
1702 case DP_C:
1703 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001704 dev_priv->hotplug_supported_mask |=
1705 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001706 name = "DPDDC-C";
1707 break;
1708 case DP_D:
1709 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001710 dev_priv->hotplug_supported_mask |=
1711 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001712 name = "DPDDC-D";
1713 break;
1714 }
1715
Chris Wilsonea5b2132010-08-04 13:50:23 +01001716 intel_dp_i2c_init(intel_dp, intel_connector, name);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001717
Eric Anholt21d40d32010-03-25 11:11:14 -07001718 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001719
Chris Wilsonea5b2132010-08-04 13:50:23 +01001720 if (output_reg == DP_A || IS_PCH_eDP(intel_dp)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001721 /* initialize panel mode from VBT if available for eDP */
1722 if (dev_priv->lfp_lvds_vbt_mode) {
1723 dev_priv->panel_fixed_mode =
1724 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1725 if (dev_priv->panel_fixed_mode) {
1726 dev_priv->panel_fixed_mode->type |=
1727 DRM_MODE_TYPE_PREFERRED;
1728 }
1729 }
1730 }
1731
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001732 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1733 * 0xd. Failure to do so will result in spurious interrupts being
1734 * generated on the port when a cable is not attached.
1735 */
1736 if (IS_G4X(dev) && !IS_GM45(dev)) {
1737 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
1738 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
1739 }
1740}