blob: 5f0c783027c970578f9751ff8ddb8551d3e6e783 [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
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +080041struct dp_link_dpll {
42 int link_bw;
43 struct dpll dpll;
44};
45
46static const struct dp_link_dpll gen4_dpll[] = {
47 { DP_LINK_BW_1_62,
48 { .p1 = 2, .p2 = 10, .n = 2, .m1 = 23, .m2 = 8 } },
49 { DP_LINK_BW_2_7,
50 { .p1 = 1, .p2 = 10, .n = 1, .m1 = 14, .m2 = 2 } }
51};
52
53static const struct dp_link_dpll pch_dpll[] = {
54 { DP_LINK_BW_1_62,
55 { .p1 = 2, .p2 = 10, .n = 1, .m1 = 12, .m2 = 9 } },
56 { DP_LINK_BW_2_7,
57 { .p1 = 1, .p2 = 10, .n = 2, .m1 = 14, .m2 = 8 } }
58};
59
Chon Ming Lee65ce4bf2013-09-04 01:30:38 +080060static const struct dp_link_dpll vlv_dpll[] = {
61 { DP_LINK_BW_1_62,
62 { .p1 = 3, .p2 = 2, .n = 5, .m1 = 5, .m2 = 3 } },
63 { DP_LINK_BW_2_7,
64 { .p1 = 2, .p2 = 2, .n = 1, .m1 = 2, .m2 = 27 } }
65};
66
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070067/**
68 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
69 * @intel_dp: DP struct
70 *
71 * If a CPU or PCH DP output is attached to an eDP panel, this function
72 * will return true, and false otherwise.
73 */
74static bool is_edp(struct intel_dp *intel_dp)
75{
Paulo Zanonida63a9f2012-10-26 19:05:46 -020076 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
77
78 return intel_dig_port->base.type == INTEL_OUTPUT_EDP;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070079}
80
Imre Deak68b4d822013-05-08 13:14:06 +030081static struct drm_device *intel_dp_to_dev(struct intel_dp *intel_dp)
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070082{
Imre Deak68b4d822013-05-08 13:14:06 +030083 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
84
85 return intel_dig_port->base.base.dev;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070086}
87
Chris Wilsondf0e9242010-09-09 16:20:55 +010088static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
89{
Paulo Zanonifa90ece2012-10-26 19:05:44 -020090 return enc_to_intel_dp(&intel_attached_encoder(connector)->base);
Chris Wilsondf0e9242010-09-09 16:20:55 +010091}
92
Chris Wilsonea5b2132010-08-04 13:50:23 +010093static void intel_dp_link_down(struct intel_dp *intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -070094
95static int
Chris Wilsonea5b2132010-08-04 13:50:23 +010096intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -070097{
Jesse Barnes7183dc22011-07-07 11:10:58 -070098 int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -070099
100 switch (max_link_bw) {
101 case DP_LINK_BW_1_62:
102 case DP_LINK_BW_2_7:
103 break;
Imre Deakd4eead52013-07-09 17:05:26 +0300104 case DP_LINK_BW_5_4: /* 1.2 capable displays may advertise higher bw */
105 max_link_bw = DP_LINK_BW_2_7;
106 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700107 default:
Imre Deakd4eead52013-07-09 17:05:26 +0300108 WARN(1, "invalid max DP link bw val %x, using 1.62Gbps\n",
109 max_link_bw);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700110 max_link_bw = DP_LINK_BW_1_62;
111 break;
112 }
113 return max_link_bw;
114}
115
Adam Jacksoncd9dde42011-10-14 12:43:49 -0400116/*
117 * The units on the numbers in the next two are... bizarre. Examples will
118 * make it clearer; this one parallels an example in the eDP spec.
119 *
120 * intel_dp_max_data_rate for one lane of 2.7GHz evaluates as:
121 *
122 * 270000 * 1 * 8 / 10 == 216000
123 *
124 * The actual data capacity of that configuration is 2.16Gbit/s, so the
125 * units are decakilobits. ->clock in a drm_display_mode is in kilohertz -
126 * or equivalently, kilopixels per second - so for 1680x1050R it'd be
127 * 119000. At 18bpp that's 2142000 kilobits per second.
128 *
129 * Thus the strange-looking division by 10 in intel_dp_link_required, to
130 * get the result in decakilobits instead of kilobits.
131 */
132
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700133static int
Keith Packardc8982612012-01-25 08:16:25 -0800134intel_dp_link_required(int pixel_clock, int bpp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700135{
Adam Jacksoncd9dde42011-10-14 12:43:49 -0400136 return (pixel_clock * bpp + 9) / 10;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700137}
138
139static int
Dave Airliefe27d532010-06-30 11:46:17 +1000140intel_dp_max_data_rate(int max_link_clock, int max_lanes)
141{
142 return (max_link_clock * max_lanes * 8) / 10;
143}
144
145static int
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700146intel_dp_mode_valid(struct drm_connector *connector,
147 struct drm_display_mode *mode)
148{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100149 struct intel_dp *intel_dp = intel_attached_dp(connector);
Jani Nikuladd06f902012-10-19 14:51:50 +0300150 struct intel_connector *intel_connector = to_intel_connector(connector);
151 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
Daniel Vetter36008362013-03-27 00:44:59 +0100152 int target_clock = mode->clock;
153 int max_rate, mode_rate, max_lanes, max_link_clock;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700154
Jani Nikuladd06f902012-10-19 14:51:50 +0300155 if (is_edp(intel_dp) && fixed_mode) {
156 if (mode->hdisplay > fixed_mode->hdisplay)
Zhao Yakui7de56f42010-07-19 09:43:14 +0100157 return MODE_PANEL;
158
Jani Nikuladd06f902012-10-19 14:51:50 +0300159 if (mode->vdisplay > fixed_mode->vdisplay)
Zhao Yakui7de56f42010-07-19 09:43:14 +0100160 return MODE_PANEL;
Daniel Vetter03afc4a2013-04-02 23:42:31 +0200161
162 target_clock = fixed_mode->clock;
Zhao Yakui7de56f42010-07-19 09:43:14 +0100163 }
164
Daniel Vetter36008362013-03-27 00:44:59 +0100165 max_link_clock = drm_dp_bw_code_to_link_rate(intel_dp_max_link_bw(intel_dp));
166 max_lanes = drm_dp_max_lane_count(intel_dp->dpcd);
167
168 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
169 mode_rate = intel_dp_link_required(target_clock, 18);
170
171 if (mode_rate > max_rate)
Daniel Vetterc4867932012-04-10 10:42:36 +0200172 return MODE_CLOCK_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700173
174 if (mode->clock < 10000)
175 return MODE_CLOCK_LOW;
176
Daniel Vetter0af78a22012-05-23 11:30:55 +0200177 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
178 return MODE_H_ILLEGAL;
179
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700180 return MODE_OK;
181}
182
183static uint32_t
184pack_aux(uint8_t *src, int src_bytes)
185{
186 int i;
187 uint32_t v = 0;
188
189 if (src_bytes > 4)
190 src_bytes = 4;
191 for (i = 0; i < src_bytes; i++)
192 v |= ((uint32_t) src[i]) << ((3-i) * 8);
193 return v;
194}
195
196static void
197unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
198{
199 int i;
200 if (dst_bytes > 4)
201 dst_bytes = 4;
202 for (i = 0; i < dst_bytes; i++)
203 dst[i] = src >> ((3-i) * 8);
204}
205
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700206/* hrawclock is 1/4 the FSB frequency */
207static int
208intel_hrawclk(struct drm_device *dev)
209{
210 struct drm_i915_private *dev_priv = dev->dev_private;
211 uint32_t clkcfg;
212
Vijay Purushothaman9473c8f2012-09-27 19:13:01 +0530213 /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
214 if (IS_VALLEYVIEW(dev))
215 return 200;
216
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700217 clkcfg = I915_READ(CLKCFG);
218 switch (clkcfg & CLKCFG_FSB_MASK) {
219 case CLKCFG_FSB_400:
220 return 100;
221 case CLKCFG_FSB_533:
222 return 133;
223 case CLKCFG_FSB_667:
224 return 166;
225 case CLKCFG_FSB_800:
226 return 200;
227 case CLKCFG_FSB_1067:
228 return 266;
229 case CLKCFG_FSB_1333:
230 return 333;
231 /* these two are just a guess; one of them might be right */
232 case CLKCFG_FSB_1600:
233 case CLKCFG_FSB_1600_ALT:
234 return 400;
235 default:
236 return 133;
237 }
238}
239
Jani Nikulabf13e812013-09-06 07:40:05 +0300240static void
241intel_dp_init_panel_power_sequencer(struct drm_device *dev,
242 struct intel_dp *intel_dp,
243 struct edp_power_seq *out);
244static void
245intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
246 struct intel_dp *intel_dp,
247 struct edp_power_seq *out);
248
249static enum pipe
250vlv_power_sequencer_pipe(struct intel_dp *intel_dp)
251{
252 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
253 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
254 struct drm_device *dev = intel_dig_port->base.base.dev;
255 struct drm_i915_private *dev_priv = dev->dev_private;
256 enum port port = intel_dig_port->port;
257 enum pipe pipe;
258
259 /* modeset should have pipe */
260 if (crtc)
261 return to_intel_crtc(crtc)->pipe;
262
263 /* init time, try to find a pipe with this port selected */
264 for (pipe = PIPE_A; pipe <= PIPE_B; pipe++) {
265 u32 port_sel = I915_READ(VLV_PIPE_PP_ON_DELAYS(pipe)) &
266 PANEL_PORT_SELECT_MASK;
267 if (port_sel == PANEL_PORT_SELECT_DPB_VLV && port == PORT_B)
268 return pipe;
269 if (port_sel == PANEL_PORT_SELECT_DPC_VLV && port == PORT_C)
270 return pipe;
271 }
272
273 /* shrug */
274 return PIPE_A;
275}
276
277static u32 _pp_ctrl_reg(struct intel_dp *intel_dp)
278{
279 struct drm_device *dev = intel_dp_to_dev(intel_dp);
280
281 if (HAS_PCH_SPLIT(dev))
282 return PCH_PP_CONTROL;
283 else
284 return VLV_PIPE_PP_CONTROL(vlv_power_sequencer_pipe(intel_dp));
285}
286
287static u32 _pp_stat_reg(struct intel_dp *intel_dp)
288{
289 struct drm_device *dev = intel_dp_to_dev(intel_dp);
290
291 if (HAS_PCH_SPLIT(dev))
292 return PCH_PP_STATUS;
293 else
294 return VLV_PIPE_PP_STATUS(vlv_power_sequencer_pipe(intel_dp));
295}
296
Keith Packardebf33b12011-09-29 15:53:27 -0700297static bool ironlake_edp_have_panel_power(struct intel_dp *intel_dp)
298{
Paulo Zanoni30add222012-10-26 19:05:45 -0200299 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packardebf33b12011-09-29 15:53:27 -0700300 struct drm_i915_private *dev_priv = dev->dev_private;
301
Jani Nikulabf13e812013-09-06 07:40:05 +0300302 return (I915_READ(_pp_stat_reg(intel_dp)) & PP_ON) != 0;
Keith Packardebf33b12011-09-29 15:53:27 -0700303}
304
305static bool ironlake_edp_have_panel_vdd(struct intel_dp *intel_dp)
306{
Paulo Zanoni30add222012-10-26 19:05:45 -0200307 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packardebf33b12011-09-29 15:53:27 -0700308 struct drm_i915_private *dev_priv = dev->dev_private;
309
Jani Nikulabf13e812013-09-06 07:40:05 +0300310 return (I915_READ(_pp_ctrl_reg(intel_dp)) & EDP_FORCE_VDD) != 0;
Keith Packardebf33b12011-09-29 15:53:27 -0700311}
312
Keith Packard9b984da2011-09-19 13:54:47 -0700313static void
314intel_dp_check_edp(struct intel_dp *intel_dp)
315{
Paulo Zanoni30add222012-10-26 19:05:45 -0200316 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packard9b984da2011-09-19 13:54:47 -0700317 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packardebf33b12011-09-29 15:53:27 -0700318
Keith Packard9b984da2011-09-19 13:54:47 -0700319 if (!is_edp(intel_dp))
320 return;
Jesse Barnes453c5422013-03-28 09:55:41 -0700321
Keith Packardebf33b12011-09-29 15:53:27 -0700322 if (!ironlake_edp_have_panel_power(intel_dp) && !ironlake_edp_have_panel_vdd(intel_dp)) {
Keith Packard9b984da2011-09-19 13:54:47 -0700323 WARN(1, "eDP powered off while attempting aux channel communication.\n");
324 DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
Jani Nikulabf13e812013-09-06 07:40:05 +0300325 I915_READ(_pp_stat_reg(intel_dp)),
326 I915_READ(_pp_ctrl_reg(intel_dp)));
Keith Packard9b984da2011-09-19 13:54:47 -0700327 }
328}
329
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100330static uint32_t
331intel_dp_aux_wait_done(struct intel_dp *intel_dp, bool has_aux_irq)
332{
333 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
334 struct drm_device *dev = intel_dig_port->base.base.dev;
335 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni9ed35ab2013-02-18 19:00:25 -0300336 uint32_t ch_ctl = intel_dp->aux_ch_ctl_reg;
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100337 uint32_t status;
338 bool done;
339
Daniel Vetteref04f002012-12-01 21:03:59 +0100340#define C (((status = I915_READ_NOTRACE(ch_ctl)) & DP_AUX_CH_CTL_SEND_BUSY) == 0)
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100341 if (has_aux_irq)
Paulo Zanonib18ac462013-02-18 19:00:24 -0300342 done = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
Imre Deak35987062013-05-21 20:03:20 +0300343 msecs_to_jiffies_timeout(10));
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100344 else
345 done = wait_for_atomic(C, 10) == 0;
346 if (!done)
347 DRM_ERROR("dp aux hw did not signal timeout (has irq: %i)!\n",
348 has_aux_irq);
349#undef C
350
351 return status;
352}
353
Chris Wilsonbc866252013-07-21 16:00:03 +0100354static uint32_t get_aux_clock_divider(struct intel_dp *intel_dp,
355 int index)
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300356{
357 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
358 struct drm_device *dev = intel_dig_port->base.base.dev;
359 struct drm_i915_private *dev_priv = dev->dev_private;
360
361 /* The clock divider is based off the hrawclk,
362 * and would like to run at 2MHz. So, take the
363 * hrawclk value and divide by 2 and use that
364 *
365 * Note that PCH attached eDP panels should use a 125MHz input
366 * clock divider.
367 */
368 if (IS_VALLEYVIEW(dev)) {
Chris Wilsonbc866252013-07-21 16:00:03 +0100369 return index ? 0 : 100;
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300370 } else if (intel_dig_port->port == PORT_A) {
Chris Wilsonbc866252013-07-21 16:00:03 +0100371 if (index)
372 return 0;
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300373 if (HAS_DDI(dev))
Chris Wilsonbc866252013-07-21 16:00:03 +0100374 return DIV_ROUND_CLOSEST(intel_ddi_get_cdclk_freq(dev_priv), 2000);
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300375 else if (IS_GEN6(dev) || IS_GEN7(dev))
376 return 200; /* SNB & IVB eDP input clock at 400Mhz */
377 else
378 return 225; /* eDP input clock at 450Mhz */
379 } else if (dev_priv->pch_id == INTEL_PCH_LPT_DEVICE_ID_TYPE) {
380 /* Workaround for non-ULT HSW */
Chris Wilsonbc866252013-07-21 16:00:03 +0100381 switch (index) {
382 case 0: return 63;
383 case 1: return 72;
384 default: return 0;
385 }
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300386 } else if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonbc866252013-07-21 16:00:03 +0100387 return index ? 0 : DIV_ROUND_UP(intel_pch_rawclk(dev), 2);
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300388 } else {
Chris Wilsonbc866252013-07-21 16:00:03 +0100389 return index ? 0 :intel_hrawclk(dev) / 2;
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300390 }
391}
392
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700393static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100394intel_dp_aux_ch(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700395 uint8_t *send, int send_bytes,
396 uint8_t *recv, int recv_size)
397{
Paulo Zanoni174edf12012-10-26 19:05:50 -0200398 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
399 struct drm_device *dev = intel_dig_port->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700400 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni9ed35ab2013-02-18 19:00:25 -0300401 uint32_t ch_ctl = intel_dp->aux_ch_ctl_reg;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700402 uint32_t ch_data = ch_ctl + 4;
Chris Wilsonbc866252013-07-21 16:00:03 +0100403 uint32_t aux_clock_divider;
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100404 int i, ret, recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700405 uint32_t status;
Chris Wilsonbc866252013-07-21 16:00:03 +0100406 int try, precharge, clock = 0;
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100407 bool has_aux_irq = INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev);
408
409 /* dp aux is extremely sensitive to irq latency, hence request the
410 * lowest possible wakeup latency and so prevent the cpu from going into
411 * deep sleep states.
412 */
413 pm_qos_update_request(&dev_priv->pm_qos, 0);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700414
Keith Packard9b984da2011-09-19 13:54:47 -0700415 intel_dp_check_edp(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800416
Daniel Vetter6b4e0a92012-06-14 22:15:00 +0200417 if (IS_GEN6(dev))
418 precharge = 3;
419 else
420 precharge = 5;
421
Paulo Zanonic67a4702013-08-19 13:18:09 -0300422 intel_aux_display_runtime_get(dev_priv);
423
Jesse Barnes11bee432011-08-01 15:02:20 -0700424 /* Try to wait for any previous AUX channel activity */
425 for (try = 0; try < 3; try++) {
Daniel Vetteref04f002012-12-01 21:03:59 +0100426 status = I915_READ_NOTRACE(ch_ctl);
Jesse Barnes11bee432011-08-01 15:02:20 -0700427 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
428 break;
429 msleep(1);
430 }
431
432 if (try == 3) {
433 WARN(1, "dp_aux_ch not started status 0x%08x\n",
434 I915_READ(ch_ctl));
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100435 ret = -EBUSY;
436 goto out;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100437 }
438
Paulo Zanoni46a5ae92013-09-17 11:14:10 -0300439 /* Only 5 data registers! */
440 if (WARN_ON(send_bytes > 20 || recv_size > 20)) {
441 ret = -E2BIG;
442 goto out;
443 }
444
Chris Wilsonbc866252013-07-21 16:00:03 +0100445 while ((aux_clock_divider = get_aux_clock_divider(intel_dp, clock++))) {
446 /* Must try at least 3 times according to DP spec */
447 for (try = 0; try < 5; try++) {
448 /* Load the send data into the aux channel data registers */
449 for (i = 0; i < send_bytes; i += 4)
450 I915_WRITE(ch_data + i,
451 pack_aux(send + i, send_bytes - i));
Akshay Joshi0206e352011-08-16 15:34:10 -0400452
Chris Wilsonbc866252013-07-21 16:00:03 +0100453 /* Send the command and wait for it to complete */
454 I915_WRITE(ch_ctl,
455 DP_AUX_CH_CTL_SEND_BUSY |
456 (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
457 DP_AUX_CH_CTL_TIME_OUT_400us |
458 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
459 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
460 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
461 DP_AUX_CH_CTL_DONE |
462 DP_AUX_CH_CTL_TIME_OUT_ERROR |
463 DP_AUX_CH_CTL_RECEIVE_ERROR);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100464
Chris Wilsonbc866252013-07-21 16:00:03 +0100465 status = intel_dp_aux_wait_done(intel_dp, has_aux_irq);
Akshay Joshi0206e352011-08-16 15:34:10 -0400466
Chris Wilsonbc866252013-07-21 16:00:03 +0100467 /* Clear done status and any errors */
468 I915_WRITE(ch_ctl,
469 status |
470 DP_AUX_CH_CTL_DONE |
471 DP_AUX_CH_CTL_TIME_OUT_ERROR |
472 DP_AUX_CH_CTL_RECEIVE_ERROR);
Adam Jacksond7e96fe2011-07-26 15:39:46 -0400473
Chris Wilsonbc866252013-07-21 16:00:03 +0100474 if (status & (DP_AUX_CH_CTL_TIME_OUT_ERROR |
475 DP_AUX_CH_CTL_RECEIVE_ERROR))
476 continue;
477 if (status & DP_AUX_CH_CTL_DONE)
478 break;
479 }
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100480 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700481 break;
482 }
483
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700484 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700485 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100486 ret = -EBUSY;
487 goto out;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700488 }
489
490 /* Check for timeout or receive error.
491 * Timeouts occur when the sink is not connected
492 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700493 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700494 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100495 ret = -EIO;
496 goto out;
Keith Packarda5b3da52009-06-11 22:30:32 -0700497 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700498
499 /* Timeouts occur when the device isn't connected, so they're
500 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700501 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800502 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100503 ret = -ETIMEDOUT;
504 goto out;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700505 }
506
507 /* Unload any bytes sent back from the other side */
508 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
509 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700510 if (recv_bytes > recv_size)
511 recv_bytes = recv_size;
Akshay Joshi0206e352011-08-16 15:34:10 -0400512
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100513 for (i = 0; i < recv_bytes; i += 4)
514 unpack_aux(I915_READ(ch_data + i),
515 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700516
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100517 ret = recv_bytes;
518out:
519 pm_qos_update_request(&dev_priv->pm_qos, PM_QOS_DEFAULT_VALUE);
Paulo Zanonic67a4702013-08-19 13:18:09 -0300520 intel_aux_display_runtime_put(dev_priv);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100521
522 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700523}
524
525/* Write data to the aux channel in native mode */
526static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100527intel_dp_aux_native_write(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700528 uint16_t address, uint8_t *send, int send_bytes)
529{
530 int ret;
531 uint8_t msg[20];
532 int msg_bytes;
533 uint8_t ack;
534
Paulo Zanoni46a5ae92013-09-17 11:14:10 -0300535 if (WARN_ON(send_bytes > 16))
536 return -E2BIG;
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_WRITE << 4;
540 msg[1] = address >> 8;
Zhenyu Wangeebc8632009-07-24 01:00:30 +0800541 msg[2] = address & 0xff;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700542 msg[3] = send_bytes - 1;
543 memcpy(&msg[4], send, send_bytes);
544 msg_bytes = send_bytes + 4;
545 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100546 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes, &ack, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700547 if (ret < 0)
548 return ret;
549 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
550 break;
551 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
552 udelay(100);
553 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700554 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700555 }
556 return send_bytes;
557}
558
559/* Write a single byte to the aux channel in native mode */
560static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100561intel_dp_aux_native_write_1(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700562 uint16_t address, uint8_t byte)
563{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100564 return intel_dp_aux_native_write(intel_dp, address, &byte, 1);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700565}
566
567/* read bytes from a native aux channel */
568static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100569intel_dp_aux_native_read(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700570 uint16_t address, uint8_t *recv, int recv_bytes)
571{
572 uint8_t msg[4];
573 int msg_bytes;
574 uint8_t reply[20];
575 int reply_bytes;
576 uint8_t ack;
577 int ret;
578
Paulo Zanoni46a5ae92013-09-17 11:14:10 -0300579 if (WARN_ON(recv_bytes > 19))
580 return -E2BIG;
581
Keith Packard9b984da2011-09-19 13:54:47 -0700582 intel_dp_check_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700583 msg[0] = AUX_NATIVE_READ << 4;
584 msg[1] = address >> 8;
585 msg[2] = address & 0xff;
586 msg[3] = recv_bytes - 1;
587
588 msg_bytes = 4;
589 reply_bytes = recv_bytes + 1;
590
591 for (;;) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100592 ret = intel_dp_aux_ch(intel_dp, msg, msg_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700593 reply, reply_bytes);
Keith Packarda5b3da52009-06-11 22:30:32 -0700594 if (ret == 0)
595 return -EPROTO;
596 if (ret < 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700597 return ret;
598 ack = reply[0];
599 if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
600 memcpy(recv, reply + 1, ret - 1);
601 return ret - 1;
602 }
603 else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
604 udelay(100);
605 else
Keith Packarda5b3da52009-06-11 22:30:32 -0700606 return -EIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700607 }
608}
609
610static int
Dave Airlieab2c0672009-12-04 10:55:24 +1000611intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
612 uint8_t write_byte, uint8_t *read_byte)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700613{
Dave Airlieab2c0672009-12-04 10:55:24 +1000614 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100615 struct intel_dp *intel_dp = container_of(adapter,
616 struct intel_dp,
617 adapter);
Dave Airlieab2c0672009-12-04 10:55:24 +1000618 uint16_t address = algo_data->address;
619 uint8_t msg[5];
620 uint8_t reply[2];
David Flynn8316f332010-12-08 16:10:21 +0000621 unsigned retry;
Dave Airlieab2c0672009-12-04 10:55:24 +1000622 int msg_bytes;
623 int reply_bytes;
624 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700625
Keith Packard9b984da2011-09-19 13:54:47 -0700626 intel_dp_check_edp(intel_dp);
Dave Airlieab2c0672009-12-04 10:55:24 +1000627 /* Set up the command byte */
628 if (mode & MODE_I2C_READ)
629 msg[0] = AUX_I2C_READ << 4;
630 else
631 msg[0] = AUX_I2C_WRITE << 4;
632
633 if (!(mode & MODE_I2C_STOP))
634 msg[0] |= AUX_I2C_MOT << 4;
635
636 msg[1] = address >> 8;
637 msg[2] = address;
638
639 switch (mode) {
640 case MODE_I2C_WRITE:
641 msg[3] = 0;
642 msg[4] = write_byte;
643 msg_bytes = 5;
644 reply_bytes = 1;
645 break;
646 case MODE_I2C_READ:
647 msg[3] = 0;
648 msg_bytes = 4;
649 reply_bytes = 2;
650 break;
651 default:
652 msg_bytes = 3;
653 reply_bytes = 1;
654 break;
655 }
656
David Flynn8316f332010-12-08 16:10:21 +0000657 for (retry = 0; retry < 5; retry++) {
658 ret = intel_dp_aux_ch(intel_dp,
659 msg, msg_bytes,
660 reply, reply_bytes);
Dave Airlieab2c0672009-12-04 10:55:24 +1000661 if (ret < 0) {
Dave Airlie3ff99162009-12-08 14:03:47 +1000662 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
Dave Airlieab2c0672009-12-04 10:55:24 +1000663 return ret;
664 }
David Flynn8316f332010-12-08 16:10:21 +0000665
666 switch (reply[0] & AUX_NATIVE_REPLY_MASK) {
667 case AUX_NATIVE_REPLY_ACK:
668 /* I2C-over-AUX Reply field is only valid
669 * when paired with AUX ACK.
670 */
671 break;
672 case AUX_NATIVE_REPLY_NACK:
673 DRM_DEBUG_KMS("aux_ch native nack\n");
674 return -EREMOTEIO;
675 case AUX_NATIVE_REPLY_DEFER:
Jani Nikula8d16f252013-09-20 16:42:15 +0300676 /*
677 * For now, just give more slack to branch devices. We
678 * could check the DPCD for I2C bit rate capabilities,
679 * and if available, adjust the interval. We could also
680 * be more careful with DP-to-Legacy adapters where a
681 * long legacy cable may force very low I2C bit rates.
682 */
683 if (intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
684 DP_DWN_STRM_PORT_PRESENT)
685 usleep_range(500, 600);
686 else
687 usleep_range(300, 400);
David Flynn8316f332010-12-08 16:10:21 +0000688 continue;
689 default:
690 DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
691 reply[0]);
692 return -EREMOTEIO;
693 }
694
Dave Airlieab2c0672009-12-04 10:55:24 +1000695 switch (reply[0] & AUX_I2C_REPLY_MASK) {
696 case AUX_I2C_REPLY_ACK:
697 if (mode == MODE_I2C_READ) {
698 *read_byte = reply[1];
699 }
700 return reply_bytes - 1;
701 case AUX_I2C_REPLY_NACK:
David Flynn8316f332010-12-08 16:10:21 +0000702 DRM_DEBUG_KMS("aux_i2c nack\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000703 return -EREMOTEIO;
704 case AUX_I2C_REPLY_DEFER:
David Flynn8316f332010-12-08 16:10:21 +0000705 DRM_DEBUG_KMS("aux_i2c defer\n");
Dave Airlieab2c0672009-12-04 10:55:24 +1000706 udelay(100);
707 break;
708 default:
David Flynn8316f332010-12-08 16:10:21 +0000709 DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
Dave Airlieab2c0672009-12-04 10:55:24 +1000710 return -EREMOTEIO;
711 }
712 }
David Flynn8316f332010-12-08 16:10:21 +0000713
714 DRM_ERROR("too many retries, giving up\n");
715 return -EREMOTEIO;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700716}
717
718static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100719intel_dp_i2c_init(struct intel_dp *intel_dp,
Zhenyu Wang55f78c42010-03-29 16:13:57 +0800720 struct intel_connector *intel_connector, const char *name)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700721{
Keith Packard0b5c5412011-09-28 16:41:05 -0700722 int ret;
723
Zhenyu Wangd54e9d22009-10-19 15:43:51 +0800724 DRM_DEBUG_KMS("i2c_init %s\n", name);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100725 intel_dp->algo.running = false;
726 intel_dp->algo.address = 0;
727 intel_dp->algo.aux_ch = intel_dp_i2c_aux_ch;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700728
Akshay Joshi0206e352011-08-16 15:34:10 -0400729 memset(&intel_dp->adapter, '\0', sizeof(intel_dp->adapter));
Chris Wilsonea5b2132010-08-04 13:50:23 +0100730 intel_dp->adapter.owner = THIS_MODULE;
731 intel_dp->adapter.class = I2C_CLASS_DDC;
Akshay Joshi0206e352011-08-16 15:34:10 -0400732 strncpy(intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100733 intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
734 intel_dp->adapter.algo_data = &intel_dp->algo;
735 intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
736
Keith Packard0b5c5412011-09-28 16:41:05 -0700737 ironlake_edp_panel_vdd_on(intel_dp);
738 ret = i2c_dp_aux_add_bus(&intel_dp->adapter);
Keith Packardbd943152011-09-18 23:09:52 -0700739 ironlake_edp_panel_vdd_off(intel_dp, false);
Keith Packard0b5c5412011-09-28 16:41:05 -0700740 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700741}
742
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200743static void
744intel_dp_set_clock(struct intel_encoder *encoder,
745 struct intel_crtc_config *pipe_config, int link_bw)
746{
747 struct drm_device *dev = encoder->base.dev;
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +0800748 const struct dp_link_dpll *divisor = NULL;
749 int i, count = 0;
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200750
751 if (IS_G4X(dev)) {
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +0800752 divisor = gen4_dpll;
753 count = ARRAY_SIZE(gen4_dpll);
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200754 } else if (IS_HASWELL(dev)) {
755 /* Haswell has special-purpose DP DDI clocks. */
756 } else if (HAS_PCH_SPLIT(dev)) {
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +0800757 divisor = pch_dpll;
758 count = ARRAY_SIZE(pch_dpll);
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200759 } else if (IS_VALLEYVIEW(dev)) {
Chon Ming Lee65ce4bf2013-09-04 01:30:38 +0800760 divisor = vlv_dpll;
761 count = ARRAY_SIZE(vlv_dpll);
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200762 }
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +0800763
764 if (divisor && count) {
765 for (i = 0; i < count; i++) {
766 if (link_bw == divisor[i].link_bw) {
767 pipe_config->dpll = divisor[i].dpll;
768 pipe_config->clock_set = true;
769 break;
770 }
771 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200772 }
773}
774
Paulo Zanoni00c09d72012-10-26 19:05:52 -0200775bool
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +0100776intel_dp_compute_config(struct intel_encoder *encoder,
777 struct intel_crtc_config *pipe_config)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700778{
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +0100779 struct drm_device *dev = encoder->base.dev;
Daniel Vetter36008362013-03-27 00:44:59 +0100780 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +0100781 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +0100782 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deakbc7d38a2013-05-16 14:40:36 +0300783 enum port port = dp_to_dig_port(intel_dp)->port;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700784 struct intel_crtc *intel_crtc = encoder->new_crtc;
Jani Nikuladd06f902012-10-19 14:51:50 +0300785 struct intel_connector *intel_connector = intel_dp->attached_connector;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700786 int lane_count, clock;
Daniel Vetter397fe152012-10-22 22:56:43 +0200787 int max_lane_count = drm_dp_max_lane_count(intel_dp->dpcd);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100788 int max_clock = intel_dp_max_link_bw(intel_dp) == DP_LINK_BW_2_7 ? 1 : 0;
Daniel Vetter083f9562012-04-20 20:23:49 +0200789 int bpp, mode_rate;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700790 static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
Daniel Vetterff9a6752013-06-01 17:16:21 +0200791 int link_avail, link_clock;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700792
Imre Deakbc7d38a2013-05-16 14:40:36 +0300793 if (HAS_PCH_SPLIT(dev) && !HAS_DDI(dev) && port != PORT_A)
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +0100794 pipe_config->has_pch_encoder = true;
795
Daniel Vetter03afc4a2013-04-02 23:42:31 +0200796 pipe_config->has_dp_encoder = true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700797
Jani Nikuladd06f902012-10-19 14:51:50 +0300798 if (is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
799 intel_fixed_panel_mode(intel_connector->panel.fixed_mode,
800 adjusted_mode);
Jesse Barnes2dd24552013-04-25 12:55:01 -0700801 if (!HAS_PCH_SPLIT(dev))
802 intel_gmch_panel_fitting(intel_crtc, pipe_config,
803 intel_connector->panel.fitting_mode);
804 else
Jesse Barnesb074cec2013-04-25 12:55:02 -0700805 intel_pch_panel_fitting(intel_crtc, pipe_config,
806 intel_connector->panel.fitting_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +0100807 }
808
Daniel Vettercb1793c2012-06-04 18:39:21 +0200809 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
Daniel Vetter0af78a22012-05-23 11:30:55 +0200810 return false;
811
Daniel Vetter083f9562012-04-20 20:23:49 +0200812 DRM_DEBUG_KMS("DP link computation with max lane count %i "
813 "max bw %02x pixel clock %iKHz\n",
Daniel Vetter71244652012-06-04 18:39:20 +0200814 max_lane_count, bws[max_clock], adjusted_mode->clock);
Daniel Vetter083f9562012-04-20 20:23:49 +0200815
Daniel Vetter36008362013-03-27 00:44:59 +0100816 /* Walk through all bpp values. Luckily they're all nicely spaced with 2
817 * bpc in between. */
Daniel Vetter3e7ca982013-06-01 19:45:56 +0200818 bpp = pipe_config->pipe_bpp;
Imre Deak79842112013-07-18 17:44:13 +0300819 if (is_edp(intel_dp) && dev_priv->vbt.edp_bpp) {
820 DRM_DEBUG_KMS("clamping bpp for eDP panel to BIOS-provided %i\n",
821 dev_priv->vbt.edp_bpp);
Daniel Vettere1b73cb2013-05-21 09:52:16 +0200822 bpp = min_t(int, bpp, dev_priv->vbt.edp_bpp);
Imre Deak79842112013-07-18 17:44:13 +0300823 }
Daniel Vetter657445f2013-05-04 10:09:18 +0200824
Daniel Vetter36008362013-03-27 00:44:59 +0100825 for (; bpp >= 6*3; bpp -= 2*3) {
Daniel Vetterff9a6752013-06-01 17:16:21 +0200826 mode_rate = intel_dp_link_required(adjusted_mode->clock, bpp);
Daniel Vetterc4867932012-04-10 10:42:36 +0200827
Daniel Vetter36008362013-03-27 00:44:59 +0100828 for (clock = 0; clock <= max_clock; clock++) {
829 for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
830 link_clock = drm_dp_bw_code_to_link_rate(bws[clock]);
831 link_avail = intel_dp_max_data_rate(link_clock,
832 lane_count);
Ville Syrjälä3685a8f2013-01-17 16:31:28 +0200833
Daniel Vetter36008362013-03-27 00:44:59 +0100834 if (mode_rate <= link_avail) {
835 goto found;
836 }
837 }
838 }
839 }
840
841 return false;
842
843found:
Ville Syrjälä55bc60d2013-01-17 16:31:29 +0200844 if (intel_dp->color_range_auto) {
845 /*
846 * See:
847 * CEA-861-E - 5.1 Default Encoding Parameters
848 * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
849 */
Thierry Reding18316c82012-12-20 15:41:44 +0100850 if (bpp != 18 && drm_match_cea_mode(adjusted_mode) > 1)
Ville Syrjälä55bc60d2013-01-17 16:31:29 +0200851 intel_dp->color_range = DP_COLOR_RANGE_16_235;
852 else
853 intel_dp->color_range = 0;
854 }
855
Ville Syrjälä3685a8f2013-01-17 16:31:28 +0200856 if (intel_dp->color_range)
Daniel Vetter50f3b012013-03-27 00:44:56 +0100857 pipe_config->limited_color_range = true;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +0200858
Daniel Vetter36008362013-03-27 00:44:59 +0100859 intel_dp->link_bw = bws[clock];
860 intel_dp->lane_count = lane_count;
Daniel Vetter657445f2013-05-04 10:09:18 +0200861 pipe_config->pipe_bpp = bpp;
Daniel Vetterff9a6752013-06-01 17:16:21 +0200862 pipe_config->port_clock = drm_dp_bw_code_to_link_rate(intel_dp->link_bw);
Daniel Vetterc4867932012-04-10 10:42:36 +0200863
Daniel Vetter36008362013-03-27 00:44:59 +0100864 DRM_DEBUG_KMS("DP link bw %02x lane count %d clock %d bpp %d\n",
865 intel_dp->link_bw, intel_dp->lane_count,
Daniel Vetterff9a6752013-06-01 17:16:21 +0200866 pipe_config->port_clock, bpp);
Daniel Vetter36008362013-03-27 00:44:59 +0100867 DRM_DEBUG_KMS("DP link bw required %i available %i\n",
868 mode_rate, link_avail);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700869
Daniel Vetter03afc4a2013-04-02 23:42:31 +0200870 intel_link_compute_m_n(bpp, lane_count,
Daniel Vetterff9a6752013-06-01 17:16:21 +0200871 adjusted_mode->clock, pipe_config->port_clock,
Daniel Vetter03afc4a2013-04-02 23:42:31 +0200872 &pipe_config->dp_m_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700873
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200874 intel_dp_set_clock(encoder, pipe_config, intel_dp->link_bw);
875
Daniel Vetter36008362013-03-27 00:44:59 +0100876 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700877}
878
Daniel Vetter7c62a162013-06-01 17:16:20 +0200879static void ironlake_set_pll_cpu_edp(struct intel_dp *intel_dp)
Daniel Vetterea9b6002012-11-29 15:59:31 +0100880{
Daniel Vetter7c62a162013-06-01 17:16:20 +0200881 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
882 struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
883 struct drm_device *dev = crtc->base.dev;
Daniel Vetterea9b6002012-11-29 15:59:31 +0100884 struct drm_i915_private *dev_priv = dev->dev_private;
885 u32 dpa_ctl;
886
Daniel Vetterff9a6752013-06-01 17:16:21 +0200887 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", crtc->config.port_clock);
Daniel Vetterea9b6002012-11-29 15:59:31 +0100888 dpa_ctl = I915_READ(DP_A);
889 dpa_ctl &= ~DP_PLL_FREQ_MASK;
890
Daniel Vetterff9a6752013-06-01 17:16:21 +0200891 if (crtc->config.port_clock == 162000) {
Daniel Vetter1ce17032012-11-29 15:59:32 +0100892 /* For a long time we've carried around a ILK-DevA w/a for the
893 * 160MHz clock. If we're really unlucky, it's still required.
894 */
895 DRM_DEBUG_KMS("160MHz cpu eDP clock, might need ilk devA w/a\n");
Daniel Vetterea9b6002012-11-29 15:59:31 +0100896 dpa_ctl |= DP_PLL_FREQ_160MHZ;
Daniel Vetter7c62a162013-06-01 17:16:20 +0200897 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Daniel Vetterea9b6002012-11-29 15:59:31 +0100898 } else {
899 dpa_ctl |= DP_PLL_FREQ_270MHZ;
Daniel Vetter7c62a162013-06-01 17:16:20 +0200900 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Daniel Vetterea9b6002012-11-29 15:59:31 +0100901 }
Daniel Vetter1ce17032012-11-29 15:59:32 +0100902
Daniel Vetterea9b6002012-11-29 15:59:31 +0100903 I915_WRITE(DP_A, dpa_ctl);
904
905 POSTING_READ(DP_A);
906 udelay(500);
907}
908
Daniel Vetterb934223d2013-07-21 21:37:05 +0200909static void intel_dp_mode_set(struct intel_encoder *encoder)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700910{
Daniel Vetterb934223d2013-07-21 21:37:05 +0200911 struct drm_device *dev = encoder->base.dev;
Keith Packard417e8222011-11-01 19:54:11 -0700912 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterb934223d2013-07-21 21:37:05 +0200913 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deakbc7d38a2013-05-16 14:40:36 +0300914 enum port port = dp_to_dig_port(intel_dp)->port;
Daniel Vetterb934223d2013-07-21 21:37:05 +0200915 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
916 struct drm_display_mode *adjusted_mode = &crtc->config.adjusted_mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700917
Keith Packard417e8222011-11-01 19:54:11 -0700918 /*
Keith Packard1a2eb462011-11-16 16:26:07 -0800919 * There are four kinds of DP registers:
Keith Packard417e8222011-11-01 19:54:11 -0700920 *
921 * IBX PCH
Keith Packard1a2eb462011-11-16 16:26:07 -0800922 * SNB CPU
923 * IVB CPU
Keith Packard417e8222011-11-01 19:54:11 -0700924 * CPT PCH
925 *
926 * IBX PCH and CPU are the same for almost everything,
927 * except that the CPU DP PLL is configured in this
928 * register
929 *
930 * CPT PCH is quite different, having many bits moved
931 * to the TRANS_DP_CTL register instead. That
932 * configuration happens (oddly) in ironlake_pch_enable
933 */
Adam Jackson9c9e7922010-04-05 17:57:59 -0400934
Keith Packard417e8222011-11-01 19:54:11 -0700935 /* Preserve the BIOS-computed detected bit. This is
936 * supposed to be read-only.
937 */
938 intel_dp->DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700939
Keith Packard417e8222011-11-01 19:54:11 -0700940 /* Handle DP bits in common between all three register formats */
Keith Packard417e8222011-11-01 19:54:11 -0700941 intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
Daniel Vetter17aa6be2013-04-30 14:01:40 +0200942 intel_dp->DP |= DP_PORT_WIDTH(intel_dp->lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700943
Wu Fengguange0dac652011-09-05 14:25:34 +0800944 if (intel_dp->has_audio) {
945 DRM_DEBUG_DRIVER("Enabling DP audio on pipe %c\n",
Daniel Vetter7c62a162013-06-01 17:16:20 +0200946 pipe_name(crtc->pipe));
Chris Wilsonea5b2132010-08-04 13:50:23 +0100947 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Daniel Vetterb934223d2013-07-21 21:37:05 +0200948 intel_write_eld(&encoder->base, adjusted_mode);
Wu Fengguange0dac652011-09-05 14:25:34 +0800949 }
Paulo Zanoni247d89f2012-10-15 15:51:33 -0300950
Keith Packard417e8222011-11-01 19:54:11 -0700951 /* Split out the IBX/CPU vs CPT settings */
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800952
Imre Deakbc7d38a2013-05-16 14:40:36 +0300953 if (port == PORT_A && IS_GEN7(dev) && !IS_VALLEYVIEW(dev)) {
Keith Packard1a2eb462011-11-16 16:26:07 -0800954 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
955 intel_dp->DP |= DP_SYNC_HS_HIGH;
956 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
957 intel_dp->DP |= DP_SYNC_VS_HIGH;
958 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
959
Jani Nikula6aba5b62013-10-04 15:08:10 +0300960 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
Keith Packard1a2eb462011-11-16 16:26:07 -0800961 intel_dp->DP |= DP_ENHANCED_FRAMING;
962
Daniel Vetter7c62a162013-06-01 17:16:20 +0200963 intel_dp->DP |= crtc->pipe << 29;
Imre Deakbc7d38a2013-05-16 14:40:36 +0300964 } else if (!HAS_PCH_CPT(dev) || port == PORT_A) {
Jesse Barnesb2634012013-03-28 09:55:40 -0700965 if (!HAS_PCH_SPLIT(dev) && !IS_VALLEYVIEW(dev))
Ville Syrjälä3685a8f2013-01-17 16:31:28 +0200966 intel_dp->DP |= intel_dp->color_range;
Keith Packard417e8222011-11-01 19:54:11 -0700967
968 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
969 intel_dp->DP |= DP_SYNC_HS_HIGH;
970 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
971 intel_dp->DP |= DP_SYNC_VS_HIGH;
972 intel_dp->DP |= DP_LINK_TRAIN_OFF;
973
Jani Nikula6aba5b62013-10-04 15:08:10 +0300974 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
Keith Packard417e8222011-11-01 19:54:11 -0700975 intel_dp->DP |= DP_ENHANCED_FRAMING;
976
Daniel Vetter7c62a162013-06-01 17:16:20 +0200977 if (crtc->pipe == 1)
Keith Packard417e8222011-11-01 19:54:11 -0700978 intel_dp->DP |= DP_PIPEB_SELECT;
Keith Packard417e8222011-11-01 19:54:11 -0700979 } else {
980 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +0800981 }
Daniel Vetterea9b6002012-11-29 15:59:31 +0100982
Imre Deakbc7d38a2013-05-16 14:40:36 +0300983 if (port == PORT_A && !IS_VALLEYVIEW(dev))
Daniel Vetter7c62a162013-06-01 17:16:20 +0200984 ironlake_set_pll_cpu_edp(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700985}
986
Keith Packard99ea7122011-11-01 19:57:50 -0700987#define IDLE_ON_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
988#define IDLE_ON_VALUE (PP_ON | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_ON_IDLE)
989
990#define IDLE_OFF_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
991#define IDLE_OFF_VALUE (0 | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
992
993#define IDLE_CYCLE_MASK (PP_ON | 0 | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
994#define IDLE_CYCLE_VALUE (0 | 0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
995
996static void ironlake_wait_panel_status(struct intel_dp *intel_dp,
997 u32 mask,
998 u32 value)
999{
Paulo Zanoni30add222012-10-26 19:05:45 -02001000 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packard99ea7122011-11-01 19:57:50 -07001001 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes453c5422013-03-28 09:55:41 -07001002 u32 pp_stat_reg, pp_ctrl_reg;
1003
Jani Nikulabf13e812013-09-06 07:40:05 +03001004 pp_stat_reg = _pp_stat_reg(intel_dp);
1005 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Keith Packard99ea7122011-11-01 19:57:50 -07001006
1007 DRM_DEBUG_KMS("mask %08x value %08x status %08x control %08x\n",
Jesse Barnes453c5422013-03-28 09:55:41 -07001008 mask, value,
1009 I915_READ(pp_stat_reg),
1010 I915_READ(pp_ctrl_reg));
Keith Packard99ea7122011-11-01 19:57:50 -07001011
Jesse Barnes453c5422013-03-28 09:55:41 -07001012 if (_wait_for((I915_READ(pp_stat_reg) & mask) == value, 5000, 10)) {
Keith Packard99ea7122011-11-01 19:57:50 -07001013 DRM_ERROR("Panel status timeout: status %08x control %08x\n",
Jesse Barnes453c5422013-03-28 09:55:41 -07001014 I915_READ(pp_stat_reg),
1015 I915_READ(pp_ctrl_reg));
Keith Packard99ea7122011-11-01 19:57:50 -07001016 }
1017}
1018
1019static void ironlake_wait_panel_on(struct intel_dp *intel_dp)
1020{
1021 DRM_DEBUG_KMS("Wait for panel power on\n");
1022 ironlake_wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
1023}
1024
Keith Packardbd943152011-09-18 23:09:52 -07001025static void ironlake_wait_panel_off(struct intel_dp *intel_dp)
1026{
Keith Packardbd943152011-09-18 23:09:52 -07001027 DRM_DEBUG_KMS("Wait for panel power off time\n");
Keith Packard99ea7122011-11-01 19:57:50 -07001028 ironlake_wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
Keith Packardbd943152011-09-18 23:09:52 -07001029}
Keith Packardbd943152011-09-18 23:09:52 -07001030
Keith Packard99ea7122011-11-01 19:57:50 -07001031static void ironlake_wait_panel_power_cycle(struct intel_dp *intel_dp)
1032{
1033 DRM_DEBUG_KMS("Wait for panel power cycle\n");
1034 ironlake_wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
1035}
Keith Packardbd943152011-09-18 23:09:52 -07001036
Keith Packard99ea7122011-11-01 19:57:50 -07001037
Keith Packard832dd3c2011-11-01 19:34:06 -07001038/* Read the current pp_control value, unlocking the register if it
1039 * is locked
1040 */
1041
Jesse Barnes453c5422013-03-28 09:55:41 -07001042static u32 ironlake_get_pp_control(struct intel_dp *intel_dp)
Keith Packard832dd3c2011-11-01 19:34:06 -07001043{
Jesse Barnes453c5422013-03-28 09:55:41 -07001044 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1045 struct drm_i915_private *dev_priv = dev->dev_private;
1046 u32 control;
Jesse Barnes453c5422013-03-28 09:55:41 -07001047
Jani Nikulabf13e812013-09-06 07:40:05 +03001048 control = I915_READ(_pp_ctrl_reg(intel_dp));
Keith Packard832dd3c2011-11-01 19:34:06 -07001049 control &= ~PANEL_UNLOCK_MASK;
1050 control |= PANEL_UNLOCK_REGS;
1051 return control;
Keith Packardbd943152011-09-18 23:09:52 -07001052}
1053
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001054void ironlake_edp_panel_vdd_on(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;
Jesse Barnes453c5422013-03-28 09:55:41 -07001059 u32 pp_stat_reg, pp_ctrl_reg;
Jesse Barnes5d613502011-01-24 17:10:54 -08001060
Keith Packard97af61f572011-09-28 16:23:51 -07001061 if (!is_edp(intel_dp))
1062 return;
Keith Packardf01eca22011-09-28 16:48:10 -07001063 DRM_DEBUG_KMS("Turn eDP VDD on\n");
Jesse Barnes5d613502011-01-24 17:10:54 -08001064
Keith Packardbd943152011-09-18 23:09:52 -07001065 WARN(intel_dp->want_panel_vdd,
1066 "eDP VDD already requested on\n");
1067
1068 intel_dp->want_panel_vdd = true;
Keith Packard99ea7122011-11-01 19:57:50 -07001069
Keith Packardbd943152011-09-18 23:09:52 -07001070 if (ironlake_edp_have_panel_vdd(intel_dp)) {
1071 DRM_DEBUG_KMS("eDP VDD already on\n");
1072 return;
1073 }
1074
Keith Packard99ea7122011-11-01 19:57:50 -07001075 if (!ironlake_edp_have_panel_power(intel_dp))
1076 ironlake_wait_panel_power_cycle(intel_dp);
1077
Jesse Barnes453c5422013-03-28 09:55:41 -07001078 pp = ironlake_get_pp_control(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001079 pp |= EDP_FORCE_VDD;
Keith Packardebf33b12011-09-29 15:53:27 -07001080
Jani Nikulabf13e812013-09-06 07:40:05 +03001081 pp_stat_reg = _pp_stat_reg(intel_dp);
1082 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001083
1084 I915_WRITE(pp_ctrl_reg, pp);
1085 POSTING_READ(pp_ctrl_reg);
1086 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
1087 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
Keith Packardebf33b12011-09-29 15:53:27 -07001088 /*
1089 * If the panel wasn't on, delay before accessing aux channel
1090 */
1091 if (!ironlake_edp_have_panel_power(intel_dp)) {
Keith Packardbd943152011-09-18 23:09:52 -07001092 DRM_DEBUG_KMS("eDP was not running\n");
Keith Packardf01eca22011-09-28 16:48:10 -07001093 msleep(intel_dp->panel_power_up_delay);
Keith Packardf01eca22011-09-28 16:48:10 -07001094 }
Jesse Barnes5d613502011-01-24 17:10:54 -08001095}
1096
Keith Packardbd943152011-09-18 23:09:52 -07001097static void ironlake_panel_vdd_off_sync(struct intel_dp *intel_dp)
Jesse Barnes5d613502011-01-24 17:10:54 -08001098{
Paulo Zanoni30add222012-10-26 19:05:45 -02001099 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001100 struct drm_i915_private *dev_priv = dev->dev_private;
1101 u32 pp;
Jesse Barnes453c5422013-03-28 09:55:41 -07001102 u32 pp_stat_reg, pp_ctrl_reg;
Jesse Barnes5d613502011-01-24 17:10:54 -08001103
Daniel Vettera0e99e62012-12-02 01:05:46 +01001104 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
1105
Keith Packardbd943152011-09-18 23:09:52 -07001106 if (!intel_dp->want_panel_vdd && ironlake_edp_have_panel_vdd(intel_dp)) {
Jesse Barnes453c5422013-03-28 09:55:41 -07001107 pp = ironlake_get_pp_control(intel_dp);
Keith Packardbd943152011-09-18 23:09:52 -07001108 pp &= ~EDP_FORCE_VDD;
Jesse Barnes453c5422013-03-28 09:55:41 -07001109
Jani Nikulabf13e812013-09-06 07:40:05 +03001110 pp_stat_reg = _pp_ctrl_reg(intel_dp);
1111 pp_ctrl_reg = _pp_stat_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001112
1113 I915_WRITE(pp_ctrl_reg, pp);
1114 POSTING_READ(pp_ctrl_reg);
Jesse Barnes5d613502011-01-24 17:10:54 -08001115
Keith Packardbd943152011-09-18 23:09:52 -07001116 /* Make sure sequencer is idle before allowing subsequent activity */
Jesse Barnes453c5422013-03-28 09:55:41 -07001117 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
1118 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
Keith Packard99ea7122011-11-01 19:57:50 -07001119 msleep(intel_dp->panel_power_down_delay);
Keith Packardbd943152011-09-18 23:09:52 -07001120 }
1121}
1122
1123static void ironlake_panel_vdd_work(struct work_struct *__work)
1124{
1125 struct intel_dp *intel_dp = container_of(to_delayed_work(__work),
1126 struct intel_dp, panel_vdd_work);
Paulo Zanoni30add222012-10-26 19:05:45 -02001127 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packardbd943152011-09-18 23:09:52 -07001128
Keith Packard627f7672011-10-31 11:30:10 -07001129 mutex_lock(&dev->mode_config.mutex);
Keith Packardbd943152011-09-18 23:09:52 -07001130 ironlake_panel_vdd_off_sync(intel_dp);
Keith Packard627f7672011-10-31 11:30:10 -07001131 mutex_unlock(&dev->mode_config.mutex);
Keith Packardbd943152011-09-18 23:09:52 -07001132}
1133
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001134void ironlake_edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
Keith Packardbd943152011-09-18 23:09:52 -07001135{
Keith Packard97af61f572011-09-28 16:23:51 -07001136 if (!is_edp(intel_dp))
1137 return;
Jesse Barnes5d613502011-01-24 17:10:54 -08001138
Keith Packardbd943152011-09-18 23:09:52 -07001139 DRM_DEBUG_KMS("Turn eDP VDD off %d\n", intel_dp->want_panel_vdd);
1140 WARN(!intel_dp->want_panel_vdd, "eDP VDD not forced on");
Keith Packardf2e8b182011-11-01 20:01:35 -07001141
Keith Packardbd943152011-09-18 23:09:52 -07001142 intel_dp->want_panel_vdd = false;
1143
1144 if (sync) {
1145 ironlake_panel_vdd_off_sync(intel_dp);
1146 } else {
1147 /*
1148 * Queue the timer to fire a long
1149 * time from now (relative to the power down delay)
1150 * to keep the panel power up across a sequence of operations
1151 */
1152 schedule_delayed_work(&intel_dp->panel_vdd_work,
1153 msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5));
1154 }
Jesse Barnes5d613502011-01-24 17:10:54 -08001155}
1156
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001157void ironlake_edp_panel_on(struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -07001158{
Paulo Zanoni30add222012-10-26 19:05:45 -02001159 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001160 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packard99ea7122011-11-01 19:57:50 -07001161 u32 pp;
Jesse Barnes453c5422013-03-28 09:55:41 -07001162 u32 pp_ctrl_reg;
Jesse Barnes9934c132010-07-22 13:18:19 -07001163
Keith Packard97af61f572011-09-28 16:23:51 -07001164 if (!is_edp(intel_dp))
Keith Packardbd943152011-09-18 23:09:52 -07001165 return;
Keith Packard99ea7122011-11-01 19:57:50 -07001166
1167 DRM_DEBUG_KMS("Turn eDP power on\n");
1168
1169 if (ironlake_edp_have_panel_power(intel_dp)) {
1170 DRM_DEBUG_KMS("eDP power already on\n");
Keith Packard7d639f32011-09-29 16:05:34 -07001171 return;
Keith Packard99ea7122011-11-01 19:57:50 -07001172 }
Jesse Barnes9934c132010-07-22 13:18:19 -07001173
Keith Packard99ea7122011-11-01 19:57:50 -07001174 ironlake_wait_panel_power_cycle(intel_dp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001175
Jani Nikulabf13e812013-09-06 07:40:05 +03001176 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001177 pp = ironlake_get_pp_control(intel_dp);
Keith Packard05ce1a42011-09-29 16:33:01 -07001178 if (IS_GEN5(dev)) {
1179 /* ILK workaround: disable reset around power sequence */
1180 pp &= ~PANEL_POWER_RESET;
Jani Nikulabf13e812013-09-06 07:40:05 +03001181 I915_WRITE(pp_ctrl_reg, pp);
1182 POSTING_READ(pp_ctrl_reg);
Keith Packard05ce1a42011-09-29 16:33:01 -07001183 }
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001184
Keith Packard1c0ae802011-09-19 13:59:29 -07001185 pp |= POWER_TARGET_ON;
Keith Packard99ea7122011-11-01 19:57:50 -07001186 if (!IS_GEN5(dev))
1187 pp |= PANEL_POWER_RESET;
1188
Jesse Barnes453c5422013-03-28 09:55:41 -07001189 I915_WRITE(pp_ctrl_reg, pp);
1190 POSTING_READ(pp_ctrl_reg);
Jesse Barnes9934c132010-07-22 13:18:19 -07001191
Keith Packard99ea7122011-11-01 19:57:50 -07001192 ironlake_wait_panel_on(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001193
Keith Packard05ce1a42011-09-29 16:33:01 -07001194 if (IS_GEN5(dev)) {
1195 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jani Nikulabf13e812013-09-06 07:40:05 +03001196 I915_WRITE(pp_ctrl_reg, pp);
1197 POSTING_READ(pp_ctrl_reg);
Keith Packard05ce1a42011-09-29 16:33:01 -07001198 }
Jesse Barnes9934c132010-07-22 13:18:19 -07001199}
1200
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001201void ironlake_edp_panel_off(struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -07001202{
Paulo Zanoni30add222012-10-26 19:05:45 -02001203 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001204 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packard99ea7122011-11-01 19:57:50 -07001205 u32 pp;
Jesse Barnes453c5422013-03-28 09:55:41 -07001206 u32 pp_ctrl_reg;
Jesse Barnes9934c132010-07-22 13:18:19 -07001207
Keith Packard97af61f572011-09-28 16:23:51 -07001208 if (!is_edp(intel_dp))
1209 return;
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001210
Keith Packard99ea7122011-11-01 19:57:50 -07001211 DRM_DEBUG_KMS("Turn eDP power off\n");
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001212
Daniel Vetter6cb49832012-05-20 17:14:50 +02001213 WARN(!intel_dp->want_panel_vdd, "Need VDD to turn off panel\n");
Jesse Barnes9934c132010-07-22 13:18:19 -07001214
Jesse Barnes453c5422013-03-28 09:55:41 -07001215 pp = ironlake_get_pp_control(intel_dp);
Daniel Vetter35a38552012-08-12 22:17:14 +02001216 /* We need to switch off panel power _and_ force vdd, for otherwise some
1217 * panels get very unhappy and cease to work. */
1218 pp &= ~(POWER_TARGET_ON | EDP_FORCE_VDD | PANEL_POWER_RESET | EDP_BLC_ENABLE);
Jesse Barnes453c5422013-03-28 09:55:41 -07001219
Jani Nikulabf13e812013-09-06 07:40:05 +03001220 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001221
1222 I915_WRITE(pp_ctrl_reg, pp);
1223 POSTING_READ(pp_ctrl_reg);
Jesse Barnes9934c132010-07-22 13:18:19 -07001224
Daniel Vetter35a38552012-08-12 22:17:14 +02001225 intel_dp->want_panel_vdd = false;
1226
Keith Packard99ea7122011-11-01 19:57:50 -07001227 ironlake_wait_panel_off(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001228}
1229
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001230void ironlake_edp_backlight_on(struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001231{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02001232 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1233 struct drm_device *dev = intel_dig_port->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001234 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonida63a9f2012-10-26 19:05:46 -02001235 int pipe = to_intel_crtc(intel_dig_port->base.base.crtc)->pipe;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001236 u32 pp;
Jesse Barnes453c5422013-03-28 09:55:41 -07001237 u32 pp_ctrl_reg;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001238
Keith Packardf01eca22011-09-28 16:48:10 -07001239 if (!is_edp(intel_dp))
1240 return;
1241
Zhao Yakui28c97732009-10-09 11:39:41 +08001242 DRM_DEBUG_KMS("\n");
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001243 /*
1244 * If we enable the backlight right away following a panel power
1245 * on, we may see slight flicker as the panel syncs with the eDP
1246 * link. So delay a bit to make sure the image is solid before
1247 * allowing it to appear.
1248 */
Keith Packardf01eca22011-09-28 16:48:10 -07001249 msleep(intel_dp->backlight_on_delay);
Jesse Barnes453c5422013-03-28 09:55:41 -07001250 pp = ironlake_get_pp_control(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001251 pp |= EDP_BLC_ENABLE;
Jesse Barnes453c5422013-03-28 09:55:41 -07001252
Jani Nikulabf13e812013-09-06 07:40:05 +03001253 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001254
1255 I915_WRITE(pp_ctrl_reg, pp);
1256 POSTING_READ(pp_ctrl_reg);
Daniel Vetter035aa3d2012-10-20 20:57:42 +02001257
1258 intel_panel_enable_backlight(dev, pipe);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001259}
1260
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001261void ironlake_edp_backlight_off(struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001262{
Paulo Zanoni30add222012-10-26 19:05:45 -02001263 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001264 struct drm_i915_private *dev_priv = dev->dev_private;
1265 u32 pp;
Jesse Barnes453c5422013-03-28 09:55:41 -07001266 u32 pp_ctrl_reg;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001267
Keith Packardf01eca22011-09-28 16:48:10 -07001268 if (!is_edp(intel_dp))
1269 return;
1270
Daniel Vetter035aa3d2012-10-20 20:57:42 +02001271 intel_panel_disable_backlight(dev);
1272
Zhao Yakui28c97732009-10-09 11:39:41 +08001273 DRM_DEBUG_KMS("\n");
Jesse Barnes453c5422013-03-28 09:55:41 -07001274 pp = ironlake_get_pp_control(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001275 pp &= ~EDP_BLC_ENABLE;
Jesse Barnes453c5422013-03-28 09:55:41 -07001276
Jani Nikulabf13e812013-09-06 07:40:05 +03001277 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001278
1279 I915_WRITE(pp_ctrl_reg, pp);
1280 POSTING_READ(pp_ctrl_reg);
Keith Packardf01eca22011-09-28 16:48:10 -07001281 msleep(intel_dp->backlight_off_delay);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001282}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001283
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001284static void ironlake_edp_pll_on(struct intel_dp *intel_dp)
Jesse Barnesd240f202010-08-13 15:43:26 -07001285{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02001286 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1287 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
1288 struct drm_device *dev = crtc->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -07001289 struct drm_i915_private *dev_priv = dev->dev_private;
1290 u32 dpa_ctl;
1291
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001292 assert_pipe_disabled(dev_priv,
1293 to_intel_crtc(crtc)->pipe);
1294
Jesse Barnesd240f202010-08-13 15:43:26 -07001295 DRM_DEBUG_KMS("\n");
1296 dpa_ctl = I915_READ(DP_A);
Daniel Vetter07679352012-09-06 22:15:42 +02001297 WARN(dpa_ctl & DP_PLL_ENABLE, "dp pll on, should be off\n");
1298 WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
1299
1300 /* We don't adjust intel_dp->DP while tearing down the link, to
1301 * facilitate link retraining (e.g. after hotplug). Hence clear all
1302 * enable bits here to ensure that we don't enable too much. */
1303 intel_dp->DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
1304 intel_dp->DP |= DP_PLL_ENABLE;
1305 I915_WRITE(DP_A, intel_dp->DP);
Jesse Barnes298b0b32010-10-07 16:01:24 -07001306 POSTING_READ(DP_A);
1307 udelay(200);
Jesse Barnesd240f202010-08-13 15:43:26 -07001308}
1309
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001310static void ironlake_edp_pll_off(struct intel_dp *intel_dp)
Jesse Barnesd240f202010-08-13 15:43:26 -07001311{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02001312 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1313 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
1314 struct drm_device *dev = crtc->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -07001315 struct drm_i915_private *dev_priv = dev->dev_private;
1316 u32 dpa_ctl;
1317
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001318 assert_pipe_disabled(dev_priv,
1319 to_intel_crtc(crtc)->pipe);
1320
Jesse Barnesd240f202010-08-13 15:43:26 -07001321 dpa_ctl = I915_READ(DP_A);
Daniel Vetter07679352012-09-06 22:15:42 +02001322 WARN((dpa_ctl & DP_PLL_ENABLE) == 0,
1323 "dp pll off, should be on\n");
1324 WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
1325
1326 /* We can't rely on the value tracked for the DP register in
1327 * intel_dp->DP because link_down must not change that (otherwise link
1328 * re-training will fail. */
Jesse Barnes298b0b32010-10-07 16:01:24 -07001329 dpa_ctl &= ~DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -07001330 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +01001331 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -07001332 udelay(200);
1333}
1334
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001335/* If the sink supports it, try to set the power state appropriately */
Paulo Zanonic19b0662012-10-15 15:51:41 -03001336void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001337{
1338 int ret, i;
1339
1340 /* Should have a valid DPCD by this point */
1341 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
1342 return;
1343
1344 if (mode != DRM_MODE_DPMS_ON) {
1345 ret = intel_dp_aux_native_write_1(intel_dp, DP_SET_POWER,
1346 DP_SET_POWER_D3);
1347 if (ret != 1)
1348 DRM_DEBUG_DRIVER("failed to write sink power state\n");
1349 } else {
1350 /*
1351 * When turning on, we need to retry for 1ms to give the sink
1352 * time to wake up.
1353 */
1354 for (i = 0; i < 3; i++) {
1355 ret = intel_dp_aux_native_write_1(intel_dp,
1356 DP_SET_POWER,
1357 DP_SET_POWER_D0);
1358 if (ret == 1)
1359 break;
1360 msleep(1);
1361 }
1362 }
1363}
1364
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001365static bool intel_dp_get_hw_state(struct intel_encoder *encoder,
1366 enum pipe *pipe)
Jesse Barnesd240f202010-08-13 15:43:26 -07001367{
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001368 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deakbc7d38a2013-05-16 14:40:36 +03001369 enum port port = dp_to_dig_port(intel_dp)->port;
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001370 struct drm_device *dev = encoder->base.dev;
1371 struct drm_i915_private *dev_priv = dev->dev_private;
1372 u32 tmp = I915_READ(intel_dp->output_reg);
Jesse Barnesd240f202010-08-13 15:43:26 -07001373
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001374 if (!(tmp & DP_PORT_EN))
1375 return false;
1376
Imre Deakbc7d38a2013-05-16 14:40:36 +03001377 if (port == PORT_A && IS_GEN7(dev) && !IS_VALLEYVIEW(dev)) {
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001378 *pipe = PORT_TO_PIPE_CPT(tmp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03001379 } else if (!HAS_PCH_CPT(dev) || port == PORT_A) {
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001380 *pipe = PORT_TO_PIPE(tmp);
1381 } else {
1382 u32 trans_sel;
1383 u32 trans_dp;
1384 int i;
1385
1386 switch (intel_dp->output_reg) {
1387 case PCH_DP_B:
1388 trans_sel = TRANS_DP_PORT_SEL_B;
1389 break;
1390 case PCH_DP_C:
1391 trans_sel = TRANS_DP_PORT_SEL_C;
1392 break;
1393 case PCH_DP_D:
1394 trans_sel = TRANS_DP_PORT_SEL_D;
1395 break;
1396 default:
1397 return true;
1398 }
1399
1400 for_each_pipe(i) {
1401 trans_dp = I915_READ(TRANS_DP_CTL(i));
1402 if ((trans_dp & TRANS_DP_PORT_SEL_MASK) == trans_sel) {
1403 *pipe = i;
1404 return true;
1405 }
1406 }
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001407
Daniel Vetter4a0833e2012-10-26 10:58:11 +02001408 DRM_DEBUG_KMS("No pipe for dp port 0x%x found\n",
1409 intel_dp->output_reg);
1410 }
Daniel Vetter19d8fe12012-07-02 13:26:27 +02001411
1412 return true;
1413}
1414
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001415static void intel_dp_get_config(struct intel_encoder *encoder,
1416 struct intel_crtc_config *pipe_config)
1417{
1418 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001419 u32 tmp, flags = 0;
Xiong Zhang63000ef2013-06-28 12:59:06 +08001420 struct drm_device *dev = encoder->base.dev;
1421 struct drm_i915_private *dev_priv = dev->dev_private;
1422 enum port port = dp_to_dig_port(intel_dp)->port;
1423 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
Ville Syrjälä18442d02013-09-13 16:00:08 +03001424 int dotclock;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001425
Xiong Zhang63000ef2013-06-28 12:59:06 +08001426 if ((port == PORT_A) || !HAS_PCH_CPT(dev)) {
1427 tmp = I915_READ(intel_dp->output_reg);
1428 if (tmp & DP_SYNC_HS_HIGH)
1429 flags |= DRM_MODE_FLAG_PHSYNC;
1430 else
1431 flags |= DRM_MODE_FLAG_NHSYNC;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001432
Xiong Zhang63000ef2013-06-28 12:59:06 +08001433 if (tmp & DP_SYNC_VS_HIGH)
1434 flags |= DRM_MODE_FLAG_PVSYNC;
1435 else
1436 flags |= DRM_MODE_FLAG_NVSYNC;
1437 } else {
1438 tmp = I915_READ(TRANS_DP_CTL(crtc->pipe));
1439 if (tmp & TRANS_DP_HSYNC_ACTIVE_HIGH)
1440 flags |= DRM_MODE_FLAG_PHSYNC;
1441 else
1442 flags |= DRM_MODE_FLAG_NHSYNC;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001443
Xiong Zhang63000ef2013-06-28 12:59:06 +08001444 if (tmp & TRANS_DP_VSYNC_ACTIVE_HIGH)
1445 flags |= DRM_MODE_FLAG_PVSYNC;
1446 else
1447 flags |= DRM_MODE_FLAG_NVSYNC;
1448 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001449
1450 pipe_config->adjusted_mode.flags |= flags;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03001451
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03001452 pipe_config->has_dp_encoder = true;
1453
1454 intel_dp_get_m_n(crtc, pipe_config);
1455
Ville Syrjälä18442d02013-09-13 16:00:08 +03001456 if (port == PORT_A) {
Jesse Barnesf1f644d2013-06-27 00:39:25 +03001457 if ((I915_READ(DP_A) & DP_PLL_FREQ_MASK) == DP_PLL_FREQ_160MHZ)
1458 pipe_config->port_clock = 162000;
1459 else
1460 pipe_config->port_clock = 270000;
1461 }
Ville Syrjälä18442d02013-09-13 16:00:08 +03001462
1463 dotclock = intel_dotclock_calculate(pipe_config->port_clock,
1464 &pipe_config->dp_m_n);
1465
1466 if (HAS_PCH_SPLIT(dev_priv->dev) && port != PORT_A)
1467 ironlake_check_encoder_dotclock(pipe_config, dotclock);
1468
1469 pipe_config->adjusted_mode.clock = dotclock;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001470}
1471
Shobhit Kumar2293bb52013-07-11 18:44:56 -03001472static bool is_edp_psr(struct intel_dp *intel_dp)
1473{
1474 return is_edp(intel_dp) &&
1475 intel_dp->psr_dpcd[0] & DP_PSR_IS_SUPPORTED;
1476}
1477
Rodrigo Vivi2b28bb12013-07-11 18:44:58 -03001478static bool intel_edp_is_psr_enabled(struct drm_device *dev)
1479{
1480 struct drm_i915_private *dev_priv = dev->dev_private;
1481
1482 if (!IS_HASWELL(dev))
1483 return false;
1484
1485 return I915_READ(EDP_PSR_CTL) & EDP_PSR_ENABLE;
1486}
1487
1488static void intel_edp_psr_write_vsc(struct intel_dp *intel_dp,
1489 struct edp_vsc_psr *vsc_psr)
1490{
1491 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1492 struct drm_device *dev = dig_port->base.base.dev;
1493 struct drm_i915_private *dev_priv = dev->dev_private;
1494 struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
1495 u32 ctl_reg = HSW_TVIDEO_DIP_CTL(crtc->config.cpu_transcoder);
1496 u32 data_reg = HSW_TVIDEO_DIP_VSC_DATA(crtc->config.cpu_transcoder);
1497 uint32_t *data = (uint32_t *) vsc_psr;
1498 unsigned int i;
1499
1500 /* As per BSPec (Pipe Video Data Island Packet), we need to disable
1501 the video DIP being updated before program video DIP data buffer
1502 registers for DIP being updated. */
1503 I915_WRITE(ctl_reg, 0);
1504 POSTING_READ(ctl_reg);
1505
1506 for (i = 0; i < VIDEO_DIP_VSC_DATA_SIZE; i += 4) {
1507 if (i < sizeof(struct edp_vsc_psr))
1508 I915_WRITE(data_reg + i, *data++);
1509 else
1510 I915_WRITE(data_reg + i, 0);
1511 }
1512
1513 I915_WRITE(ctl_reg, VIDEO_DIP_ENABLE_VSC_HSW);
1514 POSTING_READ(ctl_reg);
1515}
1516
1517static void intel_edp_psr_setup(struct intel_dp *intel_dp)
1518{
1519 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1520 struct drm_i915_private *dev_priv = dev->dev_private;
1521 struct edp_vsc_psr psr_vsc;
1522
1523 if (intel_dp->psr_setup_done)
1524 return;
1525
1526 /* Prepare VSC packet as per EDP 1.3 spec, Table 3.10 */
1527 memset(&psr_vsc, 0, sizeof(psr_vsc));
1528 psr_vsc.sdp_header.HB0 = 0;
1529 psr_vsc.sdp_header.HB1 = 0x7;
1530 psr_vsc.sdp_header.HB2 = 0x2;
1531 psr_vsc.sdp_header.HB3 = 0x8;
1532 intel_edp_psr_write_vsc(intel_dp, &psr_vsc);
1533
1534 /* Avoid continuous PSR exit by masking memup and hpd */
1535 I915_WRITE(EDP_PSR_DEBUG_CTL, EDP_PSR_DEBUG_MASK_MEMUP |
1536 EDP_PSR_DEBUG_MASK_HPD);
1537
1538 intel_dp->psr_setup_done = true;
1539}
1540
1541static void intel_edp_psr_enable_sink(struct intel_dp *intel_dp)
1542{
1543 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1544 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbc866252013-07-21 16:00:03 +01001545 uint32_t aux_clock_divider = get_aux_clock_divider(intel_dp, 0);
Rodrigo Vivi2b28bb12013-07-11 18:44:58 -03001546 int precharge = 0x3;
1547 int msg_size = 5; /* Header(4) + Message(1) */
1548
1549 /* Enable PSR in sink */
1550 if (intel_dp->psr_dpcd[1] & DP_PSR_NO_TRAIN_ON_EXIT)
1551 intel_dp_aux_native_write_1(intel_dp, DP_PSR_EN_CFG,
1552 DP_PSR_ENABLE &
1553 ~DP_PSR_MAIN_LINK_ACTIVE);
1554 else
1555 intel_dp_aux_native_write_1(intel_dp, DP_PSR_EN_CFG,
1556 DP_PSR_ENABLE |
1557 DP_PSR_MAIN_LINK_ACTIVE);
1558
1559 /* Setup AUX registers */
1560 I915_WRITE(EDP_PSR_AUX_DATA1, EDP_PSR_DPCD_COMMAND);
1561 I915_WRITE(EDP_PSR_AUX_DATA2, EDP_PSR_DPCD_NORMAL_OPERATION);
1562 I915_WRITE(EDP_PSR_AUX_CTL,
1563 DP_AUX_CH_CTL_TIME_OUT_400us |
1564 (msg_size << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
1565 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
1566 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT));
1567}
1568
1569static void intel_edp_psr_enable_source(struct intel_dp *intel_dp)
1570{
1571 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1572 struct drm_i915_private *dev_priv = dev->dev_private;
1573 uint32_t max_sleep_time = 0x1f;
1574 uint32_t idle_frames = 1;
1575 uint32_t val = 0x0;
1576
1577 if (intel_dp->psr_dpcd[1] & DP_PSR_NO_TRAIN_ON_EXIT) {
1578 val |= EDP_PSR_LINK_STANDBY;
1579 val |= EDP_PSR_TP2_TP3_TIME_0us;
1580 val |= EDP_PSR_TP1_TIME_0us;
1581 val |= EDP_PSR_SKIP_AUX_EXIT;
1582 } else
1583 val |= EDP_PSR_LINK_DISABLE;
1584
1585 I915_WRITE(EDP_PSR_CTL, val |
1586 EDP_PSR_MIN_LINK_ENTRY_TIME_8_LINES |
1587 max_sleep_time << EDP_PSR_MAX_SLEEP_TIME_SHIFT |
1588 idle_frames << EDP_PSR_IDLE_FRAME_SHIFT |
1589 EDP_PSR_ENABLE);
1590}
1591
Rodrigo Vivi3f51e472013-07-11 18:45:00 -03001592static bool intel_edp_psr_match_conditions(struct intel_dp *intel_dp)
1593{
1594 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1595 struct drm_device *dev = dig_port->base.base.dev;
1596 struct drm_i915_private *dev_priv = dev->dev_private;
1597 struct drm_crtc *crtc = dig_port->base.base.crtc;
1598 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1599 struct drm_i915_gem_object *obj = to_intel_framebuffer(crtc->fb)->obj;
1600 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
1601
1602 if (!IS_HASWELL(dev)) {
1603 DRM_DEBUG_KMS("PSR not supported on this platform\n");
1604 dev_priv->no_psr_reason = PSR_NO_SOURCE;
1605 return false;
1606 }
1607
1608 if ((intel_encoder->type != INTEL_OUTPUT_EDP) ||
1609 (dig_port->port != PORT_A)) {
1610 DRM_DEBUG_KMS("HSW ties PSR to DDI A (eDP)\n");
1611 dev_priv->no_psr_reason = PSR_HSW_NOT_DDIA;
1612 return false;
1613 }
1614
1615 if (!is_edp_psr(intel_dp)) {
1616 DRM_DEBUG_KMS("PSR not supported by this panel\n");
1617 dev_priv->no_psr_reason = PSR_NO_SINK;
1618 return false;
1619 }
1620
Rodrigo Vivi105b7c12013-07-11 18:45:02 -03001621 if (!i915_enable_psr) {
1622 DRM_DEBUG_KMS("PSR disable by flag\n");
1623 dev_priv->no_psr_reason = PSR_MODULE_PARAM;
1624 return false;
1625 }
1626
Chris Wilsoncd234b02013-08-02 20:39:49 +01001627 crtc = dig_port->base.base.crtc;
1628 if (crtc == NULL) {
1629 DRM_DEBUG_KMS("crtc not active for PSR\n");
1630 dev_priv->no_psr_reason = PSR_CRTC_NOT_ACTIVE;
1631 return false;
1632 }
1633
1634 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001635 if (!intel_crtc_active(crtc)) {
Rodrigo Vivi3f51e472013-07-11 18:45:00 -03001636 DRM_DEBUG_KMS("crtc not active for PSR\n");
1637 dev_priv->no_psr_reason = PSR_CRTC_NOT_ACTIVE;
1638 return false;
1639 }
1640
Chris Wilsoncd234b02013-08-02 20:39:49 +01001641 obj = to_intel_framebuffer(crtc->fb)->obj;
Rodrigo Vivi3f51e472013-07-11 18:45:00 -03001642 if (obj->tiling_mode != I915_TILING_X ||
1643 obj->fence_reg == I915_FENCE_REG_NONE) {
1644 DRM_DEBUG_KMS("PSR condition failed: fb not tiled or fenced\n");
1645 dev_priv->no_psr_reason = PSR_NOT_TILED;
1646 return false;
1647 }
1648
1649 if (I915_READ(SPRCTL(intel_crtc->pipe)) & SPRITE_ENABLE) {
1650 DRM_DEBUG_KMS("PSR condition failed: Sprite is Enabled\n");
1651 dev_priv->no_psr_reason = PSR_SPRITE_ENABLED;
1652 return false;
1653 }
1654
1655 if (I915_READ(HSW_STEREO_3D_CTL(intel_crtc->config.cpu_transcoder)) &
1656 S3D_ENABLE) {
1657 DRM_DEBUG_KMS("PSR condition failed: Stereo 3D is Enabled\n");
1658 dev_priv->no_psr_reason = PSR_S3D_ENABLED;
1659 return false;
1660 }
1661
Ville Syrjäläca73b4f2013-09-04 18:25:24 +03001662 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Rodrigo Vivi3f51e472013-07-11 18:45:00 -03001663 DRM_DEBUG_KMS("PSR condition failed: Interlaced is Enabled\n");
1664 dev_priv->no_psr_reason = PSR_INTERLACED_ENABLED;
1665 return false;
1666 }
1667
1668 return true;
1669}
1670
Rodrigo Vivi3d739d92013-07-11 18:45:01 -03001671static void intel_edp_psr_do_enable(struct intel_dp *intel_dp)
Rodrigo Vivi2b28bb12013-07-11 18:44:58 -03001672{
1673 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1674
Rodrigo Vivi3f51e472013-07-11 18:45:00 -03001675 if (!intel_edp_psr_match_conditions(intel_dp) ||
1676 intel_edp_is_psr_enabled(dev))
Rodrigo Vivi2b28bb12013-07-11 18:44:58 -03001677 return;
1678
1679 /* Setup PSR once */
1680 intel_edp_psr_setup(intel_dp);
1681
1682 /* Enable PSR on the panel */
1683 intel_edp_psr_enable_sink(intel_dp);
1684
1685 /* Enable PSR on the host */
1686 intel_edp_psr_enable_source(intel_dp);
1687}
1688
Rodrigo Vivi3d739d92013-07-11 18:45:01 -03001689void intel_edp_psr_enable(struct intel_dp *intel_dp)
1690{
1691 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1692
1693 if (intel_edp_psr_match_conditions(intel_dp) &&
1694 !intel_edp_is_psr_enabled(dev))
1695 intel_edp_psr_do_enable(intel_dp);
1696}
1697
Rodrigo Vivi2b28bb12013-07-11 18:44:58 -03001698void intel_edp_psr_disable(struct intel_dp *intel_dp)
1699{
1700 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1701 struct drm_i915_private *dev_priv = dev->dev_private;
1702
1703 if (!intel_edp_is_psr_enabled(dev))
1704 return;
1705
1706 I915_WRITE(EDP_PSR_CTL, I915_READ(EDP_PSR_CTL) & ~EDP_PSR_ENABLE);
1707
1708 /* Wait till PSR is idle */
1709 if (_wait_for((I915_READ(EDP_PSR_STATUS_CTL) &
1710 EDP_PSR_STATUS_STATE_MASK) == 0, 2000, 10))
1711 DRM_ERROR("Timed out waiting for PSR Idle State\n");
1712}
1713
Rodrigo Vivi3d739d92013-07-11 18:45:01 -03001714void intel_edp_psr_update(struct drm_device *dev)
1715{
1716 struct intel_encoder *encoder;
1717 struct intel_dp *intel_dp = NULL;
1718
1719 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head)
1720 if (encoder->type == INTEL_OUTPUT_EDP) {
1721 intel_dp = enc_to_intel_dp(&encoder->base);
1722
1723 if (!is_edp_psr(intel_dp))
1724 return;
1725
1726 if (!intel_edp_psr_match_conditions(intel_dp))
1727 intel_edp_psr_disable(intel_dp);
1728 else
1729 if (!intel_edp_is_psr_enabled(dev))
1730 intel_edp_psr_do_enable(intel_dp);
1731 }
1732}
1733
Daniel Vettere8cb4552012-07-01 13:05:48 +02001734static void intel_disable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07001735{
Daniel Vettere8cb4552012-07-01 13:05:48 +02001736 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deak982a3862013-05-23 19:39:40 +03001737 enum port port = dp_to_dig_port(intel_dp)->port;
1738 struct drm_device *dev = encoder->base.dev;
Daniel Vetter6cb49832012-05-20 17:14:50 +02001739
1740 /* Make sure the panel is off before trying to change the mode. But also
1741 * ensure that we have vdd while we switch off the panel. */
1742 ironlake_edp_panel_vdd_on(intel_dp);
Keith Packard21264c62011-11-01 20:25:21 -07001743 ironlake_edp_backlight_off(intel_dp);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07001744 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
Daniel Vetter35a38552012-08-12 22:17:14 +02001745 ironlake_edp_panel_off(intel_dp);
Daniel Vetter37398502012-09-06 22:15:44 +02001746
1747 /* cpu edp my only be disable _after_ the cpu pipe/plane is disabled. */
Imre Deak982a3862013-05-23 19:39:40 +03001748 if (!(port == PORT_A || IS_VALLEYVIEW(dev)))
Daniel Vetter37398502012-09-06 22:15:44 +02001749 intel_dp_link_down(intel_dp);
Jesse Barnesd240f202010-08-13 15:43:26 -07001750}
1751
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001752static void intel_post_disable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07001753{
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001754 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deak982a3862013-05-23 19:39:40 +03001755 enum port port = dp_to_dig_port(intel_dp)->port;
Jesse Barnesb2634012013-03-28 09:55:40 -07001756 struct drm_device *dev = encoder->base.dev;
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001757
Imre Deak982a3862013-05-23 19:39:40 +03001758 if (port == PORT_A || IS_VALLEYVIEW(dev)) {
Daniel Vetter37398502012-09-06 22:15:44 +02001759 intel_dp_link_down(intel_dp);
Jesse Barnesb2634012013-03-28 09:55:40 -07001760 if (!IS_VALLEYVIEW(dev))
1761 ironlake_edp_pll_off(intel_dp);
Daniel Vetter37398502012-09-06 22:15:44 +02001762 }
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001763}
1764
Daniel Vettere8cb4552012-07-01 13:05:48 +02001765static void intel_enable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07001766{
Daniel Vettere8cb4552012-07-01 13:05:48 +02001767 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1768 struct drm_device *dev = encoder->base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001769 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001770 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001771
Daniel Vetter0c33d8d2012-09-06 22:15:43 +02001772 if (WARN_ON(dp_reg & DP_PORT_EN))
1773 return;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001774
1775 ironlake_edp_panel_vdd_on(intel_dp);
1776 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
1777 intel_dp_start_link_train(intel_dp);
1778 ironlake_edp_panel_on(intel_dp);
1779 ironlake_edp_panel_vdd_off(intel_dp, true);
1780 intel_dp_complete_link_train(intel_dp);
Imre Deak3ab9c632013-05-03 12:57:41 +03001781 intel_dp_stop_link_train(intel_dp);
Jani Nikulaab1f90f2013-07-30 12:20:30 +03001782}
Jesse Barnes89b667f2013-04-18 14:51:36 -07001783
Jani Nikulaecff4f32013-09-06 07:38:29 +03001784static void g4x_enable_dp(struct intel_encoder *encoder)
1785{
Jani Nikula828f5c62013-09-05 16:44:45 +03001786 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1787
Jani Nikulaecff4f32013-09-06 07:38:29 +03001788 intel_enable_dp(encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001789 ironlake_edp_backlight_on(intel_dp);
1790}
Jesse Barnes89b667f2013-04-18 14:51:36 -07001791
Jani Nikulaab1f90f2013-07-30 12:20:30 +03001792static void vlv_enable_dp(struct intel_encoder *encoder)
1793{
Jani Nikula828f5c62013-09-05 16:44:45 +03001794 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1795
1796 ironlake_edp_backlight_on(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001797}
1798
Jani Nikulaecff4f32013-09-06 07:38:29 +03001799static void g4x_pre_enable_dp(struct intel_encoder *encoder)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001800{
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02001801 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deakbc7d38a2013-05-16 14:40:36 +03001802 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
Jani Nikulaab1f90f2013-07-30 12:20:30 +03001803
1804 if (dport->port == PORT_A)
1805 ironlake_edp_pll_on(intel_dp);
1806}
1807
1808static void vlv_pre_enable_dp(struct intel_encoder *encoder)
1809{
1810 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1811 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
Jesse Barnesb2634012013-03-28 09:55:40 -07001812 struct drm_device *dev = encoder->base.dev;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001813 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03001814 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
1815 int port = vlv_dport_to_channel(dport);
1816 int pipe = intel_crtc->pipe;
Jani Nikulabf13e812013-09-06 07:40:05 +03001817 struct edp_power_seq power_seq;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03001818 u32 val;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001819
Jani Nikulaab1f90f2013-07-30 12:20:30 +03001820 mutex_lock(&dev_priv->dpio_lock);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001821
Chon Ming Lee5e69f972013-09-05 20:41:49 +08001822 val = vlv_dpio_read(dev_priv, pipe, DPIO_DATA_LANE_A(port));
Jani Nikulaab1f90f2013-07-30 12:20:30 +03001823 val = 0;
1824 if (pipe)
1825 val |= (1<<21);
1826 else
1827 val &= ~(1<<21);
1828 val |= 0x001000c4;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08001829 vlv_dpio_write(dev_priv, pipe, DPIO_DATA_CHANNEL(port), val);
1830 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_CLOCKBUF0(port), 0x00760018);
1831 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_CLOCKBUF8(port), 0x00400888);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001832
Jani Nikulaab1f90f2013-07-30 12:20:30 +03001833 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001834
Jani Nikulabf13e812013-09-06 07:40:05 +03001835 /* init power sequencer on this pipe and port */
1836 intel_dp_init_panel_power_sequencer(dev, intel_dp, &power_seq);
1837 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp,
1838 &power_seq);
1839
Jani Nikulaab1f90f2013-07-30 12:20:30 +03001840 intel_enable_dp(encoder);
1841
1842 vlv_wait_port_ready(dev_priv, port);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001843}
1844
Jani Nikulaecff4f32013-09-06 07:38:29 +03001845static void vlv_dp_pre_pll_enable(struct intel_encoder *encoder)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001846{
1847 struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
1848 struct drm_device *dev = encoder->base.dev;
1849 struct drm_i915_private *dev_priv = dev->dev_private;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08001850 struct intel_crtc *intel_crtc =
1851 to_intel_crtc(encoder->base.crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001852 int port = vlv_dport_to_channel(dport);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08001853 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001854
Jesse Barnes89b667f2013-04-18 14:51:36 -07001855 /* Program Tx lane resets to default */
Chris Wilson0980a602013-07-26 19:57:35 +01001856 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08001857 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_TX(port),
Jesse Barnes89b667f2013-04-18 14:51:36 -07001858 DPIO_PCS_TX_LANE2_RESET |
1859 DPIO_PCS_TX_LANE1_RESET);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08001860 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_CLK(port),
Jesse Barnes89b667f2013-04-18 14:51:36 -07001861 DPIO_PCS_CLK_CRI_RXEB_EIOS_EN |
1862 DPIO_PCS_CLK_CRI_RXDIGFILTSG_EN |
1863 (1<<DPIO_PCS_CLK_DATAWIDTH_SHIFT) |
1864 DPIO_PCS_CLK_SOFT_RESET);
1865
1866 /* Fix up inter-pair skew failure */
Chon Ming Lee5e69f972013-09-05 20:41:49 +08001867 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_STAGGER1(port), 0x00750f00);
1868 vlv_dpio_write(dev_priv, pipe, DPIO_TX_CTL(port), 0x00001500);
1869 vlv_dpio_write(dev_priv, pipe, DPIO_TX_LANE(port), 0x40400000);
Chris Wilson0980a602013-07-26 19:57:35 +01001870 mutex_unlock(&dev_priv->dpio_lock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001871}
1872
1873/*
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001874 * Native read with retry for link status and receiver capability reads for
1875 * cases where the sink may still be asleep.
1876 */
1877static bool
1878intel_dp_aux_native_read_retry(struct intel_dp *intel_dp, uint16_t address,
1879 uint8_t *recv, int recv_bytes)
1880{
1881 int ret, i;
1882
1883 /*
1884 * Sinks are *supposed* to come up within 1ms from an off state,
1885 * but we're also supposed to retry 3 times per the spec.
1886 */
1887 for (i = 0; i < 3; i++) {
1888 ret = intel_dp_aux_native_read(intel_dp, address, recv,
1889 recv_bytes);
1890 if (ret == recv_bytes)
1891 return true;
1892 msleep(1);
1893 }
1894
1895 return false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001896}
1897
1898/*
1899 * Fetch AUX CH registers 0x202 - 0x207 which contain
1900 * link status information
1901 */
1902static bool
Keith Packard93f62da2011-11-01 19:45:03 -07001903intel_dp_get_link_status(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001904{
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001905 return intel_dp_aux_native_read_retry(intel_dp,
1906 DP_LANE0_1_STATUS,
Keith Packard93f62da2011-11-01 19:45:03 -07001907 link_status,
Jesse Barnesdf0c2372011-07-07 11:11:02 -07001908 DP_LINK_STATUS_SIZE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001909}
1910
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001911#if 0
1912static char *voltage_names[] = {
1913 "0.4V", "0.6V", "0.8V", "1.2V"
1914};
1915static char *pre_emph_names[] = {
1916 "0dB", "3.5dB", "6dB", "9.5dB"
1917};
1918static char *link_train_names[] = {
1919 "pattern 1", "pattern 2", "idle", "off"
1920};
1921#endif
1922
1923/*
1924 * These are source-specific values; current Intel hardware supports
1925 * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
1926 */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001927
1928static uint8_t
Keith Packard1a2eb462011-11-16 16:26:07 -08001929intel_dp_voltage_max(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001930{
Paulo Zanoni30add222012-10-26 19:05:45 -02001931 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03001932 enum port port = dp_to_dig_port(intel_dp)->port;
Keith Packard1a2eb462011-11-16 16:26:07 -08001933
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07001934 if (IS_VALLEYVIEW(dev))
1935 return DP_TRAIN_VOLTAGE_SWING_1200;
Imre Deakbc7d38a2013-05-16 14:40:36 +03001936 else if (IS_GEN7(dev) && port == PORT_A)
Keith Packard1a2eb462011-11-16 16:26:07 -08001937 return DP_TRAIN_VOLTAGE_SWING_800;
Imre Deakbc7d38a2013-05-16 14:40:36 +03001938 else if (HAS_PCH_CPT(dev) && port != PORT_A)
Keith Packard1a2eb462011-11-16 16:26:07 -08001939 return DP_TRAIN_VOLTAGE_SWING_1200;
1940 else
1941 return DP_TRAIN_VOLTAGE_SWING_800;
1942}
1943
1944static uint8_t
1945intel_dp_pre_emphasis_max(struct intel_dp *intel_dp, uint8_t voltage_swing)
1946{
Paulo Zanoni30add222012-10-26 19:05:45 -02001947 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03001948 enum port port = dp_to_dig_port(intel_dp)->port;
Keith Packard1a2eb462011-11-16 16:26:07 -08001949
Paulo Zanoni22b8bf12013-02-18 19:00:23 -03001950 if (HAS_DDI(dev)) {
Paulo Zanonid6c0d722012-10-15 15:51:34 -03001951 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1952 case DP_TRAIN_VOLTAGE_SWING_400:
1953 return DP_TRAIN_PRE_EMPHASIS_9_5;
1954 case DP_TRAIN_VOLTAGE_SWING_600:
1955 return DP_TRAIN_PRE_EMPHASIS_6;
1956 case DP_TRAIN_VOLTAGE_SWING_800:
1957 return DP_TRAIN_PRE_EMPHASIS_3_5;
1958 case DP_TRAIN_VOLTAGE_SWING_1200:
1959 default:
1960 return DP_TRAIN_PRE_EMPHASIS_0;
1961 }
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07001962 } else if (IS_VALLEYVIEW(dev)) {
1963 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1964 case DP_TRAIN_VOLTAGE_SWING_400:
1965 return DP_TRAIN_PRE_EMPHASIS_9_5;
1966 case DP_TRAIN_VOLTAGE_SWING_600:
1967 return DP_TRAIN_PRE_EMPHASIS_6;
1968 case DP_TRAIN_VOLTAGE_SWING_800:
1969 return DP_TRAIN_PRE_EMPHASIS_3_5;
1970 case DP_TRAIN_VOLTAGE_SWING_1200:
1971 default:
1972 return DP_TRAIN_PRE_EMPHASIS_0;
1973 }
Imre Deakbc7d38a2013-05-16 14:40:36 +03001974 } else if (IS_GEN7(dev) && port == PORT_A) {
Keith Packard1a2eb462011-11-16 16:26:07 -08001975 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1976 case DP_TRAIN_VOLTAGE_SWING_400:
1977 return DP_TRAIN_PRE_EMPHASIS_6;
1978 case DP_TRAIN_VOLTAGE_SWING_600:
1979 case DP_TRAIN_VOLTAGE_SWING_800:
1980 return DP_TRAIN_PRE_EMPHASIS_3_5;
1981 default:
1982 return DP_TRAIN_PRE_EMPHASIS_0;
1983 }
1984 } else {
1985 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
1986 case DP_TRAIN_VOLTAGE_SWING_400:
1987 return DP_TRAIN_PRE_EMPHASIS_6;
1988 case DP_TRAIN_VOLTAGE_SWING_600:
1989 return DP_TRAIN_PRE_EMPHASIS_6;
1990 case DP_TRAIN_VOLTAGE_SWING_800:
1991 return DP_TRAIN_PRE_EMPHASIS_3_5;
1992 case DP_TRAIN_VOLTAGE_SWING_1200:
1993 default:
1994 return DP_TRAIN_PRE_EMPHASIS_0;
1995 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001996 }
1997}
1998
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07001999static uint32_t intel_vlv_signal_levels(struct intel_dp *intel_dp)
2000{
2001 struct drm_device *dev = intel_dp_to_dev(intel_dp);
2002 struct drm_i915_private *dev_priv = dev->dev_private;
2003 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08002004 struct intel_crtc *intel_crtc =
2005 to_intel_crtc(dport->base.base.crtc);
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002006 unsigned long demph_reg_value, preemph_reg_value,
2007 uniqtranscale_reg_value;
2008 uint8_t train_set = intel_dp->train_set[0];
Jesse Barnescece5d52013-04-19 08:46:35 -07002009 int port = vlv_dport_to_channel(dport);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08002010 int pipe = intel_crtc->pipe;
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002011
2012 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
2013 case DP_TRAIN_PRE_EMPHASIS_0:
2014 preemph_reg_value = 0x0004000;
2015 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
2016 case DP_TRAIN_VOLTAGE_SWING_400:
2017 demph_reg_value = 0x2B405555;
2018 uniqtranscale_reg_value = 0x552AB83A;
2019 break;
2020 case DP_TRAIN_VOLTAGE_SWING_600:
2021 demph_reg_value = 0x2B404040;
2022 uniqtranscale_reg_value = 0x5548B83A;
2023 break;
2024 case DP_TRAIN_VOLTAGE_SWING_800:
2025 demph_reg_value = 0x2B245555;
2026 uniqtranscale_reg_value = 0x5560B83A;
2027 break;
2028 case DP_TRAIN_VOLTAGE_SWING_1200:
2029 demph_reg_value = 0x2B405555;
2030 uniqtranscale_reg_value = 0x5598DA3A;
2031 break;
2032 default:
2033 return 0;
2034 }
2035 break;
2036 case DP_TRAIN_PRE_EMPHASIS_3_5:
2037 preemph_reg_value = 0x0002000;
2038 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
2039 case DP_TRAIN_VOLTAGE_SWING_400:
2040 demph_reg_value = 0x2B404040;
2041 uniqtranscale_reg_value = 0x5552B83A;
2042 break;
2043 case DP_TRAIN_VOLTAGE_SWING_600:
2044 demph_reg_value = 0x2B404848;
2045 uniqtranscale_reg_value = 0x5580B83A;
2046 break;
2047 case DP_TRAIN_VOLTAGE_SWING_800:
2048 demph_reg_value = 0x2B404040;
2049 uniqtranscale_reg_value = 0x55ADDA3A;
2050 break;
2051 default:
2052 return 0;
2053 }
2054 break;
2055 case DP_TRAIN_PRE_EMPHASIS_6:
2056 preemph_reg_value = 0x0000000;
2057 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
2058 case DP_TRAIN_VOLTAGE_SWING_400:
2059 demph_reg_value = 0x2B305555;
2060 uniqtranscale_reg_value = 0x5570B83A;
2061 break;
2062 case DP_TRAIN_VOLTAGE_SWING_600:
2063 demph_reg_value = 0x2B2B4040;
2064 uniqtranscale_reg_value = 0x55ADDA3A;
2065 break;
2066 default:
2067 return 0;
2068 }
2069 break;
2070 case DP_TRAIN_PRE_EMPHASIS_9_5:
2071 preemph_reg_value = 0x0006000;
2072 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
2073 case DP_TRAIN_VOLTAGE_SWING_400:
2074 demph_reg_value = 0x1B405555;
2075 uniqtranscale_reg_value = 0x55ADDA3A;
2076 break;
2077 default:
2078 return 0;
2079 }
2080 break;
2081 default:
2082 return 0;
2083 }
2084
Chris Wilson0980a602013-07-26 19:57:35 +01002085 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08002086 vlv_dpio_write(dev_priv, pipe, DPIO_TX_OCALINIT(port), 0x00000000);
2087 vlv_dpio_write(dev_priv, pipe, DPIO_TX_SWING_CTL4(port), demph_reg_value);
2088 vlv_dpio_write(dev_priv, pipe, DPIO_TX_SWING_CTL2(port),
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002089 uniqtranscale_reg_value);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08002090 vlv_dpio_write(dev_priv, pipe, DPIO_TX_SWING_CTL3(port), 0x0C782040);
2091 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_STAGGER0(port), 0x00030000);
2092 vlv_dpio_write(dev_priv, pipe, DPIO_PCS_CTL_OVER1(port), preemph_reg_value);
2093 vlv_dpio_write(dev_priv, pipe, DPIO_TX_OCALINIT(port), 0x80000000);
Chris Wilson0980a602013-07-26 19:57:35 +01002094 mutex_unlock(&dev_priv->dpio_lock);
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002095
2096 return 0;
2097}
2098
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002099static void
Keith Packard93f62da2011-11-01 19:45:03 -07002100intel_get_adjust_train(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002101{
2102 uint8_t v = 0;
2103 uint8_t p = 0;
2104 int lane;
Keith Packard1a2eb462011-11-16 16:26:07 -08002105 uint8_t voltage_max;
2106 uint8_t preemph_max;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002107
Jesse Barnes33a34e42010-09-08 12:42:02 -07002108 for (lane = 0; lane < intel_dp->lane_count; lane++) {
Daniel Vetter0f037bd2012-10-18 10:15:27 +02002109 uint8_t this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
2110 uint8_t this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002111
2112 if (this_v > v)
2113 v = this_v;
2114 if (this_p > p)
2115 p = this_p;
2116 }
2117
Keith Packard1a2eb462011-11-16 16:26:07 -08002118 voltage_max = intel_dp_voltage_max(intel_dp);
Keith Packard417e8222011-11-01 19:54:11 -07002119 if (v >= voltage_max)
2120 v = voltage_max | DP_TRAIN_MAX_SWING_REACHED;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002121
Keith Packard1a2eb462011-11-16 16:26:07 -08002122 preemph_max = intel_dp_pre_emphasis_max(intel_dp, v);
2123 if (p >= preemph_max)
2124 p = preemph_max | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002125
2126 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07002127 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002128}
2129
2130static uint32_t
Paulo Zanonif0a34242012-12-06 16:51:50 -02002131intel_gen4_signal_levels(uint8_t train_set)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002132{
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002133 uint32_t signal_levels = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002134
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002135 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002136 case DP_TRAIN_VOLTAGE_SWING_400:
2137 default:
2138 signal_levels |= DP_VOLTAGE_0_4;
2139 break;
2140 case DP_TRAIN_VOLTAGE_SWING_600:
2141 signal_levels |= DP_VOLTAGE_0_6;
2142 break;
2143 case DP_TRAIN_VOLTAGE_SWING_800:
2144 signal_levels |= DP_VOLTAGE_0_8;
2145 break;
2146 case DP_TRAIN_VOLTAGE_SWING_1200:
2147 signal_levels |= DP_VOLTAGE_1_2;
2148 break;
2149 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002150 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002151 case DP_TRAIN_PRE_EMPHASIS_0:
2152 default:
2153 signal_levels |= DP_PRE_EMPHASIS_0;
2154 break;
2155 case DP_TRAIN_PRE_EMPHASIS_3_5:
2156 signal_levels |= DP_PRE_EMPHASIS_3_5;
2157 break;
2158 case DP_TRAIN_PRE_EMPHASIS_6:
2159 signal_levels |= DP_PRE_EMPHASIS_6;
2160 break;
2161 case DP_TRAIN_PRE_EMPHASIS_9_5:
2162 signal_levels |= DP_PRE_EMPHASIS_9_5;
2163 break;
2164 }
2165 return signal_levels;
2166}
2167
Zhenyu Wange3421a12010-04-08 09:43:27 +08002168/* Gen6's DP voltage swing and pre-emphasis control */
2169static uint32_t
2170intel_gen6_edp_signal_levels(uint8_t train_set)
2171{
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08002172 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
2173 DP_TRAIN_PRE_EMPHASIS_MASK);
2174 switch (signal_levels) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08002175 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08002176 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
2177 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
2178 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
2179 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002180 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08002181 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
2182 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002183 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08002184 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
2185 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002186 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08002187 case DP_TRAIN_VOLTAGE_SWING_1200 | DP_TRAIN_PRE_EMPHASIS_0:
2188 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002189 default:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08002190 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
2191 "0x%x\n", signal_levels);
2192 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08002193 }
2194}
2195
Keith Packard1a2eb462011-11-16 16:26:07 -08002196/* Gen7's DP voltage swing and pre-emphasis control */
2197static uint32_t
2198intel_gen7_edp_signal_levels(uint8_t train_set)
2199{
2200 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
2201 DP_TRAIN_PRE_EMPHASIS_MASK);
2202 switch (signal_levels) {
2203 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
2204 return EDP_LINK_TRAIN_400MV_0DB_IVB;
2205 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
2206 return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
2207 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
2208 return EDP_LINK_TRAIN_400MV_6DB_IVB;
2209
2210 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
2211 return EDP_LINK_TRAIN_600MV_0DB_IVB;
2212 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
2213 return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
2214
2215 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
2216 return EDP_LINK_TRAIN_800MV_0DB_IVB;
2217 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
2218 return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
2219
2220 default:
2221 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
2222 "0x%x\n", signal_levels);
2223 return EDP_LINK_TRAIN_500MV_0DB_IVB;
2224 }
2225}
2226
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002227/* Gen7.5's (HSW) DP voltage swing and pre-emphasis control */
2228static uint32_t
Paulo Zanonif0a34242012-12-06 16:51:50 -02002229intel_hsw_signal_levels(uint8_t train_set)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002230{
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002231 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
2232 DP_TRAIN_PRE_EMPHASIS_MASK);
2233 switch (signal_levels) {
2234 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_0:
2235 return DDI_BUF_EMP_400MV_0DB_HSW;
2236 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_3_5:
2237 return DDI_BUF_EMP_400MV_3_5DB_HSW;
2238 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_6:
2239 return DDI_BUF_EMP_400MV_6DB_HSW;
2240 case DP_TRAIN_VOLTAGE_SWING_400 | DP_TRAIN_PRE_EMPHASIS_9_5:
2241 return DDI_BUF_EMP_400MV_9_5DB_HSW;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002242
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002243 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_0:
2244 return DDI_BUF_EMP_600MV_0DB_HSW;
2245 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_3_5:
2246 return DDI_BUF_EMP_600MV_3_5DB_HSW;
2247 case DP_TRAIN_VOLTAGE_SWING_600 | DP_TRAIN_PRE_EMPHASIS_6:
2248 return DDI_BUF_EMP_600MV_6DB_HSW;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002249
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002250 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_0:
2251 return DDI_BUF_EMP_800MV_0DB_HSW;
2252 case DP_TRAIN_VOLTAGE_SWING_800 | DP_TRAIN_PRE_EMPHASIS_3_5:
2253 return DDI_BUF_EMP_800MV_3_5DB_HSW;
2254 default:
2255 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
2256 "0x%x\n", signal_levels);
2257 return DDI_BUF_EMP_400MV_0DB_HSW;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002258 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002259}
2260
Paulo Zanonif0a34242012-12-06 16:51:50 -02002261/* Properly updates "DP" with the correct signal levels. */
2262static void
2263intel_dp_set_signal_levels(struct intel_dp *intel_dp, uint32_t *DP)
2264{
2265 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03002266 enum port port = intel_dig_port->port;
Paulo Zanonif0a34242012-12-06 16:51:50 -02002267 struct drm_device *dev = intel_dig_port->base.base.dev;
2268 uint32_t signal_levels, mask;
2269 uint8_t train_set = intel_dp->train_set[0];
2270
Paulo Zanoni22b8bf12013-02-18 19:00:23 -03002271 if (HAS_DDI(dev)) {
Paulo Zanonif0a34242012-12-06 16:51:50 -02002272 signal_levels = intel_hsw_signal_levels(train_set);
2273 mask = DDI_BUF_EMP_MASK;
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002274 } else if (IS_VALLEYVIEW(dev)) {
2275 signal_levels = intel_vlv_signal_levels(intel_dp);
2276 mask = 0;
Imre Deakbc7d38a2013-05-16 14:40:36 +03002277 } else if (IS_GEN7(dev) && port == PORT_A) {
Paulo Zanonif0a34242012-12-06 16:51:50 -02002278 signal_levels = intel_gen7_edp_signal_levels(train_set);
2279 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_IVB;
Imre Deakbc7d38a2013-05-16 14:40:36 +03002280 } else if (IS_GEN6(dev) && port == PORT_A) {
Paulo Zanonif0a34242012-12-06 16:51:50 -02002281 signal_levels = intel_gen6_edp_signal_levels(train_set);
2282 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_SNB;
2283 } else {
2284 signal_levels = intel_gen4_signal_levels(train_set);
2285 mask = DP_VOLTAGE_MASK | DP_PRE_EMPHASIS_MASK;
2286 }
2287
2288 DRM_DEBUG_KMS("Using signal levels %08x\n", signal_levels);
2289
2290 *DP = (*DP & ~mask) | signal_levels;
2291}
2292
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002293static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01002294intel_dp_set_link_train(struct intel_dp *intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002295 uint32_t dp_reg_value,
Chris Wilson58e10eb2010-10-03 10:56:11 +01002296 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002297{
Paulo Zanoni174edf12012-10-26 19:05:50 -02002298 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2299 struct drm_device *dev = intel_dig_port->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002300 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni174edf12012-10-26 19:05:50 -02002301 enum port port = intel_dig_port->port;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002302 int ret;
2303
Paulo Zanoni22b8bf12013-02-18 19:00:23 -03002304 if (HAS_DDI(dev)) {
Imre Deak3ab9c632013-05-03 12:57:41 +03002305 uint32_t temp = I915_READ(DP_TP_CTL(port));
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002306
2307 if (dp_train_pat & DP_LINK_SCRAMBLING_DISABLE)
2308 temp |= DP_TP_CTL_SCRAMBLE_DISABLE;
2309 else
2310 temp &= ~DP_TP_CTL_SCRAMBLE_DISABLE;
2311
2312 temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
2313 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2314 case DP_TRAINING_PATTERN_DISABLE:
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002315 temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
2316
2317 break;
2318 case DP_TRAINING_PATTERN_1:
2319 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
2320 break;
2321 case DP_TRAINING_PATTERN_2:
2322 temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
2323 break;
2324 case DP_TRAINING_PATTERN_3:
2325 temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
2326 break;
2327 }
Paulo Zanoni174edf12012-10-26 19:05:50 -02002328 I915_WRITE(DP_TP_CTL(port), temp);
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002329
Imre Deakbc7d38a2013-05-16 14:40:36 +03002330 } else if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || port != PORT_A)) {
Paulo Zanoni47ea7542012-07-17 16:55:16 -03002331 dp_reg_value &= ~DP_LINK_TRAIN_MASK_CPT;
2332
2333 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2334 case DP_TRAINING_PATTERN_DISABLE:
2335 dp_reg_value |= DP_LINK_TRAIN_OFF_CPT;
2336 break;
2337 case DP_TRAINING_PATTERN_1:
2338 dp_reg_value |= DP_LINK_TRAIN_PAT_1_CPT;
2339 break;
2340 case DP_TRAINING_PATTERN_2:
2341 dp_reg_value |= DP_LINK_TRAIN_PAT_2_CPT;
2342 break;
2343 case DP_TRAINING_PATTERN_3:
2344 DRM_ERROR("DP training pattern 3 not supported\n");
2345 dp_reg_value |= DP_LINK_TRAIN_PAT_2_CPT;
2346 break;
2347 }
2348
2349 } else {
2350 dp_reg_value &= ~DP_LINK_TRAIN_MASK;
2351
2352 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2353 case DP_TRAINING_PATTERN_DISABLE:
2354 dp_reg_value |= DP_LINK_TRAIN_OFF;
2355 break;
2356 case DP_TRAINING_PATTERN_1:
2357 dp_reg_value |= DP_LINK_TRAIN_PAT_1;
2358 break;
2359 case DP_TRAINING_PATTERN_2:
2360 dp_reg_value |= DP_LINK_TRAIN_PAT_2;
2361 break;
2362 case DP_TRAINING_PATTERN_3:
2363 DRM_ERROR("DP training pattern 3 not supported\n");
2364 dp_reg_value |= DP_LINK_TRAIN_PAT_2;
2365 break;
2366 }
2367 }
2368
Chris Wilsonea5b2132010-08-04 13:50:23 +01002369 I915_WRITE(intel_dp->output_reg, dp_reg_value);
2370 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002371
Chris Wilsonea5b2132010-08-04 13:50:23 +01002372 intel_dp_aux_native_write_1(intel_dp,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002373 DP_TRAINING_PATTERN_SET,
2374 dp_train_pat);
2375
Paulo Zanoni47ea7542012-07-17 16:55:16 -03002376 if ((dp_train_pat & DP_TRAINING_PATTERN_MASK) !=
2377 DP_TRAINING_PATTERN_DISABLE) {
2378 ret = intel_dp_aux_native_write(intel_dp,
2379 DP_TRAINING_LANE0_SET,
2380 intel_dp->train_set,
2381 intel_dp->lane_count);
2382 if (ret != intel_dp->lane_count)
2383 return false;
2384 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002385
2386 return true;
2387}
2388
Imre Deak3ab9c632013-05-03 12:57:41 +03002389static void intel_dp_set_idle_link_train(struct intel_dp *intel_dp)
2390{
2391 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2392 struct drm_device *dev = intel_dig_port->base.base.dev;
2393 struct drm_i915_private *dev_priv = dev->dev_private;
2394 enum port port = intel_dig_port->port;
2395 uint32_t val;
2396
2397 if (!HAS_DDI(dev))
2398 return;
2399
2400 val = I915_READ(DP_TP_CTL(port));
2401 val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
2402 val |= DP_TP_CTL_LINK_TRAIN_IDLE;
2403 I915_WRITE(DP_TP_CTL(port), val);
2404
2405 /*
2406 * On PORT_A we can have only eDP in SST mode. There the only reason
2407 * we need to set idle transmission mode is to work around a HW issue
2408 * where we enable the pipe while not in idle link-training mode.
2409 * In this case there is requirement to wait for a minimum number of
2410 * idle patterns to be sent.
2411 */
2412 if (port == PORT_A)
2413 return;
2414
2415 if (wait_for((I915_READ(DP_TP_STATUS(port)) & DP_TP_STATUS_IDLE_DONE),
2416 1))
2417 DRM_ERROR("Timed out waiting for DP idle patterns\n");
2418}
2419
Jesse Barnes33a34e42010-09-08 12:42:02 -07002420/* Enable corresponding port and start training pattern 1 */
Paulo Zanonic19b0662012-10-15 15:51:41 -03002421void
Jesse Barnes33a34e42010-09-08 12:42:02 -07002422intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002423{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002424 struct drm_encoder *encoder = &dp_to_dig_port(intel_dp)->base.base;
Paulo Zanonic19b0662012-10-15 15:51:41 -03002425 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002426 int i;
2427 uint8_t voltage;
Keith Packardcdb0e952011-11-01 20:00:06 -07002428 int voltage_tries, loop_tries;
Chris Wilsonea5b2132010-08-04 13:50:23 +01002429 uint32_t DP = intel_dp->DP;
Jani Nikula6aba5b62013-10-04 15:08:10 +03002430 uint8_t link_config[2];
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002431
Paulo Zanoniaffa9352012-11-23 15:30:39 -02002432 if (HAS_DDI(dev))
Paulo Zanonic19b0662012-10-15 15:51:41 -03002433 intel_ddi_prepare_link_retrain(encoder);
2434
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002435 /* Write the link configuration data */
Jani Nikula6aba5b62013-10-04 15:08:10 +03002436 link_config[0] = intel_dp->link_bw;
2437 link_config[1] = intel_dp->lane_count;
2438 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
2439 link_config[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
2440 intel_dp_aux_native_write(intel_dp, DP_LINK_BW_SET, link_config, 2);
2441
2442 link_config[0] = 0;
2443 link_config[1] = DP_SET_ANSI_8B10B;
2444 intel_dp_aux_native_write(intel_dp, DP_DOWNSPREAD_CTRL, link_config, 2);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002445
2446 DP |= DP_PORT_EN;
Keith Packard1a2eb462011-11-16 16:26:07 -08002447
Jesse Barnes33a34e42010-09-08 12:42:02 -07002448 memset(intel_dp->train_set, 0, 4);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002449 voltage = 0xff;
Keith Packardcdb0e952011-11-01 20:00:06 -07002450 voltage_tries = 0;
2451 loop_tries = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002452 for (;;) {
Jesse Barnes33a34e42010-09-08 12:42:02 -07002453 /* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
Keith Packard93f62da2011-11-01 19:45:03 -07002454 uint8_t link_status[DP_LINK_STATUS_SIZE];
Keith Packard417e8222011-11-01 19:54:11 -07002455
Paulo Zanonif0a34242012-12-06 16:51:50 -02002456 intel_dp_set_signal_levels(intel_dp, &DP);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002457
Daniel Vettera7c96552012-10-18 10:15:30 +02002458 /* Set training pattern 1 */
Paulo Zanoni47ea7542012-07-17 16:55:16 -03002459 if (!intel_dp_set_link_train(intel_dp, DP,
Adam Jackson81055852011-07-21 17:48:37 -04002460 DP_TRAINING_PATTERN_1 |
2461 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002462 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002463
Daniel Vettera7c96552012-10-18 10:15:30 +02002464 drm_dp_link_train_clock_recovery_delay(intel_dp->dpcd);
Keith Packard93f62da2011-11-01 19:45:03 -07002465 if (!intel_dp_get_link_status(intel_dp, link_status)) {
2466 DRM_ERROR("failed to get link status\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002467 break;
Keith Packard93f62da2011-11-01 19:45:03 -07002468 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002469
Daniel Vetter01916272012-10-18 10:15:25 +02002470 if (drm_dp_clock_recovery_ok(link_status, intel_dp->lane_count)) {
Keith Packard93f62da2011-11-01 19:45:03 -07002471 DRM_DEBUG_KMS("clock recovery OK\n");
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002472 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002473 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002474
2475 /* Check to see if we've tried the max voltage */
2476 for (i = 0; i < intel_dp->lane_count; i++)
2477 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
2478 break;
Takashi Iwai3b4f8192013-03-11 18:40:16 +01002479 if (i == intel_dp->lane_count) {
Daniel Vetterb06fbda2012-10-16 09:50:25 +02002480 ++loop_tries;
2481 if (loop_tries == 5) {
Keith Packardcdb0e952011-11-01 20:00:06 -07002482 DRM_DEBUG_KMS("too many full retries, give up\n");
2483 break;
2484 }
2485 memset(intel_dp->train_set, 0, 4);
2486 voltage_tries = 0;
2487 continue;
2488 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002489
2490 /* Check to see if we've tried the same voltage 5 times */
Daniel Vetterb06fbda2012-10-16 09:50:25 +02002491 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
Chris Wilson24773672012-09-26 16:48:30 +01002492 ++voltage_tries;
Daniel Vetterb06fbda2012-10-16 09:50:25 +02002493 if (voltage_tries == 5) {
2494 DRM_DEBUG_KMS("too many voltage retries, give up\n");
2495 break;
2496 }
2497 } else
2498 voltage_tries = 0;
2499 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002500
2501 /* Compute new intel_dp->train_set as requested by target */
Keith Packard93f62da2011-11-01 19:45:03 -07002502 intel_get_adjust_train(intel_dp, link_status);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002503 }
2504
Jesse Barnes33a34e42010-09-08 12:42:02 -07002505 intel_dp->DP = DP;
2506}
2507
Paulo Zanonic19b0662012-10-15 15:51:41 -03002508void
Jesse Barnes33a34e42010-09-08 12:42:02 -07002509intel_dp_complete_link_train(struct intel_dp *intel_dp)
2510{
Jesse Barnes33a34e42010-09-08 12:42:02 -07002511 bool channel_eq = false;
Jesse Barnes37f80972011-01-05 14:45:24 -08002512 int tries, cr_tries;
Jesse Barnes33a34e42010-09-08 12:42:02 -07002513 uint32_t DP = intel_dp->DP;
2514
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002515 /* channel equalization */
2516 tries = 0;
Jesse Barnes37f80972011-01-05 14:45:24 -08002517 cr_tries = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002518 channel_eq = false;
2519 for (;;) {
Keith Packard93f62da2011-11-01 19:45:03 -07002520 uint8_t link_status[DP_LINK_STATUS_SIZE];
Zhenyu Wange3421a12010-04-08 09:43:27 +08002521
Jesse Barnes37f80972011-01-05 14:45:24 -08002522 if (cr_tries > 5) {
2523 DRM_ERROR("failed to train DP, aborting\n");
2524 intel_dp_link_down(intel_dp);
2525 break;
2526 }
2527
Paulo Zanonif0a34242012-12-06 16:51:50 -02002528 intel_dp_set_signal_levels(intel_dp, &DP);
Zhenyu Wange3421a12010-04-08 09:43:27 +08002529
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002530 /* channel eq pattern */
Paulo Zanoni47ea7542012-07-17 16:55:16 -03002531 if (!intel_dp_set_link_train(intel_dp, DP,
Adam Jackson81055852011-07-21 17:48:37 -04002532 DP_TRAINING_PATTERN_2 |
2533 DP_LINK_SCRAMBLING_DISABLE))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002534 break;
2535
Daniel Vettera7c96552012-10-18 10:15:30 +02002536 drm_dp_link_train_channel_eq_delay(intel_dp->dpcd);
Keith Packard93f62da2011-11-01 19:45:03 -07002537 if (!intel_dp_get_link_status(intel_dp, link_status))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002538 break;
Jesse Barnes869184a2010-10-07 16:01:22 -07002539
Jesse Barnes37f80972011-01-05 14:45:24 -08002540 /* Make sure clock is still ok */
Daniel Vetter01916272012-10-18 10:15:25 +02002541 if (!drm_dp_clock_recovery_ok(link_status, intel_dp->lane_count)) {
Jesse Barnes37f80972011-01-05 14:45:24 -08002542 intel_dp_start_link_train(intel_dp);
2543 cr_tries++;
2544 continue;
2545 }
2546
Daniel Vetter1ffdff12012-10-18 10:15:24 +02002547 if (drm_dp_channel_eq_ok(link_status, intel_dp->lane_count)) {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002548 channel_eq = true;
2549 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002550 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002551
Jesse Barnes37f80972011-01-05 14:45:24 -08002552 /* Try 5 times, then try clock recovery if that fails */
2553 if (tries > 5) {
2554 intel_dp_link_down(intel_dp);
2555 intel_dp_start_link_train(intel_dp);
2556 tries = 0;
2557 cr_tries++;
2558 continue;
2559 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002560
2561 /* Compute new intel_dp->train_set as requested by target */
Keith Packard93f62da2011-11-01 19:45:03 -07002562 intel_get_adjust_train(intel_dp, link_status);
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002563 ++tries;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002564 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00002565
Imre Deak3ab9c632013-05-03 12:57:41 +03002566 intel_dp_set_idle_link_train(intel_dp);
2567
2568 intel_dp->DP = DP;
2569
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002570 if (channel_eq)
Masanari Iida07f42252013-03-20 11:00:34 +09002571 DRM_DEBUG_KMS("Channel EQ done. DP Training successful\n");
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002572
Imre Deak3ab9c632013-05-03 12:57:41 +03002573}
2574
2575void intel_dp_stop_link_train(struct intel_dp *intel_dp)
2576{
2577 intel_dp_set_link_train(intel_dp, intel_dp->DP,
2578 DP_TRAINING_PATTERN_DISABLE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002579}
2580
2581static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01002582intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002583{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002584 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03002585 enum port port = intel_dig_port->port;
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002586 struct drm_device *dev = intel_dig_port->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002587 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterab527ef2012-11-29 15:59:33 +01002588 struct intel_crtc *intel_crtc =
2589 to_intel_crtc(intel_dig_port->base.base.crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +01002590 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002591
Paulo Zanonic19b0662012-10-15 15:51:41 -03002592 /*
2593 * DDI code has a strict mode set sequence and we should try to respect
2594 * it, otherwise we might hang the machine in many different ways. So we
2595 * really should be disabling the port only on a complete crtc_disable
2596 * sequence. This function is just called under two conditions on DDI
2597 * code:
2598 * - Link train failed while doing crtc_enable, and on this case we
2599 * really should respect the mode set sequence and wait for a
2600 * crtc_disable.
2601 * - Someone turned the monitor off and intel_dp_check_link_status
2602 * called us. We don't need to disable the whole port on this case, so
2603 * when someone turns the monitor on again,
2604 * intel_ddi_prepare_link_retrain will take care of redoing the link
2605 * train.
2606 */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02002607 if (HAS_DDI(dev))
Paulo Zanonic19b0662012-10-15 15:51:41 -03002608 return;
2609
Daniel Vetter0c33d8d2012-09-06 22:15:43 +02002610 if (WARN_ON((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0))
Chris Wilson1b39d6f2010-12-06 11:20:45 +00002611 return;
2612
Zhao Yakui28c97732009-10-09 11:39:41 +08002613 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002614
Imre Deakbc7d38a2013-05-16 14:40:36 +03002615 if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || port != PORT_A)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08002616 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01002617 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08002618 } else {
2619 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01002620 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08002621 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01002622 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002623
Daniel Vetterab527ef2012-11-29 15:59:33 +01002624 /* We don't really know why we're doing this */
2625 intel_wait_for_vblank(dev, intel_crtc->pipe);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002626
Daniel Vetter493a7082012-05-30 12:31:56 +02002627 if (HAS_PCH_IBX(dev) &&
Chris Wilson1b39d6f2010-12-06 11:20:45 +00002628 I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002629 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
Chris Wilson31acbcc2011-04-17 06:38:35 +01002630
Eric Anholt5bddd172010-11-18 09:32:59 +08002631 /* Hardware workaround: leaving our transcoder select
2632 * set to transcoder B while it's off will prevent the
2633 * corresponding HDMI output on transcoder A.
2634 *
2635 * Combine this with another hardware workaround:
2636 * transcoder select bit can only be cleared while the
2637 * port is enabled.
2638 */
2639 DP &= ~DP_PIPEB_SELECT;
2640 I915_WRITE(intel_dp->output_reg, DP);
2641
2642 /* Changes to enable or select take place the vblank
2643 * after being written.
2644 */
Daniel Vetterff50afe2012-11-29 15:59:34 +01002645 if (WARN_ON(crtc == NULL)) {
2646 /* We should never try to disable a port without a crtc
2647 * attached. For paranoia keep the code around for a
2648 * bit. */
Chris Wilson31acbcc2011-04-17 06:38:35 +01002649 POSTING_READ(intel_dp->output_reg);
2650 msleep(50);
2651 } else
Daniel Vetterab527ef2012-11-29 15:59:33 +01002652 intel_wait_for_vblank(dev, intel_crtc->pipe);
Eric Anholt5bddd172010-11-18 09:32:59 +08002653 }
2654
Wu Fengguang832afda2011-12-09 20:42:21 +08002655 DP &= ~DP_AUDIO_OUTPUT_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01002656 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
2657 POSTING_READ(intel_dp->output_reg);
Keith Packardf01eca22011-09-28 16:48:10 -07002658 msleep(intel_dp->panel_power_down_delay);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002659}
2660
Keith Packard26d61aa2011-07-25 20:01:09 -07002661static bool
2662intel_dp_get_dpcd(struct intel_dp *intel_dp)
Keith Packard92fd8fd2011-07-25 19:50:10 -07002663{
Damien Lespiau577c7a52012-12-13 16:09:02 +00002664 char dpcd_hex_dump[sizeof(intel_dp->dpcd) * 3];
2665
Keith Packard92fd8fd2011-07-25 19:50:10 -07002666 if (intel_dp_aux_native_read_retry(intel_dp, 0x000, intel_dp->dpcd,
Adam Jacksonedb39242012-09-18 10:58:49 -04002667 sizeof(intel_dp->dpcd)) == 0)
2668 return false; /* aux transfer failed */
Keith Packard92fd8fd2011-07-25 19:50:10 -07002669
Damien Lespiau577c7a52012-12-13 16:09:02 +00002670 hex_dump_to_buffer(intel_dp->dpcd, sizeof(intel_dp->dpcd),
2671 32, 1, dpcd_hex_dump, sizeof(dpcd_hex_dump), false);
2672 DRM_DEBUG_KMS("DPCD: %s\n", dpcd_hex_dump);
2673
Adam Jacksonedb39242012-09-18 10:58:49 -04002674 if (intel_dp->dpcd[DP_DPCD_REV] == 0)
2675 return false; /* DPCD not present */
2676
Shobhit Kumar2293bb52013-07-11 18:44:56 -03002677 /* Check if the panel supports PSR */
2678 memset(intel_dp->psr_dpcd, 0, sizeof(intel_dp->psr_dpcd));
2679 intel_dp_aux_native_read_retry(intel_dp, DP_PSR_SUPPORT,
2680 intel_dp->psr_dpcd,
2681 sizeof(intel_dp->psr_dpcd));
2682 if (is_edp_psr(intel_dp))
2683 DRM_DEBUG_KMS("Detected EDP PSR Panel.\n");
Adam Jacksonedb39242012-09-18 10:58:49 -04002684 if (!(intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
2685 DP_DWN_STRM_PORT_PRESENT))
2686 return true; /* native DP sink */
2687
2688 if (intel_dp->dpcd[DP_DPCD_REV] == 0x10)
2689 return true; /* no per-port downstream info */
2690
2691 if (intel_dp_aux_native_read_retry(intel_dp, DP_DOWNSTREAM_PORT_0,
2692 intel_dp->downstream_ports,
2693 DP_MAX_DOWNSTREAM_PORTS) == 0)
2694 return false; /* downstream port status fetch failed */
2695
2696 return true;
Keith Packard92fd8fd2011-07-25 19:50:10 -07002697}
2698
Adam Jackson0d198322012-05-14 16:05:47 -04002699static void
2700intel_dp_probe_oui(struct intel_dp *intel_dp)
2701{
2702 u8 buf[3];
2703
2704 if (!(intel_dp->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
2705 return;
2706
Daniel Vetter351cfc32012-06-12 13:20:47 +02002707 ironlake_edp_panel_vdd_on(intel_dp);
2708
Adam Jackson0d198322012-05-14 16:05:47 -04002709 if (intel_dp_aux_native_read_retry(intel_dp, DP_SINK_OUI, buf, 3))
2710 DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
2711 buf[0], buf[1], buf[2]);
2712
2713 if (intel_dp_aux_native_read_retry(intel_dp, DP_BRANCH_OUI, buf, 3))
2714 DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
2715 buf[0], buf[1], buf[2]);
Daniel Vetter351cfc32012-06-12 13:20:47 +02002716
2717 ironlake_edp_panel_vdd_off(intel_dp, false);
Adam Jackson0d198322012-05-14 16:05:47 -04002718}
2719
Jesse Barnesa60f0e32011-10-20 15:09:17 -07002720static bool
2721intel_dp_get_sink_irq(struct intel_dp *intel_dp, u8 *sink_irq_vector)
2722{
2723 int ret;
2724
2725 ret = intel_dp_aux_native_read_retry(intel_dp,
2726 DP_DEVICE_SERVICE_IRQ_VECTOR,
2727 sink_irq_vector, 1);
2728 if (!ret)
2729 return false;
2730
2731 return true;
2732}
2733
2734static void
2735intel_dp_handle_test_request(struct intel_dp *intel_dp)
2736{
2737 /* NAK by default */
Daniel Vetter9324cf72012-10-20 21:13:05 +02002738 intel_dp_aux_native_write_1(intel_dp, DP_TEST_RESPONSE, DP_TEST_NAK);
Jesse Barnesa60f0e32011-10-20 15:09:17 -07002739}
2740
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002741/*
2742 * According to DP spec
2743 * 5.1.2:
2744 * 1. Read DPCD
2745 * 2. Configure link according to Receiver Capabilities
2746 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
2747 * 4. Check link status on receipt of hot-plug interrupt
2748 */
2749
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002750void
Chris Wilsonea5b2132010-08-04 13:50:23 +01002751intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002752{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002753 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
Jesse Barnesa60f0e32011-10-20 15:09:17 -07002754 u8 sink_irq_vector;
Keith Packard93f62da2011-11-01 19:45:03 -07002755 u8 link_status[DP_LINK_STATUS_SIZE];
Jesse Barnesa60f0e32011-10-20 15:09:17 -07002756
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002757 if (!intel_encoder->connectors_active)
Keith Packardd2b996a2011-07-25 22:37:51 -07002758 return;
Jesse Barnes59cd09e2011-07-07 11:10:59 -07002759
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002760 if (WARN_ON(!intel_encoder->base.crtc))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002761 return;
2762
Keith Packard92fd8fd2011-07-25 19:50:10 -07002763 /* Try to read receiver status if the link appears to be up */
Keith Packard93f62da2011-11-01 19:45:03 -07002764 if (!intel_dp_get_link_status(intel_dp, link_status)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002765 intel_dp_link_down(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002766 return;
2767 }
2768
Keith Packard92fd8fd2011-07-25 19:50:10 -07002769 /* Now read the DPCD to see if it's actually running */
Keith Packard26d61aa2011-07-25 20:01:09 -07002770 if (!intel_dp_get_dpcd(intel_dp)) {
Jesse Barnes59cd09e2011-07-07 11:10:59 -07002771 intel_dp_link_down(intel_dp);
2772 return;
2773 }
2774
Jesse Barnesa60f0e32011-10-20 15:09:17 -07002775 /* Try to read the source of the interrupt */
2776 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
2777 intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
2778 /* Clear interrupt source */
2779 intel_dp_aux_native_write_1(intel_dp,
2780 DP_DEVICE_SERVICE_IRQ_VECTOR,
2781 sink_irq_vector);
2782
2783 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
2784 intel_dp_handle_test_request(intel_dp);
2785 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
2786 DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
2787 }
2788
Daniel Vetter1ffdff12012-10-18 10:15:24 +02002789 if (!drm_dp_channel_eq_ok(link_status, intel_dp->lane_count)) {
Keith Packard92fd8fd2011-07-25 19:50:10 -07002790 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002791 drm_get_encoder_name(&intel_encoder->base));
Jesse Barnes33a34e42010-09-08 12:42:02 -07002792 intel_dp_start_link_train(intel_dp);
2793 intel_dp_complete_link_train(intel_dp);
Imre Deak3ab9c632013-05-03 12:57:41 +03002794 intel_dp_stop_link_train(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -07002795 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002796}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002797
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002798/* XXX this is probably wrong for multiple downstream ports */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002799static enum drm_connector_status
Keith Packard26d61aa2011-07-25 20:01:09 -07002800intel_dp_detect_dpcd(struct intel_dp *intel_dp)
Adam Jackson71ba90002011-07-12 17:38:04 -04002801{
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002802 uint8_t *dpcd = intel_dp->dpcd;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002803 uint8_t type;
2804
2805 if (!intel_dp_get_dpcd(intel_dp))
2806 return connector_status_disconnected;
2807
2808 /* if there's no downstream port, we're done */
2809 if (!(dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT))
Keith Packard26d61aa2011-07-25 20:01:09 -07002810 return connector_status_connected;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002811
2812 /* If we're HPD-aware, SINK_COUNT changes dynamically */
Jani Nikulac9ff1602013-09-27 14:48:42 +03002813 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
2814 intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
Adam Jackson23235172012-09-20 16:42:45 -04002815 uint8_t reg;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002816 if (!intel_dp_aux_native_read_retry(intel_dp, DP_SINK_COUNT,
Adam Jackson23235172012-09-20 16:42:45 -04002817 &reg, 1))
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002818 return connector_status_unknown;
Adam Jackson23235172012-09-20 16:42:45 -04002819 return DP_GET_SINK_COUNT(reg) ? connector_status_connected
2820 : connector_status_disconnected;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002821 }
2822
2823 /* If no HPD, poke DDC gently */
2824 if (drm_probe_ddc(&intel_dp->adapter))
2825 return connector_status_connected;
2826
2827 /* Well we tried, say unknown for unreliable port types */
Jani Nikulac9ff1602013-09-27 14:48:42 +03002828 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
2829 type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
2830 if (type == DP_DS_PORT_TYPE_VGA ||
2831 type == DP_DS_PORT_TYPE_NON_EDID)
2832 return connector_status_unknown;
2833 } else {
2834 type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
2835 DP_DWN_STRM_PORT_TYPE_MASK;
2836 if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
2837 type == DP_DWN_STRM_PORT_TYPE_OTHER)
2838 return connector_status_unknown;
2839 }
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04002840
2841 /* Anything else is out of spec, warn and ignore */
2842 DRM_DEBUG_KMS("Broken DP branch device, ignoring\n");
Keith Packard26d61aa2011-07-25 20:01:09 -07002843 return connector_status_disconnected;
Adam Jackson71ba90002011-07-12 17:38:04 -04002844}
2845
2846static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002847ironlake_dp_detect(struct intel_dp *intel_dp)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002848{
Paulo Zanoni30add222012-10-26 19:05:45 -02002849 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Damien Lespiau1b469632012-12-13 16:09:01 +00002850 struct drm_i915_private *dev_priv = dev->dev_private;
2851 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002852 enum drm_connector_status status;
2853
Chris Wilsonfe16d942011-02-12 10:29:38 +00002854 /* Can't disconnect eDP, but you can close the lid... */
2855 if (is_edp(intel_dp)) {
Paulo Zanoni30add222012-10-26 19:05:45 -02002856 status = intel_panel_detect(dev);
Chris Wilsonfe16d942011-02-12 10:29:38 +00002857 if (status == connector_status_unknown)
2858 status = connector_status_connected;
2859 return status;
2860 }
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07002861
Damien Lespiau1b469632012-12-13 16:09:01 +00002862 if (!ibx_digital_port_connected(dev_priv, intel_dig_port))
2863 return connector_status_disconnected;
2864
Keith Packard26d61aa2011-07-25 20:01:09 -07002865 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002866}
2867
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002868static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002869g4x_dp_detect(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002870{
Paulo Zanoni30add222012-10-26 19:05:45 -02002871 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002872 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä34f2be42013-01-24 15:29:27 +02002873 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Chris Wilson10f76a32012-05-11 18:01:32 +01002874 uint32_t bit;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08002875
Jesse Barnes35aad752013-03-01 13:14:31 -08002876 /* Can't disconnect eDP, but you can close the lid... */
2877 if (is_edp(intel_dp)) {
2878 enum drm_connector_status status;
2879
2880 status = intel_panel_detect(dev);
2881 if (status == connector_status_unknown)
2882 status = connector_status_connected;
2883 return status;
2884 }
2885
Ville Syrjälä34f2be42013-01-24 15:29:27 +02002886 switch (intel_dig_port->port) {
2887 case PORT_B:
Daniel Vetter26739f12013-02-07 12:42:32 +01002888 bit = PORTB_HOTPLUG_LIVE_STATUS;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002889 break;
Ville Syrjälä34f2be42013-01-24 15:29:27 +02002890 case PORT_C:
Daniel Vetter26739f12013-02-07 12:42:32 +01002891 bit = PORTC_HOTPLUG_LIVE_STATUS;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002892 break;
Ville Syrjälä34f2be42013-01-24 15:29:27 +02002893 case PORT_D:
Daniel Vetter26739f12013-02-07 12:42:32 +01002894 bit = PORTD_HOTPLUG_LIVE_STATUS;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002895 break;
2896 default:
2897 return connector_status_unknown;
2898 }
2899
Chris Wilson10f76a32012-05-11 18:01:32 +01002900 if ((I915_READ(PORT_HOTPLUG_STAT) & bit) == 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002901 return connector_status_disconnected;
2902
Keith Packard26d61aa2011-07-25 20:01:09 -07002903 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002904}
2905
Keith Packard8c241fe2011-09-28 16:38:44 -07002906static struct edid *
2907intel_dp_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
2908{
Jani Nikula9cd300e2012-10-19 14:51:52 +03002909 struct intel_connector *intel_connector = to_intel_connector(connector);
Keith Packard8c241fe2011-09-28 16:38:44 -07002910
Jani Nikula9cd300e2012-10-19 14:51:52 +03002911 /* use cached edid if we have one */
2912 if (intel_connector->edid) {
Jani Nikula9cd300e2012-10-19 14:51:52 +03002913 /* invalid edid */
2914 if (IS_ERR(intel_connector->edid))
Jesse Barnesd6f24d02012-06-14 15:28:33 -04002915 return NULL;
2916
Jani Nikula55e9ede2013-10-01 10:38:54 +03002917 return drm_edid_duplicate(intel_connector->edid);
Jesse Barnesd6f24d02012-06-14 15:28:33 -04002918 }
2919
Jani Nikula9cd300e2012-10-19 14:51:52 +03002920 return drm_get_edid(connector, adapter);
Keith Packard8c241fe2011-09-28 16:38:44 -07002921}
2922
2923static int
2924intel_dp_get_edid_modes(struct drm_connector *connector, struct i2c_adapter *adapter)
2925{
Jani Nikula9cd300e2012-10-19 14:51:52 +03002926 struct intel_connector *intel_connector = to_intel_connector(connector);
Keith Packard8c241fe2011-09-28 16:38:44 -07002927
Jani Nikula9cd300e2012-10-19 14:51:52 +03002928 /* use cached edid if we have one */
2929 if (intel_connector->edid) {
2930 /* invalid edid */
2931 if (IS_ERR(intel_connector->edid))
2932 return 0;
2933
2934 return intel_connector_update_modes(connector,
2935 intel_connector->edid);
Jesse Barnesd6f24d02012-06-14 15:28:33 -04002936 }
2937
Jani Nikula9cd300e2012-10-19 14:51:52 +03002938 return intel_ddc_get_modes(connector, adapter);
Keith Packard8c241fe2011-09-28 16:38:44 -07002939}
2940
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002941static enum drm_connector_status
2942intel_dp_detect(struct drm_connector *connector, bool force)
2943{
2944 struct intel_dp *intel_dp = intel_attached_dp(connector);
Paulo Zanonid63885d2012-10-26 19:05:49 -02002945 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2946 struct intel_encoder *intel_encoder = &intel_dig_port->base;
Paulo Zanonifa90ece2012-10-26 19:05:44 -02002947 struct drm_device *dev = connector->dev;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002948 enum drm_connector_status status;
2949 struct edid *edid = NULL;
2950
Chris Wilson164c8592013-07-20 20:27:08 +01002951 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2952 connector->base.id, drm_get_connector_name(connector));
2953
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002954 intel_dp->has_audio = false;
2955
2956 if (HAS_PCH_SPLIT(dev))
2957 status = ironlake_dp_detect(intel_dp);
2958 else
2959 status = g4x_dp_detect(intel_dp);
Adam Jackson1b9be9d2011-07-12 17:38:01 -04002960
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002961 if (status != connector_status_connected)
2962 return status;
2963
Adam Jackson0d198322012-05-14 16:05:47 -04002964 intel_dp_probe_oui(intel_dp);
2965
Daniel Vetterc3e5f672012-02-23 17:14:47 +01002966 if (intel_dp->force_audio != HDMI_AUDIO_AUTO) {
2967 intel_dp->has_audio = (intel_dp->force_audio == HDMI_AUDIO_ON);
Chris Wilsonf6849602010-09-19 09:29:33 +01002968 } else {
Keith Packard8c241fe2011-09-28 16:38:44 -07002969 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
Chris Wilsonf6849602010-09-19 09:29:33 +01002970 if (edid) {
2971 intel_dp->has_audio = drm_detect_monitor_audio(edid);
Chris Wilsonf6849602010-09-19 09:29:33 +01002972 kfree(edid);
2973 }
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002974 }
2975
Paulo Zanonid63885d2012-10-26 19:05:49 -02002976 if (intel_encoder->type != INTEL_OUTPUT_EDP)
2977 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08002978 return connector_status_connected;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002979}
2980
2981static int intel_dp_get_modes(struct drm_connector *connector)
2982{
Chris Wilsondf0e9242010-09-09 16:20:55 +01002983 struct intel_dp *intel_dp = intel_attached_dp(connector);
Jani Nikuladd06f902012-10-19 14:51:50 +03002984 struct intel_connector *intel_connector = to_intel_connector(connector);
Paulo Zanonifa90ece2012-10-26 19:05:44 -02002985 struct drm_device *dev = connector->dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002986 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002987
2988 /* We should parse the EDID data and find out if it has an audio sink
2989 */
2990
Keith Packard8c241fe2011-09-28 16:38:44 -07002991 ret = intel_dp_get_edid_modes(connector, &intel_dp->adapter);
Jani Nikulaf8779fd2012-10-19 14:51:48 +03002992 if (ret)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002993 return ret;
2994
Jani Nikulaf8779fd2012-10-19 14:51:48 +03002995 /* if eDP has no EDID, fall back to fixed mode */
Jani Nikuladd06f902012-10-19 14:51:50 +03002996 if (is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
Jani Nikulaf8779fd2012-10-19 14:51:48 +03002997 struct drm_display_mode *mode;
Jani Nikuladd06f902012-10-19 14:51:50 +03002998 mode = drm_mode_duplicate(dev,
2999 intel_connector->panel.fixed_mode);
Jani Nikulaf8779fd2012-10-19 14:51:48 +03003000 if (mode) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003001 drm_mode_probed_add(connector, mode);
3002 return 1;
3003 }
3004 }
3005 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003006}
3007
Chris Wilson1aad7ac2011-02-09 18:46:58 +00003008static bool
3009intel_dp_detect_audio(struct drm_connector *connector)
3010{
3011 struct intel_dp *intel_dp = intel_attached_dp(connector);
3012 struct edid *edid;
3013 bool has_audio = false;
3014
Keith Packard8c241fe2011-09-28 16:38:44 -07003015 edid = intel_dp_get_edid(connector, &intel_dp->adapter);
Chris Wilson1aad7ac2011-02-09 18:46:58 +00003016 if (edid) {
3017 has_audio = drm_detect_monitor_audio(edid);
Chris Wilson1aad7ac2011-02-09 18:46:58 +00003018 kfree(edid);
3019 }
3020
3021 return has_audio;
3022}
3023
Chris Wilsonf6849602010-09-19 09:29:33 +01003024static int
3025intel_dp_set_property(struct drm_connector *connector,
3026 struct drm_property *property,
3027 uint64_t val)
3028{
Chris Wilsone953fd72011-02-21 22:23:52 +00003029 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Yuly Novikov53b41832012-10-26 12:04:00 +03003030 struct intel_connector *intel_connector = to_intel_connector(connector);
Paulo Zanonida63a9f2012-10-26 19:05:46 -02003031 struct intel_encoder *intel_encoder = intel_attached_encoder(connector);
3032 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
Chris Wilsonf6849602010-09-19 09:29:33 +01003033 int ret;
3034
Rob Clark662595d2012-10-11 20:36:04 -05003035 ret = drm_object_property_set_value(&connector->base, property, val);
Chris Wilsonf6849602010-09-19 09:29:33 +01003036 if (ret)
3037 return ret;
3038
Chris Wilson3f43c482011-05-12 22:17:24 +01003039 if (property == dev_priv->force_audio_property) {
Chris Wilson1aad7ac2011-02-09 18:46:58 +00003040 int i = val;
3041 bool has_audio;
3042
3043 if (i == intel_dp->force_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01003044 return 0;
3045
Chris Wilson1aad7ac2011-02-09 18:46:58 +00003046 intel_dp->force_audio = i;
Chris Wilsonf6849602010-09-19 09:29:33 +01003047
Daniel Vetterc3e5f672012-02-23 17:14:47 +01003048 if (i == HDMI_AUDIO_AUTO)
Chris Wilson1aad7ac2011-02-09 18:46:58 +00003049 has_audio = intel_dp_detect_audio(connector);
3050 else
Daniel Vetterc3e5f672012-02-23 17:14:47 +01003051 has_audio = (i == HDMI_AUDIO_ON);
Chris Wilson1aad7ac2011-02-09 18:46:58 +00003052
3053 if (has_audio == intel_dp->has_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01003054 return 0;
3055
Chris Wilson1aad7ac2011-02-09 18:46:58 +00003056 intel_dp->has_audio = has_audio;
Chris Wilsonf6849602010-09-19 09:29:33 +01003057 goto done;
3058 }
3059
Chris Wilsone953fd72011-02-21 22:23:52 +00003060 if (property == dev_priv->broadcast_rgb_property) {
Daniel Vetterae4edb82013-04-22 17:07:23 +02003061 bool old_auto = intel_dp->color_range_auto;
3062 uint32_t old_range = intel_dp->color_range;
3063
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02003064 switch (val) {
3065 case INTEL_BROADCAST_RGB_AUTO:
3066 intel_dp->color_range_auto = true;
3067 break;
3068 case INTEL_BROADCAST_RGB_FULL:
3069 intel_dp->color_range_auto = false;
3070 intel_dp->color_range = 0;
3071 break;
3072 case INTEL_BROADCAST_RGB_LIMITED:
3073 intel_dp->color_range_auto = false;
3074 intel_dp->color_range = DP_COLOR_RANGE_16_235;
3075 break;
3076 default:
3077 return -EINVAL;
3078 }
Daniel Vetterae4edb82013-04-22 17:07:23 +02003079
3080 if (old_auto == intel_dp->color_range_auto &&
3081 old_range == intel_dp->color_range)
3082 return 0;
3083
Chris Wilsone953fd72011-02-21 22:23:52 +00003084 goto done;
3085 }
3086
Yuly Novikov53b41832012-10-26 12:04:00 +03003087 if (is_edp(intel_dp) &&
3088 property == connector->dev->mode_config.scaling_mode_property) {
3089 if (val == DRM_MODE_SCALE_NONE) {
3090 DRM_DEBUG_KMS("no scaling not supported\n");
3091 return -EINVAL;
3092 }
3093
3094 if (intel_connector->panel.fitting_mode == val) {
3095 /* the eDP scaling property is not changed */
3096 return 0;
3097 }
3098 intel_connector->panel.fitting_mode = val;
3099
3100 goto done;
3101 }
3102
Chris Wilsonf6849602010-09-19 09:29:33 +01003103 return -EINVAL;
3104
3105done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00003106 if (intel_encoder->base.crtc)
3107 intel_crtc_restore_mode(intel_encoder->base.crtc);
Chris Wilsonf6849602010-09-19 09:29:33 +01003108
3109 return 0;
3110}
3111
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003112static void
Paulo Zanoni73845ad2013-06-12 17:27:30 -03003113intel_dp_connector_destroy(struct drm_connector *connector)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003114{
Jani Nikula1d508702012-10-19 14:51:49 +03003115 struct intel_connector *intel_connector = to_intel_connector(connector);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02003116
Jani Nikula9cd300e2012-10-19 14:51:52 +03003117 if (!IS_ERR_OR_NULL(intel_connector->edid))
3118 kfree(intel_connector->edid);
3119
Paulo Zanoniacd8db102013-06-12 17:27:23 -03003120 /* Can't call is_edp() since the encoder may have been destroyed
3121 * already. */
3122 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
Jani Nikula1d508702012-10-19 14:51:49 +03003123 intel_panel_fini(&intel_connector->panel);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02003124
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003125 drm_sysfs_connector_remove(connector);
3126 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08003127 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003128}
3129
Paulo Zanoni00c09d72012-10-26 19:05:52 -02003130void intel_dp_encoder_destroy(struct drm_encoder *encoder)
Daniel Vetter24d05922010-08-20 18:08:28 +02003131{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02003132 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
3133 struct intel_dp *intel_dp = &intel_dig_port->dp;
Daniel Vetterbd173812013-03-25 11:24:10 +01003134 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Daniel Vetter24d05922010-08-20 18:08:28 +02003135
3136 i2c_del_adapter(&intel_dp->adapter);
3137 drm_encoder_cleanup(encoder);
Keith Packardbd943152011-09-18 23:09:52 -07003138 if (is_edp(intel_dp)) {
3139 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
Daniel Vetterbd173812013-03-25 11:24:10 +01003140 mutex_lock(&dev->mode_config.mutex);
Keith Packardbd943152011-09-18 23:09:52 -07003141 ironlake_panel_vdd_off_sync(intel_dp);
Daniel Vetterbd173812013-03-25 11:24:10 +01003142 mutex_unlock(&dev->mode_config.mutex);
Keith Packardbd943152011-09-18 23:09:52 -07003143 }
Paulo Zanonida63a9f2012-10-26 19:05:46 -02003144 kfree(intel_dig_port);
Daniel Vetter24d05922010-08-20 18:08:28 +02003145}
3146
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003147static const struct drm_connector_funcs intel_dp_connector_funcs = {
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02003148 .dpms = intel_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003149 .detect = intel_dp_detect,
3150 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilsonf6849602010-09-19 09:29:33 +01003151 .set_property = intel_dp_set_property,
Paulo Zanoni73845ad2013-06-12 17:27:30 -03003152 .destroy = intel_dp_connector_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003153};
3154
3155static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
3156 .get_modes = intel_dp_get_modes,
3157 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01003158 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003159};
3160
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003161static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Daniel Vetter24d05922010-08-20 18:08:28 +02003162 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003163};
3164
Chris Wilson995b6762010-08-20 13:23:26 +01003165static void
Eric Anholt21d40d32010-03-25 11:11:14 -07003166intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07003167{
Paulo Zanonifa90ece2012-10-26 19:05:44 -02003168 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
Keith Packardc8110e52009-05-06 11:51:10 -07003169
Jesse Barnes885a5012011-07-07 11:11:01 -07003170 intel_dp_check_link_status(intel_dp);
Keith Packardc8110e52009-05-06 11:51:10 -07003171}
3172
Zhenyu Wange3421a12010-04-08 09:43:27 +08003173/* Return which DP Port should be selected for Transcoder DP control */
3174int
Akshay Joshi0206e352011-08-16 15:34:10 -04003175intel_trans_dp_port_sel(struct drm_crtc *crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08003176{
3177 struct drm_device *dev = crtc->dev;
Paulo Zanonifa90ece2012-10-26 19:05:44 -02003178 struct intel_encoder *intel_encoder;
3179 struct intel_dp *intel_dp;
Zhenyu Wange3421a12010-04-08 09:43:27 +08003180
Paulo Zanonifa90ece2012-10-26 19:05:44 -02003181 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
3182 intel_dp = enc_to_intel_dp(&intel_encoder->base);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003183
Paulo Zanonifa90ece2012-10-26 19:05:44 -02003184 if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
3185 intel_encoder->type == INTEL_OUTPUT_EDP)
Chris Wilsonea5b2132010-08-04 13:50:23 +01003186 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08003187 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01003188
Zhenyu Wange3421a12010-04-08 09:43:27 +08003189 return -1;
3190}
3191
Zhao Yakui36e83a12010-06-12 14:32:21 +08003192/* check the VBT to see whether the eDP is on DP-D port */
Adam Jacksoncb0953d2010-07-16 14:46:29 -04003193bool intel_dpd_is_edp(struct drm_device *dev)
Zhao Yakui36e83a12010-06-12 14:32:21 +08003194{
3195 struct drm_i915_private *dev_priv = dev->dev_private;
3196 struct child_device_config *p_child;
3197 int i;
3198
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03003199 if (!dev_priv->vbt.child_dev_num)
Zhao Yakui36e83a12010-06-12 14:32:21 +08003200 return false;
3201
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03003202 for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
3203 p_child = dev_priv->vbt.child_dev + i;
Zhao Yakui36e83a12010-06-12 14:32:21 +08003204
3205 if (p_child->dvo_port == PORT_IDPD &&
3206 p_child->device_type == DEVICE_TYPE_eDP)
3207 return true;
3208 }
3209 return false;
3210}
3211
Chris Wilsonf6849602010-09-19 09:29:33 +01003212static void
3213intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
3214{
Yuly Novikov53b41832012-10-26 12:04:00 +03003215 struct intel_connector *intel_connector = to_intel_connector(connector);
3216
Chris Wilson3f43c482011-05-12 22:17:24 +01003217 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +00003218 intel_attach_broadcast_rgb_property(connector);
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02003219 intel_dp->color_range_auto = true;
Yuly Novikov53b41832012-10-26 12:04:00 +03003220
3221 if (is_edp(intel_dp)) {
3222 drm_mode_create_scaling_mode_property(connector->dev);
Rob Clark6de6d842012-10-11 20:36:04 -05003223 drm_object_attach_property(
3224 &connector->base,
Yuly Novikov53b41832012-10-26 12:04:00 +03003225 connector->dev->mode_config.scaling_mode_property,
Yuly Novikov8e740cd2012-10-26 12:04:01 +03003226 DRM_MODE_SCALE_ASPECT);
3227 intel_connector->panel.fitting_mode = DRM_MODE_SCALE_ASPECT;
Yuly Novikov53b41832012-10-26 12:04:00 +03003228 }
Chris Wilsonf6849602010-09-19 09:29:33 +01003229}
3230
Daniel Vetter67a54562012-10-20 20:57:45 +02003231static void
3232intel_dp_init_panel_power_sequencer(struct drm_device *dev,
Jani Nikulaf30d26e2013-01-16 10:53:40 +02003233 struct intel_dp *intel_dp,
3234 struct edp_power_seq *out)
Daniel Vetter67a54562012-10-20 20:57:45 +02003235{
3236 struct drm_i915_private *dev_priv = dev->dev_private;
3237 struct edp_power_seq cur, vbt, spec, final;
3238 u32 pp_on, pp_off, pp_div, pp;
Jani Nikulabf13e812013-09-06 07:40:05 +03003239 int pp_ctrl_reg, pp_on_reg, pp_off_reg, pp_div_reg;
Jesse Barnes453c5422013-03-28 09:55:41 -07003240
3241 if (HAS_PCH_SPLIT(dev)) {
Jani Nikulabf13e812013-09-06 07:40:05 +03003242 pp_ctrl_reg = PCH_PP_CONTROL;
Jesse Barnes453c5422013-03-28 09:55:41 -07003243 pp_on_reg = PCH_PP_ON_DELAYS;
3244 pp_off_reg = PCH_PP_OFF_DELAYS;
3245 pp_div_reg = PCH_PP_DIVISOR;
3246 } else {
Jani Nikulabf13e812013-09-06 07:40:05 +03003247 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
3248
3249 pp_ctrl_reg = VLV_PIPE_PP_CONTROL(pipe);
3250 pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
3251 pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
3252 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
Jesse Barnes453c5422013-03-28 09:55:41 -07003253 }
Daniel Vetter67a54562012-10-20 20:57:45 +02003254
3255 /* Workaround: Need to write PP_CONTROL with the unlock key as
3256 * the very first thing. */
Jesse Barnes453c5422013-03-28 09:55:41 -07003257 pp = ironlake_get_pp_control(intel_dp);
Jani Nikulabf13e812013-09-06 07:40:05 +03003258 I915_WRITE(pp_ctrl_reg, pp);
Daniel Vetter67a54562012-10-20 20:57:45 +02003259
Jesse Barnes453c5422013-03-28 09:55:41 -07003260 pp_on = I915_READ(pp_on_reg);
3261 pp_off = I915_READ(pp_off_reg);
3262 pp_div = I915_READ(pp_div_reg);
Daniel Vetter67a54562012-10-20 20:57:45 +02003263
3264 /* Pull timing values out of registers */
3265 cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
3266 PANEL_POWER_UP_DELAY_SHIFT;
3267
3268 cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
3269 PANEL_LIGHT_ON_DELAY_SHIFT;
3270
3271 cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
3272 PANEL_LIGHT_OFF_DELAY_SHIFT;
3273
3274 cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
3275 PANEL_POWER_DOWN_DELAY_SHIFT;
3276
3277 cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
3278 PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
3279
3280 DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
3281 cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
3282
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03003283 vbt = dev_priv->vbt.edp_pps;
Daniel Vetter67a54562012-10-20 20:57:45 +02003284
3285 /* Upper limits from eDP 1.3 spec. Note that we use the clunky units of
3286 * our hw here, which are all in 100usec. */
3287 spec.t1_t3 = 210 * 10;
3288 spec.t8 = 50 * 10; /* no limit for t8, use t7 instead */
3289 spec.t9 = 50 * 10; /* no limit for t9, make it symmetric with t8 */
3290 spec.t10 = 500 * 10;
3291 /* This one is special and actually in units of 100ms, but zero
3292 * based in the hw (so we need to add 100 ms). But the sw vbt
3293 * table multiplies it with 1000 to make it in units of 100usec,
3294 * too. */
3295 spec.t11_t12 = (510 + 100) * 10;
3296
3297 DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
3298 vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
3299
3300 /* Use the max of the register settings and vbt. If both are
3301 * unset, fall back to the spec limits. */
3302#define assign_final(field) final.field = (max(cur.field, vbt.field) == 0 ? \
3303 spec.field : \
3304 max(cur.field, vbt.field))
3305 assign_final(t1_t3);
3306 assign_final(t8);
3307 assign_final(t9);
3308 assign_final(t10);
3309 assign_final(t11_t12);
3310#undef assign_final
3311
3312#define get_delay(field) (DIV_ROUND_UP(final.field, 10))
3313 intel_dp->panel_power_up_delay = get_delay(t1_t3);
3314 intel_dp->backlight_on_delay = get_delay(t8);
3315 intel_dp->backlight_off_delay = get_delay(t9);
3316 intel_dp->panel_power_down_delay = get_delay(t10);
3317 intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
3318#undef get_delay
3319
Jani Nikulaf30d26e2013-01-16 10:53:40 +02003320 DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
3321 intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
3322 intel_dp->panel_power_cycle_delay);
3323
3324 DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
3325 intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
3326
3327 if (out)
3328 *out = final;
3329}
3330
3331static void
3332intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
3333 struct intel_dp *intel_dp,
3334 struct edp_power_seq *seq)
3335{
3336 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes453c5422013-03-28 09:55:41 -07003337 u32 pp_on, pp_off, pp_div, port_sel = 0;
3338 int div = HAS_PCH_SPLIT(dev) ? intel_pch_rawclk(dev) : intel_hrawclk(dev);
3339 int pp_on_reg, pp_off_reg, pp_div_reg;
3340
3341 if (HAS_PCH_SPLIT(dev)) {
3342 pp_on_reg = PCH_PP_ON_DELAYS;
3343 pp_off_reg = PCH_PP_OFF_DELAYS;
3344 pp_div_reg = PCH_PP_DIVISOR;
3345 } else {
Jani Nikulabf13e812013-09-06 07:40:05 +03003346 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
3347
3348 pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
3349 pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
3350 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
Jesse Barnes453c5422013-03-28 09:55:41 -07003351 }
3352
Daniel Vetter67a54562012-10-20 20:57:45 +02003353 /* And finally store the new values in the power sequencer. */
Jani Nikulaf30d26e2013-01-16 10:53:40 +02003354 pp_on = (seq->t1_t3 << PANEL_POWER_UP_DELAY_SHIFT) |
3355 (seq->t8 << PANEL_LIGHT_ON_DELAY_SHIFT);
3356 pp_off = (seq->t9 << PANEL_LIGHT_OFF_DELAY_SHIFT) |
3357 (seq->t10 << PANEL_POWER_DOWN_DELAY_SHIFT);
Daniel Vetter67a54562012-10-20 20:57:45 +02003358 /* Compute the divisor for the pp clock, simply match the Bspec
3359 * formula. */
Jesse Barnes453c5422013-03-28 09:55:41 -07003360 pp_div = ((100 * div)/2 - 1) << PP_REFERENCE_DIVIDER_SHIFT;
Jani Nikulaf30d26e2013-01-16 10:53:40 +02003361 pp_div |= (DIV_ROUND_UP(seq->t11_t12, 1000)
Daniel Vetter67a54562012-10-20 20:57:45 +02003362 << PANEL_POWER_CYCLE_DELAY_SHIFT);
3363
3364 /* Haswell doesn't have any port selection bits for the panel
3365 * power sequencer any more. */
Imre Deakbc7d38a2013-05-16 14:40:36 +03003366 if (IS_VALLEYVIEW(dev)) {
Jani Nikulabf13e812013-09-06 07:40:05 +03003367 if (dp_to_dig_port(intel_dp)->port == PORT_B)
3368 port_sel = PANEL_PORT_SELECT_DPB_VLV;
3369 else
3370 port_sel = PANEL_PORT_SELECT_DPC_VLV;
Imre Deakbc7d38a2013-05-16 14:40:36 +03003371 } else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
3372 if (dp_to_dig_port(intel_dp)->port == PORT_A)
Jani Nikulaa24c1442013-09-05 16:44:46 +03003373 port_sel = PANEL_PORT_SELECT_DPA;
Daniel Vetter67a54562012-10-20 20:57:45 +02003374 else
Jani Nikulaa24c1442013-09-05 16:44:46 +03003375 port_sel = PANEL_PORT_SELECT_DPD;
Daniel Vetter67a54562012-10-20 20:57:45 +02003376 }
3377
Jesse Barnes453c5422013-03-28 09:55:41 -07003378 pp_on |= port_sel;
3379
3380 I915_WRITE(pp_on_reg, pp_on);
3381 I915_WRITE(pp_off_reg, pp_off);
3382 I915_WRITE(pp_div_reg, pp_div);
Daniel Vetter67a54562012-10-20 20:57:45 +02003383
Daniel Vetter67a54562012-10-20 20:57:45 +02003384 DRM_DEBUG_KMS("panel power sequencer register settings: PP_ON %#x, PP_OFF %#x, PP_DIV %#x\n",
Jesse Barnes453c5422013-03-28 09:55:41 -07003385 I915_READ(pp_on_reg),
3386 I915_READ(pp_off_reg),
3387 I915_READ(pp_div_reg));
Keith Packardc8110e52009-05-06 11:51:10 -07003388}
3389
Paulo Zanonied92f0b2013-06-12 17:27:24 -03003390static bool intel_edp_init_connector(struct intel_dp *intel_dp,
3391 struct intel_connector *intel_connector)
3392{
3393 struct drm_connector *connector = &intel_connector->base;
3394 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3395 struct drm_device *dev = intel_dig_port->base.base.dev;
3396 struct drm_i915_private *dev_priv = dev->dev_private;
3397 struct drm_display_mode *fixed_mode = NULL;
3398 struct edp_power_seq power_seq = { 0 };
3399 bool has_dpcd;
3400 struct drm_display_mode *scan;
3401 struct edid *edid;
3402
3403 if (!is_edp(intel_dp))
3404 return true;
3405
3406 intel_dp_init_panel_power_sequencer(dev, intel_dp, &power_seq);
3407
3408 /* Cache DPCD and EDID for edp. */
3409 ironlake_edp_panel_vdd_on(intel_dp);
3410 has_dpcd = intel_dp_get_dpcd(intel_dp);
3411 ironlake_edp_panel_vdd_off(intel_dp, false);
3412
3413 if (has_dpcd) {
3414 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
3415 dev_priv->no_aux_handshake =
3416 intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
3417 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
3418 } else {
3419 /* if this fails, presume the device is a ghost */
3420 DRM_INFO("failed to retrieve link info, disabling eDP\n");
Paulo Zanonied92f0b2013-06-12 17:27:24 -03003421 return false;
3422 }
3423
3424 /* We now know it's not a ghost, init power sequence regs. */
3425 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp,
3426 &power_seq);
3427
3428 ironlake_edp_panel_vdd_on(intel_dp);
3429 edid = drm_get_edid(connector, &intel_dp->adapter);
3430 if (edid) {
3431 if (drm_add_edid_modes(connector, edid)) {
3432 drm_mode_connector_update_edid_property(connector,
3433 edid);
3434 drm_edid_to_eld(connector, edid);
3435 } else {
3436 kfree(edid);
3437 edid = ERR_PTR(-EINVAL);
3438 }
3439 } else {
3440 edid = ERR_PTR(-ENOENT);
3441 }
3442 intel_connector->edid = edid;
3443
3444 /* prefer fixed mode from EDID if available */
3445 list_for_each_entry(scan, &connector->probed_modes, head) {
3446 if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
3447 fixed_mode = drm_mode_duplicate(dev, scan);
3448 break;
3449 }
3450 }
3451
3452 /* fallback to VBT if available for eDP */
3453 if (!fixed_mode && dev_priv->vbt.lfp_lvds_vbt_mode) {
3454 fixed_mode = drm_mode_duplicate(dev,
3455 dev_priv->vbt.lfp_lvds_vbt_mode);
3456 if (fixed_mode)
3457 fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
3458 }
3459
3460 ironlake_edp_panel_vdd_off(intel_dp, false);
3461
3462 intel_panel_init(&intel_connector->panel, fixed_mode);
3463 intel_panel_setup_backlight(connector);
3464
3465 return true;
3466}
3467
Paulo Zanoni16c25532013-06-12 17:27:25 -03003468bool
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02003469intel_dp_init_connector(struct intel_digital_port *intel_dig_port,
3470 struct intel_connector *intel_connector)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003471{
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02003472 struct drm_connector *connector = &intel_connector->base;
3473 struct intel_dp *intel_dp = &intel_dig_port->dp;
3474 struct intel_encoder *intel_encoder = &intel_dig_port->base;
3475 struct drm_device *dev = intel_encoder->base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003476 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni174edf12012-10-26 19:05:50 -02003477 enum port port = intel_dig_port->port;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08003478 const char *name = NULL;
Paulo Zanonib2a14752013-06-12 17:27:28 -03003479 int type, error;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003480
Daniel Vetter07679352012-09-06 22:15:42 +02003481 /* Preserve the current hw state. */
3482 intel_dp->DP = I915_READ(intel_dp->output_reg);
Jani Nikuladd06f902012-10-19 14:51:50 +03003483 intel_dp->attached_connector = intel_connector;
Chris Wilson3d3dc142011-02-12 10:33:12 +00003484
Imre Deakf7d24902013-05-08 13:14:05 +03003485 type = DRM_MODE_CONNECTOR_DisplayPort;
Gajanan Bhat19c03922012-09-27 19:13:07 +05303486 /*
3487 * FIXME : We need to initialize built-in panels before external panels.
3488 * For X0, DP_C is fixed as eDP. Revisit this as part of VLV eDP cleanup
3489 */
Imre Deakf7d24902013-05-08 13:14:05 +03003490 switch (port) {
3491 case PORT_A:
Gajanan Bhat19c03922012-09-27 19:13:07 +05303492 type = DRM_MODE_CONNECTOR_eDP;
Imre Deakf7d24902013-05-08 13:14:05 +03003493 break;
3494 case PORT_C:
3495 if (IS_VALLEYVIEW(dev))
3496 type = DRM_MODE_CONNECTOR_eDP;
3497 break;
3498 case PORT_D:
3499 if (HAS_PCH_SPLIT(dev) && intel_dpd_is_edp(dev))
3500 type = DRM_MODE_CONNECTOR_eDP;
3501 break;
3502 default: /* silence GCC warning */
3503 break;
Adam Jacksonb3295302010-07-16 14:46:28 -04003504 }
3505
Imre Deakf7d24902013-05-08 13:14:05 +03003506 /*
3507 * For eDP we always set the encoder type to INTEL_OUTPUT_EDP, but
3508 * for DP the encoder type can be set by the caller to
3509 * INTEL_OUTPUT_UNKNOWN for DDI, so don't rewrite it.
3510 */
3511 if (type == DRM_MODE_CONNECTOR_eDP)
3512 intel_encoder->type = INTEL_OUTPUT_EDP;
3513
Imre Deake7281ea2013-05-08 13:14:08 +03003514 DRM_DEBUG_KMS("Adding %s connector on port %c\n",
3515 type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
3516 port_name(port));
3517
Adam Jacksonb3295302010-07-16 14:46:28 -04003518 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003519 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
3520
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02003521 connector->interlace_allowed = true;
3522 connector->doublescan_allowed = 0;
Ma Lingf8aed702009-08-24 13:50:24 +08003523
Daniel Vetter66a92782012-07-12 20:08:18 +02003524 INIT_DELAYED_WORK(&intel_dp->panel_vdd_work,
3525 ironlake_panel_vdd_work);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08003526
Chris Wilsondf0e9242010-09-09 16:20:55 +01003527 intel_connector_attach_encoder(intel_connector, intel_encoder);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003528 drm_sysfs_connector_add(connector);
3529
Paulo Zanoniaffa9352012-11-23 15:30:39 -02003530 if (HAS_DDI(dev))
Paulo Zanonibcbc8892012-10-26 19:05:51 -02003531 intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
3532 else
3533 intel_connector->get_hw_state = intel_connector_get_hw_state;
3534
Paulo Zanoni9ed35ab2013-02-18 19:00:25 -03003535 intel_dp->aux_ch_ctl_reg = intel_dp->output_reg + 0x10;
3536 if (HAS_DDI(dev)) {
3537 switch (intel_dig_port->port) {
3538 case PORT_A:
3539 intel_dp->aux_ch_ctl_reg = DPA_AUX_CH_CTL;
3540 break;
3541 case PORT_B:
3542 intel_dp->aux_ch_ctl_reg = PCH_DPB_AUX_CH_CTL;
3543 break;
3544 case PORT_C:
3545 intel_dp->aux_ch_ctl_reg = PCH_DPC_AUX_CH_CTL;
3546 break;
3547 case PORT_D:
3548 intel_dp->aux_ch_ctl_reg = PCH_DPD_AUX_CH_CTL;
3549 break;
3550 default:
3551 BUG();
3552 }
3553 }
Daniel Vettere8cb4552012-07-01 13:05:48 +02003554
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003555 /* Set up the DDC bus. */
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03003556 switch (port) {
3557 case PORT_A:
Egbert Eich1d843f92013-02-25 12:06:49 -05003558 intel_encoder->hpd_pin = HPD_PORT_A;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03003559 name = "DPDDC-A";
3560 break;
3561 case PORT_B:
Egbert Eich1d843f92013-02-25 12:06:49 -05003562 intel_encoder->hpd_pin = HPD_PORT_B;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03003563 name = "DPDDC-B";
3564 break;
3565 case PORT_C:
Egbert Eich1d843f92013-02-25 12:06:49 -05003566 intel_encoder->hpd_pin = HPD_PORT_C;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03003567 name = "DPDDC-C";
3568 break;
3569 case PORT_D:
Egbert Eich1d843f92013-02-25 12:06:49 -05003570 intel_encoder->hpd_pin = HPD_PORT_D;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03003571 name = "DPDDC-D";
3572 break;
3573 default:
Damien Lespiauad1c0b12013-03-07 15:30:28 +00003574 BUG();
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08003575 }
3576
Paulo Zanonib2a14752013-06-12 17:27:28 -03003577 error = intel_dp_i2c_init(intel_dp, intel_connector, name);
3578 WARN(error, "intel_dp_i2c_init failed with error %d for port %c\n",
3579 error, port_name(port));
Dave Airliec1f05262012-08-30 11:06:18 +10003580
Rodrigo Vivi2b28bb12013-07-11 18:44:58 -03003581 intel_dp->psr_setup_done = false;
3582
Paulo Zanonib2f246a2013-06-12 17:27:26 -03003583 if (!intel_edp_init_connector(intel_dp, intel_connector)) {
Paulo Zanoni15b1d172013-06-12 17:27:27 -03003584 i2c_del_adapter(&intel_dp->adapter);
3585 if (is_edp(intel_dp)) {
3586 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
3587 mutex_lock(&dev->mode_config.mutex);
3588 ironlake_panel_vdd_off_sync(intel_dp);
3589 mutex_unlock(&dev->mode_config.mutex);
3590 }
Paulo Zanonib2f246a2013-06-12 17:27:26 -03003591 drm_sysfs_connector_remove(connector);
3592 drm_connector_cleanup(connector);
Paulo Zanoni16c25532013-06-12 17:27:25 -03003593 return false;
Paulo Zanonib2f246a2013-06-12 17:27:26 -03003594 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003595
Chris Wilsonf6849602010-09-19 09:29:33 +01003596 intel_dp_add_properties(intel_dp, connector);
3597
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003598 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
3599 * 0xd. Failure to do so will result in spurious interrupts being
3600 * generated on the port when a cable is not attached.
3601 */
3602 if (IS_G4X(dev) && !IS_GM45(dev)) {
3603 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
3604 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
3605 }
Paulo Zanoni16c25532013-06-12 17:27:25 -03003606
3607 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003608}
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02003609
3610void
3611intel_dp_init(struct drm_device *dev, int output_reg, enum port port)
3612{
3613 struct intel_digital_port *intel_dig_port;
3614 struct intel_encoder *intel_encoder;
3615 struct drm_encoder *encoder;
3616 struct intel_connector *intel_connector;
3617
3618 intel_dig_port = kzalloc(sizeof(struct intel_digital_port), GFP_KERNEL);
3619 if (!intel_dig_port)
3620 return;
3621
3622 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
3623 if (!intel_connector) {
3624 kfree(intel_dig_port);
3625 return;
3626 }
3627
3628 intel_encoder = &intel_dig_port->base;
3629 encoder = &intel_encoder->base;
3630
3631 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
3632 DRM_MODE_ENCODER_TMDS);
3633
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003634 intel_encoder->compute_config = intel_dp_compute_config;
Daniel Vetterb934223d2013-07-21 21:37:05 +02003635 intel_encoder->mode_set = intel_dp_mode_set;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02003636 intel_encoder->disable = intel_disable_dp;
3637 intel_encoder->post_disable = intel_post_disable_dp;
3638 intel_encoder->get_hw_state = intel_dp_get_hw_state;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07003639 intel_encoder->get_config = intel_dp_get_config;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03003640 if (IS_VALLEYVIEW(dev)) {
Jani Nikulaecff4f32013-09-06 07:38:29 +03003641 intel_encoder->pre_pll_enable = vlv_dp_pre_pll_enable;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03003642 intel_encoder->pre_enable = vlv_pre_enable_dp;
3643 intel_encoder->enable = vlv_enable_dp;
3644 } else {
Jani Nikulaecff4f32013-09-06 07:38:29 +03003645 intel_encoder->pre_enable = g4x_pre_enable_dp;
3646 intel_encoder->enable = g4x_enable_dp;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03003647 }
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02003648
Paulo Zanoni174edf12012-10-26 19:05:50 -02003649 intel_dig_port->port = port;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02003650 intel_dig_port->dp.output_reg = output_reg;
3651
Paulo Zanoni00c09d72012-10-26 19:05:52 -02003652 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02003653 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
3654 intel_encoder->cloneable = false;
3655 intel_encoder->hot_plug = intel_dp_hot_plug;
3656
Paulo Zanoni15b1d172013-06-12 17:27:27 -03003657 if (!intel_dp_init_connector(intel_dig_port, intel_connector)) {
3658 drm_encoder_cleanup(encoder);
3659 kfree(intel_dig_port);
Paulo Zanonib2f246a2013-06-12 17:27:26 -03003660 kfree(intel_connector);
Paulo Zanoni15b1d172013-06-12 17:27:27 -03003661 }
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02003662}