blob: b4f02826676efb9747c52af2b56c683e60d0dcd8 [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
Zhenyu Wang32f9d652009-07-24 01:00:32 +080045#define IS_eDP(i) ((i)->type == INTEL_OUTPUT_EDP)
Zhao Yakui36e83a12010-06-12 14:32:21 +080046#define IS_PCH_eDP(dp_priv) ((dp_priv)->has_edp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +080047
Keith Packarda4fc5ed2009-04-07 16:16:42 -070048struct intel_dp_priv {
49 uint32_t output_reg;
50 uint32_t DP;
51 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070052 bool has_audio;
Keith Packardc8110e52009-05-06 11:51:10 -070053 int dpms_mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070054 uint8_t link_bw;
55 uint8_t lane_count;
56 uint8_t dpcd[4];
Eric Anholt21d40d32010-03-25 11:11:14 -070057 struct intel_encoder *intel_encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070058 struct i2c_adapter adapter;
59 struct i2c_algo_dp_aux_data algo;
Zhao Yakui36e83a12010-06-12 14:32:21 +080060 bool has_edp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070061};
62
63static void
Eric Anholt21d40d32010-03-25 11:11:14 -070064intel_dp_link_train(struct intel_encoder *intel_encoder, uint32_t DP,
Keith Packarda4fc5ed2009-04-07 16:16:42 -070065 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE]);
66
67static void
Eric Anholt21d40d32010-03-25 11:11:14 -070068intel_dp_link_down(struct intel_encoder *intel_encoder, uint32_t DP);
Keith Packarda4fc5ed2009-04-07 16:16:42 -070069
Zhenyu Wang32f9d652009-07-24 01:00:32 +080070void
Eric Anholt21d40d32010-03-25 11:11:14 -070071intel_edp_link_config (struct intel_encoder *intel_encoder,
Zhenyu Wang32f9d652009-07-24 01:00:32 +080072 int *lane_num, int *link_bw)
73{
Eric Anholt21d40d32010-03-25 11:11:14 -070074 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Zhenyu Wang32f9d652009-07-24 01:00:32 +080075
76 *lane_num = dp_priv->lane_count;
77 if (dp_priv->link_bw == DP_LINK_BW_1_62)
78 *link_bw = 162000;
79 else if (dp_priv->link_bw == DP_LINK_BW_2_7)
80 *link_bw = 270000;
81}
82
Keith Packarda4fc5ed2009-04-07 16:16:42 -070083static int
Eric Anholt21d40d32010-03-25 11:11:14 -070084intel_dp_max_lane_count(struct intel_encoder *intel_encoder)
Keith Packarda4fc5ed2009-04-07 16:16:42 -070085{
Eric Anholt21d40d32010-03-25 11:11:14 -070086 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -070087 int max_lane_count = 4;
88
89 if (dp_priv->dpcd[0] >= 0x11) {
90 max_lane_count = dp_priv->dpcd[2] & 0x1f;
91 switch (max_lane_count) {
92 case 1: case 2: case 4:
93 break;
94 default:
95 max_lane_count = 4;
96 }
97 }
98 return max_lane_count;
99}
100
101static int
Eric Anholt21d40d32010-03-25 11:11:14 -0700102intel_dp_max_link_bw(struct intel_encoder *intel_encoder)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700103{
Eric Anholt21d40d32010-03-25 11:11:14 -0700104 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700105 int max_link_bw = dp_priv->dpcd[1];
106
107 switch (max_link_bw) {
108 case DP_LINK_BW_1_62:
109 case DP_LINK_BW_2_7:
110 break;
111 default:
112 max_link_bw = DP_LINK_BW_1_62;
113 break;
114 }
115 return max_link_bw;
116}
117
118static int
119intel_dp_link_clock(uint8_t link_bw)
120{
121 if (link_bw == DP_LINK_BW_2_7)
122 return 270000;
123 else
124 return 162000;
125}
126
127/* I think this is a fiction */
128static int
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800129intel_dp_link_required(struct drm_device *dev,
Eric Anholt21d40d32010-03-25 11:11:14 -0700130 struct intel_encoder *intel_encoder, int pixel_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700131{
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800132 struct drm_i915_private *dev_priv = dev->dev_private;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800133 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800134
Zhao Yakui36e83a12010-06-12 14:32:21 +0800135 if (IS_eDP(intel_encoder) || IS_PCH_eDP(dp_priv))
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);
152 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -0700153 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_encoder));
154 int max_lanes = intel_dp_max_lane_count(intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700155
Dave Airliefe27d532010-06-30 11:46:17 +1000156 /* only refuse the mode on non eDP since we have seen some wierd eDP panels
157 which are outside spec tolerances but somehow work by magic */
158 if (!IS_eDP(intel_encoder) &&
159 (intel_dp_link_required(connector->dev, intel_encoder, mode->clock)
160 > intel_dp_max_data_rate(max_link_clock, max_lanes)))
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700161 return MODE_CLOCK_HIGH;
162
163 if (mode->clock < 10000)
164 return MODE_CLOCK_LOW;
165
166 return MODE_OK;
167}
168
169static uint32_t
170pack_aux(uint8_t *src, int src_bytes)
171{
172 int i;
173 uint32_t v = 0;
174
175 if (src_bytes > 4)
176 src_bytes = 4;
177 for (i = 0; i < src_bytes; i++)
178 v |= ((uint32_t) src[i]) << ((3-i) * 8);
179 return v;
180}
181
182static void
183unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
184{
185 int i;
186 if (dst_bytes > 4)
187 dst_bytes = 4;
188 for (i = 0; i < dst_bytes; i++)
189 dst[i] = src >> ((3-i) * 8);
190}
191
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700192/* hrawclock is 1/4 the FSB frequency */
193static int
194intel_hrawclk(struct drm_device *dev)
195{
196 struct drm_i915_private *dev_priv = dev->dev_private;
197 uint32_t clkcfg;
198
199 clkcfg = I915_READ(CLKCFG);
200 switch (clkcfg & CLKCFG_FSB_MASK) {
201 case CLKCFG_FSB_400:
202 return 100;
203 case CLKCFG_FSB_533:
204 return 133;
205 case CLKCFG_FSB_667:
206 return 166;
207 case CLKCFG_FSB_800:
208 return 200;
209 case CLKCFG_FSB_1067:
210 return 266;
211 case CLKCFG_FSB_1333:
212 return 333;
213 /* these two are just a guess; one of them might be right */
214 case CLKCFG_FSB_1600:
215 case CLKCFG_FSB_1600_ALT:
216 return 400;
217 default:
218 return 133;
219 }
220}
221
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700222static int
Eric Anholt21d40d32010-03-25 11:11:14 -0700223intel_dp_aux_ch(struct intel_encoder *intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700224 uint8_t *send, int send_bytes,
225 uint8_t *recv, int recv_size)
226{
Eric Anholt21d40d32010-03-25 11:11:14 -0700227 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700228 uint32_t output_reg = dp_priv->output_reg;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800229 struct drm_device *dev = intel_encoder->enc.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700230 struct drm_i915_private *dev_priv = dev->dev_private;
231 uint32_t ch_ctl = output_reg + 0x10;
232 uint32_t ch_data = ch_ctl + 4;
233 int i;
234 int recv_bytes;
235 uint32_t ctl;
236 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700237 uint32_t aux_clock_divider;
Zhenyu Wange3421a12010-04-08 09:43:27 +0800238 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700239
240 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700241 * and would like to run at 2MHz. So, take the
242 * hrawclk value and divide by 2 and use that
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700243 */
Zhenyu Wange3421a12010-04-08 09:43:27 +0800244 if (IS_eDP(intel_encoder)) {
245 if (IS_GEN6(dev))
246 aux_clock_divider = 200; /* SNB eDP input clock at 400Mhz */
247 else
248 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
249 } else if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500250 aux_clock_divider = 62; /* IRL input clock fixed at 125Mhz */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800251 else
252 aux_clock_divider = intel_hrawclk(dev) / 2;
253
Zhenyu Wange3421a12010-04-08 09:43:27 +0800254 if (IS_GEN6(dev))
255 precharge = 3;
256 else
257 precharge = 5;
258
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700259 /* Must try at least 3 times according to DP spec */
260 for (try = 0; try < 5; try++) {
261 /* Load the send data into the aux channel data registers */
262 for (i = 0; i < send_bytes; i += 4) {
Joe Perchesa419aef2009-08-18 11:18:35 -0700263 uint32_t d = pack_aux(send + i, send_bytes - i);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700264
265 I915_WRITE(ch_data + i, d);
266 }
267
268 ctl = (DP_AUX_CH_CTL_SEND_BUSY |
269 DP_AUX_CH_CTL_TIME_OUT_400us |
270 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
Zhenyu Wange3421a12010-04-08 09:43:27 +0800271 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700272 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
273 DP_AUX_CH_CTL_DONE |
274 DP_AUX_CH_CTL_TIME_OUT_ERROR |
275 DP_AUX_CH_CTL_RECEIVE_ERROR);
276
277 /* Send the command and wait for it to complete */
278 I915_WRITE(ch_ctl, ctl);
279 (void) I915_READ(ch_ctl);
280 for (;;) {
281 udelay(100);
282 status = I915_READ(ch_ctl);
283 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
284 break;
285 }
286
287 /* Clear done status and any errors */
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800288 I915_WRITE(ch_ctl, (status |
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700289 DP_AUX_CH_CTL_DONE |
290 DP_AUX_CH_CTL_TIME_OUT_ERROR |
291 DP_AUX_CH_CTL_RECEIVE_ERROR));
292 (void) I915_READ(ch_ctl);
293 if ((status & DP_AUX_CH_CTL_TIME_OUT_ERROR) == 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700294 break;
295 }
296
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700297 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700298 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700299 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700300 }
301
302 /* Check for timeout or receive error.
303 * Timeouts occur when the sink is not connected
304 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700305 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700306 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700307 return -EIO;
308 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700309
310 /* Timeouts occur when the device isn't connected, so they're
311 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700312 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800313 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700314 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700315 }
316
317 /* Unload any bytes sent back from the other side */
318 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
319 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
320
321 if (recv_bytes > recv_size)
322 recv_bytes = recv_size;
323
324 for (i = 0; i < recv_bytes; i += 4) {
325 uint32_t d = I915_READ(ch_data + i);
326
327 unpack_aux(d, recv + i, recv_bytes - i);
328 }
329
330 return recv_bytes;
331}
332
333/* Write data to the aux channel in native mode */
334static int
Eric Anholt21d40d32010-03-25 11:11:14 -0700335intel_dp_aux_native_write(struct intel_encoder *intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700336 uint16_t address, uint8_t *send, int send_bytes)
337{
338 int ret;
339 uint8_t msg[20];
340 int msg_bytes;
341 uint8_t ack;
342
343 if (send_bytes > 16)
344 return -1;
345 msg[0] = AUX_NATIVE_WRITE << 4;
346 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800347 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700348 msg[3] = send_bytes - 1;
349 memcpy(&msg[4], send, send_bytes);
350 msg_bytes = send_bytes + 4;
351 for (;;) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700352 ret = intel_dp_aux_ch(intel_encoder, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700353 if (ret < 0)
354 return ret;
355 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
356 break;
357 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
358 udelay(100);
359 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700360 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700361 }
362 return send_bytes;
363}
364
365/* Write a single byte to the aux channel in native mode */
366static int
Eric Anholt21d40d32010-03-25 11:11:14 -0700367intel_dp_aux_native_write_1(struct intel_encoder *intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700368 uint16_t address, uint8_t byte)
369{
Eric Anholt21d40d32010-03-25 11:11:14 -0700370 return intel_dp_aux_native_write(intel_encoder, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700371}
372
373/* read bytes from a native aux channel */
374static int
Eric Anholt21d40d32010-03-25 11:11:14 -0700375intel_dp_aux_native_read(struct intel_encoder *intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700376 uint16_t address, uint8_t *recv, int recv_bytes)
377{
378 uint8_t msg[4];
379 int msg_bytes;
380 uint8_t reply[20];
381 int reply_bytes;
382 uint8_t ack;
383 int ret;
384
385 msg[0] = AUX_NATIVE_READ << 4;
386 msg[1] = address >> 8;
387 msg[2] = address & 0xff;
388 msg[3] = recv_bytes - 1;
389
390 msg_bytes = 4;
391 reply_bytes = recv_bytes + 1;
392
393 for (;;) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700394 ret = intel_dp_aux_ch(intel_encoder, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700395 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700396 if (ret == 0)
397 return -EPROTO;
398 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700399 return ret;
400 ack = reply[0];
401 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
402 memcpy(recv, reply + 1, ret - 1);
403 return ret - 1;
404 }
405 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
406 udelay(100);
407 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700408 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700409 }
410}
411
412static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000413intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
414 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700415{
Dave Airlieab2c0672009-12-04 10:55:24 +1000416 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700417 struct intel_dp_priv *dp_priv = container_of(adapter,
418 struct intel_dp_priv,
419 adapter);
Eric Anholt21d40d32010-03-25 11:11:14 -0700420 struct intel_encoder *intel_encoder = dp_priv->intel_encoder;
Dave Airlieab2c0672009-12-04 10:55:24 +1000421 uint16_t address = algo_data->address;
422 uint8_t msg[5];
423 uint8_t reply[2];
424 int msg_bytes;
425 int reply_bytes;
426 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700427
Dave Airlieab2c0672009-12-04 10:55:24 +1000428 /* Set up the command byte */
429 if (mode & MODE_I2C_READ)
430 msg[0] = AUX_I2C_READ << 4;
431 else
432 msg[0] = AUX_I2C_WRITE << 4;
433
434 if (!(mode & MODE_I2C_STOP))
435 msg[0] |= AUX_I2C_MOT << 4;
436
437 msg[1] = address >> 8;
438 msg[2] = address;
439
440 switch (mode) {
441 case MODE_I2C_WRITE:
442 msg[3] = 0;
443 msg[4] = write_byte;
444 msg_bytes = 5;
445 reply_bytes = 1;
446 break;
447 case MODE_I2C_READ:
448 msg[3] = 0;
449 msg_bytes = 4;
450 reply_bytes = 2;
451 break;
452 default:
453 msg_bytes = 3;
454 reply_bytes = 1;
455 break;
456 }
457
458 for (;;) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700459 ret = intel_dp_aux_ch(intel_encoder,
Dave Airlieab2c0672009-12-04 10:55:24 +1000460 msg, msg_bytes,
461 reply, reply_bytes);
462 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000463 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000464 return ret;
465 }
466 switch (reply[0] & AUX_I2C_REPLY_MASK) {
467 case AUX_I2C_REPLY_ACK:
468 if (mode == MODE_I2C_READ) {
469 *read_byte = reply[1];
470 }
471 return reply_bytes - 1;
472 case AUX_I2C_REPLY_NACK:
Dave Airlie3ff99162009-12-08 14:03:47 +1000473 DRM_DEBUG_KMS("aux_ch nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000474 return -EREMOTEIO;
475 case AUX_I2C_REPLY_DEFER:
Dave Airlie3ff99162009-12-08 14:03:47 +1000476 DRM_DEBUG_KMS("aux_ch defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000477 udelay(100);
478 break;
479 default:
480 DRM_ERROR("aux_ch invalid reply 0x%02x\n", reply[0]);
481 return -EREMOTEIO;
482 }
483 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700484}
485
486static int
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800487intel_dp_i2c_init(struct intel_encoder *intel_encoder,
488 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700489{
Eric Anholt21d40d32010-03-25 11:11:14 -0700490 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700491
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800492 DRM_DEBUG_KMS("i2c_init %s\n", name);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700493 dp_priv->algo.running = false;
494 dp_priv->algo.address = 0;
495 dp_priv->algo.aux_ch = intel_dp_i2c_aux_ch;
496
497 memset(&dp_priv->adapter, '\0', sizeof (dp_priv->adapter));
498 dp_priv->adapter.owner = THIS_MODULE;
499 dp_priv->adapter.class = I2C_CLASS_DDC;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800500 strncpy (dp_priv->adapter.name, name, sizeof(dp_priv->adapter.name) - 1);
501 dp_priv->adapter.name[sizeof(dp_priv->adapter.name) - 1] = '\0';
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700502 dp_priv->adapter.algo_data = &dp_priv->algo;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800503 dp_priv->adapter.dev.parent = &intel_connector->base.kdev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700504
505 return i2c_dp_aux_add_bus(&dp_priv->adapter);
506}
507
508static bool
509intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
510 struct drm_display_mode *adjusted_mode)
511{
Eric Anholt21d40d32010-03-25 11:11:14 -0700512 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
513 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700514 int lane_count, clock;
Eric Anholt21d40d32010-03-25 11:11:14 -0700515 int max_lane_count = intel_dp_max_lane_count(intel_encoder);
516 int max_clock = intel_dp_max_link_bw(intel_encoder) == DP_LINK_BW_2_7 ? 1 : 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700517 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
518
519 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
520 for (clock = 0; clock <= max_clock; clock++) {
Dave Airliefe27d532010-06-30 11:46:17 +1000521 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700522
Eric Anholt21d40d32010-03-25 11:11:14 -0700523 if (intel_dp_link_required(encoder->dev, intel_encoder, mode->clock)
Zhenyu Wang885a5fb2010-01-12 05:38:31 +0800524 <= link_avail) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700525 dp_priv->link_bw = bws[clock];
526 dp_priv->lane_count = lane_count;
527 adjusted_mode->clock = intel_dp_link_clock(dp_priv->link_bw);
Zhao Yakui28c97732009-10-09 11:39:41 +0800528 DRM_DEBUG_KMS("Display port link bw %02x lane "
529 "count %d clock %d\n",
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700530 dp_priv->link_bw, dp_priv->lane_count,
531 adjusted_mode->clock);
532 return true;
533 }
534 }
535 }
Dave Airliefe27d532010-06-30 11:46:17 +1000536
537 if (IS_eDP(intel_encoder)) {
538 /* okay we failed just pick the highest */
539 dp_priv->lane_count = max_lane_count;
540 dp_priv->link_bw = bws[max_clock];
541 adjusted_mode->clock = intel_dp_link_clock(dp_priv->link_bw);
542 DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
543 "count %d clock %d\n",
544 dp_priv->link_bw, dp_priv->lane_count,
545 adjusted_mode->clock);
546 return true;
547 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700548 return false;
549}
550
551struct intel_dp_m_n {
552 uint32_t tu;
553 uint32_t gmch_m;
554 uint32_t gmch_n;
555 uint32_t link_m;
556 uint32_t link_n;
557};
558
559static void
560intel_reduce_ratio(uint32_t *num, uint32_t *den)
561{
562 while (*num > 0xffffff || *den > 0xffffff) {
563 *num >>= 1;
564 *den >>= 1;
565 }
566}
567
568static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800569intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700570 int nlanes,
571 int pixel_clock,
572 int link_clock,
573 struct intel_dp_m_n *m_n)
574{
575 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800576 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700577 m_n->gmch_n = link_clock * nlanes;
578 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
579 m_n->link_m = pixel_clock;
580 m_n->link_n = link_clock;
581 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
582}
583
Zhao Yakui36e83a12010-06-12 14:32:21 +0800584bool intel_pch_has_edp(struct drm_crtc *crtc)
585{
586 struct drm_device *dev = crtc->dev;
587 struct drm_mode_config *mode_config = &dev->mode_config;
588 struct drm_encoder *encoder;
589
590 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
591 struct intel_encoder *intel_encoder;
592 struct intel_dp_priv *dp_priv;
593
594 if (!encoder || encoder->crtc != crtc)
595 continue;
596
597 intel_encoder = enc_to_intel_encoder(encoder);
598 dp_priv = intel_encoder->dev_priv;
599
600 if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT)
601 return dp_priv->has_edp;
602 }
603 return false;
604}
605
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700606void
607intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
608 struct drm_display_mode *adjusted_mode)
609{
610 struct drm_device *dev = crtc->dev;
611 struct drm_mode_config *mode_config = &dev->mode_config;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800612 struct drm_encoder *encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700613 struct drm_i915_private *dev_priv = dev->dev_private;
614 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Zhao Yakui36e83a12010-06-12 14:32:21 +0800615 int lane_count = 4, bpp = 24;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700616 struct intel_dp_m_n m_n;
617
618 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700619 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700620 */
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800621 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
622 struct intel_encoder *intel_encoder;
623 struct intel_dp_priv *dp_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700624
Dan Carpenterd8201ab2010-05-07 10:39:00 +0200625 if (encoder->crtc != crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700626 continue;
627
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800628 intel_encoder = enc_to_intel_encoder(encoder);
629 dp_priv = intel_encoder->dev_priv;
630
Eric Anholt21d40d32010-03-25 11:11:14 -0700631 if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700632 lane_count = dp_priv->lane_count;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800633 if (IS_PCH_eDP(dp_priv))
634 bpp = dev_priv->edp_bpp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700635 break;
636 }
637 }
638
639 /*
640 * Compute the GMCH and Link ratios. The '3' here is
641 * the number of bytes_per_pixel post-LUT, which we always
642 * set up for 8-bits of R/G/B, or 3 bytes total.
643 */
Zhao Yakui36e83a12010-06-12 14:32:21 +0800644 intel_dp_compute_m_n(bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700645 mode->clock, adjusted_mode->clock, &m_n);
646
Eric Anholtc619eed2010-01-28 16:45:52 -0800647 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800648 if (intel_crtc->pipe == 0) {
649 I915_WRITE(TRANSA_DATA_M1,
650 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
651 m_n.gmch_m);
652 I915_WRITE(TRANSA_DATA_N1, m_n.gmch_n);
653 I915_WRITE(TRANSA_DP_LINK_M1, m_n.link_m);
654 I915_WRITE(TRANSA_DP_LINK_N1, m_n.link_n);
655 } else {
656 I915_WRITE(TRANSB_DATA_M1,
657 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
658 m_n.gmch_m);
659 I915_WRITE(TRANSB_DATA_N1, m_n.gmch_n);
660 I915_WRITE(TRANSB_DP_LINK_M1, m_n.link_m);
661 I915_WRITE(TRANSB_DP_LINK_N1, m_n.link_n);
662 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700663 } else {
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800664 if (intel_crtc->pipe == 0) {
665 I915_WRITE(PIPEA_GMCH_DATA_M,
666 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
667 m_n.gmch_m);
668 I915_WRITE(PIPEA_GMCH_DATA_N,
669 m_n.gmch_n);
670 I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
671 I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
672 } else {
673 I915_WRITE(PIPEB_GMCH_DATA_M,
674 ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
675 m_n.gmch_m);
676 I915_WRITE(PIPEB_GMCH_DATA_N,
677 m_n.gmch_n);
678 I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
679 I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
680 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700681 }
682}
683
684static void
685intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
686 struct drm_display_mode *adjusted_mode)
687{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800688 struct drm_device *dev = encoder->dev;
Eric Anholt21d40d32010-03-25 11:11:14 -0700689 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
690 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
691 struct drm_crtc *crtc = intel_encoder->enc.crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700692 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
693
Zhenyu Wange3421a12010-04-08 09:43:27 +0800694 dp_priv->DP = (DP_VOLTAGE_0_4 |
Adam Jackson9c9e7922010-04-05 17:57:59 -0400695 DP_PRE_EMPHASIS_0);
696
697 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
698 dp_priv->DP |= DP_SYNC_HS_HIGH;
699 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
700 dp_priv->DP |= DP_SYNC_VS_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700701
Zhenyu Wange3421a12010-04-08 09:43:27 +0800702 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_encoder))
703 dp_priv->DP |= DP_LINK_TRAIN_OFF_CPT;
704 else
705 dp_priv->DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700706
707 switch (dp_priv->lane_count) {
708 case 1:
709 dp_priv->DP |= DP_PORT_WIDTH_1;
710 break;
711 case 2:
712 dp_priv->DP |= DP_PORT_WIDTH_2;
713 break;
714 case 4:
715 dp_priv->DP |= DP_PORT_WIDTH_4;
716 break;
717 }
718 if (dp_priv->has_audio)
719 dp_priv->DP |= DP_AUDIO_OUTPUT_ENABLE;
720
721 memset(dp_priv->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
722 dp_priv->link_configuration[0] = dp_priv->link_bw;
723 dp_priv->link_configuration[1] = dp_priv->lane_count;
724
725 /*
Adam Jackson9962c922010-05-13 14:45:42 -0400726 * Check for DPCD version > 1.1 and enhanced framing support
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700727 */
Adam Jackson9962c922010-05-13 14:45:42 -0400728 if (dp_priv->dpcd[0] >= 0x11 && (dp_priv->dpcd[2] & DP_ENHANCED_FRAME_CAP)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700729 dp_priv->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
730 dp_priv->DP |= DP_ENHANCED_FRAMING;
731 }
732
Zhenyu Wange3421a12010-04-08 09:43:27 +0800733 /* CPT DP's pipe select is decided in TRANS_DP_CTL */
734 if (intel_crtc->pipe == 1 && !HAS_PCH_CPT(dev))
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700735 dp_priv->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800736
Eric Anholt21d40d32010-03-25 11:11:14 -0700737 if (IS_eDP(intel_encoder)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800738 /* don't miss out required setting for eDP */
739 dp_priv->DP |= DP_PLL_ENABLE;
740 if (adjusted_mode->clock < 200000)
741 dp_priv->DP |= DP_PLL_FREQ_160MHZ;
742 else
743 dp_priv->DP |= DP_PLL_FREQ_270MHZ;
744 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700745}
746
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500747static void ironlake_edp_backlight_on (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800748{
749 struct drm_i915_private *dev_priv = dev->dev_private;
750 u32 pp;
751
Zhao Yakui28c97732009-10-09 11:39:41 +0800752 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800753 pp = I915_READ(PCH_PP_CONTROL);
754 pp |= EDP_BLC_ENABLE;
755 I915_WRITE(PCH_PP_CONTROL, pp);
756}
757
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500758static void ironlake_edp_backlight_off (struct drm_device *dev)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800759{
760 struct drm_i915_private *dev_priv = dev->dev_private;
761 u32 pp;
762
Zhao Yakui28c97732009-10-09 11:39:41 +0800763 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800764 pp = I915_READ(PCH_PP_CONTROL);
765 pp &= ~EDP_BLC_ENABLE;
766 I915_WRITE(PCH_PP_CONTROL, pp);
767}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700768
769static void
770intel_dp_dpms(struct drm_encoder *encoder, int mode)
771{
Eric Anholt21d40d32010-03-25 11:11:14 -0700772 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
773 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800774 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700775 struct drm_i915_private *dev_priv = dev->dev_private;
776 uint32_t dp_reg = I915_READ(dp_priv->output_reg);
777
778 if (mode != DRM_MODE_DPMS_ON) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800779 if (dp_reg & DP_PORT_EN) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700780 intel_dp_link_down(intel_encoder, dp_priv->DP);
Zhao Yakui36e83a12010-06-12 14:32:21 +0800781 if (IS_eDP(intel_encoder) || IS_PCH_eDP(dp_priv))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500782 ironlake_edp_backlight_off(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800783 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700784 } else {
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800785 if (!(dp_reg & DP_PORT_EN)) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700786 intel_dp_link_train(intel_encoder, dp_priv->DP, dp_priv->link_configuration);
Zhao Yakui36e83a12010-06-12 14:32:21 +0800787 if (IS_eDP(intel_encoder) || IS_PCH_eDP(dp_priv))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500788 ironlake_edp_backlight_on(dev);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800789 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700790 }
Keith Packardc8110e52009-05-06 11:51:10 -0700791 dp_priv->dpms_mode = mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700792}
793
794/*
795 * Fetch AUX CH registers 0x202 - 0x207 which contain
796 * link status information
797 */
798static bool
Eric Anholt21d40d32010-03-25 11:11:14 -0700799intel_dp_get_link_status(struct intel_encoder *intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700800 uint8_t link_status[DP_LINK_STATUS_SIZE])
801{
802 int ret;
803
Eric Anholt21d40d32010-03-25 11:11:14 -0700804 ret = intel_dp_aux_native_read(intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700805 DP_LANE0_1_STATUS,
806 link_status, DP_LINK_STATUS_SIZE);
807 if (ret != DP_LINK_STATUS_SIZE)
808 return false;
809 return true;
810}
811
812static uint8_t
813intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
814 int r)
815{
816 return link_status[r - DP_LANE0_1_STATUS];
817}
818
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700819static uint8_t
820intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
821 int lane)
822{
823 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
824 int s = ((lane & 1) ?
825 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
826 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
827 uint8_t l = intel_dp_link_status(link_status, i);
828
829 return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
830}
831
832static uint8_t
833intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
834 int lane)
835{
836 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
837 int s = ((lane & 1) ?
838 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
839 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
840 uint8_t l = intel_dp_link_status(link_status, i);
841
842 return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
843}
844
845
846#if 0
847static char *voltage_names[] = {
848 "0.4V", "0.6V", "0.8V", "1.2V"
849};
850static char *pre_emph_names[] = {
851 "0dB", "3.5dB", "6dB", "9.5dB"
852};
853static char *link_train_names[] = {
854 "pattern 1", "pattern 2", "idle", "off"
855};
856#endif
857
858/*
859 * These are source-specific values; current Intel hardware supports
860 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
861 */
862#define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
863
864static uint8_t
865intel_dp_pre_emphasis_max(uint8_t voltage_swing)
866{
867 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
868 case DP_TRAIN_VOLTAGE_SWING_400:
869 return DP_TRAIN_PRE_EMPHASIS_6;
870 case DP_TRAIN_VOLTAGE_SWING_600:
871 return DP_TRAIN_PRE_EMPHASIS_6;
872 case DP_TRAIN_VOLTAGE_SWING_800:
873 return DP_TRAIN_PRE_EMPHASIS_3_5;
874 case DP_TRAIN_VOLTAGE_SWING_1200:
875 default:
876 return DP_TRAIN_PRE_EMPHASIS_0;
877 }
878}
879
880static void
Eric Anholt21d40d32010-03-25 11:11:14 -0700881intel_get_adjust_train(struct intel_encoder *intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700882 uint8_t link_status[DP_LINK_STATUS_SIZE],
883 int lane_count,
884 uint8_t train_set[4])
885{
886 uint8_t v = 0;
887 uint8_t p = 0;
888 int lane;
889
890 for (lane = 0; lane < lane_count; lane++) {
891 uint8_t this_v = intel_get_adjust_request_voltage(link_status, lane);
892 uint8_t this_p = intel_get_adjust_request_pre_emphasis(link_status, lane);
893
894 if (this_v > v)
895 v = this_v;
896 if (this_p > p)
897 p = this_p;
898 }
899
900 if (v >= I830_DP_VOLTAGE_MAX)
901 v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
902
903 if (p >= intel_dp_pre_emphasis_max(v))
904 p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
905
906 for (lane = 0; lane < 4; lane++)
907 train_set[lane] = v | p;
908}
909
910static uint32_t
911intel_dp_signal_levels(uint8_t train_set, int lane_count)
912{
913 uint32_t signal_levels = 0;
914
915 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
916 case DP_TRAIN_VOLTAGE_SWING_400:
917 default:
918 signal_levels |= DP_VOLTAGE_0_4;
919 break;
920 case DP_TRAIN_VOLTAGE_SWING_600:
921 signal_levels |= DP_VOLTAGE_0_6;
922 break;
923 case DP_TRAIN_VOLTAGE_SWING_800:
924 signal_levels |= DP_VOLTAGE_0_8;
925 break;
926 case DP_TRAIN_VOLTAGE_SWING_1200:
927 signal_levels |= DP_VOLTAGE_1_2;
928 break;
929 }
930 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
931 case DP_TRAIN_PRE_EMPHASIS_0:
932 default:
933 signal_levels |= DP_PRE_EMPHASIS_0;
934 break;
935 case DP_TRAIN_PRE_EMPHASIS_3_5:
936 signal_levels |= DP_PRE_EMPHASIS_3_5;
937 break;
938 case DP_TRAIN_PRE_EMPHASIS_6:
939 signal_levels |= DP_PRE_EMPHASIS_6;
940 break;
941 case DP_TRAIN_PRE_EMPHASIS_9_5:
942 signal_levels |= DP_PRE_EMPHASIS_9_5;
943 break;
944 }
945 return signal_levels;
946}
947
Zhenyu Wange3421a12010-04-08 09:43:27 +0800948/* Gen6's DP voltage swing and pre-emphasis control */
949static uint32_t
950intel_gen6_edp_signal_levels(uint8_t train_set)
951{
952 switch (train_set & (DP_TRAIN_VOLTAGE_SWING_MASK|DP_TRAIN_PRE_EMPHASIS_MASK)) {
953 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
954 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
955 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
956 return EDP_LINK_TRAIN_400MV_6DB_SNB_B;
957 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
958 return EDP_LINK_TRAIN_600MV_3_5DB_SNB_B;
959 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
960 return EDP_LINK_TRAIN_800MV_0DB_SNB_B;
961 default:
962 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level\n");
963 return EDP_LINK_TRAIN_400MV_0DB_SNB_B;
964 }
965}
966
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700967static uint8_t
968intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
969 int lane)
970{
971 int i = DP_LANE0_1_STATUS + (lane >> 1);
972 int s = (lane & 1) * 4;
973 uint8_t l = intel_dp_link_status(link_status, i);
974
975 return (l >> s) & 0xf;
976}
977
978/* Check for clock recovery is done on all channels */
979static bool
980intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
981{
982 int lane;
983 uint8_t lane_status;
984
985 for (lane = 0; lane < lane_count; lane++) {
986 lane_status = intel_get_lane_status(link_status, lane);
987 if ((lane_status & DP_LANE_CR_DONE) == 0)
988 return false;
989 }
990 return true;
991}
992
993/* Check to see if channel eq is done on all channels */
994#define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
995 DP_LANE_CHANNEL_EQ_DONE|\
996 DP_LANE_SYMBOL_LOCKED)
997static bool
998intel_channel_eq_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
999{
1000 uint8_t lane_align;
1001 uint8_t lane_status;
1002 int lane;
1003
1004 lane_align = intel_dp_link_status(link_status,
1005 DP_LANE_ALIGN_STATUS_UPDATED);
1006 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
1007 return false;
1008 for (lane = 0; lane < lane_count; lane++) {
1009 lane_status = intel_get_lane_status(link_status, lane);
1010 if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
1011 return false;
1012 }
1013 return true;
1014}
1015
1016static bool
Eric Anholt21d40d32010-03-25 11:11:14 -07001017intel_dp_set_link_train(struct intel_encoder *intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001018 uint32_t dp_reg_value,
1019 uint8_t dp_train_pat,
1020 uint8_t train_set[4],
1021 bool first)
1022{
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001023 struct drm_device *dev = intel_encoder->enc.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001024 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -07001025 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001026 int ret;
1027
1028 I915_WRITE(dp_priv->output_reg, dp_reg_value);
1029 POSTING_READ(dp_priv->output_reg);
1030 if (first)
1031 intel_wait_for_vblank(dev);
1032
Eric Anholt21d40d32010-03-25 11:11:14 -07001033 intel_dp_aux_native_write_1(intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001034 DP_TRAINING_PATTERN_SET,
1035 dp_train_pat);
1036
Eric Anholt21d40d32010-03-25 11:11:14 -07001037 ret = intel_dp_aux_native_write(intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001038 DP_TRAINING_LANE0_SET, train_set, 4);
1039 if (ret != 4)
1040 return false;
1041
1042 return true;
1043}
1044
1045static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001046intel_dp_link_train(struct intel_encoder *intel_encoder, uint32_t DP,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001047 uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE])
1048{
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001049 struct drm_device *dev = intel_encoder->enc.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001050 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -07001051 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001052 uint8_t train_set[4];
1053 uint8_t link_status[DP_LINK_STATUS_SIZE];
1054 int i;
1055 uint8_t voltage;
1056 bool clock_recovery = false;
1057 bool channel_eq = false;
1058 bool first = true;
1059 int tries;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001060 u32 reg;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001061
1062 /* Write the link configuration data */
Adam Jacksonab00a9e2010-04-05 17:58:00 -04001063 intel_dp_aux_native_write(intel_encoder, DP_LINK_BW_SET,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001064 link_configuration, DP_LINK_CONFIGURATION_SIZE);
1065
1066 DP |= DP_PORT_EN;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001067 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_encoder))
1068 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1069 else
1070 DP &= ~DP_LINK_TRAIN_MASK;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001071 memset(train_set, 0, 4);
1072 voltage = 0xff;
1073 tries = 0;
1074 clock_recovery = false;
1075 for (;;) {
1076 /* Use train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001077 uint32_t signal_levels;
1078 if (IS_GEN6(dev) && IS_eDP(intel_encoder)) {
1079 signal_levels = intel_gen6_edp_signal_levels(train_set[0]);
1080 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1081 } else {
1082 signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
1083 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1084 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001085
Zhenyu Wange3421a12010-04-08 09:43:27 +08001086 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_encoder))
1087 reg = DP | DP_LINK_TRAIN_PAT_1_CPT;
1088 else
1089 reg = DP | DP_LINK_TRAIN_PAT_1;
1090
1091 if (!intel_dp_set_link_train(intel_encoder, reg,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001092 DP_TRAINING_PATTERN_1, train_set, first))
1093 break;
1094 first = false;
1095 /* Set training pattern 1 */
1096
1097 udelay(100);
Eric Anholt21d40d32010-03-25 11:11:14 -07001098 if (!intel_dp_get_link_status(intel_encoder, link_status))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001099 break;
1100
1101 if (intel_clock_recovery_ok(link_status, dp_priv->lane_count)) {
1102 clock_recovery = true;
1103 break;
1104 }
1105
1106 /* Check to see if we've tried the max voltage */
1107 for (i = 0; i < dp_priv->lane_count; i++)
1108 if ((train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1109 break;
1110 if (i == dp_priv->lane_count)
1111 break;
1112
1113 /* Check to see if we've tried the same voltage 5 times */
1114 if ((train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
1115 ++tries;
1116 if (tries == 5)
1117 break;
1118 } else
1119 tries = 0;
1120 voltage = train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1121
1122 /* Compute new train_set as requested by target */
Eric Anholt21d40d32010-03-25 11:11:14 -07001123 intel_get_adjust_train(intel_encoder, link_status, dp_priv->lane_count, train_set);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001124 }
1125
1126 /* channel equalization */
1127 tries = 0;
1128 channel_eq = false;
1129 for (;;) {
1130 /* Use train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001131 uint32_t signal_levels;
1132
1133 if (IS_GEN6(dev) && IS_eDP(intel_encoder)) {
1134 signal_levels = intel_gen6_edp_signal_levels(train_set[0]);
1135 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1136 } else {
1137 signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
1138 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1139 }
1140
1141 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_encoder))
1142 reg = DP | DP_LINK_TRAIN_PAT_2_CPT;
1143 else
1144 reg = DP | DP_LINK_TRAIN_PAT_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001145
1146 /* channel eq pattern */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001147 if (!intel_dp_set_link_train(intel_encoder, reg,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001148 DP_TRAINING_PATTERN_2, train_set,
1149 false))
1150 break;
1151
1152 udelay(400);
Eric Anholt21d40d32010-03-25 11:11:14 -07001153 if (!intel_dp_get_link_status(intel_encoder, link_status))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001154 break;
1155
1156 if (intel_channel_eq_ok(link_status, dp_priv->lane_count)) {
1157 channel_eq = true;
1158 break;
1159 }
1160
1161 /* Try 5 times */
1162 if (tries > 5)
1163 break;
1164
1165 /* Compute new train_set as requested by target */
Eric Anholt21d40d32010-03-25 11:11:14 -07001166 intel_get_adjust_train(intel_encoder, link_status, dp_priv->lane_count, train_set);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001167 ++tries;
1168 }
1169
Zhenyu Wange3421a12010-04-08 09:43:27 +08001170 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_encoder))
1171 reg = DP | DP_LINK_TRAIN_OFF_CPT;
1172 else
1173 reg = DP | DP_LINK_TRAIN_OFF;
1174
1175 I915_WRITE(dp_priv->output_reg, reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001176 POSTING_READ(dp_priv->output_reg);
Eric Anholt21d40d32010-03-25 11:11:14 -07001177 intel_dp_aux_native_write_1(intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001178 DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
1179}
1180
1181static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001182intel_dp_link_down(struct intel_encoder *intel_encoder, uint32_t DP)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001183{
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001184 struct drm_device *dev = intel_encoder->enc.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001185 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -07001186 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001187
Zhao Yakui28c97732009-10-09 11:39:41 +08001188 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001189
Eric Anholt21d40d32010-03-25 11:11:14 -07001190 if (IS_eDP(intel_encoder)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001191 DP &= ~DP_PLL_ENABLE;
1192 I915_WRITE(dp_priv->output_reg, DP);
1193 POSTING_READ(dp_priv->output_reg);
1194 udelay(100);
1195 }
1196
Zhenyu Wange3421a12010-04-08 09:43:27 +08001197 if (HAS_PCH_CPT(dev) && !IS_eDP(intel_encoder)) {
1198 DP &= ~DP_LINK_TRAIN_MASK_CPT;
1199 I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
1200 POSTING_READ(dp_priv->output_reg);
1201 } else {
1202 DP &= ~DP_LINK_TRAIN_MASK;
1203 I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
1204 POSTING_READ(dp_priv->output_reg);
1205 }
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001206
1207 udelay(17000);
1208
Eric Anholt21d40d32010-03-25 11:11:14 -07001209 if (IS_eDP(intel_encoder))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001210 DP |= DP_LINK_TRAIN_OFF;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001211 I915_WRITE(dp_priv->output_reg, DP & ~DP_PORT_EN);
1212 POSTING_READ(dp_priv->output_reg);
1213}
1214
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001215/*
1216 * According to DP spec
1217 * 5.1.2:
1218 * 1. Read DPCD
1219 * 2. Configure link according to Receiver Capabilities
1220 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
1221 * 4. Check link status on receipt of hot-plug interrupt
1222 */
1223
1224static void
Eric Anholt21d40d32010-03-25 11:11:14 -07001225intel_dp_check_link_status(struct intel_encoder *intel_encoder)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001226{
Eric Anholt21d40d32010-03-25 11:11:14 -07001227 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001228 uint8_t link_status[DP_LINK_STATUS_SIZE];
1229
Eric Anholt21d40d32010-03-25 11:11:14 -07001230 if (!intel_encoder->enc.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001231 return;
1232
Eric Anholt21d40d32010-03-25 11:11:14 -07001233 if (!intel_dp_get_link_status(intel_encoder, link_status)) {
1234 intel_dp_link_down(intel_encoder, dp_priv->DP);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001235 return;
1236 }
1237
1238 if (!intel_channel_eq_ok(link_status, dp_priv->lane_count))
Eric Anholt21d40d32010-03-25 11:11:14 -07001239 intel_dp_link_train(intel_encoder, dp_priv->DP, dp_priv->link_configuration);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001240}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001241
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001242static enum drm_connector_status
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001243ironlake_dp_detect(struct drm_connector *connector)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001244{
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001245 struct drm_encoder *encoder = intel_attached_encoder(connector);
1246 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -07001247 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001248 enum drm_connector_status status;
1249
1250 status = connector_status_disconnected;
Eric Anholt21d40d32010-03-25 11:11:14 -07001251 if (intel_dp_aux_native_read(intel_encoder,
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001252 0x000, dp_priv->dpcd,
1253 sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
1254 {
1255 if (dp_priv->dpcd[0] != 0)
1256 status = connector_status_connected;
1257 }
Adam Jacksona7de64e2010-05-13 14:45:43 -04001258 DRM_DEBUG_KMS("DPCD: %hx%hx%hx%hx\n", dp_priv->dpcd[0],
1259 dp_priv->dpcd[1], dp_priv->dpcd[2], dp_priv->dpcd[3]);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001260 return status;
1261}
1262
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001263/**
1264 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
1265 *
1266 * \return true if DP port is connected.
1267 * \return false if DP port is disconnected.
1268 */
1269static enum drm_connector_status
1270intel_dp_detect(struct drm_connector *connector)
1271{
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001272 struct drm_encoder *encoder = intel_attached_encoder(connector);
1273 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
1274 struct drm_device *dev = intel_encoder->enc.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001275 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -07001276 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001277 uint32_t temp, bit;
1278 enum drm_connector_status status;
1279
1280 dp_priv->has_audio = false;
1281
Eric Anholtc619eed2010-01-28 16:45:52 -08001282 if (HAS_PCH_SPLIT(dev))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001283 return ironlake_dp_detect(connector);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001284
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001285 switch (dp_priv->output_reg) {
1286 case DP_B:
1287 bit = DPB_HOTPLUG_INT_STATUS;
1288 break;
1289 case DP_C:
1290 bit = DPC_HOTPLUG_INT_STATUS;
1291 break;
1292 case DP_D:
1293 bit = DPD_HOTPLUG_INT_STATUS;
1294 break;
1295 default:
1296 return connector_status_unknown;
1297 }
1298
1299 temp = I915_READ(PORT_HOTPLUG_STAT);
1300
1301 if ((temp & bit) == 0)
1302 return connector_status_disconnected;
1303
1304 status = connector_status_disconnected;
Eric Anholt21d40d32010-03-25 11:11:14 -07001305 if (intel_dp_aux_native_read(intel_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001306 0x000, dp_priv->dpcd,
1307 sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
1308 {
1309 if (dp_priv->dpcd[0] != 0)
1310 status = connector_status_connected;
1311 }
1312 return status;
1313}
1314
1315static int intel_dp_get_modes(struct drm_connector *connector)
1316{
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001317 struct drm_encoder *encoder = intel_attached_encoder(connector);
1318 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
1319 struct drm_device *dev = intel_encoder->enc.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001320 struct drm_i915_private *dev_priv = dev->dev_private;
Zhao Yakui36e83a12010-06-12 14:32:21 +08001321 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001322 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001323
1324 /* We should parse the EDID data and find out if it has an audio sink
1325 */
1326
Zhenyu Wang335af9a2010-03-30 14:39:31 +08001327 ret = intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001328 if (ret)
1329 return ret;
1330
1331 /* if eDP has no EDID, try to use fixed panel mode from VBT */
Zhao Yakui36e83a12010-06-12 14:32:21 +08001332 if (IS_eDP(intel_encoder) || IS_PCH_eDP(dp_priv)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001333 if (dev_priv->panel_fixed_mode != NULL) {
1334 struct drm_display_mode *mode;
1335 mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
1336 drm_mode_probed_add(connector, mode);
1337 return 1;
1338 }
1339 }
1340 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001341}
1342
1343static void
1344intel_dp_destroy (struct drm_connector *connector)
1345{
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001346 drm_sysfs_connector_remove(connector);
1347 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001348 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001349}
1350
1351static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
1352 .dpms = intel_dp_dpms,
1353 .mode_fixup = intel_dp_mode_fixup,
1354 .prepare = intel_encoder_prepare,
1355 .mode_set = intel_dp_mode_set,
1356 .commit = intel_encoder_commit,
1357};
1358
1359static const struct drm_connector_funcs intel_dp_connector_funcs = {
1360 .dpms = drm_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001361 .detect = intel_dp_detect,
1362 .fill_modes = drm_helper_probe_single_connector_modes,
1363 .destroy = intel_dp_destroy,
1364};
1365
1366static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
1367 .get_modes = intel_dp_get_modes,
1368 .mode_valid = intel_dp_mode_valid,
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001369 .best_encoder = intel_attached_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001370};
1371
1372static void intel_dp_enc_destroy(struct drm_encoder *encoder)
1373{
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001374 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
1375
1376 if (intel_encoder->i2c_bus)
1377 intel_i2c_destroy(intel_encoder->i2c_bus);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001378 drm_encoder_cleanup(encoder);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001379 kfree(intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001380}
1381
1382static const struct drm_encoder_funcs intel_dp_enc_funcs = {
1383 .destroy = intel_dp_enc_destroy,
1384};
1385
1386void
Eric Anholt21d40d32010-03-25 11:11:14 -07001387intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07001388{
Eric Anholt21d40d32010-03-25 11:11:14 -07001389 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
Keith Packardc8110e52009-05-06 11:51:10 -07001390
1391 if (dp_priv->dpms_mode == DRM_MODE_DPMS_ON)
Eric Anholt21d40d32010-03-25 11:11:14 -07001392 intel_dp_check_link_status(intel_encoder);
Keith Packardc8110e52009-05-06 11:51:10 -07001393}
1394
Zhenyu Wange3421a12010-04-08 09:43:27 +08001395/* Return which DP Port should be selected for Transcoder DP control */
1396int
1397intel_trans_dp_port_sel (struct drm_crtc *crtc)
1398{
1399 struct drm_device *dev = crtc->dev;
1400 struct drm_mode_config *mode_config = &dev->mode_config;
1401 struct drm_encoder *encoder;
1402 struct intel_encoder *intel_encoder = NULL;
1403
1404 list_for_each_entry(encoder, &mode_config->encoder_list, head) {
Dan Carpenterd8201ab2010-05-07 10:39:00 +02001405 if (encoder->crtc != crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08001406 continue;
1407
1408 intel_encoder = enc_to_intel_encoder(encoder);
1409 if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT) {
1410 struct intel_dp_priv *dp_priv = intel_encoder->dev_priv;
1411 return dp_priv->output_reg;
1412 }
1413 }
1414 return -1;
1415}
1416
Zhao Yakui36e83a12010-06-12 14:32:21 +08001417/* check the VBT to see whether the eDP is on DP-D port */
1418static bool intel_dpd_is_edp(struct drm_device *dev)
1419{
1420 struct drm_i915_private *dev_priv = dev->dev_private;
1421 struct child_device_config *p_child;
1422 int i;
1423
1424 if (!dev_priv->child_dev_num)
1425 return false;
1426
1427 for (i = 0; i < dev_priv->child_dev_num; i++) {
1428 p_child = dev_priv->child_dev + i;
1429
1430 if (p_child->dvo_port == PORT_IDPD &&
1431 p_child->device_type == DEVICE_TYPE_eDP)
1432 return true;
1433 }
1434 return false;
1435}
1436
Keith Packardc8110e52009-05-06 11:51:10 -07001437void
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001438intel_dp_init(struct drm_device *dev, int output_reg)
1439{
1440 struct drm_i915_private *dev_priv = dev->dev_private;
1441 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -07001442 struct intel_encoder *intel_encoder;
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001443 struct intel_connector *intel_connector;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001444 struct intel_dp_priv *dp_priv;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001445 const char *name = NULL;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001446
Eric Anholt21d40d32010-03-25 11:11:14 -07001447 intel_encoder = kcalloc(sizeof(struct intel_encoder) +
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001448 sizeof(struct intel_dp_priv), 1, GFP_KERNEL);
Eric Anholt21d40d32010-03-25 11:11:14 -07001449 if (!intel_encoder)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001450 return;
1451
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001452 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
1453 if (!intel_connector) {
1454 kfree(intel_encoder);
1455 return;
1456 }
1457
Eric Anholt21d40d32010-03-25 11:11:14 -07001458 dp_priv = (struct intel_dp_priv *)(intel_encoder + 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001459
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001460 connector = &intel_connector->base;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001461 drm_connector_init(dev, connector, &intel_dp_connector_funcs,
1462 DRM_MODE_CONNECTOR_DisplayPort);
1463 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
1464
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001465 connector->polled = DRM_CONNECTOR_POLL_HPD;
1466
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001467 if (output_reg == DP_A)
Eric Anholt21d40d32010-03-25 11:11:14 -07001468 intel_encoder->type = INTEL_OUTPUT_EDP;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001469 else
Eric Anholt21d40d32010-03-25 11:11:14 -07001470 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001471
Zhao Yakui652af9d2009-12-02 10:03:33 +08001472 if (output_reg == DP_B || output_reg == PCH_DP_B)
Eric Anholt21d40d32010-03-25 11:11:14 -07001473 intel_encoder->clone_mask = (1 << INTEL_DP_B_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001474 else if (output_reg == DP_C || output_reg == PCH_DP_C)
Eric Anholt21d40d32010-03-25 11:11:14 -07001475 intel_encoder->clone_mask = (1 << INTEL_DP_C_CLONE_BIT);
Zhao Yakui652af9d2009-12-02 10:03:33 +08001476 else if (output_reg == DP_D || output_reg == PCH_DP_D)
Eric Anholt21d40d32010-03-25 11:11:14 -07001477 intel_encoder->clone_mask = (1 << INTEL_DP_D_CLONE_BIT);
Ma Lingf8aed702009-08-24 13:50:24 +08001478
Eric Anholt21d40d32010-03-25 11:11:14 -07001479 if (IS_eDP(intel_encoder))
1480 intel_encoder->clone_mask = (1 << INTEL_EDP_CLONE_BIT);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08001481
Zhao Yakui36e83a12010-06-12 14:32:21 +08001482 if (HAS_PCH_SPLIT(dev) && (output_reg == PCH_DP_D)) {
1483 if (intel_dpd_is_edp(dev))
1484 dp_priv->has_edp = true;
1485 }
1486
Eric Anholt21d40d32010-03-25 11:11:14 -07001487 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001488 connector->interlace_allowed = true;
1489 connector->doublescan_allowed = 0;
1490
Eric Anholt21d40d32010-03-25 11:11:14 -07001491 dp_priv->intel_encoder = intel_encoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001492 dp_priv->output_reg = output_reg;
1493 dp_priv->has_audio = false;
Keith Packardc8110e52009-05-06 11:51:10 -07001494 dp_priv->dpms_mode = DRM_MODE_DPMS_ON;
Eric Anholt21d40d32010-03-25 11:11:14 -07001495 intel_encoder->dev_priv = dp_priv;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001496
Eric Anholt21d40d32010-03-25 11:11:14 -07001497 drm_encoder_init(dev, &intel_encoder->enc, &intel_dp_enc_funcs,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001498 DRM_MODE_ENCODER_TMDS);
Eric Anholt21d40d32010-03-25 11:11:14 -07001499 drm_encoder_helper_add(&intel_encoder->enc, &intel_dp_helper_funcs);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001500
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001501 drm_mode_connector_attach_encoder(&intel_connector->base,
Eric Anholt21d40d32010-03-25 11:11:14 -07001502 &intel_encoder->enc);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001503 drm_sysfs_connector_add(connector);
1504
1505 /* Set up the DDC bus. */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001506 switch (output_reg) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001507 case DP_A:
1508 name = "DPDDC-A";
1509 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001510 case DP_B:
1511 case PCH_DP_B:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001512 dev_priv->hotplug_supported_mask |=
1513 HDMIB_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001514 name = "DPDDC-B";
1515 break;
1516 case DP_C:
1517 case PCH_DP_C:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001518 dev_priv->hotplug_supported_mask |=
1519 HDMIC_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001520 name = "DPDDC-C";
1521 break;
1522 case DP_D:
1523 case PCH_DP_D:
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001524 dev_priv->hotplug_supported_mask |=
1525 HDMID_HOTPLUG_INT_STATUS;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08001526 name = "DPDDC-D";
1527 break;
1528 }
1529
Zhenyu Wang55f78c42010-03-29 16:13:57 +08001530 intel_dp_i2c_init(intel_encoder, intel_connector, name);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001531
Eric Anholt21d40d32010-03-25 11:11:14 -07001532 intel_encoder->ddc_bus = &dp_priv->adapter;
1533 intel_encoder->hot_plug = intel_dp_hot_plug;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001534
Zhao Yakui36e83a12010-06-12 14:32:21 +08001535 if (output_reg == DP_A || IS_PCH_eDP(dp_priv)) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001536 /* initialize panel mode from VBT if available for eDP */
1537 if (dev_priv->lfp_lvds_vbt_mode) {
1538 dev_priv->panel_fixed_mode =
1539 drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1540 if (dev_priv->panel_fixed_mode) {
1541 dev_priv->panel_fixed_mode->type |=
1542 DRM_MODE_TYPE_PREFERRED;
1543 }
1544 }
1545 }
1546
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001547 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1548 * 0xd. Failure to do so will result in spurious interrupts being
1549 * generated on the port when a cable is not attached.
1550 */
1551 if (IS_G4X(dev) && !IS_GM45(dev)) {
1552 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
1553 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
1554 }
1555}