blob: ba2d0242d532befed35400e34ac3ed78813753c0 [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
Todd Previte559be302015-05-04 07:48:20 -070044/* Compliance test status bits */
45#define INTEL_DP_RESOLUTION_SHIFT_MASK 0
46#define INTEL_DP_RESOLUTION_PREFERRED (1 << INTEL_DP_RESOLUTION_SHIFT_MASK)
47#define INTEL_DP_RESOLUTION_STANDARD (2 << INTEL_DP_RESOLUTION_SHIFT_MASK)
48#define INTEL_DP_RESOLUTION_FAILSAFE (3 << INTEL_DP_RESOLUTION_SHIFT_MASK)
49
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +080050struct dp_link_dpll {
Ville Syrjälä840b32b2015-08-11 20:21:46 +030051 int clock;
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +080052 struct dpll dpll;
53};
54
55static const struct dp_link_dpll gen4_dpll[] = {
Ville Syrjälä840b32b2015-08-11 20:21:46 +030056 { 162000,
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +080057 { .p1 = 2, .p2 = 10, .n = 2, .m1 = 23, .m2 = 8 } },
Ville Syrjälä840b32b2015-08-11 20:21:46 +030058 { 270000,
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +080059 { .p1 = 1, .p2 = 10, .n = 1, .m1 = 14, .m2 = 2 } }
60};
61
62static const struct dp_link_dpll pch_dpll[] = {
Ville Syrjälä840b32b2015-08-11 20:21:46 +030063 { 162000,
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +080064 { .p1 = 2, .p2 = 10, .n = 1, .m1 = 12, .m2 = 9 } },
Ville Syrjälä840b32b2015-08-11 20:21:46 +030065 { 270000,
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +080066 { .p1 = 1, .p2 = 10, .n = 2, .m1 = 14, .m2 = 8 } }
67};
68
Chon Ming Lee65ce4bf2013-09-04 01:30:38 +080069static const struct dp_link_dpll vlv_dpll[] = {
Ville Syrjälä840b32b2015-08-11 20:21:46 +030070 { 162000,
Chon Ming Lee58f6e632013-09-25 15:47:51 +080071 { .p1 = 3, .p2 = 2, .n = 5, .m1 = 3, .m2 = 81 } },
Ville Syrjälä840b32b2015-08-11 20:21:46 +030072 { 270000,
Chon Ming Lee65ce4bf2013-09-04 01:30:38 +080073 { .p1 = 2, .p2 = 2, .n = 1, .m1 = 2, .m2 = 27 } }
74};
75
Chon Ming Leeef9348c2014-04-09 13:28:18 +030076/*
77 * CHV supports eDP 1.4 that have more link rates.
78 * Below only provides the fixed rate but exclude variable rate.
79 */
80static const struct dp_link_dpll chv_dpll[] = {
81 /*
82 * CHV requires to program fractional division for m2.
83 * m2 is stored in fixed point format using formula below
84 * (m2_int << 22) | m2_fraction
85 */
Ville Syrjälä840b32b2015-08-11 20:21:46 +030086 { 162000, /* m2_int = 32, m2_fraction = 1677722 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +030087 { .p1 = 4, .p2 = 2, .n = 1, .m1 = 2, .m2 = 0x819999a } },
Ville Syrjälä840b32b2015-08-11 20:21:46 +030088 { 270000, /* m2_int = 27, m2_fraction = 0 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +030089 { .p1 = 4, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } },
Ville Syrjälä840b32b2015-08-11 20:21:46 +030090 { 540000, /* m2_int = 27, m2_fraction = 0 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +030091 { .p1 = 2, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } }
92};
Sonika Jindal637a9c62015-05-07 09:52:08 +053093
Sonika Jindal64987fc2015-05-26 17:50:13 +053094static const int bxt_rates[] = { 162000, 216000, 243000, 270000,
95 324000, 432000, 540000 };
Sonika Jindal637a9c62015-05-07 09:52:08 +053096static const int skl_rates[] = { 162000, 216000, 270000,
Ville Syrjäläf4896f12015-03-12 17:10:27 +020097 324000, 432000, 540000 };
98static const int default_rates[] = { 162000, 270000, 540000 };
Chon Ming Leeef9348c2014-04-09 13:28:18 +030099
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700100/**
101 * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
102 * @intel_dp: DP struct
103 *
104 * If a CPU or PCH DP output is attached to an eDP panel, this function
105 * will return true, and false otherwise.
106 */
107static bool is_edp(struct intel_dp *intel_dp)
108{
Paulo Zanonida63a9f2012-10-26 19:05:46 -0200109 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
110
111 return intel_dig_port->base.type == INTEL_OUTPUT_EDP;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700112}
113
Imre Deak68b4d822013-05-08 13:14:06 +0300114static struct drm_device *intel_dp_to_dev(struct intel_dp *intel_dp)
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700115{
Imre Deak68b4d822013-05-08 13:14:06 +0300116 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
117
118 return intel_dig_port->base.base.dev;
Jesse Barnescfcb0fc2010-10-07 16:01:06 -0700119}
120
Chris Wilsondf0e9242010-09-09 16:20:55 +0100121static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
122{
Paulo Zanonifa90ece2012-10-26 19:05:44 -0200123 return enc_to_intel_dp(&intel_attached_encoder(connector)->base);
Chris Wilsondf0e9242010-09-09 16:20:55 +0100124}
125
Chris Wilsonea5b2132010-08-04 13:50:23 +0100126static void intel_dp_link_down(struct intel_dp *intel_dp);
Ville Syrjälä1e0560e2014-08-19 13:24:25 +0300127static bool edp_panel_vdd_on(struct intel_dp *intel_dp);
Daniel Vetter4be73782014-01-17 14:39:48 +0100128static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync);
Ville Syrjälä093e3f12014-10-16 21:27:33 +0300129static void vlv_init_panel_power_sequencer(struct intel_dp *intel_dp);
Ville Syrjäläa8c33442014-10-16 21:29:59 +0300130static void vlv_steal_power_sequencer(struct drm_device *dev,
131 enum pipe pipe);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700132
Ville Syrjäläe0fce782015-07-08 23:45:54 +0300133static unsigned int intel_dp_unused_lane_mask(int lane_count)
134{
135 return ~((1 << lane_count) - 1) & 0xf;
136}
137
Ville Syrjäläed4e9c12015-03-12 17:10:36 +0200138static int
139intel_dp_max_link_bw(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700140{
Jesse Barnes7183dc22011-07-07 11:10:58 -0700141 int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700142
143 switch (max_link_bw) {
144 case DP_LINK_BW_1_62:
145 case DP_LINK_BW_2_7:
Ville Syrjälä1db10e22015-03-12 17:10:32 +0200146 case DP_LINK_BW_5_4:
Imre Deakd4eead52013-07-09 17:05:26 +0300147 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700148 default:
Imre Deakd4eead52013-07-09 17:05:26 +0300149 WARN(1, "invalid max DP link bw val %x, using 1.62Gbps\n",
150 max_link_bw);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700151 max_link_bw = DP_LINK_BW_1_62;
152 break;
153 }
154 return max_link_bw;
155}
156
Paulo Zanonieeb63242014-05-06 14:56:50 +0300157static u8 intel_dp_max_lane_count(struct intel_dp *intel_dp)
158{
159 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Paulo Zanonieeb63242014-05-06 14:56:50 +0300160 u8 source_max, sink_max;
161
Ville Syrjäläccb1a832015-12-08 19:59:38 +0200162 source_max = intel_dig_port->max_lanes;
Paulo Zanonieeb63242014-05-06 14:56:50 +0300163 sink_max = drm_dp_max_lane_count(intel_dp->dpcd);
164
165 return min(source_max, sink_max);
166}
167
Adam Jacksoncd9dde42011-10-14 12:43:49 -0400168/*
169 * The units on the numbers in the next two are... bizarre. Examples will
170 * make it clearer; this one parallels an example in the eDP spec.
171 *
172 * intel_dp_max_data_rate for one lane of 2.7GHz evaluates as:
173 *
174 * 270000 * 1 * 8 / 10 == 216000
175 *
176 * The actual data capacity of that configuration is 2.16Gbit/s, so the
177 * units are decakilobits. ->clock in a drm_display_mode is in kilohertz -
178 * or equivalently, kilopixels per second - so for 1680x1050R it'd be
179 * 119000. At 18bpp that's 2142000 kilobits per second.
180 *
181 * Thus the strange-looking division by 10 in intel_dp_link_required, to
182 * get the result in decakilobits instead of kilobits.
183 */
184
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700185static int
Keith Packardc8982612012-01-25 08:16:25 -0800186intel_dp_link_required(int pixel_clock, int bpp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700187{
Adam Jacksoncd9dde42011-10-14 12:43:49 -0400188 return (pixel_clock * bpp + 9) / 10;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700189}
190
191static int
Dave Airliefe27d532010-06-30 11:46:17 +1000192intel_dp_max_data_rate(int max_link_clock, int max_lanes)
193{
194 return (max_link_clock * max_lanes * 8) / 10;
195}
196
Damien Lespiauc19de8e2013-11-28 15:29:18 +0000197static enum drm_mode_status
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700198intel_dp_mode_valid(struct drm_connector *connector,
199 struct drm_display_mode *mode)
200{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100201 struct intel_dp *intel_dp = intel_attached_dp(connector);
Jani Nikuladd06f902012-10-19 14:51:50 +0300202 struct intel_connector *intel_connector = to_intel_connector(connector);
203 struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
Daniel Vetter36008362013-03-27 00:44:59 +0100204 int target_clock = mode->clock;
205 int max_rate, mode_rate, max_lanes, max_link_clock;
Mika Kahola799487f2016-02-02 15:16:38 +0200206 int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700207
Jani Nikuladd06f902012-10-19 14:51:50 +0300208 if (is_edp(intel_dp) && fixed_mode) {
209 if (mode->hdisplay > fixed_mode->hdisplay)
Zhao Yakui7de56f42010-07-19 09:43:14 +0100210 return MODE_PANEL;
211
Jani Nikuladd06f902012-10-19 14:51:50 +0300212 if (mode->vdisplay > fixed_mode->vdisplay)
Zhao Yakui7de56f42010-07-19 09:43:14 +0100213 return MODE_PANEL;
Daniel Vetter03afc4a2013-04-02 23:42:31 +0200214
215 target_clock = fixed_mode->clock;
Zhao Yakui7de56f42010-07-19 09:43:14 +0100216 }
217
Ville Syrjälä50fec212015-03-12 17:10:34 +0200218 max_link_clock = intel_dp_max_link_rate(intel_dp);
Paulo Zanonieeb63242014-05-06 14:56:50 +0300219 max_lanes = intel_dp_max_lane_count(intel_dp);
Daniel Vetter36008362013-03-27 00:44:59 +0100220
221 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
222 mode_rate = intel_dp_link_required(target_clock, 18);
223
Mika Kahola799487f2016-02-02 15:16:38 +0200224 if (mode_rate > max_rate || target_clock > max_dotclk)
Daniel Vetterc4867932012-04-10 10:42:36 +0200225 return MODE_CLOCK_HIGH;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700226
227 if (mode->clock < 10000)
228 return MODE_CLOCK_LOW;
229
Daniel Vetter0af78a22012-05-23 11:30:55 +0200230 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
231 return MODE_H_ILLEGAL;
232
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700233 return MODE_OK;
234}
235
Rodrigo Vivia4f12892014-11-14 08:52:27 -0800236uint32_t intel_dp_pack_aux(const uint8_t *src, int src_bytes)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700237{
238 int i;
239 uint32_t v = 0;
240
241 if (src_bytes > 4)
242 src_bytes = 4;
243 for (i = 0; i < src_bytes; i++)
244 v |= ((uint32_t) src[i]) << ((3-i) * 8);
245 return v;
246}
247
Damien Lespiauc2af70e2015-02-10 19:32:23 +0000248static void intel_dp_unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700249{
250 int i;
251 if (dst_bytes > 4)
252 dst_bytes = 4;
253 for (i = 0; i < dst_bytes; i++)
254 dst[i] = src >> ((3-i) * 8);
255}
256
Jani Nikulabf13e812013-09-06 07:40:05 +0300257static void
258intel_dp_init_panel_power_sequencer(struct drm_device *dev,
Ville Syrjälä36b5f422014-10-16 21:27:30 +0300259 struct intel_dp *intel_dp);
Jani Nikulabf13e812013-09-06 07:40:05 +0300260static void
261intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
Ville Syrjälä36b5f422014-10-16 21:27:30 +0300262 struct intel_dp *intel_dp);
Jani Nikulabf13e812013-09-06 07:40:05 +0300263
Ville Syrjälä773538e82014-09-04 14:54:56 +0300264static void pps_lock(struct intel_dp *intel_dp)
265{
266 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
267 struct intel_encoder *encoder = &intel_dig_port->base;
268 struct drm_device *dev = encoder->base.dev;
269 struct drm_i915_private *dev_priv = dev->dev_private;
270 enum intel_display_power_domain power_domain;
271
272 /*
273 * See vlv_power_sequencer_reset() why we need
274 * a power domain reference here.
275 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +0100276 power_domain = intel_display_port_aux_power_domain(encoder);
Ville Syrjälä773538e82014-09-04 14:54:56 +0300277 intel_display_power_get(dev_priv, power_domain);
278
279 mutex_lock(&dev_priv->pps_mutex);
280}
281
282static void pps_unlock(struct intel_dp *intel_dp)
283{
284 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
285 struct intel_encoder *encoder = &intel_dig_port->base;
286 struct drm_device *dev = encoder->base.dev;
287 struct drm_i915_private *dev_priv = dev->dev_private;
288 enum intel_display_power_domain power_domain;
289
290 mutex_unlock(&dev_priv->pps_mutex);
291
Ville Syrjälä25f78f52015-11-16 15:01:04 +0100292 power_domain = intel_display_port_aux_power_domain(encoder);
Ville Syrjälä773538e82014-09-04 14:54:56 +0300293 intel_display_power_put(dev_priv, power_domain);
294}
295
Ville Syrjälä961a0db2014-10-16 21:29:42 +0300296static void
297vlv_power_sequencer_kick(struct intel_dp *intel_dp)
298{
299 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
300 struct drm_device *dev = intel_dig_port->base.base.dev;
301 struct drm_i915_private *dev_priv = dev->dev_private;
302 enum pipe pipe = intel_dp->pps_pipe;
Ville Syrjälä0047eed2015-07-10 10:56:24 +0300303 bool pll_enabled, release_cl_override = false;
304 enum dpio_phy phy = DPIO_PHY(pipe);
305 enum dpio_channel ch = vlv_pipe_to_channel(pipe);
Ville Syrjälä961a0db2014-10-16 21:29:42 +0300306 uint32_t DP;
307
308 if (WARN(I915_READ(intel_dp->output_reg) & DP_PORT_EN,
309 "skipping pipe %c power seqeuncer kick due to port %c being active\n",
310 pipe_name(pipe), port_name(intel_dig_port->port)))
311 return;
312
313 DRM_DEBUG_KMS("kicking pipe %c power sequencer for port %c\n",
314 pipe_name(pipe), port_name(intel_dig_port->port));
315
316 /* Preserve the BIOS-computed detected bit. This is
317 * supposed to be read-only.
318 */
319 DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
320 DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
321 DP |= DP_PORT_WIDTH(1);
322 DP |= DP_LINK_TRAIN_PAT_1;
323
324 if (IS_CHERRYVIEW(dev))
325 DP |= DP_PIPE_SELECT_CHV(pipe);
326 else if (pipe == PIPE_B)
327 DP |= DP_PIPEB_SELECT;
328
Ville Syrjäläd288f652014-10-28 13:20:22 +0200329 pll_enabled = I915_READ(DPLL(pipe)) & DPLL_VCO_ENABLE;
330
331 /*
332 * The DPLL for the pipe must be enabled for this to work.
333 * So enable temporarily it if it's not already enabled.
334 */
Ville Syrjälä0047eed2015-07-10 10:56:24 +0300335 if (!pll_enabled) {
336 release_cl_override = IS_CHERRYVIEW(dev) &&
337 !chv_phy_powergate_ch(dev_priv, phy, ch, true);
338
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +0000339 if (vlv_force_pll_on(dev, pipe, IS_CHERRYVIEW(dev) ?
340 &chv_dpll[0].dpll : &vlv_dpll[0].dpll)) {
341 DRM_ERROR("Failed to force on pll for pipe %c!\n",
342 pipe_name(pipe));
343 return;
344 }
Ville Syrjälä0047eed2015-07-10 10:56:24 +0300345 }
Ville Syrjäläd288f652014-10-28 13:20:22 +0200346
Ville Syrjälä961a0db2014-10-16 21:29:42 +0300347 /*
348 * Similar magic as in intel_dp_enable_port().
349 * We _must_ do this port enable + disable trick
350 * to make this power seqeuencer lock onto the port.
351 * Otherwise even VDD force bit won't work.
352 */
353 I915_WRITE(intel_dp->output_reg, DP);
354 POSTING_READ(intel_dp->output_reg);
355
356 I915_WRITE(intel_dp->output_reg, DP | DP_PORT_EN);
357 POSTING_READ(intel_dp->output_reg);
358
359 I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
360 POSTING_READ(intel_dp->output_reg);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200361
Ville Syrjälä0047eed2015-07-10 10:56:24 +0300362 if (!pll_enabled) {
Ville Syrjäläd288f652014-10-28 13:20:22 +0200363 vlv_force_pll_off(dev, pipe);
Ville Syrjälä0047eed2015-07-10 10:56:24 +0300364
365 if (release_cl_override)
366 chv_phy_powergate_ch(dev_priv, phy, ch, false);
367 }
Ville Syrjälä961a0db2014-10-16 21:29:42 +0300368}
369
Jani Nikulabf13e812013-09-06 07:40:05 +0300370static enum pipe
371vlv_power_sequencer_pipe(struct intel_dp *intel_dp)
372{
373 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Jani Nikulabf13e812013-09-06 07:40:05 +0300374 struct drm_device *dev = intel_dig_port->base.base.dev;
375 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300376 struct intel_encoder *encoder;
377 unsigned int pipes = (1 << PIPE_A) | (1 << PIPE_B);
Ville Syrjäläa8c33442014-10-16 21:29:59 +0300378 enum pipe pipe;
Jani Nikulabf13e812013-09-06 07:40:05 +0300379
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300380 lockdep_assert_held(&dev_priv->pps_mutex);
381
Ville Syrjäläa8c33442014-10-16 21:29:59 +0300382 /* We should never land here with regular DP ports */
383 WARN_ON(!is_edp(intel_dp));
384
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300385 if (intel_dp->pps_pipe != INVALID_PIPE)
386 return intel_dp->pps_pipe;
Jani Nikulabf13e812013-09-06 07:40:05 +0300387
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300388 /*
389 * We don't have power sequencer currently.
390 * Pick one that's not used by other ports.
391 */
Jani Nikula19c80542015-12-16 12:48:16 +0200392 for_each_intel_encoder(dev, encoder) {
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300393 struct intel_dp *tmp;
394
395 if (encoder->type != INTEL_OUTPUT_EDP)
396 continue;
397
398 tmp = enc_to_intel_dp(&encoder->base);
399
400 if (tmp->pps_pipe != INVALID_PIPE)
401 pipes &= ~(1 << tmp->pps_pipe);
402 }
403
404 /*
405 * Didn't find one. This should not happen since there
406 * are two power sequencers and up to two eDP ports.
407 */
408 if (WARN_ON(pipes == 0))
Ville Syrjäläa8c33442014-10-16 21:29:59 +0300409 pipe = PIPE_A;
410 else
411 pipe = ffs(pipes) - 1;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300412
Ville Syrjäläa8c33442014-10-16 21:29:59 +0300413 vlv_steal_power_sequencer(dev, pipe);
414 intel_dp->pps_pipe = pipe;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300415
416 DRM_DEBUG_KMS("picked pipe %c power sequencer for port %c\n",
417 pipe_name(intel_dp->pps_pipe),
418 port_name(intel_dig_port->port));
419
420 /* init power sequencer on this pipe and port */
Ville Syrjälä36b5f422014-10-16 21:27:30 +0300421 intel_dp_init_panel_power_sequencer(dev, intel_dp);
422 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300423
Ville Syrjälä961a0db2014-10-16 21:29:42 +0300424 /*
425 * Even vdd force doesn't work until we've made
426 * the power sequencer lock in on the port.
427 */
428 vlv_power_sequencer_kick(intel_dp);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300429
430 return intel_dp->pps_pipe;
431}
432
Ville Syrjälä6491ab22014-08-18 22:16:06 +0300433typedef bool (*vlv_pipe_check)(struct drm_i915_private *dev_priv,
434 enum pipe pipe);
435
436static bool vlv_pipe_has_pp_on(struct drm_i915_private *dev_priv,
437 enum pipe pipe)
438{
439 return I915_READ(VLV_PIPE_PP_STATUS(pipe)) & PP_ON;
440}
441
442static bool vlv_pipe_has_vdd_on(struct drm_i915_private *dev_priv,
443 enum pipe pipe)
444{
445 return I915_READ(VLV_PIPE_PP_CONTROL(pipe)) & EDP_FORCE_VDD;
446}
447
448static bool vlv_pipe_any(struct drm_i915_private *dev_priv,
449 enum pipe pipe)
450{
451 return true;
452}
453
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300454static enum pipe
Ville Syrjälä6491ab22014-08-18 22:16:06 +0300455vlv_initial_pps_pipe(struct drm_i915_private *dev_priv,
456 enum port port,
457 vlv_pipe_check pipe_check)
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300458{
Jani Nikulabf13e812013-09-06 07:40:05 +0300459 enum pipe pipe;
460
Jani Nikulabf13e812013-09-06 07:40:05 +0300461 for (pipe = PIPE_A; pipe <= PIPE_B; pipe++) {
462 u32 port_sel = I915_READ(VLV_PIPE_PP_ON_DELAYS(pipe)) &
463 PANEL_PORT_SELECT_MASK;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300464
465 if (port_sel != PANEL_PORT_SELECT_VLV(port))
466 continue;
467
Ville Syrjälä6491ab22014-08-18 22:16:06 +0300468 if (!pipe_check(dev_priv, pipe))
469 continue;
470
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300471 return pipe;
Jani Nikulabf13e812013-09-06 07:40:05 +0300472 }
473
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300474 return INVALID_PIPE;
475}
476
477static void
478vlv_initial_power_sequencer_setup(struct intel_dp *intel_dp)
479{
480 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
481 struct drm_device *dev = intel_dig_port->base.base.dev;
482 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300483 enum port port = intel_dig_port->port;
484
485 lockdep_assert_held(&dev_priv->pps_mutex);
486
487 /* try to find a pipe with this port selected */
Ville Syrjälä6491ab22014-08-18 22:16:06 +0300488 /* first pick one where the panel is on */
489 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
490 vlv_pipe_has_pp_on);
491 /* didn't find one? pick one where vdd is on */
492 if (intel_dp->pps_pipe == INVALID_PIPE)
493 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
494 vlv_pipe_has_vdd_on);
495 /* didn't find one? pick one with just the correct port */
496 if (intel_dp->pps_pipe == INVALID_PIPE)
497 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
498 vlv_pipe_any);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +0300499
500 /* didn't find one? just let vlv_power_sequencer_pipe() pick one when needed */
501 if (intel_dp->pps_pipe == INVALID_PIPE) {
502 DRM_DEBUG_KMS("no initial power sequencer for port %c\n",
503 port_name(port));
504 return;
505 }
506
507 DRM_DEBUG_KMS("initial power sequencer for port %c: pipe %c\n",
508 port_name(port), pipe_name(intel_dp->pps_pipe));
509
Ville Syrjälä36b5f422014-10-16 21:27:30 +0300510 intel_dp_init_panel_power_sequencer(dev, intel_dp);
511 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
Jani Nikulabf13e812013-09-06 07:40:05 +0300512}
513
Ville Syrjälä773538e82014-09-04 14:54:56 +0300514void vlv_power_sequencer_reset(struct drm_i915_private *dev_priv)
515{
516 struct drm_device *dev = dev_priv->dev;
517 struct intel_encoder *encoder;
518
Wayne Boyer666a4532015-12-09 12:29:35 -0800519 if (WARN_ON(!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)))
Ville Syrjälä773538e82014-09-04 14:54:56 +0300520 return;
521
522 /*
523 * We can't grab pps_mutex here due to deadlock with power_domain
524 * mutex when power_domain functions are called while holding pps_mutex.
525 * That also means that in order to use pps_pipe the code needs to
526 * hold both a power domain reference and pps_mutex, and the power domain
527 * reference get/put must be done while _not_ holding pps_mutex.
528 * pps_{lock,unlock}() do these steps in the correct order, so one
529 * should use them always.
530 */
531
Jani Nikula19c80542015-12-16 12:48:16 +0200532 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä773538e82014-09-04 14:54:56 +0300533 struct intel_dp *intel_dp;
534
535 if (encoder->type != INTEL_OUTPUT_EDP)
536 continue;
537
538 intel_dp = enc_to_intel_dp(&encoder->base);
539 intel_dp->pps_pipe = INVALID_PIPE;
540 }
Jani Nikulabf13e812013-09-06 07:40:05 +0300541}
542
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200543static i915_reg_t
544_pp_ctrl_reg(struct intel_dp *intel_dp)
Jani Nikulabf13e812013-09-06 07:40:05 +0300545{
546 struct drm_device *dev = intel_dp_to_dev(intel_dp);
547
Vandana Kannanb0a08be2015-06-18 11:00:55 +0530548 if (IS_BROXTON(dev))
549 return BXT_PP_CONTROL(0);
550 else if (HAS_PCH_SPLIT(dev))
Jani Nikulabf13e812013-09-06 07:40:05 +0300551 return PCH_PP_CONTROL;
552 else
553 return VLV_PIPE_PP_CONTROL(vlv_power_sequencer_pipe(intel_dp));
554}
555
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200556static i915_reg_t
557_pp_stat_reg(struct intel_dp *intel_dp)
Jani Nikulabf13e812013-09-06 07:40:05 +0300558{
559 struct drm_device *dev = intel_dp_to_dev(intel_dp);
560
Vandana Kannanb0a08be2015-06-18 11:00:55 +0530561 if (IS_BROXTON(dev))
562 return BXT_PP_STATUS(0);
563 else if (HAS_PCH_SPLIT(dev))
Jani Nikulabf13e812013-09-06 07:40:05 +0300564 return PCH_PP_STATUS;
565 else
566 return VLV_PIPE_PP_STATUS(vlv_power_sequencer_pipe(intel_dp));
567}
568
Clint Taylor01527b32014-07-07 13:01:46 -0700569/* Reboot notifier handler to shutdown panel power to guarantee T12 timing
570 This function only applicable when panel PM state is not to be tracked */
571static int edp_notify_handler(struct notifier_block *this, unsigned long code,
572 void *unused)
573{
574 struct intel_dp *intel_dp = container_of(this, typeof(* intel_dp),
575 edp_notifier);
576 struct drm_device *dev = intel_dp_to_dev(intel_dp);
577 struct drm_i915_private *dev_priv = dev->dev_private;
Clint Taylor01527b32014-07-07 13:01:46 -0700578
579 if (!is_edp(intel_dp) || code != SYS_RESTART)
580 return 0;
581
Ville Syrjälä773538e82014-09-04 14:54:56 +0300582 pps_lock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300583
Wayne Boyer666a4532015-12-09 12:29:35 -0800584 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300585 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200586 i915_reg_t pp_ctrl_reg, pp_div_reg;
Ville Syrjälä649636e2015-09-22 19:50:01 +0300587 u32 pp_div;
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300588
Clint Taylor01527b32014-07-07 13:01:46 -0700589 pp_ctrl_reg = VLV_PIPE_PP_CONTROL(pipe);
590 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
591 pp_div = I915_READ(pp_div_reg);
592 pp_div &= PP_REFERENCE_DIVIDER_MASK;
593
594 /* 0x1F write to PP_DIV_REG sets max cycle delay */
595 I915_WRITE(pp_div_reg, pp_div | 0x1F);
596 I915_WRITE(pp_ctrl_reg, PANEL_UNLOCK_REGS | PANEL_POWER_OFF);
597 msleep(intel_dp->panel_power_cycle_delay);
598 }
599
Ville Syrjälä773538e82014-09-04 14:54:56 +0300600 pps_unlock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300601
Clint Taylor01527b32014-07-07 13:01:46 -0700602 return 0;
603}
604
Daniel Vetter4be73782014-01-17 14:39:48 +0100605static bool edp_have_panel_power(struct intel_dp *intel_dp)
Keith Packardebf33b12011-09-29 15:53:27 -0700606{
Paulo Zanoni30add222012-10-26 19:05:45 -0200607 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packardebf33b12011-09-29 15:53:27 -0700608 struct drm_i915_private *dev_priv = dev->dev_private;
609
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300610 lockdep_assert_held(&dev_priv->pps_mutex);
611
Wayne Boyer666a4532015-12-09 12:29:35 -0800612 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
Ville Syrjälä9a423562014-10-16 21:29:48 +0300613 intel_dp->pps_pipe == INVALID_PIPE)
614 return false;
615
Jani Nikulabf13e812013-09-06 07:40:05 +0300616 return (I915_READ(_pp_stat_reg(intel_dp)) & PP_ON) != 0;
Keith Packardebf33b12011-09-29 15:53:27 -0700617}
618
Daniel Vetter4be73782014-01-17 14:39:48 +0100619static bool edp_have_panel_vdd(struct intel_dp *intel_dp)
Keith Packardebf33b12011-09-29 15:53:27 -0700620{
Paulo Zanoni30add222012-10-26 19:05:45 -0200621 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packardebf33b12011-09-29 15:53:27 -0700622 struct drm_i915_private *dev_priv = dev->dev_private;
623
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300624 lockdep_assert_held(&dev_priv->pps_mutex);
625
Wayne Boyer666a4532015-12-09 12:29:35 -0800626 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
Ville Syrjälä9a423562014-10-16 21:29:48 +0300627 intel_dp->pps_pipe == INVALID_PIPE)
628 return false;
629
Ville Syrjälä773538e82014-09-04 14:54:56 +0300630 return I915_READ(_pp_ctrl_reg(intel_dp)) & EDP_FORCE_VDD;
Keith Packardebf33b12011-09-29 15:53:27 -0700631}
632
Keith Packard9b984da2011-09-19 13:54:47 -0700633static void
634intel_dp_check_edp(struct intel_dp *intel_dp)
635{
Paulo Zanoni30add222012-10-26 19:05:45 -0200636 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packard9b984da2011-09-19 13:54:47 -0700637 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packardebf33b12011-09-29 15:53:27 -0700638
Keith Packard9b984da2011-09-19 13:54:47 -0700639 if (!is_edp(intel_dp))
640 return;
Jesse Barnes453c5422013-03-28 09:55:41 -0700641
Daniel Vetter4be73782014-01-17 14:39:48 +0100642 if (!edp_have_panel_power(intel_dp) && !edp_have_panel_vdd(intel_dp)) {
Keith Packard9b984da2011-09-19 13:54:47 -0700643 WARN(1, "eDP powered off while attempting aux channel communication.\n");
644 DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
Jani Nikulabf13e812013-09-06 07:40:05 +0300645 I915_READ(_pp_stat_reg(intel_dp)),
646 I915_READ(_pp_ctrl_reg(intel_dp)));
Keith Packard9b984da2011-09-19 13:54:47 -0700647 }
648}
649
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100650static uint32_t
651intel_dp_aux_wait_done(struct intel_dp *intel_dp, bool has_aux_irq)
652{
653 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
654 struct drm_device *dev = intel_dig_port->base.base.dev;
655 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200656 i915_reg_t ch_ctl = intel_dp->aux_ch_ctl_reg;
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100657 uint32_t status;
658 bool done;
659
Daniel Vetteref04f002012-12-01 21:03:59 +0100660#define C (((status = I915_READ_NOTRACE(ch_ctl)) & DP_AUX_CH_CTL_SEND_BUSY) == 0)
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100661 if (has_aux_irq)
Paulo Zanonib18ac462013-02-18 19:00:24 -0300662 done = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
Imre Deak35987062013-05-21 20:03:20 +0300663 msecs_to_jiffies_timeout(10));
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100664 else
665 done = wait_for_atomic(C, 10) == 0;
666 if (!done)
667 DRM_ERROR("dp aux hw did not signal timeout (has irq: %i)!\n",
668 has_aux_irq);
669#undef C
670
671 return status;
672}
673
Ville Syrjälä6ffb1be2016-03-02 17:22:14 +0200674static uint32_t g4x_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000675{
676 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200677 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000678
Ville Syrjäläa457f542016-03-02 17:22:17 +0200679 if (index)
680 return 0;
681
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000682 /*
683 * The clock divider is based off the hrawclk, and would like to run at
Ville Syrjäläa457f542016-03-02 17:22:17 +0200684 * 2MHz. So, take the hrawclk value and divide by 2000 and use that
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000685 */
Ville Syrjäläa457f542016-03-02 17:22:17 +0200686 return DIV_ROUND_CLOSEST(dev_priv->rawclk_freq, 2000);
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000687}
688
689static uint32_t ilk_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
690{
691 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Ville Syrjäläa457f542016-03-02 17:22:17 +0200692 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000693
694 if (index)
695 return 0;
696
Ville Syrjäläa457f542016-03-02 17:22:17 +0200697 /*
698 * The clock divider is based off the cdclk or PCH rawclk, and would
699 * like to run at 2MHz. So, take the cdclk or PCH rawclk value and
700 * divide by 2000 and use that
701 */
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200702 if (intel_dig_port->port == PORT_A)
Ville Syrjäläfce18c42015-11-30 16:23:46 +0200703 return DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 2000);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200704 else
705 return DIV_ROUND_CLOSEST(dev_priv->rawclk_freq, 2000);
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000706}
707
708static uint32_t hsw_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300709{
710 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Ville Syrjäläa457f542016-03-02 17:22:17 +0200711 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300712
Ville Syrjäläa457f542016-03-02 17:22:17 +0200713 if (intel_dig_port->port != PORT_A && HAS_PCH_LPT_H(dev_priv)) {
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300714 /* Workaround for non-ULT HSW */
Chris Wilsonbc866252013-07-21 16:00:03 +0100715 switch (index) {
716 case 0: return 63;
717 case 1: return 72;
718 default: return 0;
719 }
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300720 }
Ville Syrjäläa457f542016-03-02 17:22:17 +0200721
722 return ilk_get_aux_clock_divider(intel_dp, index);
Rodrigo Vivib84a1cf2013-07-11 18:44:57 -0300723}
724
Damien Lespiaub6b5e382014-01-20 16:00:59 +0000725static uint32_t skl_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
726{
727 /*
728 * SKL doesn't need us to program the AUX clock divider (Hardware will
729 * derive the clock from CDCLK automatically). We still implement the
730 * get_aux_clock_divider vfunc to plug-in into the existing code.
731 */
732 return index ? 0 : 1;
733}
734
Ville Syrjälä6ffb1be2016-03-02 17:22:14 +0200735static uint32_t g4x_get_aux_send_ctl(struct intel_dp *intel_dp,
736 bool has_aux_irq,
737 int send_bytes,
738 uint32_t aux_clock_divider)
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000739{
740 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
741 struct drm_device *dev = intel_dig_port->base.base.dev;
742 uint32_t precharge, timeout;
743
744 if (IS_GEN6(dev))
745 precharge = 3;
746 else
747 precharge = 5;
748
Ville Syrjäläf3c6a3a2015-11-11 20:34:10 +0200749 if (IS_BROADWELL(dev) && intel_dig_port->port == PORT_A)
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000750 timeout = DP_AUX_CH_CTL_TIME_OUT_600us;
751 else
752 timeout = DP_AUX_CH_CTL_TIME_OUT_400us;
753
754 return DP_AUX_CH_CTL_SEND_BUSY |
Damien Lespiau788d4432014-01-20 15:52:31 +0000755 DP_AUX_CH_CTL_DONE |
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000756 (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
Damien Lespiau788d4432014-01-20 15:52:31 +0000757 DP_AUX_CH_CTL_TIME_OUT_ERROR |
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000758 timeout |
Damien Lespiau788d4432014-01-20 15:52:31 +0000759 DP_AUX_CH_CTL_RECEIVE_ERROR |
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000760 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
761 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
Damien Lespiau788d4432014-01-20 15:52:31 +0000762 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT);
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000763}
764
Damien Lespiaub9ca5fa2014-01-20 16:01:00 +0000765static uint32_t skl_get_aux_send_ctl(struct intel_dp *intel_dp,
766 bool has_aux_irq,
767 int send_bytes,
768 uint32_t unused)
769{
770 return DP_AUX_CH_CTL_SEND_BUSY |
771 DP_AUX_CH_CTL_DONE |
772 (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
773 DP_AUX_CH_CTL_TIME_OUT_ERROR |
774 DP_AUX_CH_CTL_TIME_OUT_1600us |
775 DP_AUX_CH_CTL_RECEIVE_ERROR |
776 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
777 DP_AUX_CH_CTL_SYNC_PULSE_SKL(32);
778}
779
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700780static int
Chris Wilsonea5b2132010-08-04 13:50:23 +0100781intel_dp_aux_ch(struct intel_dp *intel_dp,
Daniel Vetterbd9f74a2014-10-02 09:45:35 +0200782 const uint8_t *send, int send_bytes,
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700783 uint8_t *recv, int recv_size)
784{
Paulo Zanoni174edf12012-10-26 19:05:50 -0200785 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
786 struct drm_device *dev = intel_dig_port->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700787 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200788 i915_reg_t ch_ctl = intel_dp->aux_ch_ctl_reg;
Chris Wilsonbc866252013-07-21 16:00:03 +0100789 uint32_t aux_clock_divider;
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100790 int i, ret, recv_bytes;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700791 uint32_t status;
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000792 int try, clock = 0;
Daniel Vetter4e6b7882014-02-07 16:33:20 +0100793 bool has_aux_irq = HAS_AUX_IRQ(dev);
Jani Nikula884f19e2014-03-14 16:51:14 +0200794 bool vdd;
795
Ville Syrjälä773538e82014-09-04 14:54:56 +0300796 pps_lock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300797
Ville Syrjälä72c35002014-08-18 22:16:00 +0300798 /*
799 * We will be called with VDD already enabled for dpcd/edid/oui reads.
800 * In such cases we want to leave VDD enabled and it's up to upper layers
801 * to turn it off. But for eg. i2c-dev access we need to turn it on/off
802 * ourselves.
803 */
Ville Syrjälä1e0560e2014-08-19 13:24:25 +0300804 vdd = edp_panel_vdd_on(intel_dp);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100805
806 /* dp aux is extremely sensitive to irq latency, hence request the
807 * lowest possible wakeup latency and so prevent the cpu from going into
808 * deep sleep states.
809 */
810 pm_qos_update_request(&dev_priv->pm_qos, 0);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700811
Keith Packard9b984da2011-09-19 13:54:47 -0700812 intel_dp_check_edp(intel_dp);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800813
Jesse Barnes11bee432011-08-01 15:02:20 -0700814 /* Try to wait for any previous AUX channel activity */
815 for (try = 0; try < 3; try++) {
Daniel Vetteref04f002012-12-01 21:03:59 +0100816 status = I915_READ_NOTRACE(ch_ctl);
Jesse Barnes11bee432011-08-01 15:02:20 -0700817 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
818 break;
819 msleep(1);
820 }
821
822 if (try == 3) {
Mika Kuoppala02196c72015-08-06 16:48:58 +0300823 static u32 last_status = -1;
824 const u32 status = I915_READ(ch_ctl);
825
826 if (status != last_status) {
827 WARN(1, "dp_aux_ch not started status 0x%08x\n",
828 status);
829 last_status = status;
830 }
831
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100832 ret = -EBUSY;
833 goto out;
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100834 }
835
Paulo Zanoni46a5ae92013-09-17 11:14:10 -0300836 /* Only 5 data registers! */
837 if (WARN_ON(send_bytes > 20 || recv_size > 20)) {
838 ret = -E2BIG;
839 goto out;
840 }
841
Damien Lespiauec5b01d2014-01-21 13:35:39 +0000842 while ((aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, clock++))) {
Damien Lespiau153b1102014-01-21 13:37:15 +0000843 u32 send_ctl = intel_dp->get_aux_send_ctl(intel_dp,
844 has_aux_irq,
845 send_bytes,
846 aux_clock_divider);
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000847
Chris Wilsonbc866252013-07-21 16:00:03 +0100848 /* Must try at least 3 times according to DP spec */
849 for (try = 0; try < 5; try++) {
850 /* Load the send data into the aux channel data registers */
851 for (i = 0; i < send_bytes; i += 4)
Ville Syrjälä330e20e2015-11-11 20:34:14 +0200852 I915_WRITE(intel_dp->aux_ch_data_reg[i >> 2],
Rodrigo Vivia4f12892014-11-14 08:52:27 -0800853 intel_dp_pack_aux(send + i,
854 send_bytes - i));
Akshay Joshi0206e352011-08-16 15:34:10 -0400855
Chris Wilsonbc866252013-07-21 16:00:03 +0100856 /* Send the command and wait for it to complete */
Damien Lespiau5ed12a12014-01-20 15:52:30 +0000857 I915_WRITE(ch_ctl, send_ctl);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100858
Chris Wilsonbc866252013-07-21 16:00:03 +0100859 status = intel_dp_aux_wait_done(intel_dp, has_aux_irq);
Akshay Joshi0206e352011-08-16 15:34:10 -0400860
Chris Wilsonbc866252013-07-21 16:00:03 +0100861 /* Clear done status and any errors */
862 I915_WRITE(ch_ctl,
863 status |
864 DP_AUX_CH_CTL_DONE |
865 DP_AUX_CH_CTL_TIME_OUT_ERROR |
866 DP_AUX_CH_CTL_RECEIVE_ERROR);
Adam Jacksond7e96fe2011-07-26 15:39:46 -0400867
Todd Previte74ebf292015-04-15 08:38:41 -0700868 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR)
Chris Wilsonbc866252013-07-21 16:00:03 +0100869 continue;
Todd Previte74ebf292015-04-15 08:38:41 -0700870
871 /* DP CTS 1.2 Core Rev 1.1, 4.2.1.1 & 4.2.1.2
872 * 400us delay required for errors and timeouts
873 * Timeout errors from the HW already meet this
874 * requirement so skip to next iteration
875 */
876 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
877 usleep_range(400, 500);
878 continue;
879 }
Chris Wilsonbc866252013-07-21 16:00:03 +0100880 if (status & DP_AUX_CH_CTL_DONE)
Jim Bridee058c942015-05-27 10:21:48 -0700881 goto done;
Chris Wilsonbc866252013-07-21 16:00:03 +0100882 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700883 }
884
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700885 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700886 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100887 ret = -EBUSY;
888 goto out;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700889 }
890
Jim Bridee058c942015-05-27 10:21:48 -0700891done:
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700892 /* Check for timeout or receive error.
893 * Timeouts occur when the sink is not connected
894 */
Keith Packarda5b3da52009-06-11 22:30:32 -0700895 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700896 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100897 ret = -EIO;
898 goto out;
Keith Packarda5b3da52009-06-11 22:30:32 -0700899 }
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700900
901 /* Timeouts occur when the device isn't connected, so they're
902 * "normal" -- don't fill the kernel log with these */
Keith Packarda5b3da52009-06-11 22:30:32 -0700903 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
Zhao Yakui28c97732009-10-09 11:39:41 +0800904 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100905 ret = -ETIMEDOUT;
906 goto out;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700907 }
908
909 /* Unload any bytes sent back from the other side */
910 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
911 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
Rodrigo Vivi14e01882015-12-10 11:12:27 -0800912
913 /*
914 * By BSpec: "Message sizes of 0 or >20 are not allowed."
915 * We have no idea of what happened so we return -EBUSY so
916 * drm layer takes care for the necessary retries.
917 */
918 if (recv_bytes == 0 || recv_bytes > 20) {
919 DRM_DEBUG_KMS("Forbidden recv_bytes = %d on aux transaction\n",
920 recv_bytes);
921 /*
922 * FIXME: This patch was created on top of a series that
923 * organize the retries at drm level. There EBUSY should
924 * also take care for 1ms wait before retrying.
925 * That aux retries re-org is still needed and after that is
926 * merged we remove this sleep from here.
927 */
928 usleep_range(1000, 1500);
929 ret = -EBUSY;
930 goto out;
931 }
932
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700933 if (recv_bytes > recv_size)
934 recv_bytes = recv_size;
Akshay Joshi0206e352011-08-16 15:34:10 -0400935
Chris Wilson4f7f7b72010-08-18 18:12:56 +0100936 for (i = 0; i < recv_bytes; i += 4)
Ville Syrjälä330e20e2015-11-11 20:34:14 +0200937 intel_dp_unpack_aux(I915_READ(intel_dp->aux_ch_data_reg[i >> 2]),
Rodrigo Vivia4f12892014-11-14 08:52:27 -0800938 recv + i, recv_bytes - i);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700939
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100940 ret = recv_bytes;
941out:
942 pm_qos_update_request(&dev_priv->pm_qos, PM_QOS_DEFAULT_VALUE);
943
Jani Nikula884f19e2014-03-14 16:51:14 +0200944 if (vdd)
945 edp_panel_vdd_off(intel_dp, false);
946
Ville Syrjälä773538e82014-09-04 14:54:56 +0300947 pps_unlock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +0300948
Daniel Vetter9ee32fea2012-12-01 13:53:48 +0100949 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700950}
951
Jani Nikulaa6c8aff02014-04-07 12:37:25 +0300952#define BARE_ADDRESS_SIZE 3
953#define HEADER_SIZE (BARE_ADDRESS_SIZE + 1)
Jani Nikula9d1a1032014-03-14 16:51:15 +0200954static ssize_t
955intel_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700956{
Jani Nikula9d1a1032014-03-14 16:51:15 +0200957 struct intel_dp *intel_dp = container_of(aux, struct intel_dp, aux);
958 uint8_t txbuf[20], rxbuf[20];
959 size_t txsize, rxsize;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700960 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700961
Ville Syrjäläd2d9cbb2015-03-19 11:44:06 +0200962 txbuf[0] = (msg->request << 4) |
963 ((msg->address >> 16) & 0xf);
964 txbuf[1] = (msg->address >> 8) & 0xff;
Jani Nikula9d1a1032014-03-14 16:51:15 +0200965 txbuf[2] = msg->address & 0xff;
966 txbuf[3] = msg->size - 1;
Paulo Zanoni46a5ae92013-09-17 11:14:10 -0300967
Jani Nikula9d1a1032014-03-14 16:51:15 +0200968 switch (msg->request & ~DP_AUX_I2C_MOT) {
969 case DP_AUX_NATIVE_WRITE:
970 case DP_AUX_I2C_WRITE:
Ville Syrjäläc1e741222015-08-27 17:23:27 +0300971 case DP_AUX_I2C_WRITE_STATUS_UPDATE:
Jani Nikulaa6c8aff02014-04-07 12:37:25 +0300972 txsize = msg->size ? HEADER_SIZE + msg->size : BARE_ADDRESS_SIZE;
Jani Nikulaa1ddefd2015-03-17 17:18:54 +0200973 rxsize = 2; /* 0 or 1 data bytes */
Jani Nikulaf51a44b2014-02-11 11:52:05 +0200974
Jani Nikula9d1a1032014-03-14 16:51:15 +0200975 if (WARN_ON(txsize > 20))
976 return -E2BIG;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700977
Imre Deakd81a67c2016-01-29 14:52:26 +0200978 if (msg->buffer)
979 memcpy(txbuf + HEADER_SIZE, msg->buffer, msg->size);
980 else
981 WARN_ON(msg->size);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700982
Jani Nikula9d1a1032014-03-14 16:51:15 +0200983 ret = intel_dp_aux_ch(intel_dp, txbuf, txsize, rxbuf, rxsize);
984 if (ret > 0) {
985 msg->reply = rxbuf[0] >> 4;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700986
Jani Nikulaa1ddefd2015-03-17 17:18:54 +0200987 if (ret > 1) {
988 /* Number of bytes written in a short write. */
989 ret = clamp_t(int, rxbuf[1], 0, msg->size);
990 } else {
991 /* Return payload size. */
992 ret = msg->size;
993 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700994 }
Jani Nikula9d1a1032014-03-14 16:51:15 +0200995 break;
996
997 case DP_AUX_NATIVE_READ:
998 case DP_AUX_I2C_READ:
Jani Nikulaa6c8aff02014-04-07 12:37:25 +0300999 txsize = msg->size ? HEADER_SIZE : BARE_ADDRESS_SIZE;
Jani Nikula9d1a1032014-03-14 16:51:15 +02001000 rxsize = msg->size + 1;
1001
1002 if (WARN_ON(rxsize > 20))
1003 return -E2BIG;
1004
1005 ret = intel_dp_aux_ch(intel_dp, txbuf, txsize, rxbuf, rxsize);
1006 if (ret > 0) {
1007 msg->reply = rxbuf[0] >> 4;
1008 /*
1009 * Assume happy day, and copy the data. The caller is
1010 * expected to check msg->reply before touching it.
1011 *
1012 * Return payload size.
1013 */
1014 ret--;
1015 memcpy(msg->buffer, rxbuf + 1, ret);
1016 }
1017 break;
1018
1019 default:
1020 ret = -EINVAL;
1021 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001022 }
Jani Nikulaf51a44b2014-02-11 11:52:05 +02001023
Jani Nikula9d1a1032014-03-14 16:51:15 +02001024 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001025}
1026
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001027static i915_reg_t g4x_aux_ctl_reg(struct drm_i915_private *dev_priv,
1028 enum port port)
Ville Syrjäläda00bdc2015-11-11 20:34:13 +02001029{
1030 switch (port) {
1031 case PORT_B:
1032 case PORT_C:
1033 case PORT_D:
1034 return DP_AUX_CH_CTL(port);
1035 default:
1036 MISSING_CASE(port);
1037 return DP_AUX_CH_CTL(PORT_B);
1038 }
1039}
1040
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001041static i915_reg_t g4x_aux_data_reg(struct drm_i915_private *dev_priv,
1042 enum port port, int index)
Ville Syrjälä330e20e2015-11-11 20:34:14 +02001043{
1044 switch (port) {
1045 case PORT_B:
1046 case PORT_C:
1047 case PORT_D:
1048 return DP_AUX_CH_DATA(port, index);
1049 default:
1050 MISSING_CASE(port);
1051 return DP_AUX_CH_DATA(PORT_B, index);
1052 }
1053}
1054
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001055static i915_reg_t ilk_aux_ctl_reg(struct drm_i915_private *dev_priv,
1056 enum port port)
Ville Syrjäläda00bdc2015-11-11 20:34:13 +02001057{
1058 switch (port) {
1059 case PORT_A:
1060 return DP_AUX_CH_CTL(port);
1061 case PORT_B:
1062 case PORT_C:
1063 case PORT_D:
1064 return PCH_DP_AUX_CH_CTL(port);
1065 default:
1066 MISSING_CASE(port);
1067 return DP_AUX_CH_CTL(PORT_A);
1068 }
1069}
1070
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001071static i915_reg_t ilk_aux_data_reg(struct drm_i915_private *dev_priv,
1072 enum port port, int index)
Ville Syrjälä330e20e2015-11-11 20:34:14 +02001073{
1074 switch (port) {
1075 case PORT_A:
1076 return DP_AUX_CH_DATA(port, index);
1077 case PORT_B:
1078 case PORT_C:
1079 case PORT_D:
1080 return PCH_DP_AUX_CH_DATA(port, index);
1081 default:
1082 MISSING_CASE(port);
1083 return DP_AUX_CH_DATA(PORT_A, index);
1084 }
1085}
1086
Ville Syrjäläda00bdc2015-11-11 20:34:13 +02001087/*
1088 * On SKL we don't have Aux for port E so we rely
1089 * on VBT to set a proper alternate aux channel.
1090 */
1091static enum port skl_porte_aux_port(struct drm_i915_private *dev_priv)
1092{
1093 const struct ddi_vbt_port_info *info =
1094 &dev_priv->vbt.ddi_port_info[PORT_E];
1095
1096 switch (info->alternate_aux_channel) {
1097 case DP_AUX_A:
1098 return PORT_A;
1099 case DP_AUX_B:
1100 return PORT_B;
1101 case DP_AUX_C:
1102 return PORT_C;
1103 case DP_AUX_D:
1104 return PORT_D;
1105 default:
1106 MISSING_CASE(info->alternate_aux_channel);
1107 return PORT_A;
1108 }
1109}
1110
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001111static i915_reg_t skl_aux_ctl_reg(struct drm_i915_private *dev_priv,
1112 enum port port)
Ville Syrjäläda00bdc2015-11-11 20:34:13 +02001113{
1114 if (port == PORT_E)
1115 port = skl_porte_aux_port(dev_priv);
1116
1117 switch (port) {
1118 case PORT_A:
1119 case PORT_B:
1120 case PORT_C:
1121 case PORT_D:
1122 return DP_AUX_CH_CTL(port);
1123 default:
1124 MISSING_CASE(port);
1125 return DP_AUX_CH_CTL(PORT_A);
1126 }
1127}
1128
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001129static i915_reg_t skl_aux_data_reg(struct drm_i915_private *dev_priv,
1130 enum port port, int index)
Ville Syrjälä330e20e2015-11-11 20:34:14 +02001131{
1132 if (port == PORT_E)
1133 port = skl_porte_aux_port(dev_priv);
1134
1135 switch (port) {
1136 case PORT_A:
1137 case PORT_B:
1138 case PORT_C:
1139 case PORT_D:
1140 return DP_AUX_CH_DATA(port, index);
1141 default:
1142 MISSING_CASE(port);
1143 return DP_AUX_CH_DATA(PORT_A, index);
1144 }
1145}
1146
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001147static i915_reg_t intel_aux_ctl_reg(struct drm_i915_private *dev_priv,
1148 enum port port)
Ville Syrjälä330e20e2015-11-11 20:34:14 +02001149{
1150 if (INTEL_INFO(dev_priv)->gen >= 9)
1151 return skl_aux_ctl_reg(dev_priv, port);
1152 else if (HAS_PCH_SPLIT(dev_priv))
1153 return ilk_aux_ctl_reg(dev_priv, port);
1154 else
1155 return g4x_aux_ctl_reg(dev_priv, port);
1156}
1157
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001158static i915_reg_t intel_aux_data_reg(struct drm_i915_private *dev_priv,
1159 enum port port, int index)
Ville Syrjälä330e20e2015-11-11 20:34:14 +02001160{
1161 if (INTEL_INFO(dev_priv)->gen >= 9)
1162 return skl_aux_data_reg(dev_priv, port, index);
1163 else if (HAS_PCH_SPLIT(dev_priv))
1164 return ilk_aux_data_reg(dev_priv, port, index);
1165 else
1166 return g4x_aux_data_reg(dev_priv, port, index);
1167}
1168
1169static void intel_aux_reg_init(struct intel_dp *intel_dp)
1170{
1171 struct drm_i915_private *dev_priv = to_i915(intel_dp_to_dev(intel_dp));
1172 enum port port = dp_to_dig_port(intel_dp)->port;
1173 int i;
1174
1175 intel_dp->aux_ch_ctl_reg = intel_aux_ctl_reg(dev_priv, port);
1176 for (i = 0; i < ARRAY_SIZE(intel_dp->aux_ch_data_reg); i++)
1177 intel_dp->aux_ch_data_reg[i] = intel_aux_data_reg(dev_priv, port, i);
1178}
1179
Jani Nikula9d1a1032014-03-14 16:51:15 +02001180static void
Ville Syrjäläa121f4e2015-11-11 20:34:11 +02001181intel_dp_aux_fini(struct intel_dp *intel_dp)
1182{
1183 drm_dp_aux_unregister(&intel_dp->aux);
1184 kfree(intel_dp->aux.name);
1185}
1186
1187static int
Jani Nikula9d1a1032014-03-14 16:51:15 +02001188intel_dp_aux_init(struct intel_dp *intel_dp, struct intel_connector *connector)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001189{
Jani Nikula33ad6622014-03-14 16:51:16 +02001190 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1191 enum port port = intel_dig_port->port;
Dave Airlieab2c0672009-12-04 10:55:24 +10001192 int ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001193
Ville Syrjälä330e20e2015-11-11 20:34:14 +02001194 intel_aux_reg_init(intel_dp);
David Flynn8316f332010-12-08 16:10:21 +00001195
Ville Syrjäläa121f4e2015-11-11 20:34:11 +02001196 intel_dp->aux.name = kasprintf(GFP_KERNEL, "DPDDC-%c", port_name(port));
1197 if (!intel_dp->aux.name)
1198 return -ENOMEM;
1199
Rafael Antognolli4d32c0d2016-01-21 15:10:20 -08001200 intel_dp->aux.dev = connector->base.kdev;
Jani Nikula9d1a1032014-03-14 16:51:15 +02001201 intel_dp->aux.transfer = intel_dp_aux_transfer;
David Flynn8316f332010-12-08 16:10:21 +00001202
Ville Syrjäläa121f4e2015-11-11 20:34:11 +02001203 DRM_DEBUG_KMS("registering %s bus for %s\n",
1204 intel_dp->aux.name,
Jani Nikula0b998362014-03-14 16:51:17 +02001205 connector->base.kdev->kobj.name);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001206
Dave Airlie4f71d0c2014-06-04 16:02:28 +10001207 ret = drm_dp_aux_register(&intel_dp->aux);
Jani Nikula0b998362014-03-14 16:51:17 +02001208 if (ret < 0) {
Dave Airlie4f71d0c2014-06-04 16:02:28 +10001209 DRM_ERROR("drm_dp_aux_register() for %s failed (%d)\n",
Ville Syrjäläa121f4e2015-11-11 20:34:11 +02001210 intel_dp->aux.name, ret);
1211 kfree(intel_dp->aux.name);
1212 return ret;
Dave Airlieab2c0672009-12-04 10:55:24 +10001213 }
David Flynn8316f332010-12-08 16:10:21 +00001214
Ville Syrjäläa121f4e2015-11-11 20:34:11 +02001215 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001216}
1217
Imre Deak80f65de2014-02-11 17:12:49 +02001218static void
1219intel_dp_connector_unregister(struct intel_connector *intel_connector)
1220{
1221 struct intel_dp *intel_dp = intel_attached_dp(&intel_connector->base);
1222
Rafael Antognolli4d32c0d2016-01-21 15:10:20 -08001223 intel_dp_aux_fini(intel_dp);
Imre Deak80f65de2014-02-11 17:12:49 +02001224 intel_connector_unregister(intel_connector);
1225}
1226
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05301227static int
Ville Syrjälä12f6a2e2015-03-12 17:10:30 +02001228intel_dp_sink_rates(struct intel_dp *intel_dp, const int **sink_rates)
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05301229{
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001230 if (intel_dp->num_sink_rates) {
1231 *sink_rates = intel_dp->sink_rates;
1232 return intel_dp->num_sink_rates;
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05301233 }
Ville Syrjälä12f6a2e2015-03-12 17:10:30 +02001234
1235 *sink_rates = default_rates;
1236
1237 return (intel_dp_max_link_bw(intel_dp) >> 3) + 1;
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05301238}
1239
Ander Conselvan de Oliveirae588fa12015-10-23 13:01:50 +03001240bool intel_dp_source_supports_hbr2(struct intel_dp *intel_dp)
Thulasimani,Sivakumared63baa2015-08-18 15:30:37 +05301241{
Ander Conselvan de Oliveirae588fa12015-10-23 13:01:50 +03001242 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1243 struct drm_device *dev = dig_port->base.base.dev;
1244
Thulasimani,Sivakumared63baa2015-08-18 15:30:37 +05301245 /* WaDisableHBR2:skl */
Jani Nikulae87a0052015-10-20 15:22:02 +03001246 if (IS_SKL_REVID(dev, 0, SKL_REVID_B0))
Thulasimani,Sivakumared63baa2015-08-18 15:30:37 +05301247 return false;
1248
1249 if ((IS_HASWELL(dev) && !IS_HSW_ULX(dev)) || IS_BROADWELL(dev) ||
1250 (INTEL_INFO(dev)->gen >= 9))
1251 return true;
1252 else
1253 return false;
1254}
1255
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301256static int
Ander Conselvan de Oliveirae588fa12015-10-23 13:01:50 +03001257intel_dp_source_rates(struct intel_dp *intel_dp, const int **source_rates)
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301258{
Ander Conselvan de Oliveirae588fa12015-10-23 13:01:50 +03001259 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1260 struct drm_device *dev = dig_port->base.base.dev;
Thulasimani,Sivakumaraf7080f2015-08-18 11:07:59 +05301261 int size;
1262
Sonika Jindal64987fc2015-05-26 17:50:13 +05301263 if (IS_BROXTON(dev)) {
1264 *source_rates = bxt_rates;
Thulasimani,Sivakumaraf7080f2015-08-18 11:07:59 +05301265 size = ARRAY_SIZE(bxt_rates);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07001266 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Sonika Jindal637a9c62015-05-07 09:52:08 +05301267 *source_rates = skl_rates;
Thulasimani,Sivakumaraf7080f2015-08-18 11:07:59 +05301268 size = ARRAY_SIZE(skl_rates);
1269 } else {
1270 *source_rates = default_rates;
1271 size = ARRAY_SIZE(default_rates);
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301272 }
Ville Syrjälä636280b2015-03-12 17:10:29 +02001273
Thulasimani,Sivakumared63baa2015-08-18 15:30:37 +05301274 /* This depends on the fact that 5.4 is last value in the array */
Ander Conselvan de Oliveirae588fa12015-10-23 13:01:50 +03001275 if (!intel_dp_source_supports_hbr2(intel_dp))
Thulasimani,Sivakumaraf7080f2015-08-18 11:07:59 +05301276 size--;
Ville Syrjälä636280b2015-03-12 17:10:29 +02001277
Thulasimani,Sivakumaraf7080f2015-08-18 11:07:59 +05301278 return size;
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301279}
1280
Daniel Vetter0e503382014-07-04 11:26:04 -03001281static void
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001282intel_dp_set_clock(struct intel_encoder *encoder,
Ville Syrjälä840b32b2015-08-11 20:21:46 +03001283 struct intel_crtc_state *pipe_config)
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001284{
1285 struct drm_device *dev = encoder->base.dev;
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +08001286 const struct dp_link_dpll *divisor = NULL;
1287 int i, count = 0;
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001288
1289 if (IS_G4X(dev)) {
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +08001290 divisor = gen4_dpll;
1291 count = ARRAY_SIZE(gen4_dpll);
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001292 } else if (HAS_PCH_SPLIT(dev)) {
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +08001293 divisor = pch_dpll;
1294 count = ARRAY_SIZE(pch_dpll);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001295 } else if (IS_CHERRYVIEW(dev)) {
1296 divisor = chv_dpll;
1297 count = ARRAY_SIZE(chv_dpll);
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001298 } else if (IS_VALLEYVIEW(dev)) {
Chon Ming Lee65ce4bf2013-09-04 01:30:38 +08001299 divisor = vlv_dpll;
1300 count = ARRAY_SIZE(vlv_dpll);
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001301 }
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +08001302
1303 if (divisor && count) {
1304 for (i = 0; i < count; i++) {
Ville Syrjälä840b32b2015-08-11 20:21:46 +03001305 if (pipe_config->port_clock == divisor[i].clock) {
Chon Ming Lee9dd4ffd2013-09-04 01:30:37 +08001306 pipe_config->dpll = divisor[i].dpll;
1307 pipe_config->clock_set = true;
1308 break;
1309 }
1310 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001311 }
1312}
1313
Ville Syrjälä2ecae762015-03-12 17:10:33 +02001314static int intersect_rates(const int *source_rates, int source_len,
1315 const int *sink_rates, int sink_len,
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001316 int *common_rates)
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301317{
1318 int i = 0, j = 0, k = 0;
1319
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301320 while (i < source_len && j < sink_len) {
1321 if (source_rates[i] == sink_rates[j]) {
Ville Syrjäläe6bda3e2015-03-12 17:10:37 +02001322 if (WARN_ON(k >= DP_MAX_SUPPORTED_RATES))
1323 return k;
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001324 common_rates[k] = source_rates[i];
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301325 ++k;
1326 ++i;
1327 ++j;
1328 } else if (source_rates[i] < sink_rates[j]) {
1329 ++i;
1330 } else {
1331 ++j;
1332 }
1333 }
1334 return k;
1335}
1336
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001337static int intel_dp_common_rates(struct intel_dp *intel_dp,
1338 int *common_rates)
Ville Syrjälä2ecae762015-03-12 17:10:33 +02001339{
Ville Syrjälä2ecae762015-03-12 17:10:33 +02001340 const int *source_rates, *sink_rates;
1341 int source_len, sink_len;
1342
1343 sink_len = intel_dp_sink_rates(intel_dp, &sink_rates);
Ander Conselvan de Oliveirae588fa12015-10-23 13:01:50 +03001344 source_len = intel_dp_source_rates(intel_dp, &source_rates);
Ville Syrjälä2ecae762015-03-12 17:10:33 +02001345
1346 return intersect_rates(source_rates, source_len,
1347 sink_rates, sink_len,
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001348 common_rates);
Ville Syrjälä2ecae762015-03-12 17:10:33 +02001349}
1350
Ville Syrjälä0336400e2015-03-12 17:10:39 +02001351static void snprintf_int_array(char *str, size_t len,
1352 const int *array, int nelem)
1353{
1354 int i;
1355
1356 str[0] = '\0';
1357
1358 for (i = 0; i < nelem; i++) {
Jani Nikulab2f505b2015-05-18 16:01:45 +03001359 int r = snprintf(str, len, "%s%d", i ? ", " : "", array[i]);
Ville Syrjälä0336400e2015-03-12 17:10:39 +02001360 if (r >= len)
1361 return;
1362 str += r;
1363 len -= r;
1364 }
1365}
1366
1367static void intel_dp_print_rates(struct intel_dp *intel_dp)
1368{
Ville Syrjälä0336400e2015-03-12 17:10:39 +02001369 const int *source_rates, *sink_rates;
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001370 int source_len, sink_len, common_len;
1371 int common_rates[DP_MAX_SUPPORTED_RATES];
Ville Syrjälä0336400e2015-03-12 17:10:39 +02001372 char str[128]; /* FIXME: too big for stack? */
1373
1374 if ((drm_debug & DRM_UT_KMS) == 0)
1375 return;
1376
Ander Conselvan de Oliveirae588fa12015-10-23 13:01:50 +03001377 source_len = intel_dp_source_rates(intel_dp, &source_rates);
Ville Syrjälä0336400e2015-03-12 17:10:39 +02001378 snprintf_int_array(str, sizeof(str), source_rates, source_len);
1379 DRM_DEBUG_KMS("source rates: %s\n", str);
1380
1381 sink_len = intel_dp_sink_rates(intel_dp, &sink_rates);
1382 snprintf_int_array(str, sizeof(str), sink_rates, sink_len);
1383 DRM_DEBUG_KMS("sink rates: %s\n", str);
1384
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001385 common_len = intel_dp_common_rates(intel_dp, common_rates);
1386 snprintf_int_array(str, sizeof(str), common_rates, common_len);
1387 DRM_DEBUG_KMS("common rates: %s\n", str);
Ville Syrjälä0336400e2015-03-12 17:10:39 +02001388}
1389
Ville Syrjäläf4896f12015-03-12 17:10:27 +02001390static int rate_to_index(int find, const int *rates)
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301391{
1392 int i = 0;
1393
1394 for (i = 0; i < DP_MAX_SUPPORTED_RATES; ++i)
1395 if (find == rates[i])
1396 break;
1397
1398 return i;
1399}
1400
Ville Syrjälä50fec212015-03-12 17:10:34 +02001401int
1402intel_dp_max_link_rate(struct intel_dp *intel_dp)
1403{
1404 int rates[DP_MAX_SUPPORTED_RATES] = {};
1405 int len;
1406
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001407 len = intel_dp_common_rates(intel_dp, rates);
Ville Syrjälä50fec212015-03-12 17:10:34 +02001408 if (WARN_ON(len <= 0))
1409 return 162000;
1410
1411 return rates[rate_to_index(0, rates) - 1];
1412}
1413
Ville Syrjäläed4e9c12015-03-12 17:10:36 +02001414int intel_dp_rate_select(struct intel_dp *intel_dp, int rate)
1415{
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001416 return rate_to_index(rate, intel_dp->sink_rates);
Ville Syrjäläed4e9c12015-03-12 17:10:36 +02001417}
1418
Ander Conselvan de Oliveira94223d02015-10-23 13:01:48 +03001419void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
1420 uint8_t *link_bw, uint8_t *rate_select)
Ville Syrjälä04a60f92015-07-06 15:10:06 +03001421{
1422 if (intel_dp->num_sink_rates) {
1423 *link_bw = 0;
1424 *rate_select =
1425 intel_dp_rate_select(intel_dp, port_clock);
1426 } else {
1427 *link_bw = drm_dp_link_rate_to_bw_code(port_clock);
1428 *rate_select = 0;
1429 }
1430}
1431
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001432bool
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001433intel_dp_compute_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001434 struct intel_crtc_state *pipe_config)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001435{
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001436 struct drm_device *dev = encoder->base.dev;
Daniel Vetter36008362013-03-27 00:44:59 +01001437 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02001438 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001439 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deakbc7d38a2013-05-16 14:40:36 +03001440 enum port port = dp_to_dig_port(intel_dp)->port;
Ander Conselvan de Oliveira84556d52015-03-20 16:18:10 +02001441 struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
Jani Nikuladd06f902012-10-19 14:51:50 +03001442 struct intel_connector *intel_connector = intel_dp->attached_connector;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001443 int lane_count, clock;
Jani Nikula56071a22014-05-06 14:56:52 +03001444 int min_lane_count = 1;
Paulo Zanonieeb63242014-05-06 14:56:50 +03001445 int max_lane_count = intel_dp_max_lane_count(intel_dp);
Todd Previte06ea66b2014-01-20 10:19:39 -07001446 /* Conveniently, the link BW constants become indices with a shift...*/
Jani Nikula56071a22014-05-06 14:56:52 +03001447 int min_clock = 0;
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301448 int max_clock;
Daniel Vetter083f9562012-04-20 20:23:49 +02001449 int bpp, mode_rate;
Daniel Vetterff9a6752013-06-01 17:16:21 +02001450 int link_avail, link_clock;
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001451 int common_rates[DP_MAX_SUPPORTED_RATES] = {};
1452 int common_len;
Ville Syrjälä04a60f92015-07-06 15:10:06 +03001453 uint8_t link_bw, rate_select;
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301454
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001455 common_len = intel_dp_common_rates(intel_dp, common_rates);
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301456
1457 /* No common link rates between source and sink */
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001458 WARN_ON(common_len <= 0);
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301459
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001460 max_clock = common_len - 1;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001461
Imre Deakbc7d38a2013-05-16 14:40:36 +03001462 if (HAS_PCH_SPLIT(dev) && !HAS_DDI(dev) && port != PORT_A)
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001463 pipe_config->has_pch_encoder = true;
1464
Daniel Vetter03afc4a2013-04-02 23:42:31 +02001465 pipe_config->has_dp_encoder = true;
Vandana Kannanf769cd22014-08-05 07:51:22 -07001466 pipe_config->has_drrs = false;
Jani Nikula9fcb1702015-05-05 16:32:12 +03001467 pipe_config->has_audio = intel_dp->has_audio && port != PORT_A;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001468
Jani Nikuladd06f902012-10-19 14:51:50 +03001469 if (is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
1470 intel_fixed_panel_mode(intel_connector->panel.fixed_mode,
1471 adjusted_mode);
Chandra Kondurua1b22782015-04-07 15:28:45 -07001472
1473 if (INTEL_INFO(dev)->gen >= 9) {
1474 int ret;
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02001475 ret = skl_update_scaler_crtc(pipe_config);
Chandra Kondurua1b22782015-04-07 15:28:45 -07001476 if (ret)
1477 return ret;
1478 }
1479
Matt Roperb56676272015-11-04 09:05:27 -08001480 if (HAS_GMCH_DISPLAY(dev))
Jesse Barnes2dd24552013-04-25 12:55:01 -07001481 intel_gmch_panel_fitting(intel_crtc, pipe_config,
1482 intel_connector->panel.fitting_mode);
1483 else
Jesse Barnesb074cec2013-04-25 12:55:02 -07001484 intel_pch_panel_fitting(intel_crtc, pipe_config,
1485 intel_connector->panel.fitting_mode);
Zhao Yakui0d3a1be2010-07-19 09:43:13 +01001486 }
1487
Daniel Vettercb1793c2012-06-04 18:39:21 +02001488 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
Daniel Vetter0af78a22012-05-23 11:30:55 +02001489 return false;
1490
Daniel Vetter083f9562012-04-20 20:23:49 +02001491 DRM_DEBUG_KMS("DP link computation with max lane count %i "
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301492 "max bw %d pixel clock %iKHz\n",
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001493 max_lane_count, common_rates[max_clock],
Damien Lespiau241bfc32013-09-25 16:45:37 +01001494 adjusted_mode->crtc_clock);
Daniel Vetter083f9562012-04-20 20:23:49 +02001495
Daniel Vetter36008362013-03-27 00:44:59 +01001496 /* Walk through all bpp values. Luckily they're all nicely spaced with 2
1497 * bpc in between. */
Daniel Vetter3e7ca982013-06-01 19:45:56 +02001498 bpp = pipe_config->pipe_bpp;
Jani Nikula56071a22014-05-06 14:56:52 +03001499 if (is_edp(intel_dp)) {
Thulasimani,Sivakumar22ce5622015-07-31 11:05:27 +05301500
1501 /* Get bpp from vbt only for panels that dont have bpp in edid */
1502 if (intel_connector->base.display_info.bpc == 0 &&
1503 (dev_priv->vbt.edp_bpp && dev_priv->vbt.edp_bpp < bpp)) {
Jani Nikula56071a22014-05-06 14:56:52 +03001504 DRM_DEBUG_KMS("clamping bpp for eDP panel to BIOS-provided %i\n",
1505 dev_priv->vbt.edp_bpp);
1506 bpp = dev_priv->vbt.edp_bpp;
1507 }
1508
Jani Nikula344c5bb2014-09-09 11:25:13 +03001509 /*
1510 * Use the maximum clock and number of lanes the eDP panel
1511 * advertizes being capable of. The panels are generally
1512 * designed to support only a single clock and lane
1513 * configuration, and typically these values correspond to the
1514 * native resolution of the panel.
1515 */
1516 min_lane_count = max_lane_count;
1517 min_clock = max_clock;
Imre Deak79842112013-07-18 17:44:13 +03001518 }
Daniel Vetter657445f2013-05-04 10:09:18 +02001519
Daniel Vetter36008362013-03-27 00:44:59 +01001520 for (; bpp >= 6*3; bpp -= 2*3) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001521 mode_rate = intel_dp_link_required(adjusted_mode->crtc_clock,
1522 bpp);
Daniel Vetterc4867932012-04-10 10:42:36 +02001523
Dave Airliec6930992014-07-14 11:04:39 +10001524 for (clock = min_clock; clock <= max_clock; clock++) {
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301525 for (lane_count = min_lane_count;
1526 lane_count <= max_lane_count;
1527 lane_count <<= 1) {
1528
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001529 link_clock = common_rates[clock];
Daniel Vetter36008362013-03-27 00:44:59 +01001530 link_avail = intel_dp_max_data_rate(link_clock,
1531 lane_count);
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02001532
Daniel Vetter36008362013-03-27 00:44:59 +01001533 if (mode_rate <= link_avail) {
1534 goto found;
1535 }
1536 }
1537 }
1538 }
1539
1540 return false;
1541
1542found:
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02001543 if (intel_dp->color_range_auto) {
1544 /*
1545 * See:
1546 * CEA-861-E - 5.1 Default Encoding Parameters
1547 * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
1548 */
Ville Syrjälä0f2a2a72015-07-06 15:10:00 +03001549 pipe_config->limited_color_range =
1550 bpp != 18 && drm_match_cea_mode(adjusted_mode) > 1;
1551 } else {
1552 pipe_config->limited_color_range =
1553 intel_dp->limited_color_range;
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02001554 }
1555
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +03001556 pipe_config->lane_count = lane_count;
Sonika Jindala8f3ef62015-03-05 10:02:30 +05301557
Daniel Vetter657445f2013-05-04 10:09:18 +02001558 pipe_config->pipe_bpp = bpp;
Ville Syrjälä94ca7192015-03-13 19:40:31 +02001559 pipe_config->port_clock = common_rates[clock];
Daniel Vetterc4867932012-04-10 10:42:36 +02001560
Ville Syrjälä04a60f92015-07-06 15:10:06 +03001561 intel_dp_compute_rate(intel_dp, pipe_config->port_clock,
1562 &link_bw, &rate_select);
1563
1564 DRM_DEBUG_KMS("DP link bw %02x rate select %02x lane count %d clock %d bpp %d\n",
1565 link_bw, rate_select, pipe_config->lane_count,
Daniel Vetterff9a6752013-06-01 17:16:21 +02001566 pipe_config->port_clock, bpp);
Daniel Vetter36008362013-03-27 00:44:59 +01001567 DRM_DEBUG_KMS("DP link bw required %i available %i\n",
1568 mode_rate, link_avail);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001569
Daniel Vetter03afc4a2013-04-02 23:42:31 +02001570 intel_link_compute_m_n(bpp, lane_count,
Damien Lespiau241bfc32013-09-25 16:45:37 +01001571 adjusted_mode->crtc_clock,
1572 pipe_config->port_clock,
Daniel Vetter03afc4a2013-04-02 23:42:31 +02001573 &pipe_config->dp_m_n);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001574
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05301575 if (intel_connector->panel.downclock_mode != NULL &&
Vandana Kannan96178ee2015-01-10 02:25:56 +05301576 dev_priv->drrs.type == SEAMLESS_DRRS_SUPPORT) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07001577 pipe_config->has_drrs = true;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05301578 intel_link_compute_m_n(bpp, lane_count,
1579 intel_connector->panel.downclock_mode->clock,
1580 pipe_config->port_clock,
1581 &pipe_config->dp_m2_n2);
1582 }
1583
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02001584 if (!HAS_DDI(dev))
Ville Syrjälä840b32b2015-08-11 20:21:46 +03001585 intel_dp_set_clock(encoder, pipe_config);
Daniel Vetterc6bb3532013-04-19 11:14:33 +02001586
Daniel Vetter36008362013-03-27 00:44:59 +01001587 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001588}
1589
Ville Syrjälä901c2da2015-08-17 18:05:12 +03001590void intel_dp_set_link_params(struct intel_dp *intel_dp,
1591 const struct intel_crtc_state *pipe_config)
1592{
1593 intel_dp->link_rate = pipe_config->port_clock;
1594 intel_dp->lane_count = pipe_config->lane_count;
1595}
1596
Daniel Vetter8ac33ed2014-04-24 23:54:54 +02001597static void intel_dp_prepare(struct intel_encoder *encoder)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001598{
Daniel Vetterb934223d2013-07-21 21:37:05 +02001599 struct drm_device *dev = encoder->base.dev;
Keith Packard417e8222011-11-01 19:54:11 -07001600 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterb934223d2013-07-21 21:37:05 +02001601 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deakbc7d38a2013-05-16 14:40:36 +03001602 enum port port = dp_to_dig_port(intel_dp)->port;
Daniel Vetterb934223d2013-07-21 21:37:05 +02001603 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03001604 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001605
Ville Syrjälä901c2da2015-08-17 18:05:12 +03001606 intel_dp_set_link_params(intel_dp, crtc->config);
1607
Keith Packard417e8222011-11-01 19:54:11 -07001608 /*
Keith Packard1a2eb462011-11-16 16:26:07 -08001609 * There are four kinds of DP registers:
Keith Packard417e8222011-11-01 19:54:11 -07001610 *
1611 * IBX PCH
Keith Packard1a2eb462011-11-16 16:26:07 -08001612 * SNB CPU
1613 * IVB CPU
Keith Packard417e8222011-11-01 19:54:11 -07001614 * CPT PCH
1615 *
1616 * IBX PCH and CPU are the same for almost everything,
1617 * except that the CPU DP PLL is configured in this
1618 * register
1619 *
1620 * CPT PCH is quite different, having many bits moved
1621 * to the TRANS_DP_CTL register instead. That
1622 * configuration happens (oddly) in ironlake_pch_enable
1623 */
Adam Jackson9c9e7922010-04-05 17:57:59 -04001624
Keith Packard417e8222011-11-01 19:54:11 -07001625 /* Preserve the BIOS-computed detected bit. This is
1626 * supposed to be read-only.
1627 */
1628 intel_dp->DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001629
Keith Packard417e8222011-11-01 19:54:11 -07001630 /* Handle DP bits in common between all three register formats */
Keith Packard417e8222011-11-01 19:54:11 -07001631 intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +03001632 intel_dp->DP |= DP_PORT_WIDTH(crtc->config->lane_count);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001633
Keith Packard417e8222011-11-01 19:54:11 -07001634 /* Split out the IBX/CPU vs CPT settings */
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001635
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03001636 if (IS_GEN7(dev) && port == PORT_A) {
Keith Packard1a2eb462011-11-16 16:26:07 -08001637 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
1638 intel_dp->DP |= DP_SYNC_HS_HIGH;
1639 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
1640 intel_dp->DP |= DP_SYNC_VS_HIGH;
1641 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
1642
Jani Nikula6aba5b62013-10-04 15:08:10 +03001643 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
Keith Packard1a2eb462011-11-16 16:26:07 -08001644 intel_dp->DP |= DP_ENHANCED_FRAMING;
1645
Daniel Vetter7c62a162013-06-01 17:16:20 +02001646 intel_dp->DP |= crtc->pipe << 29;
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03001647 } else if (HAS_PCH_CPT(dev) && port != PORT_A) {
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03001648 u32 trans_dp;
1649
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03001650 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03001651
1652 trans_dp = I915_READ(TRANS_DP_CTL(crtc->pipe));
1653 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
1654 trans_dp |= TRANS_DP_ENH_FRAMING;
1655 else
1656 trans_dp &= ~TRANS_DP_ENH_FRAMING;
1657 I915_WRITE(TRANS_DP_CTL(crtc->pipe), trans_dp);
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03001658 } else {
Ville Syrjälä0f2a2a72015-07-06 15:10:00 +03001659 if (!HAS_PCH_SPLIT(dev) && !IS_VALLEYVIEW(dev) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08001660 !IS_CHERRYVIEW(dev) && crtc->config->limited_color_range)
Ville Syrjälä0f2a2a72015-07-06 15:10:00 +03001661 intel_dp->DP |= DP_COLOR_RANGE_16_235;
Keith Packard417e8222011-11-01 19:54:11 -07001662
1663 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
1664 intel_dp->DP |= DP_SYNC_HS_HIGH;
1665 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
1666 intel_dp->DP |= DP_SYNC_VS_HIGH;
1667 intel_dp->DP |= DP_LINK_TRAIN_OFF;
1668
Jani Nikula6aba5b62013-10-04 15:08:10 +03001669 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
Keith Packard417e8222011-11-01 19:54:11 -07001670 intel_dp->DP |= DP_ENHANCED_FRAMING;
1671
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03001672 if (IS_CHERRYVIEW(dev))
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001673 intel_dp->DP |= DP_PIPE_SELECT_CHV(crtc->pipe);
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03001674 else if (crtc->pipe == PIPE_B)
1675 intel_dp->DP |= DP_PIPEB_SELECT;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08001676 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001677}
1678
Paulo Zanoniffd6749d2013-12-19 14:29:42 -02001679#define IDLE_ON_MASK (PP_ON | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
1680#define IDLE_ON_VALUE (PP_ON | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_ON_IDLE)
Keith Packard99ea7122011-11-01 19:57:50 -07001681
Paulo Zanoni1a5ef5b2013-12-19 14:29:43 -02001682#define IDLE_OFF_MASK (PP_ON | PP_SEQUENCE_MASK | 0 | 0)
1683#define IDLE_OFF_VALUE (0 | PP_SEQUENCE_NONE | 0 | 0)
Keith Packard99ea7122011-11-01 19:57:50 -07001684
Paulo Zanoniffd6749d2013-12-19 14:29:42 -02001685#define IDLE_CYCLE_MASK (PP_ON | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
1686#define IDLE_CYCLE_VALUE (0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
Keith Packard99ea7122011-11-01 19:57:50 -07001687
Daniel Vetter4be73782014-01-17 14:39:48 +01001688static void wait_panel_status(struct intel_dp *intel_dp,
Keith Packard99ea7122011-11-01 19:57:50 -07001689 u32 mask,
1690 u32 value)
1691{
Paulo Zanoni30add222012-10-26 19:05:45 -02001692 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Keith Packard99ea7122011-11-01 19:57:50 -07001693 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001694 i915_reg_t pp_stat_reg, pp_ctrl_reg;
Jesse Barnes453c5422013-03-28 09:55:41 -07001695
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001696 lockdep_assert_held(&dev_priv->pps_mutex);
1697
Jani Nikulabf13e812013-09-06 07:40:05 +03001698 pp_stat_reg = _pp_stat_reg(intel_dp);
1699 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Keith Packard99ea7122011-11-01 19:57:50 -07001700
1701 DRM_DEBUG_KMS("mask %08x value %08x status %08x control %08x\n",
Jesse Barnes453c5422013-03-28 09:55:41 -07001702 mask, value,
1703 I915_READ(pp_stat_reg),
1704 I915_READ(pp_ctrl_reg));
Keith Packard99ea7122011-11-01 19:57:50 -07001705
Tvrtko Ursulin3f177622016-03-03 14:36:41 +00001706 if (_wait_for((I915_READ(pp_stat_reg) & mask) == value,
1707 5 * USEC_PER_SEC, 10 * USEC_PER_MSEC))
Keith Packard99ea7122011-11-01 19:57:50 -07001708 DRM_ERROR("Panel status timeout: status %08x control %08x\n",
Jesse Barnes453c5422013-03-28 09:55:41 -07001709 I915_READ(pp_stat_reg),
1710 I915_READ(pp_ctrl_reg));
Chris Wilson54c136d2013-12-02 09:57:16 +00001711
1712 DRM_DEBUG_KMS("Wait complete\n");
Keith Packard99ea7122011-11-01 19:57:50 -07001713}
1714
Daniel Vetter4be73782014-01-17 14:39:48 +01001715static void wait_panel_on(struct intel_dp *intel_dp)
Keith Packard99ea7122011-11-01 19:57:50 -07001716{
1717 DRM_DEBUG_KMS("Wait for panel power on\n");
Daniel Vetter4be73782014-01-17 14:39:48 +01001718 wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
Keith Packard99ea7122011-11-01 19:57:50 -07001719}
1720
Daniel Vetter4be73782014-01-17 14:39:48 +01001721static void wait_panel_off(struct intel_dp *intel_dp)
Keith Packardbd943152011-09-18 23:09:52 -07001722{
Keith Packardbd943152011-09-18 23:09:52 -07001723 DRM_DEBUG_KMS("Wait for panel power off time\n");
Daniel Vetter4be73782014-01-17 14:39:48 +01001724 wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
Keith Packardbd943152011-09-18 23:09:52 -07001725}
Keith Packardbd943152011-09-18 23:09:52 -07001726
Daniel Vetter4be73782014-01-17 14:39:48 +01001727static void wait_panel_power_cycle(struct intel_dp *intel_dp)
Keith Packard99ea7122011-11-01 19:57:50 -07001728{
Abhay Kumard28d4732016-01-22 17:39:04 -08001729 ktime_t panel_power_on_time;
1730 s64 panel_power_off_duration;
1731
Keith Packard99ea7122011-11-01 19:57:50 -07001732 DRM_DEBUG_KMS("Wait for panel power cycle\n");
Paulo Zanonidce56b32013-12-19 14:29:40 -02001733
Abhay Kumard28d4732016-01-22 17:39:04 -08001734 /* take the difference of currrent time and panel power off time
1735 * and then make panel wait for t11_t12 if needed. */
1736 panel_power_on_time = ktime_get_boottime();
1737 panel_power_off_duration = ktime_ms_delta(panel_power_on_time, intel_dp->panel_power_off_time);
1738
Paulo Zanonidce56b32013-12-19 14:29:40 -02001739 /* When we disable the VDD override bit last we have to do the manual
1740 * wait. */
Abhay Kumard28d4732016-01-22 17:39:04 -08001741 if (panel_power_off_duration < (s64)intel_dp->panel_power_cycle_delay)
1742 wait_remaining_ms_from_jiffies(jiffies,
1743 intel_dp->panel_power_cycle_delay - panel_power_off_duration);
Paulo Zanonidce56b32013-12-19 14:29:40 -02001744
Daniel Vetter4be73782014-01-17 14:39:48 +01001745 wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
Keith Packard99ea7122011-11-01 19:57:50 -07001746}
Keith Packardbd943152011-09-18 23:09:52 -07001747
Daniel Vetter4be73782014-01-17 14:39:48 +01001748static void wait_backlight_on(struct intel_dp *intel_dp)
Paulo Zanonidce56b32013-12-19 14:29:40 -02001749{
1750 wait_remaining_ms_from_jiffies(intel_dp->last_power_on,
1751 intel_dp->backlight_on_delay);
1752}
1753
Daniel Vetter4be73782014-01-17 14:39:48 +01001754static void edp_wait_backlight_off(struct intel_dp *intel_dp)
Paulo Zanonidce56b32013-12-19 14:29:40 -02001755{
1756 wait_remaining_ms_from_jiffies(intel_dp->last_backlight_off,
1757 intel_dp->backlight_off_delay);
1758}
Keith Packard99ea7122011-11-01 19:57:50 -07001759
Keith Packard832dd3c2011-11-01 19:34:06 -07001760/* Read the current pp_control value, unlocking the register if it
1761 * is locked
1762 */
1763
Jesse Barnes453c5422013-03-28 09:55:41 -07001764static u32 ironlake_get_pp_control(struct intel_dp *intel_dp)
Keith Packard832dd3c2011-11-01 19:34:06 -07001765{
Jesse Barnes453c5422013-03-28 09:55:41 -07001766 struct drm_device *dev = intel_dp_to_dev(intel_dp);
1767 struct drm_i915_private *dev_priv = dev->dev_private;
1768 u32 control;
Jesse Barnes453c5422013-03-28 09:55:41 -07001769
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001770 lockdep_assert_held(&dev_priv->pps_mutex);
1771
Jani Nikulabf13e812013-09-06 07:40:05 +03001772 control = I915_READ(_pp_ctrl_reg(intel_dp));
Vandana Kannanb0a08be2015-06-18 11:00:55 +05301773 if (!IS_BROXTON(dev)) {
1774 control &= ~PANEL_UNLOCK_MASK;
1775 control |= PANEL_UNLOCK_REGS;
1776 }
Keith Packard832dd3c2011-11-01 19:34:06 -07001777 return control;
Keith Packardbd943152011-09-18 23:09:52 -07001778}
1779
Ville Syrjälä951468f2014-09-04 14:55:31 +03001780/*
1781 * Must be paired with edp_panel_vdd_off().
1782 * Must hold pps_mutex around the whole on/off sequence.
1783 * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
1784 */
Ville Syrjälä1e0560e2014-08-19 13:24:25 +03001785static bool edp_panel_vdd_on(struct intel_dp *intel_dp)
Jesse Barnes5d613502011-01-24 17:10:54 -08001786{
Paulo Zanoni30add222012-10-26 19:05:45 -02001787 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Imre Deak4e6e1a52014-03-27 17:45:11 +02001788 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1789 struct intel_encoder *intel_encoder = &intel_dig_port->base;
Jesse Barnes5d613502011-01-24 17:10:54 -08001790 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak4e6e1a52014-03-27 17:45:11 +02001791 enum intel_display_power_domain power_domain;
Jesse Barnes5d613502011-01-24 17:10:54 -08001792 u32 pp;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001793 i915_reg_t pp_stat_reg, pp_ctrl_reg;
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001794 bool need_to_disable = !intel_dp->want_panel_vdd;
Jesse Barnes5d613502011-01-24 17:10:54 -08001795
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001796 lockdep_assert_held(&dev_priv->pps_mutex);
1797
Keith Packard97af61f572011-09-28 16:23:51 -07001798 if (!is_edp(intel_dp))
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001799 return false;
Keith Packardbd943152011-09-18 23:09:52 -07001800
Egbert Eich2c623c12014-11-25 12:54:57 +01001801 cancel_delayed_work(&intel_dp->panel_vdd_work);
Keith Packardbd943152011-09-18 23:09:52 -07001802 intel_dp->want_panel_vdd = true;
Keith Packard99ea7122011-11-01 19:57:50 -07001803
Daniel Vetter4be73782014-01-17 14:39:48 +01001804 if (edp_have_panel_vdd(intel_dp))
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001805 return need_to_disable;
Paulo Zanonib0665d52013-10-30 19:50:27 -02001806
Ville Syrjälä25f78f52015-11-16 15:01:04 +01001807 power_domain = intel_display_port_aux_power_domain(intel_encoder);
Imre Deak4e6e1a52014-03-27 17:45:11 +02001808 intel_display_power_get(dev_priv, power_domain);
Paulo Zanonie9cb81a2013-11-21 13:47:23 -02001809
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001810 DRM_DEBUG_KMS("Turning eDP port %c VDD on\n",
1811 port_name(intel_dig_port->port));
Keith Packardbd943152011-09-18 23:09:52 -07001812
Daniel Vetter4be73782014-01-17 14:39:48 +01001813 if (!edp_have_panel_power(intel_dp))
1814 wait_panel_power_cycle(intel_dp);
Keith Packard99ea7122011-11-01 19:57:50 -07001815
Jesse Barnes453c5422013-03-28 09:55:41 -07001816 pp = ironlake_get_pp_control(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001817 pp |= EDP_FORCE_VDD;
Keith Packardebf33b12011-09-29 15:53:27 -07001818
Jani Nikulabf13e812013-09-06 07:40:05 +03001819 pp_stat_reg = _pp_stat_reg(intel_dp);
1820 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001821
1822 I915_WRITE(pp_ctrl_reg, pp);
1823 POSTING_READ(pp_ctrl_reg);
1824 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
1825 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
Keith Packardebf33b12011-09-29 15:53:27 -07001826 /*
1827 * If the panel wasn't on, delay before accessing aux channel
1828 */
Daniel Vetter4be73782014-01-17 14:39:48 +01001829 if (!edp_have_panel_power(intel_dp)) {
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001830 DRM_DEBUG_KMS("eDP port %c panel power wasn't enabled\n",
1831 port_name(intel_dig_port->port));
Keith Packardf01eca22011-09-28 16:48:10 -07001832 msleep(intel_dp->panel_power_up_delay);
Keith Packardf01eca22011-09-28 16:48:10 -07001833 }
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001834
1835 return need_to_disable;
1836}
1837
Ville Syrjälä951468f2014-09-04 14:55:31 +03001838/*
1839 * Must be paired with intel_edp_panel_vdd_off() or
1840 * intel_edp_panel_off().
1841 * Nested calls to these functions are not allowed since
1842 * we drop the lock. Caller must use some higher level
1843 * locking to prevent nested calls from other threads.
1844 */
Daniel Vetterb80d6c72014-03-19 15:54:37 +01001845void intel_edp_panel_vdd_on(struct intel_dp *intel_dp)
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001846{
Ville Syrjäläc695b6b2014-08-18 22:16:03 +03001847 bool vdd;
Jani Nikulaadddaaf2014-03-14 16:51:13 +02001848
Ville Syrjäläc695b6b2014-08-18 22:16:03 +03001849 if (!is_edp(intel_dp))
1850 return;
1851
Ville Syrjälä773538e82014-09-04 14:54:56 +03001852 pps_lock(intel_dp);
Ville Syrjäläc695b6b2014-08-18 22:16:03 +03001853 vdd = edp_panel_vdd_on(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03001854 pps_unlock(intel_dp);
Ville Syrjäläc695b6b2014-08-18 22:16:03 +03001855
Rob Clarke2c719b2014-12-15 13:56:32 -05001856 I915_STATE_WARN(!vdd, "eDP port %c VDD already requested on\n",
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001857 port_name(dp_to_dig_port(intel_dp)->port));
Jesse Barnes5d613502011-01-24 17:10:54 -08001858}
1859
Daniel Vetter4be73782014-01-17 14:39:48 +01001860static void edp_panel_vdd_off_sync(struct intel_dp *intel_dp)
Jesse Barnes5d613502011-01-24 17:10:54 -08001861{
Paulo Zanoni30add222012-10-26 19:05:45 -02001862 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001863 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001864 struct intel_digital_port *intel_dig_port =
1865 dp_to_dig_port(intel_dp);
1866 struct intel_encoder *intel_encoder = &intel_dig_port->base;
1867 enum intel_display_power_domain power_domain;
Jesse Barnes5d613502011-01-24 17:10:54 -08001868 u32 pp;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001869 i915_reg_t pp_stat_reg, pp_ctrl_reg;
Jesse Barnes5d613502011-01-24 17:10:54 -08001870
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001871 lockdep_assert_held(&dev_priv->pps_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001872
Ville Syrjälä15e899a2014-08-18 22:16:02 +03001873 WARN_ON(intel_dp->want_panel_vdd);
Imre Deak4e6e1a52014-03-27 17:45:11 +02001874
Ville Syrjälä15e899a2014-08-18 22:16:02 +03001875 if (!edp_have_panel_vdd(intel_dp))
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001876 return;
Paulo Zanonib0665d52013-10-30 19:50:27 -02001877
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001878 DRM_DEBUG_KMS("Turning eDP port %c VDD off\n",
1879 port_name(intel_dig_port->port));
Jesse Barnes453c5422013-03-28 09:55:41 -07001880
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001881 pp = ironlake_get_pp_control(intel_dp);
1882 pp &= ~EDP_FORCE_VDD;
Jesse Barnes453c5422013-03-28 09:55:41 -07001883
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001884 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
1885 pp_stat_reg = _pp_stat_reg(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001886
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001887 I915_WRITE(pp_ctrl_reg, pp);
1888 POSTING_READ(pp_ctrl_reg);
Paulo Zanoni90791a52013-12-06 17:32:42 -02001889
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001890 /* Make sure sequencer is idle before allowing subsequent activity */
1891 DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
1892 I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
Paulo Zanonie9cb81a2013-11-21 13:47:23 -02001893
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001894 if ((pp & POWER_TARGET_ON) == 0)
Abhay Kumard28d4732016-01-22 17:39:04 -08001895 intel_dp->panel_power_off_time = ktime_get_boottime();
Paulo Zanonie9cb81a2013-11-21 13:47:23 -02001896
Ville Syrjälä25f78f52015-11-16 15:01:04 +01001897 power_domain = intel_display_port_aux_power_domain(intel_encoder);
Ville Syrjäläbe2c9192014-08-18 22:16:01 +03001898 intel_display_power_put(dev_priv, power_domain);
Keith Packardbd943152011-09-18 23:09:52 -07001899}
1900
Daniel Vetter4be73782014-01-17 14:39:48 +01001901static void edp_panel_vdd_work(struct work_struct *__work)
Keith Packardbd943152011-09-18 23:09:52 -07001902{
1903 struct intel_dp *intel_dp = container_of(to_delayed_work(__work),
1904 struct intel_dp, panel_vdd_work);
Keith Packardbd943152011-09-18 23:09:52 -07001905
Ville Syrjälä773538e82014-09-04 14:54:56 +03001906 pps_lock(intel_dp);
Ville Syrjälä15e899a2014-08-18 22:16:02 +03001907 if (!intel_dp->want_panel_vdd)
1908 edp_panel_vdd_off_sync(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03001909 pps_unlock(intel_dp);
Keith Packardbd943152011-09-18 23:09:52 -07001910}
1911
Imre Deakaba86892014-07-30 15:57:31 +03001912static void edp_panel_vdd_schedule_off(struct intel_dp *intel_dp)
1913{
1914 unsigned long delay;
1915
1916 /*
1917 * Queue the timer to fire a long time from now (relative to the power
1918 * down delay) to keep the panel power up across a sequence of
1919 * operations.
1920 */
1921 delay = msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5);
1922 schedule_delayed_work(&intel_dp->panel_vdd_work, delay);
1923}
1924
Ville Syrjälä951468f2014-09-04 14:55:31 +03001925/*
1926 * Must be paired with edp_panel_vdd_on().
1927 * Must hold pps_mutex around the whole on/off sequence.
1928 * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
1929 */
Daniel Vetter4be73782014-01-17 14:39:48 +01001930static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
Keith Packardbd943152011-09-18 23:09:52 -07001931{
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001932 struct drm_i915_private *dev_priv =
1933 intel_dp_to_dev(intel_dp)->dev_private;
1934
1935 lockdep_assert_held(&dev_priv->pps_mutex);
1936
Keith Packard97af61f572011-09-28 16:23:51 -07001937 if (!is_edp(intel_dp))
1938 return;
Jesse Barnes5d613502011-01-24 17:10:54 -08001939
Rob Clarke2c719b2014-12-15 13:56:32 -05001940 I915_STATE_WARN(!intel_dp->want_panel_vdd, "eDP port %c VDD not forced on",
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001941 port_name(dp_to_dig_port(intel_dp)->port));
Keith Packardf2e8b182011-11-01 20:01:35 -07001942
Keith Packardbd943152011-09-18 23:09:52 -07001943 intel_dp->want_panel_vdd = false;
1944
Imre Deakaba86892014-07-30 15:57:31 +03001945 if (sync)
Daniel Vetter4be73782014-01-17 14:39:48 +01001946 edp_panel_vdd_off_sync(intel_dp);
Imre Deakaba86892014-07-30 15:57:31 +03001947 else
1948 edp_panel_vdd_schedule_off(intel_dp);
Jesse Barnes5d613502011-01-24 17:10:54 -08001949}
1950
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001951static void edp_panel_on(struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -07001952{
Paulo Zanoni30add222012-10-26 19:05:45 -02001953 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07001954 struct drm_i915_private *dev_priv = dev->dev_private;
Keith Packard99ea7122011-11-01 19:57:50 -07001955 u32 pp;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001956 i915_reg_t pp_ctrl_reg;
Jesse Barnes9934c132010-07-22 13:18:19 -07001957
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001958 lockdep_assert_held(&dev_priv->pps_mutex);
1959
Keith Packard97af61f572011-09-28 16:23:51 -07001960 if (!is_edp(intel_dp))
Keith Packardbd943152011-09-18 23:09:52 -07001961 return;
Keith Packard99ea7122011-11-01 19:57:50 -07001962
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03001963 DRM_DEBUG_KMS("Turn eDP port %c panel power on\n",
1964 port_name(dp_to_dig_port(intel_dp)->port));
Keith Packard99ea7122011-11-01 19:57:50 -07001965
Ville Syrjäläe7a89ac2014-10-16 21:30:07 +03001966 if (WARN(edp_have_panel_power(intel_dp),
1967 "eDP port %c panel power already on\n",
1968 port_name(dp_to_dig_port(intel_dp)->port)))
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001969 return;
Jesse Barnes9934c132010-07-22 13:18:19 -07001970
Daniel Vetter4be73782014-01-17 14:39:48 +01001971 wait_panel_power_cycle(intel_dp);
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001972
Jani Nikulabf13e812013-09-06 07:40:05 +03001973 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07001974 pp = ironlake_get_pp_control(intel_dp);
Keith Packard05ce1a42011-09-29 16:33:01 -07001975 if (IS_GEN5(dev)) {
1976 /* ILK workaround: disable reset around power sequence */
1977 pp &= ~PANEL_POWER_RESET;
Jani Nikulabf13e812013-09-06 07:40:05 +03001978 I915_WRITE(pp_ctrl_reg, pp);
1979 POSTING_READ(pp_ctrl_reg);
Keith Packard05ce1a42011-09-29 16:33:01 -07001980 }
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07001981
Keith Packard1c0ae802011-09-19 13:59:29 -07001982 pp |= POWER_TARGET_ON;
Keith Packard99ea7122011-11-01 19:57:50 -07001983 if (!IS_GEN5(dev))
1984 pp |= PANEL_POWER_RESET;
1985
Jesse Barnes453c5422013-03-28 09:55:41 -07001986 I915_WRITE(pp_ctrl_reg, pp);
1987 POSTING_READ(pp_ctrl_reg);
Jesse Barnes9934c132010-07-22 13:18:19 -07001988
Daniel Vetter4be73782014-01-17 14:39:48 +01001989 wait_panel_on(intel_dp);
Paulo Zanonidce56b32013-12-19 14:29:40 -02001990 intel_dp->last_power_on = jiffies;
Jesse Barnes9934c132010-07-22 13:18:19 -07001991
Keith Packard05ce1a42011-09-29 16:33:01 -07001992 if (IS_GEN5(dev)) {
1993 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
Jani Nikulabf13e812013-09-06 07:40:05 +03001994 I915_WRITE(pp_ctrl_reg, pp);
1995 POSTING_READ(pp_ctrl_reg);
Keith Packard05ce1a42011-09-29 16:33:01 -07001996 }
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001997}
Ville Syrjäläe39b9992014-09-04 14:53:14 +03001998
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03001999void intel_edp_panel_on(struct intel_dp *intel_dp)
2000{
2001 if (!is_edp(intel_dp))
2002 return;
2003
2004 pps_lock(intel_dp);
2005 edp_panel_on(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03002006 pps_unlock(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07002007}
2008
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03002009
2010static void edp_panel_off(struct intel_dp *intel_dp)
Jesse Barnes9934c132010-07-22 13:18:19 -07002011{
Imre Deak4e6e1a52014-03-27 17:45:11 +02002012 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2013 struct intel_encoder *intel_encoder = &intel_dig_port->base;
Paulo Zanoni30add222012-10-26 19:05:45 -02002014 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07002015 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak4e6e1a52014-03-27 17:45:11 +02002016 enum intel_display_power_domain power_domain;
Keith Packard99ea7122011-11-01 19:57:50 -07002017 u32 pp;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002018 i915_reg_t pp_ctrl_reg;
Jesse Barnes9934c132010-07-22 13:18:19 -07002019
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03002020 lockdep_assert_held(&dev_priv->pps_mutex);
2021
Keith Packard97af61f572011-09-28 16:23:51 -07002022 if (!is_edp(intel_dp))
2023 return;
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07002024
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03002025 DRM_DEBUG_KMS("Turn eDP port %c panel power off\n",
2026 port_name(dp_to_dig_port(intel_dp)->port));
Jesse Barnes37c6c9b2010-08-11 10:04:43 -07002027
Ville Syrjälä3936fcf2014-10-16 21:30:02 +03002028 WARN(!intel_dp->want_panel_vdd, "Need eDP port %c VDD to turn off panel\n",
2029 port_name(dp_to_dig_port(intel_dp)->port));
Jani Nikula24f3e092014-03-17 16:43:36 +02002030
Jesse Barnes453c5422013-03-28 09:55:41 -07002031 pp = ironlake_get_pp_control(intel_dp);
Daniel Vetter35a38552012-08-12 22:17:14 +02002032 /* We need to switch off panel power _and_ force vdd, for otherwise some
2033 * panels get very unhappy and cease to work. */
Patrik Jakobssonb3064152014-03-04 00:42:44 +01002034 pp &= ~(POWER_TARGET_ON | PANEL_POWER_RESET | EDP_FORCE_VDD |
2035 EDP_BLC_ENABLE);
Jesse Barnes453c5422013-03-28 09:55:41 -07002036
Jani Nikulabf13e812013-09-06 07:40:05 +03002037 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07002038
Paulo Zanoni849e39f2014-03-07 20:05:20 -03002039 intel_dp->want_panel_vdd = false;
2040
Jesse Barnes453c5422013-03-28 09:55:41 -07002041 I915_WRITE(pp_ctrl_reg, pp);
2042 POSTING_READ(pp_ctrl_reg);
Jesse Barnes9934c132010-07-22 13:18:19 -07002043
Abhay Kumard28d4732016-01-22 17:39:04 -08002044 intel_dp->panel_power_off_time = ktime_get_boottime();
Daniel Vetter4be73782014-01-17 14:39:48 +01002045 wait_panel_off(intel_dp);
Paulo Zanoni849e39f2014-03-07 20:05:20 -03002046
2047 /* We got a reference when we enabled the VDD. */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01002048 power_domain = intel_display_port_aux_power_domain(intel_encoder);
Imre Deak4e6e1a52014-03-27 17:45:11 +02002049 intel_display_power_put(dev_priv, power_domain);
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03002050}
Ville Syrjäläe39b9992014-09-04 14:53:14 +03002051
Ville Syrjälä9f0fb5b2014-10-16 21:27:32 +03002052void intel_edp_panel_off(struct intel_dp *intel_dp)
2053{
2054 if (!is_edp(intel_dp))
2055 return;
2056
2057 pps_lock(intel_dp);
2058 edp_panel_off(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03002059 pps_unlock(intel_dp);
Jesse Barnes9934c132010-07-22 13:18:19 -07002060}
2061
Jani Nikula1250d102014-08-12 17:11:39 +03002062/* Enable backlight in the panel power control. */
2063static void _intel_edp_backlight_on(struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002064{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002065 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2066 struct drm_device *dev = intel_dig_port->base.base.dev;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002067 struct drm_i915_private *dev_priv = dev->dev_private;
2068 u32 pp;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002069 i915_reg_t pp_ctrl_reg;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002070
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07002071 /*
2072 * If we enable the backlight right away following a panel power
2073 * on, we may see slight flicker as the panel syncs with the eDP
2074 * link. So delay a bit to make sure the image is solid before
2075 * allowing it to appear.
2076 */
Daniel Vetter4be73782014-01-17 14:39:48 +01002077 wait_backlight_on(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03002078
Ville Syrjälä773538e82014-09-04 14:54:56 +03002079 pps_lock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03002080
Jesse Barnes453c5422013-03-28 09:55:41 -07002081 pp = ironlake_get_pp_control(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002082 pp |= EDP_BLC_ENABLE;
Jesse Barnes453c5422013-03-28 09:55:41 -07002083
Jani Nikulabf13e812013-09-06 07:40:05 +03002084 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07002085
2086 I915_WRITE(pp_ctrl_reg, pp);
2087 POSTING_READ(pp_ctrl_reg);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03002088
Ville Syrjälä773538e82014-09-04 14:54:56 +03002089 pps_unlock(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002090}
2091
Jani Nikula1250d102014-08-12 17:11:39 +03002092/* Enable backlight PWM and backlight PP control. */
2093void intel_edp_backlight_on(struct intel_dp *intel_dp)
2094{
2095 if (!is_edp(intel_dp))
2096 return;
2097
2098 DRM_DEBUG_KMS("\n");
2099
2100 intel_panel_enable_backlight(intel_dp->attached_connector);
2101 _intel_edp_backlight_on(intel_dp);
2102}
2103
2104/* Disable backlight in the panel power control. */
2105static void _intel_edp_backlight_off(struct intel_dp *intel_dp)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002106{
Paulo Zanoni30add222012-10-26 19:05:45 -02002107 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002108 struct drm_i915_private *dev_priv = dev->dev_private;
2109 u32 pp;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002110 i915_reg_t pp_ctrl_reg;
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002111
Keith Packardf01eca22011-09-28 16:48:10 -07002112 if (!is_edp(intel_dp))
2113 return;
2114
Ville Syrjälä773538e82014-09-04 14:54:56 +03002115 pps_lock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03002116
Jesse Barnes453c5422013-03-28 09:55:41 -07002117 pp = ironlake_get_pp_control(intel_dp);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002118 pp &= ~EDP_BLC_ENABLE;
Jesse Barnes453c5422013-03-28 09:55:41 -07002119
Jani Nikulabf13e812013-09-06 07:40:05 +03002120 pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
Jesse Barnes453c5422013-03-28 09:55:41 -07002121
2122 I915_WRITE(pp_ctrl_reg, pp);
2123 POSTING_READ(pp_ctrl_reg);
Jesse Barnesf7d23232014-03-31 11:13:56 -07002124
Ville Syrjälä773538e82014-09-04 14:54:56 +03002125 pps_unlock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03002126
Paulo Zanonidce56b32013-12-19 14:29:40 -02002127 intel_dp->last_backlight_off = jiffies;
Jesse Barnesf7d23232014-03-31 11:13:56 -07002128 edp_wait_backlight_off(intel_dp);
Jani Nikula1250d102014-08-12 17:11:39 +03002129}
Jesse Barnesf7d23232014-03-31 11:13:56 -07002130
Jani Nikula1250d102014-08-12 17:11:39 +03002131/* Disable backlight PP control and backlight PWM. */
2132void intel_edp_backlight_off(struct intel_dp *intel_dp)
2133{
2134 if (!is_edp(intel_dp))
2135 return;
2136
2137 DRM_DEBUG_KMS("\n");
2138
2139 _intel_edp_backlight_off(intel_dp);
Jesse Barnesf7d23232014-03-31 11:13:56 -07002140 intel_panel_disable_backlight(intel_dp->attached_connector);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002141}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002142
Jani Nikula73580fb72014-08-12 17:11:41 +03002143/*
2144 * Hook for controlling the panel power control backlight through the bl_power
2145 * sysfs attribute. Take care to handle multiple calls.
2146 */
2147static void intel_edp_backlight_power(struct intel_connector *connector,
2148 bool enable)
2149{
2150 struct intel_dp *intel_dp = intel_attached_dp(&connector->base);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03002151 bool is_enabled;
2152
Ville Syrjälä773538e82014-09-04 14:54:56 +03002153 pps_lock(intel_dp);
Ville Syrjäläe39b9992014-09-04 14:53:14 +03002154 is_enabled = ironlake_get_pp_control(intel_dp) & EDP_BLC_ENABLE;
Ville Syrjälä773538e82014-09-04 14:54:56 +03002155 pps_unlock(intel_dp);
Jani Nikula73580fb72014-08-12 17:11:41 +03002156
2157 if (is_enabled == enable)
2158 return;
2159
Jani Nikula23ba9372014-08-27 14:08:43 +03002160 DRM_DEBUG_KMS("panel power control backlight %s\n",
2161 enable ? "enable" : "disable");
Jani Nikula73580fb72014-08-12 17:11:41 +03002162
2163 if (enable)
2164 _intel_edp_backlight_on(intel_dp);
2165 else
2166 _intel_edp_backlight_off(intel_dp);
2167}
2168
Ville Syrjälä64e10772015-10-29 21:26:01 +02002169static void assert_dp_port(struct intel_dp *intel_dp, bool state)
2170{
2171 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2172 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
2173 bool cur_state = I915_READ(intel_dp->output_reg) & DP_PORT_EN;
2174
2175 I915_STATE_WARN(cur_state != state,
2176 "DP port %c state assertion failure (expected %s, current %s)\n",
2177 port_name(dig_port->port),
Jani Nikula87ad3212016-01-14 12:53:34 +02002178 onoff(state), onoff(cur_state));
Ville Syrjälä64e10772015-10-29 21:26:01 +02002179}
2180#define assert_dp_port_disabled(d) assert_dp_port((d), false)
2181
2182static void assert_edp_pll(struct drm_i915_private *dev_priv, bool state)
2183{
2184 bool cur_state = I915_READ(DP_A) & DP_PLL_ENABLE;
2185
2186 I915_STATE_WARN(cur_state != state,
2187 "eDP PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02002188 onoff(state), onoff(cur_state));
Ville Syrjälä64e10772015-10-29 21:26:01 +02002189}
2190#define assert_edp_pll_enabled(d) assert_edp_pll((d), true)
2191#define assert_edp_pll_disabled(d) assert_edp_pll((d), false)
2192
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002193static void ironlake_edp_pll_on(struct intel_dp *intel_dp)
Jesse Barnesd240f202010-08-13 15:43:26 -07002194{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002195 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Ville Syrjälä64e10772015-10-29 21:26:01 +02002196 struct intel_crtc *crtc = to_intel_crtc(intel_dig_port->base.base.crtc);
2197 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesd240f202010-08-13 15:43:26 -07002198
Ville Syrjälä64e10772015-10-29 21:26:01 +02002199 assert_pipe_disabled(dev_priv, crtc->pipe);
2200 assert_dp_port_disabled(intel_dp);
2201 assert_edp_pll_disabled(dev_priv);
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002202
Ville Syrjäläabfce942015-10-29 21:26:03 +02002203 DRM_DEBUG_KMS("enabling eDP PLL for clock %d\n",
2204 crtc->config->port_clock);
2205
2206 intel_dp->DP &= ~DP_PLL_FREQ_MASK;
2207
2208 if (crtc->config->port_clock == 162000)
2209 intel_dp->DP |= DP_PLL_FREQ_162MHZ;
2210 else
2211 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
2212
2213 I915_WRITE(DP_A, intel_dp->DP);
2214 POSTING_READ(DP_A);
2215 udelay(500);
2216
Daniel Vetter07679352012-09-06 22:15:42 +02002217 intel_dp->DP |= DP_PLL_ENABLE;
Ville Syrjälä6fec7662015-11-10 16:16:17 +02002218
Daniel Vetter07679352012-09-06 22:15:42 +02002219 I915_WRITE(DP_A, intel_dp->DP);
Jesse Barnes298b0b32010-10-07 16:01:24 -07002220 POSTING_READ(DP_A);
2221 udelay(200);
Jesse Barnesd240f202010-08-13 15:43:26 -07002222}
2223
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002224static void ironlake_edp_pll_off(struct intel_dp *intel_dp)
Jesse Barnesd240f202010-08-13 15:43:26 -07002225{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02002226 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Ville Syrjälä64e10772015-10-29 21:26:01 +02002227 struct intel_crtc *crtc = to_intel_crtc(intel_dig_port->base.base.crtc);
2228 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesd240f202010-08-13 15:43:26 -07002229
Ville Syrjälä64e10772015-10-29 21:26:01 +02002230 assert_pipe_disabled(dev_priv, crtc->pipe);
2231 assert_dp_port_disabled(intel_dp);
2232 assert_edp_pll_enabled(dev_priv);
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002233
Ville Syrjäläabfce942015-10-29 21:26:03 +02002234 DRM_DEBUG_KMS("disabling eDP PLL\n");
2235
Ville Syrjälä6fec7662015-11-10 16:16:17 +02002236 intel_dp->DP &= ~DP_PLL_ENABLE;
Daniel Vetter07679352012-09-06 22:15:42 +02002237
Ville Syrjälä6fec7662015-11-10 16:16:17 +02002238 I915_WRITE(DP_A, intel_dp->DP);
Chris Wilson1af5fa12010-09-08 21:07:28 +01002239 POSTING_READ(DP_A);
Jesse Barnesd240f202010-08-13 15:43:26 -07002240 udelay(200);
2241}
2242
Jesse Barnesc7ad3812011-07-07 11:11:03 -07002243/* If the sink supports it, try to set the power state appropriately */
Paulo Zanonic19b0662012-10-15 15:51:41 -03002244void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
Jesse Barnesc7ad3812011-07-07 11:11:03 -07002245{
2246 int ret, i;
2247
2248 /* Should have a valid DPCD by this point */
2249 if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
2250 return;
2251
2252 if (mode != DRM_MODE_DPMS_ON) {
Jani Nikula9d1a1032014-03-14 16:51:15 +02002253 ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
2254 DP_SET_POWER_D3);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07002255 } else {
2256 /*
2257 * When turning on, we need to retry for 1ms to give the sink
2258 * time to wake up.
2259 */
2260 for (i = 0; i < 3; i++) {
Jani Nikula9d1a1032014-03-14 16:51:15 +02002261 ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
2262 DP_SET_POWER_D0);
Jesse Barnesc7ad3812011-07-07 11:11:03 -07002263 if (ret == 1)
2264 break;
2265 msleep(1);
2266 }
2267 }
Jani Nikulaf9cac722014-09-02 16:33:52 +03002268
2269 if (ret != 1)
2270 DRM_DEBUG_KMS("failed to %s sink power state\n",
2271 mode == DRM_MODE_DPMS_ON ? "enable" : "disable");
Jesse Barnesc7ad3812011-07-07 11:11:03 -07002272}
2273
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002274static bool intel_dp_get_hw_state(struct intel_encoder *encoder,
2275 enum pipe *pipe)
Jesse Barnesd240f202010-08-13 15:43:26 -07002276{
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002277 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deakbc7d38a2013-05-16 14:40:36 +03002278 enum port port = dp_to_dig_port(intel_dp)->port;
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002279 struct drm_device *dev = encoder->base.dev;
2280 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak6d129be2014-03-05 16:20:54 +02002281 enum intel_display_power_domain power_domain;
2282 u32 tmp;
Imre Deak6fa9a5e2016-02-12 18:55:18 +02002283 bool ret;
Imre Deak6d129be2014-03-05 16:20:54 +02002284
2285 power_domain = intel_display_port_power_domain(encoder);
Imre Deak6fa9a5e2016-02-12 18:55:18 +02002286 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deak6d129be2014-03-05 16:20:54 +02002287 return false;
2288
Imre Deak6fa9a5e2016-02-12 18:55:18 +02002289 ret = false;
2290
Imre Deak6d129be2014-03-05 16:20:54 +02002291 tmp = I915_READ(intel_dp->output_reg);
Jesse Barnesd240f202010-08-13 15:43:26 -07002292
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002293 if (!(tmp & DP_PORT_EN))
Imre Deak6fa9a5e2016-02-12 18:55:18 +02002294 goto out;
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002295
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03002296 if (IS_GEN7(dev) && port == PORT_A) {
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002297 *pipe = PORT_TO_PIPE_CPT(tmp);
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03002298 } else if (HAS_PCH_CPT(dev) && port != PORT_A) {
Ville Syrjäläadc289d2015-05-05 17:17:30 +03002299 enum pipe p;
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002300
Ville Syrjäläadc289d2015-05-05 17:17:30 +03002301 for_each_pipe(dev_priv, p) {
2302 u32 trans_dp = I915_READ(TRANS_DP_CTL(p));
2303 if (TRANS_DP_PIPE_TO_PORT(trans_dp) == port) {
2304 *pipe = p;
Imre Deak6fa9a5e2016-02-12 18:55:18 +02002305 ret = true;
2306
2307 goto out;
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002308 }
2309 }
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002310
Daniel Vetter4a0833e2012-10-26 10:58:11 +02002311 DRM_DEBUG_KMS("No pipe for dp port 0x%x found\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002312 i915_mmio_reg_offset(intel_dp->output_reg));
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03002313 } else if (IS_CHERRYVIEW(dev)) {
2314 *pipe = DP_PORT_TO_PIPE_CHV(tmp);
2315 } else {
2316 *pipe = PORT_TO_PIPE(tmp);
Daniel Vetter4a0833e2012-10-26 10:58:11 +02002317 }
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002318
Imre Deak6fa9a5e2016-02-12 18:55:18 +02002319 ret = true;
2320
2321out:
2322 intel_display_power_put(dev_priv, power_domain);
2323
2324 return ret;
Daniel Vetter19d8fe12012-07-02 13:26:27 +02002325}
2326
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002327static void intel_dp_get_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002328 struct intel_crtc_state *pipe_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002329{
2330 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002331 u32 tmp, flags = 0;
Xiong Zhang63000ef2013-06-28 12:59:06 +08002332 struct drm_device *dev = encoder->base.dev;
2333 struct drm_i915_private *dev_priv = dev->dev_private;
2334 enum port port = dp_to_dig_port(intel_dp)->port;
2335 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002336
Daniel Vetter9ed109a2014-04-24 23:54:52 +02002337 tmp = I915_READ(intel_dp->output_reg);
Jani Nikula9fcb1702015-05-05 16:32:12 +03002338
2339 pipe_config->has_audio = tmp & DP_AUDIO_OUTPUT_ENABLE && port != PORT_A;
Daniel Vetter9ed109a2014-04-24 23:54:52 +02002340
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03002341 if (HAS_PCH_CPT(dev) && port != PORT_A) {
Ville Syrjäläb81e34c2015-07-06 15:10:03 +03002342 u32 trans_dp = I915_READ(TRANS_DP_CTL(crtc->pipe));
2343
2344 if (trans_dp & TRANS_DP_HSYNC_ACTIVE_HIGH)
Xiong Zhang63000ef2013-06-28 12:59:06 +08002345 flags |= DRM_MODE_FLAG_PHSYNC;
2346 else
2347 flags |= DRM_MODE_FLAG_NHSYNC;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002348
Ville Syrjäläb81e34c2015-07-06 15:10:03 +03002349 if (trans_dp & TRANS_DP_VSYNC_ACTIVE_HIGH)
Xiong Zhang63000ef2013-06-28 12:59:06 +08002350 flags |= DRM_MODE_FLAG_PVSYNC;
2351 else
2352 flags |= DRM_MODE_FLAG_NVSYNC;
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03002353 } else {
2354 if (tmp & DP_SYNC_HS_HIGH)
2355 flags |= DRM_MODE_FLAG_PHSYNC;
2356 else
2357 flags |= DRM_MODE_FLAG_NHSYNC;
2358
2359 if (tmp & DP_SYNC_VS_HIGH)
2360 flags |= DRM_MODE_FLAG_PVSYNC;
2361 else
2362 flags |= DRM_MODE_FLAG_NVSYNC;
Xiong Zhang63000ef2013-06-28 12:59:06 +08002363 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002364
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002365 pipe_config->base.adjusted_mode.flags |= flags;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03002366
Ville Syrjälä8c875fc2014-09-12 15:46:29 +03002367 if (!HAS_PCH_SPLIT(dev) && !IS_VALLEYVIEW(dev) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08002368 !IS_CHERRYVIEW(dev) && tmp & DP_COLOR_RANGE_16_235)
Ville Syrjälä8c875fc2014-09-12 15:46:29 +03002369 pipe_config->limited_color_range = true;
2370
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03002371 pipe_config->has_dp_encoder = true;
2372
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +03002373 pipe_config->lane_count =
2374 ((tmp & DP_PORT_WIDTH_MASK) >> DP_PORT_WIDTH_SHIFT) + 1;
2375
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03002376 intel_dp_get_m_n(crtc, pipe_config);
2377
Ville Syrjälä18442d02013-09-13 16:00:08 +03002378 if (port == PORT_A) {
Ville Syrjäläb377e0d2015-10-29 21:25:59 +02002379 if ((I915_READ(DP_A) & DP_PLL_FREQ_MASK) == DP_PLL_FREQ_162MHZ)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03002380 pipe_config->port_clock = 162000;
2381 else
2382 pipe_config->port_clock = 270000;
2383 }
Ville Syrjälä18442d02013-09-13 16:00:08 +03002384
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02002385 pipe_config->base.adjusted_mode.crtc_clock =
2386 intel_dotclock_calculate(pipe_config->port_clock,
2387 &pipe_config->dp_m_n);
Daniel Vetter7f16e5c2013-11-04 16:28:47 +01002388
Jani Nikulac6cd2ee2013-10-21 10:52:07 +03002389 if (is_edp(intel_dp) && dev_priv->vbt.edp_bpp &&
2390 pipe_config->pipe_bpp > dev_priv->vbt.edp_bpp) {
2391 /*
2392 * This is a big fat ugly hack.
2393 *
2394 * Some machines in UEFI boot mode provide us a VBT that has 18
2395 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
2396 * unknown we fail to light up. Yet the same BIOS boots up with
2397 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
2398 * max, not what it tells us to use.
2399 *
2400 * Note: This will still be broken if the eDP panel is not lit
2401 * up by the BIOS, and thus we can't get the mode at module
2402 * load.
2403 */
2404 DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
2405 pipe_config->pipe_bpp, dev_priv->vbt.edp_bpp);
2406 dev_priv->vbt.edp_bpp = pipe_config->pipe_bpp;
2407 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002408}
2409
Daniel Vettere8cb4552012-07-01 13:05:48 +02002410static void intel_disable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07002411{
Daniel Vettere8cb4552012-07-01 13:05:48 +02002412 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deak982a3862013-05-23 19:39:40 +03002413 struct drm_device *dev = encoder->base.dev;
Jani Nikula495a5bb2014-10-27 16:26:55 +02002414 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
2415
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002416 if (crtc->config->has_audio)
Jani Nikula495a5bb2014-10-27 16:26:55 +02002417 intel_audio_codec_disable(encoder);
Daniel Vetter6cb49832012-05-20 17:14:50 +02002418
Rodrigo Vivib32c6f42014-11-20 03:44:37 -08002419 if (HAS_PSR(dev) && !HAS_DDI(dev))
2420 intel_psr_disable(intel_dp);
2421
Daniel Vetter6cb49832012-05-20 17:14:50 +02002422 /* Make sure the panel is off before trying to change the mode. But also
2423 * ensure that we have vdd while we switch off the panel. */
Jani Nikula24f3e092014-03-17 16:43:36 +02002424 intel_edp_panel_vdd_on(intel_dp);
Daniel Vetter4be73782014-01-17 14:39:48 +01002425 intel_edp_backlight_off(intel_dp);
Jani Nikulafdbc3b12013-11-12 17:10:13 +02002426 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
Daniel Vetter4be73782014-01-17 14:39:48 +01002427 intel_edp_panel_off(intel_dp);
Daniel Vetter37398502012-09-06 22:15:44 +02002428
Ville Syrjälä08aff3f2014-08-18 22:16:09 +03002429 /* disable the port before the pipe on g4x */
2430 if (INTEL_INFO(dev)->gen < 5)
Daniel Vetter37398502012-09-06 22:15:44 +02002431 intel_dp_link_down(intel_dp);
Jesse Barnesd240f202010-08-13 15:43:26 -07002432}
2433
Ville Syrjälä08aff3f2014-08-18 22:16:09 +03002434static void ilk_post_disable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07002435{
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002436 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Imre Deak982a3862013-05-23 19:39:40 +03002437 enum port port = dp_to_dig_port(intel_dp)->port;
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002438
Ville Syrjälä49277c32014-03-31 18:21:26 +03002439 intel_dp_link_down(intel_dp);
Ville Syrjäläabfce942015-10-29 21:26:03 +02002440
2441 /* Only ilk+ has port A */
Ville Syrjälä08aff3f2014-08-18 22:16:09 +03002442 if (port == PORT_A)
2443 ironlake_edp_pll_off(intel_dp);
Ville Syrjälä49277c32014-03-31 18:21:26 +03002444}
2445
2446static void vlv_post_disable_dp(struct intel_encoder *encoder)
2447{
2448 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2449
2450 intel_dp_link_down(intel_dp);
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002451}
2452
Ville Syrjäläa8f327f2015-07-09 20:14:11 +03002453static void chv_data_lane_soft_reset(struct intel_encoder *encoder,
2454 bool reset)
2455{
2456 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2457 enum dpio_channel ch = vlv_dport_to_channel(enc_to_dig_port(&encoder->base));
2458 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
2459 enum pipe pipe = crtc->pipe;
2460 uint32_t val;
2461
2462 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW0(ch));
2463 if (reset)
2464 val &= ~(DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
2465 else
2466 val |= DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET;
2467 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW0(ch), val);
2468
2469 if (crtc->config->lane_count > 2) {
2470 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW0(ch));
2471 if (reset)
2472 val &= ~(DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
2473 else
2474 val |= DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET;
2475 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW0(ch), val);
2476 }
2477
2478 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW1(ch));
2479 val |= CHV_PCS_REQ_SOFTRESET_EN;
2480 if (reset)
2481 val &= ~DPIO_PCS_CLK_SOFT_RESET;
2482 else
2483 val |= DPIO_PCS_CLK_SOFT_RESET;
2484 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW1(ch), val);
2485
2486 if (crtc->config->lane_count > 2) {
2487 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW1(ch));
2488 val |= CHV_PCS_REQ_SOFTRESET_EN;
2489 if (reset)
2490 val &= ~DPIO_PCS_CLK_SOFT_RESET;
2491 else
2492 val |= DPIO_PCS_CLK_SOFT_RESET;
2493 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW1(ch), val);
2494 }
2495}
2496
Ville Syrjälä580d3812014-04-09 13:29:00 +03002497static void chv_post_disable_dp(struct intel_encoder *encoder)
2498{
2499 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Ville Syrjälä580d3812014-04-09 13:29:00 +03002500 struct drm_device *dev = encoder->base.dev;
2501 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä580d3812014-04-09 13:29:00 +03002502
2503 intel_dp_link_down(intel_dp);
2504
Ville Syrjäläa5805162015-05-26 20:42:30 +03002505 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä580d3812014-04-09 13:29:00 +03002506
Ville Syrjäläa8f327f2015-07-09 20:14:11 +03002507 /* Assert data lane reset */
2508 chv_data_lane_soft_reset(encoder, true);
Ville Syrjälä580d3812014-04-09 13:29:00 +03002509
Ville Syrjäläa5805162015-05-26 20:42:30 +03002510 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä580d3812014-04-09 13:29:00 +03002511}
2512
Ville Syrjälä7b13b582014-08-18 22:16:08 +03002513static void
2514_intel_dp_set_link_train(struct intel_dp *intel_dp,
2515 uint32_t *DP,
2516 uint8_t dp_train_pat)
2517{
2518 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2519 struct drm_device *dev = intel_dig_port->base.base.dev;
2520 struct drm_i915_private *dev_priv = dev->dev_private;
2521 enum port port = intel_dig_port->port;
2522
2523 if (HAS_DDI(dev)) {
2524 uint32_t temp = I915_READ(DP_TP_CTL(port));
2525
2526 if (dp_train_pat & DP_LINK_SCRAMBLING_DISABLE)
2527 temp |= DP_TP_CTL_SCRAMBLE_DISABLE;
2528 else
2529 temp &= ~DP_TP_CTL_SCRAMBLE_DISABLE;
2530
2531 temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
2532 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2533 case DP_TRAINING_PATTERN_DISABLE:
2534 temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
2535
2536 break;
2537 case DP_TRAINING_PATTERN_1:
2538 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
2539 break;
2540 case DP_TRAINING_PATTERN_2:
2541 temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
2542 break;
2543 case DP_TRAINING_PATTERN_3:
2544 temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
2545 break;
2546 }
2547 I915_WRITE(DP_TP_CTL(port), temp);
2548
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03002549 } else if ((IS_GEN7(dev) && port == PORT_A) ||
2550 (HAS_PCH_CPT(dev) && port != PORT_A)) {
Ville Syrjälä7b13b582014-08-18 22:16:08 +03002551 *DP &= ~DP_LINK_TRAIN_MASK_CPT;
2552
2553 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2554 case DP_TRAINING_PATTERN_DISABLE:
2555 *DP |= DP_LINK_TRAIN_OFF_CPT;
2556 break;
2557 case DP_TRAINING_PATTERN_1:
2558 *DP |= DP_LINK_TRAIN_PAT_1_CPT;
2559 break;
2560 case DP_TRAINING_PATTERN_2:
2561 *DP |= DP_LINK_TRAIN_PAT_2_CPT;
2562 break;
2563 case DP_TRAINING_PATTERN_3:
2564 DRM_ERROR("DP training pattern 3 not supported\n");
2565 *DP |= DP_LINK_TRAIN_PAT_2_CPT;
2566 break;
2567 }
2568
2569 } else {
2570 if (IS_CHERRYVIEW(dev))
2571 *DP &= ~DP_LINK_TRAIN_MASK_CHV;
2572 else
2573 *DP &= ~DP_LINK_TRAIN_MASK;
2574
2575 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
2576 case DP_TRAINING_PATTERN_DISABLE:
2577 *DP |= DP_LINK_TRAIN_OFF;
2578 break;
2579 case DP_TRAINING_PATTERN_1:
2580 *DP |= DP_LINK_TRAIN_PAT_1;
2581 break;
2582 case DP_TRAINING_PATTERN_2:
2583 *DP |= DP_LINK_TRAIN_PAT_2;
2584 break;
2585 case DP_TRAINING_PATTERN_3:
2586 if (IS_CHERRYVIEW(dev)) {
2587 *DP |= DP_LINK_TRAIN_PAT_3_CHV;
2588 } else {
2589 DRM_ERROR("DP training pattern 3 not supported\n");
2590 *DP |= DP_LINK_TRAIN_PAT_2;
2591 }
2592 break;
2593 }
2594 }
2595}
2596
2597static void intel_dp_enable_port(struct intel_dp *intel_dp)
2598{
2599 struct drm_device *dev = intel_dp_to_dev(intel_dp);
2600 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä6fec7662015-11-10 16:16:17 +02002601 struct intel_crtc *crtc =
2602 to_intel_crtc(dp_to_dig_port(intel_dp)->base.base.crtc);
Ville Syrjälä7b13b582014-08-18 22:16:08 +03002603
Ville Syrjälä7b13b582014-08-18 22:16:08 +03002604 /* enable with pattern 1 (as per spec) */
2605 _intel_dp_set_link_train(intel_dp, &intel_dp->DP,
2606 DP_TRAINING_PATTERN_1);
2607
2608 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
2609 POSTING_READ(intel_dp->output_reg);
Ville Syrjälä7b713f52014-10-16 21:27:35 +03002610
2611 /*
2612 * Magic for VLV/CHV. We _must_ first set up the register
2613 * without actually enabling the port, and then do another
2614 * write to enable the port. Otherwise link training will
2615 * fail when the power sequencer is freshly used for this port.
2616 */
2617 intel_dp->DP |= DP_PORT_EN;
Ville Syrjälä6fec7662015-11-10 16:16:17 +02002618 if (crtc->config->has_audio)
2619 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
Ville Syrjälä7b713f52014-10-16 21:27:35 +03002620
2621 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
2622 POSTING_READ(intel_dp->output_reg);
Ville Syrjälä7b13b582014-08-18 22:16:08 +03002623}
2624
Daniel Vettere8cb4552012-07-01 13:05:48 +02002625static void intel_enable_dp(struct intel_encoder *encoder)
Jesse Barnesd240f202010-08-13 15:43:26 -07002626{
Daniel Vettere8cb4552012-07-01 13:05:48 +02002627 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2628 struct drm_device *dev = encoder->base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002629 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulac1dec792014-10-27 16:26:56 +02002630 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +01002631 uint32_t dp_reg = I915_READ(intel_dp->output_reg);
Ville Syrjäläd6fbdd12015-10-29 21:25:58 +02002632 enum port port = dp_to_dig_port(intel_dp)->port;
2633 enum pipe pipe = crtc->pipe;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002634
Daniel Vetter0c33d8d2012-09-06 22:15:43 +02002635 if (WARN_ON(dp_reg & DP_PORT_EN))
2636 return;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002637
Ville Syrjälä093e3f12014-10-16 21:27:33 +03002638 pps_lock(intel_dp);
2639
Wayne Boyer666a4532015-12-09 12:29:35 -08002640 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä093e3f12014-10-16 21:27:33 +03002641 vlv_init_panel_power_sequencer(intel_dp);
2642
Ville Syrjälä78645782015-11-20 22:09:19 +02002643 /*
2644 * We get an occasional spurious underrun between the port
2645 * enable and vdd enable, when enabling port A eDP.
2646 *
2647 * FIXME: Not sure if this applies to (PCH) port D eDP as well
2648 */
2649 if (port == PORT_A)
2650 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
2651
Ville Syrjälä7b13b582014-08-18 22:16:08 +03002652 intel_dp_enable_port(intel_dp);
Ville Syrjälä093e3f12014-10-16 21:27:33 +03002653
Ville Syrjäläd6fbdd12015-10-29 21:25:58 +02002654 if (port == PORT_A && IS_GEN5(dev_priv)) {
2655 /*
2656 * Underrun reporting for the other pipe was disabled in
2657 * g4x_pre_enable_dp(). The eDP PLL and port have now been
2658 * enabled, so it's now safe to re-enable underrun reporting.
2659 */
2660 intel_wait_for_vblank_if_active(dev_priv->dev, !pipe);
2661 intel_set_cpu_fifo_underrun_reporting(dev_priv, !pipe, true);
2662 intel_set_pch_fifo_underrun_reporting(dev_priv, !pipe, true);
2663 }
2664
Ville Syrjälä093e3f12014-10-16 21:27:33 +03002665 edp_panel_vdd_on(intel_dp);
2666 edp_panel_on(intel_dp);
2667 edp_panel_vdd_off(intel_dp, true);
2668
Ville Syrjälä78645782015-11-20 22:09:19 +02002669 if (port == PORT_A)
2670 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2671
Ville Syrjälä093e3f12014-10-16 21:27:33 +03002672 pps_unlock(intel_dp);
2673
Wayne Boyer666a4532015-12-09 12:29:35 -08002674 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002675 unsigned int lane_mask = 0x0;
2676
2677 if (IS_CHERRYVIEW(dev))
2678 lane_mask = intel_dp_unused_lane_mask(crtc->config->lane_count);
2679
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03002680 vlv_wait_port_ready(dev_priv, dp_to_dig_port(intel_dp),
2681 lane_mask);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002682 }
Ville Syrjälä61234fa2014-10-16 21:27:34 +03002683
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002684 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
2685 intel_dp_start_link_train(intel_dp);
Imre Deak3ab9c632013-05-03 12:57:41 +03002686 intel_dp_stop_link_train(intel_dp);
Jani Nikulac1dec792014-10-27 16:26:56 +02002687
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002688 if (crtc->config->has_audio) {
Jani Nikulac1dec792014-10-27 16:26:56 +02002689 DRM_DEBUG_DRIVER("Enabling DP audio on pipe %c\n",
Ville Syrjäläd6fbdd12015-10-29 21:25:58 +02002690 pipe_name(pipe));
Jani Nikulac1dec792014-10-27 16:26:56 +02002691 intel_audio_codec_enable(encoder);
2692 }
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002693}
Jesse Barnes89b667f2013-04-18 14:51:36 -07002694
Jani Nikulaecff4f32013-09-06 07:38:29 +03002695static void g4x_enable_dp(struct intel_encoder *encoder)
2696{
Jani Nikula828f5c62013-09-05 16:44:45 +03002697 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2698
Jani Nikulaecff4f32013-09-06 07:38:29 +03002699 intel_enable_dp(encoder);
Daniel Vetter4be73782014-01-17 14:39:48 +01002700 intel_edp_backlight_on(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002701}
Jesse Barnes89b667f2013-04-18 14:51:36 -07002702
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002703static void vlv_enable_dp(struct intel_encoder *encoder)
2704{
Jani Nikula828f5c62013-09-05 16:44:45 +03002705 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2706
Daniel Vetter4be73782014-01-17 14:39:48 +01002707 intel_edp_backlight_on(intel_dp);
Rodrigo Vivib32c6f42014-11-20 03:44:37 -08002708 intel_psr_enable(intel_dp);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002709}
2710
Jani Nikulaecff4f32013-09-06 07:38:29 +03002711static void g4x_pre_enable_dp(struct intel_encoder *encoder)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002712{
Ville Syrjäläd6fbdd12015-10-29 21:25:58 +02002713 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Daniel Vetter2bd2ad62012-09-06 22:15:41 +02002714 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
Ville Syrjäläd6fbdd12015-10-29 21:25:58 +02002715 enum port port = dp_to_dig_port(intel_dp)->port;
2716 enum pipe pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002717
Daniel Vetter8ac33ed2014-04-24 23:54:54 +02002718 intel_dp_prepare(encoder);
2719
Ville Syrjäläd6fbdd12015-10-29 21:25:58 +02002720 if (port == PORT_A && IS_GEN5(dev_priv)) {
2721 /*
2722 * We get FIFO underruns on the other pipe when
2723 * enabling the CPU eDP PLL, and when enabling CPU
2724 * eDP port. We could potentially avoid the PLL
2725 * underrun with a vblank wait just prior to enabling
2726 * the PLL, but that doesn't appear to help the port
2727 * enable case. Just sweep it all under the rug.
2728 */
2729 intel_set_cpu_fifo_underrun_reporting(dev_priv, !pipe, false);
2730 intel_set_pch_fifo_underrun_reporting(dev_priv, !pipe, false);
2731 }
2732
Daniel Vetterd41f1ef2014-04-24 23:54:53 +02002733 /* Only ilk+ has port A */
Ville Syrjäläabfce942015-10-29 21:26:03 +02002734 if (port == PORT_A)
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002735 ironlake_edp_pll_on(intel_dp);
2736}
2737
Ville Syrjälä83b84592014-10-16 21:29:51 +03002738static void vlv_detach_power_sequencer(struct intel_dp *intel_dp)
2739{
2740 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2741 struct drm_i915_private *dev_priv = intel_dig_port->base.base.dev->dev_private;
2742 enum pipe pipe = intel_dp->pps_pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002743 i915_reg_t pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
Ville Syrjälä83b84592014-10-16 21:29:51 +03002744
2745 edp_panel_vdd_off_sync(intel_dp);
2746
2747 /*
2748 * VLV seems to get confused when multiple power seqeuencers
2749 * have the same port selected (even if only one has power/vdd
2750 * enabled). The failure manifests as vlv_wait_port_ready() failing
2751 * CHV on the other hand doesn't seem to mind having the same port
2752 * selected in multiple power seqeuencers, but let's clear the
2753 * port select always when logically disconnecting a power sequencer
2754 * from a port.
2755 */
2756 DRM_DEBUG_KMS("detaching pipe %c power sequencer from port %c\n",
2757 pipe_name(pipe), port_name(intel_dig_port->port));
2758 I915_WRITE(pp_on_reg, 0);
2759 POSTING_READ(pp_on_reg);
2760
2761 intel_dp->pps_pipe = INVALID_PIPE;
2762}
2763
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002764static void vlv_steal_power_sequencer(struct drm_device *dev,
2765 enum pipe pipe)
2766{
2767 struct drm_i915_private *dev_priv = dev->dev_private;
2768 struct intel_encoder *encoder;
2769
2770 lockdep_assert_held(&dev_priv->pps_mutex);
2771
Ville Syrjäläac3c12e2014-10-16 21:29:56 +03002772 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
2773 return;
2774
Jani Nikula19c80542015-12-16 12:48:16 +02002775 for_each_intel_encoder(dev, encoder) {
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002776 struct intel_dp *intel_dp;
Ville Syrjälä773538e82014-09-04 14:54:56 +03002777 enum port port;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002778
2779 if (encoder->type != INTEL_OUTPUT_EDP)
2780 continue;
2781
2782 intel_dp = enc_to_intel_dp(&encoder->base);
Ville Syrjälä773538e82014-09-04 14:54:56 +03002783 port = dp_to_dig_port(intel_dp)->port;
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002784
2785 if (intel_dp->pps_pipe != pipe)
2786 continue;
2787
2788 DRM_DEBUG_KMS("stealing pipe %c power sequencer from port %c\n",
Ville Syrjälä773538e82014-09-04 14:54:56 +03002789 pipe_name(pipe), port_name(port));
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002790
Maarten Lankhorste02f9a02015-08-05 12:37:08 +02002791 WARN(encoder->base.crtc,
Ville Syrjälä034e43c2014-10-16 21:27:28 +03002792 "stealing pipe %c power sequencer from active eDP port %c\n",
2793 pipe_name(pipe), port_name(port));
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002794
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002795 /* make sure vdd is off before we steal it */
Ville Syrjälä83b84592014-10-16 21:29:51 +03002796 vlv_detach_power_sequencer(intel_dp);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002797 }
2798}
2799
2800static void vlv_init_panel_power_sequencer(struct intel_dp *intel_dp)
2801{
2802 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2803 struct intel_encoder *encoder = &intel_dig_port->base;
2804 struct drm_device *dev = encoder->base.dev;
2805 struct drm_i915_private *dev_priv = dev->dev_private;
2806 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002807
2808 lockdep_assert_held(&dev_priv->pps_mutex);
2809
Ville Syrjälä093e3f12014-10-16 21:27:33 +03002810 if (!is_edp(intel_dp))
2811 return;
2812
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002813 if (intel_dp->pps_pipe == crtc->pipe)
2814 return;
2815
2816 /*
2817 * If another power sequencer was being used on this
2818 * port previously make sure to turn off vdd there while
2819 * we still have control of it.
2820 */
2821 if (intel_dp->pps_pipe != INVALID_PIPE)
Ville Syrjälä83b84592014-10-16 21:29:51 +03002822 vlv_detach_power_sequencer(intel_dp);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002823
2824 /*
2825 * We may be stealing the power
2826 * sequencer from another port.
2827 */
2828 vlv_steal_power_sequencer(dev, crtc->pipe);
2829
2830 /* now it's all ours */
2831 intel_dp->pps_pipe = crtc->pipe;
2832
2833 DRM_DEBUG_KMS("initializing pipe %c power sequencer for port %c\n",
2834 pipe_name(intel_dp->pps_pipe), port_name(intel_dig_port->port));
2835
2836 /* init power sequencer on this pipe and port */
Ville Syrjälä36b5f422014-10-16 21:27:30 +03002837 intel_dp_init_panel_power_sequencer(dev, intel_dp);
2838 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03002839}
2840
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002841static void vlv_pre_enable_dp(struct intel_encoder *encoder)
2842{
2843 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2844 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
Jesse Barnesb2634012013-03-28 09:55:40 -07002845 struct drm_device *dev = encoder->base.dev;
Jesse Barnes89b667f2013-04-18 14:51:36 -07002846 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002847 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08002848 enum dpio_channel port = vlv_dport_to_channel(dport);
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002849 int pipe = intel_crtc->pipe;
2850 u32 val;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002851
Ville Syrjäläa5805162015-05-26 20:42:30 +03002852 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes89b667f2013-04-18 14:51:36 -07002853
Chon Ming Leeab3c7592013-11-07 10:43:30 +08002854 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW8(port));
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002855 val = 0;
2856 if (pipe)
2857 val |= (1<<21);
2858 else
2859 val &= ~(1<<21);
2860 val |= 0x001000c4;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08002861 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW8(port), val);
2862 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW14(port), 0x00760018);
2863 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW23(port), 0x00400888);
Jesse Barnes89b667f2013-04-18 14:51:36 -07002864
Ville Syrjäläa5805162015-05-26 20:42:30 +03002865 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes89b667f2013-04-18 14:51:36 -07002866
Jani Nikulaab1f90f2013-07-30 12:20:30 +03002867 intel_enable_dp(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07002868}
2869
Jani Nikulaecff4f32013-09-06 07:38:29 +03002870static void vlv_dp_pre_pll_enable(struct intel_encoder *encoder)
Jesse Barnes89b667f2013-04-18 14:51:36 -07002871{
2872 struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
2873 struct drm_device *dev = encoder->base.dev;
2874 struct drm_i915_private *dev_priv = dev->dev_private;
Chon Ming Lee5e69f972013-09-05 20:41:49 +08002875 struct intel_crtc *intel_crtc =
2876 to_intel_crtc(encoder->base.crtc);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08002877 enum dpio_channel port = vlv_dport_to_channel(dport);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08002878 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07002879
Daniel Vetter8ac33ed2014-04-24 23:54:54 +02002880 intel_dp_prepare(encoder);
2881
Jesse Barnes89b667f2013-04-18 14:51:36 -07002882 /* Program Tx lane resets to default */
Ville Syrjäläa5805162015-05-26 20:42:30 +03002883 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08002884 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW0(port),
Jesse Barnes89b667f2013-04-18 14:51:36 -07002885 DPIO_PCS_TX_LANE2_RESET |
2886 DPIO_PCS_TX_LANE1_RESET);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08002887 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW1(port),
Jesse Barnes89b667f2013-04-18 14:51:36 -07002888 DPIO_PCS_CLK_CRI_RXEB_EIOS_EN |
2889 DPIO_PCS_CLK_CRI_RXDIGFILTSG_EN |
2890 (1<<DPIO_PCS_CLK_DATAWIDTH_SHIFT) |
2891 DPIO_PCS_CLK_SOFT_RESET);
2892
2893 /* Fix up inter-pair skew failure */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08002894 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW12(port), 0x00750f00);
2895 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW11(port), 0x00001500);
2896 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW14(port), 0x40400000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03002897 mutex_unlock(&dev_priv->sb_lock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07002898}
2899
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002900static void chv_pre_enable_dp(struct intel_encoder *encoder)
2901{
2902 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2903 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
2904 struct drm_device *dev = encoder->base.dev;
2905 struct drm_i915_private *dev_priv = dev->dev_private;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002906 struct intel_crtc *intel_crtc =
2907 to_intel_crtc(encoder->base.crtc);
2908 enum dpio_channel ch = vlv_dport_to_channel(dport);
2909 int pipe = intel_crtc->pipe;
Ville Syrjälä2e523e92015-04-10 18:21:27 +03002910 int data, i, stagger;
Ville Syrjälä949c1d42014-04-09 13:28:58 +03002911 u32 val;
2912
Ville Syrjäläa5805162015-05-26 20:42:30 +03002913 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä949c1d42014-04-09 13:28:58 +03002914
Ville Syrjälä570e2a72014-08-18 14:42:46 +03002915 /* allow hardware to manage TX FIFO reset source */
2916 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW11(ch));
2917 val &= ~DPIO_LANEDESKEW_STRAP_OVRD;
2918 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW11(ch), val);
2919
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002920 if (intel_crtc->config->lane_count > 2) {
2921 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW11(ch));
2922 val &= ~DPIO_LANEDESKEW_STRAP_OVRD;
2923 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW11(ch), val);
2924 }
Ville Syrjälä570e2a72014-08-18 14:42:46 +03002925
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002926 /* Program Tx lane latency optimal setting*/
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002927 for (i = 0; i < intel_crtc->config->lane_count; i++) {
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002928 /* Set the upar bit */
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002929 if (intel_crtc->config->lane_count == 1)
2930 data = 0x0;
2931 else
2932 data = (i == 1) ? 0x0 : 0x1;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002933 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW14(ch, i),
2934 data << DPIO_UPAR_SHIFT);
2935 }
2936
2937 /* Data lane stagger programming */
Ville Syrjälä2e523e92015-04-10 18:21:27 +03002938 if (intel_crtc->config->port_clock > 270000)
2939 stagger = 0x18;
2940 else if (intel_crtc->config->port_clock > 135000)
2941 stagger = 0xd;
2942 else if (intel_crtc->config->port_clock > 67500)
2943 stagger = 0x7;
2944 else if (intel_crtc->config->port_clock > 33750)
2945 stagger = 0x4;
2946 else
2947 stagger = 0x2;
2948
2949 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW11(ch));
2950 val |= DPIO_TX2_STAGGER_MASK(0x1f);
2951 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW11(ch), val);
2952
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002953 if (intel_crtc->config->lane_count > 2) {
2954 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW11(ch));
2955 val |= DPIO_TX2_STAGGER_MASK(0x1f);
2956 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW11(ch), val);
2957 }
Ville Syrjälä2e523e92015-04-10 18:21:27 +03002958
2959 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW12(ch),
2960 DPIO_LANESTAGGER_STRAP(stagger) |
2961 DPIO_LANESTAGGER_STRAP_OVRD |
2962 DPIO_TX1_STAGGER_MASK(0x1f) |
2963 DPIO_TX1_STAGGER_MULT(6) |
2964 DPIO_TX2_STAGGER_MULT(0));
2965
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002966 if (intel_crtc->config->lane_count > 2) {
2967 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW12(ch),
2968 DPIO_LANESTAGGER_STRAP(stagger) |
2969 DPIO_LANESTAGGER_STRAP_OVRD |
2970 DPIO_TX1_STAGGER_MASK(0x1f) |
2971 DPIO_TX1_STAGGER_MULT(7) |
2972 DPIO_TX2_STAGGER_MULT(5));
2973 }
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002974
Ville Syrjäläa8f327f2015-07-09 20:14:11 +03002975 /* Deassert data lane reset */
2976 chv_data_lane_soft_reset(encoder, false);
2977
Ville Syrjäläa5805162015-05-26 20:42:30 +03002978 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002979
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002980 intel_enable_dp(encoder);
Ville Syrjäläb0b33842015-07-08 23:45:55 +03002981
2982 /* Second common lane will stay alive on its own now */
2983 if (dport->release_cl2_override) {
2984 chv_phy_powergate_ch(dev_priv, DPIO_PHY0, DPIO_CH1, false);
2985 dport->release_cl2_override = false;
2986 }
Chon Ming Leee4a1d842014-04-09 13:28:20 +03002987}
2988
Ville Syrjälä9197c882014-04-09 13:29:05 +03002989static void chv_dp_pre_pll_enable(struct intel_encoder *encoder)
2990{
2991 struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
2992 struct drm_device *dev = encoder->base.dev;
2993 struct drm_i915_private *dev_priv = dev->dev_private;
2994 struct intel_crtc *intel_crtc =
2995 to_intel_crtc(encoder->base.crtc);
2996 enum dpio_channel ch = vlv_dport_to_channel(dport);
2997 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002998 unsigned int lane_mask =
2999 intel_dp_unused_lane_mask(intel_crtc->config->lane_count);
Ville Syrjälä9197c882014-04-09 13:29:05 +03003000 u32 val;
3001
Ville Syrjälä625695f2014-06-28 02:04:02 +03003002 intel_dp_prepare(encoder);
3003
Ville Syrjäläb0b33842015-07-08 23:45:55 +03003004 /*
3005 * Must trick the second common lane into life.
3006 * Otherwise we can't even access the PLL.
3007 */
3008 if (ch == DPIO_CH0 && pipe == PIPE_B)
3009 dport->release_cl2_override =
3010 !chv_phy_powergate_ch(dev_priv, DPIO_PHY0, DPIO_CH1, true);
3011
Ville Syrjäläe0fce782015-07-08 23:45:54 +03003012 chv_phy_powergate_lanes(encoder, true, lane_mask);
3013
Ville Syrjäläa5805162015-05-26 20:42:30 +03003014 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä9197c882014-04-09 13:29:05 +03003015
Ville Syrjäläa8f327f2015-07-09 20:14:11 +03003016 /* Assert data lane reset */
3017 chv_data_lane_soft_reset(encoder, true);
3018
Ville Syrjäläb9e5ac32014-05-27 16:30:18 +03003019 /* program left/right clock distribution */
3020 if (pipe != PIPE_B) {
3021 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
3022 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
3023 if (ch == DPIO_CH0)
3024 val |= CHV_BUFLEFTENA1_FORCE;
3025 if (ch == DPIO_CH1)
3026 val |= CHV_BUFRIGHTENA1_FORCE;
3027 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
3028 } else {
3029 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
3030 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
3031 if (ch == DPIO_CH0)
3032 val |= CHV_BUFLEFTENA2_FORCE;
3033 if (ch == DPIO_CH1)
3034 val |= CHV_BUFRIGHTENA2_FORCE;
3035 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
3036 }
3037
Ville Syrjälä9197c882014-04-09 13:29:05 +03003038 /* program clock channel usage */
3039 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW8(ch));
3040 val |= CHV_PCS_USEDCLKCHANNEL_OVRRIDE;
3041 if (pipe != PIPE_B)
3042 val &= ~CHV_PCS_USEDCLKCHANNEL;
3043 else
3044 val |= CHV_PCS_USEDCLKCHANNEL;
3045 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW8(ch), val);
3046
Ville Syrjäläe0fce782015-07-08 23:45:54 +03003047 if (intel_crtc->config->lane_count > 2) {
3048 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW8(ch));
3049 val |= CHV_PCS_USEDCLKCHANNEL_OVRRIDE;
3050 if (pipe != PIPE_B)
3051 val &= ~CHV_PCS_USEDCLKCHANNEL;
3052 else
3053 val |= CHV_PCS_USEDCLKCHANNEL;
3054 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW8(ch), val);
3055 }
Ville Syrjälä9197c882014-04-09 13:29:05 +03003056
3057 /*
3058 * This a a bit weird since generally CL
3059 * matches the pipe, but here we need to
3060 * pick the CL based on the port.
3061 */
3062 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW19(ch));
3063 if (pipe != PIPE_B)
3064 val &= ~CHV_CMN_USEDCLKCHANNEL;
3065 else
3066 val |= CHV_CMN_USEDCLKCHANNEL;
3067 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW19(ch), val);
3068
Ville Syrjäläa5805162015-05-26 20:42:30 +03003069 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä9197c882014-04-09 13:29:05 +03003070}
3071
Ville Syrjäläd6db9952015-07-08 23:45:49 +03003072static void chv_dp_post_pll_disable(struct intel_encoder *encoder)
3073{
3074 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3075 enum pipe pipe = to_intel_crtc(encoder->base.crtc)->pipe;
3076 u32 val;
3077
3078 mutex_lock(&dev_priv->sb_lock);
3079
3080 /* disable left/right clock distribution */
3081 if (pipe != PIPE_B) {
3082 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
3083 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
3084 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
3085 } else {
3086 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
3087 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
3088 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
3089 }
3090
3091 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03003092
Ville Syrjäläb0b33842015-07-08 23:45:55 +03003093 /*
3094 * Leave the power down bit cleared for at least one
3095 * lane so that chv_powergate_phy_ch() will power
3096 * on something when the channel is otherwise unused.
3097 * When the port is off and the override is removed
3098 * the lanes power down anyway, so otherwise it doesn't
3099 * really matter what the state of power down bits is
3100 * after this.
3101 */
Ville Syrjäläe0fce782015-07-08 23:45:54 +03003102 chv_phy_powergate_lanes(encoder, false, 0x0);
Ville Syrjäläd6db9952015-07-08 23:45:49 +03003103}
3104
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003105/*
Jesse Barnesdf0c2372011-07-07 11:11:02 -07003106 * Native read with retry for link status and receiver capability reads for
3107 * cases where the sink may still be asleep.
Jani Nikula9d1a1032014-03-14 16:51:15 +02003108 *
3109 * Sinks are *supposed* to come up within 1ms from an off state, but we're also
3110 * supposed to retry 3 times per the spec.
Jesse Barnesdf0c2372011-07-07 11:11:02 -07003111 */
Jani Nikula9d1a1032014-03-14 16:51:15 +02003112static ssize_t
3113intel_dp_dpcd_read_wake(struct drm_dp_aux *aux, unsigned int offset,
3114 void *buffer, size_t size)
Jesse Barnesdf0c2372011-07-07 11:11:02 -07003115{
Jani Nikula9d1a1032014-03-14 16:51:15 +02003116 ssize_t ret;
3117 int i;
Jesse Barnesdf0c2372011-07-07 11:11:02 -07003118
Ville Syrjäläf6a19062014-10-16 20:46:09 +03003119 /*
3120 * Sometime we just get the same incorrect byte repeated
3121 * over the entire buffer. Doing just one throw away read
3122 * initially seems to "solve" it.
3123 */
3124 drm_dp_dpcd_read(aux, DP_DPCD_REV, buffer, 1);
3125
Jesse Barnesdf0c2372011-07-07 11:11:02 -07003126 for (i = 0; i < 3; i++) {
Jani Nikula9d1a1032014-03-14 16:51:15 +02003127 ret = drm_dp_dpcd_read(aux, offset, buffer, size);
3128 if (ret == size)
3129 return ret;
Jesse Barnesdf0c2372011-07-07 11:11:02 -07003130 msleep(1);
3131 }
3132
Jani Nikula9d1a1032014-03-14 16:51:15 +02003133 return ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003134}
3135
3136/*
3137 * Fetch AUX CH registers 0x202 - 0x207 which contain
3138 * link status information
3139 */
Ander Conselvan de Oliveira94223d02015-10-23 13:01:48 +03003140bool
Keith Packard93f62da2011-11-01 19:45:03 -07003141intel_dp_get_link_status(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003142{
Jani Nikula9d1a1032014-03-14 16:51:15 +02003143 return intel_dp_dpcd_read_wake(&intel_dp->aux,
3144 DP_LANE0_1_STATUS,
3145 link_status,
3146 DP_LINK_STATUS_SIZE) == DP_LINK_STATUS_SIZE;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003147}
3148
Paulo Zanoni11002442014-06-13 18:45:41 -03003149/* These are source-specific values. */
Ander Conselvan de Oliveira94223d02015-10-23 13:01:48 +03003150uint8_t
Keith Packard1a2eb462011-11-16 16:26:07 -08003151intel_dp_voltage_max(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003152{
Paulo Zanoni30add222012-10-26 19:05:45 -02003153 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Sonika Jindal7ad14a22015-02-25 10:29:12 +05303154 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deakbc7d38a2013-05-16 14:40:36 +03003155 enum port port = dp_to_dig_port(intel_dp)->port;
Keith Packard1a2eb462011-11-16 16:26:07 -08003156
Vandana Kannan93147262014-11-18 15:45:29 +05303157 if (IS_BROXTON(dev))
3158 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
3159 else if (INTEL_INFO(dev)->gen >= 9) {
Sonika Jindal9e458032015-05-06 17:35:48 +05303160 if (dev_priv->edp_low_vswing && port == PORT_A)
Sonika Jindal7ad14a22015-02-25 10:29:12 +05303161 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
Damien Lespiau5a9d1f12013-12-03 13:56:26 +00003162 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
Wayne Boyer666a4532015-12-09 12:29:35 -08003163 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Sonika Jindalbd600182014-08-08 16:23:41 +05303164 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
Imre Deakbc7d38a2013-05-16 14:40:36 +03003165 else if (IS_GEN7(dev) && port == PORT_A)
Sonika Jindalbd600182014-08-08 16:23:41 +05303166 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
Imre Deakbc7d38a2013-05-16 14:40:36 +03003167 else if (HAS_PCH_CPT(dev) && port != PORT_A)
Sonika Jindalbd600182014-08-08 16:23:41 +05303168 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
Keith Packard1a2eb462011-11-16 16:26:07 -08003169 else
Sonika Jindalbd600182014-08-08 16:23:41 +05303170 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
Keith Packard1a2eb462011-11-16 16:26:07 -08003171}
3172
Ander Conselvan de Oliveira94223d02015-10-23 13:01:48 +03003173uint8_t
Keith Packard1a2eb462011-11-16 16:26:07 -08003174intel_dp_pre_emphasis_max(struct intel_dp *intel_dp, uint8_t voltage_swing)
3175{
Paulo Zanoni30add222012-10-26 19:05:45 -02003176 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03003177 enum port port = dp_to_dig_port(intel_dp)->port;
Keith Packard1a2eb462011-11-16 16:26:07 -08003178
Damien Lespiau5a9d1f12013-12-03 13:56:26 +00003179 if (INTEL_INFO(dev)->gen >= 9) {
3180 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
3181 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3182 return DP_TRAIN_PRE_EMPH_LEVEL_3;
3183 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3184 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3185 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3186 return DP_TRAIN_PRE_EMPH_LEVEL_1;
Sonika Jindal7ad14a22015-02-25 10:29:12 +05303187 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
3188 return DP_TRAIN_PRE_EMPH_LEVEL_0;
Damien Lespiau5a9d1f12013-12-03 13:56:26 +00003189 default:
3190 return DP_TRAIN_PRE_EMPH_LEVEL_0;
3191 }
3192 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Paulo Zanonid6c0d722012-10-15 15:51:34 -03003193 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303194 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3195 return DP_TRAIN_PRE_EMPH_LEVEL_3;
3196 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3197 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3198 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3199 return DP_TRAIN_PRE_EMPH_LEVEL_1;
3200 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Paulo Zanonid6c0d722012-10-15 15:51:34 -03003201 default:
Sonika Jindalbd600182014-08-08 16:23:41 +05303202 return DP_TRAIN_PRE_EMPH_LEVEL_0;
Paulo Zanonid6c0d722012-10-15 15:51:34 -03003203 }
Wayne Boyer666a4532015-12-09 12:29:35 -08003204 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003205 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303206 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3207 return DP_TRAIN_PRE_EMPH_LEVEL_3;
3208 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3209 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3210 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3211 return DP_TRAIN_PRE_EMPH_LEVEL_1;
3212 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003213 default:
Sonika Jindalbd600182014-08-08 16:23:41 +05303214 return DP_TRAIN_PRE_EMPH_LEVEL_0;
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003215 }
Imre Deakbc7d38a2013-05-16 14:40:36 +03003216 } else if (IS_GEN7(dev) && port == PORT_A) {
Keith Packard1a2eb462011-11-16 16:26:07 -08003217 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303218 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3219 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3220 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3221 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3222 return DP_TRAIN_PRE_EMPH_LEVEL_1;
Keith Packard1a2eb462011-11-16 16:26:07 -08003223 default:
Sonika Jindalbd600182014-08-08 16:23:41 +05303224 return DP_TRAIN_PRE_EMPH_LEVEL_0;
Keith Packard1a2eb462011-11-16 16:26:07 -08003225 }
3226 } else {
3227 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303228 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
3229 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3230 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
3231 return DP_TRAIN_PRE_EMPH_LEVEL_2;
3232 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
3233 return DP_TRAIN_PRE_EMPH_LEVEL_1;
3234 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Keith Packard1a2eb462011-11-16 16:26:07 -08003235 default:
Sonika Jindalbd600182014-08-08 16:23:41 +05303236 return DP_TRAIN_PRE_EMPH_LEVEL_0;
Keith Packard1a2eb462011-11-16 16:26:07 -08003237 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003238 }
3239}
3240
Daniel Vetter5829975c2015-04-16 11:36:52 +02003241static uint32_t vlv_signal_levels(struct intel_dp *intel_dp)
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003242{
3243 struct drm_device *dev = intel_dp_to_dev(intel_dp);
3244 struct drm_i915_private *dev_priv = dev->dev_private;
3245 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08003246 struct intel_crtc *intel_crtc =
3247 to_intel_crtc(dport->base.base.crtc);
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003248 unsigned long demph_reg_value, preemph_reg_value,
3249 uniqtranscale_reg_value;
3250 uint8_t train_set = intel_dp->train_set[0];
Chon Ming Leee4607fc2013-11-06 14:36:35 +08003251 enum dpio_channel port = vlv_dport_to_channel(dport);
Chon Ming Lee5e69f972013-09-05 20:41:49 +08003252 int pipe = intel_crtc->pipe;
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003253
3254 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303255 case DP_TRAIN_PRE_EMPH_LEVEL_0:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003256 preemph_reg_value = 0x0004000;
3257 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303258 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003259 demph_reg_value = 0x2B405555;
3260 uniqtranscale_reg_value = 0x552AB83A;
3261 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303262 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003263 demph_reg_value = 0x2B404040;
3264 uniqtranscale_reg_value = 0x5548B83A;
3265 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303266 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003267 demph_reg_value = 0x2B245555;
3268 uniqtranscale_reg_value = 0x5560B83A;
3269 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303270 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003271 demph_reg_value = 0x2B405555;
3272 uniqtranscale_reg_value = 0x5598DA3A;
3273 break;
3274 default:
3275 return 0;
3276 }
3277 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303278 case DP_TRAIN_PRE_EMPH_LEVEL_1:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003279 preemph_reg_value = 0x0002000;
3280 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303281 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003282 demph_reg_value = 0x2B404040;
3283 uniqtranscale_reg_value = 0x5552B83A;
3284 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303285 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003286 demph_reg_value = 0x2B404848;
3287 uniqtranscale_reg_value = 0x5580B83A;
3288 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303289 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003290 demph_reg_value = 0x2B404040;
3291 uniqtranscale_reg_value = 0x55ADDA3A;
3292 break;
3293 default:
3294 return 0;
3295 }
3296 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303297 case DP_TRAIN_PRE_EMPH_LEVEL_2:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003298 preemph_reg_value = 0x0000000;
3299 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303300 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003301 demph_reg_value = 0x2B305555;
3302 uniqtranscale_reg_value = 0x5570B83A;
3303 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303304 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003305 demph_reg_value = 0x2B2B4040;
3306 uniqtranscale_reg_value = 0x55ADDA3A;
3307 break;
3308 default:
3309 return 0;
3310 }
3311 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303312 case DP_TRAIN_PRE_EMPH_LEVEL_3:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003313 preemph_reg_value = 0x0006000;
3314 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303315 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003316 demph_reg_value = 0x1B405555;
3317 uniqtranscale_reg_value = 0x55ADDA3A;
3318 break;
3319 default:
3320 return 0;
3321 }
3322 break;
3323 default:
3324 return 0;
3325 }
3326
Ville Syrjäläa5805162015-05-26 20:42:30 +03003327 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08003328 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW5(port), 0x00000000);
3329 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW4(port), demph_reg_value);
3330 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW2(port),
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003331 uniqtranscale_reg_value);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08003332 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW3(port), 0x0C782040);
3333 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW11(port), 0x00030000);
3334 vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW9(port), preemph_reg_value);
3335 vlv_dpio_write(dev_priv, pipe, VLV_TX_DW5(port), 0x80000000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03003336 mutex_unlock(&dev_priv->sb_lock);
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003337
3338 return 0;
3339}
3340
Ville Syrjälä67fa24b2015-07-08 23:45:48 +03003341static bool chv_need_uniq_trans_scale(uint8_t train_set)
3342{
3343 return (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) == DP_TRAIN_PRE_EMPH_LEVEL_0 &&
3344 (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) == DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
3345}
3346
Daniel Vetter5829975c2015-04-16 11:36:52 +02003347static uint32_t chv_signal_levels(struct intel_dp *intel_dp)
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003348{
3349 struct drm_device *dev = intel_dp_to_dev(intel_dp);
3350 struct drm_i915_private *dev_priv = dev->dev_private;
3351 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
3352 struct intel_crtc *intel_crtc = to_intel_crtc(dport->base.base.crtc);
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003353 u32 deemph_reg_value, margin_reg_value, val;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003354 uint8_t train_set = intel_dp->train_set[0];
3355 enum dpio_channel ch = vlv_dport_to_channel(dport);
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003356 enum pipe pipe = intel_crtc->pipe;
3357 int i;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003358
3359 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303360 case DP_TRAIN_PRE_EMPH_LEVEL_0:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003361 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303362 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003363 deemph_reg_value = 128;
3364 margin_reg_value = 52;
3365 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303366 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003367 deemph_reg_value = 128;
3368 margin_reg_value = 77;
3369 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303370 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003371 deemph_reg_value = 128;
3372 margin_reg_value = 102;
3373 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303374 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003375 deemph_reg_value = 128;
3376 margin_reg_value = 154;
3377 /* FIXME extra to set for 1200 */
3378 break;
3379 default:
3380 return 0;
3381 }
3382 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303383 case DP_TRAIN_PRE_EMPH_LEVEL_1:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003384 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303385 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003386 deemph_reg_value = 85;
3387 margin_reg_value = 78;
3388 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303389 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003390 deemph_reg_value = 85;
3391 margin_reg_value = 116;
3392 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303393 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003394 deemph_reg_value = 85;
3395 margin_reg_value = 154;
3396 break;
3397 default:
3398 return 0;
3399 }
3400 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303401 case DP_TRAIN_PRE_EMPH_LEVEL_2:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003402 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303403 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003404 deemph_reg_value = 64;
3405 margin_reg_value = 104;
3406 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303407 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003408 deemph_reg_value = 64;
3409 margin_reg_value = 154;
3410 break;
3411 default:
3412 return 0;
3413 }
3414 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303415 case DP_TRAIN_PRE_EMPH_LEVEL_3:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003416 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303417 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003418 deemph_reg_value = 43;
3419 margin_reg_value = 154;
3420 break;
3421 default:
3422 return 0;
3423 }
3424 break;
3425 default:
3426 return 0;
3427 }
3428
Ville Syrjäläa5805162015-05-26 20:42:30 +03003429 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003430
3431 /* Clear calc init */
Ville Syrjälä1966e592014-04-09 13:29:04 +03003432 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW10(ch));
3433 val &= ~(DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3);
Ville Syrjäläa02ef3c2014-08-18 14:42:45 +03003434 val &= ~(DPIO_PCS_TX1DEEMP_MASK | DPIO_PCS_TX2DEEMP_MASK);
3435 val |= DPIO_PCS_TX1DEEMP_9P5 | DPIO_PCS_TX2DEEMP_9P5;
Ville Syrjälä1966e592014-04-09 13:29:04 +03003436 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW10(ch), val);
3437
Ville Syrjäläe0fce782015-07-08 23:45:54 +03003438 if (intel_crtc->config->lane_count > 2) {
3439 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW10(ch));
3440 val &= ~(DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3);
3441 val &= ~(DPIO_PCS_TX1DEEMP_MASK | DPIO_PCS_TX2DEEMP_MASK);
3442 val |= DPIO_PCS_TX1DEEMP_9P5 | DPIO_PCS_TX2DEEMP_9P5;
3443 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW10(ch), val);
3444 }
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003445
Ville Syrjäläa02ef3c2014-08-18 14:42:45 +03003446 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW9(ch));
3447 val &= ~(DPIO_PCS_TX1MARGIN_MASK | DPIO_PCS_TX2MARGIN_MASK);
3448 val |= DPIO_PCS_TX1MARGIN_000 | DPIO_PCS_TX2MARGIN_000;
3449 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW9(ch), val);
3450
Ville Syrjäläe0fce782015-07-08 23:45:54 +03003451 if (intel_crtc->config->lane_count > 2) {
3452 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW9(ch));
3453 val &= ~(DPIO_PCS_TX1MARGIN_MASK | DPIO_PCS_TX2MARGIN_MASK);
3454 val |= DPIO_PCS_TX1MARGIN_000 | DPIO_PCS_TX2MARGIN_000;
3455 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW9(ch), val);
3456 }
Ville Syrjäläa02ef3c2014-08-18 14:42:45 +03003457
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003458 /* Program swing deemph */
Ville Syrjäläe0fce782015-07-08 23:45:54 +03003459 for (i = 0; i < intel_crtc->config->lane_count; i++) {
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003460 val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW4(ch, i));
3461 val &= ~DPIO_SWING_DEEMPH9P5_MASK;
3462 val |= deemph_reg_value << DPIO_SWING_DEEMPH9P5_SHIFT;
3463 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW4(ch, i), val);
3464 }
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003465
3466 /* Program swing margin */
Ville Syrjäläe0fce782015-07-08 23:45:54 +03003467 for (i = 0; i < intel_crtc->config->lane_count; i++) {
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003468 val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW2(ch, i));
Ville Syrjälä67fa24b2015-07-08 23:45:48 +03003469
Ville Syrjälä1fb44502014-06-28 02:04:03 +03003470 val &= ~DPIO_SWING_MARGIN000_MASK;
3471 val |= margin_reg_value << DPIO_SWING_MARGIN000_SHIFT;
Ville Syrjälä67fa24b2015-07-08 23:45:48 +03003472
3473 /*
3474 * Supposedly this value shouldn't matter when unique transition
3475 * scale is disabled, but in fact it does matter. Let's just
3476 * always program the same value and hope it's OK.
3477 */
3478 val &= ~(0xff << DPIO_UNIQ_TRANS_SCALE_SHIFT);
3479 val |= 0x9a << DPIO_UNIQ_TRANS_SCALE_SHIFT;
3480
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003481 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW2(ch, i), val);
3482 }
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003483
Ville Syrjälä67fa24b2015-07-08 23:45:48 +03003484 /*
3485 * The document said it needs to set bit 27 for ch0 and bit 26
3486 * for ch1. Might be a typo in the doc.
3487 * For now, for this unique transition scale selection, set bit
3488 * 27 for ch0 and ch1.
3489 */
Ville Syrjäläe0fce782015-07-08 23:45:54 +03003490 for (i = 0; i < intel_crtc->config->lane_count; i++) {
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003491 val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW3(ch, i));
Ville Syrjälä67fa24b2015-07-08 23:45:48 +03003492 if (chv_need_uniq_trans_scale(train_set))
Ville Syrjäläf72df8d2014-04-09 13:29:03 +03003493 val |= DPIO_TX_UNIQ_TRANS_SCALE_EN;
Ville Syrjälä67fa24b2015-07-08 23:45:48 +03003494 else
3495 val &= ~DPIO_TX_UNIQ_TRANS_SCALE_EN;
3496 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW3(ch, i), val);
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003497 }
3498
3499 /* Start swing calculation */
Ville Syrjälä1966e592014-04-09 13:29:04 +03003500 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW10(ch));
3501 val |= DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3;
3502 vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW10(ch), val);
3503
Ville Syrjäläe0fce782015-07-08 23:45:54 +03003504 if (intel_crtc->config->lane_count > 2) {
3505 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW10(ch));
3506 val |= DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3;
3507 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW10(ch), val);
3508 }
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003509
Ville Syrjäläa5805162015-05-26 20:42:30 +03003510 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003511
3512 return 0;
3513}
3514
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003515static uint32_t
Daniel Vetter5829975c2015-04-16 11:36:52 +02003516gen4_signal_levels(uint8_t train_set)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003517{
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003518 uint32_t signal_levels = 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003519
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003520 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303521 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003522 default:
3523 signal_levels |= DP_VOLTAGE_0_4;
3524 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303525 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003526 signal_levels |= DP_VOLTAGE_0_6;
3527 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303528 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003529 signal_levels |= DP_VOLTAGE_0_8;
3530 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303531 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003532 signal_levels |= DP_VOLTAGE_1_2;
3533 break;
3534 }
Chris Wilson3cf2efb2010-11-29 10:09:55 +00003535 switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303536 case DP_TRAIN_PRE_EMPH_LEVEL_0:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003537 default:
3538 signal_levels |= DP_PRE_EMPHASIS_0;
3539 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303540 case DP_TRAIN_PRE_EMPH_LEVEL_1:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003541 signal_levels |= DP_PRE_EMPHASIS_3_5;
3542 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303543 case DP_TRAIN_PRE_EMPH_LEVEL_2:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003544 signal_levels |= DP_PRE_EMPHASIS_6;
3545 break;
Sonika Jindalbd600182014-08-08 16:23:41 +05303546 case DP_TRAIN_PRE_EMPH_LEVEL_3:
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003547 signal_levels |= DP_PRE_EMPHASIS_9_5;
3548 break;
3549 }
3550 return signal_levels;
3551}
3552
Zhenyu Wange3421a12010-04-08 09:43:27 +08003553/* Gen6's DP voltage swing and pre-emphasis control */
3554static uint32_t
Daniel Vetter5829975c2015-04-16 11:36:52 +02003555gen6_edp_signal_levels(uint8_t train_set)
Zhenyu Wange3421a12010-04-08 09:43:27 +08003556{
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003557 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
3558 DP_TRAIN_PRE_EMPHASIS_MASK);
3559 switch (signal_levels) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303560 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3561 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003562 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
Sonika Jindalbd600182014-08-08 16:23:41 +05303563 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003564 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
Sonika Jindalbd600182014-08-08 16:23:41 +05303565 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
3566 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003567 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
Sonika Jindalbd600182014-08-08 16:23:41 +05303568 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
3569 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003570 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
Sonika Jindalbd600182014-08-08 16:23:41 +05303571 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
3572 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003573 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08003574 default:
Yuanhan Liu3c5a62b2011-01-06 18:26:08 +08003575 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
3576 "0x%x\n", signal_levels);
3577 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
Zhenyu Wange3421a12010-04-08 09:43:27 +08003578 }
3579}
3580
Keith Packard1a2eb462011-11-16 16:26:07 -08003581/* Gen7's DP voltage swing and pre-emphasis control */
3582static uint32_t
Daniel Vetter5829975c2015-04-16 11:36:52 +02003583gen7_edp_signal_levels(uint8_t train_set)
Keith Packard1a2eb462011-11-16 16:26:07 -08003584{
3585 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
3586 DP_TRAIN_PRE_EMPHASIS_MASK);
3587 switch (signal_levels) {
Sonika Jindalbd600182014-08-08 16:23:41 +05303588 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Keith Packard1a2eb462011-11-16 16:26:07 -08003589 return EDP_LINK_TRAIN_400MV_0DB_IVB;
Sonika Jindalbd600182014-08-08 16:23:41 +05303590 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Keith Packard1a2eb462011-11-16 16:26:07 -08003591 return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
Sonika Jindalbd600182014-08-08 16:23:41 +05303592 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
Keith Packard1a2eb462011-11-16 16:26:07 -08003593 return EDP_LINK_TRAIN_400MV_6DB_IVB;
3594
Sonika Jindalbd600182014-08-08 16:23:41 +05303595 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Keith Packard1a2eb462011-11-16 16:26:07 -08003596 return EDP_LINK_TRAIN_600MV_0DB_IVB;
Sonika Jindalbd600182014-08-08 16:23:41 +05303597 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Keith Packard1a2eb462011-11-16 16:26:07 -08003598 return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
3599
Sonika Jindalbd600182014-08-08 16:23:41 +05303600 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
Keith Packard1a2eb462011-11-16 16:26:07 -08003601 return EDP_LINK_TRAIN_800MV_0DB_IVB;
Sonika Jindalbd600182014-08-08 16:23:41 +05303602 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
Keith Packard1a2eb462011-11-16 16:26:07 -08003603 return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
3604
3605 default:
3606 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
3607 "0x%x\n", signal_levels);
3608 return EDP_LINK_TRAIN_500MV_0DB_IVB;
3609 }
3610}
3611
Ander Conselvan de Oliveira94223d02015-10-23 13:01:48 +03003612void
Ander Conselvan de Oliveiraf4eb6922015-10-23 13:01:44 +03003613intel_dp_set_signal_levels(struct intel_dp *intel_dp)
Paulo Zanonif0a34242012-12-06 16:51:50 -02003614{
3615 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03003616 enum port port = intel_dig_port->port;
Paulo Zanonif0a34242012-12-06 16:51:50 -02003617 struct drm_device *dev = intel_dig_port->base.base.dev;
Ander Conselvan de Oliveirab905a912015-10-23 13:01:47 +03003618 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehallf8896f52015-06-25 11:11:03 +03003619 uint32_t signal_levels, mask = 0;
Paulo Zanonif0a34242012-12-06 16:51:50 -02003620 uint8_t train_set = intel_dp->train_set[0];
3621
David Weinehallf8896f52015-06-25 11:11:03 +03003622 if (HAS_DDI(dev)) {
3623 signal_levels = ddi_signal_levels(intel_dp);
3624
3625 if (IS_BROXTON(dev))
3626 signal_levels = 0;
3627 else
3628 mask = DDI_BUF_EMP_MASK;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03003629 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter5829975c2015-04-16 11:36:52 +02003630 signal_levels = chv_signal_levels(intel_dp);
Pallavi Ge2fa6fb2013-04-18 14:44:28 -07003631 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter5829975c2015-04-16 11:36:52 +02003632 signal_levels = vlv_signal_levels(intel_dp);
Imre Deakbc7d38a2013-05-16 14:40:36 +03003633 } else if (IS_GEN7(dev) && port == PORT_A) {
Daniel Vetter5829975c2015-04-16 11:36:52 +02003634 signal_levels = gen7_edp_signal_levels(train_set);
Paulo Zanonif0a34242012-12-06 16:51:50 -02003635 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_IVB;
Imre Deakbc7d38a2013-05-16 14:40:36 +03003636 } else if (IS_GEN6(dev) && port == PORT_A) {
Daniel Vetter5829975c2015-04-16 11:36:52 +02003637 signal_levels = gen6_edp_signal_levels(train_set);
Paulo Zanonif0a34242012-12-06 16:51:50 -02003638 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_SNB;
3639 } else {
Daniel Vetter5829975c2015-04-16 11:36:52 +02003640 signal_levels = gen4_signal_levels(train_set);
Paulo Zanonif0a34242012-12-06 16:51:50 -02003641 mask = DP_VOLTAGE_MASK | DP_PRE_EMPHASIS_MASK;
3642 }
3643
Vandana Kannan96fb9f92014-11-18 15:45:27 +05303644 if (mask)
3645 DRM_DEBUG_KMS("Using signal levels %08x\n", signal_levels);
3646
3647 DRM_DEBUG_KMS("Using vswing level %d\n",
3648 train_set & DP_TRAIN_VOLTAGE_SWING_MASK);
3649 DRM_DEBUG_KMS("Using pre-emphasis level %d\n",
3650 (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) >>
3651 DP_TRAIN_PRE_EMPHASIS_SHIFT);
Paulo Zanonif0a34242012-12-06 16:51:50 -02003652
Ander Conselvan de Oliveiraf4eb6922015-10-23 13:01:44 +03003653 intel_dp->DP = (intel_dp->DP & ~mask) | signal_levels;
Ander Conselvan de Oliveirab905a912015-10-23 13:01:47 +03003654
3655 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
3656 POSTING_READ(intel_dp->output_reg);
Paulo Zanonif0a34242012-12-06 16:51:50 -02003657}
3658
Ander Conselvan de Oliveira94223d02015-10-23 13:01:48 +03003659void
Ander Conselvan de Oliveirae9c176d2015-10-23 13:01:45 +03003660intel_dp_program_link_training_pattern(struct intel_dp *intel_dp,
3661 uint8_t dp_train_pat)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003662{
Paulo Zanoni174edf12012-10-26 19:05:50 -02003663 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +03003664 struct drm_i915_private *dev_priv =
3665 to_i915(intel_dig_port->base.base.dev);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003666
Ander Conselvan de Oliveiraf4eb6922015-10-23 13:01:44 +03003667 _intel_dp_set_link_train(intel_dp, &intel_dp->DP, dp_train_pat);
Paulo Zanoni47ea7542012-07-17 16:55:16 -03003668
Ander Conselvan de Oliveiraf4eb6922015-10-23 13:01:44 +03003669 I915_WRITE(intel_dp->output_reg, intel_dp->DP);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003670 POSTING_READ(intel_dp->output_reg);
Ander Conselvan de Oliveirae9c176d2015-10-23 13:01:45 +03003671}
3672
Ander Conselvan de Oliveira94223d02015-10-23 13:01:48 +03003673void intel_dp_set_idle_link_train(struct intel_dp *intel_dp)
Imre Deak3ab9c632013-05-03 12:57:41 +03003674{
3675 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3676 struct drm_device *dev = intel_dig_port->base.base.dev;
3677 struct drm_i915_private *dev_priv = dev->dev_private;
3678 enum port port = intel_dig_port->port;
3679 uint32_t val;
3680
3681 if (!HAS_DDI(dev))
3682 return;
3683
3684 val = I915_READ(DP_TP_CTL(port));
3685 val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
3686 val |= DP_TP_CTL_LINK_TRAIN_IDLE;
3687 I915_WRITE(DP_TP_CTL(port), val);
3688
3689 /*
3690 * On PORT_A we can have only eDP in SST mode. There the only reason
3691 * we need to set idle transmission mode is to work around a HW issue
3692 * where we enable the pipe while not in idle link-training mode.
3693 * In this case there is requirement to wait for a minimum number of
3694 * idle patterns to be sent.
3695 */
3696 if (port == PORT_A)
3697 return;
3698
3699 if (wait_for((I915_READ(DP_TP_STATUS(port)) & DP_TP_STATUS_IDLE_DONE),
3700 1))
3701 DRM_ERROR("Timed out waiting for DP idle patterns\n");
3702}
3703
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003704static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01003705intel_dp_link_down(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003706{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02003707 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Ville Syrjälä1612c8b2015-05-05 17:17:34 +03003708 struct intel_crtc *crtc = to_intel_crtc(intel_dig_port->base.base.crtc);
Imre Deakbc7d38a2013-05-16 14:40:36 +03003709 enum port port = intel_dig_port->port;
Paulo Zanonida63a9f2012-10-26 19:05:46 -02003710 struct drm_device *dev = intel_dig_port->base.base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003711 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +01003712 uint32_t DP = intel_dp->DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003713
Daniel Vetterbc76e3202014-05-20 22:46:50 +02003714 if (WARN_ON(HAS_DDI(dev)))
Paulo Zanonic19b0662012-10-15 15:51:41 -03003715 return;
3716
Daniel Vetter0c33d8d2012-09-06 22:15:43 +02003717 if (WARN_ON((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0))
Chris Wilson1b39d6f2010-12-06 11:20:45 +00003718 return;
3719
Zhao Yakui28c97732009-10-09 11:39:41 +08003720 DRM_DEBUG_KMS("\n");
Zhenyu Wang32f9d652009-07-24 01:00:32 +08003721
Ville Syrjälä39e5fa82015-05-05 17:17:29 +03003722 if ((IS_GEN7(dev) && port == PORT_A) ||
3723 (HAS_PCH_CPT(dev) && port != PORT_A)) {
Zhenyu Wange3421a12010-04-08 09:43:27 +08003724 DP &= ~DP_LINK_TRAIN_MASK_CPT;
Ville Syrjälä1612c8b2015-05-05 17:17:34 +03003725 DP |= DP_LINK_TRAIN_PAT_IDLE_CPT;
Zhenyu Wange3421a12010-04-08 09:43:27 +08003726 } else {
Ville Syrjäläaad3d142014-06-28 02:04:25 +03003727 if (IS_CHERRYVIEW(dev))
3728 DP &= ~DP_LINK_TRAIN_MASK_CHV;
3729 else
3730 DP &= ~DP_LINK_TRAIN_MASK;
Ville Syrjälä1612c8b2015-05-05 17:17:34 +03003731 DP |= DP_LINK_TRAIN_PAT_IDLE;
Zhenyu Wange3421a12010-04-08 09:43:27 +08003732 }
Ville Syrjälä1612c8b2015-05-05 17:17:34 +03003733 I915_WRITE(intel_dp->output_reg, DP);
Chris Wilsonfe255d02010-09-11 21:37:48 +01003734 POSTING_READ(intel_dp->output_reg);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08003735
Ville Syrjälä1612c8b2015-05-05 17:17:34 +03003736 DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
3737 I915_WRITE(intel_dp->output_reg, DP);
3738 POSTING_READ(intel_dp->output_reg);
3739
3740 /*
3741 * HW workaround for IBX, we need to move the port
3742 * to transcoder A after disabling it to allow the
3743 * matching HDMI port to be enabled on transcoder A.
3744 */
3745 if (HAS_PCH_IBX(dev) && crtc->pipe == PIPE_B && port != PORT_A) {
Ville Syrjälä0c241d52015-10-30 19:23:22 +02003746 /*
3747 * We get CPU/PCH FIFO underruns on the other pipe when
3748 * doing the workaround. Sweep them under the rug.
3749 */
3750 intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, false);
3751 intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
3752
Ville Syrjälä1612c8b2015-05-05 17:17:34 +03003753 /* always enable with pattern 1 (as per spec) */
3754 DP &= ~(DP_PIPEB_SELECT | DP_LINK_TRAIN_MASK);
3755 DP |= DP_PORT_EN | DP_LINK_TRAIN_PAT_1;
3756 I915_WRITE(intel_dp->output_reg, DP);
3757 POSTING_READ(intel_dp->output_reg);
3758
3759 DP &= ~DP_PORT_EN;
Eric Anholt5bddd172010-11-18 09:32:59 +08003760 I915_WRITE(intel_dp->output_reg, DP);
Daniel Vetter0ca09682014-11-24 16:54:11 +01003761 POSTING_READ(intel_dp->output_reg);
Ville Syrjälä0c241d52015-10-30 19:23:22 +02003762
3763 intel_wait_for_vblank_if_active(dev_priv->dev, PIPE_A);
3764 intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, true);
3765 intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
Eric Anholt5bddd172010-11-18 09:32:59 +08003766 }
3767
Keith Packardf01eca22011-09-28 16:48:10 -07003768 msleep(intel_dp->panel_power_down_delay);
Ville Syrjälä6fec7662015-11-10 16:16:17 +02003769
3770 intel_dp->DP = DP;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003771}
3772
Keith Packard26d61aa2011-07-25 20:01:09 -07003773static bool
3774intel_dp_get_dpcd(struct intel_dp *intel_dp)
Keith Packard92fd8fd2011-07-25 19:50:10 -07003775{
Rodrigo Vivia031d702013-10-03 16:15:06 -03003776 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3777 struct drm_device *dev = dig_port->base.base.dev;
3778 struct drm_i915_private *dev_priv = dev->dev_private;
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05303779 uint8_t rev;
Rodrigo Vivia031d702013-10-03 16:15:06 -03003780
Jani Nikula9d1a1032014-03-14 16:51:15 +02003781 if (intel_dp_dpcd_read_wake(&intel_dp->aux, 0x000, intel_dp->dpcd,
3782 sizeof(intel_dp->dpcd)) < 0)
Adam Jacksonedb39242012-09-18 10:58:49 -04003783 return false; /* aux transfer failed */
Keith Packard92fd8fd2011-07-25 19:50:10 -07003784
Andy Shevchenkoa8e98152014-09-01 14:12:01 +03003785 DRM_DEBUG_KMS("DPCD: %*ph\n", (int) sizeof(intel_dp->dpcd), intel_dp->dpcd);
Damien Lespiau577c7a52012-12-13 16:09:02 +00003786
Adam Jacksonedb39242012-09-18 10:58:49 -04003787 if (intel_dp->dpcd[DP_DPCD_REV] == 0)
3788 return false; /* DPCD not present */
3789
Shobhit Kumar2293bb52013-07-11 18:44:56 -03003790 /* Check if the panel supports PSR */
3791 memset(intel_dp->psr_dpcd, 0, sizeof(intel_dp->psr_dpcd));
Jani Nikula50003932013-09-20 16:42:17 +03003792 if (is_edp(intel_dp)) {
Jani Nikula9d1a1032014-03-14 16:51:15 +02003793 intel_dp_dpcd_read_wake(&intel_dp->aux, DP_PSR_SUPPORT,
3794 intel_dp->psr_dpcd,
3795 sizeof(intel_dp->psr_dpcd));
Rodrigo Vivia031d702013-10-03 16:15:06 -03003796 if (intel_dp->psr_dpcd[0] & DP_PSR_IS_SUPPORTED) {
3797 dev_priv->psr.sink_support = true;
Jani Nikula50003932013-09-20 16:42:17 +03003798 DRM_DEBUG_KMS("Detected EDP PSR Panel.\n");
Rodrigo Vivia031d702013-10-03 16:15:06 -03003799 }
Sonika Jindal474d1ec2015-04-02 11:02:44 +05303800
3801 if (INTEL_INFO(dev)->gen >= 9 &&
3802 (intel_dp->psr_dpcd[0] & DP_PSR2_IS_SUPPORTED)) {
3803 uint8_t frame_sync_cap;
3804
3805 dev_priv->psr.sink_support = true;
3806 intel_dp_dpcd_read_wake(&intel_dp->aux,
3807 DP_SINK_DEVICE_AUX_FRAME_SYNC_CAP,
3808 &frame_sync_cap, 1);
3809 dev_priv->psr.aux_frame_sync = frame_sync_cap ? true : false;
3810 /* PSR2 needs frame sync as well */
3811 dev_priv->psr.psr2_support = dev_priv->psr.aux_frame_sync;
3812 DRM_DEBUG_KMS("PSR2 %s on sink",
3813 dev_priv->psr.psr2_support ? "supported" : "not supported");
3814 }
Jani Nikula50003932013-09-20 16:42:17 +03003815 }
3816
Jani Nikulabc5133d2015-09-03 11:16:07 +03003817 DRM_DEBUG_KMS("Display Port TPS3 support: source %s, sink %s\n",
Ander Conselvan de Oliveirae588fa12015-10-23 13:01:50 +03003818 yesno(intel_dp_source_supports_hbr2(intel_dp)),
Jani Nikula742f4912015-09-03 11:16:09 +03003819 yesno(drm_dp_tps3_supported(intel_dp->dpcd)));
Todd Previte06ea66b2014-01-20 10:19:39 -07003820
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05303821 /* Intermediate frequency support */
3822 if (is_edp(intel_dp) &&
3823 (intel_dp->dpcd[DP_EDP_CONFIGURATION_CAP] & DP_DPCD_DISPLAY_CONTROL_CAPABLE) &&
3824 (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_EDP_DPCD_REV, &rev, 1) == 1) &&
3825 (rev >= 0x03)) { /* eDp v1.4 or higher */
Ville Syrjälä94ca7192015-03-13 19:40:31 +02003826 __le16 sink_rates[DP_MAX_SUPPORTED_RATES];
Ville Syrjäläea2d8a42015-03-12 17:10:28 +02003827 int i;
3828
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05303829 intel_dp_dpcd_read_wake(&intel_dp->aux,
3830 DP_SUPPORTED_LINK_RATES,
Ville Syrjälä94ca7192015-03-13 19:40:31 +02003831 sink_rates,
3832 sizeof(sink_rates));
Ville Syrjäläea2d8a42015-03-12 17:10:28 +02003833
Ville Syrjälä94ca7192015-03-13 19:40:31 +02003834 for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
3835 int val = le16_to_cpu(sink_rates[i]);
Ville Syrjäläea2d8a42015-03-12 17:10:28 +02003836
3837 if (val == 0)
3838 break;
3839
Sonika Jindalaf77b972015-05-07 13:59:28 +05303840 /* Value read is in kHz while drm clock is saved in deca-kHz */
3841 intel_dp->sink_rates[i] = (val * 200) / 10;
Ville Syrjäläea2d8a42015-03-12 17:10:28 +02003842 }
Ville Syrjälä94ca7192015-03-13 19:40:31 +02003843 intel_dp->num_sink_rates = i;
Sonika Jindalfc0f8e22015-03-05 10:03:58 +05303844 }
Ville Syrjälä0336400e2015-03-12 17:10:39 +02003845
3846 intel_dp_print_rates(intel_dp);
3847
Adam Jacksonedb39242012-09-18 10:58:49 -04003848 if (!(intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
3849 DP_DWN_STRM_PORT_PRESENT))
3850 return true; /* native DP sink */
3851
3852 if (intel_dp->dpcd[DP_DPCD_REV] == 0x10)
3853 return true; /* no per-port downstream info */
3854
Jani Nikula9d1a1032014-03-14 16:51:15 +02003855 if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_DOWNSTREAM_PORT_0,
3856 intel_dp->downstream_ports,
3857 DP_MAX_DOWNSTREAM_PORTS) < 0)
Adam Jacksonedb39242012-09-18 10:58:49 -04003858 return false; /* downstream port status fetch failed */
3859
3860 return true;
Keith Packard92fd8fd2011-07-25 19:50:10 -07003861}
3862
Adam Jackson0d198322012-05-14 16:05:47 -04003863static void
3864intel_dp_probe_oui(struct intel_dp *intel_dp)
3865{
3866 u8 buf[3];
3867
3868 if (!(intel_dp->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
3869 return;
3870
Jani Nikula9d1a1032014-03-14 16:51:15 +02003871 if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_SINK_OUI, buf, 3) == 3)
Adam Jackson0d198322012-05-14 16:05:47 -04003872 DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
3873 buf[0], buf[1], buf[2]);
3874
Jani Nikula9d1a1032014-03-14 16:51:15 +02003875 if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_BRANCH_OUI, buf, 3) == 3)
Adam Jackson0d198322012-05-14 16:05:47 -04003876 DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
3877 buf[0], buf[1], buf[2]);
3878}
3879
Dave Airlie0e32b392014-05-02 14:02:48 +10003880static bool
3881intel_dp_probe_mst(struct intel_dp *intel_dp)
3882{
3883 u8 buf[1];
3884
Nathan Schulte7cc96132016-03-15 10:14:05 -05003885 if (!i915.enable_dp_mst)
3886 return false;
3887
Dave Airlie0e32b392014-05-02 14:02:48 +10003888 if (!intel_dp->can_mst)
3889 return false;
3890
3891 if (intel_dp->dpcd[DP_DPCD_REV] < 0x12)
3892 return false;
3893
Dave Airlie0e32b392014-05-02 14:02:48 +10003894 if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_MSTM_CAP, buf, 1)) {
3895 if (buf[0] & DP_MST_CAP) {
3896 DRM_DEBUG_KMS("Sink is MST capable\n");
3897 intel_dp->is_mst = true;
3898 } else {
3899 DRM_DEBUG_KMS("Sink is not MST capable\n");
3900 intel_dp->is_mst = false;
3901 }
3902 }
Dave Airlie0e32b392014-05-02 14:02:48 +10003903
3904 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
3905 return intel_dp->is_mst;
3906}
3907
Rodrigo Vivie5a1cab2015-07-23 16:35:48 -07003908static int intel_dp_sink_crc_stop(struct intel_dp *intel_dp)
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003909{
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003910 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
Rodrigo Vivid72f9d92015-11-05 10:50:19 -08003911 struct drm_device *dev = dig_port->base.base.dev;
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003912 struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
Rodrigo Viviad9dc912014-09-16 19:18:12 -04003913 u8 buf;
Rodrigo Vivie5a1cab2015-07-23 16:35:48 -07003914 int ret = 0;
Rodrigo Vivic6297842015-11-05 10:50:20 -08003915 int count = 0;
3916 int attempts = 10;
Paulo Zanoni4373f0f2015-05-25 18:52:29 -03003917
3918 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0) {
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003919 DRM_DEBUG_KMS("Sink CRC couldn't be stopped properly\n");
Rodrigo Vivie5a1cab2015-07-23 16:35:48 -07003920 ret = -EIO;
3921 goto out;
Paulo Zanoni4373f0f2015-05-25 18:52:29 -03003922 }
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003923
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003924 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
Rodrigo Vivie5a1cab2015-07-23 16:35:48 -07003925 buf & ~DP_TEST_SINK_START) < 0) {
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003926 DRM_DEBUG_KMS("Sink CRC couldn't be stopped properly\n");
Rodrigo Vivie5a1cab2015-07-23 16:35:48 -07003927 ret = -EIO;
3928 goto out;
3929 }
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003930
Rodrigo Vivic6297842015-11-05 10:50:20 -08003931 do {
3932 intel_wait_for_vblank(dev, intel_crtc->pipe);
3933
3934 if (drm_dp_dpcd_readb(&intel_dp->aux,
3935 DP_TEST_SINK_MISC, &buf) < 0) {
3936 ret = -EIO;
3937 goto out;
3938 }
3939 count = buf & DP_TEST_COUNT_MASK;
3940 } while (--attempts && count);
3941
3942 if (attempts == 0) {
Rodrigo Vividc5a9032016-01-29 14:44:59 -08003943 DRM_DEBUG_KMS("TIMEOUT: Sink CRC counter is not zeroed after calculation is stopped\n");
Rodrigo Vivic6297842015-11-05 10:50:20 -08003944 ret = -ETIMEDOUT;
3945 }
3946
Rodrigo Vivie5a1cab2015-07-23 16:35:48 -07003947 out:
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003948 hsw_enable_ips(intel_crtc);
Rodrigo Vivie5a1cab2015-07-23 16:35:48 -07003949 return ret;
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003950}
3951
3952static int intel_dp_sink_crc_start(struct intel_dp *intel_dp)
3953{
3954 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
Rodrigo Vivid72f9d92015-11-05 10:50:19 -08003955 struct drm_device *dev = dig_port->base.base.dev;
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003956 struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
3957 u8 buf;
Rodrigo Vivie5a1cab2015-07-23 16:35:48 -07003958 int ret;
3959
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003960 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK_MISC, &buf) < 0)
3961 return -EIO;
3962
3963 if (!(buf & DP_TEST_CRC_SUPPORTED))
3964 return -ENOTTY;
3965
3966 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0)
3967 return -EIO;
3968
Rodrigo Vivi6d8175d2015-11-05 10:50:22 -08003969 if (buf & DP_TEST_SINK_START) {
3970 ret = intel_dp_sink_crc_stop(intel_dp);
3971 if (ret)
3972 return ret;
3973 }
3974
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003975 hsw_disable_ips(intel_crtc);
3976
3977 if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
3978 buf | DP_TEST_SINK_START) < 0) {
3979 hsw_enable_ips(intel_crtc);
3980 return -EIO;
Paulo Zanoni4373f0f2015-05-25 18:52:29 -03003981 }
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02003982
Rodrigo Vivid72f9d92015-11-05 10:50:19 -08003983 intel_wait_for_vblank(dev, intel_crtc->pipe);
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003984 return 0;
3985}
3986
3987int intel_dp_sink_crc(struct intel_dp *intel_dp, u8 *crc)
3988{
3989 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3990 struct drm_device *dev = dig_port->base.base.dev;
3991 struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
3992 u8 buf;
Rodrigo Vivi621d4c72015-07-23 16:35:49 -07003993 int count, ret;
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003994 int attempts = 6;
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07003995
3996 ret = intel_dp_sink_crc_start(intel_dp);
3997 if (ret)
3998 return ret;
3999
Rodrigo Viviad9dc912014-09-16 19:18:12 -04004000 do {
Rodrigo Vivi621d4c72015-07-23 16:35:49 -07004001 intel_wait_for_vblank(dev, intel_crtc->pipe);
4002
Rodrigo Vivi1dda5f92014-10-01 07:32:37 -07004003 if (drm_dp_dpcd_readb(&intel_dp->aux,
Paulo Zanoni4373f0f2015-05-25 18:52:29 -03004004 DP_TEST_SINK_MISC, &buf) < 0) {
4005 ret = -EIO;
Rodrigo Viviafe0d672015-07-23 16:35:45 -07004006 goto stop;
Paulo Zanoni4373f0f2015-05-25 18:52:29 -03004007 }
Rodrigo Vivi621d4c72015-07-23 16:35:49 -07004008 count = buf & DP_TEST_COUNT_MASK;
Rodrigo Viviaabc95d2015-07-23 16:35:50 -07004009
Rodrigo Vivi7e38eef2015-11-05 10:50:21 -08004010 } while (--attempts && count == 0);
Rodrigo Viviad9dc912014-09-16 19:18:12 -04004011
4012 if (attempts == 0) {
Rodrigo Vivi7e38eef2015-11-05 10:50:21 -08004013 DRM_ERROR("Panel is unable to calculate any CRC after 6 vblanks\n");
4014 ret = -ETIMEDOUT;
4015 goto stop;
4016 }
4017
4018 if (drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_CRC_R_CR, crc, 6) < 0) {
4019 ret = -EIO;
4020 goto stop;
Rodrigo Viviad9dc912014-09-16 19:18:12 -04004021 }
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02004022
Rodrigo Viviafe0d672015-07-23 16:35:45 -07004023stop:
Rodrigo Vivi082dcc72015-07-30 16:26:39 -07004024 intel_dp_sink_crc_stop(intel_dp);
Paulo Zanoni4373f0f2015-05-25 18:52:29 -03004025 return ret;
Rodrigo Vivid2e216d2014-01-24 13:36:17 -02004026}
4027
Jesse Barnesa60f0e32011-10-20 15:09:17 -07004028static bool
4029intel_dp_get_sink_irq(struct intel_dp *intel_dp, u8 *sink_irq_vector)
4030{
Jani Nikula9d1a1032014-03-14 16:51:15 +02004031 return intel_dp_dpcd_read_wake(&intel_dp->aux,
4032 DP_DEVICE_SERVICE_IRQ_VECTOR,
4033 sink_irq_vector, 1) == 1;
Jesse Barnesa60f0e32011-10-20 15:09:17 -07004034}
4035
Dave Airlie0e32b392014-05-02 14:02:48 +10004036static bool
4037intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *sink_irq_vector)
4038{
4039 int ret;
4040
4041 ret = intel_dp_dpcd_read_wake(&intel_dp->aux,
4042 DP_SINK_COUNT_ESI,
4043 sink_irq_vector, 14);
4044 if (ret != 14)
4045 return false;
4046
4047 return true;
4048}
4049
Todd Previtec5d5ab72015-04-15 08:38:38 -07004050static uint8_t intel_dp_autotest_link_training(struct intel_dp *intel_dp)
Jesse Barnesa60f0e32011-10-20 15:09:17 -07004051{
Todd Previtec5d5ab72015-04-15 08:38:38 -07004052 uint8_t test_result = DP_TEST_ACK;
4053 return test_result;
4054}
4055
4056static uint8_t intel_dp_autotest_video_pattern(struct intel_dp *intel_dp)
4057{
4058 uint8_t test_result = DP_TEST_NAK;
4059 return test_result;
4060}
4061
4062static uint8_t intel_dp_autotest_edid(struct intel_dp *intel_dp)
4063{
4064 uint8_t test_result = DP_TEST_NAK;
Todd Previte559be302015-05-04 07:48:20 -07004065 struct intel_connector *intel_connector = intel_dp->attached_connector;
4066 struct drm_connector *connector = &intel_connector->base;
4067
4068 if (intel_connector->detect_edid == NULL ||
Daniel Vetterac6f2e22015-05-08 16:15:41 +02004069 connector->edid_corrupt ||
Todd Previte559be302015-05-04 07:48:20 -07004070 intel_dp->aux.i2c_defer_count > 6) {
4071 /* Check EDID read for NACKs, DEFERs and corruption
4072 * (DP CTS 1.2 Core r1.1)
4073 * 4.2.2.4 : Failed EDID read, I2C_NAK
4074 * 4.2.2.5 : Failed EDID read, I2C_DEFER
4075 * 4.2.2.6 : EDID corruption detected
4076 * Use failsafe mode for all cases
4077 */
4078 if (intel_dp->aux.i2c_nack_count > 0 ||
4079 intel_dp->aux.i2c_defer_count > 0)
4080 DRM_DEBUG_KMS("EDID read had %d NACKs, %d DEFERs\n",
4081 intel_dp->aux.i2c_nack_count,
4082 intel_dp->aux.i2c_defer_count);
4083 intel_dp->compliance_test_data = INTEL_DP_RESOLUTION_FAILSAFE;
4084 } else {
Thulasimani,Sivakumarf79b468e2015-08-07 15:14:30 +05304085 struct edid *block = intel_connector->detect_edid;
4086
4087 /* We have to write the checksum
4088 * of the last block read
4089 */
4090 block += intel_connector->detect_edid->extensions;
4091
Todd Previte559be302015-05-04 07:48:20 -07004092 if (!drm_dp_dpcd_write(&intel_dp->aux,
4093 DP_TEST_EDID_CHECKSUM,
Thulasimani,Sivakumarf79b468e2015-08-07 15:14:30 +05304094 &block->checksum,
Dan Carpenter5a1cc652015-05-12 21:07:37 +03004095 1))
Todd Previte559be302015-05-04 07:48:20 -07004096 DRM_DEBUG_KMS("Failed to write EDID checksum\n");
4097
4098 test_result = DP_TEST_ACK | DP_TEST_EDID_CHECKSUM_WRITE;
4099 intel_dp->compliance_test_data = INTEL_DP_RESOLUTION_STANDARD;
4100 }
4101
4102 /* Set test active flag here so userspace doesn't interrupt things */
4103 intel_dp->compliance_test_active = 1;
4104
Todd Previtec5d5ab72015-04-15 08:38:38 -07004105 return test_result;
4106}
4107
4108static uint8_t intel_dp_autotest_phy_pattern(struct intel_dp *intel_dp)
4109{
4110 uint8_t test_result = DP_TEST_NAK;
4111 return test_result;
4112}
4113
4114static void intel_dp_handle_test_request(struct intel_dp *intel_dp)
4115{
4116 uint8_t response = DP_TEST_NAK;
4117 uint8_t rxdata = 0;
4118 int status = 0;
4119
Todd Previtec5d5ab72015-04-15 08:38:38 -07004120 status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_REQUEST, &rxdata, 1);
4121 if (status <= 0) {
4122 DRM_DEBUG_KMS("Could not read test request from sink\n");
4123 goto update_status;
4124 }
4125
4126 switch (rxdata) {
4127 case DP_TEST_LINK_TRAINING:
4128 DRM_DEBUG_KMS("LINK_TRAINING test requested\n");
4129 intel_dp->compliance_test_type = DP_TEST_LINK_TRAINING;
4130 response = intel_dp_autotest_link_training(intel_dp);
4131 break;
4132 case DP_TEST_LINK_VIDEO_PATTERN:
4133 DRM_DEBUG_KMS("TEST_PATTERN test requested\n");
4134 intel_dp->compliance_test_type = DP_TEST_LINK_VIDEO_PATTERN;
4135 response = intel_dp_autotest_video_pattern(intel_dp);
4136 break;
4137 case DP_TEST_LINK_EDID_READ:
4138 DRM_DEBUG_KMS("EDID test requested\n");
4139 intel_dp->compliance_test_type = DP_TEST_LINK_EDID_READ;
4140 response = intel_dp_autotest_edid(intel_dp);
4141 break;
4142 case DP_TEST_LINK_PHY_TEST_PATTERN:
4143 DRM_DEBUG_KMS("PHY_PATTERN test requested\n");
4144 intel_dp->compliance_test_type = DP_TEST_LINK_PHY_TEST_PATTERN;
4145 response = intel_dp_autotest_phy_pattern(intel_dp);
4146 break;
4147 default:
4148 DRM_DEBUG_KMS("Invalid test request '%02x'\n", rxdata);
4149 break;
4150 }
4151
4152update_status:
4153 status = drm_dp_dpcd_write(&intel_dp->aux,
4154 DP_TEST_RESPONSE,
4155 &response, 1);
4156 if (status <= 0)
4157 DRM_DEBUG_KMS("Could not write test response to sink\n");
Jesse Barnesa60f0e32011-10-20 15:09:17 -07004158}
4159
Dave Airlie0e32b392014-05-02 14:02:48 +10004160static int
4161intel_dp_check_mst_status(struct intel_dp *intel_dp)
4162{
4163 bool bret;
4164
4165 if (intel_dp->is_mst) {
4166 u8 esi[16] = { 0 };
4167 int ret = 0;
4168 int retry;
4169 bool handled;
4170 bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
4171go_again:
4172 if (bret == true) {
4173
4174 /* check link status - esi[10] = 0x200c */
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +03004175 if (intel_dp->active_mst_links &&
Ville Syrjälä901c2da2015-08-17 18:05:12 +03004176 !drm_dp_channel_eq_ok(&esi[10], intel_dp->lane_count)) {
Dave Airlie0e32b392014-05-02 14:02:48 +10004177 DRM_DEBUG_KMS("channel EQ not ok, retraining\n");
4178 intel_dp_start_link_train(intel_dp);
Dave Airlie0e32b392014-05-02 14:02:48 +10004179 intel_dp_stop_link_train(intel_dp);
4180 }
4181
Andy Shevchenko6f34cc32015-01-15 13:45:09 +02004182 DRM_DEBUG_KMS("got esi %3ph\n", esi);
Dave Airlie0e32b392014-05-02 14:02:48 +10004183 ret = drm_dp_mst_hpd_irq(&intel_dp->mst_mgr, esi, &handled);
4184
4185 if (handled) {
4186 for (retry = 0; retry < 3; retry++) {
4187 int wret;
4188 wret = drm_dp_dpcd_write(&intel_dp->aux,
4189 DP_SINK_COUNT_ESI+1,
4190 &esi[1], 3);
4191 if (wret == 3) {
4192 break;
4193 }
4194 }
4195
4196 bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
4197 if (bret == true) {
Andy Shevchenko6f34cc32015-01-15 13:45:09 +02004198 DRM_DEBUG_KMS("got esi2 %3ph\n", esi);
Dave Airlie0e32b392014-05-02 14:02:48 +10004199 goto go_again;
4200 }
4201 } else
4202 ret = 0;
4203
4204 return ret;
4205 } else {
4206 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4207 DRM_DEBUG_KMS("failed to get ESI - device may have failed\n");
4208 intel_dp->is_mst = false;
4209 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
4210 /* send a hotplug event */
4211 drm_kms_helper_hotplug_event(intel_dig_port->base.base.dev);
4212 }
4213 }
4214 return -EINVAL;
4215}
4216
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004217/*
4218 * According to DP spec
4219 * 5.1.2:
4220 * 1. Read DPCD
4221 * 2. Configure link according to Receiver Capabilities
4222 * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
4223 * 4. Check link status on receipt of hot-plug interrupt
4224 */
Damien Lespiaua5146202015-02-10 19:32:22 +00004225static void
Chris Wilsonea5b2132010-08-04 13:50:23 +01004226intel_dp_check_link_status(struct intel_dp *intel_dp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004227{
Dave Airlie5b215bc2014-08-05 10:40:20 +10004228 struct drm_device *dev = intel_dp_to_dev(intel_dp);
Paulo Zanonida63a9f2012-10-26 19:05:46 -02004229 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
Jesse Barnesa60f0e32011-10-20 15:09:17 -07004230 u8 sink_irq_vector;
Keith Packard93f62da2011-11-01 19:45:03 -07004231 u8 link_status[DP_LINK_STATUS_SIZE];
Jesse Barnesa60f0e32011-10-20 15:09:17 -07004232
Dave Airlie5b215bc2014-08-05 10:40:20 +10004233 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
4234
Shubhangi Shrivastava4df69602015-10-28 15:30:36 +05304235 /*
4236 * Clearing compliance test variables to allow capturing
4237 * of values for next automated test request.
4238 */
4239 intel_dp->compliance_test_active = 0;
4240 intel_dp->compliance_test_type = 0;
4241 intel_dp->compliance_test_data = 0;
4242
Maarten Lankhorste02f9a02015-08-05 12:37:08 +02004243 if (!intel_encoder->base.crtc)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004244 return;
4245
Imre Deak1a125d82014-08-18 14:42:46 +03004246 if (!to_intel_crtc(intel_encoder->base.crtc)->active)
4247 return;
4248
Keith Packard92fd8fd2011-07-25 19:50:10 -07004249 /* Try to read receiver status if the link appears to be up */
Keith Packard93f62da2011-11-01 19:45:03 -07004250 if (!intel_dp_get_link_status(intel_dp, link_status)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004251 return;
4252 }
4253
Keith Packard92fd8fd2011-07-25 19:50:10 -07004254 /* Now read the DPCD to see if it's actually running */
Keith Packard26d61aa2011-07-25 20:01:09 -07004255 if (!intel_dp_get_dpcd(intel_dp)) {
Jesse Barnes59cd09e2011-07-07 11:10:59 -07004256 return;
4257 }
4258
Jesse Barnesa60f0e32011-10-20 15:09:17 -07004259 /* Try to read the source of the interrupt */
4260 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
4261 intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
4262 /* Clear interrupt source */
Jani Nikula9d1a1032014-03-14 16:51:15 +02004263 drm_dp_dpcd_writeb(&intel_dp->aux,
4264 DP_DEVICE_SERVICE_IRQ_VECTOR,
4265 sink_irq_vector);
Jesse Barnesa60f0e32011-10-20 15:09:17 -07004266
4267 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
Todd Previte09b1eb12015-04-20 15:27:34 -07004268 DRM_DEBUG_DRIVER("Test request in short pulse not handled\n");
Jesse Barnesa60f0e32011-10-20 15:09:17 -07004269 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
4270 DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
4271 }
4272
Shubhangi Shrivastava14631e92015-10-14 14:56:49 +05304273 /* if link training is requested we should perform it always */
4274 if ((intel_dp->compliance_test_type == DP_TEST_LINK_TRAINING) ||
4275 (!drm_dp_channel_eq_ok(link_status, intel_dp->lane_count))) {
Keith Packard92fd8fd2011-07-25 19:50:10 -07004276 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
Jani Nikula8e329a02014-06-03 14:56:21 +03004277 intel_encoder->base.name);
Jesse Barnes33a34e42010-09-08 12:42:02 -07004278 intel_dp_start_link_train(intel_dp);
Imre Deak3ab9c632013-05-03 12:57:41 +03004279 intel_dp_stop_link_train(intel_dp);
Jesse Barnes33a34e42010-09-08 12:42:02 -07004280 }
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004281}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004282
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004283/* XXX this is probably wrong for multiple downstream ports */
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004284static enum drm_connector_status
Keith Packard26d61aa2011-07-25 20:01:09 -07004285intel_dp_detect_dpcd(struct intel_dp *intel_dp)
Adam Jackson71ba90002011-07-12 17:38:04 -04004286{
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004287 uint8_t *dpcd = intel_dp->dpcd;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004288 uint8_t type;
4289
4290 if (!intel_dp_get_dpcd(intel_dp))
4291 return connector_status_disconnected;
4292
4293 /* if there's no downstream port, we're done */
4294 if (!(dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT))
Keith Packard26d61aa2011-07-25 20:01:09 -07004295 return connector_status_connected;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004296
4297 /* If we're HPD-aware, SINK_COUNT changes dynamically */
Jani Nikulac9ff1602013-09-27 14:48:42 +03004298 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
4299 intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
Adam Jackson23235172012-09-20 16:42:45 -04004300 uint8_t reg;
Jani Nikula9d1a1032014-03-14 16:51:15 +02004301
4302 if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_SINK_COUNT,
4303 &reg, 1) < 0)
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004304 return connector_status_unknown;
Jani Nikula9d1a1032014-03-14 16:51:15 +02004305
Adam Jackson23235172012-09-20 16:42:45 -04004306 return DP_GET_SINK_COUNT(reg) ? connector_status_connected
4307 : connector_status_disconnected;
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004308 }
4309
4310 /* If no HPD, poke DDC gently */
Jani Nikula0b998362014-03-14 16:51:17 +02004311 if (drm_probe_ddc(&intel_dp->aux.ddc))
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004312 return connector_status_connected;
4313
4314 /* Well we tried, say unknown for unreliable port types */
Jani Nikulac9ff1602013-09-27 14:48:42 +03004315 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
4316 type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
4317 if (type == DP_DS_PORT_TYPE_VGA ||
4318 type == DP_DS_PORT_TYPE_NON_EDID)
4319 return connector_status_unknown;
4320 } else {
4321 type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
4322 DP_DWN_STRM_PORT_TYPE_MASK;
4323 if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
4324 type == DP_DWN_STRM_PORT_TYPE_OTHER)
4325 return connector_status_unknown;
4326 }
Adam Jacksoncaf9ab22012-09-18 10:58:50 -04004327
4328 /* Anything else is out of spec, warn and ignore */
4329 DRM_DEBUG_KMS("Broken DP branch device, ignoring\n");
Keith Packard26d61aa2011-07-25 20:01:09 -07004330 return connector_status_disconnected;
Adam Jackson71ba90002011-07-12 17:38:04 -04004331}
4332
4333static enum drm_connector_status
Chris Wilsond410b562014-09-02 20:03:59 +01004334edp_detect(struct intel_dp *intel_dp)
4335{
4336 struct drm_device *dev = intel_dp_to_dev(intel_dp);
4337 enum drm_connector_status status;
4338
4339 status = intel_panel_detect(dev);
4340 if (status == connector_status_unknown)
4341 status = connector_status_connected;
4342
4343 return status;
4344}
4345
Jani Nikulab93433c2015-08-20 10:47:36 +03004346static bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
4347 struct intel_digital_port *port)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004348{
Jani Nikulab93433c2015-08-20 10:47:36 +03004349 u32 bit;
Jesse Barnes01cb9ea2010-10-07 16:01:12 -07004350
Jani Nikula0df53b72015-08-20 10:47:40 +03004351 switch (port->port) {
4352 case PORT_A:
4353 return true;
4354 case PORT_B:
4355 bit = SDE_PORTB_HOTPLUG;
4356 break;
4357 case PORT_C:
4358 bit = SDE_PORTC_HOTPLUG;
4359 break;
4360 case PORT_D:
4361 bit = SDE_PORTD_HOTPLUG;
4362 break;
4363 default:
4364 MISSING_CASE(port->port);
4365 return false;
4366 }
4367
4368 return I915_READ(SDEISR) & bit;
4369}
4370
4371static bool cpt_digital_port_connected(struct drm_i915_private *dev_priv,
4372 struct intel_digital_port *port)
4373{
4374 u32 bit;
4375
4376 switch (port->port) {
4377 case PORT_A:
4378 return true;
4379 case PORT_B:
4380 bit = SDE_PORTB_HOTPLUG_CPT;
4381 break;
4382 case PORT_C:
4383 bit = SDE_PORTC_HOTPLUG_CPT;
4384 break;
4385 case PORT_D:
4386 bit = SDE_PORTD_HOTPLUG_CPT;
4387 break;
Jani Nikulaa78695d2015-09-18 15:54:50 +03004388 case PORT_E:
4389 bit = SDE_PORTE_HOTPLUG_SPT;
4390 break;
Jani Nikula0df53b72015-08-20 10:47:40 +03004391 default:
4392 MISSING_CASE(port->port);
4393 return false;
Jani Nikulab93433c2015-08-20 10:47:36 +03004394 }
Damien Lespiau1b469632012-12-13 16:09:01 +00004395
Jani Nikulab93433c2015-08-20 10:47:36 +03004396 return I915_READ(SDEISR) & bit;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004397}
4398
Jani Nikula7e66bcf2015-08-20 10:47:39 +03004399static bool g4x_digital_port_connected(struct drm_i915_private *dev_priv,
Jani Nikula1d245982015-08-20 10:47:37 +03004400 struct intel_digital_port *port)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004401{
Jani Nikula9642c812015-08-20 10:47:41 +03004402 u32 bit;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004403
Jani Nikula9642c812015-08-20 10:47:41 +03004404 switch (port->port) {
4405 case PORT_B:
4406 bit = PORTB_HOTPLUG_LIVE_STATUS_G4X;
4407 break;
4408 case PORT_C:
4409 bit = PORTC_HOTPLUG_LIVE_STATUS_G4X;
4410 break;
4411 case PORT_D:
4412 bit = PORTD_HOTPLUG_LIVE_STATUS_G4X;
4413 break;
4414 default:
4415 MISSING_CASE(port->port);
4416 return false;
4417 }
4418
4419 return I915_READ(PORT_HOTPLUG_STAT) & bit;
4420}
4421
Ville Syrjälä0780cd32016-02-10 19:59:05 +02004422static bool gm45_digital_port_connected(struct drm_i915_private *dev_priv,
4423 struct intel_digital_port *port)
Jani Nikula9642c812015-08-20 10:47:41 +03004424{
4425 u32 bit;
4426
4427 switch (port->port) {
4428 case PORT_B:
Ville Syrjälä0780cd32016-02-10 19:59:05 +02004429 bit = PORTB_HOTPLUG_LIVE_STATUS_GM45;
Jani Nikula9642c812015-08-20 10:47:41 +03004430 break;
4431 case PORT_C:
Ville Syrjälä0780cd32016-02-10 19:59:05 +02004432 bit = PORTC_HOTPLUG_LIVE_STATUS_GM45;
Jani Nikula9642c812015-08-20 10:47:41 +03004433 break;
4434 case PORT_D:
Ville Syrjälä0780cd32016-02-10 19:59:05 +02004435 bit = PORTD_HOTPLUG_LIVE_STATUS_GM45;
Jani Nikula9642c812015-08-20 10:47:41 +03004436 break;
4437 default:
4438 MISSING_CASE(port->port);
4439 return false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004440 }
4441
Jani Nikula1d245982015-08-20 10:47:37 +03004442 return I915_READ(PORT_HOTPLUG_STAT) & bit;
Dave Airlie2a592be2014-09-01 16:58:12 +10004443}
4444
Jani Nikulae464bfd2015-08-20 10:47:42 +03004445static bool bxt_digital_port_connected(struct drm_i915_private *dev_priv,
Sonika Jindale2ec35a2015-09-11 16:58:32 +05304446 struct intel_digital_port *intel_dig_port)
Jani Nikulae464bfd2015-08-20 10:47:42 +03004447{
Sonika Jindale2ec35a2015-09-11 16:58:32 +05304448 struct intel_encoder *intel_encoder = &intel_dig_port->base;
4449 enum port port;
Jani Nikulae464bfd2015-08-20 10:47:42 +03004450 u32 bit;
4451
Sonika Jindale2ec35a2015-09-11 16:58:32 +05304452 intel_hpd_pin_to_port(intel_encoder->hpd_pin, &port);
4453 switch (port) {
Jani Nikulae464bfd2015-08-20 10:47:42 +03004454 case PORT_A:
4455 bit = BXT_DE_PORT_HP_DDIA;
4456 break;
4457 case PORT_B:
4458 bit = BXT_DE_PORT_HP_DDIB;
4459 break;
4460 case PORT_C:
4461 bit = BXT_DE_PORT_HP_DDIC;
4462 break;
4463 default:
Sonika Jindale2ec35a2015-09-11 16:58:32 +05304464 MISSING_CASE(port);
Jani Nikulae464bfd2015-08-20 10:47:42 +03004465 return false;
4466 }
4467
4468 return I915_READ(GEN8_DE_PORT_ISR) & bit;
4469}
4470
Jani Nikula7e66bcf2015-08-20 10:47:39 +03004471/*
4472 * intel_digital_port_connected - is the specified port connected?
4473 * @dev_priv: i915 private structure
4474 * @port: the port to test
4475 *
4476 * Return %true if @port is connected, %false otherwise.
4477 */
Sonika Jindal237ed862015-09-15 09:44:20 +05304478bool intel_digital_port_connected(struct drm_i915_private *dev_priv,
Jani Nikula7e66bcf2015-08-20 10:47:39 +03004479 struct intel_digital_port *port)
4480{
Jani Nikula0df53b72015-08-20 10:47:40 +03004481 if (HAS_PCH_IBX(dev_priv))
Jani Nikula7e66bcf2015-08-20 10:47:39 +03004482 return ibx_digital_port_connected(dev_priv, port);
Ville Syrjälä22824fa2016-02-11 16:44:28 +02004483 else if (HAS_PCH_SPLIT(dev_priv))
Jani Nikula0df53b72015-08-20 10:47:40 +03004484 return cpt_digital_port_connected(dev_priv, port);
Jani Nikulae464bfd2015-08-20 10:47:42 +03004485 else if (IS_BROXTON(dev_priv))
4486 return bxt_digital_port_connected(dev_priv, port);
Ville Syrjälä0780cd32016-02-10 19:59:05 +02004487 else if (IS_GM45(dev_priv))
4488 return gm45_digital_port_connected(dev_priv, port);
Jani Nikula7e66bcf2015-08-20 10:47:39 +03004489 else
4490 return g4x_digital_port_connected(dev_priv, port);
4491}
4492
Keith Packard8c241fe2011-09-28 16:38:44 -07004493static struct edid *
Chris Wilsonbeb60602014-09-02 20:04:00 +01004494intel_dp_get_edid(struct intel_dp *intel_dp)
Keith Packard8c241fe2011-09-28 16:38:44 -07004495{
Chris Wilsonbeb60602014-09-02 20:04:00 +01004496 struct intel_connector *intel_connector = intel_dp->attached_connector;
Keith Packard8c241fe2011-09-28 16:38:44 -07004497
Jani Nikula9cd300e2012-10-19 14:51:52 +03004498 /* use cached edid if we have one */
4499 if (intel_connector->edid) {
Jani Nikula9cd300e2012-10-19 14:51:52 +03004500 /* invalid edid */
4501 if (IS_ERR(intel_connector->edid))
Jesse Barnesd6f24d02012-06-14 15:28:33 -04004502 return NULL;
4503
Jani Nikula55e9ede2013-10-01 10:38:54 +03004504 return drm_edid_duplicate(intel_connector->edid);
Chris Wilsonbeb60602014-09-02 20:04:00 +01004505 } else
4506 return drm_get_edid(&intel_connector->base,
4507 &intel_dp->aux.ddc);
Keith Packard8c241fe2011-09-28 16:38:44 -07004508}
4509
Chris Wilsonbeb60602014-09-02 20:04:00 +01004510static void
4511intel_dp_set_edid(struct intel_dp *intel_dp)
Keith Packard8c241fe2011-09-28 16:38:44 -07004512{
Chris Wilsonbeb60602014-09-02 20:04:00 +01004513 struct intel_connector *intel_connector = intel_dp->attached_connector;
4514 struct edid *edid;
Keith Packard8c241fe2011-09-28 16:38:44 -07004515
Chris Wilsonbeb60602014-09-02 20:04:00 +01004516 edid = intel_dp_get_edid(intel_dp);
4517 intel_connector->detect_edid = edid;
Jani Nikula9cd300e2012-10-19 14:51:52 +03004518
Chris Wilsonbeb60602014-09-02 20:04:00 +01004519 if (intel_dp->force_audio != HDMI_AUDIO_AUTO)
4520 intel_dp->has_audio = intel_dp->force_audio == HDMI_AUDIO_ON;
4521 else
4522 intel_dp->has_audio = drm_detect_monitor_audio(edid);
4523}
Jesse Barnesd6f24d02012-06-14 15:28:33 -04004524
Chris Wilsonbeb60602014-09-02 20:04:00 +01004525static void
4526intel_dp_unset_edid(struct intel_dp *intel_dp)
4527{
4528 struct intel_connector *intel_connector = intel_dp->attached_connector;
4529
4530 kfree(intel_connector->detect_edid);
4531 intel_connector->detect_edid = NULL;
4532
4533 intel_dp->has_audio = false;
4534}
4535
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004536static enum drm_connector_status
4537intel_dp_detect(struct drm_connector *connector, bool force)
4538{
4539 struct intel_dp *intel_dp = intel_attached_dp(connector);
Paulo Zanonid63885d2012-10-26 19:05:49 -02004540 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4541 struct intel_encoder *intel_encoder = &intel_dig_port->base;
Paulo Zanonifa90ece2012-10-26 19:05:44 -02004542 struct drm_device *dev = connector->dev;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004543 enum drm_connector_status status;
Imre Deak671dedd2014-03-05 16:20:53 +02004544 enum intel_display_power_domain power_domain;
Dave Airlie0e32b392014-05-02 14:02:48 +10004545 bool ret;
Todd Previte09b1eb12015-04-20 15:27:34 -07004546 u8 sink_irq_vector;
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004547
Chris Wilson164c8592013-07-20 20:27:08 +01004548 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03004549 connector->base.id, connector->name);
Chris Wilsonbeb60602014-09-02 20:04:00 +01004550 intel_dp_unset_edid(intel_dp);
Chris Wilson164c8592013-07-20 20:27:08 +01004551
Dave Airlie0e32b392014-05-02 14:02:48 +10004552 if (intel_dp->is_mst) {
4553 /* MST devices are disconnected from a monitor POV */
4554 if (intel_encoder->type != INTEL_OUTPUT_EDP)
4555 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
Chris Wilsonbeb60602014-09-02 20:04:00 +01004556 return connector_status_disconnected;
Dave Airlie0e32b392014-05-02 14:02:48 +10004557 }
4558
Ville Syrjälä25f78f52015-11-16 15:01:04 +01004559 power_domain = intel_display_port_aux_power_domain(intel_encoder);
4560 intel_display_power_get(to_i915(dev), power_domain);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004561
Chris Wilsond410b562014-09-02 20:03:59 +01004562 /* Can't disconnect eDP, but you can close the lid... */
4563 if (is_edp(intel_dp))
4564 status = edp_detect(intel_dp);
Ander Conselvan de Oliveirac555a812015-11-18 17:19:30 +02004565 else if (intel_digital_port_connected(to_i915(dev),
4566 dp_to_dig_port(intel_dp)))
4567 status = intel_dp_detect_dpcd(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004568 else
Ander Conselvan de Oliveirac555a812015-11-18 17:19:30 +02004569 status = connector_status_disconnected;
4570
Shubhangi Shrivastava4df69602015-10-28 15:30:36 +05304571 if (status != connector_status_connected) {
4572 intel_dp->compliance_test_active = 0;
4573 intel_dp->compliance_test_type = 0;
4574 intel_dp->compliance_test_data = 0;
4575
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02004576 goto out;
Shubhangi Shrivastava4df69602015-10-28 15:30:36 +05304577 }
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004578
Adam Jackson0d198322012-05-14 16:05:47 -04004579 intel_dp_probe_oui(intel_dp);
4580
Dave Airlie0e32b392014-05-02 14:02:48 +10004581 ret = intel_dp_probe_mst(intel_dp);
4582 if (ret) {
4583 /* if we are in MST mode then this connector
4584 won't appear connected or have anything with EDID on it */
4585 if (intel_encoder->type != INTEL_OUTPUT_EDP)
4586 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
4587 status = connector_status_disconnected;
4588 goto out;
4589 }
4590
Shubhangi Shrivastava4df69602015-10-28 15:30:36 +05304591 /*
4592 * Clearing NACK and defer counts to get their exact values
4593 * while reading EDID which are required by Compliance tests
4594 * 4.2.2.4 and 4.2.2.5
4595 */
4596 intel_dp->aux.i2c_nack_count = 0;
4597 intel_dp->aux.i2c_defer_count = 0;
4598
Chris Wilsonbeb60602014-09-02 20:04:00 +01004599 intel_dp_set_edid(intel_dp);
Zhenyu Wanga9756bb2010-09-19 13:09:06 +08004600
Paulo Zanonid63885d2012-10-26 19:05:49 -02004601 if (intel_encoder->type != INTEL_OUTPUT_EDP)
4602 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02004603 status = connector_status_connected;
4604
Todd Previte09b1eb12015-04-20 15:27:34 -07004605 /* Try to read the source of the interrupt */
4606 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
4607 intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
4608 /* Clear interrupt source */
4609 drm_dp_dpcd_writeb(&intel_dp->aux,
4610 DP_DEVICE_SERVICE_IRQ_VECTOR,
4611 sink_irq_vector);
4612
4613 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
4614 intel_dp_handle_test_request(intel_dp);
4615 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
4616 DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
4617 }
4618
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02004619out:
Ville Syrjälä25f78f52015-11-16 15:01:04 +01004620 intel_display_power_put(to_i915(dev), power_domain);
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02004621 return status;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004622}
4623
Chris Wilsonbeb60602014-09-02 20:04:00 +01004624static void
4625intel_dp_force(struct drm_connector *connector)
4626{
4627 struct intel_dp *intel_dp = intel_attached_dp(connector);
4628 struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
Ville Syrjälä25f78f52015-11-16 15:01:04 +01004629 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
Chris Wilsonbeb60602014-09-02 20:04:00 +01004630 enum intel_display_power_domain power_domain;
4631
4632 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4633 connector->base.id, connector->name);
4634 intel_dp_unset_edid(intel_dp);
4635
4636 if (connector->status != connector_status_connected)
4637 return;
4638
Ville Syrjälä25f78f52015-11-16 15:01:04 +01004639 power_domain = intel_display_port_aux_power_domain(intel_encoder);
4640 intel_display_power_get(dev_priv, power_domain);
Chris Wilsonbeb60602014-09-02 20:04:00 +01004641
4642 intel_dp_set_edid(intel_dp);
4643
Ville Syrjälä25f78f52015-11-16 15:01:04 +01004644 intel_display_power_put(dev_priv, power_domain);
Chris Wilsonbeb60602014-09-02 20:04:00 +01004645
4646 if (intel_encoder->type != INTEL_OUTPUT_EDP)
4647 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
4648}
4649
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004650static int intel_dp_get_modes(struct drm_connector *connector)
4651{
Jani Nikuladd06f902012-10-19 14:51:50 +03004652 struct intel_connector *intel_connector = to_intel_connector(connector);
Chris Wilsonbeb60602014-09-02 20:04:00 +01004653 struct edid *edid;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004654
Chris Wilsonbeb60602014-09-02 20:04:00 +01004655 edid = intel_connector->detect_edid;
4656 if (edid) {
4657 int ret = intel_connector_update_modes(connector, edid);
4658 if (ret)
4659 return ret;
4660 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004661
Jani Nikulaf8779fd2012-10-19 14:51:48 +03004662 /* if eDP has no EDID, fall back to fixed mode */
Chris Wilsonbeb60602014-09-02 20:04:00 +01004663 if (is_edp(intel_attached_dp(connector)) &&
4664 intel_connector->panel.fixed_mode) {
Jani Nikulaf8779fd2012-10-19 14:51:48 +03004665 struct drm_display_mode *mode;
Chris Wilsonbeb60602014-09-02 20:04:00 +01004666
4667 mode = drm_mode_duplicate(connector->dev,
Jani Nikuladd06f902012-10-19 14:51:50 +03004668 intel_connector->panel.fixed_mode);
Jani Nikulaf8779fd2012-10-19 14:51:48 +03004669 if (mode) {
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004670 drm_mode_probed_add(connector, mode);
4671 return 1;
4672 }
4673 }
Chris Wilsonbeb60602014-09-02 20:04:00 +01004674
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004675 return 0;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004676}
4677
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004678static bool
4679intel_dp_detect_audio(struct drm_connector *connector)
4680{
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004681 bool has_audio = false;
Chris Wilsonbeb60602014-09-02 20:04:00 +01004682 struct edid *edid;
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004683
Chris Wilsonbeb60602014-09-02 20:04:00 +01004684 edid = to_intel_connector(connector)->detect_edid;
4685 if (edid)
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004686 has_audio = drm_detect_monitor_audio(edid);
Imre Deak671dedd2014-03-05 16:20:53 +02004687
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004688 return has_audio;
4689}
4690
Chris Wilsonf6849602010-09-19 09:29:33 +01004691static int
4692intel_dp_set_property(struct drm_connector *connector,
4693 struct drm_property *property,
4694 uint64_t val)
4695{
Chris Wilsone953fd72011-02-21 22:23:52 +00004696 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Yuly Novikov53b41832012-10-26 12:04:00 +03004697 struct intel_connector *intel_connector = to_intel_connector(connector);
Paulo Zanonida63a9f2012-10-26 19:05:46 -02004698 struct intel_encoder *intel_encoder = intel_attached_encoder(connector);
4699 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
Chris Wilsonf6849602010-09-19 09:29:33 +01004700 int ret;
4701
Rob Clark662595d2012-10-11 20:36:04 -05004702 ret = drm_object_property_set_value(&connector->base, property, val);
Chris Wilsonf6849602010-09-19 09:29:33 +01004703 if (ret)
4704 return ret;
4705
Chris Wilson3f43c482011-05-12 22:17:24 +01004706 if (property == dev_priv->force_audio_property) {
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004707 int i = val;
4708 bool has_audio;
4709
4710 if (i == intel_dp->force_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01004711 return 0;
4712
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004713 intel_dp->force_audio = i;
Chris Wilsonf6849602010-09-19 09:29:33 +01004714
Daniel Vetterc3e5f672012-02-23 17:14:47 +01004715 if (i == HDMI_AUDIO_AUTO)
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004716 has_audio = intel_dp_detect_audio(connector);
4717 else
Daniel Vetterc3e5f672012-02-23 17:14:47 +01004718 has_audio = (i == HDMI_AUDIO_ON);
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004719
4720 if (has_audio == intel_dp->has_audio)
Chris Wilsonf6849602010-09-19 09:29:33 +01004721 return 0;
4722
Chris Wilson1aad7ac2011-02-09 18:46:58 +00004723 intel_dp->has_audio = has_audio;
Chris Wilsonf6849602010-09-19 09:29:33 +01004724 goto done;
4725 }
4726
Chris Wilsone953fd72011-02-21 22:23:52 +00004727 if (property == dev_priv->broadcast_rgb_property) {
Daniel Vetterae4edb82013-04-22 17:07:23 +02004728 bool old_auto = intel_dp->color_range_auto;
Ville Syrjälä0f2a2a72015-07-06 15:10:00 +03004729 bool old_range = intel_dp->limited_color_range;
Daniel Vetterae4edb82013-04-22 17:07:23 +02004730
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02004731 switch (val) {
4732 case INTEL_BROADCAST_RGB_AUTO:
4733 intel_dp->color_range_auto = true;
4734 break;
4735 case INTEL_BROADCAST_RGB_FULL:
4736 intel_dp->color_range_auto = false;
Ville Syrjälä0f2a2a72015-07-06 15:10:00 +03004737 intel_dp->limited_color_range = false;
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02004738 break;
4739 case INTEL_BROADCAST_RGB_LIMITED:
4740 intel_dp->color_range_auto = false;
Ville Syrjälä0f2a2a72015-07-06 15:10:00 +03004741 intel_dp->limited_color_range = true;
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02004742 break;
4743 default:
4744 return -EINVAL;
4745 }
Daniel Vetterae4edb82013-04-22 17:07:23 +02004746
4747 if (old_auto == intel_dp->color_range_auto &&
Ville Syrjälä0f2a2a72015-07-06 15:10:00 +03004748 old_range == intel_dp->limited_color_range)
Daniel Vetterae4edb82013-04-22 17:07:23 +02004749 return 0;
4750
Chris Wilsone953fd72011-02-21 22:23:52 +00004751 goto done;
4752 }
4753
Yuly Novikov53b41832012-10-26 12:04:00 +03004754 if (is_edp(intel_dp) &&
4755 property == connector->dev->mode_config.scaling_mode_property) {
4756 if (val == DRM_MODE_SCALE_NONE) {
4757 DRM_DEBUG_KMS("no scaling not supported\n");
4758 return -EINVAL;
4759 }
4760
4761 if (intel_connector->panel.fitting_mode == val) {
4762 /* the eDP scaling property is not changed */
4763 return 0;
4764 }
4765 intel_connector->panel.fitting_mode = val;
4766
4767 goto done;
4768 }
4769
Chris Wilsonf6849602010-09-19 09:29:33 +01004770 return -EINVAL;
4771
4772done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00004773 if (intel_encoder->base.crtc)
4774 intel_crtc_restore_mode(intel_encoder->base.crtc);
Chris Wilsonf6849602010-09-19 09:29:33 +01004775
4776 return 0;
4777}
4778
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004779static void
Paulo Zanoni73845ad2013-06-12 17:27:30 -03004780intel_dp_connector_destroy(struct drm_connector *connector)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004781{
Jani Nikula1d508702012-10-19 14:51:49 +03004782 struct intel_connector *intel_connector = to_intel_connector(connector);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02004783
Chris Wilson10e972d2014-09-04 21:43:45 +01004784 kfree(intel_connector->detect_edid);
Chris Wilsonbeb60602014-09-02 20:04:00 +01004785
Jani Nikula9cd300e2012-10-19 14:51:52 +03004786 if (!IS_ERR_OR_NULL(intel_connector->edid))
4787 kfree(intel_connector->edid);
4788
Paulo Zanoniacd8db102013-06-12 17:27:23 -03004789 /* Can't call is_edp() since the encoder may have been destroyed
4790 * already. */
4791 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
Jani Nikula1d508702012-10-19 14:51:49 +03004792 intel_panel_fini(&intel_connector->panel);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02004793
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004794 drm_connector_cleanup(connector);
Zhenyu Wang55f78c42010-03-29 16:13:57 +08004795 kfree(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004796}
4797
Paulo Zanoni00c09d72012-10-26 19:05:52 -02004798void intel_dp_encoder_destroy(struct drm_encoder *encoder)
Daniel Vetter24d05922010-08-20 18:08:28 +02004799{
Paulo Zanonida63a9f2012-10-26 19:05:46 -02004800 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
4801 struct intel_dp *intel_dp = &intel_dig_port->dp;
Daniel Vetter24d05922010-08-20 18:08:28 +02004802
Dave Airlie0e32b392014-05-02 14:02:48 +10004803 intel_dp_mst_encoder_cleanup(intel_dig_port);
Keith Packardbd943152011-09-18 23:09:52 -07004804 if (is_edp(intel_dp)) {
4805 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
Ville Syrjälä951468f2014-09-04 14:55:31 +03004806 /*
4807 * vdd might still be enabled do to the delayed vdd off.
4808 * Make sure vdd is actually turned off here.
4809 */
Ville Syrjälä773538e82014-09-04 14:54:56 +03004810 pps_lock(intel_dp);
Daniel Vetter4be73782014-01-17 14:39:48 +01004811 edp_panel_vdd_off_sync(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03004812 pps_unlock(intel_dp);
4813
Clint Taylor01527b32014-07-07 13:01:46 -07004814 if (intel_dp->edp_notifier.notifier_call) {
4815 unregister_reboot_notifier(&intel_dp->edp_notifier);
4816 intel_dp->edp_notifier.notifier_call = NULL;
4817 }
Keith Packardbd943152011-09-18 23:09:52 -07004818 }
Imre Deakc8bd0e42014-12-12 17:57:38 +02004819 drm_encoder_cleanup(encoder);
Paulo Zanonida63a9f2012-10-26 19:05:46 -02004820 kfree(intel_dig_port);
Daniel Vetter24d05922010-08-20 18:08:28 +02004821}
4822
Imre Deak07f9cd02014-08-18 14:42:45 +03004823static void intel_dp_encoder_suspend(struct intel_encoder *intel_encoder)
4824{
4825 struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
4826
4827 if (!is_edp(intel_dp))
4828 return;
4829
Ville Syrjälä951468f2014-09-04 14:55:31 +03004830 /*
4831 * vdd might still be enabled do to the delayed vdd off.
4832 * Make sure vdd is actually turned off here.
4833 */
Ville Syrjäläafa4e532014-11-25 15:43:48 +02004834 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
Ville Syrjälä773538e82014-09-04 14:54:56 +03004835 pps_lock(intel_dp);
Imre Deak07f9cd02014-08-18 14:42:45 +03004836 edp_panel_vdd_off_sync(intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03004837 pps_unlock(intel_dp);
Imre Deak07f9cd02014-08-18 14:42:45 +03004838}
4839
Ville Syrjälä49e6bc52014-10-28 16:15:52 +02004840static void intel_edp_panel_vdd_sanitize(struct intel_dp *intel_dp)
4841{
4842 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4843 struct drm_device *dev = intel_dig_port->base.base.dev;
4844 struct drm_i915_private *dev_priv = dev->dev_private;
4845 enum intel_display_power_domain power_domain;
4846
4847 lockdep_assert_held(&dev_priv->pps_mutex);
4848
4849 if (!edp_have_panel_vdd(intel_dp))
4850 return;
4851
4852 /*
4853 * The VDD bit needs a power domain reference, so if the bit is
4854 * already enabled when we boot or resume, grab this reference and
4855 * schedule a vdd off, so we don't hold on to the reference
4856 * indefinitely.
4857 */
4858 DRM_DEBUG_KMS("VDD left on by BIOS, adjusting state tracking\n");
Ville Syrjälä25f78f52015-11-16 15:01:04 +01004859 power_domain = intel_display_port_aux_power_domain(&intel_dig_port->base);
Ville Syrjälä49e6bc52014-10-28 16:15:52 +02004860 intel_display_power_get(dev_priv, power_domain);
4861
4862 edp_panel_vdd_schedule_off(intel_dp);
4863}
4864
Imre Deak6d93c0c2014-07-31 14:03:36 +03004865static void intel_dp_encoder_reset(struct drm_encoder *encoder)
4866{
Ville Syrjälä49e6bc52014-10-28 16:15:52 +02004867 struct intel_dp *intel_dp;
4868
4869 if (to_intel_encoder(encoder)->type != INTEL_OUTPUT_EDP)
4870 return;
4871
4872 intel_dp = enc_to_intel_dp(encoder);
4873
4874 pps_lock(intel_dp);
4875
4876 /*
4877 * Read out the current power sequencer assignment,
4878 * in case the BIOS did something with it.
4879 */
Wayne Boyer666a4532015-12-09 12:29:35 -08004880 if (IS_VALLEYVIEW(encoder->dev) || IS_CHERRYVIEW(encoder->dev))
Ville Syrjälä49e6bc52014-10-28 16:15:52 +02004881 vlv_initial_power_sequencer_setup(intel_dp);
4882
4883 intel_edp_panel_vdd_sanitize(intel_dp);
4884
4885 pps_unlock(intel_dp);
Imre Deak6d93c0c2014-07-31 14:03:36 +03004886}
4887
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004888static const struct drm_connector_funcs intel_dp_connector_funcs = {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02004889 .dpms = drm_atomic_helper_connector_dpms,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004890 .detect = intel_dp_detect,
Chris Wilsonbeb60602014-09-02 20:04:00 +01004891 .force = intel_dp_force,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004892 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilsonf6849602010-09-19 09:29:33 +01004893 .set_property = intel_dp_set_property,
Matt Roper2545e4a2015-01-22 16:51:27 -08004894 .atomic_get_property = intel_connector_atomic_get_property,
Paulo Zanoni73845ad2013-06-12 17:27:30 -03004895 .destroy = intel_dp_connector_destroy,
Matt Roperc6f95f22015-01-22 16:50:32 -08004896 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Ander Conselvan de Oliveira98969722015-03-20 16:18:06 +02004897 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004898};
4899
4900static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
4901 .get_modes = intel_dp_get_modes,
4902 .mode_valid = intel_dp_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01004903 .best_encoder = intel_best_encoder,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004904};
4905
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004906static const struct drm_encoder_funcs intel_dp_enc_funcs = {
Imre Deak6d93c0c2014-07-31 14:03:36 +03004907 .reset = intel_dp_encoder_reset,
Daniel Vetter24d05922010-08-20 18:08:28 +02004908 .destroy = intel_dp_encoder_destroy,
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004909};
4910
Daniel Vetterb2c5c182015-01-23 06:00:31 +01004911enum irqreturn
Dave Airlie13cf5502014-06-18 11:29:35 +10004912intel_dp_hpd_pulse(struct intel_digital_port *intel_dig_port, bool long_hpd)
4913{
4914 struct intel_dp *intel_dp = &intel_dig_port->dp;
Imre Deak1c767b32014-08-18 14:42:42 +03004915 struct intel_encoder *intel_encoder = &intel_dig_port->base;
Dave Airlie0e32b392014-05-02 14:02:48 +10004916 struct drm_device *dev = intel_dig_port->base.base.dev;
4917 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak1c767b32014-08-18 14:42:42 +03004918 enum intel_display_power_domain power_domain;
Daniel Vetterb2c5c182015-01-23 06:00:31 +01004919 enum irqreturn ret = IRQ_NONE;
Imre Deak1c767b32014-08-18 14:42:42 +03004920
Takashi Iwai25400582015-11-19 12:09:56 +01004921 if (intel_dig_port->base.type != INTEL_OUTPUT_EDP &&
4922 intel_dig_port->base.type != INTEL_OUTPUT_HDMI)
Dave Airlie0e32b392014-05-02 14:02:48 +10004923 intel_dig_port->base.type = INTEL_OUTPUT_DISPLAYPORT;
Dave Airlie13cf5502014-06-18 11:29:35 +10004924
Ville Syrjälä7a7f84c2014-10-16 20:46:10 +03004925 if (long_hpd && intel_dig_port->base.type == INTEL_OUTPUT_EDP) {
4926 /*
4927 * vdd off can generate a long pulse on eDP which
4928 * would require vdd on to handle it, and thus we
4929 * would end up in an endless cycle of
4930 * "vdd off -> long hpd -> vdd on -> detect -> vdd off -> ..."
4931 */
4932 DRM_DEBUG_KMS("ignoring long hpd on eDP port %c\n",
4933 port_name(intel_dig_port->port));
Ville Syrjäläa8b3d522015-02-10 14:11:46 +02004934 return IRQ_HANDLED;
Ville Syrjälä7a7f84c2014-10-16 20:46:10 +03004935 }
4936
Ville Syrjälä26fbb772014-08-11 18:37:37 +03004937 DRM_DEBUG_KMS("got hpd irq on port %c - %s\n",
4938 port_name(intel_dig_port->port),
Dave Airlie0e32b392014-05-02 14:02:48 +10004939 long_hpd ? "long" : "short");
Dave Airlie13cf5502014-06-18 11:29:35 +10004940
Ville Syrjälä25f78f52015-11-16 15:01:04 +01004941 power_domain = intel_display_port_aux_power_domain(intel_encoder);
Imre Deak1c767b32014-08-18 14:42:42 +03004942 intel_display_power_get(dev_priv, power_domain);
4943
Dave Airlie0e32b392014-05-02 14:02:48 +10004944 if (long_hpd) {
Mika Kahola5fa836a2015-04-29 09:17:40 +03004945 /* indicate that we need to restart link training */
4946 intel_dp->train_set_valid = false;
Dave Airlie2a592be2014-09-01 16:58:12 +10004947
Jani Nikula7e66bcf2015-08-20 10:47:39 +03004948 if (!intel_digital_port_connected(dev_priv, intel_dig_port))
4949 goto mst_fail;
Dave Airlie0e32b392014-05-02 14:02:48 +10004950
4951 if (!intel_dp_get_dpcd(intel_dp)) {
4952 goto mst_fail;
4953 }
4954
4955 intel_dp_probe_oui(intel_dp);
4956
Ville Syrjäläd14e7b62015-08-20 19:37:29 +03004957 if (!intel_dp_probe_mst(intel_dp)) {
4958 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
4959 intel_dp_check_link_status(intel_dp);
4960 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlie0e32b392014-05-02 14:02:48 +10004961 goto mst_fail;
Ville Syrjäläd14e7b62015-08-20 19:37:29 +03004962 }
Dave Airlie0e32b392014-05-02 14:02:48 +10004963 } else {
4964 if (intel_dp->is_mst) {
Imre Deak1c767b32014-08-18 14:42:42 +03004965 if (intel_dp_check_mst_status(intel_dp) == -EINVAL)
Dave Airlie0e32b392014-05-02 14:02:48 +10004966 goto mst_fail;
4967 }
4968
4969 if (!intel_dp->is_mst) {
Dave Airlie5b215bc2014-08-05 10:40:20 +10004970 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlie0e32b392014-05-02 14:02:48 +10004971 intel_dp_check_link_status(intel_dp);
Dave Airlie5b215bc2014-08-05 10:40:20 +10004972 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlie0e32b392014-05-02 14:02:48 +10004973 }
4974 }
Daniel Vetterb2c5c182015-01-23 06:00:31 +01004975
4976 ret = IRQ_HANDLED;
4977
Imre Deak1c767b32014-08-18 14:42:42 +03004978 goto put_power;
Dave Airlie0e32b392014-05-02 14:02:48 +10004979mst_fail:
4980 /* if we were in MST mode, and device is not there get out of MST mode */
4981 if (intel_dp->is_mst) {
4982 DRM_DEBUG_KMS("MST device may have disappeared %d vs %d\n", intel_dp->is_mst, intel_dp->mst_mgr.mst_state);
4983 intel_dp->is_mst = false;
4984 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
4985 }
Imre Deak1c767b32014-08-18 14:42:42 +03004986put_power:
4987 intel_display_power_put(dev_priv, power_domain);
4988
4989 return ret;
Dave Airlie13cf5502014-06-18 11:29:35 +10004990}
4991
Rodrigo Vivi477ec322015-08-06 15:51:39 +08004992/* check the VBT to see whether the eDP is on another port */
Ville Syrjälä5d8a7752013-11-01 18:22:39 +02004993bool intel_dp_is_edp(struct drm_device *dev, enum port port)
Zhao Yakui36e83a12010-06-12 14:32:21 +08004994{
4995 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni768f69c2013-09-11 18:02:47 -03004996 union child_device_config *p_child;
Zhao Yakui36e83a12010-06-12 14:32:21 +08004997 int i;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +02004998 static const short port_mapping[] = {
Rodrigo Vivi477ec322015-08-06 15:51:39 +08004999 [PORT_B] = DVO_PORT_DPB,
5000 [PORT_C] = DVO_PORT_DPC,
5001 [PORT_D] = DVO_PORT_DPD,
5002 [PORT_E] = DVO_PORT_DPE,
Ville Syrjälä5d8a7752013-11-01 18:22:39 +02005003 };
Zhao Yakui36e83a12010-06-12 14:32:21 +08005004
Ville Syrjälä53ce81a2015-09-11 21:04:38 +03005005 /*
5006 * eDP not supported on g4x. so bail out early just
5007 * for a bit extra safety in case the VBT is bonkers.
5008 */
5009 if (INTEL_INFO(dev)->gen < 5)
5010 return false;
5011
Ville Syrjälä3b32a352013-11-01 18:22:41 +02005012 if (port == PORT_A)
5013 return true;
5014
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005015 if (!dev_priv->vbt.child_dev_num)
Zhao Yakui36e83a12010-06-12 14:32:21 +08005016 return false;
5017
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005018 for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
5019 p_child = dev_priv->vbt.child_dev + i;
Zhao Yakui36e83a12010-06-12 14:32:21 +08005020
Ville Syrjälä5d8a7752013-11-01 18:22:39 +02005021 if (p_child->common.dvo_port == port_mapping[port] &&
Ville Syrjäläf02586d2013-11-01 20:32:08 +02005022 (p_child->common.device_type & DEVICE_TYPE_eDP_BITS) ==
5023 (DEVICE_TYPE_eDP & DEVICE_TYPE_eDP_BITS))
Zhao Yakui36e83a12010-06-12 14:32:21 +08005024 return true;
5025 }
5026 return false;
5027}
5028
Dave Airlie0e32b392014-05-02 14:02:48 +10005029void
Chris Wilsonf6849602010-09-19 09:29:33 +01005030intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
5031{
Yuly Novikov53b41832012-10-26 12:04:00 +03005032 struct intel_connector *intel_connector = to_intel_connector(connector);
5033
Chris Wilson3f43c482011-05-12 22:17:24 +01005034 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +00005035 intel_attach_broadcast_rgb_property(connector);
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02005036 intel_dp->color_range_auto = true;
Yuly Novikov53b41832012-10-26 12:04:00 +03005037
5038 if (is_edp(intel_dp)) {
5039 drm_mode_create_scaling_mode_property(connector->dev);
Rob Clark6de6d842012-10-11 20:36:04 -05005040 drm_object_attach_property(
5041 &connector->base,
Yuly Novikov53b41832012-10-26 12:04:00 +03005042 connector->dev->mode_config.scaling_mode_property,
Yuly Novikov8e740cd2012-10-26 12:04:01 +03005043 DRM_MODE_SCALE_ASPECT);
5044 intel_connector->panel.fitting_mode = DRM_MODE_SCALE_ASPECT;
Yuly Novikov53b41832012-10-26 12:04:00 +03005045 }
Chris Wilsonf6849602010-09-19 09:29:33 +01005046}
5047
Imre Deakdada1a92014-01-29 13:25:41 +02005048static void intel_dp_init_panel_power_timestamps(struct intel_dp *intel_dp)
5049{
Abhay Kumard28d4732016-01-22 17:39:04 -08005050 intel_dp->panel_power_off_time = ktime_get_boottime();
Imre Deakdada1a92014-01-29 13:25:41 +02005051 intel_dp->last_power_on = jiffies;
5052 intel_dp->last_backlight_off = jiffies;
5053}
5054
Daniel Vetter67a54562012-10-20 20:57:45 +02005055static void
5056intel_dp_init_panel_power_sequencer(struct drm_device *dev,
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005057 struct intel_dp *intel_dp)
Daniel Vetter67a54562012-10-20 20:57:45 +02005058{
5059 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005060 struct edp_power_seq cur, vbt, spec,
5061 *final = &intel_dp->pps_delays;
Vandana Kannanb0a08be2015-06-18 11:00:55 +05305062 u32 pp_on, pp_off, pp_div = 0, pp_ctl = 0;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005063 i915_reg_t pp_ctrl_reg, pp_on_reg, pp_off_reg, pp_div_reg;
Jesse Barnes453c5422013-03-28 09:55:41 -07005064
Ville Syrjäläe39b9992014-09-04 14:53:14 +03005065 lockdep_assert_held(&dev_priv->pps_mutex);
5066
Ville Syrjälä81ddbc62014-10-16 21:27:31 +03005067 /* already initialized? */
5068 if (final->t11_t12 != 0)
5069 return;
5070
Vandana Kannanb0a08be2015-06-18 11:00:55 +05305071 if (IS_BROXTON(dev)) {
5072 /*
5073 * TODO: BXT has 2 sets of PPS registers.
5074 * Correct Register for Broxton need to be identified
5075 * using VBT. hardcoding for now
5076 */
5077 pp_ctrl_reg = BXT_PP_CONTROL(0);
5078 pp_on_reg = BXT_PP_ON_DELAYS(0);
5079 pp_off_reg = BXT_PP_OFF_DELAYS(0);
5080 } else if (HAS_PCH_SPLIT(dev)) {
Jani Nikulabf13e812013-09-06 07:40:05 +03005081 pp_ctrl_reg = PCH_PP_CONTROL;
Jesse Barnes453c5422013-03-28 09:55:41 -07005082 pp_on_reg = PCH_PP_ON_DELAYS;
5083 pp_off_reg = PCH_PP_OFF_DELAYS;
5084 pp_div_reg = PCH_PP_DIVISOR;
5085 } else {
Jani Nikulabf13e812013-09-06 07:40:05 +03005086 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
5087
5088 pp_ctrl_reg = VLV_PIPE_PP_CONTROL(pipe);
5089 pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
5090 pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
5091 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
Jesse Barnes453c5422013-03-28 09:55:41 -07005092 }
Daniel Vetter67a54562012-10-20 20:57:45 +02005093
5094 /* Workaround: Need to write PP_CONTROL with the unlock key as
5095 * the very first thing. */
Vandana Kannanb0a08be2015-06-18 11:00:55 +05305096 pp_ctl = ironlake_get_pp_control(intel_dp);
Daniel Vetter67a54562012-10-20 20:57:45 +02005097
Jesse Barnes453c5422013-03-28 09:55:41 -07005098 pp_on = I915_READ(pp_on_reg);
5099 pp_off = I915_READ(pp_off_reg);
Vandana Kannanb0a08be2015-06-18 11:00:55 +05305100 if (!IS_BROXTON(dev)) {
5101 I915_WRITE(pp_ctrl_reg, pp_ctl);
5102 pp_div = I915_READ(pp_div_reg);
5103 }
Daniel Vetter67a54562012-10-20 20:57:45 +02005104
5105 /* Pull timing values out of registers */
5106 cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
5107 PANEL_POWER_UP_DELAY_SHIFT;
5108
5109 cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
5110 PANEL_LIGHT_ON_DELAY_SHIFT;
5111
5112 cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
5113 PANEL_LIGHT_OFF_DELAY_SHIFT;
5114
5115 cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
5116 PANEL_POWER_DOWN_DELAY_SHIFT;
5117
Vandana Kannanb0a08be2015-06-18 11:00:55 +05305118 if (IS_BROXTON(dev)) {
5119 u16 tmp = (pp_ctl & BXT_POWER_CYCLE_DELAY_MASK) >>
5120 BXT_POWER_CYCLE_DELAY_SHIFT;
5121 if (tmp > 0)
5122 cur.t11_t12 = (tmp - 1) * 1000;
5123 else
5124 cur.t11_t12 = 0;
5125 } else {
5126 cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
Daniel Vetter67a54562012-10-20 20:57:45 +02005127 PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
Vandana Kannanb0a08be2015-06-18 11:00:55 +05305128 }
Daniel Vetter67a54562012-10-20 20:57:45 +02005129
5130 DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
5131 cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
5132
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005133 vbt = dev_priv->vbt.edp_pps;
Daniel Vetter67a54562012-10-20 20:57:45 +02005134
5135 /* Upper limits from eDP 1.3 spec. Note that we use the clunky units of
5136 * our hw here, which are all in 100usec. */
5137 spec.t1_t3 = 210 * 10;
5138 spec.t8 = 50 * 10; /* no limit for t8, use t7 instead */
5139 spec.t9 = 50 * 10; /* no limit for t9, make it symmetric with t8 */
5140 spec.t10 = 500 * 10;
5141 /* This one is special and actually in units of 100ms, but zero
5142 * based in the hw (so we need to add 100 ms). But the sw vbt
5143 * table multiplies it with 1000 to make it in units of 100usec,
5144 * too. */
5145 spec.t11_t12 = (510 + 100) * 10;
5146
5147 DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
5148 vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
5149
5150 /* Use the max of the register settings and vbt. If both are
5151 * unset, fall back to the spec limits. */
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005152#define assign_final(field) final->field = (max(cur.field, vbt.field) == 0 ? \
Daniel Vetter67a54562012-10-20 20:57:45 +02005153 spec.field : \
5154 max(cur.field, vbt.field))
5155 assign_final(t1_t3);
5156 assign_final(t8);
5157 assign_final(t9);
5158 assign_final(t10);
5159 assign_final(t11_t12);
5160#undef assign_final
5161
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005162#define get_delay(field) (DIV_ROUND_UP(final->field, 10))
Daniel Vetter67a54562012-10-20 20:57:45 +02005163 intel_dp->panel_power_up_delay = get_delay(t1_t3);
5164 intel_dp->backlight_on_delay = get_delay(t8);
5165 intel_dp->backlight_off_delay = get_delay(t9);
5166 intel_dp->panel_power_down_delay = get_delay(t10);
5167 intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
5168#undef get_delay
5169
Jani Nikulaf30d26e2013-01-16 10:53:40 +02005170 DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
5171 intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
5172 intel_dp->panel_power_cycle_delay);
5173
5174 DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
5175 intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
Jani Nikulaf30d26e2013-01-16 10:53:40 +02005176}
5177
5178static void
5179intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005180 struct intel_dp *intel_dp)
Jani Nikulaf30d26e2013-01-16 10:53:40 +02005181{
5182 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes453c5422013-03-28 09:55:41 -07005183 u32 pp_on, pp_off, pp_div, port_sel = 0;
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02005184 int div = dev_priv->rawclk_freq / 1000;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005185 i915_reg_t pp_on_reg, pp_off_reg, pp_div_reg, pp_ctrl_reg;
Ville Syrjäläad933b52014-08-18 22:15:56 +03005186 enum port port = dp_to_dig_port(intel_dp)->port;
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005187 const struct edp_power_seq *seq = &intel_dp->pps_delays;
Jesse Barnes453c5422013-03-28 09:55:41 -07005188
Ville Syrjäläe39b9992014-09-04 14:53:14 +03005189 lockdep_assert_held(&dev_priv->pps_mutex);
Jesse Barnes453c5422013-03-28 09:55:41 -07005190
Vandana Kannanb0a08be2015-06-18 11:00:55 +05305191 if (IS_BROXTON(dev)) {
5192 /*
5193 * TODO: BXT has 2 sets of PPS registers.
5194 * Correct Register for Broxton need to be identified
5195 * using VBT. hardcoding for now
5196 */
5197 pp_ctrl_reg = BXT_PP_CONTROL(0);
5198 pp_on_reg = BXT_PP_ON_DELAYS(0);
5199 pp_off_reg = BXT_PP_OFF_DELAYS(0);
5200
5201 } else if (HAS_PCH_SPLIT(dev)) {
Jesse Barnes453c5422013-03-28 09:55:41 -07005202 pp_on_reg = PCH_PP_ON_DELAYS;
5203 pp_off_reg = PCH_PP_OFF_DELAYS;
5204 pp_div_reg = PCH_PP_DIVISOR;
5205 } else {
Jani Nikulabf13e812013-09-06 07:40:05 +03005206 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
5207
5208 pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
5209 pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
5210 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
Jesse Barnes453c5422013-03-28 09:55:41 -07005211 }
5212
Paulo Zanonib2f19d12013-12-19 14:29:44 -02005213 /*
5214 * And finally store the new values in the power sequencer. The
5215 * backlight delays are set to 1 because we do manual waits on them. For
5216 * T8, even BSpec recommends doing it. For T9, if we don't do this,
5217 * we'll end up waiting for the backlight off delay twice: once when we
5218 * do the manual sleep, and once when we disable the panel and wait for
5219 * the PP_STATUS bit to become zero.
5220 */
Jani Nikulaf30d26e2013-01-16 10:53:40 +02005221 pp_on = (seq->t1_t3 << PANEL_POWER_UP_DELAY_SHIFT) |
Paulo Zanonib2f19d12013-12-19 14:29:44 -02005222 (1 << PANEL_LIGHT_ON_DELAY_SHIFT);
5223 pp_off = (1 << PANEL_LIGHT_OFF_DELAY_SHIFT) |
Jani Nikulaf30d26e2013-01-16 10:53:40 +02005224 (seq->t10 << PANEL_POWER_DOWN_DELAY_SHIFT);
Daniel Vetter67a54562012-10-20 20:57:45 +02005225 /* Compute the divisor for the pp clock, simply match the Bspec
5226 * formula. */
Vandana Kannanb0a08be2015-06-18 11:00:55 +05305227 if (IS_BROXTON(dev)) {
5228 pp_div = I915_READ(pp_ctrl_reg);
5229 pp_div &= ~BXT_POWER_CYCLE_DELAY_MASK;
5230 pp_div |= (DIV_ROUND_UP((seq->t11_t12 + 1), 1000)
5231 << BXT_POWER_CYCLE_DELAY_SHIFT);
5232 } else {
5233 pp_div = ((100 * div)/2 - 1) << PP_REFERENCE_DIVIDER_SHIFT;
5234 pp_div |= (DIV_ROUND_UP(seq->t11_t12, 1000)
5235 << PANEL_POWER_CYCLE_DELAY_SHIFT);
5236 }
Daniel Vetter67a54562012-10-20 20:57:45 +02005237
5238 /* Haswell doesn't have any port selection bits for the panel
5239 * power sequencer any more. */
Wayne Boyer666a4532015-12-09 12:29:35 -08005240 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläad933b52014-08-18 22:15:56 +03005241 port_sel = PANEL_PORT_SELECT_VLV(port);
Imre Deakbc7d38a2013-05-16 14:40:36 +03005242 } else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
Ville Syrjäläad933b52014-08-18 22:15:56 +03005243 if (port == PORT_A)
Jani Nikulaa24c1442013-09-05 16:44:46 +03005244 port_sel = PANEL_PORT_SELECT_DPA;
Daniel Vetter67a54562012-10-20 20:57:45 +02005245 else
Jani Nikulaa24c1442013-09-05 16:44:46 +03005246 port_sel = PANEL_PORT_SELECT_DPD;
Daniel Vetter67a54562012-10-20 20:57:45 +02005247 }
5248
Jesse Barnes453c5422013-03-28 09:55:41 -07005249 pp_on |= port_sel;
5250
5251 I915_WRITE(pp_on_reg, pp_on);
5252 I915_WRITE(pp_off_reg, pp_off);
Vandana Kannanb0a08be2015-06-18 11:00:55 +05305253 if (IS_BROXTON(dev))
5254 I915_WRITE(pp_ctrl_reg, pp_div);
5255 else
5256 I915_WRITE(pp_div_reg, pp_div);
Daniel Vetter67a54562012-10-20 20:57:45 +02005257
Daniel Vetter67a54562012-10-20 20:57:45 +02005258 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 -07005259 I915_READ(pp_on_reg),
5260 I915_READ(pp_off_reg),
Vandana Kannanb0a08be2015-06-18 11:00:55 +05305261 IS_BROXTON(dev) ?
5262 (I915_READ(pp_ctrl_reg) & BXT_POWER_CYCLE_DELAY_MASK) :
Jesse Barnes453c5422013-03-28 09:55:41 -07005263 I915_READ(pp_div_reg));
Zhenyu Wange3421a12010-04-08 09:43:27 +08005264}
5265
Vandana Kannanb33a2812015-02-13 15:33:03 +05305266/**
5267 * intel_dp_set_drrs_state - program registers for RR switch to take effect
5268 * @dev: DRM device
5269 * @refresh_rate: RR to be programmed
5270 *
5271 * This function gets called when refresh rate (RR) has to be changed from
5272 * one frequency to another. Switches can be between high and low RR
5273 * supported by the panel or to any other RR based on media playback (in
5274 * this case, RR value needs to be passed from user space).
5275 *
5276 * The caller of this function needs to take a lock on dev_priv->drrs.
5277 */
Vandana Kannan96178ee2015-01-10 02:25:56 +05305278static void intel_dp_set_drrs_state(struct drm_device *dev, int refresh_rate)
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305279{
5280 struct drm_i915_private *dev_priv = dev->dev_private;
5281 struct intel_encoder *encoder;
Vandana Kannan96178ee2015-01-10 02:25:56 +05305282 struct intel_digital_port *dig_port = NULL;
5283 struct intel_dp *intel_dp = dev_priv->drrs.dp;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005284 struct intel_crtc_state *config = NULL;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305285 struct intel_crtc *intel_crtc = NULL;
Vandana Kannan96178ee2015-01-10 02:25:56 +05305286 enum drrs_refresh_rate_type index = DRRS_HIGH_RR;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305287
5288 if (refresh_rate <= 0) {
5289 DRM_DEBUG_KMS("Refresh rate should be positive non-zero.\n");
5290 return;
5291 }
5292
Vandana Kannan96178ee2015-01-10 02:25:56 +05305293 if (intel_dp == NULL) {
5294 DRM_DEBUG_KMS("DRRS not supported.\n");
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305295 return;
5296 }
5297
Daniel Vetter1fcc9d12014-07-11 10:30:10 -07005298 /*
Rodrigo Vivie4d59f62014-11-20 02:22:08 -08005299 * FIXME: This needs proper synchronization with psr state for some
5300 * platforms that cannot have PSR and DRRS enabled at the same time.
Daniel Vetter1fcc9d12014-07-11 10:30:10 -07005301 */
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305302
Vandana Kannan96178ee2015-01-10 02:25:56 +05305303 dig_port = dp_to_dig_port(intel_dp);
5304 encoder = &dig_port->base;
Ander Conselvan de Oliveira723f9aa2015-03-20 16:18:18 +02005305 intel_crtc = to_intel_crtc(encoder->base.crtc);
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305306
5307 if (!intel_crtc) {
5308 DRM_DEBUG_KMS("DRRS: intel_crtc not initialized\n");
5309 return;
5310 }
5311
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005312 config = intel_crtc->config;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305313
Vandana Kannan96178ee2015-01-10 02:25:56 +05305314 if (dev_priv->drrs.type < SEAMLESS_DRRS_SUPPORT) {
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305315 DRM_DEBUG_KMS("Only Seamless DRRS supported.\n");
5316 return;
5317 }
5318
Vandana Kannan96178ee2015-01-10 02:25:56 +05305319 if (intel_dp->attached_connector->panel.downclock_mode->vrefresh ==
5320 refresh_rate)
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305321 index = DRRS_LOW_RR;
5322
Vandana Kannan96178ee2015-01-10 02:25:56 +05305323 if (index == dev_priv->drrs.refresh_rate_type) {
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305324 DRM_DEBUG_KMS(
5325 "DRRS requested for previously set RR...ignoring\n");
5326 return;
5327 }
5328
5329 if (!intel_crtc->active) {
5330 DRM_DEBUG_KMS("eDP encoder disabled. CRTC not Active\n");
5331 return;
5332 }
5333
Durgadoss R44395bf2015-02-13 15:33:02 +05305334 if (INTEL_INFO(dev)->gen >= 8 && !IS_CHERRYVIEW(dev)) {
Vandana Kannana4c30b12015-02-13 15:33:00 +05305335 switch (index) {
5336 case DRRS_HIGH_RR:
5337 intel_dp_set_m_n(intel_crtc, M1_N1);
5338 break;
5339 case DRRS_LOW_RR:
5340 intel_dp_set_m_n(intel_crtc, M2_N2);
5341 break;
5342 case DRRS_MAX_RR:
5343 default:
5344 DRM_ERROR("Unsupported refreshrate type\n");
5345 }
5346 } else if (INTEL_INFO(dev)->gen > 6) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005347 i915_reg_t reg = PIPECONF(intel_crtc->config->cpu_transcoder);
Ville Syrjälä649636e2015-09-22 19:50:01 +03005348 u32 val;
Vandana Kannana4c30b12015-02-13 15:33:00 +05305349
Ville Syrjälä649636e2015-09-22 19:50:01 +03005350 val = I915_READ(reg);
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305351 if (index > DRRS_HIGH_RR) {
Wayne Boyer666a4532015-12-09 12:29:35 -08005352 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Vandana Kannan6fa7aec2015-02-13 15:33:01 +05305353 val |= PIPECONF_EDP_RR_MODE_SWITCH_VLV;
5354 else
5355 val |= PIPECONF_EDP_RR_MODE_SWITCH;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305356 } else {
Wayne Boyer666a4532015-12-09 12:29:35 -08005357 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Vandana Kannan6fa7aec2015-02-13 15:33:01 +05305358 val &= ~PIPECONF_EDP_RR_MODE_SWITCH_VLV;
5359 else
5360 val &= ~PIPECONF_EDP_RR_MODE_SWITCH;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305361 }
5362 I915_WRITE(reg, val);
5363 }
5364
Vandana Kannan4e9ac942015-01-22 15:14:45 +05305365 dev_priv->drrs.refresh_rate_type = index;
5366
5367 DRM_DEBUG_KMS("eDP Refresh Rate set to : %dHz\n", refresh_rate);
5368}
5369
Vandana Kannanb33a2812015-02-13 15:33:03 +05305370/**
5371 * intel_edp_drrs_enable - init drrs struct if supported
5372 * @intel_dp: DP struct
5373 *
5374 * Initializes frontbuffer_bits and drrs.dp
5375 */
Vandana Kannanc3955782015-01-22 15:17:40 +05305376void intel_edp_drrs_enable(struct intel_dp *intel_dp)
5377{
5378 struct drm_device *dev = intel_dp_to_dev(intel_dp);
5379 struct drm_i915_private *dev_priv = dev->dev_private;
5380 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
5381 struct drm_crtc *crtc = dig_port->base.base.crtc;
5382 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5383
5384 if (!intel_crtc->config->has_drrs) {
5385 DRM_DEBUG_KMS("Panel doesn't support DRRS\n");
5386 return;
5387 }
5388
5389 mutex_lock(&dev_priv->drrs.mutex);
5390 if (WARN_ON(dev_priv->drrs.dp)) {
5391 DRM_ERROR("DRRS already enabled\n");
5392 goto unlock;
5393 }
5394
5395 dev_priv->drrs.busy_frontbuffer_bits = 0;
5396
5397 dev_priv->drrs.dp = intel_dp;
5398
5399unlock:
5400 mutex_unlock(&dev_priv->drrs.mutex);
5401}
5402
Vandana Kannanb33a2812015-02-13 15:33:03 +05305403/**
5404 * intel_edp_drrs_disable - Disable DRRS
5405 * @intel_dp: DP struct
5406 *
5407 */
Vandana Kannanc3955782015-01-22 15:17:40 +05305408void intel_edp_drrs_disable(struct intel_dp *intel_dp)
5409{
5410 struct drm_device *dev = intel_dp_to_dev(intel_dp);
5411 struct drm_i915_private *dev_priv = dev->dev_private;
5412 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
5413 struct drm_crtc *crtc = dig_port->base.base.crtc;
5414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5415
5416 if (!intel_crtc->config->has_drrs)
5417 return;
5418
5419 mutex_lock(&dev_priv->drrs.mutex);
5420 if (!dev_priv->drrs.dp) {
5421 mutex_unlock(&dev_priv->drrs.mutex);
5422 return;
5423 }
5424
5425 if (dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
5426 intel_dp_set_drrs_state(dev_priv->dev,
5427 intel_dp->attached_connector->panel.
5428 fixed_mode->vrefresh);
5429
5430 dev_priv->drrs.dp = NULL;
5431 mutex_unlock(&dev_priv->drrs.mutex);
5432
5433 cancel_delayed_work_sync(&dev_priv->drrs.work);
5434}
5435
Vandana Kannan4e9ac942015-01-22 15:14:45 +05305436static void intel_edp_drrs_downclock_work(struct work_struct *work)
5437{
5438 struct drm_i915_private *dev_priv =
5439 container_of(work, typeof(*dev_priv), drrs.work.work);
5440 struct intel_dp *intel_dp;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305441
Vandana Kannan96178ee2015-01-10 02:25:56 +05305442 mutex_lock(&dev_priv->drrs.mutex);
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305443
Vandana Kannan4e9ac942015-01-22 15:14:45 +05305444 intel_dp = dev_priv->drrs.dp;
5445
5446 if (!intel_dp)
5447 goto unlock;
5448
5449 /*
5450 * The delayed work can race with an invalidate hence we need to
5451 * recheck.
5452 */
5453
5454 if (dev_priv->drrs.busy_frontbuffer_bits)
5455 goto unlock;
5456
5457 if (dev_priv->drrs.refresh_rate_type != DRRS_LOW_RR)
5458 intel_dp_set_drrs_state(dev_priv->dev,
5459 intel_dp->attached_connector->panel.
5460 downclock_mode->vrefresh);
5461
5462unlock:
Vandana Kannan96178ee2015-01-10 02:25:56 +05305463 mutex_unlock(&dev_priv->drrs.mutex);
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305464}
5465
Vandana Kannanb33a2812015-02-13 15:33:03 +05305466/**
Ramalingam C0ddfd202015-06-15 20:50:05 +05305467 * intel_edp_drrs_invalidate - Disable Idleness DRRS
Vandana Kannanb33a2812015-02-13 15:33:03 +05305468 * @dev: DRM device
5469 * @frontbuffer_bits: frontbuffer plane tracking bits
5470 *
Ramalingam C0ddfd202015-06-15 20:50:05 +05305471 * This function gets called everytime rendering on the given planes start.
5472 * Hence DRRS needs to be Upclocked, i.e. (LOW_RR -> HIGH_RR).
Vandana Kannanb33a2812015-02-13 15:33:03 +05305473 *
5474 * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
5475 */
Vandana Kannana93fad02015-01-10 02:25:59 +05305476void intel_edp_drrs_invalidate(struct drm_device *dev,
5477 unsigned frontbuffer_bits)
5478{
5479 struct drm_i915_private *dev_priv = dev->dev_private;
5480 struct drm_crtc *crtc;
5481 enum pipe pipe;
5482
Daniel Vetter9da7d692015-04-09 16:44:15 +02005483 if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
Vandana Kannana93fad02015-01-10 02:25:59 +05305484 return;
5485
Daniel Vetter88f933a2015-04-09 16:44:16 +02005486 cancel_delayed_work(&dev_priv->drrs.work);
Ramalingam C3954e732015-03-03 12:11:46 +05305487
Vandana Kannana93fad02015-01-10 02:25:59 +05305488 mutex_lock(&dev_priv->drrs.mutex);
Daniel Vetter9da7d692015-04-09 16:44:15 +02005489 if (!dev_priv->drrs.dp) {
5490 mutex_unlock(&dev_priv->drrs.mutex);
5491 return;
5492 }
5493
Vandana Kannana93fad02015-01-10 02:25:59 +05305494 crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
5495 pipe = to_intel_crtc(crtc)->pipe;
5496
Daniel Vetterc1d038c2015-06-18 10:30:25 +02005497 frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
5498 dev_priv->drrs.busy_frontbuffer_bits |= frontbuffer_bits;
5499
Ramalingam C0ddfd202015-06-15 20:50:05 +05305500 /* invalidate means busy screen hence upclock */
Daniel Vetterc1d038c2015-06-18 10:30:25 +02005501 if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
Vandana Kannana93fad02015-01-10 02:25:59 +05305502 intel_dp_set_drrs_state(dev_priv->dev,
5503 dev_priv->drrs.dp->attached_connector->panel.
5504 fixed_mode->vrefresh);
Vandana Kannana93fad02015-01-10 02:25:59 +05305505
Vandana Kannana93fad02015-01-10 02:25:59 +05305506 mutex_unlock(&dev_priv->drrs.mutex);
5507}
5508
Vandana Kannanb33a2812015-02-13 15:33:03 +05305509/**
Ramalingam C0ddfd202015-06-15 20:50:05 +05305510 * intel_edp_drrs_flush - Restart Idleness DRRS
Vandana Kannanb33a2812015-02-13 15:33:03 +05305511 * @dev: DRM device
5512 * @frontbuffer_bits: frontbuffer plane tracking bits
5513 *
Ramalingam C0ddfd202015-06-15 20:50:05 +05305514 * This function gets called every time rendering on the given planes has
5515 * completed or flip on a crtc is completed. So DRRS should be upclocked
5516 * (LOW_RR -> HIGH_RR). And also Idleness detection should be started again,
5517 * if no other planes are dirty.
Vandana Kannanb33a2812015-02-13 15:33:03 +05305518 *
5519 * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
5520 */
Vandana Kannana93fad02015-01-10 02:25:59 +05305521void intel_edp_drrs_flush(struct drm_device *dev,
5522 unsigned frontbuffer_bits)
5523{
5524 struct drm_i915_private *dev_priv = dev->dev_private;
5525 struct drm_crtc *crtc;
5526 enum pipe pipe;
5527
Daniel Vetter9da7d692015-04-09 16:44:15 +02005528 if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
Vandana Kannana93fad02015-01-10 02:25:59 +05305529 return;
5530
Daniel Vetter88f933a2015-04-09 16:44:16 +02005531 cancel_delayed_work(&dev_priv->drrs.work);
Ramalingam C3954e732015-03-03 12:11:46 +05305532
Vandana Kannana93fad02015-01-10 02:25:59 +05305533 mutex_lock(&dev_priv->drrs.mutex);
Daniel Vetter9da7d692015-04-09 16:44:15 +02005534 if (!dev_priv->drrs.dp) {
5535 mutex_unlock(&dev_priv->drrs.mutex);
5536 return;
5537 }
5538
Vandana Kannana93fad02015-01-10 02:25:59 +05305539 crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
5540 pipe = to_intel_crtc(crtc)->pipe;
Daniel Vetterc1d038c2015-06-18 10:30:25 +02005541
5542 frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
Vandana Kannana93fad02015-01-10 02:25:59 +05305543 dev_priv->drrs.busy_frontbuffer_bits &= ~frontbuffer_bits;
5544
Ramalingam C0ddfd202015-06-15 20:50:05 +05305545 /* flush means busy screen hence upclock */
Daniel Vetterc1d038c2015-06-18 10:30:25 +02005546 if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
Ramalingam C0ddfd202015-06-15 20:50:05 +05305547 intel_dp_set_drrs_state(dev_priv->dev,
5548 dev_priv->drrs.dp->attached_connector->panel.
5549 fixed_mode->vrefresh);
5550
5551 /*
5552 * flush also means no more activity hence schedule downclock, if all
5553 * other fbs are quiescent too
5554 */
5555 if (!dev_priv->drrs.busy_frontbuffer_bits)
Vandana Kannana93fad02015-01-10 02:25:59 +05305556 schedule_delayed_work(&dev_priv->drrs.work,
5557 msecs_to_jiffies(1000));
5558 mutex_unlock(&dev_priv->drrs.mutex);
5559}
5560
Vandana Kannanb33a2812015-02-13 15:33:03 +05305561/**
5562 * DOC: Display Refresh Rate Switching (DRRS)
5563 *
5564 * Display Refresh Rate Switching (DRRS) is a power conservation feature
5565 * which enables swtching between low and high refresh rates,
5566 * dynamically, based on the usage scenario. This feature is applicable
5567 * for internal panels.
5568 *
5569 * Indication that the panel supports DRRS is given by the panel EDID, which
5570 * would list multiple refresh rates for one resolution.
5571 *
5572 * DRRS is of 2 types - static and seamless.
5573 * Static DRRS involves changing refresh rate (RR) by doing a full modeset
5574 * (may appear as a blink on screen) and is used in dock-undock scenario.
5575 * Seamless DRRS involves changing RR without any visual effect to the user
5576 * and can be used during normal system usage. This is done by programming
5577 * certain registers.
5578 *
5579 * Support for static/seamless DRRS may be indicated in the VBT based on
5580 * inputs from the panel spec.
5581 *
5582 * DRRS saves power by switching to low RR based on usage scenarios.
5583 *
5584 * eDP DRRS:-
5585 * The implementation is based on frontbuffer tracking implementation.
5586 * When there is a disturbance on the screen triggered by user activity or a
5587 * periodic system activity, DRRS is disabled (RR is changed to high RR).
5588 * When there is no movement on screen, after a timeout of 1 second, a switch
5589 * to low RR is made.
5590 * For integration with frontbuffer tracking code,
5591 * intel_edp_drrs_invalidate() and intel_edp_drrs_flush() are called.
5592 *
5593 * DRRS can be further extended to support other internal panels and also
5594 * the scenario of video playback wherein RR is set based on the rate
5595 * requested by userspace.
5596 */
5597
5598/**
5599 * intel_dp_drrs_init - Init basic DRRS work and mutex.
5600 * @intel_connector: eDP connector
5601 * @fixed_mode: preferred mode of panel
5602 *
5603 * This function is called only once at driver load to initialize basic
5604 * DRRS stuff.
5605 *
5606 * Returns:
5607 * Downclock mode if panel supports it, else return NULL.
5608 * DRRS support is determined by the presence of downclock mode (apart
5609 * from VBT setting).
5610 */
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305611static struct drm_display_mode *
Vandana Kannan96178ee2015-01-10 02:25:56 +05305612intel_dp_drrs_init(struct intel_connector *intel_connector,
5613 struct drm_display_mode *fixed_mode)
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305614{
5615 struct drm_connector *connector = &intel_connector->base;
Vandana Kannan96178ee2015-01-10 02:25:56 +05305616 struct drm_device *dev = connector->dev;
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305617 struct drm_i915_private *dev_priv = dev->dev_private;
5618 struct drm_display_mode *downclock_mode = NULL;
5619
Daniel Vetter9da7d692015-04-09 16:44:15 +02005620 INIT_DELAYED_WORK(&dev_priv->drrs.work, intel_edp_drrs_downclock_work);
5621 mutex_init(&dev_priv->drrs.mutex);
5622
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305623 if (INTEL_INFO(dev)->gen <= 6) {
5624 DRM_DEBUG_KMS("DRRS supported for Gen7 and above\n");
5625 return NULL;
5626 }
5627
5628 if (dev_priv->vbt.drrs_type != SEAMLESS_DRRS_SUPPORT) {
Damien Lespiau4079b8d2014-08-05 10:39:42 +01005629 DRM_DEBUG_KMS("VBT doesn't support DRRS\n");
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305630 return NULL;
5631 }
5632
5633 downclock_mode = intel_find_panel_downclock
5634 (dev, fixed_mode, connector);
5635
5636 if (!downclock_mode) {
Ramalingam Ca1d26342015-02-23 17:38:33 +05305637 DRM_DEBUG_KMS("Downclock mode is not found. DRRS not supported\n");
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305638 return NULL;
5639 }
5640
Vandana Kannan96178ee2015-01-10 02:25:56 +05305641 dev_priv->drrs.type = dev_priv->vbt.drrs_type;
Pradeep Bhat439d7ac2014-04-05 12:13:28 +05305642
Vandana Kannan96178ee2015-01-10 02:25:56 +05305643 dev_priv->drrs.refresh_rate_type = DRRS_HIGH_RR;
Damien Lespiau4079b8d2014-08-05 10:39:42 +01005644 DRM_DEBUG_KMS("seamless DRRS supported for eDP panel.\n");
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305645 return downclock_mode;
5646}
5647
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005648static bool intel_edp_init_connector(struct intel_dp *intel_dp,
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005649 struct intel_connector *intel_connector)
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005650{
5651 struct drm_connector *connector = &intel_connector->base;
5652 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
Paulo Zanoni63635212014-04-22 19:55:42 -03005653 struct intel_encoder *intel_encoder = &intel_dig_port->base;
5654 struct drm_device *dev = intel_encoder->base.dev;
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005655 struct drm_i915_private *dev_priv = dev->dev_private;
5656 struct drm_display_mode *fixed_mode = NULL;
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305657 struct drm_display_mode *downclock_mode = NULL;
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005658 bool has_dpcd;
5659 struct drm_display_mode *scan;
5660 struct edid *edid;
Ville Syrjälä6517d272014-11-07 11:16:02 +02005661 enum pipe pipe = INVALID_PIPE;
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005662
5663 if (!is_edp(intel_dp))
5664 return true;
5665
Ville Syrjälä49e6bc52014-10-28 16:15:52 +02005666 pps_lock(intel_dp);
5667 intel_edp_panel_vdd_sanitize(intel_dp);
5668 pps_unlock(intel_dp);
Paulo Zanoni63635212014-04-22 19:55:42 -03005669
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005670 /* Cache DPCD and EDID for edp. */
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005671 has_dpcd = intel_dp_get_dpcd(intel_dp);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005672
5673 if (has_dpcd) {
5674 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
5675 dev_priv->no_aux_handshake =
5676 intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
5677 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
5678 } else {
5679 /* if this fails, presume the device is a ghost */
5680 DRM_INFO("failed to retrieve link info, disabling eDP\n");
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005681 return false;
5682 }
5683
5684 /* We now know it's not a ghost, init power sequence regs. */
Ville Syrjälä773538e82014-09-04 14:54:56 +03005685 pps_lock(intel_dp);
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005686 intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03005687 pps_unlock(intel_dp);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005688
Daniel Vetter060c8772014-03-21 23:22:35 +01005689 mutex_lock(&dev->mode_config.mutex);
Jani Nikula0b998362014-03-14 16:51:17 +02005690 edid = drm_get_edid(connector, &intel_dp->aux.ddc);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005691 if (edid) {
5692 if (drm_add_edid_modes(connector, edid)) {
5693 drm_mode_connector_update_edid_property(connector,
5694 edid);
5695 drm_edid_to_eld(connector, edid);
5696 } else {
5697 kfree(edid);
5698 edid = ERR_PTR(-EINVAL);
5699 }
5700 } else {
5701 edid = ERR_PTR(-ENOENT);
5702 }
5703 intel_connector->edid = edid;
5704
5705 /* prefer fixed mode from EDID if available */
5706 list_for_each_entry(scan, &connector->probed_modes, head) {
5707 if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
5708 fixed_mode = drm_mode_duplicate(dev, scan);
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305709 downclock_mode = intel_dp_drrs_init(
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305710 intel_connector, fixed_mode);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005711 break;
5712 }
5713 }
5714
5715 /* fallback to VBT if available for eDP */
5716 if (!fixed_mode && dev_priv->vbt.lfp_lvds_vbt_mode) {
5717 fixed_mode = drm_mode_duplicate(dev,
5718 dev_priv->vbt.lfp_lvds_vbt_mode);
5719 if (fixed_mode)
5720 fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
5721 }
Daniel Vetter060c8772014-03-21 23:22:35 +01005722 mutex_unlock(&dev->mode_config.mutex);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005723
Wayne Boyer666a4532015-12-09 12:29:35 -08005724 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Clint Taylor01527b32014-07-07 13:01:46 -07005725 intel_dp->edp_notifier.notifier_call = edp_notify_handler;
5726 register_reboot_notifier(&intel_dp->edp_notifier);
Ville Syrjälä6517d272014-11-07 11:16:02 +02005727
5728 /*
5729 * Figure out the current pipe for the initial backlight setup.
5730 * If the current pipe isn't valid, try the PPS pipe, and if that
5731 * fails just assume pipe A.
5732 */
5733 if (IS_CHERRYVIEW(dev))
5734 pipe = DP_PORT_TO_PIPE_CHV(intel_dp->DP);
5735 else
5736 pipe = PORT_TO_PIPE(intel_dp->DP);
5737
5738 if (pipe != PIPE_A && pipe != PIPE_B)
5739 pipe = intel_dp->pps_pipe;
5740
5741 if (pipe != PIPE_A && pipe != PIPE_B)
5742 pipe = PIPE_A;
5743
5744 DRM_DEBUG_KMS("using pipe %c for initial backlight setup\n",
5745 pipe_name(pipe));
Clint Taylor01527b32014-07-07 13:01:46 -07005746 }
5747
Pradeep Bhat4f9db5b2014-04-05 12:12:31 +05305748 intel_panel_init(&intel_connector->panel, fixed_mode, downclock_mode);
Jani Nikula5507fae2015-09-14 14:03:48 +03005749 intel_connector->panel.backlight.power = intel_edp_backlight_power;
Ville Syrjälä6517d272014-11-07 11:16:02 +02005750 intel_panel_setup_backlight(connector, pipe);
Paulo Zanonied92f0b2013-06-12 17:27:24 -03005751
5752 return true;
5753}
5754
Paulo Zanoni16c25532013-06-12 17:27:25 -03005755bool
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005756intel_dp_init_connector(struct intel_digital_port *intel_dig_port,
5757 struct intel_connector *intel_connector)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005758{
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005759 struct drm_connector *connector = &intel_connector->base;
5760 struct intel_dp *intel_dp = &intel_dig_port->dp;
5761 struct intel_encoder *intel_encoder = &intel_dig_port->base;
5762 struct drm_device *dev = intel_encoder->base.dev;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005763 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni174edf12012-10-26 19:05:50 -02005764 enum port port = intel_dig_port->port;
Ville Syrjäläa121f4e2015-11-11 20:34:11 +02005765 int type, ret;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005766
Ville Syrjäläccb1a832015-12-08 19:59:38 +02005767 if (WARN(intel_dig_port->max_lanes < 1,
5768 "Not enough lanes (%d) for DP on port %c\n",
5769 intel_dig_port->max_lanes, port_name(port)))
5770 return false;
5771
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03005772 intel_dp->pps_pipe = INVALID_PIPE;
5773
Damien Lespiauec5b01d2014-01-21 13:35:39 +00005774 /* intel_dp vfuncs */
Damien Lespiaub6b5e382014-01-20 16:00:59 +00005775 if (INTEL_INFO(dev)->gen >= 9)
5776 intel_dp->get_aux_clock_divider = skl_get_aux_clock_divider;
Damien Lespiauec5b01d2014-01-21 13:35:39 +00005777 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
5778 intel_dp->get_aux_clock_divider = hsw_get_aux_clock_divider;
5779 else if (HAS_PCH_SPLIT(dev))
5780 intel_dp->get_aux_clock_divider = ilk_get_aux_clock_divider;
5781 else
Ville Syrjälä6ffb1be2016-03-02 17:22:14 +02005782 intel_dp->get_aux_clock_divider = g4x_get_aux_clock_divider;
Damien Lespiauec5b01d2014-01-21 13:35:39 +00005783
Damien Lespiaub9ca5fa2014-01-20 16:01:00 +00005784 if (INTEL_INFO(dev)->gen >= 9)
5785 intel_dp->get_aux_send_ctl = skl_get_aux_send_ctl;
5786 else
Ville Syrjälä6ffb1be2016-03-02 17:22:14 +02005787 intel_dp->get_aux_send_ctl = g4x_get_aux_send_ctl;
Damien Lespiau153b1102014-01-21 13:37:15 +00005788
Ander Conselvan de Oliveiraad642172015-10-23 13:01:49 +03005789 if (HAS_DDI(dev))
5790 intel_dp->prepare_link_retrain = intel_ddi_prepare_link_retrain;
5791
Daniel Vetter07679352012-09-06 22:15:42 +02005792 /* Preserve the current hw state. */
5793 intel_dp->DP = I915_READ(intel_dp->output_reg);
Jani Nikuladd06f902012-10-19 14:51:50 +03005794 intel_dp->attached_connector = intel_connector;
Chris Wilson3d3dc142011-02-12 10:33:12 +00005795
Ville Syrjälä3b32a352013-11-01 18:22:41 +02005796 if (intel_dp_is_edp(dev, port))
Gajanan Bhat19c03922012-09-27 19:13:07 +05305797 type = DRM_MODE_CONNECTOR_eDP;
Ville Syrjälä3b32a352013-11-01 18:22:41 +02005798 else
5799 type = DRM_MODE_CONNECTOR_DisplayPort;
Adam Jacksonb3295302010-07-16 14:46:28 -04005800
Imre Deakf7d24902013-05-08 13:14:05 +03005801 /*
5802 * For eDP we always set the encoder type to INTEL_OUTPUT_EDP, but
5803 * for DP the encoder type can be set by the caller to
5804 * INTEL_OUTPUT_UNKNOWN for DDI, so don't rewrite it.
5805 */
5806 if (type == DRM_MODE_CONNECTOR_eDP)
5807 intel_encoder->type = INTEL_OUTPUT_EDP;
5808
Ville Syrjäläc17ed5b2014-10-16 21:27:27 +03005809 /* eDP only on port B and/or C on vlv/chv */
Wayne Boyer666a4532015-12-09 12:29:35 -08005810 if (WARN_ON((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
5811 is_edp(intel_dp) && port != PORT_B && port != PORT_C))
Ville Syrjäläc17ed5b2014-10-16 21:27:27 +03005812 return false;
5813
Imre Deake7281ea2013-05-08 13:14:08 +03005814 DRM_DEBUG_KMS("Adding %s connector on port %c\n",
5815 type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
5816 port_name(port));
5817
Adam Jacksonb3295302010-07-16 14:46:28 -04005818 drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005819 drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
5820
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005821 connector->interlace_allowed = true;
5822 connector->doublescan_allowed = 0;
Ma Lingf8aed702009-08-24 13:50:24 +08005823
Daniel Vetter66a92782012-07-12 20:08:18 +02005824 INIT_DELAYED_WORK(&intel_dp->panel_vdd_work,
Daniel Vetter4be73782014-01-17 14:39:48 +01005825 edp_panel_vdd_work);
Zhenyu Wang6251ec02010-01-12 05:38:32 +08005826
Chris Wilsondf0e9242010-09-09 16:20:55 +01005827 intel_connector_attach_encoder(intel_connector, intel_encoder);
Thomas Wood34ea3d32014-05-29 16:57:41 +01005828 drm_connector_register(connector);
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005829
Paulo Zanoniaffa9352012-11-23 15:30:39 -02005830 if (HAS_DDI(dev))
Paulo Zanonibcbc8892012-10-26 19:05:51 -02005831 intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
5832 else
5833 intel_connector->get_hw_state = intel_connector_get_hw_state;
Imre Deak80f65de2014-02-11 17:12:49 +02005834 intel_connector->unregister = intel_dp_connector_unregister;
Paulo Zanonibcbc8892012-10-26 19:05:51 -02005835
Jani Nikula0b998362014-03-14 16:51:17 +02005836 /* Set up the hotplug pin. */
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03005837 switch (port) {
5838 case PORT_A:
Egbert Eich1d843f92013-02-25 12:06:49 -05005839 intel_encoder->hpd_pin = HPD_PORT_A;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03005840 break;
5841 case PORT_B:
Egbert Eich1d843f92013-02-25 12:06:49 -05005842 intel_encoder->hpd_pin = HPD_PORT_B;
Jani Nikulae87a0052015-10-20 15:22:02 +03005843 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1))
Sonika Jindalcf1d5882015-08-10 10:35:36 +05305844 intel_encoder->hpd_pin = HPD_PORT_A;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03005845 break;
5846 case PORT_C:
Egbert Eich1d843f92013-02-25 12:06:49 -05005847 intel_encoder->hpd_pin = HPD_PORT_C;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03005848 break;
5849 case PORT_D:
Egbert Eich1d843f92013-02-25 12:06:49 -05005850 intel_encoder->hpd_pin = HPD_PORT_D;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03005851 break;
Xiong Zhang26951ca2015-08-17 15:55:50 +08005852 case PORT_E:
5853 intel_encoder->hpd_pin = HPD_PORT_E;
5854 break;
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03005855 default:
Damien Lespiauad1c0b12013-03-07 15:30:28 +00005856 BUG();
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005857 }
5858
Imre Deakdada1a92014-01-29 13:25:41 +02005859 if (is_edp(intel_dp)) {
Ville Syrjälä773538e82014-09-04 14:54:56 +03005860 pps_lock(intel_dp);
Ville Syrjälä1e74a322014-10-28 16:15:51 +02005861 intel_dp_init_panel_power_timestamps(intel_dp);
Wayne Boyer666a4532015-12-09 12:29:35 -08005862 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjäläa4a5d2f2014-09-04 14:54:20 +03005863 vlv_initial_power_sequencer_setup(intel_dp);
Ville Syrjälä1e74a322014-10-28 16:15:51 +02005864 else
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005865 intel_dp_init_panel_power_sequencer(dev, intel_dp);
Ville Syrjälä773538e82014-09-04 14:54:56 +03005866 pps_unlock(intel_dp);
Imre Deakdada1a92014-01-29 13:25:41 +02005867 }
Paulo Zanoni0095e6d2013-12-19 14:29:39 -02005868
Ville Syrjäläa121f4e2015-11-11 20:34:11 +02005869 ret = intel_dp_aux_init(intel_dp, intel_connector);
5870 if (ret)
5871 goto fail;
Dave Airliec1f05262012-08-30 11:06:18 +10005872
Dave Airlie0e32b392014-05-02 14:02:48 +10005873 /* init MST on ports that can support it */
Jani Nikula0c9b3712015-05-18 17:10:01 +03005874 if (HAS_DP_MST(dev) &&
5875 (port == PORT_B || port == PORT_C || port == PORT_D))
5876 intel_dp_mst_encoder_init(intel_dig_port,
5877 intel_connector->base.base.id);
Dave Airlie0e32b392014-05-02 14:02:48 +10005878
Ville Syrjälä36b5f422014-10-16 21:27:30 +03005879 if (!intel_edp_init_connector(intel_dp, intel_connector)) {
Ville Syrjäläa121f4e2015-11-11 20:34:11 +02005880 intel_dp_aux_fini(intel_dp);
5881 intel_dp_mst_encoder_cleanup(intel_dig_port);
5882 goto fail;
Paulo Zanonib2f246a2013-06-12 17:27:26 -03005883 }
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005884
Chris Wilsonf6849602010-09-19 09:29:33 +01005885 intel_dp_add_properties(intel_dp, connector);
5886
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005887 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
5888 * 0xd. Failure to do so will result in spurious interrupts being
5889 * generated on the port when a cable is not attached.
5890 */
5891 if (IS_G4X(dev) && !IS_GM45(dev)) {
5892 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
5893 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
5894 }
Paulo Zanoni16c25532013-06-12 17:27:25 -03005895
Jani Nikulaaa7471d2015-04-01 11:15:21 +03005896 i915_debugfs_connector_add(connector);
5897
Paulo Zanoni16c25532013-06-12 17:27:25 -03005898 return true;
Ville Syrjäläa121f4e2015-11-11 20:34:11 +02005899
5900fail:
5901 if (is_edp(intel_dp)) {
5902 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
5903 /*
5904 * vdd might still be enabled do to the delayed vdd off.
5905 * Make sure vdd is actually turned off here.
5906 */
5907 pps_lock(intel_dp);
5908 edp_panel_vdd_off_sync(intel_dp);
5909 pps_unlock(intel_dp);
5910 }
5911 drm_connector_unregister(connector);
5912 drm_connector_cleanup(connector);
5913
5914 return false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005915}
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005916
5917void
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005918intel_dp_init(struct drm_device *dev,
5919 i915_reg_t output_reg, enum port port)
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005920{
Dave Airlie13cf5502014-06-18 11:29:35 +10005921 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005922 struct intel_digital_port *intel_dig_port;
5923 struct intel_encoder *intel_encoder;
5924 struct drm_encoder *encoder;
5925 struct intel_connector *intel_connector;
5926
Daniel Vetterb14c5672013-09-19 12:18:32 +02005927 intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005928 if (!intel_dig_port)
5929 return;
5930
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03005931 intel_connector = intel_connector_alloc();
Sudip Mukherjee11aee0f2015-10-08 19:27:59 +05305932 if (!intel_connector)
5933 goto err_connector_alloc;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005934
5935 intel_encoder = &intel_dig_port->base;
5936 encoder = &intel_encoder->base;
5937
Sudip Mukherjee893da0c2015-10-08 19:28:00 +05305938 if (drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
Dave Airlieade1ba72015-12-23 14:22:09 +10005939 DRM_MODE_ENCODER_TMDS, NULL))
Sudip Mukherjee893da0c2015-10-08 19:28:00 +05305940 goto err_encoder_init;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005941
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01005942 intel_encoder->compute_config = intel_dp_compute_config;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02005943 intel_encoder->disable = intel_disable_dp;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02005944 intel_encoder->get_hw_state = intel_dp_get_hw_state;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07005945 intel_encoder->get_config = intel_dp_get_config;
Imre Deak07f9cd02014-08-18 14:42:45 +03005946 intel_encoder->suspend = intel_dp_encoder_suspend;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03005947 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä9197c882014-04-09 13:29:05 +03005948 intel_encoder->pre_pll_enable = chv_dp_pre_pll_enable;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03005949 intel_encoder->pre_enable = chv_pre_enable_dp;
5950 intel_encoder->enable = vlv_enable_dp;
Ville Syrjälä580d3812014-04-09 13:29:00 +03005951 intel_encoder->post_disable = chv_post_disable_dp;
Ville Syrjäläd6db9952015-07-08 23:45:49 +03005952 intel_encoder->post_pll_disable = chv_dp_post_pll_disable;
Chon Ming Leee4a1d842014-04-09 13:28:20 +03005953 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaecff4f32013-09-06 07:38:29 +03005954 intel_encoder->pre_pll_enable = vlv_dp_pre_pll_enable;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03005955 intel_encoder->pre_enable = vlv_pre_enable_dp;
5956 intel_encoder->enable = vlv_enable_dp;
Ville Syrjälä49277c32014-03-31 18:21:26 +03005957 intel_encoder->post_disable = vlv_post_disable_dp;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03005958 } else {
Jani Nikulaecff4f32013-09-06 07:38:29 +03005959 intel_encoder->pre_enable = g4x_pre_enable_dp;
5960 intel_encoder->enable = g4x_enable_dp;
Ville Syrjälä08aff3f2014-08-18 22:16:09 +03005961 if (INTEL_INFO(dev)->gen >= 5)
5962 intel_encoder->post_disable = ilk_post_disable_dp;
Jani Nikulaab1f90f2013-07-30 12:20:30 +03005963 }
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005964
Paulo Zanoni174edf12012-10-26 19:05:50 -02005965 intel_dig_port->port = port;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005966 intel_dig_port->dp.output_reg = output_reg;
Ville Syrjäläccb1a832015-12-08 19:59:38 +02005967 intel_dig_port->max_lanes = 4;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005968
Paulo Zanoni00c09d72012-10-26 19:05:52 -02005969 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
Ville Syrjälä882ec382014-04-28 14:07:43 +03005970 if (IS_CHERRYVIEW(dev)) {
5971 if (port == PORT_D)
5972 intel_encoder->crtc_mask = 1 << 2;
5973 else
5974 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
5975 } else {
5976 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
5977 }
Ville Syrjäläbc079e82014-03-03 16:15:28 +02005978 intel_encoder->cloneable = 0;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005979
Dave Airlie13cf5502014-06-18 11:29:35 +10005980 intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
Jani Nikula5fcece82015-05-27 15:03:42 +03005981 dev_priv->hotplug.irq_port[port] = intel_dig_port;
Dave Airlie13cf5502014-06-18 11:29:35 +10005982
Sudip Mukherjee11aee0f2015-10-08 19:27:59 +05305983 if (!intel_dp_init_connector(intel_dig_port, intel_connector))
5984 goto err_init_connector;
5985
5986 return;
5987
5988err_init_connector:
5989 drm_encoder_cleanup(encoder);
Sudip Mukherjee893da0c2015-10-08 19:28:00 +05305990err_encoder_init:
Sudip Mukherjee11aee0f2015-10-08 19:27:59 +05305991 kfree(intel_connector);
5992err_connector_alloc:
5993 kfree(intel_dig_port);
5994
5995 return;
Paulo Zanonif0fec3f2012-10-26 19:05:48 -02005996}
Dave Airlie0e32b392014-05-02 14:02:48 +10005997
5998void intel_dp_mst_suspend(struct drm_device *dev)
5999{
6000 struct drm_i915_private *dev_priv = dev->dev_private;
6001 int i;
6002
6003 /* disable MST */
6004 for (i = 0; i < I915_MAX_PORTS; i++) {
Jani Nikula5fcece82015-05-27 15:03:42 +03006005 struct intel_digital_port *intel_dig_port = dev_priv->hotplug.irq_port[i];
Dave Airlie0e32b392014-05-02 14:02:48 +10006006 if (!intel_dig_port)
6007 continue;
6008
6009 if (intel_dig_port->base.type == INTEL_OUTPUT_DISPLAYPORT) {
6010 if (!intel_dig_port->dp.can_mst)
6011 continue;
6012 if (intel_dig_port->dp.is_mst)
6013 drm_dp_mst_topology_mgr_suspend(&intel_dig_port->dp.mst_mgr);
6014 }
6015 }
6016}
6017
6018void intel_dp_mst_resume(struct drm_device *dev)
6019{
6020 struct drm_i915_private *dev_priv = dev->dev_private;
6021 int i;
6022
6023 for (i = 0; i < I915_MAX_PORTS; i++) {
Jani Nikula5fcece82015-05-27 15:03:42 +03006024 struct intel_digital_port *intel_dig_port = dev_priv->hotplug.irq_port[i];
Dave Airlie0e32b392014-05-02 14:02:48 +10006025 if (!intel_dig_port)
6026 continue;
6027 if (intel_dig_port->base.type == INTEL_OUTPUT_DISPLAYPORT) {
6028 int ret;
6029
6030 if (!intel_dig_port->dp.can_mst)
6031 continue;
6032
6033 ret = drm_dp_mst_topology_mgr_resume(&intel_dig_port->dp.mst_mgr);
6034 if (ret != 0) {
6035 intel_dp_check_mst_status(&intel_dig_port->dp);
6036 }
6037 }
6038 }
6039}