blob: 6912d29f46f3c03c1f7bb61cba413ceae44b28c1 [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>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040030#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
32#include <drm/drm_crtc.h>
33#include <drm/drm_crtc_helper.h>
34#include <drm/drm_edid.h>
Keith Packarda4fc5ed2009-04-07 16:16:42 -070035#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/i915_drm.h>
Keith Packarda4fc5ed2009-04-07 16:16:42 -070037#include "i915_drv.h"
Keith Packarda4fc5ed2009-04-07 16:16:42 -070038
Keith Packarda4fc5ed2009-04-07 16:16:42 -070039#define DP_LINK_CHECK_TIMEOUT (10 * 1000)
40
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070041/**
42 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
43 * @intel_dp: DP struct
44 *
45 * If a CPU or PCH DP output is attached to an eDP panel, this function
46 * will return true, and false otherwise.
47 */
48static bool is_edp(struct intel_dp *intel_dp)
49{
Paulo Zanonida63a9f2012-10-26 19:05:46 -020050 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
51
52 return intel_dig_port->base.type == INTEL_OUTPUT_EDP;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070053}
54
55/**
56 * is_pch_edp - is the port on the PCH and attached to an eDP panel?
57 * @intel_dp: DP struct
58 *
59 * Returns true if the given DP struct corresponds to a PCH DP port attached
60 * to an eDP panel, false otherwise. Helpful for determining whether we
61 * may need FDI resources for a given DP output or not.
62 */
63static bool is_pch_edp(struct intel_dp *intel_dp)
64{
65 return intel_dp->is_pch_edp;
66}
67
Adam Jackson1c958222011-10-14 17:22:25 -040068/**
69 * is_cpu_edp - is the port on the CPU and attached to an eDP panel?
70 * @intel_dp: DP struct
71 *
72 * Returns true if the given DP struct corresponds to a CPU eDP port.
73 */
74static bool is_cpu_edp(struct intel_dp *intel_dp)
75{
76 return is_edp(intel_dp) && !is_pch_edp(intel_dp);
77}
78
Paulo Zanoni30add222012-10-26 19:05:45 -020079static struct drm_device *intel_dp_to_dev(struct intel_dp *intel_dp)
Chris Wilsonea5b2132010-08-04 13:50:23 +010080{
Paulo Zanonida63a9f2012-10-26 19:05:46 -020081 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
82
83 return intel_dig_port->base.base.dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +010084}
Keith Packarda4fc5ed2009-04-07 16:16:42 -070085
Chris Wilsondf0e9242010-09-09 16:20:55 +010086static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
87{
Paulo Zanonifa90ece2012-10-26 19:05:44 -020088 return enc_to_intel_dp(&intel_attached_encoder(connector)->base);
Chris Wilsondf0e9242010-09-09 16:20:55 +010089}
90
Jesse Barnes814948a2010-10-07 16:01:09 -070091/**
92 * intel_encoder_is_pch_edp - is the given encoder a PCH attached eDP?
93 * @encoder: DRM encoder
94 *
95 * Return true if @encoder corresponds to a PCH attached eDP panel. Needed
96 * by intel_display.c.
97 */
98bool intel_encoder_is_pch_edp(struct drm_encoder *encoder)
99{
100 struct intel_dp *intel_dp;
101
102 if (!encoder)
103 return false;
104
105 intel_dp = enc_to_intel_dp(encoder);
106
107 return is_pch_edp(intel_dp);
108}
109
Chris Wilsonea5b2132010-08-04 13:50:23 +0100110static void intel_dp_link_down(struct intel_dp *intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700111
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800112void
Akshay Joshi0206e352011-08-16 15:34:10 -0400113intel_edp_link_config(struct intel_encoder *intel_encoder,
Chris Wilsonea5b2132010-08-04 13:50:23 +0100114 int *lane_num, int *link_bw)
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800115{
Paulo Zanonifa90ece2012-10-26 19:05:44 -0200116 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800117
Chris Wilsonea5b2132010-08-04 13:50:23 +0100118 *lane_num = intel_dp->lane_count;
Daniel Vetter3b5c6622012-10-18 10:15:31 +0200119 *link_bw = drm_dp_bw_code_to_link_rate(intel_dp->link_bw);
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800120}
121
Daniel Vetter94bf2ce2012-06-04 18:39:19 +0200122int
123intel_edp_target_clock(struct intel_encoder *intel_encoder,
124 struct drm_display_mode *mode)
125{
Paulo Zanonifa90ece2012-10-26 19:05:44 -0200126 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
Jani Nikuladd06f902012-10-19 14:51:50 +0300127 struct intel_connector *intel_connector = intel_dp->attached_connector;
Daniel Vetter94bf2ce2012-06-04 18:39:19 +0200128
Jani Nikuladd06f902012-10-19 14:51:50 +0300129 if (intel_connector->panel.fixed_mode)
130 return intel_connector->panel.fixed_mode->clock;
Daniel Vetter94bf2ce2012-06-04 18:39:19 +0200131 else
132 return mode->clock;
133}
134
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700135static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100136intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700137{
Jesse Barnes7183dc22011-07-07 11:10:58 -0700138 int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700139
140 switch (max_link_bw) {
141 case DP_LINK_BW_1_62:
142 case DP_LINK_BW_2_7:
143 break;
144 default:
145 max_link_bw = DP_LINK_BW_1_62;
146 break;
147 }
148 return max_link_bw;
149}
150
151static int
152intel_dp_link_clock(uint8_t link_bw)
153{
154 if (link_bw == DP_LINK_BW_2_7)
155 return 270000;
156 else
157 return 162000;
158}
159
Adam Jacksoncd9dde42011-10-14 12:43:49 -0400160/*
161 * The units on the numbers in the next two are... bizarre. Examples will
162 * make it clearer; this one parallels an example in the eDP spec.
163 *
164 * intel_dp_max_data_rate for one lane of 2.7GHz evaluates as:
165 *
166 * 270000 * 1 * 8 / 10 == 216000
167 *
168 * The actual data capacity of that configuration is 2.16Gbit/s, so the
169 * units are decakilobits. ->clock in a drm_display_mode is in kilohertz -
170 * or equivalently, kilopixels per second - so for 1680x1050R it'd be
171 * 119000. At 18bpp that's 2142000 kilobits per second.
172 *
173 * Thus the strange-looking division by 10 in intel_dp_link_required, to
174 * get the result in decakilobits instead of kilobits.
175 */
176
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700177static int
Keith Packardc8982612012-01-25 08:16:25 -0800178intel_dp_link_required(int pixel_clock, int bpp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700179{
Adam Jacksoncd9dde42011-10-14 12:43:49 -0400180 return (pixel_clock * bpp + 9) / 10;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700181}
182
183static int
Dave Airliefe27d532010-06-30 11:46:17 +1000184intel_dp_max_data_rate(int max_link_clock, int max_lanes)
185{
186 return (max_link_clock * max_lanes * 8) / 10;
187}
188
Daniel Vetterc4867932012-04-10 10:42:36 +0200189static bool
190intel_dp_adjust_dithering(struct intel_dp *intel_dp,
191 struct drm_display_mode *mode,
Daniel Vettercb1793c2012-06-04 18:39:21 +0200192 bool adjust_mode)
Daniel Vetterc4867932012-04-10 10:42:36 +0200193{
194 int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_dp));
Daniel Vetter397fe152012-10-22 22:56:43 +0200195 int max_lanes = drm_dp_max_lane_count(intel_dp->dpcd);
Daniel Vetterc4867932012-04-10 10:42:36 +0200196 int max_rate, mode_rate;
197
198 mode_rate = intel_dp_link_required(mode->clock, 24);
199 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
200
201 if (mode_rate > max_rate) {
202 mode_rate = intel_dp_link_required(mode->clock, 18);
203 if (mode_rate > max_rate)
204 return false;
205
Daniel Vettercb1793c2012-06-04 18:39:21 +0200206 if (adjust_mode)
207 mode->private_flags
Daniel Vetterc4867932012-04-10 10:42:36 +0200208 |= INTEL_MODE_DP_FORCE_6BPC;
209
210 return true;
211 }
212
213 return true;
214}
215
Dave Airliefe27d532010-06-30 11:46:17 +1000216static int
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700217intel_dp_mode_valid(struct drm_connector *connector,
218 struct drm_display_mode *mode)
219{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100220 struct intel_dp *intel_dp = intel_attached_dp(connector);
Jani Nikuladd06f902012-10-19 14:51:50 +0300221 struct intel_connector *intel_connector = to_intel_connector(connector);
222 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700223
Jani Nikuladd06f902012-10-19 14:51:50 +0300224 if (is_edp(intel_dp) && fixed_mode) {
225 if (mode->hdisplay > fixed_mode->hdisplay)
Zhao Yakui7de56f42010-07-19 09:43:14 +0100226 return MODE_PANEL;
227
Jani Nikuladd06f902012-10-19 14:51:50 +0300228 if (mode->vdisplay > fixed_mode->vdisplay)
Zhao Yakui7de56f42010-07-19 09:43:14 +0100229 return MODE_PANEL;
230 }
231
Daniel Vettercb1793c2012-06-04 18:39:21 +0200232 if (!intel_dp_adjust_dithering(intel_dp, mode, false))
Daniel Vetterc4867932012-04-10 10:42:36 +0200233 return MODE_CLOCK_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700234
235 if (mode->clock < 10000)
236 return MODE_CLOCK_LOW;
237
Daniel Vetter0af78a22012-05-23 11:30:55 +0200238 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
239 return MODE_H_ILLEGAL;
240
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700241 return MODE_OK;
242}
243
244static uint32_t
245pack_aux(uint8_t *src, int src_bytes)
246{
247 int i;
248 uint32_t v = 0;
249
250 if (src_bytes > 4)
251 src_bytes = 4;
252 for (i = 0; i < src_bytes; i++)
253 v |= ((uint32_t) src[i]) << ((3-i) * 8);
254 return v;
255}
256
257static void
258unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
259{
260 int i;
261 if (dst_bytes > 4)
262 dst_bytes = 4;
263 for (i = 0; i < dst_bytes; i++)
264 dst[i] = src >> ((3-i) * 8);
265}
266
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700267/* hrawclock is 1/4 the FSB frequency */
268static int
269intel_hrawclk(struct drm_device *dev)
270{
271 struct drm_i915_private *dev_priv = dev->dev_private;
272 uint32_t clkcfg;
273
Vijay Purushothaman9473c8f2012-09-27 19:13:01 +0530274 /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
275 if (IS_VALLEYVIEW(dev))
276 return 200;
277
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700278 clkcfg = I915_READ(CLKCFG);
279 switch (clkcfg & CLKCFG_FSB_MASK) {
280 case CLKCFG_FSB_400:
281 return 100;
282 case CLKCFG_FSB_533:
283 return 133;
284 case CLKCFG_FSB_667:
285 return 166;
286 case CLKCFG_FSB_800:
287 return 200;
288 case CLKCFG_FSB_1067:
289 return 266;
290 case CLKCFG_FSB_1333:
291 return 333;
292 /* these two are just a guess; one of them might be right */
293 case CLKCFG_FSB_1600:
294 case CLKCFG_FSB_1600_ALT:
295 return 400;
296 default:
297 return 133;
298 }
299}
300
Keith Packardebf33b12011-09-29 15:53:27 -0700301static bool ironlake_edp_have_panel_power(struct intel_dp *intel_dp)
302{
Paulo Zanoni30add222012-10-26 19:05:45 -0200303 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packardebf33b12011-09-29 15:53:27 -0700304 struct drm_i915_private *dev_priv = dev->dev_private;
305
306 return (I915_READ(PCH_PP_STATUS) & PP_ON) != 0;
307}
308
309static bool ironlake_edp_have_panel_vdd(struct intel_dp *intel_dp)
310{
Paulo Zanoni30add222012-10-26 19:05:45 -0200311 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packardebf33b12011-09-29 15:53:27 -0700312 struct drm_i915_private *dev_priv = dev->dev_private;
313
314 return (I915_READ(PCH_PP_CONTROL) & EDP_FORCE_VDD) != 0;
315}
316
Keith Packard9b984da2011-09-19 13:54:47 -0700317static void
318intel_dp_check_edp(struct intel_dp *intel_dp)
319{
Paulo Zanoni30add222012-10-26 19:05:45 -0200320 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packard9b984da2011-09-19 13:54:47 -0700321 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packardebf33b12011-09-29 15:53:27 -0700322
Keith Packard9b984da2011-09-19 13:54:47 -0700323 if (!is_edp(intel_dp))
324 return;
Keith Packardebf33b12011-09-29 15:53:27 -0700325 if (!ironlake_edp_have_panel_power(intel_dp) && !ironlake_edp_have_panel_vdd(intel_dp)) {
Keith Packard9b984da2011-09-19 13:54:47 -0700326 WARN(1, "eDP powered off while attempting aux channel communication.\n");
327 DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
Keith Packardebf33b12011-09-29 15:53:27 -0700328 I915_READ(PCH_PP_STATUS),
Keith Packard9b984da2011-09-19 13:54:47 -0700329 I915_READ(PCH_PP_CONTROL));
330 }
331}
332
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700333static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100334intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700335 uint8_t *send, int send_bytes,
336 uint8_t *recv, int recv_size)
337{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100338 uint32_t output_reg = intel_dp->output_reg;
Paulo Zanoni174edf12012-10-26 19:05:50 -0200339 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
340 struct drm_device *dev = intel_dig_port->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700341 struct drm_i915_private *dev_priv = dev->dev_private;
342 uint32_t ch_ctl = output_reg + 0x10;
343 uint32_t ch_data = ch_ctl + 4;
344 int i;
345 int recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700346 uint32_t status;
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700347 uint32_t aux_clock_divider;
Daniel Vetter6b4e0a92012-06-14 22:15:00 +0200348 int try, precharge;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700349
Paulo Zanoni750eb992012-10-18 16:25:08 +0200350 if (IS_HASWELL(dev)) {
Paulo Zanoni174edf12012-10-26 19:05:50 -0200351 switch (intel_dig_port->port) {
Paulo Zanoni750eb992012-10-18 16:25:08 +0200352 case PORT_A:
353 ch_ctl = DPA_AUX_CH_CTL;
354 ch_data = DPA_AUX_CH_DATA1;
355 break;
356 case PORT_B:
357 ch_ctl = PCH_DPB_AUX_CH_CTL;
358 ch_data = PCH_DPB_AUX_CH_DATA1;
359 break;
360 case PORT_C:
361 ch_ctl = PCH_DPC_AUX_CH_CTL;
362 ch_data = PCH_DPC_AUX_CH_DATA1;
363 break;
364 case PORT_D:
365 ch_ctl = PCH_DPD_AUX_CH_CTL;
366 ch_data = PCH_DPD_AUX_CH_DATA1;
367 break;
368 default:
369 BUG();
370 }
371 }
372
Keith Packard9b984da2011-09-19 13:54:47 -0700373 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700374 /* The clock divider is based off the hrawclk,
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700375 * and would like to run at 2MHz. So, take the
376 * hrawclk value and divide by 2 and use that
Jesse Barnes6176b8f2010-09-08 12:42:00 -0700377 *
378 * Note that PCH attached eDP panels should use a 125MHz input
379 * clock divider.
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700380 */
Adam Jackson1c958222011-10-14 17:22:25 -0400381 if (is_cpu_edp(intel_dp)) {
Paulo Zanonib8fc2f62012-10-23 18:30:05 -0200382 if (IS_HASWELL(dev))
383 aux_clock_divider = intel_ddi_get_cdclk_freq(dev_priv) >> 1;
384 else if (IS_VALLEYVIEW(dev))
Vijay Purushothaman9473c8f2012-09-27 19:13:01 +0530385 aux_clock_divider = 100;
386 else if (IS_GEN6(dev) || IS_GEN7(dev))
Keith Packard1a2eb462011-11-16 16:26:07 -0800387 aux_clock_divider = 200; /* SNB & IVB eDP input clock at 400Mhz */
Zhenyu Wange3421a12010-04-08 09:43:27 +0800388 else
389 aux_clock_divider = 225; /* eDP input clock at 450Mhz */
390 } else if (HAS_PCH_SPLIT(dev))
Daniel Vetter6b3ec1c2012-10-20 20:57:44 +0200391 aux_clock_divider = DIV_ROUND_UP(intel_pch_rawclk(dev), 2);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800392 else
393 aux_clock_divider = intel_hrawclk(dev) / 2;
394
Daniel Vetter6b4e0a92012-06-14 22:15:00 +0200395 if (IS_GEN6(dev))
396 precharge = 3;
397 else
398 precharge = 5;
399
Jesse Barnes11bee432011-08-01 15:02:20 -0700400 /* Try to wait for any previous AUX channel activity */
401 for (try = 0; try < 3; try++) {
402 status = I915_READ(ch_ctl);
403 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
404 break;
405 msleep(1);
406 }
407
408 if (try == 3) {
409 WARN(1, "dp_aux_ch not started status 0x%08x\n",
410 I915_READ(ch_ctl));
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100411 return -EBUSY;
412 }
413
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700414 /* Must try at least 3 times according to DP spec */
415 for (try = 0; try < 5; try++) {
416 /* Load the send data into the aux channel data registers */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100417 for (i = 0; i < send_bytes; i += 4)
418 I915_WRITE(ch_data + i,
419 pack_aux(send + i, send_bytes - i));
Akshay Joshi0206e352011-08-16 15:34:10 -0400420
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700421 /* Send the command and wait for it to complete */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100422 I915_WRITE(ch_ctl,
423 DP_AUX_CH_CTL_SEND_BUSY |
424 DP_AUX_CH_CTL_TIME_OUT_400us |
425 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
426 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
427 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
428 DP_AUX_CH_CTL_DONE |
429 DP_AUX_CH_CTL_TIME_OUT_ERROR |
430 DP_AUX_CH_CTL_RECEIVE_ERROR);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700431 for (;;) {
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700432 status = I915_READ(ch_ctl);
433 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
434 break;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100435 udelay(100);
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700436 }
Akshay Joshi0206e352011-08-16 15:34:10 -0400437
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700438 /* Clear done status and any errors */
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100439 I915_WRITE(ch_ctl,
440 status |
441 DP_AUX_CH_CTL_DONE |
442 DP_AUX_CH_CTL_TIME_OUT_ERROR |
443 DP_AUX_CH_CTL_RECEIVE_ERROR);
Adam Jacksond7e96fe2011-07-26 15:39:46 -0400444
445 if (status & (DP_AUX_CH_CTL_TIME_OUT_ERROR |
446 DP_AUX_CH_CTL_RECEIVE_ERROR))
447 continue;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100448 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700449 break;
450 }
451
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700452 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700453 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700454 return -EBUSY;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700455 }
456
457 /* Check for timeout or receive error.
458 * Timeouts occur when the sink is not connected
459 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700460 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700461 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700462 return -EIO;
463 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700464
465 /* Timeouts occur when the device isn't connected, so they're
466 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700467 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800468 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Keith Packarda5b3da52009-06-11 22:30:32 -0700469 return -ETIMEDOUT;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700470 }
471
472 /* Unload any bytes sent back from the other side */
473 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
474 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700475 if (recv_bytes > recv_size)
476 recv_bytes = recv_size;
Akshay Joshi0206e352011-08-16 15:34:10 -0400477
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100478 for (i = 0; i < recv_bytes; i += 4)
479 unpack_aux(I915_READ(ch_data + i),
480 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700481
482 return recv_bytes;
483}
484
485/* Write data to the aux channel in native mode */
486static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100487intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700488 uint16_t address, uint8_t *send, int send_bytes)
489{
490 int ret;
491 uint8_t msg[20];
492 int msg_bytes;
493 uint8_t ack;
494
Keith Packard9b984da2011-09-19 13:54:47 -0700495 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700496 if (send_bytes > 16)
497 return -1;
498 msg[0] = AUX_NATIVE_WRITE << 4;
499 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800500 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700501 msg[3] = send_bytes - 1;
502 memcpy(&msg[4], send, send_bytes);
503 msg_bytes = send_bytes + 4;
504 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100505 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700506 if (ret < 0)
507 return ret;
508 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
509 break;
510 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
511 udelay(100);
512 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700513 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700514 }
515 return send_bytes;
516}
517
518/* Write a single byte to the aux channel in native mode */
519static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100520intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700521 uint16_t address, uint8_t byte)
522{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100523 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700524}
525
526/* read bytes from a native aux channel */
527static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100528intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700529 uint16_t address, uint8_t *recv, int recv_bytes)
530{
531 uint8_t msg[4];
532 int msg_bytes;
533 uint8_t reply[20];
534 int reply_bytes;
535 uint8_t ack;
536 int ret;
537
Keith Packard9b984da2011-09-19 13:54:47 -0700538 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700539 msg[0] = AUX_NATIVE_READ << 4;
540 msg[1] = address >> 8;
541 msg[2] = address & 0xff;
542 msg[3] = recv_bytes - 1;
543
544 msg_bytes = 4;
545 reply_bytes = recv_bytes + 1;
546
547 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100548 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700549 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700550 if (ret == 0)
551 return -EPROTO;
552 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700553 return ret;
554 ack = reply[0];
555 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
556 memcpy(recv, reply + 1, ret - 1);
557 return ret - 1;
558 }
559 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
560 udelay(100);
561 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700562 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700563 }
564}
565
566static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000567intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
568 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700569{
Dave Airlieab2c0672009-12-04 10:55:24 +1000570 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100571 struct intel_dp *intel_dp = container_of(adapter,
572 struct intel_dp,
573 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000574 uint16_t address = algo_data->address;
575 uint8_t msg[5];
576 uint8_t reply[2];
David Flynn8316f332010-12-08 16:10:21 +0000577 unsigned retry;
Dave Airlieab2c0672009-12-04 10:55:24 +1000578 int msg_bytes;
579 int reply_bytes;
580 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700581
Keith Packard9b984da2011-09-19 13:54:47 -0700582 intel_dp_check_edp(intel_dp);
Dave Airlieab2c0672009-12-04 10:55:24 +1000583 /* Set up the command byte */
584 if (mode & MODE_I2C_READ)
585 msg[0] = AUX_I2C_READ << 4;
586 else
587 msg[0] = AUX_I2C_WRITE << 4;
588
589 if (!(mode & MODE_I2C_STOP))
590 msg[0] |= AUX_I2C_MOT << 4;
591
592 msg[1] = address >> 8;
593 msg[2] = address;
594
595 switch (mode) {
596 case MODE_I2C_WRITE:
597 msg[3] = 0;
598 msg[4] = write_byte;
599 msg_bytes = 5;
600 reply_bytes = 1;
601 break;
602 case MODE_I2C_READ:
603 msg[3] = 0;
604 msg_bytes = 4;
605 reply_bytes = 2;
606 break;
607 default:
608 msg_bytes = 3;
609 reply_bytes = 1;
610 break;
611 }
612
David Flynn8316f332010-12-08 16:10:21 +0000613 for (retry = 0; retry < 5; retry++) {
614 ret = intel_dp_aux_ch(intel_dp,
615 msg, msg_bytes,
616 reply, reply_bytes);
Dave Airlieab2c0672009-12-04 10:55:24 +1000617 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000618 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000619 return ret;
620 }
David Flynn8316f332010-12-08 16:10:21 +0000621
622 switch (reply[0] & AUX_NATIVE_REPLY_MASK) {
623 case AUX_NATIVE_REPLY_ACK:
624 /* I2C-over-AUX Reply field is only valid
625 * when paired with AUX ACK.
626 */
627 break;
628 case AUX_NATIVE_REPLY_NACK:
629 DRM_DEBUG_KMS("aux_ch native nack\n");
630 return -EREMOTEIO;
631 case AUX_NATIVE_REPLY_DEFER:
632 udelay(100);
633 continue;
634 default:
635 DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
636 reply[0]);
637 return -EREMOTEIO;
638 }
639
Dave Airlieab2c0672009-12-04 10:55:24 +1000640 switch (reply[0] & AUX_I2C_REPLY_MASK) {
641 case AUX_I2C_REPLY_ACK:
642 if (mode == MODE_I2C_READ) {
643 *read_byte = reply[1];
644 }
645 return reply_bytes - 1;
646 case AUX_I2C_REPLY_NACK:
David Flynn8316f332010-12-08 16:10:21 +0000647 DRM_DEBUG_KMS("aux_i2c nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000648 return -EREMOTEIO;
649 case AUX_I2C_REPLY_DEFER:
David Flynn8316f332010-12-08 16:10:21 +0000650 DRM_DEBUG_KMS("aux_i2c defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000651 udelay(100);
652 break;
653 default:
David Flynn8316f332010-12-08 16:10:21 +0000654 DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
Dave Airlieab2c0672009-12-04 10:55:24 +1000655 return -EREMOTEIO;
656 }
657 }
David Flynn8316f332010-12-08 16:10:21 +0000658
659 DRM_ERROR("too many retries, giving up\n");
660 return -EREMOTEIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700661}
662
663static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100664intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800665 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700666{
Keith Packard0b5c5412011-09-28 16:41:05 -0700667 int ret;
668
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800669 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100670 intel_dp->algo.running = false;
671 intel_dp->algo.address = 0;
672 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700673
Akshay Joshi0206e352011-08-16 15:34:10 -0400674 memset(&intel_dp->adapter, '\0', sizeof(intel_dp->adapter));
Chris Wilsonea5b2132010-08-04 13:50:23 +0100675 intel_dp->adapter.owner = THIS_MODULE;
676 intel_dp->adapter.class = I2C_CLASS_DDC;
Akshay Joshi0206e352011-08-16 15:34:10 -0400677 strncpy(intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100678 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
679 intel_dp->adapter.algo_data = &intel_dp->algo;
680 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
681
Keith Packard0b5c5412011-09-28 16:41:05 -0700682 ironlake_edp_panel_vdd_on(intel_dp);
683 ret = i2c_dp_aux_add_bus(&intel_dp->adapter);
Keith Packardbd943152011-09-18 23:09:52 -0700684 ironlake_edp_panel_vdd_off(intel_dp, false);
Keith Packard0b5c5412011-09-28 16:41:05 -0700685 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700686}
687
Paulo Zanoni00c09d72012-10-26 19:05:52 -0200688bool
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200689intel_dp_mode_fixup(struct drm_encoder *encoder,
690 const struct drm_display_mode *mode,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700691 struct drm_display_mode *adjusted_mode)
692{
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100693 struct drm_device *dev = encoder->dev;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100694 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Jani Nikuladd06f902012-10-19 14:51:50 +0300695 struct intel_connector *intel_connector = intel_dp->attached_connector;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700696 int lane_count, clock;
Daniel Vetter397fe152012-10-22 22:56:43 +0200697 int max_lane_count = drm_dp_max_lane_count(intel_dp->dpcd);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100698 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Daniel Vetter083f9562012-04-20 20:23:49 +0200699 int bpp, mode_rate;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700700 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
701
Jani Nikuladd06f902012-10-19 14:51:50 +0300702 if (is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
703 intel_fixed_panel_mode(intel_connector->panel.fixed_mode,
704 adjusted_mode);
Yuly Novikov53b41832012-10-26 12:04:00 +0300705 intel_pch_panel_fitting(dev,
706 intel_connector->panel.fitting_mode,
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100707 mode, adjusted_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100708 }
709
Daniel Vettercb1793c2012-06-04 18:39:21 +0200710 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
Daniel Vetter0af78a22012-05-23 11:30:55 +0200711 return false;
712
Daniel Vetter083f9562012-04-20 20:23:49 +0200713 DRM_DEBUG_KMS("DP link computation with max lane count %i "
714 "max bw %02x pixel clock %iKHz\n",
Daniel Vetter71244652012-06-04 18:39:20 +0200715 max_lane_count, bws[max_clock], adjusted_mode->clock);
Daniel Vetter083f9562012-04-20 20:23:49 +0200716
Daniel Vettercb1793c2012-06-04 18:39:21 +0200717 if (!intel_dp_adjust_dithering(intel_dp, adjusted_mode, true))
Daniel Vetterc4867932012-04-10 10:42:36 +0200718 return false;
719
720 bpp = adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC ? 18 : 24;
Daniel Vetter71244652012-06-04 18:39:20 +0200721 mode_rate = intel_dp_link_required(adjusted_mode->clock, bpp);
Daniel Vetterc4867932012-04-10 10:42:36 +0200722
Jesse Barnes2514bc52012-06-21 15:13:50 -0700723 for (clock = 0; clock <= max_clock; clock++) {
724 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
Dave Airliefe27d532010-06-30 11:46:17 +1000725 int link_avail = intel_dp_max_data_rate(intel_dp_link_clock(bws[clock]), lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700726
Daniel Vetter083f9562012-04-20 20:23:49 +0200727 if (mode_rate <= link_avail) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100728 intel_dp->link_bw = bws[clock];
729 intel_dp->lane_count = lane_count;
730 adjusted_mode->clock = intel_dp_link_clock(intel_dp->link_bw);
Daniel Vetter083f9562012-04-20 20:23:49 +0200731 DRM_DEBUG_KMS("DP link bw %02x lane "
732 "count %d clock %d bpp %d\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100733 intel_dp->link_bw, intel_dp->lane_count,
Daniel Vetter083f9562012-04-20 20:23:49 +0200734 adjusted_mode->clock, bpp);
735 DRM_DEBUG_KMS("DP link bw required %i available %i\n",
736 mode_rate, link_avail);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700737 return true;
738 }
739 }
740 }
Dave Airliefe27d532010-06-30 11:46:17 +1000741
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700742 return false;
743}
744
745struct intel_dp_m_n {
746 uint32_t tu;
747 uint32_t gmch_m;
748 uint32_t gmch_n;
749 uint32_t link_m;
750 uint32_t link_n;
751};
752
753static void
754intel_reduce_ratio(uint32_t *num, uint32_t *den)
755{
756 while (*num > 0xffffff || *den > 0xffffff) {
757 *num >>= 1;
758 *den >>= 1;
759 }
760}
761
762static void
Zhao Yakui36e83a12010-06-12 14:32:21 +0800763intel_dp_compute_m_n(int bpp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700764 int nlanes,
765 int pixel_clock,
766 int link_clock,
767 struct intel_dp_m_n *m_n)
768{
769 m_n->tu = 64;
Zhao Yakui36e83a12010-06-12 14:32:21 +0800770 m_n->gmch_m = (pixel_clock * bpp) >> 3;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700771 m_n->gmch_n = link_clock * nlanes;
772 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
773 m_n->link_m = pixel_clock;
774 m_n->link_n = link_clock;
775 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
776}
777
778void
779intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
780 struct drm_display_mode *adjusted_mode)
781{
782 struct drm_device *dev = crtc->dev;
Paulo Zanonifa90ece2012-10-26 19:05:44 -0200783 struct intel_encoder *intel_encoder;
784 struct intel_dp *intel_dp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700785 struct drm_i915_private *dev_priv = dev->dev_private;
786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes858fa0352011-06-24 12:19:24 -0700787 int lane_count = 4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700788 struct intel_dp_m_n m_n;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800789 int pipe = intel_crtc->pipe;
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -0200790 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700791
792 /*
Eric Anholt21d40d32010-03-25 11:11:14 -0700793 * Find the lane count in the intel_encoder private
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700794 */
Paulo Zanonifa90ece2012-10-26 19:05:44 -0200795 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
796 intel_dp = enc_to_intel_dp(&intel_encoder->base);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700797
Paulo Zanonifa90ece2012-10-26 19:05:44 -0200798 if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
799 intel_encoder->type == INTEL_OUTPUT_EDP)
Keith Packard9a10f402011-11-02 13:03:47 -0700800 {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100801 lane_count = intel_dp->lane_count;
Jesse Barnes51190662010-10-07 16:01:08 -0700802 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700803 }
804 }
805
806 /*
807 * Compute the GMCH and Link ratios. The '3' here is
808 * the number of bytes_per_pixel post-LUT, which we always
809 * set up for 8-bits of R/G/B, or 3 bytes total.
810 */
Jesse Barnes858fa0352011-06-24 12:19:24 -0700811 intel_dp_compute_m_n(intel_crtc->bpp, lane_count,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700812 mode->clock, adjusted_mode->clock, &m_n);
813
Paulo Zanoni1eb8dfe2012-10-18 12:42:10 -0300814 if (IS_HASWELL(dev)) {
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -0200815 I915_WRITE(PIPE_DATA_M1(cpu_transcoder),
816 TU_SIZE(m_n.tu) | m_n.gmch_m);
817 I915_WRITE(PIPE_DATA_N1(cpu_transcoder), m_n.gmch_n);
818 I915_WRITE(PIPE_LINK_M1(cpu_transcoder), m_n.link_m);
819 I915_WRITE(PIPE_LINK_N1(cpu_transcoder), m_n.link_n);
Paulo Zanoni1eb8dfe2012-10-18 12:42:10 -0300820 } else if (HAS_PCH_SPLIT(dev)) {
Paulo Zanoni7346bfa2012-10-15 15:51:35 -0300821 I915_WRITE(TRANSDATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800822 I915_WRITE(TRANSDATA_N1(pipe), m_n.gmch_n);
823 I915_WRITE(TRANSDPLINK_M1(pipe), m_n.link_m);
824 I915_WRITE(TRANSDPLINK_N1(pipe), m_n.link_n);
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530825 } else if (IS_VALLEYVIEW(dev)) {
826 I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
827 I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
828 I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
829 I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700830 } else {
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800831 I915_WRITE(PIPE_GMCH_DATA_M(pipe),
Paulo Zanoni7346bfa2012-10-15 15:51:35 -0300832 TU_SIZE(m_n.tu) | m_n.gmch_m);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800833 I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n.gmch_n);
834 I915_WRITE(PIPE_DP_LINK_M(pipe), m_n.link_m);
835 I915_WRITE(PIPE_DP_LINK_N(pipe), m_n.link_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700836 }
837}
838
Paulo Zanoni247d89f2012-10-15 15:51:33 -0300839void intel_dp_init_link_config(struct intel_dp *intel_dp)
840{
841 memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
842 intel_dp->link_configuration[0] = intel_dp->link_bw;
843 intel_dp->link_configuration[1] = intel_dp->lane_count;
844 intel_dp->link_configuration[8] = DP_SET_ANSI_8B10B;
845 /*
846 * Check for DPCD version > 1.1 and enhanced framing support
847 */
848 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
849 (intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)) {
850 intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
851 }
852}
853
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700854static void
855intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
856 struct drm_display_mode *adjusted_mode)
857{
Zhenyu Wange3421a12010-04-08 09:43:27 +0800858 struct drm_device *dev = encoder->dev;
Keith Packard417e8222011-11-01 19:54:11 -0700859 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100860 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Paulo Zanonifa90ece2012-10-26 19:05:44 -0200861 struct drm_crtc *crtc = encoder->crtc;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700862 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
863
Keith Packard417e8222011-11-01 19:54:11 -0700864 /*
Keith Packard1a2eb462011-11-16 16:26:07 -0800865 * There are four kinds of DP registers:
Keith Packard417e8222011-11-01 19:54:11 -0700866 *
867 * IBX PCH
Keith Packard1a2eb462011-11-16 16:26:07 -0800868 * SNB CPU
869 * IVB CPU
Keith Packard417e8222011-11-01 19:54:11 -0700870 * CPT PCH
871 *
872 * IBX PCH and CPU are the same for almost everything,
873 * except that the CPU DP PLL is configured in this
874 * register
875 *
876 * CPT PCH is quite different, having many bits moved
877 * to the TRANS_DP_CTL register instead. That
878 * configuration happens (oddly) in ironlake_pch_enable
879 */
Adam Jackson9c9e7922010-04-05 17:57:59 -0400880
Keith Packard417e8222011-11-01 19:54:11 -0700881 /* Preserve the BIOS-computed detected bit. This is
882 * supposed to be read-only.
883 */
884 intel_dp->DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700885
Keith Packard417e8222011-11-01 19:54:11 -0700886 /* Handle DP bits in common between all three register formats */
Keith Packard417e8222011-11-01 19:54:11 -0700887 intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700888
Chris Wilsonea5b2132010-08-04 13:50:23 +0100889 switch (intel_dp->lane_count) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700890 case 1:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100891 intel_dp->DP |= DP_PORT_WIDTH_1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700892 break;
893 case 2:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100894 intel_dp->DP |= DP_PORT_WIDTH_2;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700895 break;
896 case 4:
Chris Wilsonea5b2132010-08-04 13:50:23 +0100897 intel_dp->DP |= DP_PORT_WIDTH_4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700898 break;
899 }
Wu Fengguange0dac652011-09-05 14:25:34 +0800900 if (intel_dp->has_audio) {
901 DRM_DEBUG_DRIVER("Enabling DP audio on pipe %c\n",
902 pipe_name(intel_crtc->pipe));
Chris Wilsonea5b2132010-08-04 13:50:23 +0100903 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Wu Fengguange0dac652011-09-05 14:25:34 +0800904 intel_write_eld(encoder, adjusted_mode);
905 }
Paulo Zanoni247d89f2012-10-15 15:51:33 -0300906
907 intel_dp_init_link_config(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700908
Keith Packard417e8222011-11-01 19:54:11 -0700909 /* Split out the IBX/CPU vs CPT settings */
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800910
Gajanan Bhat19c03922012-09-27 19:13:07 +0530911 if (is_cpu_edp(intel_dp) && IS_GEN7(dev) && !IS_VALLEYVIEW(dev)) {
Keith Packard1a2eb462011-11-16 16:26:07 -0800912 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
913 intel_dp->DP |= DP_SYNC_HS_HIGH;
914 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
915 intel_dp->DP |= DP_SYNC_VS_HIGH;
916 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
917
918 if (intel_dp->link_configuration[1] & DP_LANE_COUNT_ENHANCED_FRAME_EN)
919 intel_dp->DP |= DP_ENHANCED_FRAMING;
920
921 intel_dp->DP |= intel_crtc->pipe << 29;
922
923 /* don't miss out required setting for eDP */
Keith Packard1a2eb462011-11-16 16:26:07 -0800924 if (adjusted_mode->clock < 200000)
925 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
926 else
927 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
928 } else if (!HAS_PCH_CPT(dev) || is_cpu_edp(intel_dp)) {
Keith Packard417e8222011-11-01 19:54:11 -0700929 intel_dp->DP |= intel_dp->color_range;
930
931 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
932 intel_dp->DP |= DP_SYNC_HS_HIGH;
933 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
934 intel_dp->DP |= DP_SYNC_VS_HIGH;
935 intel_dp->DP |= DP_LINK_TRAIN_OFF;
936
937 if (intel_dp->link_configuration[1] & DP_LANE_COUNT_ENHANCED_FRAME_EN)
938 intel_dp->DP |= DP_ENHANCED_FRAMING;
939
940 if (intel_crtc->pipe == 1)
941 intel_dp->DP |= DP_PIPEB_SELECT;
942
943 if (is_cpu_edp(intel_dp)) {
944 /* don't miss out required setting for eDP */
Keith Packard417e8222011-11-01 19:54:11 -0700945 if (adjusted_mode->clock < 200000)
946 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
947 else
948 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
949 }
950 } else {
951 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800952 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700953}
954
Keith Packard99ea7122011-11-01 19:57:50 -0700955#define IDLE_ON_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
956#define IDLE_ON_VALUE (PP_ON | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_ON_IDLE)
957
958#define IDLE_OFF_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
959#define IDLE_OFF_VALUE (0 | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
960
961#define IDLE_CYCLE_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
962#define IDLE_CYCLE_VALUE (0 | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
963
964static void ironlake_wait_panel_status(struct intel_dp *intel_dp,
965 u32 mask,
966 u32 value)
967{
Paulo Zanoni30add222012-10-26 19:05:45 -0200968 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packard99ea7122011-11-01 19:57:50 -0700969 struct drm_i915_private *dev_priv = dev->dev_private;
970
971 DRM_DEBUG_KMS("mask %08x value %08x status %08x control %08x\n",
972 mask, value,
973 I915_READ(PCH_PP_STATUS),
974 I915_READ(PCH_PP_CONTROL));
975
976 if (_wait_for((I915_READ(PCH_PP_STATUS) & mask) == value, 5000, 10)) {
977 DRM_ERROR("Panel status timeout: status %08x control %08x\n",
978 I915_READ(PCH_PP_STATUS),
979 I915_READ(PCH_PP_CONTROL));
980 }
981}
982
983static void ironlake_wait_panel_on(struct intel_dp *intel_dp)
984{
985 DRM_DEBUG_KMS("Wait for panel power on\n");
986 ironlake_wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
987}
988
Keith Packardbd943152011-09-18 23:09:52 -0700989static void ironlake_wait_panel_off(struct intel_dp *intel_dp)
990{
Keith Packardbd943152011-09-18 23:09:52 -0700991 DRM_DEBUG_KMS("Wait for panel power off time\n");
Keith Packard99ea7122011-11-01 19:57:50 -0700992 ironlake_wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
Keith Packardbd943152011-09-18 23:09:52 -0700993}
Keith Packardbd943152011-09-18 23:09:52 -0700994
Keith Packard99ea7122011-11-01 19:57:50 -0700995static void ironlake_wait_panel_power_cycle(struct intel_dp *intel_dp)
996{
997 DRM_DEBUG_KMS("Wait for panel power cycle\n");
998 ironlake_wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
999}
Keith Packardbd943152011-09-18 23:09:52 -07001000
Keith Packard99ea7122011-11-01 19:57:50 -07001001
Keith Packard832dd3c2011-11-01 19:34:06 -07001002/* Read the current pp_control value, unlocking the register if it
1003 * is locked
1004 */
1005
1006static u32 ironlake_get_pp_control(struct drm_i915_private *dev_priv)
1007{
1008 u32 control = I915_READ(PCH_PP_CONTROL);
1009
1010 control &= ~PANEL_UNLOCK_MASK;
1011 control |= PANEL_UNLOCK_REGS;
1012 return control;
Keith Packardbd943152011-09-18 23:09:52 -07001013}
1014
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001015void ironlake_edp_panel_vdd_on(struct intel_dp *intel_dp)
Jesse Barnes5d613502011-01-24 17:10:54 -08001016{
Paulo Zanoni30add222012-10-26 19:05:45 -02001017 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001018 struct drm_i915_private *dev_priv = dev->dev_private;
1019 u32 pp;
1020
Keith Packard97af61f572011-09-28 16:23:51 -07001021 if (!is_edp(intel_dp))
1022 return;
Keith Packardf01eca22011-09-28 16:48:10 -07001023 DRM_DEBUG_KMS("Turn eDP VDD on\n");
Jesse Barnes5d613502011-01-24 17:10:54 -08001024
Keith Packardbd943152011-09-18 23:09:52 -07001025 WARN(intel_dp->want_panel_vdd,
1026 "eDP VDD already requested on\n");
1027
1028 intel_dp->want_panel_vdd = true;
Keith Packard99ea7122011-11-01 19:57:50 -07001029
Keith Packardbd943152011-09-18 23:09:52 -07001030 if (ironlake_edp_have_panel_vdd(intel_dp)) {
1031 DRM_DEBUG_KMS("eDP VDD already on\n");
1032 return;
1033 }
1034
Keith Packard99ea7122011-11-01 19:57:50 -07001035 if (!ironlake_edp_have_panel_power(intel_dp))
1036 ironlake_wait_panel_power_cycle(intel_dp);
1037
Keith Packard832dd3c2011-11-01 19:34:06 -07001038 pp = ironlake_get_pp_control(dev_priv);
Jesse Barnes5d613502011-01-24 17:10:54 -08001039 pp |= EDP_FORCE_VDD;
1040 I915_WRITE(PCH_PP_CONTROL, pp);
1041 POSTING_READ(PCH_PP_CONTROL);
Keith Packardf01eca22011-09-28 16:48:10 -07001042 DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
1043 I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
Keith Packardebf33b12011-09-29 15:53:27 -07001044
1045 /*
1046 * If the panel wasn't on, delay before accessing aux channel
1047 */
1048 if (!ironlake_edp_have_panel_power(intel_dp)) {
Keith Packardbd943152011-09-18 23:09:52 -07001049 DRM_DEBUG_KMS("eDP was not running\n");
Keith Packardf01eca22011-09-28 16:48:10 -07001050 msleep(intel_dp->panel_power_up_delay);
Keith Packardf01eca22011-09-28 16:48:10 -07001051 }
Jesse Barnes5d613502011-01-24 17:10:54 -08001052}
1053
Keith Packardbd943152011-09-18 23:09:52 -07001054static void ironlake_panel_vdd_off_sync(struct intel_dp *intel_dp)
Jesse Barnes5d613502011-01-24 17:10:54 -08001055{
Paulo Zanoni30add222012-10-26 19:05:45 -02001056 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001057 struct drm_i915_private *dev_priv = dev->dev_private;
1058 u32 pp;
1059
Daniel Vettera0e99e62012-12-02 01:05:46 +01001060 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
1061
Keith Packardbd943152011-09-18 23:09:52 -07001062 if (!intel_dp->want_panel_vdd && ironlake_edp_have_panel_vdd(intel_dp)) {
Keith Packard832dd3c2011-11-01 19:34:06 -07001063 pp = ironlake_get_pp_control(dev_priv);
Keith Packardbd943152011-09-18 23:09:52 -07001064 pp &= ~EDP_FORCE_VDD;
1065 I915_WRITE(PCH_PP_CONTROL, pp);
1066 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes5d613502011-01-24 17:10:54 -08001067
Keith Packardbd943152011-09-18 23:09:52 -07001068 /* Make sure sequencer is idle before allowing subsequent activity */
1069 DRM_DEBUG_KMS("PCH_PP_STATUS: 0x%08x PCH_PP_CONTROL: 0x%08x\n",
1070 I915_READ(PCH_PP_STATUS), I915_READ(PCH_PP_CONTROL));
Keith Packard99ea7122011-11-01 19:57:50 -07001071
1072 msleep(intel_dp->panel_power_down_delay);
Keith Packardbd943152011-09-18 23:09:52 -07001073 }
1074}
1075
1076static void ironlake_panel_vdd_work(struct work_struct *__work)
1077{
1078 struct intel_dp *intel_dp = container_of(to_delayed_work(__work),
1079 struct intel_dp, panel_vdd_work);
Paulo Zanoni30add222012-10-26 19:05:45 -02001080 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packardbd943152011-09-18 23:09:52 -07001081
Keith Packard627f7672011-10-31 11:30:10 -07001082 mutex_lock(&dev->mode_config.mutex);
Keith Packardbd943152011-09-18 23:09:52 -07001083 ironlake_panel_vdd_off_sync(intel_dp);
Keith Packard627f7672011-10-31 11:30:10 -07001084 mutex_unlock(&dev->mode_config.mutex);
Keith Packardbd943152011-09-18 23:09:52 -07001085}
1086
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001087void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
Keith Packardbd943152011-09-18 23:09:52 -07001088{
Keith Packard97af61f572011-09-28 16:23:51 -07001089 if (!is_edp(intel_dp))
1090 return;
Jesse Barnes5d613502011-01-24 17:10:54 -08001091
Keith Packardbd943152011-09-18 23:09:52 -07001092 DRM_DEBUG_KMS("Turn eDP VDD off %d\n", intel_dp->want_panel_vdd);
1093 WARN(!intel_dp->want_panel_vdd, "eDP VDD not forced on");
Keith Packardf2e8b182011-11-01 20:01:35 -07001094
Keith Packardbd943152011-09-18 23:09:52 -07001095 intel_dp->want_panel_vdd = false;
1096
1097 if (sync) {
1098 ironlake_panel_vdd_off_sync(intel_dp);
1099 } else {
1100 /*
1101 * Queue the timer to fire a long
1102 * time from now (relative to the power down delay)
1103 * to keep the panel power up across a sequence of operations
1104 */
1105 schedule_delayed_work(&intel_dp->panel_vdd_work,
1106 msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5));
1107 }
Jesse Barnes5d613502011-01-24 17:10:54 -08001108}
1109
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001110void ironlake_edp_panel_on(struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -07001111{
Paulo Zanoni30add222012-10-26 19:05:45 -02001112 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001113 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packard99ea7122011-11-01 19:57:50 -07001114 u32 pp;
Jesse Barnes9934c132010-07-22 13:18:19 -07001115
Keith Packard97af61f572011-09-28 16:23:51 -07001116 if (!is_edp(intel_dp))
Keith Packardbd943152011-09-18 23:09:52 -07001117 return;
Keith Packard99ea7122011-11-01 19:57:50 -07001118
1119 DRM_DEBUG_KMS("Turn eDP power on\n");
1120
1121 if (ironlake_edp_have_panel_power(intel_dp)) {
1122 DRM_DEBUG_KMS("eDP power already on\n");
Keith Packard7d639f32011-09-29 16:05:34 -07001123 return;
Keith Packard99ea7122011-11-01 19:57:50 -07001124 }
Jesse Barnes9934c132010-07-22 13:18:19 -07001125
Keith Packard99ea7122011-11-01 19:57:50 -07001126 ironlake_wait_panel_power_cycle(intel_dp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001127
Keith Packard832dd3c2011-11-01 19:34:06 -07001128 pp = ironlake_get_pp_control(dev_priv);
Keith Packard05ce1a42011-09-29 16:33:01 -07001129 if (IS_GEN5(dev)) {
1130 /* ILK workaround: disable reset around power sequence */
1131 pp &= ~PANEL_POWER_RESET;
1132 I915_WRITE(PCH_PP_CONTROL, pp);
1133 POSTING_READ(PCH_PP_CONTROL);
1134 }
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001135
Keith Packard1c0ae802011-09-19 13:59:29 -07001136 pp |= POWER_TARGET_ON;
Keith Packard99ea7122011-11-01 19:57:50 -07001137 if (!IS_GEN5(dev))
1138 pp |= PANEL_POWER_RESET;
1139
Jesse Barnes9934c132010-07-22 13:18:19 -07001140 I915_WRITE(PCH_PP_CONTROL, pp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001141 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -07001142
Keith Packard99ea7122011-11-01 19:57:50 -07001143 ironlake_wait_panel_on(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001144
Keith Packard05ce1a42011-09-29 16:33:01 -07001145 if (IS_GEN5(dev)) {
1146 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
1147 I915_WRITE(PCH_PP_CONTROL, pp);
1148 POSTING_READ(PCH_PP_CONTROL);
1149 }
Jesse Barnes9934c132010-07-22 13:18:19 -07001150}
1151
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001152void ironlake_edp_panel_off(struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -07001153{
Paulo Zanoni30add222012-10-26 19:05:45 -02001154 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001155 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packard99ea7122011-11-01 19:57:50 -07001156 u32 pp;
Jesse Barnes9934c132010-07-22 13:18:19 -07001157
Keith Packard97af61f572011-09-28 16:23:51 -07001158 if (!is_edp(intel_dp))
1159 return;
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001160
Keith Packard99ea7122011-11-01 19:57:50 -07001161 DRM_DEBUG_KMS("Turn eDP power off\n");
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001162
Daniel Vetter6cb49832012-05-20 17:14:50 +02001163 WARN(!intel_dp->want_panel_vdd, "Need VDD to turn off panel\n");
Jesse Barnes9934c132010-07-22 13:18:19 -07001164
Keith Packard832dd3c2011-11-01 19:34:06 -07001165 pp = ironlake_get_pp_control(dev_priv);
Daniel Vetter35a38552012-08-12 22:17:14 +02001166 /* We need to switch off panel power _and_ force vdd, for otherwise some
1167 * panels get very unhappy and cease to work. */
1168 pp &= ~(POWER_TARGET_ON | EDP_FORCE_VDD | PANEL_POWER_RESET | EDP_BLC_ENABLE);
Keith Packard99ea7122011-11-01 19:57:50 -07001169 I915_WRITE(PCH_PP_CONTROL, pp);
1170 POSTING_READ(PCH_PP_CONTROL);
Jesse Barnes9934c132010-07-22 13:18:19 -07001171
Daniel Vetter35a38552012-08-12 22:17:14 +02001172 intel_dp->want_panel_vdd = false;
1173
Keith Packard99ea7122011-11-01 19:57:50 -07001174 ironlake_wait_panel_off(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001175}
1176
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001177void ironlake_edp_backlight_on(struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001178{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02001179 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1180 struct drm_device *dev = intel_dig_port->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001181 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonida63a9f2012-10-26 19:05:46 -02001182 int pipe = to_intel_crtc(intel_dig_port->base.base.crtc)->pipe;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001183 u32 pp;
1184
Keith Packardf01eca22011-09-28 16:48:10 -07001185 if (!is_edp(intel_dp))
1186 return;
1187
Zhao Yakui28c97732009-10-09 11:39:41 +08001188 DRM_DEBUG_KMS("\n");
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001189 /*
1190 * If we enable the backlight right away following a panel power
1191 * on, we may see slight flicker as the panel syncs with the eDP
1192 * link. So delay a bit to make sure the image is solid before
1193 * allowing it to appear.
1194 */
Keith Packardf01eca22011-09-28 16:48:10 -07001195 msleep(intel_dp->backlight_on_delay);
Keith Packard832dd3c2011-11-01 19:34:06 -07001196 pp = ironlake_get_pp_control(dev_priv);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001197 pp |= EDP_BLC_ENABLE;
1198 I915_WRITE(PCH_PP_CONTROL, pp);
Keith Packardf01eca22011-09-28 16:48:10 -07001199 POSTING_READ(PCH_PP_CONTROL);
Daniel Vetter035aa3d2012-10-20 20:57:42 +02001200
1201 intel_panel_enable_backlight(dev, pipe);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001202}
1203
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001204void ironlake_edp_backlight_off(struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001205{
Paulo Zanoni30add222012-10-26 19:05:45 -02001206 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001207 struct drm_i915_private *dev_priv = dev->dev_private;
1208 u32 pp;
1209
Keith Packardf01eca22011-09-28 16:48:10 -07001210 if (!is_edp(intel_dp))
1211 return;
1212
Daniel Vetter035aa3d2012-10-20 20:57:42 +02001213 intel_panel_disable_backlight(dev);
1214
Zhao Yakui28c97732009-10-09 11:39:41 +08001215 DRM_DEBUG_KMS("\n");
Keith Packard832dd3c2011-11-01 19:34:06 -07001216 pp = ironlake_get_pp_control(dev_priv);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001217 pp &= ~EDP_BLC_ENABLE;
1218 I915_WRITE(PCH_PP_CONTROL, pp);
Keith Packardf01eca22011-09-28 16:48:10 -07001219 POSTING_READ(PCH_PP_CONTROL);
1220 msleep(intel_dp->backlight_off_delay);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001221}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001222
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001223static void ironlake_edp_pll_on(struct intel_dp *intel_dp)
Jesse Barnesd240f202010-08-13 15:43:26 -07001224{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02001225 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1226 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
1227 struct drm_device *dev = crtc->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -07001228 struct drm_i915_private *dev_priv = dev->dev_private;
1229 u32 dpa_ctl;
1230
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001231 assert_pipe_disabled(dev_priv,
1232 to_intel_crtc(crtc)->pipe);
1233
Jesse Barnesd240f202010-08-13 15:43:26 -07001234 DRM_DEBUG_KMS("\n");
1235 dpa_ctl = I915_READ(DP_A);
Daniel Vetter07679352012-09-06 22:15:42 +02001236 WARN(dpa_ctl & DP_PLL_ENABLE, "dp pll on, should be off\n");
1237 WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
1238
1239 /* We don't adjust intel_dp->DP while tearing down the link, to
1240 * facilitate link retraining (e.g. after hotplug). Hence clear all
1241 * enable bits here to ensure that we don't enable too much. */
1242 intel_dp->DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
1243 intel_dp->DP |= DP_PLL_ENABLE;
1244 I915_WRITE(DP_A, intel_dp->DP);
Jesse Barnes298b0b32010-10-07 16:01:24 -07001245 POSTING_READ(DP_A);
1246 udelay(200);
Jesse Barnesd240f202010-08-13 15:43:26 -07001247}
1248
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001249static void ironlake_edp_pll_off(struct intel_dp *intel_dp)
Jesse Barnesd240f202010-08-13 15:43:26 -07001250{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02001251 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1252 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
1253 struct drm_device *dev = crtc->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -07001254 struct drm_i915_private *dev_priv = dev->dev_private;
1255 u32 dpa_ctl;
1256
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001257 assert_pipe_disabled(dev_priv,
1258 to_intel_crtc(crtc)->pipe);
1259
Jesse Barnesd240f202010-08-13 15:43:26 -07001260 dpa_ctl = I915_READ(DP_A);
Daniel Vetter07679352012-09-06 22:15:42 +02001261 WARN((dpa_ctl & DP_PLL_ENABLE) == 0,
1262 "dp pll off, should be on\n");
1263 WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
1264
1265 /* We can't rely on the value tracked for the DP register in
1266 * intel_dp->DP because link_down must not change that (otherwise link
1267 * re-training will fail. */
Jesse Barnes298b0b32010-10-07 16:01:24 -07001268 dpa_ctl &= ~DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -07001269 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +01001270 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -07001271 udelay(200);
1272}
1273
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001274/* If the sink supports it, try to set the power state appropriately */
Paulo Zanonic19b0662012-10-15 15:51:41 -03001275void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001276{
1277 int ret, i;
1278
1279 /* Should have a valid DPCD by this point */
1280 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
1281 return;
1282
1283 if (mode != DRM_MODE_DPMS_ON) {
1284 ret = intel_dp_aux_native_write_1(intel_dp, DP_SET_POWER,
1285 DP_SET_POWER_D3);
1286 if (ret != 1)
1287 DRM_DEBUG_DRIVER("failed to write sink power state\n");
1288 } else {
1289 /*
1290 * When turning on, we need to retry for 1ms to give the sink
1291 * time to wake up.
1292 */
1293 for (i = 0; i < 3; i++) {
1294 ret = intel_dp_aux_native_write_1(intel_dp,
1295 DP_SET_POWER,
1296 DP_SET_POWER_D0);
1297 if (ret == 1)
1298 break;
1299 msleep(1);
1300 }
1301 }
1302}
1303
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001304static bool intel_dp_get_hw_state(struct intel_encoder *encoder,
1305 enum pipe *pipe)
Jesse Barnesd240f202010-08-13 15:43:26 -07001306{
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001307 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1308 struct drm_device *dev = encoder->base.dev;
1309 struct drm_i915_private *dev_priv = dev->dev_private;
1310 u32 tmp = I915_READ(intel_dp->output_reg);
Jesse Barnesd240f202010-08-13 15:43:26 -07001311
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001312 if (!(tmp & DP_PORT_EN))
1313 return false;
1314
1315 if (is_cpu_edp(intel_dp) && IS_GEN7(dev)) {
1316 *pipe = PORT_TO_PIPE_CPT(tmp);
1317 } else if (!HAS_PCH_CPT(dev) || is_cpu_edp(intel_dp)) {
1318 *pipe = PORT_TO_PIPE(tmp);
1319 } else {
1320 u32 trans_sel;
1321 u32 trans_dp;
1322 int i;
1323
1324 switch (intel_dp->output_reg) {
1325 case PCH_DP_B:
1326 trans_sel = TRANS_DP_PORT_SEL_B;
1327 break;
1328 case PCH_DP_C:
1329 trans_sel = TRANS_DP_PORT_SEL_C;
1330 break;
1331 case PCH_DP_D:
1332 trans_sel = TRANS_DP_PORT_SEL_D;
1333 break;
1334 default:
1335 return true;
1336 }
1337
1338 for_each_pipe(i) {
1339 trans_dp = I915_READ(TRANS_DP_CTL(i));
1340 if ((trans_dp & TRANS_DP_PORT_SEL_MASK) == trans_sel) {
1341 *pipe = i;
1342 return true;
1343 }
1344 }
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001345
Daniel Vetter4a0833e2012-10-26 10:58:11 +02001346 DRM_DEBUG_KMS("No pipe for dp port 0x%x found\n",
1347 intel_dp->output_reg);
1348 }
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001349
1350 return true;
1351}
1352
Daniel Vettere8cb4552012-07-01 13:05:48 +02001353static void intel_disable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07001354{
Daniel Vettere8cb4552012-07-01 13:05:48 +02001355 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Daniel Vetter6cb49832012-05-20 17:14:50 +02001356
1357 /* Make sure the panel is off before trying to change the mode. But also
1358 * ensure that we have vdd while we switch off the panel. */
1359 ironlake_edp_panel_vdd_on(intel_dp);
Keith Packard21264c62011-11-01 20:25:21 -07001360 ironlake_edp_backlight_off(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001361 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
Daniel Vetter35a38552012-08-12 22:17:14 +02001362 ironlake_edp_panel_off(intel_dp);
Daniel Vetter37398502012-09-06 22:15:44 +02001363
1364 /* cpu edp my only be disable _after_ the cpu pipe/plane is disabled. */
1365 if (!is_cpu_edp(intel_dp))
1366 intel_dp_link_down(intel_dp);
Jesse Barnesd240f202010-08-13 15:43:26 -07001367}
1368
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001369static void intel_post_disable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07001370{
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001371 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1372
Daniel Vetter37398502012-09-06 22:15:44 +02001373 if (is_cpu_edp(intel_dp)) {
1374 intel_dp_link_down(intel_dp);
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001375 ironlake_edp_pll_off(intel_dp);
Daniel Vetter37398502012-09-06 22:15:44 +02001376 }
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001377}
1378
Daniel Vettere8cb4552012-07-01 13:05:48 +02001379static void intel_enable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07001380{
Daniel Vettere8cb4552012-07-01 13:05:48 +02001381 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1382 struct drm_device *dev = encoder->base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001383 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001384 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001385
Daniel Vetter0c33d8d2012-09-06 22:15:43 +02001386 if (WARN_ON(dp_reg & DP_PORT_EN))
1387 return;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001388
1389 ironlake_edp_panel_vdd_on(intel_dp);
1390 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
1391 intel_dp_start_link_train(intel_dp);
1392 ironlake_edp_panel_on(intel_dp);
1393 ironlake_edp_panel_vdd_off(intel_dp, true);
1394 intel_dp_complete_link_train(intel_dp);
1395 ironlake_edp_backlight_on(intel_dp);
1396}
1397
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001398static void intel_pre_enable_dp(struct intel_encoder *encoder)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001399{
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001400 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001401
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001402 if (is_cpu_edp(intel_dp))
1403 ironlake_edp_pll_on(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001404}
1405
1406/*
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001407 * Native read with retry for link status and receiver capability reads for
1408 * cases where the sink may still be asleep.
1409 */
1410static bool
1411intel_dp_aux_native_read_retry(struct intel_dp *intel_dp, uint16_t address,
1412 uint8_t *recv, int recv_bytes)
1413{
1414 int ret, i;
1415
1416 /*
1417 * Sinks are *supposed* to come up within 1ms from an off state,
1418 * but we're also supposed to retry 3 times per the spec.
1419 */
1420 for (i = 0; i < 3; i++) {
1421 ret = intel_dp_aux_native_read(intel_dp, address, recv,
1422 recv_bytes);
1423 if (ret == recv_bytes)
1424 return true;
1425 msleep(1);
1426 }
1427
1428 return false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001429}
1430
1431/*
1432 * Fetch AUX CH registers 0x202 - 0x207 which contain
1433 * link status information
1434 */
1435static bool
Keith Packard93f62da2011-11-01 19:45:03 -07001436intel_dp_get_link_status(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001437{
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001438 return intel_dp_aux_native_read_retry(intel_dp,
1439 DP_LANE0_1_STATUS,
Keith Packard93f62da2011-11-01 19:45:03 -07001440 link_status,
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001441 DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001442}
1443
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001444#if 0
1445static char *voltage_names[] = {
1446 "0.4V", "0.6V", "0.8V", "1.2V"
1447};
1448static char *pre_emph_names[] = {
1449 "0dB", "3.5dB", "6dB", "9.5dB"
1450};
1451static char *link_train_names[] = {
1452 "pattern 1", "pattern 2", "idle", "off"
1453};
1454#endif
1455
1456/*
1457 * These are source-specific values; current Intel hardware supports
1458 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1459 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001460
1461static uint8_t
Keith Packard1a2eb462011-11-16 16:26:07 -08001462intel_dp_voltage_max(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001463{
Paulo Zanoni30add222012-10-26 19:05:45 -02001464 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packard1a2eb462011-11-16 16:26:07 -08001465
1466 if (IS_GEN7(dev) && is_cpu_edp(intel_dp))
1467 return DP_TRAIN_VOLTAGE_SWING_800;
1468 else if (HAS_PCH_CPT(dev) && !is_cpu_edp(intel_dp))
1469 return DP_TRAIN_VOLTAGE_SWING_1200;
1470 else
1471 return DP_TRAIN_VOLTAGE_SWING_800;
1472}
1473
1474static uint8_t
1475intel_dp_pre_emphasis_max(struct intel_dp *intel_dp, uint8_t voltage_swing)
1476{
Paulo Zanoni30add222012-10-26 19:05:45 -02001477 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packard1a2eb462011-11-16 16:26:07 -08001478
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001479 if (IS_HASWELL(dev)) {
1480 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1481 case DP_TRAIN_VOLTAGE_SWING_400:
1482 return DP_TRAIN_PRE_EMPHASIS_9_5;
1483 case DP_TRAIN_VOLTAGE_SWING_600:
1484 return DP_TRAIN_PRE_EMPHASIS_6;
1485 case DP_TRAIN_VOLTAGE_SWING_800:
1486 return DP_TRAIN_PRE_EMPHASIS_3_5;
1487 case DP_TRAIN_VOLTAGE_SWING_1200:
1488 default:
1489 return DP_TRAIN_PRE_EMPHASIS_0;
1490 }
1491 } else if (IS_GEN7(dev) && is_cpu_edp(intel_dp) && !IS_VALLEYVIEW(dev)) {
Keith Packard1a2eb462011-11-16 16:26:07 -08001492 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1493 case DP_TRAIN_VOLTAGE_SWING_400:
1494 return DP_TRAIN_PRE_EMPHASIS_6;
1495 case DP_TRAIN_VOLTAGE_SWING_600:
1496 case DP_TRAIN_VOLTAGE_SWING_800:
1497 return DP_TRAIN_PRE_EMPHASIS_3_5;
1498 default:
1499 return DP_TRAIN_PRE_EMPHASIS_0;
1500 }
1501 } else {
1502 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1503 case DP_TRAIN_VOLTAGE_SWING_400:
1504 return DP_TRAIN_PRE_EMPHASIS_6;
1505 case DP_TRAIN_VOLTAGE_SWING_600:
1506 return DP_TRAIN_PRE_EMPHASIS_6;
1507 case DP_TRAIN_VOLTAGE_SWING_800:
1508 return DP_TRAIN_PRE_EMPHASIS_3_5;
1509 case DP_TRAIN_VOLTAGE_SWING_1200:
1510 default:
1511 return DP_TRAIN_PRE_EMPHASIS_0;
1512 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001513 }
1514}
1515
1516static void
Keith Packard93f62da2011-11-01 19:45:03 -07001517intel_get_adjust_train(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001518{
1519 uint8_t v = 0;
1520 uint8_t p = 0;
1521 int lane;
Keith Packard1a2eb462011-11-16 16:26:07 -08001522 uint8_t voltage_max;
1523 uint8_t preemph_max;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001524
Jesse Barnes33a34e42010-09-08 12:42:02 -07001525 for (lane = 0; lane < intel_dp->lane_count; lane++) {
Daniel Vetter0f037bd2012-10-18 10:15:27 +02001526 uint8_t this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
1527 uint8_t this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001528
1529 if (this_v > v)
1530 v = this_v;
1531 if (this_p > p)
1532 p = this_p;
1533 }
1534
Keith Packard1a2eb462011-11-16 16:26:07 -08001535 voltage_max = intel_dp_voltage_max(intel_dp);
Keith Packard417e8222011-11-01 19:54:11 -07001536 if (v >= voltage_max)
1537 v = voltage_max | DP_TRAIN_MAX_SWING_REACHED;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001538
Keith Packard1a2eb462011-11-16 16:26:07 -08001539 preemph_max = intel_dp_pre_emphasis_max(intel_dp, v);
1540 if (p >= preemph_max)
1541 p = preemph_max | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001542
1543 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07001544 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001545}
1546
1547static uint32_t
Keith Packard93f62da2011-11-01 19:45:03 -07001548intel_dp_signal_levels(uint8_t train_set)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001549{
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001550 uint32_t signal_levels = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001551
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001552 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001553 case DP_TRAIN_VOLTAGE_SWING_400:
1554 default:
1555 signal_levels |= DP_VOLTAGE_0_4;
1556 break;
1557 case DP_TRAIN_VOLTAGE_SWING_600:
1558 signal_levels |= DP_VOLTAGE_0_6;
1559 break;
1560 case DP_TRAIN_VOLTAGE_SWING_800:
1561 signal_levels |= DP_VOLTAGE_0_8;
1562 break;
1563 case DP_TRAIN_VOLTAGE_SWING_1200:
1564 signal_levels |= DP_VOLTAGE_1_2;
1565 break;
1566 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001567 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001568 case DP_TRAIN_PRE_EMPHASIS_0:
1569 default:
1570 signal_levels |= DP_PRE_EMPHASIS_0;
1571 break;
1572 case DP_TRAIN_PRE_EMPHASIS_3_5:
1573 signal_levels |= DP_PRE_EMPHASIS_3_5;
1574 break;
1575 case DP_TRAIN_PRE_EMPHASIS_6:
1576 signal_levels |= DP_PRE_EMPHASIS_6;
1577 break;
1578 case DP_TRAIN_PRE_EMPHASIS_9_5:
1579 signal_levels |= DP_PRE_EMPHASIS_9_5;
1580 break;
1581 }
1582 return signal_levels;
1583}
1584
Zhenyu Wange3421a12010-04-08 09:43:27 +08001585/* Gen6's DP voltage swing and pre-emphasis control */
1586static uint32_t
1587intel_gen6_edp_signal_levels(uint8_t train_set)
1588{
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001589 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1590 DP_TRAIN_PRE_EMPHASIS_MASK);
1591 switch (signal_levels) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001592 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001593 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
1594 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
1595 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
1596 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001597 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001598 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
1599 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001600 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001601 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
1602 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001603 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001604 case DP_TRAIN_VOLTAGE_SWING_1200 | DP_TRAIN_PRE_EMPHASIS_0:
1605 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001606 default:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08001607 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
1608 "0x%x\n", signal_levels);
1609 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08001610 }
1611}
1612
Keith Packard1a2eb462011-11-16 16:26:07 -08001613/* Gen7's DP voltage swing and pre-emphasis control */
1614static uint32_t
1615intel_gen7_edp_signal_levels(uint8_t train_set)
1616{
1617 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1618 DP_TRAIN_PRE_EMPHASIS_MASK);
1619 switch (signal_levels) {
1620 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1621 return EDP_LINK_TRAIN_400MV_0DB_IVB;
1622 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
1623 return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
1624 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1625 return EDP_LINK_TRAIN_400MV_6DB_IVB;
1626
1627 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
1628 return EDP_LINK_TRAIN_600MV_0DB_IVB;
1629 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1630 return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
1631
1632 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1633 return EDP_LINK_TRAIN_800MV_0DB_IVB;
1634 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
1635 return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
1636
1637 default:
1638 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
1639 "0x%x\n", signal_levels);
1640 return EDP_LINK_TRAIN_500MV_0DB_IVB;
1641 }
1642}
1643
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001644/* Gen7.5's (HSW) DP voltage swing and pre-emphasis control */
1645static uint32_t
1646intel_dp_signal_levels_hsw(uint8_t train_set)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001647{
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001648 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1649 DP_TRAIN_PRE_EMPHASIS_MASK);
1650 switch (signal_levels) {
1651 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
1652 return DDI_BUF_EMP_400MV_0DB_HSW;
1653 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
1654 return DDI_BUF_EMP_400MV_3_5DB_HSW;
1655 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
1656 return DDI_BUF_EMP_400MV_6DB_HSW;
1657 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_9_5:
1658 return DDI_BUF_EMP_400MV_9_5DB_HSW;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001659
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001660 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
1661 return DDI_BUF_EMP_600MV_0DB_HSW;
1662 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
1663 return DDI_BUF_EMP_600MV_3_5DB_HSW;
1664 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
1665 return DDI_BUF_EMP_600MV_6DB_HSW;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001666
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001667 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
1668 return DDI_BUF_EMP_800MV_0DB_HSW;
1669 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
1670 return DDI_BUF_EMP_800MV_3_5DB_HSW;
1671 default:
1672 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
1673 "0x%x\n", signal_levels);
1674 return DDI_BUF_EMP_400MV_0DB_HSW;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001675 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001676}
1677
1678static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001679intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001680 uint32_t dp_reg_value,
Chris Wilson58e10eb2010-10-03 10:56:11 +01001681 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001682{
Paulo Zanoni174edf12012-10-26 19:05:50 -02001683 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1684 struct drm_device *dev = intel_dig_port->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001685 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni174edf12012-10-26 19:05:50 -02001686 enum port port = intel_dig_port->port;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001687 int ret;
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001688 uint32_t temp;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001689
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001690 if (IS_HASWELL(dev)) {
Paulo Zanoni174edf12012-10-26 19:05:50 -02001691 temp = I915_READ(DP_TP_CTL(port));
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001692
1693 if (dp_train_pat & DP_LINK_SCRAMBLING_DISABLE)
1694 temp |= DP_TP_CTL_SCRAMBLE_DISABLE;
1695 else
1696 temp &= ~DP_TP_CTL_SCRAMBLE_DISABLE;
1697
1698 temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
1699 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
1700 case DP_TRAINING_PATTERN_DISABLE:
1701 temp |= DP_TP_CTL_LINK_TRAIN_IDLE;
Paulo Zanoni174edf12012-10-26 19:05:50 -02001702 I915_WRITE(DP_TP_CTL(port), temp);
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001703
Paulo Zanoni174edf12012-10-26 19:05:50 -02001704 if (wait_for((I915_READ(DP_TP_STATUS(port)) &
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001705 DP_TP_STATUS_IDLE_DONE), 1))
1706 DRM_ERROR("Timed out waiting for DP idle patterns\n");
1707
1708 temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
1709 temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
1710
1711 break;
1712 case DP_TRAINING_PATTERN_1:
1713 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
1714 break;
1715 case DP_TRAINING_PATTERN_2:
1716 temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
1717 break;
1718 case DP_TRAINING_PATTERN_3:
1719 temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
1720 break;
1721 }
Paulo Zanoni174edf12012-10-26 19:05:50 -02001722 I915_WRITE(DP_TP_CTL(port), temp);
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001723
1724 } else if (HAS_PCH_CPT(dev) &&
1725 (IS_GEN7(dev) || !is_cpu_edp(intel_dp))) {
Paulo Zanoni47ea7542012-07-17 16:55:16 -03001726 dp_reg_value &= ~DP_LINK_TRAIN_MASK_CPT;
1727
1728 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
1729 case DP_TRAINING_PATTERN_DISABLE:
1730 dp_reg_value |= DP_LINK_TRAIN_OFF_CPT;
1731 break;
1732 case DP_TRAINING_PATTERN_1:
1733 dp_reg_value |= DP_LINK_TRAIN_PAT_1_CPT;
1734 break;
1735 case DP_TRAINING_PATTERN_2:
1736 dp_reg_value |= DP_LINK_TRAIN_PAT_2_CPT;
1737 break;
1738 case DP_TRAINING_PATTERN_3:
1739 DRM_ERROR("DP training pattern 3 not supported\n");
1740 dp_reg_value |= DP_LINK_TRAIN_PAT_2_CPT;
1741 break;
1742 }
1743
1744 } else {
1745 dp_reg_value &= ~DP_LINK_TRAIN_MASK;
1746
1747 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
1748 case DP_TRAINING_PATTERN_DISABLE:
1749 dp_reg_value |= DP_LINK_TRAIN_OFF;
1750 break;
1751 case DP_TRAINING_PATTERN_1:
1752 dp_reg_value |= DP_LINK_TRAIN_PAT_1;
1753 break;
1754 case DP_TRAINING_PATTERN_2:
1755 dp_reg_value |= DP_LINK_TRAIN_PAT_2;
1756 break;
1757 case DP_TRAINING_PATTERN_3:
1758 DRM_ERROR("DP training pattern 3 not supported\n");
1759 dp_reg_value |= DP_LINK_TRAIN_PAT_2;
1760 break;
1761 }
1762 }
1763
Chris Wilsonea5b2132010-08-04 13:50:23 +01001764 I915_WRITE(intel_dp->output_reg, dp_reg_value);
1765 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001766
Chris Wilsonea5b2132010-08-04 13:50:23 +01001767 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001768 DP_TRAINING_PATTERN_SET,
1769 dp_train_pat);
1770
Paulo Zanoni47ea7542012-07-17 16:55:16 -03001771 if ((dp_train_pat & DP_TRAINING_PATTERN_MASK) !=
1772 DP_TRAINING_PATTERN_DISABLE) {
1773 ret = intel_dp_aux_native_write(intel_dp,
1774 DP_TRAINING_LANE0_SET,
1775 intel_dp->train_set,
1776 intel_dp->lane_count);
1777 if (ret != intel_dp->lane_count)
1778 return false;
1779 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001780
1781 return true;
1782}
1783
Jesse Barnes33a34e42010-09-08 12:42:02 -07001784/* Enable corresponding port and start training pattern 1 */
Paulo Zanonic19b0662012-10-15 15:51:41 -03001785void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001786intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001787{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02001788 struct drm_encoder *encoder = &dp_to_dig_port(intel_dp)->base.base;
Paulo Zanonic19b0662012-10-15 15:51:41 -03001789 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001790 int i;
1791 uint8_t voltage;
1792 bool clock_recovery = false;
Keith Packardcdb0e952011-11-01 20:00:06 -07001793 int voltage_tries, loop_tries;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001794 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001795
Paulo Zanonic19b0662012-10-15 15:51:41 -03001796 if (IS_HASWELL(dev))
1797 intel_ddi_prepare_link_retrain(encoder);
1798
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001799 /* Write the link configuration data */
1800 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET,
1801 intel_dp->link_configuration,
1802 DP_LINK_CONFIGURATION_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001803
1804 DP |= DP_PORT_EN;
Keith Packard1a2eb462011-11-16 16:26:07 -08001805
Jesse Barnes33a34e42010-09-08 12:42:02 -07001806 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001807 voltage = 0xff;
Keith Packardcdb0e952011-11-01 20:00:06 -07001808 voltage_tries = 0;
1809 loop_tries = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001810 clock_recovery = false;
1811 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001812 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Keith Packard93f62da2011-11-01 19:45:03 -07001813 uint8_t link_status[DP_LINK_STATUS_SIZE];
Zhenyu Wange3421a12010-04-08 09:43:27 +08001814 uint32_t signal_levels;
Keith Packard417e8222011-11-01 19:54:11 -07001815
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001816 if (IS_HASWELL(dev)) {
1817 signal_levels = intel_dp_signal_levels_hsw(
1818 intel_dp->train_set[0]);
1819 DP = (DP & ~DDI_BUF_EMP_MASK) | signal_levels;
1820 } else if (IS_GEN7(dev) && is_cpu_edp(intel_dp) && !IS_VALLEYVIEW(dev)) {
Keith Packard1a2eb462011-11-16 16:26:07 -08001821 signal_levels = intel_gen7_edp_signal_levels(intel_dp->train_set[0]);
1822 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_IVB) | signal_levels;
1823 } else if (IS_GEN6(dev) && is_cpu_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001824 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001825 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1826 } else {
Keith Packard93f62da2011-11-01 19:45:03 -07001827 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001828 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1829 }
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001830 DRM_DEBUG_KMS("training pattern 1 signal levels %08x\n",
1831 signal_levels);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001832
Daniel Vettera7c96552012-10-18 10:15:30 +02001833 /* Set training pattern 1 */
Paulo Zanoni47ea7542012-07-17 16:55:16 -03001834 if (!intel_dp_set_link_train(intel_dp, DP,
Adam Jackson81055852011-07-21 17:48:37 -04001835 DP_TRAINING_PATTERN_1 |
1836 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001837 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001838
Daniel Vettera7c96552012-10-18 10:15:30 +02001839 drm_dp_link_train_clock_recovery_delay(intel_dp->dpcd);
Keith Packard93f62da2011-11-01 19:45:03 -07001840 if (!intel_dp_get_link_status(intel_dp, link_status)) {
1841 DRM_ERROR("failed to get link status\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001842 break;
Keith Packard93f62da2011-11-01 19:45:03 -07001843 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001844
Daniel Vetter01916272012-10-18 10:15:25 +02001845 if (drm_dp_clock_recovery_ok(link_status, intel_dp->lane_count)) {
Keith Packard93f62da2011-11-01 19:45:03 -07001846 DRM_DEBUG_KMS("clock recovery OK\n");
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001847 clock_recovery = true;
1848 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001849 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001850
1851 /* Check to see if we've tried the max voltage */
1852 for (i = 0; i < intel_dp->lane_count; i++)
1853 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
1854 break;
Paulo Zanoni0d710682012-06-29 16:03:34 -03001855 if (i == intel_dp->lane_count && voltage_tries == 5) {
Daniel Vetterb06fbda2012-10-16 09:50:25 +02001856 ++loop_tries;
1857 if (loop_tries == 5) {
Keith Packardcdb0e952011-11-01 20:00:06 -07001858 DRM_DEBUG_KMS("too many full retries, give up\n");
1859 break;
1860 }
1861 memset(intel_dp->train_set, 0, 4);
1862 voltage_tries = 0;
1863 continue;
1864 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001865
1866 /* Check to see if we've tried the same voltage 5 times */
Daniel Vetterb06fbda2012-10-16 09:50:25 +02001867 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
Chris Wilson24773672012-09-26 16:48:30 +01001868 ++voltage_tries;
Daniel Vetterb06fbda2012-10-16 09:50:25 +02001869 if (voltage_tries == 5) {
1870 DRM_DEBUG_KMS("too many voltage retries, give up\n");
1871 break;
1872 }
1873 } else
1874 voltage_tries = 0;
1875 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001876
1877 /* Compute new intel_dp->train_set as requested by target */
Keith Packard93f62da2011-11-01 19:45:03 -07001878 intel_get_adjust_train(intel_dp, link_status);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001879 }
1880
Jesse Barnes33a34e42010-09-08 12:42:02 -07001881 intel_dp->DP = DP;
1882}
1883
Paulo Zanonic19b0662012-10-15 15:51:41 -03001884void
Jesse Barnes33a34e42010-09-08 12:42:02 -07001885intel_dp_complete_link_train(struct intel_dp *intel_dp)
1886{
Paulo Zanoni30add222012-10-26 19:05:45 -02001887 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -07001888 bool channel_eq = false;
Jesse Barnes37f80972011-01-05 14:45:24 -08001889 int tries, cr_tries;
Jesse Barnes33a34e42010-09-08 12:42:02 -07001890 uint32_t DP = intel_dp->DP;
1891
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001892 /* channel equalization */
1893 tries = 0;
Jesse Barnes37f80972011-01-05 14:45:24 -08001894 cr_tries = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001895 channel_eq = false;
1896 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001897 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Zhenyu Wange3421a12010-04-08 09:43:27 +08001898 uint32_t signal_levels;
Keith Packard93f62da2011-11-01 19:45:03 -07001899 uint8_t link_status[DP_LINK_STATUS_SIZE];
Zhenyu Wange3421a12010-04-08 09:43:27 +08001900
Jesse Barnes37f80972011-01-05 14:45:24 -08001901 if (cr_tries > 5) {
1902 DRM_ERROR("failed to train DP, aborting\n");
1903 intel_dp_link_down(intel_dp);
1904 break;
1905 }
1906
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001907 if (IS_HASWELL(dev)) {
1908 signal_levels = intel_dp_signal_levels_hsw(intel_dp->train_set[0]);
1909 DP = (DP & ~DDI_BUF_EMP_MASK) | signal_levels;
1910 } else if (IS_GEN7(dev) && is_cpu_edp(intel_dp) && !IS_VALLEYVIEW(dev)) {
Keith Packard1a2eb462011-11-16 16:26:07 -08001911 signal_levels = intel_gen7_edp_signal_levels(intel_dp->train_set[0]);
1912 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_IVB) | signal_levels;
1913 } else if (IS_GEN6(dev) && is_cpu_edp(intel_dp)) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07001914 signal_levels = intel_gen6_edp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001915 DP = (DP & ~EDP_LINK_TRAIN_VOL_EMP_MASK_SNB) | signal_levels;
1916 } else {
Keith Packard93f62da2011-11-01 19:45:03 -07001917 signal_levels = intel_dp_signal_levels(intel_dp->train_set[0]);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001918 DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
1919 }
1920
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001921 /* channel eq pattern */
Paulo Zanoni47ea7542012-07-17 16:55:16 -03001922 if (!intel_dp_set_link_train(intel_dp, DP,
Adam Jackson81055852011-07-21 17:48:37 -04001923 DP_TRAINING_PATTERN_2 |
1924 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001925 break;
1926
Daniel Vettera7c96552012-10-18 10:15:30 +02001927 drm_dp_link_train_channel_eq_delay(intel_dp->dpcd);
Keith Packard93f62da2011-11-01 19:45:03 -07001928 if (!intel_dp_get_link_status(intel_dp, link_status))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001929 break;
Jesse Barnes869184a2010-10-07 16:01:22 -07001930
Jesse Barnes37f80972011-01-05 14:45:24 -08001931 /* Make sure clock is still ok */
Daniel Vetter01916272012-10-18 10:15:25 +02001932 if (!drm_dp_clock_recovery_ok(link_status, intel_dp->lane_count)) {
Jesse Barnes37f80972011-01-05 14:45:24 -08001933 intel_dp_start_link_train(intel_dp);
1934 cr_tries++;
1935 continue;
1936 }
1937
Daniel Vetter1ffdff12012-10-18 10:15:24 +02001938 if (drm_dp_channel_eq_ok(link_status, intel_dp->lane_count)) {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001939 channel_eq = true;
1940 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001941 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001942
Jesse Barnes37f80972011-01-05 14:45:24 -08001943 /* Try 5 times, then try clock recovery if that fails */
1944 if (tries > 5) {
1945 intel_dp_link_down(intel_dp);
1946 intel_dp_start_link_train(intel_dp);
1947 tries = 0;
1948 cr_tries++;
1949 continue;
1950 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001951
1952 /* Compute new intel_dp->train_set as requested by target */
Keith Packard93f62da2011-11-01 19:45:03 -07001953 intel_get_adjust_train(intel_dp, link_status);
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001954 ++tries;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001955 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00001956
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001957 if (channel_eq)
1958 DRM_DEBUG_KMS("Channel EQ done. DP Training successfull\n");
1959
Paulo Zanoni47ea7542012-07-17 16:55:16 -03001960 intel_dp_set_link_train(intel_dp, DP, DP_TRAINING_PATTERN_DISABLE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001961}
1962
1963static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01001964intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001965{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02001966 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1967 struct drm_device *dev = intel_dig_port->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001968 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001969 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001970
Paulo Zanonic19b0662012-10-15 15:51:41 -03001971 /*
1972 * DDI code has a strict mode set sequence and we should try to respect
1973 * it, otherwise we might hang the machine in many different ways. So we
1974 * really should be disabling the port only on a complete crtc_disable
1975 * sequence. This function is just called under two conditions on DDI
1976 * code:
1977 * - Link train failed while doing crtc_enable, and on this case we
1978 * really should respect the mode set sequence and wait for a
1979 * crtc_disable.
1980 * - Someone turned the monitor off and intel_dp_check_link_status
1981 * called us. We don't need to disable the whole port on this case, so
1982 * when someone turns the monitor on again,
1983 * intel_ddi_prepare_link_retrain will take care of redoing the link
1984 * train.
1985 */
1986 if (IS_HASWELL(dev))
1987 return;
1988
Daniel Vetter0c33d8d2012-09-06 22:15:43 +02001989 if (WARN_ON((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0))
Chris Wilson1b39d6f2010-12-06 11:20:45 +00001990 return;
1991
Zhao Yakui28c97732009-10-09 11:39:41 +08001992 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001993
Keith Packard1a2eb462011-11-16 16:26:07 -08001994 if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || !is_cpu_edp(intel_dp))) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08001995 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001996 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08001997 } else {
1998 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001999 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08002000 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01002001 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002002
Chris Wilsonfe255d02010-09-11 21:37:48 +01002003 msleep(17);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002004
Daniel Vetter493a7082012-05-30 12:31:56 +02002005 if (HAS_PCH_IBX(dev) &&
Chris Wilson1b39d6f2010-12-06 11:20:45 +00002006 I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002007 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
Chris Wilson31acbcc2011-04-17 06:38:35 +01002008
Eric Anholt5bddd172010-11-18 09:32:59 +08002009 /* Hardware workaround: leaving our transcoder select
2010 * set to transcoder B while it's off will prevent the
2011 * corresponding HDMI output on transcoder A.
2012 *
2013 * Combine this with another hardware workaround:
2014 * transcoder select bit can only be cleared while the
2015 * port is enabled.
2016 */
2017 DP &= ~DP_PIPEB_SELECT;
2018 I915_WRITE(intel_dp->output_reg, DP);
2019
2020 /* Changes to enable or select take place the vblank
2021 * after being written.
2022 */
Chris Wilson31acbcc2011-04-17 06:38:35 +01002023 if (crtc == NULL) {
2024 /* We can arrive here never having been attached
2025 * to a CRTC, for instance, due to inheriting
2026 * random state from the BIOS.
2027 *
2028 * If the pipe is not running, play safe and
2029 * wait for the clocks to stabilise before
2030 * continuing.
2031 */
2032 POSTING_READ(intel_dp->output_reg);
2033 msleep(50);
2034 } else
2035 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
Eric Anholt5bddd172010-11-18 09:32:59 +08002036 }
2037
Wu Fengguang832afda2011-12-09 20:42:21 +08002038 DP &= ~DP_AUDIO_OUTPUT_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01002039 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
2040 POSTING_READ(intel_dp->output_reg);
Keith Packardf01eca22011-09-28 16:48:10 -07002041 msleep(intel_dp->panel_power_down_delay);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002042}
2043
Keith Packard26d61aa2011-07-25 20:01:09 -07002044static bool
2045intel_dp_get_dpcd(struct intel_dp *intel_dp)
Keith Packard92fd8fd2011-07-25 19:50:10 -07002046{
Keith Packard92fd8fd2011-07-25 19:50:10 -07002047 if (intel_dp_aux_native_read_retry(intel_dp, 0x000, intel_dp->dpcd,
Adam Jacksonedb39242012-09-18 10:58:49 -04002048 sizeof(intel_dp->dpcd)) == 0)
2049 return false; /* aux transfer failed */
Keith Packard92fd8fd2011-07-25 19:50:10 -07002050
Adam Jacksonedb39242012-09-18 10:58:49 -04002051 if (intel_dp->dpcd[DP_DPCD_REV] == 0)
2052 return false; /* DPCD not present */
2053
2054 if (!(intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
2055 DP_DWN_STRM_PORT_PRESENT))
2056 return true; /* native DP sink */
2057
2058 if (intel_dp->dpcd[DP_DPCD_REV] == 0x10)
2059 return true; /* no per-port downstream info */
2060
2061 if (intel_dp_aux_native_read_retry(intel_dp, DP_DOWNSTREAM_PORT_0,
2062 intel_dp->downstream_ports,
2063 DP_MAX_DOWNSTREAM_PORTS) == 0)
2064 return false; /* downstream port status fetch failed */
2065
2066 return true;
Keith Packard92fd8fd2011-07-25 19:50:10 -07002067}
2068
Adam Jackson0d198322012-05-14 16:05:47 -04002069static void
2070intel_dp_probe_oui(struct intel_dp *intel_dp)
2071{
2072 u8 buf[3];
2073
2074 if (!(intel_dp->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
2075 return;
2076
Daniel Vetter351cfc32012-06-12 13:20:47 +02002077 ironlake_edp_panel_vdd_on(intel_dp);
2078
Adam Jackson0d198322012-05-14 16:05:47 -04002079 if (intel_dp_aux_native_read_retry(intel_dp, DP_SINK_OUI, buf, 3))
2080 DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
2081 buf[0], buf[1], buf[2]);
2082
2083 if (intel_dp_aux_native_read_retry(intel_dp, DP_BRANCH_OUI, buf, 3))
2084 DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
2085 buf[0], buf[1], buf[2]);
Daniel Vetter351cfc32012-06-12 13:20:47 +02002086
2087 ironlake_edp_panel_vdd_off(intel_dp, false);
Adam Jackson0d198322012-05-14 16:05:47 -04002088}
2089
Jesse Barnesa60f0e32011-10-20 15:09:17 -07002090static bool
2091intel_dp_get_sink_irq(struct intel_dp *intel_dp, u8 *sink_irq_vector)
2092{
2093 int ret;
2094
2095 ret = intel_dp_aux_native_read_retry(intel_dp,
2096 DP_DEVICE_SERVICE_IRQ_VECTOR,
2097 sink_irq_vector, 1);
2098 if (!ret)
2099 return false;
2100
2101 return true;
2102}
2103
2104static void
2105intel_dp_handle_test_request(struct intel_dp *intel_dp)
2106{
2107 /* NAK by default */
Daniel Vetter9324cf72012-10-20 21:13:05 +02002108 intel_dp_aux_native_write_1(intel_dp, DP_TEST_RESPONSE, DP_TEST_NAK);
Jesse Barnesa60f0e32011-10-20 15:09:17 -07002109}
2110
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002111/*
2112 * According to DP spec
2113 * 5.1.2:
2114 * 1. Read DPCD
2115 * 2. Configure link according to Receiver Capabilities
2116 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
2117 * 4. Check link status on receipt of hot-plug interrupt
2118 */
2119
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002120void
Chris Wilsonea5b2132010-08-04 13:50:23 +01002121intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002122{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002123 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
Jesse Barnesa60f0e32011-10-20 15:09:17 -07002124 u8 sink_irq_vector;
Keith Packard93f62da2011-11-01 19:45:03 -07002125 u8 link_status[DP_LINK_STATUS_SIZE];
Jesse Barnesa60f0e32011-10-20 15:09:17 -07002126
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002127 if (!intel_encoder->connectors_active)
Keith Packardd2b996a2011-07-25 22:37:51 -07002128 return;
Jesse Barnes59cd09e2011-07-07 11:10:59 -07002129
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002130 if (WARN_ON(!intel_encoder->base.crtc))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002131 return;
2132
Keith Packard92fd8fd2011-07-25 19:50:10 -07002133 /* Try to read receiver status if the link appears to be up */
Keith Packard93f62da2011-11-01 19:45:03 -07002134 if (!intel_dp_get_link_status(intel_dp, link_status)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002135 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002136 return;
2137 }
2138
Keith Packard92fd8fd2011-07-25 19:50:10 -07002139 /* Now read the DPCD to see if it's actually running */
Keith Packard26d61aa2011-07-25 20:01:09 -07002140 if (!intel_dp_get_dpcd(intel_dp)) {
Jesse Barnes59cd09e2011-07-07 11:10:59 -07002141 intel_dp_link_down(intel_dp);
2142 return;
2143 }
2144
Jesse Barnesa60f0e32011-10-20 15:09:17 -07002145 /* Try to read the source of the interrupt */
2146 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
2147 intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
2148 /* Clear interrupt source */
2149 intel_dp_aux_native_write_1(intel_dp,
2150 DP_DEVICE_SERVICE_IRQ_VECTOR,
2151 sink_irq_vector);
2152
2153 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
2154 intel_dp_handle_test_request(intel_dp);
2155 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
2156 DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
2157 }
2158
Daniel Vetter1ffdff12012-10-18 10:15:24 +02002159 if (!drm_dp_channel_eq_ok(link_status, intel_dp->lane_count)) {
Keith Packard92fd8fd2011-07-25 19:50:10 -07002160 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002161 drm_get_encoder_name(&intel_encoder->base));
Jesse Barnes33a34e42010-09-08 12:42:02 -07002162 intel_dp_start_link_train(intel_dp);
2163 intel_dp_complete_link_train(intel_dp);
2164 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002165}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002166
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002167/* XXX this is probably wrong for multiple downstream ports */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002168static enum drm_connector_status
Keith Packard26d61aa2011-07-25 20:01:09 -07002169intel_dp_detect_dpcd(struct intel_dp *intel_dp)
Adam Jackson71ba90002011-07-12 17:38:04 -04002170{
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002171 uint8_t *dpcd = intel_dp->dpcd;
2172 bool hpd;
2173 uint8_t type;
2174
2175 if (!intel_dp_get_dpcd(intel_dp))
2176 return connector_status_disconnected;
2177
2178 /* if there's no downstream port, we're done */
2179 if (!(dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT))
Keith Packard26d61aa2011-07-25 20:01:09 -07002180 return connector_status_connected;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002181
2182 /* If we're HPD-aware, SINK_COUNT changes dynamically */
2183 hpd = !!(intel_dp->downstream_ports[0] & DP_DS_PORT_HPD);
2184 if (hpd) {
Adam Jackson23235172012-09-20 16:42:45 -04002185 uint8_t reg;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002186 if (!intel_dp_aux_native_read_retry(intel_dp, DP_SINK_COUNT,
Adam Jackson23235172012-09-20 16:42:45 -04002187 &reg, 1))
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002188 return connector_status_unknown;
Adam Jackson23235172012-09-20 16:42:45 -04002189 return DP_GET_SINK_COUNT(reg) ? connector_status_connected
2190 : connector_status_disconnected;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002191 }
2192
2193 /* If no HPD, poke DDC gently */
2194 if (drm_probe_ddc(&intel_dp->adapter))
2195 return connector_status_connected;
2196
2197 /* Well we tried, say unknown for unreliable port types */
2198 type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
2199 if (type == DP_DS_PORT_TYPE_VGA || type == DP_DS_PORT_TYPE_NON_EDID)
2200 return connector_status_unknown;
2201
2202 /* Anything else is out of spec, warn and ignore */
2203 DRM_DEBUG_KMS("Broken DP branch device, ignoring\n");
Keith Packard26d61aa2011-07-25 20:01:09 -07002204 return connector_status_disconnected;
Adam Jackson71ba90002011-07-12 17:38:04 -04002205}
2206
2207static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002208ironlake_dp_detect(struct intel_dp *intel_dp)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002209{
Paulo Zanoni30add222012-10-26 19:05:45 -02002210 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002211 enum drm_connector_status status;
2212
Chris Wilsonfe16d942011-02-12 10:29:38 +00002213 /* Can't disconnect eDP, but you can close the lid... */
2214 if (is_edp(intel_dp)) {
Paulo Zanoni30add222012-10-26 19:05:45 -02002215 status = intel_panel_detect(dev);
Chris Wilsonfe16d942011-02-12 10:29:38 +00002216 if (status == connector_status_unknown)
2217 status = connector_status_connected;
2218 return status;
2219 }
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07002220
Keith Packard26d61aa2011-07-25 20:01:09 -07002221 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002222}
2223
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002224static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002225g4x_dp_detect(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002226{
Paulo Zanoni30add222012-10-26 19:05:45 -02002227 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002228 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson10f76a32012-05-11 18:01:32 +01002229 uint32_t bit;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002230
Chris Wilsonea5b2132010-08-04 13:50:23 +01002231 switch (intel_dp->output_reg) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002232 case DP_B:
Chris Wilson10f76a32012-05-11 18:01:32 +01002233 bit = DPB_HOTPLUG_LIVE_STATUS;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002234 break;
2235 case DP_C:
Chris Wilson10f76a32012-05-11 18:01:32 +01002236 bit = DPC_HOTPLUG_LIVE_STATUS;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002237 break;
2238 case DP_D:
Chris Wilson10f76a32012-05-11 18:01:32 +01002239 bit = DPD_HOTPLUG_LIVE_STATUS;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002240 break;
2241 default:
2242 return connector_status_unknown;
2243 }
2244
Chris Wilson10f76a32012-05-11 18:01:32 +01002245 if ((I915_READ(PORT_HOTPLUG_STAT) & bit) == 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002246 return connector_status_disconnected;
2247
Keith Packard26d61aa2011-07-25 20:01:09 -07002248 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002249}
2250
Keith Packard8c241fe2011-09-28 16:38:44 -07002251static struct edid *
2252intel_dp_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
2253{
Jani Nikula9cd300e2012-10-19 14:51:52 +03002254 struct intel_connector *intel_connector = to_intel_connector(connector);
Keith Packard8c241fe2011-09-28 16:38:44 -07002255
Jani Nikula9cd300e2012-10-19 14:51:52 +03002256 /* use cached edid if we have one */
2257 if (intel_connector->edid) {
2258 struct edid *edid;
2259 int size;
2260
2261 /* invalid edid */
2262 if (IS_ERR(intel_connector->edid))
Jesse Barnesd6f24d02012-06-14 15:28:33 -04002263 return NULL;
2264
Jani Nikula9cd300e2012-10-19 14:51:52 +03002265 size = (intel_connector->edid->extensions + 1) * EDID_LENGTH;
Jesse Barnesd6f24d02012-06-14 15:28:33 -04002266 edid = kmalloc(size, GFP_KERNEL);
2267 if (!edid)
2268 return NULL;
2269
Jani Nikula9cd300e2012-10-19 14:51:52 +03002270 memcpy(edid, intel_connector->edid, size);
Jesse Barnesd6f24d02012-06-14 15:28:33 -04002271 return edid;
2272 }
2273
Jani Nikula9cd300e2012-10-19 14:51:52 +03002274 return drm_get_edid(connector, adapter);
Keith Packard8c241fe2011-09-28 16:38:44 -07002275}
2276
2277static int
2278intel_dp_get_edid_modes(struct drm_connector *connector, struct i2c_adapter *adapter)
2279{
Jani Nikula9cd300e2012-10-19 14:51:52 +03002280 struct intel_connector *intel_connector = to_intel_connector(connector);
Keith Packard8c241fe2011-09-28 16:38:44 -07002281
Jani Nikula9cd300e2012-10-19 14:51:52 +03002282 /* use cached edid if we have one */
2283 if (intel_connector->edid) {
2284 /* invalid edid */
2285 if (IS_ERR(intel_connector->edid))
2286 return 0;
2287
2288 return intel_connector_update_modes(connector,
2289 intel_connector->edid);
Jesse Barnesd6f24d02012-06-14 15:28:33 -04002290 }
2291
Jani Nikula9cd300e2012-10-19 14:51:52 +03002292 return intel_ddc_get_modes(connector, adapter);
Keith Packard8c241fe2011-09-28 16:38:44 -07002293}
2294
2295
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002296/**
2297 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
2298 *
2299 * \return true if DP port is connected.
2300 * \return false if DP port is disconnected.
2301 */
2302static enum drm_connector_status
2303intel_dp_detect(struct drm_connector *connector, bool force)
2304{
2305 struct intel_dp *intel_dp = intel_attached_dp(connector);
Paulo Zanonid63885d2012-10-26 19:05:49 -02002306 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2307 struct intel_encoder *intel_encoder = &intel_dig_port->base;
Paulo Zanonifa90ece2012-10-26 19:05:44 -02002308 struct drm_device *dev = connector->dev;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002309 enum drm_connector_status status;
2310 struct edid *edid = NULL;
Jani Nikula898076e2012-10-25 10:58:10 +03002311 char dpcd_hex_dump[sizeof(intel_dp->dpcd) * 3];
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002312
2313 intel_dp->has_audio = false;
2314
2315 if (HAS_PCH_SPLIT(dev))
2316 status = ironlake_dp_detect(intel_dp);
2317 else
2318 status = g4x_dp_detect(intel_dp);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04002319
Jani Nikula898076e2012-10-25 10:58:10 +03002320 hex_dump_to_buffer(intel_dp->dpcd, sizeof(intel_dp->dpcd),
2321 32, 1, dpcd_hex_dump, sizeof(dpcd_hex_dump), false);
2322 DRM_DEBUG_KMS("DPCD: %s\n", dpcd_hex_dump);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04002323
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002324 if (status != connector_status_connected)
2325 return status;
2326
Adam Jackson0d198322012-05-14 16:05:47 -04002327 intel_dp_probe_oui(intel_dp);
2328
Daniel Vetterc3e5f672012-02-23 17:14:47 +01002329 if (intel_dp->force_audio != HDMI_AUDIO_AUTO) {
2330 intel_dp->has_audio = (intel_dp->force_audio == HDMI_AUDIO_ON);
Chris Wilsonf6849602010-09-19 09:29:33 +01002331 } else {
Keith Packard8c241fe2011-09-28 16:38:44 -07002332 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
Chris Wilsonf6849602010-09-19 09:29:33 +01002333 if (edid) {
2334 intel_dp->has_audio = drm_detect_monitor_audio(edid);
Chris Wilsonf6849602010-09-19 09:29:33 +01002335 kfree(edid);
2336 }
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002337 }
2338
Paulo Zanonid63885d2012-10-26 19:05:49 -02002339 if (intel_encoder->type != INTEL_OUTPUT_EDP)
2340 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002341 return connector_status_connected;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002342}
2343
2344static int intel_dp_get_modes(struct drm_connector *connector)
2345{
Chris Wilsondf0e9242010-09-09 16:20:55 +01002346 struct intel_dp *intel_dp = intel_attached_dp(connector);
Jani Nikuladd06f902012-10-19 14:51:50 +03002347 struct intel_connector *intel_connector = to_intel_connector(connector);
Paulo Zanonifa90ece2012-10-26 19:05:44 -02002348 struct drm_device *dev = connector->dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002349 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002350
2351 /* We should parse the EDID data and find out if it has an audio sink
2352 */
2353
Keith Packard8c241fe2011-09-28 16:38:44 -07002354 ret = intel_dp_get_edid_modes(connector, &intel_dp->adapter);
Jani Nikulaf8779fd2012-10-19 14:51:48 +03002355 if (ret)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002356 return ret;
2357
Jani Nikulaf8779fd2012-10-19 14:51:48 +03002358 /* if eDP has no EDID, fall back to fixed mode */
Jani Nikuladd06f902012-10-19 14:51:50 +03002359 if (is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
Jani Nikulaf8779fd2012-10-19 14:51:48 +03002360 struct drm_display_mode *mode;
Jani Nikuladd06f902012-10-19 14:51:50 +03002361 mode = drm_mode_duplicate(dev,
2362 intel_connector->panel.fixed_mode);
Jani Nikulaf8779fd2012-10-19 14:51:48 +03002363 if (mode) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002364 drm_mode_probed_add(connector, mode);
2365 return 1;
2366 }
2367 }
2368 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002369}
2370
Chris Wilson1aad7ac2011-02-09 18:46:58 +00002371static bool
2372intel_dp_detect_audio(struct drm_connector *connector)
2373{
2374 struct intel_dp *intel_dp = intel_attached_dp(connector);
2375 struct edid *edid;
2376 bool has_audio = false;
2377
Keith Packard8c241fe2011-09-28 16:38:44 -07002378 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
Chris Wilson1aad7ac2011-02-09 18:46:58 +00002379 if (edid) {
2380 has_audio = drm_detect_monitor_audio(edid);
Chris Wilson1aad7ac2011-02-09 18:46:58 +00002381 kfree(edid);
2382 }
2383
2384 return has_audio;
2385}
2386
Chris Wilsonf6849602010-09-19 09:29:33 +01002387static int
2388intel_dp_set_property(struct drm_connector *connector,
2389 struct drm_property *property,
2390 uint64_t val)
2391{
Chris Wilsone953fd72011-02-21 22:23:52 +00002392 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Yuly Novikov53b41832012-10-26 12:04:00 +03002393 struct intel_connector *intel_connector = to_intel_connector(connector);
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002394 struct intel_encoder *intel_encoder = intel_attached_encoder(connector);
2395 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
Chris Wilsonf6849602010-09-19 09:29:33 +01002396 int ret;
2397
Rob Clark662595d2012-10-11 20:36:04 -05002398 ret = drm_object_property_set_value(&connector->base, property, val);
Chris Wilsonf6849602010-09-19 09:29:33 +01002399 if (ret)
2400 return ret;
2401
Chris Wilson3f43c482011-05-12 22:17:24 +01002402 if (property == dev_priv->force_audio_property) {
Chris Wilson1aad7ac2011-02-09 18:46:58 +00002403 int i = val;
2404 bool has_audio;
2405
2406 if (i == intel_dp->force_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01002407 return 0;
2408
Chris Wilson1aad7ac2011-02-09 18:46:58 +00002409 intel_dp->force_audio = i;
Chris Wilsonf6849602010-09-19 09:29:33 +01002410
Daniel Vetterc3e5f672012-02-23 17:14:47 +01002411 if (i == HDMI_AUDIO_AUTO)
Chris Wilson1aad7ac2011-02-09 18:46:58 +00002412 has_audio = intel_dp_detect_audio(connector);
2413 else
Daniel Vetterc3e5f672012-02-23 17:14:47 +01002414 has_audio = (i == HDMI_AUDIO_ON);
Chris Wilson1aad7ac2011-02-09 18:46:58 +00002415
2416 if (has_audio == intel_dp->has_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01002417 return 0;
2418
Chris Wilson1aad7ac2011-02-09 18:46:58 +00002419 intel_dp->has_audio = has_audio;
Chris Wilsonf6849602010-09-19 09:29:33 +01002420 goto done;
2421 }
2422
Chris Wilsone953fd72011-02-21 22:23:52 +00002423 if (property == dev_priv->broadcast_rgb_property) {
2424 if (val == !!intel_dp->color_range)
2425 return 0;
2426
2427 intel_dp->color_range = val ? DP_COLOR_RANGE_16_235 : 0;
2428 goto done;
2429 }
2430
Yuly Novikov53b41832012-10-26 12:04:00 +03002431 if (is_edp(intel_dp) &&
2432 property == connector->dev->mode_config.scaling_mode_property) {
2433 if (val == DRM_MODE_SCALE_NONE) {
2434 DRM_DEBUG_KMS("no scaling not supported\n");
2435 return -EINVAL;
2436 }
2437
2438 if (intel_connector->panel.fitting_mode == val) {
2439 /* the eDP scaling property is not changed */
2440 return 0;
2441 }
2442 intel_connector->panel.fitting_mode = val;
2443
2444 goto done;
2445 }
2446
Chris Wilsonf6849602010-09-19 09:29:33 +01002447 return -EINVAL;
2448
2449done:
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002450 if (intel_encoder->base.crtc) {
2451 struct drm_crtc *crtc = intel_encoder->base.crtc;
Daniel Vettera6778b32012-07-02 09:56:42 +02002452 intel_set_mode(crtc, &crtc->mode,
2453 crtc->x, crtc->y, crtc->fb);
Chris Wilsonf6849602010-09-19 09:29:33 +01002454 }
2455
2456 return 0;
2457}
2458
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002459static void
Akshay Joshi0206e352011-08-16 15:34:10 -04002460intel_dp_destroy(struct drm_connector *connector)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002461{
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02002462 struct drm_device *dev = connector->dev;
Jani Nikulabe3cd5e2012-10-12 10:33:05 +03002463 struct intel_dp *intel_dp = intel_attached_dp(connector);
Jani Nikula1d508702012-10-19 14:51:49 +03002464 struct intel_connector *intel_connector = to_intel_connector(connector);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02002465
Jani Nikula9cd300e2012-10-19 14:51:52 +03002466 if (!IS_ERR_OR_NULL(intel_connector->edid))
2467 kfree(intel_connector->edid);
2468
Jani Nikula1d508702012-10-19 14:51:49 +03002469 if (is_edp(intel_dp)) {
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02002470 intel_panel_destroy_backlight(dev);
Jani Nikula1d508702012-10-19 14:51:49 +03002471 intel_panel_fini(&intel_connector->panel);
2472 }
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02002473
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002474 drm_sysfs_connector_remove(connector);
2475 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08002476 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002477}
2478
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002479void intel_dp_encoder_destroy(struct drm_encoder *encoder)
Daniel Vetter24d05922010-08-20 18:08:28 +02002480{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002481 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
2482 struct intel_dp *intel_dp = &intel_dig_port->dp;
Daniel Vetter24d05922010-08-20 18:08:28 +02002483
2484 i2c_del_adapter(&intel_dp->adapter);
2485 drm_encoder_cleanup(encoder);
Keith Packardbd943152011-09-18 23:09:52 -07002486 if (is_edp(intel_dp)) {
2487 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
2488 ironlake_panel_vdd_off_sync(intel_dp);
2489 }
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002490 kfree(intel_dig_port);
Daniel Vetter24d05922010-08-20 18:08:28 +02002491}
2492
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002493static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002494 .mode_fixup = intel_dp_mode_fixup,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002495 .mode_set = intel_dp_mode_set,
Daniel Vetter1f703852012-07-11 16:51:39 +02002496 .disable = intel_encoder_noop,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002497};
2498
2499static const struct drm_connector_funcs intel_dp_connector_funcs = {
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002500 .dpms = intel_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002501 .detect = intel_dp_detect,
2502 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilsonf6849602010-09-19 09:29:33 +01002503 .set_property = intel_dp_set_property,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002504 .destroy = intel_dp_destroy,
2505};
2506
2507static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
2508 .get_modes = intel_dp_get_modes,
2509 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01002510 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002511};
2512
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002513static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02002514 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002515};
2516
Chris Wilson995b6762010-08-20 13:23:26 +01002517static void
Eric Anholt21d40d32010-03-25 11:11:14 -07002518intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07002519{
Paulo Zanonifa90ece2012-10-26 19:05:44 -02002520 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
Keith Packardc8110e52009-05-06 11:51:10 -07002521
Jesse Barnes885a5012011-07-07 11:11:01 -07002522 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07002523}
2524
Zhenyu Wange3421a12010-04-08 09:43:27 +08002525/* Return which DP Port should be selected for Transcoder DP control */
2526int
Akshay Joshi0206e352011-08-16 15:34:10 -04002527intel_trans_dp_port_sel(struct drm_crtc *crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08002528{
2529 struct drm_device *dev = crtc->dev;
Paulo Zanonifa90ece2012-10-26 19:05:44 -02002530 struct intel_encoder *intel_encoder;
2531 struct intel_dp *intel_dp;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002532
Paulo Zanonifa90ece2012-10-26 19:05:44 -02002533 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
2534 intel_dp = enc_to_intel_dp(&intel_encoder->base);
Chris Wilsonea5b2132010-08-04 13:50:23 +01002535
Paulo Zanonifa90ece2012-10-26 19:05:44 -02002536 if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
2537 intel_encoder->type == INTEL_OUTPUT_EDP)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002538 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002539 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01002540
Zhenyu Wange3421a12010-04-08 09:43:27 +08002541 return -1;
2542}
2543
Zhao Yakui36e83a12010-06-12 14:32:21 +08002544/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04002545bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08002546{
2547 struct drm_i915_private *dev_priv = dev->dev_private;
2548 struct child_device_config *p_child;
2549 int i;
2550
2551 if (!dev_priv->child_dev_num)
2552 return false;
2553
2554 for (i = 0; i < dev_priv->child_dev_num; i++) {
2555 p_child = dev_priv->child_dev + i;
2556
2557 if (p_child->dvo_port == PORT_IDPD &&
2558 p_child->device_type == DEVICE_TYPE_eDP)
2559 return true;
2560 }
2561 return false;
2562}
2563
Chris Wilsonf6849602010-09-19 09:29:33 +01002564static void
2565intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
2566{
Yuly Novikov53b41832012-10-26 12:04:00 +03002567 struct intel_connector *intel_connector = to_intel_connector(connector);
2568
Chris Wilson3f43c482011-05-12 22:17:24 +01002569 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +00002570 intel_attach_broadcast_rgb_property(connector);
Yuly Novikov53b41832012-10-26 12:04:00 +03002571
2572 if (is_edp(intel_dp)) {
2573 drm_mode_create_scaling_mode_property(connector->dev);
Rob Clark6de6d842012-10-11 20:36:04 -05002574 drm_object_attach_property(
2575 &connector->base,
Yuly Novikov53b41832012-10-26 12:04:00 +03002576 connector->dev->mode_config.scaling_mode_property,
Yuly Novikov8e740cd2012-10-26 12:04:01 +03002577 DRM_MODE_SCALE_ASPECT);
2578 intel_connector->panel.fitting_mode = DRM_MODE_SCALE_ASPECT;
Yuly Novikov53b41832012-10-26 12:04:00 +03002579 }
Chris Wilsonf6849602010-09-19 09:29:33 +01002580}
2581
Daniel Vetter67a54562012-10-20 20:57:45 +02002582static void
2583intel_dp_init_panel_power_sequencer(struct drm_device *dev,
2584 struct intel_dp *intel_dp)
2585{
2586 struct drm_i915_private *dev_priv = dev->dev_private;
2587 struct edp_power_seq cur, vbt, spec, final;
2588 u32 pp_on, pp_off, pp_div, pp;
2589
2590 /* Workaround: Need to write PP_CONTROL with the unlock key as
2591 * the very first thing. */
2592 pp = ironlake_get_pp_control(dev_priv);
2593 I915_WRITE(PCH_PP_CONTROL, pp);
2594
2595 pp_on = I915_READ(PCH_PP_ON_DELAYS);
2596 pp_off = I915_READ(PCH_PP_OFF_DELAYS);
2597 pp_div = I915_READ(PCH_PP_DIVISOR);
2598
2599 /* Pull timing values out of registers */
2600 cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
2601 PANEL_POWER_UP_DELAY_SHIFT;
2602
2603 cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
2604 PANEL_LIGHT_ON_DELAY_SHIFT;
2605
2606 cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
2607 PANEL_LIGHT_OFF_DELAY_SHIFT;
2608
2609 cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
2610 PANEL_POWER_DOWN_DELAY_SHIFT;
2611
2612 cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
2613 PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
2614
2615 DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
2616 cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
2617
2618 vbt = dev_priv->edp.pps;
2619
2620 /* Upper limits from eDP 1.3 spec. Note that we use the clunky units of
2621 * our hw here, which are all in 100usec. */
2622 spec.t1_t3 = 210 * 10;
2623 spec.t8 = 50 * 10; /* no limit for t8, use t7 instead */
2624 spec.t9 = 50 * 10; /* no limit for t9, make it symmetric with t8 */
2625 spec.t10 = 500 * 10;
2626 /* This one is special and actually in units of 100ms, but zero
2627 * based in the hw (so we need to add 100 ms). But the sw vbt
2628 * table multiplies it with 1000 to make it in units of 100usec,
2629 * too. */
2630 spec.t11_t12 = (510 + 100) * 10;
2631
2632 DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
2633 vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
2634
2635 /* Use the max of the register settings and vbt. If both are
2636 * unset, fall back to the spec limits. */
2637#define assign_final(field) final.field = (max(cur.field, vbt.field) == 0 ? \
2638 spec.field : \
2639 max(cur.field, vbt.field))
2640 assign_final(t1_t3);
2641 assign_final(t8);
2642 assign_final(t9);
2643 assign_final(t10);
2644 assign_final(t11_t12);
2645#undef assign_final
2646
2647#define get_delay(field) (DIV_ROUND_UP(final.field, 10))
2648 intel_dp->panel_power_up_delay = get_delay(t1_t3);
2649 intel_dp->backlight_on_delay = get_delay(t8);
2650 intel_dp->backlight_off_delay = get_delay(t9);
2651 intel_dp->panel_power_down_delay = get_delay(t10);
2652 intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
2653#undef get_delay
2654
2655 /* And finally store the new values in the power sequencer. */
2656 pp_on = (final.t1_t3 << PANEL_POWER_UP_DELAY_SHIFT) |
2657 (final.t8 << PANEL_LIGHT_ON_DELAY_SHIFT);
2658 pp_off = (final.t9 << PANEL_LIGHT_OFF_DELAY_SHIFT) |
2659 (final.t10 << PANEL_POWER_DOWN_DELAY_SHIFT);
2660 /* Compute the divisor for the pp clock, simply match the Bspec
2661 * formula. */
2662 pp_div = ((100 * intel_pch_rawclk(dev))/2 - 1)
2663 << PP_REFERENCE_DIVIDER_SHIFT;
2664 pp_div |= (DIV_ROUND_UP(final.t11_t12, 1000)
2665 << PANEL_POWER_CYCLE_DELAY_SHIFT);
2666
2667 /* Haswell doesn't have any port selection bits for the panel
2668 * power sequencer any more. */
2669 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
2670 if (is_cpu_edp(intel_dp))
2671 pp_on |= PANEL_POWER_PORT_DP_A;
2672 else
2673 pp_on |= PANEL_POWER_PORT_DP_D;
2674 }
2675
2676 I915_WRITE(PCH_PP_ON_DELAYS, pp_on);
2677 I915_WRITE(PCH_PP_OFF_DELAYS, pp_off);
2678 I915_WRITE(PCH_PP_DIVISOR, pp_div);
2679
2680
2681 DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
2682 intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
2683 intel_dp->panel_power_cycle_delay);
2684
2685 DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
2686 intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
2687
2688 DRM_DEBUG_KMS("panel power sequencer register settings: PP_ON %#x, PP_OFF %#x, PP_DIV %#x\n",
2689 I915_READ(PCH_PP_ON_DELAYS),
2690 I915_READ(PCH_PP_OFF_DELAYS),
2691 I915_READ(PCH_PP_DIVISOR));
Keith Packardc8110e52009-05-06 11:51:10 -07002692}
2693
2694void
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02002695intel_dp_init_connector(struct intel_digital_port *intel_dig_port,
2696 struct intel_connector *intel_connector)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002697{
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02002698 struct drm_connector *connector = &intel_connector->base;
2699 struct intel_dp *intel_dp = &intel_dig_port->dp;
2700 struct intel_encoder *intel_encoder = &intel_dig_port->base;
2701 struct drm_device *dev = intel_encoder->base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002702 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulaf8779fd2012-10-19 14:51:48 +03002703 struct drm_display_mode *fixed_mode = NULL;
Paulo Zanoni174edf12012-10-26 19:05:50 -02002704 enum port port = intel_dig_port->port;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002705 const char *name = NULL;
Adam Jacksonb3295302010-07-16 14:46:28 -04002706 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002707
Daniel Vetter07679352012-09-06 22:15:42 +02002708 /* Preserve the current hw state. */
2709 intel_dp->DP = I915_READ(intel_dp->output_reg);
Jani Nikuladd06f902012-10-19 14:51:50 +03002710 intel_dp->attached_connector = intel_connector;
Chris Wilson3d3dc142011-02-12 10:33:12 +00002711
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02002712 if (HAS_PCH_SPLIT(dev) && port == PORT_D)
Adam Jacksonb3295302010-07-16 14:46:28 -04002713 if (intel_dpd_is_edp(dev))
Chris Wilsonea5b2132010-08-04 13:50:23 +01002714 intel_dp->is_pch_edp = true;
Adam Jacksonb3295302010-07-16 14:46:28 -04002715
Gajanan Bhat19c03922012-09-27 19:13:07 +05302716 /*
2717 * FIXME : We need to initialize built-in panels before external panels.
2718 * For X0, DP_C is fixed as eDP. Revisit this as part of VLV eDP cleanup
2719 */
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02002720 if (IS_VALLEYVIEW(dev) && port == PORT_C) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05302721 type = DRM_MODE_CONNECTOR_eDP;
2722 intel_encoder->type = INTEL_OUTPUT_EDP;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02002723 } else if (port == PORT_A || is_pch_edp(intel_dp)) {
Adam Jacksonb3295302010-07-16 14:46:28 -04002724 type = DRM_MODE_CONNECTOR_eDP;
2725 intel_encoder->type = INTEL_OUTPUT_EDP;
2726 } else {
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002727 /* The intel_encoder->type value may be INTEL_OUTPUT_UNKNOWN for
2728 * DDI or INTEL_OUTPUT_DISPLAYPORT for the older gens, so don't
2729 * rewrite it.
2730 */
Adam Jacksonb3295302010-07-16 14:46:28 -04002731 type = DRM_MODE_CONNECTOR_DisplayPort;
Adam Jacksonb3295302010-07-16 14:46:28 -04002732 }
2733
Adam Jacksonb3295302010-07-16 14:46:28 -04002734 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002735 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
2736
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002737 connector->polled = DRM_CONNECTOR_POLL_HPD;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02002738 connector->interlace_allowed = true;
2739 connector->doublescan_allowed = 0;
Ma Lingf8aed702009-08-24 13:50:24 +08002740
Daniel Vetter66a92782012-07-12 20:08:18 +02002741 INIT_DELAYED_WORK(&intel_dp->panel_vdd_work,
2742 ironlake_panel_vdd_work);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08002743
Chris Wilsondf0e9242010-09-09 16:20:55 +01002744 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002745 drm_sysfs_connector_add(connector);
2746
Paulo Zanonibcbc8892012-10-26 19:05:51 -02002747 if (IS_HASWELL(dev))
2748 intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
2749 else
2750 intel_connector->get_hw_state = intel_connector_get_hw_state;
2751
Daniel Vettere8cb4552012-07-01 13:05:48 +02002752
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002753 /* Set up the DDC bus. */
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03002754 switch (port) {
2755 case PORT_A:
2756 name = "DPDDC-A";
2757 break;
2758 case PORT_B:
2759 dev_priv->hotplug_supported_mask |= DPB_HOTPLUG_INT_STATUS;
2760 name = "DPDDC-B";
2761 break;
2762 case PORT_C:
2763 dev_priv->hotplug_supported_mask |= DPC_HOTPLUG_INT_STATUS;
2764 name = "DPDDC-C";
2765 break;
2766 case PORT_D:
2767 dev_priv->hotplug_supported_mask |= DPD_HOTPLUG_INT_STATUS;
2768 name = "DPDDC-D";
2769 break;
2770 default:
2771 WARN(1, "Invalid port %c\n", port_name(port));
2772 break;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002773 }
2774
Daniel Vetter67a54562012-10-20 20:57:45 +02002775 if (is_edp(intel_dp))
2776 intel_dp_init_panel_power_sequencer(dev, intel_dp);
Dave Airliec1f05262012-08-30 11:06:18 +10002777
2778 intel_dp_i2c_init(intel_dp, intel_connector, name);
2779
Daniel Vetter67a54562012-10-20 20:57:45 +02002780 /* Cache DPCD and EDID for edp. */
Dave Airliec1f05262012-08-30 11:06:18 +10002781 if (is_edp(intel_dp)) {
2782 bool ret;
Jani Nikulaf8779fd2012-10-19 14:51:48 +03002783 struct drm_display_mode *scan;
Dave Airliec1f05262012-08-30 11:06:18 +10002784 struct edid *edid;
Jesse Barnes5d613502011-01-24 17:10:54 -08002785
2786 ironlake_edp_panel_vdd_on(intel_dp);
Keith Packard59f3e272011-07-25 20:01:56 -07002787 ret = intel_dp_get_dpcd(intel_dp);
Keith Packardbd943152011-09-18 23:09:52 -07002788 ironlake_edp_panel_vdd_off(intel_dp, false);
Keith Packard99ea7122011-11-01 19:57:50 -07002789
Keith Packard59f3e272011-07-25 20:01:56 -07002790 if (ret) {
Jesse Barnes7183dc22011-07-07 11:10:58 -07002791 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
2792 dev_priv->no_aux_handshake =
2793 intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
Jesse Barnes89667382010-10-07 16:01:21 -07002794 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
2795 } else {
Chris Wilson3d3dc142011-02-12 10:33:12 +00002796 /* if this fails, presume the device is a ghost */
Takashi Iwai48898b02011-03-18 09:06:49 +00002797 DRM_INFO("failed to retrieve link info, disabling eDP\n");
Paulo Zanonifa90ece2012-10-26 19:05:44 -02002798 intel_dp_encoder_destroy(&intel_encoder->base);
2799 intel_dp_destroy(connector);
Chris Wilson3d3dc142011-02-12 10:33:12 +00002800 return;
Jesse Barnes89667382010-10-07 16:01:21 -07002801 }
Jesse Barnes89667382010-10-07 16:01:21 -07002802
Jesse Barnesd6f24d02012-06-14 15:28:33 -04002803 ironlake_edp_panel_vdd_on(intel_dp);
2804 edid = drm_get_edid(connector, &intel_dp->adapter);
2805 if (edid) {
Jani Nikula9cd300e2012-10-19 14:51:52 +03002806 if (drm_add_edid_modes(connector, edid)) {
2807 drm_mode_connector_update_edid_property(connector, edid);
2808 drm_edid_to_eld(connector, edid);
2809 } else {
2810 kfree(edid);
2811 edid = ERR_PTR(-EINVAL);
2812 }
2813 } else {
2814 edid = ERR_PTR(-ENOENT);
Jesse Barnesd6f24d02012-06-14 15:28:33 -04002815 }
Jani Nikula9cd300e2012-10-19 14:51:52 +03002816 intel_connector->edid = edid;
Jani Nikulaf8779fd2012-10-19 14:51:48 +03002817
2818 /* prefer fixed mode from EDID if available */
2819 list_for_each_entry(scan, &connector->probed_modes, head) {
2820 if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
2821 fixed_mode = drm_mode_duplicate(dev, scan);
2822 break;
2823 }
2824 }
2825
2826 /* fallback to VBT if available for eDP */
2827 if (!fixed_mode && dev_priv->lfp_lvds_vbt_mode) {
2828 fixed_mode = drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
2829 if (fixed_mode)
2830 fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
2831 }
Jani Nikulaf8779fd2012-10-19 14:51:48 +03002832
Jesse Barnesd6f24d02012-06-14 15:28:33 -04002833 ironlake_edp_panel_vdd_off(intel_dp, false);
2834 }
Keith Packard552fb0b2011-09-28 16:31:53 -07002835
Jesse Barnes4d926462010-10-07 16:01:07 -07002836 if (is_edp(intel_dp)) {
Jani Nikuladd06f902012-10-19 14:51:50 +03002837 intel_panel_init(&intel_connector->panel, fixed_mode);
Jani Nikula0657b6b2012-10-19 14:51:46 +03002838 intel_panel_setup_backlight(connector);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002839 }
2840
Chris Wilsonf6849602010-09-19 09:29:33 +01002841 intel_dp_add_properties(intel_dp, connector);
2842
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002843 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
2844 * 0xd. Failure to do so will result in spurious interrupts being
2845 * generated on the port when a cable is not attached.
2846 */
2847 if (IS_G4X(dev) && !IS_GM45(dev)) {
2848 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
2849 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
2850 }
2851}
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02002852
2853void
2854intel_dp_init(struct drm_device *dev, int output_reg, enum port port)
2855{
2856 struct intel_digital_port *intel_dig_port;
2857 struct intel_encoder *intel_encoder;
2858 struct drm_encoder *encoder;
2859 struct intel_connector *intel_connector;
2860
2861 intel_dig_port = kzalloc(sizeof(struct intel_digital_port), GFP_KERNEL);
2862 if (!intel_dig_port)
2863 return;
2864
2865 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
2866 if (!intel_connector) {
2867 kfree(intel_dig_port);
2868 return;
2869 }
2870
2871 intel_encoder = &intel_dig_port->base;
2872 encoder = &intel_encoder->base;
2873
2874 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
2875 DRM_MODE_ENCODER_TMDS);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002876 drm_encoder_helper_add(&intel_encoder->base, &intel_dp_helper_funcs);
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02002877
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002878 intel_encoder->enable = intel_enable_dp;
2879 intel_encoder->pre_enable = intel_pre_enable_dp;
2880 intel_encoder->disable = intel_disable_dp;
2881 intel_encoder->post_disable = intel_post_disable_dp;
2882 intel_encoder->get_hw_state = intel_dp_get_hw_state;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02002883
Paulo Zanoni174edf12012-10-26 19:05:50 -02002884 intel_dig_port->port = port;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02002885 intel_dig_port->dp.output_reg = output_reg;
2886
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002887 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02002888 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
2889 intel_encoder->cloneable = false;
2890 intel_encoder->hot_plug = intel_dp_hot_plug;
2891
2892 intel_dp_init_connector(intel_dig_port, intel_connector);
2893}