blob: a9b984734b1ae684cdd2bda30f00076d7b94ef57 [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>
Clint Taylor01527b32014-07-07 13:01:46 -070031#include <linux/notifier.h>
32#include <linux/reboot.h>
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/drmP.h>
Matt Roperc6f95f22015-01-22 16:50:32 -080034#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drm_crtc.h>
36#include <drm/drm_crtc_helper.h>
37#include <drm/drm_edid.h>
Keith Packarda4fc5ed2009-04-07 16:16:42 -070038#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010039#include <drm/i915_drm.h>
Keith Packarda4fc5ed2009-04-07 16:16:42 -070040#include "i915_drv.h"
Keith Packarda4fc5ed2009-04-07 16:16:42 -070041
Keith Packarda4fc5ed2009-04-07 16:16:42 -070042#define DP_LINK_CHECK_TIMEOUT (10 * 1000)
43
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +080044struct dp_link_dpll {
45 int link_bw;
46 struct dpll dpll;
47};
48
49static const struct dp_link_dpll gen4_dpll[] = {
50 { DP_LINK_BW_1_62,
51 { .p1 = 2, .p2 = 10, .n = 2, .m1 = 23, .m2 = 8 } },
52 { DP_LINK_BW_2_7,
53 { .p1 = 1, .p2 = 10, .n = 1, .m1 = 14, .m2 = 2 } }
54};
55
56static const struct dp_link_dpll pch_dpll[] = {
57 { DP_LINK_BW_1_62,
58 { .p1 = 2, .p2 = 10, .n = 1, .m1 = 12, .m2 = 9 } },
59 { DP_LINK_BW_2_7,
60 { .p1 = 1, .p2 = 10, .n = 2, .m1 = 14, .m2 = 8 } }
61};
62
Chon Ming Lee65ce4bf2013-09-04 01:30:38 +080063static const struct dp_link_dpll vlv_dpll[] = {
64 { DP_LINK_BW_1_62,
Chon Ming Lee58f6e632013-09-25 15:47:51 +080065 { .p1 = 3, .p2 = 2, .n = 5, .m1 = 3, .m2 = 81 } },
Chon Ming Lee65ce4bf2013-09-04 01:30:38 +080066 { DP_LINK_BW_2_7,
67 { .p1 = 2, .p2 = 2, .n = 1, .m1 = 2, .m2 = 27 } }
68};
69
Chon Ming Leeef9348c2014-04-09 13:28:18 +030070/*
71 * CHV supports eDP 1.4 that have more link rates.
72 * Below only provides the fixed rate but exclude variable rate.
73 */
74static const struct dp_link_dpll chv_dpll[] = {
75 /*
76 * CHV requires to program fractional division for m2.
77 * m2 is stored in fixed point format using formula below
78 * (m2_int << 22) | m2_fraction
79 */
80 { DP_LINK_BW_1_62, /* m2_int = 32, m2_fraction = 1677722 */
81 { .p1 = 4, .p2 = 2, .n = 1, .m1 = 2, .m2 = 0x819999a } },
82 { DP_LINK_BW_2_7, /* m2_int = 27, m2_fraction = 0 */
83 { .p1 = 4, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } },
84 { DP_LINK_BW_5_4, /* m2_int = 27, m2_fraction = 0 */
85 { .p1 = 2, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } }
86};
Sonika Jindala8f3ef62015-03-05 10:02:30 +053087/* Skylake supports following rates */
Ville Syrjäläf4896f12015-03-12 17:10:27 +020088static const int gen9_rates[] = { 162000, 216000, 270000,
89 324000, 432000, 540000 };
90static const int default_rates[] = { 162000, 270000, 540000 };
Chon Ming Leeef9348c2014-04-09 13:28:18 +030091
Jesse Barnescfcb0fc2010-10-07 16:01:06 -070092/**
93 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
94 * @intel_dp: DP struct
95 *
96 * If a CPU or PCH DP output is attached to an eDP panel, this function
97 * will return true, and false otherwise.
98 */
99static bool is_edp(struct intel_dp *intel_dp)
100{
Paulo Zanonida63a9f2012-10-26 19:05:46 -0200101 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
102
103 return intel_dig_port->base.type == INTEL_OUTPUT_EDP;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700104}
105
Imre Deak68b4d822013-05-08 13:14:06 +0300106static struct drm_device *intel_dp_to_dev(struct intel_dp *intel_dp)
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700107{
Imre Deak68b4d822013-05-08 13:14:06 +0300108 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
109
110 return intel_dig_port->base.base.dev;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700111}
112
Chris Wilsondf0e9242010-09-09 16:20:55 +0100113static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
114{
Paulo Zanonifa90ece2012-10-26 19:05:44 -0200115 return enc_to_intel_dp(&intel_attached_encoder(connector)->base);
Chris Wilsondf0e9242010-09-09 16:20:55 +0100116}
117
Chris Wilsonea5b2132010-08-04 13:50:23 +0100118static void intel_dp_link_down(struct intel_dp *intel_dp);
Ville Syrjälä1e0560e2014-08-19 13:24:25 +0300119static bool edp_panel_vdd_on(struct intel_dp *intel_dp);
Daniel Vetter4be73782014-01-17 14:39:48 +0100120static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync);
Ville Syrjälä093e3f12014-10-16 21:27:33 +0300121static void vlv_init_panel_power_sequencer(struct intel_dp *intel_dp);
Ville Syrjäläa8c33442014-10-16 21:29:59 +0300122static void vlv_steal_power_sequencer(struct drm_device *dev,
123 enum pipe pipe);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700124
Dave Airlie0e32b392014-05-02 14:02:48 +1000125int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100126intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700127{
Jesse Barnes7183dc22011-07-07 11:10:58 -0700128 int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
Todd Previte06ea66b2014-01-20 10:19:39 -0700129 struct drm_device *dev = intel_dp->attached_connector->base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700130
131 switch (max_link_bw) {
132 case DP_LINK_BW_1_62:
133 case DP_LINK_BW_2_7:
134 break;
Imre Deakd4eead52013-07-09 17:05:26 +0300135 case DP_LINK_BW_5_4: /* 1.2 capable displays may advertise higher bw */
Damien Lespiau8749be82015-02-11 17:43:24 +0000136 if (IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_B0)
137 /* WaDisableHBR2:skl */
138 max_link_bw = DP_LINK_BW_2_7;
139 else if (((IS_HASWELL(dev) && !IS_HSW_ULX(dev)) ||
Paulo Zanoni9bbfd202014-04-29 11:00:22 -0300140 INTEL_INFO(dev)->gen >= 8) &&
Todd Previte06ea66b2014-01-20 10:19:39 -0700141 intel_dp->dpcd[DP_DPCD_REV] >= 0x12)
142 max_link_bw = DP_LINK_BW_5_4;
143 else
144 max_link_bw = DP_LINK_BW_2_7;
Imre Deakd4eead52013-07-09 17:05:26 +0300145 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700146 default:
Imre Deakd4eead52013-07-09 17:05:26 +0300147 WARN(1, "invalid max DP link bw val %x, using 1.62Gbps\n",
148 max_link_bw);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700149 max_link_bw = DP_LINK_BW_1_62;
150 break;
151 }
152 return max_link_bw;
153}
154
Paulo Zanonieeb63242014-05-06 14:56:50 +0300155static u8 intel_dp_max_lane_count(struct intel_dp *intel_dp)
156{
157 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
158 struct drm_device *dev = intel_dig_port->base.base.dev;
159 u8 source_max, sink_max;
160
161 source_max = 4;
162 if (HAS_DDI(dev) && intel_dig_port->port == PORT_A &&
163 (intel_dig_port->saved_port_bits & DDI_A_4_LANES) == 0)
164 source_max = 2;
165
166 sink_max = drm_dp_max_lane_count(intel_dp->dpcd);
167
168 return min(source_max, sink_max);
169}
170
Adam Jacksoncd9dde42011-10-14 12:43:49 -0400171/*
172 * The units on the numbers in the next two are... bizarre. Examples will
173 * make it clearer; this one parallels an example in the eDP spec.
174 *
175 * intel_dp_max_data_rate for one lane of 2.7GHz evaluates as:
176 *
177 * 270000 * 1 * 8 / 10 == 216000
178 *
179 * The actual data capacity of that configuration is 2.16Gbit/s, so the
180 * units are decakilobits. ->clock in a drm_display_mode is in kilohertz -
181 * or equivalently, kilopixels per second - so for 1680x1050R it'd be
182 * 119000. At 18bpp that's 2142000 kilobits per second.
183 *
184 * Thus the strange-looking division by 10 in intel_dp_link_required, to
185 * get the result in decakilobits instead of kilobits.
186 */
187
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700188static int
Keith Packardc8982612012-01-25 08:16:25 -0800189intel_dp_link_required(int pixel_clock, int bpp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700190{
Adam Jacksoncd9dde42011-10-14 12:43:49 -0400191 return (pixel_clock * bpp + 9) / 10;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700192}
193
194static int
Dave Airliefe27d532010-06-30 11:46:17 +1000195intel_dp_max_data_rate(int max_link_clock, int max_lanes)
196{
197 return (max_link_clock * max_lanes * 8) / 10;
198}
199
Damien Lespiauc19de8e2013-11-28 15:29:18 +0000200static enum drm_mode_status
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700201intel_dp_mode_valid(struct drm_connector *connector,
202 struct drm_display_mode *mode)
203{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100204 struct intel_dp *intel_dp = intel_attached_dp(connector);
Jani Nikuladd06f902012-10-19 14:51:50 +0300205 struct intel_connector *intel_connector = to_intel_connector(connector);
206 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
Daniel Vetter36008362013-03-27 00:44:59 +0100207 int target_clock = mode->clock;
208 int max_rate, mode_rate, max_lanes, max_link_clock;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700209
Jani Nikuladd06f902012-10-19 14:51:50 +0300210 if (is_edp(intel_dp) && fixed_mode) {
211 if (mode->hdisplay > fixed_mode->hdisplay)
Zhao Yakui7de56f42010-07-19 09:43:14 +0100212 return MODE_PANEL;
213
Jani Nikuladd06f902012-10-19 14:51:50 +0300214 if (mode->vdisplay > fixed_mode->vdisplay)
Zhao Yakui7de56f42010-07-19 09:43:14 +0100215 return MODE_PANEL;
Daniel Vetter03afc4a2013-04-02 23:42:31 +0200216
217 target_clock = fixed_mode->clock;
Zhao Yakui7de56f42010-07-19 09:43:14 +0100218 }
219
Daniel Vetter36008362013-03-27 00:44:59 +0100220 max_link_clock = drm_dp_bw_code_to_link_rate(intel_dp_max_link_bw(intel_dp));
Paulo Zanonieeb63242014-05-06 14:56:50 +0300221 max_lanes = intel_dp_max_lane_count(intel_dp);
Daniel Vetter36008362013-03-27 00:44:59 +0100222
223 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
224 mode_rate = intel_dp_link_required(target_clock, 18);
225
226 if (mode_rate > max_rate)
Daniel Vetterc4867932012-04-10 10:42:36 +0200227 return MODE_CLOCK_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700228
229 if (mode->clock < 10000)
230 return MODE_CLOCK_LOW;
231
Daniel Vetter0af78a22012-05-23 11:30:55 +0200232 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
233 return MODE_H_ILLEGAL;
234
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700235 return MODE_OK;
236}
237
Rodrigo Vivia4f12892014-11-14 08:52:27 -0800238uint32_t intel_dp_pack_aux(const uint8_t *src, int src_bytes)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700239{
240 int i;
241 uint32_t v = 0;
242
243 if (src_bytes > 4)
244 src_bytes = 4;
245 for (i = 0; i < src_bytes; i++)
246 v |= ((uint32_t) src[i]) << ((3-i) * 8);
247 return v;
248}
249
Damien Lespiauc2af70e2015-02-10 19:32:23 +0000250static void intel_dp_unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700251{
252 int i;
253 if (dst_bytes > 4)
254 dst_bytes = 4;
255 for (i = 0; i < dst_bytes; i++)
256 dst[i] = src >> ((3-i) * 8);
257}
258
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700259/* hrawclock is 1/4 the FSB frequency */
260static int
261intel_hrawclk(struct drm_device *dev)
262{
263 struct drm_i915_private *dev_priv = dev->dev_private;
264 uint32_t clkcfg;
265
Vijay Purushothaman9473c8f2012-09-27 19:13:01 +0530266 /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
267 if (IS_VALLEYVIEW(dev))
268 return 200;
269
Keith Packardfb0f8fb2009-06-11 22:31:31 -0700270 clkcfg = I915_READ(CLKCFG);
271 switch (clkcfg & CLKCFG_FSB_MASK) {
272 case CLKCFG_FSB_400:
273 return 100;
274 case CLKCFG_FSB_533:
275 return 133;
276 case CLKCFG_FSB_667:
277 return 166;
278 case CLKCFG_FSB_800:
279 return 200;
280 case CLKCFG_FSB_1067:
281 return 266;
282 case CLKCFG_FSB_1333:
283 return 333;
284 /* these two are just a guess; one of them might be right */
285 case CLKCFG_FSB_1600:
286 case CLKCFG_FSB_1600_ALT:
287 return 400;
288 default:
289 return 133;
290 }
291}
292
Jani Nikulabf13e812013-09-06 07:40:05 +0300293static void
294intel_dp_init_panel_power_sequencer(struct drm_device *dev,
Ville Syrjälä36b5f422014-10-16 21:27:30 +0300295 struct intel_dp *intel_dp);
Jani Nikulabf13e812013-09-06 07:40:05 +0300296static void
297intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
Ville Syrjälä36b5f422014-10-16 21:27:30 +0300298 struct intel_dp *intel_dp);
Jani Nikulabf13e812013-09-06 07:40:05 +0300299
Ville Syrjälä773538e82014-09-04 14:54:56 +0300300static void pps_lock(struct intel_dp *intel_dp)
301{
302 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
303 struct intel_encoder *encoder = &intel_dig_port->base;
304 struct drm_device *dev = encoder->base.dev;
305 struct drm_i915_private *dev_priv = dev->dev_private;
306 enum intel_display_power_domain power_domain;
307
308 /*
309 * See vlv_power_sequencer_reset() why we need
310 * a power domain reference here.
311 */
312 power_domain = intel_display_port_power_domain(encoder);
313 intel_display_power_get(dev_priv, power_domain);
314
315 mutex_lock(&dev_priv->pps_mutex);
316}
317
318static void pps_unlock(struct intel_dp *intel_dp)
319{
320 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
321 struct intel_encoder *encoder = &intel_dig_port->base;
322 struct drm_device *dev = encoder->base.dev;
323 struct drm_i915_private *dev_priv = dev->dev_private;
324 enum intel_display_power_domain power_domain;
325
326 mutex_unlock(&dev_priv->pps_mutex);
327
328 power_domain = intel_display_port_power_domain(encoder);
329 intel_display_power_put(dev_priv, power_domain);
330}
331
Ville Syrjälä961a0db2014-10-16 21:29:42 +0300332static void
333vlv_power_sequencer_kick(struct intel_dp *intel_dp)
334{
335 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
336 struct drm_device *dev = intel_dig_port->base.base.dev;
337 struct drm_i915_private *dev_priv = dev->dev_private;
338 enum pipe pipe = intel_dp->pps_pipe;
Ville Syrjäläd288f652014-10-28 13:20:22 +0200339 bool pll_enabled;
Ville Syrjälä961a0db2014-10-16 21:29:42 +0300340 uint32_t DP;
341
342 if (WARN(I915_READ(intel_dp->output_reg) & DP_PORT_EN,
343 "skipping pipe %c power seqeuncer kick due to port %c being active\n",
344 pipe_name(pipe), port_name(intel_dig_port->port)))
345 return;
346
347 DRM_DEBUG_KMS("kicking pipe %c power sequencer for port %c\n",
348 pipe_name(pipe), port_name(intel_dig_port->port));
349
350 /* Preserve the BIOS-computed detected bit. This is
351 * supposed to be read-only.
352 */
353 DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
354 DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
355 DP |= DP_PORT_WIDTH(1);
356 DP |= DP_LINK_TRAIN_PAT_1;
357
358 if (IS_CHERRYVIEW(dev))
359 DP |= DP_PIPE_SELECT_CHV(pipe);
360 else if (pipe == PIPE_B)
361 DP |= DP_PIPEB_SELECT;
362
Ville Syrjäläd288f652014-10-28 13:20:22 +0200363 pll_enabled = I915_READ(DPLL(pipe)) & DPLL_VCO_ENABLE;
364
365 /*
366 * The DPLL for the pipe must be enabled for this to work.
367 * So enable temporarily it if it's not already enabled.
368 */
369 if (!pll_enabled)
370 vlv_force_pll_on(dev, pipe, IS_CHERRYVIEW(dev) ?
371 &chv_dpll[0].dpll : &vlv_dpll[0].dpll);
372
Ville Syrjälä961a0db2014-10-16 21:29:42 +0300373 /*
374 * Similar magic as in intel_dp_enable_port().
375 * We _must_ do this port enable + disable trick
376 * to make this power seqeuencer lock onto the port.
377 * Otherwise even VDD force bit won't work.
378 */
379 I915_WRITE(intel_dp->output_reg, DP);
380 POSTING_READ(intel_dp->output_reg);
381
382 I915_WRITE(intel_dp->output_reg, DP | DP_PORT_EN);
383 POSTING_READ(intel_dp->output_reg);
384
385 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
386 POSTING_READ(intel_dp->output_reg);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200387
388 if (!pll_enabled)
389 vlv_force_pll_off(dev, pipe);
Ville Syrjälä961a0db2014-10-16 21:29:42 +0300390}
391
Jani Nikulabf13e812013-09-06 07:40:05 +0300392static enum pipe
393vlv_power_sequencer_pipe(struct intel_dp *intel_dp)
394{
395 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Jani Nikulabf13e812013-09-06 07:40:05 +0300396 struct drm_device *dev = intel_dig_port->base.base.dev;
397 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300398 struct intel_encoder *encoder;
399 unsigned int pipes = (1 << PIPE_A) | (1 << PIPE_B);
Ville Syrjäläa8c33442014-10-16 21:29:59 +0300400 enum pipe pipe;
Jani Nikulabf13e812013-09-06 07:40:05 +0300401
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300402 lockdep_assert_held(&dev_priv->pps_mutex);
403
Ville Syrjäläa8c33442014-10-16 21:29:59 +0300404 /* We should never land here with regular DP ports */
405 WARN_ON(!is_edp(intel_dp));
406
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300407 if (intel_dp->pps_pipe != INVALID_PIPE)
408 return intel_dp->pps_pipe;
Jani Nikulabf13e812013-09-06 07:40:05 +0300409
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300410 /*
411 * We don't have power sequencer currently.
412 * Pick one that's not used by other ports.
413 */
414 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
415 base.head) {
416 struct intel_dp *tmp;
417
418 if (encoder->type != INTEL_OUTPUT_EDP)
419 continue;
420
421 tmp = enc_to_intel_dp(&encoder->base);
422
423 if (tmp->pps_pipe != INVALID_PIPE)
424 pipes &= ~(1 << tmp->pps_pipe);
425 }
426
427 /*
428 * Didn't find one. This should not happen since there
429 * are two power sequencers and up to two eDP ports.
430 */
431 if (WARN_ON(pipes == 0))
Ville Syrjäläa8c33442014-10-16 21:29:59 +0300432 pipe = PIPE_A;
433 else
434 pipe = ffs(pipes) - 1;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300435
Ville Syrjäläa8c33442014-10-16 21:29:59 +0300436 vlv_steal_power_sequencer(dev, pipe);
437 intel_dp->pps_pipe = pipe;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300438
439 DRM_DEBUG_KMS("picked pipe %c power sequencer for port %c\n",
440 pipe_name(intel_dp->pps_pipe),
441 port_name(intel_dig_port->port));
442
443 /* init power sequencer on this pipe and port */
Ville Syrjälä36b5f422014-10-16 21:27:30 +0300444 intel_dp_init_panel_power_sequencer(dev, intel_dp);
445 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300446
Ville Syrjälä961a0db2014-10-16 21:29:42 +0300447 /*
448 * Even vdd force doesn't work until we've made
449 * the power sequencer lock in on the port.
450 */
451 vlv_power_sequencer_kick(intel_dp);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300452
453 return intel_dp->pps_pipe;
454}
455
Ville Syrjälä6491ab22014-08-18 22:16:06 +0300456typedef bool (*vlv_pipe_check)(struct drm_i915_private *dev_priv,
457 enum pipe pipe);
458
459static bool vlv_pipe_has_pp_on(struct drm_i915_private *dev_priv,
460 enum pipe pipe)
461{
462 return I915_READ(VLV_PIPE_PP_STATUS(pipe)) & PP_ON;
463}
464
465static bool vlv_pipe_has_vdd_on(struct drm_i915_private *dev_priv,
466 enum pipe pipe)
467{
468 return I915_READ(VLV_PIPE_PP_CONTROL(pipe)) & EDP_FORCE_VDD;
469}
470
471static bool vlv_pipe_any(struct drm_i915_private *dev_priv,
472 enum pipe pipe)
473{
474 return true;
475}
476
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300477static enum pipe
Ville Syrjälä6491ab22014-08-18 22:16:06 +0300478vlv_initial_pps_pipe(struct drm_i915_private *dev_priv,
479 enum port port,
480 vlv_pipe_check pipe_check)
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300481{
Jani Nikulabf13e812013-09-06 07:40:05 +0300482 enum pipe pipe;
483
Jani Nikulabf13e812013-09-06 07:40:05 +0300484 for (pipe = PIPE_A; pipe <= PIPE_B; pipe++) {
485 u32 port_sel = I915_READ(VLV_PIPE_PP_ON_DELAYS(pipe)) &
486 PANEL_PORT_SELECT_MASK;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300487
488 if (port_sel != PANEL_PORT_SELECT_VLV(port))
489 continue;
490
Ville Syrjälä6491ab22014-08-18 22:16:06 +0300491 if (!pipe_check(dev_priv, pipe))
492 continue;
493
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300494 return pipe;
Jani Nikulabf13e812013-09-06 07:40:05 +0300495 }
496
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300497 return INVALID_PIPE;
498}
499
500static void
501vlv_initial_power_sequencer_setup(struct intel_dp *intel_dp)
502{
503 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
504 struct drm_device *dev = intel_dig_port->base.base.dev;
505 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300506 enum port port = intel_dig_port->port;
507
508 lockdep_assert_held(&dev_priv->pps_mutex);
509
510 /* try to find a pipe with this port selected */
Ville Syrjälä6491ab22014-08-18 22:16:06 +0300511 /* first pick one where the panel is on */
512 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
513 vlv_pipe_has_pp_on);
514 /* didn't find one? pick one where vdd is on */
515 if (intel_dp->pps_pipe == INVALID_PIPE)
516 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
517 vlv_pipe_has_vdd_on);
518 /* didn't find one? pick one with just the correct port */
519 if (intel_dp->pps_pipe == INVALID_PIPE)
520 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
521 vlv_pipe_any);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300522
523 /* didn't find one? just let vlv_power_sequencer_pipe() pick one when needed */
524 if (intel_dp->pps_pipe == INVALID_PIPE) {
525 DRM_DEBUG_KMS("no initial power sequencer for port %c\n",
526 port_name(port));
527 return;
528 }
529
530 DRM_DEBUG_KMS("initial power sequencer for port %c: pipe %c\n",
531 port_name(port), pipe_name(intel_dp->pps_pipe));
532
Ville Syrjälä36b5f422014-10-16 21:27:30 +0300533 intel_dp_init_panel_power_sequencer(dev, intel_dp);
534 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
Jani Nikulabf13e812013-09-06 07:40:05 +0300535}
536
Ville Syrjälä773538e82014-09-04 14:54:56 +0300537void vlv_power_sequencer_reset(struct drm_i915_private *dev_priv)
538{
539 struct drm_device *dev = dev_priv->dev;
540 struct intel_encoder *encoder;
541
542 if (WARN_ON(!IS_VALLEYVIEW(dev)))
543 return;
544
545 /*
546 * We can't grab pps_mutex here due to deadlock with power_domain
547 * mutex when power_domain functions are called while holding pps_mutex.
548 * That also means that in order to use pps_pipe the code needs to
549 * hold both a power domain reference and pps_mutex, and the power domain
550 * reference get/put must be done while _not_ holding pps_mutex.
551 * pps_{lock,unlock}() do these steps in the correct order, so one
552 * should use them always.
553 */
554
555 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
556 struct intel_dp *intel_dp;
557
558 if (encoder->type != INTEL_OUTPUT_EDP)
559 continue;
560
561 intel_dp = enc_to_intel_dp(&encoder->base);
562 intel_dp->pps_pipe = INVALID_PIPE;
563 }
Jani Nikulabf13e812013-09-06 07:40:05 +0300564}
565
566static u32 _pp_ctrl_reg(struct intel_dp *intel_dp)
567{
568 struct drm_device *dev = intel_dp_to_dev(intel_dp);
569
570 if (HAS_PCH_SPLIT(dev))
571 return PCH_PP_CONTROL;
572 else
573 return VLV_PIPE_PP_CONTROL(vlv_power_sequencer_pipe(intel_dp));
574}
575
576static u32 _pp_stat_reg(struct intel_dp *intel_dp)
577{
578 struct drm_device *dev = intel_dp_to_dev(intel_dp);
579
580 if (HAS_PCH_SPLIT(dev))
581 return PCH_PP_STATUS;
582 else
583 return VLV_PIPE_PP_STATUS(vlv_power_sequencer_pipe(intel_dp));
584}
585
Clint Taylor01527b32014-07-07 13:01:46 -0700586/* Reboot notifier handler to shutdown panel power to guarantee T12 timing
587 This function only applicable when panel PM state is not to be tracked */
588static int edp_notify_handler(struct notifier_block *this, unsigned long code,
589 void *unused)
590{
591 struct intel_dp *intel_dp = container_of(this, typeof(* intel_dp),
592 edp_notifier);
593 struct drm_device *dev = intel_dp_to_dev(intel_dp);
594 struct drm_i915_private *dev_priv = dev->dev_private;
595 u32 pp_div;
596 u32 pp_ctrl_reg, pp_div_reg;
Clint Taylor01527b32014-07-07 13:01:46 -0700597
598 if (!is_edp(intel_dp) || code != SYS_RESTART)
599 return 0;
600
Ville Syrjälä773538e82014-09-04 14:54:56 +0300601 pps_lock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300602
Clint Taylor01527b32014-07-07 13:01:46 -0700603 if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300604 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
605
Clint Taylor01527b32014-07-07 13:01:46 -0700606 pp_ctrl_reg = VLV_PIPE_PP_CONTROL(pipe);
607 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
608 pp_div = I915_READ(pp_div_reg);
609 pp_div &= PP_REFERENCE_DIVIDER_MASK;
610
611 /* 0x1F write to PP_DIV_REG sets max cycle delay */
612 I915_WRITE(pp_div_reg, pp_div | 0x1F);
613 I915_WRITE(pp_ctrl_reg, PANEL_UNLOCK_REGS | PANEL_POWER_OFF);
614 msleep(intel_dp->panel_power_cycle_delay);
615 }
616
Ville Syrjälä773538e82014-09-04 14:54:56 +0300617 pps_unlock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300618
Clint Taylor01527b32014-07-07 13:01:46 -0700619 return 0;
620}
621
Daniel Vetter4be73782014-01-17 14:39:48 +0100622static bool edp_have_panel_power(struct intel_dp *intel_dp)
Keith Packardebf33b12011-09-29 15:53:27 -0700623{
Paulo Zanoni30add222012-10-26 19:05:45 -0200624 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packardebf33b12011-09-29 15:53:27 -0700625 struct drm_i915_private *dev_priv = dev->dev_private;
626
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300627 lockdep_assert_held(&dev_priv->pps_mutex);
628
Ville Syrjälä9a423562014-10-16 21:29:48 +0300629 if (IS_VALLEYVIEW(dev) &&
630 intel_dp->pps_pipe == INVALID_PIPE)
631 return false;
632
Jani Nikulabf13e812013-09-06 07:40:05 +0300633 return (I915_READ(_pp_stat_reg(intel_dp)) & PP_ON) != 0;
Keith Packardebf33b12011-09-29 15:53:27 -0700634}
635
Daniel Vetter4be73782014-01-17 14:39:48 +0100636static bool edp_have_panel_vdd(struct intel_dp *intel_dp)
Keith Packardebf33b12011-09-29 15:53:27 -0700637{
Paulo Zanoni30add222012-10-26 19:05:45 -0200638 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packardebf33b12011-09-29 15:53:27 -0700639 struct drm_i915_private *dev_priv = dev->dev_private;
640
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300641 lockdep_assert_held(&dev_priv->pps_mutex);
642
Ville Syrjälä9a423562014-10-16 21:29:48 +0300643 if (IS_VALLEYVIEW(dev) &&
644 intel_dp->pps_pipe == INVALID_PIPE)
645 return false;
646
Ville Syrjälä773538e82014-09-04 14:54:56 +0300647 return I915_READ(_pp_ctrl_reg(intel_dp)) & EDP_FORCE_VDD;
Keith Packardebf33b12011-09-29 15:53:27 -0700648}
649
Keith Packard9b984da2011-09-19 13:54:47 -0700650static void
651intel_dp_check_edp(struct intel_dp *intel_dp)
652{
Paulo Zanoni30add222012-10-26 19:05:45 -0200653 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packard9b984da2011-09-19 13:54:47 -0700654 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packardebf33b12011-09-29 15:53:27 -0700655
Keith Packard9b984da2011-09-19 13:54:47 -0700656 if (!is_edp(intel_dp))
657 return;
Jesse Barnes453c5422013-03-28 09:55:41 -0700658
Daniel Vetter4be73782014-01-17 14:39:48 +0100659 if (!edp_have_panel_power(intel_dp) && !edp_have_panel_vdd(intel_dp)) {
Keith Packard9b984da2011-09-19 13:54:47 -0700660 WARN(1, "eDP powered off while attempting aux channel communication.\n");
661 DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
Jani Nikulabf13e812013-09-06 07:40:05 +0300662 I915_READ(_pp_stat_reg(intel_dp)),
663 I915_READ(_pp_ctrl_reg(intel_dp)));
Keith Packard9b984da2011-09-19 13:54:47 -0700664 }
665}
666
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100667static uint32_t
668intel_dp_aux_wait_done(struct intel_dp *intel_dp, bool has_aux_irq)
669{
670 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
671 struct drm_device *dev = intel_dig_port->base.base.dev;
672 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni9ed35ab2013-02-18 19:00:25 -0300673 uint32_t ch_ctl = intel_dp->aux_ch_ctl_reg;
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100674 uint32_t status;
675 bool done;
676
Daniel Vetteref04f002012-12-01 21:03:59 +0100677#define C (((status = I915_READ_NOTRACE(ch_ctl)) & DP_AUX_CH_CTL_SEND_BUSY) == 0)
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100678 if (has_aux_irq)
Paulo Zanonib18ac462013-02-18 19:00:24 -0300679 done = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
Imre Deak35987062013-05-21 20:03:20 +0300680 msecs_to_jiffies_timeout(10));
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100681 else
682 done = wait_for_atomic(C, 10) == 0;
683 if (!done)
684 DRM_ERROR("dp aux hw did not signal timeout (has irq: %i)!\n",
685 has_aux_irq);
686#undef C
687
688 return status;
689}
690
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000691static uint32_t i9xx_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
692{
693 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
694 struct drm_device *dev = intel_dig_port->base.base.dev;
695
696 /*
697 * The clock divider is based off the hrawclk, and would like to run at
698 * 2MHz. So, take the hrawclk value and divide by 2 and use that
699 */
700 return index ? 0 : intel_hrawclk(dev) / 2;
701}
702
703static uint32_t ilk_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
704{
705 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
706 struct drm_device *dev = intel_dig_port->base.base.dev;
707
708 if (index)
709 return 0;
710
711 if (intel_dig_port->port == PORT_A) {
712 if (IS_GEN6(dev) || IS_GEN7(dev))
713 return 200; /* SNB & IVB eDP input clock at 400Mhz */
714 else
715 return 225; /* eDP input clock at 450Mhz */
716 } else {
717 return DIV_ROUND_UP(intel_pch_rawclk(dev), 2);
718 }
719}
720
721static uint32_t hsw_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300722{
723 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
724 struct drm_device *dev = intel_dig_port->base.base.dev;
725 struct drm_i915_private *dev_priv = dev->dev_private;
726
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000727 if (intel_dig_port->port == PORT_A) {
Chris Wilsonbc866252013-07-21 16:00:03 +0100728 if (index)
729 return 0;
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000730 return DIV_ROUND_CLOSEST(intel_ddi_get_cdclk_freq(dev_priv), 2000);
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300731 } else if (dev_priv->pch_id == INTEL_PCH_LPT_DEVICE_ID_TYPE) {
732 /* Workaround for non-ULT HSW */
Chris Wilsonbc866252013-07-21 16:00:03 +0100733 switch (index) {
734 case 0: return 63;
735 case 1: return 72;
736 default: return 0;
737 }
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000738 } else {
Chris Wilsonbc866252013-07-21 16:00:03 +0100739 return index ? 0 : DIV_ROUND_UP(intel_pch_rawclk(dev), 2);
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300740 }
741}
742
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000743static uint32_t vlv_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
744{
745 return index ? 0 : 100;
746}
747
Damien Lespiaub6b5e382014-01-20 16:00:59 +0000748static uint32_t skl_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
749{
750 /*
751 * SKL doesn't need us to program the AUX clock divider (Hardware will
752 * derive the clock from CDCLK automatically). We still implement the
753 * get_aux_clock_divider vfunc to plug-in into the existing code.
754 */
755 return index ? 0 : 1;
756}
757
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000758static uint32_t i9xx_get_aux_send_ctl(struct intel_dp *intel_dp,
759 bool has_aux_irq,
760 int send_bytes,
761 uint32_t aux_clock_divider)
762{
763 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
764 struct drm_device *dev = intel_dig_port->base.base.dev;
765 uint32_t precharge, timeout;
766
767 if (IS_GEN6(dev))
768 precharge = 3;
769 else
770 precharge = 5;
771
772 if (IS_BROADWELL(dev) && intel_dp->aux_ch_ctl_reg == DPA_AUX_CH_CTL)
773 timeout = DP_AUX_CH_CTL_TIME_OUT_600us;
774 else
775 timeout = DP_AUX_CH_CTL_TIME_OUT_400us;
776
777 return DP_AUX_CH_CTL_SEND_BUSY |
Damien Lespiau788d4432014-01-20 15:52:31 +0000778 DP_AUX_CH_CTL_DONE |
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000779 (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
Damien Lespiau788d4432014-01-20 15:52:31 +0000780 DP_AUX_CH_CTL_TIME_OUT_ERROR |
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000781 timeout |
Damien Lespiau788d4432014-01-20 15:52:31 +0000782 DP_AUX_CH_CTL_RECEIVE_ERROR |
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000783 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
784 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
Damien Lespiau788d4432014-01-20 15:52:31 +0000785 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT);
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000786}
787
Damien Lespiaub9ca5fa2014-01-20 16:01:00 +0000788static uint32_t skl_get_aux_send_ctl(struct intel_dp *intel_dp,
789 bool has_aux_irq,
790 int send_bytes,
791 uint32_t unused)
792{
793 return DP_AUX_CH_CTL_SEND_BUSY |
794 DP_AUX_CH_CTL_DONE |
795 (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
796 DP_AUX_CH_CTL_TIME_OUT_ERROR |
797 DP_AUX_CH_CTL_TIME_OUT_1600us |
798 DP_AUX_CH_CTL_RECEIVE_ERROR |
799 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
800 DP_AUX_CH_CTL_SYNC_PULSE_SKL(32);
801}
802
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700803static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100804intel_dp_aux_ch(struct intel_dp *intel_dp,
Daniel Vetterbd9f74a2014-10-02 09:45:35 +0200805 const uint8_t *send, int send_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700806 uint8_t *recv, int recv_size)
807{
Paulo Zanoni174edf12012-10-26 19:05:50 -0200808 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
809 struct drm_device *dev = intel_dig_port->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700810 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni9ed35ab2013-02-18 19:00:25 -0300811 uint32_t ch_ctl = intel_dp->aux_ch_ctl_reg;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700812 uint32_t ch_data = ch_ctl + 4;
Chris Wilsonbc866252013-07-21 16:00:03 +0100813 uint32_t aux_clock_divider;
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100814 int i, ret, recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700815 uint32_t status;
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000816 int try, clock = 0;
Daniel Vetter4e6b7882014-02-07 16:33:20 +0100817 bool has_aux_irq = HAS_AUX_IRQ(dev);
Jani Nikula884f19e2014-03-14 16:51:14 +0200818 bool vdd;
819
Ville Syrjälä773538e82014-09-04 14:54:56 +0300820 pps_lock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300821
Ville Syrjälä72c35002014-08-18 22:16:00 +0300822 /*
823 * We will be called with VDD already enabled for dpcd/edid/oui reads.
824 * In such cases we want to leave VDD enabled and it's up to upper layers
825 * to turn it off. But for eg. i2c-dev access we need to turn it on/off
826 * ourselves.
827 */
Ville Syrjälä1e0560e2014-08-19 13:24:25 +0300828 vdd = edp_panel_vdd_on(intel_dp);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100829
830 /* dp aux is extremely sensitive to irq latency, hence request the
831 * lowest possible wakeup latency and so prevent the cpu from going into
832 * deep sleep states.
833 */
834 pm_qos_update_request(&dev_priv->pm_qos, 0);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700835
Keith Packard9b984da2011-09-19 13:54:47 -0700836 intel_dp_check_edp(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800837
Paulo Zanonic67a4702013-08-19 13:18:09 -0300838 intel_aux_display_runtime_get(dev_priv);
839
Jesse Barnes11bee432011-08-01 15:02:20 -0700840 /* Try to wait for any previous AUX channel activity */
841 for (try = 0; try < 3; try++) {
Daniel Vetteref04f002012-12-01 21:03:59 +0100842 status = I915_READ_NOTRACE(ch_ctl);
Jesse Barnes11bee432011-08-01 15:02:20 -0700843 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
844 break;
845 msleep(1);
846 }
847
848 if (try == 3) {
849 WARN(1, "dp_aux_ch not started status 0x%08x\n",
850 I915_READ(ch_ctl));
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100851 ret = -EBUSY;
852 goto out;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100853 }
854
Paulo Zanoni46a5ae92013-09-17 11:14:10 -0300855 /* Only 5 data registers! */
856 if (WARN_ON(send_bytes > 20 || recv_size > 20)) {
857 ret = -E2BIG;
858 goto out;
859 }
860
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000861 while ((aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, clock++))) {
Damien Lespiau153b1102014-01-21 13:37:15 +0000862 u32 send_ctl = intel_dp->get_aux_send_ctl(intel_dp,
863 has_aux_irq,
864 send_bytes,
865 aux_clock_divider);
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000866
Chris Wilsonbc866252013-07-21 16:00:03 +0100867 /* Must try at least 3 times according to DP spec */
868 for (try = 0; try < 5; try++) {
869 /* Load the send data into the aux channel data registers */
870 for (i = 0; i < send_bytes; i += 4)
871 I915_WRITE(ch_data + i,
Rodrigo Vivia4f12892014-11-14 08:52:27 -0800872 intel_dp_pack_aux(send + i,
873 send_bytes - i));
Akshay Joshi0206e352011-08-16 15:34:10 -0400874
Chris Wilsonbc866252013-07-21 16:00:03 +0100875 /* Send the command and wait for it to complete */
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000876 I915_WRITE(ch_ctl, send_ctl);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100877
Chris Wilsonbc866252013-07-21 16:00:03 +0100878 status = intel_dp_aux_wait_done(intel_dp, has_aux_irq);
Akshay Joshi0206e352011-08-16 15:34:10 -0400879
Chris Wilsonbc866252013-07-21 16:00:03 +0100880 /* Clear done status and any errors */
881 I915_WRITE(ch_ctl,
882 status |
883 DP_AUX_CH_CTL_DONE |
884 DP_AUX_CH_CTL_TIME_OUT_ERROR |
885 DP_AUX_CH_CTL_RECEIVE_ERROR);
Adam Jacksond7e96fe2011-07-26 15:39:46 -0400886
Chris Wilsonbc866252013-07-21 16:00:03 +0100887 if (status & (DP_AUX_CH_CTL_TIME_OUT_ERROR |
888 DP_AUX_CH_CTL_RECEIVE_ERROR))
889 continue;
890 if (status & DP_AUX_CH_CTL_DONE)
891 break;
892 }
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100893 if (status & DP_AUX_CH_CTL_DONE)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700894 break;
895 }
896
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700897 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700898 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100899 ret = -EBUSY;
900 goto out;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700901 }
902
903 /* Check for timeout or receive error.
904 * Timeouts occur when the sink is not connected
905 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700906 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700907 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100908 ret = -EIO;
909 goto out;
Keith Packarda5b3da52009-06-11 22:30:32 -0700910 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700911
912 /* Timeouts occur when the device isn't connected, so they're
913 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700914 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800915 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100916 ret = -ETIMEDOUT;
917 goto out;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700918 }
919
920 /* Unload any bytes sent back from the other side */
921 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
922 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700923 if (recv_bytes > recv_size)
924 recv_bytes = recv_size;
Akshay Joshi0206e352011-08-16 15:34:10 -0400925
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100926 for (i = 0; i < recv_bytes; i += 4)
Rodrigo Vivia4f12892014-11-14 08:52:27 -0800927 intel_dp_unpack_aux(I915_READ(ch_data + i),
928 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700929
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100930 ret = recv_bytes;
931out:
932 pm_qos_update_request(&dev_priv->pm_qos, PM_QOS_DEFAULT_VALUE);
Paulo Zanonic67a4702013-08-19 13:18:09 -0300933 intel_aux_display_runtime_put(dev_priv);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100934
Jani Nikula884f19e2014-03-14 16:51:14 +0200935 if (vdd)
936 edp_panel_vdd_off(intel_dp, false);
937
Ville Syrjälä773538e82014-09-04 14:54:56 +0300938 pps_unlock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300939
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100940 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700941}
942
Jani Nikulaa6c8aff02014-04-07 12:37:25 +0300943#define BARE_ADDRESS_SIZE 3
944#define HEADER_SIZE (BARE_ADDRESS_SIZE + 1)
Jani Nikula9d1a1032014-03-14 16:51:15 +0200945static ssize_t
946intel_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700947{
Jani Nikula9d1a1032014-03-14 16:51:15 +0200948 struct intel_dp *intel_dp = container_of(aux, struct intel_dp, aux);
949 uint8_t txbuf[20], rxbuf[20];
950 size_t txsize, rxsize;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700951 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700952
Jani Nikula9d1a1032014-03-14 16:51:15 +0200953 txbuf[0] = msg->request << 4;
954 txbuf[1] = msg->address >> 8;
955 txbuf[2] = msg->address & 0xff;
956 txbuf[3] = msg->size - 1;
Paulo Zanoni46a5ae92013-09-17 11:14:10 -0300957
Jani Nikula9d1a1032014-03-14 16:51:15 +0200958 switch (msg->request & ~DP_AUX_I2C_MOT) {
959 case DP_AUX_NATIVE_WRITE:
960 case DP_AUX_I2C_WRITE:
Jani Nikulaa6c8aff02014-04-07 12:37:25 +0300961 txsize = msg->size ? HEADER_SIZE + msg->size : BARE_ADDRESS_SIZE;
Jani Nikula9d1a1032014-03-14 16:51:15 +0200962 rxsize = 1;
Jani Nikulaf51a44b2014-02-11 11:52:05 +0200963
Jani Nikula9d1a1032014-03-14 16:51:15 +0200964 if (WARN_ON(txsize > 20))
965 return -E2BIG;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700966
Jani Nikula9d1a1032014-03-14 16:51:15 +0200967 memcpy(txbuf + HEADER_SIZE, msg->buffer, msg->size);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700968
Jani Nikula9d1a1032014-03-14 16:51:15 +0200969 ret = intel_dp_aux_ch(intel_dp, txbuf, txsize, rxbuf, rxsize);
970 if (ret > 0) {
971 msg->reply = rxbuf[0] >> 4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700972
Jani Nikula9d1a1032014-03-14 16:51:15 +0200973 /* Return payload size. */
974 ret = msg->size;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700975 }
Jani Nikula9d1a1032014-03-14 16:51:15 +0200976 break;
977
978 case DP_AUX_NATIVE_READ:
979 case DP_AUX_I2C_READ:
Jani Nikulaa6c8aff02014-04-07 12:37:25 +0300980 txsize = msg->size ? HEADER_SIZE : BARE_ADDRESS_SIZE;
Jani Nikula9d1a1032014-03-14 16:51:15 +0200981 rxsize = msg->size + 1;
982
983 if (WARN_ON(rxsize > 20))
984 return -E2BIG;
985
986 ret = intel_dp_aux_ch(intel_dp, txbuf, txsize, rxbuf, rxsize);
987 if (ret > 0) {
988 msg->reply = rxbuf[0] >> 4;
989 /*
990 * Assume happy day, and copy the data. The caller is
991 * expected to check msg->reply before touching it.
992 *
993 * Return payload size.
994 */
995 ret--;
996 memcpy(msg->buffer, rxbuf + 1, ret);
997 }
998 break;
999
1000 default:
1001 ret = -EINVAL;
1002 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001003 }
Jani Nikulaf51a44b2014-02-11 11:52:05 +02001004
Jani Nikula9d1a1032014-03-14 16:51:15 +02001005 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001006}
1007
Jani Nikula9d1a1032014-03-14 16:51:15 +02001008static void
1009intel_dp_aux_init(struct intel_dp *intel_dp, struct intel_connector *connector)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001010{
Jani Nikula9d1a1032014-03-14 16:51:15 +02001011 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jani Nikula33ad6622014-03-14 16:51:16 +02001012 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1013 enum port port = intel_dig_port->port;
Jani Nikula0b998362014-03-14 16:51:17 +02001014 const char *name = NULL;
Dave Airlieab2c0672009-12-04 10:55:24 +10001015 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001016
Jani Nikula33ad6622014-03-14 16:51:16 +02001017 switch (port) {
1018 case PORT_A:
1019 intel_dp->aux_ch_ctl_reg = DPA_AUX_CH_CTL;
Jani Nikula0b998362014-03-14 16:51:17 +02001020 name = "DPDDC-A";
Dave Airlieab2c0672009-12-04 10:55:24 +10001021 break;
Jani Nikula33ad6622014-03-14 16:51:16 +02001022 case PORT_B:
1023 intel_dp->aux_ch_ctl_reg = PCH_DPB_AUX_CH_CTL;
Jani Nikula0b998362014-03-14 16:51:17 +02001024 name = "DPDDC-B";
Jani Nikula33ad6622014-03-14 16:51:16 +02001025 break;
1026 case PORT_C:
1027 intel_dp->aux_ch_ctl_reg = PCH_DPC_AUX_CH_CTL;
Jani Nikula0b998362014-03-14 16:51:17 +02001028 name = "DPDDC-C";
Jani Nikula33ad6622014-03-14 16:51:16 +02001029 break;
1030 case PORT_D:
1031 intel_dp->aux_ch_ctl_reg = PCH_DPD_AUX_CH_CTL;
Jani Nikula0b998362014-03-14 16:51:17 +02001032 name = "DPDDC-D";
Dave Airlieab2c0672009-12-04 10:55:24 +10001033 break;
1034 default:
Jani Nikula33ad6622014-03-14 16:51:16 +02001035 BUG();
Dave Airlieab2c0672009-12-04 10:55:24 +10001036 }
1037
Damien Lespiau1b1aad72013-12-03 13:56:29 +00001038 /*
1039 * The AUX_CTL register is usually DP_CTL + 0x10.
1040 *
1041 * On Haswell and Broadwell though:
1042 * - Both port A DDI_BUF_CTL and DDI_AUX_CTL are on the CPU
1043 * - Port B/C/D AUX channels are on the PCH, DDI_BUF_CTL on the CPU
1044 *
1045 * Skylake moves AUX_CTL back next to DDI_BUF_CTL, on the CPU.
1046 */
1047 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Jani Nikula33ad6622014-03-14 16:51:16 +02001048 intel_dp->aux_ch_ctl_reg = intel_dp->output_reg + 0x10;
David Flynn8316f332010-12-08 16:10:21 +00001049
Jani Nikula0b998362014-03-14 16:51:17 +02001050 intel_dp->aux.name = name;
Jani Nikula9d1a1032014-03-14 16:51:15 +02001051 intel_dp->aux.dev = dev->dev;
1052 intel_dp->aux.transfer = intel_dp_aux_transfer;
David Flynn8316f332010-12-08 16:10:21 +00001053
Jani Nikula0b998362014-03-14 16:51:17 +02001054 DRM_DEBUG_KMS("registering %s bus for %s\n", name,
1055 connector->base.kdev->kobj.name);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001056
Dave Airlie4f71d0c2014-06-04 16:02:28 +10001057 ret = drm_dp_aux_register(&intel_dp->aux);
Jani Nikula0b998362014-03-14 16:51:17 +02001058 if (ret < 0) {
Dave Airlie4f71d0c2014-06-04 16:02:28 +10001059 DRM_ERROR("drm_dp_aux_register() for %s failed (%d)\n",
Jani Nikula0b998362014-03-14 16:51:17 +02001060 name, ret);
1061 return;
Dave Airlieab2c0672009-12-04 10:55:24 +10001062 }
David Flynn8316f332010-12-08 16:10:21 +00001063
Jani Nikula0b998362014-03-14 16:51:17 +02001064 ret = sysfs_create_link(&connector->base.kdev->kobj,
1065 &intel_dp->aux.ddc.dev.kobj,
1066 intel_dp->aux.ddc.dev.kobj.name);
1067 if (ret < 0) {
1068 DRM_ERROR("sysfs_create_link() for %s failed (%d)\n", name, ret);
Dave Airlie4f71d0c2014-06-04 16:02:28 +10001069 drm_dp_aux_unregister(&intel_dp->aux);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001070 }
1071}
1072
Imre Deak80f65de2014-02-11 17:12:49 +02001073static void
1074intel_dp_connector_unregister(struct intel_connector *intel_connector)
1075{
1076 struct intel_dp *intel_dp = intel_attached_dp(&intel_connector->base);
1077
Dave Airlie0e32b392014-05-02 14:02:48 +10001078 if (!intel_connector->mst_port)
1079 sysfs_remove_link(&intel_connector->base.kdev->kobj,
1080 intel_dp->aux.ddc.dev.kobj.name);
Imre Deak80f65de2014-02-11 17:12:49 +02001081 intel_connector_unregister(intel_connector);
1082}
1083
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001084static void
Sonika Jindalc3346ef2015-02-21 11:12:13 +05301085skl_edp_set_pll_config(struct intel_crtc_state *pipe_config, int link_clock)
Damien Lespiau5416d872014-11-14 17:24:33 +00001086{
1087 u32 ctrl1;
1088
1089 pipe_config->ddi_pll_sel = SKL_DPLL0;
1090 pipe_config->dpll_hw_state.cfgcr1 = 0;
1091 pipe_config->dpll_hw_state.cfgcr2 = 0;
1092
1093 ctrl1 = DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Sonika Jindalc3346ef2015-02-21 11:12:13 +05301094 switch (link_clock / 2) {
1095 case 81000:
Damien Lespiau5416d872014-11-14 17:24:33 +00001096 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_810,
1097 SKL_DPLL0);
1098 break;
Sonika Jindalc3346ef2015-02-21 11:12:13 +05301099 case 135000:
Damien Lespiau5416d872014-11-14 17:24:33 +00001100 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_1350,
1101 SKL_DPLL0);
1102 break;
Sonika Jindalc3346ef2015-02-21 11:12:13 +05301103 case 270000:
Damien Lespiau5416d872014-11-14 17:24:33 +00001104 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_2700,
1105 SKL_DPLL0);
1106 break;
Sonika Jindalc3346ef2015-02-21 11:12:13 +05301107 case 162000:
1108 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_1620,
1109 SKL_DPLL0);
1110 break;
1111 /* TBD: For DP link rates 2.16 GHz and 4.32 GHz, VCO is 8640 which
1112 results in CDCLK change. Need to handle the change of CDCLK by
1113 disabling pipes and re-enabling them */
1114 case 108000:
1115 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_1080,
1116 SKL_DPLL0);
1117 break;
1118 case 216000:
1119 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_2160,
1120 SKL_DPLL0);
1121 break;
1122
Damien Lespiau5416d872014-11-14 17:24:33 +00001123 }
1124 pipe_config->dpll_hw_state.ctrl1 = ctrl1;
1125}
1126
1127static void
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001128hsw_dp_set_ddi_pll_sel(struct intel_crtc_state *pipe_config, int link_bw)
Daniel Vetter0e503382014-07-04 11:26:04 -03001129{
1130 switch (link_bw) {
1131 case DP_LINK_BW_1_62:
1132 pipe_config->ddi_pll_sel = PORT_CLK_SEL_LCPLL_810;
1133 break;
1134 case DP_LINK_BW_2_7:
1135 pipe_config->ddi_pll_sel = PORT_CLK_SEL_LCPLL_1350;
1136 break;
1137 case DP_LINK_BW_5_4:
1138 pipe_config->ddi_pll_sel = PORT_CLK_SEL_LCPLL_2700;
1139 break;
1140 }
1141}
1142
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05301143static int
Ville Syrjäläf4896f12015-03-12 17:10:27 +02001144intel_read_sink_rates(struct intel_dp *intel_dp, int *sink_rates)
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05301145{
1146 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1147 int i = 0;
1148 uint16_t val;
1149
1150 if (INTEL_INFO(dev)->gen >= 9 && intel_dp->supported_rates[0]) {
1151 /*
1152 * Receiver supports only main-link rate selection by
1153 * link rate table method, so read link rates from
1154 * supported_link_rates
1155 */
1156 for (i = 0; i < DP_MAX_SUPPORTED_RATES; ++i) {
1157 val = le16_to_cpu(intel_dp->supported_rates[i]);
1158 if (val == 0)
1159 break;
1160
1161 sink_rates[i] = val * 200;
1162 }
1163
1164 if (i <= 0)
1165 DRM_ERROR("No rates in SUPPORTED_LINK_RATES");
1166 }
1167 return i;
1168}
1169
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301170static int
Ville Syrjäläf4896f12015-03-12 17:10:27 +02001171intel_read_source_rates(struct intel_dp *intel_dp, int *source_rates)
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301172{
1173 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1174 int i;
1175 int max_default_rate;
1176
1177 if (INTEL_INFO(dev)->gen >= 9 && intel_dp->supported_rates[0]) {
1178 for (i = 0; i < ARRAY_SIZE(gen9_rates); ++i)
1179 source_rates[i] = gen9_rates[i];
1180 } else {
1181 /* Index of the max_link_bw supported + 1 */
1182 max_default_rate = (intel_dp_max_link_bw(intel_dp) >> 3) + 1;
1183 for (i = 0; i < max_default_rate; ++i)
1184 source_rates[i] = default_rates[i];
1185 }
1186 return i;
1187}
1188
Daniel Vetter0e503382014-07-04 11:26:04 -03001189static void
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001190intel_dp_set_clock(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001191 struct intel_crtc_state *pipe_config, int link_bw)
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001192{
1193 struct drm_device *dev = encoder->base.dev;
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +08001194 const struct dp_link_dpll *divisor = NULL;
1195 int i, count = 0;
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001196
1197 if (IS_G4X(dev)) {
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +08001198 divisor = gen4_dpll;
1199 count = ARRAY_SIZE(gen4_dpll);
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001200 } else if (HAS_PCH_SPLIT(dev)) {
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +08001201 divisor = pch_dpll;
1202 count = ARRAY_SIZE(pch_dpll);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001203 } else if (IS_CHERRYVIEW(dev)) {
1204 divisor = chv_dpll;
1205 count = ARRAY_SIZE(chv_dpll);
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001206 } else if (IS_VALLEYVIEW(dev)) {
Chon Ming Lee65ce4bf2013-09-04 01:30:38 +08001207 divisor = vlv_dpll;
1208 count = ARRAY_SIZE(vlv_dpll);
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001209 }
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +08001210
1211 if (divisor && count) {
1212 for (i = 0; i < count; i++) {
1213 if (link_bw == divisor[i].link_bw) {
1214 pipe_config->dpll = divisor[i].dpll;
1215 pipe_config->clock_set = true;
1216 break;
1217 }
1218 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001219 }
1220}
1221
Ville Syrjäläf4896f12015-03-12 17:10:27 +02001222static int intel_supported_rates(const int *source_rates, int source_len,
1223 const int *sink_rates, int sink_len,
1224 int *supported_rates)
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301225{
1226 int i = 0, j = 0, k = 0;
1227
1228 /* For panels with edp version less than 1.4 */
1229 if (sink_len == 0) {
1230 for (i = 0; i < source_len; ++i)
1231 supported_rates[i] = source_rates[i];
1232 return source_len;
1233 }
1234
1235 /* For edp1.4 panels, find the common rates between source and sink */
1236 while (i < source_len && j < sink_len) {
1237 if (source_rates[i] == sink_rates[j]) {
1238 supported_rates[k] = source_rates[i];
1239 ++k;
1240 ++i;
1241 ++j;
1242 } else if (source_rates[i] < sink_rates[j]) {
1243 ++i;
1244 } else {
1245 ++j;
1246 }
1247 }
1248 return k;
1249}
1250
Ville Syrjäläf4896f12015-03-12 17:10:27 +02001251static int rate_to_index(int find, const int *rates)
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301252{
1253 int i = 0;
1254
1255 for (i = 0; i < DP_MAX_SUPPORTED_RATES; ++i)
1256 if (find == rates[i])
1257 break;
1258
1259 return i;
1260}
1261
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001262bool
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001263intel_dp_compute_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001264 struct intel_crtc_state *pipe_config)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001265{
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001266 struct drm_device *dev = encoder->base.dev;
Daniel Vetter36008362013-03-27 00:44:59 +01001267 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02001268 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001269 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deakbc7d38a2013-05-16 14:40:36 +03001270 enum port port = dp_to_dig_port(intel_dp)->port;
Jesse Barnes2dd24552013-04-25 12:55:01 -07001271 struct intel_crtc *intel_crtc = encoder->new_crtc;
Jani Nikuladd06f902012-10-19 14:51:50 +03001272 struct intel_connector *intel_connector = intel_dp->attached_connector;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001273 int lane_count, clock;
Jani Nikula56071a22014-05-06 14:56:52 +03001274 int min_lane_count = 1;
Paulo Zanonieeb63242014-05-06 14:56:50 +03001275 int max_lane_count = intel_dp_max_lane_count(intel_dp);
Todd Previte06ea66b2014-01-20 10:19:39 -07001276 /* Conveniently, the link BW constants become indices with a shift...*/
Jani Nikula56071a22014-05-06 14:56:52 +03001277 int min_clock = 0;
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301278 int max_clock;
Daniel Vetter083f9562012-04-20 20:23:49 +02001279 int bpp, mode_rate;
Daniel Vetterff9a6752013-06-01 17:16:21 +02001280 int link_avail, link_clock;
Ville Syrjäläf4896f12015-03-12 17:10:27 +02001281 int sink_rates[8];
1282 int supported_rates[8] = {0};
1283 int source_rates[8];
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301284 int source_len, sink_len, supported_len;
1285
1286 sink_len = intel_read_sink_rates(intel_dp, sink_rates);
1287
1288 source_len = intel_read_source_rates(intel_dp, source_rates);
1289
1290 supported_len = intel_supported_rates(source_rates, source_len,
1291 sink_rates, sink_len, supported_rates);
1292
1293 /* No common link rates between source and sink */
1294 WARN_ON(supported_len <= 0);
1295
1296 max_clock = supported_len - 1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001297
Imre Deakbc7d38a2013-05-16 14:40:36 +03001298 if (HAS_PCH_SPLIT(dev) && !HAS_DDI(dev) && port != PORT_A)
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001299 pipe_config->has_pch_encoder = true;
1300
Daniel Vetter03afc4a2013-04-02 23:42:31 +02001301 pipe_config->has_dp_encoder = true;
Vandana Kannanf769cd22014-08-05 07:51:22 -07001302 pipe_config->has_drrs = false;
Daniel Vetter9ed109a2014-04-24 23:54:52 +02001303 pipe_config->has_audio = intel_dp->has_audio;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001304
Jani Nikuladd06f902012-10-19 14:51:50 +03001305 if (is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
1306 intel_fixed_panel_mode(intel_connector->panel.fixed_mode,
1307 adjusted_mode);
Jesse Barnes2dd24552013-04-25 12:55:01 -07001308 if (!HAS_PCH_SPLIT(dev))
1309 intel_gmch_panel_fitting(intel_crtc, pipe_config,
1310 intel_connector->panel.fitting_mode);
1311 else
Jesse Barnesb074cec2013-04-25 12:55:02 -07001312 intel_pch_panel_fitting(intel_crtc, pipe_config,
1313 intel_connector->panel.fitting_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +01001314 }
1315
Daniel Vettercb1793c2012-06-04 18:39:21 +02001316 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
Daniel Vetter0af78a22012-05-23 11:30:55 +02001317 return false;
1318
Daniel Vetter083f9562012-04-20 20:23:49 +02001319 DRM_DEBUG_KMS("DP link computation with max lane count %i "
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301320 "max bw %d pixel clock %iKHz\n",
1321 max_lane_count, supported_rates[max_clock],
Damien Lespiau241bfc32013-09-25 16:45:37 +01001322 adjusted_mode->crtc_clock);
Daniel Vetter083f9562012-04-20 20:23:49 +02001323
Daniel Vetter36008362013-03-27 00:44:59 +01001324 /* Walk through all bpp values. Luckily they're all nicely spaced with 2
1325 * bpc in between. */
Daniel Vetter3e7ca982013-06-01 19:45:56 +02001326 bpp = pipe_config->pipe_bpp;
Jani Nikula56071a22014-05-06 14:56:52 +03001327 if (is_edp(intel_dp)) {
1328 if (dev_priv->vbt.edp_bpp && dev_priv->vbt.edp_bpp < bpp) {
1329 DRM_DEBUG_KMS("clamping bpp for eDP panel to BIOS-provided %i\n",
1330 dev_priv->vbt.edp_bpp);
1331 bpp = dev_priv->vbt.edp_bpp;
1332 }
1333
Jani Nikula344c5bb2014-09-09 11:25:13 +03001334 /*
1335 * Use the maximum clock and number of lanes the eDP panel
1336 * advertizes being capable of. The panels are generally
1337 * designed to support only a single clock and lane
1338 * configuration, and typically these values correspond to the
1339 * native resolution of the panel.
1340 */
1341 min_lane_count = max_lane_count;
1342 min_clock = max_clock;
Imre Deak79842112013-07-18 17:44:13 +03001343 }
Daniel Vetter657445f2013-05-04 10:09:18 +02001344
Daniel Vetter36008362013-03-27 00:44:59 +01001345 for (; bpp >= 6*3; bpp -= 2*3) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001346 mode_rate = intel_dp_link_required(adjusted_mode->crtc_clock,
1347 bpp);
Daniel Vetterc4867932012-04-10 10:42:36 +02001348
Dave Airliec6930992014-07-14 11:04:39 +10001349 for (clock = min_clock; clock <= max_clock; clock++) {
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301350 for (lane_count = min_lane_count;
1351 lane_count <= max_lane_count;
1352 lane_count <<= 1) {
1353
1354 link_clock = supported_rates[clock];
Daniel Vetter36008362013-03-27 00:44:59 +01001355 link_avail = intel_dp_max_data_rate(link_clock,
1356 lane_count);
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02001357
Daniel Vetter36008362013-03-27 00:44:59 +01001358 if (mode_rate <= link_avail) {
1359 goto found;
1360 }
1361 }
1362 }
1363 }
1364
1365 return false;
1366
1367found:
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02001368 if (intel_dp->color_range_auto) {
1369 /*
1370 * See:
1371 * CEA-861-E - 5.1 Default Encoding Parameters
1372 * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
1373 */
Thierry Reding18316c82012-12-20 15:41:44 +01001374 if (bpp != 18 && drm_match_cea_mode(adjusted_mode) > 1)
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02001375 intel_dp->color_range = DP_COLOR_RANGE_16_235;
1376 else
1377 intel_dp->color_range = 0;
1378 }
1379
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02001380 if (intel_dp->color_range)
Daniel Vetter50f3b012013-03-27 00:44:56 +01001381 pipe_config->limited_color_range = true;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02001382
Daniel Vetter36008362013-03-27 00:44:59 +01001383 intel_dp->lane_count = lane_count;
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301384
1385 intel_dp->link_bw =
1386 drm_dp_link_rate_to_bw_code(supported_rates[clock]);
1387
1388 if (INTEL_INFO(dev)->gen >= 9 && intel_dp->supported_rates[0]) {
1389 intel_dp->rate_select =
1390 rate_to_index(supported_rates[clock], sink_rates);
1391 intel_dp->link_bw = 0;
1392 }
1393
Daniel Vetter657445f2013-05-04 10:09:18 +02001394 pipe_config->pipe_bpp = bpp;
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301395 pipe_config->port_clock = supported_rates[clock];
Daniel Vetterc4867932012-04-10 10:42:36 +02001396
Daniel Vetter36008362013-03-27 00:44:59 +01001397 DRM_DEBUG_KMS("DP link bw %02x lane count %d clock %d bpp %d\n",
1398 intel_dp->link_bw, intel_dp->lane_count,
Daniel Vetterff9a6752013-06-01 17:16:21 +02001399 pipe_config->port_clock, bpp);
Daniel Vetter36008362013-03-27 00:44:59 +01001400 DRM_DEBUG_KMS("DP link bw required %i available %i\n",
1401 mode_rate, link_avail);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001402
Daniel Vetter03afc4a2013-04-02 23:42:31 +02001403 intel_link_compute_m_n(bpp, lane_count,
Damien Lespiau241bfc32013-09-25 16:45:37 +01001404 adjusted_mode->crtc_clock,
1405 pipe_config->port_clock,
Daniel Vetter03afc4a2013-04-02 23:42:31 +02001406 &pipe_config->dp_m_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001407
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05301408 if (intel_connector->panel.downclock_mode != NULL &&
Vandana Kannan96178ee2015-01-10 02:25:56 +05301409 dev_priv->drrs.type == SEAMLESS_DRRS_SUPPORT) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07001410 pipe_config->has_drrs = true;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05301411 intel_link_compute_m_n(bpp, lane_count,
1412 intel_connector->panel.downclock_mode->clock,
1413 pipe_config->port_clock,
1414 &pipe_config->dp_m2_n2);
1415 }
1416
Damien Lespiau5416d872014-11-14 17:24:33 +00001417 if (IS_SKYLAKE(dev) && is_edp(intel_dp))
Sonika Jindalc3346ef2015-02-21 11:12:13 +05301418 skl_edp_set_pll_config(pipe_config, supported_rates[clock]);
Damien Lespiau5416d872014-11-14 17:24:33 +00001419 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Daniel Vetter0e503382014-07-04 11:26:04 -03001420 hsw_dp_set_ddi_pll_sel(pipe_config, intel_dp->link_bw);
1421 else
1422 intel_dp_set_clock(encoder, pipe_config, intel_dp->link_bw);
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001423
Daniel Vetter36008362013-03-27 00:44:59 +01001424 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001425}
1426
Daniel Vetter7c62a162013-06-01 17:16:20 +02001427static void ironlake_set_pll_cpu_edp(struct intel_dp *intel_dp)
Daniel Vetterea9b6002012-11-29 15:59:31 +01001428{
Daniel Vetter7c62a162013-06-01 17:16:20 +02001429 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1430 struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
1431 struct drm_device *dev = crtc->base.dev;
Daniel Vetterea9b6002012-11-29 15:59:31 +01001432 struct drm_i915_private *dev_priv = dev->dev_private;
1433 u32 dpa_ctl;
1434
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001435 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n",
1436 crtc->config->port_clock);
Daniel Vetterea9b6002012-11-29 15:59:31 +01001437 dpa_ctl = I915_READ(DP_A);
1438 dpa_ctl &= ~DP_PLL_FREQ_MASK;
1439
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001440 if (crtc->config->port_clock == 162000) {
Daniel Vetter1ce17032012-11-29 15:59:32 +01001441 /* For a long time we've carried around a ILK-DevA w/a for the
1442 * 160MHz clock. If we're really unlucky, it's still required.
1443 */
1444 DRM_DEBUG_KMS("160MHz cpu eDP clock, might need ilk devA w/a\n");
Daniel Vetterea9b6002012-11-29 15:59:31 +01001445 dpa_ctl |= DP_PLL_FREQ_160MHZ;
Daniel Vetter7c62a162013-06-01 17:16:20 +02001446 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
Daniel Vetterea9b6002012-11-29 15:59:31 +01001447 } else {
1448 dpa_ctl |= DP_PLL_FREQ_270MHZ;
Daniel Vetter7c62a162013-06-01 17:16:20 +02001449 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
Daniel Vetterea9b6002012-11-29 15:59:31 +01001450 }
Daniel Vetter1ce17032012-11-29 15:59:32 +01001451
Daniel Vetterea9b6002012-11-29 15:59:31 +01001452 I915_WRITE(DP_A, dpa_ctl);
1453
1454 POSTING_READ(DP_A);
1455 udelay(500);
1456}
1457
Daniel Vetter8ac33ed2014-04-24 23:54:54 +02001458static void intel_dp_prepare(struct intel_encoder *encoder)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001459{
Daniel Vetterb934223d2013-07-21 21:37:05 +02001460 struct drm_device *dev = encoder->base.dev;
Keith Packard417e8222011-11-01 19:54:11 -07001461 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterb934223d2013-07-21 21:37:05 +02001462 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deakbc7d38a2013-05-16 14:40:36 +03001463 enum port port = dp_to_dig_port(intel_dp)->port;
Daniel Vetterb934223d2013-07-21 21:37:05 +02001464 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001465 struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001466
Keith Packard417e8222011-11-01 19:54:11 -07001467 /*
Keith Packard1a2eb462011-11-16 16:26:07 -08001468 * There are four kinds of DP registers:
Keith Packard417e8222011-11-01 19:54:11 -07001469 *
1470 * IBX PCH
Keith Packard1a2eb462011-11-16 16:26:07 -08001471 * SNB CPU
1472 * IVB CPU
Keith Packard417e8222011-11-01 19:54:11 -07001473 * CPT PCH
1474 *
1475 * IBX PCH and CPU are the same for almost everything,
1476 * except that the CPU DP PLL is configured in this
1477 * register
1478 *
1479 * CPT PCH is quite different, having many bits moved
1480 * to the TRANS_DP_CTL register instead. That
1481 * configuration happens (oddly) in ironlake_pch_enable
1482 */
Adam Jackson9c9e7922010-04-05 17:57:59 -04001483
Keith Packard417e8222011-11-01 19:54:11 -07001484 /* Preserve the BIOS-computed detected bit. This is
1485 * supposed to be read-only.
1486 */
1487 intel_dp->DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001488
Keith Packard417e8222011-11-01 19:54:11 -07001489 /* Handle DP bits in common between all three register formats */
Keith Packard417e8222011-11-01 19:54:11 -07001490 intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
Daniel Vetter17aa6be2013-04-30 14:01:40 +02001491 intel_dp->DP |= DP_PORT_WIDTH(intel_dp->lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001492
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001493 if (crtc->config->has_audio)
Chris Wilsonea5b2132010-08-04 13:50:23 +01001494 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Paulo Zanoni247d89f2012-10-15 15:51:33 -03001495
Keith Packard417e8222011-11-01 19:54:11 -07001496 /* Split out the IBX/CPU vs CPT settings */
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001497
Imre Deakbc7d38a2013-05-16 14:40:36 +03001498 if (port == PORT_A && IS_GEN7(dev) && !IS_VALLEYVIEW(dev)) {
Keith Packard1a2eb462011-11-16 16:26:07 -08001499 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
1500 intel_dp->DP |= DP_SYNC_HS_HIGH;
1501 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
1502 intel_dp->DP |= DP_SYNC_VS_HIGH;
1503 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
1504
Jani Nikula6aba5b62013-10-04 15:08:10 +03001505 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
Keith Packard1a2eb462011-11-16 16:26:07 -08001506 intel_dp->DP |= DP_ENHANCED_FRAMING;
1507
Daniel Vetter7c62a162013-06-01 17:16:20 +02001508 intel_dp->DP |= crtc->pipe << 29;
Imre Deakbc7d38a2013-05-16 14:40:36 +03001509 } else if (!HAS_PCH_CPT(dev) || port == PORT_A) {
Jesse Barnesb2634012013-03-28 09:55:40 -07001510 if (!HAS_PCH_SPLIT(dev) && !IS_VALLEYVIEW(dev))
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02001511 intel_dp->DP |= intel_dp->color_range;
Keith Packard417e8222011-11-01 19:54:11 -07001512
1513 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
1514 intel_dp->DP |= DP_SYNC_HS_HIGH;
1515 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
1516 intel_dp->DP |= DP_SYNC_VS_HIGH;
1517 intel_dp->DP |= DP_LINK_TRAIN_OFF;
1518
Jani Nikula6aba5b62013-10-04 15:08:10 +03001519 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
Keith Packard417e8222011-11-01 19:54:11 -07001520 intel_dp->DP |= DP_ENHANCED_FRAMING;
1521
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001522 if (!IS_CHERRYVIEW(dev)) {
1523 if (crtc->pipe == 1)
1524 intel_dp->DP |= DP_PIPEB_SELECT;
1525 } else {
1526 intel_dp->DP |= DP_PIPE_SELECT_CHV(crtc->pipe);
1527 }
Keith Packard417e8222011-11-01 19:54:11 -07001528 } else {
1529 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001530 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001531}
1532
Paulo Zanoniffd6749d2013-12-19 14:29:42 -02001533#define IDLE_ON_MASK (PP_ON | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
1534#define IDLE_ON_VALUE (PP_ON | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_ON_IDLE)
Keith Packard99ea7122011-11-01 19:57:50 -07001535
Paulo Zanoni1a5ef5b2013-12-19 14:29:43 -02001536#define IDLE_OFF_MASK (PP_ON | PP_SEQUENCE_MASK | 0 | 0)
1537#define IDLE_OFF_VALUE (0 | PP_SEQUENCE_NONE | 0 | 0)
Keith Packard99ea7122011-11-01 19:57:50 -07001538
Paulo Zanoniffd6749d2013-12-19 14:29:42 -02001539#define IDLE_CYCLE_MASK (PP_ON | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
1540#define IDLE_CYCLE_VALUE (0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
Keith Packard99ea7122011-11-01 19:57:50 -07001541
Daniel Vetter4be73782014-01-17 14:39:48 +01001542static void wait_panel_status(struct intel_dp *intel_dp,
Keith Packard99ea7122011-11-01 19:57:50 -07001543 u32 mask,
1544 u32 value)
1545{
Paulo Zanoni30add222012-10-26 19:05:45 -02001546 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packard99ea7122011-11-01 19:57:50 -07001547 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes453c5422013-03-28 09:55:41 -07001548 u32 pp_stat_reg, pp_ctrl_reg;
1549
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001550 lockdep_assert_held(&dev_priv->pps_mutex);
1551
Jani Nikulabf13e812013-09-06 07:40:05 +03001552 pp_stat_reg = _pp_stat_reg(intel_dp);
1553 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Keith Packard99ea7122011-11-01 19:57:50 -07001554
1555 DRM_DEBUG_KMS("mask %08x value %08x status %08x control %08x\n",
Jesse Barnes453c5422013-03-28 09:55:41 -07001556 mask, value,
1557 I915_READ(pp_stat_reg),
1558 I915_READ(pp_ctrl_reg));
Keith Packard99ea7122011-11-01 19:57:50 -07001559
Jesse Barnes453c5422013-03-28 09:55:41 -07001560 if (_wait_for((I915_READ(pp_stat_reg) & mask) == value, 5000, 10)) {
Keith Packard99ea7122011-11-01 19:57:50 -07001561 DRM_ERROR("Panel status timeout: status %08x control %08x\n",
Jesse Barnes453c5422013-03-28 09:55:41 -07001562 I915_READ(pp_stat_reg),
1563 I915_READ(pp_ctrl_reg));
Keith Packard99ea7122011-11-01 19:57:50 -07001564 }
Chris Wilson54c136d2013-12-02 09:57:16 +00001565
1566 DRM_DEBUG_KMS("Wait complete\n");
Keith Packard99ea7122011-11-01 19:57:50 -07001567}
1568
Daniel Vetter4be73782014-01-17 14:39:48 +01001569static void wait_panel_on(struct intel_dp *intel_dp)
Keith Packard99ea7122011-11-01 19:57:50 -07001570{
1571 DRM_DEBUG_KMS("Wait for panel power on\n");
Daniel Vetter4be73782014-01-17 14:39:48 +01001572 wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
Keith Packard99ea7122011-11-01 19:57:50 -07001573}
1574
Daniel Vetter4be73782014-01-17 14:39:48 +01001575static void wait_panel_off(struct intel_dp *intel_dp)
Keith Packardbd943152011-09-18 23:09:52 -07001576{
Keith Packardbd943152011-09-18 23:09:52 -07001577 DRM_DEBUG_KMS("Wait for panel power off time\n");
Daniel Vetter4be73782014-01-17 14:39:48 +01001578 wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
Keith Packardbd943152011-09-18 23:09:52 -07001579}
Keith Packardbd943152011-09-18 23:09:52 -07001580
Daniel Vetter4be73782014-01-17 14:39:48 +01001581static void wait_panel_power_cycle(struct intel_dp *intel_dp)
Keith Packard99ea7122011-11-01 19:57:50 -07001582{
1583 DRM_DEBUG_KMS("Wait for panel power cycle\n");
Paulo Zanonidce56b32013-12-19 14:29:40 -02001584
1585 /* When we disable the VDD override bit last we have to do the manual
1586 * wait. */
1587 wait_remaining_ms_from_jiffies(intel_dp->last_power_cycle,
1588 intel_dp->panel_power_cycle_delay);
1589
Daniel Vetter4be73782014-01-17 14:39:48 +01001590 wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
Keith Packard99ea7122011-11-01 19:57:50 -07001591}
Keith Packardbd943152011-09-18 23:09:52 -07001592
Daniel Vetter4be73782014-01-17 14:39:48 +01001593static void wait_backlight_on(struct intel_dp *intel_dp)
Paulo Zanonidce56b32013-12-19 14:29:40 -02001594{
1595 wait_remaining_ms_from_jiffies(intel_dp->last_power_on,
1596 intel_dp->backlight_on_delay);
1597}
1598
Daniel Vetter4be73782014-01-17 14:39:48 +01001599static void edp_wait_backlight_off(struct intel_dp *intel_dp)
Paulo Zanonidce56b32013-12-19 14:29:40 -02001600{
1601 wait_remaining_ms_from_jiffies(intel_dp->last_backlight_off,
1602 intel_dp->backlight_off_delay);
1603}
Keith Packard99ea7122011-11-01 19:57:50 -07001604
Keith Packard832dd3c2011-11-01 19:34:06 -07001605/* Read the current pp_control value, unlocking the register if it
1606 * is locked
1607 */
1608
Jesse Barnes453c5422013-03-28 09:55:41 -07001609static u32 ironlake_get_pp_control(struct intel_dp *intel_dp)
Keith Packard832dd3c2011-11-01 19:34:06 -07001610{
Jesse Barnes453c5422013-03-28 09:55:41 -07001611 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1612 struct drm_i915_private *dev_priv = dev->dev_private;
1613 u32 control;
Jesse Barnes453c5422013-03-28 09:55:41 -07001614
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001615 lockdep_assert_held(&dev_priv->pps_mutex);
1616
Jani Nikulabf13e812013-09-06 07:40:05 +03001617 control = I915_READ(_pp_ctrl_reg(intel_dp));
Keith Packard832dd3c2011-11-01 19:34:06 -07001618 control &= ~PANEL_UNLOCK_MASK;
1619 control |= PANEL_UNLOCK_REGS;
1620 return control;
Keith Packardbd943152011-09-18 23:09:52 -07001621}
1622
Ville Syrjälä951468f2014-09-04 14:55:31 +03001623/*
1624 * Must be paired with edp_panel_vdd_off().
1625 * Must hold pps_mutex around the whole on/off sequence.
1626 * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
1627 */
Ville Syrjälä1e0560e2014-08-19 13:24:25 +03001628static bool edp_panel_vdd_on(struct intel_dp *intel_dp)
Jesse Barnes5d613502011-01-24 17:10:54 -08001629{
Paulo Zanoni30add222012-10-26 19:05:45 -02001630 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Imre Deak4e6e1a52014-03-27 17:45:11 +02001631 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1632 struct intel_encoder *intel_encoder = &intel_dig_port->base;
Jesse Barnes5d613502011-01-24 17:10:54 -08001633 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak4e6e1a52014-03-27 17:45:11 +02001634 enum intel_display_power_domain power_domain;
Jesse Barnes5d613502011-01-24 17:10:54 -08001635 u32 pp;
Jesse Barnes453c5422013-03-28 09:55:41 -07001636 u32 pp_stat_reg, pp_ctrl_reg;
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001637 bool need_to_disable = !intel_dp->want_panel_vdd;
Jesse Barnes5d613502011-01-24 17:10:54 -08001638
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001639 lockdep_assert_held(&dev_priv->pps_mutex);
1640
Keith Packard97af61f572011-09-28 16:23:51 -07001641 if (!is_edp(intel_dp))
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001642 return false;
Keith Packardbd943152011-09-18 23:09:52 -07001643
Egbert Eich2c623c12014-11-25 12:54:57 +01001644 cancel_delayed_work(&intel_dp->panel_vdd_work);
Keith Packardbd943152011-09-18 23:09:52 -07001645 intel_dp->want_panel_vdd = true;
Keith Packard99ea7122011-11-01 19:57:50 -07001646
Daniel Vetter4be73782014-01-17 14:39:48 +01001647 if (edp_have_panel_vdd(intel_dp))
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001648 return need_to_disable;
Paulo Zanonib0665d52013-10-30 19:50:27 -02001649
Imre Deak4e6e1a52014-03-27 17:45:11 +02001650 power_domain = intel_display_port_power_domain(intel_encoder);
1651 intel_display_power_get(dev_priv, power_domain);
Paulo Zanonie9cb81a2013-11-21 13:47:23 -02001652
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001653 DRM_DEBUG_KMS("Turning eDP port %c VDD on\n",
1654 port_name(intel_dig_port->port));
Keith Packardbd943152011-09-18 23:09:52 -07001655
Daniel Vetter4be73782014-01-17 14:39:48 +01001656 if (!edp_have_panel_power(intel_dp))
1657 wait_panel_power_cycle(intel_dp);
Keith Packard99ea7122011-11-01 19:57:50 -07001658
Jesse Barnes453c5422013-03-28 09:55:41 -07001659 pp = ironlake_get_pp_control(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001660 pp |= EDP_FORCE_VDD;
Keith Packardebf33b12011-09-29 15:53:27 -07001661
Jani Nikulabf13e812013-09-06 07:40:05 +03001662 pp_stat_reg = _pp_stat_reg(intel_dp);
1663 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001664
1665 I915_WRITE(pp_ctrl_reg, pp);
1666 POSTING_READ(pp_ctrl_reg);
1667 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
1668 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
Keith Packardebf33b12011-09-29 15:53:27 -07001669 /*
1670 * If the panel wasn't on, delay before accessing aux channel
1671 */
Daniel Vetter4be73782014-01-17 14:39:48 +01001672 if (!edp_have_panel_power(intel_dp)) {
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001673 DRM_DEBUG_KMS("eDP port %c panel power wasn't enabled\n",
1674 port_name(intel_dig_port->port));
Keith Packardf01eca22011-09-28 16:48:10 -07001675 msleep(intel_dp->panel_power_up_delay);
Keith Packardf01eca22011-09-28 16:48:10 -07001676 }
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001677
1678 return need_to_disable;
1679}
1680
Ville Syrjälä951468f2014-09-04 14:55:31 +03001681/*
1682 * Must be paired with intel_edp_panel_vdd_off() or
1683 * intel_edp_panel_off().
1684 * Nested calls to these functions are not allowed since
1685 * we drop the lock. Caller must use some higher level
1686 * locking to prevent nested calls from other threads.
1687 */
Daniel Vetterb80d6c72014-03-19 15:54:37 +01001688void intel_edp_panel_vdd_on(struct intel_dp *intel_dp)
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001689{
Ville Syrjäläc695b6b2014-08-18 22:16:03 +03001690 bool vdd;
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001691
Ville Syrjäläc695b6b2014-08-18 22:16:03 +03001692 if (!is_edp(intel_dp))
1693 return;
1694
Ville Syrjälä773538e82014-09-04 14:54:56 +03001695 pps_lock(intel_dp);
Ville Syrjäläc695b6b2014-08-18 22:16:03 +03001696 vdd = edp_panel_vdd_on(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03001697 pps_unlock(intel_dp);
Ville Syrjäläc695b6b2014-08-18 22:16:03 +03001698
Rob Clarke2c719b2014-12-15 13:56:32 -05001699 I915_STATE_WARN(!vdd, "eDP port %c VDD already requested on\n",
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001700 port_name(dp_to_dig_port(intel_dp)->port));
Jesse Barnes5d613502011-01-24 17:10:54 -08001701}
1702
Daniel Vetter4be73782014-01-17 14:39:48 +01001703static void edp_panel_vdd_off_sync(struct intel_dp *intel_dp)
Jesse Barnes5d613502011-01-24 17:10:54 -08001704{
Paulo Zanoni30add222012-10-26 19:05:45 -02001705 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001706 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001707 struct intel_digital_port *intel_dig_port =
1708 dp_to_dig_port(intel_dp);
1709 struct intel_encoder *intel_encoder = &intel_dig_port->base;
1710 enum intel_display_power_domain power_domain;
Jesse Barnes5d613502011-01-24 17:10:54 -08001711 u32 pp;
Jesse Barnes453c5422013-03-28 09:55:41 -07001712 u32 pp_stat_reg, pp_ctrl_reg;
Jesse Barnes5d613502011-01-24 17:10:54 -08001713
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001714 lockdep_assert_held(&dev_priv->pps_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001715
Ville Syrjälä15e899a2014-08-18 22:16:02 +03001716 WARN_ON(intel_dp->want_panel_vdd);
Imre Deak4e6e1a52014-03-27 17:45:11 +02001717
Ville Syrjälä15e899a2014-08-18 22:16:02 +03001718 if (!edp_have_panel_vdd(intel_dp))
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001719 return;
Paulo Zanonib0665d52013-10-30 19:50:27 -02001720
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001721 DRM_DEBUG_KMS("Turning eDP port %c VDD off\n",
1722 port_name(intel_dig_port->port));
Jesse Barnes453c5422013-03-28 09:55:41 -07001723
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001724 pp = ironlake_get_pp_control(intel_dp);
1725 pp &= ~EDP_FORCE_VDD;
Jesse Barnes453c5422013-03-28 09:55:41 -07001726
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001727 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
1728 pp_stat_reg = _pp_stat_reg(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001729
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001730 I915_WRITE(pp_ctrl_reg, pp);
1731 POSTING_READ(pp_ctrl_reg);
Paulo Zanoni90791a52013-12-06 17:32:42 -02001732
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001733 /* Make sure sequencer is idle before allowing subsequent activity */
1734 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
1735 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
Paulo Zanonie9cb81a2013-11-21 13:47:23 -02001736
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001737 if ((pp & POWER_TARGET_ON) == 0)
1738 intel_dp->last_power_cycle = jiffies;
Paulo Zanonie9cb81a2013-11-21 13:47:23 -02001739
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001740 power_domain = intel_display_port_power_domain(intel_encoder);
1741 intel_display_power_put(dev_priv, power_domain);
Keith Packardbd943152011-09-18 23:09:52 -07001742}
1743
Daniel Vetter4be73782014-01-17 14:39:48 +01001744static void edp_panel_vdd_work(struct work_struct *__work)
Keith Packardbd943152011-09-18 23:09:52 -07001745{
1746 struct intel_dp *intel_dp = container_of(to_delayed_work(__work),
1747 struct intel_dp, panel_vdd_work);
Keith Packardbd943152011-09-18 23:09:52 -07001748
Ville Syrjälä773538e82014-09-04 14:54:56 +03001749 pps_lock(intel_dp);
Ville Syrjälä15e899a2014-08-18 22:16:02 +03001750 if (!intel_dp->want_panel_vdd)
1751 edp_panel_vdd_off_sync(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03001752 pps_unlock(intel_dp);
Keith Packardbd943152011-09-18 23:09:52 -07001753}
1754
Imre Deakaba86892014-07-30 15:57:31 +03001755static void edp_panel_vdd_schedule_off(struct intel_dp *intel_dp)
1756{
1757 unsigned long delay;
1758
1759 /*
1760 * Queue the timer to fire a long time from now (relative to the power
1761 * down delay) to keep the panel power up across a sequence of
1762 * operations.
1763 */
1764 delay = msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5);
1765 schedule_delayed_work(&intel_dp->panel_vdd_work, delay);
1766}
1767
Ville Syrjälä951468f2014-09-04 14:55:31 +03001768/*
1769 * Must be paired with edp_panel_vdd_on().
1770 * Must hold pps_mutex around the whole on/off sequence.
1771 * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
1772 */
Daniel Vetter4be73782014-01-17 14:39:48 +01001773static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
Keith Packardbd943152011-09-18 23:09:52 -07001774{
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001775 struct drm_i915_private *dev_priv =
1776 intel_dp_to_dev(intel_dp)->dev_private;
1777
1778 lockdep_assert_held(&dev_priv->pps_mutex);
1779
Keith Packard97af61f572011-09-28 16:23:51 -07001780 if (!is_edp(intel_dp))
1781 return;
Jesse Barnes5d613502011-01-24 17:10:54 -08001782
Rob Clarke2c719b2014-12-15 13:56:32 -05001783 I915_STATE_WARN(!intel_dp->want_panel_vdd, "eDP port %c VDD not forced on",
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001784 port_name(dp_to_dig_port(intel_dp)->port));
Keith Packardf2e8b182011-11-01 20:01:35 -07001785
Keith Packardbd943152011-09-18 23:09:52 -07001786 intel_dp->want_panel_vdd = false;
1787
Imre Deakaba86892014-07-30 15:57:31 +03001788 if (sync)
Daniel Vetter4be73782014-01-17 14:39:48 +01001789 edp_panel_vdd_off_sync(intel_dp);
Imre Deakaba86892014-07-30 15:57:31 +03001790 else
1791 edp_panel_vdd_schedule_off(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001792}
1793
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001794static void edp_panel_on(struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -07001795{
Paulo Zanoni30add222012-10-26 19:05:45 -02001796 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001797 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packard99ea7122011-11-01 19:57:50 -07001798 u32 pp;
Jesse Barnes453c5422013-03-28 09:55:41 -07001799 u32 pp_ctrl_reg;
Jesse Barnes9934c132010-07-22 13:18:19 -07001800
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001801 lockdep_assert_held(&dev_priv->pps_mutex);
1802
Keith Packard97af61f572011-09-28 16:23:51 -07001803 if (!is_edp(intel_dp))
Keith Packardbd943152011-09-18 23:09:52 -07001804 return;
Keith Packard99ea7122011-11-01 19:57:50 -07001805
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001806 DRM_DEBUG_KMS("Turn eDP port %c panel power on\n",
1807 port_name(dp_to_dig_port(intel_dp)->port));
Keith Packard99ea7122011-11-01 19:57:50 -07001808
Ville Syrjäläe7a89ac2014-10-16 21:30:07 +03001809 if (WARN(edp_have_panel_power(intel_dp),
1810 "eDP port %c panel power already on\n",
1811 port_name(dp_to_dig_port(intel_dp)->port)))
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001812 return;
Jesse Barnes9934c132010-07-22 13:18:19 -07001813
Daniel Vetter4be73782014-01-17 14:39:48 +01001814 wait_panel_power_cycle(intel_dp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001815
Jani Nikulabf13e812013-09-06 07:40:05 +03001816 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001817 pp = ironlake_get_pp_control(intel_dp);
Keith Packard05ce1a42011-09-29 16:33:01 -07001818 if (IS_GEN5(dev)) {
1819 /* ILK workaround: disable reset around power sequence */
1820 pp &= ~PANEL_POWER_RESET;
Jani Nikulabf13e812013-09-06 07:40:05 +03001821 I915_WRITE(pp_ctrl_reg, pp);
1822 POSTING_READ(pp_ctrl_reg);
Keith Packard05ce1a42011-09-29 16:33:01 -07001823 }
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001824
Keith Packard1c0ae802011-09-19 13:59:29 -07001825 pp |= POWER_TARGET_ON;
Keith Packard99ea7122011-11-01 19:57:50 -07001826 if (!IS_GEN5(dev))
1827 pp |= PANEL_POWER_RESET;
1828
Jesse Barnes453c5422013-03-28 09:55:41 -07001829 I915_WRITE(pp_ctrl_reg, pp);
1830 POSTING_READ(pp_ctrl_reg);
Jesse Barnes9934c132010-07-22 13:18:19 -07001831
Daniel Vetter4be73782014-01-17 14:39:48 +01001832 wait_panel_on(intel_dp);
Paulo Zanonidce56b32013-12-19 14:29:40 -02001833 intel_dp->last_power_on = jiffies;
Jesse Barnes9934c132010-07-22 13:18:19 -07001834
Keith Packard05ce1a42011-09-29 16:33:01 -07001835 if (IS_GEN5(dev)) {
1836 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jani Nikulabf13e812013-09-06 07:40:05 +03001837 I915_WRITE(pp_ctrl_reg, pp);
1838 POSTING_READ(pp_ctrl_reg);
Keith Packard05ce1a42011-09-29 16:33:01 -07001839 }
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001840}
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001841
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001842void intel_edp_panel_on(struct intel_dp *intel_dp)
1843{
1844 if (!is_edp(intel_dp))
1845 return;
1846
1847 pps_lock(intel_dp);
1848 edp_panel_on(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03001849 pps_unlock(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001850}
1851
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001852
1853static void edp_panel_off(struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -07001854{
Imre Deak4e6e1a52014-03-27 17:45:11 +02001855 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1856 struct intel_encoder *intel_encoder = &intel_dig_port->base;
Paulo Zanoni30add222012-10-26 19:05:45 -02001857 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001858 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak4e6e1a52014-03-27 17:45:11 +02001859 enum intel_display_power_domain power_domain;
Keith Packard99ea7122011-11-01 19:57:50 -07001860 u32 pp;
Jesse Barnes453c5422013-03-28 09:55:41 -07001861 u32 pp_ctrl_reg;
Jesse Barnes9934c132010-07-22 13:18:19 -07001862
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001863 lockdep_assert_held(&dev_priv->pps_mutex);
1864
Keith Packard97af61f572011-09-28 16:23:51 -07001865 if (!is_edp(intel_dp))
1866 return;
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001867
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001868 DRM_DEBUG_KMS("Turn eDP port %c panel power off\n",
1869 port_name(dp_to_dig_port(intel_dp)->port));
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001870
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001871 WARN(!intel_dp->want_panel_vdd, "Need eDP port %c VDD to turn off panel\n",
1872 port_name(dp_to_dig_port(intel_dp)->port));
Jani Nikula24f3e092014-03-17 16:43:36 +02001873
Jesse Barnes453c5422013-03-28 09:55:41 -07001874 pp = ironlake_get_pp_control(intel_dp);
Daniel Vetter35a38552012-08-12 22:17:14 +02001875 /* We need to switch off panel power _and_ force vdd, for otherwise some
1876 * panels get very unhappy and cease to work. */
Patrik Jakobssonb3064152014-03-04 00:42:44 +01001877 pp &= ~(POWER_TARGET_ON | PANEL_POWER_RESET | EDP_FORCE_VDD |
1878 EDP_BLC_ENABLE);
Jesse Barnes453c5422013-03-28 09:55:41 -07001879
Jani Nikulabf13e812013-09-06 07:40:05 +03001880 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001881
Paulo Zanoni849e39f2014-03-07 20:05:20 -03001882 intel_dp->want_panel_vdd = false;
1883
Jesse Barnes453c5422013-03-28 09:55:41 -07001884 I915_WRITE(pp_ctrl_reg, pp);
1885 POSTING_READ(pp_ctrl_reg);
Jesse Barnes9934c132010-07-22 13:18:19 -07001886
Paulo Zanonidce56b32013-12-19 14:29:40 -02001887 intel_dp->last_power_cycle = jiffies;
Daniel Vetter4be73782014-01-17 14:39:48 +01001888 wait_panel_off(intel_dp);
Paulo Zanoni849e39f2014-03-07 20:05:20 -03001889
1890 /* We got a reference when we enabled the VDD. */
Imre Deak4e6e1a52014-03-27 17:45:11 +02001891 power_domain = intel_display_port_power_domain(intel_encoder);
1892 intel_display_power_put(dev_priv, power_domain);
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001893}
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001894
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001895void intel_edp_panel_off(struct intel_dp *intel_dp)
1896{
1897 if (!is_edp(intel_dp))
1898 return;
1899
1900 pps_lock(intel_dp);
1901 edp_panel_off(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03001902 pps_unlock(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001903}
1904
Jani Nikula1250d102014-08-12 17:11:39 +03001905/* Enable backlight in the panel power control. */
1906static void _intel_edp_backlight_on(struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001907{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02001908 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1909 struct drm_device *dev = intel_dig_port->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001910 struct drm_i915_private *dev_priv = dev->dev_private;
1911 u32 pp;
Jesse Barnes453c5422013-03-28 09:55:41 -07001912 u32 pp_ctrl_reg;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001913
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07001914 /*
1915 * If we enable the backlight right away following a panel power
1916 * on, we may see slight flicker as the panel syncs with the eDP
1917 * link. So delay a bit to make sure the image is solid before
1918 * allowing it to appear.
1919 */
Daniel Vetter4be73782014-01-17 14:39:48 +01001920 wait_backlight_on(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001921
Ville Syrjälä773538e82014-09-04 14:54:56 +03001922 pps_lock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001923
Jesse Barnes453c5422013-03-28 09:55:41 -07001924 pp = ironlake_get_pp_control(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001925 pp |= EDP_BLC_ENABLE;
Jesse Barnes453c5422013-03-28 09:55:41 -07001926
Jani Nikulabf13e812013-09-06 07:40:05 +03001927 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001928
1929 I915_WRITE(pp_ctrl_reg, pp);
1930 POSTING_READ(pp_ctrl_reg);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001931
Ville Syrjälä773538e82014-09-04 14:54:56 +03001932 pps_unlock(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001933}
1934
Jani Nikula1250d102014-08-12 17:11:39 +03001935/* Enable backlight PWM and backlight PP control. */
1936void intel_edp_backlight_on(struct intel_dp *intel_dp)
1937{
1938 if (!is_edp(intel_dp))
1939 return;
1940
1941 DRM_DEBUG_KMS("\n");
1942
1943 intel_panel_enable_backlight(intel_dp->attached_connector);
1944 _intel_edp_backlight_on(intel_dp);
1945}
1946
1947/* Disable backlight in the panel power control. */
1948static void _intel_edp_backlight_off(struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001949{
Paulo Zanoni30add222012-10-26 19:05:45 -02001950 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001951 struct drm_i915_private *dev_priv = dev->dev_private;
1952 u32 pp;
Jesse Barnes453c5422013-03-28 09:55:41 -07001953 u32 pp_ctrl_reg;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001954
Keith Packardf01eca22011-09-28 16:48:10 -07001955 if (!is_edp(intel_dp))
1956 return;
1957
Ville Syrjälä773538e82014-09-04 14:54:56 +03001958 pps_lock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001959
Jesse Barnes453c5422013-03-28 09:55:41 -07001960 pp = ironlake_get_pp_control(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001961 pp &= ~EDP_BLC_ENABLE;
Jesse Barnes453c5422013-03-28 09:55:41 -07001962
Jani Nikulabf13e812013-09-06 07:40:05 +03001963 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001964
1965 I915_WRITE(pp_ctrl_reg, pp);
1966 POSTING_READ(pp_ctrl_reg);
Jesse Barnesf7d23232014-03-31 11:13:56 -07001967
Ville Syrjälä773538e82014-09-04 14:54:56 +03001968 pps_unlock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001969
Paulo Zanonidce56b32013-12-19 14:29:40 -02001970 intel_dp->last_backlight_off = jiffies;
Jesse Barnesf7d23232014-03-31 11:13:56 -07001971 edp_wait_backlight_off(intel_dp);
Jani Nikula1250d102014-08-12 17:11:39 +03001972}
Jesse Barnesf7d23232014-03-31 11:13:56 -07001973
Jani Nikula1250d102014-08-12 17:11:39 +03001974/* Disable backlight PP control and backlight PWM. */
1975void intel_edp_backlight_off(struct intel_dp *intel_dp)
1976{
1977 if (!is_edp(intel_dp))
1978 return;
1979
1980 DRM_DEBUG_KMS("\n");
1981
1982 _intel_edp_backlight_off(intel_dp);
Jesse Barnesf7d23232014-03-31 11:13:56 -07001983 intel_panel_disable_backlight(intel_dp->attached_connector);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001984}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001985
Jani Nikula73580fb72014-08-12 17:11:41 +03001986/*
1987 * Hook for controlling the panel power control backlight through the bl_power
1988 * sysfs attribute. Take care to handle multiple calls.
1989 */
1990static void intel_edp_backlight_power(struct intel_connector *connector,
1991 bool enable)
1992{
1993 struct intel_dp *intel_dp = intel_attached_dp(&connector->base);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001994 bool is_enabled;
1995
Ville Syrjälä773538e82014-09-04 14:54:56 +03001996 pps_lock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001997 is_enabled = ironlake_get_pp_control(intel_dp) & EDP_BLC_ENABLE;
Ville Syrjälä773538e82014-09-04 14:54:56 +03001998 pps_unlock(intel_dp);
Jani Nikula73580fb72014-08-12 17:11:41 +03001999
2000 if (is_enabled == enable)
2001 return;
2002
Jani Nikula23ba9372014-08-27 14:08:43 +03002003 DRM_DEBUG_KMS("panel power control backlight %s\n",
2004 enable ? "enable" : "disable");
Jani Nikula73580fb72014-08-12 17:11:41 +03002005
2006 if (enable)
2007 _intel_edp_backlight_on(intel_dp);
2008 else
2009 _intel_edp_backlight_off(intel_dp);
2010}
2011
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002012static void ironlake_edp_pll_on(struct intel_dp *intel_dp)
Jesse Barnesd240f202010-08-13 15:43:26 -07002013{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002014 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2015 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
2016 struct drm_device *dev = crtc->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -07002017 struct drm_i915_private *dev_priv = dev->dev_private;
2018 u32 dpa_ctl;
2019
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002020 assert_pipe_disabled(dev_priv,
2021 to_intel_crtc(crtc)->pipe);
2022
Jesse Barnesd240f202010-08-13 15:43:26 -07002023 DRM_DEBUG_KMS("\n");
2024 dpa_ctl = I915_READ(DP_A);
Daniel Vetter07679352012-09-06 22:15:42 +02002025 WARN(dpa_ctl & DP_PLL_ENABLE, "dp pll on, should be off\n");
2026 WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
2027
2028 /* We don't adjust intel_dp->DP while tearing down the link, to
2029 * facilitate link retraining (e.g. after hotplug). Hence clear all
2030 * enable bits here to ensure that we don't enable too much. */
2031 intel_dp->DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
2032 intel_dp->DP |= DP_PLL_ENABLE;
2033 I915_WRITE(DP_A, intel_dp->DP);
Jesse Barnes298b0b32010-10-07 16:01:24 -07002034 POSTING_READ(DP_A);
2035 udelay(200);
Jesse Barnesd240f202010-08-13 15:43:26 -07002036}
2037
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002038static void ironlake_edp_pll_off(struct intel_dp *intel_dp)
Jesse Barnesd240f202010-08-13 15:43:26 -07002039{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002040 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2041 struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
2042 struct drm_device *dev = crtc->dev;
Jesse Barnesd240f202010-08-13 15:43:26 -07002043 struct drm_i915_private *dev_priv = dev->dev_private;
2044 u32 dpa_ctl;
2045
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002046 assert_pipe_disabled(dev_priv,
2047 to_intel_crtc(crtc)->pipe);
2048
Jesse Barnesd240f202010-08-13 15:43:26 -07002049 dpa_ctl = I915_READ(DP_A);
Daniel Vetter07679352012-09-06 22:15:42 +02002050 WARN((dpa_ctl & DP_PLL_ENABLE) == 0,
2051 "dp pll off, should be on\n");
2052 WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
2053
2054 /* We can't rely on the value tracked for the DP register in
2055 * intel_dp->DP because link_down must not change that (otherwise link
2056 * re-training will fail. */
Jesse Barnes298b0b32010-10-07 16:01:24 -07002057 dpa_ctl &= ~DP_PLL_ENABLE;
Jesse Barnesd240f202010-08-13 15:43:26 -07002058 I915_WRITE(DP_A, dpa_ctl);
Chris Wilson1af5fa12010-09-08 21:07:28 +01002059 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -07002060 udelay(200);
2061}
2062
Jesse Barnesc7ad3812011-07-07 11:11:03 -07002063/* If the sink supports it, try to set the power state appropriately */
Paulo Zanonic19b0662012-10-15 15:51:41 -03002064void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
Jesse Barnesc7ad3812011-07-07 11:11:03 -07002065{
2066 int ret, i;
2067
2068 /* Should have a valid DPCD by this point */
2069 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
2070 return;
2071
2072 if (mode != DRM_MODE_DPMS_ON) {
Jani Nikula9d1a1032014-03-14 16:51:15 +02002073 ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
2074 DP_SET_POWER_D3);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07002075 } else {
2076 /*
2077 * When turning on, we need to retry for 1ms to give the sink
2078 * time to wake up.
2079 */
2080 for (i = 0; i < 3; i++) {
Jani Nikula9d1a1032014-03-14 16:51:15 +02002081 ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
2082 DP_SET_POWER_D0);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07002083 if (ret == 1)
2084 break;
2085 msleep(1);
2086 }
2087 }
Jani Nikulaf9cac722014-09-02 16:33:52 +03002088
2089 if (ret != 1)
2090 DRM_DEBUG_KMS("failed to %s sink power state\n",
2091 mode == DRM_MODE_DPMS_ON ? "enable" : "disable");
Jesse Barnesc7ad3812011-07-07 11:11:03 -07002092}
2093
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002094static bool intel_dp_get_hw_state(struct intel_encoder *encoder,
2095 enum pipe *pipe)
Jesse Barnesd240f202010-08-13 15:43:26 -07002096{
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002097 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deakbc7d38a2013-05-16 14:40:36 +03002098 enum port port = dp_to_dig_port(intel_dp)->port;
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002099 struct drm_device *dev = encoder->base.dev;
2100 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak6d129be2014-03-05 16:20:54 +02002101 enum intel_display_power_domain power_domain;
2102 u32 tmp;
2103
2104 power_domain = intel_display_port_power_domain(encoder);
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002105 if (!intel_display_power_is_enabled(dev_priv, power_domain))
Imre Deak6d129be2014-03-05 16:20:54 +02002106 return false;
2107
2108 tmp = I915_READ(intel_dp->output_reg);
Jesse Barnesd240f202010-08-13 15:43:26 -07002109
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002110 if (!(tmp & DP_PORT_EN))
2111 return false;
2112
Imre Deakbc7d38a2013-05-16 14:40:36 +03002113 if (port == PORT_A && IS_GEN7(dev) && !IS_VALLEYVIEW(dev)) {
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002114 *pipe = PORT_TO_PIPE_CPT(tmp);
Ville Syrjälä71485e02014-04-09 13:28:55 +03002115 } else if (IS_CHERRYVIEW(dev)) {
2116 *pipe = DP_PORT_TO_PIPE_CHV(tmp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03002117 } else if (!HAS_PCH_CPT(dev) || port == PORT_A) {
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002118 *pipe = PORT_TO_PIPE(tmp);
2119 } else {
2120 u32 trans_sel;
2121 u32 trans_dp;
2122 int i;
2123
2124 switch (intel_dp->output_reg) {
2125 case PCH_DP_B:
2126 trans_sel = TRANS_DP_PORT_SEL_B;
2127 break;
2128 case PCH_DP_C:
2129 trans_sel = TRANS_DP_PORT_SEL_C;
2130 break;
2131 case PCH_DP_D:
2132 trans_sel = TRANS_DP_PORT_SEL_D;
2133 break;
2134 default:
2135 return true;
2136 }
2137
Damien Lespiau055e3932014-08-18 13:49:10 +01002138 for_each_pipe(dev_priv, i) {
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002139 trans_dp = I915_READ(TRANS_DP_CTL(i));
2140 if ((trans_dp & TRANS_DP_PORT_SEL_MASK) == trans_sel) {
2141 *pipe = i;
2142 return true;
2143 }
2144 }
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002145
Daniel Vetter4a0833e2012-10-26 10:58:11 +02002146 DRM_DEBUG_KMS("No pipe for dp port 0x%x found\n",
2147 intel_dp->output_reg);
2148 }
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002149
2150 return true;
2151}
2152
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002153static void intel_dp_get_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002154 struct intel_crtc_state *pipe_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002155{
2156 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002157 u32 tmp, flags = 0;
Xiong Zhang63000ef2013-06-28 12:59:06 +08002158 struct drm_device *dev = encoder->base.dev;
2159 struct drm_i915_private *dev_priv = dev->dev_private;
2160 enum port port = dp_to_dig_port(intel_dp)->port;
2161 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
Ville Syrjälä18442d02013-09-13 16:00:08 +03002162 int dotclock;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002163
Daniel Vetter9ed109a2014-04-24 23:54:52 +02002164 tmp = I915_READ(intel_dp->output_reg);
2165 if (tmp & DP_AUDIO_OUTPUT_ENABLE)
2166 pipe_config->has_audio = true;
2167
Xiong Zhang63000ef2013-06-28 12:59:06 +08002168 if ((port == PORT_A) || !HAS_PCH_CPT(dev)) {
Xiong Zhang63000ef2013-06-28 12:59:06 +08002169 if (tmp & DP_SYNC_HS_HIGH)
2170 flags |= DRM_MODE_FLAG_PHSYNC;
2171 else
2172 flags |= DRM_MODE_FLAG_NHSYNC;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002173
Xiong Zhang63000ef2013-06-28 12:59:06 +08002174 if (tmp & DP_SYNC_VS_HIGH)
2175 flags |= DRM_MODE_FLAG_PVSYNC;
2176 else
2177 flags |= DRM_MODE_FLAG_NVSYNC;
2178 } else {
2179 tmp = I915_READ(TRANS_DP_CTL(crtc->pipe));
2180 if (tmp & TRANS_DP_HSYNC_ACTIVE_HIGH)
2181 flags |= DRM_MODE_FLAG_PHSYNC;
2182 else
2183 flags |= DRM_MODE_FLAG_NHSYNC;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002184
Xiong Zhang63000ef2013-06-28 12:59:06 +08002185 if (tmp & TRANS_DP_VSYNC_ACTIVE_HIGH)
2186 flags |= DRM_MODE_FLAG_PVSYNC;
2187 else
2188 flags |= DRM_MODE_FLAG_NVSYNC;
2189 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002190
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002191 pipe_config->base.adjusted_mode.flags |= flags;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03002192
Ville Syrjälä8c875fc2014-09-12 15:46:29 +03002193 if (!HAS_PCH_SPLIT(dev) && !IS_VALLEYVIEW(dev) &&
2194 tmp & DP_COLOR_RANGE_16_235)
2195 pipe_config->limited_color_range = true;
2196
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03002197 pipe_config->has_dp_encoder = true;
2198
2199 intel_dp_get_m_n(crtc, pipe_config);
2200
Ville Syrjälä18442d02013-09-13 16:00:08 +03002201 if (port == PORT_A) {
Jesse Barnesf1f644d2013-06-27 00:39:25 +03002202 if ((I915_READ(DP_A) & DP_PLL_FREQ_MASK) == DP_PLL_FREQ_160MHZ)
2203 pipe_config->port_clock = 162000;
2204 else
2205 pipe_config->port_clock = 270000;
2206 }
Ville Syrjälä18442d02013-09-13 16:00:08 +03002207
2208 dotclock = intel_dotclock_calculate(pipe_config->port_clock,
2209 &pipe_config->dp_m_n);
2210
2211 if (HAS_PCH_SPLIT(dev_priv->dev) && port != PORT_A)
2212 ironlake_check_encoder_dotclock(pipe_config, dotclock);
2213
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002214 pipe_config->base.adjusted_mode.crtc_clock = dotclock;
Daniel Vetter7f16e5c2013-11-04 16:28:47 +01002215
Jani Nikulac6cd2ee2013-10-21 10:52:07 +03002216 if (is_edp(intel_dp) && dev_priv->vbt.edp_bpp &&
2217 pipe_config->pipe_bpp > dev_priv->vbt.edp_bpp) {
2218 /*
2219 * This is a big fat ugly hack.
2220 *
2221 * Some machines in UEFI boot mode provide us a VBT that has 18
2222 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
2223 * unknown we fail to light up. Yet the same BIOS boots up with
2224 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
2225 * max, not what it tells us to use.
2226 *
2227 * Note: This will still be broken if the eDP panel is not lit
2228 * up by the BIOS, and thus we can't get the mode at module
2229 * load.
2230 */
2231 DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
2232 pipe_config->pipe_bpp, dev_priv->vbt.edp_bpp);
2233 dev_priv->vbt.edp_bpp = pipe_config->pipe_bpp;
2234 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002235}
2236
Daniel Vettere8cb4552012-07-01 13:05:48 +02002237static void intel_disable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07002238{
Daniel Vettere8cb4552012-07-01 13:05:48 +02002239 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deak982a3862013-05-23 19:39:40 +03002240 struct drm_device *dev = encoder->base.dev;
Jani Nikula495a5bb2014-10-27 16:26:55 +02002241 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
2242
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002243 if (crtc->config->has_audio)
Jani Nikula495a5bb2014-10-27 16:26:55 +02002244 intel_audio_codec_disable(encoder);
Daniel Vetter6cb49832012-05-20 17:14:50 +02002245
Rodrigo Vivib32c6f42014-11-20 03:44:37 -08002246 if (HAS_PSR(dev) && !HAS_DDI(dev))
2247 intel_psr_disable(intel_dp);
2248
Daniel Vetter6cb49832012-05-20 17:14:50 +02002249 /* Make sure the panel is off before trying to change the mode. But also
2250 * ensure that we have vdd while we switch off the panel. */
Jani Nikula24f3e092014-03-17 16:43:36 +02002251 intel_edp_panel_vdd_on(intel_dp);
Daniel Vetter4be73782014-01-17 14:39:48 +01002252 intel_edp_backlight_off(intel_dp);
Jani Nikulafdbc3b12013-11-12 17:10:13 +02002253 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
Daniel Vetter4be73782014-01-17 14:39:48 +01002254 intel_edp_panel_off(intel_dp);
Daniel Vetter37398502012-09-06 22:15:44 +02002255
Ville Syrjälä08aff3f2014-08-18 22:16:09 +03002256 /* disable the port before the pipe on g4x */
2257 if (INTEL_INFO(dev)->gen < 5)
Daniel Vetter37398502012-09-06 22:15:44 +02002258 intel_dp_link_down(intel_dp);
Jesse Barnesd240f202010-08-13 15:43:26 -07002259}
2260
Ville Syrjälä08aff3f2014-08-18 22:16:09 +03002261static void ilk_post_disable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07002262{
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002263 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deak982a3862013-05-23 19:39:40 +03002264 enum port port = dp_to_dig_port(intel_dp)->port;
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002265
Ville Syrjälä49277c32014-03-31 18:21:26 +03002266 intel_dp_link_down(intel_dp);
Ville Syrjälä08aff3f2014-08-18 22:16:09 +03002267 if (port == PORT_A)
2268 ironlake_edp_pll_off(intel_dp);
Ville Syrjälä49277c32014-03-31 18:21:26 +03002269}
2270
2271static void vlv_post_disable_dp(struct intel_encoder *encoder)
2272{
2273 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2274
2275 intel_dp_link_down(intel_dp);
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002276}
2277
Ville Syrjälä580d3812014-04-09 13:29:00 +03002278static void chv_post_disable_dp(struct intel_encoder *encoder)
2279{
2280 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2281 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
2282 struct drm_device *dev = encoder->base.dev;
2283 struct drm_i915_private *dev_priv = dev->dev_private;
2284 struct intel_crtc *intel_crtc =
2285 to_intel_crtc(encoder->base.crtc);
2286 enum dpio_channel ch = vlv_dport_to_channel(dport);
2287 enum pipe pipe = intel_crtc->pipe;
2288 u32 val;
2289
2290 intel_dp_link_down(intel_dp);
2291
2292 mutex_lock(&dev_priv->dpio_lock);
2293
2294 /* Propagate soft reset to data lane reset */
Ville Syrjälä97fd4d52014-04-09 13:29:02 +03002295 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW1(ch));
Ville Syrjäläd2152b22014-04-28 14:15:24 +03002296 val |= CHV_PCS_REQ_SOFTRESET_EN;
Ville Syrjälä97fd4d52014-04-09 13:29:02 +03002297 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW1(ch), val);
Ville Syrjäläd2152b22014-04-28 14:15:24 +03002298
Ville Syrjälä97fd4d52014-04-09 13:29:02 +03002299 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW1(ch));
2300 val |= CHV_PCS_REQ_SOFTRESET_EN;
2301 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW1(ch), val);
2302
2303 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW0(ch));
Ville Syrjälä580d3812014-04-09 13:29:00 +03002304 val &= ~(DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
Ville Syrjälä97fd4d52014-04-09 13:29:02 +03002305 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW0(ch), val);
2306
2307 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW0(ch));
2308 val &= ~(DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
2309 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW0(ch), val);
Ville Syrjälä580d3812014-04-09 13:29:00 +03002310
2311 mutex_unlock(&dev_priv->dpio_lock);
2312}
2313
Ville Syrjälä7b13b582014-08-18 22:16:08 +03002314static void
2315_intel_dp_set_link_train(struct intel_dp *intel_dp,
2316 uint32_t *DP,
2317 uint8_t dp_train_pat)
2318{
2319 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2320 struct drm_device *dev = intel_dig_port->base.base.dev;
2321 struct drm_i915_private *dev_priv = dev->dev_private;
2322 enum port port = intel_dig_port->port;
2323
2324 if (HAS_DDI(dev)) {
2325 uint32_t temp = I915_READ(DP_TP_CTL(port));
2326
2327 if (dp_train_pat & DP_LINK_SCRAMBLING_DISABLE)
2328 temp |= DP_TP_CTL_SCRAMBLE_DISABLE;
2329 else
2330 temp &= ~DP_TP_CTL_SCRAMBLE_DISABLE;
2331
2332 temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
2333 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2334 case DP_TRAINING_PATTERN_DISABLE:
2335 temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
2336
2337 break;
2338 case DP_TRAINING_PATTERN_1:
2339 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
2340 break;
2341 case DP_TRAINING_PATTERN_2:
2342 temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
2343 break;
2344 case DP_TRAINING_PATTERN_3:
2345 temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
2346 break;
2347 }
2348 I915_WRITE(DP_TP_CTL(port), temp);
2349
2350 } else if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || port != PORT_A)) {
2351 *DP &= ~DP_LINK_TRAIN_MASK_CPT;
2352
2353 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2354 case DP_TRAINING_PATTERN_DISABLE:
2355 *DP |= DP_LINK_TRAIN_OFF_CPT;
2356 break;
2357 case DP_TRAINING_PATTERN_1:
2358 *DP |= DP_LINK_TRAIN_PAT_1_CPT;
2359 break;
2360 case DP_TRAINING_PATTERN_2:
2361 *DP |= DP_LINK_TRAIN_PAT_2_CPT;
2362 break;
2363 case DP_TRAINING_PATTERN_3:
2364 DRM_ERROR("DP training pattern 3 not supported\n");
2365 *DP |= DP_LINK_TRAIN_PAT_2_CPT;
2366 break;
2367 }
2368
2369 } else {
2370 if (IS_CHERRYVIEW(dev))
2371 *DP &= ~DP_LINK_TRAIN_MASK_CHV;
2372 else
2373 *DP &= ~DP_LINK_TRAIN_MASK;
2374
2375 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2376 case DP_TRAINING_PATTERN_DISABLE:
2377 *DP |= DP_LINK_TRAIN_OFF;
2378 break;
2379 case DP_TRAINING_PATTERN_1:
2380 *DP |= DP_LINK_TRAIN_PAT_1;
2381 break;
2382 case DP_TRAINING_PATTERN_2:
2383 *DP |= DP_LINK_TRAIN_PAT_2;
2384 break;
2385 case DP_TRAINING_PATTERN_3:
2386 if (IS_CHERRYVIEW(dev)) {
2387 *DP |= DP_LINK_TRAIN_PAT_3_CHV;
2388 } else {
2389 DRM_ERROR("DP training pattern 3 not supported\n");
2390 *DP |= DP_LINK_TRAIN_PAT_2;
2391 }
2392 break;
2393 }
2394 }
2395}
2396
2397static void intel_dp_enable_port(struct intel_dp *intel_dp)
2398{
2399 struct drm_device *dev = intel_dp_to_dev(intel_dp);
2400 struct drm_i915_private *dev_priv = dev->dev_private;
2401
Ville Syrjälä7b13b582014-08-18 22:16:08 +03002402 /* enable with pattern 1 (as per spec) */
2403 _intel_dp_set_link_train(intel_dp, &intel_dp->DP,
2404 DP_TRAINING_PATTERN_1);
2405
2406 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
2407 POSTING_READ(intel_dp->output_reg);
Ville Syrjälä7b713f52014-10-16 21:27:35 +03002408
2409 /*
2410 * Magic for VLV/CHV. We _must_ first set up the register
2411 * without actually enabling the port, and then do another
2412 * write to enable the port. Otherwise link training will
2413 * fail when the power sequencer is freshly used for this port.
2414 */
2415 intel_dp->DP |= DP_PORT_EN;
2416
2417 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
2418 POSTING_READ(intel_dp->output_reg);
Ville Syrjälä7b13b582014-08-18 22:16:08 +03002419}
2420
Daniel Vettere8cb4552012-07-01 13:05:48 +02002421static void intel_enable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07002422{
Daniel Vettere8cb4552012-07-01 13:05:48 +02002423 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2424 struct drm_device *dev = encoder->base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002425 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulac1dec792014-10-27 16:26:56 +02002426 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +01002427 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002428
Daniel Vetter0c33d8d2012-09-06 22:15:43 +02002429 if (WARN_ON(dp_reg & DP_PORT_EN))
2430 return;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002431
Ville Syrjälä093e3f12014-10-16 21:27:33 +03002432 pps_lock(intel_dp);
2433
2434 if (IS_VALLEYVIEW(dev))
2435 vlv_init_panel_power_sequencer(intel_dp);
2436
Ville Syrjälä7b13b582014-08-18 22:16:08 +03002437 intel_dp_enable_port(intel_dp);
Ville Syrjälä093e3f12014-10-16 21:27:33 +03002438
2439 edp_panel_vdd_on(intel_dp);
2440 edp_panel_on(intel_dp);
2441 edp_panel_vdd_off(intel_dp, true);
2442
2443 pps_unlock(intel_dp);
2444
Ville Syrjälä61234fa2014-10-16 21:27:34 +03002445 if (IS_VALLEYVIEW(dev))
2446 vlv_wait_port_ready(dev_priv, dp_to_dig_port(intel_dp));
2447
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002448 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
2449 intel_dp_start_link_train(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002450 intel_dp_complete_link_train(intel_dp);
Imre Deak3ab9c632013-05-03 12:57:41 +03002451 intel_dp_stop_link_train(intel_dp);
Jani Nikulac1dec792014-10-27 16:26:56 +02002452
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002453 if (crtc->config->has_audio) {
Jani Nikulac1dec792014-10-27 16:26:56 +02002454 DRM_DEBUG_DRIVER("Enabling DP audio on pipe %c\n",
2455 pipe_name(crtc->pipe));
2456 intel_audio_codec_enable(encoder);
2457 }
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002458}
Jesse Barnes89b667f2013-04-18 14:51:36 -07002459
Jani Nikulaecff4f32013-09-06 07:38:29 +03002460static void g4x_enable_dp(struct intel_encoder *encoder)
2461{
Jani Nikula828f5c62013-09-05 16:44:45 +03002462 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2463
Jani Nikulaecff4f32013-09-06 07:38:29 +03002464 intel_enable_dp(encoder);
Daniel Vetter4be73782014-01-17 14:39:48 +01002465 intel_edp_backlight_on(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002466}
Jesse Barnes89b667f2013-04-18 14:51:36 -07002467
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002468static void vlv_enable_dp(struct intel_encoder *encoder)
2469{
Jani Nikula828f5c62013-09-05 16:44:45 +03002470 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2471
Daniel Vetter4be73782014-01-17 14:39:48 +01002472 intel_edp_backlight_on(intel_dp);
Rodrigo Vivib32c6f42014-11-20 03:44:37 -08002473 intel_psr_enable(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002474}
2475
Jani Nikulaecff4f32013-09-06 07:38:29 +03002476static void g4x_pre_enable_dp(struct intel_encoder *encoder)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002477{
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002478 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deakbc7d38a2013-05-16 14:40:36 +03002479 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002480
Daniel Vetter8ac33ed2014-04-24 23:54:54 +02002481 intel_dp_prepare(encoder);
2482
Daniel Vetterd41f1ef2014-04-24 23:54:53 +02002483 /* Only ilk+ has port A */
2484 if (dport->port == PORT_A) {
2485 ironlake_set_pll_cpu_edp(intel_dp);
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002486 ironlake_edp_pll_on(intel_dp);
Daniel Vetterd41f1ef2014-04-24 23:54:53 +02002487 }
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002488}
2489
Ville Syrjälä83b84592014-10-16 21:29:51 +03002490static void vlv_detach_power_sequencer(struct intel_dp *intel_dp)
2491{
2492 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2493 struct drm_i915_private *dev_priv = intel_dig_port->base.base.dev->dev_private;
2494 enum pipe pipe = intel_dp->pps_pipe;
2495 int pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
2496
2497 edp_panel_vdd_off_sync(intel_dp);
2498
2499 /*
2500 * VLV seems to get confused when multiple power seqeuencers
2501 * have the same port selected (even if only one has power/vdd
2502 * enabled). The failure manifests as vlv_wait_port_ready() failing
2503 * CHV on the other hand doesn't seem to mind having the same port
2504 * selected in multiple power seqeuencers, but let's clear the
2505 * port select always when logically disconnecting a power sequencer
2506 * from a port.
2507 */
2508 DRM_DEBUG_KMS("detaching pipe %c power sequencer from port %c\n",
2509 pipe_name(pipe), port_name(intel_dig_port->port));
2510 I915_WRITE(pp_on_reg, 0);
2511 POSTING_READ(pp_on_reg);
2512
2513 intel_dp->pps_pipe = INVALID_PIPE;
2514}
2515
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002516static void vlv_steal_power_sequencer(struct drm_device *dev,
2517 enum pipe pipe)
2518{
2519 struct drm_i915_private *dev_priv = dev->dev_private;
2520 struct intel_encoder *encoder;
2521
2522 lockdep_assert_held(&dev_priv->pps_mutex);
2523
Ville Syrjäläac3c12e2014-10-16 21:29:56 +03002524 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
2525 return;
2526
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002527 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
2528 base.head) {
2529 struct intel_dp *intel_dp;
Ville Syrjälä773538e82014-09-04 14:54:56 +03002530 enum port port;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002531
2532 if (encoder->type != INTEL_OUTPUT_EDP)
2533 continue;
2534
2535 intel_dp = enc_to_intel_dp(&encoder->base);
Ville Syrjälä773538e82014-09-04 14:54:56 +03002536 port = dp_to_dig_port(intel_dp)->port;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002537
2538 if (intel_dp->pps_pipe != pipe)
2539 continue;
2540
2541 DRM_DEBUG_KMS("stealing pipe %c power sequencer from port %c\n",
Ville Syrjälä773538e82014-09-04 14:54:56 +03002542 pipe_name(pipe), port_name(port));
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002543
Ville Syrjälä034e43c2014-10-16 21:27:28 +03002544 WARN(encoder->connectors_active,
2545 "stealing pipe %c power sequencer from active eDP port %c\n",
2546 pipe_name(pipe), port_name(port));
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002547
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002548 /* make sure vdd is off before we steal it */
Ville Syrjälä83b84592014-10-16 21:29:51 +03002549 vlv_detach_power_sequencer(intel_dp);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002550 }
2551}
2552
2553static void vlv_init_panel_power_sequencer(struct intel_dp *intel_dp)
2554{
2555 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2556 struct intel_encoder *encoder = &intel_dig_port->base;
2557 struct drm_device *dev = encoder->base.dev;
2558 struct drm_i915_private *dev_priv = dev->dev_private;
2559 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002560
2561 lockdep_assert_held(&dev_priv->pps_mutex);
2562
Ville Syrjälä093e3f12014-10-16 21:27:33 +03002563 if (!is_edp(intel_dp))
2564 return;
2565
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002566 if (intel_dp->pps_pipe == crtc->pipe)
2567 return;
2568
2569 /*
2570 * If another power sequencer was being used on this
2571 * port previously make sure to turn off vdd there while
2572 * we still have control of it.
2573 */
2574 if (intel_dp->pps_pipe != INVALID_PIPE)
Ville Syrjälä83b84592014-10-16 21:29:51 +03002575 vlv_detach_power_sequencer(intel_dp);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002576
2577 /*
2578 * We may be stealing the power
2579 * sequencer from another port.
2580 */
2581 vlv_steal_power_sequencer(dev, crtc->pipe);
2582
2583 /* now it's all ours */
2584 intel_dp->pps_pipe = crtc->pipe;
2585
2586 DRM_DEBUG_KMS("initializing pipe %c power sequencer for port %c\n",
2587 pipe_name(intel_dp->pps_pipe), port_name(intel_dig_port->port));
2588
2589 /* init power sequencer on this pipe and port */
Ville Syrjälä36b5f422014-10-16 21:27:30 +03002590 intel_dp_init_panel_power_sequencer(dev, intel_dp);
2591 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002592}
2593
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002594static void vlv_pre_enable_dp(struct intel_encoder *encoder)
2595{
2596 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2597 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
Jesse Barnesb2634012013-03-28 09:55:40 -07002598 struct drm_device *dev = encoder->base.dev;
Jesse Barnes89b667f2013-04-18 14:51:36 -07002599 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002600 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08002601 enum dpio_channel port = vlv_dport_to_channel(dport);
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002602 int pipe = intel_crtc->pipe;
2603 u32 val;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002604
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002605 mutex_lock(&dev_priv->dpio_lock);
Jesse Barnes89b667f2013-04-18 14:51:36 -07002606
Chon Ming Leeab3c7592013-11-07 10:43:30 +08002607 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW8(port));
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002608 val = 0;
2609 if (pipe)
2610 val |= (1<<21);
2611 else
2612 val &= ~(1<<21);
2613 val |= 0x001000c4;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08002614 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW8(port), val);
2615 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW14(port), 0x00760018);
2616 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW23(port), 0x00400888);
Jesse Barnes89b667f2013-04-18 14:51:36 -07002617
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002618 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes89b667f2013-04-18 14:51:36 -07002619
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002620 intel_enable_dp(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07002621}
2622
Jani Nikulaecff4f32013-09-06 07:38:29 +03002623static void vlv_dp_pre_pll_enable(struct intel_encoder *encoder)
Jesse Barnes89b667f2013-04-18 14:51:36 -07002624{
2625 struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
2626 struct drm_device *dev = encoder->base.dev;
2627 struct drm_i915_private *dev_priv = dev->dev_private;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08002628 struct intel_crtc *intel_crtc =
2629 to_intel_crtc(encoder->base.crtc);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08002630 enum dpio_channel port = vlv_dport_to_channel(dport);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08002631 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07002632
Daniel Vetter8ac33ed2014-04-24 23:54:54 +02002633 intel_dp_prepare(encoder);
2634
Jesse Barnes89b667f2013-04-18 14:51:36 -07002635 /* Program Tx lane resets to default */
Chris Wilson0980a602013-07-26 19:57:35 +01002636 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08002637 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW0(port),
Jesse Barnes89b667f2013-04-18 14:51:36 -07002638 DPIO_PCS_TX_LANE2_RESET |
2639 DPIO_PCS_TX_LANE1_RESET);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08002640 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW1(port),
Jesse Barnes89b667f2013-04-18 14:51:36 -07002641 DPIO_PCS_CLK_CRI_RXEB_EIOS_EN |
2642 DPIO_PCS_CLK_CRI_RXDIGFILTSG_EN |
2643 (1<<DPIO_PCS_CLK_DATAWIDTH_SHIFT) |
2644 DPIO_PCS_CLK_SOFT_RESET);
2645
2646 /* Fix up inter-pair skew failure */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08002647 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW12(port), 0x00750f00);
2648 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW11(port), 0x00001500);
2649 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW14(port), 0x40400000);
Chris Wilson0980a602013-07-26 19:57:35 +01002650 mutex_unlock(&dev_priv->dpio_lock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002651}
2652
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002653static void chv_pre_enable_dp(struct intel_encoder *encoder)
2654{
2655 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2656 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
2657 struct drm_device *dev = encoder->base.dev;
2658 struct drm_i915_private *dev_priv = dev->dev_private;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002659 struct intel_crtc *intel_crtc =
2660 to_intel_crtc(encoder->base.crtc);
2661 enum dpio_channel ch = vlv_dport_to_channel(dport);
2662 int pipe = intel_crtc->pipe;
2663 int data, i;
Ville Syrjälä949c1d42014-04-09 13:28:58 +03002664 u32 val;
2665
2666 mutex_lock(&dev_priv->dpio_lock);
2667
Ville Syrjälä570e2a72014-08-18 14:42:46 +03002668 /* allow hardware to manage TX FIFO reset source */
2669 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW11(ch));
2670 val &= ~DPIO_LANEDESKEW_STRAP_OVRD;
2671 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW11(ch), val);
2672
2673 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW11(ch));
2674 val &= ~DPIO_LANEDESKEW_STRAP_OVRD;
2675 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW11(ch), val);
2676
Ville Syrjälä949c1d42014-04-09 13:28:58 +03002677 /* Deassert soft data lane reset*/
Ville Syrjälä97fd4d52014-04-09 13:29:02 +03002678 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW1(ch));
Ville Syrjäläd2152b22014-04-28 14:15:24 +03002679 val |= CHV_PCS_REQ_SOFTRESET_EN;
Ville Syrjälä97fd4d52014-04-09 13:29:02 +03002680 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW1(ch), val);
Ville Syrjäläd2152b22014-04-28 14:15:24 +03002681
Ville Syrjälä97fd4d52014-04-09 13:29:02 +03002682 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW1(ch));
2683 val |= CHV_PCS_REQ_SOFTRESET_EN;
2684 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW1(ch), val);
2685
2686 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW0(ch));
Ville Syrjälä949c1d42014-04-09 13:28:58 +03002687 val |= (DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
Ville Syrjälä97fd4d52014-04-09 13:29:02 +03002688 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW0(ch), val);
2689
2690 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW0(ch));
2691 val |= (DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
2692 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW0(ch), val);
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002693
2694 /* Program Tx lane latency optimal setting*/
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002695 for (i = 0; i < 4; i++) {
2696 /* Set the latency optimal bit */
2697 data = (i == 1) ? 0x0 : 0x6;
2698 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW11(ch, i),
2699 data << DPIO_FRC_LATENCY_SHFIT);
2700
2701 /* Set the upar bit */
2702 data = (i == 1) ? 0x0 : 0x1;
2703 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW14(ch, i),
2704 data << DPIO_UPAR_SHIFT);
2705 }
2706
2707 /* Data lane stagger programming */
2708 /* FIXME: Fix up value only after power analysis */
2709
2710 mutex_unlock(&dev_priv->dpio_lock);
2711
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002712 intel_enable_dp(encoder);
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002713}
2714
Ville Syrjälä9197c882014-04-09 13:29:05 +03002715static void chv_dp_pre_pll_enable(struct intel_encoder *encoder)
2716{
2717 struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
2718 struct drm_device *dev = encoder->base.dev;
2719 struct drm_i915_private *dev_priv = dev->dev_private;
2720 struct intel_crtc *intel_crtc =
2721 to_intel_crtc(encoder->base.crtc);
2722 enum dpio_channel ch = vlv_dport_to_channel(dport);
2723 enum pipe pipe = intel_crtc->pipe;
2724 u32 val;
2725
Ville Syrjälä625695f2014-06-28 02:04:02 +03002726 intel_dp_prepare(encoder);
2727
Ville Syrjälä9197c882014-04-09 13:29:05 +03002728 mutex_lock(&dev_priv->dpio_lock);
2729
Ville Syrjäläb9e5ac32014-05-27 16:30:18 +03002730 /* program left/right clock distribution */
2731 if (pipe != PIPE_B) {
2732 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
2733 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
2734 if (ch == DPIO_CH0)
2735 val |= CHV_BUFLEFTENA1_FORCE;
2736 if (ch == DPIO_CH1)
2737 val |= CHV_BUFRIGHTENA1_FORCE;
2738 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
2739 } else {
2740 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
2741 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
2742 if (ch == DPIO_CH0)
2743 val |= CHV_BUFLEFTENA2_FORCE;
2744 if (ch == DPIO_CH1)
2745 val |= CHV_BUFRIGHTENA2_FORCE;
2746 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
2747 }
2748
Ville Syrjälä9197c882014-04-09 13:29:05 +03002749 /* program clock channel usage */
2750 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW8(ch));
2751 val |= CHV_PCS_USEDCLKCHANNEL_OVRRIDE;
2752 if (pipe != PIPE_B)
2753 val &= ~CHV_PCS_USEDCLKCHANNEL;
2754 else
2755 val |= CHV_PCS_USEDCLKCHANNEL;
2756 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW8(ch), val);
2757
2758 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW8(ch));
2759 val |= CHV_PCS_USEDCLKCHANNEL_OVRRIDE;
2760 if (pipe != PIPE_B)
2761 val &= ~CHV_PCS_USEDCLKCHANNEL;
2762 else
2763 val |= CHV_PCS_USEDCLKCHANNEL;
2764 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW8(ch), val);
2765
2766 /*
2767 * This a a bit weird since generally CL
2768 * matches the pipe, but here we need to
2769 * pick the CL based on the port.
2770 */
2771 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW19(ch));
2772 if (pipe != PIPE_B)
2773 val &= ~CHV_CMN_USEDCLKCHANNEL;
2774 else
2775 val |= CHV_CMN_USEDCLKCHANNEL;
2776 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW19(ch), val);
2777
2778 mutex_unlock(&dev_priv->dpio_lock);
2779}
2780
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002781/*
Jesse Barnesdf0c2372011-07-07 11:11:02 -07002782 * Native read with retry for link status and receiver capability reads for
2783 * cases where the sink may still be asleep.
Jani Nikula9d1a1032014-03-14 16:51:15 +02002784 *
2785 * Sinks are *supposed* to come up within 1ms from an off state, but we're also
2786 * supposed to retry 3 times per the spec.
Jesse Barnesdf0c2372011-07-07 11:11:02 -07002787 */
Jani Nikula9d1a1032014-03-14 16:51:15 +02002788static ssize_t
2789intel_dp_dpcd_read_wake(struct drm_dp_aux *aux, unsigned int offset,
2790 void *buffer, size_t size)
Jesse Barnesdf0c2372011-07-07 11:11:02 -07002791{
Jani Nikula9d1a1032014-03-14 16:51:15 +02002792 ssize_t ret;
2793 int i;
Jesse Barnesdf0c2372011-07-07 11:11:02 -07002794
Ville Syrjäläf6a19062014-10-16 20:46:09 +03002795 /*
2796 * Sometime we just get the same incorrect byte repeated
2797 * over the entire buffer. Doing just one throw away read
2798 * initially seems to "solve" it.
2799 */
2800 drm_dp_dpcd_read(aux, DP_DPCD_REV, buffer, 1);
2801
Jesse Barnesdf0c2372011-07-07 11:11:02 -07002802 for (i = 0; i < 3; i++) {
Jani Nikula9d1a1032014-03-14 16:51:15 +02002803 ret = drm_dp_dpcd_read(aux, offset, buffer, size);
2804 if (ret == size)
2805 return ret;
Jesse Barnesdf0c2372011-07-07 11:11:02 -07002806 msleep(1);
2807 }
2808
Jani Nikula9d1a1032014-03-14 16:51:15 +02002809 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002810}
2811
2812/*
2813 * Fetch AUX CH registers 0x202 - 0x207 which contain
2814 * link status information
2815 */
2816static bool
Keith Packard93f62da2011-11-01 19:45:03 -07002817intel_dp_get_link_status(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002818{
Jani Nikula9d1a1032014-03-14 16:51:15 +02002819 return intel_dp_dpcd_read_wake(&intel_dp->aux,
2820 DP_LANE0_1_STATUS,
2821 link_status,
2822 DP_LINK_STATUS_SIZE) == DP_LINK_STATUS_SIZE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002823}
2824
Paulo Zanoni11002442014-06-13 18:45:41 -03002825/* These are source-specific values. */
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002826static uint8_t
Keith Packard1a2eb462011-11-16 16:26:07 -08002827intel_dp_voltage_max(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002828{
Paulo Zanoni30add222012-10-26 19:05:45 -02002829 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Sonika Jindal7ad14a22015-02-25 10:29:12 +05302830 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deakbc7d38a2013-05-16 14:40:36 +03002831 enum port port = dp_to_dig_port(intel_dp)->port;
Keith Packard1a2eb462011-11-16 16:26:07 -08002832
Sonika Jindal7ad14a22015-02-25 10:29:12 +05302833 if (INTEL_INFO(dev)->gen >= 9) {
2834 if (dev_priv->vbt.edp_low_vswing && port == PORT_A)
2835 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
Damien Lespiau5a9d1f12013-12-03 13:56:26 +00002836 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
Sonika Jindal7ad14a22015-02-25 10:29:12 +05302837 } else if (IS_VALLEYVIEW(dev))
Sonika Jindalbd600182014-08-08 16:23:41 +05302838 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
Imre Deakbc7d38a2013-05-16 14:40:36 +03002839 else if (IS_GEN7(dev) && port == PORT_A)
Sonika Jindalbd600182014-08-08 16:23:41 +05302840 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
Imre Deakbc7d38a2013-05-16 14:40:36 +03002841 else if (HAS_PCH_CPT(dev) && port != PORT_A)
Sonika Jindalbd600182014-08-08 16:23:41 +05302842 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
Keith Packard1a2eb462011-11-16 16:26:07 -08002843 else
Sonika Jindalbd600182014-08-08 16:23:41 +05302844 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
Keith Packard1a2eb462011-11-16 16:26:07 -08002845}
2846
2847static uint8_t
2848intel_dp_pre_emphasis_max(struct intel_dp *intel_dp, uint8_t voltage_swing)
2849{
Paulo Zanoni30add222012-10-26 19:05:45 -02002850 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03002851 enum port port = dp_to_dig_port(intel_dp)->port;
Keith Packard1a2eb462011-11-16 16:26:07 -08002852
Damien Lespiau5a9d1f12013-12-03 13:56:26 +00002853 if (INTEL_INFO(dev)->gen >= 9) {
2854 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
2855 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
2856 return DP_TRAIN_PRE_EMPH_LEVEL_3;
2857 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
2858 return DP_TRAIN_PRE_EMPH_LEVEL_2;
2859 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
2860 return DP_TRAIN_PRE_EMPH_LEVEL_1;
Sonika Jindal7ad14a22015-02-25 10:29:12 +05302861 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
2862 return DP_TRAIN_PRE_EMPH_LEVEL_0;
Damien Lespiau5a9d1f12013-12-03 13:56:26 +00002863 default:
2864 return DP_TRAIN_PRE_EMPH_LEVEL_0;
2865 }
2866 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002867 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05302868 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
2869 return DP_TRAIN_PRE_EMPH_LEVEL_3;
2870 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
2871 return DP_TRAIN_PRE_EMPH_LEVEL_2;
2872 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
2873 return DP_TRAIN_PRE_EMPH_LEVEL_1;
2874 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002875 default:
Sonika Jindalbd600182014-08-08 16:23:41 +05302876 return DP_TRAIN_PRE_EMPH_LEVEL_0;
Paulo Zanonid6c0d722012-10-15 15:51:34 -03002877 }
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002878 } else if (IS_VALLEYVIEW(dev)) {
2879 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05302880 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
2881 return DP_TRAIN_PRE_EMPH_LEVEL_3;
2882 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
2883 return DP_TRAIN_PRE_EMPH_LEVEL_2;
2884 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
2885 return DP_TRAIN_PRE_EMPH_LEVEL_1;
2886 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002887 default:
Sonika Jindalbd600182014-08-08 16:23:41 +05302888 return DP_TRAIN_PRE_EMPH_LEVEL_0;
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002889 }
Imre Deakbc7d38a2013-05-16 14:40:36 +03002890 } else if (IS_GEN7(dev) && port == PORT_A) {
Keith Packard1a2eb462011-11-16 16:26:07 -08002891 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05302892 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
2893 return DP_TRAIN_PRE_EMPH_LEVEL_2;
2894 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
2895 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
2896 return DP_TRAIN_PRE_EMPH_LEVEL_1;
Keith Packard1a2eb462011-11-16 16:26:07 -08002897 default:
Sonika Jindalbd600182014-08-08 16:23:41 +05302898 return DP_TRAIN_PRE_EMPH_LEVEL_0;
Keith Packard1a2eb462011-11-16 16:26:07 -08002899 }
2900 } else {
2901 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05302902 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
2903 return DP_TRAIN_PRE_EMPH_LEVEL_2;
2904 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
2905 return DP_TRAIN_PRE_EMPH_LEVEL_2;
2906 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
2907 return DP_TRAIN_PRE_EMPH_LEVEL_1;
2908 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Keith Packard1a2eb462011-11-16 16:26:07 -08002909 default:
Sonika Jindalbd600182014-08-08 16:23:41 +05302910 return DP_TRAIN_PRE_EMPH_LEVEL_0;
Keith Packard1a2eb462011-11-16 16:26:07 -08002911 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002912 }
2913}
2914
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002915static uint32_t intel_vlv_signal_levels(struct intel_dp *intel_dp)
2916{
2917 struct drm_device *dev = intel_dp_to_dev(intel_dp);
2918 struct drm_i915_private *dev_priv = dev->dev_private;
2919 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08002920 struct intel_crtc *intel_crtc =
2921 to_intel_crtc(dport->base.base.crtc);
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002922 unsigned long demph_reg_value, preemph_reg_value,
2923 uniqtranscale_reg_value;
2924 uint8_t train_set = intel_dp->train_set[0];
Chon Ming Leee4607fc2013-11-06 14:36:35 +08002925 enum dpio_channel port = vlv_dport_to_channel(dport);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08002926 int pipe = intel_crtc->pipe;
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002927
2928 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05302929 case DP_TRAIN_PRE_EMPH_LEVEL_0:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002930 preemph_reg_value = 0x0004000;
2931 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05302932 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002933 demph_reg_value = 0x2B405555;
2934 uniqtranscale_reg_value = 0x552AB83A;
2935 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05302936 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002937 demph_reg_value = 0x2B404040;
2938 uniqtranscale_reg_value = 0x5548B83A;
2939 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05302940 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002941 demph_reg_value = 0x2B245555;
2942 uniqtranscale_reg_value = 0x5560B83A;
2943 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05302944 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002945 demph_reg_value = 0x2B405555;
2946 uniqtranscale_reg_value = 0x5598DA3A;
2947 break;
2948 default:
2949 return 0;
2950 }
2951 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05302952 case DP_TRAIN_PRE_EMPH_LEVEL_1:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002953 preemph_reg_value = 0x0002000;
2954 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05302955 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002956 demph_reg_value = 0x2B404040;
2957 uniqtranscale_reg_value = 0x5552B83A;
2958 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05302959 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002960 demph_reg_value = 0x2B404848;
2961 uniqtranscale_reg_value = 0x5580B83A;
2962 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05302963 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002964 demph_reg_value = 0x2B404040;
2965 uniqtranscale_reg_value = 0x55ADDA3A;
2966 break;
2967 default:
2968 return 0;
2969 }
2970 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05302971 case DP_TRAIN_PRE_EMPH_LEVEL_2:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002972 preemph_reg_value = 0x0000000;
2973 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05302974 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002975 demph_reg_value = 0x2B305555;
2976 uniqtranscale_reg_value = 0x5570B83A;
2977 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05302978 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002979 demph_reg_value = 0x2B2B4040;
2980 uniqtranscale_reg_value = 0x55ADDA3A;
2981 break;
2982 default:
2983 return 0;
2984 }
2985 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05302986 case DP_TRAIN_PRE_EMPH_LEVEL_3:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002987 preemph_reg_value = 0x0006000;
2988 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05302989 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07002990 demph_reg_value = 0x1B405555;
2991 uniqtranscale_reg_value = 0x55ADDA3A;
2992 break;
2993 default:
2994 return 0;
2995 }
2996 break;
2997 default:
2998 return 0;
2999 }
3000
Chris Wilson0980a602013-07-26 19:57:35 +01003001 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08003002 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW5(port), 0x00000000);
3003 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW4(port), demph_reg_value);
3004 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW2(port),
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003005 uniqtranscale_reg_value);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08003006 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW3(port), 0x0C782040);
3007 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW11(port), 0x00030000);
3008 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW9(port), preemph_reg_value);
3009 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW5(port), 0x80000000);
Chris Wilson0980a602013-07-26 19:57:35 +01003010 mutex_unlock(&dev_priv->dpio_lock);
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003011
3012 return 0;
3013}
3014
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003015static uint32_t intel_chv_signal_levels(struct intel_dp *intel_dp)
3016{
3017 struct drm_device *dev = intel_dp_to_dev(intel_dp);
3018 struct drm_i915_private *dev_priv = dev->dev_private;
3019 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
3020 struct intel_crtc *intel_crtc = to_intel_crtc(dport->base.base.crtc);
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003021 u32 deemph_reg_value, margin_reg_value, val;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003022 uint8_t train_set = intel_dp->train_set[0];
3023 enum dpio_channel ch = vlv_dport_to_channel(dport);
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003024 enum pipe pipe = intel_crtc->pipe;
3025 int i;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003026
3027 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303028 case DP_TRAIN_PRE_EMPH_LEVEL_0:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003029 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303030 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003031 deemph_reg_value = 128;
3032 margin_reg_value = 52;
3033 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303034 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003035 deemph_reg_value = 128;
3036 margin_reg_value = 77;
3037 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303038 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003039 deemph_reg_value = 128;
3040 margin_reg_value = 102;
3041 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303042 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003043 deemph_reg_value = 128;
3044 margin_reg_value = 154;
3045 /* FIXME extra to set for 1200 */
3046 break;
3047 default:
3048 return 0;
3049 }
3050 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303051 case DP_TRAIN_PRE_EMPH_LEVEL_1:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003052 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303053 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003054 deemph_reg_value = 85;
3055 margin_reg_value = 78;
3056 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303057 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003058 deemph_reg_value = 85;
3059 margin_reg_value = 116;
3060 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303061 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003062 deemph_reg_value = 85;
3063 margin_reg_value = 154;
3064 break;
3065 default:
3066 return 0;
3067 }
3068 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303069 case DP_TRAIN_PRE_EMPH_LEVEL_2:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003070 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303071 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003072 deemph_reg_value = 64;
3073 margin_reg_value = 104;
3074 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303075 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003076 deemph_reg_value = 64;
3077 margin_reg_value = 154;
3078 break;
3079 default:
3080 return 0;
3081 }
3082 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303083 case DP_TRAIN_PRE_EMPH_LEVEL_3:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003084 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303085 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003086 deemph_reg_value = 43;
3087 margin_reg_value = 154;
3088 break;
3089 default:
3090 return 0;
3091 }
3092 break;
3093 default:
3094 return 0;
3095 }
3096
3097 mutex_lock(&dev_priv->dpio_lock);
3098
3099 /* Clear calc init */
Ville Syrjälä1966e592014-04-09 13:29:04 +03003100 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW10(ch));
3101 val &= ~(DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3);
Ville Syrjäläa02ef3c2014-08-18 14:42:45 +03003102 val &= ~(DPIO_PCS_TX1DEEMP_MASK | DPIO_PCS_TX2DEEMP_MASK);
3103 val |= DPIO_PCS_TX1DEEMP_9P5 | DPIO_PCS_TX2DEEMP_9P5;
Ville Syrjälä1966e592014-04-09 13:29:04 +03003104 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW10(ch), val);
3105
3106 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW10(ch));
3107 val &= ~(DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3);
Ville Syrjäläa02ef3c2014-08-18 14:42:45 +03003108 val &= ~(DPIO_PCS_TX1DEEMP_MASK | DPIO_PCS_TX2DEEMP_MASK);
3109 val |= DPIO_PCS_TX1DEEMP_9P5 | DPIO_PCS_TX2DEEMP_9P5;
Ville Syrjälä1966e592014-04-09 13:29:04 +03003110 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW10(ch), val);
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003111
Ville Syrjäläa02ef3c2014-08-18 14:42:45 +03003112 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW9(ch));
3113 val &= ~(DPIO_PCS_TX1MARGIN_MASK | DPIO_PCS_TX2MARGIN_MASK);
3114 val |= DPIO_PCS_TX1MARGIN_000 | DPIO_PCS_TX2MARGIN_000;
3115 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW9(ch), val);
3116
3117 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW9(ch));
3118 val &= ~(DPIO_PCS_TX1MARGIN_MASK | DPIO_PCS_TX2MARGIN_MASK);
3119 val |= DPIO_PCS_TX1MARGIN_000 | DPIO_PCS_TX2MARGIN_000;
3120 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW9(ch), val);
3121
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003122 /* Program swing deemph */
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003123 for (i = 0; i < 4; i++) {
3124 val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW4(ch, i));
3125 val &= ~DPIO_SWING_DEEMPH9P5_MASK;
3126 val |= deemph_reg_value << DPIO_SWING_DEEMPH9P5_SHIFT;
3127 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW4(ch, i), val);
3128 }
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003129
3130 /* Program swing margin */
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003131 for (i = 0; i < 4; i++) {
3132 val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW2(ch, i));
Ville Syrjälä1fb44502014-06-28 02:04:03 +03003133 val &= ~DPIO_SWING_MARGIN000_MASK;
3134 val |= margin_reg_value << DPIO_SWING_MARGIN000_SHIFT;
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003135 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW2(ch, i), val);
3136 }
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003137
3138 /* Disable unique transition scale */
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003139 for (i = 0; i < 4; i++) {
3140 val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW3(ch, i));
3141 val &= ~DPIO_TX_UNIQ_TRANS_SCALE_EN;
3142 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW3(ch, i), val);
3143 }
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003144
3145 if (((train_set & DP_TRAIN_PRE_EMPHASIS_MASK)
Sonika Jindalbd600182014-08-08 16:23:41 +05303146 == DP_TRAIN_PRE_EMPH_LEVEL_0) &&
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003147 ((train_set & DP_TRAIN_VOLTAGE_SWING_MASK)
Sonika Jindalbd600182014-08-08 16:23:41 +05303148 == DP_TRAIN_VOLTAGE_SWING_LEVEL_3)) {
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003149
3150 /*
3151 * The document said it needs to set bit 27 for ch0 and bit 26
3152 * for ch1. Might be a typo in the doc.
3153 * For now, for this unique transition scale selection, set bit
3154 * 27 for ch0 and ch1.
3155 */
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003156 for (i = 0; i < 4; i++) {
3157 val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW3(ch, i));
3158 val |= DPIO_TX_UNIQ_TRANS_SCALE_EN;
3159 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW3(ch, i), val);
3160 }
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003161
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003162 for (i = 0; i < 4; i++) {
3163 val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW2(ch, i));
3164 val &= ~(0xff << DPIO_UNIQ_TRANS_SCALE_SHIFT);
3165 val |= (0x9a << DPIO_UNIQ_TRANS_SCALE_SHIFT);
3166 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW2(ch, i), val);
3167 }
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003168 }
3169
3170 /* Start swing calculation */
Ville Syrjälä1966e592014-04-09 13:29:04 +03003171 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW10(ch));
3172 val |= DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3;
3173 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW10(ch), val);
3174
3175 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW10(ch));
3176 val |= DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3;
3177 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW10(ch), val);
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003178
3179 /* LRC Bypass */
3180 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW30);
3181 val |= DPIO_LRC_BYPASS;
3182 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW30, val);
3183
3184 mutex_unlock(&dev_priv->dpio_lock);
3185
3186 return 0;
3187}
3188
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003189static void
Jani Nikula0301b3a2013-10-15 09:36:08 +03003190intel_get_adjust_train(struct intel_dp *intel_dp,
3191 const uint8_t link_status[DP_LINK_STATUS_SIZE])
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003192{
3193 uint8_t v = 0;
3194 uint8_t p = 0;
3195 int lane;
Keith Packard1a2eb462011-11-16 16:26:07 -08003196 uint8_t voltage_max;
3197 uint8_t preemph_max;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003198
Jesse Barnes33a34e42010-09-08 12:42:02 -07003199 for (lane = 0; lane < intel_dp->lane_count; lane++) {
Daniel Vetter0f037bd2012-10-18 10:15:27 +02003200 uint8_t this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
3201 uint8_t this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003202
3203 if (this_v > v)
3204 v = this_v;
3205 if (this_p > p)
3206 p = this_p;
3207 }
3208
Keith Packard1a2eb462011-11-16 16:26:07 -08003209 voltage_max = intel_dp_voltage_max(intel_dp);
Keith Packard417e8222011-11-01 19:54:11 -07003210 if (v >= voltage_max)
3211 v = voltage_max | DP_TRAIN_MAX_SWING_REACHED;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003212
Keith Packard1a2eb462011-11-16 16:26:07 -08003213 preemph_max = intel_dp_pre_emphasis_max(intel_dp, v);
3214 if (p >= preemph_max)
3215 p = preemph_max | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003216
3217 for (lane = 0; lane < 4; lane++)
Jesse Barnes33a34e42010-09-08 12:42:02 -07003218 intel_dp->train_set[lane] = v | p;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003219}
3220
3221static uint32_t
Paulo Zanonif0a34242012-12-06 16:51:50 -02003222intel_gen4_signal_levels(uint8_t train_set)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003223{
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003224 uint32_t signal_levels = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003225
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003226 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303227 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003228 default:
3229 signal_levels |= DP_VOLTAGE_0_4;
3230 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303231 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003232 signal_levels |= DP_VOLTAGE_0_6;
3233 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303234 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003235 signal_levels |= DP_VOLTAGE_0_8;
3236 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303237 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003238 signal_levels |= DP_VOLTAGE_1_2;
3239 break;
3240 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003241 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303242 case DP_TRAIN_PRE_EMPH_LEVEL_0:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003243 default:
3244 signal_levels |= DP_PRE_EMPHASIS_0;
3245 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303246 case DP_TRAIN_PRE_EMPH_LEVEL_1:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003247 signal_levels |= DP_PRE_EMPHASIS_3_5;
3248 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303249 case DP_TRAIN_PRE_EMPH_LEVEL_2:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003250 signal_levels |= DP_PRE_EMPHASIS_6;
3251 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303252 case DP_TRAIN_PRE_EMPH_LEVEL_3:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003253 signal_levels |= DP_PRE_EMPHASIS_9_5;
3254 break;
3255 }
3256 return signal_levels;
3257}
3258
Zhenyu Wange3421a12010-04-08 09:43:27 +08003259/* Gen6's DP voltage swing and pre-emphasis control */
3260static uint32_t
3261intel_gen6_edp_signal_levels(uint8_t train_set)
3262{
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003263 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
3264 DP_TRAIN_PRE_EMPHASIS_MASK);
3265 switch (signal_levels) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303266 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3267 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003268 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
Sonika Jindalbd600182014-08-08 16:23:41 +05303269 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003270 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
Sonika Jindalbd600182014-08-08 16:23:41 +05303271 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
3272 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003273 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
Sonika Jindalbd600182014-08-08 16:23:41 +05303274 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
3275 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003276 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
Sonika Jindalbd600182014-08-08 16:23:41 +05303277 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3278 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003279 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08003280 default:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003281 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
3282 "0x%x\n", signal_levels);
3283 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08003284 }
3285}
3286
Keith Packard1a2eb462011-11-16 16:26:07 -08003287/* Gen7's DP voltage swing and pre-emphasis control */
3288static uint32_t
3289intel_gen7_edp_signal_levels(uint8_t train_set)
3290{
3291 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
3292 DP_TRAIN_PRE_EMPHASIS_MASK);
3293 switch (signal_levels) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303294 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Keith Packard1a2eb462011-11-16 16:26:07 -08003295 return EDP_LINK_TRAIN_400MV_0DB_IVB;
Sonika Jindalbd600182014-08-08 16:23:41 +05303296 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Keith Packard1a2eb462011-11-16 16:26:07 -08003297 return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
Sonika Jindalbd600182014-08-08 16:23:41 +05303298 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
Keith Packard1a2eb462011-11-16 16:26:07 -08003299 return EDP_LINK_TRAIN_400MV_6DB_IVB;
3300
Sonika Jindalbd600182014-08-08 16:23:41 +05303301 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Keith Packard1a2eb462011-11-16 16:26:07 -08003302 return EDP_LINK_TRAIN_600MV_0DB_IVB;
Sonika Jindalbd600182014-08-08 16:23:41 +05303303 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Keith Packard1a2eb462011-11-16 16:26:07 -08003304 return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
3305
Sonika Jindalbd600182014-08-08 16:23:41 +05303306 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Keith Packard1a2eb462011-11-16 16:26:07 -08003307 return EDP_LINK_TRAIN_800MV_0DB_IVB;
Sonika Jindalbd600182014-08-08 16:23:41 +05303308 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Keith Packard1a2eb462011-11-16 16:26:07 -08003309 return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
3310
3311 default:
3312 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
3313 "0x%x\n", signal_levels);
3314 return EDP_LINK_TRAIN_500MV_0DB_IVB;
3315 }
3316}
3317
Paulo Zanonid6c0d722012-10-15 15:51:34 -03003318/* Gen7.5's (HSW) DP voltage swing and pre-emphasis control */
3319static uint32_t
Paulo Zanonif0a34242012-12-06 16:51:50 -02003320intel_hsw_signal_levels(uint8_t train_set)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003321{
Paulo Zanonid6c0d722012-10-15 15:51:34 -03003322 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
3323 DP_TRAIN_PRE_EMPHASIS_MASK);
3324 switch (signal_levels) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303325 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Sonika Jindalc5fe6a02014-08-11 08:57:36 +05303326 return DDI_BUF_TRANS_SELECT(0);
Sonika Jindalbd600182014-08-08 16:23:41 +05303327 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Sonika Jindalc5fe6a02014-08-11 08:57:36 +05303328 return DDI_BUF_TRANS_SELECT(1);
Sonika Jindalbd600182014-08-08 16:23:41 +05303329 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
Sonika Jindalc5fe6a02014-08-11 08:57:36 +05303330 return DDI_BUF_TRANS_SELECT(2);
Sonika Jindalbd600182014-08-08 16:23:41 +05303331 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_3:
Sonika Jindalc5fe6a02014-08-11 08:57:36 +05303332 return DDI_BUF_TRANS_SELECT(3);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003333
Sonika Jindalbd600182014-08-08 16:23:41 +05303334 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Sonika Jindalc5fe6a02014-08-11 08:57:36 +05303335 return DDI_BUF_TRANS_SELECT(4);
Sonika Jindalbd600182014-08-08 16:23:41 +05303336 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Sonika Jindalc5fe6a02014-08-11 08:57:36 +05303337 return DDI_BUF_TRANS_SELECT(5);
Sonika Jindalbd600182014-08-08 16:23:41 +05303338 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2:
Sonika Jindalc5fe6a02014-08-11 08:57:36 +05303339 return DDI_BUF_TRANS_SELECT(6);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003340
Sonika Jindalbd600182014-08-08 16:23:41 +05303341 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Sonika Jindalc5fe6a02014-08-11 08:57:36 +05303342 return DDI_BUF_TRANS_SELECT(7);
Sonika Jindalbd600182014-08-08 16:23:41 +05303343 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Sonika Jindalc5fe6a02014-08-11 08:57:36 +05303344 return DDI_BUF_TRANS_SELECT(8);
Sonika Jindal7ad14a22015-02-25 10:29:12 +05303345
3346 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3347 return DDI_BUF_TRANS_SELECT(9);
Paulo Zanonid6c0d722012-10-15 15:51:34 -03003348 default:
3349 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
3350 "0x%x\n", signal_levels);
Sonika Jindalc5fe6a02014-08-11 08:57:36 +05303351 return DDI_BUF_TRANS_SELECT(0);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003352 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003353}
3354
Paulo Zanonif0a34242012-12-06 16:51:50 -02003355/* Properly updates "DP" with the correct signal levels. */
3356static void
3357intel_dp_set_signal_levels(struct intel_dp *intel_dp, uint32_t *DP)
3358{
3359 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03003360 enum port port = intel_dig_port->port;
Paulo Zanonif0a34242012-12-06 16:51:50 -02003361 struct drm_device *dev = intel_dig_port->base.base.dev;
3362 uint32_t signal_levels, mask;
3363 uint8_t train_set = intel_dp->train_set[0];
3364
Damien Lespiau5a9d1f12013-12-03 13:56:26 +00003365 if (IS_HASWELL(dev) || IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanonif0a34242012-12-06 16:51:50 -02003366 signal_levels = intel_hsw_signal_levels(train_set);
3367 mask = DDI_BUF_EMP_MASK;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003368 } else if (IS_CHERRYVIEW(dev)) {
3369 signal_levels = intel_chv_signal_levels(intel_dp);
3370 mask = 0;
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003371 } else if (IS_VALLEYVIEW(dev)) {
3372 signal_levels = intel_vlv_signal_levels(intel_dp);
3373 mask = 0;
Imre Deakbc7d38a2013-05-16 14:40:36 +03003374 } else if (IS_GEN7(dev) && port == PORT_A) {
Paulo Zanonif0a34242012-12-06 16:51:50 -02003375 signal_levels = intel_gen7_edp_signal_levels(train_set);
3376 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_IVB;
Imre Deakbc7d38a2013-05-16 14:40:36 +03003377 } else if (IS_GEN6(dev) && port == PORT_A) {
Paulo Zanonif0a34242012-12-06 16:51:50 -02003378 signal_levels = intel_gen6_edp_signal_levels(train_set);
3379 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_SNB;
3380 } else {
3381 signal_levels = intel_gen4_signal_levels(train_set);
3382 mask = DP_VOLTAGE_MASK | DP_PRE_EMPHASIS_MASK;
3383 }
3384
3385 DRM_DEBUG_KMS("Using signal levels %08x\n", signal_levels);
3386
3387 *DP = (*DP & ~mask) | signal_levels;
3388}
3389
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003390static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01003391intel_dp_set_link_train(struct intel_dp *intel_dp,
Jani Nikula70aff662013-09-27 15:10:44 +03003392 uint32_t *DP,
Chris Wilson58e10eb2010-10-03 10:56:11 +01003393 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003394{
Paulo Zanoni174edf12012-10-26 19:05:50 -02003395 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3396 struct drm_device *dev = intel_dig_port->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003397 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula2cdfe6c2013-10-04 15:08:48 +03003398 uint8_t buf[sizeof(intel_dp->train_set) + 1];
3399 int ret, len;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003400
Ville Syrjälä7b13b582014-08-18 22:16:08 +03003401 _intel_dp_set_link_train(intel_dp, DP, dp_train_pat);
Paulo Zanoni47ea7542012-07-17 16:55:16 -03003402
Jani Nikula70aff662013-09-27 15:10:44 +03003403 I915_WRITE(intel_dp->output_reg, *DP);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003404 POSTING_READ(intel_dp->output_reg);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003405
Jani Nikula2cdfe6c2013-10-04 15:08:48 +03003406 buf[0] = dp_train_pat;
3407 if ((dp_train_pat & DP_TRAINING_PATTERN_MASK) ==
Paulo Zanoni47ea7542012-07-17 16:55:16 -03003408 DP_TRAINING_PATTERN_DISABLE) {
Jani Nikula2cdfe6c2013-10-04 15:08:48 +03003409 /* don't write DP_TRAINING_LANEx_SET on disable */
3410 len = 1;
3411 } else {
3412 /* DP_TRAINING_LANEx_SET follow DP_TRAINING_PATTERN_SET */
3413 memcpy(buf + 1, intel_dp->train_set, intel_dp->lane_count);
3414 len = intel_dp->lane_count + 1;
Paulo Zanoni47ea7542012-07-17 16:55:16 -03003415 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003416
Jani Nikula9d1a1032014-03-14 16:51:15 +02003417 ret = drm_dp_dpcd_write(&intel_dp->aux, DP_TRAINING_PATTERN_SET,
3418 buf, len);
Jani Nikula2cdfe6c2013-10-04 15:08:48 +03003419
3420 return ret == len;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003421}
3422
Jani Nikula70aff662013-09-27 15:10:44 +03003423static bool
3424intel_dp_reset_link_train(struct intel_dp *intel_dp, uint32_t *DP,
3425 uint8_t dp_train_pat)
3426{
Jani Nikula953d22e2013-10-04 15:08:47 +03003427 memset(intel_dp->train_set, 0, sizeof(intel_dp->train_set));
Jani Nikula70aff662013-09-27 15:10:44 +03003428 intel_dp_set_signal_levels(intel_dp, DP);
3429 return intel_dp_set_link_train(intel_dp, DP, dp_train_pat);
3430}
3431
3432static bool
3433intel_dp_update_link_train(struct intel_dp *intel_dp, uint32_t *DP,
Jani Nikula0301b3a2013-10-15 09:36:08 +03003434 const uint8_t link_status[DP_LINK_STATUS_SIZE])
Jani Nikula70aff662013-09-27 15:10:44 +03003435{
3436 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3437 struct drm_device *dev = intel_dig_port->base.base.dev;
3438 struct drm_i915_private *dev_priv = dev->dev_private;
3439 int ret;
3440
3441 intel_get_adjust_train(intel_dp, link_status);
3442 intel_dp_set_signal_levels(intel_dp, DP);
3443
3444 I915_WRITE(intel_dp->output_reg, *DP);
3445 POSTING_READ(intel_dp->output_reg);
3446
Jani Nikula9d1a1032014-03-14 16:51:15 +02003447 ret = drm_dp_dpcd_write(&intel_dp->aux, DP_TRAINING_LANE0_SET,
3448 intel_dp->train_set, intel_dp->lane_count);
Jani Nikula70aff662013-09-27 15:10:44 +03003449
3450 return ret == intel_dp->lane_count;
3451}
3452
Imre Deak3ab9c632013-05-03 12:57:41 +03003453static void intel_dp_set_idle_link_train(struct intel_dp *intel_dp)
3454{
3455 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3456 struct drm_device *dev = intel_dig_port->base.base.dev;
3457 struct drm_i915_private *dev_priv = dev->dev_private;
3458 enum port port = intel_dig_port->port;
3459 uint32_t val;
3460
3461 if (!HAS_DDI(dev))
3462 return;
3463
3464 val = I915_READ(DP_TP_CTL(port));
3465 val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
3466 val |= DP_TP_CTL_LINK_TRAIN_IDLE;
3467 I915_WRITE(DP_TP_CTL(port), val);
3468
3469 /*
3470 * On PORT_A we can have only eDP in SST mode. There the only reason
3471 * we need to set idle transmission mode is to work around a HW issue
3472 * where we enable the pipe while not in idle link-training mode.
3473 * In this case there is requirement to wait for a minimum number of
3474 * idle patterns to be sent.
3475 */
3476 if (port == PORT_A)
3477 return;
3478
3479 if (wait_for((I915_READ(DP_TP_STATUS(port)) & DP_TP_STATUS_IDLE_DONE),
3480 1))
3481 DRM_ERROR("Timed out waiting for DP idle patterns\n");
3482}
3483
Jesse Barnes33a34e42010-09-08 12:42:02 -07003484/* Enable corresponding port and start training pattern 1 */
Paulo Zanonic19b0662012-10-15 15:51:41 -03003485void
Jesse Barnes33a34e42010-09-08 12:42:02 -07003486intel_dp_start_link_train(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003487{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02003488 struct drm_encoder *encoder = &dp_to_dig_port(intel_dp)->base.base;
Paulo Zanonic19b0662012-10-15 15:51:41 -03003489 struct drm_device *dev = encoder->dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003490 int i;
3491 uint8_t voltage;
Keith Packardcdb0e952011-11-01 20:00:06 -07003492 int voltage_tries, loop_tries;
Chris Wilsonea5b2132010-08-04 13:50:23 +01003493 uint32_t DP = intel_dp->DP;
Jani Nikula6aba5b62013-10-04 15:08:10 +03003494 uint8_t link_config[2];
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003495
Paulo Zanoniaffa9352012-11-23 15:30:39 -02003496 if (HAS_DDI(dev))
Paulo Zanonic19b0662012-10-15 15:51:41 -03003497 intel_ddi_prepare_link_retrain(encoder);
3498
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003499 /* Write the link configuration data */
Jani Nikula6aba5b62013-10-04 15:08:10 +03003500 link_config[0] = intel_dp->link_bw;
3501 link_config[1] = intel_dp->lane_count;
3502 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
3503 link_config[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
Jani Nikula9d1a1032014-03-14 16:51:15 +02003504 drm_dp_dpcd_write(&intel_dp->aux, DP_LINK_BW_SET, link_config, 2);
Sonika Jindala8f3ef62015-03-05 10:02:30 +05303505 if (INTEL_INFO(dev)->gen >= 9 && intel_dp->supported_rates[0])
3506 drm_dp_dpcd_write(&intel_dp->aux, DP_LINK_RATE_SET,
3507 &intel_dp->rate_select, 1);
Jani Nikula6aba5b62013-10-04 15:08:10 +03003508
3509 link_config[0] = 0;
3510 link_config[1] = DP_SET_ANSI_8B10B;
Jani Nikula9d1a1032014-03-14 16:51:15 +02003511 drm_dp_dpcd_write(&intel_dp->aux, DP_DOWNSPREAD_CTRL, link_config, 2);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003512
3513 DP |= DP_PORT_EN;
Keith Packard1a2eb462011-11-16 16:26:07 -08003514
Jani Nikula70aff662013-09-27 15:10:44 +03003515 /* clock recovery */
3516 if (!intel_dp_reset_link_train(intel_dp, &DP,
3517 DP_TRAINING_PATTERN_1 |
3518 DP_LINK_SCRAMBLING_DISABLE)) {
3519 DRM_ERROR("failed to enable link training\n");
3520 return;
3521 }
3522
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003523 voltage = 0xff;
Keith Packardcdb0e952011-11-01 20:00:06 -07003524 voltage_tries = 0;
3525 loop_tries = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003526 for (;;) {
Jani Nikula70aff662013-09-27 15:10:44 +03003527 uint8_t link_status[DP_LINK_STATUS_SIZE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003528
Daniel Vettera7c96552012-10-18 10:15:30 +02003529 drm_dp_link_train_clock_recovery_delay(intel_dp->dpcd);
Keith Packard93f62da2011-11-01 19:45:03 -07003530 if (!intel_dp_get_link_status(intel_dp, link_status)) {
3531 DRM_ERROR("failed to get link status\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003532 break;
Keith Packard93f62da2011-11-01 19:45:03 -07003533 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003534
Daniel Vetter01916272012-10-18 10:15:25 +02003535 if (drm_dp_clock_recovery_ok(link_status, intel_dp->lane_count)) {
Keith Packard93f62da2011-11-01 19:45:03 -07003536 DRM_DEBUG_KMS("clock recovery OK\n");
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003537 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003538 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003539
3540 /* Check to see if we've tried the max voltage */
3541 for (i = 0; i < intel_dp->lane_count; i++)
3542 if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
3543 break;
Takashi Iwai3b4f8192013-03-11 18:40:16 +01003544 if (i == intel_dp->lane_count) {
Daniel Vetterb06fbda2012-10-16 09:50:25 +02003545 ++loop_tries;
3546 if (loop_tries == 5) {
Jani Nikula3def84b2013-10-05 16:13:56 +03003547 DRM_ERROR("too many full retries, give up\n");
Keith Packardcdb0e952011-11-01 20:00:06 -07003548 break;
3549 }
Jani Nikula70aff662013-09-27 15:10:44 +03003550 intel_dp_reset_link_train(intel_dp, &DP,
3551 DP_TRAINING_PATTERN_1 |
3552 DP_LINK_SCRAMBLING_DISABLE);
Keith Packardcdb0e952011-11-01 20:00:06 -07003553 voltage_tries = 0;
3554 continue;
3555 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003556
3557 /* Check to see if we've tried the same voltage 5 times */
Daniel Vetterb06fbda2012-10-16 09:50:25 +02003558 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
Chris Wilson24773672012-09-26 16:48:30 +01003559 ++voltage_tries;
Daniel Vetterb06fbda2012-10-16 09:50:25 +02003560 if (voltage_tries == 5) {
Jani Nikula3def84b2013-10-05 16:13:56 +03003561 DRM_ERROR("too many voltage retries, give up\n");
Daniel Vetterb06fbda2012-10-16 09:50:25 +02003562 break;
3563 }
3564 } else
3565 voltage_tries = 0;
3566 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003567
Jani Nikula70aff662013-09-27 15:10:44 +03003568 /* Update training set as requested by target */
3569 if (!intel_dp_update_link_train(intel_dp, &DP, link_status)) {
3570 DRM_ERROR("failed to update link training\n");
3571 break;
3572 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003573 }
3574
Jesse Barnes33a34e42010-09-08 12:42:02 -07003575 intel_dp->DP = DP;
3576}
3577
Paulo Zanonic19b0662012-10-15 15:51:41 -03003578void
Jesse Barnes33a34e42010-09-08 12:42:02 -07003579intel_dp_complete_link_train(struct intel_dp *intel_dp)
3580{
Jesse Barnes33a34e42010-09-08 12:42:02 -07003581 bool channel_eq = false;
Jesse Barnes37f80972011-01-05 14:45:24 -08003582 int tries, cr_tries;
Jesse Barnes33a34e42010-09-08 12:42:02 -07003583 uint32_t DP = intel_dp->DP;
Todd Previte06ea66b2014-01-20 10:19:39 -07003584 uint32_t training_pattern = DP_TRAINING_PATTERN_2;
3585
3586 /* Training Pattern 3 for HBR2 ot 1.2 devices that support it*/
3587 if (intel_dp->link_bw == DP_LINK_BW_5_4 || intel_dp->use_tps3)
3588 training_pattern = DP_TRAINING_PATTERN_3;
Jesse Barnes33a34e42010-09-08 12:42:02 -07003589
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003590 /* channel equalization */
Jani Nikula70aff662013-09-27 15:10:44 +03003591 if (!intel_dp_set_link_train(intel_dp, &DP,
Todd Previte06ea66b2014-01-20 10:19:39 -07003592 training_pattern |
Jani Nikula70aff662013-09-27 15:10:44 +03003593 DP_LINK_SCRAMBLING_DISABLE)) {
3594 DRM_ERROR("failed to start channel equalization\n");
3595 return;
3596 }
3597
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003598 tries = 0;
Jesse Barnes37f80972011-01-05 14:45:24 -08003599 cr_tries = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003600 channel_eq = false;
3601 for (;;) {
Jani Nikula70aff662013-09-27 15:10:44 +03003602 uint8_t link_status[DP_LINK_STATUS_SIZE];
Zhenyu Wange3421a12010-04-08 09:43:27 +08003603
Jesse Barnes37f80972011-01-05 14:45:24 -08003604 if (cr_tries > 5) {
3605 DRM_ERROR("failed to train DP, aborting\n");
Jesse Barnes37f80972011-01-05 14:45:24 -08003606 break;
3607 }
3608
Daniel Vettera7c96552012-10-18 10:15:30 +02003609 drm_dp_link_train_channel_eq_delay(intel_dp->dpcd);
Jani Nikula70aff662013-09-27 15:10:44 +03003610 if (!intel_dp_get_link_status(intel_dp, link_status)) {
3611 DRM_ERROR("failed to get link status\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003612 break;
Jani Nikula70aff662013-09-27 15:10:44 +03003613 }
Jesse Barnes869184a2010-10-07 16:01:22 -07003614
Jesse Barnes37f80972011-01-05 14:45:24 -08003615 /* Make sure clock is still ok */
Daniel Vetter01916272012-10-18 10:15:25 +02003616 if (!drm_dp_clock_recovery_ok(link_status, intel_dp->lane_count)) {
Jesse Barnes37f80972011-01-05 14:45:24 -08003617 intel_dp_start_link_train(intel_dp);
Jani Nikula70aff662013-09-27 15:10:44 +03003618 intel_dp_set_link_train(intel_dp, &DP,
Todd Previte06ea66b2014-01-20 10:19:39 -07003619 training_pattern |
Jani Nikula70aff662013-09-27 15:10:44 +03003620 DP_LINK_SCRAMBLING_DISABLE);
Jesse Barnes37f80972011-01-05 14:45:24 -08003621 cr_tries++;
3622 continue;
3623 }
3624
Daniel Vetter1ffdff12012-10-18 10:15:24 +02003625 if (drm_dp_channel_eq_ok(link_status, intel_dp->lane_count)) {
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003626 channel_eq = true;
3627 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003628 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003629
Jesse Barnes37f80972011-01-05 14:45:24 -08003630 /* Try 5 times, then try clock recovery if that fails */
3631 if (tries > 5) {
Jesse Barnes37f80972011-01-05 14:45:24 -08003632 intel_dp_start_link_train(intel_dp);
Jani Nikula70aff662013-09-27 15:10:44 +03003633 intel_dp_set_link_train(intel_dp, &DP,
Todd Previte06ea66b2014-01-20 10:19:39 -07003634 training_pattern |
Jani Nikula70aff662013-09-27 15:10:44 +03003635 DP_LINK_SCRAMBLING_DISABLE);
Jesse Barnes37f80972011-01-05 14:45:24 -08003636 tries = 0;
3637 cr_tries++;
3638 continue;
3639 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003640
Jani Nikula70aff662013-09-27 15:10:44 +03003641 /* Update training set as requested by target */
3642 if (!intel_dp_update_link_train(intel_dp, &DP, link_status)) {
3643 DRM_ERROR("failed to update link training\n");
3644 break;
3645 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003646 ++tries;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003647 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003648
Imre Deak3ab9c632013-05-03 12:57:41 +03003649 intel_dp_set_idle_link_train(intel_dp);
3650
3651 intel_dp->DP = DP;
3652
Paulo Zanonid6c0d722012-10-15 15:51:34 -03003653 if (channel_eq)
Masanari Iida07f42252013-03-20 11:00:34 +09003654 DRM_DEBUG_KMS("Channel EQ done. DP Training successful\n");
Paulo Zanonid6c0d722012-10-15 15:51:34 -03003655
Imre Deak3ab9c632013-05-03 12:57:41 +03003656}
3657
3658void intel_dp_stop_link_train(struct intel_dp *intel_dp)
3659{
Jani Nikula70aff662013-09-27 15:10:44 +03003660 intel_dp_set_link_train(intel_dp, &intel_dp->DP,
Imre Deak3ab9c632013-05-03 12:57:41 +03003661 DP_TRAINING_PATTERN_DISABLE);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003662}
3663
3664static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01003665intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003666{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02003667 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03003668 enum port port = intel_dig_port->port;
Paulo Zanonida63a9f2012-10-26 19:05:46 -02003669 struct drm_device *dev = intel_dig_port->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003670 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01003671 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003672
Daniel Vetterbc76e322014-05-20 22:46:50 +02003673 if (WARN_ON(HAS_DDI(dev)))
Paulo Zanonic19b0662012-10-15 15:51:41 -03003674 return;
3675
Daniel Vetter0c33d8d2012-09-06 22:15:43 +02003676 if (WARN_ON((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0))
Chris Wilson1b39d6f2010-12-06 11:20:45 +00003677 return;
3678
Zhao Yakui28c97732009-10-09 11:39:41 +08003679 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003680
Imre Deakbc7d38a2013-05-16 14:40:36 +03003681 if (HAS_PCH_CPT(dev) && (IS_GEN7(dev) || port != PORT_A)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08003682 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01003683 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE_CPT);
Zhenyu Wange3421a12010-04-08 09:43:27 +08003684 } else {
Ville Syrjäläaad3d142014-06-28 02:04:25 +03003685 if (IS_CHERRYVIEW(dev))
3686 DP &= ~DP_LINK_TRAIN_MASK_CHV;
3687 else
3688 DP &= ~DP_LINK_TRAIN_MASK;
Chris Wilsonea5b2132010-08-04 13:50:23 +01003689 I915_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
Zhenyu Wange3421a12010-04-08 09:43:27 +08003690 }
Chris Wilsonfe255d02010-09-11 21:37:48 +01003691 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08003692
Daniel Vetter493a7082012-05-30 12:31:56 +02003693 if (HAS_PCH_IBX(dev) &&
Chris Wilson1b39d6f2010-12-06 11:20:45 +00003694 I915_READ(intel_dp->output_reg) & DP_PIPEB_SELECT) {
Eric Anholt5bddd172010-11-18 09:32:59 +08003695 /* Hardware workaround: leaving our transcoder select
3696 * set to transcoder B while it's off will prevent the
3697 * corresponding HDMI output on transcoder A.
3698 *
3699 * Combine this with another hardware workaround:
3700 * transcoder select bit can only be cleared while the
3701 * port is enabled.
3702 */
3703 DP &= ~DP_PIPEB_SELECT;
3704 I915_WRITE(intel_dp->output_reg, DP);
Daniel Vetter0ca09682014-11-24 16:54:11 +01003705 POSTING_READ(intel_dp->output_reg);
Eric Anholt5bddd172010-11-18 09:32:59 +08003706 }
3707
Wu Fengguang832afda2011-12-09 20:42:21 +08003708 DP &= ~DP_AUDIO_OUTPUT_ENABLE;
Chris Wilsonea5b2132010-08-04 13:50:23 +01003709 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
3710 POSTING_READ(intel_dp->output_reg);
Keith Packardf01eca22011-09-28 16:48:10 -07003711 msleep(intel_dp->panel_power_down_delay);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003712}
3713
Keith Packard26d61aa2011-07-25 20:01:09 -07003714static bool
3715intel_dp_get_dpcd(struct intel_dp *intel_dp)
Keith Packard92fd8fd2011-07-25 19:50:10 -07003716{
Rodrigo Vivia031d702013-10-03 16:15:06 -03003717 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3718 struct drm_device *dev = dig_port->base.base.dev;
3719 struct drm_i915_private *dev_priv = dev->dev_private;
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05303720 uint8_t rev;
Rodrigo Vivia031d702013-10-03 16:15:06 -03003721
Jani Nikula9d1a1032014-03-14 16:51:15 +02003722 if (intel_dp_dpcd_read_wake(&intel_dp->aux, 0x000, intel_dp->dpcd,
3723 sizeof(intel_dp->dpcd)) < 0)
Adam Jacksonedb39242012-09-18 10:58:49 -04003724 return false; /* aux transfer failed */
Keith Packard92fd8fd2011-07-25 19:50:10 -07003725
Andy Shevchenkoa8e98152014-09-01 14:12:01 +03003726 DRM_DEBUG_KMS("DPCD: %*ph\n", (int) sizeof(intel_dp->dpcd), intel_dp->dpcd);
Damien Lespiau577c7a52012-12-13 16:09:02 +00003727
Adam Jacksonedb39242012-09-18 10:58:49 -04003728 if (intel_dp->dpcd[DP_DPCD_REV] == 0)
3729 return false; /* DPCD not present */
3730
Shobhit Kumar2293bb52013-07-11 18:44:56 -03003731 /* Check if the panel supports PSR */
3732 memset(intel_dp->psr_dpcd, 0, sizeof(intel_dp->psr_dpcd));
Jani Nikula50003932013-09-20 16:42:17 +03003733 if (is_edp(intel_dp)) {
Jani Nikula9d1a1032014-03-14 16:51:15 +02003734 intel_dp_dpcd_read_wake(&intel_dp->aux, DP_PSR_SUPPORT,
3735 intel_dp->psr_dpcd,
3736 sizeof(intel_dp->psr_dpcd));
Rodrigo Vivia031d702013-10-03 16:15:06 -03003737 if (intel_dp->psr_dpcd[0] & DP_PSR_IS_SUPPORTED) {
3738 dev_priv->psr.sink_support = true;
Jani Nikula50003932013-09-20 16:42:17 +03003739 DRM_DEBUG_KMS("Detected EDP PSR Panel.\n");
Rodrigo Vivia031d702013-10-03 16:15:06 -03003740 }
Jani Nikula50003932013-09-20 16:42:17 +03003741 }
3742
Jani Nikula7809a612014-10-29 11:03:26 +02003743 /* Training Pattern 3 support, both source and sink */
Todd Previte06ea66b2014-01-20 10:19:39 -07003744 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x12 &&
Jani Nikula7809a612014-10-29 11:03:26 +02003745 intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_TPS3_SUPPORTED &&
3746 (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)) {
Todd Previte06ea66b2014-01-20 10:19:39 -07003747 intel_dp->use_tps3 = true;
Jani Nikulaf8d8a672014-09-05 16:19:18 +03003748 DRM_DEBUG_KMS("Displayport TPS3 supported\n");
Todd Previte06ea66b2014-01-20 10:19:39 -07003749 } else
3750 intel_dp->use_tps3 = false;
3751
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05303752 /* Intermediate frequency support */
3753 if (is_edp(intel_dp) &&
3754 (intel_dp->dpcd[DP_EDP_CONFIGURATION_CAP] & DP_DPCD_DISPLAY_CONTROL_CAPABLE) &&
3755 (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_EDP_DPCD_REV, &rev, 1) == 1) &&
3756 (rev >= 0x03)) { /* eDp v1.4 or higher */
3757 intel_dp_dpcd_read_wake(&intel_dp->aux,
3758 DP_SUPPORTED_LINK_RATES,
3759 intel_dp->supported_rates,
3760 sizeof(intel_dp->supported_rates));
3761 }
Adam Jacksonedb39242012-09-18 10:58:49 -04003762 if (!(intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
3763 DP_DWN_STRM_PORT_PRESENT))
3764 return true; /* native DP sink */
3765
3766 if (intel_dp->dpcd[DP_DPCD_REV] == 0x10)
3767 return true; /* no per-port downstream info */
3768
Jani Nikula9d1a1032014-03-14 16:51:15 +02003769 if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_DOWNSTREAM_PORT_0,
3770 intel_dp->downstream_ports,
3771 DP_MAX_DOWNSTREAM_PORTS) < 0)
Adam Jacksonedb39242012-09-18 10:58:49 -04003772 return false; /* downstream port status fetch failed */
3773
3774 return true;
Keith Packard92fd8fd2011-07-25 19:50:10 -07003775}
3776
Adam Jackson0d198322012-05-14 16:05:47 -04003777static void
3778intel_dp_probe_oui(struct intel_dp *intel_dp)
3779{
3780 u8 buf[3];
3781
3782 if (!(intel_dp->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
3783 return;
3784
Jani Nikula9d1a1032014-03-14 16:51:15 +02003785 if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_SINK_OUI, buf, 3) == 3)
Adam Jackson0d198322012-05-14 16:05:47 -04003786 DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
3787 buf[0], buf[1], buf[2]);
3788
Jani Nikula9d1a1032014-03-14 16:51:15 +02003789 if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_BRANCH_OUI, buf, 3) == 3)
Adam Jackson0d198322012-05-14 16:05:47 -04003790 DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
3791 buf[0], buf[1], buf[2]);
3792}
3793
Dave Airlie0e32b392014-05-02 14:02:48 +10003794static bool
3795intel_dp_probe_mst(struct intel_dp *intel_dp)
3796{
3797 u8 buf[1];
3798
3799 if (!intel_dp->can_mst)
3800 return false;
3801
3802 if (intel_dp->dpcd[DP_DPCD_REV] < 0x12)
3803 return false;
3804
Dave Airlie0e32b392014-05-02 14:02:48 +10003805 if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_MSTM_CAP, buf, 1)) {
3806 if (buf[0] & DP_MST_CAP) {
3807 DRM_DEBUG_KMS("Sink is MST capable\n");
3808 intel_dp->is_mst = true;
3809 } else {
3810 DRM_DEBUG_KMS("Sink is not MST capable\n");
3811 intel_dp->is_mst = false;
3812 }
3813 }
Dave Airlie0e32b392014-05-02 14:02:48 +10003814
3815 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
3816 return intel_dp->is_mst;
3817}
3818
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003819int intel_dp_sink_crc(struct intel_dp *intel_dp, u8 *crc)
3820{
3821 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3822 struct drm_device *dev = intel_dig_port->base.base.dev;
3823 struct intel_crtc *intel_crtc =
3824 to_intel_crtc(intel_dig_port->base.base.crtc);
Rodrigo Viviad9dc912014-09-16 19:18:12 -04003825 u8 buf;
3826 int test_crc_count;
3827 int attempts = 6;
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003828
Rodrigo Viviad9dc912014-09-16 19:18:12 -04003829 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK_MISC, &buf) < 0)
Rodrigo Vivibda03812014-09-15 19:24:03 -04003830 return -EIO;
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003831
Rodrigo Viviad9dc912014-09-16 19:18:12 -04003832 if (!(buf & DP_TEST_CRC_SUPPORTED))
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003833 return -ENOTTY;
3834
Rodrigo Vivi1dda5f92014-10-01 07:32:37 -07003835 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0)
Rodrigo Vivibda03812014-09-15 19:24:03 -04003836 return -EIO;
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003837
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003838 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
Rodrigo Vivice31d9f2014-09-29 18:29:52 -04003839 buf | DP_TEST_SINK_START) < 0)
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003840 return -EIO;
3841
Rodrigo Vivi1dda5f92014-10-01 07:32:37 -07003842 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK_MISC, &buf) < 0)
3843 return -EIO;
Rodrigo Viviad9dc912014-09-16 19:18:12 -04003844 test_crc_count = buf & DP_TEST_COUNT_MASK;
3845
3846 do {
Rodrigo Vivi1dda5f92014-10-01 07:32:37 -07003847 if (drm_dp_dpcd_readb(&intel_dp->aux,
3848 DP_TEST_SINK_MISC, &buf) < 0)
3849 return -EIO;
Rodrigo Viviad9dc912014-09-16 19:18:12 -04003850 intel_wait_for_vblank(dev, intel_crtc->pipe);
3851 } while (--attempts && (buf & DP_TEST_COUNT_MASK) == test_crc_count);
3852
3853 if (attempts == 0) {
Daniel Vetter90bd1f42014-11-19 11:18:47 +01003854 DRM_DEBUG_KMS("Panel is unable to calculate CRC after 6 vblanks\n");
3855 return -ETIMEDOUT;
Rodrigo Viviad9dc912014-09-16 19:18:12 -04003856 }
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003857
Jani Nikula9d1a1032014-03-14 16:51:15 +02003858 if (drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_CRC_R_CR, crc, 6) < 0)
Rodrigo Vivibda03812014-09-15 19:24:03 -04003859 return -EIO;
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003860
Rodrigo Vivi1dda5f92014-10-01 07:32:37 -07003861 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0)
3862 return -EIO;
3863 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
3864 buf & ~DP_TEST_SINK_START) < 0)
3865 return -EIO;
Rodrigo Vivice31d9f2014-09-29 18:29:52 -04003866
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003867 return 0;
3868}
3869
Jesse Barnesa60f0e32011-10-20 15:09:17 -07003870static bool
3871intel_dp_get_sink_irq(struct intel_dp *intel_dp, u8 *sink_irq_vector)
3872{
Jani Nikula9d1a1032014-03-14 16:51:15 +02003873 return intel_dp_dpcd_read_wake(&intel_dp->aux,
3874 DP_DEVICE_SERVICE_IRQ_VECTOR,
3875 sink_irq_vector, 1) == 1;
Jesse Barnesa60f0e32011-10-20 15:09:17 -07003876}
3877
Dave Airlie0e32b392014-05-02 14:02:48 +10003878static bool
3879intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *sink_irq_vector)
3880{
3881 int ret;
3882
3883 ret = intel_dp_dpcd_read_wake(&intel_dp->aux,
3884 DP_SINK_COUNT_ESI,
3885 sink_irq_vector, 14);
3886 if (ret != 14)
3887 return false;
3888
3889 return true;
3890}
3891
Jesse Barnesa60f0e32011-10-20 15:09:17 -07003892static void
3893intel_dp_handle_test_request(struct intel_dp *intel_dp)
3894{
3895 /* NAK by default */
Jani Nikula9d1a1032014-03-14 16:51:15 +02003896 drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_RESPONSE, DP_TEST_NAK);
Jesse Barnesa60f0e32011-10-20 15:09:17 -07003897}
3898
Dave Airlie0e32b392014-05-02 14:02:48 +10003899static int
3900intel_dp_check_mst_status(struct intel_dp *intel_dp)
3901{
3902 bool bret;
3903
3904 if (intel_dp->is_mst) {
3905 u8 esi[16] = { 0 };
3906 int ret = 0;
3907 int retry;
3908 bool handled;
3909 bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
3910go_again:
3911 if (bret == true) {
3912
3913 /* check link status - esi[10] = 0x200c */
3914 if (intel_dp->active_mst_links && !drm_dp_channel_eq_ok(&esi[10], intel_dp->lane_count)) {
3915 DRM_DEBUG_KMS("channel EQ not ok, retraining\n");
3916 intel_dp_start_link_train(intel_dp);
3917 intel_dp_complete_link_train(intel_dp);
3918 intel_dp_stop_link_train(intel_dp);
3919 }
3920
Andy Shevchenko6f34cc32015-01-15 13:45:09 +02003921 DRM_DEBUG_KMS("got esi %3ph\n", esi);
Dave Airlie0e32b392014-05-02 14:02:48 +10003922 ret = drm_dp_mst_hpd_irq(&intel_dp->mst_mgr, esi, &handled);
3923
3924 if (handled) {
3925 for (retry = 0; retry < 3; retry++) {
3926 int wret;
3927 wret = drm_dp_dpcd_write(&intel_dp->aux,
3928 DP_SINK_COUNT_ESI+1,
3929 &esi[1], 3);
3930 if (wret == 3) {
3931 break;
3932 }
3933 }
3934
3935 bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
3936 if (bret == true) {
Andy Shevchenko6f34cc32015-01-15 13:45:09 +02003937 DRM_DEBUG_KMS("got esi2 %3ph\n", esi);
Dave Airlie0e32b392014-05-02 14:02:48 +10003938 goto go_again;
3939 }
3940 } else
3941 ret = 0;
3942
3943 return ret;
3944 } else {
3945 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3946 DRM_DEBUG_KMS("failed to get ESI - device may have failed\n");
3947 intel_dp->is_mst = false;
3948 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
3949 /* send a hotplug event */
3950 drm_kms_helper_hotplug_event(intel_dig_port->base.base.dev);
3951 }
3952 }
3953 return -EINVAL;
3954}
3955
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003956/*
3957 * According to DP spec
3958 * 5.1.2:
3959 * 1. Read DPCD
3960 * 2. Configure link according to Receiver Capabilities
3961 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
3962 * 4. Check link status on receipt of hot-plug interrupt
3963 */
Damien Lespiaua5146202015-02-10 19:32:22 +00003964static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01003965intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003966{
Dave Airlie5b215bc2014-08-05 10:40:20 +10003967 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Paulo Zanonida63a9f2012-10-26 19:05:46 -02003968 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
Jesse Barnesa60f0e32011-10-20 15:09:17 -07003969 u8 sink_irq_vector;
Keith Packard93f62da2011-11-01 19:45:03 -07003970 u8 link_status[DP_LINK_STATUS_SIZE];
Jesse Barnesa60f0e32011-10-20 15:09:17 -07003971
Dave Airlie5b215bc2014-08-05 10:40:20 +10003972 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
3973
Paulo Zanonida63a9f2012-10-26 19:05:46 -02003974 if (!intel_encoder->connectors_active)
Keith Packardd2b996a2011-07-25 22:37:51 -07003975 return;
Jesse Barnes59cd09e2011-07-07 11:10:59 -07003976
Paulo Zanonida63a9f2012-10-26 19:05:46 -02003977 if (WARN_ON(!intel_encoder->base.crtc))
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003978 return;
3979
Imre Deak1a125d82014-08-18 14:42:46 +03003980 if (!to_intel_crtc(intel_encoder->base.crtc)->active)
3981 return;
3982
Keith Packard92fd8fd2011-07-25 19:50:10 -07003983 /* Try to read receiver status if the link appears to be up */
Keith Packard93f62da2011-11-01 19:45:03 -07003984 if (!intel_dp_get_link_status(intel_dp, link_status)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003985 return;
3986 }
3987
Keith Packard92fd8fd2011-07-25 19:50:10 -07003988 /* Now read the DPCD to see if it's actually running */
Keith Packard26d61aa2011-07-25 20:01:09 -07003989 if (!intel_dp_get_dpcd(intel_dp)) {
Jesse Barnes59cd09e2011-07-07 11:10:59 -07003990 return;
3991 }
3992
Jesse Barnesa60f0e32011-10-20 15:09:17 -07003993 /* Try to read the source of the interrupt */
3994 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
3995 intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
3996 /* Clear interrupt source */
Jani Nikula9d1a1032014-03-14 16:51:15 +02003997 drm_dp_dpcd_writeb(&intel_dp->aux,
3998 DP_DEVICE_SERVICE_IRQ_VECTOR,
3999 sink_irq_vector);
Jesse Barnesa60f0e32011-10-20 15:09:17 -07004000
4001 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
4002 intel_dp_handle_test_request(intel_dp);
4003 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
4004 DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
4005 }
4006
Daniel Vetter1ffdff12012-10-18 10:15:24 +02004007 if (!drm_dp_channel_eq_ok(link_status, intel_dp->lane_count)) {
Keith Packard92fd8fd2011-07-25 19:50:10 -07004008 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
Jani Nikula8e329a02014-06-03 14:56:21 +03004009 intel_encoder->base.name);
Jesse Barnes33a34e42010-09-08 12:42:02 -07004010 intel_dp_start_link_train(intel_dp);
4011 intel_dp_complete_link_train(intel_dp);
Imre Deak3ab9c632013-05-03 12:57:41 +03004012 intel_dp_stop_link_train(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -07004013 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004014}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004015
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004016/* XXX this is probably wrong for multiple downstream ports */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004017static enum drm_connector_status
Keith Packard26d61aa2011-07-25 20:01:09 -07004018intel_dp_detect_dpcd(struct intel_dp *intel_dp)
Adam Jackson71ba90002011-07-12 17:38:04 -04004019{
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004020 uint8_t *dpcd = intel_dp->dpcd;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004021 uint8_t type;
4022
4023 if (!intel_dp_get_dpcd(intel_dp))
4024 return connector_status_disconnected;
4025
4026 /* if there's no downstream port, we're done */
4027 if (!(dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT))
Keith Packard26d61aa2011-07-25 20:01:09 -07004028 return connector_status_connected;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004029
4030 /* If we're HPD-aware, SINK_COUNT changes dynamically */
Jani Nikulac9ff1602013-09-27 14:48:42 +03004031 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
4032 intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
Adam Jackson23235172012-09-20 16:42:45 -04004033 uint8_t reg;
Jani Nikula9d1a1032014-03-14 16:51:15 +02004034
4035 if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_SINK_COUNT,
4036 &reg, 1) < 0)
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004037 return connector_status_unknown;
Jani Nikula9d1a1032014-03-14 16:51:15 +02004038
Adam Jackson23235172012-09-20 16:42:45 -04004039 return DP_GET_SINK_COUNT(reg) ? connector_status_connected
4040 : connector_status_disconnected;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004041 }
4042
4043 /* If no HPD, poke DDC gently */
Jani Nikula0b998362014-03-14 16:51:17 +02004044 if (drm_probe_ddc(&intel_dp->aux.ddc))
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004045 return connector_status_connected;
4046
4047 /* Well we tried, say unknown for unreliable port types */
Jani Nikulac9ff1602013-09-27 14:48:42 +03004048 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
4049 type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
4050 if (type == DP_DS_PORT_TYPE_VGA ||
4051 type == DP_DS_PORT_TYPE_NON_EDID)
4052 return connector_status_unknown;
4053 } else {
4054 type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
4055 DP_DWN_STRM_PORT_TYPE_MASK;
4056 if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
4057 type == DP_DWN_STRM_PORT_TYPE_OTHER)
4058 return connector_status_unknown;
4059 }
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004060
4061 /* Anything else is out of spec, warn and ignore */
4062 DRM_DEBUG_KMS("Broken DP branch device, ignoring\n");
Keith Packard26d61aa2011-07-25 20:01:09 -07004063 return connector_status_disconnected;
Adam Jackson71ba90002011-07-12 17:38:04 -04004064}
4065
4066static enum drm_connector_status
Chris Wilsond410b562014-09-02 20:03:59 +01004067edp_detect(struct intel_dp *intel_dp)
4068{
4069 struct drm_device *dev = intel_dp_to_dev(intel_dp);
4070 enum drm_connector_status status;
4071
4072 status = intel_panel_detect(dev);
4073 if (status == connector_status_unknown)
4074 status = connector_status_connected;
4075
4076 return status;
4077}
4078
4079static enum drm_connector_status
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004080ironlake_dp_detect(struct intel_dp *intel_dp)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004081{
Paulo Zanoni30add222012-10-26 19:05:45 -02004082 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Damien Lespiau1b469632012-12-13 16:09:01 +00004083 struct drm_i915_private *dev_priv = dev->dev_private;
4084 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07004085
Damien Lespiau1b469632012-12-13 16:09:01 +00004086 if (!ibx_digital_port_connected(dev_priv, intel_dig_port))
4087 return connector_status_disconnected;
4088
Keith Packard26d61aa2011-07-25 20:01:09 -07004089 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004090}
4091
Dave Airlie2a592be2014-09-01 16:58:12 +10004092static int g4x_digital_port_connected(struct drm_device *dev,
4093 struct intel_digital_port *intel_dig_port)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004094{
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004095 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson10f76a32012-05-11 18:01:32 +01004096 uint32_t bit;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004097
Todd Previte232a6ee2014-01-23 00:13:41 -07004098 if (IS_VALLEYVIEW(dev)) {
4099 switch (intel_dig_port->port) {
4100 case PORT_B:
4101 bit = PORTB_HOTPLUG_LIVE_STATUS_VLV;
4102 break;
4103 case PORT_C:
4104 bit = PORTC_HOTPLUG_LIVE_STATUS_VLV;
4105 break;
4106 case PORT_D:
4107 bit = PORTD_HOTPLUG_LIVE_STATUS_VLV;
4108 break;
4109 default:
Dave Airlie2a592be2014-09-01 16:58:12 +10004110 return -EINVAL;
Todd Previte232a6ee2014-01-23 00:13:41 -07004111 }
4112 } else {
4113 switch (intel_dig_port->port) {
4114 case PORT_B:
4115 bit = PORTB_HOTPLUG_LIVE_STATUS_G4X;
4116 break;
4117 case PORT_C:
4118 bit = PORTC_HOTPLUG_LIVE_STATUS_G4X;
4119 break;
4120 case PORT_D:
4121 bit = PORTD_HOTPLUG_LIVE_STATUS_G4X;
4122 break;
4123 default:
Dave Airlie2a592be2014-09-01 16:58:12 +10004124 return -EINVAL;
Todd Previte232a6ee2014-01-23 00:13:41 -07004125 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004126 }
4127
Chris Wilson10f76a32012-05-11 18:01:32 +01004128 if ((I915_READ(PORT_HOTPLUG_STAT) & bit) == 0)
Dave Airlie2a592be2014-09-01 16:58:12 +10004129 return 0;
4130 return 1;
4131}
4132
4133static enum drm_connector_status
4134g4x_dp_detect(struct intel_dp *intel_dp)
4135{
4136 struct drm_device *dev = intel_dp_to_dev(intel_dp);
4137 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4138 int ret;
4139
4140 /* Can't disconnect eDP, but you can close the lid... */
4141 if (is_edp(intel_dp)) {
4142 enum drm_connector_status status;
4143
4144 status = intel_panel_detect(dev);
4145 if (status == connector_status_unknown)
4146 status = connector_status_connected;
4147 return status;
4148 }
4149
4150 ret = g4x_digital_port_connected(dev, intel_dig_port);
4151 if (ret == -EINVAL)
4152 return connector_status_unknown;
4153 else if (ret == 0)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004154 return connector_status_disconnected;
4155
Keith Packard26d61aa2011-07-25 20:01:09 -07004156 return intel_dp_detect_dpcd(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004157}
4158
Keith Packard8c241fe2011-09-28 16:38:44 -07004159static struct edid *
Chris Wilsonbeb60602014-09-02 20:04:00 +01004160intel_dp_get_edid(struct intel_dp *intel_dp)
Keith Packard8c241fe2011-09-28 16:38:44 -07004161{
Chris Wilsonbeb60602014-09-02 20:04:00 +01004162 struct intel_connector *intel_connector = intel_dp->attached_connector;
Keith Packard8c241fe2011-09-28 16:38:44 -07004163
Jani Nikula9cd300e2012-10-19 14:51:52 +03004164 /* use cached edid if we have one */
4165 if (intel_connector->edid) {
Jani Nikula9cd300e2012-10-19 14:51:52 +03004166 /* invalid edid */
4167 if (IS_ERR(intel_connector->edid))
Jesse Barnesd6f24d02012-06-14 15:28:33 -04004168 return NULL;
4169
Jani Nikula55e9ede2013-10-01 10:38:54 +03004170 return drm_edid_duplicate(intel_connector->edid);
Chris Wilsonbeb60602014-09-02 20:04:00 +01004171 } else
4172 return drm_get_edid(&intel_connector->base,
4173 &intel_dp->aux.ddc);
Keith Packard8c241fe2011-09-28 16:38:44 -07004174}
4175
Chris Wilsonbeb60602014-09-02 20:04:00 +01004176static void
4177intel_dp_set_edid(struct intel_dp *intel_dp)
Keith Packard8c241fe2011-09-28 16:38:44 -07004178{
Chris Wilsonbeb60602014-09-02 20:04:00 +01004179 struct intel_connector *intel_connector = intel_dp->attached_connector;
4180 struct edid *edid;
Keith Packard8c241fe2011-09-28 16:38:44 -07004181
Chris Wilsonbeb60602014-09-02 20:04:00 +01004182 edid = intel_dp_get_edid(intel_dp);
4183 intel_connector->detect_edid = edid;
Jani Nikula9cd300e2012-10-19 14:51:52 +03004184
Chris Wilsonbeb60602014-09-02 20:04:00 +01004185 if (intel_dp->force_audio != HDMI_AUDIO_AUTO)
4186 intel_dp->has_audio = intel_dp->force_audio == HDMI_AUDIO_ON;
4187 else
4188 intel_dp->has_audio = drm_detect_monitor_audio(edid);
4189}
Jesse Barnesd6f24d02012-06-14 15:28:33 -04004190
Chris Wilsonbeb60602014-09-02 20:04:00 +01004191static void
4192intel_dp_unset_edid(struct intel_dp *intel_dp)
4193{
4194 struct intel_connector *intel_connector = intel_dp->attached_connector;
4195
4196 kfree(intel_connector->detect_edid);
4197 intel_connector->detect_edid = NULL;
4198
4199 intel_dp->has_audio = false;
4200}
4201
4202static enum intel_display_power_domain
4203intel_dp_power_get(struct intel_dp *dp)
4204{
4205 struct intel_encoder *encoder = &dp_to_dig_port(dp)->base;
4206 enum intel_display_power_domain power_domain;
4207
4208 power_domain = intel_display_port_power_domain(encoder);
4209 intel_display_power_get(to_i915(encoder->base.dev), power_domain);
4210
4211 return power_domain;
4212}
4213
4214static void
4215intel_dp_power_put(struct intel_dp *dp,
4216 enum intel_display_power_domain power_domain)
4217{
4218 struct intel_encoder *encoder = &dp_to_dig_port(dp)->base;
4219 intel_display_power_put(to_i915(encoder->base.dev), power_domain);
Keith Packard8c241fe2011-09-28 16:38:44 -07004220}
4221
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004222static enum drm_connector_status
4223intel_dp_detect(struct drm_connector *connector, bool force)
4224{
4225 struct intel_dp *intel_dp = intel_attached_dp(connector);
Paulo Zanonid63885d2012-10-26 19:05:49 -02004226 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4227 struct intel_encoder *intel_encoder = &intel_dig_port->base;
Paulo Zanonifa90ece2012-10-26 19:05:44 -02004228 struct drm_device *dev = connector->dev;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004229 enum drm_connector_status status;
Imre Deak671dedd2014-03-05 16:20:53 +02004230 enum intel_display_power_domain power_domain;
Dave Airlie0e32b392014-05-02 14:02:48 +10004231 bool ret;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004232
Chris Wilson164c8592013-07-20 20:27:08 +01004233 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03004234 connector->base.id, connector->name);
Chris Wilsonbeb60602014-09-02 20:04:00 +01004235 intel_dp_unset_edid(intel_dp);
Chris Wilson164c8592013-07-20 20:27:08 +01004236
Dave Airlie0e32b392014-05-02 14:02:48 +10004237 if (intel_dp->is_mst) {
4238 /* MST devices are disconnected from a monitor POV */
4239 if (intel_encoder->type != INTEL_OUTPUT_EDP)
4240 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
Chris Wilsonbeb60602014-09-02 20:04:00 +01004241 return connector_status_disconnected;
Dave Airlie0e32b392014-05-02 14:02:48 +10004242 }
4243
Chris Wilsonbeb60602014-09-02 20:04:00 +01004244 power_domain = intel_dp_power_get(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004245
Chris Wilsond410b562014-09-02 20:03:59 +01004246 /* Can't disconnect eDP, but you can close the lid... */
4247 if (is_edp(intel_dp))
4248 status = edp_detect(intel_dp);
4249 else if (HAS_PCH_SPLIT(dev))
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004250 status = ironlake_dp_detect(intel_dp);
4251 else
4252 status = g4x_dp_detect(intel_dp);
4253 if (status != connector_status_connected)
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02004254 goto out;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004255
Adam Jackson0d198322012-05-14 16:05:47 -04004256 intel_dp_probe_oui(intel_dp);
4257
Dave Airlie0e32b392014-05-02 14:02:48 +10004258 ret = intel_dp_probe_mst(intel_dp);
4259 if (ret) {
4260 /* if we are in MST mode then this connector
4261 won't appear connected or have anything with EDID on it */
4262 if (intel_encoder->type != INTEL_OUTPUT_EDP)
4263 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
4264 status = connector_status_disconnected;
4265 goto out;
4266 }
4267
Chris Wilsonbeb60602014-09-02 20:04:00 +01004268 intel_dp_set_edid(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004269
Paulo Zanonid63885d2012-10-26 19:05:49 -02004270 if (intel_encoder->type != INTEL_OUTPUT_EDP)
4271 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02004272 status = connector_status_connected;
4273
4274out:
Chris Wilsonbeb60602014-09-02 20:04:00 +01004275 intel_dp_power_put(intel_dp, power_domain);
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02004276 return status;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004277}
4278
Chris Wilsonbeb60602014-09-02 20:04:00 +01004279static void
4280intel_dp_force(struct drm_connector *connector)
4281{
4282 struct intel_dp *intel_dp = intel_attached_dp(connector);
4283 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
4284 enum intel_display_power_domain power_domain;
4285
4286 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4287 connector->base.id, connector->name);
4288 intel_dp_unset_edid(intel_dp);
4289
4290 if (connector->status != connector_status_connected)
4291 return;
4292
4293 power_domain = intel_dp_power_get(intel_dp);
4294
4295 intel_dp_set_edid(intel_dp);
4296
4297 intel_dp_power_put(intel_dp, power_domain);
4298
4299 if (intel_encoder->type != INTEL_OUTPUT_EDP)
4300 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
4301}
4302
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004303static int intel_dp_get_modes(struct drm_connector *connector)
4304{
Jani Nikuladd06f902012-10-19 14:51:50 +03004305 struct intel_connector *intel_connector = to_intel_connector(connector);
Chris Wilsonbeb60602014-09-02 20:04:00 +01004306 struct edid *edid;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004307
Chris Wilsonbeb60602014-09-02 20:04:00 +01004308 edid = intel_connector->detect_edid;
4309 if (edid) {
4310 int ret = intel_connector_update_modes(connector, edid);
4311 if (ret)
4312 return ret;
4313 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004314
Jani Nikulaf8779fd2012-10-19 14:51:48 +03004315 /* if eDP has no EDID, fall back to fixed mode */
Chris Wilsonbeb60602014-09-02 20:04:00 +01004316 if (is_edp(intel_attached_dp(connector)) &&
4317 intel_connector->panel.fixed_mode) {
Jani Nikulaf8779fd2012-10-19 14:51:48 +03004318 struct drm_display_mode *mode;
Chris Wilsonbeb60602014-09-02 20:04:00 +01004319
4320 mode = drm_mode_duplicate(connector->dev,
Jani Nikuladd06f902012-10-19 14:51:50 +03004321 intel_connector->panel.fixed_mode);
Jani Nikulaf8779fd2012-10-19 14:51:48 +03004322 if (mode) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004323 drm_mode_probed_add(connector, mode);
4324 return 1;
4325 }
4326 }
Chris Wilsonbeb60602014-09-02 20:04:00 +01004327
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004328 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004329}
4330
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004331static bool
4332intel_dp_detect_audio(struct drm_connector *connector)
4333{
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004334 bool has_audio = false;
Chris Wilsonbeb60602014-09-02 20:04:00 +01004335 struct edid *edid;
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004336
Chris Wilsonbeb60602014-09-02 20:04:00 +01004337 edid = to_intel_connector(connector)->detect_edid;
4338 if (edid)
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004339 has_audio = drm_detect_monitor_audio(edid);
Imre Deak671dedd2014-03-05 16:20:53 +02004340
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004341 return has_audio;
4342}
4343
Chris Wilsonf6849602010-09-19 09:29:33 +01004344static int
4345intel_dp_set_property(struct drm_connector *connector,
4346 struct drm_property *property,
4347 uint64_t val)
4348{
Chris Wilsone953fd72011-02-21 22:23:52 +00004349 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Yuly Novikov53b41832012-10-26 12:04:00 +03004350 struct intel_connector *intel_connector = to_intel_connector(connector);
Paulo Zanonida63a9f2012-10-26 19:05:46 -02004351 struct intel_encoder *intel_encoder = intel_attached_encoder(connector);
4352 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
Chris Wilsonf6849602010-09-19 09:29:33 +01004353 int ret;
4354
Rob Clark662595d2012-10-11 20:36:04 -05004355 ret = drm_object_property_set_value(&connector->base, property, val);
Chris Wilsonf6849602010-09-19 09:29:33 +01004356 if (ret)
4357 return ret;
4358
Chris Wilson3f43c482011-05-12 22:17:24 +01004359 if (property == dev_priv->force_audio_property) {
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004360 int i = val;
4361 bool has_audio;
4362
4363 if (i == intel_dp->force_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01004364 return 0;
4365
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004366 intel_dp->force_audio = i;
Chris Wilsonf6849602010-09-19 09:29:33 +01004367
Daniel Vetterc3e5f672012-02-23 17:14:47 +01004368 if (i == HDMI_AUDIO_AUTO)
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004369 has_audio = intel_dp_detect_audio(connector);
4370 else
Daniel Vetterc3e5f672012-02-23 17:14:47 +01004371 has_audio = (i == HDMI_AUDIO_ON);
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004372
4373 if (has_audio == intel_dp->has_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01004374 return 0;
4375
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004376 intel_dp->has_audio = has_audio;
Chris Wilsonf6849602010-09-19 09:29:33 +01004377 goto done;
4378 }
4379
Chris Wilsone953fd72011-02-21 22:23:52 +00004380 if (property == dev_priv->broadcast_rgb_property) {
Daniel Vetterae4edb82013-04-22 17:07:23 +02004381 bool old_auto = intel_dp->color_range_auto;
4382 uint32_t old_range = intel_dp->color_range;
4383
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02004384 switch (val) {
4385 case INTEL_BROADCAST_RGB_AUTO:
4386 intel_dp->color_range_auto = true;
4387 break;
4388 case INTEL_BROADCAST_RGB_FULL:
4389 intel_dp->color_range_auto = false;
4390 intel_dp->color_range = 0;
4391 break;
4392 case INTEL_BROADCAST_RGB_LIMITED:
4393 intel_dp->color_range_auto = false;
4394 intel_dp->color_range = DP_COLOR_RANGE_16_235;
4395 break;
4396 default:
4397 return -EINVAL;
4398 }
Daniel Vetterae4edb82013-04-22 17:07:23 +02004399
4400 if (old_auto == intel_dp->color_range_auto &&
4401 old_range == intel_dp->color_range)
4402 return 0;
4403
Chris Wilsone953fd72011-02-21 22:23:52 +00004404 goto done;
4405 }
4406
Yuly Novikov53b41832012-10-26 12:04:00 +03004407 if (is_edp(intel_dp) &&
4408 property == connector->dev->mode_config.scaling_mode_property) {
4409 if (val == DRM_MODE_SCALE_NONE) {
4410 DRM_DEBUG_KMS("no scaling not supported\n");
4411 return -EINVAL;
4412 }
4413
4414 if (intel_connector->panel.fitting_mode == val) {
4415 /* the eDP scaling property is not changed */
4416 return 0;
4417 }
4418 intel_connector->panel.fitting_mode = val;
4419
4420 goto done;
4421 }
4422
Chris Wilsonf6849602010-09-19 09:29:33 +01004423 return -EINVAL;
4424
4425done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00004426 if (intel_encoder->base.crtc)
4427 intel_crtc_restore_mode(intel_encoder->base.crtc);
Chris Wilsonf6849602010-09-19 09:29:33 +01004428
4429 return 0;
4430}
4431
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004432static void
Paulo Zanoni73845ad2013-06-12 17:27:30 -03004433intel_dp_connector_destroy(struct drm_connector *connector)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004434{
Jani Nikula1d508702012-10-19 14:51:49 +03004435 struct intel_connector *intel_connector = to_intel_connector(connector);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02004436
Chris Wilson10e972d2014-09-04 21:43:45 +01004437 kfree(intel_connector->detect_edid);
Chris Wilsonbeb60602014-09-02 20:04:00 +01004438
Jani Nikula9cd300e2012-10-19 14:51:52 +03004439 if (!IS_ERR_OR_NULL(intel_connector->edid))
4440 kfree(intel_connector->edid);
4441
Paulo Zanoniacd8db102013-06-12 17:27:23 -03004442 /* Can't call is_edp() since the encoder may have been destroyed
4443 * already. */
4444 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
Jani Nikula1d508702012-10-19 14:51:49 +03004445 intel_panel_fini(&intel_connector->panel);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02004446
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004447 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08004448 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004449}
4450
Paulo Zanoni00c09d72012-10-26 19:05:52 -02004451void intel_dp_encoder_destroy(struct drm_encoder *encoder)
Daniel Vetter24d05922010-08-20 18:08:28 +02004452{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02004453 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
4454 struct intel_dp *intel_dp = &intel_dig_port->dp;
Daniel Vetter24d05922010-08-20 18:08:28 +02004455
Dave Airlie4f71d0c2014-06-04 16:02:28 +10004456 drm_dp_aux_unregister(&intel_dp->aux);
Dave Airlie0e32b392014-05-02 14:02:48 +10004457 intel_dp_mst_encoder_cleanup(intel_dig_port);
Keith Packardbd943152011-09-18 23:09:52 -07004458 if (is_edp(intel_dp)) {
4459 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
Ville Syrjälä951468f2014-09-04 14:55:31 +03004460 /*
4461 * vdd might still be enabled do to the delayed vdd off.
4462 * Make sure vdd is actually turned off here.
4463 */
Ville Syrjälä773538e82014-09-04 14:54:56 +03004464 pps_lock(intel_dp);
Daniel Vetter4be73782014-01-17 14:39:48 +01004465 edp_panel_vdd_off_sync(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03004466 pps_unlock(intel_dp);
4467
Clint Taylor01527b32014-07-07 13:01:46 -07004468 if (intel_dp->edp_notifier.notifier_call) {
4469 unregister_reboot_notifier(&intel_dp->edp_notifier);
4470 intel_dp->edp_notifier.notifier_call = NULL;
4471 }
Keith Packardbd943152011-09-18 23:09:52 -07004472 }
Imre Deakc8bd0e42014-12-12 17:57:38 +02004473 drm_encoder_cleanup(encoder);
Paulo Zanonida63a9f2012-10-26 19:05:46 -02004474 kfree(intel_dig_port);
Daniel Vetter24d05922010-08-20 18:08:28 +02004475}
4476
Imre Deak07f9cd02014-08-18 14:42:45 +03004477static void intel_dp_encoder_suspend(struct intel_encoder *intel_encoder)
4478{
4479 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
4480
4481 if (!is_edp(intel_dp))
4482 return;
4483
Ville Syrjälä951468f2014-09-04 14:55:31 +03004484 /*
4485 * vdd might still be enabled do to the delayed vdd off.
4486 * Make sure vdd is actually turned off here.
4487 */
Ville Syrjäläafa4e532014-11-25 15:43:48 +02004488 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
Ville Syrjälä773538e82014-09-04 14:54:56 +03004489 pps_lock(intel_dp);
Imre Deak07f9cd02014-08-18 14:42:45 +03004490 edp_panel_vdd_off_sync(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03004491 pps_unlock(intel_dp);
Imre Deak07f9cd02014-08-18 14:42:45 +03004492}
4493
Ville Syrjälä49e6bc52014-10-28 16:15:52 +02004494static void intel_edp_panel_vdd_sanitize(struct intel_dp *intel_dp)
4495{
4496 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4497 struct drm_device *dev = intel_dig_port->base.base.dev;
4498 struct drm_i915_private *dev_priv = dev->dev_private;
4499 enum intel_display_power_domain power_domain;
4500
4501 lockdep_assert_held(&dev_priv->pps_mutex);
4502
4503 if (!edp_have_panel_vdd(intel_dp))
4504 return;
4505
4506 /*
4507 * The VDD bit needs a power domain reference, so if the bit is
4508 * already enabled when we boot or resume, grab this reference and
4509 * schedule a vdd off, so we don't hold on to the reference
4510 * indefinitely.
4511 */
4512 DRM_DEBUG_KMS("VDD left on by BIOS, adjusting state tracking\n");
4513 power_domain = intel_display_port_power_domain(&intel_dig_port->base);
4514 intel_display_power_get(dev_priv, power_domain);
4515
4516 edp_panel_vdd_schedule_off(intel_dp);
4517}
4518
Imre Deak6d93c0c2014-07-31 14:03:36 +03004519static void intel_dp_encoder_reset(struct drm_encoder *encoder)
4520{
Ville Syrjälä49e6bc52014-10-28 16:15:52 +02004521 struct intel_dp *intel_dp;
4522
4523 if (to_intel_encoder(encoder)->type != INTEL_OUTPUT_EDP)
4524 return;
4525
4526 intel_dp = enc_to_intel_dp(encoder);
4527
4528 pps_lock(intel_dp);
4529
4530 /*
4531 * Read out the current power sequencer assignment,
4532 * in case the BIOS did something with it.
4533 */
4534 if (IS_VALLEYVIEW(encoder->dev))
4535 vlv_initial_power_sequencer_setup(intel_dp);
4536
4537 intel_edp_panel_vdd_sanitize(intel_dp);
4538
4539 pps_unlock(intel_dp);
Imre Deak6d93c0c2014-07-31 14:03:36 +03004540}
4541
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004542static const struct drm_connector_funcs intel_dp_connector_funcs = {
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02004543 .dpms = intel_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004544 .detect = intel_dp_detect,
Chris Wilsonbeb60602014-09-02 20:04:00 +01004545 .force = intel_dp_force,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004546 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilsonf6849602010-09-19 09:29:33 +01004547 .set_property = intel_dp_set_property,
Matt Roper2545e4a2015-01-22 16:51:27 -08004548 .atomic_get_property = intel_connector_atomic_get_property,
Paulo Zanoni73845ad2013-06-12 17:27:30 -03004549 .destroy = intel_dp_connector_destroy,
Matt Roperc6f95f22015-01-22 16:50:32 -08004550 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004551};
4552
4553static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
4554 .get_modes = intel_dp_get_modes,
4555 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01004556 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004557};
4558
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004559static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Imre Deak6d93c0c2014-07-31 14:03:36 +03004560 .reset = intel_dp_encoder_reset,
Daniel Vetter24d05922010-08-20 18:08:28 +02004561 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004562};
4563
Dave Airlie0e32b392014-05-02 14:02:48 +10004564void
Eric Anholt21d40d32010-03-25 11:11:14 -07004565intel_dp_hot_plug(struct intel_encoder *intel_encoder)
Keith Packardc8110e52009-05-06 11:51:10 -07004566{
Dave Airlie0e32b392014-05-02 14:02:48 +10004567 return;
Keith Packardc8110e52009-05-06 11:51:10 -07004568}
4569
Daniel Vetterb2c5c182015-01-23 06:00:31 +01004570enum irqreturn
Dave Airlie13cf5502014-06-18 11:29:35 +10004571intel_dp_hpd_pulse(struct intel_digital_port *intel_dig_port, bool long_hpd)
4572{
4573 struct intel_dp *intel_dp = &intel_dig_port->dp;
Imre Deak1c767b32014-08-18 14:42:42 +03004574 struct intel_encoder *intel_encoder = &intel_dig_port->base;
Dave Airlie0e32b392014-05-02 14:02:48 +10004575 struct drm_device *dev = intel_dig_port->base.base.dev;
4576 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak1c767b32014-08-18 14:42:42 +03004577 enum intel_display_power_domain power_domain;
Daniel Vetterb2c5c182015-01-23 06:00:31 +01004578 enum irqreturn ret = IRQ_NONE;
Imre Deak1c767b32014-08-18 14:42:42 +03004579
Dave Airlie0e32b392014-05-02 14:02:48 +10004580 if (intel_dig_port->base.type != INTEL_OUTPUT_EDP)
4581 intel_dig_port->base.type = INTEL_OUTPUT_DISPLAYPORT;
Dave Airlie13cf5502014-06-18 11:29:35 +10004582
Ville Syrjälä7a7f84c2014-10-16 20:46:10 +03004583 if (long_hpd && intel_dig_port->base.type == INTEL_OUTPUT_EDP) {
4584 /*
4585 * vdd off can generate a long pulse on eDP which
4586 * would require vdd on to handle it, and thus we
4587 * would end up in an endless cycle of
4588 * "vdd off -> long hpd -> vdd on -> detect -> vdd off -> ..."
4589 */
4590 DRM_DEBUG_KMS("ignoring long hpd on eDP port %c\n",
4591 port_name(intel_dig_port->port));
Ville Syrjäläa8b3d522015-02-10 14:11:46 +02004592 return IRQ_HANDLED;
Ville Syrjälä7a7f84c2014-10-16 20:46:10 +03004593 }
4594
Ville Syrjälä26fbb772014-08-11 18:37:37 +03004595 DRM_DEBUG_KMS("got hpd irq on port %c - %s\n",
4596 port_name(intel_dig_port->port),
Dave Airlie0e32b392014-05-02 14:02:48 +10004597 long_hpd ? "long" : "short");
Dave Airlie13cf5502014-06-18 11:29:35 +10004598
Imre Deak1c767b32014-08-18 14:42:42 +03004599 power_domain = intel_display_port_power_domain(intel_encoder);
4600 intel_display_power_get(dev_priv, power_domain);
4601
Dave Airlie0e32b392014-05-02 14:02:48 +10004602 if (long_hpd) {
Dave Airlie2a592be2014-09-01 16:58:12 +10004603
4604 if (HAS_PCH_SPLIT(dev)) {
4605 if (!ibx_digital_port_connected(dev_priv, intel_dig_port))
4606 goto mst_fail;
4607 } else {
4608 if (g4x_digital_port_connected(dev, intel_dig_port) != 1)
4609 goto mst_fail;
4610 }
Dave Airlie0e32b392014-05-02 14:02:48 +10004611
4612 if (!intel_dp_get_dpcd(intel_dp)) {
4613 goto mst_fail;
4614 }
4615
4616 intel_dp_probe_oui(intel_dp);
4617
4618 if (!intel_dp_probe_mst(intel_dp))
4619 goto mst_fail;
4620
4621 } else {
4622 if (intel_dp->is_mst) {
Imre Deak1c767b32014-08-18 14:42:42 +03004623 if (intel_dp_check_mst_status(intel_dp) == -EINVAL)
Dave Airlie0e32b392014-05-02 14:02:48 +10004624 goto mst_fail;
4625 }
4626
4627 if (!intel_dp->is_mst) {
4628 /*
4629 * we'll check the link status via the normal hot plug path later -
4630 * but for short hpds we should check it now
4631 */
Dave Airlie5b215bc2014-08-05 10:40:20 +10004632 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlie0e32b392014-05-02 14:02:48 +10004633 intel_dp_check_link_status(intel_dp);
Dave Airlie5b215bc2014-08-05 10:40:20 +10004634 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlie0e32b392014-05-02 14:02:48 +10004635 }
4636 }
Daniel Vetterb2c5c182015-01-23 06:00:31 +01004637
4638 ret = IRQ_HANDLED;
4639
Imre Deak1c767b32014-08-18 14:42:42 +03004640 goto put_power;
Dave Airlie0e32b392014-05-02 14:02:48 +10004641mst_fail:
4642 /* if we were in MST mode, and device is not there get out of MST mode */
4643 if (intel_dp->is_mst) {
4644 DRM_DEBUG_KMS("MST device may have disappeared %d vs %d\n", intel_dp->is_mst, intel_dp->mst_mgr.mst_state);
4645 intel_dp->is_mst = false;
4646 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
4647 }
Imre Deak1c767b32014-08-18 14:42:42 +03004648put_power:
4649 intel_display_power_put(dev_priv, power_domain);
4650
4651 return ret;
Dave Airlie13cf5502014-06-18 11:29:35 +10004652}
4653
Zhenyu Wange3421a12010-04-08 09:43:27 +08004654/* Return which DP Port should be selected for Transcoder DP control */
4655int
Akshay Joshi0206e352011-08-16 15:34:10 -04004656intel_trans_dp_port_sel(struct drm_crtc *crtc)
Zhenyu Wange3421a12010-04-08 09:43:27 +08004657{
4658 struct drm_device *dev = crtc->dev;
Paulo Zanonifa90ece2012-10-26 19:05:44 -02004659 struct intel_encoder *intel_encoder;
4660 struct intel_dp *intel_dp;
Zhenyu Wange3421a12010-04-08 09:43:27 +08004661
Paulo Zanonifa90ece2012-10-26 19:05:44 -02004662 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
4663 intel_dp = enc_to_intel_dp(&intel_encoder->base);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004664
Paulo Zanonifa90ece2012-10-26 19:05:44 -02004665 if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4666 intel_encoder->type == INTEL_OUTPUT_EDP)
Chris Wilsonea5b2132010-08-04 13:50:23 +01004667 return intel_dp->output_reg;
Zhenyu Wange3421a12010-04-08 09:43:27 +08004668 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01004669
Zhenyu Wange3421a12010-04-08 09:43:27 +08004670 return -1;
4671}
4672
Zhao Yakui36e83a12010-06-12 14:32:21 +08004673/* check the VBT to see whether the eDP is on DP-D port */
Ville Syrjälä5d8a7752013-11-01 18:22:39 +02004674bool intel_dp_is_edp(struct drm_device *dev, enum port port)
Zhao Yakui36e83a12010-06-12 14:32:21 +08004675{
4676 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni768f69c2013-09-11 18:02:47 -03004677 union child_device_config *p_child;
Zhao Yakui36e83a12010-06-12 14:32:21 +08004678 int i;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +02004679 static const short port_mapping[] = {
4680 [PORT_B] = PORT_IDPB,
4681 [PORT_C] = PORT_IDPC,
4682 [PORT_D] = PORT_IDPD,
4683 };
Zhao Yakui36e83a12010-06-12 14:32:21 +08004684
Ville Syrjälä3b32a352013-11-01 18:22:41 +02004685 if (port == PORT_A)
4686 return true;
4687
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004688 if (!dev_priv->vbt.child_dev_num)
Zhao Yakui36e83a12010-06-12 14:32:21 +08004689 return false;
4690
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004691 for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
4692 p_child = dev_priv->vbt.child_dev + i;
Zhao Yakui36e83a12010-06-12 14:32:21 +08004693
Ville Syrjälä5d8a7752013-11-01 18:22:39 +02004694 if (p_child->common.dvo_port == port_mapping[port] &&
Ville Syrjäläf02586d2013-11-01 20:32:08 +02004695 (p_child->common.device_type & DEVICE_TYPE_eDP_BITS) ==
4696 (DEVICE_TYPE_eDP & DEVICE_TYPE_eDP_BITS))
Zhao Yakui36e83a12010-06-12 14:32:21 +08004697 return true;
4698 }
4699 return false;
4700}
4701
Dave Airlie0e32b392014-05-02 14:02:48 +10004702void
Chris Wilsonf6849602010-09-19 09:29:33 +01004703intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
4704{
Yuly Novikov53b41832012-10-26 12:04:00 +03004705 struct intel_connector *intel_connector = to_intel_connector(connector);
4706
Chris Wilson3f43c482011-05-12 22:17:24 +01004707 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +00004708 intel_attach_broadcast_rgb_property(connector);
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02004709 intel_dp->color_range_auto = true;
Yuly Novikov53b41832012-10-26 12:04:00 +03004710
4711 if (is_edp(intel_dp)) {
4712 drm_mode_create_scaling_mode_property(connector->dev);
Rob Clark6de6d842012-10-11 20:36:04 -05004713 drm_object_attach_property(
4714 &connector->base,
Yuly Novikov53b41832012-10-26 12:04:00 +03004715 connector->dev->mode_config.scaling_mode_property,
Yuly Novikov8e740cd2012-10-26 12:04:01 +03004716 DRM_MODE_SCALE_ASPECT);
4717 intel_connector->panel.fitting_mode = DRM_MODE_SCALE_ASPECT;
Yuly Novikov53b41832012-10-26 12:04:00 +03004718 }
Chris Wilsonf6849602010-09-19 09:29:33 +01004719}
4720
Imre Deakdada1a92014-01-29 13:25:41 +02004721static void intel_dp_init_panel_power_timestamps(struct intel_dp *intel_dp)
4722{
4723 intel_dp->last_power_cycle = jiffies;
4724 intel_dp->last_power_on = jiffies;
4725 intel_dp->last_backlight_off = jiffies;
4726}
4727
Daniel Vetter67a54562012-10-20 20:57:45 +02004728static void
4729intel_dp_init_panel_power_sequencer(struct drm_device *dev,
Ville Syrjälä36b5f422014-10-16 21:27:30 +03004730 struct intel_dp *intel_dp)
Daniel Vetter67a54562012-10-20 20:57:45 +02004731{
4732 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä36b5f422014-10-16 21:27:30 +03004733 struct edp_power_seq cur, vbt, spec,
4734 *final = &intel_dp->pps_delays;
Daniel Vetter67a54562012-10-20 20:57:45 +02004735 u32 pp_on, pp_off, pp_div, pp;
Jani Nikulabf13e812013-09-06 07:40:05 +03004736 int pp_ctrl_reg, pp_on_reg, pp_off_reg, pp_div_reg;
Jesse Barnes453c5422013-03-28 09:55:41 -07004737
Ville Syrjäläe39b9992014-09-04 14:53:14 +03004738 lockdep_assert_held(&dev_priv->pps_mutex);
4739
Ville Syrjälä81ddbc62014-10-16 21:27:31 +03004740 /* already initialized? */
4741 if (final->t11_t12 != 0)
4742 return;
4743
Jesse Barnes453c5422013-03-28 09:55:41 -07004744 if (HAS_PCH_SPLIT(dev)) {
Jani Nikulabf13e812013-09-06 07:40:05 +03004745 pp_ctrl_reg = PCH_PP_CONTROL;
Jesse Barnes453c5422013-03-28 09:55:41 -07004746 pp_on_reg = PCH_PP_ON_DELAYS;
4747 pp_off_reg = PCH_PP_OFF_DELAYS;
4748 pp_div_reg = PCH_PP_DIVISOR;
4749 } else {
Jani Nikulabf13e812013-09-06 07:40:05 +03004750 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
4751
4752 pp_ctrl_reg = VLV_PIPE_PP_CONTROL(pipe);
4753 pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
4754 pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
4755 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
Jesse Barnes453c5422013-03-28 09:55:41 -07004756 }
Daniel Vetter67a54562012-10-20 20:57:45 +02004757
4758 /* Workaround: Need to write PP_CONTROL with the unlock key as
4759 * the very first thing. */
Jesse Barnes453c5422013-03-28 09:55:41 -07004760 pp = ironlake_get_pp_control(intel_dp);
Jani Nikulabf13e812013-09-06 07:40:05 +03004761 I915_WRITE(pp_ctrl_reg, pp);
Daniel Vetter67a54562012-10-20 20:57:45 +02004762
Jesse Barnes453c5422013-03-28 09:55:41 -07004763 pp_on = I915_READ(pp_on_reg);
4764 pp_off = I915_READ(pp_off_reg);
4765 pp_div = I915_READ(pp_div_reg);
Daniel Vetter67a54562012-10-20 20:57:45 +02004766
4767 /* Pull timing values out of registers */
4768 cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
4769 PANEL_POWER_UP_DELAY_SHIFT;
4770
4771 cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
4772 PANEL_LIGHT_ON_DELAY_SHIFT;
4773
4774 cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
4775 PANEL_LIGHT_OFF_DELAY_SHIFT;
4776
4777 cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
4778 PANEL_POWER_DOWN_DELAY_SHIFT;
4779
4780 cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
4781 PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
4782
4783 DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
4784 cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
4785
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004786 vbt = dev_priv->vbt.edp_pps;
Daniel Vetter67a54562012-10-20 20:57:45 +02004787
4788 /* Upper limits from eDP 1.3 spec. Note that we use the clunky units of
4789 * our hw here, which are all in 100usec. */
4790 spec.t1_t3 = 210 * 10;
4791 spec.t8 = 50 * 10; /* no limit for t8, use t7 instead */
4792 spec.t9 = 50 * 10; /* no limit for t9, make it symmetric with t8 */
4793 spec.t10 = 500 * 10;
4794 /* This one is special and actually in units of 100ms, but zero
4795 * based in the hw (so we need to add 100 ms). But the sw vbt
4796 * table multiplies it with 1000 to make it in units of 100usec,
4797 * too. */
4798 spec.t11_t12 = (510 + 100) * 10;
4799
4800 DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
4801 vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
4802
4803 /* Use the max of the register settings and vbt. If both are
4804 * unset, fall back to the spec limits. */
Ville Syrjälä36b5f422014-10-16 21:27:30 +03004805#define assign_final(field) final->field = (max(cur.field, vbt.field) == 0 ? \
Daniel Vetter67a54562012-10-20 20:57:45 +02004806 spec.field : \
4807 max(cur.field, vbt.field))
4808 assign_final(t1_t3);
4809 assign_final(t8);
4810 assign_final(t9);
4811 assign_final(t10);
4812 assign_final(t11_t12);
4813#undef assign_final
4814
Ville Syrjälä36b5f422014-10-16 21:27:30 +03004815#define get_delay(field) (DIV_ROUND_UP(final->field, 10))
Daniel Vetter67a54562012-10-20 20:57:45 +02004816 intel_dp->panel_power_up_delay = get_delay(t1_t3);
4817 intel_dp->backlight_on_delay = get_delay(t8);
4818 intel_dp->backlight_off_delay = get_delay(t9);
4819 intel_dp->panel_power_down_delay = get_delay(t10);
4820 intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
4821#undef get_delay
4822
Jani Nikulaf30d26e2013-01-16 10:53:40 +02004823 DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
4824 intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
4825 intel_dp->panel_power_cycle_delay);
4826
4827 DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
4828 intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
Jani Nikulaf30d26e2013-01-16 10:53:40 +02004829}
4830
4831static void
4832intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
Ville Syrjälä36b5f422014-10-16 21:27:30 +03004833 struct intel_dp *intel_dp)
Jani Nikulaf30d26e2013-01-16 10:53:40 +02004834{
4835 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes453c5422013-03-28 09:55:41 -07004836 u32 pp_on, pp_off, pp_div, port_sel = 0;
4837 int div = HAS_PCH_SPLIT(dev) ? intel_pch_rawclk(dev) : intel_hrawclk(dev);
4838 int pp_on_reg, pp_off_reg, pp_div_reg;
Ville Syrjäläad933b52014-08-18 22:15:56 +03004839 enum port port = dp_to_dig_port(intel_dp)->port;
Ville Syrjälä36b5f422014-10-16 21:27:30 +03004840 const struct edp_power_seq *seq = &intel_dp->pps_delays;
Jesse Barnes453c5422013-03-28 09:55:41 -07004841
Ville Syrjäläe39b9992014-09-04 14:53:14 +03004842 lockdep_assert_held(&dev_priv->pps_mutex);
Jesse Barnes453c5422013-03-28 09:55:41 -07004843
4844 if (HAS_PCH_SPLIT(dev)) {
4845 pp_on_reg = PCH_PP_ON_DELAYS;
4846 pp_off_reg = PCH_PP_OFF_DELAYS;
4847 pp_div_reg = PCH_PP_DIVISOR;
4848 } else {
Jani Nikulabf13e812013-09-06 07:40:05 +03004849 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
4850
4851 pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
4852 pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
4853 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
Jesse Barnes453c5422013-03-28 09:55:41 -07004854 }
4855
Paulo Zanonib2f19d12013-12-19 14:29:44 -02004856 /*
4857 * And finally store the new values in the power sequencer. The
4858 * backlight delays are set to 1 because we do manual waits on them. For
4859 * T8, even BSpec recommends doing it. For T9, if we don't do this,
4860 * we'll end up waiting for the backlight off delay twice: once when we
4861 * do the manual sleep, and once when we disable the panel and wait for
4862 * the PP_STATUS bit to become zero.
4863 */
Jani Nikulaf30d26e2013-01-16 10:53:40 +02004864 pp_on = (seq->t1_t3 << PANEL_POWER_UP_DELAY_SHIFT) |
Paulo Zanonib2f19d12013-12-19 14:29:44 -02004865 (1 << PANEL_LIGHT_ON_DELAY_SHIFT);
4866 pp_off = (1 << PANEL_LIGHT_OFF_DELAY_SHIFT) |
Jani Nikulaf30d26e2013-01-16 10:53:40 +02004867 (seq->t10 << PANEL_POWER_DOWN_DELAY_SHIFT);
Daniel Vetter67a54562012-10-20 20:57:45 +02004868 /* Compute the divisor for the pp clock, simply match the Bspec
4869 * formula. */
Jesse Barnes453c5422013-03-28 09:55:41 -07004870 pp_div = ((100 * div)/2 - 1) << PP_REFERENCE_DIVIDER_SHIFT;
Jani Nikulaf30d26e2013-01-16 10:53:40 +02004871 pp_div |= (DIV_ROUND_UP(seq->t11_t12, 1000)
Daniel Vetter67a54562012-10-20 20:57:45 +02004872 << PANEL_POWER_CYCLE_DELAY_SHIFT);
4873
4874 /* Haswell doesn't have any port selection bits for the panel
4875 * power sequencer any more. */
Imre Deakbc7d38a2013-05-16 14:40:36 +03004876 if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläad933b52014-08-18 22:15:56 +03004877 port_sel = PANEL_PORT_SELECT_VLV(port);
Imre Deakbc7d38a2013-05-16 14:40:36 +03004878 } else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
Ville Syrjäläad933b52014-08-18 22:15:56 +03004879 if (port == PORT_A)
Jani Nikulaa24c1442013-09-05 16:44:46 +03004880 port_sel = PANEL_PORT_SELECT_DPA;
Daniel Vetter67a54562012-10-20 20:57:45 +02004881 else
Jani Nikulaa24c1442013-09-05 16:44:46 +03004882 port_sel = PANEL_PORT_SELECT_DPD;
Daniel Vetter67a54562012-10-20 20:57:45 +02004883 }
4884
Jesse Barnes453c5422013-03-28 09:55:41 -07004885 pp_on |= port_sel;
4886
4887 I915_WRITE(pp_on_reg, pp_on);
4888 I915_WRITE(pp_off_reg, pp_off);
4889 I915_WRITE(pp_div_reg, pp_div);
Daniel Vetter67a54562012-10-20 20:57:45 +02004890
Daniel Vetter67a54562012-10-20 20:57:45 +02004891 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 -07004892 I915_READ(pp_on_reg),
4893 I915_READ(pp_off_reg),
4894 I915_READ(pp_div_reg));
Keith Packardc8110e52009-05-06 11:51:10 -07004895}
4896
Vandana Kannanb33a2812015-02-13 15:33:03 +05304897/**
4898 * intel_dp_set_drrs_state - program registers for RR switch to take effect
4899 * @dev: DRM device
4900 * @refresh_rate: RR to be programmed
4901 *
4902 * This function gets called when refresh rate (RR) has to be changed from
4903 * one frequency to another. Switches can be between high and low RR
4904 * supported by the panel or to any other RR based on media playback (in
4905 * this case, RR value needs to be passed from user space).
4906 *
4907 * The caller of this function needs to take a lock on dev_priv->drrs.
4908 */
Vandana Kannan96178ee2015-01-10 02:25:56 +05304909static void intel_dp_set_drrs_state(struct drm_device *dev, int refresh_rate)
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304910{
4911 struct drm_i915_private *dev_priv = dev->dev_private;
4912 struct intel_encoder *encoder;
Vandana Kannan96178ee2015-01-10 02:25:56 +05304913 struct intel_digital_port *dig_port = NULL;
4914 struct intel_dp *intel_dp = dev_priv->drrs.dp;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02004915 struct intel_crtc_state *config = NULL;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304916 struct intel_crtc *intel_crtc = NULL;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304917 u32 reg, val;
Vandana Kannan96178ee2015-01-10 02:25:56 +05304918 enum drrs_refresh_rate_type index = DRRS_HIGH_RR;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304919
4920 if (refresh_rate <= 0) {
4921 DRM_DEBUG_KMS("Refresh rate should be positive non-zero.\n");
4922 return;
4923 }
4924
Vandana Kannan96178ee2015-01-10 02:25:56 +05304925 if (intel_dp == NULL) {
4926 DRM_DEBUG_KMS("DRRS not supported.\n");
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304927 return;
4928 }
4929
Daniel Vetter1fcc9d12014-07-11 10:30:10 -07004930 /*
Rodrigo Vivie4d59f62014-11-20 02:22:08 -08004931 * FIXME: This needs proper synchronization with psr state for some
4932 * platforms that cannot have PSR and DRRS enabled at the same time.
Daniel Vetter1fcc9d12014-07-11 10:30:10 -07004933 */
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304934
Vandana Kannan96178ee2015-01-10 02:25:56 +05304935 dig_port = dp_to_dig_port(intel_dp);
4936 encoder = &dig_port->base;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304937 intel_crtc = encoder->new_crtc;
4938
4939 if (!intel_crtc) {
4940 DRM_DEBUG_KMS("DRRS: intel_crtc not initialized\n");
4941 return;
4942 }
4943
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004944 config = intel_crtc->config;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304945
Vandana Kannan96178ee2015-01-10 02:25:56 +05304946 if (dev_priv->drrs.type < SEAMLESS_DRRS_SUPPORT) {
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304947 DRM_DEBUG_KMS("Only Seamless DRRS supported.\n");
4948 return;
4949 }
4950
Vandana Kannan96178ee2015-01-10 02:25:56 +05304951 if (intel_dp->attached_connector->panel.downclock_mode->vrefresh ==
4952 refresh_rate)
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304953 index = DRRS_LOW_RR;
4954
Vandana Kannan96178ee2015-01-10 02:25:56 +05304955 if (index == dev_priv->drrs.refresh_rate_type) {
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304956 DRM_DEBUG_KMS(
4957 "DRRS requested for previously set RR...ignoring\n");
4958 return;
4959 }
4960
4961 if (!intel_crtc->active) {
4962 DRM_DEBUG_KMS("eDP encoder disabled. CRTC not Active\n");
4963 return;
4964 }
4965
Durgadoss R44395bf2015-02-13 15:33:02 +05304966 if (INTEL_INFO(dev)->gen >= 8 && !IS_CHERRYVIEW(dev)) {
Vandana Kannana4c30b12015-02-13 15:33:00 +05304967 switch (index) {
4968 case DRRS_HIGH_RR:
4969 intel_dp_set_m_n(intel_crtc, M1_N1);
4970 break;
4971 case DRRS_LOW_RR:
4972 intel_dp_set_m_n(intel_crtc, M2_N2);
4973 break;
4974 case DRRS_MAX_RR:
4975 default:
4976 DRM_ERROR("Unsupported refreshrate type\n");
4977 }
4978 } else if (INTEL_INFO(dev)->gen > 6) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004979 reg = PIPECONF(intel_crtc->config->cpu_transcoder);
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304980 val = I915_READ(reg);
Vandana Kannana4c30b12015-02-13 15:33:00 +05304981
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304982 if (index > DRRS_HIGH_RR) {
Vandana Kannan6fa7aec2015-02-13 15:33:01 +05304983 if (IS_VALLEYVIEW(dev))
4984 val |= PIPECONF_EDP_RR_MODE_SWITCH_VLV;
4985 else
4986 val |= PIPECONF_EDP_RR_MODE_SWITCH;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304987 } else {
Vandana Kannan6fa7aec2015-02-13 15:33:01 +05304988 if (IS_VALLEYVIEW(dev))
4989 val &= ~PIPECONF_EDP_RR_MODE_SWITCH_VLV;
4990 else
4991 val &= ~PIPECONF_EDP_RR_MODE_SWITCH;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05304992 }
4993 I915_WRITE(reg, val);
4994 }
4995
Vandana Kannan4e9ac942015-01-22 15:14:45 +05304996 dev_priv->drrs.refresh_rate_type = index;
4997
4998 DRM_DEBUG_KMS("eDP Refresh Rate set to : %dHz\n", refresh_rate);
4999}
5000
Vandana Kannanb33a2812015-02-13 15:33:03 +05305001/**
5002 * intel_edp_drrs_enable - init drrs struct if supported
5003 * @intel_dp: DP struct
5004 *
5005 * Initializes frontbuffer_bits and drrs.dp
5006 */
Vandana Kannanc3955782015-01-22 15:17:40 +05305007void intel_edp_drrs_enable(struct intel_dp *intel_dp)
5008{
5009 struct drm_device *dev = intel_dp_to_dev(intel_dp);
5010 struct drm_i915_private *dev_priv = dev->dev_private;
5011 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
5012 struct drm_crtc *crtc = dig_port->base.base.crtc;
5013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5014
5015 if (!intel_crtc->config->has_drrs) {
5016 DRM_DEBUG_KMS("Panel doesn't support DRRS\n");
5017 return;
5018 }
5019
5020 mutex_lock(&dev_priv->drrs.mutex);
5021 if (WARN_ON(dev_priv->drrs.dp)) {
5022 DRM_ERROR("DRRS already enabled\n");
5023 goto unlock;
5024 }
5025
5026 dev_priv->drrs.busy_frontbuffer_bits = 0;
5027
5028 dev_priv->drrs.dp = intel_dp;
5029
5030unlock:
5031 mutex_unlock(&dev_priv->drrs.mutex);
5032}
5033
Vandana Kannanb33a2812015-02-13 15:33:03 +05305034/**
5035 * intel_edp_drrs_disable - Disable DRRS
5036 * @intel_dp: DP struct
5037 *
5038 */
Vandana Kannanc3955782015-01-22 15:17:40 +05305039void intel_edp_drrs_disable(struct intel_dp *intel_dp)
5040{
5041 struct drm_device *dev = intel_dp_to_dev(intel_dp);
5042 struct drm_i915_private *dev_priv = dev->dev_private;
5043 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
5044 struct drm_crtc *crtc = dig_port->base.base.crtc;
5045 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5046
5047 if (!intel_crtc->config->has_drrs)
5048 return;
5049
5050 mutex_lock(&dev_priv->drrs.mutex);
5051 if (!dev_priv->drrs.dp) {
5052 mutex_unlock(&dev_priv->drrs.mutex);
5053 return;
5054 }
5055
5056 if (dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
5057 intel_dp_set_drrs_state(dev_priv->dev,
5058 intel_dp->attached_connector->panel.
5059 fixed_mode->vrefresh);
5060
5061 dev_priv->drrs.dp = NULL;
5062 mutex_unlock(&dev_priv->drrs.mutex);
5063
5064 cancel_delayed_work_sync(&dev_priv->drrs.work);
5065}
5066
Vandana Kannan4e9ac942015-01-22 15:14:45 +05305067static void intel_edp_drrs_downclock_work(struct work_struct *work)
5068{
5069 struct drm_i915_private *dev_priv =
5070 container_of(work, typeof(*dev_priv), drrs.work.work);
5071 struct intel_dp *intel_dp;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305072
Vandana Kannan96178ee2015-01-10 02:25:56 +05305073 mutex_lock(&dev_priv->drrs.mutex);
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305074
Vandana Kannan4e9ac942015-01-22 15:14:45 +05305075 intel_dp = dev_priv->drrs.dp;
5076
5077 if (!intel_dp)
5078 goto unlock;
5079
5080 /*
5081 * The delayed work can race with an invalidate hence we need to
5082 * recheck.
5083 */
5084
5085 if (dev_priv->drrs.busy_frontbuffer_bits)
5086 goto unlock;
5087
5088 if (dev_priv->drrs.refresh_rate_type != DRRS_LOW_RR)
5089 intel_dp_set_drrs_state(dev_priv->dev,
5090 intel_dp->attached_connector->panel.
5091 downclock_mode->vrefresh);
5092
5093unlock:
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305094
Vandana Kannan96178ee2015-01-10 02:25:56 +05305095 mutex_unlock(&dev_priv->drrs.mutex);
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305096}
5097
Vandana Kannanb33a2812015-02-13 15:33:03 +05305098/**
5099 * intel_edp_drrs_invalidate - Invalidate DRRS
5100 * @dev: DRM device
5101 * @frontbuffer_bits: frontbuffer plane tracking bits
5102 *
5103 * When there is a disturbance on screen (due to cursor movement/time
5104 * update etc), DRRS needs to be invalidated, i.e. need to switch to
5105 * high RR.
5106 *
5107 * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
5108 */
Vandana Kannana93fad02015-01-10 02:25:59 +05305109void intel_edp_drrs_invalidate(struct drm_device *dev,
5110 unsigned frontbuffer_bits)
5111{
5112 struct drm_i915_private *dev_priv = dev->dev_private;
5113 struct drm_crtc *crtc;
5114 enum pipe pipe;
5115
5116 if (!dev_priv->drrs.dp)
5117 return;
5118
Ramalingam C3954e732015-03-03 12:11:46 +05305119 cancel_delayed_work_sync(&dev_priv->drrs.work);
5120
Vandana Kannana93fad02015-01-10 02:25:59 +05305121 mutex_lock(&dev_priv->drrs.mutex);
5122 crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
5123 pipe = to_intel_crtc(crtc)->pipe;
5124
5125 if (dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR) {
Vandana Kannana93fad02015-01-10 02:25:59 +05305126 intel_dp_set_drrs_state(dev_priv->dev,
5127 dev_priv->drrs.dp->attached_connector->panel.
5128 fixed_mode->vrefresh);
5129 }
5130
5131 frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
5132
5133 dev_priv->drrs.busy_frontbuffer_bits |= frontbuffer_bits;
5134 mutex_unlock(&dev_priv->drrs.mutex);
5135}
5136
Vandana Kannanb33a2812015-02-13 15:33:03 +05305137/**
5138 * intel_edp_drrs_flush - Flush DRRS
5139 * @dev: DRM device
5140 * @frontbuffer_bits: frontbuffer plane tracking bits
5141 *
5142 * When there is no movement on screen, DRRS work can be scheduled.
5143 * This DRRS work is responsible for setting relevant registers after a
5144 * timeout of 1 second.
5145 *
5146 * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
5147 */
Vandana Kannana93fad02015-01-10 02:25:59 +05305148void intel_edp_drrs_flush(struct drm_device *dev,
5149 unsigned frontbuffer_bits)
5150{
5151 struct drm_i915_private *dev_priv = dev->dev_private;
5152 struct drm_crtc *crtc;
5153 enum pipe pipe;
5154
5155 if (!dev_priv->drrs.dp)
5156 return;
5157
Ramalingam C3954e732015-03-03 12:11:46 +05305158 cancel_delayed_work_sync(&dev_priv->drrs.work);
5159
Vandana Kannana93fad02015-01-10 02:25:59 +05305160 mutex_lock(&dev_priv->drrs.mutex);
5161 crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
5162 pipe = to_intel_crtc(crtc)->pipe;
5163 dev_priv->drrs.busy_frontbuffer_bits &= ~frontbuffer_bits;
5164
Vandana Kannana93fad02015-01-10 02:25:59 +05305165 if (dev_priv->drrs.refresh_rate_type != DRRS_LOW_RR &&
5166 !dev_priv->drrs.busy_frontbuffer_bits)
5167 schedule_delayed_work(&dev_priv->drrs.work,
5168 msecs_to_jiffies(1000));
5169 mutex_unlock(&dev_priv->drrs.mutex);
5170}
5171
Vandana Kannanb33a2812015-02-13 15:33:03 +05305172/**
5173 * DOC: Display Refresh Rate Switching (DRRS)
5174 *
5175 * Display Refresh Rate Switching (DRRS) is a power conservation feature
5176 * which enables swtching between low and high refresh rates,
5177 * dynamically, based on the usage scenario. This feature is applicable
5178 * for internal panels.
5179 *
5180 * Indication that the panel supports DRRS is given by the panel EDID, which
5181 * would list multiple refresh rates for one resolution.
5182 *
5183 * DRRS is of 2 types - static and seamless.
5184 * Static DRRS involves changing refresh rate (RR) by doing a full modeset
5185 * (may appear as a blink on screen) and is used in dock-undock scenario.
5186 * Seamless DRRS involves changing RR without any visual effect to the user
5187 * and can be used during normal system usage. This is done by programming
5188 * certain registers.
5189 *
5190 * Support for static/seamless DRRS may be indicated in the VBT based on
5191 * inputs from the panel spec.
5192 *
5193 * DRRS saves power by switching to low RR based on usage scenarios.
5194 *
5195 * eDP DRRS:-
5196 * The implementation is based on frontbuffer tracking implementation.
5197 * When there is a disturbance on the screen triggered by user activity or a
5198 * periodic system activity, DRRS is disabled (RR is changed to high RR).
5199 * When there is no movement on screen, after a timeout of 1 second, a switch
5200 * to low RR is made.
5201 * For integration with frontbuffer tracking code,
5202 * intel_edp_drrs_invalidate() and intel_edp_drrs_flush() are called.
5203 *
5204 * DRRS can be further extended to support other internal panels and also
5205 * the scenario of video playback wherein RR is set based on the rate
5206 * requested by userspace.
5207 */
5208
5209/**
5210 * intel_dp_drrs_init - Init basic DRRS work and mutex.
5211 * @intel_connector: eDP connector
5212 * @fixed_mode: preferred mode of panel
5213 *
5214 * This function is called only once at driver load to initialize basic
5215 * DRRS stuff.
5216 *
5217 * Returns:
5218 * Downclock mode if panel supports it, else return NULL.
5219 * DRRS support is determined by the presence of downclock mode (apart
5220 * from VBT setting).
5221 */
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305222static struct drm_display_mode *
Vandana Kannan96178ee2015-01-10 02:25:56 +05305223intel_dp_drrs_init(struct intel_connector *intel_connector,
5224 struct drm_display_mode *fixed_mode)
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305225{
5226 struct drm_connector *connector = &intel_connector->base;
Vandana Kannan96178ee2015-01-10 02:25:56 +05305227 struct drm_device *dev = connector->dev;
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305228 struct drm_i915_private *dev_priv = dev->dev_private;
5229 struct drm_display_mode *downclock_mode = NULL;
5230
5231 if (INTEL_INFO(dev)->gen <= 6) {
5232 DRM_DEBUG_KMS("DRRS supported for Gen7 and above\n");
5233 return NULL;
5234 }
5235
5236 if (dev_priv->vbt.drrs_type != SEAMLESS_DRRS_SUPPORT) {
Damien Lespiau4079b8d2014-08-05 10:39:42 +01005237 DRM_DEBUG_KMS("VBT doesn't support DRRS\n");
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305238 return NULL;
5239 }
5240
5241 downclock_mode = intel_find_panel_downclock
5242 (dev, fixed_mode, connector);
5243
5244 if (!downclock_mode) {
Ramalingam Ca1d26342015-02-23 17:38:33 +05305245 DRM_DEBUG_KMS("Downclock mode is not found. DRRS not supported\n");
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305246 return NULL;
5247 }
5248
Vandana Kannan4e9ac942015-01-22 15:14:45 +05305249 INIT_DELAYED_WORK(&dev_priv->drrs.work, intel_edp_drrs_downclock_work);
5250
Vandana Kannan96178ee2015-01-10 02:25:56 +05305251 mutex_init(&dev_priv->drrs.mutex);
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305252
Vandana Kannan96178ee2015-01-10 02:25:56 +05305253 dev_priv->drrs.type = dev_priv->vbt.drrs_type;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305254
Vandana Kannan96178ee2015-01-10 02:25:56 +05305255 dev_priv->drrs.refresh_rate_type = DRRS_HIGH_RR;
Damien Lespiau4079b8d2014-08-05 10:39:42 +01005256 DRM_DEBUG_KMS("seamless DRRS supported for eDP panel.\n");
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305257 return downclock_mode;
5258}
5259
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005260static bool intel_edp_init_connector(struct intel_dp *intel_dp,
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005261 struct intel_connector *intel_connector)
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005262{
5263 struct drm_connector *connector = &intel_connector->base;
5264 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Paulo Zanoni63635212014-04-22 19:55:42 -03005265 struct intel_encoder *intel_encoder = &intel_dig_port->base;
5266 struct drm_device *dev = intel_encoder->base.dev;
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005267 struct drm_i915_private *dev_priv = dev->dev_private;
5268 struct drm_display_mode *fixed_mode = NULL;
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305269 struct drm_display_mode *downclock_mode = NULL;
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005270 bool has_dpcd;
5271 struct drm_display_mode *scan;
5272 struct edid *edid;
Ville Syrjälä6517d272014-11-07 11:16:02 +02005273 enum pipe pipe = INVALID_PIPE;
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005274
Vandana Kannan96178ee2015-01-10 02:25:56 +05305275 dev_priv->drrs.type = DRRS_NOT_SUPPORTED;
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305276
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005277 if (!is_edp(intel_dp))
5278 return true;
5279
Ville Syrjälä49e6bc52014-10-28 16:15:52 +02005280 pps_lock(intel_dp);
5281 intel_edp_panel_vdd_sanitize(intel_dp);
5282 pps_unlock(intel_dp);
Paulo Zanoni63635212014-04-22 19:55:42 -03005283
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005284 /* Cache DPCD and EDID for edp. */
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005285 has_dpcd = intel_dp_get_dpcd(intel_dp);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005286
5287 if (has_dpcd) {
5288 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
5289 dev_priv->no_aux_handshake =
5290 intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
5291 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
5292 } else {
5293 /* if this fails, presume the device is a ghost */
5294 DRM_INFO("failed to retrieve link info, disabling eDP\n");
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005295 return false;
5296 }
5297
5298 /* We now know it's not a ghost, init power sequence regs. */
Ville Syrjälä773538e82014-09-04 14:54:56 +03005299 pps_lock(intel_dp);
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005300 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03005301 pps_unlock(intel_dp);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005302
Daniel Vetter060c8772014-03-21 23:22:35 +01005303 mutex_lock(&dev->mode_config.mutex);
Jani Nikula0b998362014-03-14 16:51:17 +02005304 edid = drm_get_edid(connector, &intel_dp->aux.ddc);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005305 if (edid) {
5306 if (drm_add_edid_modes(connector, edid)) {
5307 drm_mode_connector_update_edid_property(connector,
5308 edid);
5309 drm_edid_to_eld(connector, edid);
5310 } else {
5311 kfree(edid);
5312 edid = ERR_PTR(-EINVAL);
5313 }
5314 } else {
5315 edid = ERR_PTR(-ENOENT);
5316 }
5317 intel_connector->edid = edid;
5318
5319 /* prefer fixed mode from EDID if available */
5320 list_for_each_entry(scan, &connector->probed_modes, head) {
5321 if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
5322 fixed_mode = drm_mode_duplicate(dev, scan);
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305323 downclock_mode = intel_dp_drrs_init(
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305324 intel_connector, fixed_mode);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005325 break;
5326 }
5327 }
5328
5329 /* fallback to VBT if available for eDP */
5330 if (!fixed_mode && dev_priv->vbt.lfp_lvds_vbt_mode) {
5331 fixed_mode = drm_mode_duplicate(dev,
5332 dev_priv->vbt.lfp_lvds_vbt_mode);
5333 if (fixed_mode)
5334 fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
5335 }
Daniel Vetter060c8772014-03-21 23:22:35 +01005336 mutex_unlock(&dev->mode_config.mutex);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005337
Clint Taylor01527b32014-07-07 13:01:46 -07005338 if (IS_VALLEYVIEW(dev)) {
5339 intel_dp->edp_notifier.notifier_call = edp_notify_handler;
5340 register_reboot_notifier(&intel_dp->edp_notifier);
Ville Syrjälä6517d272014-11-07 11:16:02 +02005341
5342 /*
5343 * Figure out the current pipe for the initial backlight setup.
5344 * If the current pipe isn't valid, try the PPS pipe, and if that
5345 * fails just assume pipe A.
5346 */
5347 if (IS_CHERRYVIEW(dev))
5348 pipe = DP_PORT_TO_PIPE_CHV(intel_dp->DP);
5349 else
5350 pipe = PORT_TO_PIPE(intel_dp->DP);
5351
5352 if (pipe != PIPE_A && pipe != PIPE_B)
5353 pipe = intel_dp->pps_pipe;
5354
5355 if (pipe != PIPE_A && pipe != PIPE_B)
5356 pipe = PIPE_A;
5357
5358 DRM_DEBUG_KMS("using pipe %c for initial backlight setup\n",
5359 pipe_name(pipe));
Clint Taylor01527b32014-07-07 13:01:46 -07005360 }
5361
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305362 intel_panel_init(&intel_connector->panel, fixed_mode, downclock_mode);
Jani Nikula73580fb72014-08-12 17:11:41 +03005363 intel_connector->panel.backlight_power = intel_edp_backlight_power;
Ville Syrjälä6517d272014-11-07 11:16:02 +02005364 intel_panel_setup_backlight(connector, pipe);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005365
5366 return true;
5367}
5368
Paulo Zanoni16c25532013-06-12 17:27:25 -03005369bool
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005370intel_dp_init_connector(struct intel_digital_port *intel_dig_port,
5371 struct intel_connector *intel_connector)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005372{
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005373 struct drm_connector *connector = &intel_connector->base;
5374 struct intel_dp *intel_dp = &intel_dig_port->dp;
5375 struct intel_encoder *intel_encoder = &intel_dig_port->base;
5376 struct drm_device *dev = intel_encoder->base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005377 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni174edf12012-10-26 19:05:50 -02005378 enum port port = intel_dig_port->port;
Jani Nikula0b998362014-03-14 16:51:17 +02005379 int type;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005380
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03005381 intel_dp->pps_pipe = INVALID_PIPE;
5382
Damien Lespiauec5b01d2014-01-21 13:35:39 +00005383 /* intel_dp vfuncs */
Damien Lespiaub6b5e382014-01-20 16:00:59 +00005384 if (INTEL_INFO(dev)->gen >= 9)
5385 intel_dp->get_aux_clock_divider = skl_get_aux_clock_divider;
5386 else if (IS_VALLEYVIEW(dev))
Damien Lespiauec5b01d2014-01-21 13:35:39 +00005387 intel_dp->get_aux_clock_divider = vlv_get_aux_clock_divider;
5388 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
5389 intel_dp->get_aux_clock_divider = hsw_get_aux_clock_divider;
5390 else if (HAS_PCH_SPLIT(dev))
5391 intel_dp->get_aux_clock_divider = ilk_get_aux_clock_divider;
5392 else
5393 intel_dp->get_aux_clock_divider = i9xx_get_aux_clock_divider;
5394
Damien Lespiaub9ca5fa2014-01-20 16:01:00 +00005395 if (INTEL_INFO(dev)->gen >= 9)
5396 intel_dp->get_aux_send_ctl = skl_get_aux_send_ctl;
5397 else
5398 intel_dp->get_aux_send_ctl = i9xx_get_aux_send_ctl;
Damien Lespiau153b1102014-01-21 13:37:15 +00005399
Daniel Vetter07679352012-09-06 22:15:42 +02005400 /* Preserve the current hw state. */
5401 intel_dp->DP = I915_READ(intel_dp->output_reg);
Jani Nikuladd06f902012-10-19 14:51:50 +03005402 intel_dp->attached_connector = intel_connector;
Chris Wilson3d3dc142011-02-12 10:33:12 +00005403
Ville Syrjälä3b32a352013-11-01 18:22:41 +02005404 if (intel_dp_is_edp(dev, port))
Gajanan Bhat19c03922012-09-27 19:13:07 +05305405 type = DRM_MODE_CONNECTOR_eDP;
Ville Syrjälä3b32a352013-11-01 18:22:41 +02005406 else
5407 type = DRM_MODE_CONNECTOR_DisplayPort;
Adam Jacksonb3295302010-07-16 14:46:28 -04005408
Imre Deakf7d24902013-05-08 13:14:05 +03005409 /*
5410 * For eDP we always set the encoder type to INTEL_OUTPUT_EDP, but
5411 * for DP the encoder type can be set by the caller to
5412 * INTEL_OUTPUT_UNKNOWN for DDI, so don't rewrite it.
5413 */
5414 if (type == DRM_MODE_CONNECTOR_eDP)
5415 intel_encoder->type = INTEL_OUTPUT_EDP;
5416
Ville Syrjäläc17ed5b2014-10-16 21:27:27 +03005417 /* eDP only on port B and/or C on vlv/chv */
5418 if (WARN_ON(IS_VALLEYVIEW(dev) && is_edp(intel_dp) &&
5419 port != PORT_B && port != PORT_C))
5420 return false;
5421
Imre Deake7281ea2013-05-08 13:14:08 +03005422 DRM_DEBUG_KMS("Adding %s connector on port %c\n",
5423 type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
5424 port_name(port));
5425
Adam Jacksonb3295302010-07-16 14:46:28 -04005426 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005427 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
5428
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005429 connector->interlace_allowed = true;
5430 connector->doublescan_allowed = 0;
Ma Lingf8aed702009-08-24 13:50:24 +08005431
Daniel Vetter66a92782012-07-12 20:08:18 +02005432 INIT_DELAYED_WORK(&intel_dp->panel_vdd_work,
Daniel Vetter4be73782014-01-17 14:39:48 +01005433 edp_panel_vdd_work);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08005434
Chris Wilsondf0e9242010-09-09 16:20:55 +01005435 intel_connector_attach_encoder(intel_connector, intel_encoder);
Thomas Wood34ea3d32014-05-29 16:57:41 +01005436 drm_connector_register(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005437
Paulo Zanoniaffa9352012-11-23 15:30:39 -02005438 if (HAS_DDI(dev))
Paulo Zanonibcbc8892012-10-26 19:05:51 -02005439 intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
5440 else
5441 intel_connector->get_hw_state = intel_connector_get_hw_state;
Imre Deak80f65de2014-02-11 17:12:49 +02005442 intel_connector->unregister = intel_dp_connector_unregister;
Paulo Zanonibcbc8892012-10-26 19:05:51 -02005443
Jani Nikula0b998362014-03-14 16:51:17 +02005444 /* Set up the hotplug pin. */
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03005445 switch (port) {
5446 case PORT_A:
Egbert Eich1d843f92013-02-25 12:06:49 -05005447 intel_encoder->hpd_pin = HPD_PORT_A;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03005448 break;
5449 case PORT_B:
Egbert Eich1d843f92013-02-25 12:06:49 -05005450 intel_encoder->hpd_pin = HPD_PORT_B;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03005451 break;
5452 case PORT_C:
Egbert Eich1d843f92013-02-25 12:06:49 -05005453 intel_encoder->hpd_pin = HPD_PORT_C;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03005454 break;
5455 case PORT_D:
Egbert Eich1d843f92013-02-25 12:06:49 -05005456 intel_encoder->hpd_pin = HPD_PORT_D;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03005457 break;
5458 default:
Damien Lespiauad1c0b12013-03-07 15:30:28 +00005459 BUG();
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005460 }
5461
Imre Deakdada1a92014-01-29 13:25:41 +02005462 if (is_edp(intel_dp)) {
Ville Syrjälä773538e82014-09-04 14:54:56 +03005463 pps_lock(intel_dp);
Ville Syrjälä1e74a322014-10-28 16:15:51 +02005464 intel_dp_init_panel_power_timestamps(intel_dp);
5465 if (IS_VALLEYVIEW(dev))
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03005466 vlv_initial_power_sequencer_setup(intel_dp);
Ville Syrjälä1e74a322014-10-28 16:15:51 +02005467 else
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005468 intel_dp_init_panel_power_sequencer(dev, intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03005469 pps_unlock(intel_dp);
Imre Deakdada1a92014-01-29 13:25:41 +02005470 }
Paulo Zanoni0095e6d2013-12-19 14:29:39 -02005471
Jani Nikula9d1a1032014-03-14 16:51:15 +02005472 intel_dp_aux_init(intel_dp, intel_connector);
Dave Airliec1f05262012-08-30 11:06:18 +10005473
Dave Airlie0e32b392014-05-02 14:02:48 +10005474 /* init MST on ports that can support it */
Damien Lespiauc86ea3d2014-12-12 14:26:58 +00005475 if (IS_HASWELL(dev) || IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Dave Airlie0e32b392014-05-02 14:02:48 +10005476 if (port == PORT_B || port == PORT_C || port == PORT_D) {
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03005477 intel_dp_mst_encoder_init(intel_dig_port,
5478 intel_connector->base.base.id);
Dave Airlie0e32b392014-05-02 14:02:48 +10005479 }
5480 }
5481
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005482 if (!intel_edp_init_connector(intel_dp, intel_connector)) {
Dave Airlie4f71d0c2014-06-04 16:02:28 +10005483 drm_dp_aux_unregister(&intel_dp->aux);
Paulo Zanoni15b1d172013-06-12 17:27:27 -03005484 if (is_edp(intel_dp)) {
5485 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
Ville Syrjälä951468f2014-09-04 14:55:31 +03005486 /*
5487 * vdd might still be enabled do to the delayed vdd off.
5488 * Make sure vdd is actually turned off here.
5489 */
Ville Syrjälä773538e82014-09-04 14:54:56 +03005490 pps_lock(intel_dp);
Daniel Vetter4be73782014-01-17 14:39:48 +01005491 edp_panel_vdd_off_sync(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03005492 pps_unlock(intel_dp);
Paulo Zanoni15b1d172013-06-12 17:27:27 -03005493 }
Thomas Wood34ea3d32014-05-29 16:57:41 +01005494 drm_connector_unregister(connector);
Paulo Zanonib2f246a2013-06-12 17:27:26 -03005495 drm_connector_cleanup(connector);
Paulo Zanoni16c25532013-06-12 17:27:25 -03005496 return false;
Paulo Zanonib2f246a2013-06-12 17:27:26 -03005497 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005498
Chris Wilsonf6849602010-09-19 09:29:33 +01005499 intel_dp_add_properties(intel_dp, connector);
5500
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005501 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
5502 * 0xd. Failure to do so will result in spurious interrupts being
5503 * generated on the port when a cable is not attached.
5504 */
5505 if (IS_G4X(dev) && !IS_GM45(dev)) {
5506 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
5507 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
5508 }
Paulo Zanoni16c25532013-06-12 17:27:25 -03005509
5510 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005511}
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005512
5513void
5514intel_dp_init(struct drm_device *dev, int output_reg, enum port port)
5515{
Dave Airlie13cf5502014-06-18 11:29:35 +10005516 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005517 struct intel_digital_port *intel_dig_port;
5518 struct intel_encoder *intel_encoder;
5519 struct drm_encoder *encoder;
5520 struct intel_connector *intel_connector;
5521
Daniel Vetterb14c5672013-09-19 12:18:32 +02005522 intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005523 if (!intel_dig_port)
5524 return;
5525
Daniel Vetterb14c5672013-09-19 12:18:32 +02005526 intel_connector = kzalloc(sizeof(*intel_connector), GFP_KERNEL);
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005527 if (!intel_connector) {
5528 kfree(intel_dig_port);
5529 return;
5530 }
5531
5532 intel_encoder = &intel_dig_port->base;
5533 encoder = &intel_encoder->base;
5534
5535 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
5536 DRM_MODE_ENCODER_TMDS);
5537
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01005538 intel_encoder->compute_config = intel_dp_compute_config;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02005539 intel_encoder->disable = intel_disable_dp;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02005540 intel_encoder->get_hw_state = intel_dp_get_hw_state;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07005541 intel_encoder->get_config = intel_dp_get_config;
Imre Deak07f9cd02014-08-18 14:42:45 +03005542 intel_encoder->suspend = intel_dp_encoder_suspend;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03005543 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä9197c882014-04-09 13:29:05 +03005544 intel_encoder->pre_pll_enable = chv_dp_pre_pll_enable;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03005545 intel_encoder->pre_enable = chv_pre_enable_dp;
5546 intel_encoder->enable = vlv_enable_dp;
Ville Syrjälä580d3812014-04-09 13:29:00 +03005547 intel_encoder->post_disable = chv_post_disable_dp;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03005548 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaecff4f32013-09-06 07:38:29 +03005549 intel_encoder->pre_pll_enable = vlv_dp_pre_pll_enable;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03005550 intel_encoder->pre_enable = vlv_pre_enable_dp;
5551 intel_encoder->enable = vlv_enable_dp;
Ville Syrjälä49277c32014-03-31 18:21:26 +03005552 intel_encoder->post_disable = vlv_post_disable_dp;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03005553 } else {
Jani Nikulaecff4f32013-09-06 07:38:29 +03005554 intel_encoder->pre_enable = g4x_pre_enable_dp;
5555 intel_encoder->enable = g4x_enable_dp;
Ville Syrjälä08aff3f2014-08-18 22:16:09 +03005556 if (INTEL_INFO(dev)->gen >= 5)
5557 intel_encoder->post_disable = ilk_post_disable_dp;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03005558 }
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005559
Paulo Zanoni174edf12012-10-26 19:05:50 -02005560 intel_dig_port->port = port;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005561 intel_dig_port->dp.output_reg = output_reg;
5562
Paulo Zanoni00c09d72012-10-26 19:05:52 -02005563 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
Ville Syrjälä882ec382014-04-28 14:07:43 +03005564 if (IS_CHERRYVIEW(dev)) {
5565 if (port == PORT_D)
5566 intel_encoder->crtc_mask = 1 << 2;
5567 else
5568 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
5569 } else {
5570 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
5571 }
Ville Syrjäläbc079e82014-03-03 16:15:28 +02005572 intel_encoder->cloneable = 0;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005573 intel_encoder->hot_plug = intel_dp_hot_plug;
5574
Dave Airlie13cf5502014-06-18 11:29:35 +10005575 intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
5576 dev_priv->hpd_irq_port[port] = intel_dig_port;
5577
Paulo Zanoni15b1d172013-06-12 17:27:27 -03005578 if (!intel_dp_init_connector(intel_dig_port, intel_connector)) {
5579 drm_encoder_cleanup(encoder);
5580 kfree(intel_dig_port);
Paulo Zanonib2f246a2013-06-12 17:27:26 -03005581 kfree(intel_connector);
Paulo Zanoni15b1d172013-06-12 17:27:27 -03005582 }
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005583}
Dave Airlie0e32b392014-05-02 14:02:48 +10005584
5585void intel_dp_mst_suspend(struct drm_device *dev)
5586{
5587 struct drm_i915_private *dev_priv = dev->dev_private;
5588 int i;
5589
5590 /* disable MST */
5591 for (i = 0; i < I915_MAX_PORTS; i++) {
5592 struct intel_digital_port *intel_dig_port = dev_priv->hpd_irq_port[i];
5593 if (!intel_dig_port)
5594 continue;
5595
5596 if (intel_dig_port->base.type == INTEL_OUTPUT_DISPLAYPORT) {
5597 if (!intel_dig_port->dp.can_mst)
5598 continue;
5599 if (intel_dig_port->dp.is_mst)
5600 drm_dp_mst_topology_mgr_suspend(&intel_dig_port->dp.mst_mgr);
5601 }
5602 }
5603}
5604
5605void intel_dp_mst_resume(struct drm_device *dev)
5606{
5607 struct drm_i915_private *dev_priv = dev->dev_private;
5608 int i;
5609
5610 for (i = 0; i < I915_MAX_PORTS; i++) {
5611 struct intel_digital_port *intel_dig_port = dev_priv->hpd_irq_port[i];
5612 if (!intel_dig_port)
5613 continue;
5614 if (intel_dig_port->base.type == INTEL_OUTPUT_DISPLAYPORT) {
5615 int ret;
5616
5617 if (!intel_dig_port->dp.can_mst)
5618 continue;
5619
5620 ret = drm_dp_mst_topology_mgr_resume(&intel_dig_port->dp.mst_mgr);
5621 if (ret != 0) {
5622 intel_dp_check_mst_status(&intel_dig_port->dp);
5623 }
5624 }
5625 }
5626}