xref: /linux/drivers/gpu/drm/i915/display/intel_ddi.c (revision 49c55f7b035b87371a6d3c53d9af9f92ddc962db)
1379bc100SJani Nikula /*
2379bc100SJani Nikula  * Copyright © 2012 Intel Corporation
3379bc100SJani Nikula  *
4379bc100SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining a
5379bc100SJani Nikula  * copy of this software and associated documentation files (the "Software"),
6379bc100SJani Nikula  * to deal in the Software without restriction, including without limitation
7379bc100SJani Nikula  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8379bc100SJani Nikula  * and/or sell copies of the Software, and to permit persons to whom the
9379bc100SJani Nikula  * Software is furnished to do so, subject to the following conditions:
10379bc100SJani Nikula  *
11379bc100SJani Nikula  * The above copyright notice and this permission notice (including the next
12379bc100SJani Nikula  * paragraph) shall be included in all copies or substantial portions of the
13379bc100SJani Nikula  * Software.
14379bc100SJani Nikula  *
15379bc100SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16379bc100SJani Nikula  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17379bc100SJani Nikula  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18379bc100SJani Nikula  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19379bc100SJani Nikula  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20379bc100SJani Nikula  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21379bc100SJani Nikula  * IN THE SOFTWARE.
22379bc100SJani Nikula  *
23379bc100SJani Nikula  * Authors:
24379bc100SJani Nikula  *    Eugeni Dodonov <eugeni.dodonov@intel.com>
25379bc100SJani Nikula  *
26379bc100SJani Nikula  */
27379bc100SJani Nikula 
28379bc100SJani Nikula #include <drm/drm_scdc_helper.h>
29379bc100SJani Nikula 
30379bc100SJani Nikula #include "i915_drv.h"
31379bc100SJani Nikula #include "intel_audio.h"
326cc42fbeSJani Nikula #include "intel_backlight.h"
33379bc100SJani Nikula #include "intel_combo_phy.h"
34379bc100SJani Nikula #include "intel_connector.h"
357c53e628SJani Nikula #include "intel_crtc.h"
36379bc100SJani Nikula #include "intel_ddi.h"
3799092a97SDave Airlie #include "intel_ddi_buf_trans.h"
387785ae0bSVille Syrjälä #include "intel_de.h"
391d455f8dSJani Nikula #include "intel_display_types.h"
40379bc100SJani Nikula #include "intel_dp.h"
41379bc100SJani Nikula #include "intel_dp_link_training.h"
42dcb38f79SDave Airlie #include "intel_dp_mst.h"
43379bc100SJani Nikula #include "intel_dpio_phy.h"
44a1b63119SJosé Roberto de Souza #include "intel_drrs.h"
45379bc100SJani Nikula #include "intel_dsi.h"
46dcb38f79SDave Airlie #include "intel_fdi.h"
47379bc100SJani Nikula #include "intel_fifo_underrun.h"
48379bc100SJani Nikula #include "intel_gmbus.h"
49379bc100SJani Nikula #include "intel_hdcp.h"
50379bc100SJani Nikula #include "intel_hdmi.h"
51379bc100SJani Nikula #include "intel_hotplug.h"
52379bc100SJani Nikula #include "intel_lspcon.h"
53abad6805SJani Nikula #include "intel_pps.h"
54379bc100SJani Nikula #include "intel_psr.h"
55865b73eaSMatt Roper #include "intel_snps_phy.h"
56bdacf087SAnshuman Gupta #include "intel_sprite.h"
57bc85328fSImre Deak #include "intel_tc.h"
58379bc100SJani Nikula #include "intel_vdsc.h"
59aa52b39dSManasi Navare #include "intel_vrr.h"
60714b1cdbSDave Airlie #include "skl_scaler.h"
6146d12f91SDave Airlie #include "skl_universal_plane.h"
62379bc100SJani Nikula 
63379bc100SJani Nikula static const u8 index_to_dp_signal_levels[] = {
64379bc100SJani Nikula 	[0] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0,
65379bc100SJani Nikula 	[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1,
66379bc100SJani Nikula 	[2] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2,
67379bc100SJani Nikula 	[3] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_3,
68379bc100SJani Nikula 	[4] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0,
69379bc100SJani Nikula 	[5] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1,
70379bc100SJani Nikula 	[6] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2,
71379bc100SJani Nikula 	[7] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0,
72379bc100SJani Nikula 	[8] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1,
73379bc100SJani Nikula 	[9] = DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0,
74379bc100SJani Nikula };
75379bc100SJani Nikula 
76a621860aSVille Syrjälä static int intel_ddi_hdmi_level(struct intel_encoder *encoder,
773e022c1fSVille Syrjälä 				const struct intel_ddi_buf_trans *trans)
78379bc100SJani Nikula {
793e022c1fSVille Syrjälä 	int level;
80379bc100SJani Nikula 
810aed3bdeSJani Nikula 	level = intel_bios_hdmi_level_shift(encoder);
820aed3bdeSJani Nikula 	if (level < 0)
833e022c1fSVille Syrjälä 		level = trans->hdmi_default_entry;
84379bc100SJani Nikula 
85379bc100SJani Nikula 	return level;
86379bc100SJani Nikula }
87379bc100SJani Nikula 
885bafd85dSVille Syrjälä static bool has_buf_trans_select(struct drm_i915_private *i915)
895bafd85dSVille Syrjälä {
905bafd85dSVille Syrjälä 	return DISPLAY_VER(i915) < 10 && !IS_BROXTON(i915);
915bafd85dSVille Syrjälä }
925bafd85dSVille Syrjälä 
93f820693bSVille Syrjälä static bool has_iboost(struct drm_i915_private *i915)
94f820693bSVille Syrjälä {
95f820693bSVille Syrjälä 	return DISPLAY_VER(i915) == 9 && !IS_BROXTON(i915);
96f820693bSVille Syrjälä }
97f820693bSVille Syrjälä 
98379bc100SJani Nikula /*
99379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
100379bc100SJani Nikula  * values in advance. This function programs the correct values for
101379bc100SJani Nikula  * DP/eDP/FDI use cases.
102379bc100SJani Nikula  */
103266152aeSVille Syrjälä void hsw_prepare_dp_ddi_buffers(struct intel_encoder *encoder,
104379bc100SJani Nikula 				const struct intel_crtc_state *crtc_state)
105379bc100SJani Nikula {
106379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
107379bc100SJani Nikula 	u32 iboost_bit = 0;
108379bc100SJani Nikula 	int i, n_entries;
109379bc100SJani Nikula 	enum port port = encoder->port;
110e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
111379bc100SJani Nikula 
112e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
113e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
114d6b10b1aSVille Syrjälä 		return;
115379bc100SJani Nikula 
116379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
117f820693bSVille Syrjälä 	if (has_iboost(dev_priv) &&
1182446e1d6SMatt Roper 	    intel_bios_encoder_dp_boost_level(encoder->devdata))
119379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
120379bc100SJani Nikula 
121379bc100SJani Nikula 	for (i = 0; i < n_entries; i++) {
122f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, i),
123e505d764SVille Syrjälä 			       trans->entries[i].hsw.trans1 | iboost_bit);
124f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, i),
125e505d764SVille Syrjälä 			       trans->entries[i].hsw.trans2);
126379bc100SJani Nikula 	}
127379bc100SJani Nikula }
128379bc100SJani Nikula 
129379bc100SJani Nikula /*
130379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
131379bc100SJani Nikula  * values in advance. This function programs the correct values for
132379bc100SJani Nikula  * HDMI/DVI use cases.
133379bc100SJani Nikula  */
134266152aeSVille Syrjälä static void hsw_prepare_hdmi_ddi_buffers(struct intel_encoder *encoder,
135e722ab8bSVille Syrjälä 					 const struct intel_crtc_state *crtc_state)
136379bc100SJani Nikula {
137379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
138d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
139379bc100SJani Nikula 	u32 iboost_bit = 0;
140379bc100SJani Nikula 	int n_entries;
141379bc100SJani Nikula 	enum port port = encoder->port;
142e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
143379bc100SJani Nikula 
144e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
145e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
146379bc100SJani Nikula 		return;
147379bc100SJani Nikula 
148379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
149f820693bSVille Syrjälä 	if (has_iboost(dev_priv) &&
1502446e1d6SMatt Roper 	    intel_bios_encoder_hdmi_boost_level(encoder->devdata))
151379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
152379bc100SJani Nikula 
153379bc100SJani Nikula 	/* Entry 9 is for HDMI: */
154f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, 9),
155e505d764SVille Syrjälä 		       trans->entries[level].hsw.trans1 | iboost_bit);
156f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, 9),
157e505d764SVille Syrjälä 		       trans->entries[level].hsw.trans2);
158379bc100SJani Nikula }
159379bc100SJani Nikula 
160dcb38f79SDave Airlie void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
161379bc100SJani Nikula 			     enum port port)
162379bc100SJani Nikula {
1635a2ad99bSManasi Navare 	if (IS_BROXTON(dev_priv)) {
1645a2ad99bSManasi Navare 		udelay(16);
165379bc100SJani Nikula 		return;
166379bc100SJani Nikula 	}
1675a2ad99bSManasi Navare 
1685a2ad99bSManasi Navare 	if (wait_for_us((intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
1695a2ad99bSManasi Navare 			 DDI_BUF_IS_IDLE), 8))
1705a2ad99bSManasi Navare 		drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get idle\n",
17147bdb1caSJani Nikula 			port_name(port));
172379bc100SJani Nikula }
173379bc100SJani Nikula 
174e828da30SManasi Navare static void intel_wait_ddi_buf_active(struct drm_i915_private *dev_priv,
175e828da30SManasi Navare 				      enum port port)
176e828da30SManasi Navare {
177f82f2563SMatt Roper 	int ret;
178f82f2563SMatt Roper 
179e828da30SManasi Navare 	/* Wait > 518 usecs for DDI_BUF_CTL to be non idle */
180ad314fecSVille Syrjälä 	if (DISPLAY_VER(dev_priv) < 10) {
181e828da30SManasi Navare 		usleep_range(518, 1000);
182e828da30SManasi Navare 		return;
183e828da30SManasi Navare 	}
184e828da30SManasi Navare 
185f82f2563SMatt Roper 	ret = _wait_for(!(intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
186f82f2563SMatt Roper 			  DDI_BUF_IS_IDLE), IS_DG2(dev_priv) ? 1200 : 500, 10, 10);
187f82f2563SMatt Roper 
188f82f2563SMatt Roper 	if (ret)
189e828da30SManasi Navare 		drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get active\n",
190e828da30SManasi Navare 			port_name(port));
191e828da30SManasi Navare }
192e828da30SManasi Navare 
193ad952982SVille Syrjälä static u32 hsw_pll_to_ddi_pll_sel(const struct intel_shared_dpll *pll)
194379bc100SJani Nikula {
195379bc100SJani Nikula 	switch (pll->info->id) {
196379bc100SJani Nikula 	case DPLL_ID_WRPLL1:
197379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL1;
198379bc100SJani Nikula 	case DPLL_ID_WRPLL2:
199379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL2;
200379bc100SJani Nikula 	case DPLL_ID_SPLL:
201379bc100SJani Nikula 		return PORT_CLK_SEL_SPLL;
202379bc100SJani Nikula 	case DPLL_ID_LCPLL_810:
203379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_810;
204379bc100SJani Nikula 	case DPLL_ID_LCPLL_1350:
205379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_1350;
206379bc100SJani Nikula 	case DPLL_ID_LCPLL_2700:
207379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_2700;
208379bc100SJani Nikula 	default:
209379bc100SJani Nikula 		MISSING_CASE(pll->info->id);
210379bc100SJani Nikula 		return PORT_CLK_SEL_NONE;
211379bc100SJani Nikula 	}
212379bc100SJani Nikula }
213379bc100SJani Nikula 
214379bc100SJani Nikula static u32 icl_pll_to_ddi_clk_sel(struct intel_encoder *encoder,
215379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
216379bc100SJani Nikula {
217379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
218379bc100SJani Nikula 	int clock = crtc_state->port_clock;
219379bc100SJani Nikula 	const enum intel_dpll_id id = pll->info->id;
220379bc100SJani Nikula 
221379bc100SJani Nikula 	switch (id) {
222379bc100SJani Nikula 	default:
223379bc100SJani Nikula 		/*
224379bc100SJani Nikula 		 * DPLL_ID_ICL_DPLL0 and DPLL_ID_ICL_DPLL1 should not be used
225379bc100SJani Nikula 		 * here, so do warn if this get passed in
226379bc100SJani Nikula 		 */
227379bc100SJani Nikula 		MISSING_CASE(id);
228379bc100SJani Nikula 		return DDI_CLK_SEL_NONE;
229379bc100SJani Nikula 	case DPLL_ID_ICL_TBTPLL:
230379bc100SJani Nikula 		switch (clock) {
231379bc100SJani Nikula 		case 162000:
232379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_162;
233379bc100SJani Nikula 		case 270000:
234379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_270;
235379bc100SJani Nikula 		case 540000:
236379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_540;
237379bc100SJani Nikula 		case 810000:
238379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_810;
239379bc100SJani Nikula 		default:
240379bc100SJani Nikula 			MISSING_CASE(clock);
241379bc100SJani Nikula 			return DDI_CLK_SEL_NONE;
242379bc100SJani Nikula 		}
243379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL1:
244379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL2:
245379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL3:
246379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL4:
2476677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL5:
2486677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL6:
249379bc100SJani Nikula 		return DDI_CLK_SEL_MG;
250379bc100SJani Nikula 	}
251379bc100SJani Nikula }
252379bc100SJani Nikula 
253414002f1SImre Deak static u32 ddi_buf_phy_link_rate(int port_clock)
254414002f1SImre Deak {
255414002f1SImre Deak 	switch (port_clock) {
256414002f1SImre Deak 	case 162000:
257414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(0);
258414002f1SImre Deak 	case 216000:
259414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(4);
260414002f1SImre Deak 	case 243000:
261414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(5);
262414002f1SImre Deak 	case 270000:
263414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(1);
264414002f1SImre Deak 	case 324000:
265414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(6);
266414002f1SImre Deak 	case 432000:
267414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(7);
268414002f1SImre Deak 	case 540000:
269414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(2);
270414002f1SImre Deak 	case 810000:
271414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(3);
272414002f1SImre Deak 	default:
273414002f1SImre Deak 		MISSING_CASE(port_clock);
274414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(0);
275414002f1SImre Deak 	}
276414002f1SImre Deak }
277414002f1SImre Deak 
278a621860aSVille Syrjälä static void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder,
279a621860aSVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
280379bc100SJani Nikula {
28155ce306cSJosé Roberto de Souza 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
282b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2837801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
28455ce306cSJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(i915, encoder->port);
285379bc100SJani Nikula 
2869f620f1dSVille Syrjälä 	/* DDI_BUF_CTL_ENABLE will be set by intel_ddi_prepare_link_retrain() later */
2877801f3b7SLucas De Marchi 	intel_dp->DP = dig_port->saved_port_bits |
2889f620f1dSVille Syrjälä 		DDI_PORT_WIDTH(crtc_state->lane_count) |
2899f620f1dSVille Syrjälä 		DDI_BUF_TRANS_SELECT(0);
29055ce306cSJosé Roberto de Souza 
291414002f1SImre Deak 	if (IS_ALDERLAKE_P(i915) && intel_phy_is_tc(i915, phy)) {
292414002f1SImre Deak 		intel_dp->DP |= ddi_buf_phy_link_rate(crtc_state->port_clock);
29311a89708SImre Deak 		if (!intel_tc_port_in_tbt_alt_mode(dig_port))
29455ce306cSJosé Roberto de Souza 			intel_dp->DP |= DDI_BUF_CTL_TC_PHY_OWNERSHIP;
295379bc100SJani Nikula 	}
296414002f1SImre Deak }
297379bc100SJani Nikula 
298379bc100SJani Nikula static int icl_calc_tbt_pll_link(struct drm_i915_private *dev_priv,
299379bc100SJani Nikula 				 enum port port)
300379bc100SJani Nikula {
301f7960e7fSJani Nikula 	u32 val = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
302379bc100SJani Nikula 
303379bc100SJani Nikula 	switch (val) {
304379bc100SJani Nikula 	case DDI_CLK_SEL_NONE:
305379bc100SJani Nikula 		return 0;
306379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_162:
307379bc100SJani Nikula 		return 162000;
308379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_270:
309379bc100SJani Nikula 		return 270000;
310379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_540:
311379bc100SJani Nikula 		return 540000;
312379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_810:
313379bc100SJani Nikula 		return 810000;
314379bc100SJani Nikula 	default:
315379bc100SJani Nikula 		MISSING_CASE(val);
316379bc100SJani Nikula 		return 0;
317379bc100SJani Nikula 	}
318379bc100SJani Nikula }
319379bc100SJani Nikula 
320379bc100SJani Nikula static void ddi_dotclock_get(struct intel_crtc_state *pipe_config)
321379bc100SJani Nikula {
322379bc100SJani Nikula 	int dotclock;
323379bc100SJani Nikula 
3249e68fa88SVille Syrjälä 	/* CRT dotclock is determined via other means */
325379bc100SJani Nikula 	if (pipe_config->has_pch_encoder)
3269e68fa88SVille Syrjälä 		return;
3279e68fa88SVille Syrjälä 
3289e68fa88SVille Syrjälä 	if (intel_crtc_has_dp_encoder(pipe_config))
329379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
330379bc100SJani Nikula 						    &pipe_config->dp_m_n);
3312969a78aSImre Deak 	else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp > 24)
3322969a78aSImre Deak 		dotclock = pipe_config->port_clock * 24 / pipe_config->pipe_bpp;
333379bc100SJani Nikula 	else
334379bc100SJani Nikula 		dotclock = pipe_config->port_clock;
335379bc100SJani Nikula 
336379bc100SJani Nikula 	if (pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
337379bc100SJani Nikula 	    !intel_crtc_has_dp_encoder(pipe_config))
338379bc100SJani Nikula 		dotclock *= 2;
339379bc100SJani Nikula 
340379bc100SJani Nikula 	if (pipe_config->pixel_multiplier)
341379bc100SJani Nikula 		dotclock /= pipe_config->pixel_multiplier;
342379bc100SJani Nikula 
3431326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.crtc_clock = dotclock;
344379bc100SJani Nikula }
345379bc100SJani Nikula 
3460c06fa15SGwan-gyeong Mun void intel_ddi_set_dp_msa(const struct intel_crtc_state *crtc_state,
3470c06fa15SGwan-gyeong Mun 			  const struct drm_connector_state *conn_state)
348379bc100SJani Nikula {
3492225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
350379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
351379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
352379bc100SJani Nikula 	u32 temp;
353379bc100SJani Nikula 
354379bc100SJani Nikula 	if (!intel_crtc_has_dp_encoder(crtc_state))
355379bc100SJani Nikula 		return;
356379bc100SJani Nikula 
3571de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder));
358379bc100SJani Nikula 
3593e706dffSVille Syrjälä 	temp = DP_MSA_MISC_SYNC_CLOCK;
360379bc100SJani Nikula 
361379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
362379bc100SJani Nikula 	case 18:
3633e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_6_BPC;
364379bc100SJani Nikula 		break;
365379bc100SJani Nikula 	case 24:
3663e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_8_BPC;
367379bc100SJani Nikula 		break;
368379bc100SJani Nikula 	case 30:
3693e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_10_BPC;
370379bc100SJani Nikula 		break;
371379bc100SJani Nikula 	case 36:
3723e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_12_BPC;
373379bc100SJani Nikula 		break;
374379bc100SJani Nikula 	default:
375379bc100SJani Nikula 		MISSING_CASE(crtc_state->pipe_bpp);
376379bc100SJani Nikula 		break;
377379bc100SJani Nikula 	}
378379bc100SJani Nikula 
379cae154fcSVille Syrjälä 	/* nonsense combination */
3801de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
381cae154fcSVille Syrjälä 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
382cae154fcSVille Syrjälä 
383cae154fcSVille Syrjälä 	if (crtc_state->limited_color_range)
3843e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_CEA_RGB;
385cae154fcSVille Syrjälä 
386379bc100SJani Nikula 	/*
387379bc100SJani Nikula 	 * As per DP 1.2 spec section 2.3.4.3 while sending
388379bc100SJani Nikula 	 * YCBCR 444 signals we should program MSA MISC1/0 fields with
389646d3dc8SVille Syrjälä 	 * colorspace information.
390379bc100SJani Nikula 	 */
391379bc100SJani Nikula 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
3923e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_YCBCR_444_BT709;
393646d3dc8SVille Syrjälä 
394379bc100SJani Nikula 	/*
395379bc100SJani Nikula 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
396379bc100SJani Nikula 	 * of Color Encoding Format and Content Color Gamut] while sending
3970c06fa15SGwan-gyeong Mun 	 * YCBCR 420, HDR BT.2020 signals we should program MSA MISC1 fields
3980c06fa15SGwan-gyeong Mun 	 * which indicate VSC SDP for the Pixel Encoding/Colorimetry Format.
399379bc100SJani Nikula 	 */
400bd8c9ccaSGwan-gyeong Mun 	if (intel_dp_needs_vsc_sdp(crtc_state, conn_state))
4013e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_VSC_SDP;
4020c06fa15SGwan-gyeong Mun 
403f7960e7fSJani Nikula 	intel_de_write(dev_priv, TRANS_MSA_MISC(cpu_transcoder), temp);
404379bc100SJani Nikula }
405379bc100SJani Nikula 
406dc5b8ed5SVille Syrjälä static u32 bdw_trans_port_sync_master_select(enum transcoder master_transcoder)
407dc5b8ed5SVille Syrjälä {
408dc5b8ed5SVille Syrjälä 	if (master_transcoder == TRANSCODER_EDP)
409dc5b8ed5SVille Syrjälä 		return 0;
410dc5b8ed5SVille Syrjälä 	else
411dc5b8ed5SVille Syrjälä 		return master_transcoder + 1;
412dc5b8ed5SVille Syrjälä }
413dc5b8ed5SVille Syrjälä 
41479ac2b1bSJani Nikula static void
41579ac2b1bSJani Nikula intel_ddi_config_transcoder_dp2(struct intel_encoder *encoder,
41679ac2b1bSJani Nikula 				const struct intel_crtc_state *crtc_state)
41779ac2b1bSJani Nikula {
41879ac2b1bSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
41979ac2b1bSJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
42079ac2b1bSJani Nikula 	u32 val = 0;
42179ac2b1bSJani Nikula 
42279ac2b1bSJani Nikula 	if (intel_dp_is_uhbr(crtc_state))
42379ac2b1bSJani Nikula 		val = TRANS_DP2_128B132B_CHANNEL_CODING;
42479ac2b1bSJani Nikula 
42579ac2b1bSJani Nikula 	intel_de_write(i915, TRANS_DP2_CTL(cpu_transcoder), val);
42679ac2b1bSJani Nikula }
42779ac2b1bSJani Nikula 
42899389390SJosé Roberto de Souza /*
42999389390SJosé Roberto de Souza  * Returns the TRANS_DDI_FUNC_CTL value based on CRTC state.
43099389390SJosé Roberto de Souza  *
43199389390SJosé Roberto de Souza  * Only intended to be used by intel_ddi_enable_transcoder_func() and
43299389390SJosé Roberto de Souza  * intel_ddi_config_transcoder_func().
43399389390SJosé Roberto de Souza  */
43499389390SJosé Roberto de Souza static u32
435eed22a46SVille Syrjälä intel_ddi_transcoder_func_reg_val_get(struct intel_encoder *encoder,
436eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
437379bc100SJani Nikula {
4382225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
439379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
440379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
441379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
442379bc100SJani Nikula 	enum port port = encoder->port;
443379bc100SJani Nikula 	u32 temp;
444379bc100SJani Nikula 
445379bc100SJani Nikula 	/* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
446379bc100SJani Nikula 	temp = TRANS_DDI_FUNC_ENABLE;
447005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
448df16b636SMahesh Kumar 		temp |= TGL_TRANS_DDI_SELECT_PORT(port);
449df16b636SMahesh Kumar 	else
450379bc100SJani Nikula 		temp |= TRANS_DDI_SELECT_PORT(port);
451379bc100SJani Nikula 
452379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
453379bc100SJani Nikula 	case 18:
454379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_6;
455379bc100SJani Nikula 		break;
456379bc100SJani Nikula 	case 24:
457379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_8;
458379bc100SJani Nikula 		break;
459379bc100SJani Nikula 	case 30:
460379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_10;
461379bc100SJani Nikula 		break;
462379bc100SJani Nikula 	case 36:
463379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_12;
464379bc100SJani Nikula 		break;
465379bc100SJani Nikula 	default:
466379bc100SJani Nikula 		BUG();
467379bc100SJani Nikula 	}
468379bc100SJani Nikula 
4691326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
470379bc100SJani Nikula 		temp |= TRANS_DDI_PVSYNC;
4711326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
472379bc100SJani Nikula 		temp |= TRANS_DDI_PHSYNC;
473379bc100SJani Nikula 
474379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP) {
475379bc100SJani Nikula 		switch (pipe) {
476379bc100SJani Nikula 		case PIPE_A:
477379bc100SJani Nikula 			/* On Haswell, can only use the always-on power well for
478379bc100SJani Nikula 			 * eDP when not using the panel fitter, and when not
479379bc100SJani Nikula 			 * using motion blur mitigation (which we don't
480379bc100SJani Nikula 			 * support). */
481379bc100SJani Nikula 			if (crtc_state->pch_pfit.force_thru)
482379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
483379bc100SJani Nikula 			else
484379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ON;
485379bc100SJani Nikula 			break;
486379bc100SJani Nikula 		case PIPE_B:
487379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
488379bc100SJani Nikula 			break;
489379bc100SJani Nikula 		case PIPE_C:
490379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
491379bc100SJani Nikula 			break;
492379bc100SJani Nikula 		default:
493379bc100SJani Nikula 			BUG();
494379bc100SJani Nikula 			break;
495379bc100SJani Nikula 		}
496379bc100SJani Nikula 	}
497379bc100SJani Nikula 
498379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
499379bc100SJani Nikula 		if (crtc_state->has_hdmi_sink)
500379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_HDMI;
501379bc100SJani Nikula 		else
502379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_DVI;
503379bc100SJani Nikula 
504379bc100SJani Nikula 		if (crtc_state->hdmi_scrambling)
505379bc100SJani Nikula 			temp |= TRANS_DDI_HDMI_SCRAMBLING;
506379bc100SJani Nikula 		if (crtc_state->hdmi_high_tmds_clock_ratio)
507379bc100SJani Nikula 			temp |= TRANS_DDI_HIGH_TMDS_CHAR_RATE;
508379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
5097bb97db8SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_FDI_OR_128B132B;
510379bc100SJani Nikula 		temp |= (crtc_state->fdi_lanes - 1) << 1;
511379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
51265213594SJani Nikula 		if (intel_dp_is_uhbr(crtc_state))
51365213594SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_FDI_OR_128B132B;
51465213594SJani Nikula 		else
515379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_DP_MST;
516379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
517b3545e08SLucas De Marchi 
518005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12) {
5196671c367SJosé Roberto de Souza 			enum transcoder master;
5206671c367SJosé Roberto de Souza 
5216671c367SJosé Roberto de Souza 			master = crtc_state->mst_master_transcoder;
5221de143ccSPankaj Bharadiya 			drm_WARN_ON(&dev_priv->drm,
5231de143ccSPankaj Bharadiya 				    master == INVALID_TRANSCODER);
5246671c367SJosé Roberto de Souza 			temp |= TRANS_DDI_MST_TRANSPORT_SELECT(master);
5256671c367SJosé Roberto de Souza 		}
526379bc100SJani Nikula 	} else {
527379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_SST;
528379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
529379bc100SJani Nikula 	}
530379bc100SJani Nikula 
53193e7e61eSLucas De Marchi 	if (IS_DISPLAY_VER(dev_priv, 8, 10) &&
532dc5b8ed5SVille Syrjälä 	    crtc_state->master_transcoder != INVALID_TRANSCODER) {
533dc5b8ed5SVille Syrjälä 		u8 master_select =
534dc5b8ed5SVille Syrjälä 			bdw_trans_port_sync_master_select(crtc_state->master_transcoder);
535dc5b8ed5SVille Syrjälä 
536dc5b8ed5SVille Syrjälä 		temp |= TRANS_DDI_PORT_SYNC_ENABLE |
537dc5b8ed5SVille Syrjälä 			TRANS_DDI_PORT_SYNC_MASTER_SELECT(master_select);
538dc5b8ed5SVille Syrjälä 	}
539dc5b8ed5SVille Syrjälä 
54099389390SJosé Roberto de Souza 	return temp;
54199389390SJosé Roberto de Souza }
54299389390SJosé Roberto de Souza 
543eed22a46SVille Syrjälä void intel_ddi_enable_transcoder_func(struct intel_encoder *encoder,
544eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
54599389390SJosé Roberto de Souza {
5462225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
54799389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
54899389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
54999389390SJosé Roberto de Souza 
550005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
551589a4cd6SVille Syrjälä 		enum transcoder master_transcoder = crtc_state->master_transcoder;
552589a4cd6SVille Syrjälä 		u32 ctl2 = 0;
553589a4cd6SVille Syrjälä 
554589a4cd6SVille Syrjälä 		if (master_transcoder != INVALID_TRANSCODER) {
555dc5b8ed5SVille Syrjälä 			u8 master_select =
556dc5b8ed5SVille Syrjälä 				bdw_trans_port_sync_master_select(master_transcoder);
557589a4cd6SVille Syrjälä 
558589a4cd6SVille Syrjälä 			ctl2 |= PORT_SYNC_MODE_ENABLE |
559d4d7d9caSVille Syrjälä 				PORT_SYNC_MODE_MASTER_SELECT(master_select);
560589a4cd6SVille Syrjälä 		}
561589a4cd6SVille Syrjälä 
562589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
563589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), ctl2);
564589a4cd6SVille Syrjälä 	}
565589a4cd6SVille Syrjälä 
566580fbdc5SImre Deak 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder),
567580fbdc5SImre Deak 		       intel_ddi_transcoder_func_reg_val_get(encoder,
568580fbdc5SImre Deak 							     crtc_state));
56999389390SJosé Roberto de Souza }
57099389390SJosé Roberto de Souza 
57199389390SJosé Roberto de Souza /*
57299389390SJosé Roberto de Souza  * Same as intel_ddi_enable_transcoder_func(), but it does not set the enable
57399389390SJosé Roberto de Souza  * bit.
57499389390SJosé Roberto de Souza  */
57599389390SJosé Roberto de Souza static void
576eed22a46SVille Syrjälä intel_ddi_config_transcoder_func(struct intel_encoder *encoder,
577eed22a46SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
57899389390SJosé Roberto de Souza {
5792225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
58099389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
58199389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
582589a4cd6SVille Syrjälä 	u32 ctl;
58399389390SJosé Roberto de Souza 
584eed22a46SVille Syrjälä 	ctl = intel_ddi_transcoder_func_reg_val_get(encoder, crtc_state);
585589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
586589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
587379bc100SJani Nikula }
588379bc100SJani Nikula 
589379bc100SJani Nikula void intel_ddi_disable_transcoder_func(const struct intel_crtc_state *crtc_state)
590379bc100SJani Nikula {
5912225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
592379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
593379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
594589a4cd6SVille Syrjälä 	u32 ctl;
595c59053dcSJosé Roberto de Souza 
596005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
597589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
598589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), 0);
599589a4cd6SVille Syrjälä 
600589a4cd6SVille Syrjälä 	ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
601dc5b8ed5SVille Syrjälä 
6021cfcdbf3SSean Paul 	drm_WARN_ON(crtc->base.dev, ctl & TRANS_DDI_HDCP_SIGNALLING);
6031cfcdbf3SSean Paul 
604589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
605379bc100SJani Nikula 
60693e7e61eSLucas De Marchi 	if (IS_DISPLAY_VER(dev_priv, 8, 10))
607dc5b8ed5SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_SYNC_ENABLE |
608dc5b8ed5SVille Syrjälä 			 TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK);
609dc5b8ed5SVille Syrjälä 
610005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
611919e4f07SJosé Roberto de Souza 		if (!intel_dp_mst_is_master_trans(crtc_state)) {
612589a4cd6SVille Syrjälä 			ctl &= ~(TGL_TRANS_DDI_PORT_MASK |
613919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
614919e4f07SJosé Roberto de Souza 		}
615df16b636SMahesh Kumar 	} else {
616589a4cd6SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_MASK | TRANS_DDI_MODE_SELECT_MASK);
617df16b636SMahesh Kumar 	}
618dc5b8ed5SVille Syrjälä 
619589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
620379bc100SJani Nikula 
621379bc100SJani Nikula 	if (dev_priv->quirks & QUIRK_INCREASE_DDI_DISABLED_TIME &&
622379bc100SJani Nikula 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
62347bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
62447bdb1caSJani Nikula 			    "Quirk Increase DDI disabled time\n");
625379bc100SJani Nikula 		/* Quirk time at 100ms for reliable operation */
626379bc100SJani Nikula 		msleep(100);
627379bc100SJani Nikula 	}
628379bc100SJani Nikula }
629379bc100SJani Nikula 
6301a67a168SAnshuman Gupta int intel_ddi_toggle_hdcp_bits(struct intel_encoder *intel_encoder,
6310b9c9290SSean Paul 			       enum transcoder cpu_transcoder,
6321a67a168SAnshuman Gupta 			       bool enable, u32 hdcp_mask)
633379bc100SJani Nikula {
634379bc100SJani Nikula 	struct drm_device *dev = intel_encoder->base.dev;
635379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
636379bc100SJani Nikula 	intel_wakeref_t wakeref;
637379bc100SJani Nikula 	int ret = 0;
638379bc100SJani Nikula 	u32 tmp;
639379bc100SJani Nikula 
640379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
641379bc100SJani Nikula 						     intel_encoder->power_domain);
6421de143ccSPankaj Bharadiya 	if (drm_WARN_ON(dev, !wakeref))
643379bc100SJani Nikula 		return -ENXIO;
644379bc100SJani Nikula 
6450b9c9290SSean Paul 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
646379bc100SJani Nikula 	if (enable)
6471a67a168SAnshuman Gupta 		tmp |= hdcp_mask;
648379bc100SJani Nikula 	else
6491a67a168SAnshuman Gupta 		tmp &= ~hdcp_mask;
6500b9c9290SSean Paul 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), tmp);
651379bc100SJani Nikula 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
652379bc100SJani Nikula 	return ret;
653379bc100SJani Nikula }
654379bc100SJani Nikula 
655379bc100SJani Nikula bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
656379bc100SJani Nikula {
657379bc100SJani Nikula 	struct drm_device *dev = intel_connector->base.dev;
658379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
659fa7edcd2SVille Syrjälä 	struct intel_encoder *encoder = intel_attached_encoder(intel_connector);
660379bc100SJani Nikula 	int type = intel_connector->base.connector_type;
661379bc100SJani Nikula 	enum port port = encoder->port;
662379bc100SJani Nikula 	enum transcoder cpu_transcoder;
663379bc100SJani Nikula 	intel_wakeref_t wakeref;
664379bc100SJani Nikula 	enum pipe pipe = 0;
665379bc100SJani Nikula 	u32 tmp;
666379bc100SJani Nikula 	bool ret;
667379bc100SJani Nikula 
668379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
669379bc100SJani Nikula 						     encoder->power_domain);
670379bc100SJani Nikula 	if (!wakeref)
671379bc100SJani Nikula 		return false;
672379bc100SJani Nikula 
673379bc100SJani Nikula 	if (!encoder->get_hw_state(encoder, &pipe)) {
674379bc100SJani Nikula 		ret = false;
675379bc100SJani Nikula 		goto out;
676379bc100SJani Nikula 	}
677379bc100SJani Nikula 
67810cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
679379bc100SJani Nikula 		cpu_transcoder = TRANSCODER_EDP;
680379bc100SJani Nikula 	else
681379bc100SJani Nikula 		cpu_transcoder = (enum transcoder) pipe;
682379bc100SJani Nikula 
683f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
684379bc100SJani Nikula 
685379bc100SJani Nikula 	switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
686379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
687379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
688379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_HDMIA;
689379bc100SJani Nikula 		break;
690379bc100SJani Nikula 
691379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
692379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_eDP ||
693379bc100SJani Nikula 		      type == DRM_MODE_CONNECTOR_DisplayPort;
694379bc100SJani Nikula 		break;
695379bc100SJani Nikula 
696379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
697379bc100SJani Nikula 		/* if the transcoder is in MST state then
698379bc100SJani Nikula 		 * connector isn't connected */
699379bc100SJani Nikula 		ret = false;
700379bc100SJani Nikula 		break;
701379bc100SJani Nikula 
7027bb97db8SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI_OR_128B132B:
70365213594SJani Nikula 		if (HAS_DP20(dev_priv))
70465213594SJani Nikula 			/* 128b/132b */
70565213594SJani Nikula 			ret = false;
70665213594SJani Nikula 		else
70765213594SJani Nikula 			/* FDI */
708379bc100SJani Nikula 			ret = type == DRM_MODE_CONNECTOR_VGA;
709379bc100SJani Nikula 		break;
710379bc100SJani Nikula 
711379bc100SJani Nikula 	default:
712379bc100SJani Nikula 		ret = false;
713379bc100SJani Nikula 		break;
714379bc100SJani Nikula 	}
715379bc100SJani Nikula 
716379bc100SJani Nikula out:
717379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
718379bc100SJani Nikula 
719379bc100SJani Nikula 	return ret;
720379bc100SJani Nikula }
721379bc100SJani Nikula 
722379bc100SJani Nikula static void intel_ddi_get_encoder_pipes(struct intel_encoder *encoder,
723379bc100SJani Nikula 					u8 *pipe_mask, bool *is_dp_mst)
724379bc100SJani Nikula {
725379bc100SJani Nikula 	struct drm_device *dev = encoder->base.dev;
726379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
727379bc100SJani Nikula 	enum port port = encoder->port;
728379bc100SJani Nikula 	intel_wakeref_t wakeref;
729379bc100SJani Nikula 	enum pipe p;
730379bc100SJani Nikula 	u32 tmp;
731379bc100SJani Nikula 	u8 mst_pipe_mask;
732379bc100SJani Nikula 
733379bc100SJani Nikula 	*pipe_mask = 0;
734379bc100SJani Nikula 	*is_dp_mst = false;
735379bc100SJani Nikula 
736379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
737379bc100SJani Nikula 						     encoder->power_domain);
738379bc100SJani Nikula 	if (!wakeref)
739379bc100SJani Nikula 		return;
740379bc100SJani Nikula 
741f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
742379bc100SJani Nikula 	if (!(tmp & DDI_BUF_CTL_ENABLE))
743379bc100SJani Nikula 		goto out;
744379bc100SJani Nikula 
74510cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A) {
746f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
747f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
748379bc100SJani Nikula 
749379bc100SJani Nikula 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
750379bc100SJani Nikula 		default:
751379bc100SJani Nikula 			MISSING_CASE(tmp & TRANS_DDI_EDP_INPUT_MASK);
752df561f66SGustavo A. R. Silva 			fallthrough;
753379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ON:
754379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
755379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_A);
756379bc100SJani Nikula 			break;
757379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
758379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_B);
759379bc100SJani Nikula 			break;
760379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
761379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_C);
762379bc100SJani Nikula 			break;
763379bc100SJani Nikula 		}
764379bc100SJani Nikula 
765379bc100SJani Nikula 		goto out;
766379bc100SJani Nikula 	}
767379bc100SJani Nikula 
768379bc100SJani Nikula 	mst_pipe_mask = 0;
769379bc100SJani Nikula 	for_each_pipe(dev_priv, p) {
770379bc100SJani Nikula 		enum transcoder cpu_transcoder = (enum transcoder)p;
771df16b636SMahesh Kumar 		unsigned int port_mask, ddi_select;
7726aa3bef1SJosé Roberto de Souza 		intel_wakeref_t trans_wakeref;
7736aa3bef1SJosé Roberto de Souza 
7746aa3bef1SJosé Roberto de Souza 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
7756aa3bef1SJosé Roberto de Souza 								   POWER_DOMAIN_TRANSCODER(cpu_transcoder));
7766aa3bef1SJosé Roberto de Souza 		if (!trans_wakeref)
7776aa3bef1SJosé Roberto de Souza 			continue;
778df16b636SMahesh Kumar 
779005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12) {
780df16b636SMahesh Kumar 			port_mask = TGL_TRANS_DDI_PORT_MASK;
781df16b636SMahesh Kumar 			ddi_select = TGL_TRANS_DDI_SELECT_PORT(port);
782df16b636SMahesh Kumar 		} else {
783df16b636SMahesh Kumar 			port_mask = TRANS_DDI_PORT_MASK;
784df16b636SMahesh Kumar 			ddi_select = TRANS_DDI_SELECT_PORT(port);
785df16b636SMahesh Kumar 		}
786379bc100SJani Nikula 
787f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
788f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
7896aa3bef1SJosé Roberto de Souza 		intel_display_power_put(dev_priv, POWER_DOMAIN_TRANSCODER(cpu_transcoder),
7906aa3bef1SJosé Roberto de Souza 					trans_wakeref);
791379bc100SJani Nikula 
792df16b636SMahesh Kumar 		if ((tmp & port_mask) != ddi_select)
793379bc100SJani Nikula 			continue;
794379bc100SJani Nikula 
79565213594SJani Nikula 		if ((tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_DP_MST ||
79665213594SJani Nikula 		    (HAS_DP20(dev_priv) &&
79765213594SJani Nikula 		     (tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_FDI_OR_128B132B))
798379bc100SJani Nikula 			mst_pipe_mask |= BIT(p);
799379bc100SJani Nikula 
800379bc100SJani Nikula 		*pipe_mask |= BIT(p);
801379bc100SJani Nikula 	}
802379bc100SJani Nikula 
803379bc100SJani Nikula 	if (!*pipe_mask)
80447bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
80547bdb1caSJani Nikula 			    "No pipe for [ENCODER:%d:%s] found\n",
80666a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name);
807379bc100SJani Nikula 
808379bc100SJani Nikula 	if (!mst_pipe_mask && hweight8(*pipe_mask) > 1) {
80947bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
81047bdb1caSJani Nikula 			    "Multiple pipes for [ENCODER:%d:%s] (pipe_mask %02x)\n",
81166a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
81266a990ddSVille Syrjälä 			    *pipe_mask);
813379bc100SJani Nikula 		*pipe_mask = BIT(ffs(*pipe_mask) - 1);
814379bc100SJani Nikula 	}
815379bc100SJani Nikula 
816379bc100SJani Nikula 	if (mst_pipe_mask && mst_pipe_mask != *pipe_mask)
81747bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
81847bdb1caSJani Nikula 			    "Conflicting MST and non-MST state for [ENCODER:%d:%s] (pipe_mask %02x mst_pipe_mask %02x)\n",
81966a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
82066a990ddSVille Syrjälä 			    *pipe_mask, mst_pipe_mask);
821379bc100SJani Nikula 	else
822379bc100SJani Nikula 		*is_dp_mst = mst_pipe_mask;
823379bc100SJani Nikula 
824379bc100SJani Nikula out:
8252446e1d6SMatt Roper 	if (*pipe_mask && (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))) {
826f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv, BXT_PHY_CTL(port));
827379bc100SJani Nikula 		if ((tmp & (BXT_PHY_CMNLANE_POWERDOWN_ACK |
828379bc100SJani Nikula 			    BXT_PHY_LANE_POWERDOWN_ACK |
829379bc100SJani Nikula 			    BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
83047bdb1caSJani Nikula 			drm_err(&dev_priv->drm,
83147bdb1caSJani Nikula 				"[ENCODER:%d:%s] enabled but PHY powered down? (PHY_CTL %08x)\n",
83247bdb1caSJani Nikula 				encoder->base.base.id, encoder->base.name, tmp);
833379bc100SJani Nikula 	}
834379bc100SJani Nikula 
835379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
836379bc100SJani Nikula }
837379bc100SJani Nikula 
838379bc100SJani Nikula bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
839379bc100SJani Nikula 			    enum pipe *pipe)
840379bc100SJani Nikula {
841379bc100SJani Nikula 	u8 pipe_mask;
842379bc100SJani Nikula 	bool is_mst;
843379bc100SJani Nikula 
844379bc100SJani Nikula 	intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
845379bc100SJani Nikula 
846379bc100SJani Nikula 	if (is_mst || !pipe_mask)
847379bc100SJani Nikula 		return false;
848379bc100SJani Nikula 
849379bc100SJani Nikula 	*pipe = ffs(pipe_mask) - 1;
850379bc100SJani Nikula 
851379bc100SJani Nikula 	return true;
852379bc100SJani Nikula }
853379bc100SJani Nikula 
85481b55ef1SJani Nikula static enum intel_display_power_domain
855379bc100SJani Nikula intel_ddi_main_link_aux_domain(struct intel_digital_port *dig_port)
856379bc100SJani Nikula {
8574da27d5dSLucas De Marchi 	/* ICL+ HW requires corresponding AUX IOs to be powered up for PSR with
858379bc100SJani Nikula 	 * DC states enabled at the same time, while for driver initiated AUX
859379bc100SJani Nikula 	 * transfers we need the same AUX IOs to be powered but with DC states
860379bc100SJani Nikula 	 * disabled. Accordingly use the AUX power domain here which leaves DC
861379bc100SJani Nikula 	 * states enabled.
862379bc100SJani Nikula 	 * However, for non-A AUX ports the corresponding non-EDP transcoders
863379bc100SJani Nikula 	 * would have already enabled power well 2 and DC_OFF. This means we can
864379bc100SJani Nikula 	 * acquire a wider POWER_DOMAIN_AUX_{B,C,D,F} reference instead of a
865379bc100SJani Nikula 	 * specific AUX_IO reference without powering up any extra wells.
866379bc100SJani Nikula 	 * Note that PSR is enabled only on Port A even though this function
867379bc100SJani Nikula 	 * returns the correct domain for other ports too.
868379bc100SJani Nikula 	 */
869379bc100SJani Nikula 	return dig_port->aux_ch == AUX_CH_A ? POWER_DOMAIN_AUX_IO_A :
870379bc100SJani Nikula 					      intel_aux_power_domain(dig_port);
871379bc100SJani Nikula }
872379bc100SJani Nikula 
873379bc100SJani Nikula static void intel_ddi_get_power_domains(struct intel_encoder *encoder,
874379bc100SJani Nikula 					struct intel_crtc_state *crtc_state)
875379bc100SJani Nikula {
876379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
877379bc100SJani Nikula 	struct intel_digital_port *dig_port;
878d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
879379bc100SJani Nikula 
880379bc100SJani Nikula 	/*
881379bc100SJani Nikula 	 * TODO: Add support for MST encoders. Atm, the following should never
882379bc100SJani Nikula 	 * happen since fake-MST encoders don't set their get_power_domains()
883379bc100SJani Nikula 	 * hook.
884379bc100SJani Nikula 	 */
8851de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
8861de143ccSPankaj Bharadiya 			intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)))
887379bc100SJani Nikula 		return;
888379bc100SJani Nikula 
889b7d02c3aSVille Syrjälä 	dig_port = enc_to_dig_port(encoder);
890f77a2db2SImre Deak 
89111a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
892a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
893a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
894f77a2db2SImre Deak 								   dig_port->ddi_io_power_domain);
895a4550977SImre Deak 	}
896379bc100SJani Nikula 
897379bc100SJani Nikula 	/*
898379bc100SJani Nikula 	 * AUX power is only needed for (e)DP mode, and for HDMI mode on TC
899379bc100SJani Nikula 	 * ports.
900379bc100SJani Nikula 	 */
901379bc100SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state) ||
902162e68e1SImre Deak 	    intel_phy_is_tc(dev_priv, phy)) {
903162e68e1SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->aux_wakeref);
904162e68e1SImre Deak 		dig_port->aux_wakeref =
905379bc100SJani Nikula 			intel_display_power_get(dev_priv,
906379bc100SJani Nikula 						intel_ddi_main_link_aux_domain(dig_port));
907379bc100SJani Nikula 	}
908162e68e1SImre Deak }
909379bc100SJani Nikula 
91002a715c3SVille Syrjälä void intel_ddi_enable_pipe_clock(struct intel_encoder *encoder,
91102a715c3SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
912379bc100SJani Nikula {
9132225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
914379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
915379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
916ed2615a8SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
917ed2615a8SMatt Roper 	u32 val;
918379bc100SJani Nikula 
919df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
920ed2615a8SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 13)
921ed2615a8SMatt Roper 			val = TGL_TRANS_CLK_SEL_PORT(phy);
922ed2615a8SMatt Roper 		else if (DISPLAY_VER(dev_priv) >= 12)
923ed2615a8SMatt Roper 			val = TGL_TRANS_CLK_SEL_PORT(encoder->port);
924df16b636SMahesh Kumar 		else
925ed2615a8SMatt Roper 			val = TRANS_CLK_SEL_PORT(encoder->port);
926ed2615a8SMatt Roper 
927ed2615a8SMatt Roper 		intel_de_write(dev_priv, TRANS_CLK_SEL(cpu_transcoder), val);
928379bc100SJani Nikula 	}
929df16b636SMahesh Kumar }
930379bc100SJani Nikula 
931379bc100SJani Nikula void intel_ddi_disable_pipe_clock(const struct intel_crtc_state *crtc_state)
932379bc100SJani Nikula {
9332225f3c6SMaarten Lankhorst 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
934379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
935379bc100SJani Nikula 
936df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
937005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12)
938f7960e7fSJani Nikula 			intel_de_write(dev_priv,
939f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
940df16b636SMahesh Kumar 				       TGL_TRANS_CLK_SEL_DISABLED);
941df16b636SMahesh Kumar 		else
942f7960e7fSJani Nikula 			intel_de_write(dev_priv,
943f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
944379bc100SJani Nikula 				       TRANS_CLK_SEL_DISABLED);
945379bc100SJani Nikula 	}
946df16b636SMahesh Kumar }
947379bc100SJani Nikula 
948379bc100SJani Nikula static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
949379bc100SJani Nikula 				enum port port, u8 iboost)
950379bc100SJani Nikula {
951379bc100SJani Nikula 	u32 tmp;
952379bc100SJani Nikula 
953f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DISPIO_CR_TX_BMU_CR0);
954379bc100SJani Nikula 	tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
955379bc100SJani Nikula 	if (iboost)
956379bc100SJani Nikula 		tmp |= iboost << BALANCE_LEG_SHIFT(port);
957379bc100SJani Nikula 	else
958379bc100SJani Nikula 		tmp |= BALANCE_LEG_DISABLE(port);
959f7960e7fSJani Nikula 	intel_de_write(dev_priv, DISPIO_CR_TX_BMU_CR0, tmp);
960379bc100SJani Nikula }
961379bc100SJani Nikula 
962379bc100SJani Nikula static void skl_ddi_set_iboost(struct intel_encoder *encoder,
963a621860aSVille Syrjälä 			       const struct intel_crtc_state *crtc_state,
964a621860aSVille Syrjälä 			       int level)
965379bc100SJani Nikula {
9667801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
967379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
968379bc100SJani Nikula 	u8 iboost;
969379bc100SJani Nikula 
970a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
971c0a950d1SJani Nikula 		iboost = intel_bios_encoder_hdmi_boost_level(encoder->devdata);
972379bc100SJani Nikula 	else
973c0a950d1SJani Nikula 		iboost = intel_bios_encoder_dp_boost_level(encoder->devdata);
974379bc100SJani Nikula 
975379bc100SJani Nikula 	if (iboost == 0) {
976e505d764SVille Syrjälä 		const struct intel_ddi_buf_trans *trans;
977379bc100SJani Nikula 		int n_entries;
978379bc100SJani Nikula 
979e505d764SVille Syrjälä 		trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
980e505d764SVille Syrjälä 		if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
981379bc100SJani Nikula 			return;
982379bc100SJani Nikula 
983e505d764SVille Syrjälä 		iboost = trans->entries[level].hsw.i_boost;
984379bc100SJani Nikula 	}
985379bc100SJani Nikula 
986379bc100SJani Nikula 	/* Make sure that the requested I_boost is valid */
987379bc100SJani Nikula 	if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
98847bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Invalid I_boost value %u\n", iboost);
989379bc100SJani Nikula 		return;
990379bc100SJani Nikula 	}
991379bc100SJani Nikula 
992f0e86e05SJosé Roberto de Souza 	_skl_ddi_set_iboost(dev_priv, encoder->port, iboost);
993379bc100SJani Nikula 
994f0e86e05SJosé Roberto de Souza 	if (encoder->port == PORT_A && dig_port->max_lanes == 4)
995379bc100SJani Nikula 		_skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
996379bc100SJani Nikula }
997379bc100SJani Nikula 
998a621860aSVille Syrjälä static u8 intel_ddi_dp_voltage_max(struct intel_dp *intel_dp,
999a621860aSVille Syrjälä 				   const struct intel_crtc_state *crtc_state)
1000379bc100SJani Nikula {
100153de0a20SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1002379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1003379bc100SJani Nikula 	int n_entries;
1004379bc100SJani Nikula 
1005c40a253bSVille Syrjälä 	encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1006379bc100SJani Nikula 
10071de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, n_entries < 1))
1008379bc100SJani Nikula 		n_entries = 1;
10091de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
10101de143ccSPankaj Bharadiya 			n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
1011379bc100SJani Nikula 		n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
1012379bc100SJani Nikula 
1013379bc100SJani Nikula 	return index_to_dp_signal_levels[n_entries - 1] &
1014379bc100SJani Nikula 		DP_TRAIN_VOLTAGE_SWING_MASK;
1015379bc100SJani Nikula }
1016379bc100SJani Nikula 
1017379bc100SJani Nikula /*
1018379bc100SJani Nikula  * We assume that the full set of pre-emphasis values can be
1019379bc100SJani Nikula  * used on all DDI platforms. Should that change we need to
1020379bc100SJani Nikula  * rethink this code.
1021379bc100SJani Nikula  */
102253de0a20SVille Syrjälä static u8 intel_ddi_dp_preemph_max(struct intel_dp *intel_dp)
1023379bc100SJani Nikula {
1024379bc100SJani Nikula 	return DP_TRAIN_PRE_EMPH_LEVEL_3;
1025379bc100SJani Nikula }
1026379bc100SJani Nikula 
10275e7fe4d9SVille Syrjälä static u32 icl_combo_phy_loadgen_select(const struct intel_crtc_state *crtc_state,
10285e7fe4d9SVille Syrjälä 					int lane)
10295e7fe4d9SVille Syrjälä {
10305e7fe4d9SVille Syrjälä 	if (crtc_state->port_clock > 600000)
10315e7fe4d9SVille Syrjälä 		return 0;
10325e7fe4d9SVille Syrjälä 
10335e7fe4d9SVille Syrjälä 	if (crtc_state->lane_count == 4)
10345e7fe4d9SVille Syrjälä 		return lane >= 1 ? LOADGEN_SELECT : 0;
10355e7fe4d9SVille Syrjälä 	else
10365e7fe4d9SVille Syrjälä 		return lane == 1 || lane == 2 ? LOADGEN_SELECT : 0;
10375e7fe4d9SVille Syrjälä }
10385e7fe4d9SVille Syrjälä 
1039a8143150SJosé Roberto de Souza static void icl_ddi_combo_vswing_program(struct intel_encoder *encoder,
1040193299adSVille Syrjälä 					 const struct intel_crtc_state *crtc_state)
1041379bc100SJani Nikula {
1042a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1043d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
1044e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
1045f0e86e05SJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1046a621860aSVille Syrjälä 	int n_entries, ln;
1047a621860aSVille Syrjälä 	u32 val;
1048379bc100SJani Nikula 
1049e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1050e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
105185da0292SVille Syrjälä 		return;
1052379bc100SJani Nikula 
1053a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP)) {
105481619f4aSJosé Roberto de Souza 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
105581619f4aSJosé Roberto de Souza 
105681619f4aSJosé Roberto de Souza 		val = EDP4K2K_MODE_OVRD_EN | EDP4K2K_MODE_OVRD_OPTIMIZED;
1057e505d764SVille Syrjälä 		intel_dp->hobl_active = is_hobl_buf_trans(trans);
105881619f4aSJosé Roberto de Souza 		intel_de_rmw(dev_priv, ICL_PORT_CL_DW10(phy), val,
105981619f4aSJosé Roberto de Souza 			     intel_dp->hobl_active ? val : 0);
106081619f4aSJosé Roberto de Souza 	}
106181619f4aSJosé Roberto de Souza 
1062379bc100SJani Nikula 	/* Set PORT_TX_DW5 */
1063e6908588SVille Syrjälä 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1064379bc100SJani Nikula 	val &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK |
1065379bc100SJani Nikula 		  TAP2_DISABLE | TAP3_DISABLE);
1066379bc100SJani Nikula 	val |= SCALING_MODE_SEL(0x2);
1067379bc100SJani Nikula 	val |= RTERM_SELECT(0x6);
1068379bc100SJani Nikula 	val |= TAP3_DISABLE;
1069f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1070379bc100SJani Nikula 
1071379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
1072e6908588SVille Syrjälä 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW2_LN(0, phy));
1073379bc100SJani Nikula 	val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
1074379bc100SJani Nikula 		 RCOMP_SCALAR_MASK);
1075e505d764SVille Syrjälä 	val |= SWING_SEL_UPPER(trans->entries[level].icl.dw2_swing_sel);
1076e505d764SVille Syrjälä 	val |= SWING_SEL_LOWER(trans->entries[level].icl.dw2_swing_sel);
1077379bc100SJani Nikula 	/* Program Rcomp scalar for every table entry */
1078379bc100SJani Nikula 	val |= RCOMP_SCALAR(0x98);
1079f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW2_GRP(phy), val);
1080379bc100SJani Nikula 
1081379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
1082379bc100SJani Nikula 	/* We cannot write to GRP. It would overwrite individual loadgen. */
1083a1f01768SVille Syrjälä 	for (ln = 0; ln < 4; ln++) {
1084f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
1085379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
1086379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
1087e505d764SVille Syrjälä 		val |= POST_CURSOR_1(trans->entries[level].icl.dw4_post_cursor_1);
1088e505d764SVille Syrjälä 		val |= POST_CURSOR_2(trans->entries[level].icl.dw4_post_cursor_2);
1089e505d764SVille Syrjälä 		val |= CURSOR_COEFF(trans->entries[level].icl.dw4_cursor_coeff);
1090f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
1091379bc100SJani Nikula 	}
1092379bc100SJani Nikula 
1093379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
1094e6908588SVille Syrjälä 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW7_LN(0, phy));
1095379bc100SJani Nikula 	val &= ~N_SCALAR_MASK;
1096e505d764SVille Syrjälä 	val |= N_SCALAR(trans->entries[level].icl.dw7_n_scalar);
1097f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW7_GRP(phy), val);
1098379bc100SJani Nikula }
1099379bc100SJani Nikula 
1100193299adSVille Syrjälä static void icl_combo_phy_set_signal_levels(struct intel_encoder *encoder,
1101193299adSVille Syrjälä 					    const struct intel_crtc_state *crtc_state)
1102379bc100SJani Nikula {
1103379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1104dc867bc7SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1105379bc100SJani Nikula 	u32 val;
11065e7fe4d9SVille Syrjälä 	int ln;
1107379bc100SJani Nikula 
1108379bc100SJani Nikula 	/*
1109379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
1110379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
1111379bc100SJani Nikula 	 * else clear to 0b.
1112379bc100SJani Nikula 	 */
1113e6908588SVille Syrjälä 	val = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_LN(0, phy));
1114a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1115379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
1116379bc100SJani Nikula 	else
1117379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
1118f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy), val);
1119379bc100SJani Nikula 
1120379bc100SJani Nikula 	/* 2. Program loadgen select */
1121379bc100SJani Nikula 	/*
1122e6908588SVille Syrjälä 	 * Program PORT_TX_DW4 depending on Bit rate and used lanes
1123379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
1124379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
1125379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
1126379bc100SJani Nikula 	 */
1127a1f01768SVille Syrjälä 	for (ln = 0; ln < 4; ln++) {
1128f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
1129379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
11305e7fe4d9SVille Syrjälä 		val |= icl_combo_phy_loadgen_select(crtc_state, ln);
1131f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
1132379bc100SJani Nikula 	}
1133379bc100SJani Nikula 
1134379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
1135f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_CL_DW5(phy));
1136379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
1137f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_CL_DW5(phy), val);
1138379bc100SJani Nikula 
1139379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
1140e6908588SVille Syrjälä 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1141379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
1142f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1143379bc100SJani Nikula 
1144379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
1145193299adSVille Syrjälä 	icl_ddi_combo_vswing_program(encoder, crtc_state);
1146379bc100SJani Nikula 
1147379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
1148e6908588SVille Syrjälä 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1149379bc100SJani Nikula 	val |= TX_TRAINING_EN;
1150f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1151379bc100SJani Nikula }
1152379bc100SJani Nikula 
1153193299adSVille Syrjälä static void icl_mg_phy_set_signal_levels(struct intel_encoder *encoder,
1154193299adSVille Syrjälä 					 const struct intel_crtc_state *crtc_state)
1155379bc100SJani Nikula {
1156379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1157f21e8b80SJosé Roberto de Souza 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
1158d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
1159e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
1160a621860aSVille Syrjälä 	int n_entries, ln;
1161a621860aSVille Syrjälä 	u32 val;
1162379bc100SJani Nikula 
116311a89708SImre Deak 	if (intel_tc_port_in_tbt_alt_mode(enc_to_dig_port(encoder)))
1164f8c6b615SVille Syrjälä 		return;
1165f8c6b615SVille Syrjälä 
1166e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1167e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
116885da0292SVille Syrjälä 		return;
1169379bc100SJani Nikula 
1170379bc100SJani Nikula 	/* Set MG_TX_LINK_PARAMS cri_use_fs32 to 0. */
1171379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1172f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port));
1173379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
1174f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port), val);
1175379bc100SJani Nikula 
1176f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port));
1177379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
1178f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port), val);
1179379bc100SJani Nikula 	}
1180379bc100SJani Nikula 
1181379bc100SJani Nikula 	/* Program MG_TX_SWINGCTRL with values from vswing table */
1182379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1183f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port));
1184379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
1185379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
1186e505d764SVille Syrjälä 			trans->entries[level].mg.cri_txdeemph_override_17_12);
1187f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port), val);
1188379bc100SJani Nikula 
1189f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port));
1190379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
1191379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
1192e505d764SVille Syrjälä 			trans->entries[level].mg.cri_txdeemph_override_17_12);
1193f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port), val);
1194379bc100SJani Nikula 	}
1195379bc100SJani Nikula 
1196379bc100SJani Nikula 	/* Program MG_TX_DRVCTRL with values from vswing table */
1197379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1198f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DRVCTRL(ln, tc_port));
1199379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1200379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
1201379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
1202e505d764SVille Syrjälä 			trans->entries[level].mg.cri_txdeemph_override_5_0) |
1203379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
1204e505d764SVille Syrjälä 				trans->entries[level].mg.cri_txdeemph_override_11_6) |
1205379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
1206f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DRVCTRL(ln, tc_port), val);
1207379bc100SJani Nikula 
1208f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DRVCTRL(ln, tc_port));
1209379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1210379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
1211379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
1212e505d764SVille Syrjälä 			trans->entries[level].mg.cri_txdeemph_override_5_0) |
1213379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
1214e505d764SVille Syrjälä 				trans->entries[level].mg.cri_txdeemph_override_11_6) |
1215379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
1216f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DRVCTRL(ln, tc_port), val);
1217379bc100SJani Nikula 
1218379bc100SJani Nikula 		/* FIXME: Program CRI_LOADGEN_SEL after the spec is updated */
1219379bc100SJani Nikula 	}
1220379bc100SJani Nikula 
1221379bc100SJani Nikula 	/*
1222379bc100SJani Nikula 	 * Program MG_CLKHUB<LN, port being used> with value from frequency table
1223379bc100SJani Nikula 	 * In case of Legacy mode on MG PHY, both TX1 and TX2 enabled so use the
1224379bc100SJani Nikula 	 * values from table for which TX1 and TX2 enabled.
1225379bc100SJani Nikula 	 */
1226379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1227f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_CLKHUB(ln, tc_port));
1228a621860aSVille Syrjälä 		if (crtc_state->port_clock < 300000)
1229379bc100SJani Nikula 			val |= CFG_LOW_RATE_LKREN_EN;
1230379bc100SJani Nikula 		else
1231379bc100SJani Nikula 			val &= ~CFG_LOW_RATE_LKREN_EN;
1232f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_CLKHUB(ln, tc_port), val);
1233379bc100SJani Nikula 	}
1234379bc100SJani Nikula 
1235379bc100SJani Nikula 	/* Program the MG_TX_DCC<LN, port being used> based on the link frequency */
1236379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1237f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DCC(ln, tc_port));
1238379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
1239a621860aSVille Syrjälä 		if (crtc_state->port_clock <= 500000) {
1240379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
1241379bc100SJani Nikula 		} else {
1242379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
1243379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
1244379bc100SJani Nikula 		}
1245f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DCC(ln, tc_port), val);
1246379bc100SJani Nikula 
1247f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DCC(ln, tc_port));
1248379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
1249a621860aSVille Syrjälä 		if (crtc_state->port_clock <= 500000) {
1250379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
1251379bc100SJani Nikula 		} else {
1252379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
1253379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
1254379bc100SJani Nikula 		}
1255f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DCC(ln, tc_port), val);
1256379bc100SJani Nikula 	}
1257379bc100SJani Nikula 
1258379bc100SJani Nikula 	/* Program MG_TX_PISO_READLOAD with values from vswing table */
1259379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1260f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
1261f7960e7fSJani Nikula 				    MG_TX1_PISO_READLOAD(ln, tc_port));
1262379bc100SJani Nikula 		val |= CRI_CALCINIT;
1263f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_PISO_READLOAD(ln, tc_port),
1264f7960e7fSJani Nikula 			       val);
1265379bc100SJani Nikula 
1266f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
1267f7960e7fSJani Nikula 				    MG_TX2_PISO_READLOAD(ln, tc_port));
1268379bc100SJani Nikula 		val |= CRI_CALCINIT;
1269f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_PISO_READLOAD(ln, tc_port),
1270f7960e7fSJani Nikula 			       val);
1271379bc100SJani Nikula 	}
1272379bc100SJani Nikula }
1273379bc100SJani Nikula 
1274193299adSVille Syrjälä static void tgl_dkl_phy_set_signal_levels(struct intel_encoder *encoder,
1275193299adSVille Syrjälä 					  const struct intel_crtc_state *crtc_state)
1276978c3e53SClinton A Taylor {
1277978c3e53SClinton A Taylor 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1278978c3e53SClinton A Taylor 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
1279d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
1280e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
1281a621860aSVille Syrjälä 	u32 val, dpcnt_mask, dpcnt_val;
1282a621860aSVille Syrjälä 	int n_entries, ln;
1283978c3e53SClinton A Taylor 
128411a89708SImre Deak 	if (intel_tc_port_in_tbt_alt_mode(enc_to_dig_port(encoder)))
1285f8c6b615SVille Syrjälä 		return;
1286f8c6b615SVille Syrjälä 
1287e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1288e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
128985da0292SVille Syrjälä 		return;
1290978c3e53SClinton A Taylor 
1291978c3e53SClinton A Taylor 	dpcnt_mask = (DKL_TX_PRESHOOT_COEFF_MASK |
1292978c3e53SClinton A Taylor 		      DKL_TX_DE_EMPAHSIS_COEFF_MASK |
1293978c3e53SClinton A Taylor 		      DKL_TX_VSWING_CONTROL_MASK);
1294247c8a73SVille Syrjälä 	dpcnt_val = DKL_TX_VSWING_CONTROL(trans->entries[level].dkl.vswing);
1295247c8a73SVille Syrjälä 	dpcnt_val |= DKL_TX_DE_EMPHASIS_COEFF(trans->entries[level].dkl.de_emphasis);
1296247c8a73SVille Syrjälä 	dpcnt_val |= DKL_TX_PRESHOOT_COEFF(trans->entries[level].dkl.preshoot);
1297978c3e53SClinton A Taylor 
1298978c3e53SClinton A Taylor 	for (ln = 0; ln < 2; ln++) {
1299f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
1300f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, ln));
1301978c3e53SClinton A Taylor 
1302f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_PMD_LANE_SUS(tc_port), 0);
13032d69c42eSJosé Roberto de Souza 
1304978c3e53SClinton A Taylor 		/* All the registers are RMW */
1305f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL0(tc_port));
1306978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
1307978c3e53SClinton A Taylor 		val |= dpcnt_val;
1308f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL0(tc_port), val);
1309978c3e53SClinton A Taylor 
1310f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL1(tc_port));
1311978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
1312978c3e53SClinton A Taylor 		val |= dpcnt_val;
1313f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL1(tc_port), val);
1314978c3e53SClinton A Taylor 
1315f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL2(tc_port));
1316978c3e53SClinton A Taylor 		val &= ~DKL_TX_DP20BITMODE;
1317f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL2(tc_port), val);
1318978c3e53SClinton A Taylor 	}
1319978c3e53SClinton A Taylor }
1320978c3e53SClinton A Taylor 
1321a621860aSVille Syrjälä static int translate_signal_level(struct intel_dp *intel_dp,
1322a621860aSVille Syrjälä 				  u8 signal_levels)
1323379bc100SJani Nikula {
13248b4f2137SPankaj Bharadiya 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1325379bc100SJani Nikula 	int i;
1326379bc100SJani Nikula 
1327379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
1328379bc100SJani Nikula 		if (index_to_dp_signal_levels[i] == signal_levels)
1329379bc100SJani Nikula 			return i;
1330379bc100SJani Nikula 	}
1331379bc100SJani Nikula 
13328b4f2137SPankaj Bharadiya 	drm_WARN(&i915->drm, 1,
13338b4f2137SPankaj Bharadiya 		 "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
1334379bc100SJani Nikula 		 signal_levels);
1335379bc100SJani Nikula 
1336379bc100SJani Nikula 	return 0;
1337379bc100SJani Nikula }
1338379bc100SJani Nikula 
13395c31e9d0SJani Nikula static int intel_ddi_dp_level(struct intel_dp *intel_dp,
13405c31e9d0SJani Nikula 			      const struct intel_crtc_state *crtc_state,
13415c31e9d0SJani Nikula 			      int lane)
1342379bc100SJani Nikula {
1343d0920a45SVille Syrjälä 	u8 train_set = intel_dp->train_set[lane];
13445c31e9d0SJani Nikula 
13455c31e9d0SJani Nikula 	if (intel_dp_is_uhbr(crtc_state)) {
13465c31e9d0SJani Nikula 		return train_set & DP_TX_FFE_PRESET_VALUE_MASK;
13475c31e9d0SJani Nikula 	} else {
1348a621860aSVille Syrjälä 		u8 signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1349379bc100SJani Nikula 						DP_TRAIN_PRE_EMPHASIS_MASK);
1350379bc100SJani Nikula 
13518b4f2137SPankaj Bharadiya 		return translate_signal_level(intel_dp, signal_levels);
1352379bc100SJani Nikula 	}
13535c31e9d0SJani Nikula }
1354379bc100SJani Nikula 
1355193299adSVille Syrjälä int intel_ddi_level(struct intel_encoder *encoder,
1356d0920a45SVille Syrjälä 		    const struct intel_crtc_state *crtc_state,
1357d0920a45SVille Syrjälä 		    int lane)
1358a046a0daSMatt Roper {
13592c63e0f9SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
13602c63e0f9SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
13612c63e0f9SVille Syrjälä 	int level, n_entries;
13622c63e0f9SVille Syrjälä 
13632c63e0f9SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
13642c63e0f9SVille Syrjälä 	if (drm_WARN_ON_ONCE(&i915->drm, !trans))
13652c63e0f9SVille Syrjälä 		return 0;
13662c63e0f9SVille Syrjälä 
1367e722ab8bSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
13683e022c1fSVille Syrjälä 		level = intel_ddi_hdmi_level(encoder, trans);
1369e722ab8bSVille Syrjälä 	else
13705c31e9d0SJani Nikula 		level = intel_ddi_dp_level(enc_to_intel_dp(encoder), crtc_state,
13715c31e9d0SJani Nikula 					   lane);
13722c63e0f9SVille Syrjälä 
13732c63e0f9SVille Syrjälä 	if (drm_WARN_ON_ONCE(&i915->drm, level >= n_entries))
13742c63e0f9SVille Syrjälä 		level = n_entries - 1;
13752c63e0f9SVille Syrjälä 
13762c63e0f9SVille Syrjälä 	return level;
1377e722ab8bSVille Syrjälä }
1378e722ab8bSVille Syrjälä 
1379e722ab8bSVille Syrjälä static void
1380e722ab8bSVille Syrjälä hsw_set_signal_levels(struct intel_encoder *encoder,
1381a621860aSVille Syrjälä 		      const struct intel_crtc_state *crtc_state)
1382fb83f72cSVille Syrjälä {
1383fb83f72cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1384e722ab8bSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1385d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
1386fb83f72cSVille Syrjälä 	enum port port = encoder->port;
1387fb83f72cSVille Syrjälä 	u32 signal_levels;
1388fb83f72cSVille Syrjälä 
1389e722ab8bSVille Syrjälä 	if (has_iboost(dev_priv))
1390e722ab8bSVille Syrjälä 		skl_ddi_set_iboost(encoder, crtc_state, level);
1391e722ab8bSVille Syrjälä 
1392e722ab8bSVille Syrjälä 	/* HDMI ignores the rest */
1393e722ab8bSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1394e722ab8bSVille Syrjälä 		return;
1395e722ab8bSVille Syrjälä 
1396fb83f72cSVille Syrjälä 	signal_levels = DDI_BUF_TRANS_SELECT(level);
1397fb83f72cSVille Syrjälä 
1398fb83f72cSVille Syrjälä 	drm_dbg_kms(&dev_priv->drm, "Using signal levels %08x\n",
1399fb83f72cSVille Syrjälä 		    signal_levels);
1400fb83f72cSVille Syrjälä 
1401fb83f72cSVille Syrjälä 	intel_dp->DP &= ~DDI_BUF_EMP_MASK;
1402fb83f72cSVille Syrjälä 	intel_dp->DP |= signal_levels;
1403fb83f72cSVille Syrjälä 
1404fb83f72cSVille Syrjälä 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
1405fb83f72cSVille Syrjälä 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
1406379bc100SJani Nikula }
1407379bc100SJani Nikula 
14084da27d5dSLucas De Marchi static void _icl_ddi_enable_clock(struct drm_i915_private *i915, i915_reg_t reg,
14099c6a5c35SVille Syrjälä 				  u32 clk_sel_mask, u32 clk_sel, u32 clk_off)
14109c6a5c35SVille Syrjälä {
14119c6a5c35SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
14129c6a5c35SVille Syrjälä 
14139c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, clk_sel_mask, clk_sel);
14149c6a5c35SVille Syrjälä 
14159c6a5c35SVille Syrjälä 	/*
14169c6a5c35SVille Syrjälä 	 * "This step and the step before must be
14179c6a5c35SVille Syrjälä 	 *  done with separate register writes."
14189c6a5c35SVille Syrjälä 	 */
14199c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, clk_off, 0);
14209c6a5c35SVille Syrjälä 
14219c6a5c35SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
14229c6a5c35SVille Syrjälä }
14239c6a5c35SVille Syrjälä 
14244da27d5dSLucas De Marchi static void _icl_ddi_disable_clock(struct drm_i915_private *i915, i915_reg_t reg,
14259c6a5c35SVille Syrjälä 				   u32 clk_off)
14269c6a5c35SVille Syrjälä {
14279c6a5c35SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
14289c6a5c35SVille Syrjälä 
14299c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, 0, clk_off);
14309c6a5c35SVille Syrjälä 
14319c6a5c35SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
14329c6a5c35SVille Syrjälä }
14339c6a5c35SVille Syrjälä 
14344da27d5dSLucas De Marchi static bool _icl_ddi_is_clock_enabled(struct drm_i915_private *i915, i915_reg_t reg,
14350fbd8694SVille Syrjälä 				      u32 clk_off)
14360fbd8694SVille Syrjälä {
14370fbd8694SVille Syrjälä 	return !(intel_de_read(i915, reg) & clk_off);
14380fbd8694SVille Syrjälä }
14390fbd8694SVille Syrjälä 
1440351221ffSVille Syrjälä static struct intel_shared_dpll *
14414da27d5dSLucas De Marchi _icl_ddi_get_pll(struct drm_i915_private *i915, i915_reg_t reg,
1442351221ffSVille Syrjälä 		 u32 clk_sel_mask, u32 clk_sel_shift)
1443351221ffSVille Syrjälä {
1444351221ffSVille Syrjälä 	enum intel_dpll_id id;
1445351221ffSVille Syrjälä 
1446351221ffSVille Syrjälä 	id = (intel_de_read(i915, reg) & clk_sel_mask) >> clk_sel_shift;
1447351221ffSVille Syrjälä 
1448351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1449351221ffSVille Syrjälä }
1450351221ffSVille Syrjälä 
145140b316d4SVille Syrjälä static void adls_ddi_enable_clock(struct intel_encoder *encoder,
145240b316d4SVille Syrjälä 				  const struct intel_crtc_state *crtc_state)
145340b316d4SVille Syrjälä {
145440b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
145540b316d4SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
145640b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
145740b316d4SVille Syrjälä 
145840b316d4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
145940b316d4SVille Syrjälä 		return;
146040b316d4SVille Syrjälä 
14614da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
146240b316d4SVille Syrjälä 			      ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
146340b316d4SVille Syrjälä 			      pll->info->id << ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy),
146440b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
146540b316d4SVille Syrjälä }
146640b316d4SVille Syrjälä 
146740b316d4SVille Syrjälä static void adls_ddi_disable_clock(struct intel_encoder *encoder)
146840b316d4SVille Syrjälä {
146940b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
147040b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
147140b316d4SVille Syrjälä 
14724da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
147340b316d4SVille Syrjälä 			       ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
147440b316d4SVille Syrjälä }
147540b316d4SVille Syrjälä 
14760fbd8694SVille Syrjälä static bool adls_ddi_is_clock_enabled(struct intel_encoder *encoder)
14770fbd8694SVille Syrjälä {
14780fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
14790fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
14800fbd8694SVille Syrjälä 
14814da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ADLS_DPCLKA_CFGCR(phy),
14820fbd8694SVille Syrjälä 					 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
14830fbd8694SVille Syrjälä }
14840fbd8694SVille Syrjälä 
1485351221ffSVille Syrjälä static struct intel_shared_dpll *adls_ddi_get_pll(struct intel_encoder *encoder)
1486351221ffSVille Syrjälä {
1487351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1488351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1489351221ffSVille Syrjälä 
14904da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ADLS_DPCLKA_CFGCR(phy),
1491351221ffSVille Syrjälä 				ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
1492351221ffSVille Syrjälä 				ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy));
1493351221ffSVille Syrjälä }
1494351221ffSVille Syrjälä 
149540b316d4SVille Syrjälä static void rkl_ddi_enable_clock(struct intel_encoder *encoder,
149640b316d4SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
149740b316d4SVille Syrjälä {
149840b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
149940b316d4SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
150040b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
150140b316d4SVille Syrjälä 
150240b316d4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
150340b316d4SVille Syrjälä 		return;
150440b316d4SVille Syrjälä 
15054da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
150640b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
150740b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
150840b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
150940b316d4SVille Syrjälä }
151040b316d4SVille Syrjälä 
151140b316d4SVille Syrjälä static void rkl_ddi_disable_clock(struct intel_encoder *encoder)
151240b316d4SVille Syrjälä {
151340b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
151440b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
151540b316d4SVille Syrjälä 
15164da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
151740b316d4SVille Syrjälä 			       RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
151840b316d4SVille Syrjälä }
151940b316d4SVille Syrjälä 
15200fbd8694SVille Syrjälä static bool rkl_ddi_is_clock_enabled(struct intel_encoder *encoder)
15210fbd8694SVille Syrjälä {
15220fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15230fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
15240fbd8694SVille Syrjälä 
15254da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
15260fbd8694SVille Syrjälä 					 RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
15270fbd8694SVille Syrjälä }
15280fbd8694SVille Syrjälä 
1529351221ffSVille Syrjälä static struct intel_shared_dpll *rkl_ddi_get_pll(struct intel_encoder *encoder)
1530351221ffSVille Syrjälä {
1531351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1532351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1533351221ffSVille Syrjälä 
15344da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1535351221ffSVille Syrjälä 				RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1536351221ffSVille Syrjälä 				RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1537351221ffSVille Syrjälä }
1538351221ffSVille Syrjälä 
153935bb6b1aSVille Syrjälä static void dg1_ddi_enable_clock(struct intel_encoder *encoder,
154011ffe972SLucas De Marchi 				 const struct intel_crtc_state *crtc_state)
154111ffe972SLucas De Marchi {
154297a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15439c6a5c35SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
154497a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
154511ffe972SLucas De Marchi 
154697a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1547f67a008eSVille Syrjälä 		return;
1548f67a008eSVille Syrjälä 
154911ffe972SLucas De Marchi 	/*
155011ffe972SLucas De Marchi 	 * If we fail this, something went very wrong: first 2 PLLs should be
155111ffe972SLucas De Marchi 	 * used by first 2 phys and last 2 PLLs by last phys
155211ffe972SLucas De Marchi 	 */
155397a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm,
155411ffe972SLucas De Marchi 			(pll->info->id < DPLL_ID_DG1_DPLL2 && phy >= PHY_C) ||
155511ffe972SLucas De Marchi 			(pll->info->id >= DPLL_ID_DG1_DPLL2 && phy < PHY_C)))
155611ffe972SLucas De Marchi 		return;
155711ffe972SLucas De Marchi 
15584da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
15597815ed88SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
15609c6a5c35SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
15619c6a5c35SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
156211ffe972SLucas De Marchi }
156311ffe972SLucas De Marchi 
156435bb6b1aSVille Syrjälä static void dg1_ddi_disable_clock(struct intel_encoder *encoder)
156535bb6b1aSVille Syrjälä {
156697a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
156797a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
156835bb6b1aSVille Syrjälä 
15694da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
15709c6a5c35SVille Syrjälä 			       DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
157135bb6b1aSVille Syrjälä }
157235bb6b1aSVille Syrjälä 
15730fbd8694SVille Syrjälä static bool dg1_ddi_is_clock_enabled(struct intel_encoder *encoder)
15740fbd8694SVille Syrjälä {
15750fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15760fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
15770fbd8694SVille Syrjälä 
15784da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, DG1_DPCLKA_CFGCR0(phy),
15790fbd8694SVille Syrjälä 					 DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
15800fbd8694SVille Syrjälä }
15810fbd8694SVille Syrjälä 
1582351221ffSVille Syrjälä static struct intel_shared_dpll *dg1_ddi_get_pll(struct intel_encoder *encoder)
1583351221ffSVille Syrjälä {
1584351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1585351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
15863352d86dSJosé Roberto de Souza 	enum intel_dpll_id id;
15873352d86dSJosé Roberto de Souza 	u32 val;
1588351221ffSVille Syrjälä 
15893352d86dSJosé Roberto de Souza 	val = intel_de_read(i915, DG1_DPCLKA_CFGCR0(phy));
15903352d86dSJosé Roberto de Souza 	val &= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
15913352d86dSJosé Roberto de Souza 	val >>= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
15923352d86dSJosé Roberto de Souza 	id = val;
15933352d86dSJosé Roberto de Souza 
15943352d86dSJosé Roberto de Souza 	/*
15953352d86dSJosé Roberto de Souza 	 * _DG1_DPCLKA0_CFGCR0 maps between DPLL 0 and 1 with one bit for phy A
15963352d86dSJosé Roberto de Souza 	 * and B while _DG1_DPCLKA1_CFGCR0 maps between DPLL 2 and 3 with one
15973352d86dSJosé Roberto de Souza 	 * bit for phy C and D.
15983352d86dSJosé Roberto de Souza 	 */
15993352d86dSJosé Roberto de Souza 	if (phy >= PHY_C)
16003352d86dSJosé Roberto de Souza 		id += DPLL_ID_DG1_DPLL2;
16013352d86dSJosé Roberto de Souza 
16023352d86dSJosé Roberto de Souza 	return intel_get_shared_dpll_by_id(i915, id);
1603351221ffSVille Syrjälä }
1604351221ffSVille Syrjälä 
160536ecb0ecSVille Syrjälä static void icl_ddi_combo_enable_clock(struct intel_encoder *encoder,
1606379bc100SJani Nikula 				       const struct intel_crtc_state *crtc_state)
1607379bc100SJani Nikula {
160897a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
16099c6a5c35SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
161097a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1611cd803bb4SMatt Roper 
161297a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1613f67a008eSVille Syrjälä 		return;
1614f67a008eSVille Syrjälä 
16154da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
161640b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
161740b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
161840b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1619379bc100SJani Nikula }
1620379bc100SJani Nikula 
162136ecb0ecSVille Syrjälä static void icl_ddi_combo_disable_clock(struct intel_encoder *encoder)
1622379bc100SJani Nikula {
162397a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
162497a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1625379bc100SJani Nikula 
16264da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
162740b316d4SVille Syrjälä 			       ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1628379bc100SJani Nikula }
1629379bc100SJani Nikula 
16300fbd8694SVille Syrjälä static bool icl_ddi_combo_is_clock_enabled(struct intel_encoder *encoder)
16310fbd8694SVille Syrjälä {
16320fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
16330fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
16340fbd8694SVille Syrjälä 
16354da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
16360fbd8694SVille Syrjälä 					 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
16370fbd8694SVille Syrjälä }
16380fbd8694SVille Syrjälä 
1639351221ffSVille Syrjälä struct intel_shared_dpll *icl_ddi_combo_get_pll(struct intel_encoder *encoder)
1640351221ffSVille Syrjälä {
1641351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1642351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1643351221ffSVille Syrjälä 
16444da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1645351221ffSVille Syrjälä 				ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1646351221ffSVille Syrjälä 				ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1647351221ffSVille Syrjälä }
1648351221ffSVille Syrjälä 
164936ecb0ecSVille Syrjälä static void jsl_ddi_tc_enable_clock(struct intel_encoder *encoder,
1650379bc100SJani Nikula 				    const struct intel_crtc_state *crtc_state)
1651379bc100SJani Nikula {
165236ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1653379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
165436ecb0ecSVille Syrjälä 	enum port port = encoder->port;
1655379bc100SJani Nikula 
165636ecb0ecSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1657379bc100SJani Nikula 		return;
1658379bc100SJani Nikula 
1659c2052d6eSJosé Roberto de Souza 	/*
166036ecb0ecSVille Syrjälä 	 * "For DDIC and DDID, program DDI_CLK_SEL to map the MG clock to the port.
166136ecb0ecSVille Syrjälä 	 *  MG does not exist, but the programming is required to ungate DDIC and DDID."
1662c2052d6eSJosé Roberto de Souza 	 */
166336ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_MG);
166436ecb0ecSVille Syrjälä 
166536ecb0ecSVille Syrjälä 	icl_ddi_combo_enable_clock(encoder, crtc_state);
1666379bc100SJani Nikula }
1667379bc100SJani Nikula 
166836ecb0ecSVille Syrjälä static void jsl_ddi_tc_disable_clock(struct intel_encoder *encoder)
1669379bc100SJani Nikula {
167036ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1671379bc100SJani Nikula 	enum port port = encoder->port;
1672379bc100SJani Nikula 
167336ecb0ecSVille Syrjälä 	icl_ddi_combo_disable_clock(encoder);
167436ecb0ecSVille Syrjälä 
167536ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1676379bc100SJani Nikula }
167736ecb0ecSVille Syrjälä 
16780fbd8694SVille Syrjälä static bool jsl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
16790fbd8694SVille Syrjälä {
16800fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
16810fbd8694SVille Syrjälä 	enum port port = encoder->port;
16820fbd8694SVille Syrjälä 	u32 tmp;
16830fbd8694SVille Syrjälä 
16840fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
16850fbd8694SVille Syrjälä 
16860fbd8694SVille Syrjälä 	if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
16870fbd8694SVille Syrjälä 		return false;
16880fbd8694SVille Syrjälä 
16890fbd8694SVille Syrjälä 	return icl_ddi_combo_is_clock_enabled(encoder);
16900fbd8694SVille Syrjälä }
16910fbd8694SVille Syrjälä 
169236ecb0ecSVille Syrjälä static void icl_ddi_tc_enable_clock(struct intel_encoder *encoder,
169336ecb0ecSVille Syrjälä 				    const struct intel_crtc_state *crtc_state)
169436ecb0ecSVille Syrjälä {
169536ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
169636ecb0ecSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
169736ecb0ecSVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
169836ecb0ecSVille Syrjälä 	enum port port = encoder->port;
169936ecb0ecSVille Syrjälä 
170036ecb0ecSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
170136ecb0ecSVille Syrjälä 		return;
170236ecb0ecSVille Syrjälä 
170336ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port),
170436ecb0ecSVille Syrjälä 		       icl_pll_to_ddi_clk_sel(encoder, crtc_state));
170536ecb0ecSVille Syrjälä 
170636ecb0ecSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
170736ecb0ecSVille Syrjälä 
170836ecb0ecSVille Syrjälä 	intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
170936ecb0ecSVille Syrjälä 		     ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port), 0);
171036ecb0ecSVille Syrjälä 
171136ecb0ecSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
171236ecb0ecSVille Syrjälä }
171336ecb0ecSVille Syrjälä 
171436ecb0ecSVille Syrjälä static void icl_ddi_tc_disable_clock(struct intel_encoder *encoder)
171536ecb0ecSVille Syrjälä {
171636ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
171736ecb0ecSVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
171836ecb0ecSVille Syrjälä 	enum port port = encoder->port;
171936ecb0ecSVille Syrjälä 
172036ecb0ecSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
172136ecb0ecSVille Syrjälä 
172236ecb0ecSVille Syrjälä 	intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
172336ecb0ecSVille Syrjälä 		     0, ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
172436ecb0ecSVille Syrjälä 
172536ecb0ecSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
172636ecb0ecSVille Syrjälä 
172736ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1728379bc100SJani Nikula }
1729379bc100SJani Nikula 
17300fbd8694SVille Syrjälä static bool icl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
17310fbd8694SVille Syrjälä {
17320fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
17330fbd8694SVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
17340fbd8694SVille Syrjälä 	enum port port = encoder->port;
17350fbd8694SVille Syrjälä 	u32 tmp;
17360fbd8694SVille Syrjälä 
17370fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
17380fbd8694SVille Syrjälä 
17390fbd8694SVille Syrjälä 	if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
17400fbd8694SVille Syrjälä 		return false;
17410fbd8694SVille Syrjälä 
17420fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, ICL_DPCLKA_CFGCR0);
17430fbd8694SVille Syrjälä 
17440fbd8694SVille Syrjälä 	return !(tmp & ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
17450fbd8694SVille Syrjälä }
17460fbd8694SVille Syrjälä 
1747351221ffSVille Syrjälä static struct intel_shared_dpll *icl_ddi_tc_get_pll(struct intel_encoder *encoder)
1748351221ffSVille Syrjälä {
1749351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1750351221ffSVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
1751351221ffSVille Syrjälä 	enum port port = encoder->port;
1752351221ffSVille Syrjälä 	enum intel_dpll_id id;
1753351221ffSVille Syrjälä 	u32 tmp;
1754351221ffSVille Syrjälä 
1755351221ffSVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
1756351221ffSVille Syrjälä 
1757351221ffSVille Syrjälä 	switch (tmp & DDI_CLK_SEL_MASK) {
1758351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_162:
1759351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_270:
1760351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_540:
1761351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_810:
1762351221ffSVille Syrjälä 		id = DPLL_ID_ICL_TBTPLL;
1763351221ffSVille Syrjälä 		break;
1764351221ffSVille Syrjälä 	case DDI_CLK_SEL_MG:
1765351221ffSVille Syrjälä 		id = icl_tc_port_to_pll_id(tc_port);
1766351221ffSVille Syrjälä 		break;
1767351221ffSVille Syrjälä 	default:
1768351221ffSVille Syrjälä 		MISSING_CASE(tmp);
1769351221ffSVille Syrjälä 		fallthrough;
1770351221ffSVille Syrjälä 	case DDI_CLK_SEL_NONE:
1771351221ffSVille Syrjälä 		return NULL;
1772351221ffSVille Syrjälä 	}
1773351221ffSVille Syrjälä 
1774351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1775351221ffSVille Syrjälä }
1776351221ffSVille Syrjälä 
1777351221ffSVille Syrjälä static struct intel_shared_dpll *bxt_ddi_get_pll(struct intel_encoder *encoder)
1778351221ffSVille Syrjälä {
1779351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1780351221ffSVille Syrjälä 	enum intel_dpll_id id;
1781351221ffSVille Syrjälä 
1782351221ffSVille Syrjälä 	switch (encoder->port) {
1783351221ffSVille Syrjälä 	case PORT_A:
1784351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL0;
1785351221ffSVille Syrjälä 		break;
1786351221ffSVille Syrjälä 	case PORT_B:
1787351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL1;
1788351221ffSVille Syrjälä 		break;
1789351221ffSVille Syrjälä 	case PORT_C:
1790351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL2;
1791351221ffSVille Syrjälä 		break;
1792351221ffSVille Syrjälä 	default:
1793351221ffSVille Syrjälä 		MISSING_CASE(encoder->port);
1794351221ffSVille Syrjälä 		return NULL;
1795351221ffSVille Syrjälä 	}
1796351221ffSVille Syrjälä 
1797351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1798351221ffSVille Syrjälä }
1799351221ffSVille Syrjälä 
180038e31f1aSVille Syrjälä static void skl_ddi_enable_clock(struct intel_encoder *encoder,
180138e31f1aSVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
180238e31f1aSVille Syrjälä {
180338e31f1aSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
180438e31f1aSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
180538e31f1aSVille Syrjälä 	enum port port = encoder->port;
180638e31f1aSVille Syrjälä 
180738e31f1aSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
180838e31f1aSVille Syrjälä 		return;
180938e31f1aSVille Syrjälä 
181038e31f1aSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
181138e31f1aSVille Syrjälä 
18127815ed88SVille Syrjälä 	intel_de_rmw(i915, DPLL_CTRL2,
18137815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_OFF(port) |
18147815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_SEL_MASK(port),
18157815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_SEL(pll->info->id, port) |
181638e31f1aSVille Syrjälä 		     DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
181738e31f1aSVille Syrjälä 
181838e31f1aSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
181938e31f1aSVille Syrjälä }
182038e31f1aSVille Syrjälä 
182138e31f1aSVille Syrjälä static void skl_ddi_disable_clock(struct intel_encoder *encoder)
182238e31f1aSVille Syrjälä {
182338e31f1aSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
182438e31f1aSVille Syrjälä 	enum port port = encoder->port;
182538e31f1aSVille Syrjälä 
1826be317ca0SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
1827be317ca0SVille Syrjälä 
18287815ed88SVille Syrjälä 	intel_de_rmw(i915, DPLL_CTRL2,
18297815ed88SVille Syrjälä 		     0, DPLL_CTRL2_DDI_CLK_OFF(port));
1830be317ca0SVille Syrjälä 
1831be317ca0SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
183238e31f1aSVille Syrjälä }
183338e31f1aSVille Syrjälä 
18340fbd8694SVille Syrjälä static bool skl_ddi_is_clock_enabled(struct intel_encoder *encoder)
18350fbd8694SVille Syrjälä {
18360fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
18370fbd8694SVille Syrjälä 	enum port port = encoder->port;
18380fbd8694SVille Syrjälä 
18390fbd8694SVille Syrjälä 	/*
18400fbd8694SVille Syrjälä 	 * FIXME Not sure if the override affects both
18410fbd8694SVille Syrjälä 	 * the PLL selection and the CLK_OFF bit.
18420fbd8694SVille Syrjälä 	 */
18430fbd8694SVille Syrjälä 	return !(intel_de_read(i915, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_OFF(port));
18440fbd8694SVille Syrjälä }
18450fbd8694SVille Syrjälä 
1846351221ffSVille Syrjälä static struct intel_shared_dpll *skl_ddi_get_pll(struct intel_encoder *encoder)
1847351221ffSVille Syrjälä {
1848351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1849351221ffSVille Syrjälä 	enum port port = encoder->port;
1850351221ffSVille Syrjälä 	enum intel_dpll_id id;
1851351221ffSVille Syrjälä 	u32 tmp;
1852351221ffSVille Syrjälä 
1853351221ffSVille Syrjälä 	tmp = intel_de_read(i915, DPLL_CTRL2);
1854351221ffSVille Syrjälä 
1855351221ffSVille Syrjälä 	/*
1856351221ffSVille Syrjälä 	 * FIXME Not sure if the override affects both
1857351221ffSVille Syrjälä 	 * the PLL selection and the CLK_OFF bit.
1858351221ffSVille Syrjälä 	 */
1859351221ffSVille Syrjälä 	if ((tmp & DPLL_CTRL2_DDI_SEL_OVERRIDE(port)) == 0)
1860351221ffSVille Syrjälä 		return NULL;
1861351221ffSVille Syrjälä 
1862351221ffSVille Syrjälä 	id = (tmp & DPLL_CTRL2_DDI_CLK_SEL_MASK(port)) >>
1863351221ffSVille Syrjälä 		DPLL_CTRL2_DDI_CLK_SEL_SHIFT(port);
1864351221ffSVille Syrjälä 
1865351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1866351221ffSVille Syrjälä }
1867351221ffSVille Syrjälä 
1868d135368dSVille Syrjälä void hsw_ddi_enable_clock(struct intel_encoder *encoder,
1869d135368dSVille Syrjälä 			  const struct intel_crtc_state *crtc_state)
1870d135368dSVille Syrjälä {
1871d135368dSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1872d135368dSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1873d135368dSVille Syrjälä 	enum port port = encoder->port;
1874d135368dSVille Syrjälä 
1875d135368dSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1876d135368dSVille Syrjälä 		return;
1877d135368dSVille Syrjälä 
1878d135368dSVille Syrjälä 	intel_de_write(i915, PORT_CLK_SEL(port), hsw_pll_to_ddi_pll_sel(pll));
1879d135368dSVille Syrjälä }
1880d135368dSVille Syrjälä 
1881d135368dSVille Syrjälä void hsw_ddi_disable_clock(struct intel_encoder *encoder)
1882d135368dSVille Syrjälä {
1883d135368dSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1884d135368dSVille Syrjälä 	enum port port = encoder->port;
1885d135368dSVille Syrjälä 
1886d135368dSVille Syrjälä 	intel_de_write(i915, PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
1887d135368dSVille Syrjälä }
1888d135368dSVille Syrjälä 
18890fbd8694SVille Syrjälä bool hsw_ddi_is_clock_enabled(struct intel_encoder *encoder)
18900fbd8694SVille Syrjälä {
18910fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
18920fbd8694SVille Syrjälä 	enum port port = encoder->port;
18930fbd8694SVille Syrjälä 
18940fbd8694SVille Syrjälä 	return intel_de_read(i915, PORT_CLK_SEL(port)) != PORT_CLK_SEL_NONE;
18950fbd8694SVille Syrjälä }
18960fbd8694SVille Syrjälä 
1897351221ffSVille Syrjälä static struct intel_shared_dpll *hsw_ddi_get_pll(struct intel_encoder *encoder)
1898351221ffSVille Syrjälä {
1899351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1900351221ffSVille Syrjälä 	enum port port = encoder->port;
1901351221ffSVille Syrjälä 	enum intel_dpll_id id;
1902351221ffSVille Syrjälä 	u32 tmp;
1903351221ffSVille Syrjälä 
1904351221ffSVille Syrjälä 	tmp = intel_de_read(i915, PORT_CLK_SEL(port));
1905351221ffSVille Syrjälä 
1906351221ffSVille Syrjälä 	switch (tmp & PORT_CLK_SEL_MASK) {
1907351221ffSVille Syrjälä 	case PORT_CLK_SEL_WRPLL1:
1908351221ffSVille Syrjälä 		id = DPLL_ID_WRPLL1;
1909351221ffSVille Syrjälä 		break;
1910351221ffSVille Syrjälä 	case PORT_CLK_SEL_WRPLL2:
1911351221ffSVille Syrjälä 		id = DPLL_ID_WRPLL2;
1912351221ffSVille Syrjälä 		break;
1913351221ffSVille Syrjälä 	case PORT_CLK_SEL_SPLL:
1914351221ffSVille Syrjälä 		id = DPLL_ID_SPLL;
1915351221ffSVille Syrjälä 		break;
1916351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_810:
1917351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_810;
1918351221ffSVille Syrjälä 		break;
1919351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_1350:
1920351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_1350;
1921351221ffSVille Syrjälä 		break;
1922351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_2700:
1923351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_2700;
1924351221ffSVille Syrjälä 		break;
1925351221ffSVille Syrjälä 	default:
1926351221ffSVille Syrjälä 		MISSING_CASE(tmp);
1927351221ffSVille Syrjälä 		fallthrough;
1928351221ffSVille Syrjälä 	case PORT_CLK_SEL_NONE:
1929351221ffSVille Syrjälä 		return NULL;
1930351221ffSVille Syrjälä 	}
1931351221ffSVille Syrjälä 
1932351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1933351221ffSVille Syrjälä }
1934351221ffSVille Syrjälä 
1935c133df69SVille Syrjälä void intel_ddi_enable_clock(struct intel_encoder *encoder,
1936c133df69SVille Syrjälä 			    const struct intel_crtc_state *crtc_state)
1937c133df69SVille Syrjälä {
1938c133df69SVille Syrjälä 	if (encoder->enable_clock)
1939c133df69SVille Syrjälä 		encoder->enable_clock(encoder, crtc_state);
1940c133df69SVille Syrjälä }
1941c133df69SVille Syrjälä 
1942d39ef5d5SVille Syrjälä void intel_ddi_disable_clock(struct intel_encoder *encoder)
1943c133df69SVille Syrjälä {
1944c133df69SVille Syrjälä 	if (encoder->disable_clock)
1945c133df69SVille Syrjälä 		encoder->disable_clock(encoder);
1946c133df69SVille Syrjälä }
1947c133df69SVille Syrjälä 
1948aaca50efSVille Syrjälä void intel_ddi_sanitize_encoder_pll_mapping(struct intel_encoder *encoder)
1949dc1ddac6SVille Syrjälä {
195097a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1951dc1ddac6SVille Syrjälä 	u32 port_mask;
1952dc1ddac6SVille Syrjälä 	bool ddi_clk_needed;
1953dc1ddac6SVille Syrjälä 
1954dc1ddac6SVille Syrjälä 	/*
1955dc1ddac6SVille Syrjälä 	 * In case of DP MST, we sanitize the primary encoder only, not the
1956dc1ddac6SVille Syrjälä 	 * virtual ones.
1957dc1ddac6SVille Syrjälä 	 */
1958dc1ddac6SVille Syrjälä 	if (encoder->type == INTEL_OUTPUT_DP_MST)
1959dc1ddac6SVille Syrjälä 		return;
1960dc1ddac6SVille Syrjälä 
1961dc1ddac6SVille Syrjälä 	if (!encoder->base.crtc && intel_encoder_is_dp(encoder)) {
1962dc1ddac6SVille Syrjälä 		u8 pipe_mask;
1963dc1ddac6SVille Syrjälä 		bool is_mst;
1964dc1ddac6SVille Syrjälä 
1965dc1ddac6SVille Syrjälä 		intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
1966dc1ddac6SVille Syrjälä 		/*
1967dc1ddac6SVille Syrjälä 		 * In the unlikely case that BIOS enables DP in MST mode, just
1968dc1ddac6SVille Syrjälä 		 * warn since our MST HW readout is incomplete.
1969dc1ddac6SVille Syrjälä 		 */
197097a24a70SVille Syrjälä 		if (drm_WARN_ON(&i915->drm, is_mst))
1971dc1ddac6SVille Syrjälä 			return;
1972dc1ddac6SVille Syrjälä 	}
1973dc1ddac6SVille Syrjälä 
1974dc1ddac6SVille Syrjälä 	port_mask = BIT(encoder->port);
1975dc1ddac6SVille Syrjälä 	ddi_clk_needed = encoder->base.crtc;
1976dc1ddac6SVille Syrjälä 
1977dc1ddac6SVille Syrjälä 	if (encoder->type == INTEL_OUTPUT_DSI) {
1978dc1ddac6SVille Syrjälä 		struct intel_encoder *other_encoder;
1979dc1ddac6SVille Syrjälä 
1980dc1ddac6SVille Syrjälä 		port_mask = intel_dsi_encoder_ports(encoder);
1981dc1ddac6SVille Syrjälä 		/*
1982dc1ddac6SVille Syrjälä 		 * Sanity check that we haven't incorrectly registered another
1983dc1ddac6SVille Syrjälä 		 * encoder using any of the ports of this DSI encoder.
1984dc1ddac6SVille Syrjälä 		 */
198597a24a70SVille Syrjälä 		for_each_intel_encoder(&i915->drm, other_encoder) {
1986dc1ddac6SVille Syrjälä 			if (other_encoder == encoder)
1987dc1ddac6SVille Syrjälä 				continue;
1988dc1ddac6SVille Syrjälä 
198997a24a70SVille Syrjälä 			if (drm_WARN_ON(&i915->drm,
1990dc1ddac6SVille Syrjälä 					port_mask & BIT(other_encoder->port)))
1991dc1ddac6SVille Syrjälä 				return;
1992dc1ddac6SVille Syrjälä 		}
1993dc1ddac6SVille Syrjälä 		/*
1994dc1ddac6SVille Syrjälä 		 * For DSI we keep the ddi clocks gated
1995dc1ddac6SVille Syrjälä 		 * except during enable/disable sequence.
1996dc1ddac6SVille Syrjälä 		 */
1997dc1ddac6SVille Syrjälä 		ddi_clk_needed = false;
1998dc1ddac6SVille Syrjälä 	}
1999dc1ddac6SVille Syrjälä 
2000f82f2563SMatt Roper 	if (ddi_clk_needed || !encoder->is_clock_enabled ||
20010fbd8694SVille Syrjälä 	    !encoder->is_clock_enabled(encoder))
20020fbd8694SVille Syrjälä 		return;
20030fbd8694SVille Syrjälä 
20040fbd8694SVille Syrjälä 	drm_notice(&i915->drm,
20050fbd8694SVille Syrjälä 		   "[ENCODER:%d:%s] is disabled/in DSI mode with an ungated DDI clock, gate it\n",
20060fbd8694SVille Syrjälä 		   encoder->base.base.id, encoder->base.name);
20070fbd8694SVille Syrjälä 
2008dc1ddac6SVille Syrjälä 	encoder->disable_clock(encoder);
2009dc1ddac6SVille Syrjälä }
2010dc1ddac6SVille Syrjälä 
20118aaf5cbdSJosé Roberto de Souza static void
20127801f3b7SLucas De Marchi icl_program_mg_dp_mode(struct intel_digital_port *dig_port,
20133b51be4eSClinton A Taylor 		       const struct intel_crtc_state *crtc_state)
2014379bc100SJani Nikula {
20157801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
20167801f3b7SLucas De Marchi 	enum tc_port tc_port = intel_port_to_tc(dev_priv, dig_port->base.port);
20175b6a9ba9SVille Syrjälä 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
20183b51be4eSClinton A Taylor 	u32 ln0, ln1, pin_assignment;
20193b51be4eSClinton A Taylor 	u8 width;
2020379bc100SJani Nikula 
20215b6a9ba9SVille Syrjälä 	if (!intel_phy_is_tc(dev_priv, phy) ||
202211a89708SImre Deak 	    intel_tc_port_in_tbt_alt_mode(dig_port))
2023379bc100SJani Nikula 		return;
2024379bc100SJani Nikula 
2025005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2026f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2027f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
2028f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
2029f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2030f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
2031f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
2032978c3e53SClinton A Taylor 	} else {
2033f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, MG_DP_MODE(0, tc_port));
2034f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, MG_DP_MODE(1, tc_port));
2035978c3e53SClinton A Taylor 	}
2036379bc100SJani Nikula 
20374f72a8eeSKhaled Almahallawy 	ln0 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2038379bc100SJani Nikula 	ln1 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2039379bc100SJani Nikula 
20403b51be4eSClinton A Taylor 	/* DPPATC */
20417801f3b7SLucas De Marchi 	pin_assignment = intel_tc_port_get_pin_assignment_mask(dig_port);
20423b51be4eSClinton A Taylor 	width = crtc_state->lane_count;
2043379bc100SJani Nikula 
20443b51be4eSClinton A Taylor 	switch (pin_assignment) {
20453b51be4eSClinton A Taylor 	case 0x0:
20461de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
204711a89708SImre Deak 			    !intel_tc_port_in_legacy_mode(dig_port));
20483b51be4eSClinton A Taylor 		if (width == 1) {
2049379bc100SJani Nikula 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
20503b51be4eSClinton A Taylor 		} else {
20513b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20523b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2053379bc100SJani Nikula 		}
2054379bc100SJani Nikula 		break;
20553b51be4eSClinton A Taylor 	case 0x1:
20563b51be4eSClinton A Taylor 		if (width == 4) {
20573b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20583b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20593b51be4eSClinton A Taylor 		}
2060379bc100SJani Nikula 		break;
20613b51be4eSClinton A Taylor 	case 0x2:
20623b51be4eSClinton A Taylor 		if (width == 2) {
20633b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20643b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20653b51be4eSClinton A Taylor 		}
20663b51be4eSClinton A Taylor 		break;
20673b51be4eSClinton A Taylor 	case 0x3:
20683b51be4eSClinton A Taylor 	case 0x5:
20693b51be4eSClinton A Taylor 		if (width == 1) {
20703b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
20713b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
20723b51be4eSClinton A Taylor 		} else {
20733b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20743b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20753b51be4eSClinton A Taylor 		}
20763b51be4eSClinton A Taylor 		break;
20773b51be4eSClinton A Taylor 	case 0x4:
20783b51be4eSClinton A Taylor 	case 0x6:
20793b51be4eSClinton A Taylor 		if (width == 1) {
20803b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
20813b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
20823b51be4eSClinton A Taylor 		} else {
20833b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20843b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20853b51be4eSClinton A Taylor 		}
20863b51be4eSClinton A Taylor 		break;
2087379bc100SJani Nikula 	default:
20883b51be4eSClinton A Taylor 		MISSING_CASE(pin_assignment);
2089379bc100SJani Nikula 	}
2090379bc100SJani Nikula 
2091005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2092f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2093f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
2094f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln0);
2095f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2096f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
2097f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln1);
2098978c3e53SClinton A Taylor 	} else {
2099f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(0, tc_port), ln0);
2100f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(1, tc_port), ln1);
2101379bc100SJani Nikula 	}
2102978c3e53SClinton A Taylor }
2103379bc100SJani Nikula 
2104ef79fafeSVille Syrjälä static enum transcoder
2105ef79fafeSVille Syrjälä tgl_dp_tp_transcoder(const struct intel_crtc_state *crtc_state)
2106ef79fafeSVille Syrjälä {
2107ef79fafeSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
2108ef79fafeSVille Syrjälä 		return crtc_state->mst_master_transcoder;
2109ef79fafeSVille Syrjälä 	else
2110ef79fafeSVille Syrjälä 		return crtc_state->cpu_transcoder;
2111ef79fafeSVille Syrjälä }
2112ef79fafeSVille Syrjälä 
2113ef79fafeSVille Syrjälä i915_reg_t dp_tp_ctl_reg(struct intel_encoder *encoder,
2114ef79fafeSVille Syrjälä 			 const struct intel_crtc_state *crtc_state)
2115ef79fafeSVille Syrjälä {
2116ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2117ef79fafeSVille Syrjälä 
2118005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
2119ef79fafeSVille Syrjälä 		return TGL_DP_TP_CTL(tgl_dp_tp_transcoder(crtc_state));
2120ef79fafeSVille Syrjälä 	else
2121ef79fafeSVille Syrjälä 		return DP_TP_CTL(encoder->port);
2122ef79fafeSVille Syrjälä }
2123ef79fafeSVille Syrjälä 
2124ef79fafeSVille Syrjälä i915_reg_t dp_tp_status_reg(struct intel_encoder *encoder,
2125ef79fafeSVille Syrjälä 			    const struct intel_crtc_state *crtc_state)
2126ef79fafeSVille Syrjälä {
2127ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2128ef79fafeSVille Syrjälä 
2129005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
2130ef79fafeSVille Syrjälä 		return TGL_DP_TP_STATUS(tgl_dp_tp_transcoder(crtc_state));
2131ef79fafeSVille Syrjälä 	else
2132ef79fafeSVille Syrjälä 		return DP_TP_STATUS(encoder->port);
2133ef79fafeSVille Syrjälä }
2134ef79fafeSVille Syrjälä 
21351639406aSManasi Navare static void intel_dp_sink_set_msa_timing_par_ignore_state(struct intel_dp *intel_dp,
21361639406aSManasi Navare 							  const struct intel_crtc_state *crtc_state,
21371639406aSManasi Navare 							  bool enable)
21381639406aSManasi Navare {
21391639406aSManasi Navare 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
21401639406aSManasi Navare 
21411639406aSManasi Navare 	if (!crtc_state->vrr.enable)
21421639406aSManasi Navare 		return;
21431639406aSManasi Navare 
21441639406aSManasi Navare 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_DOWNSPREAD_CTRL,
21451639406aSManasi Navare 			       enable ? DP_MSA_TIMING_PAR_IGNORE_EN : 0) <= 0)
21461639406aSManasi Navare 		drm_dbg_kms(&i915->drm,
21470868b1ceSVille Syrjälä 			    "Failed to %s MSA_TIMING_PAR_IGNORE in the sink\n",
21480868b1ceSVille Syrjälä 			    enabledisable(enable));
21491639406aSManasi Navare }
21501639406aSManasi Navare 
2151379bc100SJani Nikula static void intel_dp_sink_set_fec_ready(struct intel_dp *intel_dp,
2152379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
2153379bc100SJani Nikula {
215447bdb1caSJani Nikula 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
215547bdb1caSJani Nikula 
2156379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2157379bc100SJani Nikula 		return;
2158379bc100SJani Nikula 
2159379bc100SJani Nikula 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_FEC_CONFIGURATION, DP_FEC_READY) <= 0)
216047bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
216147bdb1caSJani Nikula 			    "Failed to set FEC_READY in the sink\n");
2162379bc100SJani Nikula }
2163379bc100SJani Nikula 
2164379bc100SJani Nikula static void intel_ddi_enable_fec(struct intel_encoder *encoder,
2165379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
2166379bc100SJani Nikula {
2167379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
21684444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
2169379bc100SJani Nikula 	u32 val;
2170379bc100SJani Nikula 
2171379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2172379bc100SJani Nikula 		return;
2173379bc100SJani Nikula 
2174b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
2175ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2176379bc100SJani Nikula 	val |= DP_TP_CTL_FEC_ENABLE;
2177ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2178379bc100SJani Nikula }
2179379bc100SJani Nikula 
2180379bc100SJani Nikula static void intel_ddi_disable_fec_state(struct intel_encoder *encoder,
2181379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
2182379bc100SJani Nikula {
2183379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
21844444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
2185379bc100SJani Nikula 	u32 val;
2186379bc100SJani Nikula 
2187379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2188379bc100SJani Nikula 		return;
2189379bc100SJani Nikula 
2190b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
2191ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2192379bc100SJani Nikula 	val &= ~DP_TP_CTL_FEC_ENABLE;
2193ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2194ef79fafeSVille Syrjälä 	intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2195379bc100SJani Nikula }
2196379bc100SJani Nikula 
21975cdf706fSVille Syrjälä static void intel_ddi_power_up_lanes(struct intel_encoder *encoder,
21985cdf706fSVille Syrjälä 				     const struct intel_crtc_state *crtc_state)
21995cdf706fSVille Syrjälä {
22005cdf706fSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
22015cdf706fSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
22025cdf706fSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
22035cdf706fSVille Syrjälä 
22045cdf706fSVille Syrjälä 	if (intel_phy_is_combo(i915, phy)) {
22055cdf706fSVille Syrjälä 		bool lane_reversal =
22065cdf706fSVille Syrjälä 			dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
22075cdf706fSVille Syrjälä 
22085cdf706fSVille Syrjälä 		intel_combo_phy_power_up_lanes(i915, phy, false,
22095cdf706fSVille Syrjälä 					       crtc_state->lane_count,
22105cdf706fSVille Syrjälä 					       lane_reversal);
22115cdf706fSVille Syrjälä 	}
22125cdf706fSVille Syrjälä }
22135cdf706fSVille Syrjälä 
2214f6864b27SJani Nikula /* Splitter enable for eDP MSO is limited to certain pipes. */
2215f6864b27SJani Nikula static u8 intel_ddi_splitter_pipe_mask(struct drm_i915_private *i915)
2216f6864b27SJani Nikula {
2217f6864b27SJani Nikula 	if (IS_ALDERLAKE_P(i915))
2218f6864b27SJani Nikula 		return BIT(PIPE_A) | BIT(PIPE_B);
2219f6864b27SJani Nikula 	else
2220f6864b27SJani Nikula 		return BIT(PIPE_A);
2221f6864b27SJani Nikula }
2222f6864b27SJani Nikula 
22235b616a29SJani Nikula static void intel_ddi_mso_get_config(struct intel_encoder *encoder,
22245b616a29SJani Nikula 				     struct intel_crtc_state *pipe_config)
22255b616a29SJani Nikula {
22265b616a29SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
22275b616a29SJani Nikula 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
22285b616a29SJani Nikula 	enum pipe pipe = crtc->pipe;
22295b616a29SJani Nikula 	u32 dss1;
22305b616a29SJani Nikula 
22315b616a29SJani Nikula 	if (!HAS_MSO(i915))
22325b616a29SJani Nikula 		return;
22335b616a29SJani Nikula 
22345b616a29SJani Nikula 	dss1 = intel_de_read(i915, ICL_PIPE_DSS_CTL1(pipe));
22355b616a29SJani Nikula 
22365b616a29SJani Nikula 	pipe_config->splitter.enable = dss1 & SPLITTER_ENABLE;
22375b616a29SJani Nikula 	if (!pipe_config->splitter.enable)
22385b616a29SJani Nikula 		return;
22395b616a29SJani Nikula 
2240f6864b27SJani Nikula 	if (drm_WARN_ON(&i915->drm, !(intel_ddi_splitter_pipe_mask(i915) & BIT(pipe)))) {
22415b616a29SJani Nikula 		pipe_config->splitter.enable = false;
22425b616a29SJani Nikula 		return;
22435b616a29SJani Nikula 	}
22445b616a29SJani Nikula 
22455b616a29SJani Nikula 	switch (dss1 & SPLITTER_CONFIGURATION_MASK) {
22465b616a29SJani Nikula 	default:
22475b616a29SJani Nikula 		drm_WARN(&i915->drm, true,
22485b616a29SJani Nikula 			 "Invalid splitter configuration, dss1=0x%08x\n", dss1);
22495b616a29SJani Nikula 		fallthrough;
22505b616a29SJani Nikula 	case SPLITTER_CONFIGURATION_2_SEGMENT:
22515b616a29SJani Nikula 		pipe_config->splitter.link_count = 2;
22525b616a29SJani Nikula 		break;
22535b616a29SJani Nikula 	case SPLITTER_CONFIGURATION_4_SEGMENT:
22545b616a29SJani Nikula 		pipe_config->splitter.link_count = 4;
22555b616a29SJani Nikula 		break;
22565b616a29SJani Nikula 	}
22575b616a29SJani Nikula 
22585b616a29SJani Nikula 	pipe_config->splitter.pixel_overlap = REG_FIELD_GET(OVERLAP_PIXELS_MASK, dss1);
22595b616a29SJani Nikula }
22605b616a29SJani Nikula 
2261bc71194eSJani Nikula static void intel_ddi_mso_configure(const struct intel_crtc_state *crtc_state)
2262bc71194eSJani Nikula {
2263bc71194eSJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2264bc71194eSJani Nikula 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2265bc71194eSJani Nikula 	enum pipe pipe = crtc->pipe;
2266bc71194eSJani Nikula 	u32 dss1 = 0;
2267bc71194eSJani Nikula 
2268bc71194eSJani Nikula 	if (!HAS_MSO(i915))
2269bc71194eSJani Nikula 		return;
2270bc71194eSJani Nikula 
2271bc71194eSJani Nikula 	if (crtc_state->splitter.enable) {
2272bc71194eSJani Nikula 		dss1 |= SPLITTER_ENABLE;
2273bc71194eSJani Nikula 		dss1 |= OVERLAP_PIXELS(crtc_state->splitter.pixel_overlap);
2274bc71194eSJani Nikula 		if (crtc_state->splitter.link_count == 2)
2275bc71194eSJani Nikula 			dss1 |= SPLITTER_CONFIGURATION_2_SEGMENT;
2276bc71194eSJani Nikula 		else
2277bc71194eSJani Nikula 			dss1 |= SPLITTER_CONFIGURATION_4_SEGMENT;
2278bc71194eSJani Nikula 	}
2279bc71194eSJani Nikula 
2280bc71194eSJani Nikula 	intel_de_rmw(i915, ICL_PIPE_DSS_CTL1(pipe),
2281bc71194eSJani Nikula 		     SPLITTER_ENABLE | SPLITTER_CONFIGURATION_MASK |
2282bc71194eSJani Nikula 		     OVERLAP_PIXELS_MASK, dss1);
2283bc71194eSJani Nikula }
2284bc71194eSJani Nikula 
2285f82f2563SMatt Roper static void dg2_ddi_pre_enable_dp(struct intel_atomic_state *state,
2286f82f2563SMatt Roper 				  struct intel_encoder *encoder,
2287f82f2563SMatt Roper 				  const struct intel_crtc_state *crtc_state,
2288f82f2563SMatt Roper 				  const struct drm_connector_state *conn_state)
2289f82f2563SMatt Roper {
2290f82f2563SMatt Roper 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2291f82f2563SMatt Roper 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2292f82f2563SMatt Roper 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2293f82f2563SMatt Roper 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2294f82f2563SMatt Roper 
2295f82f2563SMatt Roper 	intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
2296f82f2563SMatt Roper 				 crtc_state->lane_count);
2297f82f2563SMatt Roper 
2298f82f2563SMatt Roper 	/*
22999f620f1dSVille Syrjälä 	 * We only configure what the register value will be here.  Actual
23009f620f1dSVille Syrjälä 	 * enabling happens during link training farther down.
23019f620f1dSVille Syrjälä 	 */
23029f620f1dSVille Syrjälä 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
23039f620f1dSVille Syrjälä 
23049f620f1dSVille Syrjälä 	/*
2305f82f2563SMatt Roper 	 * 1. Enable Power Wells
2306f82f2563SMatt Roper 	 *
2307f82f2563SMatt Roper 	 * This was handled at the beginning of intel_atomic_commit_tail(),
2308f82f2563SMatt Roper 	 * before we called down into this function.
2309f82f2563SMatt Roper 	 */
2310f82f2563SMatt Roper 
2311f82f2563SMatt Roper 	/* 2. Enable Panel Power if PPS is required */
2312f82f2563SMatt Roper 	intel_pps_on(intel_dp);
2313f82f2563SMatt Roper 
2314f82f2563SMatt Roper 	/*
2315f82f2563SMatt Roper 	 * 3. Enable the port PLL.
2316f82f2563SMatt Roper 	 */
2317f82f2563SMatt Roper 	intel_ddi_enable_clock(encoder, crtc_state);
2318f82f2563SMatt Roper 
2319f82f2563SMatt Roper 	/* 4. Enable IO power */
232011a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port))
2321f82f2563SMatt Roper 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2322f82f2563SMatt Roper 								   dig_port->ddi_io_power_domain);
2323f82f2563SMatt Roper 
2324f82f2563SMatt Roper 	/*
2325f82f2563SMatt Roper 	 * 5. The rest of the below are substeps under the bspec's "Enable and
2326f82f2563SMatt Roper 	 * Train Display Port" step.  Note that steps that are specific to
2327f82f2563SMatt Roper 	 * MST will be handled by intel_mst_pre_enable_dp() before/after it
2328f82f2563SMatt Roper 	 * calls into this function.  Also intel_mst_pre_enable_dp() only calls
2329f82f2563SMatt Roper 	 * us when active_mst_links==0, so any steps designated for "single
2330f82f2563SMatt Roper 	 * stream or multi-stream master transcoder" can just be performed
2331f82f2563SMatt Roper 	 * unconditionally here.
2332f82f2563SMatt Roper 	 */
2333f82f2563SMatt Roper 
2334f82f2563SMatt Roper 	/*
2335f82f2563SMatt Roper 	 * 5.a Configure Transcoder Clock Select to direct the Port clock to the
2336f82f2563SMatt Roper 	 * Transcoder.
2337f82f2563SMatt Roper 	 */
2338f82f2563SMatt Roper 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
2339f82f2563SMatt Roper 
234079ac2b1bSJani Nikula 	/* 5.b Configure transcoder for DP 2.0 128b/132b */
234179ac2b1bSJani Nikula 	intel_ddi_config_transcoder_dp2(encoder, crtc_state);
2342f82f2563SMatt Roper 
2343f82f2563SMatt Roper 	/*
2344f82f2563SMatt Roper 	 * 5.c Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
2345f82f2563SMatt Roper 	 * Transport Select
2346f82f2563SMatt Roper 	 */
2347f82f2563SMatt Roper 	intel_ddi_config_transcoder_func(encoder, crtc_state);
2348f82f2563SMatt Roper 
2349f82f2563SMatt Roper 	/*
2350f82f2563SMatt Roper 	 * 5.d Configure & enable DP_TP_CTL with link training pattern 1
2351f82f2563SMatt Roper 	 * selected
2352f82f2563SMatt Roper 	 *
2353f82f2563SMatt Roper 	 * This will be handled by the intel_dp_start_link_train() farther
2354f82f2563SMatt Roper 	 * down this function.
2355f82f2563SMatt Roper 	 */
2356f82f2563SMatt Roper 
2357f82f2563SMatt Roper 	/* 5.e Configure voltage swing and related IO settings */
2358e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
2359f82f2563SMatt Roper 
2360f82f2563SMatt Roper 	if (!is_mst)
2361f82f2563SMatt Roper 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2362f82f2563SMatt Roper 
236301da701bSAnkit Nautiyal 	intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2364f82f2563SMatt Roper 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
2365f82f2563SMatt Roper 	/*
2366f82f2563SMatt Roper 	 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
2367f82f2563SMatt Roper 	 * in the FEC_CONFIGURATION register to 1 before initiating link
2368f82f2563SMatt Roper 	 * training
2369f82f2563SMatt Roper 	 */
2370f82f2563SMatt Roper 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
237101da701bSAnkit Nautiyal 	intel_dp_check_frl_training(intel_dp);
237201da701bSAnkit Nautiyal 	intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
2373f82f2563SMatt Roper 
2374f82f2563SMatt Roper 	/*
2375f82f2563SMatt Roper 	 * 5.h Follow DisplayPort specification training sequence (see notes for
2376f82f2563SMatt Roper 	 *     failure handling)
2377f82f2563SMatt Roper 	 * 5.i If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
2378f82f2563SMatt Roper 	 *     Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
2379f82f2563SMatt Roper 	 *     (timeout after 800 us)
2380f82f2563SMatt Roper 	 */
2381f82f2563SMatt Roper 	intel_dp_start_link_train(intel_dp, crtc_state);
2382f82f2563SMatt Roper 
2383f82f2563SMatt Roper 	/* 5.j Set DP_TP_CTL link training to Normal */
2384f82f2563SMatt Roper 	if (!is_trans_port_sync_mode(crtc_state))
2385f82f2563SMatt Roper 		intel_dp_stop_link_train(intel_dp, crtc_state);
2386f82f2563SMatt Roper 
2387f82f2563SMatt Roper 	/* 5.k Configure and enable FEC if needed */
2388f82f2563SMatt Roper 	intel_ddi_enable_fec(encoder, crtc_state);
23893126977dSVille Syrjälä 
23903126977dSVille Syrjälä 	intel_dsc_dp_pps_write(encoder, crtc_state);
23913126977dSVille Syrjälä 
23923126977dSVille Syrjälä 	intel_dsc_enable(crtc_state);
2393f82f2563SMatt Roper }
2394f82f2563SMatt Roper 
2395ede9771dSVille Syrjälä static void tgl_ddi_pre_enable_dp(struct intel_atomic_state *state,
2396ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
239799389390SJosé Roberto de Souza 				  const struct intel_crtc_state *crtc_state,
239899389390SJosé Roberto de Souza 				  const struct drm_connector_state *conn_state)
239999389390SJosé Roberto de Souza {
2400b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
240199389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2402b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
240399389390SJosé Roberto de Souza 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
240499389390SJosé Roberto de Souza 
2405a621860aSVille Syrjälä 	intel_dp_set_link_params(intel_dp,
2406a621860aSVille Syrjälä 				 crtc_state->port_clock,
2407a621860aSVille Syrjälä 				 crtc_state->lane_count);
240899389390SJosé Roberto de Souza 
24095e19c0b0SMatt Roper 	/*
24109f620f1dSVille Syrjälä 	 * We only configure what the register value will be here.  Actual
24119f620f1dSVille Syrjälä 	 * enabling happens during link training farther down.
24129f620f1dSVille Syrjälä 	 */
24139f620f1dSVille Syrjälä 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
24149f620f1dSVille Syrjälä 
24159f620f1dSVille Syrjälä 	/*
24165e19c0b0SMatt Roper 	 * 1. Enable Power Wells
24175e19c0b0SMatt Roper 	 *
24185e19c0b0SMatt Roper 	 * This was handled at the beginning of intel_atomic_commit_tail(),
24195e19c0b0SMatt Roper 	 * before we called down into this function.
24205e19c0b0SMatt Roper 	 */
242199389390SJosé Roberto de Souza 
24225e19c0b0SMatt Roper 	/* 2. Enable Panel Power if PPS is required */
2423eb46f498SJani Nikula 	intel_pps_on(intel_dp);
242499389390SJosé Roberto de Souza 
242599389390SJosé Roberto de Souza 	/*
24265e19c0b0SMatt Roper 	 * 3. For non-TBT Type-C ports, set FIA lane count
24275e19c0b0SMatt Roper 	 * (DFLEXDPSP.DPX4TXLATC)
24285e19c0b0SMatt Roper 	 *
24295e19c0b0SMatt Roper 	 * This was done before tgl_ddi_pre_enable_dp by
24301e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_encoders_pre_pll_enable().
243199389390SJosé Roberto de Souza 	 */
243299389390SJosé Roberto de Souza 
24335e19c0b0SMatt Roper 	/*
24345e19c0b0SMatt Roper 	 * 4. Enable the port PLL.
24355e19c0b0SMatt Roper 	 *
24365e19c0b0SMatt Roper 	 * The PLL enabling itself was already done before this function by
24371e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_enable_shared_dpll().  We need only
24385e19c0b0SMatt Roper 	 * configure the PLL to port mapping here.
24395e19c0b0SMatt Roper 	 */
2440c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
24416171e58bSClinton A Taylor 
24425e19c0b0SMatt Roper 	/* 5. If IO power is controlled through PWR_WELL_CTL, Enable IO Power */
244311a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
2444a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2445a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
244699389390SJosé Roberto de Souza 								   dig_port->ddi_io_power_domain);
2447a4550977SImre Deak 	}
244899389390SJosé Roberto de Souza 
24495e19c0b0SMatt Roper 	/* 6. Program DP_MODE */
24503b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
245199389390SJosé Roberto de Souza 
245299389390SJosé Roberto de Souza 	/*
24535e19c0b0SMatt Roper 	 * 7. The rest of the below are substeps under the bspec's "Enable and
24545e19c0b0SMatt Roper 	 * Train Display Port" step.  Note that steps that are specific to
24555e19c0b0SMatt Roper 	 * MST will be handled by intel_mst_pre_enable_dp() before/after it
24565e19c0b0SMatt Roper 	 * calls into this function.  Also intel_mst_pre_enable_dp() only calls
24575e19c0b0SMatt Roper 	 * us when active_mst_links==0, so any steps designated for "single
24585e19c0b0SMatt Roper 	 * stream or multi-stream master transcoder" can just be performed
24595e19c0b0SMatt Roper 	 * unconditionally here.
24605e19c0b0SMatt Roper 	 */
24615e19c0b0SMatt Roper 
24625e19c0b0SMatt Roper 	/*
24635e19c0b0SMatt Roper 	 * 7.a Configure Transcoder Clock Select to direct the Port clock to the
24645e19c0b0SMatt Roper 	 * Transcoder.
246599389390SJosé Roberto de Souza 	 */
246602a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
246799389390SJosé Roberto de Souza 
24685e19c0b0SMatt Roper 	/*
24695e19c0b0SMatt Roper 	 * 7.b Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
24705e19c0b0SMatt Roper 	 * Transport Select
24715e19c0b0SMatt Roper 	 */
2472eed22a46SVille Syrjälä 	intel_ddi_config_transcoder_func(encoder, crtc_state);
247399389390SJosé Roberto de Souza 
24745e19c0b0SMatt Roper 	/*
24755e19c0b0SMatt Roper 	 * 7.c Configure & enable DP_TP_CTL with link training pattern 1
24765e19c0b0SMatt Roper 	 * selected
24775e19c0b0SMatt Roper 	 *
24785e19c0b0SMatt Roper 	 * This will be handled by the intel_dp_start_link_train() farther
24795e19c0b0SMatt Roper 	 * down this function.
24805e19c0b0SMatt Roper 	 */
24815e19c0b0SMatt Roper 
24825e19c0b0SMatt Roper 	/* 7.e Configure voltage swing and related IO settings */
2483e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
248499389390SJosé Roberto de Souza 
24855e19c0b0SMatt Roper 	/*
24865e19c0b0SMatt Roper 	 * 7.f Combo PHY: Configure PORT_CL_DW10 Static Power Down to power up
24875e19c0b0SMatt Roper 	 * the used lanes of the DDI.
24885e19c0b0SMatt Roper 	 */
24895cdf706fSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
249099389390SJosé Roberto de Souza 
24915e19c0b0SMatt Roper 	/*
2492bc71194eSJani Nikula 	 * 7.g Program CoG/MSO configuration bits in DSS_CTL1 if selected.
2493bc71194eSJani Nikula 	 */
2494bc71194eSJani Nikula 	intel_ddi_mso_configure(crtc_state);
2495bc71194eSJani Nikula 
249699389390SJosé Roberto de Souza 	if (!is_mst)
24970e634efdSVille Syrjälä 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
249899389390SJosé Roberto de Souza 
2499522508b6SAnkit Nautiyal 	intel_dp_configure_protocol_converter(intel_dp, crtc_state);
250099389390SJosé Roberto de Souza 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
250199389390SJosé Roberto de Souza 	/*
250299389390SJosé Roberto de Souza 	 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
250399389390SJosé Roberto de Souza 	 * in the FEC_CONFIGURATION register to 1 before initiating link
250499389390SJosé Roberto de Souza 	 * training
250599389390SJosé Roberto de Souza 	 */
250699389390SJosé Roberto de Souza 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
25075e19c0b0SMatt Roper 
25084f3dd47aSAnkit Nautiyal 	intel_dp_check_frl_training(intel_dp);
250910fec80bSAnkit Nautiyal 	intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
25104f3dd47aSAnkit Nautiyal 
25115e19c0b0SMatt Roper 	/*
25125e19c0b0SMatt Roper 	 * 7.i Follow DisplayPort specification training sequence (see notes for
25135e19c0b0SMatt Roper 	 *     failure handling)
25145e19c0b0SMatt Roper 	 * 7.j If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
25155e19c0b0SMatt Roper 	 *     Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
25165e19c0b0SMatt Roper 	 *     (timeout after 800 us)
25175e19c0b0SMatt Roper 	 */
2518a621860aSVille Syrjälä 	intel_dp_start_link_train(intel_dp, crtc_state);
251999389390SJosé Roberto de Souza 
25205e19c0b0SMatt Roper 	/* 7.k Set DP_TP_CTL link training to Normal */
2521eadf6f91SManasi Navare 	if (!is_trans_port_sync_mode(crtc_state))
2522a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
252399389390SJosé Roberto de Souza 
25245e19c0b0SMatt Roper 	/* 7.l Configure and enable FEC if needed */
252599389390SJosé Roberto de Souza 	intel_ddi_enable_fec(encoder, crtc_state);
25263126977dSVille Syrjälä 
25273126977dSVille Syrjälä 	intel_dsc_dp_pps_write(encoder, crtc_state);
25283126977dSVille Syrjälä 
25294e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner)
25303126977dSVille Syrjälä 		intel_dsc_enable(crtc_state);
253199389390SJosé Roberto de Souza }
253299389390SJosé Roberto de Souza 
2533ede9771dSVille Syrjälä static void hsw_ddi_pre_enable_dp(struct intel_atomic_state *state,
2534ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
2535379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
2536379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
2537379bc100SJani Nikula {
2538b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2539379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2540379bc100SJani Nikula 	enum port port = encoder->port;
2541b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2542379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2543379bc100SJani Nikula 
2544005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 11)
25451de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
25461de143ccSPankaj Bharadiya 			    is_mst && (port == PORT_A || port == PORT_E));
2547542dfab5SJosé Roberto de Souza 	else
25481de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm, is_mst && port == PORT_A);
2549379bc100SJani Nikula 
2550a621860aSVille Syrjälä 	intel_dp_set_link_params(intel_dp,
2551a621860aSVille Syrjälä 				 crtc_state->port_clock,
2552a621860aSVille Syrjälä 				 crtc_state->lane_count);
2553379bc100SJani Nikula 
25549f620f1dSVille Syrjälä 	/*
25559f620f1dSVille Syrjälä 	 * We only configure what the register value will be here.  Actual
25569f620f1dSVille Syrjälä 	 * enabling happens during link training farther down.
25579f620f1dSVille Syrjälä 	 */
25589f620f1dSVille Syrjälä 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
25599f620f1dSVille Syrjälä 
2560eb46f498SJani Nikula 	intel_pps_on(intel_dp);
2561379bc100SJani Nikula 
2562c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
2563379bc100SJani Nikula 
256411a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
2565a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2566a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
25673b2ed431SImre Deak 								   dig_port->ddi_io_power_domain);
2568a4550977SImre Deak 	}
2569379bc100SJani Nikula 
25703b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
2571379bc100SJani Nikula 
25725bafd85dSVille Syrjälä 	if (has_buf_trans_select(dev_priv))
2573266152aeSVille Syrjälä 		hsw_prepare_dp_ddi_buffers(encoder, crtc_state);
2574379bc100SJani Nikula 
2575e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
2576e722ab8bSVille Syrjälä 
25775cdf706fSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
2578379bc100SJani Nikula 
2579379bc100SJani Nikula 	if (!is_mst)
25800e634efdSVille Syrjälä 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2581522508b6SAnkit Nautiyal 	intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2582379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state,
2583379bc100SJani Nikula 					      true);
2584379bc100SJani Nikula 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
2585a621860aSVille Syrjälä 	intel_dp_start_link_train(intel_dp, crtc_state);
2586005e9537SMatt Roper 	if ((port != PORT_A || DISPLAY_VER(dev_priv) >= 9) &&
2587eadf6f91SManasi Navare 	    !is_trans_port_sync_mode(crtc_state))
2588a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
2589379bc100SJani Nikula 
2590379bc100SJani Nikula 	intel_ddi_enable_fec(encoder, crtc_state);
2591379bc100SJani Nikula 
2592379bc100SJani Nikula 	if (!is_mst)
259302a715c3SVille Syrjälä 		intel_ddi_enable_pipe_clock(encoder, crtc_state);
2594379bc100SJani Nikula 
25953126977dSVille Syrjälä 	intel_dsc_dp_pps_write(encoder, crtc_state);
25963126977dSVille Syrjälä 
25974e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner)
25983126977dSVille Syrjälä 		intel_dsc_enable(crtc_state);
2599379bc100SJani Nikula }
2600379bc100SJani Nikula 
2601ede9771dSVille Syrjälä static void intel_ddi_pre_enable_dp(struct intel_atomic_state *state,
2602ede9771dSVille Syrjälä 				    struct intel_encoder *encoder,
260399389390SJosé Roberto de Souza 				    const struct intel_crtc_state *crtc_state,
260499389390SJosé Roberto de Souza 				    const struct drm_connector_state *conn_state)
260599389390SJosé Roberto de Souza {
260699389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
260799389390SJosé Roberto de Souza 
2608f82f2563SMatt Roper 	if (IS_DG2(dev_priv))
2609f82f2563SMatt Roper 		dg2_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
2610f82f2563SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 12)
2611ede9771dSVille Syrjälä 		tgl_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
261299389390SJosé Roberto de Souza 	else
2613ede9771dSVille Syrjälä 		hsw_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
26140c06fa15SGwan-gyeong Mun 
2615bd8c9ccaSGwan-gyeong Mun 	/* MST will call a setting of MSA after an allocating of Virtual Channel
2616bd8c9ccaSGwan-gyeong Mun 	 * from MST encoder pre_enable callback.
2617bd8c9ccaSGwan-gyeong Mun 	 */
26181fc1e8d4SJosé Roberto de Souza 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
26190c06fa15SGwan-gyeong Mun 		intel_ddi_set_dp_msa(crtc_state, conn_state);
26201c9d2eb2SJani Nikula 
26211c9d2eb2SJani Nikula 		intel_dp_set_m_n(crtc_state, M1_N1);
262299389390SJosé Roberto de Souza 	}
26231fc1e8d4SJosé Roberto de Souza }
262499389390SJosé Roberto de Souza 
2625ede9771dSVille Syrjälä static void intel_ddi_pre_enable_hdmi(struct intel_atomic_state *state,
2626ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
2627379bc100SJani Nikula 				      const struct intel_crtc_state *crtc_state,
2628379bc100SJani Nikula 				      const struct drm_connector_state *conn_state)
2629379bc100SJani Nikula {
26300ba7ffeaSLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
26310ba7ffeaSLucas De Marchi 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2632379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2633379bc100SJani Nikula 
2634379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
2635c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
2636379bc100SJani Nikula 
2637a4550977SImre Deak 	drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2638a4550977SImre Deak 	dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2639a4550977SImre Deak 							   dig_port->ddi_io_power_domain);
2640379bc100SJani Nikula 
26413b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
2642379bc100SJani Nikula 
264302a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
2644379bc100SJani Nikula 
26450ba7ffeaSLucas De Marchi 	dig_port->set_infoframes(encoder,
2646379bc100SJani Nikula 				 crtc_state->has_infoframe,
2647379bc100SJani Nikula 				 crtc_state, conn_state);
2648379bc100SJani Nikula }
2649379bc100SJani Nikula 
2650ede9771dSVille Syrjälä static void intel_ddi_pre_enable(struct intel_atomic_state *state,
2651ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
2652379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state,
2653379bc100SJani Nikula 				 const struct drm_connector_state *conn_state)
2654379bc100SJani Nikula {
26552225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2656379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2657379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
2658379bc100SJani Nikula 
2659379bc100SJani Nikula 	/*
2660379bc100SJani Nikula 	 * When called from DP MST code:
2661379bc100SJani Nikula 	 * - conn_state will be NULL
2662379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
2663379bc100SJani Nikula 	 * - the main connector associated with this port
2664379bc100SJani Nikula 	 *   won't be active or linked to a crtc
2665379bc100SJani Nikula 	 * - crtc_state will be the state of the first stream to
2666379bc100SJani Nikula 	 *   be activated on this port, and it may not be the same
2667379bc100SJani Nikula 	 *   stream that will be deactivated last, but each stream
2668379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
2669379bc100SJani Nikula 	 *   the DP link parameteres
2670379bc100SJani Nikula 	 */
2671379bc100SJani Nikula 
26721de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->has_pch_encoder);
2673379bc100SJani Nikula 
2674379bc100SJani Nikula 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2675379bc100SJani Nikula 
2676379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
2677ede9771dSVille Syrjälä 		intel_ddi_pre_enable_hdmi(state, encoder, crtc_state,
2678ede9771dSVille Syrjälä 					  conn_state);
2679379bc100SJani Nikula 	} else {
2680f7af425dSVille Syrjälä 		struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2681379bc100SJani Nikula 
2682ede9771dSVille Syrjälä 		intel_ddi_pre_enable_dp(state, encoder, crtc_state,
2683ede9771dSVille Syrjälä 					conn_state);
2684379bc100SJani Nikula 
2685f7af425dSVille Syrjälä 		/* FIXME precompute everything properly */
26860ea02bb8SJosé Roberto de Souza 		/* FIXME how do we turn infoframes off again? */
2687f7af425dSVille Syrjälä 		if (dig_port->lspcon.active && dig_port->dp.has_hdmi_sink)
2688379bc100SJani Nikula 			dig_port->set_infoframes(encoder,
2689379bc100SJani Nikula 						 crtc_state->has_infoframe,
2690379bc100SJani Nikula 						 crtc_state, conn_state);
2691379bc100SJani Nikula 	}
2692379bc100SJani Nikula }
2693379bc100SJani Nikula 
2694379bc100SJani Nikula static void intel_disable_ddi_buf(struct intel_encoder *encoder,
2695379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
2696379bc100SJani Nikula {
2697379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2698379bc100SJani Nikula 	enum port port = encoder->port;
2699379bc100SJani Nikula 	bool wait = false;
2700379bc100SJani Nikula 	u32 val;
2701379bc100SJani Nikula 
2702f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, DDI_BUF_CTL(port));
2703379bc100SJani Nikula 	if (val & DDI_BUF_CTL_ENABLE) {
2704379bc100SJani Nikula 		val &= ~DDI_BUF_CTL_ENABLE;
2705f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(port), val);
2706379bc100SJani Nikula 		wait = true;
2707379bc100SJani Nikula 	}
2708379bc100SJani Nikula 
2709e468ff06SLucas De Marchi 	if (intel_crtc_has_dp_encoder(crtc_state)) {
2710ef79fafeSVille Syrjälä 		val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2711379bc100SJani Nikula 		val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
2712379bc100SJani Nikula 		val |= DP_TP_CTL_LINK_TRAIN_PAT1;
2713ef79fafeSVille Syrjälä 		intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2714e468ff06SLucas De Marchi 	}
2715379bc100SJani Nikula 
2716379bc100SJani Nikula 	/* Disable FEC in DP Sink */
2717379bc100SJani Nikula 	intel_ddi_disable_fec_state(encoder, crtc_state);
2718379bc100SJani Nikula 
2719379bc100SJani Nikula 	if (wait)
2720379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, port);
2721379bc100SJani Nikula }
2722379bc100SJani Nikula 
2723ede9771dSVille Syrjälä static void intel_ddi_post_disable_dp(struct intel_atomic_state *state,
2724ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
2725379bc100SJani Nikula 				      const struct intel_crtc_state *old_crtc_state,
2726379bc100SJani Nikula 				      const struct drm_connector_state *old_conn_state)
2727379bc100SJani Nikula {
2728379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2729b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2730379bc100SJani Nikula 	struct intel_dp *intel_dp = &dig_port->dp;
2731379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(old_crtc_state,
2732379bc100SJani Nikula 					  INTEL_OUTPUT_DP_MST);
2733379bc100SJani Nikula 
2734c980216dSImre Deak 	if (!is_mst)
2735c980216dSImre Deak 		intel_dp_set_infoframes(encoder, false,
2736c980216dSImre Deak 					old_crtc_state, old_conn_state);
2737fa37a213SGwan-gyeong Mun 
2738379bc100SJani Nikula 	/*
2739379bc100SJani Nikula 	 * Power down sink before disabling the port, otherwise we end
2740379bc100SJani Nikula 	 * up getting interrupts from the sink on detecting link loss.
2741379bc100SJani Nikula 	 */
27420e634efdSVille Syrjälä 	intel_dp_set_power(intel_dp, DP_SET_POWER_D3);
274378eaaba3SJosé Roberto de Souza 
2744005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2745c59053dcSJosé Roberto de Souza 		if (is_mst) {
2746c59053dcSJosé Roberto de Souza 			enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
2747c59053dcSJosé Roberto de Souza 			u32 val;
2748c59053dcSJosé Roberto de Souza 
2749f7960e7fSJani Nikula 			val = intel_de_read(dev_priv,
2750f7960e7fSJani Nikula 					    TRANS_DDI_FUNC_CTL(cpu_transcoder));
2751919e4f07SJosé Roberto de Souza 			val &= ~(TGL_TRANS_DDI_PORT_MASK |
2752919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
2753f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2754f7960e7fSJani Nikula 				       TRANS_DDI_FUNC_CTL(cpu_transcoder),
2755f7960e7fSJani Nikula 				       val);
2756c59053dcSJosé Roberto de Souza 		}
2757c59053dcSJosé Roberto de Souza 	} else {
2758c59053dcSJosé Roberto de Souza 		if (!is_mst)
275950a7efb2SJosé Roberto de Souza 			intel_ddi_disable_pipe_clock(old_crtc_state);
2760c59053dcSJosé Roberto de Souza 	}
2761379bc100SJani Nikula 
2762379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
2763379bc100SJani Nikula 
27643ca8f191SJosé Roberto de Souza 	/*
27653ca8f191SJosé Roberto de Souza 	 * From TGL spec: "If single stream or multi-stream master transcoder:
27663ca8f191SJosé Roberto de Souza 	 * Configure Transcoder Clock select to direct no clock to the
27673ca8f191SJosé Roberto de Souza 	 * transcoder"
27683ca8f191SJosé Roberto de Souza 	 */
2769005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
27703ca8f191SJosé Roberto de Souza 		intel_ddi_disable_pipe_clock(old_crtc_state);
27713ca8f191SJosé Roberto de Souza 
2772eb46f498SJani Nikula 	intel_pps_vdd_on(intel_dp);
2773eb46f498SJani Nikula 	intel_pps_off(intel_dp);
2774379bc100SJani Nikula 
277511a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port))
2776a4550977SImre Deak 		intel_display_power_put(dev_priv,
2777a4550977SImre Deak 					dig_port->ddi_io_power_domain,
2778a4550977SImre Deak 					fetch_and_zero(&dig_port->ddi_io_wakeref));
2779379bc100SJani Nikula 
2780c133df69SVille Syrjälä 	intel_ddi_disable_clock(encoder);
2781379bc100SJani Nikula }
2782379bc100SJani Nikula 
2783ede9771dSVille Syrjälä static void intel_ddi_post_disable_hdmi(struct intel_atomic_state *state,
2784ede9771dSVille Syrjälä 					struct intel_encoder *encoder,
2785379bc100SJani Nikula 					const struct intel_crtc_state *old_crtc_state,
2786379bc100SJani Nikula 					const struct drm_connector_state *old_conn_state)
2787379bc100SJani Nikula {
2788379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2789b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2790379bc100SJani Nikula 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2791379bc100SJani Nikula 
2792379bc100SJani Nikula 	dig_port->set_infoframes(encoder, false,
2793379bc100SJani Nikula 				 old_crtc_state, old_conn_state);
2794379bc100SJani Nikula 
2795379bc100SJani Nikula 	intel_ddi_disable_pipe_clock(old_crtc_state);
2796379bc100SJani Nikula 
2797379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
2798379bc100SJani Nikula 
2799a4550977SImre Deak 	intel_display_power_put(dev_priv,
2800a4550977SImre Deak 				dig_port->ddi_io_power_domain,
2801a4550977SImre Deak 				fetch_and_zero(&dig_port->ddi_io_wakeref));
2802379bc100SJani Nikula 
2803c133df69SVille Syrjälä 	intel_ddi_disable_clock(encoder);
2804379bc100SJani Nikula 
2805379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
2806379bc100SJani Nikula }
2807379bc100SJani Nikula 
2808ede9771dSVille Syrjälä static void intel_ddi_post_disable(struct intel_atomic_state *state,
2809ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
2810379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
2811379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
2812379bc100SJani Nikula {
2813379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2814b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
281517bef9baSVille Syrjälä 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
281617bef9baSVille Syrjälä 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
2817379bc100SJani Nikula 
28187829c92bSVille Syrjälä 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST)) {
2819773b4b54SVille Syrjälä 		intel_crtc_vblank_off(old_crtc_state);
2820773b4b54SVille Syrjälä 
28218c66081bSVille Syrjälä 		intel_disable_transcoder(old_crtc_state);
2822773b4b54SVille Syrjälä 
2823f0651232SManasi Navare 		intel_vrr_disable(old_crtc_state);
2824f0651232SManasi Navare 
2825773b4b54SVille Syrjälä 		intel_ddi_disable_transcoder_func(old_crtc_state);
2826773b4b54SVille Syrjälä 
2827773b4b54SVille Syrjälä 		intel_dsc_disable(old_crtc_state);
2828773b4b54SVille Syrjälä 
2829005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 9)
2830f6df4d46SLucas De Marchi 			skl_scaler_disable(old_crtc_state);
2831773b4b54SVille Syrjälä 		else
28329eae5e27SLucas De Marchi 			ilk_pfit_disable(old_crtc_state);
28337829c92bSVille Syrjälä 	}
2834773b4b54SVille Syrjälä 
28354e3cdb45SManasi Navare 	if (old_crtc_state->bigjoiner_linked_crtc) {
2836f2e19b58SVille Syrjälä 		struct intel_crtc *slave_crtc =
28374e3cdb45SManasi Navare 			old_crtc_state->bigjoiner_linked_crtc;
28384e3cdb45SManasi Navare 		const struct intel_crtc_state *old_slave_crtc_state =
2839f2e19b58SVille Syrjälä 			intel_atomic_get_old_crtc_state(state, slave_crtc);
28404e3cdb45SManasi Navare 
28414e3cdb45SManasi Navare 		intel_crtc_vblank_off(old_slave_crtc_state);
28424e3cdb45SManasi Navare 
28434e3cdb45SManasi Navare 		intel_dsc_disable(old_slave_crtc_state);
28444e3cdb45SManasi Navare 		skl_scaler_disable(old_slave_crtc_state);
28454e3cdb45SManasi Navare 	}
28464e3cdb45SManasi Navare 
2847379bc100SJani Nikula 	/*
2848379bc100SJani Nikula 	 * When called from DP MST code:
2849379bc100SJani Nikula 	 * - old_conn_state will be NULL
2850379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
2851379bc100SJani Nikula 	 * - the main connector associated with this port
2852379bc100SJani Nikula 	 *   won't be active or linked to a crtc
2853379bc100SJani Nikula 	 * - old_crtc_state will be the state of the last stream to
2854379bc100SJani Nikula 	 *   be deactivated on this port, and it may not be the same
2855379bc100SJani Nikula 	 *   stream that was activated last, but each stream
2856379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
2857379bc100SJani Nikula 	 *   the DP link parameteres
2858379bc100SJani Nikula 	 */
2859379bc100SJani Nikula 
2860379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
2861ede9771dSVille Syrjälä 		intel_ddi_post_disable_hdmi(state, encoder, old_crtc_state,
2862ede9771dSVille Syrjälä 					    old_conn_state);
2863379bc100SJani Nikula 	else
2864ede9771dSVille Syrjälä 		intel_ddi_post_disable_dp(state, encoder, old_crtc_state,
2865ede9771dSVille Syrjälä 					  old_conn_state);
2866379bc100SJani Nikula 
286717bef9baSVille Syrjälä 	if (intel_crtc_has_dp_encoder(old_crtc_state) || is_tc_port)
2868162e68e1SImre Deak 		intel_display_power_put(dev_priv,
2869162e68e1SImre Deak 					intel_ddi_main_link_aux_domain(dig_port),
2870162e68e1SImre Deak 					fetch_and_zero(&dig_port->aux_wakeref));
287117bef9baSVille Syrjälä 
287217bef9baSVille Syrjälä 	if (is_tc_port)
287317bef9baSVille Syrjälä 		intel_tc_port_put_link(dig_port);
2874379bc100SJani Nikula }
2875379bc100SJani Nikula 
2876d82a855aSVille Syrjälä static void trans_port_sync_stop_link_train(struct intel_atomic_state *state,
2877d82a855aSVille Syrjälä 					    struct intel_encoder *encoder,
2878d82a855aSVille Syrjälä 					    const struct intel_crtc_state *crtc_state)
2879d82a855aSVille Syrjälä {
2880d82a855aSVille Syrjälä 	const struct drm_connector_state *conn_state;
2881d82a855aSVille Syrjälä 	struct drm_connector *conn;
2882d82a855aSVille Syrjälä 	int i;
2883d82a855aSVille Syrjälä 
2884d82a855aSVille Syrjälä 	if (!crtc_state->sync_mode_slaves_mask)
2885d82a855aSVille Syrjälä 		return;
2886d82a855aSVille Syrjälä 
2887d82a855aSVille Syrjälä 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
2888d82a855aSVille Syrjälä 		struct intel_encoder *slave_encoder =
2889d82a855aSVille Syrjälä 			to_intel_encoder(conn_state->best_encoder);
2890d82a855aSVille Syrjälä 		struct intel_crtc *slave_crtc = to_intel_crtc(conn_state->crtc);
2891d82a855aSVille Syrjälä 		const struct intel_crtc_state *slave_crtc_state;
2892d82a855aSVille Syrjälä 
2893d82a855aSVille Syrjälä 		if (!slave_crtc)
2894d82a855aSVille Syrjälä 			continue;
2895d82a855aSVille Syrjälä 
2896d82a855aSVille Syrjälä 		slave_crtc_state =
2897d82a855aSVille Syrjälä 			intel_atomic_get_new_crtc_state(state, slave_crtc);
2898d82a855aSVille Syrjälä 
2899d82a855aSVille Syrjälä 		if (slave_crtc_state->master_transcoder !=
2900d82a855aSVille Syrjälä 		    crtc_state->cpu_transcoder)
2901d82a855aSVille Syrjälä 			continue;
2902d82a855aSVille Syrjälä 
2903a621860aSVille Syrjälä 		intel_dp_stop_link_train(enc_to_intel_dp(slave_encoder),
2904a621860aSVille Syrjälä 					 slave_crtc_state);
2905d82a855aSVille Syrjälä 	}
2906d82a855aSVille Syrjälä 
2907d82a855aSVille Syrjälä 	usleep_range(200, 400);
2908d82a855aSVille Syrjälä 
2909a621860aSVille Syrjälä 	intel_dp_stop_link_train(enc_to_intel_dp(encoder),
2910a621860aSVille Syrjälä 				 crtc_state);
2911d82a855aSVille Syrjälä }
2912d82a855aSVille Syrjälä 
2913ede9771dSVille Syrjälä static void intel_enable_ddi_dp(struct intel_atomic_state *state,
2914ede9771dSVille Syrjälä 				struct intel_encoder *encoder,
2915379bc100SJani Nikula 				const struct intel_crtc_state *crtc_state,
2916379bc100SJani Nikula 				const struct drm_connector_state *conn_state)
2917379bc100SJani Nikula {
2918379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2919b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2920998cc864SUma Shankar 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2921379bc100SJani Nikula 	enum port port = encoder->port;
2922379bc100SJani Nikula 
2923005e9537SMatt Roper 	if (port == PORT_A && DISPLAY_VER(dev_priv) < 9)
2924a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
2925379bc100SJani Nikula 
2926379bc100SJani Nikula 	intel_edp_backlight_on(crtc_state, conn_state);
2927998cc864SUma Shankar 
2928998cc864SUma Shankar 	if (!dig_port->lspcon.active || dig_port->dp.has_hdmi_sink)
29291bf3657cSGwan-gyeong Mun 		intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
2930998cc864SUma Shankar 
29313a3dd534SJosé Roberto de Souza 	intel_drrs_enable(intel_dp, crtc_state);
2932379bc100SJani Nikula 
2933379bc100SJani Nikula 	if (crtc_state->has_audio)
2934379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
2935d82a855aSVille Syrjälä 
2936d82a855aSVille Syrjälä 	trans_port_sync_stop_link_train(state, encoder, crtc_state);
2937379bc100SJani Nikula }
2938379bc100SJani Nikula 
2939379bc100SJani Nikula static i915_reg_t
2940379bc100SJani Nikula gen9_chicken_trans_reg_by_port(struct drm_i915_private *dev_priv,
2941379bc100SJani Nikula 			       enum port port)
2942379bc100SJani Nikula {
294312c4d4c1SVille Syrjälä 	static const enum transcoder trans[] = {
294412c4d4c1SVille Syrjälä 		[PORT_A] = TRANSCODER_EDP,
294512c4d4c1SVille Syrjälä 		[PORT_B] = TRANSCODER_A,
294612c4d4c1SVille Syrjälä 		[PORT_C] = TRANSCODER_B,
294712c4d4c1SVille Syrjälä 		[PORT_D] = TRANSCODER_C,
294812c4d4c1SVille Syrjälä 		[PORT_E] = TRANSCODER_A,
2949379bc100SJani Nikula 	};
2950379bc100SJani Nikula 
2951005e9537SMatt Roper 	drm_WARN_ON(&dev_priv->drm, DISPLAY_VER(dev_priv) < 9);
2952379bc100SJani Nikula 
29531de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port < PORT_A || port > PORT_E))
2954379bc100SJani Nikula 		port = PORT_A;
2955379bc100SJani Nikula 
295612c4d4c1SVille Syrjälä 	return CHICKEN_TRANS(trans[port]);
2957379bc100SJani Nikula }
2958379bc100SJani Nikula 
2959ede9771dSVille Syrjälä static void intel_enable_ddi_hdmi(struct intel_atomic_state *state,
2960ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
2961379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
2962379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
2963379bc100SJani Nikula {
2964379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2965b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2966379bc100SJani Nikula 	struct drm_connector *connector = conn_state->connector;
2967379bc100SJani Nikula 	enum port port = encoder->port;
2968379bc100SJani Nikula 
2969379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
2970379bc100SJani Nikula 					       crtc_state->hdmi_high_tmds_clock_ratio,
2971379bc100SJani Nikula 					       crtc_state->hdmi_scrambling))
297247bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
297347bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to configure sink scrambling/TMDS bit clock ratio\n",
2974379bc100SJani Nikula 			    connector->base.id, connector->name);
2975379bc100SJani Nikula 
29765bafd85dSVille Syrjälä 	if (has_buf_trans_select(dev_priv))
2977e722ab8bSVille Syrjälä 		hsw_prepare_hdmi_ddi_buffers(encoder, crtc_state);
2978c9b69041SVille Syrjälä 
2979e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
2980c9b69041SVille Syrjälä 
2981379bc100SJani Nikula 	/* Display WA #1143: skl,kbl,cfl */
298293e7e61eSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv)) {
2983379bc100SJani Nikula 		/*
2984379bc100SJani Nikula 		 * For some reason these chicken bits have been
2985379bc100SJani Nikula 		 * stuffed into a transcoder register, event though
2986379bc100SJani Nikula 		 * the bits affect a specific DDI port rather than
2987379bc100SJani Nikula 		 * a specific transcoder.
2988379bc100SJani Nikula 		 */
2989379bc100SJani Nikula 		i915_reg_t reg = gen9_chicken_trans_reg_by_port(dev_priv, port);
2990379bc100SJani Nikula 		u32 val;
2991379bc100SJani Nikula 
2992f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, reg);
2993379bc100SJani Nikula 
2994379bc100SJani Nikula 		if (port == PORT_E)
2995379bc100SJani Nikula 			val |= DDIE_TRAINING_OVERRIDE_ENABLE |
2996379bc100SJani Nikula 				DDIE_TRAINING_OVERRIDE_VALUE;
2997379bc100SJani Nikula 		else
2998379bc100SJani Nikula 			val |= DDI_TRAINING_OVERRIDE_ENABLE |
2999379bc100SJani Nikula 				DDI_TRAINING_OVERRIDE_VALUE;
3000379bc100SJani Nikula 
3001f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3002f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, reg);
3003379bc100SJani Nikula 
3004379bc100SJani Nikula 		udelay(1);
3005379bc100SJani Nikula 
3006379bc100SJani Nikula 		if (port == PORT_E)
3007379bc100SJani Nikula 			val &= ~(DDIE_TRAINING_OVERRIDE_ENABLE |
3008379bc100SJani Nikula 				 DDIE_TRAINING_OVERRIDE_VALUE);
3009379bc100SJani Nikula 		else
3010379bc100SJani Nikula 			val &= ~(DDI_TRAINING_OVERRIDE_ENABLE |
3011379bc100SJani Nikula 				 DDI_TRAINING_OVERRIDE_VALUE);
3012379bc100SJani Nikula 
3013f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3014379bc100SJani Nikula 	}
3015379bc100SJani Nikula 
30161e0cb7beSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
30171e0cb7beSVille Syrjälä 
3018379bc100SJani Nikula 	/* In HDMI/DVI mode, the port width, and swing/emphasis values
3019379bc100SJani Nikula 	 * are ignored so nothing special needs to be done besides
3020379bc100SJani Nikula 	 * enabling the port.
3021414002f1SImre Deak 	 *
3022414002f1SImre Deak 	 * On ADL_P the PHY link rate and lane count must be programmed but
3023414002f1SImre Deak 	 * these are both 0 for HDMI.
3024379bc100SJani Nikula 	 */
3025f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port),
3026379bc100SJani Nikula 		       dig_port->saved_port_bits | DDI_BUF_CTL_ENABLE);
3027379bc100SJani Nikula 
3028379bc100SJani Nikula 	if (crtc_state->has_audio)
3029379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3030379bc100SJani Nikula }
3031379bc100SJani Nikula 
3032ede9771dSVille Syrjälä static void intel_enable_ddi(struct intel_atomic_state *state,
3033ede9771dSVille Syrjälä 			     struct intel_encoder *encoder,
3034379bc100SJani Nikula 			     const struct intel_crtc_state *crtc_state,
3035379bc100SJani Nikula 			     const struct drm_connector_state *conn_state)
3036379bc100SJani Nikula {
30378b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc_state->has_pch_encoder);
303821fd23acSJani Nikula 
30394e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner_slave)
3040eed22a46SVille Syrjälä 		intel_ddi_enable_transcoder_func(encoder, crtc_state);
30417c2fedd7SVille Syrjälä 
3042aa52b39dSManasi Navare 	intel_vrr_enable(encoder, crtc_state);
3043aa52b39dSManasi Navare 
30448c66081bSVille Syrjälä 	intel_enable_transcoder(crtc_state);
304521fd23acSJani Nikula 
304621fd23acSJani Nikula 	intel_crtc_vblank_on(crtc_state);
304721fd23acSJani Nikula 
3048379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3049ede9771dSVille Syrjälä 		intel_enable_ddi_hdmi(state, encoder, crtc_state, conn_state);
3050379bc100SJani Nikula 	else
3051ede9771dSVille Syrjälä 		intel_enable_ddi_dp(state, encoder, crtc_state, conn_state);
3052379bc100SJani Nikula 
3053379bc100SJani Nikula 	/* Enable hdcp if it's desired */
3054379bc100SJani Nikula 	if (conn_state->content_protection ==
3055379bc100SJani Nikula 	    DRM_MODE_CONTENT_PROTECTION_DESIRED)
3056d456512cSRamalingam C 		intel_hdcp_enable(to_intel_connector(conn_state->connector),
3057fc6097d4SAnshuman Gupta 				  crtc_state,
3058d456512cSRamalingam C 				  (u8)conn_state->hdcp_content_type);
3059379bc100SJani Nikula }
3060379bc100SJani Nikula 
3061ede9771dSVille Syrjälä static void intel_disable_ddi_dp(struct intel_atomic_state *state,
3062ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
3063379bc100SJani Nikula 				 const struct intel_crtc_state *old_crtc_state,
3064379bc100SJani Nikula 				 const struct drm_connector_state *old_conn_state)
3065379bc100SJani Nikula {
3066b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3067379bc100SJani Nikula 
3068379bc100SJani Nikula 	intel_dp->link_trained = false;
3069379bc100SJani Nikula 
3070f28c5950SVille Syrjälä 	if (old_crtc_state->has_audio)
3071f28c5950SVille Syrjälä 		intel_audio_codec_disable(encoder,
3072f28c5950SVille Syrjälä 					  old_crtc_state, old_conn_state);
3073f28c5950SVille Syrjälä 
3074f28c5950SVille Syrjälä 	intel_drrs_disable(intel_dp, old_crtc_state);
3075f28c5950SVille Syrjälä 	intel_psr_disable(intel_dp, old_crtc_state);
3076379bc100SJani Nikula 	intel_edp_backlight_off(old_conn_state);
3077379bc100SJani Nikula 	/* Disable the decompression in DP Sink */
3078379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, old_crtc_state,
3079379bc100SJani Nikula 					      false);
30801639406aSManasi Navare 	/* Disable Ignore_MSA bit in DP Sink */
30811639406aSManasi Navare 	intel_dp_sink_set_msa_timing_par_ignore_state(intel_dp, old_crtc_state,
30821639406aSManasi Navare 						      false);
3083379bc100SJani Nikula }
3084379bc100SJani Nikula 
3085ede9771dSVille Syrjälä static void intel_disable_ddi_hdmi(struct intel_atomic_state *state,
3086ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
3087379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3088379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3089379bc100SJani Nikula {
309047bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3091379bc100SJani Nikula 	struct drm_connector *connector = old_conn_state->connector;
3092379bc100SJani Nikula 
3093f28c5950SVille Syrjälä 	if (old_crtc_state->has_audio)
3094f28c5950SVille Syrjälä 		intel_audio_codec_disable(encoder,
3095f28c5950SVille Syrjälä 					  old_crtc_state, old_conn_state);
3096f28c5950SVille Syrjälä 
3097379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3098379bc100SJani Nikula 					       false, false))
309947bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
310047bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to reset sink scrambling/TMDS bit clock ratio\n",
3101379bc100SJani Nikula 			    connector->base.id, connector->name);
3102379bc100SJani Nikula }
3103379bc100SJani Nikula 
3104ede9771dSVille Syrjälä static void intel_disable_ddi(struct intel_atomic_state *state,
3105ede9771dSVille Syrjälä 			      struct intel_encoder *encoder,
3106379bc100SJani Nikula 			      const struct intel_crtc_state *old_crtc_state,
3107379bc100SJani Nikula 			      const struct drm_connector_state *old_conn_state)
3108379bc100SJani Nikula {
3109379bc100SJani Nikula 	intel_hdcp_disable(to_intel_connector(old_conn_state->connector));
3110379bc100SJani Nikula 
3111379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3112ede9771dSVille Syrjälä 		intel_disable_ddi_hdmi(state, encoder, old_crtc_state,
3113ede9771dSVille Syrjälä 				       old_conn_state);
3114379bc100SJani Nikula 	else
3115ede9771dSVille Syrjälä 		intel_disable_ddi_dp(state, encoder, old_crtc_state,
3116ede9771dSVille Syrjälä 				     old_conn_state);
3117379bc100SJani Nikula }
3118379bc100SJani Nikula 
3119ede9771dSVille Syrjälä static void intel_ddi_update_pipe_dp(struct intel_atomic_state *state,
3120ede9771dSVille Syrjälä 				     struct intel_encoder *encoder,
3121379bc100SJani Nikula 				     const struct intel_crtc_state *crtc_state,
3122379bc100SJani Nikula 				     const struct drm_connector_state *conn_state)
3123379bc100SJani Nikula {
3124b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3125379bc100SJani Nikula 
31260c06fa15SGwan-gyeong Mun 	intel_ddi_set_dp_msa(crtc_state, conn_state);
3127379bc100SJani Nikula 
312876d45d06SGwan-gyeong Mun 	intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
31293a3dd534SJosé Roberto de Souza 	intel_drrs_update(intel_dp, crtc_state);
3130379bc100SJani Nikula 
3131c0a52f8bSJani Nikula 	intel_backlight_update(state, encoder, crtc_state, conn_state);
3132379bc100SJani Nikula }
3133379bc100SJani Nikula 
3134f1c7a36bSSean Paul void intel_ddi_update_pipe(struct intel_atomic_state *state,
3135ede9771dSVille Syrjälä 			   struct intel_encoder *encoder,
3136379bc100SJani Nikula 			   const struct intel_crtc_state *crtc_state,
3137379bc100SJani Nikula 			   const struct drm_connector_state *conn_state)
3138379bc100SJani Nikula {
3139d456512cSRamalingam C 
3140f1c7a36bSSean Paul 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) &&
3141f1c7a36bSSean Paul 	    !intel_encoder_is_mst(encoder))
3142ede9771dSVille Syrjälä 		intel_ddi_update_pipe_dp(state, encoder, crtc_state,
3143ede9771dSVille Syrjälä 					 conn_state);
3144379bc100SJani Nikula 
3145ede9771dSVille Syrjälä 	intel_hdcp_update_pipe(state, encoder, crtc_state, conn_state);
3146379bc100SJani Nikula }
3147379bc100SJani Nikula 
3148379bc100SJani Nikula static void
314924a7bfe0SImre Deak intel_ddi_update_prepare(struct intel_atomic_state *state,
315024a7bfe0SImre Deak 			 struct intel_encoder *encoder,
315124a7bfe0SImre Deak 			 struct intel_crtc *crtc)
315224a7bfe0SImre Deak {
315324a7bfe0SImre Deak 	struct intel_crtc_state *crtc_state =
315424a7bfe0SImre Deak 		crtc ? intel_atomic_get_new_crtc_state(state, crtc) : NULL;
315524a7bfe0SImre Deak 	int required_lanes = crtc_state ? crtc_state->lane_count : 1;
315624a7bfe0SImre Deak 
31578b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc && crtc->active);
315824a7bfe0SImre Deak 
3159b7d02c3aSVille Syrjälä 	intel_tc_port_get_link(enc_to_dig_port(encoder),
3160b7d02c3aSVille Syrjälä 		               required_lanes);
31611326a92cSMaarten Lankhorst 	if (crtc_state && crtc_state->hw.active)
316224a7bfe0SImre Deak 		intel_update_active_dpll(state, crtc, encoder);
316324a7bfe0SImre Deak }
316424a7bfe0SImre Deak 
316524a7bfe0SImre Deak static void
316624a7bfe0SImre Deak intel_ddi_update_complete(struct intel_atomic_state *state,
316724a7bfe0SImre Deak 			  struct intel_encoder *encoder,
316824a7bfe0SImre Deak 			  struct intel_crtc *crtc)
316924a7bfe0SImre Deak {
3170b7d02c3aSVille Syrjälä 	intel_tc_port_put_link(enc_to_dig_port(encoder));
317124a7bfe0SImre Deak }
317224a7bfe0SImre Deak 
317324a7bfe0SImre Deak static void
3174ede9771dSVille Syrjälä intel_ddi_pre_pll_enable(struct intel_atomic_state *state,
3175ede9771dSVille Syrjälä 			 struct intel_encoder *encoder,
3176379bc100SJani Nikula 			 const struct intel_crtc_state *crtc_state,
3177379bc100SJani Nikula 			 const struct drm_connector_state *conn_state)
3178379bc100SJani Nikula {
3179379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3180b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3181d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3182d8fe2ab6SMatt Roper 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
3183379bc100SJani Nikula 
318424a7bfe0SImre Deak 	if (is_tc_port)
318524a7bfe0SImre Deak 		intel_tc_port_get_link(dig_port, crtc_state->lane_count);
318624a7bfe0SImre Deak 
3187162e68e1SImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state) || is_tc_port) {
3188162e68e1SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->aux_wakeref);
3189162e68e1SImre Deak 		dig_port->aux_wakeref =
3190379bc100SJani Nikula 			intel_display_power_get(dev_priv,
3191379bc100SJani Nikula 						intel_ddi_main_link_aux_domain(dig_port));
3192162e68e1SImre Deak 	}
3193379bc100SJani Nikula 
319411a89708SImre Deak 	if (is_tc_port && !intel_tc_port_in_tbt_alt_mode(dig_port))
31959d44dcb9SLucas De Marchi 		/*
31969d44dcb9SLucas De Marchi 		 * Program the lane count for static/dynamic connections on
31979d44dcb9SLucas De Marchi 		 * Type-C ports.  Skip this step for TBT.
31989d44dcb9SLucas De Marchi 		 */
31999d44dcb9SLucas De Marchi 		intel_tc_port_set_fia_lane_count(dig_port, crtc_state->lane_count);
32002446e1d6SMatt Roper 	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3201379bc100SJani Nikula 		bxt_ddi_phy_set_lane_optim_mask(encoder,
3202379bc100SJani Nikula 						crtc_state->lane_lat_optim_mask);
3203379bc100SJani Nikula }
3204379bc100SJani Nikula 
3205a621860aSVille Syrjälä static void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp,
3206a621860aSVille Syrjälä 					   const struct intel_crtc_state *crtc_state)
3207379bc100SJani Nikula {
3208ef79fafeSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3209ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3210ef79fafeSVille Syrjälä 	enum port port = encoder->port;
321135ac28a8SLucas De Marchi 	u32 dp_tp_ctl, ddi_buf_ctl;
3212379bc100SJani Nikula 	bool wait = false;
3213379bc100SJani Nikula 
3214ef79fafeSVille Syrjälä 	dp_tp_ctl = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
321535ac28a8SLucas De Marchi 
321635ac28a8SLucas De Marchi 	if (dp_tp_ctl & DP_TP_CTL_ENABLE) {
3217f7960e7fSJani Nikula 		ddi_buf_ctl = intel_de_read(dev_priv, DDI_BUF_CTL(port));
321835ac28a8SLucas De Marchi 		if (ddi_buf_ctl & DDI_BUF_CTL_ENABLE) {
3219f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_BUF_CTL(port),
322035ac28a8SLucas De Marchi 				       ddi_buf_ctl & ~DDI_BUF_CTL_ENABLE);
3221379bc100SJani Nikula 			wait = true;
3222379bc100SJani Nikula 		}
3223379bc100SJani Nikula 
322435ac28a8SLucas De Marchi 		dp_tp_ctl &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
322535ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_LINK_TRAIN_PAT1;
3226ef79fafeSVille Syrjälä 		intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3227ef79fafeSVille Syrjälä 		intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3228379bc100SJani Nikula 
3229379bc100SJani Nikula 		if (wait)
3230379bc100SJani Nikula 			intel_wait_ddi_buf_idle(dev_priv, port);
3231379bc100SJani Nikula 	}
3232379bc100SJani Nikula 
3233963501bdSImre Deak 	dp_tp_ctl = DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_PAT1;
3234a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
323535ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_MST;
3236a621860aSVille Syrjälä 	} else {
323735ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_SST;
3238379bc100SJani Nikula 		if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
323935ac28a8SLucas De Marchi 			dp_tp_ctl |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
3240379bc100SJani Nikula 	}
3241ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3242ef79fafeSVille Syrjälä 	intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3243379bc100SJani Nikula 
3244379bc100SJani Nikula 	intel_dp->DP |= DDI_BUF_CTL_ENABLE;
3245f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
3246f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
3247379bc100SJani Nikula 
3248e828da30SManasi Navare 	intel_wait_ddi_buf_active(dev_priv, port);
3249379bc100SJani Nikula }
3250379bc100SJani Nikula 
3251eee3f911SVille Syrjälä static void intel_ddi_set_link_train(struct intel_dp *intel_dp,
3252a621860aSVille Syrjälä 				     const struct intel_crtc_state *crtc_state,
3253eee3f911SVille Syrjälä 				     u8 dp_train_pat)
3254eee3f911SVille Syrjälä {
3255ef79fafeSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3256ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3257eee3f911SVille Syrjälä 	u32 temp;
3258eee3f911SVille Syrjälä 
3259ef79fafeSVille Syrjälä 	temp = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3260eee3f911SVille Syrjälä 
3261eee3f911SVille Syrjälä 	temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
32626777a855SImre Deak 	switch (intel_dp_training_pattern_symbol(dp_train_pat)) {
3263eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_DISABLE:
3264eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
3265eee3f911SVille Syrjälä 		break;
3266eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_1:
3267eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
3268eee3f911SVille Syrjälä 		break;
3269eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_2:
3270eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
3271eee3f911SVille Syrjälä 		break;
3272eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_3:
3273eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
3274eee3f911SVille Syrjälä 		break;
3275eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_4:
3276eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT4;
3277eee3f911SVille Syrjälä 		break;
3278eee3f911SVille Syrjälä 	}
3279eee3f911SVille Syrjälä 
3280ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), temp);
3281eee3f911SVille Syrjälä }
3282eee3f911SVille Syrjälä 
3283a621860aSVille Syrjälä static void intel_ddi_set_idle_link_train(struct intel_dp *intel_dp,
3284a621860aSVille Syrjälä 					  const struct intel_crtc_state *crtc_state)
32858fdda385SVille Syrjälä {
32868fdda385SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
32878fdda385SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
32888fdda385SVille Syrjälä 	enum port port = encoder->port;
32898fdda385SVille Syrjälä 	u32 val;
32908fdda385SVille Syrjälä 
3291ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
32928fdda385SVille Syrjälä 	val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
32938fdda385SVille Syrjälä 	val |= DP_TP_CTL_LINK_TRAIN_IDLE;
3294ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
32958fdda385SVille Syrjälä 
32968fdda385SVille Syrjälä 	/*
32978fdda385SVille Syrjälä 	 * Until TGL on PORT_A we can have only eDP in SST mode. There the only
32988fdda385SVille Syrjälä 	 * reason we need to set idle transmission mode is to work around a HW
32998fdda385SVille Syrjälä 	 * issue where we enable the pipe while not in idle link-training mode.
33008fdda385SVille Syrjälä 	 * In this case there is requirement to wait for a minimum number of
33018fdda385SVille Syrjälä 	 * idle patterns to be sent.
33028fdda385SVille Syrjälä 	 */
3303005e9537SMatt Roper 	if (port == PORT_A && DISPLAY_VER(dev_priv) < 12)
33048fdda385SVille Syrjälä 		return;
33058fdda385SVille Syrjälä 
3306ef79fafeSVille Syrjälä 	if (intel_de_wait_for_set(dev_priv,
3307ef79fafeSVille Syrjälä 				  dp_tp_status_reg(encoder, crtc_state),
33088fdda385SVille Syrjälä 				  DP_TP_STATUS_IDLE_DONE, 1))
33098fdda385SVille Syrjälä 		drm_err(&dev_priv->drm,
33108fdda385SVille Syrjälä 			"Timed out waiting for DP idle patterns\n");
33118fdda385SVille Syrjälä }
33128fdda385SVille Syrjälä 
3313379bc100SJani Nikula static bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
3314379bc100SJani Nikula 				       enum transcoder cpu_transcoder)
3315379bc100SJani Nikula {
3316379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP)
3317379bc100SJani Nikula 		return false;
3318379bc100SJani Nikula 
3319615a7724SAnshuman Gupta 	if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO_MMIO))
3320379bc100SJani Nikula 		return false;
3321379bc100SJani Nikula 
3322f7960e7fSJani Nikula 	return intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD) &
3323379bc100SJani Nikula 		AUDIO_OUTPUT_ENABLE(cpu_transcoder);
3324379bc100SJani Nikula }
3325379bc100SJani Nikula 
3326379bc100SJani Nikula void intel_ddi_compute_min_voltage_level(struct drm_i915_private *dev_priv,
3327379bc100SJani Nikula 					 struct intel_crtc_state *crtc_state)
3328379bc100SJani Nikula {
3329005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12 && crtc_state->port_clock > 594000)
33300fde0b1dSMatt Roper 		crtc_state->min_voltage_level = 2;
333124ea098bSTejas Upadhyay 	else if (IS_JSL_EHL(dev_priv) && crtc_state->port_clock > 594000)
33329d5fd37eSMatt Roper 		crtc_state->min_voltage_level = 3;
3333005e9537SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 11 && crtc_state->port_clock > 594000)
3334379bc100SJani Nikula 		crtc_state->min_voltage_level = 1;
3335379bc100SJani Nikula }
3336379bc100SJani Nikula 
3337dc5b8ed5SVille Syrjälä static enum transcoder bdw_transcoder_master_readout(struct drm_i915_private *dev_priv,
333802d8ea47SVille Syrjälä 						     enum transcoder cpu_transcoder)
333902d8ea47SVille Syrjälä {
3340dc5b8ed5SVille Syrjälä 	u32 master_select;
334102d8ea47SVille Syrjälä 
3342005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
3343dc5b8ed5SVille Syrjälä 		u32 ctl2 = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL2(cpu_transcoder));
334402d8ea47SVille Syrjälä 
334502d8ea47SVille Syrjälä 		if ((ctl2 & PORT_SYNC_MODE_ENABLE) == 0)
334602d8ea47SVille Syrjälä 			return INVALID_TRANSCODER;
334702d8ea47SVille Syrjälä 
3348d4d7d9caSVille Syrjälä 		master_select = REG_FIELD_GET(PORT_SYNC_MODE_MASTER_SELECT_MASK, ctl2);
3349dc5b8ed5SVille Syrjälä 	} else {
3350dc5b8ed5SVille Syrjälä 		u32 ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3351dc5b8ed5SVille Syrjälä 
3352dc5b8ed5SVille Syrjälä 		if ((ctl & TRANS_DDI_PORT_SYNC_ENABLE) == 0)
3353dc5b8ed5SVille Syrjälä 			return INVALID_TRANSCODER;
3354dc5b8ed5SVille Syrjälä 
3355dc5b8ed5SVille Syrjälä 		master_select = REG_FIELD_GET(TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK, ctl);
3356dc5b8ed5SVille Syrjälä 	}
335702d8ea47SVille Syrjälä 
335802d8ea47SVille Syrjälä 	if (master_select == 0)
335902d8ea47SVille Syrjälä 		return TRANSCODER_EDP;
336002d8ea47SVille Syrjälä 	else
336102d8ea47SVille Syrjälä 		return master_select - 1;
336202d8ea47SVille Syrjälä }
336302d8ea47SVille Syrjälä 
3364dc5b8ed5SVille Syrjälä static void bdw_get_trans_port_sync_config(struct intel_crtc_state *crtc_state)
336502d8ea47SVille Syrjälä {
336602d8ea47SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
336702d8ea47SVille Syrjälä 	u32 transcoders = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
336802d8ea47SVille Syrjälä 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
336902d8ea47SVille Syrjälä 	enum transcoder cpu_transcoder;
337002d8ea47SVille Syrjälä 
337102d8ea47SVille Syrjälä 	crtc_state->master_transcoder =
3372dc5b8ed5SVille Syrjälä 		bdw_transcoder_master_readout(dev_priv, crtc_state->cpu_transcoder);
337302d8ea47SVille Syrjälä 
337402d8ea47SVille Syrjälä 	for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder, transcoders) {
337502d8ea47SVille Syrjälä 		enum intel_display_power_domain power_domain;
337602d8ea47SVille Syrjälä 		intel_wakeref_t trans_wakeref;
337702d8ea47SVille Syrjälä 
337802d8ea47SVille Syrjälä 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
337902d8ea47SVille Syrjälä 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
338002d8ea47SVille Syrjälä 								   power_domain);
338102d8ea47SVille Syrjälä 
338202d8ea47SVille Syrjälä 		if (!trans_wakeref)
338302d8ea47SVille Syrjälä 			continue;
338402d8ea47SVille Syrjälä 
3385dc5b8ed5SVille Syrjälä 		if (bdw_transcoder_master_readout(dev_priv, cpu_transcoder) ==
338602d8ea47SVille Syrjälä 		    crtc_state->cpu_transcoder)
338702d8ea47SVille Syrjälä 			crtc_state->sync_mode_slaves_mask |= BIT(cpu_transcoder);
338802d8ea47SVille Syrjälä 
338902d8ea47SVille Syrjälä 		intel_display_power_put(dev_priv, power_domain, trans_wakeref);
339002d8ea47SVille Syrjälä 	}
339102d8ea47SVille Syrjälä 
339202d8ea47SVille Syrjälä 	drm_WARN_ON(&dev_priv->drm,
339302d8ea47SVille Syrjälä 		    crtc_state->master_transcoder != INVALID_TRANSCODER &&
339402d8ea47SVille Syrjälä 		    crtc_state->sync_mode_slaves_mask);
339502d8ea47SVille Syrjälä }
339602d8ea47SVille Syrjälä 
33970385eceaSManasi Navare static void intel_ddi_read_func_ctl(struct intel_encoder *encoder,
3398379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config)
3399379bc100SJani Nikula {
3400379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3401f15f01a7SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3402379bc100SJani Nikula 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
3403a44289b9SUma Shankar 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3404379bc100SJani Nikula 	u32 temp, flags = 0;
3405379bc100SJani Nikula 
3406f7960e7fSJani Nikula 	temp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3407379bc100SJani Nikula 	if (temp & TRANS_DDI_PHSYNC)
3408379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PHSYNC;
3409379bc100SJani Nikula 	else
3410379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NHSYNC;
3411379bc100SJani Nikula 	if (temp & TRANS_DDI_PVSYNC)
3412379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PVSYNC;
3413379bc100SJani Nikula 	else
3414379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NVSYNC;
3415379bc100SJani Nikula 
34161326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.flags |= flags;
3417379bc100SJani Nikula 
3418379bc100SJani Nikula 	switch (temp & TRANS_DDI_BPC_MASK) {
3419379bc100SJani Nikula 	case TRANS_DDI_BPC_6:
3420379bc100SJani Nikula 		pipe_config->pipe_bpp = 18;
3421379bc100SJani Nikula 		break;
3422379bc100SJani Nikula 	case TRANS_DDI_BPC_8:
3423379bc100SJani Nikula 		pipe_config->pipe_bpp = 24;
3424379bc100SJani Nikula 		break;
3425379bc100SJani Nikula 	case TRANS_DDI_BPC_10:
3426379bc100SJani Nikula 		pipe_config->pipe_bpp = 30;
3427379bc100SJani Nikula 		break;
3428379bc100SJani Nikula 	case TRANS_DDI_BPC_12:
3429379bc100SJani Nikula 		pipe_config->pipe_bpp = 36;
3430379bc100SJani Nikula 		break;
3431379bc100SJani Nikula 	default:
3432379bc100SJani Nikula 		break;
3433379bc100SJani Nikula 	}
3434379bc100SJani Nikula 
3435379bc100SJani Nikula 	switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
3436379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
3437379bc100SJani Nikula 		pipe_config->has_hdmi_sink = true;
3438379bc100SJani Nikula 
3439379bc100SJani Nikula 		pipe_config->infoframes.enable |=
3440379bc100SJani Nikula 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
3441379bc100SJani Nikula 
3442379bc100SJani Nikula 		if (pipe_config->infoframes.enable)
3443379bc100SJani Nikula 			pipe_config->has_infoframe = true;
3444379bc100SJani Nikula 
3445379bc100SJani Nikula 		if (temp & TRANS_DDI_HDMI_SCRAMBLING)
3446379bc100SJani Nikula 			pipe_config->hdmi_scrambling = true;
3447379bc100SJani Nikula 		if (temp & TRANS_DDI_HIGH_TMDS_CHAR_RATE)
3448379bc100SJani Nikula 			pipe_config->hdmi_high_tmds_clock_ratio = true;
3449df561f66SGustavo A. R. Silva 		fallthrough;
3450379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
3451379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);
3452379bc100SJani Nikula 		pipe_config->lane_count = 4;
3453379bc100SJani Nikula 		break;
3454379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
3455379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
3456379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
3457379bc100SJani Nikula 		else
3458379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
3459379bc100SJani Nikula 		pipe_config->lane_count =
3460379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
3461f15f01a7SVille Syrjälä 		intel_dp_get_m_n(crtc, pipe_config);
34628aa940c8SMaarten Lankhorst 
3463005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 11) {
3464ef79fafeSVille Syrjälä 			i915_reg_t dp_tp_ctl = dp_tp_ctl_reg(encoder, pipe_config);
34658aa940c8SMaarten Lankhorst 
34668aa940c8SMaarten Lankhorst 			pipe_config->fec_enable =
3467f7960e7fSJani Nikula 				intel_de_read(dev_priv, dp_tp_ctl) & DP_TP_CTL_FEC_ENABLE;
34688aa940c8SMaarten Lankhorst 
346947bdb1caSJani Nikula 			drm_dbg_kms(&dev_priv->drm,
347047bdb1caSJani Nikula 				    "[ENCODER:%d:%s] Fec status: %u\n",
34718aa940c8SMaarten Lankhorst 				    encoder->base.base.id, encoder->base.name,
34728aa940c8SMaarten Lankhorst 				    pipe_config->fec_enable);
34738aa940c8SMaarten Lankhorst 		}
34748aa940c8SMaarten Lankhorst 
3475a44289b9SUma Shankar 		if (dig_port->lspcon.active && dig_port->dp.has_hdmi_sink)
3476a44289b9SUma Shankar 			pipe_config->infoframes.enable |=
3477a44289b9SUma Shankar 				intel_lspcon_infoframes_enabled(encoder, pipe_config);
3478a44289b9SUma Shankar 		else
3479dee66f3eSGwan-gyeong Mun 			pipe_config->infoframes.enable |=
3480dee66f3eSGwan-gyeong Mun 				intel_hdmi_infoframes_enabled(encoder, pipe_config);
3481379bc100SJani Nikula 		break;
348265213594SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI_OR_128B132B:
348365213594SJani Nikula 		if (!HAS_DP20(dev_priv)) {
348465213594SJani Nikula 			/* FDI */
348565213594SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
348665213594SJani Nikula 			break;
348765213594SJani Nikula 		}
348865213594SJani Nikula 		fallthrough; /* 128b/132b */
3489379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
3490379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_DP_MST);
3491379bc100SJani Nikula 		pipe_config->lane_count =
3492379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
34936671c367SJosé Roberto de Souza 
3494005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12)
34956671c367SJosé Roberto de Souza 			pipe_config->mst_master_transcoder =
34966671c367SJosé Roberto de Souza 					REG_FIELD_GET(TRANS_DDI_MST_TRANSPORT_SELECT_MASK, temp);
34976671c367SJosé Roberto de Souza 
3498f15f01a7SVille Syrjälä 		intel_dp_get_m_n(crtc, pipe_config);
3499dee66f3eSGwan-gyeong Mun 
3500dee66f3eSGwan-gyeong Mun 		pipe_config->infoframes.enable |=
3501dee66f3eSGwan-gyeong Mun 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
3502379bc100SJani Nikula 		break;
3503379bc100SJani Nikula 	default:
3504379bc100SJani Nikula 		break;
3505379bc100SJani Nikula 	}
35060385eceaSManasi Navare }
35070385eceaSManasi Navare 
3508351221ffSVille Syrjälä static void intel_ddi_get_config(struct intel_encoder *encoder,
35090385eceaSManasi Navare 				 struct intel_crtc_state *pipe_config)
35100385eceaSManasi Navare {
35110385eceaSManasi Navare 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
35120385eceaSManasi Navare 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
35130385eceaSManasi Navare 
35140385eceaSManasi Navare 	/* XXX: DSI transcoder paranoia */
35150385eceaSManasi Navare 	if (drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder)))
35160385eceaSManasi Navare 		return;
35170385eceaSManasi Navare 
35180385eceaSManasi Navare 	intel_ddi_read_func_ctl(encoder, pipe_config);
3519379bc100SJani Nikula 
35205b616a29SJani Nikula 	intel_ddi_mso_get_config(encoder, pipe_config);
35215b616a29SJani Nikula 
3522379bc100SJani Nikula 	pipe_config->has_audio =
3523379bc100SJani Nikula 		intel_ddi_is_audio_enabled(dev_priv, cpu_transcoder);
3524379bc100SJani Nikula 
3525379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.bpp &&
3526379bc100SJani Nikula 	    pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
3527379bc100SJani Nikula 		/*
3528379bc100SJani Nikula 		 * This is a big fat ugly hack.
3529379bc100SJani Nikula 		 *
3530379bc100SJani Nikula 		 * Some machines in UEFI boot mode provide us a VBT that has 18
3531379bc100SJani Nikula 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
3532379bc100SJani Nikula 		 * unknown we fail to light up. Yet the same BIOS boots up with
3533379bc100SJani Nikula 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
3534379bc100SJani Nikula 		 * max, not what it tells us to use.
3535379bc100SJani Nikula 		 *
3536379bc100SJani Nikula 		 * Note: This will still be broken if the eDP panel is not lit
3537379bc100SJani Nikula 		 * up by the BIOS, and thus we can't get the mode at module
3538379bc100SJani Nikula 		 * load.
3539379bc100SJani Nikula 		 */
354047bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
354147bdb1caSJani Nikula 			    "pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
3542379bc100SJani Nikula 			    pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
3543379bc100SJani Nikula 		dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
3544379bc100SJani Nikula 	}
3545379bc100SJani Nikula 
3546351221ffSVille Syrjälä 	ddi_dotclock_get(pipe_config);
3547379bc100SJani Nikula 
35482446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3549379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
3550379bc100SJani Nikula 			bxt_ddi_phy_get_lane_lat_optim_mask(encoder);
3551379bc100SJani Nikula 
3552379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3553379bc100SJani Nikula 
3554379bc100SJani Nikula 	intel_hdmi_read_gcp_infoframe(encoder, pipe_config);
3555379bc100SJani Nikula 
3556379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3557379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_AVI,
3558379bc100SJani Nikula 			     &pipe_config->infoframes.avi);
3559379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3560379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_SPD,
3561379bc100SJani Nikula 			     &pipe_config->infoframes.spd);
3562379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3563379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_VENDOR,
3564379bc100SJani Nikula 			     &pipe_config->infoframes.hdmi);
3565379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3566379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_DRM,
3567379bc100SJani Nikula 			     &pipe_config->infoframes.drm);
356802d8ea47SVille Syrjälä 
3569005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 8)
3570dc5b8ed5SVille Syrjälä 		bdw_get_trans_port_sync_config(pipe_config);
3571dee66f3eSGwan-gyeong Mun 
3572dee66f3eSGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, HDMI_PACKET_TYPE_GAMUT_METADATA);
35732c3928e4SGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, DP_SDP_VSC);
357478b772e1SJosé Roberto de Souza 
357578b772e1SJosé Roberto de Souza 	intel_psr_get_config(encoder, pipe_config);
3576379bc100SJani Nikula }
3577379bc100SJani Nikula 
3578351221ffSVille Syrjälä void intel_ddi_get_clock(struct intel_encoder *encoder,
3579351221ffSVille Syrjälä 			 struct intel_crtc_state *crtc_state,
3580351221ffSVille Syrjälä 			 struct intel_shared_dpll *pll)
3581351221ffSVille Syrjälä {
3582351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3583351221ffSVille Syrjälä 	enum icl_port_dpll_id port_dpll_id = ICL_PORT_DPLL_DEFAULT;
3584351221ffSVille Syrjälä 	struct icl_port_dpll *port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
3585351221ffSVille Syrjälä 	bool pll_active;
3586351221ffSVille Syrjälä 
3587086877a1SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
3588086877a1SVille Syrjälä 		return;
3589086877a1SVille Syrjälä 
3590351221ffSVille Syrjälä 	port_dpll->pll = pll;
3591351221ffSVille Syrjälä 	pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
3592351221ffSVille Syrjälä 	drm_WARN_ON(&i915->drm, !pll_active);
3593351221ffSVille Syrjälä 
3594351221ffSVille Syrjälä 	icl_set_active_port_dpll(crtc_state, port_dpll_id);
3595351221ffSVille Syrjälä 
3596351221ffSVille Syrjälä 	crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
3597351221ffSVille Syrjälä 						     &crtc_state->dpll_hw_state);
3598351221ffSVille Syrjälä }
3599351221ffSVille Syrjälä 
3600865b73eaSMatt Roper static void dg2_ddi_get_config(struct intel_encoder *encoder,
3601865b73eaSMatt Roper 				struct intel_crtc_state *crtc_state)
3602865b73eaSMatt Roper {
3603865b73eaSMatt Roper 	intel_mpllb_readout_hw_state(encoder, &crtc_state->mpllb_state);
3604865b73eaSMatt Roper 	crtc_state->port_clock = intel_mpllb_calc_port_clock(encoder, &crtc_state->mpllb_state);
3605865b73eaSMatt Roper 
3606865b73eaSMatt Roper 	intel_ddi_get_config(encoder, crtc_state);
3607865b73eaSMatt Roper }
3608865b73eaSMatt Roper 
3609351221ffSVille Syrjälä static void adls_ddi_get_config(struct intel_encoder *encoder,
3610351221ffSVille Syrjälä 				struct intel_crtc_state *crtc_state)
3611351221ffSVille Syrjälä {
3612351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, adls_ddi_get_pll(encoder));
3613351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3614351221ffSVille Syrjälä }
3615351221ffSVille Syrjälä 
3616351221ffSVille Syrjälä static void rkl_ddi_get_config(struct intel_encoder *encoder,
3617351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3618351221ffSVille Syrjälä {
3619351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, rkl_ddi_get_pll(encoder));
3620351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3621351221ffSVille Syrjälä }
3622351221ffSVille Syrjälä 
3623351221ffSVille Syrjälä static void dg1_ddi_get_config(struct intel_encoder *encoder,
3624351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3625351221ffSVille Syrjälä {
3626351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, dg1_ddi_get_pll(encoder));
3627351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3628351221ffSVille Syrjälä }
3629351221ffSVille Syrjälä 
3630351221ffSVille Syrjälä static void icl_ddi_combo_get_config(struct intel_encoder *encoder,
3631351221ffSVille Syrjälä 				     struct intel_crtc_state *crtc_state)
3632351221ffSVille Syrjälä {
3633351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, icl_ddi_combo_get_pll(encoder));
3634351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3635351221ffSVille Syrjälä }
3636351221ffSVille Syrjälä 
3637086877a1SVille Syrjälä static void icl_ddi_tc_get_clock(struct intel_encoder *encoder,
3638086877a1SVille Syrjälä 				 struct intel_crtc_state *crtc_state,
3639086877a1SVille Syrjälä 				 struct intel_shared_dpll *pll)
3640351221ffSVille Syrjälä {
3641351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3642351221ffSVille Syrjälä 	enum icl_port_dpll_id port_dpll_id;
3643351221ffSVille Syrjälä 	struct icl_port_dpll *port_dpll;
3644351221ffSVille Syrjälä 	bool pll_active;
3645351221ffSVille Syrjälä 
3646086877a1SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
3647086877a1SVille Syrjälä 		return;
3648351221ffSVille Syrjälä 
3649351221ffSVille Syrjälä 	if (intel_get_shared_dpll_id(i915, pll) == DPLL_ID_ICL_TBTPLL)
3650351221ffSVille Syrjälä 		port_dpll_id = ICL_PORT_DPLL_DEFAULT;
3651351221ffSVille Syrjälä 	else
3652351221ffSVille Syrjälä 		port_dpll_id = ICL_PORT_DPLL_MG_PHY;
3653351221ffSVille Syrjälä 
3654351221ffSVille Syrjälä 	port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
3655351221ffSVille Syrjälä 
3656351221ffSVille Syrjälä 	port_dpll->pll = pll;
3657351221ffSVille Syrjälä 	pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
3658351221ffSVille Syrjälä 	drm_WARN_ON(&i915->drm, !pll_active);
3659351221ffSVille Syrjälä 
3660351221ffSVille Syrjälä 	icl_set_active_port_dpll(crtc_state, port_dpll_id);
3661351221ffSVille Syrjälä 
3662351221ffSVille Syrjälä 	if (intel_get_shared_dpll_id(i915, crtc_state->shared_dpll) == DPLL_ID_ICL_TBTPLL)
3663351221ffSVille Syrjälä 		crtc_state->port_clock = icl_calc_tbt_pll_link(i915, encoder->port);
3664351221ffSVille Syrjälä 	else
3665351221ffSVille Syrjälä 		crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
3666351221ffSVille Syrjälä 							     &crtc_state->dpll_hw_state);
3667086877a1SVille Syrjälä }
3668351221ffSVille Syrjälä 
3669086877a1SVille Syrjälä static void icl_ddi_tc_get_config(struct intel_encoder *encoder,
3670086877a1SVille Syrjälä 				  struct intel_crtc_state *crtc_state)
3671086877a1SVille Syrjälä {
3672086877a1SVille Syrjälä 	icl_ddi_tc_get_clock(encoder, crtc_state, icl_ddi_tc_get_pll(encoder));
3673351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3674351221ffSVille Syrjälä }
3675351221ffSVille Syrjälä 
3676351221ffSVille Syrjälä static void bxt_ddi_get_config(struct intel_encoder *encoder,
3677351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3678351221ffSVille Syrjälä {
3679351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, bxt_ddi_get_pll(encoder));
3680351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3681351221ffSVille Syrjälä }
3682351221ffSVille Syrjälä 
3683351221ffSVille Syrjälä static void skl_ddi_get_config(struct intel_encoder *encoder,
3684351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3685351221ffSVille Syrjälä {
3686351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, skl_ddi_get_pll(encoder));
3687351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3688351221ffSVille Syrjälä }
3689351221ffSVille Syrjälä 
3690351221ffSVille Syrjälä void hsw_ddi_get_config(struct intel_encoder *encoder,
3691351221ffSVille Syrjälä 			struct intel_crtc_state *crtc_state)
3692351221ffSVille Syrjälä {
3693351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, hsw_ddi_get_pll(encoder));
3694351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3695351221ffSVille Syrjälä }
3696351221ffSVille Syrjälä 
3697f9e76a6eSImre Deak static void intel_ddi_sync_state(struct intel_encoder *encoder,
3698f9e76a6eSImre Deak 				 const struct intel_crtc_state *crtc_state)
3699f9e76a6eSImre Deak {
37007194dc99SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
37017194dc99SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
37027194dc99SImre Deak 
37037194dc99SImre Deak 	if (intel_phy_is_tc(i915, phy))
37047194dc99SImre Deak 		intel_tc_port_sanitize(enc_to_dig_port(encoder));
37057194dc99SImre Deak 
37067194dc99SImre Deak 	if (crtc_state && intel_crtc_has_dp_encoder(crtc_state))
3707f9e76a6eSImre Deak 		intel_dp_sync_state(encoder, crtc_state);
3708f9e76a6eSImre Deak }
3709f9e76a6eSImre Deak 
3710b671d6efSImre Deak static bool intel_ddi_initial_fastset_check(struct intel_encoder *encoder,
3711b671d6efSImre Deak 					    struct intel_crtc_state *crtc_state)
3712b671d6efSImre Deak {
3713b671d6efSImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state))
3714b671d6efSImre Deak 		return intel_dp_initial_fastset_check(encoder, crtc_state);
3715b671d6efSImre Deak 
3716b671d6efSImre Deak 	return true;
3717b671d6efSImre Deak }
3718b671d6efSImre Deak 
3719379bc100SJani Nikula static enum intel_output_type
3720379bc100SJani Nikula intel_ddi_compute_output_type(struct intel_encoder *encoder,
3721379bc100SJani Nikula 			      struct intel_crtc_state *crtc_state,
3722379bc100SJani Nikula 			      struct drm_connector_state *conn_state)
3723379bc100SJani Nikula {
3724379bc100SJani Nikula 	switch (conn_state->connector->connector_type) {
3725379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_HDMIA:
3726379bc100SJani Nikula 		return INTEL_OUTPUT_HDMI;
3727379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_eDP:
3728379bc100SJani Nikula 		return INTEL_OUTPUT_EDP;
3729379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_DisplayPort:
3730379bc100SJani Nikula 		return INTEL_OUTPUT_DP;
3731379bc100SJani Nikula 	default:
3732379bc100SJani Nikula 		MISSING_CASE(conn_state->connector->connector_type);
3733379bc100SJani Nikula 		return INTEL_OUTPUT_UNUSED;
3734379bc100SJani Nikula 	}
3735379bc100SJani Nikula }
3736379bc100SJani Nikula 
3737379bc100SJani Nikula static int intel_ddi_compute_config(struct intel_encoder *encoder,
3738379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config,
3739379bc100SJani Nikula 				    struct drm_connector_state *conn_state)
3740379bc100SJani Nikula {
37412225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3742379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3743379bc100SJani Nikula 	enum port port = encoder->port;
3744379bc100SJani Nikula 	int ret;
3745379bc100SJani Nikula 
374610cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
3747379bc100SJani Nikula 		pipe_config->cpu_transcoder = TRANSCODER_EDP;
3748379bc100SJani Nikula 
3749bdacf087SAnshuman Gupta 	if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI)) {
3750379bc100SJani Nikula 		ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
3751bdacf087SAnshuman Gupta 	} else {
3752379bc100SJani Nikula 		ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
3753bdacf087SAnshuman Gupta 	}
3754bdacf087SAnshuman Gupta 
3755379bc100SJani Nikula 	if (ret)
3756379bc100SJani Nikula 		return ret;
3757379bc100SJani Nikula 
3758379bc100SJani Nikula 	if (IS_HASWELL(dev_priv) && crtc->pipe == PIPE_A &&
3759379bc100SJani Nikula 	    pipe_config->cpu_transcoder == TRANSCODER_EDP)
3760379bc100SJani Nikula 		pipe_config->pch_pfit.force_thru =
3761379bc100SJani Nikula 			pipe_config->pch_pfit.enabled ||
3762379bc100SJani Nikula 			pipe_config->crc_enabled;
3763379bc100SJani Nikula 
37642446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3765379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
3766379bc100SJani Nikula 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
3767379bc100SJani Nikula 
3768379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3769379bc100SJani Nikula 
3770379bc100SJani Nikula 	return 0;
3771379bc100SJani Nikula }
3772379bc100SJani Nikula 
3773b50a1aa6SManasi Navare static bool mode_equal(const struct drm_display_mode *mode1,
3774b50a1aa6SManasi Navare 		       const struct drm_display_mode *mode2)
3775b50a1aa6SManasi Navare {
3776b50a1aa6SManasi Navare 	return drm_mode_match(mode1, mode2,
3777b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_TIMINGS |
3778b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_FLAGS |
3779b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_3D_FLAGS) &&
3780b50a1aa6SManasi Navare 		mode1->clock == mode2->clock; /* we want an exact match */
3781b50a1aa6SManasi Navare }
3782b50a1aa6SManasi Navare 
3783b50a1aa6SManasi Navare static bool m_n_equal(const struct intel_link_m_n *m_n_1,
3784b50a1aa6SManasi Navare 		      const struct intel_link_m_n *m_n_2)
3785b50a1aa6SManasi Navare {
3786b50a1aa6SManasi Navare 	return m_n_1->tu == m_n_2->tu &&
3787b50a1aa6SManasi Navare 		m_n_1->gmch_m == m_n_2->gmch_m &&
3788b50a1aa6SManasi Navare 		m_n_1->gmch_n == m_n_2->gmch_n &&
3789b50a1aa6SManasi Navare 		m_n_1->link_m == m_n_2->link_m &&
3790b50a1aa6SManasi Navare 		m_n_1->link_n == m_n_2->link_n;
3791b50a1aa6SManasi Navare }
3792b50a1aa6SManasi Navare 
3793b50a1aa6SManasi Navare static bool crtcs_port_sync_compatible(const struct intel_crtc_state *crtc_state1,
3794b50a1aa6SManasi Navare 				       const struct intel_crtc_state *crtc_state2)
3795b50a1aa6SManasi Navare {
3796b50a1aa6SManasi Navare 	return crtc_state1->hw.active && crtc_state2->hw.active &&
3797b50a1aa6SManasi Navare 		crtc_state1->output_types == crtc_state2->output_types &&
3798b50a1aa6SManasi Navare 		crtc_state1->output_format == crtc_state2->output_format &&
3799b50a1aa6SManasi Navare 		crtc_state1->lane_count == crtc_state2->lane_count &&
3800b50a1aa6SManasi Navare 		crtc_state1->port_clock == crtc_state2->port_clock &&
3801b50a1aa6SManasi Navare 		mode_equal(&crtc_state1->hw.adjusted_mode,
3802b50a1aa6SManasi Navare 			   &crtc_state2->hw.adjusted_mode) &&
3803b50a1aa6SManasi Navare 		m_n_equal(&crtc_state1->dp_m_n, &crtc_state2->dp_m_n);
3804b50a1aa6SManasi Navare }
3805b50a1aa6SManasi Navare 
3806b50a1aa6SManasi Navare static u8
3807b50a1aa6SManasi Navare intel_ddi_port_sync_transcoders(const struct intel_crtc_state *ref_crtc_state,
3808b50a1aa6SManasi Navare 				int tile_group_id)
3809b50a1aa6SManasi Navare {
3810b50a1aa6SManasi Navare 	struct drm_connector *connector;
3811b50a1aa6SManasi Navare 	const struct drm_connector_state *conn_state;
3812b50a1aa6SManasi Navare 	struct drm_i915_private *dev_priv = to_i915(ref_crtc_state->uapi.crtc->dev);
3813b50a1aa6SManasi Navare 	struct intel_atomic_state *state =
3814b50a1aa6SManasi Navare 		to_intel_atomic_state(ref_crtc_state->uapi.state);
3815b50a1aa6SManasi Navare 	u8 transcoders = 0;
3816b50a1aa6SManasi Navare 	int i;
3817b50a1aa6SManasi Navare 
3818dc5b8ed5SVille Syrjälä 	/*
3819dc5b8ed5SVille Syrjälä 	 * We don't enable port sync on BDW due to missing w/as and
3820dc5b8ed5SVille Syrjälä 	 * due to not having adjusted the modeset sequence appropriately.
3821dc5b8ed5SVille Syrjälä 	 */
3822005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 9)
3823b50a1aa6SManasi Navare 		return 0;
3824b50a1aa6SManasi Navare 
3825b50a1aa6SManasi Navare 	if (!intel_crtc_has_type(ref_crtc_state, INTEL_OUTPUT_DP))
3826b50a1aa6SManasi Navare 		return 0;
3827b50a1aa6SManasi Navare 
3828b50a1aa6SManasi Navare 	for_each_new_connector_in_state(&state->base, connector, conn_state, i) {
3829b50a1aa6SManasi Navare 		struct intel_crtc *crtc = to_intel_crtc(conn_state->crtc);
3830b50a1aa6SManasi Navare 		const struct intel_crtc_state *crtc_state;
3831b50a1aa6SManasi Navare 
3832b50a1aa6SManasi Navare 		if (!crtc)
3833b50a1aa6SManasi Navare 			continue;
3834b50a1aa6SManasi Navare 
3835b50a1aa6SManasi Navare 		if (!connector->has_tile ||
3836b50a1aa6SManasi Navare 		    connector->tile_group->id !=
3837b50a1aa6SManasi Navare 		    tile_group_id)
3838b50a1aa6SManasi Navare 			continue;
3839b50a1aa6SManasi Navare 		crtc_state = intel_atomic_get_new_crtc_state(state,
3840b50a1aa6SManasi Navare 							     crtc);
3841b50a1aa6SManasi Navare 		if (!crtcs_port_sync_compatible(ref_crtc_state,
3842b50a1aa6SManasi Navare 						crtc_state))
3843b50a1aa6SManasi Navare 			continue;
3844b50a1aa6SManasi Navare 		transcoders |= BIT(crtc_state->cpu_transcoder);
3845b50a1aa6SManasi Navare 	}
3846b50a1aa6SManasi Navare 
3847b50a1aa6SManasi Navare 	return transcoders;
3848b50a1aa6SManasi Navare }
3849b50a1aa6SManasi Navare 
3850b50a1aa6SManasi Navare static int intel_ddi_compute_config_late(struct intel_encoder *encoder,
3851b50a1aa6SManasi Navare 					 struct intel_crtc_state *crtc_state,
3852b50a1aa6SManasi Navare 					 struct drm_connector_state *conn_state)
3853b50a1aa6SManasi Navare {
385447bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3855b50a1aa6SManasi Navare 	struct drm_connector *connector = conn_state->connector;
3856b50a1aa6SManasi Navare 	u8 port_sync_transcoders = 0;
3857b50a1aa6SManasi Navare 
385847bdb1caSJani Nikula 	drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] [CRTC:%d:%s]",
3859b50a1aa6SManasi Navare 		    encoder->base.base.id, encoder->base.name,
3860b50a1aa6SManasi Navare 		    crtc_state->uapi.crtc->base.id, crtc_state->uapi.crtc->name);
3861b50a1aa6SManasi Navare 
3862b50a1aa6SManasi Navare 	if (connector->has_tile)
3863b50a1aa6SManasi Navare 		port_sync_transcoders = intel_ddi_port_sync_transcoders(crtc_state,
3864b50a1aa6SManasi Navare 									connector->tile_group->id);
3865b50a1aa6SManasi Navare 
3866b50a1aa6SManasi Navare 	/*
3867b50a1aa6SManasi Navare 	 * EDP Transcoders cannot be ensalved
3868b50a1aa6SManasi Navare 	 * make them a master always when present
3869b50a1aa6SManasi Navare 	 */
3870b50a1aa6SManasi Navare 	if (port_sync_transcoders & BIT(TRANSCODER_EDP))
3871b50a1aa6SManasi Navare 		crtc_state->master_transcoder = TRANSCODER_EDP;
3872b50a1aa6SManasi Navare 	else
3873b50a1aa6SManasi Navare 		crtc_state->master_transcoder = ffs(port_sync_transcoders) - 1;
3874b50a1aa6SManasi Navare 
3875b50a1aa6SManasi Navare 	if (crtc_state->master_transcoder == crtc_state->cpu_transcoder) {
3876b50a1aa6SManasi Navare 		crtc_state->master_transcoder = INVALID_TRANSCODER;
3877b50a1aa6SManasi Navare 		crtc_state->sync_mode_slaves_mask =
3878b50a1aa6SManasi Navare 			port_sync_transcoders & ~BIT(crtc_state->cpu_transcoder);
3879b50a1aa6SManasi Navare 	}
3880b50a1aa6SManasi Navare 
3881b50a1aa6SManasi Navare 	return 0;
3882b50a1aa6SManasi Navare }
3883b50a1aa6SManasi Navare 
3884379bc100SJani Nikula static void intel_ddi_encoder_destroy(struct drm_encoder *encoder)
3885379bc100SJani Nikula {
38864a300e65SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->dev);
3887b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
38883e0abc76SImre Deak 	enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
3889379bc100SJani Nikula 
3890379bc100SJani Nikula 	intel_dp_encoder_flush_work(encoder);
38913e0abc76SImre Deak 	if (intel_phy_is_tc(i915, phy))
38923e0abc76SImre Deak 		intel_tc_port_flush_work(dig_port);
38934a300e65SImre Deak 	intel_display_power_flush_work(i915);
3894379bc100SJani Nikula 
3895379bc100SJani Nikula 	drm_encoder_cleanup(encoder);
3896a6c6eac9SAnshuman Gupta 	kfree(dig_port->hdcp_port_data.streams);
3897379bc100SJani Nikula 	kfree(dig_port);
3898379bc100SJani Nikula }
3899379bc100SJani Nikula 
3900764f6729SVille Syrjälä static void intel_ddi_encoder_reset(struct drm_encoder *encoder)
3901764f6729SVille Syrjälä {
3902764f6729SVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(encoder));
3903764f6729SVille Syrjälä 
3904764f6729SVille Syrjälä 	intel_dp->reset_link_params = true;
3905764f6729SVille Syrjälä 
3906764f6729SVille Syrjälä 	intel_pps_encoder_reset(intel_dp);
3907764f6729SVille Syrjälä }
3908764f6729SVille Syrjälä 
3909379bc100SJani Nikula static const struct drm_encoder_funcs intel_ddi_funcs = {
3910764f6729SVille Syrjälä 	.reset = intel_ddi_encoder_reset,
3911379bc100SJani Nikula 	.destroy = intel_ddi_encoder_destroy,
3912379bc100SJani Nikula };
3913379bc100SJani Nikula 
3914379bc100SJani Nikula static struct intel_connector *
39157801f3b7SLucas De Marchi intel_ddi_init_dp_connector(struct intel_digital_port *dig_port)
3916379bc100SJani Nikula {
3917379bc100SJani Nikula 	struct intel_connector *connector;
39187801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
3919379bc100SJani Nikula 
3920379bc100SJani Nikula 	connector = intel_connector_alloc();
3921379bc100SJani Nikula 	if (!connector)
3922379bc100SJani Nikula 		return NULL;
3923379bc100SJani Nikula 
39247801f3b7SLucas De Marchi 	dig_port->dp.output_reg = DDI_BUF_CTL(port);
39257801f3b7SLucas De Marchi 	dig_port->dp.prepare_link_retrain = intel_ddi_prepare_link_retrain;
39267801f3b7SLucas De Marchi 	dig_port->dp.set_link_train = intel_ddi_set_link_train;
39277801f3b7SLucas De Marchi 	dig_port->dp.set_idle_link_train = intel_ddi_set_idle_link_train;
3928eee3f911SVille Syrjälä 
39297801f3b7SLucas De Marchi 	dig_port->dp.voltage_max = intel_ddi_dp_voltage_max;
39307801f3b7SLucas De Marchi 	dig_port->dp.preemph_max = intel_ddi_dp_preemph_max;
393153de0a20SVille Syrjälä 
39327801f3b7SLucas De Marchi 	if (!intel_dp_init_connector(dig_port, connector)) {
3933379bc100SJani Nikula 		kfree(connector);
3934379bc100SJani Nikula 		return NULL;
3935379bc100SJani Nikula 	}
3936379bc100SJani Nikula 
3937379bc100SJani Nikula 	return connector;
3938379bc100SJani Nikula }
3939379bc100SJani Nikula 
3940379bc100SJani Nikula static int modeset_pipe(struct drm_crtc *crtc,
3941379bc100SJani Nikula 			struct drm_modeset_acquire_ctx *ctx)
3942379bc100SJani Nikula {
3943379bc100SJani Nikula 	struct drm_atomic_state *state;
3944379bc100SJani Nikula 	struct drm_crtc_state *crtc_state;
3945379bc100SJani Nikula 	int ret;
3946379bc100SJani Nikula 
3947379bc100SJani Nikula 	state = drm_atomic_state_alloc(crtc->dev);
3948379bc100SJani Nikula 	if (!state)
3949379bc100SJani Nikula 		return -ENOMEM;
3950379bc100SJani Nikula 
3951379bc100SJani Nikula 	state->acquire_ctx = ctx;
3952379bc100SJani Nikula 
3953379bc100SJani Nikula 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
3954379bc100SJani Nikula 	if (IS_ERR(crtc_state)) {
3955379bc100SJani Nikula 		ret = PTR_ERR(crtc_state);
3956379bc100SJani Nikula 		goto out;
3957379bc100SJani Nikula 	}
3958379bc100SJani Nikula 
3959379bc100SJani Nikula 	crtc_state->connectors_changed = true;
3960379bc100SJani Nikula 
3961379bc100SJani Nikula 	ret = drm_atomic_commit(state);
3962379bc100SJani Nikula out:
3963379bc100SJani Nikula 	drm_atomic_state_put(state);
3964379bc100SJani Nikula 
3965379bc100SJani Nikula 	return ret;
3966379bc100SJani Nikula }
3967379bc100SJani Nikula 
3968379bc100SJani Nikula static int intel_hdmi_reset_link(struct intel_encoder *encoder,
3969379bc100SJani Nikula 				 struct drm_modeset_acquire_ctx *ctx)
3970379bc100SJani Nikula {
3971379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3972b7d02c3aSVille Syrjälä 	struct intel_hdmi *hdmi = enc_to_intel_hdmi(encoder);
3973379bc100SJani Nikula 	struct intel_connector *connector = hdmi->attached_connector;
3974379bc100SJani Nikula 	struct i2c_adapter *adapter =
3975379bc100SJani Nikula 		intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
3976379bc100SJani Nikula 	struct drm_connector_state *conn_state;
3977379bc100SJani Nikula 	struct intel_crtc_state *crtc_state;
3978379bc100SJani Nikula 	struct intel_crtc *crtc;
3979379bc100SJani Nikula 	u8 config;
3980379bc100SJani Nikula 	int ret;
3981379bc100SJani Nikula 
3982379bc100SJani Nikula 	if (!connector || connector->base.status != connector_status_connected)
3983379bc100SJani Nikula 		return 0;
3984379bc100SJani Nikula 
3985379bc100SJani Nikula 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
3986379bc100SJani Nikula 			       ctx);
3987379bc100SJani Nikula 	if (ret)
3988379bc100SJani Nikula 		return ret;
3989379bc100SJani Nikula 
3990379bc100SJani Nikula 	conn_state = connector->base.state;
3991379bc100SJani Nikula 
3992379bc100SJani Nikula 	crtc = to_intel_crtc(conn_state->crtc);
3993379bc100SJani Nikula 	if (!crtc)
3994379bc100SJani Nikula 		return 0;
3995379bc100SJani Nikula 
3996379bc100SJani Nikula 	ret = drm_modeset_lock(&crtc->base.mutex, ctx);
3997379bc100SJani Nikula 	if (ret)
3998379bc100SJani Nikula 		return ret;
3999379bc100SJani Nikula 
4000379bc100SJani Nikula 	crtc_state = to_intel_crtc_state(crtc->base.state);
4001379bc100SJani Nikula 
40021de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm,
40031de143ccSPankaj Bharadiya 		    !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI));
4004379bc100SJani Nikula 
40051326a92cSMaarten Lankhorst 	if (!crtc_state->hw.active)
4006379bc100SJani Nikula 		return 0;
4007379bc100SJani Nikula 
4008379bc100SJani Nikula 	if (!crtc_state->hdmi_high_tmds_clock_ratio &&
4009379bc100SJani Nikula 	    !crtc_state->hdmi_scrambling)
4010379bc100SJani Nikula 		return 0;
4011379bc100SJani Nikula 
4012379bc100SJani Nikula 	if (conn_state->commit &&
4013379bc100SJani Nikula 	    !try_wait_for_completion(&conn_state->commit->hw_done))
4014379bc100SJani Nikula 		return 0;
4015379bc100SJani Nikula 
4016379bc100SJani Nikula 	ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
4017379bc100SJani Nikula 	if (ret < 0) {
401847bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Failed to read TMDS config: %d\n",
401947bdb1caSJani Nikula 			ret);
4020379bc100SJani Nikula 		return 0;
4021379bc100SJani Nikula 	}
4022379bc100SJani Nikula 
4023379bc100SJani Nikula 	if (!!(config & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40) ==
4024379bc100SJani Nikula 	    crtc_state->hdmi_high_tmds_clock_ratio &&
4025379bc100SJani Nikula 	    !!(config & SCDC_SCRAMBLING_ENABLE) ==
4026379bc100SJani Nikula 	    crtc_state->hdmi_scrambling)
4027379bc100SJani Nikula 		return 0;
4028379bc100SJani Nikula 
4029379bc100SJani Nikula 	/*
4030379bc100SJani Nikula 	 * HDMI 2.0 says that one should not send scrambled data
4031379bc100SJani Nikula 	 * prior to configuring the sink scrambling, and that
4032379bc100SJani Nikula 	 * TMDS clock/data transmission should be suspended when
4033379bc100SJani Nikula 	 * changing the TMDS clock rate in the sink. So let's
4034379bc100SJani Nikula 	 * just do a full modeset here, even though some sinks
4035379bc100SJani Nikula 	 * would be perfectly happy if were to just reconfigure
4036379bc100SJani Nikula 	 * the SCDC settings on the fly.
4037379bc100SJani Nikula 	 */
4038379bc100SJani Nikula 	return modeset_pipe(&crtc->base, ctx);
4039379bc100SJani Nikula }
4040379bc100SJani Nikula 
40413944709dSImre Deak static enum intel_hotplug_state
40423944709dSImre Deak intel_ddi_hotplug(struct intel_encoder *encoder,
40438c8919c7SImre Deak 		  struct intel_connector *connector)
4044379bc100SJani Nikula {
4045b4df5405SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4046b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4047699390f7SVille Syrjälä 	struct intel_dp *intel_dp = &dig_port->dp;
4048b4df5405SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4049b4df5405SImre Deak 	bool is_tc = intel_phy_is_tc(i915, phy);
4050379bc100SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
40513944709dSImre Deak 	enum intel_hotplug_state state;
4052379bc100SJani Nikula 	int ret;
4053379bc100SJani Nikula 
4054699390f7SVille Syrjälä 	if (intel_dp->compliance.test_active &&
4055699390f7SVille Syrjälä 	    intel_dp->compliance.test_type == DP_TEST_LINK_PHY_TEST_PATTERN) {
4056699390f7SVille Syrjälä 		intel_dp_phy_test(encoder);
4057699390f7SVille Syrjälä 		/* just do the PHY test and nothing else */
4058699390f7SVille Syrjälä 		return INTEL_HOTPLUG_UNCHANGED;
4059699390f7SVille Syrjälä 	}
4060699390f7SVille Syrjälä 
40618c8919c7SImre Deak 	state = intel_encoder_hotplug(encoder, connector);
4062379bc100SJani Nikula 
4063379bc100SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
4064379bc100SJani Nikula 
4065379bc100SJani Nikula 	for (;;) {
4066379bc100SJani Nikula 		if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA)
4067379bc100SJani Nikula 			ret = intel_hdmi_reset_link(encoder, &ctx);
4068379bc100SJani Nikula 		else
4069379bc100SJani Nikula 			ret = intel_dp_retrain_link(encoder, &ctx);
4070379bc100SJani Nikula 
4071379bc100SJani Nikula 		if (ret == -EDEADLK) {
4072379bc100SJani Nikula 			drm_modeset_backoff(&ctx);
4073379bc100SJani Nikula 			continue;
4074379bc100SJani Nikula 		}
4075379bc100SJani Nikula 
4076379bc100SJani Nikula 		break;
4077379bc100SJani Nikula 	}
4078379bc100SJani Nikula 
4079379bc100SJani Nikula 	drm_modeset_drop_locks(&ctx);
4080379bc100SJani Nikula 	drm_modeset_acquire_fini(&ctx);
40813a47ae20SPankaj Bharadiya 	drm_WARN(encoder->base.dev, ret,
40823a47ae20SPankaj Bharadiya 		 "Acquiring modeset locks failed with %i\n", ret);
4083379bc100SJani Nikula 
4084bb80c925SJosé Roberto de Souza 	/*
4085bb80c925SJosé Roberto de Souza 	 * Unpowered type-c dongles can take some time to boot and be
4086bb80c925SJosé Roberto de Souza 	 * responsible, so here giving some time to those dongles to power up
4087bb80c925SJosé Roberto de Souza 	 * and then retrying the probe.
4088bb80c925SJosé Roberto de Souza 	 *
4089bb80c925SJosé Roberto de Souza 	 * On many platforms the HDMI live state signal is known to be
4090bb80c925SJosé Roberto de Souza 	 * unreliable, so we can't use it to detect if a sink is connected or
4091bb80c925SJosé Roberto de Souza 	 * not. Instead we detect if it's connected based on whether we can
4092bb80c925SJosé Roberto de Souza 	 * read the EDID or not. That in turn has a problem during disconnect,
4093bb80c925SJosé Roberto de Souza 	 * since the HPD interrupt may be raised before the DDC lines get
4094bb80c925SJosé Roberto de Souza 	 * disconnected (due to how the required length of DDC vs. HPD
4095bb80c925SJosé Roberto de Souza 	 * connector pins are specified) and so we'll still be able to get a
4096bb80c925SJosé Roberto de Souza 	 * valid EDID. To solve this schedule another detection cycle if this
4097bb80c925SJosé Roberto de Souza 	 * time around we didn't detect any change in the sink's connection
4098bb80c925SJosé Roberto de Souza 	 * status.
4099b4df5405SImre Deak 	 *
4100b4df5405SImre Deak 	 * Type-c connectors which get their HPD signal deasserted then
4101b4df5405SImre Deak 	 * reasserted, without unplugging/replugging the sink from the
4102b4df5405SImre Deak 	 * connector, introduce a delay until the AUX channel communication
4103b4df5405SImre Deak 	 * becomes functional. Retry the detection for 5 seconds on type-c
4104b4df5405SImre Deak 	 * connectors to account for this delay.
4105bb80c925SJosé Roberto de Souza 	 */
4106b4df5405SImre Deak 	if (state == INTEL_HOTPLUG_UNCHANGED &&
4107b4df5405SImre Deak 	    connector->hotplug_retries < (is_tc ? 5 : 1) &&
4108bb80c925SJosé Roberto de Souza 	    !dig_port->dp.is_mst)
4109bb80c925SJosé Roberto de Souza 		state = INTEL_HOTPLUG_RETRY;
4110bb80c925SJosé Roberto de Souza 
41113944709dSImre Deak 	return state;
4112379bc100SJani Nikula }
4113379bc100SJani Nikula 
4114edc0e09cSVille Syrjälä static bool lpt_digital_port_connected(struct intel_encoder *encoder)
4115edc0e09cSVille Syrjälä {
4116edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4117c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.pch_hpd[encoder->hpd_pin];
4118edc0e09cSVille Syrjälä 
4119edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, SDEISR) & bit;
4120edc0e09cSVille Syrjälä }
4121edc0e09cSVille Syrjälä 
4122edc0e09cSVille Syrjälä static bool hsw_digital_port_connected(struct intel_encoder *encoder)
4123edc0e09cSVille Syrjälä {
4124edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4125c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4126edc0e09cSVille Syrjälä 
4127c7e8a3d6SVille Syrjälä 	return intel_de_read(dev_priv, DEISR) & bit;
4128edc0e09cSVille Syrjälä }
4129edc0e09cSVille Syrjälä 
4130edc0e09cSVille Syrjälä static bool bdw_digital_port_connected(struct intel_encoder *encoder)
4131edc0e09cSVille Syrjälä {
4132edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4133c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4134edc0e09cSVille Syrjälä 
4135edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, GEN8_DE_PORT_ISR) & bit;
4136edc0e09cSVille Syrjälä }
4137edc0e09cSVille Syrjälä 
4138379bc100SJani Nikula static struct intel_connector *
41397801f3b7SLucas De Marchi intel_ddi_init_hdmi_connector(struct intel_digital_port *dig_port)
4140379bc100SJani Nikula {
4141379bc100SJani Nikula 	struct intel_connector *connector;
41427801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4143379bc100SJani Nikula 
4144379bc100SJani Nikula 	connector = intel_connector_alloc();
4145379bc100SJani Nikula 	if (!connector)
4146379bc100SJani Nikula 		return NULL;
4147379bc100SJani Nikula 
41487801f3b7SLucas De Marchi 	dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
41497801f3b7SLucas De Marchi 	intel_hdmi_init_connector(dig_port, connector);
4150379bc100SJani Nikula 
4151379bc100SJani Nikula 	return connector;
4152379bc100SJani Nikula }
4153379bc100SJani Nikula 
41547801f3b7SLucas De Marchi static bool intel_ddi_a_force_4_lanes(struct intel_digital_port *dig_port)
4155379bc100SJani Nikula {
41567801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4157379bc100SJani Nikula 
41587801f3b7SLucas De Marchi 	if (dig_port->base.port != PORT_A)
4159379bc100SJani Nikula 		return false;
4160379bc100SJani Nikula 
41617801f3b7SLucas De Marchi 	if (dig_port->saved_port_bits & DDI_A_4_LANES)
4162379bc100SJani Nikula 		return false;
4163379bc100SJani Nikula 
4164379bc100SJani Nikula 	/* Broxton/Geminilake: Bspec says that DDI_A_4_LANES is the only
4165379bc100SJani Nikula 	 *                     supported configuration
4166379bc100SJani Nikula 	 */
41672446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
4168379bc100SJani Nikula 		return true;
4169379bc100SJani Nikula 
4170379bc100SJani Nikula 	return false;
4171379bc100SJani Nikula }
4172379bc100SJani Nikula 
4173379bc100SJani Nikula static int
41747801f3b7SLucas De Marchi intel_ddi_max_lanes(struct intel_digital_port *dig_port)
4175379bc100SJani Nikula {
41767801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
41777801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4178379bc100SJani Nikula 	int max_lanes = 4;
4179379bc100SJani Nikula 
4180005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
4181379bc100SJani Nikula 		return max_lanes;
4182379bc100SJani Nikula 
4183379bc100SJani Nikula 	if (port == PORT_A || port == PORT_E) {
4184f7960e7fSJani Nikula 		if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
4185379bc100SJani Nikula 			max_lanes = port == PORT_A ? 4 : 0;
4186379bc100SJani Nikula 		else
4187379bc100SJani Nikula 			/* Both A and E share 2 lanes */
4188379bc100SJani Nikula 			max_lanes = 2;
4189379bc100SJani Nikula 	}
4190379bc100SJani Nikula 
4191379bc100SJani Nikula 	/*
4192379bc100SJani Nikula 	 * Some BIOS might fail to set this bit on port A if eDP
4193379bc100SJani Nikula 	 * wasn't lit up at boot.  Force this bit set when needed
4194379bc100SJani Nikula 	 * so we use the proper lane count for our calculations.
4195379bc100SJani Nikula 	 */
41967801f3b7SLucas De Marchi 	if (intel_ddi_a_force_4_lanes(dig_port)) {
419747bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
419847bdb1caSJani Nikula 			    "Forcing DDI_A_4_LANES for port A\n");
41997801f3b7SLucas De Marchi 		dig_port->saved_port_bits |= DDI_A_4_LANES;
4200379bc100SJani Nikula 		max_lanes = 4;
4201379bc100SJani Nikula 	}
4202379bc100SJani Nikula 
4203379bc100SJani Nikula 	return max_lanes;
4204379bc100SJani Nikula }
4205379bc100SJani Nikula 
4206ddff9a60SMatt Roper static bool hti_uses_phy(struct drm_i915_private *i915, enum phy phy)
4207ddff9a60SMatt Roper {
4208ddff9a60SMatt Roper 	return i915->hti_state & HDPORT_ENABLED &&
4209ff7fb44dSJosé Roberto de Souza 	       i915->hti_state & HDPORT_DDI_USED(phy);
4210ddff9a60SMatt Roper }
4211ddff9a60SMatt Roper 
4212ed2615a8SMatt Roper static enum hpd_pin xelpd_hpd_pin(struct drm_i915_private *dev_priv,
4213ed2615a8SMatt Roper 				  enum port port)
4214ed2615a8SMatt Roper {
4215ed2615a8SMatt Roper 	if (port >= PORT_D_XELPD)
4216ed2615a8SMatt Roper 		return HPD_PORT_D + port - PORT_D_XELPD;
4217ed2615a8SMatt Roper 	else if (port >= PORT_TC1)
4218ed2615a8SMatt Roper 		return HPD_PORT_TC1 + port - PORT_TC1;
4219ed2615a8SMatt Roper 	else
4220ed2615a8SMatt Roper 		return HPD_PORT_A + port - PORT_A;
4221ed2615a8SMatt Roper }
4222ed2615a8SMatt Roper 
4223229f31e2SLucas De Marchi static enum hpd_pin dg1_hpd_pin(struct drm_i915_private *dev_priv,
4224229f31e2SLucas De Marchi 				enum port port)
4225229f31e2SLucas De Marchi {
42261d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
42271d8ca002SVille Syrjälä 		return HPD_PORT_C + port - PORT_TC1;
4228229f31e2SLucas De Marchi 	else
4229229f31e2SLucas De Marchi 		return HPD_PORT_A + port - PORT_A;
4230229f31e2SLucas De Marchi }
4231229f31e2SLucas De Marchi 
4232da51e4baSVille Syrjälä static enum hpd_pin tgl_hpd_pin(struct drm_i915_private *dev_priv,
4233da51e4baSVille Syrjälä 				enum port port)
4234da51e4baSVille Syrjälä {
42351d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
42361d8ca002SVille Syrjälä 		return HPD_PORT_TC1 + port - PORT_TC1;
4237da51e4baSVille Syrjälä 	else
4238da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4239da51e4baSVille Syrjälä }
4240da51e4baSVille Syrjälä 
4241da51e4baSVille Syrjälä static enum hpd_pin rkl_hpd_pin(struct drm_i915_private *dev_priv,
4242da51e4baSVille Syrjälä 				enum port port)
4243da51e4baSVille Syrjälä {
4244da51e4baSVille Syrjälä 	if (HAS_PCH_TGP(dev_priv))
4245da51e4baSVille Syrjälä 		return tgl_hpd_pin(dev_priv, port);
4246da51e4baSVille Syrjälä 
42471d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
42481d8ca002SVille Syrjälä 		return HPD_PORT_C + port - PORT_TC1;
4249da51e4baSVille Syrjälä 	else
4250da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4251da51e4baSVille Syrjälä }
4252da51e4baSVille Syrjälä 
4253da51e4baSVille Syrjälä static enum hpd_pin icl_hpd_pin(struct drm_i915_private *dev_priv,
4254da51e4baSVille Syrjälä 				enum port port)
4255da51e4baSVille Syrjälä {
4256da51e4baSVille Syrjälä 	if (port >= PORT_C)
4257da51e4baSVille Syrjälä 		return HPD_PORT_TC1 + port - PORT_C;
4258da51e4baSVille Syrjälä 	else
4259da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4260da51e4baSVille Syrjälä }
4261da51e4baSVille Syrjälä 
4262da51e4baSVille Syrjälä static enum hpd_pin ehl_hpd_pin(struct drm_i915_private *dev_priv,
4263da51e4baSVille Syrjälä 				enum port port)
4264da51e4baSVille Syrjälä {
4265da51e4baSVille Syrjälä 	if (port == PORT_D)
4266da51e4baSVille Syrjälä 		return HPD_PORT_A;
4267da51e4baSVille Syrjälä 
4268da51e4baSVille Syrjälä 	if (HAS_PCH_MCC(dev_priv))
4269da51e4baSVille Syrjälä 		return icl_hpd_pin(dev_priv, port);
4270da51e4baSVille Syrjälä 
4271da51e4baSVille Syrjälä 	return HPD_PORT_A + port - PORT_A;
4272da51e4baSVille Syrjälä }
4273da51e4baSVille Syrjälä 
4274c8455098SLyude Paul static enum hpd_pin skl_hpd_pin(struct drm_i915_private *dev_priv, enum port port)
4275c8455098SLyude Paul {
4276c8455098SLyude Paul 	if (HAS_PCH_TGP(dev_priv))
4277c8455098SLyude Paul 		return icl_hpd_pin(dev_priv, port);
4278c8455098SLyude Paul 
4279c8455098SLyude Paul 	return HPD_PORT_A + port - PORT_A;
4280c8455098SLyude Paul }
4281c8455098SLyude Paul 
428236ecb0ecSVille Syrjälä static bool intel_ddi_is_tc(struct drm_i915_private *i915, enum port port)
428336ecb0ecSVille Syrjälä {
4284005e9537SMatt Roper 	if (DISPLAY_VER(i915) >= 12)
428536ecb0ecSVille Syrjälä 		return port >= PORT_TC1;
4286005e9537SMatt Roper 	else if (DISPLAY_VER(i915) >= 11)
428736ecb0ecSVille Syrjälä 		return port >= PORT_C;
428836ecb0ecSVille Syrjälä 	else
428936ecb0ecSVille Syrjälä 		return false;
429036ecb0ecSVille Syrjälä }
429136ecb0ecSVille Syrjälä 
4292151ec347SImre Deak static void intel_ddi_encoder_suspend(struct intel_encoder *encoder)
4293151ec347SImre Deak {
4294151ec347SImre Deak 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4295151ec347SImre Deak 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4296151ec347SImre Deak 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4297151ec347SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4298151ec347SImre Deak 
4299151ec347SImre Deak 	intel_dp_encoder_suspend(encoder);
4300151ec347SImre Deak 
4301151ec347SImre Deak 	if (!intel_phy_is_tc(i915, phy))
4302151ec347SImre Deak 		return;
4303151ec347SImre Deak 
43043e0abc76SImre Deak 	intel_tc_port_flush_work(dig_port);
4305151ec347SImre Deak }
4306151ec347SImre Deak 
4307151ec347SImre Deak static void intel_ddi_encoder_shutdown(struct intel_encoder *encoder)
4308151ec347SImre Deak {
4309151ec347SImre Deak 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4310151ec347SImre Deak 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4311151ec347SImre Deak 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4312151ec347SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4313151ec347SImre Deak 
4314151ec347SImre Deak 	intel_dp_encoder_shutdown(encoder);
4315*49c55f7bSVille Syrjälä 	intel_hdmi_encoder_shutdown(encoder);
4316151ec347SImre Deak 
4317151ec347SImre Deak 	if (!intel_phy_is_tc(i915, phy))
4318151ec347SImre Deak 		return;
4319151ec347SImre Deak 
43203e0abc76SImre Deak 	intel_tc_port_flush_work(dig_port);
4321151ec347SImre Deak }
4322151ec347SImre Deak 
432383566d13SVille Syrjälä #define port_tc_name(port) ((port) - PORT_TC1 + '1')
432483566d13SVille Syrjälä #define tc_port_name(tc_port) ((tc_port) - TC_PORT_1 + '1')
432583566d13SVille Syrjälä 
4326379bc100SJani Nikula void intel_ddi_init(struct drm_i915_private *dev_priv, enum port port)
4327379bc100SJani Nikula {
43287801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port;
432970dfbc29SLucas De Marchi 	struct intel_encoder *encoder;
433045c0673aSJani Nikula 	const struct intel_bios_encoder_data *devdata;
4331f542d671SKai-Heng Feng 	bool init_hdmi, init_dp;
4332d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
4333379bc100SJani Nikula 
4334ddff9a60SMatt Roper 	/*
4335ddff9a60SMatt Roper 	 * On platforms with HTI (aka HDPORT), if it's enabled at boot it may
4336ddff9a60SMatt Roper 	 * have taken over some of the PHYs and made them unavailable to the
4337ddff9a60SMatt Roper 	 * driver.  In that case we should skip initializing the corresponding
4338ddff9a60SMatt Roper 	 * outputs.
4339ddff9a60SMatt Roper 	 */
4340ddff9a60SMatt Roper 	if (hti_uses_phy(dev_priv, phy)) {
4341ddff9a60SMatt Roper 		drm_dbg_kms(&dev_priv->drm, "PORT %c / PHY %c reserved by HTI\n",
4342ddff9a60SMatt Roper 			    port_name(port), phy_name(phy));
4343ddff9a60SMatt Roper 		return;
4344ddff9a60SMatt Roper 	}
4345ddff9a60SMatt Roper 
434645c0673aSJani Nikula 	devdata = intel_bios_encoder_data_lookup(dev_priv, port);
434745c0673aSJani Nikula 	if (!devdata) {
434845c0673aSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
434945c0673aSJani Nikula 			    "VBT says port %c is not present\n",
435045c0673aSJani Nikula 			    port_name(port));
435145c0673aSJani Nikula 		return;
435245c0673aSJani Nikula 	}
435345c0673aSJani Nikula 
435445c0673aSJani Nikula 	init_hdmi = intel_bios_encoder_supports_dvi(devdata) ||
435545c0673aSJani Nikula 		intel_bios_encoder_supports_hdmi(devdata);
435645c0673aSJani Nikula 	init_dp = intel_bios_encoder_supports_dp(devdata);
4357379bc100SJani Nikula 
4358379bc100SJani Nikula 	if (intel_bios_is_lspcon_present(dev_priv, port)) {
4359379bc100SJani Nikula 		/*
4360379bc100SJani Nikula 		 * Lspcon device needs to be driven with DP connector
4361379bc100SJani Nikula 		 * with special detection sequence. So make sure DP
4362379bc100SJani Nikula 		 * is initialized before lspcon.
4363379bc100SJani Nikula 		 */
4364379bc100SJani Nikula 		init_dp = true;
4365379bc100SJani Nikula 		init_hdmi = false;
436647bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm, "VBT says port %c has lspcon\n",
436747bdb1caSJani Nikula 			    port_name(port));
4368379bc100SJani Nikula 	}
4369379bc100SJani Nikula 
4370379bc100SJani Nikula 	if (!init_dp && !init_hdmi) {
437147bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
437247bdb1caSJani Nikula 			    "VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
4373379bc100SJani Nikula 			    port_name(port));
4374379bc100SJani Nikula 		return;
4375379bc100SJani Nikula 	}
4376379bc100SJani Nikula 
43777801f3b7SLucas De Marchi 	dig_port = kzalloc(sizeof(*dig_port), GFP_KERNEL);
43787801f3b7SLucas De Marchi 	if (!dig_port)
4379379bc100SJani Nikula 		return;
4380379bc100SJani Nikula 
43817801f3b7SLucas De Marchi 	encoder = &dig_port->base;
4382c0a950d1SJani Nikula 	encoder->devdata = devdata;
4383379bc100SJani Nikula 
4384ed2615a8SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 13 && port >= PORT_D_XELPD) {
4385ed2615a8SMatt Roper 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
4386ed2615a8SMatt Roper 				 DRM_MODE_ENCODER_TMDS,
4387ed2615a8SMatt Roper 				 "DDI %c/PHY %c",
4388ed2615a8SMatt Roper 				 port_name(port - PORT_D_XELPD + PORT_D),
4389ed2615a8SMatt Roper 				 phy_name(phy));
4390ed2615a8SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 12) {
43912d709a5aSVille Syrjälä 		enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
43922d709a5aSVille Syrjälä 
439370dfbc29SLucas De Marchi 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
43942d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
43952d709a5aSVille Syrjälä 				 "DDI %s%c/PHY %s%c",
43962d709a5aSVille Syrjälä 				 port >= PORT_TC1 ? "TC" : "",
439783566d13SVille Syrjälä 				 port >= PORT_TC1 ? port_tc_name(port) : port_name(port),
43982d709a5aSVille Syrjälä 				 tc_port != TC_PORT_NONE ? "TC" : "",
439983566d13SVille Syrjälä 				 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
4400005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 11) {
44012d709a5aSVille Syrjälä 		enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
44022d709a5aSVille Syrjälä 
44032d709a5aSVille Syrjälä 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
44042d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
44052d709a5aSVille Syrjälä 				 "DDI %c%s/PHY %s%c",
44062d709a5aSVille Syrjälä 				 port_name(port),
44072d709a5aSVille Syrjälä 				 port >= PORT_C ? " (TC)" : "",
44082d709a5aSVille Syrjälä 				 tc_port != TC_PORT_NONE ? "TC" : "",
440983566d13SVille Syrjälä 				 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
44102d709a5aSVille Syrjälä 	} else {
44112d709a5aSVille Syrjälä 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
44122d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
44132d709a5aSVille Syrjälä 				 "DDI %c/PHY %c", port_name(port),  phy_name(phy));
44142d709a5aSVille Syrjälä 	}
4415379bc100SJani Nikula 
441636e5e704SSean Paul 	mutex_init(&dig_port->hdcp_mutex);
441736e5e704SSean Paul 	dig_port->num_hdcp_streams = 0;
441836e5e704SSean Paul 
441970dfbc29SLucas De Marchi 	encoder->hotplug = intel_ddi_hotplug;
442070dfbc29SLucas De Marchi 	encoder->compute_output_type = intel_ddi_compute_output_type;
442170dfbc29SLucas De Marchi 	encoder->compute_config = intel_ddi_compute_config;
4422b50a1aa6SManasi Navare 	encoder->compute_config_late = intel_ddi_compute_config_late;
442370dfbc29SLucas De Marchi 	encoder->enable = intel_enable_ddi;
442470dfbc29SLucas De Marchi 	encoder->pre_pll_enable = intel_ddi_pre_pll_enable;
442570dfbc29SLucas De Marchi 	encoder->pre_enable = intel_ddi_pre_enable;
442670dfbc29SLucas De Marchi 	encoder->disable = intel_disable_ddi;
442770dfbc29SLucas De Marchi 	encoder->post_disable = intel_ddi_post_disable;
442870dfbc29SLucas De Marchi 	encoder->update_pipe = intel_ddi_update_pipe;
442970dfbc29SLucas De Marchi 	encoder->get_hw_state = intel_ddi_get_hw_state;
4430f9e76a6eSImre Deak 	encoder->sync_state = intel_ddi_sync_state;
4431b671d6efSImre Deak 	encoder->initial_fastset_check = intel_ddi_initial_fastset_check;
4432151ec347SImre Deak 	encoder->suspend = intel_ddi_encoder_suspend;
4433151ec347SImre Deak 	encoder->shutdown = intel_ddi_encoder_shutdown;
443470dfbc29SLucas De Marchi 	encoder->get_power_domains = intel_ddi_get_power_domains;
443570dfbc29SLucas De Marchi 
443670dfbc29SLucas De Marchi 	encoder->type = INTEL_OUTPUT_DDI;
443770dfbc29SLucas De Marchi 	encoder->power_domain = intel_port_to_power_domain(port);
443870dfbc29SLucas De Marchi 	encoder->port = port;
443970dfbc29SLucas De Marchi 	encoder->cloneable = 0;
444070dfbc29SLucas De Marchi 	encoder->pipe_mask = ~0;
4441da51e4baSVille Syrjälä 
4442865b73eaSMatt Roper 	if (IS_DG2(dev_priv)) {
4443f82f2563SMatt Roper 		encoder->enable_clock = intel_mpllb_enable;
4444f82f2563SMatt Roper 		encoder->disable_clock = intel_mpllb_disable;
4445865b73eaSMatt Roper 		encoder->get_config = dg2_ddi_get_config;
4446865b73eaSMatt Roper 	} else if (IS_ALDERLAKE_S(dev_priv)) {
444740b316d4SVille Syrjälä 		encoder->enable_clock = adls_ddi_enable_clock;
444840b316d4SVille Syrjälä 		encoder->disable_clock = adls_ddi_disable_clock;
44490fbd8694SVille Syrjälä 		encoder->is_clock_enabled = adls_ddi_is_clock_enabled;
4450351221ffSVille Syrjälä 		encoder->get_config = adls_ddi_get_config;
445140b316d4SVille Syrjälä 	} else if (IS_ROCKETLAKE(dev_priv)) {
445240b316d4SVille Syrjälä 		encoder->enable_clock = rkl_ddi_enable_clock;
445340b316d4SVille Syrjälä 		encoder->disable_clock = rkl_ddi_disable_clock;
44540fbd8694SVille Syrjälä 		encoder->is_clock_enabled = rkl_ddi_is_clock_enabled;
4455351221ffSVille Syrjälä 		encoder->get_config = rkl_ddi_get_config;
445636ecb0ecSVille Syrjälä 	} else if (IS_DG1(dev_priv)) {
445735bb6b1aSVille Syrjälä 		encoder->enable_clock = dg1_ddi_enable_clock;
445835bb6b1aSVille Syrjälä 		encoder->disable_clock = dg1_ddi_disable_clock;
44590fbd8694SVille Syrjälä 		encoder->is_clock_enabled = dg1_ddi_is_clock_enabled;
4460351221ffSVille Syrjälä 		encoder->get_config = dg1_ddi_get_config;
446136ecb0ecSVille Syrjälä 	} else if (IS_JSL_EHL(dev_priv)) {
446236ecb0ecSVille Syrjälä 		if (intel_ddi_is_tc(dev_priv, port)) {
446336ecb0ecSVille Syrjälä 			encoder->enable_clock = jsl_ddi_tc_enable_clock;
446436ecb0ecSVille Syrjälä 			encoder->disable_clock = jsl_ddi_tc_disable_clock;
44650fbd8694SVille Syrjälä 			encoder->is_clock_enabled = jsl_ddi_tc_is_clock_enabled;
4466351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
446736ecb0ecSVille Syrjälä 		} else {
446836ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_combo_enable_clock;
446936ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_combo_disable_clock;
44700fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
4471351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
447236ecb0ecSVille Syrjälä 		}
4473005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 11) {
447436ecb0ecSVille Syrjälä 		if (intel_ddi_is_tc(dev_priv, port)) {
447536ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_tc_enable_clock;
447636ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_tc_disable_clock;
44770fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_tc_is_clock_enabled;
4478351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_tc_get_config;
447936ecb0ecSVille Syrjälä 		} else {
448036ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_combo_enable_clock;
448136ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_combo_disable_clock;
44820fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
4483351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
448436ecb0ecSVille Syrjälä 		}
44852446e1d6SMatt Roper 	} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
4486351221ffSVille Syrjälä 		/* BXT/GLK have fixed PLL->port mapping */
4487351221ffSVille Syrjälä 		encoder->get_config = bxt_ddi_get_config;
448893e7e61eSLucas De Marchi 	} else if (DISPLAY_VER(dev_priv) == 9) {
448938e31f1aSVille Syrjälä 		encoder->enable_clock = skl_ddi_enable_clock;
449038e31f1aSVille Syrjälä 		encoder->disable_clock = skl_ddi_disable_clock;
44910fbd8694SVille Syrjälä 		encoder->is_clock_enabled = skl_ddi_is_clock_enabled;
4492351221ffSVille Syrjälä 		encoder->get_config = skl_ddi_get_config;
449338e31f1aSVille Syrjälä 	} else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
4494d135368dSVille Syrjälä 		encoder->enable_clock = hsw_ddi_enable_clock;
4495d135368dSVille Syrjälä 		encoder->disable_clock = hsw_ddi_disable_clock;
44960fbd8694SVille Syrjälä 		encoder->is_clock_enabled = hsw_ddi_is_clock_enabled;
4497351221ffSVille Syrjälä 		encoder->get_config = hsw_ddi_get_config;
4498d135368dSVille Syrjälä 	}
4499d135368dSVille Syrjälä 
4500193299adSVille Syrjälä 	if (IS_DG2(dev_priv)) {
4501193299adSVille Syrjälä 		encoder->set_signal_levels = intel_snps_phy_set_signal_levels;
4502193299adSVille Syrjälä 	} else if (DISPLAY_VER(dev_priv) >= 12) {
4503193299adSVille Syrjälä 		if (intel_phy_is_combo(dev_priv, phy))
4504193299adSVille Syrjälä 			encoder->set_signal_levels = icl_combo_phy_set_signal_levels;
4505e722ab8bSVille Syrjälä 		else
4506193299adSVille Syrjälä 			encoder->set_signal_levels = tgl_dkl_phy_set_signal_levels;
4507193299adSVille Syrjälä 	} else if (DISPLAY_VER(dev_priv) >= 11) {
4508193299adSVille Syrjälä 		if (intel_phy_is_combo(dev_priv, phy))
4509193299adSVille Syrjälä 			encoder->set_signal_levels = icl_combo_phy_set_signal_levels;
4510193299adSVille Syrjälä 		else
4511193299adSVille Syrjälä 			encoder->set_signal_levels = icl_mg_phy_set_signal_levels;
4512193299adSVille Syrjälä 	} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
45135f5ada0bSVille Syrjälä 		encoder->set_signal_levels = bxt_ddi_phy_set_signal_levels;
4514193299adSVille Syrjälä 	} else {
4515e722ab8bSVille Syrjälä 		encoder->set_signal_levels = hsw_set_signal_levels;
4516193299adSVille Syrjälä 	}
4517e722ab8bSVille Syrjälä 
4518c40a253bSVille Syrjälä 	intel_ddi_buf_trans_init(encoder);
4519c40a253bSVille Syrjälä 
4520ed2615a8SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 13)
4521ed2615a8SMatt Roper 		encoder->hpd_pin = xelpd_hpd_pin(dev_priv, port);
4522ed2615a8SMatt Roper 	else if (IS_DG1(dev_priv))
4523229f31e2SLucas De Marchi 		encoder->hpd_pin = dg1_hpd_pin(dev_priv, port);
4524229f31e2SLucas De Marchi 	else if (IS_ROCKETLAKE(dev_priv))
4525da51e4baSVille Syrjälä 		encoder->hpd_pin = rkl_hpd_pin(dev_priv, port);
4526005e9537SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 12)
4527da51e4baSVille Syrjälä 		encoder->hpd_pin = tgl_hpd_pin(dev_priv, port);
452824ea098bSTejas Upadhyay 	else if (IS_JSL_EHL(dev_priv))
4529da51e4baSVille Syrjälä 		encoder->hpd_pin = ehl_hpd_pin(dev_priv, port);
453093e7e61eSLucas De Marchi 	else if (DISPLAY_VER(dev_priv) == 11)
4531da51e4baSVille Syrjälä 		encoder->hpd_pin = icl_hpd_pin(dev_priv, port);
453293e7e61eSLucas De Marchi 	else if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv))
4533c8455098SLyude Paul 		encoder->hpd_pin = skl_hpd_pin(dev_priv, port);
4534da51e4baSVille Syrjälä 	else
453503c7e4f1SVille Syrjälä 		encoder->hpd_pin = intel_hpd_pin_default(dev_priv, port);
4536379bc100SJani Nikula 
4537005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
45387801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
45397801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
45407801f3b7SLucas De Marchi 			& DDI_BUF_PORT_REVERSAL;
4541379bc100SJani Nikula 	else
45427801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
45437801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
45447801f3b7SLucas De Marchi 			& (DDI_BUF_PORT_REVERSAL | DDI_A_4_LANES);
454570dfbc29SLucas De Marchi 
4546aaab24bbSUma Shankar 	if (intel_bios_is_lane_reversal_needed(dev_priv, port))
4547aaab24bbSUma Shankar 		dig_port->saved_port_bits |= DDI_BUF_PORT_REVERSAL;
4548aaab24bbSUma Shankar 
45497801f3b7SLucas De Marchi 	dig_port->dp.output_reg = INVALID_MMIO_REG;
45507801f3b7SLucas De Marchi 	dig_port->max_lanes = intel_ddi_max_lanes(dig_port);
45517801f3b7SLucas De Marchi 	dig_port->aux_ch = intel_bios_port_aux_ch(dev_priv, port);
4552379bc100SJani Nikula 
4553d8fe2ab6SMatt Roper 	if (intel_phy_is_tc(dev_priv, phy)) {
4554c5faae5aSJani Nikula 		bool is_legacy =
4555f08fbe6aSJani Nikula 			!intel_bios_encoder_supports_typec_usb(devdata) &&
4556f08fbe6aSJani Nikula 			!intel_bios_encoder_supports_tbt(devdata);
4557379bc100SJani Nikula 
45587801f3b7SLucas De Marchi 		intel_tc_port_init(dig_port, is_legacy);
455924a7bfe0SImre Deak 
456070dfbc29SLucas De Marchi 		encoder->update_prepare = intel_ddi_update_prepare;
456170dfbc29SLucas De Marchi 		encoder->update_complete = intel_ddi_update_complete;
4562ab7bc4e1SImre Deak 	}
4563ab7bc4e1SImre Deak 
45641de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, port > PORT_I);
45657801f3b7SLucas De Marchi 	dig_port->ddi_io_power_domain = POWER_DOMAIN_PORT_DDI_A_IO +
4566327f8d8cSLucas De Marchi 					      port - PORT_A;
4567379bc100SJani Nikula 
4568379bc100SJani Nikula 	if (init_dp) {
45697801f3b7SLucas De Marchi 		if (!intel_ddi_init_dp_connector(dig_port))
4570379bc100SJani Nikula 			goto err;
4571379bc100SJani Nikula 
45727801f3b7SLucas De Marchi 		dig_port->hpd_pulse = intel_dp_hpd_pulse;
4573bc71194eSJani Nikula 
4574f6864b27SJani Nikula 		if (dig_port->dp.mso_link_count)
4575f6864b27SJani Nikula 			encoder->pipe_mask = intel_ddi_splitter_pipe_mask(dev_priv);
4576379bc100SJani Nikula 	}
4577379bc100SJani Nikula 
4578379bc100SJani Nikula 	/* In theory we don't need the encoder->type check, but leave it just in
4579379bc100SJani Nikula 	 * case we have some really bad VBTs... */
458070dfbc29SLucas De Marchi 	if (encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
45817801f3b7SLucas De Marchi 		if (!intel_ddi_init_hdmi_connector(dig_port))
4582379bc100SJani Nikula 			goto err;
4583379bc100SJani Nikula 	}
4584379bc100SJani Nikula 
4585005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
4586edc0e09cSVille Syrjälä 		if (intel_phy_is_tc(dev_priv, phy))
45877801f3b7SLucas De Marchi 			dig_port->connected = intel_tc_port_connected;
4588edc0e09cSVille Syrjälä 		else
45897801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4590005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 8) {
45912446e1d6SMatt Roper 		if (port == PORT_A || IS_GEMINILAKE(dev_priv) ||
45922446e1d6SMatt Roper 		    IS_BROXTON(dev_priv))
45937801f3b7SLucas De Marchi 			dig_port->connected = bdw_digital_port_connected;
4594edc0e09cSVille Syrjälä 		else
45957801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4596edc0e09cSVille Syrjälä 	} else {
4597c7e8a3d6SVille Syrjälä 		if (port == PORT_A)
45987801f3b7SLucas De Marchi 			dig_port->connected = hsw_digital_port_connected;
4599edc0e09cSVille Syrjälä 		else
46007801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4601edc0e09cSVille Syrjälä 	}
4602edc0e09cSVille Syrjälä 
46037801f3b7SLucas De Marchi 	intel_infoframe_init(dig_port);
4604379bc100SJani Nikula 
4605379bc100SJani Nikula 	return;
4606379bc100SJani Nikula 
4607379bc100SJani Nikula err:
460870dfbc29SLucas De Marchi 	drm_encoder_cleanup(&encoder->base);
46097801f3b7SLucas De Marchi 	kfree(dig_port);
4610379bc100SJani Nikula }
4611