xref: /linux/drivers/gpu/drm/i915/display/intel_ddi.c (revision 305448e55745dd4634272794c8c01edc872bcf63)
1379bc100SJani Nikula /*
2379bc100SJani Nikula  * Copyright © 2012 Intel Corporation
3379bc100SJani Nikula  *
4379bc100SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining a
5379bc100SJani Nikula  * copy of this software and associated documentation files (the "Software"),
6379bc100SJani Nikula  * to deal in the Software without restriction, including without limitation
7379bc100SJani Nikula  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8379bc100SJani Nikula  * and/or sell copies of the Software, and to permit persons to whom the
9379bc100SJani Nikula  * Software is furnished to do so, subject to the following conditions:
10379bc100SJani Nikula  *
11379bc100SJani Nikula  * The above copyright notice and this permission notice (including the next
12379bc100SJani Nikula  * paragraph) shall be included in all copies or substantial portions of the
13379bc100SJani Nikula  * Software.
14379bc100SJani Nikula  *
15379bc100SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16379bc100SJani Nikula  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17379bc100SJani Nikula  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18379bc100SJani Nikula  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19379bc100SJani Nikula  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20379bc100SJani Nikula  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21379bc100SJani Nikula  * IN THE SOFTWARE.
22379bc100SJani Nikula  *
23379bc100SJani Nikula  * Authors:
24379bc100SJani Nikula  *    Eugeni Dodonov <eugeni.dodonov@intel.com>
25379bc100SJani Nikula  *
26379bc100SJani Nikula  */
27379bc100SJani Nikula 
28379bc100SJani Nikula #include <drm/drm_scdc_helper.h>
29379bc100SJani Nikula 
30379bc100SJani Nikula #include "i915_drv.h"
31379bc100SJani Nikula #include "intel_audio.h"
326cc42fbeSJani Nikula #include "intel_backlight.h"
33379bc100SJani Nikula #include "intel_combo_phy.h"
34379bc100SJani Nikula #include "intel_connector.h"
357c53e628SJani Nikula #include "intel_crtc.h"
36379bc100SJani Nikula #include "intel_ddi.h"
3799092a97SDave Airlie #include "intel_ddi_buf_trans.h"
387785ae0bSVille Syrjälä #include "intel_de.h"
391d455f8dSJani Nikula #include "intel_display_types.h"
40379bc100SJani Nikula #include "intel_dp.h"
41379bc100SJani Nikula #include "intel_dp_link_training.h"
42dcb38f79SDave Airlie #include "intel_dp_mst.h"
43379bc100SJani Nikula #include "intel_dpio_phy.h"
44a1b63119SJosé Roberto de Souza #include "intel_drrs.h"
45379bc100SJani Nikula #include "intel_dsi.h"
46dcb38f79SDave Airlie #include "intel_fdi.h"
47379bc100SJani Nikula #include "intel_fifo_underrun.h"
48379bc100SJani Nikula #include "intel_gmbus.h"
49379bc100SJani Nikula #include "intel_hdcp.h"
50379bc100SJani Nikula #include "intel_hdmi.h"
51379bc100SJani Nikula #include "intel_hotplug.h"
52379bc100SJani Nikula #include "intel_lspcon.h"
53abad6805SJani Nikula #include "intel_pps.h"
54379bc100SJani Nikula #include "intel_psr.h"
55865b73eaSMatt Roper #include "intel_snps_phy.h"
56bdacf087SAnshuman Gupta #include "intel_sprite.h"
57bc85328fSImre Deak #include "intel_tc.h"
58379bc100SJani Nikula #include "intel_vdsc.h"
59aa52b39dSManasi Navare #include "intel_vrr.h"
60714b1cdbSDave Airlie #include "skl_scaler.h"
6146d12f91SDave Airlie #include "skl_universal_plane.h"
62379bc100SJani Nikula 
63379bc100SJani Nikula static const u8 index_to_dp_signal_levels[] = {
64379bc100SJani Nikula 	[0] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0,
65379bc100SJani Nikula 	[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1,
66379bc100SJani Nikula 	[2] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2,
67379bc100SJani Nikula 	[3] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_3,
68379bc100SJani Nikula 	[4] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0,
69379bc100SJani Nikula 	[5] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1,
70379bc100SJani Nikula 	[6] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2,
71379bc100SJani Nikula 	[7] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0,
72379bc100SJani Nikula 	[8] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1,
73379bc100SJani Nikula 	[9] = DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0,
74379bc100SJani Nikula };
75379bc100SJani Nikula 
76a621860aSVille Syrjälä static int intel_ddi_hdmi_level(struct intel_encoder *encoder,
773e022c1fSVille Syrjälä 				const struct intel_ddi_buf_trans *trans)
78379bc100SJani Nikula {
793e022c1fSVille Syrjälä 	int level;
80379bc100SJani Nikula 
810aed3bdeSJani Nikula 	level = intel_bios_hdmi_level_shift(encoder);
820aed3bdeSJani Nikula 	if (level < 0)
833e022c1fSVille Syrjälä 		level = trans->hdmi_default_entry;
84379bc100SJani Nikula 
85379bc100SJani Nikula 	return level;
86379bc100SJani Nikula }
87379bc100SJani Nikula 
885bafd85dSVille Syrjälä static bool has_buf_trans_select(struct drm_i915_private *i915)
895bafd85dSVille Syrjälä {
905bafd85dSVille Syrjälä 	return DISPLAY_VER(i915) < 10 && !IS_BROXTON(i915);
915bafd85dSVille Syrjälä }
925bafd85dSVille Syrjälä 
93f820693bSVille Syrjälä static bool has_iboost(struct drm_i915_private *i915)
94f820693bSVille Syrjälä {
95f820693bSVille Syrjälä 	return DISPLAY_VER(i915) == 9 && !IS_BROXTON(i915);
96f820693bSVille Syrjälä }
97f820693bSVille Syrjälä 
98379bc100SJani Nikula /*
99379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
100379bc100SJani Nikula  * values in advance. This function programs the correct values for
101379bc100SJani Nikula  * DP/eDP/FDI use cases.
102379bc100SJani Nikula  */
103266152aeSVille Syrjälä void hsw_prepare_dp_ddi_buffers(struct intel_encoder *encoder,
104379bc100SJani Nikula 				const struct intel_crtc_state *crtc_state)
105379bc100SJani Nikula {
106379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
107379bc100SJani Nikula 	u32 iboost_bit = 0;
108379bc100SJani Nikula 	int i, n_entries;
109379bc100SJani Nikula 	enum port port = encoder->port;
110e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
111379bc100SJani Nikula 
112e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
113e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
114d6b10b1aSVille Syrjälä 		return;
115379bc100SJani Nikula 
116379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
117f820693bSVille Syrjälä 	if (has_iboost(dev_priv) &&
1182446e1d6SMatt Roper 	    intel_bios_encoder_dp_boost_level(encoder->devdata))
119379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
120379bc100SJani Nikula 
121379bc100SJani Nikula 	for (i = 0; i < n_entries; i++) {
122f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, i),
123e505d764SVille Syrjälä 			       trans->entries[i].hsw.trans1 | iboost_bit);
124f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, i),
125e505d764SVille Syrjälä 			       trans->entries[i].hsw.trans2);
126379bc100SJani Nikula 	}
127379bc100SJani Nikula }
128379bc100SJani Nikula 
129379bc100SJani Nikula /*
130379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
131379bc100SJani Nikula  * values in advance. This function programs the correct values for
132379bc100SJani Nikula  * HDMI/DVI use cases.
133379bc100SJani Nikula  */
134266152aeSVille Syrjälä static void hsw_prepare_hdmi_ddi_buffers(struct intel_encoder *encoder,
135e722ab8bSVille Syrjälä 					 const struct intel_crtc_state *crtc_state)
136379bc100SJani Nikula {
137379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
138d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
139379bc100SJani Nikula 	u32 iboost_bit = 0;
140379bc100SJani Nikula 	int n_entries;
141379bc100SJani Nikula 	enum port port = encoder->port;
142e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
143379bc100SJani Nikula 
144e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
145e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
146379bc100SJani Nikula 		return;
147379bc100SJani Nikula 
148379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
149f820693bSVille Syrjälä 	if (has_iboost(dev_priv) &&
1502446e1d6SMatt Roper 	    intel_bios_encoder_hdmi_boost_level(encoder->devdata))
151379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
152379bc100SJani Nikula 
153379bc100SJani Nikula 	/* Entry 9 is for HDMI: */
154f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, 9),
155e505d764SVille Syrjälä 		       trans->entries[level].hsw.trans1 | iboost_bit);
156f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, 9),
157e505d764SVille Syrjälä 		       trans->entries[level].hsw.trans2);
158379bc100SJani Nikula }
159379bc100SJani Nikula 
160dcb38f79SDave Airlie void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
161379bc100SJani Nikula 			     enum port port)
162379bc100SJani Nikula {
1635a2ad99bSManasi Navare 	if (IS_BROXTON(dev_priv)) {
1645a2ad99bSManasi Navare 		udelay(16);
165379bc100SJani Nikula 		return;
166379bc100SJani Nikula 	}
1675a2ad99bSManasi Navare 
1685a2ad99bSManasi Navare 	if (wait_for_us((intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
1695a2ad99bSManasi Navare 			 DDI_BUF_IS_IDLE), 8))
1705a2ad99bSManasi Navare 		drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get idle\n",
17147bdb1caSJani Nikula 			port_name(port));
172379bc100SJani Nikula }
173379bc100SJani Nikula 
174e828da30SManasi Navare static void intel_wait_ddi_buf_active(struct drm_i915_private *dev_priv,
175e828da30SManasi Navare 				      enum port port)
176e828da30SManasi Navare {
177f82f2563SMatt Roper 	int ret;
178f82f2563SMatt Roper 
179e828da30SManasi Navare 	/* Wait > 518 usecs for DDI_BUF_CTL to be non idle */
180ad314fecSVille Syrjälä 	if (DISPLAY_VER(dev_priv) < 10) {
181e828da30SManasi Navare 		usleep_range(518, 1000);
182e828da30SManasi Navare 		return;
183e828da30SManasi Navare 	}
184e828da30SManasi Navare 
185f82f2563SMatt Roper 	ret = _wait_for(!(intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
186f82f2563SMatt Roper 			  DDI_BUF_IS_IDLE), IS_DG2(dev_priv) ? 1200 : 500, 10, 10);
187f82f2563SMatt Roper 
188f82f2563SMatt Roper 	if (ret)
189e828da30SManasi Navare 		drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get active\n",
190e828da30SManasi Navare 			port_name(port));
191e828da30SManasi Navare }
192e828da30SManasi Navare 
193ad952982SVille Syrjälä static u32 hsw_pll_to_ddi_pll_sel(const struct intel_shared_dpll *pll)
194379bc100SJani Nikula {
195379bc100SJani Nikula 	switch (pll->info->id) {
196379bc100SJani Nikula 	case DPLL_ID_WRPLL1:
197379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL1;
198379bc100SJani Nikula 	case DPLL_ID_WRPLL2:
199379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL2;
200379bc100SJani Nikula 	case DPLL_ID_SPLL:
201379bc100SJani Nikula 		return PORT_CLK_SEL_SPLL;
202379bc100SJani Nikula 	case DPLL_ID_LCPLL_810:
203379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_810;
204379bc100SJani Nikula 	case DPLL_ID_LCPLL_1350:
205379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_1350;
206379bc100SJani Nikula 	case DPLL_ID_LCPLL_2700:
207379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_2700;
208379bc100SJani Nikula 	default:
209379bc100SJani Nikula 		MISSING_CASE(pll->info->id);
210379bc100SJani Nikula 		return PORT_CLK_SEL_NONE;
211379bc100SJani Nikula 	}
212379bc100SJani Nikula }
213379bc100SJani Nikula 
214379bc100SJani Nikula static u32 icl_pll_to_ddi_clk_sel(struct intel_encoder *encoder,
215379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
216379bc100SJani Nikula {
217379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
218379bc100SJani Nikula 	int clock = crtc_state->port_clock;
219379bc100SJani Nikula 	const enum intel_dpll_id id = pll->info->id;
220379bc100SJani Nikula 
221379bc100SJani Nikula 	switch (id) {
222379bc100SJani Nikula 	default:
223379bc100SJani Nikula 		/*
224379bc100SJani Nikula 		 * DPLL_ID_ICL_DPLL0 and DPLL_ID_ICL_DPLL1 should not be used
225379bc100SJani Nikula 		 * here, so do warn if this get passed in
226379bc100SJani Nikula 		 */
227379bc100SJani Nikula 		MISSING_CASE(id);
228379bc100SJani Nikula 		return DDI_CLK_SEL_NONE;
229379bc100SJani Nikula 	case DPLL_ID_ICL_TBTPLL:
230379bc100SJani Nikula 		switch (clock) {
231379bc100SJani Nikula 		case 162000:
232379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_162;
233379bc100SJani Nikula 		case 270000:
234379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_270;
235379bc100SJani Nikula 		case 540000:
236379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_540;
237379bc100SJani Nikula 		case 810000:
238379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_810;
239379bc100SJani Nikula 		default:
240379bc100SJani Nikula 			MISSING_CASE(clock);
241379bc100SJani Nikula 			return DDI_CLK_SEL_NONE;
242379bc100SJani Nikula 		}
243379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL1:
244379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL2:
245379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL3:
246379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL4:
2476677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL5:
2486677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL6:
249379bc100SJani Nikula 		return DDI_CLK_SEL_MG;
250379bc100SJani Nikula 	}
251379bc100SJani Nikula }
252379bc100SJani Nikula 
253414002f1SImre Deak static u32 ddi_buf_phy_link_rate(int port_clock)
254414002f1SImre Deak {
255414002f1SImre Deak 	switch (port_clock) {
256414002f1SImre Deak 	case 162000:
257414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(0);
258414002f1SImre Deak 	case 216000:
259414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(4);
260414002f1SImre Deak 	case 243000:
261414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(5);
262414002f1SImre Deak 	case 270000:
263414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(1);
264414002f1SImre Deak 	case 324000:
265414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(6);
266414002f1SImre Deak 	case 432000:
267414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(7);
268414002f1SImre Deak 	case 540000:
269414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(2);
270414002f1SImre Deak 	case 810000:
271414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(3);
272414002f1SImre Deak 	default:
273414002f1SImre Deak 		MISSING_CASE(port_clock);
274414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(0);
275414002f1SImre Deak 	}
276414002f1SImre Deak }
277414002f1SImre Deak 
278a621860aSVille Syrjälä static void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder,
279a621860aSVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
280379bc100SJani Nikula {
28155ce306cSJosé Roberto de Souza 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
282b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2837801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
28455ce306cSJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(i915, encoder->port);
285379bc100SJani Nikula 
2869f620f1dSVille Syrjälä 	/* DDI_BUF_CTL_ENABLE will be set by intel_ddi_prepare_link_retrain() later */
2877801f3b7SLucas De Marchi 	intel_dp->DP = dig_port->saved_port_bits |
2889f620f1dSVille Syrjälä 		DDI_PORT_WIDTH(crtc_state->lane_count) |
2899f620f1dSVille Syrjälä 		DDI_BUF_TRANS_SELECT(0);
29055ce306cSJosé Roberto de Souza 
291414002f1SImre Deak 	if (IS_ALDERLAKE_P(i915) && intel_phy_is_tc(i915, phy)) {
292414002f1SImre Deak 		intel_dp->DP |= ddi_buf_phy_link_rate(crtc_state->port_clock);
29311a89708SImre Deak 		if (!intel_tc_port_in_tbt_alt_mode(dig_port))
29455ce306cSJosé Roberto de Souza 			intel_dp->DP |= DDI_BUF_CTL_TC_PHY_OWNERSHIP;
295379bc100SJani Nikula 	}
296414002f1SImre Deak }
297379bc100SJani Nikula 
298379bc100SJani Nikula static int icl_calc_tbt_pll_link(struct drm_i915_private *dev_priv,
299379bc100SJani Nikula 				 enum port port)
300379bc100SJani Nikula {
301f7960e7fSJani Nikula 	u32 val = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
302379bc100SJani Nikula 
303379bc100SJani Nikula 	switch (val) {
304379bc100SJani Nikula 	case DDI_CLK_SEL_NONE:
305379bc100SJani Nikula 		return 0;
306379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_162:
307379bc100SJani Nikula 		return 162000;
308379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_270:
309379bc100SJani Nikula 		return 270000;
310379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_540:
311379bc100SJani Nikula 		return 540000;
312379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_810:
313379bc100SJani Nikula 		return 810000;
314379bc100SJani Nikula 	default:
315379bc100SJani Nikula 		MISSING_CASE(val);
316379bc100SJani Nikula 		return 0;
317379bc100SJani Nikula 	}
318379bc100SJani Nikula }
319379bc100SJani Nikula 
320379bc100SJani Nikula static void ddi_dotclock_get(struct intel_crtc_state *pipe_config)
321379bc100SJani Nikula {
322379bc100SJani Nikula 	int dotclock;
323379bc100SJani Nikula 
3249e68fa88SVille Syrjälä 	/* CRT dotclock is determined via other means */
325379bc100SJani Nikula 	if (pipe_config->has_pch_encoder)
3269e68fa88SVille Syrjälä 		return;
3279e68fa88SVille Syrjälä 
3289e68fa88SVille Syrjälä 	if (intel_crtc_has_dp_encoder(pipe_config))
329379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
330379bc100SJani Nikula 						    &pipe_config->dp_m_n);
3312969a78aSImre Deak 	else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp > 24)
3322969a78aSImre Deak 		dotclock = pipe_config->port_clock * 24 / pipe_config->pipe_bpp;
333379bc100SJani Nikula 	else
334379bc100SJani Nikula 		dotclock = pipe_config->port_clock;
335379bc100SJani Nikula 
336379bc100SJani Nikula 	if (pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
337379bc100SJani Nikula 	    !intel_crtc_has_dp_encoder(pipe_config))
338379bc100SJani Nikula 		dotclock *= 2;
339379bc100SJani Nikula 
340379bc100SJani Nikula 	if (pipe_config->pixel_multiplier)
341379bc100SJani Nikula 		dotclock /= pipe_config->pixel_multiplier;
342379bc100SJani Nikula 
3431326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.crtc_clock = dotclock;
344379bc100SJani Nikula }
345379bc100SJani Nikula 
3460c06fa15SGwan-gyeong Mun void intel_ddi_set_dp_msa(const struct intel_crtc_state *crtc_state,
3470c06fa15SGwan-gyeong Mun 			  const struct drm_connector_state *conn_state)
348379bc100SJani Nikula {
3492225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
350379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
351379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
352379bc100SJani Nikula 	u32 temp;
353379bc100SJani Nikula 
354379bc100SJani Nikula 	if (!intel_crtc_has_dp_encoder(crtc_state))
355379bc100SJani Nikula 		return;
356379bc100SJani Nikula 
3571de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder));
358379bc100SJani Nikula 
3593e706dffSVille Syrjälä 	temp = DP_MSA_MISC_SYNC_CLOCK;
360379bc100SJani Nikula 
361379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
362379bc100SJani Nikula 	case 18:
3633e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_6_BPC;
364379bc100SJani Nikula 		break;
365379bc100SJani Nikula 	case 24:
3663e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_8_BPC;
367379bc100SJani Nikula 		break;
368379bc100SJani Nikula 	case 30:
3693e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_10_BPC;
370379bc100SJani Nikula 		break;
371379bc100SJani Nikula 	case 36:
3723e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_12_BPC;
373379bc100SJani Nikula 		break;
374379bc100SJani Nikula 	default:
375379bc100SJani Nikula 		MISSING_CASE(crtc_state->pipe_bpp);
376379bc100SJani Nikula 		break;
377379bc100SJani Nikula 	}
378379bc100SJani Nikula 
379cae154fcSVille Syrjälä 	/* nonsense combination */
3801de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
381cae154fcSVille Syrjälä 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
382cae154fcSVille Syrjälä 
383cae154fcSVille Syrjälä 	if (crtc_state->limited_color_range)
3843e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_CEA_RGB;
385cae154fcSVille Syrjälä 
386379bc100SJani Nikula 	/*
387379bc100SJani Nikula 	 * As per DP 1.2 spec section 2.3.4.3 while sending
388379bc100SJani Nikula 	 * YCBCR 444 signals we should program MSA MISC1/0 fields with
389646d3dc8SVille Syrjälä 	 * colorspace information.
390379bc100SJani Nikula 	 */
391379bc100SJani Nikula 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
3923e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_YCBCR_444_BT709;
393646d3dc8SVille Syrjälä 
394379bc100SJani Nikula 	/*
395379bc100SJani Nikula 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
396379bc100SJani Nikula 	 * of Color Encoding Format and Content Color Gamut] while sending
3970c06fa15SGwan-gyeong Mun 	 * YCBCR 420, HDR BT.2020 signals we should program MSA MISC1 fields
3980c06fa15SGwan-gyeong Mun 	 * which indicate VSC SDP for the Pixel Encoding/Colorimetry Format.
399379bc100SJani Nikula 	 */
400bd8c9ccaSGwan-gyeong Mun 	if (intel_dp_needs_vsc_sdp(crtc_state, conn_state))
4013e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_VSC_SDP;
4020c06fa15SGwan-gyeong Mun 
403f7960e7fSJani Nikula 	intel_de_write(dev_priv, TRANS_MSA_MISC(cpu_transcoder), temp);
404379bc100SJani Nikula }
405379bc100SJani Nikula 
406dc5b8ed5SVille Syrjälä static u32 bdw_trans_port_sync_master_select(enum transcoder master_transcoder)
407dc5b8ed5SVille Syrjälä {
408dc5b8ed5SVille Syrjälä 	if (master_transcoder == TRANSCODER_EDP)
409dc5b8ed5SVille Syrjälä 		return 0;
410dc5b8ed5SVille Syrjälä 	else
411dc5b8ed5SVille Syrjälä 		return master_transcoder + 1;
412dc5b8ed5SVille Syrjälä }
413dc5b8ed5SVille Syrjälä 
41479ac2b1bSJani Nikula static void
41579ac2b1bSJani Nikula intel_ddi_config_transcoder_dp2(struct intel_encoder *encoder,
41679ac2b1bSJani Nikula 				const struct intel_crtc_state *crtc_state)
41779ac2b1bSJani Nikula {
41879ac2b1bSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
41979ac2b1bSJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
42079ac2b1bSJani Nikula 	u32 val = 0;
42179ac2b1bSJani Nikula 
42279ac2b1bSJani Nikula 	if (intel_dp_is_uhbr(crtc_state))
42379ac2b1bSJani Nikula 		val = TRANS_DP2_128B132B_CHANNEL_CODING;
42479ac2b1bSJani Nikula 
42579ac2b1bSJani Nikula 	intel_de_write(i915, TRANS_DP2_CTL(cpu_transcoder), val);
42679ac2b1bSJani Nikula }
42779ac2b1bSJani Nikula 
42899389390SJosé Roberto de Souza /*
42999389390SJosé Roberto de Souza  * Returns the TRANS_DDI_FUNC_CTL value based on CRTC state.
43099389390SJosé Roberto de Souza  *
43199389390SJosé Roberto de Souza  * Only intended to be used by intel_ddi_enable_transcoder_func() and
43299389390SJosé Roberto de Souza  * intel_ddi_config_transcoder_func().
43399389390SJosé Roberto de Souza  */
43499389390SJosé Roberto de Souza static u32
435eed22a46SVille Syrjälä intel_ddi_transcoder_func_reg_val_get(struct intel_encoder *encoder,
436eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
437379bc100SJani Nikula {
4382225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
439379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
440379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
441379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
442379bc100SJani Nikula 	enum port port = encoder->port;
443379bc100SJani Nikula 	u32 temp;
444379bc100SJani Nikula 
445379bc100SJani Nikula 	/* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
446379bc100SJani Nikula 	temp = TRANS_DDI_FUNC_ENABLE;
447005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
448df16b636SMahesh Kumar 		temp |= TGL_TRANS_DDI_SELECT_PORT(port);
449df16b636SMahesh Kumar 	else
450379bc100SJani Nikula 		temp |= TRANS_DDI_SELECT_PORT(port);
451379bc100SJani Nikula 
452379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
453379bc100SJani Nikula 	case 18:
454379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_6;
455379bc100SJani Nikula 		break;
456379bc100SJani Nikula 	case 24:
457379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_8;
458379bc100SJani Nikula 		break;
459379bc100SJani Nikula 	case 30:
460379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_10;
461379bc100SJani Nikula 		break;
462379bc100SJani Nikula 	case 36:
463379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_12;
464379bc100SJani Nikula 		break;
465379bc100SJani Nikula 	default:
466379bc100SJani Nikula 		BUG();
467379bc100SJani Nikula 	}
468379bc100SJani Nikula 
4691326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
470379bc100SJani Nikula 		temp |= TRANS_DDI_PVSYNC;
4711326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
472379bc100SJani Nikula 		temp |= TRANS_DDI_PHSYNC;
473379bc100SJani Nikula 
474379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP) {
475379bc100SJani Nikula 		switch (pipe) {
476379bc100SJani Nikula 		case PIPE_A:
477379bc100SJani Nikula 			/* On Haswell, can only use the always-on power well for
478379bc100SJani Nikula 			 * eDP when not using the panel fitter, and when not
479379bc100SJani Nikula 			 * using motion blur mitigation (which we don't
480379bc100SJani Nikula 			 * support). */
481379bc100SJani Nikula 			if (crtc_state->pch_pfit.force_thru)
482379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
483379bc100SJani Nikula 			else
484379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ON;
485379bc100SJani Nikula 			break;
486379bc100SJani Nikula 		case PIPE_B:
487379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
488379bc100SJani Nikula 			break;
489379bc100SJani Nikula 		case PIPE_C:
490379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
491379bc100SJani Nikula 			break;
492379bc100SJani Nikula 		default:
493379bc100SJani Nikula 			BUG();
494379bc100SJani Nikula 			break;
495379bc100SJani Nikula 		}
496379bc100SJani Nikula 	}
497379bc100SJani Nikula 
498379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
499379bc100SJani Nikula 		if (crtc_state->has_hdmi_sink)
500379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_HDMI;
501379bc100SJani Nikula 		else
502379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_DVI;
503379bc100SJani Nikula 
504379bc100SJani Nikula 		if (crtc_state->hdmi_scrambling)
505379bc100SJani Nikula 			temp |= TRANS_DDI_HDMI_SCRAMBLING;
506379bc100SJani Nikula 		if (crtc_state->hdmi_high_tmds_clock_ratio)
507379bc100SJani Nikula 			temp |= TRANS_DDI_HIGH_TMDS_CHAR_RATE;
508379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
5097bb97db8SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_FDI_OR_128B132B;
510379bc100SJani Nikula 		temp |= (crtc_state->fdi_lanes - 1) << 1;
511379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
51265213594SJani Nikula 		if (intel_dp_is_uhbr(crtc_state))
51365213594SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_FDI_OR_128B132B;
51465213594SJani Nikula 		else
515379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_DP_MST;
516379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
517b3545e08SLucas De Marchi 
518005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12) {
5196671c367SJosé Roberto de Souza 			enum transcoder master;
5206671c367SJosé Roberto de Souza 
5216671c367SJosé Roberto de Souza 			master = crtc_state->mst_master_transcoder;
5221de143ccSPankaj Bharadiya 			drm_WARN_ON(&dev_priv->drm,
5231de143ccSPankaj Bharadiya 				    master == INVALID_TRANSCODER);
5246671c367SJosé Roberto de Souza 			temp |= TRANS_DDI_MST_TRANSPORT_SELECT(master);
5256671c367SJosé Roberto de Souza 		}
526379bc100SJani Nikula 	} else {
527379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_SST;
528379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
529379bc100SJani Nikula 	}
530379bc100SJani Nikula 
53193e7e61eSLucas De Marchi 	if (IS_DISPLAY_VER(dev_priv, 8, 10) &&
532dc5b8ed5SVille Syrjälä 	    crtc_state->master_transcoder != INVALID_TRANSCODER) {
533dc5b8ed5SVille Syrjälä 		u8 master_select =
534dc5b8ed5SVille Syrjälä 			bdw_trans_port_sync_master_select(crtc_state->master_transcoder);
535dc5b8ed5SVille Syrjälä 
536dc5b8ed5SVille Syrjälä 		temp |= TRANS_DDI_PORT_SYNC_ENABLE |
537dc5b8ed5SVille Syrjälä 			TRANS_DDI_PORT_SYNC_MASTER_SELECT(master_select);
538dc5b8ed5SVille Syrjälä 	}
539dc5b8ed5SVille Syrjälä 
54099389390SJosé Roberto de Souza 	return temp;
54199389390SJosé Roberto de Souza }
54299389390SJosé Roberto de Souza 
543eed22a46SVille Syrjälä void intel_ddi_enable_transcoder_func(struct intel_encoder *encoder,
544eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
54599389390SJosé Roberto de Souza {
5462225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
54799389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
54899389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
54999389390SJosé Roberto de Souza 
550005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
551589a4cd6SVille Syrjälä 		enum transcoder master_transcoder = crtc_state->master_transcoder;
552589a4cd6SVille Syrjälä 		u32 ctl2 = 0;
553589a4cd6SVille Syrjälä 
554589a4cd6SVille Syrjälä 		if (master_transcoder != INVALID_TRANSCODER) {
555dc5b8ed5SVille Syrjälä 			u8 master_select =
556dc5b8ed5SVille Syrjälä 				bdw_trans_port_sync_master_select(master_transcoder);
557589a4cd6SVille Syrjälä 
558589a4cd6SVille Syrjälä 			ctl2 |= PORT_SYNC_MODE_ENABLE |
559d4d7d9caSVille Syrjälä 				PORT_SYNC_MODE_MASTER_SELECT(master_select);
560589a4cd6SVille Syrjälä 		}
561589a4cd6SVille Syrjälä 
562589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
563589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), ctl2);
564589a4cd6SVille Syrjälä 	}
565589a4cd6SVille Syrjälä 
566580fbdc5SImre Deak 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder),
567580fbdc5SImre Deak 		       intel_ddi_transcoder_func_reg_val_get(encoder,
568580fbdc5SImre Deak 							     crtc_state));
56999389390SJosé Roberto de Souza }
57099389390SJosé Roberto de Souza 
57199389390SJosé Roberto de Souza /*
57299389390SJosé Roberto de Souza  * Same as intel_ddi_enable_transcoder_func(), but it does not set the enable
57399389390SJosé Roberto de Souza  * bit.
57499389390SJosé Roberto de Souza  */
57599389390SJosé Roberto de Souza static void
576eed22a46SVille Syrjälä intel_ddi_config_transcoder_func(struct intel_encoder *encoder,
577eed22a46SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
57899389390SJosé Roberto de Souza {
5792225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
58099389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
58199389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
582589a4cd6SVille Syrjälä 	u32 ctl;
58399389390SJosé Roberto de Souza 
584eed22a46SVille Syrjälä 	ctl = intel_ddi_transcoder_func_reg_val_get(encoder, crtc_state);
585589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
586589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
587379bc100SJani Nikula }
588379bc100SJani Nikula 
589379bc100SJani Nikula void intel_ddi_disable_transcoder_func(const struct intel_crtc_state *crtc_state)
590379bc100SJani Nikula {
5912225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
592379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
593379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
594589a4cd6SVille Syrjälä 	u32 ctl;
595c59053dcSJosé Roberto de Souza 
596005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
597589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
598589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), 0);
599589a4cd6SVille Syrjälä 
600589a4cd6SVille Syrjälä 	ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
601dc5b8ed5SVille Syrjälä 
6021cfcdbf3SSean Paul 	drm_WARN_ON(crtc->base.dev, ctl & TRANS_DDI_HDCP_SIGNALLING);
6031cfcdbf3SSean Paul 
604589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
605379bc100SJani Nikula 
60693e7e61eSLucas De Marchi 	if (IS_DISPLAY_VER(dev_priv, 8, 10))
607dc5b8ed5SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_SYNC_ENABLE |
608dc5b8ed5SVille Syrjälä 			 TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK);
609dc5b8ed5SVille Syrjälä 
610005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
611919e4f07SJosé Roberto de Souza 		if (!intel_dp_mst_is_master_trans(crtc_state)) {
612589a4cd6SVille Syrjälä 			ctl &= ~(TGL_TRANS_DDI_PORT_MASK |
613919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
614919e4f07SJosé Roberto de Souza 		}
615df16b636SMahesh Kumar 	} else {
616589a4cd6SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_MASK | TRANS_DDI_MODE_SELECT_MASK);
617df16b636SMahesh Kumar 	}
618dc5b8ed5SVille Syrjälä 
619589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
620379bc100SJani Nikula 
621379bc100SJani Nikula 	if (dev_priv->quirks & QUIRK_INCREASE_DDI_DISABLED_TIME &&
622379bc100SJani Nikula 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
62347bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
62447bdb1caSJani Nikula 			    "Quirk Increase DDI disabled time\n");
625379bc100SJani Nikula 		/* Quirk time at 100ms for reliable operation */
626379bc100SJani Nikula 		msleep(100);
627379bc100SJani Nikula 	}
628379bc100SJani Nikula }
629379bc100SJani Nikula 
6301a67a168SAnshuman Gupta int intel_ddi_toggle_hdcp_bits(struct intel_encoder *intel_encoder,
6310b9c9290SSean Paul 			       enum transcoder cpu_transcoder,
6321a67a168SAnshuman Gupta 			       bool enable, u32 hdcp_mask)
633379bc100SJani Nikula {
634379bc100SJani Nikula 	struct drm_device *dev = intel_encoder->base.dev;
635379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
636379bc100SJani Nikula 	intel_wakeref_t wakeref;
637379bc100SJani Nikula 	int ret = 0;
638379bc100SJani Nikula 	u32 tmp;
639379bc100SJani Nikula 
640379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
641379bc100SJani Nikula 						     intel_encoder->power_domain);
6421de143ccSPankaj Bharadiya 	if (drm_WARN_ON(dev, !wakeref))
643379bc100SJani Nikula 		return -ENXIO;
644379bc100SJani Nikula 
6450b9c9290SSean Paul 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
646379bc100SJani Nikula 	if (enable)
6471a67a168SAnshuman Gupta 		tmp |= hdcp_mask;
648379bc100SJani Nikula 	else
6491a67a168SAnshuman Gupta 		tmp &= ~hdcp_mask;
6500b9c9290SSean Paul 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), tmp);
651379bc100SJani Nikula 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
652379bc100SJani Nikula 	return ret;
653379bc100SJani Nikula }
654379bc100SJani Nikula 
655379bc100SJani Nikula bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
656379bc100SJani Nikula {
657379bc100SJani Nikula 	struct drm_device *dev = intel_connector->base.dev;
658379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
659fa7edcd2SVille Syrjälä 	struct intel_encoder *encoder = intel_attached_encoder(intel_connector);
660379bc100SJani Nikula 	int type = intel_connector->base.connector_type;
661379bc100SJani Nikula 	enum port port = encoder->port;
662379bc100SJani Nikula 	enum transcoder cpu_transcoder;
663379bc100SJani Nikula 	intel_wakeref_t wakeref;
664379bc100SJani Nikula 	enum pipe pipe = 0;
665379bc100SJani Nikula 	u32 tmp;
666379bc100SJani Nikula 	bool ret;
667379bc100SJani Nikula 
668379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
669379bc100SJani Nikula 						     encoder->power_domain);
670379bc100SJani Nikula 	if (!wakeref)
671379bc100SJani Nikula 		return false;
672379bc100SJani Nikula 
673379bc100SJani Nikula 	if (!encoder->get_hw_state(encoder, &pipe)) {
674379bc100SJani Nikula 		ret = false;
675379bc100SJani Nikula 		goto out;
676379bc100SJani Nikula 	}
677379bc100SJani Nikula 
67810cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
679379bc100SJani Nikula 		cpu_transcoder = TRANSCODER_EDP;
680379bc100SJani Nikula 	else
681379bc100SJani Nikula 		cpu_transcoder = (enum transcoder) pipe;
682379bc100SJani Nikula 
683f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
684379bc100SJani Nikula 
685379bc100SJani Nikula 	switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
686379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
687379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
688379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_HDMIA;
689379bc100SJani Nikula 		break;
690379bc100SJani Nikula 
691379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
692379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_eDP ||
693379bc100SJani Nikula 		      type == DRM_MODE_CONNECTOR_DisplayPort;
694379bc100SJani Nikula 		break;
695379bc100SJani Nikula 
696379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
697379bc100SJani Nikula 		/* if the transcoder is in MST state then
698379bc100SJani Nikula 		 * connector isn't connected */
699379bc100SJani Nikula 		ret = false;
700379bc100SJani Nikula 		break;
701379bc100SJani Nikula 
7027bb97db8SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI_OR_128B132B:
70365213594SJani Nikula 		if (HAS_DP20(dev_priv))
70465213594SJani Nikula 			/* 128b/132b */
70565213594SJani Nikula 			ret = false;
70665213594SJani Nikula 		else
70765213594SJani Nikula 			/* FDI */
708379bc100SJani Nikula 			ret = type == DRM_MODE_CONNECTOR_VGA;
709379bc100SJani Nikula 		break;
710379bc100SJani Nikula 
711379bc100SJani Nikula 	default:
712379bc100SJani Nikula 		ret = false;
713379bc100SJani Nikula 		break;
714379bc100SJani Nikula 	}
715379bc100SJani Nikula 
716379bc100SJani Nikula out:
717379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
718379bc100SJani Nikula 
719379bc100SJani Nikula 	return ret;
720379bc100SJani Nikula }
721379bc100SJani Nikula 
722379bc100SJani Nikula static void intel_ddi_get_encoder_pipes(struct intel_encoder *encoder,
723379bc100SJani Nikula 					u8 *pipe_mask, bool *is_dp_mst)
724379bc100SJani Nikula {
725379bc100SJani Nikula 	struct drm_device *dev = encoder->base.dev;
726379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
727379bc100SJani Nikula 	enum port port = encoder->port;
728379bc100SJani Nikula 	intel_wakeref_t wakeref;
729379bc100SJani Nikula 	enum pipe p;
730379bc100SJani Nikula 	u32 tmp;
731379bc100SJani Nikula 	u8 mst_pipe_mask;
732379bc100SJani Nikula 
733379bc100SJani Nikula 	*pipe_mask = 0;
734379bc100SJani Nikula 	*is_dp_mst = false;
735379bc100SJani Nikula 
736379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
737379bc100SJani Nikula 						     encoder->power_domain);
738379bc100SJani Nikula 	if (!wakeref)
739379bc100SJani Nikula 		return;
740379bc100SJani Nikula 
741f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
742379bc100SJani Nikula 	if (!(tmp & DDI_BUF_CTL_ENABLE))
743379bc100SJani Nikula 		goto out;
744379bc100SJani Nikula 
74510cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A) {
746f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
747f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
748379bc100SJani Nikula 
749379bc100SJani Nikula 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
750379bc100SJani Nikula 		default:
751379bc100SJani Nikula 			MISSING_CASE(tmp & TRANS_DDI_EDP_INPUT_MASK);
752df561f66SGustavo A. R. Silva 			fallthrough;
753379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ON:
754379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
755379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_A);
756379bc100SJani Nikula 			break;
757379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
758379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_B);
759379bc100SJani Nikula 			break;
760379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
761379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_C);
762379bc100SJani Nikula 			break;
763379bc100SJani Nikula 		}
764379bc100SJani Nikula 
765379bc100SJani Nikula 		goto out;
766379bc100SJani Nikula 	}
767379bc100SJani Nikula 
768379bc100SJani Nikula 	mst_pipe_mask = 0;
769379bc100SJani Nikula 	for_each_pipe(dev_priv, p) {
770379bc100SJani Nikula 		enum transcoder cpu_transcoder = (enum transcoder)p;
771df16b636SMahesh Kumar 		unsigned int port_mask, ddi_select;
7726aa3bef1SJosé Roberto de Souza 		intel_wakeref_t trans_wakeref;
7736aa3bef1SJosé Roberto de Souza 
7746aa3bef1SJosé Roberto de Souza 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
7756aa3bef1SJosé Roberto de Souza 								   POWER_DOMAIN_TRANSCODER(cpu_transcoder));
7766aa3bef1SJosé Roberto de Souza 		if (!trans_wakeref)
7776aa3bef1SJosé Roberto de Souza 			continue;
778df16b636SMahesh Kumar 
779005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12) {
780df16b636SMahesh Kumar 			port_mask = TGL_TRANS_DDI_PORT_MASK;
781df16b636SMahesh Kumar 			ddi_select = TGL_TRANS_DDI_SELECT_PORT(port);
782df16b636SMahesh Kumar 		} else {
783df16b636SMahesh Kumar 			port_mask = TRANS_DDI_PORT_MASK;
784df16b636SMahesh Kumar 			ddi_select = TRANS_DDI_SELECT_PORT(port);
785df16b636SMahesh Kumar 		}
786379bc100SJani Nikula 
787f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
788f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
7896aa3bef1SJosé Roberto de Souza 		intel_display_power_put(dev_priv, POWER_DOMAIN_TRANSCODER(cpu_transcoder),
7906aa3bef1SJosé Roberto de Souza 					trans_wakeref);
791379bc100SJani Nikula 
792df16b636SMahesh Kumar 		if ((tmp & port_mask) != ddi_select)
793379bc100SJani Nikula 			continue;
794379bc100SJani Nikula 
79565213594SJani Nikula 		if ((tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_DP_MST ||
79665213594SJani Nikula 		    (HAS_DP20(dev_priv) &&
79765213594SJani Nikula 		     (tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_FDI_OR_128B132B))
798379bc100SJani Nikula 			mst_pipe_mask |= BIT(p);
799379bc100SJani Nikula 
800379bc100SJani Nikula 		*pipe_mask |= BIT(p);
801379bc100SJani Nikula 	}
802379bc100SJani Nikula 
803379bc100SJani Nikula 	if (!*pipe_mask)
80447bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
80547bdb1caSJani Nikula 			    "No pipe for [ENCODER:%d:%s] found\n",
80666a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name);
807379bc100SJani Nikula 
808379bc100SJani Nikula 	if (!mst_pipe_mask && hweight8(*pipe_mask) > 1) {
80947bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
81047bdb1caSJani Nikula 			    "Multiple pipes for [ENCODER:%d:%s] (pipe_mask %02x)\n",
81166a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
81266a990ddSVille Syrjälä 			    *pipe_mask);
813379bc100SJani Nikula 		*pipe_mask = BIT(ffs(*pipe_mask) - 1);
814379bc100SJani Nikula 	}
815379bc100SJani Nikula 
816379bc100SJani Nikula 	if (mst_pipe_mask && mst_pipe_mask != *pipe_mask)
81747bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
81847bdb1caSJani Nikula 			    "Conflicting MST and non-MST state for [ENCODER:%d:%s] (pipe_mask %02x mst_pipe_mask %02x)\n",
81966a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
82066a990ddSVille Syrjälä 			    *pipe_mask, mst_pipe_mask);
821379bc100SJani Nikula 	else
822379bc100SJani Nikula 		*is_dp_mst = mst_pipe_mask;
823379bc100SJani Nikula 
824379bc100SJani Nikula out:
8252446e1d6SMatt Roper 	if (*pipe_mask && (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))) {
826f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv, BXT_PHY_CTL(port));
827379bc100SJani Nikula 		if ((tmp & (BXT_PHY_CMNLANE_POWERDOWN_ACK |
828379bc100SJani Nikula 			    BXT_PHY_LANE_POWERDOWN_ACK |
829379bc100SJani Nikula 			    BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
83047bdb1caSJani Nikula 			drm_err(&dev_priv->drm,
83147bdb1caSJani Nikula 				"[ENCODER:%d:%s] enabled but PHY powered down? (PHY_CTL %08x)\n",
83247bdb1caSJani Nikula 				encoder->base.base.id, encoder->base.name, tmp);
833379bc100SJani Nikula 	}
834379bc100SJani Nikula 
835379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
836379bc100SJani Nikula }
837379bc100SJani Nikula 
838379bc100SJani Nikula bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
839379bc100SJani Nikula 			    enum pipe *pipe)
840379bc100SJani Nikula {
841379bc100SJani Nikula 	u8 pipe_mask;
842379bc100SJani Nikula 	bool is_mst;
843379bc100SJani Nikula 
844379bc100SJani Nikula 	intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
845379bc100SJani Nikula 
846379bc100SJani Nikula 	if (is_mst || !pipe_mask)
847379bc100SJani Nikula 		return false;
848379bc100SJani Nikula 
849379bc100SJani Nikula 	*pipe = ffs(pipe_mask) - 1;
850379bc100SJani Nikula 
851379bc100SJani Nikula 	return true;
852379bc100SJani Nikula }
853379bc100SJani Nikula 
85481b55ef1SJani Nikula static enum intel_display_power_domain
855379bc100SJani Nikula intel_ddi_main_link_aux_domain(struct intel_digital_port *dig_port)
856379bc100SJani Nikula {
8574da27d5dSLucas De Marchi 	/* ICL+ HW requires corresponding AUX IOs to be powered up for PSR with
858379bc100SJani Nikula 	 * DC states enabled at the same time, while for driver initiated AUX
859379bc100SJani Nikula 	 * transfers we need the same AUX IOs to be powered but with DC states
860379bc100SJani Nikula 	 * disabled. Accordingly use the AUX power domain here which leaves DC
861379bc100SJani Nikula 	 * states enabled.
862379bc100SJani Nikula 	 * However, for non-A AUX ports the corresponding non-EDP transcoders
863379bc100SJani Nikula 	 * would have already enabled power well 2 and DC_OFF. This means we can
864379bc100SJani Nikula 	 * acquire a wider POWER_DOMAIN_AUX_{B,C,D,F} reference instead of a
865379bc100SJani Nikula 	 * specific AUX_IO reference without powering up any extra wells.
866379bc100SJani Nikula 	 * Note that PSR is enabled only on Port A even though this function
867379bc100SJani Nikula 	 * returns the correct domain for other ports too.
868379bc100SJani Nikula 	 */
869379bc100SJani Nikula 	return dig_port->aux_ch == AUX_CH_A ? POWER_DOMAIN_AUX_IO_A :
870379bc100SJani Nikula 					      intel_aux_power_domain(dig_port);
871379bc100SJani Nikula }
872379bc100SJani Nikula 
873379bc100SJani Nikula static void intel_ddi_get_power_domains(struct intel_encoder *encoder,
874379bc100SJani Nikula 					struct intel_crtc_state *crtc_state)
875379bc100SJani Nikula {
876379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
877379bc100SJani Nikula 	struct intel_digital_port *dig_port;
878d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
879379bc100SJani Nikula 
880379bc100SJani Nikula 	/*
881379bc100SJani Nikula 	 * TODO: Add support for MST encoders. Atm, the following should never
882379bc100SJani Nikula 	 * happen since fake-MST encoders don't set their get_power_domains()
883379bc100SJani Nikula 	 * hook.
884379bc100SJani Nikula 	 */
8851de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
8861de143ccSPankaj Bharadiya 			intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)))
887379bc100SJani Nikula 		return;
888379bc100SJani Nikula 
889b7d02c3aSVille Syrjälä 	dig_port = enc_to_dig_port(encoder);
890f77a2db2SImre Deak 
89111a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
892a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
893a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
894f77a2db2SImre Deak 								   dig_port->ddi_io_power_domain);
895a4550977SImre Deak 	}
896379bc100SJani Nikula 
897379bc100SJani Nikula 	/*
898379bc100SJani Nikula 	 * AUX power is only needed for (e)DP mode, and for HDMI mode on TC
899379bc100SJani Nikula 	 * ports.
900379bc100SJani Nikula 	 */
901379bc100SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state) ||
902162e68e1SImre Deak 	    intel_phy_is_tc(dev_priv, phy)) {
903162e68e1SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->aux_wakeref);
904162e68e1SImre Deak 		dig_port->aux_wakeref =
905379bc100SJani Nikula 			intel_display_power_get(dev_priv,
906379bc100SJani Nikula 						intel_ddi_main_link_aux_domain(dig_port));
907379bc100SJani Nikula 	}
908162e68e1SImre Deak }
909379bc100SJani Nikula 
91002a715c3SVille Syrjälä void intel_ddi_enable_pipe_clock(struct intel_encoder *encoder,
91102a715c3SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
912379bc100SJani Nikula {
9132225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
914379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
915379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
916ed2615a8SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
917ed2615a8SMatt Roper 	u32 val;
918379bc100SJani Nikula 
919df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
920ed2615a8SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 13)
921ed2615a8SMatt Roper 			val = TGL_TRANS_CLK_SEL_PORT(phy);
922ed2615a8SMatt Roper 		else if (DISPLAY_VER(dev_priv) >= 12)
923ed2615a8SMatt Roper 			val = TGL_TRANS_CLK_SEL_PORT(encoder->port);
924df16b636SMahesh Kumar 		else
925ed2615a8SMatt Roper 			val = TRANS_CLK_SEL_PORT(encoder->port);
926ed2615a8SMatt Roper 
927ed2615a8SMatt Roper 		intel_de_write(dev_priv, TRANS_CLK_SEL(cpu_transcoder), val);
928379bc100SJani Nikula 	}
929df16b636SMahesh Kumar }
930379bc100SJani Nikula 
931379bc100SJani Nikula void intel_ddi_disable_pipe_clock(const struct intel_crtc_state *crtc_state)
932379bc100SJani Nikula {
9332225f3c6SMaarten Lankhorst 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
934379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
935379bc100SJani Nikula 
936df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
937005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12)
938f7960e7fSJani Nikula 			intel_de_write(dev_priv,
939f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
940df16b636SMahesh Kumar 				       TGL_TRANS_CLK_SEL_DISABLED);
941df16b636SMahesh Kumar 		else
942f7960e7fSJani Nikula 			intel_de_write(dev_priv,
943f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
944379bc100SJani Nikula 				       TRANS_CLK_SEL_DISABLED);
945379bc100SJani Nikula 	}
946df16b636SMahesh Kumar }
947379bc100SJani Nikula 
948379bc100SJani Nikula static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
949379bc100SJani Nikula 				enum port port, u8 iboost)
950379bc100SJani Nikula {
951379bc100SJani Nikula 	u32 tmp;
952379bc100SJani Nikula 
953f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DISPIO_CR_TX_BMU_CR0);
954379bc100SJani Nikula 	tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
955379bc100SJani Nikula 	if (iboost)
956379bc100SJani Nikula 		tmp |= iboost << BALANCE_LEG_SHIFT(port);
957379bc100SJani Nikula 	else
958379bc100SJani Nikula 		tmp |= BALANCE_LEG_DISABLE(port);
959f7960e7fSJani Nikula 	intel_de_write(dev_priv, DISPIO_CR_TX_BMU_CR0, tmp);
960379bc100SJani Nikula }
961379bc100SJani Nikula 
962379bc100SJani Nikula static void skl_ddi_set_iboost(struct intel_encoder *encoder,
963a621860aSVille Syrjälä 			       const struct intel_crtc_state *crtc_state,
964a621860aSVille Syrjälä 			       int level)
965379bc100SJani Nikula {
9667801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
967379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
968379bc100SJani Nikula 	u8 iboost;
969379bc100SJani Nikula 
970a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
971c0a950d1SJani Nikula 		iboost = intel_bios_encoder_hdmi_boost_level(encoder->devdata);
972379bc100SJani Nikula 	else
973c0a950d1SJani Nikula 		iboost = intel_bios_encoder_dp_boost_level(encoder->devdata);
974379bc100SJani Nikula 
975379bc100SJani Nikula 	if (iboost == 0) {
976e505d764SVille Syrjälä 		const struct intel_ddi_buf_trans *trans;
977379bc100SJani Nikula 		int n_entries;
978379bc100SJani Nikula 
979e505d764SVille Syrjälä 		trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
980e505d764SVille Syrjälä 		if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
981379bc100SJani Nikula 			return;
982379bc100SJani Nikula 
983e505d764SVille Syrjälä 		iboost = trans->entries[level].hsw.i_boost;
984379bc100SJani Nikula 	}
985379bc100SJani Nikula 
986379bc100SJani Nikula 	/* Make sure that the requested I_boost is valid */
987379bc100SJani Nikula 	if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
98847bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Invalid I_boost value %u\n", iboost);
989379bc100SJani Nikula 		return;
990379bc100SJani Nikula 	}
991379bc100SJani Nikula 
992f0e86e05SJosé Roberto de Souza 	_skl_ddi_set_iboost(dev_priv, encoder->port, iboost);
993379bc100SJani Nikula 
994f0e86e05SJosé Roberto de Souza 	if (encoder->port == PORT_A && dig_port->max_lanes == 4)
995379bc100SJani Nikula 		_skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
996379bc100SJani Nikula }
997379bc100SJani Nikula 
998a621860aSVille Syrjälä static u8 intel_ddi_dp_voltage_max(struct intel_dp *intel_dp,
999a621860aSVille Syrjälä 				   const struct intel_crtc_state *crtc_state)
1000379bc100SJani Nikula {
100153de0a20SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1002379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1003379bc100SJani Nikula 	int n_entries;
1004379bc100SJani Nikula 
1005c40a253bSVille Syrjälä 	encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1006379bc100SJani Nikula 
10071de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, n_entries < 1))
1008379bc100SJani Nikula 		n_entries = 1;
10091de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
10101de143ccSPankaj Bharadiya 			n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
1011379bc100SJani Nikula 		n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
1012379bc100SJani Nikula 
1013379bc100SJani Nikula 	return index_to_dp_signal_levels[n_entries - 1] &
1014379bc100SJani Nikula 		DP_TRAIN_VOLTAGE_SWING_MASK;
1015379bc100SJani Nikula }
1016379bc100SJani Nikula 
1017379bc100SJani Nikula /*
1018379bc100SJani Nikula  * We assume that the full set of pre-emphasis values can be
1019379bc100SJani Nikula  * used on all DDI platforms. Should that change we need to
1020379bc100SJani Nikula  * rethink this code.
1021379bc100SJani Nikula  */
102253de0a20SVille Syrjälä static u8 intel_ddi_dp_preemph_max(struct intel_dp *intel_dp)
1023379bc100SJani Nikula {
1024379bc100SJani Nikula 	return DP_TRAIN_PRE_EMPH_LEVEL_3;
1025379bc100SJani Nikula }
1026379bc100SJani Nikula 
10275e7fe4d9SVille Syrjälä static u32 icl_combo_phy_loadgen_select(const struct intel_crtc_state *crtc_state,
10285e7fe4d9SVille Syrjälä 					int lane)
10295e7fe4d9SVille Syrjälä {
10305e7fe4d9SVille Syrjälä 	if (crtc_state->port_clock > 600000)
10315e7fe4d9SVille Syrjälä 		return 0;
10325e7fe4d9SVille Syrjälä 
10335e7fe4d9SVille Syrjälä 	if (crtc_state->lane_count == 4)
10345e7fe4d9SVille Syrjälä 		return lane >= 1 ? LOADGEN_SELECT : 0;
10355e7fe4d9SVille Syrjälä 	else
10365e7fe4d9SVille Syrjälä 		return lane == 1 || lane == 2 ? LOADGEN_SELECT : 0;
10375e7fe4d9SVille Syrjälä }
10385e7fe4d9SVille Syrjälä 
1039a8143150SJosé Roberto de Souza static void icl_ddi_combo_vswing_program(struct intel_encoder *encoder,
1040193299adSVille Syrjälä 					 const struct intel_crtc_state *crtc_state)
1041379bc100SJani Nikula {
1042a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1043e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
1044f0e86e05SJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1045a621860aSVille Syrjälä 	int n_entries, ln;
1046a621860aSVille Syrjälä 	u32 val;
1047379bc100SJani Nikula 
1048e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1049e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
105085da0292SVille Syrjälä 		return;
1051379bc100SJani Nikula 
1052a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP)) {
105381619f4aSJosé Roberto de Souza 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
105481619f4aSJosé Roberto de Souza 
105581619f4aSJosé Roberto de Souza 		val = EDP4K2K_MODE_OVRD_EN | EDP4K2K_MODE_OVRD_OPTIMIZED;
1056e505d764SVille Syrjälä 		intel_dp->hobl_active = is_hobl_buf_trans(trans);
105781619f4aSJosé Roberto de Souza 		intel_de_rmw(dev_priv, ICL_PORT_CL_DW10(phy), val,
105881619f4aSJosé Roberto de Souza 			     intel_dp->hobl_active ? val : 0);
105981619f4aSJosé Roberto de Souza 	}
106081619f4aSJosé Roberto de Souza 
1061379bc100SJani Nikula 	/* Set PORT_TX_DW5 */
1062e6908588SVille Syrjälä 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1063379bc100SJani Nikula 	val &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK |
1064379bc100SJani Nikula 		  TAP2_DISABLE | TAP3_DISABLE);
1065379bc100SJani Nikula 	val |= SCALING_MODE_SEL(0x2);
1066379bc100SJani Nikula 	val |= RTERM_SELECT(0x6);
1067379bc100SJani Nikula 	val |= TAP3_DISABLE;
1068f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1069379bc100SJani Nikula 
1070379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
1071f20ca899SVille Syrjälä 	for (ln = 0; ln < 4; ln++) {
107231e914a2SVille Syrjälä 		int level = intel_ddi_level(encoder, crtc_state, ln);
107331e914a2SVille Syrjälä 
1074f20ca899SVille Syrjälä 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW2_LN(ln, phy));
1075379bc100SJani Nikula 		val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
1076379bc100SJani Nikula 			 RCOMP_SCALAR_MASK);
1077e505d764SVille Syrjälä 		val |= SWING_SEL_UPPER(trans->entries[level].icl.dw2_swing_sel);
1078e505d764SVille Syrjälä 		val |= SWING_SEL_LOWER(trans->entries[level].icl.dw2_swing_sel);
1079379bc100SJani Nikula 		/* Program Rcomp scalar for every table entry */
1080379bc100SJani Nikula 		val |= RCOMP_SCALAR(0x98);
1081f20ca899SVille Syrjälä 		intel_de_write(dev_priv, ICL_PORT_TX_DW2_LN(ln, phy), val);
1082f20ca899SVille Syrjälä 	}
1083379bc100SJani Nikula 
1084379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
1085379bc100SJani Nikula 	/* We cannot write to GRP. It would overwrite individual loadgen. */
1086a1f01768SVille Syrjälä 	for (ln = 0; ln < 4; ln++) {
108731e914a2SVille Syrjälä 		int level = intel_ddi_level(encoder, crtc_state, ln);
108831e914a2SVille Syrjälä 
1089f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
1090379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
1091379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
1092e505d764SVille Syrjälä 		val |= POST_CURSOR_1(trans->entries[level].icl.dw4_post_cursor_1);
1093e505d764SVille Syrjälä 		val |= POST_CURSOR_2(trans->entries[level].icl.dw4_post_cursor_2);
1094e505d764SVille Syrjälä 		val |= CURSOR_COEFF(trans->entries[level].icl.dw4_cursor_coeff);
1095f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
1096379bc100SJani Nikula 	}
1097379bc100SJani Nikula 
1098379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
1099f20ca899SVille Syrjälä 	for (ln = 0; ln < 4; ln++) {
110031e914a2SVille Syrjälä 		int level = intel_ddi_level(encoder, crtc_state, ln);
110131e914a2SVille Syrjälä 
1102f20ca899SVille Syrjälä 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW7_LN(ln, phy));
1103379bc100SJani Nikula 		val &= ~N_SCALAR_MASK;
1104e505d764SVille Syrjälä 		val |= N_SCALAR(trans->entries[level].icl.dw7_n_scalar);
1105f20ca899SVille Syrjälä 		intel_de_write(dev_priv, ICL_PORT_TX_DW7_LN(ln, phy), val);
1106f20ca899SVille Syrjälä 	}
1107379bc100SJani Nikula }
1108379bc100SJani Nikula 
1109193299adSVille Syrjälä static void icl_combo_phy_set_signal_levels(struct intel_encoder *encoder,
1110193299adSVille Syrjälä 					    const struct intel_crtc_state *crtc_state)
1111379bc100SJani Nikula {
1112379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1113dc867bc7SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1114379bc100SJani Nikula 	u32 val;
11155e7fe4d9SVille Syrjälä 	int ln;
1116379bc100SJani Nikula 
1117379bc100SJani Nikula 	/*
1118379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
1119379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
1120379bc100SJani Nikula 	 * else clear to 0b.
1121379bc100SJani Nikula 	 */
1122e6908588SVille Syrjälä 	val = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_LN(0, phy));
1123a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1124379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
1125379bc100SJani Nikula 	else
1126379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
1127f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy), val);
1128379bc100SJani Nikula 
1129379bc100SJani Nikula 	/* 2. Program loadgen select */
1130379bc100SJani Nikula 	/*
1131e6908588SVille Syrjälä 	 * Program PORT_TX_DW4 depending on Bit rate and used lanes
1132379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
1133379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
1134379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
1135379bc100SJani Nikula 	 */
1136a1f01768SVille Syrjälä 	for (ln = 0; ln < 4; ln++) {
1137f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
1138379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
11395e7fe4d9SVille Syrjälä 		val |= icl_combo_phy_loadgen_select(crtc_state, ln);
1140f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
1141379bc100SJani Nikula 	}
1142379bc100SJani Nikula 
1143379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
1144f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_CL_DW5(phy));
1145379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
1146f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_CL_DW5(phy), val);
1147379bc100SJani Nikula 
1148379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
1149e6908588SVille Syrjälä 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1150379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
1151f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1152379bc100SJani Nikula 
1153379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
1154193299adSVille Syrjälä 	icl_ddi_combo_vswing_program(encoder, crtc_state);
1155379bc100SJani Nikula 
1156379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
1157e6908588SVille Syrjälä 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
1158379bc100SJani Nikula 	val |= TX_TRAINING_EN;
1159f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1160379bc100SJani Nikula }
1161379bc100SJani Nikula 
1162193299adSVille Syrjälä static void icl_mg_phy_set_signal_levels(struct intel_encoder *encoder,
1163193299adSVille Syrjälä 					 const struct intel_crtc_state *crtc_state)
1164379bc100SJani Nikula {
1165379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1166f21e8b80SJosé Roberto de Souza 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
1167e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
1168a621860aSVille Syrjälä 	int n_entries, ln;
1169a621860aSVille Syrjälä 	u32 val;
1170379bc100SJani Nikula 
117111a89708SImre Deak 	if (intel_tc_port_in_tbt_alt_mode(enc_to_dig_port(encoder)))
1172f8c6b615SVille Syrjälä 		return;
1173f8c6b615SVille Syrjälä 
1174e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1175e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
117685da0292SVille Syrjälä 		return;
1177379bc100SJani Nikula 
1178379bc100SJani Nikula 	/* Set MG_TX_LINK_PARAMS cri_use_fs32 to 0. */
1179379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1180f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port));
1181379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
1182f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port), val);
1183379bc100SJani Nikula 
1184f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port));
1185379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
1186f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port), val);
1187379bc100SJani Nikula 	}
1188379bc100SJani Nikula 
1189379bc100SJani Nikula 	/* Program MG_TX_SWINGCTRL with values from vswing table */
1190379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1191*305448e5SVille Syrjälä 		int level;
1192*305448e5SVille Syrjälä 
1193*305448e5SVille Syrjälä 		level = intel_ddi_level(encoder, crtc_state, 2*ln+0);
1194*305448e5SVille Syrjälä 
1195f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port));
1196379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
1197379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
1198e505d764SVille Syrjälä 			trans->entries[level].mg.cri_txdeemph_override_17_12);
1199f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port), val);
1200379bc100SJani Nikula 
1201*305448e5SVille Syrjälä 		level = intel_ddi_level(encoder, crtc_state, 2*ln+1);
1202*305448e5SVille Syrjälä 
1203f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port));
1204379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
1205379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
1206e505d764SVille Syrjälä 			trans->entries[level].mg.cri_txdeemph_override_17_12);
1207f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port), val);
1208379bc100SJani Nikula 	}
1209379bc100SJani Nikula 
1210379bc100SJani Nikula 	/* Program MG_TX_DRVCTRL with values from vswing table */
1211379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1212*305448e5SVille Syrjälä 		int level;
1213*305448e5SVille Syrjälä 
1214*305448e5SVille Syrjälä 		level = intel_ddi_level(encoder, crtc_state, 2*ln+0);
1215*305448e5SVille Syrjälä 
1216f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DRVCTRL(ln, tc_port));
1217379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1218379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
1219379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
1220e505d764SVille Syrjälä 			trans->entries[level].mg.cri_txdeemph_override_5_0) |
1221379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
1222e505d764SVille Syrjälä 				trans->entries[level].mg.cri_txdeemph_override_11_6) |
1223379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
1224f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DRVCTRL(ln, tc_port), val);
1225379bc100SJani Nikula 
1226*305448e5SVille Syrjälä 		level = intel_ddi_level(encoder, crtc_state, 2*ln+1);
1227*305448e5SVille Syrjälä 
1228f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DRVCTRL(ln, tc_port));
1229379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1230379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
1231379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
1232e505d764SVille Syrjälä 			trans->entries[level].mg.cri_txdeemph_override_5_0) |
1233379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
1234e505d764SVille Syrjälä 				trans->entries[level].mg.cri_txdeemph_override_11_6) |
1235379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
1236f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DRVCTRL(ln, tc_port), val);
1237379bc100SJani Nikula 
1238379bc100SJani Nikula 		/* FIXME: Program CRI_LOADGEN_SEL after the spec is updated */
1239379bc100SJani Nikula 	}
1240379bc100SJani Nikula 
1241379bc100SJani Nikula 	/*
1242379bc100SJani Nikula 	 * Program MG_CLKHUB<LN, port being used> with value from frequency table
1243379bc100SJani Nikula 	 * In case of Legacy mode on MG PHY, both TX1 and TX2 enabled so use the
1244379bc100SJani Nikula 	 * values from table for which TX1 and TX2 enabled.
1245379bc100SJani Nikula 	 */
1246379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1247f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_CLKHUB(ln, tc_port));
1248a621860aSVille Syrjälä 		if (crtc_state->port_clock < 300000)
1249379bc100SJani Nikula 			val |= CFG_LOW_RATE_LKREN_EN;
1250379bc100SJani Nikula 		else
1251379bc100SJani Nikula 			val &= ~CFG_LOW_RATE_LKREN_EN;
1252f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_CLKHUB(ln, tc_port), val);
1253379bc100SJani Nikula 	}
1254379bc100SJani Nikula 
1255379bc100SJani Nikula 	/* Program the MG_TX_DCC<LN, port being used> based on the link frequency */
1256379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1257f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DCC(ln, tc_port));
1258379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
1259a621860aSVille Syrjälä 		if (crtc_state->port_clock <= 500000) {
1260379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
1261379bc100SJani Nikula 		} else {
1262379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
1263379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
1264379bc100SJani Nikula 		}
1265f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DCC(ln, tc_port), val);
1266379bc100SJani Nikula 
1267f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DCC(ln, tc_port));
1268379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
1269a621860aSVille Syrjälä 		if (crtc_state->port_clock <= 500000) {
1270379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
1271379bc100SJani Nikula 		} else {
1272379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
1273379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
1274379bc100SJani Nikula 		}
1275f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DCC(ln, tc_port), val);
1276379bc100SJani Nikula 	}
1277379bc100SJani Nikula 
1278379bc100SJani Nikula 	/* Program MG_TX_PISO_READLOAD with values from vswing table */
1279379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1280f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
1281f7960e7fSJani Nikula 				    MG_TX1_PISO_READLOAD(ln, tc_port));
1282379bc100SJani Nikula 		val |= CRI_CALCINIT;
1283f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_PISO_READLOAD(ln, tc_port),
1284f7960e7fSJani Nikula 			       val);
1285379bc100SJani Nikula 
1286f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
1287f7960e7fSJani Nikula 				    MG_TX2_PISO_READLOAD(ln, tc_port));
1288379bc100SJani Nikula 		val |= CRI_CALCINIT;
1289f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_PISO_READLOAD(ln, tc_port),
1290f7960e7fSJani Nikula 			       val);
1291379bc100SJani Nikula 	}
1292379bc100SJani Nikula }
1293379bc100SJani Nikula 
1294193299adSVille Syrjälä static void tgl_dkl_phy_set_signal_levels(struct intel_encoder *encoder,
1295193299adSVille Syrjälä 					  const struct intel_crtc_state *crtc_state)
1296978c3e53SClinton A Taylor {
1297978c3e53SClinton A Taylor 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1298978c3e53SClinton A Taylor 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
1299d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
1300e505d764SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
1301a621860aSVille Syrjälä 	u32 val, dpcnt_mask, dpcnt_val;
1302a621860aSVille Syrjälä 	int n_entries, ln;
1303978c3e53SClinton A Taylor 
130411a89708SImre Deak 	if (intel_tc_port_in_tbt_alt_mode(enc_to_dig_port(encoder)))
1305f8c6b615SVille Syrjälä 		return;
1306f8c6b615SVille Syrjälä 
1307e505d764SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1308e505d764SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
130985da0292SVille Syrjälä 		return;
1310978c3e53SClinton A Taylor 
1311978c3e53SClinton A Taylor 	dpcnt_mask = (DKL_TX_PRESHOOT_COEFF_MASK |
1312978c3e53SClinton A Taylor 		      DKL_TX_DE_EMPAHSIS_COEFF_MASK |
1313978c3e53SClinton A Taylor 		      DKL_TX_VSWING_CONTROL_MASK);
1314247c8a73SVille Syrjälä 	dpcnt_val = DKL_TX_VSWING_CONTROL(trans->entries[level].dkl.vswing);
1315247c8a73SVille Syrjälä 	dpcnt_val |= DKL_TX_DE_EMPHASIS_COEFF(trans->entries[level].dkl.de_emphasis);
1316247c8a73SVille Syrjälä 	dpcnt_val |= DKL_TX_PRESHOOT_COEFF(trans->entries[level].dkl.preshoot);
1317978c3e53SClinton A Taylor 
1318978c3e53SClinton A Taylor 	for (ln = 0; ln < 2; ln++) {
1319f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
1320f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, ln));
1321978c3e53SClinton A Taylor 
1322f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_PMD_LANE_SUS(tc_port), 0);
13232d69c42eSJosé Roberto de Souza 
1324978c3e53SClinton A Taylor 		/* All the registers are RMW */
1325f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL0(tc_port));
1326978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
1327978c3e53SClinton A Taylor 		val |= dpcnt_val;
1328f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL0(tc_port), val);
1329978c3e53SClinton A Taylor 
1330f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL1(tc_port));
1331978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
1332978c3e53SClinton A Taylor 		val |= dpcnt_val;
1333f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL1(tc_port), val);
1334978c3e53SClinton A Taylor 
1335f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL2(tc_port));
1336978c3e53SClinton A Taylor 		val &= ~DKL_TX_DP20BITMODE;
1337f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL2(tc_port), val);
1338978c3e53SClinton A Taylor 	}
1339978c3e53SClinton A Taylor }
1340978c3e53SClinton A Taylor 
1341a621860aSVille Syrjälä static int translate_signal_level(struct intel_dp *intel_dp,
1342a621860aSVille Syrjälä 				  u8 signal_levels)
1343379bc100SJani Nikula {
13448b4f2137SPankaj Bharadiya 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1345379bc100SJani Nikula 	int i;
1346379bc100SJani Nikula 
1347379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
1348379bc100SJani Nikula 		if (index_to_dp_signal_levels[i] == signal_levels)
1349379bc100SJani Nikula 			return i;
1350379bc100SJani Nikula 	}
1351379bc100SJani Nikula 
13528b4f2137SPankaj Bharadiya 	drm_WARN(&i915->drm, 1,
13538b4f2137SPankaj Bharadiya 		 "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
1354379bc100SJani Nikula 		 signal_levels);
1355379bc100SJani Nikula 
1356379bc100SJani Nikula 	return 0;
1357379bc100SJani Nikula }
1358379bc100SJani Nikula 
13595c31e9d0SJani Nikula static int intel_ddi_dp_level(struct intel_dp *intel_dp,
13605c31e9d0SJani Nikula 			      const struct intel_crtc_state *crtc_state,
13615c31e9d0SJani Nikula 			      int lane)
1362379bc100SJani Nikula {
1363d0920a45SVille Syrjälä 	u8 train_set = intel_dp->train_set[lane];
13645c31e9d0SJani Nikula 
13655c31e9d0SJani Nikula 	if (intel_dp_is_uhbr(crtc_state)) {
13665c31e9d0SJani Nikula 		return train_set & DP_TX_FFE_PRESET_VALUE_MASK;
13675c31e9d0SJani Nikula 	} else {
1368a621860aSVille Syrjälä 		u8 signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1369379bc100SJani Nikula 						DP_TRAIN_PRE_EMPHASIS_MASK);
1370379bc100SJani Nikula 
13718b4f2137SPankaj Bharadiya 		return translate_signal_level(intel_dp, signal_levels);
1372379bc100SJani Nikula 	}
13735c31e9d0SJani Nikula }
1374379bc100SJani Nikula 
1375193299adSVille Syrjälä int intel_ddi_level(struct intel_encoder *encoder,
1376d0920a45SVille Syrjälä 		    const struct intel_crtc_state *crtc_state,
1377d0920a45SVille Syrjälä 		    int lane)
1378a046a0daSMatt Roper {
13792c63e0f9SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
13802c63e0f9SVille Syrjälä 	const struct intel_ddi_buf_trans *trans;
13812c63e0f9SVille Syrjälä 	int level, n_entries;
13822c63e0f9SVille Syrjälä 
13832c63e0f9SVille Syrjälä 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
13842c63e0f9SVille Syrjälä 	if (drm_WARN_ON_ONCE(&i915->drm, !trans))
13852c63e0f9SVille Syrjälä 		return 0;
13862c63e0f9SVille Syrjälä 
1387e722ab8bSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
13883e022c1fSVille Syrjälä 		level = intel_ddi_hdmi_level(encoder, trans);
1389e722ab8bSVille Syrjälä 	else
13905c31e9d0SJani Nikula 		level = intel_ddi_dp_level(enc_to_intel_dp(encoder), crtc_state,
13915c31e9d0SJani Nikula 					   lane);
13922c63e0f9SVille Syrjälä 
13932c63e0f9SVille Syrjälä 	if (drm_WARN_ON_ONCE(&i915->drm, level >= n_entries))
13942c63e0f9SVille Syrjälä 		level = n_entries - 1;
13952c63e0f9SVille Syrjälä 
13962c63e0f9SVille Syrjälä 	return level;
1397e722ab8bSVille Syrjälä }
1398e722ab8bSVille Syrjälä 
1399e722ab8bSVille Syrjälä static void
1400e722ab8bSVille Syrjälä hsw_set_signal_levels(struct intel_encoder *encoder,
1401a621860aSVille Syrjälä 		      const struct intel_crtc_state *crtc_state)
1402fb83f72cSVille Syrjälä {
1403fb83f72cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1404e722ab8bSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1405d0920a45SVille Syrjälä 	int level = intel_ddi_level(encoder, crtc_state, 0);
1406fb83f72cSVille Syrjälä 	enum port port = encoder->port;
1407fb83f72cSVille Syrjälä 	u32 signal_levels;
1408fb83f72cSVille Syrjälä 
1409e722ab8bSVille Syrjälä 	if (has_iboost(dev_priv))
1410e722ab8bSVille Syrjälä 		skl_ddi_set_iboost(encoder, crtc_state, level);
1411e722ab8bSVille Syrjälä 
1412e722ab8bSVille Syrjälä 	/* HDMI ignores the rest */
1413e722ab8bSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1414e722ab8bSVille Syrjälä 		return;
1415e722ab8bSVille Syrjälä 
1416fb83f72cSVille Syrjälä 	signal_levels = DDI_BUF_TRANS_SELECT(level);
1417fb83f72cSVille Syrjälä 
1418fb83f72cSVille Syrjälä 	drm_dbg_kms(&dev_priv->drm, "Using signal levels %08x\n",
1419fb83f72cSVille Syrjälä 		    signal_levels);
1420fb83f72cSVille Syrjälä 
1421fb83f72cSVille Syrjälä 	intel_dp->DP &= ~DDI_BUF_EMP_MASK;
1422fb83f72cSVille Syrjälä 	intel_dp->DP |= signal_levels;
1423fb83f72cSVille Syrjälä 
1424fb83f72cSVille Syrjälä 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
1425fb83f72cSVille Syrjälä 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
1426379bc100SJani Nikula }
1427379bc100SJani Nikula 
14284da27d5dSLucas De Marchi static void _icl_ddi_enable_clock(struct drm_i915_private *i915, i915_reg_t reg,
14299c6a5c35SVille Syrjälä 				  u32 clk_sel_mask, u32 clk_sel, u32 clk_off)
14309c6a5c35SVille Syrjälä {
14319c6a5c35SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
14329c6a5c35SVille Syrjälä 
14339c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, clk_sel_mask, clk_sel);
14349c6a5c35SVille Syrjälä 
14359c6a5c35SVille Syrjälä 	/*
14369c6a5c35SVille Syrjälä 	 * "This step and the step before must be
14379c6a5c35SVille Syrjälä 	 *  done with separate register writes."
14389c6a5c35SVille Syrjälä 	 */
14399c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, clk_off, 0);
14409c6a5c35SVille Syrjälä 
14419c6a5c35SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
14429c6a5c35SVille Syrjälä }
14439c6a5c35SVille Syrjälä 
14444da27d5dSLucas De Marchi static void _icl_ddi_disable_clock(struct drm_i915_private *i915, i915_reg_t reg,
14459c6a5c35SVille Syrjälä 				   u32 clk_off)
14469c6a5c35SVille Syrjälä {
14479c6a5c35SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
14489c6a5c35SVille Syrjälä 
14499c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, 0, clk_off);
14509c6a5c35SVille Syrjälä 
14519c6a5c35SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
14529c6a5c35SVille Syrjälä }
14539c6a5c35SVille Syrjälä 
14544da27d5dSLucas De Marchi static bool _icl_ddi_is_clock_enabled(struct drm_i915_private *i915, i915_reg_t reg,
14550fbd8694SVille Syrjälä 				      u32 clk_off)
14560fbd8694SVille Syrjälä {
14570fbd8694SVille Syrjälä 	return !(intel_de_read(i915, reg) & clk_off);
14580fbd8694SVille Syrjälä }
14590fbd8694SVille Syrjälä 
1460351221ffSVille Syrjälä static struct intel_shared_dpll *
14614da27d5dSLucas De Marchi _icl_ddi_get_pll(struct drm_i915_private *i915, i915_reg_t reg,
1462351221ffSVille Syrjälä 		 u32 clk_sel_mask, u32 clk_sel_shift)
1463351221ffSVille Syrjälä {
1464351221ffSVille Syrjälä 	enum intel_dpll_id id;
1465351221ffSVille Syrjälä 
1466351221ffSVille Syrjälä 	id = (intel_de_read(i915, reg) & clk_sel_mask) >> clk_sel_shift;
1467351221ffSVille Syrjälä 
1468351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1469351221ffSVille Syrjälä }
1470351221ffSVille Syrjälä 
147140b316d4SVille Syrjälä static void adls_ddi_enable_clock(struct intel_encoder *encoder,
147240b316d4SVille Syrjälä 				  const struct intel_crtc_state *crtc_state)
147340b316d4SVille Syrjälä {
147440b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
147540b316d4SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
147640b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
147740b316d4SVille Syrjälä 
147840b316d4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
147940b316d4SVille Syrjälä 		return;
148040b316d4SVille Syrjälä 
14814da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
148240b316d4SVille Syrjälä 			      ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
148340b316d4SVille Syrjälä 			      pll->info->id << ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy),
148440b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
148540b316d4SVille Syrjälä }
148640b316d4SVille Syrjälä 
148740b316d4SVille Syrjälä static void adls_ddi_disable_clock(struct intel_encoder *encoder)
148840b316d4SVille Syrjälä {
148940b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
149040b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
149140b316d4SVille Syrjälä 
14924da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
149340b316d4SVille Syrjälä 			       ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
149440b316d4SVille Syrjälä }
149540b316d4SVille Syrjälä 
14960fbd8694SVille Syrjälä static bool adls_ddi_is_clock_enabled(struct intel_encoder *encoder)
14970fbd8694SVille Syrjälä {
14980fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
14990fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
15000fbd8694SVille Syrjälä 
15014da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ADLS_DPCLKA_CFGCR(phy),
15020fbd8694SVille Syrjälä 					 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
15030fbd8694SVille Syrjälä }
15040fbd8694SVille Syrjälä 
1505351221ffSVille Syrjälä static struct intel_shared_dpll *adls_ddi_get_pll(struct intel_encoder *encoder)
1506351221ffSVille Syrjälä {
1507351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1508351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1509351221ffSVille Syrjälä 
15104da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ADLS_DPCLKA_CFGCR(phy),
1511351221ffSVille Syrjälä 				ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
1512351221ffSVille Syrjälä 				ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy));
1513351221ffSVille Syrjälä }
1514351221ffSVille Syrjälä 
151540b316d4SVille Syrjälä static void rkl_ddi_enable_clock(struct intel_encoder *encoder,
151640b316d4SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
151740b316d4SVille Syrjälä {
151840b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
151940b316d4SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
152040b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
152140b316d4SVille Syrjälä 
152240b316d4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
152340b316d4SVille Syrjälä 		return;
152440b316d4SVille Syrjälä 
15254da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
152640b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
152740b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
152840b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
152940b316d4SVille Syrjälä }
153040b316d4SVille Syrjälä 
153140b316d4SVille Syrjälä static void rkl_ddi_disable_clock(struct intel_encoder *encoder)
153240b316d4SVille Syrjälä {
153340b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
153440b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
153540b316d4SVille Syrjälä 
15364da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
153740b316d4SVille Syrjälä 			       RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
153840b316d4SVille Syrjälä }
153940b316d4SVille Syrjälä 
15400fbd8694SVille Syrjälä static bool rkl_ddi_is_clock_enabled(struct intel_encoder *encoder)
15410fbd8694SVille Syrjälä {
15420fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15430fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
15440fbd8694SVille Syrjälä 
15454da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
15460fbd8694SVille Syrjälä 					 RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
15470fbd8694SVille Syrjälä }
15480fbd8694SVille Syrjälä 
1549351221ffSVille Syrjälä static struct intel_shared_dpll *rkl_ddi_get_pll(struct intel_encoder *encoder)
1550351221ffSVille Syrjälä {
1551351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1552351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1553351221ffSVille Syrjälä 
15544da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1555351221ffSVille Syrjälä 				RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1556351221ffSVille Syrjälä 				RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1557351221ffSVille Syrjälä }
1558351221ffSVille Syrjälä 
155935bb6b1aSVille Syrjälä static void dg1_ddi_enable_clock(struct intel_encoder *encoder,
156011ffe972SLucas De Marchi 				 const struct intel_crtc_state *crtc_state)
156111ffe972SLucas De Marchi {
156297a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15639c6a5c35SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
156497a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
156511ffe972SLucas De Marchi 
156697a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1567f67a008eSVille Syrjälä 		return;
1568f67a008eSVille Syrjälä 
156911ffe972SLucas De Marchi 	/*
157011ffe972SLucas De Marchi 	 * If we fail this, something went very wrong: first 2 PLLs should be
157111ffe972SLucas De Marchi 	 * used by first 2 phys and last 2 PLLs by last phys
157211ffe972SLucas De Marchi 	 */
157397a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm,
157411ffe972SLucas De Marchi 			(pll->info->id < DPLL_ID_DG1_DPLL2 && phy >= PHY_C) ||
157511ffe972SLucas De Marchi 			(pll->info->id >= DPLL_ID_DG1_DPLL2 && phy < PHY_C)))
157611ffe972SLucas De Marchi 		return;
157711ffe972SLucas De Marchi 
15784da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
15797815ed88SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
15809c6a5c35SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
15819c6a5c35SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
158211ffe972SLucas De Marchi }
158311ffe972SLucas De Marchi 
158435bb6b1aSVille Syrjälä static void dg1_ddi_disable_clock(struct intel_encoder *encoder)
158535bb6b1aSVille Syrjälä {
158697a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
158797a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
158835bb6b1aSVille Syrjälä 
15894da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
15909c6a5c35SVille Syrjälä 			       DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
159135bb6b1aSVille Syrjälä }
159235bb6b1aSVille Syrjälä 
15930fbd8694SVille Syrjälä static bool dg1_ddi_is_clock_enabled(struct intel_encoder *encoder)
15940fbd8694SVille Syrjälä {
15950fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15960fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
15970fbd8694SVille Syrjälä 
15984da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, DG1_DPCLKA_CFGCR0(phy),
15990fbd8694SVille Syrjälä 					 DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
16000fbd8694SVille Syrjälä }
16010fbd8694SVille Syrjälä 
1602351221ffSVille Syrjälä static struct intel_shared_dpll *dg1_ddi_get_pll(struct intel_encoder *encoder)
1603351221ffSVille Syrjälä {
1604351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1605351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
16063352d86dSJosé Roberto de Souza 	enum intel_dpll_id id;
16073352d86dSJosé Roberto de Souza 	u32 val;
1608351221ffSVille Syrjälä 
16093352d86dSJosé Roberto de Souza 	val = intel_de_read(i915, DG1_DPCLKA_CFGCR0(phy));
16103352d86dSJosé Roberto de Souza 	val &= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
16113352d86dSJosé Roberto de Souza 	val >>= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
16123352d86dSJosé Roberto de Souza 	id = val;
16133352d86dSJosé Roberto de Souza 
16143352d86dSJosé Roberto de Souza 	/*
16153352d86dSJosé Roberto de Souza 	 * _DG1_DPCLKA0_CFGCR0 maps between DPLL 0 and 1 with one bit for phy A
16163352d86dSJosé Roberto de Souza 	 * and B while _DG1_DPCLKA1_CFGCR0 maps between DPLL 2 and 3 with one
16173352d86dSJosé Roberto de Souza 	 * bit for phy C and D.
16183352d86dSJosé Roberto de Souza 	 */
16193352d86dSJosé Roberto de Souza 	if (phy >= PHY_C)
16203352d86dSJosé Roberto de Souza 		id += DPLL_ID_DG1_DPLL2;
16213352d86dSJosé Roberto de Souza 
16223352d86dSJosé Roberto de Souza 	return intel_get_shared_dpll_by_id(i915, id);
1623351221ffSVille Syrjälä }
1624351221ffSVille Syrjälä 
162536ecb0ecSVille Syrjälä static void icl_ddi_combo_enable_clock(struct intel_encoder *encoder,
1626379bc100SJani Nikula 				       const struct intel_crtc_state *crtc_state)
1627379bc100SJani Nikula {
162897a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
16299c6a5c35SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
163097a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1631cd803bb4SMatt Roper 
163297a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1633f67a008eSVille Syrjälä 		return;
1634f67a008eSVille Syrjälä 
16354da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
163640b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
163740b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
163840b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1639379bc100SJani Nikula }
1640379bc100SJani Nikula 
164136ecb0ecSVille Syrjälä static void icl_ddi_combo_disable_clock(struct intel_encoder *encoder)
1642379bc100SJani Nikula {
164397a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
164497a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1645379bc100SJani Nikula 
16464da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
164740b316d4SVille Syrjälä 			       ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1648379bc100SJani Nikula }
1649379bc100SJani Nikula 
16500fbd8694SVille Syrjälä static bool icl_ddi_combo_is_clock_enabled(struct intel_encoder *encoder)
16510fbd8694SVille Syrjälä {
16520fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
16530fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
16540fbd8694SVille Syrjälä 
16554da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
16560fbd8694SVille Syrjälä 					 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
16570fbd8694SVille Syrjälä }
16580fbd8694SVille Syrjälä 
1659351221ffSVille Syrjälä struct intel_shared_dpll *icl_ddi_combo_get_pll(struct intel_encoder *encoder)
1660351221ffSVille Syrjälä {
1661351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1662351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1663351221ffSVille Syrjälä 
16644da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1665351221ffSVille Syrjälä 				ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1666351221ffSVille Syrjälä 				ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1667351221ffSVille Syrjälä }
1668351221ffSVille Syrjälä 
166936ecb0ecSVille Syrjälä static void jsl_ddi_tc_enable_clock(struct intel_encoder *encoder,
1670379bc100SJani Nikula 				    const struct intel_crtc_state *crtc_state)
1671379bc100SJani Nikula {
167236ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1673379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
167436ecb0ecSVille Syrjälä 	enum port port = encoder->port;
1675379bc100SJani Nikula 
167636ecb0ecSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1677379bc100SJani Nikula 		return;
1678379bc100SJani Nikula 
1679c2052d6eSJosé Roberto de Souza 	/*
168036ecb0ecSVille Syrjälä 	 * "For DDIC and DDID, program DDI_CLK_SEL to map the MG clock to the port.
168136ecb0ecSVille Syrjälä 	 *  MG does not exist, but the programming is required to ungate DDIC and DDID."
1682c2052d6eSJosé Roberto de Souza 	 */
168336ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_MG);
168436ecb0ecSVille Syrjälä 
168536ecb0ecSVille Syrjälä 	icl_ddi_combo_enable_clock(encoder, crtc_state);
1686379bc100SJani Nikula }
1687379bc100SJani Nikula 
168836ecb0ecSVille Syrjälä static void jsl_ddi_tc_disable_clock(struct intel_encoder *encoder)
1689379bc100SJani Nikula {
169036ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1691379bc100SJani Nikula 	enum port port = encoder->port;
1692379bc100SJani Nikula 
169336ecb0ecSVille Syrjälä 	icl_ddi_combo_disable_clock(encoder);
169436ecb0ecSVille Syrjälä 
169536ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1696379bc100SJani Nikula }
169736ecb0ecSVille Syrjälä 
16980fbd8694SVille Syrjälä static bool jsl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
16990fbd8694SVille Syrjälä {
17000fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
17010fbd8694SVille Syrjälä 	enum port port = encoder->port;
17020fbd8694SVille Syrjälä 	u32 tmp;
17030fbd8694SVille Syrjälä 
17040fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
17050fbd8694SVille Syrjälä 
17060fbd8694SVille Syrjälä 	if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
17070fbd8694SVille Syrjälä 		return false;
17080fbd8694SVille Syrjälä 
17090fbd8694SVille Syrjälä 	return icl_ddi_combo_is_clock_enabled(encoder);
17100fbd8694SVille Syrjälä }
17110fbd8694SVille Syrjälä 
171236ecb0ecSVille Syrjälä static void icl_ddi_tc_enable_clock(struct intel_encoder *encoder,
171336ecb0ecSVille Syrjälä 				    const struct intel_crtc_state *crtc_state)
171436ecb0ecSVille Syrjälä {
171536ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
171636ecb0ecSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
171736ecb0ecSVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
171836ecb0ecSVille Syrjälä 	enum port port = encoder->port;
171936ecb0ecSVille Syrjälä 
172036ecb0ecSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
172136ecb0ecSVille Syrjälä 		return;
172236ecb0ecSVille Syrjälä 
172336ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port),
172436ecb0ecSVille Syrjälä 		       icl_pll_to_ddi_clk_sel(encoder, crtc_state));
172536ecb0ecSVille Syrjälä 
172636ecb0ecSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
172736ecb0ecSVille Syrjälä 
172836ecb0ecSVille Syrjälä 	intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
172936ecb0ecSVille Syrjälä 		     ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port), 0);
173036ecb0ecSVille Syrjälä 
173136ecb0ecSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
173236ecb0ecSVille Syrjälä }
173336ecb0ecSVille Syrjälä 
173436ecb0ecSVille Syrjälä static void icl_ddi_tc_disable_clock(struct intel_encoder *encoder)
173536ecb0ecSVille Syrjälä {
173636ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
173736ecb0ecSVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
173836ecb0ecSVille Syrjälä 	enum port port = encoder->port;
173936ecb0ecSVille Syrjälä 
174036ecb0ecSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
174136ecb0ecSVille Syrjälä 
174236ecb0ecSVille Syrjälä 	intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
174336ecb0ecSVille Syrjälä 		     0, ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
174436ecb0ecSVille Syrjälä 
174536ecb0ecSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
174636ecb0ecSVille Syrjälä 
174736ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1748379bc100SJani Nikula }
1749379bc100SJani Nikula 
17500fbd8694SVille Syrjälä static bool icl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
17510fbd8694SVille Syrjälä {
17520fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
17530fbd8694SVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
17540fbd8694SVille Syrjälä 	enum port port = encoder->port;
17550fbd8694SVille Syrjälä 	u32 tmp;
17560fbd8694SVille Syrjälä 
17570fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
17580fbd8694SVille Syrjälä 
17590fbd8694SVille Syrjälä 	if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
17600fbd8694SVille Syrjälä 		return false;
17610fbd8694SVille Syrjälä 
17620fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, ICL_DPCLKA_CFGCR0);
17630fbd8694SVille Syrjälä 
17640fbd8694SVille Syrjälä 	return !(tmp & ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
17650fbd8694SVille Syrjälä }
17660fbd8694SVille Syrjälä 
1767351221ffSVille Syrjälä static struct intel_shared_dpll *icl_ddi_tc_get_pll(struct intel_encoder *encoder)
1768351221ffSVille Syrjälä {
1769351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1770351221ffSVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
1771351221ffSVille Syrjälä 	enum port port = encoder->port;
1772351221ffSVille Syrjälä 	enum intel_dpll_id id;
1773351221ffSVille Syrjälä 	u32 tmp;
1774351221ffSVille Syrjälä 
1775351221ffSVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
1776351221ffSVille Syrjälä 
1777351221ffSVille Syrjälä 	switch (tmp & DDI_CLK_SEL_MASK) {
1778351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_162:
1779351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_270:
1780351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_540:
1781351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_810:
1782351221ffSVille Syrjälä 		id = DPLL_ID_ICL_TBTPLL;
1783351221ffSVille Syrjälä 		break;
1784351221ffSVille Syrjälä 	case DDI_CLK_SEL_MG:
1785351221ffSVille Syrjälä 		id = icl_tc_port_to_pll_id(tc_port);
1786351221ffSVille Syrjälä 		break;
1787351221ffSVille Syrjälä 	default:
1788351221ffSVille Syrjälä 		MISSING_CASE(tmp);
1789351221ffSVille Syrjälä 		fallthrough;
1790351221ffSVille Syrjälä 	case DDI_CLK_SEL_NONE:
1791351221ffSVille Syrjälä 		return NULL;
1792351221ffSVille Syrjälä 	}
1793351221ffSVille Syrjälä 
1794351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1795351221ffSVille Syrjälä }
1796351221ffSVille Syrjälä 
1797351221ffSVille Syrjälä static struct intel_shared_dpll *bxt_ddi_get_pll(struct intel_encoder *encoder)
1798351221ffSVille Syrjälä {
1799351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1800351221ffSVille Syrjälä 	enum intel_dpll_id id;
1801351221ffSVille Syrjälä 
1802351221ffSVille Syrjälä 	switch (encoder->port) {
1803351221ffSVille Syrjälä 	case PORT_A:
1804351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL0;
1805351221ffSVille Syrjälä 		break;
1806351221ffSVille Syrjälä 	case PORT_B:
1807351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL1;
1808351221ffSVille Syrjälä 		break;
1809351221ffSVille Syrjälä 	case PORT_C:
1810351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL2;
1811351221ffSVille Syrjälä 		break;
1812351221ffSVille Syrjälä 	default:
1813351221ffSVille Syrjälä 		MISSING_CASE(encoder->port);
1814351221ffSVille Syrjälä 		return NULL;
1815351221ffSVille Syrjälä 	}
1816351221ffSVille Syrjälä 
1817351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1818351221ffSVille Syrjälä }
1819351221ffSVille Syrjälä 
182038e31f1aSVille Syrjälä static void skl_ddi_enable_clock(struct intel_encoder *encoder,
182138e31f1aSVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
182238e31f1aSVille Syrjälä {
182338e31f1aSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
182438e31f1aSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
182538e31f1aSVille Syrjälä 	enum port port = encoder->port;
182638e31f1aSVille Syrjälä 
182738e31f1aSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
182838e31f1aSVille Syrjälä 		return;
182938e31f1aSVille Syrjälä 
183038e31f1aSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
183138e31f1aSVille Syrjälä 
18327815ed88SVille Syrjälä 	intel_de_rmw(i915, DPLL_CTRL2,
18337815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_OFF(port) |
18347815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_SEL_MASK(port),
18357815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_SEL(pll->info->id, port) |
183638e31f1aSVille Syrjälä 		     DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
183738e31f1aSVille Syrjälä 
183838e31f1aSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
183938e31f1aSVille Syrjälä }
184038e31f1aSVille Syrjälä 
184138e31f1aSVille Syrjälä static void skl_ddi_disable_clock(struct intel_encoder *encoder)
184238e31f1aSVille Syrjälä {
184338e31f1aSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
184438e31f1aSVille Syrjälä 	enum port port = encoder->port;
184538e31f1aSVille Syrjälä 
1846be317ca0SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
1847be317ca0SVille Syrjälä 
18487815ed88SVille Syrjälä 	intel_de_rmw(i915, DPLL_CTRL2,
18497815ed88SVille Syrjälä 		     0, DPLL_CTRL2_DDI_CLK_OFF(port));
1850be317ca0SVille Syrjälä 
1851be317ca0SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
185238e31f1aSVille Syrjälä }
185338e31f1aSVille Syrjälä 
18540fbd8694SVille Syrjälä static bool skl_ddi_is_clock_enabled(struct intel_encoder *encoder)
18550fbd8694SVille Syrjälä {
18560fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
18570fbd8694SVille Syrjälä 	enum port port = encoder->port;
18580fbd8694SVille Syrjälä 
18590fbd8694SVille Syrjälä 	/*
18600fbd8694SVille Syrjälä 	 * FIXME Not sure if the override affects both
18610fbd8694SVille Syrjälä 	 * the PLL selection and the CLK_OFF bit.
18620fbd8694SVille Syrjälä 	 */
18630fbd8694SVille Syrjälä 	return !(intel_de_read(i915, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_OFF(port));
18640fbd8694SVille Syrjälä }
18650fbd8694SVille Syrjälä 
1866351221ffSVille Syrjälä static struct intel_shared_dpll *skl_ddi_get_pll(struct intel_encoder *encoder)
1867351221ffSVille Syrjälä {
1868351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1869351221ffSVille Syrjälä 	enum port port = encoder->port;
1870351221ffSVille Syrjälä 	enum intel_dpll_id id;
1871351221ffSVille Syrjälä 	u32 tmp;
1872351221ffSVille Syrjälä 
1873351221ffSVille Syrjälä 	tmp = intel_de_read(i915, DPLL_CTRL2);
1874351221ffSVille Syrjälä 
1875351221ffSVille Syrjälä 	/*
1876351221ffSVille Syrjälä 	 * FIXME Not sure if the override affects both
1877351221ffSVille Syrjälä 	 * the PLL selection and the CLK_OFF bit.
1878351221ffSVille Syrjälä 	 */
1879351221ffSVille Syrjälä 	if ((tmp & DPLL_CTRL2_DDI_SEL_OVERRIDE(port)) == 0)
1880351221ffSVille Syrjälä 		return NULL;
1881351221ffSVille Syrjälä 
1882351221ffSVille Syrjälä 	id = (tmp & DPLL_CTRL2_DDI_CLK_SEL_MASK(port)) >>
1883351221ffSVille Syrjälä 		DPLL_CTRL2_DDI_CLK_SEL_SHIFT(port);
1884351221ffSVille Syrjälä 
1885351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1886351221ffSVille Syrjälä }
1887351221ffSVille Syrjälä 
1888d135368dSVille Syrjälä void hsw_ddi_enable_clock(struct intel_encoder *encoder,
1889d135368dSVille Syrjälä 			  const struct intel_crtc_state *crtc_state)
1890d135368dSVille Syrjälä {
1891d135368dSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1892d135368dSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1893d135368dSVille Syrjälä 	enum port port = encoder->port;
1894d135368dSVille Syrjälä 
1895d135368dSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1896d135368dSVille Syrjälä 		return;
1897d135368dSVille Syrjälä 
1898d135368dSVille Syrjälä 	intel_de_write(i915, PORT_CLK_SEL(port), hsw_pll_to_ddi_pll_sel(pll));
1899d135368dSVille Syrjälä }
1900d135368dSVille Syrjälä 
1901d135368dSVille Syrjälä void hsw_ddi_disable_clock(struct intel_encoder *encoder)
1902d135368dSVille Syrjälä {
1903d135368dSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1904d135368dSVille Syrjälä 	enum port port = encoder->port;
1905d135368dSVille Syrjälä 
1906d135368dSVille Syrjälä 	intel_de_write(i915, PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
1907d135368dSVille Syrjälä }
1908d135368dSVille Syrjälä 
19090fbd8694SVille Syrjälä bool hsw_ddi_is_clock_enabled(struct intel_encoder *encoder)
19100fbd8694SVille Syrjälä {
19110fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
19120fbd8694SVille Syrjälä 	enum port port = encoder->port;
19130fbd8694SVille Syrjälä 
19140fbd8694SVille Syrjälä 	return intel_de_read(i915, PORT_CLK_SEL(port)) != PORT_CLK_SEL_NONE;
19150fbd8694SVille Syrjälä }
19160fbd8694SVille Syrjälä 
1917351221ffSVille Syrjälä static struct intel_shared_dpll *hsw_ddi_get_pll(struct intel_encoder *encoder)
1918351221ffSVille Syrjälä {
1919351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1920351221ffSVille Syrjälä 	enum port port = encoder->port;
1921351221ffSVille Syrjälä 	enum intel_dpll_id id;
1922351221ffSVille Syrjälä 	u32 tmp;
1923351221ffSVille Syrjälä 
1924351221ffSVille Syrjälä 	tmp = intel_de_read(i915, PORT_CLK_SEL(port));
1925351221ffSVille Syrjälä 
1926351221ffSVille Syrjälä 	switch (tmp & PORT_CLK_SEL_MASK) {
1927351221ffSVille Syrjälä 	case PORT_CLK_SEL_WRPLL1:
1928351221ffSVille Syrjälä 		id = DPLL_ID_WRPLL1;
1929351221ffSVille Syrjälä 		break;
1930351221ffSVille Syrjälä 	case PORT_CLK_SEL_WRPLL2:
1931351221ffSVille Syrjälä 		id = DPLL_ID_WRPLL2;
1932351221ffSVille Syrjälä 		break;
1933351221ffSVille Syrjälä 	case PORT_CLK_SEL_SPLL:
1934351221ffSVille Syrjälä 		id = DPLL_ID_SPLL;
1935351221ffSVille Syrjälä 		break;
1936351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_810:
1937351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_810;
1938351221ffSVille Syrjälä 		break;
1939351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_1350:
1940351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_1350;
1941351221ffSVille Syrjälä 		break;
1942351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_2700:
1943351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_2700;
1944351221ffSVille Syrjälä 		break;
1945351221ffSVille Syrjälä 	default:
1946351221ffSVille Syrjälä 		MISSING_CASE(tmp);
1947351221ffSVille Syrjälä 		fallthrough;
1948351221ffSVille Syrjälä 	case PORT_CLK_SEL_NONE:
1949351221ffSVille Syrjälä 		return NULL;
1950351221ffSVille Syrjälä 	}
1951351221ffSVille Syrjälä 
1952351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1953351221ffSVille Syrjälä }
1954351221ffSVille Syrjälä 
1955c133df69SVille Syrjälä void intel_ddi_enable_clock(struct intel_encoder *encoder,
1956c133df69SVille Syrjälä 			    const struct intel_crtc_state *crtc_state)
1957c133df69SVille Syrjälä {
1958c133df69SVille Syrjälä 	if (encoder->enable_clock)
1959c133df69SVille Syrjälä 		encoder->enable_clock(encoder, crtc_state);
1960c133df69SVille Syrjälä }
1961c133df69SVille Syrjälä 
1962d39ef5d5SVille Syrjälä void intel_ddi_disable_clock(struct intel_encoder *encoder)
1963c133df69SVille Syrjälä {
1964c133df69SVille Syrjälä 	if (encoder->disable_clock)
1965c133df69SVille Syrjälä 		encoder->disable_clock(encoder);
1966c133df69SVille Syrjälä }
1967c133df69SVille Syrjälä 
1968aaca50efSVille Syrjälä void intel_ddi_sanitize_encoder_pll_mapping(struct intel_encoder *encoder)
1969dc1ddac6SVille Syrjälä {
197097a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1971dc1ddac6SVille Syrjälä 	u32 port_mask;
1972dc1ddac6SVille Syrjälä 	bool ddi_clk_needed;
1973dc1ddac6SVille Syrjälä 
1974dc1ddac6SVille Syrjälä 	/*
1975dc1ddac6SVille Syrjälä 	 * In case of DP MST, we sanitize the primary encoder only, not the
1976dc1ddac6SVille Syrjälä 	 * virtual ones.
1977dc1ddac6SVille Syrjälä 	 */
1978dc1ddac6SVille Syrjälä 	if (encoder->type == INTEL_OUTPUT_DP_MST)
1979dc1ddac6SVille Syrjälä 		return;
1980dc1ddac6SVille Syrjälä 
1981dc1ddac6SVille Syrjälä 	if (!encoder->base.crtc && intel_encoder_is_dp(encoder)) {
1982dc1ddac6SVille Syrjälä 		u8 pipe_mask;
1983dc1ddac6SVille Syrjälä 		bool is_mst;
1984dc1ddac6SVille Syrjälä 
1985dc1ddac6SVille Syrjälä 		intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
1986dc1ddac6SVille Syrjälä 		/*
1987dc1ddac6SVille Syrjälä 		 * In the unlikely case that BIOS enables DP in MST mode, just
1988dc1ddac6SVille Syrjälä 		 * warn since our MST HW readout is incomplete.
1989dc1ddac6SVille Syrjälä 		 */
199097a24a70SVille Syrjälä 		if (drm_WARN_ON(&i915->drm, is_mst))
1991dc1ddac6SVille Syrjälä 			return;
1992dc1ddac6SVille Syrjälä 	}
1993dc1ddac6SVille Syrjälä 
1994dc1ddac6SVille Syrjälä 	port_mask = BIT(encoder->port);
1995dc1ddac6SVille Syrjälä 	ddi_clk_needed = encoder->base.crtc;
1996dc1ddac6SVille Syrjälä 
1997dc1ddac6SVille Syrjälä 	if (encoder->type == INTEL_OUTPUT_DSI) {
1998dc1ddac6SVille Syrjälä 		struct intel_encoder *other_encoder;
1999dc1ddac6SVille Syrjälä 
2000dc1ddac6SVille Syrjälä 		port_mask = intel_dsi_encoder_ports(encoder);
2001dc1ddac6SVille Syrjälä 		/*
2002dc1ddac6SVille Syrjälä 		 * Sanity check that we haven't incorrectly registered another
2003dc1ddac6SVille Syrjälä 		 * encoder using any of the ports of this DSI encoder.
2004dc1ddac6SVille Syrjälä 		 */
200597a24a70SVille Syrjälä 		for_each_intel_encoder(&i915->drm, other_encoder) {
2006dc1ddac6SVille Syrjälä 			if (other_encoder == encoder)
2007dc1ddac6SVille Syrjälä 				continue;
2008dc1ddac6SVille Syrjälä 
200997a24a70SVille Syrjälä 			if (drm_WARN_ON(&i915->drm,
2010dc1ddac6SVille Syrjälä 					port_mask & BIT(other_encoder->port)))
2011dc1ddac6SVille Syrjälä 				return;
2012dc1ddac6SVille Syrjälä 		}
2013dc1ddac6SVille Syrjälä 		/*
2014dc1ddac6SVille Syrjälä 		 * For DSI we keep the ddi clocks gated
2015dc1ddac6SVille Syrjälä 		 * except during enable/disable sequence.
2016dc1ddac6SVille Syrjälä 		 */
2017dc1ddac6SVille Syrjälä 		ddi_clk_needed = false;
2018dc1ddac6SVille Syrjälä 	}
2019dc1ddac6SVille Syrjälä 
2020f82f2563SMatt Roper 	if (ddi_clk_needed || !encoder->is_clock_enabled ||
20210fbd8694SVille Syrjälä 	    !encoder->is_clock_enabled(encoder))
20220fbd8694SVille Syrjälä 		return;
20230fbd8694SVille Syrjälä 
20240fbd8694SVille Syrjälä 	drm_notice(&i915->drm,
20250fbd8694SVille Syrjälä 		   "[ENCODER:%d:%s] is disabled/in DSI mode with an ungated DDI clock, gate it\n",
20260fbd8694SVille Syrjälä 		   encoder->base.base.id, encoder->base.name);
20270fbd8694SVille Syrjälä 
2028dc1ddac6SVille Syrjälä 	encoder->disable_clock(encoder);
2029dc1ddac6SVille Syrjälä }
2030dc1ddac6SVille Syrjälä 
20318aaf5cbdSJosé Roberto de Souza static void
20327801f3b7SLucas De Marchi icl_program_mg_dp_mode(struct intel_digital_port *dig_port,
20333b51be4eSClinton A Taylor 		       const struct intel_crtc_state *crtc_state)
2034379bc100SJani Nikula {
20357801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
20367801f3b7SLucas De Marchi 	enum tc_port tc_port = intel_port_to_tc(dev_priv, dig_port->base.port);
20375b6a9ba9SVille Syrjälä 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
20383b51be4eSClinton A Taylor 	u32 ln0, ln1, pin_assignment;
20393b51be4eSClinton A Taylor 	u8 width;
2040379bc100SJani Nikula 
20415b6a9ba9SVille Syrjälä 	if (!intel_phy_is_tc(dev_priv, phy) ||
204211a89708SImre Deak 	    intel_tc_port_in_tbt_alt_mode(dig_port))
2043379bc100SJani Nikula 		return;
2044379bc100SJani Nikula 
2045005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2046f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2047f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
2048f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
2049f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2050f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
2051f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
2052978c3e53SClinton A Taylor 	} else {
2053f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, MG_DP_MODE(0, tc_port));
2054f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, MG_DP_MODE(1, tc_port));
2055978c3e53SClinton A Taylor 	}
2056379bc100SJani Nikula 
20574f72a8eeSKhaled Almahallawy 	ln0 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2058379bc100SJani Nikula 	ln1 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2059379bc100SJani Nikula 
20603b51be4eSClinton A Taylor 	/* DPPATC */
20617801f3b7SLucas De Marchi 	pin_assignment = intel_tc_port_get_pin_assignment_mask(dig_port);
20623b51be4eSClinton A Taylor 	width = crtc_state->lane_count;
2063379bc100SJani Nikula 
20643b51be4eSClinton A Taylor 	switch (pin_assignment) {
20653b51be4eSClinton A Taylor 	case 0x0:
20661de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
206711a89708SImre Deak 			    !intel_tc_port_in_legacy_mode(dig_port));
20683b51be4eSClinton A Taylor 		if (width == 1) {
2069379bc100SJani Nikula 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
20703b51be4eSClinton A Taylor 		} else {
20713b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20723b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2073379bc100SJani Nikula 		}
2074379bc100SJani Nikula 		break;
20753b51be4eSClinton A Taylor 	case 0x1:
20763b51be4eSClinton A Taylor 		if (width == 4) {
20773b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20783b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20793b51be4eSClinton A Taylor 		}
2080379bc100SJani Nikula 		break;
20813b51be4eSClinton A Taylor 	case 0x2:
20823b51be4eSClinton A Taylor 		if (width == 2) {
20833b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20843b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20853b51be4eSClinton A Taylor 		}
20863b51be4eSClinton A Taylor 		break;
20873b51be4eSClinton A Taylor 	case 0x3:
20883b51be4eSClinton A Taylor 	case 0x5:
20893b51be4eSClinton A Taylor 		if (width == 1) {
20903b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
20913b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
20923b51be4eSClinton A Taylor 		} else {
20933b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20943b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20953b51be4eSClinton A Taylor 		}
20963b51be4eSClinton A Taylor 		break;
20973b51be4eSClinton A Taylor 	case 0x4:
20983b51be4eSClinton A Taylor 	case 0x6:
20993b51be4eSClinton A Taylor 		if (width == 1) {
21003b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
21013b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
21023b51be4eSClinton A Taylor 		} else {
21033b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
21043b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
21053b51be4eSClinton A Taylor 		}
21063b51be4eSClinton A Taylor 		break;
2107379bc100SJani Nikula 	default:
21083b51be4eSClinton A Taylor 		MISSING_CASE(pin_assignment);
2109379bc100SJani Nikula 	}
2110379bc100SJani Nikula 
2111005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2112f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2113f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
2114f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln0);
2115f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2116f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
2117f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln1);
2118978c3e53SClinton A Taylor 	} else {
2119f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(0, tc_port), ln0);
2120f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(1, tc_port), ln1);
2121379bc100SJani Nikula 	}
2122978c3e53SClinton A Taylor }
2123379bc100SJani Nikula 
2124ef79fafeSVille Syrjälä static enum transcoder
2125ef79fafeSVille Syrjälä tgl_dp_tp_transcoder(const struct intel_crtc_state *crtc_state)
2126ef79fafeSVille Syrjälä {
2127ef79fafeSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
2128ef79fafeSVille Syrjälä 		return crtc_state->mst_master_transcoder;
2129ef79fafeSVille Syrjälä 	else
2130ef79fafeSVille Syrjälä 		return crtc_state->cpu_transcoder;
2131ef79fafeSVille Syrjälä }
2132ef79fafeSVille Syrjälä 
2133ef79fafeSVille Syrjälä i915_reg_t dp_tp_ctl_reg(struct intel_encoder *encoder,
2134ef79fafeSVille Syrjälä 			 const struct intel_crtc_state *crtc_state)
2135ef79fafeSVille Syrjälä {
2136ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2137ef79fafeSVille Syrjälä 
2138005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
2139ef79fafeSVille Syrjälä 		return TGL_DP_TP_CTL(tgl_dp_tp_transcoder(crtc_state));
2140ef79fafeSVille Syrjälä 	else
2141ef79fafeSVille Syrjälä 		return DP_TP_CTL(encoder->port);
2142ef79fafeSVille Syrjälä }
2143ef79fafeSVille Syrjälä 
2144ef79fafeSVille Syrjälä i915_reg_t dp_tp_status_reg(struct intel_encoder *encoder,
2145ef79fafeSVille Syrjälä 			    const struct intel_crtc_state *crtc_state)
2146ef79fafeSVille Syrjälä {
2147ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2148ef79fafeSVille Syrjälä 
2149005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
2150ef79fafeSVille Syrjälä 		return TGL_DP_TP_STATUS(tgl_dp_tp_transcoder(crtc_state));
2151ef79fafeSVille Syrjälä 	else
2152ef79fafeSVille Syrjälä 		return DP_TP_STATUS(encoder->port);
2153ef79fafeSVille Syrjälä }
2154ef79fafeSVille Syrjälä 
21551639406aSManasi Navare static void intel_dp_sink_set_msa_timing_par_ignore_state(struct intel_dp *intel_dp,
21561639406aSManasi Navare 							  const struct intel_crtc_state *crtc_state,
21571639406aSManasi Navare 							  bool enable)
21581639406aSManasi Navare {
21591639406aSManasi Navare 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
21601639406aSManasi Navare 
21611639406aSManasi Navare 	if (!crtc_state->vrr.enable)
21621639406aSManasi Navare 		return;
21631639406aSManasi Navare 
21641639406aSManasi Navare 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_DOWNSPREAD_CTRL,
21651639406aSManasi Navare 			       enable ? DP_MSA_TIMING_PAR_IGNORE_EN : 0) <= 0)
21661639406aSManasi Navare 		drm_dbg_kms(&i915->drm,
21670868b1ceSVille Syrjälä 			    "Failed to %s MSA_TIMING_PAR_IGNORE in the sink\n",
21680868b1ceSVille Syrjälä 			    enabledisable(enable));
21691639406aSManasi Navare }
21701639406aSManasi Navare 
2171379bc100SJani Nikula static void intel_dp_sink_set_fec_ready(struct intel_dp *intel_dp,
2172379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
2173379bc100SJani Nikula {
217447bdb1caSJani Nikula 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
217547bdb1caSJani Nikula 
2176379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2177379bc100SJani Nikula 		return;
2178379bc100SJani Nikula 
2179379bc100SJani Nikula 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_FEC_CONFIGURATION, DP_FEC_READY) <= 0)
218047bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
218147bdb1caSJani Nikula 			    "Failed to set FEC_READY in the sink\n");
2182379bc100SJani Nikula }
2183379bc100SJani Nikula 
2184379bc100SJani Nikula static void intel_ddi_enable_fec(struct intel_encoder *encoder,
2185379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
2186379bc100SJani Nikula {
2187379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
21884444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
2189379bc100SJani Nikula 	u32 val;
2190379bc100SJani Nikula 
2191379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2192379bc100SJani Nikula 		return;
2193379bc100SJani Nikula 
2194b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
2195ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2196379bc100SJani Nikula 	val |= DP_TP_CTL_FEC_ENABLE;
2197ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2198379bc100SJani Nikula }
2199379bc100SJani Nikula 
2200379bc100SJani Nikula static void intel_ddi_disable_fec_state(struct intel_encoder *encoder,
2201379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
2202379bc100SJani Nikula {
2203379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
22044444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
2205379bc100SJani Nikula 	u32 val;
2206379bc100SJani Nikula 
2207379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2208379bc100SJani Nikula 		return;
2209379bc100SJani Nikula 
2210b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
2211ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2212379bc100SJani Nikula 	val &= ~DP_TP_CTL_FEC_ENABLE;
2213ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2214ef79fafeSVille Syrjälä 	intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2215379bc100SJani Nikula }
2216379bc100SJani Nikula 
22175cdf706fSVille Syrjälä static void intel_ddi_power_up_lanes(struct intel_encoder *encoder,
22185cdf706fSVille Syrjälä 				     const struct intel_crtc_state *crtc_state)
22195cdf706fSVille Syrjälä {
22205cdf706fSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
22215cdf706fSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
22225cdf706fSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
22235cdf706fSVille Syrjälä 
22245cdf706fSVille Syrjälä 	if (intel_phy_is_combo(i915, phy)) {
22255cdf706fSVille Syrjälä 		bool lane_reversal =
22265cdf706fSVille Syrjälä 			dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
22275cdf706fSVille Syrjälä 
22285cdf706fSVille Syrjälä 		intel_combo_phy_power_up_lanes(i915, phy, false,
22295cdf706fSVille Syrjälä 					       crtc_state->lane_count,
22305cdf706fSVille Syrjälä 					       lane_reversal);
22315cdf706fSVille Syrjälä 	}
22325cdf706fSVille Syrjälä }
22335cdf706fSVille Syrjälä 
2234f6864b27SJani Nikula /* Splitter enable for eDP MSO is limited to certain pipes. */
2235f6864b27SJani Nikula static u8 intel_ddi_splitter_pipe_mask(struct drm_i915_private *i915)
2236f6864b27SJani Nikula {
2237f6864b27SJani Nikula 	if (IS_ALDERLAKE_P(i915))
2238f6864b27SJani Nikula 		return BIT(PIPE_A) | BIT(PIPE_B);
2239f6864b27SJani Nikula 	else
2240f6864b27SJani Nikula 		return BIT(PIPE_A);
2241f6864b27SJani Nikula }
2242f6864b27SJani Nikula 
22435b616a29SJani Nikula static void intel_ddi_mso_get_config(struct intel_encoder *encoder,
22445b616a29SJani Nikula 				     struct intel_crtc_state *pipe_config)
22455b616a29SJani Nikula {
22465b616a29SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
22475b616a29SJani Nikula 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
22485b616a29SJani Nikula 	enum pipe pipe = crtc->pipe;
22495b616a29SJani Nikula 	u32 dss1;
22505b616a29SJani Nikula 
22515b616a29SJani Nikula 	if (!HAS_MSO(i915))
22525b616a29SJani Nikula 		return;
22535b616a29SJani Nikula 
22545b616a29SJani Nikula 	dss1 = intel_de_read(i915, ICL_PIPE_DSS_CTL1(pipe));
22555b616a29SJani Nikula 
22565b616a29SJani Nikula 	pipe_config->splitter.enable = dss1 & SPLITTER_ENABLE;
22575b616a29SJani Nikula 	if (!pipe_config->splitter.enable)
22585b616a29SJani Nikula 		return;
22595b616a29SJani Nikula 
2260f6864b27SJani Nikula 	if (drm_WARN_ON(&i915->drm, !(intel_ddi_splitter_pipe_mask(i915) & BIT(pipe)))) {
22615b616a29SJani Nikula 		pipe_config->splitter.enable = false;
22625b616a29SJani Nikula 		return;
22635b616a29SJani Nikula 	}
22645b616a29SJani Nikula 
22655b616a29SJani Nikula 	switch (dss1 & SPLITTER_CONFIGURATION_MASK) {
22665b616a29SJani Nikula 	default:
22675b616a29SJani Nikula 		drm_WARN(&i915->drm, true,
22685b616a29SJani Nikula 			 "Invalid splitter configuration, dss1=0x%08x\n", dss1);
22695b616a29SJani Nikula 		fallthrough;
22705b616a29SJani Nikula 	case SPLITTER_CONFIGURATION_2_SEGMENT:
22715b616a29SJani Nikula 		pipe_config->splitter.link_count = 2;
22725b616a29SJani Nikula 		break;
22735b616a29SJani Nikula 	case SPLITTER_CONFIGURATION_4_SEGMENT:
22745b616a29SJani Nikula 		pipe_config->splitter.link_count = 4;
22755b616a29SJani Nikula 		break;
22765b616a29SJani Nikula 	}
22775b616a29SJani Nikula 
22785b616a29SJani Nikula 	pipe_config->splitter.pixel_overlap = REG_FIELD_GET(OVERLAP_PIXELS_MASK, dss1);
22795b616a29SJani Nikula }
22805b616a29SJani Nikula 
2281bc71194eSJani Nikula static void intel_ddi_mso_configure(const struct intel_crtc_state *crtc_state)
2282bc71194eSJani Nikula {
2283bc71194eSJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2284bc71194eSJani Nikula 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2285bc71194eSJani Nikula 	enum pipe pipe = crtc->pipe;
2286bc71194eSJani Nikula 	u32 dss1 = 0;
2287bc71194eSJani Nikula 
2288bc71194eSJani Nikula 	if (!HAS_MSO(i915))
2289bc71194eSJani Nikula 		return;
2290bc71194eSJani Nikula 
2291bc71194eSJani Nikula 	if (crtc_state->splitter.enable) {
2292bc71194eSJani Nikula 		dss1 |= SPLITTER_ENABLE;
2293bc71194eSJani Nikula 		dss1 |= OVERLAP_PIXELS(crtc_state->splitter.pixel_overlap);
2294bc71194eSJani Nikula 		if (crtc_state->splitter.link_count == 2)
2295bc71194eSJani Nikula 			dss1 |= SPLITTER_CONFIGURATION_2_SEGMENT;
2296bc71194eSJani Nikula 		else
2297bc71194eSJani Nikula 			dss1 |= SPLITTER_CONFIGURATION_4_SEGMENT;
2298bc71194eSJani Nikula 	}
2299bc71194eSJani Nikula 
2300bc71194eSJani Nikula 	intel_de_rmw(i915, ICL_PIPE_DSS_CTL1(pipe),
2301bc71194eSJani Nikula 		     SPLITTER_ENABLE | SPLITTER_CONFIGURATION_MASK |
2302bc71194eSJani Nikula 		     OVERLAP_PIXELS_MASK, dss1);
2303bc71194eSJani Nikula }
2304bc71194eSJani Nikula 
2305f82f2563SMatt Roper static void dg2_ddi_pre_enable_dp(struct intel_atomic_state *state,
2306f82f2563SMatt Roper 				  struct intel_encoder *encoder,
2307f82f2563SMatt Roper 				  const struct intel_crtc_state *crtc_state,
2308f82f2563SMatt Roper 				  const struct drm_connector_state *conn_state)
2309f82f2563SMatt Roper {
2310f82f2563SMatt Roper 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2311f82f2563SMatt Roper 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2312f82f2563SMatt Roper 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2313f82f2563SMatt Roper 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2314f82f2563SMatt Roper 
2315f82f2563SMatt Roper 	intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
2316f82f2563SMatt Roper 				 crtc_state->lane_count);
2317f82f2563SMatt Roper 
2318f82f2563SMatt Roper 	/*
23199f620f1dSVille Syrjälä 	 * We only configure what the register value will be here.  Actual
23209f620f1dSVille Syrjälä 	 * enabling happens during link training farther down.
23219f620f1dSVille Syrjälä 	 */
23229f620f1dSVille Syrjälä 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
23239f620f1dSVille Syrjälä 
23249f620f1dSVille Syrjälä 	/*
2325f82f2563SMatt Roper 	 * 1. Enable Power Wells
2326f82f2563SMatt Roper 	 *
2327f82f2563SMatt Roper 	 * This was handled at the beginning of intel_atomic_commit_tail(),
2328f82f2563SMatt Roper 	 * before we called down into this function.
2329f82f2563SMatt Roper 	 */
2330f82f2563SMatt Roper 
2331f82f2563SMatt Roper 	/* 2. Enable Panel Power if PPS is required */
2332f82f2563SMatt Roper 	intel_pps_on(intel_dp);
2333f82f2563SMatt Roper 
2334f82f2563SMatt Roper 	/*
2335f82f2563SMatt Roper 	 * 3. Enable the port PLL.
2336f82f2563SMatt Roper 	 */
2337f82f2563SMatt Roper 	intel_ddi_enable_clock(encoder, crtc_state);
2338f82f2563SMatt Roper 
2339f82f2563SMatt Roper 	/* 4. Enable IO power */
234011a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port))
2341f82f2563SMatt Roper 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2342f82f2563SMatt Roper 								   dig_port->ddi_io_power_domain);
2343f82f2563SMatt Roper 
2344f82f2563SMatt Roper 	/*
2345f82f2563SMatt Roper 	 * 5. The rest of the below are substeps under the bspec's "Enable and
2346f82f2563SMatt Roper 	 * Train Display Port" step.  Note that steps that are specific to
2347f82f2563SMatt Roper 	 * MST will be handled by intel_mst_pre_enable_dp() before/after it
2348f82f2563SMatt Roper 	 * calls into this function.  Also intel_mst_pre_enable_dp() only calls
2349f82f2563SMatt Roper 	 * us when active_mst_links==0, so any steps designated for "single
2350f82f2563SMatt Roper 	 * stream or multi-stream master transcoder" can just be performed
2351f82f2563SMatt Roper 	 * unconditionally here.
2352f82f2563SMatt Roper 	 */
2353f82f2563SMatt Roper 
2354f82f2563SMatt Roper 	/*
2355f82f2563SMatt Roper 	 * 5.a Configure Transcoder Clock Select to direct the Port clock to the
2356f82f2563SMatt Roper 	 * Transcoder.
2357f82f2563SMatt Roper 	 */
2358f82f2563SMatt Roper 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
2359f82f2563SMatt Roper 
236079ac2b1bSJani Nikula 	/* 5.b Configure transcoder for DP 2.0 128b/132b */
236179ac2b1bSJani Nikula 	intel_ddi_config_transcoder_dp2(encoder, crtc_state);
2362f82f2563SMatt Roper 
2363f82f2563SMatt Roper 	/*
2364f82f2563SMatt Roper 	 * 5.c Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
2365f82f2563SMatt Roper 	 * Transport Select
2366f82f2563SMatt Roper 	 */
2367f82f2563SMatt Roper 	intel_ddi_config_transcoder_func(encoder, crtc_state);
2368f82f2563SMatt Roper 
2369f82f2563SMatt Roper 	/*
2370f82f2563SMatt Roper 	 * 5.d Configure & enable DP_TP_CTL with link training pattern 1
2371f82f2563SMatt Roper 	 * selected
2372f82f2563SMatt Roper 	 *
2373f82f2563SMatt Roper 	 * This will be handled by the intel_dp_start_link_train() farther
2374f82f2563SMatt Roper 	 * down this function.
2375f82f2563SMatt Roper 	 */
2376f82f2563SMatt Roper 
2377f82f2563SMatt Roper 	/* 5.e Configure voltage swing and related IO settings */
2378e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
2379f82f2563SMatt Roper 
2380f82f2563SMatt Roper 	if (!is_mst)
2381f82f2563SMatt Roper 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2382f82f2563SMatt Roper 
238301da701bSAnkit Nautiyal 	intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2384f82f2563SMatt Roper 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
2385f82f2563SMatt Roper 	/*
2386f82f2563SMatt Roper 	 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
2387f82f2563SMatt Roper 	 * in the FEC_CONFIGURATION register to 1 before initiating link
2388f82f2563SMatt Roper 	 * training
2389f82f2563SMatt Roper 	 */
2390f82f2563SMatt Roper 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
239101da701bSAnkit Nautiyal 	intel_dp_check_frl_training(intel_dp);
239201da701bSAnkit Nautiyal 	intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
2393f82f2563SMatt Roper 
2394f82f2563SMatt Roper 	/*
2395f82f2563SMatt Roper 	 * 5.h Follow DisplayPort specification training sequence (see notes for
2396f82f2563SMatt Roper 	 *     failure handling)
2397f82f2563SMatt Roper 	 * 5.i If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
2398f82f2563SMatt Roper 	 *     Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
2399f82f2563SMatt Roper 	 *     (timeout after 800 us)
2400f82f2563SMatt Roper 	 */
2401f82f2563SMatt Roper 	intel_dp_start_link_train(intel_dp, crtc_state);
2402f82f2563SMatt Roper 
2403f82f2563SMatt Roper 	/* 5.j Set DP_TP_CTL link training to Normal */
2404f82f2563SMatt Roper 	if (!is_trans_port_sync_mode(crtc_state))
2405f82f2563SMatt Roper 		intel_dp_stop_link_train(intel_dp, crtc_state);
2406f82f2563SMatt Roper 
2407f82f2563SMatt Roper 	/* 5.k Configure and enable FEC if needed */
2408f82f2563SMatt Roper 	intel_ddi_enable_fec(encoder, crtc_state);
24093126977dSVille Syrjälä 
24103126977dSVille Syrjälä 	intel_dsc_dp_pps_write(encoder, crtc_state);
24113126977dSVille Syrjälä 
24123126977dSVille Syrjälä 	intel_dsc_enable(crtc_state);
2413f82f2563SMatt Roper }
2414f82f2563SMatt Roper 
2415ede9771dSVille Syrjälä static void tgl_ddi_pre_enable_dp(struct intel_atomic_state *state,
2416ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
241799389390SJosé Roberto de Souza 				  const struct intel_crtc_state *crtc_state,
241899389390SJosé Roberto de Souza 				  const struct drm_connector_state *conn_state)
241999389390SJosé Roberto de Souza {
2420b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
242199389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2422b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
242399389390SJosé Roberto de Souza 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
242499389390SJosé Roberto de Souza 
2425a621860aSVille Syrjälä 	intel_dp_set_link_params(intel_dp,
2426a621860aSVille Syrjälä 				 crtc_state->port_clock,
2427a621860aSVille Syrjälä 				 crtc_state->lane_count);
242899389390SJosé Roberto de Souza 
24295e19c0b0SMatt Roper 	/*
24309f620f1dSVille Syrjälä 	 * We only configure what the register value will be here.  Actual
24319f620f1dSVille Syrjälä 	 * enabling happens during link training farther down.
24329f620f1dSVille Syrjälä 	 */
24339f620f1dSVille Syrjälä 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
24349f620f1dSVille Syrjälä 
24359f620f1dSVille Syrjälä 	/*
24365e19c0b0SMatt Roper 	 * 1. Enable Power Wells
24375e19c0b0SMatt Roper 	 *
24385e19c0b0SMatt Roper 	 * This was handled at the beginning of intel_atomic_commit_tail(),
24395e19c0b0SMatt Roper 	 * before we called down into this function.
24405e19c0b0SMatt Roper 	 */
244199389390SJosé Roberto de Souza 
24425e19c0b0SMatt Roper 	/* 2. Enable Panel Power if PPS is required */
2443eb46f498SJani Nikula 	intel_pps_on(intel_dp);
244499389390SJosé Roberto de Souza 
244599389390SJosé Roberto de Souza 	/*
24465e19c0b0SMatt Roper 	 * 3. For non-TBT Type-C ports, set FIA lane count
24475e19c0b0SMatt Roper 	 * (DFLEXDPSP.DPX4TXLATC)
24485e19c0b0SMatt Roper 	 *
24495e19c0b0SMatt Roper 	 * This was done before tgl_ddi_pre_enable_dp by
24501e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_encoders_pre_pll_enable().
245199389390SJosé Roberto de Souza 	 */
245299389390SJosé Roberto de Souza 
24535e19c0b0SMatt Roper 	/*
24545e19c0b0SMatt Roper 	 * 4. Enable the port PLL.
24555e19c0b0SMatt Roper 	 *
24565e19c0b0SMatt Roper 	 * The PLL enabling itself was already done before this function by
24571e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_enable_shared_dpll().  We need only
24585e19c0b0SMatt Roper 	 * configure the PLL to port mapping here.
24595e19c0b0SMatt Roper 	 */
2460c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
24616171e58bSClinton A Taylor 
24625e19c0b0SMatt Roper 	/* 5. If IO power is controlled through PWR_WELL_CTL, Enable IO Power */
246311a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
2464a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2465a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
246699389390SJosé Roberto de Souza 								   dig_port->ddi_io_power_domain);
2467a4550977SImre Deak 	}
246899389390SJosé Roberto de Souza 
24695e19c0b0SMatt Roper 	/* 6. Program DP_MODE */
24703b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
247199389390SJosé Roberto de Souza 
247299389390SJosé Roberto de Souza 	/*
24735e19c0b0SMatt Roper 	 * 7. The rest of the below are substeps under the bspec's "Enable and
24745e19c0b0SMatt Roper 	 * Train Display Port" step.  Note that steps that are specific to
24755e19c0b0SMatt Roper 	 * MST will be handled by intel_mst_pre_enable_dp() before/after it
24765e19c0b0SMatt Roper 	 * calls into this function.  Also intel_mst_pre_enable_dp() only calls
24775e19c0b0SMatt Roper 	 * us when active_mst_links==0, so any steps designated for "single
24785e19c0b0SMatt Roper 	 * stream or multi-stream master transcoder" can just be performed
24795e19c0b0SMatt Roper 	 * unconditionally here.
24805e19c0b0SMatt Roper 	 */
24815e19c0b0SMatt Roper 
24825e19c0b0SMatt Roper 	/*
24835e19c0b0SMatt Roper 	 * 7.a Configure Transcoder Clock Select to direct the Port clock to the
24845e19c0b0SMatt Roper 	 * Transcoder.
248599389390SJosé Roberto de Souza 	 */
248602a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
248799389390SJosé Roberto de Souza 
24885e19c0b0SMatt Roper 	/*
24895e19c0b0SMatt Roper 	 * 7.b Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
24905e19c0b0SMatt Roper 	 * Transport Select
24915e19c0b0SMatt Roper 	 */
2492eed22a46SVille Syrjälä 	intel_ddi_config_transcoder_func(encoder, crtc_state);
249399389390SJosé Roberto de Souza 
24945e19c0b0SMatt Roper 	/*
24955e19c0b0SMatt Roper 	 * 7.c Configure & enable DP_TP_CTL with link training pattern 1
24965e19c0b0SMatt Roper 	 * selected
24975e19c0b0SMatt Roper 	 *
24985e19c0b0SMatt Roper 	 * This will be handled by the intel_dp_start_link_train() farther
24995e19c0b0SMatt Roper 	 * down this function.
25005e19c0b0SMatt Roper 	 */
25015e19c0b0SMatt Roper 
25025e19c0b0SMatt Roper 	/* 7.e Configure voltage swing and related IO settings */
2503e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
250499389390SJosé Roberto de Souza 
25055e19c0b0SMatt Roper 	/*
25065e19c0b0SMatt Roper 	 * 7.f Combo PHY: Configure PORT_CL_DW10 Static Power Down to power up
25075e19c0b0SMatt Roper 	 * the used lanes of the DDI.
25085e19c0b0SMatt Roper 	 */
25095cdf706fSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
251099389390SJosé Roberto de Souza 
25115e19c0b0SMatt Roper 	/*
2512bc71194eSJani Nikula 	 * 7.g Program CoG/MSO configuration bits in DSS_CTL1 if selected.
2513bc71194eSJani Nikula 	 */
2514bc71194eSJani Nikula 	intel_ddi_mso_configure(crtc_state);
2515bc71194eSJani Nikula 
251699389390SJosé Roberto de Souza 	if (!is_mst)
25170e634efdSVille Syrjälä 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
251899389390SJosé Roberto de Souza 
2519522508b6SAnkit Nautiyal 	intel_dp_configure_protocol_converter(intel_dp, crtc_state);
252099389390SJosé Roberto de Souza 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
252199389390SJosé Roberto de Souza 	/*
252299389390SJosé Roberto de Souza 	 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
252399389390SJosé Roberto de Souza 	 * in the FEC_CONFIGURATION register to 1 before initiating link
252499389390SJosé Roberto de Souza 	 * training
252599389390SJosé Roberto de Souza 	 */
252699389390SJosé Roberto de Souza 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
25275e19c0b0SMatt Roper 
25284f3dd47aSAnkit Nautiyal 	intel_dp_check_frl_training(intel_dp);
252910fec80bSAnkit Nautiyal 	intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
25304f3dd47aSAnkit Nautiyal 
25315e19c0b0SMatt Roper 	/*
25325e19c0b0SMatt Roper 	 * 7.i Follow DisplayPort specification training sequence (see notes for
25335e19c0b0SMatt Roper 	 *     failure handling)
25345e19c0b0SMatt Roper 	 * 7.j If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
25355e19c0b0SMatt Roper 	 *     Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
25365e19c0b0SMatt Roper 	 *     (timeout after 800 us)
25375e19c0b0SMatt Roper 	 */
2538a621860aSVille Syrjälä 	intel_dp_start_link_train(intel_dp, crtc_state);
253999389390SJosé Roberto de Souza 
25405e19c0b0SMatt Roper 	/* 7.k Set DP_TP_CTL link training to Normal */
2541eadf6f91SManasi Navare 	if (!is_trans_port_sync_mode(crtc_state))
2542a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
254399389390SJosé Roberto de Souza 
25445e19c0b0SMatt Roper 	/* 7.l Configure and enable FEC if needed */
254599389390SJosé Roberto de Souza 	intel_ddi_enable_fec(encoder, crtc_state);
25463126977dSVille Syrjälä 
25473126977dSVille Syrjälä 	intel_dsc_dp_pps_write(encoder, crtc_state);
25483126977dSVille Syrjälä 
25494e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner)
25503126977dSVille Syrjälä 		intel_dsc_enable(crtc_state);
255199389390SJosé Roberto de Souza }
255299389390SJosé Roberto de Souza 
2553ede9771dSVille Syrjälä static void hsw_ddi_pre_enable_dp(struct intel_atomic_state *state,
2554ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
2555379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
2556379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
2557379bc100SJani Nikula {
2558b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2559379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2560379bc100SJani Nikula 	enum port port = encoder->port;
2561b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2562379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2563379bc100SJani Nikula 
2564005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 11)
25651de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
25661de143ccSPankaj Bharadiya 			    is_mst && (port == PORT_A || port == PORT_E));
2567542dfab5SJosé Roberto de Souza 	else
25681de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm, is_mst && port == PORT_A);
2569379bc100SJani Nikula 
2570a621860aSVille Syrjälä 	intel_dp_set_link_params(intel_dp,
2571a621860aSVille Syrjälä 				 crtc_state->port_clock,
2572a621860aSVille Syrjälä 				 crtc_state->lane_count);
2573379bc100SJani Nikula 
25749f620f1dSVille Syrjälä 	/*
25759f620f1dSVille Syrjälä 	 * We only configure what the register value will be here.  Actual
25769f620f1dSVille Syrjälä 	 * enabling happens during link training farther down.
25779f620f1dSVille Syrjälä 	 */
25789f620f1dSVille Syrjälä 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
25799f620f1dSVille Syrjälä 
2580eb46f498SJani Nikula 	intel_pps_on(intel_dp);
2581379bc100SJani Nikula 
2582c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
2583379bc100SJani Nikula 
258411a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port)) {
2585a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2586a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
25873b2ed431SImre Deak 								   dig_port->ddi_io_power_domain);
2588a4550977SImre Deak 	}
2589379bc100SJani Nikula 
25903b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
2591379bc100SJani Nikula 
25925bafd85dSVille Syrjälä 	if (has_buf_trans_select(dev_priv))
2593266152aeSVille Syrjälä 		hsw_prepare_dp_ddi_buffers(encoder, crtc_state);
2594379bc100SJani Nikula 
2595e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
2596e722ab8bSVille Syrjälä 
25975cdf706fSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
2598379bc100SJani Nikula 
2599379bc100SJani Nikula 	if (!is_mst)
26000e634efdSVille Syrjälä 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2601522508b6SAnkit Nautiyal 	intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2602379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state,
2603379bc100SJani Nikula 					      true);
2604379bc100SJani Nikula 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
2605a621860aSVille Syrjälä 	intel_dp_start_link_train(intel_dp, crtc_state);
2606005e9537SMatt Roper 	if ((port != PORT_A || DISPLAY_VER(dev_priv) >= 9) &&
2607eadf6f91SManasi Navare 	    !is_trans_port_sync_mode(crtc_state))
2608a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
2609379bc100SJani Nikula 
2610379bc100SJani Nikula 	intel_ddi_enable_fec(encoder, crtc_state);
2611379bc100SJani Nikula 
2612379bc100SJani Nikula 	if (!is_mst)
261302a715c3SVille Syrjälä 		intel_ddi_enable_pipe_clock(encoder, crtc_state);
2614379bc100SJani Nikula 
26153126977dSVille Syrjälä 	intel_dsc_dp_pps_write(encoder, crtc_state);
26163126977dSVille Syrjälä 
26174e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner)
26183126977dSVille Syrjälä 		intel_dsc_enable(crtc_state);
2619379bc100SJani Nikula }
2620379bc100SJani Nikula 
2621ede9771dSVille Syrjälä static void intel_ddi_pre_enable_dp(struct intel_atomic_state *state,
2622ede9771dSVille Syrjälä 				    struct intel_encoder *encoder,
262399389390SJosé Roberto de Souza 				    const struct intel_crtc_state *crtc_state,
262499389390SJosé Roberto de Souza 				    const struct drm_connector_state *conn_state)
262599389390SJosé Roberto de Souza {
262699389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
262799389390SJosé Roberto de Souza 
2628f82f2563SMatt Roper 	if (IS_DG2(dev_priv))
2629f82f2563SMatt Roper 		dg2_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
2630f82f2563SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 12)
2631ede9771dSVille Syrjälä 		tgl_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
263299389390SJosé Roberto de Souza 	else
2633ede9771dSVille Syrjälä 		hsw_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
26340c06fa15SGwan-gyeong Mun 
2635bd8c9ccaSGwan-gyeong Mun 	/* MST will call a setting of MSA after an allocating of Virtual Channel
2636bd8c9ccaSGwan-gyeong Mun 	 * from MST encoder pre_enable callback.
2637bd8c9ccaSGwan-gyeong Mun 	 */
26381fc1e8d4SJosé Roberto de Souza 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
26390c06fa15SGwan-gyeong Mun 		intel_ddi_set_dp_msa(crtc_state, conn_state);
26401c9d2eb2SJani Nikula 
26411c9d2eb2SJani Nikula 		intel_dp_set_m_n(crtc_state, M1_N1);
264299389390SJosé Roberto de Souza 	}
26431fc1e8d4SJosé Roberto de Souza }
264499389390SJosé Roberto de Souza 
2645ede9771dSVille Syrjälä static void intel_ddi_pre_enable_hdmi(struct intel_atomic_state *state,
2646ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
2647379bc100SJani Nikula 				      const struct intel_crtc_state *crtc_state,
2648379bc100SJani Nikula 				      const struct drm_connector_state *conn_state)
2649379bc100SJani Nikula {
26500ba7ffeaSLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
26510ba7ffeaSLucas De Marchi 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2652379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2653379bc100SJani Nikula 
2654379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
2655c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
2656379bc100SJani Nikula 
2657a4550977SImre Deak 	drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2658a4550977SImre Deak 	dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2659a4550977SImre Deak 							   dig_port->ddi_io_power_domain);
2660379bc100SJani Nikula 
26613b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
2662379bc100SJani Nikula 
266302a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
2664379bc100SJani Nikula 
26650ba7ffeaSLucas De Marchi 	dig_port->set_infoframes(encoder,
2666379bc100SJani Nikula 				 crtc_state->has_infoframe,
2667379bc100SJani Nikula 				 crtc_state, conn_state);
2668379bc100SJani Nikula }
2669379bc100SJani Nikula 
2670ede9771dSVille Syrjälä static void intel_ddi_pre_enable(struct intel_atomic_state *state,
2671ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
2672379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state,
2673379bc100SJani Nikula 				 const struct drm_connector_state *conn_state)
2674379bc100SJani Nikula {
26752225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2676379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2677379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
2678379bc100SJani Nikula 
2679379bc100SJani Nikula 	/*
2680379bc100SJani Nikula 	 * When called from DP MST code:
2681379bc100SJani Nikula 	 * - conn_state will be NULL
2682379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
2683379bc100SJani Nikula 	 * - the main connector associated with this port
2684379bc100SJani Nikula 	 *   won't be active or linked to a crtc
2685379bc100SJani Nikula 	 * - crtc_state will be the state of the first stream to
2686379bc100SJani Nikula 	 *   be activated on this port, and it may not be the same
2687379bc100SJani Nikula 	 *   stream that will be deactivated last, but each stream
2688379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
2689379bc100SJani Nikula 	 *   the DP link parameteres
2690379bc100SJani Nikula 	 */
2691379bc100SJani Nikula 
26921de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->has_pch_encoder);
2693379bc100SJani Nikula 
2694379bc100SJani Nikula 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2695379bc100SJani Nikula 
2696379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
2697ede9771dSVille Syrjälä 		intel_ddi_pre_enable_hdmi(state, encoder, crtc_state,
2698ede9771dSVille Syrjälä 					  conn_state);
2699379bc100SJani Nikula 	} else {
2700f7af425dSVille Syrjälä 		struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2701379bc100SJani Nikula 
2702ede9771dSVille Syrjälä 		intel_ddi_pre_enable_dp(state, encoder, crtc_state,
2703ede9771dSVille Syrjälä 					conn_state);
2704379bc100SJani Nikula 
2705f7af425dSVille Syrjälä 		/* FIXME precompute everything properly */
27060ea02bb8SJosé Roberto de Souza 		/* FIXME how do we turn infoframes off again? */
2707f7af425dSVille Syrjälä 		if (dig_port->lspcon.active && dig_port->dp.has_hdmi_sink)
2708379bc100SJani Nikula 			dig_port->set_infoframes(encoder,
2709379bc100SJani Nikula 						 crtc_state->has_infoframe,
2710379bc100SJani Nikula 						 crtc_state, conn_state);
2711379bc100SJani Nikula 	}
2712379bc100SJani Nikula }
2713379bc100SJani Nikula 
2714379bc100SJani Nikula static void intel_disable_ddi_buf(struct intel_encoder *encoder,
2715379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
2716379bc100SJani Nikula {
2717379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2718379bc100SJani Nikula 	enum port port = encoder->port;
2719379bc100SJani Nikula 	bool wait = false;
2720379bc100SJani Nikula 	u32 val;
2721379bc100SJani Nikula 
2722f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, DDI_BUF_CTL(port));
2723379bc100SJani Nikula 	if (val & DDI_BUF_CTL_ENABLE) {
2724379bc100SJani Nikula 		val &= ~DDI_BUF_CTL_ENABLE;
2725f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(port), val);
2726379bc100SJani Nikula 		wait = true;
2727379bc100SJani Nikula 	}
2728379bc100SJani Nikula 
2729e468ff06SLucas De Marchi 	if (intel_crtc_has_dp_encoder(crtc_state)) {
2730ef79fafeSVille Syrjälä 		val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2731379bc100SJani Nikula 		val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
2732379bc100SJani Nikula 		val |= DP_TP_CTL_LINK_TRAIN_PAT1;
2733ef79fafeSVille Syrjälä 		intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2734e468ff06SLucas De Marchi 	}
2735379bc100SJani Nikula 
2736379bc100SJani Nikula 	/* Disable FEC in DP Sink */
2737379bc100SJani Nikula 	intel_ddi_disable_fec_state(encoder, crtc_state);
2738379bc100SJani Nikula 
2739379bc100SJani Nikula 	if (wait)
2740379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, port);
2741379bc100SJani Nikula }
2742379bc100SJani Nikula 
2743ede9771dSVille Syrjälä static void intel_ddi_post_disable_dp(struct intel_atomic_state *state,
2744ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
2745379bc100SJani Nikula 				      const struct intel_crtc_state *old_crtc_state,
2746379bc100SJani Nikula 				      const struct drm_connector_state *old_conn_state)
2747379bc100SJani Nikula {
2748379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2749b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2750379bc100SJani Nikula 	struct intel_dp *intel_dp = &dig_port->dp;
2751379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(old_crtc_state,
2752379bc100SJani Nikula 					  INTEL_OUTPUT_DP_MST);
2753379bc100SJani Nikula 
2754c980216dSImre Deak 	if (!is_mst)
2755c980216dSImre Deak 		intel_dp_set_infoframes(encoder, false,
2756c980216dSImre Deak 					old_crtc_state, old_conn_state);
2757fa37a213SGwan-gyeong Mun 
2758379bc100SJani Nikula 	/*
2759379bc100SJani Nikula 	 * Power down sink before disabling the port, otherwise we end
2760379bc100SJani Nikula 	 * up getting interrupts from the sink on detecting link loss.
2761379bc100SJani Nikula 	 */
27620e634efdSVille Syrjälä 	intel_dp_set_power(intel_dp, DP_SET_POWER_D3);
276378eaaba3SJosé Roberto de Souza 
2764005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2765c59053dcSJosé Roberto de Souza 		if (is_mst) {
2766c59053dcSJosé Roberto de Souza 			enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
2767c59053dcSJosé Roberto de Souza 			u32 val;
2768c59053dcSJosé Roberto de Souza 
2769f7960e7fSJani Nikula 			val = intel_de_read(dev_priv,
2770f7960e7fSJani Nikula 					    TRANS_DDI_FUNC_CTL(cpu_transcoder));
2771919e4f07SJosé Roberto de Souza 			val &= ~(TGL_TRANS_DDI_PORT_MASK |
2772919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
2773f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2774f7960e7fSJani Nikula 				       TRANS_DDI_FUNC_CTL(cpu_transcoder),
2775f7960e7fSJani Nikula 				       val);
2776c59053dcSJosé Roberto de Souza 		}
2777c59053dcSJosé Roberto de Souza 	} else {
2778c59053dcSJosé Roberto de Souza 		if (!is_mst)
277950a7efb2SJosé Roberto de Souza 			intel_ddi_disable_pipe_clock(old_crtc_state);
2780c59053dcSJosé Roberto de Souza 	}
2781379bc100SJani Nikula 
2782379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
2783379bc100SJani Nikula 
27843ca8f191SJosé Roberto de Souza 	/*
27853ca8f191SJosé Roberto de Souza 	 * From TGL spec: "If single stream or multi-stream master transcoder:
27863ca8f191SJosé Roberto de Souza 	 * Configure Transcoder Clock select to direct no clock to the
27873ca8f191SJosé Roberto de Souza 	 * transcoder"
27883ca8f191SJosé Roberto de Souza 	 */
2789005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
27903ca8f191SJosé Roberto de Souza 		intel_ddi_disable_pipe_clock(old_crtc_state);
27913ca8f191SJosé Roberto de Souza 
2792eb46f498SJani Nikula 	intel_pps_vdd_on(intel_dp);
2793eb46f498SJani Nikula 	intel_pps_off(intel_dp);
2794379bc100SJani Nikula 
279511a89708SImre Deak 	if (!intel_tc_port_in_tbt_alt_mode(dig_port))
2796a4550977SImre Deak 		intel_display_power_put(dev_priv,
2797a4550977SImre Deak 					dig_port->ddi_io_power_domain,
2798a4550977SImre Deak 					fetch_and_zero(&dig_port->ddi_io_wakeref));
2799379bc100SJani Nikula 
2800c133df69SVille Syrjälä 	intel_ddi_disable_clock(encoder);
2801379bc100SJani Nikula }
2802379bc100SJani Nikula 
2803ede9771dSVille Syrjälä static void intel_ddi_post_disable_hdmi(struct intel_atomic_state *state,
2804ede9771dSVille Syrjälä 					struct intel_encoder *encoder,
2805379bc100SJani Nikula 					const struct intel_crtc_state *old_crtc_state,
2806379bc100SJani Nikula 					const struct drm_connector_state *old_conn_state)
2807379bc100SJani Nikula {
2808379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2809b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2810379bc100SJani Nikula 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2811379bc100SJani Nikula 
2812379bc100SJani Nikula 	dig_port->set_infoframes(encoder, false,
2813379bc100SJani Nikula 				 old_crtc_state, old_conn_state);
2814379bc100SJani Nikula 
2815379bc100SJani Nikula 	intel_ddi_disable_pipe_clock(old_crtc_state);
2816379bc100SJani Nikula 
2817379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
2818379bc100SJani Nikula 
2819a4550977SImre Deak 	intel_display_power_put(dev_priv,
2820a4550977SImre Deak 				dig_port->ddi_io_power_domain,
2821a4550977SImre Deak 				fetch_and_zero(&dig_port->ddi_io_wakeref));
2822379bc100SJani Nikula 
2823c133df69SVille Syrjälä 	intel_ddi_disable_clock(encoder);
2824379bc100SJani Nikula 
2825379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
2826379bc100SJani Nikula }
2827379bc100SJani Nikula 
2828ede9771dSVille Syrjälä static void intel_ddi_post_disable(struct intel_atomic_state *state,
2829ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
2830379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
2831379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
2832379bc100SJani Nikula {
2833379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2834b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
283517bef9baSVille Syrjälä 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
283617bef9baSVille Syrjälä 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
2837379bc100SJani Nikula 
28387829c92bSVille Syrjälä 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST)) {
2839773b4b54SVille Syrjälä 		intel_crtc_vblank_off(old_crtc_state);
2840773b4b54SVille Syrjälä 
28418c66081bSVille Syrjälä 		intel_disable_transcoder(old_crtc_state);
2842773b4b54SVille Syrjälä 
2843f0651232SManasi Navare 		intel_vrr_disable(old_crtc_state);
2844f0651232SManasi Navare 
2845773b4b54SVille Syrjälä 		intel_ddi_disable_transcoder_func(old_crtc_state);
2846773b4b54SVille Syrjälä 
2847773b4b54SVille Syrjälä 		intel_dsc_disable(old_crtc_state);
2848773b4b54SVille Syrjälä 
2849005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 9)
2850f6df4d46SLucas De Marchi 			skl_scaler_disable(old_crtc_state);
2851773b4b54SVille Syrjälä 		else
28529eae5e27SLucas De Marchi 			ilk_pfit_disable(old_crtc_state);
28537829c92bSVille Syrjälä 	}
2854773b4b54SVille Syrjälä 
28554e3cdb45SManasi Navare 	if (old_crtc_state->bigjoiner_linked_crtc) {
2856f2e19b58SVille Syrjälä 		struct intel_crtc *slave_crtc =
28574e3cdb45SManasi Navare 			old_crtc_state->bigjoiner_linked_crtc;
28584e3cdb45SManasi Navare 		const struct intel_crtc_state *old_slave_crtc_state =
2859f2e19b58SVille Syrjälä 			intel_atomic_get_old_crtc_state(state, slave_crtc);
28604e3cdb45SManasi Navare 
28614e3cdb45SManasi Navare 		intel_crtc_vblank_off(old_slave_crtc_state);
28624e3cdb45SManasi Navare 
28634e3cdb45SManasi Navare 		intel_dsc_disable(old_slave_crtc_state);
28644e3cdb45SManasi Navare 		skl_scaler_disable(old_slave_crtc_state);
28654e3cdb45SManasi Navare 	}
28664e3cdb45SManasi Navare 
2867379bc100SJani Nikula 	/*
2868379bc100SJani Nikula 	 * When called from DP MST code:
2869379bc100SJani Nikula 	 * - old_conn_state will be NULL
2870379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
2871379bc100SJani Nikula 	 * - the main connector associated with this port
2872379bc100SJani Nikula 	 *   won't be active or linked to a crtc
2873379bc100SJani Nikula 	 * - old_crtc_state will be the state of the last stream to
2874379bc100SJani Nikula 	 *   be deactivated on this port, and it may not be the same
2875379bc100SJani Nikula 	 *   stream that was activated last, but each stream
2876379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
2877379bc100SJani Nikula 	 *   the DP link parameteres
2878379bc100SJani Nikula 	 */
2879379bc100SJani Nikula 
2880379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
2881ede9771dSVille Syrjälä 		intel_ddi_post_disable_hdmi(state, encoder, old_crtc_state,
2882ede9771dSVille Syrjälä 					    old_conn_state);
2883379bc100SJani Nikula 	else
2884ede9771dSVille Syrjälä 		intel_ddi_post_disable_dp(state, encoder, old_crtc_state,
2885ede9771dSVille Syrjälä 					  old_conn_state);
2886379bc100SJani Nikula 
288717bef9baSVille Syrjälä 	if (intel_crtc_has_dp_encoder(old_crtc_state) || is_tc_port)
2888162e68e1SImre Deak 		intel_display_power_put(dev_priv,
2889162e68e1SImre Deak 					intel_ddi_main_link_aux_domain(dig_port),
2890162e68e1SImre Deak 					fetch_and_zero(&dig_port->aux_wakeref));
289117bef9baSVille Syrjälä 
289217bef9baSVille Syrjälä 	if (is_tc_port)
289317bef9baSVille Syrjälä 		intel_tc_port_put_link(dig_port);
2894379bc100SJani Nikula }
2895379bc100SJani Nikula 
2896d82a855aSVille Syrjälä static void trans_port_sync_stop_link_train(struct intel_atomic_state *state,
2897d82a855aSVille Syrjälä 					    struct intel_encoder *encoder,
2898d82a855aSVille Syrjälä 					    const struct intel_crtc_state *crtc_state)
2899d82a855aSVille Syrjälä {
2900d82a855aSVille Syrjälä 	const struct drm_connector_state *conn_state;
2901d82a855aSVille Syrjälä 	struct drm_connector *conn;
2902d82a855aSVille Syrjälä 	int i;
2903d82a855aSVille Syrjälä 
2904d82a855aSVille Syrjälä 	if (!crtc_state->sync_mode_slaves_mask)
2905d82a855aSVille Syrjälä 		return;
2906d82a855aSVille Syrjälä 
2907d82a855aSVille Syrjälä 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
2908d82a855aSVille Syrjälä 		struct intel_encoder *slave_encoder =
2909d82a855aSVille Syrjälä 			to_intel_encoder(conn_state->best_encoder);
2910d82a855aSVille Syrjälä 		struct intel_crtc *slave_crtc = to_intel_crtc(conn_state->crtc);
2911d82a855aSVille Syrjälä 		const struct intel_crtc_state *slave_crtc_state;
2912d82a855aSVille Syrjälä 
2913d82a855aSVille Syrjälä 		if (!slave_crtc)
2914d82a855aSVille Syrjälä 			continue;
2915d82a855aSVille Syrjälä 
2916d82a855aSVille Syrjälä 		slave_crtc_state =
2917d82a855aSVille Syrjälä 			intel_atomic_get_new_crtc_state(state, slave_crtc);
2918d82a855aSVille Syrjälä 
2919d82a855aSVille Syrjälä 		if (slave_crtc_state->master_transcoder !=
2920d82a855aSVille Syrjälä 		    crtc_state->cpu_transcoder)
2921d82a855aSVille Syrjälä 			continue;
2922d82a855aSVille Syrjälä 
2923a621860aSVille Syrjälä 		intel_dp_stop_link_train(enc_to_intel_dp(slave_encoder),
2924a621860aSVille Syrjälä 					 slave_crtc_state);
2925d82a855aSVille Syrjälä 	}
2926d82a855aSVille Syrjälä 
2927d82a855aSVille Syrjälä 	usleep_range(200, 400);
2928d82a855aSVille Syrjälä 
2929a621860aSVille Syrjälä 	intel_dp_stop_link_train(enc_to_intel_dp(encoder),
2930a621860aSVille Syrjälä 				 crtc_state);
2931d82a855aSVille Syrjälä }
2932d82a855aSVille Syrjälä 
2933ede9771dSVille Syrjälä static void intel_enable_ddi_dp(struct intel_atomic_state *state,
2934ede9771dSVille Syrjälä 				struct intel_encoder *encoder,
2935379bc100SJani Nikula 				const struct intel_crtc_state *crtc_state,
2936379bc100SJani Nikula 				const struct drm_connector_state *conn_state)
2937379bc100SJani Nikula {
2938379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2939b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2940998cc864SUma Shankar 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2941379bc100SJani Nikula 	enum port port = encoder->port;
2942379bc100SJani Nikula 
2943005e9537SMatt Roper 	if (port == PORT_A && DISPLAY_VER(dev_priv) < 9)
2944a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
2945379bc100SJani Nikula 
2946379bc100SJani Nikula 	intel_edp_backlight_on(crtc_state, conn_state);
2947998cc864SUma Shankar 
2948998cc864SUma Shankar 	if (!dig_port->lspcon.active || dig_port->dp.has_hdmi_sink)
29491bf3657cSGwan-gyeong Mun 		intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
2950998cc864SUma Shankar 
29513a3dd534SJosé Roberto de Souza 	intel_drrs_enable(intel_dp, crtc_state);
2952379bc100SJani Nikula 
2953379bc100SJani Nikula 	if (crtc_state->has_audio)
2954379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
2955d82a855aSVille Syrjälä 
2956d82a855aSVille Syrjälä 	trans_port_sync_stop_link_train(state, encoder, crtc_state);
2957379bc100SJani Nikula }
2958379bc100SJani Nikula 
2959379bc100SJani Nikula static i915_reg_t
2960379bc100SJani Nikula gen9_chicken_trans_reg_by_port(struct drm_i915_private *dev_priv,
2961379bc100SJani Nikula 			       enum port port)
2962379bc100SJani Nikula {
296312c4d4c1SVille Syrjälä 	static const enum transcoder trans[] = {
296412c4d4c1SVille Syrjälä 		[PORT_A] = TRANSCODER_EDP,
296512c4d4c1SVille Syrjälä 		[PORT_B] = TRANSCODER_A,
296612c4d4c1SVille Syrjälä 		[PORT_C] = TRANSCODER_B,
296712c4d4c1SVille Syrjälä 		[PORT_D] = TRANSCODER_C,
296812c4d4c1SVille Syrjälä 		[PORT_E] = TRANSCODER_A,
2969379bc100SJani Nikula 	};
2970379bc100SJani Nikula 
2971005e9537SMatt Roper 	drm_WARN_ON(&dev_priv->drm, DISPLAY_VER(dev_priv) < 9);
2972379bc100SJani Nikula 
29731de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port < PORT_A || port > PORT_E))
2974379bc100SJani Nikula 		port = PORT_A;
2975379bc100SJani Nikula 
297612c4d4c1SVille Syrjälä 	return CHICKEN_TRANS(trans[port]);
2977379bc100SJani Nikula }
2978379bc100SJani Nikula 
2979ede9771dSVille Syrjälä static void intel_enable_ddi_hdmi(struct intel_atomic_state *state,
2980ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
2981379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
2982379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
2983379bc100SJani Nikula {
2984379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2985b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2986379bc100SJani Nikula 	struct drm_connector *connector = conn_state->connector;
2987379bc100SJani Nikula 	enum port port = encoder->port;
2988379bc100SJani Nikula 
2989379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
2990379bc100SJani Nikula 					       crtc_state->hdmi_high_tmds_clock_ratio,
2991379bc100SJani Nikula 					       crtc_state->hdmi_scrambling))
299247bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
299347bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to configure sink scrambling/TMDS bit clock ratio\n",
2994379bc100SJani Nikula 			    connector->base.id, connector->name);
2995379bc100SJani Nikula 
29965bafd85dSVille Syrjälä 	if (has_buf_trans_select(dev_priv))
2997e722ab8bSVille Syrjälä 		hsw_prepare_hdmi_ddi_buffers(encoder, crtc_state);
2998c9b69041SVille Syrjälä 
2999e722ab8bSVille Syrjälä 	encoder->set_signal_levels(encoder, crtc_state);
3000c9b69041SVille Syrjälä 
3001379bc100SJani Nikula 	/* Display WA #1143: skl,kbl,cfl */
300293e7e61eSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv)) {
3003379bc100SJani Nikula 		/*
3004379bc100SJani Nikula 		 * For some reason these chicken bits have been
3005379bc100SJani Nikula 		 * stuffed into a transcoder register, event though
3006379bc100SJani Nikula 		 * the bits affect a specific DDI port rather than
3007379bc100SJani Nikula 		 * a specific transcoder.
3008379bc100SJani Nikula 		 */
3009379bc100SJani Nikula 		i915_reg_t reg = gen9_chicken_trans_reg_by_port(dev_priv, port);
3010379bc100SJani Nikula 		u32 val;
3011379bc100SJani Nikula 
3012f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, reg);
3013379bc100SJani Nikula 
3014379bc100SJani Nikula 		if (port == PORT_E)
3015379bc100SJani Nikula 			val |= DDIE_TRAINING_OVERRIDE_ENABLE |
3016379bc100SJani Nikula 				DDIE_TRAINING_OVERRIDE_VALUE;
3017379bc100SJani Nikula 		else
3018379bc100SJani Nikula 			val |= DDI_TRAINING_OVERRIDE_ENABLE |
3019379bc100SJani Nikula 				DDI_TRAINING_OVERRIDE_VALUE;
3020379bc100SJani Nikula 
3021f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3022f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, reg);
3023379bc100SJani Nikula 
3024379bc100SJani Nikula 		udelay(1);
3025379bc100SJani Nikula 
3026379bc100SJani Nikula 		if (port == PORT_E)
3027379bc100SJani Nikula 			val &= ~(DDIE_TRAINING_OVERRIDE_ENABLE |
3028379bc100SJani Nikula 				 DDIE_TRAINING_OVERRIDE_VALUE);
3029379bc100SJani Nikula 		else
3030379bc100SJani Nikula 			val &= ~(DDI_TRAINING_OVERRIDE_ENABLE |
3031379bc100SJani Nikula 				 DDI_TRAINING_OVERRIDE_VALUE);
3032379bc100SJani Nikula 
3033f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3034379bc100SJani Nikula 	}
3035379bc100SJani Nikula 
30361e0cb7beSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
30371e0cb7beSVille Syrjälä 
3038379bc100SJani Nikula 	/* In HDMI/DVI mode, the port width, and swing/emphasis values
3039379bc100SJani Nikula 	 * are ignored so nothing special needs to be done besides
3040379bc100SJani Nikula 	 * enabling the port.
3041414002f1SImre Deak 	 *
3042414002f1SImre Deak 	 * On ADL_P the PHY link rate and lane count must be programmed but
3043414002f1SImre Deak 	 * these are both 0 for HDMI.
3044379bc100SJani Nikula 	 */
3045f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port),
3046379bc100SJani Nikula 		       dig_port->saved_port_bits | DDI_BUF_CTL_ENABLE);
3047379bc100SJani Nikula 
3048379bc100SJani Nikula 	if (crtc_state->has_audio)
3049379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3050379bc100SJani Nikula }
3051379bc100SJani Nikula 
3052ede9771dSVille Syrjälä static void intel_enable_ddi(struct intel_atomic_state *state,
3053ede9771dSVille Syrjälä 			     struct intel_encoder *encoder,
3054379bc100SJani Nikula 			     const struct intel_crtc_state *crtc_state,
3055379bc100SJani Nikula 			     const struct drm_connector_state *conn_state)
3056379bc100SJani Nikula {
30578b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc_state->has_pch_encoder);
305821fd23acSJani Nikula 
30594e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner_slave)
3060eed22a46SVille Syrjälä 		intel_ddi_enable_transcoder_func(encoder, crtc_state);
30617c2fedd7SVille Syrjälä 
3062aa52b39dSManasi Navare 	intel_vrr_enable(encoder, crtc_state);
3063aa52b39dSManasi Navare 
30648c66081bSVille Syrjälä 	intel_enable_transcoder(crtc_state);
306521fd23acSJani Nikula 
306621fd23acSJani Nikula 	intel_crtc_vblank_on(crtc_state);
306721fd23acSJani Nikula 
3068379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3069ede9771dSVille Syrjälä 		intel_enable_ddi_hdmi(state, encoder, crtc_state, conn_state);
3070379bc100SJani Nikula 	else
3071ede9771dSVille Syrjälä 		intel_enable_ddi_dp(state, encoder, crtc_state, conn_state);
3072379bc100SJani Nikula 
3073379bc100SJani Nikula 	/* Enable hdcp if it's desired */
3074379bc100SJani Nikula 	if (conn_state->content_protection ==
3075379bc100SJani Nikula 	    DRM_MODE_CONTENT_PROTECTION_DESIRED)
3076d456512cSRamalingam C 		intel_hdcp_enable(to_intel_connector(conn_state->connector),
3077fc6097d4SAnshuman Gupta 				  crtc_state,
3078d456512cSRamalingam C 				  (u8)conn_state->hdcp_content_type);
3079379bc100SJani Nikula }
3080379bc100SJani Nikula 
3081ede9771dSVille Syrjälä static void intel_disable_ddi_dp(struct intel_atomic_state *state,
3082ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
3083379bc100SJani Nikula 				 const struct intel_crtc_state *old_crtc_state,
3084379bc100SJani Nikula 				 const struct drm_connector_state *old_conn_state)
3085379bc100SJani Nikula {
3086b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3087379bc100SJani Nikula 
3088379bc100SJani Nikula 	intel_dp->link_trained = false;
3089379bc100SJani Nikula 
3090f28c5950SVille Syrjälä 	if (old_crtc_state->has_audio)
3091f28c5950SVille Syrjälä 		intel_audio_codec_disable(encoder,
3092f28c5950SVille Syrjälä 					  old_crtc_state, old_conn_state);
3093f28c5950SVille Syrjälä 
3094f28c5950SVille Syrjälä 	intel_drrs_disable(intel_dp, old_crtc_state);
3095f28c5950SVille Syrjälä 	intel_psr_disable(intel_dp, old_crtc_state);
3096379bc100SJani Nikula 	intel_edp_backlight_off(old_conn_state);
3097379bc100SJani Nikula 	/* Disable the decompression in DP Sink */
3098379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, old_crtc_state,
3099379bc100SJani Nikula 					      false);
31001639406aSManasi Navare 	/* Disable Ignore_MSA bit in DP Sink */
31011639406aSManasi Navare 	intel_dp_sink_set_msa_timing_par_ignore_state(intel_dp, old_crtc_state,
31021639406aSManasi Navare 						      false);
3103379bc100SJani Nikula }
3104379bc100SJani Nikula 
3105ede9771dSVille Syrjälä static void intel_disable_ddi_hdmi(struct intel_atomic_state *state,
3106ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
3107379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3108379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3109379bc100SJani Nikula {
311047bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3111379bc100SJani Nikula 	struct drm_connector *connector = old_conn_state->connector;
3112379bc100SJani Nikula 
3113f28c5950SVille Syrjälä 	if (old_crtc_state->has_audio)
3114f28c5950SVille Syrjälä 		intel_audio_codec_disable(encoder,
3115f28c5950SVille Syrjälä 					  old_crtc_state, old_conn_state);
3116f28c5950SVille Syrjälä 
3117379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3118379bc100SJani Nikula 					       false, false))
311947bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
312047bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to reset sink scrambling/TMDS bit clock ratio\n",
3121379bc100SJani Nikula 			    connector->base.id, connector->name);
3122379bc100SJani Nikula }
3123379bc100SJani Nikula 
3124ede9771dSVille Syrjälä static void intel_disable_ddi(struct intel_atomic_state *state,
3125ede9771dSVille Syrjälä 			      struct intel_encoder *encoder,
3126379bc100SJani Nikula 			      const struct intel_crtc_state *old_crtc_state,
3127379bc100SJani Nikula 			      const struct drm_connector_state *old_conn_state)
3128379bc100SJani Nikula {
3129379bc100SJani Nikula 	intel_hdcp_disable(to_intel_connector(old_conn_state->connector));
3130379bc100SJani Nikula 
3131379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3132ede9771dSVille Syrjälä 		intel_disable_ddi_hdmi(state, encoder, old_crtc_state,
3133ede9771dSVille Syrjälä 				       old_conn_state);
3134379bc100SJani Nikula 	else
3135ede9771dSVille Syrjälä 		intel_disable_ddi_dp(state, encoder, old_crtc_state,
3136ede9771dSVille Syrjälä 				     old_conn_state);
3137379bc100SJani Nikula }
3138379bc100SJani Nikula 
3139ede9771dSVille Syrjälä static void intel_ddi_update_pipe_dp(struct intel_atomic_state *state,
3140ede9771dSVille Syrjälä 				     struct intel_encoder *encoder,
3141379bc100SJani Nikula 				     const struct intel_crtc_state *crtc_state,
3142379bc100SJani Nikula 				     const struct drm_connector_state *conn_state)
3143379bc100SJani Nikula {
3144b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3145379bc100SJani Nikula 
31460c06fa15SGwan-gyeong Mun 	intel_ddi_set_dp_msa(crtc_state, conn_state);
3147379bc100SJani Nikula 
314876d45d06SGwan-gyeong Mun 	intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
31493a3dd534SJosé Roberto de Souza 	intel_drrs_update(intel_dp, crtc_state);
3150379bc100SJani Nikula 
3151c0a52f8bSJani Nikula 	intel_backlight_update(state, encoder, crtc_state, conn_state);
3152379bc100SJani Nikula }
3153379bc100SJani Nikula 
3154f1c7a36bSSean Paul void intel_ddi_update_pipe(struct intel_atomic_state *state,
3155ede9771dSVille Syrjälä 			   struct intel_encoder *encoder,
3156379bc100SJani Nikula 			   const struct intel_crtc_state *crtc_state,
3157379bc100SJani Nikula 			   const struct drm_connector_state *conn_state)
3158379bc100SJani Nikula {
3159d456512cSRamalingam C 
3160f1c7a36bSSean Paul 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) &&
3161f1c7a36bSSean Paul 	    !intel_encoder_is_mst(encoder))
3162ede9771dSVille Syrjälä 		intel_ddi_update_pipe_dp(state, encoder, crtc_state,
3163ede9771dSVille Syrjälä 					 conn_state);
3164379bc100SJani Nikula 
3165ede9771dSVille Syrjälä 	intel_hdcp_update_pipe(state, encoder, crtc_state, conn_state);
3166379bc100SJani Nikula }
3167379bc100SJani Nikula 
3168379bc100SJani Nikula static void
316924a7bfe0SImre Deak intel_ddi_update_prepare(struct intel_atomic_state *state,
317024a7bfe0SImre Deak 			 struct intel_encoder *encoder,
317124a7bfe0SImre Deak 			 struct intel_crtc *crtc)
317224a7bfe0SImre Deak {
317324a7bfe0SImre Deak 	struct intel_crtc_state *crtc_state =
317424a7bfe0SImre Deak 		crtc ? intel_atomic_get_new_crtc_state(state, crtc) : NULL;
317524a7bfe0SImre Deak 	int required_lanes = crtc_state ? crtc_state->lane_count : 1;
317624a7bfe0SImre Deak 
31778b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc && crtc->active);
317824a7bfe0SImre Deak 
3179b7d02c3aSVille Syrjälä 	intel_tc_port_get_link(enc_to_dig_port(encoder),
3180b7d02c3aSVille Syrjälä 		               required_lanes);
31811326a92cSMaarten Lankhorst 	if (crtc_state && crtc_state->hw.active)
318224a7bfe0SImre Deak 		intel_update_active_dpll(state, crtc, encoder);
318324a7bfe0SImre Deak }
318424a7bfe0SImre Deak 
318524a7bfe0SImre Deak static void
318624a7bfe0SImre Deak intel_ddi_update_complete(struct intel_atomic_state *state,
318724a7bfe0SImre Deak 			  struct intel_encoder *encoder,
318824a7bfe0SImre Deak 			  struct intel_crtc *crtc)
318924a7bfe0SImre Deak {
3190b7d02c3aSVille Syrjälä 	intel_tc_port_put_link(enc_to_dig_port(encoder));
319124a7bfe0SImre Deak }
319224a7bfe0SImre Deak 
319324a7bfe0SImre Deak static void
3194ede9771dSVille Syrjälä intel_ddi_pre_pll_enable(struct intel_atomic_state *state,
3195ede9771dSVille Syrjälä 			 struct intel_encoder *encoder,
3196379bc100SJani Nikula 			 const struct intel_crtc_state *crtc_state,
3197379bc100SJani Nikula 			 const struct drm_connector_state *conn_state)
3198379bc100SJani Nikula {
3199379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3200b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3201d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3202d8fe2ab6SMatt Roper 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
3203379bc100SJani Nikula 
320424a7bfe0SImre Deak 	if (is_tc_port)
320524a7bfe0SImre Deak 		intel_tc_port_get_link(dig_port, crtc_state->lane_count);
320624a7bfe0SImre Deak 
3207162e68e1SImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state) || is_tc_port) {
3208162e68e1SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->aux_wakeref);
3209162e68e1SImre Deak 		dig_port->aux_wakeref =
3210379bc100SJani Nikula 			intel_display_power_get(dev_priv,
3211379bc100SJani Nikula 						intel_ddi_main_link_aux_domain(dig_port));
3212162e68e1SImre Deak 	}
3213379bc100SJani Nikula 
321411a89708SImre Deak 	if (is_tc_port && !intel_tc_port_in_tbt_alt_mode(dig_port))
32159d44dcb9SLucas De Marchi 		/*
32169d44dcb9SLucas De Marchi 		 * Program the lane count for static/dynamic connections on
32179d44dcb9SLucas De Marchi 		 * Type-C ports.  Skip this step for TBT.
32189d44dcb9SLucas De Marchi 		 */
32199d44dcb9SLucas De Marchi 		intel_tc_port_set_fia_lane_count(dig_port, crtc_state->lane_count);
32202446e1d6SMatt Roper 	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3221379bc100SJani Nikula 		bxt_ddi_phy_set_lane_optim_mask(encoder,
3222379bc100SJani Nikula 						crtc_state->lane_lat_optim_mask);
3223379bc100SJani Nikula }
3224379bc100SJani Nikula 
3225a621860aSVille Syrjälä static void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp,
3226a621860aSVille Syrjälä 					   const struct intel_crtc_state *crtc_state)
3227379bc100SJani Nikula {
3228ef79fafeSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3229ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3230ef79fafeSVille Syrjälä 	enum port port = encoder->port;
323135ac28a8SLucas De Marchi 	u32 dp_tp_ctl, ddi_buf_ctl;
3232379bc100SJani Nikula 	bool wait = false;
3233379bc100SJani Nikula 
3234ef79fafeSVille Syrjälä 	dp_tp_ctl = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
323535ac28a8SLucas De Marchi 
323635ac28a8SLucas De Marchi 	if (dp_tp_ctl & DP_TP_CTL_ENABLE) {
3237f7960e7fSJani Nikula 		ddi_buf_ctl = intel_de_read(dev_priv, DDI_BUF_CTL(port));
323835ac28a8SLucas De Marchi 		if (ddi_buf_ctl & DDI_BUF_CTL_ENABLE) {
3239f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_BUF_CTL(port),
324035ac28a8SLucas De Marchi 				       ddi_buf_ctl & ~DDI_BUF_CTL_ENABLE);
3241379bc100SJani Nikula 			wait = true;
3242379bc100SJani Nikula 		}
3243379bc100SJani Nikula 
324435ac28a8SLucas De Marchi 		dp_tp_ctl &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
324535ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_LINK_TRAIN_PAT1;
3246ef79fafeSVille Syrjälä 		intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3247ef79fafeSVille Syrjälä 		intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3248379bc100SJani Nikula 
3249379bc100SJani Nikula 		if (wait)
3250379bc100SJani Nikula 			intel_wait_ddi_buf_idle(dev_priv, port);
3251379bc100SJani Nikula 	}
3252379bc100SJani Nikula 
3253963501bdSImre Deak 	dp_tp_ctl = DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_PAT1;
3254a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
325535ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_MST;
3256a621860aSVille Syrjälä 	} else {
325735ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_SST;
3258379bc100SJani Nikula 		if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
325935ac28a8SLucas De Marchi 			dp_tp_ctl |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
3260379bc100SJani Nikula 	}
3261ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3262ef79fafeSVille Syrjälä 	intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3263379bc100SJani Nikula 
3264379bc100SJani Nikula 	intel_dp->DP |= DDI_BUF_CTL_ENABLE;
3265f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
3266f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
3267379bc100SJani Nikula 
3268e828da30SManasi Navare 	intel_wait_ddi_buf_active(dev_priv, port);
3269379bc100SJani Nikula }
3270379bc100SJani Nikula 
3271eee3f911SVille Syrjälä static void intel_ddi_set_link_train(struct intel_dp *intel_dp,
3272a621860aSVille Syrjälä 				     const struct intel_crtc_state *crtc_state,
3273eee3f911SVille Syrjälä 				     u8 dp_train_pat)
3274eee3f911SVille Syrjälä {
3275ef79fafeSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3276ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3277eee3f911SVille Syrjälä 	u32 temp;
3278eee3f911SVille Syrjälä 
3279ef79fafeSVille Syrjälä 	temp = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3280eee3f911SVille Syrjälä 
3281eee3f911SVille Syrjälä 	temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
32826777a855SImre Deak 	switch (intel_dp_training_pattern_symbol(dp_train_pat)) {
3283eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_DISABLE:
3284eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
3285eee3f911SVille Syrjälä 		break;
3286eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_1:
3287eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
3288eee3f911SVille Syrjälä 		break;
3289eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_2:
3290eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
3291eee3f911SVille Syrjälä 		break;
3292eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_3:
3293eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
3294eee3f911SVille Syrjälä 		break;
3295eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_4:
3296eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT4;
3297eee3f911SVille Syrjälä 		break;
3298eee3f911SVille Syrjälä 	}
3299eee3f911SVille Syrjälä 
3300ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), temp);
3301eee3f911SVille Syrjälä }
3302eee3f911SVille Syrjälä 
3303a621860aSVille Syrjälä static void intel_ddi_set_idle_link_train(struct intel_dp *intel_dp,
3304a621860aSVille Syrjälä 					  const struct intel_crtc_state *crtc_state)
33058fdda385SVille Syrjälä {
33068fdda385SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
33078fdda385SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
33088fdda385SVille Syrjälä 	enum port port = encoder->port;
33098fdda385SVille Syrjälä 	u32 val;
33108fdda385SVille Syrjälä 
3311ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
33128fdda385SVille Syrjälä 	val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
33138fdda385SVille Syrjälä 	val |= DP_TP_CTL_LINK_TRAIN_IDLE;
3314ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
33158fdda385SVille Syrjälä 
33168fdda385SVille Syrjälä 	/*
33178fdda385SVille Syrjälä 	 * Until TGL on PORT_A we can have only eDP in SST mode. There the only
33188fdda385SVille Syrjälä 	 * reason we need to set idle transmission mode is to work around a HW
33198fdda385SVille Syrjälä 	 * issue where we enable the pipe while not in idle link-training mode.
33208fdda385SVille Syrjälä 	 * In this case there is requirement to wait for a minimum number of
33218fdda385SVille Syrjälä 	 * idle patterns to be sent.
33228fdda385SVille Syrjälä 	 */
3323005e9537SMatt Roper 	if (port == PORT_A && DISPLAY_VER(dev_priv) < 12)
33248fdda385SVille Syrjälä 		return;
33258fdda385SVille Syrjälä 
3326ef79fafeSVille Syrjälä 	if (intel_de_wait_for_set(dev_priv,
3327ef79fafeSVille Syrjälä 				  dp_tp_status_reg(encoder, crtc_state),
33288fdda385SVille Syrjälä 				  DP_TP_STATUS_IDLE_DONE, 1))
33298fdda385SVille Syrjälä 		drm_err(&dev_priv->drm,
33308fdda385SVille Syrjälä 			"Timed out waiting for DP idle patterns\n");
33318fdda385SVille Syrjälä }
33328fdda385SVille Syrjälä 
3333379bc100SJani Nikula static bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
3334379bc100SJani Nikula 				       enum transcoder cpu_transcoder)
3335379bc100SJani Nikula {
3336379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP)
3337379bc100SJani Nikula 		return false;
3338379bc100SJani Nikula 
3339615a7724SAnshuman Gupta 	if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO_MMIO))
3340379bc100SJani Nikula 		return false;
3341379bc100SJani Nikula 
3342f7960e7fSJani Nikula 	return intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD) &
3343379bc100SJani Nikula 		AUDIO_OUTPUT_ENABLE(cpu_transcoder);
3344379bc100SJani Nikula }
3345379bc100SJani Nikula 
3346379bc100SJani Nikula void intel_ddi_compute_min_voltage_level(struct drm_i915_private *dev_priv,
3347379bc100SJani Nikula 					 struct intel_crtc_state *crtc_state)
3348379bc100SJani Nikula {
3349005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12 && crtc_state->port_clock > 594000)
33500fde0b1dSMatt Roper 		crtc_state->min_voltage_level = 2;
335124ea098bSTejas Upadhyay 	else if (IS_JSL_EHL(dev_priv) && crtc_state->port_clock > 594000)
33529d5fd37eSMatt Roper 		crtc_state->min_voltage_level = 3;
3353005e9537SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 11 && crtc_state->port_clock > 594000)
3354379bc100SJani Nikula 		crtc_state->min_voltage_level = 1;
3355379bc100SJani Nikula }
3356379bc100SJani Nikula 
3357dc5b8ed5SVille Syrjälä static enum transcoder bdw_transcoder_master_readout(struct drm_i915_private *dev_priv,
335802d8ea47SVille Syrjälä 						     enum transcoder cpu_transcoder)
335902d8ea47SVille Syrjälä {
3360dc5b8ed5SVille Syrjälä 	u32 master_select;
336102d8ea47SVille Syrjälä 
3362005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
3363dc5b8ed5SVille Syrjälä 		u32 ctl2 = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL2(cpu_transcoder));
336402d8ea47SVille Syrjälä 
336502d8ea47SVille Syrjälä 		if ((ctl2 & PORT_SYNC_MODE_ENABLE) == 0)
336602d8ea47SVille Syrjälä 			return INVALID_TRANSCODER;
336702d8ea47SVille Syrjälä 
3368d4d7d9caSVille Syrjälä 		master_select = REG_FIELD_GET(PORT_SYNC_MODE_MASTER_SELECT_MASK, ctl2);
3369dc5b8ed5SVille Syrjälä 	} else {
3370dc5b8ed5SVille Syrjälä 		u32 ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3371dc5b8ed5SVille Syrjälä 
3372dc5b8ed5SVille Syrjälä 		if ((ctl & TRANS_DDI_PORT_SYNC_ENABLE) == 0)
3373dc5b8ed5SVille Syrjälä 			return INVALID_TRANSCODER;
3374dc5b8ed5SVille Syrjälä 
3375dc5b8ed5SVille Syrjälä 		master_select = REG_FIELD_GET(TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK, ctl);
3376dc5b8ed5SVille Syrjälä 	}
337702d8ea47SVille Syrjälä 
337802d8ea47SVille Syrjälä 	if (master_select == 0)
337902d8ea47SVille Syrjälä 		return TRANSCODER_EDP;
338002d8ea47SVille Syrjälä 	else
338102d8ea47SVille Syrjälä 		return master_select - 1;
338202d8ea47SVille Syrjälä }
338302d8ea47SVille Syrjälä 
3384dc5b8ed5SVille Syrjälä static void bdw_get_trans_port_sync_config(struct intel_crtc_state *crtc_state)
338502d8ea47SVille Syrjälä {
338602d8ea47SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
338702d8ea47SVille Syrjälä 	u32 transcoders = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
338802d8ea47SVille Syrjälä 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
338902d8ea47SVille Syrjälä 	enum transcoder cpu_transcoder;
339002d8ea47SVille Syrjälä 
339102d8ea47SVille Syrjälä 	crtc_state->master_transcoder =
3392dc5b8ed5SVille Syrjälä 		bdw_transcoder_master_readout(dev_priv, crtc_state->cpu_transcoder);
339302d8ea47SVille Syrjälä 
339402d8ea47SVille Syrjälä 	for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder, transcoders) {
339502d8ea47SVille Syrjälä 		enum intel_display_power_domain power_domain;
339602d8ea47SVille Syrjälä 		intel_wakeref_t trans_wakeref;
339702d8ea47SVille Syrjälä 
339802d8ea47SVille Syrjälä 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
339902d8ea47SVille Syrjälä 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
340002d8ea47SVille Syrjälä 								   power_domain);
340102d8ea47SVille Syrjälä 
340202d8ea47SVille Syrjälä 		if (!trans_wakeref)
340302d8ea47SVille Syrjälä 			continue;
340402d8ea47SVille Syrjälä 
3405dc5b8ed5SVille Syrjälä 		if (bdw_transcoder_master_readout(dev_priv, cpu_transcoder) ==
340602d8ea47SVille Syrjälä 		    crtc_state->cpu_transcoder)
340702d8ea47SVille Syrjälä 			crtc_state->sync_mode_slaves_mask |= BIT(cpu_transcoder);
340802d8ea47SVille Syrjälä 
340902d8ea47SVille Syrjälä 		intel_display_power_put(dev_priv, power_domain, trans_wakeref);
341002d8ea47SVille Syrjälä 	}
341102d8ea47SVille Syrjälä 
341202d8ea47SVille Syrjälä 	drm_WARN_ON(&dev_priv->drm,
341302d8ea47SVille Syrjälä 		    crtc_state->master_transcoder != INVALID_TRANSCODER &&
341402d8ea47SVille Syrjälä 		    crtc_state->sync_mode_slaves_mask);
341502d8ea47SVille Syrjälä }
341602d8ea47SVille Syrjälä 
34170385eceaSManasi Navare static void intel_ddi_read_func_ctl(struct intel_encoder *encoder,
3418379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config)
3419379bc100SJani Nikula {
3420379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3421f15f01a7SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3422379bc100SJani Nikula 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
3423a44289b9SUma Shankar 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3424379bc100SJani Nikula 	u32 temp, flags = 0;
3425379bc100SJani Nikula 
3426f7960e7fSJani Nikula 	temp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3427379bc100SJani Nikula 	if (temp & TRANS_DDI_PHSYNC)
3428379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PHSYNC;
3429379bc100SJani Nikula 	else
3430379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NHSYNC;
3431379bc100SJani Nikula 	if (temp & TRANS_DDI_PVSYNC)
3432379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PVSYNC;
3433379bc100SJani Nikula 	else
3434379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NVSYNC;
3435379bc100SJani Nikula 
34361326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.flags |= flags;
3437379bc100SJani Nikula 
3438379bc100SJani Nikula 	switch (temp & TRANS_DDI_BPC_MASK) {
3439379bc100SJani Nikula 	case TRANS_DDI_BPC_6:
3440379bc100SJani Nikula 		pipe_config->pipe_bpp = 18;
3441379bc100SJani Nikula 		break;
3442379bc100SJani Nikula 	case TRANS_DDI_BPC_8:
3443379bc100SJani Nikula 		pipe_config->pipe_bpp = 24;
3444379bc100SJani Nikula 		break;
3445379bc100SJani Nikula 	case TRANS_DDI_BPC_10:
3446379bc100SJani Nikula 		pipe_config->pipe_bpp = 30;
3447379bc100SJani Nikula 		break;
3448379bc100SJani Nikula 	case TRANS_DDI_BPC_12:
3449379bc100SJani Nikula 		pipe_config->pipe_bpp = 36;
3450379bc100SJani Nikula 		break;
3451379bc100SJani Nikula 	default:
3452379bc100SJani Nikula 		break;
3453379bc100SJani Nikula 	}
3454379bc100SJani Nikula 
3455379bc100SJani Nikula 	switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
3456379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
3457379bc100SJani Nikula 		pipe_config->has_hdmi_sink = true;
3458379bc100SJani Nikula 
3459379bc100SJani Nikula 		pipe_config->infoframes.enable |=
3460379bc100SJani Nikula 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
3461379bc100SJani Nikula 
3462379bc100SJani Nikula 		if (pipe_config->infoframes.enable)
3463379bc100SJani Nikula 			pipe_config->has_infoframe = true;
3464379bc100SJani Nikula 
3465379bc100SJani Nikula 		if (temp & TRANS_DDI_HDMI_SCRAMBLING)
3466379bc100SJani Nikula 			pipe_config->hdmi_scrambling = true;
3467379bc100SJani Nikula 		if (temp & TRANS_DDI_HIGH_TMDS_CHAR_RATE)
3468379bc100SJani Nikula 			pipe_config->hdmi_high_tmds_clock_ratio = true;
3469df561f66SGustavo A. R. Silva 		fallthrough;
3470379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
3471379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);
3472379bc100SJani Nikula 		pipe_config->lane_count = 4;
3473379bc100SJani Nikula 		break;
3474379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
3475379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
3476379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
3477379bc100SJani Nikula 		else
3478379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
3479379bc100SJani Nikula 		pipe_config->lane_count =
3480379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
3481f15f01a7SVille Syrjälä 		intel_dp_get_m_n(crtc, pipe_config);
34828aa940c8SMaarten Lankhorst 
3483005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 11) {
3484ef79fafeSVille Syrjälä 			i915_reg_t dp_tp_ctl = dp_tp_ctl_reg(encoder, pipe_config);
34858aa940c8SMaarten Lankhorst 
34868aa940c8SMaarten Lankhorst 			pipe_config->fec_enable =
3487f7960e7fSJani Nikula 				intel_de_read(dev_priv, dp_tp_ctl) & DP_TP_CTL_FEC_ENABLE;
34888aa940c8SMaarten Lankhorst 
348947bdb1caSJani Nikula 			drm_dbg_kms(&dev_priv->drm,
349047bdb1caSJani Nikula 				    "[ENCODER:%d:%s] Fec status: %u\n",
34918aa940c8SMaarten Lankhorst 				    encoder->base.base.id, encoder->base.name,
34928aa940c8SMaarten Lankhorst 				    pipe_config->fec_enable);
34938aa940c8SMaarten Lankhorst 		}
34948aa940c8SMaarten Lankhorst 
3495a44289b9SUma Shankar 		if (dig_port->lspcon.active && dig_port->dp.has_hdmi_sink)
3496a44289b9SUma Shankar 			pipe_config->infoframes.enable |=
3497a44289b9SUma Shankar 				intel_lspcon_infoframes_enabled(encoder, pipe_config);
3498a44289b9SUma Shankar 		else
3499dee66f3eSGwan-gyeong Mun 			pipe_config->infoframes.enable |=
3500dee66f3eSGwan-gyeong Mun 				intel_hdmi_infoframes_enabled(encoder, pipe_config);
3501379bc100SJani Nikula 		break;
350265213594SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI_OR_128B132B:
350365213594SJani Nikula 		if (!HAS_DP20(dev_priv)) {
350465213594SJani Nikula 			/* FDI */
350565213594SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
350665213594SJani Nikula 			break;
350765213594SJani Nikula 		}
350865213594SJani Nikula 		fallthrough; /* 128b/132b */
3509379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
3510379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_DP_MST);
3511379bc100SJani Nikula 		pipe_config->lane_count =
3512379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
35136671c367SJosé Roberto de Souza 
3514005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12)
35156671c367SJosé Roberto de Souza 			pipe_config->mst_master_transcoder =
35166671c367SJosé Roberto de Souza 					REG_FIELD_GET(TRANS_DDI_MST_TRANSPORT_SELECT_MASK, temp);
35176671c367SJosé Roberto de Souza 
3518f15f01a7SVille Syrjälä 		intel_dp_get_m_n(crtc, pipe_config);
3519dee66f3eSGwan-gyeong Mun 
3520dee66f3eSGwan-gyeong Mun 		pipe_config->infoframes.enable |=
3521dee66f3eSGwan-gyeong Mun 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
3522379bc100SJani Nikula 		break;
3523379bc100SJani Nikula 	default:
3524379bc100SJani Nikula 		break;
3525379bc100SJani Nikula 	}
35260385eceaSManasi Navare }
35270385eceaSManasi Navare 
3528351221ffSVille Syrjälä static void intel_ddi_get_config(struct intel_encoder *encoder,
35290385eceaSManasi Navare 				 struct intel_crtc_state *pipe_config)
35300385eceaSManasi Navare {
35310385eceaSManasi Navare 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
35320385eceaSManasi Navare 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
35330385eceaSManasi Navare 
35340385eceaSManasi Navare 	/* XXX: DSI transcoder paranoia */
35350385eceaSManasi Navare 	if (drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder)))
35360385eceaSManasi Navare 		return;
35370385eceaSManasi Navare 
35380385eceaSManasi Navare 	intel_ddi_read_func_ctl(encoder, pipe_config);
3539379bc100SJani Nikula 
35405b616a29SJani Nikula 	intel_ddi_mso_get_config(encoder, pipe_config);
35415b616a29SJani Nikula 
3542379bc100SJani Nikula 	pipe_config->has_audio =
3543379bc100SJani Nikula 		intel_ddi_is_audio_enabled(dev_priv, cpu_transcoder);
3544379bc100SJani Nikula 
3545379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.bpp &&
3546379bc100SJani Nikula 	    pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
3547379bc100SJani Nikula 		/*
3548379bc100SJani Nikula 		 * This is a big fat ugly hack.
3549379bc100SJani Nikula 		 *
3550379bc100SJani Nikula 		 * Some machines in UEFI boot mode provide us a VBT that has 18
3551379bc100SJani Nikula 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
3552379bc100SJani Nikula 		 * unknown we fail to light up. Yet the same BIOS boots up with
3553379bc100SJani Nikula 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
3554379bc100SJani Nikula 		 * max, not what it tells us to use.
3555379bc100SJani Nikula 		 *
3556379bc100SJani Nikula 		 * Note: This will still be broken if the eDP panel is not lit
3557379bc100SJani Nikula 		 * up by the BIOS, and thus we can't get the mode at module
3558379bc100SJani Nikula 		 * load.
3559379bc100SJani Nikula 		 */
356047bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
356147bdb1caSJani Nikula 			    "pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
3562379bc100SJani Nikula 			    pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
3563379bc100SJani Nikula 		dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
3564379bc100SJani Nikula 	}
3565379bc100SJani Nikula 
3566351221ffSVille Syrjälä 	ddi_dotclock_get(pipe_config);
3567379bc100SJani Nikula 
35682446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3569379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
3570379bc100SJani Nikula 			bxt_ddi_phy_get_lane_lat_optim_mask(encoder);
3571379bc100SJani Nikula 
3572379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3573379bc100SJani Nikula 
3574379bc100SJani Nikula 	intel_hdmi_read_gcp_infoframe(encoder, pipe_config);
3575379bc100SJani Nikula 
3576379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3577379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_AVI,
3578379bc100SJani Nikula 			     &pipe_config->infoframes.avi);
3579379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3580379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_SPD,
3581379bc100SJani Nikula 			     &pipe_config->infoframes.spd);
3582379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3583379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_VENDOR,
3584379bc100SJani Nikula 			     &pipe_config->infoframes.hdmi);
3585379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3586379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_DRM,
3587379bc100SJani Nikula 			     &pipe_config->infoframes.drm);
358802d8ea47SVille Syrjälä 
3589005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 8)
3590dc5b8ed5SVille Syrjälä 		bdw_get_trans_port_sync_config(pipe_config);
3591dee66f3eSGwan-gyeong Mun 
3592dee66f3eSGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, HDMI_PACKET_TYPE_GAMUT_METADATA);
35932c3928e4SGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, DP_SDP_VSC);
359478b772e1SJosé Roberto de Souza 
359578b772e1SJosé Roberto de Souza 	intel_psr_get_config(encoder, pipe_config);
3596379bc100SJani Nikula }
3597379bc100SJani Nikula 
3598351221ffSVille Syrjälä void intel_ddi_get_clock(struct intel_encoder *encoder,
3599351221ffSVille Syrjälä 			 struct intel_crtc_state *crtc_state,
3600351221ffSVille Syrjälä 			 struct intel_shared_dpll *pll)
3601351221ffSVille Syrjälä {
3602351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3603351221ffSVille Syrjälä 	enum icl_port_dpll_id port_dpll_id = ICL_PORT_DPLL_DEFAULT;
3604351221ffSVille Syrjälä 	struct icl_port_dpll *port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
3605351221ffSVille Syrjälä 	bool pll_active;
3606351221ffSVille Syrjälä 
3607086877a1SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
3608086877a1SVille Syrjälä 		return;
3609086877a1SVille Syrjälä 
3610351221ffSVille Syrjälä 	port_dpll->pll = pll;
3611351221ffSVille Syrjälä 	pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
3612351221ffSVille Syrjälä 	drm_WARN_ON(&i915->drm, !pll_active);
3613351221ffSVille Syrjälä 
3614351221ffSVille Syrjälä 	icl_set_active_port_dpll(crtc_state, port_dpll_id);
3615351221ffSVille Syrjälä 
3616351221ffSVille Syrjälä 	crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
3617351221ffSVille Syrjälä 						     &crtc_state->dpll_hw_state);
3618351221ffSVille Syrjälä }
3619351221ffSVille Syrjälä 
3620865b73eaSMatt Roper static void dg2_ddi_get_config(struct intel_encoder *encoder,
3621865b73eaSMatt Roper 				struct intel_crtc_state *crtc_state)
3622865b73eaSMatt Roper {
3623865b73eaSMatt Roper 	intel_mpllb_readout_hw_state(encoder, &crtc_state->mpllb_state);
3624865b73eaSMatt Roper 	crtc_state->port_clock = intel_mpllb_calc_port_clock(encoder, &crtc_state->mpllb_state);
3625865b73eaSMatt Roper 
3626865b73eaSMatt Roper 	intel_ddi_get_config(encoder, crtc_state);
3627865b73eaSMatt Roper }
3628865b73eaSMatt Roper 
3629351221ffSVille Syrjälä static void adls_ddi_get_config(struct intel_encoder *encoder,
3630351221ffSVille Syrjälä 				struct intel_crtc_state *crtc_state)
3631351221ffSVille Syrjälä {
3632351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, adls_ddi_get_pll(encoder));
3633351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3634351221ffSVille Syrjälä }
3635351221ffSVille Syrjälä 
3636351221ffSVille Syrjälä static void rkl_ddi_get_config(struct intel_encoder *encoder,
3637351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3638351221ffSVille Syrjälä {
3639351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, rkl_ddi_get_pll(encoder));
3640351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3641351221ffSVille Syrjälä }
3642351221ffSVille Syrjälä 
3643351221ffSVille Syrjälä static void dg1_ddi_get_config(struct intel_encoder *encoder,
3644351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3645351221ffSVille Syrjälä {
3646351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, dg1_ddi_get_pll(encoder));
3647351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3648351221ffSVille Syrjälä }
3649351221ffSVille Syrjälä 
3650351221ffSVille Syrjälä static void icl_ddi_combo_get_config(struct intel_encoder *encoder,
3651351221ffSVille Syrjälä 				     struct intel_crtc_state *crtc_state)
3652351221ffSVille Syrjälä {
3653351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, icl_ddi_combo_get_pll(encoder));
3654351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3655351221ffSVille Syrjälä }
3656351221ffSVille Syrjälä 
3657086877a1SVille Syrjälä static void icl_ddi_tc_get_clock(struct intel_encoder *encoder,
3658086877a1SVille Syrjälä 				 struct intel_crtc_state *crtc_state,
3659086877a1SVille Syrjälä 				 struct intel_shared_dpll *pll)
3660351221ffSVille Syrjälä {
3661351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3662351221ffSVille Syrjälä 	enum icl_port_dpll_id port_dpll_id;
3663351221ffSVille Syrjälä 	struct icl_port_dpll *port_dpll;
3664351221ffSVille Syrjälä 	bool pll_active;
3665351221ffSVille Syrjälä 
3666086877a1SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
3667086877a1SVille Syrjälä 		return;
3668351221ffSVille Syrjälä 
3669351221ffSVille Syrjälä 	if (intel_get_shared_dpll_id(i915, pll) == DPLL_ID_ICL_TBTPLL)
3670351221ffSVille Syrjälä 		port_dpll_id = ICL_PORT_DPLL_DEFAULT;
3671351221ffSVille Syrjälä 	else
3672351221ffSVille Syrjälä 		port_dpll_id = ICL_PORT_DPLL_MG_PHY;
3673351221ffSVille Syrjälä 
3674351221ffSVille Syrjälä 	port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
3675351221ffSVille Syrjälä 
3676351221ffSVille Syrjälä 	port_dpll->pll = pll;
3677351221ffSVille Syrjälä 	pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
3678351221ffSVille Syrjälä 	drm_WARN_ON(&i915->drm, !pll_active);
3679351221ffSVille Syrjälä 
3680351221ffSVille Syrjälä 	icl_set_active_port_dpll(crtc_state, port_dpll_id);
3681351221ffSVille Syrjälä 
3682351221ffSVille Syrjälä 	if (intel_get_shared_dpll_id(i915, crtc_state->shared_dpll) == DPLL_ID_ICL_TBTPLL)
3683351221ffSVille Syrjälä 		crtc_state->port_clock = icl_calc_tbt_pll_link(i915, encoder->port);
3684351221ffSVille Syrjälä 	else
3685351221ffSVille Syrjälä 		crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
3686351221ffSVille Syrjälä 							     &crtc_state->dpll_hw_state);
3687086877a1SVille Syrjälä }
3688351221ffSVille Syrjälä 
3689086877a1SVille Syrjälä static void icl_ddi_tc_get_config(struct intel_encoder *encoder,
3690086877a1SVille Syrjälä 				  struct intel_crtc_state *crtc_state)
3691086877a1SVille Syrjälä {
3692086877a1SVille Syrjälä 	icl_ddi_tc_get_clock(encoder, crtc_state, icl_ddi_tc_get_pll(encoder));
3693351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3694351221ffSVille Syrjälä }
3695351221ffSVille Syrjälä 
3696351221ffSVille Syrjälä static void bxt_ddi_get_config(struct intel_encoder *encoder,
3697351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3698351221ffSVille Syrjälä {
3699351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, bxt_ddi_get_pll(encoder));
3700351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3701351221ffSVille Syrjälä }
3702351221ffSVille Syrjälä 
3703351221ffSVille Syrjälä static void skl_ddi_get_config(struct intel_encoder *encoder,
3704351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3705351221ffSVille Syrjälä {
3706351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, skl_ddi_get_pll(encoder));
3707351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3708351221ffSVille Syrjälä }
3709351221ffSVille Syrjälä 
3710351221ffSVille Syrjälä void hsw_ddi_get_config(struct intel_encoder *encoder,
3711351221ffSVille Syrjälä 			struct intel_crtc_state *crtc_state)
3712351221ffSVille Syrjälä {
3713351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, hsw_ddi_get_pll(encoder));
3714351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3715351221ffSVille Syrjälä }
3716351221ffSVille Syrjälä 
3717f9e76a6eSImre Deak static void intel_ddi_sync_state(struct intel_encoder *encoder,
3718f9e76a6eSImre Deak 				 const struct intel_crtc_state *crtc_state)
3719f9e76a6eSImre Deak {
37207194dc99SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
37217194dc99SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
37227194dc99SImre Deak 
37237194dc99SImre Deak 	if (intel_phy_is_tc(i915, phy))
37247194dc99SImre Deak 		intel_tc_port_sanitize(enc_to_dig_port(encoder));
37257194dc99SImre Deak 
37267194dc99SImre Deak 	if (crtc_state && intel_crtc_has_dp_encoder(crtc_state))
3727f9e76a6eSImre Deak 		intel_dp_sync_state(encoder, crtc_state);
3728f9e76a6eSImre Deak }
3729f9e76a6eSImre Deak 
3730b671d6efSImre Deak static bool intel_ddi_initial_fastset_check(struct intel_encoder *encoder,
3731b671d6efSImre Deak 					    struct intel_crtc_state *crtc_state)
3732b671d6efSImre Deak {
3733b671d6efSImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state))
3734b671d6efSImre Deak 		return intel_dp_initial_fastset_check(encoder, crtc_state);
3735b671d6efSImre Deak 
3736b671d6efSImre Deak 	return true;
3737b671d6efSImre Deak }
3738b671d6efSImre Deak 
3739379bc100SJani Nikula static enum intel_output_type
3740379bc100SJani Nikula intel_ddi_compute_output_type(struct intel_encoder *encoder,
3741379bc100SJani Nikula 			      struct intel_crtc_state *crtc_state,
3742379bc100SJani Nikula 			      struct drm_connector_state *conn_state)
3743379bc100SJani Nikula {
3744379bc100SJani Nikula 	switch (conn_state->connector->connector_type) {
3745379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_HDMIA:
3746379bc100SJani Nikula 		return INTEL_OUTPUT_HDMI;
3747379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_eDP:
3748379bc100SJani Nikula 		return INTEL_OUTPUT_EDP;
3749379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_DisplayPort:
3750379bc100SJani Nikula 		return INTEL_OUTPUT_DP;
3751379bc100SJani Nikula 	default:
3752379bc100SJani Nikula 		MISSING_CASE(conn_state->connector->connector_type);
3753379bc100SJani Nikula 		return INTEL_OUTPUT_UNUSED;
3754379bc100SJani Nikula 	}
3755379bc100SJani Nikula }
3756379bc100SJani Nikula 
3757379bc100SJani Nikula static int intel_ddi_compute_config(struct intel_encoder *encoder,
3758379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config,
3759379bc100SJani Nikula 				    struct drm_connector_state *conn_state)
3760379bc100SJani Nikula {
37612225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3762379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3763379bc100SJani Nikula 	enum port port = encoder->port;
3764379bc100SJani Nikula 	int ret;
3765379bc100SJani Nikula 
376610cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
3767379bc100SJani Nikula 		pipe_config->cpu_transcoder = TRANSCODER_EDP;
3768379bc100SJani Nikula 
3769bdacf087SAnshuman Gupta 	if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI)) {
3770379bc100SJani Nikula 		ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
3771bdacf087SAnshuman Gupta 	} else {
3772379bc100SJani Nikula 		ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
3773bdacf087SAnshuman Gupta 	}
3774bdacf087SAnshuman Gupta 
3775379bc100SJani Nikula 	if (ret)
3776379bc100SJani Nikula 		return ret;
3777379bc100SJani Nikula 
3778379bc100SJani Nikula 	if (IS_HASWELL(dev_priv) && crtc->pipe == PIPE_A &&
3779379bc100SJani Nikula 	    pipe_config->cpu_transcoder == TRANSCODER_EDP)
3780379bc100SJani Nikula 		pipe_config->pch_pfit.force_thru =
3781379bc100SJani Nikula 			pipe_config->pch_pfit.enabled ||
3782379bc100SJani Nikula 			pipe_config->crc_enabled;
3783379bc100SJani Nikula 
37842446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3785379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
3786379bc100SJani Nikula 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
3787379bc100SJani Nikula 
3788379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3789379bc100SJani Nikula 
3790379bc100SJani Nikula 	return 0;
3791379bc100SJani Nikula }
3792379bc100SJani Nikula 
3793b50a1aa6SManasi Navare static bool mode_equal(const struct drm_display_mode *mode1,
3794b50a1aa6SManasi Navare 		       const struct drm_display_mode *mode2)
3795b50a1aa6SManasi Navare {
3796b50a1aa6SManasi Navare 	return drm_mode_match(mode1, mode2,
3797b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_TIMINGS |
3798b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_FLAGS |
3799b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_3D_FLAGS) &&
3800b50a1aa6SManasi Navare 		mode1->clock == mode2->clock; /* we want an exact match */
3801b50a1aa6SManasi Navare }
3802b50a1aa6SManasi Navare 
3803b50a1aa6SManasi Navare static bool m_n_equal(const struct intel_link_m_n *m_n_1,
3804b50a1aa6SManasi Navare 		      const struct intel_link_m_n *m_n_2)
3805b50a1aa6SManasi Navare {
3806b50a1aa6SManasi Navare 	return m_n_1->tu == m_n_2->tu &&
3807b50a1aa6SManasi Navare 		m_n_1->gmch_m == m_n_2->gmch_m &&
3808b50a1aa6SManasi Navare 		m_n_1->gmch_n == m_n_2->gmch_n &&
3809b50a1aa6SManasi Navare 		m_n_1->link_m == m_n_2->link_m &&
3810b50a1aa6SManasi Navare 		m_n_1->link_n == m_n_2->link_n;
3811b50a1aa6SManasi Navare }
3812b50a1aa6SManasi Navare 
3813b50a1aa6SManasi Navare static bool crtcs_port_sync_compatible(const struct intel_crtc_state *crtc_state1,
3814b50a1aa6SManasi Navare 				       const struct intel_crtc_state *crtc_state2)
3815b50a1aa6SManasi Navare {
3816b50a1aa6SManasi Navare 	return crtc_state1->hw.active && crtc_state2->hw.active &&
3817b50a1aa6SManasi Navare 		crtc_state1->output_types == crtc_state2->output_types &&
3818b50a1aa6SManasi Navare 		crtc_state1->output_format == crtc_state2->output_format &&
3819b50a1aa6SManasi Navare 		crtc_state1->lane_count == crtc_state2->lane_count &&
3820b50a1aa6SManasi Navare 		crtc_state1->port_clock == crtc_state2->port_clock &&
3821b50a1aa6SManasi Navare 		mode_equal(&crtc_state1->hw.adjusted_mode,
3822b50a1aa6SManasi Navare 			   &crtc_state2->hw.adjusted_mode) &&
3823b50a1aa6SManasi Navare 		m_n_equal(&crtc_state1->dp_m_n, &crtc_state2->dp_m_n);
3824b50a1aa6SManasi Navare }
3825b50a1aa6SManasi Navare 
3826b50a1aa6SManasi Navare static u8
3827b50a1aa6SManasi Navare intel_ddi_port_sync_transcoders(const struct intel_crtc_state *ref_crtc_state,
3828b50a1aa6SManasi Navare 				int tile_group_id)
3829b50a1aa6SManasi Navare {
3830b50a1aa6SManasi Navare 	struct drm_connector *connector;
3831b50a1aa6SManasi Navare 	const struct drm_connector_state *conn_state;
3832b50a1aa6SManasi Navare 	struct drm_i915_private *dev_priv = to_i915(ref_crtc_state->uapi.crtc->dev);
3833b50a1aa6SManasi Navare 	struct intel_atomic_state *state =
3834b50a1aa6SManasi Navare 		to_intel_atomic_state(ref_crtc_state->uapi.state);
3835b50a1aa6SManasi Navare 	u8 transcoders = 0;
3836b50a1aa6SManasi Navare 	int i;
3837b50a1aa6SManasi Navare 
3838dc5b8ed5SVille Syrjälä 	/*
3839dc5b8ed5SVille Syrjälä 	 * We don't enable port sync on BDW due to missing w/as and
3840dc5b8ed5SVille Syrjälä 	 * due to not having adjusted the modeset sequence appropriately.
3841dc5b8ed5SVille Syrjälä 	 */
3842005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 9)
3843b50a1aa6SManasi Navare 		return 0;
3844b50a1aa6SManasi Navare 
3845b50a1aa6SManasi Navare 	if (!intel_crtc_has_type(ref_crtc_state, INTEL_OUTPUT_DP))
3846b50a1aa6SManasi Navare 		return 0;
3847b50a1aa6SManasi Navare 
3848b50a1aa6SManasi Navare 	for_each_new_connector_in_state(&state->base, connector, conn_state, i) {
3849b50a1aa6SManasi Navare 		struct intel_crtc *crtc = to_intel_crtc(conn_state->crtc);
3850b50a1aa6SManasi Navare 		const struct intel_crtc_state *crtc_state;
3851b50a1aa6SManasi Navare 
3852b50a1aa6SManasi Navare 		if (!crtc)
3853b50a1aa6SManasi Navare 			continue;
3854b50a1aa6SManasi Navare 
3855b50a1aa6SManasi Navare 		if (!connector->has_tile ||
3856b50a1aa6SManasi Navare 		    connector->tile_group->id !=
3857b50a1aa6SManasi Navare 		    tile_group_id)
3858b50a1aa6SManasi Navare 			continue;
3859b50a1aa6SManasi Navare 		crtc_state = intel_atomic_get_new_crtc_state(state,
3860b50a1aa6SManasi Navare 							     crtc);
3861b50a1aa6SManasi Navare 		if (!crtcs_port_sync_compatible(ref_crtc_state,
3862b50a1aa6SManasi Navare 						crtc_state))
3863b50a1aa6SManasi Navare 			continue;
3864b50a1aa6SManasi Navare 		transcoders |= BIT(crtc_state->cpu_transcoder);
3865b50a1aa6SManasi Navare 	}
3866b50a1aa6SManasi Navare 
3867b50a1aa6SManasi Navare 	return transcoders;
3868b50a1aa6SManasi Navare }
3869b50a1aa6SManasi Navare 
3870b50a1aa6SManasi Navare static int intel_ddi_compute_config_late(struct intel_encoder *encoder,
3871b50a1aa6SManasi Navare 					 struct intel_crtc_state *crtc_state,
3872b50a1aa6SManasi Navare 					 struct drm_connector_state *conn_state)
3873b50a1aa6SManasi Navare {
387447bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3875b50a1aa6SManasi Navare 	struct drm_connector *connector = conn_state->connector;
3876b50a1aa6SManasi Navare 	u8 port_sync_transcoders = 0;
3877b50a1aa6SManasi Navare 
387847bdb1caSJani Nikula 	drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] [CRTC:%d:%s]",
3879b50a1aa6SManasi Navare 		    encoder->base.base.id, encoder->base.name,
3880b50a1aa6SManasi Navare 		    crtc_state->uapi.crtc->base.id, crtc_state->uapi.crtc->name);
3881b50a1aa6SManasi Navare 
3882b50a1aa6SManasi Navare 	if (connector->has_tile)
3883b50a1aa6SManasi Navare 		port_sync_transcoders = intel_ddi_port_sync_transcoders(crtc_state,
3884b50a1aa6SManasi Navare 									connector->tile_group->id);
3885b50a1aa6SManasi Navare 
3886b50a1aa6SManasi Navare 	/*
3887b50a1aa6SManasi Navare 	 * EDP Transcoders cannot be ensalved
3888b50a1aa6SManasi Navare 	 * make them a master always when present
3889b50a1aa6SManasi Navare 	 */
3890b50a1aa6SManasi Navare 	if (port_sync_transcoders & BIT(TRANSCODER_EDP))
3891b50a1aa6SManasi Navare 		crtc_state->master_transcoder = TRANSCODER_EDP;
3892b50a1aa6SManasi Navare 	else
3893b50a1aa6SManasi Navare 		crtc_state->master_transcoder = ffs(port_sync_transcoders) - 1;
3894b50a1aa6SManasi Navare 
3895b50a1aa6SManasi Navare 	if (crtc_state->master_transcoder == crtc_state->cpu_transcoder) {
3896b50a1aa6SManasi Navare 		crtc_state->master_transcoder = INVALID_TRANSCODER;
3897b50a1aa6SManasi Navare 		crtc_state->sync_mode_slaves_mask =
3898b50a1aa6SManasi Navare 			port_sync_transcoders & ~BIT(crtc_state->cpu_transcoder);
3899b50a1aa6SManasi Navare 	}
3900b50a1aa6SManasi Navare 
3901b50a1aa6SManasi Navare 	return 0;
3902b50a1aa6SManasi Navare }
3903b50a1aa6SManasi Navare 
3904379bc100SJani Nikula static void intel_ddi_encoder_destroy(struct drm_encoder *encoder)
3905379bc100SJani Nikula {
39064a300e65SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->dev);
3907b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
39083e0abc76SImre Deak 	enum phy phy = intel_port_to_phy(i915, dig_port->base.port);
3909379bc100SJani Nikula 
3910379bc100SJani Nikula 	intel_dp_encoder_flush_work(encoder);
39113e0abc76SImre Deak 	if (intel_phy_is_tc(i915, phy))
39123e0abc76SImre Deak 		intel_tc_port_flush_work(dig_port);
39134a300e65SImre Deak 	intel_display_power_flush_work(i915);
3914379bc100SJani Nikula 
3915379bc100SJani Nikula 	drm_encoder_cleanup(encoder);
3916a6c6eac9SAnshuman Gupta 	kfree(dig_port->hdcp_port_data.streams);
3917379bc100SJani Nikula 	kfree(dig_port);
3918379bc100SJani Nikula }
3919379bc100SJani Nikula 
3920764f6729SVille Syrjälä static void intel_ddi_encoder_reset(struct drm_encoder *encoder)
3921764f6729SVille Syrjälä {
3922764f6729SVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(encoder));
3923764f6729SVille Syrjälä 
3924764f6729SVille Syrjälä 	intel_dp->reset_link_params = true;
3925764f6729SVille Syrjälä 
3926764f6729SVille Syrjälä 	intel_pps_encoder_reset(intel_dp);
3927764f6729SVille Syrjälä }
3928764f6729SVille Syrjälä 
3929379bc100SJani Nikula static const struct drm_encoder_funcs intel_ddi_funcs = {
3930764f6729SVille Syrjälä 	.reset = intel_ddi_encoder_reset,
3931379bc100SJani Nikula 	.destroy = intel_ddi_encoder_destroy,
3932379bc100SJani Nikula };
3933379bc100SJani Nikula 
3934379bc100SJani Nikula static struct intel_connector *
39357801f3b7SLucas De Marchi intel_ddi_init_dp_connector(struct intel_digital_port *dig_port)
3936379bc100SJani Nikula {
3937379bc100SJani Nikula 	struct intel_connector *connector;
39387801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
3939379bc100SJani Nikula 
3940379bc100SJani Nikula 	connector = intel_connector_alloc();
3941379bc100SJani Nikula 	if (!connector)
3942379bc100SJani Nikula 		return NULL;
3943379bc100SJani Nikula 
39447801f3b7SLucas De Marchi 	dig_port->dp.output_reg = DDI_BUF_CTL(port);
39457801f3b7SLucas De Marchi 	dig_port->dp.prepare_link_retrain = intel_ddi_prepare_link_retrain;
39467801f3b7SLucas De Marchi 	dig_port->dp.set_link_train = intel_ddi_set_link_train;
39477801f3b7SLucas De Marchi 	dig_port->dp.set_idle_link_train = intel_ddi_set_idle_link_train;
3948eee3f911SVille Syrjälä 
39497801f3b7SLucas De Marchi 	dig_port->dp.voltage_max = intel_ddi_dp_voltage_max;
39507801f3b7SLucas De Marchi 	dig_port->dp.preemph_max = intel_ddi_dp_preemph_max;
395153de0a20SVille Syrjälä 
39527801f3b7SLucas De Marchi 	if (!intel_dp_init_connector(dig_port, connector)) {
3953379bc100SJani Nikula 		kfree(connector);
3954379bc100SJani Nikula 		return NULL;
3955379bc100SJani Nikula 	}
3956379bc100SJani Nikula 
3957379bc100SJani Nikula 	return connector;
3958379bc100SJani Nikula }
3959379bc100SJani Nikula 
3960379bc100SJani Nikula static int modeset_pipe(struct drm_crtc *crtc,
3961379bc100SJani Nikula 			struct drm_modeset_acquire_ctx *ctx)
3962379bc100SJani Nikula {
3963379bc100SJani Nikula 	struct drm_atomic_state *state;
3964379bc100SJani Nikula 	struct drm_crtc_state *crtc_state;
3965379bc100SJani Nikula 	int ret;
3966379bc100SJani Nikula 
3967379bc100SJani Nikula 	state = drm_atomic_state_alloc(crtc->dev);
3968379bc100SJani Nikula 	if (!state)
3969379bc100SJani Nikula 		return -ENOMEM;
3970379bc100SJani Nikula 
3971379bc100SJani Nikula 	state->acquire_ctx = ctx;
3972379bc100SJani Nikula 
3973379bc100SJani Nikula 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
3974379bc100SJani Nikula 	if (IS_ERR(crtc_state)) {
3975379bc100SJani Nikula 		ret = PTR_ERR(crtc_state);
3976379bc100SJani Nikula 		goto out;
3977379bc100SJani Nikula 	}
3978379bc100SJani Nikula 
3979379bc100SJani Nikula 	crtc_state->connectors_changed = true;
3980379bc100SJani Nikula 
3981379bc100SJani Nikula 	ret = drm_atomic_commit(state);
3982379bc100SJani Nikula out:
3983379bc100SJani Nikula 	drm_atomic_state_put(state);
3984379bc100SJani Nikula 
3985379bc100SJani Nikula 	return ret;
3986379bc100SJani Nikula }
3987379bc100SJani Nikula 
3988379bc100SJani Nikula static int intel_hdmi_reset_link(struct intel_encoder *encoder,
3989379bc100SJani Nikula 				 struct drm_modeset_acquire_ctx *ctx)
3990379bc100SJani Nikula {
3991379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3992b7d02c3aSVille Syrjälä 	struct intel_hdmi *hdmi = enc_to_intel_hdmi(encoder);
3993379bc100SJani Nikula 	struct intel_connector *connector = hdmi->attached_connector;
3994379bc100SJani Nikula 	struct i2c_adapter *adapter =
3995379bc100SJani Nikula 		intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
3996379bc100SJani Nikula 	struct drm_connector_state *conn_state;
3997379bc100SJani Nikula 	struct intel_crtc_state *crtc_state;
3998379bc100SJani Nikula 	struct intel_crtc *crtc;
3999379bc100SJani Nikula 	u8 config;
4000379bc100SJani Nikula 	int ret;
4001379bc100SJani Nikula 
4002379bc100SJani Nikula 	if (!connector || connector->base.status != connector_status_connected)
4003379bc100SJani Nikula 		return 0;
4004379bc100SJani Nikula 
4005379bc100SJani Nikula 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4006379bc100SJani Nikula 			       ctx);
4007379bc100SJani Nikula 	if (ret)
4008379bc100SJani Nikula 		return ret;
4009379bc100SJani Nikula 
4010379bc100SJani Nikula 	conn_state = connector->base.state;
4011379bc100SJani Nikula 
4012379bc100SJani Nikula 	crtc = to_intel_crtc(conn_state->crtc);
4013379bc100SJani Nikula 	if (!crtc)
4014379bc100SJani Nikula 		return 0;
4015379bc100SJani Nikula 
4016379bc100SJani Nikula 	ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4017379bc100SJani Nikula 	if (ret)
4018379bc100SJani Nikula 		return ret;
4019379bc100SJani Nikula 
4020379bc100SJani Nikula 	crtc_state = to_intel_crtc_state(crtc->base.state);
4021379bc100SJani Nikula 
40221de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm,
40231de143ccSPankaj Bharadiya 		    !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI));
4024379bc100SJani Nikula 
40251326a92cSMaarten Lankhorst 	if (!crtc_state->hw.active)
4026379bc100SJani Nikula 		return 0;
4027379bc100SJani Nikula 
4028379bc100SJani Nikula 	if (!crtc_state->hdmi_high_tmds_clock_ratio &&
4029379bc100SJani Nikula 	    !crtc_state->hdmi_scrambling)
4030379bc100SJani Nikula 		return 0;
4031379bc100SJani Nikula 
4032379bc100SJani Nikula 	if (conn_state->commit &&
4033379bc100SJani Nikula 	    !try_wait_for_completion(&conn_state->commit->hw_done))
4034379bc100SJani Nikula 		return 0;
4035379bc100SJani Nikula 
4036379bc100SJani Nikula 	ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
4037379bc100SJani Nikula 	if (ret < 0) {
403847bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Failed to read TMDS config: %d\n",
403947bdb1caSJani Nikula 			ret);
4040379bc100SJani Nikula 		return 0;
4041379bc100SJani Nikula 	}
4042379bc100SJani Nikula 
4043379bc100SJani Nikula 	if (!!(config & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40) ==
4044379bc100SJani Nikula 	    crtc_state->hdmi_high_tmds_clock_ratio &&
4045379bc100SJani Nikula 	    !!(config & SCDC_SCRAMBLING_ENABLE) ==
4046379bc100SJani Nikula 	    crtc_state->hdmi_scrambling)
4047379bc100SJani Nikula 		return 0;
4048379bc100SJani Nikula 
4049379bc100SJani Nikula 	/*
4050379bc100SJani Nikula 	 * HDMI 2.0 says that one should not send scrambled data
4051379bc100SJani Nikula 	 * prior to configuring the sink scrambling, and that
4052379bc100SJani Nikula 	 * TMDS clock/data transmission should be suspended when
4053379bc100SJani Nikula 	 * changing the TMDS clock rate in the sink. So let's
4054379bc100SJani Nikula 	 * just do a full modeset here, even though some sinks
4055379bc100SJani Nikula 	 * would be perfectly happy if were to just reconfigure
4056379bc100SJani Nikula 	 * the SCDC settings on the fly.
4057379bc100SJani Nikula 	 */
4058379bc100SJani Nikula 	return modeset_pipe(&crtc->base, ctx);
4059379bc100SJani Nikula }
4060379bc100SJani Nikula 
40613944709dSImre Deak static enum intel_hotplug_state
40623944709dSImre Deak intel_ddi_hotplug(struct intel_encoder *encoder,
40638c8919c7SImre Deak 		  struct intel_connector *connector)
4064379bc100SJani Nikula {
4065b4df5405SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4066b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4067699390f7SVille Syrjälä 	struct intel_dp *intel_dp = &dig_port->dp;
4068b4df5405SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4069b4df5405SImre Deak 	bool is_tc = intel_phy_is_tc(i915, phy);
4070379bc100SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
40713944709dSImre Deak 	enum intel_hotplug_state state;
4072379bc100SJani Nikula 	int ret;
4073379bc100SJani Nikula 
4074699390f7SVille Syrjälä 	if (intel_dp->compliance.test_active &&
4075699390f7SVille Syrjälä 	    intel_dp->compliance.test_type == DP_TEST_LINK_PHY_TEST_PATTERN) {
4076699390f7SVille Syrjälä 		intel_dp_phy_test(encoder);
4077699390f7SVille Syrjälä 		/* just do the PHY test and nothing else */
4078699390f7SVille Syrjälä 		return INTEL_HOTPLUG_UNCHANGED;
4079699390f7SVille Syrjälä 	}
4080699390f7SVille Syrjälä 
40818c8919c7SImre Deak 	state = intel_encoder_hotplug(encoder, connector);
4082379bc100SJani Nikula 
4083379bc100SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
4084379bc100SJani Nikula 
4085379bc100SJani Nikula 	for (;;) {
4086379bc100SJani Nikula 		if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA)
4087379bc100SJani Nikula 			ret = intel_hdmi_reset_link(encoder, &ctx);
4088379bc100SJani Nikula 		else
4089379bc100SJani Nikula 			ret = intel_dp_retrain_link(encoder, &ctx);
4090379bc100SJani Nikula 
4091379bc100SJani Nikula 		if (ret == -EDEADLK) {
4092379bc100SJani Nikula 			drm_modeset_backoff(&ctx);
4093379bc100SJani Nikula 			continue;
4094379bc100SJani Nikula 		}
4095379bc100SJani Nikula 
4096379bc100SJani Nikula 		break;
4097379bc100SJani Nikula 	}
4098379bc100SJani Nikula 
4099379bc100SJani Nikula 	drm_modeset_drop_locks(&ctx);
4100379bc100SJani Nikula 	drm_modeset_acquire_fini(&ctx);
41013a47ae20SPankaj Bharadiya 	drm_WARN(encoder->base.dev, ret,
41023a47ae20SPankaj Bharadiya 		 "Acquiring modeset locks failed with %i\n", ret);
4103379bc100SJani Nikula 
4104bb80c925SJosé Roberto de Souza 	/*
4105bb80c925SJosé Roberto de Souza 	 * Unpowered type-c dongles can take some time to boot and be
4106bb80c925SJosé Roberto de Souza 	 * responsible, so here giving some time to those dongles to power up
4107bb80c925SJosé Roberto de Souza 	 * and then retrying the probe.
4108bb80c925SJosé Roberto de Souza 	 *
4109bb80c925SJosé Roberto de Souza 	 * On many platforms the HDMI live state signal is known to be
4110bb80c925SJosé Roberto de Souza 	 * unreliable, so we can't use it to detect if a sink is connected or
4111bb80c925SJosé Roberto de Souza 	 * not. Instead we detect if it's connected based on whether we can
4112bb80c925SJosé Roberto de Souza 	 * read the EDID or not. That in turn has a problem during disconnect,
4113bb80c925SJosé Roberto de Souza 	 * since the HPD interrupt may be raised before the DDC lines get
4114bb80c925SJosé Roberto de Souza 	 * disconnected (due to how the required length of DDC vs. HPD
4115bb80c925SJosé Roberto de Souza 	 * connector pins are specified) and so we'll still be able to get a
4116bb80c925SJosé Roberto de Souza 	 * valid EDID. To solve this schedule another detection cycle if this
4117bb80c925SJosé Roberto de Souza 	 * time around we didn't detect any change in the sink's connection
4118bb80c925SJosé Roberto de Souza 	 * status.
4119b4df5405SImre Deak 	 *
4120b4df5405SImre Deak 	 * Type-c connectors which get their HPD signal deasserted then
4121b4df5405SImre Deak 	 * reasserted, without unplugging/replugging the sink from the
4122b4df5405SImre Deak 	 * connector, introduce a delay until the AUX channel communication
4123b4df5405SImre Deak 	 * becomes functional. Retry the detection for 5 seconds on type-c
4124b4df5405SImre Deak 	 * connectors to account for this delay.
4125bb80c925SJosé Roberto de Souza 	 */
4126b4df5405SImre Deak 	if (state == INTEL_HOTPLUG_UNCHANGED &&
4127b4df5405SImre Deak 	    connector->hotplug_retries < (is_tc ? 5 : 1) &&
4128bb80c925SJosé Roberto de Souza 	    !dig_port->dp.is_mst)
4129bb80c925SJosé Roberto de Souza 		state = INTEL_HOTPLUG_RETRY;
4130bb80c925SJosé Roberto de Souza 
41313944709dSImre Deak 	return state;
4132379bc100SJani Nikula }
4133379bc100SJani Nikula 
4134edc0e09cSVille Syrjälä static bool lpt_digital_port_connected(struct intel_encoder *encoder)
4135edc0e09cSVille Syrjälä {
4136edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4137c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.pch_hpd[encoder->hpd_pin];
4138edc0e09cSVille Syrjälä 
4139edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, SDEISR) & bit;
4140edc0e09cSVille Syrjälä }
4141edc0e09cSVille Syrjälä 
4142edc0e09cSVille Syrjälä static bool hsw_digital_port_connected(struct intel_encoder *encoder)
4143edc0e09cSVille Syrjälä {
4144edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4145c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4146edc0e09cSVille Syrjälä 
4147c7e8a3d6SVille Syrjälä 	return intel_de_read(dev_priv, DEISR) & bit;
4148edc0e09cSVille Syrjälä }
4149edc0e09cSVille Syrjälä 
4150edc0e09cSVille Syrjälä static bool bdw_digital_port_connected(struct intel_encoder *encoder)
4151edc0e09cSVille Syrjälä {
4152edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4153c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4154edc0e09cSVille Syrjälä 
4155edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, GEN8_DE_PORT_ISR) & bit;
4156edc0e09cSVille Syrjälä }
4157edc0e09cSVille Syrjälä 
4158379bc100SJani Nikula static struct intel_connector *
41597801f3b7SLucas De Marchi intel_ddi_init_hdmi_connector(struct intel_digital_port *dig_port)
4160379bc100SJani Nikula {
4161379bc100SJani Nikula 	struct intel_connector *connector;
41627801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4163379bc100SJani Nikula 
4164379bc100SJani Nikula 	connector = intel_connector_alloc();
4165379bc100SJani Nikula 	if (!connector)
4166379bc100SJani Nikula 		return NULL;
4167379bc100SJani Nikula 
41687801f3b7SLucas De Marchi 	dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
41697801f3b7SLucas De Marchi 	intel_hdmi_init_connector(dig_port, connector);
4170379bc100SJani Nikula 
4171379bc100SJani Nikula 	return connector;
4172379bc100SJani Nikula }
4173379bc100SJani Nikula 
41747801f3b7SLucas De Marchi static bool intel_ddi_a_force_4_lanes(struct intel_digital_port *dig_port)
4175379bc100SJani Nikula {
41767801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4177379bc100SJani Nikula 
41787801f3b7SLucas De Marchi 	if (dig_port->base.port != PORT_A)
4179379bc100SJani Nikula 		return false;
4180379bc100SJani Nikula 
41817801f3b7SLucas De Marchi 	if (dig_port->saved_port_bits & DDI_A_4_LANES)
4182379bc100SJani Nikula 		return false;
4183379bc100SJani Nikula 
4184379bc100SJani Nikula 	/* Broxton/Geminilake: Bspec says that DDI_A_4_LANES is the only
4185379bc100SJani Nikula 	 *                     supported configuration
4186379bc100SJani Nikula 	 */
41872446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
4188379bc100SJani Nikula 		return true;
4189379bc100SJani Nikula 
4190379bc100SJani Nikula 	return false;
4191379bc100SJani Nikula }
4192379bc100SJani Nikula 
4193379bc100SJani Nikula static int
41947801f3b7SLucas De Marchi intel_ddi_max_lanes(struct intel_digital_port *dig_port)
4195379bc100SJani Nikula {
41967801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
41977801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4198379bc100SJani Nikula 	int max_lanes = 4;
4199379bc100SJani Nikula 
4200005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
4201379bc100SJani Nikula 		return max_lanes;
4202379bc100SJani Nikula 
4203379bc100SJani Nikula 	if (port == PORT_A || port == PORT_E) {
4204f7960e7fSJani Nikula 		if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
4205379bc100SJani Nikula 			max_lanes = port == PORT_A ? 4 : 0;
4206379bc100SJani Nikula 		else
4207379bc100SJani Nikula 			/* Both A and E share 2 lanes */
4208379bc100SJani Nikula 			max_lanes = 2;
4209379bc100SJani Nikula 	}
4210379bc100SJani Nikula 
4211379bc100SJani Nikula 	/*
4212379bc100SJani Nikula 	 * Some BIOS might fail to set this bit on port A if eDP
4213379bc100SJani Nikula 	 * wasn't lit up at boot.  Force this bit set when needed
4214379bc100SJani Nikula 	 * so we use the proper lane count for our calculations.
4215379bc100SJani Nikula 	 */
42167801f3b7SLucas De Marchi 	if (intel_ddi_a_force_4_lanes(dig_port)) {
421747bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
421847bdb1caSJani Nikula 			    "Forcing DDI_A_4_LANES for port A\n");
42197801f3b7SLucas De Marchi 		dig_port->saved_port_bits |= DDI_A_4_LANES;
4220379bc100SJani Nikula 		max_lanes = 4;
4221379bc100SJani Nikula 	}
4222379bc100SJani Nikula 
4223379bc100SJani Nikula 	return max_lanes;
4224379bc100SJani Nikula }
4225379bc100SJani Nikula 
4226ddff9a60SMatt Roper static bool hti_uses_phy(struct drm_i915_private *i915, enum phy phy)
4227ddff9a60SMatt Roper {
4228ddff9a60SMatt Roper 	return i915->hti_state & HDPORT_ENABLED &&
4229ff7fb44dSJosé Roberto de Souza 	       i915->hti_state & HDPORT_DDI_USED(phy);
4230ddff9a60SMatt Roper }
4231ddff9a60SMatt Roper 
4232ed2615a8SMatt Roper static enum hpd_pin xelpd_hpd_pin(struct drm_i915_private *dev_priv,
4233ed2615a8SMatt Roper 				  enum port port)
4234ed2615a8SMatt Roper {
4235ed2615a8SMatt Roper 	if (port >= PORT_D_XELPD)
4236ed2615a8SMatt Roper 		return HPD_PORT_D + port - PORT_D_XELPD;
4237ed2615a8SMatt Roper 	else if (port >= PORT_TC1)
4238ed2615a8SMatt Roper 		return HPD_PORT_TC1 + port - PORT_TC1;
4239ed2615a8SMatt Roper 	else
4240ed2615a8SMatt Roper 		return HPD_PORT_A + port - PORT_A;
4241ed2615a8SMatt Roper }
4242ed2615a8SMatt Roper 
4243229f31e2SLucas De Marchi static enum hpd_pin dg1_hpd_pin(struct drm_i915_private *dev_priv,
4244229f31e2SLucas De Marchi 				enum port port)
4245229f31e2SLucas De Marchi {
42461d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
42471d8ca002SVille Syrjälä 		return HPD_PORT_C + port - PORT_TC1;
4248229f31e2SLucas De Marchi 	else
4249229f31e2SLucas De Marchi 		return HPD_PORT_A + port - PORT_A;
4250229f31e2SLucas De Marchi }
4251229f31e2SLucas De Marchi 
4252da51e4baSVille Syrjälä static enum hpd_pin tgl_hpd_pin(struct drm_i915_private *dev_priv,
4253da51e4baSVille Syrjälä 				enum port port)
4254da51e4baSVille Syrjälä {
42551d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
42561d8ca002SVille Syrjälä 		return HPD_PORT_TC1 + port - PORT_TC1;
4257da51e4baSVille Syrjälä 	else
4258da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4259da51e4baSVille Syrjälä }
4260da51e4baSVille Syrjälä 
4261da51e4baSVille Syrjälä static enum hpd_pin rkl_hpd_pin(struct drm_i915_private *dev_priv,
4262da51e4baSVille Syrjälä 				enum port port)
4263da51e4baSVille Syrjälä {
4264da51e4baSVille Syrjälä 	if (HAS_PCH_TGP(dev_priv))
4265da51e4baSVille Syrjälä 		return tgl_hpd_pin(dev_priv, port);
4266da51e4baSVille Syrjälä 
42671d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
42681d8ca002SVille Syrjälä 		return HPD_PORT_C + port - PORT_TC1;
4269da51e4baSVille Syrjälä 	else
4270da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4271da51e4baSVille Syrjälä }
4272da51e4baSVille Syrjälä 
4273da51e4baSVille Syrjälä static enum hpd_pin icl_hpd_pin(struct drm_i915_private *dev_priv,
4274da51e4baSVille Syrjälä 				enum port port)
4275da51e4baSVille Syrjälä {
4276da51e4baSVille Syrjälä 	if (port >= PORT_C)
4277da51e4baSVille Syrjälä 		return HPD_PORT_TC1 + port - PORT_C;
4278da51e4baSVille Syrjälä 	else
4279da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4280da51e4baSVille Syrjälä }
4281da51e4baSVille Syrjälä 
4282da51e4baSVille Syrjälä static enum hpd_pin ehl_hpd_pin(struct drm_i915_private *dev_priv,
4283da51e4baSVille Syrjälä 				enum port port)
4284da51e4baSVille Syrjälä {
4285da51e4baSVille Syrjälä 	if (port == PORT_D)
4286da51e4baSVille Syrjälä 		return HPD_PORT_A;
4287da51e4baSVille Syrjälä 
4288da51e4baSVille Syrjälä 	if (HAS_PCH_MCC(dev_priv))
4289da51e4baSVille Syrjälä 		return icl_hpd_pin(dev_priv, port);
4290da51e4baSVille Syrjälä 
4291da51e4baSVille Syrjälä 	return HPD_PORT_A + port - PORT_A;
4292da51e4baSVille Syrjälä }
4293da51e4baSVille Syrjälä 
4294c8455098SLyude Paul static enum hpd_pin skl_hpd_pin(struct drm_i915_private *dev_priv, enum port port)
4295c8455098SLyude Paul {
4296c8455098SLyude Paul 	if (HAS_PCH_TGP(dev_priv))
4297c8455098SLyude Paul 		return icl_hpd_pin(dev_priv, port);
4298c8455098SLyude Paul 
4299c8455098SLyude Paul 	return HPD_PORT_A + port - PORT_A;
4300c8455098SLyude Paul }
4301c8455098SLyude Paul 
430236ecb0ecSVille Syrjälä static bool intel_ddi_is_tc(struct drm_i915_private *i915, enum port port)
430336ecb0ecSVille Syrjälä {
4304005e9537SMatt Roper 	if (DISPLAY_VER(i915) >= 12)
430536ecb0ecSVille Syrjälä 		return port >= PORT_TC1;
4306005e9537SMatt Roper 	else if (DISPLAY_VER(i915) >= 11)
430736ecb0ecSVille Syrjälä 		return port >= PORT_C;
430836ecb0ecSVille Syrjälä 	else
430936ecb0ecSVille Syrjälä 		return false;
431036ecb0ecSVille Syrjälä }
431136ecb0ecSVille Syrjälä 
4312151ec347SImre Deak static void intel_ddi_encoder_suspend(struct intel_encoder *encoder)
4313151ec347SImre Deak {
4314151ec347SImre Deak 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4315151ec347SImre Deak 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4316151ec347SImre Deak 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4317151ec347SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4318151ec347SImre Deak 
4319151ec347SImre Deak 	intel_dp_encoder_suspend(encoder);
4320151ec347SImre Deak 
4321151ec347SImre Deak 	if (!intel_phy_is_tc(i915, phy))
4322151ec347SImre Deak 		return;
4323151ec347SImre Deak 
43243e0abc76SImre Deak 	intel_tc_port_flush_work(dig_port);
4325151ec347SImre Deak }
4326151ec347SImre Deak 
4327151ec347SImre Deak static void intel_ddi_encoder_shutdown(struct intel_encoder *encoder)
4328151ec347SImre Deak {
4329151ec347SImre Deak 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4330151ec347SImre Deak 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4331151ec347SImre Deak 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4332151ec347SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4333151ec347SImre Deak 
4334151ec347SImre Deak 	intel_dp_encoder_shutdown(encoder);
433549c55f7bSVille Syrjälä 	intel_hdmi_encoder_shutdown(encoder);
4336151ec347SImre Deak 
4337151ec347SImre Deak 	if (!intel_phy_is_tc(i915, phy))
4338151ec347SImre Deak 		return;
4339151ec347SImre Deak 
43403e0abc76SImre Deak 	intel_tc_port_flush_work(dig_port);
4341151ec347SImre Deak }
4342151ec347SImre Deak 
434383566d13SVille Syrjälä #define port_tc_name(port) ((port) - PORT_TC1 + '1')
434483566d13SVille Syrjälä #define tc_port_name(tc_port) ((tc_port) - TC_PORT_1 + '1')
434583566d13SVille Syrjälä 
4346379bc100SJani Nikula void intel_ddi_init(struct drm_i915_private *dev_priv, enum port port)
4347379bc100SJani Nikula {
43487801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port;
434970dfbc29SLucas De Marchi 	struct intel_encoder *encoder;
435045c0673aSJani Nikula 	const struct intel_bios_encoder_data *devdata;
4351f542d671SKai-Heng Feng 	bool init_hdmi, init_dp;
4352d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
4353379bc100SJani Nikula 
4354ddff9a60SMatt Roper 	/*
4355ddff9a60SMatt Roper 	 * On platforms with HTI (aka HDPORT), if it's enabled at boot it may
4356ddff9a60SMatt Roper 	 * have taken over some of the PHYs and made them unavailable to the
4357ddff9a60SMatt Roper 	 * driver.  In that case we should skip initializing the corresponding
4358ddff9a60SMatt Roper 	 * outputs.
4359ddff9a60SMatt Roper 	 */
4360ddff9a60SMatt Roper 	if (hti_uses_phy(dev_priv, phy)) {
4361ddff9a60SMatt Roper 		drm_dbg_kms(&dev_priv->drm, "PORT %c / PHY %c reserved by HTI\n",
4362ddff9a60SMatt Roper 			    port_name(port), phy_name(phy));
4363ddff9a60SMatt Roper 		return;
4364ddff9a60SMatt Roper 	}
4365ddff9a60SMatt Roper 
436645c0673aSJani Nikula 	devdata = intel_bios_encoder_data_lookup(dev_priv, port);
436745c0673aSJani Nikula 	if (!devdata) {
436845c0673aSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
436945c0673aSJani Nikula 			    "VBT says port %c is not present\n",
437045c0673aSJani Nikula 			    port_name(port));
437145c0673aSJani Nikula 		return;
437245c0673aSJani Nikula 	}
437345c0673aSJani Nikula 
437445c0673aSJani Nikula 	init_hdmi = intel_bios_encoder_supports_dvi(devdata) ||
437545c0673aSJani Nikula 		intel_bios_encoder_supports_hdmi(devdata);
437645c0673aSJani Nikula 	init_dp = intel_bios_encoder_supports_dp(devdata);
4377379bc100SJani Nikula 
4378379bc100SJani Nikula 	if (intel_bios_is_lspcon_present(dev_priv, port)) {
4379379bc100SJani Nikula 		/*
4380379bc100SJani Nikula 		 * Lspcon device needs to be driven with DP connector
4381379bc100SJani Nikula 		 * with special detection sequence. So make sure DP
4382379bc100SJani Nikula 		 * is initialized before lspcon.
4383379bc100SJani Nikula 		 */
4384379bc100SJani Nikula 		init_dp = true;
4385379bc100SJani Nikula 		init_hdmi = false;
438647bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm, "VBT says port %c has lspcon\n",
438747bdb1caSJani Nikula 			    port_name(port));
4388379bc100SJani Nikula 	}
4389379bc100SJani Nikula 
4390379bc100SJani Nikula 	if (!init_dp && !init_hdmi) {
439147bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
439247bdb1caSJani Nikula 			    "VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
4393379bc100SJani Nikula 			    port_name(port));
4394379bc100SJani Nikula 		return;
4395379bc100SJani Nikula 	}
4396379bc100SJani Nikula 
43977801f3b7SLucas De Marchi 	dig_port = kzalloc(sizeof(*dig_port), GFP_KERNEL);
43987801f3b7SLucas De Marchi 	if (!dig_port)
4399379bc100SJani Nikula 		return;
4400379bc100SJani Nikula 
44017801f3b7SLucas De Marchi 	encoder = &dig_port->base;
4402c0a950d1SJani Nikula 	encoder->devdata = devdata;
4403379bc100SJani Nikula 
4404ed2615a8SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 13 && port >= PORT_D_XELPD) {
4405ed2615a8SMatt Roper 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
4406ed2615a8SMatt Roper 				 DRM_MODE_ENCODER_TMDS,
4407ed2615a8SMatt Roper 				 "DDI %c/PHY %c",
4408ed2615a8SMatt Roper 				 port_name(port - PORT_D_XELPD + PORT_D),
4409ed2615a8SMatt Roper 				 phy_name(phy));
4410ed2615a8SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 12) {
44112d709a5aSVille Syrjälä 		enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
44122d709a5aSVille Syrjälä 
441370dfbc29SLucas De Marchi 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
44142d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
44152d709a5aSVille Syrjälä 				 "DDI %s%c/PHY %s%c",
44162d709a5aSVille Syrjälä 				 port >= PORT_TC1 ? "TC" : "",
441783566d13SVille Syrjälä 				 port >= PORT_TC1 ? port_tc_name(port) : port_name(port),
44182d709a5aSVille Syrjälä 				 tc_port != TC_PORT_NONE ? "TC" : "",
441983566d13SVille Syrjälä 				 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
4420005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 11) {
44212d709a5aSVille Syrjälä 		enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
44222d709a5aSVille Syrjälä 
44232d709a5aSVille Syrjälä 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
44242d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
44252d709a5aSVille Syrjälä 				 "DDI %c%s/PHY %s%c",
44262d709a5aSVille Syrjälä 				 port_name(port),
44272d709a5aSVille Syrjälä 				 port >= PORT_C ? " (TC)" : "",
44282d709a5aSVille Syrjälä 				 tc_port != TC_PORT_NONE ? "TC" : "",
442983566d13SVille Syrjälä 				 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
44302d709a5aSVille Syrjälä 	} else {
44312d709a5aSVille Syrjälä 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
44322d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
44332d709a5aSVille Syrjälä 				 "DDI %c/PHY %c", port_name(port),  phy_name(phy));
44342d709a5aSVille Syrjälä 	}
4435379bc100SJani Nikula 
443636e5e704SSean Paul 	mutex_init(&dig_port->hdcp_mutex);
443736e5e704SSean Paul 	dig_port->num_hdcp_streams = 0;
443836e5e704SSean Paul 
443970dfbc29SLucas De Marchi 	encoder->hotplug = intel_ddi_hotplug;
444070dfbc29SLucas De Marchi 	encoder->compute_output_type = intel_ddi_compute_output_type;
444170dfbc29SLucas De Marchi 	encoder->compute_config = intel_ddi_compute_config;
4442b50a1aa6SManasi Navare 	encoder->compute_config_late = intel_ddi_compute_config_late;
444370dfbc29SLucas De Marchi 	encoder->enable = intel_enable_ddi;
444470dfbc29SLucas De Marchi 	encoder->pre_pll_enable = intel_ddi_pre_pll_enable;
444570dfbc29SLucas De Marchi 	encoder->pre_enable = intel_ddi_pre_enable;
444670dfbc29SLucas De Marchi 	encoder->disable = intel_disable_ddi;
444770dfbc29SLucas De Marchi 	encoder->post_disable = intel_ddi_post_disable;
444870dfbc29SLucas De Marchi 	encoder->update_pipe = intel_ddi_update_pipe;
444970dfbc29SLucas De Marchi 	encoder->get_hw_state = intel_ddi_get_hw_state;
4450f9e76a6eSImre Deak 	encoder->sync_state = intel_ddi_sync_state;
4451b671d6efSImre Deak 	encoder->initial_fastset_check = intel_ddi_initial_fastset_check;
4452151ec347SImre Deak 	encoder->suspend = intel_ddi_encoder_suspend;
4453151ec347SImre Deak 	encoder->shutdown = intel_ddi_encoder_shutdown;
445470dfbc29SLucas De Marchi 	encoder->get_power_domains = intel_ddi_get_power_domains;
445570dfbc29SLucas De Marchi 
445670dfbc29SLucas De Marchi 	encoder->type = INTEL_OUTPUT_DDI;
445770dfbc29SLucas De Marchi 	encoder->power_domain = intel_port_to_power_domain(port);
445870dfbc29SLucas De Marchi 	encoder->port = port;
445970dfbc29SLucas De Marchi 	encoder->cloneable = 0;
446070dfbc29SLucas De Marchi 	encoder->pipe_mask = ~0;
4461da51e4baSVille Syrjälä 
4462865b73eaSMatt Roper 	if (IS_DG2(dev_priv)) {
4463f82f2563SMatt Roper 		encoder->enable_clock = intel_mpllb_enable;
4464f82f2563SMatt Roper 		encoder->disable_clock = intel_mpllb_disable;
4465865b73eaSMatt Roper 		encoder->get_config = dg2_ddi_get_config;
4466865b73eaSMatt Roper 	} else if (IS_ALDERLAKE_S(dev_priv)) {
446740b316d4SVille Syrjälä 		encoder->enable_clock = adls_ddi_enable_clock;
446840b316d4SVille Syrjälä 		encoder->disable_clock = adls_ddi_disable_clock;
44690fbd8694SVille Syrjälä 		encoder->is_clock_enabled = adls_ddi_is_clock_enabled;
4470351221ffSVille Syrjälä 		encoder->get_config = adls_ddi_get_config;
447140b316d4SVille Syrjälä 	} else if (IS_ROCKETLAKE(dev_priv)) {
447240b316d4SVille Syrjälä 		encoder->enable_clock = rkl_ddi_enable_clock;
447340b316d4SVille Syrjälä 		encoder->disable_clock = rkl_ddi_disable_clock;
44740fbd8694SVille Syrjälä 		encoder->is_clock_enabled = rkl_ddi_is_clock_enabled;
4475351221ffSVille Syrjälä 		encoder->get_config = rkl_ddi_get_config;
447636ecb0ecSVille Syrjälä 	} else if (IS_DG1(dev_priv)) {
447735bb6b1aSVille Syrjälä 		encoder->enable_clock = dg1_ddi_enable_clock;
447835bb6b1aSVille Syrjälä 		encoder->disable_clock = dg1_ddi_disable_clock;
44790fbd8694SVille Syrjälä 		encoder->is_clock_enabled = dg1_ddi_is_clock_enabled;
4480351221ffSVille Syrjälä 		encoder->get_config = dg1_ddi_get_config;
448136ecb0ecSVille Syrjälä 	} else if (IS_JSL_EHL(dev_priv)) {
448236ecb0ecSVille Syrjälä 		if (intel_ddi_is_tc(dev_priv, port)) {
448336ecb0ecSVille Syrjälä 			encoder->enable_clock = jsl_ddi_tc_enable_clock;
448436ecb0ecSVille Syrjälä 			encoder->disable_clock = jsl_ddi_tc_disable_clock;
44850fbd8694SVille Syrjälä 			encoder->is_clock_enabled = jsl_ddi_tc_is_clock_enabled;
4486351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
448736ecb0ecSVille Syrjälä 		} else {
448836ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_combo_enable_clock;
448936ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_combo_disable_clock;
44900fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
4491351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
449236ecb0ecSVille Syrjälä 		}
4493005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 11) {
449436ecb0ecSVille Syrjälä 		if (intel_ddi_is_tc(dev_priv, port)) {
449536ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_tc_enable_clock;
449636ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_tc_disable_clock;
44970fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_tc_is_clock_enabled;
4498351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_tc_get_config;
449936ecb0ecSVille Syrjälä 		} else {
450036ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_combo_enable_clock;
450136ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_combo_disable_clock;
45020fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
4503351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
450436ecb0ecSVille Syrjälä 		}
45052446e1d6SMatt Roper 	} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
4506351221ffSVille Syrjälä 		/* BXT/GLK have fixed PLL->port mapping */
4507351221ffSVille Syrjälä 		encoder->get_config = bxt_ddi_get_config;
450893e7e61eSLucas De Marchi 	} else if (DISPLAY_VER(dev_priv) == 9) {
450938e31f1aSVille Syrjälä 		encoder->enable_clock = skl_ddi_enable_clock;
451038e31f1aSVille Syrjälä 		encoder->disable_clock = skl_ddi_disable_clock;
45110fbd8694SVille Syrjälä 		encoder->is_clock_enabled = skl_ddi_is_clock_enabled;
4512351221ffSVille Syrjälä 		encoder->get_config = skl_ddi_get_config;
451338e31f1aSVille Syrjälä 	} else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
4514d135368dSVille Syrjälä 		encoder->enable_clock = hsw_ddi_enable_clock;
4515d135368dSVille Syrjälä 		encoder->disable_clock = hsw_ddi_disable_clock;
45160fbd8694SVille Syrjälä 		encoder->is_clock_enabled = hsw_ddi_is_clock_enabled;
4517351221ffSVille Syrjälä 		encoder->get_config = hsw_ddi_get_config;
4518d135368dSVille Syrjälä 	}
4519d135368dSVille Syrjälä 
4520193299adSVille Syrjälä 	if (IS_DG2(dev_priv)) {
4521193299adSVille Syrjälä 		encoder->set_signal_levels = intel_snps_phy_set_signal_levels;
4522193299adSVille Syrjälä 	} else if (DISPLAY_VER(dev_priv) >= 12) {
4523193299adSVille Syrjälä 		if (intel_phy_is_combo(dev_priv, phy))
4524193299adSVille Syrjälä 			encoder->set_signal_levels = icl_combo_phy_set_signal_levels;
4525e722ab8bSVille Syrjälä 		else
4526193299adSVille Syrjälä 			encoder->set_signal_levels = tgl_dkl_phy_set_signal_levels;
4527193299adSVille Syrjälä 	} else if (DISPLAY_VER(dev_priv) >= 11) {
4528193299adSVille Syrjälä 		if (intel_phy_is_combo(dev_priv, phy))
4529193299adSVille Syrjälä 			encoder->set_signal_levels = icl_combo_phy_set_signal_levels;
4530193299adSVille Syrjälä 		else
4531193299adSVille Syrjälä 			encoder->set_signal_levels = icl_mg_phy_set_signal_levels;
4532193299adSVille Syrjälä 	} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
45335f5ada0bSVille Syrjälä 		encoder->set_signal_levels = bxt_ddi_phy_set_signal_levels;
4534193299adSVille Syrjälä 	} else {
4535e722ab8bSVille Syrjälä 		encoder->set_signal_levels = hsw_set_signal_levels;
4536193299adSVille Syrjälä 	}
4537e722ab8bSVille Syrjälä 
4538c40a253bSVille Syrjälä 	intel_ddi_buf_trans_init(encoder);
4539c40a253bSVille Syrjälä 
4540ed2615a8SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 13)
4541ed2615a8SMatt Roper 		encoder->hpd_pin = xelpd_hpd_pin(dev_priv, port);
4542ed2615a8SMatt Roper 	else if (IS_DG1(dev_priv))
4543229f31e2SLucas De Marchi 		encoder->hpd_pin = dg1_hpd_pin(dev_priv, port);
4544229f31e2SLucas De Marchi 	else if (IS_ROCKETLAKE(dev_priv))
4545da51e4baSVille Syrjälä 		encoder->hpd_pin = rkl_hpd_pin(dev_priv, port);
4546005e9537SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 12)
4547da51e4baSVille Syrjälä 		encoder->hpd_pin = tgl_hpd_pin(dev_priv, port);
454824ea098bSTejas Upadhyay 	else if (IS_JSL_EHL(dev_priv))
4549da51e4baSVille Syrjälä 		encoder->hpd_pin = ehl_hpd_pin(dev_priv, port);
455093e7e61eSLucas De Marchi 	else if (DISPLAY_VER(dev_priv) == 11)
4551da51e4baSVille Syrjälä 		encoder->hpd_pin = icl_hpd_pin(dev_priv, port);
455293e7e61eSLucas De Marchi 	else if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv))
4553c8455098SLyude Paul 		encoder->hpd_pin = skl_hpd_pin(dev_priv, port);
4554da51e4baSVille Syrjälä 	else
455503c7e4f1SVille Syrjälä 		encoder->hpd_pin = intel_hpd_pin_default(dev_priv, port);
4556379bc100SJani Nikula 
4557005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
45587801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
45597801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
45607801f3b7SLucas De Marchi 			& DDI_BUF_PORT_REVERSAL;
4561379bc100SJani Nikula 	else
45627801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
45637801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
45647801f3b7SLucas De Marchi 			& (DDI_BUF_PORT_REVERSAL | DDI_A_4_LANES);
456570dfbc29SLucas De Marchi 
4566aaab24bbSUma Shankar 	if (intel_bios_is_lane_reversal_needed(dev_priv, port))
4567aaab24bbSUma Shankar 		dig_port->saved_port_bits |= DDI_BUF_PORT_REVERSAL;
4568aaab24bbSUma Shankar 
45697801f3b7SLucas De Marchi 	dig_port->dp.output_reg = INVALID_MMIO_REG;
45707801f3b7SLucas De Marchi 	dig_port->max_lanes = intel_ddi_max_lanes(dig_port);
45717801f3b7SLucas De Marchi 	dig_port->aux_ch = intel_bios_port_aux_ch(dev_priv, port);
4572379bc100SJani Nikula 
4573d8fe2ab6SMatt Roper 	if (intel_phy_is_tc(dev_priv, phy)) {
4574c5faae5aSJani Nikula 		bool is_legacy =
4575f08fbe6aSJani Nikula 			!intel_bios_encoder_supports_typec_usb(devdata) &&
4576f08fbe6aSJani Nikula 			!intel_bios_encoder_supports_tbt(devdata);
4577379bc100SJani Nikula 
45787801f3b7SLucas De Marchi 		intel_tc_port_init(dig_port, is_legacy);
457924a7bfe0SImre Deak 
458070dfbc29SLucas De Marchi 		encoder->update_prepare = intel_ddi_update_prepare;
458170dfbc29SLucas De Marchi 		encoder->update_complete = intel_ddi_update_complete;
4582ab7bc4e1SImre Deak 	}
4583ab7bc4e1SImre Deak 
45841de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, port > PORT_I);
45857801f3b7SLucas De Marchi 	dig_port->ddi_io_power_domain = POWER_DOMAIN_PORT_DDI_A_IO +
4586327f8d8cSLucas De Marchi 					      port - PORT_A;
4587379bc100SJani Nikula 
4588379bc100SJani Nikula 	if (init_dp) {
45897801f3b7SLucas De Marchi 		if (!intel_ddi_init_dp_connector(dig_port))
4590379bc100SJani Nikula 			goto err;
4591379bc100SJani Nikula 
45927801f3b7SLucas De Marchi 		dig_port->hpd_pulse = intel_dp_hpd_pulse;
4593bc71194eSJani Nikula 
4594f6864b27SJani Nikula 		if (dig_port->dp.mso_link_count)
4595f6864b27SJani Nikula 			encoder->pipe_mask = intel_ddi_splitter_pipe_mask(dev_priv);
4596379bc100SJani Nikula 	}
4597379bc100SJani Nikula 
4598379bc100SJani Nikula 	/* In theory we don't need the encoder->type check, but leave it just in
4599379bc100SJani Nikula 	 * case we have some really bad VBTs... */
460070dfbc29SLucas De Marchi 	if (encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
46017801f3b7SLucas De Marchi 		if (!intel_ddi_init_hdmi_connector(dig_port))
4602379bc100SJani Nikula 			goto err;
4603379bc100SJani Nikula 	}
4604379bc100SJani Nikula 
4605005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
4606edc0e09cSVille Syrjälä 		if (intel_phy_is_tc(dev_priv, phy))
46077801f3b7SLucas De Marchi 			dig_port->connected = intel_tc_port_connected;
4608edc0e09cSVille Syrjälä 		else
46097801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4610005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 8) {
46112446e1d6SMatt Roper 		if (port == PORT_A || IS_GEMINILAKE(dev_priv) ||
46122446e1d6SMatt Roper 		    IS_BROXTON(dev_priv))
46137801f3b7SLucas De Marchi 			dig_port->connected = bdw_digital_port_connected;
4614edc0e09cSVille Syrjälä 		else
46157801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4616edc0e09cSVille Syrjälä 	} else {
4617c7e8a3d6SVille Syrjälä 		if (port == PORT_A)
46187801f3b7SLucas De Marchi 			dig_port->connected = hsw_digital_port_connected;
4619edc0e09cSVille Syrjälä 		else
46207801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4621edc0e09cSVille Syrjälä 	}
4622edc0e09cSVille Syrjälä 
46237801f3b7SLucas De Marchi 	intel_infoframe_init(dig_port);
4624379bc100SJani Nikula 
4625379bc100SJani Nikula 	return;
4626379bc100SJani Nikula 
4627379bc100SJani Nikula err:
462870dfbc29SLucas De Marchi 	drm_encoder_cleanup(&encoder->base);
46297801f3b7SLucas De Marchi 	kfree(dig_port);
4630379bc100SJani Nikula }
4631