xref: /linux/drivers/gpu/drm/i915/display/intel_ddi.c (revision 5c31e9d013b52cc8420ca97e5ae004c9d4b8cf7f)
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 
324379bc100SJani Nikula 	if (pipe_config->has_pch_encoder)
325379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
326379bc100SJani Nikula 						    &pipe_config->fdi_m_n);
327379bc100SJani Nikula 	else if (intel_crtc_has_dp_encoder(pipe_config))
328379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
329379bc100SJani Nikula 						    &pipe_config->dp_m_n);
3302969a78aSImre Deak 	else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp > 24)
3312969a78aSImre Deak 		dotclock = pipe_config->port_clock * 24 / pipe_config->pipe_bpp;
332379bc100SJani Nikula 	else
333379bc100SJani Nikula 		dotclock = pipe_config->port_clock;
334379bc100SJani Nikula 
335379bc100SJani Nikula 	if (pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
336379bc100SJani Nikula 	    !intel_crtc_has_dp_encoder(pipe_config))
337379bc100SJani Nikula 		dotclock *= 2;
338379bc100SJani Nikula 
339379bc100SJani Nikula 	if (pipe_config->pixel_multiplier)
340379bc100SJani Nikula 		dotclock /= pipe_config->pixel_multiplier;
341379bc100SJani Nikula 
3421326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.crtc_clock = dotclock;
343379bc100SJani Nikula }
344379bc100SJani Nikula 
3450c06fa15SGwan-gyeong Mun void intel_ddi_set_dp_msa(const struct intel_crtc_state *crtc_state,
3460c06fa15SGwan-gyeong Mun 			  const struct drm_connector_state *conn_state)
347379bc100SJani Nikula {
3482225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
349379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
350379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
351379bc100SJani Nikula 	u32 temp;
352379bc100SJani Nikula 
353379bc100SJani Nikula 	if (!intel_crtc_has_dp_encoder(crtc_state))
354379bc100SJani Nikula 		return;
355379bc100SJani Nikula 
3561de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder));
357379bc100SJani Nikula 
3583e706dffSVille Syrjälä 	temp = DP_MSA_MISC_SYNC_CLOCK;
359379bc100SJani Nikula 
360379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
361379bc100SJani Nikula 	case 18:
3623e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_6_BPC;
363379bc100SJani Nikula 		break;
364379bc100SJani Nikula 	case 24:
3653e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_8_BPC;
366379bc100SJani Nikula 		break;
367379bc100SJani Nikula 	case 30:
3683e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_10_BPC;
369379bc100SJani Nikula 		break;
370379bc100SJani Nikula 	case 36:
3713e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_12_BPC;
372379bc100SJani Nikula 		break;
373379bc100SJani Nikula 	default:
374379bc100SJani Nikula 		MISSING_CASE(crtc_state->pipe_bpp);
375379bc100SJani Nikula 		break;
376379bc100SJani Nikula 	}
377379bc100SJani Nikula 
378cae154fcSVille Syrjälä 	/* nonsense combination */
3791de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
380cae154fcSVille Syrjälä 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
381cae154fcSVille Syrjälä 
382cae154fcSVille Syrjälä 	if (crtc_state->limited_color_range)
3833e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_CEA_RGB;
384cae154fcSVille Syrjälä 
385379bc100SJani Nikula 	/*
386379bc100SJani Nikula 	 * As per DP 1.2 spec section 2.3.4.3 while sending
387379bc100SJani Nikula 	 * YCBCR 444 signals we should program MSA MISC1/0 fields with
388646d3dc8SVille Syrjälä 	 * colorspace information.
389379bc100SJani Nikula 	 */
390379bc100SJani Nikula 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
3913e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_YCBCR_444_BT709;
392646d3dc8SVille Syrjälä 
393379bc100SJani Nikula 	/*
394379bc100SJani Nikula 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
395379bc100SJani Nikula 	 * of Color Encoding Format and Content Color Gamut] while sending
3960c06fa15SGwan-gyeong Mun 	 * YCBCR 420, HDR BT.2020 signals we should program MSA MISC1 fields
3970c06fa15SGwan-gyeong Mun 	 * which indicate VSC SDP for the Pixel Encoding/Colorimetry Format.
398379bc100SJani Nikula 	 */
399bd8c9ccaSGwan-gyeong Mun 	if (intel_dp_needs_vsc_sdp(crtc_state, conn_state))
4003e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_VSC_SDP;
4010c06fa15SGwan-gyeong Mun 
402f7960e7fSJani Nikula 	intel_de_write(dev_priv, TRANS_MSA_MISC(cpu_transcoder), temp);
403379bc100SJani Nikula }
404379bc100SJani Nikula 
405dc5b8ed5SVille Syrjälä static u32 bdw_trans_port_sync_master_select(enum transcoder master_transcoder)
406dc5b8ed5SVille Syrjälä {
407dc5b8ed5SVille Syrjälä 	if (master_transcoder == TRANSCODER_EDP)
408dc5b8ed5SVille Syrjälä 		return 0;
409dc5b8ed5SVille Syrjälä 	else
410dc5b8ed5SVille Syrjälä 		return master_transcoder + 1;
411dc5b8ed5SVille Syrjälä }
412dc5b8ed5SVille Syrjälä 
41379ac2b1bSJani Nikula static void
41479ac2b1bSJani Nikula intel_ddi_config_transcoder_dp2(struct intel_encoder *encoder,
41579ac2b1bSJani Nikula 				const struct intel_crtc_state *crtc_state)
41679ac2b1bSJani Nikula {
41779ac2b1bSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
41879ac2b1bSJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
41979ac2b1bSJani Nikula 	u32 val = 0;
42079ac2b1bSJani Nikula 
42179ac2b1bSJani Nikula 	if (intel_dp_is_uhbr(crtc_state))
42279ac2b1bSJani Nikula 		val = TRANS_DP2_128B132B_CHANNEL_CODING;
42379ac2b1bSJani Nikula 
42479ac2b1bSJani Nikula 	intel_de_write(i915, TRANS_DP2_CTL(cpu_transcoder), val);
42579ac2b1bSJani Nikula }
42679ac2b1bSJani Nikula 
42799389390SJosé Roberto de Souza /*
42899389390SJosé Roberto de Souza  * Returns the TRANS_DDI_FUNC_CTL value based on CRTC state.
42999389390SJosé Roberto de Souza  *
43099389390SJosé Roberto de Souza  * Only intended to be used by intel_ddi_enable_transcoder_func() and
43199389390SJosé Roberto de Souza  * intel_ddi_config_transcoder_func().
43299389390SJosé Roberto de Souza  */
43399389390SJosé Roberto de Souza static u32
434eed22a46SVille Syrjälä intel_ddi_transcoder_func_reg_val_get(struct intel_encoder *encoder,
435eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
436379bc100SJani Nikula {
4372225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
438379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
439379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
440379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
441379bc100SJani Nikula 	enum port port = encoder->port;
442379bc100SJani Nikula 	u32 temp;
443379bc100SJani Nikula 
444379bc100SJani Nikula 	/* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
445379bc100SJani Nikula 	temp = TRANS_DDI_FUNC_ENABLE;
446005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
447df16b636SMahesh Kumar 		temp |= TGL_TRANS_DDI_SELECT_PORT(port);
448df16b636SMahesh Kumar 	else
449379bc100SJani Nikula 		temp |= TRANS_DDI_SELECT_PORT(port);
450379bc100SJani Nikula 
451379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
452379bc100SJani Nikula 	case 18:
453379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_6;
454379bc100SJani Nikula 		break;
455379bc100SJani Nikula 	case 24:
456379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_8;
457379bc100SJani Nikula 		break;
458379bc100SJani Nikula 	case 30:
459379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_10;
460379bc100SJani Nikula 		break;
461379bc100SJani Nikula 	case 36:
462379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_12;
463379bc100SJani Nikula 		break;
464379bc100SJani Nikula 	default:
465379bc100SJani Nikula 		BUG();
466379bc100SJani Nikula 	}
467379bc100SJani Nikula 
4681326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
469379bc100SJani Nikula 		temp |= TRANS_DDI_PVSYNC;
4701326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
471379bc100SJani Nikula 		temp |= TRANS_DDI_PHSYNC;
472379bc100SJani Nikula 
473379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP) {
474379bc100SJani Nikula 		switch (pipe) {
475379bc100SJani Nikula 		case PIPE_A:
476379bc100SJani Nikula 			/* On Haswell, can only use the always-on power well for
477379bc100SJani Nikula 			 * eDP when not using the panel fitter, and when not
478379bc100SJani Nikula 			 * using motion blur mitigation (which we don't
479379bc100SJani Nikula 			 * support). */
480379bc100SJani Nikula 			if (crtc_state->pch_pfit.force_thru)
481379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
482379bc100SJani Nikula 			else
483379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ON;
484379bc100SJani Nikula 			break;
485379bc100SJani Nikula 		case PIPE_B:
486379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
487379bc100SJani Nikula 			break;
488379bc100SJani Nikula 		case PIPE_C:
489379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
490379bc100SJani Nikula 			break;
491379bc100SJani Nikula 		default:
492379bc100SJani Nikula 			BUG();
493379bc100SJani Nikula 			break;
494379bc100SJani Nikula 		}
495379bc100SJani Nikula 	}
496379bc100SJani Nikula 
497379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
498379bc100SJani Nikula 		if (crtc_state->has_hdmi_sink)
499379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_HDMI;
500379bc100SJani Nikula 		else
501379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_DVI;
502379bc100SJani Nikula 
503379bc100SJani Nikula 		if (crtc_state->hdmi_scrambling)
504379bc100SJani Nikula 			temp |= TRANS_DDI_HDMI_SCRAMBLING;
505379bc100SJani Nikula 		if (crtc_state->hdmi_high_tmds_clock_ratio)
506379bc100SJani Nikula 			temp |= TRANS_DDI_HIGH_TMDS_CHAR_RATE;
507379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
5087bb97db8SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_FDI_OR_128B132B;
509379bc100SJani Nikula 		temp |= (crtc_state->fdi_lanes - 1) << 1;
510379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
51165213594SJani Nikula 		if (intel_dp_is_uhbr(crtc_state))
51265213594SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_FDI_OR_128B132B;
51365213594SJani Nikula 		else
514379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_DP_MST;
515379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
516b3545e08SLucas De Marchi 
517005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12) {
5186671c367SJosé Roberto de Souza 			enum transcoder master;
5196671c367SJosé Roberto de Souza 
5206671c367SJosé Roberto de Souza 			master = crtc_state->mst_master_transcoder;
5211de143ccSPankaj Bharadiya 			drm_WARN_ON(&dev_priv->drm,
5221de143ccSPankaj Bharadiya 				    master == INVALID_TRANSCODER);
5236671c367SJosé Roberto de Souza 			temp |= TRANS_DDI_MST_TRANSPORT_SELECT(master);
5246671c367SJosé Roberto de Souza 		}
525379bc100SJani Nikula 	} else {
526379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_SST;
527379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
528379bc100SJani Nikula 	}
529379bc100SJani Nikula 
53093e7e61eSLucas De Marchi 	if (IS_DISPLAY_VER(dev_priv, 8, 10) &&
531dc5b8ed5SVille Syrjälä 	    crtc_state->master_transcoder != INVALID_TRANSCODER) {
532dc5b8ed5SVille Syrjälä 		u8 master_select =
533dc5b8ed5SVille Syrjälä 			bdw_trans_port_sync_master_select(crtc_state->master_transcoder);
534dc5b8ed5SVille Syrjälä 
535dc5b8ed5SVille Syrjälä 		temp |= TRANS_DDI_PORT_SYNC_ENABLE |
536dc5b8ed5SVille Syrjälä 			TRANS_DDI_PORT_SYNC_MASTER_SELECT(master_select);
537dc5b8ed5SVille Syrjälä 	}
538dc5b8ed5SVille Syrjälä 
53999389390SJosé Roberto de Souza 	return temp;
54099389390SJosé Roberto de Souza }
54199389390SJosé Roberto de Souza 
542eed22a46SVille Syrjälä void intel_ddi_enable_transcoder_func(struct intel_encoder *encoder,
543eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
54499389390SJosé Roberto de Souza {
5452225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
54699389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
54799389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
54899389390SJosé Roberto de Souza 
549005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
550589a4cd6SVille Syrjälä 		enum transcoder master_transcoder = crtc_state->master_transcoder;
551589a4cd6SVille Syrjälä 		u32 ctl2 = 0;
552589a4cd6SVille Syrjälä 
553589a4cd6SVille Syrjälä 		if (master_transcoder != INVALID_TRANSCODER) {
554dc5b8ed5SVille Syrjälä 			u8 master_select =
555dc5b8ed5SVille Syrjälä 				bdw_trans_port_sync_master_select(master_transcoder);
556589a4cd6SVille Syrjälä 
557589a4cd6SVille Syrjälä 			ctl2 |= PORT_SYNC_MODE_ENABLE |
558d4d7d9caSVille Syrjälä 				PORT_SYNC_MODE_MASTER_SELECT(master_select);
559589a4cd6SVille Syrjälä 		}
560589a4cd6SVille Syrjälä 
561589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
562589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), ctl2);
563589a4cd6SVille Syrjälä 	}
564589a4cd6SVille Syrjälä 
565580fbdc5SImre Deak 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder),
566580fbdc5SImre Deak 		       intel_ddi_transcoder_func_reg_val_get(encoder,
567580fbdc5SImre Deak 							     crtc_state));
56899389390SJosé Roberto de Souza }
56999389390SJosé Roberto de Souza 
57099389390SJosé Roberto de Souza /*
57199389390SJosé Roberto de Souza  * Same as intel_ddi_enable_transcoder_func(), but it does not set the enable
57299389390SJosé Roberto de Souza  * bit.
57399389390SJosé Roberto de Souza  */
57499389390SJosé Roberto de Souza static void
575eed22a46SVille Syrjälä intel_ddi_config_transcoder_func(struct intel_encoder *encoder,
576eed22a46SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
57799389390SJosé Roberto de Souza {
5782225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
57999389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
58099389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
581589a4cd6SVille Syrjälä 	u32 ctl;
58299389390SJosé Roberto de Souza 
583eed22a46SVille Syrjälä 	ctl = intel_ddi_transcoder_func_reg_val_get(encoder, crtc_state);
584589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
585589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
586379bc100SJani Nikula }
587379bc100SJani Nikula 
588379bc100SJani Nikula void intel_ddi_disable_transcoder_func(const struct intel_crtc_state *crtc_state)
589379bc100SJani Nikula {
5902225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
591379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
592379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
593589a4cd6SVille Syrjälä 	u32 ctl;
594c59053dcSJosé Roberto de Souza 
595005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
596589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
597589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), 0);
598589a4cd6SVille Syrjälä 
599589a4cd6SVille Syrjälä 	ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
600dc5b8ed5SVille Syrjälä 
6011cfcdbf3SSean Paul 	drm_WARN_ON(crtc->base.dev, ctl & TRANS_DDI_HDCP_SIGNALLING);
6021cfcdbf3SSean Paul 
603589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
604379bc100SJani Nikula 
60593e7e61eSLucas De Marchi 	if (IS_DISPLAY_VER(dev_priv, 8, 10))
606dc5b8ed5SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_SYNC_ENABLE |
607dc5b8ed5SVille Syrjälä 			 TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK);
608dc5b8ed5SVille Syrjälä 
609005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
610919e4f07SJosé Roberto de Souza 		if (!intel_dp_mst_is_master_trans(crtc_state)) {
611589a4cd6SVille Syrjälä 			ctl &= ~(TGL_TRANS_DDI_PORT_MASK |
612919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
613919e4f07SJosé Roberto de Souza 		}
614df16b636SMahesh Kumar 	} else {
615589a4cd6SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_MASK | TRANS_DDI_MODE_SELECT_MASK);
616df16b636SMahesh Kumar 	}
617dc5b8ed5SVille Syrjälä 
618589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
619379bc100SJani Nikula 
620379bc100SJani Nikula 	if (dev_priv->quirks & QUIRK_INCREASE_DDI_DISABLED_TIME &&
621379bc100SJani Nikula 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
62247bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
62347bdb1caSJani Nikula 			    "Quirk Increase DDI disabled time\n");
624379bc100SJani Nikula 		/* Quirk time at 100ms for reliable operation */
625379bc100SJani Nikula 		msleep(100);
626379bc100SJani Nikula 	}
627379bc100SJani Nikula }
628379bc100SJani Nikula 
6291a67a168SAnshuman Gupta int intel_ddi_toggle_hdcp_bits(struct intel_encoder *intel_encoder,
6300b9c9290SSean Paul 			       enum transcoder cpu_transcoder,
6311a67a168SAnshuman Gupta 			       bool enable, u32 hdcp_mask)
632379bc100SJani Nikula {
633379bc100SJani Nikula 	struct drm_device *dev = intel_encoder->base.dev;
634379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
635379bc100SJani Nikula 	intel_wakeref_t wakeref;
636379bc100SJani Nikula 	int ret = 0;
637379bc100SJani Nikula 	u32 tmp;
638379bc100SJani Nikula 
639379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
640379bc100SJani Nikula 						     intel_encoder->power_domain);
6411de143ccSPankaj Bharadiya 	if (drm_WARN_ON(dev, !wakeref))
642379bc100SJani Nikula 		return -ENXIO;
643379bc100SJani Nikula 
6440b9c9290SSean Paul 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
645379bc100SJani Nikula 	if (enable)
6461a67a168SAnshuman Gupta 		tmp |= hdcp_mask;
647379bc100SJani Nikula 	else
6481a67a168SAnshuman Gupta 		tmp &= ~hdcp_mask;
6490b9c9290SSean Paul 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), tmp);
650379bc100SJani Nikula 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
651379bc100SJani Nikula 	return ret;
652379bc100SJani Nikula }
653379bc100SJani Nikula 
654379bc100SJani Nikula bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
655379bc100SJani Nikula {
656379bc100SJani Nikula 	struct drm_device *dev = intel_connector->base.dev;
657379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
658fa7edcd2SVille Syrjälä 	struct intel_encoder *encoder = intel_attached_encoder(intel_connector);
659379bc100SJani Nikula 	int type = intel_connector->base.connector_type;
660379bc100SJani Nikula 	enum port port = encoder->port;
661379bc100SJani Nikula 	enum transcoder cpu_transcoder;
662379bc100SJani Nikula 	intel_wakeref_t wakeref;
663379bc100SJani Nikula 	enum pipe pipe = 0;
664379bc100SJani Nikula 	u32 tmp;
665379bc100SJani Nikula 	bool ret;
666379bc100SJani Nikula 
667379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
668379bc100SJani Nikula 						     encoder->power_domain);
669379bc100SJani Nikula 	if (!wakeref)
670379bc100SJani Nikula 		return false;
671379bc100SJani Nikula 
672379bc100SJani Nikula 	if (!encoder->get_hw_state(encoder, &pipe)) {
673379bc100SJani Nikula 		ret = false;
674379bc100SJani Nikula 		goto out;
675379bc100SJani Nikula 	}
676379bc100SJani Nikula 
67710cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
678379bc100SJani Nikula 		cpu_transcoder = TRANSCODER_EDP;
679379bc100SJani Nikula 	else
680379bc100SJani Nikula 		cpu_transcoder = (enum transcoder) pipe;
681379bc100SJani Nikula 
682f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
683379bc100SJani Nikula 
684379bc100SJani Nikula 	switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
685379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
686379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
687379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_HDMIA;
688379bc100SJani Nikula 		break;
689379bc100SJani Nikula 
690379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
691379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_eDP ||
692379bc100SJani Nikula 		      type == DRM_MODE_CONNECTOR_DisplayPort;
693379bc100SJani Nikula 		break;
694379bc100SJani Nikula 
695379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
696379bc100SJani Nikula 		/* if the transcoder is in MST state then
697379bc100SJani Nikula 		 * connector isn't connected */
698379bc100SJani Nikula 		ret = false;
699379bc100SJani Nikula 		break;
700379bc100SJani Nikula 
7017bb97db8SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI_OR_128B132B:
70265213594SJani Nikula 		if (HAS_DP20(dev_priv))
70365213594SJani Nikula 			/* 128b/132b */
70465213594SJani Nikula 			ret = false;
70565213594SJani Nikula 		else
70665213594SJani Nikula 			/* FDI */
707379bc100SJani Nikula 			ret = type == DRM_MODE_CONNECTOR_VGA;
708379bc100SJani Nikula 		break;
709379bc100SJani Nikula 
710379bc100SJani Nikula 	default:
711379bc100SJani Nikula 		ret = false;
712379bc100SJani Nikula 		break;
713379bc100SJani Nikula 	}
714379bc100SJani Nikula 
715379bc100SJani Nikula out:
716379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
717379bc100SJani Nikula 
718379bc100SJani Nikula 	return ret;
719379bc100SJani Nikula }
720379bc100SJani Nikula 
721379bc100SJani Nikula static void intel_ddi_get_encoder_pipes(struct intel_encoder *encoder,
722379bc100SJani Nikula 					u8 *pipe_mask, bool *is_dp_mst)
723379bc100SJani Nikula {
724379bc100SJani Nikula 	struct drm_device *dev = encoder->base.dev;
725379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
726379bc100SJani Nikula 	enum port port = encoder->port;
727379bc100SJani Nikula 	intel_wakeref_t wakeref;
728379bc100SJani Nikula 	enum pipe p;
729379bc100SJani Nikula 	u32 tmp;
730379bc100SJani Nikula 	u8 mst_pipe_mask;
731379bc100SJani Nikula 
732379bc100SJani Nikula 	*pipe_mask = 0;
733379bc100SJani Nikula 	*is_dp_mst = false;
734379bc100SJani Nikula 
735379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
736379bc100SJani Nikula 						     encoder->power_domain);
737379bc100SJani Nikula 	if (!wakeref)
738379bc100SJani Nikula 		return;
739379bc100SJani Nikula 
740f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
741379bc100SJani Nikula 	if (!(tmp & DDI_BUF_CTL_ENABLE))
742379bc100SJani Nikula 		goto out;
743379bc100SJani Nikula 
74410cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A) {
745f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
746f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
747379bc100SJani Nikula 
748379bc100SJani Nikula 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
749379bc100SJani Nikula 		default:
750379bc100SJani Nikula 			MISSING_CASE(tmp & TRANS_DDI_EDP_INPUT_MASK);
751df561f66SGustavo A. R. Silva 			fallthrough;
752379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ON:
753379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
754379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_A);
755379bc100SJani Nikula 			break;
756379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
757379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_B);
758379bc100SJani Nikula 			break;
759379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
760379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_C);
761379bc100SJani Nikula 			break;
762379bc100SJani Nikula 		}
763379bc100SJani Nikula 
764379bc100SJani Nikula 		goto out;
765379bc100SJani Nikula 	}
766379bc100SJani Nikula 
767379bc100SJani Nikula 	mst_pipe_mask = 0;
768379bc100SJani Nikula 	for_each_pipe(dev_priv, p) {
769379bc100SJani Nikula 		enum transcoder cpu_transcoder = (enum transcoder)p;
770df16b636SMahesh Kumar 		unsigned int port_mask, ddi_select;
7716aa3bef1SJosé Roberto de Souza 		intel_wakeref_t trans_wakeref;
7726aa3bef1SJosé Roberto de Souza 
7736aa3bef1SJosé Roberto de Souza 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
7746aa3bef1SJosé Roberto de Souza 								   POWER_DOMAIN_TRANSCODER(cpu_transcoder));
7756aa3bef1SJosé Roberto de Souza 		if (!trans_wakeref)
7766aa3bef1SJosé Roberto de Souza 			continue;
777df16b636SMahesh Kumar 
778005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12) {
779df16b636SMahesh Kumar 			port_mask = TGL_TRANS_DDI_PORT_MASK;
780df16b636SMahesh Kumar 			ddi_select = TGL_TRANS_DDI_SELECT_PORT(port);
781df16b636SMahesh Kumar 		} else {
782df16b636SMahesh Kumar 			port_mask = TRANS_DDI_PORT_MASK;
783df16b636SMahesh Kumar 			ddi_select = TRANS_DDI_SELECT_PORT(port);
784df16b636SMahesh Kumar 		}
785379bc100SJani Nikula 
786f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
787f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
7886aa3bef1SJosé Roberto de Souza 		intel_display_power_put(dev_priv, POWER_DOMAIN_TRANSCODER(cpu_transcoder),
7896aa3bef1SJosé Roberto de Souza 					trans_wakeref);
790379bc100SJani Nikula 
791df16b636SMahesh Kumar 		if ((tmp & port_mask) != ddi_select)
792379bc100SJani Nikula 			continue;
793379bc100SJani Nikula 
79465213594SJani Nikula 		if ((tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_DP_MST ||
79565213594SJani Nikula 		    (HAS_DP20(dev_priv) &&
79665213594SJani Nikula 		     (tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_FDI_OR_128B132B))
797379bc100SJani Nikula 			mst_pipe_mask |= BIT(p);
798379bc100SJani Nikula 
799379bc100SJani Nikula 		*pipe_mask |= BIT(p);
800379bc100SJani Nikula 	}
801379bc100SJani Nikula 
802379bc100SJani Nikula 	if (!*pipe_mask)
80347bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
80447bdb1caSJani Nikula 			    "No pipe for [ENCODER:%d:%s] found\n",
80566a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name);
806379bc100SJani Nikula 
807379bc100SJani Nikula 	if (!mst_pipe_mask && hweight8(*pipe_mask) > 1) {
80847bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
80947bdb1caSJani Nikula 			    "Multiple pipes for [ENCODER:%d:%s] (pipe_mask %02x)\n",
81066a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
81166a990ddSVille Syrjälä 			    *pipe_mask);
812379bc100SJani Nikula 		*pipe_mask = BIT(ffs(*pipe_mask) - 1);
813379bc100SJani Nikula 	}
814379bc100SJani Nikula 
815379bc100SJani Nikula 	if (mst_pipe_mask && mst_pipe_mask != *pipe_mask)
81647bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
81747bdb1caSJani Nikula 			    "Conflicting MST and non-MST state for [ENCODER:%d:%s] (pipe_mask %02x mst_pipe_mask %02x)\n",
81866a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
81966a990ddSVille Syrjälä 			    *pipe_mask, mst_pipe_mask);
820379bc100SJani Nikula 	else
821379bc100SJani Nikula 		*is_dp_mst = mst_pipe_mask;
822379bc100SJani Nikula 
823379bc100SJani Nikula out:
8242446e1d6SMatt Roper 	if (*pipe_mask && (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))) {
825f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv, BXT_PHY_CTL(port));
826379bc100SJani Nikula 		if ((tmp & (BXT_PHY_CMNLANE_POWERDOWN_ACK |
827379bc100SJani Nikula 			    BXT_PHY_LANE_POWERDOWN_ACK |
828379bc100SJani Nikula 			    BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
82947bdb1caSJani Nikula 			drm_err(&dev_priv->drm,
83047bdb1caSJani Nikula 				"[ENCODER:%d:%s] enabled but PHY powered down? (PHY_CTL %08x)\n",
83147bdb1caSJani Nikula 				encoder->base.base.id, encoder->base.name, tmp);
832379bc100SJani Nikula 	}
833379bc100SJani Nikula 
834379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
835379bc100SJani Nikula }
836379bc100SJani Nikula 
837379bc100SJani Nikula bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
838379bc100SJani Nikula 			    enum pipe *pipe)
839379bc100SJani Nikula {
840379bc100SJani Nikula 	u8 pipe_mask;
841379bc100SJani Nikula 	bool is_mst;
842379bc100SJani Nikula 
843379bc100SJani Nikula 	intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
844379bc100SJani Nikula 
845379bc100SJani Nikula 	if (is_mst || !pipe_mask)
846379bc100SJani Nikula 		return false;
847379bc100SJani Nikula 
848379bc100SJani Nikula 	*pipe = ffs(pipe_mask) - 1;
849379bc100SJani Nikula 
850379bc100SJani Nikula 	return true;
851379bc100SJani Nikula }
852379bc100SJani Nikula 
85381b55ef1SJani Nikula static enum intel_display_power_domain
854379bc100SJani Nikula intel_ddi_main_link_aux_domain(struct intel_digital_port *dig_port)
855379bc100SJani Nikula {
8564da27d5dSLucas De Marchi 	/* ICL+ HW requires corresponding AUX IOs to be powered up for PSR with
857379bc100SJani Nikula 	 * DC states enabled at the same time, while for driver initiated AUX
858379bc100SJani Nikula 	 * transfers we need the same AUX IOs to be powered but with DC states
859379bc100SJani Nikula 	 * disabled. Accordingly use the AUX power domain here which leaves DC
860379bc100SJani Nikula 	 * states enabled.
861379bc100SJani Nikula 	 * However, for non-A AUX ports the corresponding non-EDP transcoders
862379bc100SJani Nikula 	 * would have already enabled power well 2 and DC_OFF. This means we can
863379bc100SJani Nikula 	 * acquire a wider POWER_DOMAIN_AUX_{B,C,D,F} reference instead of a
864379bc100SJani Nikula 	 * specific AUX_IO reference without powering up any extra wells.
865379bc100SJani Nikula 	 * Note that PSR is enabled only on Port A even though this function
866379bc100SJani Nikula 	 * returns the correct domain for other ports too.
867379bc100SJani Nikula 	 */
868379bc100SJani Nikula 	return dig_port->aux_ch == AUX_CH_A ? POWER_DOMAIN_AUX_IO_A :
869379bc100SJani Nikula 					      intel_aux_power_domain(dig_port);
870379bc100SJani Nikula }
871379bc100SJani Nikula 
872379bc100SJani Nikula static void intel_ddi_get_power_domains(struct intel_encoder *encoder,
873379bc100SJani Nikula 					struct intel_crtc_state *crtc_state)
874379bc100SJani Nikula {
875379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
876379bc100SJani Nikula 	struct intel_digital_port *dig_port;
877d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
878379bc100SJani Nikula 
879379bc100SJani Nikula 	/*
880379bc100SJani Nikula 	 * TODO: Add support for MST encoders. Atm, the following should never
881379bc100SJani Nikula 	 * happen since fake-MST encoders don't set their get_power_domains()
882379bc100SJani Nikula 	 * hook.
883379bc100SJani Nikula 	 */
8841de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
8851de143ccSPankaj Bharadiya 			intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)))
886379bc100SJani Nikula 		return;
887379bc100SJani Nikula 
888b7d02c3aSVille Syrjälä 	dig_port = enc_to_dig_port(encoder);
889f77a2db2SImre Deak 
89011a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
891a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
892a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
893f77a2db2SImre Deak 								   dig_port->ddi_io_power_domain);
894a4550977SImre Deak 	}
895379bc100SJani Nikula 
896379bc100SJani Nikula 	/*
897379bc100SJani Nikula 	 * AUX power is only needed for (e)DP mode, and for HDMI mode on TC
898379bc100SJani Nikula 	 * ports.
899379bc100SJani Nikula 	 */
900379bc100SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state) ||
901162e68e1SImre Deak 	    intel_phy_is_tc(dev_priv, phy)) {
902162e68e1SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->aux_wakeref);
903162e68e1SImre Deak 		dig_port->aux_wakeref =
904379bc100SJani Nikula 			intel_display_power_get(dev_priv,
905379bc100SJani Nikula 						intel_ddi_main_link_aux_domain(dig_port));
906379bc100SJani Nikula 	}
907162e68e1SImre Deak }
908379bc100SJani Nikula 
90902a715c3SVille Syrjälä void intel_ddi_enable_pipe_clock(struct intel_encoder *encoder,
91002a715c3SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
911379bc100SJani Nikula {
9122225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
913379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
914379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
915ed2615a8SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
916ed2615a8SMatt Roper 	u32 val;
917379bc100SJani Nikula 
918df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
919ed2615a8SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 13)
920ed2615a8SMatt Roper 			val = TGL_TRANS_CLK_SEL_PORT(phy);
921ed2615a8SMatt Roper 		else if (DISPLAY_VER(dev_priv) >= 12)
922ed2615a8SMatt Roper 			val = TGL_TRANS_CLK_SEL_PORT(encoder->port);
923df16b636SMahesh Kumar 		else
924ed2615a8SMatt Roper 			val = TRANS_CLK_SEL_PORT(encoder->port);
925ed2615a8SMatt Roper 
926ed2615a8SMatt Roper 		intel_de_write(dev_priv, TRANS_CLK_SEL(cpu_transcoder), val);
927379bc100SJani Nikula 	}
928df16b636SMahesh Kumar }
929379bc100SJani Nikula 
930379bc100SJani Nikula void intel_ddi_disable_pipe_clock(const struct intel_crtc_state *crtc_state)
931379bc100SJani Nikula {
9322225f3c6SMaarten Lankhorst 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
933379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
934379bc100SJani Nikula 
935df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
936005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12)
937f7960e7fSJani Nikula 			intel_de_write(dev_priv,
938f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
939df16b636SMahesh Kumar 				       TGL_TRANS_CLK_SEL_DISABLED);
940df16b636SMahesh Kumar 		else
941f7960e7fSJani Nikula 			intel_de_write(dev_priv,
942f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
943379bc100SJani Nikula 				       TRANS_CLK_SEL_DISABLED);
944379bc100SJani Nikula 	}
945df16b636SMahesh Kumar }
946379bc100SJani Nikula 
947379bc100SJani Nikula static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
948379bc100SJani Nikula 				enum port port, u8 iboost)
949379bc100SJani Nikula {
950379bc100SJani Nikula 	u32 tmp;
951379bc100SJani Nikula 
952f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DISPIO_CR_TX_BMU_CR0);
953379bc100SJani Nikula 	tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
954379bc100SJani Nikula 	if (iboost)
955379bc100SJani Nikula 		tmp |= iboost << BALANCE_LEG_SHIFT(port);
956379bc100SJani Nikula 	else
957379bc100SJani Nikula 		tmp |= BALANCE_LEG_DISABLE(port);
958f7960e7fSJani Nikula 	intel_de_write(dev_priv, DISPIO_CR_TX_BMU_CR0, tmp);
959379bc100SJani Nikula }
960379bc100SJani Nikula 
961379bc100SJani Nikula static void skl_ddi_set_iboost(struct intel_encoder *encoder,
962a621860aSVille Syrjälä 			       const struct intel_crtc_state *crtc_state,
963a621860aSVille Syrjälä 			       int level)
964379bc100SJani Nikula {
9657801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
966379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
967379bc100SJani Nikula 	u8 iboost;
968379bc100SJani Nikula 
969a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
970c0a950d1SJani Nikula 		iboost = intel_bios_encoder_hdmi_boost_level(encoder->devdata);
971379bc100SJani Nikula 	else
972c0a950d1SJani Nikula 		iboost = intel_bios_encoder_dp_boost_level(encoder->devdata);
973379bc100SJani Nikula 
974379bc100SJani Nikula 	if (iboost == 0) {
975e505d764SVille Syrjälä 		const struct intel_ddi_buf_trans *trans;
976379bc100SJani Nikula 		int n_entries;
977379bc100SJani Nikula 
978e505d764SVille Syrjälä 		trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
979e505d764SVille Syrjälä 		if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
980379bc100SJani Nikula 			return;
981379bc100SJani Nikula 
982e505d764SVille Syrjälä 		iboost = trans->entries[level].hsw.i_boost;
983379bc100SJani Nikula 	}
984379bc100SJani Nikula 
985379bc100SJani Nikula 	/* Make sure that the requested I_boost is valid */
986379bc100SJani Nikula 	if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
98747bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Invalid I_boost value %u\n", iboost);
988379bc100SJani Nikula 		return;
989379bc100SJani Nikula 	}
990379bc100SJani Nikula 
991f0e86e05SJosé Roberto de Souza 	_skl_ddi_set_iboost(dev_priv, encoder->port, iboost);
992379bc100SJani Nikula 
993f0e86e05SJosé Roberto de Souza 	if (encoder->port == PORT_A && dig_port->max_lanes == 4)
994379bc100SJani Nikula 		_skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
995379bc100SJani Nikula }
996379bc100SJani Nikula 
997a621860aSVille Syrjälä static u8 intel_ddi_dp_voltage_max(struct intel_dp *intel_dp,
998a621860aSVille Syrjälä 				   const struct intel_crtc_state *crtc_state)
999379bc100SJani Nikula {
100053de0a20SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1001379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1002379bc100SJani Nikula 	int n_entries;
1003379bc100SJani Nikula 
1004c40a253bSVille Syrjälä 	encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1005379bc100SJani Nikula 
10061de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, n_entries < 1))
1007379bc100SJani Nikula 		n_entries = 1;
10081de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
10091de143ccSPankaj Bharadiya 			n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
1010379bc100SJani Nikula 		n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
1011379bc100SJani Nikula 
1012379bc100SJani Nikula 	return index_to_dp_signal_levels[n_entries - 1] &
1013379bc100SJani Nikula 		DP_TRAIN_VOLTAGE_SWING_MASK;
1014379bc100SJani Nikula }
1015379bc100SJani Nikula 
1016379bc100SJani Nikula /*
1017379bc100SJani Nikula  * We assume that the full set of pre-emphasis values can be
1018379bc100SJani Nikula  * used on all DDI platforms. Should that change we need to
1019379bc100SJani Nikula  * rethink this code.
1020379bc100SJani Nikula  */
102153de0a20SVille Syrjälä static u8 intel_ddi_dp_preemph_max(struct intel_dp *intel_dp)
1022379bc100SJani Nikula {
1023379bc100SJani Nikula 	return DP_TRAIN_PRE_EMPH_LEVEL_3;
1024379bc100SJani Nikula }
1025379bc100SJani Nikula 
1026a8143150SJosé Roberto de Souza static void icl_ddi_combo_vswing_program(struct intel_encoder *encoder,
1027193299adSVille Syrjälä 					 const struct intel_crtc_state *crtc_state)
1028379bc100SJani Nikula {
1029a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1030d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
1031e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
1032f0e86e05SJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1033a621860aSVille Syrjälä 	int n_entries, ln;
1034a621860aSVille Syrjälä 	u32 val;
1035379bc100SJani Nikula 
1036e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1037e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
103885da0292SVille Syrjälä 		return;
1039379bc100SJani Nikula 
1040a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP)) {
104181619f4aSJosé Roberto de Souza 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
104281619f4aSJosé Roberto de Souza 
104381619f4aSJosé Roberto de Souza 		val = EDP4K2K_MODE_OVRD_EN | EDP4K2K_MODE_OVRD_OPTIMIZED;
1044e505d764SVille Syrjälä 		intel_dp->hobl_active = is_hobl_buf_trans(trans);
104581619f4aSJosé Roberto de Souza 		intel_de_rmw(dev_priv, ICL_PORT_CL_DW10(phy), val,
104681619f4aSJosé Roberto de Souza 			     intel_dp->hobl_active ? val : 0);
104781619f4aSJosé Roberto de Souza 	}
104881619f4aSJosé Roberto de Souza 
1049379bc100SJani Nikula 	/* Set PORT_TX_DW5 */
1050f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
1051379bc100SJani Nikula 	val &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK |
1052379bc100SJani Nikula 		  TAP2_DISABLE | TAP3_DISABLE);
1053379bc100SJani Nikula 	val |= SCALING_MODE_SEL(0x2);
1054379bc100SJani Nikula 	val |= RTERM_SELECT(0x6);
1055379bc100SJani Nikula 	val |= TAP3_DISABLE;
1056f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1057379bc100SJani Nikula 
1058379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
1059f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW2_LN0(phy));
1060379bc100SJani Nikula 	val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
1061379bc100SJani Nikula 		 RCOMP_SCALAR_MASK);
1062e505d764SVille Syrjälä 	val |= SWING_SEL_UPPER(trans->entries[level].icl.dw2_swing_sel);
1063e505d764SVille Syrjälä 	val |= SWING_SEL_LOWER(trans->entries[level].icl.dw2_swing_sel);
1064379bc100SJani Nikula 	/* Program Rcomp scalar for every table entry */
1065379bc100SJani Nikula 	val |= RCOMP_SCALAR(0x98);
1066f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW2_GRP(phy), val);
1067379bc100SJani Nikula 
1068379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
1069379bc100SJani Nikula 	/* We cannot write to GRP. It would overwrite individual loadgen. */
1070379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
1071f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
1072379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
1073379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
1074e505d764SVille Syrjälä 		val |= POST_CURSOR_1(trans->entries[level].icl.dw4_post_cursor_1);
1075e505d764SVille Syrjälä 		val |= POST_CURSOR_2(trans->entries[level].icl.dw4_post_cursor_2);
1076e505d764SVille Syrjälä 		val |= CURSOR_COEFF(trans->entries[level].icl.dw4_cursor_coeff);
1077f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
1078379bc100SJani Nikula 	}
1079379bc100SJani Nikula 
1080379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
1081f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW7_LN0(phy));
1082379bc100SJani Nikula 	val &= ~N_SCALAR_MASK;
1083e505d764SVille Syrjälä 	val |= N_SCALAR(trans->entries[level].icl.dw7_n_scalar);
1084f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW7_GRP(phy), val);
1085379bc100SJani Nikula }
1086379bc100SJani Nikula 
1087193299adSVille Syrjälä static void icl_combo_phy_set_signal_levels(struct intel_encoder *encoder,
1088193299adSVille Syrjälä 					    const struct intel_crtc_state *crtc_state)
1089379bc100SJani Nikula {
1090379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1091dc867bc7SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1092a621860aSVille Syrjälä 	int width, rate, ln;
1093379bc100SJani Nikula 	u32 val;
1094379bc100SJani Nikula 
1095a621860aSVille Syrjälä 	width = crtc_state->lane_count;
1096a621860aSVille Syrjälä 	rate = crtc_state->port_clock;
1097379bc100SJani Nikula 
1098379bc100SJani Nikula 	/*
1099379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
1100379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
1101379bc100SJani Nikula 	 * else clear to 0b.
1102379bc100SJani Nikula 	 */
1103f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_LN0(phy));
1104a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1105379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
1106379bc100SJani Nikula 	else
1107379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
1108f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy), val);
1109379bc100SJani Nikula 
1110379bc100SJani Nikula 	/* 2. Program loadgen select */
1111379bc100SJani Nikula 	/*
1112379bc100SJani Nikula 	 * Program PORT_TX_DW4_LN depending on Bit rate and used lanes
1113379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
1114379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
1115379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
1116379bc100SJani Nikula 	 */
1117379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
1118f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
1119379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
1120379bc100SJani Nikula 
1121379bc100SJani Nikula 		if ((rate <= 600000 && width == 4 && ln >= 1) ||
1122379bc100SJani Nikula 		    (rate <= 600000 && width < 4 && (ln == 1 || ln == 2))) {
1123379bc100SJani Nikula 			val |= LOADGEN_SELECT;
1124379bc100SJani Nikula 		}
1125f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
1126379bc100SJani Nikula 	}
1127379bc100SJani Nikula 
1128379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
1129f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_CL_DW5(phy));
1130379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
1131f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_CL_DW5(phy), val);
1132379bc100SJani Nikula 
1133379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
1134f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
1135379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
1136f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1137379bc100SJani Nikula 
1138379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
1139193299adSVille Syrjälä 	icl_ddi_combo_vswing_program(encoder, crtc_state);
1140379bc100SJani Nikula 
1141379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
1142f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
1143379bc100SJani Nikula 	val |= TX_TRAINING_EN;
1144f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1145379bc100SJani Nikula }
1146379bc100SJani Nikula 
1147193299adSVille Syrjälä static void icl_mg_phy_set_signal_levels(struct intel_encoder *encoder,
1148193299adSVille Syrjälä 					 const struct intel_crtc_state *crtc_state)
1149379bc100SJani Nikula {
1150379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1151f21e8b80SJosé Roberto de Souza 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
1152d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
1153e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
1154a621860aSVille Syrjälä 	int n_entries, ln;
1155a621860aSVille Syrjälä 	u32 val;
1156379bc100SJani Nikula 
115711a89708SImre Deak 	if (intel_tc_port_in_tbt_alt_mode(enc_to_dig_port(encoder)))
1158f8c6b615SVille Syrjälä 		return;
1159f8c6b615SVille Syrjälä 
1160e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1161e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
116285da0292SVille Syrjälä 		return;
1163379bc100SJani Nikula 
1164379bc100SJani Nikula 	/* Set MG_TX_LINK_PARAMS cri_use_fs32 to 0. */
1165379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1166f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port));
1167379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
1168f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port), val);
1169379bc100SJani Nikula 
1170f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port));
1171379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
1172f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port), val);
1173379bc100SJani Nikula 	}
1174379bc100SJani Nikula 
1175379bc100SJani Nikula 	/* Program MG_TX_SWINGCTRL with values from vswing table */
1176379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1177f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port));
1178379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
1179379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
1180e505d764SVille Syrjälä 			trans->entries[level].mg.cri_txdeemph_override_17_12);
1181f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port), val);
1182379bc100SJani Nikula 
1183f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_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_TX2_SWINGCTRL(ln, tc_port), val);
1188379bc100SJani Nikula 	}
1189379bc100SJani Nikula 
1190379bc100SJani Nikula 	/* Program MG_TX_DRVCTRL with values from vswing table */
1191379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1192f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DRVCTRL(ln, tc_port));
1193379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1194379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
1195379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
1196e505d764SVille Syrjälä 			trans->entries[level].mg.cri_txdeemph_override_5_0) |
1197379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
1198e505d764SVille Syrjälä 				trans->entries[level].mg.cri_txdeemph_override_11_6) |
1199379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
1200f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DRVCTRL(ln, tc_port), val);
1201379bc100SJani Nikula 
1202f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DRVCTRL(ln, tc_port));
1203379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1204379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
1205379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
1206e505d764SVille Syrjälä 			trans->entries[level].mg.cri_txdeemph_override_5_0) |
1207379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
1208e505d764SVille Syrjälä 				trans->entries[level].mg.cri_txdeemph_override_11_6) |
1209379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
1210f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DRVCTRL(ln, tc_port), val);
1211379bc100SJani Nikula 
1212379bc100SJani Nikula 		/* FIXME: Program CRI_LOADGEN_SEL after the spec is updated */
1213379bc100SJani Nikula 	}
1214379bc100SJani Nikula 
1215379bc100SJani Nikula 	/*
1216379bc100SJani Nikula 	 * Program MG_CLKHUB<LN, port being used> with value from frequency table
1217379bc100SJani Nikula 	 * In case of Legacy mode on MG PHY, both TX1 and TX2 enabled so use the
1218379bc100SJani Nikula 	 * values from table for which TX1 and TX2 enabled.
1219379bc100SJani Nikula 	 */
1220379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1221f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_CLKHUB(ln, tc_port));
1222a621860aSVille Syrjälä 		if (crtc_state->port_clock < 300000)
1223379bc100SJani Nikula 			val |= CFG_LOW_RATE_LKREN_EN;
1224379bc100SJani Nikula 		else
1225379bc100SJani Nikula 			val &= ~CFG_LOW_RATE_LKREN_EN;
1226f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_CLKHUB(ln, tc_port), val);
1227379bc100SJani Nikula 	}
1228379bc100SJani Nikula 
1229379bc100SJani Nikula 	/* Program the MG_TX_DCC<LN, port being used> based on the link frequency */
1230379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1231f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DCC(ln, tc_port));
1232379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
1233a621860aSVille Syrjälä 		if (crtc_state->port_clock <= 500000) {
1234379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
1235379bc100SJani Nikula 		} else {
1236379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
1237379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
1238379bc100SJani Nikula 		}
1239f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DCC(ln, tc_port), val);
1240379bc100SJani Nikula 
1241f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DCC(ln, tc_port));
1242379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
1243a621860aSVille Syrjälä 		if (crtc_state->port_clock <= 500000) {
1244379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
1245379bc100SJani Nikula 		} else {
1246379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
1247379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
1248379bc100SJani Nikula 		}
1249f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DCC(ln, tc_port), val);
1250379bc100SJani Nikula 	}
1251379bc100SJani Nikula 
1252379bc100SJani Nikula 	/* Program MG_TX_PISO_READLOAD with values from vswing table */
1253379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1254f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
1255f7960e7fSJani Nikula 				    MG_TX1_PISO_READLOAD(ln, tc_port));
1256379bc100SJani Nikula 		val |= CRI_CALCINIT;
1257f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_PISO_READLOAD(ln, tc_port),
1258f7960e7fSJani Nikula 			       val);
1259379bc100SJani Nikula 
1260f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
1261f7960e7fSJani Nikula 				    MG_TX2_PISO_READLOAD(ln, tc_port));
1262379bc100SJani Nikula 		val |= CRI_CALCINIT;
1263f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_PISO_READLOAD(ln, tc_port),
1264f7960e7fSJani Nikula 			       val);
1265379bc100SJani Nikula 	}
1266379bc100SJani Nikula }
1267379bc100SJani Nikula 
1268193299adSVille Syrjälä static void tgl_dkl_phy_set_signal_levels(struct intel_encoder *encoder,
1269193299adSVille Syrjälä 					  const struct intel_crtc_state *crtc_state)
1270978c3e53SClinton A Taylor {
1271978c3e53SClinton A Taylor 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1272978c3e53SClinton A Taylor 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
1273d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
1274e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
1275a621860aSVille Syrjälä 	u32 val, dpcnt_mask, dpcnt_val;
1276a621860aSVille Syrjälä 	int n_entries, ln;
1277978c3e53SClinton A Taylor 
127811a89708SImre Deak 	if (intel_tc_port_in_tbt_alt_mode(enc_to_dig_port(encoder)))
1279f8c6b615SVille Syrjälä 		return;
1280f8c6b615SVille Syrjälä 
1281e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1282e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
128385da0292SVille Syrjälä 		return;
1284978c3e53SClinton A Taylor 
1285978c3e53SClinton A Taylor 	dpcnt_mask = (DKL_TX_PRESHOOT_COEFF_MASK |
1286978c3e53SClinton A Taylor 		      DKL_TX_DE_EMPAHSIS_COEFF_MASK |
1287978c3e53SClinton A Taylor 		      DKL_TX_VSWING_CONTROL_MASK);
1288e505d764SVille Syrjälä 	dpcnt_val = DKL_TX_VSWING_CONTROL(trans->entries[level].dkl.dkl_vswing_control);
1289e505d764SVille Syrjälä 	dpcnt_val |= DKL_TX_DE_EMPHASIS_COEFF(trans->entries[level].dkl.dkl_de_emphasis_control);
1290e505d764SVille Syrjälä 	dpcnt_val |= DKL_TX_PRESHOOT_COEFF(trans->entries[level].dkl.dkl_preshoot_control);
1291978c3e53SClinton A Taylor 
1292978c3e53SClinton A Taylor 	for (ln = 0; ln < 2; ln++) {
1293f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
1294f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, ln));
1295978c3e53SClinton A Taylor 
1296f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_PMD_LANE_SUS(tc_port), 0);
12972d69c42eSJosé Roberto de Souza 
1298978c3e53SClinton A Taylor 		/* All the registers are RMW */
1299f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL0(tc_port));
1300978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
1301978c3e53SClinton A Taylor 		val |= dpcnt_val;
1302f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL0(tc_port), val);
1303978c3e53SClinton A Taylor 
1304f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL1(tc_port));
1305978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
1306978c3e53SClinton A Taylor 		val |= dpcnt_val;
1307f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL1(tc_port), val);
1308978c3e53SClinton A Taylor 
1309f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL2(tc_port));
1310978c3e53SClinton A Taylor 		val &= ~DKL_TX_DP20BITMODE;
1311f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL2(tc_port), val);
131203bca4a8SMika Kahola 
131303bca4a8SMika Kahola 		if ((intel_crtc_has_dp_encoder(crtc_state) &&
131403bca4a8SMika Kahola 		     crtc_state->port_clock == 162000) ||
131503bca4a8SMika Kahola 		    (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) &&
131603bca4a8SMika Kahola 		     crtc_state->port_clock == 594000))
131703bca4a8SMika Kahola 			val |= DKL_TX_LOADGEN_SHARING_PMD_DISABLE;
131803bca4a8SMika Kahola 		else
131903bca4a8SMika Kahola 			val &= ~DKL_TX_LOADGEN_SHARING_PMD_DISABLE;
1320978c3e53SClinton A Taylor 	}
1321978c3e53SClinton A Taylor }
1322978c3e53SClinton A Taylor 
1323a621860aSVille Syrjälä static int translate_signal_level(struct intel_dp *intel_dp,
1324a621860aSVille Syrjälä 				  u8 signal_levels)
1325379bc100SJani Nikula {
13268b4f2137SPankaj Bharadiya 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1327379bc100SJani Nikula 	int i;
1328379bc100SJani Nikula 
1329379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
1330379bc100SJani Nikula 		if (index_to_dp_signal_levels[i] == signal_levels)
1331379bc100SJani Nikula 			return i;
1332379bc100SJani Nikula 	}
1333379bc100SJani Nikula 
13348b4f2137SPankaj Bharadiya 	drm_WARN(&i915->drm, 1,
13358b4f2137SPankaj Bharadiya 		 "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
1336379bc100SJani Nikula 		 signal_levels);
1337379bc100SJani Nikula 
1338379bc100SJani Nikula 	return 0;
1339379bc100SJani Nikula }
1340379bc100SJani Nikula 
1341*5c31e9d0SJani Nikula static int intel_ddi_dp_level(struct intel_dp *intel_dp,
1342*5c31e9d0SJani Nikula 			      const struct intel_crtc_state *crtc_state,
1343*5c31e9d0SJani Nikula 			      int lane)
1344379bc100SJani Nikula {
1345d0920a45SVille Syrjälä 	u8 train_set = intel_dp->train_set[lane];
1346*5c31e9d0SJani Nikula 
1347*5c31e9d0SJani Nikula 	if (intel_dp_is_uhbr(crtc_state)) {
1348*5c31e9d0SJani Nikula 		return train_set & DP_TX_FFE_PRESET_VALUE_MASK;
1349*5c31e9d0SJani Nikula 	} else {
1350a621860aSVille Syrjälä 		u8 signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1351379bc100SJani Nikula 						DP_TRAIN_PRE_EMPHASIS_MASK);
1352379bc100SJani Nikula 
13538b4f2137SPankaj Bharadiya 		return translate_signal_level(intel_dp, signal_levels);
1354379bc100SJani Nikula 	}
1355*5c31e9d0SJani Nikula }
1356379bc100SJani Nikula 
1357193299adSVille Syrjälä int intel_ddi_level(struct intel_encoder *encoder,
1358d0920a45SVille Syrjälä 		    const struct intel_crtc_state *crtc_state,
1359d0920a45SVille Syrjälä 		    int lane)
1360a046a0daSMatt Roper {
13612c63e0f9SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
13622c63e0f9SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
13632c63e0f9SVille Syrjälä 	int level, n_entries;
13642c63e0f9SVille Syrjälä 
13652c63e0f9SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
13662c63e0f9SVille Syrjälä 	if (drm_WARN_ON_ONCE(&i915->drm, !trans))
13672c63e0f9SVille Syrjälä 		return 0;
13682c63e0f9SVille Syrjälä 
1369e722ab8bSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
13703e022c1fSVille Syrjälä 		level = intel_ddi_hdmi_level(encoder, trans);
1371e722ab8bSVille Syrjälä 	else
1372*5c31e9d0SJani Nikula 		level = intel_ddi_dp_level(enc_to_intel_dp(encoder), crtc_state,
1373*5c31e9d0SJani Nikula 					   lane);
13742c63e0f9SVille Syrjälä 
13752c63e0f9SVille Syrjälä 	if (drm_WARN_ON_ONCE(&i915->drm, level >= n_entries))
13762c63e0f9SVille Syrjälä 		level = n_entries - 1;
13772c63e0f9SVille Syrjälä 
13782c63e0f9SVille Syrjälä 	return level;
1379e722ab8bSVille Syrjälä }
1380e722ab8bSVille Syrjälä 
1381e722ab8bSVille Syrjälä static void
1382e722ab8bSVille Syrjälä hsw_set_signal_levels(struct intel_encoder *encoder,
1383a621860aSVille Syrjälä 		      const struct intel_crtc_state *crtc_state)
1384fb83f72cSVille Syrjälä {
1385fb83f72cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1386e722ab8bSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1387d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
1388fb83f72cSVille Syrjälä 	enum port port = encoder->port;
1389fb83f72cSVille Syrjälä 	u32 signal_levels;
1390fb83f72cSVille Syrjälä 
1391e722ab8bSVille Syrjälä 	if (has_iboost(dev_priv))
1392e722ab8bSVille Syrjälä 		skl_ddi_set_iboost(encoder, crtc_state, level);
1393e722ab8bSVille Syrjälä 
1394e722ab8bSVille Syrjälä 	/* HDMI ignores the rest */
1395e722ab8bSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1396e722ab8bSVille Syrjälä 		return;
1397e722ab8bSVille Syrjälä 
1398fb83f72cSVille Syrjälä 	signal_levels = DDI_BUF_TRANS_SELECT(level);
1399fb83f72cSVille Syrjälä 
1400fb83f72cSVille Syrjälä 	drm_dbg_kms(&dev_priv->drm, "Using signal levels %08x\n",
1401fb83f72cSVille Syrjälä 		    signal_levels);
1402fb83f72cSVille Syrjälä 
1403fb83f72cSVille Syrjälä 	intel_dp->DP &= ~DDI_BUF_EMP_MASK;
1404fb83f72cSVille Syrjälä 	intel_dp->DP |= signal_levels;
1405fb83f72cSVille Syrjälä 
1406fb83f72cSVille Syrjälä 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
1407fb83f72cSVille Syrjälä 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
1408379bc100SJani Nikula }
1409379bc100SJani Nikula 
14104da27d5dSLucas De Marchi static void _icl_ddi_enable_clock(struct drm_i915_private *i915, i915_reg_t reg,
14119c6a5c35SVille Syrjälä 				  u32 clk_sel_mask, u32 clk_sel, u32 clk_off)
14129c6a5c35SVille Syrjälä {
14139c6a5c35SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
14149c6a5c35SVille Syrjälä 
14159c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, clk_sel_mask, clk_sel);
14169c6a5c35SVille Syrjälä 
14179c6a5c35SVille Syrjälä 	/*
14189c6a5c35SVille Syrjälä 	 * "This step and the step before must be
14199c6a5c35SVille Syrjälä 	 *  done with separate register writes."
14209c6a5c35SVille Syrjälä 	 */
14219c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, clk_off, 0);
14229c6a5c35SVille Syrjälä 
14239c6a5c35SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
14249c6a5c35SVille Syrjälä }
14259c6a5c35SVille Syrjälä 
14264da27d5dSLucas De Marchi static void _icl_ddi_disable_clock(struct drm_i915_private *i915, i915_reg_t reg,
14279c6a5c35SVille Syrjälä 				   u32 clk_off)
14289c6a5c35SVille Syrjälä {
14299c6a5c35SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
14309c6a5c35SVille Syrjälä 
14319c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, 0, clk_off);
14329c6a5c35SVille Syrjälä 
14339c6a5c35SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
14349c6a5c35SVille Syrjälä }
14359c6a5c35SVille Syrjälä 
14364da27d5dSLucas De Marchi static bool _icl_ddi_is_clock_enabled(struct drm_i915_private *i915, i915_reg_t reg,
14370fbd8694SVille Syrjälä 				      u32 clk_off)
14380fbd8694SVille Syrjälä {
14390fbd8694SVille Syrjälä 	return !(intel_de_read(i915, reg) & clk_off);
14400fbd8694SVille Syrjälä }
14410fbd8694SVille Syrjälä 
1442351221ffSVille Syrjälä static struct intel_shared_dpll *
14434da27d5dSLucas De Marchi _icl_ddi_get_pll(struct drm_i915_private *i915, i915_reg_t reg,
1444351221ffSVille Syrjälä 		 u32 clk_sel_mask, u32 clk_sel_shift)
1445351221ffSVille Syrjälä {
1446351221ffSVille Syrjälä 	enum intel_dpll_id id;
1447351221ffSVille Syrjälä 
1448351221ffSVille Syrjälä 	id = (intel_de_read(i915, reg) & clk_sel_mask) >> clk_sel_shift;
1449351221ffSVille Syrjälä 
1450351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1451351221ffSVille Syrjälä }
1452351221ffSVille Syrjälä 
145340b316d4SVille Syrjälä static void adls_ddi_enable_clock(struct intel_encoder *encoder,
145440b316d4SVille Syrjälä 				  const struct intel_crtc_state *crtc_state)
145540b316d4SVille Syrjälä {
145640b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
145740b316d4SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
145840b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
145940b316d4SVille Syrjälä 
146040b316d4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
146140b316d4SVille Syrjälä 		return;
146240b316d4SVille Syrjälä 
14634da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
146440b316d4SVille Syrjälä 			      ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
146540b316d4SVille Syrjälä 			      pll->info->id << ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy),
146640b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
146740b316d4SVille Syrjälä }
146840b316d4SVille Syrjälä 
146940b316d4SVille Syrjälä static void adls_ddi_disable_clock(struct intel_encoder *encoder)
147040b316d4SVille Syrjälä {
147140b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
147240b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
147340b316d4SVille Syrjälä 
14744da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
147540b316d4SVille Syrjälä 			       ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
147640b316d4SVille Syrjälä }
147740b316d4SVille Syrjälä 
14780fbd8694SVille Syrjälä static bool adls_ddi_is_clock_enabled(struct intel_encoder *encoder)
14790fbd8694SVille Syrjälä {
14800fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
14810fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
14820fbd8694SVille Syrjälä 
14834da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ADLS_DPCLKA_CFGCR(phy),
14840fbd8694SVille Syrjälä 					 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
14850fbd8694SVille Syrjälä }
14860fbd8694SVille Syrjälä 
1487351221ffSVille Syrjälä static struct intel_shared_dpll *adls_ddi_get_pll(struct intel_encoder *encoder)
1488351221ffSVille Syrjälä {
1489351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1490351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1491351221ffSVille Syrjälä 
14924da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ADLS_DPCLKA_CFGCR(phy),
1493351221ffSVille Syrjälä 				ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
1494351221ffSVille Syrjälä 				ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy));
1495351221ffSVille Syrjälä }
1496351221ffSVille Syrjälä 
149740b316d4SVille Syrjälä static void rkl_ddi_enable_clock(struct intel_encoder *encoder,
149840b316d4SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
149940b316d4SVille Syrjälä {
150040b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
150140b316d4SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
150240b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
150340b316d4SVille Syrjälä 
150440b316d4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
150540b316d4SVille Syrjälä 		return;
150640b316d4SVille Syrjälä 
15074da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
150840b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
150940b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
151040b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
151140b316d4SVille Syrjälä }
151240b316d4SVille Syrjälä 
151340b316d4SVille Syrjälä static void rkl_ddi_disable_clock(struct intel_encoder *encoder)
151440b316d4SVille Syrjälä {
151540b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
151640b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
151740b316d4SVille Syrjälä 
15184da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
151940b316d4SVille Syrjälä 			       RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
152040b316d4SVille Syrjälä }
152140b316d4SVille Syrjälä 
15220fbd8694SVille Syrjälä static bool rkl_ddi_is_clock_enabled(struct intel_encoder *encoder)
15230fbd8694SVille Syrjälä {
15240fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15250fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
15260fbd8694SVille Syrjälä 
15274da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
15280fbd8694SVille Syrjälä 					 RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
15290fbd8694SVille Syrjälä }
15300fbd8694SVille Syrjälä 
1531351221ffSVille Syrjälä static struct intel_shared_dpll *rkl_ddi_get_pll(struct intel_encoder *encoder)
1532351221ffSVille Syrjälä {
1533351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1534351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1535351221ffSVille Syrjälä 
15364da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1537351221ffSVille Syrjälä 				RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1538351221ffSVille Syrjälä 				RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1539351221ffSVille Syrjälä }
1540351221ffSVille Syrjälä 
154135bb6b1aSVille Syrjälä static void dg1_ddi_enable_clock(struct intel_encoder *encoder,
154211ffe972SLucas De Marchi 				 const struct intel_crtc_state *crtc_state)
154311ffe972SLucas De Marchi {
154497a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15459c6a5c35SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
154697a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
154711ffe972SLucas De Marchi 
154897a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1549f67a008eSVille Syrjälä 		return;
1550f67a008eSVille Syrjälä 
155111ffe972SLucas De Marchi 	/*
155211ffe972SLucas De Marchi 	 * If we fail this, something went very wrong: first 2 PLLs should be
155311ffe972SLucas De Marchi 	 * used by first 2 phys and last 2 PLLs by last phys
155411ffe972SLucas De Marchi 	 */
155597a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm,
155611ffe972SLucas De Marchi 			(pll->info->id < DPLL_ID_DG1_DPLL2 && phy >= PHY_C) ||
155711ffe972SLucas De Marchi 			(pll->info->id >= DPLL_ID_DG1_DPLL2 && phy < PHY_C)))
155811ffe972SLucas De Marchi 		return;
155911ffe972SLucas De Marchi 
15604da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
15617815ed88SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
15629c6a5c35SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
15639c6a5c35SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
156411ffe972SLucas De Marchi }
156511ffe972SLucas De Marchi 
156635bb6b1aSVille Syrjälä static void dg1_ddi_disable_clock(struct intel_encoder *encoder)
156735bb6b1aSVille Syrjälä {
156897a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
156997a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
157035bb6b1aSVille Syrjälä 
15714da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
15729c6a5c35SVille Syrjälä 			       DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
157335bb6b1aSVille Syrjälä }
157435bb6b1aSVille Syrjälä 
15750fbd8694SVille Syrjälä static bool dg1_ddi_is_clock_enabled(struct intel_encoder *encoder)
15760fbd8694SVille Syrjälä {
15770fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15780fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
15790fbd8694SVille Syrjälä 
15804da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, DG1_DPCLKA_CFGCR0(phy),
15810fbd8694SVille Syrjälä 					 DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
15820fbd8694SVille Syrjälä }
15830fbd8694SVille Syrjälä 
1584351221ffSVille Syrjälä static struct intel_shared_dpll *dg1_ddi_get_pll(struct intel_encoder *encoder)
1585351221ffSVille Syrjälä {
1586351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1587351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
15883352d86dSJosé Roberto de Souza 	enum intel_dpll_id id;
15893352d86dSJosé Roberto de Souza 	u32 val;
1590351221ffSVille Syrjälä 
15913352d86dSJosé Roberto de Souza 	val = intel_de_read(i915, DG1_DPCLKA_CFGCR0(phy));
15923352d86dSJosé Roberto de Souza 	val &= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
15933352d86dSJosé Roberto de Souza 	val >>= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
15943352d86dSJosé Roberto de Souza 	id = val;
15953352d86dSJosé Roberto de Souza 
15963352d86dSJosé Roberto de Souza 	/*
15973352d86dSJosé Roberto de Souza 	 * _DG1_DPCLKA0_CFGCR0 maps between DPLL 0 and 1 with one bit for phy A
15983352d86dSJosé Roberto de Souza 	 * and B while _DG1_DPCLKA1_CFGCR0 maps between DPLL 2 and 3 with one
15993352d86dSJosé Roberto de Souza 	 * bit for phy C and D.
16003352d86dSJosé Roberto de Souza 	 */
16013352d86dSJosé Roberto de Souza 	if (phy >= PHY_C)
16023352d86dSJosé Roberto de Souza 		id += DPLL_ID_DG1_DPLL2;
16033352d86dSJosé Roberto de Souza 
16043352d86dSJosé Roberto de Souza 	return intel_get_shared_dpll_by_id(i915, id);
1605351221ffSVille Syrjälä }
1606351221ffSVille Syrjälä 
160736ecb0ecSVille Syrjälä static void icl_ddi_combo_enable_clock(struct intel_encoder *encoder,
1608379bc100SJani Nikula 				       const struct intel_crtc_state *crtc_state)
1609379bc100SJani Nikula {
161097a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
16119c6a5c35SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
161297a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1613cd803bb4SMatt Roper 
161497a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1615f67a008eSVille Syrjälä 		return;
1616f67a008eSVille Syrjälä 
16174da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
161840b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
161940b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
162040b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1621379bc100SJani Nikula }
1622379bc100SJani Nikula 
162336ecb0ecSVille Syrjälä static void icl_ddi_combo_disable_clock(struct intel_encoder *encoder)
1624379bc100SJani Nikula {
162597a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
162697a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1627379bc100SJani Nikula 
16284da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
162940b316d4SVille Syrjälä 			       ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1630379bc100SJani Nikula }
1631379bc100SJani Nikula 
16320fbd8694SVille Syrjälä static bool icl_ddi_combo_is_clock_enabled(struct intel_encoder *encoder)
16330fbd8694SVille Syrjälä {
16340fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
16350fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
16360fbd8694SVille Syrjälä 
16374da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
16380fbd8694SVille Syrjälä 					 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
16390fbd8694SVille Syrjälä }
16400fbd8694SVille Syrjälä 
1641351221ffSVille Syrjälä struct intel_shared_dpll *icl_ddi_combo_get_pll(struct intel_encoder *encoder)
1642351221ffSVille Syrjälä {
1643351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1644351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1645351221ffSVille Syrjälä 
16464da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1647351221ffSVille Syrjälä 				ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1648351221ffSVille Syrjälä 				ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1649351221ffSVille Syrjälä }
1650351221ffSVille Syrjälä 
165136ecb0ecSVille Syrjälä static void jsl_ddi_tc_enable_clock(struct intel_encoder *encoder,
1652379bc100SJani Nikula 				    const struct intel_crtc_state *crtc_state)
1653379bc100SJani Nikula {
165436ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1655379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
165636ecb0ecSVille Syrjälä 	enum port port = encoder->port;
1657379bc100SJani Nikula 
165836ecb0ecSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1659379bc100SJani Nikula 		return;
1660379bc100SJani Nikula 
1661c2052d6eSJosé Roberto de Souza 	/*
166236ecb0ecSVille Syrjälä 	 * "For DDIC and DDID, program DDI_CLK_SEL to map the MG clock to the port.
166336ecb0ecSVille Syrjälä 	 *  MG does not exist, but the programming is required to ungate DDIC and DDID."
1664c2052d6eSJosé Roberto de Souza 	 */
166536ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_MG);
166636ecb0ecSVille Syrjälä 
166736ecb0ecSVille Syrjälä 	icl_ddi_combo_enable_clock(encoder, crtc_state);
1668379bc100SJani Nikula }
1669379bc100SJani Nikula 
167036ecb0ecSVille Syrjälä static void jsl_ddi_tc_disable_clock(struct intel_encoder *encoder)
1671379bc100SJani Nikula {
167236ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1673379bc100SJani Nikula 	enum port port = encoder->port;
1674379bc100SJani Nikula 
167536ecb0ecSVille Syrjälä 	icl_ddi_combo_disable_clock(encoder);
167636ecb0ecSVille Syrjälä 
167736ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1678379bc100SJani Nikula }
167936ecb0ecSVille Syrjälä 
16800fbd8694SVille Syrjälä static bool jsl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
16810fbd8694SVille Syrjälä {
16820fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
16830fbd8694SVille Syrjälä 	enum port port = encoder->port;
16840fbd8694SVille Syrjälä 	u32 tmp;
16850fbd8694SVille Syrjälä 
16860fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
16870fbd8694SVille Syrjälä 
16880fbd8694SVille Syrjälä 	if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
16890fbd8694SVille Syrjälä 		return false;
16900fbd8694SVille Syrjälä 
16910fbd8694SVille Syrjälä 	return icl_ddi_combo_is_clock_enabled(encoder);
16920fbd8694SVille Syrjälä }
16930fbd8694SVille Syrjälä 
169436ecb0ecSVille Syrjälä static void icl_ddi_tc_enable_clock(struct intel_encoder *encoder,
169536ecb0ecSVille Syrjälä 				    const struct intel_crtc_state *crtc_state)
169636ecb0ecSVille Syrjälä {
169736ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
169836ecb0ecSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
169936ecb0ecSVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
170036ecb0ecSVille Syrjälä 	enum port port = encoder->port;
170136ecb0ecSVille Syrjälä 
170236ecb0ecSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
170336ecb0ecSVille Syrjälä 		return;
170436ecb0ecSVille Syrjälä 
170536ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port),
170636ecb0ecSVille Syrjälä 		       icl_pll_to_ddi_clk_sel(encoder, crtc_state));
170736ecb0ecSVille Syrjälä 
170836ecb0ecSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
170936ecb0ecSVille Syrjälä 
171036ecb0ecSVille Syrjälä 	intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
171136ecb0ecSVille Syrjälä 		     ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port), 0);
171236ecb0ecSVille Syrjälä 
171336ecb0ecSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
171436ecb0ecSVille Syrjälä }
171536ecb0ecSVille Syrjälä 
171636ecb0ecSVille Syrjälä static void icl_ddi_tc_disable_clock(struct intel_encoder *encoder)
171736ecb0ecSVille Syrjälä {
171836ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
171936ecb0ecSVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
172036ecb0ecSVille Syrjälä 	enum port port = encoder->port;
172136ecb0ecSVille Syrjälä 
172236ecb0ecSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
172336ecb0ecSVille Syrjälä 
172436ecb0ecSVille Syrjälä 	intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
172536ecb0ecSVille Syrjälä 		     0, ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
172636ecb0ecSVille Syrjälä 
172736ecb0ecSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
172836ecb0ecSVille Syrjälä 
172936ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1730379bc100SJani Nikula }
1731379bc100SJani Nikula 
17320fbd8694SVille Syrjälä static bool icl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
17330fbd8694SVille Syrjälä {
17340fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
17350fbd8694SVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
17360fbd8694SVille Syrjälä 	enum port port = encoder->port;
17370fbd8694SVille Syrjälä 	u32 tmp;
17380fbd8694SVille Syrjälä 
17390fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
17400fbd8694SVille Syrjälä 
17410fbd8694SVille Syrjälä 	if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
17420fbd8694SVille Syrjälä 		return false;
17430fbd8694SVille Syrjälä 
17440fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, ICL_DPCLKA_CFGCR0);
17450fbd8694SVille Syrjälä 
17460fbd8694SVille Syrjälä 	return !(tmp & ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
17470fbd8694SVille Syrjälä }
17480fbd8694SVille Syrjälä 
1749351221ffSVille Syrjälä static struct intel_shared_dpll *icl_ddi_tc_get_pll(struct intel_encoder *encoder)
1750351221ffSVille Syrjälä {
1751351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1752351221ffSVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
1753351221ffSVille Syrjälä 	enum port port = encoder->port;
1754351221ffSVille Syrjälä 	enum intel_dpll_id id;
1755351221ffSVille Syrjälä 	u32 tmp;
1756351221ffSVille Syrjälä 
1757351221ffSVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
1758351221ffSVille Syrjälä 
1759351221ffSVille Syrjälä 	switch (tmp & DDI_CLK_SEL_MASK) {
1760351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_162:
1761351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_270:
1762351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_540:
1763351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_810:
1764351221ffSVille Syrjälä 		id = DPLL_ID_ICL_TBTPLL;
1765351221ffSVille Syrjälä 		break;
1766351221ffSVille Syrjälä 	case DDI_CLK_SEL_MG:
1767351221ffSVille Syrjälä 		id = icl_tc_port_to_pll_id(tc_port);
1768351221ffSVille Syrjälä 		break;
1769351221ffSVille Syrjälä 	default:
1770351221ffSVille Syrjälä 		MISSING_CASE(tmp);
1771351221ffSVille Syrjälä 		fallthrough;
1772351221ffSVille Syrjälä 	case DDI_CLK_SEL_NONE:
1773351221ffSVille Syrjälä 		return NULL;
1774351221ffSVille Syrjälä 	}
1775351221ffSVille Syrjälä 
1776351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1777351221ffSVille Syrjälä }
1778351221ffSVille Syrjälä 
1779351221ffSVille Syrjälä static struct intel_shared_dpll *bxt_ddi_get_pll(struct intel_encoder *encoder)
1780351221ffSVille Syrjälä {
1781351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1782351221ffSVille Syrjälä 	enum intel_dpll_id id;
1783351221ffSVille Syrjälä 
1784351221ffSVille Syrjälä 	switch (encoder->port) {
1785351221ffSVille Syrjälä 	case PORT_A:
1786351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL0;
1787351221ffSVille Syrjälä 		break;
1788351221ffSVille Syrjälä 	case PORT_B:
1789351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL1;
1790351221ffSVille Syrjälä 		break;
1791351221ffSVille Syrjälä 	case PORT_C:
1792351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL2;
1793351221ffSVille Syrjälä 		break;
1794351221ffSVille Syrjälä 	default:
1795351221ffSVille Syrjälä 		MISSING_CASE(encoder->port);
1796351221ffSVille Syrjälä 		return NULL;
1797351221ffSVille Syrjälä 	}
1798351221ffSVille Syrjälä 
1799351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1800351221ffSVille Syrjälä }
1801351221ffSVille Syrjälä 
180238e31f1aSVille Syrjälä static void skl_ddi_enable_clock(struct intel_encoder *encoder,
180338e31f1aSVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
180438e31f1aSVille Syrjälä {
180538e31f1aSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
180638e31f1aSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
180738e31f1aSVille Syrjälä 	enum port port = encoder->port;
180838e31f1aSVille Syrjälä 
180938e31f1aSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
181038e31f1aSVille Syrjälä 		return;
181138e31f1aSVille Syrjälä 
181238e31f1aSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
181338e31f1aSVille Syrjälä 
18147815ed88SVille Syrjälä 	intel_de_rmw(i915, DPLL_CTRL2,
18157815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_OFF(port) |
18167815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_SEL_MASK(port),
18177815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_SEL(pll->info->id, port) |
181838e31f1aSVille Syrjälä 		     DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
181938e31f1aSVille Syrjälä 
182038e31f1aSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
182138e31f1aSVille Syrjälä }
182238e31f1aSVille Syrjälä 
182338e31f1aSVille Syrjälä static void skl_ddi_disable_clock(struct intel_encoder *encoder)
182438e31f1aSVille Syrjälä {
182538e31f1aSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
182638e31f1aSVille Syrjälä 	enum port port = encoder->port;
182738e31f1aSVille Syrjälä 
1828be317ca0SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
1829be317ca0SVille Syrjälä 
18307815ed88SVille Syrjälä 	intel_de_rmw(i915, DPLL_CTRL2,
18317815ed88SVille Syrjälä 		     0, DPLL_CTRL2_DDI_CLK_OFF(port));
1832be317ca0SVille Syrjälä 
1833be317ca0SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
183438e31f1aSVille Syrjälä }
183538e31f1aSVille Syrjälä 
18360fbd8694SVille Syrjälä static bool skl_ddi_is_clock_enabled(struct intel_encoder *encoder)
18370fbd8694SVille Syrjälä {
18380fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
18390fbd8694SVille Syrjälä 	enum port port = encoder->port;
18400fbd8694SVille Syrjälä 
18410fbd8694SVille Syrjälä 	/*
18420fbd8694SVille Syrjälä 	 * FIXME Not sure if the override affects both
18430fbd8694SVille Syrjälä 	 * the PLL selection and the CLK_OFF bit.
18440fbd8694SVille Syrjälä 	 */
18450fbd8694SVille Syrjälä 	return !(intel_de_read(i915, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_OFF(port));
18460fbd8694SVille Syrjälä }
18470fbd8694SVille Syrjälä 
1848351221ffSVille Syrjälä static struct intel_shared_dpll *skl_ddi_get_pll(struct intel_encoder *encoder)
1849351221ffSVille Syrjälä {
1850351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1851351221ffSVille Syrjälä 	enum port port = encoder->port;
1852351221ffSVille Syrjälä 	enum intel_dpll_id id;
1853351221ffSVille Syrjälä 	u32 tmp;
1854351221ffSVille Syrjälä 
1855351221ffSVille Syrjälä 	tmp = intel_de_read(i915, DPLL_CTRL2);
1856351221ffSVille Syrjälä 
1857351221ffSVille Syrjälä 	/*
1858351221ffSVille Syrjälä 	 * FIXME Not sure if the override affects both
1859351221ffSVille Syrjälä 	 * the PLL selection and the CLK_OFF bit.
1860351221ffSVille Syrjälä 	 */
1861351221ffSVille Syrjälä 	if ((tmp & DPLL_CTRL2_DDI_SEL_OVERRIDE(port)) == 0)
1862351221ffSVille Syrjälä 		return NULL;
1863351221ffSVille Syrjälä 
1864351221ffSVille Syrjälä 	id = (tmp & DPLL_CTRL2_DDI_CLK_SEL_MASK(port)) >>
1865351221ffSVille Syrjälä 		DPLL_CTRL2_DDI_CLK_SEL_SHIFT(port);
1866351221ffSVille Syrjälä 
1867351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1868351221ffSVille Syrjälä }
1869351221ffSVille Syrjälä 
1870d135368dSVille Syrjälä void hsw_ddi_enable_clock(struct intel_encoder *encoder,
1871d135368dSVille Syrjälä 			  const struct intel_crtc_state *crtc_state)
1872d135368dSVille Syrjälä {
1873d135368dSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1874d135368dSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1875d135368dSVille Syrjälä 	enum port port = encoder->port;
1876d135368dSVille Syrjälä 
1877d135368dSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1878d135368dSVille Syrjälä 		return;
1879d135368dSVille Syrjälä 
1880d135368dSVille Syrjälä 	intel_de_write(i915, PORT_CLK_SEL(port), hsw_pll_to_ddi_pll_sel(pll));
1881d135368dSVille Syrjälä }
1882d135368dSVille Syrjälä 
1883d135368dSVille Syrjälä void hsw_ddi_disable_clock(struct intel_encoder *encoder)
1884d135368dSVille Syrjälä {
1885d135368dSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1886d135368dSVille Syrjälä 	enum port port = encoder->port;
1887d135368dSVille Syrjälä 
1888d135368dSVille Syrjälä 	intel_de_write(i915, PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
1889d135368dSVille Syrjälä }
1890d135368dSVille Syrjälä 
18910fbd8694SVille Syrjälä bool hsw_ddi_is_clock_enabled(struct intel_encoder *encoder)
18920fbd8694SVille Syrjälä {
18930fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
18940fbd8694SVille Syrjälä 	enum port port = encoder->port;
18950fbd8694SVille Syrjälä 
18960fbd8694SVille Syrjälä 	return intel_de_read(i915, PORT_CLK_SEL(port)) != PORT_CLK_SEL_NONE;
18970fbd8694SVille Syrjälä }
18980fbd8694SVille Syrjälä 
1899351221ffSVille Syrjälä static struct intel_shared_dpll *hsw_ddi_get_pll(struct intel_encoder *encoder)
1900351221ffSVille Syrjälä {
1901351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1902351221ffSVille Syrjälä 	enum port port = encoder->port;
1903351221ffSVille Syrjälä 	enum intel_dpll_id id;
1904351221ffSVille Syrjälä 	u32 tmp;
1905351221ffSVille Syrjälä 
1906351221ffSVille Syrjälä 	tmp = intel_de_read(i915, PORT_CLK_SEL(port));
1907351221ffSVille Syrjälä 
1908351221ffSVille Syrjälä 	switch (tmp & PORT_CLK_SEL_MASK) {
1909351221ffSVille Syrjälä 	case PORT_CLK_SEL_WRPLL1:
1910351221ffSVille Syrjälä 		id = DPLL_ID_WRPLL1;
1911351221ffSVille Syrjälä 		break;
1912351221ffSVille Syrjälä 	case PORT_CLK_SEL_WRPLL2:
1913351221ffSVille Syrjälä 		id = DPLL_ID_WRPLL2;
1914351221ffSVille Syrjälä 		break;
1915351221ffSVille Syrjälä 	case PORT_CLK_SEL_SPLL:
1916351221ffSVille Syrjälä 		id = DPLL_ID_SPLL;
1917351221ffSVille Syrjälä 		break;
1918351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_810:
1919351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_810;
1920351221ffSVille Syrjälä 		break;
1921351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_1350:
1922351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_1350;
1923351221ffSVille Syrjälä 		break;
1924351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_2700:
1925351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_2700;
1926351221ffSVille Syrjälä 		break;
1927351221ffSVille Syrjälä 	default:
1928351221ffSVille Syrjälä 		MISSING_CASE(tmp);
1929351221ffSVille Syrjälä 		fallthrough;
1930351221ffSVille Syrjälä 	case PORT_CLK_SEL_NONE:
1931351221ffSVille Syrjälä 		return NULL;
1932351221ffSVille Syrjälä 	}
1933351221ffSVille Syrjälä 
1934351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1935351221ffSVille Syrjälä }
1936351221ffSVille Syrjälä 
1937c133df69SVille Syrjälä void intel_ddi_enable_clock(struct intel_encoder *encoder,
1938c133df69SVille Syrjälä 			    const struct intel_crtc_state *crtc_state)
1939c133df69SVille Syrjälä {
1940c133df69SVille Syrjälä 	if (encoder->enable_clock)
1941c133df69SVille Syrjälä 		encoder->enable_clock(encoder, crtc_state);
1942c133df69SVille Syrjälä }
1943c133df69SVille Syrjälä 
1944c133df69SVille Syrjälä static void intel_ddi_disable_clock(struct intel_encoder *encoder)
1945c133df69SVille Syrjälä {
1946c133df69SVille Syrjälä 	if (encoder->disable_clock)
1947c133df69SVille Syrjälä 		encoder->disable_clock(encoder);
1948c133df69SVille Syrjälä }
1949c133df69SVille Syrjälä 
1950aaca50efSVille Syrjälä void intel_ddi_sanitize_encoder_pll_mapping(struct intel_encoder *encoder)
1951dc1ddac6SVille Syrjälä {
195297a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1953dc1ddac6SVille Syrjälä 	u32 port_mask;
1954dc1ddac6SVille Syrjälä 	bool ddi_clk_needed;
1955dc1ddac6SVille Syrjälä 
1956dc1ddac6SVille Syrjälä 	/*
1957dc1ddac6SVille Syrjälä 	 * In case of DP MST, we sanitize the primary encoder only, not the
1958dc1ddac6SVille Syrjälä 	 * virtual ones.
1959dc1ddac6SVille Syrjälä 	 */
1960dc1ddac6SVille Syrjälä 	if (encoder->type == INTEL_OUTPUT_DP_MST)
1961dc1ddac6SVille Syrjälä 		return;
1962dc1ddac6SVille Syrjälä 
1963dc1ddac6SVille Syrjälä 	if (!encoder->base.crtc && intel_encoder_is_dp(encoder)) {
1964dc1ddac6SVille Syrjälä 		u8 pipe_mask;
1965dc1ddac6SVille Syrjälä 		bool is_mst;
1966dc1ddac6SVille Syrjälä 
1967dc1ddac6SVille Syrjälä 		intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
1968dc1ddac6SVille Syrjälä 		/*
1969dc1ddac6SVille Syrjälä 		 * In the unlikely case that BIOS enables DP in MST mode, just
1970dc1ddac6SVille Syrjälä 		 * warn since our MST HW readout is incomplete.
1971dc1ddac6SVille Syrjälä 		 */
197297a24a70SVille Syrjälä 		if (drm_WARN_ON(&i915->drm, is_mst))
1973dc1ddac6SVille Syrjälä 			return;
1974dc1ddac6SVille Syrjälä 	}
1975dc1ddac6SVille Syrjälä 
1976dc1ddac6SVille Syrjälä 	port_mask = BIT(encoder->port);
1977dc1ddac6SVille Syrjälä 	ddi_clk_needed = encoder->base.crtc;
1978dc1ddac6SVille Syrjälä 
1979dc1ddac6SVille Syrjälä 	if (encoder->type == INTEL_OUTPUT_DSI) {
1980dc1ddac6SVille Syrjälä 		struct intel_encoder *other_encoder;
1981dc1ddac6SVille Syrjälä 
1982dc1ddac6SVille Syrjälä 		port_mask = intel_dsi_encoder_ports(encoder);
1983dc1ddac6SVille Syrjälä 		/*
1984dc1ddac6SVille Syrjälä 		 * Sanity check that we haven't incorrectly registered another
1985dc1ddac6SVille Syrjälä 		 * encoder using any of the ports of this DSI encoder.
1986dc1ddac6SVille Syrjälä 		 */
198797a24a70SVille Syrjälä 		for_each_intel_encoder(&i915->drm, other_encoder) {
1988dc1ddac6SVille Syrjälä 			if (other_encoder == encoder)
1989dc1ddac6SVille Syrjälä 				continue;
1990dc1ddac6SVille Syrjälä 
199197a24a70SVille Syrjälä 			if (drm_WARN_ON(&i915->drm,
1992dc1ddac6SVille Syrjälä 					port_mask & BIT(other_encoder->port)))
1993dc1ddac6SVille Syrjälä 				return;
1994dc1ddac6SVille Syrjälä 		}
1995dc1ddac6SVille Syrjälä 		/*
1996dc1ddac6SVille Syrjälä 		 * For DSI we keep the ddi clocks gated
1997dc1ddac6SVille Syrjälä 		 * except during enable/disable sequence.
1998dc1ddac6SVille Syrjälä 		 */
1999dc1ddac6SVille Syrjälä 		ddi_clk_needed = false;
2000dc1ddac6SVille Syrjälä 	}
2001dc1ddac6SVille Syrjälä 
2002f82f2563SMatt Roper 	if (ddi_clk_needed || !encoder->is_clock_enabled ||
20030fbd8694SVille Syrjälä 	    !encoder->is_clock_enabled(encoder))
20040fbd8694SVille Syrjälä 		return;
20050fbd8694SVille Syrjälä 
20060fbd8694SVille Syrjälä 	drm_notice(&i915->drm,
20070fbd8694SVille Syrjälä 		   "[ENCODER:%d:%s] is disabled/in DSI mode with an ungated DDI clock, gate it\n",
20080fbd8694SVille Syrjälä 		   encoder->base.base.id, encoder->base.name);
20090fbd8694SVille Syrjälä 
2010dc1ddac6SVille Syrjälä 	encoder->disable_clock(encoder);
2011dc1ddac6SVille Syrjälä }
2012dc1ddac6SVille Syrjälä 
20138aaf5cbdSJosé Roberto de Souza static void
20147801f3b7SLucas De Marchi icl_program_mg_dp_mode(struct intel_digital_port *dig_port,
20153b51be4eSClinton A Taylor 		       const struct intel_crtc_state *crtc_state)
2016379bc100SJani Nikula {
20177801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
20187801f3b7SLucas De Marchi 	enum tc_port tc_port = intel_port_to_tc(dev_priv, dig_port->base.port);
20195b6a9ba9SVille Syrjälä 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
20203b51be4eSClinton A Taylor 	u32 ln0, ln1, pin_assignment;
20213b51be4eSClinton A Taylor 	u8 width;
2022379bc100SJani Nikula 
20235b6a9ba9SVille Syrjälä 	if (!intel_phy_is_tc(dev_priv, phy) ||
202411a89708SImre Deak 	    intel_tc_port_in_tbt_alt_mode(dig_port))
2025379bc100SJani Nikula 		return;
2026379bc100SJani Nikula 
2027005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2028f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2029f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
2030f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
2031f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2032f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
2033f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
2034978c3e53SClinton A Taylor 	} else {
2035f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, MG_DP_MODE(0, tc_port));
2036f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, MG_DP_MODE(1, tc_port));
2037978c3e53SClinton A Taylor 	}
2038379bc100SJani Nikula 
20394f72a8eeSKhaled Almahallawy 	ln0 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2040379bc100SJani Nikula 	ln1 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2041379bc100SJani Nikula 
20423b51be4eSClinton A Taylor 	/* DPPATC */
20437801f3b7SLucas De Marchi 	pin_assignment = intel_tc_port_get_pin_assignment_mask(dig_port);
20443b51be4eSClinton A Taylor 	width = crtc_state->lane_count;
2045379bc100SJani Nikula 
20463b51be4eSClinton A Taylor 	switch (pin_assignment) {
20473b51be4eSClinton A Taylor 	case 0x0:
20481de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
204911a89708SImre Deak 			    !intel_tc_port_in_legacy_mode(dig_port));
20503b51be4eSClinton A Taylor 		if (width == 1) {
2051379bc100SJani Nikula 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
20523b51be4eSClinton A Taylor 		} else {
20533b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20543b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2055379bc100SJani Nikula 		}
2056379bc100SJani Nikula 		break;
20573b51be4eSClinton A Taylor 	case 0x1:
20583b51be4eSClinton A Taylor 		if (width == 4) {
20593b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20603b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20613b51be4eSClinton A Taylor 		}
2062379bc100SJani Nikula 		break;
20633b51be4eSClinton A Taylor 	case 0x2:
20643b51be4eSClinton A Taylor 		if (width == 2) {
20653b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20663b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20673b51be4eSClinton A Taylor 		}
20683b51be4eSClinton A Taylor 		break;
20693b51be4eSClinton A Taylor 	case 0x3:
20703b51be4eSClinton A Taylor 	case 0x5:
20713b51be4eSClinton A Taylor 		if (width == 1) {
20723b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
20733b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
20743b51be4eSClinton A Taylor 		} else {
20753b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20763b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20773b51be4eSClinton A Taylor 		}
20783b51be4eSClinton A Taylor 		break;
20793b51be4eSClinton A Taylor 	case 0x4:
20803b51be4eSClinton A Taylor 	case 0x6:
20813b51be4eSClinton A Taylor 		if (width == 1) {
20823b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
20833b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
20843b51be4eSClinton A Taylor 		} else {
20853b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20863b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20873b51be4eSClinton A Taylor 		}
20883b51be4eSClinton A Taylor 		break;
2089379bc100SJani Nikula 	default:
20903b51be4eSClinton A Taylor 		MISSING_CASE(pin_assignment);
2091379bc100SJani Nikula 	}
2092379bc100SJani Nikula 
2093005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2094f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2095f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
2096f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln0);
2097f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2098f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
2099f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln1);
2100978c3e53SClinton A Taylor 	} else {
2101f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(0, tc_port), ln0);
2102f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(1, tc_port), ln1);
2103379bc100SJani Nikula 	}
2104978c3e53SClinton A Taylor }
2105379bc100SJani Nikula 
2106ef79fafeSVille Syrjälä static enum transcoder
2107ef79fafeSVille Syrjälä tgl_dp_tp_transcoder(const struct intel_crtc_state *crtc_state)
2108ef79fafeSVille Syrjälä {
2109ef79fafeSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
2110ef79fafeSVille Syrjälä 		return crtc_state->mst_master_transcoder;
2111ef79fafeSVille Syrjälä 	else
2112ef79fafeSVille Syrjälä 		return crtc_state->cpu_transcoder;
2113ef79fafeSVille Syrjälä }
2114ef79fafeSVille Syrjälä 
2115ef79fafeSVille Syrjälä i915_reg_t dp_tp_ctl_reg(struct intel_encoder *encoder,
2116ef79fafeSVille Syrjälä 			 const struct intel_crtc_state *crtc_state)
2117ef79fafeSVille Syrjälä {
2118ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2119ef79fafeSVille Syrjälä 
2120005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
2121ef79fafeSVille Syrjälä 		return TGL_DP_TP_CTL(tgl_dp_tp_transcoder(crtc_state));
2122ef79fafeSVille Syrjälä 	else
2123ef79fafeSVille Syrjälä 		return DP_TP_CTL(encoder->port);
2124ef79fafeSVille Syrjälä }
2125ef79fafeSVille Syrjälä 
2126ef79fafeSVille Syrjälä i915_reg_t dp_tp_status_reg(struct intel_encoder *encoder,
2127ef79fafeSVille Syrjälä 			    const struct intel_crtc_state *crtc_state)
2128ef79fafeSVille Syrjälä {
2129ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2130ef79fafeSVille Syrjälä 
2131005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
2132ef79fafeSVille Syrjälä 		return TGL_DP_TP_STATUS(tgl_dp_tp_transcoder(crtc_state));
2133ef79fafeSVille Syrjälä 	else
2134ef79fafeSVille Syrjälä 		return DP_TP_STATUS(encoder->port);
2135ef79fafeSVille Syrjälä }
2136ef79fafeSVille Syrjälä 
21371639406aSManasi Navare static void intel_dp_sink_set_msa_timing_par_ignore_state(struct intel_dp *intel_dp,
21381639406aSManasi Navare 							  const struct intel_crtc_state *crtc_state,
21391639406aSManasi Navare 							  bool enable)
21401639406aSManasi Navare {
21411639406aSManasi Navare 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
21421639406aSManasi Navare 
21431639406aSManasi Navare 	if (!crtc_state->vrr.enable)
21441639406aSManasi Navare 		return;
21451639406aSManasi Navare 
21461639406aSManasi Navare 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_DOWNSPREAD_CTRL,
21471639406aSManasi Navare 			       enable ? DP_MSA_TIMING_PAR_IGNORE_EN : 0) <= 0)
21481639406aSManasi Navare 		drm_dbg_kms(&i915->drm,
21490868b1ceSVille Syrjälä 			    "Failed to %s MSA_TIMING_PAR_IGNORE in the sink\n",
21500868b1ceSVille Syrjälä 			    enabledisable(enable));
21511639406aSManasi Navare }
21521639406aSManasi Navare 
2153379bc100SJani Nikula static void intel_dp_sink_set_fec_ready(struct intel_dp *intel_dp,
2154379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
2155379bc100SJani Nikula {
215647bdb1caSJani Nikula 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
215747bdb1caSJani Nikula 
2158379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2159379bc100SJani Nikula 		return;
2160379bc100SJani Nikula 
2161379bc100SJani Nikula 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_FEC_CONFIGURATION, DP_FEC_READY) <= 0)
216247bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
216347bdb1caSJani Nikula 			    "Failed to set FEC_READY in the sink\n");
2164379bc100SJani Nikula }
2165379bc100SJani Nikula 
2166379bc100SJani Nikula static void intel_ddi_enable_fec(struct intel_encoder *encoder,
2167379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
2168379bc100SJani Nikula {
2169379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
21704444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
2171379bc100SJani Nikula 	u32 val;
2172379bc100SJani Nikula 
2173379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2174379bc100SJani Nikula 		return;
2175379bc100SJani Nikula 
2176b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
2177ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2178379bc100SJani Nikula 	val |= DP_TP_CTL_FEC_ENABLE;
2179ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2180379bc100SJani Nikula }
2181379bc100SJani Nikula 
2182379bc100SJani Nikula static void intel_ddi_disable_fec_state(struct intel_encoder *encoder,
2183379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
2184379bc100SJani Nikula {
2185379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
21864444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
2187379bc100SJani Nikula 	u32 val;
2188379bc100SJani Nikula 
2189379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2190379bc100SJani Nikula 		return;
2191379bc100SJani Nikula 
2192b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
2193ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2194379bc100SJani Nikula 	val &= ~DP_TP_CTL_FEC_ENABLE;
2195ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2196ef79fafeSVille Syrjälä 	intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2197379bc100SJani Nikula }
2198379bc100SJani Nikula 
21995cdf706fSVille Syrjälä static void intel_ddi_power_up_lanes(struct intel_encoder *encoder,
22005cdf706fSVille Syrjälä 				     const struct intel_crtc_state *crtc_state)
22015cdf706fSVille Syrjälä {
22025cdf706fSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
22035cdf706fSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
22045cdf706fSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
22055cdf706fSVille Syrjälä 
22065cdf706fSVille Syrjälä 	if (intel_phy_is_combo(i915, phy)) {
22075cdf706fSVille Syrjälä 		bool lane_reversal =
22085cdf706fSVille Syrjälä 			dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
22095cdf706fSVille Syrjälä 
22105cdf706fSVille Syrjälä 		intel_combo_phy_power_up_lanes(i915, phy, false,
22115cdf706fSVille Syrjälä 					       crtc_state->lane_count,
22125cdf706fSVille Syrjälä 					       lane_reversal);
22135cdf706fSVille Syrjälä 	}
22145cdf706fSVille Syrjälä }
22155cdf706fSVille Syrjälä 
2216f6864b27SJani Nikula /* Splitter enable for eDP MSO is limited to certain pipes. */
2217f6864b27SJani Nikula static u8 intel_ddi_splitter_pipe_mask(struct drm_i915_private *i915)
2218f6864b27SJani Nikula {
2219f6864b27SJani Nikula 	if (IS_ALDERLAKE_P(i915))
2220f6864b27SJani Nikula 		return BIT(PIPE_A) | BIT(PIPE_B);
2221f6864b27SJani Nikula 	else
2222f6864b27SJani Nikula 		return BIT(PIPE_A);
2223f6864b27SJani Nikula }
2224f6864b27SJani Nikula 
22255b616a29SJani Nikula static void intel_ddi_mso_get_config(struct intel_encoder *encoder,
22265b616a29SJani Nikula 				     struct intel_crtc_state *pipe_config)
22275b616a29SJani Nikula {
22285b616a29SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
22295b616a29SJani Nikula 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
22305b616a29SJani Nikula 	enum pipe pipe = crtc->pipe;
22315b616a29SJani Nikula 	u32 dss1;
22325b616a29SJani Nikula 
22335b616a29SJani Nikula 	if (!HAS_MSO(i915))
22345b616a29SJani Nikula 		return;
22355b616a29SJani Nikula 
22365b616a29SJani Nikula 	dss1 = intel_de_read(i915, ICL_PIPE_DSS_CTL1(pipe));
22375b616a29SJani Nikula 
22385b616a29SJani Nikula 	pipe_config->splitter.enable = dss1 & SPLITTER_ENABLE;
22395b616a29SJani Nikula 	if (!pipe_config->splitter.enable)
22405b616a29SJani Nikula 		return;
22415b616a29SJani Nikula 
2242f6864b27SJani Nikula 	if (drm_WARN_ON(&i915->drm, !(intel_ddi_splitter_pipe_mask(i915) & BIT(pipe)))) {
22435b616a29SJani Nikula 		pipe_config->splitter.enable = false;
22445b616a29SJani Nikula 		return;
22455b616a29SJani Nikula 	}
22465b616a29SJani Nikula 
22475b616a29SJani Nikula 	switch (dss1 & SPLITTER_CONFIGURATION_MASK) {
22485b616a29SJani Nikula 	default:
22495b616a29SJani Nikula 		drm_WARN(&i915->drm, true,
22505b616a29SJani Nikula 			 "Invalid splitter configuration, dss1=0x%08x\n", dss1);
22515b616a29SJani Nikula 		fallthrough;
22525b616a29SJani Nikula 	case SPLITTER_CONFIGURATION_2_SEGMENT:
22535b616a29SJani Nikula 		pipe_config->splitter.link_count = 2;
22545b616a29SJani Nikula 		break;
22555b616a29SJani Nikula 	case SPLITTER_CONFIGURATION_4_SEGMENT:
22565b616a29SJani Nikula 		pipe_config->splitter.link_count = 4;
22575b616a29SJani Nikula 		break;
22585b616a29SJani Nikula 	}
22595b616a29SJani Nikula 
22605b616a29SJani Nikula 	pipe_config->splitter.pixel_overlap = REG_FIELD_GET(OVERLAP_PIXELS_MASK, dss1);
22615b616a29SJani Nikula }
22625b616a29SJani Nikula 
2263bc71194eSJani Nikula static void intel_ddi_mso_configure(const struct intel_crtc_state *crtc_state)
2264bc71194eSJani Nikula {
2265bc71194eSJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2266bc71194eSJani Nikula 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2267bc71194eSJani Nikula 	enum pipe pipe = crtc->pipe;
2268bc71194eSJani Nikula 	u32 dss1 = 0;
2269bc71194eSJani Nikula 
2270bc71194eSJani Nikula 	if (!HAS_MSO(i915))
2271bc71194eSJani Nikula 		return;
2272bc71194eSJani Nikula 
2273bc71194eSJani Nikula 	if (crtc_state->splitter.enable) {
2274bc71194eSJani Nikula 		dss1 |= SPLITTER_ENABLE;
2275bc71194eSJani Nikula 		dss1 |= OVERLAP_PIXELS(crtc_state->splitter.pixel_overlap);
2276bc71194eSJani Nikula 		if (crtc_state->splitter.link_count == 2)
2277bc71194eSJani Nikula 			dss1 |= SPLITTER_CONFIGURATION_2_SEGMENT;
2278bc71194eSJani Nikula 		else
2279bc71194eSJani Nikula 			dss1 |= SPLITTER_CONFIGURATION_4_SEGMENT;
2280bc71194eSJani Nikula 	}
2281bc71194eSJani Nikula 
2282bc71194eSJani Nikula 	intel_de_rmw(i915, ICL_PIPE_DSS_CTL1(pipe),
2283bc71194eSJani Nikula 		     SPLITTER_ENABLE | SPLITTER_CONFIGURATION_MASK |
2284bc71194eSJani Nikula 		     OVERLAP_PIXELS_MASK, dss1);
2285bc71194eSJani Nikula }
2286bc71194eSJani Nikula 
2287f82f2563SMatt Roper static void dg2_ddi_pre_enable_dp(struct intel_atomic_state *state,
2288f82f2563SMatt Roper 				  struct intel_encoder *encoder,
2289f82f2563SMatt Roper 				  const struct intel_crtc_state *crtc_state,
2290f82f2563SMatt Roper 				  const struct drm_connector_state *conn_state)
2291f82f2563SMatt Roper {
2292f82f2563SMatt Roper 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2293f82f2563SMatt Roper 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2294f82f2563SMatt Roper 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2295f82f2563SMatt Roper 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2296f82f2563SMatt Roper 
2297f82f2563SMatt Roper 	intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
2298f82f2563SMatt Roper 				 crtc_state->lane_count);
2299f82f2563SMatt Roper 
2300f82f2563SMatt Roper 	/*
23019f620f1dSVille Syrjälä 	 * We only configure what the register value will be here.  Actual
23029f620f1dSVille Syrjälä 	 * enabling happens during link training farther down.
23039f620f1dSVille Syrjälä 	 */
23049f620f1dSVille Syrjälä 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
23059f620f1dSVille Syrjälä 
23069f620f1dSVille Syrjälä 	/*
2307f82f2563SMatt Roper 	 * 1. Enable Power Wells
2308f82f2563SMatt Roper 	 *
2309f82f2563SMatt Roper 	 * This was handled at the beginning of intel_atomic_commit_tail(),
2310f82f2563SMatt Roper 	 * before we called down into this function.
2311f82f2563SMatt Roper 	 */
2312f82f2563SMatt Roper 
2313f82f2563SMatt Roper 	/* 2. Enable Panel Power if PPS is required */
2314f82f2563SMatt Roper 	intel_pps_on(intel_dp);
2315f82f2563SMatt Roper 
2316f82f2563SMatt Roper 	/*
2317f82f2563SMatt Roper 	 * 3. Enable the port PLL.
2318f82f2563SMatt Roper 	 */
2319f82f2563SMatt Roper 	intel_ddi_enable_clock(encoder, crtc_state);
2320f82f2563SMatt Roper 
2321f82f2563SMatt Roper 	/* 4. Enable IO power */
232211a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port))
2323f82f2563SMatt Roper 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2324f82f2563SMatt Roper 								   dig_port->ddi_io_power_domain);
2325f82f2563SMatt Roper 
2326f82f2563SMatt Roper 	/*
2327f82f2563SMatt Roper 	 * 5. The rest of the below are substeps under the bspec's "Enable and
2328f82f2563SMatt Roper 	 * Train Display Port" step.  Note that steps that are specific to
2329f82f2563SMatt Roper 	 * MST will be handled by intel_mst_pre_enable_dp() before/after it
2330f82f2563SMatt Roper 	 * calls into this function.  Also intel_mst_pre_enable_dp() only calls
2331f82f2563SMatt Roper 	 * us when active_mst_links==0, so any steps designated for "single
2332f82f2563SMatt Roper 	 * stream or multi-stream master transcoder" can just be performed
2333f82f2563SMatt Roper 	 * unconditionally here.
2334f82f2563SMatt Roper 	 */
2335f82f2563SMatt Roper 
2336f82f2563SMatt Roper 	/*
2337f82f2563SMatt Roper 	 * 5.a Configure Transcoder Clock Select to direct the Port clock to the
2338f82f2563SMatt Roper 	 * Transcoder.
2339f82f2563SMatt Roper 	 */
2340f82f2563SMatt Roper 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
2341f82f2563SMatt Roper 
234279ac2b1bSJani Nikula 	/* 5.b Configure transcoder for DP 2.0 128b/132b */
234379ac2b1bSJani Nikula 	intel_ddi_config_transcoder_dp2(encoder, crtc_state);
2344f82f2563SMatt Roper 
2345f82f2563SMatt Roper 	/*
2346f82f2563SMatt Roper 	 * 5.c Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
2347f82f2563SMatt Roper 	 * Transport Select
2348f82f2563SMatt Roper 	 */
2349f82f2563SMatt Roper 	intel_ddi_config_transcoder_func(encoder, crtc_state);
2350f82f2563SMatt Roper 
2351f82f2563SMatt Roper 	/*
2352f82f2563SMatt Roper 	 * 5.d Configure & enable DP_TP_CTL with link training pattern 1
2353f82f2563SMatt Roper 	 * selected
2354f82f2563SMatt Roper 	 *
2355f82f2563SMatt Roper 	 * This will be handled by the intel_dp_start_link_train() farther
2356f82f2563SMatt Roper 	 * down this function.
2357f82f2563SMatt Roper 	 */
2358f82f2563SMatt Roper 
2359f82f2563SMatt Roper 	/* 5.e Configure voltage swing and related IO settings */
2360e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
2361f82f2563SMatt Roper 
2362f82f2563SMatt Roper 	if (!is_mst)
2363f82f2563SMatt Roper 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2364f82f2563SMatt Roper 
236501da701bSAnkit Nautiyal 	intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2366f82f2563SMatt Roper 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
2367f82f2563SMatt Roper 	/*
2368f82f2563SMatt Roper 	 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
2369f82f2563SMatt Roper 	 * in the FEC_CONFIGURATION register to 1 before initiating link
2370f82f2563SMatt Roper 	 * training
2371f82f2563SMatt Roper 	 */
2372f82f2563SMatt Roper 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
237301da701bSAnkit Nautiyal 	intel_dp_check_frl_training(intel_dp);
237401da701bSAnkit Nautiyal 	intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
2375f82f2563SMatt Roper 
2376f82f2563SMatt Roper 	/*
2377f82f2563SMatt Roper 	 * 5.h Follow DisplayPort specification training sequence (see notes for
2378f82f2563SMatt Roper 	 *     failure handling)
2379f82f2563SMatt Roper 	 * 5.i If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
2380f82f2563SMatt Roper 	 *     Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
2381f82f2563SMatt Roper 	 *     (timeout after 800 us)
2382f82f2563SMatt Roper 	 */
2383f82f2563SMatt Roper 	intel_dp_start_link_train(intel_dp, crtc_state);
2384f82f2563SMatt Roper 
2385f82f2563SMatt Roper 	/* 5.j Set DP_TP_CTL link training to Normal */
2386f82f2563SMatt Roper 	if (!is_trans_port_sync_mode(crtc_state))
2387f82f2563SMatt Roper 		intel_dp_stop_link_train(intel_dp, crtc_state);
2388f82f2563SMatt Roper 
2389f82f2563SMatt Roper 	/* 5.k Configure and enable FEC if needed */
2390f82f2563SMatt Roper 	intel_ddi_enable_fec(encoder, crtc_state);
2391f82f2563SMatt Roper 	intel_dsc_enable(encoder, crtc_state);
2392f82f2563SMatt Roper }
2393f82f2563SMatt Roper 
2394ede9771dSVille Syrjälä static void tgl_ddi_pre_enable_dp(struct intel_atomic_state *state,
2395ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
239699389390SJosé Roberto de Souza 				  const struct intel_crtc_state *crtc_state,
239799389390SJosé Roberto de Souza 				  const struct drm_connector_state *conn_state)
239899389390SJosé Roberto de Souza {
2399b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
240099389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2401b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
240299389390SJosé Roberto de Souza 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
240399389390SJosé Roberto de Souza 
2404a621860aSVille Syrjälä 	intel_dp_set_link_params(intel_dp,
2405a621860aSVille Syrjälä 				 crtc_state->port_clock,
2406a621860aSVille Syrjälä 				 crtc_state->lane_count);
240799389390SJosé Roberto de Souza 
24085e19c0b0SMatt Roper 	/*
24099f620f1dSVille Syrjälä 	 * We only configure what the register value will be here.  Actual
24109f620f1dSVille Syrjälä 	 * enabling happens during link training farther down.
24119f620f1dSVille Syrjälä 	 */
24129f620f1dSVille Syrjälä 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
24139f620f1dSVille Syrjälä 
24149f620f1dSVille Syrjälä 	/*
24155e19c0b0SMatt Roper 	 * 1. Enable Power Wells
24165e19c0b0SMatt Roper 	 *
24175e19c0b0SMatt Roper 	 * This was handled at the beginning of intel_atomic_commit_tail(),
24185e19c0b0SMatt Roper 	 * before we called down into this function.
24195e19c0b0SMatt Roper 	 */
242099389390SJosé Roberto de Souza 
24215e19c0b0SMatt Roper 	/* 2. Enable Panel Power if PPS is required */
2422eb46f498SJani Nikula 	intel_pps_on(intel_dp);
242399389390SJosé Roberto de Souza 
242499389390SJosé Roberto de Souza 	/*
24255e19c0b0SMatt Roper 	 * 3. For non-TBT Type-C ports, set FIA lane count
24265e19c0b0SMatt Roper 	 * (DFLEXDPSP.DPX4TXLATC)
24275e19c0b0SMatt Roper 	 *
24285e19c0b0SMatt Roper 	 * This was done before tgl_ddi_pre_enable_dp by
24291e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_encoders_pre_pll_enable().
243099389390SJosé Roberto de Souza 	 */
243199389390SJosé Roberto de Souza 
24325e19c0b0SMatt Roper 	/*
24335e19c0b0SMatt Roper 	 * 4. Enable the port PLL.
24345e19c0b0SMatt Roper 	 *
24355e19c0b0SMatt Roper 	 * The PLL enabling itself was already done before this function by
24361e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_enable_shared_dpll().  We need only
24375e19c0b0SMatt Roper 	 * configure the PLL to port mapping here.
24385e19c0b0SMatt Roper 	 */
2439c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
24406171e58bSClinton A Taylor 
24415e19c0b0SMatt Roper 	/* 5. If IO power is controlled through PWR_WELL_CTL, Enable IO Power */
244211a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
2443a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2444a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
244599389390SJosé Roberto de Souza 								   dig_port->ddi_io_power_domain);
2446a4550977SImre Deak 	}
244799389390SJosé Roberto de Souza 
24485e19c0b0SMatt Roper 	/* 6. Program DP_MODE */
24493b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
245099389390SJosé Roberto de Souza 
245199389390SJosé Roberto de Souza 	/*
24525e19c0b0SMatt Roper 	 * 7. The rest of the below are substeps under the bspec's "Enable and
24535e19c0b0SMatt Roper 	 * Train Display Port" step.  Note that steps that are specific to
24545e19c0b0SMatt Roper 	 * MST will be handled by intel_mst_pre_enable_dp() before/after it
24555e19c0b0SMatt Roper 	 * calls into this function.  Also intel_mst_pre_enable_dp() only calls
24565e19c0b0SMatt Roper 	 * us when active_mst_links==0, so any steps designated for "single
24575e19c0b0SMatt Roper 	 * stream or multi-stream master transcoder" can just be performed
24585e19c0b0SMatt Roper 	 * unconditionally here.
24595e19c0b0SMatt Roper 	 */
24605e19c0b0SMatt Roper 
24615e19c0b0SMatt Roper 	/*
24625e19c0b0SMatt Roper 	 * 7.a Configure Transcoder Clock Select to direct the Port clock to the
24635e19c0b0SMatt Roper 	 * Transcoder.
246499389390SJosé Roberto de Souza 	 */
246502a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
246699389390SJosé Roberto de Souza 
24675e19c0b0SMatt Roper 	/*
24685e19c0b0SMatt Roper 	 * 7.b Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
24695e19c0b0SMatt Roper 	 * Transport Select
24705e19c0b0SMatt Roper 	 */
2471eed22a46SVille Syrjälä 	intel_ddi_config_transcoder_func(encoder, crtc_state);
247299389390SJosé Roberto de Souza 
24735e19c0b0SMatt Roper 	/*
24745e19c0b0SMatt Roper 	 * 7.c Configure & enable DP_TP_CTL with link training pattern 1
24755e19c0b0SMatt Roper 	 * selected
24765e19c0b0SMatt Roper 	 *
24775e19c0b0SMatt Roper 	 * This will be handled by the intel_dp_start_link_train() farther
24785e19c0b0SMatt Roper 	 * down this function.
24795e19c0b0SMatt Roper 	 */
24805e19c0b0SMatt Roper 
24815e19c0b0SMatt Roper 	/* 7.e Configure voltage swing and related IO settings */
2482e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
248399389390SJosé Roberto de Souza 
24845e19c0b0SMatt Roper 	/*
24855e19c0b0SMatt Roper 	 * 7.f Combo PHY: Configure PORT_CL_DW10 Static Power Down to power up
24865e19c0b0SMatt Roper 	 * the used lanes of the DDI.
24875e19c0b0SMatt Roper 	 */
24885cdf706fSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
248999389390SJosé Roberto de Souza 
24905e19c0b0SMatt Roper 	/*
2491bc71194eSJani Nikula 	 * 7.g Program CoG/MSO configuration bits in DSS_CTL1 if selected.
2492bc71194eSJani Nikula 	 */
2493bc71194eSJani Nikula 	intel_ddi_mso_configure(crtc_state);
2494bc71194eSJani Nikula 
249599389390SJosé Roberto de Souza 	if (!is_mst)
24960e634efdSVille Syrjälä 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
249799389390SJosé Roberto de Souza 
2498522508b6SAnkit Nautiyal 	intel_dp_configure_protocol_converter(intel_dp, crtc_state);
249999389390SJosé Roberto de Souza 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
250099389390SJosé Roberto de Souza 	/*
250199389390SJosé Roberto de Souza 	 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
250299389390SJosé Roberto de Souza 	 * in the FEC_CONFIGURATION register to 1 before initiating link
250399389390SJosé Roberto de Souza 	 * training
250499389390SJosé Roberto de Souza 	 */
250599389390SJosé Roberto de Souza 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
25065e19c0b0SMatt Roper 
25074f3dd47aSAnkit Nautiyal 	intel_dp_check_frl_training(intel_dp);
250810fec80bSAnkit Nautiyal 	intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
25094f3dd47aSAnkit Nautiyal 
25105e19c0b0SMatt Roper 	/*
25115e19c0b0SMatt Roper 	 * 7.i Follow DisplayPort specification training sequence (see notes for
25125e19c0b0SMatt Roper 	 *     failure handling)
25135e19c0b0SMatt Roper 	 * 7.j If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
25145e19c0b0SMatt Roper 	 *     Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
25155e19c0b0SMatt Roper 	 *     (timeout after 800 us)
25165e19c0b0SMatt Roper 	 */
2517a621860aSVille Syrjälä 	intel_dp_start_link_train(intel_dp, crtc_state);
251899389390SJosé Roberto de Souza 
25195e19c0b0SMatt Roper 	/* 7.k Set DP_TP_CTL link training to Normal */
2520eadf6f91SManasi Navare 	if (!is_trans_port_sync_mode(crtc_state))
2521a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
252299389390SJosé Roberto de Souza 
25235e19c0b0SMatt Roper 	/* 7.l Configure and enable FEC if needed */
252499389390SJosé Roberto de Souza 	intel_ddi_enable_fec(encoder, crtc_state);
25254e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner)
252699389390SJosé Roberto de Souza 		intel_dsc_enable(encoder, crtc_state);
252799389390SJosé Roberto de Souza }
252899389390SJosé Roberto de Souza 
2529ede9771dSVille Syrjälä static void hsw_ddi_pre_enable_dp(struct intel_atomic_state *state,
2530ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
2531379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
2532379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
2533379bc100SJani Nikula {
2534b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2535379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2536379bc100SJani Nikula 	enum port port = encoder->port;
2537b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2538379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2539379bc100SJani Nikula 
2540005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 11)
25411de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
25421de143ccSPankaj Bharadiya 			    is_mst && (port == PORT_A || port == PORT_E));
2543542dfab5SJosé Roberto de Souza 	else
25441de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm, is_mst && port == PORT_A);
2545379bc100SJani Nikula 
2546a621860aSVille Syrjälä 	intel_dp_set_link_params(intel_dp,
2547a621860aSVille Syrjälä 				 crtc_state->port_clock,
2548a621860aSVille Syrjälä 				 crtc_state->lane_count);
2549379bc100SJani Nikula 
25509f620f1dSVille Syrjälä 	/*
25519f620f1dSVille Syrjälä 	 * We only configure what the register value will be here.  Actual
25529f620f1dSVille Syrjälä 	 * enabling happens during link training farther down.
25539f620f1dSVille Syrjälä 	 */
25549f620f1dSVille Syrjälä 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
25559f620f1dSVille Syrjälä 
2556eb46f498SJani Nikula 	intel_pps_on(intel_dp);
2557379bc100SJani Nikula 
2558c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
2559379bc100SJani Nikula 
256011a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
2561a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2562a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
25633b2ed431SImre Deak 								   dig_port->ddi_io_power_domain);
2564a4550977SImre Deak 	}
2565379bc100SJani Nikula 
25663b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
2567379bc100SJani Nikula 
25685bafd85dSVille Syrjälä 	if (has_buf_trans_select(dev_priv))
2569266152aeSVille Syrjälä 		hsw_prepare_dp_ddi_buffers(encoder, crtc_state);
2570379bc100SJani Nikula 
2571e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
2572e722ab8bSVille Syrjälä 
25735cdf706fSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
2574379bc100SJani Nikula 
2575379bc100SJani Nikula 	if (!is_mst)
25760e634efdSVille Syrjälä 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2577522508b6SAnkit Nautiyal 	intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2578379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state,
2579379bc100SJani Nikula 					      true);
2580379bc100SJani Nikula 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
2581a621860aSVille Syrjälä 	intel_dp_start_link_train(intel_dp, crtc_state);
2582005e9537SMatt Roper 	if ((port != PORT_A || DISPLAY_VER(dev_priv) >= 9) &&
2583eadf6f91SManasi Navare 	    !is_trans_port_sync_mode(crtc_state))
2584a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
2585379bc100SJani Nikula 
2586379bc100SJani Nikula 	intel_ddi_enable_fec(encoder, crtc_state);
2587379bc100SJani Nikula 
2588379bc100SJani Nikula 	if (!is_mst)
258902a715c3SVille Syrjälä 		intel_ddi_enable_pipe_clock(encoder, crtc_state);
2590379bc100SJani Nikula 
25914e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner)
2592379bc100SJani Nikula 		intel_dsc_enable(encoder, crtc_state);
2593379bc100SJani Nikula }
2594379bc100SJani Nikula 
2595ede9771dSVille Syrjälä static void intel_ddi_pre_enable_dp(struct intel_atomic_state *state,
2596ede9771dSVille Syrjälä 				    struct intel_encoder *encoder,
259799389390SJosé Roberto de Souza 				    const struct intel_crtc_state *crtc_state,
259899389390SJosé Roberto de Souza 				    const struct drm_connector_state *conn_state)
259999389390SJosé Roberto de Souza {
260099389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
260199389390SJosé Roberto de Souza 
2602f82f2563SMatt Roper 	if (IS_DG2(dev_priv))
2603f82f2563SMatt Roper 		dg2_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
2604f82f2563SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 12)
2605ede9771dSVille Syrjälä 		tgl_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
260699389390SJosé Roberto de Souza 	else
2607ede9771dSVille Syrjälä 		hsw_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
26080c06fa15SGwan-gyeong Mun 
2609bd8c9ccaSGwan-gyeong Mun 	/* MST will call a setting of MSA after an allocating of Virtual Channel
2610bd8c9ccaSGwan-gyeong Mun 	 * from MST encoder pre_enable callback.
2611bd8c9ccaSGwan-gyeong Mun 	 */
26121fc1e8d4SJosé Roberto de Souza 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
26130c06fa15SGwan-gyeong Mun 		intel_ddi_set_dp_msa(crtc_state, conn_state);
26141c9d2eb2SJani Nikula 
26151c9d2eb2SJani Nikula 		intel_dp_set_m_n(crtc_state, M1_N1);
261699389390SJosé Roberto de Souza 	}
26171fc1e8d4SJosé Roberto de Souza }
261899389390SJosé Roberto de Souza 
2619ede9771dSVille Syrjälä static void intel_ddi_pre_enable_hdmi(struct intel_atomic_state *state,
2620ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
2621379bc100SJani Nikula 				      const struct intel_crtc_state *crtc_state,
2622379bc100SJani Nikula 				      const struct drm_connector_state *conn_state)
2623379bc100SJani Nikula {
26240ba7ffeaSLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
26250ba7ffeaSLucas De Marchi 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2626379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2627379bc100SJani Nikula 
2628379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
2629c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
2630379bc100SJani Nikula 
2631a4550977SImre Deak 	drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2632a4550977SImre Deak 	dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2633a4550977SImre Deak 							   dig_port->ddi_io_power_domain);
2634379bc100SJani Nikula 
26353b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
2636379bc100SJani Nikula 
263702a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
2638379bc100SJani Nikula 
26390ba7ffeaSLucas De Marchi 	dig_port->set_infoframes(encoder,
2640379bc100SJani Nikula 				 crtc_state->has_infoframe,
2641379bc100SJani Nikula 				 crtc_state, conn_state);
2642379bc100SJani Nikula }
2643379bc100SJani Nikula 
2644ede9771dSVille Syrjälä static void intel_ddi_pre_enable(struct intel_atomic_state *state,
2645ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
2646379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state,
2647379bc100SJani Nikula 				 const struct drm_connector_state *conn_state)
2648379bc100SJani Nikula {
26492225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2650379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2651379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
2652379bc100SJani Nikula 
2653379bc100SJani Nikula 	/*
2654379bc100SJani Nikula 	 * When called from DP MST code:
2655379bc100SJani Nikula 	 * - conn_state will be NULL
2656379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
2657379bc100SJani Nikula 	 * - the main connector associated with this port
2658379bc100SJani Nikula 	 *   won't be active or linked to a crtc
2659379bc100SJani Nikula 	 * - crtc_state will be the state of the first stream to
2660379bc100SJani Nikula 	 *   be activated on this port, and it may not be the same
2661379bc100SJani Nikula 	 *   stream that will be deactivated last, but each stream
2662379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
2663379bc100SJani Nikula 	 *   the DP link parameteres
2664379bc100SJani Nikula 	 */
2665379bc100SJani Nikula 
26661de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->has_pch_encoder);
2667379bc100SJani Nikula 
2668379bc100SJani Nikula 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2669379bc100SJani Nikula 
2670379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
2671ede9771dSVille Syrjälä 		intel_ddi_pre_enable_hdmi(state, encoder, crtc_state,
2672ede9771dSVille Syrjälä 					  conn_state);
2673379bc100SJani Nikula 	} else {
2674f7af425dSVille Syrjälä 		struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2675379bc100SJani Nikula 
2676ede9771dSVille Syrjälä 		intel_ddi_pre_enable_dp(state, encoder, crtc_state,
2677ede9771dSVille Syrjälä 					conn_state);
2678379bc100SJani Nikula 
2679f7af425dSVille Syrjälä 		/* FIXME precompute everything properly */
26800ea02bb8SJosé Roberto de Souza 		/* FIXME how do we turn infoframes off again? */
2681f7af425dSVille Syrjälä 		if (dig_port->lspcon.active && dig_port->dp.has_hdmi_sink)
2682379bc100SJani Nikula 			dig_port->set_infoframes(encoder,
2683379bc100SJani Nikula 						 crtc_state->has_infoframe,
2684379bc100SJani Nikula 						 crtc_state, conn_state);
2685379bc100SJani Nikula 	}
2686379bc100SJani Nikula }
2687379bc100SJani Nikula 
2688379bc100SJani Nikula static void intel_disable_ddi_buf(struct intel_encoder *encoder,
2689379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
2690379bc100SJani Nikula {
2691379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2692379bc100SJani Nikula 	enum port port = encoder->port;
2693379bc100SJani Nikula 	bool wait = false;
2694379bc100SJani Nikula 	u32 val;
2695379bc100SJani Nikula 
2696f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, DDI_BUF_CTL(port));
2697379bc100SJani Nikula 	if (val & DDI_BUF_CTL_ENABLE) {
2698379bc100SJani Nikula 		val &= ~DDI_BUF_CTL_ENABLE;
2699f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(port), val);
2700379bc100SJani Nikula 		wait = true;
2701379bc100SJani Nikula 	}
2702379bc100SJani Nikula 
2703e468ff06SLucas De Marchi 	if (intel_crtc_has_dp_encoder(crtc_state)) {
2704ef79fafeSVille Syrjälä 		val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2705379bc100SJani Nikula 		val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
2706379bc100SJani Nikula 		val |= DP_TP_CTL_LINK_TRAIN_PAT1;
2707ef79fafeSVille Syrjälä 		intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2708e468ff06SLucas De Marchi 	}
2709379bc100SJani Nikula 
2710379bc100SJani Nikula 	/* Disable FEC in DP Sink */
2711379bc100SJani Nikula 	intel_ddi_disable_fec_state(encoder, crtc_state);
2712379bc100SJani Nikula 
2713379bc100SJani Nikula 	if (wait)
2714379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, port);
2715379bc100SJani Nikula }
2716379bc100SJani Nikula 
2717ede9771dSVille Syrjälä static void intel_ddi_post_disable_dp(struct intel_atomic_state *state,
2718ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
2719379bc100SJani Nikula 				      const struct intel_crtc_state *old_crtc_state,
2720379bc100SJani Nikula 				      const struct drm_connector_state *old_conn_state)
2721379bc100SJani Nikula {
2722379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2723b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2724379bc100SJani Nikula 	struct intel_dp *intel_dp = &dig_port->dp;
2725379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(old_crtc_state,
2726379bc100SJani Nikula 					  INTEL_OUTPUT_DP_MST);
2727379bc100SJani Nikula 
2728c980216dSImre Deak 	if (!is_mst)
2729c980216dSImre Deak 		intel_dp_set_infoframes(encoder, false,
2730c980216dSImre Deak 					old_crtc_state, old_conn_state);
2731fa37a213SGwan-gyeong Mun 
2732379bc100SJani Nikula 	/*
2733379bc100SJani Nikula 	 * Power down sink before disabling the port, otherwise we end
2734379bc100SJani Nikula 	 * up getting interrupts from the sink on detecting link loss.
2735379bc100SJani Nikula 	 */
27360e634efdSVille Syrjälä 	intel_dp_set_power(intel_dp, DP_SET_POWER_D3);
273778eaaba3SJosé Roberto de Souza 
2738005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2739c59053dcSJosé Roberto de Souza 		if (is_mst) {
2740c59053dcSJosé Roberto de Souza 			enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
2741c59053dcSJosé Roberto de Souza 			u32 val;
2742c59053dcSJosé Roberto de Souza 
2743f7960e7fSJani Nikula 			val = intel_de_read(dev_priv,
2744f7960e7fSJani Nikula 					    TRANS_DDI_FUNC_CTL(cpu_transcoder));
2745919e4f07SJosé Roberto de Souza 			val &= ~(TGL_TRANS_DDI_PORT_MASK |
2746919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
2747f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2748f7960e7fSJani Nikula 				       TRANS_DDI_FUNC_CTL(cpu_transcoder),
2749f7960e7fSJani Nikula 				       val);
2750c59053dcSJosé Roberto de Souza 		}
2751c59053dcSJosé Roberto de Souza 	} else {
2752c59053dcSJosé Roberto de Souza 		if (!is_mst)
275350a7efb2SJosé Roberto de Souza 			intel_ddi_disable_pipe_clock(old_crtc_state);
2754c59053dcSJosé Roberto de Souza 	}
2755379bc100SJani Nikula 
2756379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
2757379bc100SJani Nikula 
27583ca8f191SJosé Roberto de Souza 	/*
27593ca8f191SJosé Roberto de Souza 	 * From TGL spec: "If single stream or multi-stream master transcoder:
27603ca8f191SJosé Roberto de Souza 	 * Configure Transcoder Clock select to direct no clock to the
27613ca8f191SJosé Roberto de Souza 	 * transcoder"
27623ca8f191SJosé Roberto de Souza 	 */
2763005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
27643ca8f191SJosé Roberto de Souza 		intel_ddi_disable_pipe_clock(old_crtc_state);
27653ca8f191SJosé Roberto de Souza 
2766eb46f498SJani Nikula 	intel_pps_vdd_on(intel_dp);
2767eb46f498SJani Nikula 	intel_pps_off(intel_dp);
2768379bc100SJani Nikula 
276911a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port))
2770a4550977SImre Deak 		intel_display_power_put(dev_priv,
2771a4550977SImre Deak 					dig_port->ddi_io_power_domain,
2772a4550977SImre Deak 					fetch_and_zero(&dig_port->ddi_io_wakeref));
2773379bc100SJani Nikula 
2774c133df69SVille Syrjälä 	intel_ddi_disable_clock(encoder);
2775379bc100SJani Nikula }
2776379bc100SJani Nikula 
2777ede9771dSVille Syrjälä static void intel_ddi_post_disable_hdmi(struct intel_atomic_state *state,
2778ede9771dSVille Syrjälä 					struct intel_encoder *encoder,
2779379bc100SJani Nikula 					const struct intel_crtc_state *old_crtc_state,
2780379bc100SJani Nikula 					const struct drm_connector_state *old_conn_state)
2781379bc100SJani Nikula {
2782379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2783b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2784379bc100SJani Nikula 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2785379bc100SJani Nikula 
2786379bc100SJani Nikula 	dig_port->set_infoframes(encoder, false,
2787379bc100SJani Nikula 				 old_crtc_state, old_conn_state);
2788379bc100SJani Nikula 
2789379bc100SJani Nikula 	intel_ddi_disable_pipe_clock(old_crtc_state);
2790379bc100SJani Nikula 
2791379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
2792379bc100SJani Nikula 
2793a4550977SImre Deak 	intel_display_power_put(dev_priv,
2794a4550977SImre Deak 				dig_port->ddi_io_power_domain,
2795a4550977SImre Deak 				fetch_and_zero(&dig_port->ddi_io_wakeref));
2796379bc100SJani Nikula 
2797c133df69SVille Syrjälä 	intel_ddi_disable_clock(encoder);
2798379bc100SJani Nikula 
2799379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
2800379bc100SJani Nikula }
2801379bc100SJani Nikula 
2802ede9771dSVille Syrjälä static void intel_ddi_post_disable(struct intel_atomic_state *state,
2803ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
2804379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
2805379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
2806379bc100SJani Nikula {
2807379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2808b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
280917bef9baSVille Syrjälä 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
281017bef9baSVille Syrjälä 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
2811379bc100SJani Nikula 
28127829c92bSVille Syrjälä 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST)) {
2813773b4b54SVille Syrjälä 		intel_crtc_vblank_off(old_crtc_state);
2814773b4b54SVille Syrjälä 
28158c66081bSVille Syrjälä 		intel_disable_transcoder(old_crtc_state);
2816773b4b54SVille Syrjälä 
2817f0651232SManasi Navare 		intel_vrr_disable(old_crtc_state);
2818f0651232SManasi Navare 
2819773b4b54SVille Syrjälä 		intel_ddi_disable_transcoder_func(old_crtc_state);
2820773b4b54SVille Syrjälä 
2821773b4b54SVille Syrjälä 		intel_dsc_disable(old_crtc_state);
2822773b4b54SVille Syrjälä 
2823005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 9)
2824f6df4d46SLucas De Marchi 			skl_scaler_disable(old_crtc_state);
2825773b4b54SVille Syrjälä 		else
28269eae5e27SLucas De Marchi 			ilk_pfit_disable(old_crtc_state);
28277829c92bSVille Syrjälä 	}
2828773b4b54SVille Syrjälä 
28294e3cdb45SManasi Navare 	if (old_crtc_state->bigjoiner_linked_crtc) {
28304e3cdb45SManasi Navare 		struct intel_atomic_state *state =
28314e3cdb45SManasi Navare 			to_intel_atomic_state(old_crtc_state->uapi.state);
28324e3cdb45SManasi Navare 		struct intel_crtc *slave =
28334e3cdb45SManasi Navare 			old_crtc_state->bigjoiner_linked_crtc;
28344e3cdb45SManasi Navare 		const struct intel_crtc_state *old_slave_crtc_state =
28354e3cdb45SManasi Navare 			intel_atomic_get_old_crtc_state(state, slave);
28364e3cdb45SManasi Navare 
28374e3cdb45SManasi Navare 		intel_crtc_vblank_off(old_slave_crtc_state);
28384e3cdb45SManasi Navare 
28394e3cdb45SManasi Navare 		intel_dsc_disable(old_slave_crtc_state);
28404e3cdb45SManasi Navare 		skl_scaler_disable(old_slave_crtc_state);
28414e3cdb45SManasi Navare 	}
28424e3cdb45SManasi Navare 
2843379bc100SJani Nikula 	/*
2844379bc100SJani Nikula 	 * When called from DP MST code:
2845379bc100SJani Nikula 	 * - old_conn_state will be NULL
2846379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
2847379bc100SJani Nikula 	 * - the main connector associated with this port
2848379bc100SJani Nikula 	 *   won't be active or linked to a crtc
2849379bc100SJani Nikula 	 * - old_crtc_state will be the state of the last stream to
2850379bc100SJani Nikula 	 *   be deactivated on this port, and it may not be the same
2851379bc100SJani Nikula 	 *   stream that was activated last, but each stream
2852379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
2853379bc100SJani Nikula 	 *   the DP link parameteres
2854379bc100SJani Nikula 	 */
2855379bc100SJani Nikula 
2856379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
2857ede9771dSVille Syrjälä 		intel_ddi_post_disable_hdmi(state, encoder, old_crtc_state,
2858ede9771dSVille Syrjälä 					    old_conn_state);
2859379bc100SJani Nikula 	else
2860ede9771dSVille Syrjälä 		intel_ddi_post_disable_dp(state, encoder, old_crtc_state,
2861ede9771dSVille Syrjälä 					  old_conn_state);
2862379bc100SJani Nikula 
286317bef9baSVille Syrjälä 	if (intel_crtc_has_dp_encoder(old_crtc_state) || is_tc_port)
2864162e68e1SImre Deak 		intel_display_power_put(dev_priv,
2865162e68e1SImre Deak 					intel_ddi_main_link_aux_domain(dig_port),
2866162e68e1SImre Deak 					fetch_and_zero(&dig_port->aux_wakeref));
286717bef9baSVille Syrjälä 
286817bef9baSVille Syrjälä 	if (is_tc_port)
286917bef9baSVille Syrjälä 		intel_tc_port_put_link(dig_port);
2870379bc100SJani Nikula }
2871379bc100SJani Nikula 
2872ede9771dSVille Syrjälä void intel_ddi_fdi_post_disable(struct intel_atomic_state *state,
2873ede9771dSVille Syrjälä 				struct intel_encoder *encoder,
2874379bc100SJani Nikula 				const struct intel_crtc_state *old_crtc_state,
2875379bc100SJani Nikula 				const struct drm_connector_state *old_conn_state)
2876379bc100SJani Nikula {
2877379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2878379bc100SJani Nikula 	u32 val;
2879379bc100SJani Nikula 
2880379bc100SJani Nikula 	/*
2881379bc100SJani Nikula 	 * Bspec lists this as both step 13 (before DDI_BUF_CTL disable)
2882379bc100SJani Nikula 	 * and step 18 (after clearing PORT_CLK_SEL). Based on a BUN,
2883379bc100SJani Nikula 	 * step 13 is the correct place for it. Step 18 is where it was
2884379bc100SJani Nikula 	 * originally before the BUN.
2885379bc100SJani Nikula 	 */
2886f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
2887379bc100SJani Nikula 	val &= ~FDI_RX_ENABLE;
2888f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
2889379bc100SJani Nikula 
2890379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
2891c133df69SVille Syrjälä 	intel_ddi_disable_clock(encoder);
2892379bc100SJani Nikula 
2893f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_MISC(PIPE_A));
2894379bc100SJani Nikula 	val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
2895379bc100SJani Nikula 	val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
2896f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_MISC(PIPE_A), val);
2897379bc100SJani Nikula 
2898f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
2899379bc100SJani Nikula 	val &= ~FDI_PCDCLK;
2900f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
2901379bc100SJani Nikula 
2902f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
2903379bc100SJani Nikula 	val &= ~FDI_RX_PLL_ENABLE;
2904f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
2905379bc100SJani Nikula }
2906379bc100SJani Nikula 
2907d82a855aSVille Syrjälä static void trans_port_sync_stop_link_train(struct intel_atomic_state *state,
2908d82a855aSVille Syrjälä 					    struct intel_encoder *encoder,
2909d82a855aSVille Syrjälä 					    const struct intel_crtc_state *crtc_state)
2910d82a855aSVille Syrjälä {
2911d82a855aSVille Syrjälä 	const struct drm_connector_state *conn_state;
2912d82a855aSVille Syrjälä 	struct drm_connector *conn;
2913d82a855aSVille Syrjälä 	int i;
2914d82a855aSVille Syrjälä 
2915d82a855aSVille Syrjälä 	if (!crtc_state->sync_mode_slaves_mask)
2916d82a855aSVille Syrjälä 		return;
2917d82a855aSVille Syrjälä 
2918d82a855aSVille Syrjälä 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
2919d82a855aSVille Syrjälä 		struct intel_encoder *slave_encoder =
2920d82a855aSVille Syrjälä 			to_intel_encoder(conn_state->best_encoder);
2921d82a855aSVille Syrjälä 		struct intel_crtc *slave_crtc = to_intel_crtc(conn_state->crtc);
2922d82a855aSVille Syrjälä 		const struct intel_crtc_state *slave_crtc_state;
2923d82a855aSVille Syrjälä 
2924d82a855aSVille Syrjälä 		if (!slave_crtc)
2925d82a855aSVille Syrjälä 			continue;
2926d82a855aSVille Syrjälä 
2927d82a855aSVille Syrjälä 		slave_crtc_state =
2928d82a855aSVille Syrjälä 			intel_atomic_get_new_crtc_state(state, slave_crtc);
2929d82a855aSVille Syrjälä 
2930d82a855aSVille Syrjälä 		if (slave_crtc_state->master_transcoder !=
2931d82a855aSVille Syrjälä 		    crtc_state->cpu_transcoder)
2932d82a855aSVille Syrjälä 			continue;
2933d82a855aSVille Syrjälä 
2934a621860aSVille Syrjälä 		intel_dp_stop_link_train(enc_to_intel_dp(slave_encoder),
2935a621860aSVille Syrjälä 					 slave_crtc_state);
2936d82a855aSVille Syrjälä 	}
2937d82a855aSVille Syrjälä 
2938d82a855aSVille Syrjälä 	usleep_range(200, 400);
2939d82a855aSVille Syrjälä 
2940a621860aSVille Syrjälä 	intel_dp_stop_link_train(enc_to_intel_dp(encoder),
2941a621860aSVille Syrjälä 				 crtc_state);
2942d82a855aSVille Syrjälä }
2943d82a855aSVille Syrjälä 
2944ede9771dSVille Syrjälä static void intel_enable_ddi_dp(struct intel_atomic_state *state,
2945ede9771dSVille Syrjälä 				struct intel_encoder *encoder,
2946379bc100SJani Nikula 				const struct intel_crtc_state *crtc_state,
2947379bc100SJani Nikula 				const struct drm_connector_state *conn_state)
2948379bc100SJani Nikula {
2949379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2950b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2951998cc864SUma Shankar 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2952379bc100SJani Nikula 	enum port port = encoder->port;
2953379bc100SJani Nikula 
2954005e9537SMatt Roper 	if (port == PORT_A && DISPLAY_VER(dev_priv) < 9)
2955a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
2956379bc100SJani Nikula 
2957379bc100SJani Nikula 	intel_edp_backlight_on(crtc_state, conn_state);
2958998cc864SUma Shankar 
2959998cc864SUma Shankar 	if (!dig_port->lspcon.active || dig_port->dp.has_hdmi_sink)
29601bf3657cSGwan-gyeong Mun 		intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
2961998cc864SUma Shankar 
29623a3dd534SJosé Roberto de Souza 	intel_drrs_enable(intel_dp, crtc_state);
2963379bc100SJani Nikula 
2964379bc100SJani Nikula 	if (crtc_state->has_audio)
2965379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
2966d82a855aSVille Syrjälä 
2967d82a855aSVille Syrjälä 	trans_port_sync_stop_link_train(state, encoder, crtc_state);
2968379bc100SJani Nikula }
2969379bc100SJani Nikula 
2970379bc100SJani Nikula static i915_reg_t
2971379bc100SJani Nikula gen9_chicken_trans_reg_by_port(struct drm_i915_private *dev_priv,
2972379bc100SJani Nikula 			       enum port port)
2973379bc100SJani Nikula {
297412c4d4c1SVille Syrjälä 	static const enum transcoder trans[] = {
297512c4d4c1SVille Syrjälä 		[PORT_A] = TRANSCODER_EDP,
297612c4d4c1SVille Syrjälä 		[PORT_B] = TRANSCODER_A,
297712c4d4c1SVille Syrjälä 		[PORT_C] = TRANSCODER_B,
297812c4d4c1SVille Syrjälä 		[PORT_D] = TRANSCODER_C,
297912c4d4c1SVille Syrjälä 		[PORT_E] = TRANSCODER_A,
2980379bc100SJani Nikula 	};
2981379bc100SJani Nikula 
2982005e9537SMatt Roper 	drm_WARN_ON(&dev_priv->drm, DISPLAY_VER(dev_priv) < 9);
2983379bc100SJani Nikula 
29841de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port < PORT_A || port > PORT_E))
2985379bc100SJani Nikula 		port = PORT_A;
2986379bc100SJani Nikula 
298712c4d4c1SVille Syrjälä 	return CHICKEN_TRANS(trans[port]);
2988379bc100SJani Nikula }
2989379bc100SJani Nikula 
2990ede9771dSVille Syrjälä static void intel_enable_ddi_hdmi(struct intel_atomic_state *state,
2991ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
2992379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
2993379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
2994379bc100SJani Nikula {
2995379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2996b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2997379bc100SJani Nikula 	struct drm_connector *connector = conn_state->connector;
2998379bc100SJani Nikula 	enum port port = encoder->port;
2999379bc100SJani Nikula 
3000379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3001379bc100SJani Nikula 					       crtc_state->hdmi_high_tmds_clock_ratio,
3002379bc100SJani Nikula 					       crtc_state->hdmi_scrambling))
300347bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
300447bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to configure sink scrambling/TMDS bit clock ratio\n",
3005379bc100SJani Nikula 			    connector->base.id, connector->name);
3006379bc100SJani Nikula 
30075bafd85dSVille Syrjälä 	if (has_buf_trans_select(dev_priv))
3008e722ab8bSVille Syrjälä 		hsw_prepare_hdmi_ddi_buffers(encoder, crtc_state);
3009c9b69041SVille Syrjälä 
3010e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
3011c9b69041SVille Syrjälä 
3012379bc100SJani Nikula 	/* Display WA #1143: skl,kbl,cfl */
301393e7e61eSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv)) {
3014379bc100SJani Nikula 		/*
3015379bc100SJani Nikula 		 * For some reason these chicken bits have been
3016379bc100SJani Nikula 		 * stuffed into a transcoder register, event though
3017379bc100SJani Nikula 		 * the bits affect a specific DDI port rather than
3018379bc100SJani Nikula 		 * a specific transcoder.
3019379bc100SJani Nikula 		 */
3020379bc100SJani Nikula 		i915_reg_t reg = gen9_chicken_trans_reg_by_port(dev_priv, port);
3021379bc100SJani Nikula 		u32 val;
3022379bc100SJani Nikula 
3023f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, reg);
3024379bc100SJani Nikula 
3025379bc100SJani Nikula 		if (port == PORT_E)
3026379bc100SJani Nikula 			val |= DDIE_TRAINING_OVERRIDE_ENABLE |
3027379bc100SJani Nikula 				DDIE_TRAINING_OVERRIDE_VALUE;
3028379bc100SJani Nikula 		else
3029379bc100SJani Nikula 			val |= DDI_TRAINING_OVERRIDE_ENABLE |
3030379bc100SJani Nikula 				DDI_TRAINING_OVERRIDE_VALUE;
3031379bc100SJani Nikula 
3032f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3033f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, reg);
3034379bc100SJani Nikula 
3035379bc100SJani Nikula 		udelay(1);
3036379bc100SJani Nikula 
3037379bc100SJani Nikula 		if (port == PORT_E)
3038379bc100SJani Nikula 			val &= ~(DDIE_TRAINING_OVERRIDE_ENABLE |
3039379bc100SJani Nikula 				 DDIE_TRAINING_OVERRIDE_VALUE);
3040379bc100SJani Nikula 		else
3041379bc100SJani Nikula 			val &= ~(DDI_TRAINING_OVERRIDE_ENABLE |
3042379bc100SJani Nikula 				 DDI_TRAINING_OVERRIDE_VALUE);
3043379bc100SJani Nikula 
3044f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3045379bc100SJani Nikula 	}
3046379bc100SJani Nikula 
30471e0cb7beSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
30481e0cb7beSVille Syrjälä 
3049379bc100SJani Nikula 	/* In HDMI/DVI mode, the port width, and swing/emphasis values
3050379bc100SJani Nikula 	 * are ignored so nothing special needs to be done besides
3051379bc100SJani Nikula 	 * enabling the port.
3052414002f1SImre Deak 	 *
3053414002f1SImre Deak 	 * On ADL_P the PHY link rate and lane count must be programmed but
3054414002f1SImre Deak 	 * these are both 0 for HDMI.
3055379bc100SJani Nikula 	 */
3056f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port),
3057379bc100SJani Nikula 		       dig_port->saved_port_bits | DDI_BUF_CTL_ENABLE);
3058379bc100SJani Nikula 
3059379bc100SJani Nikula 	if (crtc_state->has_audio)
3060379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3061379bc100SJani Nikula }
3062379bc100SJani Nikula 
3063ede9771dSVille Syrjälä static void intel_enable_ddi(struct intel_atomic_state *state,
3064ede9771dSVille Syrjälä 			     struct intel_encoder *encoder,
3065379bc100SJani Nikula 			     const struct intel_crtc_state *crtc_state,
3066379bc100SJani Nikula 			     const struct drm_connector_state *conn_state)
3067379bc100SJani Nikula {
30688b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc_state->has_pch_encoder);
306921fd23acSJani Nikula 
30704e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner_slave)
3071eed22a46SVille Syrjälä 		intel_ddi_enable_transcoder_func(encoder, crtc_state);
30727c2fedd7SVille Syrjälä 
3073aa52b39dSManasi Navare 	intel_vrr_enable(encoder, crtc_state);
3074aa52b39dSManasi Navare 
30758c66081bSVille Syrjälä 	intel_enable_transcoder(crtc_state);
307621fd23acSJani Nikula 
307721fd23acSJani Nikula 	intel_crtc_vblank_on(crtc_state);
307821fd23acSJani Nikula 
3079379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3080ede9771dSVille Syrjälä 		intel_enable_ddi_hdmi(state, encoder, crtc_state, conn_state);
3081379bc100SJani Nikula 	else
3082ede9771dSVille Syrjälä 		intel_enable_ddi_dp(state, encoder, crtc_state, conn_state);
3083379bc100SJani Nikula 
3084379bc100SJani Nikula 	/* Enable hdcp if it's desired */
3085379bc100SJani Nikula 	if (conn_state->content_protection ==
3086379bc100SJani Nikula 	    DRM_MODE_CONTENT_PROTECTION_DESIRED)
3087d456512cSRamalingam C 		intel_hdcp_enable(to_intel_connector(conn_state->connector),
3088fc6097d4SAnshuman Gupta 				  crtc_state,
3089d456512cSRamalingam C 				  (u8)conn_state->hdcp_content_type);
3090379bc100SJani Nikula }
3091379bc100SJani Nikula 
3092ede9771dSVille Syrjälä static void intel_disable_ddi_dp(struct intel_atomic_state *state,
3093ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
3094379bc100SJani Nikula 				 const struct intel_crtc_state *old_crtc_state,
3095379bc100SJani Nikula 				 const struct drm_connector_state *old_conn_state)
3096379bc100SJani Nikula {
3097b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3098379bc100SJani Nikula 
3099379bc100SJani Nikula 	intel_dp->link_trained = false;
3100379bc100SJani Nikula 
3101379bc100SJani Nikula 	intel_edp_backlight_off(old_conn_state);
3102379bc100SJani Nikula 	/* Disable the decompression in DP Sink */
3103379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, old_crtc_state,
3104379bc100SJani Nikula 					      false);
31051639406aSManasi Navare 	/* Disable Ignore_MSA bit in DP Sink */
31061639406aSManasi Navare 	intel_dp_sink_set_msa_timing_par_ignore_state(intel_dp, old_crtc_state,
31071639406aSManasi Navare 						      false);
3108379bc100SJani Nikula }
3109379bc100SJani Nikula 
3110ede9771dSVille Syrjälä static void intel_disable_ddi_hdmi(struct intel_atomic_state *state,
3111ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
3112379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3113379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3114379bc100SJani Nikula {
311547bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3116379bc100SJani Nikula 	struct drm_connector *connector = old_conn_state->connector;
3117379bc100SJani Nikula 
3118379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3119379bc100SJani Nikula 					       false, false))
312047bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
312147bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to reset sink scrambling/TMDS bit clock ratio\n",
3122379bc100SJani Nikula 			    connector->base.id, connector->name);
3123379bc100SJani Nikula }
3124379bc100SJani Nikula 
312584030adbSJosé Roberto de Souza static void intel_pre_disable_ddi(struct intel_atomic_state *state,
312684030adbSJosé Roberto de Souza 				  struct intel_encoder *encoder,
312784030adbSJosé Roberto de Souza 				  const struct intel_crtc_state *old_crtc_state,
312884030adbSJosé Roberto de Souza 				  const struct drm_connector_state *old_conn_state)
312984030adbSJosé Roberto de Souza {
313084030adbSJosé Roberto de Souza 	struct intel_dp *intel_dp;
313184030adbSJosé Roberto de Souza 
313284030adbSJosé Roberto de Souza 	if (old_crtc_state->has_audio)
313384030adbSJosé Roberto de Souza 		intel_audio_codec_disable(encoder, old_crtc_state,
313484030adbSJosé Roberto de Souza 					  old_conn_state);
313584030adbSJosé Roberto de Souza 
313684030adbSJosé Roberto de Souza 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
313784030adbSJosé Roberto de Souza 		return;
313884030adbSJosé Roberto de Souza 
313984030adbSJosé Roberto de Souza 	intel_dp = enc_to_intel_dp(encoder);
31403a3dd534SJosé Roberto de Souza 	intel_drrs_disable(intel_dp, old_crtc_state);
314184030adbSJosé Roberto de Souza 	intel_psr_disable(intel_dp, old_crtc_state);
314284030adbSJosé Roberto de Souza }
314384030adbSJosé Roberto de Souza 
3144ede9771dSVille Syrjälä static void intel_disable_ddi(struct intel_atomic_state *state,
3145ede9771dSVille Syrjälä 			      struct intel_encoder *encoder,
3146379bc100SJani Nikula 			      const struct intel_crtc_state *old_crtc_state,
3147379bc100SJani Nikula 			      const struct drm_connector_state *old_conn_state)
3148379bc100SJani Nikula {
3149379bc100SJani Nikula 	intel_hdcp_disable(to_intel_connector(old_conn_state->connector));
3150379bc100SJani Nikula 
3151379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3152ede9771dSVille Syrjälä 		intel_disable_ddi_hdmi(state, encoder, old_crtc_state,
3153ede9771dSVille Syrjälä 				       old_conn_state);
3154379bc100SJani Nikula 	else
3155ede9771dSVille Syrjälä 		intel_disable_ddi_dp(state, encoder, old_crtc_state,
3156ede9771dSVille Syrjälä 				     old_conn_state);
3157379bc100SJani Nikula }
3158379bc100SJani Nikula 
3159ede9771dSVille Syrjälä static void intel_ddi_update_pipe_dp(struct intel_atomic_state *state,
3160ede9771dSVille Syrjälä 				     struct intel_encoder *encoder,
3161379bc100SJani Nikula 				     const struct intel_crtc_state *crtc_state,
3162379bc100SJani Nikula 				     const struct drm_connector_state *conn_state)
3163379bc100SJani Nikula {
3164b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3165379bc100SJani Nikula 
31660c06fa15SGwan-gyeong Mun 	intel_ddi_set_dp_msa(crtc_state, conn_state);
3167379bc100SJani Nikula 
316876d45d06SGwan-gyeong Mun 	intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
31693a3dd534SJosé Roberto de Souza 	intel_drrs_update(intel_dp, crtc_state);
3170379bc100SJani Nikula 
3171c0a52f8bSJani Nikula 	intel_backlight_update(state, encoder, crtc_state, conn_state);
3172379bc100SJani Nikula }
3173379bc100SJani Nikula 
3174f1c7a36bSSean Paul void intel_ddi_update_pipe(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 {
3179d456512cSRamalingam C 
3180f1c7a36bSSean Paul 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) &&
3181f1c7a36bSSean Paul 	    !intel_encoder_is_mst(encoder))
3182ede9771dSVille Syrjälä 		intel_ddi_update_pipe_dp(state, encoder, crtc_state,
3183ede9771dSVille Syrjälä 					 conn_state);
3184379bc100SJani Nikula 
3185ede9771dSVille Syrjälä 	intel_hdcp_update_pipe(state, encoder, crtc_state, conn_state);
3186379bc100SJani Nikula }
3187379bc100SJani Nikula 
3188379bc100SJani Nikula static void
318924a7bfe0SImre Deak intel_ddi_update_prepare(struct intel_atomic_state *state,
319024a7bfe0SImre Deak 			 struct intel_encoder *encoder,
319124a7bfe0SImre Deak 			 struct intel_crtc *crtc)
319224a7bfe0SImre Deak {
319324a7bfe0SImre Deak 	struct intel_crtc_state *crtc_state =
319424a7bfe0SImre Deak 		crtc ? intel_atomic_get_new_crtc_state(state, crtc) : NULL;
319524a7bfe0SImre Deak 	int required_lanes = crtc_state ? crtc_state->lane_count : 1;
319624a7bfe0SImre Deak 
31978b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc && crtc->active);
319824a7bfe0SImre Deak 
3199b7d02c3aSVille Syrjälä 	intel_tc_port_get_link(enc_to_dig_port(encoder),
3200b7d02c3aSVille Syrjälä 		               required_lanes);
32011326a92cSMaarten Lankhorst 	if (crtc_state && crtc_state->hw.active)
320224a7bfe0SImre Deak 		intel_update_active_dpll(state, crtc, encoder);
320324a7bfe0SImre Deak }
320424a7bfe0SImre Deak 
320524a7bfe0SImre Deak static void
320624a7bfe0SImre Deak intel_ddi_update_complete(struct intel_atomic_state *state,
320724a7bfe0SImre Deak 			  struct intel_encoder *encoder,
320824a7bfe0SImre Deak 			  struct intel_crtc *crtc)
320924a7bfe0SImre Deak {
3210b7d02c3aSVille Syrjälä 	intel_tc_port_put_link(enc_to_dig_port(encoder));
321124a7bfe0SImre Deak }
321224a7bfe0SImre Deak 
321324a7bfe0SImre Deak static void
3214ede9771dSVille Syrjälä intel_ddi_pre_pll_enable(struct intel_atomic_state *state,
3215ede9771dSVille Syrjälä 			 struct intel_encoder *encoder,
3216379bc100SJani Nikula 			 const struct intel_crtc_state *crtc_state,
3217379bc100SJani Nikula 			 const struct drm_connector_state *conn_state)
3218379bc100SJani Nikula {
3219379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3220b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3221d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3222d8fe2ab6SMatt Roper 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
3223379bc100SJani Nikula 
322424a7bfe0SImre Deak 	if (is_tc_port)
322524a7bfe0SImre Deak 		intel_tc_port_get_link(dig_port, crtc_state->lane_count);
322624a7bfe0SImre Deak 
3227162e68e1SImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state) || is_tc_port) {
3228162e68e1SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->aux_wakeref);
3229162e68e1SImre Deak 		dig_port->aux_wakeref =
3230379bc100SJani Nikula 			intel_display_power_get(dev_priv,
3231379bc100SJani Nikula 						intel_ddi_main_link_aux_domain(dig_port));
3232162e68e1SImre Deak 	}
3233379bc100SJani Nikula 
323411a89708SImre Deak 	if (is_tc_port && !intel_tc_port_in_tbt_alt_mode(dig_port))
32359d44dcb9SLucas De Marchi 		/*
32369d44dcb9SLucas De Marchi 		 * Program the lane count for static/dynamic connections on
32379d44dcb9SLucas De Marchi 		 * Type-C ports.  Skip this step for TBT.
32389d44dcb9SLucas De Marchi 		 */
32399d44dcb9SLucas De Marchi 		intel_tc_port_set_fia_lane_count(dig_port, crtc_state->lane_count);
32402446e1d6SMatt Roper 	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3241379bc100SJani Nikula 		bxt_ddi_phy_set_lane_optim_mask(encoder,
3242379bc100SJani Nikula 						crtc_state->lane_lat_optim_mask);
3243379bc100SJani Nikula }
3244379bc100SJani Nikula 
3245a621860aSVille Syrjälä static void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp,
3246a621860aSVille Syrjälä 					   const struct intel_crtc_state *crtc_state)
3247379bc100SJani Nikula {
3248ef79fafeSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3249ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3250ef79fafeSVille Syrjälä 	enum port port = encoder->port;
325135ac28a8SLucas De Marchi 	u32 dp_tp_ctl, ddi_buf_ctl;
3252379bc100SJani Nikula 	bool wait = false;
3253379bc100SJani Nikula 
3254ef79fafeSVille Syrjälä 	dp_tp_ctl = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
325535ac28a8SLucas De Marchi 
325635ac28a8SLucas De Marchi 	if (dp_tp_ctl & DP_TP_CTL_ENABLE) {
3257f7960e7fSJani Nikula 		ddi_buf_ctl = intel_de_read(dev_priv, DDI_BUF_CTL(port));
325835ac28a8SLucas De Marchi 		if (ddi_buf_ctl & DDI_BUF_CTL_ENABLE) {
3259f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_BUF_CTL(port),
326035ac28a8SLucas De Marchi 				       ddi_buf_ctl & ~DDI_BUF_CTL_ENABLE);
3261379bc100SJani Nikula 			wait = true;
3262379bc100SJani Nikula 		}
3263379bc100SJani Nikula 
326435ac28a8SLucas De Marchi 		dp_tp_ctl &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
326535ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_LINK_TRAIN_PAT1;
3266ef79fafeSVille Syrjälä 		intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3267ef79fafeSVille Syrjälä 		intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3268379bc100SJani Nikula 
3269379bc100SJani Nikula 		if (wait)
3270379bc100SJani Nikula 			intel_wait_ddi_buf_idle(dev_priv, port);
3271379bc100SJani Nikula 	}
3272379bc100SJani Nikula 
3273963501bdSImre Deak 	dp_tp_ctl = DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_PAT1;
3274a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
327535ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_MST;
3276a621860aSVille Syrjälä 	} else {
327735ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_SST;
3278379bc100SJani Nikula 		if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
327935ac28a8SLucas De Marchi 			dp_tp_ctl |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
3280379bc100SJani Nikula 	}
3281ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3282ef79fafeSVille Syrjälä 	intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3283379bc100SJani Nikula 
3284379bc100SJani Nikula 	intel_dp->DP |= DDI_BUF_CTL_ENABLE;
3285f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
3286f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
3287379bc100SJani Nikula 
3288e828da30SManasi Navare 	intel_wait_ddi_buf_active(dev_priv, port);
3289379bc100SJani Nikula }
3290379bc100SJani Nikula 
3291eee3f911SVille Syrjälä static void intel_ddi_set_link_train(struct intel_dp *intel_dp,
3292a621860aSVille Syrjälä 				     const struct intel_crtc_state *crtc_state,
3293eee3f911SVille Syrjälä 				     u8 dp_train_pat)
3294eee3f911SVille Syrjälä {
3295ef79fafeSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3296ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3297eee3f911SVille Syrjälä 	u32 temp;
3298eee3f911SVille Syrjälä 
3299ef79fafeSVille Syrjälä 	temp = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3300eee3f911SVille Syrjälä 
3301eee3f911SVille Syrjälä 	temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
33026777a855SImre Deak 	switch (intel_dp_training_pattern_symbol(dp_train_pat)) {
3303eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_DISABLE:
3304eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
3305eee3f911SVille Syrjälä 		break;
3306eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_1:
3307eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
3308eee3f911SVille Syrjälä 		break;
3309eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_2:
3310eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
3311eee3f911SVille Syrjälä 		break;
3312eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_3:
3313eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
3314eee3f911SVille Syrjälä 		break;
3315eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_4:
3316eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT4;
3317eee3f911SVille Syrjälä 		break;
3318eee3f911SVille Syrjälä 	}
3319eee3f911SVille Syrjälä 
3320ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), temp);
3321eee3f911SVille Syrjälä }
3322eee3f911SVille Syrjälä 
3323a621860aSVille Syrjälä static void intel_ddi_set_idle_link_train(struct intel_dp *intel_dp,
3324a621860aSVille Syrjälä 					  const struct intel_crtc_state *crtc_state)
33258fdda385SVille Syrjälä {
33268fdda385SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
33278fdda385SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
33288fdda385SVille Syrjälä 	enum port port = encoder->port;
33298fdda385SVille Syrjälä 	u32 val;
33308fdda385SVille Syrjälä 
3331ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
33328fdda385SVille Syrjälä 	val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
33338fdda385SVille Syrjälä 	val |= DP_TP_CTL_LINK_TRAIN_IDLE;
3334ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
33358fdda385SVille Syrjälä 
33368fdda385SVille Syrjälä 	/*
33378fdda385SVille Syrjälä 	 * Until TGL on PORT_A we can have only eDP in SST mode. There the only
33388fdda385SVille Syrjälä 	 * reason we need to set idle transmission mode is to work around a HW
33398fdda385SVille Syrjälä 	 * issue where we enable the pipe while not in idle link-training mode.
33408fdda385SVille Syrjälä 	 * In this case there is requirement to wait for a minimum number of
33418fdda385SVille Syrjälä 	 * idle patterns to be sent.
33428fdda385SVille Syrjälä 	 */
3343005e9537SMatt Roper 	if (port == PORT_A && DISPLAY_VER(dev_priv) < 12)
33448fdda385SVille Syrjälä 		return;
33458fdda385SVille Syrjälä 
3346ef79fafeSVille Syrjälä 	if (intel_de_wait_for_set(dev_priv,
3347ef79fafeSVille Syrjälä 				  dp_tp_status_reg(encoder, crtc_state),
33488fdda385SVille Syrjälä 				  DP_TP_STATUS_IDLE_DONE, 1))
33498fdda385SVille Syrjälä 		drm_err(&dev_priv->drm,
33508fdda385SVille Syrjälä 			"Timed out waiting for DP idle patterns\n");
33518fdda385SVille Syrjälä }
33528fdda385SVille Syrjälä 
3353379bc100SJani Nikula static bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
3354379bc100SJani Nikula 				       enum transcoder cpu_transcoder)
3355379bc100SJani Nikula {
3356379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP)
3357379bc100SJani Nikula 		return false;
3358379bc100SJani Nikula 
3359615a7724SAnshuman Gupta 	if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO_MMIO))
3360379bc100SJani Nikula 		return false;
3361379bc100SJani Nikula 
3362f7960e7fSJani Nikula 	return intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD) &
3363379bc100SJani Nikula 		AUDIO_OUTPUT_ENABLE(cpu_transcoder);
3364379bc100SJani Nikula }
3365379bc100SJani Nikula 
3366379bc100SJani Nikula void intel_ddi_compute_min_voltage_level(struct drm_i915_private *dev_priv,
3367379bc100SJani Nikula 					 struct intel_crtc_state *crtc_state)
3368379bc100SJani Nikula {
3369005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12 && crtc_state->port_clock > 594000)
33700fde0b1dSMatt Roper 		crtc_state->min_voltage_level = 2;
337124ea098bSTejas Upadhyay 	else if (IS_JSL_EHL(dev_priv) && crtc_state->port_clock > 594000)
33729d5fd37eSMatt Roper 		crtc_state->min_voltage_level = 3;
3373005e9537SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 11 && crtc_state->port_clock > 594000)
3374379bc100SJani Nikula 		crtc_state->min_voltage_level = 1;
3375379bc100SJani Nikula }
3376379bc100SJani Nikula 
3377dc5b8ed5SVille Syrjälä static enum transcoder bdw_transcoder_master_readout(struct drm_i915_private *dev_priv,
337802d8ea47SVille Syrjälä 						     enum transcoder cpu_transcoder)
337902d8ea47SVille Syrjälä {
3380dc5b8ed5SVille Syrjälä 	u32 master_select;
338102d8ea47SVille Syrjälä 
3382005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
3383dc5b8ed5SVille Syrjälä 		u32 ctl2 = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL2(cpu_transcoder));
338402d8ea47SVille Syrjälä 
338502d8ea47SVille Syrjälä 		if ((ctl2 & PORT_SYNC_MODE_ENABLE) == 0)
338602d8ea47SVille Syrjälä 			return INVALID_TRANSCODER;
338702d8ea47SVille Syrjälä 
3388d4d7d9caSVille Syrjälä 		master_select = REG_FIELD_GET(PORT_SYNC_MODE_MASTER_SELECT_MASK, ctl2);
3389dc5b8ed5SVille Syrjälä 	} else {
3390dc5b8ed5SVille Syrjälä 		u32 ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3391dc5b8ed5SVille Syrjälä 
3392dc5b8ed5SVille Syrjälä 		if ((ctl & TRANS_DDI_PORT_SYNC_ENABLE) == 0)
3393dc5b8ed5SVille Syrjälä 			return INVALID_TRANSCODER;
3394dc5b8ed5SVille Syrjälä 
3395dc5b8ed5SVille Syrjälä 		master_select = REG_FIELD_GET(TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK, ctl);
3396dc5b8ed5SVille Syrjälä 	}
339702d8ea47SVille Syrjälä 
339802d8ea47SVille Syrjälä 	if (master_select == 0)
339902d8ea47SVille Syrjälä 		return TRANSCODER_EDP;
340002d8ea47SVille Syrjälä 	else
340102d8ea47SVille Syrjälä 		return master_select - 1;
340202d8ea47SVille Syrjälä }
340302d8ea47SVille Syrjälä 
3404dc5b8ed5SVille Syrjälä static void bdw_get_trans_port_sync_config(struct intel_crtc_state *crtc_state)
340502d8ea47SVille Syrjälä {
340602d8ea47SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
340702d8ea47SVille Syrjälä 	u32 transcoders = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
340802d8ea47SVille Syrjälä 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
340902d8ea47SVille Syrjälä 	enum transcoder cpu_transcoder;
341002d8ea47SVille Syrjälä 
341102d8ea47SVille Syrjälä 	crtc_state->master_transcoder =
3412dc5b8ed5SVille Syrjälä 		bdw_transcoder_master_readout(dev_priv, crtc_state->cpu_transcoder);
341302d8ea47SVille Syrjälä 
341402d8ea47SVille Syrjälä 	for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder, transcoders) {
341502d8ea47SVille Syrjälä 		enum intel_display_power_domain power_domain;
341602d8ea47SVille Syrjälä 		intel_wakeref_t trans_wakeref;
341702d8ea47SVille Syrjälä 
341802d8ea47SVille Syrjälä 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
341902d8ea47SVille Syrjälä 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
342002d8ea47SVille Syrjälä 								   power_domain);
342102d8ea47SVille Syrjälä 
342202d8ea47SVille Syrjälä 		if (!trans_wakeref)
342302d8ea47SVille Syrjälä 			continue;
342402d8ea47SVille Syrjälä 
3425dc5b8ed5SVille Syrjälä 		if (bdw_transcoder_master_readout(dev_priv, cpu_transcoder) ==
342602d8ea47SVille Syrjälä 		    crtc_state->cpu_transcoder)
342702d8ea47SVille Syrjälä 			crtc_state->sync_mode_slaves_mask |= BIT(cpu_transcoder);
342802d8ea47SVille Syrjälä 
342902d8ea47SVille Syrjälä 		intel_display_power_put(dev_priv, power_domain, trans_wakeref);
343002d8ea47SVille Syrjälä 	}
343102d8ea47SVille Syrjälä 
343202d8ea47SVille Syrjälä 	drm_WARN_ON(&dev_priv->drm,
343302d8ea47SVille Syrjälä 		    crtc_state->master_transcoder != INVALID_TRANSCODER &&
343402d8ea47SVille Syrjälä 		    crtc_state->sync_mode_slaves_mask);
343502d8ea47SVille Syrjälä }
343602d8ea47SVille Syrjälä 
34370385eceaSManasi Navare static void intel_ddi_read_func_ctl(struct intel_encoder *encoder,
3438379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config)
3439379bc100SJani Nikula {
3440379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3441f15f01a7SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3442379bc100SJani Nikula 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
3443a44289b9SUma Shankar 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3444379bc100SJani Nikula 	u32 temp, flags = 0;
3445379bc100SJani Nikula 
3446f7960e7fSJani Nikula 	temp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3447379bc100SJani Nikula 	if (temp & TRANS_DDI_PHSYNC)
3448379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PHSYNC;
3449379bc100SJani Nikula 	else
3450379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NHSYNC;
3451379bc100SJani Nikula 	if (temp & TRANS_DDI_PVSYNC)
3452379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PVSYNC;
3453379bc100SJani Nikula 	else
3454379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NVSYNC;
3455379bc100SJani Nikula 
34561326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.flags |= flags;
3457379bc100SJani Nikula 
3458379bc100SJani Nikula 	switch (temp & TRANS_DDI_BPC_MASK) {
3459379bc100SJani Nikula 	case TRANS_DDI_BPC_6:
3460379bc100SJani Nikula 		pipe_config->pipe_bpp = 18;
3461379bc100SJani Nikula 		break;
3462379bc100SJani Nikula 	case TRANS_DDI_BPC_8:
3463379bc100SJani Nikula 		pipe_config->pipe_bpp = 24;
3464379bc100SJani Nikula 		break;
3465379bc100SJani Nikula 	case TRANS_DDI_BPC_10:
3466379bc100SJani Nikula 		pipe_config->pipe_bpp = 30;
3467379bc100SJani Nikula 		break;
3468379bc100SJani Nikula 	case TRANS_DDI_BPC_12:
3469379bc100SJani Nikula 		pipe_config->pipe_bpp = 36;
3470379bc100SJani Nikula 		break;
3471379bc100SJani Nikula 	default:
3472379bc100SJani Nikula 		break;
3473379bc100SJani Nikula 	}
3474379bc100SJani Nikula 
3475379bc100SJani Nikula 	switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
3476379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
3477379bc100SJani Nikula 		pipe_config->has_hdmi_sink = true;
3478379bc100SJani Nikula 
3479379bc100SJani Nikula 		pipe_config->infoframes.enable |=
3480379bc100SJani Nikula 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
3481379bc100SJani Nikula 
3482379bc100SJani Nikula 		if (pipe_config->infoframes.enable)
3483379bc100SJani Nikula 			pipe_config->has_infoframe = true;
3484379bc100SJani Nikula 
3485379bc100SJani Nikula 		if (temp & TRANS_DDI_HDMI_SCRAMBLING)
3486379bc100SJani Nikula 			pipe_config->hdmi_scrambling = true;
3487379bc100SJani Nikula 		if (temp & TRANS_DDI_HIGH_TMDS_CHAR_RATE)
3488379bc100SJani Nikula 			pipe_config->hdmi_high_tmds_clock_ratio = true;
3489df561f66SGustavo A. R. Silva 		fallthrough;
3490379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
3491379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);
3492379bc100SJani Nikula 		pipe_config->lane_count = 4;
3493379bc100SJani Nikula 		break;
3494379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
3495379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
3496379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
3497379bc100SJani Nikula 		else
3498379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
3499379bc100SJani Nikula 		pipe_config->lane_count =
3500379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
3501f15f01a7SVille Syrjälä 		intel_dp_get_m_n(crtc, pipe_config);
35028aa940c8SMaarten Lankhorst 
3503005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 11) {
3504ef79fafeSVille Syrjälä 			i915_reg_t dp_tp_ctl = dp_tp_ctl_reg(encoder, pipe_config);
35058aa940c8SMaarten Lankhorst 
35068aa940c8SMaarten Lankhorst 			pipe_config->fec_enable =
3507f7960e7fSJani Nikula 				intel_de_read(dev_priv, dp_tp_ctl) & DP_TP_CTL_FEC_ENABLE;
35088aa940c8SMaarten Lankhorst 
350947bdb1caSJani Nikula 			drm_dbg_kms(&dev_priv->drm,
351047bdb1caSJani Nikula 				    "[ENCODER:%d:%s] Fec status: %u\n",
35118aa940c8SMaarten Lankhorst 				    encoder->base.base.id, encoder->base.name,
35128aa940c8SMaarten Lankhorst 				    pipe_config->fec_enable);
35138aa940c8SMaarten Lankhorst 		}
35148aa940c8SMaarten Lankhorst 
3515a44289b9SUma Shankar 		if (dig_port->lspcon.active && dig_port->dp.has_hdmi_sink)
3516a44289b9SUma Shankar 			pipe_config->infoframes.enable |=
3517a44289b9SUma Shankar 				intel_lspcon_infoframes_enabled(encoder, pipe_config);
3518a44289b9SUma Shankar 		else
3519dee66f3eSGwan-gyeong Mun 			pipe_config->infoframes.enable |=
3520dee66f3eSGwan-gyeong Mun 				intel_hdmi_infoframes_enabled(encoder, pipe_config);
3521379bc100SJani Nikula 		break;
352265213594SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI_OR_128B132B:
352365213594SJani Nikula 		if (!HAS_DP20(dev_priv)) {
352465213594SJani Nikula 			/* FDI */
352565213594SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
352665213594SJani Nikula 			break;
352765213594SJani Nikula 		}
352865213594SJani Nikula 		fallthrough; /* 128b/132b */
3529379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
3530379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_DP_MST);
3531379bc100SJani Nikula 		pipe_config->lane_count =
3532379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
35336671c367SJosé Roberto de Souza 
3534005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12)
35356671c367SJosé Roberto de Souza 			pipe_config->mst_master_transcoder =
35366671c367SJosé Roberto de Souza 					REG_FIELD_GET(TRANS_DDI_MST_TRANSPORT_SELECT_MASK, temp);
35376671c367SJosé Roberto de Souza 
3538f15f01a7SVille Syrjälä 		intel_dp_get_m_n(crtc, pipe_config);
3539dee66f3eSGwan-gyeong Mun 
3540dee66f3eSGwan-gyeong Mun 		pipe_config->infoframes.enable |=
3541dee66f3eSGwan-gyeong Mun 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
3542379bc100SJani Nikula 		break;
3543379bc100SJani Nikula 	default:
3544379bc100SJani Nikula 		break;
3545379bc100SJani Nikula 	}
35460385eceaSManasi Navare }
35470385eceaSManasi Navare 
3548351221ffSVille Syrjälä static void intel_ddi_get_config(struct intel_encoder *encoder,
35490385eceaSManasi Navare 				 struct intel_crtc_state *pipe_config)
35500385eceaSManasi Navare {
35510385eceaSManasi Navare 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
35520385eceaSManasi Navare 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
35530385eceaSManasi Navare 
35540385eceaSManasi Navare 	/* XXX: DSI transcoder paranoia */
35550385eceaSManasi Navare 	if (drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder)))
35560385eceaSManasi Navare 		return;
35570385eceaSManasi Navare 
35580385eceaSManasi Navare 	if (pipe_config->bigjoiner_slave) {
35590385eceaSManasi Navare 		/* read out pipe settings from master */
35600385eceaSManasi Navare 		enum transcoder save = pipe_config->cpu_transcoder;
35610385eceaSManasi Navare 
35620385eceaSManasi Navare 		/* Our own transcoder needs to be disabled when reading it in intel_ddi_read_func_ctl() */
35630385eceaSManasi Navare 		WARN_ON(pipe_config->output_types);
35640385eceaSManasi Navare 		pipe_config->cpu_transcoder = (enum transcoder)pipe_config->bigjoiner_linked_crtc->pipe;
35650385eceaSManasi Navare 		intel_ddi_read_func_ctl(encoder, pipe_config);
35660385eceaSManasi Navare 		pipe_config->cpu_transcoder = save;
35670385eceaSManasi Navare 	} else {
35680385eceaSManasi Navare 		intel_ddi_read_func_ctl(encoder, pipe_config);
35690385eceaSManasi Navare 	}
3570379bc100SJani Nikula 
35715b616a29SJani Nikula 	intel_ddi_mso_get_config(encoder, pipe_config);
35725b616a29SJani Nikula 
3573379bc100SJani Nikula 	pipe_config->has_audio =
3574379bc100SJani Nikula 		intel_ddi_is_audio_enabled(dev_priv, cpu_transcoder);
3575379bc100SJani Nikula 
3576379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.bpp &&
3577379bc100SJani Nikula 	    pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
3578379bc100SJani Nikula 		/*
3579379bc100SJani Nikula 		 * This is a big fat ugly hack.
3580379bc100SJani Nikula 		 *
3581379bc100SJani Nikula 		 * Some machines in UEFI boot mode provide us a VBT that has 18
3582379bc100SJani Nikula 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
3583379bc100SJani Nikula 		 * unknown we fail to light up. Yet the same BIOS boots up with
3584379bc100SJani Nikula 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
3585379bc100SJani Nikula 		 * max, not what it tells us to use.
3586379bc100SJani Nikula 		 *
3587379bc100SJani Nikula 		 * Note: This will still be broken if the eDP panel is not lit
3588379bc100SJani Nikula 		 * up by the BIOS, and thus we can't get the mode at module
3589379bc100SJani Nikula 		 * load.
3590379bc100SJani Nikula 		 */
359147bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
359247bdb1caSJani Nikula 			    "pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
3593379bc100SJani Nikula 			    pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
3594379bc100SJani Nikula 		dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
3595379bc100SJani Nikula 	}
3596379bc100SJani Nikula 
35970385eceaSManasi Navare 	if (!pipe_config->bigjoiner_slave)
3598351221ffSVille Syrjälä 		ddi_dotclock_get(pipe_config);
3599379bc100SJani Nikula 
36002446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3601379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
3602379bc100SJani Nikula 			bxt_ddi_phy_get_lane_lat_optim_mask(encoder);
3603379bc100SJani Nikula 
3604379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3605379bc100SJani Nikula 
3606379bc100SJani Nikula 	intel_hdmi_read_gcp_infoframe(encoder, pipe_config);
3607379bc100SJani Nikula 
3608379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3609379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_AVI,
3610379bc100SJani Nikula 			     &pipe_config->infoframes.avi);
3611379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3612379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_SPD,
3613379bc100SJani Nikula 			     &pipe_config->infoframes.spd);
3614379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3615379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_VENDOR,
3616379bc100SJani Nikula 			     &pipe_config->infoframes.hdmi);
3617379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3618379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_DRM,
3619379bc100SJani Nikula 			     &pipe_config->infoframes.drm);
362002d8ea47SVille Syrjälä 
3621005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 8)
3622dc5b8ed5SVille Syrjälä 		bdw_get_trans_port_sync_config(pipe_config);
3623dee66f3eSGwan-gyeong Mun 
3624dee66f3eSGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, HDMI_PACKET_TYPE_GAMUT_METADATA);
36252c3928e4SGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, DP_SDP_VSC);
362678b772e1SJosé Roberto de Souza 
362778b772e1SJosé Roberto de Souza 	intel_psr_get_config(encoder, pipe_config);
3628379bc100SJani Nikula }
3629379bc100SJani Nikula 
3630351221ffSVille Syrjälä void intel_ddi_get_clock(struct intel_encoder *encoder,
3631351221ffSVille Syrjälä 			 struct intel_crtc_state *crtc_state,
3632351221ffSVille Syrjälä 			 struct intel_shared_dpll *pll)
3633351221ffSVille Syrjälä {
3634351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3635351221ffSVille Syrjälä 	enum icl_port_dpll_id port_dpll_id = ICL_PORT_DPLL_DEFAULT;
3636351221ffSVille Syrjälä 	struct icl_port_dpll *port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
3637351221ffSVille Syrjälä 	bool pll_active;
3638351221ffSVille Syrjälä 
3639086877a1SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
3640086877a1SVille Syrjälä 		return;
3641086877a1SVille Syrjälä 
3642351221ffSVille Syrjälä 	port_dpll->pll = pll;
3643351221ffSVille Syrjälä 	pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
3644351221ffSVille Syrjälä 	drm_WARN_ON(&i915->drm, !pll_active);
3645351221ffSVille Syrjälä 
3646351221ffSVille Syrjälä 	icl_set_active_port_dpll(crtc_state, port_dpll_id);
3647351221ffSVille Syrjälä 
3648351221ffSVille Syrjälä 	crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
3649351221ffSVille Syrjälä 						     &crtc_state->dpll_hw_state);
3650351221ffSVille Syrjälä }
3651351221ffSVille Syrjälä 
3652865b73eaSMatt Roper static void dg2_ddi_get_config(struct intel_encoder *encoder,
3653865b73eaSMatt Roper 				struct intel_crtc_state *crtc_state)
3654865b73eaSMatt Roper {
3655865b73eaSMatt Roper 	intel_mpllb_readout_hw_state(encoder, &crtc_state->mpllb_state);
3656865b73eaSMatt Roper 	crtc_state->port_clock = intel_mpllb_calc_port_clock(encoder, &crtc_state->mpllb_state);
3657865b73eaSMatt Roper 
3658865b73eaSMatt Roper 	intel_ddi_get_config(encoder, crtc_state);
3659865b73eaSMatt Roper }
3660865b73eaSMatt Roper 
3661351221ffSVille Syrjälä static void adls_ddi_get_config(struct intel_encoder *encoder,
3662351221ffSVille Syrjälä 				struct intel_crtc_state *crtc_state)
3663351221ffSVille Syrjälä {
3664351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, adls_ddi_get_pll(encoder));
3665351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3666351221ffSVille Syrjälä }
3667351221ffSVille Syrjälä 
3668351221ffSVille Syrjälä static void rkl_ddi_get_config(struct intel_encoder *encoder,
3669351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3670351221ffSVille Syrjälä {
3671351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, rkl_ddi_get_pll(encoder));
3672351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3673351221ffSVille Syrjälä }
3674351221ffSVille Syrjälä 
3675351221ffSVille Syrjälä static void dg1_ddi_get_config(struct intel_encoder *encoder,
3676351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3677351221ffSVille Syrjälä {
3678351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, dg1_ddi_get_pll(encoder));
3679351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3680351221ffSVille Syrjälä }
3681351221ffSVille Syrjälä 
3682351221ffSVille Syrjälä static void icl_ddi_combo_get_config(struct intel_encoder *encoder,
3683351221ffSVille Syrjälä 				     struct intel_crtc_state *crtc_state)
3684351221ffSVille Syrjälä {
3685351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, icl_ddi_combo_get_pll(encoder));
3686351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3687351221ffSVille Syrjälä }
3688351221ffSVille Syrjälä 
3689086877a1SVille Syrjälä static void icl_ddi_tc_get_clock(struct intel_encoder *encoder,
3690086877a1SVille Syrjälä 				 struct intel_crtc_state *crtc_state,
3691086877a1SVille Syrjälä 				 struct intel_shared_dpll *pll)
3692351221ffSVille Syrjälä {
3693351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3694351221ffSVille Syrjälä 	enum icl_port_dpll_id port_dpll_id;
3695351221ffSVille Syrjälä 	struct icl_port_dpll *port_dpll;
3696351221ffSVille Syrjälä 	bool pll_active;
3697351221ffSVille Syrjälä 
3698086877a1SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
3699086877a1SVille Syrjälä 		return;
3700351221ffSVille Syrjälä 
3701351221ffSVille Syrjälä 	if (intel_get_shared_dpll_id(i915, pll) == DPLL_ID_ICL_TBTPLL)
3702351221ffSVille Syrjälä 		port_dpll_id = ICL_PORT_DPLL_DEFAULT;
3703351221ffSVille Syrjälä 	else
3704351221ffSVille Syrjälä 		port_dpll_id = ICL_PORT_DPLL_MG_PHY;
3705351221ffSVille Syrjälä 
3706351221ffSVille Syrjälä 	port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
3707351221ffSVille Syrjälä 
3708351221ffSVille Syrjälä 	port_dpll->pll = pll;
3709351221ffSVille Syrjälä 	pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
3710351221ffSVille Syrjälä 	drm_WARN_ON(&i915->drm, !pll_active);
3711351221ffSVille Syrjälä 
3712351221ffSVille Syrjälä 	icl_set_active_port_dpll(crtc_state, port_dpll_id);
3713351221ffSVille Syrjälä 
3714351221ffSVille Syrjälä 	if (intel_get_shared_dpll_id(i915, crtc_state->shared_dpll) == DPLL_ID_ICL_TBTPLL)
3715351221ffSVille Syrjälä 		crtc_state->port_clock = icl_calc_tbt_pll_link(i915, encoder->port);
3716351221ffSVille Syrjälä 	else
3717351221ffSVille Syrjälä 		crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
3718351221ffSVille Syrjälä 							     &crtc_state->dpll_hw_state);
3719086877a1SVille Syrjälä }
3720351221ffSVille Syrjälä 
3721086877a1SVille Syrjälä static void icl_ddi_tc_get_config(struct intel_encoder *encoder,
3722086877a1SVille Syrjälä 				  struct intel_crtc_state *crtc_state)
3723086877a1SVille Syrjälä {
3724086877a1SVille Syrjälä 	icl_ddi_tc_get_clock(encoder, crtc_state, icl_ddi_tc_get_pll(encoder));
3725351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3726351221ffSVille Syrjälä }
3727351221ffSVille Syrjälä 
3728351221ffSVille Syrjälä static void bxt_ddi_get_config(struct intel_encoder *encoder,
3729351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3730351221ffSVille Syrjälä {
3731351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, bxt_ddi_get_pll(encoder));
3732351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3733351221ffSVille Syrjälä }
3734351221ffSVille Syrjälä 
3735351221ffSVille Syrjälä static void skl_ddi_get_config(struct intel_encoder *encoder,
3736351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3737351221ffSVille Syrjälä {
3738351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, skl_ddi_get_pll(encoder));
3739351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3740351221ffSVille Syrjälä }
3741351221ffSVille Syrjälä 
3742351221ffSVille Syrjälä void hsw_ddi_get_config(struct intel_encoder *encoder,
3743351221ffSVille Syrjälä 			struct intel_crtc_state *crtc_state)
3744351221ffSVille Syrjälä {
3745351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, hsw_ddi_get_pll(encoder));
3746351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3747351221ffSVille Syrjälä }
3748351221ffSVille Syrjälä 
3749f9e76a6eSImre Deak static void intel_ddi_sync_state(struct intel_encoder *encoder,
3750f9e76a6eSImre Deak 				 const struct intel_crtc_state *crtc_state)
3751f9e76a6eSImre Deak {
37527194dc99SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
37537194dc99SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
37547194dc99SImre Deak 
37557194dc99SImre Deak 	if (intel_phy_is_tc(i915, phy))
37567194dc99SImre Deak 		intel_tc_port_sanitize(enc_to_dig_port(encoder));
37577194dc99SImre Deak 
37587194dc99SImre Deak 	if (crtc_state && intel_crtc_has_dp_encoder(crtc_state))
3759f9e76a6eSImre Deak 		intel_dp_sync_state(encoder, crtc_state);
3760f9e76a6eSImre Deak }
3761f9e76a6eSImre Deak 
3762b671d6efSImre Deak static bool intel_ddi_initial_fastset_check(struct intel_encoder *encoder,
3763b671d6efSImre Deak 					    struct intel_crtc_state *crtc_state)
3764b671d6efSImre Deak {
3765b671d6efSImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state))
3766b671d6efSImre Deak 		return intel_dp_initial_fastset_check(encoder, crtc_state);
3767b671d6efSImre Deak 
3768b671d6efSImre Deak 	return true;
3769b671d6efSImre Deak }
3770b671d6efSImre Deak 
3771379bc100SJani Nikula static enum intel_output_type
3772379bc100SJani Nikula intel_ddi_compute_output_type(struct intel_encoder *encoder,
3773379bc100SJani Nikula 			      struct intel_crtc_state *crtc_state,
3774379bc100SJani Nikula 			      struct drm_connector_state *conn_state)
3775379bc100SJani Nikula {
3776379bc100SJani Nikula 	switch (conn_state->connector->connector_type) {
3777379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_HDMIA:
3778379bc100SJani Nikula 		return INTEL_OUTPUT_HDMI;
3779379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_eDP:
3780379bc100SJani Nikula 		return INTEL_OUTPUT_EDP;
3781379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_DisplayPort:
3782379bc100SJani Nikula 		return INTEL_OUTPUT_DP;
3783379bc100SJani Nikula 	default:
3784379bc100SJani Nikula 		MISSING_CASE(conn_state->connector->connector_type);
3785379bc100SJani Nikula 		return INTEL_OUTPUT_UNUSED;
3786379bc100SJani Nikula 	}
3787379bc100SJani Nikula }
3788379bc100SJani Nikula 
3789379bc100SJani Nikula static int intel_ddi_compute_config(struct intel_encoder *encoder,
3790379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config,
3791379bc100SJani Nikula 				    struct drm_connector_state *conn_state)
3792379bc100SJani Nikula {
37932225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3794379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3795379bc100SJani Nikula 	enum port port = encoder->port;
3796379bc100SJani Nikula 	int ret;
3797379bc100SJani Nikula 
379810cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
3799379bc100SJani Nikula 		pipe_config->cpu_transcoder = TRANSCODER_EDP;
3800379bc100SJani Nikula 
3801bdacf087SAnshuman Gupta 	if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI)) {
3802379bc100SJani Nikula 		ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
3803bdacf087SAnshuman Gupta 	} else {
3804379bc100SJani Nikula 		ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
3805bdacf087SAnshuman Gupta 	}
3806bdacf087SAnshuman Gupta 
3807379bc100SJani Nikula 	if (ret)
3808379bc100SJani Nikula 		return ret;
3809379bc100SJani Nikula 
3810379bc100SJani Nikula 	if (IS_HASWELL(dev_priv) && crtc->pipe == PIPE_A &&
3811379bc100SJani Nikula 	    pipe_config->cpu_transcoder == TRANSCODER_EDP)
3812379bc100SJani Nikula 		pipe_config->pch_pfit.force_thru =
3813379bc100SJani Nikula 			pipe_config->pch_pfit.enabled ||
3814379bc100SJani Nikula 			pipe_config->crc_enabled;
3815379bc100SJani Nikula 
38162446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3817379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
3818379bc100SJani Nikula 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
3819379bc100SJani Nikula 
3820379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3821379bc100SJani Nikula 
3822379bc100SJani Nikula 	return 0;
3823379bc100SJani Nikula }
3824379bc100SJani Nikula 
3825b50a1aa6SManasi Navare static bool mode_equal(const struct drm_display_mode *mode1,
3826b50a1aa6SManasi Navare 		       const struct drm_display_mode *mode2)
3827b50a1aa6SManasi Navare {
3828b50a1aa6SManasi Navare 	return drm_mode_match(mode1, mode2,
3829b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_TIMINGS |
3830b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_FLAGS |
3831b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_3D_FLAGS) &&
3832b50a1aa6SManasi Navare 		mode1->clock == mode2->clock; /* we want an exact match */
3833b50a1aa6SManasi Navare }
3834b50a1aa6SManasi Navare 
3835b50a1aa6SManasi Navare static bool m_n_equal(const struct intel_link_m_n *m_n_1,
3836b50a1aa6SManasi Navare 		      const struct intel_link_m_n *m_n_2)
3837b50a1aa6SManasi Navare {
3838b50a1aa6SManasi Navare 	return m_n_1->tu == m_n_2->tu &&
3839b50a1aa6SManasi Navare 		m_n_1->gmch_m == m_n_2->gmch_m &&
3840b50a1aa6SManasi Navare 		m_n_1->gmch_n == m_n_2->gmch_n &&
3841b50a1aa6SManasi Navare 		m_n_1->link_m == m_n_2->link_m &&
3842b50a1aa6SManasi Navare 		m_n_1->link_n == m_n_2->link_n;
3843b50a1aa6SManasi Navare }
3844b50a1aa6SManasi Navare 
3845b50a1aa6SManasi Navare static bool crtcs_port_sync_compatible(const struct intel_crtc_state *crtc_state1,
3846b50a1aa6SManasi Navare 				       const struct intel_crtc_state *crtc_state2)
3847b50a1aa6SManasi Navare {
3848b50a1aa6SManasi Navare 	return crtc_state1->hw.active && crtc_state2->hw.active &&
3849b50a1aa6SManasi Navare 		crtc_state1->output_types == crtc_state2->output_types &&
3850b50a1aa6SManasi Navare 		crtc_state1->output_format == crtc_state2->output_format &&
3851b50a1aa6SManasi Navare 		crtc_state1->lane_count == crtc_state2->lane_count &&
3852b50a1aa6SManasi Navare 		crtc_state1->port_clock == crtc_state2->port_clock &&
3853b50a1aa6SManasi Navare 		mode_equal(&crtc_state1->hw.adjusted_mode,
3854b50a1aa6SManasi Navare 			   &crtc_state2->hw.adjusted_mode) &&
3855b50a1aa6SManasi Navare 		m_n_equal(&crtc_state1->dp_m_n, &crtc_state2->dp_m_n);
3856b50a1aa6SManasi Navare }
3857b50a1aa6SManasi Navare 
3858b50a1aa6SManasi Navare static u8
3859b50a1aa6SManasi Navare intel_ddi_port_sync_transcoders(const struct intel_crtc_state *ref_crtc_state,
3860b50a1aa6SManasi Navare 				int tile_group_id)
3861b50a1aa6SManasi Navare {
3862b50a1aa6SManasi Navare 	struct drm_connector *connector;
3863b50a1aa6SManasi Navare 	const struct drm_connector_state *conn_state;
3864b50a1aa6SManasi Navare 	struct drm_i915_private *dev_priv = to_i915(ref_crtc_state->uapi.crtc->dev);
3865b50a1aa6SManasi Navare 	struct intel_atomic_state *state =
3866b50a1aa6SManasi Navare 		to_intel_atomic_state(ref_crtc_state->uapi.state);
3867b50a1aa6SManasi Navare 	u8 transcoders = 0;
3868b50a1aa6SManasi Navare 	int i;
3869b50a1aa6SManasi Navare 
3870dc5b8ed5SVille Syrjälä 	/*
3871dc5b8ed5SVille Syrjälä 	 * We don't enable port sync on BDW due to missing w/as and
3872dc5b8ed5SVille Syrjälä 	 * due to not having adjusted the modeset sequence appropriately.
3873dc5b8ed5SVille Syrjälä 	 */
3874005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 9)
3875b50a1aa6SManasi Navare 		return 0;
3876b50a1aa6SManasi Navare 
3877b50a1aa6SManasi Navare 	if (!intel_crtc_has_type(ref_crtc_state, INTEL_OUTPUT_DP))
3878b50a1aa6SManasi Navare 		return 0;
3879b50a1aa6SManasi Navare 
3880b50a1aa6SManasi Navare 	for_each_new_connector_in_state(&state->base, connector, conn_state, i) {
3881b50a1aa6SManasi Navare 		struct intel_crtc *crtc = to_intel_crtc(conn_state->crtc);
3882b50a1aa6SManasi Navare 		const struct intel_crtc_state *crtc_state;
3883b50a1aa6SManasi Navare 
3884b50a1aa6SManasi Navare 		if (!crtc)
3885b50a1aa6SManasi Navare 			continue;
3886b50a1aa6SManasi Navare 
3887b50a1aa6SManasi Navare 		if (!connector->has_tile ||
3888b50a1aa6SManasi Navare 		    connector->tile_group->id !=
3889b50a1aa6SManasi Navare 		    tile_group_id)
3890b50a1aa6SManasi Navare 			continue;
3891b50a1aa6SManasi Navare 		crtc_state = intel_atomic_get_new_crtc_state(state,
3892b50a1aa6SManasi Navare 							     crtc);
3893b50a1aa6SManasi Navare 		if (!crtcs_port_sync_compatible(ref_crtc_state,
3894b50a1aa6SManasi Navare 						crtc_state))
3895b50a1aa6SManasi Navare 			continue;
3896b50a1aa6SManasi Navare 		transcoders |= BIT(crtc_state->cpu_transcoder);
3897b50a1aa6SManasi Navare 	}
3898b50a1aa6SManasi Navare 
3899b50a1aa6SManasi Navare 	return transcoders;
3900b50a1aa6SManasi Navare }
3901b50a1aa6SManasi Navare 
3902b50a1aa6SManasi Navare static int intel_ddi_compute_config_late(struct intel_encoder *encoder,
3903b50a1aa6SManasi Navare 					 struct intel_crtc_state *crtc_state,
3904b50a1aa6SManasi Navare 					 struct drm_connector_state *conn_state)
3905b50a1aa6SManasi Navare {
390647bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3907b50a1aa6SManasi Navare 	struct drm_connector *connector = conn_state->connector;
3908b50a1aa6SManasi Navare 	u8 port_sync_transcoders = 0;
3909b50a1aa6SManasi Navare 
391047bdb1caSJani Nikula 	drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] [CRTC:%d:%s]",
3911b50a1aa6SManasi Navare 		    encoder->base.base.id, encoder->base.name,
3912b50a1aa6SManasi Navare 		    crtc_state->uapi.crtc->base.id, crtc_state->uapi.crtc->name);
3913b50a1aa6SManasi Navare 
3914b50a1aa6SManasi Navare 	if (connector->has_tile)
3915b50a1aa6SManasi Navare 		port_sync_transcoders = intel_ddi_port_sync_transcoders(crtc_state,
3916b50a1aa6SManasi Navare 									connector->tile_group->id);
3917b50a1aa6SManasi Navare 
3918b50a1aa6SManasi Navare 	/*
3919b50a1aa6SManasi Navare 	 * EDP Transcoders cannot be ensalved
3920b50a1aa6SManasi Navare 	 * make them a master always when present
3921b50a1aa6SManasi Navare 	 */
3922b50a1aa6SManasi Navare 	if (port_sync_transcoders & BIT(TRANSCODER_EDP))
3923b50a1aa6SManasi Navare 		crtc_state->master_transcoder = TRANSCODER_EDP;
3924b50a1aa6SManasi Navare 	else
3925b50a1aa6SManasi Navare 		crtc_state->master_transcoder = ffs(port_sync_transcoders) - 1;
3926b50a1aa6SManasi Navare 
3927b50a1aa6SManasi Navare 	if (crtc_state->master_transcoder == crtc_state->cpu_transcoder) {
3928b50a1aa6SManasi Navare 		crtc_state->master_transcoder = INVALID_TRANSCODER;
3929b50a1aa6SManasi Navare 		crtc_state->sync_mode_slaves_mask =
3930b50a1aa6SManasi Navare 			port_sync_transcoders & ~BIT(crtc_state->cpu_transcoder);
3931b50a1aa6SManasi Navare 	}
3932b50a1aa6SManasi Navare 
3933b50a1aa6SManasi Navare 	return 0;
3934b50a1aa6SManasi Navare }
3935b50a1aa6SManasi Navare 
3936379bc100SJani Nikula static void intel_ddi_encoder_destroy(struct drm_encoder *encoder)
3937379bc100SJani Nikula {
39384a300e65SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->dev);
3939b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
39403e0abc76SImre Deak 	enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
3941379bc100SJani Nikula 
3942379bc100SJani Nikula 	intel_dp_encoder_flush_work(encoder);
39433e0abc76SImre Deak 	if (intel_phy_is_tc(i915, phy))
39443e0abc76SImre Deak 		intel_tc_port_flush_work(dig_port);
39454a300e65SImre Deak 	intel_display_power_flush_work(i915);
3946379bc100SJani Nikula 
3947379bc100SJani Nikula 	drm_encoder_cleanup(encoder);
3948a6c6eac9SAnshuman Gupta 	kfree(dig_port->hdcp_port_data.streams);
3949379bc100SJani Nikula 	kfree(dig_port);
3950379bc100SJani Nikula }
3951379bc100SJani Nikula 
3952764f6729SVille Syrjälä static void intel_ddi_encoder_reset(struct drm_encoder *encoder)
3953764f6729SVille Syrjälä {
3954764f6729SVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(encoder));
3955764f6729SVille Syrjälä 
3956764f6729SVille Syrjälä 	intel_dp->reset_link_params = true;
3957764f6729SVille Syrjälä 
3958764f6729SVille Syrjälä 	intel_pps_encoder_reset(intel_dp);
3959764f6729SVille Syrjälä }
3960764f6729SVille Syrjälä 
3961379bc100SJani Nikula static const struct drm_encoder_funcs intel_ddi_funcs = {
3962764f6729SVille Syrjälä 	.reset = intel_ddi_encoder_reset,
3963379bc100SJani Nikula 	.destroy = intel_ddi_encoder_destroy,
3964379bc100SJani Nikula };
3965379bc100SJani Nikula 
3966379bc100SJani Nikula static struct intel_connector *
39677801f3b7SLucas De Marchi intel_ddi_init_dp_connector(struct intel_digital_port *dig_port)
3968379bc100SJani Nikula {
3969379bc100SJani Nikula 	struct intel_connector *connector;
39707801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
3971379bc100SJani Nikula 
3972379bc100SJani Nikula 	connector = intel_connector_alloc();
3973379bc100SJani Nikula 	if (!connector)
3974379bc100SJani Nikula 		return NULL;
3975379bc100SJani Nikula 
39767801f3b7SLucas De Marchi 	dig_port->dp.output_reg = DDI_BUF_CTL(port);
39777801f3b7SLucas De Marchi 	dig_port->dp.prepare_link_retrain = intel_ddi_prepare_link_retrain;
39787801f3b7SLucas De Marchi 	dig_port->dp.set_link_train = intel_ddi_set_link_train;
39797801f3b7SLucas De Marchi 	dig_port->dp.set_idle_link_train = intel_ddi_set_idle_link_train;
3980eee3f911SVille Syrjälä 
39817801f3b7SLucas De Marchi 	dig_port->dp.voltage_max = intel_ddi_dp_voltage_max;
39827801f3b7SLucas De Marchi 	dig_port->dp.preemph_max = intel_ddi_dp_preemph_max;
398353de0a20SVille Syrjälä 
39847801f3b7SLucas De Marchi 	if (!intel_dp_init_connector(dig_port, connector)) {
3985379bc100SJani Nikula 		kfree(connector);
3986379bc100SJani Nikula 		return NULL;
3987379bc100SJani Nikula 	}
3988379bc100SJani Nikula 
3989379bc100SJani Nikula 	return connector;
3990379bc100SJani Nikula }
3991379bc100SJani Nikula 
3992379bc100SJani Nikula static int modeset_pipe(struct drm_crtc *crtc,
3993379bc100SJani Nikula 			struct drm_modeset_acquire_ctx *ctx)
3994379bc100SJani Nikula {
3995379bc100SJani Nikula 	struct drm_atomic_state *state;
3996379bc100SJani Nikula 	struct drm_crtc_state *crtc_state;
3997379bc100SJani Nikula 	int ret;
3998379bc100SJani Nikula 
3999379bc100SJani Nikula 	state = drm_atomic_state_alloc(crtc->dev);
4000379bc100SJani Nikula 	if (!state)
4001379bc100SJani Nikula 		return -ENOMEM;
4002379bc100SJani Nikula 
4003379bc100SJani Nikula 	state->acquire_ctx = ctx;
4004379bc100SJani Nikula 
4005379bc100SJani Nikula 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
4006379bc100SJani Nikula 	if (IS_ERR(crtc_state)) {
4007379bc100SJani Nikula 		ret = PTR_ERR(crtc_state);
4008379bc100SJani Nikula 		goto out;
4009379bc100SJani Nikula 	}
4010379bc100SJani Nikula 
4011379bc100SJani Nikula 	crtc_state->connectors_changed = true;
4012379bc100SJani Nikula 
4013379bc100SJani Nikula 	ret = drm_atomic_commit(state);
4014379bc100SJani Nikula out:
4015379bc100SJani Nikula 	drm_atomic_state_put(state);
4016379bc100SJani Nikula 
4017379bc100SJani Nikula 	return ret;
4018379bc100SJani Nikula }
4019379bc100SJani Nikula 
4020379bc100SJani Nikula static int intel_hdmi_reset_link(struct intel_encoder *encoder,
4021379bc100SJani Nikula 				 struct drm_modeset_acquire_ctx *ctx)
4022379bc100SJani Nikula {
4023379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4024b7d02c3aSVille Syrjälä 	struct intel_hdmi *hdmi = enc_to_intel_hdmi(encoder);
4025379bc100SJani Nikula 	struct intel_connector *connector = hdmi->attached_connector;
4026379bc100SJani Nikula 	struct i2c_adapter *adapter =
4027379bc100SJani Nikula 		intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
4028379bc100SJani Nikula 	struct drm_connector_state *conn_state;
4029379bc100SJani Nikula 	struct intel_crtc_state *crtc_state;
4030379bc100SJani Nikula 	struct intel_crtc *crtc;
4031379bc100SJani Nikula 	u8 config;
4032379bc100SJani Nikula 	int ret;
4033379bc100SJani Nikula 
4034379bc100SJani Nikula 	if (!connector || connector->base.status != connector_status_connected)
4035379bc100SJani Nikula 		return 0;
4036379bc100SJani Nikula 
4037379bc100SJani Nikula 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4038379bc100SJani Nikula 			       ctx);
4039379bc100SJani Nikula 	if (ret)
4040379bc100SJani Nikula 		return ret;
4041379bc100SJani Nikula 
4042379bc100SJani Nikula 	conn_state = connector->base.state;
4043379bc100SJani Nikula 
4044379bc100SJani Nikula 	crtc = to_intel_crtc(conn_state->crtc);
4045379bc100SJani Nikula 	if (!crtc)
4046379bc100SJani Nikula 		return 0;
4047379bc100SJani Nikula 
4048379bc100SJani Nikula 	ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4049379bc100SJani Nikula 	if (ret)
4050379bc100SJani Nikula 		return ret;
4051379bc100SJani Nikula 
4052379bc100SJani Nikula 	crtc_state = to_intel_crtc_state(crtc->base.state);
4053379bc100SJani Nikula 
40541de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm,
40551de143ccSPankaj Bharadiya 		    !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI));
4056379bc100SJani Nikula 
40571326a92cSMaarten Lankhorst 	if (!crtc_state->hw.active)
4058379bc100SJani Nikula 		return 0;
4059379bc100SJani Nikula 
4060379bc100SJani Nikula 	if (!crtc_state->hdmi_high_tmds_clock_ratio &&
4061379bc100SJani Nikula 	    !crtc_state->hdmi_scrambling)
4062379bc100SJani Nikula 		return 0;
4063379bc100SJani Nikula 
4064379bc100SJani Nikula 	if (conn_state->commit &&
4065379bc100SJani Nikula 	    !try_wait_for_completion(&conn_state->commit->hw_done))
4066379bc100SJani Nikula 		return 0;
4067379bc100SJani Nikula 
4068379bc100SJani Nikula 	ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
4069379bc100SJani Nikula 	if (ret < 0) {
407047bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Failed to read TMDS config: %d\n",
407147bdb1caSJani Nikula 			ret);
4072379bc100SJani Nikula 		return 0;
4073379bc100SJani Nikula 	}
4074379bc100SJani Nikula 
4075379bc100SJani Nikula 	if (!!(config & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40) ==
4076379bc100SJani Nikula 	    crtc_state->hdmi_high_tmds_clock_ratio &&
4077379bc100SJani Nikula 	    !!(config & SCDC_SCRAMBLING_ENABLE) ==
4078379bc100SJani Nikula 	    crtc_state->hdmi_scrambling)
4079379bc100SJani Nikula 		return 0;
4080379bc100SJani Nikula 
4081379bc100SJani Nikula 	/*
4082379bc100SJani Nikula 	 * HDMI 2.0 says that one should not send scrambled data
4083379bc100SJani Nikula 	 * prior to configuring the sink scrambling, and that
4084379bc100SJani Nikula 	 * TMDS clock/data transmission should be suspended when
4085379bc100SJani Nikula 	 * changing the TMDS clock rate in the sink. So let's
4086379bc100SJani Nikula 	 * just do a full modeset here, even though some sinks
4087379bc100SJani Nikula 	 * would be perfectly happy if were to just reconfigure
4088379bc100SJani Nikula 	 * the SCDC settings on the fly.
4089379bc100SJani Nikula 	 */
4090379bc100SJani Nikula 	return modeset_pipe(&crtc->base, ctx);
4091379bc100SJani Nikula }
4092379bc100SJani Nikula 
40933944709dSImre Deak static enum intel_hotplug_state
40943944709dSImre Deak intel_ddi_hotplug(struct intel_encoder *encoder,
40958c8919c7SImre Deak 		  struct intel_connector *connector)
4096379bc100SJani Nikula {
4097b4df5405SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4098b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4099699390f7SVille Syrjälä 	struct intel_dp *intel_dp = &dig_port->dp;
4100b4df5405SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4101b4df5405SImre Deak 	bool is_tc = intel_phy_is_tc(i915, phy);
4102379bc100SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
41033944709dSImre Deak 	enum intel_hotplug_state state;
4104379bc100SJani Nikula 	int ret;
4105379bc100SJani Nikula 
4106699390f7SVille Syrjälä 	if (intel_dp->compliance.test_active &&
4107699390f7SVille Syrjälä 	    intel_dp->compliance.test_type == DP_TEST_LINK_PHY_TEST_PATTERN) {
4108699390f7SVille Syrjälä 		intel_dp_phy_test(encoder);
4109699390f7SVille Syrjälä 		/* just do the PHY test and nothing else */
4110699390f7SVille Syrjälä 		return INTEL_HOTPLUG_UNCHANGED;
4111699390f7SVille Syrjälä 	}
4112699390f7SVille Syrjälä 
41138c8919c7SImre Deak 	state = intel_encoder_hotplug(encoder, connector);
4114379bc100SJani Nikula 
4115379bc100SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
4116379bc100SJani Nikula 
4117379bc100SJani Nikula 	for (;;) {
4118379bc100SJani Nikula 		if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA)
4119379bc100SJani Nikula 			ret = intel_hdmi_reset_link(encoder, &ctx);
4120379bc100SJani Nikula 		else
4121379bc100SJani Nikula 			ret = intel_dp_retrain_link(encoder, &ctx);
4122379bc100SJani Nikula 
4123379bc100SJani Nikula 		if (ret == -EDEADLK) {
4124379bc100SJani Nikula 			drm_modeset_backoff(&ctx);
4125379bc100SJani Nikula 			continue;
4126379bc100SJani Nikula 		}
4127379bc100SJani Nikula 
4128379bc100SJani Nikula 		break;
4129379bc100SJani Nikula 	}
4130379bc100SJani Nikula 
4131379bc100SJani Nikula 	drm_modeset_drop_locks(&ctx);
4132379bc100SJani Nikula 	drm_modeset_acquire_fini(&ctx);
41333a47ae20SPankaj Bharadiya 	drm_WARN(encoder->base.dev, ret,
41343a47ae20SPankaj Bharadiya 		 "Acquiring modeset locks failed with %i\n", ret);
4135379bc100SJani Nikula 
4136bb80c925SJosé Roberto de Souza 	/*
4137bb80c925SJosé Roberto de Souza 	 * Unpowered type-c dongles can take some time to boot and be
4138bb80c925SJosé Roberto de Souza 	 * responsible, so here giving some time to those dongles to power up
4139bb80c925SJosé Roberto de Souza 	 * and then retrying the probe.
4140bb80c925SJosé Roberto de Souza 	 *
4141bb80c925SJosé Roberto de Souza 	 * On many platforms the HDMI live state signal is known to be
4142bb80c925SJosé Roberto de Souza 	 * unreliable, so we can't use it to detect if a sink is connected or
4143bb80c925SJosé Roberto de Souza 	 * not. Instead we detect if it's connected based on whether we can
4144bb80c925SJosé Roberto de Souza 	 * read the EDID or not. That in turn has a problem during disconnect,
4145bb80c925SJosé Roberto de Souza 	 * since the HPD interrupt may be raised before the DDC lines get
4146bb80c925SJosé Roberto de Souza 	 * disconnected (due to how the required length of DDC vs. HPD
4147bb80c925SJosé Roberto de Souza 	 * connector pins are specified) and so we'll still be able to get a
4148bb80c925SJosé Roberto de Souza 	 * valid EDID. To solve this schedule another detection cycle if this
4149bb80c925SJosé Roberto de Souza 	 * time around we didn't detect any change in the sink's connection
4150bb80c925SJosé Roberto de Souza 	 * status.
4151b4df5405SImre Deak 	 *
4152b4df5405SImre Deak 	 * Type-c connectors which get their HPD signal deasserted then
4153b4df5405SImre Deak 	 * reasserted, without unplugging/replugging the sink from the
4154b4df5405SImre Deak 	 * connector, introduce a delay until the AUX channel communication
4155b4df5405SImre Deak 	 * becomes functional. Retry the detection for 5 seconds on type-c
4156b4df5405SImre Deak 	 * connectors to account for this delay.
4157bb80c925SJosé Roberto de Souza 	 */
4158b4df5405SImre Deak 	if (state == INTEL_HOTPLUG_UNCHANGED &&
4159b4df5405SImre Deak 	    connector->hotplug_retries < (is_tc ? 5 : 1) &&
4160bb80c925SJosé Roberto de Souza 	    !dig_port->dp.is_mst)
4161bb80c925SJosé Roberto de Souza 		state = INTEL_HOTPLUG_RETRY;
4162bb80c925SJosé Roberto de Souza 
41633944709dSImre Deak 	return state;
4164379bc100SJani Nikula }
4165379bc100SJani Nikula 
4166edc0e09cSVille Syrjälä static bool lpt_digital_port_connected(struct intel_encoder *encoder)
4167edc0e09cSVille Syrjälä {
4168edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4169c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.pch_hpd[encoder->hpd_pin];
4170edc0e09cSVille Syrjälä 
4171edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, SDEISR) & bit;
4172edc0e09cSVille Syrjälä }
4173edc0e09cSVille Syrjälä 
4174edc0e09cSVille Syrjälä static bool hsw_digital_port_connected(struct intel_encoder *encoder)
4175edc0e09cSVille Syrjälä {
4176edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4177c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4178edc0e09cSVille Syrjälä 
4179c7e8a3d6SVille Syrjälä 	return intel_de_read(dev_priv, DEISR) & bit;
4180edc0e09cSVille Syrjälä }
4181edc0e09cSVille Syrjälä 
4182edc0e09cSVille Syrjälä static bool bdw_digital_port_connected(struct intel_encoder *encoder)
4183edc0e09cSVille Syrjälä {
4184edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4185c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4186edc0e09cSVille Syrjälä 
4187edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, GEN8_DE_PORT_ISR) & bit;
4188edc0e09cSVille Syrjälä }
4189edc0e09cSVille Syrjälä 
4190379bc100SJani Nikula static struct intel_connector *
41917801f3b7SLucas De Marchi intel_ddi_init_hdmi_connector(struct intel_digital_port *dig_port)
4192379bc100SJani Nikula {
4193379bc100SJani Nikula 	struct intel_connector *connector;
41947801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4195379bc100SJani Nikula 
4196379bc100SJani Nikula 	connector = intel_connector_alloc();
4197379bc100SJani Nikula 	if (!connector)
4198379bc100SJani Nikula 		return NULL;
4199379bc100SJani Nikula 
42007801f3b7SLucas De Marchi 	dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
42017801f3b7SLucas De Marchi 	intel_hdmi_init_connector(dig_port, connector);
4202379bc100SJani Nikula 
4203379bc100SJani Nikula 	return connector;
4204379bc100SJani Nikula }
4205379bc100SJani Nikula 
42067801f3b7SLucas De Marchi static bool intel_ddi_a_force_4_lanes(struct intel_digital_port *dig_port)
4207379bc100SJani Nikula {
42087801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4209379bc100SJani Nikula 
42107801f3b7SLucas De Marchi 	if (dig_port->base.port != PORT_A)
4211379bc100SJani Nikula 		return false;
4212379bc100SJani Nikula 
42137801f3b7SLucas De Marchi 	if (dig_port->saved_port_bits & DDI_A_4_LANES)
4214379bc100SJani Nikula 		return false;
4215379bc100SJani Nikula 
4216379bc100SJani Nikula 	/* Broxton/Geminilake: Bspec says that DDI_A_4_LANES is the only
4217379bc100SJani Nikula 	 *                     supported configuration
4218379bc100SJani Nikula 	 */
42192446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
4220379bc100SJani Nikula 		return true;
4221379bc100SJani Nikula 
4222379bc100SJani Nikula 	return false;
4223379bc100SJani Nikula }
4224379bc100SJani Nikula 
4225379bc100SJani Nikula static int
42267801f3b7SLucas De Marchi intel_ddi_max_lanes(struct intel_digital_port *dig_port)
4227379bc100SJani Nikula {
42287801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
42297801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4230379bc100SJani Nikula 	int max_lanes = 4;
4231379bc100SJani Nikula 
4232005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
4233379bc100SJani Nikula 		return max_lanes;
4234379bc100SJani Nikula 
4235379bc100SJani Nikula 	if (port == PORT_A || port == PORT_E) {
4236f7960e7fSJani Nikula 		if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
4237379bc100SJani Nikula 			max_lanes = port == PORT_A ? 4 : 0;
4238379bc100SJani Nikula 		else
4239379bc100SJani Nikula 			/* Both A and E share 2 lanes */
4240379bc100SJani Nikula 			max_lanes = 2;
4241379bc100SJani Nikula 	}
4242379bc100SJani Nikula 
4243379bc100SJani Nikula 	/*
4244379bc100SJani Nikula 	 * Some BIOS might fail to set this bit on port A if eDP
4245379bc100SJani Nikula 	 * wasn't lit up at boot.  Force this bit set when needed
4246379bc100SJani Nikula 	 * so we use the proper lane count for our calculations.
4247379bc100SJani Nikula 	 */
42487801f3b7SLucas De Marchi 	if (intel_ddi_a_force_4_lanes(dig_port)) {
424947bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
425047bdb1caSJani Nikula 			    "Forcing DDI_A_4_LANES for port A\n");
42517801f3b7SLucas De Marchi 		dig_port->saved_port_bits |= DDI_A_4_LANES;
4252379bc100SJani Nikula 		max_lanes = 4;
4253379bc100SJani Nikula 	}
4254379bc100SJani Nikula 
4255379bc100SJani Nikula 	return max_lanes;
4256379bc100SJani Nikula }
4257379bc100SJani Nikula 
4258ddff9a60SMatt Roper static bool hti_uses_phy(struct drm_i915_private *i915, enum phy phy)
4259ddff9a60SMatt Roper {
4260ddff9a60SMatt Roper 	return i915->hti_state & HDPORT_ENABLED &&
4261ff7fb44dSJosé Roberto de Souza 	       i915->hti_state & HDPORT_DDI_USED(phy);
4262ddff9a60SMatt Roper }
4263ddff9a60SMatt Roper 
4264ed2615a8SMatt Roper static enum hpd_pin xelpd_hpd_pin(struct drm_i915_private *dev_priv,
4265ed2615a8SMatt Roper 				  enum port port)
4266ed2615a8SMatt Roper {
4267ed2615a8SMatt Roper 	if (port >= PORT_D_XELPD)
4268ed2615a8SMatt Roper 		return HPD_PORT_D + port - PORT_D_XELPD;
4269ed2615a8SMatt Roper 	else if (port >= PORT_TC1)
4270ed2615a8SMatt Roper 		return HPD_PORT_TC1 + port - PORT_TC1;
4271ed2615a8SMatt Roper 	else
4272ed2615a8SMatt Roper 		return HPD_PORT_A + port - PORT_A;
4273ed2615a8SMatt Roper }
4274ed2615a8SMatt Roper 
4275229f31e2SLucas De Marchi static enum hpd_pin dg1_hpd_pin(struct drm_i915_private *dev_priv,
4276229f31e2SLucas De Marchi 				enum port port)
4277229f31e2SLucas De Marchi {
42781d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
42791d8ca002SVille Syrjälä 		return HPD_PORT_C + port - PORT_TC1;
4280229f31e2SLucas De Marchi 	else
4281229f31e2SLucas De Marchi 		return HPD_PORT_A + port - PORT_A;
4282229f31e2SLucas De Marchi }
4283229f31e2SLucas De Marchi 
4284da51e4baSVille Syrjälä static enum hpd_pin tgl_hpd_pin(struct drm_i915_private *dev_priv,
4285da51e4baSVille Syrjälä 				enum port port)
4286da51e4baSVille Syrjälä {
42871d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
42881d8ca002SVille Syrjälä 		return HPD_PORT_TC1 + port - PORT_TC1;
4289da51e4baSVille Syrjälä 	else
4290da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4291da51e4baSVille Syrjälä }
4292da51e4baSVille Syrjälä 
4293da51e4baSVille Syrjälä static enum hpd_pin rkl_hpd_pin(struct drm_i915_private *dev_priv,
4294da51e4baSVille Syrjälä 				enum port port)
4295da51e4baSVille Syrjälä {
4296da51e4baSVille Syrjälä 	if (HAS_PCH_TGP(dev_priv))
4297da51e4baSVille Syrjälä 		return tgl_hpd_pin(dev_priv, port);
4298da51e4baSVille Syrjälä 
42991d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
43001d8ca002SVille Syrjälä 		return HPD_PORT_C + port - PORT_TC1;
4301da51e4baSVille Syrjälä 	else
4302da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4303da51e4baSVille Syrjälä }
4304da51e4baSVille Syrjälä 
4305da51e4baSVille Syrjälä static enum hpd_pin icl_hpd_pin(struct drm_i915_private *dev_priv,
4306da51e4baSVille Syrjälä 				enum port port)
4307da51e4baSVille Syrjälä {
4308da51e4baSVille Syrjälä 	if (port >= PORT_C)
4309da51e4baSVille Syrjälä 		return HPD_PORT_TC1 + port - PORT_C;
4310da51e4baSVille Syrjälä 	else
4311da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4312da51e4baSVille Syrjälä }
4313da51e4baSVille Syrjälä 
4314da51e4baSVille Syrjälä static enum hpd_pin ehl_hpd_pin(struct drm_i915_private *dev_priv,
4315da51e4baSVille Syrjälä 				enum port port)
4316da51e4baSVille Syrjälä {
4317da51e4baSVille Syrjälä 	if (port == PORT_D)
4318da51e4baSVille Syrjälä 		return HPD_PORT_A;
4319da51e4baSVille Syrjälä 
4320da51e4baSVille Syrjälä 	if (HAS_PCH_MCC(dev_priv))
4321da51e4baSVille Syrjälä 		return icl_hpd_pin(dev_priv, port);
4322da51e4baSVille Syrjälä 
4323da51e4baSVille Syrjälä 	return HPD_PORT_A + port - PORT_A;
4324da51e4baSVille Syrjälä }
4325da51e4baSVille Syrjälä 
4326c8455098SLyude Paul static enum hpd_pin skl_hpd_pin(struct drm_i915_private *dev_priv, enum port port)
4327c8455098SLyude Paul {
4328c8455098SLyude Paul 	if (HAS_PCH_TGP(dev_priv))
4329c8455098SLyude Paul 		return icl_hpd_pin(dev_priv, port);
4330c8455098SLyude Paul 
4331c8455098SLyude Paul 	return HPD_PORT_A + port - PORT_A;
4332c8455098SLyude Paul }
4333c8455098SLyude Paul 
433436ecb0ecSVille Syrjälä static bool intel_ddi_is_tc(struct drm_i915_private *i915, enum port port)
433536ecb0ecSVille Syrjälä {
4336005e9537SMatt Roper 	if (DISPLAY_VER(i915) >= 12)
433736ecb0ecSVille Syrjälä 		return port >= PORT_TC1;
4338005e9537SMatt Roper 	else if (DISPLAY_VER(i915) >= 11)
433936ecb0ecSVille Syrjälä 		return port >= PORT_C;
434036ecb0ecSVille Syrjälä 	else
434136ecb0ecSVille Syrjälä 		return false;
434236ecb0ecSVille Syrjälä }
434336ecb0ecSVille Syrjälä 
4344151ec347SImre Deak static void intel_ddi_encoder_suspend(struct intel_encoder *encoder)
4345151ec347SImre Deak {
4346151ec347SImre Deak 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4347151ec347SImre Deak 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4348151ec347SImre Deak 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4349151ec347SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4350151ec347SImre Deak 
4351151ec347SImre Deak 	intel_dp_encoder_suspend(encoder);
4352151ec347SImre Deak 
4353151ec347SImre Deak 	if (!intel_phy_is_tc(i915, phy))
4354151ec347SImre Deak 		return;
4355151ec347SImre Deak 
43563e0abc76SImre Deak 	intel_tc_port_flush_work(dig_port);
4357151ec347SImre Deak }
4358151ec347SImre Deak 
4359151ec347SImre Deak static void intel_ddi_encoder_shutdown(struct intel_encoder *encoder)
4360151ec347SImre Deak {
4361151ec347SImre Deak 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4362151ec347SImre Deak 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4363151ec347SImre Deak 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4364151ec347SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4365151ec347SImre Deak 
4366151ec347SImre Deak 	intel_dp_encoder_shutdown(encoder);
4367151ec347SImre Deak 
4368151ec347SImre Deak 	if (!intel_phy_is_tc(i915, phy))
4369151ec347SImre Deak 		return;
4370151ec347SImre Deak 
43713e0abc76SImre Deak 	intel_tc_port_flush_work(dig_port);
4372151ec347SImre Deak }
4373151ec347SImre Deak 
437483566d13SVille Syrjälä #define port_tc_name(port) ((port) - PORT_TC1 + '1')
437583566d13SVille Syrjälä #define tc_port_name(tc_port) ((tc_port) - TC_PORT_1 + '1')
437683566d13SVille Syrjälä 
4377379bc100SJani Nikula void intel_ddi_init(struct drm_i915_private *dev_priv, enum port port)
4378379bc100SJani Nikula {
43797801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port;
438070dfbc29SLucas De Marchi 	struct intel_encoder *encoder;
438145c0673aSJani Nikula 	const struct intel_bios_encoder_data *devdata;
4382f542d671SKai-Heng Feng 	bool init_hdmi, init_dp;
4383d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
4384379bc100SJani Nikula 
4385ddff9a60SMatt Roper 	/*
4386ddff9a60SMatt Roper 	 * On platforms with HTI (aka HDPORT), if it's enabled at boot it may
4387ddff9a60SMatt Roper 	 * have taken over some of the PHYs and made them unavailable to the
4388ddff9a60SMatt Roper 	 * driver.  In that case we should skip initializing the corresponding
4389ddff9a60SMatt Roper 	 * outputs.
4390ddff9a60SMatt Roper 	 */
4391ddff9a60SMatt Roper 	if (hti_uses_phy(dev_priv, phy)) {
4392ddff9a60SMatt Roper 		drm_dbg_kms(&dev_priv->drm, "PORT %c / PHY %c reserved by HTI\n",
4393ddff9a60SMatt Roper 			    port_name(port), phy_name(phy));
4394ddff9a60SMatt Roper 		return;
4395ddff9a60SMatt Roper 	}
4396ddff9a60SMatt Roper 
439745c0673aSJani Nikula 	devdata = intel_bios_encoder_data_lookup(dev_priv, port);
439845c0673aSJani Nikula 	if (!devdata) {
439945c0673aSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
440045c0673aSJani Nikula 			    "VBT says port %c is not present\n",
440145c0673aSJani Nikula 			    port_name(port));
440245c0673aSJani Nikula 		return;
440345c0673aSJani Nikula 	}
440445c0673aSJani Nikula 
440545c0673aSJani Nikula 	init_hdmi = intel_bios_encoder_supports_dvi(devdata) ||
440645c0673aSJani Nikula 		intel_bios_encoder_supports_hdmi(devdata);
440745c0673aSJani Nikula 	init_dp = intel_bios_encoder_supports_dp(devdata);
4408379bc100SJani Nikula 
4409379bc100SJani Nikula 	if (intel_bios_is_lspcon_present(dev_priv, port)) {
4410379bc100SJani Nikula 		/*
4411379bc100SJani Nikula 		 * Lspcon device needs to be driven with DP connector
4412379bc100SJani Nikula 		 * with special detection sequence. So make sure DP
4413379bc100SJani Nikula 		 * is initialized before lspcon.
4414379bc100SJani Nikula 		 */
4415379bc100SJani Nikula 		init_dp = true;
4416379bc100SJani Nikula 		init_hdmi = false;
441747bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm, "VBT says port %c has lspcon\n",
441847bdb1caSJani Nikula 			    port_name(port));
4419379bc100SJani Nikula 	}
4420379bc100SJani Nikula 
4421379bc100SJani Nikula 	if (!init_dp && !init_hdmi) {
442247bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
442347bdb1caSJani Nikula 			    "VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
4424379bc100SJani Nikula 			    port_name(port));
4425379bc100SJani Nikula 		return;
4426379bc100SJani Nikula 	}
4427379bc100SJani Nikula 
44287801f3b7SLucas De Marchi 	dig_port = kzalloc(sizeof(*dig_port), GFP_KERNEL);
44297801f3b7SLucas De Marchi 	if (!dig_port)
4430379bc100SJani Nikula 		return;
4431379bc100SJani Nikula 
44327801f3b7SLucas De Marchi 	encoder = &dig_port->base;
4433c0a950d1SJani Nikula 	encoder->devdata = devdata;
4434379bc100SJani Nikula 
4435ed2615a8SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 13 && port >= PORT_D_XELPD) {
4436ed2615a8SMatt Roper 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
4437ed2615a8SMatt Roper 				 DRM_MODE_ENCODER_TMDS,
4438ed2615a8SMatt Roper 				 "DDI %c/PHY %c",
4439ed2615a8SMatt Roper 				 port_name(port - PORT_D_XELPD + PORT_D),
4440ed2615a8SMatt Roper 				 phy_name(phy));
4441ed2615a8SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 12) {
44422d709a5aSVille Syrjälä 		enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
44432d709a5aSVille Syrjälä 
444470dfbc29SLucas De Marchi 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
44452d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
44462d709a5aSVille Syrjälä 				 "DDI %s%c/PHY %s%c",
44472d709a5aSVille Syrjälä 				 port >= PORT_TC1 ? "TC" : "",
444883566d13SVille Syrjälä 				 port >= PORT_TC1 ? port_tc_name(port) : port_name(port),
44492d709a5aSVille Syrjälä 				 tc_port != TC_PORT_NONE ? "TC" : "",
445083566d13SVille Syrjälä 				 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
4451005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 11) {
44522d709a5aSVille Syrjälä 		enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
44532d709a5aSVille Syrjälä 
44542d709a5aSVille Syrjälä 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
44552d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
44562d709a5aSVille Syrjälä 				 "DDI %c%s/PHY %s%c",
44572d709a5aSVille Syrjälä 				 port_name(port),
44582d709a5aSVille Syrjälä 				 port >= PORT_C ? " (TC)" : "",
44592d709a5aSVille Syrjälä 				 tc_port != TC_PORT_NONE ? "TC" : "",
446083566d13SVille Syrjälä 				 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
44612d709a5aSVille Syrjälä 	} else {
44622d709a5aSVille Syrjälä 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
44632d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
44642d709a5aSVille Syrjälä 				 "DDI %c/PHY %c", port_name(port),  phy_name(phy));
44652d709a5aSVille Syrjälä 	}
4466379bc100SJani Nikula 
446736e5e704SSean Paul 	mutex_init(&dig_port->hdcp_mutex);
446836e5e704SSean Paul 	dig_port->num_hdcp_streams = 0;
446936e5e704SSean Paul 
447070dfbc29SLucas De Marchi 	encoder->hotplug = intel_ddi_hotplug;
447170dfbc29SLucas De Marchi 	encoder->compute_output_type = intel_ddi_compute_output_type;
447270dfbc29SLucas De Marchi 	encoder->compute_config = intel_ddi_compute_config;
4473b50a1aa6SManasi Navare 	encoder->compute_config_late = intel_ddi_compute_config_late;
447470dfbc29SLucas De Marchi 	encoder->enable = intel_enable_ddi;
447570dfbc29SLucas De Marchi 	encoder->pre_pll_enable = intel_ddi_pre_pll_enable;
447670dfbc29SLucas De Marchi 	encoder->pre_enable = intel_ddi_pre_enable;
447784030adbSJosé Roberto de Souza 	encoder->pre_disable = intel_pre_disable_ddi;
447870dfbc29SLucas De Marchi 	encoder->disable = intel_disable_ddi;
447970dfbc29SLucas De Marchi 	encoder->post_disable = intel_ddi_post_disable;
448070dfbc29SLucas De Marchi 	encoder->update_pipe = intel_ddi_update_pipe;
448170dfbc29SLucas De Marchi 	encoder->get_hw_state = intel_ddi_get_hw_state;
4482f9e76a6eSImre Deak 	encoder->sync_state = intel_ddi_sync_state;
4483b671d6efSImre Deak 	encoder->initial_fastset_check = intel_ddi_initial_fastset_check;
4484151ec347SImre Deak 	encoder->suspend = intel_ddi_encoder_suspend;
4485151ec347SImre Deak 	encoder->shutdown = intel_ddi_encoder_shutdown;
448670dfbc29SLucas De Marchi 	encoder->get_power_domains = intel_ddi_get_power_domains;
448770dfbc29SLucas De Marchi 
448870dfbc29SLucas De Marchi 	encoder->type = INTEL_OUTPUT_DDI;
448970dfbc29SLucas De Marchi 	encoder->power_domain = intel_port_to_power_domain(port);
449070dfbc29SLucas De Marchi 	encoder->port = port;
449170dfbc29SLucas De Marchi 	encoder->cloneable = 0;
449270dfbc29SLucas De Marchi 	encoder->pipe_mask = ~0;
4493da51e4baSVille Syrjälä 
4494865b73eaSMatt Roper 	if (IS_DG2(dev_priv)) {
4495f82f2563SMatt Roper 		encoder->enable_clock = intel_mpllb_enable;
4496f82f2563SMatt Roper 		encoder->disable_clock = intel_mpllb_disable;
4497865b73eaSMatt Roper 		encoder->get_config = dg2_ddi_get_config;
4498865b73eaSMatt Roper 	} else if (IS_ALDERLAKE_S(dev_priv)) {
449940b316d4SVille Syrjälä 		encoder->enable_clock = adls_ddi_enable_clock;
450040b316d4SVille Syrjälä 		encoder->disable_clock = adls_ddi_disable_clock;
45010fbd8694SVille Syrjälä 		encoder->is_clock_enabled = adls_ddi_is_clock_enabled;
4502351221ffSVille Syrjälä 		encoder->get_config = adls_ddi_get_config;
450340b316d4SVille Syrjälä 	} else if (IS_ROCKETLAKE(dev_priv)) {
450440b316d4SVille Syrjälä 		encoder->enable_clock = rkl_ddi_enable_clock;
450540b316d4SVille Syrjälä 		encoder->disable_clock = rkl_ddi_disable_clock;
45060fbd8694SVille Syrjälä 		encoder->is_clock_enabled = rkl_ddi_is_clock_enabled;
4507351221ffSVille Syrjälä 		encoder->get_config = rkl_ddi_get_config;
450836ecb0ecSVille Syrjälä 	} else if (IS_DG1(dev_priv)) {
450935bb6b1aSVille Syrjälä 		encoder->enable_clock = dg1_ddi_enable_clock;
451035bb6b1aSVille Syrjälä 		encoder->disable_clock = dg1_ddi_disable_clock;
45110fbd8694SVille Syrjälä 		encoder->is_clock_enabled = dg1_ddi_is_clock_enabled;
4512351221ffSVille Syrjälä 		encoder->get_config = dg1_ddi_get_config;
451336ecb0ecSVille Syrjälä 	} else if (IS_JSL_EHL(dev_priv)) {
451436ecb0ecSVille Syrjälä 		if (intel_ddi_is_tc(dev_priv, port)) {
451536ecb0ecSVille Syrjälä 			encoder->enable_clock = jsl_ddi_tc_enable_clock;
451636ecb0ecSVille Syrjälä 			encoder->disable_clock = jsl_ddi_tc_disable_clock;
45170fbd8694SVille Syrjälä 			encoder->is_clock_enabled = jsl_ddi_tc_is_clock_enabled;
4518351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
451936ecb0ecSVille Syrjälä 		} else {
452036ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_combo_enable_clock;
452136ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_combo_disable_clock;
45220fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
4523351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
452436ecb0ecSVille Syrjälä 		}
4525005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 11) {
452636ecb0ecSVille Syrjälä 		if (intel_ddi_is_tc(dev_priv, port)) {
452736ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_tc_enable_clock;
452836ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_tc_disable_clock;
45290fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_tc_is_clock_enabled;
4530351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_tc_get_config;
453136ecb0ecSVille Syrjälä 		} else {
453236ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_combo_enable_clock;
453336ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_combo_disable_clock;
45340fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
4535351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
453636ecb0ecSVille Syrjälä 		}
45372446e1d6SMatt Roper 	} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
4538351221ffSVille Syrjälä 		/* BXT/GLK have fixed PLL->port mapping */
4539351221ffSVille Syrjälä 		encoder->get_config = bxt_ddi_get_config;
454093e7e61eSLucas De Marchi 	} else if (DISPLAY_VER(dev_priv) == 9) {
454138e31f1aSVille Syrjälä 		encoder->enable_clock = skl_ddi_enable_clock;
454238e31f1aSVille Syrjälä 		encoder->disable_clock = skl_ddi_disable_clock;
45430fbd8694SVille Syrjälä 		encoder->is_clock_enabled = skl_ddi_is_clock_enabled;
4544351221ffSVille Syrjälä 		encoder->get_config = skl_ddi_get_config;
454538e31f1aSVille Syrjälä 	} else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
4546d135368dSVille Syrjälä 		encoder->enable_clock = hsw_ddi_enable_clock;
4547d135368dSVille Syrjälä 		encoder->disable_clock = hsw_ddi_disable_clock;
45480fbd8694SVille Syrjälä 		encoder->is_clock_enabled = hsw_ddi_is_clock_enabled;
4549351221ffSVille Syrjälä 		encoder->get_config = hsw_ddi_get_config;
4550d135368dSVille Syrjälä 	}
4551d135368dSVille Syrjälä 
4552193299adSVille Syrjälä 	if (IS_DG2(dev_priv)) {
4553193299adSVille Syrjälä 		encoder->set_signal_levels = intel_snps_phy_set_signal_levels;
4554193299adSVille Syrjälä 	} else if (DISPLAY_VER(dev_priv) >= 12) {
4555193299adSVille Syrjälä 		if (intel_phy_is_combo(dev_priv, phy))
4556193299adSVille Syrjälä 			encoder->set_signal_levels = icl_combo_phy_set_signal_levels;
4557e722ab8bSVille Syrjälä 		else
4558193299adSVille Syrjälä 			encoder->set_signal_levels = tgl_dkl_phy_set_signal_levels;
4559193299adSVille Syrjälä 	} else if (DISPLAY_VER(dev_priv) >= 11) {
4560193299adSVille Syrjälä 		if (intel_phy_is_combo(dev_priv, phy))
4561193299adSVille Syrjälä 			encoder->set_signal_levels = icl_combo_phy_set_signal_levels;
4562193299adSVille Syrjälä 		else
4563193299adSVille Syrjälä 			encoder->set_signal_levels = icl_mg_phy_set_signal_levels;
4564193299adSVille Syrjälä 	} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
45655f5ada0bSVille Syrjälä 		encoder->set_signal_levels = bxt_ddi_phy_set_signal_levels;
4566193299adSVille Syrjälä 	} else {
4567e722ab8bSVille Syrjälä 		encoder->set_signal_levels = hsw_set_signal_levels;
4568193299adSVille Syrjälä 	}
4569e722ab8bSVille Syrjälä 
4570c40a253bSVille Syrjälä 	intel_ddi_buf_trans_init(encoder);
4571c40a253bSVille Syrjälä 
4572ed2615a8SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 13)
4573ed2615a8SMatt Roper 		encoder->hpd_pin = xelpd_hpd_pin(dev_priv, port);
4574ed2615a8SMatt Roper 	else if (IS_DG1(dev_priv))
4575229f31e2SLucas De Marchi 		encoder->hpd_pin = dg1_hpd_pin(dev_priv, port);
4576229f31e2SLucas De Marchi 	else if (IS_ROCKETLAKE(dev_priv))
4577da51e4baSVille Syrjälä 		encoder->hpd_pin = rkl_hpd_pin(dev_priv, port);
4578005e9537SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 12)
4579da51e4baSVille Syrjälä 		encoder->hpd_pin = tgl_hpd_pin(dev_priv, port);
458024ea098bSTejas Upadhyay 	else if (IS_JSL_EHL(dev_priv))
4581da51e4baSVille Syrjälä 		encoder->hpd_pin = ehl_hpd_pin(dev_priv, port);
458293e7e61eSLucas De Marchi 	else if (DISPLAY_VER(dev_priv) == 11)
4583da51e4baSVille Syrjälä 		encoder->hpd_pin = icl_hpd_pin(dev_priv, port);
458493e7e61eSLucas De Marchi 	else if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv))
4585c8455098SLyude Paul 		encoder->hpd_pin = skl_hpd_pin(dev_priv, port);
4586da51e4baSVille Syrjälä 	else
458703c7e4f1SVille Syrjälä 		encoder->hpd_pin = intel_hpd_pin_default(dev_priv, port);
4588379bc100SJani Nikula 
4589005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
45907801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
45917801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
45927801f3b7SLucas De Marchi 			& DDI_BUF_PORT_REVERSAL;
4593379bc100SJani Nikula 	else
45947801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
45957801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
45967801f3b7SLucas De Marchi 			& (DDI_BUF_PORT_REVERSAL | DDI_A_4_LANES);
459770dfbc29SLucas De Marchi 
4598aaab24bbSUma Shankar 	if (intel_bios_is_lane_reversal_needed(dev_priv, port))
4599aaab24bbSUma Shankar 		dig_port->saved_port_bits |= DDI_BUF_PORT_REVERSAL;
4600aaab24bbSUma Shankar 
46017801f3b7SLucas De Marchi 	dig_port->dp.output_reg = INVALID_MMIO_REG;
46027801f3b7SLucas De Marchi 	dig_port->max_lanes = intel_ddi_max_lanes(dig_port);
46037801f3b7SLucas De Marchi 	dig_port->aux_ch = intel_bios_port_aux_ch(dev_priv, port);
4604379bc100SJani Nikula 
4605d8fe2ab6SMatt Roper 	if (intel_phy_is_tc(dev_priv, phy)) {
4606c5faae5aSJani Nikula 		bool is_legacy =
4607f08fbe6aSJani Nikula 			!intel_bios_encoder_supports_typec_usb(devdata) &&
4608f08fbe6aSJani Nikula 			!intel_bios_encoder_supports_tbt(devdata);
4609379bc100SJani Nikula 
46107801f3b7SLucas De Marchi 		intel_tc_port_init(dig_port, is_legacy);
461124a7bfe0SImre Deak 
461270dfbc29SLucas De Marchi 		encoder->update_prepare = intel_ddi_update_prepare;
461370dfbc29SLucas De Marchi 		encoder->update_complete = intel_ddi_update_complete;
4614ab7bc4e1SImre Deak 	}
4615ab7bc4e1SImre Deak 
46161de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, port > PORT_I);
46177801f3b7SLucas De Marchi 	dig_port->ddi_io_power_domain = POWER_DOMAIN_PORT_DDI_A_IO +
4618327f8d8cSLucas De Marchi 					      port - PORT_A;
4619379bc100SJani Nikula 
4620379bc100SJani Nikula 	if (init_dp) {
46217801f3b7SLucas De Marchi 		if (!intel_ddi_init_dp_connector(dig_port))
4622379bc100SJani Nikula 			goto err;
4623379bc100SJani Nikula 
46247801f3b7SLucas De Marchi 		dig_port->hpd_pulse = intel_dp_hpd_pulse;
4625bc71194eSJani Nikula 
4626f6864b27SJani Nikula 		if (dig_port->dp.mso_link_count)
4627f6864b27SJani Nikula 			encoder->pipe_mask = intel_ddi_splitter_pipe_mask(dev_priv);
4628379bc100SJani Nikula 	}
4629379bc100SJani Nikula 
4630379bc100SJani Nikula 	/* In theory we don't need the encoder->type check, but leave it just in
4631379bc100SJani Nikula 	 * case we have some really bad VBTs... */
463270dfbc29SLucas De Marchi 	if (encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
46337801f3b7SLucas De Marchi 		if (!intel_ddi_init_hdmi_connector(dig_port))
4634379bc100SJani Nikula 			goto err;
4635379bc100SJani Nikula 	}
4636379bc100SJani Nikula 
4637005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
4638edc0e09cSVille Syrjälä 		if (intel_phy_is_tc(dev_priv, phy))
46397801f3b7SLucas De Marchi 			dig_port->connected = intel_tc_port_connected;
4640edc0e09cSVille Syrjälä 		else
46417801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4642005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 8) {
46432446e1d6SMatt Roper 		if (port == PORT_A || IS_GEMINILAKE(dev_priv) ||
46442446e1d6SMatt Roper 		    IS_BROXTON(dev_priv))
46457801f3b7SLucas De Marchi 			dig_port->connected = bdw_digital_port_connected;
4646edc0e09cSVille Syrjälä 		else
46477801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4648edc0e09cSVille Syrjälä 	} else {
4649c7e8a3d6SVille Syrjälä 		if (port == PORT_A)
46507801f3b7SLucas De Marchi 			dig_port->connected = hsw_digital_port_connected;
4651edc0e09cSVille Syrjälä 		else
46527801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4653edc0e09cSVille Syrjälä 	}
4654edc0e09cSVille Syrjälä 
46557801f3b7SLucas De Marchi 	intel_infoframe_init(dig_port);
4656379bc100SJani Nikula 
4657379bc100SJani Nikula 	return;
4658379bc100SJani Nikula 
4659379bc100SJani Nikula err:
466070dfbc29SLucas De Marchi 	drm_encoder_cleanup(&encoder->base);
46617801f3b7SLucas De Marchi 	kfree(dig_port);
4662379bc100SJani Nikula }
4663